blob: 0af3dd37ae6f7ea99478e90f3a0f23547239d1a7 [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>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053018#include "ufshcd.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020019#include "ufs_quirks.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053020#include "unipro.h"
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +020021#include "ufs-sysfs.h"
Avri Altmandf032bf2018-10-07 17:30:35 +030022#include "ufs_bsg.h"
Satya Tangiraladf043c742020-07-06 20:04:14 +000023#include "ufshcd-crypto.h"
Asutosh Das3d17b9b2020-04-22 14:41:42 -070024#include <asm/unaligned.h>
25#include <linux/blkdev.h>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053026
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -080027#define CREATE_TRACE_POINTS
28#include <trace/events/ufs.h>
29
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053030#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
31 UTP_TASK_REQ_COMPL |\
32 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053033/* UIC command timeout, unit: ms */
34#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053035
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053036/* NOP OUT retries waiting for NOP IN response */
37#define NOP_OUT_RETRIES 10
38/* Timeout after 30 msecs if NOP OUT hangs without response */
39#define NOP_OUT_TIMEOUT 30 /* msecs */
40
Dolev Raviv68078d52013-07-30 00:35:58 +053041/* Query request retries */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080042#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +053043/* Query request timeout */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080044#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +053045
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053046/* Task management command timeout */
47#define TM_CMD_TIMEOUT 100 /* msecs */
48
Yaniv Gardi64238fb2016-02-01 15:02:43 +020049/* maximum number of retries for a general UIC command */
50#define UFS_UIC_COMMAND_RETRIES 3
51
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030052/* maximum number of link-startup retries */
53#define DME_LINKSTARTUP_RETRIES 3
54
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020055/* Maximum retries for Hibern8 enter */
56#define UIC_HIBERN8_ENTER_RETRIES 3
57
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030058/* maximum number of reset retries before giving up */
59#define MAX_HOST_RESET_RETRIES 5
60
Dolev Raviv68078d52013-07-30 00:35:58 +053061/* Expose the flag value from utp_upiu_query.value */
62#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
63
Seungwon Jeon7d568652013-08-31 21:40:20 +053064/* Interrupt aggregation default timeout, unit: 40us */
65#define INT_AGGR_DEF_TO 0x02
66
Stanley Chu49615ba2019-09-16 23:56:50 +080067/* default delay of autosuspend: 2000 ms */
68#define RPM_AUTOSUSPEND_DELAY_MS 2000
69
Stanley Chu51dd9052020-05-22 16:32:12 +080070/* Default delay of RPM device flush delayed work */
71#define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000
72
Can Guo09f17792020-02-10 19:40:49 -080073/* Default value of wait time before gating device ref clock */
74#define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */
75
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030076#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
77 ({ \
78 int _ret; \
79 if (_on) \
80 _ret = ufshcd_enable_vreg(_dev, _vreg); \
81 else \
82 _ret = ufshcd_disable_vreg(_dev, _vreg); \
83 _ret; \
84 })
85
Tomas Winklerba809172018-06-14 11:14:09 +030086#define ufshcd_hex_dump(prefix_str, buf, len) do { \
87 size_t __len = (len); \
88 print_hex_dump(KERN_ERR, prefix_str, \
89 __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\
90 16, 4, buf, __len, false); \
91} while (0)
92
93int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
94 const char *prefix)
95{
Marc Gonzalezd6724752019-01-22 18:29:22 +010096 u32 *regs;
97 size_t pos;
98
99 if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */
100 return -EINVAL;
Tomas Winklerba809172018-06-14 11:14:09 +0300101
Can Guocddaeba2019-11-14 22:09:27 -0800102 regs = kzalloc(len, GFP_ATOMIC);
Tomas Winklerba809172018-06-14 11:14:09 +0300103 if (!regs)
104 return -ENOMEM;
105
Marc Gonzalezd6724752019-01-22 18:29:22 +0100106 for (pos = 0; pos < len; pos += 4)
107 regs[pos / 4] = ufshcd_readl(hba, offset + pos);
108
Tomas Winklerba809172018-06-14 11:14:09 +0300109 ufshcd_hex_dump(prefix, regs, len);
110 kfree(regs);
111
112 return 0;
113}
114EXPORT_SYMBOL_GPL(ufshcd_dump_regs);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800115
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530116enum {
117 UFSHCD_MAX_CHANNEL = 0,
118 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530119 UFSHCD_CMD_PER_LUN = 32,
120 UFSHCD_CAN_QUEUE = 32,
121};
122
123/* UFSHCD states */
124enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530125 UFSHCD_STATE_RESET,
126 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530127 UFSHCD_STATE_OPERATIONAL,
Zang Leigang141f8162016-11-16 11:29:37 +0800128 UFSHCD_STATE_EH_SCHEDULED,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530129};
130
131/* UFSHCD error handling flags */
132enum {
133 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530134};
135
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530136/* UFSHCD UIC layer error flags */
137enum {
138 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200139 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
140 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
141 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
142 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
143 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530144};
145
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530146#define ufshcd_set_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300147 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530148#define ufshcd_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300149 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530150#define ufshcd_clear_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300151 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530152
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +0200153struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300154 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
155 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
156 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
157 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
158 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
159 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
160};
161
162static inline enum ufs_dev_pwr_mode
163ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
164{
165 return ufs_pm_lvl_states[lvl].dev_state;
166}
167
168static inline enum uic_link_state
169ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
170{
171 return ufs_pm_lvl_states[lvl].link_state;
172}
173
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -0800174static inline enum ufs_pm_level
175ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
176 enum uic_link_state link_state)
177{
178 enum ufs_pm_level lvl;
179
180 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
181 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
182 (ufs_pm_lvl_states[lvl].link_state == link_state))
183 return lvl;
184 }
185
186 /* if no match found, return the level 0 */
187 return UFS_PM_LVL_0;
188}
189
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800190static struct ufs_dev_fix ufs_fixups[] = {
191 /* UFS cards deviations table */
Stanley Chuc0a18ee2020-06-12 09:26:24 +0800192 UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL,
193 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800194 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
Stanley Chued0b40f2020-06-12 09:26:25 +0800195 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
196 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE |
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800197 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
Stanley Chued0b40f2020-06-12 09:26:25 +0800198 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
199 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
200 UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/,
201 UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800202 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
203 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
204 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
205 UFS_DEVICE_QUIRK_PA_TACTIVATE),
206 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
207 UFS_DEVICE_QUIRK_PA_TACTIVATE),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800208 END_FIX
209};
210
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -0800211static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530212static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530213static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Dolev Ravive7d38252016-12-22 18:40:07 -0800214static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530215static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300216static void ufshcd_hba_exit(struct ufs_hba *hba);
Bean Huo1b9e2142020-01-20 14:08:15 +0100217static int ufshcd_probe_hba(struct ufs_hba *hba, bool async);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300218static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
219 bool skip_ref_clk);
220static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300221static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300222static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300223static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800224static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
225static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800226static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800227static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300228static irqreturn_t ufshcd_intr(int irq, void *__hba);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300229static int ufshcd_change_power_mode(struct ufs_hba *hba,
230 struct ufs_pa_layer_attr *pwr_mode);
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700231static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba);
232static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba);
233static int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable);
234static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set);
235static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable);
236
Yaniv Gardi14497322016-02-01 15:02:39 +0200237static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
238{
239 return tag >= 0 && tag < hba->nutrs;
240}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300241
Can Guo5231d382019-12-05 02:14:46 +0000242static inline void ufshcd_enable_irq(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300243{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300244 if (!hba->is_irq_enabled) {
Can Guo5231d382019-12-05 02:14:46 +0000245 enable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300246 hba->is_irq_enabled = true;
247 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300248}
249
250static inline void ufshcd_disable_irq(struct ufs_hba *hba)
251{
252 if (hba->is_irq_enabled) {
Can Guo5231d382019-12-05 02:14:46 +0000253 disable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300254 hba->is_irq_enabled = false;
255 }
256}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530257
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700258static inline void ufshcd_wb_config(struct ufs_hba *hba)
259{
260 int ret;
261
Stanley Chu79e35202020-05-08 16:01:15 +0800262 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -0700263 return;
264
265 ret = ufshcd_wb_ctrl(hba, true);
266 if (ret)
267 dev_err(hba->dev, "%s: Enable WB failed: %d\n", __func__, ret);
268 else
269 dev_info(hba->dev, "%s: Write Booster Configured\n", __func__);
270 ret = ufshcd_wb_toggle_flush_during_h8(hba, true);
271 if (ret)
272 dev_err(hba->dev, "%s: En WB flush during H8: failed: %d\n",
273 __func__, ret);
274 ufshcd_wb_toggle_flush(hba, true);
275}
276
Subhash Jadavani38135532018-05-03 16:37:18 +0530277static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
278{
279 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt))
280 scsi_unblock_requests(hba->host);
281}
282
283static void ufshcd_scsi_block_requests(struct ufs_hba *hba)
284{
285 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1)
286 scsi_block_requests(hba->host);
287}
288
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300289static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag,
290 const char *str)
291{
292 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
293
294 trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->sc.cdb);
295}
296
297static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba, unsigned int tag,
298 const char *str)
299{
300 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
301
302 trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->qr);
303}
304
305static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag,
306 const char *str)
307{
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300308 int off = (int)tag - hba->nutrs;
Christoph Hellwig391e3882018-10-07 17:30:32 +0300309 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[off];
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300310
Christoph Hellwig391e3882018-10-07 17:30:32 +0300311 trace_ufshcd_upiu(dev_name(hba->dev), str, &descp->req_header,
312 &descp->input_param1);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300313}
314
Stanley Chuaa5c6972020-06-15 15:22:35 +0800315static void ufshcd_add_uic_command_trace(struct ufs_hba *hba,
316 struct uic_command *ucmd,
317 const char *str)
318{
319 u32 cmd;
320
321 if (!trace_ufshcd_uic_command_enabled())
322 return;
323
324 if (!strcmp(str, "send"))
325 cmd = ucmd->command;
326 else
327 cmd = ufshcd_readl(hba, REG_UIC_COMMAND);
328
329 trace_ufshcd_uic_command(dev_name(hba->dev), str, cmd,
330 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1),
331 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2),
332 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3));
333}
334
Lee Susman1a07f2d2016-12-22 18:42:03 -0800335static void ufshcd_add_command_trace(struct ufs_hba *hba,
336 unsigned int tag, const char *str)
337{
338 sector_t lba = -1;
339 u8 opcode = 0;
340 u32 intr, doorbell;
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300341 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800342 struct scsi_cmnd *cmd = lrbp->cmd;
Lee Susman1a07f2d2016-12-22 18:42:03 -0800343 int transfer_len = -1;
344
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300345 if (!trace_ufshcd_command_enabled()) {
346 /* trace UPIU W/O tracing command */
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800347 if (cmd)
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300348 ufshcd_add_cmd_upiu_trace(hba, tag, str);
Lee Susman1a07f2d2016-12-22 18:42:03 -0800349 return;
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300350 }
Lee Susman1a07f2d2016-12-22 18:42:03 -0800351
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800352 if (cmd) { /* data phase exists */
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300353 /* trace UPIU also */
354 ufshcd_add_cmd_upiu_trace(hba, tag, str);
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800355 opcode = cmd->cmnd[0];
Lee Susman1a07f2d2016-12-22 18:42:03 -0800356 if ((opcode == READ_10) || (opcode == WRITE_10)) {
357 /*
358 * Currently we only fully trace read(10) and write(10)
359 * commands
360 */
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800361 if (cmd->request && cmd->request->bio)
362 lba = cmd->request->bio->bi_iter.bi_sector;
Lee Susman1a07f2d2016-12-22 18:42:03 -0800363 transfer_len = be32_to_cpu(
364 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
365 }
366 }
367
368 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
369 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
370 trace_ufshcd_command(dev_name(hba->dev), str, tag,
371 doorbell, transfer_len, intr, lba, opcode);
372}
373
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800374static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
375{
376 struct ufs_clk_info *clki;
377 struct list_head *head = &hba->clk_list_head;
378
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300379 if (list_empty(head))
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800380 return;
381
382 list_for_each_entry(clki, head, list) {
383 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
384 clki->max_freq)
385 dev_err(hba->dev, "clk: %s, rate: %u\n",
386 clki->name, clki->curr_freq);
387 }
388}
389
Stanley Chu48d5b972019-07-10 21:38:18 +0800390static void ufshcd_print_err_hist(struct ufs_hba *hba,
391 struct ufs_err_reg_hist *err_hist,
392 char *err_name)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800393{
394 int i;
Stanley Chu27752642019-01-28 22:04:26 +0800395 bool found = false;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800396
Stanley Chu48d5b972019-07-10 21:38:18 +0800397 for (i = 0; i < UFS_ERR_REG_HIST_LENGTH; i++) {
398 int p = (i + err_hist->pos) % UFS_ERR_REG_HIST_LENGTH;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800399
Stanley Chu645728a2020-01-04 22:26:06 +0800400 if (err_hist->tstamp[p] == 0)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800401 continue;
Stanley Chuc5397f12019-07-10 21:38:20 +0800402 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p,
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800403 err_hist->reg[p], ktime_to_us(err_hist->tstamp[p]));
Stanley Chu27752642019-01-28 22:04:26 +0800404 found = true;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800405 }
Stanley Chu27752642019-01-28 22:04:26 +0800406
407 if (!found)
Stanley Chufd1fb4d2020-01-04 22:26:08 +0800408 dev_err(hba->dev, "No record of %s\n", err_name);
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800409}
410
Dolev Raviv66cc8202016-12-22 18:39:42 -0800411static void ufshcd_print_host_regs(struct ufs_hba *hba)
412{
Tomas Winklerba809172018-06-14 11:14:09 +0300413 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
Dolev Raviv66cc8202016-12-22 18:39:42 -0800414 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n",
415 hba->ufs_version, hba->capabilities);
416 dev_err(hba->dev,
417 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n",
418 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800419 dev_err(hba->dev,
420 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d\n",
421 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
422 hba->ufs_stats.hibern8_exit_cnt);
423
Stanley Chu48d5b972019-07-10 21:38:18 +0800424 ufshcd_print_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err");
425 ufshcd_print_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err");
426 ufshcd_print_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err");
427 ufshcd_print_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err");
428 ufshcd_print_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err");
Stanley Chud3c615b2019-07-10 21:38:19 +0800429 ufshcd_print_err_hist(hba, &hba->ufs_stats.auto_hibern8_err,
430 "auto_hibern8_err");
Stanley Chu8808b4e2019-07-10 21:38:21 +0800431 ufshcd_print_err_hist(hba, &hba->ufs_stats.fatal_err, "fatal_err");
432 ufshcd_print_err_hist(hba, &hba->ufs_stats.link_startup_err,
433 "link_startup_fail");
434 ufshcd_print_err_hist(hba, &hba->ufs_stats.resume_err, "resume_fail");
435 ufshcd_print_err_hist(hba, &hba->ufs_stats.suspend_err,
436 "suspend_fail");
437 ufshcd_print_err_hist(hba, &hba->ufs_stats.dev_reset, "dev_reset");
438 ufshcd_print_err_hist(hba, &hba->ufs_stats.host_reset, "host_reset");
439 ufshcd_print_err_hist(hba, &hba->ufs_stats.task_abort, "task_abort");
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800440
441 ufshcd_print_clk_freqs(hba);
442
Stanley Chu7c486d912019-12-24 21:01:06 +0800443 ufshcd_vops_dbg_register_dump(hba);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800444}
445
446static
447void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
448{
449 struct ufshcd_lrb *lrbp;
Gilad Broner7fabb772017-02-03 16:56:50 -0800450 int prdt_length;
Dolev Raviv66cc8202016-12-22 18:39:42 -0800451 int tag;
452
453 for_each_set_bit(tag, &bitmap, hba->nutrs) {
454 lrbp = &hba->lrb[tag];
455
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800456 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
457 tag, ktime_to_us(lrbp->issue_time_stamp));
Zang Leigang09017182017-09-27 10:06:06 +0800458 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
459 tag, ktime_to_us(lrbp->compl_time_stamp));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800460 dev_err(hba->dev,
461 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
462 tag, (u64)lrbp->utrd_dma_addr);
463
Dolev Raviv66cc8202016-12-22 18:39:42 -0800464 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
465 sizeof(struct utp_transfer_req_desc));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800466 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
467 (u64)lrbp->ucd_req_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800468 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
469 sizeof(struct utp_upiu_req));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800470 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
471 (u64)lrbp->ucd_rsp_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800472 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
473 sizeof(struct utp_upiu_rsp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800474
Gilad Broner7fabb772017-02-03 16:56:50 -0800475 prdt_length = le16_to_cpu(
476 lrbp->utr_descriptor_ptr->prd_table_length);
477 dev_err(hba->dev,
478 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
479 tag, prdt_length,
480 (u64)lrbp->ucd_prdt_dma_addr);
481
482 if (pr_prdt)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800483 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
Gilad Broner7fabb772017-02-03 16:56:50 -0800484 sizeof(struct ufshcd_sg_entry) * prdt_length);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800485 }
486}
487
488static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
489{
Dolev Raviv66cc8202016-12-22 18:39:42 -0800490 int tag;
491
492 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
Christoph Hellwig391e3882018-10-07 17:30:32 +0300493 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag];
494
Dolev Raviv66cc8202016-12-22 18:39:42 -0800495 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
Christoph Hellwig391e3882018-10-07 17:30:32 +0300496 ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800497 }
498}
499
Gilad Broner6ba65582017-02-03 16:57:28 -0800500static void ufshcd_print_host_state(struct ufs_hba *hba)
501{
502 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
Bart Van Assche7252a362019-12-09 10:13:08 -0800503 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n",
504 hba->outstanding_reqs, hba->outstanding_tasks);
Gilad Broner6ba65582017-02-03 16:57:28 -0800505 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
506 hba->saved_err, hba->saved_uic_err);
507 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
508 hba->curr_dev_pwr_mode, hba->uic_link_state);
509 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
510 hba->pm_op_in_progress, hba->is_sys_suspended);
511 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
512 hba->auto_bkops_enabled, hba->host->host_self_blocked);
513 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
514 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
515 hba->eh_flags, hba->req_abort_count);
516 dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n",
517 hba->capabilities, hba->caps);
518 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
519 hba->dev_quirks);
520}
521
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800522/**
523 * ufshcd_print_pwr_info - print power params as saved in hba
524 * power info
525 * @hba: per-adapter instance
526 */
527static void ufshcd_print_pwr_info(struct ufs_hba *hba)
528{
529 static const char * const names[] = {
530 "INVALID MODE",
531 "FAST MODE",
532 "SLOW_MODE",
533 "INVALID MODE",
534 "FASTAUTO_MODE",
535 "SLOWAUTO_MODE",
536 "INVALID MODE",
537 };
538
539 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
540 __func__,
541 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
542 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
543 names[hba->pwr_info.pwr_rx],
544 names[hba->pwr_info.pwr_tx],
545 hba->pwr_info.hs_rate);
546}
547
Stanley Chu5c955c12020-03-18 18:40:12 +0800548void ufshcd_delay_us(unsigned long us, unsigned long tolerance)
549{
550 if (!us)
551 return;
552
553 if (us < 10)
554 udelay(us);
555 else
556 usleep_range(us, us + tolerance);
557}
558EXPORT_SYMBOL_GPL(ufshcd_delay_us);
559
Bart Van Assche5cac1092020-05-07 15:27:50 -0700560/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530561 * ufshcd_wait_for_register - wait for register value to change
Bart Van Assche5cac1092020-05-07 15:27:50 -0700562 * @hba: per-adapter interface
563 * @reg: mmio register offset
564 * @mask: mask to apply to the read register value
565 * @val: value to wait for
566 * @interval_us: polling interval in microseconds
567 * @timeout_ms: timeout in milliseconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530568 *
Bart Van Assche5cac1092020-05-07 15:27:50 -0700569 * Return:
570 * -ETIMEDOUT on error, zero on success.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530571 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200572int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
573 u32 val, unsigned long interval_us,
Bart Van Assche5cac1092020-05-07 15:27:50 -0700574 unsigned long timeout_ms)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530575{
576 int err = 0;
577 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
578
579 /* ignore bits that we don't intend to wait on */
580 val = val & mask;
581
582 while ((ufshcd_readl(hba, reg) & mask) != val) {
Bart Van Assche5cac1092020-05-07 15:27:50 -0700583 usleep_range(interval_us, interval_us + 50);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530584 if (time_after(jiffies, timeout)) {
585 if ((ufshcd_readl(hba, reg) & mask) != val)
586 err = -ETIMEDOUT;
587 break;
588 }
589 }
590
591 return err;
592}
593
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530594/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530595 * ufshcd_get_intr_mask - Get the interrupt bit mask
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800596 * @hba: Pointer to adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530597 *
598 * Returns interrupt bit mask per version
599 */
600static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
601{
Yaniv Gardic01848c2016-12-05 19:25:02 -0800602 u32 intr_mask = 0;
603
604 switch (hba->ufs_version) {
605 case UFSHCI_VERSION_10:
606 intr_mask = INTERRUPT_MASK_ALL_VER_10;
607 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800608 case UFSHCI_VERSION_11:
609 case UFSHCI_VERSION_20:
610 intr_mask = INTERRUPT_MASK_ALL_VER_11;
611 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800612 case UFSHCI_VERSION_21:
613 default:
614 intr_mask = INTERRUPT_MASK_ALL_VER_21;
Tomohiro Kusumi031d1e02017-03-23 12:49:04 +0200615 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800616 }
617
618 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530619}
620
621/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530622 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800623 * @hba: Pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530624 *
625 * Returns UFSHCI version supported by the controller
626 */
627static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
628{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200629 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
630 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300631
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530632 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530633}
634
635/**
636 * ufshcd_is_device_present - Check if any device connected to
637 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300638 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530639 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300640 * Returns true if device present, false if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530641 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300642static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530643{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300644 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300645 DEVICE_PRESENT) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530646}
647
648/**
649 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800650 * @lrbp: pointer to local command reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530651 *
652 * This function is used to get the OCS field from UTRD
653 * Returns the OCS field in the UTRD
654 */
655static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
656{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530657 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530658}
659
660/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530661 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
662 * @hba: per adapter instance
663 * @pos: position of the bit to be cleared
664 */
665static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
666{
Alim Akhtar87183842020-05-28 06:46:49 +0530667 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
668 ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
669 else
670 ufshcd_writel(hba, ~(1 << pos),
671 REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Alim Akhtar1399c5b2018-05-06 15:44:15 +0530672}
673
674/**
675 * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register
676 * @hba: per adapter instance
677 * @pos: position of the bit to be cleared
678 */
679static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
680{
Alim Akhtar87183842020-05-28 06:46:49 +0530681 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
682 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
683 else
684 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530685}
686
687/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200688 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
689 * @hba: per adapter instance
690 * @tag: position of the bit to be cleared
691 */
692static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
693{
694 __clear_bit(tag, &hba->outstanding_reqs);
695}
696
697/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530698 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
699 * @reg: Register value of host controller status
700 *
701 * Returns integer, 0 on Success and positive value if failed
702 */
703static inline int ufshcd_get_lists_status(u32 reg)
704{
Tomohiro Kusumi6cf16112017-04-26 20:28:58 +0300705 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530706}
707
708/**
709 * ufshcd_get_uic_cmd_result - Get the UIC command result
710 * @hba: Pointer to adapter instance
711 *
712 * This function gets the result of UIC command completion
713 * Returns 0 on success, non zero value on error
714 */
715static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
716{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530717 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530718 MASK_UIC_COMMAND_RESULT;
719}
720
721/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530722 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
723 * @hba: Pointer to adapter instance
724 *
725 * This function gets UIC command argument3
726 * Returns 0 on success, non zero value on error
727 */
728static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
729{
730 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
731}
732
733/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530734 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530735 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530736 */
737static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530738ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530739{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530740 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530741}
742
743/**
744 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
745 * @ucd_rsp_ptr: pointer to response UPIU
746 *
747 * This function gets the response status and scsi_status from response UPIU
748 * Returns the response result code.
749 */
750static inline int
751ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
752{
753 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
754}
755
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530756/*
757 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
758 * from response UPIU
759 * @ucd_rsp_ptr: pointer to response UPIU
760 *
761 * Return the data segment length.
762 */
763static inline unsigned int
764ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
765{
766 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
767 MASK_RSP_UPIU_DATA_SEG_LEN;
768}
769
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530770/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530771 * ufshcd_is_exception_event - Check if the device raised an exception event
772 * @ucd_rsp_ptr: pointer to response UPIU
773 *
774 * The function checks if the device raised an exception event indicated in
775 * the Device Information field of response UPIU.
776 *
777 * Returns true if exception is raised, false otherwise.
778 */
779static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
780{
781 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
782 MASK_RSP_EXCEPTION_EVENT ? true : false;
783}
784
785/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530786 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530787 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530788 */
789static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530790ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530791{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530792 ufshcd_writel(hba, INT_AGGR_ENABLE |
793 INT_AGGR_COUNTER_AND_TIMER_RESET,
794 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
795}
796
797/**
798 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
799 * @hba: per adapter instance
800 * @cnt: Interrupt aggregation counter threshold
801 * @tmout: Interrupt aggregation timeout value
802 */
803static inline void
804ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
805{
806 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
807 INT_AGGR_COUNTER_THLD_VAL(cnt) |
808 INT_AGGR_TIMEOUT_VAL(tmout),
809 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530810}
811
812/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300813 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
814 * @hba: per adapter instance
815 */
816static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
817{
818 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
819}
820
821/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530822 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
823 * When run-stop registers are set to 1, it indicates the
824 * host controller that it can process the requests
825 * @hba: per adapter instance
826 */
827static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
828{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530829 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
830 REG_UTP_TASK_REQ_LIST_RUN_STOP);
831 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
832 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530833}
834
835/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530836 * ufshcd_hba_start - Start controller initialization sequence
837 * @hba: per adapter instance
838 */
839static inline void ufshcd_hba_start(struct ufs_hba *hba)
840{
Satya Tangiraladf043c742020-07-06 20:04:14 +0000841 u32 val = CONTROLLER_ENABLE;
842
843 if (ufshcd_crypto_enable(hba))
844 val |= CRYPTO_GENERAL_ENABLE;
845
846 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530847}
848
849/**
850 * ufshcd_is_hba_active - Get controller state
851 * @hba: per adapter instance
852 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300853 * Returns false if controller is active, true otherwise
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530854 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300855static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530856{
Tomohiro Kusumi4a8eec22017-03-28 16:49:25 +0300857 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
858 ? false : true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530859}
860
Yaniv Gardi37113102016-03-10 17:37:16 +0200861u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
862{
863 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
864 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
865 (hba->ufs_version == UFSHCI_VERSION_11))
866 return UFS_UNIPRO_VER_1_41;
867 else
868 return UFS_UNIPRO_VER_1_6;
869}
870EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
871
872static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
873{
874 /*
875 * If both host and device support UniPro ver1.6 or later, PA layer
876 * parameters tuning happens during link startup itself.
877 *
878 * We can manually tune PA layer parameters if either host or device
879 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
880 * logic simple, we will only do manual tuning if local unipro version
881 * doesn't support ver1.6 or later.
882 */
883 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
884 return true;
885 else
886 return false;
887}
888
Subhash Jadavani394b9492020-03-26 02:25:40 -0700889/**
890 * ufshcd_set_clk_freq - set UFS controller clock frequencies
891 * @hba: per adapter instance
892 * @scale_up: If True, set max possible frequency othewise set low frequency
893 *
894 * Returns 0 if successful
895 * Returns < 0 for any other errors
896 */
897static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800898{
899 int ret = 0;
900 struct ufs_clk_info *clki;
901 struct list_head *head = &hba->clk_list_head;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800902
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300903 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800904 goto out;
905
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800906 list_for_each_entry(clki, head, list) {
907 if (!IS_ERR_OR_NULL(clki->clk)) {
908 if (scale_up && clki->max_freq) {
909 if (clki->curr_freq == clki->max_freq)
910 continue;
911
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800912 ret = clk_set_rate(clki->clk, clki->max_freq);
913 if (ret) {
914 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
915 __func__, clki->name,
916 clki->max_freq, ret);
917 break;
918 }
919 trace_ufshcd_clk_scaling(dev_name(hba->dev),
920 "scaled up", clki->name,
921 clki->curr_freq,
922 clki->max_freq);
923
924 clki->curr_freq = clki->max_freq;
925
926 } else if (!scale_up && clki->min_freq) {
927 if (clki->curr_freq == clki->min_freq)
928 continue;
929
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800930 ret = clk_set_rate(clki->clk, clki->min_freq);
931 if (ret) {
932 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
933 __func__, clki->name,
934 clki->min_freq, ret);
935 break;
936 }
937 trace_ufshcd_clk_scaling(dev_name(hba->dev),
938 "scaled down", clki->name,
939 clki->curr_freq,
940 clki->min_freq);
941 clki->curr_freq = clki->min_freq;
942 }
943 }
944 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
945 clki->name, clk_get_rate(clki->clk));
946 }
947
Subhash Jadavani394b9492020-03-26 02:25:40 -0700948out:
949 return ret;
950}
951
952/**
953 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
954 * @hba: per adapter instance
955 * @scale_up: True if scaling up and false if scaling down
956 *
957 * Returns 0 if successful
958 * Returns < 0 for any other errors
959 */
960static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
961{
962 int ret = 0;
963 ktime_t start = ktime_get();
964
965 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
966 if (ret)
967 goto out;
968
969 ret = ufshcd_set_clk_freq(hba, scale_up);
970 if (ret)
971 goto out;
972
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800973 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
Subhash Jadavani394b9492020-03-26 02:25:40 -0700974 if (ret)
975 ufshcd_set_clk_freq(hba, !scale_up);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800976
977out:
Subhash Jadavani394b9492020-03-26 02:25:40 -0700978 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800979 (scale_up ? "up" : "down"),
980 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
981 return ret;
982}
983
984/**
985 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
986 * @hba: per adapter instance
987 * @scale_up: True if scaling up and false if scaling down
988 *
989 * Returns true if scaling is required, false otherwise.
990 */
991static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
992 bool scale_up)
993{
994 struct ufs_clk_info *clki;
995 struct list_head *head = &hba->clk_list_head;
996
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300997 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800998 return false;
999
1000 list_for_each_entry(clki, head, list) {
1001 if (!IS_ERR_OR_NULL(clki->clk)) {
1002 if (scale_up && clki->max_freq) {
1003 if (clki->curr_freq == clki->max_freq)
1004 continue;
1005 return true;
1006 } else if (!scale_up && clki->min_freq) {
1007 if (clki->curr_freq == clki->min_freq)
1008 continue;
1009 return true;
1010 }
1011 }
1012 }
1013
1014 return false;
1015}
1016
1017static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
1018 u64 wait_timeout_us)
1019{
1020 unsigned long flags;
1021 int ret = 0;
1022 u32 tm_doorbell;
1023 u32 tr_doorbell;
1024 bool timeout = false, do_last_check = false;
1025 ktime_t start;
1026
1027 ufshcd_hold(hba, false);
1028 spin_lock_irqsave(hba->host->host_lock, flags);
1029 /*
1030 * Wait for all the outstanding tasks/transfer requests.
1031 * Verify by checking the doorbell registers are clear.
1032 */
1033 start = ktime_get();
1034 do {
1035 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
1036 ret = -EBUSY;
1037 goto out;
1038 }
1039
1040 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
1041 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1042 if (!tm_doorbell && !tr_doorbell) {
1043 timeout = false;
1044 break;
1045 } else if (do_last_check) {
1046 break;
1047 }
1048
1049 spin_unlock_irqrestore(hba->host->host_lock, flags);
1050 schedule();
1051 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
1052 wait_timeout_us) {
1053 timeout = true;
1054 /*
1055 * We might have scheduled out for long time so make
1056 * sure to check if doorbells are cleared by this time
1057 * or not.
1058 */
1059 do_last_check = true;
1060 }
1061 spin_lock_irqsave(hba->host->host_lock, flags);
1062 } while (tm_doorbell || tr_doorbell);
1063
1064 if (timeout) {
1065 dev_err(hba->dev,
1066 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1067 __func__, tm_doorbell, tr_doorbell);
1068 ret = -EBUSY;
1069 }
1070out:
1071 spin_unlock_irqrestore(hba->host->host_lock, flags);
1072 ufshcd_release(hba);
1073 return ret;
1074}
1075
1076/**
1077 * ufshcd_scale_gear - scale up/down UFS gear
1078 * @hba: per adapter instance
1079 * @scale_up: True for scaling up gear and false for scaling down
1080 *
1081 * Returns 0 for success,
1082 * Returns -EBUSY if scaling can't happen at this time
1083 * Returns non-zero for any other errors
1084 */
1085static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1086{
1087 #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1
1088 int ret = 0;
1089 struct ufs_pa_layer_attr new_pwr_info;
1090
1091 if (scale_up) {
1092 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1093 sizeof(struct ufs_pa_layer_attr));
1094 } else {
1095 memcpy(&new_pwr_info, &hba->pwr_info,
1096 sizeof(struct ufs_pa_layer_attr));
1097
1098 if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN
1099 || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) {
1100 /* save the current power mode */
1101 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1102 &hba->pwr_info,
1103 sizeof(struct ufs_pa_layer_attr));
1104
1105 /* scale down gear */
1106 new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1107 new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1108 }
1109 }
1110
1111 /* check if the power mode needs to be changed or not? */
Can Guo6a9df812020-02-11 21:38:28 -08001112 ret = ufshcd_config_pwr_mode(hba, &new_pwr_info);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001113 if (ret)
1114 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1115 __func__, ret,
1116 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1117 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1118
1119 return ret;
1120}
1121
1122static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1123{
1124 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1125 int ret = 0;
1126 /*
1127 * make sure that there are no outstanding requests when
1128 * clock scaling is in progress
1129 */
Subhash Jadavani38135532018-05-03 16:37:18 +05301130 ufshcd_scsi_block_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001131 down_write(&hba->clk_scaling_lock);
1132 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
1133 ret = -EBUSY;
1134 up_write(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301135 ufshcd_scsi_unblock_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001136 }
1137
1138 return ret;
1139}
1140
1141static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
1142{
1143 up_write(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301144 ufshcd_scsi_unblock_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001145}
1146
1147/**
1148 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1149 * @hba: per adapter instance
1150 * @scale_up: True for scaling up and false for scalin down
1151 *
1152 * Returns 0 for success,
1153 * Returns -EBUSY if scaling can't happen at this time
1154 * Returns non-zero for any other errors
1155 */
1156static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1157{
1158 int ret = 0;
1159
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001160 /* let's not get into low power until clock scaling is completed */
1161 ufshcd_hold(hba, false);
1162
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001163 ret = ufshcd_clock_scaling_prepare(hba);
1164 if (ret)
Subhash Jadavani394b9492020-03-26 02:25:40 -07001165 goto out;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001166
1167 /* scale down the gear before scaling down clocks */
1168 if (!scale_up) {
1169 ret = ufshcd_scale_gear(hba, false);
1170 if (ret)
Subhash Jadavani394b9492020-03-26 02:25:40 -07001171 goto out_unprepare;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001172 }
1173
1174 ret = ufshcd_scale_clks(hba, scale_up);
1175 if (ret) {
1176 if (!scale_up)
1177 ufshcd_scale_gear(hba, true);
Subhash Jadavani394b9492020-03-26 02:25:40 -07001178 goto out_unprepare;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001179 }
1180
1181 /* scale up the gear after scaling up clocks */
1182 if (scale_up) {
1183 ret = ufshcd_scale_gear(hba, true);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001184 if (ret) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001185 ufshcd_scale_clks(hba, false);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001186 goto out_unprepare;
1187 }
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001188 }
1189
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001190 /* Enable Write Booster if we have scaled up else disable it */
1191 up_write(&hba->clk_scaling_lock);
1192 ufshcd_wb_ctrl(hba, scale_up);
1193 down_write(&hba->clk_scaling_lock);
1194
Subhash Jadavani394b9492020-03-26 02:25:40 -07001195out_unprepare:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001196 ufshcd_clock_scaling_unprepare(hba);
Subhash Jadavani394b9492020-03-26 02:25:40 -07001197out:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001198 ufshcd_release(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001199 return ret;
1200}
1201
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001202static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1203{
1204 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1205 clk_scaling.suspend_work);
1206 unsigned long irq_flags;
1207
1208 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1209 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1210 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1211 return;
1212 }
1213 hba->clk_scaling.is_suspended = true;
1214 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1215
1216 __ufshcd_suspend_clkscaling(hba);
1217}
1218
1219static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1220{
1221 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1222 clk_scaling.resume_work);
1223 unsigned long irq_flags;
1224
1225 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1226 if (!hba->clk_scaling.is_suspended) {
1227 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1228 return;
1229 }
1230 hba->clk_scaling.is_suspended = false;
1231 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1232
1233 devfreq_resume_device(hba->devfreq);
1234}
1235
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001236static int ufshcd_devfreq_target(struct device *dev,
1237 unsigned long *freq, u32 flags)
1238{
1239 int ret = 0;
1240 struct ufs_hba *hba = dev_get_drvdata(dev);
1241 ktime_t start;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001242 bool scale_up, sched_clk_scaling_suspend_work = false;
Bjorn Andersson092b4552018-05-17 23:26:37 -07001243 struct list_head *clk_list = &hba->clk_list_head;
1244 struct ufs_clk_info *clki;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001245 unsigned long irq_flags;
1246
1247 if (!ufshcd_is_clkscaling_supported(hba))
1248 return -EINVAL;
1249
Asutosh Das91831d32020-03-25 11:29:00 -07001250 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list);
1251 /* Override with the closest supported frequency */
1252 *freq = (unsigned long) clk_round_rate(clki->clk, *freq);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001253 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1254 if (ufshcd_eh_in_progress(hba)) {
1255 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1256 return 0;
1257 }
1258
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001259 if (!hba->clk_scaling.active_reqs)
1260 sched_clk_scaling_suspend_work = true;
1261
Bjorn Andersson092b4552018-05-17 23:26:37 -07001262 if (list_empty(clk_list)) {
1263 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1264 goto out;
1265 }
1266
Asutosh Das91831d32020-03-25 11:29:00 -07001267 /* Decide based on the rounded-off frequency and update */
Bjorn Andersson092b4552018-05-17 23:26:37 -07001268 scale_up = (*freq == clki->max_freq) ? true : false;
Asutosh Das91831d32020-03-25 11:29:00 -07001269 if (!scale_up)
1270 *freq = clki->min_freq;
1271 /* Update the frequency */
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001272 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1273 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1274 ret = 0;
1275 goto out; /* no state change required */
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001276 }
1277 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1278
1279 start = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001280 ret = ufshcd_devfreq_scale(hba, scale_up);
1281
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001282 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1283 (scale_up ? "up" : "down"),
1284 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1285
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001286out:
1287 if (sched_clk_scaling_suspend_work)
1288 queue_work(hba->clk_scaling.workq,
1289 &hba->clk_scaling.suspend_work);
1290
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001291 return ret;
1292}
1293
Bart Van Assche7252a362019-12-09 10:13:08 -08001294static bool ufshcd_is_busy(struct request *req, void *priv, bool reserved)
1295{
1296 int *busy = priv;
1297
1298 WARN_ON_ONCE(reserved);
1299 (*busy)++;
1300 return false;
1301}
1302
1303/* Whether or not any tag is in use by a request that is in progress. */
1304static bool ufshcd_any_tag_in_use(struct ufs_hba *hba)
1305{
1306 struct request_queue *q = hba->cmd_queue;
1307 int busy = 0;
1308
1309 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_is_busy, &busy);
1310 return busy;
1311}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001312
1313static int ufshcd_devfreq_get_dev_status(struct device *dev,
1314 struct devfreq_dev_status *stat)
1315{
1316 struct ufs_hba *hba = dev_get_drvdata(dev);
1317 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1318 unsigned long flags;
Asutosh Das91831d32020-03-25 11:29:00 -07001319 struct list_head *clk_list = &hba->clk_list_head;
1320 struct ufs_clk_info *clki;
Stanley Chub1bf66d2020-06-11 18:10:43 +08001321 ktime_t curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001322
1323 if (!ufshcd_is_clkscaling_supported(hba))
1324 return -EINVAL;
1325
1326 memset(stat, 0, sizeof(*stat));
1327
1328 spin_lock_irqsave(hba->host->host_lock, flags);
Stanley Chub1bf66d2020-06-11 18:10:43 +08001329 curr_t = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001330 if (!scaling->window_start_t)
1331 goto start_window;
1332
Asutosh Das91831d32020-03-25 11:29:00 -07001333 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1334 /*
1335 * If current frequency is 0, then the ondemand governor considers
1336 * there's no initial frequency set. And it always requests to set
1337 * to max. frequency.
1338 */
1339 stat->current_frequency = clki->curr_freq;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001340 if (scaling->is_busy_started)
Stanley Chub1bf66d2020-06-11 18:10:43 +08001341 scaling->tot_busy_t += ktime_us_delta(curr_t,
1342 scaling->busy_start_t);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001343
Stanley Chub1bf66d2020-06-11 18:10:43 +08001344 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001345 stat->busy_time = scaling->tot_busy_t;
1346start_window:
Stanley Chub1bf66d2020-06-11 18:10:43 +08001347 scaling->window_start_t = curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001348 scaling->tot_busy_t = 0;
1349
1350 if (hba->outstanding_reqs) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08001351 scaling->busy_start_t = curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001352 scaling->is_busy_started = true;
1353 } else {
1354 scaling->busy_start_t = 0;
1355 scaling->is_busy_started = false;
1356 }
1357 spin_unlock_irqrestore(hba->host->host_lock, flags);
1358 return 0;
1359}
1360
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001361static int ufshcd_devfreq_init(struct ufs_hba *hba)
1362{
Bjorn Andersson092b4552018-05-17 23:26:37 -07001363 struct list_head *clk_list = &hba->clk_list_head;
1364 struct ufs_clk_info *clki;
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001365 struct devfreq *devfreq;
1366 int ret;
1367
Bjorn Andersson092b4552018-05-17 23:26:37 -07001368 /* Skip devfreq if we don't have any clocks in the list */
1369 if (list_empty(clk_list))
1370 return 0;
1371
1372 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1373 dev_pm_opp_add(hba->dev, clki->min_freq, 0);
1374 dev_pm_opp_add(hba->dev, clki->max_freq, 0);
1375
Stanley Chu90b84912020-05-09 17:37:13 +08001376 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile,
1377 &hba->vps->ondemand_data);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001378 devfreq = devfreq_add_device(hba->dev,
Stanley Chu90b84912020-05-09 17:37:13 +08001379 &hba->vps->devfreq_profile,
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001380 DEVFREQ_GOV_SIMPLE_ONDEMAND,
Stanley Chu90b84912020-05-09 17:37:13 +08001381 &hba->vps->ondemand_data);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001382 if (IS_ERR(devfreq)) {
1383 ret = PTR_ERR(devfreq);
1384 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001385
1386 dev_pm_opp_remove(hba->dev, clki->min_freq);
1387 dev_pm_opp_remove(hba->dev, clki->max_freq);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001388 return ret;
1389 }
1390
1391 hba->devfreq = devfreq;
1392
1393 return 0;
1394}
1395
Bjorn Andersson092b4552018-05-17 23:26:37 -07001396static void ufshcd_devfreq_remove(struct ufs_hba *hba)
1397{
1398 struct list_head *clk_list = &hba->clk_list_head;
1399 struct ufs_clk_info *clki;
1400
1401 if (!hba->devfreq)
1402 return;
1403
1404 devfreq_remove_device(hba->devfreq);
1405 hba->devfreq = NULL;
1406
1407 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1408 dev_pm_opp_remove(hba->dev, clki->min_freq);
1409 dev_pm_opp_remove(hba->dev, clki->max_freq);
1410}
1411
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001412static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1413{
1414 unsigned long flags;
1415
1416 devfreq_suspend_device(hba->devfreq);
1417 spin_lock_irqsave(hba->host->host_lock, flags);
1418 hba->clk_scaling.window_start_t = 0;
1419 spin_unlock_irqrestore(hba->host->host_lock, flags);
1420}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001421
Gilad Bronera5082532016-10-17 17:10:00 -07001422static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1423{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001424 unsigned long flags;
1425 bool suspend = false;
1426
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001427 if (!ufshcd_is_clkscaling_supported(hba))
1428 return;
1429
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001430 spin_lock_irqsave(hba->host->host_lock, flags);
1431 if (!hba->clk_scaling.is_suspended) {
1432 suspend = true;
1433 hba->clk_scaling.is_suspended = true;
1434 }
1435 spin_unlock_irqrestore(hba->host->host_lock, flags);
1436
1437 if (suspend)
1438 __ufshcd_suspend_clkscaling(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07001439}
1440
1441static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1442{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001443 unsigned long flags;
1444 bool resume = false;
1445
1446 if (!ufshcd_is_clkscaling_supported(hba))
1447 return;
1448
1449 spin_lock_irqsave(hba->host->host_lock, flags);
1450 if (hba->clk_scaling.is_suspended) {
1451 resume = true;
1452 hba->clk_scaling.is_suspended = false;
1453 }
1454 spin_unlock_irqrestore(hba->host->host_lock, flags);
1455
1456 if (resume)
1457 devfreq_resume_device(hba->devfreq);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001458}
1459
1460static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1461 struct device_attribute *attr, char *buf)
1462{
1463 struct ufs_hba *hba = dev_get_drvdata(dev);
1464
1465 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
1466}
1467
1468static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1469 struct device_attribute *attr, const char *buf, size_t count)
1470{
1471 struct ufs_hba *hba = dev_get_drvdata(dev);
1472 u32 value;
1473 int err;
1474
1475 if (kstrtou32(buf, 0, &value))
1476 return -EINVAL;
1477
1478 value = !!value;
1479 if (value == hba->clk_scaling.is_allowed)
1480 goto out;
1481
1482 pm_runtime_get_sync(hba->dev);
1483 ufshcd_hold(hba, false);
1484
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001485 cancel_work_sync(&hba->clk_scaling.suspend_work);
1486 cancel_work_sync(&hba->clk_scaling.resume_work);
1487
1488 hba->clk_scaling.is_allowed = value;
1489
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001490 if (value) {
1491 ufshcd_resume_clkscaling(hba);
1492 } else {
1493 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001494 err = ufshcd_devfreq_scale(hba, true);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001495 if (err)
1496 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1497 __func__, err);
1498 }
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001499
1500 ufshcd_release(hba);
1501 pm_runtime_put_sync(hba->dev);
1502out:
1503 return count;
Gilad Bronera5082532016-10-17 17:10:00 -07001504}
1505
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001506static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
1507{
1508 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1509 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1510 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1511 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1512 hba->clk_scaling.enable_attr.attr.mode = 0644;
1513 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1514 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1515}
1516
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001517static void ufshcd_ungate_work(struct work_struct *work)
1518{
1519 int ret;
1520 unsigned long flags;
1521 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1522 clk_gating.ungate_work);
1523
1524 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1525
1526 spin_lock_irqsave(hba->host->host_lock, flags);
1527 if (hba->clk_gating.state == CLKS_ON) {
1528 spin_unlock_irqrestore(hba->host->host_lock, flags);
1529 goto unblock_reqs;
1530 }
1531
1532 spin_unlock_irqrestore(hba->host->host_lock, flags);
1533 ufshcd_setup_clocks(hba, true);
1534
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001535 ufshcd_enable_irq(hba);
1536
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001537 /* Exit from hibern8 */
1538 if (ufshcd_can_hibern8_during_gating(hba)) {
1539 /* Prevent gating in this path */
1540 hba->clk_gating.is_suspended = true;
1541 if (ufshcd_is_link_hibern8(hba)) {
1542 ret = ufshcd_uic_hibern8_exit(hba);
1543 if (ret)
1544 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1545 __func__, ret);
1546 else
1547 ufshcd_set_link_active(hba);
1548 }
1549 hba->clk_gating.is_suspended = false;
1550 }
1551unblock_reqs:
Subhash Jadavani38135532018-05-03 16:37:18 +05301552 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001553}
1554
1555/**
1556 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1557 * Also, exit from hibern8 mode and set the link as active.
1558 * @hba: per adapter instance
1559 * @async: This indicates whether caller should ungate clocks asynchronously.
1560 */
1561int ufshcd_hold(struct ufs_hba *hba, bool async)
1562{
1563 int rc = 0;
Stanley Chu93b6c5d2020-08-09 13:07:34 +08001564 bool flush_result;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001565 unsigned long flags;
1566
1567 if (!ufshcd_is_clkgating_allowed(hba))
1568 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001569 spin_lock_irqsave(hba->host->host_lock, flags);
1570 hba->clk_gating.active_reqs++;
1571
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001572 if (ufshcd_eh_in_progress(hba)) {
1573 spin_unlock_irqrestore(hba->host->host_lock, flags);
1574 return 0;
1575 }
1576
Sahitya Tummala856b3482014-09-25 15:32:34 +03001577start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001578 switch (hba->clk_gating.state) {
1579 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001580 /*
1581 * Wait for the ungate work to complete if in progress.
1582 * Though the clocks may be in ON state, the link could
1583 * still be in hibner8 state if hibern8 is allowed
1584 * during clock gating.
1585 * Make sure we exit hibern8 state also in addition to
1586 * clocks being ON.
1587 */
1588 if (ufshcd_can_hibern8_during_gating(hba) &&
1589 ufshcd_is_link_hibern8(hba)) {
Can Guoc63d6092020-02-10 19:40:48 -08001590 if (async) {
1591 rc = -EAGAIN;
1592 hba->clk_gating.active_reqs--;
1593 break;
1594 }
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001595 spin_unlock_irqrestore(hba->host->host_lock, flags);
Stanley Chu93b6c5d2020-08-09 13:07:34 +08001596 flush_result = flush_work(&hba->clk_gating.ungate_work);
1597 if (hba->clk_gating.is_suspended && !flush_result)
1598 goto out;
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001599 spin_lock_irqsave(hba->host->host_lock, flags);
1600 goto start;
1601 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001602 break;
1603 case REQ_CLKS_OFF:
1604 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1605 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001606 trace_ufshcd_clk_gating(dev_name(hba->dev),
1607 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001608 break;
1609 }
1610 /*
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +03001611 * If we are here, it means gating work is either done or
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001612 * currently running. Hence, fall through to cancel gating
1613 * work and to enable clocks.
1614 */
Tomas Winkler30eb2e42018-11-26 10:10:34 +02001615 /* fallthrough */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001616 case CLKS_OFF:
Subhash Jadavani38135532018-05-03 16:37:18 +05301617 ufshcd_scsi_block_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001618 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001619 trace_ufshcd_clk_gating(dev_name(hba->dev),
1620 hba->clk_gating.state);
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301621 queue_work(hba->clk_gating.clk_gating_workq,
1622 &hba->clk_gating.ungate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001623 /*
1624 * fall through to check if we should wait for this
1625 * work to be done or not.
1626 */
Tomas Winkler30eb2e42018-11-26 10:10:34 +02001627 /* fallthrough */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001628 case REQ_CLKS_ON:
1629 if (async) {
1630 rc = -EAGAIN;
1631 hba->clk_gating.active_reqs--;
1632 break;
1633 }
1634
1635 spin_unlock_irqrestore(hba->host->host_lock, flags);
1636 flush_work(&hba->clk_gating.ungate_work);
1637 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001638 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001639 goto start;
1640 default:
1641 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1642 __func__, hba->clk_gating.state);
1643 break;
1644 }
1645 spin_unlock_irqrestore(hba->host->host_lock, flags);
1646out:
1647 return rc;
1648}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001649EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001650
1651static void ufshcd_gate_work(struct work_struct *work)
1652{
1653 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1654 clk_gating.gate_work.work);
1655 unsigned long flags;
1656
1657 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -07001658 /*
1659 * In case you are here to cancel this work the gating state
1660 * would be marked as REQ_CLKS_ON. In this case save time by
1661 * skipping the gating work and exit after changing the clock
1662 * state to CLKS_ON.
1663 */
1664 if (hba->clk_gating.is_suspended ||
Asutosh Das18f013742019-11-14 22:09:29 -08001665 (hba->clk_gating.state != REQ_CLKS_OFF)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001666 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001667 trace_ufshcd_clk_gating(dev_name(hba->dev),
1668 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001669 goto rel_lock;
1670 }
1671
1672 if (hba->clk_gating.active_reqs
1673 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
Bart Van Assche7252a362019-12-09 10:13:08 -08001674 || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001675 || hba->active_uic_cmd || hba->uic_async_done)
1676 goto rel_lock;
1677
1678 spin_unlock_irqrestore(hba->host->host_lock, flags);
1679
1680 /* put the link into hibern8 mode before turning off clocks */
1681 if (ufshcd_can_hibern8_during_gating(hba)) {
1682 if (ufshcd_uic_hibern8_enter(hba)) {
1683 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001684 trace_ufshcd_clk_gating(dev_name(hba->dev),
1685 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001686 goto out;
1687 }
1688 ufshcd_set_link_hibern8(hba);
1689 }
1690
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001691 ufshcd_disable_irq(hba);
1692
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001693 if (!ufshcd_is_link_active(hba))
1694 ufshcd_setup_clocks(hba, false);
1695 else
1696 /* If link is active, device ref_clk can't be switched off */
1697 __ufshcd_setup_clocks(hba, false, true);
1698
1699 /*
1700 * In case you are here to cancel this work the gating state
1701 * would be marked as REQ_CLKS_ON. In this case keep the state
1702 * as REQ_CLKS_ON which would anyway imply that clocks are off
1703 * and a request to turn them on is pending. By doing this way,
1704 * we keep the state machine in tact and this would ultimately
1705 * prevent from doing cancel work multiple times when there are
1706 * new requests arriving before the current cancel work is done.
1707 */
1708 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001709 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001710 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001711 trace_ufshcd_clk_gating(dev_name(hba->dev),
1712 hba->clk_gating.state);
1713 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001714rel_lock:
1715 spin_unlock_irqrestore(hba->host->host_lock, flags);
1716out:
1717 return;
1718}
1719
1720/* host lock must be held before calling this variant */
1721static void __ufshcd_release(struct ufs_hba *hba)
1722{
1723 if (!ufshcd_is_clkgating_allowed(hba))
1724 return;
1725
1726 hba->clk_gating.active_reqs--;
1727
1728 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1729 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
Bart Van Assche7252a362019-12-09 10:13:08 -08001730 || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001731 || hba->active_uic_cmd || hba->uic_async_done
1732 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001733 return;
1734
1735 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001736 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Evan Greenf4bb7702018-10-05 10:27:32 -07001737 queue_delayed_work(hba->clk_gating.clk_gating_workq,
1738 &hba->clk_gating.gate_work,
1739 msecs_to_jiffies(hba->clk_gating.delay_ms));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001740}
1741
1742void ufshcd_release(struct ufs_hba *hba)
1743{
1744 unsigned long flags;
1745
1746 spin_lock_irqsave(hba->host->host_lock, flags);
1747 __ufshcd_release(hba);
1748 spin_unlock_irqrestore(hba->host->host_lock, flags);
1749}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001750EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001751
1752static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1753 struct device_attribute *attr, char *buf)
1754{
1755 struct ufs_hba *hba = dev_get_drvdata(dev);
1756
1757 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1758}
1759
1760static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1761 struct device_attribute *attr, const char *buf, size_t count)
1762{
1763 struct ufs_hba *hba = dev_get_drvdata(dev);
1764 unsigned long flags, value;
1765
1766 if (kstrtoul(buf, 0, &value))
1767 return -EINVAL;
1768
1769 spin_lock_irqsave(hba->host->host_lock, flags);
1770 hba->clk_gating.delay_ms = value;
1771 spin_unlock_irqrestore(hba->host->host_lock, flags);
1772 return count;
1773}
1774
Sahitya Tummalab4274112016-12-22 18:40:39 -08001775static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1776 struct device_attribute *attr, char *buf)
1777{
1778 struct ufs_hba *hba = dev_get_drvdata(dev);
1779
1780 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1781}
1782
1783static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1784 struct device_attribute *attr, const char *buf, size_t count)
1785{
1786 struct ufs_hba *hba = dev_get_drvdata(dev);
1787 unsigned long flags;
1788 u32 value;
1789
1790 if (kstrtou32(buf, 0, &value))
1791 return -EINVAL;
1792
1793 value = !!value;
1794 if (value == hba->clk_gating.is_enabled)
1795 goto out;
1796
1797 if (value) {
1798 ufshcd_release(hba);
1799 } else {
1800 spin_lock_irqsave(hba->host->host_lock, flags);
1801 hba->clk_gating.active_reqs++;
1802 spin_unlock_irqrestore(hba->host->host_lock, flags);
1803 }
1804
1805 hba->clk_gating.is_enabled = value;
1806out:
1807 return count;
1808}
1809
Vivek Gautameebcc192018-08-07 23:17:39 +05301810static void ufshcd_init_clk_scaling(struct ufs_hba *hba)
1811{
1812 char wq_name[sizeof("ufs_clkscaling_00")];
1813
1814 if (!ufshcd_is_clkscaling_supported(hba))
1815 return;
1816
1817 INIT_WORK(&hba->clk_scaling.suspend_work,
1818 ufshcd_clk_scaling_suspend_work);
1819 INIT_WORK(&hba->clk_scaling.resume_work,
1820 ufshcd_clk_scaling_resume_work);
1821
1822 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d",
1823 hba->host->host_no);
1824 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
1825
1826 ufshcd_clkscaling_init_sysfs(hba);
1827}
1828
1829static void ufshcd_exit_clk_scaling(struct ufs_hba *hba)
1830{
1831 if (!ufshcd_is_clkscaling_supported(hba))
1832 return;
1833
1834 destroy_workqueue(hba->clk_scaling.workq);
1835 ufshcd_devfreq_remove(hba);
1836}
1837
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001838static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1839{
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301840 char wq_name[sizeof("ufs_clk_gating_00")];
1841
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001842 if (!ufshcd_is_clkgating_allowed(hba))
1843 return;
1844
1845 hba->clk_gating.delay_ms = 150;
1846 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1847 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1848
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301849 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
1850 hba->host->host_no);
1851 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
1852 WQ_MEM_RECLAIM);
1853
Sahitya Tummalab4274112016-12-22 18:40:39 -08001854 hba->clk_gating.is_enabled = true;
1855
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001856 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1857 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1858 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1859 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
Sahitya Tummalab4274112016-12-22 18:40:39 -08001860 hba->clk_gating.delay_attr.attr.mode = 0644;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001861 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1862 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Sahitya Tummalab4274112016-12-22 18:40:39 -08001863
1864 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1865 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1866 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1867 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1868 hba->clk_gating.enable_attr.attr.mode = 0644;
1869 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1870 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001871}
1872
1873static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1874{
1875 if (!ufshcd_is_clkgating_allowed(hba))
1876 return;
1877 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001878 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001879 cancel_work_sync(&hba->clk_gating.ungate_work);
1880 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301881 destroy_workqueue(hba->clk_gating.clk_gating_workq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001882}
1883
Sahitya Tummala856b3482014-09-25 15:32:34 +03001884/* Must be called with host lock acquired */
1885static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1886{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001887 bool queue_resume_work = false;
Stanley Chub1bf66d2020-06-11 18:10:43 +08001888 ktime_t curr_t = ktime_get();
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001889
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001890 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001891 return;
1892
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001893 if (!hba->clk_scaling.active_reqs++)
1894 queue_resume_work = true;
1895
1896 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1897 return;
1898
1899 if (queue_resume_work)
1900 queue_work(hba->clk_scaling.workq,
1901 &hba->clk_scaling.resume_work);
1902
1903 if (!hba->clk_scaling.window_start_t) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08001904 hba->clk_scaling.window_start_t = curr_t;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001905 hba->clk_scaling.tot_busy_t = 0;
1906 hba->clk_scaling.is_busy_started = false;
1907 }
1908
Sahitya Tummala856b3482014-09-25 15:32:34 +03001909 if (!hba->clk_scaling.is_busy_started) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08001910 hba->clk_scaling.busy_start_t = curr_t;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001911 hba->clk_scaling.is_busy_started = true;
1912 }
1913}
1914
1915static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1916{
1917 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1918
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001919 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001920 return;
1921
1922 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1923 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1924 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001925 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001926 scaling->is_busy_started = false;
1927 }
1928}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301929/**
1930 * ufshcd_send_command - Send SCSI or device management commands
1931 * @hba: per adapter instance
1932 * @task_tag: Task tag of the command
1933 */
1934static inline
1935void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1936{
Stanley Chu6edfdcf2020-07-06 14:07:07 +08001937 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag];
1938
1939 lrbp->issue_time_stamp = ktime_get();
1940 lrbp->compl_time_stamp = ktime_set(0, 0);
1941 ufshcd_vops_setup_xfer_req(hba, task_tag, (lrbp->cmd ? true : false));
Bart Van Asscheeacf36f2019-12-24 14:02:46 -08001942 ufshcd_add_command_trace(hba, task_tag, "send");
Sahitya Tummala856b3482014-09-25 15:32:34 +03001943 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301944 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301945 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001946 /* Make sure that doorbell is committed immediately */
1947 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301948}
1949
1950/**
1951 * ufshcd_copy_sense_data - Copy sense data in case of check condition
Bart Van Assche8aa29f12018-03-01 15:07:20 -08001952 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301953 */
1954static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1955{
1956 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301957 if (lrbp->sense_buffer &&
1958 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001959 int len_to_copy;
1960
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301961 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Avri Altman09a5a242018-11-22 20:04:56 +02001962 len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001963
Avri Altman09a5a242018-11-22 20:04:56 +02001964 memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data,
1965 len_to_copy);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301966 }
1967}
1968
1969/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301970 * ufshcd_copy_query_response() - Copy the Query Response and the data
1971 * descriptor
1972 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08001973 * @lrbp: pointer to local reference block
Dolev Raviv68078d52013-07-30 00:35:58 +05301974 */
1975static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001976int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05301977{
1978 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1979
Dolev Raviv68078d52013-07-30 00:35:58 +05301980 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301981
Dolev Raviv68078d52013-07-30 00:35:58 +05301982 /* Get the descriptor */
Avri Altman1c908362019-05-21 11:24:22 +03001983 if (hba->dev_cmd.query.descriptor &&
1984 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001985 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05301986 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001987 u16 resp_len;
1988 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05301989
1990 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001991 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301992 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001993 buf_len = be16_to_cpu(
1994 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001995 if (likely(buf_len >= resp_len)) {
1996 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
1997 } else {
1998 dev_warn(hba->dev,
Bean Huo3d4881d2019-11-12 23:34:35 +01001999 "%s: rsp size %d is bigger than buffer size %d",
2000 __func__, resp_len, buf_len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002001 return -EINVAL;
2002 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302003 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002004
2005 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302006}
2007
2008/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302009 * ufshcd_hba_capabilities - Read controller capabilities
2010 * @hba: per adapter instance
Satya Tangiraladf043c742020-07-06 20:04:14 +00002011 *
2012 * Return: 0 on success, negative on error.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302013 */
Satya Tangiraladf043c742020-07-06 20:04:14 +00002014static inline int ufshcd_hba_capabilities(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302015{
Satya Tangiraladf043c742020-07-06 20:04:14 +00002016 int err;
2017
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302018 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302019
2020 /* nutrs and nutmrs are 0 based values */
2021 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2022 hba->nutmrs =
2023 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002024
2025 /* Read crypto capabilities */
2026 err = ufshcd_hba_init_crypto_capabilities(hba);
2027 if (err)
2028 dev_err(hba->dev, "crypto setup failed\n");
2029
2030 return err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302031}
2032
2033/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302034 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2035 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302036 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302037 * Return true on success, else false
2038 */
2039static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2040{
2041 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2042 return true;
2043 else
2044 return false;
2045}
2046
2047/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302048 * ufshcd_get_upmcrs - Get the power mode change request status
2049 * @hba: Pointer to adapter instance
2050 *
2051 * This function gets the UPMCRS field of HCS register
2052 * Returns value of UPMCRS field
2053 */
2054static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2055{
2056 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2057}
2058
2059/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302060 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
2061 * @hba: per adapter instance
2062 * @uic_cmd: UIC command
2063 *
2064 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302065 */
2066static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302067ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302068{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302069 WARN_ON(hba->active_uic_cmd);
2070
2071 hba->active_uic_cmd = uic_cmd;
2072
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302073 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302074 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2075 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2076 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302077
Stanley Chuaa5c6972020-06-15 15:22:35 +08002078 ufshcd_add_uic_command_trace(hba, uic_cmd, "send");
2079
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302080 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302081 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302082 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302083}
2084
2085/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302086 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
2087 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002088 * @uic_cmd: UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302089 *
2090 * Must be called with mutex held.
2091 * Returns 0 only if success.
2092 */
2093static int
2094ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2095{
2096 int ret;
2097 unsigned long flags;
2098
2099 if (wait_for_completion_timeout(&uic_cmd->done,
2100 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
2101 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2102 else
2103 ret = -ETIMEDOUT;
2104
2105 spin_lock_irqsave(hba->host->host_lock, flags);
2106 hba->active_uic_cmd = NULL;
2107 spin_unlock_irqrestore(hba->host->host_lock, flags);
2108
2109 return ret;
2110}
2111
2112/**
2113 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2114 * @hba: per adapter instance
2115 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002116 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302117 *
2118 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002119 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302120 * Returns 0 only if success.
2121 */
2122static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002123__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2124 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302125{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302126 if (!ufshcd_ready_for_uic_cmd(hba)) {
2127 dev_err(hba->dev,
2128 "Controller not ready to accept UIC commands\n");
2129 return -EIO;
2130 }
2131
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002132 if (completion)
2133 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302134
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302135 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302136
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002137 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302138}
2139
2140/**
2141 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2142 * @hba: per adapter instance
2143 * @uic_cmd: UIC command
2144 *
2145 * Returns 0 only if success.
2146 */
Avri Altmane77044c52018-10-07 17:30:39 +03002147int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302148{
2149 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002150 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302151
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002152 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302153 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002154 ufshcd_add_delay_before_dme_cmd(hba);
2155
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002156 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002157 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002158 spin_unlock_irqrestore(hba->host->host_lock, flags);
2159 if (!ret)
2160 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2161
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302162 mutex_unlock(&hba->uic_cmd_mutex);
2163
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002164 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302165 return ret;
2166}
2167
2168/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302169 * ufshcd_map_sg - Map scatter-gather list to prdt
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002170 * @hba: per adapter instance
2171 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302172 *
2173 * Returns 0 in case of success, non-zero value in case of failure
2174 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002175static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302176{
2177 struct ufshcd_sg_entry *prd_table;
2178 struct scatterlist *sg;
2179 struct scsi_cmnd *cmd;
2180 int sg_segments;
2181 int i;
2182
2183 cmd = lrbp->cmd;
2184 sg_segments = scsi_dma_map(cmd);
2185 if (sg_segments < 0)
2186 return sg_segments;
2187
2188 if (sg_segments) {
Alim Akhtar26f968d2020-05-28 06:46:52 +05302189
2190 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2191 lrbp->utr_descriptor_ptr->prd_table_length =
2192 cpu_to_le16((sg_segments *
2193 sizeof(struct ufshcd_sg_entry)));
2194 else
2195 lrbp->utr_descriptor_ptr->prd_table_length =
2196 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302197
2198 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2199
2200 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2201 prd_table[i].size =
2202 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2203 prd_table[i].base_addr =
2204 cpu_to_le32(lower_32_bits(sg->dma_address));
2205 prd_table[i].upper_addr =
2206 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002207 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302208 }
2209 } else {
2210 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2211 }
2212
2213 return 0;
2214}
2215
2216/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302217 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302218 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302219 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302220 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302221static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302222{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302223 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2224
2225 if (hba->ufs_version == UFSHCI_VERSION_10) {
2226 u32 rw;
2227 rw = set & INTERRUPT_MASK_RW_VER_10;
2228 set = rw | ((set ^ intrs) & intrs);
2229 } else {
2230 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302231 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302232
2233 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2234}
2235
2236/**
2237 * ufshcd_disable_intr - disable interrupts
2238 * @hba: per adapter instance
2239 * @intrs: interrupt bits
2240 */
2241static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2242{
2243 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2244
2245 if (hba->ufs_version == UFSHCI_VERSION_10) {
2246 u32 rw;
2247 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2248 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2249 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2250
2251 } else {
2252 set &= ~intrs;
2253 }
2254
2255 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302256}
2257
2258/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302259 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2260 * descriptor according to request
2261 * @lrbp: pointer to local reference block
2262 * @upiu_flags: flags required in the header
2263 * @cmd_dir: requests data direction
2264 */
2265static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Bean Huoa23064c2020-07-06 14:39:36 +02002266 u8 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302267{
2268 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2269 u32 data_direction;
2270 u32 dword_0;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002271 u32 dword_1 = 0;
2272 u32 dword_3 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302273
2274 if (cmd_dir == DMA_FROM_DEVICE) {
2275 data_direction = UTP_DEVICE_TO_HOST;
2276 *upiu_flags = UPIU_CMD_FLAGS_READ;
2277 } else if (cmd_dir == DMA_TO_DEVICE) {
2278 data_direction = UTP_HOST_TO_DEVICE;
2279 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2280 } else {
2281 data_direction = UTP_NO_DATA_TRANSFER;
2282 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2283 }
2284
2285 dword_0 = data_direction | (lrbp->command_type
2286 << UPIU_COMMAND_TYPE_OFFSET);
2287 if (lrbp->intr_cmd)
2288 dword_0 |= UTP_REQ_DESC_INT_CMD;
2289
Satya Tangiraladf043c742020-07-06 20:04:14 +00002290 /* Prepare crypto related dwords */
2291 ufshcd_prepare_req_desc_hdr_crypto(lrbp, &dword_0, &dword_1, &dword_3);
2292
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302293 /* Transfer request descriptor header fields */
2294 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002295 req_desc->header.dword_1 = cpu_to_le32(dword_1);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302296 /*
2297 * assigning invalid value for command status. Controller
2298 * updates OCS on command completion, with the command
2299 * status
2300 */
2301 req_desc->header.dword_2 =
2302 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002303 req_desc->header.dword_3 = cpu_to_le32(dword_3);
Yaniv Gardi51047262016-02-01 15:02:38 +02002304
2305 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302306}
2307
2308/**
2309 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2310 * for scsi commands
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002311 * @lrbp: local reference block pointer
2312 * @upiu_flags: flags
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302313 */
2314static
Bean Huoa23064c2020-07-06 14:39:36 +02002315void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302316{
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002317 struct scsi_cmnd *cmd = lrbp->cmd;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302318 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002319 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302320
2321 /* command descriptor fields */
2322 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2323 UPIU_TRANSACTION_COMMAND, upiu_flags,
2324 lrbp->lun, lrbp->task_tag);
2325 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2326 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2327
2328 /* Total EHS length and Data segment length will be zero */
2329 ucd_req_ptr->header.dword_2 = 0;
2330
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002331 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302332
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002333 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE);
Avri Altmana851b2b2018-10-07 17:30:34 +03002334 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002335 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002336
2337 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302338}
2339
Dolev Raviv68078d52013-07-30 00:35:58 +05302340/**
2341 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2342 * for query requsts
2343 * @hba: UFS hba
2344 * @lrbp: local reference block pointer
2345 * @upiu_flags: flags
2346 */
2347static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
Bean Huoa23064c2020-07-06 14:39:36 +02002348 struct ufshcd_lrb *lrbp, u8 upiu_flags)
Dolev Raviv68078d52013-07-30 00:35:58 +05302349{
2350 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2351 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302352 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302353
2354 /* Query request header */
2355 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2356 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2357 lrbp->lun, lrbp->task_tag);
2358 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2359 0, query->request.query_func, 0, 0);
2360
Zang Leigang68612852016-08-25 17:39:19 +08002361 /* Data segment length only need for WRITE_DESC */
2362 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2363 ucd_req_ptr->header.dword_2 =
2364 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2365 else
2366 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302367
2368 /* Copy the Query Request buffer as is */
2369 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2370 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302371
2372 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002373 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
Avri Altman220d17a62018-10-07 17:30:36 +03002374 memcpy(ucd_req_ptr + 1, query->descriptor, len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002375
Yaniv Gardi51047262016-02-01 15:02:38 +02002376 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302377}
2378
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302379static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2380{
2381 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2382
2383 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2384
2385 /* command descriptor fields */
2386 ucd_req_ptr->header.dword_0 =
2387 UPIU_HEADER_DWORD(
2388 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002389 /* clear rest of the fields of basic header */
2390 ucd_req_ptr->header.dword_1 = 0;
2391 ucd_req_ptr->header.dword_2 = 0;
2392
2393 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302394}
2395
2396/**
Joao Pinto300bb132016-05-11 12:21:27 +01002397 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
2398 * for Device Management Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002399 * @hba: per adapter instance
2400 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302401 */
Joao Pinto300bb132016-05-11 12:21:27 +01002402static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302403{
Bean Huoa23064c2020-07-06 14:39:36 +02002404 u8 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302405 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302406
kehuanlin83dc7e32017-09-06 17:58:39 +08002407 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2408 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002409 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
kehuanlin83dc7e32017-09-06 17:58:39 +08002410 else
2411 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002412
2413 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2414 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2415 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2416 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2417 ufshcd_prepare_utp_nop_upiu(lrbp);
2418 else
2419 ret = -EINVAL;
2420
2421 return ret;
2422}
2423
2424/**
2425 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2426 * for SCSI Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002427 * @hba: per adapter instance
2428 * @lrbp: pointer to local reference block
Joao Pinto300bb132016-05-11 12:21:27 +01002429 */
2430static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2431{
Bean Huoa23064c2020-07-06 14:39:36 +02002432 u8 upiu_flags;
Joao Pinto300bb132016-05-11 12:21:27 +01002433 int ret = 0;
2434
kehuanlin83dc7e32017-09-06 17:58:39 +08002435 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2436 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002437 lrbp->command_type = UTP_CMD_TYPE_SCSI;
kehuanlin83dc7e32017-09-06 17:58:39 +08002438 else
2439 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002440
2441 if (likely(lrbp->cmd)) {
2442 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2443 lrbp->cmd->sc_data_direction);
2444 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2445 } else {
2446 ret = -EINVAL;
2447 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302448
2449 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302450}
2451
2452/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002453 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002454 * @upiu_wlun_id: UPIU W-LUN id
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002455 *
2456 * Returns SCSI W-LUN id
2457 */
2458static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2459{
2460 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2461}
2462
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08002463static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i)
2464{
2465 struct utp_transfer_cmd_desc *cmd_descp = hba->ucdl_base_addr;
2466 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr;
2467 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr +
2468 i * sizeof(struct utp_transfer_cmd_desc);
2469 u16 response_offset = offsetof(struct utp_transfer_cmd_desc,
2470 response_upiu);
2471 u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
2472
2473 lrb->utr_descriptor_ptr = utrdlp + i;
2474 lrb->utrd_dma_addr = hba->utrdl_dma_addr +
2475 i * sizeof(struct utp_transfer_req_desc);
2476 lrb->ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i);
2477 lrb->ucd_req_dma_addr = cmd_desc_element_addr;
2478 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2479 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset;
2480 lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2481 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset;
2482}
2483
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002484/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302485 * ufshcd_queuecommand - main entry point for SCSI requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002486 * @host: SCSI host pointer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302487 * @cmd: command from SCSI Midlayer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302488 *
2489 * Returns 0 for success, non-zero in case of failure
2490 */
2491static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2492{
2493 struct ufshcd_lrb *lrbp;
2494 struct ufs_hba *hba;
2495 unsigned long flags;
2496 int tag;
2497 int err = 0;
2498
2499 hba = shost_priv(host);
2500
2501 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002502 if (!ufshcd_valid_tag(hba, tag)) {
2503 dev_err(hba->dev,
2504 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2505 __func__, tag, cmd, cmd->request);
2506 BUG();
2507 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302508
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002509 if (!down_read_trylock(&hba->clk_scaling_lock))
2510 return SCSI_MLQUEUE_HOST_BUSY;
2511
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302512 spin_lock_irqsave(hba->host->host_lock, flags);
2513 switch (hba->ufshcd_state) {
2514 case UFSHCD_STATE_OPERATIONAL:
2515 break;
Zang Leigang141f8162016-11-16 11:29:37 +08002516 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302517 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302518 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302519 goto out_unlock;
2520 case UFSHCD_STATE_ERROR:
2521 set_host_byte(cmd, DID_ERROR);
2522 cmd->scsi_done(cmd);
2523 goto out_unlock;
2524 default:
2525 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2526 __func__, hba->ufshcd_state);
2527 set_host_byte(cmd, DID_BAD_TARGET);
2528 cmd->scsi_done(cmd);
2529 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302530 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002531
2532 /* if error handling is in progress, don't issue commands */
2533 if (ufshcd_eh_in_progress(hba)) {
2534 set_host_byte(cmd, DID_ERROR);
2535 cmd->scsi_done(cmd);
2536 goto out_unlock;
2537 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302538 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302539
Gilad Broner7fabb772017-02-03 16:56:50 -08002540 hba->req_abort_count = 0;
2541
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002542 err = ufshcd_hold(hba, true);
2543 if (err) {
2544 err = SCSI_MLQUEUE_HOST_BUSY;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002545 goto out;
2546 }
2547 WARN_ON(hba->clk_gating.state != CLKS_ON);
2548
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302549 lrbp = &hba->lrb[tag];
2550
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302551 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302552 lrbp->cmd = cmd;
Avri Altman09a5a242018-11-22 20:04:56 +02002553 lrbp->sense_bufflen = UFS_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302554 lrbp->sense_buffer = cmd->sense_buffer;
2555 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002556 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002557 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002558
2559 ufshcd_prepare_lrbp_crypto(cmd->request, lrbp);
2560
Gilad Bronere0b299e2017-02-03 16:56:40 -08002561 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302562
Joao Pinto300bb132016-05-11 12:21:27 +01002563 ufshcd_comp_scsi_upiu(hba, lrbp);
2564
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002565 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302566 if (err) {
2567 lrbp->cmd = NULL;
Can Guo17c7d352019-12-05 02:14:33 +00002568 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302569 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302570 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002571 /* Make sure descriptors are ready before ringing the doorbell */
2572 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302573
2574 /* issue command to the controller */
2575 spin_lock_irqsave(hba->host->host_lock, flags);
2576 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302577out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302578 spin_unlock_irqrestore(hba->host->host_lock, flags);
2579out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002580 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302581 return err;
2582}
2583
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302584static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2585 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2586{
2587 lrbp->cmd = NULL;
2588 lrbp->sense_bufflen = 0;
2589 lrbp->sense_buffer = NULL;
2590 lrbp->task_tag = tag;
2591 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302592 lrbp->intr_cmd = true; /* No interrupt aggregation */
Satya Tangiraladf043c742020-07-06 20:04:14 +00002593 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302594 hba->dev_cmd.type = cmd_type;
2595
Joao Pinto300bb132016-05-11 12:21:27 +01002596 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302597}
2598
2599static int
2600ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2601{
2602 int err = 0;
2603 unsigned long flags;
2604 u32 mask = 1 << tag;
2605
2606 /* clear outstanding transaction before retry */
2607 spin_lock_irqsave(hba->host->host_lock, flags);
2608 ufshcd_utrl_clear(hba, tag);
2609 spin_unlock_irqrestore(hba->host->host_lock, flags);
2610
2611 /*
2612 * wait for for h/w to clear corresponding bit in door-bell.
2613 * max. wait is 1 sec.
2614 */
2615 err = ufshcd_wait_for_register(hba,
2616 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Bart Van Assche5cac1092020-05-07 15:27:50 -07002617 mask, ~mask, 1000, 1000);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302618
2619 return err;
2620}
2621
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002622static int
2623ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2624{
2625 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2626
2627 /* Get the UPIU response */
2628 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2629 UPIU_RSP_CODE_OFFSET;
2630 return query_res->response;
2631}
2632
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302633/**
2634 * ufshcd_dev_cmd_completion() - handles device management command responses
2635 * @hba: per adapter instance
2636 * @lrbp: pointer to local reference block
2637 */
2638static int
2639ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2640{
2641 int resp;
2642 int err = 0;
2643
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002644 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302645 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2646
2647 switch (resp) {
2648 case UPIU_TRANSACTION_NOP_IN:
2649 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2650 err = -EINVAL;
2651 dev_err(hba->dev, "%s: unexpected response %x\n",
2652 __func__, resp);
2653 }
2654 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302655 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002656 err = ufshcd_check_query_response(hba, lrbp);
2657 if (!err)
2658 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302659 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302660 case UPIU_TRANSACTION_REJECT_UPIU:
2661 /* TODO: handle Reject UPIU Response */
2662 err = -EPERM;
2663 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2664 __func__);
2665 break;
2666 default:
2667 err = -EINVAL;
2668 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2669 __func__, resp);
2670 break;
2671 }
2672
2673 return err;
2674}
2675
2676static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2677 struct ufshcd_lrb *lrbp, int max_timeout)
2678{
2679 int err = 0;
2680 unsigned long time_left;
2681 unsigned long flags;
2682
2683 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2684 msecs_to_jiffies(max_timeout));
2685
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002686 /* Make sure descriptors are ready before ringing the doorbell */
2687 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302688 spin_lock_irqsave(hba->host->host_lock, flags);
2689 hba->dev_cmd.complete = NULL;
2690 if (likely(time_left)) {
2691 err = ufshcd_get_tr_ocs(lrbp);
2692 if (!err)
2693 err = ufshcd_dev_cmd_completion(hba, lrbp);
2694 }
2695 spin_unlock_irqrestore(hba->host->host_lock, flags);
2696
2697 if (!time_left) {
2698 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002699 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2700 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302701 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002702 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302703 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002704 /*
2705 * in case of an error, after clearing the doorbell,
2706 * we also need to clear the outstanding_request
2707 * field in hba
2708 */
2709 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302710 }
2711
2712 return err;
2713}
2714
2715/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302716 * ufshcd_exec_dev_cmd - API for sending device management requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002717 * @hba: UFS hba
2718 * @cmd_type: specifies the type (NOP, Query...)
2719 * @timeout: time in seconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302720 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302721 * NOTE: Since there is only one available tag for device management commands,
2722 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302723 */
2724static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2725 enum dev_cmd_type cmd_type, int timeout)
2726{
Bart Van Assche7252a362019-12-09 10:13:08 -08002727 struct request_queue *q = hba->cmd_queue;
2728 struct request *req;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302729 struct ufshcd_lrb *lrbp;
2730 int err;
2731 int tag;
2732 struct completion wait;
2733 unsigned long flags;
2734
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002735 down_read(&hba->clk_scaling_lock);
2736
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302737 /*
2738 * Get free slot, sleep if slots are unavailable.
2739 * Even though we use wait_event() which sleeps indefinitely,
2740 * the maximum wait time is bounded by SCSI request timeout.
2741 */
Bart Van Assche7252a362019-12-09 10:13:08 -08002742 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03002743 if (IS_ERR(req)) {
2744 err = PTR_ERR(req);
2745 goto out_unlock;
2746 }
Bart Van Assche7252a362019-12-09 10:13:08 -08002747 tag = req->tag;
2748 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302749
2750 init_completion(&wait);
2751 lrbp = &hba->lrb[tag];
2752 WARN_ON(lrbp->cmd);
2753 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2754 if (unlikely(err))
2755 goto out_put_tag;
2756
2757 hba->dev_cmd.complete = &wait;
2758
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03002759 ufshcd_add_query_upiu_trace(hba, tag, "query_send");
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002760 /* Make sure descriptors are ready before ringing the doorbell */
2761 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302762 spin_lock_irqsave(hba->host->host_lock, flags);
2763 ufshcd_send_command(hba, tag);
2764 spin_unlock_irqrestore(hba->host->host_lock, flags);
2765
2766 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2767
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03002768 ufshcd_add_query_upiu_trace(hba, tag,
2769 err ? "query_complete_err" : "query_complete");
2770
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302771out_put_tag:
Bart Van Assche7252a362019-12-09 10:13:08 -08002772 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03002773out_unlock:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002774 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302775 return err;
2776}
2777
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302778/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002779 * ufshcd_init_query() - init the query response and request parameters
2780 * @hba: per-adapter instance
2781 * @request: address of the request pointer to be initialized
2782 * @response: address of the response pointer to be initialized
2783 * @opcode: operation to perform
2784 * @idn: flag idn to access
2785 * @index: LU number to access
2786 * @selector: query/flag/descriptor further identification
2787 */
2788static inline void ufshcd_init_query(struct ufs_hba *hba,
2789 struct ufs_query_req **request, struct ufs_query_res **response,
2790 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2791{
2792 *request = &hba->dev_cmd.query.request;
2793 *response = &hba->dev_cmd.query.response;
2794 memset(*request, 0, sizeof(struct ufs_query_req));
2795 memset(*response, 0, sizeof(struct ufs_query_res));
2796 (*request)->upiu_req.opcode = opcode;
2797 (*request)->upiu_req.idn = idn;
2798 (*request)->upiu_req.index = index;
2799 (*request)->upiu_req.selector = selector;
2800}
2801
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002802static int ufshcd_query_flag_retry(struct ufs_hba *hba,
Stanley Chu1f34eed2020-05-08 16:01:12 +08002803 enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002804{
2805 int ret;
2806 int retries;
2807
2808 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
Stanley Chu1f34eed2020-05-08 16:01:12 +08002809 ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res);
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002810 if (ret)
2811 dev_dbg(hba->dev,
2812 "%s: failed with error %d, retries %d\n",
2813 __func__, ret, retries);
2814 else
2815 break;
2816 }
2817
2818 if (ret)
2819 dev_err(hba->dev,
2820 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2821 __func__, opcode, idn, ret, retries);
2822 return ret;
2823}
2824
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002825/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302826 * ufshcd_query_flag() - API function for sending flag query requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002827 * @hba: per-adapter instance
2828 * @opcode: flag query to perform
2829 * @idn: flag idn to access
Stanley Chu1f34eed2020-05-08 16:01:12 +08002830 * @index: flag index to access
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002831 * @flag_res: the flag value after the query request completes
Dolev Raviv68078d52013-07-30 00:35:58 +05302832 *
2833 * Returns 0 for success, non-zero in case of failure
2834 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002835int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Stanley Chu1f34eed2020-05-08 16:01:12 +08002836 enum flag_idn idn, u8 index, bool *flag_res)
Dolev Raviv68078d52013-07-30 00:35:58 +05302837{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002838 struct ufs_query_req *request = NULL;
2839 struct ufs_query_res *response = NULL;
Stanley Chu1f34eed2020-05-08 16:01:12 +08002840 int err, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002841 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302842
2843 BUG_ON(!hba);
2844
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002845 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302846 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002847 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2848 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302849
2850 switch (opcode) {
2851 case UPIU_QUERY_OPCODE_SET_FLAG:
2852 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2853 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2854 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2855 break;
2856 case UPIU_QUERY_OPCODE_READ_FLAG:
2857 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2858 if (!flag_res) {
2859 /* No dummy reads */
2860 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2861 __func__);
2862 err = -EINVAL;
2863 goto out_unlock;
2864 }
2865 break;
2866 default:
2867 dev_err(hba->dev,
2868 "%s: Expected query flag opcode but got = %d\n",
2869 __func__, opcode);
2870 err = -EINVAL;
2871 goto out_unlock;
2872 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302873
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002874 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05302875
2876 if (err) {
2877 dev_err(hba->dev,
2878 "%s: Sending flag query for idn %d failed, err = %d\n",
2879 __func__, idn, err);
2880 goto out_unlock;
2881 }
2882
2883 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302884 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302885 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2886
2887out_unlock:
2888 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002889 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05302890 return err;
2891}
2892
2893/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302894 * ufshcd_query_attr - API function for sending attribute requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002895 * @hba: per-adapter instance
2896 * @opcode: attribute opcode
2897 * @idn: attribute idn to access
2898 * @index: index field
2899 * @selector: selector field
2900 * @attr_val: the attribute value after the query request completes
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302901 *
2902 * Returns 0 for success, non-zero in case of failure
2903*/
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02002904int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
2905 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302906{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002907 struct ufs_query_req *request = NULL;
2908 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302909 int err;
2910
2911 BUG_ON(!hba);
2912
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002913 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302914 if (!attr_val) {
2915 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2916 __func__, opcode);
2917 err = -EINVAL;
2918 goto out;
2919 }
2920
2921 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002922 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2923 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302924
2925 switch (opcode) {
2926 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2927 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302928 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302929 break;
2930 case UPIU_QUERY_OPCODE_READ_ATTR:
2931 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2932 break;
2933 default:
2934 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2935 __func__, opcode);
2936 err = -EINVAL;
2937 goto out_unlock;
2938 }
2939
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002940 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302941
2942 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002943 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2944 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302945 goto out_unlock;
2946 }
2947
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302948 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302949
2950out_unlock:
2951 mutex_unlock(&hba->dev_cmd.lock);
2952out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002953 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302954 return err;
2955}
2956
2957/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002958 * ufshcd_query_attr_retry() - API function for sending query
2959 * attribute with retries
2960 * @hba: per-adapter instance
2961 * @opcode: attribute opcode
2962 * @idn: attribute idn to access
2963 * @index: index field
2964 * @selector: selector field
2965 * @attr_val: the attribute value after the query request
2966 * completes
2967 *
2968 * Returns 0 for success, non-zero in case of failure
2969*/
2970static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2971 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2972 u32 *attr_val)
2973{
2974 int ret = 0;
2975 u32 retries;
2976
Bart Van Assche68c9fcf2019-12-24 14:02:43 -08002977 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002978 ret = ufshcd_query_attr(hba, opcode, idn, index,
2979 selector, attr_val);
2980 if (ret)
2981 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2982 __func__, ret, retries);
2983 else
2984 break;
2985 }
2986
2987 if (ret)
2988 dev_err(hba->dev,
2989 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2990 __func__, idn, ret, QUERY_REQ_RETRIES);
2991 return ret;
2992}
2993
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002994static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002995 enum query_opcode opcode, enum desc_idn idn, u8 index,
2996 u8 selector, u8 *desc_buf, int *buf_len)
2997{
2998 struct ufs_query_req *request = NULL;
2999 struct ufs_query_res *response = NULL;
3000 int err;
3001
3002 BUG_ON(!hba);
3003
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003004 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003005 if (!desc_buf) {
3006 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3007 __func__, opcode);
3008 err = -EINVAL;
3009 goto out;
3010 }
3011
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003012 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003013 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3014 __func__, *buf_len);
3015 err = -EINVAL;
3016 goto out;
3017 }
3018
3019 mutex_lock(&hba->dev_cmd.lock);
3020 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3021 selector);
3022 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003023 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003024
3025 switch (opcode) {
3026 case UPIU_QUERY_OPCODE_WRITE_DESC:
3027 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3028 break;
3029 case UPIU_QUERY_OPCODE_READ_DESC:
3030 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3031 break;
3032 default:
3033 dev_err(hba->dev,
3034 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3035 __func__, opcode);
3036 err = -EINVAL;
3037 goto out_unlock;
3038 }
3039
3040 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3041
3042 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08003043 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3044 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003045 goto out_unlock;
3046 }
3047
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003048 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003049
3050out_unlock:
Bean Huocfcbae32019-11-12 23:34:36 +01003051 hba->dev_cmd.query.descriptor = NULL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003052 mutex_unlock(&hba->dev_cmd.lock);
3053out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003054 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003055 return err;
3056}
3057
3058/**
Bart Van Assche8aa29f12018-03-01 15:07:20 -08003059 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
3060 * @hba: per-adapter instance
3061 * @opcode: attribute opcode
3062 * @idn: attribute idn to access
3063 * @index: index field
3064 * @selector: selector field
3065 * @desc_buf: the buffer that contains the descriptor
3066 * @buf_len: length parameter passed to the device
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003067 *
3068 * Returns 0 for success, non-zero in case of failure.
3069 * The buf_len parameter will contain, on return, the length parameter
3070 * received on the response.
3071 */
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02003072int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
3073 enum query_opcode opcode,
3074 enum desc_idn idn, u8 index,
3075 u8 selector,
3076 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003077{
3078 int err;
3079 int retries;
3080
3081 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3082 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3083 selector, desc_buf, buf_len);
3084 if (!err || err == -EINVAL)
3085 break;
3086 }
3087
3088 return err;
3089}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003090
3091/**
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003092 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
3093 * @hba: Pointer to adapter instance
3094 * @desc_id: descriptor idn value
3095 * @desc_len: mapped desc length (out)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003096 */
Bean Huo7a0bf852020-06-03 11:19:58 +02003097void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
3098 int *desc_len)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003099{
Bean Huo7a0bf852020-06-03 11:19:58 +02003100 if (desc_id >= QUERY_DESC_IDN_MAX || desc_id == QUERY_DESC_IDN_RFU_0 ||
3101 desc_id == QUERY_DESC_IDN_RFU_1)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003102 *desc_len = 0;
Bean Huo7a0bf852020-06-03 11:19:58 +02003103 else
3104 *desc_len = hba->desc_size[desc_id];
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003105}
3106EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
3107
Bean Huo7a0bf852020-06-03 11:19:58 +02003108static void ufshcd_update_desc_length(struct ufs_hba *hba,
Bean Huo72fb6902020-06-03 11:19:59 +02003109 enum desc_idn desc_id, int desc_index,
Bean Huo7a0bf852020-06-03 11:19:58 +02003110 unsigned char desc_len)
3111{
3112 if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE &&
Bean Huo72fb6902020-06-03 11:19:59 +02003113 desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT)
3114 /* For UFS 3.1, the normal unit descriptor is 10 bytes larger
3115 * than the RPMB unit, however, both descriptors share the same
3116 * desc_idn, to cover both unit descriptors with one length, we
3117 * choose the normal unit descriptor length by desc_index.
3118 */
Bean Huo7a0bf852020-06-03 11:19:58 +02003119 hba->desc_size[desc_id] = desc_len;
3120}
3121
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003122/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003123 * ufshcd_read_desc_param - read the specified descriptor parameter
3124 * @hba: Pointer to adapter instance
3125 * @desc_id: descriptor idn value
3126 * @desc_index: descriptor index
3127 * @param_offset: offset of the parameter to read
3128 * @param_read_buf: pointer to buffer where parameter would be read
3129 * @param_size: sizeof(param_read_buf)
3130 *
3131 * Return 0 in case of success, non-zero otherwise
3132 */
Stanislav Nijnikov45bced82018-02-15 14:14:02 +02003133int ufshcd_read_desc_param(struct ufs_hba *hba,
3134 enum desc_idn desc_id,
3135 int desc_index,
3136 u8 param_offset,
3137 u8 *param_read_buf,
3138 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003139{
3140 int ret;
3141 u8 *desc_buf;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003142 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003143 bool is_kmalloc = true;
3144
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003145 /* Safety check */
3146 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003147 return -EINVAL;
3148
Bean Huo7a0bf852020-06-03 11:19:58 +02003149 /* Get the length of descriptor */
3150 ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
3151 if (!buff_len) {
3152 dev_err(hba->dev, "%s: Failed to get desc length", __func__);
3153 return -EINVAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003154 }
3155
3156 /* Check whether we need temp memory */
3157 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003158 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3159 if (!desc_buf)
3160 return -ENOMEM;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003161 } else {
3162 desc_buf = param_read_buf;
3163 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003164 }
3165
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003166 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003167 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003168 desc_id, desc_index, 0,
3169 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003170
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003171 if (ret) {
3172 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3173 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003174 goto out;
3175 }
3176
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003177 /* Sanity check */
3178 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3179 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3180 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3181 ret = -EINVAL;
3182 goto out;
3183 }
3184
Bean Huo7a0bf852020-06-03 11:19:58 +02003185 /* Update descriptor length */
3186 buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET];
Bean Huo72fb6902020-06-03 11:19:59 +02003187 ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len);
Bean Huo7a0bf852020-06-03 11:19:58 +02003188
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003189 /* Check wherher we will not copy more data, than available */
Bean Huocbe193f2020-06-03 11:19:57 +02003190 if (is_kmalloc && (param_offset + param_size) > buff_len)
3191 param_size = buff_len - param_offset;
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003192
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003193 if (is_kmalloc)
3194 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3195out:
3196 if (is_kmalloc)
3197 kfree(desc_buf);
3198 return ret;
3199}
3200
Yaniv Gardib573d482016-03-10 17:37:09 +02003201/**
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003202 * struct uc_string_id - unicode string
3203 *
3204 * @len: size of this descriptor inclusive
3205 * @type: descriptor type
3206 * @uc: unicode string character
3207 */
3208struct uc_string_id {
3209 u8 len;
3210 u8 type;
Gustavo A. R. Silvaec38c0a2020-05-07 14:25:50 -05003211 wchar_t uc[];
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003212} __packed;
3213
3214/* replace non-printable or non-ASCII characters with spaces */
3215static inline char ufshcd_remove_non_printable(u8 ch)
3216{
3217 return (ch >= 0x20 && ch <= 0x7e) ? ch : ' ';
3218}
3219
3220/**
Yaniv Gardib573d482016-03-10 17:37:09 +02003221 * ufshcd_read_string_desc - read string descriptor
3222 * @hba: pointer to adapter instance
3223 * @desc_index: descriptor index
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003224 * @buf: pointer to buffer where descriptor would be read,
3225 * the caller should free the memory.
Yaniv Gardib573d482016-03-10 17:37:09 +02003226 * @ascii: if true convert from unicode to ascii characters
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003227 * null terminated string.
Yaniv Gardib573d482016-03-10 17:37:09 +02003228 *
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003229 * Return:
3230 * * string size on success.
3231 * * -ENOMEM: on allocation failure
3232 * * -EINVAL: on a wrong parameter
Yaniv Gardib573d482016-03-10 17:37:09 +02003233 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003234int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
3235 u8 **buf, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003236{
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003237 struct uc_string_id *uc_str;
3238 u8 *str;
3239 int ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003240
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003241 if (!buf)
3242 return -EINVAL;
Yaniv Gardib573d482016-03-10 17:37:09 +02003243
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003244 uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
3245 if (!uc_str)
3246 return -ENOMEM;
3247
Bean Huoc4607a02020-06-03 11:19:56 +02003248 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0,
3249 (u8 *)uc_str, QUERY_DESC_MAX_SIZE);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003250 if (ret < 0) {
3251 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
3252 QUERY_REQ_RETRIES, ret);
3253 str = NULL;
3254 goto out;
3255 }
3256
3257 if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
3258 dev_dbg(hba->dev, "String Desc is of zero length\n");
3259 str = NULL;
3260 ret = 0;
Yaniv Gardib573d482016-03-10 17:37:09 +02003261 goto out;
3262 }
3263
3264 if (ascii) {
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003265 ssize_t ascii_len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003266 int i;
Yaniv Gardib573d482016-03-10 17:37:09 +02003267 /* remove header and divide by 2 to move from UTF16 to UTF8 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003268 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3269 str = kzalloc(ascii_len, GFP_KERNEL);
3270 if (!str) {
3271 ret = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003272 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003273 }
3274
3275 /*
3276 * the descriptor contains string in UTF16 format
3277 * we need to convert to utf-8 so it can be displayed
3278 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003279 ret = utf16s_to_utf8s(uc_str->uc,
3280 uc_str->len - QUERY_DESC_HDR_SIZE,
3281 UTF16_BIG_ENDIAN, str, ascii_len);
Yaniv Gardib573d482016-03-10 17:37:09 +02003282
3283 /* replace non-printable or non-ASCII characters with spaces */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003284 for (i = 0; i < ret; i++)
3285 str[i] = ufshcd_remove_non_printable(str[i]);
Yaniv Gardib573d482016-03-10 17:37:09 +02003286
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003287 str[ret++] = '\0';
3288
3289 } else {
YueHaibing5f577042019-08-31 12:44:24 +00003290 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003291 if (!str) {
3292 ret = -ENOMEM;
3293 goto out;
3294 }
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003295 ret = uc_str->len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003296 }
3297out:
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003298 *buf = str;
3299 kfree(uc_str);
3300 return ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003301}
Yaniv Gardib573d482016-03-10 17:37:09 +02003302
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003303/**
3304 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3305 * @hba: Pointer to adapter instance
3306 * @lun: lun id
3307 * @param_offset: offset of the parameter to read
3308 * @param_read_buf: pointer to buffer where parameter would be read
3309 * @param_size: sizeof(param_read_buf)
3310 *
3311 * Return 0 in case of success, non-zero otherwise
3312 */
3313static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3314 int lun,
3315 enum unit_desc_param param_offset,
3316 u8 *param_read_buf,
3317 u32 param_size)
3318{
3319 /*
3320 * Unit descriptors are only available for general purpose LUs (LUN id
3321 * from 0 to 7) and RPMB Well known LU.
3322 */
Bean Huo1baa8012020-01-20 14:08:20 +01003323 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003324 return -EOPNOTSUPP;
3325
3326 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3327 param_offset, param_read_buf, param_size);
3328}
3329
Can Guo09f17792020-02-10 19:40:49 -08003330static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba)
3331{
3332 int err = 0;
3333 u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3334
3335 if (hba->dev_info.wspecversion >= 0x300) {
3336 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3337 QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0,
3338 &gating_wait);
3339 if (err)
3340 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n",
3341 err, gating_wait);
3342
3343 if (gating_wait == 0) {
3344 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3345 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n",
3346 gating_wait);
3347 }
3348
3349 hba->dev_info.clk_gating_wait_us = gating_wait;
3350 }
3351
3352 return err;
3353}
3354
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003355/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303356 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3357 * @hba: per adapter instance
3358 *
3359 * 1. Allocate DMA memory for Command Descriptor array
3360 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3361 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3362 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3363 * (UTMRDL)
3364 * 4. Allocate memory for local reference block(lrb).
3365 *
3366 * Returns 0 for success, non-zero in case of failure
3367 */
3368static int ufshcd_memory_alloc(struct ufs_hba *hba)
3369{
3370 size_t utmrdl_size, utrdl_size, ucdl_size;
3371
3372 /* Allocate memory for UTP command descriptors */
3373 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003374 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3375 ucdl_size,
3376 &hba->ucdl_dma_addr,
3377 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303378
3379 /*
3380 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3381 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3382 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3383 * be aligned to 128 bytes as well
3384 */
3385 if (!hba->ucdl_base_addr ||
3386 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303387 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303388 "Command Descriptor Memory allocation failed\n");
3389 goto out;
3390 }
3391
3392 /*
3393 * Allocate memory for UTP Transfer descriptors
3394 * UFSHCI requires 1024 byte alignment of UTRD
3395 */
3396 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003397 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3398 utrdl_size,
3399 &hba->utrdl_dma_addr,
3400 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303401 if (!hba->utrdl_base_addr ||
3402 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303403 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303404 "Transfer Descriptor Memory allocation failed\n");
3405 goto out;
3406 }
3407
3408 /*
3409 * Allocate memory for UTP Task Management descriptors
3410 * UFSHCI requires 1024 byte alignment of UTMRD
3411 */
3412 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003413 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3414 utmrdl_size,
3415 &hba->utmrdl_dma_addr,
3416 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303417 if (!hba->utmrdl_base_addr ||
3418 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303419 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303420 "Task Management Descriptor Memory allocation failed\n");
3421 goto out;
3422 }
3423
3424 /* Allocate memory for local reference block */
Kees Cooka86854d2018-06-12 14:07:58 -07003425 hba->lrb = devm_kcalloc(hba->dev,
3426 hba->nutrs, sizeof(struct ufshcd_lrb),
Seungwon Jeon2953f852013-06-27 13:31:54 +09003427 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303428 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303429 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303430 goto out;
3431 }
3432 return 0;
3433out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303434 return -ENOMEM;
3435}
3436
3437/**
3438 * ufshcd_host_memory_configure - configure local reference block with
3439 * memory offsets
3440 * @hba: per adapter instance
3441 *
3442 * Configure Host memory space
3443 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3444 * address.
3445 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3446 * and PRDT offset.
3447 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3448 * into local reference block.
3449 */
3450static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3451{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303452 struct utp_transfer_req_desc *utrdlp;
3453 dma_addr_t cmd_desc_dma_addr;
3454 dma_addr_t cmd_desc_element_addr;
3455 u16 response_offset;
3456 u16 prdt_offset;
3457 int cmd_desc_size;
3458 int i;
3459
3460 utrdlp = hba->utrdl_base_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303461
3462 response_offset =
3463 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3464 prdt_offset =
3465 offsetof(struct utp_transfer_cmd_desc, prd_table);
3466
3467 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3468 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3469
3470 for (i = 0; i < hba->nutrs; i++) {
3471 /* Configure UTRD with command descriptor base address */
3472 cmd_desc_element_addr =
3473 (cmd_desc_dma_addr + (cmd_desc_size * i));
3474 utrdlp[i].command_desc_base_addr_lo =
3475 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3476 utrdlp[i].command_desc_base_addr_hi =
3477 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3478
3479 /* Response upiu and prdt offset should be in double words */
Alim Akhtar26f968d2020-05-28 06:46:52 +05303480 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3481 utrdlp[i].response_upiu_offset =
3482 cpu_to_le16(response_offset);
3483 utrdlp[i].prd_table_offset =
3484 cpu_to_le16(prdt_offset);
3485 utrdlp[i].response_upiu_length =
3486 cpu_to_le16(ALIGNED_UPIU_SIZE);
3487 } else {
3488 utrdlp[i].response_upiu_offset =
3489 cpu_to_le16(response_offset >> 2);
3490 utrdlp[i].prd_table_offset =
3491 cpu_to_le16(prdt_offset >> 2);
3492 utrdlp[i].response_upiu_length =
3493 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
3494 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303495
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08003496 ufshcd_init_lrb(hba, &hba->lrb[i], i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303497 }
3498}
3499
3500/**
3501 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3502 * @hba: per adapter instance
3503 *
3504 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3505 * in order to initialize the Unipro link startup procedure.
3506 * Once the Unipro links are up, the device connected to the controller
3507 * is detected.
3508 *
3509 * Returns 0 on success, non-zero value on failure
3510 */
3511static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3512{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303513 struct uic_command uic_cmd = {0};
3514 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303515
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303516 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3517
3518 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3519 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003520 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303521 "dme-link-startup: error code %d\n", ret);
3522 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303523}
Alim Akhtar39bf2d82020-05-28 06:46:51 +05303524/**
3525 * ufshcd_dme_reset - UIC command for DME_RESET
3526 * @hba: per adapter instance
3527 *
3528 * DME_RESET command is issued in order to reset UniPro stack.
3529 * This function now deals with cold reset.
3530 *
3531 * Returns 0 on success, non-zero value on failure
3532 */
3533static int ufshcd_dme_reset(struct ufs_hba *hba)
3534{
3535 struct uic_command uic_cmd = {0};
3536 int ret;
3537
3538 uic_cmd.command = UIC_CMD_DME_RESET;
3539
3540 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3541 if (ret)
3542 dev_err(hba->dev,
3543 "dme-reset: error code %d\n", ret);
3544
3545 return ret;
3546}
3547
3548/**
3549 * ufshcd_dme_enable - UIC command for DME_ENABLE
3550 * @hba: per adapter instance
3551 *
3552 * DME_ENABLE command is issued in order to enable UniPro stack.
3553 *
3554 * Returns 0 on success, non-zero value on failure
3555 */
3556static int ufshcd_dme_enable(struct ufs_hba *hba)
3557{
3558 struct uic_command uic_cmd = {0};
3559 int ret;
3560
3561 uic_cmd.command = UIC_CMD_DME_ENABLE;
3562
3563 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3564 if (ret)
3565 dev_err(hba->dev,
3566 "dme-reset: error code %d\n", ret);
3567
3568 return ret;
3569}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303570
Yaniv Gardicad2e032015-03-31 17:37:14 +03003571static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3572{
3573 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3574 unsigned long min_sleep_time_us;
3575
3576 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3577 return;
3578
3579 /*
3580 * last_dme_cmd_tstamp will be 0 only for 1st call to
3581 * this function
3582 */
3583 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3584 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3585 } else {
3586 unsigned long delta =
3587 (unsigned long) ktime_to_us(
3588 ktime_sub(ktime_get(),
3589 hba->last_dme_cmd_tstamp));
3590
3591 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3592 min_sleep_time_us =
3593 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3594 else
3595 return; /* no more delay required */
3596 }
3597
3598 /* allow sleep for extra 50us if needed */
3599 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3600}
3601
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303602/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303603 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3604 * @hba: per adapter instance
3605 * @attr_sel: uic command argument1
3606 * @attr_set: attribute set type as uic command argument2
3607 * @mib_val: setting value as uic command argument3
3608 * @peer: indicate whether peer or local
3609 *
3610 * Returns 0 on success, non-zero value on failure
3611 */
3612int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3613 u8 attr_set, u32 mib_val, u8 peer)
3614{
3615 struct uic_command uic_cmd = {0};
3616 static const char *const action[] = {
3617 "dme-set",
3618 "dme-peer-set"
3619 };
3620 const char *set = action[!!peer];
3621 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003622 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303623
3624 uic_cmd.command = peer ?
3625 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3626 uic_cmd.argument1 = attr_sel;
3627 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3628 uic_cmd.argument3 = mib_val;
3629
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003630 do {
3631 /* for peer attributes we retry upon failure */
3632 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3633 if (ret)
3634 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3635 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3636 } while (ret && peer && --retries);
3637
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003638 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003639 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003640 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3641 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303642
3643 return ret;
3644}
3645EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3646
3647/**
3648 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3649 * @hba: per adapter instance
3650 * @attr_sel: uic command argument1
3651 * @mib_val: the value of the attribute as returned by the UIC command
3652 * @peer: indicate whether peer or local
3653 *
3654 * Returns 0 on success, non-zero value on failure
3655 */
3656int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3657 u32 *mib_val, u8 peer)
3658{
3659 struct uic_command uic_cmd = {0};
3660 static const char *const action[] = {
3661 "dme-get",
3662 "dme-peer-get"
3663 };
3664 const char *get = action[!!peer];
3665 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003666 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003667 struct ufs_pa_layer_attr orig_pwr_info;
3668 struct ufs_pa_layer_attr temp_pwr_info;
3669 bool pwr_mode_change = false;
3670
3671 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3672 orig_pwr_info = hba->pwr_info;
3673 temp_pwr_info = orig_pwr_info;
3674
3675 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3676 orig_pwr_info.pwr_rx == FAST_MODE) {
3677 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3678 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3679 pwr_mode_change = true;
3680 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3681 orig_pwr_info.pwr_rx == SLOW_MODE) {
3682 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3683 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3684 pwr_mode_change = true;
3685 }
3686 if (pwr_mode_change) {
3687 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3688 if (ret)
3689 goto out;
3690 }
3691 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303692
3693 uic_cmd.command = peer ?
3694 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3695 uic_cmd.argument1 = attr_sel;
3696
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003697 do {
3698 /* for peer attributes we retry upon failure */
3699 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3700 if (ret)
3701 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3702 get, UIC_GET_ATTR_ID(attr_sel), ret);
3703 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303704
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003705 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003706 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003707 get, UIC_GET_ATTR_ID(attr_sel),
3708 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003709
3710 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303711 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003712
3713 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3714 && pwr_mode_change)
3715 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303716out:
3717 return ret;
3718}
3719EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3720
3721/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003722 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3723 * state) and waits for it to take effect.
3724 *
3725 * @hba: per adapter instance
3726 * @cmd: UIC command to execute
3727 *
3728 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3729 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3730 * and device UniPro link and hence it's final completion would be indicated by
3731 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3732 * addition to normal UIC command completion Status (UCCS). This function only
3733 * returns after the relevant status bits indicate the completion.
3734 *
3735 * Returns 0 on success, non-zero value on failure
3736 */
3737static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3738{
3739 struct completion uic_async_done;
3740 unsigned long flags;
3741 u8 status;
3742 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003743 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003744
3745 mutex_lock(&hba->uic_cmd_mutex);
3746 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003747 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003748
3749 spin_lock_irqsave(hba->host->host_lock, flags);
3750 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003751 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3752 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3753 /*
3754 * Make sure UIC command completion interrupt is disabled before
3755 * issuing UIC command.
3756 */
3757 wmb();
3758 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003759 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003760 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3761 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003762 if (ret) {
3763 dev_err(hba->dev,
3764 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3765 cmd->command, cmd->argument3, ret);
3766 goto out;
3767 }
3768
3769 if (!wait_for_completion_timeout(hba->uic_async_done,
3770 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3771 dev_err(hba->dev,
3772 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3773 cmd->command, cmd->argument3);
3774 ret = -ETIMEDOUT;
3775 goto out;
3776 }
3777
3778 status = ufshcd_get_upmcrs(hba);
3779 if (status != PWR_LOCAL) {
3780 dev_err(hba->dev,
Zang Leigang479da362017-09-19 16:50:30 +08003781 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003782 cmd->command, status);
3783 ret = (status != PWR_OK) ? status : -1;
3784 }
3785out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003786 if (ret) {
3787 ufshcd_print_host_state(hba);
3788 ufshcd_print_pwr_info(hba);
3789 ufshcd_print_host_regs(hba);
3790 }
3791
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003792 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003793 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003794 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003795 if (reenable_intr)
3796 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003797 spin_unlock_irqrestore(hba->host->host_lock, flags);
3798 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003799
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003800 return ret;
3801}
3802
3803/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303804 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3805 * using DME_SET primitives.
3806 * @hba: per adapter instance
3807 * @mode: powr mode value
3808 *
3809 * Returns 0 on success, non-zero value on failure
3810 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303811static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303812{
3813 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003814 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303815
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003816 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3817 ret = ufshcd_dme_set(hba,
3818 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3819 if (ret) {
3820 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3821 __func__, ret);
3822 goto out;
3823 }
3824 }
3825
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303826 uic_cmd.command = UIC_CMD_DME_SET;
3827 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3828 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003829 ufshcd_hold(hba, false);
3830 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3831 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303832
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003833out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003834 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003835}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303836
Stanley Chu087c5ef2020-03-27 17:53:28 +08003837int ufshcd_link_recovery(struct ufs_hba *hba)
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003838{
3839 int ret;
3840 unsigned long flags;
3841
3842 spin_lock_irqsave(hba->host->host_lock, flags);
3843 hba->ufshcd_state = UFSHCD_STATE_RESET;
3844 ufshcd_set_eh_in_progress(hba);
3845 spin_unlock_irqrestore(hba->host->host_lock, flags);
3846
Can Guoebdd1df2019-11-14 22:09:24 -08003847 /* Reset the attached device */
3848 ufshcd_vops_device_reset(hba);
3849
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003850 ret = ufshcd_host_reset_and_restore(hba);
3851
3852 spin_lock_irqsave(hba->host->host_lock, flags);
3853 if (ret)
3854 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3855 ufshcd_clear_eh_in_progress(hba);
3856 spin_unlock_irqrestore(hba->host->host_lock, flags);
3857
3858 if (ret)
3859 dev_err(hba->dev, "%s: link recovery failed, err %d",
3860 __func__, ret);
3861
3862 return ret;
3863}
Stanley Chu087c5ef2020-03-27 17:53:28 +08003864EXPORT_SYMBOL_GPL(ufshcd_link_recovery);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003865
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003866static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003867{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003868 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003869 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003870 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003871
Kiwoong Kimee32c902016-11-10 21:17:43 +09003872 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3873
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003874 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003875 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003876 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
3877 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003878
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003879 if (ret) {
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003880 int err;
3881
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003882 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3883 __func__, ret);
3884
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003885 /*
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003886 * If link recovery fails then return error code returned from
3887 * ufshcd_link_recovery().
3888 * If link recovery succeeds then return -EAGAIN to attempt
3889 * hibern8 enter retry again.
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003890 */
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003891 err = ufshcd_link_recovery(hba);
3892 if (err) {
3893 dev_err(hba->dev, "%s: link recovery failed", __func__);
3894 ret = err;
3895 } else {
3896 ret = -EAGAIN;
3897 }
Kiwoong Kimee32c902016-11-10 21:17:43 +09003898 } else
3899 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3900 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003901
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003902 return ret;
3903}
3904
3905static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3906{
3907 int ret = 0, retries;
3908
3909 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
3910 ret = __ufshcd_uic_hibern8_enter(hba);
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003911 if (!ret)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003912 goto out;
3913 }
3914out:
3915 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003916}
3917
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08003918int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003919{
3920 struct uic_command uic_cmd = {0};
3921 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003922 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003923
Kiwoong Kimee32c902016-11-10 21:17:43 +09003924 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3925
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003926 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3927 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003928 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
3929 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3930
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303931 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003932 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3933 __func__, ret);
3934 ret = ufshcd_link_recovery(hba);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003935 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09003936 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3937 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003938 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
3939 hba->ufs_stats.hibern8_exit_cnt++;
3940 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303941
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303942 return ret;
3943}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08003944EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303945
Stanley Chuba7af5e2019-12-30 13:32:28 +08003946void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit)
3947{
3948 unsigned long flags;
Can Guobe7594a2020-03-05 00:53:07 -08003949 bool update = false;
Stanley Chuba7af5e2019-12-30 13:32:28 +08003950
Can Guobe7594a2020-03-05 00:53:07 -08003951 if (!ufshcd_is_auto_hibern8_supported(hba))
Stanley Chuba7af5e2019-12-30 13:32:28 +08003952 return;
3953
3954 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08003955 if (hba->ahit != ahit) {
3956 hba->ahit = ahit;
3957 update = true;
3958 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08003959 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08003960
3961 if (update && !pm_runtime_suspended(hba->dev)) {
3962 pm_runtime_get_sync(hba->dev);
3963 ufshcd_hold(hba, false);
3964 ufshcd_auto_hibern8_enable(hba);
3965 ufshcd_release(hba);
3966 pm_runtime_put(hba->dev);
3967 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08003968}
3969EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
3970
Can Guo71d848b2019-11-14 22:09:26 -08003971void ufshcd_auto_hibern8_enable(struct ufs_hba *hba)
Adrian Hunterad448372018-03-20 15:07:38 +02003972{
3973 unsigned long flags;
3974
Stanley Chuee5f1042019-05-21 14:44:52 +08003975 if (!ufshcd_is_auto_hibern8_supported(hba) || !hba->ahit)
Adrian Hunterad448372018-03-20 15:07:38 +02003976 return;
3977
3978 spin_lock_irqsave(hba->host->host_lock, flags);
3979 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
3980 spin_unlock_irqrestore(hba->host->host_lock, flags);
3981}
3982
Yaniv Gardi50646362014-10-23 13:25:13 +03003983 /**
3984 * ufshcd_init_pwr_info - setting the POR (power on reset)
3985 * values in hba power info
3986 * @hba: per-adapter instance
3987 */
3988static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3989{
3990 hba->pwr_info.gear_rx = UFS_PWM_G1;
3991 hba->pwr_info.gear_tx = UFS_PWM_G1;
3992 hba->pwr_info.lane_rx = 1;
3993 hba->pwr_info.lane_tx = 1;
3994 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3995 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3996 hba->pwr_info.hs_rate = 0;
3997}
3998
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303999/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004000 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4001 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304002 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004003static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304004{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004005 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4006
4007 if (hba->max_pwr_info.is_valid)
4008 return 0;
4009
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004010 pwr_info->pwr_tx = FAST_MODE;
4011 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004012 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304013
4014 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004015 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4016 &pwr_info->lane_rx);
4017 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4018 &pwr_info->lane_tx);
4019
4020 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4021 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4022 __func__,
4023 pwr_info->lane_rx,
4024 pwr_info->lane_tx);
4025 return -EINVAL;
4026 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304027
4028 /*
4029 * First, get the maximum gears of HS speed.
4030 * If a zero value, it means there is no HSGEAR capability.
4031 * Then, get the maximum gears of PWM speed.
4032 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004033 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4034 if (!pwr_info->gear_rx) {
4035 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4036 &pwr_info->gear_rx);
4037 if (!pwr_info->gear_rx) {
4038 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4039 __func__, pwr_info->gear_rx);
4040 return -EINVAL;
4041 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004042 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304043 }
4044
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004045 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4046 &pwr_info->gear_tx);
4047 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304048 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004049 &pwr_info->gear_tx);
4050 if (!pwr_info->gear_tx) {
4051 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4052 __func__, pwr_info->gear_tx);
4053 return -EINVAL;
4054 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004055 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004056 }
4057
4058 hba->max_pwr_info.is_valid = true;
4059 return 0;
4060}
4061
4062static int ufshcd_change_power_mode(struct ufs_hba *hba,
4063 struct ufs_pa_layer_attr *pwr_mode)
4064{
4065 int ret;
4066
4067 /* if already configured to the requested pwr_mode */
4068 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4069 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4070 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4071 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4072 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4073 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4074 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4075 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4076 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304077 }
4078
4079 /*
4080 * Configure attributes for power mode change with below.
4081 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4082 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4083 * - PA_HSSERIES
4084 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004085 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4086 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4087 pwr_mode->lane_rx);
4088 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4089 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304090 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004091 else
4092 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304093
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004094 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4095 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4096 pwr_mode->lane_tx);
4097 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4098 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304099 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004100 else
4101 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304102
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004103 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4104 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4105 pwr_mode->pwr_rx == FAST_MODE ||
4106 pwr_mode->pwr_tx == FAST_MODE)
4107 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4108 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304109
Can Guo08342532019-12-05 02:14:42 +00004110 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4111 DL_FC0ProtectionTimeOutVal_Default);
4112 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4113 DL_TC0ReplayTimeOutVal_Default);
4114 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4115 DL_AFC0ReqTimeOutVal_Default);
4116 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
4117 DL_FC1ProtectionTimeOutVal_Default);
4118 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
4119 DL_TC1ReplayTimeOutVal_Default);
4120 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
4121 DL_AFC1ReqTimeOutVal_Default);
4122
4123 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4124 DL_FC0ProtectionTimeOutVal_Default);
4125 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4126 DL_TC0ReplayTimeOutVal_Default);
4127 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4128 DL_AFC0ReqTimeOutVal_Default);
4129
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004130 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4131 | pwr_mode->pwr_tx);
4132
4133 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304134 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004135 "%s: power mode change failed %d\n", __func__, ret);
4136 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004137 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4138 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004139
4140 memcpy(&hba->pwr_info, pwr_mode,
4141 sizeof(struct ufs_pa_layer_attr));
4142 }
4143
4144 return ret;
4145}
4146
4147/**
4148 * ufshcd_config_pwr_mode - configure a new power mode
4149 * @hba: per-adapter instance
4150 * @desired_pwr_mode: desired power configuration
4151 */
Alim Akhtar0d846e72018-05-06 15:44:18 +05304152int ufshcd_config_pwr_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004153 struct ufs_pa_layer_attr *desired_pwr_mode)
4154{
4155 struct ufs_pa_layer_attr final_params = { 0 };
4156 int ret;
4157
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004158 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4159 desired_pwr_mode, &final_params);
4160
4161 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004162 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4163
4164 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304165
4166 return ret;
4167}
Alim Akhtar0d846e72018-05-06 15:44:18 +05304168EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304169
4170/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304171 * ufshcd_complete_dev_init() - checks device readiness
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004172 * @hba: per-adapter instance
Dolev Raviv68078d52013-07-30 00:35:58 +05304173 *
4174 * Set fDeviceInit flag and poll until device toggles it.
4175 */
4176static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4177{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004178 int i;
4179 int err;
Jason Yan7dfdcc32020-04-26 17:43:05 +08004180 bool flag_res = true;
Dolev Raviv68078d52013-07-30 00:35:58 +05304181
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004182 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08004183 QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304184 if (err) {
4185 dev_err(hba->dev,
4186 "%s setting fDeviceInit flag failed with error %d\n",
4187 __func__, err);
4188 goto out;
4189 }
4190
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004191 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
4192 for (i = 0; i < 1000 && !err && flag_res; i++)
4193 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08004194 QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res);
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004195
Dolev Raviv68078d52013-07-30 00:35:58 +05304196 if (err)
4197 dev_err(hba->dev,
4198 "%s reading fDeviceInit flag failed with error %d\n",
4199 __func__, err);
4200 else if (flag_res)
4201 dev_err(hba->dev,
4202 "%s fDeviceInit was not cleared by the device\n",
4203 __func__);
4204
4205out:
4206 return err;
4207}
4208
4209/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304210 * ufshcd_make_hba_operational - Make UFS controller operational
4211 * @hba: per adapter instance
4212 *
4213 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004214 * 1. Enable required interrupts
4215 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004216 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004217 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304218 *
4219 * Returns 0 on success, non-zero value on failure
4220 */
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004221int ufshcd_make_hba_operational(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304222{
4223 int err = 0;
4224 u32 reg;
4225
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304226 /* Enable required interrupts */
4227 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4228
4229 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004230 if (ufshcd_is_intr_aggr_allowed(hba))
4231 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4232 else
4233 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304234
4235 /* Configure UTRL and UTMRL base address registers */
4236 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4237 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4238 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4239 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4240 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4241 REG_UTP_TASK_REQ_LIST_BASE_L);
4242 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4243 REG_UTP_TASK_REQ_LIST_BASE_H);
4244
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304245 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004246 * Make sure base address and interrupt setup are updated before
4247 * enabling the run/stop registers below.
4248 */
4249 wmb();
4250
4251 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304252 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304253 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004254 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304255 if (!(ufshcd_get_lists_status(reg))) {
4256 ufshcd_enable_run_stop_reg(hba);
4257 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304258 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304259 "Host controller not ready to process requests");
4260 err = -EIO;
4261 goto out;
4262 }
4263
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304264out:
4265 return err;
4266}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004267EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304268
4269/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004270 * ufshcd_hba_stop - Send controller to reset state
4271 * @hba: per adapter instance
Yaniv Gardi596585a2016-03-10 17:37:08 +02004272 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07004273static inline void ufshcd_hba_stop(struct ufs_hba *hba)
Yaniv Gardi596585a2016-03-10 17:37:08 +02004274{
Bart Van Assche5cac1092020-05-07 15:27:50 -07004275 unsigned long flags;
Yaniv Gardi596585a2016-03-10 17:37:08 +02004276 int err;
4277
Bart Van Assche5cac1092020-05-07 15:27:50 -07004278 /*
4279 * Obtain the host lock to prevent that the controller is disabled
4280 * while the UFS interrupt handler is active on another CPU.
4281 */
4282 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004283 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
Bart Van Assche5cac1092020-05-07 15:27:50 -07004284 spin_unlock_irqrestore(hba->host->host_lock, flags);
4285
Yaniv Gardi596585a2016-03-10 17:37:08 +02004286 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4287 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
Bart Van Assche5cac1092020-05-07 15:27:50 -07004288 10, 1);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004289 if (err)
4290 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4291}
4292
4293/**
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304294 * ufshcd_hba_execute_hce - initialize the controller
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304295 * @hba: per adapter instance
4296 *
4297 * The controller resets itself and controller firmware initialization
4298 * sequence kicks off. When controller is ready it will set
4299 * the Host Controller Enable bit to 1.
4300 *
4301 * Returns 0 on success, non-zero value on failure
4302 */
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304303static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304304{
4305 int retry;
4306
Yaniv Gardi596585a2016-03-10 17:37:08 +02004307 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304308 /* change controller state to "reset state" */
Bart Van Assche5cac1092020-05-07 15:27:50 -07004309 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304310
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004311 /* UniPro link is disabled at this point */
4312 ufshcd_set_link_off(hba);
4313
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004314 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004315
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304316 /* start controller initialization sequence */
4317 ufshcd_hba_start(hba);
4318
4319 /*
4320 * To initialize a UFS host controller HCE bit must be set to 1.
4321 * During initialization the HCE bit value changes from 1->0->1.
4322 * When the host controller completes initialization sequence
4323 * it sets the value of HCE bit to 1. The same HCE bit is read back
4324 * to check if the controller has completed initialization sequence.
4325 * So without this delay the value HCE = 1, set in the previous
4326 * instruction might be read back.
4327 * This delay can be changed based on the controller.
4328 */
Stanley Chu90b84912020-05-09 17:37:13 +08004329 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304330
4331 /* wait for the host controller to complete initialization */
Stanley Chu9fc305e2020-03-18 18:40:15 +08004332 retry = 50;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304333 while (ufshcd_is_hba_active(hba)) {
4334 if (retry) {
4335 retry--;
4336 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304337 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304338 "Controller enable failed\n");
4339 return -EIO;
4340 }
Stanley Chu9fc305e2020-03-18 18:40:15 +08004341 usleep_range(1000, 1100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304342 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004343
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004344 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004345 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004346
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004347 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004348
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304349 return 0;
4350}
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304351
4352int ufshcd_hba_enable(struct ufs_hba *hba)
4353{
4354 int ret;
4355
4356 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
4357 ufshcd_set_link_off(hba);
4358 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4359
4360 /* enable UIC related interrupts */
4361 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4362 ret = ufshcd_dme_reset(hba);
4363 if (!ret) {
4364 ret = ufshcd_dme_enable(hba);
4365 if (!ret)
4366 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4367 if (ret)
4368 dev_err(hba->dev,
4369 "Host controller enable failed with non-hce\n");
4370 }
4371 } else {
4372 ret = ufshcd_hba_execute_hce(hba);
4373 }
4374
4375 return ret;
4376}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004377EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
4378
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004379static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4380{
Stanley Chuba0320f2020-03-18 18:40:10 +08004381 int tx_lanes = 0, i, err = 0;
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004382
4383 if (!peer)
4384 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4385 &tx_lanes);
4386 else
4387 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4388 &tx_lanes);
4389 for (i = 0; i < tx_lanes; i++) {
4390 if (!peer)
4391 err = ufshcd_dme_set(hba,
4392 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4393 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4394 0);
4395 else
4396 err = ufshcd_dme_peer_set(hba,
4397 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4398 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4399 0);
4400 if (err) {
4401 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4402 __func__, peer, i, err);
4403 break;
4404 }
4405 }
4406
4407 return err;
4408}
4409
4410static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4411{
4412 return ufshcd_disable_tx_lcc(hba, true);
4413}
4414
Stanley Chua5fe372d2020-01-04 22:26:07 +08004415void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist,
4416 u32 reg)
Stanley Chu8808b4e2019-07-10 21:38:21 +08004417{
4418 reg_hist->reg[reg_hist->pos] = reg;
4419 reg_hist->tstamp[reg_hist->pos] = ktime_get();
4420 reg_hist->pos = (reg_hist->pos + 1) % UFS_ERR_REG_HIST_LENGTH;
4421}
Stanley Chua5fe372d2020-01-04 22:26:07 +08004422EXPORT_SYMBOL_GPL(ufshcd_update_reg_hist);
Stanley Chu8808b4e2019-07-10 21:38:21 +08004423
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304424/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304425 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304426 * @hba: per adapter instance
4427 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304428 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304429 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304430static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304431{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304432 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004433 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004434 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304435
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004436 /*
4437 * If UFS device isn't active then we will have to issue link startup
4438 * 2 times to make sure the device state move to active.
4439 */
4440 if (!ufshcd_is_ufs_dev_active(hba))
4441 link_startup_again = true;
4442
4443link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004444 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004445 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304446
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004447 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004448
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004449 /* check if device is detected by inter-connect layer */
4450 if (!ret && !ufshcd_is_device_present(hba)) {
Stanley Chu8808b4e2019-07-10 21:38:21 +08004451 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4452 0);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004453 dev_err(hba->dev, "%s: Device not present\n", __func__);
4454 ret = -ENXIO;
4455 goto out;
4456 }
4457
4458 /*
4459 * DME link lost indication is only received when link is up,
4460 * but we can't be sure if the link is up until link startup
4461 * succeeds. So reset the local Uni-Pro and try again.
4462 */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004463 if (ret && ufshcd_hba_enable(hba)) {
4464 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4465 (u32)ret);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004466 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004467 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004468 } while (ret && retries--);
4469
Stanley Chu8808b4e2019-07-10 21:38:21 +08004470 if (ret) {
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004471 /* failed to get the link up... retire */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004472 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4473 (u32)ret);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304474 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004475 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304476
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004477 if (link_startup_again) {
4478 link_startup_again = false;
4479 retries = DME_LINKSTARTUP_RETRIES;
4480 goto link_startup;
4481 }
4482
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004483 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4484 ufshcd_init_pwr_info(hba);
4485 ufshcd_print_pwr_info(hba);
4486
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004487 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4488 ret = ufshcd_disable_device_tx_lcc(hba);
4489 if (ret)
4490 goto out;
4491 }
4492
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004493 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004494 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4495 if (ret)
4496 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004497
4498 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304499out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004500 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304501 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004502 ufshcd_print_host_state(hba);
4503 ufshcd_print_pwr_info(hba);
4504 ufshcd_print_host_regs(hba);
4505 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304506 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304507}
4508
4509/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304510 * ufshcd_verify_dev_init() - Verify device initialization
4511 * @hba: per-adapter instance
4512 *
4513 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4514 * device Transport Protocol (UTP) layer is ready after a reset.
4515 * If the UTP layer at the device side is not initialized, it may
4516 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4517 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4518 */
4519static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4520{
4521 int err = 0;
4522 int retries;
4523
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004524 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304525 mutex_lock(&hba->dev_cmd.lock);
4526 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4527 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4528 NOP_OUT_TIMEOUT);
4529
4530 if (!err || err == -ETIMEDOUT)
4531 break;
4532
4533 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4534 }
4535 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004536 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304537
4538 if (err)
4539 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4540 return err;
4541}
4542
4543/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004544 * ufshcd_set_queue_depth - set lun queue depth
4545 * @sdev: pointer to SCSI device
4546 *
4547 * Read bLUQueueDepth value and activate scsi tagged command
4548 * queueing. For WLUN, queue depth is set to 1. For best-effort
4549 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4550 * value that host can queue.
4551 */
4552static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4553{
4554 int ret = 0;
4555 u8 lun_qdepth;
4556 struct ufs_hba *hba;
4557
4558 hba = shost_priv(sdev->host);
4559
4560 lun_qdepth = hba->nutrs;
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02004561 ret = ufshcd_read_unit_desc_param(hba,
4562 ufshcd_scsi_to_upiu_lun(sdev->lun),
4563 UNIT_DESC_PARAM_LU_Q_DEPTH,
4564 &lun_qdepth,
4565 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004566
4567 /* Some WLUN doesn't support unit descriptor */
4568 if (ret == -EOPNOTSUPP)
4569 lun_qdepth = 1;
4570 else if (!lun_qdepth)
4571 /* eventually, we can figure out the real queue depth */
4572 lun_qdepth = hba->nutrs;
4573 else
4574 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4575
4576 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4577 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004578 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004579}
4580
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004581/*
4582 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4583 * @hba: per-adapter instance
4584 * @lun: UFS device lun id
4585 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4586 *
4587 * Returns 0 in case of success and b_lu_write_protect status would be returned
4588 * @b_lu_write_protect parameter.
4589 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4590 * Returns -EINVAL in case of invalid parameters passed to this function.
4591 */
4592static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4593 u8 lun,
4594 u8 *b_lu_write_protect)
4595{
4596 int ret;
4597
4598 if (!b_lu_write_protect)
4599 ret = -EINVAL;
4600 /*
4601 * According to UFS device spec, RPMB LU can't be write
4602 * protected so skip reading bLUWriteProtect parameter for
4603 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4604 */
Bean Huo1baa8012020-01-20 14:08:20 +01004605 else if (lun >= hba->dev_info.max_lu_supported)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004606 ret = -ENOTSUPP;
4607 else
4608 ret = ufshcd_read_unit_desc_param(hba,
4609 lun,
4610 UNIT_DESC_PARAM_LU_WR_PROTECT,
4611 b_lu_write_protect,
4612 sizeof(*b_lu_write_protect));
4613 return ret;
4614}
4615
4616/**
4617 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4618 * status
4619 * @hba: per-adapter instance
4620 * @sdev: pointer to SCSI device
4621 *
4622 */
4623static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4624 struct scsi_device *sdev)
4625{
4626 if (hba->dev_info.f_power_on_wp_en &&
4627 !hba->dev_info.is_lu_power_on_wp) {
4628 u8 b_lu_write_protect;
4629
4630 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4631 &b_lu_write_protect) &&
4632 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4633 hba->dev_info.is_lu_power_on_wp = true;
4634 }
4635}
4636
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004637/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304638 * ufshcd_slave_alloc - handle initial SCSI device configurations
4639 * @sdev: pointer to SCSI device
4640 *
4641 * Returns success
4642 */
4643static int ufshcd_slave_alloc(struct scsi_device *sdev)
4644{
4645 struct ufs_hba *hba;
4646
4647 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304648
4649 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4650 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304651
Can Guoa3a76392019-12-05 02:14:30 +00004652 /* DBD field should be set to 1 in mode sense(10) */
4653 sdev->set_dbd_for_ms = 1;
4654
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304655 /* allow SCSI layer to restart the device in case of errors */
4656 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004657
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004658 /* REPORT SUPPORTED OPERATION CODES is not supported */
4659 sdev->no_report_opcodes = 1;
4660
Sujit Reddy Thumma84af7e82018-01-24 09:52:35 +05304661 /* WRITE_SAME command is not supported */
4662 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004663
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004664 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004665
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004666 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4667
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004668 return 0;
4669}
4670
4671/**
4672 * ufshcd_change_queue_depth - change queue depth
4673 * @sdev: pointer to SCSI device
4674 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004675 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004676 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004677 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004678static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004679{
4680 struct ufs_hba *hba = shost_priv(sdev->host);
4681
4682 if (depth > hba->nutrs)
4683 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004684 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304685}
4686
4687/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004688 * ufshcd_slave_configure - adjust SCSI device configurations
4689 * @sdev: pointer to SCSI device
4690 */
4691static int ufshcd_slave_configure(struct scsi_device *sdev)
4692{
Stanley Chu49615ba2019-09-16 23:56:50 +08004693 struct ufs_hba *hba = shost_priv(sdev->host);
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004694 struct request_queue *q = sdev->request_queue;
4695
4696 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
Stanley Chu49615ba2019-09-16 23:56:50 +08004697
4698 if (ufshcd_is_rpm_autosuspend_allowed(hba))
4699 sdev->rpm_autosuspend = 1;
4700
Satya Tangiraladf043c742020-07-06 20:04:14 +00004701 ufshcd_crypto_setup_rq_keyslot_manager(hba, q);
4702
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004703 return 0;
4704}
4705
4706/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304707 * ufshcd_slave_destroy - remove SCSI device configurations
4708 * @sdev: pointer to SCSI device
4709 */
4710static void ufshcd_slave_destroy(struct scsi_device *sdev)
4711{
4712 struct ufs_hba *hba;
4713
4714 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004715 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004716 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4717 unsigned long flags;
4718
4719 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004720 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004721 spin_unlock_irqrestore(hba->host->host_lock, flags);
4722 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304723}
4724
4725/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304726 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004727 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304728 * @scsi_status: SCSI command status
4729 *
4730 * Returns value base on SCSI command status
4731 */
4732static inline int
4733ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4734{
4735 int result = 0;
4736
4737 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304738 case SAM_STAT_CHECK_CONDITION:
4739 ufshcd_copy_sense_data(lrbp);
Tomas Winkler30eb2e42018-11-26 10:10:34 +02004740 /* fallthrough */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304741 case SAM_STAT_GOOD:
4742 result |= DID_OK << 16 |
4743 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304744 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304745 break;
4746 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304747 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304748 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304749 ufshcd_copy_sense_data(lrbp);
4750 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304751 break;
4752 default:
4753 result |= DID_ERROR << 16;
4754 break;
4755 } /* end of switch */
4756
4757 return result;
4758}
4759
4760/**
4761 * ufshcd_transfer_rsp_status - Get overall status of the response
4762 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004763 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304764 *
4765 * Returns result of the command to notify SCSI midlayer
4766 */
4767static inline int
4768ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4769{
4770 int result = 0;
4771 int scsi_status;
4772 int ocs;
4773
4774 /* overall command status of utrd */
4775 ocs = ufshcd_get_tr_ocs(lrbp);
4776
Kiwoong Kimd779a6e2020-05-28 06:46:53 +05304777 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) {
4778 if (be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_1) &
4779 MASK_RSP_UPIU_RESULT)
4780 ocs = OCS_SUCCESS;
4781 }
4782
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304783 switch (ocs) {
4784 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304785 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004786 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304787 switch (result) {
4788 case UPIU_TRANSACTION_RESPONSE:
4789 /*
4790 * get the response UPIU result to extract
4791 * the SCSI command status
4792 */
4793 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4794
4795 /*
4796 * get the result based on SCSI status response
4797 * to notify the SCSI midlayer of the command status
4798 */
4799 scsi_status = result & MASK_SCSI_STATUS;
4800 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304801
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004802 /*
4803 * Currently we are only supporting BKOPs exception
4804 * events hence we can ignore BKOPs exception event
4805 * during power management callbacks. BKOPs exception
4806 * event is not expected to be raised in runtime suspend
4807 * callback as it allows the urgent bkops.
4808 * During system suspend, we are anyway forcefully
4809 * disabling the bkops and if urgent bkops is needed
4810 * it will be enabled on system resume. Long term
4811 * solution could be to abort the system suspend if
4812 * UFS device needs urgent BKOPs.
4813 */
4814 if (!hba->pm_op_in_progress &&
Sayali Lokhande2824ec92020-02-10 19:40:44 -08004815 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr) &&
4816 schedule_work(&hba->eeh_work)) {
4817 /*
4818 * Prevent suspend once eeh_work is scheduled
4819 * to avoid deadlock between ufshcd_suspend
4820 * and exception event handler.
4821 */
4822 pm_runtime_get_noresume(hba->dev);
4823 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304824 break;
4825 case UPIU_TRANSACTION_REJECT_UPIU:
4826 /* TODO: handle Reject UPIU Response */
4827 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304828 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304829 "Reject UPIU not fully implemented\n");
4830 break;
4831 default:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304832 dev_err(hba->dev,
4833 "Unexpected request response code = %x\n",
4834 result);
Stanley Chue0347d82019-04-15 20:23:38 +08004835 result = DID_ERROR << 16;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304836 break;
4837 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304838 break;
4839 case OCS_ABORTED:
4840 result |= DID_ABORT << 16;
4841 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304842 case OCS_INVALID_COMMAND_STATUS:
4843 result |= DID_REQUEUE << 16;
4844 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304845 case OCS_INVALID_CMD_TABLE_ATTR:
4846 case OCS_INVALID_PRDT_ATTR:
4847 case OCS_MISMATCH_DATA_BUF_SIZE:
4848 case OCS_MISMATCH_RESP_UPIU_SIZE:
4849 case OCS_PEER_COMM_FAILURE:
4850 case OCS_FATAL_ERROR:
Satya Tangirala5e7341e2020-07-06 20:04:12 +00004851 case OCS_DEVICE_FATAL_ERROR:
4852 case OCS_INVALID_CRYPTO_CONFIG:
4853 case OCS_GENERAL_CRYPTO_ERROR:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304854 default:
4855 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304856 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004857 "OCS error from controller = %x for tag %d\n",
4858 ocs, lrbp->task_tag);
4859 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08004860 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304861 break;
4862 } /* end of switch */
4863
Can Guo2df74b62019-11-25 22:53:33 -08004864 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs)
Dolev Raviv66cc8202016-12-22 18:39:42 -08004865 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304866 return result;
4867}
4868
4869/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304870 * ufshcd_uic_cmd_compl - handle completion of uic command
4871 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304872 * @intr_status: interrupt status generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004873 *
4874 * Returns
4875 * IRQ_HANDLED - If interrupt is valid
4876 * IRQ_NONE - If invalid interrupt
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304877 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004878static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304879{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004880 irqreturn_t retval = IRQ_NONE;
4881
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304882 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304883 hba->active_uic_cmd->argument2 |=
4884 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304885 hba->active_uic_cmd->argument3 =
4886 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304887 complete(&hba->active_uic_cmd->done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004888 retval = IRQ_HANDLED;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304889 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304890
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004891 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004892 complete(hba->uic_async_done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004893 retval = IRQ_HANDLED;
4894 }
Stanley Chuaa5c6972020-06-15 15:22:35 +08004895
4896 if (retval == IRQ_HANDLED)
4897 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd,
4898 "complete");
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004899 return retval;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304900}
4901
4902/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004903 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304904 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004905 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304906 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004907static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4908 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304909{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304910 struct ufshcd_lrb *lrbp;
4911 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304912 int result;
4913 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004914
Dolev Ravive9d501b2014-07-01 12:22:37 +03004915 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4916 lrbp = &hba->lrb[index];
Stanley Chua3170372020-07-06 14:07:06 +08004917 lrbp->compl_time_stamp = ktime_get();
Dolev Ravive9d501b2014-07-01 12:22:37 +03004918 cmd = lrbp->cmd;
4919 if (cmd) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004920 ufshcd_add_command_trace(hba, index, "complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004921 result = ufshcd_transfer_rsp_status(hba, lrbp);
4922 scsi_dma_unmap(cmd);
4923 cmd->result = result;
4924 /* Mark completed command as NULL in LRB */
4925 lrbp->cmd = NULL;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004926 /* Do not touch lrbp after scsi done */
4927 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004928 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01004929 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
4930 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004931 if (hba->dev_cmd.complete) {
4932 ufshcd_add_command_trace(hba, index,
4933 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004934 complete(hba->dev_cmd.complete);
Lee Susman1a07f2d2016-12-22 18:42:03 -08004935 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03004936 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08004937 if (ufshcd_is_clkscaling_supported(hba))
4938 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004939 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304940
4941 /* clear corresponding bits of completed commands */
4942 hba->outstanding_reqs ^= completed_reqs;
4943
Sahitya Tummala856b3482014-09-25 15:32:34 +03004944 ufshcd_clk_scaling_update_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304945}
4946
4947/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004948 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4949 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004950 *
4951 * Returns
4952 * IRQ_HANDLED - If interrupt is valid
4953 * IRQ_NONE - If invalid interrupt
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004954 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004955static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004956{
4957 unsigned long completed_reqs;
4958 u32 tr_doorbell;
4959
4960 /* Resetting interrupt aggregation counters first and reading the
4961 * DOOR_BELL afterward allows us to handle all the completed requests.
4962 * In order to prevent other interrupts starvation the DB is read once
4963 * after reset. The down side of this solution is the possibility of
4964 * false interrupt if device completes another request after resetting
4965 * aggregation and before reading the DB.
4966 */
Alim Akhtarb638b5e2020-05-28 06:46:50 +05304967 if (ufshcd_is_intr_aggr_allowed(hba) &&
4968 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004969 ufshcd_reset_intr_aggr(hba);
4970
4971 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4972 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
4973
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004974 if (completed_reqs) {
4975 __ufshcd_transfer_req_compl(hba, completed_reqs);
4976 return IRQ_HANDLED;
4977 } else {
4978 return IRQ_NONE;
4979 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004980}
4981
4982/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304983 * ufshcd_disable_ee - disable exception event
4984 * @hba: per-adapter instance
4985 * @mask: exception event to disable
4986 *
4987 * Disables exception event in the device so that the EVENT_ALERT
4988 * bit is not set.
4989 *
4990 * Returns zero on success, non-zero error value on failure.
4991 */
4992static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4993{
4994 int err = 0;
4995 u32 val;
4996
4997 if (!(hba->ee_ctrl_mask & mask))
4998 goto out;
4999
5000 val = hba->ee_ctrl_mask & ~mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03005001 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005002 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305003 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5004 if (!err)
5005 hba->ee_ctrl_mask &= ~mask;
5006out:
5007 return err;
5008}
5009
5010/**
5011 * ufshcd_enable_ee - enable exception event
5012 * @hba: per-adapter instance
5013 * @mask: exception event to enable
5014 *
5015 * Enable corresponding exception event in the device to allow
5016 * device to alert host in critical scenarios.
5017 *
5018 * Returns zero on success, non-zero error value on failure.
5019 */
5020static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
5021{
5022 int err = 0;
5023 u32 val;
5024
5025 if (hba->ee_ctrl_mask & mask)
5026 goto out;
5027
5028 val = hba->ee_ctrl_mask | mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03005029 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005030 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305031 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5032 if (!err)
5033 hba->ee_ctrl_mask |= mask;
5034out:
5035 return err;
5036}
5037
5038/**
5039 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5040 * @hba: per-adapter instance
5041 *
5042 * Allow device to manage background operations on its own. Enabling
5043 * this might lead to inconsistent latencies during normal data transfers
5044 * as the device is allowed to manage its own way of handling background
5045 * operations.
5046 *
5047 * Returns zero on success, non-zero on failure.
5048 */
5049static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5050{
5051 int err = 0;
5052
5053 if (hba->auto_bkops_enabled)
5054 goto out;
5055
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005056 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005057 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305058 if (err) {
5059 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5060 __func__, err);
5061 goto out;
5062 }
5063
5064 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005065 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305066
5067 /* No need of URGENT_BKOPS exception from the device */
5068 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5069 if (err)
5070 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5071 __func__, err);
5072out:
5073 return err;
5074}
5075
5076/**
5077 * ufshcd_disable_auto_bkops - block device in doing background operations
5078 * @hba: per-adapter instance
5079 *
5080 * Disabling background operations improves command response latency but
5081 * has drawback of device moving into critical state where the device is
5082 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5083 * host is idle so that BKOPS are managed effectively without any negative
5084 * impacts.
5085 *
5086 * Returns zero on success, non-zero on failure.
5087 */
5088static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5089{
5090 int err = 0;
5091
5092 if (!hba->auto_bkops_enabled)
5093 goto out;
5094
5095 /*
5096 * If host assisted BKOPs is to be enabled, make sure
5097 * urgent bkops exception is allowed.
5098 */
5099 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5100 if (err) {
5101 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5102 __func__, err);
5103 goto out;
5104 }
5105
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005106 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005107 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305108 if (err) {
5109 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5110 __func__, err);
5111 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5112 goto out;
5113 }
5114
5115 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005116 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Asutosh Das24366c2a2019-11-25 22:53:30 -08005117 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305118out:
5119 return err;
5120}
5121
5122/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005123 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305124 * @hba: per adapter instance
5125 *
5126 * After a device reset the device may toggle the BKOPS_EN flag
5127 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005128 * as well. This function would change the auto-bkops state based on
5129 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305130 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005131static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305132{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005133 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5134 hba->auto_bkops_enabled = false;
5135 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5136 ufshcd_enable_auto_bkops(hba);
5137 } else {
5138 hba->auto_bkops_enabled = true;
5139 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5140 ufshcd_disable_auto_bkops(hba);
5141 }
Stanley Chu7b6668d2020-05-30 22:12:00 +08005142 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
Asutosh Das24366c2a2019-11-25 22:53:30 -08005143 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305144}
5145
5146static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5147{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005148 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305149 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5150}
5151
5152/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005153 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5154 * @hba: per-adapter instance
5155 * @status: bkops_status value
5156 *
5157 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5158 * flag in the device to permit background operations if the device
5159 * bkops_status is greater than or equal to "status" argument passed to
5160 * this function, disable otherwise.
5161 *
5162 * Returns 0 for success, non-zero in case of failure.
5163 *
5164 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5165 * to know whether auto bkops is enabled or disabled after this function
5166 * returns control to it.
5167 */
5168static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5169 enum bkops_status status)
5170{
5171 int err;
5172 u32 curr_status = 0;
5173
5174 err = ufshcd_get_bkops_status(hba, &curr_status);
5175 if (err) {
5176 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5177 __func__, err);
5178 goto out;
5179 } else if (curr_status > BKOPS_STATUS_MAX) {
5180 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5181 __func__, curr_status);
5182 err = -EINVAL;
5183 goto out;
5184 }
5185
5186 if (curr_status >= status)
5187 err = ufshcd_enable_auto_bkops(hba);
5188 else
5189 err = ufshcd_disable_auto_bkops(hba);
5190out:
5191 return err;
5192}
5193
5194/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305195 * ufshcd_urgent_bkops - handle urgent bkops exception event
5196 * @hba: per-adapter instance
5197 *
5198 * Enable fBackgroundOpsEn flag in the device to permit background
5199 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005200 *
5201 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5202 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305203 */
5204static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5205{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005206 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305207}
5208
5209static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5210{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005211 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305212 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5213}
5214
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005215static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5216{
5217 int err;
5218 u32 curr_status = 0;
5219
5220 if (hba->is_urgent_bkops_lvl_checked)
5221 goto enable_auto_bkops;
5222
5223 err = ufshcd_get_bkops_status(hba, &curr_status);
5224 if (err) {
5225 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5226 __func__, err);
5227 goto out;
5228 }
5229
5230 /*
5231 * We are seeing that some devices are raising the urgent bkops
5232 * exception events even when BKOPS status doesn't indicate performace
5233 * impacted or critical. Handle these device by determining their urgent
5234 * bkops status at runtime.
5235 */
5236 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5237 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5238 __func__, curr_status);
5239 /* update the current status as the urgent bkops level */
5240 hba->urgent_bkops_lvl = curr_status;
5241 hba->is_urgent_bkops_lvl_checked = true;
5242 }
5243
5244enable_auto_bkops:
5245 err = ufshcd_enable_auto_bkops(hba);
5246out:
5247 if (err < 0)
5248 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5249 __func__, err);
5250}
5251
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005252static int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable)
5253{
5254 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005255 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005256 enum query_opcode opcode;
5257
Stanley Chu79e35202020-05-08 16:01:15 +08005258 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005259 return 0;
5260
5261 if (!(enable ^ hba->wb_enabled))
5262 return 0;
5263 if (enable)
5264 opcode = UPIU_QUERY_OPCODE_SET_FLAG;
5265 else
5266 opcode = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5267
Stanley Chue31011a2020-05-22 16:32:11 +08005268 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005269 ret = ufshcd_query_flag_retry(hba, opcode,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005270 QUERY_FLAG_IDN_WB_EN, index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005271 if (ret) {
5272 dev_err(hba->dev, "%s write booster %s failed %d\n",
5273 __func__, enable ? "enable" : "disable", ret);
5274 return ret;
5275 }
5276
5277 hba->wb_enabled = enable;
5278 dev_dbg(hba->dev, "%s write booster %s %d\n",
5279 __func__, enable ? "enable" : "disable", ret);
5280
5281 return ret;
5282}
5283
5284static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set)
5285{
5286 int val;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005287 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005288
5289 if (set)
5290 val = UPIU_QUERY_OPCODE_SET_FLAG;
5291 else
5292 val = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5293
Stanley Chue31011a2020-05-22 16:32:11 +08005294 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005295 return ufshcd_query_flag_retry(hba, val,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005296 QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8,
5297 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005298}
5299
5300static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable)
5301{
5302 if (enable)
5303 ufshcd_wb_buf_flush_enable(hba);
5304 else
5305 ufshcd_wb_buf_flush_disable(hba);
5306
5307}
5308
5309static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba)
5310{
5311 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005312 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005313
Stanley Chu79e35202020-05-08 16:01:15 +08005314 if (!ufshcd_is_wb_allowed(hba) || hba->wb_buf_flush_enabled)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005315 return 0;
5316
Stanley Chue31011a2020-05-22 16:32:11 +08005317 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005318 ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005319 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005320 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005321 if (ret)
5322 dev_err(hba->dev, "%s WB - buf flush enable failed %d\n",
5323 __func__, ret);
5324 else
5325 hba->wb_buf_flush_enabled = true;
5326
5327 dev_dbg(hba->dev, "WB - Flush enabled: %d\n", ret);
5328 return ret;
5329}
5330
5331static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba)
5332{
5333 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005334 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005335
Stanley Chu79e35202020-05-08 16:01:15 +08005336 if (!ufshcd_is_wb_allowed(hba) || !hba->wb_buf_flush_enabled)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005337 return 0;
5338
Stanley Chue31011a2020-05-22 16:32:11 +08005339 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005340 ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005341 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN,
5342 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005343 if (ret) {
5344 dev_warn(hba->dev, "%s: WB - buf flush disable failed %d\n",
5345 __func__, ret);
5346 } else {
5347 hba->wb_buf_flush_enabled = false;
5348 dev_dbg(hba->dev, "WB - Flush disabled: %d\n", ret);
5349 }
5350
5351 return ret;
5352}
5353
5354static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba,
5355 u32 avail_buf)
5356{
5357 u32 cur_buf;
5358 int ret;
Stanley Chue31011a2020-05-22 16:32:11 +08005359 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005360
Stanley Chue31011a2020-05-22 16:32:11 +08005361 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005362 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5363 QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE,
Stanley Chue31011a2020-05-22 16:32:11 +08005364 index, 0, &cur_buf);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005365 if (ret) {
5366 dev_err(hba->dev, "%s dCurWriteBoosterBufferSize read failed %d\n",
5367 __func__, ret);
5368 return false;
5369 }
5370
5371 if (!cur_buf) {
5372 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n",
5373 cur_buf);
5374 return false;
5375 }
Stanley Chud14734ae2020-05-09 17:37:15 +08005376 /* Let it continue to flush when available buffer exceeds threshold */
5377 if (avail_buf < hba->vps->wb_flush_threshold)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005378 return true;
5379
5380 return false;
5381}
5382
Stanley Chu51dd9052020-05-22 16:32:12 +08005383static bool ufshcd_wb_need_flush(struct ufs_hba *hba)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005384{
5385 int ret;
5386 u32 avail_buf;
Stanley Chue31011a2020-05-22 16:32:11 +08005387 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005388
Stanley Chu79e35202020-05-08 16:01:15 +08005389 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005390 return false;
5391 /*
5392 * The ufs device needs the vcc to be ON to flush.
5393 * With user-space reduction enabled, it's enough to enable flush
5394 * by checking only the available buffer. The threshold
5395 * defined here is > 90% full.
5396 * With user-space preserved enabled, the current-buffer
5397 * should be checked too because the wb buffer size can reduce
5398 * when disk tends to be full. This info is provided by current
5399 * buffer (dCurrentWriteBoosterBufferSize). There's no point in
5400 * keeping vcc on when current buffer is empty.
5401 */
Stanley Chue31011a2020-05-22 16:32:11 +08005402 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005403 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5404 QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE,
Stanley Chue31011a2020-05-22 16:32:11 +08005405 index, 0, &avail_buf);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005406 if (ret) {
5407 dev_warn(hba->dev, "%s dAvailableWriteBoosterBufferSize read failed %d\n",
5408 __func__, ret);
5409 return false;
5410 }
5411
5412 if (!hba->dev_info.b_presrv_uspc_en) {
Stanley Chud14734ae2020-05-09 17:37:15 +08005413 if (avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005414 return true;
5415 return false;
5416 }
5417
5418 return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf);
5419}
5420
Stanley Chu51dd9052020-05-22 16:32:12 +08005421static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
5422{
5423 struct ufs_hba *hba = container_of(to_delayed_work(work),
5424 struct ufs_hba,
5425 rpm_dev_flush_recheck_work);
5426 /*
5427 * To prevent unnecessary VCC power drain after device finishes
5428 * WriteBooster buffer flush or Auto BKOPs, force runtime resume
5429 * after a certain delay to recheck the threshold by next runtime
5430 * suspend.
5431 */
5432 pm_runtime_get_sync(hba->dev);
5433 pm_runtime_put_sync(hba->dev);
5434}
5435
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305436/**
5437 * ufshcd_exception_event_handler - handle exceptions raised by device
5438 * @work: pointer to work data
5439 *
5440 * Read bExceptionEventStatus attribute from the device and handle the
5441 * exception event accordingly.
5442 */
5443static void ufshcd_exception_event_handler(struct work_struct *work)
5444{
5445 struct ufs_hba *hba;
5446 int err;
5447 u32 status = 0;
5448 hba = container_of(work, struct ufs_hba, eeh_work);
5449
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305450 pm_runtime_get_sync(hba->dev);
Stanley Chu03e1d282019-12-24 21:01:05 +08005451 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305452 err = ufshcd_get_ee_status(hba, &status);
5453 if (err) {
5454 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5455 __func__, err);
5456 goto out;
5457 }
5458
5459 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005460
5461 if (status & MASK_EE_URGENT_BKOPS)
5462 ufshcd_bkops_exception_event_handler(hba);
5463
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305464out:
Stanley Chu03e1d282019-12-24 21:01:05 +08005465 ufshcd_scsi_unblock_requests(hba);
Sayali Lokhande2824ec92020-02-10 19:40:44 -08005466 /*
5467 * pm_runtime_get_noresume is called while scheduling
5468 * eeh_work to avoid suspend racing with exception work.
5469 * Hence decrement usage counter using pm_runtime_put_noidle
5470 * to allow suspend on completion of exception event handler.
5471 */
5472 pm_runtime_put_noidle(hba->dev);
5473 pm_runtime_put(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305474 return;
5475}
5476
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005477/* Complete requests that have door-bell cleared */
5478static void ufshcd_complete_requests(struct ufs_hba *hba)
5479{
5480 ufshcd_transfer_req_compl(hba);
5481 ufshcd_tmc_handler(hba);
5482}
5483
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305484/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005485 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5486 * to recover from the DL NAC errors or not.
5487 * @hba: per-adapter instance
5488 *
5489 * Returns true if error handling is required, false otherwise
5490 */
5491static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5492{
5493 unsigned long flags;
5494 bool err_handling = true;
5495
5496 spin_lock_irqsave(hba->host->host_lock, flags);
5497 /*
5498 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5499 * device fatal error and/or DL NAC & REPLAY timeout errors.
5500 */
5501 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5502 goto out;
5503
5504 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5505 ((hba->saved_err & UIC_ERROR) &&
5506 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
5507 goto out;
5508
5509 if ((hba->saved_err & UIC_ERROR) &&
5510 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5511 int err;
5512 /*
5513 * wait for 50ms to see if we can get any other errors or not.
5514 */
5515 spin_unlock_irqrestore(hba->host->host_lock, flags);
5516 msleep(50);
5517 spin_lock_irqsave(hba->host->host_lock, flags);
5518
5519 /*
5520 * now check if we have got any other severe errors other than
5521 * DL NAC error?
5522 */
5523 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5524 ((hba->saved_err & UIC_ERROR) &&
5525 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5526 goto out;
5527
5528 /*
5529 * As DL NAC is the only error received so far, send out NOP
5530 * command to confirm if link is still active or not.
5531 * - If we don't get any response then do error recovery.
5532 * - If we get response then clear the DL NAC error bit.
5533 */
5534
5535 spin_unlock_irqrestore(hba->host->host_lock, flags);
5536 err = ufshcd_verify_dev_init(hba);
5537 spin_lock_irqsave(hba->host->host_lock, flags);
5538
5539 if (err)
5540 goto out;
5541
5542 /* Link seems to be alive hence ignore the DL NAC errors */
5543 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5544 hba->saved_err &= ~UIC_ERROR;
5545 /* clear NAC error */
5546 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5547 if (!hba->saved_uic_err) {
5548 err_handling = false;
5549 goto out;
5550 }
5551 }
5552out:
5553 spin_unlock_irqrestore(hba->host->host_lock, flags);
5554 return err_handling;
5555}
5556
5557/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305558 * ufshcd_err_handler - handle UFS errors that require s/w attention
5559 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305560 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305561static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305562{
5563 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305564 unsigned long flags;
5565 u32 err_xfer = 0;
5566 u32 err_tm = 0;
5567 int err = 0;
5568 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005569 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305570
5571 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305572
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305573 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005574 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305575
5576 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005577 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305578 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305579
5580 hba->ufshcd_state = UFSHCD_STATE_RESET;
5581 ufshcd_set_eh_in_progress(hba);
5582
5583 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005584 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005585
5586 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5587 bool ret;
5588
5589 spin_unlock_irqrestore(hba->host->host_lock, flags);
5590 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5591 ret = ufshcd_quirk_dl_nac_errors(hba);
5592 spin_lock_irqsave(hba->host->host_lock, flags);
5593 if (!ret)
5594 goto skip_err_handling;
5595 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005596 if ((hba->saved_err & INT_FATAL_ERRORS) ||
Stanley Chu82174442019-05-21 14:44:54 +08005597 (hba->saved_err & UFSHCD_UIC_HIBERN8_MASK) ||
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005598 ((hba->saved_err & UIC_ERROR) &&
5599 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5600 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5601 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5602 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305603
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005604 /*
5605 * if host reset is required then skip clearing the pending
Can Guo2df74b62019-11-25 22:53:33 -08005606 * transfers forcefully because they will get cleared during
5607 * host reset and restore
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005608 */
5609 if (needs_reset)
5610 goto skip_pending_xfer_clear;
5611
5612 /* release lock as clear command might sleep */
5613 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305614 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005615 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5616 if (ufshcd_clear_cmd(hba, tag)) {
5617 err_xfer = true;
5618 goto lock_skip_pending_xfer_clear;
5619 }
5620 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305621
5622 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005623 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5624 if (ufshcd_clear_tm_cmd(hba, tag)) {
5625 err_tm = true;
5626 goto lock_skip_pending_xfer_clear;
5627 }
5628 }
5629
5630lock_skip_pending_xfer_clear:
5631 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305632
5633 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005634 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305635
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005636 if (err_xfer || err_tm)
5637 needs_reset = true;
5638
5639skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305640 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005641 if (needs_reset) {
5642 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5643
5644 /*
5645 * ufshcd_reset_and_restore() does the link reinitialization
5646 * which will need atleast one empty doorbell slot to send the
5647 * device management commands (NOP and query commands).
5648 * If there is no slot empty at this moment then free up last
5649 * slot forcefully.
5650 */
5651 if (hba->outstanding_reqs == max_doorbells)
5652 __ufshcd_transfer_req_compl(hba,
5653 (1UL << (hba->nutrs - 1)));
5654
5655 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305656 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005657 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305658 if (err) {
5659 dev_err(hba->dev, "%s: reset and restore failed\n",
5660 __func__);
5661 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5662 }
5663 /*
5664 * Inform scsi mid-layer that we did reset and allow to handle
5665 * Unit Attention properly.
5666 */
5667 scsi_report_bus_reset(hba->host, 0);
5668 hba->saved_err = 0;
5669 hba->saved_uic_err = 0;
5670 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005671
Yaniv Gardi583fa622016-03-10 17:37:13 +02005672skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005673 if (!needs_reset) {
5674 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5675 if (hba->saved_err || hba->saved_uic_err)
5676 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5677 __func__, hba->saved_err, hba->saved_uic_err);
5678 }
5679
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305680 ufshcd_clear_eh_in_progress(hba);
5681
5682out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005683 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani38135532018-05-03 16:37:18 +05305684 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005685 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305686 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305687}
5688
5689/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305690 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5691 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005692 *
5693 * Returns
5694 * IRQ_HANDLED - If interrupt is valid
5695 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305696 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005697static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305698{
5699 u32 reg;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005700 irqreturn_t retval = IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305701
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005702 /* PHY layer lane error */
5703 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5704 /* Ignore LINERESET indication, as this is not an error */
5705 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005706 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005707 /*
5708 * To know whether this error is fatal or not, DB timeout
5709 * must be checked but this error is handled separately.
5710 */
5711 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
Stanley Chu48d5b972019-07-10 21:38:18 +08005712 ufshcd_update_reg_hist(&hba->ufs_stats.pa_err, reg);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005713 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005714 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005715
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305716 /* PA_INIT_ERROR is fatal and needs UIC reset */
5717 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005718 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
5719 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005720 ufshcd_update_reg_hist(&hba->ufs_stats.dl_err, reg);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005721
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005722 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
5723 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
5724 else if (hba->dev_quirks &
5725 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5726 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5727 hba->uic_error |=
5728 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5729 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5730 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5731 }
5732 retval |= IRQ_HANDLED;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005733 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305734
5735 /* UIC NL/TL/DME errors needs software retry */
5736 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005737 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
5738 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005739 ufshcd_update_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305740 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005741 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005742 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305743
5744 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005745 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
5746 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005747 ufshcd_update_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305748 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005749 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005750 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305751
5752 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005753 if ((reg & UIC_DME_ERROR) &&
5754 (reg & UIC_DME_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005755 ufshcd_update_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305756 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005757 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005758 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305759
5760 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5761 __func__, hba->uic_error);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005762 return retval;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305763}
5764
Stanley Chu82174442019-05-21 14:44:54 +08005765static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
5766 u32 intr_mask)
5767{
Stanley Chu5a244e02020-01-29 18:52:50 +08005768 if (!ufshcd_is_auto_hibern8_supported(hba) ||
5769 !ufshcd_is_auto_hibern8_enabled(hba))
Stanley Chu82174442019-05-21 14:44:54 +08005770 return false;
5771
5772 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
5773 return false;
5774
5775 if (hba->active_uic_cmd &&
5776 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
5777 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
5778 return false;
5779
5780 return true;
5781}
5782
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305783/**
5784 * ufshcd_check_errors - Check for errors that need s/w attention
5785 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005786 *
5787 * Returns
5788 * IRQ_HANDLED - If interrupt is valid
5789 * IRQ_NONE - If invalid interrupt
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305790 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005791static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305792{
5793 bool queue_eh_work = false;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005794 irqreturn_t retval = IRQ_NONE;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305795
Stanley Chud3c615b2019-07-10 21:38:19 +08005796 if (hba->errors & INT_FATAL_ERRORS) {
5797 ufshcd_update_reg_hist(&hba->ufs_stats.fatal_err, hba->errors);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305798 queue_eh_work = true;
Stanley Chud3c615b2019-07-10 21:38:19 +08005799 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305800
5801 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305802 hba->uic_error = 0;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005803 retval = ufshcd_update_uic_error(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305804 if (hba->uic_error)
5805 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305806 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305807
Stanley Chu82174442019-05-21 14:44:54 +08005808 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
5809 dev_err(hba->dev,
5810 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
5811 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
5812 "Enter" : "Exit",
5813 hba->errors, ufshcd_get_upmcrs(hba));
Stanley Chud3c615b2019-07-10 21:38:19 +08005814 ufshcd_update_reg_hist(&hba->ufs_stats.auto_hibern8_err,
5815 hba->errors);
Stanley Chu82174442019-05-21 14:44:54 +08005816 queue_eh_work = true;
5817 }
5818
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305819 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005820 /*
5821 * update the transfer error masks to sticky bits, let's do this
5822 * irrespective of current ufshcd_state.
5823 */
5824 hba->saved_err |= hba->errors;
5825 hba->saved_uic_err |= hba->uic_error;
5826
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305827 /* handle fatal errors only when link is functional */
5828 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5829 /* block commands from scsi mid-layer */
Subhash Jadavani38135532018-05-03 16:37:18 +05305830 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305831
Zang Leigang141f8162016-11-16 11:29:37 +08005832 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08005833
5834 /* dump controller state before resetting */
5835 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5836 bool pr_prdt = !!(hba->saved_err &
5837 SYSTEM_BUS_FATAL_ERROR);
5838
5839 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5840 __func__, hba->saved_err,
5841 hba->saved_uic_err);
5842
5843 ufshcd_print_host_regs(hba);
5844 ufshcd_print_pwr_info(hba);
5845 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5846 ufshcd_print_trs(hba, hba->outstanding_reqs,
5847 pr_prdt);
5848 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305849 schedule_work(&hba->eh_work);
5850 }
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005851 retval |= IRQ_HANDLED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305852 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305853 /*
5854 * if (!queue_eh_work) -
5855 * Other errors are either non-fatal where host recovers
5856 * itself without s/w intervention or errors that will be
5857 * handled by the SCSI core layer.
5858 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005859 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305860}
5861
Bart Van Assche69a6c262019-12-09 10:13:09 -08005862struct ctm_info {
5863 struct ufs_hba *hba;
5864 unsigned long pending;
5865 unsigned int ncpl;
5866};
5867
5868static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved)
5869{
5870 struct ctm_info *const ci = priv;
5871 struct completion *c;
5872
5873 WARN_ON_ONCE(reserved);
5874 if (test_bit(req->tag, &ci->pending))
5875 return true;
5876 ci->ncpl++;
5877 c = req->end_io_data;
5878 if (c)
5879 complete(c);
5880 return true;
5881}
5882
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305883/**
5884 * ufshcd_tmc_handler - handle task management function completion
5885 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005886 *
5887 * Returns
5888 * IRQ_HANDLED - If interrupt is valid
5889 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305890 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005891static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305892{
Bart Van Assche69a6c262019-12-09 10:13:09 -08005893 struct request_queue *q = hba->tmf_queue;
5894 struct ctm_info ci = {
5895 .hba = hba,
5896 .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL),
5897 };
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305898
Bart Van Assche69a6c262019-12-09 10:13:09 -08005899 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci);
5900 return ci.ncpl ? IRQ_HANDLED : IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305901}
5902
5903/**
5904 * ufshcd_sl_intr - Interrupt service routine
5905 * @hba: per adapter instance
5906 * @intr_status: contains interrupts generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005907 *
5908 * Returns
5909 * IRQ_HANDLED - If interrupt is valid
5910 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305911 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005912static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305913{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005914 irqreturn_t retval = IRQ_NONE;
5915
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305916 hba->errors = UFSHCD_ERROR_MASK & intr_status;
Stanley Chu82174442019-05-21 14:44:54 +08005917
5918 if (ufshcd_is_auto_hibern8_error(hba, intr_status))
5919 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
5920
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305921 if (hba->errors)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005922 retval |= ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305923
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305924 if (intr_status & UFSHCD_UIC_MASK)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005925 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305926
5927 if (intr_status & UTP_TASK_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005928 retval |= ufshcd_tmc_handler(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305929
5930 if (intr_status & UTP_TRANSFER_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005931 retval |= ufshcd_transfer_req_compl(hba);
5932
5933 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305934}
5935
5936/**
5937 * ufshcd_intr - Main interrupt service routine
5938 * @irq: irq number
5939 * @__hba: pointer to adapter instance
5940 *
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005941 * Returns
5942 * IRQ_HANDLED - If interrupt is valid
5943 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305944 */
5945static irqreturn_t ufshcd_intr(int irq, void *__hba)
5946{
Adrian Hunter127d5f72020-08-11 16:39:36 +03005947 u32 intr_status, enabled_intr_status = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305948 irqreturn_t retval = IRQ_NONE;
5949 struct ufs_hba *hba = __hba;
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305950 int retries = hba->nutrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305951
5952 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305953 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305954
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305955 /*
5956 * There could be max of hba->nutrs reqs in flight and in worst case
5957 * if the reqs get finished 1 by 1 after the interrupt status is
5958 * read, make sure we handle them by checking the interrupt status
5959 * again in a loop until we process all of the reqs before returning.
5960 */
Adrian Hunter127d5f72020-08-11 16:39:36 +03005961 while (intr_status && retries--) {
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305962 enabled_intr_status =
5963 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
5964 if (intr_status)
5965 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005966 if (enabled_intr_status)
5967 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005968
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305969 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Adrian Hunter127d5f72020-08-11 16:39:36 +03005970 }
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305971
Adrian Hunter6337f582020-08-11 16:39:35 +03005972 if (enabled_intr_status && retval == IRQ_NONE) {
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005973 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n",
5974 __func__, intr_status);
5975 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
5976 }
5977
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305978 spin_unlock(hba->host->host_lock);
5979 return retval;
5980}
5981
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305982static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5983{
5984 int err = 0;
5985 u32 mask = 1 << tag;
5986 unsigned long flags;
5987
5988 if (!test_bit(tag, &hba->outstanding_tasks))
5989 goto out;
5990
5991 spin_lock_irqsave(hba->host->host_lock, flags);
Alim Akhtar1399c5b2018-05-06 15:44:15 +05305992 ufshcd_utmrl_clear(hba, tag);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305993 spin_unlock_irqrestore(hba->host->host_lock, flags);
5994
5995 /* poll for max. 1 sec to clear door bell register by h/w */
5996 err = ufshcd_wait_for_register(hba,
5997 REG_UTP_TASK_REQ_DOOR_BELL,
Bart Van Assche5cac1092020-05-07 15:27:50 -07005998 mask, 0, 1000, 1000);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305999out:
6000 return err;
6001}
6002
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006003static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
6004 struct utp_task_req_desc *treq, u8 tm_function)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306005{
Bart Van Assche69a6c262019-12-09 10:13:09 -08006006 struct request_queue *q = hba->tmf_queue;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006007 struct Scsi_Host *host = hba->host;
Bart Van Assche69a6c262019-12-09 10:13:09 -08006008 DECLARE_COMPLETION_ONSTACK(wait);
6009 struct request *req;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306010 unsigned long flags;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006011 int free_slot, task_tag, err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306012
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306013 /*
6014 * Get free slot, sleep if slots are unavailable.
6015 * Even though we use wait_event() which sleeps indefinitely,
6016 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
6017 */
Bart Van Assche69a6c262019-12-09 10:13:09 -08006018 req = blk_get_request(q, REQ_OP_DRV_OUT, BLK_MQ_REQ_RESERVED);
6019 req->end_io_data = &wait;
6020 free_slot = req->tag;
6021 WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006022 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306023
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306024 spin_lock_irqsave(host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306025 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306026
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006027 treq->req_header.dword_0 |= cpu_to_be32(task_tag);
6028
6029 memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq));
Kiwoong Kimd2877be2016-11-10 21:16:15 +09006030 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
6031
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306032 /* send command to the controller */
6033 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02006034
6035 /* Make sure descriptors are ready before ringing the task doorbell */
6036 wmb();
6037
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306038 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07006039 /* Make sure that doorbell is committed immediately */
6040 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306041
6042 spin_unlock_irqrestore(host->host_lock, flags);
6043
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03006044 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_send");
6045
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306046 /* wait until the task management command is completed */
Bart Van Assche69a6c262019-12-09 10:13:09 -08006047 err = wait_for_completion_io_timeout(&wait,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306048 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306049 if (!err) {
Bart Van Assche69a6c262019-12-09 10:13:09 -08006050 /*
6051 * Make sure that ufshcd_compl_tm() does not trigger a
6052 * use-after-free.
6053 */
6054 req->end_io_data = NULL;
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03006055 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete_err");
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306056 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6057 __func__, tm_function);
6058 if (ufshcd_clear_tm_cmd(hba, free_slot))
6059 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
6060 __func__, free_slot);
6061 err = -ETIMEDOUT;
6062 } else {
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006063 err = 0;
6064 memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq));
6065
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03006066 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306067 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306068
Stanley Chub5572172019-08-19 21:43:28 +08006069 spin_lock_irqsave(hba->host->host_lock, flags);
6070 __clear_bit(free_slot, &hba->outstanding_tasks);
6071 spin_unlock_irqrestore(hba->host->host_lock, flags);
6072
Bart Van Assche69a6c262019-12-09 10:13:09 -08006073 blk_put_request(req);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306074
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006075 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306076 return err;
6077}
6078
6079/**
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006080 * ufshcd_issue_tm_cmd - issues task management commands to controller
6081 * @hba: per adapter instance
6082 * @lun_id: LUN ID to which TM command is sent
6083 * @task_id: task ID to which the TM command is applicable
6084 * @tm_function: task management function opcode
6085 * @tm_response: task management service response return value
6086 *
6087 * Returns non-zero value on error, zero on success.
6088 */
6089static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
6090 u8 tm_function, u8 *tm_response)
6091{
6092 struct utp_task_req_desc treq = { { 0 }, };
6093 int ocs_value, err;
6094
6095 /* Configure task request descriptor */
6096 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6097 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6098
6099 /* Configure task request UPIU */
6100 treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) |
6101 cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24);
6102 treq.req_header.dword_1 = cpu_to_be32(tm_function << 16);
6103
6104 /*
6105 * The host shall provide the same value for LUN field in the basic
6106 * header and for Input Parameter.
6107 */
6108 treq.input_param1 = cpu_to_be32(lun_id);
6109 treq.input_param2 = cpu_to_be32(task_id);
6110
6111 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
6112 if (err == -ETIMEDOUT)
6113 return err;
6114
6115 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6116 if (ocs_value != OCS_SUCCESS)
6117 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
6118 __func__, ocs_value);
6119 else if (tm_response)
6120 *tm_response = be32_to_cpu(treq.output_param1) &
6121 MASK_TM_SERVICE_RESP;
6122 return err;
6123}
6124
6125/**
Avri Altman5e0a86e2018-10-07 17:30:37 +03006126 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
6127 * @hba: per-adapter instance
6128 * @req_upiu: upiu request
6129 * @rsp_upiu: upiu reply
Avri Altman5e0a86e2018-10-07 17:30:37 +03006130 * @desc_buff: pointer to descriptor buffer, NULL if NA
6131 * @buff_len: descriptor size, 0 if NA
Bart Van Assched0e97602019-10-29 16:07:08 -07006132 * @cmd_type: specifies the type (NOP, Query...)
Avri Altman5e0a86e2018-10-07 17:30:37 +03006133 * @desc_op: descriptor operation
6134 *
6135 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
6136 * Therefore, it "rides" the device management infrastructure: uses its tag and
6137 * tasks work queues.
6138 *
6139 * Since there is only one available tag for device management commands,
6140 * the caller is expected to hold the hba->dev_cmd.lock mutex.
6141 */
6142static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
6143 struct utp_upiu_req *req_upiu,
6144 struct utp_upiu_req *rsp_upiu,
6145 u8 *desc_buff, int *buff_len,
Bart Van Assche7f674c32019-10-29 16:07:09 -07006146 enum dev_cmd_type cmd_type,
Avri Altman5e0a86e2018-10-07 17:30:37 +03006147 enum query_opcode desc_op)
6148{
Bart Van Assche7252a362019-12-09 10:13:08 -08006149 struct request_queue *q = hba->cmd_queue;
6150 struct request *req;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006151 struct ufshcd_lrb *lrbp;
6152 int err = 0;
6153 int tag;
6154 struct completion wait;
6155 unsigned long flags;
Bean Huoa23064c2020-07-06 14:39:36 +02006156 u8 upiu_flags;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006157
6158 down_read(&hba->clk_scaling_lock);
6159
Bart Van Assche7252a362019-12-09 10:13:08 -08006160 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03006161 if (IS_ERR(req)) {
6162 err = PTR_ERR(req);
6163 goto out_unlock;
6164 }
Bart Van Assche7252a362019-12-09 10:13:08 -08006165 tag = req->tag;
6166 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Avri Altman5e0a86e2018-10-07 17:30:37 +03006167
6168 init_completion(&wait);
6169 lrbp = &hba->lrb[tag];
6170 WARN_ON(lrbp->cmd);
6171
6172 lrbp->cmd = NULL;
6173 lrbp->sense_bufflen = 0;
6174 lrbp->sense_buffer = NULL;
6175 lrbp->task_tag = tag;
6176 lrbp->lun = 0;
6177 lrbp->intr_cmd = true;
Satya Tangiraladf043c742020-07-06 20:04:14 +00006178 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006179 hba->dev_cmd.type = cmd_type;
6180
6181 switch (hba->ufs_version) {
6182 case UFSHCI_VERSION_10:
6183 case UFSHCI_VERSION_11:
6184 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
6185 break;
6186 default:
6187 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
6188 break;
6189 }
6190
6191 /* update the task tag in the request upiu */
6192 req_upiu->header.dword_0 |= cpu_to_be32(tag);
6193
6194 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
6195
6196 /* just copy the upiu request as it is */
6197 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
6198 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
6199 /* The Data Segment Area is optional depending upon the query
6200 * function value. for WRITE DESCRIPTOR, the data segment
6201 * follows right after the tsf.
6202 */
6203 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
6204 *buff_len = 0;
6205 }
6206
6207 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
6208
6209 hba->dev_cmd.complete = &wait;
6210
6211 /* Make sure descriptors are ready before ringing the doorbell */
6212 wmb();
6213 spin_lock_irqsave(hba->host->host_lock, flags);
6214 ufshcd_send_command(hba, tag);
6215 spin_unlock_irqrestore(hba->host->host_lock, flags);
6216
6217 /*
6218 * ignore the returning value here - ufshcd_check_query_response is
6219 * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
6220 * read the response directly ignoring all errors.
6221 */
6222 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT);
6223
6224 /* just copy the upiu response as it is */
6225 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
Avri Altman4bbbe242019-02-20 09:11:13 +02006226 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
6227 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
6228 u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
6229 MASK_QUERY_DATA_SEG_LEN;
6230
6231 if (*buff_len >= resp_len) {
6232 memcpy(desc_buff, descp, resp_len);
6233 *buff_len = resp_len;
6234 } else {
Bean Huo3d4881d2019-11-12 23:34:35 +01006235 dev_warn(hba->dev,
6236 "%s: rsp size %d is bigger than buffer size %d",
6237 __func__, resp_len, *buff_len);
Avri Altman4bbbe242019-02-20 09:11:13 +02006238 *buff_len = 0;
6239 err = -EINVAL;
6240 }
6241 }
Avri Altman5e0a86e2018-10-07 17:30:37 +03006242
Bart Van Assche7252a362019-12-09 10:13:08 -08006243 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03006244out_unlock:
Avri Altman5e0a86e2018-10-07 17:30:37 +03006245 up_read(&hba->clk_scaling_lock);
6246 return err;
6247}
6248
6249/**
6250 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
6251 * @hba: per-adapter instance
6252 * @req_upiu: upiu request
6253 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
6254 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
6255 * @desc_buff: pointer to descriptor buffer, NULL if NA
6256 * @buff_len: descriptor size, 0 if NA
6257 * @desc_op: descriptor operation
6258 *
6259 * Supports UTP Transfer requests (nop and query), and UTP Task
6260 * Management requests.
6261 * It is up to the caller to fill the upiu conent properly, as it will
6262 * be copied without any further input validations.
6263 */
6264int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
6265 struct utp_upiu_req *req_upiu,
6266 struct utp_upiu_req *rsp_upiu,
6267 int msgcode,
6268 u8 *desc_buff, int *buff_len,
6269 enum query_opcode desc_op)
6270{
6271 int err;
Bart Van Assche7f674c32019-10-29 16:07:09 -07006272 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006273 struct utp_task_req_desc treq = { { 0 }, };
6274 int ocs_value;
6275 u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC;
6276
Avri Altman5e0a86e2018-10-07 17:30:37 +03006277 switch (msgcode) {
6278 case UPIU_TRANSACTION_NOP_OUT:
6279 cmd_type = DEV_CMD_TYPE_NOP;
6280 /* fall through */
6281 case UPIU_TRANSACTION_QUERY_REQ:
6282 ufshcd_hold(hba, false);
6283 mutex_lock(&hba->dev_cmd.lock);
6284 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
6285 desc_buff, buff_len,
6286 cmd_type, desc_op);
6287 mutex_unlock(&hba->dev_cmd.lock);
6288 ufshcd_release(hba);
6289
6290 break;
6291 case UPIU_TRANSACTION_TASK_REQ:
6292 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6293 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6294
6295 memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu));
6296
6297 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
6298 if (err == -ETIMEDOUT)
6299 break;
6300
6301 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6302 if (ocs_value != OCS_SUCCESS) {
6303 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
6304 ocs_value);
6305 break;
6306 }
6307
6308 memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu));
6309
6310 break;
6311 default:
6312 err = -EINVAL;
6313
6314 break;
6315 }
6316
Avri Altman5e0a86e2018-10-07 17:30:37 +03006317 return err;
6318}
6319
6320/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306321 * ufshcd_eh_device_reset_handler - device reset handler registered to
6322 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306323 * @cmd: SCSI command pointer
6324 *
6325 * Returns SUCCESS/FAILED
6326 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306327static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306328{
6329 struct Scsi_Host *host;
6330 struct ufs_hba *hba;
6331 unsigned int tag;
6332 u32 pos;
6333 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306334 u8 resp = 0xF;
6335 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306336 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306337
6338 host = cmd->device->host;
6339 hba = shost_priv(host);
6340 tag = cmd->request->tag;
6341
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306342 lrbp = &hba->lrb[tag];
6343 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
6344 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306345 if (!err)
6346 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306347 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306348 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306349
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306350 /* clear the commands that were pending for corresponding LUN */
6351 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6352 if (hba->lrb[pos].lun == lrbp->lun) {
6353 err = ufshcd_clear_cmd(hba, pos);
6354 if (err)
6355 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306356 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306357 }
6358 spin_lock_irqsave(host->host_lock, flags);
6359 ufshcd_transfer_req_compl(hba);
6360 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08006361
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306362out:
Gilad Broner7fabb772017-02-03 16:56:50 -08006363 hba->req_abort_count = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08006364 ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306365 if (!err) {
6366 err = SUCCESS;
6367 } else {
6368 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6369 err = FAILED;
6370 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306371 return err;
6372}
6373
Gilad Bronere0b299e2017-02-03 16:56:40 -08006374static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6375{
6376 struct ufshcd_lrb *lrbp;
6377 int tag;
6378
6379 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6380 lrbp = &hba->lrb[tag];
6381 lrbp->req_abort_skip = true;
6382 }
6383}
6384
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306385/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306386 * ufshcd_abort - abort a specific command
6387 * @cmd: SCSI command pointer
6388 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306389 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6390 * command, and in host controller by clearing the door-bell register. There can
6391 * be race between controller sending the command to the device while abort is
6392 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6393 * really issued and then try to abort it.
6394 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306395 * Returns SUCCESS/FAILED
6396 */
6397static int ufshcd_abort(struct scsi_cmnd *cmd)
6398{
6399 struct Scsi_Host *host;
6400 struct ufs_hba *hba;
6401 unsigned long flags;
6402 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306403 int err = 0;
6404 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306405 u8 resp = 0xF;
6406 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03006407 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306408
6409 host = cmd->device->host;
6410 hba = shost_priv(host);
6411 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08006412 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02006413 if (!ufshcd_valid_tag(hba, tag)) {
6414 dev_err(hba->dev,
6415 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6416 __func__, tag, cmd, cmd->request);
6417 BUG();
6418 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306419
Dolev Ravive7d38252016-12-22 18:40:07 -08006420 /*
6421 * Task abort to the device W-LUN is illegal. When this command
6422 * will fail, due to spec violation, scsi err handling next step
6423 * will be to send LU reset which, again, is a spec violation.
6424 * To avoid these unnecessary/illegal step we skip to the last error
6425 * handling stage: reset and restore.
6426 */
6427 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
6428 return ufshcd_eh_host_reset_handler(cmd);
6429
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006430 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03006431 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02006432 /* If command is already aborted/completed, return SUCCESS */
6433 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6434 dev_err(hba->dev,
6435 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6436 __func__, tag, hba->outstanding_reqs, reg);
6437 goto out;
6438 }
6439
Dolev Ravive9d501b2014-07-01 12:22:37 +03006440 if (!(reg & (1 << tag))) {
6441 dev_err(hba->dev,
6442 "%s: cmd was completed, but without a notifying intr, tag = %d",
6443 __func__, tag);
6444 }
6445
Dolev Raviv66cc8202016-12-22 18:39:42 -08006446 /* Print Transfer Request of aborted task */
6447 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08006448
Gilad Broner7fabb772017-02-03 16:56:50 -08006449 /*
6450 * Print detailed info about aborted request.
6451 * As more than one request might get aborted at the same time,
6452 * print full information only for the first aborted request in order
6453 * to reduce repeated printouts. For other aborted requests only print
6454 * basic details.
6455 */
6456 scsi_print_command(hba->lrb[tag].cmd);
6457 if (!hba->req_abort_count) {
Stanley Chu8808b4e2019-07-10 21:38:21 +08006458 ufshcd_update_reg_hist(&hba->ufs_stats.task_abort, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08006459 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08006460 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08006461 ufshcd_print_pwr_info(hba);
6462 ufshcd_print_trs(hba, 1 << tag, true);
6463 } else {
6464 ufshcd_print_trs(hba, 1 << tag, false);
6465 }
6466 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08006467
6468 /* Skip task abort in case previous aborts failed and report failure */
6469 if (lrbp->req_abort_skip) {
6470 err = -EIO;
6471 goto out;
6472 }
6473
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306474 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6475 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6476 UFS_QUERY_TASK, &resp);
6477 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6478 /* cmd pending in the device */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006479 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
6480 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306481 break;
6482 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306483 /*
6484 * cmd not pending in the device, check if it is
6485 * in transition.
6486 */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006487 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
6488 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306489 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6490 if (reg & (1 << tag)) {
6491 /* sleep for max. 200us to stabilize */
6492 usleep_range(100, 200);
6493 continue;
6494 }
6495 /* command completed already */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006496 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
6497 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306498 goto out;
6499 } else {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006500 dev_err(hba->dev,
6501 "%s: no response from device. tag = %d, err %d\n",
6502 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306503 if (!err)
6504 err = resp; /* service response error */
6505 goto out;
6506 }
6507 }
6508
6509 if (!poll_cnt) {
6510 err = -EBUSY;
6511 goto out;
6512 }
6513
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306514 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6515 UFS_ABORT_TASK, &resp);
6516 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006517 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306518 err = resp; /* service response error */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006519 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
6520 __func__, tag, err);
6521 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306522 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306523 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306524
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306525 err = ufshcd_clear_cmd(hba, tag);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006526 if (err) {
6527 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
6528 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306529 goto out;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006530 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306531
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306532 scsi_dma_unmap(cmd);
6533
6534 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02006535 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306536 hba->lrb[tag].cmd = NULL;
6537 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306538
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306539out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306540 if (!err) {
6541 err = SUCCESS;
6542 } else {
6543 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08006544 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306545 err = FAILED;
6546 }
6547
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006548 /*
6549 * This ufshcd_release() corresponds to the original scsi cmd that got
6550 * aborted here (as we won't get any IRQ for it).
6551 */
6552 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306553 return err;
6554}
6555
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306556/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306557 * ufshcd_host_reset_and_restore - reset and restore host controller
6558 * @hba: per-adapter instance
6559 *
6560 * Note that host controller reset may issue DME_RESET to
6561 * local and remote (device) Uni-Pro stack and the attributes
6562 * are reset to default state.
6563 *
6564 * Returns zero on success, non-zero on failure
6565 */
6566static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6567{
6568 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306569 unsigned long flags;
6570
Can Guo2df74b62019-11-25 22:53:33 -08006571 /*
6572 * Stop the host controller and complete the requests
6573 * cleared by h/w
6574 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07006575 ufshcd_hba_stop(hba);
6576
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306577 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo2df74b62019-11-25 22:53:33 -08006578 hba->silence_err_logs = true;
6579 ufshcd_complete_requests(hba);
6580 hba->silence_err_logs = false;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306581 spin_unlock_irqrestore(hba->host->host_lock, flags);
6582
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08006583 /* scale up clocks to max frequency before full reinitialization */
Subhash Jadavani394b9492020-03-26 02:25:40 -07006584 ufshcd_set_clk_freq(hba, true);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08006585
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306586 err = ufshcd_hba_enable(hba);
6587 if (err)
6588 goto out;
6589
6590 /* Establish the link again and restore the device */
Bean Huo1b9e2142020-01-20 14:08:15 +01006591 err = ufshcd_probe_hba(hba, false);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006592
6593 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306594 err = -EIO;
6595out:
6596 if (err)
6597 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
Stanley Chu8808b4e2019-07-10 21:38:21 +08006598 ufshcd_update_reg_hist(&hba->ufs_stats.host_reset, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306599 return err;
6600}
6601
6602/**
6603 * ufshcd_reset_and_restore - reset and re-initialize host/device
6604 * @hba: per-adapter instance
6605 *
6606 * Reset and recover device, host and re-establish link. This
6607 * is helpful to recover the communication in fatal error conditions.
6608 *
6609 * Returns zero on success, non-zero on failure
6610 */
6611static int ufshcd_reset_and_restore(struct ufs_hba *hba)
6612{
6613 int err = 0;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006614 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306615
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006616 do {
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07006617 /* Reset the attached device */
6618 ufshcd_vops_device_reset(hba);
6619
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006620 err = ufshcd_host_reset_and_restore(hba);
6621 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306622
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306623 return err;
6624}
6625
6626/**
6627 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006628 * @cmd: SCSI command pointer
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306629 *
6630 * Returns SUCCESS/FAILED
6631 */
6632static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
6633{
6634 int err;
6635 unsigned long flags;
6636 struct ufs_hba *hba;
6637
6638 hba = shost_priv(cmd->device->host);
6639
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006640 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306641 /*
6642 * Check if there is any race with fatal error handling.
6643 * If so, wait for it to complete. Even though fatal error
6644 * handling does reset and restore in some cases, don't assume
6645 * anything out of it. We are just avoiding race here.
6646 */
6647 do {
6648 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306649 if (!(work_pending(&hba->eh_work) ||
Zang Leigang8dc0da72017-06-24 19:14:32 +08006650 hba->ufshcd_state == UFSHCD_STATE_RESET ||
6651 hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306652 break;
6653 spin_unlock_irqrestore(hba->host->host_lock, flags);
6654 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306655 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306656 } while (1);
6657
6658 hba->ufshcd_state = UFSHCD_STATE_RESET;
6659 ufshcd_set_eh_in_progress(hba);
6660 spin_unlock_irqrestore(hba->host->host_lock, flags);
6661
6662 err = ufshcd_reset_and_restore(hba);
6663
6664 spin_lock_irqsave(hba->host->host_lock, flags);
6665 if (!err) {
6666 err = SUCCESS;
6667 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6668 } else {
6669 err = FAILED;
6670 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6671 }
6672 ufshcd_clear_eh_in_progress(hba);
6673 spin_unlock_irqrestore(hba->host->host_lock, flags);
6674
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006675 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306676 return err;
6677}
6678
6679/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006680 * ufshcd_get_max_icc_level - calculate the ICC level
6681 * @sup_curr_uA: max. current supported by the regulator
6682 * @start_scan: row at the desc table to start scan from
6683 * @buff: power descriptor buffer
6684 *
6685 * Returns calculated max ICC level for specific regulator
6686 */
6687static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
6688{
6689 int i;
6690 int curr_uA;
6691 u16 data;
6692 u16 unit;
6693
6694 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02006695 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006696 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
6697 ATTR_ICC_LVL_UNIT_OFFSET;
6698 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
6699 switch (unit) {
6700 case UFSHCD_NANO_AMP:
6701 curr_uA = curr_uA / 1000;
6702 break;
6703 case UFSHCD_MILI_AMP:
6704 curr_uA = curr_uA * 1000;
6705 break;
6706 case UFSHCD_AMP:
6707 curr_uA = curr_uA * 1000 * 1000;
6708 break;
6709 case UFSHCD_MICRO_AMP:
6710 default:
6711 break;
6712 }
6713 if (sup_curr_uA >= curr_uA)
6714 break;
6715 }
6716 if (i < 0) {
6717 i = 0;
6718 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
6719 }
6720
6721 return (u32)i;
6722}
6723
6724/**
6725 * ufshcd_calc_icc_level - calculate the max ICC level
6726 * In case regulators are not initialized we'll return 0
6727 * @hba: per-adapter instance
6728 * @desc_buf: power descriptor buffer to extract ICC levels from.
6729 * @len: length of desc_buff
6730 *
6731 * Returns calculated ICC level
6732 */
6733static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
6734 u8 *desc_buf, int len)
6735{
6736 u32 icc_level = 0;
6737
6738 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
6739 !hba->vreg_info.vccq2) {
6740 dev_err(hba->dev,
6741 "%s: Regulator capability was not set, actvIccLevel=%d",
6742 __func__, icc_level);
6743 goto out;
6744 }
6745
Stanley Chu0487fff2019-03-28 17:16:25 +08006746 if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006747 icc_level = ufshcd_get_max_icc_level(
6748 hba->vreg_info.vcc->max_uA,
6749 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
6750 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
6751
Stanley Chu0487fff2019-03-28 17:16:25 +08006752 if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006753 icc_level = ufshcd_get_max_icc_level(
6754 hba->vreg_info.vccq->max_uA,
6755 icc_level,
6756 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
6757
Stanley Chu0487fff2019-03-28 17:16:25 +08006758 if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006759 icc_level = ufshcd_get_max_icc_level(
6760 hba->vreg_info.vccq2->max_uA,
6761 icc_level,
6762 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
6763out:
6764 return icc_level;
6765}
6766
Can Guoe89860f2020-03-26 02:25:41 -07006767static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006768{
6769 int ret;
Bean Huo7a0bf852020-06-03 11:19:58 +02006770 int buff_len = hba->desc_size[QUERY_DESC_IDN_POWER];
Kees Cookbbe21d72018-05-02 16:58:09 -07006771 u8 *desc_buf;
Can Guoe89860f2020-03-26 02:25:41 -07006772 u32 icc_level;
Kees Cookbbe21d72018-05-02 16:58:09 -07006773
6774 desc_buf = kmalloc(buff_len, GFP_KERNEL);
6775 if (!desc_buf)
6776 return;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006777
Bean Huoc4607a02020-06-03 11:19:56 +02006778 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0,
6779 desc_buf, buff_len);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006780 if (ret) {
6781 dev_err(hba->dev,
6782 "%s: Failed reading power descriptor.len = %d ret = %d",
6783 __func__, buff_len, ret);
Kees Cookbbe21d72018-05-02 16:58:09 -07006784 goto out;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006785 }
6786
Can Guoe89860f2020-03-26 02:25:41 -07006787 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf,
6788 buff_len);
6789 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006790
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02006791 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Can Guoe89860f2020-03-26 02:25:41 -07006792 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006793
6794 if (ret)
6795 dev_err(hba->dev,
6796 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
Can Guoe89860f2020-03-26 02:25:41 -07006797 __func__, icc_level, ret);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006798
Kees Cookbbe21d72018-05-02 16:58:09 -07006799out:
6800 kfree(desc_buf);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006801}
6802
Can Guofb276f72020-03-25 18:09:59 -07006803static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
6804{
6805 scsi_autopm_get_device(sdev);
6806 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev);
6807 if (sdev->rpm_autosuspend)
6808 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev,
6809 RPM_AUTOSUSPEND_DELAY_MS);
6810 scsi_autopm_put_device(sdev);
6811}
6812
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006813/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006814 * ufshcd_scsi_add_wlus - Adds required W-LUs
6815 * @hba: per-adapter instance
6816 *
6817 * UFS device specification requires the UFS devices to support 4 well known
6818 * logical units:
6819 * "REPORT_LUNS" (address: 01h)
6820 * "UFS Device" (address: 50h)
6821 * "RPMB" (address: 44h)
6822 * "BOOT" (address: 30h)
6823 * UFS device's power management needs to be controlled by "POWER CONDITION"
6824 * field of SSU (START STOP UNIT) command. But this "power condition" field
6825 * will take effect only when its sent to "UFS device" well known logical unit
6826 * hence we require the scsi_device instance to represent this logical unit in
6827 * order for the UFS host driver to send the SSU command for power management.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006828 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006829 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
6830 * Block) LU so user space process can control this LU. User space may also
6831 * want to have access to BOOT LU.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006832 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006833 * This function adds scsi device instances for each of all well known LUs
6834 * (except "REPORT LUNS" LU).
6835 *
6836 * Returns zero on success (all required W-LUs are added successfully),
6837 * non-zero error value on failure (if failed to add any of the required W-LU).
6838 */
6839static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
6840{
6841 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006842 struct scsi_device *sdev_rpmb;
6843 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006844
6845 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
6846 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
6847 if (IS_ERR(hba->sdev_ufs_device)) {
6848 ret = PTR_ERR(hba->sdev_ufs_device);
6849 hba->sdev_ufs_device = NULL;
6850 goto out;
6851 }
Can Guofb276f72020-03-25 18:09:59 -07006852 ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006853 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006854
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006855 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006856 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006857 if (IS_ERR(sdev_rpmb)) {
6858 ret = PTR_ERR(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006859 goto remove_sdev_ufs_device;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006860 }
Can Guofb276f72020-03-25 18:09:59 -07006861 ufshcd_blk_pm_runtime_init(sdev_rpmb);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006862 scsi_device_put(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006863
6864 sdev_boot = __scsi_add_device(hba->host, 0, 0,
6865 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Can Guofb276f72020-03-25 18:09:59 -07006866 if (IS_ERR(sdev_boot)) {
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006867 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
Can Guofb276f72020-03-25 18:09:59 -07006868 } else {
6869 ufshcd_blk_pm_runtime_init(sdev_boot);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006870 scsi_device_put(sdev_boot);
Can Guofb276f72020-03-25 18:09:59 -07006871 }
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006872 goto out;
6873
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006874remove_sdev_ufs_device:
6875 scsi_remove_device(hba->sdev_ufs_device);
6876out:
6877 return ret;
6878}
6879
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006880static void ufshcd_wb_probe(struct ufs_hba *hba, u8 *desc_buf)
6881{
Stanley Chua7f1e692020-06-25 11:04:30 +08006882 struct ufs_dev_info *dev_info = &hba->dev_info;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006883 u8 lun;
6884 u32 d_lu_wb_buf_alloc;
6885
Stanley Chu817d7e12020-05-08 16:01:08 +08006886 if (!ufshcd_is_wb_allowed(hba))
6887 return;
Stanley Chua7f1e692020-06-25 11:04:30 +08006888 /*
6889 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or
6890 * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES
6891 * enabled
6892 */
6893 if (!(dev_info->wspecversion >= 0x310 ||
6894 dev_info->wspecversion == 0x220 ||
6895 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES)))
6896 goto wb_disabled;
Stanley Chu817d7e12020-05-08 16:01:08 +08006897
Bean Huo7a0bf852020-06-03 11:19:58 +02006898 if (hba->desc_size[QUERY_DESC_IDN_DEVICE] <
6899 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP + 4)
Stanley Chu817d7e12020-05-08 16:01:08 +08006900 goto wb_disabled;
6901
Stanley Chua7f1e692020-06-25 11:04:30 +08006902 dev_info->d_ext_ufs_feature_sup =
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006903 get_unaligned_be32(desc_buf +
6904 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
Stanley Chu817d7e12020-05-08 16:01:08 +08006905
Stanley Chua7f1e692020-06-25 11:04:30 +08006906 if (!(dev_info->d_ext_ufs_feature_sup & UFS_DEV_WRITE_BOOSTER_SUP))
Stanley Chu817d7e12020-05-08 16:01:08 +08006907 goto wb_disabled;
6908
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006909 /*
6910 * WB may be supported but not configured while provisioning.
6911 * The spec says, in dedicated wb buffer mode,
6912 * a max of 1 lun would have wb buffer configured.
6913 * Now only shared buffer mode is supported.
6914 */
Stanley Chua7f1e692020-06-25 11:04:30 +08006915 dev_info->b_wb_buffer_type =
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006916 desc_buf[DEVICE_DESC_PARAM_WB_TYPE];
6917
Stanley Chua7f1e692020-06-25 11:04:30 +08006918 dev_info->b_presrv_uspc_en =
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006919 desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN];
6920
Stanley Chua7f1e692020-06-25 11:04:30 +08006921 if (dev_info->b_wb_buffer_type == WB_BUF_MODE_SHARED) {
6922 dev_info->d_wb_alloc_units =
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006923 get_unaligned_be32(desc_buf +
6924 DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS);
Stanley Chua7f1e692020-06-25 11:04:30 +08006925 if (!dev_info->d_wb_alloc_units)
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006926 goto wb_disabled;
6927 } else {
6928 for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) {
6929 d_lu_wb_buf_alloc = 0;
6930 ufshcd_read_unit_desc_param(hba,
6931 lun,
6932 UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS,
6933 (u8 *)&d_lu_wb_buf_alloc,
6934 sizeof(d_lu_wb_buf_alloc));
6935 if (d_lu_wb_buf_alloc) {
Stanley Chua7f1e692020-06-25 11:04:30 +08006936 dev_info->wb_dedicated_lu = lun;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006937 break;
6938 }
6939 }
Stanley Chu817d7e12020-05-08 16:01:08 +08006940
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006941 if (!d_lu_wb_buf_alloc)
6942 goto wb_disabled;
6943 }
Stanley Chu817d7e12020-05-08 16:01:08 +08006944 return;
6945
6946wb_disabled:
6947 hba->caps &= ~UFSHCD_CAP_WB_EN;
6948}
6949
Stanley Chu8db269a2020-05-08 16:01:10 +08006950void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups)
Stanley Chu817d7e12020-05-08 16:01:08 +08006951{
6952 struct ufs_dev_fix *f;
6953 struct ufs_dev_info *dev_info = &hba->dev_info;
6954
Stanley Chu8db269a2020-05-08 16:01:10 +08006955 if (!fixups)
6956 return;
6957
6958 for (f = fixups; f->quirk; f++) {
Stanley Chu817d7e12020-05-08 16:01:08 +08006959 if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
6960 f->wmanufacturerid == UFS_ANY_VENDOR) &&
6961 ((dev_info->model &&
6962 STR_PRFX_EQUAL(f->model, dev_info->model)) ||
6963 !strcmp(f->model, UFS_ANY_MODEL)))
6964 hba->dev_quirks |= f->quirk;
6965 }
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006966}
Stanley Chu8db269a2020-05-08 16:01:10 +08006967EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006968
Stanley Chuc28c00b2020-05-08 16:01:09 +08006969static void ufs_fixup_device_setup(struct ufs_hba *hba)
6970{
6971 /* fix by general quirk table */
Stanley Chu8db269a2020-05-08 16:01:10 +08006972 ufshcd_fixup_dev_quirks(hba, ufs_fixups);
Stanley Chuc28c00b2020-05-08 16:01:09 +08006973
6974 /* allow vendors to fix quirks */
6975 ufshcd_vops_fixup_dev_quirks(hba);
6976}
6977
Bean Huo09750062020-01-20 14:08:14 +01006978static int ufs_get_device_desc(struct ufs_hba *hba)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006979{
6980 int err;
6981 u8 model_index;
Kees Cookbbe21d72018-05-02 16:58:09 -07006982 u8 *desc_buf;
Bean Huo09750062020-01-20 14:08:14 +01006983 struct ufs_dev_info *dev_info = &hba->dev_info;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006984
Bean Huo458a45f2020-06-03 11:19:55 +02006985 desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
Kees Cookbbe21d72018-05-02 16:58:09 -07006986 if (!desc_buf) {
6987 err = -ENOMEM;
6988 goto out;
6989 }
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006990
Bean Huoc4607a02020-06-03 11:19:56 +02006991 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
Bean Huo7a0bf852020-06-03 11:19:58 +02006992 hba->desc_size[QUERY_DESC_IDN_DEVICE]);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006993 if (err) {
6994 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
6995 __func__, err);
6996 goto out;
6997 }
6998
6999 /*
7000 * getting vendor (manufacturerID) and Bank Index in big endian
7001 * format
7002 */
Bean Huo09750062020-01-20 14:08:14 +01007003 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007004 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
7005
Can Guo09f17792020-02-10 19:40:49 -08007006 /* getting Specification Version in big endian format */
7007 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
7008 desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
7009
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007010 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007011
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007012 err = ufshcd_read_string_desc(hba, model_index,
Bean Huo09750062020-01-20 14:08:14 +01007013 &dev_info->model, SD_ASCII_STD);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007014 if (err < 0) {
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007015 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
7016 __func__, err);
7017 goto out;
7018 }
7019
Stanley Chu817d7e12020-05-08 16:01:08 +08007020 ufs_fixup_device_setup(hba);
7021
Stanley Chua7f1e692020-06-25 11:04:30 +08007022 ufshcd_wb_probe(hba, desc_buf);
Stanley Chu817d7e12020-05-08 16:01:08 +08007023
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007024 /*
7025 * ufshcd_read_string_desc returns size of the string
7026 * reset the error value
7027 */
7028 err = 0;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007029
7030out:
Kees Cookbbe21d72018-05-02 16:58:09 -07007031 kfree(desc_buf);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007032 return err;
7033}
7034
Bean Huo09750062020-01-20 14:08:14 +01007035static void ufs_put_device_desc(struct ufs_hba *hba)
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007036{
Bean Huo09750062020-01-20 14:08:14 +01007037 struct ufs_dev_info *dev_info = &hba->dev_info;
7038
7039 kfree(dev_info->model);
7040 dev_info->model = NULL;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007041}
7042
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007043/**
Yaniv Gardi37113102016-03-10 17:37:16 +02007044 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
7045 * @hba: per-adapter instance
7046 *
7047 * PA_TActivate parameter can be tuned manually if UniPro version is less than
7048 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
7049 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
7050 * the hibern8 exit latency.
7051 *
7052 * Returns zero on success, non-zero error value on failure.
7053 */
7054static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
7055{
7056 int ret = 0;
7057 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
7058
7059 ret = ufshcd_dme_peer_get(hba,
7060 UIC_ARG_MIB_SEL(
7061 RX_MIN_ACTIVATETIME_CAPABILITY,
7062 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7063 &peer_rx_min_activatetime);
7064 if (ret)
7065 goto out;
7066
7067 /* make sure proper unit conversion is applied */
7068 tuned_pa_tactivate =
7069 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
7070 / PA_TACTIVATE_TIME_UNIT_US);
7071 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7072 tuned_pa_tactivate);
7073
7074out:
7075 return ret;
7076}
7077
7078/**
7079 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
7080 * @hba: per-adapter instance
7081 *
7082 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
7083 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
7084 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
7085 * This optimal value can help reduce the hibern8 exit latency.
7086 *
7087 * Returns zero on success, non-zero error value on failure.
7088 */
7089static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
7090{
7091 int ret = 0;
7092 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
7093 u32 max_hibern8_time, tuned_pa_hibern8time;
7094
7095 ret = ufshcd_dme_get(hba,
7096 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
7097 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
7098 &local_tx_hibern8_time_cap);
7099 if (ret)
7100 goto out;
7101
7102 ret = ufshcd_dme_peer_get(hba,
7103 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
7104 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7105 &peer_rx_hibern8_time_cap);
7106 if (ret)
7107 goto out;
7108
7109 max_hibern8_time = max(local_tx_hibern8_time_cap,
7110 peer_rx_hibern8_time_cap);
7111 /* make sure proper unit conversion is applied */
7112 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
7113 / PA_HIBERN8_TIME_UNIT_US);
7114 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
7115 tuned_pa_hibern8time);
7116out:
7117 return ret;
7118}
7119
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007120/**
7121 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
7122 * less than device PA_TACTIVATE time.
7123 * @hba: per-adapter instance
7124 *
7125 * Some UFS devices require host PA_TACTIVATE to be lower than device
7126 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
7127 * for such devices.
7128 *
7129 * Returns zero on success, non-zero error value on failure.
7130 */
7131static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
7132{
7133 int ret = 0;
7134 u32 granularity, peer_granularity;
7135 u32 pa_tactivate, peer_pa_tactivate;
7136 u32 pa_tactivate_us, peer_pa_tactivate_us;
7137 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
7138
7139 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7140 &granularity);
7141 if (ret)
7142 goto out;
7143
7144 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7145 &peer_granularity);
7146 if (ret)
7147 goto out;
7148
7149 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
7150 (granularity > PA_GRANULARITY_MAX_VAL)) {
7151 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
7152 __func__, granularity);
7153 return -EINVAL;
7154 }
7155
7156 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
7157 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
7158 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
7159 __func__, peer_granularity);
7160 return -EINVAL;
7161 }
7162
7163 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
7164 if (ret)
7165 goto out;
7166
7167 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
7168 &peer_pa_tactivate);
7169 if (ret)
7170 goto out;
7171
7172 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
7173 peer_pa_tactivate_us = peer_pa_tactivate *
7174 gran_to_us_table[peer_granularity - 1];
7175
7176 if (pa_tactivate_us > peer_pa_tactivate_us) {
7177 u32 new_peer_pa_tactivate;
7178
7179 new_peer_pa_tactivate = pa_tactivate_us /
7180 gran_to_us_table[peer_granularity - 1];
7181 new_peer_pa_tactivate++;
7182 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7183 new_peer_pa_tactivate);
7184 }
7185
7186out:
7187 return ret;
7188}
7189
Bean Huo09750062020-01-20 14:08:14 +01007190static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
Yaniv Gardi37113102016-03-10 17:37:16 +02007191{
7192 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
7193 ufshcd_tune_pa_tactivate(hba);
7194 ufshcd_tune_pa_hibern8time(hba);
7195 }
7196
Can Guoe91ed9e2020-02-23 20:09:21 -08007197 ufshcd_vops_apply_dev_quirks(hba);
7198
Yaniv Gardi37113102016-03-10 17:37:16 +02007199 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
7200 /* set 1ms timeout for PA_TACTIVATE */
7201 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007202
7203 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
7204 ufshcd_quirk_tune_host_pa_tactivate(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02007205}
7206
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007207static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
7208{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007209 hba->ufs_stats.hibern8_exit_cnt = 0;
7210 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08007211 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007212}
7213
Bean Huo731f0622020-01-20 14:08:19 +01007214static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
7215{
7216 int err;
7217 size_t buff_len;
7218 u8 *desc_buf;
7219
Bean Huo7a0bf852020-06-03 11:19:58 +02007220 buff_len = hba->desc_size[QUERY_DESC_IDN_GEOMETRY];
Bean Huo731f0622020-01-20 14:08:19 +01007221 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7222 if (!desc_buf) {
7223 err = -ENOMEM;
7224 goto out;
7225 }
7226
Bean Huoc4607a02020-06-03 11:19:56 +02007227 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
7228 desc_buf, buff_len);
Bean Huo731f0622020-01-20 14:08:19 +01007229 if (err) {
7230 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
7231 __func__, err);
7232 goto out;
7233 }
7234
7235 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
7236 hba->dev_info.max_lu_supported = 32;
7237 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
7238 hba->dev_info.max_lu_supported = 8;
7239
7240out:
7241 kfree(desc_buf);
7242 return err;
7243}
7244
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307245static struct ufs_ref_clk ufs_ref_clk_freqs[] = {
7246 {19200000, REF_CLK_FREQ_19_2_MHZ},
7247 {26000000, REF_CLK_FREQ_26_MHZ},
7248 {38400000, REF_CLK_FREQ_38_4_MHZ},
7249 {52000000, REF_CLK_FREQ_52_MHZ},
7250 {0, REF_CLK_FREQ_INVAL},
7251};
7252
7253static enum ufs_ref_clk_freq
7254ufs_get_bref_clk_from_hz(unsigned long freq)
7255{
7256 int i;
7257
7258 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
7259 if (ufs_ref_clk_freqs[i].freq_hz == freq)
7260 return ufs_ref_clk_freqs[i].val;
7261
7262 return REF_CLK_FREQ_INVAL;
7263}
7264
7265void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
7266{
7267 unsigned long freq;
7268
7269 freq = clk_get_rate(refclk);
7270
7271 hba->dev_ref_clk_freq =
7272 ufs_get_bref_clk_from_hz(freq);
7273
7274 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
7275 dev_err(hba->dev,
7276 "invalid ref_clk setting = %ld\n", freq);
7277}
7278
7279static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
7280{
7281 int err;
7282 u32 ref_clk;
7283 u32 freq = hba->dev_ref_clk_freq;
7284
7285 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
7286 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
7287
7288 if (err) {
7289 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
7290 err);
7291 goto out;
7292 }
7293
7294 if (ref_clk == freq)
7295 goto out; /* nothing to update */
7296
7297 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7298 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
7299
7300 if (err) {
7301 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
7302 ufs_ref_clk_freqs[freq].freq_hz);
7303 goto out;
7304 }
7305
7306 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
7307 ufs_ref_clk_freqs[freq].freq_hz);
7308
7309out:
7310 return err;
7311}
7312
Bean Huo1b9e2142020-01-20 14:08:15 +01007313static int ufshcd_device_params_init(struct ufs_hba *hba)
7314{
7315 bool flag;
Bean Huo7a0bf852020-06-03 11:19:58 +02007316 int ret, i;
Bean Huo1b9e2142020-01-20 14:08:15 +01007317
Bean Huo7a0bf852020-06-03 11:19:58 +02007318 /* Init device descriptor sizes */
7319 for (i = 0; i < QUERY_DESC_IDN_MAX; i++)
7320 hba->desc_size[i] = QUERY_DESC_MAX_SIZE;
Bean Huo1b9e2142020-01-20 14:08:15 +01007321
Bean Huo731f0622020-01-20 14:08:19 +01007322 /* Init UFS geometry descriptor related parameters */
7323 ret = ufshcd_device_geo_params_init(hba);
7324 if (ret)
7325 goto out;
7326
Bean Huo1b9e2142020-01-20 14:08:15 +01007327 /* Check and apply UFS device quirks */
7328 ret = ufs_get_device_desc(hba);
7329 if (ret) {
7330 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
7331 __func__, ret);
7332 goto out;
7333 }
7334
Can Guo09f17792020-02-10 19:40:49 -08007335 ufshcd_get_ref_clk_gating_wait(hba);
7336
Bean Huo1b9e2142020-01-20 14:08:15 +01007337 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08007338 QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag))
Bean Huo1b9e2142020-01-20 14:08:15 +01007339 hba->dev_info.f_power_on_wp_en = flag;
7340
Bean Huo2b35b2a2020-01-20 14:08:16 +01007341 /* Probe maximum power mode co-supported by both UFS host and device */
7342 if (ufshcd_get_max_pwr_mode(hba))
7343 dev_err(hba->dev,
7344 "%s: Failed getting max supported power mode\n",
7345 __func__);
Bean Huo1b9e2142020-01-20 14:08:15 +01007346out:
7347 return ret;
7348}
7349
7350/**
7351 * ufshcd_add_lus - probe and add UFS logical units
7352 * @hba: per-adapter instance
7353 */
7354static int ufshcd_add_lus(struct ufs_hba *hba)
7355{
7356 int ret;
7357
Bean Huo1b9e2142020-01-20 14:08:15 +01007358 /* Add required well known logical units to scsi mid layer */
7359 ret = ufshcd_scsi_add_wlus(hba);
7360 if (ret)
7361 goto out;
7362
7363 /* Initialize devfreq after UFS device is detected */
7364 if (ufshcd_is_clkscaling_supported(hba)) {
7365 memcpy(&hba->clk_scaling.saved_pwr_info.info,
7366 &hba->pwr_info,
7367 sizeof(struct ufs_pa_layer_attr));
7368 hba->clk_scaling.saved_pwr_info.is_valid = true;
7369 if (!hba->devfreq) {
7370 ret = ufshcd_devfreq_init(hba);
7371 if (ret)
7372 goto out;
7373 }
7374
7375 hba->clk_scaling.is_allowed = true;
7376 }
7377
7378 ufs_bsg_probe(hba);
7379 scsi_scan_host(hba->host);
7380 pm_runtime_put_sync(hba->dev);
7381
Bean Huo1b9e2142020-01-20 14:08:15 +01007382out:
7383 return ret;
7384}
7385
Yaniv Gardi37113102016-03-10 17:37:16 +02007386/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007387 * ufshcd_probe_hba - probe hba to detect device and initialize
7388 * @hba: per-adapter instance
Bean Huo1b9e2142020-01-20 14:08:15 +01007389 * @async: asynchronous execution or not
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007390 *
7391 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307392 */
Bean Huo1b9e2142020-01-20 14:08:15 +01007393static int ufshcd_probe_hba(struct ufs_hba *hba, bool async)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307394{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307395 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007396 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307397
7398 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307399 if (ret)
7400 goto out;
7401
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007402 /* Debug counters initialization */
7403 ufshcd_clear_dbg_ufs_stats(hba);
7404
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007405 /* UniPro link is active now */
7406 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05307407
Bean Huo1b9e2142020-01-20 14:08:15 +01007408 /* Verify device initialization by sending NOP OUT UPIU */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307409 ret = ufshcd_verify_dev_init(hba);
7410 if (ret)
7411 goto out;
7412
Bean Huo1b9e2142020-01-20 14:08:15 +01007413 /* Initiate UFS initialization, and waiting until completion */
Dolev Raviv68078d52013-07-30 00:35:58 +05307414 ret = ufshcd_complete_dev_init(hba);
7415 if (ret)
7416 goto out;
7417
Bean Huo1b9e2142020-01-20 14:08:15 +01007418 /*
7419 * Initialize UFS device parameters used by driver, these
7420 * parameters are associated with UFS descriptors.
7421 */
7422 if (async) {
7423 ret = ufshcd_device_params_init(hba);
7424 if (ret)
7425 goto out;
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02007426 }
7427
Bean Huo09750062020-01-20 14:08:14 +01007428 ufshcd_tune_unipro_params(hba);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007429
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007430 /* UFS device is also active now */
7431 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307432 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007433 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307434
Bean Huo2b35b2a2020-01-20 14:08:16 +01007435 /* Gear up to HS gear if supported */
7436 if (hba->max_pwr_info.is_valid) {
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307437 /*
7438 * Set the right value to bRefClkFreq before attempting to
7439 * switch to HS gears.
7440 */
7441 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
7442 ufshcd_set_dev_ref_clk(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007443 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007444 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007445 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7446 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007447 goto out;
7448 }
Can Guo6a9df812020-02-11 21:38:28 -08007449 ufshcd_print_pwr_info(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007450 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007451
Can Guoe89860f2020-03-26 02:25:41 -07007452 /*
7453 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
7454 * and for removable UFS card as well, hence always set the parameter.
7455 * Note: Error handler may issue the device reset hence resetting
7456 * bActiveICCLevel as well so it is always safe to set this here.
7457 */
7458 ufshcd_set_active_icc_lvl(hba);
7459
Yaniv Gardi53c12d02016-02-01 15:02:45 +02007460 /* set the state as operational after switching to desired gear */
7461 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00007462
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007463 ufshcd_wb_config(hba);
Can Guo71d848b2019-11-14 22:09:26 -08007464 /* Enable Auto-Hibernate if configured */
7465 ufshcd_auto_hibern8_enable(hba);
7466
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307467out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007468
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007469 trace_ufshcd_init(dev_name(hba->dev), ret,
7470 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007471 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007472 return ret;
7473}
7474
7475/**
7476 * ufshcd_async_scan - asynchronous execution for probing hba
7477 * @data: data pointer to pass to this function
7478 * @cookie: cookie data
7479 */
7480static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7481{
7482 struct ufs_hba *hba = (struct ufs_hba *)data;
Bean Huo1b9e2142020-01-20 14:08:15 +01007483 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007484
Bean Huo1b9e2142020-01-20 14:08:15 +01007485 /* Initialize hba, detect and initialize UFS device */
7486 ret = ufshcd_probe_hba(hba, true);
7487 if (ret)
7488 goto out;
7489
7490 /* Probe and add UFS logical units */
7491 ret = ufshcd_add_lus(hba);
7492out:
7493 /*
7494 * If we failed to initialize the device or the device is not
7495 * present, turn off the power/clocks etc.
7496 */
7497 if (ret) {
7498 pm_runtime_put_sync(hba->dev);
7499 ufshcd_exit_clk_scaling(hba);
7500 ufshcd_hba_exit(hba);
7501 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307502}
7503
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007504static const struct attribute_group *ufshcd_driver_groups[] = {
7505 &ufs_sysfs_unit_descriptor_group,
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02007506 &ufs_sysfs_lun_attributes_group,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007507 NULL,
7508};
7509
Stanley Chu90b84912020-05-09 17:37:13 +08007510static struct ufs_hba_variant_params ufs_hba_vps = {
7511 .hba_enable_delay_us = 1000,
Stanley Chud14734ae2020-05-09 17:37:15 +08007512 .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40),
Stanley Chu90b84912020-05-09 17:37:13 +08007513 .devfreq_profile.polling_ms = 100,
7514 .devfreq_profile.target = ufshcd_devfreq_target,
7515 .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status,
7516 .ondemand_data.upthreshold = 70,
7517 .ondemand_data.downdifferential = 5,
7518};
7519
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307520static struct scsi_host_template ufshcd_driver_template = {
7521 .module = THIS_MODULE,
7522 .name = UFSHCD,
7523 .proc_name = UFSHCD,
7524 .queuecommand = ufshcd_queuecommand,
7525 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09007526 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307527 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03007528 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307529 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307530 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7531 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307532 .this_id = -1,
7533 .sg_tablesize = SG_ALL,
7534 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
7535 .can_queue = UFSHCD_CAN_QUEUE,
Christoph Hellwig552a9902019-06-17 14:19:55 +02007536 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007537 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007538 .track_queue_depth = 1,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007539 .sdev_groups = ufshcd_driver_groups,
Christoph Hellwig4af14d12018-12-13 16:17:09 +01007540 .dma_boundary = PAGE_SIZE - 1,
Stanley Chu49615ba2019-09-16 23:56:50 +08007541 .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307542};
7543
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007544static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
7545 int ua)
7546{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007547 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007548
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007549 if (!vreg)
7550 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007551
Stanley Chu0487fff2019-03-28 17:16:25 +08007552 /*
7553 * "set_load" operation shall be required on those regulators
7554 * which specifically configured current limitation. Otherwise
7555 * zero max_uA may cause unexpected behavior when regulator is
7556 * enabled or set as high power mode.
7557 */
7558 if (!vreg->max_uA)
7559 return 0;
7560
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007561 ret = regulator_set_load(vreg->reg, ua);
7562 if (ret < 0) {
7563 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
7564 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007565 }
7566
7567 return ret;
7568}
7569
7570static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
7571 struct ufs_vreg *vreg)
7572{
Marc Gonzalez73067982019-02-27 11:41:45 +01007573 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007574}
7575
7576static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
7577 struct ufs_vreg *vreg)
7578{
Adrian Hunter7c7cfdc2019-08-14 15:59:50 +03007579 if (!vreg)
7580 return 0;
7581
Marc Gonzalez73067982019-02-27 11:41:45 +01007582 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007583}
7584
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007585static int ufshcd_config_vreg(struct device *dev,
7586 struct ufs_vreg *vreg, bool on)
7587{
7588 int ret = 0;
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06007589 struct regulator *reg;
7590 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007591 int min_uV, uA_load;
7592
7593 BUG_ON(!vreg);
7594
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06007595 reg = vreg->reg;
7596 name = vreg->name;
7597
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007598 if (regulator_count_voltages(reg) > 0) {
Asutosh Das90d88f42020-02-10 19:40:45 -08007599 uA_load = on ? vreg->max_uA : 0;
7600 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
7601 if (ret)
7602 goto out;
7603
Stanley Chu3b141e82019-03-28 17:16:24 +08007604 if (vreg->min_uV && vreg->max_uV) {
7605 min_uV = on ? vreg->min_uV : 0;
7606 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
7607 if (ret) {
7608 dev_err(dev,
7609 "%s: %s set voltage failed, err=%d\n",
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007610 __func__, name, ret);
Stanley Chu3b141e82019-03-28 17:16:24 +08007611 goto out;
7612 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007613 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007614 }
7615out:
7616 return ret;
7617}
7618
7619static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
7620{
7621 int ret = 0;
7622
Marc Gonzalez73067982019-02-27 11:41:45 +01007623 if (!vreg || vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007624 goto out;
7625
7626 ret = ufshcd_config_vreg(dev, vreg, true);
7627 if (!ret)
7628 ret = regulator_enable(vreg->reg);
7629
7630 if (!ret)
7631 vreg->enabled = true;
7632 else
7633 dev_err(dev, "%s: %s enable failed, err=%d\n",
7634 __func__, vreg->name, ret);
7635out:
7636 return ret;
7637}
7638
7639static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
7640{
7641 int ret = 0;
7642
Marc Gonzalez73067982019-02-27 11:41:45 +01007643 if (!vreg || !vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007644 goto out;
7645
7646 ret = regulator_disable(vreg->reg);
7647
7648 if (!ret) {
7649 /* ignore errors on applying disable config */
7650 ufshcd_config_vreg(dev, vreg, false);
7651 vreg->enabled = false;
7652 } else {
7653 dev_err(dev, "%s: %s disable failed, err=%d\n",
7654 __func__, vreg->name, ret);
7655 }
7656out:
7657 return ret;
7658}
7659
7660static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
7661{
7662 int ret = 0;
7663 struct device *dev = hba->dev;
7664 struct ufs_vreg_info *info = &hba->vreg_info;
7665
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007666 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
7667 if (ret)
7668 goto out;
7669
7670 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
7671 if (ret)
7672 goto out;
7673
7674 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
7675 if (ret)
7676 goto out;
7677
7678out:
7679 if (ret) {
7680 ufshcd_toggle_vreg(dev, info->vccq2, false);
7681 ufshcd_toggle_vreg(dev, info->vccq, false);
7682 ufshcd_toggle_vreg(dev, info->vcc, false);
7683 }
7684 return ret;
7685}
7686
Raviv Shvili6a771a62014-09-25 15:32:24 +03007687static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
7688{
7689 struct ufs_vreg_info *info = &hba->vreg_info;
7690
Zeng Guangyue60b7b822019-03-30 17:03:13 +08007691 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007692}
7693
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007694static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
7695{
7696 int ret = 0;
7697
7698 if (!vreg)
7699 goto out;
7700
7701 vreg->reg = devm_regulator_get(dev, vreg->name);
7702 if (IS_ERR(vreg->reg)) {
7703 ret = PTR_ERR(vreg->reg);
7704 dev_err(dev, "%s: %s get failed, err=%d\n",
7705 __func__, vreg->name, ret);
7706 }
7707out:
7708 return ret;
7709}
7710
7711static int ufshcd_init_vreg(struct ufs_hba *hba)
7712{
7713 int ret = 0;
7714 struct device *dev = hba->dev;
7715 struct ufs_vreg_info *info = &hba->vreg_info;
7716
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007717 ret = ufshcd_get_vreg(dev, info->vcc);
7718 if (ret)
7719 goto out;
7720
7721 ret = ufshcd_get_vreg(dev, info->vccq);
7722 if (ret)
7723 goto out;
7724
7725 ret = ufshcd_get_vreg(dev, info->vccq2);
7726out:
7727 return ret;
7728}
7729
Raviv Shvili6a771a62014-09-25 15:32:24 +03007730static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
7731{
7732 struct ufs_vreg_info *info = &hba->vreg_info;
7733
7734 if (info)
7735 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
7736
7737 return 0;
7738}
7739
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007740static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
7741 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007742{
7743 int ret = 0;
7744 struct ufs_clk_info *clki;
7745 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007746 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007747 ktime_t start = ktime_get();
7748 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007749
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007750 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007751 goto out;
7752
Can Guo38f32422020-02-10 19:40:47 -08007753 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
7754 if (ret)
7755 return ret;
Subhash Jadavani1e879e82016-10-06 21:48:22 -07007756
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007757 list_for_each_entry(clki, head, list) {
7758 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007759 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
7760 continue;
7761
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007762 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007763 if (on && !clki->enabled) {
7764 ret = clk_prepare_enable(clki->clk);
7765 if (ret) {
7766 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
7767 __func__, clki->name, ret);
7768 goto out;
7769 }
7770 } else if (!on && clki->enabled) {
7771 clk_disable_unprepare(clki->clk);
7772 }
7773 clki->enabled = on;
7774 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
7775 clki->name, on ? "en" : "dis");
7776 }
7777 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007778
Can Guo38f32422020-02-10 19:40:47 -08007779 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
7780 if (ret)
7781 return ret;
Subhash Jadavani1e879e82016-10-06 21:48:22 -07007782
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007783out:
7784 if (ret) {
7785 list_for_each_entry(clki, head, list) {
7786 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
7787 clk_disable_unprepare(clki->clk);
7788 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007789 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007790 spin_lock_irqsave(hba->host->host_lock, flags);
7791 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007792 trace_ufshcd_clk_gating(dev_name(hba->dev),
7793 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007794 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007795 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007796
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007797 if (clk_state_changed)
7798 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
7799 (on ? "on" : "off"),
7800 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007801 return ret;
7802}
7803
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007804static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
7805{
7806 return __ufshcd_setup_clocks(hba, on, false);
7807}
7808
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007809static int ufshcd_init_clocks(struct ufs_hba *hba)
7810{
7811 int ret = 0;
7812 struct ufs_clk_info *clki;
7813 struct device *dev = hba->dev;
7814 struct list_head *head = &hba->clk_list_head;
7815
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007816 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007817 goto out;
7818
7819 list_for_each_entry(clki, head, list) {
7820 if (!clki->name)
7821 continue;
7822
7823 clki->clk = devm_clk_get(dev, clki->name);
7824 if (IS_ERR(clki->clk)) {
7825 ret = PTR_ERR(clki->clk);
7826 dev_err(dev, "%s: %s clk get failed, %d\n",
7827 __func__, clki->name, ret);
7828 goto out;
7829 }
7830
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307831 /*
7832 * Parse device ref clk freq as per device tree "ref_clk".
7833 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
7834 * in ufshcd_alloc_host().
7835 */
7836 if (!strcmp(clki->name, "ref_clk"))
7837 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
7838
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007839 if (clki->max_freq) {
7840 ret = clk_set_rate(clki->clk, clki->max_freq);
7841 if (ret) {
7842 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
7843 __func__, clki->name,
7844 clki->max_freq, ret);
7845 goto out;
7846 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03007847 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007848 }
7849 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
7850 clki->name, clk_get_rate(clki->clk));
7851 }
7852out:
7853 return ret;
7854}
7855
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007856static int ufshcd_variant_hba_init(struct ufs_hba *hba)
7857{
7858 int err = 0;
7859
7860 if (!hba->vops)
7861 goto out;
7862
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007863 err = ufshcd_vops_init(hba);
7864 if (err)
7865 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007866
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007867 err = ufshcd_vops_setup_regulators(hba, true);
7868 if (err)
7869 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007870
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007871 goto out;
7872
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007873out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007874 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007875out:
7876 if (err)
7877 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007878 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007879 return err;
7880}
7881
7882static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
7883{
7884 if (!hba->vops)
7885 return;
7886
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007887 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007888
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007889 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007890}
7891
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007892static int ufshcd_hba_init(struct ufs_hba *hba)
7893{
7894 int err;
7895
Raviv Shvili6a771a62014-09-25 15:32:24 +03007896 /*
7897 * Handle host controller power separately from the UFS device power
7898 * rails as it will help controlling the UFS host controller power
7899 * collapse easily which is different than UFS device power collapse.
7900 * Also, enable the host controller power before we go ahead with rest
7901 * of the initialization here.
7902 */
7903 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007904 if (err)
7905 goto out;
7906
Raviv Shvili6a771a62014-09-25 15:32:24 +03007907 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007908 if (err)
7909 goto out;
7910
Raviv Shvili6a771a62014-09-25 15:32:24 +03007911 err = ufshcd_init_clocks(hba);
7912 if (err)
7913 goto out_disable_hba_vreg;
7914
7915 err = ufshcd_setup_clocks(hba, true);
7916 if (err)
7917 goto out_disable_hba_vreg;
7918
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007919 err = ufshcd_init_vreg(hba);
7920 if (err)
7921 goto out_disable_clks;
7922
7923 err = ufshcd_setup_vreg(hba, true);
7924 if (err)
7925 goto out_disable_clks;
7926
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007927 err = ufshcd_variant_hba_init(hba);
7928 if (err)
7929 goto out_disable_vreg;
7930
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007931 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007932 goto out;
7933
7934out_disable_vreg:
7935 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007936out_disable_clks:
7937 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007938out_disable_hba_vreg:
7939 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007940out:
7941 return err;
7942}
7943
7944static void ufshcd_hba_exit(struct ufs_hba *hba)
7945{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007946 if (hba->is_powered) {
7947 ufshcd_variant_hba_exit(hba);
7948 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07007949 ufshcd_suspend_clkscaling(hba);
Vivek Gautameebcc192018-08-07 23:17:39 +05307950 if (ufshcd_is_clkscaling_supported(hba))
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08007951 if (hba->devfreq)
7952 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007953 ufshcd_setup_clocks(hba, false);
7954 ufshcd_setup_hba_vreg(hba, false);
7955 hba->is_powered = false;
Bean Huo09750062020-01-20 14:08:14 +01007956 ufs_put_device_desc(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007957 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007958}
7959
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007960static int
7961ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307962{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007963 unsigned char cmd[6] = {REQUEST_SENSE,
7964 0,
7965 0,
7966 0,
Avri Altman09a5a242018-11-22 20:04:56 +02007967 UFS_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007968 0};
7969 char *buffer;
7970 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307971
Avri Altman09a5a242018-11-22 20:04:56 +02007972 buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007973 if (!buffer) {
7974 ret = -ENOMEM;
7975 goto out;
7976 }
7977
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007978 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
Avri Altman09a5a242018-11-22 20:04:56 +02007979 UFS_SENSE_SIZE, NULL, NULL,
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007980 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007981 if (ret)
7982 pr_err("%s: failed with err %d\n", __func__, ret);
7983
7984 kfree(buffer);
7985out:
7986 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307987}
7988
7989/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007990 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
7991 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307992 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007993 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307994 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007995 * Returns 0 if requested power mode is set successfully
7996 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307997 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007998static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
7999 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308000{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008001 unsigned char cmd[6] = { START_STOP };
8002 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008003 struct scsi_device *sdp;
8004 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008005 int ret;
8006
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008007 spin_lock_irqsave(hba->host->host_lock, flags);
8008 sdp = hba->sdev_ufs_device;
8009 if (sdp) {
8010 ret = scsi_device_get(sdp);
8011 if (!ret && !scsi_device_online(sdp)) {
8012 ret = -ENODEV;
8013 scsi_device_put(sdp);
8014 }
8015 } else {
8016 ret = -ENODEV;
8017 }
8018 spin_unlock_irqrestore(hba->host->host_lock, flags);
8019
8020 if (ret)
8021 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008022
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308023 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008024 * If scsi commands fail, the scsi mid-layer schedules scsi error-
8025 * handling, which would wait for host to be resumed. Since we know
8026 * we are functional while we are here, skip host resume in error
8027 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308028 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008029 hba->host->eh_noresume = 1;
8030 if (hba->wlun_dev_clr_ua) {
8031 ret = ufshcd_send_request_sense(hba, sdp);
8032 if (ret)
8033 goto out;
8034 /* Unit attention condition is cleared now */
8035 hba->wlun_dev_clr_ua = false;
8036 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308037
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008038 cmd[4] = pwr_mode << 4;
8039
8040 /*
8041 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02008042 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008043 * already suspended childs.
8044 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01008045 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
8046 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008047 if (ret) {
8048 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02008049 "START_STOP failed for power mode: %d, result %x\n",
8050 pwr_mode, ret);
Johannes Thumshirnc65be1a2018-06-25 13:20:58 +02008051 if (driver_byte(ret) == DRIVER_SENSE)
Hannes Reinecke21045512015-01-08 07:43:46 +01008052 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008053 }
8054
8055 if (!ret)
8056 hba->curr_dev_pwr_mode = pwr_mode;
8057out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008058 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008059 hba->host->eh_noresume = 0;
8060 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308061}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308062
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008063static int ufshcd_link_state_transition(struct ufs_hba *hba,
8064 enum uic_link_state req_link_state,
8065 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308066{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008067 int ret = 0;
8068
8069 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308070 return 0;
8071
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008072 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
8073 ret = ufshcd_uic_hibern8_enter(hba);
8074 if (!ret)
8075 ufshcd_set_link_hibern8(hba);
8076 else
8077 goto out;
8078 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308079 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008080 * If autobkops is enabled, link can't be turned off because
8081 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308082 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008083 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
Dan Carpenterdc30c9e2019-12-13 13:49:35 +03008084 (!check_for_bkops || !hba->auto_bkops_enabled)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008085 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008086 * Let's make sure that link is in low power mode, we are doing
8087 * this currently by putting the link in Hibern8. Otherway to
8088 * put the link in low power mode is to send the DME end point
8089 * to device and then send the DME reset command to local
8090 * unipro. But putting the link in hibern8 is much faster.
8091 */
8092 ret = ufshcd_uic_hibern8_enter(hba);
8093 if (ret)
8094 goto out;
8095 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008096 * Change controller state to "reset state" which
8097 * should also put the link in off/reset state
8098 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07008099 ufshcd_hba_stop(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008100 /*
8101 * TODO: Check if we need any delay to make sure that
8102 * controller is reset
8103 */
8104 ufshcd_set_link_off(hba);
8105 }
8106
8107out:
8108 return ret;
8109}
8110
8111static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
8112{
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008113 bool vcc_off = false;
8114
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008115 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02008116 * It seems some UFS devices may keep drawing more than sleep current
8117 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
8118 * To avoid this situation, add 2ms delay before putting these UFS
8119 * rails in LPM mode.
8120 */
8121 if (!ufshcd_is_link_active(hba) &&
8122 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
8123 usleep_range(2000, 2100);
8124
8125 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008126 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
8127 * power.
8128 *
8129 * If UFS device and link is in OFF state, all power supplies (VCC,
8130 * VCCQ, VCCQ2) can be turned off if power on write protect is not
8131 * required. If UFS link is inactive (Hibern8 or OFF state) and device
8132 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
8133 *
8134 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
8135 * in low power state which would save some power.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008136 *
8137 * If Write Booster is enabled and the device needs to flush the WB
8138 * buffer OR if bkops status is urgent for WB, keep Vcc on.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008139 */
8140 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8141 !hba->dev_info.is_lu_power_on_wp) {
8142 ufshcd_setup_vreg(hba, false);
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008143 vcc_off = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008144 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Stanley Chu51dd9052020-05-22 16:32:12 +08008145 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008146 vcc_off = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008147 if (!ufshcd_is_link_active(hba)) {
8148 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8149 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
8150 }
8151 }
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008152
8153 /*
8154 * Some UFS devices require delay after VCC power rail is turned-off.
8155 */
8156 if (vcc_off && hba->vreg_info.vcc &&
8157 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)
8158 usleep_range(5000, 5100);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008159}
8160
8161static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
8162{
8163 int ret = 0;
8164
8165 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8166 !hba->dev_info.is_lu_power_on_wp) {
8167 ret = ufshcd_setup_vreg(hba, true);
8168 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008169 if (!ret && !ufshcd_is_link_active(hba)) {
8170 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
8171 if (ret)
8172 goto vcc_disable;
8173 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
8174 if (ret)
8175 goto vccq_lpm;
8176 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07008177 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008178 }
8179 goto out;
8180
8181vccq_lpm:
8182 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8183vcc_disable:
8184 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8185out:
8186 return ret;
8187}
8188
8189static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
8190{
8191 if (ufshcd_is_link_off(hba))
8192 ufshcd_setup_hba_vreg(hba, false);
8193}
8194
8195static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
8196{
8197 if (ufshcd_is_link_off(hba))
8198 ufshcd_setup_hba_vreg(hba, true);
8199}
8200
8201/**
8202 * ufshcd_suspend - helper function for suspend operations
8203 * @hba: per adapter instance
8204 * @pm_op: desired low power operation type
8205 *
8206 * This function will try to put the UFS device and link into low power
8207 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
8208 * (System PM level).
8209 *
8210 * If this function is called during shutdown, it will make sure that
8211 * both UFS device and UFS link is powered off.
8212 *
8213 * NOTE: UFS device & link must be active before we enter in this function.
8214 *
8215 * Returns 0 for success and non-zero for failure
8216 */
8217static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8218{
8219 int ret = 0;
8220 enum ufs_pm_level pm_lvl;
8221 enum ufs_dev_pwr_mode req_dev_pwr_mode;
8222 enum uic_link_state req_link_state;
8223
8224 hba->pm_op_in_progress = 1;
8225 if (!ufshcd_is_shutdown_pm(pm_op)) {
8226 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
8227 hba->rpm_lvl : hba->spm_lvl;
8228 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
8229 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
8230 } else {
8231 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
8232 req_link_state = UIC_LINK_OFF_STATE;
8233 }
8234
8235 /*
8236 * If we can't transition into any of the low power modes
8237 * just gate the clocks.
8238 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008239 ufshcd_hold(hba, false);
8240 hba->clk_gating.is_suspended = true;
8241
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008242 if (hba->clk_scaling.is_allowed) {
8243 cancel_work_sync(&hba->clk_scaling.suspend_work);
8244 cancel_work_sync(&hba->clk_scaling.resume_work);
8245 ufshcd_suspend_clkscaling(hba);
8246 }
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008247
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008248 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8249 req_link_state == UIC_LINK_ACTIVE_STATE) {
8250 goto disable_clks;
8251 }
8252
8253 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8254 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008255 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008256
8257 /* UFS device & link must be active before we enter in this function */
8258 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8259 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008260 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008261 }
8262
8263 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03008264 if (ufshcd_can_autobkops_during_suspend(hba)) {
8265 /*
8266 * The device is idle with no requests in the queue,
8267 * allow background operations if bkops status shows
8268 * that performance might be impacted.
8269 */
8270 ret = ufshcd_urgent_bkops(hba);
8271 if (ret)
8272 goto enable_gating;
8273 } else {
8274 /* make sure that auto bkops is disabled */
8275 ufshcd_disable_auto_bkops(hba);
8276 }
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008277 /*
Stanley Chu51dd9052020-05-22 16:32:12 +08008278 * If device needs to do BKOP or WB buffer flush during
8279 * Hibern8, keep device power mode as "active power mode"
8280 * and VCC supply.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008281 */
Stanley Chu51dd9052020-05-22 16:32:12 +08008282 hba->dev_info.b_rpm_dev_flush_capable =
8283 hba->auto_bkops_enabled ||
8284 (((req_link_state == UIC_LINK_HIBERN8_STATE) ||
8285 ((req_link_state == UIC_LINK_ACTIVE_STATE) &&
8286 ufshcd_is_auto_hibern8_enabled(hba))) &&
8287 ufshcd_wb_need_flush(hba));
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008288 }
8289
Stanley Chu51dd9052020-05-22 16:32:12 +08008290 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) {
8291 if ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
8292 !ufshcd_is_runtime_pm(pm_op)) {
8293 /* ensure that bkops is disabled */
8294 ufshcd_disable_auto_bkops(hba);
8295 }
8296
8297 if (!hba->dev_info.b_rpm_dev_flush_capable) {
8298 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8299 if (ret)
8300 goto enable_gating;
8301 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008302 }
8303
Sayali Lokhande2824ec92020-02-10 19:40:44 -08008304 flush_work(&hba->eeh_work);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008305 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
8306 if (ret)
8307 goto set_dev_active;
8308
8309 ufshcd_vreg_set_lpm(hba);
8310
8311disable_clks:
8312 /*
8313 * Call vendor specific suspend callback. As these callbacks may access
8314 * vendor specific host controller register space call them before the
8315 * host clocks are ON.
8316 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008317 ret = ufshcd_vops_suspend(hba, pm_op);
8318 if (ret)
8319 goto set_link_active;
Stanley Chudcb6cec2019-12-07 20:22:00 +08008320 /*
8321 * Disable the host irq as host controller as there won't be any
8322 * host controller transaction expected till resume.
8323 */
8324 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008325
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008326 if (!ufshcd_is_link_active(hba))
8327 ufshcd_setup_clocks(hba, false);
8328 else
8329 /* If link is active, device ref_clk can't be switched off */
8330 __ufshcd_setup_clocks(hba, false, true);
8331
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008332 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008333 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Stanley Chudcb6cec2019-12-07 20:22:00 +08008334
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008335 /* Put the host controller in low power mode if possible */
8336 ufshcd_hba_vreg_set_lpm(hba);
8337 goto out;
8338
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008339set_link_active:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008340 if (hba->clk_scaling.is_allowed)
8341 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008342 ufshcd_vreg_set_hpm(hba);
8343 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
8344 ufshcd_set_link_active(hba);
8345 else if (ufshcd_is_link_off(hba))
8346 ufshcd_host_reset_and_restore(hba);
8347set_dev_active:
8348 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8349 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008350enable_gating:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008351 if (hba->clk_scaling.is_allowed)
8352 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008353 hba->clk_gating.is_suspended = false;
Stanley Chu51dd9052020-05-22 16:32:12 +08008354 hba->dev_info.b_rpm_dev_flush_capable = false;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008355 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008356out:
Stanley Chu51dd9052020-05-22 16:32:12 +08008357 if (hba->dev_info.b_rpm_dev_flush_capable) {
8358 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work,
8359 msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS));
8360 }
8361
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008362 hba->pm_op_in_progress = 0;
Stanley Chu51dd9052020-05-22 16:32:12 +08008363
Stanley Chu8808b4e2019-07-10 21:38:21 +08008364 if (ret)
8365 ufshcd_update_reg_hist(&hba->ufs_stats.suspend_err, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008366 return ret;
8367}
8368
8369/**
8370 * ufshcd_resume - helper function for resume operations
8371 * @hba: per adapter instance
8372 * @pm_op: runtime PM or system PM
8373 *
8374 * This function basically brings the UFS device, UniPro link and controller
8375 * to active state.
8376 *
8377 * Returns 0 for success and non-zero for failure
8378 */
8379static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8380{
8381 int ret;
8382 enum uic_link_state old_link_state;
8383
8384 hba->pm_op_in_progress = 1;
8385 old_link_state = hba->uic_link_state;
8386
8387 ufshcd_hba_vreg_set_hpm(hba);
8388 /* Make sure clocks are enabled before accessing controller */
8389 ret = ufshcd_setup_clocks(hba, true);
8390 if (ret)
8391 goto out;
8392
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008393 /* enable the host irq as host controller would be active soon */
Can Guo5231d382019-12-05 02:14:46 +00008394 ufshcd_enable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008395
8396 ret = ufshcd_vreg_set_hpm(hba);
8397 if (ret)
8398 goto disable_irq_and_vops_clks;
8399
8400 /*
8401 * Call vendor specific resume callback. As these callbacks may access
8402 * vendor specific host controller register space call them when the
8403 * host clocks are ON.
8404 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008405 ret = ufshcd_vops_resume(hba, pm_op);
8406 if (ret)
8407 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008408
8409 if (ufshcd_is_link_hibern8(hba)) {
8410 ret = ufshcd_uic_hibern8_exit(hba);
8411 if (!ret)
8412 ufshcd_set_link_active(hba);
8413 else
8414 goto vendor_suspend;
8415 } else if (ufshcd_is_link_off(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008416 /*
Asutosh Das089f5b62020-04-13 23:14:48 -07008417 * A full initialization of the host and the device is
8418 * required since the link was put to off during suspend.
8419 */
8420 ret = ufshcd_reset_and_restore(hba);
8421 /*
8422 * ufshcd_reset_and_restore() should have already
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008423 * set the link state as active
8424 */
8425 if (ret || !ufshcd_is_link_active(hba))
8426 goto vendor_suspend;
8427 }
8428
8429 if (!ufshcd_is_ufs_dev_active(hba)) {
8430 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8431 if (ret)
8432 goto set_old_link_state;
8433 }
8434
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08008435 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8436 ufshcd_enable_auto_bkops(hba);
8437 else
8438 /*
8439 * If BKOPs operations are urgently needed at this moment then
8440 * keep auto-bkops enabled or else disable it.
8441 */
8442 ufshcd_urgent_bkops(hba);
8443
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008444 hba->clk_gating.is_suspended = false;
8445
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008446 if (hba->clk_scaling.is_allowed)
8447 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008448
Adrian Hunterad448372018-03-20 15:07:38 +02008449 /* Enable Auto-Hibernate if configured */
8450 ufshcd_auto_hibern8_enable(hba);
8451
Stanley Chu51dd9052020-05-22 16:32:12 +08008452 if (hba->dev_info.b_rpm_dev_flush_capable) {
8453 hba->dev_info.b_rpm_dev_flush_capable = false;
8454 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
8455 }
8456
Can Guo71d848b2019-11-14 22:09:26 -08008457 /* Schedule clock gating in case of no access to UFS device yet */
8458 ufshcd_release(hba);
8459
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008460 goto out;
8461
8462set_old_link_state:
8463 ufshcd_link_state_transition(hba, old_link_state, 0);
8464vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008465 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008466disable_vreg:
8467 ufshcd_vreg_set_lpm(hba);
8468disable_irq_and_vops_clks:
8469 ufshcd_disable_irq(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008470 if (hba->clk_scaling.is_allowed)
8471 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008472 ufshcd_setup_clocks(hba, false);
8473out:
8474 hba->pm_op_in_progress = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08008475 if (ret)
8476 ufshcd_update_reg_hist(&hba->ufs_stats.resume_err, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008477 return ret;
8478}
8479
8480/**
8481 * ufshcd_system_suspend - system suspend routine
8482 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008483 *
8484 * Check the description of ufshcd_suspend() function for more details.
8485 *
8486 * Returns 0 for success and non-zero for failure
8487 */
8488int ufshcd_system_suspend(struct ufs_hba *hba)
8489{
8490 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008491 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008492
8493 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03008494 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008495
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008496 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8497 hba->curr_dev_pwr_mode) &&
8498 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8499 hba->uic_link_state))
8500 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008501
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008502 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008503 /*
8504 * UFS device and/or UFS link low power states during runtime
8505 * suspend seems to be different than what is expected during
8506 * system suspend. Hence runtime resume the devic & link and
8507 * let the system suspend low power states to take effect.
8508 * TODO: If resume takes longer time, we might have optimize
8509 * it in future by not resuming everything if possible.
8510 */
8511 ret = ufshcd_runtime_resume(hba);
8512 if (ret)
8513 goto out;
8514 }
8515
8516 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
8517out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008518 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
8519 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008520 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03008521 if (!ret)
8522 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008523 return ret;
8524}
8525EXPORT_SYMBOL(ufshcd_system_suspend);
8526
8527/**
8528 * ufshcd_system_resume - system resume routine
8529 * @hba: per adapter instance
8530 *
8531 * Returns 0 for success and non-zero for failure
8532 */
8533
8534int ufshcd_system_resume(struct ufs_hba *hba)
8535{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008536 int ret = 0;
8537 ktime_t start = ktime_get();
8538
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008539 if (!hba)
8540 return -EINVAL;
8541
8542 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008543 /*
8544 * Let the runtime resume take care of resuming
8545 * if runtime suspended.
8546 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008547 goto out;
8548 else
8549 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
8550out:
8551 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
8552 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008553 hba->curr_dev_pwr_mode, hba->uic_link_state);
Stanley Chuce9e7bc2019-01-07 22:19:34 +08008554 if (!ret)
8555 hba->is_sys_suspended = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008556 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008557}
8558EXPORT_SYMBOL(ufshcd_system_resume);
8559
8560/**
8561 * ufshcd_runtime_suspend - runtime suspend routine
8562 * @hba: per adapter instance
8563 *
8564 * Check the description of ufshcd_suspend() function for more details.
8565 *
8566 * Returns 0 for success and non-zero for failure
8567 */
8568int ufshcd_runtime_suspend(struct ufs_hba *hba)
8569{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008570 int ret = 0;
8571 ktime_t start = ktime_get();
8572
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008573 if (!hba)
8574 return -EINVAL;
8575
8576 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008577 goto out;
8578 else
8579 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
8580out:
8581 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
8582 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008583 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008584 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308585}
8586EXPORT_SYMBOL(ufshcd_runtime_suspend);
8587
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008588/**
8589 * ufshcd_runtime_resume - runtime resume routine
8590 * @hba: per adapter instance
8591 *
8592 * This function basically brings the UFS device, UniPro link and controller
8593 * to active state. Following operations are done in this function:
8594 *
8595 * 1. Turn on all the controller related clocks
8596 * 2. Bring the UniPro link out of Hibernate state
8597 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
8598 * to active state.
8599 * 4. If auto-bkops is enabled on the device, disable it.
8600 *
8601 * So following would be the possible power state after this function return
8602 * successfully:
8603 * S1: UFS device in Active state with VCC rail ON
8604 * UniPro link in Active state
8605 * All the UFS/UniPro controller clocks are ON
8606 *
8607 * Returns 0 for success and non-zero for failure
8608 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308609int ufshcd_runtime_resume(struct ufs_hba *hba)
8610{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008611 int ret = 0;
8612 ktime_t start = ktime_get();
8613
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008614 if (!hba)
8615 return -EINVAL;
8616
8617 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008618 goto out;
8619 else
8620 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
8621out:
8622 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
8623 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008624 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008625 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308626}
8627EXPORT_SYMBOL(ufshcd_runtime_resume);
8628
8629int ufshcd_runtime_idle(struct ufs_hba *hba)
8630{
8631 return 0;
8632}
8633EXPORT_SYMBOL(ufshcd_runtime_idle);
8634
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308635/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008636 * ufshcd_shutdown - shutdown routine
8637 * @hba: per adapter instance
8638 *
8639 * This function would power off both UFS device and UFS link.
8640 *
8641 * Returns 0 always to allow force shutdown even in case of errors.
8642 */
8643int ufshcd_shutdown(struct ufs_hba *hba)
8644{
8645 int ret = 0;
8646
Stanley Chuf51913e2019-09-18 12:20:38 +08008647 if (!hba->is_powered)
8648 goto out;
8649
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008650 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
8651 goto out;
8652
8653 if (pm_runtime_suspended(hba->dev)) {
8654 ret = ufshcd_runtime_resume(hba);
8655 if (ret)
8656 goto out;
8657 }
8658
8659 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
8660out:
8661 if (ret)
8662 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
8663 /* allow force shutdown even in case of errors */
8664 return 0;
8665}
8666EXPORT_SYMBOL(ufshcd_shutdown);
8667
8668/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308669 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308670 * data structure memory
Bart Van Assche8aa29f12018-03-01 15:07:20 -08008671 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308672 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308673void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308674{
Avri Altmandf032bf2018-10-07 17:30:35 +03008675 ufs_bsg_remove(hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02008676 ufs_sysfs_remove_nodes(hba->dev);
Bart Van Assche69a6c262019-12-09 10:13:09 -08008677 blk_cleanup_queue(hba->tmf_queue);
8678 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08008679 blk_cleanup_queue(hba->cmd_queue);
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05308680 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308681 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308682 ufshcd_disable_intr(hba, hba->intr_mask);
Bart Van Assche5cac1092020-05-07 15:27:50 -07008683 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308684
Vivek Gautameebcc192018-08-07 23:17:39 +05308685 ufshcd_exit_clk_scaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008686 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008687 if (ufshcd_is_clkscaling_supported(hba))
8688 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008689 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308690}
8691EXPORT_SYMBOL_GPL(ufshcd_remove);
8692
8693/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02008694 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
8695 * @hba: pointer to Host Bus Adapter (HBA)
8696 */
8697void ufshcd_dealloc_host(struct ufs_hba *hba)
8698{
Satya Tangiraladf043c742020-07-06 20:04:14 +00008699 ufshcd_crypto_destroy_keyslot_manager(hba);
Yaniv Gardi47555a52015-10-28 13:15:49 +02008700 scsi_host_put(hba->host);
8701}
8702EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
8703
8704/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008705 * ufshcd_set_dma_mask - Set dma mask based on the controller
8706 * addressing capability
8707 * @hba: per adapter instance
8708 *
8709 * Returns 0 for success, non-zero for failure
8710 */
8711static int ufshcd_set_dma_mask(struct ufs_hba *hba)
8712{
8713 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
8714 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
8715 return 0;
8716 }
8717 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
8718}
8719
8720/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008721 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308722 * @dev: pointer to device handle
8723 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308724 * Returns 0 on success, non-zero value on failure
8725 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008726int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308727{
8728 struct Scsi_Host *host;
8729 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008730 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308731
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308732 if (!dev) {
8733 dev_err(dev,
8734 "Invalid memory reference for dev is NULL\n");
8735 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308736 goto out_error;
8737 }
8738
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308739 host = scsi_host_alloc(&ufshcd_driver_template,
8740 sizeof(struct ufs_hba));
8741 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308742 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308743 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308744 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308745 }
8746 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308747 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308748 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008749 *hba_handle = hba;
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05308750 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008751
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03008752 INIT_LIST_HEAD(&hba->clk_list_head);
8753
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008754out_error:
8755 return err;
8756}
8757EXPORT_SYMBOL(ufshcd_alloc_host);
8758
Bart Van Assche69a6c262019-12-09 10:13:09 -08008759/* This function exists because blk_mq_alloc_tag_set() requires this. */
8760static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
8761 const struct blk_mq_queue_data *qd)
8762{
8763 WARN_ON_ONCE(true);
8764 return BLK_STS_NOTSUPP;
8765}
8766
8767static const struct blk_mq_ops ufshcd_tmf_ops = {
8768 .queue_rq = ufshcd_queue_tmf,
8769};
8770
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008771/**
8772 * ufshcd_init - Driver initialization routine
8773 * @hba: per-adapter instance
8774 * @mmio_base: base register address
8775 * @irq: Interrupt line of device
8776 * Returns 0 on success, non-zero value on failure
8777 */
8778int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
8779{
8780 int err;
8781 struct Scsi_Host *host = hba->host;
8782 struct device *dev = hba->dev;
8783
8784 if (!mmio_base) {
8785 dev_err(hba->dev,
8786 "Invalid memory reference for mmio_base is NULL\n");
8787 err = -ENODEV;
8788 goto out_error;
8789 }
8790
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308791 hba->mmio_base = mmio_base;
8792 hba->irq = irq;
Stanley Chu90b84912020-05-09 17:37:13 +08008793 hba->vps = &ufs_hba_vps;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308794
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008795 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008796 if (err)
8797 goto out_error;
8798
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308799 /* Read capabilities registers */
Satya Tangiraladf043c742020-07-06 20:04:14 +00008800 err = ufshcd_hba_capabilities(hba);
8801 if (err)
8802 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308803
8804 /* Get UFS version supported by the controller */
8805 hba->ufs_version = ufshcd_get_ufs_version(hba);
8806
Yaniv Gardic01848c2016-12-05 19:25:02 -08008807 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
8808 (hba->ufs_version != UFSHCI_VERSION_11) &&
8809 (hba->ufs_version != UFSHCI_VERSION_20) &&
8810 (hba->ufs_version != UFSHCI_VERSION_21))
8811 dev_err(hba->dev, "invalid UFS version 0x%x\n",
8812 hba->ufs_version);
8813
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308814 /* Get Interrupt bit mask per version */
8815 hba->intr_mask = ufshcd_get_intr_mask(hba);
8816
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008817 err = ufshcd_set_dma_mask(hba);
8818 if (err) {
8819 dev_err(hba->dev, "set dma mask failed\n");
8820 goto out_disable;
8821 }
8822
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308823 /* Allocate memory for host memory space */
8824 err = ufshcd_memory_alloc(hba);
8825 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308826 dev_err(hba->dev, "Memory allocation failed\n");
8827 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308828 }
8829
8830 /* Configure LRB */
8831 ufshcd_host_memory_configure(hba);
8832
8833 host->can_queue = hba->nutrs;
8834 host->cmd_per_lun = hba->nutrs;
8835 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03008836 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308837 host->max_channel = UFSHCD_MAX_CHANNEL;
8838 host->unique_id = host->host_no;
Avri Altmana851b2b2018-10-07 17:30:34 +03008839 host->max_cmd_len = UFS_CDB_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308840
Dolev Raviv7eb584d2014-09-25 15:32:31 +03008841 hba->max_pwr_info.is_valid = false;
8842
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308843 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05308844 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308845 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308846
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308847 /* Initialize UIC command mutex */
8848 mutex_init(&hba->uic_cmd_mutex);
8849
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05308850 /* Initialize mutex for device management commands */
8851 mutex_init(&hba->dev_cmd.lock);
8852
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08008853 init_rwsem(&hba->clk_scaling_lock);
8854
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008855 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02008856
Vivek Gautameebcc192018-08-07 23:17:39 +05308857 ufshcd_init_clk_scaling(hba);
8858
Yaniv Gardi199ef132016-03-10 17:37:06 +02008859 /*
8860 * In order to avoid any spurious interrupt immediately after
8861 * registering UFS controller interrupt handler, clear any pending UFS
8862 * interrupt status and disable all the UFS interrupts.
8863 */
8864 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
8865 REG_INTERRUPT_STATUS);
8866 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
8867 /*
8868 * Make sure that UFS interrupts are disabled and any pending interrupt
8869 * status is cleared before registering UFS interrupt handler.
8870 */
8871 mb();
8872
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308873 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09008874 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308875 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308876 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008877 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008878 } else {
8879 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308880 }
8881
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308882 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308883 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308884 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008885 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308886 }
8887
Bart Van Assche7252a362019-12-09 10:13:08 -08008888 hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set);
8889 if (IS_ERR(hba->cmd_queue)) {
8890 err = PTR_ERR(hba->cmd_queue);
8891 goto out_remove_scsi_host;
8892 }
8893
Bart Van Assche69a6c262019-12-09 10:13:09 -08008894 hba->tmf_tag_set = (struct blk_mq_tag_set) {
8895 .nr_hw_queues = 1,
8896 .queue_depth = hba->nutmrs,
8897 .ops = &ufshcd_tmf_ops,
8898 .flags = BLK_MQ_F_NO_SCHED,
8899 };
8900 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
8901 if (err < 0)
8902 goto free_cmd_queue;
8903 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set);
8904 if (IS_ERR(hba->tmf_queue)) {
8905 err = PTR_ERR(hba->tmf_queue);
8906 goto free_tmf_tag_set;
8907 }
8908
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07008909 /* Reset the attached device */
8910 ufshcd_vops_device_reset(hba);
8911
Satya Tangiraladf043c742020-07-06 20:04:14 +00008912 ufshcd_init_crypto(hba);
8913
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308914 /* Host controller enable */
8915 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308916 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308917 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08008918 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08008919 ufshcd_print_host_state(hba);
Bart Van Assche69a6c262019-12-09 10:13:09 -08008920 goto free_tmf_queue;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308921 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308922
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08008923 /*
8924 * Set the default power management level for runtime and system PM.
8925 * Default power saving mode is to keep UFS link in Hibern8 state
8926 * and UFS device in sleep state.
8927 */
8928 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
8929 UFS_SLEEP_PWR_MODE,
8930 UIC_LINK_HIBERN8_STATE);
8931 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
8932 UFS_SLEEP_PWR_MODE,
8933 UIC_LINK_HIBERN8_STATE);
8934
Stanley Chu51dd9052020-05-22 16:32:12 +08008935 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work,
8936 ufshcd_rpm_dev_flush_recheck_work);
8937
Adrian Hunterad448372018-03-20 15:07:38 +02008938 /* Set the default auto-hiberate idle timer value to 150 ms */
Stanley Chuf571b372019-05-21 14:44:53 +08008939 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
Adrian Hunterad448372018-03-20 15:07:38 +02008940 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
8941 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
8942 }
8943
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05308944 /* Hold auto suspend until async scan completes */
8945 pm_runtime_get_sync(dev);
Subhash Jadavani38135532018-05-03 16:37:18 +05308946 atomic_set(&hba->scsi_block_reqs_cnt, 0);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008947 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08008948 * We are assuming that device wasn't put in sleep/power-down
8949 * state exclusively during the boot stage before kernel.
8950 * This assumption helps avoid doing link startup twice during
8951 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008952 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08008953 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008954
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308955 async_schedule(ufshcd_async_scan, hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02008956 ufs_sysfs_add_nodes(hba->dev);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308957
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308958 return 0;
8959
Bart Van Assche69a6c262019-12-09 10:13:09 -08008960free_tmf_queue:
8961 blk_cleanup_queue(hba->tmf_queue);
8962free_tmf_tag_set:
8963 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08008964free_cmd_queue:
8965 blk_cleanup_queue(hba->cmd_queue);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308966out_remove_scsi_host:
8967 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008968exit_gating:
Vivek Gautameebcc192018-08-07 23:17:39 +05308969 ufshcd_exit_clk_scaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008970 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308971out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008972 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008973 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308974out_error:
8975 return err;
8976}
8977EXPORT_SYMBOL_GPL(ufshcd_init);
8978
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308979MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
8980MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05308981MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308982MODULE_LICENSE("GPL");
8983MODULE_VERSION(UFSHCD_DRIVER_VERSION);