blob: b1b69207ca17101d1af17379a22be9c32717f0ae [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>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053019#include "ufshcd.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020020#include "ufs_quirks.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053021#include "unipro.h"
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +020022#include "ufs-sysfs.h"
Adrian Hunterb6cacaf2021-01-07 09:25:38 +020023#include "ufs-debugfs.h"
Avri Altmandf032bf2018-10-07 17:30:35 +030024#include "ufs_bsg.h"
Satya Tangiraladf043c742020-07-06 20:04:14 +000025#include "ufshcd-crypto.h"
Asutosh Das3d17b9b2020-04-22 14:41:42 -070026#include <asm/unaligned.h>
27#include <linux/blkdev.h>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053028
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -080029#define CREATE_TRACE_POINTS
30#include <trace/events/ufs.h>
31
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053032#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
33 UTP_TASK_REQ_COMPL |\
34 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053035/* UIC command timeout, unit: ms */
36#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053037
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053038/* NOP OUT retries waiting for NOP IN response */
39#define NOP_OUT_RETRIES 10
Daejun Park782e2ef2020-09-02 11:58:52 +090040/* Timeout after 50 msecs if NOP OUT hangs without response */
41#define NOP_OUT_TIMEOUT 50 /* msecs */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053042
Dolev Raviv68078d52013-07-30 00:35:58 +053043/* Query request retries */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080044#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +053045/* Query request timeout */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080046#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +053047
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053048/* Task management command timeout */
49#define TM_CMD_TIMEOUT 100 /* msecs */
50
Yaniv Gardi64238fb2016-02-01 15:02:43 +020051/* maximum number of retries for a general UIC command */
52#define UFS_UIC_COMMAND_RETRIES 3
53
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030054/* maximum number of link-startup retries */
55#define DME_LINKSTARTUP_RETRIES 3
56
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020057/* Maximum retries for Hibern8 enter */
58#define UIC_HIBERN8_ENTER_RETRIES 3
59
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030060/* maximum number of reset retries before giving up */
61#define MAX_HOST_RESET_RETRIES 5
62
Dolev Raviv68078d52013-07-30 00:35:58 +053063/* Expose the flag value from utp_upiu_query.value */
64#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
65
Seungwon Jeon7d568652013-08-31 21:40:20 +053066/* Interrupt aggregation default timeout, unit: 40us */
67#define INT_AGGR_DEF_TO 0x02
68
Stanley Chu49615ba2019-09-16 23:56:50 +080069/* default delay of autosuspend: 2000 ms */
70#define RPM_AUTOSUSPEND_DELAY_MS 2000
71
Stanley Chu51dd9052020-05-22 16:32:12 +080072/* Default delay of RPM device flush delayed work */
73#define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000
74
Can Guo09f17792020-02-10 19:40:49 -080075/* Default value of wait time before gating device ref clock */
76#define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */
77
Kiwoong Kim29707fa2020-08-10 19:02:27 +090078/* Polling time to wait for fDeviceInit */
79#define FDEVICEINIT_COMPL_TIMEOUT 1500 /* millisecs */
80
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030081#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
82 ({ \
83 int _ret; \
84 if (_on) \
85 _ret = ufshcd_enable_vreg(_dev, _vreg); \
86 else \
87 _ret = ufshcd_disable_vreg(_dev, _vreg); \
88 _ret; \
89 })
90
Tomas Winklerba809172018-06-14 11:14:09 +030091#define ufshcd_hex_dump(prefix_str, buf, len) do { \
92 size_t __len = (len); \
93 print_hex_dump(KERN_ERR, prefix_str, \
94 __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\
95 16, 4, buf, __len, false); \
96} while (0)
97
Can Guofb7afe22021-01-13 19:13:27 -080098static bool early_suspend;
99
Tomas Winklerba809172018-06-14 11:14:09 +0300100int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
101 const char *prefix)
102{
Marc Gonzalezd6724752019-01-22 18:29:22 +0100103 u32 *regs;
104 size_t pos;
105
106 if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */
107 return -EINVAL;
Tomas Winklerba809172018-06-14 11:14:09 +0300108
Can Guocddaeba2019-11-14 22:09:27 -0800109 regs = kzalloc(len, GFP_ATOMIC);
Tomas Winklerba809172018-06-14 11:14:09 +0300110 if (!regs)
111 return -ENOMEM;
112
Marc Gonzalezd6724752019-01-22 18:29:22 +0100113 for (pos = 0; pos < len; pos += 4)
114 regs[pos / 4] = ufshcd_readl(hba, offset + pos);
115
Tomas Winklerba809172018-06-14 11:14:09 +0300116 ufshcd_hex_dump(prefix, regs, len);
117 kfree(regs);
118
119 return 0;
120}
121EXPORT_SYMBOL_GPL(ufshcd_dump_regs);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800122
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530123enum {
124 UFSHCD_MAX_CHANNEL = 0,
125 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530126 UFSHCD_CMD_PER_LUN = 32,
127 UFSHCD_CAN_QUEUE = 32,
128};
129
130/* UFSHCD states */
131enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530132 UFSHCD_STATE_RESET,
133 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530134 UFSHCD_STATE_OPERATIONAL,
Can Guo5586dd82020-08-09 05:15:54 -0700135 UFSHCD_STATE_EH_SCHEDULED_FATAL,
136 UFSHCD_STATE_EH_SCHEDULED_NON_FATAL,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530137};
138
139/* UFSHCD error handling flags */
140enum {
141 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530142};
143
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530144/* UFSHCD UIC layer error flags */
145enum {
146 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200147 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
148 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
149 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
150 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
151 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Can Guo2355b662020-08-24 19:07:06 -0700152 UFSHCD_UIC_PA_GENERIC_ERROR = (1 << 6), /* Generic PA error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530153};
154
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530155#define ufshcd_set_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300156 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530157#define ufshcd_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300158 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530159#define ufshcd_clear_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300160 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530161
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +0200162struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300163 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
164 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
165 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
166 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
167 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
168 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
Adrian Hunterfe1d4c22020-11-03 16:14:02 +0200169 /*
170 * For DeepSleep, the link is first put in hibern8 and then off.
171 * Leaving the link in hibern8 is not supported.
172 */
173 {UFS_DEEPSLEEP_PWR_MODE, UIC_LINK_OFF_STATE},
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300174};
175
176static inline enum ufs_dev_pwr_mode
177ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
178{
179 return ufs_pm_lvl_states[lvl].dev_state;
180}
181
182static inline enum uic_link_state
183ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
184{
185 return ufs_pm_lvl_states[lvl].link_state;
186}
187
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -0800188static inline enum ufs_pm_level
189ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
190 enum uic_link_state link_state)
191{
192 enum ufs_pm_level lvl;
193
194 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
195 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
196 (ufs_pm_lvl_states[lvl].link_state == link_state))
197 return lvl;
198 }
199
200 /* if no match found, return the level 0 */
201 return UFS_PM_LVL_0;
202}
203
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800204static struct ufs_dev_fix ufs_fixups[] = {
205 /* UFS cards deviations table */
Stanley Chuc0a18ee2020-06-12 09:26:24 +0800206 UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL,
207 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800208 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
Stanley Chued0b40f2020-06-12 09:26:25 +0800209 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
210 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE |
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800211 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
Stanley Chued0b40f2020-06-12 09:26:25 +0800212 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
213 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
214 UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/,
215 UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800216 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
217 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
218 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
219 UFS_DEVICE_QUIRK_PA_TACTIVATE),
220 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
221 UFS_DEVICE_QUIRK_PA_TACTIVATE),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800222 END_FIX
223};
224
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -0800225static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530226static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530227static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Dolev Ravive7d38252016-12-22 18:40:07 -0800228static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530229static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300230static void ufshcd_hba_exit(struct ufs_hba *hba);
Randall Huang19186512020-11-30 20:14:02 -0800231static int ufshcd_clear_ua_wluns(struct ufs_hba *hba);
Bean Huo1b9e2142020-01-20 14:08:15 +0100232static int ufshcd_probe_hba(struct ufs_hba *hba, bool async);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300233static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300234static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300235static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300236static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800237static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
238static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800239static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800240static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300241static irqreturn_t ufshcd_intr(int irq, void *__hba);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300242static int ufshcd_change_power_mode(struct ufs_hba *hba,
243 struct ufs_pa_layer_attr *pwr_mode);
Can Guo4db7a232020-08-09 05:15:51 -0700244static void ufshcd_schedule_eh_work(struct ufs_hba *hba);
Can Guoc72e79c2020-08-09 05:15:52 -0700245static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on);
246static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on);
247static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
248 struct ufs_vreg *vreg);
Can Guo307348f2020-08-24 19:07:05 -0700249static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag);
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700250static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba);
251static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700252static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set);
253static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable);
Can Guodd7143e2020-10-27 12:10:36 -0700254static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
255static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700256
Yaniv Gardi14497322016-02-01 15:02:39 +0200257static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
258{
259 return tag >= 0 && tag < hba->nutrs;
260}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300261
Can Guo5231d382019-12-05 02:14:46 +0000262static inline void ufshcd_enable_irq(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300263{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300264 if (!hba->is_irq_enabled) {
Can Guo5231d382019-12-05 02:14:46 +0000265 enable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300266 hba->is_irq_enabled = true;
267 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300268}
269
270static inline void ufshcd_disable_irq(struct ufs_hba *hba)
271{
272 if (hba->is_irq_enabled) {
Can Guo5231d382019-12-05 02:14:46 +0000273 disable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300274 hba->is_irq_enabled = false;
275 }
276}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530277
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700278static inline void ufshcd_wb_config(struct ufs_hba *hba)
279{
280 int ret;
281
Stanley Chu79e35202020-05-08 16:01:15 +0800282 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700283 return;
284
285 ret = ufshcd_wb_ctrl(hba, true);
286 if (ret)
287 dev_err(hba->dev, "%s: Enable WB failed: %d\n", __func__, ret);
288 else
289 dev_info(hba->dev, "%s: Write Booster Configured\n", __func__);
290 ret = ufshcd_wb_toggle_flush_during_h8(hba, true);
291 if (ret)
292 dev_err(hba->dev, "%s: En WB flush during H8: failed: %d\n",
293 __func__, ret);
Stanley Chu21acf462020-12-22 15:29:05 +0800294 if (!(hba->quirks & UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL))
295 ufshcd_wb_toggle_flush(hba, true);
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700296}
297
Subhash Jadavani38135532018-05-03 16:37:18 +0530298static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
299{
300 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt))
301 scsi_unblock_requests(hba->host);
302}
303
304static void ufshcd_scsi_block_requests(struct ufs_hba *hba)
305{
306 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1)
307 scsi_block_requests(hba->host);
308}
309
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300310static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag,
Bean Huo28fa68f2021-01-05 12:34:42 +0100311 enum ufs_trace_str_t str_t)
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300312{
313 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
314
Bean Huo9d5095e2021-01-05 12:34:43 +0100315 if (!trace_ufshcd_upiu_enabled())
316 return;
317
Bean Huo867fdc22021-01-05 12:34:46 +0100318 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq->header, &rq->sc.cdb,
319 UFS_TSF_CDB);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300320}
321
Avri Altmanfb475b72021-01-10 10:46:18 +0200322static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba,
323 enum ufs_trace_str_t str_t,
324 struct utp_upiu_req *rq_rsp)
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300325{
Bean Huo9d5095e2021-01-05 12:34:43 +0100326 if (!trace_ufshcd_upiu_enabled())
327 return;
328
Bean Huobe20b512021-01-05 12:34:44 +0100329 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq_rsp->header,
Bean Huo867fdc22021-01-05 12:34:46 +0100330 &rq_rsp->qr, UFS_TSF_OSF);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300331}
332
333static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag,
Bean Huo28fa68f2021-01-05 12:34:42 +0100334 enum ufs_trace_str_t str_t)
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300335{
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300336 int off = (int)tag - hba->nutrs;
Christoph Hellwig391e3882018-10-07 17:30:32 +0300337 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[off];
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300338
Bean Huo9d5095e2021-01-05 12:34:43 +0100339 if (!trace_ufshcd_upiu_enabled())
340 return;
341
Bean Huo0ed083e2021-01-05 12:34:45 +0100342 if (str_t == UFS_TM_SEND)
343 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &descp->req_header,
Bean Huo867fdc22021-01-05 12:34:46 +0100344 &descp->input_param1, UFS_TSF_TM_INPUT);
Bean Huo0ed083e2021-01-05 12:34:45 +0100345 else
346 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &descp->rsp_header,
Bean Huo867fdc22021-01-05 12:34:46 +0100347 &descp->output_param1, UFS_TSF_TM_OUTPUT);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300348}
349
Stanley Chuaa5c6972020-06-15 15:22:35 +0800350static void ufshcd_add_uic_command_trace(struct ufs_hba *hba,
351 struct uic_command *ucmd,
Bean Huo28fa68f2021-01-05 12:34:42 +0100352 enum ufs_trace_str_t str_t)
Stanley Chuaa5c6972020-06-15 15:22:35 +0800353{
354 u32 cmd;
355
356 if (!trace_ufshcd_uic_command_enabled())
357 return;
358
Bean Huo28fa68f2021-01-05 12:34:42 +0100359 if (str_t == UFS_CMD_SEND)
Stanley Chuaa5c6972020-06-15 15:22:35 +0800360 cmd = ucmd->command;
361 else
362 cmd = ufshcd_readl(hba, REG_UIC_COMMAND);
363
Bean Huo28fa68f2021-01-05 12:34:42 +0100364 trace_ufshcd_uic_command(dev_name(hba->dev), str_t, cmd,
Stanley Chuaa5c6972020-06-15 15:22:35 +0800365 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1),
366 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2),
367 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3));
368}
369
Bean Huo28fa68f2021-01-05 12:34:42 +0100370static void ufshcd_add_command_trace(struct ufs_hba *hba, unsigned int tag,
371 enum ufs_trace_str_t str_t)
Lee Susman1a07f2d2016-12-22 18:42:03 -0800372{
373 sector_t lba = -1;
Jaegeuk Kim69a314d2020-11-17 08:58:37 -0800374 u8 opcode = 0, group_id = 0;
Lee Susman1a07f2d2016-12-22 18:42:03 -0800375 u32 intr, doorbell;
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300376 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800377 struct scsi_cmnd *cmd = lrbp->cmd;
Lee Susman1a07f2d2016-12-22 18:42:03 -0800378 int transfer_len = -1;
379
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300380 if (!trace_ufshcd_command_enabled()) {
381 /* trace UPIU W/O tracing command */
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800382 if (cmd)
Bean Huo28fa68f2021-01-05 12:34:42 +0100383 ufshcd_add_cmd_upiu_trace(hba, tag, str_t);
Lee Susman1a07f2d2016-12-22 18:42:03 -0800384 return;
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300385 }
Lee Susman1a07f2d2016-12-22 18:42:03 -0800386
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800387 if (cmd) { /* data phase exists */
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300388 /* trace UPIU also */
Bean Huo28fa68f2021-01-05 12:34:42 +0100389 ufshcd_add_cmd_upiu_trace(hba, tag, str_t);
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800390 opcode = cmd->cmnd[0];
Lee Susman1a07f2d2016-12-22 18:42:03 -0800391 if ((opcode == READ_10) || (opcode == WRITE_10)) {
392 /*
393 * Currently we only fully trace read(10) and write(10)
394 * commands
395 */
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800396 if (cmd->request && cmd->request->bio)
397 lba = cmd->request->bio->bi_iter.bi_sector;
Lee Susman1a07f2d2016-12-22 18:42:03 -0800398 transfer_len = be32_to_cpu(
399 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
Jaegeuk Kim69a314d2020-11-17 08:58:37 -0800400 if (opcode == WRITE_10)
401 group_id = lrbp->cmd->cmnd[6];
Leo Liou3754cde2020-11-17 08:58:39 -0800402 } else if (opcode == UNMAP) {
403 if (cmd->request) {
404 lba = scsi_get_lba(cmd);
405 transfer_len = blk_rq_bytes(cmd->request);
406 }
Lee Susman1a07f2d2016-12-22 18:42:03 -0800407 }
408 }
409
410 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
411 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Bean Huo28fa68f2021-01-05 12:34:42 +0100412 trace_ufshcd_command(dev_name(hba->dev), str_t, tag,
Jaegeuk Kim69a314d2020-11-17 08:58:37 -0800413 doorbell, transfer_len, intr, lba, opcode, group_id);
Lee Susman1a07f2d2016-12-22 18:42:03 -0800414}
415
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800416static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
417{
418 struct ufs_clk_info *clki;
419 struct list_head *head = &hba->clk_list_head;
420
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300421 if (list_empty(head))
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800422 return;
423
424 list_for_each_entry(clki, head, list) {
425 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
426 clki->max_freq)
427 dev_err(hba->dev, "clk: %s, rate: %u\n",
428 clki->name, clki->curr_freq);
429 }
430}
431
Stanley Chue965e5e2020-12-05 19:58:59 +0800432static void ufshcd_print_evt(struct ufs_hba *hba, u32 id,
433 char *err_name)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800434{
435 int i;
Stanley Chu27752642019-01-28 22:04:26 +0800436 bool found = false;
Stanley Chue965e5e2020-12-05 19:58:59 +0800437 struct ufs_event_hist *e;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800438
Stanley Chue965e5e2020-12-05 19:58:59 +0800439 if (id >= UFS_EVT_CNT)
440 return;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800441
Stanley Chue965e5e2020-12-05 19:58:59 +0800442 e = &hba->ufs_stats.event[id];
443
444 for (i = 0; i < UFS_EVENT_HIST_LENGTH; i++) {
445 int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH;
446
447 if (e->tstamp[p] == 0)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800448 continue;
Stanley Chuc5397f12019-07-10 21:38:20 +0800449 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p,
Stanley Chue965e5e2020-12-05 19:58:59 +0800450 e->val[p], ktime_to_us(e->tstamp[p]));
Stanley Chu27752642019-01-28 22:04:26 +0800451 found = true;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800452 }
Stanley Chu27752642019-01-28 22:04:26 +0800453
454 if (!found)
Stanley Chufd1fb4d2020-01-04 22:26:08 +0800455 dev_err(hba->dev, "No record of %s\n", err_name);
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800456}
457
Stanley Chue965e5e2020-12-05 19:58:59 +0800458static void ufshcd_print_evt_hist(struct ufs_hba *hba)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800459{
Tomas Winklerba809172018-06-14 11:14:09 +0300460 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800461
Stanley Chue965e5e2020-12-05 19:58:59 +0800462 ufshcd_print_evt(hba, UFS_EVT_PA_ERR, "pa_err");
463 ufshcd_print_evt(hba, UFS_EVT_DL_ERR, "dl_err");
464 ufshcd_print_evt(hba, UFS_EVT_NL_ERR, "nl_err");
465 ufshcd_print_evt(hba, UFS_EVT_TL_ERR, "tl_err");
466 ufshcd_print_evt(hba, UFS_EVT_DME_ERR, "dme_err");
467 ufshcd_print_evt(hba, UFS_EVT_AUTO_HIBERN8_ERR,
468 "auto_hibern8_err");
469 ufshcd_print_evt(hba, UFS_EVT_FATAL_ERR, "fatal_err");
470 ufshcd_print_evt(hba, UFS_EVT_LINK_STARTUP_FAIL,
471 "link_startup_fail");
472 ufshcd_print_evt(hba, UFS_EVT_RESUME_ERR, "resume_fail");
473 ufshcd_print_evt(hba, UFS_EVT_SUSPEND_ERR,
474 "suspend_fail");
475 ufshcd_print_evt(hba, UFS_EVT_DEV_RESET, "dev_reset");
476 ufshcd_print_evt(hba, UFS_EVT_HOST_RESET, "host_reset");
477 ufshcd_print_evt(hba, UFS_EVT_ABORT, "task_abort");
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800478
Stanley Chu7c486d912019-12-24 21:01:06 +0800479 ufshcd_vops_dbg_register_dump(hba);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800480}
481
482static
483void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
484{
485 struct ufshcd_lrb *lrbp;
Gilad Broner7fabb772017-02-03 16:56:50 -0800486 int prdt_length;
Dolev Raviv66cc8202016-12-22 18:39:42 -0800487 int tag;
488
489 for_each_set_bit(tag, &bitmap, hba->nutrs) {
490 lrbp = &hba->lrb[tag];
491
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800492 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
493 tag, ktime_to_us(lrbp->issue_time_stamp));
Zang Leigang09017182017-09-27 10:06:06 +0800494 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
495 tag, ktime_to_us(lrbp->compl_time_stamp));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800496 dev_err(hba->dev,
497 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
498 tag, (u64)lrbp->utrd_dma_addr);
499
Dolev Raviv66cc8202016-12-22 18:39:42 -0800500 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
501 sizeof(struct utp_transfer_req_desc));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800502 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
503 (u64)lrbp->ucd_req_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800504 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
505 sizeof(struct utp_upiu_req));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800506 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
507 (u64)lrbp->ucd_rsp_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800508 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
509 sizeof(struct utp_upiu_rsp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800510
Gilad Broner7fabb772017-02-03 16:56:50 -0800511 prdt_length = le16_to_cpu(
512 lrbp->utr_descriptor_ptr->prd_table_length);
Eric Biggerscc770ce2020-08-25 19:10:40 -0700513 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
514 prdt_length /= sizeof(struct ufshcd_sg_entry);
515
Gilad Broner7fabb772017-02-03 16:56:50 -0800516 dev_err(hba->dev,
517 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
518 tag, prdt_length,
519 (u64)lrbp->ucd_prdt_dma_addr);
520
521 if (pr_prdt)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800522 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
Gilad Broner7fabb772017-02-03 16:56:50 -0800523 sizeof(struct ufshcd_sg_entry) * prdt_length);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800524 }
525}
526
527static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
528{
Dolev Raviv66cc8202016-12-22 18:39:42 -0800529 int tag;
530
531 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
Christoph Hellwig391e3882018-10-07 17:30:32 +0300532 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag];
533
Dolev Raviv66cc8202016-12-22 18:39:42 -0800534 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
Christoph Hellwig391e3882018-10-07 17:30:32 +0300535 ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800536 }
537}
538
Gilad Broner6ba65582017-02-03 16:57:28 -0800539static void ufshcd_print_host_state(struct ufs_hba *hba)
540{
Can Guo3f8af602020-08-09 05:15:50 -0700541 struct scsi_device *sdev_ufs = hba->sdev_ufs_device;
542
Gilad Broner6ba65582017-02-03 16:57:28 -0800543 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
Bart Van Assche7252a362019-12-09 10:13:08 -0800544 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n",
545 hba->outstanding_reqs, hba->outstanding_tasks);
Gilad Broner6ba65582017-02-03 16:57:28 -0800546 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
547 hba->saved_err, hba->saved_uic_err);
548 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
549 hba->curr_dev_pwr_mode, hba->uic_link_state);
550 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
551 hba->pm_op_in_progress, hba->is_sys_suspended);
552 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
553 hba->auto_bkops_enabled, hba->host->host_self_blocked);
554 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
Can Guo3f8af602020-08-09 05:15:50 -0700555 dev_err(hba->dev,
556 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt=%d\n",
557 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
558 hba->ufs_stats.hibern8_exit_cnt);
559 dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n",
560 ktime_to_us(hba->ufs_stats.last_intr_ts),
561 hba->ufs_stats.last_intr_status);
Gilad Broner6ba65582017-02-03 16:57:28 -0800562 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
563 hba->eh_flags, hba->req_abort_count);
Can Guo3f8af602020-08-09 05:15:50 -0700564 dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n",
565 hba->ufs_version, hba->capabilities, hba->caps);
Gilad Broner6ba65582017-02-03 16:57:28 -0800566 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
567 hba->dev_quirks);
Can Guo3f8af602020-08-09 05:15:50 -0700568 if (sdev_ufs)
569 dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n",
570 sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev);
571
572 ufshcd_print_clk_freqs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -0800573}
574
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800575/**
576 * ufshcd_print_pwr_info - print power params as saved in hba
577 * power info
578 * @hba: per-adapter instance
579 */
580static void ufshcd_print_pwr_info(struct ufs_hba *hba)
581{
582 static const char * const names[] = {
583 "INVALID MODE",
584 "FAST MODE",
585 "SLOW_MODE",
586 "INVALID MODE",
587 "FASTAUTO_MODE",
588 "SLOWAUTO_MODE",
589 "INVALID MODE",
590 };
591
592 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
593 __func__,
594 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
595 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
596 names[hba->pwr_info.pwr_rx],
597 names[hba->pwr_info.pwr_tx],
598 hba->pwr_info.hs_rate);
599}
600
Stanley Chu31a5d9c2020-12-08 21:56:35 +0800601static void ufshcd_device_reset(struct ufs_hba *hba)
602{
603 int err;
604
605 err = ufshcd_vops_device_reset(hba);
606
607 if (!err) {
608 ufshcd_set_ufs_dev_active(hba);
609 if (ufshcd_is_wb_allowed(hba)) {
Bean Huo4cd48992021-01-19 17:38:46 +0100610 hba->dev_info.wb_enabled = false;
611 hba->dev_info.wb_buf_flush_enabled = false;
Stanley Chu31a5d9c2020-12-08 21:56:35 +0800612 }
613 }
614 if (err != -EOPNOTSUPP)
615 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err);
616}
617
Stanley Chu5c955c12020-03-18 18:40:12 +0800618void ufshcd_delay_us(unsigned long us, unsigned long tolerance)
619{
620 if (!us)
621 return;
622
623 if (us < 10)
624 udelay(us);
625 else
626 usleep_range(us, us + tolerance);
627}
628EXPORT_SYMBOL_GPL(ufshcd_delay_us);
629
Bart Van Assche5cac1092020-05-07 15:27:50 -0700630/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530631 * ufshcd_wait_for_register - wait for register value to change
Bart Van Assche5cac1092020-05-07 15:27:50 -0700632 * @hba: per-adapter interface
633 * @reg: mmio register offset
634 * @mask: mask to apply to the read register value
635 * @val: value to wait for
636 * @interval_us: polling interval in microseconds
637 * @timeout_ms: timeout in milliseconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530638 *
Bart Van Assche5cac1092020-05-07 15:27:50 -0700639 * Return:
640 * -ETIMEDOUT on error, zero on success.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530641 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200642int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
643 u32 val, unsigned long interval_us,
Bart Van Assche5cac1092020-05-07 15:27:50 -0700644 unsigned long timeout_ms)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530645{
646 int err = 0;
647 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
648
649 /* ignore bits that we don't intend to wait on */
650 val = val & mask;
651
652 while ((ufshcd_readl(hba, reg) & mask) != val) {
Bart Van Assche5cac1092020-05-07 15:27:50 -0700653 usleep_range(interval_us, interval_us + 50);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530654 if (time_after(jiffies, timeout)) {
655 if ((ufshcd_readl(hba, reg) & mask) != val)
656 err = -ETIMEDOUT;
657 break;
658 }
659 }
660
661 return err;
662}
663
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530664/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530665 * ufshcd_get_intr_mask - Get the interrupt bit mask
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800666 * @hba: Pointer to adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530667 *
668 * Returns interrupt bit mask per version
669 */
670static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
671{
Yaniv Gardic01848c2016-12-05 19:25:02 -0800672 u32 intr_mask = 0;
673
674 switch (hba->ufs_version) {
675 case UFSHCI_VERSION_10:
676 intr_mask = INTERRUPT_MASK_ALL_VER_10;
677 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800678 case UFSHCI_VERSION_11:
679 case UFSHCI_VERSION_20:
680 intr_mask = INTERRUPT_MASK_ALL_VER_11;
681 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800682 case UFSHCI_VERSION_21:
683 default:
684 intr_mask = INTERRUPT_MASK_ALL_VER_21;
Tomohiro Kusumi031d1e02017-03-23 12:49:04 +0200685 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800686 }
687
688 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530689}
690
691/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530692 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800693 * @hba: Pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530694 *
695 * Returns UFSHCI version supported by the controller
696 */
697static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
698{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200699 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
700 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300701
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530702 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530703}
704
705/**
706 * ufshcd_is_device_present - Check if any device connected to
707 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300708 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530709 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300710 * Returns true if device present, false if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530711 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300712static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530713{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300714 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300715 DEVICE_PRESENT) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530716}
717
718/**
719 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800720 * @lrbp: pointer to local command reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530721 *
722 * This function is used to get the OCS field from UTRD
723 * Returns the OCS field in the UTRD
724 */
725static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
726{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530727 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530728}
729
730/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530731 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
732 * @hba: per adapter instance
733 * @pos: position of the bit to be cleared
734 */
735static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
736{
Alim Akhtar87183842020-05-28 06:46:49 +0530737 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
738 ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
739 else
740 ufshcd_writel(hba, ~(1 << pos),
741 REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Alim Akhtar1399c5b2018-05-06 15:44:15 +0530742}
743
744/**
745 * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register
746 * @hba: per adapter instance
747 * @pos: position of the bit to be cleared
748 */
749static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
750{
Alim Akhtar87183842020-05-28 06:46:49 +0530751 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
752 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
753 else
754 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530755}
756
757/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200758 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
759 * @hba: per adapter instance
760 * @tag: position of the bit to be cleared
761 */
762static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
763{
764 __clear_bit(tag, &hba->outstanding_reqs);
765}
766
767/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530768 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
769 * @reg: Register value of host controller status
770 *
771 * Returns integer, 0 on Success and positive value if failed
772 */
773static inline int ufshcd_get_lists_status(u32 reg)
774{
Tomohiro Kusumi6cf16112017-04-26 20:28:58 +0300775 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530776}
777
778/**
779 * ufshcd_get_uic_cmd_result - Get the UIC command result
780 * @hba: Pointer to adapter instance
781 *
782 * This function gets the result of UIC command completion
783 * Returns 0 on success, non zero value on error
784 */
785static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
786{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530787 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530788 MASK_UIC_COMMAND_RESULT;
789}
790
791/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530792 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
793 * @hba: Pointer to adapter instance
794 *
795 * This function gets UIC command argument3
796 * Returns 0 on success, non zero value on error
797 */
798static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
799{
800 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
801}
802
803/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530804 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530805 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530806 */
807static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530808ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530809{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530810 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530811}
812
813/**
814 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
815 * @ucd_rsp_ptr: pointer to response UPIU
816 *
817 * This function gets the response status and scsi_status from response UPIU
818 * Returns the response result code.
819 */
820static inline int
821ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
822{
823 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
824}
825
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530826/*
827 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
828 * from response UPIU
829 * @ucd_rsp_ptr: pointer to response UPIU
830 *
831 * Return the data segment length.
832 */
833static inline unsigned int
834ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
835{
836 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
837 MASK_RSP_UPIU_DATA_SEG_LEN;
838}
839
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530840/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530841 * ufshcd_is_exception_event - Check if the device raised an exception event
842 * @ucd_rsp_ptr: pointer to response UPIU
843 *
844 * The function checks if the device raised an exception event indicated in
845 * the Device Information field of response UPIU.
846 *
847 * Returns true if exception is raised, false otherwise.
848 */
849static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
850{
851 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
852 MASK_RSP_EXCEPTION_EVENT ? true : false;
853}
854
855/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530856 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530857 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530858 */
859static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530860ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530861{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530862 ufshcd_writel(hba, INT_AGGR_ENABLE |
863 INT_AGGR_COUNTER_AND_TIMER_RESET,
864 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
865}
866
867/**
868 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
869 * @hba: per adapter instance
870 * @cnt: Interrupt aggregation counter threshold
871 * @tmout: Interrupt aggregation timeout value
872 */
873static inline void
874ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
875{
876 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
877 INT_AGGR_COUNTER_THLD_VAL(cnt) |
878 INT_AGGR_TIMEOUT_VAL(tmout),
879 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530880}
881
882/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300883 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
884 * @hba: per adapter instance
885 */
886static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
887{
888 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
889}
890
891/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530892 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
893 * When run-stop registers are set to 1, it indicates the
894 * host controller that it can process the requests
895 * @hba: per adapter instance
896 */
897static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
898{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530899 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
900 REG_UTP_TASK_REQ_LIST_RUN_STOP);
901 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
902 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530903}
904
905/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530906 * ufshcd_hba_start - Start controller initialization sequence
907 * @hba: per adapter instance
908 */
909static inline void ufshcd_hba_start(struct ufs_hba *hba)
910{
Satya Tangiraladf043c742020-07-06 20:04:14 +0000911 u32 val = CONTROLLER_ENABLE;
912
913 if (ufshcd_crypto_enable(hba))
914 val |= CRYPTO_GENERAL_ENABLE;
915
916 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530917}
918
919/**
920 * ufshcd_is_hba_active - Get controller state
921 * @hba: per adapter instance
922 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300923 * Returns false if controller is active, true otherwise
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530924 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300925static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530926{
Tomohiro Kusumi4a8eec22017-03-28 16:49:25 +0300927 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
928 ? false : true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530929}
930
Yaniv Gardi37113102016-03-10 17:37:16 +0200931u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
932{
933 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
934 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
935 (hba->ufs_version == UFSHCI_VERSION_11))
936 return UFS_UNIPRO_VER_1_41;
937 else
938 return UFS_UNIPRO_VER_1_6;
939}
940EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
941
942static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
943{
944 /*
945 * If both host and device support UniPro ver1.6 or later, PA layer
946 * parameters tuning happens during link startup itself.
947 *
948 * We can manually tune PA layer parameters if either host or device
949 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
950 * logic simple, we will only do manual tuning if local unipro version
951 * doesn't support ver1.6 or later.
952 */
953 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
954 return true;
955 else
956 return false;
957}
958
Subhash Jadavani394b9492020-03-26 02:25:40 -0700959/**
960 * ufshcd_set_clk_freq - set UFS controller clock frequencies
961 * @hba: per adapter instance
962 * @scale_up: If True, set max possible frequency othewise set low frequency
963 *
964 * Returns 0 if successful
965 * Returns < 0 for any other errors
966 */
967static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800968{
969 int ret = 0;
970 struct ufs_clk_info *clki;
971 struct list_head *head = &hba->clk_list_head;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800972
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300973 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800974 goto out;
975
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800976 list_for_each_entry(clki, head, list) {
977 if (!IS_ERR_OR_NULL(clki->clk)) {
978 if (scale_up && clki->max_freq) {
979 if (clki->curr_freq == clki->max_freq)
980 continue;
981
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800982 ret = clk_set_rate(clki->clk, clki->max_freq);
983 if (ret) {
984 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
985 __func__, clki->name,
986 clki->max_freq, ret);
987 break;
988 }
989 trace_ufshcd_clk_scaling(dev_name(hba->dev),
990 "scaled up", clki->name,
991 clki->curr_freq,
992 clki->max_freq);
993
994 clki->curr_freq = clki->max_freq;
995
996 } else if (!scale_up && clki->min_freq) {
997 if (clki->curr_freq == clki->min_freq)
998 continue;
999
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001000 ret = clk_set_rate(clki->clk, clki->min_freq);
1001 if (ret) {
1002 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
1003 __func__, clki->name,
1004 clki->min_freq, ret);
1005 break;
1006 }
1007 trace_ufshcd_clk_scaling(dev_name(hba->dev),
1008 "scaled down", clki->name,
1009 clki->curr_freq,
1010 clki->min_freq);
1011 clki->curr_freq = clki->min_freq;
1012 }
1013 }
1014 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
1015 clki->name, clk_get_rate(clki->clk));
1016 }
1017
Subhash Jadavani394b9492020-03-26 02:25:40 -07001018out:
1019 return ret;
1020}
1021
1022/**
1023 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
1024 * @hba: per adapter instance
1025 * @scale_up: True if scaling up and false if scaling down
1026 *
1027 * Returns 0 if successful
1028 * Returns < 0 for any other errors
1029 */
1030static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
1031{
1032 int ret = 0;
1033 ktime_t start = ktime_get();
1034
1035 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
1036 if (ret)
1037 goto out;
1038
1039 ret = ufshcd_set_clk_freq(hba, scale_up);
1040 if (ret)
1041 goto out;
1042
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001043 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
Subhash Jadavani394b9492020-03-26 02:25:40 -07001044 if (ret)
1045 ufshcd_set_clk_freq(hba, !scale_up);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001046
1047out:
Subhash Jadavani394b9492020-03-26 02:25:40 -07001048 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001049 (scale_up ? "up" : "down"),
1050 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1051 return ret;
1052}
1053
1054/**
1055 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
1056 * @hba: per adapter instance
1057 * @scale_up: True if scaling up and false if scaling down
1058 *
1059 * Returns true if scaling is required, false otherwise.
1060 */
1061static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
1062 bool scale_up)
1063{
1064 struct ufs_clk_info *clki;
1065 struct list_head *head = &hba->clk_list_head;
1066
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03001067 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001068 return false;
1069
1070 list_for_each_entry(clki, head, list) {
1071 if (!IS_ERR_OR_NULL(clki->clk)) {
1072 if (scale_up && clki->max_freq) {
1073 if (clki->curr_freq == clki->max_freq)
1074 continue;
1075 return true;
1076 } else if (!scale_up && clki->min_freq) {
1077 if (clki->curr_freq == clki->min_freq)
1078 continue;
1079 return true;
1080 }
1081 }
1082 }
1083
1084 return false;
1085}
1086
1087static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
1088 u64 wait_timeout_us)
1089{
1090 unsigned long flags;
1091 int ret = 0;
1092 u32 tm_doorbell;
1093 u32 tr_doorbell;
1094 bool timeout = false, do_last_check = false;
1095 ktime_t start;
1096
1097 ufshcd_hold(hba, false);
1098 spin_lock_irqsave(hba->host->host_lock, flags);
1099 /*
1100 * Wait for all the outstanding tasks/transfer requests.
1101 * Verify by checking the doorbell registers are clear.
1102 */
1103 start = ktime_get();
1104 do {
1105 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
1106 ret = -EBUSY;
1107 goto out;
1108 }
1109
1110 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
1111 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1112 if (!tm_doorbell && !tr_doorbell) {
1113 timeout = false;
1114 break;
1115 } else if (do_last_check) {
1116 break;
1117 }
1118
1119 spin_unlock_irqrestore(hba->host->host_lock, flags);
1120 schedule();
1121 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
1122 wait_timeout_us) {
1123 timeout = true;
1124 /*
1125 * We might have scheduled out for long time so make
1126 * sure to check if doorbells are cleared by this time
1127 * or not.
1128 */
1129 do_last_check = true;
1130 }
1131 spin_lock_irqsave(hba->host->host_lock, flags);
1132 } while (tm_doorbell || tr_doorbell);
1133
1134 if (timeout) {
1135 dev_err(hba->dev,
1136 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1137 __func__, tm_doorbell, tr_doorbell);
1138 ret = -EBUSY;
1139 }
1140out:
1141 spin_unlock_irqrestore(hba->host->host_lock, flags);
1142 ufshcd_release(hba);
1143 return ret;
1144}
1145
1146/**
1147 * ufshcd_scale_gear - scale up/down UFS gear
1148 * @hba: per adapter instance
1149 * @scale_up: True for scaling up gear and false for scaling down
1150 *
1151 * Returns 0 for success,
1152 * Returns -EBUSY if scaling can't happen at this time
1153 * Returns non-zero for any other errors
1154 */
1155static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1156{
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001157 int ret = 0;
1158 struct ufs_pa_layer_attr new_pwr_info;
1159
1160 if (scale_up) {
1161 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1162 sizeof(struct ufs_pa_layer_attr));
1163 } else {
1164 memcpy(&new_pwr_info, &hba->pwr_info,
1165 sizeof(struct ufs_pa_layer_attr));
1166
Can Guo29b87e92020-11-26 17:58:48 -08001167 if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear ||
1168 hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001169 /* save the current power mode */
1170 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1171 &hba->pwr_info,
1172 sizeof(struct ufs_pa_layer_attr));
1173
1174 /* scale down gear */
Can Guo29b87e92020-11-26 17:58:48 -08001175 new_pwr_info.gear_tx = hba->clk_scaling.min_gear;
1176 new_pwr_info.gear_rx = hba->clk_scaling.min_gear;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001177 }
1178 }
1179
1180 /* check if the power mode needs to be changed or not? */
Can Guo6a9df812020-02-11 21:38:28 -08001181 ret = ufshcd_config_pwr_mode(hba, &new_pwr_info);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001182 if (ret)
1183 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1184 __func__, ret,
1185 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1186 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1187
1188 return ret;
1189}
1190
1191static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1192{
1193 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1194 int ret = 0;
1195 /*
1196 * make sure that there are no outstanding requests when
1197 * clock scaling is in progress
1198 */
Subhash Jadavani38135532018-05-03 16:37:18 +05301199 ufshcd_scsi_block_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001200 down_write(&hba->clk_scaling_lock);
Can Guo0e9d4ca2021-01-20 02:04:21 -08001201
1202 if (!hba->clk_scaling.is_allowed ||
1203 ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001204 ret = -EBUSY;
1205 up_write(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301206 ufshcd_scsi_unblock_requests(hba);
Can Guo0e9d4ca2021-01-20 02:04:21 -08001207 goto out;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001208 }
1209
Can Guo0e9d4ca2021-01-20 02:04:21 -08001210 /* let's not get into low power until clock scaling is completed */
1211 ufshcd_hold(hba, false);
1212
1213out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001214 return ret;
1215}
1216
Can Guo0e9d4ca2021-01-20 02:04:21 -08001217static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, bool writelock)
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001218{
Can Guo0e9d4ca2021-01-20 02:04:21 -08001219 if (writelock)
1220 up_write(&hba->clk_scaling_lock);
1221 else
1222 up_read(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301223 ufshcd_scsi_unblock_requests(hba);
Can Guo0e9d4ca2021-01-20 02:04:21 -08001224 ufshcd_release(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001225}
1226
1227/**
1228 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1229 * @hba: per adapter instance
1230 * @scale_up: True for scaling up and false for scalin down
1231 *
1232 * Returns 0 for success,
1233 * Returns -EBUSY if scaling can't happen at this time
1234 * Returns non-zero for any other errors
1235 */
1236static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1237{
1238 int ret = 0;
Can Guo0e9d4ca2021-01-20 02:04:21 -08001239 bool is_writelock = true;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001240
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001241 ret = ufshcd_clock_scaling_prepare(hba);
1242 if (ret)
Can Guo0e9d4ca2021-01-20 02:04:21 -08001243 return ret;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001244
1245 /* scale down the gear before scaling down clocks */
1246 if (!scale_up) {
1247 ret = ufshcd_scale_gear(hba, false);
1248 if (ret)
Subhash Jadavani394b9492020-03-26 02:25:40 -07001249 goto out_unprepare;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001250 }
1251
1252 ret = ufshcd_scale_clks(hba, scale_up);
1253 if (ret) {
1254 if (!scale_up)
1255 ufshcd_scale_gear(hba, true);
Subhash Jadavani394b9492020-03-26 02:25:40 -07001256 goto out_unprepare;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001257 }
1258
1259 /* scale up the gear after scaling up clocks */
1260 if (scale_up) {
1261 ret = ufshcd_scale_gear(hba, true);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001262 if (ret) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001263 ufshcd_scale_clks(hba, false);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001264 goto out_unprepare;
1265 }
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001266 }
1267
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001268 /* Enable Write Booster if we have scaled up else disable it */
Can Guo0e9d4ca2021-01-20 02:04:21 -08001269 downgrade_write(&hba->clk_scaling_lock);
1270 is_writelock = false;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001271 ufshcd_wb_ctrl(hba, scale_up);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001272
Subhash Jadavani394b9492020-03-26 02:25:40 -07001273out_unprepare:
Can Guo0e9d4ca2021-01-20 02:04:21 -08001274 ufshcd_clock_scaling_unprepare(hba, is_writelock);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001275 return ret;
1276}
1277
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001278static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1279{
1280 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1281 clk_scaling.suspend_work);
1282 unsigned long irq_flags;
1283
1284 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1285 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1286 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1287 return;
1288 }
1289 hba->clk_scaling.is_suspended = true;
1290 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1291
1292 __ufshcd_suspend_clkscaling(hba);
1293}
1294
1295static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1296{
1297 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1298 clk_scaling.resume_work);
1299 unsigned long irq_flags;
1300
1301 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1302 if (!hba->clk_scaling.is_suspended) {
1303 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1304 return;
1305 }
1306 hba->clk_scaling.is_suspended = false;
1307 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1308
1309 devfreq_resume_device(hba->devfreq);
1310}
1311
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001312static int ufshcd_devfreq_target(struct device *dev,
1313 unsigned long *freq, u32 flags)
1314{
1315 int ret = 0;
1316 struct ufs_hba *hba = dev_get_drvdata(dev);
1317 ktime_t start;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001318 bool scale_up, sched_clk_scaling_suspend_work = false;
Bjorn Andersson092b4552018-05-17 23:26:37 -07001319 struct list_head *clk_list = &hba->clk_list_head;
1320 struct ufs_clk_info *clki;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001321 unsigned long irq_flags;
1322
1323 if (!ufshcd_is_clkscaling_supported(hba))
1324 return -EINVAL;
1325
Asutosh Das91831d32020-03-25 11:29:00 -07001326 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list);
1327 /* Override with the closest supported frequency */
1328 *freq = (unsigned long) clk_round_rate(clki->clk, *freq);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001329 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1330 if (ufshcd_eh_in_progress(hba)) {
1331 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1332 return 0;
1333 }
1334
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001335 if (!hba->clk_scaling.active_reqs)
1336 sched_clk_scaling_suspend_work = true;
1337
Bjorn Andersson092b4552018-05-17 23:26:37 -07001338 if (list_empty(clk_list)) {
1339 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1340 goto out;
1341 }
1342
Asutosh Das91831d32020-03-25 11:29:00 -07001343 /* Decide based on the rounded-off frequency and update */
Bjorn Andersson092b4552018-05-17 23:26:37 -07001344 scale_up = (*freq == clki->max_freq) ? true : false;
Asutosh Das91831d32020-03-25 11:29:00 -07001345 if (!scale_up)
1346 *freq = clki->min_freq;
1347 /* Update the frequency */
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001348 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1349 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1350 ret = 0;
1351 goto out; /* no state change required */
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001352 }
1353 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1354
1355 start = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001356 ret = ufshcd_devfreq_scale(hba, scale_up);
1357
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001358 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1359 (scale_up ? "up" : "down"),
1360 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1361
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001362out:
1363 if (sched_clk_scaling_suspend_work)
1364 queue_work(hba->clk_scaling.workq,
1365 &hba->clk_scaling.suspend_work);
1366
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001367 return ret;
1368}
1369
Bart Van Assche7252a362019-12-09 10:13:08 -08001370static bool ufshcd_is_busy(struct request *req, void *priv, bool reserved)
1371{
1372 int *busy = priv;
1373
1374 WARN_ON_ONCE(reserved);
1375 (*busy)++;
1376 return false;
1377}
1378
1379/* Whether or not any tag is in use by a request that is in progress. */
1380static bool ufshcd_any_tag_in_use(struct ufs_hba *hba)
1381{
1382 struct request_queue *q = hba->cmd_queue;
1383 int busy = 0;
1384
1385 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_is_busy, &busy);
1386 return busy;
1387}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001388
1389static int ufshcd_devfreq_get_dev_status(struct device *dev,
1390 struct devfreq_dev_status *stat)
1391{
1392 struct ufs_hba *hba = dev_get_drvdata(dev);
1393 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1394 unsigned long flags;
Asutosh Das91831d32020-03-25 11:29:00 -07001395 struct list_head *clk_list = &hba->clk_list_head;
1396 struct ufs_clk_info *clki;
Stanley Chub1bf66d2020-06-11 18:10:43 +08001397 ktime_t curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001398
1399 if (!ufshcd_is_clkscaling_supported(hba))
1400 return -EINVAL;
1401
1402 memset(stat, 0, sizeof(*stat));
1403
1404 spin_lock_irqsave(hba->host->host_lock, flags);
Stanley Chub1bf66d2020-06-11 18:10:43 +08001405 curr_t = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001406 if (!scaling->window_start_t)
1407 goto start_window;
1408
Asutosh Das91831d32020-03-25 11:29:00 -07001409 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1410 /*
1411 * If current frequency is 0, then the ondemand governor considers
1412 * there's no initial frequency set. And it always requests to set
1413 * to max. frequency.
1414 */
1415 stat->current_frequency = clki->curr_freq;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001416 if (scaling->is_busy_started)
Stanley Chub1bf66d2020-06-11 18:10:43 +08001417 scaling->tot_busy_t += ktime_us_delta(curr_t,
1418 scaling->busy_start_t);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001419
Stanley Chub1bf66d2020-06-11 18:10:43 +08001420 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001421 stat->busy_time = scaling->tot_busy_t;
1422start_window:
Stanley Chub1bf66d2020-06-11 18:10:43 +08001423 scaling->window_start_t = curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001424 scaling->tot_busy_t = 0;
1425
1426 if (hba->outstanding_reqs) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08001427 scaling->busy_start_t = curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001428 scaling->is_busy_started = true;
1429 } else {
1430 scaling->busy_start_t = 0;
1431 scaling->is_busy_started = false;
1432 }
1433 spin_unlock_irqrestore(hba->host->host_lock, flags);
1434 return 0;
1435}
1436
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001437static int ufshcd_devfreq_init(struct ufs_hba *hba)
1438{
Bjorn Andersson092b4552018-05-17 23:26:37 -07001439 struct list_head *clk_list = &hba->clk_list_head;
1440 struct ufs_clk_info *clki;
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001441 struct devfreq *devfreq;
1442 int ret;
1443
Bjorn Andersson092b4552018-05-17 23:26:37 -07001444 /* Skip devfreq if we don't have any clocks in the list */
1445 if (list_empty(clk_list))
1446 return 0;
1447
1448 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1449 dev_pm_opp_add(hba->dev, clki->min_freq, 0);
1450 dev_pm_opp_add(hba->dev, clki->max_freq, 0);
1451
Stanley Chu90b84912020-05-09 17:37:13 +08001452 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile,
1453 &hba->vps->ondemand_data);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001454 devfreq = devfreq_add_device(hba->dev,
Stanley Chu90b84912020-05-09 17:37:13 +08001455 &hba->vps->devfreq_profile,
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001456 DEVFREQ_GOV_SIMPLE_ONDEMAND,
Stanley Chu90b84912020-05-09 17:37:13 +08001457 &hba->vps->ondemand_data);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001458 if (IS_ERR(devfreq)) {
1459 ret = PTR_ERR(devfreq);
1460 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001461
1462 dev_pm_opp_remove(hba->dev, clki->min_freq);
1463 dev_pm_opp_remove(hba->dev, clki->max_freq);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001464 return ret;
1465 }
1466
1467 hba->devfreq = devfreq;
1468
1469 return 0;
1470}
1471
Bjorn Andersson092b4552018-05-17 23:26:37 -07001472static void ufshcd_devfreq_remove(struct ufs_hba *hba)
1473{
1474 struct list_head *clk_list = &hba->clk_list_head;
1475 struct ufs_clk_info *clki;
1476
1477 if (!hba->devfreq)
1478 return;
1479
1480 devfreq_remove_device(hba->devfreq);
1481 hba->devfreq = NULL;
1482
1483 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1484 dev_pm_opp_remove(hba->dev, clki->min_freq);
1485 dev_pm_opp_remove(hba->dev, clki->max_freq);
1486}
1487
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001488static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1489{
1490 unsigned long flags;
1491
1492 devfreq_suspend_device(hba->devfreq);
1493 spin_lock_irqsave(hba->host->host_lock, flags);
1494 hba->clk_scaling.window_start_t = 0;
1495 spin_unlock_irqrestore(hba->host->host_lock, flags);
1496}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001497
Gilad Bronera5082532016-10-17 17:10:00 -07001498static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1499{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001500 unsigned long flags;
1501 bool suspend = false;
1502
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001503 if (!ufshcd_is_clkscaling_supported(hba))
1504 return;
1505
Stanley Chuf9a7fa32021-01-20 23:01:40 +08001506 cancel_work_sync(&hba->clk_scaling.suspend_work);
1507 cancel_work_sync(&hba->clk_scaling.resume_work);
1508
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001509 spin_lock_irqsave(hba->host->host_lock, flags);
1510 if (!hba->clk_scaling.is_suspended) {
1511 suspend = true;
1512 hba->clk_scaling.is_suspended = true;
1513 }
1514 spin_unlock_irqrestore(hba->host->host_lock, flags);
1515
1516 if (suspend)
1517 __ufshcd_suspend_clkscaling(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07001518}
1519
1520static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1521{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001522 unsigned long flags;
1523 bool resume = false;
1524
1525 if (!ufshcd_is_clkscaling_supported(hba))
1526 return;
1527
1528 spin_lock_irqsave(hba->host->host_lock, flags);
1529 if (hba->clk_scaling.is_suspended) {
1530 resume = true;
1531 hba->clk_scaling.is_suspended = false;
1532 }
1533 spin_unlock_irqrestore(hba->host->host_lock, flags);
1534
1535 if (resume)
1536 devfreq_resume_device(hba->devfreq);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001537}
1538
1539static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1540 struct device_attribute *attr, char *buf)
1541{
1542 struct ufs_hba *hba = dev_get_drvdata(dev);
1543
Can Guo0e9d4ca2021-01-20 02:04:21 -08001544 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_enabled);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001545}
1546
1547static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1548 struct device_attribute *attr, const char *buf, size_t count)
1549{
1550 struct ufs_hba *hba = dev_get_drvdata(dev);
1551 u32 value;
Can Guo9cd20d32021-01-13 19:13:28 -08001552 int err = 0;
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001553
1554 if (kstrtou32(buf, 0, &value))
1555 return -EINVAL;
1556
Can Guo9cd20d32021-01-13 19:13:28 -08001557 down(&hba->host_sem);
1558 if (!ufshcd_is_user_access_allowed(hba)) {
1559 err = -EBUSY;
1560 goto out;
1561 }
1562
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001563 value = !!value;
Can Guo0e9d4ca2021-01-20 02:04:21 -08001564 if (value == hba->clk_scaling.is_enabled)
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001565 goto out;
1566
1567 pm_runtime_get_sync(hba->dev);
1568 ufshcd_hold(hba, false);
1569
Can Guo0e9d4ca2021-01-20 02:04:21 -08001570 hba->clk_scaling.is_enabled = value;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001571
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001572 if (value) {
1573 ufshcd_resume_clkscaling(hba);
1574 } else {
1575 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001576 err = ufshcd_devfreq_scale(hba, true);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001577 if (err)
1578 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1579 __func__, err);
1580 }
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001581
1582 ufshcd_release(hba);
1583 pm_runtime_put_sync(hba->dev);
1584out:
Can Guo9cd20d32021-01-13 19:13:28 -08001585 up(&hba->host_sem);
1586 return err ? err : count;
Gilad Bronera5082532016-10-17 17:10:00 -07001587}
1588
Can Guo4543d9d2021-01-20 02:04:22 -08001589static void ufshcd_init_clk_scaling_sysfs(struct ufs_hba *hba)
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001590{
1591 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1592 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1593 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1594 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1595 hba->clk_scaling.enable_attr.attr.mode = 0644;
1596 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1597 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1598}
1599
Can Guo4543d9d2021-01-20 02:04:22 -08001600static void ufshcd_remove_clk_scaling_sysfs(struct ufs_hba *hba)
1601{
1602 if (hba->clk_scaling.enable_attr.attr.name)
1603 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
1604}
1605
1606static void ufshcd_init_clk_scaling(struct ufs_hba *hba)
1607{
1608 char wq_name[sizeof("ufs_clkscaling_00")];
1609
1610 if (!ufshcd_is_clkscaling_supported(hba))
1611 return;
1612
1613 INIT_WORK(&hba->clk_scaling.suspend_work,
1614 ufshcd_clk_scaling_suspend_work);
1615 INIT_WORK(&hba->clk_scaling.resume_work,
1616 ufshcd_clk_scaling_resume_work);
1617
1618 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d",
1619 hba->host->host_no);
1620 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
1621
1622 hba->clk_scaling.is_initialized = true;
1623}
1624
1625static void ufshcd_exit_clk_scaling(struct ufs_hba *hba)
1626{
1627 if (!hba->clk_scaling.is_initialized)
1628 return;
1629
1630 ufshcd_remove_clk_scaling_sysfs(hba);
1631 destroy_workqueue(hba->clk_scaling.workq);
1632 ufshcd_devfreq_remove(hba);
1633 hba->clk_scaling.is_initialized = false;
1634}
1635
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001636static void ufshcd_ungate_work(struct work_struct *work)
1637{
1638 int ret;
1639 unsigned long flags;
1640 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1641 clk_gating.ungate_work);
1642
1643 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1644
1645 spin_lock_irqsave(hba->host->host_lock, flags);
1646 if (hba->clk_gating.state == CLKS_ON) {
1647 spin_unlock_irqrestore(hba->host->host_lock, flags);
1648 goto unblock_reqs;
1649 }
1650
1651 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guodd7143e2020-10-27 12:10:36 -07001652 ufshcd_hba_vreg_set_hpm(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001653 ufshcd_setup_clocks(hba, true);
1654
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001655 ufshcd_enable_irq(hba);
1656
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001657 /* Exit from hibern8 */
1658 if (ufshcd_can_hibern8_during_gating(hba)) {
1659 /* Prevent gating in this path */
1660 hba->clk_gating.is_suspended = true;
1661 if (ufshcd_is_link_hibern8(hba)) {
1662 ret = ufshcd_uic_hibern8_exit(hba);
1663 if (ret)
1664 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1665 __func__, ret);
1666 else
1667 ufshcd_set_link_active(hba);
1668 }
1669 hba->clk_gating.is_suspended = false;
1670 }
1671unblock_reqs:
Subhash Jadavani38135532018-05-03 16:37:18 +05301672 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001673}
1674
1675/**
1676 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1677 * Also, exit from hibern8 mode and set the link as active.
1678 * @hba: per adapter instance
1679 * @async: This indicates whether caller should ungate clocks asynchronously.
1680 */
1681int ufshcd_hold(struct ufs_hba *hba, bool async)
1682{
1683 int rc = 0;
Stanley Chu93b6c5d2020-08-09 13:07:34 +08001684 bool flush_result;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001685 unsigned long flags;
1686
1687 if (!ufshcd_is_clkgating_allowed(hba))
1688 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001689 spin_lock_irqsave(hba->host->host_lock, flags);
1690 hba->clk_gating.active_reqs++;
1691
Sahitya Tummala856b3482014-09-25 15:32:34 +03001692start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001693 switch (hba->clk_gating.state) {
1694 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001695 /*
1696 * Wait for the ungate work to complete if in progress.
1697 * Though the clocks may be in ON state, the link could
1698 * still be in hibner8 state if hibern8 is allowed
1699 * during clock gating.
1700 * Make sure we exit hibern8 state also in addition to
1701 * clocks being ON.
1702 */
1703 if (ufshcd_can_hibern8_during_gating(hba) &&
1704 ufshcd_is_link_hibern8(hba)) {
Can Guoc63d6092020-02-10 19:40:48 -08001705 if (async) {
1706 rc = -EAGAIN;
1707 hba->clk_gating.active_reqs--;
1708 break;
1709 }
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001710 spin_unlock_irqrestore(hba->host->host_lock, flags);
Stanley Chu93b6c5d2020-08-09 13:07:34 +08001711 flush_result = flush_work(&hba->clk_gating.ungate_work);
1712 if (hba->clk_gating.is_suspended && !flush_result)
1713 goto out;
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001714 spin_lock_irqsave(hba->host->host_lock, flags);
1715 goto start;
1716 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001717 break;
1718 case REQ_CLKS_OFF:
1719 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1720 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001721 trace_ufshcd_clk_gating(dev_name(hba->dev),
1722 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001723 break;
1724 }
1725 /*
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +03001726 * If we are here, it means gating work is either done or
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001727 * currently running. Hence, fall through to cancel gating
1728 * work and to enable clocks.
1729 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001730 fallthrough;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001731 case CLKS_OFF:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001732 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001733 trace_ufshcd_clk_gating(dev_name(hba->dev),
1734 hba->clk_gating.state);
Can Guoda3fecb2020-11-02 22:24:39 -08001735 if (queue_work(hba->clk_gating.clk_gating_workq,
1736 &hba->clk_gating.ungate_work))
1737 ufshcd_scsi_block_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001738 /*
1739 * fall through to check if we should wait for this
1740 * work to be done or not.
1741 */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05001742 fallthrough;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001743 case REQ_CLKS_ON:
1744 if (async) {
1745 rc = -EAGAIN;
1746 hba->clk_gating.active_reqs--;
1747 break;
1748 }
1749
1750 spin_unlock_irqrestore(hba->host->host_lock, flags);
1751 flush_work(&hba->clk_gating.ungate_work);
1752 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001753 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001754 goto start;
1755 default:
1756 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1757 __func__, hba->clk_gating.state);
1758 break;
1759 }
1760 spin_unlock_irqrestore(hba->host->host_lock, flags);
1761out:
1762 return rc;
1763}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001764EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001765
1766static void ufshcd_gate_work(struct work_struct *work)
1767{
1768 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1769 clk_gating.gate_work.work);
1770 unsigned long flags;
Can Guo4db7a232020-08-09 05:15:51 -07001771 int ret;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001772
1773 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -07001774 /*
1775 * In case you are here to cancel this work the gating state
1776 * would be marked as REQ_CLKS_ON. In this case save time by
1777 * skipping the gating work and exit after changing the clock
1778 * state to CLKS_ON.
1779 */
1780 if (hba->clk_gating.is_suspended ||
Asutosh Das18f013742019-11-14 22:09:29 -08001781 (hba->clk_gating.state != REQ_CLKS_OFF)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001782 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001783 trace_ufshcd_clk_gating(dev_name(hba->dev),
1784 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001785 goto rel_lock;
1786 }
1787
1788 if (hba->clk_gating.active_reqs
1789 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
Bart Van Assche7252a362019-12-09 10:13:08 -08001790 || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001791 || hba->active_uic_cmd || hba->uic_async_done)
1792 goto rel_lock;
1793
1794 spin_unlock_irqrestore(hba->host->host_lock, flags);
1795
1796 /* put the link into hibern8 mode before turning off clocks */
1797 if (ufshcd_can_hibern8_during_gating(hba)) {
Can Guo4db7a232020-08-09 05:15:51 -07001798 ret = ufshcd_uic_hibern8_enter(hba);
1799 if (ret) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001800 hba->clk_gating.state = CLKS_ON;
Can Guo4db7a232020-08-09 05:15:51 -07001801 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
1802 __func__, ret);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001803 trace_ufshcd_clk_gating(dev_name(hba->dev),
1804 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001805 goto out;
1806 }
1807 ufshcd_set_link_hibern8(hba);
1808 }
1809
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001810 ufshcd_disable_irq(hba);
1811
Can Guo81309c22020-11-25 18:01:00 -08001812 ufshcd_setup_clocks(hba, false);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001813
Can Guodd7143e2020-10-27 12:10:36 -07001814 /* Put the host controller in low power mode if possible */
1815 ufshcd_hba_vreg_set_lpm(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001816 /*
1817 * In case you are here to cancel this work the gating state
1818 * would be marked as REQ_CLKS_ON. In this case keep the state
1819 * as REQ_CLKS_ON which would anyway imply that clocks are off
1820 * and a request to turn them on is pending. By doing this way,
1821 * we keep the state machine in tact and this would ultimately
1822 * prevent from doing cancel work multiple times when there are
1823 * new requests arriving before the current cancel work is done.
1824 */
1825 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001826 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001827 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001828 trace_ufshcd_clk_gating(dev_name(hba->dev),
1829 hba->clk_gating.state);
1830 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001831rel_lock:
1832 spin_unlock_irqrestore(hba->host->host_lock, flags);
1833out:
1834 return;
1835}
1836
1837/* host lock must be held before calling this variant */
1838static void __ufshcd_release(struct ufs_hba *hba)
1839{
1840 if (!ufshcd_is_clkgating_allowed(hba))
1841 return;
1842
1843 hba->clk_gating.active_reqs--;
1844
Can Guo4db7a232020-08-09 05:15:51 -07001845 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
1846 hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
Jaegeuk Kim8eb456b2020-11-17 08:58:38 -08001847 hba->outstanding_tasks ||
Jaegeuk Kimfd62de12020-11-17 08:58:33 -08001848 hba->active_uic_cmd || hba->uic_async_done ||
1849 hba->clk_gating.state == CLKS_OFF)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001850 return;
1851
1852 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001853 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Evan Greenf4bb7702018-10-05 10:27:32 -07001854 queue_delayed_work(hba->clk_gating.clk_gating_workq,
1855 &hba->clk_gating.gate_work,
1856 msecs_to_jiffies(hba->clk_gating.delay_ms));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001857}
1858
1859void ufshcd_release(struct ufs_hba *hba)
1860{
1861 unsigned long flags;
1862
1863 spin_lock_irqsave(hba->host->host_lock, flags);
1864 __ufshcd_release(hba);
1865 spin_unlock_irqrestore(hba->host->host_lock, flags);
1866}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001867EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001868
1869static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1870 struct device_attribute *attr, char *buf)
1871{
1872 struct ufs_hba *hba = dev_get_drvdata(dev);
1873
1874 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1875}
1876
1877static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1878 struct device_attribute *attr, const char *buf, size_t count)
1879{
1880 struct ufs_hba *hba = dev_get_drvdata(dev);
1881 unsigned long flags, value;
1882
1883 if (kstrtoul(buf, 0, &value))
1884 return -EINVAL;
1885
1886 spin_lock_irqsave(hba->host->host_lock, flags);
1887 hba->clk_gating.delay_ms = value;
1888 spin_unlock_irqrestore(hba->host->host_lock, flags);
1889 return count;
1890}
1891
Sahitya Tummalab4274112016-12-22 18:40:39 -08001892static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1893 struct device_attribute *attr, char *buf)
1894{
1895 struct ufs_hba *hba = dev_get_drvdata(dev);
1896
1897 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1898}
1899
1900static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1901 struct device_attribute *attr, const char *buf, size_t count)
1902{
1903 struct ufs_hba *hba = dev_get_drvdata(dev);
1904 unsigned long flags;
1905 u32 value;
1906
1907 if (kstrtou32(buf, 0, &value))
1908 return -EINVAL;
1909
1910 value = !!value;
Jaegeuk Kimb6645112020-11-17 08:58:34 -08001911
1912 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001913 if (value == hba->clk_gating.is_enabled)
1914 goto out;
1915
Jaegeuk Kimb6645112020-11-17 08:58:34 -08001916 if (value)
1917 __ufshcd_release(hba);
1918 else
Sahitya Tummalab4274112016-12-22 18:40:39 -08001919 hba->clk_gating.active_reqs++;
Sahitya Tummalab4274112016-12-22 18:40:39 -08001920
1921 hba->clk_gating.is_enabled = value;
1922out:
Jaegeuk Kimb6645112020-11-17 08:58:34 -08001923 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001924 return count;
1925}
1926
Can Guo4543d9d2021-01-20 02:04:22 -08001927static void ufshcd_init_clk_gating_sysfs(struct ufs_hba *hba)
Vivek Gautameebcc192018-08-07 23:17:39 +05301928{
Can Guo4543d9d2021-01-20 02:04:22 -08001929 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1930 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1931 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1932 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
1933 hba->clk_gating.delay_attr.attr.mode = 0644;
1934 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1935 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Vivek Gautameebcc192018-08-07 23:17:39 +05301936
Can Guo4543d9d2021-01-20 02:04:22 -08001937 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1938 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1939 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1940 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1941 hba->clk_gating.enable_attr.attr.mode = 0644;
1942 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1943 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Vivek Gautameebcc192018-08-07 23:17:39 +05301944}
1945
Can Guo4543d9d2021-01-20 02:04:22 -08001946static void ufshcd_remove_clk_gating_sysfs(struct ufs_hba *hba)
Vivek Gautameebcc192018-08-07 23:17:39 +05301947{
Can Guo4543d9d2021-01-20 02:04:22 -08001948 if (hba->clk_gating.delay_attr.attr.name)
1949 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
1950 if (hba->clk_gating.enable_attr.attr.name)
1951 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Vivek Gautameebcc192018-08-07 23:17:39 +05301952}
1953
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001954static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1955{
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301956 char wq_name[sizeof("ufs_clk_gating_00")];
1957
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001958 if (!ufshcd_is_clkgating_allowed(hba))
1959 return;
1960
Can Guo2dec9472020-08-09 05:15:47 -07001961 hba->clk_gating.state = CLKS_ON;
1962
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001963 hba->clk_gating.delay_ms = 150;
1964 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1965 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1966
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301967 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
1968 hba->host->host_no);
1969 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
Jaegeuk Kime93e6e42020-11-17 08:58:36 -08001970 WQ_MEM_RECLAIM | WQ_HIGHPRI);
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301971
Can Guo4543d9d2021-01-20 02:04:22 -08001972 ufshcd_init_clk_gating_sysfs(hba);
1973
Sahitya Tummalab4274112016-12-22 18:40:39 -08001974 hba->clk_gating.is_enabled = true;
Can Guo4543d9d2021-01-20 02:04:22 -08001975 hba->clk_gating.is_initialized = true;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001976}
1977
1978static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1979{
Can Guo4543d9d2021-01-20 02:04:22 -08001980 if (!hba->clk_gating.is_initialized)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001981 return;
Can Guo4543d9d2021-01-20 02:04:22 -08001982 ufshcd_remove_clk_gating_sysfs(hba);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001983 cancel_work_sync(&hba->clk_gating.ungate_work);
1984 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301985 destroy_workqueue(hba->clk_gating.clk_gating_workq);
Can Guo4543d9d2021-01-20 02:04:22 -08001986 hba->clk_gating.is_initialized = false;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001987}
1988
Sahitya Tummala856b3482014-09-25 15:32:34 +03001989/* Must be called with host lock acquired */
1990static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1991{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001992 bool queue_resume_work = false;
Stanley Chub1bf66d2020-06-11 18:10:43 +08001993 ktime_t curr_t = ktime_get();
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001994
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001995 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001996 return;
1997
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001998 if (!hba->clk_scaling.active_reqs++)
1999 queue_resume_work = true;
2000
Can Guo0e9d4ca2021-01-20 02:04:21 -08002001 if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress)
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08002002 return;
2003
2004 if (queue_resume_work)
2005 queue_work(hba->clk_scaling.workq,
2006 &hba->clk_scaling.resume_work);
2007
2008 if (!hba->clk_scaling.window_start_t) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08002009 hba->clk_scaling.window_start_t = curr_t;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08002010 hba->clk_scaling.tot_busy_t = 0;
2011 hba->clk_scaling.is_busy_started = false;
2012 }
2013
Sahitya Tummala856b3482014-09-25 15:32:34 +03002014 if (!hba->clk_scaling.is_busy_started) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08002015 hba->clk_scaling.busy_start_t = curr_t;
Sahitya Tummala856b3482014-09-25 15:32:34 +03002016 hba->clk_scaling.is_busy_started = true;
2017 }
2018}
2019
2020static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
2021{
2022 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
2023
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08002024 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03002025 return;
2026
2027 if (!hba->outstanding_reqs && scaling->is_busy_started) {
2028 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
2029 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002030 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03002031 scaling->is_busy_started = false;
2032 }
2033}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302034/**
2035 * ufshcd_send_command - Send SCSI or device management commands
2036 * @hba: per adapter instance
2037 * @task_tag: Task tag of the command
2038 */
2039static inline
2040void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
2041{
Stanley Chu6edfdcf2020-07-06 14:07:07 +08002042 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag];
2043
2044 lrbp->issue_time_stamp = ktime_get();
2045 lrbp->compl_time_stamp = ktime_set(0, 0);
2046 ufshcd_vops_setup_xfer_req(hba, task_tag, (lrbp->cmd ? true : false));
Bean Huo28fa68f2021-01-05 12:34:42 +01002047 ufshcd_add_command_trace(hba, task_tag, UFS_CMD_SEND);
Sahitya Tummala856b3482014-09-25 15:32:34 +03002048 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302049 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302050 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002051 /* Make sure that doorbell is committed immediately */
2052 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302053}
2054
2055/**
2056 * ufshcd_copy_sense_data - Copy sense data in case of check condition
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002057 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302058 */
2059static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
2060{
2061 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302062 if (lrbp->sense_buffer &&
2063 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07002064 int len_to_copy;
2065
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302066 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Avri Altman09a5a242018-11-22 20:04:56 +02002067 len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07002068
Avri Altman09a5a242018-11-22 20:04:56 +02002069 memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data,
2070 len_to_copy);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302071 }
2072}
2073
2074/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302075 * ufshcd_copy_query_response() - Copy the Query Response and the data
2076 * descriptor
2077 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002078 * @lrbp: pointer to local reference block
Dolev Raviv68078d52013-07-30 00:35:58 +05302079 */
2080static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002081int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05302082{
2083 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2084
Dolev Raviv68078d52013-07-30 00:35:58 +05302085 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302086
Dolev Raviv68078d52013-07-30 00:35:58 +05302087 /* Get the descriptor */
Avri Altman1c908362019-05-21 11:24:22 +03002088 if (hba->dev_cmd.query.descriptor &&
2089 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002090 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05302091 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002092 u16 resp_len;
2093 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05302094
2095 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002096 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302097 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002098 buf_len = be16_to_cpu(
2099 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002100 if (likely(buf_len >= resp_len)) {
2101 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
2102 } else {
2103 dev_warn(hba->dev,
Bean Huo3d4881d2019-11-12 23:34:35 +01002104 "%s: rsp size %d is bigger than buffer size %d",
2105 __func__, resp_len, buf_len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002106 return -EINVAL;
2107 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302108 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002109
2110 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302111}
2112
2113/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302114 * ufshcd_hba_capabilities - Read controller capabilities
2115 * @hba: per adapter instance
Satya Tangiraladf043c742020-07-06 20:04:14 +00002116 *
2117 * Return: 0 on success, negative on error.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302118 */
Satya Tangiraladf043c742020-07-06 20:04:14 +00002119static inline int ufshcd_hba_capabilities(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302120{
Satya Tangiraladf043c742020-07-06 20:04:14 +00002121 int err;
2122
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302123 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302124
2125 /* nutrs and nutmrs are 0 based values */
2126 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2127 hba->nutmrs =
2128 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002129
2130 /* Read crypto capabilities */
2131 err = ufshcd_hba_init_crypto_capabilities(hba);
2132 if (err)
2133 dev_err(hba->dev, "crypto setup failed\n");
2134
2135 return err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302136}
2137
2138/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302139 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2140 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302141 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302142 * Return true on success, else false
2143 */
2144static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2145{
2146 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2147 return true;
2148 else
2149 return false;
2150}
2151
2152/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302153 * ufshcd_get_upmcrs - Get the power mode change request status
2154 * @hba: Pointer to adapter instance
2155 *
2156 * This function gets the UPMCRS field of HCS register
2157 * Returns value of UPMCRS field
2158 */
2159static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2160{
2161 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2162}
2163
2164/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302165 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
2166 * @hba: per adapter instance
2167 * @uic_cmd: UIC command
2168 *
2169 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302170 */
2171static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302172ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302173{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302174 WARN_ON(hba->active_uic_cmd);
2175
2176 hba->active_uic_cmd = uic_cmd;
2177
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302178 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302179 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2180 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2181 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302182
Bean Huo28fa68f2021-01-05 12:34:42 +01002183 ufshcd_add_uic_command_trace(hba, uic_cmd, UFS_CMD_SEND);
Stanley Chuaa5c6972020-06-15 15:22:35 +08002184
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302185 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302186 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302187 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302188}
2189
2190/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302191 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
2192 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002193 * @uic_cmd: UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302194 *
2195 * Must be called with mutex held.
2196 * Returns 0 only if success.
2197 */
2198static int
2199ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2200{
2201 int ret;
2202 unsigned long flags;
2203
2204 if (wait_for_completion_timeout(&uic_cmd->done,
Can Guo0f52fcb92020-11-02 22:24:40 -08002205 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302206 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
Can Guo0f52fcb92020-11-02 22:24:40 -08002207 } else {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302208 ret = -ETIMEDOUT;
Can Guo0f52fcb92020-11-02 22:24:40 -08002209 dev_err(hba->dev,
2210 "uic cmd 0x%x with arg3 0x%x completion timeout\n",
2211 uic_cmd->command, uic_cmd->argument3);
2212
2213 if (!uic_cmd->cmd_active) {
2214 dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n",
2215 __func__);
2216 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2217 }
2218 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302219
2220 spin_lock_irqsave(hba->host->host_lock, flags);
2221 hba->active_uic_cmd = NULL;
2222 spin_unlock_irqrestore(hba->host->host_lock, flags);
2223
2224 return ret;
2225}
2226
2227/**
2228 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2229 * @hba: per adapter instance
2230 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002231 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302232 *
2233 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002234 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302235 * Returns 0 only if success.
2236 */
2237static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002238__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2239 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302240{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302241 if (!ufshcd_ready_for_uic_cmd(hba)) {
2242 dev_err(hba->dev,
2243 "Controller not ready to accept UIC commands\n");
2244 return -EIO;
2245 }
2246
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002247 if (completion)
2248 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302249
Can Guo0f52fcb92020-11-02 22:24:40 -08002250 uic_cmd->cmd_active = 1;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302251 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302252
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002253 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302254}
2255
2256/**
2257 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2258 * @hba: per adapter instance
2259 * @uic_cmd: UIC command
2260 *
2261 * Returns 0 only if success.
2262 */
Avri Altmane77044c52018-10-07 17:30:39 +03002263int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302264{
2265 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002266 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302267
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002268 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302269 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002270 ufshcd_add_delay_before_dme_cmd(hba);
2271
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002272 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002273 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002274 spin_unlock_irqrestore(hba->host->host_lock, flags);
2275 if (!ret)
2276 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2277
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302278 mutex_unlock(&hba->uic_cmd_mutex);
2279
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002280 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302281 return ret;
2282}
2283
2284/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302285 * ufshcd_map_sg - Map scatter-gather list to prdt
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002286 * @hba: per adapter instance
2287 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302288 *
2289 * Returns 0 in case of success, non-zero value in case of failure
2290 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002291static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302292{
2293 struct ufshcd_sg_entry *prd_table;
2294 struct scatterlist *sg;
2295 struct scsi_cmnd *cmd;
2296 int sg_segments;
2297 int i;
2298
2299 cmd = lrbp->cmd;
2300 sg_segments = scsi_dma_map(cmd);
2301 if (sg_segments < 0)
2302 return sg_segments;
2303
2304 if (sg_segments) {
Alim Akhtar26f968d2020-05-28 06:46:52 +05302305
2306 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2307 lrbp->utr_descriptor_ptr->prd_table_length =
2308 cpu_to_le16((sg_segments *
2309 sizeof(struct ufshcd_sg_entry)));
2310 else
2311 lrbp->utr_descriptor_ptr->prd_table_length =
2312 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302313
2314 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2315
2316 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2317 prd_table[i].size =
2318 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2319 prd_table[i].base_addr =
2320 cpu_to_le32(lower_32_bits(sg->dma_address));
2321 prd_table[i].upper_addr =
2322 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002323 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302324 }
2325 } else {
2326 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2327 }
2328
2329 return 0;
2330}
2331
2332/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302333 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302334 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302335 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302336 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302337static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302338{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302339 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2340
2341 if (hba->ufs_version == UFSHCI_VERSION_10) {
2342 u32 rw;
2343 rw = set & INTERRUPT_MASK_RW_VER_10;
2344 set = rw | ((set ^ intrs) & intrs);
2345 } else {
2346 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302347 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302348
2349 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2350}
2351
2352/**
2353 * ufshcd_disable_intr - disable interrupts
2354 * @hba: per adapter instance
2355 * @intrs: interrupt bits
2356 */
2357static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2358{
2359 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2360
2361 if (hba->ufs_version == UFSHCI_VERSION_10) {
2362 u32 rw;
2363 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2364 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2365 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2366
2367 } else {
2368 set &= ~intrs;
2369 }
2370
2371 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302372}
2373
2374/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302375 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2376 * descriptor according to request
2377 * @lrbp: pointer to local reference block
2378 * @upiu_flags: flags required in the header
2379 * @cmd_dir: requests data direction
2380 */
2381static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Bean Huoa23064c2020-07-06 14:39:36 +02002382 u8 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302383{
2384 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2385 u32 data_direction;
2386 u32 dword_0;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002387 u32 dword_1 = 0;
2388 u32 dword_3 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302389
2390 if (cmd_dir == DMA_FROM_DEVICE) {
2391 data_direction = UTP_DEVICE_TO_HOST;
2392 *upiu_flags = UPIU_CMD_FLAGS_READ;
2393 } else if (cmd_dir == DMA_TO_DEVICE) {
2394 data_direction = UTP_HOST_TO_DEVICE;
2395 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2396 } else {
2397 data_direction = UTP_NO_DATA_TRANSFER;
2398 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2399 }
2400
2401 dword_0 = data_direction | (lrbp->command_type
2402 << UPIU_COMMAND_TYPE_OFFSET);
2403 if (lrbp->intr_cmd)
2404 dword_0 |= UTP_REQ_DESC_INT_CMD;
2405
Satya Tangiraladf043c742020-07-06 20:04:14 +00002406 /* Prepare crypto related dwords */
2407 ufshcd_prepare_req_desc_hdr_crypto(lrbp, &dword_0, &dword_1, &dword_3);
2408
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302409 /* Transfer request descriptor header fields */
2410 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002411 req_desc->header.dword_1 = cpu_to_le32(dword_1);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302412 /*
2413 * assigning invalid value for command status. Controller
2414 * updates OCS on command completion, with the command
2415 * status
2416 */
2417 req_desc->header.dword_2 =
2418 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002419 req_desc->header.dword_3 = cpu_to_le32(dword_3);
Yaniv Gardi51047262016-02-01 15:02:38 +02002420
2421 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302422}
2423
2424/**
2425 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2426 * for scsi commands
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002427 * @lrbp: local reference block pointer
2428 * @upiu_flags: flags
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302429 */
2430static
Bean Huoa23064c2020-07-06 14:39:36 +02002431void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302432{
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002433 struct scsi_cmnd *cmd = lrbp->cmd;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302434 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002435 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302436
2437 /* command descriptor fields */
2438 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2439 UPIU_TRANSACTION_COMMAND, upiu_flags,
2440 lrbp->lun, lrbp->task_tag);
2441 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2442 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2443
2444 /* Total EHS length and Data segment length will be zero */
2445 ucd_req_ptr->header.dword_2 = 0;
2446
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002447 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302448
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002449 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE);
Avri Altmana851b2b2018-10-07 17:30:34 +03002450 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002451 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002452
2453 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302454}
2455
Dolev Raviv68078d52013-07-30 00:35:58 +05302456/**
2457 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2458 * for query requsts
2459 * @hba: UFS hba
2460 * @lrbp: local reference block pointer
2461 * @upiu_flags: flags
2462 */
2463static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
Bean Huoa23064c2020-07-06 14:39:36 +02002464 struct ufshcd_lrb *lrbp, u8 upiu_flags)
Dolev Raviv68078d52013-07-30 00:35:58 +05302465{
2466 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2467 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302468 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302469
2470 /* Query request header */
2471 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2472 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2473 lrbp->lun, lrbp->task_tag);
2474 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2475 0, query->request.query_func, 0, 0);
2476
Zang Leigang68612852016-08-25 17:39:19 +08002477 /* Data segment length only need for WRITE_DESC */
2478 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2479 ucd_req_ptr->header.dword_2 =
2480 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2481 else
2482 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302483
2484 /* Copy the Query Request buffer as is */
2485 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2486 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302487
2488 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002489 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
Avri Altman220d17a62018-10-07 17:30:36 +03002490 memcpy(ucd_req_ptr + 1, query->descriptor, len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002491
Yaniv Gardi51047262016-02-01 15:02:38 +02002492 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302493}
2494
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302495static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2496{
2497 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2498
2499 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2500
2501 /* command descriptor fields */
2502 ucd_req_ptr->header.dword_0 =
2503 UPIU_HEADER_DWORD(
2504 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002505 /* clear rest of the fields of basic header */
2506 ucd_req_ptr->header.dword_1 = 0;
2507 ucd_req_ptr->header.dword_2 = 0;
2508
2509 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302510}
2511
2512/**
Bean Huof273c542020-08-14 11:50:33 +02002513 * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU)
Joao Pinto300bb132016-05-11 12:21:27 +01002514 * for Device Management Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002515 * @hba: per adapter instance
2516 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302517 */
Bean Huof273c542020-08-14 11:50:33 +02002518static int ufshcd_compose_devman_upiu(struct ufs_hba *hba,
2519 struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302520{
Bean Huoa23064c2020-07-06 14:39:36 +02002521 u8 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302522 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302523
kehuanlin83dc7e32017-09-06 17:58:39 +08002524 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2525 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002526 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
kehuanlin83dc7e32017-09-06 17:58:39 +08002527 else
2528 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002529
2530 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2531 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2532 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2533 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2534 ufshcd_prepare_utp_nop_upiu(lrbp);
2535 else
2536 ret = -EINVAL;
2537
2538 return ret;
2539}
2540
2541/**
2542 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2543 * for SCSI Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002544 * @hba: per adapter instance
2545 * @lrbp: pointer to local reference block
Joao Pinto300bb132016-05-11 12:21:27 +01002546 */
2547static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2548{
Bean Huoa23064c2020-07-06 14:39:36 +02002549 u8 upiu_flags;
Joao Pinto300bb132016-05-11 12:21:27 +01002550 int ret = 0;
2551
kehuanlin83dc7e32017-09-06 17:58:39 +08002552 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2553 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002554 lrbp->command_type = UTP_CMD_TYPE_SCSI;
kehuanlin83dc7e32017-09-06 17:58:39 +08002555 else
2556 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002557
2558 if (likely(lrbp->cmd)) {
2559 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2560 lrbp->cmd->sc_data_direction);
2561 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2562 } else {
2563 ret = -EINVAL;
2564 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302565
2566 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302567}
2568
2569/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002570 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002571 * @upiu_wlun_id: UPIU W-LUN id
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002572 *
2573 * Returns SCSI W-LUN id
2574 */
2575static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2576{
2577 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2578}
2579
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08002580static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i)
2581{
2582 struct utp_transfer_cmd_desc *cmd_descp = hba->ucdl_base_addr;
2583 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr;
2584 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr +
2585 i * sizeof(struct utp_transfer_cmd_desc);
2586 u16 response_offset = offsetof(struct utp_transfer_cmd_desc,
2587 response_upiu);
2588 u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
2589
2590 lrb->utr_descriptor_ptr = utrdlp + i;
2591 lrb->utrd_dma_addr = hba->utrdl_dma_addr +
2592 i * sizeof(struct utp_transfer_req_desc);
2593 lrb->ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i);
2594 lrb->ucd_req_dma_addr = cmd_desc_element_addr;
2595 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2596 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset;
2597 lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2598 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset;
2599}
2600
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002601/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302602 * ufshcd_queuecommand - main entry point for SCSI requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002603 * @host: SCSI host pointer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302604 * @cmd: command from SCSI Midlayer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302605 *
2606 * Returns 0 for success, non-zero in case of failure
2607 */
2608static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2609{
2610 struct ufshcd_lrb *lrbp;
2611 struct ufs_hba *hba;
2612 unsigned long flags;
2613 int tag;
2614 int err = 0;
2615
2616 hba = shost_priv(host);
2617
2618 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002619 if (!ufshcd_valid_tag(hba, tag)) {
2620 dev_err(hba->dev,
2621 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2622 __func__, tag, cmd, cmd->request);
2623 BUG();
2624 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302625
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002626 if (!down_read_trylock(&hba->clk_scaling_lock))
2627 return SCSI_MLQUEUE_HOST_BUSY;
2628
Gilad Broner7fabb772017-02-03 16:56:50 -08002629 hba->req_abort_count = 0;
2630
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002631 err = ufshcd_hold(hba, true);
2632 if (err) {
2633 err = SCSI_MLQUEUE_HOST_BUSY;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002634 goto out;
2635 }
Can Guo2dec9472020-08-09 05:15:47 -07002636 WARN_ON(ufshcd_is_clkgating_allowed(hba) &&
2637 (hba->clk_gating.state != CLKS_ON));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002638
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302639 lrbp = &hba->lrb[tag];
Can Guo7a7e66c2020-12-02 04:04:02 -08002640 if (unlikely(lrbp->in_use)) {
2641 if (hba->pm_op_in_progress)
2642 set_host_byte(cmd, DID_BAD_TARGET);
2643 else
2644 err = SCSI_MLQUEUE_HOST_BUSY;
2645 ufshcd_release(hba);
2646 goto out;
2647 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302648
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302649 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302650 lrbp->cmd = cmd;
Avri Altman09a5a242018-11-22 20:04:56 +02002651 lrbp->sense_bufflen = UFS_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302652 lrbp->sense_buffer = cmd->sense_buffer;
2653 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002654 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002655 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002656
2657 ufshcd_prepare_lrbp_crypto(cmd->request, lrbp);
2658
Gilad Bronere0b299e2017-02-03 16:56:40 -08002659 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302660
Joao Pinto300bb132016-05-11 12:21:27 +01002661 ufshcd_comp_scsi_upiu(hba, lrbp);
2662
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002663 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302664 if (err) {
2665 lrbp->cmd = NULL;
Can Guo17c7d352019-12-05 02:14:33 +00002666 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302667 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302668 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002669 /* Make sure descriptors are ready before ringing the doorbell */
2670 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302671
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302672 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo5586dd82020-08-09 05:15:54 -07002673 switch (hba->ufshcd_state) {
2674 case UFSHCD_STATE_OPERATIONAL:
2675 case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL:
2676 break;
2677 case UFSHCD_STATE_EH_SCHEDULED_FATAL:
2678 /*
2679 * pm_runtime_get_sync() is used at error handling preparation
2680 * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's
2681 * PM ops, it can never be finished if we let SCSI layer keep
2682 * retrying it, which gets err handler stuck forever. Neither
2683 * can we let the scsi cmd pass through, because UFS is in bad
2684 * state, the scsi cmd may eventually time out, which will get
2685 * err handler blocked for too long. So, just fail the scsi cmd
2686 * sent from PM ops, err handler can recover PM error anyways.
2687 */
2688 if (hba->pm_op_in_progress) {
2689 hba->force_reset = true;
2690 set_host_byte(cmd, DID_BAD_TARGET);
2691 goto out_compl_cmd;
2692 }
2693 fallthrough;
2694 case UFSHCD_STATE_RESET:
2695 err = SCSI_MLQUEUE_HOST_BUSY;
2696 goto out_compl_cmd;
2697 case UFSHCD_STATE_ERROR:
2698 set_host_byte(cmd, DID_ERROR);
2699 goto out_compl_cmd;
2700 default:
2701 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2702 __func__, hba->ufshcd_state);
2703 set_host_byte(cmd, DID_BAD_TARGET);
2704 goto out_compl_cmd;
2705 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302706 ufshcd_send_command(hba, tag);
2707 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guo5586dd82020-08-09 05:15:54 -07002708 goto out;
2709
2710out_compl_cmd:
2711 scsi_dma_unmap(lrbp->cmd);
2712 lrbp->cmd = NULL;
2713 spin_unlock_irqrestore(hba->host->host_lock, flags);
2714 ufshcd_release(hba);
2715 if (!err)
2716 cmd->scsi_done(cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302717out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002718 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302719 return err;
2720}
2721
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302722static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2723 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2724{
2725 lrbp->cmd = NULL;
2726 lrbp->sense_bufflen = 0;
2727 lrbp->sense_buffer = NULL;
2728 lrbp->task_tag = tag;
2729 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302730 lrbp->intr_cmd = true; /* No interrupt aggregation */
Satya Tangiraladf043c742020-07-06 20:04:14 +00002731 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302732 hba->dev_cmd.type = cmd_type;
2733
Bean Huof273c542020-08-14 11:50:33 +02002734 return ufshcd_compose_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302735}
2736
2737static int
2738ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2739{
2740 int err = 0;
2741 unsigned long flags;
2742 u32 mask = 1 << tag;
2743
2744 /* clear outstanding transaction before retry */
2745 spin_lock_irqsave(hba->host->host_lock, flags);
2746 ufshcd_utrl_clear(hba, tag);
2747 spin_unlock_irqrestore(hba->host->host_lock, flags);
2748
2749 /*
2750 * wait for for h/w to clear corresponding bit in door-bell.
2751 * max. wait is 1 sec.
2752 */
2753 err = ufshcd_wait_for_register(hba,
2754 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Bart Van Assche5cac1092020-05-07 15:27:50 -07002755 mask, ~mask, 1000, 1000);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302756
2757 return err;
2758}
2759
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002760static int
2761ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2762{
2763 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2764
2765 /* Get the UPIU response */
2766 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2767 UPIU_RSP_CODE_OFFSET;
2768 return query_res->response;
2769}
2770
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302771/**
2772 * ufshcd_dev_cmd_completion() - handles device management command responses
2773 * @hba: per adapter instance
2774 * @lrbp: pointer to local reference block
2775 */
2776static int
2777ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2778{
2779 int resp;
2780 int err = 0;
2781
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002782 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302783 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2784
2785 switch (resp) {
2786 case UPIU_TRANSACTION_NOP_IN:
2787 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2788 err = -EINVAL;
2789 dev_err(hba->dev, "%s: unexpected response %x\n",
2790 __func__, resp);
2791 }
2792 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302793 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002794 err = ufshcd_check_query_response(hba, lrbp);
2795 if (!err)
2796 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302797 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302798 case UPIU_TRANSACTION_REJECT_UPIU:
2799 /* TODO: handle Reject UPIU Response */
2800 err = -EPERM;
2801 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2802 __func__);
2803 break;
2804 default:
2805 err = -EINVAL;
2806 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2807 __func__, resp);
2808 break;
2809 }
2810
2811 return err;
2812}
2813
2814static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2815 struct ufshcd_lrb *lrbp, int max_timeout)
2816{
2817 int err = 0;
2818 unsigned long time_left;
2819 unsigned long flags;
2820
2821 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2822 msecs_to_jiffies(max_timeout));
2823
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002824 /* Make sure descriptors are ready before ringing the doorbell */
2825 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302826 spin_lock_irqsave(hba->host->host_lock, flags);
2827 hba->dev_cmd.complete = NULL;
2828 if (likely(time_left)) {
2829 err = ufshcd_get_tr_ocs(lrbp);
2830 if (!err)
2831 err = ufshcd_dev_cmd_completion(hba, lrbp);
2832 }
2833 spin_unlock_irqrestore(hba->host->host_lock, flags);
2834
2835 if (!time_left) {
2836 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002837 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2838 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302839 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002840 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302841 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002842 /*
2843 * in case of an error, after clearing the doorbell,
2844 * we also need to clear the outstanding_request
2845 * field in hba
2846 */
2847 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302848 }
2849
2850 return err;
2851}
2852
2853/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302854 * ufshcd_exec_dev_cmd - API for sending device management requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002855 * @hba: UFS hba
2856 * @cmd_type: specifies the type (NOP, Query...)
2857 * @timeout: time in seconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302858 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302859 * NOTE: Since there is only one available tag for device management commands,
2860 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302861 */
2862static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2863 enum dev_cmd_type cmd_type, int timeout)
2864{
Bart Van Assche7252a362019-12-09 10:13:08 -08002865 struct request_queue *q = hba->cmd_queue;
2866 struct request *req;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302867 struct ufshcd_lrb *lrbp;
2868 int err;
2869 int tag;
2870 struct completion wait;
2871 unsigned long flags;
2872
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002873 down_read(&hba->clk_scaling_lock);
2874
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302875 /*
2876 * Get free slot, sleep if slots are unavailable.
2877 * Even though we use wait_event() which sleeps indefinitely,
2878 * the maximum wait time is bounded by SCSI request timeout.
2879 */
Bart Van Assche7252a362019-12-09 10:13:08 -08002880 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03002881 if (IS_ERR(req)) {
2882 err = PTR_ERR(req);
2883 goto out_unlock;
2884 }
Bart Van Assche7252a362019-12-09 10:13:08 -08002885 tag = req->tag;
2886 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302887
2888 init_completion(&wait);
2889 lrbp = &hba->lrb[tag];
Can Guo7a7e66c2020-12-02 04:04:02 -08002890 if (unlikely(lrbp->in_use)) {
2891 err = -EBUSY;
2892 goto out;
2893 }
2894
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302895 WARN_ON(lrbp->cmd);
2896 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2897 if (unlikely(err))
2898 goto out_put_tag;
2899
2900 hba->dev_cmd.complete = &wait;
2901
Avri Altmanfb475b72021-01-10 10:46:18 +02002902 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr);
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002903 /* Make sure descriptors are ready before ringing the doorbell */
2904 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302905 spin_lock_irqsave(hba->host->host_lock, flags);
2906 ufshcd_send_command(hba, tag);
2907 spin_unlock_irqrestore(hba->host->host_lock, flags);
2908
2909 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2910
Can Guo7a7e66c2020-12-02 04:04:02 -08002911out:
Avri Altmanfb475b72021-01-10 10:46:18 +02002912 ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP,
2913 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03002914
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302915out_put_tag:
Bart Van Assche7252a362019-12-09 10:13:08 -08002916 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03002917out_unlock:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002918 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302919 return err;
2920}
2921
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302922/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002923 * ufshcd_init_query() - init the query response and request parameters
2924 * @hba: per-adapter instance
2925 * @request: address of the request pointer to be initialized
2926 * @response: address of the response pointer to be initialized
2927 * @opcode: operation to perform
2928 * @idn: flag idn to access
2929 * @index: LU number to access
2930 * @selector: query/flag/descriptor further identification
2931 */
2932static inline void ufshcd_init_query(struct ufs_hba *hba,
2933 struct ufs_query_req **request, struct ufs_query_res **response,
2934 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2935{
2936 *request = &hba->dev_cmd.query.request;
2937 *response = &hba->dev_cmd.query.response;
2938 memset(*request, 0, sizeof(struct ufs_query_req));
2939 memset(*response, 0, sizeof(struct ufs_query_res));
2940 (*request)->upiu_req.opcode = opcode;
2941 (*request)->upiu_req.idn = idn;
2942 (*request)->upiu_req.index = index;
2943 (*request)->upiu_req.selector = selector;
2944}
2945
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002946static int ufshcd_query_flag_retry(struct ufs_hba *hba,
Stanley Chu1f34eed2020-05-08 16:01:12 +08002947 enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002948{
2949 int ret;
2950 int retries;
2951
2952 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
Stanley Chu1f34eed2020-05-08 16:01:12 +08002953 ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res);
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002954 if (ret)
2955 dev_dbg(hba->dev,
2956 "%s: failed with error %d, retries %d\n",
2957 __func__, ret, retries);
2958 else
2959 break;
2960 }
2961
2962 if (ret)
2963 dev_err(hba->dev,
2964 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2965 __func__, opcode, idn, ret, retries);
2966 return ret;
2967}
2968
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002969/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302970 * ufshcd_query_flag() - API function for sending flag query requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002971 * @hba: per-adapter instance
2972 * @opcode: flag query to perform
2973 * @idn: flag idn to access
Stanley Chu1f34eed2020-05-08 16:01:12 +08002974 * @index: flag index to access
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002975 * @flag_res: the flag value after the query request completes
Dolev Raviv68078d52013-07-30 00:35:58 +05302976 *
2977 * Returns 0 for success, non-zero in case of failure
2978 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002979int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Stanley Chu1f34eed2020-05-08 16:01:12 +08002980 enum flag_idn idn, u8 index, bool *flag_res)
Dolev Raviv68078d52013-07-30 00:35:58 +05302981{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002982 struct ufs_query_req *request = NULL;
2983 struct ufs_query_res *response = NULL;
Stanley Chu1f34eed2020-05-08 16:01:12 +08002984 int err, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002985 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302986
2987 BUG_ON(!hba);
2988
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002989 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302990 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002991 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2992 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302993
2994 switch (opcode) {
2995 case UPIU_QUERY_OPCODE_SET_FLAG:
2996 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2997 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2998 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2999 break;
3000 case UPIU_QUERY_OPCODE_READ_FLAG:
3001 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3002 if (!flag_res) {
3003 /* No dummy reads */
3004 dev_err(hba->dev, "%s: Invalid argument for read request\n",
3005 __func__);
3006 err = -EINVAL;
3007 goto out_unlock;
3008 }
3009 break;
3010 default:
3011 dev_err(hba->dev,
3012 "%s: Expected query flag opcode but got = %d\n",
3013 __func__, opcode);
3014 err = -EINVAL;
3015 goto out_unlock;
3016 }
Dolev Raviv68078d52013-07-30 00:35:58 +05303017
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003018 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05303019
3020 if (err) {
3021 dev_err(hba->dev,
3022 "%s: Sending flag query for idn %d failed, err = %d\n",
3023 __func__, idn, err);
3024 goto out_unlock;
3025 }
3026
3027 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303028 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05303029 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
3030
3031out_unlock:
3032 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003033 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05303034 return err;
3035}
3036
3037/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303038 * ufshcd_query_attr - API function for sending attribute requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08003039 * @hba: per-adapter instance
3040 * @opcode: attribute opcode
3041 * @idn: attribute idn to access
3042 * @index: index field
3043 * @selector: selector field
3044 * @attr_val: the attribute value after the query request completes
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303045 *
3046 * Returns 0 for success, non-zero in case of failure
3047*/
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02003048int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
3049 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303050{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003051 struct ufs_query_req *request = NULL;
3052 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303053 int err;
3054
3055 BUG_ON(!hba);
3056
3057 if (!attr_val) {
3058 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
3059 __func__, opcode);
jintae jang8ca1a402020-12-03 14:25:32 +09003060 return -EINVAL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303061 }
3062
jintae jang8ca1a402020-12-03 14:25:32 +09003063 ufshcd_hold(hba, false);
3064
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303065 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003066 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3067 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303068
3069 switch (opcode) {
3070 case UPIU_QUERY_OPCODE_WRITE_ATTR:
3071 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303072 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303073 break;
3074 case UPIU_QUERY_OPCODE_READ_ATTR:
3075 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3076 break;
3077 default:
3078 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
3079 __func__, opcode);
3080 err = -EINVAL;
3081 goto out_unlock;
3082 }
3083
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003084 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303085
3086 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08003087 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3088 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303089 goto out_unlock;
3090 }
3091
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303092 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303093
3094out_unlock:
3095 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003096 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303097 return err;
3098}
3099
3100/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003101 * ufshcd_query_attr_retry() - API function for sending query
3102 * attribute with retries
3103 * @hba: per-adapter instance
3104 * @opcode: attribute opcode
3105 * @idn: attribute idn to access
3106 * @index: index field
3107 * @selector: selector field
3108 * @attr_val: the attribute value after the query request
3109 * completes
3110 *
3111 * Returns 0 for success, non-zero in case of failure
3112*/
3113static int ufshcd_query_attr_retry(struct ufs_hba *hba,
3114 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
3115 u32 *attr_val)
3116{
3117 int ret = 0;
3118 u32 retries;
3119
Bart Van Assche68c9fcf2019-12-24 14:02:43 -08003120 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003121 ret = ufshcd_query_attr(hba, opcode, idn, index,
3122 selector, attr_val);
3123 if (ret)
3124 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
3125 __func__, ret, retries);
3126 else
3127 break;
3128 }
3129
3130 if (ret)
3131 dev_err(hba->dev,
3132 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
3133 __func__, idn, ret, QUERY_REQ_RETRIES);
3134 return ret;
3135}
3136
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003137static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003138 enum query_opcode opcode, enum desc_idn idn, u8 index,
3139 u8 selector, u8 *desc_buf, int *buf_len)
3140{
3141 struct ufs_query_req *request = NULL;
3142 struct ufs_query_res *response = NULL;
3143 int err;
3144
3145 BUG_ON(!hba);
3146
3147 if (!desc_buf) {
3148 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3149 __func__, opcode);
jintae jang8ca1a402020-12-03 14:25:32 +09003150 return -EINVAL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003151 }
3152
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003153 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003154 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3155 __func__, *buf_len);
jintae jang8ca1a402020-12-03 14:25:32 +09003156 return -EINVAL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003157 }
3158
jintae jang8ca1a402020-12-03 14:25:32 +09003159 ufshcd_hold(hba, false);
3160
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003161 mutex_lock(&hba->dev_cmd.lock);
3162 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3163 selector);
3164 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003165 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003166
3167 switch (opcode) {
3168 case UPIU_QUERY_OPCODE_WRITE_DESC:
3169 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3170 break;
3171 case UPIU_QUERY_OPCODE_READ_DESC:
3172 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3173 break;
3174 default:
3175 dev_err(hba->dev,
3176 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3177 __func__, opcode);
3178 err = -EINVAL;
3179 goto out_unlock;
3180 }
3181
3182 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3183
3184 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08003185 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3186 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003187 goto out_unlock;
3188 }
3189
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003190 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003191
3192out_unlock:
Bean Huocfcbae32019-11-12 23:34:36 +01003193 hba->dev_cmd.query.descriptor = NULL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003194 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003195 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003196 return err;
3197}
3198
3199/**
Bart Van Assche8aa29f12018-03-01 15:07:20 -08003200 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
3201 * @hba: per-adapter instance
3202 * @opcode: attribute opcode
3203 * @idn: attribute idn to access
3204 * @index: index field
3205 * @selector: selector field
3206 * @desc_buf: the buffer that contains the descriptor
3207 * @buf_len: length parameter passed to the device
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003208 *
3209 * Returns 0 for success, non-zero in case of failure.
3210 * The buf_len parameter will contain, on return, the length parameter
3211 * received on the response.
3212 */
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02003213int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
3214 enum query_opcode opcode,
3215 enum desc_idn idn, u8 index,
3216 u8 selector,
3217 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003218{
3219 int err;
3220 int retries;
3221
3222 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3223 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3224 selector, desc_buf, buf_len);
3225 if (!err || err == -EINVAL)
3226 break;
3227 }
3228
3229 return err;
3230}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003231
3232/**
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003233 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
3234 * @hba: Pointer to adapter instance
3235 * @desc_id: descriptor idn value
3236 * @desc_len: mapped desc length (out)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003237 */
Bean Huo7a0bf852020-06-03 11:19:58 +02003238void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
3239 int *desc_len)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003240{
Bean Huo7a0bf852020-06-03 11:19:58 +02003241 if (desc_id >= QUERY_DESC_IDN_MAX || desc_id == QUERY_DESC_IDN_RFU_0 ||
3242 desc_id == QUERY_DESC_IDN_RFU_1)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003243 *desc_len = 0;
Bean Huo7a0bf852020-06-03 11:19:58 +02003244 else
3245 *desc_len = hba->desc_size[desc_id];
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003246}
3247EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
3248
Bean Huo7a0bf852020-06-03 11:19:58 +02003249static void ufshcd_update_desc_length(struct ufs_hba *hba,
Bean Huo72fb6902020-06-03 11:19:59 +02003250 enum desc_idn desc_id, int desc_index,
Bean Huo7a0bf852020-06-03 11:19:58 +02003251 unsigned char desc_len)
3252{
3253 if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE &&
Bean Huo72fb6902020-06-03 11:19:59 +02003254 desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT)
3255 /* For UFS 3.1, the normal unit descriptor is 10 bytes larger
3256 * than the RPMB unit, however, both descriptors share the same
3257 * desc_idn, to cover both unit descriptors with one length, we
3258 * choose the normal unit descriptor length by desc_index.
3259 */
Bean Huo7a0bf852020-06-03 11:19:58 +02003260 hba->desc_size[desc_id] = desc_len;
3261}
3262
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003263/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003264 * ufshcd_read_desc_param - read the specified descriptor parameter
3265 * @hba: Pointer to adapter instance
3266 * @desc_id: descriptor idn value
3267 * @desc_index: descriptor index
3268 * @param_offset: offset of the parameter to read
3269 * @param_read_buf: pointer to buffer where parameter would be read
3270 * @param_size: sizeof(param_read_buf)
3271 *
3272 * Return 0 in case of success, non-zero otherwise
3273 */
Stanislav Nijnikov45bced82018-02-15 14:14:02 +02003274int ufshcd_read_desc_param(struct ufs_hba *hba,
3275 enum desc_idn desc_id,
3276 int desc_index,
3277 u8 param_offset,
3278 u8 *param_read_buf,
3279 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003280{
3281 int ret;
3282 u8 *desc_buf;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003283 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003284 bool is_kmalloc = true;
3285
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003286 /* Safety check */
3287 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003288 return -EINVAL;
3289
Bean Huo7a0bf852020-06-03 11:19:58 +02003290 /* Get the length of descriptor */
3291 ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
3292 if (!buff_len) {
Can Guo1699f982020-10-21 22:59:00 -07003293 dev_err(hba->dev, "%s: Failed to get desc length\n", __func__);
3294 return -EINVAL;
3295 }
3296
3297 if (param_offset >= buff_len) {
3298 dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n",
3299 __func__, param_offset, desc_id, buff_len);
Bean Huo7a0bf852020-06-03 11:19:58 +02003300 return -EINVAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003301 }
3302
3303 /* Check whether we need temp memory */
3304 if (param_offset != 0 || param_size < buff_len) {
Can Guo1699f982020-10-21 22:59:00 -07003305 desc_buf = kzalloc(buff_len, GFP_KERNEL);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003306 if (!desc_buf)
3307 return -ENOMEM;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003308 } else {
3309 desc_buf = param_read_buf;
3310 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003311 }
3312
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003313 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003314 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003315 desc_id, desc_index, 0,
3316 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003317
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003318 if (ret) {
Can Guo1699f982020-10-21 22:59:00 -07003319 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 -08003320 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003321 goto out;
3322 }
3323
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003324 /* Sanity check */
3325 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
Can Guo1699f982020-10-21 22:59:00 -07003326 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n",
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003327 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3328 ret = -EINVAL;
3329 goto out;
3330 }
3331
Bean Huo7a0bf852020-06-03 11:19:58 +02003332 /* Update descriptor length */
3333 buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET];
Bean Huo72fb6902020-06-03 11:19:59 +02003334 ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len);
Bean Huo7a0bf852020-06-03 11:19:58 +02003335
Can Guo1699f982020-10-21 22:59:00 -07003336 if (is_kmalloc) {
3337 /* Make sure we don't copy more data than available */
3338 if (param_offset + param_size > buff_len)
3339 param_size = buff_len - param_offset;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003340 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
Can Guo1699f982020-10-21 22:59:00 -07003341 }
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003342out:
3343 if (is_kmalloc)
3344 kfree(desc_buf);
3345 return ret;
3346}
3347
Yaniv Gardib573d482016-03-10 17:37:09 +02003348/**
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003349 * struct uc_string_id - unicode string
3350 *
3351 * @len: size of this descriptor inclusive
3352 * @type: descriptor type
3353 * @uc: unicode string character
3354 */
3355struct uc_string_id {
3356 u8 len;
3357 u8 type;
Gustavo A. R. Silvaec38c0a2020-05-07 14:25:50 -05003358 wchar_t uc[];
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003359} __packed;
3360
3361/* replace non-printable or non-ASCII characters with spaces */
3362static inline char ufshcd_remove_non_printable(u8 ch)
3363{
3364 return (ch >= 0x20 && ch <= 0x7e) ? ch : ' ';
3365}
3366
3367/**
Yaniv Gardib573d482016-03-10 17:37:09 +02003368 * ufshcd_read_string_desc - read string descriptor
3369 * @hba: pointer to adapter instance
3370 * @desc_index: descriptor index
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003371 * @buf: pointer to buffer where descriptor would be read,
3372 * the caller should free the memory.
Yaniv Gardib573d482016-03-10 17:37:09 +02003373 * @ascii: if true convert from unicode to ascii characters
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003374 * null terminated string.
Yaniv Gardib573d482016-03-10 17:37:09 +02003375 *
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003376 * Return:
3377 * * string size on success.
3378 * * -ENOMEM: on allocation failure
3379 * * -EINVAL: on a wrong parameter
Yaniv Gardib573d482016-03-10 17:37:09 +02003380 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003381int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
3382 u8 **buf, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003383{
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003384 struct uc_string_id *uc_str;
3385 u8 *str;
3386 int ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003387
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003388 if (!buf)
3389 return -EINVAL;
Yaniv Gardib573d482016-03-10 17:37:09 +02003390
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003391 uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
3392 if (!uc_str)
3393 return -ENOMEM;
3394
Bean Huoc4607a02020-06-03 11:19:56 +02003395 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0,
3396 (u8 *)uc_str, QUERY_DESC_MAX_SIZE);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003397 if (ret < 0) {
3398 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
3399 QUERY_REQ_RETRIES, ret);
3400 str = NULL;
3401 goto out;
3402 }
3403
3404 if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
3405 dev_dbg(hba->dev, "String Desc is of zero length\n");
3406 str = NULL;
3407 ret = 0;
Yaniv Gardib573d482016-03-10 17:37:09 +02003408 goto out;
3409 }
3410
3411 if (ascii) {
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003412 ssize_t ascii_len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003413 int i;
Yaniv Gardib573d482016-03-10 17:37:09 +02003414 /* remove header and divide by 2 to move from UTF16 to UTF8 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003415 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3416 str = kzalloc(ascii_len, GFP_KERNEL);
3417 if (!str) {
3418 ret = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003419 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003420 }
3421
3422 /*
3423 * the descriptor contains string in UTF16 format
3424 * we need to convert to utf-8 so it can be displayed
3425 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003426 ret = utf16s_to_utf8s(uc_str->uc,
3427 uc_str->len - QUERY_DESC_HDR_SIZE,
3428 UTF16_BIG_ENDIAN, str, ascii_len);
Yaniv Gardib573d482016-03-10 17:37:09 +02003429
3430 /* replace non-printable or non-ASCII characters with spaces */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003431 for (i = 0; i < ret; i++)
3432 str[i] = ufshcd_remove_non_printable(str[i]);
Yaniv Gardib573d482016-03-10 17:37:09 +02003433
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003434 str[ret++] = '\0';
3435
3436 } else {
YueHaibing5f577042019-08-31 12:44:24 +00003437 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003438 if (!str) {
3439 ret = -ENOMEM;
3440 goto out;
3441 }
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003442 ret = uc_str->len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003443 }
3444out:
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003445 *buf = str;
3446 kfree(uc_str);
3447 return ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003448}
Yaniv Gardib573d482016-03-10 17:37:09 +02003449
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003450/**
3451 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3452 * @hba: Pointer to adapter instance
3453 * @lun: lun id
3454 * @param_offset: offset of the parameter to read
3455 * @param_read_buf: pointer to buffer where parameter would be read
3456 * @param_size: sizeof(param_read_buf)
3457 *
3458 * Return 0 in case of success, non-zero otherwise
3459 */
3460static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3461 int lun,
3462 enum unit_desc_param param_offset,
3463 u8 *param_read_buf,
3464 u32 param_size)
3465{
3466 /*
3467 * Unit descriptors are only available for general purpose LUs (LUN id
3468 * from 0 to 7) and RPMB Well known LU.
3469 */
Jaegeuk Kima2fca522021-01-11 01:59:27 -08003470 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun, param_offset))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003471 return -EOPNOTSUPP;
3472
3473 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3474 param_offset, param_read_buf, param_size);
3475}
3476
Can Guo09f17792020-02-10 19:40:49 -08003477static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba)
3478{
3479 int err = 0;
3480 u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3481
3482 if (hba->dev_info.wspecversion >= 0x300) {
3483 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3484 QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0,
3485 &gating_wait);
3486 if (err)
3487 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n",
3488 err, gating_wait);
3489
3490 if (gating_wait == 0) {
3491 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3492 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n",
3493 gating_wait);
3494 }
3495
3496 hba->dev_info.clk_gating_wait_us = gating_wait;
3497 }
3498
3499 return err;
3500}
3501
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003502/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303503 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3504 * @hba: per adapter instance
3505 *
3506 * 1. Allocate DMA memory for Command Descriptor array
3507 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3508 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3509 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3510 * (UTMRDL)
3511 * 4. Allocate memory for local reference block(lrb).
3512 *
3513 * Returns 0 for success, non-zero in case of failure
3514 */
3515static int ufshcd_memory_alloc(struct ufs_hba *hba)
3516{
3517 size_t utmrdl_size, utrdl_size, ucdl_size;
3518
3519 /* Allocate memory for UTP command descriptors */
3520 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003521 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3522 ucdl_size,
3523 &hba->ucdl_dma_addr,
3524 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303525
3526 /*
3527 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3528 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3529 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3530 * be aligned to 128 bytes as well
3531 */
3532 if (!hba->ucdl_base_addr ||
3533 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303534 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303535 "Command Descriptor Memory allocation failed\n");
3536 goto out;
3537 }
3538
3539 /*
3540 * Allocate memory for UTP Transfer descriptors
3541 * UFSHCI requires 1024 byte alignment of UTRD
3542 */
3543 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003544 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3545 utrdl_size,
3546 &hba->utrdl_dma_addr,
3547 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303548 if (!hba->utrdl_base_addr ||
3549 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303550 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303551 "Transfer Descriptor Memory allocation failed\n");
3552 goto out;
3553 }
3554
3555 /*
3556 * Allocate memory for UTP Task Management descriptors
3557 * UFSHCI requires 1024 byte alignment of UTMRD
3558 */
3559 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003560 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3561 utmrdl_size,
3562 &hba->utmrdl_dma_addr,
3563 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303564 if (!hba->utmrdl_base_addr ||
3565 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303566 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303567 "Task Management Descriptor Memory allocation failed\n");
3568 goto out;
3569 }
3570
3571 /* Allocate memory for local reference block */
Kees Cooka86854d2018-06-12 14:07:58 -07003572 hba->lrb = devm_kcalloc(hba->dev,
3573 hba->nutrs, sizeof(struct ufshcd_lrb),
Seungwon Jeon2953f852013-06-27 13:31:54 +09003574 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303575 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303576 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303577 goto out;
3578 }
3579 return 0;
3580out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303581 return -ENOMEM;
3582}
3583
3584/**
3585 * ufshcd_host_memory_configure - configure local reference block with
3586 * memory offsets
3587 * @hba: per adapter instance
3588 *
3589 * Configure Host memory space
3590 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3591 * address.
3592 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3593 * and PRDT offset.
3594 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3595 * into local reference block.
3596 */
3597static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3598{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303599 struct utp_transfer_req_desc *utrdlp;
3600 dma_addr_t cmd_desc_dma_addr;
3601 dma_addr_t cmd_desc_element_addr;
3602 u16 response_offset;
3603 u16 prdt_offset;
3604 int cmd_desc_size;
3605 int i;
3606
3607 utrdlp = hba->utrdl_base_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303608
3609 response_offset =
3610 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3611 prdt_offset =
3612 offsetof(struct utp_transfer_cmd_desc, prd_table);
3613
3614 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3615 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3616
3617 for (i = 0; i < hba->nutrs; i++) {
3618 /* Configure UTRD with command descriptor base address */
3619 cmd_desc_element_addr =
3620 (cmd_desc_dma_addr + (cmd_desc_size * i));
3621 utrdlp[i].command_desc_base_addr_lo =
3622 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3623 utrdlp[i].command_desc_base_addr_hi =
3624 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3625
3626 /* Response upiu and prdt offset should be in double words */
Alim Akhtar26f968d2020-05-28 06:46:52 +05303627 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3628 utrdlp[i].response_upiu_offset =
3629 cpu_to_le16(response_offset);
3630 utrdlp[i].prd_table_offset =
3631 cpu_to_le16(prdt_offset);
3632 utrdlp[i].response_upiu_length =
3633 cpu_to_le16(ALIGNED_UPIU_SIZE);
3634 } else {
3635 utrdlp[i].response_upiu_offset =
3636 cpu_to_le16(response_offset >> 2);
3637 utrdlp[i].prd_table_offset =
3638 cpu_to_le16(prdt_offset >> 2);
3639 utrdlp[i].response_upiu_length =
3640 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
3641 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303642
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08003643 ufshcd_init_lrb(hba, &hba->lrb[i], i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303644 }
3645}
3646
3647/**
3648 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3649 * @hba: per adapter instance
3650 *
3651 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3652 * in order to initialize the Unipro link startup procedure.
3653 * Once the Unipro links are up, the device connected to the controller
3654 * is detected.
3655 *
3656 * Returns 0 on success, non-zero value on failure
3657 */
3658static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3659{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303660 struct uic_command uic_cmd = {0};
3661 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303662
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303663 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3664
3665 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3666 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003667 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303668 "dme-link-startup: error code %d\n", ret);
3669 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303670}
Alim Akhtar39bf2d82020-05-28 06:46:51 +05303671/**
3672 * ufshcd_dme_reset - UIC command for DME_RESET
3673 * @hba: per adapter instance
3674 *
3675 * DME_RESET command is issued in order to reset UniPro stack.
3676 * This function now deals with cold reset.
3677 *
3678 * Returns 0 on success, non-zero value on failure
3679 */
3680static int ufshcd_dme_reset(struct ufs_hba *hba)
3681{
3682 struct uic_command uic_cmd = {0};
3683 int ret;
3684
3685 uic_cmd.command = UIC_CMD_DME_RESET;
3686
3687 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3688 if (ret)
3689 dev_err(hba->dev,
3690 "dme-reset: error code %d\n", ret);
3691
3692 return ret;
3693}
3694
Stanley Chufc85a742020-11-16 14:50:52 +08003695int ufshcd_dme_configure_adapt(struct ufs_hba *hba,
3696 int agreed_gear,
3697 int adapt_val)
3698{
3699 int ret;
3700
3701 if (agreed_gear != UFS_HS_G4)
Bjorn Andersson66df79c2020-11-20 20:48:10 -08003702 adapt_val = PA_NO_ADAPT;
Stanley Chufc85a742020-11-16 14:50:52 +08003703
3704 ret = ufshcd_dme_set(hba,
3705 UIC_ARG_MIB(PA_TXHSADAPTTYPE),
3706 adapt_val);
3707 return ret;
3708}
3709EXPORT_SYMBOL_GPL(ufshcd_dme_configure_adapt);
3710
Alim Akhtar39bf2d82020-05-28 06:46:51 +05303711/**
3712 * ufshcd_dme_enable - UIC command for DME_ENABLE
3713 * @hba: per adapter instance
3714 *
3715 * DME_ENABLE command is issued in order to enable UniPro stack.
3716 *
3717 * Returns 0 on success, non-zero value on failure
3718 */
3719static int ufshcd_dme_enable(struct ufs_hba *hba)
3720{
3721 struct uic_command uic_cmd = {0};
3722 int ret;
3723
3724 uic_cmd.command = UIC_CMD_DME_ENABLE;
3725
3726 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3727 if (ret)
3728 dev_err(hba->dev,
Bean Huo1fa05702020-12-07 20:01:37 +01003729 "dme-enable: error code %d\n", ret);
Alim Akhtar39bf2d82020-05-28 06:46:51 +05303730
3731 return ret;
3732}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303733
Yaniv Gardicad2e032015-03-31 17:37:14 +03003734static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3735{
3736 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3737 unsigned long min_sleep_time_us;
3738
3739 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3740 return;
3741
3742 /*
3743 * last_dme_cmd_tstamp will be 0 only for 1st call to
3744 * this function
3745 */
3746 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3747 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3748 } else {
3749 unsigned long delta =
3750 (unsigned long) ktime_to_us(
3751 ktime_sub(ktime_get(),
3752 hba->last_dme_cmd_tstamp));
3753
3754 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3755 min_sleep_time_us =
3756 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3757 else
3758 return; /* no more delay required */
3759 }
3760
3761 /* allow sleep for extra 50us if needed */
3762 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3763}
3764
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303765/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303766 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3767 * @hba: per adapter instance
3768 * @attr_sel: uic command argument1
3769 * @attr_set: attribute set type as uic command argument2
3770 * @mib_val: setting value as uic command argument3
3771 * @peer: indicate whether peer or local
3772 *
3773 * Returns 0 on success, non-zero value on failure
3774 */
3775int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3776 u8 attr_set, u32 mib_val, u8 peer)
3777{
3778 struct uic_command uic_cmd = {0};
3779 static const char *const action[] = {
3780 "dme-set",
3781 "dme-peer-set"
3782 };
3783 const char *set = action[!!peer];
3784 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003785 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303786
3787 uic_cmd.command = peer ?
3788 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3789 uic_cmd.argument1 = attr_sel;
3790 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3791 uic_cmd.argument3 = mib_val;
3792
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003793 do {
3794 /* for peer attributes we retry upon failure */
3795 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3796 if (ret)
3797 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3798 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3799 } while (ret && peer && --retries);
3800
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003801 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003802 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003803 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3804 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303805
3806 return ret;
3807}
3808EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3809
3810/**
3811 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3812 * @hba: per adapter instance
3813 * @attr_sel: uic command argument1
3814 * @mib_val: the value of the attribute as returned by the UIC command
3815 * @peer: indicate whether peer or local
3816 *
3817 * Returns 0 on success, non-zero value on failure
3818 */
3819int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3820 u32 *mib_val, u8 peer)
3821{
3822 struct uic_command uic_cmd = {0};
3823 static const char *const action[] = {
3824 "dme-get",
3825 "dme-peer-get"
3826 };
3827 const char *get = action[!!peer];
3828 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003829 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003830 struct ufs_pa_layer_attr orig_pwr_info;
3831 struct ufs_pa_layer_attr temp_pwr_info;
3832 bool pwr_mode_change = false;
3833
3834 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3835 orig_pwr_info = hba->pwr_info;
3836 temp_pwr_info = orig_pwr_info;
3837
3838 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3839 orig_pwr_info.pwr_rx == FAST_MODE) {
3840 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3841 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3842 pwr_mode_change = true;
3843 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3844 orig_pwr_info.pwr_rx == SLOW_MODE) {
3845 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3846 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3847 pwr_mode_change = true;
3848 }
3849 if (pwr_mode_change) {
3850 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3851 if (ret)
3852 goto out;
3853 }
3854 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303855
3856 uic_cmd.command = peer ?
3857 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3858 uic_cmd.argument1 = attr_sel;
3859
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003860 do {
3861 /* for peer attributes we retry upon failure */
3862 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3863 if (ret)
3864 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3865 get, UIC_GET_ATTR_ID(attr_sel), ret);
3866 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303867
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003868 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003869 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003870 get, UIC_GET_ATTR_ID(attr_sel),
3871 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003872
3873 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303874 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003875
3876 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3877 && pwr_mode_change)
3878 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303879out:
3880 return ret;
3881}
3882EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3883
3884/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003885 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3886 * state) and waits for it to take effect.
3887 *
3888 * @hba: per adapter instance
3889 * @cmd: UIC command to execute
3890 *
3891 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3892 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3893 * and device UniPro link and hence it's final completion would be indicated by
3894 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3895 * addition to normal UIC command completion Status (UCCS). This function only
3896 * returns after the relevant status bits indicate the completion.
3897 *
3898 * Returns 0 on success, non-zero value on failure
3899 */
3900static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3901{
3902 struct completion uic_async_done;
3903 unsigned long flags;
3904 u8 status;
3905 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003906 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003907
3908 mutex_lock(&hba->uic_cmd_mutex);
3909 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003910 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003911
3912 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo4db7a232020-08-09 05:15:51 -07003913 if (ufshcd_is_link_broken(hba)) {
3914 ret = -ENOLINK;
3915 goto out_unlock;
3916 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003917 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003918 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3919 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3920 /*
3921 * Make sure UIC command completion interrupt is disabled before
3922 * issuing UIC command.
3923 */
3924 wmb();
3925 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003926 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003927 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3928 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003929 if (ret) {
3930 dev_err(hba->dev,
3931 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3932 cmd->command, cmd->argument3, ret);
3933 goto out;
3934 }
3935
3936 if (!wait_for_completion_timeout(hba->uic_async_done,
3937 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3938 dev_err(hba->dev,
3939 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3940 cmd->command, cmd->argument3);
Can Guo0f52fcb92020-11-02 22:24:40 -08003941
3942 if (!cmd->cmd_active) {
3943 dev_err(hba->dev, "%s: Power Mode Change operation has been completed, go check UPMCRS\n",
3944 __func__);
3945 goto check_upmcrs;
3946 }
3947
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003948 ret = -ETIMEDOUT;
3949 goto out;
3950 }
3951
Can Guo0f52fcb92020-11-02 22:24:40 -08003952check_upmcrs:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003953 status = ufshcd_get_upmcrs(hba);
3954 if (status != PWR_LOCAL) {
3955 dev_err(hba->dev,
Zang Leigang479da362017-09-19 16:50:30 +08003956 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003957 cmd->command, status);
3958 ret = (status != PWR_OK) ? status : -1;
3959 }
3960out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003961 if (ret) {
3962 ufshcd_print_host_state(hba);
3963 ufshcd_print_pwr_info(hba);
Stanley Chue965e5e2020-12-05 19:58:59 +08003964 ufshcd_print_evt_hist(hba);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003965 }
3966
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003967 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003968 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003969 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003970 if (reenable_intr)
3971 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Can Guo4db7a232020-08-09 05:15:51 -07003972 if (ret) {
3973 ufshcd_set_link_broken(hba);
3974 ufshcd_schedule_eh_work(hba);
3975 }
3976out_unlock:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003977 spin_unlock_irqrestore(hba->host->host_lock, flags);
3978 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003979
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003980 return ret;
3981}
3982
3983/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303984 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3985 * using DME_SET primitives.
3986 * @hba: per adapter instance
3987 * @mode: powr mode value
3988 *
3989 * Returns 0 on success, non-zero value on failure
3990 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303991static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303992{
3993 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003994 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303995
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003996 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3997 ret = ufshcd_dme_set(hba,
3998 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3999 if (ret) {
4000 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
4001 __func__, ret);
4002 goto out;
4003 }
4004 }
4005
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304006 uic_cmd.command = UIC_CMD_DME_SET;
4007 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
4008 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004009 ufshcd_hold(hba, false);
4010 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
4011 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304012
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03004013out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004014 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004015}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304016
Stanley Chu087c5ef2020-03-27 17:53:28 +08004017int ufshcd_link_recovery(struct ufs_hba *hba)
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004018{
4019 int ret;
4020 unsigned long flags;
4021
4022 spin_lock_irqsave(hba->host->host_lock, flags);
4023 hba->ufshcd_state = UFSHCD_STATE_RESET;
4024 ufshcd_set_eh_in_progress(hba);
4025 spin_unlock_irqrestore(hba->host->host_lock, flags);
4026
Can Guoebdd1df2019-11-14 22:09:24 -08004027 /* Reset the attached device */
Stanley Chu31a5d9c2020-12-08 21:56:35 +08004028 ufshcd_device_reset(hba);
Can Guoebdd1df2019-11-14 22:09:24 -08004029
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004030 ret = ufshcd_host_reset_and_restore(hba);
4031
4032 spin_lock_irqsave(hba->host->host_lock, flags);
4033 if (ret)
4034 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4035 ufshcd_clear_eh_in_progress(hba);
4036 spin_unlock_irqrestore(hba->host->host_lock, flags);
4037
4038 if (ret)
4039 dev_err(hba->dev, "%s: link recovery failed, err %d",
4040 __func__, ret);
4041
4042 return ret;
4043}
Stanley Chu087c5ef2020-03-27 17:53:28 +08004044EXPORT_SYMBOL_GPL(ufshcd_link_recovery);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004045
Can Guo4db7a232020-08-09 05:15:51 -07004046static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004047{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004048 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004049 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08004050 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004051
Kiwoong Kimee32c902016-11-10 21:17:43 +09004052 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
4053
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004054 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004055 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08004056 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
4057 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004058
Can Guo4db7a232020-08-09 05:15:51 -07004059 if (ret)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004060 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
4061 __func__, ret);
Can Guo4db7a232020-08-09 05:15:51 -07004062 else
Kiwoong Kimee32c902016-11-10 21:17:43 +09004063 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
4064 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004065
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004066 return ret;
4067}
4068
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004069int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004070{
4071 struct uic_command uic_cmd = {0};
4072 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08004073 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004074
Kiwoong Kimee32c902016-11-10 21:17:43 +09004075 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
4076
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004077 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
4078 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08004079 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
4080 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
4081
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304082 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004083 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
4084 __func__, ret);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004085 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09004086 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
4087 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004088 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
4089 hba->ufs_stats.hibern8_exit_cnt++;
4090 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304091
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304092 return ret;
4093}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004094EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304095
Stanley Chuba7af5e2019-12-30 13:32:28 +08004096void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit)
4097{
4098 unsigned long flags;
Can Guobe7594a2020-03-05 00:53:07 -08004099 bool update = false;
Stanley Chuba7af5e2019-12-30 13:32:28 +08004100
Can Guobe7594a2020-03-05 00:53:07 -08004101 if (!ufshcd_is_auto_hibern8_supported(hba))
Stanley Chuba7af5e2019-12-30 13:32:28 +08004102 return;
4103
4104 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08004105 if (hba->ahit != ahit) {
4106 hba->ahit = ahit;
4107 update = true;
4108 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08004109 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08004110
4111 if (update && !pm_runtime_suspended(hba->dev)) {
4112 pm_runtime_get_sync(hba->dev);
4113 ufshcd_hold(hba, false);
4114 ufshcd_auto_hibern8_enable(hba);
4115 ufshcd_release(hba);
4116 pm_runtime_put(hba->dev);
4117 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08004118}
4119EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
4120
Can Guo71d848b2019-11-14 22:09:26 -08004121void ufshcd_auto_hibern8_enable(struct ufs_hba *hba)
Adrian Hunterad448372018-03-20 15:07:38 +02004122{
4123 unsigned long flags;
4124
Bao D. Nguyen499f7a92020-08-28 18:05:13 -07004125 if (!ufshcd_is_auto_hibern8_supported(hba))
Adrian Hunterad448372018-03-20 15:07:38 +02004126 return;
4127
4128 spin_lock_irqsave(hba->host->host_lock, flags);
4129 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
4130 spin_unlock_irqrestore(hba->host->host_lock, flags);
4131}
4132
Yaniv Gardi50646362014-10-23 13:25:13 +03004133 /**
4134 * ufshcd_init_pwr_info - setting the POR (power on reset)
4135 * values in hba power info
4136 * @hba: per-adapter instance
4137 */
4138static void ufshcd_init_pwr_info(struct ufs_hba *hba)
4139{
4140 hba->pwr_info.gear_rx = UFS_PWM_G1;
4141 hba->pwr_info.gear_tx = UFS_PWM_G1;
4142 hba->pwr_info.lane_rx = 1;
4143 hba->pwr_info.lane_tx = 1;
4144 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
4145 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
4146 hba->pwr_info.hs_rate = 0;
4147}
4148
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304149/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004150 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4151 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304152 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004153static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304154{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004155 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4156
4157 if (hba->max_pwr_info.is_valid)
4158 return 0;
4159
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004160 pwr_info->pwr_tx = FAST_MODE;
4161 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004162 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304163
4164 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004165 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4166 &pwr_info->lane_rx);
4167 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4168 &pwr_info->lane_tx);
4169
4170 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4171 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4172 __func__,
4173 pwr_info->lane_rx,
4174 pwr_info->lane_tx);
4175 return -EINVAL;
4176 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304177
4178 /*
4179 * First, get the maximum gears of HS speed.
4180 * If a zero value, it means there is no HSGEAR capability.
4181 * Then, get the maximum gears of PWM speed.
4182 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004183 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4184 if (!pwr_info->gear_rx) {
4185 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4186 &pwr_info->gear_rx);
4187 if (!pwr_info->gear_rx) {
4188 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4189 __func__, pwr_info->gear_rx);
4190 return -EINVAL;
4191 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004192 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304193 }
4194
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004195 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4196 &pwr_info->gear_tx);
4197 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304198 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004199 &pwr_info->gear_tx);
4200 if (!pwr_info->gear_tx) {
4201 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4202 __func__, pwr_info->gear_tx);
4203 return -EINVAL;
4204 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004205 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004206 }
4207
4208 hba->max_pwr_info.is_valid = true;
4209 return 0;
4210}
4211
4212static int ufshcd_change_power_mode(struct ufs_hba *hba,
4213 struct ufs_pa_layer_attr *pwr_mode)
4214{
4215 int ret;
4216
4217 /* if already configured to the requested pwr_mode */
Can Guo2355b662020-08-24 19:07:06 -07004218 if (!hba->force_pmc &&
4219 pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004220 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4221 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4222 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4223 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4224 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4225 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4226 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4227 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304228 }
4229
4230 /*
4231 * Configure attributes for power mode change with below.
4232 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4233 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4234 * - PA_HSSERIES
4235 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004236 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4237 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4238 pwr_mode->lane_rx);
4239 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4240 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304241 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004242 else
4243 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304244
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004245 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4246 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4247 pwr_mode->lane_tx);
4248 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4249 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304250 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004251 else
4252 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304253
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004254 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4255 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4256 pwr_mode->pwr_rx == FAST_MODE ||
4257 pwr_mode->pwr_tx == FAST_MODE)
4258 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4259 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304260
Kiwoong Kimb1d0d2e2020-12-21 10:24:40 +09004261 if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) {
4262 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4263 DL_FC0ProtectionTimeOutVal_Default);
4264 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4265 DL_TC0ReplayTimeOutVal_Default);
4266 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4267 DL_AFC0ReqTimeOutVal_Default);
4268 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
4269 DL_FC1ProtectionTimeOutVal_Default);
4270 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
4271 DL_TC1ReplayTimeOutVal_Default);
4272 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
4273 DL_AFC1ReqTimeOutVal_Default);
Can Guo08342532019-12-05 02:14:42 +00004274
Kiwoong Kimb1d0d2e2020-12-21 10:24:40 +09004275 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4276 DL_FC0ProtectionTimeOutVal_Default);
4277 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4278 DL_TC0ReplayTimeOutVal_Default);
4279 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4280 DL_AFC0ReqTimeOutVal_Default);
4281 }
Can Guo08342532019-12-05 02:14:42 +00004282
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004283 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4284 | pwr_mode->pwr_tx);
4285
4286 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304287 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004288 "%s: power mode change failed %d\n", __func__, ret);
4289 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004290 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4291 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004292
4293 memcpy(&hba->pwr_info, pwr_mode,
4294 sizeof(struct ufs_pa_layer_attr));
4295 }
4296
4297 return ret;
4298}
4299
4300/**
4301 * ufshcd_config_pwr_mode - configure a new power mode
4302 * @hba: per-adapter instance
4303 * @desired_pwr_mode: desired power configuration
4304 */
Alim Akhtar0d846e72018-05-06 15:44:18 +05304305int ufshcd_config_pwr_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004306 struct ufs_pa_layer_attr *desired_pwr_mode)
4307{
4308 struct ufs_pa_layer_attr final_params = { 0 };
4309 int ret;
4310
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004311 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4312 desired_pwr_mode, &final_params);
4313
4314 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004315 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4316
4317 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304318
4319 return ret;
4320}
Alim Akhtar0d846e72018-05-06 15:44:18 +05304321EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304322
4323/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304324 * ufshcd_complete_dev_init() - checks device readiness
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004325 * @hba: per-adapter instance
Dolev Raviv68078d52013-07-30 00:35:58 +05304326 *
4327 * Set fDeviceInit flag and poll until device toggles it.
4328 */
4329static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4330{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004331 int err;
Jason Yan7dfdcc32020-04-26 17:43:05 +08004332 bool flag_res = true;
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004333 ktime_t timeout;
Dolev Raviv68078d52013-07-30 00:35:58 +05304334
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004335 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08004336 QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304337 if (err) {
4338 dev_err(hba->dev,
4339 "%s setting fDeviceInit flag failed with error %d\n",
4340 __func__, err);
4341 goto out;
4342 }
4343
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004344 /* Poll fDeviceInit flag to be cleared */
4345 timeout = ktime_add_ms(ktime_get(), FDEVICEINIT_COMPL_TIMEOUT);
4346 do {
4347 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4348 QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res);
4349 if (!flag_res)
4350 break;
4351 usleep_range(5000, 10000);
4352 } while (ktime_before(ktime_get(), timeout));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004353
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004354 if (err) {
Dolev Raviv68078d52013-07-30 00:35:58 +05304355 dev_err(hba->dev,
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004356 "%s reading fDeviceInit flag failed with error %d\n",
4357 __func__, err);
4358 } else if (flag_res) {
Dolev Raviv68078d52013-07-30 00:35:58 +05304359 dev_err(hba->dev,
Kiwoong Kim29707fa2020-08-10 19:02:27 +09004360 "%s fDeviceInit was not cleared by the device\n",
4361 __func__);
4362 err = -EBUSY;
4363 }
Dolev Raviv68078d52013-07-30 00:35:58 +05304364out:
4365 return err;
4366}
4367
4368/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304369 * ufshcd_make_hba_operational - Make UFS controller operational
4370 * @hba: per adapter instance
4371 *
4372 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004373 * 1. Enable required interrupts
4374 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004375 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004376 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304377 *
4378 * Returns 0 on success, non-zero value on failure
4379 */
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004380int ufshcd_make_hba_operational(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304381{
4382 int err = 0;
4383 u32 reg;
4384
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304385 /* Enable required interrupts */
4386 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4387
4388 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004389 if (ufshcd_is_intr_aggr_allowed(hba))
4390 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4391 else
4392 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304393
4394 /* Configure UTRL and UTMRL base address registers */
4395 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4396 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4397 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4398 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4399 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4400 REG_UTP_TASK_REQ_LIST_BASE_L);
4401 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4402 REG_UTP_TASK_REQ_LIST_BASE_H);
4403
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304404 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004405 * Make sure base address and interrupt setup are updated before
4406 * enabling the run/stop registers below.
4407 */
4408 wmb();
4409
4410 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304411 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304412 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004413 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304414 if (!(ufshcd_get_lists_status(reg))) {
4415 ufshcd_enable_run_stop_reg(hba);
4416 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304417 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304418 "Host controller not ready to process requests");
4419 err = -EIO;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304420 }
4421
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304422 return err;
4423}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004424EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304425
4426/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004427 * ufshcd_hba_stop - Send controller to reset state
4428 * @hba: per adapter instance
Yaniv Gardi596585a2016-03-10 17:37:08 +02004429 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07004430static inline void ufshcd_hba_stop(struct ufs_hba *hba)
Yaniv Gardi596585a2016-03-10 17:37:08 +02004431{
Bart Van Assche5cac1092020-05-07 15:27:50 -07004432 unsigned long flags;
Yaniv Gardi596585a2016-03-10 17:37:08 +02004433 int err;
4434
Bart Van Assche5cac1092020-05-07 15:27:50 -07004435 /*
4436 * Obtain the host lock to prevent that the controller is disabled
4437 * while the UFS interrupt handler is active on another CPU.
4438 */
4439 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004440 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
Bart Van Assche5cac1092020-05-07 15:27:50 -07004441 spin_unlock_irqrestore(hba->host->host_lock, flags);
4442
Yaniv Gardi596585a2016-03-10 17:37:08 +02004443 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4444 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
Bart Van Assche5cac1092020-05-07 15:27:50 -07004445 10, 1);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004446 if (err)
4447 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4448}
4449
4450/**
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304451 * ufshcd_hba_execute_hce - initialize the controller
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304452 * @hba: per adapter instance
4453 *
4454 * The controller resets itself and controller firmware initialization
4455 * sequence kicks off. When controller is ready it will set
4456 * the Host Controller Enable bit to 1.
4457 *
4458 * Returns 0 on success, non-zero value on failure
4459 */
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304460static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304461{
Stanley Chu6081b122020-11-12 13:45:37 +08004462 int retry_outer = 3;
4463 int retry_inner;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304464
Stanley Chu6081b122020-11-12 13:45:37 +08004465start:
Yaniv Gardi596585a2016-03-10 17:37:08 +02004466 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304467 /* change controller state to "reset state" */
Bart Van Assche5cac1092020-05-07 15:27:50 -07004468 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304469
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004470 /* UniPro link is disabled at this point */
4471 ufshcd_set_link_off(hba);
4472
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004473 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004474
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304475 /* start controller initialization sequence */
4476 ufshcd_hba_start(hba);
4477
4478 /*
4479 * To initialize a UFS host controller HCE bit must be set to 1.
4480 * During initialization the HCE bit value changes from 1->0->1.
4481 * When the host controller completes initialization sequence
4482 * it sets the value of HCE bit to 1. The same HCE bit is read back
4483 * to check if the controller has completed initialization sequence.
4484 * So without this delay the value HCE = 1, set in the previous
4485 * instruction might be read back.
4486 * This delay can be changed based on the controller.
4487 */
Stanley Chu90b84912020-05-09 17:37:13 +08004488 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304489
4490 /* wait for the host controller to complete initialization */
Stanley Chu6081b122020-11-12 13:45:37 +08004491 retry_inner = 50;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304492 while (ufshcd_is_hba_active(hba)) {
Stanley Chu6081b122020-11-12 13:45:37 +08004493 if (retry_inner) {
4494 retry_inner--;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304495 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304496 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304497 "Controller enable failed\n");
Stanley Chu6081b122020-11-12 13:45:37 +08004498 if (retry_outer) {
4499 retry_outer--;
4500 goto start;
4501 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304502 return -EIO;
4503 }
Stanley Chu9fc305e2020-03-18 18:40:15 +08004504 usleep_range(1000, 1100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304505 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004506
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004507 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004508 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004509
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004510 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004511
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304512 return 0;
4513}
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304514
4515int ufshcd_hba_enable(struct ufs_hba *hba)
4516{
4517 int ret;
4518
4519 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
4520 ufshcd_set_link_off(hba);
4521 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4522
4523 /* enable UIC related interrupts */
4524 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4525 ret = ufshcd_dme_reset(hba);
4526 if (!ret) {
4527 ret = ufshcd_dme_enable(hba);
4528 if (!ret)
4529 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4530 if (ret)
4531 dev_err(hba->dev,
4532 "Host controller enable failed with non-hce\n");
4533 }
4534 } else {
4535 ret = ufshcd_hba_execute_hce(hba);
4536 }
4537
4538 return ret;
4539}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004540EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
4541
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004542static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4543{
Stanley Chuba0320f2020-03-18 18:40:10 +08004544 int tx_lanes = 0, i, err = 0;
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004545
4546 if (!peer)
4547 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4548 &tx_lanes);
4549 else
4550 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4551 &tx_lanes);
4552 for (i = 0; i < tx_lanes; i++) {
4553 if (!peer)
4554 err = ufshcd_dme_set(hba,
4555 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4556 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4557 0);
4558 else
4559 err = ufshcd_dme_peer_set(hba,
4560 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4561 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4562 0);
4563 if (err) {
4564 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4565 __func__, peer, i, err);
4566 break;
4567 }
4568 }
4569
4570 return err;
4571}
4572
4573static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4574{
4575 return ufshcd_disable_tx_lcc(hba, true);
4576}
4577
Stanley Chue965e5e2020-12-05 19:58:59 +08004578void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val)
Stanley Chu8808b4e2019-07-10 21:38:21 +08004579{
Stanley Chue965e5e2020-12-05 19:58:59 +08004580 struct ufs_event_hist *e;
4581
4582 if (id >= UFS_EVT_CNT)
4583 return;
4584
4585 e = &hba->ufs_stats.event[id];
4586 e->val[e->pos] = val;
4587 e->tstamp[e->pos] = ktime_get();
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02004588 e->cnt += 1;
Stanley Chue965e5e2020-12-05 19:58:59 +08004589 e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH;
Stanley Chu172614a2020-12-05 19:59:00 +08004590
4591 ufshcd_vops_event_notify(hba, id, &val);
Stanley Chu8808b4e2019-07-10 21:38:21 +08004592}
Stanley Chue965e5e2020-12-05 19:58:59 +08004593EXPORT_SYMBOL_GPL(ufshcd_update_evt_hist);
Stanley Chu8808b4e2019-07-10 21:38:21 +08004594
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304595/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304596 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304597 * @hba: per adapter instance
4598 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304599 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304600 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304601static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304602{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304603 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004604 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004605 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304606
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004607 /*
4608 * If UFS device isn't active then we will have to issue link startup
4609 * 2 times to make sure the device state move to active.
4610 */
4611 if (!ufshcd_is_ufs_dev_active(hba))
4612 link_startup_again = true;
4613
4614link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004615 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004616 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304617
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004618 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004619
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004620 /* check if device is detected by inter-connect layer */
4621 if (!ret && !ufshcd_is_device_present(hba)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08004622 ufshcd_update_evt_hist(hba,
4623 UFS_EVT_LINK_STARTUP_FAIL,
Stanley Chu8808b4e2019-07-10 21:38:21 +08004624 0);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004625 dev_err(hba->dev, "%s: Device not present\n", __func__);
4626 ret = -ENXIO;
4627 goto out;
4628 }
4629
4630 /*
4631 * DME link lost indication is only received when link is up,
4632 * but we can't be sure if the link is up until link startup
4633 * succeeds. So reset the local Uni-Pro and try again.
4634 */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004635 if (ret && ufshcd_hba_enable(hba)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08004636 ufshcd_update_evt_hist(hba,
4637 UFS_EVT_LINK_STARTUP_FAIL,
Stanley Chu8808b4e2019-07-10 21:38:21 +08004638 (u32)ret);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004639 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004640 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004641 } while (ret && retries--);
4642
Stanley Chu8808b4e2019-07-10 21:38:21 +08004643 if (ret) {
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004644 /* failed to get the link up... retire */
Stanley Chue965e5e2020-12-05 19:58:59 +08004645 ufshcd_update_evt_hist(hba,
4646 UFS_EVT_LINK_STARTUP_FAIL,
Stanley Chu8808b4e2019-07-10 21:38:21 +08004647 (u32)ret);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304648 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004649 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304650
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004651 if (link_startup_again) {
4652 link_startup_again = false;
4653 retries = DME_LINKSTARTUP_RETRIES;
4654 goto link_startup;
4655 }
4656
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004657 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4658 ufshcd_init_pwr_info(hba);
4659 ufshcd_print_pwr_info(hba);
4660
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004661 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4662 ret = ufshcd_disable_device_tx_lcc(hba);
4663 if (ret)
4664 goto out;
4665 }
4666
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004667 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004668 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4669 if (ret)
4670 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004671
Can Guo2355b662020-08-24 19:07:06 -07004672 /* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */
4673 ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004674 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304675out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004676 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304677 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004678 ufshcd_print_host_state(hba);
4679 ufshcd_print_pwr_info(hba);
Stanley Chue965e5e2020-12-05 19:58:59 +08004680 ufshcd_print_evt_hist(hba);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004681 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304682 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304683}
4684
4685/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304686 * ufshcd_verify_dev_init() - Verify device initialization
4687 * @hba: per-adapter instance
4688 *
4689 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4690 * device Transport Protocol (UTP) layer is ready after a reset.
4691 * If the UTP layer at the device side is not initialized, it may
4692 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4693 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4694 */
4695static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4696{
4697 int err = 0;
4698 int retries;
4699
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004700 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304701 mutex_lock(&hba->dev_cmd.lock);
4702 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4703 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4704 NOP_OUT_TIMEOUT);
4705
4706 if (!err || err == -ETIMEDOUT)
4707 break;
4708
4709 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4710 }
4711 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004712 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304713
4714 if (err)
4715 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4716 return err;
4717}
4718
4719/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004720 * ufshcd_set_queue_depth - set lun queue depth
4721 * @sdev: pointer to SCSI device
4722 *
4723 * Read bLUQueueDepth value and activate scsi tagged command
4724 * queueing. For WLUN, queue depth is set to 1. For best-effort
4725 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4726 * value that host can queue.
4727 */
4728static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4729{
4730 int ret = 0;
4731 u8 lun_qdepth;
4732 struct ufs_hba *hba;
4733
4734 hba = shost_priv(sdev->host);
4735
4736 lun_qdepth = hba->nutrs;
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02004737 ret = ufshcd_read_unit_desc_param(hba,
4738 ufshcd_scsi_to_upiu_lun(sdev->lun),
4739 UNIT_DESC_PARAM_LU_Q_DEPTH,
4740 &lun_qdepth,
4741 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004742
4743 /* Some WLUN doesn't support unit descriptor */
4744 if (ret == -EOPNOTSUPP)
4745 lun_qdepth = 1;
4746 else if (!lun_qdepth)
4747 /* eventually, we can figure out the real queue depth */
4748 lun_qdepth = hba->nutrs;
4749 else
4750 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4751
4752 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4753 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004754 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004755}
4756
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004757/*
4758 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4759 * @hba: per-adapter instance
4760 * @lun: UFS device lun id
4761 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4762 *
4763 * Returns 0 in case of success and b_lu_write_protect status would be returned
4764 * @b_lu_write_protect parameter.
4765 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4766 * Returns -EINVAL in case of invalid parameters passed to this function.
4767 */
4768static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4769 u8 lun,
4770 u8 *b_lu_write_protect)
4771{
4772 int ret;
4773
4774 if (!b_lu_write_protect)
4775 ret = -EINVAL;
4776 /*
4777 * According to UFS device spec, RPMB LU can't be write
4778 * protected so skip reading bLUWriteProtect parameter for
4779 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4780 */
Bean Huo1baa8012020-01-20 14:08:20 +01004781 else if (lun >= hba->dev_info.max_lu_supported)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004782 ret = -ENOTSUPP;
4783 else
4784 ret = ufshcd_read_unit_desc_param(hba,
4785 lun,
4786 UNIT_DESC_PARAM_LU_WR_PROTECT,
4787 b_lu_write_protect,
4788 sizeof(*b_lu_write_protect));
4789 return ret;
4790}
4791
4792/**
4793 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4794 * status
4795 * @hba: per-adapter instance
4796 * @sdev: pointer to SCSI device
4797 *
4798 */
4799static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4800 struct scsi_device *sdev)
4801{
4802 if (hba->dev_info.f_power_on_wp_en &&
4803 !hba->dev_info.is_lu_power_on_wp) {
4804 u8 b_lu_write_protect;
4805
4806 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4807 &b_lu_write_protect) &&
4808 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4809 hba->dev_info.is_lu_power_on_wp = true;
4810 }
4811}
4812
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004813/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304814 * ufshcd_slave_alloc - handle initial SCSI device configurations
4815 * @sdev: pointer to SCSI device
4816 *
4817 * Returns success
4818 */
4819static int ufshcd_slave_alloc(struct scsi_device *sdev)
4820{
4821 struct ufs_hba *hba;
4822
4823 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304824
4825 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4826 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304827
Can Guoa3a76392019-12-05 02:14:30 +00004828 /* DBD field should be set to 1 in mode sense(10) */
4829 sdev->set_dbd_for_ms = 1;
4830
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304831 /* allow SCSI layer to restart the device in case of errors */
4832 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004833
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004834 /* REPORT SUPPORTED OPERATION CODES is not supported */
4835 sdev->no_report_opcodes = 1;
4836
Sujit Reddy Thumma84af7e82018-01-24 09:52:35 +05304837 /* WRITE_SAME command is not supported */
4838 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004839
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004840 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004841
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004842 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4843
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004844 return 0;
4845}
4846
4847/**
4848 * ufshcd_change_queue_depth - change queue depth
4849 * @sdev: pointer to SCSI device
4850 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004851 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004852 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004853 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004854static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004855{
4856 struct ufs_hba *hba = shost_priv(sdev->host);
4857
4858 if (depth > hba->nutrs)
4859 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004860 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304861}
4862
4863/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004864 * ufshcd_slave_configure - adjust SCSI device configurations
4865 * @sdev: pointer to SCSI device
4866 */
4867static int ufshcd_slave_configure(struct scsi_device *sdev)
4868{
Stanley Chu49615ba2019-09-16 23:56:50 +08004869 struct ufs_hba *hba = shost_priv(sdev->host);
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004870 struct request_queue *q = sdev->request_queue;
4871
4872 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
Kiwoong Kim2b2bfc8a2021-01-19 12:33:41 +09004873 if (hba->quirks & UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE)
4874 blk_queue_update_dma_alignment(q, PAGE_SIZE - 1);
Stanley Chu49615ba2019-09-16 23:56:50 +08004875
4876 if (ufshcd_is_rpm_autosuspend_allowed(hba))
4877 sdev->rpm_autosuspend = 1;
4878
Satya Tangiraladf043c742020-07-06 20:04:14 +00004879 ufshcd_crypto_setup_rq_keyslot_manager(hba, q);
4880
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004881 return 0;
4882}
4883
4884/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304885 * ufshcd_slave_destroy - remove SCSI device configurations
4886 * @sdev: pointer to SCSI device
4887 */
4888static void ufshcd_slave_destroy(struct scsi_device *sdev)
4889{
4890 struct ufs_hba *hba;
4891
4892 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004893 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004894 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4895 unsigned long flags;
4896
4897 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004898 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004899 spin_unlock_irqrestore(hba->host->host_lock, flags);
4900 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304901}
4902
4903/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304904 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004905 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304906 * @scsi_status: SCSI command status
4907 *
4908 * Returns value base on SCSI command status
4909 */
4910static inline int
4911ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4912{
4913 int result = 0;
4914
4915 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304916 case SAM_STAT_CHECK_CONDITION:
4917 ufshcd_copy_sense_data(lrbp);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004918 fallthrough;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304919 case SAM_STAT_GOOD:
4920 result |= DID_OK << 16 |
4921 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304922 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304923 break;
4924 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304925 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304926 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304927 ufshcd_copy_sense_data(lrbp);
4928 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304929 break;
4930 default:
4931 result |= DID_ERROR << 16;
4932 break;
4933 } /* end of switch */
4934
4935 return result;
4936}
4937
4938/**
4939 * ufshcd_transfer_rsp_status - Get overall status of the response
4940 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004941 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304942 *
4943 * Returns result of the command to notify SCSI midlayer
4944 */
4945static inline int
4946ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4947{
4948 int result = 0;
4949 int scsi_status;
4950 int ocs;
4951
4952 /* overall command status of utrd */
4953 ocs = ufshcd_get_tr_ocs(lrbp);
4954
Kiwoong Kimd779a6e2020-05-28 06:46:53 +05304955 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) {
4956 if (be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_1) &
4957 MASK_RSP_UPIU_RESULT)
4958 ocs = OCS_SUCCESS;
4959 }
4960
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304961 switch (ocs) {
4962 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304963 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004964 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304965 switch (result) {
4966 case UPIU_TRANSACTION_RESPONSE:
4967 /*
4968 * get the response UPIU result to extract
4969 * the SCSI command status
4970 */
4971 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4972
4973 /*
4974 * get the result based on SCSI status response
4975 * to notify the SCSI midlayer of the command status
4976 */
4977 scsi_status = result & MASK_SCSI_STATUS;
4978 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304979
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004980 /*
4981 * Currently we are only supporting BKOPs exception
4982 * events hence we can ignore BKOPs exception event
4983 * during power management callbacks. BKOPs exception
4984 * event is not expected to be raised in runtime suspend
4985 * callback as it allows the urgent bkops.
4986 * During system suspend, we are anyway forcefully
4987 * disabling the bkops and if urgent bkops is needed
4988 * it will be enabled on system resume. Long term
4989 * solution could be to abort the system suspend if
4990 * UFS device needs urgent BKOPs.
4991 */
4992 if (!hba->pm_op_in_progress &&
Sayali Lokhande2824ec92020-02-10 19:40:44 -08004993 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr) &&
4994 schedule_work(&hba->eeh_work)) {
4995 /*
4996 * Prevent suspend once eeh_work is scheduled
4997 * to avoid deadlock between ufshcd_suspend
4998 * and exception event handler.
4999 */
5000 pm_runtime_get_noresume(hba->dev);
5001 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305002 break;
5003 case UPIU_TRANSACTION_REJECT_UPIU:
5004 /* TODO: handle Reject UPIU Response */
5005 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305006 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305007 "Reject UPIU not fully implemented\n");
5008 break;
5009 default:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305010 dev_err(hba->dev,
5011 "Unexpected request response code = %x\n",
5012 result);
Stanley Chue0347d82019-04-15 20:23:38 +08005013 result = DID_ERROR << 16;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305014 break;
5015 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305016 break;
5017 case OCS_ABORTED:
5018 result |= DID_ABORT << 16;
5019 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305020 case OCS_INVALID_COMMAND_STATUS:
5021 result |= DID_REQUEUE << 16;
5022 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305023 case OCS_INVALID_CMD_TABLE_ATTR:
5024 case OCS_INVALID_PRDT_ATTR:
5025 case OCS_MISMATCH_DATA_BUF_SIZE:
5026 case OCS_MISMATCH_RESP_UPIU_SIZE:
5027 case OCS_PEER_COMM_FAILURE:
5028 case OCS_FATAL_ERROR:
Satya Tangirala5e7341e2020-07-06 20:04:12 +00005029 case OCS_DEVICE_FATAL_ERROR:
5030 case OCS_INVALID_CRYPTO_CONFIG:
5031 case OCS_GENERAL_CRYPTO_ERROR:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305032 default:
5033 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305034 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005035 "OCS error from controller = %x for tag %d\n",
5036 ocs, lrbp->task_tag);
Stanley Chue965e5e2020-12-05 19:58:59 +08005037 ufshcd_print_evt_hist(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08005038 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305039 break;
5040 } /* end of switch */
5041
Can Guo2df74b62019-11-25 22:53:33 -08005042 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs)
Dolev Raviv66cc8202016-12-22 18:39:42 -08005043 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305044 return result;
5045}
5046
5047/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305048 * ufshcd_uic_cmd_compl - handle completion of uic command
5049 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305050 * @intr_status: interrupt status generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005051 *
5052 * Returns
5053 * IRQ_HANDLED - If interrupt is valid
5054 * IRQ_NONE - If invalid interrupt
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305055 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005056static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305057{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005058 irqreturn_t retval = IRQ_NONE;
5059
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305060 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305061 hba->active_uic_cmd->argument2 |=
5062 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05305063 hba->active_uic_cmd->argument3 =
5064 ufshcd_get_dme_attr_val(hba);
Can Guo0f52fcb92020-11-02 22:24:40 -08005065 if (!hba->uic_async_done)
5066 hba->active_uic_cmd->cmd_active = 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305067 complete(&hba->active_uic_cmd->done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005068 retval = IRQ_HANDLED;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305069 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305070
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005071 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
Can Guo0f52fcb92020-11-02 22:24:40 -08005072 hba->active_uic_cmd->cmd_active = 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005073 complete(hba->uic_async_done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005074 retval = IRQ_HANDLED;
5075 }
Stanley Chuaa5c6972020-06-15 15:22:35 +08005076
5077 if (retval == IRQ_HANDLED)
5078 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd,
Bean Huo28fa68f2021-01-05 12:34:42 +01005079 UFS_CMD_COMP);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005080 return retval;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305081}
5082
5083/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005084 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305085 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005086 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305087 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005088static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
5089 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305090{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305091 struct ufshcd_lrb *lrbp;
5092 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305093 int result;
5094 int index;
Can Guo7a7e66c2020-12-02 04:04:02 -08005095 bool update_scaling = false;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005096
Dolev Ravive9d501b2014-07-01 12:22:37 +03005097 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
5098 lrbp = &hba->lrb[index];
Can Guo7a7e66c2020-12-02 04:04:02 -08005099 lrbp->in_use = false;
Stanley Chua3170372020-07-06 14:07:06 +08005100 lrbp->compl_time_stamp = ktime_get();
Dolev Ravive9d501b2014-07-01 12:22:37 +03005101 cmd = lrbp->cmd;
5102 if (cmd) {
Bean Huo28fa68f2021-01-05 12:34:42 +01005103 ufshcd_add_command_trace(hba, index, UFS_CMD_COMP);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005104 result = ufshcd_transfer_rsp_status(hba, lrbp);
5105 scsi_dma_unmap(cmd);
5106 cmd->result = result;
5107 /* Mark completed command as NULL in LRB */
5108 lrbp->cmd = NULL;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005109 /* Do not touch lrbp after scsi done */
5110 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005111 __ufshcd_release(hba);
Can Guo7a7e66c2020-12-02 04:04:02 -08005112 update_scaling = true;
Joao Pinto300bb132016-05-11 12:21:27 +01005113 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
5114 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08005115 if (hba->dev_cmd.complete) {
5116 ufshcd_add_command_trace(hba, index,
Bean Huo28fa68f2021-01-05 12:34:42 +01005117 UFS_DEV_COMP);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005118 complete(hba->dev_cmd.complete);
Can Guo7a7e66c2020-12-02 04:04:02 -08005119 update_scaling = true;
Lee Susman1a07f2d2016-12-22 18:42:03 -08005120 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03005121 }
Can Guo7a7e66c2020-12-02 04:04:02 -08005122 if (ufshcd_is_clkscaling_supported(hba) && update_scaling)
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08005123 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005124 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305125
5126 /* clear corresponding bits of completed commands */
5127 hba->outstanding_reqs ^= completed_reqs;
5128
Sahitya Tummala856b3482014-09-25 15:32:34 +03005129 ufshcd_clk_scaling_update_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305130}
5131
5132/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005133 * ufshcd_transfer_req_compl - handle SCSI and query command completion
5134 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005135 *
5136 * Returns
5137 * IRQ_HANDLED - If interrupt is valid
5138 * IRQ_NONE - If invalid interrupt
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005139 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005140static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005141{
5142 unsigned long completed_reqs;
5143 u32 tr_doorbell;
5144
5145 /* Resetting interrupt aggregation counters first and reading the
5146 * DOOR_BELL afterward allows us to handle all the completed requests.
5147 * In order to prevent other interrupts starvation the DB is read once
5148 * after reset. The down side of this solution is the possibility of
5149 * false interrupt if device completes another request after resetting
5150 * aggregation and before reading the DB.
5151 */
Alim Akhtarb638b5e2020-05-28 06:46:50 +05305152 if (ufshcd_is_intr_aggr_allowed(hba) &&
5153 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005154 ufshcd_reset_intr_aggr(hba);
5155
5156 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5157 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
5158
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005159 if (completed_reqs) {
5160 __ufshcd_transfer_req_compl(hba, completed_reqs);
5161 return IRQ_HANDLED;
5162 } else {
5163 return IRQ_NONE;
5164 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005165}
5166
5167/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305168 * ufshcd_disable_ee - disable exception event
5169 * @hba: per-adapter instance
5170 * @mask: exception event to disable
5171 *
5172 * Disables exception event in the device so that the EVENT_ALERT
5173 * bit is not set.
5174 *
5175 * Returns zero on success, non-zero error value on failure.
5176 */
5177static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
5178{
5179 int err = 0;
5180 u32 val;
5181
5182 if (!(hba->ee_ctrl_mask & mask))
5183 goto out;
5184
5185 val = hba->ee_ctrl_mask & ~mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03005186 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005187 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305188 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5189 if (!err)
5190 hba->ee_ctrl_mask &= ~mask;
5191out:
5192 return err;
5193}
5194
5195/**
5196 * ufshcd_enable_ee - enable exception event
5197 * @hba: per-adapter instance
5198 * @mask: exception event to enable
5199 *
5200 * Enable corresponding exception event in the device to allow
5201 * device to alert host in critical scenarios.
5202 *
5203 * Returns zero on success, non-zero error value on failure.
5204 */
5205static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
5206{
5207 int err = 0;
5208 u32 val;
5209
5210 if (hba->ee_ctrl_mask & mask)
5211 goto out;
5212
5213 val = hba->ee_ctrl_mask | mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03005214 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005215 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305216 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5217 if (!err)
5218 hba->ee_ctrl_mask |= mask;
5219out:
5220 return err;
5221}
5222
5223/**
5224 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5225 * @hba: per-adapter instance
5226 *
5227 * Allow device to manage background operations on its own. Enabling
5228 * this might lead to inconsistent latencies during normal data transfers
5229 * as the device is allowed to manage its own way of handling background
5230 * operations.
5231 *
5232 * Returns zero on success, non-zero on failure.
5233 */
5234static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5235{
5236 int err = 0;
5237
5238 if (hba->auto_bkops_enabled)
5239 goto out;
5240
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005241 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005242 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305243 if (err) {
5244 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5245 __func__, err);
5246 goto out;
5247 }
5248
5249 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005250 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305251
5252 /* No need of URGENT_BKOPS exception from the device */
5253 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5254 if (err)
5255 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5256 __func__, err);
5257out:
5258 return err;
5259}
5260
5261/**
5262 * ufshcd_disable_auto_bkops - block device in doing background operations
5263 * @hba: per-adapter instance
5264 *
5265 * Disabling background operations improves command response latency but
5266 * has drawback of device moving into critical state where the device is
5267 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5268 * host is idle so that BKOPS are managed effectively without any negative
5269 * impacts.
5270 *
5271 * Returns zero on success, non-zero on failure.
5272 */
5273static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5274{
5275 int err = 0;
5276
5277 if (!hba->auto_bkops_enabled)
5278 goto out;
5279
5280 /*
5281 * If host assisted BKOPs is to be enabled, make sure
5282 * urgent bkops exception is allowed.
5283 */
5284 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5285 if (err) {
5286 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5287 __func__, err);
5288 goto out;
5289 }
5290
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005291 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005292 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305293 if (err) {
5294 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5295 __func__, err);
5296 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5297 goto out;
5298 }
5299
5300 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005301 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Asutosh Das24366c2a2019-11-25 22:53:30 -08005302 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305303out:
5304 return err;
5305}
5306
5307/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005308 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305309 * @hba: per adapter instance
5310 *
5311 * After a device reset the device may toggle the BKOPS_EN flag
5312 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005313 * as well. This function would change the auto-bkops state based on
5314 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305315 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005316static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305317{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005318 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5319 hba->auto_bkops_enabled = false;
5320 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5321 ufshcd_enable_auto_bkops(hba);
5322 } else {
5323 hba->auto_bkops_enabled = true;
5324 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5325 ufshcd_disable_auto_bkops(hba);
5326 }
Stanley Chu7b6668d2020-05-30 22:12:00 +08005327 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
Asutosh Das24366c2a2019-11-25 22:53:30 -08005328 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305329}
5330
5331static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5332{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005333 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305334 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5335}
5336
5337/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005338 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5339 * @hba: per-adapter instance
5340 * @status: bkops_status value
5341 *
5342 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5343 * flag in the device to permit background operations if the device
5344 * bkops_status is greater than or equal to "status" argument passed to
5345 * this function, disable otherwise.
5346 *
5347 * Returns 0 for success, non-zero in case of failure.
5348 *
5349 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5350 * to know whether auto bkops is enabled or disabled after this function
5351 * returns control to it.
5352 */
5353static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5354 enum bkops_status status)
5355{
5356 int err;
5357 u32 curr_status = 0;
5358
5359 err = ufshcd_get_bkops_status(hba, &curr_status);
5360 if (err) {
5361 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5362 __func__, err);
5363 goto out;
5364 } else if (curr_status > BKOPS_STATUS_MAX) {
5365 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5366 __func__, curr_status);
5367 err = -EINVAL;
5368 goto out;
5369 }
5370
5371 if (curr_status >= status)
5372 err = ufshcd_enable_auto_bkops(hba);
5373 else
5374 err = ufshcd_disable_auto_bkops(hba);
5375out:
5376 return err;
5377}
5378
5379/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305380 * ufshcd_urgent_bkops - handle urgent bkops exception event
5381 * @hba: per-adapter instance
5382 *
5383 * Enable fBackgroundOpsEn flag in the device to permit background
5384 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005385 *
5386 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5387 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305388 */
5389static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5390{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005391 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305392}
5393
5394static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5395{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005396 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305397 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5398}
5399
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005400static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5401{
5402 int err;
5403 u32 curr_status = 0;
5404
5405 if (hba->is_urgent_bkops_lvl_checked)
5406 goto enable_auto_bkops;
5407
5408 err = ufshcd_get_bkops_status(hba, &curr_status);
5409 if (err) {
5410 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5411 __func__, err);
5412 goto out;
5413 }
5414
5415 /*
5416 * We are seeing that some devices are raising the urgent bkops
5417 * exception events even when BKOPS status doesn't indicate performace
5418 * impacted or critical. Handle these device by determining their urgent
5419 * bkops status at runtime.
5420 */
5421 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5422 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5423 __func__, curr_status);
5424 /* update the current status as the urgent bkops level */
5425 hba->urgent_bkops_lvl = curr_status;
5426 hba->is_urgent_bkops_lvl_checked = true;
5427 }
5428
5429enable_auto_bkops:
5430 err = ufshcd_enable_auto_bkops(hba);
5431out:
5432 if (err < 0)
5433 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5434 __func__, err);
5435}
5436
Bean Huo8e834ca2021-01-19 17:38:42 +01005437int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005438{
5439 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005440 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005441 enum query_opcode opcode;
5442
Stanley Chu79e35202020-05-08 16:01:15 +08005443 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005444 return 0;
5445
Bean Huo4cd48992021-01-19 17:38:46 +01005446 if (!(enable ^ hba->dev_info.wb_enabled))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005447 return 0;
5448 if (enable)
5449 opcode = UPIU_QUERY_OPCODE_SET_FLAG;
5450 else
5451 opcode = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5452
Stanley Chue31011a2020-05-22 16:32:11 +08005453 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005454 ret = ufshcd_query_flag_retry(hba, opcode,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005455 QUERY_FLAG_IDN_WB_EN, index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005456 if (ret) {
5457 dev_err(hba->dev, "%s write booster %s failed %d\n",
5458 __func__, enable ? "enable" : "disable", ret);
5459 return ret;
5460 }
5461
Bean Huo4cd48992021-01-19 17:38:46 +01005462 hba->dev_info.wb_enabled = enable;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005463 dev_dbg(hba->dev, "%s write booster %s %d\n",
5464 __func__, enable ? "enable" : "disable", ret);
5465
5466 return ret;
5467}
5468
5469static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set)
5470{
5471 int val;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005472 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005473
5474 if (set)
5475 val = UPIU_QUERY_OPCODE_SET_FLAG;
5476 else
5477 val = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5478
Stanley Chue31011a2020-05-22 16:32:11 +08005479 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005480 return ufshcd_query_flag_retry(hba, val,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005481 QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8,
5482 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005483}
5484
5485static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable)
5486{
5487 if (enable)
5488 ufshcd_wb_buf_flush_enable(hba);
5489 else
5490 ufshcd_wb_buf_flush_disable(hba);
5491
5492}
5493
5494static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba)
5495{
5496 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005497 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005498
Bean Huo4cd48992021-01-19 17:38:46 +01005499 if (!ufshcd_is_wb_allowed(hba) || hba->dev_info.wb_buf_flush_enabled)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005500 return 0;
5501
Stanley Chue31011a2020-05-22 16:32:11 +08005502 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005503 ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005504 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005505 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005506 if (ret)
5507 dev_err(hba->dev, "%s WB - buf flush enable failed %d\n",
5508 __func__, ret);
5509 else
Bean Huo4cd48992021-01-19 17:38:46 +01005510 hba->dev_info.wb_buf_flush_enabled = true;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005511
5512 dev_dbg(hba->dev, "WB - Flush enabled: %d\n", ret);
5513 return ret;
5514}
5515
5516static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba)
5517{
5518 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005519 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005520
Bean Huo4cd48992021-01-19 17:38:46 +01005521 if (!ufshcd_is_wb_allowed(hba) || !hba->dev_info.wb_buf_flush_enabled)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005522 return 0;
5523
Stanley Chue31011a2020-05-22 16:32:11 +08005524 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005525 ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005526 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN,
5527 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005528 if (ret) {
5529 dev_warn(hba->dev, "%s: WB - buf flush disable failed %d\n",
5530 __func__, ret);
5531 } else {
Bean Huo4cd48992021-01-19 17:38:46 +01005532 hba->dev_info.wb_buf_flush_enabled = false;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005533 dev_dbg(hba->dev, "WB - Flush disabled: %d\n", ret);
5534 }
5535
5536 return ret;
5537}
5538
5539static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba,
5540 u32 avail_buf)
5541{
5542 u32 cur_buf;
5543 int ret;
Stanley Chue31011a2020-05-22 16:32:11 +08005544 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005545
Stanley Chue31011a2020-05-22 16:32:11 +08005546 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005547 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5548 QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE,
Stanley Chue31011a2020-05-22 16:32:11 +08005549 index, 0, &cur_buf);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005550 if (ret) {
5551 dev_err(hba->dev, "%s dCurWriteBoosterBufferSize read failed %d\n",
5552 __func__, ret);
5553 return false;
5554 }
5555
5556 if (!cur_buf) {
5557 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n",
5558 cur_buf);
5559 return false;
5560 }
Stanley Chud14734ae2020-05-09 17:37:15 +08005561 /* Let it continue to flush when available buffer exceeds threshold */
5562 if (avail_buf < hba->vps->wb_flush_threshold)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005563 return true;
5564
5565 return false;
5566}
5567
Stanley Chu51dd9052020-05-22 16:32:12 +08005568static bool ufshcd_wb_need_flush(struct ufs_hba *hba)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005569{
5570 int ret;
5571 u32 avail_buf;
Stanley Chue31011a2020-05-22 16:32:11 +08005572 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005573
Stanley Chu79e35202020-05-08 16:01:15 +08005574 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005575 return false;
5576 /*
5577 * The ufs device needs the vcc to be ON to flush.
5578 * With user-space reduction enabled, it's enough to enable flush
5579 * by checking only the available buffer. The threshold
5580 * defined here is > 90% full.
5581 * With user-space preserved enabled, the current-buffer
5582 * should be checked too because the wb buffer size can reduce
5583 * when disk tends to be full. This info is provided by current
5584 * buffer (dCurrentWriteBoosterBufferSize). There's no point in
5585 * keeping vcc on when current buffer is empty.
5586 */
Stanley Chue31011a2020-05-22 16:32:11 +08005587 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005588 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5589 QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE,
Stanley Chue31011a2020-05-22 16:32:11 +08005590 index, 0, &avail_buf);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005591 if (ret) {
5592 dev_warn(hba->dev, "%s dAvailableWriteBoosterBufferSize read failed %d\n",
5593 __func__, ret);
5594 return false;
5595 }
5596
5597 if (!hba->dev_info.b_presrv_uspc_en) {
Stanley Chud14734ae2020-05-09 17:37:15 +08005598 if (avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005599 return true;
5600 return false;
5601 }
5602
5603 return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf);
5604}
5605
Stanley Chu51dd9052020-05-22 16:32:12 +08005606static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
5607{
5608 struct ufs_hba *hba = container_of(to_delayed_work(work),
5609 struct ufs_hba,
5610 rpm_dev_flush_recheck_work);
5611 /*
5612 * To prevent unnecessary VCC power drain after device finishes
5613 * WriteBooster buffer flush or Auto BKOPs, force runtime resume
5614 * after a certain delay to recheck the threshold by next runtime
5615 * suspend.
5616 */
5617 pm_runtime_get_sync(hba->dev);
5618 pm_runtime_put_sync(hba->dev);
5619}
5620
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305621/**
5622 * ufshcd_exception_event_handler - handle exceptions raised by device
5623 * @work: pointer to work data
5624 *
5625 * Read bExceptionEventStatus attribute from the device and handle the
5626 * exception event accordingly.
5627 */
5628static void ufshcd_exception_event_handler(struct work_struct *work)
5629{
5630 struct ufs_hba *hba;
5631 int err;
5632 u32 status = 0;
5633 hba = container_of(work, struct ufs_hba, eeh_work);
5634
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305635 pm_runtime_get_sync(hba->dev);
Stanley Chu03e1d282019-12-24 21:01:05 +08005636 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305637 err = ufshcd_get_ee_status(hba, &status);
5638 if (err) {
5639 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5640 __func__, err);
5641 goto out;
5642 }
5643
5644 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005645
5646 if (status & MASK_EE_URGENT_BKOPS)
5647 ufshcd_bkops_exception_event_handler(hba);
5648
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305649out:
Stanley Chu03e1d282019-12-24 21:01:05 +08005650 ufshcd_scsi_unblock_requests(hba);
Sayali Lokhande2824ec92020-02-10 19:40:44 -08005651 /*
5652 * pm_runtime_get_noresume is called while scheduling
5653 * eeh_work to avoid suspend racing with exception work.
5654 * Hence decrement usage counter using pm_runtime_put_noidle
5655 * to allow suspend on completion of exception event handler.
5656 */
5657 pm_runtime_put_noidle(hba->dev);
5658 pm_runtime_put(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305659 return;
5660}
5661
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005662/* Complete requests that have door-bell cleared */
5663static void ufshcd_complete_requests(struct ufs_hba *hba)
5664{
5665 ufshcd_transfer_req_compl(hba);
5666 ufshcd_tmc_handler(hba);
5667}
5668
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305669/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005670 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5671 * to recover from the DL NAC errors or not.
5672 * @hba: per-adapter instance
5673 *
5674 * Returns true if error handling is required, false otherwise
5675 */
5676static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5677{
5678 unsigned long flags;
5679 bool err_handling = true;
5680
5681 spin_lock_irqsave(hba->host->host_lock, flags);
5682 /*
5683 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5684 * device fatal error and/or DL NAC & REPLAY timeout errors.
5685 */
5686 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5687 goto out;
5688
5689 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5690 ((hba->saved_err & UIC_ERROR) &&
5691 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
5692 goto out;
5693
5694 if ((hba->saved_err & UIC_ERROR) &&
5695 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5696 int err;
5697 /*
5698 * wait for 50ms to see if we can get any other errors or not.
5699 */
5700 spin_unlock_irqrestore(hba->host->host_lock, flags);
5701 msleep(50);
5702 spin_lock_irqsave(hba->host->host_lock, flags);
5703
5704 /*
5705 * now check if we have got any other severe errors other than
5706 * DL NAC error?
5707 */
5708 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5709 ((hba->saved_err & UIC_ERROR) &&
5710 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5711 goto out;
5712
5713 /*
5714 * As DL NAC is the only error received so far, send out NOP
5715 * command to confirm if link is still active or not.
5716 * - If we don't get any response then do error recovery.
5717 * - If we get response then clear the DL NAC error bit.
5718 */
5719
5720 spin_unlock_irqrestore(hba->host->host_lock, flags);
5721 err = ufshcd_verify_dev_init(hba);
5722 spin_lock_irqsave(hba->host->host_lock, flags);
5723
5724 if (err)
5725 goto out;
5726
5727 /* Link seems to be alive hence ignore the DL NAC errors */
5728 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5729 hba->saved_err &= ~UIC_ERROR;
5730 /* clear NAC error */
5731 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
Bean Huob0008622020-08-14 11:50:34 +02005732 if (!hba->saved_uic_err)
Yaniv Gardi583fa622016-03-10 17:37:13 +02005733 err_handling = false;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005734 }
5735out:
5736 spin_unlock_irqrestore(hba->host->host_lock, flags);
5737 return err_handling;
5738}
5739
Can Guo4db7a232020-08-09 05:15:51 -07005740/* host lock must be held before calling this func */
5741static inline bool ufshcd_is_saved_err_fatal(struct ufs_hba *hba)
5742{
5743 return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) ||
5744 (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK));
5745}
5746
5747/* host lock must be held before calling this func */
5748static inline void ufshcd_schedule_eh_work(struct ufs_hba *hba)
5749{
5750 /* handle fatal errors only when link is not in error state */
5751 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) {
Can Guo5586dd82020-08-09 05:15:54 -07005752 if (hba->force_reset || ufshcd_is_link_broken(hba) ||
5753 ufshcd_is_saved_err_fatal(hba))
5754 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL;
5755 else
5756 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL;
5757 queue_work(hba->eh_wq, &hba->eh_work);
Can Guo4db7a232020-08-09 05:15:51 -07005758 }
5759}
5760
Can Guoc72e79c2020-08-09 05:15:52 -07005761static void ufshcd_err_handling_prepare(struct ufs_hba *hba)
5762{
5763 pm_runtime_get_sync(hba->dev);
Can Guo88a92d62020-12-02 04:04:01 -08005764 if (pm_runtime_status_suspended(hba->dev) || hba->is_sys_suspended) {
5765 enum ufs_pm_op pm_op;
5766
Can Guoc72e79c2020-08-09 05:15:52 -07005767 /*
5768 * Don't assume anything of pm_runtime_get_sync(), if
5769 * resume fails, irq and clocks can be OFF, and powers
5770 * can be OFF or in LPM.
5771 */
5772 ufshcd_setup_hba_vreg(hba, true);
5773 ufshcd_enable_irq(hba);
5774 ufshcd_setup_vreg(hba, true);
5775 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5776 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5777 ufshcd_hold(hba, false);
5778 if (!ufshcd_is_clkgating_allowed(hba))
5779 ufshcd_setup_clocks(hba, true);
5780 ufshcd_release(hba);
Can Guo88a92d62020-12-02 04:04:01 -08005781 pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM;
5782 ufshcd_vops_resume(hba, pm_op);
Can Guoc72e79c2020-08-09 05:15:52 -07005783 } else {
5784 ufshcd_hold(hba, false);
Stanley Chuf9a7fa32021-01-20 23:01:40 +08005785 if (hba->clk_scaling.is_enabled)
Can Guoc72e79c2020-08-09 05:15:52 -07005786 ufshcd_suspend_clkscaling(hba);
Can Guo0e9d4ca2021-01-20 02:04:21 -08005787 down_write(&hba->clk_scaling_lock);
5788 hba->clk_scaling.is_allowed = false;
5789 up_write(&hba->clk_scaling_lock);
Can Guoc72e79c2020-08-09 05:15:52 -07005790 }
5791}
5792
5793static void ufshcd_err_handling_unprepare(struct ufs_hba *hba)
5794{
5795 ufshcd_release(hba);
Can Guo0e9d4ca2021-01-20 02:04:21 -08005796 down_write(&hba->clk_scaling_lock);
5797 hba->clk_scaling.is_allowed = true;
5798 up_write(&hba->clk_scaling_lock);
5799 if (hba->clk_scaling.is_enabled)
Can Guoc72e79c2020-08-09 05:15:52 -07005800 ufshcd_resume_clkscaling(hba);
5801 pm_runtime_put(hba->dev);
5802}
5803
5804static inline bool ufshcd_err_handling_should_stop(struct ufs_hba *hba)
5805{
Can Guo9cd20d32021-01-13 19:13:28 -08005806 return (!hba->is_powered || hba->shutting_down ||
5807 hba->ufshcd_state == UFSHCD_STATE_ERROR ||
Can Guoc72e79c2020-08-09 05:15:52 -07005808 (!(hba->saved_err || hba->saved_uic_err || hba->force_reset ||
Can Guo9cd20d32021-01-13 19:13:28 -08005809 ufshcd_is_link_broken(hba))));
Can Guoc72e79c2020-08-09 05:15:52 -07005810}
5811
5812#ifdef CONFIG_PM
5813static void ufshcd_recover_pm_error(struct ufs_hba *hba)
5814{
5815 struct Scsi_Host *shost = hba->host;
5816 struct scsi_device *sdev;
5817 struct request_queue *q;
5818 int ret;
5819
Can Guo88a92d62020-12-02 04:04:01 -08005820 hba->is_sys_suspended = false;
Can Guoc72e79c2020-08-09 05:15:52 -07005821 /*
5822 * Set RPM status of hba device to RPM_ACTIVE,
5823 * this also clears its runtime error.
5824 */
5825 ret = pm_runtime_set_active(hba->dev);
5826 /*
5827 * If hba device had runtime error, we also need to resume those
5828 * scsi devices under hba in case any of them has failed to be
5829 * resumed due to hba runtime resume failure. This is to unblock
5830 * blk_queue_enter in case there are bios waiting inside it.
5831 */
5832 if (!ret) {
5833 shost_for_each_device(sdev, shost) {
5834 q = sdev->request_queue;
5835 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
5836 q->rpm_status == RPM_SUSPENDING))
5837 pm_request_resume(q->dev);
5838 }
5839 }
5840}
5841#else
5842static inline void ufshcd_recover_pm_error(struct ufs_hba *hba)
5843{
5844}
5845#endif
5846
Can Guo2355b662020-08-24 19:07:06 -07005847static bool ufshcd_is_pwr_mode_restore_needed(struct ufs_hba *hba)
5848{
5849 struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info;
5850 u32 mode;
5851
5852 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode);
5853
5854 if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK))
5855 return true;
5856
5857 if (pwr_info->pwr_tx != (mode & PWRMODE_MASK))
5858 return true;
5859
5860 return false;
5861}
5862
Yaniv Gardi583fa622016-03-10 17:37:13 +02005863/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305864 * ufshcd_err_handler - handle UFS errors that require s/w attention
5865 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305866 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305867static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305868{
5869 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305870 unsigned long flags;
Can Guo307348f2020-08-24 19:07:05 -07005871 bool err_xfer = false;
5872 bool err_tm = false;
Can Guo2355b662020-08-24 19:07:06 -07005873 int err = 0, pmc_err;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305874 int tag;
Can Guo2355b662020-08-24 19:07:06 -07005875 bool needs_reset = false, needs_restore = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305876
5877 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305878
Can Guo9cd20d32021-01-13 19:13:28 -08005879 down(&hba->host_sem);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305880 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guoc72e79c2020-08-09 05:15:52 -07005881 if (ufshcd_err_handling_should_stop(hba)) {
Can Guo4db7a232020-08-09 05:15:51 -07005882 if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
5883 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5884 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guo9cd20d32021-01-13 19:13:28 -08005885 up(&hba->host_sem);
Can Guo4db7a232020-08-09 05:15:51 -07005886 return;
5887 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305888 ufshcd_set_eh_in_progress(hba);
Can Guo4db7a232020-08-09 05:15:51 -07005889 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guoc72e79c2020-08-09 05:15:52 -07005890 ufshcd_err_handling_prepare(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305891 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo5586dd82020-08-09 05:15:54 -07005892 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305893 hba->ufshcd_state = UFSHCD_STATE_RESET;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305894
5895 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005896 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005897
Can Guo88a92d62020-12-02 04:04:01 -08005898 /*
5899 * A full reset and restore might have happened after preparation
5900 * is finished, double check whether we should stop.
5901 */
5902 if (ufshcd_err_handling_should_stop(hba))
5903 goto skip_err_handling;
5904
Yaniv Gardi583fa622016-03-10 17:37:13 +02005905 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5906 bool ret;
5907
5908 spin_unlock_irqrestore(hba->host->host_lock, flags);
5909 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5910 ret = ufshcd_quirk_dl_nac_errors(hba);
5911 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo88a92d62020-12-02 04:04:01 -08005912 if (!ret && ufshcd_err_handling_should_stop(hba))
Yaniv Gardi583fa622016-03-10 17:37:13 +02005913 goto skip_err_handling;
5914 }
Can Guo4db7a232020-08-09 05:15:51 -07005915
Can Guo2355b662020-08-24 19:07:06 -07005916 if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
5917 (hba->saved_uic_err &&
5918 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
Can Guoc3be8d1e2020-08-09 05:15:53 -07005919 bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR);
5920
5921 spin_unlock_irqrestore(hba->host->host_lock, flags);
5922 ufshcd_print_host_state(hba);
5923 ufshcd_print_pwr_info(hba);
Stanley Chue965e5e2020-12-05 19:58:59 +08005924 ufshcd_print_evt_hist(hba);
Can Guoc3be8d1e2020-08-09 05:15:53 -07005925 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5926 ufshcd_print_trs(hba, hba->outstanding_reqs, pr_prdt);
5927 spin_lock_irqsave(hba->host->host_lock, flags);
5928 }
5929
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005930 /*
5931 * if host reset is required then skip clearing the pending
Can Guo2df74b62019-11-25 22:53:33 -08005932 * transfers forcefully because they will get cleared during
5933 * host reset and restore
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005934 */
Can Guo88a92d62020-12-02 04:04:01 -08005935 if (hba->force_reset || ufshcd_is_link_broken(hba) ||
5936 ufshcd_is_saved_err_fatal(hba) ||
5937 ((hba->saved_err & UIC_ERROR) &&
5938 (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5939 UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) {
5940 needs_reset = true;
Can Guo2355b662020-08-24 19:07:06 -07005941 goto do_reset;
Can Guo88a92d62020-12-02 04:04:01 -08005942 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005943
Can Guo2355b662020-08-24 19:07:06 -07005944 /*
5945 * If LINERESET was caught, UFS might have been put to PWM mode,
5946 * check if power mode restore is needed.
5947 */
5948 if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) {
5949 hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
5950 if (!hba->saved_uic_err)
5951 hba->saved_err &= ~UIC_ERROR;
5952 spin_unlock_irqrestore(hba->host->host_lock, flags);
5953 if (ufshcd_is_pwr_mode_restore_needed(hba))
5954 needs_restore = true;
5955 spin_lock_irqsave(hba->host->host_lock, flags);
5956 if (!hba->saved_err && !needs_restore)
5957 goto skip_err_handling;
5958 }
5959
5960 hba->silence_err_logs = true;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005961 /* release lock as clear command might sleep */
5962 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305963 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005964 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
Can Guo307348f2020-08-24 19:07:05 -07005965 if (ufshcd_try_to_abort_task(hba, tag)) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005966 err_xfer = true;
5967 goto lock_skip_pending_xfer_clear;
5968 }
5969 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305970
5971 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005972 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5973 if (ufshcd_clear_tm_cmd(hba, tag)) {
5974 err_tm = true;
5975 goto lock_skip_pending_xfer_clear;
5976 }
5977 }
5978
5979lock_skip_pending_xfer_clear:
5980 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305981
5982 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005983 ufshcd_complete_requests(hba);
Can Guo2355b662020-08-24 19:07:06 -07005984 hba->silence_err_logs = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305985
Can Guo2355b662020-08-24 19:07:06 -07005986 if (err_xfer || err_tm) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005987 needs_reset = true;
Can Guo2355b662020-08-24 19:07:06 -07005988 goto do_reset;
5989 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005990
Can Guo2355b662020-08-24 19:07:06 -07005991 /*
5992 * After all reqs and tasks are cleared from doorbell,
5993 * now it is safe to retore power mode.
5994 */
5995 if (needs_restore) {
5996 spin_unlock_irqrestore(hba->host->host_lock, flags);
5997 /*
5998 * Hold the scaling lock just in case dev cmds
5999 * are sent via bsg and/or sysfs.
6000 */
6001 down_write(&hba->clk_scaling_lock);
6002 hba->force_pmc = true;
6003 pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info));
6004 if (pmc_err) {
6005 needs_reset = true;
6006 dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n",
6007 __func__, pmc_err);
6008 }
6009 hba->force_pmc = false;
6010 ufshcd_print_pwr_info(hba);
6011 up_write(&hba->clk_scaling_lock);
6012 spin_lock_irqsave(hba->host->host_lock, flags);
6013 }
6014
6015do_reset:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306016 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006017 if (needs_reset) {
6018 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
6019
6020 /*
6021 * ufshcd_reset_and_restore() does the link reinitialization
6022 * which will need atleast one empty doorbell slot to send the
6023 * device management commands (NOP and query commands).
6024 * If there is no slot empty at this moment then free up last
6025 * slot forcefully.
6026 */
6027 if (hba->outstanding_reqs == max_doorbells)
6028 __ufshcd_transfer_req_compl(hba,
6029 (1UL << (hba->nutrs - 1)));
6030
Can Guo4db7a232020-08-09 05:15:51 -07006031 hba->force_reset = false;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006032 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306033 err = ufshcd_reset_and_restore(hba);
Can Guo4db7a232020-08-09 05:15:51 -07006034 if (err)
6035 dev_err(hba->dev, "%s: reset and restore failed with err %d\n",
6036 __func__, err);
Can Guoc72e79c2020-08-09 05:15:52 -07006037 else
6038 ufshcd_recover_pm_error(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006039 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306040 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006041
Yaniv Gardi583fa622016-03-10 17:37:13 +02006042skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006043 if (!needs_reset) {
Can Guo4db7a232020-08-09 05:15:51 -07006044 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
6045 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006046 if (hba->saved_err || hba->saved_uic_err)
6047 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
6048 __func__, hba->saved_err, hba->saved_uic_err);
6049 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306050 ufshcd_clear_eh_in_progress(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006051 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani38135532018-05-03 16:37:18 +05306052 ufshcd_scsi_unblock_requests(hba);
Can Guoc72e79c2020-08-09 05:15:52 -07006053 ufshcd_err_handling_unprepare(hba);
Can Guo9cd20d32021-01-13 19:13:28 -08006054 up(&hba->host_sem);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306055}
6056
6057/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306058 * ufshcd_update_uic_error - check and set fatal UIC error flags.
6059 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006060 *
6061 * Returns
6062 * IRQ_HANDLED - If interrupt is valid
6063 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306064 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006065static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306066{
6067 u32 reg;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006068 irqreturn_t retval = IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306069
Can Guo2355b662020-08-24 19:07:06 -07006070 /* PHY layer error */
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08006071 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08006072 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Can Guo2355b662020-08-24 19:07:06 -07006073 (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006074 ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg);
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08006075 /*
6076 * To know whether this error is fatal or not, DB timeout
6077 * must be checked but this error is handled separately.
6078 */
Can Guo2355b662020-08-24 19:07:06 -07006079 if (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)
6080 dev_dbg(hba->dev, "%s: UIC Lane error reported\n",
6081 __func__);
6082
6083 /* Got a LINERESET indication. */
6084 if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) {
6085 struct uic_command *cmd = NULL;
6086
6087 hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR;
6088 if (hba->uic_async_done && hba->active_uic_cmd)
6089 cmd = hba->active_uic_cmd;
6090 /*
6091 * Ignore the LINERESET during power mode change
6092 * operation via DME_SET command.
6093 */
6094 if (cmd && (cmd->command == UIC_CMD_DME_SET))
6095 hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
6096 }
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006097 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006098 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08006099
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306100 /* PA_INIT_ERROR is fatal and needs UIC reset */
6101 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006102 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
6103 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006104 ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006105
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006106 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
6107 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
6108 else if (hba->dev_quirks &
6109 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
6110 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
6111 hba->uic_error |=
6112 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
6113 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
6114 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
6115 }
6116 retval |= IRQ_HANDLED;
Yaniv Gardi583fa622016-03-10 17:37:13 +02006117 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306118
6119 /* UIC NL/TL/DME errors needs software retry */
6120 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006121 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
6122 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006123 ufshcd_update_evt_hist(hba, UFS_EVT_NL_ERR, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306124 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006125 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006126 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306127
6128 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006129 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
6130 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006131 ufshcd_update_evt_hist(hba, UFS_EVT_TL_ERR, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306132 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006133 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006134 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306135
6136 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006137 if ((reg & UIC_DME_ERROR) &&
6138 (reg & UIC_DME_ERROR_CODE_MASK)) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006139 ufshcd_update_evt_hist(hba, UFS_EVT_DME_ERR, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306140 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006141 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006142 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306143
6144 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
6145 __func__, hba->uic_error);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006146 return retval;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306147}
6148
Stanley Chu82174442019-05-21 14:44:54 +08006149static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
6150 u32 intr_mask)
6151{
Stanley Chu5a244e02020-01-29 18:52:50 +08006152 if (!ufshcd_is_auto_hibern8_supported(hba) ||
6153 !ufshcd_is_auto_hibern8_enabled(hba))
Stanley Chu82174442019-05-21 14:44:54 +08006154 return false;
6155
6156 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
6157 return false;
6158
6159 if (hba->active_uic_cmd &&
6160 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
6161 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
6162 return false;
6163
6164 return true;
6165}
6166
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306167/**
6168 * ufshcd_check_errors - Check for errors that need s/w attention
6169 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006170 *
6171 * Returns
6172 * IRQ_HANDLED - If interrupt is valid
6173 * IRQ_NONE - If invalid interrupt
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306174 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006175static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306176{
6177 bool queue_eh_work = false;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006178 irqreturn_t retval = IRQ_NONE;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306179
Stanley Chud3c615b2019-07-10 21:38:19 +08006180 if (hba->errors & INT_FATAL_ERRORS) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006181 ufshcd_update_evt_hist(hba, UFS_EVT_FATAL_ERR,
6182 hba->errors);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306183 queue_eh_work = true;
Stanley Chud3c615b2019-07-10 21:38:19 +08006184 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306185
6186 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306187 hba->uic_error = 0;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006188 retval = ufshcd_update_uic_error(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306189 if (hba->uic_error)
6190 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306191 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306192
Stanley Chu82174442019-05-21 14:44:54 +08006193 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
6194 dev_err(hba->dev,
6195 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
6196 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
6197 "Enter" : "Exit",
6198 hba->errors, ufshcd_get_upmcrs(hba));
Stanley Chue965e5e2020-12-05 19:58:59 +08006199 ufshcd_update_evt_hist(hba, UFS_EVT_AUTO_HIBERN8_ERR,
Stanley Chud3c615b2019-07-10 21:38:19 +08006200 hba->errors);
Can Guo4db7a232020-08-09 05:15:51 -07006201 ufshcd_set_link_broken(hba);
Stanley Chu82174442019-05-21 14:44:54 +08006202 queue_eh_work = true;
6203 }
6204
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306205 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02006206 /*
6207 * update the transfer error masks to sticky bits, let's do this
6208 * irrespective of current ufshcd_state.
6209 */
6210 hba->saved_err |= hba->errors;
6211 hba->saved_uic_err |= hba->uic_error;
6212
Can Guo4db7a232020-08-09 05:15:51 -07006213 /* dump controller state before resetting */
Can Guoace38042020-12-02 04:04:03 -08006214 if ((hba->saved_err &
6215 (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
Can Guo2355b662020-08-24 19:07:06 -07006216 (hba->saved_uic_err &&
6217 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
Can Guo4db7a232020-08-09 05:15:51 -07006218 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
Dolev Raviv66cc8202016-12-22 18:39:42 -08006219 __func__, hba->saved_err,
6220 hba->saved_uic_err);
Can Guoc3be8d1e2020-08-09 05:15:53 -07006221 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE,
6222 "host_regs: ");
Can Guo4db7a232020-08-09 05:15:51 -07006223 ufshcd_print_pwr_info(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306224 }
Can Guo4db7a232020-08-09 05:15:51 -07006225 ufshcd_schedule_eh_work(hba);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006226 retval |= IRQ_HANDLED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306227 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306228 /*
6229 * if (!queue_eh_work) -
6230 * Other errors are either non-fatal where host recovers
6231 * itself without s/w intervention or errors that will be
6232 * handled by the SCSI core layer.
6233 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006234 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306235}
6236
Bart Van Assche69a6c262019-12-09 10:13:09 -08006237struct ctm_info {
6238 struct ufs_hba *hba;
6239 unsigned long pending;
6240 unsigned int ncpl;
6241};
6242
6243static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved)
6244{
6245 struct ctm_info *const ci = priv;
6246 struct completion *c;
6247
6248 WARN_ON_ONCE(reserved);
6249 if (test_bit(req->tag, &ci->pending))
6250 return true;
6251 ci->ncpl++;
6252 c = req->end_io_data;
6253 if (c)
6254 complete(c);
6255 return true;
6256}
6257
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306258/**
6259 * ufshcd_tmc_handler - handle task management function completion
6260 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006261 *
6262 * Returns
6263 * IRQ_HANDLED - If interrupt is valid
6264 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306265 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006266static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306267{
Bart Van Assche69a6c262019-12-09 10:13:09 -08006268 struct request_queue *q = hba->tmf_queue;
6269 struct ctm_info ci = {
6270 .hba = hba,
6271 .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL),
6272 };
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306273
Bart Van Assche69a6c262019-12-09 10:13:09 -08006274 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci);
6275 return ci.ncpl ? IRQ_HANDLED : IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306276}
6277
6278/**
6279 * ufshcd_sl_intr - Interrupt service routine
6280 * @hba: per adapter instance
6281 * @intr_status: contains interrupts generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006282 *
6283 * Returns
6284 * IRQ_HANDLED - If interrupt is valid
6285 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306286 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006287static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306288{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006289 irqreturn_t retval = IRQ_NONE;
6290
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306291 hba->errors = UFSHCD_ERROR_MASK & intr_status;
Stanley Chu82174442019-05-21 14:44:54 +08006292
6293 if (ufshcd_is_auto_hibern8_error(hba, intr_status))
6294 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
6295
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306296 if (hba->errors)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006297 retval |= ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306298
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05306299 if (intr_status & UFSHCD_UIC_MASK)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006300 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306301
6302 if (intr_status & UTP_TASK_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006303 retval |= ufshcd_tmc_handler(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306304
6305 if (intr_status & UTP_TRANSFER_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006306 retval |= ufshcd_transfer_req_compl(hba);
6307
6308 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306309}
6310
6311/**
6312 * ufshcd_intr - Main interrupt service routine
6313 * @irq: irq number
6314 * @__hba: pointer to adapter instance
6315 *
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006316 * Returns
6317 * IRQ_HANDLED - If interrupt is valid
6318 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306319 */
6320static irqreturn_t ufshcd_intr(int irq, void *__hba)
6321{
Adrian Hunter127d5f72020-08-11 16:39:36 +03006322 u32 intr_status, enabled_intr_status = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306323 irqreturn_t retval = IRQ_NONE;
6324 struct ufs_hba *hba = __hba;
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306325 int retries = hba->nutrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306326
6327 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306328 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Can Guo3f8af602020-08-09 05:15:50 -07006329 hba->ufs_stats.last_intr_status = intr_status;
6330 hba->ufs_stats.last_intr_ts = ktime_get();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306331
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306332 /*
6333 * There could be max of hba->nutrs reqs in flight and in worst case
6334 * if the reqs get finished 1 by 1 after the interrupt status is
6335 * read, make sure we handle them by checking the interrupt status
6336 * again in a loop until we process all of the reqs before returning.
6337 */
Adrian Hunter127d5f72020-08-11 16:39:36 +03006338 while (intr_status && retries--) {
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306339 enabled_intr_status =
6340 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Bean Huo60ec3752021-01-18 21:12:33 +01006341 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006342 if (enabled_intr_status)
6343 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02006344
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306345 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Adrian Hunter127d5f72020-08-11 16:39:36 +03006346 }
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05306347
Adrian Hunter6337f582020-08-11 16:39:35 +03006348 if (enabled_intr_status && retval == IRQ_NONE) {
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08006349 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n",
6350 __func__, intr_status);
6351 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
6352 }
6353
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306354 spin_unlock(hba->host->host_lock);
6355 return retval;
6356}
6357
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306358static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
6359{
6360 int err = 0;
6361 u32 mask = 1 << tag;
6362 unsigned long flags;
6363
6364 if (!test_bit(tag, &hba->outstanding_tasks))
6365 goto out;
6366
6367 spin_lock_irqsave(hba->host->host_lock, flags);
Alim Akhtar1399c5b2018-05-06 15:44:15 +05306368 ufshcd_utmrl_clear(hba, tag);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306369 spin_unlock_irqrestore(hba->host->host_lock, flags);
6370
6371 /* poll for max. 1 sec to clear door bell register by h/w */
6372 err = ufshcd_wait_for_register(hba,
6373 REG_UTP_TASK_REQ_DOOR_BELL,
Bart Van Assche5cac1092020-05-07 15:27:50 -07006374 mask, 0, 1000, 1000);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306375out:
6376 return err;
6377}
6378
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006379static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
6380 struct utp_task_req_desc *treq, u8 tm_function)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306381{
Bart Van Assche69a6c262019-12-09 10:13:09 -08006382 struct request_queue *q = hba->tmf_queue;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006383 struct Scsi_Host *host = hba->host;
Bart Van Assche69a6c262019-12-09 10:13:09 -08006384 DECLARE_COMPLETION_ONSTACK(wait);
6385 struct request *req;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306386 unsigned long flags;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006387 int free_slot, task_tag, err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306388
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306389 /*
6390 * Get free slot, sleep if slots are unavailable.
6391 * Even though we use wait_event() which sleeps indefinitely,
6392 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
6393 */
Bart Van Assche69a6c262019-12-09 10:13:09 -08006394 req = blk_get_request(q, REQ_OP_DRV_OUT, BLK_MQ_REQ_RESERVED);
6395 req->end_io_data = &wait;
6396 free_slot = req->tag;
6397 WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006398 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306399
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306400 spin_lock_irqsave(host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306401 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306402
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006403 treq->req_header.dword_0 |= cpu_to_be32(task_tag);
6404
6405 memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq));
Kiwoong Kimd2877be2016-11-10 21:16:15 +09006406 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
6407
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306408 /* send command to the controller */
6409 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02006410
6411 /* Make sure descriptors are ready before ringing the task doorbell */
6412 wmb();
6413
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306414 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07006415 /* Make sure that doorbell is committed immediately */
6416 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306417
6418 spin_unlock_irqrestore(host->host_lock, flags);
6419
Bean Huo28fa68f2021-01-05 12:34:42 +01006420 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_SEND);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03006421
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306422 /* wait until the task management command is completed */
Bart Van Assche69a6c262019-12-09 10:13:09 -08006423 err = wait_for_completion_io_timeout(&wait,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306424 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306425 if (!err) {
Bart Van Assche69a6c262019-12-09 10:13:09 -08006426 /*
6427 * Make sure that ufshcd_compl_tm() does not trigger a
6428 * use-after-free.
6429 */
6430 req->end_io_data = NULL;
Bean Huo28fa68f2021-01-05 12:34:42 +01006431 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306432 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6433 __func__, tm_function);
6434 if (ufshcd_clear_tm_cmd(hba, free_slot))
6435 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
6436 __func__, free_slot);
6437 err = -ETIMEDOUT;
6438 } else {
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006439 err = 0;
6440 memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq));
6441
Bean Huo28fa68f2021-01-05 12:34:42 +01006442 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_COMP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306443 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306444
Stanley Chub5572172019-08-19 21:43:28 +08006445 spin_lock_irqsave(hba->host->host_lock, flags);
6446 __clear_bit(free_slot, &hba->outstanding_tasks);
6447 spin_unlock_irqrestore(hba->host->host_lock, flags);
6448
Bart Van Assche69a6c262019-12-09 10:13:09 -08006449 blk_put_request(req);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306450
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006451 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306452 return err;
6453}
6454
6455/**
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006456 * ufshcd_issue_tm_cmd - issues task management commands to controller
6457 * @hba: per adapter instance
6458 * @lun_id: LUN ID to which TM command is sent
6459 * @task_id: task ID to which the TM command is applicable
6460 * @tm_function: task management function opcode
6461 * @tm_response: task management service response return value
6462 *
6463 * Returns non-zero value on error, zero on success.
6464 */
6465static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
6466 u8 tm_function, u8 *tm_response)
6467{
6468 struct utp_task_req_desc treq = { { 0 }, };
6469 int ocs_value, err;
6470
6471 /* Configure task request descriptor */
6472 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6473 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6474
6475 /* Configure task request UPIU */
6476 treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) |
6477 cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24);
6478 treq.req_header.dword_1 = cpu_to_be32(tm_function << 16);
6479
6480 /*
6481 * The host shall provide the same value for LUN field in the basic
6482 * header and for Input Parameter.
6483 */
6484 treq.input_param1 = cpu_to_be32(lun_id);
6485 treq.input_param2 = cpu_to_be32(task_id);
6486
6487 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
6488 if (err == -ETIMEDOUT)
6489 return err;
6490
6491 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6492 if (ocs_value != OCS_SUCCESS)
6493 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
6494 __func__, ocs_value);
6495 else if (tm_response)
6496 *tm_response = be32_to_cpu(treq.output_param1) &
6497 MASK_TM_SERVICE_RESP;
6498 return err;
6499}
6500
6501/**
Avri Altman5e0a86e2018-10-07 17:30:37 +03006502 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
6503 * @hba: per-adapter instance
6504 * @req_upiu: upiu request
6505 * @rsp_upiu: upiu reply
Avri Altman5e0a86e2018-10-07 17:30:37 +03006506 * @desc_buff: pointer to descriptor buffer, NULL if NA
6507 * @buff_len: descriptor size, 0 if NA
Bart Van Assched0e97602019-10-29 16:07:08 -07006508 * @cmd_type: specifies the type (NOP, Query...)
Avri Altman5e0a86e2018-10-07 17:30:37 +03006509 * @desc_op: descriptor operation
6510 *
6511 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
6512 * Therefore, it "rides" the device management infrastructure: uses its tag and
6513 * tasks work queues.
6514 *
6515 * Since there is only one available tag for device management commands,
6516 * the caller is expected to hold the hba->dev_cmd.lock mutex.
6517 */
6518static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
6519 struct utp_upiu_req *req_upiu,
6520 struct utp_upiu_req *rsp_upiu,
6521 u8 *desc_buff, int *buff_len,
Bart Van Assche7f674c32019-10-29 16:07:09 -07006522 enum dev_cmd_type cmd_type,
Avri Altman5e0a86e2018-10-07 17:30:37 +03006523 enum query_opcode desc_op)
6524{
Bart Van Assche7252a362019-12-09 10:13:08 -08006525 struct request_queue *q = hba->cmd_queue;
6526 struct request *req;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006527 struct ufshcd_lrb *lrbp;
6528 int err = 0;
6529 int tag;
6530 struct completion wait;
6531 unsigned long flags;
Bean Huoa23064c2020-07-06 14:39:36 +02006532 u8 upiu_flags;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006533
6534 down_read(&hba->clk_scaling_lock);
6535
Bart Van Assche7252a362019-12-09 10:13:08 -08006536 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03006537 if (IS_ERR(req)) {
6538 err = PTR_ERR(req);
6539 goto out_unlock;
6540 }
Bart Van Assche7252a362019-12-09 10:13:08 -08006541 tag = req->tag;
6542 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Avri Altman5e0a86e2018-10-07 17:30:37 +03006543
6544 init_completion(&wait);
6545 lrbp = &hba->lrb[tag];
Can Guo7a7e66c2020-12-02 04:04:02 -08006546 if (unlikely(lrbp->in_use)) {
6547 err = -EBUSY;
6548 goto out;
6549 }
Avri Altman5e0a86e2018-10-07 17:30:37 +03006550
Can Guo7a7e66c2020-12-02 04:04:02 -08006551 WARN_ON(lrbp->cmd);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006552 lrbp->cmd = NULL;
6553 lrbp->sense_bufflen = 0;
6554 lrbp->sense_buffer = NULL;
6555 lrbp->task_tag = tag;
6556 lrbp->lun = 0;
6557 lrbp->intr_cmd = true;
Satya Tangiraladf043c742020-07-06 20:04:14 +00006558 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006559 hba->dev_cmd.type = cmd_type;
6560
6561 switch (hba->ufs_version) {
6562 case UFSHCI_VERSION_10:
6563 case UFSHCI_VERSION_11:
6564 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
6565 break;
6566 default:
6567 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
6568 break;
6569 }
6570
6571 /* update the task tag in the request upiu */
6572 req_upiu->header.dword_0 |= cpu_to_be32(tag);
6573
6574 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
6575
6576 /* just copy the upiu request as it is */
6577 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
6578 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
6579 /* The Data Segment Area is optional depending upon the query
6580 * function value. for WRITE DESCRIPTOR, the data segment
6581 * follows right after the tsf.
6582 */
6583 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
6584 *buff_len = 0;
6585 }
6586
6587 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
6588
6589 hba->dev_cmd.complete = &wait;
6590
6591 /* Make sure descriptors are ready before ringing the doorbell */
6592 wmb();
6593 spin_lock_irqsave(hba->host->host_lock, flags);
6594 ufshcd_send_command(hba, tag);
6595 spin_unlock_irqrestore(hba->host->host_lock, flags);
6596
6597 /*
6598 * ignore the returning value here - ufshcd_check_query_response is
6599 * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
6600 * read the response directly ignoring all errors.
6601 */
6602 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT);
6603
6604 /* just copy the upiu response as it is */
6605 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
Avri Altman4bbbe242019-02-20 09:11:13 +02006606 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
6607 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
6608 u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
6609 MASK_QUERY_DATA_SEG_LEN;
6610
6611 if (*buff_len >= resp_len) {
6612 memcpy(desc_buff, descp, resp_len);
6613 *buff_len = resp_len;
6614 } else {
Bean Huo3d4881d2019-11-12 23:34:35 +01006615 dev_warn(hba->dev,
6616 "%s: rsp size %d is bigger than buffer size %d",
6617 __func__, resp_len, *buff_len);
Avri Altman4bbbe242019-02-20 09:11:13 +02006618 *buff_len = 0;
6619 err = -EINVAL;
6620 }
6621 }
Avri Altman5e0a86e2018-10-07 17:30:37 +03006622
Can Guo7a7e66c2020-12-02 04:04:02 -08006623out:
Bart Van Assche7252a362019-12-09 10:13:08 -08006624 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03006625out_unlock:
Avri Altman5e0a86e2018-10-07 17:30:37 +03006626 up_read(&hba->clk_scaling_lock);
6627 return err;
6628}
6629
6630/**
6631 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
6632 * @hba: per-adapter instance
6633 * @req_upiu: upiu request
6634 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
6635 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
6636 * @desc_buff: pointer to descriptor buffer, NULL if NA
6637 * @buff_len: descriptor size, 0 if NA
6638 * @desc_op: descriptor operation
6639 *
6640 * Supports UTP Transfer requests (nop and query), and UTP Task
6641 * Management requests.
6642 * It is up to the caller to fill the upiu conent properly, as it will
6643 * be copied without any further input validations.
6644 */
6645int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
6646 struct utp_upiu_req *req_upiu,
6647 struct utp_upiu_req *rsp_upiu,
6648 int msgcode,
6649 u8 *desc_buff, int *buff_len,
6650 enum query_opcode desc_op)
6651{
6652 int err;
Bart Van Assche7f674c32019-10-29 16:07:09 -07006653 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006654 struct utp_task_req_desc treq = { { 0 }, };
6655 int ocs_value;
6656 u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC;
6657
Avri Altman5e0a86e2018-10-07 17:30:37 +03006658 switch (msgcode) {
6659 case UPIU_TRANSACTION_NOP_OUT:
6660 cmd_type = DEV_CMD_TYPE_NOP;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05006661 fallthrough;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006662 case UPIU_TRANSACTION_QUERY_REQ:
6663 ufshcd_hold(hba, false);
6664 mutex_lock(&hba->dev_cmd.lock);
6665 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
6666 desc_buff, buff_len,
6667 cmd_type, desc_op);
6668 mutex_unlock(&hba->dev_cmd.lock);
6669 ufshcd_release(hba);
6670
6671 break;
6672 case UPIU_TRANSACTION_TASK_REQ:
6673 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6674 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6675
6676 memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu));
6677
6678 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
6679 if (err == -ETIMEDOUT)
6680 break;
6681
6682 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6683 if (ocs_value != OCS_SUCCESS) {
6684 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
6685 ocs_value);
6686 break;
6687 }
6688
6689 memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu));
6690
6691 break;
6692 default:
6693 err = -EINVAL;
6694
6695 break;
6696 }
6697
Avri Altman5e0a86e2018-10-07 17:30:37 +03006698 return err;
6699}
6700
6701/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306702 * ufshcd_eh_device_reset_handler - device reset handler registered to
6703 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306704 * @cmd: SCSI command pointer
6705 *
6706 * Returns SUCCESS/FAILED
6707 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306708static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306709{
6710 struct Scsi_Host *host;
6711 struct ufs_hba *hba;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306712 u32 pos;
6713 int err;
Can Guo35fc4cd2020-12-28 04:04:36 -08006714 u8 resp = 0xF, lun;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306715 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306716
6717 host = cmd->device->host;
6718 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306719
Can Guo35fc4cd2020-12-28 04:04:36 -08006720 lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
6721 err = ufshcd_issue_tm_cmd(hba, lun, 0, UFS_LOGICAL_RESET, &resp);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306722 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306723 if (!err)
6724 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306725 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306726 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306727
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306728 /* clear the commands that were pending for corresponding LUN */
6729 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
Can Guo35fc4cd2020-12-28 04:04:36 -08006730 if (hba->lrb[pos].lun == lun) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306731 err = ufshcd_clear_cmd(hba, pos);
6732 if (err)
6733 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306734 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306735 }
6736 spin_lock_irqsave(host->host_lock, flags);
6737 ufshcd_transfer_req_compl(hba);
6738 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08006739
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306740out:
Gilad Broner7fabb772017-02-03 16:56:50 -08006741 hba->req_abort_count = 0;
Stanley Chue965e5e2020-12-05 19:58:59 +08006742 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306743 if (!err) {
6744 err = SUCCESS;
6745 } else {
6746 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6747 err = FAILED;
6748 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306749 return err;
6750}
6751
Gilad Bronere0b299e2017-02-03 16:56:40 -08006752static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6753{
6754 struct ufshcd_lrb *lrbp;
6755 int tag;
6756
6757 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6758 lrbp = &hba->lrb[tag];
6759 lrbp->req_abort_skip = true;
6760 }
6761}
6762
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306763/**
Can Guo307348f2020-08-24 19:07:05 -07006764 * ufshcd_try_to_abort_task - abort a specific task
Lee Jonesd23ec0b2020-11-02 14:23:51 +00006765 * @hba: Pointer to adapter instance
6766 * @tag: Task tag/index to be aborted
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306767 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306768 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6769 * command, and in host controller by clearing the door-bell register. There can
6770 * be race between controller sending the command to the device while abort is
6771 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6772 * really issued and then try to abort it.
6773 *
Can Guo307348f2020-08-24 19:07:05 -07006774 * Returns zero on success, non-zero on failure
6775 */
6776static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag)
6777{
6778 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
6779 int err = 0;
6780 int poll_cnt;
6781 u8 resp = 0xF;
6782 u32 reg;
6783
6784 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6785 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6786 UFS_QUERY_TASK, &resp);
6787 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6788 /* cmd pending in the device */
6789 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
6790 __func__, tag);
6791 break;
6792 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6793 /*
6794 * cmd not pending in the device, check if it is
6795 * in transition.
6796 */
6797 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
6798 __func__, tag);
6799 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6800 if (reg & (1 << tag)) {
6801 /* sleep for max. 200us to stabilize */
6802 usleep_range(100, 200);
6803 continue;
6804 }
6805 /* command completed already */
6806 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
6807 __func__, tag);
6808 goto out;
6809 } else {
6810 dev_err(hba->dev,
6811 "%s: no response from device. tag = %d, err %d\n",
6812 __func__, tag, err);
6813 if (!err)
6814 err = resp; /* service response error */
6815 goto out;
6816 }
6817 }
6818
6819 if (!poll_cnt) {
6820 err = -EBUSY;
6821 goto out;
6822 }
6823
6824 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6825 UFS_ABORT_TASK, &resp);
6826 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6827 if (!err) {
6828 err = resp; /* service response error */
6829 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
6830 __func__, tag, err);
6831 }
6832 goto out;
6833 }
6834
6835 err = ufshcd_clear_cmd(hba, tag);
6836 if (err)
6837 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
6838 __func__, tag, err);
6839
6840out:
6841 return err;
6842}
6843
6844/**
6845 * ufshcd_abort - scsi host template eh_abort_handler callback
6846 * @cmd: SCSI command pointer
6847 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306848 * Returns SUCCESS/FAILED
6849 */
6850static int ufshcd_abort(struct scsi_cmnd *cmd)
6851{
6852 struct Scsi_Host *host;
6853 struct ufs_hba *hba;
6854 unsigned long flags;
6855 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306856 int err = 0;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306857 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03006858 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306859
6860 host = cmd->device->host;
6861 hba = shost_priv(host);
6862 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08006863 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02006864 if (!ufshcd_valid_tag(hba, tag)) {
6865 dev_err(hba->dev,
6866 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6867 __func__, tag, cmd, cmd->request);
6868 BUG();
6869 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306870
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006871 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03006872 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02006873 /* If command is already aborted/completed, return SUCCESS */
6874 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6875 dev_err(hba->dev,
6876 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6877 __func__, tag, hba->outstanding_reqs, reg);
6878 goto out;
6879 }
6880
Dolev Raviv66cc8202016-12-22 18:39:42 -08006881 /* Print Transfer Request of aborted task */
Bean Huod87a1f62020-08-11 16:18:59 +02006882 dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08006883
Gilad Broner7fabb772017-02-03 16:56:50 -08006884 /*
6885 * Print detailed info about aborted request.
6886 * As more than one request might get aborted at the same time,
6887 * print full information only for the first aborted request in order
6888 * to reduce repeated printouts. For other aborted requests only print
6889 * basic details.
6890 */
Can Guo7a7e66c2020-12-02 04:04:02 -08006891 scsi_print_command(cmd);
Gilad Broner7fabb772017-02-03 16:56:50 -08006892 if (!hba->req_abort_count) {
Stanley Chue965e5e2020-12-05 19:58:59 +08006893 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag);
6894 ufshcd_print_evt_hist(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08006895 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08006896 ufshcd_print_pwr_info(hba);
6897 ufshcd_print_trs(hba, 1 << tag, true);
6898 } else {
6899 ufshcd_print_trs(hba, 1 << tag, false);
6900 }
6901 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08006902
Bean Huod87a1f62020-08-11 16:18:59 +02006903 if (!(reg & (1 << tag))) {
6904 dev_err(hba->dev,
6905 "%s: cmd was completed, but without a notifying intr, tag = %d",
6906 __func__, tag);
6907 goto cleanup;
6908 }
6909
Can Guo7a7e66c2020-12-02 04:04:02 -08006910 /*
6911 * Task abort to the device W-LUN is illegal. When this command
6912 * will fail, due to spec violation, scsi err handling next step
6913 * will be to send LU reset which, again, is a spec violation.
6914 * To avoid these unnecessary/illegal steps, first we clean up
6915 * the lrb taken by this cmd and mark the lrb as in_use, then
6916 * queue the eh_work and bail.
6917 */
6918 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) {
6919 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun);
6920 spin_lock_irqsave(host->host_lock, flags);
6921 if (lrbp->cmd) {
6922 __ufshcd_transfer_req_compl(hba, (1UL << tag));
6923 __set_bit(tag, &hba->outstanding_reqs);
6924 lrbp->in_use = true;
6925 hba->force_reset = true;
6926 ufshcd_schedule_eh_work(hba);
6927 }
6928
6929 spin_unlock_irqrestore(host->host_lock, flags);
6930 goto out;
6931 }
6932
Gilad Bronere0b299e2017-02-03 16:56:40 -08006933 /* Skip task abort in case previous aborts failed and report failure */
Martin K. Petersen02f74152020-09-15 11:24:32 -04006934 if (lrbp->req_abort_skip)
Gilad Bronere0b299e2017-02-03 16:56:40 -08006935 err = -EIO;
Martin K. Petersen02f74152020-09-15 11:24:32 -04006936 else
6937 err = ufshcd_try_to_abort_task(hba, tag);
Gilad Bronere0b299e2017-02-03 16:56:40 -08006938
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306939 if (!err) {
Martin K. Petersen02f74152020-09-15 11:24:32 -04006940cleanup:
6941 spin_lock_irqsave(host->host_lock, flags);
6942 __ufshcd_transfer_req_compl(hba, (1UL << tag));
6943 spin_unlock_irqrestore(host->host_lock, flags);
6944out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306945 err = SUCCESS;
6946 } else {
6947 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08006948 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306949 err = FAILED;
6950 }
6951
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006952 /*
6953 * This ufshcd_release() corresponds to the original scsi cmd that got
6954 * aborted here (as we won't get any IRQ for it).
6955 */
6956 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306957 return err;
6958}
6959
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306960/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306961 * ufshcd_host_reset_and_restore - reset and restore host controller
6962 * @hba: per-adapter instance
6963 *
6964 * Note that host controller reset may issue DME_RESET to
6965 * local and remote (device) Uni-Pro stack and the attributes
6966 * are reset to default state.
6967 *
6968 * Returns zero on success, non-zero on failure
6969 */
6970static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6971{
6972 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306973 unsigned long flags;
6974
Can Guo2df74b62019-11-25 22:53:33 -08006975 /*
6976 * Stop the host controller and complete the requests
6977 * cleared by h/w
6978 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07006979 ufshcd_hba_stop(hba);
6980
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306981 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo2df74b62019-11-25 22:53:33 -08006982 hba->silence_err_logs = true;
6983 ufshcd_complete_requests(hba);
6984 hba->silence_err_logs = false;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306985 spin_unlock_irqrestore(hba->host->host_lock, flags);
6986
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08006987 /* scale up clocks to max frequency before full reinitialization */
Subhash Jadavani394b9492020-03-26 02:25:40 -07006988 ufshcd_set_clk_freq(hba, true);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08006989
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306990 err = ufshcd_hba_enable(hba);
6991 if (err)
6992 goto out;
6993
6994 /* Establish the link again and restore the device */
Bean Huo1b9e2142020-01-20 14:08:15 +01006995 err = ufshcd_probe_hba(hba, false);
Randall Huang19186512020-11-30 20:14:02 -08006996 if (!err)
6997 ufshcd_clear_ua_wluns(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306998out:
6999 if (err)
7000 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
Stanley Chue965e5e2020-12-05 19:58:59 +08007001 ufshcd_update_evt_hist(hba, UFS_EVT_HOST_RESET, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307002 return err;
7003}
7004
7005/**
7006 * ufshcd_reset_and_restore - reset and re-initialize host/device
7007 * @hba: per-adapter instance
7008 *
7009 * Reset and recover device, host and re-establish link. This
7010 * is helpful to recover the communication in fatal error conditions.
7011 *
7012 * Returns zero on success, non-zero on failure
7013 */
7014static int ufshcd_reset_and_restore(struct ufs_hba *hba)
7015{
Can Guo4db7a232020-08-09 05:15:51 -07007016 u32 saved_err;
7017 u32 saved_uic_err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307018 int err = 0;
Can Guo4db7a232020-08-09 05:15:51 -07007019 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007020 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307021
Can Guo4db7a232020-08-09 05:15:51 -07007022 /*
7023 * This is a fresh start, cache and clear saved error first,
7024 * in case new error generated during reset and restore.
7025 */
7026 spin_lock_irqsave(hba->host->host_lock, flags);
7027 saved_err = hba->saved_err;
7028 saved_uic_err = hba->saved_uic_err;
7029 hba->saved_err = 0;
7030 hba->saved_uic_err = 0;
7031 spin_unlock_irqrestore(hba->host->host_lock, flags);
7032
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007033 do {
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07007034 /* Reset the attached device */
Stanley Chu31a5d9c2020-12-08 21:56:35 +08007035 ufshcd_device_reset(hba);
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07007036
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007037 err = ufshcd_host_reset_and_restore(hba);
7038 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307039
Can Guo4db7a232020-08-09 05:15:51 -07007040 spin_lock_irqsave(hba->host->host_lock, flags);
7041 /*
7042 * Inform scsi mid-layer that we did reset and allow to handle
7043 * Unit Attention properly.
7044 */
7045 scsi_report_bus_reset(hba->host, 0);
7046 if (err) {
Can Guo88a92d62020-12-02 04:04:01 -08007047 hba->ufshcd_state = UFSHCD_STATE_ERROR;
Can Guo4db7a232020-08-09 05:15:51 -07007048 hba->saved_err |= saved_err;
7049 hba->saved_uic_err |= saved_uic_err;
7050 }
7051 spin_unlock_irqrestore(hba->host->host_lock, flags);
7052
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307053 return err;
7054}
7055
7056/**
7057 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
Bart Van Assche8aa29f12018-03-01 15:07:20 -08007058 * @cmd: SCSI command pointer
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307059 *
7060 * Returns SUCCESS/FAILED
7061 */
7062static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
7063{
Can Guo4db7a232020-08-09 05:15:51 -07007064 int err = SUCCESS;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307065 unsigned long flags;
7066 struct ufs_hba *hba;
7067
7068 hba = shost_priv(cmd->device->host);
7069
Can Guo4db7a232020-08-09 05:15:51 -07007070 spin_lock_irqsave(hba->host->host_lock, flags);
7071 hba->force_reset = true;
7072 ufshcd_schedule_eh_work(hba);
7073 dev_err(hba->dev, "%s: reset in progress - 1\n", __func__);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307074 spin_unlock_irqrestore(hba->host->host_lock, flags);
7075
Can Guo4db7a232020-08-09 05:15:51 -07007076 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307077
7078 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo4db7a232020-08-09 05:15:51 -07007079 if (hba->ufshcd_state == UFSHCD_STATE_ERROR)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307080 err = FAILED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307081 spin_unlock_irqrestore(hba->host->host_lock, flags);
7082
7083 return err;
7084}
7085
7086/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007087 * ufshcd_get_max_icc_level - calculate the ICC level
7088 * @sup_curr_uA: max. current supported by the regulator
7089 * @start_scan: row at the desc table to start scan from
7090 * @buff: power descriptor buffer
7091 *
7092 * Returns calculated max ICC level for specific regulator
7093 */
7094static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
7095{
7096 int i;
7097 int curr_uA;
7098 u16 data;
7099 u16 unit;
7100
7101 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02007102 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007103 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
7104 ATTR_ICC_LVL_UNIT_OFFSET;
7105 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
7106 switch (unit) {
7107 case UFSHCD_NANO_AMP:
7108 curr_uA = curr_uA / 1000;
7109 break;
7110 case UFSHCD_MILI_AMP:
7111 curr_uA = curr_uA * 1000;
7112 break;
7113 case UFSHCD_AMP:
7114 curr_uA = curr_uA * 1000 * 1000;
7115 break;
7116 case UFSHCD_MICRO_AMP:
7117 default:
7118 break;
7119 }
7120 if (sup_curr_uA >= curr_uA)
7121 break;
7122 }
7123 if (i < 0) {
7124 i = 0;
7125 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
7126 }
7127
7128 return (u32)i;
7129}
7130
7131/**
7132 * ufshcd_calc_icc_level - calculate the max ICC level
7133 * In case regulators are not initialized we'll return 0
7134 * @hba: per-adapter instance
7135 * @desc_buf: power descriptor buffer to extract ICC levels from.
7136 * @len: length of desc_buff
7137 *
7138 * Returns calculated ICC level
7139 */
7140static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
7141 u8 *desc_buf, int len)
7142{
7143 u32 icc_level = 0;
7144
7145 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
7146 !hba->vreg_info.vccq2) {
7147 dev_err(hba->dev,
7148 "%s: Regulator capability was not set, actvIccLevel=%d",
7149 __func__, icc_level);
7150 goto out;
7151 }
7152
Stanley Chu0487fff2019-03-28 17:16:25 +08007153 if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007154 icc_level = ufshcd_get_max_icc_level(
7155 hba->vreg_info.vcc->max_uA,
7156 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
7157 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
7158
Stanley Chu0487fff2019-03-28 17:16:25 +08007159 if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007160 icc_level = ufshcd_get_max_icc_level(
7161 hba->vreg_info.vccq->max_uA,
7162 icc_level,
7163 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
7164
Stanley Chu0487fff2019-03-28 17:16:25 +08007165 if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007166 icc_level = ufshcd_get_max_icc_level(
7167 hba->vreg_info.vccq2->max_uA,
7168 icc_level,
7169 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
7170out:
7171 return icc_level;
7172}
7173
Can Guoe89860f2020-03-26 02:25:41 -07007174static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007175{
7176 int ret;
Bean Huo7a0bf852020-06-03 11:19:58 +02007177 int buff_len = hba->desc_size[QUERY_DESC_IDN_POWER];
Kees Cookbbe21d72018-05-02 16:58:09 -07007178 u8 *desc_buf;
Can Guoe89860f2020-03-26 02:25:41 -07007179 u32 icc_level;
Kees Cookbbe21d72018-05-02 16:58:09 -07007180
7181 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7182 if (!desc_buf)
7183 return;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007184
Bean Huoc4607a02020-06-03 11:19:56 +02007185 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0,
7186 desc_buf, buff_len);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007187 if (ret) {
7188 dev_err(hba->dev,
7189 "%s: Failed reading power descriptor.len = %d ret = %d",
7190 __func__, buff_len, ret);
Kees Cookbbe21d72018-05-02 16:58:09 -07007191 goto out;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007192 }
7193
Can Guoe89860f2020-03-26 02:25:41 -07007194 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf,
7195 buff_len);
7196 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007197
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02007198 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Can Guoe89860f2020-03-26 02:25:41 -07007199 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007200
7201 if (ret)
7202 dev_err(hba->dev,
7203 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
Can Guoe89860f2020-03-26 02:25:41 -07007204 __func__, icc_level, ret);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007205
Kees Cookbbe21d72018-05-02 16:58:09 -07007206out:
7207 kfree(desc_buf);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007208}
7209
Can Guofb276f72020-03-25 18:09:59 -07007210static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
7211{
7212 scsi_autopm_get_device(sdev);
7213 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev);
7214 if (sdev->rpm_autosuspend)
7215 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev,
7216 RPM_AUTOSUSPEND_DELAY_MS);
7217 scsi_autopm_put_device(sdev);
7218}
7219
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007220/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007221 * ufshcd_scsi_add_wlus - Adds required W-LUs
7222 * @hba: per-adapter instance
7223 *
7224 * UFS device specification requires the UFS devices to support 4 well known
7225 * logical units:
7226 * "REPORT_LUNS" (address: 01h)
7227 * "UFS Device" (address: 50h)
7228 * "RPMB" (address: 44h)
7229 * "BOOT" (address: 30h)
7230 * UFS device's power management needs to be controlled by "POWER CONDITION"
7231 * field of SSU (START STOP UNIT) command. But this "power condition" field
7232 * will take effect only when its sent to "UFS device" well known logical unit
7233 * hence we require the scsi_device instance to represent this logical unit in
7234 * order for the UFS host driver to send the SSU command for power management.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08007235 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007236 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
7237 * Block) LU so user space process can control this LU. User space may also
7238 * want to have access to BOOT LU.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08007239 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007240 * This function adds scsi device instances for each of all well known LUs
7241 * (except "REPORT LUNS" LU).
7242 *
7243 * Returns zero on success (all required W-LUs are added successfully),
7244 * non-zero error value on failure (if failed to add any of the required W-LU).
7245 */
7246static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
7247{
7248 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007249 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007250
7251 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
7252 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
7253 if (IS_ERR(hba->sdev_ufs_device)) {
7254 ret = PTR_ERR(hba->sdev_ufs_device);
7255 hba->sdev_ufs_device = NULL;
7256 goto out;
7257 }
Can Guofb276f72020-03-25 18:09:59 -07007258 ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007259 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007260
Jaegeuk Kim4f3e9002020-11-17 08:58:35 -08007261 hba->sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007262 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Jaegeuk Kim4f3e9002020-11-17 08:58:35 -08007263 if (IS_ERR(hba->sdev_rpmb)) {
7264 ret = PTR_ERR(hba->sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08007265 goto remove_sdev_ufs_device;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007266 }
Jaegeuk Kim4f3e9002020-11-17 08:58:35 -08007267 ufshcd_blk_pm_runtime_init(hba->sdev_rpmb);
7268 scsi_device_put(hba->sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08007269
7270 sdev_boot = __scsi_add_device(hba->host, 0, 0,
7271 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Can Guofb276f72020-03-25 18:09:59 -07007272 if (IS_ERR(sdev_boot)) {
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08007273 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
Can Guofb276f72020-03-25 18:09:59 -07007274 } else {
7275 ufshcd_blk_pm_runtime_init(sdev_boot);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08007276 scsi_device_put(sdev_boot);
Can Guofb276f72020-03-25 18:09:59 -07007277 }
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007278 goto out;
7279
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007280remove_sdev_ufs_device:
7281 scsi_remove_device(hba->sdev_ufs_device);
7282out:
7283 return ret;
7284}
7285
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007286static void ufshcd_wb_probe(struct ufs_hba *hba, u8 *desc_buf)
7287{
Stanley Chua7f1e692020-06-25 11:04:30 +08007288 struct ufs_dev_info *dev_info = &hba->dev_info;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007289 u8 lun;
7290 u32 d_lu_wb_buf_alloc;
Bean Huoe8d03812021-01-19 17:38:45 +01007291 u32 ext_ufs_feature;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007292
Stanley Chu817d7e12020-05-08 16:01:08 +08007293 if (!ufshcd_is_wb_allowed(hba))
7294 return;
Stanley Chua7f1e692020-06-25 11:04:30 +08007295 /*
7296 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or
7297 * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES
7298 * enabled
7299 */
7300 if (!(dev_info->wspecversion >= 0x310 ||
7301 dev_info->wspecversion == 0x220 ||
7302 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES)))
7303 goto wb_disabled;
Stanley Chu817d7e12020-05-08 16:01:08 +08007304
Bean Huo7a0bf852020-06-03 11:19:58 +02007305 if (hba->desc_size[QUERY_DESC_IDN_DEVICE] <
7306 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP + 4)
Stanley Chu817d7e12020-05-08 16:01:08 +08007307 goto wb_disabled;
7308
Bean Huoe8d03812021-01-19 17:38:45 +01007309 ext_ufs_feature = get_unaligned_be32(desc_buf +
7310 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
Stanley Chu817d7e12020-05-08 16:01:08 +08007311
Bean Huoe8d03812021-01-19 17:38:45 +01007312 if (!(ext_ufs_feature & UFS_DEV_WRITE_BOOSTER_SUP))
Stanley Chu817d7e12020-05-08 16:01:08 +08007313 goto wb_disabled;
7314
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007315 /*
Bean Huoae1ce1f2021-01-19 17:38:44 +01007316 * WB may be supported but not configured while provisioning. The spec
7317 * says, in dedicated wb buffer mode, a max of 1 lun would have wb
7318 * buffer configured.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007319 */
Bean Huo4cd48992021-01-19 17:38:46 +01007320 dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE];
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007321
Stanley Chua7f1e692020-06-25 11:04:30 +08007322 dev_info->b_presrv_uspc_en =
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007323 desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN];
7324
Bean Huo4cd48992021-01-19 17:38:46 +01007325 if (dev_info->wb_buffer_type == WB_BUF_MODE_SHARED) {
Bean Huoe8d03812021-01-19 17:38:45 +01007326 if (!get_unaligned_be32(desc_buf +
7327 DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS))
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007328 goto wb_disabled;
7329 } else {
7330 for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) {
7331 d_lu_wb_buf_alloc = 0;
7332 ufshcd_read_unit_desc_param(hba,
7333 lun,
7334 UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS,
7335 (u8 *)&d_lu_wb_buf_alloc,
7336 sizeof(d_lu_wb_buf_alloc));
7337 if (d_lu_wb_buf_alloc) {
Stanley Chua7f1e692020-06-25 11:04:30 +08007338 dev_info->wb_dedicated_lu = lun;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007339 break;
7340 }
7341 }
Stanley Chu817d7e12020-05-08 16:01:08 +08007342
Stanley Chu6f8d5a62020-05-08 16:01:13 +08007343 if (!d_lu_wb_buf_alloc)
7344 goto wb_disabled;
7345 }
Stanley Chu817d7e12020-05-08 16:01:08 +08007346 return;
7347
7348wb_disabled:
7349 hba->caps &= ~UFSHCD_CAP_WB_EN;
7350}
7351
Stanley Chu8db269a2020-05-08 16:01:10 +08007352void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups)
Stanley Chu817d7e12020-05-08 16:01:08 +08007353{
7354 struct ufs_dev_fix *f;
7355 struct ufs_dev_info *dev_info = &hba->dev_info;
7356
Stanley Chu8db269a2020-05-08 16:01:10 +08007357 if (!fixups)
7358 return;
7359
7360 for (f = fixups; f->quirk; f++) {
Stanley Chu817d7e12020-05-08 16:01:08 +08007361 if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
7362 f->wmanufacturerid == UFS_ANY_VENDOR) &&
7363 ((dev_info->model &&
7364 STR_PRFX_EQUAL(f->model, dev_info->model)) ||
7365 !strcmp(f->model, UFS_ANY_MODEL)))
7366 hba->dev_quirks |= f->quirk;
7367 }
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007368}
Stanley Chu8db269a2020-05-08 16:01:10 +08007369EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007370
Stanley Chuc28c00b2020-05-08 16:01:09 +08007371static void ufs_fixup_device_setup(struct ufs_hba *hba)
7372{
7373 /* fix by general quirk table */
Stanley Chu8db269a2020-05-08 16:01:10 +08007374 ufshcd_fixup_dev_quirks(hba, ufs_fixups);
Stanley Chuc28c00b2020-05-08 16:01:09 +08007375
7376 /* allow vendors to fix quirks */
7377 ufshcd_vops_fixup_dev_quirks(hba);
7378}
7379
Bean Huo09750062020-01-20 14:08:14 +01007380static int ufs_get_device_desc(struct ufs_hba *hba)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007381{
7382 int err;
7383 u8 model_index;
Kees Cookbbe21d72018-05-02 16:58:09 -07007384 u8 *desc_buf;
Bean Huo09750062020-01-20 14:08:14 +01007385 struct ufs_dev_info *dev_info = &hba->dev_info;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007386
Bean Huo458a45f2020-06-03 11:19:55 +02007387 desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
Kees Cookbbe21d72018-05-02 16:58:09 -07007388 if (!desc_buf) {
7389 err = -ENOMEM;
7390 goto out;
7391 }
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007392
Bean Huoc4607a02020-06-03 11:19:56 +02007393 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
Bean Huo7a0bf852020-06-03 11:19:58 +02007394 hba->desc_size[QUERY_DESC_IDN_DEVICE]);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007395 if (err) {
7396 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
7397 __func__, err);
7398 goto out;
7399 }
7400
7401 /*
7402 * getting vendor (manufacturerID) and Bank Index in big endian
7403 * format
7404 */
Bean Huo09750062020-01-20 14:08:14 +01007405 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007406 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
7407
Can Guo09f17792020-02-10 19:40:49 -08007408 /* getting Specification Version in big endian format */
7409 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
7410 desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
7411
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007412 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007413
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007414 err = ufshcd_read_string_desc(hba, model_index,
Bean Huo09750062020-01-20 14:08:14 +01007415 &dev_info->model, SD_ASCII_STD);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007416 if (err < 0) {
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007417 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
7418 __func__, err);
7419 goto out;
7420 }
7421
Stanley Chu817d7e12020-05-08 16:01:08 +08007422 ufs_fixup_device_setup(hba);
7423
Stanley Chua7f1e692020-06-25 11:04:30 +08007424 ufshcd_wb_probe(hba, desc_buf);
Stanley Chu817d7e12020-05-08 16:01:08 +08007425
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007426 /*
7427 * ufshcd_read_string_desc returns size of the string
7428 * reset the error value
7429 */
7430 err = 0;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007431
7432out:
Kees Cookbbe21d72018-05-02 16:58:09 -07007433 kfree(desc_buf);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007434 return err;
7435}
7436
Bean Huo09750062020-01-20 14:08:14 +01007437static void ufs_put_device_desc(struct ufs_hba *hba)
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007438{
Bean Huo09750062020-01-20 14:08:14 +01007439 struct ufs_dev_info *dev_info = &hba->dev_info;
7440
7441 kfree(dev_info->model);
7442 dev_info->model = NULL;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007443}
7444
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007445/**
Yaniv Gardi37113102016-03-10 17:37:16 +02007446 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
7447 * @hba: per-adapter instance
7448 *
7449 * PA_TActivate parameter can be tuned manually if UniPro version is less than
7450 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
7451 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
7452 * the hibern8 exit latency.
7453 *
7454 * Returns zero on success, non-zero error value on failure.
7455 */
7456static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
7457{
7458 int ret = 0;
7459 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
7460
7461 ret = ufshcd_dme_peer_get(hba,
7462 UIC_ARG_MIB_SEL(
7463 RX_MIN_ACTIVATETIME_CAPABILITY,
7464 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7465 &peer_rx_min_activatetime);
7466 if (ret)
7467 goto out;
7468
7469 /* make sure proper unit conversion is applied */
7470 tuned_pa_tactivate =
7471 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
7472 / PA_TACTIVATE_TIME_UNIT_US);
7473 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7474 tuned_pa_tactivate);
7475
7476out:
7477 return ret;
7478}
7479
7480/**
7481 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
7482 * @hba: per-adapter instance
7483 *
7484 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
7485 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
7486 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
7487 * This optimal value can help reduce the hibern8 exit latency.
7488 *
7489 * Returns zero on success, non-zero error value on failure.
7490 */
7491static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
7492{
7493 int ret = 0;
7494 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
7495 u32 max_hibern8_time, tuned_pa_hibern8time;
7496
7497 ret = ufshcd_dme_get(hba,
7498 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
7499 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
7500 &local_tx_hibern8_time_cap);
7501 if (ret)
7502 goto out;
7503
7504 ret = ufshcd_dme_peer_get(hba,
7505 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
7506 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7507 &peer_rx_hibern8_time_cap);
7508 if (ret)
7509 goto out;
7510
7511 max_hibern8_time = max(local_tx_hibern8_time_cap,
7512 peer_rx_hibern8_time_cap);
7513 /* make sure proper unit conversion is applied */
7514 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
7515 / PA_HIBERN8_TIME_UNIT_US);
7516 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
7517 tuned_pa_hibern8time);
7518out:
7519 return ret;
7520}
7521
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007522/**
7523 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
7524 * less than device PA_TACTIVATE time.
7525 * @hba: per-adapter instance
7526 *
7527 * Some UFS devices require host PA_TACTIVATE to be lower than device
7528 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
7529 * for such devices.
7530 *
7531 * Returns zero on success, non-zero error value on failure.
7532 */
7533static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
7534{
7535 int ret = 0;
7536 u32 granularity, peer_granularity;
7537 u32 pa_tactivate, peer_pa_tactivate;
7538 u32 pa_tactivate_us, peer_pa_tactivate_us;
7539 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
7540
7541 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7542 &granularity);
7543 if (ret)
7544 goto out;
7545
7546 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7547 &peer_granularity);
7548 if (ret)
7549 goto out;
7550
7551 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
7552 (granularity > PA_GRANULARITY_MAX_VAL)) {
7553 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
7554 __func__, granularity);
7555 return -EINVAL;
7556 }
7557
7558 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
7559 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
7560 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
7561 __func__, peer_granularity);
7562 return -EINVAL;
7563 }
7564
7565 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
7566 if (ret)
7567 goto out;
7568
7569 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
7570 &peer_pa_tactivate);
7571 if (ret)
7572 goto out;
7573
7574 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
7575 peer_pa_tactivate_us = peer_pa_tactivate *
7576 gran_to_us_table[peer_granularity - 1];
7577
7578 if (pa_tactivate_us > peer_pa_tactivate_us) {
7579 u32 new_peer_pa_tactivate;
7580
7581 new_peer_pa_tactivate = pa_tactivate_us /
7582 gran_to_us_table[peer_granularity - 1];
7583 new_peer_pa_tactivate++;
7584 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7585 new_peer_pa_tactivate);
7586 }
7587
7588out:
7589 return ret;
7590}
7591
Bean Huo09750062020-01-20 14:08:14 +01007592static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
Yaniv Gardi37113102016-03-10 17:37:16 +02007593{
7594 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
7595 ufshcd_tune_pa_tactivate(hba);
7596 ufshcd_tune_pa_hibern8time(hba);
7597 }
7598
Can Guoe91ed9e2020-02-23 20:09:21 -08007599 ufshcd_vops_apply_dev_quirks(hba);
7600
Yaniv Gardi37113102016-03-10 17:37:16 +02007601 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
7602 /* set 1ms timeout for PA_TACTIVATE */
7603 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007604
7605 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
7606 ufshcd_quirk_tune_host_pa_tactivate(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02007607}
7608
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007609static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
7610{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007611 hba->ufs_stats.hibern8_exit_cnt = 0;
7612 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08007613 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007614}
7615
Bean Huo731f0622020-01-20 14:08:19 +01007616static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
7617{
7618 int err;
7619 size_t buff_len;
7620 u8 *desc_buf;
7621
Bean Huo7a0bf852020-06-03 11:19:58 +02007622 buff_len = hba->desc_size[QUERY_DESC_IDN_GEOMETRY];
Bean Huo731f0622020-01-20 14:08:19 +01007623 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7624 if (!desc_buf) {
7625 err = -ENOMEM;
7626 goto out;
7627 }
7628
Bean Huoc4607a02020-06-03 11:19:56 +02007629 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
7630 desc_buf, buff_len);
Bean Huo731f0622020-01-20 14:08:19 +01007631 if (err) {
7632 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
7633 __func__, err);
7634 goto out;
7635 }
7636
7637 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
7638 hba->dev_info.max_lu_supported = 32;
7639 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
7640 hba->dev_info.max_lu_supported = 8;
7641
7642out:
7643 kfree(desc_buf);
7644 return err;
7645}
7646
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307647static struct ufs_ref_clk ufs_ref_clk_freqs[] = {
7648 {19200000, REF_CLK_FREQ_19_2_MHZ},
7649 {26000000, REF_CLK_FREQ_26_MHZ},
7650 {38400000, REF_CLK_FREQ_38_4_MHZ},
7651 {52000000, REF_CLK_FREQ_52_MHZ},
7652 {0, REF_CLK_FREQ_INVAL},
7653};
7654
7655static enum ufs_ref_clk_freq
7656ufs_get_bref_clk_from_hz(unsigned long freq)
7657{
7658 int i;
7659
7660 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
7661 if (ufs_ref_clk_freqs[i].freq_hz == freq)
7662 return ufs_ref_clk_freqs[i].val;
7663
7664 return REF_CLK_FREQ_INVAL;
7665}
7666
7667void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
7668{
7669 unsigned long freq;
7670
7671 freq = clk_get_rate(refclk);
7672
7673 hba->dev_ref_clk_freq =
7674 ufs_get_bref_clk_from_hz(freq);
7675
7676 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
7677 dev_err(hba->dev,
7678 "invalid ref_clk setting = %ld\n", freq);
7679}
7680
7681static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
7682{
7683 int err;
7684 u32 ref_clk;
7685 u32 freq = hba->dev_ref_clk_freq;
7686
7687 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
7688 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
7689
7690 if (err) {
7691 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
7692 err);
7693 goto out;
7694 }
7695
7696 if (ref_clk == freq)
7697 goto out; /* nothing to update */
7698
7699 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7700 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
7701
7702 if (err) {
7703 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
7704 ufs_ref_clk_freqs[freq].freq_hz);
7705 goto out;
7706 }
7707
7708 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
7709 ufs_ref_clk_freqs[freq].freq_hz);
7710
7711out:
7712 return err;
7713}
7714
Bean Huo1b9e2142020-01-20 14:08:15 +01007715static int ufshcd_device_params_init(struct ufs_hba *hba)
7716{
7717 bool flag;
Bean Huo7a0bf852020-06-03 11:19:58 +02007718 int ret, i;
Bean Huo1b9e2142020-01-20 14:08:15 +01007719
Bean Huo7a0bf852020-06-03 11:19:58 +02007720 /* Init device descriptor sizes */
7721 for (i = 0; i < QUERY_DESC_IDN_MAX; i++)
7722 hba->desc_size[i] = QUERY_DESC_MAX_SIZE;
Bean Huo1b9e2142020-01-20 14:08:15 +01007723
Bean Huo731f0622020-01-20 14:08:19 +01007724 /* Init UFS geometry descriptor related parameters */
7725 ret = ufshcd_device_geo_params_init(hba);
7726 if (ret)
7727 goto out;
7728
Bean Huo1b9e2142020-01-20 14:08:15 +01007729 /* Check and apply UFS device quirks */
7730 ret = ufs_get_device_desc(hba);
7731 if (ret) {
7732 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
7733 __func__, ret);
7734 goto out;
7735 }
7736
Can Guo09f17792020-02-10 19:40:49 -08007737 ufshcd_get_ref_clk_gating_wait(hba);
7738
Bean Huo1b9e2142020-01-20 14:08:15 +01007739 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08007740 QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag))
Bean Huo1b9e2142020-01-20 14:08:15 +01007741 hba->dev_info.f_power_on_wp_en = flag;
7742
Bean Huo2b35b2a2020-01-20 14:08:16 +01007743 /* Probe maximum power mode co-supported by both UFS host and device */
7744 if (ufshcd_get_max_pwr_mode(hba))
7745 dev_err(hba->dev,
7746 "%s: Failed getting max supported power mode\n",
7747 __func__);
Bean Huo1b9e2142020-01-20 14:08:15 +01007748out:
7749 return ret;
7750}
7751
7752/**
7753 * ufshcd_add_lus - probe and add UFS logical units
7754 * @hba: per-adapter instance
7755 */
7756static int ufshcd_add_lus(struct ufs_hba *hba)
7757{
7758 int ret;
7759
Bean Huo1b9e2142020-01-20 14:08:15 +01007760 /* Add required well known logical units to scsi mid layer */
7761 ret = ufshcd_scsi_add_wlus(hba);
7762 if (ret)
7763 goto out;
7764
7765 /* Initialize devfreq after UFS device is detected */
7766 if (ufshcd_is_clkscaling_supported(hba)) {
7767 memcpy(&hba->clk_scaling.saved_pwr_info.info,
7768 &hba->pwr_info,
7769 sizeof(struct ufs_pa_layer_attr));
7770 hba->clk_scaling.saved_pwr_info.is_valid = true;
Stanley Chub058fa82021-01-20 23:01:41 +08007771 hba->clk_scaling.is_allowed = true;
Bean Huo1b9e2142020-01-20 14:08:15 +01007772
Stanley Chub058fa82021-01-20 23:01:41 +08007773 ret = ufshcd_devfreq_init(hba);
7774 if (ret)
7775 goto out;
7776
7777 hba->clk_scaling.is_enabled = true;
7778 ufshcd_init_clk_scaling_sysfs(hba);
Bean Huo1b9e2142020-01-20 14:08:15 +01007779 }
7780
7781 ufs_bsg_probe(hba);
7782 scsi_scan_host(hba->host);
7783 pm_runtime_put_sync(hba->dev);
7784
Bean Huo1b9e2142020-01-20 14:08:15 +01007785out:
7786 return ret;
7787}
7788
Jaegeuk Kim4f3e9002020-11-17 08:58:35 -08007789static int
7790ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp);
7791
7792static int ufshcd_clear_ua_wlun(struct ufs_hba *hba, u8 wlun)
7793{
7794 struct scsi_device *sdp;
7795 unsigned long flags;
7796 int ret = 0;
7797
7798 spin_lock_irqsave(hba->host->host_lock, flags);
7799 if (wlun == UFS_UPIU_UFS_DEVICE_WLUN)
7800 sdp = hba->sdev_ufs_device;
7801 else if (wlun == UFS_UPIU_RPMB_WLUN)
7802 sdp = hba->sdev_rpmb;
7803 else
Arnd Bergmann4c602442020-12-03 23:31:26 +01007804 BUG();
Jaegeuk Kim4f3e9002020-11-17 08:58:35 -08007805 if (sdp) {
7806 ret = scsi_device_get(sdp);
7807 if (!ret && !scsi_device_online(sdp)) {
7808 ret = -ENODEV;
7809 scsi_device_put(sdp);
7810 }
7811 } else {
7812 ret = -ENODEV;
7813 }
7814 spin_unlock_irqrestore(hba->host->host_lock, flags);
7815 if (ret)
7816 goto out_err;
7817
7818 ret = ufshcd_send_request_sense(hba, sdp);
7819 scsi_device_put(sdp);
7820out_err:
7821 if (ret)
7822 dev_err(hba->dev, "%s: UAC clear LU=%x ret = %d\n",
7823 __func__, wlun, ret);
7824 return ret;
7825}
7826
7827static int ufshcd_clear_ua_wluns(struct ufs_hba *hba)
7828{
7829 int ret = 0;
7830
7831 if (!hba->wlun_dev_clr_ua)
7832 goto out;
7833
7834 ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_UFS_DEVICE_WLUN);
7835 if (!ret)
7836 ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_RPMB_WLUN);
7837 if (!ret)
7838 hba->wlun_dev_clr_ua = false;
7839out:
7840 if (ret)
7841 dev_err(hba->dev, "%s: Failed to clear UAC WLUNS ret = %d\n",
7842 __func__, ret);
7843 return ret;
7844}
7845
Yaniv Gardi37113102016-03-10 17:37:16 +02007846/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007847 * ufshcd_probe_hba - probe hba to detect device and initialize
7848 * @hba: per-adapter instance
Bean Huo1b9e2142020-01-20 14:08:15 +01007849 * @async: asynchronous execution or not
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007850 *
7851 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307852 */
Bean Huo1b9e2142020-01-20 14:08:15 +01007853static int ufshcd_probe_hba(struct ufs_hba *hba, bool async)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307854{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307855 int ret;
Can Guo4db7a232020-08-09 05:15:51 -07007856 unsigned long flags;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007857 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307858
7859 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307860 if (ret)
7861 goto out;
7862
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007863 /* Debug counters initialization */
7864 ufshcd_clear_dbg_ufs_stats(hba);
7865
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007866 /* UniPro link is active now */
7867 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05307868
Bean Huo1b9e2142020-01-20 14:08:15 +01007869 /* Verify device initialization by sending NOP OUT UPIU */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307870 ret = ufshcd_verify_dev_init(hba);
7871 if (ret)
7872 goto out;
7873
Bean Huo1b9e2142020-01-20 14:08:15 +01007874 /* Initiate UFS initialization, and waiting until completion */
Dolev Raviv68078d52013-07-30 00:35:58 +05307875 ret = ufshcd_complete_dev_init(hba);
7876 if (ret)
7877 goto out;
7878
Bean Huo1b9e2142020-01-20 14:08:15 +01007879 /*
7880 * Initialize UFS device parameters used by driver, these
7881 * parameters are associated with UFS descriptors.
7882 */
7883 if (async) {
7884 ret = ufshcd_device_params_init(hba);
7885 if (ret)
7886 goto out;
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02007887 }
7888
Bean Huo09750062020-01-20 14:08:14 +01007889 ufshcd_tune_unipro_params(hba);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007890
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007891 /* UFS device is also active now */
7892 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307893 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007894 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307895
Bean Huo2b35b2a2020-01-20 14:08:16 +01007896 /* Gear up to HS gear if supported */
7897 if (hba->max_pwr_info.is_valid) {
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307898 /*
7899 * Set the right value to bRefClkFreq before attempting to
7900 * switch to HS gears.
7901 */
7902 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
7903 ufshcd_set_dev_ref_clk(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007904 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007905 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007906 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7907 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007908 goto out;
7909 }
Can Guo6a9df812020-02-11 21:38:28 -08007910 ufshcd_print_pwr_info(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007911 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007912
Can Guoe89860f2020-03-26 02:25:41 -07007913 /*
7914 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
7915 * and for removable UFS card as well, hence always set the parameter.
7916 * Note: Error handler may issue the device reset hence resetting
7917 * bActiveICCLevel as well so it is always safe to set this here.
7918 */
7919 ufshcd_set_active_icc_lvl(hba);
7920
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007921 ufshcd_wb_config(hba);
Can Guo71d848b2019-11-14 22:09:26 -08007922 /* Enable Auto-Hibernate if configured */
7923 ufshcd_auto_hibern8_enable(hba);
7924
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307925out:
Can Guo4db7a232020-08-09 05:15:51 -07007926 spin_lock_irqsave(hba->host->host_lock, flags);
7927 if (ret)
7928 hba->ufshcd_state = UFSHCD_STATE_ERROR;
7929 else if (hba->ufshcd_state == UFSHCD_STATE_RESET)
7930 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
7931 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007932
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007933 trace_ufshcd_init(dev_name(hba->dev), ret,
7934 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007935 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007936 return ret;
7937}
7938
7939/**
7940 * ufshcd_async_scan - asynchronous execution for probing hba
7941 * @data: data pointer to pass to this function
7942 * @cookie: cookie data
7943 */
7944static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7945{
7946 struct ufs_hba *hba = (struct ufs_hba *)data;
Bean Huo1b9e2142020-01-20 14:08:15 +01007947 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007948
Can Guo9cd20d32021-01-13 19:13:28 -08007949 down(&hba->host_sem);
Bean Huo1b9e2142020-01-20 14:08:15 +01007950 /* Initialize hba, detect and initialize UFS device */
7951 ret = ufshcd_probe_hba(hba, true);
Can Guo9cd20d32021-01-13 19:13:28 -08007952 up(&hba->host_sem);
Bean Huo1b9e2142020-01-20 14:08:15 +01007953 if (ret)
7954 goto out;
7955
7956 /* Probe and add UFS logical units */
7957 ret = ufshcd_add_lus(hba);
7958out:
7959 /*
7960 * If we failed to initialize the device or the device is not
7961 * present, turn off the power/clocks etc.
7962 */
7963 if (ret) {
7964 pm_runtime_put_sync(hba->dev);
Bean Huo1b9e2142020-01-20 14:08:15 +01007965 ufshcd_hba_exit(hba);
Jaegeuk Kim4f3e9002020-11-17 08:58:35 -08007966 } else {
7967 ufshcd_clear_ua_wluns(hba);
Bean Huo1b9e2142020-01-20 14:08:15 +01007968 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307969}
7970
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007971static const struct attribute_group *ufshcd_driver_groups[] = {
7972 &ufs_sysfs_unit_descriptor_group,
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02007973 &ufs_sysfs_lun_attributes_group,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007974 NULL,
7975};
7976
Stanley Chu90b84912020-05-09 17:37:13 +08007977static struct ufs_hba_variant_params ufs_hba_vps = {
7978 .hba_enable_delay_us = 1000,
Stanley Chud14734ae2020-05-09 17:37:15 +08007979 .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40),
Stanley Chu90b84912020-05-09 17:37:13 +08007980 .devfreq_profile.polling_ms = 100,
7981 .devfreq_profile.target = ufshcd_devfreq_target,
7982 .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status,
7983 .ondemand_data.upthreshold = 70,
7984 .ondemand_data.downdifferential = 5,
7985};
7986
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307987static struct scsi_host_template ufshcd_driver_template = {
7988 .module = THIS_MODULE,
7989 .name = UFSHCD,
7990 .proc_name = UFSHCD,
7991 .queuecommand = ufshcd_queuecommand,
7992 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09007993 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307994 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03007995 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307996 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307997 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7998 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307999 .this_id = -1,
8000 .sg_tablesize = SG_ALL,
8001 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
8002 .can_queue = UFSHCD_CAN_QUEUE,
Christoph Hellwig552a9902019-06-17 14:19:55 +02008003 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008004 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01008005 .track_queue_depth = 1,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02008006 .sdev_groups = ufshcd_driver_groups,
Christoph Hellwig4af14d12018-12-13 16:17:09 +01008007 .dma_boundary = PAGE_SIZE - 1,
Stanley Chu49615ba2019-09-16 23:56:50 +08008008 .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308009};
8010
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008011static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
8012 int ua)
8013{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08008014 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008015
Bjorn Andersson7b16a072015-02-11 19:35:28 -08008016 if (!vreg)
8017 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008018
Stanley Chu0487fff2019-03-28 17:16:25 +08008019 /*
8020 * "set_load" operation shall be required on those regulators
8021 * which specifically configured current limitation. Otherwise
8022 * zero max_uA may cause unexpected behavior when regulator is
8023 * enabled or set as high power mode.
8024 */
8025 if (!vreg->max_uA)
8026 return 0;
8027
Bjorn Andersson7b16a072015-02-11 19:35:28 -08008028 ret = regulator_set_load(vreg->reg, ua);
8029 if (ret < 0) {
8030 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
8031 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008032 }
8033
8034 return ret;
8035}
8036
8037static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
8038 struct ufs_vreg *vreg)
8039{
Marc Gonzalez73067982019-02-27 11:41:45 +01008040 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008041}
8042
8043static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
8044 struct ufs_vreg *vreg)
8045{
Adrian Hunter7c7cfdc2019-08-14 15:59:50 +03008046 if (!vreg)
8047 return 0;
8048
Marc Gonzalez73067982019-02-27 11:41:45 +01008049 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008050}
8051
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008052static int ufshcd_config_vreg(struct device *dev,
8053 struct ufs_vreg *vreg, bool on)
8054{
8055 int ret = 0;
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06008056 struct regulator *reg;
8057 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008058 int min_uV, uA_load;
8059
8060 BUG_ON(!vreg);
8061
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06008062 reg = vreg->reg;
8063 name = vreg->name;
8064
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008065 if (regulator_count_voltages(reg) > 0) {
Asutosh Das90d88f42020-02-10 19:40:45 -08008066 uA_load = on ? vreg->max_uA : 0;
8067 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
8068 if (ret)
8069 goto out;
8070
Stanley Chu3b141e82019-03-28 17:16:24 +08008071 if (vreg->min_uV && vreg->max_uV) {
8072 min_uV = on ? vreg->min_uV : 0;
8073 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
Bean Huob0008622020-08-14 11:50:34 +02008074 if (ret)
Stanley Chu3b141e82019-03-28 17:16:24 +08008075 dev_err(dev,
8076 "%s: %s set voltage failed, err=%d\n",
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008077 __func__, name, ret);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008078 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008079 }
8080out:
8081 return ret;
8082}
8083
8084static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
8085{
8086 int ret = 0;
8087
Marc Gonzalez73067982019-02-27 11:41:45 +01008088 if (!vreg || vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008089 goto out;
8090
8091 ret = ufshcd_config_vreg(dev, vreg, true);
8092 if (!ret)
8093 ret = regulator_enable(vreg->reg);
8094
8095 if (!ret)
8096 vreg->enabled = true;
8097 else
8098 dev_err(dev, "%s: %s enable failed, err=%d\n",
8099 __func__, vreg->name, ret);
8100out:
8101 return ret;
8102}
8103
8104static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
8105{
8106 int ret = 0;
8107
Stanley Chuf8162ac2020-12-07 13:49:54 +08008108 if (!vreg || !vreg->enabled || vreg->always_on)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008109 goto out;
8110
8111 ret = regulator_disable(vreg->reg);
8112
8113 if (!ret) {
8114 /* ignore errors on applying disable config */
8115 ufshcd_config_vreg(dev, vreg, false);
8116 vreg->enabled = false;
8117 } else {
8118 dev_err(dev, "%s: %s disable failed, err=%d\n",
8119 __func__, vreg->name, ret);
8120 }
8121out:
8122 return ret;
8123}
8124
8125static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
8126{
8127 int ret = 0;
8128 struct device *dev = hba->dev;
8129 struct ufs_vreg_info *info = &hba->vreg_info;
8130
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008131 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
8132 if (ret)
8133 goto out;
8134
8135 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
8136 if (ret)
8137 goto out;
8138
8139 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008140
8141out:
8142 if (ret) {
8143 ufshcd_toggle_vreg(dev, info->vccq2, false);
8144 ufshcd_toggle_vreg(dev, info->vccq, false);
8145 ufshcd_toggle_vreg(dev, info->vcc, false);
8146 }
8147 return ret;
8148}
8149
Raviv Shvili6a771a62014-09-25 15:32:24 +03008150static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
8151{
8152 struct ufs_vreg_info *info = &hba->vreg_info;
8153
Zeng Guangyue60b7b822019-03-30 17:03:13 +08008154 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03008155}
8156
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008157static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
8158{
8159 int ret = 0;
8160
8161 if (!vreg)
8162 goto out;
8163
8164 vreg->reg = devm_regulator_get(dev, vreg->name);
8165 if (IS_ERR(vreg->reg)) {
8166 ret = PTR_ERR(vreg->reg);
8167 dev_err(dev, "%s: %s get failed, err=%d\n",
8168 __func__, vreg->name, ret);
8169 }
8170out:
8171 return ret;
8172}
8173
8174static int ufshcd_init_vreg(struct ufs_hba *hba)
8175{
8176 int ret = 0;
8177 struct device *dev = hba->dev;
8178 struct ufs_vreg_info *info = &hba->vreg_info;
8179
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008180 ret = ufshcd_get_vreg(dev, info->vcc);
8181 if (ret)
8182 goto out;
8183
8184 ret = ufshcd_get_vreg(dev, info->vccq);
Bean Huob0008622020-08-14 11:50:34 +02008185 if (!ret)
8186 ret = ufshcd_get_vreg(dev, info->vccq2);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008187out:
8188 return ret;
8189}
8190
Raviv Shvili6a771a62014-09-25 15:32:24 +03008191static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
8192{
8193 struct ufs_vreg_info *info = &hba->vreg_info;
8194
8195 if (info)
8196 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
8197
8198 return 0;
8199}
8200
Can Guo81309c22020-11-25 18:01:00 -08008201static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008202{
8203 int ret = 0;
8204 struct ufs_clk_info *clki;
8205 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008206 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08008207 ktime_t start = ktime_get();
8208 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008209
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03008210 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008211 goto out;
8212
Can Guo38f32422020-02-10 19:40:47 -08008213 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
8214 if (ret)
8215 return ret;
Subhash Jadavani1e879e82016-10-06 21:48:22 -07008216
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008217 list_for_each_entry(clki, head, list) {
8218 if (!IS_ERR_OR_NULL(clki->clk)) {
Can Guo81309c22020-11-25 18:01:00 -08008219 /*
8220 * Don't disable clocks which are needed
8221 * to keep the link active.
8222 */
8223 if (ufshcd_is_link_active(hba) &&
8224 clki->keep_link_active)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008225 continue;
8226
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08008227 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008228 if (on && !clki->enabled) {
8229 ret = clk_prepare_enable(clki->clk);
8230 if (ret) {
8231 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
8232 __func__, clki->name, ret);
8233 goto out;
8234 }
8235 } else if (!on && clki->enabled) {
8236 clk_disable_unprepare(clki->clk);
8237 }
8238 clki->enabled = on;
8239 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
8240 clki->name, on ? "en" : "dis");
8241 }
8242 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008243
Can Guo38f32422020-02-10 19:40:47 -08008244 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
8245 if (ret)
8246 return ret;
Subhash Jadavani1e879e82016-10-06 21:48:22 -07008247
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008248out:
8249 if (ret) {
8250 list_for_each_entry(clki, head, list) {
8251 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
8252 clk_disable_unprepare(clki->clk);
8253 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008254 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008255 spin_lock_irqsave(hba->host->host_lock, flags);
8256 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008257 trace_ufshcd_clk_gating(dev_name(hba->dev),
8258 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008259 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008260 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008261
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08008262 if (clk_state_changed)
8263 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
8264 (on ? "on" : "off"),
8265 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008266 return ret;
8267}
8268
8269static int ufshcd_init_clocks(struct ufs_hba *hba)
8270{
8271 int ret = 0;
8272 struct ufs_clk_info *clki;
8273 struct device *dev = hba->dev;
8274 struct list_head *head = &hba->clk_list_head;
8275
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03008276 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008277 goto out;
8278
8279 list_for_each_entry(clki, head, list) {
8280 if (!clki->name)
8281 continue;
8282
8283 clki->clk = devm_clk_get(dev, clki->name);
8284 if (IS_ERR(clki->clk)) {
8285 ret = PTR_ERR(clki->clk);
8286 dev_err(dev, "%s: %s clk get failed, %d\n",
8287 __func__, clki->name, ret);
8288 goto out;
8289 }
8290
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05308291 /*
8292 * Parse device ref clk freq as per device tree "ref_clk".
8293 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
8294 * in ufshcd_alloc_host().
8295 */
8296 if (!strcmp(clki->name, "ref_clk"))
8297 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
8298
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008299 if (clki->max_freq) {
8300 ret = clk_set_rate(clki->clk, clki->max_freq);
8301 if (ret) {
8302 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
8303 __func__, clki->name,
8304 clki->max_freq, ret);
8305 goto out;
8306 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03008307 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008308 }
8309 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
8310 clki->name, clk_get_rate(clki->clk));
8311 }
8312out:
8313 return ret;
8314}
8315
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008316static int ufshcd_variant_hba_init(struct ufs_hba *hba)
8317{
8318 int err = 0;
8319
8320 if (!hba->vops)
8321 goto out;
8322
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008323 err = ufshcd_vops_init(hba);
8324 if (err)
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008325 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008326 __func__, ufshcd_get_var_name(hba), err);
Stanley Chuade921a2020-12-05 20:00:38 +08008327out:
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008328 return err;
8329}
8330
8331static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
8332{
8333 if (!hba->vops)
8334 return;
8335
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008336 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008337}
8338
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008339static int ufshcd_hba_init(struct ufs_hba *hba)
8340{
8341 int err;
8342
Raviv Shvili6a771a62014-09-25 15:32:24 +03008343 /*
8344 * Handle host controller power separately from the UFS device power
8345 * rails as it will help controlling the UFS host controller power
8346 * collapse easily which is different than UFS device power collapse.
8347 * Also, enable the host controller power before we go ahead with rest
8348 * of the initialization here.
8349 */
8350 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008351 if (err)
8352 goto out;
8353
Raviv Shvili6a771a62014-09-25 15:32:24 +03008354 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008355 if (err)
8356 goto out;
8357
Raviv Shvili6a771a62014-09-25 15:32:24 +03008358 err = ufshcd_init_clocks(hba);
8359 if (err)
8360 goto out_disable_hba_vreg;
8361
8362 err = ufshcd_setup_clocks(hba, true);
8363 if (err)
8364 goto out_disable_hba_vreg;
8365
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008366 err = ufshcd_init_vreg(hba);
8367 if (err)
8368 goto out_disable_clks;
8369
8370 err = ufshcd_setup_vreg(hba, true);
8371 if (err)
8372 goto out_disable_clks;
8373
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008374 err = ufshcd_variant_hba_init(hba);
8375 if (err)
8376 goto out_disable_vreg;
8377
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02008378 ufs_debugfs_hba_init(hba);
8379
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008380 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008381 goto out;
8382
8383out_disable_vreg:
8384 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03008385out_disable_clks:
8386 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03008387out_disable_hba_vreg:
8388 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008389out:
8390 return err;
8391}
8392
8393static void ufshcd_hba_exit(struct ufs_hba *hba)
8394{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008395 if (hba->is_powered) {
Can Guo4543d9d2021-01-20 02:04:22 -08008396 ufshcd_exit_clk_scaling(hba);
8397 ufshcd_exit_clk_gating(hba);
8398 if (hba->eh_wq)
8399 destroy_workqueue(hba->eh_wq);
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02008400 ufs_debugfs_hba_exit(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008401 ufshcd_variant_hba_exit(hba);
8402 ufshcd_setup_vreg(hba, false);
8403 ufshcd_setup_clocks(hba, false);
8404 ufshcd_setup_hba_vreg(hba, false);
8405 hba->is_powered = false;
Bean Huo09750062020-01-20 14:08:14 +01008406 ufs_put_device_desc(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03008407 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008408}
8409
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008410static int
8411ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308412{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008413 unsigned char cmd[6] = {REQUEST_SENSE,
8414 0,
8415 0,
8416 0,
Avri Altman09a5a242018-11-22 20:04:56 +02008417 UFS_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008418 0};
8419 char *buffer;
8420 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308421
Avri Altman09a5a242018-11-22 20:04:56 +02008422 buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008423 if (!buffer) {
8424 ret = -ENOMEM;
8425 goto out;
8426 }
8427
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01008428 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
Avri Altman09a5a242018-11-22 20:04:56 +02008429 UFS_SENSE_SIZE, NULL, NULL,
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01008430 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008431 if (ret)
8432 pr_err("%s: failed with err %d\n", __func__, ret);
8433
8434 kfree(buffer);
8435out:
8436 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308437}
8438
8439/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008440 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
8441 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308442 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008443 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308444 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008445 * Returns 0 if requested power mode is set successfully
8446 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308447 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008448static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
8449 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308450{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008451 unsigned char cmd[6] = { START_STOP };
8452 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008453 struct scsi_device *sdp;
8454 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008455 int ret;
8456
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008457 spin_lock_irqsave(hba->host->host_lock, flags);
8458 sdp = hba->sdev_ufs_device;
8459 if (sdp) {
8460 ret = scsi_device_get(sdp);
8461 if (!ret && !scsi_device_online(sdp)) {
8462 ret = -ENODEV;
8463 scsi_device_put(sdp);
8464 }
8465 } else {
8466 ret = -ENODEV;
8467 }
8468 spin_unlock_irqrestore(hba->host->host_lock, flags);
8469
8470 if (ret)
8471 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008472
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308473 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008474 * If scsi commands fail, the scsi mid-layer schedules scsi error-
8475 * handling, which would wait for host to be resumed. Since we know
8476 * we are functional while we are here, skip host resume in error
8477 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308478 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008479 hba->host->eh_noresume = 1;
Randall Huang19186512020-11-30 20:14:02 -08008480 ufshcd_clear_ua_wluns(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308481
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008482 cmd[4] = pwr_mode << 4;
8483
8484 /*
8485 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02008486 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008487 * already suspended childs.
8488 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01008489 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
8490 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008491 if (ret) {
8492 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02008493 "START_STOP failed for power mode: %d, result %x\n",
8494 pwr_mode, ret);
Johannes Thumshirnc65be1a2018-06-25 13:20:58 +02008495 if (driver_byte(ret) == DRIVER_SENSE)
Hannes Reinecke21045512015-01-08 07:43:46 +01008496 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008497 }
8498
8499 if (!ret)
8500 hba->curr_dev_pwr_mode = pwr_mode;
Randall Huang19186512020-11-30 20:14:02 -08008501
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008502 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008503 hba->host->eh_noresume = 0;
8504 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308505}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308506
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008507static int ufshcd_link_state_transition(struct ufs_hba *hba,
8508 enum uic_link_state req_link_state,
8509 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308510{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008511 int ret = 0;
8512
8513 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308514 return 0;
8515
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008516 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
8517 ret = ufshcd_uic_hibern8_enter(hba);
Can Guo4db7a232020-08-09 05:15:51 -07008518 if (!ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008519 ufshcd_set_link_hibern8(hba);
Can Guo4db7a232020-08-09 05:15:51 -07008520 } else {
8521 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
8522 __func__, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008523 goto out;
Can Guo4db7a232020-08-09 05:15:51 -07008524 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008525 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308526 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008527 * If autobkops is enabled, link can't be turned off because
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008528 * turning off the link would also turn off the device, except in the
8529 * case of DeepSleep where the device is expected to remain powered.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308530 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008531 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
Dan Carpenterdc30c9e2019-12-13 13:49:35 +03008532 (!check_for_bkops || !hba->auto_bkops_enabled)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008533 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008534 * Let's make sure that link is in low power mode, we are doing
8535 * this currently by putting the link in Hibern8. Otherway to
8536 * put the link in low power mode is to send the DME end point
8537 * to device and then send the DME reset command to local
8538 * unipro. But putting the link in hibern8 is much faster.
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008539 *
8540 * Note also that putting the link in Hibern8 is a requirement
8541 * for entering DeepSleep.
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008542 */
8543 ret = ufshcd_uic_hibern8_enter(hba);
Can Guo4db7a232020-08-09 05:15:51 -07008544 if (ret) {
8545 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
8546 __func__, ret);
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008547 goto out;
Can Guo4db7a232020-08-09 05:15:51 -07008548 }
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008549 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008550 * Change controller state to "reset state" which
8551 * should also put the link in off/reset state
8552 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07008553 ufshcd_hba_stop(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008554 /*
8555 * TODO: Check if we need any delay to make sure that
8556 * controller is reset
8557 */
8558 ufshcd_set_link_off(hba);
8559 }
8560
8561out:
8562 return ret;
8563}
8564
8565static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
8566{
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008567 bool vcc_off = false;
8568
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008569 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02008570 * It seems some UFS devices may keep drawing more than sleep current
8571 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
8572 * To avoid this situation, add 2ms delay before putting these UFS
8573 * rails in LPM mode.
8574 */
8575 if (!ufshcd_is_link_active(hba) &&
8576 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
8577 usleep_range(2000, 2100);
8578
8579 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008580 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
8581 * power.
8582 *
8583 * If UFS device and link is in OFF state, all power supplies (VCC,
8584 * VCCQ, VCCQ2) can be turned off if power on write protect is not
8585 * required. If UFS link is inactive (Hibern8 or OFF state) and device
8586 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
8587 *
8588 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
8589 * in low power state which would save some power.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008590 *
8591 * If Write Booster is enabled and the device needs to flush the WB
8592 * buffer OR if bkops status is urgent for WB, keep Vcc on.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008593 */
8594 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8595 !hba->dev_info.is_lu_power_on_wp) {
8596 ufshcd_setup_vreg(hba, false);
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008597 vcc_off = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008598 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Stanley Chu51dd9052020-05-22 16:32:12 +08008599 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008600 vcc_off = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008601 if (!ufshcd_is_link_active(hba)) {
8602 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8603 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
8604 }
8605 }
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008606
8607 /*
8608 * Some UFS devices require delay after VCC power rail is turned-off.
8609 */
8610 if (vcc_off && hba->vreg_info.vcc &&
8611 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)
8612 usleep_range(5000, 5100);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008613}
8614
8615static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
8616{
8617 int ret = 0;
8618
8619 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8620 !hba->dev_info.is_lu_power_on_wp) {
8621 ret = ufshcd_setup_vreg(hba, true);
8622 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008623 if (!ret && !ufshcd_is_link_active(hba)) {
8624 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
8625 if (ret)
8626 goto vcc_disable;
8627 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
8628 if (ret)
8629 goto vccq_lpm;
8630 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07008631 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008632 }
8633 goto out;
8634
8635vccq_lpm:
8636 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8637vcc_disable:
8638 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8639out:
8640 return ret;
8641}
8642
8643static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
8644{
Can Guodd7143e2020-10-27 12:10:36 -07008645 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008646 ufshcd_setup_hba_vreg(hba, false);
8647}
8648
8649static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
8650{
Can Guodd7143e2020-10-27 12:10:36 -07008651 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008652 ufshcd_setup_hba_vreg(hba, true);
8653}
8654
8655/**
8656 * ufshcd_suspend - helper function for suspend operations
8657 * @hba: per adapter instance
8658 * @pm_op: desired low power operation type
8659 *
8660 * This function will try to put the UFS device and link into low power
8661 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
8662 * (System PM level).
8663 *
8664 * If this function is called during shutdown, it will make sure that
8665 * both UFS device and UFS link is powered off.
8666 *
8667 * NOTE: UFS device & link must be active before we enter in this function.
8668 *
8669 * Returns 0 for success and non-zero for failure
8670 */
8671static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8672{
8673 int ret = 0;
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008674 int check_for_bkops;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008675 enum ufs_pm_level pm_lvl;
8676 enum ufs_dev_pwr_mode req_dev_pwr_mode;
8677 enum uic_link_state req_link_state;
8678
8679 hba->pm_op_in_progress = 1;
8680 if (!ufshcd_is_shutdown_pm(pm_op)) {
8681 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
8682 hba->rpm_lvl : hba->spm_lvl;
8683 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
8684 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
8685 } else {
8686 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
8687 req_link_state = UIC_LINK_OFF_STATE;
8688 }
8689
8690 /*
8691 * If we can't transition into any of the low power modes
8692 * just gate the clocks.
8693 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008694 ufshcd_hold(hba, false);
8695 hba->clk_gating.is_suspended = true;
8696
Stanley Chuf9a7fa32021-01-20 23:01:40 +08008697 if (hba->clk_scaling.is_enabled)
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008698 ufshcd_suspend_clkscaling(hba);
Stanley Chuf9a7fa32021-01-20 23:01:40 +08008699
Can Guo0e9d4ca2021-01-20 02:04:21 -08008700 down_write(&hba->clk_scaling_lock);
8701 hba->clk_scaling.is_allowed = false;
8702 up_write(&hba->clk_scaling_lock);
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008703
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008704 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8705 req_link_state == UIC_LINK_ACTIVE_STATE) {
8706 goto disable_clks;
8707 }
8708
8709 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8710 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008711 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008712
8713 /* UFS device & link must be active before we enter in this function */
8714 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8715 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008716 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008717 }
8718
8719 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03008720 if (ufshcd_can_autobkops_during_suspend(hba)) {
8721 /*
8722 * The device is idle with no requests in the queue,
8723 * allow background operations if bkops status shows
8724 * that performance might be impacted.
8725 */
8726 ret = ufshcd_urgent_bkops(hba);
8727 if (ret)
8728 goto enable_gating;
8729 } else {
8730 /* make sure that auto bkops is disabled */
8731 ufshcd_disable_auto_bkops(hba);
8732 }
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008733 /*
Stanley Chu51dd9052020-05-22 16:32:12 +08008734 * If device needs to do BKOP or WB buffer flush during
8735 * Hibern8, keep device power mode as "active power mode"
8736 * and VCC supply.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008737 */
Stanley Chu51dd9052020-05-22 16:32:12 +08008738 hba->dev_info.b_rpm_dev_flush_capable =
8739 hba->auto_bkops_enabled ||
8740 (((req_link_state == UIC_LINK_HIBERN8_STATE) ||
8741 ((req_link_state == UIC_LINK_ACTIVE_STATE) &&
8742 ufshcd_is_auto_hibern8_enabled(hba))) &&
8743 ufshcd_wb_need_flush(hba));
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008744 }
8745
Kiwoong Kim6948a962020-12-19 15:40:39 +09008746 flush_work(&hba->eeh_work);
8747
Stanley Chu51dd9052020-05-22 16:32:12 +08008748 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) {
Bean Huo939785d2020-11-25 19:53:00 +01008749 if (!ufshcd_is_runtime_pm(pm_op))
Stanley Chu51dd9052020-05-22 16:32:12 +08008750 /* ensure that bkops is disabled */
8751 ufshcd_disable_auto_bkops(hba);
Stanley Chu51dd9052020-05-22 16:32:12 +08008752
8753 if (!hba->dev_info.b_rpm_dev_flush_capable) {
8754 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8755 if (ret)
8756 goto enable_gating;
8757 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008758 }
8759
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008760 /*
8761 * In the case of DeepSleep, the device is expected to remain powered
8762 * with the link off, so do not check for bkops.
8763 */
8764 check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba);
8765 ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008766 if (ret)
8767 goto set_dev_active;
8768
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008769disable_clks:
8770 /*
8771 * Call vendor specific suspend callback. As these callbacks may access
8772 * vendor specific host controller register space call them before the
8773 * host clocks are ON.
8774 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008775 ret = ufshcd_vops_suspend(hba, pm_op);
8776 if (ret)
8777 goto set_link_active;
Stanley Chudcb6cec2019-12-07 20:22:00 +08008778 /*
8779 * Disable the host irq as host controller as there won't be any
8780 * host controller transaction expected till resume.
8781 */
8782 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008783
Can Guo81309c22020-11-25 18:01:00 -08008784 ufshcd_setup_clocks(hba, false);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008785
Can Guo2dec9472020-08-09 05:15:47 -07008786 if (ufshcd_is_clkgating_allowed(hba)) {
8787 hba->clk_gating.state = CLKS_OFF;
8788 trace_ufshcd_clk_gating(dev_name(hba->dev),
8789 hba->clk_gating.state);
8790 }
Stanley Chudcb6cec2019-12-07 20:22:00 +08008791
Ziqi Chen528db9e2021-01-08 18:56:24 +08008792 ufshcd_vreg_set_lpm(hba);
8793
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008794 /* Put the host controller in low power mode if possible */
8795 ufshcd_hba_vreg_set_lpm(hba);
8796 goto out;
8797
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008798set_link_active:
8799 ufshcd_vreg_set_hpm(hba);
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008800 /*
8801 * Device hardware reset is required to exit DeepSleep. Also, for
8802 * DeepSleep, the link is off so host reset and restore will be done
8803 * further below.
8804 */
8805 if (ufshcd_is_ufs_dev_deepsleep(hba)) {
Stanley Chu31a5d9c2020-12-08 21:56:35 +08008806 ufshcd_device_reset(hba);
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008807 WARN_ON(!ufshcd_is_link_off(hba));
8808 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008809 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
8810 ufshcd_set_link_active(hba);
8811 else if (ufshcd_is_link_off(hba))
8812 ufshcd_host_reset_and_restore(hba);
8813set_dev_active:
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008814 /* Can also get here needing to exit DeepSleep */
8815 if (ufshcd_is_ufs_dev_deepsleep(hba)) {
Stanley Chu31a5d9c2020-12-08 21:56:35 +08008816 ufshcd_device_reset(hba);
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008817 ufshcd_host_reset_and_restore(hba);
8818 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008819 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8820 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008821enable_gating:
Can Guo0e9d4ca2021-01-20 02:04:21 -08008822 down_write(&hba->clk_scaling_lock);
8823 hba->clk_scaling.is_allowed = true;
8824 up_write(&hba->clk_scaling_lock);
8825 if (hba->clk_scaling.is_enabled)
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008826 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008827 hba->clk_gating.is_suspended = false;
Stanley Chu51dd9052020-05-22 16:32:12 +08008828 hba->dev_info.b_rpm_dev_flush_capable = false;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008829 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008830out:
Stanley Chu51dd9052020-05-22 16:32:12 +08008831 if (hba->dev_info.b_rpm_dev_flush_capable) {
8832 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work,
8833 msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS));
8834 }
8835
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008836 hba->pm_op_in_progress = 0;
Stanley Chu51dd9052020-05-22 16:32:12 +08008837
Stanley Chu8808b4e2019-07-10 21:38:21 +08008838 if (ret)
Stanley Chue965e5e2020-12-05 19:58:59 +08008839 ufshcd_update_evt_hist(hba, UFS_EVT_SUSPEND_ERR, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008840 return ret;
8841}
8842
8843/**
8844 * ufshcd_resume - helper function for resume operations
8845 * @hba: per adapter instance
8846 * @pm_op: runtime PM or system PM
8847 *
8848 * This function basically brings the UFS device, UniPro link and controller
8849 * to active state.
8850 *
8851 * Returns 0 for success and non-zero for failure
8852 */
8853static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8854{
8855 int ret;
8856 enum uic_link_state old_link_state;
8857
8858 hba->pm_op_in_progress = 1;
8859 old_link_state = hba->uic_link_state;
8860
8861 ufshcd_hba_vreg_set_hpm(hba);
Ziqi Chen528db9e2021-01-08 18:56:24 +08008862 ret = ufshcd_vreg_set_hpm(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008863 if (ret)
8864 goto out;
8865
Ziqi Chen528db9e2021-01-08 18:56:24 +08008866 /* Make sure clocks are enabled before accessing controller */
8867 ret = ufshcd_setup_clocks(hba, true);
8868 if (ret)
8869 goto disable_vreg;
8870
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008871 /* enable the host irq as host controller would be active soon */
Can Guo5231d382019-12-05 02:14:46 +00008872 ufshcd_enable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008873
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008874 /*
8875 * Call vendor specific resume callback. As these callbacks may access
8876 * vendor specific host controller register space call them when the
8877 * host clocks are ON.
8878 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008879 ret = ufshcd_vops_resume(hba, pm_op);
8880 if (ret)
Ziqi Chen528db9e2021-01-08 18:56:24 +08008881 goto disable_irq_and_vops_clks;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008882
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008883 /* For DeepSleep, the only supported option is to have the link off */
8884 WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba));
8885
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008886 if (ufshcd_is_link_hibern8(hba)) {
8887 ret = ufshcd_uic_hibern8_exit(hba);
Can Guo4db7a232020-08-09 05:15:51 -07008888 if (!ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008889 ufshcd_set_link_active(hba);
Can Guo4db7a232020-08-09 05:15:51 -07008890 } else {
8891 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
8892 __func__, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008893 goto vendor_suspend;
Can Guo4db7a232020-08-09 05:15:51 -07008894 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008895 } else if (ufshcd_is_link_off(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008896 /*
Asutosh Das089f5b62020-04-13 23:14:48 -07008897 * A full initialization of the host and the device is
8898 * required since the link was put to off during suspend.
Adrian Hunterfe1d4c22020-11-03 16:14:02 +02008899 * Note, in the case of DeepSleep, the device will exit
8900 * DeepSleep due to device reset.
Asutosh Das089f5b62020-04-13 23:14:48 -07008901 */
8902 ret = ufshcd_reset_and_restore(hba);
8903 /*
8904 * ufshcd_reset_and_restore() should have already
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008905 * set the link state as active
8906 */
8907 if (ret || !ufshcd_is_link_active(hba))
8908 goto vendor_suspend;
8909 }
8910
8911 if (!ufshcd_is_ufs_dev_active(hba)) {
8912 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8913 if (ret)
8914 goto set_old_link_state;
8915 }
8916
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08008917 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8918 ufshcd_enable_auto_bkops(hba);
8919 else
8920 /*
8921 * If BKOPs operations are urgently needed at this moment then
8922 * keep auto-bkops enabled or else disable it.
8923 */
8924 ufshcd_urgent_bkops(hba);
8925
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008926 hba->clk_gating.is_suspended = false;
8927
Can Guo0e9d4ca2021-01-20 02:04:21 -08008928 down_write(&hba->clk_scaling_lock);
8929 hba->clk_scaling.is_allowed = true;
8930 up_write(&hba->clk_scaling_lock);
8931 if (hba->clk_scaling.is_enabled)
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008932 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008933
Adrian Hunterad448372018-03-20 15:07:38 +02008934 /* Enable Auto-Hibernate if configured */
8935 ufshcd_auto_hibern8_enable(hba);
8936
Stanley Chu51dd9052020-05-22 16:32:12 +08008937 if (hba->dev_info.b_rpm_dev_flush_capable) {
8938 hba->dev_info.b_rpm_dev_flush_capable = false;
8939 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
8940 }
8941
Can Guo71d848b2019-11-14 22:09:26 -08008942 /* Schedule clock gating in case of no access to UFS device yet */
8943 ufshcd_release(hba);
8944
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008945 goto out;
8946
8947set_old_link_state:
8948 ufshcd_link_state_transition(hba, old_link_state, 0);
8949vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008950 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008951disable_irq_and_vops_clks:
8952 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008953 ufshcd_setup_clocks(hba, false);
Can Guo2dec9472020-08-09 05:15:47 -07008954 if (ufshcd_is_clkgating_allowed(hba)) {
8955 hba->clk_gating.state = CLKS_OFF;
8956 trace_ufshcd_clk_gating(dev_name(hba->dev),
8957 hba->clk_gating.state);
8958 }
Ziqi Chen528db9e2021-01-08 18:56:24 +08008959disable_vreg:
8960 ufshcd_vreg_set_lpm(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008961out:
8962 hba->pm_op_in_progress = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08008963 if (ret)
Stanley Chue965e5e2020-12-05 19:58:59 +08008964 ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008965 return ret;
8966}
8967
8968/**
8969 * ufshcd_system_suspend - system suspend routine
8970 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008971 *
8972 * Check the description of ufshcd_suspend() function for more details.
8973 *
8974 * Returns 0 for success and non-zero for failure
8975 */
8976int ufshcd_system_suspend(struct ufs_hba *hba)
8977{
8978 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008979 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008980
Can Guofb7afe22021-01-13 19:13:27 -08008981 if (!hba) {
8982 early_suspend = true;
8983 return 0;
8984 }
8985
Can Guo9cd20d32021-01-13 19:13:28 -08008986 down(&hba->host_sem);
Can Guofb7afe22021-01-13 19:13:27 -08008987
8988 if (!hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03008989 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008990
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008991 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8992 hba->curr_dev_pwr_mode) &&
8993 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
Stanley Chu1d538642020-12-22 15:29:04 +08008994 hba->uic_link_state) &&
8995 !hba->dev_info.b_rpm_dev_flush_capable)
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008996 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008997
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008998 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008999 /*
9000 * UFS device and/or UFS link low power states during runtime
9001 * suspend seems to be different than what is expected during
9002 * system suspend. Hence runtime resume the devic & link and
9003 * let the system suspend low power states to take effect.
9004 * TODO: If resume takes longer time, we might have optimize
9005 * it in future by not resuming everything if possible.
9006 */
9007 ret = ufshcd_runtime_resume(hba);
9008 if (ret)
9009 goto out;
9010 }
9011
9012 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
9013out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009014 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
9015 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08009016 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03009017 if (!ret)
9018 hba->is_sys_suspended = true;
Can Guo88a92d62020-12-02 04:04:01 -08009019 else
Can Guo9cd20d32021-01-13 19:13:28 -08009020 up(&hba->host_sem);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009021 return ret;
9022}
9023EXPORT_SYMBOL(ufshcd_system_suspend);
9024
9025/**
9026 * ufshcd_system_resume - system resume routine
9027 * @hba: per adapter instance
9028 *
9029 * Returns 0 for success and non-zero for failure
9030 */
9031
9032int ufshcd_system_resume(struct ufs_hba *hba)
9033{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009034 int ret = 0;
9035 ktime_t start = ktime_get();
9036
Can Guofb7afe22021-01-13 19:13:27 -08009037 if (!hba)
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07009038 return -EINVAL;
Can Guofb7afe22021-01-13 19:13:27 -08009039
9040 if (unlikely(early_suspend)) {
9041 early_suspend = false;
Can Guo9cd20d32021-01-13 19:13:28 -08009042 down(&hba->host_sem);
Can Guo88a92d62020-12-02 04:04:01 -08009043 }
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07009044
9045 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009046 /*
9047 * Let the runtime resume take care of resuming
9048 * if runtime suspended.
9049 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009050 goto out;
9051 else
9052 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
9053out:
9054 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
9055 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08009056 hba->curr_dev_pwr_mode, hba->uic_link_state);
Stanley Chuce9e7bc2019-01-07 22:19:34 +08009057 if (!ret)
9058 hba->is_sys_suspended = false;
Can Guo9cd20d32021-01-13 19:13:28 -08009059 up(&hba->host_sem);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009060 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009061}
9062EXPORT_SYMBOL(ufshcd_system_resume);
9063
9064/**
9065 * ufshcd_runtime_suspend - runtime suspend routine
9066 * @hba: per adapter instance
9067 *
9068 * Check the description of ufshcd_suspend() function for more details.
9069 *
9070 * Returns 0 for success and non-zero for failure
9071 */
9072int ufshcd_runtime_suspend(struct ufs_hba *hba)
9073{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009074 int ret = 0;
9075 ktime_t start = ktime_get();
9076
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07009077 if (!hba)
9078 return -EINVAL;
9079
9080 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009081 goto out;
9082 else
9083 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
9084out:
9085 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
9086 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08009087 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009088 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309089}
9090EXPORT_SYMBOL(ufshcd_runtime_suspend);
9091
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009092/**
9093 * ufshcd_runtime_resume - runtime resume routine
9094 * @hba: per adapter instance
9095 *
9096 * This function basically brings the UFS device, UniPro link and controller
9097 * to active state. Following operations are done in this function:
9098 *
9099 * 1. Turn on all the controller related clocks
9100 * 2. Bring the UniPro link out of Hibernate state
9101 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
9102 * to active state.
9103 * 4. If auto-bkops is enabled on the device, disable it.
9104 *
9105 * So following would be the possible power state after this function return
9106 * successfully:
9107 * S1: UFS device in Active state with VCC rail ON
9108 * UniPro link in Active state
9109 * All the UFS/UniPro controller clocks are ON
9110 *
9111 * Returns 0 for success and non-zero for failure
9112 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309113int ufshcd_runtime_resume(struct ufs_hba *hba)
9114{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009115 int ret = 0;
9116 ktime_t start = ktime_get();
9117
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07009118 if (!hba)
9119 return -EINVAL;
9120
9121 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009122 goto out;
9123 else
9124 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
9125out:
9126 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
9127 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08009128 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08009129 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309130}
9131EXPORT_SYMBOL(ufshcd_runtime_resume);
9132
9133int ufshcd_runtime_idle(struct ufs_hba *hba)
9134{
9135 return 0;
9136}
9137EXPORT_SYMBOL(ufshcd_runtime_idle);
9138
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309139/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009140 * ufshcd_shutdown - shutdown routine
9141 * @hba: per adapter instance
9142 *
9143 * This function would power off both UFS device and UFS link.
9144 *
9145 * Returns 0 always to allow force shutdown even in case of errors.
9146 */
9147int ufshcd_shutdown(struct ufs_hba *hba)
9148{
9149 int ret = 0;
9150
Can Guo9cd20d32021-01-13 19:13:28 -08009151 down(&hba->host_sem);
9152 hba->shutting_down = true;
9153 up(&hba->host_sem);
9154
Stanley Chuf51913e2019-09-18 12:20:38 +08009155 if (!hba->is_powered)
9156 goto out;
9157
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009158 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
9159 goto out;
9160
Stanley Chue92643d2020-11-19 14:29:16 +08009161 pm_runtime_get_sync(hba->dev);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009162
9163 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
9164out:
9165 if (ret)
9166 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
Can Guo88a92d62020-12-02 04:04:01 -08009167 hba->is_powered = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009168 /* allow force shutdown even in case of errors */
9169 return 0;
9170}
9171EXPORT_SYMBOL(ufshcd_shutdown);
9172
9173/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309174 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309175 * data structure memory
Bart Van Assche8aa29f12018-03-01 15:07:20 -08009176 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309177 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309178void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309179{
Avri Altmandf032bf2018-10-07 17:30:35 +03009180 ufs_bsg_remove(hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02009181 ufs_sysfs_remove_nodes(hba->dev);
Bart Van Assche69a6c262019-12-09 10:13:09 -08009182 blk_cleanup_queue(hba->tmf_queue);
9183 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08009184 blk_cleanup_queue(hba->cmd_queue);
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05309185 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309186 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05309187 ufshcd_disable_intr(hba, hba->intr_mask);
Bart Van Assche5cac1092020-05-07 15:27:50 -07009188 ufshcd_hba_stop(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009189 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309190}
9191EXPORT_SYMBOL_GPL(ufshcd_remove);
9192
9193/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02009194 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
9195 * @hba: pointer to Host Bus Adapter (HBA)
9196 */
9197void ufshcd_dealloc_host(struct ufs_hba *hba)
9198{
Satya Tangiraladf043c742020-07-06 20:04:14 +00009199 ufshcd_crypto_destroy_keyslot_manager(hba);
Yaniv Gardi47555a52015-10-28 13:15:49 +02009200 scsi_host_put(hba->host);
9201}
9202EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
9203
9204/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09009205 * ufshcd_set_dma_mask - Set dma mask based on the controller
9206 * addressing capability
9207 * @hba: per adapter instance
9208 *
9209 * Returns 0 for success, non-zero for failure
9210 */
9211static int ufshcd_set_dma_mask(struct ufs_hba *hba)
9212{
9213 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
9214 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
9215 return 0;
9216 }
9217 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
9218}
9219
9220/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009221 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309222 * @dev: pointer to device handle
9223 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309224 * Returns 0 on success, non-zero value on failure
9225 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009226int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309227{
9228 struct Scsi_Host *host;
9229 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009230 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309231
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309232 if (!dev) {
9233 dev_err(dev,
9234 "Invalid memory reference for dev is NULL\n");
9235 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309236 goto out_error;
9237 }
9238
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309239 host = scsi_host_alloc(&ufshcd_driver_template,
9240 sizeof(struct ufs_hba));
9241 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309242 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309243 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309244 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309245 }
9246 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309247 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309248 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009249 *hba_handle = hba;
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05309250 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009251
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03009252 INIT_LIST_HEAD(&hba->clk_list_head);
9253
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009254out_error:
9255 return err;
9256}
9257EXPORT_SYMBOL(ufshcd_alloc_host);
9258
Bart Van Assche69a6c262019-12-09 10:13:09 -08009259/* This function exists because blk_mq_alloc_tag_set() requires this. */
9260static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
9261 const struct blk_mq_queue_data *qd)
9262{
9263 WARN_ON_ONCE(true);
9264 return BLK_STS_NOTSUPP;
9265}
9266
9267static const struct blk_mq_ops ufshcd_tmf_ops = {
9268 .queue_rq = ufshcd_queue_tmf,
9269};
9270
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009271/**
9272 * ufshcd_init - Driver initialization routine
9273 * @hba: per-adapter instance
9274 * @mmio_base: base register address
9275 * @irq: Interrupt line of device
9276 * Returns 0 on success, non-zero value on failure
9277 */
9278int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
9279{
9280 int err;
9281 struct Scsi_Host *host = hba->host;
9282 struct device *dev = hba->dev;
Can Guo4db7a232020-08-09 05:15:51 -07009283 char eh_wq_name[sizeof("ufs_eh_wq_00")];
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009284
9285 if (!mmio_base) {
9286 dev_err(hba->dev,
9287 "Invalid memory reference for mmio_base is NULL\n");
9288 err = -ENODEV;
9289 goto out_error;
9290 }
9291
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309292 hba->mmio_base = mmio_base;
9293 hba->irq = irq;
Stanley Chu90b84912020-05-09 17:37:13 +08009294 hba->vps = &ufs_hba_vps;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309295
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009296 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009297 if (err)
9298 goto out_error;
9299
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309300 /* Read capabilities registers */
Satya Tangiraladf043c742020-07-06 20:04:14 +00009301 err = ufshcd_hba_capabilities(hba);
9302 if (err)
9303 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309304
9305 /* Get UFS version supported by the controller */
9306 hba->ufs_version = ufshcd_get_ufs_version(hba);
9307
Yaniv Gardic01848c2016-12-05 19:25:02 -08009308 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
9309 (hba->ufs_version != UFSHCI_VERSION_11) &&
9310 (hba->ufs_version != UFSHCI_VERSION_20) &&
9311 (hba->ufs_version != UFSHCI_VERSION_21))
9312 dev_err(hba->dev, "invalid UFS version 0x%x\n",
9313 hba->ufs_version);
9314
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05309315 /* Get Interrupt bit mask per version */
9316 hba->intr_mask = ufshcd_get_intr_mask(hba);
9317
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09009318 err = ufshcd_set_dma_mask(hba);
9319 if (err) {
9320 dev_err(hba->dev, "set dma mask failed\n");
9321 goto out_disable;
9322 }
9323
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309324 /* Allocate memory for host memory space */
9325 err = ufshcd_memory_alloc(hba);
9326 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309327 dev_err(hba->dev, "Memory allocation failed\n");
9328 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309329 }
9330
9331 /* Configure LRB */
9332 ufshcd_host_memory_configure(hba);
9333
9334 host->can_queue = hba->nutrs;
9335 host->cmd_per_lun = hba->nutrs;
9336 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03009337 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309338 host->max_channel = UFSHCD_MAX_CHANNEL;
9339 host->unique_id = host->host_no;
Avri Altmana851b2b2018-10-07 17:30:34 +03009340 host->max_cmd_len = UFS_CDB_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309341
Dolev Raviv7eb584d2014-09-25 15:32:31 +03009342 hba->max_pwr_info.is_valid = false;
9343
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309344 /* Initialize work queues */
Can Guo4db7a232020-08-09 05:15:51 -07009345 snprintf(eh_wq_name, sizeof(eh_wq_name), "ufs_eh_wq_%d",
9346 hba->host->host_no);
9347 hba->eh_wq = create_singlethread_workqueue(eh_wq_name);
9348 if (!hba->eh_wq) {
9349 dev_err(hba->dev, "%s: failed to create eh workqueue\n",
9350 __func__);
9351 err = -ENOMEM;
9352 goto out_disable;
9353 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05309354 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309355 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309356
Can Guo9cd20d32021-01-13 19:13:28 -08009357 sema_init(&hba->host_sem, 1);
Can Guo88a92d62020-12-02 04:04:01 -08009358
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309359 /* Initialize UIC command mutex */
9360 mutex_init(&hba->uic_cmd_mutex);
9361
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05309362 /* Initialize mutex for device management commands */
9363 mutex_init(&hba->dev_cmd.lock);
9364
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08009365 init_rwsem(&hba->clk_scaling_lock);
9366
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009367 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02009368
Vivek Gautameebcc192018-08-07 23:17:39 +05309369 ufshcd_init_clk_scaling(hba);
9370
Yaniv Gardi199ef132016-03-10 17:37:06 +02009371 /*
9372 * In order to avoid any spurious interrupt immediately after
9373 * registering UFS controller interrupt handler, clear any pending UFS
9374 * interrupt status and disable all the UFS interrupts.
9375 */
9376 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
9377 REG_INTERRUPT_STATUS);
9378 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
9379 /*
9380 * Make sure that UFS interrupts are disabled and any pending interrupt
9381 * status is cleared before registering UFS interrupt handler.
9382 */
9383 mb();
9384
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309385 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09009386 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309387 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309388 dev_err(hba->dev, "request irq failed\n");
Can Guo4543d9d2021-01-20 02:04:22 -08009389 goto out_disable;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009390 } else {
9391 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309392 }
9393
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309394 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309395 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309396 dev_err(hba->dev, "scsi_add_host failed\n");
Can Guo4543d9d2021-01-20 02:04:22 -08009397 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309398 }
9399
Bart Van Assche7252a362019-12-09 10:13:08 -08009400 hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set);
9401 if (IS_ERR(hba->cmd_queue)) {
9402 err = PTR_ERR(hba->cmd_queue);
9403 goto out_remove_scsi_host;
9404 }
9405
Bart Van Assche69a6c262019-12-09 10:13:09 -08009406 hba->tmf_tag_set = (struct blk_mq_tag_set) {
9407 .nr_hw_queues = 1,
9408 .queue_depth = hba->nutmrs,
9409 .ops = &ufshcd_tmf_ops,
9410 .flags = BLK_MQ_F_NO_SCHED,
9411 };
9412 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
9413 if (err < 0)
9414 goto free_cmd_queue;
9415 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set);
9416 if (IS_ERR(hba->tmf_queue)) {
9417 err = PTR_ERR(hba->tmf_queue);
9418 goto free_tmf_tag_set;
9419 }
9420
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07009421 /* Reset the attached device */
Stanley Chu31a5d9c2020-12-08 21:56:35 +08009422 ufshcd_device_reset(hba);
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07009423
Satya Tangiraladf043c742020-07-06 20:04:14 +00009424 ufshcd_init_crypto(hba);
9425
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309426 /* Host controller enable */
9427 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309428 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309429 dev_err(hba->dev, "Host controller enable failed\n");
Stanley Chue965e5e2020-12-05 19:58:59 +08009430 ufshcd_print_evt_hist(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08009431 ufshcd_print_host_state(hba);
Bart Van Assche69a6c262019-12-09 10:13:09 -08009432 goto free_tmf_queue;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309433 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309434
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08009435 /*
9436 * Set the default power management level for runtime and system PM.
9437 * Default power saving mode is to keep UFS link in Hibern8 state
9438 * and UFS device in sleep state.
9439 */
9440 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9441 UFS_SLEEP_PWR_MODE,
9442 UIC_LINK_HIBERN8_STATE);
9443 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9444 UFS_SLEEP_PWR_MODE,
9445 UIC_LINK_HIBERN8_STATE);
9446
Stanley Chu51dd9052020-05-22 16:32:12 +08009447 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work,
9448 ufshcd_rpm_dev_flush_recheck_work);
9449
Adrian Hunterad448372018-03-20 15:07:38 +02009450 /* Set the default auto-hiberate idle timer value to 150 ms */
Stanley Chuf571b372019-05-21 14:44:53 +08009451 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
Adrian Hunterad448372018-03-20 15:07:38 +02009452 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
9453 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
9454 }
9455
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05309456 /* Hold auto suspend until async scan completes */
9457 pm_runtime_get_sync(dev);
Subhash Jadavani38135532018-05-03 16:37:18 +05309458 atomic_set(&hba->scsi_block_reqs_cnt, 0);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009459 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08009460 * We are assuming that device wasn't put in sleep/power-down
9461 * state exclusively during the boot stage before kernel.
9462 * This assumption helps avoid doing link startup twice during
9463 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009464 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08009465 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009466
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309467 async_schedule(ufshcd_async_scan, hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02009468 ufs_sysfs_add_nodes(hba->dev);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309469
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309470 return 0;
9471
Bart Van Assche69a6c262019-12-09 10:13:09 -08009472free_tmf_queue:
9473 blk_cleanup_queue(hba->tmf_queue);
9474free_tmf_tag_set:
9475 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08009476free_cmd_queue:
9477 blk_cleanup_queue(hba->cmd_queue);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309478out_remove_scsi_host:
9479 scsi_remove_host(hba->host);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309480out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009481 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009482 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309483out_error:
9484 return err;
9485}
9486EXPORT_SYMBOL_GPL(ufshcd_init);
9487
Adrian Hunterb6cacaf2021-01-07 09:25:38 +02009488static int __init ufshcd_core_init(void)
9489{
9490 ufs_debugfs_init();
9491 return 0;
9492}
9493
9494static void __exit ufshcd_core_exit(void)
9495{
9496 ufs_debugfs_exit();
9497}
9498
9499module_init(ufshcd_core_init);
9500module_exit(ufshcd_core_exit);
9501
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309502MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
9503MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05309504MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309505MODULE_LICENSE("GPL");
9506MODULE_VERSION(UFSHCD_DRIVER_VERSION);