blob: 71c650f2054ce38ec806bb1252293ee04d98b1dd [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 Ravivff8e20c2016-12-22 18:42:18 -0800414
Stanley Chu48d5b972019-07-10 21:38:18 +0800415 ufshcd_print_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err");
416 ufshcd_print_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err");
417 ufshcd_print_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err");
418 ufshcd_print_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err");
419 ufshcd_print_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err");
Stanley Chud3c615b2019-07-10 21:38:19 +0800420 ufshcd_print_err_hist(hba, &hba->ufs_stats.auto_hibern8_err,
421 "auto_hibern8_err");
Stanley Chu8808b4e2019-07-10 21:38:21 +0800422 ufshcd_print_err_hist(hba, &hba->ufs_stats.fatal_err, "fatal_err");
423 ufshcd_print_err_hist(hba, &hba->ufs_stats.link_startup_err,
424 "link_startup_fail");
425 ufshcd_print_err_hist(hba, &hba->ufs_stats.resume_err, "resume_fail");
426 ufshcd_print_err_hist(hba, &hba->ufs_stats.suspend_err,
427 "suspend_fail");
428 ufshcd_print_err_hist(hba, &hba->ufs_stats.dev_reset, "dev_reset");
429 ufshcd_print_err_hist(hba, &hba->ufs_stats.host_reset, "host_reset");
430 ufshcd_print_err_hist(hba, &hba->ufs_stats.task_abort, "task_abort");
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800431
Stanley Chu7c486d912019-12-24 21:01:06 +0800432 ufshcd_vops_dbg_register_dump(hba);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800433}
434
435static
436void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
437{
438 struct ufshcd_lrb *lrbp;
Gilad Broner7fabb772017-02-03 16:56:50 -0800439 int prdt_length;
Dolev Raviv66cc8202016-12-22 18:39:42 -0800440 int tag;
441
442 for_each_set_bit(tag, &bitmap, hba->nutrs) {
443 lrbp = &hba->lrb[tag];
444
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800445 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
446 tag, ktime_to_us(lrbp->issue_time_stamp));
Zang Leigang09017182017-09-27 10:06:06 +0800447 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
448 tag, ktime_to_us(lrbp->compl_time_stamp));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800449 dev_err(hba->dev,
450 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
451 tag, (u64)lrbp->utrd_dma_addr);
452
Dolev Raviv66cc8202016-12-22 18:39:42 -0800453 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
454 sizeof(struct utp_transfer_req_desc));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800455 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
456 (u64)lrbp->ucd_req_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800457 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
458 sizeof(struct utp_upiu_req));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800459 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
460 (u64)lrbp->ucd_rsp_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800461 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
462 sizeof(struct utp_upiu_rsp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800463
Gilad Broner7fabb772017-02-03 16:56:50 -0800464 prdt_length = le16_to_cpu(
465 lrbp->utr_descriptor_ptr->prd_table_length);
466 dev_err(hba->dev,
467 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
468 tag, prdt_length,
469 (u64)lrbp->ucd_prdt_dma_addr);
470
471 if (pr_prdt)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800472 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
Gilad Broner7fabb772017-02-03 16:56:50 -0800473 sizeof(struct ufshcd_sg_entry) * prdt_length);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800474 }
475}
476
477static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
478{
Dolev Raviv66cc8202016-12-22 18:39:42 -0800479 int tag;
480
481 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
Christoph Hellwig391e3882018-10-07 17:30:32 +0300482 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag];
483
Dolev Raviv66cc8202016-12-22 18:39:42 -0800484 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
Christoph Hellwig391e3882018-10-07 17:30:32 +0300485 ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800486 }
487}
488
Gilad Broner6ba65582017-02-03 16:57:28 -0800489static void ufshcd_print_host_state(struct ufs_hba *hba)
490{
Can Guo3f8af602020-08-09 05:15:50 -0700491 struct scsi_device *sdev_ufs = hba->sdev_ufs_device;
492
Gilad Broner6ba65582017-02-03 16:57:28 -0800493 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
Bart Van Assche7252a362019-12-09 10:13:08 -0800494 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n",
495 hba->outstanding_reqs, hba->outstanding_tasks);
Gilad Broner6ba65582017-02-03 16:57:28 -0800496 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
497 hba->saved_err, hba->saved_uic_err);
498 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
499 hba->curr_dev_pwr_mode, hba->uic_link_state);
500 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
501 hba->pm_op_in_progress, hba->is_sys_suspended);
502 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
503 hba->auto_bkops_enabled, hba->host->host_self_blocked);
504 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
Can Guo3f8af602020-08-09 05:15:50 -0700505 dev_err(hba->dev,
506 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt=%d\n",
507 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
508 hba->ufs_stats.hibern8_exit_cnt);
509 dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n",
510 ktime_to_us(hba->ufs_stats.last_intr_ts),
511 hba->ufs_stats.last_intr_status);
Gilad Broner6ba65582017-02-03 16:57:28 -0800512 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
513 hba->eh_flags, hba->req_abort_count);
Can Guo3f8af602020-08-09 05:15:50 -0700514 dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n",
515 hba->ufs_version, hba->capabilities, hba->caps);
Gilad Broner6ba65582017-02-03 16:57:28 -0800516 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
517 hba->dev_quirks);
Can Guo3f8af602020-08-09 05:15:50 -0700518 if (sdev_ufs)
519 dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n",
520 sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev);
521
522 ufshcd_print_clk_freqs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -0800523}
524
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800525/**
526 * ufshcd_print_pwr_info - print power params as saved in hba
527 * power info
528 * @hba: per-adapter instance
529 */
530static void ufshcd_print_pwr_info(struct ufs_hba *hba)
531{
532 static const char * const names[] = {
533 "INVALID MODE",
534 "FAST MODE",
535 "SLOW_MODE",
536 "INVALID MODE",
537 "FASTAUTO_MODE",
538 "SLOWAUTO_MODE",
539 "INVALID MODE",
540 };
541
542 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
543 __func__,
544 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
545 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
546 names[hba->pwr_info.pwr_rx],
547 names[hba->pwr_info.pwr_tx],
548 hba->pwr_info.hs_rate);
549}
550
Stanley Chu5c955c12020-03-18 18:40:12 +0800551void ufshcd_delay_us(unsigned long us, unsigned long tolerance)
552{
553 if (!us)
554 return;
555
556 if (us < 10)
557 udelay(us);
558 else
559 usleep_range(us, us + tolerance);
560}
561EXPORT_SYMBOL_GPL(ufshcd_delay_us);
562
Bart Van Assche5cac1092020-05-07 15:27:50 -0700563/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530564 * ufshcd_wait_for_register - wait for register value to change
Bart Van Assche5cac1092020-05-07 15:27:50 -0700565 * @hba: per-adapter interface
566 * @reg: mmio register offset
567 * @mask: mask to apply to the read register value
568 * @val: value to wait for
569 * @interval_us: polling interval in microseconds
570 * @timeout_ms: timeout in milliseconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530571 *
Bart Van Assche5cac1092020-05-07 15:27:50 -0700572 * Return:
573 * -ETIMEDOUT on error, zero on success.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530574 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200575int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
576 u32 val, unsigned long interval_us,
Bart Van Assche5cac1092020-05-07 15:27:50 -0700577 unsigned long timeout_ms)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530578{
579 int err = 0;
580 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
581
582 /* ignore bits that we don't intend to wait on */
583 val = val & mask;
584
585 while ((ufshcd_readl(hba, reg) & mask) != val) {
Bart Van Assche5cac1092020-05-07 15:27:50 -0700586 usleep_range(interval_us, interval_us + 50);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530587 if (time_after(jiffies, timeout)) {
588 if ((ufshcd_readl(hba, reg) & mask) != val)
589 err = -ETIMEDOUT;
590 break;
591 }
592 }
593
594 return err;
595}
596
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530597/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530598 * ufshcd_get_intr_mask - Get the interrupt bit mask
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800599 * @hba: Pointer to adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530600 *
601 * Returns interrupt bit mask per version
602 */
603static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
604{
Yaniv Gardic01848c2016-12-05 19:25:02 -0800605 u32 intr_mask = 0;
606
607 switch (hba->ufs_version) {
608 case UFSHCI_VERSION_10:
609 intr_mask = INTERRUPT_MASK_ALL_VER_10;
610 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800611 case UFSHCI_VERSION_11:
612 case UFSHCI_VERSION_20:
613 intr_mask = INTERRUPT_MASK_ALL_VER_11;
614 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800615 case UFSHCI_VERSION_21:
616 default:
617 intr_mask = INTERRUPT_MASK_ALL_VER_21;
Tomohiro Kusumi031d1e02017-03-23 12:49:04 +0200618 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800619 }
620
621 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530622}
623
624/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530625 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800626 * @hba: Pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530627 *
628 * Returns UFSHCI version supported by the controller
629 */
630static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
631{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200632 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
633 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300634
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530635 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530636}
637
638/**
639 * ufshcd_is_device_present - Check if any device connected to
640 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300641 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530642 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300643 * Returns true if device present, false if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530644 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300645static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530646{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300647 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300648 DEVICE_PRESENT) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530649}
650
651/**
652 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800653 * @lrbp: pointer to local command reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530654 *
655 * This function is used to get the OCS field from UTRD
656 * Returns the OCS field in the UTRD
657 */
658static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
659{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530660 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530661}
662
663/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530664 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
665 * @hba: per adapter instance
666 * @pos: position of the bit to be cleared
667 */
668static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
669{
Alim Akhtar87183842020-05-28 06:46:49 +0530670 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
671 ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
672 else
673 ufshcd_writel(hba, ~(1 << pos),
674 REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Alim Akhtar1399c5b2018-05-06 15:44:15 +0530675}
676
677/**
678 * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register
679 * @hba: per adapter instance
680 * @pos: position of the bit to be cleared
681 */
682static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
683{
Alim Akhtar87183842020-05-28 06:46:49 +0530684 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
685 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
686 else
687 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530688}
689
690/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200691 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
692 * @hba: per adapter instance
693 * @tag: position of the bit to be cleared
694 */
695static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
696{
697 __clear_bit(tag, &hba->outstanding_reqs);
698}
699
700/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530701 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
702 * @reg: Register value of host controller status
703 *
704 * Returns integer, 0 on Success and positive value if failed
705 */
706static inline int ufshcd_get_lists_status(u32 reg)
707{
Tomohiro Kusumi6cf16112017-04-26 20:28:58 +0300708 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530709}
710
711/**
712 * ufshcd_get_uic_cmd_result - Get the UIC command result
713 * @hba: Pointer to adapter instance
714 *
715 * This function gets the result of UIC command completion
716 * Returns 0 on success, non zero value on error
717 */
718static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
719{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530720 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530721 MASK_UIC_COMMAND_RESULT;
722}
723
724/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530725 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
726 * @hba: Pointer to adapter instance
727 *
728 * This function gets UIC command argument3
729 * Returns 0 on success, non zero value on error
730 */
731static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
732{
733 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
734}
735
736/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530737 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530738 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530739 */
740static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530741ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530742{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530743 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530744}
745
746/**
747 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
748 * @ucd_rsp_ptr: pointer to response UPIU
749 *
750 * This function gets the response status and scsi_status from response UPIU
751 * Returns the response result code.
752 */
753static inline int
754ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
755{
756 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
757}
758
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530759/*
760 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
761 * from response UPIU
762 * @ucd_rsp_ptr: pointer to response UPIU
763 *
764 * Return the data segment length.
765 */
766static inline unsigned int
767ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
768{
769 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
770 MASK_RSP_UPIU_DATA_SEG_LEN;
771}
772
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530773/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530774 * ufshcd_is_exception_event - Check if the device raised an exception event
775 * @ucd_rsp_ptr: pointer to response UPIU
776 *
777 * The function checks if the device raised an exception event indicated in
778 * the Device Information field of response UPIU.
779 *
780 * Returns true if exception is raised, false otherwise.
781 */
782static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
783{
784 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
785 MASK_RSP_EXCEPTION_EVENT ? true : false;
786}
787
788/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530789 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530790 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530791 */
792static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530793ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530794{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530795 ufshcd_writel(hba, INT_AGGR_ENABLE |
796 INT_AGGR_COUNTER_AND_TIMER_RESET,
797 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
798}
799
800/**
801 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
802 * @hba: per adapter instance
803 * @cnt: Interrupt aggregation counter threshold
804 * @tmout: Interrupt aggregation timeout value
805 */
806static inline void
807ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
808{
809 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
810 INT_AGGR_COUNTER_THLD_VAL(cnt) |
811 INT_AGGR_TIMEOUT_VAL(tmout),
812 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530813}
814
815/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300816 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
817 * @hba: per adapter instance
818 */
819static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
820{
821 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
822}
823
824/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530825 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
826 * When run-stop registers are set to 1, it indicates the
827 * host controller that it can process the requests
828 * @hba: per adapter instance
829 */
830static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
831{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530832 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
833 REG_UTP_TASK_REQ_LIST_RUN_STOP);
834 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
835 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530836}
837
838/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530839 * ufshcd_hba_start - Start controller initialization sequence
840 * @hba: per adapter instance
841 */
842static inline void ufshcd_hba_start(struct ufs_hba *hba)
843{
Satya Tangiraladf043c742020-07-06 20:04:14 +0000844 u32 val = CONTROLLER_ENABLE;
845
846 if (ufshcd_crypto_enable(hba))
847 val |= CRYPTO_GENERAL_ENABLE;
848
849 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530850}
851
852/**
853 * ufshcd_is_hba_active - Get controller state
854 * @hba: per adapter instance
855 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300856 * Returns false if controller is active, true otherwise
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530857 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300858static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530859{
Tomohiro Kusumi4a8eec22017-03-28 16:49:25 +0300860 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
861 ? false : true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530862}
863
Yaniv Gardi37113102016-03-10 17:37:16 +0200864u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
865{
866 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
867 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
868 (hba->ufs_version == UFSHCI_VERSION_11))
869 return UFS_UNIPRO_VER_1_41;
870 else
871 return UFS_UNIPRO_VER_1_6;
872}
873EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
874
875static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
876{
877 /*
878 * If both host and device support UniPro ver1.6 or later, PA layer
879 * parameters tuning happens during link startup itself.
880 *
881 * We can manually tune PA layer parameters if either host or device
882 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
883 * logic simple, we will only do manual tuning if local unipro version
884 * doesn't support ver1.6 or later.
885 */
886 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
887 return true;
888 else
889 return false;
890}
891
Subhash Jadavani394b9492020-03-26 02:25:40 -0700892/**
893 * ufshcd_set_clk_freq - set UFS controller clock frequencies
894 * @hba: per adapter instance
895 * @scale_up: If True, set max possible frequency othewise set low frequency
896 *
897 * Returns 0 if successful
898 * Returns < 0 for any other errors
899 */
900static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800901{
902 int ret = 0;
903 struct ufs_clk_info *clki;
904 struct list_head *head = &hba->clk_list_head;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800905
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300906 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800907 goto out;
908
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800909 list_for_each_entry(clki, head, list) {
910 if (!IS_ERR_OR_NULL(clki->clk)) {
911 if (scale_up && clki->max_freq) {
912 if (clki->curr_freq == clki->max_freq)
913 continue;
914
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800915 ret = clk_set_rate(clki->clk, clki->max_freq);
916 if (ret) {
917 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
918 __func__, clki->name,
919 clki->max_freq, ret);
920 break;
921 }
922 trace_ufshcd_clk_scaling(dev_name(hba->dev),
923 "scaled up", clki->name,
924 clki->curr_freq,
925 clki->max_freq);
926
927 clki->curr_freq = clki->max_freq;
928
929 } else if (!scale_up && clki->min_freq) {
930 if (clki->curr_freq == clki->min_freq)
931 continue;
932
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800933 ret = clk_set_rate(clki->clk, clki->min_freq);
934 if (ret) {
935 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
936 __func__, clki->name,
937 clki->min_freq, ret);
938 break;
939 }
940 trace_ufshcd_clk_scaling(dev_name(hba->dev),
941 "scaled down", clki->name,
942 clki->curr_freq,
943 clki->min_freq);
944 clki->curr_freq = clki->min_freq;
945 }
946 }
947 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
948 clki->name, clk_get_rate(clki->clk));
949 }
950
Subhash Jadavani394b9492020-03-26 02:25:40 -0700951out:
952 return ret;
953}
954
955/**
956 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
957 * @hba: per adapter instance
958 * @scale_up: True if scaling up and false if scaling down
959 *
960 * Returns 0 if successful
961 * Returns < 0 for any other errors
962 */
963static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
964{
965 int ret = 0;
966 ktime_t start = ktime_get();
967
968 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
969 if (ret)
970 goto out;
971
972 ret = ufshcd_set_clk_freq(hba, scale_up);
973 if (ret)
974 goto out;
975
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800976 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
Subhash Jadavani394b9492020-03-26 02:25:40 -0700977 if (ret)
978 ufshcd_set_clk_freq(hba, !scale_up);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800979
980out:
Subhash Jadavani394b9492020-03-26 02:25:40 -0700981 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800982 (scale_up ? "up" : "down"),
983 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
984 return ret;
985}
986
987/**
988 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
989 * @hba: per adapter instance
990 * @scale_up: True if scaling up and false if scaling down
991 *
992 * Returns true if scaling is required, false otherwise.
993 */
994static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
995 bool scale_up)
996{
997 struct ufs_clk_info *clki;
998 struct list_head *head = &hba->clk_list_head;
999
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03001000 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001001 return false;
1002
1003 list_for_each_entry(clki, head, list) {
1004 if (!IS_ERR_OR_NULL(clki->clk)) {
1005 if (scale_up && clki->max_freq) {
1006 if (clki->curr_freq == clki->max_freq)
1007 continue;
1008 return true;
1009 } else if (!scale_up && clki->min_freq) {
1010 if (clki->curr_freq == clki->min_freq)
1011 continue;
1012 return true;
1013 }
1014 }
1015 }
1016
1017 return false;
1018}
1019
1020static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
1021 u64 wait_timeout_us)
1022{
1023 unsigned long flags;
1024 int ret = 0;
1025 u32 tm_doorbell;
1026 u32 tr_doorbell;
1027 bool timeout = false, do_last_check = false;
1028 ktime_t start;
1029
1030 ufshcd_hold(hba, false);
1031 spin_lock_irqsave(hba->host->host_lock, flags);
1032 /*
1033 * Wait for all the outstanding tasks/transfer requests.
1034 * Verify by checking the doorbell registers are clear.
1035 */
1036 start = ktime_get();
1037 do {
1038 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
1039 ret = -EBUSY;
1040 goto out;
1041 }
1042
1043 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
1044 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1045 if (!tm_doorbell && !tr_doorbell) {
1046 timeout = false;
1047 break;
1048 } else if (do_last_check) {
1049 break;
1050 }
1051
1052 spin_unlock_irqrestore(hba->host->host_lock, flags);
1053 schedule();
1054 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
1055 wait_timeout_us) {
1056 timeout = true;
1057 /*
1058 * We might have scheduled out for long time so make
1059 * sure to check if doorbells are cleared by this time
1060 * or not.
1061 */
1062 do_last_check = true;
1063 }
1064 spin_lock_irqsave(hba->host->host_lock, flags);
1065 } while (tm_doorbell || tr_doorbell);
1066
1067 if (timeout) {
1068 dev_err(hba->dev,
1069 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1070 __func__, tm_doorbell, tr_doorbell);
1071 ret = -EBUSY;
1072 }
1073out:
1074 spin_unlock_irqrestore(hba->host->host_lock, flags);
1075 ufshcd_release(hba);
1076 return ret;
1077}
1078
1079/**
1080 * ufshcd_scale_gear - scale up/down UFS gear
1081 * @hba: per adapter instance
1082 * @scale_up: True for scaling up gear and false for scaling down
1083 *
1084 * Returns 0 for success,
1085 * Returns -EBUSY if scaling can't happen at this time
1086 * Returns non-zero for any other errors
1087 */
1088static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1089{
1090 #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1
1091 int ret = 0;
1092 struct ufs_pa_layer_attr new_pwr_info;
1093
1094 if (scale_up) {
1095 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1096 sizeof(struct ufs_pa_layer_attr));
1097 } else {
1098 memcpy(&new_pwr_info, &hba->pwr_info,
1099 sizeof(struct ufs_pa_layer_attr));
1100
1101 if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN
1102 || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) {
1103 /* save the current power mode */
1104 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1105 &hba->pwr_info,
1106 sizeof(struct ufs_pa_layer_attr));
1107
1108 /* scale down gear */
1109 new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1110 new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1111 }
1112 }
1113
1114 /* check if the power mode needs to be changed or not? */
Can Guo6a9df812020-02-11 21:38:28 -08001115 ret = ufshcd_config_pwr_mode(hba, &new_pwr_info);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001116 if (ret)
1117 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1118 __func__, ret,
1119 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1120 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1121
1122 return ret;
1123}
1124
1125static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1126{
1127 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1128 int ret = 0;
1129 /*
1130 * make sure that there are no outstanding requests when
1131 * clock scaling is in progress
1132 */
Subhash Jadavani38135532018-05-03 16:37:18 +05301133 ufshcd_scsi_block_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001134 down_write(&hba->clk_scaling_lock);
1135 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
1136 ret = -EBUSY;
1137 up_write(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301138 ufshcd_scsi_unblock_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001139 }
1140
1141 return ret;
1142}
1143
1144static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
1145{
1146 up_write(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301147 ufshcd_scsi_unblock_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001148}
1149
1150/**
1151 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1152 * @hba: per adapter instance
1153 * @scale_up: True for scaling up and false for scalin down
1154 *
1155 * Returns 0 for success,
1156 * Returns -EBUSY if scaling can't happen at this time
1157 * Returns non-zero for any other errors
1158 */
1159static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1160{
1161 int ret = 0;
1162
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001163 /* let's not get into low power until clock scaling is completed */
1164 ufshcd_hold(hba, false);
1165
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001166 ret = ufshcd_clock_scaling_prepare(hba);
1167 if (ret)
Subhash Jadavani394b9492020-03-26 02:25:40 -07001168 goto out;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001169
1170 /* scale down the gear before scaling down clocks */
1171 if (!scale_up) {
1172 ret = ufshcd_scale_gear(hba, false);
1173 if (ret)
Subhash Jadavani394b9492020-03-26 02:25:40 -07001174 goto out_unprepare;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001175 }
1176
1177 ret = ufshcd_scale_clks(hba, scale_up);
1178 if (ret) {
1179 if (!scale_up)
1180 ufshcd_scale_gear(hba, true);
Subhash Jadavani394b9492020-03-26 02:25:40 -07001181 goto out_unprepare;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001182 }
1183
1184 /* scale up the gear after scaling up clocks */
1185 if (scale_up) {
1186 ret = ufshcd_scale_gear(hba, true);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001187 if (ret) {
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001188 ufshcd_scale_clks(hba, false);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001189 goto out_unprepare;
1190 }
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001191 }
1192
Asutosh Das3d17b9b2020-04-22 14:41:42 -07001193 /* Enable Write Booster if we have scaled up else disable it */
1194 up_write(&hba->clk_scaling_lock);
1195 ufshcd_wb_ctrl(hba, scale_up);
1196 down_write(&hba->clk_scaling_lock);
1197
Subhash Jadavani394b9492020-03-26 02:25:40 -07001198out_unprepare:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001199 ufshcd_clock_scaling_unprepare(hba);
Subhash Jadavani394b9492020-03-26 02:25:40 -07001200out:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001201 ufshcd_release(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001202 return ret;
1203}
1204
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001205static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1206{
1207 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1208 clk_scaling.suspend_work);
1209 unsigned long irq_flags;
1210
1211 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1212 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1213 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1214 return;
1215 }
1216 hba->clk_scaling.is_suspended = true;
1217 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1218
1219 __ufshcd_suspend_clkscaling(hba);
1220}
1221
1222static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1223{
1224 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1225 clk_scaling.resume_work);
1226 unsigned long irq_flags;
1227
1228 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1229 if (!hba->clk_scaling.is_suspended) {
1230 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1231 return;
1232 }
1233 hba->clk_scaling.is_suspended = false;
1234 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1235
1236 devfreq_resume_device(hba->devfreq);
1237}
1238
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001239static int ufshcd_devfreq_target(struct device *dev,
1240 unsigned long *freq, u32 flags)
1241{
1242 int ret = 0;
1243 struct ufs_hba *hba = dev_get_drvdata(dev);
1244 ktime_t start;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001245 bool scale_up, sched_clk_scaling_suspend_work = false;
Bjorn Andersson092b4552018-05-17 23:26:37 -07001246 struct list_head *clk_list = &hba->clk_list_head;
1247 struct ufs_clk_info *clki;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001248 unsigned long irq_flags;
1249
1250 if (!ufshcd_is_clkscaling_supported(hba))
1251 return -EINVAL;
1252
Asutosh Das91831d32020-03-25 11:29:00 -07001253 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list);
1254 /* Override with the closest supported frequency */
1255 *freq = (unsigned long) clk_round_rate(clki->clk, *freq);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001256 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1257 if (ufshcd_eh_in_progress(hba)) {
1258 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1259 return 0;
1260 }
1261
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001262 if (!hba->clk_scaling.active_reqs)
1263 sched_clk_scaling_suspend_work = true;
1264
Bjorn Andersson092b4552018-05-17 23:26:37 -07001265 if (list_empty(clk_list)) {
1266 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1267 goto out;
1268 }
1269
Asutosh Das91831d32020-03-25 11:29:00 -07001270 /* Decide based on the rounded-off frequency and update */
Bjorn Andersson092b4552018-05-17 23:26:37 -07001271 scale_up = (*freq == clki->max_freq) ? true : false;
Asutosh Das91831d32020-03-25 11:29:00 -07001272 if (!scale_up)
1273 *freq = clki->min_freq;
1274 /* Update the frequency */
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001275 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1276 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1277 ret = 0;
1278 goto out; /* no state change required */
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001279 }
1280 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1281
1282 start = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001283 ret = ufshcd_devfreq_scale(hba, scale_up);
1284
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001285 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1286 (scale_up ? "up" : "down"),
1287 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1288
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001289out:
1290 if (sched_clk_scaling_suspend_work)
1291 queue_work(hba->clk_scaling.workq,
1292 &hba->clk_scaling.suspend_work);
1293
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001294 return ret;
1295}
1296
Bart Van Assche7252a362019-12-09 10:13:08 -08001297static bool ufshcd_is_busy(struct request *req, void *priv, bool reserved)
1298{
1299 int *busy = priv;
1300
1301 WARN_ON_ONCE(reserved);
1302 (*busy)++;
1303 return false;
1304}
1305
1306/* Whether or not any tag is in use by a request that is in progress. */
1307static bool ufshcd_any_tag_in_use(struct ufs_hba *hba)
1308{
1309 struct request_queue *q = hba->cmd_queue;
1310 int busy = 0;
1311
1312 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_is_busy, &busy);
1313 return busy;
1314}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001315
1316static int ufshcd_devfreq_get_dev_status(struct device *dev,
1317 struct devfreq_dev_status *stat)
1318{
1319 struct ufs_hba *hba = dev_get_drvdata(dev);
1320 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1321 unsigned long flags;
Asutosh Das91831d32020-03-25 11:29:00 -07001322 struct list_head *clk_list = &hba->clk_list_head;
1323 struct ufs_clk_info *clki;
Stanley Chub1bf66d2020-06-11 18:10:43 +08001324 ktime_t curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001325
1326 if (!ufshcd_is_clkscaling_supported(hba))
1327 return -EINVAL;
1328
1329 memset(stat, 0, sizeof(*stat));
1330
1331 spin_lock_irqsave(hba->host->host_lock, flags);
Stanley Chub1bf66d2020-06-11 18:10:43 +08001332 curr_t = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001333 if (!scaling->window_start_t)
1334 goto start_window;
1335
Asutosh Das91831d32020-03-25 11:29:00 -07001336 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1337 /*
1338 * If current frequency is 0, then the ondemand governor considers
1339 * there's no initial frequency set. And it always requests to set
1340 * to max. frequency.
1341 */
1342 stat->current_frequency = clki->curr_freq;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001343 if (scaling->is_busy_started)
Stanley Chub1bf66d2020-06-11 18:10:43 +08001344 scaling->tot_busy_t += ktime_us_delta(curr_t,
1345 scaling->busy_start_t);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001346
Stanley Chub1bf66d2020-06-11 18:10:43 +08001347 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001348 stat->busy_time = scaling->tot_busy_t;
1349start_window:
Stanley Chub1bf66d2020-06-11 18:10:43 +08001350 scaling->window_start_t = curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001351 scaling->tot_busy_t = 0;
1352
1353 if (hba->outstanding_reqs) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08001354 scaling->busy_start_t = curr_t;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001355 scaling->is_busy_started = true;
1356 } else {
1357 scaling->busy_start_t = 0;
1358 scaling->is_busy_started = false;
1359 }
1360 spin_unlock_irqrestore(hba->host->host_lock, flags);
1361 return 0;
1362}
1363
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001364static int ufshcd_devfreq_init(struct ufs_hba *hba)
1365{
Bjorn Andersson092b4552018-05-17 23:26:37 -07001366 struct list_head *clk_list = &hba->clk_list_head;
1367 struct ufs_clk_info *clki;
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001368 struct devfreq *devfreq;
1369 int ret;
1370
Bjorn Andersson092b4552018-05-17 23:26:37 -07001371 /* Skip devfreq if we don't have any clocks in the list */
1372 if (list_empty(clk_list))
1373 return 0;
1374
1375 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1376 dev_pm_opp_add(hba->dev, clki->min_freq, 0);
1377 dev_pm_opp_add(hba->dev, clki->max_freq, 0);
1378
Stanley Chu90b84912020-05-09 17:37:13 +08001379 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile,
1380 &hba->vps->ondemand_data);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001381 devfreq = devfreq_add_device(hba->dev,
Stanley Chu90b84912020-05-09 17:37:13 +08001382 &hba->vps->devfreq_profile,
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001383 DEVFREQ_GOV_SIMPLE_ONDEMAND,
Stanley Chu90b84912020-05-09 17:37:13 +08001384 &hba->vps->ondemand_data);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001385 if (IS_ERR(devfreq)) {
1386 ret = PTR_ERR(devfreq);
1387 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001388
1389 dev_pm_opp_remove(hba->dev, clki->min_freq);
1390 dev_pm_opp_remove(hba->dev, clki->max_freq);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001391 return ret;
1392 }
1393
1394 hba->devfreq = devfreq;
1395
1396 return 0;
1397}
1398
Bjorn Andersson092b4552018-05-17 23:26:37 -07001399static void ufshcd_devfreq_remove(struct ufs_hba *hba)
1400{
1401 struct list_head *clk_list = &hba->clk_list_head;
1402 struct ufs_clk_info *clki;
1403
1404 if (!hba->devfreq)
1405 return;
1406
1407 devfreq_remove_device(hba->devfreq);
1408 hba->devfreq = NULL;
1409
1410 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1411 dev_pm_opp_remove(hba->dev, clki->min_freq);
1412 dev_pm_opp_remove(hba->dev, clki->max_freq);
1413}
1414
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001415static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1416{
1417 unsigned long flags;
1418
1419 devfreq_suspend_device(hba->devfreq);
1420 spin_lock_irqsave(hba->host->host_lock, flags);
1421 hba->clk_scaling.window_start_t = 0;
1422 spin_unlock_irqrestore(hba->host->host_lock, flags);
1423}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001424
Gilad Bronera5082532016-10-17 17:10:00 -07001425static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1426{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001427 unsigned long flags;
1428 bool suspend = false;
1429
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001430 if (!ufshcd_is_clkscaling_supported(hba))
1431 return;
1432
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001433 spin_lock_irqsave(hba->host->host_lock, flags);
1434 if (!hba->clk_scaling.is_suspended) {
1435 suspend = true;
1436 hba->clk_scaling.is_suspended = true;
1437 }
1438 spin_unlock_irqrestore(hba->host->host_lock, flags);
1439
1440 if (suspend)
1441 __ufshcd_suspend_clkscaling(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07001442}
1443
1444static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1445{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001446 unsigned long flags;
1447 bool resume = false;
1448
1449 if (!ufshcd_is_clkscaling_supported(hba))
1450 return;
1451
1452 spin_lock_irqsave(hba->host->host_lock, flags);
1453 if (hba->clk_scaling.is_suspended) {
1454 resume = true;
1455 hba->clk_scaling.is_suspended = false;
1456 }
1457 spin_unlock_irqrestore(hba->host->host_lock, flags);
1458
1459 if (resume)
1460 devfreq_resume_device(hba->devfreq);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001461}
1462
1463static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1464 struct device_attribute *attr, char *buf)
1465{
1466 struct ufs_hba *hba = dev_get_drvdata(dev);
1467
1468 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
1469}
1470
1471static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1472 struct device_attribute *attr, const char *buf, size_t count)
1473{
1474 struct ufs_hba *hba = dev_get_drvdata(dev);
1475 u32 value;
1476 int err;
1477
1478 if (kstrtou32(buf, 0, &value))
1479 return -EINVAL;
1480
1481 value = !!value;
1482 if (value == hba->clk_scaling.is_allowed)
1483 goto out;
1484
1485 pm_runtime_get_sync(hba->dev);
1486 ufshcd_hold(hba, false);
1487
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001488 cancel_work_sync(&hba->clk_scaling.suspend_work);
1489 cancel_work_sync(&hba->clk_scaling.resume_work);
1490
1491 hba->clk_scaling.is_allowed = value;
1492
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001493 if (value) {
1494 ufshcd_resume_clkscaling(hba);
1495 } else {
1496 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001497 err = ufshcd_devfreq_scale(hba, true);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001498 if (err)
1499 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1500 __func__, err);
1501 }
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001502
1503 ufshcd_release(hba);
1504 pm_runtime_put_sync(hba->dev);
1505out:
1506 return count;
Gilad Bronera5082532016-10-17 17:10:00 -07001507}
1508
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001509static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
1510{
1511 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1512 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1513 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1514 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1515 hba->clk_scaling.enable_attr.attr.mode = 0644;
1516 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1517 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1518}
1519
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001520static void ufshcd_ungate_work(struct work_struct *work)
1521{
1522 int ret;
1523 unsigned long flags;
1524 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1525 clk_gating.ungate_work);
1526
1527 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1528
1529 spin_lock_irqsave(hba->host->host_lock, flags);
1530 if (hba->clk_gating.state == CLKS_ON) {
1531 spin_unlock_irqrestore(hba->host->host_lock, flags);
1532 goto unblock_reqs;
1533 }
1534
1535 spin_unlock_irqrestore(hba->host->host_lock, flags);
1536 ufshcd_setup_clocks(hba, true);
1537
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001538 ufshcd_enable_irq(hba);
1539
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001540 /* Exit from hibern8 */
1541 if (ufshcd_can_hibern8_during_gating(hba)) {
1542 /* Prevent gating in this path */
1543 hba->clk_gating.is_suspended = true;
1544 if (ufshcd_is_link_hibern8(hba)) {
1545 ret = ufshcd_uic_hibern8_exit(hba);
1546 if (ret)
1547 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1548 __func__, ret);
1549 else
1550 ufshcd_set_link_active(hba);
1551 }
1552 hba->clk_gating.is_suspended = false;
1553 }
1554unblock_reqs:
Subhash Jadavani38135532018-05-03 16:37:18 +05301555 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001556}
1557
1558/**
1559 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1560 * Also, exit from hibern8 mode and set the link as active.
1561 * @hba: per adapter instance
1562 * @async: This indicates whether caller should ungate clocks asynchronously.
1563 */
1564int ufshcd_hold(struct ufs_hba *hba, bool async)
1565{
1566 int rc = 0;
1567 unsigned long flags;
1568
1569 if (!ufshcd_is_clkgating_allowed(hba))
1570 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001571 spin_lock_irqsave(hba->host->host_lock, flags);
1572 hba->clk_gating.active_reqs++;
1573
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001574 if (ufshcd_eh_in_progress(hba)) {
1575 spin_unlock_irqrestore(hba->host->host_lock, flags);
1576 return 0;
1577 }
1578
Sahitya Tummala856b3482014-09-25 15:32:34 +03001579start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001580 switch (hba->clk_gating.state) {
1581 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001582 /*
1583 * Wait for the ungate work to complete if in progress.
1584 * Though the clocks may be in ON state, the link could
1585 * still be in hibner8 state if hibern8 is allowed
1586 * during clock gating.
1587 * Make sure we exit hibern8 state also in addition to
1588 * clocks being ON.
1589 */
1590 if (ufshcd_can_hibern8_during_gating(hba) &&
1591 ufshcd_is_link_hibern8(hba)) {
Can Guoc63d6092020-02-10 19:40:48 -08001592 if (async) {
1593 rc = -EAGAIN;
1594 hba->clk_gating.active_reqs--;
1595 break;
1596 }
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001597 spin_unlock_irqrestore(hba->host->host_lock, flags);
1598 flush_work(&hba->clk_gating.ungate_work);
1599 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
Can Guo2dec9472020-08-09 05:15:47 -07001845 hba->clk_gating.state = CLKS_ON;
1846
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001847 hba->clk_gating.delay_ms = 150;
1848 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1849 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1850
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301851 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
1852 hba->host->host_no);
1853 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
1854 WQ_MEM_RECLAIM);
1855
Sahitya Tummalab4274112016-12-22 18:40:39 -08001856 hba->clk_gating.is_enabled = true;
1857
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001858 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1859 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1860 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1861 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
Sahitya Tummalab4274112016-12-22 18:40:39 -08001862 hba->clk_gating.delay_attr.attr.mode = 0644;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001863 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1864 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Sahitya Tummalab4274112016-12-22 18:40:39 -08001865
1866 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1867 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1868 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1869 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1870 hba->clk_gating.enable_attr.attr.mode = 0644;
1871 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1872 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001873}
1874
1875static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1876{
1877 if (!ufshcd_is_clkgating_allowed(hba))
1878 return;
1879 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001880 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001881 cancel_work_sync(&hba->clk_gating.ungate_work);
1882 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301883 destroy_workqueue(hba->clk_gating.clk_gating_workq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001884}
1885
Sahitya Tummala856b3482014-09-25 15:32:34 +03001886/* Must be called with host lock acquired */
1887static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1888{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001889 bool queue_resume_work = false;
Stanley Chub1bf66d2020-06-11 18:10:43 +08001890 ktime_t curr_t = ktime_get();
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001891
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001892 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001893 return;
1894
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001895 if (!hba->clk_scaling.active_reqs++)
1896 queue_resume_work = true;
1897
1898 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1899 return;
1900
1901 if (queue_resume_work)
1902 queue_work(hba->clk_scaling.workq,
1903 &hba->clk_scaling.resume_work);
1904
1905 if (!hba->clk_scaling.window_start_t) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08001906 hba->clk_scaling.window_start_t = curr_t;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001907 hba->clk_scaling.tot_busy_t = 0;
1908 hba->clk_scaling.is_busy_started = false;
1909 }
1910
Sahitya Tummala856b3482014-09-25 15:32:34 +03001911 if (!hba->clk_scaling.is_busy_started) {
Stanley Chub1bf66d2020-06-11 18:10:43 +08001912 hba->clk_scaling.busy_start_t = curr_t;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001913 hba->clk_scaling.is_busy_started = true;
1914 }
1915}
1916
1917static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1918{
1919 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1920
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001921 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001922 return;
1923
1924 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1925 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1926 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001927 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001928 scaling->is_busy_started = false;
1929 }
1930}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301931/**
1932 * ufshcd_send_command - Send SCSI or device management commands
1933 * @hba: per adapter instance
1934 * @task_tag: Task tag of the command
1935 */
1936static inline
1937void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1938{
Stanley Chu6edfdcf2020-07-06 14:07:07 +08001939 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag];
1940
1941 lrbp->issue_time_stamp = ktime_get();
1942 lrbp->compl_time_stamp = ktime_set(0, 0);
1943 ufshcd_vops_setup_xfer_req(hba, task_tag, (lrbp->cmd ? true : false));
Bart Van Asscheeacf36f2019-12-24 14:02:46 -08001944 ufshcd_add_command_trace(hba, task_tag, "send");
Sahitya Tummala856b3482014-09-25 15:32:34 +03001945 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301946 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301947 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001948 /* Make sure that doorbell is committed immediately */
1949 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301950}
1951
1952/**
1953 * ufshcd_copy_sense_data - Copy sense data in case of check condition
Bart Van Assche8aa29f12018-03-01 15:07:20 -08001954 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301955 */
1956static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1957{
1958 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301959 if (lrbp->sense_buffer &&
1960 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001961 int len_to_copy;
1962
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301963 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Avri Altman09a5a242018-11-22 20:04:56 +02001964 len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001965
Avri Altman09a5a242018-11-22 20:04:56 +02001966 memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data,
1967 len_to_copy);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301968 }
1969}
1970
1971/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301972 * ufshcd_copy_query_response() - Copy the Query Response and the data
1973 * descriptor
1974 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08001975 * @lrbp: pointer to local reference block
Dolev Raviv68078d52013-07-30 00:35:58 +05301976 */
1977static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001978int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05301979{
1980 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1981
Dolev Raviv68078d52013-07-30 00:35:58 +05301982 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301983
Dolev Raviv68078d52013-07-30 00:35:58 +05301984 /* Get the descriptor */
Avri Altman1c908362019-05-21 11:24:22 +03001985 if (hba->dev_cmd.query.descriptor &&
1986 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001987 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05301988 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001989 u16 resp_len;
1990 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05301991
1992 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001993 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301994 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001995 buf_len = be16_to_cpu(
1996 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001997 if (likely(buf_len >= resp_len)) {
1998 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
1999 } else {
2000 dev_warn(hba->dev,
Bean Huo3d4881d2019-11-12 23:34:35 +01002001 "%s: rsp size %d is bigger than buffer size %d",
2002 __func__, resp_len, buf_len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002003 return -EINVAL;
2004 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302005 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002006
2007 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302008}
2009
2010/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302011 * ufshcd_hba_capabilities - Read controller capabilities
2012 * @hba: per adapter instance
Satya Tangiraladf043c742020-07-06 20:04:14 +00002013 *
2014 * Return: 0 on success, negative on error.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302015 */
Satya Tangiraladf043c742020-07-06 20:04:14 +00002016static inline int ufshcd_hba_capabilities(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302017{
Satya Tangiraladf043c742020-07-06 20:04:14 +00002018 int err;
2019
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302020 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302021
2022 /* nutrs and nutmrs are 0 based values */
2023 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2024 hba->nutmrs =
2025 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002026
2027 /* Read crypto capabilities */
2028 err = ufshcd_hba_init_crypto_capabilities(hba);
2029 if (err)
2030 dev_err(hba->dev, "crypto setup failed\n");
2031
2032 return err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302033}
2034
2035/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302036 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2037 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302038 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302039 * Return true on success, else false
2040 */
2041static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2042{
2043 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2044 return true;
2045 else
2046 return false;
2047}
2048
2049/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302050 * ufshcd_get_upmcrs - Get the power mode change request status
2051 * @hba: Pointer to adapter instance
2052 *
2053 * This function gets the UPMCRS field of HCS register
2054 * Returns value of UPMCRS field
2055 */
2056static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2057{
2058 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2059}
2060
2061/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302062 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
2063 * @hba: per adapter instance
2064 * @uic_cmd: UIC command
2065 *
2066 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302067 */
2068static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302069ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302070{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302071 WARN_ON(hba->active_uic_cmd);
2072
2073 hba->active_uic_cmd = uic_cmd;
2074
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302075 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302076 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2077 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2078 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302079
Stanley Chuaa5c6972020-06-15 15:22:35 +08002080 ufshcd_add_uic_command_trace(hba, uic_cmd, "send");
2081
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302082 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302083 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302084 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302085}
2086
2087/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302088 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
2089 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002090 * @uic_cmd: UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302091 *
2092 * Must be called with mutex held.
2093 * Returns 0 only if success.
2094 */
2095static int
2096ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2097{
2098 int ret;
2099 unsigned long flags;
2100
2101 if (wait_for_completion_timeout(&uic_cmd->done,
2102 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
2103 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2104 else
2105 ret = -ETIMEDOUT;
2106
2107 spin_lock_irqsave(hba->host->host_lock, flags);
2108 hba->active_uic_cmd = NULL;
2109 spin_unlock_irqrestore(hba->host->host_lock, flags);
2110
2111 return ret;
2112}
2113
2114/**
2115 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2116 * @hba: per adapter instance
2117 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002118 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302119 *
2120 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002121 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302122 * Returns 0 only if success.
2123 */
2124static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002125__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2126 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302127{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302128 if (!ufshcd_ready_for_uic_cmd(hba)) {
2129 dev_err(hba->dev,
2130 "Controller not ready to accept UIC commands\n");
2131 return -EIO;
2132 }
2133
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002134 if (completion)
2135 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302136
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302137 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302138
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002139 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302140}
2141
2142/**
2143 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2144 * @hba: per adapter instance
2145 * @uic_cmd: UIC command
2146 *
2147 * Returns 0 only if success.
2148 */
Avri Altmane77044c52018-10-07 17:30:39 +03002149int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302150{
2151 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002152 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302153
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002154 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302155 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002156 ufshcd_add_delay_before_dme_cmd(hba);
2157
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002158 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002159 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002160 spin_unlock_irqrestore(hba->host->host_lock, flags);
2161 if (!ret)
2162 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2163
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302164 mutex_unlock(&hba->uic_cmd_mutex);
2165
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002166 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302167 return ret;
2168}
2169
2170/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302171 * ufshcd_map_sg - Map scatter-gather list to prdt
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002172 * @hba: per adapter instance
2173 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302174 *
2175 * Returns 0 in case of success, non-zero value in case of failure
2176 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002177static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302178{
2179 struct ufshcd_sg_entry *prd_table;
2180 struct scatterlist *sg;
2181 struct scsi_cmnd *cmd;
2182 int sg_segments;
2183 int i;
2184
2185 cmd = lrbp->cmd;
2186 sg_segments = scsi_dma_map(cmd);
2187 if (sg_segments < 0)
2188 return sg_segments;
2189
2190 if (sg_segments) {
Alim Akhtar26f968d2020-05-28 06:46:52 +05302191
2192 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2193 lrbp->utr_descriptor_ptr->prd_table_length =
2194 cpu_to_le16((sg_segments *
2195 sizeof(struct ufshcd_sg_entry)));
2196 else
2197 lrbp->utr_descriptor_ptr->prd_table_length =
2198 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302199
2200 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2201
2202 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2203 prd_table[i].size =
2204 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2205 prd_table[i].base_addr =
2206 cpu_to_le32(lower_32_bits(sg->dma_address));
2207 prd_table[i].upper_addr =
2208 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002209 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302210 }
2211 } else {
2212 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2213 }
2214
2215 return 0;
2216}
2217
2218/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302219 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302220 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302221 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302222 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302223static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302224{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302225 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2226
2227 if (hba->ufs_version == UFSHCI_VERSION_10) {
2228 u32 rw;
2229 rw = set & INTERRUPT_MASK_RW_VER_10;
2230 set = rw | ((set ^ intrs) & intrs);
2231 } else {
2232 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302233 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302234
2235 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2236}
2237
2238/**
2239 * ufshcd_disable_intr - disable interrupts
2240 * @hba: per adapter instance
2241 * @intrs: interrupt bits
2242 */
2243static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2244{
2245 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2246
2247 if (hba->ufs_version == UFSHCI_VERSION_10) {
2248 u32 rw;
2249 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2250 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2251 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2252
2253 } else {
2254 set &= ~intrs;
2255 }
2256
2257 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302258}
2259
2260/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302261 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2262 * descriptor according to request
2263 * @lrbp: pointer to local reference block
2264 * @upiu_flags: flags required in the header
2265 * @cmd_dir: requests data direction
2266 */
2267static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Bean Huoa23064c2020-07-06 14:39:36 +02002268 u8 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302269{
2270 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2271 u32 data_direction;
2272 u32 dword_0;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002273 u32 dword_1 = 0;
2274 u32 dword_3 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302275
2276 if (cmd_dir == DMA_FROM_DEVICE) {
2277 data_direction = UTP_DEVICE_TO_HOST;
2278 *upiu_flags = UPIU_CMD_FLAGS_READ;
2279 } else if (cmd_dir == DMA_TO_DEVICE) {
2280 data_direction = UTP_HOST_TO_DEVICE;
2281 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2282 } else {
2283 data_direction = UTP_NO_DATA_TRANSFER;
2284 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2285 }
2286
2287 dword_0 = data_direction | (lrbp->command_type
2288 << UPIU_COMMAND_TYPE_OFFSET);
2289 if (lrbp->intr_cmd)
2290 dword_0 |= UTP_REQ_DESC_INT_CMD;
2291
Satya Tangiraladf043c742020-07-06 20:04:14 +00002292 /* Prepare crypto related dwords */
2293 ufshcd_prepare_req_desc_hdr_crypto(lrbp, &dword_0, &dword_1, &dword_3);
2294
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302295 /* Transfer request descriptor header fields */
2296 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002297 req_desc->header.dword_1 = cpu_to_le32(dword_1);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302298 /*
2299 * assigning invalid value for command status. Controller
2300 * updates OCS on command completion, with the command
2301 * status
2302 */
2303 req_desc->header.dword_2 =
2304 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Satya Tangiraladf043c742020-07-06 20:04:14 +00002305 req_desc->header.dword_3 = cpu_to_le32(dword_3);
Yaniv Gardi51047262016-02-01 15:02:38 +02002306
2307 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302308}
2309
2310/**
2311 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2312 * for scsi commands
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002313 * @lrbp: local reference block pointer
2314 * @upiu_flags: flags
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302315 */
2316static
Bean Huoa23064c2020-07-06 14:39:36 +02002317void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302318{
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002319 struct scsi_cmnd *cmd = lrbp->cmd;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302320 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002321 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302322
2323 /* command descriptor fields */
2324 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2325 UPIU_TRANSACTION_COMMAND, upiu_flags,
2326 lrbp->lun, lrbp->task_tag);
2327 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2328 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2329
2330 /* Total EHS length and Data segment length will be zero */
2331 ucd_req_ptr->header.dword_2 = 0;
2332
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002333 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302334
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002335 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE);
Avri Altmana851b2b2018-10-07 17:30:34 +03002336 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002337 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002338
2339 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302340}
2341
Dolev Raviv68078d52013-07-30 00:35:58 +05302342/**
2343 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2344 * for query requsts
2345 * @hba: UFS hba
2346 * @lrbp: local reference block pointer
2347 * @upiu_flags: flags
2348 */
2349static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
Bean Huoa23064c2020-07-06 14:39:36 +02002350 struct ufshcd_lrb *lrbp, u8 upiu_flags)
Dolev Raviv68078d52013-07-30 00:35:58 +05302351{
2352 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2353 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302354 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302355
2356 /* Query request header */
2357 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2358 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2359 lrbp->lun, lrbp->task_tag);
2360 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2361 0, query->request.query_func, 0, 0);
2362
Zang Leigang68612852016-08-25 17:39:19 +08002363 /* Data segment length only need for WRITE_DESC */
2364 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2365 ucd_req_ptr->header.dword_2 =
2366 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2367 else
2368 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302369
2370 /* Copy the Query Request buffer as is */
2371 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2372 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302373
2374 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002375 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
Avri Altman220d17a62018-10-07 17:30:36 +03002376 memcpy(ucd_req_ptr + 1, query->descriptor, len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002377
Yaniv Gardi51047262016-02-01 15:02:38 +02002378 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302379}
2380
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302381static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2382{
2383 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2384
2385 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2386
2387 /* command descriptor fields */
2388 ucd_req_ptr->header.dword_0 =
2389 UPIU_HEADER_DWORD(
2390 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002391 /* clear rest of the fields of basic header */
2392 ucd_req_ptr->header.dword_1 = 0;
2393 ucd_req_ptr->header.dword_2 = 0;
2394
2395 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302396}
2397
2398/**
Joao Pinto300bb132016-05-11 12:21:27 +01002399 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
2400 * for Device Management Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002401 * @hba: per adapter instance
2402 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302403 */
Joao Pinto300bb132016-05-11 12:21:27 +01002404static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302405{
Bean Huoa23064c2020-07-06 14:39:36 +02002406 u8 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302407 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302408
kehuanlin83dc7e32017-09-06 17:58:39 +08002409 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2410 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002411 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
kehuanlin83dc7e32017-09-06 17:58:39 +08002412 else
2413 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002414
2415 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2416 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2417 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2418 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2419 ufshcd_prepare_utp_nop_upiu(lrbp);
2420 else
2421 ret = -EINVAL;
2422
2423 return ret;
2424}
2425
2426/**
2427 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2428 * for SCSI Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002429 * @hba: per adapter instance
2430 * @lrbp: pointer to local reference block
Joao Pinto300bb132016-05-11 12:21:27 +01002431 */
2432static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2433{
Bean Huoa23064c2020-07-06 14:39:36 +02002434 u8 upiu_flags;
Joao Pinto300bb132016-05-11 12:21:27 +01002435 int ret = 0;
2436
kehuanlin83dc7e32017-09-06 17:58:39 +08002437 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2438 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002439 lrbp->command_type = UTP_CMD_TYPE_SCSI;
kehuanlin83dc7e32017-09-06 17:58:39 +08002440 else
2441 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002442
2443 if (likely(lrbp->cmd)) {
2444 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2445 lrbp->cmd->sc_data_direction);
2446 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2447 } else {
2448 ret = -EINVAL;
2449 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302450
2451 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302452}
2453
2454/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002455 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002456 * @upiu_wlun_id: UPIU W-LUN id
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002457 *
2458 * Returns SCSI W-LUN id
2459 */
2460static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2461{
2462 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2463}
2464
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08002465static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i)
2466{
2467 struct utp_transfer_cmd_desc *cmd_descp = hba->ucdl_base_addr;
2468 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr;
2469 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr +
2470 i * sizeof(struct utp_transfer_cmd_desc);
2471 u16 response_offset = offsetof(struct utp_transfer_cmd_desc,
2472 response_upiu);
2473 u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
2474
2475 lrb->utr_descriptor_ptr = utrdlp + i;
2476 lrb->utrd_dma_addr = hba->utrdl_dma_addr +
2477 i * sizeof(struct utp_transfer_req_desc);
2478 lrb->ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i);
2479 lrb->ucd_req_dma_addr = cmd_desc_element_addr;
2480 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2481 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset;
2482 lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2483 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset;
2484}
2485
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002486/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302487 * ufshcd_queuecommand - main entry point for SCSI requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002488 * @host: SCSI host pointer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302489 * @cmd: command from SCSI Midlayer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302490 *
2491 * Returns 0 for success, non-zero in case of failure
2492 */
2493static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2494{
2495 struct ufshcd_lrb *lrbp;
2496 struct ufs_hba *hba;
2497 unsigned long flags;
2498 int tag;
2499 int err = 0;
2500
2501 hba = shost_priv(host);
2502
2503 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002504 if (!ufshcd_valid_tag(hba, tag)) {
2505 dev_err(hba->dev,
2506 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2507 __func__, tag, cmd, cmd->request);
2508 BUG();
2509 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302510
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002511 if (!down_read_trylock(&hba->clk_scaling_lock))
2512 return SCSI_MLQUEUE_HOST_BUSY;
2513
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302514 spin_lock_irqsave(hba->host->host_lock, flags);
2515 switch (hba->ufshcd_state) {
2516 case UFSHCD_STATE_OPERATIONAL:
2517 break;
Zang Leigang141f8162016-11-16 11:29:37 +08002518 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302519 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302520 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302521 goto out_unlock;
2522 case UFSHCD_STATE_ERROR:
2523 set_host_byte(cmd, DID_ERROR);
2524 cmd->scsi_done(cmd);
2525 goto out_unlock;
2526 default:
2527 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2528 __func__, hba->ufshcd_state);
2529 set_host_byte(cmd, DID_BAD_TARGET);
2530 cmd->scsi_done(cmd);
2531 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302532 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002533
2534 /* if error handling is in progress, don't issue commands */
2535 if (ufshcd_eh_in_progress(hba)) {
2536 set_host_byte(cmd, DID_ERROR);
2537 cmd->scsi_done(cmd);
2538 goto out_unlock;
2539 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302540 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302541
Gilad Broner7fabb772017-02-03 16:56:50 -08002542 hba->req_abort_count = 0;
2543
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002544 err = ufshcd_hold(hba, true);
2545 if (err) {
2546 err = SCSI_MLQUEUE_HOST_BUSY;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002547 goto out;
2548 }
Can Guo2dec9472020-08-09 05:15:47 -07002549 WARN_ON(ufshcd_is_clkgating_allowed(hba) &&
2550 (hba->clk_gating.state != CLKS_ON));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002551
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302552 lrbp = &hba->lrb[tag];
2553
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302554 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302555 lrbp->cmd = cmd;
Avri Altman09a5a242018-11-22 20:04:56 +02002556 lrbp->sense_bufflen = UFS_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302557 lrbp->sense_buffer = cmd->sense_buffer;
2558 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002559 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002560 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Satya Tangiraladf043c742020-07-06 20:04:14 +00002561
2562 ufshcd_prepare_lrbp_crypto(cmd->request, lrbp);
2563
Gilad Bronere0b299e2017-02-03 16:56:40 -08002564 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302565
Joao Pinto300bb132016-05-11 12:21:27 +01002566 ufshcd_comp_scsi_upiu(hba, lrbp);
2567
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002568 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302569 if (err) {
2570 lrbp->cmd = NULL;
Can Guo17c7d352019-12-05 02:14:33 +00002571 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302572 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302573 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002574 /* Make sure descriptors are ready before ringing the doorbell */
2575 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302576
2577 /* issue command to the controller */
2578 spin_lock_irqsave(hba->host->host_lock, flags);
2579 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302580out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302581 spin_unlock_irqrestore(hba->host->host_lock, flags);
2582out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002583 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302584 return err;
2585}
2586
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302587static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2588 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2589{
2590 lrbp->cmd = NULL;
2591 lrbp->sense_bufflen = 0;
2592 lrbp->sense_buffer = NULL;
2593 lrbp->task_tag = tag;
2594 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302595 lrbp->intr_cmd = true; /* No interrupt aggregation */
Satya Tangiraladf043c742020-07-06 20:04:14 +00002596 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302597 hba->dev_cmd.type = cmd_type;
2598
Joao Pinto300bb132016-05-11 12:21:27 +01002599 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302600}
2601
2602static int
2603ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2604{
2605 int err = 0;
2606 unsigned long flags;
2607 u32 mask = 1 << tag;
2608
2609 /* clear outstanding transaction before retry */
2610 spin_lock_irqsave(hba->host->host_lock, flags);
2611 ufshcd_utrl_clear(hba, tag);
2612 spin_unlock_irqrestore(hba->host->host_lock, flags);
2613
2614 /*
2615 * wait for for h/w to clear corresponding bit in door-bell.
2616 * max. wait is 1 sec.
2617 */
2618 err = ufshcd_wait_for_register(hba,
2619 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Bart Van Assche5cac1092020-05-07 15:27:50 -07002620 mask, ~mask, 1000, 1000);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302621
2622 return err;
2623}
2624
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002625static int
2626ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2627{
2628 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2629
2630 /* Get the UPIU response */
2631 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2632 UPIU_RSP_CODE_OFFSET;
2633 return query_res->response;
2634}
2635
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302636/**
2637 * ufshcd_dev_cmd_completion() - handles device management command responses
2638 * @hba: per adapter instance
2639 * @lrbp: pointer to local reference block
2640 */
2641static int
2642ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2643{
2644 int resp;
2645 int err = 0;
2646
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002647 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302648 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2649
2650 switch (resp) {
2651 case UPIU_TRANSACTION_NOP_IN:
2652 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2653 err = -EINVAL;
2654 dev_err(hba->dev, "%s: unexpected response %x\n",
2655 __func__, resp);
2656 }
2657 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302658 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002659 err = ufshcd_check_query_response(hba, lrbp);
2660 if (!err)
2661 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302662 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302663 case UPIU_TRANSACTION_REJECT_UPIU:
2664 /* TODO: handle Reject UPIU Response */
2665 err = -EPERM;
2666 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2667 __func__);
2668 break;
2669 default:
2670 err = -EINVAL;
2671 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2672 __func__, resp);
2673 break;
2674 }
2675
2676 return err;
2677}
2678
2679static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2680 struct ufshcd_lrb *lrbp, int max_timeout)
2681{
2682 int err = 0;
2683 unsigned long time_left;
2684 unsigned long flags;
2685
2686 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2687 msecs_to_jiffies(max_timeout));
2688
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002689 /* Make sure descriptors are ready before ringing the doorbell */
2690 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302691 spin_lock_irqsave(hba->host->host_lock, flags);
2692 hba->dev_cmd.complete = NULL;
2693 if (likely(time_left)) {
2694 err = ufshcd_get_tr_ocs(lrbp);
2695 if (!err)
2696 err = ufshcd_dev_cmd_completion(hba, lrbp);
2697 }
2698 spin_unlock_irqrestore(hba->host->host_lock, flags);
2699
2700 if (!time_left) {
2701 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002702 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2703 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302704 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002705 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302706 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002707 /*
2708 * in case of an error, after clearing the doorbell,
2709 * we also need to clear the outstanding_request
2710 * field in hba
2711 */
2712 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302713 }
2714
2715 return err;
2716}
2717
2718/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302719 * ufshcd_exec_dev_cmd - API for sending device management requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002720 * @hba: UFS hba
2721 * @cmd_type: specifies the type (NOP, Query...)
2722 * @timeout: time in seconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302723 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302724 * NOTE: Since there is only one available tag for device management commands,
2725 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302726 */
2727static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2728 enum dev_cmd_type cmd_type, int timeout)
2729{
Bart Van Assche7252a362019-12-09 10:13:08 -08002730 struct request_queue *q = hba->cmd_queue;
2731 struct request *req;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302732 struct ufshcd_lrb *lrbp;
2733 int err;
2734 int tag;
2735 struct completion wait;
2736 unsigned long flags;
2737
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002738 down_read(&hba->clk_scaling_lock);
2739
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302740 /*
2741 * Get free slot, sleep if slots are unavailable.
2742 * Even though we use wait_event() which sleeps indefinitely,
2743 * the maximum wait time is bounded by SCSI request timeout.
2744 */
Bart Van Assche7252a362019-12-09 10:13:08 -08002745 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03002746 if (IS_ERR(req)) {
2747 err = PTR_ERR(req);
2748 goto out_unlock;
2749 }
Bart Van Assche7252a362019-12-09 10:13:08 -08002750 tag = req->tag;
2751 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302752
2753 init_completion(&wait);
2754 lrbp = &hba->lrb[tag];
2755 WARN_ON(lrbp->cmd);
2756 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2757 if (unlikely(err))
2758 goto out_put_tag;
2759
2760 hba->dev_cmd.complete = &wait;
2761
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03002762 ufshcd_add_query_upiu_trace(hba, tag, "query_send");
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002763 /* Make sure descriptors are ready before ringing the doorbell */
2764 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302765 spin_lock_irqsave(hba->host->host_lock, flags);
2766 ufshcd_send_command(hba, tag);
2767 spin_unlock_irqrestore(hba->host->host_lock, flags);
2768
2769 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2770
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03002771 ufshcd_add_query_upiu_trace(hba, tag,
2772 err ? "query_complete_err" : "query_complete");
2773
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302774out_put_tag:
Bart Van Assche7252a362019-12-09 10:13:08 -08002775 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03002776out_unlock:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002777 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302778 return err;
2779}
2780
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302781/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002782 * ufshcd_init_query() - init the query response and request parameters
2783 * @hba: per-adapter instance
2784 * @request: address of the request pointer to be initialized
2785 * @response: address of the response pointer to be initialized
2786 * @opcode: operation to perform
2787 * @idn: flag idn to access
2788 * @index: LU number to access
2789 * @selector: query/flag/descriptor further identification
2790 */
2791static inline void ufshcd_init_query(struct ufs_hba *hba,
2792 struct ufs_query_req **request, struct ufs_query_res **response,
2793 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2794{
2795 *request = &hba->dev_cmd.query.request;
2796 *response = &hba->dev_cmd.query.response;
2797 memset(*request, 0, sizeof(struct ufs_query_req));
2798 memset(*response, 0, sizeof(struct ufs_query_res));
2799 (*request)->upiu_req.opcode = opcode;
2800 (*request)->upiu_req.idn = idn;
2801 (*request)->upiu_req.index = index;
2802 (*request)->upiu_req.selector = selector;
2803}
2804
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002805static int ufshcd_query_flag_retry(struct ufs_hba *hba,
Stanley Chu1f34eed2020-05-08 16:01:12 +08002806 enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002807{
2808 int ret;
2809 int retries;
2810
2811 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
Stanley Chu1f34eed2020-05-08 16:01:12 +08002812 ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res);
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002813 if (ret)
2814 dev_dbg(hba->dev,
2815 "%s: failed with error %d, retries %d\n",
2816 __func__, ret, retries);
2817 else
2818 break;
2819 }
2820
2821 if (ret)
2822 dev_err(hba->dev,
2823 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2824 __func__, opcode, idn, ret, retries);
2825 return ret;
2826}
2827
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002828/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302829 * ufshcd_query_flag() - API function for sending flag query requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002830 * @hba: per-adapter instance
2831 * @opcode: flag query to perform
2832 * @idn: flag idn to access
Stanley Chu1f34eed2020-05-08 16:01:12 +08002833 * @index: flag index to access
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002834 * @flag_res: the flag value after the query request completes
Dolev Raviv68078d52013-07-30 00:35:58 +05302835 *
2836 * Returns 0 for success, non-zero in case of failure
2837 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002838int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Stanley Chu1f34eed2020-05-08 16:01:12 +08002839 enum flag_idn idn, u8 index, bool *flag_res)
Dolev Raviv68078d52013-07-30 00:35:58 +05302840{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002841 struct ufs_query_req *request = NULL;
2842 struct ufs_query_res *response = NULL;
Stanley Chu1f34eed2020-05-08 16:01:12 +08002843 int err, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002844 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302845
2846 BUG_ON(!hba);
2847
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002848 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302849 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002850 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2851 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302852
2853 switch (opcode) {
2854 case UPIU_QUERY_OPCODE_SET_FLAG:
2855 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2856 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2857 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2858 break;
2859 case UPIU_QUERY_OPCODE_READ_FLAG:
2860 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2861 if (!flag_res) {
2862 /* No dummy reads */
2863 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2864 __func__);
2865 err = -EINVAL;
2866 goto out_unlock;
2867 }
2868 break;
2869 default:
2870 dev_err(hba->dev,
2871 "%s: Expected query flag opcode but got = %d\n",
2872 __func__, opcode);
2873 err = -EINVAL;
2874 goto out_unlock;
2875 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302876
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002877 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05302878
2879 if (err) {
2880 dev_err(hba->dev,
2881 "%s: Sending flag query for idn %d failed, err = %d\n",
2882 __func__, idn, err);
2883 goto out_unlock;
2884 }
2885
2886 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302887 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302888 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2889
2890out_unlock:
2891 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002892 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05302893 return err;
2894}
2895
2896/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302897 * ufshcd_query_attr - API function for sending attribute requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002898 * @hba: per-adapter instance
2899 * @opcode: attribute opcode
2900 * @idn: attribute idn to access
2901 * @index: index field
2902 * @selector: selector field
2903 * @attr_val: the attribute value after the query request completes
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302904 *
2905 * Returns 0 for success, non-zero in case of failure
2906*/
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02002907int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
2908 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302909{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002910 struct ufs_query_req *request = NULL;
2911 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302912 int err;
2913
2914 BUG_ON(!hba);
2915
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002916 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302917 if (!attr_val) {
2918 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2919 __func__, opcode);
2920 err = -EINVAL;
2921 goto out;
2922 }
2923
2924 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002925 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2926 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302927
2928 switch (opcode) {
2929 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2930 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302931 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302932 break;
2933 case UPIU_QUERY_OPCODE_READ_ATTR:
2934 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2935 break;
2936 default:
2937 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2938 __func__, opcode);
2939 err = -EINVAL;
2940 goto out_unlock;
2941 }
2942
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002943 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302944
2945 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002946 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2947 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302948 goto out_unlock;
2949 }
2950
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302951 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302952
2953out_unlock:
2954 mutex_unlock(&hba->dev_cmd.lock);
2955out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002956 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302957 return err;
2958}
2959
2960/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002961 * ufshcd_query_attr_retry() - API function for sending query
2962 * attribute with retries
2963 * @hba: per-adapter instance
2964 * @opcode: attribute opcode
2965 * @idn: attribute idn to access
2966 * @index: index field
2967 * @selector: selector field
2968 * @attr_val: the attribute value after the query request
2969 * completes
2970 *
2971 * Returns 0 for success, non-zero in case of failure
2972*/
2973static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2974 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2975 u32 *attr_val)
2976{
2977 int ret = 0;
2978 u32 retries;
2979
Bart Van Assche68c9fcf2019-12-24 14:02:43 -08002980 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002981 ret = ufshcd_query_attr(hba, opcode, idn, index,
2982 selector, attr_val);
2983 if (ret)
2984 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2985 __func__, ret, retries);
2986 else
2987 break;
2988 }
2989
2990 if (ret)
2991 dev_err(hba->dev,
2992 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2993 __func__, idn, ret, QUERY_REQ_RETRIES);
2994 return ret;
2995}
2996
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002997static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002998 enum query_opcode opcode, enum desc_idn idn, u8 index,
2999 u8 selector, u8 *desc_buf, int *buf_len)
3000{
3001 struct ufs_query_req *request = NULL;
3002 struct ufs_query_res *response = NULL;
3003 int err;
3004
3005 BUG_ON(!hba);
3006
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003007 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003008 if (!desc_buf) {
3009 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3010 __func__, opcode);
3011 err = -EINVAL;
3012 goto out;
3013 }
3014
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003015 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003016 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3017 __func__, *buf_len);
3018 err = -EINVAL;
3019 goto out;
3020 }
3021
3022 mutex_lock(&hba->dev_cmd.lock);
3023 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3024 selector);
3025 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003026 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003027
3028 switch (opcode) {
3029 case UPIU_QUERY_OPCODE_WRITE_DESC:
3030 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3031 break;
3032 case UPIU_QUERY_OPCODE_READ_DESC:
3033 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3034 break;
3035 default:
3036 dev_err(hba->dev,
3037 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3038 __func__, opcode);
3039 err = -EINVAL;
3040 goto out_unlock;
3041 }
3042
3043 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3044
3045 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08003046 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3047 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003048 goto out_unlock;
3049 }
3050
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003051 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003052
3053out_unlock:
Bean Huocfcbae32019-11-12 23:34:36 +01003054 hba->dev_cmd.query.descriptor = NULL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003055 mutex_unlock(&hba->dev_cmd.lock);
3056out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003057 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003058 return err;
3059}
3060
3061/**
Bart Van Assche8aa29f12018-03-01 15:07:20 -08003062 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
3063 * @hba: per-adapter instance
3064 * @opcode: attribute opcode
3065 * @idn: attribute idn to access
3066 * @index: index field
3067 * @selector: selector field
3068 * @desc_buf: the buffer that contains the descriptor
3069 * @buf_len: length parameter passed to the device
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003070 *
3071 * Returns 0 for success, non-zero in case of failure.
3072 * The buf_len parameter will contain, on return, the length parameter
3073 * received on the response.
3074 */
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02003075int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
3076 enum query_opcode opcode,
3077 enum desc_idn idn, u8 index,
3078 u8 selector,
3079 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003080{
3081 int err;
3082 int retries;
3083
3084 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3085 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3086 selector, desc_buf, buf_len);
3087 if (!err || err == -EINVAL)
3088 break;
3089 }
3090
3091 return err;
3092}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003093
3094/**
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003095 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
3096 * @hba: Pointer to adapter instance
3097 * @desc_id: descriptor idn value
3098 * @desc_len: mapped desc length (out)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003099 */
Bean Huo7a0bf852020-06-03 11:19:58 +02003100void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
3101 int *desc_len)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003102{
Bean Huo7a0bf852020-06-03 11:19:58 +02003103 if (desc_id >= QUERY_DESC_IDN_MAX || desc_id == QUERY_DESC_IDN_RFU_0 ||
3104 desc_id == QUERY_DESC_IDN_RFU_1)
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003105 *desc_len = 0;
Bean Huo7a0bf852020-06-03 11:19:58 +02003106 else
3107 *desc_len = hba->desc_size[desc_id];
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003108}
3109EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
3110
Bean Huo7a0bf852020-06-03 11:19:58 +02003111static void ufshcd_update_desc_length(struct ufs_hba *hba,
Bean Huo72fb6902020-06-03 11:19:59 +02003112 enum desc_idn desc_id, int desc_index,
Bean Huo7a0bf852020-06-03 11:19:58 +02003113 unsigned char desc_len)
3114{
3115 if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE &&
Bean Huo72fb6902020-06-03 11:19:59 +02003116 desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT)
3117 /* For UFS 3.1, the normal unit descriptor is 10 bytes larger
3118 * than the RPMB unit, however, both descriptors share the same
3119 * desc_idn, to cover both unit descriptors with one length, we
3120 * choose the normal unit descriptor length by desc_index.
3121 */
Bean Huo7a0bf852020-06-03 11:19:58 +02003122 hba->desc_size[desc_id] = desc_len;
3123}
3124
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003125/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003126 * ufshcd_read_desc_param - read the specified descriptor parameter
3127 * @hba: Pointer to adapter instance
3128 * @desc_id: descriptor idn value
3129 * @desc_index: descriptor index
3130 * @param_offset: offset of the parameter to read
3131 * @param_read_buf: pointer to buffer where parameter would be read
3132 * @param_size: sizeof(param_read_buf)
3133 *
3134 * Return 0 in case of success, non-zero otherwise
3135 */
Stanislav Nijnikov45bced82018-02-15 14:14:02 +02003136int ufshcd_read_desc_param(struct ufs_hba *hba,
3137 enum desc_idn desc_id,
3138 int desc_index,
3139 u8 param_offset,
3140 u8 *param_read_buf,
3141 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003142{
3143 int ret;
3144 u8 *desc_buf;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003145 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003146 bool is_kmalloc = true;
3147
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003148 /* Safety check */
3149 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003150 return -EINVAL;
3151
Bean Huo7a0bf852020-06-03 11:19:58 +02003152 /* Get the length of descriptor */
3153 ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
3154 if (!buff_len) {
3155 dev_err(hba->dev, "%s: Failed to get desc length", __func__);
3156 return -EINVAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003157 }
3158
3159 /* Check whether we need temp memory */
3160 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003161 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3162 if (!desc_buf)
3163 return -ENOMEM;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003164 } else {
3165 desc_buf = param_read_buf;
3166 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003167 }
3168
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003169 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003170 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003171 desc_id, desc_index, 0,
3172 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003173
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003174 if (ret) {
3175 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3176 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003177 goto out;
3178 }
3179
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003180 /* Sanity check */
3181 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3182 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3183 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3184 ret = -EINVAL;
3185 goto out;
3186 }
3187
Bean Huo7a0bf852020-06-03 11:19:58 +02003188 /* Update descriptor length */
3189 buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET];
Bean Huo72fb6902020-06-03 11:19:59 +02003190 ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len);
Bean Huo7a0bf852020-06-03 11:19:58 +02003191
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003192 /* Check wherher we will not copy more data, than available */
Bean Huocbe193f2020-06-03 11:19:57 +02003193 if (is_kmalloc && (param_offset + param_size) > buff_len)
3194 param_size = buff_len - param_offset;
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003195
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003196 if (is_kmalloc)
3197 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3198out:
3199 if (is_kmalloc)
3200 kfree(desc_buf);
3201 return ret;
3202}
3203
Yaniv Gardib573d482016-03-10 17:37:09 +02003204/**
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003205 * struct uc_string_id - unicode string
3206 *
3207 * @len: size of this descriptor inclusive
3208 * @type: descriptor type
3209 * @uc: unicode string character
3210 */
3211struct uc_string_id {
3212 u8 len;
3213 u8 type;
Gustavo A. R. Silvaec38c0a2020-05-07 14:25:50 -05003214 wchar_t uc[];
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003215} __packed;
3216
3217/* replace non-printable or non-ASCII characters with spaces */
3218static inline char ufshcd_remove_non_printable(u8 ch)
3219{
3220 return (ch >= 0x20 && ch <= 0x7e) ? ch : ' ';
3221}
3222
3223/**
Yaniv Gardib573d482016-03-10 17:37:09 +02003224 * ufshcd_read_string_desc - read string descriptor
3225 * @hba: pointer to adapter instance
3226 * @desc_index: descriptor index
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003227 * @buf: pointer to buffer where descriptor would be read,
3228 * the caller should free the memory.
Yaniv Gardib573d482016-03-10 17:37:09 +02003229 * @ascii: if true convert from unicode to ascii characters
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003230 * null terminated string.
Yaniv Gardib573d482016-03-10 17:37:09 +02003231 *
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003232 * Return:
3233 * * string size on success.
3234 * * -ENOMEM: on allocation failure
3235 * * -EINVAL: on a wrong parameter
Yaniv Gardib573d482016-03-10 17:37:09 +02003236 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003237int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
3238 u8 **buf, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003239{
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003240 struct uc_string_id *uc_str;
3241 u8 *str;
3242 int ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003243
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003244 if (!buf)
3245 return -EINVAL;
Yaniv Gardib573d482016-03-10 17:37:09 +02003246
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003247 uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
3248 if (!uc_str)
3249 return -ENOMEM;
3250
Bean Huoc4607a02020-06-03 11:19:56 +02003251 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0,
3252 (u8 *)uc_str, QUERY_DESC_MAX_SIZE);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003253 if (ret < 0) {
3254 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
3255 QUERY_REQ_RETRIES, ret);
3256 str = NULL;
3257 goto out;
3258 }
3259
3260 if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
3261 dev_dbg(hba->dev, "String Desc is of zero length\n");
3262 str = NULL;
3263 ret = 0;
Yaniv Gardib573d482016-03-10 17:37:09 +02003264 goto out;
3265 }
3266
3267 if (ascii) {
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003268 ssize_t ascii_len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003269 int i;
Yaniv Gardib573d482016-03-10 17:37:09 +02003270 /* remove header and divide by 2 to move from UTF16 to UTF8 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003271 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3272 str = kzalloc(ascii_len, GFP_KERNEL);
3273 if (!str) {
3274 ret = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003275 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003276 }
3277
3278 /*
3279 * the descriptor contains string in UTF16 format
3280 * we need to convert to utf-8 so it can be displayed
3281 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003282 ret = utf16s_to_utf8s(uc_str->uc,
3283 uc_str->len - QUERY_DESC_HDR_SIZE,
3284 UTF16_BIG_ENDIAN, str, ascii_len);
Yaniv Gardib573d482016-03-10 17:37:09 +02003285
3286 /* replace non-printable or non-ASCII characters with spaces */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003287 for (i = 0; i < ret; i++)
3288 str[i] = ufshcd_remove_non_printable(str[i]);
Yaniv Gardib573d482016-03-10 17:37:09 +02003289
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003290 str[ret++] = '\0';
3291
3292 } else {
YueHaibing5f577042019-08-31 12:44:24 +00003293 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003294 if (!str) {
3295 ret = -ENOMEM;
3296 goto out;
3297 }
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003298 ret = uc_str->len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003299 }
3300out:
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003301 *buf = str;
3302 kfree(uc_str);
3303 return ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003304}
Yaniv Gardib573d482016-03-10 17:37:09 +02003305
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003306/**
3307 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3308 * @hba: Pointer to adapter instance
3309 * @lun: lun id
3310 * @param_offset: offset of the parameter to read
3311 * @param_read_buf: pointer to buffer where parameter would be read
3312 * @param_size: sizeof(param_read_buf)
3313 *
3314 * Return 0 in case of success, non-zero otherwise
3315 */
3316static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3317 int lun,
3318 enum unit_desc_param param_offset,
3319 u8 *param_read_buf,
3320 u32 param_size)
3321{
3322 /*
3323 * Unit descriptors are only available for general purpose LUs (LUN id
3324 * from 0 to 7) and RPMB Well known LU.
3325 */
Bean Huo1baa8012020-01-20 14:08:20 +01003326 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003327 return -EOPNOTSUPP;
3328
3329 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3330 param_offset, param_read_buf, param_size);
3331}
3332
Can Guo09f17792020-02-10 19:40:49 -08003333static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba)
3334{
3335 int err = 0;
3336 u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3337
3338 if (hba->dev_info.wspecversion >= 0x300) {
3339 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3340 QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0,
3341 &gating_wait);
3342 if (err)
3343 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n",
3344 err, gating_wait);
3345
3346 if (gating_wait == 0) {
3347 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3348 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n",
3349 gating_wait);
3350 }
3351
3352 hba->dev_info.clk_gating_wait_us = gating_wait;
3353 }
3354
3355 return err;
3356}
3357
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003358/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303359 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3360 * @hba: per adapter instance
3361 *
3362 * 1. Allocate DMA memory for Command Descriptor array
3363 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3364 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3365 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3366 * (UTMRDL)
3367 * 4. Allocate memory for local reference block(lrb).
3368 *
3369 * Returns 0 for success, non-zero in case of failure
3370 */
3371static int ufshcd_memory_alloc(struct ufs_hba *hba)
3372{
3373 size_t utmrdl_size, utrdl_size, ucdl_size;
3374
3375 /* Allocate memory for UTP command descriptors */
3376 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003377 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3378 ucdl_size,
3379 &hba->ucdl_dma_addr,
3380 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303381
3382 /*
3383 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3384 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3385 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3386 * be aligned to 128 bytes as well
3387 */
3388 if (!hba->ucdl_base_addr ||
3389 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303390 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303391 "Command Descriptor Memory allocation failed\n");
3392 goto out;
3393 }
3394
3395 /*
3396 * Allocate memory for UTP Transfer descriptors
3397 * UFSHCI requires 1024 byte alignment of UTRD
3398 */
3399 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003400 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3401 utrdl_size,
3402 &hba->utrdl_dma_addr,
3403 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303404 if (!hba->utrdl_base_addr ||
3405 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303406 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303407 "Transfer Descriptor Memory allocation failed\n");
3408 goto out;
3409 }
3410
3411 /*
3412 * Allocate memory for UTP Task Management descriptors
3413 * UFSHCI requires 1024 byte alignment of UTMRD
3414 */
3415 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003416 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3417 utmrdl_size,
3418 &hba->utmrdl_dma_addr,
3419 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303420 if (!hba->utmrdl_base_addr ||
3421 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303422 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303423 "Task Management Descriptor Memory allocation failed\n");
3424 goto out;
3425 }
3426
3427 /* Allocate memory for local reference block */
Kees Cooka86854d2018-06-12 14:07:58 -07003428 hba->lrb = devm_kcalloc(hba->dev,
3429 hba->nutrs, sizeof(struct ufshcd_lrb),
Seungwon Jeon2953f852013-06-27 13:31:54 +09003430 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303431 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303432 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303433 goto out;
3434 }
3435 return 0;
3436out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303437 return -ENOMEM;
3438}
3439
3440/**
3441 * ufshcd_host_memory_configure - configure local reference block with
3442 * memory offsets
3443 * @hba: per adapter instance
3444 *
3445 * Configure Host memory space
3446 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3447 * address.
3448 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3449 * and PRDT offset.
3450 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3451 * into local reference block.
3452 */
3453static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3454{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303455 struct utp_transfer_req_desc *utrdlp;
3456 dma_addr_t cmd_desc_dma_addr;
3457 dma_addr_t cmd_desc_element_addr;
3458 u16 response_offset;
3459 u16 prdt_offset;
3460 int cmd_desc_size;
3461 int i;
3462
3463 utrdlp = hba->utrdl_base_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303464
3465 response_offset =
3466 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3467 prdt_offset =
3468 offsetof(struct utp_transfer_cmd_desc, prd_table);
3469
3470 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3471 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3472
3473 for (i = 0; i < hba->nutrs; i++) {
3474 /* Configure UTRD with command descriptor base address */
3475 cmd_desc_element_addr =
3476 (cmd_desc_dma_addr + (cmd_desc_size * i));
3477 utrdlp[i].command_desc_base_addr_lo =
3478 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3479 utrdlp[i].command_desc_base_addr_hi =
3480 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3481
3482 /* Response upiu and prdt offset should be in double words */
Alim Akhtar26f968d2020-05-28 06:46:52 +05303483 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3484 utrdlp[i].response_upiu_offset =
3485 cpu_to_le16(response_offset);
3486 utrdlp[i].prd_table_offset =
3487 cpu_to_le16(prdt_offset);
3488 utrdlp[i].response_upiu_length =
3489 cpu_to_le16(ALIGNED_UPIU_SIZE);
3490 } else {
3491 utrdlp[i].response_upiu_offset =
3492 cpu_to_le16(response_offset >> 2);
3493 utrdlp[i].prd_table_offset =
3494 cpu_to_le16(prdt_offset >> 2);
3495 utrdlp[i].response_upiu_length =
3496 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
3497 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303498
Bart Van Assche4d2b8d42020-01-22 19:56:35 -08003499 ufshcd_init_lrb(hba, &hba->lrb[i], i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303500 }
3501}
3502
3503/**
3504 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3505 * @hba: per adapter instance
3506 *
3507 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3508 * in order to initialize the Unipro link startup procedure.
3509 * Once the Unipro links are up, the device connected to the controller
3510 * is detected.
3511 *
3512 * Returns 0 on success, non-zero value on failure
3513 */
3514static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3515{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303516 struct uic_command uic_cmd = {0};
3517 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303518
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303519 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3520
3521 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3522 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003523 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303524 "dme-link-startup: error code %d\n", ret);
3525 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303526}
Alim Akhtar39bf2d82020-05-28 06:46:51 +05303527/**
3528 * ufshcd_dme_reset - UIC command for DME_RESET
3529 * @hba: per adapter instance
3530 *
3531 * DME_RESET command is issued in order to reset UniPro stack.
3532 * This function now deals with cold reset.
3533 *
3534 * Returns 0 on success, non-zero value on failure
3535 */
3536static int ufshcd_dme_reset(struct ufs_hba *hba)
3537{
3538 struct uic_command uic_cmd = {0};
3539 int ret;
3540
3541 uic_cmd.command = UIC_CMD_DME_RESET;
3542
3543 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3544 if (ret)
3545 dev_err(hba->dev,
3546 "dme-reset: error code %d\n", ret);
3547
3548 return ret;
3549}
3550
3551/**
3552 * ufshcd_dme_enable - UIC command for DME_ENABLE
3553 * @hba: per adapter instance
3554 *
3555 * DME_ENABLE command is issued in order to enable UniPro stack.
3556 *
3557 * Returns 0 on success, non-zero value on failure
3558 */
3559static int ufshcd_dme_enable(struct ufs_hba *hba)
3560{
3561 struct uic_command uic_cmd = {0};
3562 int ret;
3563
3564 uic_cmd.command = UIC_CMD_DME_ENABLE;
3565
3566 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3567 if (ret)
3568 dev_err(hba->dev,
3569 "dme-reset: error code %d\n", ret);
3570
3571 return ret;
3572}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303573
Yaniv Gardicad2e032015-03-31 17:37:14 +03003574static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3575{
3576 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3577 unsigned long min_sleep_time_us;
3578
3579 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3580 return;
3581
3582 /*
3583 * last_dme_cmd_tstamp will be 0 only for 1st call to
3584 * this function
3585 */
3586 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3587 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3588 } else {
3589 unsigned long delta =
3590 (unsigned long) ktime_to_us(
3591 ktime_sub(ktime_get(),
3592 hba->last_dme_cmd_tstamp));
3593
3594 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3595 min_sleep_time_us =
3596 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3597 else
3598 return; /* no more delay required */
3599 }
3600
3601 /* allow sleep for extra 50us if needed */
3602 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3603}
3604
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303605/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303606 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3607 * @hba: per adapter instance
3608 * @attr_sel: uic command argument1
3609 * @attr_set: attribute set type as uic command argument2
3610 * @mib_val: setting value as uic command argument3
3611 * @peer: indicate whether peer or local
3612 *
3613 * Returns 0 on success, non-zero value on failure
3614 */
3615int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3616 u8 attr_set, u32 mib_val, u8 peer)
3617{
3618 struct uic_command uic_cmd = {0};
3619 static const char *const action[] = {
3620 "dme-set",
3621 "dme-peer-set"
3622 };
3623 const char *set = action[!!peer];
3624 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003625 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303626
3627 uic_cmd.command = peer ?
3628 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3629 uic_cmd.argument1 = attr_sel;
3630 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3631 uic_cmd.argument3 = mib_val;
3632
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003633 do {
3634 /* for peer attributes we retry upon failure */
3635 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3636 if (ret)
3637 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3638 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3639 } while (ret && peer && --retries);
3640
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003641 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003642 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003643 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3644 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303645
3646 return ret;
3647}
3648EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3649
3650/**
3651 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3652 * @hba: per adapter instance
3653 * @attr_sel: uic command argument1
3654 * @mib_val: the value of the attribute as returned by the UIC command
3655 * @peer: indicate whether peer or local
3656 *
3657 * Returns 0 on success, non-zero value on failure
3658 */
3659int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3660 u32 *mib_val, u8 peer)
3661{
3662 struct uic_command uic_cmd = {0};
3663 static const char *const action[] = {
3664 "dme-get",
3665 "dme-peer-get"
3666 };
3667 const char *get = action[!!peer];
3668 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003669 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003670 struct ufs_pa_layer_attr orig_pwr_info;
3671 struct ufs_pa_layer_attr temp_pwr_info;
3672 bool pwr_mode_change = false;
3673
3674 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3675 orig_pwr_info = hba->pwr_info;
3676 temp_pwr_info = orig_pwr_info;
3677
3678 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3679 orig_pwr_info.pwr_rx == FAST_MODE) {
3680 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3681 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3682 pwr_mode_change = true;
3683 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3684 orig_pwr_info.pwr_rx == SLOW_MODE) {
3685 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3686 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3687 pwr_mode_change = true;
3688 }
3689 if (pwr_mode_change) {
3690 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3691 if (ret)
3692 goto out;
3693 }
3694 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303695
3696 uic_cmd.command = peer ?
3697 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3698 uic_cmd.argument1 = attr_sel;
3699
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003700 do {
3701 /* for peer attributes we retry upon failure */
3702 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3703 if (ret)
3704 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3705 get, UIC_GET_ATTR_ID(attr_sel), ret);
3706 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303707
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003708 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003709 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003710 get, UIC_GET_ATTR_ID(attr_sel),
3711 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003712
3713 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303714 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003715
3716 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3717 && pwr_mode_change)
3718 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303719out:
3720 return ret;
3721}
3722EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3723
3724/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003725 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3726 * state) and waits for it to take effect.
3727 *
3728 * @hba: per adapter instance
3729 * @cmd: UIC command to execute
3730 *
3731 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3732 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3733 * and device UniPro link and hence it's final completion would be indicated by
3734 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3735 * addition to normal UIC command completion Status (UCCS). This function only
3736 * returns after the relevant status bits indicate the completion.
3737 *
3738 * Returns 0 on success, non-zero value on failure
3739 */
3740static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3741{
3742 struct completion uic_async_done;
3743 unsigned long flags;
3744 u8 status;
3745 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003746 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003747
3748 mutex_lock(&hba->uic_cmd_mutex);
3749 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003750 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003751
3752 spin_lock_irqsave(hba->host->host_lock, flags);
3753 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003754 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3755 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3756 /*
3757 * Make sure UIC command completion interrupt is disabled before
3758 * issuing UIC command.
3759 */
3760 wmb();
3761 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003762 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003763 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3764 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003765 if (ret) {
3766 dev_err(hba->dev,
3767 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3768 cmd->command, cmd->argument3, ret);
3769 goto out;
3770 }
3771
3772 if (!wait_for_completion_timeout(hba->uic_async_done,
3773 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3774 dev_err(hba->dev,
3775 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3776 cmd->command, cmd->argument3);
3777 ret = -ETIMEDOUT;
3778 goto out;
3779 }
3780
3781 status = ufshcd_get_upmcrs(hba);
3782 if (status != PWR_LOCAL) {
3783 dev_err(hba->dev,
Zang Leigang479da362017-09-19 16:50:30 +08003784 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003785 cmd->command, status);
3786 ret = (status != PWR_OK) ? status : -1;
3787 }
3788out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003789 if (ret) {
3790 ufshcd_print_host_state(hba);
3791 ufshcd_print_pwr_info(hba);
3792 ufshcd_print_host_regs(hba);
3793 }
3794
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003795 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003796 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003797 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003798 if (reenable_intr)
3799 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003800 spin_unlock_irqrestore(hba->host->host_lock, flags);
3801 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003802
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003803 return ret;
3804}
3805
3806/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303807 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3808 * using DME_SET primitives.
3809 * @hba: per adapter instance
3810 * @mode: powr mode value
3811 *
3812 * Returns 0 on success, non-zero value on failure
3813 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303814static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303815{
3816 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003817 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303818
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003819 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3820 ret = ufshcd_dme_set(hba,
3821 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3822 if (ret) {
3823 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3824 __func__, ret);
3825 goto out;
3826 }
3827 }
3828
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303829 uic_cmd.command = UIC_CMD_DME_SET;
3830 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3831 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003832 ufshcd_hold(hba, false);
3833 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3834 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303835
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003836out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003837 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003838}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303839
Stanley Chu087c5ef2020-03-27 17:53:28 +08003840int ufshcd_link_recovery(struct ufs_hba *hba)
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003841{
3842 int ret;
3843 unsigned long flags;
3844
3845 spin_lock_irqsave(hba->host->host_lock, flags);
3846 hba->ufshcd_state = UFSHCD_STATE_RESET;
3847 ufshcd_set_eh_in_progress(hba);
3848 spin_unlock_irqrestore(hba->host->host_lock, flags);
3849
Can Guoebdd1df2019-11-14 22:09:24 -08003850 /* Reset the attached device */
3851 ufshcd_vops_device_reset(hba);
3852
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003853 ret = ufshcd_host_reset_and_restore(hba);
3854
3855 spin_lock_irqsave(hba->host->host_lock, flags);
3856 if (ret)
3857 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3858 ufshcd_clear_eh_in_progress(hba);
3859 spin_unlock_irqrestore(hba->host->host_lock, flags);
3860
3861 if (ret)
3862 dev_err(hba->dev, "%s: link recovery failed, err %d",
3863 __func__, ret);
3864
3865 return ret;
3866}
Stanley Chu087c5ef2020-03-27 17:53:28 +08003867EXPORT_SYMBOL_GPL(ufshcd_link_recovery);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003868
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003869static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003870{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003871 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003872 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003873 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003874
Kiwoong Kimee32c902016-11-10 21:17:43 +09003875 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3876
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003877 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003878 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003879 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
3880 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003881
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003882 if (ret) {
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003883 int err;
3884
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003885 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3886 __func__, ret);
3887
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003888 /*
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003889 * If link recovery fails then return error code returned from
3890 * ufshcd_link_recovery().
3891 * If link recovery succeeds then return -EAGAIN to attempt
3892 * hibern8 enter retry again.
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003893 */
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003894 err = ufshcd_link_recovery(hba);
3895 if (err) {
3896 dev_err(hba->dev, "%s: link recovery failed", __func__);
3897 ret = err;
3898 } else {
3899 ret = -EAGAIN;
3900 }
Kiwoong Kimee32c902016-11-10 21:17:43 +09003901 } else
3902 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3903 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003904
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003905 return ret;
3906}
3907
3908static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3909{
3910 int ret = 0, retries;
3911
3912 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
3913 ret = __ufshcd_uic_hibern8_enter(hba);
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003914 if (!ret)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003915 goto out;
3916 }
3917out:
3918 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003919}
3920
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08003921int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003922{
3923 struct uic_command uic_cmd = {0};
3924 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003925 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003926
Kiwoong Kimee32c902016-11-10 21:17:43 +09003927 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3928
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003929 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3930 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003931 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
3932 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3933
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303934 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003935 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3936 __func__, ret);
3937 ret = ufshcd_link_recovery(hba);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003938 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09003939 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3940 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003941 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
3942 hba->ufs_stats.hibern8_exit_cnt++;
3943 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303944
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303945 return ret;
3946}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08003947EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303948
Stanley Chuba7af5e2019-12-30 13:32:28 +08003949void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit)
3950{
3951 unsigned long flags;
Can Guobe7594a2020-03-05 00:53:07 -08003952 bool update = false;
Stanley Chuba7af5e2019-12-30 13:32:28 +08003953
Can Guobe7594a2020-03-05 00:53:07 -08003954 if (!ufshcd_is_auto_hibern8_supported(hba))
Stanley Chuba7af5e2019-12-30 13:32:28 +08003955 return;
3956
3957 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08003958 if (hba->ahit != ahit) {
3959 hba->ahit = ahit;
3960 update = true;
3961 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08003962 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08003963
3964 if (update && !pm_runtime_suspended(hba->dev)) {
3965 pm_runtime_get_sync(hba->dev);
3966 ufshcd_hold(hba, false);
3967 ufshcd_auto_hibern8_enable(hba);
3968 ufshcd_release(hba);
3969 pm_runtime_put(hba->dev);
3970 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08003971}
3972EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
3973
Can Guo71d848b2019-11-14 22:09:26 -08003974void ufshcd_auto_hibern8_enable(struct ufs_hba *hba)
Adrian Hunterad448372018-03-20 15:07:38 +02003975{
3976 unsigned long flags;
3977
Stanley Chuee5f1042019-05-21 14:44:52 +08003978 if (!ufshcd_is_auto_hibern8_supported(hba) || !hba->ahit)
Adrian Hunterad448372018-03-20 15:07:38 +02003979 return;
3980
3981 spin_lock_irqsave(hba->host->host_lock, flags);
3982 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
3983 spin_unlock_irqrestore(hba->host->host_lock, flags);
3984}
3985
Yaniv Gardi50646362014-10-23 13:25:13 +03003986 /**
3987 * ufshcd_init_pwr_info - setting the POR (power on reset)
3988 * values in hba power info
3989 * @hba: per-adapter instance
3990 */
3991static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3992{
3993 hba->pwr_info.gear_rx = UFS_PWM_G1;
3994 hba->pwr_info.gear_tx = UFS_PWM_G1;
3995 hba->pwr_info.lane_rx = 1;
3996 hba->pwr_info.lane_tx = 1;
3997 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3998 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3999 hba->pwr_info.hs_rate = 0;
4000}
4001
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304002/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004003 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4004 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304005 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004006static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304007{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004008 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4009
4010 if (hba->max_pwr_info.is_valid)
4011 return 0;
4012
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004013 pwr_info->pwr_tx = FAST_MODE;
4014 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004015 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304016
4017 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004018 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4019 &pwr_info->lane_rx);
4020 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4021 &pwr_info->lane_tx);
4022
4023 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4024 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4025 __func__,
4026 pwr_info->lane_rx,
4027 pwr_info->lane_tx);
4028 return -EINVAL;
4029 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304030
4031 /*
4032 * First, get the maximum gears of HS speed.
4033 * If a zero value, it means there is no HSGEAR capability.
4034 * Then, get the maximum gears of PWM speed.
4035 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004036 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4037 if (!pwr_info->gear_rx) {
4038 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4039 &pwr_info->gear_rx);
4040 if (!pwr_info->gear_rx) {
4041 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4042 __func__, pwr_info->gear_rx);
4043 return -EINVAL;
4044 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004045 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304046 }
4047
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004048 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4049 &pwr_info->gear_tx);
4050 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304051 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004052 &pwr_info->gear_tx);
4053 if (!pwr_info->gear_tx) {
4054 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4055 __func__, pwr_info->gear_tx);
4056 return -EINVAL;
4057 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08004058 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004059 }
4060
4061 hba->max_pwr_info.is_valid = true;
4062 return 0;
4063}
4064
4065static int ufshcd_change_power_mode(struct ufs_hba *hba,
4066 struct ufs_pa_layer_attr *pwr_mode)
4067{
4068 int ret;
4069
4070 /* if already configured to the requested pwr_mode */
4071 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4072 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4073 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4074 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4075 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4076 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4077 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4078 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4079 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304080 }
4081
4082 /*
4083 * Configure attributes for power mode change with below.
4084 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4085 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4086 * - PA_HSSERIES
4087 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004088 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4089 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4090 pwr_mode->lane_rx);
4091 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4092 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304093 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004094 else
4095 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304096
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004097 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4098 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4099 pwr_mode->lane_tx);
4100 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4101 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304102 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004103 else
4104 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304105
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004106 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4107 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4108 pwr_mode->pwr_rx == FAST_MODE ||
4109 pwr_mode->pwr_tx == FAST_MODE)
4110 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4111 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304112
Can Guo08342532019-12-05 02:14:42 +00004113 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4114 DL_FC0ProtectionTimeOutVal_Default);
4115 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4116 DL_TC0ReplayTimeOutVal_Default);
4117 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4118 DL_AFC0ReqTimeOutVal_Default);
4119 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
4120 DL_FC1ProtectionTimeOutVal_Default);
4121 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
4122 DL_TC1ReplayTimeOutVal_Default);
4123 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
4124 DL_AFC1ReqTimeOutVal_Default);
4125
4126 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4127 DL_FC0ProtectionTimeOutVal_Default);
4128 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4129 DL_TC0ReplayTimeOutVal_Default);
4130 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4131 DL_AFC0ReqTimeOutVal_Default);
4132
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004133 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4134 | pwr_mode->pwr_tx);
4135
4136 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304137 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004138 "%s: power mode change failed %d\n", __func__, ret);
4139 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004140 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4141 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004142
4143 memcpy(&hba->pwr_info, pwr_mode,
4144 sizeof(struct ufs_pa_layer_attr));
4145 }
4146
4147 return ret;
4148}
4149
4150/**
4151 * ufshcd_config_pwr_mode - configure a new power mode
4152 * @hba: per-adapter instance
4153 * @desired_pwr_mode: desired power configuration
4154 */
Alim Akhtar0d846e72018-05-06 15:44:18 +05304155int ufshcd_config_pwr_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004156 struct ufs_pa_layer_attr *desired_pwr_mode)
4157{
4158 struct ufs_pa_layer_attr final_params = { 0 };
4159 int ret;
4160
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004161 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4162 desired_pwr_mode, &final_params);
4163
4164 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004165 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4166
4167 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304168
4169 return ret;
4170}
Alim Akhtar0d846e72018-05-06 15:44:18 +05304171EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304172
4173/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304174 * ufshcd_complete_dev_init() - checks device readiness
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004175 * @hba: per-adapter instance
Dolev Raviv68078d52013-07-30 00:35:58 +05304176 *
4177 * Set fDeviceInit flag and poll until device toggles it.
4178 */
4179static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4180{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004181 int i;
4182 int err;
Jason Yan7dfdcc32020-04-26 17:43:05 +08004183 bool flag_res = true;
Dolev Raviv68078d52013-07-30 00:35:58 +05304184
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004185 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08004186 QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304187 if (err) {
4188 dev_err(hba->dev,
4189 "%s setting fDeviceInit flag failed with error %d\n",
4190 __func__, err);
4191 goto out;
4192 }
4193
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004194 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
4195 for (i = 0; i < 1000 && !err && flag_res; i++)
4196 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08004197 QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res);
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004198
Dolev Raviv68078d52013-07-30 00:35:58 +05304199 if (err)
4200 dev_err(hba->dev,
4201 "%s reading fDeviceInit flag failed with error %d\n",
4202 __func__, err);
4203 else if (flag_res)
4204 dev_err(hba->dev,
4205 "%s fDeviceInit was not cleared by the device\n",
4206 __func__);
4207
4208out:
4209 return err;
4210}
4211
4212/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304213 * ufshcd_make_hba_operational - Make UFS controller operational
4214 * @hba: per adapter instance
4215 *
4216 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004217 * 1. Enable required interrupts
4218 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004219 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004220 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304221 *
4222 * Returns 0 on success, non-zero value on failure
4223 */
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004224int ufshcd_make_hba_operational(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304225{
4226 int err = 0;
4227 u32 reg;
4228
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304229 /* Enable required interrupts */
4230 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4231
4232 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004233 if (ufshcd_is_intr_aggr_allowed(hba))
4234 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4235 else
4236 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304237
4238 /* Configure UTRL and UTMRL base address registers */
4239 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4240 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4241 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4242 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4243 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4244 REG_UTP_TASK_REQ_LIST_BASE_L);
4245 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4246 REG_UTP_TASK_REQ_LIST_BASE_H);
4247
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304248 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004249 * Make sure base address and interrupt setup are updated before
4250 * enabling the run/stop registers below.
4251 */
4252 wmb();
4253
4254 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304255 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304256 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004257 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304258 if (!(ufshcd_get_lists_status(reg))) {
4259 ufshcd_enable_run_stop_reg(hba);
4260 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304261 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304262 "Host controller not ready to process requests");
4263 err = -EIO;
4264 goto out;
4265 }
4266
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304267out:
4268 return err;
4269}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004270EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304271
4272/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004273 * ufshcd_hba_stop - Send controller to reset state
4274 * @hba: per adapter instance
Yaniv Gardi596585a2016-03-10 17:37:08 +02004275 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07004276static inline void ufshcd_hba_stop(struct ufs_hba *hba)
Yaniv Gardi596585a2016-03-10 17:37:08 +02004277{
Bart Van Assche5cac1092020-05-07 15:27:50 -07004278 unsigned long flags;
Yaniv Gardi596585a2016-03-10 17:37:08 +02004279 int err;
4280
Bart Van Assche5cac1092020-05-07 15:27:50 -07004281 /*
4282 * Obtain the host lock to prevent that the controller is disabled
4283 * while the UFS interrupt handler is active on another CPU.
4284 */
4285 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004286 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
Bart Van Assche5cac1092020-05-07 15:27:50 -07004287 spin_unlock_irqrestore(hba->host->host_lock, flags);
4288
Yaniv Gardi596585a2016-03-10 17:37:08 +02004289 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4290 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
Bart Van Assche5cac1092020-05-07 15:27:50 -07004291 10, 1);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004292 if (err)
4293 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4294}
4295
4296/**
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304297 * ufshcd_hba_execute_hce - initialize the controller
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304298 * @hba: per adapter instance
4299 *
4300 * The controller resets itself and controller firmware initialization
4301 * sequence kicks off. When controller is ready it will set
4302 * the Host Controller Enable bit to 1.
4303 *
4304 * Returns 0 on success, non-zero value on failure
4305 */
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304306static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304307{
4308 int retry;
4309
Yaniv Gardi596585a2016-03-10 17:37:08 +02004310 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304311 /* change controller state to "reset state" */
Bart Van Assche5cac1092020-05-07 15:27:50 -07004312 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304313
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004314 /* UniPro link is disabled at this point */
4315 ufshcd_set_link_off(hba);
4316
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004317 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004318
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304319 /* start controller initialization sequence */
4320 ufshcd_hba_start(hba);
4321
4322 /*
4323 * To initialize a UFS host controller HCE bit must be set to 1.
4324 * During initialization the HCE bit value changes from 1->0->1.
4325 * When the host controller completes initialization sequence
4326 * it sets the value of HCE bit to 1. The same HCE bit is read back
4327 * to check if the controller has completed initialization sequence.
4328 * So without this delay the value HCE = 1, set in the previous
4329 * instruction might be read back.
4330 * This delay can be changed based on the controller.
4331 */
Stanley Chu90b84912020-05-09 17:37:13 +08004332 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304333
4334 /* wait for the host controller to complete initialization */
Stanley Chu9fc305e2020-03-18 18:40:15 +08004335 retry = 50;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304336 while (ufshcd_is_hba_active(hba)) {
4337 if (retry) {
4338 retry--;
4339 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304340 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304341 "Controller enable failed\n");
4342 return -EIO;
4343 }
Stanley Chu9fc305e2020-03-18 18:40:15 +08004344 usleep_range(1000, 1100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304345 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004346
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004347 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004348 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004349
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004350 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004351
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304352 return 0;
4353}
Alim Akhtar39bf2d82020-05-28 06:46:51 +05304354
4355int ufshcd_hba_enable(struct ufs_hba *hba)
4356{
4357 int ret;
4358
4359 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
4360 ufshcd_set_link_off(hba);
4361 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4362
4363 /* enable UIC related interrupts */
4364 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4365 ret = ufshcd_dme_reset(hba);
4366 if (!ret) {
4367 ret = ufshcd_dme_enable(hba);
4368 if (!ret)
4369 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4370 if (ret)
4371 dev_err(hba->dev,
4372 "Host controller enable failed with non-hce\n");
4373 }
4374 } else {
4375 ret = ufshcd_hba_execute_hce(hba);
4376 }
4377
4378 return ret;
4379}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004380EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
4381
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004382static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4383{
Stanley Chuba0320f2020-03-18 18:40:10 +08004384 int tx_lanes = 0, i, err = 0;
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004385
4386 if (!peer)
4387 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4388 &tx_lanes);
4389 else
4390 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4391 &tx_lanes);
4392 for (i = 0; i < tx_lanes; i++) {
4393 if (!peer)
4394 err = ufshcd_dme_set(hba,
4395 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4396 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4397 0);
4398 else
4399 err = ufshcd_dme_peer_set(hba,
4400 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4401 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4402 0);
4403 if (err) {
4404 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4405 __func__, peer, i, err);
4406 break;
4407 }
4408 }
4409
4410 return err;
4411}
4412
4413static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4414{
4415 return ufshcd_disable_tx_lcc(hba, true);
4416}
4417
Stanley Chua5fe372d2020-01-04 22:26:07 +08004418void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist,
4419 u32 reg)
Stanley Chu8808b4e2019-07-10 21:38:21 +08004420{
4421 reg_hist->reg[reg_hist->pos] = reg;
4422 reg_hist->tstamp[reg_hist->pos] = ktime_get();
4423 reg_hist->pos = (reg_hist->pos + 1) % UFS_ERR_REG_HIST_LENGTH;
4424}
Stanley Chua5fe372d2020-01-04 22:26:07 +08004425EXPORT_SYMBOL_GPL(ufshcd_update_reg_hist);
Stanley Chu8808b4e2019-07-10 21:38:21 +08004426
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304427/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304428 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304429 * @hba: per adapter instance
4430 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304431 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304432 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304433static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304434{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304435 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004436 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004437 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304438
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004439 /*
4440 * If UFS device isn't active then we will have to issue link startup
4441 * 2 times to make sure the device state move to active.
4442 */
4443 if (!ufshcd_is_ufs_dev_active(hba))
4444 link_startup_again = true;
4445
4446link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004447 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004448 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304449
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004450 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004451
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004452 /* check if device is detected by inter-connect layer */
4453 if (!ret && !ufshcd_is_device_present(hba)) {
Stanley Chu8808b4e2019-07-10 21:38:21 +08004454 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4455 0);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004456 dev_err(hba->dev, "%s: Device not present\n", __func__);
4457 ret = -ENXIO;
4458 goto out;
4459 }
4460
4461 /*
4462 * DME link lost indication is only received when link is up,
4463 * but we can't be sure if the link is up until link startup
4464 * succeeds. So reset the local Uni-Pro and try again.
4465 */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004466 if (ret && ufshcd_hba_enable(hba)) {
4467 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4468 (u32)ret);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004469 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004470 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004471 } while (ret && retries--);
4472
Stanley Chu8808b4e2019-07-10 21:38:21 +08004473 if (ret) {
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004474 /* failed to get the link up... retire */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004475 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4476 (u32)ret);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304477 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004478 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304479
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004480 if (link_startup_again) {
4481 link_startup_again = false;
4482 retries = DME_LINKSTARTUP_RETRIES;
4483 goto link_startup;
4484 }
4485
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004486 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4487 ufshcd_init_pwr_info(hba);
4488 ufshcd_print_pwr_info(hba);
4489
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004490 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4491 ret = ufshcd_disable_device_tx_lcc(hba);
4492 if (ret)
4493 goto out;
4494 }
4495
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004496 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004497 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4498 if (ret)
4499 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004500
4501 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304502out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004503 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304504 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004505 ufshcd_print_host_state(hba);
4506 ufshcd_print_pwr_info(hba);
4507 ufshcd_print_host_regs(hba);
4508 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304509 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304510}
4511
4512/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304513 * ufshcd_verify_dev_init() - Verify device initialization
4514 * @hba: per-adapter instance
4515 *
4516 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4517 * device Transport Protocol (UTP) layer is ready after a reset.
4518 * If the UTP layer at the device side is not initialized, it may
4519 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4520 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4521 */
4522static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4523{
4524 int err = 0;
4525 int retries;
4526
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004527 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304528 mutex_lock(&hba->dev_cmd.lock);
4529 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4530 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4531 NOP_OUT_TIMEOUT);
4532
4533 if (!err || err == -ETIMEDOUT)
4534 break;
4535
4536 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4537 }
4538 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004539 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304540
4541 if (err)
4542 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4543 return err;
4544}
4545
4546/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004547 * ufshcd_set_queue_depth - set lun queue depth
4548 * @sdev: pointer to SCSI device
4549 *
4550 * Read bLUQueueDepth value and activate scsi tagged command
4551 * queueing. For WLUN, queue depth is set to 1. For best-effort
4552 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4553 * value that host can queue.
4554 */
4555static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4556{
4557 int ret = 0;
4558 u8 lun_qdepth;
4559 struct ufs_hba *hba;
4560
4561 hba = shost_priv(sdev->host);
4562
4563 lun_qdepth = hba->nutrs;
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02004564 ret = ufshcd_read_unit_desc_param(hba,
4565 ufshcd_scsi_to_upiu_lun(sdev->lun),
4566 UNIT_DESC_PARAM_LU_Q_DEPTH,
4567 &lun_qdepth,
4568 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004569
4570 /* Some WLUN doesn't support unit descriptor */
4571 if (ret == -EOPNOTSUPP)
4572 lun_qdepth = 1;
4573 else if (!lun_qdepth)
4574 /* eventually, we can figure out the real queue depth */
4575 lun_qdepth = hba->nutrs;
4576 else
4577 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4578
4579 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4580 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004581 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004582}
4583
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004584/*
4585 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4586 * @hba: per-adapter instance
4587 * @lun: UFS device lun id
4588 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4589 *
4590 * Returns 0 in case of success and b_lu_write_protect status would be returned
4591 * @b_lu_write_protect parameter.
4592 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4593 * Returns -EINVAL in case of invalid parameters passed to this function.
4594 */
4595static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4596 u8 lun,
4597 u8 *b_lu_write_protect)
4598{
4599 int ret;
4600
4601 if (!b_lu_write_protect)
4602 ret = -EINVAL;
4603 /*
4604 * According to UFS device spec, RPMB LU can't be write
4605 * protected so skip reading bLUWriteProtect parameter for
4606 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4607 */
Bean Huo1baa8012020-01-20 14:08:20 +01004608 else if (lun >= hba->dev_info.max_lu_supported)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004609 ret = -ENOTSUPP;
4610 else
4611 ret = ufshcd_read_unit_desc_param(hba,
4612 lun,
4613 UNIT_DESC_PARAM_LU_WR_PROTECT,
4614 b_lu_write_protect,
4615 sizeof(*b_lu_write_protect));
4616 return ret;
4617}
4618
4619/**
4620 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4621 * status
4622 * @hba: per-adapter instance
4623 * @sdev: pointer to SCSI device
4624 *
4625 */
4626static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4627 struct scsi_device *sdev)
4628{
4629 if (hba->dev_info.f_power_on_wp_en &&
4630 !hba->dev_info.is_lu_power_on_wp) {
4631 u8 b_lu_write_protect;
4632
4633 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4634 &b_lu_write_protect) &&
4635 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4636 hba->dev_info.is_lu_power_on_wp = true;
4637 }
4638}
4639
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004640/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304641 * ufshcd_slave_alloc - handle initial SCSI device configurations
4642 * @sdev: pointer to SCSI device
4643 *
4644 * Returns success
4645 */
4646static int ufshcd_slave_alloc(struct scsi_device *sdev)
4647{
4648 struct ufs_hba *hba;
4649
4650 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304651
4652 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4653 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304654
Can Guoa3a76392019-12-05 02:14:30 +00004655 /* DBD field should be set to 1 in mode sense(10) */
4656 sdev->set_dbd_for_ms = 1;
4657
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304658 /* allow SCSI layer to restart the device in case of errors */
4659 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004660
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004661 /* REPORT SUPPORTED OPERATION CODES is not supported */
4662 sdev->no_report_opcodes = 1;
4663
Sujit Reddy Thumma84af7e82018-01-24 09:52:35 +05304664 /* WRITE_SAME command is not supported */
4665 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004666
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004667 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004668
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004669 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4670
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004671 return 0;
4672}
4673
4674/**
4675 * ufshcd_change_queue_depth - change queue depth
4676 * @sdev: pointer to SCSI device
4677 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004678 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004679 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004680 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004681static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004682{
4683 struct ufs_hba *hba = shost_priv(sdev->host);
4684
4685 if (depth > hba->nutrs)
4686 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004687 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304688}
4689
4690/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004691 * ufshcd_slave_configure - adjust SCSI device configurations
4692 * @sdev: pointer to SCSI device
4693 */
4694static int ufshcd_slave_configure(struct scsi_device *sdev)
4695{
Stanley Chu49615ba2019-09-16 23:56:50 +08004696 struct ufs_hba *hba = shost_priv(sdev->host);
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004697 struct request_queue *q = sdev->request_queue;
4698
4699 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
Stanley Chu49615ba2019-09-16 23:56:50 +08004700
4701 if (ufshcd_is_rpm_autosuspend_allowed(hba))
4702 sdev->rpm_autosuspend = 1;
4703
Satya Tangiraladf043c742020-07-06 20:04:14 +00004704 ufshcd_crypto_setup_rq_keyslot_manager(hba, q);
4705
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004706 return 0;
4707}
4708
4709/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304710 * ufshcd_slave_destroy - remove SCSI device configurations
4711 * @sdev: pointer to SCSI device
4712 */
4713static void ufshcd_slave_destroy(struct scsi_device *sdev)
4714{
4715 struct ufs_hba *hba;
4716
4717 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004718 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004719 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4720 unsigned long flags;
4721
4722 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004723 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004724 spin_unlock_irqrestore(hba->host->host_lock, flags);
4725 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304726}
4727
4728/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304729 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004730 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304731 * @scsi_status: SCSI command status
4732 *
4733 * Returns value base on SCSI command status
4734 */
4735static inline int
4736ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4737{
4738 int result = 0;
4739
4740 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304741 case SAM_STAT_CHECK_CONDITION:
4742 ufshcd_copy_sense_data(lrbp);
Tomas Winkler30eb2e42018-11-26 10:10:34 +02004743 /* fallthrough */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304744 case SAM_STAT_GOOD:
4745 result |= DID_OK << 16 |
4746 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304747 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304748 break;
4749 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304750 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304751 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304752 ufshcd_copy_sense_data(lrbp);
4753 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304754 break;
4755 default:
4756 result |= DID_ERROR << 16;
4757 break;
4758 } /* end of switch */
4759
4760 return result;
4761}
4762
4763/**
4764 * ufshcd_transfer_rsp_status - Get overall status of the response
4765 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004766 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304767 *
4768 * Returns result of the command to notify SCSI midlayer
4769 */
4770static inline int
4771ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4772{
4773 int result = 0;
4774 int scsi_status;
4775 int ocs;
4776
4777 /* overall command status of utrd */
4778 ocs = ufshcd_get_tr_ocs(lrbp);
4779
Kiwoong Kimd779a6e2020-05-28 06:46:53 +05304780 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) {
4781 if (be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_1) &
4782 MASK_RSP_UPIU_RESULT)
4783 ocs = OCS_SUCCESS;
4784 }
4785
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304786 switch (ocs) {
4787 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304788 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004789 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304790 switch (result) {
4791 case UPIU_TRANSACTION_RESPONSE:
4792 /*
4793 * get the response UPIU result to extract
4794 * the SCSI command status
4795 */
4796 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4797
4798 /*
4799 * get the result based on SCSI status response
4800 * to notify the SCSI midlayer of the command status
4801 */
4802 scsi_status = result & MASK_SCSI_STATUS;
4803 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304804
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004805 /*
4806 * Currently we are only supporting BKOPs exception
4807 * events hence we can ignore BKOPs exception event
4808 * during power management callbacks. BKOPs exception
4809 * event is not expected to be raised in runtime suspend
4810 * callback as it allows the urgent bkops.
4811 * During system suspend, we are anyway forcefully
4812 * disabling the bkops and if urgent bkops is needed
4813 * it will be enabled on system resume. Long term
4814 * solution could be to abort the system suspend if
4815 * UFS device needs urgent BKOPs.
4816 */
4817 if (!hba->pm_op_in_progress &&
Sayali Lokhande2824ec92020-02-10 19:40:44 -08004818 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr) &&
4819 schedule_work(&hba->eeh_work)) {
4820 /*
4821 * Prevent suspend once eeh_work is scheduled
4822 * to avoid deadlock between ufshcd_suspend
4823 * and exception event handler.
4824 */
4825 pm_runtime_get_noresume(hba->dev);
4826 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304827 break;
4828 case UPIU_TRANSACTION_REJECT_UPIU:
4829 /* TODO: handle Reject UPIU Response */
4830 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304831 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304832 "Reject UPIU not fully implemented\n");
4833 break;
4834 default:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304835 dev_err(hba->dev,
4836 "Unexpected request response code = %x\n",
4837 result);
Stanley Chue0347d82019-04-15 20:23:38 +08004838 result = DID_ERROR << 16;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304839 break;
4840 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304841 break;
4842 case OCS_ABORTED:
4843 result |= DID_ABORT << 16;
4844 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304845 case OCS_INVALID_COMMAND_STATUS:
4846 result |= DID_REQUEUE << 16;
4847 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304848 case OCS_INVALID_CMD_TABLE_ATTR:
4849 case OCS_INVALID_PRDT_ATTR:
4850 case OCS_MISMATCH_DATA_BUF_SIZE:
4851 case OCS_MISMATCH_RESP_UPIU_SIZE:
4852 case OCS_PEER_COMM_FAILURE:
4853 case OCS_FATAL_ERROR:
Satya Tangirala5e7341e2020-07-06 20:04:12 +00004854 case OCS_DEVICE_FATAL_ERROR:
4855 case OCS_INVALID_CRYPTO_CONFIG:
4856 case OCS_GENERAL_CRYPTO_ERROR:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304857 default:
4858 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304859 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004860 "OCS error from controller = %x for tag %d\n",
4861 ocs, lrbp->task_tag);
4862 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08004863 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304864 break;
4865 } /* end of switch */
4866
Can Guo2df74b62019-11-25 22:53:33 -08004867 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs)
Dolev Raviv66cc8202016-12-22 18:39:42 -08004868 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304869 return result;
4870}
4871
4872/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304873 * ufshcd_uic_cmd_compl - handle completion of uic command
4874 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304875 * @intr_status: interrupt status generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004876 *
4877 * Returns
4878 * IRQ_HANDLED - If interrupt is valid
4879 * IRQ_NONE - If invalid interrupt
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304880 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004881static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304882{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004883 irqreturn_t retval = IRQ_NONE;
4884
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304885 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304886 hba->active_uic_cmd->argument2 |=
4887 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304888 hba->active_uic_cmd->argument3 =
4889 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304890 complete(&hba->active_uic_cmd->done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004891 retval = IRQ_HANDLED;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304892 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304893
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004894 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004895 complete(hba->uic_async_done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004896 retval = IRQ_HANDLED;
4897 }
Stanley Chuaa5c6972020-06-15 15:22:35 +08004898
4899 if (retval == IRQ_HANDLED)
4900 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd,
4901 "complete");
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004902 return retval;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304903}
4904
4905/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004906 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304907 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004908 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304909 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004910static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4911 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304912{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304913 struct ufshcd_lrb *lrbp;
4914 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304915 int result;
4916 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004917
Dolev Ravive9d501b2014-07-01 12:22:37 +03004918 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4919 lrbp = &hba->lrb[index];
Stanley Chua3170372020-07-06 14:07:06 +08004920 lrbp->compl_time_stamp = ktime_get();
Dolev Ravive9d501b2014-07-01 12:22:37 +03004921 cmd = lrbp->cmd;
4922 if (cmd) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004923 ufshcd_add_command_trace(hba, index, "complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004924 result = ufshcd_transfer_rsp_status(hba, lrbp);
4925 scsi_dma_unmap(cmd);
4926 cmd->result = result;
4927 /* Mark completed command as NULL in LRB */
4928 lrbp->cmd = NULL;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004929 /* Do not touch lrbp after scsi done */
4930 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004931 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01004932 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
4933 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004934 if (hba->dev_cmd.complete) {
4935 ufshcd_add_command_trace(hba, index,
4936 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004937 complete(hba->dev_cmd.complete);
Lee Susman1a07f2d2016-12-22 18:42:03 -08004938 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03004939 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08004940 if (ufshcd_is_clkscaling_supported(hba))
4941 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004942 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304943
4944 /* clear corresponding bits of completed commands */
4945 hba->outstanding_reqs ^= completed_reqs;
4946
Sahitya Tummala856b3482014-09-25 15:32:34 +03004947 ufshcd_clk_scaling_update_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304948}
4949
4950/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004951 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4952 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004953 *
4954 * Returns
4955 * IRQ_HANDLED - If interrupt is valid
4956 * IRQ_NONE - If invalid interrupt
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004957 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004958static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004959{
4960 unsigned long completed_reqs;
4961 u32 tr_doorbell;
4962
4963 /* Resetting interrupt aggregation counters first and reading the
4964 * DOOR_BELL afterward allows us to handle all the completed requests.
4965 * In order to prevent other interrupts starvation the DB is read once
4966 * after reset. The down side of this solution is the possibility of
4967 * false interrupt if device completes another request after resetting
4968 * aggregation and before reading the DB.
4969 */
Alim Akhtarb638b5e2020-05-28 06:46:50 +05304970 if (ufshcd_is_intr_aggr_allowed(hba) &&
4971 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004972 ufshcd_reset_intr_aggr(hba);
4973
4974 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4975 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
4976
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004977 if (completed_reqs) {
4978 __ufshcd_transfer_req_compl(hba, completed_reqs);
4979 return IRQ_HANDLED;
4980 } else {
4981 return IRQ_NONE;
4982 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004983}
4984
4985/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304986 * ufshcd_disable_ee - disable exception event
4987 * @hba: per-adapter instance
4988 * @mask: exception event to disable
4989 *
4990 * Disables exception event in the device so that the EVENT_ALERT
4991 * bit is not set.
4992 *
4993 * Returns zero on success, non-zero error value on failure.
4994 */
4995static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4996{
4997 int err = 0;
4998 u32 val;
4999
5000 if (!(hba->ee_ctrl_mask & mask))
5001 goto out;
5002
5003 val = hba->ee_ctrl_mask & ~mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03005004 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005005 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305006 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5007 if (!err)
5008 hba->ee_ctrl_mask &= ~mask;
5009out:
5010 return err;
5011}
5012
5013/**
5014 * ufshcd_enable_ee - enable exception event
5015 * @hba: per-adapter instance
5016 * @mask: exception event to enable
5017 *
5018 * Enable corresponding exception event in the device to allow
5019 * device to alert host in critical scenarios.
5020 *
5021 * Returns zero on success, non-zero error value on failure.
5022 */
5023static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
5024{
5025 int err = 0;
5026 u32 val;
5027
5028 if (hba->ee_ctrl_mask & mask)
5029 goto out;
5030
5031 val = hba->ee_ctrl_mask | mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03005032 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005033 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305034 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5035 if (!err)
5036 hba->ee_ctrl_mask |= mask;
5037out:
5038 return err;
5039}
5040
5041/**
5042 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5043 * @hba: per-adapter instance
5044 *
5045 * Allow device to manage background operations on its own. Enabling
5046 * this might lead to inconsistent latencies during normal data transfers
5047 * as the device is allowed to manage its own way of handling background
5048 * operations.
5049 *
5050 * Returns zero on success, non-zero on failure.
5051 */
5052static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5053{
5054 int err = 0;
5055
5056 if (hba->auto_bkops_enabled)
5057 goto out;
5058
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005059 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005060 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305061 if (err) {
5062 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5063 __func__, err);
5064 goto out;
5065 }
5066
5067 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005068 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305069
5070 /* No need of URGENT_BKOPS exception from the device */
5071 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5072 if (err)
5073 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5074 __func__, err);
5075out:
5076 return err;
5077}
5078
5079/**
5080 * ufshcd_disable_auto_bkops - block device in doing background operations
5081 * @hba: per-adapter instance
5082 *
5083 * Disabling background operations improves command response latency but
5084 * has drawback of device moving into critical state where the device is
5085 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5086 * host is idle so that BKOPS are managed effectively without any negative
5087 * impacts.
5088 *
5089 * Returns zero on success, non-zero on failure.
5090 */
5091static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5092{
5093 int err = 0;
5094
5095 if (!hba->auto_bkops_enabled)
5096 goto out;
5097
5098 /*
5099 * If host assisted BKOPs is to be enabled, make sure
5100 * urgent bkops exception is allowed.
5101 */
5102 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5103 if (err) {
5104 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5105 __func__, err);
5106 goto out;
5107 }
5108
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005109 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005110 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305111 if (err) {
5112 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5113 __func__, err);
5114 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5115 goto out;
5116 }
5117
5118 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005119 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Asutosh Das24366c2a2019-11-25 22:53:30 -08005120 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305121out:
5122 return err;
5123}
5124
5125/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005126 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305127 * @hba: per adapter instance
5128 *
5129 * After a device reset the device may toggle the BKOPS_EN flag
5130 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005131 * as well. This function would change the auto-bkops state based on
5132 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305133 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005134static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305135{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005136 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5137 hba->auto_bkops_enabled = false;
5138 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5139 ufshcd_enable_auto_bkops(hba);
5140 } else {
5141 hba->auto_bkops_enabled = true;
5142 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5143 ufshcd_disable_auto_bkops(hba);
5144 }
Stanley Chu7b6668d2020-05-30 22:12:00 +08005145 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
Asutosh Das24366c2a2019-11-25 22:53:30 -08005146 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305147}
5148
5149static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5150{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005151 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305152 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5153}
5154
5155/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005156 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5157 * @hba: per-adapter instance
5158 * @status: bkops_status value
5159 *
5160 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5161 * flag in the device to permit background operations if the device
5162 * bkops_status is greater than or equal to "status" argument passed to
5163 * this function, disable otherwise.
5164 *
5165 * Returns 0 for success, non-zero in case of failure.
5166 *
5167 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5168 * to know whether auto bkops is enabled or disabled after this function
5169 * returns control to it.
5170 */
5171static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5172 enum bkops_status status)
5173{
5174 int err;
5175 u32 curr_status = 0;
5176
5177 err = ufshcd_get_bkops_status(hba, &curr_status);
5178 if (err) {
5179 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5180 __func__, err);
5181 goto out;
5182 } else if (curr_status > BKOPS_STATUS_MAX) {
5183 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5184 __func__, curr_status);
5185 err = -EINVAL;
5186 goto out;
5187 }
5188
5189 if (curr_status >= status)
5190 err = ufshcd_enable_auto_bkops(hba);
5191 else
5192 err = ufshcd_disable_auto_bkops(hba);
5193out:
5194 return err;
5195}
5196
5197/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305198 * ufshcd_urgent_bkops - handle urgent bkops exception event
5199 * @hba: per-adapter instance
5200 *
5201 * Enable fBackgroundOpsEn flag in the device to permit background
5202 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005203 *
5204 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5205 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305206 */
5207static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5208{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005209 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305210}
5211
5212static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5213{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005214 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305215 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5216}
5217
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005218static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5219{
5220 int err;
5221 u32 curr_status = 0;
5222
5223 if (hba->is_urgent_bkops_lvl_checked)
5224 goto enable_auto_bkops;
5225
5226 err = ufshcd_get_bkops_status(hba, &curr_status);
5227 if (err) {
5228 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5229 __func__, err);
5230 goto out;
5231 }
5232
5233 /*
5234 * We are seeing that some devices are raising the urgent bkops
5235 * exception events even when BKOPS status doesn't indicate performace
5236 * impacted or critical. Handle these device by determining their urgent
5237 * bkops status at runtime.
5238 */
5239 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5240 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5241 __func__, curr_status);
5242 /* update the current status as the urgent bkops level */
5243 hba->urgent_bkops_lvl = curr_status;
5244 hba->is_urgent_bkops_lvl_checked = true;
5245 }
5246
5247enable_auto_bkops:
5248 err = ufshcd_enable_auto_bkops(hba);
5249out:
5250 if (err < 0)
5251 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5252 __func__, err);
5253}
5254
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005255static int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable)
5256{
5257 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005258 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005259 enum query_opcode opcode;
5260
Stanley Chu79e35202020-05-08 16:01:15 +08005261 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005262 return 0;
5263
5264 if (!(enable ^ hba->wb_enabled))
5265 return 0;
5266 if (enable)
5267 opcode = UPIU_QUERY_OPCODE_SET_FLAG;
5268 else
5269 opcode = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5270
Stanley Chue31011a2020-05-22 16:32:11 +08005271 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005272 ret = ufshcd_query_flag_retry(hba, opcode,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005273 QUERY_FLAG_IDN_WB_EN, index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005274 if (ret) {
5275 dev_err(hba->dev, "%s write booster %s failed %d\n",
5276 __func__, enable ? "enable" : "disable", ret);
5277 return ret;
5278 }
5279
5280 hba->wb_enabled = enable;
5281 dev_dbg(hba->dev, "%s write booster %s %d\n",
5282 __func__, enable ? "enable" : "disable", ret);
5283
5284 return ret;
5285}
5286
5287static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set)
5288{
5289 int val;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005290 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005291
5292 if (set)
5293 val = UPIU_QUERY_OPCODE_SET_FLAG;
5294 else
5295 val = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5296
Stanley Chue31011a2020-05-22 16:32:11 +08005297 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005298 return ufshcd_query_flag_retry(hba, val,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005299 QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8,
5300 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005301}
5302
5303static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable)
5304{
5305 if (enable)
5306 ufshcd_wb_buf_flush_enable(hba);
5307 else
5308 ufshcd_wb_buf_flush_disable(hba);
5309
5310}
5311
5312static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba)
5313{
5314 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005315 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005316
Stanley Chu79e35202020-05-08 16:01:15 +08005317 if (!ufshcd_is_wb_allowed(hba) || hba->wb_buf_flush_enabled)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005318 return 0;
5319
Stanley Chue31011a2020-05-22 16:32:11 +08005320 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005321 ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08005322 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005323 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005324 if (ret)
5325 dev_err(hba->dev, "%s WB - buf flush enable failed %d\n",
5326 __func__, ret);
5327 else
5328 hba->wb_buf_flush_enabled = true;
5329
5330 dev_dbg(hba->dev, "WB - Flush enabled: %d\n", ret);
5331 return ret;
5332}
5333
5334static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba)
5335{
5336 int ret;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005337 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005338
Stanley Chu79e35202020-05-08 16:01:15 +08005339 if (!ufshcd_is_wb_allowed(hba) || !hba->wb_buf_flush_enabled)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005340 return 0;
5341
Stanley Chue31011a2020-05-22 16:32:11 +08005342 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005343 ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Stanley Chu6f8d5a62020-05-08 16:01:13 +08005344 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN,
5345 index, NULL);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005346 if (ret) {
5347 dev_warn(hba->dev, "%s: WB - buf flush disable failed %d\n",
5348 __func__, ret);
5349 } else {
5350 hba->wb_buf_flush_enabled = false;
5351 dev_dbg(hba->dev, "WB - Flush disabled: %d\n", ret);
5352 }
5353
5354 return ret;
5355}
5356
5357static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba,
5358 u32 avail_buf)
5359{
5360 u32 cur_buf;
5361 int ret;
Stanley Chue31011a2020-05-22 16:32:11 +08005362 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005363
Stanley Chue31011a2020-05-22 16:32:11 +08005364 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005365 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5366 QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE,
Stanley Chue31011a2020-05-22 16:32:11 +08005367 index, 0, &cur_buf);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005368 if (ret) {
5369 dev_err(hba->dev, "%s dCurWriteBoosterBufferSize read failed %d\n",
5370 __func__, ret);
5371 return false;
5372 }
5373
5374 if (!cur_buf) {
5375 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n",
5376 cur_buf);
5377 return false;
5378 }
Stanley Chud14734ae2020-05-09 17:37:15 +08005379 /* Let it continue to flush when available buffer exceeds threshold */
5380 if (avail_buf < hba->vps->wb_flush_threshold)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005381 return true;
5382
5383 return false;
5384}
5385
Stanley Chu51dd9052020-05-22 16:32:12 +08005386static bool ufshcd_wb_need_flush(struct ufs_hba *hba)
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005387{
5388 int ret;
5389 u32 avail_buf;
Stanley Chue31011a2020-05-22 16:32:11 +08005390 u8 index;
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005391
Stanley Chu79e35202020-05-08 16:01:15 +08005392 if (!ufshcd_is_wb_allowed(hba))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005393 return false;
5394 /*
5395 * The ufs device needs the vcc to be ON to flush.
5396 * With user-space reduction enabled, it's enough to enable flush
5397 * by checking only the available buffer. The threshold
5398 * defined here is > 90% full.
5399 * With user-space preserved enabled, the current-buffer
5400 * should be checked too because the wb buffer size can reduce
5401 * when disk tends to be full. This info is provided by current
5402 * buffer (dCurrentWriteBoosterBufferSize). There's no point in
5403 * keeping vcc on when current buffer is empty.
5404 */
Stanley Chue31011a2020-05-22 16:32:11 +08005405 index = ufshcd_wb_get_query_index(hba);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005406 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5407 QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE,
Stanley Chue31011a2020-05-22 16:32:11 +08005408 index, 0, &avail_buf);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005409 if (ret) {
5410 dev_warn(hba->dev, "%s dAvailableWriteBoosterBufferSize read failed %d\n",
5411 __func__, ret);
5412 return false;
5413 }
5414
5415 if (!hba->dev_info.b_presrv_uspc_en) {
Stanley Chud14734ae2020-05-09 17:37:15 +08005416 if (avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10))
Asutosh Das3d17b9b2020-04-22 14:41:42 -07005417 return true;
5418 return false;
5419 }
5420
5421 return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf);
5422}
5423
Stanley Chu51dd9052020-05-22 16:32:12 +08005424static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
5425{
5426 struct ufs_hba *hba = container_of(to_delayed_work(work),
5427 struct ufs_hba,
5428 rpm_dev_flush_recheck_work);
5429 /*
5430 * To prevent unnecessary VCC power drain after device finishes
5431 * WriteBooster buffer flush or Auto BKOPs, force runtime resume
5432 * after a certain delay to recheck the threshold by next runtime
5433 * suspend.
5434 */
5435 pm_runtime_get_sync(hba->dev);
5436 pm_runtime_put_sync(hba->dev);
5437}
5438
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305439/**
5440 * ufshcd_exception_event_handler - handle exceptions raised by device
5441 * @work: pointer to work data
5442 *
5443 * Read bExceptionEventStatus attribute from the device and handle the
5444 * exception event accordingly.
5445 */
5446static void ufshcd_exception_event_handler(struct work_struct *work)
5447{
5448 struct ufs_hba *hba;
5449 int err;
5450 u32 status = 0;
5451 hba = container_of(work, struct ufs_hba, eeh_work);
5452
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305453 pm_runtime_get_sync(hba->dev);
Stanley Chu03e1d282019-12-24 21:01:05 +08005454 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305455 err = ufshcd_get_ee_status(hba, &status);
5456 if (err) {
5457 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5458 __func__, err);
5459 goto out;
5460 }
5461
5462 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005463
5464 if (status & MASK_EE_URGENT_BKOPS)
5465 ufshcd_bkops_exception_event_handler(hba);
5466
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305467out:
Stanley Chu03e1d282019-12-24 21:01:05 +08005468 ufshcd_scsi_unblock_requests(hba);
Sayali Lokhande2824ec92020-02-10 19:40:44 -08005469 /*
5470 * pm_runtime_get_noresume is called while scheduling
5471 * eeh_work to avoid suspend racing with exception work.
5472 * Hence decrement usage counter using pm_runtime_put_noidle
5473 * to allow suspend on completion of exception event handler.
5474 */
5475 pm_runtime_put_noidle(hba->dev);
5476 pm_runtime_put(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305477 return;
5478}
5479
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005480/* Complete requests that have door-bell cleared */
5481static void ufshcd_complete_requests(struct ufs_hba *hba)
5482{
5483 ufshcd_transfer_req_compl(hba);
5484 ufshcd_tmc_handler(hba);
5485}
5486
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305487/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005488 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5489 * to recover from the DL NAC errors or not.
5490 * @hba: per-adapter instance
5491 *
5492 * Returns true if error handling is required, false otherwise
5493 */
5494static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5495{
5496 unsigned long flags;
5497 bool err_handling = true;
5498
5499 spin_lock_irqsave(hba->host->host_lock, flags);
5500 /*
5501 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5502 * device fatal error and/or DL NAC & REPLAY timeout errors.
5503 */
5504 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5505 goto out;
5506
5507 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5508 ((hba->saved_err & UIC_ERROR) &&
5509 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
5510 goto out;
5511
5512 if ((hba->saved_err & UIC_ERROR) &&
5513 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5514 int err;
5515 /*
5516 * wait for 50ms to see if we can get any other errors or not.
5517 */
5518 spin_unlock_irqrestore(hba->host->host_lock, flags);
5519 msleep(50);
5520 spin_lock_irqsave(hba->host->host_lock, flags);
5521
5522 /*
5523 * now check if we have got any other severe errors other than
5524 * DL NAC error?
5525 */
5526 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5527 ((hba->saved_err & UIC_ERROR) &&
5528 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5529 goto out;
5530
5531 /*
5532 * As DL NAC is the only error received so far, send out NOP
5533 * command to confirm if link is still active or not.
5534 * - If we don't get any response then do error recovery.
5535 * - If we get response then clear the DL NAC error bit.
5536 */
5537
5538 spin_unlock_irqrestore(hba->host->host_lock, flags);
5539 err = ufshcd_verify_dev_init(hba);
5540 spin_lock_irqsave(hba->host->host_lock, flags);
5541
5542 if (err)
5543 goto out;
5544
5545 /* Link seems to be alive hence ignore the DL NAC errors */
5546 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5547 hba->saved_err &= ~UIC_ERROR;
5548 /* clear NAC error */
5549 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5550 if (!hba->saved_uic_err) {
5551 err_handling = false;
5552 goto out;
5553 }
5554 }
5555out:
5556 spin_unlock_irqrestore(hba->host->host_lock, flags);
5557 return err_handling;
5558}
5559
5560/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305561 * ufshcd_err_handler - handle UFS errors that require s/w attention
5562 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305563 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305564static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305565{
5566 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305567 unsigned long flags;
5568 u32 err_xfer = 0;
5569 u32 err_tm = 0;
5570 int err = 0;
5571 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005572 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305573
5574 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305575
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305576 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005577 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305578
5579 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005580 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305581 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305582
5583 hba->ufshcd_state = UFSHCD_STATE_RESET;
5584 ufshcd_set_eh_in_progress(hba);
5585
5586 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005587 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005588
5589 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5590 bool ret;
5591
5592 spin_unlock_irqrestore(hba->host->host_lock, flags);
5593 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5594 ret = ufshcd_quirk_dl_nac_errors(hba);
5595 spin_lock_irqsave(hba->host->host_lock, flags);
5596 if (!ret)
5597 goto skip_err_handling;
5598 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005599 if ((hba->saved_err & INT_FATAL_ERRORS) ||
Stanley Chu82174442019-05-21 14:44:54 +08005600 (hba->saved_err & UFSHCD_UIC_HIBERN8_MASK) ||
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005601 ((hba->saved_err & UIC_ERROR) &&
5602 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5603 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5604 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5605 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305606
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005607 /*
5608 * if host reset is required then skip clearing the pending
Can Guo2df74b62019-11-25 22:53:33 -08005609 * transfers forcefully because they will get cleared during
5610 * host reset and restore
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005611 */
5612 if (needs_reset)
5613 goto skip_pending_xfer_clear;
5614
5615 /* release lock as clear command might sleep */
5616 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305617 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005618 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5619 if (ufshcd_clear_cmd(hba, tag)) {
5620 err_xfer = true;
5621 goto lock_skip_pending_xfer_clear;
5622 }
5623 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305624
5625 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005626 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5627 if (ufshcd_clear_tm_cmd(hba, tag)) {
5628 err_tm = true;
5629 goto lock_skip_pending_xfer_clear;
5630 }
5631 }
5632
5633lock_skip_pending_xfer_clear:
5634 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305635
5636 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005637 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305638
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005639 if (err_xfer || err_tm)
5640 needs_reset = true;
5641
5642skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305643 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005644 if (needs_reset) {
5645 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5646
5647 /*
5648 * ufshcd_reset_and_restore() does the link reinitialization
5649 * which will need atleast one empty doorbell slot to send the
5650 * device management commands (NOP and query commands).
5651 * If there is no slot empty at this moment then free up last
5652 * slot forcefully.
5653 */
5654 if (hba->outstanding_reqs == max_doorbells)
5655 __ufshcd_transfer_req_compl(hba,
5656 (1UL << (hba->nutrs - 1)));
5657
5658 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305659 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005660 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305661 if (err) {
5662 dev_err(hba->dev, "%s: reset and restore failed\n",
5663 __func__);
5664 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5665 }
5666 /*
5667 * Inform scsi mid-layer that we did reset and allow to handle
5668 * Unit Attention properly.
5669 */
5670 scsi_report_bus_reset(hba->host, 0);
5671 hba->saved_err = 0;
5672 hba->saved_uic_err = 0;
5673 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005674
Yaniv Gardi583fa622016-03-10 17:37:13 +02005675skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005676 if (!needs_reset) {
5677 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5678 if (hba->saved_err || hba->saved_uic_err)
5679 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5680 __func__, hba->saved_err, hba->saved_uic_err);
5681 }
5682
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305683 ufshcd_clear_eh_in_progress(hba);
5684
5685out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005686 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani38135532018-05-03 16:37:18 +05305687 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005688 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305689 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305690}
5691
5692/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305693 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5694 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005695 *
5696 * Returns
5697 * IRQ_HANDLED - If interrupt is valid
5698 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305699 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005700static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305701{
5702 u32 reg;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005703 irqreturn_t retval = IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305704
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005705 /* PHY layer lane error */
5706 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5707 /* Ignore LINERESET indication, as this is not an error */
5708 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005709 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005710 /*
5711 * To know whether this error is fatal or not, DB timeout
5712 * must be checked but this error is handled separately.
5713 */
5714 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
Stanley Chu48d5b972019-07-10 21:38:18 +08005715 ufshcd_update_reg_hist(&hba->ufs_stats.pa_err, reg);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005716 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005717 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005718
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305719 /* PA_INIT_ERROR is fatal and needs UIC reset */
5720 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005721 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
5722 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005723 ufshcd_update_reg_hist(&hba->ufs_stats.dl_err, reg);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005724
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005725 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
5726 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
5727 else if (hba->dev_quirks &
5728 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5729 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5730 hba->uic_error |=
5731 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5732 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5733 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5734 }
5735 retval |= IRQ_HANDLED;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005736 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305737
5738 /* UIC NL/TL/DME errors needs software retry */
5739 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005740 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
5741 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005742 ufshcd_update_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305743 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005744 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005745 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305746
5747 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005748 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
5749 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005750 ufshcd_update_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305751 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005752 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005753 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305754
5755 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005756 if ((reg & UIC_DME_ERROR) &&
5757 (reg & UIC_DME_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005758 ufshcd_update_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305759 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005760 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005761 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305762
5763 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5764 __func__, hba->uic_error);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005765 return retval;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305766}
5767
Stanley Chu82174442019-05-21 14:44:54 +08005768static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
5769 u32 intr_mask)
5770{
Stanley Chu5a244e02020-01-29 18:52:50 +08005771 if (!ufshcd_is_auto_hibern8_supported(hba) ||
5772 !ufshcd_is_auto_hibern8_enabled(hba))
Stanley Chu82174442019-05-21 14:44:54 +08005773 return false;
5774
5775 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
5776 return false;
5777
5778 if (hba->active_uic_cmd &&
5779 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
5780 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
5781 return false;
5782
5783 return true;
5784}
5785
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305786/**
5787 * ufshcd_check_errors - Check for errors that need s/w attention
5788 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005789 *
5790 * Returns
5791 * IRQ_HANDLED - If interrupt is valid
5792 * IRQ_NONE - If invalid interrupt
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305793 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005794static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305795{
5796 bool queue_eh_work = false;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005797 irqreturn_t retval = IRQ_NONE;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305798
Stanley Chud3c615b2019-07-10 21:38:19 +08005799 if (hba->errors & INT_FATAL_ERRORS) {
5800 ufshcd_update_reg_hist(&hba->ufs_stats.fatal_err, hba->errors);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305801 queue_eh_work = true;
Stanley Chud3c615b2019-07-10 21:38:19 +08005802 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305803
5804 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305805 hba->uic_error = 0;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005806 retval = ufshcd_update_uic_error(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305807 if (hba->uic_error)
5808 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305809 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305810
Stanley Chu82174442019-05-21 14:44:54 +08005811 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
5812 dev_err(hba->dev,
5813 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
5814 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
5815 "Enter" : "Exit",
5816 hba->errors, ufshcd_get_upmcrs(hba));
Stanley Chud3c615b2019-07-10 21:38:19 +08005817 ufshcd_update_reg_hist(&hba->ufs_stats.auto_hibern8_err,
5818 hba->errors);
Stanley Chu82174442019-05-21 14:44:54 +08005819 queue_eh_work = true;
5820 }
5821
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305822 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005823 /*
5824 * update the transfer error masks to sticky bits, let's do this
5825 * irrespective of current ufshcd_state.
5826 */
5827 hba->saved_err |= hba->errors;
5828 hba->saved_uic_err |= hba->uic_error;
5829
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305830 /* handle fatal errors only when link is functional */
5831 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5832 /* block commands from scsi mid-layer */
Subhash Jadavani38135532018-05-03 16:37:18 +05305833 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305834
Zang Leigang141f8162016-11-16 11:29:37 +08005835 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08005836
5837 /* dump controller state before resetting */
5838 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5839 bool pr_prdt = !!(hba->saved_err &
5840 SYSTEM_BUS_FATAL_ERROR);
5841
5842 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5843 __func__, hba->saved_err,
5844 hba->saved_uic_err);
5845
5846 ufshcd_print_host_regs(hba);
5847 ufshcd_print_pwr_info(hba);
5848 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5849 ufshcd_print_trs(hba, hba->outstanding_reqs,
5850 pr_prdt);
5851 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305852 schedule_work(&hba->eh_work);
5853 }
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005854 retval |= IRQ_HANDLED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305855 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305856 /*
5857 * if (!queue_eh_work) -
5858 * Other errors are either non-fatal where host recovers
5859 * itself without s/w intervention or errors that will be
5860 * handled by the SCSI core layer.
5861 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005862 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305863}
5864
Bart Van Assche69a6c262019-12-09 10:13:09 -08005865struct ctm_info {
5866 struct ufs_hba *hba;
5867 unsigned long pending;
5868 unsigned int ncpl;
5869};
5870
5871static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved)
5872{
5873 struct ctm_info *const ci = priv;
5874 struct completion *c;
5875
5876 WARN_ON_ONCE(reserved);
5877 if (test_bit(req->tag, &ci->pending))
5878 return true;
5879 ci->ncpl++;
5880 c = req->end_io_data;
5881 if (c)
5882 complete(c);
5883 return true;
5884}
5885
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305886/**
5887 * ufshcd_tmc_handler - handle task management function completion
5888 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005889 *
5890 * Returns
5891 * IRQ_HANDLED - If interrupt is valid
5892 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305893 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005894static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305895{
Bart Van Assche69a6c262019-12-09 10:13:09 -08005896 struct request_queue *q = hba->tmf_queue;
5897 struct ctm_info ci = {
5898 .hba = hba,
5899 .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL),
5900 };
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305901
Bart Van Assche69a6c262019-12-09 10:13:09 -08005902 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci);
5903 return ci.ncpl ? IRQ_HANDLED : IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305904}
5905
5906/**
5907 * ufshcd_sl_intr - Interrupt service routine
5908 * @hba: per adapter instance
5909 * @intr_status: contains interrupts generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005910 *
5911 * Returns
5912 * IRQ_HANDLED - If interrupt is valid
5913 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305914 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005915static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305916{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005917 irqreturn_t retval = IRQ_NONE;
5918
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305919 hba->errors = UFSHCD_ERROR_MASK & intr_status;
Stanley Chu82174442019-05-21 14:44:54 +08005920
5921 if (ufshcd_is_auto_hibern8_error(hba, intr_status))
5922 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
5923
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305924 if (hba->errors)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005925 retval |= ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305926
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305927 if (intr_status & UFSHCD_UIC_MASK)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005928 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305929
5930 if (intr_status & UTP_TASK_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005931 retval |= ufshcd_tmc_handler(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305932
5933 if (intr_status & UTP_TRANSFER_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005934 retval |= ufshcd_transfer_req_compl(hba);
5935
5936 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305937}
5938
5939/**
5940 * ufshcd_intr - Main interrupt service routine
5941 * @irq: irq number
5942 * @__hba: pointer to adapter instance
5943 *
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005944 * Returns
5945 * IRQ_HANDLED - If interrupt is valid
5946 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305947 */
5948static irqreturn_t ufshcd_intr(int irq, void *__hba)
5949{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005950 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305951 irqreturn_t retval = IRQ_NONE;
5952 struct ufs_hba *hba = __hba;
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305953 int retries = hba->nutrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305954
5955 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305956 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Can Guo3f8af602020-08-09 05:15:50 -07005957 hba->ufs_stats.last_intr_status = intr_status;
5958 hba->ufs_stats.last_intr_ts = ktime_get();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305959
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305960 /*
5961 * There could be max of hba->nutrs reqs in flight and in worst case
5962 * if the reqs get finished 1 by 1 after the interrupt status is
5963 * read, make sure we handle them by checking the interrupt status
5964 * again in a loop until we process all of the reqs before returning.
5965 */
5966 do {
5967 enabled_intr_status =
5968 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
5969 if (intr_status)
5970 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005971 if (enabled_intr_status)
5972 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005973
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305974 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
5975 } while (intr_status && --retries);
5976
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005977 if (retval == IRQ_NONE) {
5978 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n",
5979 __func__, intr_status);
5980 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
5981 }
5982
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305983 spin_unlock(hba->host->host_lock);
5984 return retval;
5985}
5986
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305987static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5988{
5989 int err = 0;
5990 u32 mask = 1 << tag;
5991 unsigned long flags;
5992
5993 if (!test_bit(tag, &hba->outstanding_tasks))
5994 goto out;
5995
5996 spin_lock_irqsave(hba->host->host_lock, flags);
Alim Akhtar1399c5b2018-05-06 15:44:15 +05305997 ufshcd_utmrl_clear(hba, tag);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305998 spin_unlock_irqrestore(hba->host->host_lock, flags);
5999
6000 /* poll for max. 1 sec to clear door bell register by h/w */
6001 err = ufshcd_wait_for_register(hba,
6002 REG_UTP_TASK_REQ_DOOR_BELL,
Bart Van Assche5cac1092020-05-07 15:27:50 -07006003 mask, 0, 1000, 1000);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306004out:
6005 return err;
6006}
6007
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006008static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
6009 struct utp_task_req_desc *treq, u8 tm_function)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306010{
Bart Van Assche69a6c262019-12-09 10:13:09 -08006011 struct request_queue *q = hba->tmf_queue;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006012 struct Scsi_Host *host = hba->host;
Bart Van Assche69a6c262019-12-09 10:13:09 -08006013 DECLARE_COMPLETION_ONSTACK(wait);
6014 struct request *req;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306015 unsigned long flags;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006016 int free_slot, task_tag, err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306017
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306018 /*
6019 * Get free slot, sleep if slots are unavailable.
6020 * Even though we use wait_event() which sleeps indefinitely,
6021 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
6022 */
Bart Van Assche69a6c262019-12-09 10:13:09 -08006023 req = blk_get_request(q, REQ_OP_DRV_OUT, BLK_MQ_REQ_RESERVED);
6024 req->end_io_data = &wait;
6025 free_slot = req->tag;
6026 WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006027 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306028
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306029 spin_lock_irqsave(host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306030 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306031
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006032 treq->req_header.dword_0 |= cpu_to_be32(task_tag);
6033
6034 memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq));
Kiwoong Kimd2877be2016-11-10 21:16:15 +09006035 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
6036
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306037 /* send command to the controller */
6038 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02006039
6040 /* Make sure descriptors are ready before ringing the task doorbell */
6041 wmb();
6042
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306043 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07006044 /* Make sure that doorbell is committed immediately */
6045 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306046
6047 spin_unlock_irqrestore(host->host_lock, flags);
6048
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03006049 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_send");
6050
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306051 /* wait until the task management command is completed */
Bart Van Assche69a6c262019-12-09 10:13:09 -08006052 err = wait_for_completion_io_timeout(&wait,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306053 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306054 if (!err) {
Bart Van Assche69a6c262019-12-09 10:13:09 -08006055 /*
6056 * Make sure that ufshcd_compl_tm() does not trigger a
6057 * use-after-free.
6058 */
6059 req->end_io_data = NULL;
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03006060 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete_err");
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306061 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6062 __func__, tm_function);
6063 if (ufshcd_clear_tm_cmd(hba, free_slot))
6064 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
6065 __func__, free_slot);
6066 err = -ETIMEDOUT;
6067 } else {
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006068 err = 0;
6069 memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq));
6070
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03006071 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306072 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306073
Stanley Chub5572172019-08-19 21:43:28 +08006074 spin_lock_irqsave(hba->host->host_lock, flags);
6075 __clear_bit(free_slot, &hba->outstanding_tasks);
6076 spin_unlock_irqrestore(hba->host->host_lock, flags);
6077
Bart Van Assche69a6c262019-12-09 10:13:09 -08006078 blk_put_request(req);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306079
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006080 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306081 return err;
6082}
6083
6084/**
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03006085 * ufshcd_issue_tm_cmd - issues task management commands to controller
6086 * @hba: per adapter instance
6087 * @lun_id: LUN ID to which TM command is sent
6088 * @task_id: task ID to which the TM command is applicable
6089 * @tm_function: task management function opcode
6090 * @tm_response: task management service response return value
6091 *
6092 * Returns non-zero value on error, zero on success.
6093 */
6094static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
6095 u8 tm_function, u8 *tm_response)
6096{
6097 struct utp_task_req_desc treq = { { 0 }, };
6098 int ocs_value, err;
6099
6100 /* Configure task request descriptor */
6101 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6102 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6103
6104 /* Configure task request UPIU */
6105 treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) |
6106 cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24);
6107 treq.req_header.dword_1 = cpu_to_be32(tm_function << 16);
6108
6109 /*
6110 * The host shall provide the same value for LUN field in the basic
6111 * header and for Input Parameter.
6112 */
6113 treq.input_param1 = cpu_to_be32(lun_id);
6114 treq.input_param2 = cpu_to_be32(task_id);
6115
6116 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
6117 if (err == -ETIMEDOUT)
6118 return err;
6119
6120 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6121 if (ocs_value != OCS_SUCCESS)
6122 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
6123 __func__, ocs_value);
6124 else if (tm_response)
6125 *tm_response = be32_to_cpu(treq.output_param1) &
6126 MASK_TM_SERVICE_RESP;
6127 return err;
6128}
6129
6130/**
Avri Altman5e0a86e2018-10-07 17:30:37 +03006131 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
6132 * @hba: per-adapter instance
6133 * @req_upiu: upiu request
6134 * @rsp_upiu: upiu reply
Avri Altman5e0a86e2018-10-07 17:30:37 +03006135 * @desc_buff: pointer to descriptor buffer, NULL if NA
6136 * @buff_len: descriptor size, 0 if NA
Bart Van Assched0e97602019-10-29 16:07:08 -07006137 * @cmd_type: specifies the type (NOP, Query...)
Avri Altman5e0a86e2018-10-07 17:30:37 +03006138 * @desc_op: descriptor operation
6139 *
6140 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
6141 * Therefore, it "rides" the device management infrastructure: uses its tag and
6142 * tasks work queues.
6143 *
6144 * Since there is only one available tag for device management commands,
6145 * the caller is expected to hold the hba->dev_cmd.lock mutex.
6146 */
6147static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
6148 struct utp_upiu_req *req_upiu,
6149 struct utp_upiu_req *rsp_upiu,
6150 u8 *desc_buff, int *buff_len,
Bart Van Assche7f674c32019-10-29 16:07:09 -07006151 enum dev_cmd_type cmd_type,
Avri Altman5e0a86e2018-10-07 17:30:37 +03006152 enum query_opcode desc_op)
6153{
Bart Van Assche7252a362019-12-09 10:13:08 -08006154 struct request_queue *q = hba->cmd_queue;
6155 struct request *req;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006156 struct ufshcd_lrb *lrbp;
6157 int err = 0;
6158 int tag;
6159 struct completion wait;
6160 unsigned long flags;
Bean Huoa23064c2020-07-06 14:39:36 +02006161 u8 upiu_flags;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006162
6163 down_read(&hba->clk_scaling_lock);
6164
Bart Van Assche7252a362019-12-09 10:13:08 -08006165 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03006166 if (IS_ERR(req)) {
6167 err = PTR_ERR(req);
6168 goto out_unlock;
6169 }
Bart Van Assche7252a362019-12-09 10:13:08 -08006170 tag = req->tag;
6171 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Avri Altman5e0a86e2018-10-07 17:30:37 +03006172
6173 init_completion(&wait);
6174 lrbp = &hba->lrb[tag];
6175 WARN_ON(lrbp->cmd);
6176
6177 lrbp->cmd = NULL;
6178 lrbp->sense_bufflen = 0;
6179 lrbp->sense_buffer = NULL;
6180 lrbp->task_tag = tag;
6181 lrbp->lun = 0;
6182 lrbp->intr_cmd = true;
Satya Tangiraladf043c742020-07-06 20:04:14 +00006183 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
Avri Altman5e0a86e2018-10-07 17:30:37 +03006184 hba->dev_cmd.type = cmd_type;
6185
6186 switch (hba->ufs_version) {
6187 case UFSHCI_VERSION_10:
6188 case UFSHCI_VERSION_11:
6189 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
6190 break;
6191 default:
6192 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
6193 break;
6194 }
6195
6196 /* update the task tag in the request upiu */
6197 req_upiu->header.dword_0 |= cpu_to_be32(tag);
6198
6199 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
6200
6201 /* just copy the upiu request as it is */
6202 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
6203 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
6204 /* The Data Segment Area is optional depending upon the query
6205 * function value. for WRITE DESCRIPTOR, the data segment
6206 * follows right after the tsf.
6207 */
6208 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
6209 *buff_len = 0;
6210 }
6211
6212 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
6213
6214 hba->dev_cmd.complete = &wait;
6215
6216 /* Make sure descriptors are ready before ringing the doorbell */
6217 wmb();
6218 spin_lock_irqsave(hba->host->host_lock, flags);
6219 ufshcd_send_command(hba, tag);
6220 spin_unlock_irqrestore(hba->host->host_lock, flags);
6221
6222 /*
6223 * ignore the returning value here - ufshcd_check_query_response is
6224 * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
6225 * read the response directly ignoring all errors.
6226 */
6227 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT);
6228
6229 /* just copy the upiu response as it is */
6230 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
Avri Altman4bbbe242019-02-20 09:11:13 +02006231 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
6232 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
6233 u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
6234 MASK_QUERY_DATA_SEG_LEN;
6235
6236 if (*buff_len >= resp_len) {
6237 memcpy(desc_buff, descp, resp_len);
6238 *buff_len = resp_len;
6239 } else {
Bean Huo3d4881d2019-11-12 23:34:35 +01006240 dev_warn(hba->dev,
6241 "%s: rsp size %d is bigger than buffer size %d",
6242 __func__, resp_len, *buff_len);
Avri Altman4bbbe242019-02-20 09:11:13 +02006243 *buff_len = 0;
6244 err = -EINVAL;
6245 }
6246 }
Avri Altman5e0a86e2018-10-07 17:30:37 +03006247
Bart Van Assche7252a362019-12-09 10:13:08 -08006248 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03006249out_unlock:
Avri Altman5e0a86e2018-10-07 17:30:37 +03006250 up_read(&hba->clk_scaling_lock);
6251 return err;
6252}
6253
6254/**
6255 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
6256 * @hba: per-adapter instance
6257 * @req_upiu: upiu request
6258 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
6259 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
6260 * @desc_buff: pointer to descriptor buffer, NULL if NA
6261 * @buff_len: descriptor size, 0 if NA
6262 * @desc_op: descriptor operation
6263 *
6264 * Supports UTP Transfer requests (nop and query), and UTP Task
6265 * Management requests.
6266 * It is up to the caller to fill the upiu conent properly, as it will
6267 * be copied without any further input validations.
6268 */
6269int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
6270 struct utp_upiu_req *req_upiu,
6271 struct utp_upiu_req *rsp_upiu,
6272 int msgcode,
6273 u8 *desc_buff, int *buff_len,
6274 enum query_opcode desc_op)
6275{
6276 int err;
Bart Van Assche7f674c32019-10-29 16:07:09 -07006277 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
Avri Altman5e0a86e2018-10-07 17:30:37 +03006278 struct utp_task_req_desc treq = { { 0 }, };
6279 int ocs_value;
6280 u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC;
6281
Avri Altman5e0a86e2018-10-07 17:30:37 +03006282 switch (msgcode) {
6283 case UPIU_TRANSACTION_NOP_OUT:
6284 cmd_type = DEV_CMD_TYPE_NOP;
6285 /* fall through */
6286 case UPIU_TRANSACTION_QUERY_REQ:
6287 ufshcd_hold(hba, false);
6288 mutex_lock(&hba->dev_cmd.lock);
6289 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
6290 desc_buff, buff_len,
6291 cmd_type, desc_op);
6292 mutex_unlock(&hba->dev_cmd.lock);
6293 ufshcd_release(hba);
6294
6295 break;
6296 case UPIU_TRANSACTION_TASK_REQ:
6297 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6298 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6299
6300 memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu));
6301
6302 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
6303 if (err == -ETIMEDOUT)
6304 break;
6305
6306 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6307 if (ocs_value != OCS_SUCCESS) {
6308 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
6309 ocs_value);
6310 break;
6311 }
6312
6313 memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu));
6314
6315 break;
6316 default:
6317 err = -EINVAL;
6318
6319 break;
6320 }
6321
Avri Altman5e0a86e2018-10-07 17:30:37 +03006322 return err;
6323}
6324
6325/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306326 * ufshcd_eh_device_reset_handler - device reset handler registered to
6327 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306328 * @cmd: SCSI command pointer
6329 *
6330 * Returns SUCCESS/FAILED
6331 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306332static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306333{
6334 struct Scsi_Host *host;
6335 struct ufs_hba *hba;
6336 unsigned int tag;
6337 u32 pos;
6338 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306339 u8 resp = 0xF;
6340 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306341 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306342
6343 host = cmd->device->host;
6344 hba = shost_priv(host);
6345 tag = cmd->request->tag;
6346
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306347 lrbp = &hba->lrb[tag];
6348 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
6349 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306350 if (!err)
6351 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306352 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306353 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306354
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306355 /* clear the commands that were pending for corresponding LUN */
6356 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6357 if (hba->lrb[pos].lun == lrbp->lun) {
6358 err = ufshcd_clear_cmd(hba, pos);
6359 if (err)
6360 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306361 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306362 }
6363 spin_lock_irqsave(host->host_lock, flags);
6364 ufshcd_transfer_req_compl(hba);
6365 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08006366
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306367out:
Gilad Broner7fabb772017-02-03 16:56:50 -08006368 hba->req_abort_count = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08006369 ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306370 if (!err) {
6371 err = SUCCESS;
6372 } else {
6373 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6374 err = FAILED;
6375 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306376 return err;
6377}
6378
Gilad Bronere0b299e2017-02-03 16:56:40 -08006379static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6380{
6381 struct ufshcd_lrb *lrbp;
6382 int tag;
6383
6384 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6385 lrbp = &hba->lrb[tag];
6386 lrbp->req_abort_skip = true;
6387 }
6388}
6389
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306390/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306391 * ufshcd_abort - abort a specific command
6392 * @cmd: SCSI command pointer
6393 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306394 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6395 * command, and in host controller by clearing the door-bell register. There can
6396 * be race between controller sending the command to the device while abort is
6397 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6398 * really issued and then try to abort it.
6399 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306400 * Returns SUCCESS/FAILED
6401 */
6402static int ufshcd_abort(struct scsi_cmnd *cmd)
6403{
6404 struct Scsi_Host *host;
6405 struct ufs_hba *hba;
6406 unsigned long flags;
6407 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306408 int err = 0;
6409 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306410 u8 resp = 0xF;
6411 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03006412 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306413
6414 host = cmd->device->host;
6415 hba = shost_priv(host);
6416 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08006417 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02006418 if (!ufshcd_valid_tag(hba, tag)) {
6419 dev_err(hba->dev,
6420 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6421 __func__, tag, cmd, cmd->request);
6422 BUG();
6423 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306424
Dolev Ravive7d38252016-12-22 18:40:07 -08006425 /*
6426 * Task abort to the device W-LUN is illegal. When this command
6427 * will fail, due to spec violation, scsi err handling next step
6428 * will be to send LU reset which, again, is a spec violation.
6429 * To avoid these unnecessary/illegal step we skip to the last error
6430 * handling stage: reset and restore.
6431 */
6432 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
6433 return ufshcd_eh_host_reset_handler(cmd);
6434
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006435 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03006436 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02006437 /* If command is already aborted/completed, return SUCCESS */
6438 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6439 dev_err(hba->dev,
6440 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6441 __func__, tag, hba->outstanding_reqs, reg);
6442 goto out;
6443 }
6444
Dolev Ravive9d501b2014-07-01 12:22:37 +03006445 if (!(reg & (1 << tag))) {
6446 dev_err(hba->dev,
6447 "%s: cmd was completed, but without a notifying intr, tag = %d",
6448 __func__, tag);
6449 }
6450
Dolev Raviv66cc8202016-12-22 18:39:42 -08006451 /* Print Transfer Request of aborted task */
6452 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08006453
Gilad Broner7fabb772017-02-03 16:56:50 -08006454 /*
6455 * Print detailed info about aborted request.
6456 * As more than one request might get aborted at the same time,
6457 * print full information only for the first aborted request in order
6458 * to reduce repeated printouts. For other aborted requests only print
6459 * basic details.
6460 */
6461 scsi_print_command(hba->lrb[tag].cmd);
6462 if (!hba->req_abort_count) {
Stanley Chu8808b4e2019-07-10 21:38:21 +08006463 ufshcd_update_reg_hist(&hba->ufs_stats.task_abort, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08006464 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08006465 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08006466 ufshcd_print_pwr_info(hba);
6467 ufshcd_print_trs(hba, 1 << tag, true);
6468 } else {
6469 ufshcd_print_trs(hba, 1 << tag, false);
6470 }
6471 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08006472
6473 /* Skip task abort in case previous aborts failed and report failure */
6474 if (lrbp->req_abort_skip) {
6475 err = -EIO;
6476 goto out;
6477 }
6478
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306479 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6480 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6481 UFS_QUERY_TASK, &resp);
6482 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6483 /* cmd pending in the device */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006484 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
6485 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306486 break;
6487 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306488 /*
6489 * cmd not pending in the device, check if it is
6490 * in transition.
6491 */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006492 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
6493 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306494 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6495 if (reg & (1 << tag)) {
6496 /* sleep for max. 200us to stabilize */
6497 usleep_range(100, 200);
6498 continue;
6499 }
6500 /* command completed already */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006501 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
6502 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306503 goto out;
6504 } else {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006505 dev_err(hba->dev,
6506 "%s: no response from device. tag = %d, err %d\n",
6507 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306508 if (!err)
6509 err = resp; /* service response error */
6510 goto out;
6511 }
6512 }
6513
6514 if (!poll_cnt) {
6515 err = -EBUSY;
6516 goto out;
6517 }
6518
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306519 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6520 UFS_ABORT_TASK, &resp);
6521 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006522 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306523 err = resp; /* service response error */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006524 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
6525 __func__, tag, err);
6526 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306527 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306528 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306529
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306530 err = ufshcd_clear_cmd(hba, tag);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006531 if (err) {
6532 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
6533 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306534 goto out;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006535 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306536
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306537 scsi_dma_unmap(cmd);
6538
6539 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02006540 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306541 hba->lrb[tag].cmd = NULL;
6542 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306543
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306544out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306545 if (!err) {
6546 err = SUCCESS;
6547 } else {
6548 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08006549 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306550 err = FAILED;
6551 }
6552
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006553 /*
6554 * This ufshcd_release() corresponds to the original scsi cmd that got
6555 * aborted here (as we won't get any IRQ for it).
6556 */
6557 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306558 return err;
6559}
6560
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306561/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306562 * ufshcd_host_reset_and_restore - reset and restore host controller
6563 * @hba: per-adapter instance
6564 *
6565 * Note that host controller reset may issue DME_RESET to
6566 * local and remote (device) Uni-Pro stack and the attributes
6567 * are reset to default state.
6568 *
6569 * Returns zero on success, non-zero on failure
6570 */
6571static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6572{
6573 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306574 unsigned long flags;
6575
Can Guo2df74b62019-11-25 22:53:33 -08006576 /*
6577 * Stop the host controller and complete the requests
6578 * cleared by h/w
6579 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07006580 ufshcd_hba_stop(hba);
6581
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306582 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guo2df74b62019-11-25 22:53:33 -08006583 hba->silence_err_logs = true;
6584 ufshcd_complete_requests(hba);
6585 hba->silence_err_logs = false;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306586 spin_unlock_irqrestore(hba->host->host_lock, flags);
6587
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08006588 /* scale up clocks to max frequency before full reinitialization */
Subhash Jadavani394b9492020-03-26 02:25:40 -07006589 ufshcd_set_clk_freq(hba, true);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08006590
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306591 err = ufshcd_hba_enable(hba);
6592 if (err)
6593 goto out;
6594
6595 /* Establish the link again and restore the device */
Bean Huo1b9e2142020-01-20 14:08:15 +01006596 err = ufshcd_probe_hba(hba, false);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006597
6598 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306599 err = -EIO;
6600out:
6601 if (err)
6602 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
Stanley Chu8808b4e2019-07-10 21:38:21 +08006603 ufshcd_update_reg_hist(&hba->ufs_stats.host_reset, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306604 return err;
6605}
6606
6607/**
6608 * ufshcd_reset_and_restore - reset and re-initialize host/device
6609 * @hba: per-adapter instance
6610 *
6611 * Reset and recover device, host and re-establish link. This
6612 * is helpful to recover the communication in fatal error conditions.
6613 *
6614 * Returns zero on success, non-zero on failure
6615 */
6616static int ufshcd_reset_and_restore(struct ufs_hba *hba)
6617{
6618 int err = 0;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006619 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306620
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006621 do {
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07006622 /* Reset the attached device */
6623 ufshcd_vops_device_reset(hba);
6624
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006625 err = ufshcd_host_reset_and_restore(hba);
6626 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306627
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306628 return err;
6629}
6630
6631/**
6632 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006633 * @cmd: SCSI command pointer
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306634 *
6635 * Returns SUCCESS/FAILED
6636 */
6637static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
6638{
6639 int err;
6640 unsigned long flags;
6641 struct ufs_hba *hba;
6642
6643 hba = shost_priv(cmd->device->host);
6644
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006645 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306646 /*
6647 * Check if there is any race with fatal error handling.
6648 * If so, wait for it to complete. Even though fatal error
6649 * handling does reset and restore in some cases, don't assume
6650 * anything out of it. We are just avoiding race here.
6651 */
6652 do {
6653 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306654 if (!(work_pending(&hba->eh_work) ||
Zang Leigang8dc0da72017-06-24 19:14:32 +08006655 hba->ufshcd_state == UFSHCD_STATE_RESET ||
6656 hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306657 break;
6658 spin_unlock_irqrestore(hba->host->host_lock, flags);
6659 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306660 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306661 } while (1);
6662
6663 hba->ufshcd_state = UFSHCD_STATE_RESET;
6664 ufshcd_set_eh_in_progress(hba);
6665 spin_unlock_irqrestore(hba->host->host_lock, flags);
6666
6667 err = ufshcd_reset_and_restore(hba);
6668
6669 spin_lock_irqsave(hba->host->host_lock, flags);
6670 if (!err) {
6671 err = SUCCESS;
6672 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6673 } else {
6674 err = FAILED;
6675 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6676 }
6677 ufshcd_clear_eh_in_progress(hba);
6678 spin_unlock_irqrestore(hba->host->host_lock, flags);
6679
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006680 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306681 return err;
6682}
6683
6684/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006685 * ufshcd_get_max_icc_level - calculate the ICC level
6686 * @sup_curr_uA: max. current supported by the regulator
6687 * @start_scan: row at the desc table to start scan from
6688 * @buff: power descriptor buffer
6689 *
6690 * Returns calculated max ICC level for specific regulator
6691 */
6692static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
6693{
6694 int i;
6695 int curr_uA;
6696 u16 data;
6697 u16 unit;
6698
6699 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02006700 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006701 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
6702 ATTR_ICC_LVL_UNIT_OFFSET;
6703 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
6704 switch (unit) {
6705 case UFSHCD_NANO_AMP:
6706 curr_uA = curr_uA / 1000;
6707 break;
6708 case UFSHCD_MILI_AMP:
6709 curr_uA = curr_uA * 1000;
6710 break;
6711 case UFSHCD_AMP:
6712 curr_uA = curr_uA * 1000 * 1000;
6713 break;
6714 case UFSHCD_MICRO_AMP:
6715 default:
6716 break;
6717 }
6718 if (sup_curr_uA >= curr_uA)
6719 break;
6720 }
6721 if (i < 0) {
6722 i = 0;
6723 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
6724 }
6725
6726 return (u32)i;
6727}
6728
6729/**
6730 * ufshcd_calc_icc_level - calculate the max ICC level
6731 * In case regulators are not initialized we'll return 0
6732 * @hba: per-adapter instance
6733 * @desc_buf: power descriptor buffer to extract ICC levels from.
6734 * @len: length of desc_buff
6735 *
6736 * Returns calculated ICC level
6737 */
6738static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
6739 u8 *desc_buf, int len)
6740{
6741 u32 icc_level = 0;
6742
6743 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
6744 !hba->vreg_info.vccq2) {
6745 dev_err(hba->dev,
6746 "%s: Regulator capability was not set, actvIccLevel=%d",
6747 __func__, icc_level);
6748 goto out;
6749 }
6750
Stanley Chu0487fff2019-03-28 17:16:25 +08006751 if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006752 icc_level = ufshcd_get_max_icc_level(
6753 hba->vreg_info.vcc->max_uA,
6754 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
6755 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
6756
Stanley Chu0487fff2019-03-28 17:16:25 +08006757 if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006758 icc_level = ufshcd_get_max_icc_level(
6759 hba->vreg_info.vccq->max_uA,
6760 icc_level,
6761 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
6762
Stanley Chu0487fff2019-03-28 17:16:25 +08006763 if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006764 icc_level = ufshcd_get_max_icc_level(
6765 hba->vreg_info.vccq2->max_uA,
6766 icc_level,
6767 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
6768out:
6769 return icc_level;
6770}
6771
Can Guoe89860f2020-03-26 02:25:41 -07006772static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006773{
6774 int ret;
Bean Huo7a0bf852020-06-03 11:19:58 +02006775 int buff_len = hba->desc_size[QUERY_DESC_IDN_POWER];
Kees Cookbbe21d72018-05-02 16:58:09 -07006776 u8 *desc_buf;
Can Guoe89860f2020-03-26 02:25:41 -07006777 u32 icc_level;
Kees Cookbbe21d72018-05-02 16:58:09 -07006778
6779 desc_buf = kmalloc(buff_len, GFP_KERNEL);
6780 if (!desc_buf)
6781 return;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006782
Bean Huoc4607a02020-06-03 11:19:56 +02006783 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0,
6784 desc_buf, buff_len);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006785 if (ret) {
6786 dev_err(hba->dev,
6787 "%s: Failed reading power descriptor.len = %d ret = %d",
6788 __func__, buff_len, ret);
Kees Cookbbe21d72018-05-02 16:58:09 -07006789 goto out;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006790 }
6791
Can Guoe89860f2020-03-26 02:25:41 -07006792 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf,
6793 buff_len);
6794 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006795
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02006796 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Can Guoe89860f2020-03-26 02:25:41 -07006797 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006798
6799 if (ret)
6800 dev_err(hba->dev,
6801 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
Can Guoe89860f2020-03-26 02:25:41 -07006802 __func__, icc_level, ret);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006803
Kees Cookbbe21d72018-05-02 16:58:09 -07006804out:
6805 kfree(desc_buf);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006806}
6807
Can Guofb276f72020-03-25 18:09:59 -07006808static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
6809{
6810 scsi_autopm_get_device(sdev);
6811 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev);
6812 if (sdev->rpm_autosuspend)
6813 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev,
6814 RPM_AUTOSUSPEND_DELAY_MS);
6815 scsi_autopm_put_device(sdev);
6816}
6817
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006818/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006819 * ufshcd_scsi_add_wlus - Adds required W-LUs
6820 * @hba: per-adapter instance
6821 *
6822 * UFS device specification requires the UFS devices to support 4 well known
6823 * logical units:
6824 * "REPORT_LUNS" (address: 01h)
6825 * "UFS Device" (address: 50h)
6826 * "RPMB" (address: 44h)
6827 * "BOOT" (address: 30h)
6828 * UFS device's power management needs to be controlled by "POWER CONDITION"
6829 * field of SSU (START STOP UNIT) command. But this "power condition" field
6830 * will take effect only when its sent to "UFS device" well known logical unit
6831 * hence we require the scsi_device instance to represent this logical unit in
6832 * order for the UFS host driver to send the SSU command for power management.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006833 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006834 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
6835 * Block) LU so user space process can control this LU. User space may also
6836 * want to have access to BOOT LU.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006837 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006838 * This function adds scsi device instances for each of all well known LUs
6839 * (except "REPORT LUNS" LU).
6840 *
6841 * Returns zero on success (all required W-LUs are added successfully),
6842 * non-zero error value on failure (if failed to add any of the required W-LU).
6843 */
6844static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
6845{
6846 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006847 struct scsi_device *sdev_rpmb;
6848 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006849
6850 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
6851 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
6852 if (IS_ERR(hba->sdev_ufs_device)) {
6853 ret = PTR_ERR(hba->sdev_ufs_device);
6854 hba->sdev_ufs_device = NULL;
6855 goto out;
6856 }
Can Guofb276f72020-03-25 18:09:59 -07006857 ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006858 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006859
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006860 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006861 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006862 if (IS_ERR(sdev_rpmb)) {
6863 ret = PTR_ERR(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006864 goto remove_sdev_ufs_device;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006865 }
Can Guofb276f72020-03-25 18:09:59 -07006866 ufshcd_blk_pm_runtime_init(sdev_rpmb);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006867 scsi_device_put(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006868
6869 sdev_boot = __scsi_add_device(hba->host, 0, 0,
6870 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Can Guofb276f72020-03-25 18:09:59 -07006871 if (IS_ERR(sdev_boot)) {
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006872 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
Can Guofb276f72020-03-25 18:09:59 -07006873 } else {
6874 ufshcd_blk_pm_runtime_init(sdev_boot);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006875 scsi_device_put(sdev_boot);
Can Guofb276f72020-03-25 18:09:59 -07006876 }
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006877 goto out;
6878
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006879remove_sdev_ufs_device:
6880 scsi_remove_device(hba->sdev_ufs_device);
6881out:
6882 return ret;
6883}
6884
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006885static void ufshcd_wb_probe(struct ufs_hba *hba, u8 *desc_buf)
6886{
Stanley Chua7f1e692020-06-25 11:04:30 +08006887 struct ufs_dev_info *dev_info = &hba->dev_info;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006888 u8 lun;
6889 u32 d_lu_wb_buf_alloc;
6890
Stanley Chu817d7e12020-05-08 16:01:08 +08006891 if (!ufshcd_is_wb_allowed(hba))
6892 return;
Stanley Chua7f1e692020-06-25 11:04:30 +08006893 /*
6894 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or
6895 * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES
6896 * enabled
6897 */
6898 if (!(dev_info->wspecversion >= 0x310 ||
6899 dev_info->wspecversion == 0x220 ||
6900 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES)))
6901 goto wb_disabled;
Stanley Chu817d7e12020-05-08 16:01:08 +08006902
Bean Huo7a0bf852020-06-03 11:19:58 +02006903 if (hba->desc_size[QUERY_DESC_IDN_DEVICE] <
6904 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP + 4)
Stanley Chu817d7e12020-05-08 16:01:08 +08006905 goto wb_disabled;
6906
Stanley Chua7f1e692020-06-25 11:04:30 +08006907 dev_info->d_ext_ufs_feature_sup =
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006908 get_unaligned_be32(desc_buf +
6909 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
Stanley Chu817d7e12020-05-08 16:01:08 +08006910
Stanley Chua7f1e692020-06-25 11:04:30 +08006911 if (!(dev_info->d_ext_ufs_feature_sup & UFS_DEV_WRITE_BOOSTER_SUP))
Stanley Chu817d7e12020-05-08 16:01:08 +08006912 goto wb_disabled;
6913
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006914 /*
6915 * WB may be supported but not configured while provisioning.
6916 * The spec says, in dedicated wb buffer mode,
6917 * a max of 1 lun would have wb buffer configured.
6918 * Now only shared buffer mode is supported.
6919 */
Stanley Chua7f1e692020-06-25 11:04:30 +08006920 dev_info->b_wb_buffer_type =
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006921 desc_buf[DEVICE_DESC_PARAM_WB_TYPE];
6922
Stanley Chua7f1e692020-06-25 11:04:30 +08006923 dev_info->b_presrv_uspc_en =
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006924 desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN];
6925
Stanley Chua7f1e692020-06-25 11:04:30 +08006926 if (dev_info->b_wb_buffer_type == WB_BUF_MODE_SHARED) {
6927 dev_info->d_wb_alloc_units =
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006928 get_unaligned_be32(desc_buf +
6929 DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS);
Stanley Chua7f1e692020-06-25 11:04:30 +08006930 if (!dev_info->d_wb_alloc_units)
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006931 goto wb_disabled;
6932 } else {
6933 for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) {
6934 d_lu_wb_buf_alloc = 0;
6935 ufshcd_read_unit_desc_param(hba,
6936 lun,
6937 UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS,
6938 (u8 *)&d_lu_wb_buf_alloc,
6939 sizeof(d_lu_wb_buf_alloc));
6940 if (d_lu_wb_buf_alloc) {
Stanley Chua7f1e692020-06-25 11:04:30 +08006941 dev_info->wb_dedicated_lu = lun;
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006942 break;
6943 }
6944 }
Stanley Chu817d7e12020-05-08 16:01:08 +08006945
Stanley Chu6f8d5a62020-05-08 16:01:13 +08006946 if (!d_lu_wb_buf_alloc)
6947 goto wb_disabled;
6948 }
Stanley Chu817d7e12020-05-08 16:01:08 +08006949 return;
6950
6951wb_disabled:
6952 hba->caps &= ~UFSHCD_CAP_WB_EN;
6953}
6954
Stanley Chu8db269a2020-05-08 16:01:10 +08006955void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups)
Stanley Chu817d7e12020-05-08 16:01:08 +08006956{
6957 struct ufs_dev_fix *f;
6958 struct ufs_dev_info *dev_info = &hba->dev_info;
6959
Stanley Chu8db269a2020-05-08 16:01:10 +08006960 if (!fixups)
6961 return;
6962
6963 for (f = fixups; f->quirk; f++) {
Stanley Chu817d7e12020-05-08 16:01:08 +08006964 if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
6965 f->wmanufacturerid == UFS_ANY_VENDOR) &&
6966 ((dev_info->model &&
6967 STR_PRFX_EQUAL(f->model, dev_info->model)) ||
6968 !strcmp(f->model, UFS_ANY_MODEL)))
6969 hba->dev_quirks |= f->quirk;
6970 }
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006971}
Stanley Chu8db269a2020-05-08 16:01:10 +08006972EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks);
Asutosh Das3d17b9b2020-04-22 14:41:42 -07006973
Stanley Chuc28c00b2020-05-08 16:01:09 +08006974static void ufs_fixup_device_setup(struct ufs_hba *hba)
6975{
6976 /* fix by general quirk table */
Stanley Chu8db269a2020-05-08 16:01:10 +08006977 ufshcd_fixup_dev_quirks(hba, ufs_fixups);
Stanley Chuc28c00b2020-05-08 16:01:09 +08006978
6979 /* allow vendors to fix quirks */
6980 ufshcd_vops_fixup_dev_quirks(hba);
6981}
6982
Bean Huo09750062020-01-20 14:08:14 +01006983static int ufs_get_device_desc(struct ufs_hba *hba)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006984{
6985 int err;
6986 u8 model_index;
Kees Cookbbe21d72018-05-02 16:58:09 -07006987 u8 *desc_buf;
Bean Huo09750062020-01-20 14:08:14 +01006988 struct ufs_dev_info *dev_info = &hba->dev_info;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006989
Bean Huo458a45f2020-06-03 11:19:55 +02006990 desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
Kees Cookbbe21d72018-05-02 16:58:09 -07006991 if (!desc_buf) {
6992 err = -ENOMEM;
6993 goto out;
6994 }
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006995
Bean Huoc4607a02020-06-03 11:19:56 +02006996 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
Bean Huo7a0bf852020-06-03 11:19:58 +02006997 hba->desc_size[QUERY_DESC_IDN_DEVICE]);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006998 if (err) {
6999 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
7000 __func__, err);
7001 goto out;
7002 }
7003
7004 /*
7005 * getting vendor (manufacturerID) and Bank Index in big endian
7006 * format
7007 */
Bean Huo09750062020-01-20 14:08:14 +01007008 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007009 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
7010
Can Guo09f17792020-02-10 19:40:49 -08007011 /* getting Specification Version in big endian format */
7012 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
7013 desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
7014
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007015 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007016
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007017 err = ufshcd_read_string_desc(hba, model_index,
Bean Huo09750062020-01-20 14:08:14 +01007018 &dev_info->model, SD_ASCII_STD);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007019 if (err < 0) {
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007020 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
7021 __func__, err);
7022 goto out;
7023 }
7024
Stanley Chu817d7e12020-05-08 16:01:08 +08007025 ufs_fixup_device_setup(hba);
7026
Stanley Chua7f1e692020-06-25 11:04:30 +08007027 ufshcd_wb_probe(hba, desc_buf);
Stanley Chu817d7e12020-05-08 16:01:08 +08007028
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007029 /*
7030 * ufshcd_read_string_desc returns size of the string
7031 * reset the error value
7032 */
7033 err = 0;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007034
7035out:
Kees Cookbbe21d72018-05-02 16:58:09 -07007036 kfree(desc_buf);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02007037 return err;
7038}
7039
Bean Huo09750062020-01-20 14:08:14 +01007040static void ufs_put_device_desc(struct ufs_hba *hba)
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007041{
Bean Huo09750062020-01-20 14:08:14 +01007042 struct ufs_dev_info *dev_info = &hba->dev_info;
7043
7044 kfree(dev_info->model);
7045 dev_info->model = NULL;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007046}
7047
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007048/**
Yaniv Gardi37113102016-03-10 17:37:16 +02007049 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
7050 * @hba: per-adapter instance
7051 *
7052 * PA_TActivate parameter can be tuned manually if UniPro version is less than
7053 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
7054 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
7055 * the hibern8 exit latency.
7056 *
7057 * Returns zero on success, non-zero error value on failure.
7058 */
7059static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
7060{
7061 int ret = 0;
7062 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
7063
7064 ret = ufshcd_dme_peer_get(hba,
7065 UIC_ARG_MIB_SEL(
7066 RX_MIN_ACTIVATETIME_CAPABILITY,
7067 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7068 &peer_rx_min_activatetime);
7069 if (ret)
7070 goto out;
7071
7072 /* make sure proper unit conversion is applied */
7073 tuned_pa_tactivate =
7074 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
7075 / PA_TACTIVATE_TIME_UNIT_US);
7076 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7077 tuned_pa_tactivate);
7078
7079out:
7080 return ret;
7081}
7082
7083/**
7084 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
7085 * @hba: per-adapter instance
7086 *
7087 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
7088 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
7089 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
7090 * This optimal value can help reduce the hibern8 exit latency.
7091 *
7092 * Returns zero on success, non-zero error value on failure.
7093 */
7094static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
7095{
7096 int ret = 0;
7097 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
7098 u32 max_hibern8_time, tuned_pa_hibern8time;
7099
7100 ret = ufshcd_dme_get(hba,
7101 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
7102 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
7103 &local_tx_hibern8_time_cap);
7104 if (ret)
7105 goto out;
7106
7107 ret = ufshcd_dme_peer_get(hba,
7108 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
7109 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7110 &peer_rx_hibern8_time_cap);
7111 if (ret)
7112 goto out;
7113
7114 max_hibern8_time = max(local_tx_hibern8_time_cap,
7115 peer_rx_hibern8_time_cap);
7116 /* make sure proper unit conversion is applied */
7117 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
7118 / PA_HIBERN8_TIME_UNIT_US);
7119 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
7120 tuned_pa_hibern8time);
7121out:
7122 return ret;
7123}
7124
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007125/**
7126 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
7127 * less than device PA_TACTIVATE time.
7128 * @hba: per-adapter instance
7129 *
7130 * Some UFS devices require host PA_TACTIVATE to be lower than device
7131 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
7132 * for such devices.
7133 *
7134 * Returns zero on success, non-zero error value on failure.
7135 */
7136static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
7137{
7138 int ret = 0;
7139 u32 granularity, peer_granularity;
7140 u32 pa_tactivate, peer_pa_tactivate;
7141 u32 pa_tactivate_us, peer_pa_tactivate_us;
7142 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
7143
7144 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7145 &granularity);
7146 if (ret)
7147 goto out;
7148
7149 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7150 &peer_granularity);
7151 if (ret)
7152 goto out;
7153
7154 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
7155 (granularity > PA_GRANULARITY_MAX_VAL)) {
7156 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
7157 __func__, granularity);
7158 return -EINVAL;
7159 }
7160
7161 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
7162 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
7163 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
7164 __func__, peer_granularity);
7165 return -EINVAL;
7166 }
7167
7168 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
7169 if (ret)
7170 goto out;
7171
7172 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
7173 &peer_pa_tactivate);
7174 if (ret)
7175 goto out;
7176
7177 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
7178 peer_pa_tactivate_us = peer_pa_tactivate *
7179 gran_to_us_table[peer_granularity - 1];
7180
7181 if (pa_tactivate_us > peer_pa_tactivate_us) {
7182 u32 new_peer_pa_tactivate;
7183
7184 new_peer_pa_tactivate = pa_tactivate_us /
7185 gran_to_us_table[peer_granularity - 1];
7186 new_peer_pa_tactivate++;
7187 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7188 new_peer_pa_tactivate);
7189 }
7190
7191out:
7192 return ret;
7193}
7194
Bean Huo09750062020-01-20 14:08:14 +01007195static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
Yaniv Gardi37113102016-03-10 17:37:16 +02007196{
7197 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
7198 ufshcd_tune_pa_tactivate(hba);
7199 ufshcd_tune_pa_hibern8time(hba);
7200 }
7201
Can Guoe91ed9e2020-02-23 20:09:21 -08007202 ufshcd_vops_apply_dev_quirks(hba);
7203
Yaniv Gardi37113102016-03-10 17:37:16 +02007204 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
7205 /* set 1ms timeout for PA_TACTIVATE */
7206 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08007207
7208 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
7209 ufshcd_quirk_tune_host_pa_tactivate(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02007210}
7211
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007212static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
7213{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007214 hba->ufs_stats.hibern8_exit_cnt = 0;
7215 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08007216 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007217}
7218
Bean Huo731f0622020-01-20 14:08:19 +01007219static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
7220{
7221 int err;
7222 size_t buff_len;
7223 u8 *desc_buf;
7224
Bean Huo7a0bf852020-06-03 11:19:58 +02007225 buff_len = hba->desc_size[QUERY_DESC_IDN_GEOMETRY];
Bean Huo731f0622020-01-20 14:08:19 +01007226 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7227 if (!desc_buf) {
7228 err = -ENOMEM;
7229 goto out;
7230 }
7231
Bean Huoc4607a02020-06-03 11:19:56 +02007232 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
7233 desc_buf, buff_len);
Bean Huo731f0622020-01-20 14:08:19 +01007234 if (err) {
7235 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
7236 __func__, err);
7237 goto out;
7238 }
7239
7240 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
7241 hba->dev_info.max_lu_supported = 32;
7242 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
7243 hba->dev_info.max_lu_supported = 8;
7244
7245out:
7246 kfree(desc_buf);
7247 return err;
7248}
7249
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307250static struct ufs_ref_clk ufs_ref_clk_freqs[] = {
7251 {19200000, REF_CLK_FREQ_19_2_MHZ},
7252 {26000000, REF_CLK_FREQ_26_MHZ},
7253 {38400000, REF_CLK_FREQ_38_4_MHZ},
7254 {52000000, REF_CLK_FREQ_52_MHZ},
7255 {0, REF_CLK_FREQ_INVAL},
7256};
7257
7258static enum ufs_ref_clk_freq
7259ufs_get_bref_clk_from_hz(unsigned long freq)
7260{
7261 int i;
7262
7263 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
7264 if (ufs_ref_clk_freqs[i].freq_hz == freq)
7265 return ufs_ref_clk_freqs[i].val;
7266
7267 return REF_CLK_FREQ_INVAL;
7268}
7269
7270void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
7271{
7272 unsigned long freq;
7273
7274 freq = clk_get_rate(refclk);
7275
7276 hba->dev_ref_clk_freq =
7277 ufs_get_bref_clk_from_hz(freq);
7278
7279 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
7280 dev_err(hba->dev,
7281 "invalid ref_clk setting = %ld\n", freq);
7282}
7283
7284static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
7285{
7286 int err;
7287 u32 ref_clk;
7288 u32 freq = hba->dev_ref_clk_freq;
7289
7290 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
7291 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
7292
7293 if (err) {
7294 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
7295 err);
7296 goto out;
7297 }
7298
7299 if (ref_clk == freq)
7300 goto out; /* nothing to update */
7301
7302 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7303 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
7304
7305 if (err) {
7306 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
7307 ufs_ref_clk_freqs[freq].freq_hz);
7308 goto out;
7309 }
7310
7311 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
7312 ufs_ref_clk_freqs[freq].freq_hz);
7313
7314out:
7315 return err;
7316}
7317
Bean Huo1b9e2142020-01-20 14:08:15 +01007318static int ufshcd_device_params_init(struct ufs_hba *hba)
7319{
7320 bool flag;
Bean Huo7a0bf852020-06-03 11:19:58 +02007321 int ret, i;
Bean Huo1b9e2142020-01-20 14:08:15 +01007322
Bean Huo7a0bf852020-06-03 11:19:58 +02007323 /* Init device descriptor sizes */
7324 for (i = 0; i < QUERY_DESC_IDN_MAX; i++)
7325 hba->desc_size[i] = QUERY_DESC_MAX_SIZE;
Bean Huo1b9e2142020-01-20 14:08:15 +01007326
Bean Huo731f0622020-01-20 14:08:19 +01007327 /* Init UFS geometry descriptor related parameters */
7328 ret = ufshcd_device_geo_params_init(hba);
7329 if (ret)
7330 goto out;
7331
Bean Huo1b9e2142020-01-20 14:08:15 +01007332 /* Check and apply UFS device quirks */
7333 ret = ufs_get_device_desc(hba);
7334 if (ret) {
7335 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
7336 __func__, ret);
7337 goto out;
7338 }
7339
Can Guo09f17792020-02-10 19:40:49 -08007340 ufshcd_get_ref_clk_gating_wait(hba);
7341
Bean Huo1b9e2142020-01-20 14:08:15 +01007342 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
Stanley Chu1f34eed2020-05-08 16:01:12 +08007343 QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag))
Bean Huo1b9e2142020-01-20 14:08:15 +01007344 hba->dev_info.f_power_on_wp_en = flag;
7345
Bean Huo2b35b2a2020-01-20 14:08:16 +01007346 /* Probe maximum power mode co-supported by both UFS host and device */
7347 if (ufshcd_get_max_pwr_mode(hba))
7348 dev_err(hba->dev,
7349 "%s: Failed getting max supported power mode\n",
7350 __func__);
Bean Huo1b9e2142020-01-20 14:08:15 +01007351out:
7352 return ret;
7353}
7354
7355/**
7356 * ufshcd_add_lus - probe and add UFS logical units
7357 * @hba: per-adapter instance
7358 */
7359static int ufshcd_add_lus(struct ufs_hba *hba)
7360{
7361 int ret;
7362
Bean Huo1b9e2142020-01-20 14:08:15 +01007363 /* Add required well known logical units to scsi mid layer */
7364 ret = ufshcd_scsi_add_wlus(hba);
7365 if (ret)
7366 goto out;
7367
7368 /* Initialize devfreq after UFS device is detected */
7369 if (ufshcd_is_clkscaling_supported(hba)) {
7370 memcpy(&hba->clk_scaling.saved_pwr_info.info,
7371 &hba->pwr_info,
7372 sizeof(struct ufs_pa_layer_attr));
7373 hba->clk_scaling.saved_pwr_info.is_valid = true;
7374 if (!hba->devfreq) {
7375 ret = ufshcd_devfreq_init(hba);
7376 if (ret)
7377 goto out;
7378 }
7379
7380 hba->clk_scaling.is_allowed = true;
7381 }
7382
7383 ufs_bsg_probe(hba);
7384 scsi_scan_host(hba->host);
7385 pm_runtime_put_sync(hba->dev);
7386
Bean Huo1b9e2142020-01-20 14:08:15 +01007387out:
7388 return ret;
7389}
7390
Yaniv Gardi37113102016-03-10 17:37:16 +02007391/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007392 * ufshcd_probe_hba - probe hba to detect device and initialize
7393 * @hba: per-adapter instance
Bean Huo1b9e2142020-01-20 14:08:15 +01007394 * @async: asynchronous execution or not
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007395 *
7396 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307397 */
Bean Huo1b9e2142020-01-20 14:08:15 +01007398static int ufshcd_probe_hba(struct ufs_hba *hba, bool async)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307399{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307400 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007401 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307402
7403 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307404 if (ret)
7405 goto out;
7406
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007407 /* Debug counters initialization */
7408 ufshcd_clear_dbg_ufs_stats(hba);
7409
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007410 /* UniPro link is active now */
7411 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05307412
Bean Huo1b9e2142020-01-20 14:08:15 +01007413 /* Verify device initialization by sending NOP OUT UPIU */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307414 ret = ufshcd_verify_dev_init(hba);
7415 if (ret)
7416 goto out;
7417
Bean Huo1b9e2142020-01-20 14:08:15 +01007418 /* Initiate UFS initialization, and waiting until completion */
Dolev Raviv68078d52013-07-30 00:35:58 +05307419 ret = ufshcd_complete_dev_init(hba);
7420 if (ret)
7421 goto out;
7422
Bean Huo1b9e2142020-01-20 14:08:15 +01007423 /*
7424 * Initialize UFS device parameters used by driver, these
7425 * parameters are associated with UFS descriptors.
7426 */
7427 if (async) {
7428 ret = ufshcd_device_params_init(hba);
7429 if (ret)
7430 goto out;
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02007431 }
7432
Bean Huo09750062020-01-20 14:08:14 +01007433 ufshcd_tune_unipro_params(hba);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007434
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007435 /* UFS device is also active now */
7436 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307437 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007438 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307439
Bean Huo2b35b2a2020-01-20 14:08:16 +01007440 /* Gear up to HS gear if supported */
7441 if (hba->max_pwr_info.is_valid) {
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307442 /*
7443 * Set the right value to bRefClkFreq before attempting to
7444 * switch to HS gears.
7445 */
7446 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
7447 ufshcd_set_dev_ref_clk(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007448 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007449 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007450 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7451 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007452 goto out;
7453 }
Can Guo6a9df812020-02-11 21:38:28 -08007454 ufshcd_print_pwr_info(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007455 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007456
Can Guoe89860f2020-03-26 02:25:41 -07007457 /*
7458 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
7459 * and for removable UFS card as well, hence always set the parameter.
7460 * Note: Error handler may issue the device reset hence resetting
7461 * bActiveICCLevel as well so it is always safe to set this here.
7462 */
7463 ufshcd_set_active_icc_lvl(hba);
7464
Yaniv Gardi53c12d02016-02-01 15:02:45 +02007465 /* set the state as operational after switching to desired gear */
7466 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00007467
Asutosh Das3d17b9b2020-04-22 14:41:42 -07007468 ufshcd_wb_config(hba);
Can Guo71d848b2019-11-14 22:09:26 -08007469 /* Enable Auto-Hibernate if configured */
7470 ufshcd_auto_hibern8_enable(hba);
7471
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307472out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007473
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007474 trace_ufshcd_init(dev_name(hba->dev), ret,
7475 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007476 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007477 return ret;
7478}
7479
7480/**
7481 * ufshcd_async_scan - asynchronous execution for probing hba
7482 * @data: data pointer to pass to this function
7483 * @cookie: cookie data
7484 */
7485static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7486{
7487 struct ufs_hba *hba = (struct ufs_hba *)data;
Bean Huo1b9e2142020-01-20 14:08:15 +01007488 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007489
Bean Huo1b9e2142020-01-20 14:08:15 +01007490 /* Initialize hba, detect and initialize UFS device */
7491 ret = ufshcd_probe_hba(hba, true);
7492 if (ret)
7493 goto out;
7494
7495 /* Probe and add UFS logical units */
7496 ret = ufshcd_add_lus(hba);
7497out:
7498 /*
7499 * If we failed to initialize the device or the device is not
7500 * present, turn off the power/clocks etc.
7501 */
7502 if (ret) {
7503 pm_runtime_put_sync(hba->dev);
7504 ufshcd_exit_clk_scaling(hba);
7505 ufshcd_hba_exit(hba);
7506 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307507}
7508
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007509static const struct attribute_group *ufshcd_driver_groups[] = {
7510 &ufs_sysfs_unit_descriptor_group,
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02007511 &ufs_sysfs_lun_attributes_group,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007512 NULL,
7513};
7514
Stanley Chu90b84912020-05-09 17:37:13 +08007515static struct ufs_hba_variant_params ufs_hba_vps = {
7516 .hba_enable_delay_us = 1000,
Stanley Chud14734ae2020-05-09 17:37:15 +08007517 .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40),
Stanley Chu90b84912020-05-09 17:37:13 +08007518 .devfreq_profile.polling_ms = 100,
7519 .devfreq_profile.target = ufshcd_devfreq_target,
7520 .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status,
7521 .ondemand_data.upthreshold = 70,
7522 .ondemand_data.downdifferential = 5,
7523};
7524
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307525static struct scsi_host_template ufshcd_driver_template = {
7526 .module = THIS_MODULE,
7527 .name = UFSHCD,
7528 .proc_name = UFSHCD,
7529 .queuecommand = ufshcd_queuecommand,
7530 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09007531 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307532 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03007533 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307534 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307535 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7536 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307537 .this_id = -1,
7538 .sg_tablesize = SG_ALL,
7539 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
7540 .can_queue = UFSHCD_CAN_QUEUE,
Christoph Hellwig552a9902019-06-17 14:19:55 +02007541 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007542 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007543 .track_queue_depth = 1,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007544 .sdev_groups = ufshcd_driver_groups,
Christoph Hellwig4af14d12018-12-13 16:17:09 +01007545 .dma_boundary = PAGE_SIZE - 1,
Stanley Chu49615ba2019-09-16 23:56:50 +08007546 .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307547};
7548
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007549static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
7550 int ua)
7551{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007552 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007553
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007554 if (!vreg)
7555 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007556
Stanley Chu0487fff2019-03-28 17:16:25 +08007557 /*
7558 * "set_load" operation shall be required on those regulators
7559 * which specifically configured current limitation. Otherwise
7560 * zero max_uA may cause unexpected behavior when regulator is
7561 * enabled or set as high power mode.
7562 */
7563 if (!vreg->max_uA)
7564 return 0;
7565
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007566 ret = regulator_set_load(vreg->reg, ua);
7567 if (ret < 0) {
7568 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
7569 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007570 }
7571
7572 return ret;
7573}
7574
7575static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
7576 struct ufs_vreg *vreg)
7577{
Marc Gonzalez73067982019-02-27 11:41:45 +01007578 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007579}
7580
7581static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
7582 struct ufs_vreg *vreg)
7583{
Adrian Hunter7c7cfdc2019-08-14 15:59:50 +03007584 if (!vreg)
7585 return 0;
7586
Marc Gonzalez73067982019-02-27 11:41:45 +01007587 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007588}
7589
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007590static int ufshcd_config_vreg(struct device *dev,
7591 struct ufs_vreg *vreg, bool on)
7592{
7593 int ret = 0;
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06007594 struct regulator *reg;
7595 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007596 int min_uV, uA_load;
7597
7598 BUG_ON(!vreg);
7599
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06007600 reg = vreg->reg;
7601 name = vreg->name;
7602
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007603 if (regulator_count_voltages(reg) > 0) {
Asutosh Das90d88f42020-02-10 19:40:45 -08007604 uA_load = on ? vreg->max_uA : 0;
7605 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
7606 if (ret)
7607 goto out;
7608
Stanley Chu3b141e82019-03-28 17:16:24 +08007609 if (vreg->min_uV && vreg->max_uV) {
7610 min_uV = on ? vreg->min_uV : 0;
7611 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
7612 if (ret) {
7613 dev_err(dev,
7614 "%s: %s set voltage failed, err=%d\n",
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007615 __func__, name, ret);
Stanley Chu3b141e82019-03-28 17:16:24 +08007616 goto out;
7617 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007618 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007619 }
7620out:
7621 return ret;
7622}
7623
7624static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
7625{
7626 int ret = 0;
7627
Marc Gonzalez73067982019-02-27 11:41:45 +01007628 if (!vreg || vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007629 goto out;
7630
7631 ret = ufshcd_config_vreg(dev, vreg, true);
7632 if (!ret)
7633 ret = regulator_enable(vreg->reg);
7634
7635 if (!ret)
7636 vreg->enabled = true;
7637 else
7638 dev_err(dev, "%s: %s enable failed, err=%d\n",
7639 __func__, vreg->name, ret);
7640out:
7641 return ret;
7642}
7643
7644static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
7645{
7646 int ret = 0;
7647
Marc Gonzalez73067982019-02-27 11:41:45 +01007648 if (!vreg || !vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007649 goto out;
7650
7651 ret = regulator_disable(vreg->reg);
7652
7653 if (!ret) {
7654 /* ignore errors on applying disable config */
7655 ufshcd_config_vreg(dev, vreg, false);
7656 vreg->enabled = false;
7657 } else {
7658 dev_err(dev, "%s: %s disable failed, err=%d\n",
7659 __func__, vreg->name, ret);
7660 }
7661out:
7662 return ret;
7663}
7664
7665static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
7666{
7667 int ret = 0;
7668 struct device *dev = hba->dev;
7669 struct ufs_vreg_info *info = &hba->vreg_info;
7670
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007671 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
7672 if (ret)
7673 goto out;
7674
7675 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
7676 if (ret)
7677 goto out;
7678
7679 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
7680 if (ret)
7681 goto out;
7682
7683out:
7684 if (ret) {
7685 ufshcd_toggle_vreg(dev, info->vccq2, false);
7686 ufshcd_toggle_vreg(dev, info->vccq, false);
7687 ufshcd_toggle_vreg(dev, info->vcc, false);
7688 }
7689 return ret;
7690}
7691
Raviv Shvili6a771a62014-09-25 15:32:24 +03007692static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
7693{
7694 struct ufs_vreg_info *info = &hba->vreg_info;
7695
Zeng Guangyue60b7b822019-03-30 17:03:13 +08007696 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007697}
7698
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007699static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
7700{
7701 int ret = 0;
7702
7703 if (!vreg)
7704 goto out;
7705
7706 vreg->reg = devm_regulator_get(dev, vreg->name);
7707 if (IS_ERR(vreg->reg)) {
7708 ret = PTR_ERR(vreg->reg);
7709 dev_err(dev, "%s: %s get failed, err=%d\n",
7710 __func__, vreg->name, ret);
7711 }
7712out:
7713 return ret;
7714}
7715
7716static int ufshcd_init_vreg(struct ufs_hba *hba)
7717{
7718 int ret = 0;
7719 struct device *dev = hba->dev;
7720 struct ufs_vreg_info *info = &hba->vreg_info;
7721
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007722 ret = ufshcd_get_vreg(dev, info->vcc);
7723 if (ret)
7724 goto out;
7725
7726 ret = ufshcd_get_vreg(dev, info->vccq);
7727 if (ret)
7728 goto out;
7729
7730 ret = ufshcd_get_vreg(dev, info->vccq2);
7731out:
7732 return ret;
7733}
7734
Raviv Shvili6a771a62014-09-25 15:32:24 +03007735static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
7736{
7737 struct ufs_vreg_info *info = &hba->vreg_info;
7738
7739 if (info)
7740 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
7741
7742 return 0;
7743}
7744
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007745static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
7746 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007747{
7748 int ret = 0;
7749 struct ufs_clk_info *clki;
7750 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007751 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007752 ktime_t start = ktime_get();
7753 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007754
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007755 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007756 goto out;
7757
Can Guo38f32422020-02-10 19:40:47 -08007758 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
7759 if (ret)
7760 return ret;
Subhash Jadavani1e879e82016-10-06 21:48:22 -07007761
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007762 list_for_each_entry(clki, head, list) {
7763 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007764 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
7765 continue;
7766
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007767 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007768 if (on && !clki->enabled) {
7769 ret = clk_prepare_enable(clki->clk);
7770 if (ret) {
7771 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
7772 __func__, clki->name, ret);
7773 goto out;
7774 }
7775 } else if (!on && clki->enabled) {
7776 clk_disable_unprepare(clki->clk);
7777 }
7778 clki->enabled = on;
7779 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
7780 clki->name, on ? "en" : "dis");
7781 }
7782 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007783
Can Guo38f32422020-02-10 19:40:47 -08007784 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
7785 if (ret)
7786 return ret;
Subhash Jadavani1e879e82016-10-06 21:48:22 -07007787
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007788out:
7789 if (ret) {
7790 list_for_each_entry(clki, head, list) {
7791 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
7792 clk_disable_unprepare(clki->clk);
7793 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007794 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007795 spin_lock_irqsave(hba->host->host_lock, flags);
7796 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007797 trace_ufshcd_clk_gating(dev_name(hba->dev),
7798 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007799 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007800 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007801
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007802 if (clk_state_changed)
7803 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
7804 (on ? "on" : "off"),
7805 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007806 return ret;
7807}
7808
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007809static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
7810{
7811 return __ufshcd_setup_clocks(hba, on, false);
7812}
7813
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007814static int ufshcd_init_clocks(struct ufs_hba *hba)
7815{
7816 int ret = 0;
7817 struct ufs_clk_info *clki;
7818 struct device *dev = hba->dev;
7819 struct list_head *head = &hba->clk_list_head;
7820
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007821 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007822 goto out;
7823
7824 list_for_each_entry(clki, head, list) {
7825 if (!clki->name)
7826 continue;
7827
7828 clki->clk = devm_clk_get(dev, clki->name);
7829 if (IS_ERR(clki->clk)) {
7830 ret = PTR_ERR(clki->clk);
7831 dev_err(dev, "%s: %s clk get failed, %d\n",
7832 __func__, clki->name, ret);
7833 goto out;
7834 }
7835
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307836 /*
7837 * Parse device ref clk freq as per device tree "ref_clk".
7838 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
7839 * in ufshcd_alloc_host().
7840 */
7841 if (!strcmp(clki->name, "ref_clk"))
7842 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
7843
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007844 if (clki->max_freq) {
7845 ret = clk_set_rate(clki->clk, clki->max_freq);
7846 if (ret) {
7847 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
7848 __func__, clki->name,
7849 clki->max_freq, ret);
7850 goto out;
7851 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03007852 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007853 }
7854 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
7855 clki->name, clk_get_rate(clki->clk));
7856 }
7857out:
7858 return ret;
7859}
7860
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007861static int ufshcd_variant_hba_init(struct ufs_hba *hba)
7862{
7863 int err = 0;
7864
7865 if (!hba->vops)
7866 goto out;
7867
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007868 err = ufshcd_vops_init(hba);
7869 if (err)
7870 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007871
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007872 err = ufshcd_vops_setup_regulators(hba, true);
7873 if (err)
7874 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007875
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007876 goto out;
7877
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007878out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007879 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007880out:
7881 if (err)
7882 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007883 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007884 return err;
7885}
7886
7887static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
7888{
7889 if (!hba->vops)
7890 return;
7891
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007892 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007893
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007894 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007895}
7896
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007897static int ufshcd_hba_init(struct ufs_hba *hba)
7898{
7899 int err;
7900
Raviv Shvili6a771a62014-09-25 15:32:24 +03007901 /*
7902 * Handle host controller power separately from the UFS device power
7903 * rails as it will help controlling the UFS host controller power
7904 * collapse easily which is different than UFS device power collapse.
7905 * Also, enable the host controller power before we go ahead with rest
7906 * of the initialization here.
7907 */
7908 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007909 if (err)
7910 goto out;
7911
Raviv Shvili6a771a62014-09-25 15:32:24 +03007912 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007913 if (err)
7914 goto out;
7915
Raviv Shvili6a771a62014-09-25 15:32:24 +03007916 err = ufshcd_init_clocks(hba);
7917 if (err)
7918 goto out_disable_hba_vreg;
7919
7920 err = ufshcd_setup_clocks(hba, true);
7921 if (err)
7922 goto out_disable_hba_vreg;
7923
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007924 err = ufshcd_init_vreg(hba);
7925 if (err)
7926 goto out_disable_clks;
7927
7928 err = ufshcd_setup_vreg(hba, true);
7929 if (err)
7930 goto out_disable_clks;
7931
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007932 err = ufshcd_variant_hba_init(hba);
7933 if (err)
7934 goto out_disable_vreg;
7935
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007936 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007937 goto out;
7938
7939out_disable_vreg:
7940 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007941out_disable_clks:
7942 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007943out_disable_hba_vreg:
7944 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007945out:
7946 return err;
7947}
7948
7949static void ufshcd_hba_exit(struct ufs_hba *hba)
7950{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007951 if (hba->is_powered) {
7952 ufshcd_variant_hba_exit(hba);
7953 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07007954 ufshcd_suspend_clkscaling(hba);
Vivek Gautameebcc192018-08-07 23:17:39 +05307955 if (ufshcd_is_clkscaling_supported(hba))
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08007956 if (hba->devfreq)
7957 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007958 ufshcd_setup_clocks(hba, false);
7959 ufshcd_setup_hba_vreg(hba, false);
7960 hba->is_powered = false;
Bean Huo09750062020-01-20 14:08:14 +01007961 ufs_put_device_desc(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007962 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007963}
7964
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007965static int
7966ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307967{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007968 unsigned char cmd[6] = {REQUEST_SENSE,
7969 0,
7970 0,
7971 0,
Avri Altman09a5a242018-11-22 20:04:56 +02007972 UFS_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007973 0};
7974 char *buffer;
7975 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307976
Avri Altman09a5a242018-11-22 20:04:56 +02007977 buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007978 if (!buffer) {
7979 ret = -ENOMEM;
7980 goto out;
7981 }
7982
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007983 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
Avri Altman09a5a242018-11-22 20:04:56 +02007984 UFS_SENSE_SIZE, NULL, NULL,
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007985 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007986 if (ret)
7987 pr_err("%s: failed with err %d\n", __func__, ret);
7988
7989 kfree(buffer);
7990out:
7991 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307992}
7993
7994/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007995 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
7996 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307997 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007998 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307999 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008000 * Returns 0 if requested power mode is set successfully
8001 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308002 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008003static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
8004 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308005{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008006 unsigned char cmd[6] = { START_STOP };
8007 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008008 struct scsi_device *sdp;
8009 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008010 int ret;
8011
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008012 spin_lock_irqsave(hba->host->host_lock, flags);
8013 sdp = hba->sdev_ufs_device;
8014 if (sdp) {
8015 ret = scsi_device_get(sdp);
8016 if (!ret && !scsi_device_online(sdp)) {
8017 ret = -ENODEV;
8018 scsi_device_put(sdp);
8019 }
8020 } else {
8021 ret = -ENODEV;
8022 }
8023 spin_unlock_irqrestore(hba->host->host_lock, flags);
8024
8025 if (ret)
8026 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008027
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308028 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008029 * If scsi commands fail, the scsi mid-layer schedules scsi error-
8030 * handling, which would wait for host to be resumed. Since we know
8031 * we are functional while we are here, skip host resume in error
8032 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308033 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008034 hba->host->eh_noresume = 1;
8035 if (hba->wlun_dev_clr_ua) {
8036 ret = ufshcd_send_request_sense(hba, sdp);
8037 if (ret)
8038 goto out;
8039 /* Unit attention condition is cleared now */
8040 hba->wlun_dev_clr_ua = false;
8041 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308042
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008043 cmd[4] = pwr_mode << 4;
8044
8045 /*
8046 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02008047 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008048 * already suspended childs.
8049 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01008050 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
8051 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008052 if (ret) {
8053 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02008054 "START_STOP failed for power mode: %d, result %x\n",
8055 pwr_mode, ret);
Johannes Thumshirnc65be1a2018-06-25 13:20:58 +02008056 if (driver_byte(ret) == DRIVER_SENSE)
Hannes Reinecke21045512015-01-08 07:43:46 +01008057 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008058 }
8059
8060 if (!ret)
8061 hba->curr_dev_pwr_mode = pwr_mode;
8062out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03008063 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008064 hba->host->eh_noresume = 0;
8065 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308066}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308067
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008068static int ufshcd_link_state_transition(struct ufs_hba *hba,
8069 enum uic_link_state req_link_state,
8070 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308071{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008072 int ret = 0;
8073
8074 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308075 return 0;
8076
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008077 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
8078 ret = ufshcd_uic_hibern8_enter(hba);
8079 if (!ret)
8080 ufshcd_set_link_hibern8(hba);
8081 else
8082 goto out;
8083 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308084 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008085 * If autobkops is enabled, link can't be turned off because
8086 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308087 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008088 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
Dan Carpenterdc30c9e2019-12-13 13:49:35 +03008089 (!check_for_bkops || !hba->auto_bkops_enabled)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008090 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008091 * Let's make sure that link is in low power mode, we are doing
8092 * this currently by putting the link in Hibern8. Otherway to
8093 * put the link in low power mode is to send the DME end point
8094 * to device and then send the DME reset command to local
8095 * unipro. But putting the link in hibern8 is much faster.
8096 */
8097 ret = ufshcd_uic_hibern8_enter(hba);
8098 if (ret)
8099 goto out;
8100 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008101 * Change controller state to "reset state" which
8102 * should also put the link in off/reset state
8103 */
Bart Van Assche5cac1092020-05-07 15:27:50 -07008104 ufshcd_hba_stop(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008105 /*
8106 * TODO: Check if we need any delay to make sure that
8107 * controller is reset
8108 */
8109 ufshcd_set_link_off(hba);
8110 }
8111
8112out:
8113 return ret;
8114}
8115
8116static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
8117{
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008118 bool vcc_off = false;
8119
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008120 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02008121 * It seems some UFS devices may keep drawing more than sleep current
8122 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
8123 * To avoid this situation, add 2ms delay before putting these UFS
8124 * rails in LPM mode.
8125 */
8126 if (!ufshcd_is_link_active(hba) &&
8127 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
8128 usleep_range(2000, 2100);
8129
8130 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008131 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
8132 * power.
8133 *
8134 * If UFS device and link is in OFF state, all power supplies (VCC,
8135 * VCCQ, VCCQ2) can be turned off if power on write protect is not
8136 * required. If UFS link is inactive (Hibern8 or OFF state) and device
8137 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
8138 *
8139 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
8140 * in low power state which would save some power.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008141 *
8142 * If Write Booster is enabled and the device needs to flush the WB
8143 * buffer OR if bkops status is urgent for WB, keep Vcc on.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008144 */
8145 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8146 !hba->dev_info.is_lu_power_on_wp) {
8147 ufshcd_setup_vreg(hba, false);
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008148 vcc_off = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008149 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Stanley Chu51dd9052020-05-22 16:32:12 +08008150 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008151 vcc_off = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008152 if (!ufshcd_is_link_active(hba)) {
8153 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8154 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
8155 }
8156 }
Stanley Chuc4df6ee2020-07-29 13:18:39 +08008157
8158 /*
8159 * Some UFS devices require delay after VCC power rail is turned-off.
8160 */
8161 if (vcc_off && hba->vreg_info.vcc &&
8162 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)
8163 usleep_range(5000, 5100);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008164}
8165
8166static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
8167{
8168 int ret = 0;
8169
8170 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8171 !hba->dev_info.is_lu_power_on_wp) {
8172 ret = ufshcd_setup_vreg(hba, true);
8173 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008174 if (!ret && !ufshcd_is_link_active(hba)) {
8175 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
8176 if (ret)
8177 goto vcc_disable;
8178 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
8179 if (ret)
8180 goto vccq_lpm;
8181 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07008182 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008183 }
8184 goto out;
8185
8186vccq_lpm:
8187 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8188vcc_disable:
8189 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8190out:
8191 return ret;
8192}
8193
8194static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
8195{
8196 if (ufshcd_is_link_off(hba))
8197 ufshcd_setup_hba_vreg(hba, false);
8198}
8199
8200static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
8201{
8202 if (ufshcd_is_link_off(hba))
8203 ufshcd_setup_hba_vreg(hba, true);
8204}
8205
8206/**
8207 * ufshcd_suspend - helper function for suspend operations
8208 * @hba: per adapter instance
8209 * @pm_op: desired low power operation type
8210 *
8211 * This function will try to put the UFS device and link into low power
8212 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
8213 * (System PM level).
8214 *
8215 * If this function is called during shutdown, it will make sure that
8216 * both UFS device and UFS link is powered off.
8217 *
8218 * NOTE: UFS device & link must be active before we enter in this function.
8219 *
8220 * Returns 0 for success and non-zero for failure
8221 */
8222static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8223{
8224 int ret = 0;
8225 enum ufs_pm_level pm_lvl;
8226 enum ufs_dev_pwr_mode req_dev_pwr_mode;
8227 enum uic_link_state req_link_state;
8228
8229 hba->pm_op_in_progress = 1;
8230 if (!ufshcd_is_shutdown_pm(pm_op)) {
8231 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
8232 hba->rpm_lvl : hba->spm_lvl;
8233 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
8234 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
8235 } else {
8236 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
8237 req_link_state = UIC_LINK_OFF_STATE;
8238 }
8239
8240 /*
8241 * If we can't transition into any of the low power modes
8242 * just gate the clocks.
8243 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008244 ufshcd_hold(hba, false);
8245 hba->clk_gating.is_suspended = true;
8246
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008247 if (hba->clk_scaling.is_allowed) {
8248 cancel_work_sync(&hba->clk_scaling.suspend_work);
8249 cancel_work_sync(&hba->clk_scaling.resume_work);
8250 ufshcd_suspend_clkscaling(hba);
8251 }
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008252
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008253 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8254 req_link_state == UIC_LINK_ACTIVE_STATE) {
8255 goto disable_clks;
8256 }
8257
8258 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8259 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008260 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008261
8262 /* UFS device & link must be active before we enter in this function */
8263 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8264 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07008265 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008266 }
8267
8268 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03008269 if (ufshcd_can_autobkops_during_suspend(hba)) {
8270 /*
8271 * The device is idle with no requests in the queue,
8272 * allow background operations if bkops status shows
8273 * that performance might be impacted.
8274 */
8275 ret = ufshcd_urgent_bkops(hba);
8276 if (ret)
8277 goto enable_gating;
8278 } else {
8279 /* make sure that auto bkops is disabled */
8280 ufshcd_disable_auto_bkops(hba);
8281 }
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008282 /*
Stanley Chu51dd9052020-05-22 16:32:12 +08008283 * If device needs to do BKOP or WB buffer flush during
8284 * Hibern8, keep device power mode as "active power mode"
8285 * and VCC supply.
Asutosh Das3d17b9b2020-04-22 14:41:42 -07008286 */
Stanley Chu51dd9052020-05-22 16:32:12 +08008287 hba->dev_info.b_rpm_dev_flush_capable =
8288 hba->auto_bkops_enabled ||
8289 (((req_link_state == UIC_LINK_HIBERN8_STATE) ||
8290 ((req_link_state == UIC_LINK_ACTIVE_STATE) &&
8291 ufshcd_is_auto_hibern8_enabled(hba))) &&
8292 ufshcd_wb_need_flush(hba));
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008293 }
8294
Stanley Chu51dd9052020-05-22 16:32:12 +08008295 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) {
8296 if ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
8297 !ufshcd_is_runtime_pm(pm_op)) {
8298 /* ensure that bkops is disabled */
8299 ufshcd_disable_auto_bkops(hba);
8300 }
8301
8302 if (!hba->dev_info.b_rpm_dev_flush_capable) {
8303 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8304 if (ret)
8305 goto enable_gating;
8306 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008307 }
8308
Sayali Lokhande2824ec92020-02-10 19:40:44 -08008309 flush_work(&hba->eeh_work);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008310 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
8311 if (ret)
8312 goto set_dev_active;
8313
8314 ufshcd_vreg_set_lpm(hba);
8315
8316disable_clks:
8317 /*
8318 * Call vendor specific suspend callback. As these callbacks may access
8319 * vendor specific host controller register space call them before the
8320 * host clocks are ON.
8321 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008322 ret = ufshcd_vops_suspend(hba, pm_op);
8323 if (ret)
8324 goto set_link_active;
Stanley Chudcb6cec2019-12-07 20:22:00 +08008325 /*
8326 * Disable the host irq as host controller as there won't be any
8327 * host controller transaction expected till resume.
8328 */
8329 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008330
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008331 if (!ufshcd_is_link_active(hba))
8332 ufshcd_setup_clocks(hba, false);
8333 else
8334 /* If link is active, device ref_clk can't be switched off */
8335 __ufshcd_setup_clocks(hba, false, true);
8336
Can Guo2dec9472020-08-09 05:15:47 -07008337 if (ufshcd_is_clkgating_allowed(hba)) {
8338 hba->clk_gating.state = CLKS_OFF;
8339 trace_ufshcd_clk_gating(dev_name(hba->dev),
8340 hba->clk_gating.state);
8341 }
Stanley Chudcb6cec2019-12-07 20:22:00 +08008342
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008343 /* Put the host controller in low power mode if possible */
8344 ufshcd_hba_vreg_set_lpm(hba);
8345 goto out;
8346
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008347set_link_active:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008348 if (hba->clk_scaling.is_allowed)
8349 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008350 ufshcd_vreg_set_hpm(hba);
8351 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
8352 ufshcd_set_link_active(hba);
8353 else if (ufshcd_is_link_off(hba))
8354 ufshcd_host_reset_and_restore(hba);
8355set_dev_active:
8356 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8357 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008358enable_gating:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008359 if (hba->clk_scaling.is_allowed)
8360 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008361 hba->clk_gating.is_suspended = false;
Stanley Chu51dd9052020-05-22 16:32:12 +08008362 hba->dev_info.b_rpm_dev_flush_capable = false;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008363 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008364out:
Stanley Chu51dd9052020-05-22 16:32:12 +08008365 if (hba->dev_info.b_rpm_dev_flush_capable) {
8366 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work,
8367 msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS));
8368 }
8369
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008370 hba->pm_op_in_progress = 0;
Stanley Chu51dd9052020-05-22 16:32:12 +08008371
Stanley Chu8808b4e2019-07-10 21:38:21 +08008372 if (ret)
8373 ufshcd_update_reg_hist(&hba->ufs_stats.suspend_err, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008374 return ret;
8375}
8376
8377/**
8378 * ufshcd_resume - helper function for resume operations
8379 * @hba: per adapter instance
8380 * @pm_op: runtime PM or system PM
8381 *
8382 * This function basically brings the UFS device, UniPro link and controller
8383 * to active state.
8384 *
8385 * Returns 0 for success and non-zero for failure
8386 */
8387static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8388{
8389 int ret;
8390 enum uic_link_state old_link_state;
8391
8392 hba->pm_op_in_progress = 1;
8393 old_link_state = hba->uic_link_state;
8394
8395 ufshcd_hba_vreg_set_hpm(hba);
8396 /* Make sure clocks are enabled before accessing controller */
8397 ret = ufshcd_setup_clocks(hba, true);
8398 if (ret)
8399 goto out;
8400
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008401 /* enable the host irq as host controller would be active soon */
Can Guo5231d382019-12-05 02:14:46 +00008402 ufshcd_enable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008403
8404 ret = ufshcd_vreg_set_hpm(hba);
8405 if (ret)
8406 goto disable_irq_and_vops_clks;
8407
8408 /*
8409 * Call vendor specific resume callback. As these callbacks may access
8410 * vendor specific host controller register space call them when the
8411 * host clocks are ON.
8412 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008413 ret = ufshcd_vops_resume(hba, pm_op);
8414 if (ret)
8415 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008416
8417 if (ufshcd_is_link_hibern8(hba)) {
8418 ret = ufshcd_uic_hibern8_exit(hba);
8419 if (!ret)
8420 ufshcd_set_link_active(hba);
8421 else
8422 goto vendor_suspend;
8423 } else if (ufshcd_is_link_off(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008424 /*
Asutosh Das089f5b62020-04-13 23:14:48 -07008425 * A full initialization of the host and the device is
8426 * required since the link was put to off during suspend.
8427 */
8428 ret = ufshcd_reset_and_restore(hba);
8429 /*
8430 * ufshcd_reset_and_restore() should have already
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008431 * set the link state as active
8432 */
8433 if (ret || !ufshcd_is_link_active(hba))
8434 goto vendor_suspend;
8435 }
8436
8437 if (!ufshcd_is_ufs_dev_active(hba)) {
8438 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8439 if (ret)
8440 goto set_old_link_state;
8441 }
8442
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08008443 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8444 ufshcd_enable_auto_bkops(hba);
8445 else
8446 /*
8447 * If BKOPs operations are urgently needed at this moment then
8448 * keep auto-bkops enabled or else disable it.
8449 */
8450 ufshcd_urgent_bkops(hba);
8451
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008452 hba->clk_gating.is_suspended = false;
8453
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008454 if (hba->clk_scaling.is_allowed)
8455 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008456
Adrian Hunterad448372018-03-20 15:07:38 +02008457 /* Enable Auto-Hibernate if configured */
8458 ufshcd_auto_hibern8_enable(hba);
8459
Stanley Chu51dd9052020-05-22 16:32:12 +08008460 if (hba->dev_info.b_rpm_dev_flush_capable) {
8461 hba->dev_info.b_rpm_dev_flush_capable = false;
8462 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
8463 }
8464
Can Guo71d848b2019-11-14 22:09:26 -08008465 /* Schedule clock gating in case of no access to UFS device yet */
8466 ufshcd_release(hba);
8467
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008468 goto out;
8469
8470set_old_link_state:
8471 ufshcd_link_state_transition(hba, old_link_state, 0);
8472vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008473 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008474disable_vreg:
8475 ufshcd_vreg_set_lpm(hba);
8476disable_irq_and_vops_clks:
8477 ufshcd_disable_irq(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008478 if (hba->clk_scaling.is_allowed)
8479 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008480 ufshcd_setup_clocks(hba, false);
Can Guo2dec9472020-08-09 05:15:47 -07008481 if (ufshcd_is_clkgating_allowed(hba)) {
8482 hba->clk_gating.state = CLKS_OFF;
8483 trace_ufshcd_clk_gating(dev_name(hba->dev),
8484 hba->clk_gating.state);
8485 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008486out:
8487 hba->pm_op_in_progress = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08008488 if (ret)
8489 ufshcd_update_reg_hist(&hba->ufs_stats.resume_err, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008490 return ret;
8491}
8492
8493/**
8494 * ufshcd_system_suspend - system suspend routine
8495 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008496 *
8497 * Check the description of ufshcd_suspend() function for more details.
8498 *
8499 * Returns 0 for success and non-zero for failure
8500 */
8501int ufshcd_system_suspend(struct ufs_hba *hba)
8502{
8503 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008504 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008505
8506 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03008507 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008508
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008509 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8510 hba->curr_dev_pwr_mode) &&
8511 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8512 hba->uic_link_state))
8513 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008514
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008515 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008516 /*
8517 * UFS device and/or UFS link low power states during runtime
8518 * suspend seems to be different than what is expected during
8519 * system suspend. Hence runtime resume the devic & link and
8520 * let the system suspend low power states to take effect.
8521 * TODO: If resume takes longer time, we might have optimize
8522 * it in future by not resuming everything if possible.
8523 */
8524 ret = ufshcd_runtime_resume(hba);
8525 if (ret)
8526 goto out;
8527 }
8528
8529 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
8530out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008531 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
8532 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008533 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03008534 if (!ret)
8535 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008536 return ret;
8537}
8538EXPORT_SYMBOL(ufshcd_system_suspend);
8539
8540/**
8541 * ufshcd_system_resume - system resume routine
8542 * @hba: per adapter instance
8543 *
8544 * Returns 0 for success and non-zero for failure
8545 */
8546
8547int ufshcd_system_resume(struct ufs_hba *hba)
8548{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008549 int ret = 0;
8550 ktime_t start = ktime_get();
8551
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008552 if (!hba)
8553 return -EINVAL;
8554
8555 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008556 /*
8557 * Let the runtime resume take care of resuming
8558 * if runtime suspended.
8559 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008560 goto out;
8561 else
8562 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
8563out:
8564 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
8565 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008566 hba->curr_dev_pwr_mode, hba->uic_link_state);
Stanley Chuce9e7bc2019-01-07 22:19:34 +08008567 if (!ret)
8568 hba->is_sys_suspended = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008569 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008570}
8571EXPORT_SYMBOL(ufshcd_system_resume);
8572
8573/**
8574 * ufshcd_runtime_suspend - runtime suspend routine
8575 * @hba: per adapter instance
8576 *
8577 * Check the description of ufshcd_suspend() function for more details.
8578 *
8579 * Returns 0 for success and non-zero for failure
8580 */
8581int ufshcd_runtime_suspend(struct ufs_hba *hba)
8582{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008583 int ret = 0;
8584 ktime_t start = ktime_get();
8585
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008586 if (!hba)
8587 return -EINVAL;
8588
8589 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008590 goto out;
8591 else
8592 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
8593out:
8594 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
8595 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008596 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008597 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308598}
8599EXPORT_SYMBOL(ufshcd_runtime_suspend);
8600
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008601/**
8602 * ufshcd_runtime_resume - runtime resume routine
8603 * @hba: per adapter instance
8604 *
8605 * This function basically brings the UFS device, UniPro link and controller
8606 * to active state. Following operations are done in this function:
8607 *
8608 * 1. Turn on all the controller related clocks
8609 * 2. Bring the UniPro link out of Hibernate state
8610 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
8611 * to active state.
8612 * 4. If auto-bkops is enabled on the device, disable it.
8613 *
8614 * So following would be the possible power state after this function return
8615 * successfully:
8616 * S1: UFS device in Active state with VCC rail ON
8617 * UniPro link in Active state
8618 * All the UFS/UniPro controller clocks are ON
8619 *
8620 * Returns 0 for success and non-zero for failure
8621 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308622int ufshcd_runtime_resume(struct ufs_hba *hba)
8623{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008624 int ret = 0;
8625 ktime_t start = ktime_get();
8626
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008627 if (!hba)
8628 return -EINVAL;
8629
8630 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008631 goto out;
8632 else
8633 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
8634out:
8635 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
8636 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008637 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008638 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308639}
8640EXPORT_SYMBOL(ufshcd_runtime_resume);
8641
8642int ufshcd_runtime_idle(struct ufs_hba *hba)
8643{
8644 return 0;
8645}
8646EXPORT_SYMBOL(ufshcd_runtime_idle);
8647
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308648/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008649 * ufshcd_shutdown - shutdown routine
8650 * @hba: per adapter instance
8651 *
8652 * This function would power off both UFS device and UFS link.
8653 *
8654 * Returns 0 always to allow force shutdown even in case of errors.
8655 */
8656int ufshcd_shutdown(struct ufs_hba *hba)
8657{
8658 int ret = 0;
8659
Stanley Chuf51913e2019-09-18 12:20:38 +08008660 if (!hba->is_powered)
8661 goto out;
8662
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008663 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
8664 goto out;
8665
8666 if (pm_runtime_suspended(hba->dev)) {
8667 ret = ufshcd_runtime_resume(hba);
8668 if (ret)
8669 goto out;
8670 }
8671
8672 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
8673out:
8674 if (ret)
8675 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
8676 /* allow force shutdown even in case of errors */
8677 return 0;
8678}
8679EXPORT_SYMBOL(ufshcd_shutdown);
8680
8681/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308682 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308683 * data structure memory
Bart Van Assche8aa29f12018-03-01 15:07:20 -08008684 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308685 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308686void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308687{
Avri Altmandf032bf2018-10-07 17:30:35 +03008688 ufs_bsg_remove(hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02008689 ufs_sysfs_remove_nodes(hba->dev);
Bart Van Assche69a6c262019-12-09 10:13:09 -08008690 blk_cleanup_queue(hba->tmf_queue);
8691 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08008692 blk_cleanup_queue(hba->cmd_queue);
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05308693 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308694 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308695 ufshcd_disable_intr(hba, hba->intr_mask);
Bart Van Assche5cac1092020-05-07 15:27:50 -07008696 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308697
Vivek Gautameebcc192018-08-07 23:17:39 +05308698 ufshcd_exit_clk_scaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008699 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008700 if (ufshcd_is_clkscaling_supported(hba))
8701 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008702 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308703}
8704EXPORT_SYMBOL_GPL(ufshcd_remove);
8705
8706/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02008707 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
8708 * @hba: pointer to Host Bus Adapter (HBA)
8709 */
8710void ufshcd_dealloc_host(struct ufs_hba *hba)
8711{
Satya Tangiraladf043c742020-07-06 20:04:14 +00008712 ufshcd_crypto_destroy_keyslot_manager(hba);
Yaniv Gardi47555a52015-10-28 13:15:49 +02008713 scsi_host_put(hba->host);
8714}
8715EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
8716
8717/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008718 * ufshcd_set_dma_mask - Set dma mask based on the controller
8719 * addressing capability
8720 * @hba: per adapter instance
8721 *
8722 * Returns 0 for success, non-zero for failure
8723 */
8724static int ufshcd_set_dma_mask(struct ufs_hba *hba)
8725{
8726 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
8727 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
8728 return 0;
8729 }
8730 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
8731}
8732
8733/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008734 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308735 * @dev: pointer to device handle
8736 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308737 * Returns 0 on success, non-zero value on failure
8738 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008739int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308740{
8741 struct Scsi_Host *host;
8742 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008743 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308744
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308745 if (!dev) {
8746 dev_err(dev,
8747 "Invalid memory reference for dev is NULL\n");
8748 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308749 goto out_error;
8750 }
8751
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308752 host = scsi_host_alloc(&ufshcd_driver_template,
8753 sizeof(struct ufs_hba));
8754 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308755 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308756 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308757 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308758 }
8759 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308760 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308761 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008762 *hba_handle = hba;
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05308763 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008764
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03008765 INIT_LIST_HEAD(&hba->clk_list_head);
8766
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008767out_error:
8768 return err;
8769}
8770EXPORT_SYMBOL(ufshcd_alloc_host);
8771
Bart Van Assche69a6c262019-12-09 10:13:09 -08008772/* This function exists because blk_mq_alloc_tag_set() requires this. */
8773static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
8774 const struct blk_mq_queue_data *qd)
8775{
8776 WARN_ON_ONCE(true);
8777 return BLK_STS_NOTSUPP;
8778}
8779
8780static const struct blk_mq_ops ufshcd_tmf_ops = {
8781 .queue_rq = ufshcd_queue_tmf,
8782};
8783
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008784/**
8785 * ufshcd_init - Driver initialization routine
8786 * @hba: per-adapter instance
8787 * @mmio_base: base register address
8788 * @irq: Interrupt line of device
8789 * Returns 0 on success, non-zero value on failure
8790 */
8791int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
8792{
8793 int err;
8794 struct Scsi_Host *host = hba->host;
8795 struct device *dev = hba->dev;
8796
8797 if (!mmio_base) {
8798 dev_err(hba->dev,
8799 "Invalid memory reference for mmio_base is NULL\n");
8800 err = -ENODEV;
8801 goto out_error;
8802 }
8803
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308804 hba->mmio_base = mmio_base;
8805 hba->irq = irq;
Stanley Chu90b84912020-05-09 17:37:13 +08008806 hba->vps = &ufs_hba_vps;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308807
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008808 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008809 if (err)
8810 goto out_error;
8811
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308812 /* Read capabilities registers */
Satya Tangiraladf043c742020-07-06 20:04:14 +00008813 err = ufshcd_hba_capabilities(hba);
8814 if (err)
8815 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308816
8817 /* Get UFS version supported by the controller */
8818 hba->ufs_version = ufshcd_get_ufs_version(hba);
8819
Yaniv Gardic01848c2016-12-05 19:25:02 -08008820 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
8821 (hba->ufs_version != UFSHCI_VERSION_11) &&
8822 (hba->ufs_version != UFSHCI_VERSION_20) &&
8823 (hba->ufs_version != UFSHCI_VERSION_21))
8824 dev_err(hba->dev, "invalid UFS version 0x%x\n",
8825 hba->ufs_version);
8826
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308827 /* Get Interrupt bit mask per version */
8828 hba->intr_mask = ufshcd_get_intr_mask(hba);
8829
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008830 err = ufshcd_set_dma_mask(hba);
8831 if (err) {
8832 dev_err(hba->dev, "set dma mask failed\n");
8833 goto out_disable;
8834 }
8835
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308836 /* Allocate memory for host memory space */
8837 err = ufshcd_memory_alloc(hba);
8838 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308839 dev_err(hba->dev, "Memory allocation failed\n");
8840 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308841 }
8842
8843 /* Configure LRB */
8844 ufshcd_host_memory_configure(hba);
8845
8846 host->can_queue = hba->nutrs;
8847 host->cmd_per_lun = hba->nutrs;
8848 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03008849 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308850 host->max_channel = UFSHCD_MAX_CHANNEL;
8851 host->unique_id = host->host_no;
Avri Altmana851b2b2018-10-07 17:30:34 +03008852 host->max_cmd_len = UFS_CDB_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308853
Dolev Raviv7eb584d2014-09-25 15:32:31 +03008854 hba->max_pwr_info.is_valid = false;
8855
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308856 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05308857 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308858 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308859
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308860 /* Initialize UIC command mutex */
8861 mutex_init(&hba->uic_cmd_mutex);
8862
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05308863 /* Initialize mutex for device management commands */
8864 mutex_init(&hba->dev_cmd.lock);
8865
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08008866 init_rwsem(&hba->clk_scaling_lock);
8867
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008868 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02008869
Vivek Gautameebcc192018-08-07 23:17:39 +05308870 ufshcd_init_clk_scaling(hba);
8871
Yaniv Gardi199ef132016-03-10 17:37:06 +02008872 /*
8873 * In order to avoid any spurious interrupt immediately after
8874 * registering UFS controller interrupt handler, clear any pending UFS
8875 * interrupt status and disable all the UFS interrupts.
8876 */
8877 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
8878 REG_INTERRUPT_STATUS);
8879 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
8880 /*
8881 * Make sure that UFS interrupts are disabled and any pending interrupt
8882 * status is cleared before registering UFS interrupt handler.
8883 */
8884 mb();
8885
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308886 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09008887 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308888 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308889 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008890 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008891 } else {
8892 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308893 }
8894
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308895 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308896 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308897 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008898 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308899 }
8900
Bart Van Assche7252a362019-12-09 10:13:08 -08008901 hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set);
8902 if (IS_ERR(hba->cmd_queue)) {
8903 err = PTR_ERR(hba->cmd_queue);
8904 goto out_remove_scsi_host;
8905 }
8906
Bart Van Assche69a6c262019-12-09 10:13:09 -08008907 hba->tmf_tag_set = (struct blk_mq_tag_set) {
8908 .nr_hw_queues = 1,
8909 .queue_depth = hba->nutmrs,
8910 .ops = &ufshcd_tmf_ops,
8911 .flags = BLK_MQ_F_NO_SCHED,
8912 };
8913 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
8914 if (err < 0)
8915 goto free_cmd_queue;
8916 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set);
8917 if (IS_ERR(hba->tmf_queue)) {
8918 err = PTR_ERR(hba->tmf_queue);
8919 goto free_tmf_tag_set;
8920 }
8921
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07008922 /* Reset the attached device */
8923 ufshcd_vops_device_reset(hba);
8924
Satya Tangiraladf043c742020-07-06 20:04:14 +00008925 ufshcd_init_crypto(hba);
8926
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308927 /* Host controller enable */
8928 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308929 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308930 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08008931 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08008932 ufshcd_print_host_state(hba);
Bart Van Assche69a6c262019-12-09 10:13:09 -08008933 goto free_tmf_queue;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308934 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308935
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08008936 /*
8937 * Set the default power management level for runtime and system PM.
8938 * Default power saving mode is to keep UFS link in Hibern8 state
8939 * and UFS device in sleep state.
8940 */
8941 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
8942 UFS_SLEEP_PWR_MODE,
8943 UIC_LINK_HIBERN8_STATE);
8944 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
8945 UFS_SLEEP_PWR_MODE,
8946 UIC_LINK_HIBERN8_STATE);
8947
Stanley Chu51dd9052020-05-22 16:32:12 +08008948 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work,
8949 ufshcd_rpm_dev_flush_recheck_work);
8950
Adrian Hunterad448372018-03-20 15:07:38 +02008951 /* Set the default auto-hiberate idle timer value to 150 ms */
Stanley Chuf571b372019-05-21 14:44:53 +08008952 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
Adrian Hunterad448372018-03-20 15:07:38 +02008953 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
8954 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
8955 }
8956
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05308957 /* Hold auto suspend until async scan completes */
8958 pm_runtime_get_sync(dev);
Subhash Jadavani38135532018-05-03 16:37:18 +05308959 atomic_set(&hba->scsi_block_reqs_cnt, 0);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008960 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08008961 * We are assuming that device wasn't put in sleep/power-down
8962 * state exclusively during the boot stage before kernel.
8963 * This assumption helps avoid doing link startup twice during
8964 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008965 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08008966 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008967
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308968 async_schedule(ufshcd_async_scan, hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02008969 ufs_sysfs_add_nodes(hba->dev);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308970
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308971 return 0;
8972
Bart Van Assche69a6c262019-12-09 10:13:09 -08008973free_tmf_queue:
8974 blk_cleanup_queue(hba->tmf_queue);
8975free_tmf_tag_set:
8976 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08008977free_cmd_queue:
8978 blk_cleanup_queue(hba->cmd_queue);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308979out_remove_scsi_host:
8980 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008981exit_gating:
Vivek Gautameebcc192018-08-07 23:17:39 +05308982 ufshcd_exit_clk_scaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008983 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308984out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008985 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008986 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308987out_error:
8988 return err;
8989}
8990EXPORT_SYMBOL_GPL(ufshcd_init);
8991
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308992MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
8993MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05308994MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308995MODULE_LICENSE("GPL");
8996MODULE_VERSION(UFSHCD_DRIVER_VERSION);