Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Universal Flash Storage Host controller driver |
| 3 | * |
| 4 | * This code is based on drivers/scsi/ufs/ufshcd.h |
| 5 | * Copyright (C) 2011-2013 Samsung India Software Operations |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 6 | * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved. |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 7 | * |
| 8 | * Authors: |
| 9 | * Santosh Yaraganavi <santosh.sy@samsung.com> |
| 10 | * Vinayak Holikatti <h.vinayak@samsung.com> |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License |
| 14 | * as published by the Free Software Foundation; either version 2 |
| 15 | * of the License, or (at your option) any later version. |
| 16 | * See the COPYING file in the top-level directory or visit |
| 17 | * <http://www.gnu.org/licenses/gpl-2.0.html> |
| 18 | * |
| 19 | * This program is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * This program is provided "AS IS" and "WITH ALL FAULTS" and |
| 25 | * without warranty of any kind. You are solely responsible for |
| 26 | * determining the appropriateness of using and distributing |
| 27 | * the program and assume all risks associated with your exercise |
| 28 | * of rights with respect to the program, including but not limited |
| 29 | * to infringement of third party rights, the risks and costs of |
| 30 | * program errors, damage to or loss of data, programs or equipment, |
| 31 | * and unavailability or interruption of operations. Under no |
| 32 | * circumstances will the contributor of this Program be liable for |
| 33 | * any damages of any kind arising from your use or distribution of |
| 34 | * this program. |
| 35 | */ |
| 36 | |
| 37 | #ifndef _UFSHCD_H |
| 38 | #define _UFSHCD_H |
| 39 | |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/kernel.h> |
| 42 | #include <linux/init.h> |
| 43 | #include <linux/interrupt.h> |
| 44 | #include <linux/io.h> |
| 45 | #include <linux/delay.h> |
| 46 | #include <linux/slab.h> |
| 47 | #include <linux/spinlock.h> |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 48 | #include <linux/rwsem.h> |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 49 | #include <linux/workqueue.h> |
| 50 | #include <linux/errno.h> |
| 51 | #include <linux/types.h> |
| 52 | #include <linux/wait.h> |
| 53 | #include <linux/bitops.h> |
| 54 | #include <linux/pm_runtime.h> |
| 55 | #include <linux/clk.h> |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 56 | #include <linux/completion.h> |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 57 | #include <linux/regulator/consumer.h> |
Stanley Chu | 5a244e0 | 2020-01-29 18:52:50 +0800 | [diff] [blame] | 58 | #include <linux/bitfield.h> |
Asutosh Das | 2c75f9a | 2020-03-25 11:29:01 -0700 | [diff] [blame] | 59 | #include <linux/devfreq.h> |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 60 | #include <linux/keyslot-manager.h> |
Yaniv Gardi | f37aabc | 2016-03-10 17:37:20 +0200 | [diff] [blame] | 61 | #include "unipro.h" |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 62 | |
| 63 | #include <asm/irq.h> |
| 64 | #include <asm/byteorder.h> |
| 65 | #include <scsi/scsi.h> |
| 66 | #include <scsi/scsi_cmnd.h> |
| 67 | #include <scsi/scsi_host.h> |
| 68 | #include <scsi/scsi_tcq.h> |
| 69 | #include <scsi/scsi_dbg.h> |
| 70 | #include <scsi/scsi_eh.h> |
| 71 | |
| 72 | #include "ufs.h" |
Stanley Chu | c28c00b | 2020-05-08 16:01:09 +0800 | [diff] [blame] | 73 | #include "ufs_quirks.h" |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 74 | #include "ufshci.h" |
| 75 | |
| 76 | #define UFSHCD "ufshcd" |
| 77 | #define UFSHCD_DRIVER_VERSION "0.2" |
| 78 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 79 | struct ufs_hba; |
| 80 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 81 | enum dev_cmd_type { |
| 82 | DEV_CMD_TYPE_NOP = 0x0, |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 83 | DEV_CMD_TYPE_QUERY = 0x1, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 84 | }; |
| 85 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 86 | /** |
| 87 | * struct uic_command - UIC command structure |
| 88 | * @command: UIC command |
| 89 | * @argument1: UIC command argument 1 |
| 90 | * @argument2: UIC command argument 2 |
| 91 | * @argument3: UIC command argument 3 |
| 92 | * @cmd_active: Indicate if UIC command is outstanding |
| 93 | * @result: UIC command result |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 94 | * @done: UIC command completion |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 95 | */ |
| 96 | struct uic_command { |
| 97 | u32 command; |
| 98 | u32 argument1; |
| 99 | u32 argument2; |
| 100 | u32 argument3; |
| 101 | int cmd_active; |
| 102 | int result; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 103 | struct completion done; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 104 | }; |
| 105 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 106 | /* Used to differentiate the power management options */ |
| 107 | enum ufs_pm_op { |
| 108 | UFS_RUNTIME_PM, |
| 109 | UFS_SYSTEM_PM, |
| 110 | UFS_SHUTDOWN_PM, |
| 111 | }; |
| 112 | |
| 113 | #define ufshcd_is_runtime_pm(op) ((op) == UFS_RUNTIME_PM) |
| 114 | #define ufshcd_is_system_pm(op) ((op) == UFS_SYSTEM_PM) |
| 115 | #define ufshcd_is_shutdown_pm(op) ((op) == UFS_SHUTDOWN_PM) |
| 116 | |
| 117 | /* Host <-> Device UniPro Link state */ |
| 118 | enum uic_link_state { |
| 119 | UIC_LINK_OFF_STATE = 0, /* Link powered down or disabled */ |
| 120 | UIC_LINK_ACTIVE_STATE = 1, /* Link is in Fast/Slow/Sleep state */ |
| 121 | UIC_LINK_HIBERN8_STATE = 2, /* Link is in Hibernate state */ |
| 122 | }; |
| 123 | |
| 124 | #define ufshcd_is_link_off(hba) ((hba)->uic_link_state == UIC_LINK_OFF_STATE) |
| 125 | #define ufshcd_is_link_active(hba) ((hba)->uic_link_state == \ |
| 126 | UIC_LINK_ACTIVE_STATE) |
| 127 | #define ufshcd_is_link_hibern8(hba) ((hba)->uic_link_state == \ |
| 128 | UIC_LINK_HIBERN8_STATE) |
| 129 | #define ufshcd_set_link_off(hba) ((hba)->uic_link_state = UIC_LINK_OFF_STATE) |
| 130 | #define ufshcd_set_link_active(hba) ((hba)->uic_link_state = \ |
| 131 | UIC_LINK_ACTIVE_STATE) |
| 132 | #define ufshcd_set_link_hibern8(hba) ((hba)->uic_link_state = \ |
| 133 | UIC_LINK_HIBERN8_STATE) |
| 134 | |
Stanley Chu | 1764fa2 | 2020-03-27 17:58:35 +0800 | [diff] [blame] | 135 | #define ufshcd_set_ufs_dev_active(h) \ |
| 136 | ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE) |
| 137 | #define ufshcd_set_ufs_dev_sleep(h) \ |
| 138 | ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE) |
| 139 | #define ufshcd_set_ufs_dev_poweroff(h) \ |
| 140 | ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE) |
| 141 | #define ufshcd_is_ufs_dev_active(h) \ |
| 142 | ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE) |
| 143 | #define ufshcd_is_ufs_dev_sleep(h) \ |
| 144 | ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE) |
| 145 | #define ufshcd_is_ufs_dev_poweroff(h) \ |
| 146 | ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE) |
| 147 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 148 | /* |
| 149 | * UFS Power management levels. |
| 150 | * Each level is in increasing order of power savings. |
| 151 | */ |
| 152 | enum ufs_pm_level { |
| 153 | UFS_PM_LVL_0, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE */ |
| 154 | UFS_PM_LVL_1, /* UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE */ |
| 155 | UFS_PM_LVL_2, /* UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE */ |
| 156 | UFS_PM_LVL_3, /* UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE */ |
| 157 | UFS_PM_LVL_4, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE */ |
| 158 | UFS_PM_LVL_5, /* UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE */ |
| 159 | UFS_PM_LVL_MAX |
| 160 | }; |
| 161 | |
| 162 | struct ufs_pm_lvl_states { |
| 163 | enum ufs_dev_pwr_mode dev_state; |
| 164 | enum uic_link_state link_state; |
| 165 | }; |
| 166 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 167 | /** |
| 168 | * struct ufshcd_lrb - local reference block |
| 169 | * @utr_descriptor_ptr: UTRD address of the command |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 170 | * @ucd_req_ptr: UCD address of the command |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 171 | * @ucd_rsp_ptr: Response UPIU address for this command |
| 172 | * @ucd_prdt_ptr: PRDT address of the command |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 173 | * @utrd_dma_addr: UTRD dma address for debug |
| 174 | * @ucd_prdt_dma_addr: PRDT dma address for debug |
| 175 | * @ucd_rsp_dma_addr: UPIU response dma address for debug |
| 176 | * @ucd_req_dma_addr: UPIU request dma address for debug |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 177 | * @cmd: pointer to SCSI command |
| 178 | * @sense_buffer: pointer to sense buffer address of the SCSI command |
| 179 | * @sense_bufflen: Length of the sense buffer |
| 180 | * @scsi_status: SCSI status of the command |
| 181 | * @command_type: SCSI, UFS, Query. |
| 182 | * @task_tag: Task tag of the command |
| 183 | * @lun: LUN of the command |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 184 | * @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation) |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 185 | * @issue_time_stamp: time stamp for debug purposes |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 186 | * @compl_time_stamp: time stamp for statistics |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 187 | * @crypto_key_slot: the key slot to use for inline crypto (-1 if none) |
Satya Tangirala | 53d5b29 | 2019-10-24 14:44:28 -0700 | [diff] [blame] | 188 | * @data_unit_num: the data unit number for the first block for inline crypto |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 189 | * @req_abort_skip: skip request abort task flag |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 190 | */ |
| 191 | struct ufshcd_lrb { |
| 192 | struct utp_transfer_req_desc *utr_descriptor_ptr; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 193 | struct utp_upiu_req *ucd_req_ptr; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 194 | struct utp_upiu_rsp *ucd_rsp_ptr; |
| 195 | struct ufshcd_sg_entry *ucd_prdt_ptr; |
| 196 | |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 197 | dma_addr_t utrd_dma_addr; |
| 198 | dma_addr_t ucd_req_dma_addr; |
| 199 | dma_addr_t ucd_rsp_dma_addr; |
| 200 | dma_addr_t ucd_prdt_dma_addr; |
| 201 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 202 | struct scsi_cmnd *cmd; |
| 203 | u8 *sense_buffer; |
| 204 | unsigned int sense_bufflen; |
| 205 | int scsi_status; |
| 206 | |
| 207 | int command_type; |
| 208 | int task_tag; |
Subhash Jadavani | 0ce147d | 2014-09-25 15:32:29 +0300 | [diff] [blame] | 209 | u8 lun; /* UPIU LUN id field is only 8-bit wide */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 210 | bool intr_cmd; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 211 | ktime_t issue_time_stamp; |
Zang Leigang | 0901718 | 2017-09-27 10:06:06 +0800 | [diff] [blame] | 212 | ktime_t compl_time_stamp; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 213 | #ifdef CONFIG_SCSI_UFS_CRYPTO |
| 214 | int crypto_key_slot; |
Satya Tangirala | 53d5b29 | 2019-10-24 14:44:28 -0700 | [diff] [blame] | 215 | u64 data_unit_num; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 216 | #endif |
Gilad Broner | e0b299e | 2017-02-03 16:56:40 -0800 | [diff] [blame] | 217 | |
| 218 | bool req_abort_skip; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 219 | }; |
| 220 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 221 | /** |
Tomas Winkler | a230c2f | 2016-02-09 10:25:41 +0200 | [diff] [blame] | 222 | * struct ufs_query - holds relevant data structures for query request |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 223 | * @request: request upiu and function |
| 224 | * @descriptor: buffer for sending/receiving descriptor |
| 225 | * @response: response upiu and response |
| 226 | */ |
| 227 | struct ufs_query { |
| 228 | struct ufs_query_req request; |
| 229 | u8 *descriptor; |
| 230 | struct ufs_query_res response; |
| 231 | }; |
| 232 | |
| 233 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 234 | * struct ufs_dev_cmd - all assosiated fields with device management commands |
| 235 | * @type: device management command type - Query, NOP OUT |
| 236 | * @lock: lock to allow one command at a time |
| 237 | * @complete: internal commands completion |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 238 | */ |
| 239 | struct ufs_dev_cmd { |
| 240 | enum dev_cmd_type type; |
| 241 | struct mutex lock; |
| 242 | struct completion *complete; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 243 | struct ufs_query query; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 244 | }; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 245 | |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 246 | struct ufs_desc_size { |
| 247 | int dev_desc; |
| 248 | int pwr_desc; |
| 249 | int geom_desc; |
| 250 | int interc_desc; |
| 251 | int unit_desc; |
| 252 | int conf_desc; |
Stanislav Nijnikov | c648c2d | 2018-02-15 14:14:05 +0200 | [diff] [blame] | 253 | int hlth_desc; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 256 | /** |
| 257 | * struct ufs_clk_info - UFS clock related info |
| 258 | * @list: list headed by hba->clk_list_head |
| 259 | * @clk: clock node |
| 260 | * @name: clock name |
| 261 | * @max_freq: maximum frequency supported by the clock |
Sahitya Tummala | 4cff6d99 | 2014-09-25 15:32:33 +0300 | [diff] [blame] | 262 | * @min_freq: min frequency that can be used for clock scaling |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 263 | * @curr_freq: indicates the current frequency that it is set to |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 264 | * @enabled: variable to check against multiple enable/disable |
| 265 | */ |
| 266 | struct ufs_clk_info { |
| 267 | struct list_head list; |
| 268 | struct clk *clk; |
| 269 | const char *name; |
| 270 | u32 max_freq; |
Sahitya Tummala | 4cff6d99 | 2014-09-25 15:32:33 +0300 | [diff] [blame] | 271 | u32 min_freq; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 272 | u32 curr_freq; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 273 | bool enabled; |
| 274 | }; |
| 275 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 276 | enum ufs_notify_change_status { |
| 277 | PRE_CHANGE, |
| 278 | POST_CHANGE, |
| 279 | }; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 280 | |
| 281 | struct ufs_pa_layer_attr { |
| 282 | u32 gear_rx; |
| 283 | u32 gear_tx; |
| 284 | u32 lane_rx; |
| 285 | u32 lane_tx; |
| 286 | u32 pwr_rx; |
| 287 | u32 pwr_tx; |
| 288 | u32 hs_rate; |
| 289 | }; |
| 290 | |
| 291 | struct ufs_pwr_mode_info { |
| 292 | bool is_valid; |
| 293 | struct ufs_pa_layer_attr info; |
| 294 | }; |
| 295 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 296 | /** |
| 297 | * struct ufs_hba_variant_ops - variant specific callbacks |
| 298 | * @name: variant name |
| 299 | * @init: called when the driver is initialized |
| 300 | * @exit: called to cleanup everything done in init |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 301 | * @get_ufs_hci_version: called to get UFS HCI version |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 302 | * @clk_scale_notify: notifies that clks are scaled up/down |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 303 | * @setup_clocks: called before touching any of the controller registers |
| 304 | * @setup_regulators: called before accessing the host controller |
| 305 | * @hce_enable_notify: called before and after HCE enable bit is set to allow |
| 306 | * variant specific Uni-Pro initialization. |
| 307 | * @link_startup_notify: called before and after Link startup is carried out |
| 308 | * to allow variant specific Uni-Pro initialization. |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 309 | * @pwr_change_notify: called before and after a power mode change |
| 310 | * is carried out to allow vendor spesific capabilities |
| 311 | * to be set. |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 312 | * @setup_xfer_req: called before any transfer request is issued |
| 313 | * to set some things |
Kiwoong Kim | d2877be | 2016-11-10 21:16:15 +0900 | [diff] [blame] | 314 | * @setup_task_mgmt: called before any task management request is issued |
| 315 | * to set some things |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 316 | * @hibern8_notify: called around hibern8 enter/exit |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 317 | * @apply_dev_quirks: called to apply device specific quirks |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 318 | * @suspend: called during host controller PM callback |
| 319 | * @resume: called during host controller PM callback |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 320 | * @dbg_register_dump: used to dump controller debug information |
Joao Pinto | 4b9ffb5 | 2016-05-11 12:21:30 +0100 | [diff] [blame] | 321 | * @phy_initialization: used to initialize phys |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 322 | * @device_reset: called to issue a reset pulse on the UFS device |
Eric Biggers | 27b2ed4 | 2020-07-14 09:31:24 -0700 | [diff] [blame^] | 323 | * @program_key: program or evict an inline encryption key |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 324 | */ |
| 325 | struct ufs_hba_variant_ops { |
| 326 | const char *name; |
| 327 | int (*init)(struct ufs_hba *); |
| 328 | void (*exit)(struct ufs_hba *); |
Yaniv Gardi | 9949e70 | 2015-05-17 18:55:05 +0300 | [diff] [blame] | 329 | u32 (*get_ufs_hci_version)(struct ufs_hba *); |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 330 | int (*clk_scale_notify)(struct ufs_hba *, bool, |
| 331 | enum ufs_notify_change_status); |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 332 | int (*setup_clocks)(struct ufs_hba *, bool, |
| 333 | enum ufs_notify_change_status); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 334 | int (*setup_regulators)(struct ufs_hba *, bool); |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 335 | int (*hce_enable_notify)(struct ufs_hba *, |
| 336 | enum ufs_notify_change_status); |
| 337 | int (*link_startup_notify)(struct ufs_hba *, |
| 338 | enum ufs_notify_change_status); |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 339 | int (*pwr_change_notify)(struct ufs_hba *, |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 340 | enum ufs_notify_change_status status, |
| 341 | struct ufs_pa_layer_attr *, |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 342 | struct ufs_pa_layer_attr *); |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 343 | void (*setup_xfer_req)(struct ufs_hba *, int, bool); |
Kiwoong Kim | d2877be | 2016-11-10 21:16:15 +0900 | [diff] [blame] | 344 | void (*setup_task_mgmt)(struct ufs_hba *, int, u8); |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 345 | void (*hibern8_notify)(struct ufs_hba *, enum uic_cmd_dme, |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 346 | enum ufs_notify_change_status); |
Bean Huo | 0975006 | 2020-01-20 14:08:14 +0100 | [diff] [blame] | 347 | int (*apply_dev_quirks)(struct ufs_hba *hba); |
Stanley Chu | c28c00b | 2020-05-08 16:01:09 +0800 | [diff] [blame] | 348 | void (*fixup_dev_quirks)(struct ufs_hba *hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 349 | int (*suspend)(struct ufs_hba *, enum ufs_pm_op); |
| 350 | int (*resume)(struct ufs_hba *, enum ufs_pm_op); |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 351 | void (*dbg_register_dump)(struct ufs_hba *hba); |
Joao Pinto | 4b9ffb5 | 2016-05-11 12:21:30 +0100 | [diff] [blame] | 352 | int (*phy_initialization)(struct ufs_hba *); |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 353 | void (*device_reset)(struct ufs_hba *hba); |
Asutosh Das | 2c75f9a | 2020-03-25 11:29:01 -0700 | [diff] [blame] | 354 | void (*config_scaling_param)(struct ufs_hba *hba, |
| 355 | struct devfreq_dev_profile *profile, |
| 356 | void *data); |
Eric Biggers | 884e364 | 2020-01-09 21:10:06 -0800 | [diff] [blame] | 357 | int (*program_key)(struct ufs_hba *hba, |
| 358 | const union ufs_crypto_cfg_entry *cfg, int slot); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 359 | }; |
| 360 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 361 | /* clock gating state */ |
| 362 | enum clk_gating_state { |
| 363 | CLKS_OFF, |
| 364 | CLKS_ON, |
| 365 | REQ_CLKS_OFF, |
| 366 | REQ_CLKS_ON, |
| 367 | }; |
| 368 | |
| 369 | /** |
| 370 | * struct ufs_clk_gating - UFS clock gating related info |
| 371 | * @gate_work: worker to turn off clocks after some delay as specified in |
| 372 | * delay_ms |
| 373 | * @ungate_work: worker to turn on clocks that will be used in case of |
| 374 | * interrupt context |
| 375 | * @state: the current clocks state |
| 376 | * @delay_ms: gating delay in ms |
| 377 | * @is_suspended: clk gating is suspended when set to 1 which can be used |
| 378 | * during suspend/resume |
| 379 | * @delay_attr: sysfs attribute to control delay_attr |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 380 | * @enable_attr: sysfs attribute to enable/disable clock gating |
| 381 | * @is_enabled: Indicates the current status of clock gating |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 382 | * @active_reqs: number of requests that are pending and should be waited for |
| 383 | * completion before gating clocks. |
| 384 | */ |
| 385 | struct ufs_clk_gating { |
| 386 | struct delayed_work gate_work; |
| 387 | struct work_struct ungate_work; |
| 388 | enum clk_gating_state state; |
| 389 | unsigned long delay_ms; |
| 390 | bool is_suspended; |
| 391 | struct device_attribute delay_attr; |
Sahitya Tummala | b427411 | 2016-12-22 18:40:39 -0800 | [diff] [blame] | 392 | struct device_attribute enable_attr; |
| 393 | bool is_enabled; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 394 | int active_reqs; |
Vijay Viswanath | 10e5e37 | 2018-05-03 16:37:22 +0530 | [diff] [blame] | 395 | struct workqueue_struct *clk_gating_workq; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 396 | }; |
| 397 | |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 398 | struct ufs_saved_pwr_info { |
| 399 | struct ufs_pa_layer_attr info; |
| 400 | bool is_valid; |
| 401 | }; |
| 402 | |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 403 | /** |
| 404 | * struct ufs_clk_scaling - UFS clock scaling related data |
| 405 | * @active_reqs: number of requests that are pending. If this is zero when |
| 406 | * devfreq ->target() function is called then schedule "suspend_work" to |
| 407 | * suspend devfreq. |
| 408 | * @tot_busy_t: Total busy time in current polling window |
| 409 | * @window_start_t: Start time (in jiffies) of the current polling window |
| 410 | * @busy_start_t: Start time of current busy period |
| 411 | * @enable_attr: sysfs attribute to enable/disable clock scaling |
| 412 | * @saved_pwr_info: UFS power mode may also be changed during scaling and this |
| 413 | * one keeps track of previous power mode. |
| 414 | * @workq: workqueue to schedule devfreq suspend/resume work |
| 415 | * @suspend_work: worker to suspend devfreq |
| 416 | * @resume_work: worker to resume devfreq |
| 417 | * @is_allowed: tracks if scaling is currently allowed or not |
| 418 | * @is_busy_started: tracks if busy period has started or not |
| 419 | * @is_suspended: tracks if devfreq is suspended or not |
| 420 | */ |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 421 | struct ufs_clk_scaling { |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 422 | int active_reqs; |
| 423 | unsigned long tot_busy_t; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 424 | unsigned long window_start_t; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 425 | ktime_t busy_start_t; |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 426 | struct device_attribute enable_attr; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 427 | struct ufs_saved_pwr_info saved_pwr_info; |
subhashj@codeaurora.org | 401f1e4 | 2017-02-03 16:57:39 -0800 | [diff] [blame] | 428 | struct workqueue_struct *workq; |
| 429 | struct work_struct suspend_work; |
| 430 | struct work_struct resume_work; |
| 431 | bool is_allowed; |
| 432 | bool is_busy_started; |
| 433 | bool is_suspended; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 434 | }; |
| 435 | |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 436 | #define UFS_ERR_REG_HIST_LENGTH 8 |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 437 | /** |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 438 | * struct ufs_err_reg_hist - keeps history of errors |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 439 | * @pos: index to indicate cyclic buffer position |
| 440 | * @reg: cyclic buffer for registers value |
| 441 | * @tstamp: cyclic buffer for time stamp |
| 442 | */ |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 443 | struct ufs_err_reg_hist { |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 444 | int pos; |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 445 | u32 reg[UFS_ERR_REG_HIST_LENGTH]; |
| 446 | ktime_t tstamp[UFS_ERR_REG_HIST_LENGTH]; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 447 | }; |
| 448 | |
| 449 | /** |
| 450 | * struct ufs_stats - keeps usage/err statistics |
| 451 | * @hibern8_exit_cnt: Counter to keep track of number of exits, |
| 452 | * reset this after link-startup. |
| 453 | * @last_hibern8_exit_tstamp: Set time after the hibern8 exit. |
| 454 | * Clear after the first successful command completion. |
| 455 | * @pa_err: tracks pa-uic errors |
| 456 | * @dl_err: tracks dl-uic errors |
| 457 | * @nl_err: tracks nl-uic errors |
| 458 | * @tl_err: tracks tl-uic errors |
| 459 | * @dme_err: tracks dme errors |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 460 | * @auto_hibern8_err: tracks auto-hibernate errors |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 461 | * @fatal_err: tracks fatal errors |
| 462 | * @linkup_err: tracks link-startup errors |
| 463 | * @resume_err: tracks resume errors |
| 464 | * @suspend_err: tracks suspend errors |
| 465 | * @dev_reset: tracks device reset events |
| 466 | * @host_reset: tracks host reset events |
| 467 | * @tsk_abort: tracks task abort events |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 468 | */ |
| 469 | struct ufs_stats { |
| 470 | u32 hibern8_exit_cnt; |
| 471 | ktime_t last_hibern8_exit_tstamp; |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 472 | |
| 473 | /* uic specific errors */ |
Stanley Chu | 48d5b97 | 2019-07-10 21:38:18 +0800 | [diff] [blame] | 474 | struct ufs_err_reg_hist pa_err; |
| 475 | struct ufs_err_reg_hist dl_err; |
| 476 | struct ufs_err_reg_hist nl_err; |
| 477 | struct ufs_err_reg_hist tl_err; |
| 478 | struct ufs_err_reg_hist dme_err; |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 479 | |
| 480 | /* fatal errors */ |
Stanley Chu | d3c615b | 2019-07-10 21:38:19 +0800 | [diff] [blame] | 481 | struct ufs_err_reg_hist auto_hibern8_err; |
Stanley Chu | 8808b4e | 2019-07-10 21:38:21 +0800 | [diff] [blame] | 482 | struct ufs_err_reg_hist fatal_err; |
| 483 | struct ufs_err_reg_hist link_startup_err; |
| 484 | struct ufs_err_reg_hist resume_err; |
| 485 | struct ufs_err_reg_hist suspend_err; |
| 486 | |
| 487 | /* abnormal events */ |
| 488 | struct ufs_err_reg_hist dev_reset; |
| 489 | struct ufs_err_reg_hist host_reset; |
| 490 | struct ufs_err_reg_hist task_abort; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 491 | }; |
| 492 | |
Christoph Hellwig | c3f7d1f | 2020-02-21 06:08:12 -0800 | [diff] [blame] | 493 | enum ufshcd_quirks { |
| 494 | /* Interrupt aggregation support is broken */ |
| 495 | UFSHCD_QUIRK_BROKEN_INTR_AGGR = 1 << 0, |
| 496 | |
| 497 | /* |
| 498 | * delay before each dme command is required as the unipro |
| 499 | * layer has shown instabilities |
| 500 | */ |
| 501 | UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS = 1 << 1, |
| 502 | |
| 503 | /* |
| 504 | * If UFS host controller is having issue in processing LCC (Line |
| 505 | * Control Command) coming from device then enable this quirk. |
| 506 | * When this quirk is enabled, host controller driver should disable |
| 507 | * the LCC transmission on UFS device (by clearing TX_LCC_ENABLE |
| 508 | * attribute of device to 0). |
| 509 | */ |
| 510 | UFSHCD_QUIRK_BROKEN_LCC = 1 << 2, |
| 511 | |
| 512 | /* |
| 513 | * The attribute PA_RXHSUNTERMCAP specifies whether or not the |
| 514 | * inbound Link supports unterminated line in HS mode. Setting this |
| 515 | * attribute to 1 fixes moving to HS gear. |
| 516 | */ |
| 517 | UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP = 1 << 3, |
| 518 | |
| 519 | /* |
| 520 | * This quirk needs to be enabled if the host controller only allows |
| 521 | * accessing the peer dme attributes in AUTO mode (FAST AUTO or |
| 522 | * SLOW AUTO). |
| 523 | */ |
| 524 | UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE = 1 << 4, |
| 525 | |
| 526 | /* |
| 527 | * This quirk needs to be enabled if the host controller doesn't |
| 528 | * advertise the correct version in UFS_VER register. If this quirk |
| 529 | * is enabled, standard UFS host driver will call the vendor specific |
| 530 | * ops (get_ufs_hci_version) to get the correct version. |
| 531 | */ |
| 532 | UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION = 1 << 5, |
Greg Kroah-Hartman | 7719f75 | 2020-04-07 18:33:53 +0200 | [diff] [blame] | 533 | |
| 534 | /* |
| 535 | * This quirk needs to be enabled if the host controller reports |
| 536 | * OCS FATAL ERROR with device error through sense data |
| 537 | */ |
| 538 | UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR = 1 << 6, |
Christoph Hellwig | c3f7d1f | 2020-02-21 06:08:12 -0800 | [diff] [blame] | 539 | }; |
| 540 | |
Stanley Chu | c201468 | 2020-03-18 18:40:11 +0800 | [diff] [blame] | 541 | enum ufshcd_caps { |
| 542 | /* Allow dynamic clk gating */ |
| 543 | UFSHCD_CAP_CLK_GATING = 1 << 0, |
| 544 | |
| 545 | /* Allow hiberb8 with clk gating */ |
| 546 | UFSHCD_CAP_HIBERN8_WITH_CLK_GATING = 1 << 1, |
| 547 | |
| 548 | /* Allow dynamic clk scaling */ |
| 549 | UFSHCD_CAP_CLK_SCALING = 1 << 2, |
| 550 | |
| 551 | /* Allow auto bkops to enabled during runtime suspend */ |
| 552 | UFSHCD_CAP_AUTO_BKOPS_SUSPEND = 1 << 3, |
| 553 | |
| 554 | /* |
| 555 | * This capability allows host controller driver to use the UFS HCI's |
| 556 | * interrupt aggregation capability. |
| 557 | * CAUTION: Enabling this might reduce overall UFS throughput. |
| 558 | */ |
| 559 | UFSHCD_CAP_INTR_AGGR = 1 << 4, |
| 560 | |
| 561 | /* |
| 562 | * This capability allows the device auto-bkops to be always enabled |
| 563 | * except during suspend (both runtime and suspend). |
| 564 | * Enabling this capability means that device will always be allowed |
| 565 | * to do background operation when it's active but it might degrade |
| 566 | * the performance of ongoing read/write operations. |
| 567 | */ |
| 568 | UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND = 1 << 5, |
| 569 | |
| 570 | /* |
| 571 | * This capability allows host controller driver to automatically |
| 572 | * enable runtime power management by itself instead of waiting |
| 573 | * for userspace to control the power management. |
| 574 | */ |
| 575 | UFSHCD_CAP_RPM_AUTOSUSPEND = 1 << 6, |
Greg Kroah-Hartman | 7719f75 | 2020-04-07 18:33:53 +0200 | [diff] [blame] | 576 | |
| 577 | /* |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 578 | * This capability allows the host controller driver to turn-on |
| 579 | * WriteBooster, if the underlying device supports it and is |
| 580 | * provisioned to be used. This would increase the write performance. |
| 581 | */ |
| 582 | UFSHCD_CAP_WB_EN = 1 << 7, |
Greg Kroah-Hartman | b378328 | 2020-06-24 07:11:16 +0200 | [diff] [blame] | 583 | |
| 584 | /* |
Greg Kroah-Hartman | 7719f75 | 2020-04-07 18:33:53 +0200 | [diff] [blame] | 585 | * This capability allows the host controller driver to use the |
| 586 | * inline crypto engine, if it is present |
| 587 | */ |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 588 | UFSHCD_CAP_CRYPTO = 1 << 8, |
Stanley Chu | c201468 | 2020-03-18 18:40:11 +0800 | [diff] [blame] | 589 | }; |
| 590 | |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 591 | struct ufs_hba_variant_params { |
| 592 | struct devfreq_dev_profile devfreq_profile; |
| 593 | struct devfreq_simple_ondemand_data ondemand_data; |
| 594 | u16 hba_enable_delay_us; |
Stanley Chu | d14734ae | 2020-05-09 17:37:15 +0800 | [diff] [blame] | 595 | u32 wb_flush_threshold; |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 596 | }; |
| 597 | |
Yaniv Gardi | 3a4bf06 | 2014-09-25 15:32:27 +0300 | [diff] [blame] | 598 | /** |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 599 | * struct ufs_hba - per adapter private structure |
| 600 | * @mmio_base: UFSHCI base register address |
| 601 | * @ucdl_base_addr: UFS Command Descriptor base address |
| 602 | * @utrdl_base_addr: UTP Transfer Request Descriptor base address |
| 603 | * @utmrdl_base_addr: UTP Task Management Descriptor base address |
| 604 | * @ucdl_dma_addr: UFS Command Descriptor DMA address |
| 605 | * @utrdl_dma_addr: UTRDL DMA address |
| 606 | * @utmrdl_dma_addr: UTMRDL DMA address |
| 607 | * @host: Scsi_Host instance of the driver |
| 608 | * @dev: device handle |
| 609 | * @lrb: local reference block |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 610 | * @cmd_queue: Used to allocate command tags from hba->host->tag_set. |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 611 | * @outstanding_tasks: Bits representing outstanding task requests |
| 612 | * @outstanding_reqs: Bits representing outstanding transfer requests |
| 613 | * @capabilities: UFS Controller Capabilities |
| 614 | * @nutrs: Transfer Request Queue depth supported by controller |
| 615 | * @nutmrs: Task Management Queue depth supported by controller |
| 616 | * @ufs_version: UFS Version to which controller complies |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 617 | * @vops: pointer to variant specific operations |
| 618 | * @priv: pointer to variant specific private data |
Eric Biggers | 8de80df | 2020-02-24 14:37:12 -0800 | [diff] [blame] | 619 | * @sg_entry_size: size of struct ufshcd_sg_entry (may include variant fields) |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 620 | * @irq: Irq number of the controller |
| 621 | * @active_uic_cmd: handle of active UIC command |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 622 | * @uic_cmd_mutex: mutex for uic command |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 623 | * @tmf_tag_set: TMF tag set. |
| 624 | * @tmf_queue: Used to allocate TMF tags. |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 625 | * @pwr_done: completion for power mode change |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 626 | * @ufshcd_state: UFSHCD states |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 627 | * @eh_flags: Error handling flags |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 628 | * @intr_mask: Interrupt Mask Bits |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 629 | * @ee_ctrl_mask: Exception event control mask |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 630 | * @is_powered: flag to check if HBA is powered |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 631 | * @eh_work: Worker to handle UFS errors that require s/w attention |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 632 | * @eeh_work: Worker to handle exception events |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 633 | * @errors: HBA errors |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 634 | * @uic_error: UFS interconnect layer error status |
| 635 | * @saved_err: sticky error mask |
| 636 | * @saved_uic_err: sticky UIC error mask |
Can Guo | 46c0912 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 637 | * @silence_err_logs: flag to silence error logs |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 638 | * @dev_cmd: ufs device management command information |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 639 | * @last_dme_cmd_tstamp: time stamp of the last completed DME command |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 640 | * @auto_bkops_enabled: to track whether bkops is enabled in device |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 641 | * @vreg_info: UFS device voltage regulator information |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 642 | * @clk_list_head: UFS host controller clocks list node head |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 643 | * @pwr_info: holds current power mode |
| 644 | * @max_pwr_info: keeps the device max valid pwm |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 645 | * @desc_size: descriptor sizes reported by device |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 646 | * @urgent_bkops_lvl: keeps track of urgent bkops level for device |
| 647 | * @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for |
| 648 | * device is known or not. |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 649 | * @scsi_block_reqs_cnt: reference counting for scsi block requests |
Satya Tangirala | 4bac110 | 2019-10-24 14:44:27 -0700 | [diff] [blame] | 650 | * @crypto_capabilities: Content of crypto capabilities register (0x100) |
| 651 | * @crypto_cap_array: Array of crypto capabilities |
| 652 | * @crypto_cfg_register: Start of the crypto cfg array |
Satya Tangirala | 4bac110 | 2019-10-24 14:44:27 -0700 | [diff] [blame] | 653 | * @ksm: the keyslot manager tied to this hba |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 654 | */ |
| 655 | struct ufs_hba { |
| 656 | void __iomem *mmio_base; |
| 657 | |
| 658 | /* Virtual memory reference */ |
| 659 | struct utp_transfer_cmd_desc *ucdl_base_addr; |
| 660 | struct utp_transfer_req_desc *utrdl_base_addr; |
| 661 | struct utp_task_req_desc *utmrdl_base_addr; |
| 662 | |
| 663 | /* DMA memory reference */ |
| 664 | dma_addr_t ucdl_dma_addr; |
| 665 | dma_addr_t utrdl_dma_addr; |
| 666 | dma_addr_t utmrdl_dma_addr; |
| 667 | |
| 668 | struct Scsi_Host *host; |
| 669 | struct device *dev; |
Bart Van Assche | 7252a36 | 2019-12-09 10:13:08 -0800 | [diff] [blame] | 670 | struct request_queue *cmd_queue; |
Subhash Jadavani | 2a8fa60 | 2014-09-25 15:32:28 +0300 | [diff] [blame] | 671 | /* |
| 672 | * This field is to keep a reference to "scsi_device" corresponding to |
| 673 | * "UFS device" W-LU. |
| 674 | */ |
| 675 | struct scsi_device *sdev_ufs_device; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 676 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 677 | enum ufs_dev_pwr_mode curr_dev_pwr_mode; |
| 678 | enum uic_link_state uic_link_state; |
| 679 | /* Desired UFS power management level during runtime PM */ |
| 680 | enum ufs_pm_level rpm_lvl; |
| 681 | /* Desired UFS power management level during system PM */ |
| 682 | enum ufs_pm_level spm_lvl; |
subhashj@codeaurora.org | 09690d5 | 2016-12-22 18:41:00 -0800 | [diff] [blame] | 683 | struct device_attribute rpm_lvl_attr; |
| 684 | struct device_attribute spm_lvl_attr; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 685 | int pm_op_in_progress; |
| 686 | |
Adrian Hunter | ad44837 | 2018-03-20 15:07:38 +0200 | [diff] [blame] | 687 | /* Auto-Hibernate Idle Timer register value */ |
| 688 | u32 ahit; |
| 689 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 690 | struct ufshcd_lrb *lrb; |
| 691 | |
| 692 | unsigned long outstanding_tasks; |
| 693 | unsigned long outstanding_reqs; |
| 694 | |
| 695 | u32 capabilities; |
| 696 | int nutrs; |
| 697 | int nutmrs; |
| 698 | u32 ufs_version; |
Arnd Bergmann | 176eb92 | 2019-03-04 20:39:11 +0100 | [diff] [blame] | 699 | const struct ufs_hba_variant_ops *vops; |
Stanley Chu | 90b8491 | 2020-05-09 17:37:13 +0800 | [diff] [blame] | 700 | struct ufs_hba_variant_params *vps; |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 701 | void *priv; |
Eric Biggers | 8de80df | 2020-02-24 14:37:12 -0800 | [diff] [blame] | 702 | size_t sg_entry_size; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 703 | unsigned int irq; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 704 | bool is_irq_enabled; |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 705 | enum ufs_ref_clk_freq dev_ref_clk_freq; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 706 | |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 707 | unsigned int quirks; /* Deviations from standard UFSHCI spec. */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 708 | |
Yaniv Gardi | c58ab7a | 2016-03-10 17:37:10 +0200 | [diff] [blame] | 709 | /* Device deviations from standard UFS device spec. */ |
| 710 | unsigned int dev_quirks; |
| 711 | |
Bart Van Assche | 69a6c26 | 2019-12-09 10:13:09 -0800 | [diff] [blame] | 712 | struct blk_mq_tag_set tmf_tag_set; |
| 713 | struct request_queue *tmf_queue; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 714 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 715 | struct uic_command *active_uic_cmd; |
| 716 | struct mutex uic_cmd_mutex; |
| 717 | struct completion *uic_async_done; |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 718 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 719 | u32 ufshcd_state; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 720 | u32 eh_flags; |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 721 | u32 intr_mask; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 722 | u16 ee_ctrl_mask; |
Sujit Reddy Thumma | 1d337ec | 2014-09-25 15:32:26 +0300 | [diff] [blame] | 723 | bool is_powered; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 724 | |
| 725 | /* Work Queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 726 | struct work_struct eh_work; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 727 | struct work_struct eeh_work; |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 728 | |
| 729 | /* HBA Errors */ |
| 730 | u32 errors; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 731 | u32 uic_error; |
| 732 | u32 saved_err; |
| 733 | u32 saved_uic_err; |
Dolev Raviv | ff8e20c | 2016-12-22 18:42:18 -0800 | [diff] [blame] | 734 | struct ufs_stats ufs_stats; |
Can Guo | 46c0912 | 2019-11-25 22:53:33 -0800 | [diff] [blame] | 735 | bool silence_err_logs; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 736 | |
| 737 | /* Device management request data */ |
| 738 | struct ufs_dev_cmd dev_cmd; |
Yaniv Gardi | cad2e03 | 2015-03-31 17:37:14 +0300 | [diff] [blame] | 739 | ktime_t last_dme_cmd_tstamp; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 740 | |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 741 | /* Keeps information of the UFS device connected to this host */ |
| 742 | struct ufs_dev_info dev_info; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 743 | bool auto_bkops_enabled; |
Sujit Reddy Thumma | aa49761 | 2014-09-25 15:32:22 +0300 | [diff] [blame] | 744 | struct ufs_vreg_info vreg_info; |
Sujit Reddy Thumma | c6e79da | 2014-09-25 15:32:23 +0300 | [diff] [blame] | 745 | struct list_head clk_list_head; |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 746 | |
| 747 | bool wlun_dev_clr_ua; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 748 | |
Gilad Broner | 7fabb77 | 2017-02-03 16:56:50 -0800 | [diff] [blame] | 749 | /* Number of requests aborts */ |
| 750 | int req_abort_count; |
| 751 | |
Yaniv Gardi | 54b879b | 2016-03-10 17:37:05 +0200 | [diff] [blame] | 752 | /* Number of lanes available (1 or 2) for Rx/Tx */ |
| 753 | u32 lanes_per_direction; |
Dolev Raviv | 7eb584d | 2014-09-25 15:32:31 +0300 | [diff] [blame] | 754 | struct ufs_pa_layer_attr pwr_info; |
| 755 | struct ufs_pwr_mode_info max_pwr_info; |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 756 | |
| 757 | struct ufs_clk_gating clk_gating; |
| 758 | /* Control to enable/disable host capabilities */ |
| 759 | u32 caps; |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 760 | |
| 761 | struct devfreq *devfreq; |
| 762 | struct ufs_clk_scaling clk_scaling; |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 763 | bool is_sys_suspended; |
Yaniv Gardi | afdfff5 | 2016-03-10 17:37:15 +0200 | [diff] [blame] | 764 | |
| 765 | enum bkops_status urgent_bkops_lvl; |
| 766 | bool is_urgent_bkops_lvl_checked; |
subhashj@codeaurora.org | a3cd5ec | 2017-02-03 16:57:02 -0800 | [diff] [blame] | 767 | |
| 768 | struct rw_semaphore clk_scaling_lock; |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 769 | struct ufs_desc_size desc_size; |
Subhash Jadavani | 3813553 | 2018-05-03 16:37:18 +0530 | [diff] [blame] | 770 | atomic_t scsi_block_reqs_cnt; |
Avri Altman | df032bf | 2018-10-07 17:30:35 +0300 | [diff] [blame] | 771 | |
| 772 | struct device bsg_dev; |
| 773 | struct request_queue *bsg_queue; |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 774 | bool wb_buf_flush_enabled; |
| 775 | bool wb_enabled; |
Stanley Chu | 51dd905 | 2020-05-22 16:32:12 +0800 | [diff] [blame] | 776 | struct delayed_work rpm_dev_flush_recheck_work; |
Satya Tangirala | 4bac110 | 2019-10-24 14:44:27 -0700 | [diff] [blame] | 777 | |
| 778 | #ifdef CONFIG_SCSI_UFS_CRYPTO |
Satya Tangirala | 4bac110 | 2019-10-24 14:44:27 -0700 | [diff] [blame] | 779 | union ufs_crypto_capabilities crypto_capabilities; |
| 780 | union ufs_crypto_cap_entry *crypto_cap_array; |
| 781 | u32 crypto_cfg_register; |
Satya Tangirala | c2b86b7 | 2020-06-16 14:33:37 -0700 | [diff] [blame] | 782 | struct blk_keyslot_manager ksm; |
| 783 | #endif |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 784 | }; |
| 785 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 786 | /* Returns true if clocks can be gated. Otherwise false */ |
| 787 | static inline bool ufshcd_is_clkgating_allowed(struct ufs_hba *hba) |
| 788 | { |
| 789 | return hba->caps & UFSHCD_CAP_CLK_GATING; |
| 790 | } |
| 791 | static inline bool ufshcd_can_hibern8_during_gating(struct ufs_hba *hba) |
| 792 | { |
| 793 | return hba->caps & UFSHCD_CAP_HIBERN8_WITH_CLK_GATING; |
| 794 | } |
Sahitya Tummala | fcb0c4b | 2016-12-22 18:40:50 -0800 | [diff] [blame] | 795 | static inline int ufshcd_is_clkscaling_supported(struct ufs_hba *hba) |
Sahitya Tummala | 856b348 | 2014-09-25 15:32:34 +0300 | [diff] [blame] | 796 | { |
| 797 | return hba->caps & UFSHCD_CAP_CLK_SCALING; |
| 798 | } |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 799 | static inline bool ufshcd_can_autobkops_during_suspend(struct ufs_hba *hba) |
| 800 | { |
| 801 | return hba->caps & UFSHCD_CAP_AUTO_BKOPS_SUSPEND; |
| 802 | } |
Stanley Chu | a30de0e | 2019-09-16 23:56:50 +0800 | [diff] [blame] | 803 | static inline bool ufshcd_is_rpm_autosuspend_allowed(struct ufs_hba *hba) |
| 804 | { |
| 805 | return hba->caps & UFSHCD_CAP_RPM_AUTOSUSPEND; |
| 806 | } |
Subhash Jadavani | 374a246 | 2014-09-25 15:32:35 +0300 | [diff] [blame] | 807 | |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 808 | static inline bool ufshcd_is_intr_aggr_allowed(struct ufs_hba *hba) |
| 809 | { |
Joao Pinto | 4b9ffb5 | 2016-05-11 12:21:30 +0100 | [diff] [blame] | 810 | /* DWC UFS Core has the Interrupt aggregation feature but is not detectable*/ |
| 811 | #ifndef CONFIG_SCSI_UFS_DWC |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 812 | if ((hba->caps & UFSHCD_CAP_INTR_AGGR) && |
| 813 | !(hba->quirks & UFSHCD_QUIRK_BROKEN_INTR_AGGR)) |
| 814 | return true; |
| 815 | else |
| 816 | return false; |
Joao Pinto | 4b9ffb5 | 2016-05-11 12:21:30 +0100 | [diff] [blame] | 817 | #else |
| 818 | return true; |
| 819 | #endif |
Yaniv Gardi | b852190 | 2015-05-17 18:54:57 +0300 | [diff] [blame] | 820 | } |
| 821 | |
Stanley Chu | ee5f104 | 2019-05-21 14:44:52 +0800 | [diff] [blame] | 822 | static inline bool ufshcd_is_auto_hibern8_supported(struct ufs_hba *hba) |
| 823 | { |
| 824 | return (hba->capabilities & MASK_AUTO_HIBERN8_SUPPORT); |
| 825 | } |
| 826 | |
Stanley Chu | 5a244e0 | 2020-01-29 18:52:50 +0800 | [diff] [blame] | 827 | static inline bool ufshcd_is_auto_hibern8_enabled(struct ufs_hba *hba) |
| 828 | { |
| 829 | return FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, hba->ahit) ? true : false; |
| 830 | } |
| 831 | |
Asutosh Das | 3d17b9b | 2020-04-22 14:41:42 -0700 | [diff] [blame] | 832 | static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba) |
| 833 | { |
| 834 | return hba->caps & UFSHCD_CAP_WB_EN; |
| 835 | } |
| 836 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 837 | #define ufshcd_writel(hba, val, reg) \ |
| 838 | writel((val), (hba)->mmio_base + (reg)) |
| 839 | #define ufshcd_readl(hba, reg) \ |
| 840 | readl((hba)->mmio_base + (reg)) |
| 841 | |
Dolev Raviv | e785060 | 2014-09-25 15:32:36 +0300 | [diff] [blame] | 842 | /** |
| 843 | * ufshcd_rmwl - read modify write into a register |
| 844 | * @hba - per adapter instance |
| 845 | * @mask - mask to apply on read value |
| 846 | * @val - actual value to write |
| 847 | * @reg - register address |
| 848 | */ |
| 849 | static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg) |
| 850 | { |
| 851 | u32 tmp; |
| 852 | |
| 853 | tmp = ufshcd_readl(hba, reg); |
| 854 | tmp &= ~mask; |
| 855 | tmp |= (val & mask); |
| 856 | ufshcd_writel(hba, tmp, reg); |
| 857 | } |
| 858 | |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 859 | int ufshcd_alloc_host(struct device *, struct ufs_hba **); |
Yaniv Gardi | 47555a5 | 2015-10-28 13:15:49 +0200 | [diff] [blame] | 860 | void ufshcd_dealloc_host(struct ufs_hba *); |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 861 | int ufshcd_hba_enable(struct ufs_hba *hba); |
Sujit Reddy Thumma | 5c0c28a | 2014-09-25 15:32:21 +0300 | [diff] [blame] | 862 | int ufshcd_init(struct ufs_hba * , void __iomem * , unsigned int); |
Stanley Chu | 087c5ef | 2020-03-27 17:53:28 +0800 | [diff] [blame] | 863 | int ufshcd_link_recovery(struct ufs_hba *hba); |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 864 | int ufshcd_make_hba_operational(struct ufs_hba *hba); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 865 | void ufshcd_remove(struct ufs_hba *); |
Stanley Chu | 9d19bf7a | 2020-01-17 11:51:07 +0800 | [diff] [blame] | 866 | int ufshcd_uic_hibern8_exit(struct ufs_hba *hba); |
Stanley Chu | 5c955c1 | 2020-03-18 18:40:12 +0800 | [diff] [blame] | 867 | void ufshcd_delay_us(unsigned long us, unsigned long tolerance); |
Yaniv Gardi | 596585a | 2016-03-10 17:37:08 +0200 | [diff] [blame] | 868 | int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, |
| 869 | u32 val, unsigned long interval_us, |
Bart Van Assche | 5cac109 | 2020-05-07 15:27:50 -0700 | [diff] [blame] | 870 | unsigned long timeout_ms); |
Subhash Jadavani | 9e1e8a7 | 2018-10-16 14:29:41 +0530 | [diff] [blame] | 871 | void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk); |
Stanley Chu | a5fe372d | 2020-01-04 22:26:07 +0800 | [diff] [blame] | 872 | void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist, |
| 873 | u32 reg); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 874 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 875 | static inline void check_upiu_size(void) |
| 876 | { |
| 877 | BUILD_BUG_ON(ALIGNED_UPIU_SIZE < |
| 878 | GENERAL_UPIU_REQUEST_SIZE + QUERY_DESC_MAX_SIZE); |
| 879 | } |
| 880 | |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 881 | /** |
| 882 | * ufshcd_set_variant - set variant specific data to the hba |
| 883 | * @hba - per adapter instance |
| 884 | * @variant - pointer to variant specific data |
| 885 | */ |
| 886 | static inline void ufshcd_set_variant(struct ufs_hba *hba, void *variant) |
| 887 | { |
| 888 | BUG_ON(!hba); |
| 889 | hba->priv = variant; |
| 890 | } |
| 891 | |
| 892 | /** |
| 893 | * ufshcd_get_variant - get variant specific data from the hba |
| 894 | * @hba - per adapter instance |
| 895 | */ |
| 896 | static inline void *ufshcd_get_variant(struct ufs_hba *hba) |
| 897 | { |
| 898 | BUG_ON(!hba); |
| 899 | return hba->priv; |
| 900 | } |
subhashj@codeaurora.org | 4e768e7 | 2016-12-22 18:41:22 -0800 | [diff] [blame] | 901 | static inline bool ufshcd_keep_autobkops_enabled_except_suspend( |
| 902 | struct ufs_hba *hba) |
| 903 | { |
| 904 | return hba->caps & UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND; |
| 905 | } |
Yaniv Gardi | 1ce5898 | 2015-10-28 13:15:47 +0200 | [diff] [blame] | 906 | |
Stanley Chu | e31011a | 2020-05-22 16:32:11 +0800 | [diff] [blame] | 907 | static inline u8 ufshcd_wb_get_query_index(struct ufs_hba *hba) |
Stanley Chu | 6f8d5a6 | 2020-05-08 16:01:13 +0800 | [diff] [blame] | 908 | { |
| 909 | if (hba->dev_info.b_wb_buffer_type == WB_BUF_MODE_LU_DEDICATED) |
| 910 | return hba->dev_info.wb_dedicated_lu; |
| 911 | return 0; |
| 912 | } |
| 913 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 914 | extern int ufshcd_runtime_suspend(struct ufs_hba *hba); |
| 915 | extern int ufshcd_runtime_resume(struct ufs_hba *hba); |
| 916 | extern int ufshcd_runtime_idle(struct ufs_hba *hba); |
Subhash Jadavani | 57d104c | 2014-09-25 15:32:30 +0300 | [diff] [blame] | 917 | extern int ufshcd_system_suspend(struct ufs_hba *hba); |
| 918 | extern int ufshcd_system_resume(struct ufs_hba *hba); |
| 919 | extern int ufshcd_shutdown(struct ufs_hba *hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 920 | extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, |
| 921 | u8 attr_set, u32 mib_val, u8 peer); |
| 922 | extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, |
| 923 | u32 *mib_val, u8 peer); |
Alim Akhtar | 0d846e7 | 2018-05-06 15:44:18 +0530 | [diff] [blame] | 924 | extern int ufshcd_config_pwr_mode(struct ufs_hba *hba, |
| 925 | struct ufs_pa_layer_attr *desired_pwr_mode); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 926 | |
| 927 | /* UIC command interfaces for DME primitives */ |
| 928 | #define DME_LOCAL 0 |
| 929 | #define DME_PEER 1 |
| 930 | #define ATTR_SET_NOR 0 /* NORMAL */ |
| 931 | #define ATTR_SET_ST 1 /* STATIC */ |
| 932 | |
| 933 | static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel, |
| 934 | u32 mib_val) |
| 935 | { |
| 936 | return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR, |
| 937 | mib_val, DME_LOCAL); |
| 938 | } |
| 939 | |
| 940 | static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel, |
| 941 | u32 mib_val) |
| 942 | { |
| 943 | return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST, |
| 944 | mib_val, DME_LOCAL); |
| 945 | } |
| 946 | |
| 947 | static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel, |
| 948 | u32 mib_val) |
| 949 | { |
| 950 | return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR, |
| 951 | mib_val, DME_PEER); |
| 952 | } |
| 953 | |
| 954 | static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel, |
| 955 | u32 mib_val) |
| 956 | { |
| 957 | return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST, |
| 958 | mib_val, DME_PEER); |
| 959 | } |
| 960 | |
| 961 | static inline int ufshcd_dme_get(struct ufs_hba *hba, |
| 962 | u32 attr_sel, u32 *mib_val) |
| 963 | { |
| 964 | return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL); |
| 965 | } |
| 966 | |
| 967 | static inline int ufshcd_dme_peer_get(struct ufs_hba *hba, |
| 968 | u32 attr_sel, u32 *mib_val) |
| 969 | { |
| 970 | return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER); |
| 971 | } |
| 972 | |
Yaniv Gardi | f37aabc | 2016-03-10 17:37:20 +0200 | [diff] [blame] | 973 | static inline bool ufshcd_is_hs_mode(struct ufs_pa_layer_attr *pwr_info) |
| 974 | { |
| 975 | return (pwr_info->pwr_rx == FAST_MODE || |
| 976 | pwr_info->pwr_rx == FASTAUTO_MODE) && |
| 977 | (pwr_info->pwr_tx == FAST_MODE || |
| 978 | pwr_info->pwr_tx == FASTAUTO_MODE); |
| 979 | } |
| 980 | |
Stanley Chu | 984eaac | 2020-02-07 15:03:57 +0800 | [diff] [blame] | 981 | static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba) |
| 982 | { |
| 983 | return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0); |
| 984 | } |
| 985 | |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 986 | /* Expose Query-Request API */ |
Stanislav Nijnikov | 2238d31 | 2018-02-15 14:14:07 +0200 | [diff] [blame] | 987 | int ufshcd_query_descriptor_retry(struct ufs_hba *hba, |
| 988 | enum query_opcode opcode, |
| 989 | enum desc_idn idn, u8 index, |
| 990 | u8 selector, |
| 991 | u8 *desc_buf, int *buf_len); |
Stanislav Nijnikov | 45bced8 | 2018-02-15 14:14:02 +0200 | [diff] [blame] | 992 | int ufshcd_read_desc_param(struct ufs_hba *hba, |
| 993 | enum desc_idn desc_id, |
| 994 | int desc_index, |
| 995 | u8 param_offset, |
| 996 | u8 *param_read_buf, |
| 997 | u8 param_size); |
Stanislav Nijnikov | ec92b59 | 2018-02-15 14:14:11 +0200 | [diff] [blame] | 998 | int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, |
| 999 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val); |
Yaniv Gardi | dc3c8d3 | 2016-02-01 15:02:46 +0200 | [diff] [blame] | 1000 | int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
Stanley Chu | 1f34eed | 2020-05-08 16:01:12 +0800 | [diff] [blame] | 1001 | enum flag_idn idn, u8 index, bool *flag_res); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 1002 | |
Can Guo | 71d848b | 2019-11-14 22:09:26 -0800 | [diff] [blame] | 1003 | void ufshcd_auto_hibern8_enable(struct ufs_hba *hba); |
Stanley Chu | ba7af5e | 2019-12-30 13:32:28 +0800 | [diff] [blame] | 1004 | void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit); |
Stanley Chu | 8db269a | 2020-05-08 16:01:10 +0800 | [diff] [blame] | 1005 | void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups); |
Tomas Winkler | 4b828fe | 2019-07-30 08:55:17 +0300 | [diff] [blame] | 1006 | #define SD_ASCII_STD true |
| 1007 | #define SD_RAW false |
| 1008 | int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, |
| 1009 | u8 **buf, bool ascii); |
Stanislav Nijnikov | 2238d31 | 2018-02-15 14:14:07 +0200 | [diff] [blame] | 1010 | |
Sahitya Tummala | 1ab27c9 | 2014-09-25 15:32:32 +0300 | [diff] [blame] | 1011 | int ufshcd_hold(struct ufs_hba *hba, bool async); |
| 1012 | void ufshcd_release(struct ufs_hba *hba); |
Potomski, MichalX | a4b0e8a | 2017-02-23 09:05:30 +0000 | [diff] [blame] | 1013 | |
| 1014 | int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id, |
| 1015 | int *desc_length); |
| 1016 | |
Yaniv Gardi | 3711310 | 2016-03-10 17:37:16 +0200 | [diff] [blame] | 1017 | u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba); |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 1018 | |
Avri Altman | e77044c5 | 2018-10-07 17:30:39 +0300 | [diff] [blame] | 1019 | int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd); |
| 1020 | |
Avri Altman | 5e0a86e | 2018-10-07 17:30:37 +0300 | [diff] [blame] | 1021 | int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, |
| 1022 | struct utp_upiu_req *req_upiu, |
| 1023 | struct utp_upiu_req *rsp_upiu, |
| 1024 | int msgcode, |
| 1025 | u8 *desc_buff, int *buff_len, |
| 1026 | enum query_opcode desc_op); |
| 1027 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 1028 | /* Wrapper functions for safely calling variant operations */ |
| 1029 | static inline const char *ufshcd_get_var_name(struct ufs_hba *hba) |
| 1030 | { |
| 1031 | if (hba->vops) |
| 1032 | return hba->vops->name; |
| 1033 | return ""; |
| 1034 | } |
| 1035 | |
| 1036 | static inline int ufshcd_vops_init(struct ufs_hba *hba) |
| 1037 | { |
| 1038 | if (hba->vops && hba->vops->init) |
| 1039 | return hba->vops->init(hba); |
| 1040 | |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
| 1044 | static inline void ufshcd_vops_exit(struct ufs_hba *hba) |
| 1045 | { |
| 1046 | if (hba->vops && hba->vops->exit) |
| 1047 | return hba->vops->exit(hba); |
| 1048 | } |
| 1049 | |
| 1050 | static inline u32 ufshcd_vops_get_ufs_hci_version(struct ufs_hba *hba) |
| 1051 | { |
| 1052 | if (hba->vops && hba->vops->get_ufs_hci_version) |
| 1053 | return hba->vops->get_ufs_hci_version(hba); |
| 1054 | |
| 1055 | return ufshcd_readl(hba, REG_UFS_VERSION); |
| 1056 | } |
| 1057 | |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1058 | static inline int ufshcd_vops_clk_scale_notify(struct ufs_hba *hba, |
| 1059 | bool up, enum ufs_notify_change_status status) |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 1060 | { |
| 1061 | if (hba->vops && hba->vops->clk_scale_notify) |
Yaniv Gardi | f06fcc7 | 2015-10-28 13:15:51 +0200 | [diff] [blame] | 1062 | return hba->vops->clk_scale_notify(hba, up, status); |
| 1063 | return 0; |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 1064 | } |
| 1065 | |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 1066 | static inline int ufshcd_vops_setup_clocks(struct ufs_hba *hba, bool on, |
| 1067 | enum ufs_notify_change_status status) |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 1068 | { |
| 1069 | if (hba->vops && hba->vops->setup_clocks) |
Subhash Jadavani | 1e879e8 | 2016-10-06 21:48:22 -0700 | [diff] [blame] | 1070 | return hba->vops->setup_clocks(hba, on, status); |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 1071 | return 0; |
| 1072 | } |
| 1073 | |
| 1074 | static inline int ufshcd_vops_setup_regulators(struct ufs_hba *hba, bool status) |
| 1075 | { |
| 1076 | if (hba->vops && hba->vops->setup_regulators) |
| 1077 | return hba->vops->setup_regulators(hba, status); |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
| 1082 | static inline int ufshcd_vops_hce_enable_notify(struct ufs_hba *hba, |
| 1083 | bool status) |
| 1084 | { |
| 1085 | if (hba->vops && hba->vops->hce_enable_notify) |
| 1086 | return hba->vops->hce_enable_notify(hba, status); |
| 1087 | |
| 1088 | return 0; |
| 1089 | } |
| 1090 | static inline int ufshcd_vops_link_startup_notify(struct ufs_hba *hba, |
| 1091 | bool status) |
| 1092 | { |
| 1093 | if (hba->vops && hba->vops->link_startup_notify) |
| 1094 | return hba->vops->link_startup_notify(hba, status); |
| 1095 | |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static inline int ufshcd_vops_pwr_change_notify(struct ufs_hba *hba, |
| 1100 | bool status, |
| 1101 | struct ufs_pa_layer_attr *dev_max_params, |
| 1102 | struct ufs_pa_layer_attr *dev_req_params) |
| 1103 | { |
| 1104 | if (hba->vops && hba->vops->pwr_change_notify) |
| 1105 | return hba->vops->pwr_change_notify(hba, status, |
| 1106 | dev_max_params, dev_req_params); |
| 1107 | |
| 1108 | return -ENOTSUPP; |
| 1109 | } |
| 1110 | |
Kiwoong Kim | 0e675ef | 2016-11-10 21:14:36 +0900 | [diff] [blame] | 1111 | static inline void ufshcd_vops_setup_xfer_req(struct ufs_hba *hba, int tag, |
| 1112 | bool is_scsi_cmd) |
| 1113 | { |
| 1114 | if (hba->vops && hba->vops->setup_xfer_req) |
| 1115 | return hba->vops->setup_xfer_req(hba, tag, is_scsi_cmd); |
| 1116 | } |
| 1117 | |
Kiwoong Kim | d2877be | 2016-11-10 21:16:15 +0900 | [diff] [blame] | 1118 | static inline void ufshcd_vops_setup_task_mgmt(struct ufs_hba *hba, |
| 1119 | int tag, u8 tm_function) |
| 1120 | { |
| 1121 | if (hba->vops && hba->vops->setup_task_mgmt) |
| 1122 | return hba->vops->setup_task_mgmt(hba, tag, tm_function); |
| 1123 | } |
| 1124 | |
Kiwoong Kim | ee32c90 | 2016-11-10 21:17:43 +0900 | [diff] [blame] | 1125 | static inline void ufshcd_vops_hibern8_notify(struct ufs_hba *hba, |
| 1126 | enum uic_cmd_dme cmd, |
| 1127 | enum ufs_notify_change_status status) |
| 1128 | { |
| 1129 | if (hba->vops && hba->vops->hibern8_notify) |
| 1130 | return hba->vops->hibern8_notify(hba, cmd, status); |
| 1131 | } |
| 1132 | |
Subhash Jadavani | 56d4a18 | 2016-12-05 19:25:32 -0800 | [diff] [blame] | 1133 | static inline int ufshcd_vops_apply_dev_quirks(struct ufs_hba *hba) |
| 1134 | { |
| 1135 | if (hba->vops && hba->vops->apply_dev_quirks) |
| 1136 | return hba->vops->apply_dev_quirks(hba); |
| 1137 | return 0; |
| 1138 | } |
| 1139 | |
Stanley Chu | c28c00b | 2020-05-08 16:01:09 +0800 | [diff] [blame] | 1140 | static inline void ufshcd_vops_fixup_dev_quirks(struct ufs_hba *hba) |
| 1141 | { |
| 1142 | if (hba->vops && hba->vops->fixup_dev_quirks) |
| 1143 | hba->vops->fixup_dev_quirks(hba); |
| 1144 | } |
| 1145 | |
Yaniv Gardi | 0263bcd | 2015-10-28 13:15:48 +0200 | [diff] [blame] | 1146 | static inline int ufshcd_vops_suspend(struct ufs_hba *hba, enum ufs_pm_op op) |
| 1147 | { |
| 1148 | if (hba->vops && hba->vops->suspend) |
| 1149 | return hba->vops->suspend(hba, op); |
| 1150 | |
| 1151 | return 0; |
| 1152 | } |
| 1153 | |
| 1154 | static inline int ufshcd_vops_resume(struct ufs_hba *hba, enum ufs_pm_op op) |
| 1155 | { |
| 1156 | if (hba->vops && hba->vops->resume) |
| 1157 | return hba->vops->resume(hba, op); |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
Yaniv Gardi | 6e3fd44 | 2015-10-28 13:15:50 +0200 | [diff] [blame] | 1162 | static inline void ufshcd_vops_dbg_register_dump(struct ufs_hba *hba) |
| 1163 | { |
| 1164 | if (hba->vops && hba->vops->dbg_register_dump) |
| 1165 | hba->vops->dbg_register_dump(hba); |
| 1166 | } |
| 1167 | |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 1168 | static inline void ufshcd_vops_device_reset(struct ufs_hba *hba) |
| 1169 | { |
Stanley Chu | a5fe372d | 2020-01-04 22:26:07 +0800 | [diff] [blame] | 1170 | if (hba->vops && hba->vops->device_reset) { |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 1171 | hba->vops->device_reset(hba); |
Stanley Chu | 1764fa2 | 2020-03-27 17:58:35 +0800 | [diff] [blame] | 1172 | ufshcd_set_ufs_dev_active(hba); |
Stanley Chu | a5fe372d | 2020-01-04 22:26:07 +0800 | [diff] [blame] | 1173 | ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, 0); |
| 1174 | } |
Bjorn Andersson | d8d9f79 | 2019-08-28 12:17:54 -0700 | [diff] [blame] | 1175 | } |
| 1176 | |
Asutosh Das | 2c75f9a | 2020-03-25 11:29:01 -0700 | [diff] [blame] | 1177 | static inline void ufshcd_vops_config_scaling_param(struct ufs_hba *hba, |
| 1178 | struct devfreq_dev_profile |
| 1179 | *profile, void *data) |
| 1180 | { |
| 1181 | if (hba->vops && hba->vops->config_scaling_param) |
| 1182 | hba->vops->config_scaling_param(hba, profile, data); |
| 1183 | } |
| 1184 | |
Stanislav Nijnikov | cbb6813 | 2018-02-15 14:14:01 +0200 | [diff] [blame] | 1185 | extern struct ufs_pm_lvl_states ufs_pm_lvl_states[]; |
| 1186 | |
Stanislav Nijnikov | d829fc8 | 2018-02-15 14:14:09 +0200 | [diff] [blame] | 1187 | /* |
| 1188 | * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN |
| 1189 | * @scsi_lun: scsi LUN id |
| 1190 | * |
| 1191 | * Returns UPIU LUN id |
| 1192 | */ |
| 1193 | static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun) |
| 1194 | { |
| 1195 | if (scsi_is_wlun(scsi_lun)) |
| 1196 | return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID) |
| 1197 | | UFS_UPIU_WLUN_ID; |
| 1198 | else |
| 1199 | return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID; |
| 1200 | } |
| 1201 | |
Tomas Winkler | ba80917 | 2018-06-14 11:14:09 +0300 | [diff] [blame] | 1202 | int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len, |
| 1203 | const char *prefix); |
Greg Kroah-Hartman | 4f92040 | 2020-02-03 14:11:50 +0000 | [diff] [blame] | 1204 | int ufshcd_uic_hibern8_enter(struct ufs_hba *hba); |
| 1205 | int ufshcd_uic_hibern8_exit(struct ufs_hba *hba); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 1206 | #endif /* End of Header */ |