1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Universal Flash Storage Host controller driver Core
4 * Copyright (C) 2011-2013 Samsung India Software Operations
5 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
12 #include <linux/async.h>
13 #include <linux/devfreq.h>
14 #include <linux/nls.h>
16 #include <linux/bitfield.h>
17 #include <linux/blk-pm.h>
18 #include <linux/blkdev.h>
20 #include "ufs_quirks.h"
22 #include "ufs-sysfs.h"
24 #include "ufshcd-crypto.h"
25 #include <asm/unaligned.h>
26 #include <linux/blkdev.h>
28 #define CREATE_TRACE_POINTS
29 #include <trace/events/ufs.h>
31 #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
34 /* UIC command timeout, unit: ms */
35 #define UIC_CMD_TIMEOUT 500
37 /* NOP OUT retries waiting for NOP IN response */
38 #define NOP_OUT_RETRIES 10
39 /* Timeout after 50 msecs if NOP OUT hangs without response */
40 #define NOP_OUT_TIMEOUT 50 /* msecs */
42 /* Query request retries */
43 #define QUERY_REQ_RETRIES 3
44 /* Query request timeout */
45 #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
47 /* Task management command timeout */
48 #define TM_CMD_TIMEOUT 100 /* msecs */
50 /* maximum number of retries for a general UIC command */
51 #define UFS_UIC_COMMAND_RETRIES 3
53 /* maximum number of link-startup retries */
54 #define DME_LINKSTARTUP_RETRIES 3
56 /* Maximum retries for Hibern8 enter */
57 #define UIC_HIBERN8_ENTER_RETRIES 3
59 /* maximum number of reset retries before giving up */
60 #define MAX_HOST_RESET_RETRIES 5
62 /* Expose the flag value from utp_upiu_query.value */
63 #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
65 /* Interrupt aggregation default timeout, unit: 40us */
66 #define INT_AGGR_DEF_TO 0x02
68 /* default delay of autosuspend: 2000 ms */
69 #define RPM_AUTOSUSPEND_DELAY_MS 2000
71 /* Default delay of RPM device flush delayed work */
72 #define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000
74 /* Default value of wait time before gating device ref clock */
75 #define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */
77 /* Polling time to wait for fDeviceInit */
78 #define FDEVICEINIT_COMPL_TIMEOUT 1500 /* millisecs */
80 #define ufshcd_toggle_vreg(_dev, _vreg, _on) \
84 _ret = ufshcd_enable_vreg(_dev, _vreg); \
86 _ret = ufshcd_disable_vreg(_dev, _vreg); \
90 #define ufshcd_hex_dump(prefix_str, buf, len) do { \
91 size_t __len = (len); \
92 print_hex_dump(KERN_ERR, prefix_str, \
93 __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\
94 16, 4, buf, __len, false); \
97 int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
103 if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */
106 regs = kzalloc(len, GFP_ATOMIC);
110 for (pos = 0; pos < len; pos += 4)
111 regs[pos / 4] = ufshcd_readl(hba, offset + pos);
113 ufshcd_hex_dump(prefix, regs, len);
118 EXPORT_SYMBOL_GPL(ufshcd_dump_regs);
121 UFSHCD_MAX_CHANNEL = 0,
123 UFSHCD_CMD_PER_LUN = 32,
124 UFSHCD_CAN_QUEUE = 32,
131 UFSHCD_STATE_OPERATIONAL,
132 UFSHCD_STATE_EH_SCHEDULED_FATAL,
133 UFSHCD_STATE_EH_SCHEDULED_NON_FATAL,
136 /* UFSHCD error handling flags */
138 UFSHCD_EH_IN_PROGRESS = (1 << 0),
141 /* UFSHCD UIC layer error flags */
143 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
144 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
145 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
146 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
147 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
148 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
149 UFSHCD_UIC_PA_GENERIC_ERROR = (1 << 6), /* Generic PA error */
152 #define ufshcd_set_eh_in_progress(h) \
153 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
154 #define ufshcd_eh_in_progress(h) \
155 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
156 #define ufshcd_clear_eh_in_progress(h) \
157 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
159 struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
160 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
161 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
162 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
163 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
164 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
165 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
167 * For DeepSleep, the link is first put in hibern8 and then off.
168 * Leaving the link in hibern8 is not supported.
170 {UFS_DEEPSLEEP_PWR_MODE, UIC_LINK_OFF_STATE},
173 static inline enum ufs_dev_pwr_mode
174 ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
176 return ufs_pm_lvl_states[lvl].dev_state;
179 static inline enum uic_link_state
180 ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
182 return ufs_pm_lvl_states[lvl].link_state;
185 static inline enum ufs_pm_level
186 ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
187 enum uic_link_state link_state)
189 enum ufs_pm_level lvl;
191 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
192 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
193 (ufs_pm_lvl_states[lvl].link_state == link_state))
197 /* if no match found, return the level 0 */
201 static struct ufs_dev_fix ufs_fixups[] = {
202 /* UFS cards deviations table */
203 UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL,
204 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
205 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
206 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
207 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE |
208 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
209 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
210 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
211 UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/,
212 UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME),
213 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
214 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
215 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
216 UFS_DEVICE_QUIRK_PA_TACTIVATE),
217 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
218 UFS_DEVICE_QUIRK_PA_TACTIVATE),
222 static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
223 static void ufshcd_async_scan(void *data, async_cookie_t cookie);
224 static int ufshcd_reset_and_restore(struct ufs_hba *hba);
225 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
226 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
227 static void ufshcd_hba_exit(struct ufs_hba *hba);
228 static int ufshcd_probe_hba(struct ufs_hba *hba, bool async);
229 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
230 static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
231 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
232 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
233 static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
234 static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
235 static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
236 static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
237 static irqreturn_t ufshcd_intr(int irq, void *__hba);
238 static int ufshcd_change_power_mode(struct ufs_hba *hba,
239 struct ufs_pa_layer_attr *pwr_mode);
240 static void ufshcd_schedule_eh_work(struct ufs_hba *hba);
241 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on);
242 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on);
243 static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
244 struct ufs_vreg *vreg);
245 static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag);
246 static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba);
247 static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba);
248 static int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable);
249 static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set);
250 static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable);
251 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
252 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
254 static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
256 return tag >= 0 && tag < hba->nutrs;
259 static inline void ufshcd_enable_irq(struct ufs_hba *hba)
261 if (!hba->is_irq_enabled) {
262 enable_irq(hba->irq);
263 hba->is_irq_enabled = true;
267 static inline void ufshcd_disable_irq(struct ufs_hba *hba)
269 if (hba->is_irq_enabled) {
270 disable_irq(hba->irq);
271 hba->is_irq_enabled = false;
275 static inline void ufshcd_wb_config(struct ufs_hba *hba)
279 if (!ufshcd_is_wb_allowed(hba))
282 ret = ufshcd_wb_ctrl(hba, true);
284 dev_err(hba->dev, "%s: Enable WB failed: %d\n", __func__, ret);
286 dev_info(hba->dev, "%s: Write Booster Configured\n", __func__);
287 ret = ufshcd_wb_toggle_flush_during_h8(hba, true);
289 dev_err(hba->dev, "%s: En WB flush during H8: failed: %d\n",
291 ufshcd_wb_toggle_flush(hba, true);
294 static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
296 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt))
297 scsi_unblock_requests(hba->host);
300 static void ufshcd_scsi_block_requests(struct ufs_hba *hba)
302 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1)
303 scsi_block_requests(hba->host);
306 static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag,
309 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
311 trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->sc.cdb);
314 static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba, unsigned int tag,
317 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
319 trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->qr);
322 static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag,
325 int off = (int)tag - hba->nutrs;
326 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[off];
328 trace_ufshcd_upiu(dev_name(hba->dev), str, &descp->req_header,
329 &descp->input_param1);
332 static void ufshcd_add_uic_command_trace(struct ufs_hba *hba,
333 struct uic_command *ucmd,
338 if (!trace_ufshcd_uic_command_enabled())
341 if (!strcmp(str, "send"))
344 cmd = ufshcd_readl(hba, REG_UIC_COMMAND);
346 trace_ufshcd_uic_command(dev_name(hba->dev), str, cmd,
347 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1),
348 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2),
349 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3));
352 static void ufshcd_add_command_trace(struct ufs_hba *hba,
353 unsigned int tag, const char *str)
356 u8 opcode = 0, group_id = 0;
358 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
359 struct scsi_cmnd *cmd = lrbp->cmd;
360 int transfer_len = -1;
362 if (!trace_ufshcd_command_enabled()) {
363 /* trace UPIU W/O tracing command */
365 ufshcd_add_cmd_upiu_trace(hba, tag, str);
369 if (cmd) { /* data phase exists */
370 /* trace UPIU also */
371 ufshcd_add_cmd_upiu_trace(hba, tag, str);
372 opcode = cmd->cmnd[0];
373 if ((opcode == READ_10) || (opcode == WRITE_10)) {
375 * Currently we only fully trace read(10) and write(10)
378 if (cmd->request && cmd->request->bio)
379 lba = cmd->request->bio->bi_iter.bi_sector;
380 transfer_len = be32_to_cpu(
381 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
382 if (opcode == WRITE_10)
383 group_id = lrbp->cmd->cmnd[6];
384 } else if (opcode == UNMAP) {
386 lba = scsi_get_lba(cmd);
387 transfer_len = blk_rq_bytes(cmd->request);
392 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
393 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
394 trace_ufshcd_command(dev_name(hba->dev), str, tag,
395 doorbell, transfer_len, intr, lba, opcode, group_id);
398 static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
400 struct ufs_clk_info *clki;
401 struct list_head *head = &hba->clk_list_head;
403 if (list_empty(head))
406 list_for_each_entry(clki, head, list) {
407 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
409 dev_err(hba->dev, "clk: %s, rate: %u\n",
410 clki->name, clki->curr_freq);
414 static void ufshcd_print_evt(struct ufs_hba *hba, u32 id,
419 struct ufs_event_hist *e;
421 if (id >= UFS_EVT_CNT)
424 e = &hba->ufs_stats.event[id];
426 for (i = 0; i < UFS_EVENT_HIST_LENGTH; i++) {
427 int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH;
429 if (e->tstamp[p] == 0)
431 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p,
432 e->val[p], ktime_to_us(e->tstamp[p]));
437 dev_err(hba->dev, "No record of %s\n", err_name);
440 static void ufshcd_print_evt_hist(struct ufs_hba *hba)
442 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
444 ufshcd_print_evt(hba, UFS_EVT_PA_ERR, "pa_err");
445 ufshcd_print_evt(hba, UFS_EVT_DL_ERR, "dl_err");
446 ufshcd_print_evt(hba, UFS_EVT_NL_ERR, "nl_err");
447 ufshcd_print_evt(hba, UFS_EVT_TL_ERR, "tl_err");
448 ufshcd_print_evt(hba, UFS_EVT_DME_ERR, "dme_err");
449 ufshcd_print_evt(hba, UFS_EVT_AUTO_HIBERN8_ERR,
451 ufshcd_print_evt(hba, UFS_EVT_FATAL_ERR, "fatal_err");
452 ufshcd_print_evt(hba, UFS_EVT_LINK_STARTUP_FAIL,
453 "link_startup_fail");
454 ufshcd_print_evt(hba, UFS_EVT_RESUME_ERR, "resume_fail");
455 ufshcd_print_evt(hba, UFS_EVT_SUSPEND_ERR,
457 ufshcd_print_evt(hba, UFS_EVT_DEV_RESET, "dev_reset");
458 ufshcd_print_evt(hba, UFS_EVT_HOST_RESET, "host_reset");
459 ufshcd_print_evt(hba, UFS_EVT_ABORT, "task_abort");
461 ufshcd_vops_dbg_register_dump(hba);
465 void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
467 struct ufshcd_lrb *lrbp;
471 for_each_set_bit(tag, &bitmap, hba->nutrs) {
472 lrbp = &hba->lrb[tag];
474 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
475 tag, ktime_to_us(lrbp->issue_time_stamp));
476 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
477 tag, ktime_to_us(lrbp->compl_time_stamp));
479 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
480 tag, (u64)lrbp->utrd_dma_addr);
482 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
483 sizeof(struct utp_transfer_req_desc));
484 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
485 (u64)lrbp->ucd_req_dma_addr);
486 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
487 sizeof(struct utp_upiu_req));
488 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
489 (u64)lrbp->ucd_rsp_dma_addr);
490 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
491 sizeof(struct utp_upiu_rsp));
493 prdt_length = le16_to_cpu(
494 lrbp->utr_descriptor_ptr->prd_table_length);
495 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
496 prdt_length /= sizeof(struct ufshcd_sg_entry);
499 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
501 (u64)lrbp->ucd_prdt_dma_addr);
504 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
505 sizeof(struct ufshcd_sg_entry) * prdt_length);
509 static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
513 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
514 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag];
516 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
517 ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp));
521 static void ufshcd_print_host_state(struct ufs_hba *hba)
523 struct scsi_device *sdev_ufs = hba->sdev_ufs_device;
525 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
526 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n",
527 hba->outstanding_reqs, hba->outstanding_tasks);
528 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
529 hba->saved_err, hba->saved_uic_err);
530 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
531 hba->curr_dev_pwr_mode, hba->uic_link_state);
532 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
533 hba->pm_op_in_progress, hba->is_sys_suspended);
534 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
535 hba->auto_bkops_enabled, hba->host->host_self_blocked);
536 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
538 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt=%d\n",
539 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
540 hba->ufs_stats.hibern8_exit_cnt);
541 dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n",
542 ktime_to_us(hba->ufs_stats.last_intr_ts),
543 hba->ufs_stats.last_intr_status);
544 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
545 hba->eh_flags, hba->req_abort_count);
546 dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n",
547 hba->ufs_version, hba->capabilities, hba->caps);
548 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
551 dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n",
552 sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev);
554 ufshcd_print_clk_freqs(hba);
558 * ufshcd_print_pwr_info - print power params as saved in hba
560 * @hba: per-adapter instance
562 static void ufshcd_print_pwr_info(struct ufs_hba *hba)
564 static const char * const names[] = {
574 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
576 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
577 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
578 names[hba->pwr_info.pwr_rx],
579 names[hba->pwr_info.pwr_tx],
580 hba->pwr_info.hs_rate);
583 void ufshcd_delay_us(unsigned long us, unsigned long tolerance)
591 usleep_range(us, us + tolerance);
593 EXPORT_SYMBOL_GPL(ufshcd_delay_us);
596 * ufshcd_wait_for_register - wait for register value to change
597 * @hba: per-adapter interface
598 * @reg: mmio register offset
599 * @mask: mask to apply to the read register value
600 * @val: value to wait for
601 * @interval_us: polling interval in microseconds
602 * @timeout_ms: timeout in milliseconds
605 * -ETIMEDOUT on error, zero on success.
607 int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
608 u32 val, unsigned long interval_us,
609 unsigned long timeout_ms)
612 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
614 /* ignore bits that we don't intend to wait on */
617 while ((ufshcd_readl(hba, reg) & mask) != val) {
618 usleep_range(interval_us, interval_us + 50);
619 if (time_after(jiffies, timeout)) {
620 if ((ufshcd_readl(hba, reg) & mask) != val)
630 * ufshcd_get_intr_mask - Get the interrupt bit mask
631 * @hba: Pointer to adapter instance
633 * Returns interrupt bit mask per version
635 static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
639 switch (hba->ufs_version) {
640 case UFSHCI_VERSION_10:
641 intr_mask = INTERRUPT_MASK_ALL_VER_10;
643 case UFSHCI_VERSION_11:
644 case UFSHCI_VERSION_20:
645 intr_mask = INTERRUPT_MASK_ALL_VER_11;
647 case UFSHCI_VERSION_21:
649 intr_mask = INTERRUPT_MASK_ALL_VER_21;
657 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
658 * @hba: Pointer to adapter instance
660 * Returns UFSHCI version supported by the controller
662 static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
664 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
665 return ufshcd_vops_get_ufs_hci_version(hba);
667 return ufshcd_readl(hba, REG_UFS_VERSION);
671 * ufshcd_is_device_present - Check if any device connected to
672 * the host controller
673 * @hba: pointer to adapter instance
675 * Returns true if device present, false if no device detected
677 static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
679 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
680 DEVICE_PRESENT) ? true : false;
684 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
685 * @lrbp: pointer to local command reference block
687 * This function is used to get the OCS field from UTRD
688 * Returns the OCS field in the UTRD
690 static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
692 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
696 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
697 * @hba: per adapter instance
698 * @pos: position of the bit to be cleared
700 static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
702 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
703 ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
705 ufshcd_writel(hba, ~(1 << pos),
706 REG_UTP_TRANSFER_REQ_LIST_CLEAR);
710 * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register
711 * @hba: per adapter instance
712 * @pos: position of the bit to be cleared
714 static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
716 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
717 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
719 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
723 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
724 * @hba: per adapter instance
725 * @tag: position of the bit to be cleared
727 static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
729 __clear_bit(tag, &hba->outstanding_reqs);
733 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
734 * @reg: Register value of host controller status
736 * Returns integer, 0 on Success and positive value if failed
738 static inline int ufshcd_get_lists_status(u32 reg)
740 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
744 * ufshcd_get_uic_cmd_result - Get the UIC command result
745 * @hba: Pointer to adapter instance
747 * This function gets the result of UIC command completion
748 * Returns 0 on success, non zero value on error
750 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
752 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
753 MASK_UIC_COMMAND_RESULT;
757 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
758 * @hba: Pointer to adapter instance
760 * This function gets UIC command argument3
761 * Returns 0 on success, non zero value on error
763 static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
765 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
769 * ufshcd_get_req_rsp - returns the TR response transaction type
770 * @ucd_rsp_ptr: pointer to response UPIU
773 ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
775 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
779 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
780 * @ucd_rsp_ptr: pointer to response UPIU
782 * This function gets the response status and scsi_status from response UPIU
783 * Returns the response result code.
786 ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
788 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
792 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
794 * @ucd_rsp_ptr: pointer to response UPIU
796 * Return the data segment length.
798 static inline unsigned int
799 ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
801 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
802 MASK_RSP_UPIU_DATA_SEG_LEN;
806 * ufshcd_is_exception_event - Check if the device raised an exception event
807 * @ucd_rsp_ptr: pointer to response UPIU
809 * The function checks if the device raised an exception event indicated in
810 * the Device Information field of response UPIU.
812 * Returns true if exception is raised, false otherwise.
814 static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
816 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
817 MASK_RSP_EXCEPTION_EVENT ? true : false;
821 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
822 * @hba: per adapter instance
825 ufshcd_reset_intr_aggr(struct ufs_hba *hba)
827 ufshcd_writel(hba, INT_AGGR_ENABLE |
828 INT_AGGR_COUNTER_AND_TIMER_RESET,
829 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
833 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
834 * @hba: per adapter instance
835 * @cnt: Interrupt aggregation counter threshold
836 * @tmout: Interrupt aggregation timeout value
839 ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
841 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
842 INT_AGGR_COUNTER_THLD_VAL(cnt) |
843 INT_AGGR_TIMEOUT_VAL(tmout),
844 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
848 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
849 * @hba: per adapter instance
851 static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
853 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
857 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
858 * When run-stop registers are set to 1, it indicates the
859 * host controller that it can process the requests
860 * @hba: per adapter instance
862 static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
864 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
865 REG_UTP_TASK_REQ_LIST_RUN_STOP);
866 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
867 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
871 * ufshcd_hba_start - Start controller initialization sequence
872 * @hba: per adapter instance
874 static inline void ufshcd_hba_start(struct ufs_hba *hba)
876 u32 val = CONTROLLER_ENABLE;
878 if (ufshcd_crypto_enable(hba))
879 val |= CRYPTO_GENERAL_ENABLE;
881 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
885 * ufshcd_is_hba_active - Get controller state
886 * @hba: per adapter instance
888 * Returns false if controller is active, true otherwise
890 static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
892 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
896 u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
898 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
899 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
900 (hba->ufs_version == UFSHCI_VERSION_11))
901 return UFS_UNIPRO_VER_1_41;
903 return UFS_UNIPRO_VER_1_6;
905 EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
907 static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
910 * If both host and device support UniPro ver1.6 or later, PA layer
911 * parameters tuning happens during link startup itself.
913 * We can manually tune PA layer parameters if either host or device
914 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
915 * logic simple, we will only do manual tuning if local unipro version
916 * doesn't support ver1.6 or later.
918 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
925 * ufshcd_set_clk_freq - set UFS controller clock frequencies
926 * @hba: per adapter instance
927 * @scale_up: If True, set max possible frequency othewise set low frequency
929 * Returns 0 if successful
930 * Returns < 0 for any other errors
932 static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
935 struct ufs_clk_info *clki;
936 struct list_head *head = &hba->clk_list_head;
938 if (list_empty(head))
941 list_for_each_entry(clki, head, list) {
942 if (!IS_ERR_OR_NULL(clki->clk)) {
943 if (scale_up && clki->max_freq) {
944 if (clki->curr_freq == clki->max_freq)
947 ret = clk_set_rate(clki->clk, clki->max_freq);
949 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
950 __func__, clki->name,
951 clki->max_freq, ret);
954 trace_ufshcd_clk_scaling(dev_name(hba->dev),
955 "scaled up", clki->name,
959 clki->curr_freq = clki->max_freq;
961 } else if (!scale_up && clki->min_freq) {
962 if (clki->curr_freq == clki->min_freq)
965 ret = clk_set_rate(clki->clk, clki->min_freq);
967 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
968 __func__, clki->name,
969 clki->min_freq, ret);
972 trace_ufshcd_clk_scaling(dev_name(hba->dev),
973 "scaled down", clki->name,
976 clki->curr_freq = clki->min_freq;
979 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
980 clki->name, clk_get_rate(clki->clk));
988 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
989 * @hba: per adapter instance
990 * @scale_up: True if scaling up and false if scaling down
992 * Returns 0 if successful
993 * Returns < 0 for any other errors
995 static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
998 ktime_t start = ktime_get();
1000 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
1004 ret = ufshcd_set_clk_freq(hba, scale_up);
1008 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1010 ufshcd_set_clk_freq(hba, !scale_up);
1013 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1014 (scale_up ? "up" : "down"),
1015 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1020 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
1021 * @hba: per adapter instance
1022 * @scale_up: True if scaling up and false if scaling down
1024 * Returns true if scaling is required, false otherwise.
1026 static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
1029 struct ufs_clk_info *clki;
1030 struct list_head *head = &hba->clk_list_head;
1032 if (list_empty(head))
1035 list_for_each_entry(clki, head, list) {
1036 if (!IS_ERR_OR_NULL(clki->clk)) {
1037 if (scale_up && clki->max_freq) {
1038 if (clki->curr_freq == clki->max_freq)
1041 } else if (!scale_up && clki->min_freq) {
1042 if (clki->curr_freq == clki->min_freq)
1052 static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
1053 u64 wait_timeout_us)
1055 unsigned long flags;
1059 bool timeout = false, do_last_check = false;
1062 ufshcd_hold(hba, false);
1063 spin_lock_irqsave(hba->host->host_lock, flags);
1065 * Wait for all the outstanding tasks/transfer requests.
1066 * Verify by checking the doorbell registers are clear.
1068 start = ktime_get();
1070 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
1075 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
1076 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1077 if (!tm_doorbell && !tr_doorbell) {
1080 } else if (do_last_check) {
1084 spin_unlock_irqrestore(hba->host->host_lock, flags);
1086 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
1090 * We might have scheduled out for long time so make
1091 * sure to check if doorbells are cleared by this time
1094 do_last_check = true;
1096 spin_lock_irqsave(hba->host->host_lock, flags);
1097 } while (tm_doorbell || tr_doorbell);
1101 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1102 __func__, tm_doorbell, tr_doorbell);
1106 spin_unlock_irqrestore(hba->host->host_lock, flags);
1107 ufshcd_release(hba);
1112 * ufshcd_scale_gear - scale up/down UFS gear
1113 * @hba: per adapter instance
1114 * @scale_up: True for scaling up gear and false for scaling down
1116 * Returns 0 for success,
1117 * Returns -EBUSY if scaling can't happen at this time
1118 * Returns non-zero for any other errors
1120 static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1123 struct ufs_pa_layer_attr new_pwr_info;
1126 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1127 sizeof(struct ufs_pa_layer_attr));
1129 memcpy(&new_pwr_info, &hba->pwr_info,
1130 sizeof(struct ufs_pa_layer_attr));
1132 if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear ||
1133 hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) {
1134 /* save the current power mode */
1135 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1137 sizeof(struct ufs_pa_layer_attr));
1139 /* scale down gear */
1140 new_pwr_info.gear_tx = hba->clk_scaling.min_gear;
1141 new_pwr_info.gear_rx = hba->clk_scaling.min_gear;
1145 /* check if the power mode needs to be changed or not? */
1146 ret = ufshcd_config_pwr_mode(hba, &new_pwr_info);
1148 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1150 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1151 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1156 static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1158 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1161 * make sure that there are no outstanding requests when
1162 * clock scaling is in progress
1164 ufshcd_scsi_block_requests(hba);
1165 down_write(&hba->clk_scaling_lock);
1166 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
1168 up_write(&hba->clk_scaling_lock);
1169 ufshcd_scsi_unblock_requests(hba);
1175 static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
1177 up_write(&hba->clk_scaling_lock);
1178 ufshcd_scsi_unblock_requests(hba);
1182 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1183 * @hba: per adapter instance
1184 * @scale_up: True for scaling up and false for scalin down
1186 * Returns 0 for success,
1187 * Returns -EBUSY if scaling can't happen at this time
1188 * Returns non-zero for any other errors
1190 static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1194 /* let's not get into low power until clock scaling is completed */
1195 ufshcd_hold(hba, false);
1197 ret = ufshcd_clock_scaling_prepare(hba);
1201 /* scale down the gear before scaling down clocks */
1203 ret = ufshcd_scale_gear(hba, false);
1208 ret = ufshcd_scale_clks(hba, scale_up);
1211 ufshcd_scale_gear(hba, true);
1215 /* scale up the gear after scaling up clocks */
1217 ret = ufshcd_scale_gear(hba, true);
1219 ufshcd_scale_clks(hba, false);
1224 /* Enable Write Booster if we have scaled up else disable it */
1225 up_write(&hba->clk_scaling_lock);
1226 ufshcd_wb_ctrl(hba, scale_up);
1227 down_write(&hba->clk_scaling_lock);
1230 ufshcd_clock_scaling_unprepare(hba);
1232 ufshcd_release(hba);
1236 static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1238 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1239 clk_scaling.suspend_work);
1240 unsigned long irq_flags;
1242 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1243 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1244 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1247 hba->clk_scaling.is_suspended = true;
1248 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1250 __ufshcd_suspend_clkscaling(hba);
1253 static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1255 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1256 clk_scaling.resume_work);
1257 unsigned long irq_flags;
1259 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1260 if (!hba->clk_scaling.is_suspended) {
1261 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1264 hba->clk_scaling.is_suspended = false;
1265 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1267 devfreq_resume_device(hba->devfreq);
1270 static int ufshcd_devfreq_target(struct device *dev,
1271 unsigned long *freq, u32 flags)
1274 struct ufs_hba *hba = dev_get_drvdata(dev);
1276 bool scale_up, sched_clk_scaling_suspend_work = false;
1277 struct list_head *clk_list = &hba->clk_list_head;
1278 struct ufs_clk_info *clki;
1279 unsigned long irq_flags;
1281 if (!ufshcd_is_clkscaling_supported(hba))
1284 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list);
1285 /* Override with the closest supported frequency */
1286 *freq = (unsigned long) clk_round_rate(clki->clk, *freq);
1287 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1288 if (ufshcd_eh_in_progress(hba)) {
1289 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1293 if (!hba->clk_scaling.active_reqs)
1294 sched_clk_scaling_suspend_work = true;
1296 if (list_empty(clk_list)) {
1297 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1301 /* Decide based on the rounded-off frequency and update */
1302 scale_up = (*freq == clki->max_freq) ? true : false;
1304 *freq = clki->min_freq;
1305 /* Update the frequency */
1306 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1307 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1309 goto out; /* no state change required */
1311 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1313 start = ktime_get();
1314 ret = ufshcd_devfreq_scale(hba, scale_up);
1316 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1317 (scale_up ? "up" : "down"),
1318 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1321 if (sched_clk_scaling_suspend_work)
1322 queue_work(hba->clk_scaling.workq,
1323 &hba->clk_scaling.suspend_work);
1328 static bool ufshcd_is_busy(struct request *req, void *priv, bool reserved)
1332 WARN_ON_ONCE(reserved);
1337 /* Whether or not any tag is in use by a request that is in progress. */
1338 static bool ufshcd_any_tag_in_use(struct ufs_hba *hba)
1340 struct request_queue *q = hba->cmd_queue;
1343 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_is_busy, &busy);
1347 static int ufshcd_devfreq_get_dev_status(struct device *dev,
1348 struct devfreq_dev_status *stat)
1350 struct ufs_hba *hba = dev_get_drvdata(dev);
1351 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1352 unsigned long flags;
1353 struct list_head *clk_list = &hba->clk_list_head;
1354 struct ufs_clk_info *clki;
1357 if (!ufshcd_is_clkscaling_supported(hba))
1360 memset(stat, 0, sizeof(*stat));
1362 spin_lock_irqsave(hba->host->host_lock, flags);
1363 curr_t = ktime_get();
1364 if (!scaling->window_start_t)
1367 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1369 * If current frequency is 0, then the ondemand governor considers
1370 * there's no initial frequency set. And it always requests to set
1371 * to max. frequency.
1373 stat->current_frequency = clki->curr_freq;
1374 if (scaling->is_busy_started)
1375 scaling->tot_busy_t += ktime_us_delta(curr_t,
1376 scaling->busy_start_t);
1378 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t);
1379 stat->busy_time = scaling->tot_busy_t;
1381 scaling->window_start_t = curr_t;
1382 scaling->tot_busy_t = 0;
1384 if (hba->outstanding_reqs) {
1385 scaling->busy_start_t = curr_t;
1386 scaling->is_busy_started = true;
1388 scaling->busy_start_t = 0;
1389 scaling->is_busy_started = false;
1391 spin_unlock_irqrestore(hba->host->host_lock, flags);
1395 static int ufshcd_devfreq_init(struct ufs_hba *hba)
1397 struct list_head *clk_list = &hba->clk_list_head;
1398 struct ufs_clk_info *clki;
1399 struct devfreq *devfreq;
1402 /* Skip devfreq if we don't have any clocks in the list */
1403 if (list_empty(clk_list))
1406 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1407 dev_pm_opp_add(hba->dev, clki->min_freq, 0);
1408 dev_pm_opp_add(hba->dev, clki->max_freq, 0);
1410 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile,
1411 &hba->vps->ondemand_data);
1412 devfreq = devfreq_add_device(hba->dev,
1413 &hba->vps->devfreq_profile,
1414 DEVFREQ_GOV_SIMPLE_ONDEMAND,
1415 &hba->vps->ondemand_data);
1416 if (IS_ERR(devfreq)) {
1417 ret = PTR_ERR(devfreq);
1418 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret);
1420 dev_pm_opp_remove(hba->dev, clki->min_freq);
1421 dev_pm_opp_remove(hba->dev, clki->max_freq);
1425 hba->devfreq = devfreq;
1430 static void ufshcd_devfreq_remove(struct ufs_hba *hba)
1432 struct list_head *clk_list = &hba->clk_list_head;
1433 struct ufs_clk_info *clki;
1438 devfreq_remove_device(hba->devfreq);
1439 hba->devfreq = NULL;
1441 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1442 dev_pm_opp_remove(hba->dev, clki->min_freq);
1443 dev_pm_opp_remove(hba->dev, clki->max_freq);
1446 static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1448 unsigned long flags;
1450 devfreq_suspend_device(hba->devfreq);
1451 spin_lock_irqsave(hba->host->host_lock, flags);
1452 hba->clk_scaling.window_start_t = 0;
1453 spin_unlock_irqrestore(hba->host->host_lock, flags);
1456 static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1458 unsigned long flags;
1459 bool suspend = false;
1461 if (!ufshcd_is_clkscaling_supported(hba))
1464 spin_lock_irqsave(hba->host->host_lock, flags);
1465 if (!hba->clk_scaling.is_suspended) {
1467 hba->clk_scaling.is_suspended = true;
1469 spin_unlock_irqrestore(hba->host->host_lock, flags);
1472 __ufshcd_suspend_clkscaling(hba);
1475 static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1477 unsigned long flags;
1478 bool resume = false;
1480 if (!ufshcd_is_clkscaling_supported(hba))
1483 spin_lock_irqsave(hba->host->host_lock, flags);
1484 if (hba->clk_scaling.is_suspended) {
1486 hba->clk_scaling.is_suspended = false;
1488 spin_unlock_irqrestore(hba->host->host_lock, flags);
1491 devfreq_resume_device(hba->devfreq);
1494 static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1495 struct device_attribute *attr, char *buf)
1497 struct ufs_hba *hba = dev_get_drvdata(dev);
1499 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
1502 static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1503 struct device_attribute *attr, const char *buf, size_t count)
1505 struct ufs_hba *hba = dev_get_drvdata(dev);
1509 if (kstrtou32(buf, 0, &value))
1513 if (value == hba->clk_scaling.is_allowed)
1516 pm_runtime_get_sync(hba->dev);
1517 ufshcd_hold(hba, false);
1519 cancel_work_sync(&hba->clk_scaling.suspend_work);
1520 cancel_work_sync(&hba->clk_scaling.resume_work);
1522 hba->clk_scaling.is_allowed = value;
1525 ufshcd_resume_clkscaling(hba);
1527 ufshcd_suspend_clkscaling(hba);
1528 err = ufshcd_devfreq_scale(hba, true);
1530 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1534 ufshcd_release(hba);
1535 pm_runtime_put_sync(hba->dev);
1540 static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
1542 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1543 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1544 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1545 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1546 hba->clk_scaling.enable_attr.attr.mode = 0644;
1547 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1548 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1551 static void ufshcd_ungate_work(struct work_struct *work)
1554 unsigned long flags;
1555 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1556 clk_gating.ungate_work);
1558 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1560 spin_lock_irqsave(hba->host->host_lock, flags);
1561 if (hba->clk_gating.state == CLKS_ON) {
1562 spin_unlock_irqrestore(hba->host->host_lock, flags);
1566 spin_unlock_irqrestore(hba->host->host_lock, flags);
1567 ufshcd_hba_vreg_set_hpm(hba);
1568 ufshcd_setup_clocks(hba, true);
1570 ufshcd_enable_irq(hba);
1572 /* Exit from hibern8 */
1573 if (ufshcd_can_hibern8_during_gating(hba)) {
1574 /* Prevent gating in this path */
1575 hba->clk_gating.is_suspended = true;
1576 if (ufshcd_is_link_hibern8(hba)) {
1577 ret = ufshcd_uic_hibern8_exit(hba);
1579 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1582 ufshcd_set_link_active(hba);
1584 hba->clk_gating.is_suspended = false;
1587 ufshcd_scsi_unblock_requests(hba);
1591 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1592 * Also, exit from hibern8 mode and set the link as active.
1593 * @hba: per adapter instance
1594 * @async: This indicates whether caller should ungate clocks asynchronously.
1596 int ufshcd_hold(struct ufs_hba *hba, bool async)
1600 unsigned long flags;
1602 if (!ufshcd_is_clkgating_allowed(hba))
1604 spin_lock_irqsave(hba->host->host_lock, flags);
1605 hba->clk_gating.active_reqs++;
1608 switch (hba->clk_gating.state) {
1611 * Wait for the ungate work to complete if in progress.
1612 * Though the clocks may be in ON state, the link could
1613 * still be in hibner8 state if hibern8 is allowed
1614 * during clock gating.
1615 * Make sure we exit hibern8 state also in addition to
1618 if (ufshcd_can_hibern8_during_gating(hba) &&
1619 ufshcd_is_link_hibern8(hba)) {
1622 hba->clk_gating.active_reqs--;
1625 spin_unlock_irqrestore(hba->host->host_lock, flags);
1626 flush_result = flush_work(&hba->clk_gating.ungate_work);
1627 if (hba->clk_gating.is_suspended && !flush_result)
1629 spin_lock_irqsave(hba->host->host_lock, flags);
1634 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1635 hba->clk_gating.state = CLKS_ON;
1636 trace_ufshcd_clk_gating(dev_name(hba->dev),
1637 hba->clk_gating.state);
1641 * If we are here, it means gating work is either done or
1642 * currently running. Hence, fall through to cancel gating
1643 * work and to enable clocks.
1647 ufshcd_scsi_block_requests(hba);
1648 hba->clk_gating.state = REQ_CLKS_ON;
1649 trace_ufshcd_clk_gating(dev_name(hba->dev),
1650 hba->clk_gating.state);
1651 queue_work(hba->clk_gating.clk_gating_workq,
1652 &hba->clk_gating.ungate_work);
1654 * fall through to check if we should wait for this
1655 * work to be done or not.
1661 hba->clk_gating.active_reqs--;
1665 spin_unlock_irqrestore(hba->host->host_lock, flags);
1666 flush_work(&hba->clk_gating.ungate_work);
1667 /* Make sure state is CLKS_ON before returning */
1668 spin_lock_irqsave(hba->host->host_lock, flags);
1671 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1672 __func__, hba->clk_gating.state);
1675 spin_unlock_irqrestore(hba->host->host_lock, flags);
1679 EXPORT_SYMBOL_GPL(ufshcd_hold);
1681 static void ufshcd_gate_work(struct work_struct *work)
1683 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1684 clk_gating.gate_work.work);
1685 unsigned long flags;
1688 spin_lock_irqsave(hba->host->host_lock, flags);
1690 * In case you are here to cancel this work the gating state
1691 * would be marked as REQ_CLKS_ON. In this case save time by
1692 * skipping the gating work and exit after changing the clock
1695 if (hba->clk_gating.is_suspended ||
1696 (hba->clk_gating.state != REQ_CLKS_OFF)) {
1697 hba->clk_gating.state = CLKS_ON;
1698 trace_ufshcd_clk_gating(dev_name(hba->dev),
1699 hba->clk_gating.state);
1703 if (hba->clk_gating.active_reqs
1704 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1705 || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks
1706 || hba->active_uic_cmd || hba->uic_async_done)
1709 spin_unlock_irqrestore(hba->host->host_lock, flags);
1711 /* put the link into hibern8 mode before turning off clocks */
1712 if (ufshcd_can_hibern8_during_gating(hba)) {
1713 ret = ufshcd_uic_hibern8_enter(hba);
1715 hba->clk_gating.state = CLKS_ON;
1716 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
1718 trace_ufshcd_clk_gating(dev_name(hba->dev),
1719 hba->clk_gating.state);
1722 ufshcd_set_link_hibern8(hba);
1725 ufshcd_disable_irq(hba);
1727 ufshcd_setup_clocks(hba, false);
1729 /* Put the host controller in low power mode if possible */
1730 ufshcd_hba_vreg_set_lpm(hba);
1732 * In case you are here to cancel this work the gating state
1733 * would be marked as REQ_CLKS_ON. In this case keep the state
1734 * as REQ_CLKS_ON which would anyway imply that clocks are off
1735 * and a request to turn them on is pending. By doing this way,
1736 * we keep the state machine in tact and this would ultimately
1737 * prevent from doing cancel work multiple times when there are
1738 * new requests arriving before the current cancel work is done.
1740 spin_lock_irqsave(hba->host->host_lock, flags);
1741 if (hba->clk_gating.state == REQ_CLKS_OFF) {
1742 hba->clk_gating.state = CLKS_OFF;
1743 trace_ufshcd_clk_gating(dev_name(hba->dev),
1744 hba->clk_gating.state);
1747 spin_unlock_irqrestore(hba->host->host_lock, flags);
1752 /* host lock must be held before calling this variant */
1753 static void __ufshcd_release(struct ufs_hba *hba)
1755 if (!ufshcd_is_clkgating_allowed(hba))
1758 hba->clk_gating.active_reqs--;
1760 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
1761 hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
1762 hba->outstanding_tasks ||
1763 hba->active_uic_cmd || hba->uic_async_done ||
1764 hba->clk_gating.state == CLKS_OFF)
1767 hba->clk_gating.state = REQ_CLKS_OFF;
1768 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
1769 queue_delayed_work(hba->clk_gating.clk_gating_workq,
1770 &hba->clk_gating.gate_work,
1771 msecs_to_jiffies(hba->clk_gating.delay_ms));
1774 void ufshcd_release(struct ufs_hba *hba)
1776 unsigned long flags;
1778 spin_lock_irqsave(hba->host->host_lock, flags);
1779 __ufshcd_release(hba);
1780 spin_unlock_irqrestore(hba->host->host_lock, flags);
1782 EXPORT_SYMBOL_GPL(ufshcd_release);
1784 static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1785 struct device_attribute *attr, char *buf)
1787 struct ufs_hba *hba = dev_get_drvdata(dev);
1789 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1792 static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1793 struct device_attribute *attr, const char *buf, size_t count)
1795 struct ufs_hba *hba = dev_get_drvdata(dev);
1796 unsigned long flags, value;
1798 if (kstrtoul(buf, 0, &value))
1801 spin_lock_irqsave(hba->host->host_lock, flags);
1802 hba->clk_gating.delay_ms = value;
1803 spin_unlock_irqrestore(hba->host->host_lock, flags);
1807 static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1808 struct device_attribute *attr, char *buf)
1810 struct ufs_hba *hba = dev_get_drvdata(dev);
1812 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1815 static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1816 struct device_attribute *attr, const char *buf, size_t count)
1818 struct ufs_hba *hba = dev_get_drvdata(dev);
1819 unsigned long flags;
1822 if (kstrtou32(buf, 0, &value))
1827 spin_lock_irqsave(hba->host->host_lock, flags);
1828 if (value == hba->clk_gating.is_enabled)
1832 __ufshcd_release(hba);
1834 hba->clk_gating.active_reqs++;
1836 hba->clk_gating.is_enabled = value;
1838 spin_unlock_irqrestore(hba->host->host_lock, flags);
1842 static void ufshcd_init_clk_scaling(struct ufs_hba *hba)
1844 char wq_name[sizeof("ufs_clkscaling_00")];
1846 if (!ufshcd_is_clkscaling_supported(hba))
1849 if (!hba->clk_scaling.min_gear)
1850 hba->clk_scaling.min_gear = UFS_HS_G1;
1852 INIT_WORK(&hba->clk_scaling.suspend_work,
1853 ufshcd_clk_scaling_suspend_work);
1854 INIT_WORK(&hba->clk_scaling.resume_work,
1855 ufshcd_clk_scaling_resume_work);
1857 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d",
1858 hba->host->host_no);
1859 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
1861 ufshcd_clkscaling_init_sysfs(hba);
1864 static void ufshcd_exit_clk_scaling(struct ufs_hba *hba)
1866 if (!ufshcd_is_clkscaling_supported(hba))
1869 destroy_workqueue(hba->clk_scaling.workq);
1870 ufshcd_devfreq_remove(hba);
1873 static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1875 char wq_name[sizeof("ufs_clk_gating_00")];
1877 if (!ufshcd_is_clkgating_allowed(hba))
1880 hba->clk_gating.state = CLKS_ON;
1882 hba->clk_gating.delay_ms = 150;
1883 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1884 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1886 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
1887 hba->host->host_no);
1888 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
1889 WQ_MEM_RECLAIM | WQ_HIGHPRI);
1891 hba->clk_gating.is_enabled = true;
1893 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1894 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1895 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1896 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
1897 hba->clk_gating.delay_attr.attr.mode = 0644;
1898 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1899 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
1901 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1902 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1903 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1904 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1905 hba->clk_gating.enable_attr.attr.mode = 0644;
1906 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1907 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
1910 static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1912 if (!ufshcd_is_clkgating_allowed(hba))
1914 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
1915 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
1916 cancel_work_sync(&hba->clk_gating.ungate_work);
1917 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1918 destroy_workqueue(hba->clk_gating.clk_gating_workq);
1921 /* Must be called with host lock acquired */
1922 static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1924 bool queue_resume_work = false;
1925 ktime_t curr_t = ktime_get();
1927 if (!ufshcd_is_clkscaling_supported(hba))
1930 if (!hba->clk_scaling.active_reqs++)
1931 queue_resume_work = true;
1933 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1936 if (queue_resume_work)
1937 queue_work(hba->clk_scaling.workq,
1938 &hba->clk_scaling.resume_work);
1940 if (!hba->clk_scaling.window_start_t) {
1941 hba->clk_scaling.window_start_t = curr_t;
1942 hba->clk_scaling.tot_busy_t = 0;
1943 hba->clk_scaling.is_busy_started = false;
1946 if (!hba->clk_scaling.is_busy_started) {
1947 hba->clk_scaling.busy_start_t = curr_t;
1948 hba->clk_scaling.is_busy_started = true;
1952 static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1954 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1956 if (!ufshcd_is_clkscaling_supported(hba))
1959 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1960 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1961 scaling->busy_start_t));
1962 scaling->busy_start_t = 0;
1963 scaling->is_busy_started = false;
1967 * ufshcd_send_command - Send SCSI or device management commands
1968 * @hba: per adapter instance
1969 * @task_tag: Task tag of the command
1972 void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1974 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag];
1976 lrbp->issue_time_stamp = ktime_get();
1977 lrbp->compl_time_stamp = ktime_set(0, 0);
1978 ufshcd_vops_setup_xfer_req(hba, task_tag, (lrbp->cmd ? true : false));
1979 ufshcd_add_command_trace(hba, task_tag, "send");
1980 ufshcd_clk_scaling_start_busy(hba);
1981 __set_bit(task_tag, &hba->outstanding_reqs);
1982 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1983 /* Make sure that doorbell is committed immediately */
1988 * ufshcd_copy_sense_data - Copy sense data in case of check condition
1989 * @lrbp: pointer to local reference block
1991 static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1994 if (lrbp->sense_buffer &&
1995 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
1998 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
1999 len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
2001 memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data,
2007 * ufshcd_copy_query_response() - Copy the Query Response and the data
2009 * @hba: per adapter instance
2010 * @lrbp: pointer to local reference block
2013 int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2015 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2017 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
2019 /* Get the descriptor */
2020 if (hba->dev_cmd.query.descriptor &&
2021 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
2022 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
2023 GENERAL_UPIU_REQUEST_SIZE;
2027 /* data segment length */
2028 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
2029 MASK_QUERY_DATA_SEG_LEN;
2030 buf_len = be16_to_cpu(
2031 hba->dev_cmd.query.request.upiu_req.length);
2032 if (likely(buf_len >= resp_len)) {
2033 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
2036 "%s: rsp size %d is bigger than buffer size %d",
2037 __func__, resp_len, buf_len);
2046 * ufshcd_hba_capabilities - Read controller capabilities
2047 * @hba: per adapter instance
2049 * Return: 0 on success, negative on error.
2051 static inline int ufshcd_hba_capabilities(struct ufs_hba *hba)
2055 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
2057 /* nutrs and nutmrs are 0 based values */
2058 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2060 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
2062 /* Read crypto capabilities */
2063 err = ufshcd_hba_init_crypto_capabilities(hba);
2065 dev_err(hba->dev, "crypto setup failed\n");
2071 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2072 * to accept UIC commands
2073 * @hba: per adapter instance
2074 * Return true on success, else false
2076 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2078 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2085 * ufshcd_get_upmcrs - Get the power mode change request status
2086 * @hba: Pointer to adapter instance
2088 * This function gets the UPMCRS field of HCS register
2089 * Returns value of UPMCRS field
2091 static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2093 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2097 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
2098 * @hba: per adapter instance
2099 * @uic_cmd: UIC command
2101 * Mutex must be held.
2104 ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2106 WARN_ON(hba->active_uic_cmd);
2108 hba->active_uic_cmd = uic_cmd;
2111 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2112 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2113 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
2115 ufshcd_add_uic_command_trace(hba, uic_cmd, "send");
2118 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
2123 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
2124 * @hba: per adapter instance
2125 * @uic_cmd: UIC command
2127 * Must be called with mutex held.
2128 * Returns 0 only if success.
2131 ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2134 unsigned long flags;
2136 if (wait_for_completion_timeout(&uic_cmd->done,
2137 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
2138 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2142 spin_lock_irqsave(hba->host->host_lock, flags);
2143 hba->active_uic_cmd = NULL;
2144 spin_unlock_irqrestore(hba->host->host_lock, flags);
2150 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2151 * @hba: per adapter instance
2152 * @uic_cmd: UIC command
2153 * @completion: initialize the completion only if this is set to true
2155 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
2156 * with mutex held and host_lock locked.
2157 * Returns 0 only if success.
2160 __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2163 if (!ufshcd_ready_for_uic_cmd(hba)) {
2165 "Controller not ready to accept UIC commands\n");
2170 init_completion(&uic_cmd->done);
2172 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
2178 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2179 * @hba: per adapter instance
2180 * @uic_cmd: UIC command
2182 * Returns 0 only if success.
2184 int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2187 unsigned long flags;
2189 ufshcd_hold(hba, false);
2190 mutex_lock(&hba->uic_cmd_mutex);
2191 ufshcd_add_delay_before_dme_cmd(hba);
2193 spin_lock_irqsave(hba->host->host_lock, flags);
2194 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
2195 spin_unlock_irqrestore(hba->host->host_lock, flags);
2197 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2199 mutex_unlock(&hba->uic_cmd_mutex);
2201 ufshcd_release(hba);
2206 * ufshcd_map_sg - Map scatter-gather list to prdt
2207 * @hba: per adapter instance
2208 * @lrbp: pointer to local reference block
2210 * Returns 0 in case of success, non-zero value in case of failure
2212 static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2214 struct ufshcd_sg_entry *prd_table;
2215 struct scatterlist *sg;
2216 struct scsi_cmnd *cmd;
2221 sg_segments = scsi_dma_map(cmd);
2222 if (sg_segments < 0)
2227 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2228 lrbp->utr_descriptor_ptr->prd_table_length =
2229 cpu_to_le16((sg_segments *
2230 sizeof(struct ufshcd_sg_entry)));
2232 lrbp->utr_descriptor_ptr->prd_table_length =
2233 cpu_to_le16((u16) (sg_segments));
2235 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2237 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2239 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2240 prd_table[i].base_addr =
2241 cpu_to_le32(lower_32_bits(sg->dma_address));
2242 prd_table[i].upper_addr =
2243 cpu_to_le32(upper_32_bits(sg->dma_address));
2244 prd_table[i].reserved = 0;
2247 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2254 * ufshcd_enable_intr - enable interrupts
2255 * @hba: per adapter instance
2256 * @intrs: interrupt bits
2258 static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
2260 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2262 if (hba->ufs_version == UFSHCI_VERSION_10) {
2264 rw = set & INTERRUPT_MASK_RW_VER_10;
2265 set = rw | ((set ^ intrs) & intrs);
2270 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2274 * ufshcd_disable_intr - disable interrupts
2275 * @hba: per adapter instance
2276 * @intrs: interrupt bits
2278 static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2280 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2282 if (hba->ufs_version == UFSHCI_VERSION_10) {
2284 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2285 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2286 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2292 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2296 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2297 * descriptor according to request
2298 * @lrbp: pointer to local reference block
2299 * @upiu_flags: flags required in the header
2300 * @cmd_dir: requests data direction
2302 static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
2303 u8 *upiu_flags, enum dma_data_direction cmd_dir)
2305 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2311 if (cmd_dir == DMA_FROM_DEVICE) {
2312 data_direction = UTP_DEVICE_TO_HOST;
2313 *upiu_flags = UPIU_CMD_FLAGS_READ;
2314 } else if (cmd_dir == DMA_TO_DEVICE) {
2315 data_direction = UTP_HOST_TO_DEVICE;
2316 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2318 data_direction = UTP_NO_DATA_TRANSFER;
2319 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2322 dword_0 = data_direction | (lrbp->command_type
2323 << UPIU_COMMAND_TYPE_OFFSET);
2325 dword_0 |= UTP_REQ_DESC_INT_CMD;
2327 /* Prepare crypto related dwords */
2328 ufshcd_prepare_req_desc_hdr_crypto(lrbp, &dword_0, &dword_1, &dword_3);
2330 /* Transfer request descriptor header fields */
2331 req_desc->header.dword_0 = cpu_to_le32(dword_0);
2332 req_desc->header.dword_1 = cpu_to_le32(dword_1);
2334 * assigning invalid value for command status. Controller
2335 * updates OCS on command completion, with the command
2338 req_desc->header.dword_2 =
2339 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
2340 req_desc->header.dword_3 = cpu_to_le32(dword_3);
2342 req_desc->prd_table_length = 0;
2346 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2348 * @lrbp: local reference block pointer
2349 * @upiu_flags: flags
2352 void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags)
2354 struct scsi_cmnd *cmd = lrbp->cmd;
2355 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2356 unsigned short cdb_len;
2358 /* command descriptor fields */
2359 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2360 UPIU_TRANSACTION_COMMAND, upiu_flags,
2361 lrbp->lun, lrbp->task_tag);
2362 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2363 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2365 /* Total EHS length and Data segment length will be zero */
2366 ucd_req_ptr->header.dword_2 = 0;
2368 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length);
2370 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE);
2371 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
2372 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len);
2374 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
2378 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2381 * @lrbp: local reference block pointer
2382 * @upiu_flags: flags
2384 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2385 struct ufshcd_lrb *lrbp, u8 upiu_flags)
2387 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2388 struct ufs_query *query = &hba->dev_cmd.query;
2389 u16 len = be16_to_cpu(query->request.upiu_req.length);
2391 /* Query request header */
2392 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2393 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2394 lrbp->lun, lrbp->task_tag);
2395 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2396 0, query->request.query_func, 0, 0);
2398 /* Data segment length only need for WRITE_DESC */
2399 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2400 ucd_req_ptr->header.dword_2 =
2401 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2403 ucd_req_ptr->header.dword_2 = 0;
2405 /* Copy the Query Request buffer as is */
2406 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2409 /* Copy the Descriptor */
2410 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2411 memcpy(ucd_req_ptr + 1, query->descriptor, len);
2413 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
2416 static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2418 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2420 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2422 /* command descriptor fields */
2423 ucd_req_ptr->header.dword_0 =
2425 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
2426 /* clear rest of the fields of basic header */
2427 ucd_req_ptr->header.dword_1 = 0;
2428 ucd_req_ptr->header.dword_2 = 0;
2430 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
2434 * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU)
2435 * for Device Management Purposes
2436 * @hba: per adapter instance
2437 * @lrbp: pointer to local reference block
2439 static int ufshcd_compose_devman_upiu(struct ufs_hba *hba,
2440 struct ufshcd_lrb *lrbp)
2445 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2446 (hba->ufs_version == UFSHCI_VERSION_11))
2447 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
2449 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
2451 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2452 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2453 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2454 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2455 ufshcd_prepare_utp_nop_upiu(lrbp);
2463 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2465 * @hba: per adapter instance
2466 * @lrbp: pointer to local reference block
2468 static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2473 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2474 (hba->ufs_version == UFSHCI_VERSION_11))
2475 lrbp->command_type = UTP_CMD_TYPE_SCSI;
2477 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
2479 if (likely(lrbp->cmd)) {
2480 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2481 lrbp->cmd->sc_data_direction);
2482 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2491 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2492 * @upiu_wlun_id: UPIU W-LUN id
2494 * Returns SCSI W-LUN id
2496 static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2498 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2501 static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i)
2503 struct utp_transfer_cmd_desc *cmd_descp = hba->ucdl_base_addr;
2504 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr;
2505 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr +
2506 i * sizeof(struct utp_transfer_cmd_desc);
2507 u16 response_offset = offsetof(struct utp_transfer_cmd_desc,
2509 u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
2511 lrb->utr_descriptor_ptr = utrdlp + i;
2512 lrb->utrd_dma_addr = hba->utrdl_dma_addr +
2513 i * sizeof(struct utp_transfer_req_desc);
2514 lrb->ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i);
2515 lrb->ucd_req_dma_addr = cmd_desc_element_addr;
2516 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2517 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset;
2518 lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2519 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset;
2523 * ufshcd_queuecommand - main entry point for SCSI requests
2524 * @host: SCSI host pointer
2525 * @cmd: command from SCSI Midlayer
2527 * Returns 0 for success, non-zero in case of failure
2529 static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2531 struct ufshcd_lrb *lrbp;
2532 struct ufs_hba *hba;
2533 unsigned long flags;
2537 hba = shost_priv(host);
2539 tag = cmd->request->tag;
2540 if (!ufshcd_valid_tag(hba, tag)) {
2542 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2543 __func__, tag, cmd, cmd->request);
2547 if (!down_read_trylock(&hba->clk_scaling_lock))
2548 return SCSI_MLQUEUE_HOST_BUSY;
2550 hba->req_abort_count = 0;
2552 err = ufshcd_hold(hba, true);
2554 err = SCSI_MLQUEUE_HOST_BUSY;
2557 WARN_ON(ufshcd_is_clkgating_allowed(hba) &&
2558 (hba->clk_gating.state != CLKS_ON));
2560 lrbp = &hba->lrb[tag];
2561 if (unlikely(lrbp->in_use)) {
2562 if (hba->pm_op_in_progress)
2563 set_host_byte(cmd, DID_BAD_TARGET);
2565 err = SCSI_MLQUEUE_HOST_BUSY;
2566 ufshcd_release(hba);
2572 lrbp->sense_bufflen = UFS_SENSE_SIZE;
2573 lrbp->sense_buffer = cmd->sense_buffer;
2574 lrbp->task_tag = tag;
2575 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
2576 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
2578 ufshcd_prepare_lrbp_crypto(cmd->request, lrbp);
2580 lrbp->req_abort_skip = false;
2582 ufshcd_comp_scsi_upiu(hba, lrbp);
2584 err = ufshcd_map_sg(hba, lrbp);
2587 ufshcd_release(hba);
2590 /* Make sure descriptors are ready before ringing the doorbell */
2593 spin_lock_irqsave(hba->host->host_lock, flags);
2594 switch (hba->ufshcd_state) {
2595 case UFSHCD_STATE_OPERATIONAL:
2596 case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL:
2598 case UFSHCD_STATE_EH_SCHEDULED_FATAL:
2600 * pm_runtime_get_sync() is used at error handling preparation
2601 * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's
2602 * PM ops, it can never be finished if we let SCSI layer keep
2603 * retrying it, which gets err handler stuck forever. Neither
2604 * can we let the scsi cmd pass through, because UFS is in bad
2605 * state, the scsi cmd may eventually time out, which will get
2606 * err handler blocked for too long. So, just fail the scsi cmd
2607 * sent from PM ops, err handler can recover PM error anyways.
2609 if (hba->pm_op_in_progress) {
2610 hba->force_reset = true;
2611 set_host_byte(cmd, DID_BAD_TARGET);
2615 case UFSHCD_STATE_RESET:
2616 err = SCSI_MLQUEUE_HOST_BUSY;
2618 case UFSHCD_STATE_ERROR:
2619 set_host_byte(cmd, DID_ERROR);
2622 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2623 __func__, hba->ufshcd_state);
2624 set_host_byte(cmd, DID_BAD_TARGET);
2627 ufshcd_send_command(hba, tag);
2628 spin_unlock_irqrestore(hba->host->host_lock, flags);
2632 scsi_dma_unmap(lrbp->cmd);
2634 spin_unlock_irqrestore(hba->host->host_lock, flags);
2635 ufshcd_release(hba);
2637 cmd->scsi_done(cmd);
2639 up_read(&hba->clk_scaling_lock);
2643 static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2644 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2647 lrbp->sense_bufflen = 0;
2648 lrbp->sense_buffer = NULL;
2649 lrbp->task_tag = tag;
2650 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
2651 lrbp->intr_cmd = true; /* No interrupt aggregation */
2652 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
2653 hba->dev_cmd.type = cmd_type;
2655 return ufshcd_compose_devman_upiu(hba, lrbp);
2659 ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2662 unsigned long flags;
2663 u32 mask = 1 << tag;
2665 /* clear outstanding transaction before retry */
2666 spin_lock_irqsave(hba->host->host_lock, flags);
2667 ufshcd_utrl_clear(hba, tag);
2668 spin_unlock_irqrestore(hba->host->host_lock, flags);
2671 * wait for for h/w to clear corresponding bit in door-bell.
2672 * max. wait is 1 sec.
2674 err = ufshcd_wait_for_register(hba,
2675 REG_UTP_TRANSFER_REQ_DOOR_BELL,
2676 mask, ~mask, 1000, 1000);
2682 ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2684 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2686 /* Get the UPIU response */
2687 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2688 UPIU_RSP_CODE_OFFSET;
2689 return query_res->response;
2693 * ufshcd_dev_cmd_completion() - handles device management command responses
2694 * @hba: per adapter instance
2695 * @lrbp: pointer to local reference block
2698 ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2703 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
2704 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2707 case UPIU_TRANSACTION_NOP_IN:
2708 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2710 dev_err(hba->dev, "%s: unexpected response %x\n",
2714 case UPIU_TRANSACTION_QUERY_RSP:
2715 err = ufshcd_check_query_response(hba, lrbp);
2717 err = ufshcd_copy_query_response(hba, lrbp);
2719 case UPIU_TRANSACTION_REJECT_UPIU:
2720 /* TODO: handle Reject UPIU Response */
2722 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2727 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2735 static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2736 struct ufshcd_lrb *lrbp, int max_timeout)
2739 unsigned long time_left;
2740 unsigned long flags;
2742 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2743 msecs_to_jiffies(max_timeout));
2745 /* Make sure descriptors are ready before ringing the doorbell */
2747 spin_lock_irqsave(hba->host->host_lock, flags);
2748 hba->dev_cmd.complete = NULL;
2749 if (likely(time_left)) {
2750 err = ufshcd_get_tr_ocs(lrbp);
2752 err = ufshcd_dev_cmd_completion(hba, lrbp);
2754 spin_unlock_irqrestore(hba->host->host_lock, flags);
2758 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2759 __func__, lrbp->task_tag);
2760 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
2761 /* successfully cleared the command, retry if needed */
2764 * in case of an error, after clearing the doorbell,
2765 * we also need to clear the outstanding_request
2768 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
2775 * ufshcd_exec_dev_cmd - API for sending device management requests
2777 * @cmd_type: specifies the type (NOP, Query...)
2778 * @timeout: time in seconds
2780 * NOTE: Since there is only one available tag for device management commands,
2781 * it is expected you hold the hba->dev_cmd.lock mutex.
2783 static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2784 enum dev_cmd_type cmd_type, int timeout)
2786 struct request_queue *q = hba->cmd_queue;
2787 struct request *req;
2788 struct ufshcd_lrb *lrbp;
2791 struct completion wait;
2792 unsigned long flags;
2794 down_read(&hba->clk_scaling_lock);
2797 * Get free slot, sleep if slots are unavailable.
2798 * Even though we use wait_event() which sleeps indefinitely,
2799 * the maximum wait time is bounded by SCSI request timeout.
2801 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
2807 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
2809 init_completion(&wait);
2810 lrbp = &hba->lrb[tag];
2811 if (unlikely(lrbp->in_use)) {
2817 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2821 hba->dev_cmd.complete = &wait;
2823 ufshcd_add_query_upiu_trace(hba, tag, "query_send");
2824 /* Make sure descriptors are ready before ringing the doorbell */
2826 spin_lock_irqsave(hba->host->host_lock, flags);
2827 ufshcd_send_command(hba, tag);
2828 spin_unlock_irqrestore(hba->host->host_lock, flags);
2830 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2833 ufshcd_add_query_upiu_trace(hba, tag,
2834 err ? "query_complete_err" : "query_complete");
2837 blk_put_request(req);
2839 up_read(&hba->clk_scaling_lock);
2844 * ufshcd_init_query() - init the query response and request parameters
2845 * @hba: per-adapter instance
2846 * @request: address of the request pointer to be initialized
2847 * @response: address of the response pointer to be initialized
2848 * @opcode: operation to perform
2849 * @idn: flag idn to access
2850 * @index: LU number to access
2851 * @selector: query/flag/descriptor further identification
2853 static inline void ufshcd_init_query(struct ufs_hba *hba,
2854 struct ufs_query_req **request, struct ufs_query_res **response,
2855 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2857 *request = &hba->dev_cmd.query.request;
2858 *response = &hba->dev_cmd.query.response;
2859 memset(*request, 0, sizeof(struct ufs_query_req));
2860 memset(*response, 0, sizeof(struct ufs_query_res));
2861 (*request)->upiu_req.opcode = opcode;
2862 (*request)->upiu_req.idn = idn;
2863 (*request)->upiu_req.index = index;
2864 (*request)->upiu_req.selector = selector;
2867 static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2868 enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
2873 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
2874 ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res);
2877 "%s: failed with error %d, retries %d\n",
2878 __func__, ret, retries);
2885 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2886 __func__, opcode, idn, ret, retries);
2891 * ufshcd_query_flag() - API function for sending flag query requests
2892 * @hba: per-adapter instance
2893 * @opcode: flag query to perform
2894 * @idn: flag idn to access
2895 * @index: flag index to access
2896 * @flag_res: the flag value after the query request completes
2898 * Returns 0 for success, non-zero in case of failure
2900 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
2901 enum flag_idn idn, u8 index, bool *flag_res)
2903 struct ufs_query_req *request = NULL;
2904 struct ufs_query_res *response = NULL;
2905 int err, selector = 0;
2906 int timeout = QUERY_REQ_TIMEOUT;
2910 ufshcd_hold(hba, false);
2911 mutex_lock(&hba->dev_cmd.lock);
2912 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2916 case UPIU_QUERY_OPCODE_SET_FLAG:
2917 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2918 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2919 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2921 case UPIU_QUERY_OPCODE_READ_FLAG:
2922 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2924 /* No dummy reads */
2925 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2933 "%s: Expected query flag opcode but got = %d\n",
2939 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
2943 "%s: Sending flag query for idn %d failed, err = %d\n",
2944 __func__, idn, err);
2949 *flag_res = (be32_to_cpu(response->upiu_res.value) &
2950 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2953 mutex_unlock(&hba->dev_cmd.lock);
2954 ufshcd_release(hba);
2959 * ufshcd_query_attr - API function for sending attribute requests
2960 * @hba: per-adapter instance
2961 * @opcode: attribute opcode
2962 * @idn: attribute idn to access
2963 * @index: index field
2964 * @selector: selector field
2965 * @attr_val: the attribute value after the query request completes
2967 * Returns 0 for success, non-zero in case of failure
2969 int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
2970 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
2972 struct ufs_query_req *request = NULL;
2973 struct ufs_query_res *response = NULL;
2979 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2984 ufshcd_hold(hba, false);
2986 mutex_lock(&hba->dev_cmd.lock);
2987 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2991 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2992 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2993 request->upiu_req.value = cpu_to_be32(*attr_val);
2995 case UPIU_QUERY_OPCODE_READ_ATTR:
2996 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2999 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
3005 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3008 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3009 __func__, opcode, idn, index, err);
3013 *attr_val = be32_to_cpu(response->upiu_res.value);
3016 mutex_unlock(&hba->dev_cmd.lock);
3017 ufshcd_release(hba);
3022 * ufshcd_query_attr_retry() - API function for sending query
3023 * attribute with retries
3024 * @hba: per-adapter instance
3025 * @opcode: attribute opcode
3026 * @idn: attribute idn to access
3027 * @index: index field
3028 * @selector: selector field
3029 * @attr_val: the attribute value after the query request
3032 * Returns 0 for success, non-zero in case of failure
3034 static int ufshcd_query_attr_retry(struct ufs_hba *hba,
3035 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
3041 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3042 ret = ufshcd_query_attr(hba, opcode, idn, index,
3043 selector, attr_val);
3045 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
3046 __func__, ret, retries);
3053 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
3054 __func__, idn, ret, QUERY_REQ_RETRIES);
3058 static int __ufshcd_query_descriptor(struct ufs_hba *hba,
3059 enum query_opcode opcode, enum desc_idn idn, u8 index,
3060 u8 selector, u8 *desc_buf, int *buf_len)
3062 struct ufs_query_req *request = NULL;
3063 struct ufs_query_res *response = NULL;
3069 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3074 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
3075 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3076 __func__, *buf_len);
3080 ufshcd_hold(hba, false);
3082 mutex_lock(&hba->dev_cmd.lock);
3083 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3085 hba->dev_cmd.query.descriptor = desc_buf;
3086 request->upiu_req.length = cpu_to_be16(*buf_len);
3089 case UPIU_QUERY_OPCODE_WRITE_DESC:
3090 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3092 case UPIU_QUERY_OPCODE_READ_DESC:
3093 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3097 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3103 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3106 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3107 __func__, opcode, idn, index, err);
3111 *buf_len = be16_to_cpu(response->upiu_res.length);
3114 hba->dev_cmd.query.descriptor = NULL;
3115 mutex_unlock(&hba->dev_cmd.lock);
3116 ufshcd_release(hba);
3121 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
3122 * @hba: per-adapter instance
3123 * @opcode: attribute opcode
3124 * @idn: attribute idn to access
3125 * @index: index field
3126 * @selector: selector field
3127 * @desc_buf: the buffer that contains the descriptor
3128 * @buf_len: length parameter passed to the device
3130 * Returns 0 for success, non-zero in case of failure.
3131 * The buf_len parameter will contain, on return, the length parameter
3132 * received on the response.
3134 int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
3135 enum query_opcode opcode,
3136 enum desc_idn idn, u8 index,
3138 u8 *desc_buf, int *buf_len)
3143 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3144 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3145 selector, desc_buf, buf_len);
3146 if (!err || err == -EINVAL)
3154 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
3155 * @hba: Pointer to adapter instance
3156 * @desc_id: descriptor idn value
3157 * @desc_len: mapped desc length (out)
3159 void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
3162 if (desc_id >= QUERY_DESC_IDN_MAX || desc_id == QUERY_DESC_IDN_RFU_0 ||
3163 desc_id == QUERY_DESC_IDN_RFU_1)
3166 *desc_len = hba->desc_size[desc_id];
3168 EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
3170 static void ufshcd_update_desc_length(struct ufs_hba *hba,
3171 enum desc_idn desc_id, int desc_index,
3172 unsigned char desc_len)
3174 if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE &&
3175 desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT)
3176 /* For UFS 3.1, the normal unit descriptor is 10 bytes larger
3177 * than the RPMB unit, however, both descriptors share the same
3178 * desc_idn, to cover both unit descriptors with one length, we
3179 * choose the normal unit descriptor length by desc_index.
3181 hba->desc_size[desc_id] = desc_len;
3185 * ufshcd_read_desc_param - read the specified descriptor parameter
3186 * @hba: Pointer to adapter instance
3187 * @desc_id: descriptor idn value
3188 * @desc_index: descriptor index
3189 * @param_offset: offset of the parameter to read
3190 * @param_read_buf: pointer to buffer where parameter would be read
3191 * @param_size: sizeof(param_read_buf)
3193 * Return 0 in case of success, non-zero otherwise
3195 int ufshcd_read_desc_param(struct ufs_hba *hba,
3196 enum desc_idn desc_id,
3205 bool is_kmalloc = true;
3208 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
3211 /* Get the length of descriptor */
3212 ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
3214 dev_err(hba->dev, "%s: Failed to get desc length", __func__);
3218 /* Check whether we need temp memory */
3219 if (param_offset != 0 || param_size < buff_len) {
3220 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3224 desc_buf = param_read_buf;
3228 /* Request for full descriptor */
3229 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
3230 desc_id, desc_index, 0,
3231 desc_buf, &buff_len);
3234 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3235 __func__, desc_id, desc_index, param_offset, ret);
3240 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3241 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3242 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3247 /* Update descriptor length */
3248 buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET];
3249 ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len);
3251 /* Check wherher we will not copy more data, than available */
3252 if (is_kmalloc && (param_offset + param_size) > buff_len)
3253 param_size = buff_len - param_offset;
3256 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3264 * struct uc_string_id - unicode string
3266 * @len: size of this descriptor inclusive
3267 * @type: descriptor type
3268 * @uc: unicode string character
3270 struct uc_string_id {
3276 /* replace non-printable or non-ASCII characters with spaces */
3277 static inline char ufshcd_remove_non_printable(u8 ch)
3279 return (ch >= 0x20 && ch <= 0x7e) ? ch : ' ';
3283 * ufshcd_read_string_desc - read string descriptor
3284 * @hba: pointer to adapter instance
3285 * @desc_index: descriptor index
3286 * @buf: pointer to buffer where descriptor would be read,
3287 * the caller should free the memory.
3288 * @ascii: if true convert from unicode to ascii characters
3289 * null terminated string.
3292 * * string size on success.
3293 * * -ENOMEM: on allocation failure
3294 * * -EINVAL: on a wrong parameter
3296 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
3297 u8 **buf, bool ascii)
3299 struct uc_string_id *uc_str;
3306 uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
3310 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0,
3311 (u8 *)uc_str, QUERY_DESC_MAX_SIZE);
3313 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
3314 QUERY_REQ_RETRIES, ret);
3319 if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
3320 dev_dbg(hba->dev, "String Desc is of zero length\n");
3329 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3330 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3331 str = kzalloc(ascii_len, GFP_KERNEL);
3338 * the descriptor contains string in UTF16 format
3339 * we need to convert to utf-8 so it can be displayed
3341 ret = utf16s_to_utf8s(uc_str->uc,
3342 uc_str->len - QUERY_DESC_HDR_SIZE,
3343 UTF16_BIG_ENDIAN, str, ascii_len);
3345 /* replace non-printable or non-ASCII characters with spaces */
3346 for (i = 0; i < ret; i++)
3347 str[i] = ufshcd_remove_non_printable(str[i]);
3352 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL);
3366 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3367 * @hba: Pointer to adapter instance
3369 * @param_offset: offset of the parameter to read
3370 * @param_read_buf: pointer to buffer where parameter would be read
3371 * @param_size: sizeof(param_read_buf)
3373 * Return 0 in case of success, non-zero otherwise
3375 static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3377 enum unit_desc_param param_offset,
3382 * Unit descriptors are only available for general purpose LUs (LUN id
3383 * from 0 to 7) and RPMB Well known LU.
3385 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun))
3388 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3389 param_offset, param_read_buf, param_size);
3392 static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba)
3395 u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3397 if (hba->dev_info.wspecversion >= 0x300) {
3398 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3399 QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0,
3402 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n",
3405 if (gating_wait == 0) {
3406 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3407 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n",
3411 hba->dev_info.clk_gating_wait_us = gating_wait;
3418 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3419 * @hba: per adapter instance
3421 * 1. Allocate DMA memory for Command Descriptor array
3422 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3423 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3424 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3426 * 4. Allocate memory for local reference block(lrb).
3428 * Returns 0 for success, non-zero in case of failure
3430 static int ufshcd_memory_alloc(struct ufs_hba *hba)
3432 size_t utmrdl_size, utrdl_size, ucdl_size;
3434 /* Allocate memory for UTP command descriptors */
3435 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
3436 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3438 &hba->ucdl_dma_addr,
3442 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3443 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3444 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3445 * be aligned to 128 bytes as well
3447 if (!hba->ucdl_base_addr ||
3448 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
3450 "Command Descriptor Memory allocation failed\n");
3455 * Allocate memory for UTP Transfer descriptors
3456 * UFSHCI requires 1024 byte alignment of UTRD
3458 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
3459 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3461 &hba->utrdl_dma_addr,
3463 if (!hba->utrdl_base_addr ||
3464 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
3466 "Transfer Descriptor Memory allocation failed\n");
3471 * Allocate memory for UTP Task Management descriptors
3472 * UFSHCI requires 1024 byte alignment of UTMRD
3474 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
3475 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3477 &hba->utmrdl_dma_addr,
3479 if (!hba->utmrdl_base_addr ||
3480 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
3482 "Task Management Descriptor Memory allocation failed\n");
3486 /* Allocate memory for local reference block */
3487 hba->lrb = devm_kcalloc(hba->dev,
3488 hba->nutrs, sizeof(struct ufshcd_lrb),
3491 dev_err(hba->dev, "LRB Memory allocation failed\n");
3500 * ufshcd_host_memory_configure - configure local reference block with
3502 * @hba: per adapter instance
3504 * Configure Host memory space
3505 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3507 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3509 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3510 * into local reference block.
3512 static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3514 struct utp_transfer_req_desc *utrdlp;
3515 dma_addr_t cmd_desc_dma_addr;
3516 dma_addr_t cmd_desc_element_addr;
3517 u16 response_offset;
3522 utrdlp = hba->utrdl_base_addr;
3525 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3527 offsetof(struct utp_transfer_cmd_desc, prd_table);
3529 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3530 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3532 for (i = 0; i < hba->nutrs; i++) {
3533 /* Configure UTRD with command descriptor base address */
3534 cmd_desc_element_addr =
3535 (cmd_desc_dma_addr + (cmd_desc_size * i));
3536 utrdlp[i].command_desc_base_addr_lo =
3537 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3538 utrdlp[i].command_desc_base_addr_hi =
3539 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3541 /* Response upiu and prdt offset should be in double words */
3542 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3543 utrdlp[i].response_upiu_offset =
3544 cpu_to_le16(response_offset);
3545 utrdlp[i].prd_table_offset =
3546 cpu_to_le16(prdt_offset);
3547 utrdlp[i].response_upiu_length =
3548 cpu_to_le16(ALIGNED_UPIU_SIZE);
3550 utrdlp[i].response_upiu_offset =
3551 cpu_to_le16(response_offset >> 2);
3552 utrdlp[i].prd_table_offset =
3553 cpu_to_le16(prdt_offset >> 2);
3554 utrdlp[i].response_upiu_length =
3555 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
3558 ufshcd_init_lrb(hba, &hba->lrb[i], i);
3563 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3564 * @hba: per adapter instance
3566 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3567 * in order to initialize the Unipro link startup procedure.
3568 * Once the Unipro links are up, the device connected to the controller
3571 * Returns 0 on success, non-zero value on failure
3573 static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3575 struct uic_command uic_cmd = {0};
3578 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3580 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3583 "dme-link-startup: error code %d\n", ret);
3587 * ufshcd_dme_reset - UIC command for DME_RESET
3588 * @hba: per adapter instance
3590 * DME_RESET command is issued in order to reset UniPro stack.
3591 * This function now deals with cold reset.
3593 * Returns 0 on success, non-zero value on failure
3595 static int ufshcd_dme_reset(struct ufs_hba *hba)
3597 struct uic_command uic_cmd = {0};
3600 uic_cmd.command = UIC_CMD_DME_RESET;
3602 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3605 "dme-reset: error code %d\n", ret);
3610 int ufshcd_dme_configure_adapt(struct ufs_hba *hba,
3616 if (agreed_gear != UFS_HS_G4)
3617 adapt_val = PA_NO_ADAPT;
3619 ret = ufshcd_dme_set(hba,
3620 UIC_ARG_MIB(PA_TXHSADAPTTYPE),
3624 EXPORT_SYMBOL_GPL(ufshcd_dme_configure_adapt);
3627 * ufshcd_dme_enable - UIC command for DME_ENABLE
3628 * @hba: per adapter instance
3630 * DME_ENABLE command is issued in order to enable UniPro stack.
3632 * Returns 0 on success, non-zero value on failure
3634 static int ufshcd_dme_enable(struct ufs_hba *hba)
3636 struct uic_command uic_cmd = {0};
3639 uic_cmd.command = UIC_CMD_DME_ENABLE;
3641 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3644 "dme-reset: error code %d\n", ret);
3649 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3651 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3652 unsigned long min_sleep_time_us;
3654 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3658 * last_dme_cmd_tstamp will be 0 only for 1st call to
3661 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3662 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3664 unsigned long delta =
3665 (unsigned long) ktime_to_us(
3666 ktime_sub(ktime_get(),
3667 hba->last_dme_cmd_tstamp));
3669 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3671 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3673 return; /* no more delay required */
3676 /* allow sleep for extra 50us if needed */
3677 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3681 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3682 * @hba: per adapter instance
3683 * @attr_sel: uic command argument1
3684 * @attr_set: attribute set type as uic command argument2
3685 * @mib_val: setting value as uic command argument3
3686 * @peer: indicate whether peer or local
3688 * Returns 0 on success, non-zero value on failure
3690 int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3691 u8 attr_set, u32 mib_val, u8 peer)
3693 struct uic_command uic_cmd = {0};
3694 static const char *const action[] = {
3698 const char *set = action[!!peer];
3700 int retries = UFS_UIC_COMMAND_RETRIES;
3702 uic_cmd.command = peer ?
3703 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3704 uic_cmd.argument1 = attr_sel;
3705 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3706 uic_cmd.argument3 = mib_val;
3709 /* for peer attributes we retry upon failure */
3710 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3712 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3713 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3714 } while (ret && peer && --retries);
3717 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
3718 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3719 UFS_UIC_COMMAND_RETRIES - retries);
3723 EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3726 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3727 * @hba: per adapter instance
3728 * @attr_sel: uic command argument1
3729 * @mib_val: the value of the attribute as returned by the UIC command
3730 * @peer: indicate whether peer or local
3732 * Returns 0 on success, non-zero value on failure
3734 int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3735 u32 *mib_val, u8 peer)
3737 struct uic_command uic_cmd = {0};
3738 static const char *const action[] = {
3742 const char *get = action[!!peer];
3744 int retries = UFS_UIC_COMMAND_RETRIES;
3745 struct ufs_pa_layer_attr orig_pwr_info;
3746 struct ufs_pa_layer_attr temp_pwr_info;
3747 bool pwr_mode_change = false;
3749 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3750 orig_pwr_info = hba->pwr_info;
3751 temp_pwr_info = orig_pwr_info;
3753 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3754 orig_pwr_info.pwr_rx == FAST_MODE) {
3755 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3756 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3757 pwr_mode_change = true;
3758 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3759 orig_pwr_info.pwr_rx == SLOW_MODE) {
3760 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3761 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3762 pwr_mode_change = true;
3764 if (pwr_mode_change) {
3765 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3771 uic_cmd.command = peer ?
3772 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3773 uic_cmd.argument1 = attr_sel;
3776 /* for peer attributes we retry upon failure */
3777 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3779 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3780 get, UIC_GET_ATTR_ID(attr_sel), ret);
3781 } while (ret && peer && --retries);
3784 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
3785 get, UIC_GET_ATTR_ID(attr_sel),
3786 UFS_UIC_COMMAND_RETRIES - retries);
3788 if (mib_val && !ret)
3789 *mib_val = uic_cmd.argument3;
3791 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3793 ufshcd_change_power_mode(hba, &orig_pwr_info);
3797 EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3800 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3801 * state) and waits for it to take effect.
3803 * @hba: per adapter instance
3804 * @cmd: UIC command to execute
3806 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3807 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3808 * and device UniPro link and hence it's final completion would be indicated by
3809 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3810 * addition to normal UIC command completion Status (UCCS). This function only
3811 * returns after the relevant status bits indicate the completion.
3813 * Returns 0 on success, non-zero value on failure
3815 static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3817 struct completion uic_async_done;
3818 unsigned long flags;
3821 bool reenable_intr = false;
3823 mutex_lock(&hba->uic_cmd_mutex);
3824 init_completion(&uic_async_done);
3825 ufshcd_add_delay_before_dme_cmd(hba);
3827 spin_lock_irqsave(hba->host->host_lock, flags);
3828 if (ufshcd_is_link_broken(hba)) {
3832 hba->uic_async_done = &uic_async_done;
3833 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3834 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3836 * Make sure UIC command completion interrupt is disabled before
3837 * issuing UIC command.
3840 reenable_intr = true;
3842 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3843 spin_unlock_irqrestore(hba->host->host_lock, flags);
3846 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3847 cmd->command, cmd->argument3, ret);
3851 if (!wait_for_completion_timeout(hba->uic_async_done,
3852 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3854 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3855 cmd->command, cmd->argument3);
3860 status = ufshcd_get_upmcrs(hba);
3861 if (status != PWR_LOCAL) {
3863 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
3864 cmd->command, status);
3865 ret = (status != PWR_OK) ? status : -1;
3869 ufshcd_print_host_state(hba);
3870 ufshcd_print_pwr_info(hba);
3871 ufshcd_print_evt_hist(hba);
3874 spin_lock_irqsave(hba->host->host_lock, flags);
3875 hba->active_uic_cmd = NULL;
3876 hba->uic_async_done = NULL;
3878 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
3880 ufshcd_set_link_broken(hba);
3881 ufshcd_schedule_eh_work(hba);
3884 spin_unlock_irqrestore(hba->host->host_lock, flags);
3885 mutex_unlock(&hba->uic_cmd_mutex);
3891 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3892 * using DME_SET primitives.
3893 * @hba: per adapter instance
3894 * @mode: powr mode value
3896 * Returns 0 on success, non-zero value on failure
3898 static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
3900 struct uic_command uic_cmd = {0};
3903 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3904 ret = ufshcd_dme_set(hba,
3905 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3907 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3913 uic_cmd.command = UIC_CMD_DME_SET;
3914 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3915 uic_cmd.argument3 = mode;
3916 ufshcd_hold(hba, false);
3917 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3918 ufshcd_release(hba);
3924 int ufshcd_link_recovery(struct ufs_hba *hba)
3927 unsigned long flags;
3929 spin_lock_irqsave(hba->host->host_lock, flags);
3930 hba->ufshcd_state = UFSHCD_STATE_RESET;
3931 ufshcd_set_eh_in_progress(hba);
3932 spin_unlock_irqrestore(hba->host->host_lock, flags);
3934 /* Reset the attached device */
3935 ufshcd_vops_device_reset(hba);
3937 ret = ufshcd_host_reset_and_restore(hba);
3939 spin_lock_irqsave(hba->host->host_lock, flags);
3941 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3942 ufshcd_clear_eh_in_progress(hba);
3943 spin_unlock_irqrestore(hba->host->host_lock, flags);
3946 dev_err(hba->dev, "%s: link recovery failed, err %d",
3951 EXPORT_SYMBOL_GPL(ufshcd_link_recovery);
3953 static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3956 struct uic_command uic_cmd = {0};
3957 ktime_t start = ktime_get();
3959 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3961 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
3962 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3963 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
3964 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3967 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3970 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3976 int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
3978 struct uic_command uic_cmd = {0};
3980 ktime_t start = ktime_get();
3982 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3984 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3985 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3986 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
3987 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3990 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3993 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3995 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
3996 hba->ufs_stats.hibern8_exit_cnt++;
4001 EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit);
4003 void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit)
4005 unsigned long flags;
4006 bool update = false;
4008 if (!ufshcd_is_auto_hibern8_supported(hba))
4011 spin_lock_irqsave(hba->host->host_lock, flags);
4012 if (hba->ahit != ahit) {
4016 spin_unlock_irqrestore(hba->host->host_lock, flags);
4018 if (update && !pm_runtime_suspended(hba->dev)) {
4019 pm_runtime_get_sync(hba->dev);
4020 ufshcd_hold(hba, false);
4021 ufshcd_auto_hibern8_enable(hba);
4022 ufshcd_release(hba);
4023 pm_runtime_put(hba->dev);
4026 EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
4028 void ufshcd_auto_hibern8_enable(struct ufs_hba *hba)
4030 unsigned long flags;
4032 if (!ufshcd_is_auto_hibern8_supported(hba))
4035 spin_lock_irqsave(hba->host->host_lock, flags);
4036 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
4037 spin_unlock_irqrestore(hba->host->host_lock, flags);
4041 * ufshcd_init_pwr_info - setting the POR (power on reset)
4042 * values in hba power info
4043 * @hba: per-adapter instance
4045 static void ufshcd_init_pwr_info(struct ufs_hba *hba)
4047 hba->pwr_info.gear_rx = UFS_PWM_G1;
4048 hba->pwr_info.gear_tx = UFS_PWM_G1;
4049 hba->pwr_info.lane_rx = 1;
4050 hba->pwr_info.lane_tx = 1;
4051 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
4052 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
4053 hba->pwr_info.hs_rate = 0;
4057 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4058 * @hba: per-adapter instance
4060 static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
4062 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4064 if (hba->max_pwr_info.is_valid)
4067 pwr_info->pwr_tx = FAST_MODE;
4068 pwr_info->pwr_rx = FAST_MODE;
4069 pwr_info->hs_rate = PA_HS_MODE_B;
4071 /* Get the connected lane count */
4072 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4073 &pwr_info->lane_rx);
4074 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4075 &pwr_info->lane_tx);
4077 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4078 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4086 * First, get the maximum gears of HS speed.
4087 * If a zero value, it means there is no HSGEAR capability.
4088 * Then, get the maximum gears of PWM speed.
4090 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4091 if (!pwr_info->gear_rx) {
4092 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4093 &pwr_info->gear_rx);
4094 if (!pwr_info->gear_rx) {
4095 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4096 __func__, pwr_info->gear_rx);
4099 pwr_info->pwr_rx = SLOW_MODE;
4102 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4103 &pwr_info->gear_tx);
4104 if (!pwr_info->gear_tx) {
4105 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4106 &pwr_info->gear_tx);
4107 if (!pwr_info->gear_tx) {
4108 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4109 __func__, pwr_info->gear_tx);
4112 pwr_info->pwr_tx = SLOW_MODE;
4115 hba->max_pwr_info.is_valid = true;
4119 static int ufshcd_change_power_mode(struct ufs_hba *hba,
4120 struct ufs_pa_layer_attr *pwr_mode)
4124 /* if already configured to the requested pwr_mode */
4125 if (!hba->force_pmc &&
4126 pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4127 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4128 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4129 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4130 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4131 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4132 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4133 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4138 * Configure attributes for power mode change with below.
4139 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4140 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4143 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4144 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4146 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4147 pwr_mode->pwr_rx == FAST_MODE)
4148 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
4150 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
4152 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4153 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4155 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4156 pwr_mode->pwr_tx == FAST_MODE)
4157 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
4159 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
4161 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4162 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4163 pwr_mode->pwr_rx == FAST_MODE ||
4164 pwr_mode->pwr_tx == FAST_MODE)
4165 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4168 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4169 DL_FC0ProtectionTimeOutVal_Default);
4170 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4171 DL_TC0ReplayTimeOutVal_Default);
4172 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4173 DL_AFC0ReqTimeOutVal_Default);
4174 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
4175 DL_FC1ProtectionTimeOutVal_Default);
4176 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
4177 DL_TC1ReplayTimeOutVal_Default);
4178 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
4179 DL_AFC1ReqTimeOutVal_Default);
4181 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4182 DL_FC0ProtectionTimeOutVal_Default);
4183 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4184 DL_TC0ReplayTimeOutVal_Default);
4185 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4186 DL_AFC0ReqTimeOutVal_Default);
4188 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4189 | pwr_mode->pwr_tx);
4193 "%s: power mode change failed %d\n", __func__, ret);
4195 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4198 memcpy(&hba->pwr_info, pwr_mode,
4199 sizeof(struct ufs_pa_layer_attr));
4206 * ufshcd_config_pwr_mode - configure a new power mode
4207 * @hba: per-adapter instance
4208 * @desired_pwr_mode: desired power configuration
4210 int ufshcd_config_pwr_mode(struct ufs_hba *hba,
4211 struct ufs_pa_layer_attr *desired_pwr_mode)
4213 struct ufs_pa_layer_attr final_params = { 0 };
4216 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4217 desired_pwr_mode, &final_params);
4220 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4222 ret = ufshcd_change_power_mode(hba, &final_params);
4226 EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
4229 * ufshcd_complete_dev_init() - checks device readiness
4230 * @hba: per-adapter instance
4232 * Set fDeviceInit flag and poll until device toggles it.
4234 static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4237 bool flag_res = true;
4240 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
4241 QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL);
4244 "%s setting fDeviceInit flag failed with error %d\n",
4249 /* Poll fDeviceInit flag to be cleared */
4250 timeout = ktime_add_ms(ktime_get(), FDEVICEINIT_COMPL_TIMEOUT);
4252 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4253 QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res);
4256 usleep_range(5000, 10000);
4257 } while (ktime_before(ktime_get(), timeout));
4261 "%s reading fDeviceInit flag failed with error %d\n",
4263 } else if (flag_res) {
4265 "%s fDeviceInit was not cleared by the device\n",
4274 * ufshcd_make_hba_operational - Make UFS controller operational
4275 * @hba: per adapter instance
4277 * To bring UFS host controller to operational state,
4278 * 1. Enable required interrupts
4279 * 2. Configure interrupt aggregation
4280 * 3. Program UTRL and UTMRL base address
4281 * 4. Configure run-stop-registers
4283 * Returns 0 on success, non-zero value on failure
4285 int ufshcd_make_hba_operational(struct ufs_hba *hba)
4290 /* Enable required interrupts */
4291 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4293 /* Configure interrupt aggregation */
4294 if (ufshcd_is_intr_aggr_allowed(hba))
4295 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4297 ufshcd_disable_intr_aggr(hba);
4299 /* Configure UTRL and UTMRL base address registers */
4300 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4301 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4302 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4303 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4304 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4305 REG_UTP_TASK_REQ_LIST_BASE_L);
4306 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4307 REG_UTP_TASK_REQ_LIST_BASE_H);
4310 * Make sure base address and interrupt setup are updated before
4311 * enabling the run/stop registers below.
4316 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
4318 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
4319 if (!(ufshcd_get_lists_status(reg))) {
4320 ufshcd_enable_run_stop_reg(hba);
4323 "Host controller not ready to process requests");
4329 EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
4332 * ufshcd_hba_stop - Send controller to reset state
4333 * @hba: per adapter instance
4335 static inline void ufshcd_hba_stop(struct ufs_hba *hba)
4337 unsigned long flags;
4341 * Obtain the host lock to prevent that the controller is disabled
4342 * while the UFS interrupt handler is active on another CPU.
4344 spin_lock_irqsave(hba->host->host_lock, flags);
4345 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
4346 spin_unlock_irqrestore(hba->host->host_lock, flags);
4348 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4349 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
4352 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4356 * ufshcd_hba_execute_hce - initialize the controller
4357 * @hba: per adapter instance
4359 * The controller resets itself and controller firmware initialization
4360 * sequence kicks off. When controller is ready it will set
4361 * the Host Controller Enable bit to 1.
4363 * Returns 0 on success, non-zero value on failure
4365 static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
4367 int retry_outer = 3;
4371 if (!ufshcd_is_hba_active(hba))
4372 /* change controller state to "reset state" */
4373 ufshcd_hba_stop(hba);
4375 /* UniPro link is disabled at this point */
4376 ufshcd_set_link_off(hba);
4378 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4380 /* start controller initialization sequence */
4381 ufshcd_hba_start(hba);
4384 * To initialize a UFS host controller HCE bit must be set to 1.
4385 * During initialization the HCE bit value changes from 1->0->1.
4386 * When the host controller completes initialization sequence
4387 * it sets the value of HCE bit to 1. The same HCE bit is read back
4388 * to check if the controller has completed initialization sequence.
4389 * So without this delay the value HCE = 1, set in the previous
4390 * instruction might be read back.
4391 * This delay can be changed based on the controller.
4393 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100);
4395 /* wait for the host controller to complete initialization */
4397 while (ufshcd_is_hba_active(hba)) {
4402 "Controller enable failed\n");
4409 usleep_range(1000, 1100);
4412 /* enable UIC related interrupts */
4413 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4415 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4420 int ufshcd_hba_enable(struct ufs_hba *hba)
4424 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
4425 ufshcd_set_link_off(hba);
4426 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4428 /* enable UIC related interrupts */
4429 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4430 ret = ufshcd_dme_reset(hba);
4432 ret = ufshcd_dme_enable(hba);
4434 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4437 "Host controller enable failed with non-hce\n");
4440 ret = ufshcd_hba_execute_hce(hba);
4445 EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
4447 static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4449 int tx_lanes = 0, i, err = 0;
4452 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4455 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4457 for (i = 0; i < tx_lanes; i++) {
4459 err = ufshcd_dme_set(hba,
4460 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4461 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4464 err = ufshcd_dme_peer_set(hba,
4465 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4466 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4469 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4470 __func__, peer, i, err);
4478 static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4480 return ufshcd_disable_tx_lcc(hba, true);
4483 void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val)
4485 struct ufs_event_hist *e;
4487 if (id >= UFS_EVT_CNT)
4490 e = &hba->ufs_stats.event[id];
4491 e->val[e->pos] = val;
4492 e->tstamp[e->pos] = ktime_get();
4493 e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH;
4495 ufshcd_vops_event_notify(hba, id, &val);
4497 EXPORT_SYMBOL_GPL(ufshcd_update_evt_hist);
4500 * ufshcd_link_startup - Initialize unipro link startup
4501 * @hba: per adapter instance
4503 * Returns 0 for success, non-zero in case of failure
4505 static int ufshcd_link_startup(struct ufs_hba *hba)
4508 int retries = DME_LINKSTARTUP_RETRIES;
4509 bool link_startup_again = false;
4512 * If UFS device isn't active then we will have to issue link startup
4513 * 2 times to make sure the device state move to active.
4515 if (!ufshcd_is_ufs_dev_active(hba))
4516 link_startup_again = true;
4520 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
4522 ret = ufshcd_dme_link_startup(hba);
4524 /* check if device is detected by inter-connect layer */
4525 if (!ret && !ufshcd_is_device_present(hba)) {
4526 ufshcd_update_evt_hist(hba,
4527 UFS_EVT_LINK_STARTUP_FAIL,
4529 dev_err(hba->dev, "%s: Device not present\n", __func__);
4535 * DME link lost indication is only received when link is up,
4536 * but we can't be sure if the link is up until link startup
4537 * succeeds. So reset the local Uni-Pro and try again.
4539 if (ret && ufshcd_hba_enable(hba)) {
4540 ufshcd_update_evt_hist(hba,
4541 UFS_EVT_LINK_STARTUP_FAIL,
4545 } while (ret && retries--);
4548 /* failed to get the link up... retire */
4549 ufshcd_update_evt_hist(hba,
4550 UFS_EVT_LINK_STARTUP_FAIL,
4555 if (link_startup_again) {
4556 link_startup_again = false;
4557 retries = DME_LINKSTARTUP_RETRIES;
4561 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4562 ufshcd_init_pwr_info(hba);
4563 ufshcd_print_pwr_info(hba);
4565 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4566 ret = ufshcd_disable_device_tx_lcc(hba);
4571 /* Include any host controller configuration via UIC commands */
4572 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4576 /* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */
4577 ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
4578 ret = ufshcd_make_hba_operational(hba);
4581 dev_err(hba->dev, "link startup failed %d\n", ret);
4582 ufshcd_print_host_state(hba);
4583 ufshcd_print_pwr_info(hba);
4584 ufshcd_print_evt_hist(hba);
4590 * ufshcd_verify_dev_init() - Verify device initialization
4591 * @hba: per-adapter instance
4593 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4594 * device Transport Protocol (UTP) layer is ready after a reset.
4595 * If the UTP layer at the device side is not initialized, it may
4596 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4597 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4599 static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4604 ufshcd_hold(hba, false);
4605 mutex_lock(&hba->dev_cmd.lock);
4606 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4607 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4610 if (!err || err == -ETIMEDOUT)
4613 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4615 mutex_unlock(&hba->dev_cmd.lock);
4616 ufshcd_release(hba);
4619 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4624 * ufshcd_set_queue_depth - set lun queue depth
4625 * @sdev: pointer to SCSI device
4627 * Read bLUQueueDepth value and activate scsi tagged command
4628 * queueing. For WLUN, queue depth is set to 1. For best-effort
4629 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4630 * value that host can queue.
4632 static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4636 struct ufs_hba *hba;
4638 hba = shost_priv(sdev->host);
4640 lun_qdepth = hba->nutrs;
4641 ret = ufshcd_read_unit_desc_param(hba,
4642 ufshcd_scsi_to_upiu_lun(sdev->lun),
4643 UNIT_DESC_PARAM_LU_Q_DEPTH,
4645 sizeof(lun_qdepth));
4647 /* Some WLUN doesn't support unit descriptor */
4648 if (ret == -EOPNOTSUPP)
4650 else if (!lun_qdepth)
4651 /* eventually, we can figure out the real queue depth */
4652 lun_qdepth = hba->nutrs;
4654 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4656 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4657 __func__, lun_qdepth);
4658 scsi_change_queue_depth(sdev, lun_qdepth);
4662 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4663 * @hba: per-adapter instance
4664 * @lun: UFS device lun id
4665 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4667 * Returns 0 in case of success and b_lu_write_protect status would be returned
4668 * @b_lu_write_protect parameter.
4669 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4670 * Returns -EINVAL in case of invalid parameters passed to this function.
4672 static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4674 u8 *b_lu_write_protect)
4678 if (!b_lu_write_protect)
4681 * According to UFS device spec, RPMB LU can't be write
4682 * protected so skip reading bLUWriteProtect parameter for
4683 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4685 else if (lun >= hba->dev_info.max_lu_supported)
4688 ret = ufshcd_read_unit_desc_param(hba,
4690 UNIT_DESC_PARAM_LU_WR_PROTECT,
4692 sizeof(*b_lu_write_protect));
4697 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4699 * @hba: per-adapter instance
4700 * @sdev: pointer to SCSI device
4703 static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4704 struct scsi_device *sdev)
4706 if (hba->dev_info.f_power_on_wp_en &&
4707 !hba->dev_info.is_lu_power_on_wp) {
4708 u8 b_lu_write_protect;
4710 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4711 &b_lu_write_protect) &&
4712 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4713 hba->dev_info.is_lu_power_on_wp = true;
4718 * ufshcd_slave_alloc - handle initial SCSI device configurations
4719 * @sdev: pointer to SCSI device
4723 static int ufshcd_slave_alloc(struct scsi_device *sdev)
4725 struct ufs_hba *hba;
4727 hba = shost_priv(sdev->host);
4729 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4730 sdev->use_10_for_ms = 1;
4732 /* DBD field should be set to 1 in mode sense(10) */
4733 sdev->set_dbd_for_ms = 1;
4735 /* allow SCSI layer to restart the device in case of errors */
4736 sdev->allow_restart = 1;
4738 /* REPORT SUPPORTED OPERATION CODES is not supported */
4739 sdev->no_report_opcodes = 1;
4741 /* WRITE_SAME command is not supported */
4742 sdev->no_write_same = 1;
4744 ufshcd_set_queue_depth(sdev);
4746 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4752 * ufshcd_change_queue_depth - change queue depth
4753 * @sdev: pointer to SCSI device
4754 * @depth: required depth to set
4756 * Change queue depth and make sure the max. limits are not crossed.
4758 static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
4760 struct ufs_hba *hba = shost_priv(sdev->host);
4762 if (depth > hba->nutrs)
4764 return scsi_change_queue_depth(sdev, depth);
4768 * ufshcd_slave_configure - adjust SCSI device configurations
4769 * @sdev: pointer to SCSI device
4771 static int ufshcd_slave_configure(struct scsi_device *sdev)
4773 struct ufs_hba *hba = shost_priv(sdev->host);
4774 struct request_queue *q = sdev->request_queue;
4776 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
4778 if (ufshcd_is_rpm_autosuspend_allowed(hba))
4779 sdev->rpm_autosuspend = 1;
4781 ufshcd_crypto_setup_rq_keyslot_manager(hba, q);
4787 * ufshcd_slave_destroy - remove SCSI device configurations
4788 * @sdev: pointer to SCSI device
4790 static void ufshcd_slave_destroy(struct scsi_device *sdev)
4792 struct ufs_hba *hba;
4794 hba = shost_priv(sdev->host);
4795 /* Drop the reference as it won't be needed anymore */
4796 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4797 unsigned long flags;
4799 spin_lock_irqsave(hba->host->host_lock, flags);
4800 hba->sdev_ufs_device = NULL;
4801 spin_unlock_irqrestore(hba->host->host_lock, flags);
4806 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4807 * @lrbp: pointer to local reference block of completed command
4808 * @scsi_status: SCSI command status
4810 * Returns value base on SCSI command status
4813 ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4817 switch (scsi_status) {
4818 case SAM_STAT_CHECK_CONDITION:
4819 ufshcd_copy_sense_data(lrbp);
4822 result |= DID_OK << 16 |
4823 COMMAND_COMPLETE << 8 |
4826 case SAM_STAT_TASK_SET_FULL:
4828 case SAM_STAT_TASK_ABORTED:
4829 ufshcd_copy_sense_data(lrbp);
4830 result |= scsi_status;
4833 result |= DID_ERROR << 16;
4835 } /* end of switch */
4841 * ufshcd_transfer_rsp_status - Get overall status of the response
4842 * @hba: per adapter instance
4843 * @lrbp: pointer to local reference block of completed command
4845 * Returns result of the command to notify SCSI midlayer
4848 ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4854 /* overall command status of utrd */
4855 ocs = ufshcd_get_tr_ocs(lrbp);
4857 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) {
4858 if (be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_1) &
4859 MASK_RSP_UPIU_RESULT)
4865 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
4866 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
4868 case UPIU_TRANSACTION_RESPONSE:
4870 * get the response UPIU result to extract
4871 * the SCSI command status
4873 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4876 * get the result based on SCSI status response
4877 * to notify the SCSI midlayer of the command status
4879 scsi_status = result & MASK_SCSI_STATUS;
4880 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
4883 * Currently we are only supporting BKOPs exception
4884 * events hence we can ignore BKOPs exception event
4885 * during power management callbacks. BKOPs exception
4886 * event is not expected to be raised in runtime suspend
4887 * callback as it allows the urgent bkops.
4888 * During system suspend, we are anyway forcefully
4889 * disabling the bkops and if urgent bkops is needed
4890 * it will be enabled on system resume. Long term
4891 * solution could be to abort the system suspend if
4892 * UFS device needs urgent BKOPs.
4894 if (!hba->pm_op_in_progress &&
4895 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr) &&
4896 schedule_work(&hba->eeh_work)) {
4898 * Prevent suspend once eeh_work is scheduled
4899 * to avoid deadlock between ufshcd_suspend
4900 * and exception event handler.
4902 pm_runtime_get_noresume(hba->dev);
4905 case UPIU_TRANSACTION_REJECT_UPIU:
4906 /* TODO: handle Reject UPIU Response */
4907 result = DID_ERROR << 16;
4909 "Reject UPIU not fully implemented\n");
4913 "Unexpected request response code = %x\n",
4915 result = DID_ERROR << 16;
4920 result |= DID_ABORT << 16;
4922 case OCS_INVALID_COMMAND_STATUS:
4923 result |= DID_REQUEUE << 16;
4925 case OCS_INVALID_CMD_TABLE_ATTR:
4926 case OCS_INVALID_PRDT_ATTR:
4927 case OCS_MISMATCH_DATA_BUF_SIZE:
4928 case OCS_MISMATCH_RESP_UPIU_SIZE:
4929 case OCS_PEER_COMM_FAILURE:
4930 case OCS_FATAL_ERROR:
4931 case OCS_DEVICE_FATAL_ERROR:
4932 case OCS_INVALID_CRYPTO_CONFIG:
4933 case OCS_GENERAL_CRYPTO_ERROR:
4935 result |= DID_ERROR << 16;
4937 "OCS error from controller = %x for tag %d\n",
4938 ocs, lrbp->task_tag);
4939 ufshcd_print_evt_hist(hba);
4940 ufshcd_print_host_state(hba);
4942 } /* end of switch */
4944 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs)
4945 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
4950 * ufshcd_uic_cmd_compl - handle completion of uic command
4951 * @hba: per adapter instance
4952 * @intr_status: interrupt status generated by the controller
4955 * IRQ_HANDLED - If interrupt is valid
4956 * IRQ_NONE - If invalid interrupt
4958 static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
4960 irqreturn_t retval = IRQ_NONE;
4962 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
4963 hba->active_uic_cmd->argument2 |=
4964 ufshcd_get_uic_cmd_result(hba);
4965 hba->active_uic_cmd->argument3 =
4966 ufshcd_get_dme_attr_val(hba);
4967 complete(&hba->active_uic_cmd->done);
4968 retval = IRQ_HANDLED;
4971 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
4972 complete(hba->uic_async_done);
4973 retval = IRQ_HANDLED;
4976 if (retval == IRQ_HANDLED)
4977 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd,
4983 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
4984 * @hba: per adapter instance
4985 * @completed_reqs: requests to complete
4987 static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4988 unsigned long completed_reqs)
4990 struct ufshcd_lrb *lrbp;
4991 struct scsi_cmnd *cmd;
4994 bool update_scaling = false;
4996 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4997 lrbp = &hba->lrb[index];
4998 lrbp->in_use = false;
4999 lrbp->compl_time_stamp = ktime_get();
5002 ufshcd_add_command_trace(hba, index, "complete");
5003 result = ufshcd_transfer_rsp_status(hba, lrbp);
5004 scsi_dma_unmap(cmd);
5005 cmd->result = result;
5006 /* Mark completed command as NULL in LRB */
5008 /* Do not touch lrbp after scsi done */
5009 cmd->scsi_done(cmd);
5010 __ufshcd_release(hba);
5011 update_scaling = true;
5012 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
5013 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
5014 if (hba->dev_cmd.complete) {
5015 ufshcd_add_command_trace(hba, index,
5017 complete(hba->dev_cmd.complete);
5018 update_scaling = true;
5021 if (ufshcd_is_clkscaling_supported(hba) && update_scaling)
5022 hba->clk_scaling.active_reqs--;
5025 /* clear corresponding bits of completed commands */
5026 hba->outstanding_reqs ^= completed_reqs;
5028 ufshcd_clk_scaling_update_busy(hba);
5032 * ufshcd_transfer_req_compl - handle SCSI and query command completion
5033 * @hba: per adapter instance
5036 * IRQ_HANDLED - If interrupt is valid
5037 * IRQ_NONE - If invalid interrupt
5039 static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
5041 unsigned long completed_reqs;
5044 /* Resetting interrupt aggregation counters first and reading the
5045 * DOOR_BELL afterward allows us to handle all the completed requests.
5046 * In order to prevent other interrupts starvation the DB is read once
5047 * after reset. The down side of this solution is the possibility of
5048 * false interrupt if device completes another request after resetting
5049 * aggregation and before reading the DB.
5051 if (ufshcd_is_intr_aggr_allowed(hba) &&
5052 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
5053 ufshcd_reset_intr_aggr(hba);
5055 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5056 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
5058 if (completed_reqs) {
5059 __ufshcd_transfer_req_compl(hba, completed_reqs);
5067 * ufshcd_disable_ee - disable exception event
5068 * @hba: per-adapter instance
5069 * @mask: exception event to disable
5071 * Disables exception event in the device so that the EVENT_ALERT
5074 * Returns zero on success, non-zero error value on failure.
5076 static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
5081 if (!(hba->ee_ctrl_mask & mask))
5084 val = hba->ee_ctrl_mask & ~mask;
5085 val &= MASK_EE_STATUS;
5086 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5087 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5089 hba->ee_ctrl_mask &= ~mask;
5095 * ufshcd_enable_ee - enable exception event
5096 * @hba: per-adapter instance
5097 * @mask: exception event to enable
5099 * Enable corresponding exception event in the device to allow
5100 * device to alert host in critical scenarios.
5102 * Returns zero on success, non-zero error value on failure.
5104 static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
5109 if (hba->ee_ctrl_mask & mask)
5112 val = hba->ee_ctrl_mask | mask;
5113 val &= MASK_EE_STATUS;
5114 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5115 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5117 hba->ee_ctrl_mask |= mask;
5123 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5124 * @hba: per-adapter instance
5126 * Allow device to manage background operations on its own. Enabling
5127 * this might lead to inconsistent latencies during normal data transfers
5128 * as the device is allowed to manage its own way of handling background
5131 * Returns zero on success, non-zero on failure.
5133 static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5137 if (hba->auto_bkops_enabled)
5140 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
5141 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
5143 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5148 hba->auto_bkops_enabled = true;
5149 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
5151 /* No need of URGENT_BKOPS exception from the device */
5152 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5154 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5161 * ufshcd_disable_auto_bkops - block device in doing background operations
5162 * @hba: per-adapter instance
5164 * Disabling background operations improves command response latency but
5165 * has drawback of device moving into critical state where the device is
5166 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5167 * host is idle so that BKOPS are managed effectively without any negative
5170 * Returns zero on success, non-zero on failure.
5172 static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5176 if (!hba->auto_bkops_enabled)
5180 * If host assisted BKOPs is to be enabled, make sure
5181 * urgent bkops exception is allowed.
5183 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5185 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5190 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
5191 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
5193 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5195 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5199 hba->auto_bkops_enabled = false;
5200 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
5201 hba->is_urgent_bkops_lvl_checked = false;
5207 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
5208 * @hba: per adapter instance
5210 * After a device reset the device may toggle the BKOPS_EN flag
5211 * to default value. The s/w tracking variables should be updated
5212 * as well. This function would change the auto-bkops state based on
5213 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
5215 static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
5217 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5218 hba->auto_bkops_enabled = false;
5219 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5220 ufshcd_enable_auto_bkops(hba);
5222 hba->auto_bkops_enabled = true;
5223 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5224 ufshcd_disable_auto_bkops(hba);
5226 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5227 hba->is_urgent_bkops_lvl_checked = false;
5230 static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5232 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5233 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5237 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5238 * @hba: per-adapter instance
5239 * @status: bkops_status value
5241 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5242 * flag in the device to permit background operations if the device
5243 * bkops_status is greater than or equal to "status" argument passed to
5244 * this function, disable otherwise.
5246 * Returns 0 for success, non-zero in case of failure.
5248 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5249 * to know whether auto bkops is enabled or disabled after this function
5250 * returns control to it.
5252 static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5253 enum bkops_status status)
5256 u32 curr_status = 0;
5258 err = ufshcd_get_bkops_status(hba, &curr_status);
5260 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5263 } else if (curr_status > BKOPS_STATUS_MAX) {
5264 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5265 __func__, curr_status);
5270 if (curr_status >= status)
5271 err = ufshcd_enable_auto_bkops(hba);
5273 err = ufshcd_disable_auto_bkops(hba);
5279 * ufshcd_urgent_bkops - handle urgent bkops exception event
5280 * @hba: per-adapter instance
5282 * Enable fBackgroundOpsEn flag in the device to permit background
5285 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5286 * and negative error value for any other failure.
5288 static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5290 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
5293 static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5295 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5296 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5299 static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5302 u32 curr_status = 0;
5304 if (hba->is_urgent_bkops_lvl_checked)
5305 goto enable_auto_bkops;
5307 err = ufshcd_get_bkops_status(hba, &curr_status);
5309 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5315 * We are seeing that some devices are raising the urgent bkops
5316 * exception events even when BKOPS status doesn't indicate performace
5317 * impacted or critical. Handle these device by determining their urgent
5318 * bkops status at runtime.
5320 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5321 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5322 __func__, curr_status);
5323 /* update the current status as the urgent bkops level */
5324 hba->urgent_bkops_lvl = curr_status;
5325 hba->is_urgent_bkops_lvl_checked = true;
5329 err = ufshcd_enable_auto_bkops(hba);
5332 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5336 static int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable)
5340 enum query_opcode opcode;
5342 if (!ufshcd_is_wb_allowed(hba))
5345 if (!(enable ^ hba->wb_enabled))
5348 opcode = UPIU_QUERY_OPCODE_SET_FLAG;
5350 opcode = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5352 index = ufshcd_wb_get_query_index(hba);
5353 ret = ufshcd_query_flag_retry(hba, opcode,
5354 QUERY_FLAG_IDN_WB_EN, index, NULL);
5356 dev_err(hba->dev, "%s write booster %s failed %d\n",
5357 __func__, enable ? "enable" : "disable", ret);
5361 hba->wb_enabled = enable;
5362 dev_dbg(hba->dev, "%s write booster %s %d\n",
5363 __func__, enable ? "enable" : "disable", ret);
5368 static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set)
5374 val = UPIU_QUERY_OPCODE_SET_FLAG;
5376 val = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5378 index = ufshcd_wb_get_query_index(hba);
5379 return ufshcd_query_flag_retry(hba, val,
5380 QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8,
5384 static inline void ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable)
5386 if (hba->quirks & UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL)
5390 ufshcd_wb_buf_flush_enable(hba);
5392 ufshcd_wb_buf_flush_disable(hba);
5396 static int ufshcd_wb_buf_flush_enable(struct ufs_hba *hba)
5401 if (!ufshcd_is_wb_allowed(hba) || hba->wb_buf_flush_enabled)
5404 index = ufshcd_wb_get_query_index(hba);
5405 ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
5406 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN,
5409 dev_err(hba->dev, "%s WB - buf flush enable failed %d\n",
5412 hba->wb_buf_flush_enabled = true;
5414 dev_dbg(hba->dev, "WB - Flush enabled: %d\n", ret);
5418 static int ufshcd_wb_buf_flush_disable(struct ufs_hba *hba)
5423 if (!ufshcd_is_wb_allowed(hba) || !hba->wb_buf_flush_enabled)
5426 index = ufshcd_wb_get_query_index(hba);
5427 ret = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
5428 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN,
5431 dev_warn(hba->dev, "%s: WB - buf flush disable failed %d\n",
5434 hba->wb_buf_flush_enabled = false;
5435 dev_dbg(hba->dev, "WB - Flush disabled: %d\n", ret);
5441 static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba,
5448 index = ufshcd_wb_get_query_index(hba);
5449 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5450 QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE,
5451 index, 0, &cur_buf);
5453 dev_err(hba->dev, "%s dCurWriteBoosterBufferSize read failed %d\n",
5459 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n",
5463 /* Let it continue to flush when available buffer exceeds threshold */
5464 if (avail_buf < hba->vps->wb_flush_threshold)
5470 static bool ufshcd_wb_need_flush(struct ufs_hba *hba)
5476 if (!ufshcd_is_wb_allowed(hba))
5479 * The ufs device needs the vcc to be ON to flush.
5480 * With user-space reduction enabled, it's enough to enable flush
5481 * by checking only the available buffer. The threshold
5482 * defined here is > 90% full.
5483 * With user-space preserved enabled, the current-buffer
5484 * should be checked too because the wb buffer size can reduce
5485 * when disk tends to be full. This info is provided by current
5486 * buffer (dCurrentWriteBoosterBufferSize). There's no point in
5487 * keeping vcc on when current buffer is empty.
5489 index = ufshcd_wb_get_query_index(hba);
5490 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5491 QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE,
5492 index, 0, &avail_buf);
5494 dev_warn(hba->dev, "%s dAvailableWriteBoosterBufferSize read failed %d\n",
5499 if (!hba->dev_info.b_presrv_uspc_en) {
5500 if (avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10))
5505 return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf);
5508 static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
5510 struct ufs_hba *hba = container_of(to_delayed_work(work),
5512 rpm_dev_flush_recheck_work);
5514 * To prevent unnecessary VCC power drain after device finishes
5515 * WriteBooster buffer flush or Auto BKOPs, force runtime resume
5516 * after a certain delay to recheck the threshold by next runtime
5519 pm_runtime_get_sync(hba->dev);
5520 pm_runtime_put_sync(hba->dev);
5524 * ufshcd_exception_event_handler - handle exceptions raised by device
5525 * @work: pointer to work data
5527 * Read bExceptionEventStatus attribute from the device and handle the
5528 * exception event accordingly.
5530 static void ufshcd_exception_event_handler(struct work_struct *work)
5532 struct ufs_hba *hba;
5535 hba = container_of(work, struct ufs_hba, eeh_work);
5537 pm_runtime_get_sync(hba->dev);
5538 ufshcd_scsi_block_requests(hba);
5539 err = ufshcd_get_ee_status(hba, &status);
5541 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5546 status &= hba->ee_ctrl_mask;
5548 if (status & MASK_EE_URGENT_BKOPS)
5549 ufshcd_bkops_exception_event_handler(hba);
5552 ufshcd_scsi_unblock_requests(hba);
5554 * pm_runtime_get_noresume is called while scheduling
5555 * eeh_work to avoid suspend racing with exception work.
5556 * Hence decrement usage counter using pm_runtime_put_noidle
5557 * to allow suspend on completion of exception event handler.
5559 pm_runtime_put_noidle(hba->dev);
5560 pm_runtime_put(hba->dev);
5564 /* Complete requests that have door-bell cleared */
5565 static void ufshcd_complete_requests(struct ufs_hba *hba)
5567 ufshcd_transfer_req_compl(hba);
5568 ufshcd_tmc_handler(hba);
5572 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5573 * to recover from the DL NAC errors or not.
5574 * @hba: per-adapter instance
5576 * Returns true if error handling is required, false otherwise
5578 static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5580 unsigned long flags;
5581 bool err_handling = true;
5583 spin_lock_irqsave(hba->host->host_lock, flags);
5585 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5586 * device fatal error and/or DL NAC & REPLAY timeout errors.
5588 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5591 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5592 ((hba->saved_err & UIC_ERROR) &&
5593 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
5596 if ((hba->saved_err & UIC_ERROR) &&
5597 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5600 * wait for 50ms to see if we can get any other errors or not.
5602 spin_unlock_irqrestore(hba->host->host_lock, flags);
5604 spin_lock_irqsave(hba->host->host_lock, flags);
5607 * now check if we have got any other severe errors other than
5610 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5611 ((hba->saved_err & UIC_ERROR) &&
5612 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5616 * As DL NAC is the only error received so far, send out NOP
5617 * command to confirm if link is still active or not.
5618 * - If we don't get any response then do error recovery.
5619 * - If we get response then clear the DL NAC error bit.
5622 spin_unlock_irqrestore(hba->host->host_lock, flags);
5623 err = ufshcd_verify_dev_init(hba);
5624 spin_lock_irqsave(hba->host->host_lock, flags);
5629 /* Link seems to be alive hence ignore the DL NAC errors */
5630 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5631 hba->saved_err &= ~UIC_ERROR;
5632 /* clear NAC error */
5633 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5634 if (!hba->saved_uic_err)
5635 err_handling = false;
5638 spin_unlock_irqrestore(hba->host->host_lock, flags);
5639 return err_handling;
5642 /* host lock must be held before calling this func */
5643 static inline bool ufshcd_is_saved_err_fatal(struct ufs_hba *hba)
5645 return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) ||
5646 (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK));
5649 /* host lock must be held before calling this func */
5650 static inline void ufshcd_schedule_eh_work(struct ufs_hba *hba)
5652 /* handle fatal errors only when link is not in error state */
5653 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) {
5654 if (hba->force_reset || ufshcd_is_link_broken(hba) ||
5655 ufshcd_is_saved_err_fatal(hba))
5656 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL;
5658 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL;
5659 queue_work(hba->eh_wq, &hba->eh_work);
5663 static void ufshcd_err_handling_prepare(struct ufs_hba *hba)
5665 pm_runtime_get_sync(hba->dev);
5666 if (pm_runtime_status_suspended(hba->dev) || hba->is_sys_suspended) {
5667 enum ufs_pm_op pm_op;
5670 * Don't assume anything of pm_runtime_get_sync(), if
5671 * resume fails, irq and clocks can be OFF, and powers
5672 * can be OFF or in LPM.
5674 ufshcd_setup_hba_vreg(hba, true);
5675 ufshcd_enable_irq(hba);
5676 ufshcd_setup_vreg(hba, true);
5677 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5678 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5679 ufshcd_hold(hba, false);
5680 if (!ufshcd_is_clkgating_allowed(hba))
5681 ufshcd_setup_clocks(hba, true);
5682 ufshcd_release(hba);
5683 pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM;
5684 ufshcd_vops_resume(hba, pm_op);
5686 ufshcd_hold(hba, false);
5687 if (hba->clk_scaling.is_allowed) {
5688 cancel_work_sync(&hba->clk_scaling.suspend_work);
5689 cancel_work_sync(&hba->clk_scaling.resume_work);
5690 ufshcd_suspend_clkscaling(hba);
5695 static void ufshcd_err_handling_unprepare(struct ufs_hba *hba)
5697 ufshcd_release(hba);
5698 if (hba->clk_scaling.is_allowed)
5699 ufshcd_resume_clkscaling(hba);
5700 pm_runtime_put(hba->dev);
5703 static inline bool ufshcd_err_handling_should_stop(struct ufs_hba *hba)
5705 return (!hba->is_powered || hba->ufshcd_state == UFSHCD_STATE_ERROR ||
5706 (!(hba->saved_err || hba->saved_uic_err || hba->force_reset ||
5707 ufshcd_is_link_broken(hba))));
5711 static void ufshcd_recover_pm_error(struct ufs_hba *hba)
5713 struct Scsi_Host *shost = hba->host;
5714 struct scsi_device *sdev;
5715 struct request_queue *q;
5718 hba->is_sys_suspended = false;
5720 * Set RPM status of hba device to RPM_ACTIVE,
5721 * this also clears its runtime error.
5723 ret = pm_runtime_set_active(hba->dev);
5725 * If hba device had runtime error, we also need to resume those
5726 * scsi devices under hba in case any of them has failed to be
5727 * resumed due to hba runtime resume failure. This is to unblock
5728 * blk_queue_enter in case there are bios waiting inside it.
5731 shost_for_each_device(sdev, shost) {
5732 q = sdev->request_queue;
5733 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
5734 q->rpm_status == RPM_SUSPENDING))
5735 pm_request_resume(q->dev);
5740 static inline void ufshcd_recover_pm_error(struct ufs_hba *hba)
5745 static bool ufshcd_is_pwr_mode_restore_needed(struct ufs_hba *hba)
5747 struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info;
5750 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode);
5752 if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK))
5755 if (pwr_info->pwr_tx != (mode & PWRMODE_MASK))
5762 * ufshcd_err_handler - handle UFS errors that require s/w attention
5763 * @work: pointer to work structure
5765 static void ufshcd_err_handler(struct work_struct *work)
5767 struct ufs_hba *hba;
5768 unsigned long flags;
5769 bool err_xfer = false;
5770 bool err_tm = false;
5771 int err = 0, pmc_err;
5773 bool needs_reset = false, needs_restore = false;
5775 hba = container_of(work, struct ufs_hba, eh_work);
5778 spin_lock_irqsave(hba->host->host_lock, flags);
5779 if (ufshcd_err_handling_should_stop(hba)) {
5780 if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
5781 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5782 spin_unlock_irqrestore(hba->host->host_lock, flags);
5786 ufshcd_set_eh_in_progress(hba);
5787 spin_unlock_irqrestore(hba->host->host_lock, flags);
5788 ufshcd_err_handling_prepare(hba);
5789 spin_lock_irqsave(hba->host->host_lock, flags);
5790 ufshcd_scsi_block_requests(hba);
5791 hba->ufshcd_state = UFSHCD_STATE_RESET;
5793 /* Complete requests that have door-bell cleared by h/w */
5794 ufshcd_complete_requests(hba);
5797 * A full reset and restore might have happened after preparation
5798 * is finished, double check whether we should stop.
5800 if (ufshcd_err_handling_should_stop(hba))
5801 goto skip_err_handling;
5803 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5806 spin_unlock_irqrestore(hba->host->host_lock, flags);
5807 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5808 ret = ufshcd_quirk_dl_nac_errors(hba);
5809 spin_lock_irqsave(hba->host->host_lock, flags);
5810 if (!ret && ufshcd_err_handling_should_stop(hba))
5811 goto skip_err_handling;
5814 if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
5815 (hba->saved_uic_err &&
5816 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
5817 bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR);
5819 spin_unlock_irqrestore(hba->host->host_lock, flags);
5820 ufshcd_print_host_state(hba);
5821 ufshcd_print_pwr_info(hba);
5822 ufshcd_print_evt_hist(hba);
5823 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5824 ufshcd_print_trs(hba, hba->outstanding_reqs, pr_prdt);
5825 spin_lock_irqsave(hba->host->host_lock, flags);
5829 * if host reset is required then skip clearing the pending
5830 * transfers forcefully because they will get cleared during
5831 * host reset and restore
5833 if (hba->force_reset || ufshcd_is_link_broken(hba) ||
5834 ufshcd_is_saved_err_fatal(hba) ||
5835 ((hba->saved_err & UIC_ERROR) &&
5836 (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5837 UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) {
5843 * If LINERESET was caught, UFS might have been put to PWM mode,
5844 * check if power mode restore is needed.
5846 if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) {
5847 hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
5848 if (!hba->saved_uic_err)
5849 hba->saved_err &= ~UIC_ERROR;
5850 spin_unlock_irqrestore(hba->host->host_lock, flags);
5851 if (ufshcd_is_pwr_mode_restore_needed(hba))
5852 needs_restore = true;
5853 spin_lock_irqsave(hba->host->host_lock, flags);
5854 if (!hba->saved_err && !needs_restore)
5855 goto skip_err_handling;
5858 hba->silence_err_logs = true;
5859 /* release lock as clear command might sleep */
5860 spin_unlock_irqrestore(hba->host->host_lock, flags);
5861 /* Clear pending transfer requests */
5862 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5863 if (ufshcd_try_to_abort_task(hba, tag)) {
5865 goto lock_skip_pending_xfer_clear;
5869 /* Clear pending task management requests */
5870 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5871 if (ufshcd_clear_tm_cmd(hba, tag)) {
5873 goto lock_skip_pending_xfer_clear;
5877 lock_skip_pending_xfer_clear:
5878 spin_lock_irqsave(hba->host->host_lock, flags);
5880 /* Complete the requests that are cleared by s/w */
5881 ufshcd_complete_requests(hba);
5882 hba->silence_err_logs = false;
5884 if (err_xfer || err_tm) {
5890 * After all reqs and tasks are cleared from doorbell,
5891 * now it is safe to retore power mode.
5893 if (needs_restore) {
5894 spin_unlock_irqrestore(hba->host->host_lock, flags);
5896 * Hold the scaling lock just in case dev cmds
5897 * are sent via bsg and/or sysfs.
5899 down_write(&hba->clk_scaling_lock);
5900 hba->force_pmc = true;
5901 pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info));
5904 dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n",
5907 hba->force_pmc = false;
5908 ufshcd_print_pwr_info(hba);
5909 up_write(&hba->clk_scaling_lock);
5910 spin_lock_irqsave(hba->host->host_lock, flags);
5914 /* Fatal errors need reset */
5916 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5919 * ufshcd_reset_and_restore() does the link reinitialization
5920 * which will need atleast one empty doorbell slot to send the
5921 * device management commands (NOP and query commands).
5922 * If there is no slot empty at this moment then free up last
5925 if (hba->outstanding_reqs == max_doorbells)
5926 __ufshcd_transfer_req_compl(hba,
5927 (1UL << (hba->nutrs - 1)));
5929 hba->force_reset = false;
5930 spin_unlock_irqrestore(hba->host->host_lock, flags);
5931 err = ufshcd_reset_and_restore(hba);
5933 dev_err(hba->dev, "%s: reset and restore failed with err %d\n",
5936 ufshcd_recover_pm_error(hba);
5937 spin_lock_irqsave(hba->host->host_lock, flags);
5942 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
5943 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5944 if (hba->saved_err || hba->saved_uic_err)
5945 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5946 __func__, hba->saved_err, hba->saved_uic_err);
5948 ufshcd_clear_eh_in_progress(hba);
5949 spin_unlock_irqrestore(hba->host->host_lock, flags);
5950 ufshcd_scsi_unblock_requests(hba);
5951 ufshcd_err_handling_unprepare(hba);
5956 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5957 * @hba: per-adapter instance
5960 * IRQ_HANDLED - If interrupt is valid
5961 * IRQ_NONE - If invalid interrupt
5963 static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
5966 irqreturn_t retval = IRQ_NONE;
5968 /* PHY layer error */
5969 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5970 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
5971 (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) {
5972 ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg);
5974 * To know whether this error is fatal or not, DB timeout
5975 * must be checked but this error is handled separately.
5977 if (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)
5978 dev_dbg(hba->dev, "%s: UIC Lane error reported\n",
5981 /* Got a LINERESET indication. */
5982 if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) {
5983 struct uic_command *cmd = NULL;
5985 hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR;
5986 if (hba->uic_async_done && hba->active_uic_cmd)
5987 cmd = hba->active_uic_cmd;
5989 * Ignore the LINERESET during power mode change
5990 * operation via DME_SET command.
5992 if (cmd && (cmd->command == UIC_CMD_DME_SET))
5993 hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
5995 retval |= IRQ_HANDLED;
5998 /* PA_INIT_ERROR is fatal and needs UIC reset */
5999 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
6000 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
6001 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
6002 ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg);
6004 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
6005 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
6006 else if (hba->dev_quirks &
6007 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
6008 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
6010 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
6011 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
6012 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
6014 retval |= IRQ_HANDLED;
6017 /* UIC NL/TL/DME errors needs software retry */
6018 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
6019 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
6020 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
6021 ufshcd_update_evt_hist(hba, UFS_EVT_NL_ERR, reg);
6022 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
6023 retval |= IRQ_HANDLED;
6026 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
6027 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
6028 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
6029 ufshcd_update_evt_hist(hba, UFS_EVT_TL_ERR, reg);
6030 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
6031 retval |= IRQ_HANDLED;
6034 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
6035 if ((reg & UIC_DME_ERROR) &&
6036 (reg & UIC_DME_ERROR_CODE_MASK)) {
6037 ufshcd_update_evt_hist(hba, UFS_EVT_DME_ERR, reg);
6038 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
6039 retval |= IRQ_HANDLED;
6042 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
6043 __func__, hba->uic_error);
6047 static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
6050 if (!ufshcd_is_auto_hibern8_supported(hba) ||
6051 !ufshcd_is_auto_hibern8_enabled(hba))
6054 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
6057 if (hba->active_uic_cmd &&
6058 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
6059 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
6066 * ufshcd_check_errors - Check for errors that need s/w attention
6067 * @hba: per-adapter instance
6070 * IRQ_HANDLED - If interrupt is valid
6071 * IRQ_NONE - If invalid interrupt
6073 static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba)
6075 bool queue_eh_work = false;
6076 irqreturn_t retval = IRQ_NONE;
6078 if (hba->errors & INT_FATAL_ERRORS) {
6079 ufshcd_update_evt_hist(hba, UFS_EVT_FATAL_ERR,
6081 queue_eh_work = true;
6084 if (hba->errors & UIC_ERROR) {
6086 retval = ufshcd_update_uic_error(hba);
6088 queue_eh_work = true;
6091 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
6093 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
6094 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
6096 hba->errors, ufshcd_get_upmcrs(hba));
6097 ufshcd_update_evt_hist(hba, UFS_EVT_AUTO_HIBERN8_ERR,
6099 ufshcd_set_link_broken(hba);
6100 queue_eh_work = true;
6103 if (queue_eh_work) {
6105 * update the transfer error masks to sticky bits, let's do this
6106 * irrespective of current ufshcd_state.
6108 hba->saved_err |= hba->errors;
6109 hba->saved_uic_err |= hba->uic_error;
6111 /* dump controller state before resetting */
6112 if ((hba->saved_err &
6113 (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
6114 (hba->saved_uic_err &&
6115 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
6116 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
6117 __func__, hba->saved_err,
6118 hba->saved_uic_err);
6119 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE,
6121 ufshcd_print_pwr_info(hba);
6123 ufshcd_schedule_eh_work(hba);
6124 retval |= IRQ_HANDLED;
6127 * if (!queue_eh_work) -
6128 * Other errors are either non-fatal where host recovers
6129 * itself without s/w intervention or errors that will be
6130 * handled by the SCSI core layer.
6136 struct ufs_hba *hba;
6137 unsigned long pending;
6141 static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved)
6143 struct ctm_info *const ci = priv;
6144 struct completion *c;
6146 WARN_ON_ONCE(reserved);
6147 if (test_bit(req->tag, &ci->pending))
6150 c = req->end_io_data;
6157 * ufshcd_tmc_handler - handle task management function completion
6158 * @hba: per adapter instance
6161 * IRQ_HANDLED - If interrupt is valid
6162 * IRQ_NONE - If invalid interrupt
6164 static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
6166 struct request_queue *q = hba->tmf_queue;
6167 struct ctm_info ci = {
6169 .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL),
6172 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci);
6173 return ci.ncpl ? IRQ_HANDLED : IRQ_NONE;
6177 * ufshcd_sl_intr - Interrupt service routine
6178 * @hba: per adapter instance
6179 * @intr_status: contains interrupts generated by the controller
6182 * IRQ_HANDLED - If interrupt is valid
6183 * IRQ_NONE - If invalid interrupt
6185 static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
6187 irqreturn_t retval = IRQ_NONE;
6189 hba->errors = UFSHCD_ERROR_MASK & intr_status;
6191 if (ufshcd_is_auto_hibern8_error(hba, intr_status))
6192 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
6195 retval |= ufshcd_check_errors(hba);
6197 if (intr_status & UFSHCD_UIC_MASK)
6198 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
6200 if (intr_status & UTP_TASK_REQ_COMPL)
6201 retval |= ufshcd_tmc_handler(hba);
6203 if (intr_status & UTP_TRANSFER_REQ_COMPL)
6204 retval |= ufshcd_transfer_req_compl(hba);
6210 * ufshcd_intr - Main interrupt service routine
6212 * @__hba: pointer to adapter instance
6215 * IRQ_HANDLED - If interrupt is valid
6216 * IRQ_NONE - If invalid interrupt
6218 static irqreturn_t ufshcd_intr(int irq, void *__hba)
6220 u32 intr_status, enabled_intr_status = 0;
6221 irqreturn_t retval = IRQ_NONE;
6222 struct ufs_hba *hba = __hba;
6223 int retries = hba->nutrs;
6225 spin_lock(hba->host->host_lock);
6226 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
6227 hba->ufs_stats.last_intr_status = intr_status;
6228 hba->ufs_stats.last_intr_ts = ktime_get();
6231 * There could be max of hba->nutrs reqs in flight and in worst case
6232 * if the reqs get finished 1 by 1 after the interrupt status is
6233 * read, make sure we handle them by checking the interrupt status
6234 * again in a loop until we process all of the reqs before returning.
6236 while (intr_status && retries--) {
6237 enabled_intr_status =
6238 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
6240 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
6241 if (enabled_intr_status)
6242 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
6244 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
6247 if (enabled_intr_status && retval == IRQ_NONE) {
6248 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n",
6249 __func__, intr_status);
6250 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
6253 spin_unlock(hba->host->host_lock);
6257 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
6260 u32 mask = 1 << tag;
6261 unsigned long flags;
6263 if (!test_bit(tag, &hba->outstanding_tasks))
6266 spin_lock_irqsave(hba->host->host_lock, flags);
6267 ufshcd_utmrl_clear(hba, tag);
6268 spin_unlock_irqrestore(hba->host->host_lock, flags);
6270 /* poll for max. 1 sec to clear door bell register by h/w */
6271 err = ufshcd_wait_for_register(hba,
6272 REG_UTP_TASK_REQ_DOOR_BELL,
6273 mask, 0, 1000, 1000);
6278 static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
6279 struct utp_task_req_desc *treq, u8 tm_function)
6281 struct request_queue *q = hba->tmf_queue;
6282 struct Scsi_Host *host = hba->host;
6283 DECLARE_COMPLETION_ONSTACK(wait);
6284 struct request *req;
6285 unsigned long flags;
6286 int free_slot, task_tag, err;
6289 * Get free slot, sleep if slots are unavailable.
6290 * Even though we use wait_event() which sleeps indefinitely,
6291 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
6293 req = blk_get_request(q, REQ_OP_DRV_OUT, BLK_MQ_REQ_RESERVED);
6294 req->end_io_data = &wait;
6295 free_slot = req->tag;
6296 WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs);
6297 ufshcd_hold(hba, false);
6299 spin_lock_irqsave(host->host_lock, flags);
6300 task_tag = hba->nutrs + free_slot;
6302 treq->req_header.dword_0 |= cpu_to_be32(task_tag);
6304 memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq));
6305 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
6307 /* send command to the controller */
6308 __set_bit(free_slot, &hba->outstanding_tasks);
6310 /* Make sure descriptors are ready before ringing the task doorbell */
6313 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
6314 /* Make sure that doorbell is committed immediately */
6317 spin_unlock_irqrestore(host->host_lock, flags);
6319 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_send");
6321 /* wait until the task management command is completed */
6322 err = wait_for_completion_io_timeout(&wait,
6323 msecs_to_jiffies(TM_CMD_TIMEOUT));
6326 * Make sure that ufshcd_compl_tm() does not trigger a
6329 req->end_io_data = NULL;
6330 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete_err");
6331 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6332 __func__, tm_function);
6333 if (ufshcd_clear_tm_cmd(hba, free_slot))
6334 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
6335 __func__, free_slot);
6339 memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq));
6341 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete");
6344 spin_lock_irqsave(hba->host->host_lock, flags);
6345 __clear_bit(free_slot, &hba->outstanding_tasks);
6346 spin_unlock_irqrestore(hba->host->host_lock, flags);
6348 blk_put_request(req);
6350 ufshcd_release(hba);
6355 * ufshcd_issue_tm_cmd - issues task management commands to controller
6356 * @hba: per adapter instance
6357 * @lun_id: LUN ID to which TM command is sent
6358 * @task_id: task ID to which the TM command is applicable
6359 * @tm_function: task management function opcode
6360 * @tm_response: task management service response return value
6362 * Returns non-zero value on error, zero on success.
6364 static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
6365 u8 tm_function, u8 *tm_response)
6367 struct utp_task_req_desc treq = { { 0 }, };
6370 /* Configure task request descriptor */
6371 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6372 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6374 /* Configure task request UPIU */
6375 treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) |
6376 cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24);
6377 treq.req_header.dword_1 = cpu_to_be32(tm_function << 16);
6380 * The host shall provide the same value for LUN field in the basic
6381 * header and for Input Parameter.
6383 treq.input_param1 = cpu_to_be32(lun_id);
6384 treq.input_param2 = cpu_to_be32(task_id);
6386 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
6387 if (err == -ETIMEDOUT)
6390 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6391 if (ocs_value != OCS_SUCCESS)
6392 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
6393 __func__, ocs_value);
6394 else if (tm_response)
6395 *tm_response = be32_to_cpu(treq.output_param1) &
6396 MASK_TM_SERVICE_RESP;
6401 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
6402 * @hba: per-adapter instance
6403 * @req_upiu: upiu request
6404 * @rsp_upiu: upiu reply
6405 * @desc_buff: pointer to descriptor buffer, NULL if NA
6406 * @buff_len: descriptor size, 0 if NA
6407 * @cmd_type: specifies the type (NOP, Query...)
6408 * @desc_op: descriptor operation
6410 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
6411 * Therefore, it "rides" the device management infrastructure: uses its tag and
6412 * tasks work queues.
6414 * Since there is only one available tag for device management commands,
6415 * the caller is expected to hold the hba->dev_cmd.lock mutex.
6417 static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
6418 struct utp_upiu_req *req_upiu,
6419 struct utp_upiu_req *rsp_upiu,
6420 u8 *desc_buff, int *buff_len,
6421 enum dev_cmd_type cmd_type,
6422 enum query_opcode desc_op)
6424 struct request_queue *q = hba->cmd_queue;
6425 struct request *req;
6426 struct ufshcd_lrb *lrbp;
6429 struct completion wait;
6430 unsigned long flags;
6433 down_read(&hba->clk_scaling_lock);
6435 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
6441 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
6443 init_completion(&wait);
6444 lrbp = &hba->lrb[tag];
6445 if (unlikely(lrbp->in_use)) {
6452 lrbp->sense_bufflen = 0;
6453 lrbp->sense_buffer = NULL;
6454 lrbp->task_tag = tag;
6456 lrbp->intr_cmd = true;
6457 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
6458 hba->dev_cmd.type = cmd_type;
6460 switch (hba->ufs_version) {
6461 case UFSHCI_VERSION_10:
6462 case UFSHCI_VERSION_11:
6463 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
6466 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
6470 /* update the task tag in the request upiu */
6471 req_upiu->header.dword_0 |= cpu_to_be32(tag);
6473 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
6475 /* just copy the upiu request as it is */
6476 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
6477 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
6478 /* The Data Segment Area is optional depending upon the query
6479 * function value. for WRITE DESCRIPTOR, the data segment
6480 * follows right after the tsf.
6482 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
6486 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
6488 hba->dev_cmd.complete = &wait;
6490 /* Make sure descriptors are ready before ringing the doorbell */
6492 spin_lock_irqsave(hba->host->host_lock, flags);
6493 ufshcd_send_command(hba, tag);
6494 spin_unlock_irqrestore(hba->host->host_lock, flags);
6497 * ignore the returning value here - ufshcd_check_query_response is
6498 * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
6499 * read the response directly ignoring all errors.
6501 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT);
6503 /* just copy the upiu response as it is */
6504 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
6505 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
6506 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
6507 u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
6508 MASK_QUERY_DATA_SEG_LEN;
6510 if (*buff_len >= resp_len) {
6511 memcpy(desc_buff, descp, resp_len);
6512 *buff_len = resp_len;
6515 "%s: rsp size %d is bigger than buffer size %d",
6516 __func__, resp_len, *buff_len);
6523 blk_put_request(req);
6525 up_read(&hba->clk_scaling_lock);
6530 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
6531 * @hba: per-adapter instance
6532 * @req_upiu: upiu request
6533 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
6534 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
6535 * @desc_buff: pointer to descriptor buffer, NULL if NA
6536 * @buff_len: descriptor size, 0 if NA
6537 * @desc_op: descriptor operation
6539 * Supports UTP Transfer requests (nop and query), and UTP Task
6540 * Management requests.
6541 * It is up to the caller to fill the upiu conent properly, as it will
6542 * be copied without any further input validations.
6544 int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
6545 struct utp_upiu_req *req_upiu,
6546 struct utp_upiu_req *rsp_upiu,
6548 u8 *desc_buff, int *buff_len,
6549 enum query_opcode desc_op)
6552 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
6553 struct utp_task_req_desc treq = { { 0 }, };
6555 u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC;
6558 case UPIU_TRANSACTION_NOP_OUT:
6559 cmd_type = DEV_CMD_TYPE_NOP;
6561 case UPIU_TRANSACTION_QUERY_REQ:
6562 ufshcd_hold(hba, false);
6563 mutex_lock(&hba->dev_cmd.lock);
6564 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
6565 desc_buff, buff_len,
6567 mutex_unlock(&hba->dev_cmd.lock);
6568 ufshcd_release(hba);
6571 case UPIU_TRANSACTION_TASK_REQ:
6572 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6573 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6575 memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu));
6577 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
6578 if (err == -ETIMEDOUT)
6581 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6582 if (ocs_value != OCS_SUCCESS) {
6583 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
6588 memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu));
6601 * ufshcd_eh_device_reset_handler - device reset handler registered to
6603 * @cmd: SCSI command pointer
6605 * Returns SUCCESS/FAILED
6607 static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
6609 struct Scsi_Host *host;
6610 struct ufs_hba *hba;
6615 struct ufshcd_lrb *lrbp;
6616 unsigned long flags;
6618 host = cmd->device->host;
6619 hba = shost_priv(host);
6620 tag = cmd->request->tag;
6622 lrbp = &hba->lrb[tag];
6623 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
6624 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6630 /* clear the commands that were pending for corresponding LUN */
6631 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6632 if (hba->lrb[pos].lun == lrbp->lun) {
6633 err = ufshcd_clear_cmd(hba, pos);
6638 spin_lock_irqsave(host->host_lock, flags);
6639 ufshcd_transfer_req_compl(hba);
6640 spin_unlock_irqrestore(host->host_lock, flags);
6643 hba->req_abort_count = 0;
6644 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, (u32)err);
6648 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6654 static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6656 struct ufshcd_lrb *lrbp;
6659 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6660 lrbp = &hba->lrb[tag];
6661 lrbp->req_abort_skip = true;
6666 * ufshcd_try_to_abort_task - abort a specific task
6667 * @hba: Pointer to adapter instance
6668 * @tag: Task tag/index to be aborted
6670 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6671 * command, and in host controller by clearing the door-bell register. There can
6672 * be race between controller sending the command to the device while abort is
6673 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6674 * really issued and then try to abort it.
6676 * Returns zero on success, non-zero on failure
6678 static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag)
6680 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
6686 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6687 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6688 UFS_QUERY_TASK, &resp);
6689 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6690 /* cmd pending in the device */
6691 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
6694 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6696 * cmd not pending in the device, check if it is
6699 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
6701 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6702 if (reg & (1 << tag)) {
6703 /* sleep for max. 200us to stabilize */
6704 usleep_range(100, 200);
6707 /* command completed already */
6708 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
6713 "%s: no response from device. tag = %d, err %d\n",
6714 __func__, tag, err);
6716 err = resp; /* service response error */
6726 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6727 UFS_ABORT_TASK, &resp);
6728 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6730 err = resp; /* service response error */
6731 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
6732 __func__, tag, err);
6737 err = ufshcd_clear_cmd(hba, tag);
6739 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
6740 __func__, tag, err);
6747 * ufshcd_abort - scsi host template eh_abort_handler callback
6748 * @cmd: SCSI command pointer
6750 * Returns SUCCESS/FAILED
6752 static int ufshcd_abort(struct scsi_cmnd *cmd)
6754 struct Scsi_Host *host;
6755 struct ufs_hba *hba;
6756 unsigned long flags;
6759 struct ufshcd_lrb *lrbp;
6762 host = cmd->device->host;
6763 hba = shost_priv(host);
6764 tag = cmd->request->tag;
6765 lrbp = &hba->lrb[tag];
6766 if (!ufshcd_valid_tag(hba, tag)) {
6768 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6769 __func__, tag, cmd, cmd->request);
6773 ufshcd_hold(hba, false);
6774 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6775 /* If command is already aborted/completed, return SUCCESS */
6776 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6778 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6779 __func__, tag, hba->outstanding_reqs, reg);
6783 /* Print Transfer Request of aborted task */
6784 dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
6787 * Print detailed info about aborted request.
6788 * As more than one request might get aborted at the same time,
6789 * print full information only for the first aborted request in order
6790 * to reduce repeated printouts. For other aborted requests only print
6793 scsi_print_command(cmd);
6794 if (!hba->req_abort_count) {
6795 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag);
6796 ufshcd_print_evt_hist(hba);
6797 ufshcd_print_host_state(hba);
6798 ufshcd_print_pwr_info(hba);
6799 ufshcd_print_trs(hba, 1 << tag, true);
6801 ufshcd_print_trs(hba, 1 << tag, false);
6803 hba->req_abort_count++;
6805 if (!(reg & (1 << tag))) {
6807 "%s: cmd was completed, but without a notifying intr, tag = %d",
6813 * Task abort to the device W-LUN is illegal. When this command
6814 * will fail, due to spec violation, scsi err handling next step
6815 * will be to send LU reset which, again, is a spec violation.
6816 * To avoid these unnecessary/illegal steps, first we clean up
6817 * the lrb taken by this cmd and mark the lrb as in_use, then
6818 * queue the eh_work and bail.
6820 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) {
6821 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun);
6822 spin_lock_irqsave(host->host_lock, flags);
6824 __ufshcd_transfer_req_compl(hba, (1UL << tag));
6825 __set_bit(tag, &hba->outstanding_reqs);
6826 lrbp->in_use = true;
6827 hba->force_reset = true;
6828 ufshcd_schedule_eh_work(hba);
6831 spin_unlock_irqrestore(host->host_lock, flags);
6835 /* Skip task abort in case previous aborts failed and report failure */
6836 if (lrbp->req_abort_skip)
6839 err = ufshcd_try_to_abort_task(hba, tag);
6843 spin_lock_irqsave(host->host_lock, flags);
6844 __ufshcd_transfer_req_compl(hba, (1UL << tag));
6845 spin_unlock_irqrestore(host->host_lock, flags);
6849 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6850 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
6855 * This ufshcd_release() corresponds to the original scsi cmd that got
6856 * aborted here (as we won't get any IRQ for it).
6858 ufshcd_release(hba);
6863 * ufshcd_host_reset_and_restore - reset and restore host controller
6864 * @hba: per-adapter instance
6866 * Note that host controller reset may issue DME_RESET to
6867 * local and remote (device) Uni-Pro stack and the attributes
6868 * are reset to default state.
6870 * Returns zero on success, non-zero on failure
6872 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6875 unsigned long flags;
6878 * Stop the host controller and complete the requests
6881 ufshcd_hba_stop(hba);
6883 spin_lock_irqsave(hba->host->host_lock, flags);
6884 hba->silence_err_logs = true;
6885 ufshcd_complete_requests(hba);
6886 hba->silence_err_logs = false;
6887 spin_unlock_irqrestore(hba->host->host_lock, flags);
6889 /* scale up clocks to max frequency before full reinitialization */
6890 ufshcd_set_clk_freq(hba, true);
6892 err = ufshcd_hba_enable(hba);
6896 /* Establish the link again and restore the device */
6897 err = ufshcd_probe_hba(hba, false);
6901 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
6902 ufshcd_update_evt_hist(hba, UFS_EVT_HOST_RESET, (u32)err);
6907 * ufshcd_reset_and_restore - reset and re-initialize host/device
6908 * @hba: per-adapter instance
6910 * Reset and recover device, host and re-establish link. This
6911 * is helpful to recover the communication in fatal error conditions.
6913 * Returns zero on success, non-zero on failure
6915 static int ufshcd_reset_and_restore(struct ufs_hba *hba)
6920 unsigned long flags;
6921 int retries = MAX_HOST_RESET_RETRIES;
6924 * This is a fresh start, cache and clear saved error first,
6925 * in case new error generated during reset and restore.
6927 spin_lock_irqsave(hba->host->host_lock, flags);
6928 saved_err = hba->saved_err;
6929 saved_uic_err = hba->saved_uic_err;
6931 hba->saved_uic_err = 0;
6932 spin_unlock_irqrestore(hba->host->host_lock, flags);
6935 /* Reset the attached device */
6936 ufshcd_vops_device_reset(hba);
6938 err = ufshcd_host_reset_and_restore(hba);
6939 } while (err && --retries);
6941 spin_lock_irqsave(hba->host->host_lock, flags);
6943 * Inform scsi mid-layer that we did reset and allow to handle
6944 * Unit Attention properly.
6946 scsi_report_bus_reset(hba->host, 0);
6948 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6949 hba->saved_err |= saved_err;
6950 hba->saved_uic_err |= saved_uic_err;
6952 spin_unlock_irqrestore(hba->host->host_lock, flags);
6958 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
6959 * @cmd: SCSI command pointer
6961 * Returns SUCCESS/FAILED
6963 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
6966 unsigned long flags;
6967 struct ufs_hba *hba;
6969 hba = shost_priv(cmd->device->host);
6971 spin_lock_irqsave(hba->host->host_lock, flags);
6972 hba->force_reset = true;
6973 ufshcd_schedule_eh_work(hba);
6974 dev_err(hba->dev, "%s: reset in progress - 1\n", __func__);
6975 spin_unlock_irqrestore(hba->host->host_lock, flags);
6977 flush_work(&hba->eh_work);
6979 spin_lock_irqsave(hba->host->host_lock, flags);
6980 if (hba->ufshcd_state == UFSHCD_STATE_ERROR)
6982 spin_unlock_irqrestore(hba->host->host_lock, flags);
6988 * ufshcd_get_max_icc_level - calculate the ICC level
6989 * @sup_curr_uA: max. current supported by the regulator
6990 * @start_scan: row at the desc table to start scan from
6991 * @buff: power descriptor buffer
6993 * Returns calculated max ICC level for specific regulator
6995 static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
7002 for (i = start_scan; i >= 0; i--) {
7003 data = be16_to_cpup((__be16 *)&buff[2 * i]);
7004 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
7005 ATTR_ICC_LVL_UNIT_OFFSET;
7006 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
7008 case UFSHCD_NANO_AMP:
7009 curr_uA = curr_uA / 1000;
7011 case UFSHCD_MILI_AMP:
7012 curr_uA = curr_uA * 1000;
7015 curr_uA = curr_uA * 1000 * 1000;
7017 case UFSHCD_MICRO_AMP:
7021 if (sup_curr_uA >= curr_uA)
7026 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
7033 * ufshcd_calc_icc_level - calculate the max ICC level
7034 * In case regulators are not initialized we'll return 0
7035 * @hba: per-adapter instance
7036 * @desc_buf: power descriptor buffer to extract ICC levels from.
7037 * @len: length of desc_buff
7039 * Returns calculated ICC level
7041 static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
7042 u8 *desc_buf, int len)
7046 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
7047 !hba->vreg_info.vccq2) {
7049 "%s: Regulator capability was not set, actvIccLevel=%d",
7050 __func__, icc_level);
7054 if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA)
7055 icc_level = ufshcd_get_max_icc_level(
7056 hba->vreg_info.vcc->max_uA,
7057 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
7058 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
7060 if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA)
7061 icc_level = ufshcd_get_max_icc_level(
7062 hba->vreg_info.vccq->max_uA,
7064 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
7066 if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA)
7067 icc_level = ufshcd_get_max_icc_level(
7068 hba->vreg_info.vccq2->max_uA,
7070 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
7075 static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
7078 int buff_len = hba->desc_size[QUERY_DESC_IDN_POWER];
7082 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7086 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0,
7087 desc_buf, buff_len);
7090 "%s: Failed reading power descriptor.len = %d ret = %d",
7091 __func__, buff_len, ret);
7095 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf,
7097 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
7099 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7100 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
7104 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
7105 __func__, icc_level, ret);
7111 static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
7113 scsi_autopm_get_device(sdev);
7114 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev);
7115 if (sdev->rpm_autosuspend)
7116 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev,
7117 RPM_AUTOSUSPEND_DELAY_MS);
7118 scsi_autopm_put_device(sdev);
7122 * ufshcd_scsi_add_wlus - Adds required W-LUs
7123 * @hba: per-adapter instance
7125 * UFS device specification requires the UFS devices to support 4 well known
7127 * "REPORT_LUNS" (address: 01h)
7128 * "UFS Device" (address: 50h)
7129 * "RPMB" (address: 44h)
7130 * "BOOT" (address: 30h)
7131 * UFS device's power management needs to be controlled by "POWER CONDITION"
7132 * field of SSU (START STOP UNIT) command. But this "power condition" field
7133 * will take effect only when its sent to "UFS device" well known logical unit
7134 * hence we require the scsi_device instance to represent this logical unit in
7135 * order for the UFS host driver to send the SSU command for power management.
7137 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
7138 * Block) LU so user space process can control this LU. User space may also
7139 * want to have access to BOOT LU.
7141 * This function adds scsi device instances for each of all well known LUs
7142 * (except "REPORT LUNS" LU).
7144 * Returns zero on success (all required W-LUs are added successfully),
7145 * non-zero error value on failure (if failed to add any of the required W-LU).
7147 static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
7150 struct scsi_device *sdev_boot;
7152 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
7153 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
7154 if (IS_ERR(hba->sdev_ufs_device)) {
7155 ret = PTR_ERR(hba->sdev_ufs_device);
7156 hba->sdev_ufs_device = NULL;
7159 ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device);
7160 scsi_device_put(hba->sdev_ufs_device);
7162 hba->sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
7163 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
7164 if (IS_ERR(hba->sdev_rpmb)) {
7165 ret = PTR_ERR(hba->sdev_rpmb);
7166 goto remove_sdev_ufs_device;
7168 ufshcd_blk_pm_runtime_init(hba->sdev_rpmb);
7169 scsi_device_put(hba->sdev_rpmb);
7171 sdev_boot = __scsi_add_device(hba->host, 0, 0,
7172 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
7173 if (IS_ERR(sdev_boot)) {
7174 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
7176 ufshcd_blk_pm_runtime_init(sdev_boot);
7177 scsi_device_put(sdev_boot);
7181 remove_sdev_ufs_device:
7182 scsi_remove_device(hba->sdev_ufs_device);
7187 static void ufshcd_wb_probe(struct ufs_hba *hba, u8 *desc_buf)
7189 struct ufs_dev_info *dev_info = &hba->dev_info;
7191 u32 d_lu_wb_buf_alloc;
7193 if (!ufshcd_is_wb_allowed(hba))
7196 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or
7197 * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES
7200 if (!(dev_info->wspecversion >= 0x310 ||
7201 dev_info->wspecversion == 0x220 ||
7202 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES)))
7205 if (hba->desc_size[QUERY_DESC_IDN_DEVICE] <
7206 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP + 4)
7209 dev_info->d_ext_ufs_feature_sup =
7210 get_unaligned_be32(desc_buf +
7211 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
7213 if (!(dev_info->d_ext_ufs_feature_sup & UFS_DEV_WRITE_BOOSTER_SUP))
7217 * WB may be supported but not configured while provisioning.
7218 * The spec says, in dedicated wb buffer mode,
7219 * a max of 1 lun would have wb buffer configured.
7220 * Now only shared buffer mode is supported.
7222 dev_info->b_wb_buffer_type =
7223 desc_buf[DEVICE_DESC_PARAM_WB_TYPE];
7225 dev_info->b_presrv_uspc_en =
7226 desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN];
7228 if (dev_info->b_wb_buffer_type == WB_BUF_MODE_SHARED) {
7229 dev_info->d_wb_alloc_units =
7230 get_unaligned_be32(desc_buf +
7231 DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS);
7232 if (!dev_info->d_wb_alloc_units)
7235 for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) {
7236 d_lu_wb_buf_alloc = 0;
7237 ufshcd_read_unit_desc_param(hba,
7239 UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS,
7240 (u8 *)&d_lu_wb_buf_alloc,
7241 sizeof(d_lu_wb_buf_alloc));
7242 if (d_lu_wb_buf_alloc) {
7243 dev_info->wb_dedicated_lu = lun;
7248 if (!d_lu_wb_buf_alloc)
7254 hba->caps &= ~UFSHCD_CAP_WB_EN;
7257 void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups)
7259 struct ufs_dev_fix *f;
7260 struct ufs_dev_info *dev_info = &hba->dev_info;
7265 for (f = fixups; f->quirk; f++) {
7266 if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
7267 f->wmanufacturerid == UFS_ANY_VENDOR) &&
7268 ((dev_info->model &&
7269 STR_PRFX_EQUAL(f->model, dev_info->model)) ||
7270 !strcmp(f->model, UFS_ANY_MODEL)))
7271 hba->dev_quirks |= f->quirk;
7274 EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks);
7276 static void ufs_fixup_device_setup(struct ufs_hba *hba)
7278 /* fix by general quirk table */
7279 ufshcd_fixup_dev_quirks(hba, ufs_fixups);
7281 /* allow vendors to fix quirks */
7282 ufshcd_vops_fixup_dev_quirks(hba);
7285 static int ufs_get_device_desc(struct ufs_hba *hba)
7290 struct ufs_dev_info *dev_info = &hba->dev_info;
7292 desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
7298 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
7299 hba->desc_size[QUERY_DESC_IDN_DEVICE]);
7301 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
7307 * getting vendor (manufacturerID) and Bank Index in big endian
7310 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
7311 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
7313 /* getting Specification Version in big endian format */
7314 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
7315 desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
7317 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
7319 err = ufshcd_read_string_desc(hba, model_index,
7320 &dev_info->model, SD_ASCII_STD);
7322 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
7327 ufs_fixup_device_setup(hba);
7329 ufshcd_wb_probe(hba, desc_buf);
7332 * ufshcd_read_string_desc returns size of the string
7333 * reset the error value
7342 static void ufs_put_device_desc(struct ufs_hba *hba)
7344 struct ufs_dev_info *dev_info = &hba->dev_info;
7346 kfree(dev_info->model);
7347 dev_info->model = NULL;
7351 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
7352 * @hba: per-adapter instance
7354 * PA_TActivate parameter can be tuned manually if UniPro version is less than
7355 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
7356 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
7357 * the hibern8 exit latency.
7359 * Returns zero on success, non-zero error value on failure.
7361 static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
7364 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
7366 ret = ufshcd_dme_peer_get(hba,
7368 RX_MIN_ACTIVATETIME_CAPABILITY,
7369 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7370 &peer_rx_min_activatetime);
7374 /* make sure proper unit conversion is applied */
7375 tuned_pa_tactivate =
7376 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
7377 / PA_TACTIVATE_TIME_UNIT_US);
7378 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7379 tuned_pa_tactivate);
7386 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
7387 * @hba: per-adapter instance
7389 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
7390 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
7391 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
7392 * This optimal value can help reduce the hibern8 exit latency.
7394 * Returns zero on success, non-zero error value on failure.
7396 static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
7399 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
7400 u32 max_hibern8_time, tuned_pa_hibern8time;
7402 ret = ufshcd_dme_get(hba,
7403 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
7404 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
7405 &local_tx_hibern8_time_cap);
7409 ret = ufshcd_dme_peer_get(hba,
7410 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
7411 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7412 &peer_rx_hibern8_time_cap);
7416 max_hibern8_time = max(local_tx_hibern8_time_cap,
7417 peer_rx_hibern8_time_cap);
7418 /* make sure proper unit conversion is applied */
7419 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
7420 / PA_HIBERN8_TIME_UNIT_US);
7421 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
7422 tuned_pa_hibern8time);
7428 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
7429 * less than device PA_TACTIVATE time.
7430 * @hba: per-adapter instance
7432 * Some UFS devices require host PA_TACTIVATE to be lower than device
7433 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
7436 * Returns zero on success, non-zero error value on failure.
7438 static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
7441 u32 granularity, peer_granularity;
7442 u32 pa_tactivate, peer_pa_tactivate;
7443 u32 pa_tactivate_us, peer_pa_tactivate_us;
7444 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
7446 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7451 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7456 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
7457 (granularity > PA_GRANULARITY_MAX_VAL)) {
7458 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
7459 __func__, granularity);
7463 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
7464 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
7465 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
7466 __func__, peer_granularity);
7470 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
7474 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
7475 &peer_pa_tactivate);
7479 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
7480 peer_pa_tactivate_us = peer_pa_tactivate *
7481 gran_to_us_table[peer_granularity - 1];
7483 if (pa_tactivate_us > peer_pa_tactivate_us) {
7484 u32 new_peer_pa_tactivate;
7486 new_peer_pa_tactivate = pa_tactivate_us /
7487 gran_to_us_table[peer_granularity - 1];
7488 new_peer_pa_tactivate++;
7489 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7490 new_peer_pa_tactivate);
7497 static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
7499 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
7500 ufshcd_tune_pa_tactivate(hba);
7501 ufshcd_tune_pa_hibern8time(hba);
7504 ufshcd_vops_apply_dev_quirks(hba);
7506 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
7507 /* set 1ms timeout for PA_TACTIVATE */
7508 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
7510 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
7511 ufshcd_quirk_tune_host_pa_tactivate(hba);
7514 static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
7516 hba->ufs_stats.hibern8_exit_cnt = 0;
7517 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
7518 hba->req_abort_count = 0;
7521 static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
7527 buff_len = hba->desc_size[QUERY_DESC_IDN_GEOMETRY];
7528 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7534 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
7535 desc_buf, buff_len);
7537 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
7542 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
7543 hba->dev_info.max_lu_supported = 32;
7544 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
7545 hba->dev_info.max_lu_supported = 8;
7552 static struct ufs_ref_clk ufs_ref_clk_freqs[] = {
7553 {19200000, REF_CLK_FREQ_19_2_MHZ},
7554 {26000000, REF_CLK_FREQ_26_MHZ},
7555 {38400000, REF_CLK_FREQ_38_4_MHZ},
7556 {52000000, REF_CLK_FREQ_52_MHZ},
7557 {0, REF_CLK_FREQ_INVAL},
7560 static enum ufs_ref_clk_freq
7561 ufs_get_bref_clk_from_hz(unsigned long freq)
7565 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
7566 if (ufs_ref_clk_freqs[i].freq_hz == freq)
7567 return ufs_ref_clk_freqs[i].val;
7569 return REF_CLK_FREQ_INVAL;
7572 void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
7576 freq = clk_get_rate(refclk);
7578 hba->dev_ref_clk_freq =
7579 ufs_get_bref_clk_from_hz(freq);
7581 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
7583 "invalid ref_clk setting = %ld\n", freq);
7586 static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
7590 u32 freq = hba->dev_ref_clk_freq;
7592 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
7593 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
7596 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
7601 if (ref_clk == freq)
7602 goto out; /* nothing to update */
7604 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7605 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
7608 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
7609 ufs_ref_clk_freqs[freq].freq_hz);
7613 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
7614 ufs_ref_clk_freqs[freq].freq_hz);
7620 static int ufshcd_device_params_init(struct ufs_hba *hba)
7625 /* Init device descriptor sizes */
7626 for (i = 0; i < QUERY_DESC_IDN_MAX; i++)
7627 hba->desc_size[i] = QUERY_DESC_MAX_SIZE;
7629 /* Init UFS geometry descriptor related parameters */
7630 ret = ufshcd_device_geo_params_init(hba);
7634 /* Check and apply UFS device quirks */
7635 ret = ufs_get_device_desc(hba);
7637 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
7642 ufshcd_get_ref_clk_gating_wait(hba);
7644 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
7645 QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag))
7646 hba->dev_info.f_power_on_wp_en = flag;
7648 /* Probe maximum power mode co-supported by both UFS host and device */
7649 if (ufshcd_get_max_pwr_mode(hba))
7651 "%s: Failed getting max supported power mode\n",
7658 * ufshcd_add_lus - probe and add UFS logical units
7659 * @hba: per-adapter instance
7661 static int ufshcd_add_lus(struct ufs_hba *hba)
7665 /* Add required well known logical units to scsi mid layer */
7666 ret = ufshcd_scsi_add_wlus(hba);
7670 /* Initialize devfreq after UFS device is detected */
7671 if (ufshcd_is_clkscaling_supported(hba)) {
7672 memcpy(&hba->clk_scaling.saved_pwr_info.info,
7674 sizeof(struct ufs_pa_layer_attr));
7675 hba->clk_scaling.saved_pwr_info.is_valid = true;
7676 if (!hba->devfreq) {
7677 ret = ufshcd_devfreq_init(hba);
7682 hba->clk_scaling.is_allowed = true;
7686 scsi_scan_host(hba->host);
7687 pm_runtime_put_sync(hba->dev);
7694 ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp);
7696 static int ufshcd_clear_ua_wlun(struct ufs_hba *hba, u8 wlun)
7698 struct scsi_device *sdp;
7699 unsigned long flags;
7702 spin_lock_irqsave(hba->host->host_lock, flags);
7703 if (wlun == UFS_UPIU_UFS_DEVICE_WLUN)
7704 sdp = hba->sdev_ufs_device;
7705 else if (wlun == UFS_UPIU_RPMB_WLUN)
7706 sdp = hba->sdev_rpmb;
7710 ret = scsi_device_get(sdp);
7711 if (!ret && !scsi_device_online(sdp)) {
7713 scsi_device_put(sdp);
7718 spin_unlock_irqrestore(hba->host->host_lock, flags);
7722 ret = ufshcd_send_request_sense(hba, sdp);
7723 scsi_device_put(sdp);
7726 dev_err(hba->dev, "%s: UAC clear LU=%x ret = %d\n",
7727 __func__, wlun, ret);
7731 static int ufshcd_clear_ua_wluns(struct ufs_hba *hba)
7735 if (!hba->wlun_dev_clr_ua)
7738 ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_UFS_DEVICE_WLUN);
7740 ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_RPMB_WLUN);
7742 hba->wlun_dev_clr_ua = false;
7745 dev_err(hba->dev, "%s: Failed to clear UAC WLUNS ret = %d\n",
7751 * ufshcd_probe_hba - probe hba to detect device and initialize
7752 * @hba: per-adapter instance
7753 * @async: asynchronous execution or not
7755 * Execute link-startup and verify device initialization
7757 static int ufshcd_probe_hba(struct ufs_hba *hba, bool async)
7760 unsigned long flags;
7761 ktime_t start = ktime_get();
7763 ret = ufshcd_link_startup(hba);
7767 /* Debug counters initialization */
7768 ufshcd_clear_dbg_ufs_stats(hba);
7770 /* UniPro link is active now */
7771 ufshcd_set_link_active(hba);
7773 /* Verify device initialization by sending NOP OUT UPIU */
7774 ret = ufshcd_verify_dev_init(hba);
7778 /* Initiate UFS initialization, and waiting until completion */
7779 ret = ufshcd_complete_dev_init(hba);
7784 * Initialize UFS device parameters used by driver, these
7785 * parameters are associated with UFS descriptors.
7788 ret = ufshcd_device_params_init(hba);
7793 ufshcd_tune_unipro_params(hba);
7795 /* UFS device is also active now */
7796 ufshcd_set_ufs_dev_active(hba);
7797 ufshcd_force_reset_auto_bkops(hba);
7798 hba->wlun_dev_clr_ua = true;
7800 /* Gear up to HS gear if supported */
7801 if (hba->max_pwr_info.is_valid) {
7803 * Set the right value to bRefClkFreq before attempting to
7804 * switch to HS gears.
7806 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
7807 ufshcd_set_dev_ref_clk(hba);
7808 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
7810 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7814 ufshcd_print_pwr_info(hba);
7818 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
7819 * and for removable UFS card as well, hence always set the parameter.
7820 * Note: Error handler may issue the device reset hence resetting
7821 * bActiveICCLevel as well so it is always safe to set this here.
7823 ufshcd_set_active_icc_lvl(hba);
7825 ufshcd_wb_config(hba);
7826 /* Enable Auto-Hibernate if configured */
7827 ufshcd_auto_hibern8_enable(hba);
7830 spin_lock_irqsave(hba->host->host_lock, flags);
7832 hba->ufshcd_state = UFSHCD_STATE_ERROR;
7833 else if (hba->ufshcd_state == UFSHCD_STATE_RESET)
7834 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
7835 spin_unlock_irqrestore(hba->host->host_lock, flags);
7837 trace_ufshcd_init(dev_name(hba->dev), ret,
7838 ktime_to_us(ktime_sub(ktime_get(), start)),
7839 hba->curr_dev_pwr_mode, hba->uic_link_state);
7844 * ufshcd_async_scan - asynchronous execution for probing hba
7845 * @data: data pointer to pass to this function
7846 * @cookie: cookie data
7848 static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7850 struct ufs_hba *hba = (struct ufs_hba *)data;
7854 /* Initialize hba, detect and initialize UFS device */
7855 ret = ufshcd_probe_hba(hba, true);
7860 /* Probe and add UFS logical units */
7861 ret = ufshcd_add_lus(hba);
7864 * If we failed to initialize the device or the device is not
7865 * present, turn off the power/clocks etc.
7868 pm_runtime_put_sync(hba->dev);
7869 ufshcd_exit_clk_scaling(hba);
7870 ufshcd_hba_exit(hba);
7872 ufshcd_clear_ua_wluns(hba);
7876 static const struct attribute_group *ufshcd_driver_groups[] = {
7877 &ufs_sysfs_unit_descriptor_group,
7878 &ufs_sysfs_lun_attributes_group,
7882 static struct ufs_hba_variant_params ufs_hba_vps = {
7883 .hba_enable_delay_us = 1000,
7884 .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40),
7885 .devfreq_profile.polling_ms = 100,
7886 .devfreq_profile.target = ufshcd_devfreq_target,
7887 .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status,
7888 .ondemand_data.upthreshold = 70,
7889 .ondemand_data.downdifferential = 5,
7892 static struct scsi_host_template ufshcd_driver_template = {
7893 .module = THIS_MODULE,
7895 .proc_name = UFSHCD,
7896 .queuecommand = ufshcd_queuecommand,
7897 .slave_alloc = ufshcd_slave_alloc,
7898 .slave_configure = ufshcd_slave_configure,
7899 .slave_destroy = ufshcd_slave_destroy,
7900 .change_queue_depth = ufshcd_change_queue_depth,
7901 .eh_abort_handler = ufshcd_abort,
7902 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7903 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
7905 .sg_tablesize = SG_ALL,
7906 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
7907 .can_queue = UFSHCD_CAN_QUEUE,
7908 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
7909 .max_host_blocked = 1,
7910 .track_queue_depth = 1,
7911 .sdev_groups = ufshcd_driver_groups,
7912 .dma_boundary = PAGE_SIZE - 1,
7913 .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS,
7916 static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
7925 * "set_load" operation shall be required on those regulators
7926 * which specifically configured current limitation. Otherwise
7927 * zero max_uA may cause unexpected behavior when regulator is
7928 * enabled or set as high power mode.
7933 ret = regulator_set_load(vreg->reg, ua);
7935 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
7936 __func__, vreg->name, ua, ret);
7942 static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
7943 struct ufs_vreg *vreg)
7945 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
7948 static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
7949 struct ufs_vreg *vreg)
7954 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
7957 static int ufshcd_config_vreg(struct device *dev,
7958 struct ufs_vreg *vreg, bool on)
7961 struct regulator *reg;
7963 int min_uV, uA_load;
7970 if (regulator_count_voltages(reg) > 0) {
7971 uA_load = on ? vreg->max_uA : 0;
7972 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
7976 if (vreg->min_uV && vreg->max_uV) {
7977 min_uV = on ? vreg->min_uV : 0;
7978 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
7981 "%s: %s set voltage failed, err=%d\n",
7982 __func__, name, ret);
7989 static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
7993 if (!vreg || vreg->enabled)
7996 ret = ufshcd_config_vreg(dev, vreg, true);
7998 ret = regulator_enable(vreg->reg);
8001 vreg->enabled = true;
8003 dev_err(dev, "%s: %s enable failed, err=%d\n",
8004 __func__, vreg->name, ret);
8009 static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
8013 if (!vreg || !vreg->enabled)
8016 ret = regulator_disable(vreg->reg);
8019 /* ignore errors on applying disable config */
8020 ufshcd_config_vreg(dev, vreg, false);
8021 vreg->enabled = false;
8023 dev_err(dev, "%s: %s disable failed, err=%d\n",
8024 __func__, vreg->name, ret);
8030 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
8033 struct device *dev = hba->dev;
8034 struct ufs_vreg_info *info = &hba->vreg_info;
8036 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
8040 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
8044 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
8048 ufshcd_toggle_vreg(dev, info->vccq2, false);
8049 ufshcd_toggle_vreg(dev, info->vccq, false);
8050 ufshcd_toggle_vreg(dev, info->vcc, false);
8055 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
8057 struct ufs_vreg_info *info = &hba->vreg_info;
8059 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
8062 static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
8069 vreg->reg = devm_regulator_get(dev, vreg->name);
8070 if (IS_ERR(vreg->reg)) {
8071 ret = PTR_ERR(vreg->reg);
8072 dev_err(dev, "%s: %s get failed, err=%d\n",
8073 __func__, vreg->name, ret);
8079 static int ufshcd_init_vreg(struct ufs_hba *hba)
8082 struct device *dev = hba->dev;
8083 struct ufs_vreg_info *info = &hba->vreg_info;
8085 ret = ufshcd_get_vreg(dev, info->vcc);
8089 ret = ufshcd_get_vreg(dev, info->vccq);
8091 ret = ufshcd_get_vreg(dev, info->vccq2);
8096 static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
8098 struct ufs_vreg_info *info = &hba->vreg_info;
8101 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
8106 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
8109 struct ufs_clk_info *clki;
8110 struct list_head *head = &hba->clk_list_head;
8111 unsigned long flags;
8112 ktime_t start = ktime_get();
8113 bool clk_state_changed = false;
8115 if (list_empty(head))
8118 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
8122 list_for_each_entry(clki, head, list) {
8123 if (!IS_ERR_OR_NULL(clki->clk)) {
8125 * Don't disable clocks which are needed
8126 * to keep the link active.
8128 if (ufshcd_is_link_active(hba) &&
8129 clki->keep_link_active)
8132 clk_state_changed = on ^ clki->enabled;
8133 if (on && !clki->enabled) {
8134 ret = clk_prepare_enable(clki->clk);
8136 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
8137 __func__, clki->name, ret);
8140 } else if (!on && clki->enabled) {
8141 clk_disable_unprepare(clki->clk);
8144 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
8145 clki->name, on ? "en" : "dis");
8149 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
8155 list_for_each_entry(clki, head, list) {
8156 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
8157 clk_disable_unprepare(clki->clk);
8159 } else if (!ret && on) {
8160 spin_lock_irqsave(hba->host->host_lock, flags);
8161 hba->clk_gating.state = CLKS_ON;
8162 trace_ufshcd_clk_gating(dev_name(hba->dev),
8163 hba->clk_gating.state);
8164 spin_unlock_irqrestore(hba->host->host_lock, flags);
8167 if (clk_state_changed)
8168 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
8169 (on ? "on" : "off"),
8170 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
8174 static int ufshcd_init_clocks(struct ufs_hba *hba)
8177 struct ufs_clk_info *clki;
8178 struct device *dev = hba->dev;
8179 struct list_head *head = &hba->clk_list_head;
8181 if (list_empty(head))
8184 list_for_each_entry(clki, head, list) {
8188 clki->clk = devm_clk_get(dev, clki->name);
8189 if (IS_ERR(clki->clk)) {
8190 ret = PTR_ERR(clki->clk);
8191 dev_err(dev, "%s: %s clk get failed, %d\n",
8192 __func__, clki->name, ret);
8197 * Parse device ref clk freq as per device tree "ref_clk".
8198 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
8199 * in ufshcd_alloc_host().
8201 if (!strcmp(clki->name, "ref_clk"))
8202 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
8204 if (clki->max_freq) {
8205 ret = clk_set_rate(clki->clk, clki->max_freq);
8207 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
8208 __func__, clki->name,
8209 clki->max_freq, ret);
8212 clki->curr_freq = clki->max_freq;
8214 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
8215 clki->name, clk_get_rate(clki->clk));
8221 static int ufshcd_variant_hba_init(struct ufs_hba *hba)
8228 err = ufshcd_vops_init(hba);
8230 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
8231 __func__, ufshcd_get_var_name(hba), err);
8236 static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
8241 ufshcd_vops_exit(hba);
8244 static int ufshcd_hba_init(struct ufs_hba *hba)
8249 * Handle host controller power separately from the UFS device power
8250 * rails as it will help controlling the UFS host controller power
8251 * collapse easily which is different than UFS device power collapse.
8252 * Also, enable the host controller power before we go ahead with rest
8253 * of the initialization here.
8255 err = ufshcd_init_hba_vreg(hba);
8259 err = ufshcd_setup_hba_vreg(hba, true);
8263 err = ufshcd_init_clocks(hba);
8265 goto out_disable_hba_vreg;
8267 err = ufshcd_setup_clocks(hba, true);
8269 goto out_disable_hba_vreg;
8271 err = ufshcd_init_vreg(hba);
8273 goto out_disable_clks;
8275 err = ufshcd_setup_vreg(hba, true);
8277 goto out_disable_clks;
8279 err = ufshcd_variant_hba_init(hba);
8281 goto out_disable_vreg;
8283 hba->is_powered = true;
8287 ufshcd_setup_vreg(hba, false);
8289 ufshcd_setup_clocks(hba, false);
8290 out_disable_hba_vreg:
8291 ufshcd_setup_hba_vreg(hba, false);
8296 static void ufshcd_hba_exit(struct ufs_hba *hba)
8298 if (hba->is_powered) {
8299 ufshcd_variant_hba_exit(hba);
8300 ufshcd_setup_vreg(hba, false);
8301 ufshcd_suspend_clkscaling(hba);
8302 if (ufshcd_is_clkscaling_supported(hba))
8304 ufshcd_suspend_clkscaling(hba);
8305 ufshcd_setup_clocks(hba, false);
8306 ufshcd_setup_hba_vreg(hba, false);
8307 hba->is_powered = false;
8308 ufs_put_device_desc(hba);
8313 ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
8315 unsigned char cmd[6] = {REQUEST_SENSE,
8324 buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL);
8330 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
8331 UFS_SENSE_SIZE, NULL, NULL,
8332 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
8334 pr_err("%s: failed with err %d\n", __func__, ret);
8342 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
8344 * @hba: per adapter instance
8345 * @pwr_mode: device power mode to set
8347 * Returns 0 if requested power mode is set successfully
8348 * Returns non-zero if failed to set the requested power mode
8350 static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
8351 enum ufs_dev_pwr_mode pwr_mode)
8353 unsigned char cmd[6] = { START_STOP };
8354 struct scsi_sense_hdr sshdr;
8355 struct scsi_device *sdp;
8356 unsigned long flags;
8359 spin_lock_irqsave(hba->host->host_lock, flags);
8360 sdp = hba->sdev_ufs_device;
8362 ret = scsi_device_get(sdp);
8363 if (!ret && !scsi_device_online(sdp)) {
8365 scsi_device_put(sdp);
8370 spin_unlock_irqrestore(hba->host->host_lock, flags);
8376 * If scsi commands fail, the scsi mid-layer schedules scsi error-
8377 * handling, which would wait for host to be resumed. Since we know
8378 * we are functional while we are here, skip host resume in error
8381 hba->host->eh_noresume = 1;
8382 if (hba->wlun_dev_clr_ua) {
8383 ret = ufshcd_send_request_sense(hba, sdp);
8386 /* Unit attention condition is cleared now */
8387 hba->wlun_dev_clr_ua = false;
8390 cmd[4] = pwr_mode << 4;
8393 * Current function would be generally called from the power management
8394 * callbacks hence set the RQF_PM flag so that it doesn't resume the
8395 * already suspended childs.
8397 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
8398 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
8400 sdev_printk(KERN_WARNING, sdp,
8401 "START_STOP failed for power mode: %d, result %x\n",
8403 if (driver_byte(ret) == DRIVER_SENSE)
8404 scsi_print_sense_hdr(sdp, NULL, &sshdr);
8408 hba->curr_dev_pwr_mode = pwr_mode;
8410 scsi_device_put(sdp);
8411 hba->host->eh_noresume = 0;
8415 static int ufshcd_link_state_transition(struct ufs_hba *hba,
8416 enum uic_link_state req_link_state,
8417 int check_for_bkops)
8421 if (req_link_state == hba->uic_link_state)
8424 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
8425 ret = ufshcd_uic_hibern8_enter(hba);
8427 ufshcd_set_link_hibern8(hba);
8429 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
8435 * If autobkops is enabled, link can't be turned off because
8436 * turning off the link would also turn off the device, except in the
8437 * case of DeepSleep where the device is expected to remain powered.
8439 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
8440 (!check_for_bkops || !hba->auto_bkops_enabled)) {
8442 * Let's make sure that link is in low power mode, we are doing
8443 * this currently by putting the link in Hibern8. Otherway to
8444 * put the link in low power mode is to send the DME end point
8445 * to device and then send the DME reset command to local
8446 * unipro. But putting the link in hibern8 is much faster.
8448 * Note also that putting the link in Hibern8 is a requirement
8449 * for entering DeepSleep.
8451 ret = ufshcd_uic_hibern8_enter(hba);
8453 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
8458 * Change controller state to "reset state" which
8459 * should also put the link in off/reset state
8461 ufshcd_hba_stop(hba);
8463 * TODO: Check if we need any delay to make sure that
8464 * controller is reset
8466 ufshcd_set_link_off(hba);
8473 static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
8475 bool vcc_off = false;
8478 * It seems some UFS devices may keep drawing more than sleep current
8479 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
8480 * To avoid this situation, add 2ms delay before putting these UFS
8481 * rails in LPM mode.
8483 if (!ufshcd_is_link_active(hba) &&
8484 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
8485 usleep_range(2000, 2100);
8488 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
8491 * If UFS device and link is in OFF state, all power supplies (VCC,
8492 * VCCQ, VCCQ2) can be turned off if power on write protect is not
8493 * required. If UFS link is inactive (Hibern8 or OFF state) and device
8494 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
8496 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
8497 * in low power state which would save some power.
8499 * If Write Booster is enabled and the device needs to flush the WB
8500 * buffer OR if bkops status is urgent for WB, keep Vcc on.
8502 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8503 !hba->dev_info.is_lu_power_on_wp) {
8504 ufshcd_setup_vreg(hba, false);
8506 } else if (!ufshcd_is_ufs_dev_active(hba)) {
8507 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8509 if (!ufshcd_is_link_active(hba)) {
8510 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8511 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
8516 * Some UFS devices require delay after VCC power rail is turned-off.
8518 if (vcc_off && hba->vreg_info.vcc &&
8519 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)
8520 usleep_range(5000, 5100);
8523 static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
8527 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8528 !hba->dev_info.is_lu_power_on_wp) {
8529 ret = ufshcd_setup_vreg(hba, true);
8530 } else if (!ufshcd_is_ufs_dev_active(hba)) {
8531 if (!ret && !ufshcd_is_link_active(hba)) {
8532 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
8535 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
8539 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
8544 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8546 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8551 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
8553 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
8554 ufshcd_setup_hba_vreg(hba, false);
8557 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
8559 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
8560 ufshcd_setup_hba_vreg(hba, true);
8564 * ufshcd_suspend - helper function for suspend operations
8565 * @hba: per adapter instance
8566 * @pm_op: desired low power operation type
8568 * This function will try to put the UFS device and link into low power
8569 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
8570 * (System PM level).
8572 * If this function is called during shutdown, it will make sure that
8573 * both UFS device and UFS link is powered off.
8575 * NOTE: UFS device & link must be active before we enter in this function.
8577 * Returns 0 for success and non-zero for failure
8579 static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8582 int check_for_bkops;
8583 enum ufs_pm_level pm_lvl;
8584 enum ufs_dev_pwr_mode req_dev_pwr_mode;
8585 enum uic_link_state req_link_state;
8587 hba->pm_op_in_progress = 1;
8588 if (!ufshcd_is_shutdown_pm(pm_op)) {
8589 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
8590 hba->rpm_lvl : hba->spm_lvl;
8591 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
8592 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
8594 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
8595 req_link_state = UIC_LINK_OFF_STATE;
8599 * If we can't transition into any of the low power modes
8600 * just gate the clocks.
8602 ufshcd_hold(hba, false);
8603 hba->clk_gating.is_suspended = true;
8605 if (hba->clk_scaling.is_allowed) {
8606 cancel_work_sync(&hba->clk_scaling.suspend_work);
8607 cancel_work_sync(&hba->clk_scaling.resume_work);
8608 ufshcd_suspend_clkscaling(hba);
8611 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8612 req_link_state == UIC_LINK_ACTIVE_STATE) {
8616 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8617 (req_link_state == hba->uic_link_state))
8620 /* UFS device & link must be active before we enter in this function */
8621 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8626 if (ufshcd_is_runtime_pm(pm_op)) {
8627 if (ufshcd_can_autobkops_during_suspend(hba)) {
8629 * The device is idle with no requests in the queue,
8630 * allow background operations if bkops status shows
8631 * that performance might be impacted.
8633 ret = ufshcd_urgent_bkops(hba);
8637 /* make sure that auto bkops is disabled */
8638 ufshcd_disable_auto_bkops(hba);
8641 * If device needs to do BKOP or WB buffer flush during
8642 * Hibern8, keep device power mode as "active power mode"
8645 hba->dev_info.b_rpm_dev_flush_capable =
8646 hba->auto_bkops_enabled ||
8647 (((req_link_state == UIC_LINK_HIBERN8_STATE) ||
8648 ((req_link_state == UIC_LINK_ACTIVE_STATE) &&
8649 ufshcd_is_auto_hibern8_enabled(hba))) &&
8650 ufshcd_wb_need_flush(hba));
8653 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) {
8654 if (!ufshcd_is_runtime_pm(pm_op))
8655 /* ensure that bkops is disabled */
8656 ufshcd_disable_auto_bkops(hba);
8658 if (!hba->dev_info.b_rpm_dev_flush_capable) {
8659 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8665 flush_work(&hba->eeh_work);
8668 * In the case of DeepSleep, the device is expected to remain powered
8669 * with the link off, so do not check for bkops.
8671 check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba);
8672 ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops);
8674 goto set_dev_active;
8676 ufshcd_vreg_set_lpm(hba);
8680 * Call vendor specific suspend callback. As these callbacks may access
8681 * vendor specific host controller register space call them before the
8682 * host clocks are ON.
8684 ret = ufshcd_vops_suspend(hba, pm_op);
8686 goto set_link_active;
8688 * Disable the host irq as host controller as there won't be any
8689 * host controller transaction expected till resume.
8691 ufshcd_disable_irq(hba);
8693 ufshcd_setup_clocks(hba, false);
8695 if (ufshcd_is_clkgating_allowed(hba)) {
8696 hba->clk_gating.state = CLKS_OFF;
8697 trace_ufshcd_clk_gating(dev_name(hba->dev),
8698 hba->clk_gating.state);
8701 /* Put the host controller in low power mode if possible */
8702 ufshcd_hba_vreg_set_lpm(hba);
8706 if (hba->clk_scaling.is_allowed)
8707 ufshcd_resume_clkscaling(hba);
8708 ufshcd_vreg_set_hpm(hba);
8710 * Device hardware reset is required to exit DeepSleep. Also, for
8711 * DeepSleep, the link is off so host reset and restore will be done
8714 if (ufshcd_is_ufs_dev_deepsleep(hba)) {
8715 ufshcd_vops_device_reset(hba);
8716 WARN_ON(!ufshcd_is_link_off(hba));
8718 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
8719 ufshcd_set_link_active(hba);
8720 else if (ufshcd_is_link_off(hba))
8721 ufshcd_host_reset_and_restore(hba);
8723 /* Can also get here needing to exit DeepSleep */
8724 if (ufshcd_is_ufs_dev_deepsleep(hba)) {
8725 ufshcd_vops_device_reset(hba);
8726 ufshcd_host_reset_and_restore(hba);
8728 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8729 ufshcd_disable_auto_bkops(hba);
8731 if (hba->clk_scaling.is_allowed)
8732 ufshcd_resume_clkscaling(hba);
8733 hba->clk_gating.is_suspended = false;
8734 hba->dev_info.b_rpm_dev_flush_capable = false;
8735 ufshcd_release(hba);
8737 if (hba->dev_info.b_rpm_dev_flush_capable) {
8738 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work,
8739 msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS));
8742 hba->pm_op_in_progress = 0;
8745 ufshcd_update_evt_hist(hba, UFS_EVT_SUSPEND_ERR, (u32)ret);
8750 * ufshcd_resume - helper function for resume operations
8751 * @hba: per adapter instance
8752 * @pm_op: runtime PM or system PM
8754 * This function basically brings the UFS device, UniPro link and controller
8757 * Returns 0 for success and non-zero for failure
8759 static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8762 enum uic_link_state old_link_state;
8764 hba->pm_op_in_progress = 1;
8765 old_link_state = hba->uic_link_state;
8767 ufshcd_hba_vreg_set_hpm(hba);
8768 /* Make sure clocks are enabled before accessing controller */
8769 ret = ufshcd_setup_clocks(hba, true);
8773 /* enable the host irq as host controller would be active soon */
8774 ufshcd_enable_irq(hba);
8776 ret = ufshcd_vreg_set_hpm(hba);
8778 goto disable_irq_and_vops_clks;
8781 * Call vendor specific resume callback. As these callbacks may access
8782 * vendor specific host controller register space call them when the
8783 * host clocks are ON.
8785 ret = ufshcd_vops_resume(hba, pm_op);
8789 /* For DeepSleep, the only supported option is to have the link off */
8790 WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba));
8792 if (ufshcd_is_link_hibern8(hba)) {
8793 ret = ufshcd_uic_hibern8_exit(hba);
8795 ufshcd_set_link_active(hba);
8797 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
8799 goto vendor_suspend;
8801 } else if (ufshcd_is_link_off(hba)) {
8803 * A full initialization of the host and the device is
8804 * required since the link was put to off during suspend.
8805 * Note, in the case of DeepSleep, the device will exit
8806 * DeepSleep due to device reset.
8808 ret = ufshcd_reset_and_restore(hba);
8810 * ufshcd_reset_and_restore() should have already
8811 * set the link state as active
8813 if (ret || !ufshcd_is_link_active(hba))
8814 goto vendor_suspend;
8817 if (!ufshcd_is_ufs_dev_active(hba)) {
8818 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8820 goto set_old_link_state;
8823 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8824 ufshcd_enable_auto_bkops(hba);
8827 * If BKOPs operations are urgently needed at this moment then
8828 * keep auto-bkops enabled or else disable it.
8830 ufshcd_urgent_bkops(hba);
8832 hba->clk_gating.is_suspended = false;
8834 if (hba->clk_scaling.is_allowed)
8835 ufshcd_resume_clkscaling(hba);
8837 /* Enable Auto-Hibernate if configured */
8838 ufshcd_auto_hibern8_enable(hba);
8840 if (hba->dev_info.b_rpm_dev_flush_capable) {
8841 hba->dev_info.b_rpm_dev_flush_capable = false;
8842 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
8845 /* Schedule clock gating in case of no access to UFS device yet */
8846 ufshcd_release(hba);
8851 ufshcd_link_state_transition(hba, old_link_state, 0);
8853 ufshcd_vops_suspend(hba, pm_op);
8855 ufshcd_vreg_set_lpm(hba);
8856 disable_irq_and_vops_clks:
8857 ufshcd_disable_irq(hba);
8858 if (hba->clk_scaling.is_allowed)
8859 ufshcd_suspend_clkscaling(hba);
8860 ufshcd_setup_clocks(hba, false);
8861 if (ufshcd_is_clkgating_allowed(hba)) {
8862 hba->clk_gating.state = CLKS_OFF;
8863 trace_ufshcd_clk_gating(dev_name(hba->dev),
8864 hba->clk_gating.state);
8867 hba->pm_op_in_progress = 0;
8869 ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)ret);
8874 * ufshcd_system_suspend - system suspend routine
8875 * @hba: per adapter instance
8877 * Check the description of ufshcd_suspend() function for more details.
8879 * Returns 0 for success and non-zero for failure
8881 int ufshcd_system_suspend(struct ufs_hba *hba)
8884 ktime_t start = ktime_get();
8887 if (!hba || !hba->is_powered)
8890 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8891 hba->curr_dev_pwr_mode) &&
8892 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8893 hba->uic_link_state))
8896 if (pm_runtime_suspended(hba->dev)) {
8898 * UFS device and/or UFS link low power states during runtime
8899 * suspend seems to be different than what is expected during
8900 * system suspend. Hence runtime resume the devic & link and
8901 * let the system suspend low power states to take effect.
8902 * TODO: If resume takes longer time, we might have optimize
8903 * it in future by not resuming everything if possible.
8905 ret = ufshcd_runtime_resume(hba);
8910 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
8912 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
8913 ktime_to_us(ktime_sub(ktime_get(), start)),
8914 hba->curr_dev_pwr_mode, hba->uic_link_state);
8916 hba->is_sys_suspended = true;
8921 EXPORT_SYMBOL(ufshcd_system_suspend);
8924 * ufshcd_system_resume - system resume routine
8925 * @hba: per adapter instance
8927 * Returns 0 for success and non-zero for failure
8930 int ufshcd_system_resume(struct ufs_hba *hba)
8933 ktime_t start = ktime_get();
8940 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
8942 * Let the runtime resume take care of resuming
8943 * if runtime suspended.
8947 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
8949 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
8950 ktime_to_us(ktime_sub(ktime_get(), start)),
8951 hba->curr_dev_pwr_mode, hba->uic_link_state);
8953 hba->is_sys_suspended = false;
8957 EXPORT_SYMBOL(ufshcd_system_resume);
8960 * ufshcd_runtime_suspend - runtime suspend routine
8961 * @hba: per adapter instance
8963 * Check the description of ufshcd_suspend() function for more details.
8965 * Returns 0 for success and non-zero for failure
8967 int ufshcd_runtime_suspend(struct ufs_hba *hba)
8970 ktime_t start = ktime_get();
8975 if (!hba->is_powered)
8978 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
8980 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
8981 ktime_to_us(ktime_sub(ktime_get(), start)),
8982 hba->curr_dev_pwr_mode, hba->uic_link_state);
8985 EXPORT_SYMBOL(ufshcd_runtime_suspend);
8988 * ufshcd_runtime_resume - runtime resume routine
8989 * @hba: per adapter instance
8991 * This function basically brings the UFS device, UniPro link and controller
8992 * to active state. Following operations are done in this function:
8994 * 1. Turn on all the controller related clocks
8995 * 2. Bring the UniPro link out of Hibernate state
8996 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
8998 * 4. If auto-bkops is enabled on the device, disable it.
9000 * So following would be the possible power state after this function return
9002 * S1: UFS device in Active state with VCC rail ON
9003 * UniPro link in Active state
9004 * All the UFS/UniPro controller clocks are ON
9006 * Returns 0 for success and non-zero for failure
9008 int ufshcd_runtime_resume(struct ufs_hba *hba)
9011 ktime_t start = ktime_get();
9016 if (!hba->is_powered)
9019 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
9021 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
9022 ktime_to_us(ktime_sub(ktime_get(), start)),
9023 hba->curr_dev_pwr_mode, hba->uic_link_state);
9026 EXPORT_SYMBOL(ufshcd_runtime_resume);
9028 int ufshcd_runtime_idle(struct ufs_hba *hba)
9032 EXPORT_SYMBOL(ufshcd_runtime_idle);
9035 * ufshcd_shutdown - shutdown routine
9036 * @hba: per adapter instance
9038 * This function would power off both UFS device and UFS link.
9040 * Returns 0 always to allow force shutdown even in case of errors.
9042 int ufshcd_shutdown(struct ufs_hba *hba)
9047 if (!hba->is_powered)
9050 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
9053 if (pm_runtime_suspended(hba->dev)) {
9054 ret = ufshcd_runtime_resume(hba);
9059 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
9062 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
9063 hba->is_powered = false;
9065 /* allow force shutdown even in case of errors */
9068 EXPORT_SYMBOL(ufshcd_shutdown);
9071 * ufshcd_remove - de-allocate SCSI host and host memory space
9072 * data structure memory
9073 * @hba: per adapter instance
9075 void ufshcd_remove(struct ufs_hba *hba)
9077 ufs_bsg_remove(hba);
9078 ufs_sysfs_remove_nodes(hba->dev);
9079 blk_cleanup_queue(hba->tmf_queue);
9080 blk_mq_free_tag_set(&hba->tmf_tag_set);
9081 blk_cleanup_queue(hba->cmd_queue);
9082 scsi_remove_host(hba->host);
9083 /* disable interrupts */
9084 ufshcd_disable_intr(hba, hba->intr_mask);
9085 ufshcd_hba_stop(hba);
9087 ufshcd_exit_clk_scaling(hba);
9088 ufshcd_exit_clk_gating(hba);
9089 if (ufshcd_is_clkscaling_supported(hba))
9090 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
9091 ufshcd_hba_exit(hba);
9093 EXPORT_SYMBOL_GPL(ufshcd_remove);
9096 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
9097 * @hba: pointer to Host Bus Adapter (HBA)
9099 void ufshcd_dealloc_host(struct ufs_hba *hba)
9101 ufshcd_crypto_destroy_keyslot_manager(hba);
9102 scsi_host_put(hba->host);
9104 EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
9107 * ufshcd_set_dma_mask - Set dma mask based on the controller
9108 * addressing capability
9109 * @hba: per adapter instance
9111 * Returns 0 for success, non-zero for failure
9113 static int ufshcd_set_dma_mask(struct ufs_hba *hba)
9115 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
9116 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
9119 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
9123 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
9124 * @dev: pointer to device handle
9125 * @hba_handle: driver private handle
9126 * Returns 0 on success, non-zero value on failure
9128 int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
9130 struct Scsi_Host *host;
9131 struct ufs_hba *hba;
9136 "Invalid memory reference for dev is NULL\n");
9141 host = scsi_host_alloc(&ufshcd_driver_template,
9142 sizeof(struct ufs_hba));
9144 dev_err(dev, "scsi_host_alloc failed\n");
9148 hba = shost_priv(host);
9152 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
9154 INIT_LIST_HEAD(&hba->clk_list_head);
9159 EXPORT_SYMBOL(ufshcd_alloc_host);
9161 /* This function exists because blk_mq_alloc_tag_set() requires this. */
9162 static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
9163 const struct blk_mq_queue_data *qd)
9166 return BLK_STS_NOTSUPP;
9169 static const struct blk_mq_ops ufshcd_tmf_ops = {
9170 .queue_rq = ufshcd_queue_tmf,
9174 * ufshcd_init - Driver initialization routine
9175 * @hba: per-adapter instance
9176 * @mmio_base: base register address
9177 * @irq: Interrupt line of device
9178 * Returns 0 on success, non-zero value on failure
9180 int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
9183 struct Scsi_Host *host = hba->host;
9184 struct device *dev = hba->dev;
9185 char eh_wq_name[sizeof("ufs_eh_wq_00")];
9189 "Invalid memory reference for mmio_base is NULL\n");
9194 hba->mmio_base = mmio_base;
9196 hba->vps = &ufs_hba_vps;
9198 err = ufshcd_hba_init(hba);
9202 /* Read capabilities registers */
9203 err = ufshcd_hba_capabilities(hba);
9207 /* Get UFS version supported by the controller */
9208 hba->ufs_version = ufshcd_get_ufs_version(hba);
9210 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
9211 (hba->ufs_version != UFSHCI_VERSION_11) &&
9212 (hba->ufs_version != UFSHCI_VERSION_20) &&
9213 (hba->ufs_version != UFSHCI_VERSION_21))
9214 dev_err(hba->dev, "invalid UFS version 0x%x\n",
9217 /* Get Interrupt bit mask per version */
9218 hba->intr_mask = ufshcd_get_intr_mask(hba);
9220 err = ufshcd_set_dma_mask(hba);
9222 dev_err(hba->dev, "set dma mask failed\n");
9226 /* Allocate memory for host memory space */
9227 err = ufshcd_memory_alloc(hba);
9229 dev_err(hba->dev, "Memory allocation failed\n");
9234 ufshcd_host_memory_configure(hba);
9236 host->can_queue = hba->nutrs;
9237 host->cmd_per_lun = hba->nutrs;
9238 host->max_id = UFSHCD_MAX_ID;
9239 host->max_lun = UFS_MAX_LUNS;
9240 host->max_channel = UFSHCD_MAX_CHANNEL;
9241 host->unique_id = host->host_no;
9242 host->max_cmd_len = UFS_CDB_SIZE;
9244 hba->max_pwr_info.is_valid = false;
9246 /* Initialize work queues */
9247 snprintf(eh_wq_name, sizeof(eh_wq_name), "ufs_eh_wq_%d",
9248 hba->host->host_no);
9249 hba->eh_wq = create_singlethread_workqueue(eh_wq_name);
9251 dev_err(hba->dev, "%s: failed to create eh workqueue\n",
9256 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
9257 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
9259 sema_init(&hba->eh_sem, 1);
9261 /* Initialize UIC command mutex */
9262 mutex_init(&hba->uic_cmd_mutex);
9264 /* Initialize mutex for device management commands */
9265 mutex_init(&hba->dev_cmd.lock);
9267 init_rwsem(&hba->clk_scaling_lock);
9269 ufshcd_init_clk_gating(hba);
9271 ufshcd_init_clk_scaling(hba);
9274 * In order to avoid any spurious interrupt immediately after
9275 * registering UFS controller interrupt handler, clear any pending UFS
9276 * interrupt status and disable all the UFS interrupts.
9278 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
9279 REG_INTERRUPT_STATUS);
9280 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
9282 * Make sure that UFS interrupts are disabled and any pending interrupt
9283 * status is cleared before registering UFS interrupt handler.
9287 /* IRQ registration */
9288 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
9290 dev_err(hba->dev, "request irq failed\n");
9293 hba->is_irq_enabled = true;
9296 err = scsi_add_host(host, hba->dev);
9298 dev_err(hba->dev, "scsi_add_host failed\n");
9302 hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set);
9303 if (IS_ERR(hba->cmd_queue)) {
9304 err = PTR_ERR(hba->cmd_queue);
9305 goto out_remove_scsi_host;
9308 hba->tmf_tag_set = (struct blk_mq_tag_set) {
9310 .queue_depth = hba->nutmrs,
9311 .ops = &ufshcd_tmf_ops,
9312 .flags = BLK_MQ_F_NO_SCHED,
9314 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
9316 goto free_cmd_queue;
9317 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set);
9318 if (IS_ERR(hba->tmf_queue)) {
9319 err = PTR_ERR(hba->tmf_queue);
9320 goto free_tmf_tag_set;
9323 /* Reset the attached device */
9324 ufshcd_vops_device_reset(hba);
9326 ufshcd_init_crypto(hba);
9328 /* Host controller enable */
9329 err = ufshcd_hba_enable(hba);
9331 dev_err(hba->dev, "Host controller enable failed\n");
9332 ufshcd_print_evt_hist(hba);
9333 ufshcd_print_host_state(hba);
9334 goto free_tmf_queue;
9338 * Set the default power management level for runtime and system PM.
9339 * Default power saving mode is to keep UFS link in Hibern8 state
9340 * and UFS device in sleep state.
9342 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9344 UIC_LINK_HIBERN8_STATE);
9345 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9347 UIC_LINK_HIBERN8_STATE);
9349 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work,
9350 ufshcd_rpm_dev_flush_recheck_work);
9352 /* Set the default auto-hiberate idle timer value to 150 ms */
9353 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
9354 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
9355 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
9358 /* Hold auto suspend until async scan completes */
9359 pm_runtime_get_sync(dev);
9360 atomic_set(&hba->scsi_block_reqs_cnt, 0);
9362 * We are assuming that device wasn't put in sleep/power-down
9363 * state exclusively during the boot stage before kernel.
9364 * This assumption helps avoid doing link startup twice during
9365 * ufshcd_probe_hba().
9367 ufshcd_set_ufs_dev_active(hba);
9369 async_schedule(ufshcd_async_scan, hba);
9370 ufs_sysfs_add_nodes(hba->dev);
9375 blk_cleanup_queue(hba->tmf_queue);
9377 blk_mq_free_tag_set(&hba->tmf_tag_set);
9379 blk_cleanup_queue(hba->cmd_queue);
9380 out_remove_scsi_host:
9381 scsi_remove_host(hba->host);
9383 ufshcd_exit_clk_scaling(hba);
9384 ufshcd_exit_clk_gating(hba);
9386 hba->is_irq_enabled = false;
9387 ufshcd_hba_exit(hba);
9391 EXPORT_SYMBOL_GPL(ufshcd_init);
9393 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
9394 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
9395 MODULE_DESCRIPTION("Generic UFS host controller driver Core");
9396 MODULE_LICENSE("GPL");
9397 MODULE_VERSION(UFSHCD_DRIVER_VERSION);