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"
23 #include "ufs-debugfs.h"
25 #include "ufshcd-crypto.h"
26 #include <asm/unaligned.h>
27 #include <linux/blkdev.h>
29 #define CREATE_TRACE_POINTS
30 #include <trace/events/ufs.h>
32 #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
35 /* UIC command timeout, unit: ms */
36 #define UIC_CMD_TIMEOUT 500
38 /* NOP OUT retries waiting for NOP IN response */
39 #define NOP_OUT_RETRIES 10
40 /* Timeout after 50 msecs if NOP OUT hangs without response */
41 #define NOP_OUT_TIMEOUT 50 /* msecs */
43 /* Query request retries */
44 #define QUERY_REQ_RETRIES 3
45 /* Query request timeout */
46 #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
48 /* Task management command timeout */
49 #define TM_CMD_TIMEOUT 100 /* msecs */
51 /* maximum number of retries for a general UIC command */
52 #define UFS_UIC_COMMAND_RETRIES 3
54 /* maximum number of link-startup retries */
55 #define DME_LINKSTARTUP_RETRIES 3
57 /* Maximum retries for Hibern8 enter */
58 #define UIC_HIBERN8_ENTER_RETRIES 3
60 /* maximum number of reset retries before giving up */
61 #define MAX_HOST_RESET_RETRIES 5
63 /* Expose the flag value from utp_upiu_query.value */
64 #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
66 /* Interrupt aggregation default timeout, unit: 40us */
67 #define INT_AGGR_DEF_TO 0x02
69 /* default delay of autosuspend: 2000 ms */
70 #define RPM_AUTOSUSPEND_DELAY_MS 2000
72 /* Default delay of RPM device flush delayed work */
73 #define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000
75 /* Default value of wait time before gating device ref clock */
76 #define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */
78 /* Polling time to wait for fDeviceInit */
79 #define FDEVICEINIT_COMPL_TIMEOUT 1500 /* millisecs */
81 #define ufshcd_toggle_vreg(_dev, _vreg, _on) \
85 _ret = ufshcd_enable_vreg(_dev, _vreg); \
87 _ret = ufshcd_disable_vreg(_dev, _vreg); \
91 #define ufshcd_hex_dump(prefix_str, buf, len) do { \
92 size_t __len = (len); \
93 print_hex_dump(KERN_ERR, prefix_str, \
94 __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\
95 16, 4, buf, __len, false); \
98 static bool early_suspend;
100 int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
106 if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */
109 regs = kzalloc(len, GFP_ATOMIC);
113 for (pos = 0; pos < len; pos += 4)
114 regs[pos / 4] = ufshcd_readl(hba, offset + pos);
116 ufshcd_hex_dump(prefix, regs, len);
121 EXPORT_SYMBOL_GPL(ufshcd_dump_regs);
124 UFSHCD_MAX_CHANNEL = 0,
126 UFSHCD_CMD_PER_LUN = 32,
127 UFSHCD_CAN_QUEUE = 32,
134 UFSHCD_STATE_OPERATIONAL,
135 UFSHCD_STATE_EH_SCHEDULED_FATAL,
136 UFSHCD_STATE_EH_SCHEDULED_NON_FATAL,
139 /* UFSHCD error handling flags */
141 UFSHCD_EH_IN_PROGRESS = (1 << 0),
144 /* UFSHCD UIC layer error flags */
146 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
147 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
148 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
149 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
150 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
151 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
152 UFSHCD_UIC_PA_GENERIC_ERROR = (1 << 6), /* Generic PA error */
155 #define ufshcd_set_eh_in_progress(h) \
156 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
157 #define ufshcd_eh_in_progress(h) \
158 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
159 #define ufshcd_clear_eh_in_progress(h) \
160 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
162 struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
163 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
164 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
165 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
166 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
167 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
168 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
170 * For DeepSleep, the link is first put in hibern8 and then off.
171 * Leaving the link in hibern8 is not supported.
173 {UFS_DEEPSLEEP_PWR_MODE, UIC_LINK_OFF_STATE},
176 static inline enum ufs_dev_pwr_mode
177 ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
179 return ufs_pm_lvl_states[lvl].dev_state;
182 static inline enum uic_link_state
183 ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
185 return ufs_pm_lvl_states[lvl].link_state;
188 static inline enum ufs_pm_level
189 ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
190 enum uic_link_state link_state)
192 enum ufs_pm_level lvl;
194 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
195 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
196 (ufs_pm_lvl_states[lvl].link_state == link_state))
200 /* if no match found, return the level 0 */
204 static struct ufs_dev_fix ufs_fixups[] = {
205 /* UFS cards deviations table */
206 UFS_FIX(UFS_VENDOR_MICRON, UFS_ANY_MODEL,
207 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
208 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
209 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM |
210 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE |
211 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
212 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
213 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
214 UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/,
215 UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME),
216 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
217 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
218 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
219 UFS_DEVICE_QUIRK_PA_TACTIVATE),
220 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
221 UFS_DEVICE_QUIRK_PA_TACTIVATE),
225 static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
226 static void ufshcd_async_scan(void *data, async_cookie_t cookie);
227 static int ufshcd_reset_and_restore(struct ufs_hba *hba);
228 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
229 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
230 static void ufshcd_hba_exit(struct ufs_hba *hba);
231 static int ufshcd_clear_ua_wluns(struct ufs_hba *hba);
232 static int ufshcd_probe_hba(struct ufs_hba *hba, bool async);
233 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
234 static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
235 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
236 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
237 static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
238 static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
239 static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
240 static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
241 static irqreturn_t ufshcd_intr(int irq, void *__hba);
242 static int ufshcd_change_power_mode(struct ufs_hba *hba,
243 struct ufs_pa_layer_attr *pwr_mode);
244 static void ufshcd_schedule_eh_work(struct ufs_hba *hba);
245 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on);
246 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on);
247 static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
248 struct ufs_vreg *vreg);
249 static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag);
250 static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set);
251 static inline int ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable);
252 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
253 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
255 static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
257 return tag >= 0 && tag < hba->nutrs;
260 static inline void ufshcd_enable_irq(struct ufs_hba *hba)
262 if (!hba->is_irq_enabled) {
263 enable_irq(hba->irq);
264 hba->is_irq_enabled = true;
268 static inline void ufshcd_disable_irq(struct ufs_hba *hba)
270 if (hba->is_irq_enabled) {
271 disable_irq(hba->irq);
272 hba->is_irq_enabled = false;
276 static inline void ufshcd_wb_config(struct ufs_hba *hba)
280 if (!ufshcd_is_wb_allowed(hba))
283 ret = ufshcd_wb_ctrl(hba, true);
285 dev_err(hba->dev, "%s: Enable WB failed: %d\n", __func__, ret);
287 dev_info(hba->dev, "%s: Write Booster Configured\n", __func__);
288 ret = ufshcd_wb_toggle_flush_during_h8(hba, true);
290 dev_err(hba->dev, "%s: En WB flush during H8: failed: %d\n",
292 if (!(hba->quirks & UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL))
293 ufshcd_wb_toggle_flush(hba, true);
296 static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
298 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt))
299 scsi_unblock_requests(hba->host);
302 static void ufshcd_scsi_block_requests(struct ufs_hba *hba)
304 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1)
305 scsi_block_requests(hba->host);
308 static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag,
309 enum ufs_trace_str_t str_t)
311 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
313 if (!trace_ufshcd_upiu_enabled())
316 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq->header, &rq->sc.cdb,
320 static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba,
321 enum ufs_trace_str_t str_t,
322 struct utp_upiu_req *rq_rsp)
324 if (!trace_ufshcd_upiu_enabled())
327 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq_rsp->header,
328 &rq_rsp->qr, UFS_TSF_OSF);
331 static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag,
332 enum ufs_trace_str_t str_t)
334 int off = (int)tag - hba->nutrs;
335 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[off];
337 if (!trace_ufshcd_upiu_enabled())
340 if (str_t == UFS_TM_SEND)
341 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &descp->req_header,
342 &descp->input_param1, UFS_TSF_TM_INPUT);
344 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &descp->rsp_header,
345 &descp->output_param1, UFS_TSF_TM_OUTPUT);
348 static void ufshcd_add_uic_command_trace(struct ufs_hba *hba,
349 struct uic_command *ucmd,
350 enum ufs_trace_str_t str_t)
354 if (!trace_ufshcd_uic_command_enabled())
357 if (str_t == UFS_CMD_SEND)
360 cmd = ufshcd_readl(hba, REG_UIC_COMMAND);
362 trace_ufshcd_uic_command(dev_name(hba->dev), str_t, cmd,
363 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1),
364 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2),
365 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3));
368 static void ufshcd_add_command_trace(struct ufs_hba *hba, unsigned int tag,
369 enum ufs_trace_str_t str_t)
372 u8 opcode = 0, group_id = 0;
374 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
375 struct scsi_cmnd *cmd = lrbp->cmd;
376 int transfer_len = -1;
378 if (!trace_ufshcd_command_enabled()) {
379 /* trace UPIU W/O tracing command */
381 ufshcd_add_cmd_upiu_trace(hba, tag, str_t);
385 if (cmd) { /* data phase exists */
386 /* trace UPIU also */
387 ufshcd_add_cmd_upiu_trace(hba, tag, str_t);
388 opcode = cmd->cmnd[0];
389 if ((opcode == READ_10) || (opcode == WRITE_10)) {
391 * Currently we only fully trace read(10) and write(10)
394 if (cmd->request && cmd->request->bio)
395 lba = cmd->request->bio->bi_iter.bi_sector;
396 transfer_len = be32_to_cpu(
397 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
398 if (opcode == WRITE_10)
399 group_id = lrbp->cmd->cmnd[6];
400 } else if (opcode == UNMAP) {
402 lba = scsi_get_lba(cmd);
403 transfer_len = blk_rq_bytes(cmd->request);
408 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
409 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
410 trace_ufshcd_command(dev_name(hba->dev), str_t, tag,
411 doorbell, transfer_len, intr, lba, opcode, group_id);
414 static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
416 struct ufs_clk_info *clki;
417 struct list_head *head = &hba->clk_list_head;
419 if (list_empty(head))
422 list_for_each_entry(clki, head, list) {
423 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
425 dev_err(hba->dev, "clk: %s, rate: %u\n",
426 clki->name, clki->curr_freq);
430 static void ufshcd_print_evt(struct ufs_hba *hba, u32 id,
435 struct ufs_event_hist *e;
437 if (id >= UFS_EVT_CNT)
440 e = &hba->ufs_stats.event[id];
442 for (i = 0; i < UFS_EVENT_HIST_LENGTH; i++) {
443 int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH;
445 if (e->tstamp[p] == 0)
447 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p,
448 e->val[p], ktime_to_us(e->tstamp[p]));
453 dev_err(hba->dev, "No record of %s\n", err_name);
455 dev_err(hba->dev, "%s: total cnt=%llu\n", err_name, e->cnt);
458 static void ufshcd_print_evt_hist(struct ufs_hba *hba)
460 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
462 ufshcd_print_evt(hba, UFS_EVT_PA_ERR, "pa_err");
463 ufshcd_print_evt(hba, UFS_EVT_DL_ERR, "dl_err");
464 ufshcd_print_evt(hba, UFS_EVT_NL_ERR, "nl_err");
465 ufshcd_print_evt(hba, UFS_EVT_TL_ERR, "tl_err");
466 ufshcd_print_evt(hba, UFS_EVT_DME_ERR, "dme_err");
467 ufshcd_print_evt(hba, UFS_EVT_AUTO_HIBERN8_ERR,
469 ufshcd_print_evt(hba, UFS_EVT_FATAL_ERR, "fatal_err");
470 ufshcd_print_evt(hba, UFS_EVT_LINK_STARTUP_FAIL,
471 "link_startup_fail");
472 ufshcd_print_evt(hba, UFS_EVT_RESUME_ERR, "resume_fail");
473 ufshcd_print_evt(hba, UFS_EVT_SUSPEND_ERR,
475 ufshcd_print_evt(hba, UFS_EVT_DEV_RESET, "dev_reset");
476 ufshcd_print_evt(hba, UFS_EVT_HOST_RESET, "host_reset");
477 ufshcd_print_evt(hba, UFS_EVT_ABORT, "task_abort");
479 ufshcd_vops_dbg_register_dump(hba);
483 void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
485 struct ufshcd_lrb *lrbp;
489 for_each_set_bit(tag, &bitmap, hba->nutrs) {
490 lrbp = &hba->lrb[tag];
492 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
493 tag, ktime_to_us(lrbp->issue_time_stamp));
494 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
495 tag, ktime_to_us(lrbp->compl_time_stamp));
497 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
498 tag, (u64)lrbp->utrd_dma_addr);
500 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
501 sizeof(struct utp_transfer_req_desc));
502 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
503 (u64)lrbp->ucd_req_dma_addr);
504 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
505 sizeof(struct utp_upiu_req));
506 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
507 (u64)lrbp->ucd_rsp_dma_addr);
508 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
509 sizeof(struct utp_upiu_rsp));
511 prdt_length = le16_to_cpu(
512 lrbp->utr_descriptor_ptr->prd_table_length);
513 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
514 prdt_length /= sizeof(struct ufshcd_sg_entry);
517 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
519 (u64)lrbp->ucd_prdt_dma_addr);
522 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
523 sizeof(struct ufshcd_sg_entry) * prdt_length);
527 static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
531 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
532 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag];
534 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
535 ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp));
539 static void ufshcd_print_host_state(struct ufs_hba *hba)
541 struct scsi_device *sdev_ufs = hba->sdev_ufs_device;
543 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
544 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n",
545 hba->outstanding_reqs, hba->outstanding_tasks);
546 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
547 hba->saved_err, hba->saved_uic_err);
548 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
549 hba->curr_dev_pwr_mode, hba->uic_link_state);
550 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
551 hba->pm_op_in_progress, hba->is_sys_suspended);
552 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
553 hba->auto_bkops_enabled, hba->host->host_self_blocked);
554 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
556 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt=%d\n",
557 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
558 hba->ufs_stats.hibern8_exit_cnt);
559 dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n",
560 ktime_to_us(hba->ufs_stats.last_intr_ts),
561 hba->ufs_stats.last_intr_status);
562 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
563 hba->eh_flags, hba->req_abort_count);
564 dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n",
565 hba->ufs_version, hba->capabilities, hba->caps);
566 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
569 dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n",
570 sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev);
572 ufshcd_print_clk_freqs(hba);
576 * ufshcd_print_pwr_info - print power params as saved in hba
578 * @hba: per-adapter instance
580 static void ufshcd_print_pwr_info(struct ufs_hba *hba)
582 static const char * const names[] = {
592 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
594 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
595 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
596 names[hba->pwr_info.pwr_rx],
597 names[hba->pwr_info.pwr_tx],
598 hba->pwr_info.hs_rate);
601 static void ufshcd_device_reset(struct ufs_hba *hba)
605 err = ufshcd_vops_device_reset(hba);
608 ufshcd_set_ufs_dev_active(hba);
609 if (ufshcd_is_wb_allowed(hba)) {
610 hba->dev_info.wb_enabled = false;
611 hba->dev_info.wb_buf_flush_enabled = false;
614 if (err != -EOPNOTSUPP)
615 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err);
618 void ufshcd_delay_us(unsigned long us, unsigned long tolerance)
626 usleep_range(us, us + tolerance);
628 EXPORT_SYMBOL_GPL(ufshcd_delay_us);
631 * ufshcd_wait_for_register - wait for register value to change
632 * @hba: per-adapter interface
633 * @reg: mmio register offset
634 * @mask: mask to apply to the read register value
635 * @val: value to wait for
636 * @interval_us: polling interval in microseconds
637 * @timeout_ms: timeout in milliseconds
640 * -ETIMEDOUT on error, zero on success.
642 int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
643 u32 val, unsigned long interval_us,
644 unsigned long timeout_ms)
647 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
649 /* ignore bits that we don't intend to wait on */
652 while ((ufshcd_readl(hba, reg) & mask) != val) {
653 usleep_range(interval_us, interval_us + 50);
654 if (time_after(jiffies, timeout)) {
655 if ((ufshcd_readl(hba, reg) & mask) != val)
665 * ufshcd_get_intr_mask - Get the interrupt bit mask
666 * @hba: Pointer to adapter instance
668 * Returns interrupt bit mask per version
670 static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
674 switch (hba->ufs_version) {
675 case UFSHCI_VERSION_10:
676 intr_mask = INTERRUPT_MASK_ALL_VER_10;
678 case UFSHCI_VERSION_11:
679 case UFSHCI_VERSION_20:
680 intr_mask = INTERRUPT_MASK_ALL_VER_11;
682 case UFSHCI_VERSION_21:
684 intr_mask = INTERRUPT_MASK_ALL_VER_21;
692 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
693 * @hba: Pointer to adapter instance
695 * Returns UFSHCI version supported by the controller
697 static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
699 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
700 return ufshcd_vops_get_ufs_hci_version(hba);
702 return ufshcd_readl(hba, REG_UFS_VERSION);
706 * ufshcd_is_device_present - Check if any device connected to
707 * the host controller
708 * @hba: pointer to adapter instance
710 * Returns true if device present, false if no device detected
712 static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
714 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
715 DEVICE_PRESENT) ? true : false;
719 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
720 * @lrbp: pointer to local command reference block
722 * This function is used to get the OCS field from UTRD
723 * Returns the OCS field in the UTRD
725 static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
727 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
731 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
732 * @hba: per adapter instance
733 * @pos: position of the bit to be cleared
735 static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
737 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
738 ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
740 ufshcd_writel(hba, ~(1 << pos),
741 REG_UTP_TRANSFER_REQ_LIST_CLEAR);
745 * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register
746 * @hba: per adapter instance
747 * @pos: position of the bit to be cleared
749 static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
751 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
752 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
754 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
758 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
759 * @hba: per adapter instance
760 * @tag: position of the bit to be cleared
762 static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
764 __clear_bit(tag, &hba->outstanding_reqs);
768 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
769 * @reg: Register value of host controller status
771 * Returns integer, 0 on Success and positive value if failed
773 static inline int ufshcd_get_lists_status(u32 reg)
775 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
779 * ufshcd_get_uic_cmd_result - Get the UIC command result
780 * @hba: Pointer to adapter instance
782 * This function gets the result of UIC command completion
783 * Returns 0 on success, non zero value on error
785 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
787 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
788 MASK_UIC_COMMAND_RESULT;
792 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
793 * @hba: Pointer to adapter instance
795 * This function gets UIC command argument3
796 * Returns 0 on success, non zero value on error
798 static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
800 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
804 * ufshcd_get_req_rsp - returns the TR response transaction type
805 * @ucd_rsp_ptr: pointer to response UPIU
808 ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
810 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
814 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
815 * @ucd_rsp_ptr: pointer to response UPIU
817 * This function gets the response status and scsi_status from response UPIU
818 * Returns the response result code.
821 ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
823 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
827 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
829 * @ucd_rsp_ptr: pointer to response UPIU
831 * Return the data segment length.
833 static inline unsigned int
834 ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
836 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
837 MASK_RSP_UPIU_DATA_SEG_LEN;
841 * ufshcd_is_exception_event - Check if the device raised an exception event
842 * @ucd_rsp_ptr: pointer to response UPIU
844 * The function checks if the device raised an exception event indicated in
845 * the Device Information field of response UPIU.
847 * Returns true if exception is raised, false otherwise.
849 static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
851 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
852 MASK_RSP_EXCEPTION_EVENT ? true : false;
856 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
857 * @hba: per adapter instance
860 ufshcd_reset_intr_aggr(struct ufs_hba *hba)
862 ufshcd_writel(hba, INT_AGGR_ENABLE |
863 INT_AGGR_COUNTER_AND_TIMER_RESET,
864 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
868 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
869 * @hba: per adapter instance
870 * @cnt: Interrupt aggregation counter threshold
871 * @tmout: Interrupt aggregation timeout value
874 ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
876 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
877 INT_AGGR_COUNTER_THLD_VAL(cnt) |
878 INT_AGGR_TIMEOUT_VAL(tmout),
879 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
883 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
884 * @hba: per adapter instance
886 static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
888 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
892 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
893 * When run-stop registers are set to 1, it indicates the
894 * host controller that it can process the requests
895 * @hba: per adapter instance
897 static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
899 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
900 REG_UTP_TASK_REQ_LIST_RUN_STOP);
901 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
902 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
906 * ufshcd_hba_start - Start controller initialization sequence
907 * @hba: per adapter instance
909 static inline void ufshcd_hba_start(struct ufs_hba *hba)
911 u32 val = CONTROLLER_ENABLE;
913 if (ufshcd_crypto_enable(hba))
914 val |= CRYPTO_GENERAL_ENABLE;
916 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
920 * ufshcd_is_hba_active - Get controller state
921 * @hba: per adapter instance
923 * Returns false if controller is active, true otherwise
925 static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
927 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
931 u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
933 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
934 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
935 (hba->ufs_version == UFSHCI_VERSION_11))
936 return UFS_UNIPRO_VER_1_41;
938 return UFS_UNIPRO_VER_1_6;
940 EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
942 static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
945 * If both host and device support UniPro ver1.6 or later, PA layer
946 * parameters tuning happens during link startup itself.
948 * We can manually tune PA layer parameters if either host or device
949 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
950 * logic simple, we will only do manual tuning if local unipro version
951 * doesn't support ver1.6 or later.
953 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
960 * ufshcd_set_clk_freq - set UFS controller clock frequencies
961 * @hba: per adapter instance
962 * @scale_up: If True, set max possible frequency othewise set low frequency
964 * Returns 0 if successful
965 * Returns < 0 for any other errors
967 static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
970 struct ufs_clk_info *clki;
971 struct list_head *head = &hba->clk_list_head;
973 if (list_empty(head))
976 list_for_each_entry(clki, head, list) {
977 if (!IS_ERR_OR_NULL(clki->clk)) {
978 if (scale_up && clki->max_freq) {
979 if (clki->curr_freq == clki->max_freq)
982 ret = clk_set_rate(clki->clk, clki->max_freq);
984 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
985 __func__, clki->name,
986 clki->max_freq, ret);
989 trace_ufshcd_clk_scaling(dev_name(hba->dev),
990 "scaled up", clki->name,
994 clki->curr_freq = clki->max_freq;
996 } else if (!scale_up && clki->min_freq) {
997 if (clki->curr_freq == clki->min_freq)
1000 ret = clk_set_rate(clki->clk, clki->min_freq);
1002 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
1003 __func__, clki->name,
1004 clki->min_freq, ret);
1007 trace_ufshcd_clk_scaling(dev_name(hba->dev),
1008 "scaled down", clki->name,
1011 clki->curr_freq = clki->min_freq;
1014 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
1015 clki->name, clk_get_rate(clki->clk));
1023 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
1024 * @hba: per adapter instance
1025 * @scale_up: True if scaling up and false if scaling down
1027 * Returns 0 if successful
1028 * Returns < 0 for any other errors
1030 static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
1033 ktime_t start = ktime_get();
1035 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
1039 ret = ufshcd_set_clk_freq(hba, scale_up);
1043 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1045 ufshcd_set_clk_freq(hba, !scale_up);
1048 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1049 (scale_up ? "up" : "down"),
1050 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1055 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
1056 * @hba: per adapter instance
1057 * @scale_up: True if scaling up and false if scaling down
1059 * Returns true if scaling is required, false otherwise.
1061 static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
1064 struct ufs_clk_info *clki;
1065 struct list_head *head = &hba->clk_list_head;
1067 if (list_empty(head))
1070 list_for_each_entry(clki, head, list) {
1071 if (!IS_ERR_OR_NULL(clki->clk)) {
1072 if (scale_up && clki->max_freq) {
1073 if (clki->curr_freq == clki->max_freq)
1076 } else if (!scale_up && clki->min_freq) {
1077 if (clki->curr_freq == clki->min_freq)
1087 static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
1088 u64 wait_timeout_us)
1090 unsigned long flags;
1094 bool timeout = false, do_last_check = false;
1097 ufshcd_hold(hba, false);
1098 spin_lock_irqsave(hba->host->host_lock, flags);
1100 * Wait for all the outstanding tasks/transfer requests.
1101 * Verify by checking the doorbell registers are clear.
1103 start = ktime_get();
1105 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
1110 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
1111 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1112 if (!tm_doorbell && !tr_doorbell) {
1115 } else if (do_last_check) {
1119 spin_unlock_irqrestore(hba->host->host_lock, flags);
1121 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
1125 * We might have scheduled out for long time so make
1126 * sure to check if doorbells are cleared by this time
1129 do_last_check = true;
1131 spin_lock_irqsave(hba->host->host_lock, flags);
1132 } while (tm_doorbell || tr_doorbell);
1136 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1137 __func__, tm_doorbell, tr_doorbell);
1141 spin_unlock_irqrestore(hba->host->host_lock, flags);
1142 ufshcd_release(hba);
1147 * ufshcd_scale_gear - scale up/down UFS gear
1148 * @hba: per adapter instance
1149 * @scale_up: True for scaling up gear and false for scaling down
1151 * Returns 0 for success,
1152 * Returns -EBUSY if scaling can't happen at this time
1153 * Returns non-zero for any other errors
1155 static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1158 struct ufs_pa_layer_attr new_pwr_info;
1161 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1162 sizeof(struct ufs_pa_layer_attr));
1164 memcpy(&new_pwr_info, &hba->pwr_info,
1165 sizeof(struct ufs_pa_layer_attr));
1167 if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear ||
1168 hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) {
1169 /* save the current power mode */
1170 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1172 sizeof(struct ufs_pa_layer_attr));
1174 /* scale down gear */
1175 new_pwr_info.gear_tx = hba->clk_scaling.min_gear;
1176 new_pwr_info.gear_rx = hba->clk_scaling.min_gear;
1180 /* check if the power mode needs to be changed or not? */
1181 ret = ufshcd_config_pwr_mode(hba, &new_pwr_info);
1183 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1185 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1186 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1191 static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1193 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1196 * make sure that there are no outstanding requests when
1197 * clock scaling is in progress
1199 ufshcd_scsi_block_requests(hba);
1200 down_write(&hba->clk_scaling_lock);
1202 if (!hba->clk_scaling.is_allowed ||
1203 ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
1205 up_write(&hba->clk_scaling_lock);
1206 ufshcd_scsi_unblock_requests(hba);
1210 /* let's not get into low power until clock scaling is completed */
1211 ufshcd_hold(hba, false);
1217 static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, bool writelock)
1220 up_write(&hba->clk_scaling_lock);
1222 up_read(&hba->clk_scaling_lock);
1223 ufshcd_scsi_unblock_requests(hba);
1224 ufshcd_release(hba);
1228 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1229 * @hba: per adapter instance
1230 * @scale_up: True for scaling up and false for scalin down
1232 * Returns 0 for success,
1233 * Returns -EBUSY if scaling can't happen at this time
1234 * Returns non-zero for any other errors
1236 static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1239 bool is_writelock = true;
1241 ret = ufshcd_clock_scaling_prepare(hba);
1245 /* scale down the gear before scaling down clocks */
1247 ret = ufshcd_scale_gear(hba, false);
1252 ret = ufshcd_scale_clks(hba, scale_up);
1255 ufshcd_scale_gear(hba, true);
1259 /* scale up the gear after scaling up clocks */
1261 ret = ufshcd_scale_gear(hba, true);
1263 ufshcd_scale_clks(hba, false);
1268 /* Enable Write Booster if we have scaled up else disable it */
1269 downgrade_write(&hba->clk_scaling_lock);
1270 is_writelock = false;
1271 ufshcd_wb_ctrl(hba, scale_up);
1274 ufshcd_clock_scaling_unprepare(hba, is_writelock);
1278 static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1280 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1281 clk_scaling.suspend_work);
1282 unsigned long irq_flags;
1284 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1285 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1286 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1289 hba->clk_scaling.is_suspended = true;
1290 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1292 __ufshcd_suspend_clkscaling(hba);
1295 static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1297 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1298 clk_scaling.resume_work);
1299 unsigned long irq_flags;
1301 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1302 if (!hba->clk_scaling.is_suspended) {
1303 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1306 hba->clk_scaling.is_suspended = false;
1307 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1309 devfreq_resume_device(hba->devfreq);
1312 static int ufshcd_devfreq_target(struct device *dev,
1313 unsigned long *freq, u32 flags)
1316 struct ufs_hba *hba = dev_get_drvdata(dev);
1318 bool scale_up, sched_clk_scaling_suspend_work = false;
1319 struct list_head *clk_list = &hba->clk_list_head;
1320 struct ufs_clk_info *clki;
1321 unsigned long irq_flags;
1323 if (!ufshcd_is_clkscaling_supported(hba))
1326 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list);
1327 /* Override with the closest supported frequency */
1328 *freq = (unsigned long) clk_round_rate(clki->clk, *freq);
1329 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1330 if (ufshcd_eh_in_progress(hba)) {
1331 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1335 if (!hba->clk_scaling.active_reqs)
1336 sched_clk_scaling_suspend_work = true;
1338 if (list_empty(clk_list)) {
1339 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1343 /* Decide based on the rounded-off frequency and update */
1344 scale_up = (*freq == clki->max_freq) ? true : false;
1346 *freq = clki->min_freq;
1347 /* Update the frequency */
1348 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1349 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1351 goto out; /* no state change required */
1353 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1355 start = ktime_get();
1356 ret = ufshcd_devfreq_scale(hba, scale_up);
1358 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1359 (scale_up ? "up" : "down"),
1360 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1363 if (sched_clk_scaling_suspend_work)
1364 queue_work(hba->clk_scaling.workq,
1365 &hba->clk_scaling.suspend_work);
1370 static bool ufshcd_is_busy(struct request *req, void *priv, bool reserved)
1374 WARN_ON_ONCE(reserved);
1379 /* Whether or not any tag is in use by a request that is in progress. */
1380 static bool ufshcd_any_tag_in_use(struct ufs_hba *hba)
1382 struct request_queue *q = hba->cmd_queue;
1385 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_is_busy, &busy);
1389 static int ufshcd_devfreq_get_dev_status(struct device *dev,
1390 struct devfreq_dev_status *stat)
1392 struct ufs_hba *hba = dev_get_drvdata(dev);
1393 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1394 unsigned long flags;
1395 struct list_head *clk_list = &hba->clk_list_head;
1396 struct ufs_clk_info *clki;
1399 if (!ufshcd_is_clkscaling_supported(hba))
1402 memset(stat, 0, sizeof(*stat));
1404 spin_lock_irqsave(hba->host->host_lock, flags);
1405 curr_t = ktime_get();
1406 if (!scaling->window_start_t)
1409 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1411 * If current frequency is 0, then the ondemand governor considers
1412 * there's no initial frequency set. And it always requests to set
1413 * to max. frequency.
1415 stat->current_frequency = clki->curr_freq;
1416 if (scaling->is_busy_started)
1417 scaling->tot_busy_t += ktime_us_delta(curr_t,
1418 scaling->busy_start_t);
1420 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t);
1421 stat->busy_time = scaling->tot_busy_t;
1423 scaling->window_start_t = curr_t;
1424 scaling->tot_busy_t = 0;
1426 if (hba->outstanding_reqs) {
1427 scaling->busy_start_t = curr_t;
1428 scaling->is_busy_started = true;
1430 scaling->busy_start_t = 0;
1431 scaling->is_busy_started = false;
1433 spin_unlock_irqrestore(hba->host->host_lock, flags);
1437 static int ufshcd_devfreq_init(struct ufs_hba *hba)
1439 struct list_head *clk_list = &hba->clk_list_head;
1440 struct ufs_clk_info *clki;
1441 struct devfreq *devfreq;
1444 /* Skip devfreq if we don't have any clocks in the list */
1445 if (list_empty(clk_list))
1448 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1449 dev_pm_opp_add(hba->dev, clki->min_freq, 0);
1450 dev_pm_opp_add(hba->dev, clki->max_freq, 0);
1452 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile,
1453 &hba->vps->ondemand_data);
1454 devfreq = devfreq_add_device(hba->dev,
1455 &hba->vps->devfreq_profile,
1456 DEVFREQ_GOV_SIMPLE_ONDEMAND,
1457 &hba->vps->ondemand_data);
1458 if (IS_ERR(devfreq)) {
1459 ret = PTR_ERR(devfreq);
1460 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret);
1462 dev_pm_opp_remove(hba->dev, clki->min_freq);
1463 dev_pm_opp_remove(hba->dev, clki->max_freq);
1467 hba->devfreq = devfreq;
1472 static void ufshcd_devfreq_remove(struct ufs_hba *hba)
1474 struct list_head *clk_list = &hba->clk_list_head;
1475 struct ufs_clk_info *clki;
1480 devfreq_remove_device(hba->devfreq);
1481 hba->devfreq = NULL;
1483 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1484 dev_pm_opp_remove(hba->dev, clki->min_freq);
1485 dev_pm_opp_remove(hba->dev, clki->max_freq);
1488 static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1490 unsigned long flags;
1492 devfreq_suspend_device(hba->devfreq);
1493 spin_lock_irqsave(hba->host->host_lock, flags);
1494 hba->clk_scaling.window_start_t = 0;
1495 spin_unlock_irqrestore(hba->host->host_lock, flags);
1498 static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1500 unsigned long flags;
1501 bool suspend = false;
1503 cancel_work_sync(&hba->clk_scaling.suspend_work);
1504 cancel_work_sync(&hba->clk_scaling.resume_work);
1506 spin_lock_irqsave(hba->host->host_lock, flags);
1507 if (!hba->clk_scaling.is_suspended) {
1509 hba->clk_scaling.is_suspended = true;
1511 spin_unlock_irqrestore(hba->host->host_lock, flags);
1514 __ufshcd_suspend_clkscaling(hba);
1517 static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1519 unsigned long flags;
1520 bool resume = false;
1522 spin_lock_irqsave(hba->host->host_lock, flags);
1523 if (hba->clk_scaling.is_suspended) {
1525 hba->clk_scaling.is_suspended = false;
1527 spin_unlock_irqrestore(hba->host->host_lock, flags);
1530 devfreq_resume_device(hba->devfreq);
1533 static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1534 struct device_attribute *attr, char *buf)
1536 struct ufs_hba *hba = dev_get_drvdata(dev);
1538 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_enabled);
1541 static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1542 struct device_attribute *attr, const char *buf, size_t count)
1544 struct ufs_hba *hba = dev_get_drvdata(dev);
1548 if (kstrtou32(buf, 0, &value))
1551 down(&hba->host_sem);
1552 if (!ufshcd_is_user_access_allowed(hba)) {
1558 if (value == hba->clk_scaling.is_enabled)
1561 pm_runtime_get_sync(hba->dev);
1562 ufshcd_hold(hba, false);
1564 hba->clk_scaling.is_enabled = value;
1567 ufshcd_resume_clkscaling(hba);
1569 ufshcd_suspend_clkscaling(hba);
1570 err = ufshcd_devfreq_scale(hba, true);
1572 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1576 ufshcd_release(hba);
1577 pm_runtime_put_sync(hba->dev);
1580 return err ? err : count;
1583 static void ufshcd_init_clk_scaling_sysfs(struct ufs_hba *hba)
1585 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1586 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1587 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1588 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1589 hba->clk_scaling.enable_attr.attr.mode = 0644;
1590 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1591 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1594 static void ufshcd_remove_clk_scaling_sysfs(struct ufs_hba *hba)
1596 if (hba->clk_scaling.enable_attr.attr.name)
1597 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
1600 static void ufshcd_init_clk_scaling(struct ufs_hba *hba)
1602 char wq_name[sizeof("ufs_clkscaling_00")];
1604 if (!ufshcd_is_clkscaling_supported(hba))
1607 if (!hba->clk_scaling.min_gear)
1608 hba->clk_scaling.min_gear = UFS_HS_G1;
1610 INIT_WORK(&hba->clk_scaling.suspend_work,
1611 ufshcd_clk_scaling_suspend_work);
1612 INIT_WORK(&hba->clk_scaling.resume_work,
1613 ufshcd_clk_scaling_resume_work);
1615 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d",
1616 hba->host->host_no);
1617 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
1619 hba->clk_scaling.is_initialized = true;
1622 static void ufshcd_exit_clk_scaling(struct ufs_hba *hba)
1624 if (!hba->clk_scaling.is_initialized)
1627 ufshcd_remove_clk_scaling_sysfs(hba);
1628 destroy_workqueue(hba->clk_scaling.workq);
1629 ufshcd_devfreq_remove(hba);
1630 hba->clk_scaling.is_initialized = false;
1633 static void ufshcd_ungate_work(struct work_struct *work)
1636 unsigned long flags;
1637 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1638 clk_gating.ungate_work);
1640 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1642 spin_lock_irqsave(hba->host->host_lock, flags);
1643 if (hba->clk_gating.state == CLKS_ON) {
1644 spin_unlock_irqrestore(hba->host->host_lock, flags);
1648 spin_unlock_irqrestore(hba->host->host_lock, flags);
1649 ufshcd_hba_vreg_set_hpm(hba);
1650 ufshcd_setup_clocks(hba, true);
1652 ufshcd_enable_irq(hba);
1654 /* Exit from hibern8 */
1655 if (ufshcd_can_hibern8_during_gating(hba)) {
1656 /* Prevent gating in this path */
1657 hba->clk_gating.is_suspended = true;
1658 if (ufshcd_is_link_hibern8(hba)) {
1659 ret = ufshcd_uic_hibern8_exit(hba);
1661 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1664 ufshcd_set_link_active(hba);
1666 hba->clk_gating.is_suspended = false;
1669 ufshcd_scsi_unblock_requests(hba);
1673 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1674 * Also, exit from hibern8 mode and set the link as active.
1675 * @hba: per adapter instance
1676 * @async: This indicates whether caller should ungate clocks asynchronously.
1678 int ufshcd_hold(struct ufs_hba *hba, bool async)
1682 unsigned long flags;
1684 if (!ufshcd_is_clkgating_allowed(hba))
1686 spin_lock_irqsave(hba->host->host_lock, flags);
1687 hba->clk_gating.active_reqs++;
1690 switch (hba->clk_gating.state) {
1693 * Wait for the ungate work to complete if in progress.
1694 * Though the clocks may be in ON state, the link could
1695 * still be in hibner8 state if hibern8 is allowed
1696 * during clock gating.
1697 * Make sure we exit hibern8 state also in addition to
1700 if (ufshcd_can_hibern8_during_gating(hba) &&
1701 ufshcd_is_link_hibern8(hba)) {
1704 hba->clk_gating.active_reqs--;
1707 spin_unlock_irqrestore(hba->host->host_lock, flags);
1708 flush_result = flush_work(&hba->clk_gating.ungate_work);
1709 if (hba->clk_gating.is_suspended && !flush_result)
1711 spin_lock_irqsave(hba->host->host_lock, flags);
1716 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1717 hba->clk_gating.state = CLKS_ON;
1718 trace_ufshcd_clk_gating(dev_name(hba->dev),
1719 hba->clk_gating.state);
1723 * If we are here, it means gating work is either done or
1724 * currently running. Hence, fall through to cancel gating
1725 * work and to enable clocks.
1729 hba->clk_gating.state = REQ_CLKS_ON;
1730 trace_ufshcd_clk_gating(dev_name(hba->dev),
1731 hba->clk_gating.state);
1732 if (queue_work(hba->clk_gating.clk_gating_workq,
1733 &hba->clk_gating.ungate_work))
1734 ufshcd_scsi_block_requests(hba);
1736 * fall through to check if we should wait for this
1737 * work to be done or not.
1743 hba->clk_gating.active_reqs--;
1747 spin_unlock_irqrestore(hba->host->host_lock, flags);
1748 flush_work(&hba->clk_gating.ungate_work);
1749 /* Make sure state is CLKS_ON before returning */
1750 spin_lock_irqsave(hba->host->host_lock, flags);
1753 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1754 __func__, hba->clk_gating.state);
1757 spin_unlock_irqrestore(hba->host->host_lock, flags);
1761 EXPORT_SYMBOL_GPL(ufshcd_hold);
1763 static void ufshcd_gate_work(struct work_struct *work)
1765 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1766 clk_gating.gate_work.work);
1767 unsigned long flags;
1770 spin_lock_irqsave(hba->host->host_lock, flags);
1772 * In case you are here to cancel this work the gating state
1773 * would be marked as REQ_CLKS_ON. In this case save time by
1774 * skipping the gating work and exit after changing the clock
1777 if (hba->clk_gating.is_suspended ||
1778 (hba->clk_gating.state != REQ_CLKS_OFF)) {
1779 hba->clk_gating.state = CLKS_ON;
1780 trace_ufshcd_clk_gating(dev_name(hba->dev),
1781 hba->clk_gating.state);
1785 if (hba->clk_gating.active_reqs
1786 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1787 || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks
1788 || hba->active_uic_cmd || hba->uic_async_done)
1791 spin_unlock_irqrestore(hba->host->host_lock, flags);
1793 /* put the link into hibern8 mode before turning off clocks */
1794 if (ufshcd_can_hibern8_during_gating(hba)) {
1795 ret = ufshcd_uic_hibern8_enter(hba);
1797 hba->clk_gating.state = CLKS_ON;
1798 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
1800 trace_ufshcd_clk_gating(dev_name(hba->dev),
1801 hba->clk_gating.state);
1804 ufshcd_set_link_hibern8(hba);
1807 ufshcd_disable_irq(hba);
1809 ufshcd_setup_clocks(hba, false);
1811 /* Put the host controller in low power mode if possible */
1812 ufshcd_hba_vreg_set_lpm(hba);
1814 * In case you are here to cancel this work the gating state
1815 * would be marked as REQ_CLKS_ON. In this case keep the state
1816 * as REQ_CLKS_ON which would anyway imply that clocks are off
1817 * and a request to turn them on is pending. By doing this way,
1818 * we keep the state machine in tact and this would ultimately
1819 * prevent from doing cancel work multiple times when there are
1820 * new requests arriving before the current cancel work is done.
1822 spin_lock_irqsave(hba->host->host_lock, flags);
1823 if (hba->clk_gating.state == REQ_CLKS_OFF) {
1824 hba->clk_gating.state = CLKS_OFF;
1825 trace_ufshcd_clk_gating(dev_name(hba->dev),
1826 hba->clk_gating.state);
1829 spin_unlock_irqrestore(hba->host->host_lock, flags);
1834 /* host lock must be held before calling this variant */
1835 static void __ufshcd_release(struct ufs_hba *hba)
1837 if (!ufshcd_is_clkgating_allowed(hba))
1840 hba->clk_gating.active_reqs--;
1842 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended ||
1843 hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL ||
1844 hba->outstanding_tasks ||
1845 hba->active_uic_cmd || hba->uic_async_done ||
1846 hba->clk_gating.state == CLKS_OFF)
1849 hba->clk_gating.state = REQ_CLKS_OFF;
1850 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
1851 queue_delayed_work(hba->clk_gating.clk_gating_workq,
1852 &hba->clk_gating.gate_work,
1853 msecs_to_jiffies(hba->clk_gating.delay_ms));
1856 void ufshcd_release(struct ufs_hba *hba)
1858 unsigned long flags;
1860 spin_lock_irqsave(hba->host->host_lock, flags);
1861 __ufshcd_release(hba);
1862 spin_unlock_irqrestore(hba->host->host_lock, flags);
1864 EXPORT_SYMBOL_GPL(ufshcd_release);
1866 static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1867 struct device_attribute *attr, char *buf)
1869 struct ufs_hba *hba = dev_get_drvdata(dev);
1871 return sysfs_emit(buf, "%lu\n", hba->clk_gating.delay_ms);
1874 static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1875 struct device_attribute *attr, const char *buf, size_t count)
1877 struct ufs_hba *hba = dev_get_drvdata(dev);
1878 unsigned long flags, value;
1880 if (kstrtoul(buf, 0, &value))
1883 spin_lock_irqsave(hba->host->host_lock, flags);
1884 hba->clk_gating.delay_ms = value;
1885 spin_unlock_irqrestore(hba->host->host_lock, flags);
1889 static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1890 struct device_attribute *attr, char *buf)
1892 struct ufs_hba *hba = dev_get_drvdata(dev);
1894 return sysfs_emit(buf, "%d\n", hba->clk_gating.is_enabled);
1897 static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1898 struct device_attribute *attr, const char *buf, size_t count)
1900 struct ufs_hba *hba = dev_get_drvdata(dev);
1901 unsigned long flags;
1904 if (kstrtou32(buf, 0, &value))
1909 spin_lock_irqsave(hba->host->host_lock, flags);
1910 if (value == hba->clk_gating.is_enabled)
1914 __ufshcd_release(hba);
1916 hba->clk_gating.active_reqs++;
1918 hba->clk_gating.is_enabled = value;
1920 spin_unlock_irqrestore(hba->host->host_lock, flags);
1924 static void ufshcd_init_clk_gating_sysfs(struct ufs_hba *hba)
1926 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1927 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1928 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1929 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
1930 hba->clk_gating.delay_attr.attr.mode = 0644;
1931 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1932 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
1934 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1935 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1936 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1937 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1938 hba->clk_gating.enable_attr.attr.mode = 0644;
1939 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1940 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
1943 static void ufshcd_remove_clk_gating_sysfs(struct ufs_hba *hba)
1945 if (hba->clk_gating.delay_attr.attr.name)
1946 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
1947 if (hba->clk_gating.enable_attr.attr.name)
1948 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
1951 static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1953 char wq_name[sizeof("ufs_clk_gating_00")];
1955 if (!ufshcd_is_clkgating_allowed(hba))
1958 hba->clk_gating.state = CLKS_ON;
1960 hba->clk_gating.delay_ms = 150;
1961 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1962 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1964 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
1965 hba->host->host_no);
1966 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
1967 WQ_MEM_RECLAIM | WQ_HIGHPRI);
1969 ufshcd_init_clk_gating_sysfs(hba);
1971 hba->clk_gating.is_enabled = true;
1972 hba->clk_gating.is_initialized = true;
1975 static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1977 if (!hba->clk_gating.is_initialized)
1979 ufshcd_remove_clk_gating_sysfs(hba);
1980 cancel_work_sync(&hba->clk_gating.ungate_work);
1981 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1982 destroy_workqueue(hba->clk_gating.clk_gating_workq);
1983 hba->clk_gating.is_initialized = false;
1986 /* Must be called with host lock acquired */
1987 static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1989 bool queue_resume_work = false;
1990 ktime_t curr_t = ktime_get();
1992 if (!ufshcd_is_clkscaling_supported(hba))
1995 if (!hba->clk_scaling.active_reqs++)
1996 queue_resume_work = true;
1998 if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress)
2001 if (queue_resume_work)
2002 queue_work(hba->clk_scaling.workq,
2003 &hba->clk_scaling.resume_work);
2005 if (!hba->clk_scaling.window_start_t) {
2006 hba->clk_scaling.window_start_t = curr_t;
2007 hba->clk_scaling.tot_busy_t = 0;
2008 hba->clk_scaling.is_busy_started = false;
2011 if (!hba->clk_scaling.is_busy_started) {
2012 hba->clk_scaling.busy_start_t = curr_t;
2013 hba->clk_scaling.is_busy_started = true;
2017 static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
2019 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
2021 if (!ufshcd_is_clkscaling_supported(hba))
2024 if (!hba->outstanding_reqs && scaling->is_busy_started) {
2025 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
2026 scaling->busy_start_t));
2027 scaling->busy_start_t = 0;
2028 scaling->is_busy_started = false;
2032 * ufshcd_send_command - Send SCSI or device management commands
2033 * @hba: per adapter instance
2034 * @task_tag: Task tag of the command
2037 void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
2039 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag];
2041 lrbp->issue_time_stamp = ktime_get();
2042 lrbp->compl_time_stamp = ktime_set(0, 0);
2043 ufshcd_vops_setup_xfer_req(hba, task_tag, (lrbp->cmd ? true : false));
2044 ufshcd_add_command_trace(hba, task_tag, UFS_CMD_SEND);
2045 ufshcd_clk_scaling_start_busy(hba);
2046 __set_bit(task_tag, &hba->outstanding_reqs);
2047 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
2048 /* Make sure that doorbell is committed immediately */
2053 * ufshcd_copy_sense_data - Copy sense data in case of check condition
2054 * @lrbp: pointer to local reference block
2056 static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
2059 if (lrbp->sense_buffer &&
2060 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
2063 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
2064 len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
2066 memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data,
2072 * ufshcd_copy_query_response() - Copy the Query Response and the data
2074 * @hba: per adapter instance
2075 * @lrbp: pointer to local reference block
2078 int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2080 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2082 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
2084 /* Get the descriptor */
2085 if (hba->dev_cmd.query.descriptor &&
2086 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
2087 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
2088 GENERAL_UPIU_REQUEST_SIZE;
2092 /* data segment length */
2093 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
2094 MASK_QUERY_DATA_SEG_LEN;
2095 buf_len = be16_to_cpu(
2096 hba->dev_cmd.query.request.upiu_req.length);
2097 if (likely(buf_len >= resp_len)) {
2098 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
2101 "%s: rsp size %d is bigger than buffer size %d",
2102 __func__, resp_len, buf_len);
2111 * ufshcd_hba_capabilities - Read controller capabilities
2112 * @hba: per adapter instance
2114 * Return: 0 on success, negative on error.
2116 static inline int ufshcd_hba_capabilities(struct ufs_hba *hba)
2120 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
2122 /* nutrs and nutmrs are 0 based values */
2123 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2125 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
2127 /* Read crypto capabilities */
2128 err = ufshcd_hba_init_crypto_capabilities(hba);
2130 dev_err(hba->dev, "crypto setup failed\n");
2136 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2137 * to accept UIC commands
2138 * @hba: per adapter instance
2139 * Return true on success, else false
2141 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2143 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2150 * ufshcd_get_upmcrs - Get the power mode change request status
2151 * @hba: Pointer to adapter instance
2153 * This function gets the UPMCRS field of HCS register
2154 * Returns value of UPMCRS field
2156 static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2158 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2162 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
2163 * @hba: per adapter instance
2164 * @uic_cmd: UIC command
2166 * Mutex must be held.
2169 ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2171 WARN_ON(hba->active_uic_cmd);
2173 hba->active_uic_cmd = uic_cmd;
2176 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2177 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2178 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
2180 ufshcd_add_uic_command_trace(hba, uic_cmd, UFS_CMD_SEND);
2183 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
2188 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
2189 * @hba: per adapter instance
2190 * @uic_cmd: UIC command
2192 * Must be called with mutex held.
2193 * Returns 0 only if success.
2196 ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2199 unsigned long flags;
2201 if (wait_for_completion_timeout(&uic_cmd->done,
2202 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2203 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2207 "uic cmd 0x%x with arg3 0x%x completion timeout\n",
2208 uic_cmd->command, uic_cmd->argument3);
2210 if (!uic_cmd->cmd_active) {
2211 dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n",
2213 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2217 spin_lock_irqsave(hba->host->host_lock, flags);
2218 hba->active_uic_cmd = NULL;
2219 spin_unlock_irqrestore(hba->host->host_lock, flags);
2225 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2226 * @hba: per adapter instance
2227 * @uic_cmd: UIC command
2228 * @completion: initialize the completion only if this is set to true
2230 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
2231 * with mutex held and host_lock locked.
2232 * Returns 0 only if success.
2235 __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2238 if (!ufshcd_ready_for_uic_cmd(hba)) {
2240 "Controller not ready to accept UIC commands\n");
2245 init_completion(&uic_cmd->done);
2247 uic_cmd->cmd_active = 1;
2248 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
2254 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2255 * @hba: per adapter instance
2256 * @uic_cmd: UIC command
2258 * Returns 0 only if success.
2260 int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2263 unsigned long flags;
2265 ufshcd_hold(hba, false);
2266 mutex_lock(&hba->uic_cmd_mutex);
2267 ufshcd_add_delay_before_dme_cmd(hba);
2269 spin_lock_irqsave(hba->host->host_lock, flags);
2270 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
2271 spin_unlock_irqrestore(hba->host->host_lock, flags);
2273 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2275 mutex_unlock(&hba->uic_cmd_mutex);
2277 ufshcd_release(hba);
2282 * ufshcd_map_sg - Map scatter-gather list to prdt
2283 * @hba: per adapter instance
2284 * @lrbp: pointer to local reference block
2286 * Returns 0 in case of success, non-zero value in case of failure
2288 static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2290 struct ufshcd_sg_entry *prd_table;
2291 struct scatterlist *sg;
2292 struct scsi_cmnd *cmd;
2297 sg_segments = scsi_dma_map(cmd);
2298 if (sg_segments < 0)
2303 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2304 lrbp->utr_descriptor_ptr->prd_table_length =
2305 cpu_to_le16((sg_segments *
2306 sizeof(struct ufshcd_sg_entry)));
2308 lrbp->utr_descriptor_ptr->prd_table_length =
2309 cpu_to_le16((u16) (sg_segments));
2311 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2313 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2315 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2316 prd_table[i].base_addr =
2317 cpu_to_le32(lower_32_bits(sg->dma_address));
2318 prd_table[i].upper_addr =
2319 cpu_to_le32(upper_32_bits(sg->dma_address));
2320 prd_table[i].reserved = 0;
2323 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2330 * ufshcd_enable_intr - enable interrupts
2331 * @hba: per adapter instance
2332 * @intrs: interrupt bits
2334 static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
2336 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2338 if (hba->ufs_version == UFSHCI_VERSION_10) {
2340 rw = set & INTERRUPT_MASK_RW_VER_10;
2341 set = rw | ((set ^ intrs) & intrs);
2346 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2350 * ufshcd_disable_intr - disable interrupts
2351 * @hba: per adapter instance
2352 * @intrs: interrupt bits
2354 static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2356 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2358 if (hba->ufs_version == UFSHCI_VERSION_10) {
2360 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2361 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2362 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2368 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2372 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2373 * descriptor according to request
2374 * @lrbp: pointer to local reference block
2375 * @upiu_flags: flags required in the header
2376 * @cmd_dir: requests data direction
2378 static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
2379 u8 *upiu_flags, enum dma_data_direction cmd_dir)
2381 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2387 if (cmd_dir == DMA_FROM_DEVICE) {
2388 data_direction = UTP_DEVICE_TO_HOST;
2389 *upiu_flags = UPIU_CMD_FLAGS_READ;
2390 } else if (cmd_dir == DMA_TO_DEVICE) {
2391 data_direction = UTP_HOST_TO_DEVICE;
2392 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2394 data_direction = UTP_NO_DATA_TRANSFER;
2395 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2398 dword_0 = data_direction | (lrbp->command_type
2399 << UPIU_COMMAND_TYPE_OFFSET);
2401 dword_0 |= UTP_REQ_DESC_INT_CMD;
2403 /* Prepare crypto related dwords */
2404 ufshcd_prepare_req_desc_hdr_crypto(lrbp, &dword_0, &dword_1, &dword_3);
2406 /* Transfer request descriptor header fields */
2407 req_desc->header.dword_0 = cpu_to_le32(dword_0);
2408 req_desc->header.dword_1 = cpu_to_le32(dword_1);
2410 * assigning invalid value for command status. Controller
2411 * updates OCS on command completion, with the command
2414 req_desc->header.dword_2 =
2415 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
2416 req_desc->header.dword_3 = cpu_to_le32(dword_3);
2418 req_desc->prd_table_length = 0;
2422 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2424 * @lrbp: local reference block pointer
2425 * @upiu_flags: flags
2428 void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags)
2430 struct scsi_cmnd *cmd = lrbp->cmd;
2431 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2432 unsigned short cdb_len;
2434 /* command descriptor fields */
2435 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2436 UPIU_TRANSACTION_COMMAND, upiu_flags,
2437 lrbp->lun, lrbp->task_tag);
2438 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2439 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2441 /* Total EHS length and Data segment length will be zero */
2442 ucd_req_ptr->header.dword_2 = 0;
2444 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length);
2446 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE);
2447 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
2448 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len);
2450 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
2454 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2457 * @lrbp: local reference block pointer
2458 * @upiu_flags: flags
2460 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2461 struct ufshcd_lrb *lrbp, u8 upiu_flags)
2463 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2464 struct ufs_query *query = &hba->dev_cmd.query;
2465 u16 len = be16_to_cpu(query->request.upiu_req.length);
2467 /* Query request header */
2468 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2469 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2470 lrbp->lun, lrbp->task_tag);
2471 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2472 0, query->request.query_func, 0, 0);
2474 /* Data segment length only need for WRITE_DESC */
2475 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2476 ucd_req_ptr->header.dword_2 =
2477 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2479 ucd_req_ptr->header.dword_2 = 0;
2481 /* Copy the Query Request buffer as is */
2482 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2485 /* Copy the Descriptor */
2486 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2487 memcpy(ucd_req_ptr + 1, query->descriptor, len);
2489 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
2492 static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2494 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2496 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2498 /* command descriptor fields */
2499 ucd_req_ptr->header.dword_0 =
2501 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
2502 /* clear rest of the fields of basic header */
2503 ucd_req_ptr->header.dword_1 = 0;
2504 ucd_req_ptr->header.dword_2 = 0;
2506 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
2510 * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU)
2511 * for Device Management Purposes
2512 * @hba: per adapter instance
2513 * @lrbp: pointer to local reference block
2515 static int ufshcd_compose_devman_upiu(struct ufs_hba *hba,
2516 struct ufshcd_lrb *lrbp)
2521 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2522 (hba->ufs_version == UFSHCI_VERSION_11))
2523 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
2525 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
2527 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2528 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2529 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2530 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2531 ufshcd_prepare_utp_nop_upiu(lrbp);
2539 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2541 * @hba: per adapter instance
2542 * @lrbp: pointer to local reference block
2544 static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2549 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2550 (hba->ufs_version == UFSHCI_VERSION_11))
2551 lrbp->command_type = UTP_CMD_TYPE_SCSI;
2553 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
2555 if (likely(lrbp->cmd)) {
2556 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2557 lrbp->cmd->sc_data_direction);
2558 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2567 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2568 * @upiu_wlun_id: UPIU W-LUN id
2570 * Returns SCSI W-LUN id
2572 static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2574 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2577 static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i)
2579 struct utp_transfer_cmd_desc *cmd_descp = hba->ucdl_base_addr;
2580 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr;
2581 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr +
2582 i * sizeof(struct utp_transfer_cmd_desc);
2583 u16 response_offset = offsetof(struct utp_transfer_cmd_desc,
2585 u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table);
2587 lrb->utr_descriptor_ptr = utrdlp + i;
2588 lrb->utrd_dma_addr = hba->utrdl_dma_addr +
2589 i * sizeof(struct utp_transfer_req_desc);
2590 lrb->ucd_req_ptr = (struct utp_upiu_req *)(cmd_descp + i);
2591 lrb->ucd_req_dma_addr = cmd_desc_element_addr;
2592 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2593 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset;
2594 lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2595 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset;
2599 * ufshcd_queuecommand - main entry point for SCSI requests
2600 * @host: SCSI host pointer
2601 * @cmd: command from SCSI Midlayer
2603 * Returns 0 for success, non-zero in case of failure
2605 static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2607 struct ufshcd_lrb *lrbp;
2608 struct ufs_hba *hba;
2609 unsigned long flags;
2613 hba = shost_priv(host);
2615 tag = cmd->request->tag;
2616 if (!ufshcd_valid_tag(hba, tag)) {
2618 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2619 __func__, tag, cmd, cmd->request);
2623 if (!down_read_trylock(&hba->clk_scaling_lock))
2624 return SCSI_MLQUEUE_HOST_BUSY;
2626 hba->req_abort_count = 0;
2628 err = ufshcd_hold(hba, true);
2630 err = SCSI_MLQUEUE_HOST_BUSY;
2633 WARN_ON(ufshcd_is_clkgating_allowed(hba) &&
2634 (hba->clk_gating.state != CLKS_ON));
2636 lrbp = &hba->lrb[tag];
2637 if (unlikely(lrbp->in_use)) {
2638 if (hba->pm_op_in_progress)
2639 set_host_byte(cmd, DID_BAD_TARGET);
2641 err = SCSI_MLQUEUE_HOST_BUSY;
2642 ufshcd_release(hba);
2648 lrbp->sense_bufflen = UFS_SENSE_SIZE;
2649 lrbp->sense_buffer = cmd->sense_buffer;
2650 lrbp->task_tag = tag;
2651 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
2652 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
2654 ufshcd_prepare_lrbp_crypto(cmd->request, lrbp);
2656 lrbp->req_abort_skip = false;
2658 ufshcd_comp_scsi_upiu(hba, lrbp);
2660 err = ufshcd_map_sg(hba, lrbp);
2663 ufshcd_release(hba);
2666 /* Make sure descriptors are ready before ringing the doorbell */
2669 spin_lock_irqsave(hba->host->host_lock, flags);
2670 switch (hba->ufshcd_state) {
2671 case UFSHCD_STATE_OPERATIONAL:
2672 case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL:
2674 case UFSHCD_STATE_EH_SCHEDULED_FATAL:
2676 * pm_runtime_get_sync() is used at error handling preparation
2677 * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's
2678 * PM ops, it can never be finished if we let SCSI layer keep
2679 * retrying it, which gets err handler stuck forever. Neither
2680 * can we let the scsi cmd pass through, because UFS is in bad
2681 * state, the scsi cmd may eventually time out, which will get
2682 * err handler blocked for too long. So, just fail the scsi cmd
2683 * sent from PM ops, err handler can recover PM error anyways.
2685 if (hba->pm_op_in_progress) {
2686 hba->force_reset = true;
2687 set_host_byte(cmd, DID_BAD_TARGET);
2691 case UFSHCD_STATE_RESET:
2692 err = SCSI_MLQUEUE_HOST_BUSY;
2694 case UFSHCD_STATE_ERROR:
2695 set_host_byte(cmd, DID_ERROR);
2698 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2699 __func__, hba->ufshcd_state);
2700 set_host_byte(cmd, DID_BAD_TARGET);
2703 ufshcd_send_command(hba, tag);
2704 spin_unlock_irqrestore(hba->host->host_lock, flags);
2708 scsi_dma_unmap(lrbp->cmd);
2710 spin_unlock_irqrestore(hba->host->host_lock, flags);
2711 ufshcd_release(hba);
2713 cmd->scsi_done(cmd);
2715 up_read(&hba->clk_scaling_lock);
2719 static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2720 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2723 lrbp->sense_bufflen = 0;
2724 lrbp->sense_buffer = NULL;
2725 lrbp->task_tag = tag;
2726 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
2727 lrbp->intr_cmd = true; /* No interrupt aggregation */
2728 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
2729 hba->dev_cmd.type = cmd_type;
2731 return ufshcd_compose_devman_upiu(hba, lrbp);
2735 ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2738 unsigned long flags;
2739 u32 mask = 1 << tag;
2741 /* clear outstanding transaction before retry */
2742 spin_lock_irqsave(hba->host->host_lock, flags);
2743 ufshcd_utrl_clear(hba, tag);
2744 spin_unlock_irqrestore(hba->host->host_lock, flags);
2747 * wait for for h/w to clear corresponding bit in door-bell.
2748 * max. wait is 1 sec.
2750 err = ufshcd_wait_for_register(hba,
2751 REG_UTP_TRANSFER_REQ_DOOR_BELL,
2752 mask, ~mask, 1000, 1000);
2758 ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2760 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2762 /* Get the UPIU response */
2763 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2764 UPIU_RSP_CODE_OFFSET;
2765 return query_res->response;
2769 * ufshcd_dev_cmd_completion() - handles device management command responses
2770 * @hba: per adapter instance
2771 * @lrbp: pointer to local reference block
2774 ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2779 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
2780 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2783 case UPIU_TRANSACTION_NOP_IN:
2784 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2786 dev_err(hba->dev, "%s: unexpected response %x\n",
2790 case UPIU_TRANSACTION_QUERY_RSP:
2791 err = ufshcd_check_query_response(hba, lrbp);
2793 err = ufshcd_copy_query_response(hba, lrbp);
2795 case UPIU_TRANSACTION_REJECT_UPIU:
2796 /* TODO: handle Reject UPIU Response */
2798 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2803 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2811 static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2812 struct ufshcd_lrb *lrbp, int max_timeout)
2815 unsigned long time_left;
2816 unsigned long flags;
2818 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2819 msecs_to_jiffies(max_timeout));
2821 /* Make sure descriptors are ready before ringing the doorbell */
2823 spin_lock_irqsave(hba->host->host_lock, flags);
2824 hba->dev_cmd.complete = NULL;
2825 if (likely(time_left)) {
2826 err = ufshcd_get_tr_ocs(lrbp);
2828 err = ufshcd_dev_cmd_completion(hba, lrbp);
2830 spin_unlock_irqrestore(hba->host->host_lock, flags);
2834 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2835 __func__, lrbp->task_tag);
2836 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
2837 /* successfully cleared the command, retry if needed */
2840 * in case of an error, after clearing the doorbell,
2841 * we also need to clear the outstanding_request
2844 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
2851 * ufshcd_exec_dev_cmd - API for sending device management requests
2853 * @cmd_type: specifies the type (NOP, Query...)
2854 * @timeout: time in seconds
2856 * NOTE: Since there is only one available tag for device management commands,
2857 * it is expected you hold the hba->dev_cmd.lock mutex.
2859 static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2860 enum dev_cmd_type cmd_type, int timeout)
2862 struct request_queue *q = hba->cmd_queue;
2863 struct request *req;
2864 struct ufshcd_lrb *lrbp;
2867 struct completion wait;
2868 unsigned long flags;
2870 down_read(&hba->clk_scaling_lock);
2873 * Get free slot, sleep if slots are unavailable.
2874 * Even though we use wait_event() which sleeps indefinitely,
2875 * the maximum wait time is bounded by SCSI request timeout.
2877 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
2883 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
2885 init_completion(&wait);
2886 lrbp = &hba->lrb[tag];
2887 if (unlikely(lrbp->in_use)) {
2893 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2897 hba->dev_cmd.complete = &wait;
2899 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr);
2900 /* Make sure descriptors are ready before ringing the doorbell */
2902 spin_lock_irqsave(hba->host->host_lock, flags);
2903 ufshcd_send_command(hba, tag);
2904 spin_unlock_irqrestore(hba->host->host_lock, flags);
2906 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2909 ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP,
2910 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr);
2913 blk_put_request(req);
2915 up_read(&hba->clk_scaling_lock);
2920 * ufshcd_init_query() - init the query response and request parameters
2921 * @hba: per-adapter instance
2922 * @request: address of the request pointer to be initialized
2923 * @response: address of the response pointer to be initialized
2924 * @opcode: operation to perform
2925 * @idn: flag idn to access
2926 * @index: LU number to access
2927 * @selector: query/flag/descriptor further identification
2929 static inline void ufshcd_init_query(struct ufs_hba *hba,
2930 struct ufs_query_req **request, struct ufs_query_res **response,
2931 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2933 *request = &hba->dev_cmd.query.request;
2934 *response = &hba->dev_cmd.query.response;
2935 memset(*request, 0, sizeof(struct ufs_query_req));
2936 memset(*response, 0, sizeof(struct ufs_query_res));
2937 (*request)->upiu_req.opcode = opcode;
2938 (*request)->upiu_req.idn = idn;
2939 (*request)->upiu_req.index = index;
2940 (*request)->upiu_req.selector = selector;
2943 static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2944 enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res)
2949 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
2950 ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res);
2953 "%s: failed with error %d, retries %d\n",
2954 __func__, ret, retries);
2961 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2962 __func__, opcode, idn, ret, retries);
2967 * ufshcd_query_flag() - API function for sending flag query requests
2968 * @hba: per-adapter instance
2969 * @opcode: flag query to perform
2970 * @idn: flag idn to access
2971 * @index: flag index to access
2972 * @flag_res: the flag value after the query request completes
2974 * Returns 0 for success, non-zero in case of failure
2976 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
2977 enum flag_idn idn, u8 index, bool *flag_res)
2979 struct ufs_query_req *request = NULL;
2980 struct ufs_query_res *response = NULL;
2981 int err, selector = 0;
2982 int timeout = QUERY_REQ_TIMEOUT;
2986 ufshcd_hold(hba, false);
2987 mutex_lock(&hba->dev_cmd.lock);
2988 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2992 case UPIU_QUERY_OPCODE_SET_FLAG:
2993 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2994 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2995 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2997 case UPIU_QUERY_OPCODE_READ_FLAG:
2998 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3000 /* No dummy reads */
3001 dev_err(hba->dev, "%s: Invalid argument for read request\n",
3009 "%s: Expected query flag opcode but got = %d\n",
3015 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
3019 "%s: Sending flag query for idn %d failed, err = %d\n",
3020 __func__, idn, err);
3025 *flag_res = (be32_to_cpu(response->upiu_res.value) &
3026 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
3029 mutex_unlock(&hba->dev_cmd.lock);
3030 ufshcd_release(hba);
3035 * ufshcd_query_attr - API function for sending attribute requests
3036 * @hba: per-adapter instance
3037 * @opcode: attribute opcode
3038 * @idn: attribute idn to access
3039 * @index: index field
3040 * @selector: selector field
3041 * @attr_val: the attribute value after the query request completes
3043 * Returns 0 for success, non-zero in case of failure
3045 int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
3046 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
3048 struct ufs_query_req *request = NULL;
3049 struct ufs_query_res *response = NULL;
3055 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
3060 ufshcd_hold(hba, false);
3062 mutex_lock(&hba->dev_cmd.lock);
3063 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3067 case UPIU_QUERY_OPCODE_WRITE_ATTR:
3068 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3069 request->upiu_req.value = cpu_to_be32(*attr_val);
3071 case UPIU_QUERY_OPCODE_READ_ATTR:
3072 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3075 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
3081 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3084 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3085 __func__, opcode, idn, index, err);
3089 *attr_val = be32_to_cpu(response->upiu_res.value);
3092 mutex_unlock(&hba->dev_cmd.lock);
3093 ufshcd_release(hba);
3098 * ufshcd_query_attr_retry() - API function for sending query
3099 * attribute with retries
3100 * @hba: per-adapter instance
3101 * @opcode: attribute opcode
3102 * @idn: attribute idn to access
3103 * @index: index field
3104 * @selector: selector field
3105 * @attr_val: the attribute value after the query request
3108 * Returns 0 for success, non-zero in case of failure
3110 static int ufshcd_query_attr_retry(struct ufs_hba *hba,
3111 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
3117 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3118 ret = ufshcd_query_attr(hba, opcode, idn, index,
3119 selector, attr_val);
3121 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
3122 __func__, ret, retries);
3129 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
3130 __func__, idn, ret, QUERY_REQ_RETRIES);
3134 static int __ufshcd_query_descriptor(struct ufs_hba *hba,
3135 enum query_opcode opcode, enum desc_idn idn, u8 index,
3136 u8 selector, u8 *desc_buf, int *buf_len)
3138 struct ufs_query_req *request = NULL;
3139 struct ufs_query_res *response = NULL;
3145 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3150 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
3151 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3152 __func__, *buf_len);
3156 ufshcd_hold(hba, false);
3158 mutex_lock(&hba->dev_cmd.lock);
3159 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3161 hba->dev_cmd.query.descriptor = desc_buf;
3162 request->upiu_req.length = cpu_to_be16(*buf_len);
3165 case UPIU_QUERY_OPCODE_WRITE_DESC:
3166 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3168 case UPIU_QUERY_OPCODE_READ_DESC:
3169 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3173 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3179 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3182 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3183 __func__, opcode, idn, index, err);
3187 *buf_len = be16_to_cpu(response->upiu_res.length);
3190 hba->dev_cmd.query.descriptor = NULL;
3191 mutex_unlock(&hba->dev_cmd.lock);
3192 ufshcd_release(hba);
3197 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
3198 * @hba: per-adapter instance
3199 * @opcode: attribute opcode
3200 * @idn: attribute idn to access
3201 * @index: index field
3202 * @selector: selector field
3203 * @desc_buf: the buffer that contains the descriptor
3204 * @buf_len: length parameter passed to the device
3206 * Returns 0 for success, non-zero in case of failure.
3207 * The buf_len parameter will contain, on return, the length parameter
3208 * received on the response.
3210 int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
3211 enum query_opcode opcode,
3212 enum desc_idn idn, u8 index,
3214 u8 *desc_buf, int *buf_len)
3219 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3220 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3221 selector, desc_buf, buf_len);
3222 if (!err || err == -EINVAL)
3230 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
3231 * @hba: Pointer to adapter instance
3232 * @desc_id: descriptor idn value
3233 * @desc_len: mapped desc length (out)
3235 void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
3238 if (desc_id >= QUERY_DESC_IDN_MAX || desc_id == QUERY_DESC_IDN_RFU_0 ||
3239 desc_id == QUERY_DESC_IDN_RFU_1)
3242 *desc_len = hba->desc_size[desc_id];
3244 EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
3246 static void ufshcd_update_desc_length(struct ufs_hba *hba,
3247 enum desc_idn desc_id, int desc_index,
3248 unsigned char desc_len)
3250 if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE &&
3251 desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT)
3252 /* For UFS 3.1, the normal unit descriptor is 10 bytes larger
3253 * than the RPMB unit, however, both descriptors share the same
3254 * desc_idn, to cover both unit descriptors with one length, we
3255 * choose the normal unit descriptor length by desc_index.
3257 hba->desc_size[desc_id] = desc_len;
3261 * ufshcd_read_desc_param - read the specified descriptor parameter
3262 * @hba: Pointer to adapter instance
3263 * @desc_id: descriptor idn value
3264 * @desc_index: descriptor index
3265 * @param_offset: offset of the parameter to read
3266 * @param_read_buf: pointer to buffer where parameter would be read
3267 * @param_size: sizeof(param_read_buf)
3269 * Return 0 in case of success, non-zero otherwise
3271 int ufshcd_read_desc_param(struct ufs_hba *hba,
3272 enum desc_idn desc_id,
3281 bool is_kmalloc = true;
3284 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
3287 /* Get the length of descriptor */
3288 ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
3290 dev_err(hba->dev, "%s: Failed to get desc length\n", __func__);
3294 if (param_offset >= buff_len) {
3295 dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n",
3296 __func__, param_offset, desc_id, buff_len);
3300 /* Check whether we need temp memory */
3301 if (param_offset != 0 || param_size < buff_len) {
3302 desc_buf = kzalloc(buff_len, GFP_KERNEL);
3306 desc_buf = param_read_buf;
3310 /* Request for full descriptor */
3311 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
3312 desc_id, desc_index, 0,
3313 desc_buf, &buff_len);
3316 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n",
3317 __func__, desc_id, desc_index, param_offset, ret);
3322 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3323 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n",
3324 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3329 /* Update descriptor length */
3330 buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET];
3331 ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len);
3334 /* Make sure we don't copy more data than available */
3335 if (param_offset + param_size > buff_len)
3336 param_size = buff_len - param_offset;
3337 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3346 * struct uc_string_id - unicode string
3348 * @len: size of this descriptor inclusive
3349 * @type: descriptor type
3350 * @uc: unicode string character
3352 struct uc_string_id {
3358 /* replace non-printable or non-ASCII characters with spaces */
3359 static inline char ufshcd_remove_non_printable(u8 ch)
3361 return (ch >= 0x20 && ch <= 0x7e) ? ch : ' ';
3365 * ufshcd_read_string_desc - read string descriptor
3366 * @hba: pointer to adapter instance
3367 * @desc_index: descriptor index
3368 * @buf: pointer to buffer where descriptor would be read,
3369 * the caller should free the memory.
3370 * @ascii: if true convert from unicode to ascii characters
3371 * null terminated string.
3374 * * string size on success.
3375 * * -ENOMEM: on allocation failure
3376 * * -EINVAL: on a wrong parameter
3378 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
3379 u8 **buf, bool ascii)
3381 struct uc_string_id *uc_str;
3388 uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
3392 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0,
3393 (u8 *)uc_str, QUERY_DESC_MAX_SIZE);
3395 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
3396 QUERY_REQ_RETRIES, ret);
3401 if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
3402 dev_dbg(hba->dev, "String Desc is of zero length\n");
3411 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3412 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3413 str = kzalloc(ascii_len, GFP_KERNEL);
3420 * the descriptor contains string in UTF16 format
3421 * we need to convert to utf-8 so it can be displayed
3423 ret = utf16s_to_utf8s(uc_str->uc,
3424 uc_str->len - QUERY_DESC_HDR_SIZE,
3425 UTF16_BIG_ENDIAN, str, ascii_len);
3427 /* replace non-printable or non-ASCII characters with spaces */
3428 for (i = 0; i < ret; i++)
3429 str[i] = ufshcd_remove_non_printable(str[i]);
3434 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL);
3448 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3449 * @hba: Pointer to adapter instance
3451 * @param_offset: offset of the parameter to read
3452 * @param_read_buf: pointer to buffer where parameter would be read
3453 * @param_size: sizeof(param_read_buf)
3455 * Return 0 in case of success, non-zero otherwise
3457 static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3459 enum unit_desc_param param_offset,
3464 * Unit descriptors are only available for general purpose LUs (LUN id
3465 * from 0 to 7) and RPMB Well known LU.
3467 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun, param_offset))
3470 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3471 param_offset, param_read_buf, param_size);
3474 static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba)
3477 u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3479 if (hba->dev_info.wspecversion >= 0x300) {
3480 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3481 QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0,
3484 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n",
3487 if (gating_wait == 0) {
3488 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US;
3489 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n",
3493 hba->dev_info.clk_gating_wait_us = gating_wait;
3500 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3501 * @hba: per adapter instance
3503 * 1. Allocate DMA memory for Command Descriptor array
3504 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3505 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3506 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3508 * 4. Allocate memory for local reference block(lrb).
3510 * Returns 0 for success, non-zero in case of failure
3512 static int ufshcd_memory_alloc(struct ufs_hba *hba)
3514 size_t utmrdl_size, utrdl_size, ucdl_size;
3516 /* Allocate memory for UTP command descriptors */
3517 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
3518 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3520 &hba->ucdl_dma_addr,
3524 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3525 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3526 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3527 * be aligned to 128 bytes as well
3529 if (!hba->ucdl_base_addr ||
3530 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
3532 "Command Descriptor Memory allocation failed\n");
3537 * Allocate memory for UTP Transfer descriptors
3538 * UFSHCI requires 1024 byte alignment of UTRD
3540 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
3541 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3543 &hba->utrdl_dma_addr,
3545 if (!hba->utrdl_base_addr ||
3546 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
3548 "Transfer Descriptor Memory allocation failed\n");
3553 * Allocate memory for UTP Task Management descriptors
3554 * UFSHCI requires 1024 byte alignment of UTMRD
3556 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
3557 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3559 &hba->utmrdl_dma_addr,
3561 if (!hba->utmrdl_base_addr ||
3562 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
3564 "Task Management Descriptor Memory allocation failed\n");
3568 /* Allocate memory for local reference block */
3569 hba->lrb = devm_kcalloc(hba->dev,
3570 hba->nutrs, sizeof(struct ufshcd_lrb),
3573 dev_err(hba->dev, "LRB Memory allocation failed\n");
3582 * ufshcd_host_memory_configure - configure local reference block with
3584 * @hba: per adapter instance
3586 * Configure Host memory space
3587 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3589 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3591 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3592 * into local reference block.
3594 static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3596 struct utp_transfer_req_desc *utrdlp;
3597 dma_addr_t cmd_desc_dma_addr;
3598 dma_addr_t cmd_desc_element_addr;
3599 u16 response_offset;
3604 utrdlp = hba->utrdl_base_addr;
3607 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3609 offsetof(struct utp_transfer_cmd_desc, prd_table);
3611 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3612 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3614 for (i = 0; i < hba->nutrs; i++) {
3615 /* Configure UTRD with command descriptor base address */
3616 cmd_desc_element_addr =
3617 (cmd_desc_dma_addr + (cmd_desc_size * i));
3618 utrdlp[i].command_desc_base_addr_lo =
3619 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3620 utrdlp[i].command_desc_base_addr_hi =
3621 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3623 /* Response upiu and prdt offset should be in double words */
3624 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3625 utrdlp[i].response_upiu_offset =
3626 cpu_to_le16(response_offset);
3627 utrdlp[i].prd_table_offset =
3628 cpu_to_le16(prdt_offset);
3629 utrdlp[i].response_upiu_length =
3630 cpu_to_le16(ALIGNED_UPIU_SIZE);
3632 utrdlp[i].response_upiu_offset =
3633 cpu_to_le16(response_offset >> 2);
3634 utrdlp[i].prd_table_offset =
3635 cpu_to_le16(prdt_offset >> 2);
3636 utrdlp[i].response_upiu_length =
3637 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
3640 ufshcd_init_lrb(hba, &hba->lrb[i], i);
3645 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3646 * @hba: per adapter instance
3648 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3649 * in order to initialize the Unipro link startup procedure.
3650 * Once the Unipro links are up, the device connected to the controller
3653 * Returns 0 on success, non-zero value on failure
3655 static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3657 struct uic_command uic_cmd = {0};
3660 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3662 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3665 "dme-link-startup: error code %d\n", ret);
3669 * ufshcd_dme_reset - UIC command for DME_RESET
3670 * @hba: per adapter instance
3672 * DME_RESET command is issued in order to reset UniPro stack.
3673 * This function now deals with cold reset.
3675 * Returns 0 on success, non-zero value on failure
3677 static int ufshcd_dme_reset(struct ufs_hba *hba)
3679 struct uic_command uic_cmd = {0};
3682 uic_cmd.command = UIC_CMD_DME_RESET;
3684 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3687 "dme-reset: error code %d\n", ret);
3692 int ufshcd_dme_configure_adapt(struct ufs_hba *hba,
3698 if (agreed_gear != UFS_HS_G4)
3699 adapt_val = PA_NO_ADAPT;
3701 ret = ufshcd_dme_set(hba,
3702 UIC_ARG_MIB(PA_TXHSADAPTTYPE),
3706 EXPORT_SYMBOL_GPL(ufshcd_dme_configure_adapt);
3709 * ufshcd_dme_enable - UIC command for DME_ENABLE
3710 * @hba: per adapter instance
3712 * DME_ENABLE command is issued in order to enable UniPro stack.
3714 * Returns 0 on success, non-zero value on failure
3716 static int ufshcd_dme_enable(struct ufs_hba *hba)
3718 struct uic_command uic_cmd = {0};
3721 uic_cmd.command = UIC_CMD_DME_ENABLE;
3723 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3726 "dme-enable: error code %d\n", ret);
3731 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3733 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3734 unsigned long min_sleep_time_us;
3736 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3740 * last_dme_cmd_tstamp will be 0 only for 1st call to
3743 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3744 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3746 unsigned long delta =
3747 (unsigned long) ktime_to_us(
3748 ktime_sub(ktime_get(),
3749 hba->last_dme_cmd_tstamp));
3751 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3753 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3755 return; /* no more delay required */
3758 /* allow sleep for extra 50us if needed */
3759 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3763 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3764 * @hba: per adapter instance
3765 * @attr_sel: uic command argument1
3766 * @attr_set: attribute set type as uic command argument2
3767 * @mib_val: setting value as uic command argument3
3768 * @peer: indicate whether peer or local
3770 * Returns 0 on success, non-zero value on failure
3772 int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3773 u8 attr_set, u32 mib_val, u8 peer)
3775 struct uic_command uic_cmd = {0};
3776 static const char *const action[] = {
3780 const char *set = action[!!peer];
3782 int retries = UFS_UIC_COMMAND_RETRIES;
3784 uic_cmd.command = peer ?
3785 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3786 uic_cmd.argument1 = attr_sel;
3787 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3788 uic_cmd.argument3 = mib_val;
3791 /* for peer attributes we retry upon failure */
3792 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3794 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3795 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3796 } while (ret && peer && --retries);
3799 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
3800 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3801 UFS_UIC_COMMAND_RETRIES - retries);
3805 EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3808 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3809 * @hba: per adapter instance
3810 * @attr_sel: uic command argument1
3811 * @mib_val: the value of the attribute as returned by the UIC command
3812 * @peer: indicate whether peer or local
3814 * Returns 0 on success, non-zero value on failure
3816 int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3817 u32 *mib_val, u8 peer)
3819 struct uic_command uic_cmd = {0};
3820 static const char *const action[] = {
3824 const char *get = action[!!peer];
3826 int retries = UFS_UIC_COMMAND_RETRIES;
3827 struct ufs_pa_layer_attr orig_pwr_info;
3828 struct ufs_pa_layer_attr temp_pwr_info;
3829 bool pwr_mode_change = false;
3831 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3832 orig_pwr_info = hba->pwr_info;
3833 temp_pwr_info = orig_pwr_info;
3835 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3836 orig_pwr_info.pwr_rx == FAST_MODE) {
3837 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3838 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3839 pwr_mode_change = true;
3840 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3841 orig_pwr_info.pwr_rx == SLOW_MODE) {
3842 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3843 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3844 pwr_mode_change = true;
3846 if (pwr_mode_change) {
3847 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3853 uic_cmd.command = peer ?
3854 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3855 uic_cmd.argument1 = attr_sel;
3858 /* for peer attributes we retry upon failure */
3859 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3861 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3862 get, UIC_GET_ATTR_ID(attr_sel), ret);
3863 } while (ret && peer && --retries);
3866 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
3867 get, UIC_GET_ATTR_ID(attr_sel),
3868 UFS_UIC_COMMAND_RETRIES - retries);
3870 if (mib_val && !ret)
3871 *mib_val = uic_cmd.argument3;
3873 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3875 ufshcd_change_power_mode(hba, &orig_pwr_info);
3879 EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3882 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3883 * state) and waits for it to take effect.
3885 * @hba: per adapter instance
3886 * @cmd: UIC command to execute
3888 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3889 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3890 * and device UniPro link and hence it's final completion would be indicated by
3891 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3892 * addition to normal UIC command completion Status (UCCS). This function only
3893 * returns after the relevant status bits indicate the completion.
3895 * Returns 0 on success, non-zero value on failure
3897 static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3899 struct completion uic_async_done;
3900 unsigned long flags;
3903 bool reenable_intr = false;
3905 mutex_lock(&hba->uic_cmd_mutex);
3906 init_completion(&uic_async_done);
3907 ufshcd_add_delay_before_dme_cmd(hba);
3909 spin_lock_irqsave(hba->host->host_lock, flags);
3910 if (ufshcd_is_link_broken(hba)) {
3914 hba->uic_async_done = &uic_async_done;
3915 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3916 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3918 * Make sure UIC command completion interrupt is disabled before
3919 * issuing UIC command.
3922 reenable_intr = true;
3924 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3925 spin_unlock_irqrestore(hba->host->host_lock, flags);
3928 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3929 cmd->command, cmd->argument3, ret);
3933 if (!wait_for_completion_timeout(hba->uic_async_done,
3934 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3936 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3937 cmd->command, cmd->argument3);
3939 if (!cmd->cmd_active) {
3940 dev_err(hba->dev, "%s: Power Mode Change operation has been completed, go check UPMCRS\n",
3950 status = ufshcd_get_upmcrs(hba);
3951 if (status != PWR_LOCAL) {
3953 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
3954 cmd->command, status);
3955 ret = (status != PWR_OK) ? status : -1;
3959 ufshcd_print_host_state(hba);
3960 ufshcd_print_pwr_info(hba);
3961 ufshcd_print_evt_hist(hba);
3964 spin_lock_irqsave(hba->host->host_lock, flags);
3965 hba->active_uic_cmd = NULL;
3966 hba->uic_async_done = NULL;
3968 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
3970 ufshcd_set_link_broken(hba);
3971 ufshcd_schedule_eh_work(hba);
3974 spin_unlock_irqrestore(hba->host->host_lock, flags);
3975 mutex_unlock(&hba->uic_cmd_mutex);
3981 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3982 * using DME_SET primitives.
3983 * @hba: per adapter instance
3984 * @mode: powr mode value
3986 * Returns 0 on success, non-zero value on failure
3988 static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
3990 struct uic_command uic_cmd = {0};
3993 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3994 ret = ufshcd_dme_set(hba,
3995 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3997 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
4003 uic_cmd.command = UIC_CMD_DME_SET;
4004 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
4005 uic_cmd.argument3 = mode;
4006 ufshcd_hold(hba, false);
4007 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
4008 ufshcd_release(hba);
4014 int ufshcd_link_recovery(struct ufs_hba *hba)
4017 unsigned long flags;
4019 spin_lock_irqsave(hba->host->host_lock, flags);
4020 hba->ufshcd_state = UFSHCD_STATE_RESET;
4021 ufshcd_set_eh_in_progress(hba);
4022 spin_unlock_irqrestore(hba->host->host_lock, flags);
4024 /* Reset the attached device */
4025 ufshcd_device_reset(hba);
4027 ret = ufshcd_host_reset_and_restore(hba);
4029 spin_lock_irqsave(hba->host->host_lock, flags);
4031 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4032 ufshcd_clear_eh_in_progress(hba);
4033 spin_unlock_irqrestore(hba->host->host_lock, flags);
4036 dev_err(hba->dev, "%s: link recovery failed, err %d",
4039 ufshcd_clear_ua_wluns(hba);
4043 EXPORT_SYMBOL_GPL(ufshcd_link_recovery);
4045 static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
4048 struct uic_command uic_cmd = {0};
4049 ktime_t start = ktime_get();
4051 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
4053 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
4054 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
4055 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
4056 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
4059 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
4062 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
4068 int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
4070 struct uic_command uic_cmd = {0};
4072 ktime_t start = ktime_get();
4074 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
4076 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
4077 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
4078 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
4079 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
4082 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
4085 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
4087 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
4088 hba->ufs_stats.hibern8_exit_cnt++;
4093 EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit);
4095 void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit)
4097 unsigned long flags;
4098 bool update = false;
4100 if (!ufshcd_is_auto_hibern8_supported(hba))
4103 spin_lock_irqsave(hba->host->host_lock, flags);
4104 if (hba->ahit != ahit) {
4108 spin_unlock_irqrestore(hba->host->host_lock, flags);
4110 if (update && !pm_runtime_suspended(hba->dev)) {
4111 pm_runtime_get_sync(hba->dev);
4112 ufshcd_hold(hba, false);
4113 ufshcd_auto_hibern8_enable(hba);
4114 ufshcd_release(hba);
4115 pm_runtime_put(hba->dev);
4118 EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
4120 void ufshcd_auto_hibern8_enable(struct ufs_hba *hba)
4122 unsigned long flags;
4124 if (!ufshcd_is_auto_hibern8_supported(hba))
4127 spin_lock_irqsave(hba->host->host_lock, flags);
4128 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
4129 spin_unlock_irqrestore(hba->host->host_lock, flags);
4133 * ufshcd_init_pwr_info - setting the POR (power on reset)
4134 * values in hba power info
4135 * @hba: per-adapter instance
4137 static void ufshcd_init_pwr_info(struct ufs_hba *hba)
4139 hba->pwr_info.gear_rx = UFS_PWM_G1;
4140 hba->pwr_info.gear_tx = UFS_PWM_G1;
4141 hba->pwr_info.lane_rx = 1;
4142 hba->pwr_info.lane_tx = 1;
4143 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
4144 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
4145 hba->pwr_info.hs_rate = 0;
4149 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4150 * @hba: per-adapter instance
4152 static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
4154 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4156 if (hba->max_pwr_info.is_valid)
4159 pwr_info->pwr_tx = FAST_MODE;
4160 pwr_info->pwr_rx = FAST_MODE;
4161 pwr_info->hs_rate = PA_HS_MODE_B;
4163 /* Get the connected lane count */
4164 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4165 &pwr_info->lane_rx);
4166 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4167 &pwr_info->lane_tx);
4169 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4170 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4178 * First, get the maximum gears of HS speed.
4179 * If a zero value, it means there is no HSGEAR capability.
4180 * Then, get the maximum gears of PWM speed.
4182 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4183 if (!pwr_info->gear_rx) {
4184 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4185 &pwr_info->gear_rx);
4186 if (!pwr_info->gear_rx) {
4187 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4188 __func__, pwr_info->gear_rx);
4191 pwr_info->pwr_rx = SLOW_MODE;
4194 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4195 &pwr_info->gear_tx);
4196 if (!pwr_info->gear_tx) {
4197 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4198 &pwr_info->gear_tx);
4199 if (!pwr_info->gear_tx) {
4200 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4201 __func__, pwr_info->gear_tx);
4204 pwr_info->pwr_tx = SLOW_MODE;
4207 hba->max_pwr_info.is_valid = true;
4211 static int ufshcd_change_power_mode(struct ufs_hba *hba,
4212 struct ufs_pa_layer_attr *pwr_mode)
4216 /* if already configured to the requested pwr_mode */
4217 if (!hba->force_pmc &&
4218 pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4219 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4220 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4221 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4222 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4223 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4224 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4225 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4230 * Configure attributes for power mode change with below.
4231 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4232 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4235 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4236 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4238 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4239 pwr_mode->pwr_rx == FAST_MODE)
4240 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
4242 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
4244 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4245 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4247 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4248 pwr_mode->pwr_tx == FAST_MODE)
4249 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
4251 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
4253 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4254 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4255 pwr_mode->pwr_rx == FAST_MODE ||
4256 pwr_mode->pwr_tx == FAST_MODE)
4257 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4260 if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) {
4261 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4262 DL_FC0ProtectionTimeOutVal_Default);
4263 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4264 DL_TC0ReplayTimeOutVal_Default);
4265 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4266 DL_AFC0ReqTimeOutVal_Default);
4267 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
4268 DL_FC1ProtectionTimeOutVal_Default);
4269 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
4270 DL_TC1ReplayTimeOutVal_Default);
4271 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
4272 DL_AFC1ReqTimeOutVal_Default);
4274 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4275 DL_FC0ProtectionTimeOutVal_Default);
4276 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4277 DL_TC0ReplayTimeOutVal_Default);
4278 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4279 DL_AFC0ReqTimeOutVal_Default);
4282 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4283 | pwr_mode->pwr_tx);
4287 "%s: power mode change failed %d\n", __func__, ret);
4289 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4292 memcpy(&hba->pwr_info, pwr_mode,
4293 sizeof(struct ufs_pa_layer_attr));
4300 * ufshcd_config_pwr_mode - configure a new power mode
4301 * @hba: per-adapter instance
4302 * @desired_pwr_mode: desired power configuration
4304 int ufshcd_config_pwr_mode(struct ufs_hba *hba,
4305 struct ufs_pa_layer_attr *desired_pwr_mode)
4307 struct ufs_pa_layer_attr final_params = { 0 };
4310 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4311 desired_pwr_mode, &final_params);
4314 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4316 ret = ufshcd_change_power_mode(hba, &final_params);
4320 EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
4323 * ufshcd_complete_dev_init() - checks device readiness
4324 * @hba: per-adapter instance
4326 * Set fDeviceInit flag and poll until device toggles it.
4328 static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4331 bool flag_res = true;
4334 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
4335 QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL);
4338 "%s setting fDeviceInit flag failed with error %d\n",
4343 /* Poll fDeviceInit flag to be cleared */
4344 timeout = ktime_add_ms(ktime_get(), FDEVICEINIT_COMPL_TIMEOUT);
4346 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4347 QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res);
4350 usleep_range(5000, 10000);
4351 } while (ktime_before(ktime_get(), timeout));
4355 "%s reading fDeviceInit flag failed with error %d\n",
4357 } else if (flag_res) {
4359 "%s fDeviceInit was not cleared by the device\n",
4368 * ufshcd_make_hba_operational - Make UFS controller operational
4369 * @hba: per adapter instance
4371 * To bring UFS host controller to operational state,
4372 * 1. Enable required interrupts
4373 * 2. Configure interrupt aggregation
4374 * 3. Program UTRL and UTMRL base address
4375 * 4. Configure run-stop-registers
4377 * Returns 0 on success, non-zero value on failure
4379 int ufshcd_make_hba_operational(struct ufs_hba *hba)
4384 /* Enable required interrupts */
4385 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4387 /* Configure interrupt aggregation */
4388 if (ufshcd_is_intr_aggr_allowed(hba))
4389 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4391 ufshcd_disable_intr_aggr(hba);
4393 /* Configure UTRL and UTMRL base address registers */
4394 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4395 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4396 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4397 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4398 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4399 REG_UTP_TASK_REQ_LIST_BASE_L);
4400 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4401 REG_UTP_TASK_REQ_LIST_BASE_H);
4404 * Make sure base address and interrupt setup are updated before
4405 * enabling the run/stop registers below.
4410 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
4412 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
4413 if (!(ufshcd_get_lists_status(reg))) {
4414 ufshcd_enable_run_stop_reg(hba);
4417 "Host controller not ready to process requests");
4423 EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
4426 * ufshcd_hba_stop - Send controller to reset state
4427 * @hba: per adapter instance
4429 static inline void ufshcd_hba_stop(struct ufs_hba *hba)
4431 unsigned long flags;
4435 * Obtain the host lock to prevent that the controller is disabled
4436 * while the UFS interrupt handler is active on another CPU.
4438 spin_lock_irqsave(hba->host->host_lock, flags);
4439 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
4440 spin_unlock_irqrestore(hba->host->host_lock, flags);
4442 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4443 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
4446 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4450 * ufshcd_hba_execute_hce - initialize the controller
4451 * @hba: per adapter instance
4453 * The controller resets itself and controller firmware initialization
4454 * sequence kicks off. When controller is ready it will set
4455 * the Host Controller Enable bit to 1.
4457 * Returns 0 on success, non-zero value on failure
4459 static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
4461 int retry_outer = 3;
4465 if (!ufshcd_is_hba_active(hba))
4466 /* change controller state to "reset state" */
4467 ufshcd_hba_stop(hba);
4469 /* UniPro link is disabled at this point */
4470 ufshcd_set_link_off(hba);
4472 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4474 /* start controller initialization sequence */
4475 ufshcd_hba_start(hba);
4478 * To initialize a UFS host controller HCE bit must be set to 1.
4479 * During initialization the HCE bit value changes from 1->0->1.
4480 * When the host controller completes initialization sequence
4481 * it sets the value of HCE bit to 1. The same HCE bit is read back
4482 * to check if the controller has completed initialization sequence.
4483 * So without this delay the value HCE = 1, set in the previous
4484 * instruction might be read back.
4485 * This delay can be changed based on the controller.
4487 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100);
4489 /* wait for the host controller to complete initialization */
4491 while (ufshcd_is_hba_active(hba)) {
4496 "Controller enable failed\n");
4503 usleep_range(1000, 1100);
4506 /* enable UIC related interrupts */
4507 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4509 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4514 int ufshcd_hba_enable(struct ufs_hba *hba)
4518 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
4519 ufshcd_set_link_off(hba);
4520 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4522 /* enable UIC related interrupts */
4523 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4524 ret = ufshcd_dme_reset(hba);
4526 ret = ufshcd_dme_enable(hba);
4528 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4531 "Host controller enable failed with non-hce\n");
4534 ret = ufshcd_hba_execute_hce(hba);
4539 EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
4541 static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4543 int tx_lanes = 0, i, err = 0;
4546 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4549 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4551 for (i = 0; i < tx_lanes; i++) {
4553 err = ufshcd_dme_set(hba,
4554 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4555 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4558 err = ufshcd_dme_peer_set(hba,
4559 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4560 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4563 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4564 __func__, peer, i, err);
4572 static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4574 return ufshcd_disable_tx_lcc(hba, true);
4577 void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val)
4579 struct ufs_event_hist *e;
4581 if (id >= UFS_EVT_CNT)
4584 e = &hba->ufs_stats.event[id];
4585 e->val[e->pos] = val;
4586 e->tstamp[e->pos] = ktime_get();
4588 e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH;
4590 ufshcd_vops_event_notify(hba, id, &val);
4592 EXPORT_SYMBOL_GPL(ufshcd_update_evt_hist);
4595 * ufshcd_link_startup - Initialize unipro link startup
4596 * @hba: per adapter instance
4598 * Returns 0 for success, non-zero in case of failure
4600 static int ufshcd_link_startup(struct ufs_hba *hba)
4603 int retries = DME_LINKSTARTUP_RETRIES;
4604 bool link_startup_again = false;
4607 * If UFS device isn't active then we will have to issue link startup
4608 * 2 times to make sure the device state move to active.
4610 if (!ufshcd_is_ufs_dev_active(hba))
4611 link_startup_again = true;
4615 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
4617 ret = ufshcd_dme_link_startup(hba);
4619 /* check if device is detected by inter-connect layer */
4620 if (!ret && !ufshcd_is_device_present(hba)) {
4621 ufshcd_update_evt_hist(hba,
4622 UFS_EVT_LINK_STARTUP_FAIL,
4624 dev_err(hba->dev, "%s: Device not present\n", __func__);
4630 * DME link lost indication is only received when link is up,
4631 * but we can't be sure if the link is up until link startup
4632 * succeeds. So reset the local Uni-Pro and try again.
4634 if (ret && ufshcd_hba_enable(hba)) {
4635 ufshcd_update_evt_hist(hba,
4636 UFS_EVT_LINK_STARTUP_FAIL,
4640 } while (ret && retries--);
4643 /* failed to get the link up... retire */
4644 ufshcd_update_evt_hist(hba,
4645 UFS_EVT_LINK_STARTUP_FAIL,
4650 if (link_startup_again) {
4651 link_startup_again = false;
4652 retries = DME_LINKSTARTUP_RETRIES;
4656 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4657 ufshcd_init_pwr_info(hba);
4658 ufshcd_print_pwr_info(hba);
4660 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4661 ret = ufshcd_disable_device_tx_lcc(hba);
4666 /* Include any host controller configuration via UIC commands */
4667 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4671 /* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */
4672 ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
4673 ret = ufshcd_make_hba_operational(hba);
4676 dev_err(hba->dev, "link startup failed %d\n", ret);
4677 ufshcd_print_host_state(hba);
4678 ufshcd_print_pwr_info(hba);
4679 ufshcd_print_evt_hist(hba);
4685 * ufshcd_verify_dev_init() - Verify device initialization
4686 * @hba: per-adapter instance
4688 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4689 * device Transport Protocol (UTP) layer is ready after a reset.
4690 * If the UTP layer at the device side is not initialized, it may
4691 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4692 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4694 static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4699 ufshcd_hold(hba, false);
4700 mutex_lock(&hba->dev_cmd.lock);
4701 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4702 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4705 if (!err || err == -ETIMEDOUT)
4708 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4710 mutex_unlock(&hba->dev_cmd.lock);
4711 ufshcd_release(hba);
4714 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4719 * ufshcd_set_queue_depth - set lun queue depth
4720 * @sdev: pointer to SCSI device
4722 * Read bLUQueueDepth value and activate scsi tagged command
4723 * queueing. For WLUN, queue depth is set to 1. For best-effort
4724 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4725 * value that host can queue.
4727 static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4731 struct ufs_hba *hba;
4733 hba = shost_priv(sdev->host);
4735 lun_qdepth = hba->nutrs;
4736 ret = ufshcd_read_unit_desc_param(hba,
4737 ufshcd_scsi_to_upiu_lun(sdev->lun),
4738 UNIT_DESC_PARAM_LU_Q_DEPTH,
4740 sizeof(lun_qdepth));
4742 /* Some WLUN doesn't support unit descriptor */
4743 if (ret == -EOPNOTSUPP)
4745 else if (!lun_qdepth)
4746 /* eventually, we can figure out the real queue depth */
4747 lun_qdepth = hba->nutrs;
4749 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4751 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4752 __func__, lun_qdepth);
4753 scsi_change_queue_depth(sdev, lun_qdepth);
4757 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4758 * @hba: per-adapter instance
4759 * @lun: UFS device lun id
4760 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4762 * Returns 0 in case of success and b_lu_write_protect status would be returned
4763 * @b_lu_write_protect parameter.
4764 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4765 * Returns -EINVAL in case of invalid parameters passed to this function.
4767 static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4769 u8 *b_lu_write_protect)
4773 if (!b_lu_write_protect)
4776 * According to UFS device spec, RPMB LU can't be write
4777 * protected so skip reading bLUWriteProtect parameter for
4778 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4780 else if (lun >= hba->dev_info.max_lu_supported)
4783 ret = ufshcd_read_unit_desc_param(hba,
4785 UNIT_DESC_PARAM_LU_WR_PROTECT,
4787 sizeof(*b_lu_write_protect));
4792 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4794 * @hba: per-adapter instance
4795 * @sdev: pointer to SCSI device
4798 static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4799 struct scsi_device *sdev)
4801 if (hba->dev_info.f_power_on_wp_en &&
4802 !hba->dev_info.is_lu_power_on_wp) {
4803 u8 b_lu_write_protect;
4805 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4806 &b_lu_write_protect) &&
4807 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4808 hba->dev_info.is_lu_power_on_wp = true;
4813 * ufshcd_slave_alloc - handle initial SCSI device configurations
4814 * @sdev: pointer to SCSI device
4818 static int ufshcd_slave_alloc(struct scsi_device *sdev)
4820 struct ufs_hba *hba;
4822 hba = shost_priv(sdev->host);
4824 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4825 sdev->use_10_for_ms = 1;
4827 /* DBD field should be set to 1 in mode sense(10) */
4828 sdev->set_dbd_for_ms = 1;
4830 /* allow SCSI layer to restart the device in case of errors */
4831 sdev->allow_restart = 1;
4833 /* REPORT SUPPORTED OPERATION CODES is not supported */
4834 sdev->no_report_opcodes = 1;
4836 /* WRITE_SAME command is not supported */
4837 sdev->no_write_same = 1;
4839 ufshcd_set_queue_depth(sdev);
4841 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4847 * ufshcd_change_queue_depth - change queue depth
4848 * @sdev: pointer to SCSI device
4849 * @depth: required depth to set
4851 * Change queue depth and make sure the max. limits are not crossed.
4853 static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
4855 struct ufs_hba *hba = shost_priv(sdev->host);
4857 if (depth > hba->nutrs)
4859 return scsi_change_queue_depth(sdev, depth);
4863 * ufshcd_slave_configure - adjust SCSI device configurations
4864 * @sdev: pointer to SCSI device
4866 static int ufshcd_slave_configure(struct scsi_device *sdev)
4868 struct ufs_hba *hba = shost_priv(sdev->host);
4869 struct request_queue *q = sdev->request_queue;
4871 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
4872 if (hba->quirks & UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE)
4873 blk_queue_update_dma_alignment(q, PAGE_SIZE - 1);
4875 if (ufshcd_is_rpm_autosuspend_allowed(hba))
4876 sdev->rpm_autosuspend = 1;
4878 ufshcd_crypto_setup_rq_keyslot_manager(hba, q);
4884 * ufshcd_slave_destroy - remove SCSI device configurations
4885 * @sdev: pointer to SCSI device
4887 static void ufshcd_slave_destroy(struct scsi_device *sdev)
4889 struct ufs_hba *hba;
4891 hba = shost_priv(sdev->host);
4892 /* Drop the reference as it won't be needed anymore */
4893 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4894 unsigned long flags;
4896 spin_lock_irqsave(hba->host->host_lock, flags);
4897 hba->sdev_ufs_device = NULL;
4898 spin_unlock_irqrestore(hba->host->host_lock, flags);
4903 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4904 * @lrbp: pointer to local reference block of completed command
4905 * @scsi_status: SCSI command status
4907 * Returns value base on SCSI command status
4910 ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4914 switch (scsi_status) {
4915 case SAM_STAT_CHECK_CONDITION:
4916 ufshcd_copy_sense_data(lrbp);
4919 result |= DID_OK << 16 | scsi_status;
4921 case SAM_STAT_TASK_SET_FULL:
4923 case SAM_STAT_TASK_ABORTED:
4924 ufshcd_copy_sense_data(lrbp);
4925 result |= scsi_status;
4928 result |= DID_ERROR << 16;
4930 } /* end of switch */
4936 * ufshcd_transfer_rsp_status - Get overall status of the response
4937 * @hba: per adapter instance
4938 * @lrbp: pointer to local reference block of completed command
4940 * Returns result of the command to notify SCSI midlayer
4943 ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4949 /* overall command status of utrd */
4950 ocs = ufshcd_get_tr_ocs(lrbp);
4952 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) {
4953 if (be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_1) &
4954 MASK_RSP_UPIU_RESULT)
4960 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
4961 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
4963 case UPIU_TRANSACTION_RESPONSE:
4965 * get the response UPIU result to extract
4966 * the SCSI command status
4968 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4971 * get the result based on SCSI status response
4972 * to notify the SCSI midlayer of the command status
4974 scsi_status = result & MASK_SCSI_STATUS;
4975 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
4978 * Currently we are only supporting BKOPs exception
4979 * events hence we can ignore BKOPs exception event
4980 * during power management callbacks. BKOPs exception
4981 * event is not expected to be raised in runtime suspend
4982 * callback as it allows the urgent bkops.
4983 * During system suspend, we are anyway forcefully
4984 * disabling the bkops and if urgent bkops is needed
4985 * it will be enabled on system resume. Long term
4986 * solution could be to abort the system suspend if
4987 * UFS device needs urgent BKOPs.
4989 if (!hba->pm_op_in_progress &&
4990 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr) &&
4991 schedule_work(&hba->eeh_work)) {
4993 * Prevent suspend once eeh_work is scheduled
4994 * to avoid deadlock between ufshcd_suspend
4995 * and exception event handler.
4997 pm_runtime_get_noresume(hba->dev);
5000 case UPIU_TRANSACTION_REJECT_UPIU:
5001 /* TODO: handle Reject UPIU Response */
5002 result = DID_ERROR << 16;
5004 "Reject UPIU not fully implemented\n");
5008 "Unexpected request response code = %x\n",
5010 result = DID_ERROR << 16;
5015 result |= DID_ABORT << 16;
5017 case OCS_INVALID_COMMAND_STATUS:
5018 result |= DID_REQUEUE << 16;
5020 case OCS_INVALID_CMD_TABLE_ATTR:
5021 case OCS_INVALID_PRDT_ATTR:
5022 case OCS_MISMATCH_DATA_BUF_SIZE:
5023 case OCS_MISMATCH_RESP_UPIU_SIZE:
5024 case OCS_PEER_COMM_FAILURE:
5025 case OCS_FATAL_ERROR:
5026 case OCS_DEVICE_FATAL_ERROR:
5027 case OCS_INVALID_CRYPTO_CONFIG:
5028 case OCS_GENERAL_CRYPTO_ERROR:
5030 result |= DID_ERROR << 16;
5032 "OCS error from controller = %x for tag %d\n",
5033 ocs, lrbp->task_tag);
5034 ufshcd_print_evt_hist(hba);
5035 ufshcd_print_host_state(hba);
5037 } /* end of switch */
5039 if ((host_byte(result) != DID_OK) &&
5040 (host_byte(result) != DID_REQUEUE) && !hba->silence_err_logs)
5041 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
5046 * ufshcd_uic_cmd_compl - handle completion of uic command
5047 * @hba: per adapter instance
5048 * @intr_status: interrupt status generated by the controller
5051 * IRQ_HANDLED - If interrupt is valid
5052 * IRQ_NONE - If invalid interrupt
5054 static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
5056 irqreturn_t retval = IRQ_NONE;
5058 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
5059 hba->active_uic_cmd->argument2 |=
5060 ufshcd_get_uic_cmd_result(hba);
5061 hba->active_uic_cmd->argument3 =
5062 ufshcd_get_dme_attr_val(hba);
5063 if (!hba->uic_async_done)
5064 hba->active_uic_cmd->cmd_active = 0;
5065 complete(&hba->active_uic_cmd->done);
5066 retval = IRQ_HANDLED;
5069 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
5070 hba->active_uic_cmd->cmd_active = 0;
5071 complete(hba->uic_async_done);
5072 retval = IRQ_HANDLED;
5075 if (retval == IRQ_HANDLED)
5076 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd,
5082 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
5083 * @hba: per adapter instance
5084 * @completed_reqs: requests to complete
5086 static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
5087 unsigned long completed_reqs)
5089 struct ufshcd_lrb *lrbp;
5090 struct scsi_cmnd *cmd;
5093 bool update_scaling = false;
5095 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
5096 lrbp = &hba->lrb[index];
5097 lrbp->in_use = false;
5098 lrbp->compl_time_stamp = ktime_get();
5101 ufshcd_add_command_trace(hba, index, UFS_CMD_COMP);
5102 result = ufshcd_transfer_rsp_status(hba, lrbp);
5103 scsi_dma_unmap(cmd);
5104 cmd->result = result;
5105 /* Mark completed command as NULL in LRB */
5107 /* Do not touch lrbp after scsi done */
5108 cmd->scsi_done(cmd);
5109 __ufshcd_release(hba);
5110 update_scaling = true;
5111 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
5112 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
5113 if (hba->dev_cmd.complete) {
5114 ufshcd_add_command_trace(hba, index,
5116 complete(hba->dev_cmd.complete);
5117 update_scaling = true;
5120 if (ufshcd_is_clkscaling_supported(hba) && update_scaling)
5121 hba->clk_scaling.active_reqs--;
5124 /* clear corresponding bits of completed commands */
5125 hba->outstanding_reqs ^= completed_reqs;
5127 ufshcd_clk_scaling_update_busy(hba);
5131 * ufshcd_transfer_req_compl - handle SCSI and query command completion
5132 * @hba: per adapter instance
5135 * IRQ_HANDLED - If interrupt is valid
5136 * IRQ_NONE - If invalid interrupt
5138 static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
5140 unsigned long completed_reqs;
5143 /* Resetting interrupt aggregation counters first and reading the
5144 * DOOR_BELL afterward allows us to handle all the completed requests.
5145 * In order to prevent other interrupts starvation the DB is read once
5146 * after reset. The down side of this solution is the possibility of
5147 * false interrupt if device completes another request after resetting
5148 * aggregation and before reading the DB.
5150 if (ufshcd_is_intr_aggr_allowed(hba) &&
5151 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
5152 ufshcd_reset_intr_aggr(hba);
5154 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5155 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
5157 if (completed_reqs) {
5158 __ufshcd_transfer_req_compl(hba, completed_reqs);
5166 * ufshcd_disable_ee - disable exception event
5167 * @hba: per-adapter instance
5168 * @mask: exception event to disable
5170 * Disables exception event in the device so that the EVENT_ALERT
5173 * Returns zero on success, non-zero error value on failure.
5175 static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
5180 if (!(hba->ee_ctrl_mask & mask))
5183 val = hba->ee_ctrl_mask & ~mask;
5184 val &= MASK_EE_STATUS;
5185 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5186 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5188 hba->ee_ctrl_mask &= ~mask;
5194 * ufshcd_enable_ee - enable exception event
5195 * @hba: per-adapter instance
5196 * @mask: exception event to enable
5198 * Enable corresponding exception event in the device to allow
5199 * device to alert host in critical scenarios.
5201 * Returns zero on success, non-zero error value on failure.
5203 static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
5208 if (hba->ee_ctrl_mask & mask)
5211 val = hba->ee_ctrl_mask | mask;
5212 val &= MASK_EE_STATUS;
5213 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5214 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5216 hba->ee_ctrl_mask |= mask;
5222 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5223 * @hba: per-adapter instance
5225 * Allow device to manage background operations on its own. Enabling
5226 * this might lead to inconsistent latencies during normal data transfers
5227 * as the device is allowed to manage its own way of handling background
5230 * Returns zero on success, non-zero on failure.
5232 static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5236 if (hba->auto_bkops_enabled)
5239 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
5240 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
5242 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5247 hba->auto_bkops_enabled = true;
5248 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
5250 /* No need of URGENT_BKOPS exception from the device */
5251 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5253 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5260 * ufshcd_disable_auto_bkops - block device in doing background operations
5261 * @hba: per-adapter instance
5263 * Disabling background operations improves command response latency but
5264 * has drawback of device moving into critical state where the device is
5265 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5266 * host is idle so that BKOPS are managed effectively without any negative
5269 * Returns zero on success, non-zero on failure.
5271 static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5275 if (!hba->auto_bkops_enabled)
5279 * If host assisted BKOPs is to be enabled, make sure
5280 * urgent bkops exception is allowed.
5282 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5284 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5289 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
5290 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL);
5292 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5294 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5298 hba->auto_bkops_enabled = false;
5299 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
5300 hba->is_urgent_bkops_lvl_checked = false;
5306 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
5307 * @hba: per adapter instance
5309 * After a device reset the device may toggle the BKOPS_EN flag
5310 * to default value. The s/w tracking variables should be updated
5311 * as well. This function would change the auto-bkops state based on
5312 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
5314 static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
5316 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5317 hba->auto_bkops_enabled = false;
5318 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5319 ufshcd_enable_auto_bkops(hba);
5321 hba->auto_bkops_enabled = true;
5322 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5323 ufshcd_disable_auto_bkops(hba);
5325 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5326 hba->is_urgent_bkops_lvl_checked = false;
5329 static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5331 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5332 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5336 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5337 * @hba: per-adapter instance
5338 * @status: bkops_status value
5340 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5341 * flag in the device to permit background operations if the device
5342 * bkops_status is greater than or equal to "status" argument passed to
5343 * this function, disable otherwise.
5345 * Returns 0 for success, non-zero in case of failure.
5347 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5348 * to know whether auto bkops is enabled or disabled after this function
5349 * returns control to it.
5351 static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5352 enum bkops_status status)
5355 u32 curr_status = 0;
5357 err = ufshcd_get_bkops_status(hba, &curr_status);
5359 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5362 } else if (curr_status > BKOPS_STATUS_MAX) {
5363 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5364 __func__, curr_status);
5369 if (curr_status >= status)
5370 err = ufshcd_enable_auto_bkops(hba);
5372 err = ufshcd_disable_auto_bkops(hba);
5378 * ufshcd_urgent_bkops - handle urgent bkops exception event
5379 * @hba: per-adapter instance
5381 * Enable fBackgroundOpsEn flag in the device to permit background
5384 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5385 * and negative error value for any other failure.
5387 static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5389 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
5392 static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5394 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5395 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5398 static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5401 u32 curr_status = 0;
5403 if (hba->is_urgent_bkops_lvl_checked)
5404 goto enable_auto_bkops;
5406 err = ufshcd_get_bkops_status(hba, &curr_status);
5408 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5414 * We are seeing that some devices are raising the urgent bkops
5415 * exception events even when BKOPS status doesn't indicate performace
5416 * impacted or critical. Handle these device by determining their urgent
5417 * bkops status at runtime.
5419 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5420 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5421 __func__, curr_status);
5422 /* update the current status as the urgent bkops level */
5423 hba->urgent_bkops_lvl = curr_status;
5424 hba->is_urgent_bkops_lvl_checked = true;
5428 err = ufshcd_enable_auto_bkops(hba);
5431 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5435 int ufshcd_wb_ctrl(struct ufs_hba *hba, bool enable)
5439 enum query_opcode opcode;
5441 if (!ufshcd_is_wb_allowed(hba))
5444 if (!(enable ^ hba->dev_info.wb_enabled))
5447 opcode = UPIU_QUERY_OPCODE_SET_FLAG;
5449 opcode = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5451 index = ufshcd_wb_get_query_index(hba);
5452 ret = ufshcd_query_flag_retry(hba, opcode,
5453 QUERY_FLAG_IDN_WB_EN, index, NULL);
5455 dev_err(hba->dev, "%s write booster %s failed %d\n",
5456 __func__, enable ? "enable" : "disable", ret);
5460 hba->dev_info.wb_enabled = enable;
5461 dev_dbg(hba->dev, "%s write booster %s %d\n",
5462 __func__, enable ? "enable" : "disable", ret);
5467 static int ufshcd_wb_toggle_flush_during_h8(struct ufs_hba *hba, bool set)
5473 val = UPIU_QUERY_OPCODE_SET_FLAG;
5475 val = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5477 index = ufshcd_wb_get_query_index(hba);
5478 return ufshcd_query_flag_retry(hba, val,
5479 QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8,
5483 static inline int ufshcd_wb_toggle_flush(struct ufs_hba *hba, bool enable)
5487 enum query_opcode opcode;
5489 if (!ufshcd_is_wb_allowed(hba) ||
5490 hba->dev_info.wb_buf_flush_enabled == enable)
5494 opcode = UPIU_QUERY_OPCODE_SET_FLAG;
5496 opcode = UPIU_QUERY_OPCODE_CLEAR_FLAG;
5498 index = ufshcd_wb_get_query_index(hba);
5499 ret = ufshcd_query_flag_retry(hba, opcode,
5500 QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN, index,
5503 dev_err(hba->dev, "%s WB-Buf Flush %s failed %d\n", __func__,
5504 enable ? "enable" : "disable", ret);
5508 hba->dev_info.wb_buf_flush_enabled = enable;
5510 dev_dbg(hba->dev, "WB-Buf Flush %s\n", enable ? "enabled" : "disabled");
5516 static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba,
5523 index = ufshcd_wb_get_query_index(hba);
5524 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5525 QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE,
5526 index, 0, &cur_buf);
5528 dev_err(hba->dev, "%s dCurWriteBoosterBufferSize read failed %d\n",
5534 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n",
5538 /* Let it continue to flush when available buffer exceeds threshold */
5539 if (avail_buf < hba->vps->wb_flush_threshold)
5545 static bool ufshcd_wb_need_flush(struct ufs_hba *hba)
5551 if (!ufshcd_is_wb_allowed(hba))
5554 * The ufs device needs the vcc to be ON to flush.
5555 * With user-space reduction enabled, it's enough to enable flush
5556 * by checking only the available buffer. The threshold
5557 * defined here is > 90% full.
5558 * With user-space preserved enabled, the current-buffer
5559 * should be checked too because the wb buffer size can reduce
5560 * when disk tends to be full. This info is provided by current
5561 * buffer (dCurrentWriteBoosterBufferSize). There's no point in
5562 * keeping vcc on when current buffer is empty.
5564 index = ufshcd_wb_get_query_index(hba);
5565 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
5566 QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE,
5567 index, 0, &avail_buf);
5569 dev_warn(hba->dev, "%s dAvailableWriteBoosterBufferSize read failed %d\n",
5574 if (!hba->dev_info.b_presrv_uspc_en) {
5575 if (avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10))
5580 return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf);
5583 static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work)
5585 struct ufs_hba *hba = container_of(to_delayed_work(work),
5587 rpm_dev_flush_recheck_work);
5589 * To prevent unnecessary VCC power drain after device finishes
5590 * WriteBooster buffer flush or Auto BKOPs, force runtime resume
5591 * after a certain delay to recheck the threshold by next runtime
5594 pm_runtime_get_sync(hba->dev);
5595 pm_runtime_put_sync(hba->dev);
5599 * ufshcd_exception_event_handler - handle exceptions raised by device
5600 * @work: pointer to work data
5602 * Read bExceptionEventStatus attribute from the device and handle the
5603 * exception event accordingly.
5605 static void ufshcd_exception_event_handler(struct work_struct *work)
5607 struct ufs_hba *hba;
5610 hba = container_of(work, struct ufs_hba, eeh_work);
5612 pm_runtime_get_sync(hba->dev);
5613 ufshcd_scsi_block_requests(hba);
5614 err = ufshcd_get_ee_status(hba, &status);
5616 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5621 status &= hba->ee_ctrl_mask;
5623 if (status & MASK_EE_URGENT_BKOPS)
5624 ufshcd_bkops_exception_event_handler(hba);
5627 ufshcd_scsi_unblock_requests(hba);
5629 * pm_runtime_get_noresume is called while scheduling
5630 * eeh_work to avoid suspend racing with exception work.
5631 * Hence decrement usage counter using pm_runtime_put_noidle
5632 * to allow suspend on completion of exception event handler.
5634 pm_runtime_put_noidle(hba->dev);
5635 pm_runtime_put(hba->dev);
5639 /* Complete requests that have door-bell cleared */
5640 static void ufshcd_complete_requests(struct ufs_hba *hba)
5642 ufshcd_transfer_req_compl(hba);
5643 ufshcd_tmc_handler(hba);
5647 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5648 * to recover from the DL NAC errors or not.
5649 * @hba: per-adapter instance
5651 * Returns true if error handling is required, false otherwise
5653 static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5655 unsigned long flags;
5656 bool err_handling = true;
5658 spin_lock_irqsave(hba->host->host_lock, flags);
5660 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5661 * device fatal error and/or DL NAC & REPLAY timeout errors.
5663 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5666 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5667 ((hba->saved_err & UIC_ERROR) &&
5668 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
5671 if ((hba->saved_err & UIC_ERROR) &&
5672 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5675 * wait for 50ms to see if we can get any other errors or not.
5677 spin_unlock_irqrestore(hba->host->host_lock, flags);
5679 spin_lock_irqsave(hba->host->host_lock, flags);
5682 * now check if we have got any other severe errors other than
5685 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5686 ((hba->saved_err & UIC_ERROR) &&
5687 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5691 * As DL NAC is the only error received so far, send out NOP
5692 * command to confirm if link is still active or not.
5693 * - If we don't get any response then do error recovery.
5694 * - If we get response then clear the DL NAC error bit.
5697 spin_unlock_irqrestore(hba->host->host_lock, flags);
5698 err = ufshcd_verify_dev_init(hba);
5699 spin_lock_irqsave(hba->host->host_lock, flags);
5704 /* Link seems to be alive hence ignore the DL NAC errors */
5705 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5706 hba->saved_err &= ~UIC_ERROR;
5707 /* clear NAC error */
5708 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5709 if (!hba->saved_uic_err)
5710 err_handling = false;
5713 spin_unlock_irqrestore(hba->host->host_lock, flags);
5714 return err_handling;
5717 /* host lock must be held before calling this func */
5718 static inline bool ufshcd_is_saved_err_fatal(struct ufs_hba *hba)
5720 return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) ||
5721 (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK));
5724 /* host lock must be held before calling this func */
5725 static inline void ufshcd_schedule_eh_work(struct ufs_hba *hba)
5727 /* handle fatal errors only when link is not in error state */
5728 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) {
5729 if (hba->force_reset || ufshcd_is_link_broken(hba) ||
5730 ufshcd_is_saved_err_fatal(hba))
5731 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL;
5733 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL;
5734 queue_work(hba->eh_wq, &hba->eh_work);
5738 static void ufshcd_clk_scaling_allow(struct ufs_hba *hba, bool allow)
5740 down_write(&hba->clk_scaling_lock);
5741 hba->clk_scaling.is_allowed = allow;
5742 up_write(&hba->clk_scaling_lock);
5745 static void ufshcd_clk_scaling_suspend(struct ufs_hba *hba, bool suspend)
5748 if (hba->clk_scaling.is_enabled)
5749 ufshcd_suspend_clkscaling(hba);
5750 ufshcd_clk_scaling_allow(hba, false);
5752 ufshcd_clk_scaling_allow(hba, true);
5753 if (hba->clk_scaling.is_enabled)
5754 ufshcd_resume_clkscaling(hba);
5758 static void ufshcd_err_handling_prepare(struct ufs_hba *hba)
5760 pm_runtime_get_sync(hba->dev);
5761 if (pm_runtime_status_suspended(hba->dev) || hba->is_sys_suspended) {
5762 enum ufs_pm_op pm_op;
5765 * Don't assume anything of pm_runtime_get_sync(), if
5766 * resume fails, irq and clocks can be OFF, and powers
5767 * can be OFF or in LPM.
5769 ufshcd_setup_hba_vreg(hba, true);
5770 ufshcd_enable_irq(hba);
5771 ufshcd_setup_vreg(hba, true);
5772 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5773 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5774 ufshcd_hold(hba, false);
5775 if (!ufshcd_is_clkgating_allowed(hba))
5776 ufshcd_setup_clocks(hba, true);
5777 ufshcd_release(hba);
5778 pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM;
5779 ufshcd_vops_resume(hba, pm_op);
5781 ufshcd_hold(hba, false);
5782 if (ufshcd_is_clkscaling_supported(hba) &&
5783 hba->clk_scaling.is_enabled)
5784 ufshcd_suspend_clkscaling(hba);
5785 ufshcd_clk_scaling_allow(hba, false);
5789 static void ufshcd_err_handling_unprepare(struct ufs_hba *hba)
5791 ufshcd_release(hba);
5792 if (ufshcd_is_clkscaling_supported(hba))
5793 ufshcd_clk_scaling_suspend(hba, false);
5794 pm_runtime_put(hba->dev);
5797 static inline bool ufshcd_err_handling_should_stop(struct ufs_hba *hba)
5799 return (!hba->is_powered || hba->shutting_down ||
5800 hba->ufshcd_state == UFSHCD_STATE_ERROR ||
5801 (!(hba->saved_err || hba->saved_uic_err || hba->force_reset ||
5802 ufshcd_is_link_broken(hba))));
5806 static void ufshcd_recover_pm_error(struct ufs_hba *hba)
5808 struct Scsi_Host *shost = hba->host;
5809 struct scsi_device *sdev;
5810 struct request_queue *q;
5813 hba->is_sys_suspended = false;
5815 * Set RPM status of hba device to RPM_ACTIVE,
5816 * this also clears its runtime error.
5818 ret = pm_runtime_set_active(hba->dev);
5820 * If hba device had runtime error, we also need to resume those
5821 * scsi devices under hba in case any of them has failed to be
5822 * resumed due to hba runtime resume failure. This is to unblock
5823 * blk_queue_enter in case there are bios waiting inside it.
5826 shost_for_each_device(sdev, shost) {
5827 q = sdev->request_queue;
5828 if (q->dev && (q->rpm_status == RPM_SUSPENDED ||
5829 q->rpm_status == RPM_SUSPENDING))
5830 pm_request_resume(q->dev);
5835 static inline void ufshcd_recover_pm_error(struct ufs_hba *hba)
5840 static bool ufshcd_is_pwr_mode_restore_needed(struct ufs_hba *hba)
5842 struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info;
5845 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode);
5847 if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK))
5850 if (pwr_info->pwr_tx != (mode & PWRMODE_MASK))
5857 * ufshcd_err_handler - handle UFS errors that require s/w attention
5858 * @work: pointer to work structure
5860 static void ufshcd_err_handler(struct work_struct *work)
5862 struct ufs_hba *hba;
5863 unsigned long flags;
5864 bool err_xfer = false;
5865 bool err_tm = false;
5866 int err = 0, pmc_err;
5868 bool needs_reset = false, needs_restore = false;
5870 hba = container_of(work, struct ufs_hba, eh_work);
5872 down(&hba->host_sem);
5873 spin_lock_irqsave(hba->host->host_lock, flags);
5874 if (ufshcd_err_handling_should_stop(hba)) {
5875 if (hba->ufshcd_state != UFSHCD_STATE_ERROR)
5876 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5877 spin_unlock_irqrestore(hba->host->host_lock, flags);
5881 ufshcd_set_eh_in_progress(hba);
5882 spin_unlock_irqrestore(hba->host->host_lock, flags);
5883 ufshcd_err_handling_prepare(hba);
5884 spin_lock_irqsave(hba->host->host_lock, flags);
5885 ufshcd_scsi_block_requests(hba);
5886 hba->ufshcd_state = UFSHCD_STATE_RESET;
5888 /* Complete requests that have door-bell cleared by h/w */
5889 ufshcd_complete_requests(hba);
5892 * A full reset and restore might have happened after preparation
5893 * is finished, double check whether we should stop.
5895 if (ufshcd_err_handling_should_stop(hba))
5896 goto skip_err_handling;
5898 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5901 spin_unlock_irqrestore(hba->host->host_lock, flags);
5902 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5903 ret = ufshcd_quirk_dl_nac_errors(hba);
5904 spin_lock_irqsave(hba->host->host_lock, flags);
5905 if (!ret && ufshcd_err_handling_should_stop(hba))
5906 goto skip_err_handling;
5909 if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
5910 (hba->saved_uic_err &&
5911 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
5912 bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR);
5914 spin_unlock_irqrestore(hba->host->host_lock, flags);
5915 ufshcd_print_host_state(hba);
5916 ufshcd_print_pwr_info(hba);
5917 ufshcd_print_evt_hist(hba);
5918 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5919 ufshcd_print_trs(hba, hba->outstanding_reqs, pr_prdt);
5920 spin_lock_irqsave(hba->host->host_lock, flags);
5924 * if host reset is required then skip clearing the pending
5925 * transfers forcefully because they will get cleared during
5926 * host reset and restore
5928 if (hba->force_reset || ufshcd_is_link_broken(hba) ||
5929 ufshcd_is_saved_err_fatal(hba) ||
5930 ((hba->saved_err & UIC_ERROR) &&
5931 (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5932 UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) {
5938 * If LINERESET was caught, UFS might have been put to PWM mode,
5939 * check if power mode restore is needed.
5941 if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) {
5942 hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
5943 if (!hba->saved_uic_err)
5944 hba->saved_err &= ~UIC_ERROR;
5945 spin_unlock_irqrestore(hba->host->host_lock, flags);
5946 if (ufshcd_is_pwr_mode_restore_needed(hba))
5947 needs_restore = true;
5948 spin_lock_irqsave(hba->host->host_lock, flags);
5949 if (!hba->saved_err && !needs_restore)
5950 goto skip_err_handling;
5953 hba->silence_err_logs = true;
5954 /* release lock as clear command might sleep */
5955 spin_unlock_irqrestore(hba->host->host_lock, flags);
5956 /* Clear pending transfer requests */
5957 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5958 if (ufshcd_try_to_abort_task(hba, tag)) {
5960 goto lock_skip_pending_xfer_clear;
5964 /* Clear pending task management requests */
5965 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5966 if (ufshcd_clear_tm_cmd(hba, tag)) {
5968 goto lock_skip_pending_xfer_clear;
5972 lock_skip_pending_xfer_clear:
5973 spin_lock_irqsave(hba->host->host_lock, flags);
5975 /* Complete the requests that are cleared by s/w */
5976 ufshcd_complete_requests(hba);
5977 hba->silence_err_logs = false;
5979 if (err_xfer || err_tm) {
5985 * After all reqs and tasks are cleared from doorbell,
5986 * now it is safe to retore power mode.
5988 if (needs_restore) {
5989 spin_unlock_irqrestore(hba->host->host_lock, flags);
5991 * Hold the scaling lock just in case dev cmds
5992 * are sent via bsg and/or sysfs.
5994 down_write(&hba->clk_scaling_lock);
5995 hba->force_pmc = true;
5996 pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info));
5999 dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n",
6002 hba->force_pmc = false;
6003 ufshcd_print_pwr_info(hba);
6004 up_write(&hba->clk_scaling_lock);
6005 spin_lock_irqsave(hba->host->host_lock, flags);
6009 /* Fatal errors need reset */
6011 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
6014 * ufshcd_reset_and_restore() does the link reinitialization
6015 * which will need atleast one empty doorbell slot to send the
6016 * device management commands (NOP and query commands).
6017 * If there is no slot empty at this moment then free up last
6020 if (hba->outstanding_reqs == max_doorbells)
6021 __ufshcd_transfer_req_compl(hba,
6022 (1UL << (hba->nutrs - 1)));
6024 hba->force_reset = false;
6025 spin_unlock_irqrestore(hba->host->host_lock, flags);
6026 err = ufshcd_reset_and_restore(hba);
6028 dev_err(hba->dev, "%s: reset and restore failed with err %d\n",
6031 ufshcd_recover_pm_error(hba);
6032 spin_lock_irqsave(hba->host->host_lock, flags);
6037 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
6038 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6039 if (hba->saved_err || hba->saved_uic_err)
6040 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
6041 __func__, hba->saved_err, hba->saved_uic_err);
6043 ufshcd_clear_eh_in_progress(hba);
6044 spin_unlock_irqrestore(hba->host->host_lock, flags);
6045 ufshcd_scsi_unblock_requests(hba);
6046 ufshcd_err_handling_unprepare(hba);
6049 if (!err && needs_reset)
6050 ufshcd_clear_ua_wluns(hba);
6054 * ufshcd_update_uic_error - check and set fatal UIC error flags.
6055 * @hba: per-adapter instance
6058 * IRQ_HANDLED - If interrupt is valid
6059 * IRQ_NONE - If invalid interrupt
6061 static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
6064 irqreturn_t retval = IRQ_NONE;
6066 /* PHY layer error */
6067 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
6068 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
6069 (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) {
6070 ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg);
6072 * To know whether this error is fatal or not, DB timeout
6073 * must be checked but this error is handled separately.
6075 if (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)
6076 dev_dbg(hba->dev, "%s: UIC Lane error reported\n",
6079 /* Got a LINERESET indication. */
6080 if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) {
6081 struct uic_command *cmd = NULL;
6083 hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR;
6084 if (hba->uic_async_done && hba->active_uic_cmd)
6085 cmd = hba->active_uic_cmd;
6087 * Ignore the LINERESET during power mode change
6088 * operation via DME_SET command.
6090 if (cmd && (cmd->command == UIC_CMD_DME_SET))
6091 hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR;
6093 retval |= IRQ_HANDLED;
6096 /* PA_INIT_ERROR is fatal and needs UIC reset */
6097 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
6098 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
6099 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
6100 ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg);
6102 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
6103 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
6104 else if (hba->dev_quirks &
6105 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
6106 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
6108 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
6109 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
6110 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
6112 retval |= IRQ_HANDLED;
6115 /* UIC NL/TL/DME errors needs software retry */
6116 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
6117 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
6118 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
6119 ufshcd_update_evt_hist(hba, UFS_EVT_NL_ERR, reg);
6120 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
6121 retval |= IRQ_HANDLED;
6124 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
6125 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
6126 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
6127 ufshcd_update_evt_hist(hba, UFS_EVT_TL_ERR, reg);
6128 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
6129 retval |= IRQ_HANDLED;
6132 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
6133 if ((reg & UIC_DME_ERROR) &&
6134 (reg & UIC_DME_ERROR_CODE_MASK)) {
6135 ufshcd_update_evt_hist(hba, UFS_EVT_DME_ERR, reg);
6136 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
6137 retval |= IRQ_HANDLED;
6140 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
6141 __func__, hba->uic_error);
6145 static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
6148 if (!ufshcd_is_auto_hibern8_supported(hba) ||
6149 !ufshcd_is_auto_hibern8_enabled(hba))
6152 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
6155 if (hba->active_uic_cmd &&
6156 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
6157 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
6164 * ufshcd_check_errors - Check for errors that need s/w attention
6165 * @hba: per-adapter instance
6168 * IRQ_HANDLED - If interrupt is valid
6169 * IRQ_NONE - If invalid interrupt
6171 static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba)
6173 bool queue_eh_work = false;
6174 irqreturn_t retval = IRQ_NONE;
6176 if (hba->errors & INT_FATAL_ERRORS) {
6177 ufshcd_update_evt_hist(hba, UFS_EVT_FATAL_ERR,
6179 queue_eh_work = true;
6182 if (hba->errors & UIC_ERROR) {
6184 retval = ufshcd_update_uic_error(hba);
6186 queue_eh_work = true;
6189 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
6191 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
6192 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
6194 hba->errors, ufshcd_get_upmcrs(hba));
6195 ufshcd_update_evt_hist(hba, UFS_EVT_AUTO_HIBERN8_ERR,
6197 ufshcd_set_link_broken(hba);
6198 queue_eh_work = true;
6201 if (queue_eh_work) {
6203 * update the transfer error masks to sticky bits, let's do this
6204 * irrespective of current ufshcd_state.
6206 hba->saved_err |= hba->errors;
6207 hba->saved_uic_err |= hba->uic_error;
6209 /* dump controller state before resetting */
6210 if ((hba->saved_err &
6211 (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) ||
6212 (hba->saved_uic_err &&
6213 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) {
6214 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
6215 __func__, hba->saved_err,
6216 hba->saved_uic_err);
6217 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE,
6219 ufshcd_print_pwr_info(hba);
6221 ufshcd_schedule_eh_work(hba);
6222 retval |= IRQ_HANDLED;
6225 * if (!queue_eh_work) -
6226 * Other errors are either non-fatal where host recovers
6227 * itself without s/w intervention or errors that will be
6228 * handled by the SCSI core layer.
6234 struct ufs_hba *hba;
6235 unsigned long pending;
6239 static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved)
6241 struct ctm_info *const ci = priv;
6242 struct completion *c;
6244 WARN_ON_ONCE(reserved);
6245 if (test_bit(req->tag, &ci->pending))
6248 c = req->end_io_data;
6255 * ufshcd_tmc_handler - handle task management function completion
6256 * @hba: per adapter instance
6259 * IRQ_HANDLED - If interrupt is valid
6260 * IRQ_NONE - If invalid interrupt
6262 static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
6264 struct request_queue *q = hba->tmf_queue;
6265 struct ctm_info ci = {
6267 .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL),
6270 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci);
6271 return ci.ncpl ? IRQ_HANDLED : IRQ_NONE;
6275 * ufshcd_sl_intr - Interrupt service routine
6276 * @hba: per adapter instance
6277 * @intr_status: contains interrupts generated by the controller
6280 * IRQ_HANDLED - If interrupt is valid
6281 * IRQ_NONE - If invalid interrupt
6283 static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
6285 irqreturn_t retval = IRQ_NONE;
6287 hba->errors = UFSHCD_ERROR_MASK & intr_status;
6289 if (ufshcd_is_auto_hibern8_error(hba, intr_status))
6290 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
6293 retval |= ufshcd_check_errors(hba);
6295 if (intr_status & UFSHCD_UIC_MASK)
6296 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
6298 if (intr_status & UTP_TASK_REQ_COMPL)
6299 retval |= ufshcd_tmc_handler(hba);
6301 if (intr_status & UTP_TRANSFER_REQ_COMPL)
6302 retval |= ufshcd_transfer_req_compl(hba);
6308 * ufshcd_intr - Main interrupt service routine
6310 * @__hba: pointer to adapter instance
6313 * IRQ_HANDLED - If interrupt is valid
6314 * IRQ_NONE - If invalid interrupt
6316 static irqreturn_t ufshcd_intr(int irq, void *__hba)
6318 u32 intr_status, enabled_intr_status = 0;
6319 irqreturn_t retval = IRQ_NONE;
6320 struct ufs_hba *hba = __hba;
6321 int retries = hba->nutrs;
6323 spin_lock(hba->host->host_lock);
6324 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
6325 hba->ufs_stats.last_intr_status = intr_status;
6326 hba->ufs_stats.last_intr_ts = ktime_get();
6329 * There could be max of hba->nutrs reqs in flight and in worst case
6330 * if the reqs get finished 1 by 1 after the interrupt status is
6331 * read, make sure we handle them by checking the interrupt status
6332 * again in a loop until we process all of the reqs before returning.
6334 while (intr_status && retries--) {
6335 enabled_intr_status =
6336 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
6337 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
6338 if (enabled_intr_status)
6339 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
6341 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
6344 if (enabled_intr_status && retval == IRQ_NONE &&
6345 !ufshcd_eh_in_progress(hba)) {
6346 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x (0x%08x, 0x%08x)\n",
6349 hba->ufs_stats.last_intr_status,
6350 enabled_intr_status);
6351 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
6354 spin_unlock(hba->host->host_lock);
6358 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
6361 u32 mask = 1 << tag;
6362 unsigned long flags;
6364 if (!test_bit(tag, &hba->outstanding_tasks))
6367 spin_lock_irqsave(hba->host->host_lock, flags);
6368 ufshcd_utmrl_clear(hba, tag);
6369 spin_unlock_irqrestore(hba->host->host_lock, flags);
6371 /* poll for max. 1 sec to clear door bell register by h/w */
6372 err = ufshcd_wait_for_register(hba,
6373 REG_UTP_TASK_REQ_DOOR_BELL,
6374 mask, 0, 1000, 1000);
6379 static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
6380 struct utp_task_req_desc *treq, u8 tm_function)
6382 struct request_queue *q = hba->tmf_queue;
6383 struct Scsi_Host *host = hba->host;
6384 DECLARE_COMPLETION_ONSTACK(wait);
6385 struct request *req;
6386 unsigned long flags;
6387 int free_slot, task_tag, err;
6390 * Get free slot, sleep if slots are unavailable.
6391 * Even though we use wait_event() which sleeps indefinitely,
6392 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
6394 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
6396 return PTR_ERR(req);
6398 req->end_io_data = &wait;
6399 free_slot = req->tag;
6400 WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs);
6401 ufshcd_hold(hba, false);
6403 spin_lock_irqsave(host->host_lock, flags);
6404 task_tag = hba->nutrs + free_slot;
6406 treq->req_header.dword_0 |= cpu_to_be32(task_tag);
6408 memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq));
6409 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
6411 /* send command to the controller */
6412 __set_bit(free_slot, &hba->outstanding_tasks);
6414 /* Make sure descriptors are ready before ringing the task doorbell */
6417 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
6418 /* Make sure that doorbell is committed immediately */
6421 spin_unlock_irqrestore(host->host_lock, flags);
6423 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_SEND);
6425 /* wait until the task management command is completed */
6426 err = wait_for_completion_io_timeout(&wait,
6427 msecs_to_jiffies(TM_CMD_TIMEOUT));
6430 * Make sure that ufshcd_compl_tm() does not trigger a
6433 req->end_io_data = NULL;
6434 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR);
6435 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6436 __func__, tm_function);
6437 if (ufshcd_clear_tm_cmd(hba, free_slot))
6438 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
6439 __func__, free_slot);
6443 memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq));
6445 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_COMP);
6448 spin_lock_irqsave(hba->host->host_lock, flags);
6449 __clear_bit(free_slot, &hba->outstanding_tasks);
6450 spin_unlock_irqrestore(hba->host->host_lock, flags);
6452 blk_put_request(req);
6454 ufshcd_release(hba);
6459 * ufshcd_issue_tm_cmd - issues task management commands to controller
6460 * @hba: per adapter instance
6461 * @lun_id: LUN ID to which TM command is sent
6462 * @task_id: task ID to which the TM command is applicable
6463 * @tm_function: task management function opcode
6464 * @tm_response: task management service response return value
6466 * Returns non-zero value on error, zero on success.
6468 static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
6469 u8 tm_function, u8 *tm_response)
6471 struct utp_task_req_desc treq = { { 0 }, };
6474 /* Configure task request descriptor */
6475 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6476 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6478 /* Configure task request UPIU */
6479 treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) |
6480 cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24);
6481 treq.req_header.dword_1 = cpu_to_be32(tm_function << 16);
6484 * The host shall provide the same value for LUN field in the basic
6485 * header and for Input Parameter.
6487 treq.input_param1 = cpu_to_be32(lun_id);
6488 treq.input_param2 = cpu_to_be32(task_id);
6490 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
6491 if (err == -ETIMEDOUT)
6494 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6495 if (ocs_value != OCS_SUCCESS)
6496 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
6497 __func__, ocs_value);
6498 else if (tm_response)
6499 *tm_response = be32_to_cpu(treq.output_param1) &
6500 MASK_TM_SERVICE_RESP;
6505 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
6506 * @hba: per-adapter instance
6507 * @req_upiu: upiu request
6508 * @rsp_upiu: upiu reply
6509 * @desc_buff: pointer to descriptor buffer, NULL if NA
6510 * @buff_len: descriptor size, 0 if NA
6511 * @cmd_type: specifies the type (NOP, Query...)
6512 * @desc_op: descriptor operation
6514 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
6515 * Therefore, it "rides" the device management infrastructure: uses its tag and
6516 * tasks work queues.
6518 * Since there is only one available tag for device management commands,
6519 * the caller is expected to hold the hba->dev_cmd.lock mutex.
6521 static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
6522 struct utp_upiu_req *req_upiu,
6523 struct utp_upiu_req *rsp_upiu,
6524 u8 *desc_buff, int *buff_len,
6525 enum dev_cmd_type cmd_type,
6526 enum query_opcode desc_op)
6528 struct request_queue *q = hba->cmd_queue;
6529 struct request *req;
6530 struct ufshcd_lrb *lrbp;
6533 struct completion wait;
6534 unsigned long flags;
6537 down_read(&hba->clk_scaling_lock);
6539 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
6545 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
6547 init_completion(&wait);
6548 lrbp = &hba->lrb[tag];
6549 if (unlikely(lrbp->in_use)) {
6556 lrbp->sense_bufflen = 0;
6557 lrbp->sense_buffer = NULL;
6558 lrbp->task_tag = tag;
6560 lrbp->intr_cmd = true;
6561 ufshcd_prepare_lrbp_crypto(NULL, lrbp);
6562 hba->dev_cmd.type = cmd_type;
6564 switch (hba->ufs_version) {
6565 case UFSHCI_VERSION_10:
6566 case UFSHCI_VERSION_11:
6567 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
6570 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
6574 /* update the task tag in the request upiu */
6575 req_upiu->header.dword_0 |= cpu_to_be32(tag);
6577 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
6579 /* just copy the upiu request as it is */
6580 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
6581 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
6582 /* The Data Segment Area is optional depending upon the query
6583 * function value. for WRITE DESCRIPTOR, the data segment
6584 * follows right after the tsf.
6586 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
6590 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
6592 hba->dev_cmd.complete = &wait;
6594 /* Make sure descriptors are ready before ringing the doorbell */
6596 spin_lock_irqsave(hba->host->host_lock, flags);
6597 ufshcd_send_command(hba, tag);
6598 spin_unlock_irqrestore(hba->host->host_lock, flags);
6601 * ignore the returning value here - ufshcd_check_query_response is
6602 * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
6603 * read the response directly ignoring all errors.
6605 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT);
6607 /* just copy the upiu response as it is */
6608 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
6609 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
6610 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
6611 u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
6612 MASK_QUERY_DATA_SEG_LEN;
6614 if (*buff_len >= resp_len) {
6615 memcpy(desc_buff, descp, resp_len);
6616 *buff_len = resp_len;
6619 "%s: rsp size %d is bigger than buffer size %d",
6620 __func__, resp_len, *buff_len);
6627 blk_put_request(req);
6629 up_read(&hba->clk_scaling_lock);
6634 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
6635 * @hba: per-adapter instance
6636 * @req_upiu: upiu request
6637 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
6638 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
6639 * @desc_buff: pointer to descriptor buffer, NULL if NA
6640 * @buff_len: descriptor size, 0 if NA
6641 * @desc_op: descriptor operation
6643 * Supports UTP Transfer requests (nop and query), and UTP Task
6644 * Management requests.
6645 * It is up to the caller to fill the upiu conent properly, as it will
6646 * be copied without any further input validations.
6648 int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
6649 struct utp_upiu_req *req_upiu,
6650 struct utp_upiu_req *rsp_upiu,
6652 u8 *desc_buff, int *buff_len,
6653 enum query_opcode desc_op)
6656 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
6657 struct utp_task_req_desc treq = { { 0 }, };
6659 u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC;
6662 case UPIU_TRANSACTION_NOP_OUT:
6663 cmd_type = DEV_CMD_TYPE_NOP;
6665 case UPIU_TRANSACTION_QUERY_REQ:
6666 ufshcd_hold(hba, false);
6667 mutex_lock(&hba->dev_cmd.lock);
6668 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
6669 desc_buff, buff_len,
6671 mutex_unlock(&hba->dev_cmd.lock);
6672 ufshcd_release(hba);
6675 case UPIU_TRANSACTION_TASK_REQ:
6676 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6677 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6679 memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu));
6681 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
6682 if (err == -ETIMEDOUT)
6685 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6686 if (ocs_value != OCS_SUCCESS) {
6687 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
6692 memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu));
6705 * ufshcd_eh_device_reset_handler - device reset handler registered to
6707 * @cmd: SCSI command pointer
6709 * Returns SUCCESS/FAILED
6711 static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
6713 struct Scsi_Host *host;
6714 struct ufs_hba *hba;
6718 unsigned long flags;
6720 host = cmd->device->host;
6721 hba = shost_priv(host);
6723 lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
6724 err = ufshcd_issue_tm_cmd(hba, lun, 0, UFS_LOGICAL_RESET, &resp);
6725 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6731 /* clear the commands that were pending for corresponding LUN */
6732 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6733 if (hba->lrb[pos].lun == lun) {
6734 err = ufshcd_clear_cmd(hba, pos);
6739 spin_lock_irqsave(host->host_lock, flags);
6740 ufshcd_transfer_req_compl(hba);
6741 spin_unlock_irqrestore(host->host_lock, flags);
6744 hba->req_abort_count = 0;
6745 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, (u32)err);
6749 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6755 static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6757 struct ufshcd_lrb *lrbp;
6760 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6761 lrbp = &hba->lrb[tag];
6762 lrbp->req_abort_skip = true;
6767 * ufshcd_try_to_abort_task - abort a specific task
6768 * @hba: Pointer to adapter instance
6769 * @tag: Task tag/index to be aborted
6771 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6772 * command, and in host controller by clearing the door-bell register. There can
6773 * be race between controller sending the command to the device while abort is
6774 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6775 * really issued and then try to abort it.
6777 * Returns zero on success, non-zero on failure
6779 static int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag)
6781 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
6787 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6788 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6789 UFS_QUERY_TASK, &resp);
6790 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6791 /* cmd pending in the device */
6792 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
6795 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6797 * cmd not pending in the device, check if it is
6800 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
6802 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6803 if (reg & (1 << tag)) {
6804 /* sleep for max. 200us to stabilize */
6805 usleep_range(100, 200);
6808 /* command completed already */
6809 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
6814 "%s: no response from device. tag = %d, err %d\n",
6815 __func__, tag, err);
6817 err = resp; /* service response error */
6827 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6828 UFS_ABORT_TASK, &resp);
6829 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
6831 err = resp; /* service response error */
6832 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
6833 __func__, tag, err);
6838 err = ufshcd_clear_cmd(hba, tag);
6840 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
6841 __func__, tag, err);
6848 * ufshcd_abort - scsi host template eh_abort_handler callback
6849 * @cmd: SCSI command pointer
6851 * Returns SUCCESS/FAILED
6853 static int ufshcd_abort(struct scsi_cmnd *cmd)
6855 struct Scsi_Host *host;
6856 struct ufs_hba *hba;
6857 unsigned long flags;
6860 struct ufshcd_lrb *lrbp;
6863 host = cmd->device->host;
6864 hba = shost_priv(host);
6865 tag = cmd->request->tag;
6866 lrbp = &hba->lrb[tag];
6867 if (!ufshcd_valid_tag(hba, tag)) {
6869 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6870 __func__, tag, cmd, cmd->request);
6874 ufshcd_hold(hba, false);
6875 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6876 /* If command is already aborted/completed, return SUCCESS */
6877 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6879 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6880 __func__, tag, hba->outstanding_reqs, reg);
6884 /* Print Transfer Request of aborted task */
6885 dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
6888 * Print detailed info about aborted request.
6889 * As more than one request might get aborted at the same time,
6890 * print full information only for the first aborted request in order
6891 * to reduce repeated printouts. For other aborted requests only print
6894 scsi_print_command(cmd);
6895 if (!hba->req_abort_count) {
6896 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag);
6897 ufshcd_print_evt_hist(hba);
6898 ufshcd_print_host_state(hba);
6899 ufshcd_print_pwr_info(hba);
6900 ufshcd_print_trs(hba, 1 << tag, true);
6902 ufshcd_print_trs(hba, 1 << tag, false);
6904 hba->req_abort_count++;
6906 if (!(reg & (1 << tag))) {
6908 "%s: cmd was completed, but without a notifying intr, tag = %d",
6914 * Task abort to the device W-LUN is illegal. When this command
6915 * will fail, due to spec violation, scsi err handling next step
6916 * will be to send LU reset which, again, is a spec violation.
6917 * To avoid these unnecessary/illegal steps, first we clean up
6918 * the lrb taken by this cmd and mark the lrb as in_use, then
6919 * queue the eh_work and bail.
6921 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) {
6922 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun);
6923 spin_lock_irqsave(host->host_lock, flags);
6925 __ufshcd_transfer_req_compl(hba, (1UL << tag));
6926 __set_bit(tag, &hba->outstanding_reqs);
6927 lrbp->in_use = true;
6928 hba->force_reset = true;
6929 ufshcd_schedule_eh_work(hba);
6932 spin_unlock_irqrestore(host->host_lock, flags);
6936 /* Skip task abort in case previous aborts failed and report failure */
6937 if (lrbp->req_abort_skip)
6940 err = ufshcd_try_to_abort_task(hba, tag);
6944 spin_lock_irqsave(host->host_lock, flags);
6945 __ufshcd_transfer_req_compl(hba, (1UL << tag));
6946 spin_unlock_irqrestore(host->host_lock, flags);
6950 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6951 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
6956 * This ufshcd_release() corresponds to the original scsi cmd that got
6957 * aborted here (as we won't get any IRQ for it).
6959 ufshcd_release(hba);
6964 * ufshcd_host_reset_and_restore - reset and restore host controller
6965 * @hba: per-adapter instance
6967 * Note that host controller reset may issue DME_RESET to
6968 * local and remote (device) Uni-Pro stack and the attributes
6969 * are reset to default state.
6971 * Returns zero on success, non-zero on failure
6973 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6976 unsigned long flags;
6979 * Stop the host controller and complete the requests
6982 ufshcd_hba_stop(hba);
6984 spin_lock_irqsave(hba->host->host_lock, flags);
6985 hba->silence_err_logs = true;
6986 ufshcd_complete_requests(hba);
6987 hba->silence_err_logs = false;
6988 spin_unlock_irqrestore(hba->host->host_lock, flags);
6990 /* scale up clocks to max frequency before full reinitialization */
6991 ufshcd_set_clk_freq(hba, true);
6993 err = ufshcd_hba_enable(hba);
6995 /* Establish the link again and restore the device */
6997 err = ufshcd_probe_hba(hba, false);
7000 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
7001 ufshcd_update_evt_hist(hba, UFS_EVT_HOST_RESET, (u32)err);
7006 * ufshcd_reset_and_restore - reset and re-initialize host/device
7007 * @hba: per-adapter instance
7009 * Reset and recover device, host and re-establish link. This
7010 * is helpful to recover the communication in fatal error conditions.
7012 * Returns zero on success, non-zero on failure
7014 static int ufshcd_reset_and_restore(struct ufs_hba *hba)
7019 unsigned long flags;
7020 int retries = MAX_HOST_RESET_RETRIES;
7023 * This is a fresh start, cache and clear saved error first,
7024 * in case new error generated during reset and restore.
7026 spin_lock_irqsave(hba->host->host_lock, flags);
7027 saved_err = hba->saved_err;
7028 saved_uic_err = hba->saved_uic_err;
7030 hba->saved_uic_err = 0;
7031 spin_unlock_irqrestore(hba->host->host_lock, flags);
7034 /* Reset the attached device */
7035 ufshcd_device_reset(hba);
7037 err = ufshcd_host_reset_and_restore(hba);
7038 } while (err && --retries);
7040 spin_lock_irqsave(hba->host->host_lock, flags);
7042 * Inform scsi mid-layer that we did reset and allow to handle
7043 * Unit Attention properly.
7045 scsi_report_bus_reset(hba->host, 0);
7047 hba->ufshcd_state = UFSHCD_STATE_ERROR;
7048 hba->saved_err |= saved_err;
7049 hba->saved_uic_err |= saved_uic_err;
7051 spin_unlock_irqrestore(hba->host->host_lock, flags);
7057 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
7058 * @cmd: SCSI command pointer
7060 * Returns SUCCESS/FAILED
7062 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
7065 unsigned long flags;
7066 struct ufs_hba *hba;
7068 hba = shost_priv(cmd->device->host);
7070 spin_lock_irqsave(hba->host->host_lock, flags);
7071 hba->force_reset = true;
7072 ufshcd_schedule_eh_work(hba);
7073 dev_err(hba->dev, "%s: reset in progress - 1\n", __func__);
7074 spin_unlock_irqrestore(hba->host->host_lock, flags);
7076 flush_work(&hba->eh_work);
7078 spin_lock_irqsave(hba->host->host_lock, flags);
7079 if (hba->ufshcd_state == UFSHCD_STATE_ERROR)
7081 spin_unlock_irqrestore(hba->host->host_lock, flags);
7087 * ufshcd_get_max_icc_level - calculate the ICC level
7088 * @sup_curr_uA: max. current supported by the regulator
7089 * @start_scan: row at the desc table to start scan from
7090 * @buff: power descriptor buffer
7092 * Returns calculated max ICC level for specific regulator
7094 static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
7101 for (i = start_scan; i >= 0; i--) {
7102 data = be16_to_cpup((__be16 *)&buff[2 * i]);
7103 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
7104 ATTR_ICC_LVL_UNIT_OFFSET;
7105 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
7107 case UFSHCD_NANO_AMP:
7108 curr_uA = curr_uA / 1000;
7110 case UFSHCD_MILI_AMP:
7111 curr_uA = curr_uA * 1000;
7114 curr_uA = curr_uA * 1000 * 1000;
7116 case UFSHCD_MICRO_AMP:
7120 if (sup_curr_uA >= curr_uA)
7125 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
7132 * ufshcd_calc_icc_level - calculate the max ICC level
7133 * In case regulators are not initialized we'll return 0
7134 * @hba: per-adapter instance
7135 * @desc_buf: power descriptor buffer to extract ICC levels from.
7136 * @len: length of desc_buff
7138 * Returns calculated ICC level
7140 static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
7141 u8 *desc_buf, int len)
7145 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
7146 !hba->vreg_info.vccq2) {
7148 "%s: Regulator capability was not set, actvIccLevel=%d",
7149 __func__, icc_level);
7153 if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA)
7154 icc_level = ufshcd_get_max_icc_level(
7155 hba->vreg_info.vcc->max_uA,
7156 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
7157 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
7159 if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA)
7160 icc_level = ufshcd_get_max_icc_level(
7161 hba->vreg_info.vccq->max_uA,
7163 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
7165 if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA)
7166 icc_level = ufshcd_get_max_icc_level(
7167 hba->vreg_info.vccq2->max_uA,
7169 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
7174 static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
7177 int buff_len = hba->desc_size[QUERY_DESC_IDN_POWER];
7181 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7185 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0,
7186 desc_buf, buff_len);
7189 "%s: Failed reading power descriptor.len = %d ret = %d",
7190 __func__, buff_len, ret);
7194 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf,
7196 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
7198 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7199 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
7203 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
7204 __func__, icc_level, ret);
7210 static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev)
7212 scsi_autopm_get_device(sdev);
7213 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev);
7214 if (sdev->rpm_autosuspend)
7215 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev,
7216 RPM_AUTOSUSPEND_DELAY_MS);
7217 scsi_autopm_put_device(sdev);
7221 * ufshcd_scsi_add_wlus - Adds required W-LUs
7222 * @hba: per-adapter instance
7224 * UFS device specification requires the UFS devices to support 4 well known
7226 * "REPORT_LUNS" (address: 01h)
7227 * "UFS Device" (address: 50h)
7228 * "RPMB" (address: 44h)
7229 * "BOOT" (address: 30h)
7230 * UFS device's power management needs to be controlled by "POWER CONDITION"
7231 * field of SSU (START STOP UNIT) command. But this "power condition" field
7232 * will take effect only when its sent to "UFS device" well known logical unit
7233 * hence we require the scsi_device instance to represent this logical unit in
7234 * order for the UFS host driver to send the SSU command for power management.
7236 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
7237 * Block) LU so user space process can control this LU. User space may also
7238 * want to have access to BOOT LU.
7240 * This function adds scsi device instances for each of all well known LUs
7241 * (except "REPORT LUNS" LU).
7243 * Returns zero on success (all required W-LUs are added successfully),
7244 * non-zero error value on failure (if failed to add any of the required W-LU).
7246 static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
7249 struct scsi_device *sdev_boot;
7251 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
7252 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
7253 if (IS_ERR(hba->sdev_ufs_device)) {
7254 ret = PTR_ERR(hba->sdev_ufs_device);
7255 hba->sdev_ufs_device = NULL;
7258 ufshcd_blk_pm_runtime_init(hba->sdev_ufs_device);
7259 scsi_device_put(hba->sdev_ufs_device);
7261 hba->sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
7262 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
7263 if (IS_ERR(hba->sdev_rpmb)) {
7264 ret = PTR_ERR(hba->sdev_rpmb);
7265 goto remove_sdev_ufs_device;
7267 ufshcd_blk_pm_runtime_init(hba->sdev_rpmb);
7268 scsi_device_put(hba->sdev_rpmb);
7270 sdev_boot = __scsi_add_device(hba->host, 0, 0,
7271 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
7272 if (IS_ERR(sdev_boot)) {
7273 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
7275 ufshcd_blk_pm_runtime_init(sdev_boot);
7276 scsi_device_put(sdev_boot);
7280 remove_sdev_ufs_device:
7281 scsi_remove_device(hba->sdev_ufs_device);
7286 static void ufshcd_wb_probe(struct ufs_hba *hba, u8 *desc_buf)
7288 struct ufs_dev_info *dev_info = &hba->dev_info;
7290 u32 d_lu_wb_buf_alloc;
7291 u32 ext_ufs_feature;
7293 if (!ufshcd_is_wb_allowed(hba))
7296 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or
7297 * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES
7300 if (!(dev_info->wspecversion >= 0x310 ||
7301 dev_info->wspecversion == 0x220 ||
7302 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES)))
7305 if (hba->desc_size[QUERY_DESC_IDN_DEVICE] <
7306 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP + 4)
7309 ext_ufs_feature = get_unaligned_be32(desc_buf +
7310 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP);
7312 if (!(ext_ufs_feature & UFS_DEV_WRITE_BOOSTER_SUP))
7316 * WB may be supported but not configured while provisioning. The spec
7317 * says, in dedicated wb buffer mode, a max of 1 lun would have wb
7318 * buffer configured.
7320 dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE];
7322 dev_info->b_presrv_uspc_en =
7323 desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN];
7325 if (dev_info->wb_buffer_type == WB_BUF_MODE_SHARED) {
7326 if (!get_unaligned_be32(desc_buf +
7327 DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS))
7330 for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) {
7331 d_lu_wb_buf_alloc = 0;
7332 ufshcd_read_unit_desc_param(hba,
7334 UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS,
7335 (u8 *)&d_lu_wb_buf_alloc,
7336 sizeof(d_lu_wb_buf_alloc));
7337 if (d_lu_wb_buf_alloc) {
7338 dev_info->wb_dedicated_lu = lun;
7343 if (!d_lu_wb_buf_alloc)
7349 hba->caps &= ~UFSHCD_CAP_WB_EN;
7352 void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, struct ufs_dev_fix *fixups)
7354 struct ufs_dev_fix *f;
7355 struct ufs_dev_info *dev_info = &hba->dev_info;
7360 for (f = fixups; f->quirk; f++) {
7361 if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
7362 f->wmanufacturerid == UFS_ANY_VENDOR) &&
7363 ((dev_info->model &&
7364 STR_PRFX_EQUAL(f->model, dev_info->model)) ||
7365 !strcmp(f->model, UFS_ANY_MODEL)))
7366 hba->dev_quirks |= f->quirk;
7369 EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks);
7371 static void ufs_fixup_device_setup(struct ufs_hba *hba)
7373 /* fix by general quirk table */
7374 ufshcd_fixup_dev_quirks(hba, ufs_fixups);
7376 /* allow vendors to fix quirks */
7377 ufshcd_vops_fixup_dev_quirks(hba);
7380 static int ufs_get_device_desc(struct ufs_hba *hba)
7385 struct ufs_dev_info *dev_info = &hba->dev_info;
7387 desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
7393 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
7394 hba->desc_size[QUERY_DESC_IDN_DEVICE]);
7396 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
7402 * getting vendor (manufacturerID) and Bank Index in big endian
7405 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
7406 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
7408 /* getting Specification Version in big endian format */
7409 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 |
7410 desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1];
7412 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
7414 err = ufshcd_read_string_desc(hba, model_index,
7415 &dev_info->model, SD_ASCII_STD);
7417 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
7422 ufs_fixup_device_setup(hba);
7424 ufshcd_wb_probe(hba, desc_buf);
7427 * ufshcd_read_string_desc returns size of the string
7428 * reset the error value
7437 static void ufs_put_device_desc(struct ufs_hba *hba)
7439 struct ufs_dev_info *dev_info = &hba->dev_info;
7441 kfree(dev_info->model);
7442 dev_info->model = NULL;
7446 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
7447 * @hba: per-adapter instance
7449 * PA_TActivate parameter can be tuned manually if UniPro version is less than
7450 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
7451 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
7452 * the hibern8 exit latency.
7454 * Returns zero on success, non-zero error value on failure.
7456 static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
7459 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
7461 ret = ufshcd_dme_peer_get(hba,
7463 RX_MIN_ACTIVATETIME_CAPABILITY,
7464 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7465 &peer_rx_min_activatetime);
7469 /* make sure proper unit conversion is applied */
7470 tuned_pa_tactivate =
7471 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
7472 / PA_TACTIVATE_TIME_UNIT_US);
7473 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7474 tuned_pa_tactivate);
7481 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
7482 * @hba: per-adapter instance
7484 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
7485 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
7486 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
7487 * This optimal value can help reduce the hibern8 exit latency.
7489 * Returns zero on success, non-zero error value on failure.
7491 static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
7494 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
7495 u32 max_hibern8_time, tuned_pa_hibern8time;
7497 ret = ufshcd_dme_get(hba,
7498 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
7499 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
7500 &local_tx_hibern8_time_cap);
7504 ret = ufshcd_dme_peer_get(hba,
7505 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
7506 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
7507 &peer_rx_hibern8_time_cap);
7511 max_hibern8_time = max(local_tx_hibern8_time_cap,
7512 peer_rx_hibern8_time_cap);
7513 /* make sure proper unit conversion is applied */
7514 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
7515 / PA_HIBERN8_TIME_UNIT_US);
7516 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
7517 tuned_pa_hibern8time);
7523 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
7524 * less than device PA_TACTIVATE time.
7525 * @hba: per-adapter instance
7527 * Some UFS devices require host PA_TACTIVATE to be lower than device
7528 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
7531 * Returns zero on success, non-zero error value on failure.
7533 static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
7536 u32 granularity, peer_granularity;
7537 u32 pa_tactivate, peer_pa_tactivate;
7538 u32 pa_tactivate_us, peer_pa_tactivate_us;
7539 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
7541 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7546 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
7551 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
7552 (granularity > PA_GRANULARITY_MAX_VAL)) {
7553 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
7554 __func__, granularity);
7558 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
7559 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
7560 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
7561 __func__, peer_granularity);
7565 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
7569 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
7570 &peer_pa_tactivate);
7574 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
7575 peer_pa_tactivate_us = peer_pa_tactivate *
7576 gran_to_us_table[peer_granularity - 1];
7578 if (pa_tactivate_us > peer_pa_tactivate_us) {
7579 u32 new_peer_pa_tactivate;
7581 new_peer_pa_tactivate = pa_tactivate_us /
7582 gran_to_us_table[peer_granularity - 1];
7583 new_peer_pa_tactivate++;
7584 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
7585 new_peer_pa_tactivate);
7592 static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
7594 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
7595 ufshcd_tune_pa_tactivate(hba);
7596 ufshcd_tune_pa_hibern8time(hba);
7599 ufshcd_vops_apply_dev_quirks(hba);
7601 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
7602 /* set 1ms timeout for PA_TACTIVATE */
7603 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
7605 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
7606 ufshcd_quirk_tune_host_pa_tactivate(hba);
7609 static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
7611 hba->ufs_stats.hibern8_exit_cnt = 0;
7612 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
7613 hba->req_abort_count = 0;
7616 static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
7622 buff_len = hba->desc_size[QUERY_DESC_IDN_GEOMETRY];
7623 desc_buf = kmalloc(buff_len, GFP_KERNEL);
7629 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
7630 desc_buf, buff_len);
7632 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
7637 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
7638 hba->dev_info.max_lu_supported = 32;
7639 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
7640 hba->dev_info.max_lu_supported = 8;
7647 static struct ufs_ref_clk ufs_ref_clk_freqs[] = {
7648 {19200000, REF_CLK_FREQ_19_2_MHZ},
7649 {26000000, REF_CLK_FREQ_26_MHZ},
7650 {38400000, REF_CLK_FREQ_38_4_MHZ},
7651 {52000000, REF_CLK_FREQ_52_MHZ},
7652 {0, REF_CLK_FREQ_INVAL},
7655 static enum ufs_ref_clk_freq
7656 ufs_get_bref_clk_from_hz(unsigned long freq)
7660 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
7661 if (ufs_ref_clk_freqs[i].freq_hz == freq)
7662 return ufs_ref_clk_freqs[i].val;
7664 return REF_CLK_FREQ_INVAL;
7667 void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
7671 freq = clk_get_rate(refclk);
7673 hba->dev_ref_clk_freq =
7674 ufs_get_bref_clk_from_hz(freq);
7676 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
7678 "invalid ref_clk setting = %ld\n", freq);
7681 static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
7685 u32 freq = hba->dev_ref_clk_freq;
7687 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
7688 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
7691 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
7696 if (ref_clk == freq)
7697 goto out; /* nothing to update */
7699 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
7700 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
7703 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
7704 ufs_ref_clk_freqs[freq].freq_hz);
7708 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
7709 ufs_ref_clk_freqs[freq].freq_hz);
7715 static int ufshcd_device_params_init(struct ufs_hba *hba)
7720 /* Init device descriptor sizes */
7721 for (i = 0; i < QUERY_DESC_IDN_MAX; i++)
7722 hba->desc_size[i] = QUERY_DESC_MAX_SIZE;
7724 /* Init UFS geometry descriptor related parameters */
7725 ret = ufshcd_device_geo_params_init(hba);
7729 /* Check and apply UFS device quirks */
7730 ret = ufs_get_device_desc(hba);
7732 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
7737 ufshcd_get_ref_clk_gating_wait(hba);
7739 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
7740 QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag))
7741 hba->dev_info.f_power_on_wp_en = flag;
7743 /* Probe maximum power mode co-supported by both UFS host and device */
7744 if (ufshcd_get_max_pwr_mode(hba))
7746 "%s: Failed getting max supported power mode\n",
7753 * ufshcd_add_lus - probe and add UFS logical units
7754 * @hba: per-adapter instance
7756 static int ufshcd_add_lus(struct ufs_hba *hba)
7760 /* Add required well known logical units to scsi mid layer */
7761 ret = ufshcd_scsi_add_wlus(hba);
7765 ufshcd_clear_ua_wluns(hba);
7767 /* Initialize devfreq after UFS device is detected */
7768 if (ufshcd_is_clkscaling_supported(hba)) {
7769 memcpy(&hba->clk_scaling.saved_pwr_info.info,
7771 sizeof(struct ufs_pa_layer_attr));
7772 hba->clk_scaling.saved_pwr_info.is_valid = true;
7773 hba->clk_scaling.is_allowed = true;
7775 ret = ufshcd_devfreq_init(hba);
7779 hba->clk_scaling.is_enabled = true;
7780 ufshcd_init_clk_scaling_sysfs(hba);
7784 scsi_scan_host(hba->host);
7785 pm_runtime_put_sync(hba->dev);
7792 ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp);
7794 static int ufshcd_clear_ua_wlun(struct ufs_hba *hba, u8 wlun)
7796 struct scsi_device *sdp;
7797 unsigned long flags;
7800 spin_lock_irqsave(hba->host->host_lock, flags);
7801 if (wlun == UFS_UPIU_UFS_DEVICE_WLUN)
7802 sdp = hba->sdev_ufs_device;
7803 else if (wlun == UFS_UPIU_RPMB_WLUN)
7804 sdp = hba->sdev_rpmb;
7808 ret = scsi_device_get(sdp);
7809 if (!ret && !scsi_device_online(sdp)) {
7811 scsi_device_put(sdp);
7816 spin_unlock_irqrestore(hba->host->host_lock, flags);
7820 ret = ufshcd_send_request_sense(hba, sdp);
7821 scsi_device_put(sdp);
7824 dev_err(hba->dev, "%s: UAC clear LU=%x ret = %d\n",
7825 __func__, wlun, ret);
7829 static int ufshcd_clear_ua_wluns(struct ufs_hba *hba)
7833 if (!hba->wlun_dev_clr_ua)
7836 ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_UFS_DEVICE_WLUN);
7838 ret = ufshcd_clear_ua_wlun(hba, UFS_UPIU_RPMB_WLUN);
7840 hba->wlun_dev_clr_ua = false;
7843 dev_err(hba->dev, "%s: Failed to clear UAC WLUNS ret = %d\n",
7849 * ufshcd_probe_hba - probe hba to detect device and initialize
7850 * @hba: per-adapter instance
7851 * @async: asynchronous execution or not
7853 * Execute link-startup and verify device initialization
7855 static int ufshcd_probe_hba(struct ufs_hba *hba, bool async)
7858 unsigned long flags;
7859 ktime_t start = ktime_get();
7861 ret = ufshcd_link_startup(hba);
7865 /* Debug counters initialization */
7866 ufshcd_clear_dbg_ufs_stats(hba);
7868 /* UniPro link is active now */
7869 ufshcd_set_link_active(hba);
7871 /* Verify device initialization by sending NOP OUT UPIU */
7872 ret = ufshcd_verify_dev_init(hba);
7876 /* Initiate UFS initialization, and waiting until completion */
7877 ret = ufshcd_complete_dev_init(hba);
7882 * Initialize UFS device parameters used by driver, these
7883 * parameters are associated with UFS descriptors.
7886 ret = ufshcd_device_params_init(hba);
7891 ufshcd_tune_unipro_params(hba);
7893 /* UFS device is also active now */
7894 ufshcd_set_ufs_dev_active(hba);
7895 ufshcd_force_reset_auto_bkops(hba);
7896 hba->wlun_dev_clr_ua = true;
7898 /* Gear up to HS gear if supported */
7899 if (hba->max_pwr_info.is_valid) {
7901 * Set the right value to bRefClkFreq before attempting to
7902 * switch to HS gears.
7904 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
7905 ufshcd_set_dev_ref_clk(hba);
7906 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
7908 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7912 ufshcd_print_pwr_info(hba);
7916 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
7917 * and for removable UFS card as well, hence always set the parameter.
7918 * Note: Error handler may issue the device reset hence resetting
7919 * bActiveICCLevel as well so it is always safe to set this here.
7921 ufshcd_set_active_icc_lvl(hba);
7923 ufshcd_wb_config(hba);
7924 /* Enable Auto-Hibernate if configured */
7925 ufshcd_auto_hibern8_enable(hba);
7928 spin_lock_irqsave(hba->host->host_lock, flags);
7930 hba->ufshcd_state = UFSHCD_STATE_ERROR;
7931 else if (hba->ufshcd_state == UFSHCD_STATE_RESET)
7932 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
7933 spin_unlock_irqrestore(hba->host->host_lock, flags);
7935 trace_ufshcd_init(dev_name(hba->dev), ret,
7936 ktime_to_us(ktime_sub(ktime_get(), start)),
7937 hba->curr_dev_pwr_mode, hba->uic_link_state);
7942 * ufshcd_async_scan - asynchronous execution for probing hba
7943 * @data: data pointer to pass to this function
7944 * @cookie: cookie data
7946 static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7948 struct ufs_hba *hba = (struct ufs_hba *)data;
7951 down(&hba->host_sem);
7952 /* Initialize hba, detect and initialize UFS device */
7953 ret = ufshcd_probe_hba(hba, true);
7958 /* Probe and add UFS logical units */
7959 ret = ufshcd_add_lus(hba);
7962 * If we failed to initialize the device or the device is not
7963 * present, turn off the power/clocks etc.
7966 pm_runtime_put_sync(hba->dev);
7967 ufshcd_hba_exit(hba);
7971 static const struct attribute_group *ufshcd_driver_groups[] = {
7972 &ufs_sysfs_unit_descriptor_group,
7973 &ufs_sysfs_lun_attributes_group,
7977 static struct ufs_hba_variant_params ufs_hba_vps = {
7978 .hba_enable_delay_us = 1000,
7979 .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40),
7980 .devfreq_profile.polling_ms = 100,
7981 .devfreq_profile.target = ufshcd_devfreq_target,
7982 .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status,
7983 .ondemand_data.upthreshold = 70,
7984 .ondemand_data.downdifferential = 5,
7987 static struct scsi_host_template ufshcd_driver_template = {
7988 .module = THIS_MODULE,
7990 .proc_name = UFSHCD,
7991 .queuecommand = ufshcd_queuecommand,
7992 .slave_alloc = ufshcd_slave_alloc,
7993 .slave_configure = ufshcd_slave_configure,
7994 .slave_destroy = ufshcd_slave_destroy,
7995 .change_queue_depth = ufshcd_change_queue_depth,
7996 .eh_abort_handler = ufshcd_abort,
7997 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7998 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
8000 .sg_tablesize = SG_ALL,
8001 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
8002 .can_queue = UFSHCD_CAN_QUEUE,
8003 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
8004 .max_host_blocked = 1,
8005 .track_queue_depth = 1,
8006 .sdev_groups = ufshcd_driver_groups,
8007 .dma_boundary = PAGE_SIZE - 1,
8008 .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS,
8011 static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
8020 * "set_load" operation shall be required on those regulators
8021 * which specifically configured current limitation. Otherwise
8022 * zero max_uA may cause unexpected behavior when regulator is
8023 * enabled or set as high power mode.
8028 ret = regulator_set_load(vreg->reg, ua);
8030 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
8031 __func__, vreg->name, ua, ret);
8037 static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
8038 struct ufs_vreg *vreg)
8040 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
8043 static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
8044 struct ufs_vreg *vreg)
8049 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
8052 static int ufshcd_config_vreg(struct device *dev,
8053 struct ufs_vreg *vreg, bool on)
8056 struct regulator *reg;
8058 int min_uV, uA_load;
8065 if (regulator_count_voltages(reg) > 0) {
8066 uA_load = on ? vreg->max_uA : 0;
8067 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
8071 if (vreg->min_uV && vreg->max_uV) {
8072 min_uV = on ? vreg->min_uV : 0;
8073 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
8076 "%s: %s set voltage failed, err=%d\n",
8077 __func__, name, ret);
8084 static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
8088 if (!vreg || vreg->enabled)
8091 ret = ufshcd_config_vreg(dev, vreg, true);
8093 ret = regulator_enable(vreg->reg);
8096 vreg->enabled = true;
8098 dev_err(dev, "%s: %s enable failed, err=%d\n",
8099 __func__, vreg->name, ret);
8104 static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
8108 if (!vreg || !vreg->enabled || vreg->always_on)
8111 ret = regulator_disable(vreg->reg);
8114 /* ignore errors on applying disable config */
8115 ufshcd_config_vreg(dev, vreg, false);
8116 vreg->enabled = false;
8118 dev_err(dev, "%s: %s disable failed, err=%d\n",
8119 __func__, vreg->name, ret);
8125 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
8128 struct device *dev = hba->dev;
8129 struct ufs_vreg_info *info = &hba->vreg_info;
8131 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
8135 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
8139 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
8143 ufshcd_toggle_vreg(dev, info->vccq2, false);
8144 ufshcd_toggle_vreg(dev, info->vccq, false);
8145 ufshcd_toggle_vreg(dev, info->vcc, false);
8150 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
8152 struct ufs_vreg_info *info = &hba->vreg_info;
8154 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
8157 static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
8164 vreg->reg = devm_regulator_get(dev, vreg->name);
8165 if (IS_ERR(vreg->reg)) {
8166 ret = PTR_ERR(vreg->reg);
8167 dev_err(dev, "%s: %s get failed, err=%d\n",
8168 __func__, vreg->name, ret);
8174 static int ufshcd_init_vreg(struct ufs_hba *hba)
8177 struct device *dev = hba->dev;
8178 struct ufs_vreg_info *info = &hba->vreg_info;
8180 ret = ufshcd_get_vreg(dev, info->vcc);
8184 ret = ufshcd_get_vreg(dev, info->vccq);
8186 ret = ufshcd_get_vreg(dev, info->vccq2);
8191 static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
8193 struct ufs_vreg_info *info = &hba->vreg_info;
8196 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
8201 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
8204 struct ufs_clk_info *clki;
8205 struct list_head *head = &hba->clk_list_head;
8206 unsigned long flags;
8207 ktime_t start = ktime_get();
8208 bool clk_state_changed = false;
8210 if (list_empty(head))
8213 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
8217 list_for_each_entry(clki, head, list) {
8218 if (!IS_ERR_OR_NULL(clki->clk)) {
8220 * Don't disable clocks which are needed
8221 * to keep the link active.
8223 if (ufshcd_is_link_active(hba) &&
8224 clki->keep_link_active)
8227 clk_state_changed = on ^ clki->enabled;
8228 if (on && !clki->enabled) {
8229 ret = clk_prepare_enable(clki->clk);
8231 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
8232 __func__, clki->name, ret);
8235 } else if (!on && clki->enabled) {
8236 clk_disable_unprepare(clki->clk);
8239 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
8240 clki->name, on ? "en" : "dis");
8244 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
8250 list_for_each_entry(clki, head, list) {
8251 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
8252 clk_disable_unprepare(clki->clk);
8254 } else if (!ret && on) {
8255 spin_lock_irqsave(hba->host->host_lock, flags);
8256 hba->clk_gating.state = CLKS_ON;
8257 trace_ufshcd_clk_gating(dev_name(hba->dev),
8258 hba->clk_gating.state);
8259 spin_unlock_irqrestore(hba->host->host_lock, flags);
8262 if (clk_state_changed)
8263 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
8264 (on ? "on" : "off"),
8265 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
8269 static int ufshcd_init_clocks(struct ufs_hba *hba)
8272 struct ufs_clk_info *clki;
8273 struct device *dev = hba->dev;
8274 struct list_head *head = &hba->clk_list_head;
8276 if (list_empty(head))
8279 list_for_each_entry(clki, head, list) {
8283 clki->clk = devm_clk_get(dev, clki->name);
8284 if (IS_ERR(clki->clk)) {
8285 ret = PTR_ERR(clki->clk);
8286 dev_err(dev, "%s: %s clk get failed, %d\n",
8287 __func__, clki->name, ret);
8292 * Parse device ref clk freq as per device tree "ref_clk".
8293 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
8294 * in ufshcd_alloc_host().
8296 if (!strcmp(clki->name, "ref_clk"))
8297 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
8299 if (clki->max_freq) {
8300 ret = clk_set_rate(clki->clk, clki->max_freq);
8302 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
8303 __func__, clki->name,
8304 clki->max_freq, ret);
8307 clki->curr_freq = clki->max_freq;
8309 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
8310 clki->name, clk_get_rate(clki->clk));
8316 static int ufshcd_variant_hba_init(struct ufs_hba *hba)
8323 err = ufshcd_vops_init(hba);
8325 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
8326 __func__, ufshcd_get_var_name(hba), err);
8331 static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
8336 ufshcd_vops_exit(hba);
8339 static int ufshcd_hba_init(struct ufs_hba *hba)
8344 * Handle host controller power separately from the UFS device power
8345 * rails as it will help controlling the UFS host controller power
8346 * collapse easily which is different than UFS device power collapse.
8347 * Also, enable the host controller power before we go ahead with rest
8348 * of the initialization here.
8350 err = ufshcd_init_hba_vreg(hba);
8354 err = ufshcd_setup_hba_vreg(hba, true);
8358 err = ufshcd_init_clocks(hba);
8360 goto out_disable_hba_vreg;
8362 err = ufshcd_setup_clocks(hba, true);
8364 goto out_disable_hba_vreg;
8366 err = ufshcd_init_vreg(hba);
8368 goto out_disable_clks;
8370 err = ufshcd_setup_vreg(hba, true);
8372 goto out_disable_clks;
8374 err = ufshcd_variant_hba_init(hba);
8376 goto out_disable_vreg;
8378 ufs_debugfs_hba_init(hba);
8380 hba->is_powered = true;
8384 ufshcd_setup_vreg(hba, false);
8386 ufshcd_setup_clocks(hba, false);
8387 out_disable_hba_vreg:
8388 ufshcd_setup_hba_vreg(hba, false);
8393 static void ufshcd_hba_exit(struct ufs_hba *hba)
8395 if (hba->is_powered) {
8396 ufshcd_exit_clk_scaling(hba);
8397 ufshcd_exit_clk_gating(hba);
8399 destroy_workqueue(hba->eh_wq);
8400 ufs_debugfs_hba_exit(hba);
8401 ufshcd_variant_hba_exit(hba);
8402 ufshcd_setup_vreg(hba, false);
8403 ufshcd_setup_clocks(hba, false);
8404 ufshcd_setup_hba_vreg(hba, false);
8405 hba->is_powered = false;
8406 ufs_put_device_desc(hba);
8411 ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
8413 unsigned char cmd[6] = {REQUEST_SENSE,
8422 buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL);
8428 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
8429 UFS_SENSE_SIZE, NULL, NULL,
8430 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
8432 pr_err("%s: failed with err %d\n", __func__, ret);
8440 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
8442 * @hba: per adapter instance
8443 * @pwr_mode: device power mode to set
8445 * Returns 0 if requested power mode is set successfully
8446 * Returns non-zero if failed to set the requested power mode
8448 static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
8449 enum ufs_dev_pwr_mode pwr_mode)
8451 unsigned char cmd[6] = { START_STOP };
8452 struct scsi_sense_hdr sshdr;
8453 struct scsi_device *sdp;
8454 unsigned long flags;
8457 spin_lock_irqsave(hba->host->host_lock, flags);
8458 sdp = hba->sdev_ufs_device;
8460 ret = scsi_device_get(sdp);
8461 if (!ret && !scsi_device_online(sdp)) {
8463 scsi_device_put(sdp);
8468 spin_unlock_irqrestore(hba->host->host_lock, flags);
8474 * If scsi commands fail, the scsi mid-layer schedules scsi error-
8475 * handling, which would wait for host to be resumed. Since we know
8476 * we are functional while we are here, skip host resume in error
8479 hba->host->eh_noresume = 1;
8480 ufshcd_clear_ua_wluns(hba);
8482 cmd[4] = pwr_mode << 4;
8485 * Current function would be generally called from the power management
8486 * callbacks hence set the RQF_PM flag so that it doesn't resume the
8487 * already suspended childs.
8489 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
8490 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
8492 sdev_printk(KERN_WARNING, sdp,
8493 "START_STOP failed for power mode: %d, result %x\n",
8495 if (driver_byte(ret) == DRIVER_SENSE)
8496 scsi_print_sense_hdr(sdp, NULL, &sshdr);
8500 hba->curr_dev_pwr_mode = pwr_mode;
8502 scsi_device_put(sdp);
8503 hba->host->eh_noresume = 0;
8507 static int ufshcd_link_state_transition(struct ufs_hba *hba,
8508 enum uic_link_state req_link_state,
8509 int check_for_bkops)
8513 if (req_link_state == hba->uic_link_state)
8516 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
8517 ret = ufshcd_uic_hibern8_enter(hba);
8519 ufshcd_set_link_hibern8(hba);
8521 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
8527 * If autobkops is enabled, link can't be turned off because
8528 * turning off the link would also turn off the device, except in the
8529 * case of DeepSleep where the device is expected to remain powered.
8531 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
8532 (!check_for_bkops || !hba->auto_bkops_enabled)) {
8534 * Let's make sure that link is in low power mode, we are doing
8535 * this currently by putting the link in Hibern8. Otherway to
8536 * put the link in low power mode is to send the DME end point
8537 * to device and then send the DME reset command to local
8538 * unipro. But putting the link in hibern8 is much faster.
8540 * Note also that putting the link in Hibern8 is a requirement
8541 * for entering DeepSleep.
8543 ret = ufshcd_uic_hibern8_enter(hba);
8545 dev_err(hba->dev, "%s: hibern8 enter failed %d\n",
8550 * Change controller state to "reset state" which
8551 * should also put the link in off/reset state
8553 ufshcd_hba_stop(hba);
8555 * TODO: Check if we need any delay to make sure that
8556 * controller is reset
8558 ufshcd_set_link_off(hba);
8565 static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
8567 bool vcc_off = false;
8570 * It seems some UFS devices may keep drawing more than sleep current
8571 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
8572 * To avoid this situation, add 2ms delay before putting these UFS
8573 * rails in LPM mode.
8575 if (!ufshcd_is_link_active(hba) &&
8576 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
8577 usleep_range(2000, 2100);
8580 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
8583 * If UFS device and link is in OFF state, all power supplies (VCC,
8584 * VCCQ, VCCQ2) can be turned off if power on write protect is not
8585 * required. If UFS link is inactive (Hibern8 or OFF state) and device
8586 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
8588 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
8589 * in low power state which would save some power.
8591 * If Write Booster is enabled and the device needs to flush the WB
8592 * buffer OR if bkops status is urgent for WB, keep Vcc on.
8594 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8595 !hba->dev_info.is_lu_power_on_wp) {
8596 ufshcd_setup_vreg(hba, false);
8598 } else if (!ufshcd_is_ufs_dev_active(hba)) {
8599 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8601 if (!ufshcd_is_link_active(hba)) {
8602 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8603 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
8608 * Some UFS devices require delay after VCC power rail is turned-off.
8610 if (vcc_off && hba->vreg_info.vcc &&
8611 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM)
8612 usleep_range(5000, 5100);
8615 static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
8619 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8620 !hba->dev_info.is_lu_power_on_wp) {
8621 ret = ufshcd_setup_vreg(hba, true);
8622 } else if (!ufshcd_is_ufs_dev_active(hba)) {
8623 if (!ret && !ufshcd_is_link_active(hba)) {
8624 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
8627 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
8631 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
8636 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8638 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8643 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
8645 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
8646 ufshcd_setup_hba_vreg(hba, false);
8649 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
8651 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba))
8652 ufshcd_setup_hba_vreg(hba, true);
8656 * ufshcd_suspend - helper function for suspend operations
8657 * @hba: per adapter instance
8658 * @pm_op: desired low power operation type
8660 * This function will try to put the UFS device and link into low power
8661 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
8662 * (System PM level).
8664 * If this function is called during shutdown, it will make sure that
8665 * both UFS device and UFS link is powered off.
8667 * NOTE: UFS device & link must be active before we enter in this function.
8669 * Returns 0 for success and non-zero for failure
8671 static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8674 int check_for_bkops;
8675 enum ufs_pm_level pm_lvl;
8676 enum ufs_dev_pwr_mode req_dev_pwr_mode;
8677 enum uic_link_state req_link_state;
8679 hba->pm_op_in_progress = 1;
8680 if (!ufshcd_is_shutdown_pm(pm_op)) {
8681 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
8682 hba->rpm_lvl : hba->spm_lvl;
8683 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
8684 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
8686 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
8687 req_link_state = UIC_LINK_OFF_STATE;
8691 * If we can't transition into any of the low power modes
8692 * just gate the clocks.
8694 ufshcd_hold(hba, false);
8695 hba->clk_gating.is_suspended = true;
8697 if (ufshcd_is_clkscaling_supported(hba))
8698 ufshcd_clk_scaling_suspend(hba, true);
8700 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8701 req_link_state == UIC_LINK_ACTIVE_STATE) {
8705 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8706 (req_link_state == hba->uic_link_state))
8709 /* UFS device & link must be active before we enter in this function */
8710 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8715 if (ufshcd_is_runtime_pm(pm_op)) {
8716 if (ufshcd_can_autobkops_during_suspend(hba)) {
8718 * The device is idle with no requests in the queue,
8719 * allow background operations if bkops status shows
8720 * that performance might be impacted.
8722 ret = ufshcd_urgent_bkops(hba);
8726 /* make sure that auto bkops is disabled */
8727 ufshcd_disable_auto_bkops(hba);
8730 * If device needs to do BKOP or WB buffer flush during
8731 * Hibern8, keep device power mode as "active power mode"
8734 hba->dev_info.b_rpm_dev_flush_capable =
8735 hba->auto_bkops_enabled ||
8736 (((req_link_state == UIC_LINK_HIBERN8_STATE) ||
8737 ((req_link_state == UIC_LINK_ACTIVE_STATE) &&
8738 ufshcd_is_auto_hibern8_enabled(hba))) &&
8739 ufshcd_wb_need_flush(hba));
8742 flush_work(&hba->eeh_work);
8744 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) {
8745 if (!ufshcd_is_runtime_pm(pm_op))
8746 /* ensure that bkops is disabled */
8747 ufshcd_disable_auto_bkops(hba);
8749 if (!hba->dev_info.b_rpm_dev_flush_capable) {
8750 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8757 * In the case of DeepSleep, the device is expected to remain powered
8758 * with the link off, so do not check for bkops.
8760 check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba);
8761 ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops);
8763 goto set_dev_active;
8767 * Call vendor specific suspend callback. As these callbacks may access
8768 * vendor specific host controller register space call them before the
8769 * host clocks are ON.
8771 ret = ufshcd_vops_suspend(hba, pm_op);
8773 goto set_link_active;
8775 * Disable the host irq as host controller as there won't be any
8776 * host controller transaction expected till resume.
8778 ufshcd_disable_irq(hba);
8780 ufshcd_setup_clocks(hba, false);
8782 if (ufshcd_is_clkgating_allowed(hba)) {
8783 hba->clk_gating.state = CLKS_OFF;
8784 trace_ufshcd_clk_gating(dev_name(hba->dev),
8785 hba->clk_gating.state);
8788 ufshcd_vreg_set_lpm(hba);
8790 /* Put the host controller in low power mode if possible */
8791 ufshcd_hba_vreg_set_lpm(hba);
8795 ufshcd_vreg_set_hpm(hba);
8797 * Device hardware reset is required to exit DeepSleep. Also, for
8798 * DeepSleep, the link is off so host reset and restore will be done
8801 if (ufshcd_is_ufs_dev_deepsleep(hba)) {
8802 ufshcd_device_reset(hba);
8803 WARN_ON(!ufshcd_is_link_off(hba));
8805 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
8806 ufshcd_set_link_active(hba);
8807 else if (ufshcd_is_link_off(hba))
8808 ufshcd_host_reset_and_restore(hba);
8810 /* Can also get here needing to exit DeepSleep */
8811 if (ufshcd_is_ufs_dev_deepsleep(hba)) {
8812 ufshcd_device_reset(hba);
8813 ufshcd_host_reset_and_restore(hba);
8815 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8816 ufshcd_disable_auto_bkops(hba);
8818 if (ufshcd_is_clkscaling_supported(hba))
8819 ufshcd_clk_scaling_suspend(hba, false);
8821 hba->clk_gating.is_suspended = false;
8822 hba->dev_info.b_rpm_dev_flush_capable = false;
8823 ufshcd_clear_ua_wluns(hba);
8824 ufshcd_release(hba);
8826 if (hba->dev_info.b_rpm_dev_flush_capable) {
8827 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work,
8828 msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS));
8831 hba->pm_op_in_progress = 0;
8834 ufshcd_update_evt_hist(hba, UFS_EVT_SUSPEND_ERR, (u32)ret);
8839 * ufshcd_resume - helper function for resume operations
8840 * @hba: per adapter instance
8841 * @pm_op: runtime PM or system PM
8843 * This function basically brings the UFS device, UniPro link and controller
8846 * Returns 0 for success and non-zero for failure
8848 static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8851 enum uic_link_state old_link_state;
8853 hba->pm_op_in_progress = 1;
8854 old_link_state = hba->uic_link_state;
8856 ufshcd_hba_vreg_set_hpm(hba);
8857 ret = ufshcd_vreg_set_hpm(hba);
8861 /* Make sure clocks are enabled before accessing controller */
8862 ret = ufshcd_setup_clocks(hba, true);
8866 /* enable the host irq as host controller would be active soon */
8867 ufshcd_enable_irq(hba);
8870 * Call vendor specific resume callback. As these callbacks may access
8871 * vendor specific host controller register space call them when the
8872 * host clocks are ON.
8874 ret = ufshcd_vops_resume(hba, pm_op);
8876 goto disable_irq_and_vops_clks;
8878 /* For DeepSleep, the only supported option is to have the link off */
8879 WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba));
8881 if (ufshcd_is_link_hibern8(hba)) {
8882 ret = ufshcd_uic_hibern8_exit(hba);
8884 ufshcd_set_link_active(hba);
8886 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
8888 goto vendor_suspend;
8890 } else if (ufshcd_is_link_off(hba)) {
8892 * A full initialization of the host and the device is
8893 * required since the link was put to off during suspend.
8894 * Note, in the case of DeepSleep, the device will exit
8895 * DeepSleep due to device reset.
8897 ret = ufshcd_reset_and_restore(hba);
8899 * ufshcd_reset_and_restore() should have already
8900 * set the link state as active
8902 if (ret || !ufshcd_is_link_active(hba))
8903 goto vendor_suspend;
8906 if (!ufshcd_is_ufs_dev_active(hba)) {
8907 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8909 goto set_old_link_state;
8912 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8913 ufshcd_enable_auto_bkops(hba);
8916 * If BKOPs operations are urgently needed at this moment then
8917 * keep auto-bkops enabled or else disable it.
8919 ufshcd_urgent_bkops(hba);
8921 hba->clk_gating.is_suspended = false;
8923 if (ufshcd_is_clkscaling_supported(hba))
8924 ufshcd_clk_scaling_suspend(hba, false);
8926 /* Enable Auto-Hibernate if configured */
8927 ufshcd_auto_hibern8_enable(hba);
8929 if (hba->dev_info.b_rpm_dev_flush_capable) {
8930 hba->dev_info.b_rpm_dev_flush_capable = false;
8931 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work);
8934 ufshcd_clear_ua_wluns(hba);
8936 /* Schedule clock gating in case of no access to UFS device yet */
8937 ufshcd_release(hba);
8942 ufshcd_link_state_transition(hba, old_link_state, 0);
8944 ufshcd_vops_suspend(hba, pm_op);
8945 disable_irq_and_vops_clks:
8946 ufshcd_disable_irq(hba);
8947 ufshcd_setup_clocks(hba, false);
8948 if (ufshcd_is_clkgating_allowed(hba)) {
8949 hba->clk_gating.state = CLKS_OFF;
8950 trace_ufshcd_clk_gating(dev_name(hba->dev),
8951 hba->clk_gating.state);
8954 ufshcd_vreg_set_lpm(hba);
8956 hba->pm_op_in_progress = 0;
8958 ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)ret);
8963 * ufshcd_system_suspend - system suspend routine
8964 * @hba: per adapter instance
8966 * Check the description of ufshcd_suspend() function for more details.
8968 * Returns 0 for success and non-zero for failure
8970 int ufshcd_system_suspend(struct ufs_hba *hba)
8973 ktime_t start = ktime_get();
8976 early_suspend = true;
8980 down(&hba->host_sem);
8982 if (!hba->is_powered)
8985 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8986 hba->curr_dev_pwr_mode) &&
8987 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8988 hba->uic_link_state) &&
8989 !hba->dev_info.b_rpm_dev_flush_capable)
8992 if (pm_runtime_suspended(hba->dev)) {
8994 * UFS device and/or UFS link low power states during runtime
8995 * suspend seems to be different than what is expected during
8996 * system suspend. Hence runtime resume the devic & link and
8997 * let the system suspend low power states to take effect.
8998 * TODO: If resume takes longer time, we might have optimize
8999 * it in future by not resuming everything if possible.
9001 ret = ufshcd_runtime_resume(hba);
9006 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
9008 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
9009 ktime_to_us(ktime_sub(ktime_get(), start)),
9010 hba->curr_dev_pwr_mode, hba->uic_link_state);
9012 hba->is_sys_suspended = true;
9017 EXPORT_SYMBOL(ufshcd_system_suspend);
9020 * ufshcd_system_resume - system resume routine
9021 * @hba: per adapter instance
9023 * Returns 0 for success and non-zero for failure
9026 int ufshcd_system_resume(struct ufs_hba *hba)
9029 ktime_t start = ktime_get();
9034 if (unlikely(early_suspend)) {
9035 early_suspend = false;
9036 down(&hba->host_sem);
9039 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
9041 * Let the runtime resume take care of resuming
9042 * if runtime suspended.
9046 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
9048 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
9049 ktime_to_us(ktime_sub(ktime_get(), start)),
9050 hba->curr_dev_pwr_mode, hba->uic_link_state);
9052 hba->is_sys_suspended = false;
9056 EXPORT_SYMBOL(ufshcd_system_resume);
9059 * ufshcd_runtime_suspend - runtime suspend routine
9060 * @hba: per adapter instance
9062 * Check the description of ufshcd_suspend() function for more details.
9064 * Returns 0 for success and non-zero for failure
9066 int ufshcd_runtime_suspend(struct ufs_hba *hba)
9069 ktime_t start = ktime_get();
9074 if (!hba->is_powered)
9077 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
9079 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
9080 ktime_to_us(ktime_sub(ktime_get(), start)),
9081 hba->curr_dev_pwr_mode, hba->uic_link_state);
9084 EXPORT_SYMBOL(ufshcd_runtime_suspend);
9087 * ufshcd_runtime_resume - runtime resume routine
9088 * @hba: per adapter instance
9090 * This function basically brings the UFS device, UniPro link and controller
9091 * to active state. Following operations are done in this function:
9093 * 1. Turn on all the controller related clocks
9094 * 2. Bring the UniPro link out of Hibernate state
9095 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
9097 * 4. If auto-bkops is enabled on the device, disable it.
9099 * So following would be the possible power state after this function return
9101 * S1: UFS device in Active state with VCC rail ON
9102 * UniPro link in Active state
9103 * All the UFS/UniPro controller clocks are ON
9105 * Returns 0 for success and non-zero for failure
9107 int ufshcd_runtime_resume(struct ufs_hba *hba)
9110 ktime_t start = ktime_get();
9115 if (!hba->is_powered)
9118 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
9120 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
9121 ktime_to_us(ktime_sub(ktime_get(), start)),
9122 hba->curr_dev_pwr_mode, hba->uic_link_state);
9125 EXPORT_SYMBOL(ufshcd_runtime_resume);
9127 int ufshcd_runtime_idle(struct ufs_hba *hba)
9131 EXPORT_SYMBOL(ufshcd_runtime_idle);
9134 * ufshcd_shutdown - shutdown routine
9135 * @hba: per adapter instance
9137 * This function would power off both UFS device and UFS link.
9139 * Returns 0 always to allow force shutdown even in case of errors.
9141 int ufshcd_shutdown(struct ufs_hba *hba)
9145 down(&hba->host_sem);
9146 hba->shutting_down = true;
9149 if (!hba->is_powered)
9152 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
9155 pm_runtime_get_sync(hba->dev);
9157 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
9160 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
9161 hba->is_powered = false;
9162 /* allow force shutdown even in case of errors */
9165 EXPORT_SYMBOL(ufshcd_shutdown);
9168 * ufshcd_remove - de-allocate SCSI host and host memory space
9169 * data structure memory
9170 * @hba: per adapter instance
9172 void ufshcd_remove(struct ufs_hba *hba)
9174 ufs_bsg_remove(hba);
9175 ufs_sysfs_remove_nodes(hba->dev);
9176 blk_cleanup_queue(hba->tmf_queue);
9177 blk_mq_free_tag_set(&hba->tmf_tag_set);
9178 blk_cleanup_queue(hba->cmd_queue);
9179 scsi_remove_host(hba->host);
9180 /* disable interrupts */
9181 ufshcd_disable_intr(hba, hba->intr_mask);
9182 ufshcd_hba_stop(hba);
9183 ufshcd_hba_exit(hba);
9185 EXPORT_SYMBOL_GPL(ufshcd_remove);
9188 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
9189 * @hba: pointer to Host Bus Adapter (HBA)
9191 void ufshcd_dealloc_host(struct ufs_hba *hba)
9193 scsi_host_put(hba->host);
9195 EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
9198 * ufshcd_set_dma_mask - Set dma mask based on the controller
9199 * addressing capability
9200 * @hba: per adapter instance
9202 * Returns 0 for success, non-zero for failure
9204 static int ufshcd_set_dma_mask(struct ufs_hba *hba)
9206 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
9207 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
9210 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
9214 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
9215 * @dev: pointer to device handle
9216 * @hba_handle: driver private handle
9217 * Returns 0 on success, non-zero value on failure
9219 int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
9221 struct Scsi_Host *host;
9222 struct ufs_hba *hba;
9227 "Invalid memory reference for dev is NULL\n");
9232 host = scsi_host_alloc(&ufshcd_driver_template,
9233 sizeof(struct ufs_hba));
9235 dev_err(dev, "scsi_host_alloc failed\n");
9239 hba = shost_priv(host);
9243 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
9245 INIT_LIST_HEAD(&hba->clk_list_head);
9250 EXPORT_SYMBOL(ufshcd_alloc_host);
9252 /* This function exists because blk_mq_alloc_tag_set() requires this. */
9253 static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
9254 const struct blk_mq_queue_data *qd)
9257 return BLK_STS_NOTSUPP;
9260 static const struct blk_mq_ops ufshcd_tmf_ops = {
9261 .queue_rq = ufshcd_queue_tmf,
9265 * ufshcd_init - Driver initialization routine
9266 * @hba: per-adapter instance
9267 * @mmio_base: base register address
9268 * @irq: Interrupt line of device
9269 * Returns 0 on success, non-zero value on failure
9271 int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
9274 struct Scsi_Host *host = hba->host;
9275 struct device *dev = hba->dev;
9276 char eh_wq_name[sizeof("ufs_eh_wq_00")];
9280 "Invalid memory reference for mmio_base is NULL\n");
9285 hba->mmio_base = mmio_base;
9287 hba->vps = &ufs_hba_vps;
9289 err = ufshcd_hba_init(hba);
9293 /* Read capabilities registers */
9294 err = ufshcd_hba_capabilities(hba);
9298 /* Get UFS version supported by the controller */
9299 hba->ufs_version = ufshcd_get_ufs_version(hba);
9301 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
9302 (hba->ufs_version != UFSHCI_VERSION_11) &&
9303 (hba->ufs_version != UFSHCI_VERSION_20) &&
9304 (hba->ufs_version != UFSHCI_VERSION_21))
9305 dev_err(hba->dev, "invalid UFS version 0x%x\n",
9308 /* Get Interrupt bit mask per version */
9309 hba->intr_mask = ufshcd_get_intr_mask(hba);
9311 err = ufshcd_set_dma_mask(hba);
9313 dev_err(hba->dev, "set dma mask failed\n");
9317 /* Allocate memory for host memory space */
9318 err = ufshcd_memory_alloc(hba);
9320 dev_err(hba->dev, "Memory allocation failed\n");
9325 ufshcd_host_memory_configure(hba);
9327 host->can_queue = hba->nutrs;
9328 host->cmd_per_lun = hba->nutrs;
9329 host->max_id = UFSHCD_MAX_ID;
9330 host->max_lun = UFS_MAX_LUNS;
9331 host->max_channel = UFSHCD_MAX_CHANNEL;
9332 host->unique_id = host->host_no;
9333 host->max_cmd_len = UFS_CDB_SIZE;
9335 hba->max_pwr_info.is_valid = false;
9337 /* Initialize work queues */
9338 snprintf(eh_wq_name, sizeof(eh_wq_name), "ufs_eh_wq_%d",
9339 hba->host->host_no);
9340 hba->eh_wq = create_singlethread_workqueue(eh_wq_name);
9342 dev_err(hba->dev, "%s: failed to create eh workqueue\n",
9347 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
9348 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
9350 sema_init(&hba->host_sem, 1);
9352 /* Initialize UIC command mutex */
9353 mutex_init(&hba->uic_cmd_mutex);
9355 /* Initialize mutex for device management commands */
9356 mutex_init(&hba->dev_cmd.lock);
9358 init_rwsem(&hba->clk_scaling_lock);
9360 ufshcd_init_clk_gating(hba);
9362 ufshcd_init_clk_scaling(hba);
9365 * In order to avoid any spurious interrupt immediately after
9366 * registering UFS controller interrupt handler, clear any pending UFS
9367 * interrupt status and disable all the UFS interrupts.
9369 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
9370 REG_INTERRUPT_STATUS);
9371 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
9373 * Make sure that UFS interrupts are disabled and any pending interrupt
9374 * status is cleared before registering UFS interrupt handler.
9378 /* IRQ registration */
9379 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
9381 dev_err(hba->dev, "request irq failed\n");
9384 hba->is_irq_enabled = true;
9387 err = scsi_add_host(host, hba->dev);
9389 dev_err(hba->dev, "scsi_add_host failed\n");
9393 hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set);
9394 if (IS_ERR(hba->cmd_queue)) {
9395 err = PTR_ERR(hba->cmd_queue);
9396 goto out_remove_scsi_host;
9399 hba->tmf_tag_set = (struct blk_mq_tag_set) {
9401 .queue_depth = hba->nutmrs,
9402 .ops = &ufshcd_tmf_ops,
9403 .flags = BLK_MQ_F_NO_SCHED,
9405 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
9407 goto free_cmd_queue;
9408 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set);
9409 if (IS_ERR(hba->tmf_queue)) {
9410 err = PTR_ERR(hba->tmf_queue);
9411 goto free_tmf_tag_set;
9414 /* Reset the attached device */
9415 ufshcd_device_reset(hba);
9417 ufshcd_init_crypto(hba);
9419 /* Host controller enable */
9420 err = ufshcd_hba_enable(hba);
9422 dev_err(hba->dev, "Host controller enable failed\n");
9423 ufshcd_print_evt_hist(hba);
9424 ufshcd_print_host_state(hba);
9425 goto free_tmf_queue;
9429 * Set the default power management level for runtime and system PM.
9430 * Default power saving mode is to keep UFS link in Hibern8 state
9431 * and UFS device in sleep state.
9433 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9435 UIC_LINK_HIBERN8_STATE);
9436 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9438 UIC_LINK_HIBERN8_STATE);
9440 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work,
9441 ufshcd_rpm_dev_flush_recheck_work);
9443 /* Set the default auto-hiberate idle timer value to 150 ms */
9444 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
9445 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
9446 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
9449 /* Hold auto suspend until async scan completes */
9450 pm_runtime_get_sync(dev);
9451 atomic_set(&hba->scsi_block_reqs_cnt, 0);
9453 * We are assuming that device wasn't put in sleep/power-down
9454 * state exclusively during the boot stage before kernel.
9455 * This assumption helps avoid doing link startup twice during
9456 * ufshcd_probe_hba().
9458 ufshcd_set_ufs_dev_active(hba);
9460 async_schedule(ufshcd_async_scan, hba);
9461 ufs_sysfs_add_nodes(hba->dev);
9466 blk_cleanup_queue(hba->tmf_queue);
9468 blk_mq_free_tag_set(&hba->tmf_tag_set);
9470 blk_cleanup_queue(hba->cmd_queue);
9471 out_remove_scsi_host:
9472 scsi_remove_host(hba->host);
9474 hba->is_irq_enabled = false;
9475 ufshcd_hba_exit(hba);
9479 EXPORT_SYMBOL_GPL(ufshcd_init);
9481 static int __init ufshcd_core_init(void)
9487 static void __exit ufshcd_core_exit(void)
9492 module_init(ufshcd_core_init);
9493 module_exit(ufshcd_core_exit);
9495 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
9496 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
9497 MODULE_DESCRIPTION("Generic UFS host controller driver Core");
9498 MODULE_LICENSE("GPL");
9499 MODULE_VERSION(UFSHCD_DRIVER_VERSION);