blob: 50b12d60dc1b2deae027c1a476773fca56a6261a [file] [log] [blame]
Bean Huo67351112020-06-05 22:05:19 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302/*
Vinayak Holikattie0eca632013-02-25 21:44:33 +05303 * Universal Flash Storage Host controller driver Core
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304 * Copyright (C) 2011-2013 Samsung India Software Operations
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02005 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307 * Authors:
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053010 */
11
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053012#include <linux/async.h>
Sahitya Tummala856b3482014-09-25 15:32:34 +030013#include <linux/devfreq.h>
Yaniv Gardib573d482016-03-10 17:37:09 +020014#include <linux/nls.h>
Yaniv Gardi54b879b2016-03-10 17:37:05 +020015#include <linux/of.h>
Adrian Hunterad448372018-03-20 15:07:38 +020016#include <linux/bitfield.h>
Can Guofb276f72020-03-25 18:09:59 -070017#include <linux/blk-pm.h>
Can Guoc72e79c2020-08-09 05:15:52 -070018#include <linux/blkdev.h>
Asutosh Dasb294ff32021-04-23 17:20:16 -070019#include <scsi/scsi_driver.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053020#include "ufshcd.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020021#include "ufs_quirks.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053022#include "unipro.h"
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +020023#include "ufs-sysfs.h"
Adrian Hunterb6cacaf2021-01-07 09:25:38 +020024#include "ufs-debugfs.h"
Bart Van Asschec11a1ae2021-07-21 20:34:39 -070025#include "ufs-fault-injection.h"
Avri Altmandf032bf2018-10-07 17:30:35 +030026#include "ufs_bsg.h"
Satya Tangiraladf043c742020-07-06 20:04:14 +000027#include "ufshcd-crypto.h"
Daejun Parkf02bc972021-07-12 17:58:30 +090028#include "ufshpb.h"
Asutosh Das3d17b9b2020-04-22 14:41:42 -070029#include <asm/unaligned.h>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053030
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -080031#define CREATE_TRACE_POINTS
32#include <trace/events/ufs.h>
33
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053034#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
35 UTP_TASK_REQ_COMPL |\
36 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053037/* UIC command timeout, unit: ms */
38#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053039
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053040/* NOP OUT retries waiting for NOP IN response */
41#define NOP_OUT_RETRIES 10
Daejun Park782e2ef2020-09-02 11:58:52 +090042/* Timeout after 50 msecs if NOP OUT hangs without response */
43#define NOP_OUT_TIMEOUT 50 /* msecs */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053044
Dolev Raviv68078d52013-07-30 00:35:58 +053045/* Query request retries */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080046#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +053047/* Query request timeout */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080048#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +053049
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053050/* Task management command timeout */
51#define TM_CMD_TIMEOUT 100 /* msecs */
52
Yaniv Gardi64238fb2016-02-01 15:02:43 +020053/* maximum number of retries for a general UIC command */
54#define UFS_UIC_COMMAND_RETRIES 3
55
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030056/* maximum number of link-startup retries */
57#define DME_LINKSTARTUP_RETRIES 3
58
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020059/* Maximum retries for Hibern8 enter */
60#define UIC_HIBERN8_ENTER_RETRIES 3
61
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030062/* maximum number of reset retries before giving up */
63#define MAX_HOST_RESET_RETRIES 5
64
Adrian Hunter87bf6a62021-10-02 18:45:50 +030065/* Maximum number of error handler retries before giving up */
66#define MAX_ERR_HANDLER_RETRIES 5
67
Dolev Raviv68078d52013-07-30 00:35:58 +053068/* Expose the flag value from utp_upiu_query.value */
69#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
70
Seungwon Jeon7d568652013-08-31 21:40:20 +053071/* Interrupt aggregation default timeout, unit: 40us */
72#define INT_AGGR_DEF_TO 0x02
73
Stanley Chu49615ba2019-09-16 23:56:50 +080074/* default delay of autosuspend: 2000 ms */
75#define RPM_AUTOSUSPEND_DELAY_MS 2000
76
Stanley Chu51dd9052020-05-22 16:32:12 +080077/* Default delay of RPM device flush delayed work */
78#define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000
79
Can Guo09f17792020-02-10 19:40:49 -080080/* Default value of wait time before gating device ref clock */
81#define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */
82
Kiwoong Kim29707fa2020-08-10 19:02:27 +090083/* Polling time to wait for fDeviceInit */
84#define FDEVICEINIT_COMPL_TIMEOUT 1500 /* millisecs */
85
Asutosh Dasb294ff32021-04-23 17:20:16 -070086#define wlun_dev_to_hba(dv) shost_priv(to_scsi_device(dv)->host)
87
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030088#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
89 ({ \
90 int _ret; \
91 if (_on) \
92 _ret = ufshcd_enable_vreg(_dev, _vreg); \
93 else \
94 _ret = ufshcd_disable_vreg(_dev, _vreg); \
95 _ret; \
96 })
97
Tomas Winklerba809172018-06-14 11:14:09 +030098#define ufshcd_hex_dump(prefix_str, buf, len) do { \
99 size_t __len = (len); \
100 print_hex_dump(KERN_ERR, prefix_str, \
101 __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\
102 16, 4, buf, __len, false); \
103} while (0)
104
105int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
106 const char *prefix)
107{
Marc Gonzalezd6724752019-01-22 18:29:22 +0100108 u32 *regs;
109 size_t pos;
110
111 if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */
112 return -EINVAL;
Tomas Winklerba809172018-06-14 11:14:09 +0300113
Can Guocddaeba2019-11-14 22:09:27 -0800114 regs = kzalloc(len, GFP_ATOMIC);
Tomas Winklerba809172018-06-14 11:14:09 +0300115 if (!regs)
116 return -ENOMEM;
117
Marc Gonzalezd6724752019-01-22 18:29:22 +0100118 for (pos = 0; pos < len; pos += 4)
119 regs[pos / 4] = ufshcd_readl(hba, offset + pos);
120
Tomas Winklerba809172018-06-14 11:14:09 +0300121 ufshcd_hex_dump(prefix, regs, len);
122 kfree(regs);
123
124 return 0;
125}
126EXPORT_SYMBOL_GPL(ufshcd_dump_regs);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800127
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530128enum {
129 UFSHCD_MAX_CHANNEL = 0,
130 UFSHCD_MAX_ID = 1,
Bart Van Assche945c3cc2021-12-03 15:19:42 -0800131 UFSHCD_NUM_RESERVED = 1,
132 UFSHCD_CMD_PER_LUN = 32 - UFSHCD_NUM_RESERVED,
133 UFSHCD_CAN_QUEUE = 32 - UFSHCD_NUM_RESERVED,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530134};
135
Bart Van Assche4693fad2021-10-20 14:40:18 -0700136static const char *const ufshcd_state_name[] = {
137 [UFSHCD_STATE_RESET] = "reset",
138 [UFSHCD_STATE_OPERATIONAL] = "operational",
139 [UFSHCD_STATE_ERROR] = "error",
140 [UFSHCD_STATE_EH_SCHEDULED_FATAL] = "eh_fatal",
141 [UFSHCD_STATE_EH_SCHEDULED_NON_FATAL] = "eh_non_fatal",
142};
143
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530144/* UFSHCD error handling flags */
145enum {
146 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530147};
148
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530149/* UFSHCD UIC layer error flags */
150enum {
151 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200152 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
153 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
154 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
155 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
156 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Can Guo2355b662020-08-24 19:07:06 -0700157 UFSHCD_UIC_PA_GENERIC_ERROR = (1 << 6), /* Generic PA error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530158};
159
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530160#define ufshcd_set_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300161 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530162#define ufshcd_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300163 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530164#define ufshcd_clear_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300165 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530166
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +0200167struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
Bart Van Asschee2ac7ab2021-05-19 13:20:58 -0700168 [UFS_PM_LVL_0] = {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
169 [UFS_PM_LVL_1] = {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
170 [UFS_PM_LVL_2] = {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
171 [UFS_PM_LVL_3] = {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
172 [UFS_PM_LVL_4] = {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
173 [UFS_PM_LVL_5] = {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
Adrian Hunterfe1d4c22020-11-03 16:14:02 +0200174 /*
175 * For DeepSleep, the link is first put in hibern8 and then off.
176 * Leaving the link in hibern8 is not supported.
177 */
Bart Van Asschee2ac7ab2021-05-19 13:20:58 -0700178 [UFS_PM_LVL_6] = {UFS_DEEPSLEEP_PWR_MODE, UIC_LINK_OFF_STATE},
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300179};
180
181static inline enum ufs_dev_pwr_mode
182ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
183{
184 return ufs_pm_lvl_states[lvl].dev_state;
185}
186
187static inline enum uic_link_state
188ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
189{
190 return ufs_pm_lvl_states[lvl].link_state;
191}
192
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -0800193static inline enum ufs_pm_level
194ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
195 enum uic_link_state link_state)
196{
197 enum ufs_pm_level lvl;
198
199 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
200 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
201 (ufs_pm_lvl_states[lvl].link_state == link_state))
202 return lvl;
203 }
204
205 /* if no match found, return the level 0 */
206 return UFS_PM_LVL_0;
207}
208
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800209static struct ufs_dev_fix ufs_fixups[] = {
210 /* UFS cards deviations table */
Stanley Chuc0a18ee2020-06-12 09:26:24 +0800211 UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL,
Bean Huo63522bf2021-08-04 20:21:27 +0200212 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
213 UFS_DEVICE_QUIRK_SWAP_L2P_ENTRY_FOR_HPB_READ),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800214 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
Stanley Chued0b40f2020-06-12 09:26:25 +0800215 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
216 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE |
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800217 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
Stanley Chued0b40f2020-06-12 09:26:25 +0800218 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
219 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
220 UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/,
221 UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800222 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
223 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
224 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
225 UFS_DEVICE_QUIRK_PA_TACTIVATE),
226 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
227 UFS_DEVICE_QUIRK_PA_TACTIVATE),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800228 END_FIX
229};
230
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -0800231static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530232static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530233static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Dolev Ravive7d38252016-12-22 18:40:07 -0800234static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530235static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300236static void ufshcd_hba_exit(struct ufs_hba *hba);
Bean Huo68444d72021-09-29 22:06:39 +0200237static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300238static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300239static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300240static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800241static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
242static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800243static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800244static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300245static irqreturn_t ufshcd_intr(int irq, void *__hba);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300246static int ufshcd_change_power_mode(struct ufs_hba *hba,
247 struct ufs_pa_layer_attr *pwr_mode);
Can Guoc72e79c2020-08-09 05:15:52 -0700248static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on);
249static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on);
250static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
251 struct ufs_vreg *vreg);
Can Guo307348f2020-08-24 19:07:05 -0700252static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag);
Yue Hu3b5f3c02021-03-18 17:55:36 +0800253static void ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set);
254static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable);
Can Guodd7143e2020-10-27 12:10:36 -0700255static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
256static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700257
Can Guo5231d382019-12-05 02:14:46 +0000258static inline void ufshcd_enable_irq(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300259{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300260 if (!hba->is_irq_enabled) {
Can Guo5231d382019-12-05 02:14:46 +0000261 enable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300262 hba->is_irq_enabled = true;
263 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300264}
265
266static inline void ufshcd_disable_irq(struct ufs_hba *hba)
267{
268 if (hba->is_irq_enabled) {
Can Guo5231d382019-12-05 02:14:46 +0000269 disable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300270 hba->is_irq_enabled = false;
271 }
272}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530273
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700274static inline void ufshcd_wb_config(struct ufs_hba *hba)
275{
Stanley Chu79e35202020-05-08 16:01:15 +0800276 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700277 return;
278
Yue Hu3b5f3c02021-03-18 17:55:36 +0800279 ufshcd_wb_toggle(hba, true);
280
281 ufshcd_wb_toggle_flush_during_h8(hba, true);
Stanley Chu21acf462020-12-22 15:29:05 +0800282 if (!(hba->quirks & UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL))
283 ufshcd_wb_toggle_flush(hba, true);
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700284}
285
Subhash Jadavani38135532018-05-03 16:37:18 +0530286static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
287{
288 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt))
289 scsi_unblock_requests(hba->host);
290}
291
292static void ufshcd_scsi_block_requests(struct ufs_hba *hba)
293{
294 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1)
295 scsi_block_requests(hba->host);
296}
297
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300298static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag,
Bean Huo28fa68f2021-01-05 12:34:42 +0100299 enum ufs_trace_str_t str_t)
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300300{
301 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
Bean Huo89ac2c3b2021-05-31 12:43:06 +0200302 struct utp_upiu_header *header;
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300303
Bean Huo9d5095e2021-01-05 12:34:43 +0100304 if (!trace_ufshcd_upiu_enabled())
305 return;
306
Bean Huo89ac2c3b2021-05-31 12:43:06 +0200307 if (str_t == UFS_CMD_SEND)
308 header = &rq->header;
309 else
310 header = &hba->lrb[tag].ucd_rsp_ptr->header;
311
312 trace_ufshcd_upiu(dev_name(hba->dev), str_t, header, &rq->sc.cdb,
Bean Huo867fdc22021-01-05 12:34:46 +0100313 UFS_TSF_CDB);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300314}
315
Avri Altmanfb475b72021-01-10 10:46:18 +0200316static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba,
317 enum ufs_trace_str_t str_t,
318 struct utp_upiu_req *rq_rsp)
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300319{
Bean Huo9d5095e2021-01-05 12:34:43 +0100320 if (!trace_ufshcd_upiu_enabled())
321 return;
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300322
Bean Huobe20b512021-01-05 12:34:44 +0100323 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq_rsp->header,
Bean Huo867fdc22021-01-05 12:34:46 +0100324 &rq_rsp->qr, UFS_TSF_OSF);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300325}
326
327static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag,
Bean Huo28fa68f2021-01-05 12:34:42 +0100328 enum ufs_trace_str_t str_t)
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300329{
Jonathan Hsue8c2da72021-09-24 16:58:48 +0800330 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[tag];
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300331
Bean Huo9d5095e2021-01-05 12:34:43 +0100332 if (!trace_ufshcd_upiu_enabled())
333 return;
334
Bean Huo0ed083e2021-01-05 12:34:45 +0100335 if (str_t == UFS_TM_SEND)
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -0500336 trace_ufshcd_upiu(dev_name(hba->dev), str_t,
337 &descp->upiu_req.req_header,
338 &descp->upiu_req.input_param1,
339 UFS_TSF_TM_INPUT);
Bean Huo0ed083e2021-01-05 12:34:45 +0100340 else
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -0500341 trace_ufshcd_upiu(dev_name(hba->dev), str_t,
342 &descp->upiu_rsp.rsp_header,
343 &descp->upiu_rsp.output_param1,
344 UFS_TSF_TM_OUTPUT);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300345}
346
Stanley Chuaa5c6972020-06-15 15:22:35 +0800347static void ufshcd_add_uic_command_trace(struct ufs_hba *hba,
348 struct uic_command *ucmd,
Bean Huo28fa68f2021-01-05 12:34:42 +0100349 enum ufs_trace_str_t str_t)
Stanley Chuaa5c6972020-06-15 15:22:35 +0800350{
351 u32 cmd;
352
353 if (!trace_ufshcd_uic_command_enabled())
354 return;
355
Bean Huo28fa68f2021-01-05 12:34:42 +0100356 if (str_t == UFS_CMD_SEND)
Stanley Chuaa5c6972020-06-15 15:22:35 +0800357 cmd = ucmd->command;
358 else
359 cmd = ufshcd_readl(hba, REG_UIC_COMMAND);
360
Bean Huo28fa68f2021-01-05 12:34:42 +0100361 trace_ufshcd_uic_command(dev_name(hba->dev), str_t, cmd,
Stanley Chuaa5c6972020-06-15 15:22:35 +0800362 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1),
363 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2),
364 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3));
365}
366
Bean Huo28fa68f2021-01-05 12:34:42 +0100367static void ufshcd_add_command_trace(struct ufs_hba *hba, unsigned int tag,
368 enum ufs_trace_str_t str_t)
Lee Susman1a07f2d2016-12-22 18:42:03 -0800369{
Colin Ian King102851f2021-08-04 14:32:41 +0100370 u64 lba;
Jaegeuk Kim69a314d2020-11-17 08:58:37 -0800371 u8 opcode = 0, group_id = 0;
Lee Susman1a07f2d2016-12-22 18:42:03 -0800372 u32 intr, doorbell;
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300373 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800374 struct scsi_cmnd *cmd = lrbp->cmd;
Bart Van Assche3f2c1002021-08-09 16:03:50 -0700375 struct request *rq = scsi_cmd_to_rq(cmd);
Lee Susman1a07f2d2016-12-22 18:42:03 -0800376 int transfer_len = -1;
377
Bean Huo44b5de32021-05-31 12:43:07 +0200378 if (!cmd)
379 return;
380
Bean Huo44b5de32021-05-31 12:43:07 +0200381 /* trace UPIU also */
382 ufshcd_add_cmd_upiu_trace(hba, tag, str_t);
Bean Huof0101af2021-08-02 20:08:03 +0200383 if (!trace_ufshcd_command_enabled())
384 return;
385
Bean Huo44b5de32021-05-31 12:43:07 +0200386 opcode = cmd->cmnd[0];
Martin K. Petersen54815082021-06-08 23:39:29 -0400387 lba = scsi_get_lba(cmd);
Bean Huo04c073f2021-05-31 12:43:05 +0200388
Bean Huo44b5de32021-05-31 12:43:07 +0200389 if (opcode == READ_10 || opcode == WRITE_10) {
390 /*
391 * Currently we only fully trace read(10) and write(10) commands
392 */
393 transfer_len =
394 be32_to_cpu(lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
395 if (opcode == WRITE_10)
396 group_id = lrbp->cmd->cmnd[6];
397 } else if (opcode == UNMAP) {
398 /*
399 * The number of Bytes to be unmapped beginning with the lba.
400 */
Bart Van Assche3f2c1002021-08-09 16:03:50 -0700401 transfer_len = blk_rq_bytes(rq);
Lee Susman1a07f2d2016-12-22 18:42:03 -0800402 }
403
404 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
405 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Bean Huo28fa68f2021-01-05 12:34:42 +0100406 trace_ufshcd_command(dev_name(hba->dev), str_t, tag,
Jaegeuk Kim69a314d2020-11-17 08:58:37 -0800407 doorbell, transfer_len, intr, lba, opcode, group_id);
Lee Susman1a07f2d2016-12-22 18:42:03 -0800408}
409
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800410static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
411{
412 struct ufs_clk_info *clki;
413 struct list_head *head = &hba->clk_list_head;
414
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300415 if (list_empty(head))
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800416 return;
417
418 list_for_each_entry(clki, head, list) {
419 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
420 clki->max_freq)
421 dev_err(hba->dev, "clk: %s, rate: %u\n",
422 clki->name, clki->curr_freq);
423 }
424}
425
Stanley Chue965e5e2020-12-05 19:58:59 +0800426static void ufshcd_print_evt(struct ufs_hba *hba, u32 id,
427 char *err_name)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800428{
429 int i;
Stanley Chu27752642019-01-28 22:04:26 +0800430 bool found = false;
Stanley Chue965e5e2020-12-05 19:58:59 +0800431 struct ufs_event_hist *e;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800432
Stanley Chue965e5e2020-12-05 19:58:59 +0800433 if (id >= UFS_EVT_CNT)
434 return;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800435
Stanley Chue965e5e2020-12-05 19:58:59 +0800436 e = &hba->ufs_stats.event[id];
437
438 for (i = 0; i < UFS_EVENT_HIST_LENGTH; i++) {
439 int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH;
440
441 if (e->tstamp[p] == 0)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800442 continue;
Stanley Chuc5397f12019-07-10 21:38:20 +0800443 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p,
Stanley Chue965e5e2020-12-05 19:58:59 +0800444 e->val[p], ktime_to_us(e->tstamp[p]));
Stanley Chu27752642019-01-28 22:04:26 +0800445 found = true;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800446 }
Stanley Chu27752642019-01-28 22:04:26 +0800447
448 if (!found)
Stanley Chufd1fb4d2020-01-04 22:26:08 +0800449 dev_err(hba->dev, "No record of %s\n", err_name);
DooHyun Hwangbafd09f2021-02-03 19:14:43 +0900450 else
451 dev_err(hba->dev, "%s: total cnt=%llu\n", err_name, e->cnt);
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800452}
453
Stanley Chue965e5e2020-12-05 19:58:59 +0800454static void ufshcd_print_evt_hist(struct ufs_hba *hba)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800455{
Tomas Winklerba809172018-06-14 11:14:09 +0300456 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800457
Stanley Chue965e5e2020-12-05 19:58:59 +0800458 ufshcd_print_evt(hba, UFS_EVT_PA_ERR, "pa_err");
459 ufshcd_print_evt(hba, UFS_EVT_DL_ERR, "dl_err");
460 ufshcd_print_evt(hba, UFS_EVT_NL_ERR, "nl_err");
461 ufshcd_print_evt(hba, UFS_EVT_TL_ERR, "tl_err");
462 ufshcd_print_evt(hba, UFS_EVT_DME_ERR, "dme_err");
463 ufshcd_print_evt(hba, UFS_EVT_AUTO_HIBERN8_ERR,
464 "auto_hibern8_err");
465 ufshcd_print_evt(hba, UFS_EVT_FATAL_ERR, "fatal_err");
466 ufshcd_print_evt(hba, UFS_EVT_LINK_STARTUP_FAIL,
467 "link_startup_fail");
468 ufshcd_print_evt(hba, UFS_EVT_RESUME_ERR, "resume_fail");
469 ufshcd_print_evt(hba, UFS_EVT_SUSPEND_ERR,
470 "suspend_fail");
471 ufshcd_print_evt(hba, UFS_EVT_DEV_RESET, "dev_reset");
472 ufshcd_print_evt(hba, UFS_EVT_HOST_RESET, "host_reset");
473 ufshcd_print_evt(hba, UFS_EVT_ABORT, "task_abort");
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800474
Stanley Chu7c486d912019-12-24 21:01:06 +0800475 ufshcd_vops_dbg_register_dump(hba);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800476}
477
478static
479void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
480{
481 struct ufshcd_lrb *lrbp;
Gilad Broner7fabb772017-02-03 16:56:50 -0800482 int prdt_length;
Dolev Raviv66cc8202016-12-22 18:39:42 -0800483 int tag;
484
485 for_each_set_bit(tag, &bitmap, hba->nutrs) {
486 lrbp = &hba->lrb[tag];
487
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800488 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
489 tag, ktime_to_us(lrbp->issue_time_stamp));
Zang Leigang09017182017-09-27 10:06:06 +0800490 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
491 tag, ktime_to_us(lrbp->compl_time_stamp));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800492 dev_err(hba->dev,
493 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
494 tag, (u64)lrbp->utrd_dma_addr);
495
Dolev Raviv66cc8202016-12-22 18:39:42 -0800496 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
497 sizeof(struct utp_transfer_req_desc));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800498 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
499 (u64)lrbp->ucd_req_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800500 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
501 sizeof(struct utp_upiu_req));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800502 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
503 (u64)lrbp->ucd_rsp_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800504 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
505 sizeof(struct utp_upiu_rsp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800506
Gilad Broner7fabb772017-02-03 16:56:50 -0800507 prdt_length = le16_to_cpu(
508 lrbp->utr_descriptor_ptr->prd_table_length);
Eric Biggerscc770ce2020-08-25 19:10:40 -0700509 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
510 prdt_length /= sizeof(struct ufshcd_sg_entry);
511
Gilad Broner7fabb772017-02-03 16:56:50 -0800512 dev_err(hba->dev,
513 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
514 tag, prdt_length,
515 (u64)lrbp->ucd_prdt_dma_addr);
516
517 if (pr_prdt)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800518 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
Gilad Broner7fabb772017-02-03 16:56:50 -0800519 sizeof(struct ufshcd_sg_entry) * prdt_length);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800520 }
521}
522
523static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
524{
Dolev Raviv66cc8202016-12-22 18:39:42 -0800525 int tag;
526
527 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
Christoph Hellwig391e3882018-10-07 17:30:32 +0300528 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag];
529
Dolev Raviv66cc8202016-12-22 18:39:42 -0800530 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
Christoph Hellwig391e3882018-10-07 17:30:32 +0300531 ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800532 }
533}
534
Gilad Broner6ba65582017-02-03 16:57:28 -0800535static void ufshcd_print_host_state(struct ufs_hba *hba)
536{
Can Guo3f8af602020-08-09 05:15:50 -0700537 struct scsi_device *sdev_ufs = hba->sdev_ufs_device;
538
Gilad Broner6ba65582017-02-03 16:57:28 -0800539 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
Bart Van Assche7252a362019-12-09 10:13:08 -0800540 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n",
541 hba->outstanding_reqs, hba->outstanding_tasks);
Gilad Broner6ba65582017-02-03 16:57:28 -0800542 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
543 hba->saved_err, hba->saved_uic_err);
544 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
545 hba->curr_dev_pwr_mode, hba->uic_link_state);
546 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
547 hba->pm_op_in_progress, hba->is_sys_suspended);
548 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
549 hba->auto_bkops_enabled, hba->host->host_self_blocked);
550 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
Can Guo3f8af602020-08-09 05:15:50 -0700551 dev_err(hba->dev,
552 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt=%d\n",
553 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
554 hba->ufs_stats.hibern8_exit_cnt);
555 dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n",
556 ktime_to_us(hba->ufs_stats.last_intr_ts),
557 hba->ufs_stats.last_intr_status);
Gilad Broner6ba65582017-02-03 16:57:28 -0800558 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
559 hba->eh_flags, hba->req_abort_count);
Can Guo3f8af602020-08-09 05:15:50 -0700560 dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n",
561 hba->ufs_version, hba->capabilities, hba->caps);
Gilad Broner6ba65582017-02-03 16:57:28 -0800562 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
563 hba->dev_quirks);
Can Guo3f8af602020-08-09 05:15:50 -0700564 if (sdev_ufs)
565 dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n",
566 sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev);
567
568 ufshcd_print_clk_freqs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -0800569}
570
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800571/**
572 * ufshcd_print_pwr_info - print power params as saved in hba
573 * power info
574 * @hba: per-adapter instance
575 */
576static void ufshcd_print_pwr_info(struct ufs_hba *hba)
577{
578 static const char * const names[] = {
579 "INVALID MODE",
580 "FAST MODE",
581 "SLOW_MODE",
582 "INVALID MODE",
583 "FASTAUTO_MODE",
584 "SLOWAUTO_MODE",
585 "INVALID MODE",
586 };
587
588 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
589 __func__,
590 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
591 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
592 names[hba->pwr_info.pwr_rx],
593 names[hba->pwr_info.pwr_tx],
594 hba->pwr_info.hs_rate);
595}
596
Stanley Chu31a5d9c2020-12-08 21:56:35 +0800597static void ufshcd_device_reset(struct ufs_hba *hba)
598{
599 int err;
600
601 err = ufshcd_vops_device_reset(hba);
602
603 if (!err) {
604 ufshcd_set_ufs_dev_active(hba);
605 if (ufshcd_is_wb_allowed(hba)) {
Bean Huo4cd48992021-01-19 17:38:46 +0100606 hba->dev_info.wb_enabled = false;
607 hba->dev_info.wb_buf_flush_enabled = false;
Stanley Chu31a5d9c2020-12-08 21:56:35 +0800608 }
609 }
610 if (err != -EOPNOTSUPP)
611 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err);
612}
613
Stanley Chu5c955c12020-03-18 18:40:12 +0800614void ufshcd_delay_us(unsigned long us, unsigned long tolerance)
615{
616 if (!us)
617 return;
618
619 if (us < 10)
620 udelay(us);
621 else
622 usleep_range(us, us + tolerance);
623}
624EXPORT_SYMBOL_GPL(ufshcd_delay_us);
625
Bart Van Assche5cac1092020-05-07 15:27:50 -0700626/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530627 * ufshcd_wait_for_register - wait for register value to change
Bart Van Assche5cac1092020-05-07 15:27:50 -0700628 * @hba: per-adapter interface
629 * @reg: mmio register offset
630 * @mask: mask to apply to the read register value
631 * @val: value to wait for
632 * @interval_us: polling interval in microseconds
633 * @timeout_ms: timeout in milliseconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530634 *
Bart Van Assche5cac1092020-05-07 15:27:50 -0700635 * Return:
636 * -ETIMEDOUT on error, zero on success.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530637 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200638int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
639 u32 val, unsigned long interval_us,
Bart Van Assche5cac1092020-05-07 15:27:50 -0700640 unsigned long timeout_ms)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530641{
642 int err = 0;
643 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
644
645 /* ignore bits that we don't intend to wait on */
646 val = val & mask;
647
648 while ((ufshcd_readl(hba, reg) & mask) != val) {
Bart Van Assche5cac1092020-05-07 15:27:50 -0700649 usleep_range(interval_us, interval_us + 50);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530650 if (time_after(jiffies, timeout)) {
651 if ((ufshcd_readl(hba, reg) & mask) != val)
652 err = -ETIMEDOUT;
653 break;
654 }
655 }
656
657 return err;
658}
659
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530660/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530661 * ufshcd_get_intr_mask - Get the interrupt bit mask
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800662 * @hba: Pointer to adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530663 *
664 * Returns interrupt bit mask per version
665 */
666static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
667{
Caleb Connolly51428812021-03-10 15:33:42 +0000668 if (hba->ufs_version == ufshci_version(1, 0))
669 return INTERRUPT_MASK_ALL_VER_10;
670 if (hba->ufs_version <= ufshci_version(2, 0))
671 return INTERRUPT_MASK_ALL_VER_11;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800672
Caleb Connolly51428812021-03-10 15:33:42 +0000673 return INTERRUPT_MASK_ALL_VER_21;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530674}
675
676/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530677 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800678 * @hba: Pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530679 *
680 * Returns UFSHCI version supported by the controller
681 */
682static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
683{
Caleb Connolly51428812021-03-10 15:33:42 +0000684 u32 ufshci_ver;
Yaniv Gardi9949e702015-05-17 18:55:05 +0300685
Caleb Connolly51428812021-03-10 15:33:42 +0000686 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
687 ufshci_ver = ufshcd_vops_get_ufs_hci_version(hba);
688 else
689 ufshci_ver = ufshcd_readl(hba, REG_UFS_VERSION);
690
691 /*
692 * UFSHCI v1.x uses a different version scheme, in order
693 * to allow the use of comparisons with the ufshci_version
694 * function, we convert it to the same scheme as ufs 2.0+.
695 */
696 if (ufshci_ver & 0x00010000)
697 return ufshci_version(1, ufshci_ver & 0x00000100);
698
699 return ufshci_ver;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530700}
701
702/**
703 * ufshcd_is_device_present - Check if any device connected to
704 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300705 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530706 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300707 * Returns true if device present, false if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530708 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300709static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530710{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300711 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300712 DEVICE_PRESENT) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530713}
714
715/**
716 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800717 * @lrbp: pointer to local command reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530718 *
719 * This function is used to get the OCS field from UTRD
720 * Returns the OCS field in the UTRD
721 */
Bart Van Assche957d63e2021-10-20 14:40:17 -0700722static enum utp_ocs ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530723{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530724 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530725}
726
727/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530728 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
729 * @hba: per adapter instance
730 * @pos: position of the bit to be cleared
731 */
732static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
733{
Alim Akhtar87183842020-05-28 06:46:49 +0530734 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
735 ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
736 else
737 ufshcd_writel(hba, ~(1 << pos),
738 REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Alim Akhtar1399c5b2018-05-06 15:44:15 +0530739}
740
741/**
742 * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register
743 * @hba: per adapter instance
744 * @pos: position of the bit to be cleared
745 */
746static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
747{
Alim Akhtar87183842020-05-28 06:46:49 +0530748 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
749 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
750 else
751 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530752}
753
754/**
755 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
756 * @reg: Register value of host controller status
757 *
758 * Returns integer, 0 on Success and positive value if failed
759 */
760static inline int ufshcd_get_lists_status(u32 reg)
761{
Tomohiro Kusumi6cf16112017-04-26 20:28:58 +0300762 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530763}
764
765/**
766 * ufshcd_get_uic_cmd_result - Get the UIC command result
767 * @hba: Pointer to adapter instance
768 *
769 * This function gets the result of UIC command completion
770 * Returns 0 on success, non zero value on error
771 */
772static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
773{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530774 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530775 MASK_UIC_COMMAND_RESULT;
776}
777
778/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530779 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
780 * @hba: Pointer to adapter instance
781 *
782 * This function gets UIC command argument3
783 * Returns 0 on success, non zero value on error
784 */
785static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
786{
787 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
788}
789
790/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530791 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530792 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530793 */
794static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530795ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530796{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530797 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530798}
799
800/**
801 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
802 * @ucd_rsp_ptr: pointer to response UPIU
803 *
804 * This function gets the response status and scsi_status from response UPIU
805 * Returns the response result code.
806 */
807static inline int
808ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
809{
810 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
811}
812
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530813/*
814 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
815 * from response UPIU
816 * @ucd_rsp_ptr: pointer to response UPIU
817 *
818 * Return the data segment length.
819 */
820static inline unsigned int
821ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
822{
823 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
824 MASK_RSP_UPIU_DATA_SEG_LEN;
825}
826
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530827/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530828 * ufshcd_is_exception_event - Check if the device raised an exception event
829 * @ucd_rsp_ptr: pointer to response UPIU
830 *
831 * The function checks if the device raised an exception event indicated in
832 * the Device Information field of response UPIU.
833 *
834 * Returns true if exception is raised, false otherwise.
835 */
836static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
837{
838 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
839 MASK_RSP_EXCEPTION_EVENT ? true : false;
840}
841
842/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530843 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530844 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530845 */
846static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530847ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530848{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530849 ufshcd_writel(hba, INT_AGGR_ENABLE |
850 INT_AGGR_COUNTER_AND_TIMER_RESET,
851 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
852}
853
854/**
855 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
856 * @hba: per adapter instance
857 * @cnt: Interrupt aggregation counter threshold
858 * @tmout: Interrupt aggregation timeout value
859 */
860static inline void
861ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
862{
863 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
864 INT_AGGR_COUNTER_THLD_VAL(cnt) |
865 INT_AGGR_TIMEOUT_VAL(tmout),
866 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530867}
868
869/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300870 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
871 * @hba: per adapter instance
872 */
873static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
874{
875 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
876}
877
878/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530879 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
880 * When run-stop registers are set to 1, it indicates the
881 * host controller that it can process the requests
882 * @hba: per adapter instance
883 */
884static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
885{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530886 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
887 REG_UTP_TASK_REQ_LIST_RUN_STOP);
888 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
889 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530890}
891
892/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530893 * ufshcd_hba_start - Start controller initialization sequence
894 * @hba: per adapter instance
895 */
896static inline void ufshcd_hba_start(struct ufs_hba *hba)
897{
Satya Tangiraladf043c742020-07-06 20:04:14 +0000898 u32 val = CONTROLLER_ENABLE;
899
900 if (ufshcd_crypto_enable(hba))
901 val |= CRYPTO_GENERAL_ENABLE;
902
903 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530904}
905
906/**
907 * ufshcd_is_hba_active - Get controller state
908 * @hba: per adapter instance
909 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300910 * Returns false if controller is active, true otherwise
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530911 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300912static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530913{
Tomohiro Kusumi4a8eec22017-03-28 16:49:25 +0300914 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
915 ? false : true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530916}
917
Yaniv Gardi37113102016-03-10 17:37:16 +0200918u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
919{
920 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
Caleb Connolly51428812021-03-10 15:33:42 +0000921 if (hba->ufs_version <= ufshci_version(1, 1))
Yaniv Gardi37113102016-03-10 17:37:16 +0200922 return UFS_UNIPRO_VER_1_41;
923 else
924 return UFS_UNIPRO_VER_1_6;
925}
926EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
927
928static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
929{
930 /*
931 * If both host and device support UniPro ver1.6 or later, PA layer
932 * parameters tuning happens during link startup itself.
933 *
934 * We can manually tune PA layer parameters if either host or device
935 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
936 * logic simple, we will only do manual tuning if local unipro version
937 * doesn't support ver1.6 or later.
938 */
939 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
940 return true;
941 else
942 return false;
943}
944
Subhash Jadavani394b9492020-03-26 02:25:40 -0700945/**
946 * ufshcd_set_clk_freq - set UFS controller clock frequencies
947 * @hba: per adapter instance
948 * @scale_up: If True, set max possible frequency othewise set low frequency
949 *
950 * Returns 0 if successful
951 * Returns < 0 for any other errors
952 */
953static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800954{
955 int ret = 0;
956 struct ufs_clk_info *clki;
957 struct list_head *head = &hba->clk_list_head;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800958
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300959 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800960 goto out;
961
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800962 list_for_each_entry(clki, head, list) {
963 if (!IS_ERR_OR_NULL(clki->clk)) {
964 if (scale_up && clki->max_freq) {
965 if (clki->curr_freq == clki->max_freq)
966 continue;
967
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800968 ret = clk_set_rate(clki->clk, clki->max_freq);
969 if (ret) {
970 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
971 __func__, clki->name,
972 clki->max_freq, ret);
973 break;
974 }
975 trace_ufshcd_clk_scaling(dev_name(hba->dev),
976 "scaled up", clki->name,
977 clki->curr_freq,
978 clki->max_freq);
979
980 clki->curr_freq = clki->max_freq;
981
982 } else if (!scale_up && clki->min_freq) {
983 if (clki->curr_freq == clki->min_freq)
984 continue;
985
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800986 ret = clk_set_rate(clki->clk, clki->min_freq);
987 if (ret) {
988 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
989 __func__, clki->name,
990 clki->min_freq, ret);
991 break;
992 }
993 trace_ufshcd_clk_scaling(dev_name(hba->dev),
994 "scaled down", clki->name,
995 clki->curr_freq,
996 clki->min_freq);
997 clki->curr_freq = clki->min_freq;
998 }
999 }
1000 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
1001 clki->name, clk_get_rate(clki->clk));
1002 }
1003
Subhash Jadavani394b9492020-03-26 02:25:40 -07001004out:
1005 return ret;
1006}
1007
1008/**
1009 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
1010 * @hba: per adapter instance
1011 * @scale_up: True if scaling up and false if scaling down
1012 *
1013 * Returns 0 if successful
1014 * Returns < 0 for any other errors
1015 */
1016static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
1017{
1018 int ret = 0;
1019 ktime_t start = ktime_get();
1020
1021 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
1022 if (ret)
1023 goto out;
1024
1025 ret = ufshcd_set_clk_freq(hba, scale_up);
1026 if (ret)
1027 goto out;
1028
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001029 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
Subhash Jadavani394b9492020-03-26 02:25:40 -07001030 if (ret)
1031 ufshcd_set_clk_freq(hba, !scale_up);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001032
1033out:
Subhash Jadavani394b9492020-03-26 02:25:40 -07001034 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001035 (scale_up ? "up" : "down"),
1036 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1037 return ret;
1038}
1039
1040/**
1041 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
1042 * @hba: per adapter instance
1043 * @scale_up: True if scaling up and false if scaling down
1044 *
1045 * Returns true if scaling is required, false otherwise.
1046 */
1047static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
1048 bool scale_up)
1049{
1050 struct ufs_clk_info *clki;
1051 struct list_head *head = &hba->clk_list_head;
1052
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03001053 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001054 return false;
1055
1056 list_for_each_entry(clki, head, list) {
1057 if (!IS_ERR_OR_NULL(clki->clk)) {
1058 if (scale_up && clki->max_freq) {
1059 if (clki->curr_freq == clki->max_freq)
1060 continue;
1061 return true;
1062 } else if (!scale_up && clki->min_freq) {
1063 if (clki->curr_freq == clki->min_freq)
1064 continue;
1065 return true;
1066 }
1067 }
1068 }
1069
1070 return false;
1071}
1072
Bart Van Assche8d077ed2021-12-03 15:19:49 -08001073/*
1074 * Determine the number of pending commands by counting the bits in the SCSI
1075 * device budget maps. This approach has been selected because a bit is set in
1076 * the budget map before scsi_host_queue_ready() checks the host_self_blocked
1077 * flag. The host_self_blocked flag can be modified by calling
1078 * scsi_block_requests() or scsi_unblock_requests().
1079 */
1080static u32 ufshcd_pending_cmds(struct ufs_hba *hba)
1081{
1082 struct scsi_device *sdev;
1083 u32 pending = 0;
1084
Bean Huo99c66a82021-12-14 13:05:37 +01001085 lockdep_assert_held(hba->host->host_lock);
1086 __shost_for_each_device(sdev, hba->host)
Bart Van Assche8d077ed2021-12-03 15:19:49 -08001087 pending += sbitmap_weight(&sdev->budget_map);
1088
1089 return pending;
1090}
1091
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001092static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
1093 u64 wait_timeout_us)
1094{
1095 unsigned long flags;
1096 int ret = 0;
1097 u32 tm_doorbell;
Bart Van Assche8d077ed2021-12-03 15:19:49 -08001098 u32 tr_pending;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001099 bool timeout = false, do_last_check = false;
1100 ktime_t start;
1101
1102 ufshcd_hold(hba, false);
1103 spin_lock_irqsave(hba->host->host_lock, flags);
1104 /*
1105 * Wait for all the outstanding tasks/transfer requests.
1106 * Verify by checking the doorbell registers are clear.
1107 */
1108 start = ktime_get();
1109 do {
1110 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
1111 ret = -EBUSY;
1112 goto out;
1113 }
1114
1115 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Bart Van Assche8d077ed2021-12-03 15:19:49 -08001116 tr_pending = ufshcd_pending_cmds(hba);
1117 if (!tm_doorbell && !tr_pending) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001118 timeout = false;
1119 break;
1120 } else if (do_last_check) {
1121 break;
1122 }
1123
1124 spin_unlock_irqrestore(hba->host->host_lock, flags);
1125 schedule();
1126 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
1127 wait_timeout_us) {
1128 timeout = true;
1129 /*
1130 * We might have scheduled out for long time so make
1131 * sure to check if doorbells are cleared by this time
1132 * or not.
1133 */
1134 do_last_check = true;
1135 }
1136 spin_lock_irqsave(hba->host->host_lock, flags);
Bart Van Assche8d077ed2021-12-03 15:19:49 -08001137 } while (tm_doorbell || tr_pending);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001138
1139 if (timeout) {
1140 dev_err(hba->dev,
1141 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
Bart Van Assche8d077ed2021-12-03 15:19:49 -08001142 __func__, tm_doorbell, tr_pending);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001143 ret = -EBUSY;
1144 }
1145out:
1146 spin_unlock_irqrestore(hba->host->host_lock, flags);
1147 ufshcd_release(hba);
1148 return ret;
1149}
1150
1151/**
1152 * ufshcd_scale_gear - scale up/down UFS gear
1153 * @hba: per adapter instance
1154 * @scale_up: True for scaling up gear and false for scaling down
1155 *
1156 * Returns 0 for success,
1157 * Returns -EBUSY if scaling can't happen at this time
1158 * Returns non-zero for any other errors
1159 */
1160static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1161{
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001162 int ret = 0;
1163 struct ufs_pa_layer_attr new_pwr_info;
1164
1165 if (scale_up) {
1166 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1167 sizeof(struct ufs_pa_layer_attr));
1168 } else {
1169 memcpy(&new_pwr_info, &hba->pwr_info,
1170 sizeof(struct ufs_pa_layer_attr));
1171
Can Guo29b87e92020-11-26 17:58:48 -08001172 if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear ||
1173 hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001174 /* save the current power mode */
1175 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1176 &hba->pwr_info,
1177 sizeof(struct ufs_pa_layer_attr));
1178
1179 /* scale down gear */
Can Guo29b87e92020-11-26 17:58:48 -08001180 new_pwr_info.gear_tx = hba->clk_scaling.min_gear;
1181 new_pwr_info.gear_rx = hba->clk_scaling.min_gear;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001182 }
1183 }
1184
1185 /* check if the power mode needs to be changed or not? */
Can Guo6a9df812020-02-11 21:38:28 -08001186 ret = ufshcd_config_pwr_mode(hba, &new_pwr_info);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001187 if (ret)
1188 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1189 __func__, ret,
1190 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1191 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1192
1193 return ret;
1194}
1195
1196static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1197{
1198 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1199 int ret = 0;
1200 /*
1201 * make sure that there are no outstanding requests when
1202 * clock scaling is in progress
1203 */
Subhash Jadavani38135532018-05-03 16:37:18 +05301204 ufshcd_scsi_block_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001205 down_write(&hba->clk_scaling_lock);
Can Guo0e9d4ca2021-01-20 02:04:21 -08001206
1207 if (!hba->clk_scaling.is_allowed ||
1208 ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001209 ret = -EBUSY;
1210 up_write(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301211 ufshcd_scsi_unblock_requests(hba);
Can Guo0e9d4ca2021-01-20 02:04:21 -08001212 goto out;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001213 }
1214
Can Guo0e9d4ca2021-01-20 02:04:21 -08001215 /* let's not get into low power until clock scaling is completed */
1216 ufshcd_hold(hba, false);
1217
1218out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001219 return ret;
1220}
1221
Can Guo0e9d4ca2021-01-20 02:04:21 -08001222static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, bool writelock)
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001223{
Can Guo0e9d4ca2021-01-20 02:04:21 -08001224 if (writelock)
1225 up_write(&hba->clk_scaling_lock);
1226 else
1227 up_read(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301228 ufshcd_scsi_unblock_requests(hba);
Can Guo0e9d4ca2021-01-20 02:04:21 -08001229 ufshcd_release(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001230}
1231
1232/**
1233 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1234 * @hba: per adapter instance
1235 * @scale_up: True for scaling up and false for scalin down
1236 *
1237 * Returns 0 for success,
1238 * Returns -EBUSY if scaling can't happen at this time
1239 * Returns non-zero for any other errors
1240 */
1241static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1242{
1243 int ret = 0;
Can Guo0e9d4ca2021-01-20 02:04:21 -08001244 bool is_writelock = true;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001245
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001246 ret = ufshcd_clock_scaling_prepare(hba);
1247 if (ret)
Can Guo0e9d4ca2021-01-20 02:04:21 -08001248 return ret;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001249
1250 /* scale down the gear before scaling down clocks */
1251 if (!scale_up) {
1252 ret = ufshcd_scale_gear(hba, false);
1253 if (ret)
Subhash Jadavani394b9492020-03-26 02:25:40 -07001254 goto out_unprepare;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001255 }
1256
1257 ret = ufshcd_scale_clks(hba, scale_up);
1258 if (ret) {
1259 if (!scale_up)
1260 ufshcd_scale_gear(hba, true);
Subhash Jadavani394b9492020-03-26 02:25:40 -07001261 goto out_unprepare;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001262 }
1263
1264 /* scale up the gear after scaling up clocks */
1265 if (scale_up) {
1266 ret = ufshcd_scale_gear(hba, true);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001267 if (ret) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001268 ufshcd_scale_clks(hba, false);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001269 goto out_unprepare;
1270 }
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001271 }
1272
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001273 /* Enable Write Booster if we have scaled up else disable it */
Can Guo0e9d4ca2021-01-20 02:04:21 -08001274 downgrade_write(&hba->clk_scaling_lock);
1275 is_writelock = false;
Yue Hu3b5f3c02021-03-18 17:55:36 +08001276 ufshcd_wb_toggle(hba, scale_up);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001277
Subhash Jadavani394b9492020-03-26 02:25:40 -07001278out_unprepare:
Can Guo0e9d4ca2021-01-20 02:04:21 -08001279 ufshcd_clock_scaling_unprepare(hba, is_writelock);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001280 return ret;
1281}
1282
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001283static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1284{
1285 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1286 clk_scaling.suspend_work);
1287 unsigned long irq_flags;
1288
1289 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1290 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1291 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1292 return;
1293 }
1294 hba->clk_scaling.is_suspended = true;
1295 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1296
1297 __ufshcd_suspend_clkscaling(hba);
1298}
1299
1300static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1301{
1302 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1303 clk_scaling.resume_work);
1304 unsigned long irq_flags;
1305
1306 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1307 if (!hba->clk_scaling.is_suspended) {
1308 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1309 return;
1310 }
1311 hba->clk_scaling.is_suspended = false;
1312 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1313
1314 devfreq_resume_device(hba->devfreq);
1315}
1316
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001317static int ufshcd_devfreq_target(struct device *dev,
1318 unsigned long *freq, u32 flags)
1319{
1320 int ret = 0;
1321 struct ufs_hba *hba = dev_get_drvdata(dev);
1322 ktime_t start;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001323 bool scale_up, sched_clk_scaling_suspend_work = false;
Bjorn Andersson092b4552018-05-17 23:26:37 -07001324 struct list_head *clk_list = &hba->clk_list_head;
1325 struct ufs_clk_info *clki;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001326 unsigned long irq_flags;
1327
1328 if (!ufshcd_is_clkscaling_supported(hba))
1329 return -EINVAL;
1330
Asutosh Das91831d32020-03-25 11:29:00 -07001331 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list);
1332 /* Override with the closest supported frequency */
1333 *freq = (unsigned long) clk_round_rate(clki->clk, *freq);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001334 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1335 if (ufshcd_eh_in_progress(hba)) {
1336 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1337 return 0;
1338 }
1339
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001340 if (!hba->clk_scaling.active_reqs)
1341 sched_clk_scaling_suspend_work = true;
1342
Bjorn Andersson092b4552018-05-17 23:26:37 -07001343 if (list_empty(clk_list)) {
1344 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1345 goto out;
1346 }
1347
Asutosh Das91831d32020-03-25 11:29:00 -07001348 /* Decide based on the rounded-off frequency and update */
Bjorn Andersson092b4552018-05-17 23:26:37 -07001349 scale_up = (*freq == clki->max_freq) ? true : false;
Asutosh Das91831d32020-03-25 11:29:00 -07001350 if (!scale_up)
1351 *freq = clki->min_freq;
1352 /* Update the frequency */
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001353 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1354 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1355 ret = 0;
1356 goto out; /* no state change required */
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001357 }
1358 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1359
1360 start = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001361 ret = ufshcd_devfreq_scale(hba, scale_up);
1362
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001363 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1364 (scale_up ? "up" : "down"),
1365 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1366
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001367out:
1368 if (sched_clk_scaling_suspend_work)
1369 queue_work(hba->clk_scaling.workq,
1370 &hba->clk_scaling.suspend_work);
1371
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001372 return ret;
1373}
1374
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001375static int ufshcd_devfreq_get_dev_status(struct device *dev,
1376 struct devfreq_dev_status *stat)
1377{
1378 struct ufs_hba *hba = dev_get_drvdata(dev);
1379 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1380 unsigned long flags;
Asutosh Das91831d32020-03-25 11:29:00 -07001381 struct list_head *clk_list = &hba->clk_list_head;
1382 struct ufs_clk_info *clki;
Stanley Chub1bf66d2020-06-11 18:10:43 +08001383 ktime_t curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001384
1385 if (!ufshcd_is_clkscaling_supported(hba))
1386 return -EINVAL;
1387
1388 memset(stat, 0, sizeof(*stat));
1389
1390 spin_lock_irqsave(hba->host->host_lock, flags);
Stanley Chub1bf66d2020-06-11 18:10:43 +08001391 curr_t = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001392 if (!scaling->window_start_t)
1393 goto start_window;
1394
Asutosh Das91831d32020-03-25 11:29:00 -07001395 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1396 /*
1397 * If current frequency is 0, then the ondemand governor considers
1398 * there's no initial frequency set. And it always requests to set
1399 * to max. frequency.
1400 */
1401 stat->current_frequency = clki->curr_freq;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001402 if (scaling->is_busy_started)
Stanley Chub1bf66d2020-06-11 18:10:43 +08001403 scaling->tot_busy_t += ktime_us_delta(curr_t,
1404 scaling->busy_start_t);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001405
Stanley Chub1bf66d2020-06-11 18:10:43 +08001406 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001407 stat->busy_time = scaling->tot_busy_t;
1408start_window:
Stanley Chub1bf66d2020-06-11 18:10:43 +08001409 scaling->window_start_t = curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001410 scaling->tot_busy_t = 0;
1411
1412 if (hba->outstanding_reqs) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08001413 scaling->busy_start_t = curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001414 scaling->is_busy_started = true;
1415 } else {
1416 scaling->busy_start_t = 0;
1417 scaling->is_busy_started = false;
1418 }
1419 spin_unlock_irqrestore(hba->host->host_lock, flags);
1420 return 0;
1421}
1422
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001423static int ufshcd_devfreq_init(struct ufs_hba *hba)
1424{
Bjorn Andersson092b4552018-05-17 23:26:37 -07001425 struct list_head *clk_list = &hba->clk_list_head;
1426 struct ufs_clk_info *clki;
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001427 struct devfreq *devfreq;
1428 int ret;
1429
Bjorn Andersson092b4552018-05-17 23:26:37 -07001430 /* Skip devfreq if we don't have any clocks in the list */
1431 if (list_empty(clk_list))
1432 return 0;
1433
1434 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1435 dev_pm_opp_add(hba->dev, clki->min_freq, 0);
1436 dev_pm_opp_add(hba->dev, clki->max_freq, 0);
1437
Stanley Chu90b84912020-05-09 17:37:13 +08001438 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile,
1439 &hba->vps->ondemand_data);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001440 devfreq = devfreq_add_device(hba->dev,
Stanley Chu90b84912020-05-09 17:37:13 +08001441 &hba->vps->devfreq_profile,
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001442 DEVFREQ_GOV_SIMPLE_ONDEMAND,
Stanley Chu90b84912020-05-09 17:37:13 +08001443 &hba->vps->ondemand_data);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001444 if (IS_ERR(devfreq)) {
1445 ret = PTR_ERR(devfreq);
1446 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001447
1448 dev_pm_opp_remove(hba->dev, clki->min_freq);
1449 dev_pm_opp_remove(hba->dev, clki->max_freq);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001450 return ret;
1451 }
1452
1453 hba->devfreq = devfreq;
1454
1455 return 0;
1456}
1457
Bjorn Andersson092b4552018-05-17 23:26:37 -07001458static void ufshcd_devfreq_remove(struct ufs_hba *hba)
1459{
1460 struct list_head *clk_list = &hba->clk_list_head;
1461 struct ufs_clk_info *clki;
1462
1463 if (!hba->devfreq)
1464 return;
1465
1466 devfreq_remove_device(hba->devfreq);
1467 hba->devfreq = NULL;
1468
1469 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1470 dev_pm_opp_remove(hba->dev, clki->min_freq);
1471 dev_pm_opp_remove(hba->dev, clki->max_freq);
1472}
1473
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001474static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1475{
1476 unsigned long flags;
1477
1478 devfreq_suspend_device(hba->devfreq);
1479 spin_lock_irqsave(hba->host->host_lock, flags);
1480 hba->clk_scaling.window_start_t = 0;
1481 spin_unlock_irqrestore(hba->host->host_lock, flags);
1482}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001483
Gilad Bronera5082532016-10-17 17:10:00 -07001484static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1485{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001486 unsigned long flags;
1487 bool suspend = false;
1488
Stanley Chuf9a7fa32021-01-20 23:01:40 +08001489 cancel_work_sync(&hba->clk_scaling.suspend_work);
1490 cancel_work_sync(&hba->clk_scaling.resume_work);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001491
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001492 spin_lock_irqsave(hba->host->host_lock, flags);
1493 if (!hba->clk_scaling.is_suspended) {
1494 suspend = true;
1495 hba->clk_scaling.is_suspended = true;
1496 }
1497 spin_unlock_irqrestore(hba->host->host_lock, flags);
1498
1499 if (suspend)
1500 __ufshcd_suspend_clkscaling(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07001501}
1502
1503static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1504{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001505 unsigned long flags;
1506 bool resume = false;
1507
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001508 spin_lock_irqsave(hba->host->host_lock, flags);
1509 if (hba->clk_scaling.is_suspended) {
1510 resume = true;
1511 hba->clk_scaling.is_suspended = false;
1512 }
1513 spin_unlock_irqrestore(hba->host->host_lock, flags);
1514
1515 if (resume)
1516 devfreq_resume_device(hba->devfreq);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001517}
1518
1519static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1520 struct device_attribute *attr, char *buf)
1521{
1522 struct ufs_hba *hba = dev_get_drvdata(dev);
1523
Jiapeng Chong1481b7f2021-03-02 14:08:18 +08001524 return sysfs_emit(buf, "%d\n", hba->clk_scaling.is_enabled);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001525}
1526
1527static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1528 struct device_attribute *attr, const char *buf, size_t count)
1529{
1530 struct ufs_hba *hba = dev_get_drvdata(dev);
1531 u32 value;
Can Guo9cd20d32021-01-13 19:13:28 -08001532 int err = 0;
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001533
1534 if (kstrtou32(buf, 0, &value))
1535 return -EINVAL;
1536
Can Guo9cd20d32021-01-13 19:13:28 -08001537 down(&hba->host_sem);
1538 if (!ufshcd_is_user_access_allowed(hba)) {
1539 err = -EBUSY;
1540 goto out;
1541 }
1542
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001543 value = !!value;
Can Guo0e9d4ca2021-01-20 02:04:21 -08001544 if (value == hba->clk_scaling.is_enabled)
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001545 goto out;
1546
Asutosh Dasb294ff32021-04-23 17:20:16 -07001547 ufshcd_rpm_get_sync(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001548 ufshcd_hold(hba, false);
1549
Can Guo0e9d4ca2021-01-20 02:04:21 -08001550 hba->clk_scaling.is_enabled = value;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001551
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001552 if (value) {
1553 ufshcd_resume_clkscaling(hba);
1554 } else {
1555 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001556 err = ufshcd_devfreq_scale(hba, true);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001557 if (err)
1558 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1559 __func__, err);
1560 }
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001561
1562 ufshcd_release(hba);
Asutosh Dasb294ff32021-04-23 17:20:16 -07001563 ufshcd_rpm_put_sync(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001564out:
Can Guo9cd20d32021-01-13 19:13:28 -08001565 up(&hba->host_sem);
1566 return err ? err : count;
Gilad Bronera5082532016-10-17 17:10:00 -07001567}
1568
Can Guo4543d9d2021-01-20 02:04:22 -08001569static void ufshcd_init_clk_scaling_sysfs(struct ufs_hba *hba)
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001570{
1571 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1572 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1573 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1574 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1575 hba->clk_scaling.enable_attr.attr.mode = 0644;
1576 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1577 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1578}
1579
Can Guo4543d9d2021-01-20 02:04:22 -08001580static void ufshcd_remove_clk_scaling_sysfs(struct ufs_hba *hba)
1581{
1582 if (hba->clk_scaling.enable_attr.attr.name)
1583 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
1584}
1585
1586static void ufshcd_init_clk_scaling(struct ufs_hba *hba)
1587{
1588 char wq_name[sizeof("ufs_clkscaling_00")];
1589
1590 if (!ufshcd_is_clkscaling_supported(hba))
1591 return;
1592
Can Guo80d892f2021-01-27 18:49:27 -08001593 if (!hba->clk_scaling.min_gear)
1594 hba->clk_scaling.min_gear = UFS_HS_G1;
1595
Can Guo4543d9d2021-01-20 02:04:22 -08001596 INIT_WORK(&hba->clk_scaling.suspend_work,
1597 ufshcd_clk_scaling_suspend_work);
1598 INIT_WORK(&hba->clk_scaling.resume_work,
1599 ufshcd_clk_scaling_resume_work);
1600
1601 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d",
1602 hba->host->host_no);
1603 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
1604
1605 hba->clk_scaling.is_initialized = true;
1606}
1607
1608static void ufshcd_exit_clk_scaling(struct ufs_hba *hba)
1609{
1610 if (!hba->clk_scaling.is_initialized)
1611 return;
1612
1613 ufshcd_remove_clk_scaling_sysfs(hba);
1614 destroy_workqueue(hba->clk_scaling.workq);
1615 ufshcd_devfreq_remove(hba);
1616 hba->clk_scaling.is_initialized = false;
1617}
1618
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001619static void ufshcd_ungate_work(struct work_struct *work)
1620{
1621 int ret;
1622 unsigned long flags;
1623 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1624 clk_gating.ungate_work);
1625
1626 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1627
1628 spin_lock_irqsave(hba->host->host_lock, flags);
1629 if (hba->clk_gating.state == CLKS_ON) {
1630 spin_unlock_irqrestore(hba->host->host_lock, flags);
1631 goto unblock_reqs;
1632 }
1633
1634 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guodd7143e2020-10-27 12:10:36 -07001635 ufshcd_hba_vreg_set_hpm(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001636 ufshcd_setup_clocks(hba, true);
1637
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001638 ufshcd_enable_irq(hba);
1639
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001640 /* Exit from hibern8 */
1641 if (ufshcd_can_hibern8_during_gating(hba)) {
1642 /* Prevent gating in this path */
1643 hba->clk_gating.is_suspended = true;
1644 if (ufshcd_is_link_hibern8(hba)) {
1645 ret = ufshcd_uic_hibern8_exit(hba);
1646 if (ret)
1647 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1648 __func__, ret);
1649 else
1650 ufshcd_set_link_active(hba);
1651 }
1652 hba->clk_gating.is_suspended = false;
1653 }
1654unblock_reqs:
Subhash Jadavani38135532018-05-03 16:37:18 +05301655 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001656}
1657
1658/**
1659 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1660 * Also, exit from hibern8 mode and set the link as active.
1661 * @hba: per adapter instance
1662 * @async: This indicates whether caller should ungate clocks asynchronously.
1663 */
1664int ufshcd_hold(struct ufs_hba *hba, bool async)
1665{
1666 int rc = 0;
Stanley Chu93b6c5d2020-08-09 13:07:34 +08001667 bool flush_result;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001668 unsigned long flags;
1669
Bart Van Assche3489c342021-12-03 15:19:47 -08001670 if (!ufshcd_is_clkgating_allowed(hba) ||
1671 !hba->clk_gating.is_initialized)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001672 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001673 spin_lock_irqsave(hba->host->host_lock, flags);
1674 hba->clk_gating.active_reqs++;
1675
Sahitya Tummala856b3482014-09-25 15:32:34 +03001676start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001677 switch (hba->clk_gating.state) {
1678 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001679 /*
1680 * Wait for the ungate work to complete if in progress.
1681 * Though the clocks may be in ON state, the link could
1682 * still be in hibner8 state if hibern8 is allowed
1683 * during clock gating.
1684 * Make sure we exit hibern8 state also in addition to
1685 * clocks being ON.
1686 */
1687 if (ufshcd_can_hibern8_during_gating(hba) &&
1688 ufshcd_is_link_hibern8(hba)) {
Can Guoc63d6092020-02-10 19:40:48 -08001689 if (async) {
1690 rc = -EAGAIN;
1691 hba->clk_gating.active_reqs--;
1692 break;
1693 }
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001694 spin_unlock_irqrestore(hba->host->host_lock, flags);
Stanley Chu93b6c5d2020-08-09 13:07:34 +08001695 flush_result = flush_work(&hba->clk_gating.ungate_work);
1696 if (hba->clk_gating.is_suspended && !flush_result)
1697 goto out;
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001698 spin_lock_irqsave(hba->host->host_lock, flags);
1699 goto start;
1700 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001701 break;
1702 case REQ_CLKS_OFF:
1703 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1704 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001705 trace_ufshcd_clk_gating(dev_name(hba->dev),
1706 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001707 break;
1708 }
1709 /*
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +03001710 * If we are here, it means gating work is either done or
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001711 * currently running. Hence, fall through to cancel gating
1712 * work and to enable clocks.
1713 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001714 fallthrough;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001715 case CLKS_OFF:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001716 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001717 trace_ufshcd_clk_gating(dev_name(hba->dev),
1718 hba->clk_gating.state);
Can Guoda3fecb2020-11-02 22:24:39 -08001719 if (queue_work(hba->clk_gating.clk_gating_workq,
1720 &hba->clk_gating.ungate_work))
1721 ufshcd_scsi_block_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001722 /*
1723 * fall through to check if we should wait for this
1724 * work to be done or not.
1725 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001726 fallthrough;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001727 case REQ_CLKS_ON:
1728 if (async) {
1729 rc = -EAGAIN;
1730 hba->clk_gating.active_reqs--;
1731 break;
1732 }
1733
1734 spin_unlock_irqrestore(hba->host->host_lock, flags);
1735 flush_work(&hba->clk_gating.ungate_work);
1736 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001737 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001738 goto start;
1739 default:
1740 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1741 __func__, hba->clk_gating.state);
1742 break;
1743 }
1744 spin_unlock_irqrestore(hba->host->host_lock, flags);
1745out:
1746 return rc;
1747}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001748EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001749
1750static void ufshcd_gate_work(struct work_struct *work)
1751{
1752 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1753 clk_gating.gate_work.work);
1754 unsigned long flags;
Can Guo4db7a232020-08-09 05:15:51 -07001755 int ret;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001756
1757 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -07001758 /*
1759 * In case you are here to cancel this work the gating state
1760 * would be marked as REQ_CLKS_ON. In this case save time by
1761 * skipping the gating work and exit after changing the clock
1762 * state to CLKS_ON.
1763 */
1764 if (hba->clk_gating.is_suspended ||
Asutosh Das18f013742019-11-14 22:09:29 -08001765 (hba->clk_gating.state != REQ_CLKS_OFF)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001766 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001767 trace_ufshcd_clk_gating(dev_name(hba->dev),
1768 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001769 goto rel_lock;
1770 }
1771
1772 if (hba->clk_gating.active_reqs
1773 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
Bart Van Asschebd0b3532021-12-03 15:19:40 -08001774 || hba->outstanding_reqs || hba->outstanding_tasks
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001775 || hba->active_uic_cmd || hba->uic_async_done)
1776 goto rel_lock;
1777
1778 spin_unlock_irqrestore(hba->host->host_lock, flags);
1779
1780 /* put the link into hibern8 mode before turning off clocks */
1781 if (ufshcd_can_hibern8_during_gating(hba)) {
Can Guo4db7a232020-08-09 05:15:51 -07001782 ret = ufshcd_uic_hibern8_enter(hba);
1783 if (ret) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001784 hba->clk_gating.state = CLKS_ON;
Can Guo4db7a232020-08-09 05:15:51 -07001785 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
1786 __func__, ret);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001787 trace_ufshcd_clk_gating(dev_name(hba->dev),
1788 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001789 goto out;
1790 }
1791 ufshcd_set_link_hibern8(hba);
1792 }
1793
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001794 ufshcd_disable_irq(hba);
1795
Can Guo81309c22020-11-25 18:01:00 -08001796 ufshcd_setup_clocks(hba, false);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001797
Can Guodd7143e2020-10-27 12:10:36 -07001798 /* Put the host controller in low power mode if possible */
1799 ufshcd_hba_vreg_set_lpm(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001800 /*
1801 * In case you are here to cancel this work the gating state
1802 * would be marked as REQ_CLKS_ON. In this case keep the state
1803 * as REQ_CLKS_ON which would anyway imply that clocks are off
1804 * and a request to turn them on is pending. By doing this way,
1805 * we keep the state machine in tact and this would ultimately
1806 * prevent from doing cancel work multiple times when there are
1807 * new requests arriving before the current cancel work is done.
1808 */
1809 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001810 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001811 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001812 trace_ufshcd_clk_gating(dev_name(hba->dev),
1813 hba->clk_gating.state);
1814 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001815rel_lock:
1816 spin_unlock_irqrestore(hba->host->host_lock, flags);
1817out:
1818 return;
1819}
1820
1821/* host lock must be held before calling this variant */
1822static void __ufshcd_release(struct ufs_hba *hba)
1823{
1824 if (!ufshcd_is_clkgating_allowed(hba))
1825 return;
1826
1827 hba->clk_gating.active_reqs--;
1828
Can Guo4db7a232020-08-09 05:15:51 -07001829 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
1830 hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
Bart Van Assche3489c342021-12-03 15:19:47 -08001831 hba->outstanding_tasks || !hba->clk_gating.is_initialized ||
Jaegeuk Kimfd62de12020-11-17 08:58:33 -08001832 hba->active_uic_cmd || hba->uic_async_done ||
1833 hba->clk_gating.state == CLKS_OFF)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001834 return;
1835
1836 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001837 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Evan Greenf4bb7702018-10-05 10:27:32 -07001838 queue_delayed_work(hba->clk_gating.clk_gating_workq,
1839 &hba->clk_gating.gate_work,
1840 msecs_to_jiffies(hba->clk_gating.delay_ms));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001841}
1842
1843void ufshcd_release(struct ufs_hba *hba)
1844{
1845 unsigned long flags;
1846
1847 spin_lock_irqsave(hba->host->host_lock, flags);
1848 __ufshcd_release(hba);
1849 spin_unlock_irqrestore(hba->host->host_lock, flags);
1850}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001851EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001852
1853static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1854 struct device_attribute *attr, char *buf)
1855{
1856 struct ufs_hba *hba = dev_get_drvdata(dev);
1857
DooHyun Hwangbafd09f2021-02-03 19:14:43 +09001858 return sysfs_emit(buf, "%lu\n", hba->clk_gating.delay_ms);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001859}
1860
1861static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1862 struct device_attribute *attr, const char *buf, size_t count)
1863{
1864 struct ufs_hba *hba = dev_get_drvdata(dev);
1865 unsigned long flags, value;
1866
1867 if (kstrtoul(buf, 0, &value))
1868 return -EINVAL;
1869
1870 spin_lock_irqsave(hba->host->host_lock, flags);
1871 hba->clk_gating.delay_ms = value;
1872 spin_unlock_irqrestore(hba->host->host_lock, flags);
1873 return count;
1874}
1875
Sahitya Tummalab4274112016-12-22 18:40:39 -08001876static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1877 struct device_attribute *attr, char *buf)
1878{
1879 struct ufs_hba *hba = dev_get_drvdata(dev);
1880
DooHyun Hwangbafd09f2021-02-03 19:14:43 +09001881 return sysfs_emit(buf, "%d\n", hba->clk_gating.is_enabled);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001882}
1883
1884static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1885 struct device_attribute *attr, const char *buf, size_t count)
1886{
1887 struct ufs_hba *hba = dev_get_drvdata(dev);
1888 unsigned long flags;
1889 u32 value;
1890
1891 if (kstrtou32(buf, 0, &value))
1892 return -EINVAL;
1893
1894 value = !!value;
Jaegeuk Kimb6645112020-11-17 08:58:34 -08001895
1896 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001897 if (value == hba->clk_gating.is_enabled)
1898 goto out;
1899
Jaegeuk Kimb6645112020-11-17 08:58:34 -08001900 if (value)
1901 __ufshcd_release(hba);
1902 else
Sahitya Tummalab4274112016-12-22 18:40:39 -08001903 hba->clk_gating.active_reqs++;
Sahitya Tummalab4274112016-12-22 18:40:39 -08001904
1905 hba->clk_gating.is_enabled = value;
1906out:
Jaegeuk Kimb6645112020-11-17 08:58:34 -08001907 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001908 return count;
1909}
1910
Can Guo4543d9d2021-01-20 02:04:22 -08001911static void ufshcd_init_clk_gating_sysfs(struct ufs_hba *hba)
Vivek Gautameebcc192018-08-07 23:17:39 +05301912{
Can Guo4543d9d2021-01-20 02:04:22 -08001913 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1914 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1915 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1916 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
1917 hba->clk_gating.delay_attr.attr.mode = 0644;
1918 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1919 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Vivek Gautameebcc192018-08-07 23:17:39 +05301920
Can Guo4543d9d2021-01-20 02:04:22 -08001921 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1922 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1923 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1924 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1925 hba->clk_gating.enable_attr.attr.mode = 0644;
1926 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1927 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Vivek Gautameebcc192018-08-07 23:17:39 +05301928}
1929
Can Guo4543d9d2021-01-20 02:04:22 -08001930static void ufshcd_remove_clk_gating_sysfs(struct ufs_hba *hba)
Vivek Gautameebcc192018-08-07 23:17:39 +05301931{
Can Guo4543d9d2021-01-20 02:04:22 -08001932 if (hba->clk_gating.delay_attr.attr.name)
1933 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
1934 if (hba->clk_gating.enable_attr.attr.name)
1935 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Vivek Gautameebcc192018-08-07 23:17:39 +05301936}
1937
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001938static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1939{
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301940 char wq_name[sizeof("ufs_clk_gating_00")];
1941
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001942 if (!ufshcd_is_clkgating_allowed(hba))
1943 return;
1944
Can Guo2dec9472020-08-09 05:15:47 -07001945 hba->clk_gating.state = CLKS_ON;
1946
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001947 hba->clk_gating.delay_ms = 150;
1948 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1949 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1950
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301951 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
1952 hba->host->host_no);
1953 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
Jaegeuk Kime93e6e42020-11-17 08:58:36 -08001954 WQ_MEM_RECLAIM | WQ_HIGHPRI);
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301955
Can Guo4543d9d2021-01-20 02:04:22 -08001956 ufshcd_init_clk_gating_sysfs(hba);
1957
Sahitya Tummalab4274112016-12-22 18:40:39 -08001958 hba->clk_gating.is_enabled = true;
Can Guo4543d9d2021-01-20 02:04:22 -08001959 hba->clk_gating.is_initialized = true;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001960}
1961
1962static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1963{
Can Guo4543d9d2021-01-20 02:04:22 -08001964 if (!hba->clk_gating.is_initialized)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001965 return;
Bart Van Assche3489c342021-12-03 15:19:47 -08001966
Can Guo4543d9d2021-01-20 02:04:22 -08001967 ufshcd_remove_clk_gating_sysfs(hba);
Bart Van Assche3489c342021-12-03 15:19:47 -08001968
1969 /* Ungate the clock if necessary. */
1970 ufshcd_hold(hba, false);
Can Guo4543d9d2021-01-20 02:04:22 -08001971 hba->clk_gating.is_initialized = false;
Bart Van Assche3489c342021-12-03 15:19:47 -08001972 ufshcd_release(hba);
1973
1974 destroy_workqueue(hba->clk_gating.clk_gating_workq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001975}
1976
Sahitya Tummala856b3482014-09-25 15:32:34 +03001977/* Must be called with host lock acquired */
1978static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1979{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001980 bool queue_resume_work = false;
Stanley Chub1bf66d2020-06-11 18:10:43 +08001981 ktime_t curr_t = ktime_get();
Can Guoa45f9372021-05-24 01:36:57 -07001982 unsigned long flags;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001983
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001984 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001985 return;
1986
Can Guoa45f9372021-05-24 01:36:57 -07001987 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001988 if (!hba->clk_scaling.active_reqs++)
1989 queue_resume_work = true;
1990
Can Guoa45f9372021-05-24 01:36:57 -07001991 if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress) {
1992 spin_unlock_irqrestore(hba->host->host_lock, flags);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001993 return;
Can Guoa45f9372021-05-24 01:36:57 -07001994 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001995
1996 if (queue_resume_work)
1997 queue_work(hba->clk_scaling.workq,
1998 &hba->clk_scaling.resume_work);
1999
2000 if (!hba->clk_scaling.window_start_t) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08002001 hba->clk_scaling.window_start_t = curr_t;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08002002 hba->clk_scaling.tot_busy_t = 0;
2003 hba->clk_scaling.is_busy_started = false;
2004 }
2005
Sahitya Tummala856b3482014-09-25 15:32:34 +03002006 if (!hba->clk_scaling.is_busy_started) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08002007 hba->clk_scaling.busy_start_t = curr_t;
Sahitya Tummala856b3482014-09-25 15:32:34 +03002008 hba->clk_scaling.is_busy_started = true;
2009 }
Can Guoa45f9372021-05-24 01:36:57 -07002010 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sahitya Tummala856b3482014-09-25 15:32:34 +03002011}
2012
2013static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
2014{
2015 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
Can Guoa45f9372021-05-24 01:36:57 -07002016 unsigned long flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03002017
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08002018 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03002019 return;
2020
Can Guoa45f9372021-05-24 01:36:57 -07002021 spin_lock_irqsave(hba->host->host_lock, flags);
2022 hba->clk_scaling.active_reqs--;
Sahitya Tummala856b3482014-09-25 15:32:34 +03002023 if (!hba->outstanding_reqs && scaling->is_busy_started) {
2024 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
2025 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002026 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03002027 scaling->is_busy_started = false;
2028 }
Can Guoa45f9372021-05-24 01:36:57 -07002029 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sahitya Tummala856b3482014-09-25 15:32:34 +03002030}
Can Guo1d8613a2021-04-21 19:28:39 -07002031
2032static inline int ufshcd_monitor_opcode2dir(u8 opcode)
2033{
2034 if (opcode == READ_6 || opcode == READ_10 || opcode == READ_16)
2035 return READ;
2036 else if (opcode == WRITE_6 || opcode == WRITE_10 || opcode == WRITE_16)
2037 return WRITE;
2038 else
2039 return -EINVAL;
2040}
2041
2042static inline bool ufshcd_should_inform_monitor(struct ufs_hba *hba,
2043 struct ufshcd_lrb *lrbp)
2044{
2045 struct ufs_hba_monitor *m = &hba->monitor;
2046
2047 return (m->enabled && lrbp && lrbp->cmd &&
2048 (!m->chunk_size || m->chunk_size == lrbp->cmd->sdb.length) &&
2049 ktime_before(hba->monitor.enabled_ts, lrbp->issue_time_stamp));
2050}
2051
2052static void ufshcd_start_monitor(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2053{
2054 int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd);
Can Guoa45f9372021-05-24 01:36:57 -07002055 unsigned long flags;
Can Guo1d8613a2021-04-21 19:28:39 -07002056
Can Guoa45f9372021-05-24 01:36:57 -07002057 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo1d8613a2021-04-21 19:28:39 -07002058 if (dir >= 0 && hba->monitor.nr_queued[dir]++ == 0)
2059 hba->monitor.busy_start_ts[dir] = ktime_get();
Can Guoa45f9372021-05-24 01:36:57 -07002060 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guo1d8613a2021-04-21 19:28:39 -07002061}
2062
2063static void ufshcd_update_monitor(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2064{
2065 int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd);
Can Guoa45f9372021-05-24 01:36:57 -07002066 unsigned long flags;
Can Guo1d8613a2021-04-21 19:28:39 -07002067
Can Guoa45f9372021-05-24 01:36:57 -07002068 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo1d8613a2021-04-21 19:28:39 -07002069 if (dir >= 0 && hba->monitor.nr_queued[dir] > 0) {
Bart Van Assche3f2c1002021-08-09 16:03:50 -07002070 struct request *req = scsi_cmd_to_rq(lrbp->cmd);
Can Guo1d8613a2021-04-21 19:28:39 -07002071 struct ufs_hba_monitor *m = &hba->monitor;
2072 ktime_t now, inc, lat;
2073
2074 now = lrbp->compl_time_stamp;
2075 inc = ktime_sub(now, m->busy_start_ts[dir]);
2076 m->total_busy[dir] = ktime_add(m->total_busy[dir], inc);
2077 m->nr_sec_rw[dir] += blk_rq_sectors(req);
2078
2079 /* Update latencies */
2080 m->nr_req[dir]++;
2081 lat = ktime_sub(now, lrbp->issue_time_stamp);
2082 m->lat_sum[dir] += lat;
2083 if (m->lat_max[dir] < lat || !m->lat_max[dir])
2084 m->lat_max[dir] = lat;
2085 if (m->lat_min[dir] > lat || !m->lat_min[dir])
2086 m->lat_min[dir] = lat;
2087
2088 m->nr_queued[dir]--;
2089 /* Push forward the busy start of monitor */
2090 m->busy_start_ts[dir] = now;
2091 }
Can Guoa45f9372021-05-24 01:36:57 -07002092 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guo1d8613a2021-04-21 19:28:39 -07002093}
2094
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302095/**
2096 * ufshcd_send_command - Send SCSI or device management commands
2097 * @hba: per adapter instance
2098 * @task_tag: Task tag of the command
2099 */
2100static inline
2101void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
2102{
Stanley Chu6edfdcf2020-07-06 14:07:07 +08002103 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag];
Bart Van Assche1f522c52021-07-21 20:34:32 -07002104 unsigned long flags;
Stanley Chu6edfdcf2020-07-06 14:07:07 +08002105
2106 lrbp->issue_time_stamp = ktime_get();
2107 lrbp->compl_time_stamp = ktime_set(0, 0);
Bean Huo28fa68f2021-01-05 12:34:42 +01002108 ufshcd_add_command_trace(hba, task_tag, UFS_CMD_SEND);
Sahitya Tummala856b3482014-09-25 15:32:34 +03002109 ufshcd_clk_scaling_start_busy(hba);
Can Guo1d8613a2021-04-21 19:28:39 -07002110 if (unlikely(ufshcd_should_inform_monitor(hba, lrbp)))
2111 ufshcd_start_monitor(hba, lrbp);
Bart Van Assche169f5eb2021-07-21 20:34:34 -07002112
2113 spin_lock_irqsave(&hba->outstanding_lock, flags);
Bart Van Asschea024ad02021-07-21 20:34:33 -07002114 if (hba->vops && hba->vops->setup_xfer_req)
2115 hba->vops->setup_xfer_req(hba, task_tag, !!lrbp->cmd);
Bart Van Assche169f5eb2021-07-21 20:34:34 -07002116 __set_bit(task_tag, &hba->outstanding_reqs);
Bart Van Assche1f522c52021-07-21 20:34:32 -07002117 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Bart Van Assche169f5eb2021-07-21 20:34:34 -07002118 spin_unlock_irqrestore(&hba->outstanding_lock, flags);
2119
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002120 /* Make sure that doorbell is committed immediately */
2121 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302122}
2123
2124/**
2125 * ufshcd_copy_sense_data - Copy sense data in case of check condition
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002126 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302127 */
2128static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
2129{
2130 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302131 if (lrbp->sense_buffer &&
2132 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07002133 int len_to_copy;
2134
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302135 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Avri Altman09a5a242018-11-22 20:04:56 +02002136 len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07002137
Avri Altman09a5a242018-11-22 20:04:56 +02002138 memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data,
2139 len_to_copy);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302140 }
2141}
2142
2143/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302144 * ufshcd_copy_query_response() - Copy the Query Response and the data
2145 * descriptor
2146 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002147 * @lrbp: pointer to local reference block
Dolev Raviv68078d52013-07-30 00:35:58 +05302148 */
2149static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002150int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05302151{
2152 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2153
Dolev Raviv68078d52013-07-30 00:35:58 +05302154 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302155
Dolev Raviv68078d52013-07-30 00:35:58 +05302156 /* Get the descriptor */
Avri Altman1c908362019-05-21 11:24:22 +03002157 if (hba->dev_cmd.query.descriptor &&
2158 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002159 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05302160 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002161 u16 resp_len;
2162 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05302163
2164 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002165 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302166 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002167 buf_len = be16_to_cpu(
2168 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002169 if (likely(buf_len >= resp_len)) {
2170 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
2171 } else {
2172 dev_warn(hba->dev,
Bean Huo3d4881d2019-11-12 23:34:35 +01002173 "%s: rsp size %d is bigger than buffer size %d",
2174 __func__, resp_len, buf_len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002175 return -EINVAL;
2176 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302177 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002178
2179 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302180}
2181
2182/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302183 * ufshcd_hba_capabilities - Read controller capabilities
2184 * @hba: per adapter instance
Satya Tangiraladf043c742020-07-06 20:04:14 +00002185 *
2186 * Return: 0 on success, negative on error.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302187 */
Satya Tangiraladf043c742020-07-06 20:04:14 +00002188static inline int ufshcd_hba_capabilities(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302189{
Satya Tangiraladf043c742020-07-06 20:04:14 +00002190 int err;
2191
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302192 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302193
2194 /* nutrs and nutmrs are 0 based values */
2195 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2196 hba->nutmrs =
2197 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
Bart Van Assche945c3cc2021-12-03 15:19:42 -08002198 hba->reserved_slot = hba->nutrs - 1;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002199
2200 /* Read crypto capabilities */
2201 err = ufshcd_hba_init_crypto_capabilities(hba);
2202 if (err)
2203 dev_err(hba->dev, "crypto setup failed\n");
2204
2205 return err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302206}
2207
2208/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302209 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2210 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302211 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302212 * Return true on success, else false
2213 */
2214static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2215{
2216 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2217 return true;
2218 else
2219 return false;
2220}
2221
2222/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302223 * ufshcd_get_upmcrs - Get the power mode change request status
2224 * @hba: Pointer to adapter instance
2225 *
2226 * This function gets the UPMCRS field of HCS register
2227 * Returns value of UPMCRS field
2228 */
2229static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2230{
2231 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2232}
2233
2234/**
Bart Van Assche35c7d872021-07-21 20:34:28 -07002235 * ufshcd_dispatch_uic_cmd - Dispatch an UIC command to the Unipro layer
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302236 * @hba: per adapter instance
2237 * @uic_cmd: UIC command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302238 */
2239static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302240ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302241{
Bart Van Assche35c7d872021-07-21 20:34:28 -07002242 lockdep_assert_held(&hba->uic_cmd_mutex);
2243
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302244 WARN_ON(hba->active_uic_cmd);
2245
2246 hba->active_uic_cmd = uic_cmd;
2247
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302248 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302249 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2250 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2251 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302252
Bean Huo28fa68f2021-01-05 12:34:42 +01002253 ufshcd_add_uic_command_trace(hba, uic_cmd, UFS_CMD_SEND);
Stanley Chuaa5c6972020-06-15 15:22:35 +08002254
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302255 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302256 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302257 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302258}
2259
2260/**
Bart Van Assche35c7d872021-07-21 20:34:28 -07002261 * ufshcd_wait_for_uic_cmd - Wait for completion of an UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302262 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002263 * @uic_cmd: UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302264 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302265 * Returns 0 only if success.
2266 */
2267static int
2268ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2269{
2270 int ret;
2271 unsigned long flags;
2272
Bart Van Assche35c7d872021-07-21 20:34:28 -07002273 lockdep_assert_held(&hba->uic_cmd_mutex);
2274
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302275 if (wait_for_completion_timeout(&uic_cmd->done,
Can Guo0f52fcb92020-11-02 22:24:40 -08002276 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302277 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
Can Guo0f52fcb92020-11-02 22:24:40 -08002278 } else {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302279 ret = -ETIMEDOUT;
Can Guo0f52fcb92020-11-02 22:24:40 -08002280 dev_err(hba->dev,
2281 "uic cmd 0x%x with arg3 0x%x completion timeout\n",
2282 uic_cmd->command, uic_cmd->argument3);
2283
2284 if (!uic_cmd->cmd_active) {
2285 dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n",
2286 __func__);
2287 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2288 }
2289 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302290
2291 spin_lock_irqsave(hba->host->host_lock, flags);
2292 hba->active_uic_cmd = NULL;
2293 spin_unlock_irqrestore(hba->host->host_lock, flags);
2294
2295 return ret;
2296}
2297
2298/**
2299 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2300 * @hba: per adapter instance
2301 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002302 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302303 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302304 * Returns 0 only if success.
2305 */
2306static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002307__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2308 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302309{
Bart Van Assche35c7d872021-07-21 20:34:28 -07002310 lockdep_assert_held(&hba->uic_cmd_mutex);
2311 lockdep_assert_held(hba->host->host_lock);
2312
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302313 if (!ufshcd_ready_for_uic_cmd(hba)) {
2314 dev_err(hba->dev,
2315 "Controller not ready to accept UIC commands\n");
2316 return -EIO;
2317 }
2318
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002319 if (completion)
2320 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302321
Can Guo0f52fcb92020-11-02 22:24:40 -08002322 uic_cmd->cmd_active = 1;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302323 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302324
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002325 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302326}
2327
2328/**
2329 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2330 * @hba: per adapter instance
2331 * @uic_cmd: UIC command
2332 *
2333 * Returns 0 only if success.
2334 */
Avri Altmane77044c52018-10-07 17:30:39 +03002335int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302336{
2337 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002338 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302339
jongmin jeonga22bcfd2021-10-18 21:42:02 +09002340 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD)
2341 return 0;
2342
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002343 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302344 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002345 ufshcd_add_delay_before_dme_cmd(hba);
2346
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002347 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002348 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002349 spin_unlock_irqrestore(hba->host->host_lock, flags);
2350 if (!ret)
2351 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2352
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302353 mutex_unlock(&hba->uic_cmd_mutex);
2354
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002355 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302356 return ret;
2357}
2358
2359/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302360 * ufshcd_map_sg - Map scatter-gather list to prdt
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002361 * @hba: per adapter instance
2362 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302363 *
2364 * Returns 0 in case of success, non-zero value in case of failure
2365 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002366static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302367{
2368 struct ufshcd_sg_entry *prd_table;
2369 struct scatterlist *sg;
2370 struct scsi_cmnd *cmd;
2371 int sg_segments;
2372 int i;
2373
2374 cmd = lrbp->cmd;
2375 sg_segments = scsi_dma_map(cmd);
2376 if (sg_segments < 0)
2377 return sg_segments;
2378
2379 if (sg_segments) {
Alim Akhtar26f968d2020-05-28 06:46:52 +05302380
2381 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2382 lrbp->utr_descriptor_ptr->prd_table_length =
2383 cpu_to_le16((sg_segments *
2384 sizeof(struct ufshcd_sg_entry)));
2385 else
2386 lrbp->utr_descriptor_ptr->prd_table_length =
Bart Van Assche3ad317a2021-10-20 14:40:22 -07002387 cpu_to_le16(sg_segments);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302388
Bart Van Assche3ad317a2021-10-20 14:40:22 -07002389 prd_table = lrbp->ucd_prdt_ptr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302390
2391 scsi_for_each_sg(cmd, sg, sg_segments, i) {
Bart Van Assche1ea7d802021-10-20 14:40:24 -07002392 const unsigned int len = sg_dma_len(sg);
2393
2394 /*
2395 * From the UFSHCI spec: "Data Byte Count (DBC): A '0'
2396 * based value that indicates the length, in bytes, of
2397 * the data block. A maximum of length of 256KB may
2398 * exist for any entry. Bits 1:0 of this field shall be
2399 * 11b to indicate Dword granularity. A value of '3'
2400 * indicates 4 bytes, '7' indicates 8 bytes, etc."
2401 */
2402 WARN_ONCE(len > 256 * 1024, "len = %#x\n", len);
2403 prd_table[i].size = cpu_to_le32(len - 1);
2404 prd_table[i].addr = cpu_to_le64(sg->dma_address);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002405 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302406 }
2407 } else {
2408 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2409 }
2410
2411 return 0;
2412}
2413
2414/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302415 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302416 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302417 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302418 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302419static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302420{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302421 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2422
Caleb Connolly51428812021-03-10 15:33:42 +00002423 if (hba->ufs_version == ufshci_version(1, 0)) {
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302424 u32 rw;
2425 rw = set & INTERRUPT_MASK_RW_VER_10;
2426 set = rw | ((set ^ intrs) & intrs);
2427 } else {
2428 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302429 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302430
2431 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2432}
2433
2434/**
2435 * ufshcd_disable_intr - disable interrupts
2436 * @hba: per adapter instance
2437 * @intrs: interrupt bits
2438 */
2439static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2440{
2441 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2442
Caleb Connolly51428812021-03-10 15:33:42 +00002443 if (hba->ufs_version == ufshci_version(1, 0)) {
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302444 u32 rw;
2445 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2446 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2447 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2448
2449 } else {
2450 set &= ~intrs;
2451 }
2452
2453 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302454}
2455
2456/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302457 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2458 * descriptor according to request
2459 * @lrbp: pointer to local reference block
2460 * @upiu_flags: flags required in the header
2461 * @cmd_dir: requests data direction
2462 */
2463static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Bean Huoa23064c2020-07-06 14:39:36 +02002464 u8 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302465{
2466 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2467 u32 data_direction;
2468 u32 dword_0;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002469 u32 dword_1 = 0;
2470 u32 dword_3 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302471
2472 if (cmd_dir == DMA_FROM_DEVICE) {
2473 data_direction = UTP_DEVICE_TO_HOST;
2474 *upiu_flags = UPIU_CMD_FLAGS_READ;
2475 } else if (cmd_dir == DMA_TO_DEVICE) {
2476 data_direction = UTP_HOST_TO_DEVICE;
2477 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2478 } else {
2479 data_direction = UTP_NO_DATA_TRANSFER;
2480 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2481 }
2482
2483 dword_0 = data_direction | (lrbp->command_type
2484 << UPIU_COMMAND_TYPE_OFFSET);
2485 if (lrbp->intr_cmd)
2486 dword_0 |= UTP_REQ_DESC_INT_CMD;
2487
Satya Tangiraladf043c742020-07-06 20:04:14 +00002488 /* Prepare crypto related dwords */
2489 ufshcd_prepare_req_desc_hdr_crypto(lrbp, &dword_0, &dword_1, &dword_3);
2490
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302491 /* Transfer request descriptor header fields */
2492 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002493 req_desc->header.dword_1 = cpu_to_le32(dword_1);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302494 /*
2495 * assigning invalid value for command status. Controller
2496 * updates OCS on command completion, with the command
2497 * status
2498 */
2499 req_desc->header.dword_2 =
2500 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002501 req_desc->header.dword_3 = cpu_to_le32(dword_3);
Yaniv Gardi51047262016-02-01 15:02:38 +02002502
2503 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302504}
2505
2506/**
2507 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2508 * for scsi commands
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002509 * @lrbp: local reference block pointer
2510 * @upiu_flags: flags
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302511 */
2512static
Bean Huoa23064c2020-07-06 14:39:36 +02002513void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302514{
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002515 struct scsi_cmnd *cmd = lrbp->cmd;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302516 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002517 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302518
2519 /* command descriptor fields */
2520 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2521 UPIU_TRANSACTION_COMMAND, upiu_flags,
2522 lrbp->lun, lrbp->task_tag);
2523 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2524 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2525
2526 /* Total EHS length and Data segment length will be zero */
2527 ucd_req_ptr->header.dword_2 = 0;
2528
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002529 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302530
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002531 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE);
Avri Altmana851b2b2018-10-07 17:30:34 +03002532 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002533 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002534
2535 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302536}
2537
Dolev Raviv68078d52013-07-30 00:35:58 +05302538/**
2539 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2540 * for query requsts
2541 * @hba: UFS hba
2542 * @lrbp: local reference block pointer
2543 * @upiu_flags: flags
2544 */
2545static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
Bean Huoa23064c2020-07-06 14:39:36 +02002546 struct ufshcd_lrb *lrbp, u8 upiu_flags)
Dolev Raviv68078d52013-07-30 00:35:58 +05302547{
2548 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2549 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302550 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302551
2552 /* Query request header */
2553 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2554 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2555 lrbp->lun, lrbp->task_tag);
2556 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2557 0, query->request.query_func, 0, 0);
2558
Zang Leigang68612852016-08-25 17:39:19 +08002559 /* Data segment length only need for WRITE_DESC */
2560 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2561 ucd_req_ptr->header.dword_2 =
2562 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2563 else
2564 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302565
2566 /* Copy the Query Request buffer as is */
2567 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2568 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302569
2570 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002571 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
Avri Altman220d17a62018-10-07 17:30:36 +03002572 memcpy(ucd_req_ptr + 1, query->descriptor, len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002573
Yaniv Gardi51047262016-02-01 15:02:38 +02002574 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302575}
2576
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302577static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2578{
2579 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2580
2581 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2582
2583 /* command descriptor fields */
2584 ucd_req_ptr->header.dword_0 =
2585 UPIU_HEADER_DWORD(
2586 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002587 /* clear rest of the fields of basic header */
2588 ucd_req_ptr->header.dword_1 = 0;
2589 ucd_req_ptr->header.dword_2 = 0;
2590
2591 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302592}
2593
2594/**
Bean Huof273c542020-08-14 11:50:33 +02002595 * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU)
Joao Pinto300bb132016-05-11 12:21:27 +01002596 * for Device Management Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002597 * @hba: per adapter instance
2598 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302599 */
Bean Huof273c542020-08-14 11:50:33 +02002600static int ufshcd_compose_devman_upiu(struct ufs_hba *hba,
2601 struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302602{
Bean Huoa23064c2020-07-06 14:39:36 +02002603 u8 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302604 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302605
Caleb Connolly51428812021-03-10 15:33:42 +00002606 if (hba->ufs_version <= ufshci_version(1, 1))
Joao Pinto300bb132016-05-11 12:21:27 +01002607 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
kehuanlin83dc7e32017-09-06 17:58:39 +08002608 else
2609 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002610
2611 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2612 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2613 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2614 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2615 ufshcd_prepare_utp_nop_upiu(lrbp);
2616 else
2617 ret = -EINVAL;
2618
2619 return ret;
2620}
2621
2622/**
2623 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2624 * for SCSI Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002625 * @hba: per adapter instance
2626 * @lrbp: pointer to local reference block
Joao Pinto300bb132016-05-11 12:21:27 +01002627 */
2628static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2629{
Bean Huoa23064c2020-07-06 14:39:36 +02002630 u8 upiu_flags;
Joao Pinto300bb132016-05-11 12:21:27 +01002631 int ret = 0;
2632
Caleb Connolly51428812021-03-10 15:33:42 +00002633 if (hba->ufs_version <= ufshci_version(1, 1))
Joao Pinto300bb132016-05-11 12:21:27 +01002634 lrbp->command_type = UTP_CMD_TYPE_SCSI;
kehuanlin83dc7e32017-09-06 17:58:39 +08002635 else
2636 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002637
2638 if (likely(lrbp->cmd)) {
2639 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2640 lrbp->cmd->sc_data_direction);
2641 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2642 } else {
2643 ret = -EINVAL;
2644 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302645
2646 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302647}
2648
2649/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002650 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002651 * @upiu_wlun_id: UPIU W-LUN id
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002652 *
2653 * Returns SCSI W-LUN id
2654 */
2655static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2656{
2657 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2658}
2659
Asutosh Dasb294ff32021-04-23 17:20:16 -07002660static inline bool is_device_wlun(struct scsi_device *sdev)
2661{
2662 return sdev->lun ==
2663 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN);
2664}
2665
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08002666/*
2667 * Associate the UFS controller queue with the default and poll HCTX types.
2668 * Initialize the mq_map[] arrays.
2669 */
2670static int ufshcd_map_queues(struct Scsi_Host *shost)
2671{
2672 int i, ret;
2673
2674 for (i = 0; i < shost->nr_maps; i++) {
2675 struct blk_mq_queue_map *map = &shost->tag_set.map[i];
2676
2677 switch (i) {
2678 case HCTX_TYPE_DEFAULT:
2679 case HCTX_TYPE_POLL:
2680 map->nr_queues = 1;
2681 break;
2682 case HCTX_TYPE_READ:
2683 map->nr_queues = 0;
2684 break;
2685 default:
2686 WARN_ON_ONCE(true);
2687 }
2688 map->queue_offset = 0;
2689 ret = blk_mq_map_queues(map);
2690 WARN_ON_ONCE(ret);
2691 }
2692
2693 return 0;
2694}
2695
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08002696static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i)
2697{
2698 struct utp_transfer_cmd_desc *cmd_descp = hba->ucdl_base_addr;
2699 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr;
2700 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr +
2701 i * sizeof(struct utp_transfer_cmd_desc);
2702 u16 response_offset = offsetof(struct utp_transfer_cmd_desc,
2703 response_upiu);
2704 u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
2705
2706 lrb->utr_descriptor_ptr = utrdlp + i;
2707 lrb->utrd_dma_addr = hba->utrdl_dma_addr +
2708 i * sizeof(struct utp_transfer_req_desc);
2709 lrb->ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i);
2710 lrb->ucd_req_dma_addr = cmd_desc_element_addr;
2711 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2712 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset;
Bart Van Assche3ad317a2021-10-20 14:40:22 -07002713 lrb->ucd_prdt_ptr = cmd_descp[i].prd_table;
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08002714 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset;
2715}
2716
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002717/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302718 * ufshcd_queuecommand - main entry point for SCSI requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002719 * @host: SCSI host pointer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302720 * @cmd: command from SCSI Midlayer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302721 *
2722 * Returns 0 for success, non-zero in case of failure
2723 */
2724static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2725{
Bart Van Assche4728ab42021-07-21 20:34:27 -07002726 struct ufs_hba *hba = shost_priv(host);
Bart Van Assche3f2c1002021-08-09 16:03:50 -07002727 int tag = scsi_cmd_to_rq(cmd)->tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302728 struct ufshcd_lrb *lrbp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302729 int err = 0;
2730
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08002731 WARN_ONCE(tag < 0 || tag >= hba->nutrs, "Invalid tag %d\n", tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302732
Bart Van Assche5675c382021-12-03 15:19:48 -08002733 /*
2734 * Allows the UFS error handler to wait for prior ufshcd_queuecommand()
2735 * calls.
2736 */
2737 rcu_read_lock();
2738
Can Guoa45f9372021-05-24 01:36:57 -07002739 switch (hba->ufshcd_state) {
2740 case UFSHCD_STATE_OPERATIONAL:
Adrian Hunterd489f18a2021-10-08 11:40:48 +03002741 break;
Can Guoa45f9372021-05-24 01:36:57 -07002742 case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL:
Adrian Hunterd489f18a2021-10-08 11:40:48 +03002743 /*
2744 * SCSI error handler can call ->queuecommand() while UFS error
2745 * handler is in progress. Error interrupts could change the
2746 * state from UFSHCD_STATE_RESET to
2747 * UFSHCD_STATE_EH_SCHEDULED_NON_FATAL. Prevent requests
2748 * being issued in that case.
2749 */
2750 if (ufshcd_eh_in_progress(hba)) {
2751 err = SCSI_MLQUEUE_HOST_BUSY;
2752 goto out;
2753 }
Can Guoa45f9372021-05-24 01:36:57 -07002754 break;
2755 case UFSHCD_STATE_EH_SCHEDULED_FATAL:
2756 /*
2757 * pm_runtime_get_sync() is used at error handling preparation
2758 * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's
2759 * PM ops, it can never be finished if we let SCSI layer keep
2760 * retrying it, which gets err handler stuck forever. Neither
2761 * can we let the scsi cmd pass through, because UFS is in bad
2762 * state, the scsi cmd may eventually time out, which will get
2763 * err handler blocked for too long. So, just fail the scsi cmd
2764 * sent from PM ops, err handler can recover PM error anyways.
2765 */
2766 if (hba->pm_op_in_progress) {
2767 hba->force_reset = true;
2768 set_host_byte(cmd, DID_BAD_TARGET);
Bart Van Assche35c37302021-10-07 13:46:01 -07002769 scsi_done(cmd);
Can Guoa45f9372021-05-24 01:36:57 -07002770 goto out;
2771 }
2772 fallthrough;
2773 case UFSHCD_STATE_RESET:
2774 err = SCSI_MLQUEUE_HOST_BUSY;
2775 goto out;
2776 case UFSHCD_STATE_ERROR:
2777 set_host_byte(cmd, DID_ERROR);
Bart Van Assche35c37302021-10-07 13:46:01 -07002778 scsi_done(cmd);
Can Guoa45f9372021-05-24 01:36:57 -07002779 goto out;
Can Guoa45f9372021-05-24 01:36:57 -07002780 }
2781
Gilad Broner7fabb772017-02-03 16:56:50 -08002782 hba->req_abort_count = 0;
2783
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002784 err = ufshcd_hold(hba, true);
2785 if (err) {
2786 err = SCSI_MLQUEUE_HOST_BUSY;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002787 goto out;
2788 }
Can Guo2dec9472020-08-09 05:15:47 -07002789 WARN_ON(ufshcd_is_clkgating_allowed(hba) &&
2790 (hba->clk_gating.state != CLKS_ON));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002791
Can Guoa45f9372021-05-24 01:36:57 -07002792 lrbp = &hba->lrb[tag];
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302793 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302794 lrbp->cmd = cmd;
Avri Altman09a5a242018-11-22 20:04:56 +02002795 lrbp->sense_bufflen = UFS_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302796 lrbp->sense_buffer = cmd->sense_buffer;
2797 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002798 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002799 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002800
Bart Van Assche3f2c1002021-08-09 16:03:50 -07002801 ufshcd_prepare_lrbp_crypto(scsi_cmd_to_rq(cmd), lrbp);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002802
Gilad Bronere0b299e2017-02-03 16:56:40 -08002803 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302804
Avri Altman09d9e4d2021-10-30 09:23:01 +03002805 ufshpb_prep(hba, lrbp);
Daejun Park2fff76f2021-07-12 17:59:36 +09002806
Joao Pinto300bb132016-05-11 12:21:27 +01002807 ufshcd_comp_scsi_upiu(hba, lrbp);
2808
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002809 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302810 if (err) {
2811 lrbp->cmd = NULL;
Can Guo17c7d352019-12-05 02:14:33 +00002812 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302813 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302814 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302815
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302816 ufshcd_send_command(hba, tag);
Bart Van Assche5675c382021-12-03 15:19:48 -08002817
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302818out:
Bart Van Assche5675c382021-12-03 15:19:48 -08002819 rcu_read_unlock();
2820
Adrian Hunter88b099002021-09-17 17:43:49 +03002821 if (ufs_trigger_eh()) {
2822 unsigned long flags;
2823
2824 spin_lock_irqsave(hba->host->host_lock, flags);
2825 ufshcd_schedule_eh_work(hba);
2826 spin_unlock_irqrestore(hba->host->host_lock, flags);
2827 }
Bart Van Asschec11a1ae2021-07-21 20:34:39 -07002828
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302829 return err;
2830}
2831
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302832static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2833 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2834{
2835 lrbp->cmd = NULL;
2836 lrbp->sense_bufflen = 0;
2837 lrbp->sense_buffer = NULL;
2838 lrbp->task_tag = tag;
2839 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302840 lrbp->intr_cmd = true; /* No interrupt aggregation */
Satya Tangiraladf043c742020-07-06 20:04:14 +00002841 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302842 hba->dev_cmd.type = cmd_type;
2843
Bean Huof273c542020-08-14 11:50:33 +02002844 return ufshcd_compose_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302845}
2846
2847static int
2848ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2849{
2850 int err = 0;
2851 unsigned long flags;
2852 u32 mask = 1 << tag;
2853
2854 /* clear outstanding transaction before retry */
2855 spin_lock_irqsave(hba->host->host_lock, flags);
2856 ufshcd_utrl_clear(hba, tag);
2857 spin_unlock_irqrestore(hba->host->host_lock, flags);
2858
2859 /*
Keoseong Park32424902021-06-04 11:40:38 +09002860 * wait for h/w to clear corresponding bit in door-bell.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302861 * max. wait is 1 sec.
2862 */
2863 err = ufshcd_wait_for_register(hba,
2864 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Bart Van Assche5cac1092020-05-07 15:27:50 -07002865 mask, ~mask, 1000, 1000);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302866
2867 return err;
2868}
2869
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002870static int
2871ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2872{
2873 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2874
2875 /* Get the UPIU response */
2876 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2877 UPIU_RSP_CODE_OFFSET;
2878 return query_res->response;
2879}
2880
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302881/**
2882 * ufshcd_dev_cmd_completion() - handles device management command responses
2883 * @hba: per adapter instance
2884 * @lrbp: pointer to local reference block
2885 */
2886static int
2887ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2888{
2889 int resp;
2890 int err = 0;
2891
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002892 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302893 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2894
2895 switch (resp) {
2896 case UPIU_TRANSACTION_NOP_IN:
2897 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2898 err = -EINVAL;
2899 dev_err(hba->dev, "%s: unexpected response %x\n",
2900 __func__, resp);
2901 }
2902 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302903 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002904 err = ufshcd_check_query_response(hba, lrbp);
2905 if (!err)
2906 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302907 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302908 case UPIU_TRANSACTION_REJECT_UPIU:
2909 /* TODO: handle Reject UPIU Response */
2910 err = -EPERM;
2911 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2912 __func__);
2913 break;
2914 default:
2915 err = -EINVAL;
2916 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2917 __func__, resp);
2918 break;
2919 }
2920
2921 return err;
2922}
2923
2924static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2925 struct ufshcd_lrb *lrbp, int max_timeout)
2926{
2927 int err = 0;
2928 unsigned long time_left;
2929 unsigned long flags;
2930
2931 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2932 msecs_to_jiffies(max_timeout));
2933
2934 spin_lock_irqsave(hba->host->host_lock, flags);
2935 hba->dev_cmd.complete = NULL;
2936 if (likely(time_left)) {
2937 err = ufshcd_get_tr_ocs(lrbp);
2938 if (!err)
2939 err = ufshcd_dev_cmd_completion(hba, lrbp);
2940 }
2941 spin_unlock_irqrestore(hba->host->host_lock, flags);
2942
2943 if (!time_left) {
2944 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002945 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2946 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302947 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002948 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302949 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002950 /*
2951 * in case of an error, after clearing the doorbell,
2952 * we also need to clear the outstanding_request
2953 * field in hba
2954 */
Bart Van Assche169f5eb2021-07-21 20:34:34 -07002955 spin_lock_irqsave(&hba->outstanding_lock, flags);
2956 __clear_bit(lrbp->task_tag, &hba->outstanding_reqs);
2957 spin_unlock_irqrestore(&hba->outstanding_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302958 }
2959
2960 return err;
2961}
2962
2963/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302964 * ufshcd_exec_dev_cmd - API for sending device management requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002965 * @hba: UFS hba
2966 * @cmd_type: specifies the type (NOP, Query...)
Bart Van Assched0b2b702021-05-13 09:49:12 -07002967 * @timeout: timeout in milliseconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302968 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302969 * NOTE: Since there is only one available tag for device management commands,
2970 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302971 */
2972static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2973 enum dev_cmd_type cmd_type, int timeout)
2974{
Bart Van Assche8a686f22021-07-21 20:34:26 -07002975 DECLARE_COMPLETION_ONSTACK(wait);
Bart Van Assche945c3cc2021-12-03 15:19:42 -08002976 const u32 tag = hba->reserved_slot;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302977 struct ufshcd_lrb *lrbp;
2978 int err;
Bart Van Assche945c3cc2021-12-03 15:19:42 -08002979
2980 /* Protects use of hba->reserved_slot. */
2981 lockdep_assert_held(&hba->dev_cmd.lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302982
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002983 down_read(&hba->clk_scaling_lock);
2984
Can Guoa45f9372021-05-24 01:36:57 -07002985 lrbp = &hba->lrb[tag];
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302986 WARN_ON(lrbp->cmd);
2987 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2988 if (unlikely(err))
Can Guoeb783bb2021-06-09 01:24:00 -07002989 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302990
2991 hba->dev_cmd.complete = &wait;
2992
Avri Altmanfb475b72021-01-10 10:46:18 +02002993 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr);
Can Guoa45f9372021-05-24 01:36:57 -07002994
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302995 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302996 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
Avri Altmanfb475b72021-01-10 10:46:18 +02002997 ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP,
2998 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03002999
Can Guoeb783bb2021-06-09 01:24:00 -07003000out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08003001 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303002 return err;
3003}
3004
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303005/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003006 * ufshcd_init_query() - init the query response and request parameters
3007 * @hba: per-adapter instance
3008 * @request: address of the request pointer to be initialized
3009 * @response: address of the response pointer to be initialized
3010 * @opcode: operation to perform
3011 * @idn: flag idn to access
3012 * @index: LU number to access
3013 * @selector: query/flag/descriptor further identification
3014 */
3015static inline void ufshcd_init_query(struct ufs_hba *hba,
3016 struct ufs_query_req **request, struct ufs_query_res **response,
3017 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
3018{
3019 *request = &hba->dev_cmd.query.request;
3020 *response = &hba->dev_cmd.query.response;
3021 memset(*request, 0, sizeof(struct ufs_query_req));
3022 memset(*response, 0, sizeof(struct ufs_query_res));
3023 (*request)->upiu_req.opcode = opcode;
3024 (*request)->upiu_req.idn = idn;
3025 (*request)->upiu_req.index = index;
3026 (*request)->upiu_req.selector = selector;
3027}
3028
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003029static int ufshcd_query_flag_retry(struct ufs_hba *hba,
Stanley Chu1f34eed2020-05-08 16:01:12 +08003030 enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003031{
3032 int ret;
3033 int retries;
3034
3035 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
Stanley Chu1f34eed2020-05-08 16:01:12 +08003036 ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res);
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003037 if (ret)
3038 dev_dbg(hba->dev,
3039 "%s: failed with error %d, retries %d\n",
3040 __func__, ret, retries);
3041 else
3042 break;
3043 }
3044
3045 if (ret)
3046 dev_err(hba->dev,
3047 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
3048 __func__, opcode, idn, ret, retries);
3049 return ret;
3050}
3051
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003052/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303053 * ufshcd_query_flag() - API function for sending flag query requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08003054 * @hba: per-adapter instance
3055 * @opcode: flag query to perform
3056 * @idn: flag idn to access
Stanley Chu1f34eed2020-05-08 16:01:12 +08003057 * @index: flag index to access
Bart Van Assche8aa29f12018-03-01 15:07:20 -08003058 * @flag_res: the flag value after the query request completes
Dolev Raviv68078d52013-07-30 00:35:58 +05303059 *
3060 * Returns 0 for success, non-zero in case of failure
3061 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003062int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Stanley Chu1f34eed2020-05-08 16:01:12 +08003063 enum flag_idn idn, u8 index, bool *flag_res)
Dolev Raviv68078d52013-07-30 00:35:58 +05303064{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003065 struct ufs_query_req *request = NULL;
3066 struct ufs_query_res *response = NULL;
Stanley Chu1f34eed2020-05-08 16:01:12 +08003067 int err, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003068 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05303069
3070 BUG_ON(!hba);
3071
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003072 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05303073 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003074 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3075 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05303076
3077 switch (opcode) {
3078 case UPIU_QUERY_OPCODE_SET_FLAG:
3079 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
3080 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
3081 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3082 break;
3083 case UPIU_QUERY_OPCODE_READ_FLAG:
3084 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3085 if (!flag_res) {
3086 /* No dummy reads */
3087 dev_err(hba->dev, "%s: Invalid argument for read request\n",
3088 __func__);
3089 err = -EINVAL;
3090 goto out_unlock;
3091 }
3092 break;
3093 default:
3094 dev_err(hba->dev,
3095 "%s: Expected query flag opcode but got = %d\n",
3096 __func__, opcode);
3097 err = -EINVAL;
3098 goto out_unlock;
3099 }
Dolev Raviv68078d52013-07-30 00:35:58 +05303100
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003101 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05303102
3103 if (err) {
3104 dev_err(hba->dev,
3105 "%s: Sending flag query for idn %d failed, err = %d\n",
3106 __func__, idn, err);
3107 goto out_unlock;
3108 }
3109
3110 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303111 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05303112 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
3113
3114out_unlock:
3115 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003116 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05303117 return err;
3118}
3119
3120/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303121 * ufshcd_query_attr - API function for sending attribute requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08003122 * @hba: per-adapter instance
3123 * @opcode: attribute opcode
3124 * @idn: attribute idn to access
3125 * @index: index field
3126 * @selector: selector field
3127 * @attr_val: the attribute value after the query request completes
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303128 *
3129 * Returns 0 for success, non-zero in case of failure
3130*/
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02003131int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
3132 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303133{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003134 struct ufs_query_req *request = NULL;
3135 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303136 int err;
3137
3138 BUG_ON(!hba);
3139
3140 if (!attr_val) {
3141 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
3142 __func__, opcode);
jintae jang8ca1a402020-12-03 14:25:32 +09003143 return -EINVAL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303144 }
3145
jintae jang8ca1a402020-12-03 14:25:32 +09003146 ufshcd_hold(hba, false);
3147
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303148 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003149 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3150 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303151
3152 switch (opcode) {
3153 case UPIU_QUERY_OPCODE_WRITE_ATTR:
3154 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303155 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303156 break;
3157 case UPIU_QUERY_OPCODE_READ_ATTR:
3158 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3159 break;
3160 default:
3161 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
3162 __func__, opcode);
3163 err = -EINVAL;
3164 goto out_unlock;
3165 }
3166
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003167 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303168
3169 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08003170 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3171 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303172 goto out_unlock;
3173 }
3174
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303175 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303176
3177out_unlock:
3178 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003179 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303180 return err;
3181}
3182
3183/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003184 * ufshcd_query_attr_retry() - API function for sending query
3185 * attribute with retries
3186 * @hba: per-adapter instance
3187 * @opcode: attribute opcode
3188 * @idn: attribute idn to access
3189 * @index: index field
3190 * @selector: selector field
3191 * @attr_val: the attribute value after the query request
3192 * completes
3193 *
3194 * Returns 0 for success, non-zero in case of failure
3195*/
Daejun Park41d8a932021-07-12 18:00:25 +09003196int ufshcd_query_attr_retry(struct ufs_hba *hba,
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003197 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
3198 u32 *attr_val)
3199{
3200 int ret = 0;
3201 u32 retries;
3202
Bart Van Assche68c9fcf2019-12-24 14:02:43 -08003203 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003204 ret = ufshcd_query_attr(hba, opcode, idn, index,
3205 selector, attr_val);
3206 if (ret)
3207 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
3208 __func__, ret, retries);
3209 else
3210 break;
3211 }
3212
3213 if (ret)
3214 dev_err(hba->dev,
3215 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
3216 __func__, idn, ret, QUERY_REQ_RETRIES);
3217 return ret;
3218}
3219
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003220static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003221 enum query_opcode opcode, enum desc_idn idn, u8 index,
3222 u8 selector, u8 *desc_buf, int *buf_len)
3223{
3224 struct ufs_query_req *request = NULL;
3225 struct ufs_query_res *response = NULL;
3226 int err;
3227
3228 BUG_ON(!hba);
3229
3230 if (!desc_buf) {
3231 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3232 __func__, opcode);
jintae jang8ca1a402020-12-03 14:25:32 +09003233 return -EINVAL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003234 }
3235
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003236 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003237 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3238 __func__, *buf_len);
jintae jang8ca1a402020-12-03 14:25:32 +09003239 return -EINVAL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003240 }
3241
jintae jang8ca1a402020-12-03 14:25:32 +09003242 ufshcd_hold(hba, false);
3243
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003244 mutex_lock(&hba->dev_cmd.lock);
3245 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3246 selector);
3247 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003248 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003249
3250 switch (opcode) {
3251 case UPIU_QUERY_OPCODE_WRITE_DESC:
3252 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3253 break;
3254 case UPIU_QUERY_OPCODE_READ_DESC:
3255 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3256 break;
3257 default:
3258 dev_err(hba->dev,
3259 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3260 __func__, opcode);
3261 err = -EINVAL;
3262 goto out_unlock;
3263 }
3264
3265 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3266
3267 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08003268 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3269 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003270 goto out_unlock;
3271 }
3272
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003273 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003274
3275out_unlock:
Bean Huocfcbae32019-11-12 23:34:36 +01003276 hba->dev_cmd.query.descriptor = NULL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003277 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003278 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003279 return err;
3280}
3281
3282/**
Bart Van Assche8aa29f12018-03-01 15:07:20 -08003283 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
3284 * @hba: per-adapter instance
3285 * @opcode: attribute opcode
3286 * @idn: attribute idn to access
3287 * @index: index field
3288 * @selector: selector field
3289 * @desc_buf: the buffer that contains the descriptor
3290 * @buf_len: length parameter passed to the device
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003291 *
3292 * Returns 0 for success, non-zero in case of failure.
3293 * The buf_len parameter will contain, on return, the length parameter
3294 * received on the response.
3295 */
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02003296int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
3297 enum query_opcode opcode,
3298 enum desc_idn idn, u8 index,
3299 u8 selector,
3300 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003301{
3302 int err;
3303 int retries;
3304
3305 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3306 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3307 selector, desc_buf, buf_len);
3308 if (!err || err == -EINVAL)
3309 break;
3310 }
3311
3312 return err;
3313}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003314
3315/**
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003316 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
3317 * @hba: Pointer to adapter instance
3318 * @desc_id: descriptor idn value
3319 * @desc_len: mapped desc length (out)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003320 */
Bean Huo7a0bf852020-06-03 11:19:58 +02003321void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
3322 int *desc_len)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003323{
Bean Huo7a0bf852020-06-03 11:19:58 +02003324 if (desc_id >= QUERY_DESC_IDN_MAX || desc_id == QUERY_DESC_IDN_RFU_0 ||
3325 desc_id == QUERY_DESC_IDN_RFU_1)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003326 *desc_len = 0;
Bean Huo7a0bf852020-06-03 11:19:58 +02003327 else
3328 *desc_len = hba->desc_size[desc_id];
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003329}
3330EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
3331
Bean Huo7a0bf852020-06-03 11:19:58 +02003332static void ufshcd_update_desc_length(struct ufs_hba *hba,
Bean Huo72fb6902020-06-03 11:19:59 +02003333 enum desc_idn desc_id, int desc_index,
Bean Huo7a0bf852020-06-03 11:19:58 +02003334 unsigned char desc_len)
3335{
3336 if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE &&
Bean Huo72fb6902020-06-03 11:19:59 +02003337 desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT)
3338 /* For UFS 3.1, the normal unit descriptor is 10 bytes larger
3339 * than the RPMB unit, however, both descriptors share the same
3340 * desc_idn, to cover both unit descriptors with one length, we
3341 * choose the normal unit descriptor length by desc_index.
3342 */
Bean Huo7a0bf852020-06-03 11:19:58 +02003343 hba->desc_size[desc_id] = desc_len;
3344}
3345
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003346/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003347 * ufshcd_read_desc_param - read the specified descriptor parameter
3348 * @hba: Pointer to adapter instance
3349 * @desc_id: descriptor idn value
3350 * @desc_index: descriptor index
3351 * @param_offset: offset of the parameter to read
3352 * @param_read_buf: pointer to buffer where parameter would be read
3353 * @param_size: sizeof(param_read_buf)
3354 *
3355 * Return 0 in case of success, non-zero otherwise
3356 */
Stanislav Nijnikov45bced82018-02-15 14:14:02 +02003357int ufshcd_read_desc_param(struct ufs_hba *hba,
3358 enum desc_idn desc_id,
3359 int desc_index,
3360 u8 param_offset,
3361 u8 *param_read_buf,
3362 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003363{
3364 int ret;
3365 u8 *desc_buf;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003366 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003367 bool is_kmalloc = true;
3368
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003369 /* Safety check */
3370 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003371 return -EINVAL;
3372
Bean Huo7a0bf852020-06-03 11:19:58 +02003373 /* Get the length of descriptor */
3374 ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
3375 if (!buff_len) {
Can Guo1699f982020-10-21 22:59:00 -07003376 dev_err(hba->dev, "%s: Failed to get desc length\n", __func__);
3377 return -EINVAL;
3378 }
3379
3380 if (param_offset >= buff_len) {
3381 dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n",
3382 __func__, param_offset, desc_id, buff_len);
Bean Huo7a0bf852020-06-03 11:19:58 +02003383 return -EINVAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003384 }
3385
3386 /* Check whether we need temp memory */
3387 if (param_offset != 0 || param_size < buff_len) {
Can Guo1699f982020-10-21 22:59:00 -07003388 desc_buf = kzalloc(buff_len, GFP_KERNEL);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003389 if (!desc_buf)
3390 return -ENOMEM;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003391 } else {
3392 desc_buf = param_read_buf;
3393 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003394 }
3395
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003396 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003397 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003398 desc_id, desc_index, 0,
3399 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003400
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003401 if (ret) {
Can Guo1699f982020-10-21 22:59:00 -07003402 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n",
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003403 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003404 goto out;
3405 }
3406
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003407 /* Sanity check */
3408 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
Can Guo1699f982020-10-21 22:59:00 -07003409 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n",
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003410 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3411 ret = -EINVAL;
3412 goto out;
3413 }
3414
Bean Huo7a0bf852020-06-03 11:19:58 +02003415 /* Update descriptor length */
3416 buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET];
Bean Huo72fb6902020-06-03 11:19:59 +02003417 ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len);
Bean Huo7a0bf852020-06-03 11:19:58 +02003418
Can Guo1699f982020-10-21 22:59:00 -07003419 if (is_kmalloc) {
3420 /* Make sure we don't copy more data than available */
Bart Van Assched3d9c452021-07-21 20:34:22 -07003421 if (param_offset >= buff_len)
3422 ret = -EINVAL;
3423 else
3424 memcpy(param_read_buf, &desc_buf[param_offset],
3425 min_t(u32, param_size, buff_len - param_offset));
Can Guo1699f982020-10-21 22:59:00 -07003426 }
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003427out:
3428 if (is_kmalloc)
3429 kfree(desc_buf);
3430 return ret;
3431}
3432
Yaniv Gardib573d482016-03-10 17:37:09 +02003433/**
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003434 * struct uc_string_id - unicode string
3435 *
3436 * @len: size of this descriptor inclusive
3437 * @type: descriptor type
3438 * @uc: unicode string character
3439 */
3440struct uc_string_id {
3441 u8 len;
3442 u8 type;
Gustavo A. R. Silvaec38c0a2020-05-07 14:25:50 -05003443 wchar_t uc[];
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003444} __packed;
3445
3446/* replace non-printable or non-ASCII characters with spaces */
3447static inline char ufshcd_remove_non_printable(u8 ch)
3448{
3449 return (ch >= 0x20 && ch <= 0x7e) ? ch : ' ';
3450}
3451
3452/**
Yaniv Gardib573d482016-03-10 17:37:09 +02003453 * ufshcd_read_string_desc - read string descriptor
3454 * @hba: pointer to adapter instance
3455 * @desc_index: descriptor index
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003456 * @buf: pointer to buffer where descriptor would be read,
3457 * the caller should free the memory.
Yaniv Gardib573d482016-03-10 17:37:09 +02003458 * @ascii: if true convert from unicode to ascii characters
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003459 * null terminated string.
Yaniv Gardib573d482016-03-10 17:37:09 +02003460 *
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003461 * Return:
3462 * * string size on success.
3463 * * -ENOMEM: on allocation failure
3464 * * -EINVAL: on a wrong parameter
Yaniv Gardib573d482016-03-10 17:37:09 +02003465 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003466int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
3467 u8 **buf, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003468{
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003469 struct uc_string_id *uc_str;
3470 u8 *str;
3471 int ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003472
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003473 if (!buf)
3474 return -EINVAL;
Yaniv Gardib573d482016-03-10 17:37:09 +02003475
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003476 uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
3477 if (!uc_str)
3478 return -ENOMEM;
3479
Bean Huoc4607a02020-06-03 11:19:56 +02003480 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0,
3481 (u8 *)uc_str, QUERY_DESC_MAX_SIZE);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003482 if (ret < 0) {
3483 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
3484 QUERY_REQ_RETRIES, ret);
3485 str = NULL;
3486 goto out;
3487 }
3488
3489 if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
3490 dev_dbg(hba->dev, "String Desc is of zero length\n");
3491 str = NULL;
3492 ret = 0;
Yaniv Gardib573d482016-03-10 17:37:09 +02003493 goto out;
3494 }
3495
3496 if (ascii) {
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003497 ssize_t ascii_len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003498 int i;
Yaniv Gardib573d482016-03-10 17:37:09 +02003499 /* remove header and divide by 2 to move from UTF16 to UTF8 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003500 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3501 str = kzalloc(ascii_len, GFP_KERNEL);
3502 if (!str) {
3503 ret = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003504 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003505 }
3506
3507 /*
3508 * the descriptor contains string in UTF16 format
3509 * we need to convert to utf-8 so it can be displayed
3510 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003511 ret = utf16s_to_utf8s(uc_str->uc,
3512 uc_str->len - QUERY_DESC_HDR_SIZE,
3513 UTF16_BIG_ENDIAN, str, ascii_len);
Yaniv Gardib573d482016-03-10 17:37:09 +02003514
3515 /* replace non-printable or non-ASCII characters with spaces */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003516 for (i = 0; i < ret; i++)
3517 str[i] = ufshcd_remove_non_printable(str[i]);
Yaniv Gardib573d482016-03-10 17:37:09 +02003518
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003519 str[ret++] = '\0';
3520
3521 } else {
YueHaibing5f577042019-08-31 12:44:24 +00003522 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003523 if (!str) {
3524 ret = -ENOMEM;
3525 goto out;
3526 }
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003527 ret = uc_str->len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003528 }
3529out:
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003530 *buf = str;
3531 kfree(uc_str);
3532 return ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003533}
Yaniv Gardib573d482016-03-10 17:37:09 +02003534
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003535/**
3536 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3537 * @hba: Pointer to adapter instance
3538 * @lun: lun id
3539 * @param_offset: offset of the parameter to read
3540 * @param_read_buf: pointer to buffer where parameter would be read
3541 * @param_size: sizeof(param_read_buf)
3542 *
3543 * Return 0 in case of success, non-zero otherwise
3544 */
3545static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3546 int lun,
3547 enum unit_desc_param param_offset,
3548 u8 *param_read_buf,
3549 u32 param_size)
3550{
3551 /*
3552 * Unit descriptors are only available for general purpose LUs (LUN id
3553 * from 0 to 7) and RPMB Well known LU.
3554 */
Jaegeuk Kima2fca522021-01-11 01:59:27 -08003555 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun, param_offset))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003556 return -EOPNOTSUPP;
3557
3558 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3559 param_offset, param_read_buf, param_size);
3560}
3561
Can Guo09f17792020-02-10 19:40:49 -08003562static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba)
3563{
3564 int err = 0;
3565 u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3566
3567 if (hba->dev_info.wspecversion >= 0x300) {
3568 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3569 QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0,
3570 &gating_wait);
3571 if (err)
3572 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n",
3573 err, gating_wait);
3574
3575 if (gating_wait == 0) {
3576 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3577 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n",
3578 gating_wait);
3579 }
3580
3581 hba->dev_info.clk_gating_wait_us = gating_wait;
3582 }
3583
3584 return err;
3585}
3586
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003587/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303588 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3589 * @hba: per adapter instance
3590 *
3591 * 1. Allocate DMA memory for Command Descriptor array
3592 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3593 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3594 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3595 * (UTMRDL)
3596 * 4. Allocate memory for local reference block(lrb).
3597 *
3598 * Returns 0 for success, non-zero in case of failure
3599 */
3600static int ufshcd_memory_alloc(struct ufs_hba *hba)
3601{
3602 size_t utmrdl_size, utrdl_size, ucdl_size;
3603
3604 /* Allocate memory for UTP command descriptors */
3605 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003606 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3607 ucdl_size,
3608 &hba->ucdl_dma_addr,
3609 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303610
3611 /*
3612 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3613 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3614 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3615 * be aligned to 128 bytes as well
3616 */
3617 if (!hba->ucdl_base_addr ||
3618 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303619 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303620 "Command Descriptor Memory allocation failed\n");
3621 goto out;
3622 }
3623
3624 /*
3625 * Allocate memory for UTP Transfer descriptors
3626 * UFSHCI requires 1024 byte alignment of UTRD
3627 */
3628 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003629 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3630 utrdl_size,
3631 &hba->utrdl_dma_addr,
3632 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303633 if (!hba->utrdl_base_addr ||
3634 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303635 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303636 "Transfer Descriptor Memory allocation failed\n");
3637 goto out;
3638 }
3639
3640 /*
3641 * Allocate memory for UTP Task Management descriptors
3642 * UFSHCI requires 1024 byte alignment of UTMRD
3643 */
3644 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003645 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3646 utmrdl_size,
3647 &hba->utmrdl_dma_addr,
3648 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303649 if (!hba->utmrdl_base_addr ||
3650 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303651 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303652 "Task Management Descriptor Memory allocation failed\n");
3653 goto out;
3654 }
3655
3656 /* Allocate memory for local reference block */
Kees Cooka86854d2018-06-12 14:07:58 -07003657 hba->lrb = devm_kcalloc(hba->dev,
3658 hba->nutrs, sizeof(struct ufshcd_lrb),
Seungwon Jeon2953f852013-06-27 13:31:54 +09003659 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303660 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303661 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303662 goto out;
3663 }
3664 return 0;
3665out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303666 return -ENOMEM;
3667}
3668
3669/**
3670 * ufshcd_host_memory_configure - configure local reference block with
3671 * memory offsets
3672 * @hba: per adapter instance
3673 *
3674 * Configure Host memory space
3675 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3676 * address.
3677 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3678 * and PRDT offset.
3679 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3680 * into local reference block.
3681 */
3682static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3683{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303684 struct utp_transfer_req_desc *utrdlp;
3685 dma_addr_t cmd_desc_dma_addr;
3686 dma_addr_t cmd_desc_element_addr;
3687 u16 response_offset;
3688 u16 prdt_offset;
3689 int cmd_desc_size;
3690 int i;
3691
3692 utrdlp = hba->utrdl_base_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303693
3694 response_offset =
3695 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3696 prdt_offset =
3697 offsetof(struct utp_transfer_cmd_desc, prd_table);
3698
3699 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3700 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3701
3702 for (i = 0; i < hba->nutrs; i++) {
3703 /* Configure UTRD with command descriptor base address */
3704 cmd_desc_element_addr =
3705 (cmd_desc_dma_addr + (cmd_desc_size * i));
3706 utrdlp[i].command_desc_base_addr_lo =
3707 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3708 utrdlp[i].command_desc_base_addr_hi =
3709 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3710
3711 /* Response upiu and prdt offset should be in double words */
Alim Akhtar26f968d2020-05-28 06:46:52 +05303712 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3713 utrdlp[i].response_upiu_offset =
3714 cpu_to_le16(response_offset);
3715 utrdlp[i].prd_table_offset =
3716 cpu_to_le16(prdt_offset);
3717 utrdlp[i].response_upiu_length =
3718 cpu_to_le16(ALIGNED_UPIU_SIZE);
3719 } else {
3720 utrdlp[i].response_upiu_offset =
3721 cpu_to_le16(response_offset >> 2);
3722 utrdlp[i].prd_table_offset =
3723 cpu_to_le16(prdt_offset >> 2);
3724 utrdlp[i].response_upiu_length =
3725 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
3726 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303727
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08003728 ufshcd_init_lrb(hba, &hba->lrb[i], i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303729 }
3730}
3731
3732/**
3733 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3734 * @hba: per adapter instance
3735 *
3736 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3737 * in order to initialize the Unipro link startup procedure.
3738 * Once the Unipro links are up, the device connected to the controller
3739 * is detected.
3740 *
3741 * Returns 0 on success, non-zero value on failure
3742 */
3743static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3744{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303745 struct uic_command uic_cmd = {0};
3746 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303747
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303748 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3749
3750 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3751 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003752 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303753 "dme-link-startup: error code %d\n", ret);
3754 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303755}
Alim Akhtar39bf2d82020-05-28 06:46:51 +05303756/**
3757 * ufshcd_dme_reset - UIC command for DME_RESET
3758 * @hba: per adapter instance
3759 *
3760 * DME_RESET command is issued in order to reset UniPro stack.
3761 * This function now deals with cold reset.
3762 *
3763 * Returns 0 on success, non-zero value on failure
3764 */
3765static int ufshcd_dme_reset(struct ufs_hba *hba)
3766{
3767 struct uic_command uic_cmd = {0};
3768 int ret;
3769
3770 uic_cmd.command = UIC_CMD_DME_RESET;
3771
3772 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3773 if (ret)
3774 dev_err(hba->dev,
3775 "dme-reset: error code %d\n", ret);
3776
3777 return ret;
3778}
3779
Stanley Chufc85a742020-11-16 14:50:52 +08003780int ufshcd_dme_configure_adapt(struct ufs_hba *hba,
3781 int agreed_gear,
3782 int adapt_val)
3783{
3784 int ret;
3785
3786 if (agreed_gear != UFS_HS_G4)
Bjorn Andersson66df79c2020-11-20 20:48:10 -08003787 adapt_val = PA_NO_ADAPT;
Stanley Chufc85a742020-11-16 14:50:52 +08003788
3789 ret = ufshcd_dme_set(hba,
3790 UIC_ARG_MIB(PA_TXHSADAPTTYPE),
3791 adapt_val);
3792 return ret;
3793}
3794EXPORT_SYMBOL_GPL(ufshcd_dme_configure_adapt);
3795
Alim Akhtar39bf2d82020-05-28 06:46:51 +05303796/**
3797 * ufshcd_dme_enable - UIC command for DME_ENABLE
3798 * @hba: per adapter instance
3799 *
3800 * DME_ENABLE command is issued in order to enable UniPro stack.
3801 *
3802 * Returns 0 on success, non-zero value on failure
3803 */
3804static int ufshcd_dme_enable(struct ufs_hba *hba)
3805{
3806 struct uic_command uic_cmd = {0};
3807 int ret;
3808
3809 uic_cmd.command = UIC_CMD_DME_ENABLE;
3810
3811 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3812 if (ret)
3813 dev_err(hba->dev,
Bean Huo1fa05702020-12-07 20:01:37 +01003814 "dme-enable: error code %d\n", ret);
Alim Akhtar39bf2d82020-05-28 06:46:51 +05303815
3816 return ret;
3817}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303818
Yaniv Gardicad2e032015-03-31 17:37:14 +03003819static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3820{
3821 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3822 unsigned long min_sleep_time_us;
3823
3824 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3825 return;
3826
3827 /*
3828 * last_dme_cmd_tstamp will be 0 only for 1st call to
3829 * this function
3830 */
3831 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3832 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3833 } else {
3834 unsigned long delta =
3835 (unsigned long) ktime_to_us(
3836 ktime_sub(ktime_get(),
3837 hba->last_dme_cmd_tstamp));
3838
3839 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3840 min_sleep_time_us =
3841 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3842 else
3843 return; /* no more delay required */
3844 }
3845
3846 /* allow sleep for extra 50us if needed */
3847 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3848}
3849
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303850/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303851 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3852 * @hba: per adapter instance
3853 * @attr_sel: uic command argument1
3854 * @attr_set: attribute set type as uic command argument2
3855 * @mib_val: setting value as uic command argument3
3856 * @peer: indicate whether peer or local
3857 *
3858 * Returns 0 on success, non-zero value on failure
3859 */
3860int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3861 u8 attr_set, u32 mib_val, u8 peer)
3862{
3863 struct uic_command uic_cmd = {0};
3864 static const char *const action[] = {
3865 "dme-set",
3866 "dme-peer-set"
3867 };
3868 const char *set = action[!!peer];
3869 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003870 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303871
3872 uic_cmd.command = peer ?
3873 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3874 uic_cmd.argument1 = attr_sel;
3875 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3876 uic_cmd.argument3 = mib_val;
3877
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003878 do {
3879 /* for peer attributes we retry upon failure */
3880 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3881 if (ret)
3882 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3883 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3884 } while (ret && peer && --retries);
3885
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003886 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003887 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003888 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3889 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303890
3891 return ret;
3892}
3893EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3894
3895/**
3896 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3897 * @hba: per adapter instance
3898 * @attr_sel: uic command argument1
3899 * @mib_val: the value of the attribute as returned by the UIC command
3900 * @peer: indicate whether peer or local
3901 *
3902 * Returns 0 on success, non-zero value on failure
3903 */
3904int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3905 u32 *mib_val, u8 peer)
3906{
3907 struct uic_command uic_cmd = {0};
3908 static const char *const action[] = {
3909 "dme-get",
3910 "dme-peer-get"
3911 };
3912 const char *get = action[!!peer];
3913 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003914 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003915 struct ufs_pa_layer_attr orig_pwr_info;
3916 struct ufs_pa_layer_attr temp_pwr_info;
3917 bool pwr_mode_change = false;
3918
3919 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3920 orig_pwr_info = hba->pwr_info;
3921 temp_pwr_info = orig_pwr_info;
3922
3923 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3924 orig_pwr_info.pwr_rx == FAST_MODE) {
3925 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3926 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3927 pwr_mode_change = true;
3928 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3929 orig_pwr_info.pwr_rx == SLOW_MODE) {
3930 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3931 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3932 pwr_mode_change = true;
3933 }
3934 if (pwr_mode_change) {
3935 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3936 if (ret)
3937 goto out;
3938 }
3939 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303940
3941 uic_cmd.command = peer ?
3942 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3943 uic_cmd.argument1 = attr_sel;
3944
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003945 do {
3946 /* for peer attributes we retry upon failure */
3947 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3948 if (ret)
3949 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3950 get, UIC_GET_ATTR_ID(attr_sel), ret);
3951 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303952
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003953 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003954 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003955 get, UIC_GET_ATTR_ID(attr_sel),
3956 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003957
3958 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303959 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003960
3961 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3962 && pwr_mode_change)
3963 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303964out:
3965 return ret;
3966}
3967EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3968
3969/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003970 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3971 * state) and waits for it to take effect.
3972 *
3973 * @hba: per adapter instance
3974 * @cmd: UIC command to execute
3975 *
3976 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3977 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3978 * and device UniPro link and hence it's final completion would be indicated by
3979 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3980 * addition to normal UIC command completion Status (UCCS). This function only
3981 * returns after the relevant status bits indicate the completion.
3982 *
3983 * Returns 0 on success, non-zero value on failure
3984 */
3985static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3986{
Bart Van Assche8a686f22021-07-21 20:34:26 -07003987 DECLARE_COMPLETION_ONSTACK(uic_async_done);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003988 unsigned long flags;
3989 u8 status;
3990 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003991 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003992
3993 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003994 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003995
3996 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo4db7a232020-08-09 05:15:51 -07003997 if (ufshcd_is_link_broken(hba)) {
3998 ret = -ENOLINK;
3999 goto out_unlock;
4000 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004001 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004002 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
4003 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
4004 /*
4005 * Make sure UIC command completion interrupt is disabled before
4006 * issuing UIC command.
4007 */
4008 wmb();
4009 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004010 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004011 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
4012 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004013 if (ret) {
4014 dev_err(hba->dev,
4015 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
4016 cmd->command, cmd->argument3, ret);
4017 goto out;
4018 }
4019
4020 if (!wait_for_completion_timeout(hba->uic_async_done,
4021 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
4022 dev_err(hba->dev,
4023 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
4024 cmd->command, cmd->argument3);
Can Guo0f52fcb92020-11-02 22:24:40 -08004025
4026 if (!cmd->cmd_active) {
4027 dev_err(hba->dev, "%s: Power Mode Change operation has been completed, go check UPMCRS\n",
4028 __func__);
4029 goto check_upmcrs;
4030 }
4031
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004032 ret = -ETIMEDOUT;
4033 goto out;
4034 }
4035
Can Guo0f52fcb92020-11-02 22:24:40 -08004036check_upmcrs:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004037 status = ufshcd_get_upmcrs(hba);
4038 if (status != PWR_LOCAL) {
4039 dev_err(hba->dev,
Zang Leigang479da362017-09-19 16:50:30 +08004040 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004041 cmd->command, status);
4042 ret = (status != PWR_OK) ? status : -1;
4043 }
4044out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004045 if (ret) {
4046 ufshcd_print_host_state(hba);
4047 ufshcd_print_pwr_info(hba);
Stanley Chue965e5e2020-12-05 19:58:59 +08004048 ufshcd_print_evt_hist(hba);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004049 }
4050
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004051 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004052 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004053 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004054 if (reenable_intr)
4055 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Can Guo4db7a232020-08-09 05:15:51 -07004056 if (ret) {
4057 ufshcd_set_link_broken(hba);
Adrian Hunter88b099002021-09-17 17:43:49 +03004058 ufshcd_schedule_eh_work(hba);
Can Guo4db7a232020-08-09 05:15:51 -07004059 }
4060out_unlock:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004061 spin_unlock_irqrestore(hba->host->host_lock, flags);
4062 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004063
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004064 return ret;
4065}
4066
4067/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304068 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
4069 * using DME_SET primitives.
4070 * @hba: per adapter instance
4071 * @mode: powr mode value
4072 *
4073 * Returns 0 on success, non-zero value on failure
4074 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05304075static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304076{
4077 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004078 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304079
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03004080 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
4081 ret = ufshcd_dme_set(hba,
4082 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
4083 if (ret) {
4084 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
4085 __func__, ret);
4086 goto out;
4087 }
4088 }
4089
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304090 uic_cmd.command = UIC_CMD_DME_SET;
4091 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
4092 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004093 ufshcd_hold(hba, false);
4094 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
4095 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304096
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03004097out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004098 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004099}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304100
Stanley Chu087c5ef2020-03-27 17:53:28 +08004101int ufshcd_link_recovery(struct ufs_hba *hba)
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004102{
4103 int ret;
4104 unsigned long flags;
4105
4106 spin_lock_irqsave(hba->host->host_lock, flags);
4107 hba->ufshcd_state = UFSHCD_STATE_RESET;
4108 ufshcd_set_eh_in_progress(hba);
4109 spin_unlock_irqrestore(hba->host->host_lock, flags);
4110
Can Guoebdd1df2019-11-14 22:09:24 -08004111 /* Reset the attached device */
Stanley Chu31a5d9c2020-12-08 21:56:35 +08004112 ufshcd_device_reset(hba);
Can Guoebdd1df2019-11-14 22:09:24 -08004113
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004114 ret = ufshcd_host_reset_and_restore(hba);
4115
4116 spin_lock_irqsave(hba->host->host_lock, flags);
4117 if (ret)
4118 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4119 ufshcd_clear_eh_in_progress(hba);
4120 spin_unlock_irqrestore(hba->host->host_lock, flags);
4121
4122 if (ret)
4123 dev_err(hba->dev, "%s: link recovery failed, err %d",
4124 __func__, ret);
4125
4126 return ret;
4127}
Stanley Chu087c5ef2020-03-27 17:53:28 +08004128EXPORT_SYMBOL_GPL(ufshcd_link_recovery);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004129
Asutosh Das525943a2021-09-28 02:06:12 -07004130int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004131{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004132 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004133 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08004134 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004135
Kiwoong Kimee32c902016-11-10 21:17:43 +09004136 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
4137
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004138 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004139 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08004140 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
4141 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004142
Can Guo4db7a232020-08-09 05:15:51 -07004143 if (ret)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004144 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
4145 __func__, ret);
Can Guo4db7a232020-08-09 05:15:51 -07004146 else
Kiwoong Kimee32c902016-11-10 21:17:43 +09004147 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
4148 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004149
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004150 return ret;
4151}
Asutosh Das525943a2021-09-28 02:06:12 -07004152EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_enter);
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004153
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004154int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004155{
4156 struct uic_command uic_cmd = {0};
4157 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08004158 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004159
Kiwoong Kimee32c902016-11-10 21:17:43 +09004160 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
4161
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004162 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
4163 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08004164 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
4165 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
4166
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304167 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004168 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
4169 __func__, ret);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004170 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09004171 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
4172 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004173 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
4174 hba->ufs_stats.hibern8_exit_cnt++;
4175 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304176
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304177 return ret;
4178}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004179EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304180
Stanley Chuba7af5e2019-12-30 13:32:28 +08004181void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit)
4182{
4183 unsigned long flags;
Can Guobe7594a2020-03-05 00:53:07 -08004184 bool update = false;
Stanley Chuba7af5e2019-12-30 13:32:28 +08004185
Can Guobe7594a2020-03-05 00:53:07 -08004186 if (!ufshcd_is_auto_hibern8_supported(hba))
Stanley Chuba7af5e2019-12-30 13:32:28 +08004187 return;
4188
4189 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08004190 if (hba->ahit != ahit) {
4191 hba->ahit = ahit;
4192 update = true;
4193 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08004194 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08004195
Asutosh Dasb294ff32021-04-23 17:20:16 -07004196 if (update &&
4197 !pm_runtime_suspended(&hba->sdev_ufs_device->sdev_gendev)) {
4198 ufshcd_rpm_get_sync(hba);
Can Guobe7594a2020-03-05 00:53:07 -08004199 ufshcd_hold(hba, false);
4200 ufshcd_auto_hibern8_enable(hba);
4201 ufshcd_release(hba);
Asutosh Dasb294ff32021-04-23 17:20:16 -07004202 ufshcd_rpm_put_sync(hba);
Can Guobe7594a2020-03-05 00:53:07 -08004203 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08004204}
4205EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
4206
Can Guo71d848b2019-11-14 22:09:26 -08004207void ufshcd_auto_hibern8_enable(struct ufs_hba *hba)
Adrian Hunterad448372018-03-20 15:07:38 +02004208{
4209 unsigned long flags;
4210
Bao D. Nguyen499f7a92020-08-28 18:05:13 -07004211 if (!ufshcd_is_auto_hibern8_supported(hba))
Adrian Hunterad448372018-03-20 15:07:38 +02004212 return;
4213
4214 spin_lock_irqsave(hba->host->host_lock, flags);
4215 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
4216 spin_unlock_irqrestore(hba->host->host_lock, flags);
4217}
4218
Yaniv Gardi50646362014-10-23 13:25:13 +03004219 /**
4220 * ufshcd_init_pwr_info - setting the POR (power on reset)
4221 * values in hba power info
4222 * @hba: per-adapter instance
4223 */
4224static void ufshcd_init_pwr_info(struct ufs_hba *hba)
4225{
4226 hba->pwr_info.gear_rx = UFS_PWM_G1;
4227 hba->pwr_info.gear_tx = UFS_PWM_G1;
4228 hba->pwr_info.lane_rx = 1;
4229 hba->pwr_info.lane_tx = 1;
4230 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
4231 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
4232 hba->pwr_info.hs_rate = 0;
4233}
4234
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304235/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004236 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4237 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304238 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004239static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304240{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004241 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4242
4243 if (hba->max_pwr_info.is_valid)
4244 return 0;
4245
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004246 pwr_info->pwr_tx = FAST_MODE;
4247 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004248 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304249
4250 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004251 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4252 &pwr_info->lane_rx);
4253 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4254 &pwr_info->lane_tx);
4255
4256 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4257 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4258 __func__,
4259 pwr_info->lane_rx,
4260 pwr_info->lane_tx);
4261 return -EINVAL;
4262 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304263
4264 /*
4265 * First, get the maximum gears of HS speed.
4266 * If a zero value, it means there is no HSGEAR capability.
4267 * Then, get the maximum gears of PWM speed.
4268 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004269 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4270 if (!pwr_info->gear_rx) {
4271 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4272 &pwr_info->gear_rx);
4273 if (!pwr_info->gear_rx) {
4274 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4275 __func__, pwr_info->gear_rx);
4276 return -EINVAL;
4277 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004278 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304279 }
4280
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004281 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4282 &pwr_info->gear_tx);
4283 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304284 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004285 &pwr_info->gear_tx);
4286 if (!pwr_info->gear_tx) {
4287 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4288 __func__, pwr_info->gear_tx);
4289 return -EINVAL;
4290 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004291 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004292 }
4293
4294 hba->max_pwr_info.is_valid = true;
4295 return 0;
4296}
4297
4298static int ufshcd_change_power_mode(struct ufs_hba *hba,
4299 struct ufs_pa_layer_attr *pwr_mode)
4300{
4301 int ret;
4302
4303 /* if already configured to the requested pwr_mode */
Can Guo2355b662020-08-24 19:07:06 -07004304 if (!hba->force_pmc &&
4305 pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004306 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4307 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4308 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4309 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4310 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4311 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4312 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4313 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304314 }
4315
4316 /*
4317 * Configure attributes for power mode change with below.
4318 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4319 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4320 * - PA_HSSERIES
4321 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004322 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4323 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4324 pwr_mode->lane_rx);
4325 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4326 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304327 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004328 else
4329 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304330
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004331 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4332 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4333 pwr_mode->lane_tx);
4334 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4335 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304336 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004337 else
4338 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304339
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004340 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4341 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4342 pwr_mode->pwr_rx == FAST_MODE ||
4343 pwr_mode->pwr_tx == FAST_MODE)
4344 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4345 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304346
Kiwoong Kimb1d0d2e2020-12-21 10:24:40 +09004347 if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) {
4348 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4349 DL_FC0ProtectionTimeOutVal_Default);
4350 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4351 DL_TC0ReplayTimeOutVal_Default);
4352 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4353 DL_AFC0ReqTimeOutVal_Default);
4354 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
4355 DL_FC1ProtectionTimeOutVal_Default);
4356 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
4357 DL_TC1ReplayTimeOutVal_Default);
4358 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
4359 DL_AFC1ReqTimeOutVal_Default);
Can Guo08342532019-12-05 02:14:42 +00004360
Kiwoong Kimb1d0d2e2020-12-21 10:24:40 +09004361 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4362 DL_FC0ProtectionTimeOutVal_Default);
4363 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4364 DL_TC0ReplayTimeOutVal_Default);
4365 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4366 DL_AFC0ReqTimeOutVal_Default);
4367 }
Can Guo08342532019-12-05 02:14:42 +00004368
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004369 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4370 | pwr_mode->pwr_tx);
4371
4372 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304373 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004374 "%s: power mode change failed %d\n", __func__, ret);
4375 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004376 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4377 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004378
4379 memcpy(&hba->pwr_info, pwr_mode,
4380 sizeof(struct ufs_pa_layer_attr));
4381 }
4382
4383 return ret;
4384}
4385
4386/**
4387 * ufshcd_config_pwr_mode - configure a new power mode
4388 * @hba: per-adapter instance
4389 * @desired_pwr_mode: desired power configuration
4390 */
Alim Akhtar0d846e72018-05-06 15:44:18 +05304391int ufshcd_config_pwr_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004392 struct ufs_pa_layer_attr *desired_pwr_mode)
4393{
4394 struct ufs_pa_layer_attr final_params = { 0 };
4395 int ret;
4396
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004397 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4398 desired_pwr_mode, &final_params);
4399
4400 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004401 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4402
4403 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304404
4405 return ret;
4406}
Alim Akhtar0d846e72018-05-06 15:44:18 +05304407EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304408
4409/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304410 * ufshcd_complete_dev_init() - checks device readiness
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004411 * @hba: per-adapter instance
Dolev Raviv68078d52013-07-30 00:35:58 +05304412 *
4413 * Set fDeviceInit flag and poll until device toggles it.
4414 */
4415static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4416{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004417 int err;
Jason Yan7dfdcc32020-04-26 17:43:05 +08004418 bool flag_res = true;
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004419 ktime_t timeout;
Dolev Raviv68078d52013-07-30 00:35:58 +05304420
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004421 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08004422 QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304423 if (err) {
4424 dev_err(hba->dev,
4425 "%s setting fDeviceInit flag failed with error %d\n",
4426 __func__, err);
4427 goto out;
4428 }
4429
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004430 /* Poll fDeviceInit flag to be cleared */
4431 timeout = ktime_add_ms(ktime_get(), FDEVICEINIT_COMPL_TIMEOUT);
4432 do {
4433 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4434 QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res);
4435 if (!flag_res)
4436 break;
4437 usleep_range(5000, 10000);
4438 } while (ktime_before(ktime_get(), timeout));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004439
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004440 if (err) {
Dolev Raviv68078d52013-07-30 00:35:58 +05304441 dev_err(hba->dev,
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004442 "%s reading fDeviceInit flag failed with error %d\n",
4443 __func__, err);
4444 } else if (flag_res) {
Dolev Raviv68078d52013-07-30 00:35:58 +05304445 dev_err(hba->dev,
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004446 "%s fDeviceInit was not cleared by the device\n",
4447 __func__);
4448 err = -EBUSY;
4449 }
Dolev Raviv68078d52013-07-30 00:35:58 +05304450out:
4451 return err;
4452}
4453
4454/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304455 * ufshcd_make_hba_operational - Make UFS controller operational
4456 * @hba: per adapter instance
4457 *
4458 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004459 * 1. Enable required interrupts
4460 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004461 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004462 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304463 *
4464 * Returns 0 on success, non-zero value on failure
4465 */
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004466int ufshcd_make_hba_operational(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304467{
4468 int err = 0;
4469 u32 reg;
4470
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304471 /* Enable required interrupts */
4472 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4473
4474 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004475 if (ufshcd_is_intr_aggr_allowed(hba))
4476 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4477 else
4478 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304479
4480 /* Configure UTRL and UTMRL base address registers */
4481 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4482 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4483 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4484 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4485 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4486 REG_UTP_TASK_REQ_LIST_BASE_L);
4487 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4488 REG_UTP_TASK_REQ_LIST_BASE_H);
4489
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304490 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004491 * Make sure base address and interrupt setup are updated before
4492 * enabling the run/stop registers below.
4493 */
4494 wmb();
4495
4496 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304497 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304498 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004499 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304500 if (!(ufshcd_get_lists_status(reg))) {
4501 ufshcd_enable_run_stop_reg(hba);
4502 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304503 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304504 "Host controller not ready to process requests");
4505 err = -EIO;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304506 }
4507
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304508 return err;
4509}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004510EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304511
4512/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004513 * ufshcd_hba_stop - Send controller to reset state
4514 * @hba: per adapter instance
Yaniv Gardi596585a2016-03-10 17:37:08 +02004515 */
Alice.Chao3a95f5b2021-05-28 11:36:21 +08004516void ufshcd_hba_stop(struct ufs_hba *hba)
Yaniv Gardi596585a2016-03-10 17:37:08 +02004517{
Bart Van Assche5cac1092020-05-07 15:27:50 -07004518 unsigned long flags;
Yaniv Gardi596585a2016-03-10 17:37:08 +02004519 int err;
4520
Bart Van Assche5cac1092020-05-07 15:27:50 -07004521 /*
4522 * Obtain the host lock to prevent that the controller is disabled
4523 * while the UFS interrupt handler is active on another CPU.
4524 */
4525 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004526 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
Bart Van Assche5cac1092020-05-07 15:27:50 -07004527 spin_unlock_irqrestore(hba->host->host_lock, flags);
4528
Yaniv Gardi596585a2016-03-10 17:37:08 +02004529 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4530 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
Bart Van Assche5cac1092020-05-07 15:27:50 -07004531 10, 1);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004532 if (err)
4533 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4534}
Alice.Chao3a95f5b2021-05-28 11:36:21 +08004535EXPORT_SYMBOL_GPL(ufshcd_hba_stop);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004536
4537/**
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304538 * ufshcd_hba_execute_hce - initialize the controller
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304539 * @hba: per adapter instance
4540 *
4541 * The controller resets itself and controller firmware initialization
4542 * sequence kicks off. When controller is ready it will set
4543 * the Host Controller Enable bit to 1.
4544 *
4545 * Returns 0 on success, non-zero value on failure
4546 */
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304547static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304548{
Stanley Chu6081b122020-11-12 13:45:37 +08004549 int retry_outer = 3;
4550 int retry_inner;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304551
Stanley Chu6081b122020-11-12 13:45:37 +08004552start:
Yaniv Gardi596585a2016-03-10 17:37:08 +02004553 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304554 /* change controller state to "reset state" */
Bart Van Assche5cac1092020-05-07 15:27:50 -07004555 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304556
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004557 /* UniPro link is disabled at this point */
4558 ufshcd_set_link_off(hba);
4559
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004560 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004561
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304562 /* start controller initialization sequence */
4563 ufshcd_hba_start(hba);
4564
4565 /*
4566 * To initialize a UFS host controller HCE bit must be set to 1.
4567 * During initialization the HCE bit value changes from 1->0->1.
4568 * When the host controller completes initialization sequence
4569 * it sets the value of HCE bit to 1. The same HCE bit is read back
4570 * to check if the controller has completed initialization sequence.
4571 * So without this delay the value HCE = 1, set in the previous
4572 * instruction might be read back.
4573 * This delay can be changed based on the controller.
4574 */
Stanley Chu90b84912020-05-09 17:37:13 +08004575 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304576
4577 /* wait for the host controller to complete initialization */
Stanley Chu6081b122020-11-12 13:45:37 +08004578 retry_inner = 50;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304579 while (ufshcd_is_hba_active(hba)) {
Stanley Chu6081b122020-11-12 13:45:37 +08004580 if (retry_inner) {
4581 retry_inner--;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304582 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304583 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304584 "Controller enable failed\n");
Stanley Chu6081b122020-11-12 13:45:37 +08004585 if (retry_outer) {
4586 retry_outer--;
4587 goto start;
4588 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304589 return -EIO;
4590 }
Stanley Chu9fc305e2020-03-18 18:40:15 +08004591 usleep_range(1000, 1100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304592 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004593
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004594 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004595 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004596
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004597 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004598
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304599 return 0;
4600}
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304601
4602int ufshcd_hba_enable(struct ufs_hba *hba)
4603{
4604 int ret;
4605
4606 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
4607 ufshcd_set_link_off(hba);
4608 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4609
4610 /* enable UIC related interrupts */
4611 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4612 ret = ufshcd_dme_reset(hba);
4613 if (!ret) {
4614 ret = ufshcd_dme_enable(hba);
4615 if (!ret)
4616 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4617 if (ret)
4618 dev_err(hba->dev,
4619 "Host controller enable failed with non-hce\n");
4620 }
4621 } else {
4622 ret = ufshcd_hba_execute_hce(hba);
4623 }
4624
4625 return ret;
4626}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004627EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
4628
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004629static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4630{
Stanley Chuba0320f2020-03-18 18:40:10 +08004631 int tx_lanes = 0, i, err = 0;
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004632
4633 if (!peer)
4634 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4635 &tx_lanes);
4636 else
4637 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4638 &tx_lanes);
4639 for (i = 0; i < tx_lanes; i++) {
4640 if (!peer)
4641 err = ufshcd_dme_set(hba,
4642 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4643 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4644 0);
4645 else
4646 err = ufshcd_dme_peer_set(hba,
4647 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4648 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4649 0);
4650 if (err) {
4651 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4652 __func__, peer, i, err);
4653 break;
4654 }
4655 }
4656
4657 return err;
4658}
4659
4660static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4661{
4662 return ufshcd_disable_tx_lcc(hba, true);
4663}
4664
Stanley Chue965e5e2020-12-05 19:58:59 +08004665void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val)
Stanley Chu8808b4e2019-07-10 21:38:21 +08004666{
Stanley Chue965e5e2020-12-05 19:58:59 +08004667 struct ufs_event_hist *e;
4668
4669 if (id >= UFS_EVT_CNT)
4670 return;
4671
4672 e = &hba->ufs_stats.event[id];
4673 e->val[e->pos] = val;
4674 e->tstamp[e->pos] = ktime_get();
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02004675 e->cnt += 1;
Stanley Chue965e5e2020-12-05 19:58:59 +08004676 e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH;
Stanley Chu172614a2020-12-05 19:59:00 +08004677
4678 ufshcd_vops_event_notify(hba, id, &val);
Stanley Chu8808b4e2019-07-10 21:38:21 +08004679}
Stanley Chue965e5e2020-12-05 19:58:59 +08004680EXPORT_SYMBOL_GPL(ufshcd_update_evt_hist);
Stanley Chu8808b4e2019-07-10 21:38:21 +08004681
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304682/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304683 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304684 * @hba: per adapter instance
4685 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304686 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304687 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304688static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304689{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304690 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004691 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004692 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304693
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004694 /*
4695 * If UFS device isn't active then we will have to issue link startup
4696 * 2 times to make sure the device state move to active.
4697 */
4698 if (!ufshcd_is_ufs_dev_active(hba))
4699 link_startup_again = true;
4700
4701link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004702 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004703 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304704
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004705 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004706
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004707 /* check if device is detected by inter-connect layer */
4708 if (!ret && !ufshcd_is_device_present(hba)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08004709 ufshcd_update_evt_hist(hba,
4710 UFS_EVT_LINK_STARTUP_FAIL,
Stanley Chu8808b4e2019-07-10 21:38:21 +08004711 0);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004712 dev_err(hba->dev, "%s: Device not present\n", __func__);
4713 ret = -ENXIO;
4714 goto out;
4715 }
4716
4717 /*
4718 * DME link lost indication is only received when link is up,
4719 * but we can't be sure if the link is up until link startup
4720 * succeeds. So reset the local Uni-Pro and try again.
4721 */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004722 if (ret && ufshcd_hba_enable(hba)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08004723 ufshcd_update_evt_hist(hba,
4724 UFS_EVT_LINK_STARTUP_FAIL,
Stanley Chu8808b4e2019-07-10 21:38:21 +08004725 (u32)ret);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004726 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004727 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004728 } while (ret && retries--);
4729
Stanley Chu8808b4e2019-07-10 21:38:21 +08004730 if (ret) {
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004731 /* failed to get the link up... retire */
Stanley Chue965e5e2020-12-05 19:58:59 +08004732 ufshcd_update_evt_hist(hba,
4733 UFS_EVT_LINK_STARTUP_FAIL,
Stanley Chu8808b4e2019-07-10 21:38:21 +08004734 (u32)ret);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304735 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004736 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304737
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004738 if (link_startup_again) {
4739 link_startup_again = false;
4740 retries = DME_LINKSTARTUP_RETRIES;
4741 goto link_startup;
4742 }
4743
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004744 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4745 ufshcd_init_pwr_info(hba);
4746 ufshcd_print_pwr_info(hba);
4747
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004748 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4749 ret = ufshcd_disable_device_tx_lcc(hba);
4750 if (ret)
4751 goto out;
4752 }
4753
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004754 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004755 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4756 if (ret)
4757 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004758
Can Guo2355b662020-08-24 19:07:06 -07004759 /* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */
4760 ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004761 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304762out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004763 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304764 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004765 ufshcd_print_host_state(hba);
4766 ufshcd_print_pwr_info(hba);
Stanley Chue965e5e2020-12-05 19:58:59 +08004767 ufshcd_print_evt_hist(hba);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004768 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304769 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304770}
4771
4772/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304773 * ufshcd_verify_dev_init() - Verify device initialization
4774 * @hba: per-adapter instance
4775 *
4776 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4777 * device Transport Protocol (UTP) layer is ready after a reset.
4778 * If the UTP layer at the device side is not initialized, it may
4779 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4780 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4781 */
4782static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4783{
4784 int err = 0;
4785 int retries;
4786
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004787 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304788 mutex_lock(&hba->dev_cmd.lock);
4789 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4790 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
Adrian Hunter1cbc9ad2021-08-31 17:53:17 +03004791 hba->nop_out_timeout);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304792
4793 if (!err || err == -ETIMEDOUT)
4794 break;
4795
4796 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4797 }
4798 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004799 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304800
4801 if (err)
4802 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4803 return err;
4804}
4805
4806/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004807 * ufshcd_set_queue_depth - set lun queue depth
4808 * @sdev: pointer to SCSI device
4809 *
4810 * Read bLUQueueDepth value and activate scsi tagged command
4811 * queueing. For WLUN, queue depth is set to 1. For best-effort
4812 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4813 * value that host can queue.
4814 */
4815static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4816{
4817 int ret = 0;
4818 u8 lun_qdepth;
4819 struct ufs_hba *hba;
4820
4821 hba = shost_priv(sdev->host);
4822
4823 lun_qdepth = hba->nutrs;
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02004824 ret = ufshcd_read_unit_desc_param(hba,
4825 ufshcd_scsi_to_upiu_lun(sdev->lun),
4826 UNIT_DESC_PARAM_LU_Q_DEPTH,
4827 &lun_qdepth,
4828 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004829
4830 /* Some WLUN doesn't support unit descriptor */
4831 if (ret == -EOPNOTSUPP)
4832 lun_qdepth = 1;
4833 else if (!lun_qdepth)
4834 /* eventually, we can figure out the real queue depth */
4835 lun_qdepth = hba->nutrs;
4836 else
4837 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4838
4839 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4840 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004841 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004842}
4843
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004844/*
4845 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4846 * @hba: per-adapter instance
4847 * @lun: UFS device lun id
4848 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4849 *
4850 * Returns 0 in case of success and b_lu_write_protect status would be returned
4851 * @b_lu_write_protect parameter.
4852 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4853 * Returns -EINVAL in case of invalid parameters passed to this function.
4854 */
4855static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4856 u8 lun,
4857 u8 *b_lu_write_protect)
4858{
4859 int ret;
4860
4861 if (!b_lu_write_protect)
4862 ret = -EINVAL;
4863 /*
4864 * According to UFS device spec, RPMB LU can't be write
4865 * protected so skip reading bLUWriteProtect parameter for
4866 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4867 */
Bean Huo1baa8012020-01-20 14:08:20 +01004868 else if (lun >= hba->dev_info.max_lu_supported)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004869 ret = -ENOTSUPP;
4870 else
4871 ret = ufshcd_read_unit_desc_param(hba,
4872 lun,
4873 UNIT_DESC_PARAM_LU_WR_PROTECT,
4874 b_lu_write_protect,
4875 sizeof(*b_lu_write_protect));
4876 return ret;
4877}
4878
4879/**
4880 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4881 * status
4882 * @hba: per-adapter instance
4883 * @sdev: pointer to SCSI device
4884 *
4885 */
4886static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4887 struct scsi_device *sdev)
4888{
4889 if (hba->dev_info.f_power_on_wp_en &&
4890 !hba->dev_info.is_lu_power_on_wp) {
4891 u8 b_lu_write_protect;
4892
4893 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4894 &b_lu_write_protect) &&
4895 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4896 hba->dev_info.is_lu_power_on_wp = true;
4897 }
4898}
4899
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004900/**
Asutosh Dasb294ff32021-04-23 17:20:16 -07004901 * ufshcd_setup_links - associate link b/w device wlun and other luns
4902 * @sdev: pointer to SCSI device
4903 * @hba: pointer to ufs hba
4904 */
4905static void ufshcd_setup_links(struct ufs_hba *hba, struct scsi_device *sdev)
4906{
4907 struct device_link *link;
4908
4909 /*
4910 * Device wlun is the supplier & rest of the luns are consumers.
4911 * This ensures that device wlun suspends after all other luns.
4912 */
4913 if (hba->sdev_ufs_device) {
4914 link = device_link_add(&sdev->sdev_gendev,
4915 &hba->sdev_ufs_device->sdev_gendev,
4916 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE);
4917 if (!link) {
4918 dev_err(&sdev->sdev_gendev, "Failed establishing link - %s\n",
4919 dev_name(&hba->sdev_ufs_device->sdev_gendev));
4920 return;
4921 }
4922 hba->luns_avail--;
4923 /* Ignore REPORT_LUN wlun probing */
4924 if (hba->luns_avail == 1) {
4925 ufshcd_rpm_put(hba);
4926 return;
4927 }
4928 } else {
4929 /*
4930 * Device wlun is probed. The assumption is that WLUNs are
4931 * scanned before other LUNs.
4932 */
4933 hba->luns_avail--;
4934 }
4935}
4936
4937/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304938 * ufshcd_slave_alloc - handle initial SCSI device configurations
4939 * @sdev: pointer to SCSI device
4940 *
4941 * Returns success
4942 */
4943static int ufshcd_slave_alloc(struct scsi_device *sdev)
4944{
4945 struct ufs_hba *hba;
4946
4947 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304948
4949 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4950 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304951
Can Guoa3a76392019-12-05 02:14:30 +00004952 /* DBD field should be set to 1 in mode sense(10) */
4953 sdev->set_dbd_for_ms = 1;
4954
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304955 /* allow SCSI layer to restart the device in case of errors */
4956 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004957
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004958 /* REPORT SUPPORTED OPERATION CODES is not supported */
4959 sdev->no_report_opcodes = 1;
4960
Sujit Reddy Thumma84af7e82018-01-24 09:52:35 +05304961 /* WRITE_SAME command is not supported */
4962 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004963
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004964 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004965
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004966 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4967
Asutosh Dasb294ff32021-04-23 17:20:16 -07004968 ufshcd_setup_links(hba, sdev);
4969
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004970 return 0;
4971}
4972
4973/**
4974 * ufshcd_change_queue_depth - change queue depth
4975 * @sdev: pointer to SCSI device
4976 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004977 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004978 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004979 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004980static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004981{
Bart Van Asschefc21da82021-12-03 15:19:41 -08004982 return scsi_change_queue_depth(sdev, min(depth, sdev->host->can_queue));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304983}
4984
Daejun Parkf02bc972021-07-12 17:58:30 +09004985static void ufshcd_hpb_destroy(struct ufs_hba *hba, struct scsi_device *sdev)
4986{
4987 /* skip well-known LU */
Daejun Park41d8a932021-07-12 18:00:25 +09004988 if ((sdev->lun >= UFS_UPIU_MAX_UNIT_NUM_ID) ||
4989 !(hba->dev_info.hpb_enabled) || !ufshpb_is_allowed(hba))
Daejun Parkf02bc972021-07-12 17:58:30 +09004990 return;
4991
4992 ufshpb_destroy_lu(hba, sdev);
4993}
4994
4995static void ufshcd_hpb_configure(struct ufs_hba *hba, struct scsi_device *sdev)
4996{
4997 /* skip well-known LU */
4998 if ((sdev->lun >= UFS_UPIU_MAX_UNIT_NUM_ID) ||
4999 !(hba->dev_info.hpb_enabled) || !ufshpb_is_allowed(hba))
5000 return;
5001
5002 ufshpb_init_hpb_lu(hba, sdev);
5003}
5004
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305005/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005006 * ufshcd_slave_configure - adjust SCSI device configurations
5007 * @sdev: pointer to SCSI device
5008 */
5009static int ufshcd_slave_configure(struct scsi_device *sdev)
5010{
Stanley Chu49615ba2019-09-16 23:56:50 +08005011 struct ufs_hba *hba = shost_priv(sdev->host);
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005012 struct request_queue *q = sdev->request_queue;
5013
Daejun Parkf02bc972021-07-12 17:58:30 +09005014 ufshcd_hpb_configure(hba, sdev);
5015
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005016 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
Kiwoong Kim2b2bfc8a2021-01-19 12:33:41 +09005017 if (hba->quirks & UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE)
5018 blk_queue_update_dma_alignment(q, PAGE_SIZE - 1);
Asutosh Dasb294ff32021-04-23 17:20:16 -07005019 /*
5020 * Block runtime-pm until all consumers are added.
5021 * Refer ufshcd_setup_links().
5022 */
5023 if (is_device_wlun(sdev))
5024 pm_runtime_get_noresume(&sdev->sdev_gendev);
5025 else if (ufshcd_is_rpm_autosuspend_allowed(hba))
Stanley Chu49615ba2019-09-16 23:56:50 +08005026 sdev->rpm_autosuspend = 1;
5027
Eric Biggerscb77cb52021-10-18 11:04:52 -07005028 ufshcd_crypto_register(hba, q);
Satya Tangiraladf043c742020-07-06 20:04:14 +00005029
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005030 return 0;
5031}
5032
5033/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305034 * ufshcd_slave_destroy - remove SCSI device configurations
5035 * @sdev: pointer to SCSI device
5036 */
5037static void ufshcd_slave_destroy(struct scsi_device *sdev)
5038{
5039 struct ufs_hba *hba;
Adrian Hunterbf259672021-08-06 16:04:41 +03005040 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305041
5042 hba = shost_priv(sdev->host);
Daejun Parkf02bc972021-07-12 17:58:30 +09005043
5044 ufshcd_hpb_destroy(hba, sdev);
5045
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005046 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005047 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005048 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005049 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005050 spin_unlock_irqrestore(hba->host->host_lock, flags);
Adrian Hunterbf259672021-08-06 16:04:41 +03005051 } else if (hba->sdev_ufs_device) {
5052 struct device *supplier = NULL;
5053
5054 /* Ensure UFS Device WLUN exists and does not disappear */
5055 spin_lock_irqsave(hba->host->host_lock, flags);
5056 if (hba->sdev_ufs_device) {
5057 supplier = &hba->sdev_ufs_device->sdev_gendev;
5058 get_device(supplier);
5059 }
5060 spin_unlock_irqrestore(hba->host->host_lock, flags);
5061
5062 if (supplier) {
5063 /*
5064 * If a LUN fails to probe (e.g. absent BOOT WLUN), the
5065 * device will not have been registered but can still
5066 * have a device link holding a reference to the device.
5067 */
5068 device_link_remove(&sdev->sdev_gendev, supplier);
5069 put_device(supplier);
5070 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005071 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305072}
5073
5074/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305075 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
Bart Van Assche8aa29f12018-03-01 15:07:20 -08005076 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305077 * @scsi_status: SCSI command status
5078 *
5079 * Returns value base on SCSI command status
5080 */
5081static inline int
5082ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
5083{
5084 int result = 0;
5085
5086 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05305087 case SAM_STAT_CHECK_CONDITION:
5088 ufshcd_copy_sense_data(lrbp);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05005089 fallthrough;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305090 case SAM_STAT_GOOD:
Hannes Reineckedb83d8a2021-01-13 10:04:48 +01005091 result |= DID_OK << 16 | scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305092 break;
5093 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05305094 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305095 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05305096 ufshcd_copy_sense_data(lrbp);
5097 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305098 break;
5099 default:
5100 result |= DID_ERROR << 16;
5101 break;
5102 } /* end of switch */
5103
5104 return result;
5105}
5106
5107/**
5108 * ufshcd_transfer_rsp_status - Get overall status of the response
5109 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08005110 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305111 *
5112 * Returns result of the command to notify SCSI midlayer
5113 */
5114static inline int
5115ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
5116{
5117 int result = 0;
5118 int scsi_status;
Bart Van Assche957d63e2021-10-20 14:40:17 -07005119 enum utp_ocs ocs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305120
5121 /* overall command status of utrd */
5122 ocs = ufshcd_get_tr_ocs(lrbp);
5123
Kiwoong Kimd779a6e2020-05-28 06:46:53 +05305124 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) {
5125 if (be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_1) &
5126 MASK_RSP_UPIU_RESULT)
5127 ocs = OCS_SUCCESS;
5128 }
5129
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305130 switch (ocs) {
5131 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305132 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005133 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305134 switch (result) {
5135 case UPIU_TRANSACTION_RESPONSE:
5136 /*
5137 * get the response UPIU result to extract
5138 * the SCSI command status
5139 */
5140 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
5141
5142 /*
5143 * get the result based on SCSI status response
5144 * to notify the SCSI midlayer of the command status
5145 */
5146 scsi_status = result & MASK_SCSI_STATUS;
5147 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305148
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02005149 /*
5150 * Currently we are only supporting BKOPs exception
5151 * events hence we can ignore BKOPs exception event
5152 * during power management callbacks. BKOPs exception
5153 * event is not expected to be raised in runtime suspend
5154 * callback as it allows the urgent bkops.
5155 * During system suspend, we are anyway forcefully
5156 * disabling the bkops and if urgent bkops is needed
5157 * it will be enabled on system resume. Long term
5158 * solution could be to abort the system suspend if
5159 * UFS device needs urgent BKOPs.
5160 */
5161 if (!hba->pm_op_in_progress &&
Can Guoaa53f582021-02-23 21:36:47 -08005162 !ufshcd_eh_in_progress(hba) &&
Asutosh Dasb294ff32021-04-23 17:20:16 -07005163 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
5164 /* Flushed in suspend */
5165 schedule_work(&hba->eeh_work);
Daejun Park4b5f4902021-07-12 17:58:59 +09005166
5167 if (scsi_status == SAM_STAT_GOOD)
5168 ufshpb_rsp_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305169 break;
5170 case UPIU_TRANSACTION_REJECT_UPIU:
5171 /* TODO: handle Reject UPIU Response */
5172 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305173 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305174 "Reject UPIU not fully implemented\n");
5175 break;
5176 default:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305177 dev_err(hba->dev,
5178 "Unexpected request response code = %x\n",
5179 result);
Stanley Chue0347d82019-04-15 20:23:38 +08005180 result = DID_ERROR << 16;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305181 break;
5182 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305183 break;
5184 case OCS_ABORTED:
5185 result |= DID_ABORT << 16;
5186 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305187 case OCS_INVALID_COMMAND_STATUS:
5188 result |= DID_REQUEUE << 16;
5189 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305190 case OCS_INVALID_CMD_TABLE_ATTR:
5191 case OCS_INVALID_PRDT_ATTR:
5192 case OCS_MISMATCH_DATA_BUF_SIZE:
5193 case OCS_MISMATCH_RESP_UPIU_SIZE:
5194 case OCS_PEER_COMM_FAILURE:
5195 case OCS_FATAL_ERROR:
Satya Tangirala5e7341e2020-07-06 20:04:12 +00005196 case OCS_DEVICE_FATAL_ERROR:
5197 case OCS_INVALID_CRYPTO_CONFIG:
5198 case OCS_GENERAL_CRYPTO_ERROR:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305199 default:
5200 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305201 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005202 "OCS error from controller = %x for tag %d\n",
5203 ocs, lrbp->task_tag);
Stanley Chue965e5e2020-12-05 19:58:59 +08005204 ufshcd_print_evt_hist(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08005205 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305206 break;
5207 } /* end of switch */
5208
Jaegeuk Kimeeb1b552021-01-07 10:53:16 -08005209 if ((host_byte(result) != DID_OK) &&
5210 (host_byte(result) != DID_REQUEUE) && !hba->silence_err_logs)
Dolev Raviv66cc8202016-12-22 18:39:42 -08005211 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305212 return result;
5213}
5214
Can Guoa45f9372021-05-24 01:36:57 -07005215static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
5216 u32 intr_mask)
5217{
5218 if (!ufshcd_is_auto_hibern8_supported(hba) ||
5219 !ufshcd_is_auto_hibern8_enabled(hba))
5220 return false;
5221
5222 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
5223 return false;
5224
5225 if (hba->active_uic_cmd &&
5226 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
5227 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
5228 return false;
5229
5230 return true;
5231}
5232
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305233/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305234 * ufshcd_uic_cmd_compl - handle completion of uic command
5235 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305236 * @intr_status: interrupt status generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005237 *
5238 * Returns
5239 * IRQ_HANDLED - If interrupt is valid
5240 * IRQ_NONE - If invalid interrupt
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305241 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005242static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305243{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005244 irqreturn_t retval = IRQ_NONE;
5245
Can Guoa45f9372021-05-24 01:36:57 -07005246 spin_lock(hba->host->host_lock);
5247 if (ufshcd_is_auto_hibern8_error(hba, intr_status))
5248 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
5249
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305250 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305251 hba->active_uic_cmd->argument2 |=
5252 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05305253 hba->active_uic_cmd->argument3 =
5254 ufshcd_get_dme_attr_val(hba);
Can Guo0f52fcb92020-11-02 22:24:40 -08005255 if (!hba->uic_async_done)
5256 hba->active_uic_cmd->cmd_active = 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305257 complete(&hba->active_uic_cmd->done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005258 retval = IRQ_HANDLED;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305259 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305260
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005261 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
Can Guo0f52fcb92020-11-02 22:24:40 -08005262 hba->active_uic_cmd->cmd_active = 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005263 complete(hba->uic_async_done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005264 retval = IRQ_HANDLED;
5265 }
Stanley Chuaa5c6972020-06-15 15:22:35 +08005266
5267 if (retval == IRQ_HANDLED)
5268 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd,
Bean Huo28fa68f2021-01-05 12:34:42 +01005269 UFS_CMD_COMP);
Can Guoa45f9372021-05-24 01:36:57 -07005270 spin_unlock(hba->host->host_lock);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005271 return retval;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305272}
5273
Bart Van Assche6f8dafd2021-12-03 15:19:45 -08005274/* Release the resources allocated for processing a SCSI command. */
5275static void ufshcd_release_scsi_cmd(struct ufs_hba *hba,
5276 struct ufshcd_lrb *lrbp)
5277{
5278 struct scsi_cmnd *cmd = lrbp->cmd;
5279
5280 scsi_dma_unmap(cmd);
5281 lrbp->cmd = NULL; /* Mark the command as completed. */
5282 ufshcd_release(hba);
5283 ufshcd_clk_scaling_update_busy(hba);
5284}
5285
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305286/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005287 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305288 * @hba: per adapter instance
Bart Van Assche73dc3c42021-07-21 20:34:38 -07005289 * @completed_reqs: bitmask that indicates which requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305290 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005291static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
Bart Van Assche11682522021-10-20 14:40:15 -07005292 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305293{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305294 struct ufshcd_lrb *lrbp;
5295 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305296 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005297
Dolev Ravive9d501b2014-07-01 12:22:37 +03005298 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
5299 lrbp = &hba->lrb[index];
Stanley Chua3170372020-07-06 14:07:06 +08005300 lrbp->compl_time_stamp = ktime_get();
Dolev Ravive9d501b2014-07-01 12:22:37 +03005301 cmd = lrbp->cmd;
5302 if (cmd) {
Can Guo1d8613a2021-04-21 19:28:39 -07005303 if (unlikely(ufshcd_should_inform_monitor(hba, lrbp)))
5304 ufshcd_update_monitor(hba, lrbp);
Bean Huo28fa68f2021-01-05 12:34:42 +01005305 ufshcd_add_command_trace(hba, index, UFS_CMD_COMP);
Bart Van Assche6f8dafd2021-12-03 15:19:45 -08005306 cmd->result = ufshcd_transfer_rsp_status(hba, lrbp);
5307 ufshcd_release_scsi_cmd(hba, lrbp);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005308 /* Do not touch lrbp after scsi done */
Bart Van Assche35c37302021-10-07 13:46:01 -07005309 scsi_done(cmd);
Joao Pinto300bb132016-05-11 12:21:27 +01005310 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
5311 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08005312 if (hba->dev_cmd.complete) {
5313 ufshcd_add_command_trace(hba, index,
Bean Huo28fa68f2021-01-05 12:34:42 +01005314 UFS_DEV_COMP);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005315 complete(hba->dev_cmd.complete);
Bart Van Assche3eb9dcc2021-12-03 15:19:44 -08005316 ufshcd_clk_scaling_update_busy(hba);
Lee Susman1a07f2d2016-12-22 18:42:03 -08005317 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03005318 }
5319 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305320}
5321
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08005322/*
5323 * Returns > 0 if one or more commands have been completed or 0 if no
5324 * requests have been completed.
5325 */
5326static int ufshcd_poll(struct Scsi_Host *shost, unsigned int queue_num)
5327{
5328 struct ufs_hba *hba = shost_priv(shost);
5329 unsigned long completed_reqs, flags;
5330 u32 tr_doorbell;
5331
5332 spin_lock_irqsave(&hba->outstanding_lock, flags);
5333 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5334 completed_reqs = ~tr_doorbell & hba->outstanding_reqs;
5335 WARN_ONCE(completed_reqs & ~hba->outstanding_reqs,
5336 "completed: %#lx; outstanding: %#lx\n", completed_reqs,
5337 hba->outstanding_reqs);
5338 hba->outstanding_reqs &= ~completed_reqs;
5339 spin_unlock_irqrestore(&hba->outstanding_lock, flags);
5340
5341 if (completed_reqs)
5342 __ufshcd_transfer_req_compl(hba, completed_reqs);
5343
5344 return completed_reqs;
5345}
5346
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305347/**
Bart Van Assche1f522c52021-07-21 20:34:32 -07005348 * ufshcd_transfer_req_compl - handle SCSI and query command completion
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005349 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005350 *
5351 * Returns
5352 * IRQ_HANDLED - If interrupt is valid
5353 * IRQ_NONE - If invalid interrupt
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005354 */
Bart Van Assche11682522021-10-20 14:40:15 -07005355static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005356{
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005357 /* Resetting interrupt aggregation counters first and reading the
5358 * DOOR_BELL afterward allows us to handle all the completed requests.
5359 * In order to prevent other interrupts starvation the DB is read once
5360 * after reset. The down side of this solution is the possibility of
5361 * false interrupt if device completes another request after resetting
5362 * aggregation and before reading the DB.
5363 */
Alim Akhtarb638b5e2020-05-28 06:46:50 +05305364 if (ufshcd_is_intr_aggr_allowed(hba) &&
5365 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005366 ufshcd_reset_intr_aggr(hba);
5367
Bart Van Asschec11a1ae2021-07-21 20:34:39 -07005368 if (ufs_fail_completion())
5369 return IRQ_HANDLED;
5370
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08005371 /*
5372 * Ignore the ufshcd_poll() return value and return IRQ_HANDLED since we
5373 * do not want polling to trigger spurious interrupt complaints.
5374 */
5375 ufshcd_poll(hba->host, 0);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005376
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08005377 return IRQ_HANDLED;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005378}
5379
Adrian Hunter7deedfd2021-02-09 08:24:37 +02005380int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask)
Adrian Huntercd469472021-02-09 08:24:36 +02005381{
5382 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5383 QUERY_ATTR_IDN_EE_CONTROL, 0, 0,
5384 &ee_ctrl_mask);
5385}
5386
Adrian Hunter7deedfd2021-02-09 08:24:37 +02005387int ufshcd_write_ee_control(struct ufs_hba *hba)
Adrian Huntercd469472021-02-09 08:24:36 +02005388{
5389 int err;
5390
5391 mutex_lock(&hba->ee_ctrl_mutex);
5392 err = __ufshcd_write_ee_control(hba, hba->ee_ctrl_mask);
5393 mutex_unlock(&hba->ee_ctrl_mutex);
5394 if (err)
5395 dev_err(hba->dev, "%s: failed to write ee control %d\n",
5396 __func__, err);
5397 return err;
5398}
5399
5400int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask, u16 *other_mask,
5401 u16 set, u16 clr)
5402{
5403 u16 new_mask, ee_ctrl_mask;
5404 int err = 0;
5405
5406 mutex_lock(&hba->ee_ctrl_mutex);
5407 new_mask = (*mask & ~clr) | set;
5408 ee_ctrl_mask = new_mask | *other_mask;
5409 if (ee_ctrl_mask != hba->ee_ctrl_mask)
5410 err = __ufshcd_write_ee_control(hba, ee_ctrl_mask);
5411 /* Still need to update 'mask' even if 'ee_ctrl_mask' was unchanged */
5412 if (!err) {
5413 hba->ee_ctrl_mask = ee_ctrl_mask;
5414 *mask = new_mask;
5415 }
5416 mutex_unlock(&hba->ee_ctrl_mutex);
5417 return err;
5418}
5419
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005420/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305421 * ufshcd_disable_ee - disable exception event
5422 * @hba: per-adapter instance
5423 * @mask: exception event to disable
5424 *
5425 * Disables exception event in the device so that the EVENT_ALERT
5426 * bit is not set.
5427 *
5428 * Returns zero on success, non-zero error value on failure.
5429 */
Adrian Huntercd469472021-02-09 08:24:36 +02005430static inline int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305431{
Adrian Huntercd469472021-02-09 08:24:36 +02005432 return ufshcd_update_ee_drv_mask(hba, 0, mask);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305433}
5434
5435/**
5436 * ufshcd_enable_ee - enable exception event
5437 * @hba: per-adapter instance
5438 * @mask: exception event to enable
5439 *
5440 * Enable corresponding exception event in the device to allow
5441 * device to alert host in critical scenarios.
5442 *
5443 * Returns zero on success, non-zero error value on failure.
5444 */
Adrian Huntercd469472021-02-09 08:24:36 +02005445static inline int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305446{
Adrian Huntercd469472021-02-09 08:24:36 +02005447 return ufshcd_update_ee_drv_mask(hba, mask, 0);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305448}
5449
5450/**
5451 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5452 * @hba: per-adapter instance
5453 *
5454 * Allow device to manage background operations on its own. Enabling
5455 * this might lead to inconsistent latencies during normal data transfers
5456 * as the device is allowed to manage its own way of handling background
5457 * operations.
5458 *
5459 * Returns zero on success, non-zero on failure.
5460 */
5461static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5462{
5463 int err = 0;
5464
5465 if (hba->auto_bkops_enabled)
5466 goto out;
5467
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005468 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005469 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305470 if (err) {
5471 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5472 __func__, err);
5473 goto out;
5474 }
5475
5476 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005477 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305478
5479 /* No need of URGENT_BKOPS exception from the device */
5480 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5481 if (err)
5482 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5483 __func__, err);
5484out:
5485 return err;
5486}
5487
5488/**
5489 * ufshcd_disable_auto_bkops - block device in doing background operations
5490 * @hba: per-adapter instance
5491 *
5492 * Disabling background operations improves command response latency but
5493 * has drawback of device moving into critical state where the device is
5494 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5495 * host is idle so that BKOPS are managed effectively without any negative
5496 * impacts.
5497 *
5498 * Returns zero on success, non-zero on failure.
5499 */
5500static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5501{
5502 int err = 0;
5503
5504 if (!hba->auto_bkops_enabled)
5505 goto out;
5506
5507 /*
5508 * If host assisted BKOPs is to be enabled, make sure
5509 * urgent bkops exception is allowed.
5510 */
5511 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5512 if (err) {
5513 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5514 __func__, err);
5515 goto out;
5516 }
5517
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005518 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005519 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305520 if (err) {
5521 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5522 __func__, err);
5523 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5524 goto out;
5525 }
5526
5527 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005528 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Asutosh Das24366c2a2019-11-25 22:53:30 -08005529 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305530out:
5531 return err;
5532}
5533
5534/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005535 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305536 * @hba: per adapter instance
5537 *
5538 * After a device reset the device may toggle the BKOPS_EN flag
5539 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005540 * as well. This function would change the auto-bkops state based on
5541 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305542 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005543static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305544{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005545 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5546 hba->auto_bkops_enabled = false;
5547 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5548 ufshcd_enable_auto_bkops(hba);
5549 } else {
5550 hba->auto_bkops_enabled = true;
5551 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5552 ufshcd_disable_auto_bkops(hba);
5553 }
Stanley Chu7b6668d2020-05-30 22:12:00 +08005554 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
Asutosh Das24366c2a2019-11-25 22:53:30 -08005555 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305556}
5557
5558static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5559{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005560 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305561 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5562}
5563
5564/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005565 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5566 * @hba: per-adapter instance
5567 * @status: bkops_status value
5568 *
5569 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5570 * flag in the device to permit background operations if the device
5571 * bkops_status is greater than or equal to "status" argument passed to
5572 * this function, disable otherwise.
5573 *
5574 * Returns 0 for success, non-zero in case of failure.
5575 *
5576 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5577 * to know whether auto bkops is enabled or disabled after this function
5578 * returns control to it.
5579 */
5580static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5581 enum bkops_status status)
5582{
5583 int err;
5584 u32 curr_status = 0;
5585
5586 err = ufshcd_get_bkops_status(hba, &curr_status);
5587 if (err) {
5588 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5589 __func__, err);
5590 goto out;
5591 } else if (curr_status > BKOPS_STATUS_MAX) {
5592 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5593 __func__, curr_status);
5594 err = -EINVAL;
5595 goto out;
5596 }
5597
5598 if (curr_status >= status)
5599 err = ufshcd_enable_auto_bkops(hba);
5600 else
5601 err = ufshcd_disable_auto_bkops(hba);
5602out:
5603 return err;
5604}
5605
5606/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305607 * ufshcd_urgent_bkops - handle urgent bkops exception event
5608 * @hba: per-adapter instance
5609 *
5610 * Enable fBackgroundOpsEn flag in the device to permit background
5611 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005612 *
5613 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5614 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305615 */
5616static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5617{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005618 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305619}
5620
5621static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5622{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005623 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305624 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5625}
5626
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005627static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5628{
5629 int err;
5630 u32 curr_status = 0;
5631
5632 if (hba->is_urgent_bkops_lvl_checked)
5633 goto enable_auto_bkops;
5634
5635 err = ufshcd_get_bkops_status(hba, &curr_status);
5636 if (err) {
5637 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5638 __func__, err);
5639 goto out;
5640 }
5641
5642 /*
5643 * We are seeing that some devices are raising the urgent bkops
5644 * exception events even when BKOPS status doesn't indicate performace
5645 * impacted or critical. Handle these device by determining their urgent
5646 * bkops status at runtime.
5647 */
5648 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5649 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5650 __func__, curr_status);
5651 /* update the current status as the urgent bkops level */
5652 hba->urgent_bkops_lvl = curr_status;
5653 hba->is_urgent_bkops_lvl_checked = true;
5654 }
5655
5656enable_auto_bkops:
5657 err = ufshcd_enable_auto_bkops(hba);
5658out:
5659 if (err < 0)
5660 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5661 __func__, err);
5662}
5663
Avri Altman322c4b22021-09-15 09:04:07 +03005664static void ufshcd_temp_exception_event_handler(struct ufs_hba *hba, u16 status)
5665{
5666 u32 value;
5667
5668 if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5669 QUERY_ATTR_IDN_CASE_ROUGH_TEMP, 0, 0, &value))
5670 return;
5671
5672 dev_info(hba->dev, "exception Tcase %d\n", value - 80);
5673
5674 ufs_hwmon_notify_event(hba, status & MASK_EE_URGENT_TEMP);
5675
5676 /*
5677 * A placeholder for the platform vendors to add whatever additional
5678 * steps required
5679 */
5680}
5681
Yue Hu3b5f3c02021-03-18 17:55:36 +08005682static int __ufshcd_wb_toggle(struct ufs_hba *hba, bool set, enum flag_idn idn)
5683{
5684 u8 index;
5685 enum query_opcode opcode = set ? UPIU_QUERY_OPCODE_SET_FLAG :
5686 UPIU_QUERY_OPCODE_CLEAR_FLAG;
5687
5688 index = ufshcd_wb_get_query_index(hba);
5689 return ufshcd_query_flag_retry(hba, opcode, idn, index, NULL);
5690}
5691
5692int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005693{
5694 int ret;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005695
Stanley Chu79e35202020-05-08 16:01:15 +08005696 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005697 return 0;
5698
Bean Huo4cd48992021-01-19 17:38:46 +01005699 if (!(enable ^ hba->dev_info.wb_enabled))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005700 return 0;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005701
Yue Hu3b5f3c02021-03-18 17:55:36 +08005702 ret = __ufshcd_wb_toggle(hba, enable, QUERY_FLAG_IDN_WB_EN);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005703 if (ret) {
Yue Hu3b5f3c02021-03-18 17:55:36 +08005704 dev_err(hba->dev, "%s Write Booster %s failed %d\n",
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005705 __func__, enable ? "enable" : "disable", ret);
5706 return ret;
5707 }
5708
Bean Huo4cd48992021-01-19 17:38:46 +01005709 hba->dev_info.wb_enabled = enable;
Yue Hu3b5f3c02021-03-18 17:55:36 +08005710 dev_info(hba->dev, "%s Write Booster %s\n",
5711 __func__, enable ? "enabled" : "disabled");
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005712
5713 return ret;
5714}
5715
Yue Hu3b5f3c02021-03-18 17:55:36 +08005716static void ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005717{
Bean Huod3ba6222021-01-21 19:57:36 +01005718 int ret;
Yue Hu3b5f3c02021-03-18 17:55:36 +08005719
5720 ret = __ufshcd_wb_toggle(hba, set,
5721 QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8);
5722 if (ret) {
5723 dev_err(hba->dev, "%s: WB-Buf Flush during H8 %s failed: %d\n",
5724 __func__, set ? "enable" : "disable", ret);
5725 return;
5726 }
5727 dev_dbg(hba->dev, "%s WB-Buf Flush during H8 %s\n",
5728 __func__, set ? "enabled" : "disabled");
5729}
5730
5731static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable)
5732{
5733 int ret;
Bean Huod3ba6222021-01-21 19:57:36 +01005734
5735 if (!ufshcd_is_wb_allowed(hba) ||
5736 hba->dev_info.wb_buf_flush_enabled == enable)
Yue Hu3b5f3c02021-03-18 17:55:36 +08005737 return;
Bean Huod3ba6222021-01-21 19:57:36 +01005738
Yue Hu3b5f3c02021-03-18 17:55:36 +08005739 ret = __ufshcd_wb_toggle(hba, enable, QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005740 if (ret) {
Bean Huod3ba6222021-01-21 19:57:36 +01005741 dev_err(hba->dev, "%s WB-Buf Flush %s failed %d\n", __func__,
5742 enable ? "enable" : "disable", ret);
Yue Hu3b5f3c02021-03-18 17:55:36 +08005743 return;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005744 }
5745
Bean Huod3ba6222021-01-21 19:57:36 +01005746 hba->dev_info.wb_buf_flush_enabled = enable;
5747
Yue Hu3b5f3c02021-03-18 17:55:36 +08005748 dev_dbg(hba->dev, "%s WB-Buf Flush %s\n",
5749 __func__, enable ? "enabled" : "disabled");
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005750}
5751
5752static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba,
5753 u32 avail_buf)
5754{
5755 u32 cur_buf;
5756 int ret;
Stanley Chue31011a2020-05-22 16:32:11 +08005757 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005758
Stanley Chue31011a2020-05-22 16:32:11 +08005759 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005760 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5761 QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE,
Stanley Chue31011a2020-05-22 16:32:11 +08005762 index, 0, &cur_buf);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005763 if (ret) {
5764 dev_err(hba->dev, "%s dCurWriteBoosterBufferSize read failed %d\n",
5765 __func__, ret);
5766 return false;
5767 }
5768
5769 if (!cur_buf) {
5770 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n",
5771 cur_buf);
5772 return false;
5773 }
Stanley Chud14734ae2020-05-09 17:37:15 +08005774 /* Let it continue to flush when available buffer exceeds threshold */
5775 if (avail_buf < hba->vps->wb_flush_threshold)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005776 return true;
5777
5778 return false;
5779}
5780
Stanley Chu51dd9052020-05-22 16:32:12 +08005781static bool ufshcd_wb_need_flush(struct ufs_hba *hba)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005782{
5783 int ret;
5784 u32 avail_buf;
Stanley Chue31011a2020-05-22 16:32:11 +08005785 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005786
Stanley Chu79e35202020-05-08 16:01:15 +08005787 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005788 return false;
5789 /*
5790 * The ufs device needs the vcc to be ON to flush.
5791 * With user-space reduction enabled, it's enough to enable flush
5792 * by checking only the available buffer. The threshold
5793 * defined here is > 90% full.
5794 * With user-space preserved enabled, the current-buffer
5795 * should be checked too because the wb buffer size can reduce
5796 * when disk tends to be full. This info is provided by current
5797 * buffer (dCurrentWriteBoosterBufferSize). There's no point in
5798 * keeping vcc on when current buffer is empty.
5799 */
Stanley Chue31011a2020-05-22 16:32:11 +08005800 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005801 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5802 QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE,
Stanley Chue31011a2020-05-22 16:32:11 +08005803 index, 0, &avail_buf);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005804 if (ret) {
5805 dev_warn(hba->dev, "%s dAvailableWriteBoosterBufferSize read failed %d\n",
5806 __func__, ret);
5807 return false;
5808 }
5809
5810 if (!hba->dev_info.b_presrv_uspc_en) {
Stanley Chud14734ae2020-05-09 17:37:15 +08005811 if (avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005812 return true;
5813 return false;
5814 }
5815
5816 return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf);
5817}
5818
Stanley Chu51dd9052020-05-22 16:32:12 +08005819static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
5820{
5821 struct ufs_hba *hba = container_of(to_delayed_work(work),
5822 struct ufs_hba,
5823 rpm_dev_flush_recheck_work);
5824 /*
5825 * To prevent unnecessary VCC power drain after device finishes
5826 * WriteBooster buffer flush or Auto BKOPs, force runtime resume
5827 * after a certain delay to recheck the threshold by next runtime
5828 * suspend.
5829 */
Asutosh Dasb294ff32021-04-23 17:20:16 -07005830 ufshcd_rpm_get_sync(hba);
5831 ufshcd_rpm_put_sync(hba);
Stanley Chu51dd9052020-05-22 16:32:12 +08005832}
5833
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305834/**
5835 * ufshcd_exception_event_handler - handle exceptions raised by device
5836 * @work: pointer to work data
5837 *
5838 * Read bExceptionEventStatus attribute from the device and handle the
5839 * exception event accordingly.
5840 */
5841static void ufshcd_exception_event_handler(struct work_struct *work)
5842{
5843 struct ufs_hba *hba;
5844 int err;
5845 u32 status = 0;
5846 hba = container_of(work, struct ufs_hba, eeh_work);
5847
Stanley Chu03e1d282019-12-24 21:01:05 +08005848 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305849 err = ufshcd_get_ee_status(hba, &status);
5850 if (err) {
5851 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5852 __func__, err);
5853 goto out;
5854 }
5855
Adrian Hunterf7733622021-02-09 08:24:34 +02005856 trace_ufshcd_exception_event(dev_name(hba->dev), status);
5857
Adrian Huntercd469472021-02-09 08:24:36 +02005858 if (status & hba->ee_drv_mask & MASK_EE_URGENT_BKOPS)
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005859 ufshcd_bkops_exception_event_handler(hba);
5860
Avri Altman322c4b22021-09-15 09:04:07 +03005861 if (status & hba->ee_drv_mask & MASK_EE_URGENT_TEMP)
5862 ufshcd_temp_exception_event_handler(hba, status);
5863
Adrian Hunter7deedfd2021-02-09 08:24:37 +02005864 ufs_debugfs_exception_event(hba, status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305865out:
Stanley Chu03e1d282019-12-24 21:01:05 +08005866 ufshcd_scsi_unblock_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305867}
5868
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005869/* Complete requests that have door-bell cleared */
5870static void ufshcd_complete_requests(struct ufs_hba *hba)
5871{
Bart Van Assche11682522021-10-20 14:40:15 -07005872 ufshcd_transfer_req_compl(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005873 ufshcd_tmc_handler(hba);
5874}
5875
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305876/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005877 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5878 * to recover from the DL NAC errors or not.
5879 * @hba: per-adapter instance
5880 *
5881 * Returns true if error handling is required, false otherwise
5882 */
5883static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5884{
5885 unsigned long flags;
5886 bool err_handling = true;
5887
5888 spin_lock_irqsave(hba->host->host_lock, flags);
5889 /*
5890 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5891 * device fatal error and/or DL NAC & REPLAY timeout errors.
5892 */
5893 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5894 goto out;
5895
5896 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5897 ((hba->saved_err & UIC_ERROR) &&
5898 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
5899 goto out;
5900
5901 if ((hba->saved_err & UIC_ERROR) &&
5902 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5903 int err;
5904 /*
5905 * wait for 50ms to see if we can get any other errors or not.
5906 */
5907 spin_unlock_irqrestore(hba->host->host_lock, flags);
5908 msleep(50);
5909 spin_lock_irqsave(hba->host->host_lock, flags);
5910
5911 /*
5912 * now check if we have got any other severe errors other than
5913 * DL NAC error?
5914 */
5915 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5916 ((hba->saved_err & UIC_ERROR) &&
5917 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5918 goto out;
5919
5920 /*
5921 * As DL NAC is the only error received so far, send out NOP
5922 * command to confirm if link is still active or not.
5923 * - If we don't get any response then do error recovery.
5924 * - If we get response then clear the DL NAC error bit.
5925 */
5926
5927 spin_unlock_irqrestore(hba->host->host_lock, flags);
5928 err = ufshcd_verify_dev_init(hba);
5929 spin_lock_irqsave(hba->host->host_lock, flags);
5930
5931 if (err)
5932 goto out;
5933
5934 /* Link seems to be alive hence ignore the DL NAC errors */
5935 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5936 hba->saved_err &= ~UIC_ERROR;
5937 /* clear NAC error */
5938 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
Bean Huob0008622020-08-14 11:50:34 +02005939 if (!hba->saved_uic_err)
Yaniv Gardi583fa622016-03-10 17:37:13 +02005940 err_handling = false;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005941 }
5942out:
5943 spin_unlock_irqrestore(hba->host->host_lock, flags);
5944 return err_handling;
5945}
5946
Adrian Hunter88b099002021-09-17 17:43:49 +03005947/* host lock must be held before calling this func */
5948static inline bool ufshcd_is_saved_err_fatal(struct ufs_hba *hba)
5949{
5950 return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) ||
5951 (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK));
5952}
5953
Bart Van Assche267a59f2021-10-20 14:40:19 -07005954void ufshcd_schedule_eh_work(struct ufs_hba *hba)
Adrian Hunter88b099002021-09-17 17:43:49 +03005955{
Bart Van Assche267a59f2021-10-20 14:40:19 -07005956 lockdep_assert_held(hba->host->host_lock);
5957
Adrian Hunter88b099002021-09-17 17:43:49 +03005958 /* handle fatal errors only when link is not in error state */
5959 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) {
5960 if (hba->force_reset || ufshcd_is_link_broken(hba) ||
5961 ufshcd_is_saved_err_fatal(hba))
5962 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL;
5963 else
5964 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL;
5965 queue_work(hba->eh_wq, &hba->eh_work);
5966 }
5967}
5968
Stanley Chu348e1bc2021-01-20 23:01:42 +08005969static void ufshcd_clk_scaling_allow(struct ufs_hba *hba, bool allow)
5970{
5971 down_write(&hba->clk_scaling_lock);
5972 hba->clk_scaling.is_allowed = allow;
5973 up_write(&hba->clk_scaling_lock);
5974}
5975
5976static void ufshcd_clk_scaling_suspend(struct ufs_hba *hba, bool suspend)
5977{
5978 if (suspend) {
5979 if (hba->clk_scaling.is_enabled)
5980 ufshcd_suspend_clkscaling(hba);
5981 ufshcd_clk_scaling_allow(hba, false);
5982 } else {
5983 ufshcd_clk_scaling_allow(hba, true);
5984 if (hba->clk_scaling.is_enabled)
5985 ufshcd_resume_clkscaling(hba);
5986 }
5987}
5988
Can Guoc72e79c2020-08-09 05:15:52 -07005989static void ufshcd_err_handling_prepare(struct ufs_hba *hba)
5990{
Asutosh Dasb294ff32021-04-23 17:20:16 -07005991 ufshcd_rpm_get_sync(hba);
5992 if (pm_runtime_status_suspended(&hba->sdev_ufs_device->sdev_gendev) ||
5993 hba->is_sys_suspended) {
Can Guo88a92d62020-12-02 04:04:01 -08005994 enum ufs_pm_op pm_op;
5995
Can Guoc72e79c2020-08-09 05:15:52 -07005996 /*
Asutosh Dasb294ff32021-04-23 17:20:16 -07005997 * Don't assume anything of resume, if
Can Guoc72e79c2020-08-09 05:15:52 -07005998 * resume fails, irq and clocks can be OFF, and powers
5999 * can be OFF or in LPM.
6000 */
6001 ufshcd_setup_hba_vreg(hba, true);
6002 ufshcd_enable_irq(hba);
6003 ufshcd_setup_vreg(hba, true);
6004 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6005 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6006 ufshcd_hold(hba, false);
6007 if (!ufshcd_is_clkgating_allowed(hba))
6008 ufshcd_setup_clocks(hba, true);
6009 ufshcd_release(hba);
Can Guo88a92d62020-12-02 04:04:01 -08006010 pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM;
6011 ufshcd_vops_resume(hba, pm_op);
Can Guoc72e79c2020-08-09 05:15:52 -07006012 } else {
6013 ufshcd_hold(hba, false);
Stanley Chu348e1bc2021-01-20 23:01:42 +08006014 if (ufshcd_is_clkscaling_supported(hba) &&
6015 hba->clk_scaling.is_enabled)
Can Guoc72e79c2020-08-09 05:15:52 -07006016 ufshcd_suspend_clkscaling(hba);
Stanley Chu348e1bc2021-01-20 23:01:42 +08006017 ufshcd_clk_scaling_allow(hba, false);
Can Guoc72e79c2020-08-09 05:15:52 -07006018 }
Can Guoaa53f582021-02-23 21:36:47 -08006019 ufshcd_scsi_block_requests(hba);
6020 /* Drain ufshcd_queuecommand() */
Bart Van Assche5675c382021-12-03 15:19:48 -08006021 synchronize_rcu();
Can Guoaa53f582021-02-23 21:36:47 -08006022 cancel_work_sync(&hba->eeh_work);
Can Guoc72e79c2020-08-09 05:15:52 -07006023}
6024
6025static void ufshcd_err_handling_unprepare(struct ufs_hba *hba)
6026{
Can Guoaa53f582021-02-23 21:36:47 -08006027 ufshcd_scsi_unblock_requests(hba);
Can Guoc72e79c2020-08-09 05:15:52 -07006028 ufshcd_release(hba);
Stanley Chu348e1bc2021-01-20 23:01:42 +08006029 if (ufshcd_is_clkscaling_supported(hba))
6030 ufshcd_clk_scaling_suspend(hba, false);
Asutosh Dasb294ff32021-04-23 17:20:16 -07006031 ufshcd_rpm_put(hba);
Can Guoc72e79c2020-08-09 05:15:52 -07006032}
6033
6034static inline bool ufshcd_err_handling_should_stop(struct ufs_hba *hba)
6035{
Can Guo9cd20d32021-01-13 19:13:28 -08006036 return (!hba->is_powered || hba->shutting_down ||
Asutosh Dasb294ff32021-04-23 17:20:16 -07006037 !hba->sdev_ufs_device ||
Can Guo9cd20d32021-01-13 19:13:28 -08006038 hba->ufshcd_state == UFSHCD_STATE_ERROR ||
Can Guoc72e79c2020-08-09 05:15:52 -07006039 (!(hba->saved_err || hba->saved_uic_err || hba->force_reset ||
Can Guo9cd20d32021-01-13 19:13:28 -08006040 ufshcd_is_link_broken(hba))));
Can Guoc72e79c2020-08-09 05:15:52 -07006041}
6042
6043#ifdef CONFIG_PM
6044static void ufshcd_recover_pm_error(struct ufs_hba *hba)
6045{
6046 struct Scsi_Host *shost = hba->host;
6047 struct scsi_device *sdev;
6048 struct request_queue *q;
6049 int ret;
6050
Can Guo88a92d62020-12-02 04:04:01 -08006051 hba->is_sys_suspended = false;
Can Guoc72e79c2020-08-09 05:15:52 -07006052 /*
Asutosh Dasb294ff32021-04-23 17:20:16 -07006053 * Set RPM status of wlun device to RPM_ACTIVE,
Can Guoc72e79c2020-08-09 05:15:52 -07006054 * this also clears its runtime error.
6055 */
Asutosh Dasb294ff32021-04-23 17:20:16 -07006056 ret = pm_runtime_set_active(&hba->sdev_ufs_device->sdev_gendev);
6057
6058 /* hba device might have a runtime error otherwise */
6059 if (ret)
6060 ret = pm_runtime_set_active(hba->dev);
Can Guoc72e79c2020-08-09 05:15:52 -07006061 /*
Asutosh Dasb294ff32021-04-23 17:20:16 -07006062 * If wlun device had runtime error, we also need to resume those
6063 * consumer scsi devices in case any of them has failed to be
6064 * resumed due to supplier runtime resume failure. This is to unblock
Can Guoc72e79c2020-08-09 05:15:52 -07006065 * blk_queue_enter in case there are bios waiting inside it.
6066 */
6067 if (!ret) {
6068 shost_for_each_device(sdev, shost) {
6069 q = sdev->request_queue;
6070 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
6071 q->rpm_status == RPM_SUSPENDING))
6072 pm_request_resume(q->dev);
6073 }
6074 }
6075}
6076#else
6077static inline void ufshcd_recover_pm_error(struct ufs_hba *hba)
6078{
6079}
6080#endif
6081
Can Guo2355b662020-08-24 19:07:06 -07006082static bool ufshcd_is_pwr_mode_restore_needed(struct ufs_hba *hba)
6083{
6084 struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info;
6085 u32 mode;
6086
6087 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode);
6088
6089 if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK))
6090 return true;
6091
6092 if (pwr_info->pwr_tx != (mode & PWRMODE_MASK))
6093 return true;
6094
6095 return false;
6096}
6097
Yaniv Gardi583fa622016-03-10 17:37:13 +02006098/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306099 * ufshcd_err_handler - handle UFS errors that require s/w attention
Adrian Hunter88b099002021-09-17 17:43:49 +03006100 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306101 */
Adrian Hunter88b099002021-09-17 17:43:49 +03006102static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306103{
Adrian Hunter87bf6a62021-10-02 18:45:50 +03006104 int retries = MAX_ERR_HANDLER_RETRIES;
Adrian Hunter88b099002021-09-17 17:43:49 +03006105 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306106 unsigned long flags;
Adrian Hunter87bf6a62021-10-02 18:45:50 +03006107 bool needs_restore;
6108 bool needs_reset;
6109 bool err_xfer;
6110 bool err_tm;
6111 int pmc_err;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306112 int tag;
6113
Adrian Hunter88b099002021-09-17 17:43:49 +03006114 hba = container_of(work, struct ufs_hba, eh_work);
6115
Bart Van Assche4693fad2021-10-20 14:40:18 -07006116 dev_info(hba->dev,
6117 "%s started; HBA state %s; powered %d; shutting down %d; saved_err = %d; saved_uic_err = %d; force_reset = %d%s\n",
6118 __func__, ufshcd_state_name[hba->ufshcd_state],
6119 hba->is_powered, hba->shutting_down, hba->saved_err,
6120 hba->saved_uic_err, hba->force_reset,
6121 ufshcd_is_link_broken(hba) ? "; link is broken" : "");
6122
Can Guo9cd20d32021-01-13 19:13:28 -08006123 down(&hba->host_sem);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306124 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guoc72e79c2020-08-09 05:15:52 -07006125 if (ufshcd_err_handling_should_stop(hba)) {
Can Guo4db7a232020-08-09 05:15:51 -07006126 if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
6127 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6128 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guo9cd20d32021-01-13 19:13:28 -08006129 up(&hba->host_sem);
Can Guo4db7a232020-08-09 05:15:51 -07006130 return;
6131 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306132 ufshcd_set_eh_in_progress(hba);
Can Guo4db7a232020-08-09 05:15:51 -07006133 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guoc72e79c2020-08-09 05:15:52 -07006134 ufshcd_err_handling_prepare(hba);
Can Guoa45f9372021-05-24 01:36:57 -07006135 /* Complete requests that have door-bell cleared by h/w */
6136 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306137 spin_lock_irqsave(hba->host->host_lock, flags);
Adrian Hunter87bf6a62021-10-02 18:45:50 +03006138again:
6139 needs_restore = false;
6140 needs_reset = false;
6141 err_xfer = false;
6142 err_tm = false;
6143
Can Guoaa53f582021-02-23 21:36:47 -08006144 if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
6145 hba->ufshcd_state = UFSHCD_STATE_RESET;
Can Guo88a92d62020-12-02 04:04:01 -08006146 /*
6147 * A full reset and restore might have happened after preparation
6148 * is finished, double check whether we should stop.
6149 */
6150 if (ufshcd_err_handling_should_stop(hba))
6151 goto skip_err_handling;
6152
Yaniv Gardi583fa622016-03-10 17:37:13 +02006153 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
6154 bool ret;
6155
6156 spin_unlock_irqrestore(hba->host->host_lock, flags);
6157 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
6158 ret = ufshcd_quirk_dl_nac_errors(hba);
6159 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo88a92d62020-12-02 04:04:01 -08006160 if (!ret && ufshcd_err_handling_should_stop(hba))
Yaniv Gardi583fa622016-03-10 17:37:13 +02006161 goto skip_err_handling;
6162 }
Can Guo4db7a232020-08-09 05:15:51 -07006163
Can Guo2355b662020-08-24 19:07:06 -07006164 if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
6165 (hba->saved_uic_err &&
6166 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
Can Guoc3be8d1e2020-08-09 05:15:53 -07006167 bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR);
6168
6169 spin_unlock_irqrestore(hba->host->host_lock, flags);
6170 ufshcd_print_host_state(hba);
6171 ufshcd_print_pwr_info(hba);
Stanley Chue965e5e2020-12-05 19:58:59 +08006172 ufshcd_print_evt_hist(hba);
Can Guoc3be8d1e2020-08-09 05:15:53 -07006173 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
6174 ufshcd_print_trs(hba, hba->outstanding_reqs, pr_prdt);
6175 spin_lock_irqsave(hba->host->host_lock, flags);
6176 }
6177
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006178 /*
6179 * if host reset is required then skip clearing the pending
Can Guo2df74b62019-11-25 22:53:33 -08006180 * transfers forcefully because they will get cleared during
6181 * host reset and restore
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006182 */
Can Guo88a92d62020-12-02 04:04:01 -08006183 if (hba->force_reset || ufshcd_is_link_broken(hba) ||
6184 ufshcd_is_saved_err_fatal(hba) ||
6185 ((hba->saved_err & UIC_ERROR) &&
6186 (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
6187 UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) {
6188 needs_reset = true;
Can Guo2355b662020-08-24 19:07:06 -07006189 goto do_reset;
Can Guo88a92d62020-12-02 04:04:01 -08006190 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006191
Can Guo2355b662020-08-24 19:07:06 -07006192 /*
6193 * If LINERESET was caught, UFS might have been put to PWM mode,
6194 * check if power mode restore is needed.
6195 */
6196 if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) {
6197 hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
6198 if (!hba->saved_uic_err)
6199 hba->saved_err &= ~UIC_ERROR;
6200 spin_unlock_irqrestore(hba->host->host_lock, flags);
6201 if (ufshcd_is_pwr_mode_restore_needed(hba))
6202 needs_restore = true;
6203 spin_lock_irqsave(hba->host->host_lock, flags);
6204 if (!hba->saved_err && !needs_restore)
6205 goto skip_err_handling;
6206 }
6207
6208 hba->silence_err_logs = true;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006209 /* release lock as clear command might sleep */
6210 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306211 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006212 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
Can Guo307348f2020-08-24 19:07:05 -07006213 if (ufshcd_try_to_abort_task(hba, tag)) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006214 err_xfer = true;
6215 goto lock_skip_pending_xfer_clear;
6216 }
Bart Van Assche4693fad2021-10-20 14:40:18 -07006217 dev_err(hba->dev, "Aborted tag %d / CDB %#02x\n", tag,
6218 hba->lrb[tag].cmd ? hba->lrb[tag].cmd->cmnd[0] : -1);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006219 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306220
6221 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006222 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
6223 if (ufshcd_clear_tm_cmd(hba, tag)) {
6224 err_tm = true;
6225 goto lock_skip_pending_xfer_clear;
6226 }
6227 }
6228
6229lock_skip_pending_xfer_clear:
Bart Van Assche11682522021-10-20 14:40:15 -07006230 /* Complete the requests that are cleared by s/w */
6231 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306232
Can Guoa45f9372021-05-24 01:36:57 -07006233 spin_lock_irqsave(hba->host->host_lock, flags);
6234 hba->silence_err_logs = false;
Can Guo2355b662020-08-24 19:07:06 -07006235 if (err_xfer || err_tm) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006236 needs_reset = true;
Can Guo2355b662020-08-24 19:07:06 -07006237 goto do_reset;
6238 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006239
Can Guo2355b662020-08-24 19:07:06 -07006240 /*
6241 * After all reqs and tasks are cleared from doorbell,
6242 * now it is safe to retore power mode.
6243 */
6244 if (needs_restore) {
6245 spin_unlock_irqrestore(hba->host->host_lock, flags);
6246 /*
6247 * Hold the scaling lock just in case dev cmds
6248 * are sent via bsg and/or sysfs.
6249 */
6250 down_write(&hba->clk_scaling_lock);
6251 hba->force_pmc = true;
6252 pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info));
6253 if (pmc_err) {
6254 needs_reset = true;
6255 dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n",
6256 __func__, pmc_err);
6257 }
6258 hba->force_pmc = false;
6259 ufshcd_print_pwr_info(hba);
6260 up_write(&hba->clk_scaling_lock);
6261 spin_lock_irqsave(hba->host->host_lock, flags);
6262 }
6263
6264do_reset:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306265 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006266 if (needs_reset) {
Adrian Hunter87bf6a62021-10-02 18:45:50 +03006267 int err;
6268
Can Guo4db7a232020-08-09 05:15:51 -07006269 hba->force_reset = false;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006270 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306271 err = ufshcd_reset_and_restore(hba);
Can Guo4db7a232020-08-09 05:15:51 -07006272 if (err)
6273 dev_err(hba->dev, "%s: reset and restore failed with err %d\n",
6274 __func__, err);
Can Guoc72e79c2020-08-09 05:15:52 -07006275 else
6276 ufshcd_recover_pm_error(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006277 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306278 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006279
Yaniv Gardi583fa622016-03-10 17:37:13 +02006280skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006281 if (!needs_reset) {
Can Guo4db7a232020-08-09 05:15:51 -07006282 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
6283 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006284 if (hba->saved_err || hba->saved_uic_err)
6285 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
6286 __func__, hba->saved_err, hba->saved_uic_err);
6287 }
Adrian Hunter87bf6a62021-10-02 18:45:50 +03006288 /* Exit in an operational state or dead */
6289 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL &&
6290 hba->ufshcd_state != UFSHCD_STATE_ERROR) {
6291 if (--retries)
6292 goto again;
6293 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6294 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306295 ufshcd_clear_eh_in_progress(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006296 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guoc72e79c2020-08-09 05:15:52 -07006297 ufshcd_err_handling_unprepare(hba);
Can Guo9cd20d32021-01-13 19:13:28 -08006298 up(&hba->host_sem);
Bart Van Assche4693fad2021-10-20 14:40:18 -07006299
6300 dev_info(hba->dev, "%s finished; HBA state %s\n", __func__,
6301 ufshcd_state_name[hba->ufshcd_state]);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306302}
6303
6304/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306305 * ufshcd_update_uic_error - check and set fatal UIC error flags.
6306 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006307 *
6308 * Returns
6309 * IRQ_HANDLED - If interrupt is valid
6310 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306311 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006312static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306313{
6314 u32 reg;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006315 irqreturn_t retval = IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306316
Can Guo2355b662020-08-24 19:07:06 -07006317 /* PHY layer error */
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08006318 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08006319 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Can Guo2355b662020-08-24 19:07:06 -07006320 (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006321 ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg);
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08006322 /*
6323 * To know whether this error is fatal or not, DB timeout
6324 * must be checked but this error is handled separately.
6325 */
Can Guo2355b662020-08-24 19:07:06 -07006326 if (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)
6327 dev_dbg(hba->dev, "%s: UIC Lane error reported\n",
6328 __func__);
6329
6330 /* Got a LINERESET indication. */
6331 if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) {
6332 struct uic_command *cmd = NULL;
6333
6334 hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR;
6335 if (hba->uic_async_done && hba->active_uic_cmd)
6336 cmd = hba->active_uic_cmd;
6337 /*
6338 * Ignore the LINERESET during power mode change
6339 * operation via DME_SET command.
6340 */
6341 if (cmd && (cmd->command == UIC_CMD_DME_SET))
6342 hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
6343 }
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006344 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006345 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08006346
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306347 /* PA_INIT_ERROR is fatal and needs UIC reset */
6348 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006349 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
6350 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006351 ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006352
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006353 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
6354 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
6355 else if (hba->dev_quirks &
6356 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
6357 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
6358 hba->uic_error |=
6359 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
6360 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
6361 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
6362 }
6363 retval |= IRQ_HANDLED;
Yaniv Gardi583fa622016-03-10 17:37:13 +02006364 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306365
6366 /* UIC NL/TL/DME errors needs software retry */
6367 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006368 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
6369 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006370 ufshcd_update_evt_hist(hba, UFS_EVT_NL_ERR, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306371 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006372 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006373 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306374
6375 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006376 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
6377 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006378 ufshcd_update_evt_hist(hba, UFS_EVT_TL_ERR, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306379 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006380 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006381 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306382
6383 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006384 if ((reg & UIC_DME_ERROR) &&
6385 (reg & UIC_DME_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006386 ufshcd_update_evt_hist(hba, UFS_EVT_DME_ERR, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306387 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006388 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006389 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306390
6391 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
6392 __func__, hba->uic_error);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006393 return retval;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306394}
6395
6396/**
6397 * ufshcd_check_errors - Check for errors that need s/w attention
6398 * @hba: per-adapter instance
Can Guoa45f9372021-05-24 01:36:57 -07006399 * @intr_status: interrupt status generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006400 *
6401 * Returns
6402 * IRQ_HANDLED - If interrupt is valid
6403 * IRQ_NONE - If invalid interrupt
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306404 */
Can Guoa45f9372021-05-24 01:36:57 -07006405static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba, u32 intr_status)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306406{
6407 bool queue_eh_work = false;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006408 irqreturn_t retval = IRQ_NONE;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306409
Can Guoa45f9372021-05-24 01:36:57 -07006410 spin_lock(hba->host->host_lock);
6411 hba->errors |= UFSHCD_ERROR_MASK & intr_status;
6412
Stanley Chud3c615b2019-07-10 21:38:19 +08006413 if (hba->errors & INT_FATAL_ERRORS) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006414 ufshcd_update_evt_hist(hba, UFS_EVT_FATAL_ERR,
6415 hba->errors);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306416 queue_eh_work = true;
Stanley Chud3c615b2019-07-10 21:38:19 +08006417 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306418
6419 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306420 hba->uic_error = 0;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006421 retval = ufshcd_update_uic_error(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306422 if (hba->uic_error)
6423 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306424 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306425
Stanley Chu82174442019-05-21 14:44:54 +08006426 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
6427 dev_err(hba->dev,
6428 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
6429 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
6430 "Enter" : "Exit",
6431 hba->errors, ufshcd_get_upmcrs(hba));
Stanley Chue965e5e2020-12-05 19:58:59 +08006432 ufshcd_update_evt_hist(hba, UFS_EVT_AUTO_HIBERN8_ERR,
Stanley Chud3c615b2019-07-10 21:38:19 +08006433 hba->errors);
Can Guo4db7a232020-08-09 05:15:51 -07006434 ufshcd_set_link_broken(hba);
Stanley Chu82174442019-05-21 14:44:54 +08006435 queue_eh_work = true;
6436 }
6437
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306438 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006439 /*
6440 * update the transfer error masks to sticky bits, let's do this
6441 * irrespective of current ufshcd_state.
6442 */
6443 hba->saved_err |= hba->errors;
6444 hba->saved_uic_err |= hba->uic_error;
6445
Can Guo4db7a232020-08-09 05:15:51 -07006446 /* dump controller state before resetting */
Can Guoace38042020-12-02 04:04:03 -08006447 if ((hba->saved_err &
6448 (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
Can Guo2355b662020-08-24 19:07:06 -07006449 (hba->saved_uic_err &&
6450 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
Can Guo4db7a232020-08-09 05:15:51 -07006451 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
Dolev Raviv66cc8202016-12-22 18:39:42 -08006452 __func__, hba->saved_err,
6453 hba->saved_uic_err);
Can Guoc3be8d1e2020-08-09 05:15:53 -07006454 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE,
6455 "host_regs: ");
Can Guo4db7a232020-08-09 05:15:51 -07006456 ufshcd_print_pwr_info(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306457 }
Adrian Hunter88b099002021-09-17 17:43:49 +03006458 ufshcd_schedule_eh_work(hba);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006459 retval |= IRQ_HANDLED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306460 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306461 /*
6462 * if (!queue_eh_work) -
6463 * Other errors are either non-fatal where host recovers
6464 * itself without s/w intervention or errors that will be
6465 * handled by the SCSI core layer.
6466 */
Can Guoa45f9372021-05-24 01:36:57 -07006467 hba->errors = 0;
6468 hba->uic_error = 0;
6469 spin_unlock(hba->host->host_lock);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006470 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306471}
6472
6473/**
6474 * ufshcd_tmc_handler - handle task management function completion
6475 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006476 *
6477 * Returns
6478 * IRQ_HANDLED - If interrupt is valid
6479 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306480 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006481static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306482{
Adrian Hunterf5ef3362021-09-22 12:10:59 +03006483 unsigned long flags, pending, issued;
6484 irqreturn_t ret = IRQ_NONE;
6485 int tag;
6486
Can Guoa45f9372021-05-24 01:36:57 -07006487 spin_lock_irqsave(hba->host->host_lock, flags);
Adrian Hunter5cb37a22021-11-08 08:48:15 +02006488 pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Adrian Hunterf5ef3362021-09-22 12:10:59 +03006489 issued = hba->outstanding_tasks & ~pending;
6490 for_each_set_bit(tag, &issued, hba->nutmrs) {
6491 struct request *req = hba->tmf_rqs[tag];
6492 struct completion *c = req->end_io_data;
6493
6494 complete(c);
6495 ret = IRQ_HANDLED;
6496 }
Can Guoa45f9372021-05-24 01:36:57 -07006497 spin_unlock_irqrestore(hba->host->host_lock, flags);
6498
Adrian Hunterf5ef3362021-09-22 12:10:59 +03006499 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306500}
6501
6502/**
6503 * ufshcd_sl_intr - Interrupt service routine
6504 * @hba: per adapter instance
6505 * @intr_status: contains interrupts generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006506 *
6507 * Returns
6508 * IRQ_HANDLED - If interrupt is valid
6509 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306510 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006511static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306512{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006513 irqreturn_t retval = IRQ_NONE;
6514
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05306515 if (intr_status & UFSHCD_UIC_MASK)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006516 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306517
Can Guoa45f9372021-05-24 01:36:57 -07006518 if (intr_status & UFSHCD_ERROR_MASK || hba->errors)
6519 retval |= ufshcd_check_errors(hba, intr_status);
6520
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306521 if (intr_status & UTP_TASK_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006522 retval |= ufshcd_tmc_handler(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306523
6524 if (intr_status & UTP_TRANSFER_REQ_COMPL)
Bart Van Assche11682522021-10-20 14:40:15 -07006525 retval |= ufshcd_transfer_req_compl(hba);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006526
6527 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306528}
6529
6530/**
6531 * ufshcd_intr - Main interrupt service routine
6532 * @irq: irq number
6533 * @__hba: pointer to adapter instance
6534 *
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006535 * Returns
6536 * IRQ_HANDLED - If interrupt is valid
6537 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306538 */
6539static irqreturn_t ufshcd_intr(int irq, void *__hba)
6540{
Adrian Hunter127d5f72020-08-11 16:39:36 +03006541 u32 intr_status, enabled_intr_status = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306542 irqreturn_t retval = IRQ_NONE;
6543 struct ufs_hba *hba = __hba;
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306544 int retries = hba->nutrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306545
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306546 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Can Guo3f8af602020-08-09 05:15:50 -07006547 hba->ufs_stats.last_intr_status = intr_status;
6548 hba->ufs_stats.last_intr_ts = ktime_get();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306549
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306550 /*
6551 * There could be max of hba->nutrs reqs in flight and in worst case
6552 * if the reqs get finished 1 by 1 after the interrupt status is
6553 * read, make sure we handle them by checking the interrupt status
6554 * again in a loop until we process all of the reqs before returning.
6555 */
Adrian Hunter127d5f72020-08-11 16:39:36 +03006556 while (intr_status && retries--) {
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306557 enabled_intr_status =
6558 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Bean Huo60ec3752021-01-18 21:12:33 +01006559 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006560 if (enabled_intr_status)
6561 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02006562
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306563 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Adrian Hunter127d5f72020-08-11 16:39:36 +03006564 }
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306565
Jaegeuk Kimeeb1b552021-01-07 10:53:16 -08006566 if (enabled_intr_status && retval == IRQ_NONE &&
Bart Van Assche40d2fd02021-05-19 13:20:57 -07006567 (!(enabled_intr_status & UTP_TRANSFER_REQ_COMPL) ||
6568 hba->outstanding_reqs) && !ufshcd_eh_in_progress(hba)) {
Jaegeuk Kimeeb1b552021-01-07 10:53:16 -08006569 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x (0x%08x, 0x%08x)\n",
6570 __func__,
6571 intr_status,
6572 hba->ufs_stats.last_intr_status,
6573 enabled_intr_status);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006574 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
6575 }
6576
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306577 return retval;
6578}
6579
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306580static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
6581{
6582 int err = 0;
6583 u32 mask = 1 << tag;
6584 unsigned long flags;
6585
6586 if (!test_bit(tag, &hba->outstanding_tasks))
6587 goto out;
6588
6589 spin_lock_irqsave(hba->host->host_lock, flags);
Alim Akhtar1399c5b2018-05-06 15:44:15 +05306590 ufshcd_utmrl_clear(hba, tag);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306591 spin_unlock_irqrestore(hba->host->host_lock, flags);
6592
6593 /* poll for max. 1 sec to clear door bell register by h/w */
6594 err = ufshcd_wait_for_register(hba,
6595 REG_UTP_TASK_REQ_DOOR_BELL,
Bart Van Assche5cac1092020-05-07 15:27:50 -07006596 mask, 0, 1000, 1000);
Bart Van Assche4693fad2021-10-20 14:40:18 -07006597
6598 dev_err(hba->dev, "Clearing task management function with tag %d %s\n",
6599 tag, err ? "succeeded" : "failed");
6600
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306601out:
6602 return err;
6603}
6604
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006605static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
6606 struct utp_task_req_desc *treq, u8 tm_function)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306607{
Bart Van Assche69a6c262019-12-09 10:13:09 -08006608 struct request_queue *q = hba->tmf_queue;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006609 struct Scsi_Host *host = hba->host;
Bart Van Assche69a6c262019-12-09 10:13:09 -08006610 DECLARE_COMPLETION_ONSTACK(wait);
6611 struct request *req;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306612 unsigned long flags;
Can Guo4b42d5572021-04-01 00:39:09 -07006613 int task_tag, err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306614
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306615 /*
Christoph Hellwig0bf6d962021-10-25 09:05:07 +02006616 * blk_mq_alloc_request() is used here only to get a free tag.
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306617 */
Christoph Hellwig0bf6d962021-10-25 09:05:07 +02006618 req = blk_mq_alloc_request(q, REQ_OP_DRV_OUT, 0);
Jaegeuk Kimeeb1b552021-01-07 10:53:16 -08006619 if (IS_ERR(req))
6620 return PTR_ERR(req);
6621
Bart Van Assche69a6c262019-12-09 10:13:09 -08006622 req->end_io_data = &wait;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006623 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306624
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306625 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306626
Can Guo4b42d5572021-04-01 00:39:09 -07006627 task_tag = req->tag;
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08006628 WARN_ONCE(task_tag < 0 || task_tag >= hba->nutmrs, "Invalid tag %d\n",
6629 task_tag);
Adrian Hunterf5ef3362021-09-22 12:10:59 +03006630 hba->tmf_rqs[req->tag] = req;
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -05006631 treq->upiu_req.req_header.dword_0 |= cpu_to_be32(task_tag);
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006632
Can Guo4b42d5572021-04-01 00:39:09 -07006633 memcpy(hba->utmrdl_base_addr + task_tag, treq, sizeof(*treq));
6634 ufshcd_vops_setup_task_mgmt(hba, task_tag, tm_function);
Kiwoong Kimd2877be2016-11-10 21:16:15 +09006635
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306636 /* send command to the controller */
Can Guo4b42d5572021-04-01 00:39:09 -07006637 __set_bit(task_tag, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02006638
Can Guo4b42d5572021-04-01 00:39:09 -07006639 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07006640 /* Make sure that doorbell is committed immediately */
6641 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306642
6643 spin_unlock_irqrestore(host->host_lock, flags);
6644
Bean Huo28fa68f2021-01-05 12:34:42 +01006645 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_SEND);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03006646
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306647 /* wait until the task management command is completed */
Bart Van Assche69a6c262019-12-09 10:13:09 -08006648 err = wait_for_completion_io_timeout(&wait,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306649 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306650 if (!err) {
Bean Huo28fa68f2021-01-05 12:34:42 +01006651 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306652 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6653 __func__, tm_function);
Can Guo4b42d5572021-04-01 00:39:09 -07006654 if (ufshcd_clear_tm_cmd(hba, task_tag))
6655 dev_WARN(hba->dev, "%s: unable to clear tm cmd (slot %d) after timeout\n",
6656 __func__, task_tag);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306657 err = -ETIMEDOUT;
6658 } else {
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006659 err = 0;
Can Guo4b42d5572021-04-01 00:39:09 -07006660 memcpy(treq, hba->utmrdl_base_addr + task_tag, sizeof(*treq));
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006661
Bean Huo28fa68f2021-01-05 12:34:42 +01006662 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_COMP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306663 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306664
Stanley Chub5572172019-08-19 21:43:28 +08006665 spin_lock_irqsave(hba->host->host_lock, flags);
Adrian Hunterf5ef3362021-09-22 12:10:59 +03006666 hba->tmf_rqs[req->tag] = NULL;
Can Guo4b42d5572021-04-01 00:39:09 -07006667 __clear_bit(task_tag, &hba->outstanding_tasks);
Stanley Chub5572172019-08-19 21:43:28 +08006668 spin_unlock_irqrestore(hba->host->host_lock, flags);
6669
Can Guo4b42d5572021-04-01 00:39:09 -07006670 ufshcd_release(hba);
Christoph Hellwig0bf6d962021-10-25 09:05:07 +02006671 blk_mq_free_request(req);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306672
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306673 return err;
6674}
6675
6676/**
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006677 * ufshcd_issue_tm_cmd - issues task management commands to controller
6678 * @hba: per adapter instance
6679 * @lun_id: LUN ID to which TM command is sent
6680 * @task_id: task ID to which the TM command is applicable
6681 * @tm_function: task management function opcode
6682 * @tm_response: task management service response return value
6683 *
6684 * Returns non-zero value on error, zero on success.
6685 */
6686static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
6687 u8 tm_function, u8 *tm_response)
6688{
6689 struct utp_task_req_desc treq = { { 0 }, };
Bart Van Assche957d63e2021-10-20 14:40:17 -07006690 enum utp_ocs ocs_value;
6691 int err;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006692
6693 /* Configure task request descriptor */
6694 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6695 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6696
6697 /* Configure task request UPIU */
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -05006698 treq.upiu_req.req_header.dword_0 = cpu_to_be32(lun_id << 8) |
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006699 cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24);
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -05006700 treq.upiu_req.req_header.dword_1 = cpu_to_be32(tm_function << 16);
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006701
6702 /*
6703 * The host shall provide the same value for LUN field in the basic
6704 * header and for Input Parameter.
6705 */
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -05006706 treq.upiu_req.input_param1 = cpu_to_be32(lun_id);
6707 treq.upiu_req.input_param2 = cpu_to_be32(task_id);
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006708
6709 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
6710 if (err == -ETIMEDOUT)
6711 return err;
6712
6713 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6714 if (ocs_value != OCS_SUCCESS)
6715 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
6716 __func__, ocs_value);
6717 else if (tm_response)
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -05006718 *tm_response = be32_to_cpu(treq.upiu_rsp.output_param1) &
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006719 MASK_TM_SERVICE_RESP;
6720 return err;
6721}
6722
6723/**
Avri Altman5e0a86e2018-10-07 17:30:37 +03006724 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
6725 * @hba: per-adapter instance
6726 * @req_upiu: upiu request
6727 * @rsp_upiu: upiu reply
Avri Altman5e0a86e2018-10-07 17:30:37 +03006728 * @desc_buff: pointer to descriptor buffer, NULL if NA
6729 * @buff_len: descriptor size, 0 if NA
Bart Van Assched0e97602019-10-29 16:07:08 -07006730 * @cmd_type: specifies the type (NOP, Query...)
Avri Altman5e0a86e2018-10-07 17:30:37 +03006731 * @desc_op: descriptor operation
6732 *
6733 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
6734 * Therefore, it "rides" the device management infrastructure: uses its tag and
6735 * tasks work queues.
6736 *
6737 * Since there is only one available tag for device management commands,
6738 * the caller is expected to hold the hba->dev_cmd.lock mutex.
6739 */
6740static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
6741 struct utp_upiu_req *req_upiu,
6742 struct utp_upiu_req *rsp_upiu,
6743 u8 *desc_buff, int *buff_len,
Bart Van Assche7f674c32019-10-29 16:07:09 -07006744 enum dev_cmd_type cmd_type,
Avri Altman5e0a86e2018-10-07 17:30:37 +03006745 enum query_opcode desc_op)
6746{
Bart Van Assche8a686f22021-07-21 20:34:26 -07006747 DECLARE_COMPLETION_ONSTACK(wait);
Bart Van Assche945c3cc2021-12-03 15:19:42 -08006748 const u32 tag = hba->reserved_slot;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006749 struct ufshcd_lrb *lrbp;
6750 int err = 0;
Bean Huoa23064c2020-07-06 14:39:36 +02006751 u8 upiu_flags;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006752
Bart Van Assche945c3cc2021-12-03 15:19:42 -08006753 /* Protects use of hba->reserved_slot. */
6754 lockdep_assert_held(&hba->dev_cmd.lock);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006755
Bart Van Assche945c3cc2021-12-03 15:19:42 -08006756 down_read(&hba->clk_scaling_lock);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006757
Can Guoa45f9372021-05-24 01:36:57 -07006758 lrbp = &hba->lrb[tag];
Can Guo7a7e66c2020-12-02 04:04:02 -08006759 WARN_ON(lrbp->cmd);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006760 lrbp->cmd = NULL;
6761 lrbp->sense_bufflen = 0;
6762 lrbp->sense_buffer = NULL;
6763 lrbp->task_tag = tag;
6764 lrbp->lun = 0;
6765 lrbp->intr_cmd = true;
Satya Tangiraladf043c742020-07-06 20:04:14 +00006766 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006767 hba->dev_cmd.type = cmd_type;
6768
Caleb Connolly51428812021-03-10 15:33:42 +00006769 if (hba->ufs_version <= ufshci_version(1, 1))
Avri Altman5e0a86e2018-10-07 17:30:37 +03006770 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
Caleb Connolly51428812021-03-10 15:33:42 +00006771 else
Avri Altman5e0a86e2018-10-07 17:30:37 +03006772 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006773
6774 /* update the task tag in the request upiu */
6775 req_upiu->header.dword_0 |= cpu_to_be32(tag);
6776
6777 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
6778
6779 /* just copy the upiu request as it is */
6780 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
6781 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
6782 /* The Data Segment Area is optional depending upon the query
6783 * function value. for WRITE DESCRIPTOR, the data segment
6784 * follows right after the tsf.
6785 */
6786 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
6787 *buff_len = 0;
6788 }
6789
6790 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
6791
6792 hba->dev_cmd.complete = &wait;
6793
Bean Huo10542482021-05-31 12:43:08 +02006794 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006795
Can Guoa45f9372021-05-24 01:36:57 -07006796 ufshcd_send_command(hba, tag);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006797 /*
6798 * ignore the returning value here - ufshcd_check_query_response is
6799 * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
6800 * read the response directly ignoring all errors.
6801 */
6802 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT);
6803
6804 /* just copy the upiu response as it is */
6805 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
Avri Altman4bbbe242019-02-20 09:11:13 +02006806 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
6807 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
6808 u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
6809 MASK_QUERY_DATA_SEG_LEN;
6810
6811 if (*buff_len >= resp_len) {
6812 memcpy(desc_buff, descp, resp_len);
6813 *buff_len = resp_len;
6814 } else {
Bean Huo3d4881d2019-11-12 23:34:35 +01006815 dev_warn(hba->dev,
6816 "%s: rsp size %d is bigger than buffer size %d",
6817 __func__, resp_len, *buff_len);
Avri Altman4bbbe242019-02-20 09:11:13 +02006818 *buff_len = 0;
6819 err = -EINVAL;
6820 }
6821 }
Bean Huo10542482021-05-31 12:43:08 +02006822 ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP,
6823 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006824
Avri Altman5e0a86e2018-10-07 17:30:37 +03006825 up_read(&hba->clk_scaling_lock);
6826 return err;
6827}
6828
6829/**
6830 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
6831 * @hba: per-adapter instance
6832 * @req_upiu: upiu request
6833 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
6834 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
6835 * @desc_buff: pointer to descriptor buffer, NULL if NA
6836 * @buff_len: descriptor size, 0 if NA
6837 * @desc_op: descriptor operation
6838 *
6839 * Supports UTP Transfer requests (nop and query), and UTP Task
6840 * Management requests.
6841 * It is up to the caller to fill the upiu conent properly, as it will
6842 * be copied without any further input validations.
6843 */
6844int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
6845 struct utp_upiu_req *req_upiu,
6846 struct utp_upiu_req *rsp_upiu,
6847 int msgcode,
6848 u8 *desc_buff, int *buff_len,
6849 enum query_opcode desc_op)
6850{
6851 int err;
Bart Van Assche7f674c32019-10-29 16:07:09 -07006852 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006853 struct utp_task_req_desc treq = { { 0 }, };
Bart Van Assche957d63e2021-10-20 14:40:17 -07006854 enum utp_ocs ocs_value;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006855 u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC;
6856
Avri Altman5e0a86e2018-10-07 17:30:37 +03006857 switch (msgcode) {
6858 case UPIU_TRANSACTION_NOP_OUT:
6859 cmd_type = DEV_CMD_TYPE_NOP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006860 fallthrough;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006861 case UPIU_TRANSACTION_QUERY_REQ:
6862 ufshcd_hold(hba, false);
6863 mutex_lock(&hba->dev_cmd.lock);
6864 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
6865 desc_buff, buff_len,
6866 cmd_type, desc_op);
6867 mutex_unlock(&hba->dev_cmd.lock);
6868 ufshcd_release(hba);
6869
6870 break;
6871 case UPIU_TRANSACTION_TASK_REQ:
6872 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6873 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6874
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -05006875 memcpy(&treq.upiu_req, req_upiu, sizeof(*req_upiu));
Avri Altman5e0a86e2018-10-07 17:30:37 +03006876
6877 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
6878 if (err == -ETIMEDOUT)
6879 break;
6880
6881 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6882 if (ocs_value != OCS_SUCCESS) {
6883 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
6884 ocs_value);
6885 break;
6886 }
6887
Gustavo A. R. Silva1352eec2021-03-31 17:43:38 -05006888 memcpy(rsp_upiu, &treq.upiu_rsp, sizeof(*rsp_upiu));
Avri Altman5e0a86e2018-10-07 17:30:37 +03006889
6890 break;
6891 default:
6892 err = -EINVAL;
6893
6894 break;
6895 }
6896
Avri Altman5e0a86e2018-10-07 17:30:37 +03006897 return err;
6898}
6899
6900/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306901 * ufshcd_eh_device_reset_handler - device reset handler registered to
6902 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306903 * @cmd: SCSI command pointer
6904 *
6905 * Returns SUCCESS/FAILED
6906 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306907static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306908{
6909 struct Scsi_Host *host;
6910 struct ufs_hba *hba;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306911 u32 pos;
6912 int err;
Can Guo35fc4cd2020-12-28 04:04:36 -08006913 u8 resp = 0xF, lun;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306914
6915 host = cmd->device->host;
6916 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306917
Can Guo35fc4cd2020-12-28 04:04:36 -08006918 lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
6919 err = ufshcd_issue_tm_cmd(hba, lun, 0, UFS_LOGICAL_RESET, &resp);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306920 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306921 if (!err)
6922 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306923 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306924 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306925
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306926 /* clear the commands that were pending for corresponding LUN */
6927 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
Can Guo35fc4cd2020-12-28 04:04:36 -08006928 if (hba->lrb[pos].lun == lun) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306929 err = ufshcd_clear_cmd(hba, pos);
6930 if (err)
6931 break;
Bart Van Assche11682522021-10-20 14:40:15 -07006932 __ufshcd_transfer_req_compl(hba, 1U << pos);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306933 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306934 }
Gilad Broner7fabb772017-02-03 16:56:50 -08006935
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306936out:
Gilad Broner7fabb772017-02-03 16:56:50 -08006937 hba->req_abort_count = 0;
Stanley Chue965e5e2020-12-05 19:58:59 +08006938 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306939 if (!err) {
6940 err = SUCCESS;
6941 } else {
6942 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6943 err = FAILED;
6944 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306945 return err;
6946}
6947
Gilad Bronere0b299e2017-02-03 16:56:40 -08006948static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6949{
6950 struct ufshcd_lrb *lrbp;
6951 int tag;
6952
6953 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6954 lrbp = &hba->lrb[tag];
6955 lrbp->req_abort_skip = true;
6956 }
6957}
6958
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306959/**
Can Guo307348f2020-08-24 19:07:05 -07006960 * ufshcd_try_to_abort_task - abort a specific task
Lee Jonesd23ec0b2020-11-02 14:23:51 +00006961 * @hba: Pointer to adapter instance
6962 * @tag: Task tag/index to be aborted
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306963 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306964 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6965 * command, and in host controller by clearing the door-bell register. There can
6966 * be race between controller sending the command to the device while abort is
6967 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6968 * really issued and then try to abort it.
6969 *
Can Guo307348f2020-08-24 19:07:05 -07006970 * Returns zero on success, non-zero on failure
6971 */
6972static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag)
6973{
6974 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
6975 int err = 0;
6976 int poll_cnt;
6977 u8 resp = 0xF;
6978 u32 reg;
6979
6980 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6981 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6982 UFS_QUERY_TASK, &resp);
6983 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6984 /* cmd pending in the device */
6985 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
6986 __func__, tag);
6987 break;
6988 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6989 /*
6990 * cmd not pending in the device, check if it is
6991 * in transition.
6992 */
6993 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
6994 __func__, tag);
6995 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6996 if (reg & (1 << tag)) {
6997 /* sleep for max. 200us to stabilize */
6998 usleep_range(100, 200);
6999 continue;
7000 }
7001 /* command completed already */
7002 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
7003 __func__, tag);
7004 goto out;
7005 } else {
7006 dev_err(hba->dev,
7007 "%s: no response from device. tag = %d, err %d\n",
7008 __func__, tag, err);
7009 if (!err)
7010 err = resp; /* service response error */
7011 goto out;
7012 }
7013 }
7014
7015 if (!poll_cnt) {
7016 err = -EBUSY;
7017 goto out;
7018 }
7019
7020 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
7021 UFS_ABORT_TASK, &resp);
7022 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
7023 if (!err) {
7024 err = resp; /* service response error */
7025 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
7026 __func__, tag, err);
7027 }
7028 goto out;
7029 }
7030
7031 err = ufshcd_clear_cmd(hba, tag);
7032 if (err)
7033 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
7034 __func__, tag, err);
7035
7036out:
7037 return err;
7038}
7039
7040/**
7041 * ufshcd_abort - scsi host template eh_abort_handler callback
7042 * @cmd: SCSI command pointer
7043 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307044 * Returns SUCCESS/FAILED
7045 */
7046static int ufshcd_abort(struct scsi_cmnd *cmd)
7047{
Bart Van Assche4728ab42021-07-21 20:34:27 -07007048 struct Scsi_Host *host = cmd->device->host;
7049 struct ufs_hba *hba = shost_priv(host);
Bart Van Assche3f2c1002021-08-09 16:03:50 -07007050 int tag = scsi_cmd_to_rq(cmd)->tag;
Bart Van Assche4728ab42021-07-21 20:34:27 -07007051 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307052 unsigned long flags;
Bart Van Assche64180742021-07-21 20:34:35 -07007053 int err = FAILED;
Bart Van Assche1fbaa02d2021-12-03 15:19:46 -08007054 bool outstanding;
Dolev Ravive9d501b2014-07-01 12:22:37 +03007055 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307056
Bart Van Assche4728ab42021-07-21 20:34:27 -07007057 WARN_ONCE(tag < 0, "Invalid tag %d\n", tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307058
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007059 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03007060 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Bart Van Assche64180742021-07-21 20:34:35 -07007061 /* If command is already aborted/completed, return FAILED. */
Yaniv Gardi14497322016-02-01 15:02:39 +02007062 if (!(test_bit(tag, &hba->outstanding_reqs))) {
7063 dev_err(hba->dev,
7064 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
7065 __func__, tag, hba->outstanding_reqs, reg);
Bart Van Assche64180742021-07-21 20:34:35 -07007066 goto release;
Yaniv Gardi14497322016-02-01 15:02:39 +02007067 }
7068
Dolev Raviv66cc8202016-12-22 18:39:42 -08007069 /* Print Transfer Request of aborted task */
Bean Huod87a1f62020-08-11 16:18:59 +02007070 dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08007071
Gilad Broner7fabb772017-02-03 16:56:50 -08007072 /*
7073 * Print detailed info about aborted request.
7074 * As more than one request might get aborted at the same time,
7075 * print full information only for the first aborted request in order
7076 * to reduce repeated printouts. For other aborted requests only print
7077 * basic details.
7078 */
Can Guo7a7e66c2020-12-02 04:04:02 -08007079 scsi_print_command(cmd);
Gilad Broner7fabb772017-02-03 16:56:50 -08007080 if (!hba->req_abort_count) {
Stanley Chue965e5e2020-12-05 19:58:59 +08007081 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag);
7082 ufshcd_print_evt_hist(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08007083 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08007084 ufshcd_print_pwr_info(hba);
7085 ufshcd_print_trs(hba, 1 << tag, true);
7086 } else {
7087 ufshcd_print_trs(hba, 1 << tag, false);
7088 }
7089 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08007090
Bean Huod87a1f62020-08-11 16:18:59 +02007091 if (!(reg & (1 << tag))) {
7092 dev_err(hba->dev,
7093 "%s: cmd was completed, but without a notifying intr, tag = %d",
7094 __func__, tag);
Bart Van Assche11682522021-10-20 14:40:15 -07007095 __ufshcd_transfer_req_compl(hba, 1UL << tag);
Bart Van Assche64180742021-07-21 20:34:35 -07007096 goto release;
Bean Huod87a1f62020-08-11 16:18:59 +02007097 }
7098
Can Guo7a7e66c2020-12-02 04:04:02 -08007099 /*
7100 * Task abort to the device W-LUN is illegal. When this command
7101 * will fail, due to spec violation, scsi err handling next step
7102 * will be to send LU reset which, again, is a spec violation.
7103 * To avoid these unnecessary/illegal steps, first we clean up
Can Guoa45f9372021-05-24 01:36:57 -07007104 * the lrb taken by this cmd and re-set it in outstanding_reqs,
Adrian Hunter88b099002021-09-17 17:43:49 +03007105 * then queue the eh_work and bail.
Can Guo7a7e66c2020-12-02 04:04:02 -08007106 */
7107 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) {
7108 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun);
Bart Van Assche64180742021-07-21 20:34:35 -07007109
Can Guo7a7e66c2020-12-02 04:04:02 -08007110 spin_lock_irqsave(host->host_lock, flags);
Can Guoa45f9372021-05-24 01:36:57 -07007111 hba->force_reset = true;
Adrian Hunter88b099002021-09-17 17:43:49 +03007112 ufshcd_schedule_eh_work(hba);
Can Guo7a7e66c2020-12-02 04:04:02 -08007113 spin_unlock_irqrestore(host->host_lock, flags);
Bart Van Assche64180742021-07-21 20:34:35 -07007114 goto release;
Can Guo7a7e66c2020-12-02 04:04:02 -08007115 }
7116
Gilad Bronere0b299e2017-02-03 16:56:40 -08007117 /* Skip task abort in case previous aborts failed and report failure */
Bart Van Assche64180742021-07-21 20:34:35 -07007118 if (lrbp->req_abort_skip) {
7119 dev_err(hba->dev, "%s: skipping abort\n", __func__);
7120 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
7121 goto release;
7122 }
Gilad Bronere0b299e2017-02-03 16:56:40 -08007123
Bart Van Assche64180742021-07-21 20:34:35 -07007124 err = ufshcd_try_to_abort_task(hba, tag);
7125 if (err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05307126 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08007127 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05307128 err = FAILED;
Bart Van Assche64180742021-07-21 20:34:35 -07007129 goto release;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05307130 }
7131
Bart Van Assche1fbaa02d2021-12-03 15:19:46 -08007132 /*
7133 * Clear the corresponding bit from outstanding_reqs since the command
7134 * has been aborted successfully.
7135 */
7136 spin_lock_irqsave(&hba->outstanding_lock, flags);
7137 outstanding = __test_and_clear_bit(tag, &hba->outstanding_reqs);
7138 spin_unlock_irqrestore(&hba->outstanding_lock, flags);
7139
7140 if (outstanding)
7141 ufshcd_release_scsi_cmd(hba, lrbp);
7142
Bart Van Assche64180742021-07-21 20:34:35 -07007143 err = SUCCESS;
7144
7145release:
7146 /* Matches the ufshcd_hold() call at the start of this function. */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007147 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307148 return err;
7149}
7150
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307151/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307152 * ufshcd_host_reset_and_restore - reset and restore host controller
7153 * @hba: per-adapter instance
7154 *
7155 * Note that host controller reset may issue DME_RESET to
7156 * local and remote (device) Uni-Pro stack and the attributes
7157 * are reset to default state.
7158 *
7159 * Returns zero on success, non-zero on failure
7160 */
7161static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
7162{
7163 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307164
Can Guo2df74b62019-11-25 22:53:33 -08007165 /*
7166 * Stop the host controller and complete the requests
7167 * cleared by h/w
7168 */
Daejun Parkf02bc972021-07-12 17:58:30 +09007169 ufshpb_reset_host(hba);
Bart Van Assche5cac1092020-05-07 15:27:50 -07007170 ufshcd_hba_stop(hba);
Can Guo2df74b62019-11-25 22:53:33 -08007171 hba->silence_err_logs = true;
Bart Van Assche11682522021-10-20 14:40:15 -07007172 ufshcd_complete_requests(hba);
Can Guo2df74b62019-11-25 22:53:33 -08007173 hba->silence_err_logs = false;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307174
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08007175 /* scale up clocks to max frequency before full reinitialization */
Subhash Jadavani394b9492020-03-26 02:25:40 -07007176 ufshcd_set_clk_freq(hba, true);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08007177
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307178 err = ufshcd_hba_enable(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307179
7180 /* Establish the link again and restore the device */
Randall Huang19186512020-11-30 20:14:02 -08007181 if (!err)
Jaegeuk Kim4ee7ee52021-01-07 10:53:15 -08007182 err = ufshcd_probe_hba(hba, false);
7183
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307184 if (err)
7185 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
Stanley Chue965e5e2020-12-05 19:58:59 +08007186 ufshcd_update_evt_hist(hba, UFS_EVT_HOST_RESET, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307187 return err;
7188}
7189
7190/**
7191 * ufshcd_reset_and_restore - reset and re-initialize host/device
7192 * @hba: per-adapter instance
7193 *
7194 * Reset and recover device, host and re-establish link. This
7195 * is helpful to recover the communication in fatal error conditions.
7196 *
7197 * Returns zero on success, non-zero on failure
7198 */
7199static int ufshcd_reset_and_restore(struct ufs_hba *hba)
7200{
Adrian Hunter54a40452021-10-02 18:45:49 +03007201 u32 saved_err = 0;
7202 u32 saved_uic_err = 0;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307203 int err = 0;
Can Guo4db7a232020-08-09 05:15:51 -07007204 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007205 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307206
Can Guo4db7a232020-08-09 05:15:51 -07007207 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007208 do {
Adrian Hunter54a40452021-10-02 18:45:49 +03007209 /*
7210 * This is a fresh start, cache and clear saved error first,
7211 * in case new error generated during reset and restore.
7212 */
7213 saved_err |= hba->saved_err;
7214 saved_uic_err |= hba->saved_uic_err;
7215 hba->saved_err = 0;
7216 hba->saved_uic_err = 0;
7217 hba->force_reset = false;
7218 hba->ufshcd_state = UFSHCD_STATE_RESET;
7219 spin_unlock_irqrestore(hba->host->host_lock, flags);
7220
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07007221 /* Reset the attached device */
Stanley Chu31a5d9c2020-12-08 21:56:35 +08007222 ufshcd_device_reset(hba);
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07007223
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007224 err = ufshcd_host_reset_and_restore(hba);
Adrian Hunter54a40452021-10-02 18:45:49 +03007225
7226 spin_lock_irqsave(hba->host->host_lock, flags);
7227 if (err)
7228 continue;
7229 /* Do not exit unless operational or dead */
7230 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL &&
7231 hba->ufshcd_state != UFSHCD_STATE_ERROR &&
7232 hba->ufshcd_state != UFSHCD_STATE_EH_SCHEDULED_NON_FATAL)
7233 err = -EAGAIN;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007234 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307235
Can Guo4db7a232020-08-09 05:15:51 -07007236 /*
7237 * Inform scsi mid-layer that we did reset and allow to handle
7238 * Unit Attention properly.
7239 */
7240 scsi_report_bus_reset(hba->host, 0);
7241 if (err) {
Can Guo88a92d62020-12-02 04:04:01 -08007242 hba->ufshcd_state = UFSHCD_STATE_ERROR;
Can Guo4db7a232020-08-09 05:15:51 -07007243 hba->saved_err |= saved_err;
7244 hba->saved_uic_err |= saved_uic_err;
7245 }
7246 spin_unlock_irqrestore(hba->host->host_lock, flags);
7247
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307248 return err;
7249}
7250
7251/**
7252 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
Bart Van Assche8aa29f12018-03-01 15:07:20 -08007253 * @cmd: SCSI command pointer
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307254 *
7255 * Returns SUCCESS/FAILED
7256 */
7257static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
7258{
Can Guo4db7a232020-08-09 05:15:51 -07007259 int err = SUCCESS;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307260 unsigned long flags;
7261 struct ufs_hba *hba;
7262
7263 hba = shost_priv(cmd->device->host);
7264
Can Guo4db7a232020-08-09 05:15:51 -07007265 spin_lock_irqsave(hba->host->host_lock, flags);
7266 hba->force_reset = true;
Adrian Hunter88b099002021-09-17 17:43:49 +03007267 ufshcd_schedule_eh_work(hba);
Can Guo4db7a232020-08-09 05:15:51 -07007268 dev_err(hba->dev, "%s: reset in progress - 1\n", __func__);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307269 spin_unlock_irqrestore(hba->host->host_lock, flags);
7270
Adrian Hunter88b099002021-09-17 17:43:49 +03007271 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307272
7273 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo4db7a232020-08-09 05:15:51 -07007274 if (hba->ufshcd_state == UFSHCD_STATE_ERROR)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307275 err = FAILED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307276 spin_unlock_irqrestore(hba->host->host_lock, flags);
7277
7278 return err;
7279}
7280
7281/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007282 * ufshcd_get_max_icc_level - calculate the ICC level
7283 * @sup_curr_uA: max. current supported by the regulator
7284 * @start_scan: row at the desc table to start scan from
7285 * @buff: power descriptor buffer
7286 *
7287 * Returns calculated max ICC level for specific regulator
7288 */
7289static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
7290{
7291 int i;
7292 int curr_uA;
7293 u16 data;
7294 u16 unit;
7295
7296 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02007297 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007298 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
7299 ATTR_ICC_LVL_UNIT_OFFSET;
7300 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
7301 switch (unit) {
7302 case UFSHCD_NANO_AMP:
7303 curr_uA = curr_uA / 1000;
7304 break;
7305 case UFSHCD_MILI_AMP:
7306 curr_uA = curr_uA * 1000;
7307 break;
7308 case UFSHCD_AMP:
7309 curr_uA = curr_uA * 1000 * 1000;
7310 break;
7311 case UFSHCD_MICRO_AMP:
7312 default:
7313 break;
7314 }
7315 if (sup_curr_uA >= curr_uA)
7316 break;
7317 }
7318 if (i < 0) {
7319 i = 0;
7320 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
7321 }
7322
7323 return (u32)i;
7324}
7325
7326/**
Lee Jones11eea9b2021-03-12 09:47:10 +00007327 * ufshcd_find_max_sup_active_icc_level - calculate the max ICC level
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007328 * In case regulators are not initialized we'll return 0
7329 * @hba: per-adapter instance
7330 * @desc_buf: power descriptor buffer to extract ICC levels from.
7331 * @len: length of desc_buff
7332 *
7333 * Returns calculated ICC level
7334 */
7335static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
7336 u8 *desc_buf, int len)
7337{
7338 u32 icc_level = 0;
7339
7340 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
7341 !hba->vreg_info.vccq2) {
7342 dev_err(hba->dev,
7343 "%s: Regulator capability was not set, actvIccLevel=%d",
7344 __func__, icc_level);
7345 goto out;
7346 }
7347
Yue Hu08730452021-03-19 15:09:16 +08007348 if (hba->vreg_info.vcc->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007349 icc_level = ufshcd_get_max_icc_level(
7350 hba->vreg_info.vcc->max_uA,
7351 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
7352 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
7353
Yue Hu08730452021-03-19 15:09:16 +08007354 if (hba->vreg_info.vccq->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007355 icc_level = ufshcd_get_max_icc_level(
7356 hba->vreg_info.vccq->max_uA,
7357 icc_level,
7358 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
7359
Yue Hu08730452021-03-19 15:09:16 +08007360 if (hba->vreg_info.vccq2->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007361 icc_level = ufshcd_get_max_icc_level(
7362 hba->vreg_info.vccq2->max_uA,
7363 icc_level,
7364 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
7365out:
7366 return icc_level;
7367}
7368
Can Guoe89860f2020-03-26 02:25:41 -07007369static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007370{
7371 int ret;
Bean Huo7a0bf852020-06-03 11:19:58 +02007372 int buff_len = hba->desc_size[QUERY_DESC_IDN_POWER];
Kees Cookbbe21d72018-05-02 16:58:09 -07007373 u8 *desc_buf;
Can Guoe89860f2020-03-26 02:25:41 -07007374 u32 icc_level;
Kees Cookbbe21d72018-05-02 16:58:09 -07007375
7376 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7377 if (!desc_buf)
7378 return;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007379
Bean Huoc4607a02020-06-03 11:19:56 +02007380 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0,
7381 desc_buf, buff_len);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007382 if (ret) {
7383 dev_err(hba->dev,
7384 "%s: Failed reading power descriptor.len = %d ret = %d",
7385 __func__, buff_len, ret);
Kees Cookbbe21d72018-05-02 16:58:09 -07007386 goto out;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007387 }
7388
Can Guoe89860f2020-03-26 02:25:41 -07007389 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf,
7390 buff_len);
7391 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007392
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02007393 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Can Guoe89860f2020-03-26 02:25:41 -07007394 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007395
7396 if (ret)
7397 dev_err(hba->dev,
7398 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
Can Guoe89860f2020-03-26 02:25:41 -07007399 __func__, icc_level, ret);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007400
Kees Cookbbe21d72018-05-02 16:58:09 -07007401out:
7402 kfree(desc_buf);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007403}
7404
Can Guofb276f72020-03-25 18:09:59 -07007405static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
7406{
7407 scsi_autopm_get_device(sdev);
7408 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev);
7409 if (sdev->rpm_autosuspend)
7410 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev,
7411 RPM_AUTOSUSPEND_DELAY_MS);
7412 scsi_autopm_put_device(sdev);
7413}
7414
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007415/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007416 * ufshcd_scsi_add_wlus - Adds required W-LUs
7417 * @hba: per-adapter instance
7418 *
7419 * UFS device specification requires the UFS devices to support 4 well known
7420 * logical units:
7421 * "REPORT_LUNS" (address: 01h)
7422 * "UFS Device" (address: 50h)
7423 * "RPMB" (address: 44h)
7424 * "BOOT" (address: 30h)
7425 * UFS device's power management needs to be controlled by "POWER CONDITION"
7426 * field of SSU (START STOP UNIT) command. But this "power condition" field
7427 * will take effect only when its sent to "UFS device" well known logical unit
7428 * hence we require the scsi_device instance to represent this logical unit in
7429 * order for the UFS host driver to send the SSU command for power management.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08007430 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007431 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
7432 * Block) LU so user space process can control this LU. User space may also
7433 * want to have access to BOOT LU.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08007434 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007435 * This function adds scsi device instances for each of all well known LUs
7436 * (except "REPORT LUNS" LU).
7437 *
7438 * Returns zero on success (all required W-LUs are added successfully),
7439 * non-zero error value on failure (if failed to add any of the required W-LU).
7440 */
7441static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
7442{
7443 int ret = 0;
Bart Van Assche59830c02021-12-03 15:19:37 -08007444 struct scsi_device *sdev_boot, *sdev_rpmb;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007445
7446 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
7447 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
7448 if (IS_ERR(hba->sdev_ufs_device)) {
7449 ret = PTR_ERR(hba->sdev_ufs_device);
7450 hba->sdev_ufs_device = NULL;
7451 goto out;
7452 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007453 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007454
Bart Van Assche59830c02021-12-03 15:19:37 -08007455 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007456 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Bart Van Assche59830c02021-12-03 15:19:37 -08007457 if (IS_ERR(sdev_rpmb)) {
7458 ret = PTR_ERR(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08007459 goto remove_sdev_ufs_device;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007460 }
Bart Van Assche59830c02021-12-03 15:19:37 -08007461 ufshcd_blk_pm_runtime_init(sdev_rpmb);
7462 scsi_device_put(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08007463
7464 sdev_boot = __scsi_add_device(hba->host, 0, 0,
7465 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Can Guofb276f72020-03-25 18:09:59 -07007466 if (IS_ERR(sdev_boot)) {
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08007467 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
Can Guofb276f72020-03-25 18:09:59 -07007468 } else {
7469 ufshcd_blk_pm_runtime_init(sdev_boot);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08007470 scsi_device_put(sdev_boot);
Can Guofb276f72020-03-25 18:09:59 -07007471 }
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007472 goto out;
7473
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007474remove_sdev_ufs_device:
7475 scsi_remove_device(hba->sdev_ufs_device);
7476out:
7477 return ret;
7478}
7479
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007480static void ufshcd_wb_probe(struct ufs_hba *hba, u8 *desc_buf)
7481{
Stanley Chua7f1e692020-06-25 11:04:30 +08007482 struct ufs_dev_info *dev_info = &hba->dev_info;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007483 u8 lun;
7484 u32 d_lu_wb_buf_alloc;
Bean Huoe8d03812021-01-19 17:38:45 +01007485 u32 ext_ufs_feature;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007486
Stanley Chu817d7e12020-05-08 16:01:08 +08007487 if (!ufshcd_is_wb_allowed(hba))
7488 return;
Stanley Chua7f1e692020-06-25 11:04:30 +08007489 /*
7490 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or
7491 * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES
7492 * enabled
7493 */
7494 if (!(dev_info->wspecversion >= 0x310 ||
7495 dev_info->wspecversion == 0x220 ||
7496 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES)))
7497 goto wb_disabled;
Stanley Chu817d7e12020-05-08 16:01:08 +08007498
Bean Huo7a0bf852020-06-03 11:19:58 +02007499 if (hba->desc_size[QUERY_DESC_IDN_DEVICE] <
7500 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP + 4)
Stanley Chu817d7e12020-05-08 16:01:08 +08007501 goto wb_disabled;
7502
Bean Huoe8d03812021-01-19 17:38:45 +01007503 ext_ufs_feature = get_unaligned_be32(desc_buf +
7504 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
Stanley Chu817d7e12020-05-08 16:01:08 +08007505
Bean Huoe8d03812021-01-19 17:38:45 +01007506 if (!(ext_ufs_feature & UFS_DEV_WRITE_BOOSTER_SUP))
Stanley Chu817d7e12020-05-08 16:01:08 +08007507 goto wb_disabled;
7508
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007509 /*
Bean Huoae1ce1f2021-01-19 17:38:44 +01007510 * WB may be supported but not configured while provisioning. The spec
7511 * says, in dedicated wb buffer mode, a max of 1 lun would have wb
7512 * buffer configured.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007513 */
Bean Huo4cd48992021-01-19 17:38:46 +01007514 dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE];
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007515
Stanley Chua7f1e692020-06-25 11:04:30 +08007516 dev_info->b_presrv_uspc_en =
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007517 desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN];
7518
Bean Huo4cd48992021-01-19 17:38:46 +01007519 if (dev_info->wb_buffer_type == WB_BUF_MODE_SHARED) {
Bean Huoe8d03812021-01-19 17:38:45 +01007520 if (!get_unaligned_be32(desc_buf +
7521 DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS))
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007522 goto wb_disabled;
7523 } else {
7524 for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) {
7525 d_lu_wb_buf_alloc = 0;
7526 ufshcd_read_unit_desc_param(hba,
7527 lun,
7528 UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS,
7529 (u8 *)&d_lu_wb_buf_alloc,
7530 sizeof(d_lu_wb_buf_alloc));
7531 if (d_lu_wb_buf_alloc) {
Stanley Chua7f1e692020-06-25 11:04:30 +08007532 dev_info->wb_dedicated_lu = lun;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007533 break;
7534 }
7535 }
Stanley Chu817d7e12020-05-08 16:01:08 +08007536
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007537 if (!d_lu_wb_buf_alloc)
7538 goto wb_disabled;
7539 }
Stanley Chu817d7e12020-05-08 16:01:08 +08007540 return;
7541
7542wb_disabled:
7543 hba->caps &= ~UFSHCD_CAP_WB_EN;
7544}
7545
Avri Altmane88e2d32021-09-15 09:04:06 +03007546static void ufshcd_temp_notif_probe(struct ufs_hba *hba, u8 *desc_buf)
7547{
7548 struct ufs_dev_info *dev_info = &hba->dev_info;
7549 u32 ext_ufs_feature;
7550 u8 mask = 0;
7551
7552 if (!(hba->caps & UFSHCD_CAP_TEMP_NOTIF) || dev_info->wspecversion < 0x300)
7553 return;
7554
7555 ext_ufs_feature = get_unaligned_be32(desc_buf + DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
7556
7557 if (ext_ufs_feature & UFS_DEV_LOW_TEMP_NOTIF)
7558 mask |= MASK_EE_TOO_LOW_TEMP;
7559
7560 if (ext_ufs_feature & UFS_DEV_HIGH_TEMP_NOTIF)
7561 mask |= MASK_EE_TOO_HIGH_TEMP;
7562
7563 if (mask) {
7564 ufshcd_enable_ee(hba, mask);
7565 ufs_hwmon_probe(hba, mask);
7566 }
7567}
7568
Stanley Chu8db269a2020-05-08 16:01:10 +08007569void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups)
Stanley Chu817d7e12020-05-08 16:01:08 +08007570{
7571 struct ufs_dev_fix *f;
7572 struct ufs_dev_info *dev_info = &hba->dev_info;
7573
Stanley Chu8db269a2020-05-08 16:01:10 +08007574 if (!fixups)
7575 return;
7576
7577 for (f = fixups; f->quirk; f++) {
Stanley Chu817d7e12020-05-08 16:01:08 +08007578 if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
7579 f->wmanufacturerid == UFS_ANY_VENDOR) &&
7580 ((dev_info->model &&
7581 STR_PRFX_EQUAL(f->model, dev_info->model)) ||
7582 !strcmp(f->model, UFS_ANY_MODEL)))
7583 hba->dev_quirks |= f->quirk;
7584 }
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007585}
Stanley Chu8db269a2020-05-08 16:01:10 +08007586EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007587
Stanley Chuc28c00b2020-05-08 16:01:09 +08007588static void ufs_fixup_device_setup(struct ufs_hba *hba)
7589{
7590 /* fix by general quirk table */
Stanley Chu8db269a2020-05-08 16:01:10 +08007591 ufshcd_fixup_dev_quirks(hba, ufs_fixups);
Stanley Chuc28c00b2020-05-08 16:01:09 +08007592
7593 /* allow vendors to fix quirks */
7594 ufshcd_vops_fixup_dev_quirks(hba);
7595}
7596
Bean Huo09750062020-01-20 14:08:14 +01007597static int ufs_get_device_desc(struct ufs_hba *hba)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007598{
7599 int err;
7600 u8 model_index;
Daejun Parkf02bc972021-07-12 17:58:30 +09007601 u8 b_ufs_feature_sup;
Kees Cookbbe21d72018-05-02 16:58:09 -07007602 u8 *desc_buf;
Bean Huo09750062020-01-20 14:08:14 +01007603 struct ufs_dev_info *dev_info = &hba->dev_info;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007604
Bean Huo458a45f2020-06-03 11:19:55 +02007605 desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
Kees Cookbbe21d72018-05-02 16:58:09 -07007606 if (!desc_buf) {
7607 err = -ENOMEM;
7608 goto out;
7609 }
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007610
Bean Huoc4607a02020-06-03 11:19:56 +02007611 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
Bean Huo7a0bf852020-06-03 11:19:58 +02007612 hba->desc_size[QUERY_DESC_IDN_DEVICE]);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007613 if (err) {
7614 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
7615 __func__, err);
7616 goto out;
7617 }
7618
7619 /*
7620 * getting vendor (manufacturerID) and Bank Index in big endian
7621 * format
7622 */
Bean Huo09750062020-01-20 14:08:14 +01007623 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007624 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
7625
Can Guo09f17792020-02-10 19:40:49 -08007626 /* getting Specification Version in big endian format */
7627 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
7628 desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
Daejun Parkf02bc972021-07-12 17:58:30 +09007629 b_ufs_feature_sup = desc_buf[DEVICE_DESC_PARAM_UFS_FEAT];
Can Guo09f17792020-02-10 19:40:49 -08007630
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007631 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007632
Daejun Parkf02bc972021-07-12 17:58:30 +09007633 if (dev_info->wspecversion >= UFS_DEV_HPB_SUPPORT_VERSION &&
7634 (b_ufs_feature_sup & UFS_DEV_HPB_SUPPORT)) {
Daejun Park41d8a932021-07-12 18:00:25 +09007635 bool hpb_en = false;
7636
Daejun Parkf02bc972021-07-12 17:58:30 +09007637 ufshpb_get_dev_info(hba, desc_buf);
Daejun Park41d8a932021-07-12 18:00:25 +09007638
7639 if (!ufshpb_is_legacy(hba))
7640 err = ufshcd_query_flag_retry(hba,
7641 UPIU_QUERY_OPCODE_READ_FLAG,
7642 QUERY_FLAG_IDN_HPB_EN, 0,
7643 &hpb_en);
7644
7645 if (ufshpb_is_legacy(hba) || (!err && hpb_en))
7646 dev_info->hpb_enabled = true;
Daejun Parkf02bc972021-07-12 17:58:30 +09007647 }
7648
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007649 err = ufshcd_read_string_desc(hba, model_index,
Bean Huo09750062020-01-20 14:08:14 +01007650 &dev_info->model, SD_ASCII_STD);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007651 if (err < 0) {
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007652 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
7653 __func__, err);
7654 goto out;
7655 }
7656
Asutosh Dasb294ff32021-04-23 17:20:16 -07007657 hba->luns_avail = desc_buf[DEVICE_DESC_PARAM_NUM_LU] +
7658 desc_buf[DEVICE_DESC_PARAM_NUM_WLU];
7659
Stanley Chu817d7e12020-05-08 16:01:08 +08007660 ufs_fixup_device_setup(hba);
7661
Stanley Chua7f1e692020-06-25 11:04:30 +08007662 ufshcd_wb_probe(hba, desc_buf);
Stanley Chu817d7e12020-05-08 16:01:08 +08007663
Avri Altmane88e2d32021-09-15 09:04:06 +03007664 ufshcd_temp_notif_probe(hba, desc_buf);
7665
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007666 /*
7667 * ufshcd_read_string_desc returns size of the string
7668 * reset the error value
7669 */
7670 err = 0;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007671
7672out:
Kees Cookbbe21d72018-05-02 16:58:09 -07007673 kfree(desc_buf);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007674 return err;
7675}
7676
Bean Huo09750062020-01-20 14:08:14 +01007677static void ufs_put_device_desc(struct ufs_hba *hba)
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007678{
Bean Huo09750062020-01-20 14:08:14 +01007679 struct ufs_dev_info *dev_info = &hba->dev_info;
7680
7681 kfree(dev_info->model);
7682 dev_info->model = NULL;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007683}
7684
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007685/**
Yaniv Gardi37113102016-03-10 17:37:16 +02007686 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
7687 * @hba: per-adapter instance
7688 *
7689 * PA_TActivate parameter can be tuned manually if UniPro version is less than
7690 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
7691 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
7692 * the hibern8 exit latency.
7693 *
7694 * Returns zero on success, non-zero error value on failure.
7695 */
7696static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
7697{
7698 int ret = 0;
7699 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
7700
7701 ret = ufshcd_dme_peer_get(hba,
7702 UIC_ARG_MIB_SEL(
7703 RX_MIN_ACTIVATETIME_CAPABILITY,
7704 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7705 &peer_rx_min_activatetime);
7706 if (ret)
7707 goto out;
7708
7709 /* make sure proper unit conversion is applied */
7710 tuned_pa_tactivate =
7711 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
7712 / PA_TACTIVATE_TIME_UNIT_US);
7713 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7714 tuned_pa_tactivate);
7715
7716out:
7717 return ret;
7718}
7719
7720/**
7721 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
7722 * @hba: per-adapter instance
7723 *
7724 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
7725 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
7726 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
7727 * This optimal value can help reduce the hibern8 exit latency.
7728 *
7729 * Returns zero on success, non-zero error value on failure.
7730 */
7731static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
7732{
7733 int ret = 0;
7734 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
7735 u32 max_hibern8_time, tuned_pa_hibern8time;
7736
7737 ret = ufshcd_dme_get(hba,
7738 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
7739 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
7740 &local_tx_hibern8_time_cap);
7741 if (ret)
7742 goto out;
7743
7744 ret = ufshcd_dme_peer_get(hba,
7745 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
7746 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7747 &peer_rx_hibern8_time_cap);
7748 if (ret)
7749 goto out;
7750
7751 max_hibern8_time = max(local_tx_hibern8_time_cap,
7752 peer_rx_hibern8_time_cap);
7753 /* make sure proper unit conversion is applied */
7754 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
7755 / PA_HIBERN8_TIME_UNIT_US);
7756 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
7757 tuned_pa_hibern8time);
7758out:
7759 return ret;
7760}
7761
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007762/**
7763 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
7764 * less than device PA_TACTIVATE time.
7765 * @hba: per-adapter instance
7766 *
7767 * Some UFS devices require host PA_TACTIVATE to be lower than device
7768 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
7769 * for such devices.
7770 *
7771 * Returns zero on success, non-zero error value on failure.
7772 */
7773static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
7774{
7775 int ret = 0;
7776 u32 granularity, peer_granularity;
7777 u32 pa_tactivate, peer_pa_tactivate;
7778 u32 pa_tactivate_us, peer_pa_tactivate_us;
7779 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
7780
7781 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7782 &granularity);
7783 if (ret)
7784 goto out;
7785
7786 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7787 &peer_granularity);
7788 if (ret)
7789 goto out;
7790
7791 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
7792 (granularity > PA_GRANULARITY_MAX_VAL)) {
7793 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
7794 __func__, granularity);
7795 return -EINVAL;
7796 }
7797
7798 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
7799 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
7800 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
7801 __func__, peer_granularity);
7802 return -EINVAL;
7803 }
7804
7805 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
7806 if (ret)
7807 goto out;
7808
7809 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
7810 &peer_pa_tactivate);
7811 if (ret)
7812 goto out;
7813
7814 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
7815 peer_pa_tactivate_us = peer_pa_tactivate *
7816 gran_to_us_table[peer_granularity - 1];
7817
SEO HOYOUNG90086612022-01-07 06:39:24 +09007818 if (pa_tactivate_us >= peer_pa_tactivate_us) {
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007819 u32 new_peer_pa_tactivate;
7820
7821 new_peer_pa_tactivate = pa_tactivate_us /
7822 gran_to_us_table[peer_granularity - 1];
7823 new_peer_pa_tactivate++;
7824 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7825 new_peer_pa_tactivate);
7826 }
7827
7828out:
7829 return ret;
7830}
7831
Bean Huo09750062020-01-20 14:08:14 +01007832static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
Yaniv Gardi37113102016-03-10 17:37:16 +02007833{
7834 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
7835 ufshcd_tune_pa_tactivate(hba);
7836 ufshcd_tune_pa_hibern8time(hba);
7837 }
7838
Can Guoe91ed9e2020-02-23 20:09:21 -08007839 ufshcd_vops_apply_dev_quirks(hba);
7840
Yaniv Gardi37113102016-03-10 17:37:16 +02007841 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
7842 /* set 1ms timeout for PA_TACTIVATE */
7843 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007844
7845 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
7846 ufshcd_quirk_tune_host_pa_tactivate(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02007847}
7848
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007849static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
7850{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007851 hba->ufs_stats.hibern8_exit_cnt = 0;
7852 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08007853 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007854}
7855
Bean Huo731f0622020-01-20 14:08:19 +01007856static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
7857{
7858 int err;
7859 size_t buff_len;
7860 u8 *desc_buf;
7861
Bean Huo7a0bf852020-06-03 11:19:58 +02007862 buff_len = hba->desc_size[QUERY_DESC_IDN_GEOMETRY];
Bean Huo731f0622020-01-20 14:08:19 +01007863 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7864 if (!desc_buf) {
7865 err = -ENOMEM;
7866 goto out;
7867 }
7868
Bean Huoc4607a02020-06-03 11:19:56 +02007869 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
7870 desc_buf, buff_len);
Bean Huo731f0622020-01-20 14:08:19 +01007871 if (err) {
7872 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
7873 __func__, err);
7874 goto out;
7875 }
7876
7877 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
7878 hba->dev_info.max_lu_supported = 32;
7879 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
7880 hba->dev_info.max_lu_supported = 8;
7881
Daejun Parkf02bc972021-07-12 17:58:30 +09007882 if (hba->desc_size[QUERY_DESC_IDN_GEOMETRY] >=
7883 GEOMETRY_DESC_PARAM_HPB_MAX_ACTIVE_REGS)
7884 ufshpb_get_geo_info(hba, desc_buf);
7885
Bean Huo731f0622020-01-20 14:08:19 +01007886out:
7887 kfree(desc_buf);
7888 return err;
7889}
7890
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307891static struct ufs_ref_clk ufs_ref_clk_freqs[] = {
7892 {19200000, REF_CLK_FREQ_19_2_MHZ},
7893 {26000000, REF_CLK_FREQ_26_MHZ},
7894 {38400000, REF_CLK_FREQ_38_4_MHZ},
7895 {52000000, REF_CLK_FREQ_52_MHZ},
7896 {0, REF_CLK_FREQ_INVAL},
7897};
7898
7899static enum ufs_ref_clk_freq
7900ufs_get_bref_clk_from_hz(unsigned long freq)
7901{
7902 int i;
7903
7904 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
7905 if (ufs_ref_clk_freqs[i].freq_hz == freq)
7906 return ufs_ref_clk_freqs[i].val;
7907
7908 return REF_CLK_FREQ_INVAL;
7909}
7910
7911void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
7912{
7913 unsigned long freq;
7914
7915 freq = clk_get_rate(refclk);
7916
7917 hba->dev_ref_clk_freq =
7918 ufs_get_bref_clk_from_hz(freq);
7919
7920 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
7921 dev_err(hba->dev,
7922 "invalid ref_clk setting = %ld\n", freq);
7923}
7924
7925static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
7926{
7927 int err;
7928 u32 ref_clk;
7929 u32 freq = hba->dev_ref_clk_freq;
7930
7931 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
7932 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
7933
7934 if (err) {
7935 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
7936 err);
7937 goto out;
7938 }
7939
7940 if (ref_clk == freq)
7941 goto out; /* nothing to update */
7942
7943 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7944 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
7945
7946 if (err) {
7947 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
7948 ufs_ref_clk_freqs[freq].freq_hz);
7949 goto out;
7950 }
7951
7952 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
7953 ufs_ref_clk_freqs[freq].freq_hz);
7954
7955out:
7956 return err;
7957}
7958
Bean Huo1b9e2142020-01-20 14:08:15 +01007959static int ufshcd_device_params_init(struct ufs_hba *hba)
7960{
7961 bool flag;
Bean Huo7a0bf852020-06-03 11:19:58 +02007962 int ret, i;
Bean Huo1b9e2142020-01-20 14:08:15 +01007963
Bean Huo7a0bf852020-06-03 11:19:58 +02007964 /* Init device descriptor sizes */
7965 for (i = 0; i < QUERY_DESC_IDN_MAX; i++)
7966 hba->desc_size[i] = QUERY_DESC_MAX_SIZE;
Bean Huo1b9e2142020-01-20 14:08:15 +01007967
Bean Huo731f0622020-01-20 14:08:19 +01007968 /* Init UFS geometry descriptor related parameters */
7969 ret = ufshcd_device_geo_params_init(hba);
7970 if (ret)
7971 goto out;
7972
Bean Huo1b9e2142020-01-20 14:08:15 +01007973 /* Check and apply UFS device quirks */
7974 ret = ufs_get_device_desc(hba);
7975 if (ret) {
7976 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
7977 __func__, ret);
7978 goto out;
7979 }
7980
Can Guo09f17792020-02-10 19:40:49 -08007981 ufshcd_get_ref_clk_gating_wait(hba);
7982
Bean Huo1b9e2142020-01-20 14:08:15 +01007983 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08007984 QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag))
Bean Huo1b9e2142020-01-20 14:08:15 +01007985 hba->dev_info.f_power_on_wp_en = flag;
7986
Bean Huo2b35b2a2020-01-20 14:08:16 +01007987 /* Probe maximum power mode co-supported by both UFS host and device */
7988 if (ufshcd_get_max_pwr_mode(hba))
7989 dev_err(hba->dev,
7990 "%s: Failed getting max supported power mode\n",
7991 __func__);
Bean Huo1b9e2142020-01-20 14:08:15 +01007992out:
7993 return ret;
7994}
7995
7996/**
7997 * ufshcd_add_lus - probe and add UFS logical units
7998 * @hba: per-adapter instance
7999 */
8000static int ufshcd_add_lus(struct ufs_hba *hba)
8001{
8002 int ret;
8003
Bean Huo1b9e2142020-01-20 14:08:15 +01008004 /* Add required well known logical units to scsi mid layer */
8005 ret = ufshcd_scsi_add_wlus(hba);
8006 if (ret)
8007 goto out;
8008
8009 /* Initialize devfreq after UFS device is detected */
8010 if (ufshcd_is_clkscaling_supported(hba)) {
8011 memcpy(&hba->clk_scaling.saved_pwr_info.info,
8012 &hba->pwr_info,
8013 sizeof(struct ufs_pa_layer_attr));
8014 hba->clk_scaling.saved_pwr_info.is_valid = true;
Bean Huo1b9e2142020-01-20 14:08:15 +01008015 hba->clk_scaling.is_allowed = true;
Bean Huo1b9e2142020-01-20 14:08:15 +01008016
Stanley Chub058fa82021-01-20 23:01:41 +08008017 ret = ufshcd_devfreq_init(hba);
8018 if (ret)
8019 goto out;
8020
8021 hba->clk_scaling.is_enabled = true;
8022 ufshcd_init_clk_scaling_sysfs(hba);
Bean Huo1b9e2142020-01-20 14:08:15 +01008023 }
8024
8025 ufs_bsg_probe(hba);
Daejun Parkf02bc972021-07-12 17:58:30 +09008026 ufshpb_init(hba);
Bean Huo1b9e2142020-01-20 14:08:15 +01008027 scsi_scan_host(hba->host);
8028 pm_runtime_put_sync(hba->dev);
8029
Bean Huo1b9e2142020-01-20 14:08:15 +01008030out:
8031 return ret;
8032}
8033
Yaniv Gardi37113102016-03-10 17:37:16 +02008034/**
Bart Van Assche568dd992021-07-21 20:34:25 -07008035 * ufshcd_probe_hba - probe hba to detect device and initialize it
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008036 * @hba: per-adapter instance
Bart Van Assche568dd992021-07-21 20:34:25 -07008037 * @init_dev_params: whether or not to call ufshcd_device_params_init().
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008038 *
8039 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308040 */
Bart Van Assche568dd992021-07-21 20:34:25 -07008041static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308042{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308043 int ret;
Can Guo4db7a232020-08-09 05:15:51 -07008044 unsigned long flags;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008045 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308046
Can Guoaa53f582021-02-23 21:36:47 -08008047 hba->ufshcd_state = UFSHCD_STATE_RESET;
8048
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308049 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05308050 if (ret)
8051 goto out;
8052
jongmin jeong10fb4f82021-10-18 21:42:03 +09008053 if (hba->quirks & UFSHCD_QUIRK_SKIP_PH_CONFIGURATION)
8054 goto out;
8055
Dolev Ravivff8e20c2016-12-22 18:42:18 -08008056 /* Debug counters initialization */
8057 ufshcd_clear_dbg_ufs_stats(hba);
8058
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008059 /* UniPro link is active now */
8060 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05308061
Bean Huo1b9e2142020-01-20 14:08:15 +01008062 /* Verify device initialization by sending NOP OUT UPIU */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05308063 ret = ufshcd_verify_dev_init(hba);
8064 if (ret)
8065 goto out;
8066
Bean Huo1b9e2142020-01-20 14:08:15 +01008067 /* Initiate UFS initialization, and waiting until completion */
Dolev Raviv68078d52013-07-30 00:35:58 +05308068 ret = ufshcd_complete_dev_init(hba);
8069 if (ret)
8070 goto out;
8071
Bean Huo1b9e2142020-01-20 14:08:15 +01008072 /*
8073 * Initialize UFS device parameters used by driver, these
8074 * parameters are associated with UFS descriptors.
8075 */
Bart Van Assche568dd992021-07-21 20:34:25 -07008076 if (init_dev_params) {
Bean Huo1b9e2142020-01-20 14:08:15 +01008077 ret = ufshcd_device_params_init(hba);
8078 if (ret)
8079 goto out;
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02008080 }
8081
Bean Huo09750062020-01-20 14:08:14 +01008082 ufshcd_tune_unipro_params(hba);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03008083
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008084 /* UFS device is also active now */
8085 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308086 ufshcd_force_reset_auto_bkops(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05308087
Bean Huo2b35b2a2020-01-20 14:08:16 +01008088 /* Gear up to HS gear if supported */
8089 if (hba->max_pwr_info.is_valid) {
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05308090 /*
8091 * Set the right value to bRefClkFreq before attempting to
8092 * switch to HS gears.
8093 */
8094 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
8095 ufshcd_set_dev_ref_clk(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03008096 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07008097 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03008098 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
8099 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07008100 goto out;
8101 }
Can Guo6a9df812020-02-11 21:38:28 -08008102 ufshcd_print_pwr_info(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03008103 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008104
Can Guoe89860f2020-03-26 02:25:41 -07008105 /*
8106 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
8107 * and for removable UFS card as well, hence always set the parameter.
8108 * Note: Error handler may issue the device reset hence resetting
8109 * bActiveICCLevel as well so it is always safe to set this here.
8110 */
8111 ufshcd_set_active_icc_lvl(hba);
8112
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008113 ufshcd_wb_config(hba);
Adrian Huntercd469472021-02-09 08:24:36 +02008114 if (hba->ee_usr_mask)
8115 ufshcd_write_ee_control(hba);
Can Guo71d848b2019-11-14 22:09:26 -08008116 /* Enable Auto-Hibernate if configured */
8117 ufshcd_auto_hibern8_enable(hba);
8118
Daejun Parkf02bc972021-07-12 17:58:30 +09008119 ufshpb_reset(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05308120out:
Can Guo4db7a232020-08-09 05:15:51 -07008121 spin_lock_irqsave(hba->host->host_lock, flags);
8122 if (ret)
8123 hba->ufshcd_state = UFSHCD_STATE_ERROR;
8124 else if (hba->ufshcd_state == UFSHCD_STATE_RESET)
8125 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
8126 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008127
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008128 trace_ufshcd_init(dev_name(hba->dev), ret,
8129 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008130 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008131 return ret;
8132}
8133
8134/**
8135 * ufshcd_async_scan - asynchronous execution for probing hba
8136 * @data: data pointer to pass to this function
8137 * @cookie: cookie data
8138 */
8139static void ufshcd_async_scan(void *data, async_cookie_t cookie)
8140{
8141 struct ufs_hba *hba = (struct ufs_hba *)data;
Bean Huo1b9e2142020-01-20 14:08:15 +01008142 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008143
Can Guo9cd20d32021-01-13 19:13:28 -08008144 down(&hba->host_sem);
Bean Huo1b9e2142020-01-20 14:08:15 +01008145 /* Initialize hba, detect and initialize UFS device */
8146 ret = ufshcd_probe_hba(hba, true);
Can Guo9cd20d32021-01-13 19:13:28 -08008147 up(&hba->host_sem);
Bean Huo1b9e2142020-01-20 14:08:15 +01008148 if (ret)
8149 goto out;
8150
8151 /* Probe and add UFS logical units */
8152 ret = ufshcd_add_lus(hba);
8153out:
8154 /*
8155 * If we failed to initialize the device or the device is not
8156 * present, turn off the power/clocks etc.
8157 */
8158 if (ret) {
8159 pm_runtime_put_sync(hba->dev);
Bean Huo1b9e2142020-01-20 14:08:15 +01008160 ufshcd_hba_exit(hba);
8161 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308162}
8163
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02008164static const struct attribute_group *ufshcd_driver_groups[] = {
8165 &ufs_sysfs_unit_descriptor_group,
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02008166 &ufs_sysfs_lun_attributes_group,
Daejun Parkf02bc972021-07-12 17:58:30 +09008167#ifdef CONFIG_SCSI_UFS_HPB
8168 &ufs_sysfs_hpb_stat_group,
Daejun Park41d8a932021-07-12 18:00:25 +09008169 &ufs_sysfs_hpb_param_group,
Daejun Parkf02bc972021-07-12 17:58:30 +09008170#endif
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02008171 NULL,
8172};
8173
Stanley Chu90b84912020-05-09 17:37:13 +08008174static struct ufs_hba_variant_params ufs_hba_vps = {
8175 .hba_enable_delay_us = 1000,
Stanley Chud14734ae2020-05-09 17:37:15 +08008176 .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40),
Stanley Chu90b84912020-05-09 17:37:13 +08008177 .devfreq_profile.polling_ms = 100,
8178 .devfreq_profile.target = ufshcd_devfreq_target,
8179 .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status,
8180 .ondemand_data.upthreshold = 70,
8181 .ondemand_data.downdifferential = 5,
8182};
8183
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308184static struct scsi_host_template ufshcd_driver_template = {
8185 .module = THIS_MODULE,
8186 .name = UFSHCD,
8187 .proc_name = UFSHCD,
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08008188 .map_queues = ufshcd_map_queues,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308189 .queuecommand = ufshcd_queuecommand,
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08008190 .mq_poll = ufshcd_poll,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308191 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09008192 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308193 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03008194 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308195 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05308196 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
8197 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308198 .this_id = -1,
8199 .sg_tablesize = SG_ALL,
8200 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
8201 .can_queue = UFSHCD_CAN_QUEUE,
Christoph Hellwig552a9902019-06-17 14:19:55 +02008202 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008203 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01008204 .track_queue_depth = 1,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02008205 .sdev_groups = ufshcd_driver_groups,
Christoph Hellwig4af14d12018-12-13 16:17:09 +01008206 .dma_boundary = PAGE_SIZE - 1,
Stanley Chu49615ba2019-09-16 23:56:50 +08008207 .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308208};
8209
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008210static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
8211 int ua)
8212{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08008213 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008214
Bjorn Andersson7b16a072015-02-11 19:35:28 -08008215 if (!vreg)
8216 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008217
Stanley Chu0487fff2019-03-28 17:16:25 +08008218 /*
8219 * "set_load" operation shall be required on those regulators
8220 * which specifically configured current limitation. Otherwise
8221 * zero max_uA may cause unexpected behavior when regulator is
8222 * enabled or set as high power mode.
8223 */
8224 if (!vreg->max_uA)
8225 return 0;
8226
Bjorn Andersson7b16a072015-02-11 19:35:28 -08008227 ret = regulator_set_load(vreg->reg, ua);
8228 if (ret < 0) {
8229 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
8230 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008231 }
8232
8233 return ret;
8234}
8235
8236static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
8237 struct ufs_vreg *vreg)
8238{
Marc Gonzalez73067982019-02-27 11:41:45 +01008239 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008240}
8241
8242static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
8243 struct ufs_vreg *vreg)
8244{
Adrian Hunter7c7cfdc2019-08-14 15:59:50 +03008245 if (!vreg)
8246 return 0;
8247
Marc Gonzalez73067982019-02-27 11:41:45 +01008248 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008249}
8250
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008251static int ufshcd_config_vreg(struct device *dev,
8252 struct ufs_vreg *vreg, bool on)
8253{
8254 int ret = 0;
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06008255 struct regulator *reg;
8256 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008257 int min_uV, uA_load;
8258
8259 BUG_ON(!vreg);
8260
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06008261 reg = vreg->reg;
8262 name = vreg->name;
8263
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008264 if (regulator_count_voltages(reg) > 0) {
Asutosh Das90d88f42020-02-10 19:40:45 -08008265 uA_load = on ? vreg->max_uA : 0;
8266 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
8267 if (ret)
8268 goto out;
8269
Stanley Chu3b141e82019-03-28 17:16:24 +08008270 if (vreg->min_uV && vreg->max_uV) {
8271 min_uV = on ? vreg->min_uV : 0;
8272 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
Bean Huob0008622020-08-14 11:50:34 +02008273 if (ret)
Stanley Chu3b141e82019-03-28 17:16:24 +08008274 dev_err(dev,
8275 "%s: %s set voltage failed, err=%d\n",
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008276 __func__, name, ret);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008277 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008278 }
8279out:
8280 return ret;
8281}
8282
8283static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
8284{
8285 int ret = 0;
8286
Marc Gonzalez73067982019-02-27 11:41:45 +01008287 if (!vreg || vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008288 goto out;
8289
8290 ret = ufshcd_config_vreg(dev, vreg, true);
8291 if (!ret)
8292 ret = regulator_enable(vreg->reg);
8293
8294 if (!ret)
8295 vreg->enabled = true;
8296 else
8297 dev_err(dev, "%s: %s enable failed, err=%d\n",
8298 __func__, vreg->name, ret);
8299out:
8300 return ret;
8301}
8302
8303static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
8304{
8305 int ret = 0;
8306
Stanley Chuf8162ac2020-12-07 13:49:54 +08008307 if (!vreg || !vreg->enabled || vreg->always_on)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008308 goto out;
8309
8310 ret = regulator_disable(vreg->reg);
8311
8312 if (!ret) {
8313 /* ignore errors on applying disable config */
8314 ufshcd_config_vreg(dev, vreg, false);
8315 vreg->enabled = false;
8316 } else {
8317 dev_err(dev, "%s: %s disable failed, err=%d\n",
8318 __func__, vreg->name, ret);
8319 }
8320out:
8321 return ret;
8322}
8323
8324static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
8325{
8326 int ret = 0;
8327 struct device *dev = hba->dev;
8328 struct ufs_vreg_info *info = &hba->vreg_info;
8329
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008330 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
8331 if (ret)
8332 goto out;
8333
8334 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
8335 if (ret)
8336 goto out;
8337
8338 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008339
8340out:
8341 if (ret) {
8342 ufshcd_toggle_vreg(dev, info->vccq2, false);
8343 ufshcd_toggle_vreg(dev, info->vccq, false);
8344 ufshcd_toggle_vreg(dev, info->vcc, false);
8345 }
8346 return ret;
8347}
8348
Raviv Shvili6a771a62014-09-25 15:32:24 +03008349static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
8350{
8351 struct ufs_vreg_info *info = &hba->vreg_info;
8352
Zeng Guangyue60b7b822019-03-30 17:03:13 +08008353 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03008354}
8355
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008356static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
8357{
8358 int ret = 0;
8359
8360 if (!vreg)
8361 goto out;
8362
8363 vreg->reg = devm_regulator_get(dev, vreg->name);
8364 if (IS_ERR(vreg->reg)) {
8365 ret = PTR_ERR(vreg->reg);
8366 dev_err(dev, "%s: %s get failed, err=%d\n",
8367 __func__, vreg->name, ret);
8368 }
8369out:
8370 return ret;
8371}
8372
8373static int ufshcd_init_vreg(struct ufs_hba *hba)
8374{
8375 int ret = 0;
8376 struct device *dev = hba->dev;
8377 struct ufs_vreg_info *info = &hba->vreg_info;
8378
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008379 ret = ufshcd_get_vreg(dev, info->vcc);
8380 if (ret)
8381 goto out;
8382
8383 ret = ufshcd_get_vreg(dev, info->vccq);
Bean Huob0008622020-08-14 11:50:34 +02008384 if (!ret)
8385 ret = ufshcd_get_vreg(dev, info->vccq2);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008386out:
8387 return ret;
8388}
8389
Raviv Shvili6a771a62014-09-25 15:32:24 +03008390static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
8391{
8392 struct ufs_vreg_info *info = &hba->vreg_info;
8393
8394 if (info)
8395 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
8396
8397 return 0;
8398}
8399
Can Guo81309c22020-11-25 18:01:00 -08008400static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008401{
8402 int ret = 0;
8403 struct ufs_clk_info *clki;
8404 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008405 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08008406 ktime_t start = ktime_get();
8407 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008408
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03008409 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008410 goto out;
8411
Can Guo38f32422020-02-10 19:40:47 -08008412 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
8413 if (ret)
8414 return ret;
Subhash Jadavani1e879e82016-10-06 21:48:22 -07008415
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008416 list_for_each_entry(clki, head, list) {
8417 if (!IS_ERR_OR_NULL(clki->clk)) {
Can Guo81309c22020-11-25 18:01:00 -08008418 /*
8419 * Don't disable clocks which are needed
8420 * to keep the link active.
8421 */
8422 if (ufshcd_is_link_active(hba) &&
8423 clki->keep_link_active)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008424 continue;
8425
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08008426 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008427 if (on && !clki->enabled) {
8428 ret = clk_prepare_enable(clki->clk);
8429 if (ret) {
8430 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
8431 __func__, clki->name, ret);
8432 goto out;
8433 }
8434 } else if (!on && clki->enabled) {
8435 clk_disable_unprepare(clki->clk);
8436 }
8437 clki->enabled = on;
8438 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
8439 clki->name, on ? "en" : "dis");
8440 }
8441 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008442
Can Guo38f32422020-02-10 19:40:47 -08008443 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
8444 if (ret)
8445 return ret;
Subhash Jadavani1e879e82016-10-06 21:48:22 -07008446
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008447out:
8448 if (ret) {
8449 list_for_each_entry(clki, head, list) {
8450 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
8451 clk_disable_unprepare(clki->clk);
8452 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008453 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008454 spin_lock_irqsave(hba->host->host_lock, flags);
8455 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008456 trace_ufshcd_clk_gating(dev_name(hba->dev),
8457 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008458 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008459 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008460
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08008461 if (clk_state_changed)
8462 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
8463 (on ? "on" : "off"),
8464 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008465 return ret;
8466}
8467
8468static int ufshcd_init_clocks(struct ufs_hba *hba)
8469{
8470 int ret = 0;
8471 struct ufs_clk_info *clki;
8472 struct device *dev = hba->dev;
8473 struct list_head *head = &hba->clk_list_head;
8474
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03008475 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008476 goto out;
8477
8478 list_for_each_entry(clki, head, list) {
8479 if (!clki->name)
8480 continue;
8481
8482 clki->clk = devm_clk_get(dev, clki->name);
8483 if (IS_ERR(clki->clk)) {
8484 ret = PTR_ERR(clki->clk);
8485 dev_err(dev, "%s: %s clk get failed, %d\n",
8486 __func__, clki->name, ret);
8487 goto out;
8488 }
8489
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05308490 /*
8491 * Parse device ref clk freq as per device tree "ref_clk".
8492 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
8493 * in ufshcd_alloc_host().
8494 */
8495 if (!strcmp(clki->name, "ref_clk"))
8496 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
8497
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008498 if (clki->max_freq) {
8499 ret = clk_set_rate(clki->clk, clki->max_freq);
8500 if (ret) {
8501 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
8502 __func__, clki->name,
8503 clki->max_freq, ret);
8504 goto out;
8505 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03008506 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008507 }
8508 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
8509 clki->name, clk_get_rate(clki->clk));
8510 }
8511out:
8512 return ret;
8513}
8514
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008515static int ufshcd_variant_hba_init(struct ufs_hba *hba)
8516{
8517 int err = 0;
8518
8519 if (!hba->vops)
8520 goto out;
8521
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008522 err = ufshcd_vops_init(hba);
8523 if (err)
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008524 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008525 __func__, ufshcd_get_var_name(hba), err);
Stanley Chuade921a2020-12-05 20:00:38 +08008526out:
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008527 return err;
8528}
8529
8530static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
8531{
8532 if (!hba->vops)
8533 return;
8534
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008535 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008536}
8537
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008538static int ufshcd_hba_init(struct ufs_hba *hba)
8539{
8540 int err;
8541
Raviv Shvili6a771a62014-09-25 15:32:24 +03008542 /*
8543 * Handle host controller power separately from the UFS device power
8544 * rails as it will help controlling the UFS host controller power
8545 * collapse easily which is different than UFS device power collapse.
8546 * Also, enable the host controller power before we go ahead with rest
8547 * of the initialization here.
8548 */
8549 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008550 if (err)
8551 goto out;
8552
Raviv Shvili6a771a62014-09-25 15:32:24 +03008553 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008554 if (err)
8555 goto out;
8556
Raviv Shvili6a771a62014-09-25 15:32:24 +03008557 err = ufshcd_init_clocks(hba);
8558 if (err)
8559 goto out_disable_hba_vreg;
8560
8561 err = ufshcd_setup_clocks(hba, true);
8562 if (err)
8563 goto out_disable_hba_vreg;
8564
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008565 err = ufshcd_init_vreg(hba);
8566 if (err)
8567 goto out_disable_clks;
8568
8569 err = ufshcd_setup_vreg(hba, true);
8570 if (err)
8571 goto out_disable_clks;
8572
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008573 err = ufshcd_variant_hba_init(hba);
8574 if (err)
8575 goto out_disable_vreg;
8576
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02008577 ufs_debugfs_hba_init(hba);
8578
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008579 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008580 goto out;
8581
8582out_disable_vreg:
8583 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008584out_disable_clks:
8585 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03008586out_disable_hba_vreg:
8587 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008588out:
8589 return err;
8590}
8591
8592static void ufshcd_hba_exit(struct ufs_hba *hba)
8593{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008594 if (hba->is_powered) {
Can Guo4543d9d2021-01-20 02:04:22 -08008595 ufshcd_exit_clk_scaling(hba);
8596 ufshcd_exit_clk_gating(hba);
Adrian Hunter88b099002021-09-17 17:43:49 +03008597 if (hba->eh_wq)
8598 destroy_workqueue(hba->eh_wq);
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02008599 ufs_debugfs_hba_exit(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008600 ufshcd_variant_hba_exit(hba);
8601 ufshcd_setup_vreg(hba, false);
8602 ufshcd_setup_clocks(hba, false);
8603 ufshcd_setup_hba_vreg(hba, false);
8604 hba->is_powered = false;
Bean Huo09750062020-01-20 14:08:14 +01008605 ufs_put_device_desc(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008606 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008607}
8608
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308609/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008610 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
8611 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308612 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008613 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308614 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008615 * Returns 0 if requested power mode is set successfully
Kiwoong Kimad6c8a42022-01-21 14:33:02 +09008616 * Returns < 0 if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308617 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008618static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
8619 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308620{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008621 unsigned char cmd[6] = { START_STOP };
8622 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008623 struct scsi_device *sdp;
8624 unsigned long flags;
Jaegeuk Kimaf21c3f2021-10-01 11:20:14 -07008625 int ret, retries;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008626
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008627 spin_lock_irqsave(hba->host->host_lock, flags);
8628 sdp = hba->sdev_ufs_device;
8629 if (sdp) {
8630 ret = scsi_device_get(sdp);
8631 if (!ret && !scsi_device_online(sdp)) {
8632 ret = -ENODEV;
8633 scsi_device_put(sdp);
8634 }
8635 } else {
8636 ret = -ENODEV;
8637 }
8638 spin_unlock_irqrestore(hba->host->host_lock, flags);
8639
8640 if (ret)
8641 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008642
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308643 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008644 * If scsi commands fail, the scsi mid-layer schedules scsi error-
8645 * handling, which would wait for host to be resumed. Since we know
8646 * we are functional while we are here, skip host resume in error
8647 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308648 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008649 hba->host->eh_noresume = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308650
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008651 cmd[4] = pwr_mode << 4;
8652
8653 /*
8654 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02008655 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008656 * already suspended childs.
8657 */
Jaegeuk Kimaf21c3f2021-10-01 11:20:14 -07008658 for (retries = 3; retries > 0; --retries) {
8659 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
8660 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
8661 if (!scsi_status_is_check_condition(ret) ||
8662 !scsi_sense_valid(&sshdr) ||
8663 sshdr.sense_key != UNIT_ATTENTION)
8664 break;
8665 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008666 if (ret) {
8667 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02008668 "START_STOP failed for power mode: %d, result %x\n",
8669 pwr_mode, ret);
Kiwoong Kimad6c8a42022-01-21 14:33:02 +09008670 if (ret > 0) {
8671 if (scsi_sense_valid(&sshdr))
8672 scsi_print_sense_hdr(sdp, NULL, &sshdr);
8673 ret = -EIO;
8674 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008675 }
8676
8677 if (!ret)
8678 hba->curr_dev_pwr_mode = pwr_mode;
Randall Huang19186512020-11-30 20:14:02 -08008679
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008680 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008681 hba->host->eh_noresume = 0;
8682 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308683}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308684
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008685static int ufshcd_link_state_transition(struct ufs_hba *hba,
8686 enum uic_link_state req_link_state,
8687 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308688{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008689 int ret = 0;
8690
8691 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308692 return 0;
8693
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008694 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
8695 ret = ufshcd_uic_hibern8_enter(hba);
Can Guo4db7a232020-08-09 05:15:51 -07008696 if (!ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008697 ufshcd_set_link_hibern8(hba);
Can Guo4db7a232020-08-09 05:15:51 -07008698 } else {
8699 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
8700 __func__, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008701 goto out;
Can Guo4db7a232020-08-09 05:15:51 -07008702 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008703 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308704 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008705 * If autobkops is enabled, link can't be turned off because
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008706 * turning off the link would also turn off the device, except in the
8707 * case of DeepSleep where the device is expected to remain powered.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308708 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008709 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
Dan Carpenterdc30c9e2019-12-13 13:49:35 +03008710 (!check_for_bkops || !hba->auto_bkops_enabled)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008711 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008712 * Let's make sure that link is in low power mode, we are doing
8713 * this currently by putting the link in Hibern8. Otherway to
8714 * put the link in low power mode is to send the DME end point
8715 * to device and then send the DME reset command to local
8716 * unipro. But putting the link in hibern8 is much faster.
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008717 *
8718 * Note also that putting the link in Hibern8 is a requirement
8719 * for entering DeepSleep.
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008720 */
8721 ret = ufshcd_uic_hibern8_enter(hba);
Can Guo4db7a232020-08-09 05:15:51 -07008722 if (ret) {
8723 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
8724 __func__, ret);
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008725 goto out;
Can Guo4db7a232020-08-09 05:15:51 -07008726 }
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008727 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008728 * Change controller state to "reset state" which
8729 * should also put the link in off/reset state
8730 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07008731 ufshcd_hba_stop(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008732 /*
8733 * TODO: Check if we need any delay to make sure that
8734 * controller is reset
8735 */
8736 ufshcd_set_link_off(hba);
8737 }
8738
8739out:
8740 return ret;
8741}
8742
8743static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
8744{
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008745 bool vcc_off = false;
8746
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008747 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02008748 * It seems some UFS devices may keep drawing more than sleep current
8749 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
8750 * To avoid this situation, add 2ms delay before putting these UFS
8751 * rails in LPM mode.
8752 */
8753 if (!ufshcd_is_link_active(hba) &&
8754 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
8755 usleep_range(2000, 2100);
8756
8757 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008758 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
8759 * power.
8760 *
8761 * If UFS device and link is in OFF state, all power supplies (VCC,
8762 * VCCQ, VCCQ2) can be turned off if power on write protect is not
8763 * required. If UFS link is inactive (Hibern8 or OFF state) and device
8764 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
8765 *
8766 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
8767 * in low power state which would save some power.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008768 *
8769 * If Write Booster is enabled and the device needs to flush the WB
8770 * buffer OR if bkops status is urgent for WB, keep Vcc on.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008771 */
8772 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8773 !hba->dev_info.is_lu_power_on_wp) {
8774 ufshcd_setup_vreg(hba, false);
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008775 vcc_off = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008776 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Stanley Chu51dd9052020-05-22 16:32:12 +08008777 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008778 vcc_off = true;
Can Guo23043dd2021-04-25 20:48:38 -07008779 if (ufshcd_is_link_hibern8(hba) || ufshcd_is_link_off(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008780 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8781 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
8782 }
8783 }
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008784
8785 /*
8786 * Some UFS devices require delay after VCC power rail is turned-off.
8787 */
8788 if (vcc_off && hba->vreg_info.vcc &&
8789 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)
8790 usleep_range(5000, 5100);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008791}
8792
Bart Van Assche9bb25e52021-07-21 20:34:24 -07008793#ifdef CONFIG_PM
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008794static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
8795{
8796 int ret = 0;
8797
8798 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8799 !hba->dev_info.is_lu_power_on_wp) {
8800 ret = ufshcd_setup_vreg(hba, true);
8801 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Can Guo23043dd2021-04-25 20:48:38 -07008802 if (!ufshcd_is_link_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008803 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
8804 if (ret)
8805 goto vcc_disable;
8806 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
8807 if (ret)
8808 goto vccq_lpm;
8809 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07008810 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008811 }
8812 goto out;
8813
8814vccq_lpm:
8815 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8816vcc_disable:
8817 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8818out:
8819 return ret;
8820}
Bart Van Assche9bb25e52021-07-21 20:34:24 -07008821#endif /* CONFIG_PM */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008822
8823static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
8824{
Can Guodd7143e2020-10-27 12:10:36 -07008825 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008826 ufshcd_setup_hba_vreg(hba, false);
8827}
8828
8829static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
8830{
Can Guodd7143e2020-10-27 12:10:36 -07008831 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008832 ufshcd_setup_hba_vreg(hba, true);
8833}
8834
Asutosh Dasb294ff32021-04-23 17:20:16 -07008835static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008836{
8837 int ret = 0;
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008838 int check_for_bkops;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008839 enum ufs_pm_level pm_lvl;
8840 enum ufs_dev_pwr_mode req_dev_pwr_mode;
8841 enum uic_link_state req_link_state;
8842
Asutosh Dasb294ff32021-04-23 17:20:16 -07008843 hba->pm_op_in_progress = true;
Bart Van Assche4c6cb9e2021-05-13 10:12:29 -07008844 if (pm_op != UFS_SHUTDOWN_PM) {
8845 pm_lvl = pm_op == UFS_RUNTIME_PM ?
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008846 hba->rpm_lvl : hba->spm_lvl;
8847 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
8848 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
8849 } else {
8850 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
8851 req_link_state = UIC_LINK_OFF_STATE;
8852 }
8853
Daejun Parkf02bc972021-07-12 17:58:30 +09008854 ufshpb_suspend(hba);
8855
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008856 /*
8857 * If we can't transition into any of the low power modes
8858 * just gate the clocks.
8859 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008860 ufshcd_hold(hba, false);
8861 hba->clk_gating.is_suspended = true;
8862
Stanley Chu348e1bc2021-01-20 23:01:42 +08008863 if (ufshcd_is_clkscaling_supported(hba))
8864 ufshcd_clk_scaling_suspend(hba, true);
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008865
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008866 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8867 req_link_state == UIC_LINK_ACTIVE_STATE) {
Asutosh Dasb294ff32021-04-23 17:20:16 -07008868 goto vops_suspend;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008869 }
8870
8871 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8872 (req_link_state == hba->uic_link_state))
Asutosh Dasb294ff32021-04-23 17:20:16 -07008873 goto enable_scaling;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008874
8875 /* UFS device & link must be active before we enter in this function */
8876 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8877 ret = -EINVAL;
Asutosh Dasb294ff32021-04-23 17:20:16 -07008878 goto enable_scaling;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008879 }
8880
Bart Van Assche4c6cb9e2021-05-13 10:12:29 -07008881 if (pm_op == UFS_RUNTIME_PM) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03008882 if (ufshcd_can_autobkops_during_suspend(hba)) {
8883 /*
8884 * The device is idle with no requests in the queue,
8885 * allow background operations if bkops status shows
8886 * that performance might be impacted.
8887 */
8888 ret = ufshcd_urgent_bkops(hba);
8889 if (ret)
Asutosh Dasb294ff32021-04-23 17:20:16 -07008890 goto enable_scaling;
Subhash Jadavani374a2462014-09-25 15:32:35 +03008891 } else {
8892 /* make sure that auto bkops is disabled */
8893 ufshcd_disable_auto_bkops(hba);
8894 }
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008895 /*
Stanley Chu51dd9052020-05-22 16:32:12 +08008896 * If device needs to do BKOP or WB buffer flush during
8897 * Hibern8, keep device power mode as "active power mode"
8898 * and VCC supply.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008899 */
Stanley Chu51dd9052020-05-22 16:32:12 +08008900 hba->dev_info.b_rpm_dev_flush_capable =
8901 hba->auto_bkops_enabled ||
8902 (((req_link_state == UIC_LINK_HIBERN8_STATE) ||
8903 ((req_link_state == UIC_LINK_ACTIVE_STATE) &&
8904 ufshcd_is_auto_hibern8_enabled(hba))) &&
8905 ufshcd_wb_need_flush(hba));
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008906 }
8907
Kiwoong Kim6948a962020-12-19 15:40:39 +09008908 flush_work(&hba->eeh_work);
8909
Peter Wang9561f582021-10-06 13:47:05 +08008910 ret = ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE);
8911 if (ret)
8912 goto enable_scaling;
8913
Stanley Chu51dd9052020-05-22 16:32:12 +08008914 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) {
Bart Van Assche4c6cb9e2021-05-13 10:12:29 -07008915 if (pm_op != UFS_RUNTIME_PM)
Stanley Chu51dd9052020-05-22 16:32:12 +08008916 /* ensure that bkops is disabled */
8917 ufshcd_disable_auto_bkops(hba);
Stanley Chu51dd9052020-05-22 16:32:12 +08008918
8919 if (!hba->dev_info.b_rpm_dev_flush_capable) {
8920 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8921 if (ret)
Asutosh Dasb294ff32021-04-23 17:20:16 -07008922 goto enable_scaling;
Stanley Chu51dd9052020-05-22 16:32:12 +08008923 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008924 }
8925
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008926 /*
8927 * In the case of DeepSleep, the device is expected to remain powered
8928 * with the link off, so do not check for bkops.
8929 */
8930 check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba);
8931 ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008932 if (ret)
8933 goto set_dev_active;
8934
Asutosh Dasb294ff32021-04-23 17:20:16 -07008935vops_suspend:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008936 /*
8937 * Call vendor specific suspend callback. As these callbacks may access
8938 * vendor specific host controller register space call them before the
8939 * host clocks are ON.
8940 */
Peter Wang9561f582021-10-06 13:47:05 +08008941 ret = ufshcd_vops_suspend(hba, pm_op, POST_CHANGE);
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008942 if (ret)
8943 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008944 goto out;
8945
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008946set_link_active:
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008947 /*
8948 * Device hardware reset is required to exit DeepSleep. Also, for
8949 * DeepSleep, the link is off so host reset and restore will be done
8950 * further below.
8951 */
8952 if (ufshcd_is_ufs_dev_deepsleep(hba)) {
Stanley Chu31a5d9c2020-12-08 21:56:35 +08008953 ufshcd_device_reset(hba);
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008954 WARN_ON(!ufshcd_is_link_off(hba));
8955 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008956 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
8957 ufshcd_set_link_active(hba);
8958 else if (ufshcd_is_link_off(hba))
8959 ufshcd_host_reset_and_restore(hba);
8960set_dev_active:
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008961 /* Can also get here needing to exit DeepSleep */
8962 if (ufshcd_is_ufs_dev_deepsleep(hba)) {
Stanley Chu31a5d9c2020-12-08 21:56:35 +08008963 ufshcd_device_reset(hba);
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008964 ufshcd_host_reset_and_restore(hba);
8965 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008966 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8967 ufshcd_disable_auto_bkops(hba);
Asutosh Dasb294ff32021-04-23 17:20:16 -07008968enable_scaling:
Stanley Chu348e1bc2021-01-20 23:01:42 +08008969 if (ufshcd_is_clkscaling_supported(hba))
8970 ufshcd_clk_scaling_suspend(hba, false);
8971
Stanley Chu51dd9052020-05-22 16:32:12 +08008972 hba->dev_info.b_rpm_dev_flush_capable = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008973out:
Stanley Chu51dd9052020-05-22 16:32:12 +08008974 if (hba->dev_info.b_rpm_dev_flush_capable) {
8975 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work,
8976 msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS));
8977 }
8978
Asutosh Dasb294ff32021-04-23 17:20:16 -07008979 if (ret) {
8980 ufshcd_update_evt_hist(hba, UFS_EVT_WL_SUSP_ERR, (u32)ret);
8981 hba->clk_gating.is_suspended = false;
8982 ufshcd_release(hba);
Daejun Parkf02bc972021-07-12 17:58:30 +09008983 ufshpb_resume(hba);
Asutosh Dasb294ff32021-04-23 17:20:16 -07008984 }
8985 hba->pm_op_in_progress = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008986 return ret;
8987}
8988
YueHaibing75d645a2021-06-17 11:13:26 +08008989#ifdef CONFIG_PM
Asutosh Dasb294ff32021-04-23 17:20:16 -07008990static int __ufshcd_wl_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008991{
8992 int ret;
Asutosh Dasb294ff32021-04-23 17:20:16 -07008993 enum uic_link_state old_link_state = hba->uic_link_state;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008994
Asutosh Dasb294ff32021-04-23 17:20:16 -07008995 hba->pm_op_in_progress = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008996
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008997 /*
8998 * Call vendor specific resume callback. As these callbacks may access
8999 * vendor specific host controller register space call them when the
9000 * host clocks are ON.
9001 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02009002 ret = ufshcd_vops_resume(hba, pm_op);
9003 if (ret)
Asutosh Dasb294ff32021-04-23 17:20:16 -07009004 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009005
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02009006 /* For DeepSleep, the only supported option is to have the link off */
9007 WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba));
9008
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009009 if (ufshcd_is_link_hibern8(hba)) {
9010 ret = ufshcd_uic_hibern8_exit(hba);
Can Guo4db7a232020-08-09 05:15:51 -07009011 if (!ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009012 ufshcd_set_link_active(hba);
Can Guo4db7a232020-08-09 05:15:51 -07009013 } else {
9014 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
9015 __func__, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009016 goto vendor_suspend;
Can Guo4db7a232020-08-09 05:15:51 -07009017 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009018 } else if (ufshcd_is_link_off(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009019 /*
Asutosh Das089f5b62020-04-13 23:14:48 -07009020 * A full initialization of the host and the device is
9021 * required since the link was put to off during suspend.
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02009022 * Note, in the case of DeepSleep, the device will exit
9023 * DeepSleep due to device reset.
Asutosh Das089f5b62020-04-13 23:14:48 -07009024 */
9025 ret = ufshcd_reset_and_restore(hba);
9026 /*
9027 * ufshcd_reset_and_restore() should have already
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009028 * set the link state as active
9029 */
9030 if (ret || !ufshcd_is_link_active(hba))
9031 goto vendor_suspend;
9032 }
9033
9034 if (!ufshcd_is_ufs_dev_active(hba)) {
9035 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
9036 if (ret)
9037 goto set_old_link_state;
9038 }
9039
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08009040 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
9041 ufshcd_enable_auto_bkops(hba);
9042 else
9043 /*
9044 * If BKOPs operations are urgently needed at this moment then
9045 * keep auto-bkops enabled or else disable it.
9046 */
9047 ufshcd_urgent_bkops(hba);
9048
Adrian Huntercd469472021-02-09 08:24:36 +02009049 if (hba->ee_usr_mask)
9050 ufshcd_write_ee_control(hba);
9051
Stanley Chu348e1bc2021-01-20 23:01:42 +08009052 if (ufshcd_is_clkscaling_supported(hba))
9053 ufshcd_clk_scaling_suspend(hba, false);
Sahitya Tummala856b3482014-09-25 15:32:34 +03009054
Stanley Chu51dd9052020-05-22 16:32:12 +08009055 if (hba->dev_info.b_rpm_dev_flush_capable) {
9056 hba->dev_info.b_rpm_dev_flush_capable = false;
9057 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
9058 }
9059
Asutosh Dasb294ff32021-04-23 17:20:16 -07009060 /* Enable Auto-Hibernate if configured */
9061 ufshcd_auto_hibern8_enable(hba);
Daejun Parkf02bc972021-07-12 17:58:30 +09009062
9063 ufshpb_resume(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009064 goto out;
9065
9066set_old_link_state:
9067 ufshcd_link_state_transition(hba, old_link_state, 0);
9068vendor_suspend:
Peter Wang9561f582021-10-06 13:47:05 +08009069 ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE);
9070 ufshcd_vops_suspend(hba, pm_op, POST_CHANGE);
Asutosh Dasb294ff32021-04-23 17:20:16 -07009071out:
9072 if (ret)
9073 ufshcd_update_evt_hist(hba, UFS_EVT_WL_RES_ERR, (u32)ret);
9074 hba->clk_gating.is_suspended = false;
9075 ufshcd_release(hba);
9076 hba->pm_op_in_progress = false;
9077 return ret;
9078}
9079
9080static int ufshcd_wl_runtime_suspend(struct device *dev)
9081{
9082 struct scsi_device *sdev = to_scsi_device(dev);
9083 struct ufs_hba *hba;
9084 int ret;
9085 ktime_t start = ktime_get();
9086
9087 hba = shost_priv(sdev->host);
9088
9089 ret = __ufshcd_wl_suspend(hba, UFS_RUNTIME_PM);
9090 if (ret)
9091 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret);
9092
9093 trace_ufshcd_wl_runtime_suspend(dev_name(dev), ret,
9094 ktime_to_us(ktime_sub(ktime_get(), start)),
9095 hba->curr_dev_pwr_mode, hba->uic_link_state);
9096
9097 return ret;
9098}
9099
9100static int ufshcd_wl_runtime_resume(struct device *dev)
9101{
9102 struct scsi_device *sdev = to_scsi_device(dev);
9103 struct ufs_hba *hba;
9104 int ret = 0;
9105 ktime_t start = ktime_get();
9106
9107 hba = shost_priv(sdev->host);
9108
9109 ret = __ufshcd_wl_resume(hba, UFS_RUNTIME_PM);
9110 if (ret)
9111 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret);
9112
9113 trace_ufshcd_wl_runtime_resume(dev_name(dev), ret,
9114 ktime_to_us(ktime_sub(ktime_get(), start)),
9115 hba->curr_dev_pwr_mode, hba->uic_link_state);
9116
9117 return ret;
9118}
YueHaibing75d645a2021-06-17 11:13:26 +08009119#endif
Asutosh Dasb294ff32021-04-23 17:20:16 -07009120
9121#ifdef CONFIG_PM_SLEEP
9122static int ufshcd_wl_suspend(struct device *dev)
9123{
9124 struct scsi_device *sdev = to_scsi_device(dev);
9125 struct ufs_hba *hba;
9126 int ret = 0;
9127 ktime_t start = ktime_get();
9128
9129 hba = shost_priv(sdev->host);
9130 down(&hba->host_sem);
9131
9132 if (pm_runtime_suspended(dev))
9133 goto out;
9134
9135 ret = __ufshcd_wl_suspend(hba, UFS_SYSTEM_PM);
9136 if (ret) {
9137 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret);
9138 up(&hba->host_sem);
9139 }
9140
9141out:
9142 if (!ret)
9143 hba->is_sys_suspended = true;
9144 trace_ufshcd_wl_suspend(dev_name(dev), ret,
9145 ktime_to_us(ktime_sub(ktime_get(), start)),
9146 hba->curr_dev_pwr_mode, hba->uic_link_state);
9147
9148 return ret;
9149}
9150
9151static int ufshcd_wl_resume(struct device *dev)
9152{
9153 struct scsi_device *sdev = to_scsi_device(dev);
9154 struct ufs_hba *hba;
9155 int ret = 0;
9156 ktime_t start = ktime_get();
9157
9158 hba = shost_priv(sdev->host);
9159
9160 if (pm_runtime_suspended(dev))
9161 goto out;
9162
9163 ret = __ufshcd_wl_resume(hba, UFS_SYSTEM_PM);
9164 if (ret)
9165 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret);
9166out:
9167 trace_ufshcd_wl_resume(dev_name(dev), ret,
9168 ktime_to_us(ktime_sub(ktime_get(), start)),
9169 hba->curr_dev_pwr_mode, hba->uic_link_state);
9170 if (!ret)
9171 hba->is_sys_suspended = false;
9172 up(&hba->host_sem);
9173 return ret;
9174}
9175#endif
9176
9177static void ufshcd_wl_shutdown(struct device *dev)
9178{
9179 struct scsi_device *sdev = to_scsi_device(dev);
9180 struct ufs_hba *hba;
9181
9182 hba = shost_priv(sdev->host);
9183
9184 down(&hba->host_sem);
9185 hba->shutting_down = true;
9186 up(&hba->host_sem);
9187
9188 /* Turn on everything while shutting down */
9189 ufshcd_rpm_get_sync(hba);
9190 scsi_device_quiesce(sdev);
9191 shost_for_each_device(sdev, hba->host) {
9192 if (sdev == hba->sdev_ufs_device)
9193 continue;
9194 scsi_device_quiesce(sdev);
9195 }
9196 __ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM);
9197}
9198
9199/**
9200 * ufshcd_suspend - helper function for suspend operations
9201 * @hba: per adapter instance
9202 *
9203 * This function will put disable irqs, turn off clocks
9204 * and set vreg and hba-vreg in lpm mode.
Asutosh Dasb294ff32021-04-23 17:20:16 -07009205 */
9206static int ufshcd_suspend(struct ufs_hba *hba)
9207{
9208 int ret;
9209
9210 if (!hba->is_powered)
9211 return 0;
9212 /*
9213 * Disable the host irq as host controller as there won't be any
9214 * host controller transaction expected till resume.
9215 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009216 ufshcd_disable_irq(hba);
Asutosh Dasb294ff32021-04-23 17:20:16 -07009217 ret = ufshcd_setup_clocks(hba, false);
9218 if (ret) {
9219 ufshcd_enable_irq(hba);
9220 return ret;
9221 }
Can Guo2dec9472020-08-09 05:15:47 -07009222 if (ufshcd_is_clkgating_allowed(hba)) {
9223 hba->clk_gating.state = CLKS_OFF;
9224 trace_ufshcd_clk_gating(dev_name(hba->dev),
9225 hba->clk_gating.state);
9226 }
Asutosh Dasb294ff32021-04-23 17:20:16 -07009227
9228 ufshcd_vreg_set_lpm(hba);
9229 /* Put the host controller in low power mode if possible */
9230 ufshcd_hba_vreg_set_lpm(hba);
9231 return ret;
9232}
9233
Bart Van Assche9bb25e52021-07-21 20:34:24 -07009234#ifdef CONFIG_PM
Asutosh Dasb294ff32021-04-23 17:20:16 -07009235/**
9236 * ufshcd_resume - helper function for resume operations
9237 * @hba: per adapter instance
9238 *
9239 * This function basically turns on the regulators, clocks and
9240 * irqs of the hba.
Asutosh Dasb294ff32021-04-23 17:20:16 -07009241 *
9242 * Returns 0 for success and non-zero for failure
9243 */
9244static int ufshcd_resume(struct ufs_hba *hba)
9245{
9246 int ret;
9247
9248 if (!hba->is_powered)
9249 return 0;
9250
9251 ufshcd_hba_vreg_set_hpm(hba);
9252 ret = ufshcd_vreg_set_hpm(hba);
9253 if (ret)
9254 goto out;
9255
9256 /* Make sure clocks are enabled before accessing controller */
9257 ret = ufshcd_setup_clocks(hba, true);
9258 if (ret)
9259 goto disable_vreg;
9260
9261 /* enable the host irq as host controller would be active soon */
9262 ufshcd_enable_irq(hba);
9263 goto out;
9264
Ziqi Chen528db9e2021-01-08 18:56:24 +08009265disable_vreg:
9266 ufshcd_vreg_set_lpm(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009267out:
Stanley Chu8808b4e2019-07-10 21:38:21 +08009268 if (ret)
Stanley Chue965e5e2020-12-05 19:58:59 +08009269 ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009270 return ret;
9271}
Bart Van Assche9bb25e52021-07-21 20:34:24 -07009272#endif /* CONFIG_PM */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009273
Bart Van Assche9bb25e52021-07-21 20:34:24 -07009274#ifdef CONFIG_PM_SLEEP
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009275/**
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009276 * ufshcd_system_suspend - system suspend callback
9277 * @dev: Device associated with the UFS controller.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009278 *
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009279 * Executed before putting the system into a sleep state in which the contents
9280 * of main memory are preserved.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009281 *
9282 * Returns 0 for success and non-zero for failure
9283 */
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009284int ufshcd_system_suspend(struct device *dev)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009285{
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009286 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009287 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009288 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009289
Asutosh Dasb294ff32021-04-23 17:20:16 -07009290 if (pm_runtime_suspended(hba->dev))
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08009291 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009292
Asutosh Dasb294ff32021-04-23 17:20:16 -07009293 ret = ufshcd_suspend(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009294out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009295 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
9296 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08009297 hba->curr_dev_pwr_mode, hba->uic_link_state);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009298 return ret;
9299}
9300EXPORT_SYMBOL(ufshcd_system_suspend);
9301
9302/**
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009303 * ufshcd_system_resume - system resume callback
9304 * @dev: Device associated with the UFS controller.
9305 *
9306 * Executed after waking the system up from a sleep state in which the contents
9307 * of main memory were preserved.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009308 *
9309 * Returns 0 for success and non-zero for failure
9310 */
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009311int ufshcd_system_resume(struct device *dev)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009312{
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009313 struct ufs_hba *hba = dev_get_drvdata(dev);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009314 ktime_t start = ktime_get();
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009315 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009316
Asutosh Dasb294ff32021-04-23 17:20:16 -07009317 if (pm_runtime_suspended(hba->dev))
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009318 goto out;
Asutosh Dasb294ff32021-04-23 17:20:16 -07009319
9320 ret = ufshcd_resume(hba);
9321
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009322out:
9323 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
9324 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08009325 hba->curr_dev_pwr_mode, hba->uic_link_state);
Asutosh Dasb294ff32021-04-23 17:20:16 -07009326
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009327 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009328}
9329EXPORT_SYMBOL(ufshcd_system_resume);
Bart Van Assche9bb25e52021-07-21 20:34:24 -07009330#endif /* CONFIG_PM_SLEEP */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009331
Bart Van Assche9bb25e52021-07-21 20:34:24 -07009332#ifdef CONFIG_PM
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009333/**
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009334 * ufshcd_runtime_suspend - runtime suspend callback
9335 * @dev: Device associated with the UFS controller.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009336 *
9337 * Check the description of ufshcd_suspend() function for more details.
9338 *
9339 * Returns 0 for success and non-zero for failure
9340 */
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009341int ufshcd_runtime_suspend(struct device *dev)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009342{
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009343 struct ufs_hba *hba = dev_get_drvdata(dev);
Asutosh Dasb294ff32021-04-23 17:20:16 -07009344 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009345 ktime_t start = ktime_get();
9346
Asutosh Dasb294ff32021-04-23 17:20:16 -07009347 ret = ufshcd_suspend(hba);
9348
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009349 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
9350 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08009351 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009352 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309353}
9354EXPORT_SYMBOL(ufshcd_runtime_suspend);
9355
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009356/**
9357 * ufshcd_runtime_resume - runtime resume routine
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009358 * @dev: Device associated with the UFS controller.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009359 *
Asutosh Dasb294ff32021-04-23 17:20:16 -07009360 * This function basically brings controller
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009361 * to active state. Following operations are done in this function:
9362 *
9363 * 1. Turn on all the controller related clocks
Asutosh Dasb294ff32021-04-23 17:20:16 -07009364 * 2. Turn ON VCC rail
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009365 */
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009366int ufshcd_runtime_resume(struct device *dev)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309367{
Bart Van Asschef1ecbe12021-07-21 20:34:23 -07009368 struct ufs_hba *hba = dev_get_drvdata(dev);
Asutosh Dasb294ff32021-04-23 17:20:16 -07009369 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009370 ktime_t start = ktime_get();
9371
Asutosh Dasb294ff32021-04-23 17:20:16 -07009372 ret = ufshcd_resume(hba);
9373
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009374 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
9375 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08009376 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009377 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309378}
9379EXPORT_SYMBOL(ufshcd_runtime_resume);
Bart Van Assche9bb25e52021-07-21 20:34:24 -07009380#endif /* CONFIG_PM */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309381
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309382/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009383 * ufshcd_shutdown - shutdown routine
9384 * @hba: per adapter instance
9385 *
Asutosh Dasb294ff32021-04-23 17:20:16 -07009386 * This function would turn off both UFS device and UFS hba
9387 * regulators. It would also disable clocks.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009388 *
9389 * Returns 0 always to allow force shutdown even in case of errors.
9390 */
9391int ufshcd_shutdown(struct ufs_hba *hba)
9392{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009393 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
9394 goto out;
9395
Stanley Chue92643d2020-11-19 14:29:16 +08009396 pm_runtime_get_sync(hba->dev);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009397
Asutosh Dasb294ff32021-04-23 17:20:16 -07009398 ufshcd_suspend(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009399out:
Can Guo88a92d62020-12-02 04:04:01 -08009400 hba->is_powered = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009401 /* allow force shutdown even in case of errors */
9402 return 0;
9403}
9404EXPORT_SYMBOL(ufshcd_shutdown);
9405
9406/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309407 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309408 * data structure memory
Bart Van Assche8aa29f12018-03-01 15:07:20 -08009409 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309410 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309411void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309412{
Asutosh Dasb294ff32021-04-23 17:20:16 -07009413 if (hba->sdev_ufs_device)
9414 ufshcd_rpm_get_sync(hba);
Avri Altmane88e2d32021-09-15 09:04:06 +03009415 ufs_hwmon_remove(hba);
Avri Altmandf032bf2018-10-07 17:30:35 +03009416 ufs_bsg_remove(hba);
Daejun Park4b5f4902021-07-12 17:58:59 +09009417 ufshpb_remove(hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02009418 ufs_sysfs_remove_nodes(hba->dev);
Bart Van Assche69a6c262019-12-09 10:13:09 -08009419 blk_cleanup_queue(hba->tmf_queue);
9420 blk_mq_free_tag_set(&hba->tmf_tag_set);
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05309421 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309422 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05309423 ufshcd_disable_intr(hba, hba->intr_mask);
Bart Van Assche5cac1092020-05-07 15:27:50 -07009424 ufshcd_hba_stop(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009425 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309426}
9427EXPORT_SYMBOL_GPL(ufshcd_remove);
9428
9429/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02009430 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
9431 * @hba: pointer to Host Bus Adapter (HBA)
9432 */
9433void ufshcd_dealloc_host(struct ufs_hba *hba)
9434{
9435 scsi_host_put(hba->host);
9436}
9437EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
9438
9439/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09009440 * ufshcd_set_dma_mask - Set dma mask based on the controller
9441 * addressing capability
9442 * @hba: per adapter instance
9443 *
9444 * Returns 0 for success, non-zero for failure
9445 */
9446static int ufshcd_set_dma_mask(struct ufs_hba *hba)
9447{
9448 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
9449 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
9450 return 0;
9451 }
9452 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
9453}
9454
9455/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009456 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309457 * @dev: pointer to device handle
9458 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309459 * Returns 0 on success, non-zero value on failure
9460 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009461int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309462{
9463 struct Scsi_Host *host;
9464 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009465 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309466
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309467 if (!dev) {
9468 dev_err(dev,
9469 "Invalid memory reference for dev is NULL\n");
9470 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309471 goto out_error;
9472 }
9473
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309474 host = scsi_host_alloc(&ufshcd_driver_template,
9475 sizeof(struct ufs_hba));
9476 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309477 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309478 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309479 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309480 }
Bart Van Asscheeaab9b52021-12-03 15:19:50 -08009481 host->nr_maps = HCTX_TYPE_POLL + 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309482 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309483 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309484 hba->dev = dev;
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05309485 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
Adrian Hunter1cbc9ad2021-08-31 17:53:17 +03009486 hba->nop_out_timeout = NOP_OUT_TIMEOUT;
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03009487 INIT_LIST_HEAD(&hba->clk_list_head);
Bart Van Assche169f5eb2021-07-21 20:34:34 -07009488 spin_lock_init(&hba->outstanding_lock);
9489
9490 *hba_handle = hba;
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03009491
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009492out_error:
9493 return err;
9494}
9495EXPORT_SYMBOL(ufshcd_alloc_host);
9496
Bart Van Assche69a6c262019-12-09 10:13:09 -08009497/* This function exists because blk_mq_alloc_tag_set() requires this. */
9498static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
9499 const struct blk_mq_queue_data *qd)
9500{
9501 WARN_ON_ONCE(true);
9502 return BLK_STS_NOTSUPP;
9503}
9504
9505static const struct blk_mq_ops ufshcd_tmf_ops = {
9506 .queue_rq = ufshcd_queue_tmf,
9507};
9508
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009509/**
9510 * ufshcd_init - Driver initialization routine
9511 * @hba: per-adapter instance
9512 * @mmio_base: base register address
9513 * @irq: Interrupt line of device
9514 * Returns 0 on success, non-zero value on failure
9515 */
9516int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
9517{
9518 int err;
9519 struct Scsi_Host *host = hba->host;
9520 struct device *dev = hba->dev;
Adrian Hunter88b099002021-09-17 17:43:49 +03009521 char eh_wq_name[sizeof("ufs_eh_wq_00")];
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009522
Bart Van Assche21ad0e42021-12-03 15:19:39 -08009523 /*
9524 * dev_set_drvdata() must be called before any callbacks are registered
9525 * that use dev_get_drvdata() (frequency scaling, clock scaling, hwmon,
9526 * sysfs).
9527 */
9528 dev_set_drvdata(dev, hba);
9529
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009530 if (!mmio_base) {
9531 dev_err(hba->dev,
9532 "Invalid memory reference for mmio_base is NULL\n");
9533 err = -ENODEV;
9534 goto out_error;
9535 }
9536
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309537 hba->mmio_base = mmio_base;
9538 hba->irq = irq;
Stanley Chu90b84912020-05-09 17:37:13 +08009539 hba->vps = &ufs_hba_vps;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309540
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009541 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009542 if (err)
9543 goto out_error;
9544
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309545 /* Read capabilities registers */
Satya Tangiraladf043c742020-07-06 20:04:14 +00009546 err = ufshcd_hba_capabilities(hba);
9547 if (err)
9548 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309549
9550 /* Get UFS version supported by the controller */
9551 hba->ufs_version = ufshcd_get_ufs_version(hba);
9552
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05309553 /* Get Interrupt bit mask per version */
9554 hba->intr_mask = ufshcd_get_intr_mask(hba);
9555
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09009556 err = ufshcd_set_dma_mask(hba);
9557 if (err) {
9558 dev_err(hba->dev, "set dma mask failed\n");
9559 goto out_disable;
9560 }
9561
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309562 /* Allocate memory for host memory space */
9563 err = ufshcd_memory_alloc(hba);
9564 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309565 dev_err(hba->dev, "Memory allocation failed\n");
9566 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309567 }
9568
9569 /* Configure LRB */
9570 ufshcd_host_memory_configure(hba);
9571
Bart Van Assche945c3cc2021-12-03 15:19:42 -08009572 host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED;
9573 host->cmd_per_lun = hba->nutrs - UFSHCD_NUM_RESERVED;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309574 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03009575 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309576 host->max_channel = UFSHCD_MAX_CHANNEL;
9577 host->unique_id = host->host_no;
Avri Altmana851b2b2018-10-07 17:30:34 +03009578 host->max_cmd_len = UFS_CDB_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309579
Dolev Raviv7eb584d2014-09-25 15:32:31 +03009580 hba->max_pwr_info.is_valid = false;
9581
Adrian Hunter88b099002021-09-17 17:43:49 +03009582 /* Initialize work queues */
9583 snprintf(eh_wq_name, sizeof(eh_wq_name), "ufs_eh_wq_%d",
9584 hba->host->host_no);
9585 hba->eh_wq = create_singlethread_workqueue(eh_wq_name);
9586 if (!hba->eh_wq) {
9587 dev_err(hba->dev, "%s: failed to create eh workqueue\n",
9588 __func__);
9589 err = -ENOMEM;
9590 goto out_disable;
9591 }
9592 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309593 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309594
Can Guo9cd20d32021-01-13 19:13:28 -08009595 sema_init(&hba->host_sem, 1);
Can Guo88a92d62020-12-02 04:04:01 -08009596
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309597 /* Initialize UIC command mutex */
9598 mutex_init(&hba->uic_cmd_mutex);
9599
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05309600 /* Initialize mutex for device management commands */
9601 mutex_init(&hba->dev_cmd.lock);
9602
Adrian Huntercd469472021-02-09 08:24:36 +02009603 /* Initialize mutex for exception event control */
9604 mutex_init(&hba->ee_ctrl_mutex);
9605
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08009606 init_rwsem(&hba->clk_scaling_lock);
9607
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009608 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02009609
Vivek Gautameebcc192018-08-07 23:17:39 +05309610 ufshcd_init_clk_scaling(hba);
9611
Yaniv Gardi199ef132016-03-10 17:37:06 +02009612 /*
9613 * In order to avoid any spurious interrupt immediately after
9614 * registering UFS controller interrupt handler, clear any pending UFS
9615 * interrupt status and disable all the UFS interrupts.
9616 */
9617 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
9618 REG_INTERRUPT_STATUS);
9619 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
9620 /*
9621 * Make sure that UFS interrupts are disabled and any pending interrupt
9622 * status is cleared before registering UFS interrupt handler.
9623 */
9624 mb();
9625
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309626 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09009627 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309628 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309629 dev_err(hba->dev, "request irq failed\n");
Can Guo4543d9d2021-01-20 02:04:22 -08009630 goto out_disable;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009631 } else {
9632 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309633 }
9634
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309635 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309636 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309637 dev_err(hba->dev, "scsi_add_host failed\n");
Can Guo4543d9d2021-01-20 02:04:22 -08009638 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309639 }
9640
Bart Van Assche69a6c262019-12-09 10:13:09 -08009641 hba->tmf_tag_set = (struct blk_mq_tag_set) {
9642 .nr_hw_queues = 1,
9643 .queue_depth = hba->nutmrs,
9644 .ops = &ufshcd_tmf_ops,
9645 .flags = BLK_MQ_F_NO_SCHED,
9646 };
9647 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
9648 if (err < 0)
Bart Van Assche511a0832021-12-03 15:19:43 -08009649 goto out_remove_scsi_host;
Bart Van Assche69a6c262019-12-09 10:13:09 -08009650 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set);
9651 if (IS_ERR(hba->tmf_queue)) {
9652 err = PTR_ERR(hba->tmf_queue);
9653 goto free_tmf_tag_set;
9654 }
Adrian Hunterf5ef3362021-09-22 12:10:59 +03009655 hba->tmf_rqs = devm_kcalloc(hba->dev, hba->nutmrs,
9656 sizeof(*hba->tmf_rqs), GFP_KERNEL);
9657 if (!hba->tmf_rqs) {
9658 err = -ENOMEM;
9659 goto free_tmf_queue;
9660 }
Bart Van Assche69a6c262019-12-09 10:13:09 -08009661
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07009662 /* Reset the attached device */
Stanley Chu31a5d9c2020-12-08 21:56:35 +08009663 ufshcd_device_reset(hba);
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07009664
Satya Tangiraladf043c742020-07-06 20:04:14 +00009665 ufshcd_init_crypto(hba);
9666
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309667 /* Host controller enable */
9668 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309669 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309670 dev_err(hba->dev, "Host controller enable failed\n");
Stanley Chue965e5e2020-12-05 19:58:59 +08009671 ufshcd_print_evt_hist(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08009672 ufshcd_print_host_state(hba);
Bart Van Assche69a6c262019-12-09 10:13:09 -08009673 goto free_tmf_queue;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309674 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309675
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08009676 /*
9677 * Set the default power management level for runtime and system PM.
9678 * Default power saving mode is to keep UFS link in Hibern8 state
9679 * and UFS device in sleep state.
9680 */
9681 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9682 UFS_SLEEP_PWR_MODE,
9683 UIC_LINK_HIBERN8_STATE);
9684 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9685 UFS_SLEEP_PWR_MODE,
9686 UIC_LINK_HIBERN8_STATE);
9687
Stanley Chu51dd9052020-05-22 16:32:12 +08009688 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work,
9689 ufshcd_rpm_dev_flush_recheck_work);
9690
Adrian Hunterad448372018-03-20 15:07:38 +02009691 /* Set the default auto-hiberate idle timer value to 150 ms */
Stanley Chuf571b372019-05-21 14:44:53 +08009692 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
Adrian Hunterad448372018-03-20 15:07:38 +02009693 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
9694 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
9695 }
9696
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05309697 /* Hold auto suspend until async scan completes */
9698 pm_runtime_get_sync(dev);
Subhash Jadavani38135532018-05-03 16:37:18 +05309699 atomic_set(&hba->scsi_block_reqs_cnt, 0);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009700 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08009701 * We are assuming that device wasn't put in sleep/power-down
9702 * state exclusively during the boot stage before kernel.
9703 * This assumption helps avoid doing link startup twice during
9704 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009705 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08009706 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009707
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309708 async_schedule(ufshcd_async_scan, hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02009709 ufs_sysfs_add_nodes(hba->dev);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309710
Vincent Palomares10845142021-07-27 18:27:43 -07009711 device_enable_async_suspend(dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309712 return 0;
9713
Bart Van Assche69a6c262019-12-09 10:13:09 -08009714free_tmf_queue:
9715 blk_cleanup_queue(hba->tmf_queue);
9716free_tmf_tag_set:
9717 blk_mq_free_tag_set(&hba->tmf_tag_set);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309718out_remove_scsi_host:
9719 scsi_remove_host(hba->host);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309720out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009721 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009722 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309723out_error:
9724 return err;
9725}
9726EXPORT_SYMBOL_GPL(ufshcd_init);
9727
Asutosh Dasb294ff32021-04-23 17:20:16 -07009728void ufshcd_resume_complete(struct device *dev)
9729{
9730 struct ufs_hba *hba = dev_get_drvdata(dev);
9731
9732 if (hba->complete_put) {
9733 ufshcd_rpm_put(hba);
9734 hba->complete_put = false;
9735 }
Asutosh Dasb294ff32021-04-23 17:20:16 -07009736}
9737EXPORT_SYMBOL_GPL(ufshcd_resume_complete);
9738
Adrian Hunterddba1cf2021-10-27 16:06:14 +03009739static bool ufshcd_rpm_ok_for_spm(struct ufs_hba *hba)
9740{
9741 struct device *dev = &hba->sdev_ufs_device->sdev_gendev;
9742 enum ufs_dev_pwr_mode dev_pwr_mode;
9743 enum uic_link_state link_state;
9744 unsigned long flags;
9745 bool res;
9746
9747 spin_lock_irqsave(&dev->power.lock, flags);
9748 dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl);
9749 link_state = ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl);
9750 res = pm_runtime_suspended(dev) &&
9751 hba->curr_dev_pwr_mode == dev_pwr_mode &&
9752 hba->uic_link_state == link_state &&
9753 !hba->dev_info.b_rpm_dev_flush_capable;
9754 spin_unlock_irqrestore(&dev->power.lock, flags);
9755
9756 return res;
9757}
9758
9759int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm)
Asutosh Dasb294ff32021-04-23 17:20:16 -07009760{
9761 struct ufs_hba *hba = dev_get_drvdata(dev);
9762 int ret;
9763
9764 /*
9765 * SCSI assumes that runtime-pm and system-pm for scsi drivers
9766 * are same. And it doesn't wake up the device for system-suspend
9767 * if it's runtime suspended. But ufs doesn't follow that.
9768 * Refer ufshcd_resume_complete()
9769 */
9770 if (hba->sdev_ufs_device) {
Adrian Hunterddba1cf2021-10-27 16:06:14 +03009771 /* Prevent runtime suspend */
9772 ufshcd_rpm_get_noresume(hba);
9773 /*
9774 * Check if already runtime suspended in same state as system
9775 * suspend would be.
9776 */
9777 if (!rpm_ok_for_spm || !ufshcd_rpm_ok_for_spm(hba)) {
9778 /* RPM state is not ok for SPM, so runtime resume */
9779 ret = ufshcd_rpm_resume(hba);
9780 if (ret < 0 && ret != -EACCES) {
9781 ufshcd_rpm_put(hba);
9782 return ret;
9783 }
Asutosh Dasb294ff32021-04-23 17:20:16 -07009784 }
9785 hba->complete_put = true;
9786 }
Asutosh Dasb294ff32021-04-23 17:20:16 -07009787 return 0;
9788}
Adrian Hunterddba1cf2021-10-27 16:06:14 +03009789EXPORT_SYMBOL_GPL(__ufshcd_suspend_prepare);
9790
9791int ufshcd_suspend_prepare(struct device *dev)
9792{
9793 return __ufshcd_suspend_prepare(dev, true);
9794}
Asutosh Dasb294ff32021-04-23 17:20:16 -07009795EXPORT_SYMBOL_GPL(ufshcd_suspend_prepare);
9796
9797#ifdef CONFIG_PM_SLEEP
9798static int ufshcd_wl_poweroff(struct device *dev)
9799{
9800 struct scsi_device *sdev = to_scsi_device(dev);
9801 struct ufs_hba *hba = shost_priv(sdev->host);
9802
9803 __ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM);
9804 return 0;
9805}
9806#endif
9807
9808static int ufshcd_wl_probe(struct device *dev)
9809{
9810 struct scsi_device *sdev = to_scsi_device(dev);
9811
9812 if (!is_device_wlun(sdev))
9813 return -ENODEV;
9814
9815 blk_pm_runtime_init(sdev->request_queue, dev);
9816 pm_runtime_set_autosuspend_delay(dev, 0);
9817 pm_runtime_allow(dev);
9818
9819 return 0;
9820}
9821
9822static int ufshcd_wl_remove(struct device *dev)
9823{
9824 pm_runtime_forbid(dev);
9825 return 0;
9826}
9827
9828static const struct dev_pm_ops ufshcd_wl_pm_ops = {
9829#ifdef CONFIG_PM_SLEEP
9830 .suspend = ufshcd_wl_suspend,
9831 .resume = ufshcd_wl_resume,
9832 .freeze = ufshcd_wl_suspend,
9833 .thaw = ufshcd_wl_resume,
9834 .poweroff = ufshcd_wl_poweroff,
9835 .restore = ufshcd_wl_resume,
9836#endif
9837 SET_RUNTIME_PM_OPS(ufshcd_wl_runtime_suspend, ufshcd_wl_runtime_resume, NULL)
9838};
9839
9840/*
9841 * ufs_dev_wlun_template - describes ufs device wlun
9842 * ufs-device wlun - used to send pm commands
9843 * All luns are consumers of ufs-device wlun.
9844 *
9845 * Currently, no sd driver is present for wluns.
9846 * Hence the no specific pm operations are performed.
9847 * With ufs design, SSU should be sent to ufs-device wlun.
9848 * Hence register a scsi driver for ufs wluns only.
9849 */
9850static struct scsi_driver ufs_dev_wlun_template = {
9851 .gendrv = {
9852 .name = "ufs_device_wlun",
9853 .owner = THIS_MODULE,
9854 .probe = ufshcd_wl_probe,
9855 .remove = ufshcd_wl_remove,
9856 .pm = &ufshcd_wl_pm_ops,
9857 .shutdown = ufshcd_wl_shutdown,
9858 },
9859};
9860
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02009861static int __init ufshcd_core_init(void)
9862{
Asutosh Dasb294ff32021-04-23 17:20:16 -07009863 int ret;
9864
Bart Van Assche9a868c82021-10-20 14:40:23 -07009865 /* Verify that there are no gaps in struct utp_transfer_cmd_desc. */
9866 static_assert(sizeof(struct utp_transfer_cmd_desc) ==
9867 2 * ALIGNED_UPIU_SIZE +
9868 SG_ALL * sizeof(struct ufshcd_sg_entry));
9869
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02009870 ufs_debugfs_init();
Asutosh Dasb294ff32021-04-23 17:20:16 -07009871
9872 ret = scsi_register_driver(&ufs_dev_wlun_template.gendrv);
9873 if (ret)
Bart Van Asscheedc05962021-10-01 11:20:15 -07009874 ufs_debugfs_exit();
Asutosh Dasb294ff32021-04-23 17:20:16 -07009875 return ret;
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02009876}
9877
9878static void __exit ufshcd_core_exit(void)
9879{
9880 ufs_debugfs_exit();
Asutosh Dasb294ff32021-04-23 17:20:16 -07009881 scsi_unregister_driver(&ufs_dev_wlun_template.gendrv);
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02009882}
9883
9884module_init(ufshcd_core_init);
9885module_exit(ufshcd_core_exit);
9886
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309887MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
9888MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05309889MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309890MODULE_LICENSE("GPL");
9891MODULE_VERSION(UFSHCD_DRIVER_VERSION);