1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 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. 6 * 7 * Authors: 8 * Santosh Yaraganavi <santosh.sy@samsung.com> 9 * Vinayak Holikatti <h.vinayak@samsung.com> 10 */ 11 12 #include <linux/async.h> 13 #include <linux/devfreq.h> 14 #include <linux/nls.h> 15 #include <linux/of.h> 16 #include <linux/bitfield.h> 17 #include <linux/blk-pm.h> 18 #include <linux/blkdev.h> 19 #include <linux/clk.h> 20 #include <linux/delay.h> 21 #include <linux/interrupt.h> 22 #include <linux/module.h> 23 #include <linux/pm_opp.h> 24 #include <linux/regulator/consumer.h> 25 #include <linux/sched/clock.h> 26 #include <linux/iopoll.h> 27 #include <scsi/scsi_cmnd.h> 28 #include <scsi/scsi_dbg.h> 29 #include <scsi/scsi_driver.h> 30 #include <scsi/scsi_eh.h> 31 #include "ufshcd-priv.h" 32 #include <ufs/ufs_quirks.h> 33 #include <ufs/unipro.h> 34 #include "ufs-sysfs.h" 35 #include "ufs-debugfs.h" 36 #include "ufs-fault-injection.h" 37 #include "ufs_bsg.h" 38 #include "ufshcd-crypto.h" 39 #include <asm/unaligned.h> 40 41 #define CREATE_TRACE_POINTS 42 #include <trace/events/ufs.h> 43 44 #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ 45 UTP_TASK_REQ_COMPL |\ 46 UFSHCD_ERROR_MASK) 47 48 #define UFSHCD_ENABLE_MCQ_INTRS (UTP_TASK_REQ_COMPL |\ 49 UFSHCD_ERROR_MASK |\ 50 MCQ_CQ_EVENT_STATUS) 51 52 53 /* UIC command timeout, unit: ms */ 54 #define UIC_CMD_TIMEOUT 500 55 56 /* NOP OUT retries waiting for NOP IN response */ 57 #define NOP_OUT_RETRIES 10 58 /* Timeout after 50 msecs if NOP OUT hangs without response */ 59 #define NOP_OUT_TIMEOUT 50 /* msecs */ 60 61 /* Query request retries */ 62 #define QUERY_REQ_RETRIES 3 63 /* Query request timeout */ 64 #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */ 65 66 /* Advanced RPMB request timeout */ 67 #define ADVANCED_RPMB_REQ_TIMEOUT 3000 /* 3 seconds */ 68 69 /* Task management command timeout */ 70 #define TM_CMD_TIMEOUT 100 /* msecs */ 71 72 /* maximum number of retries for a general UIC command */ 73 #define UFS_UIC_COMMAND_RETRIES 3 74 75 /* maximum number of link-startup retries */ 76 #define DME_LINKSTARTUP_RETRIES 3 77 78 /* maximum number of reset retries before giving up */ 79 #define MAX_HOST_RESET_RETRIES 5 80 81 /* Maximum number of error handler retries before giving up */ 82 #define MAX_ERR_HANDLER_RETRIES 5 83 84 /* Expose the flag value from utp_upiu_query.value */ 85 #define MASK_QUERY_UPIU_FLAG_LOC 0xFF 86 87 /* Interrupt aggregation default timeout, unit: 40us */ 88 #define INT_AGGR_DEF_TO 0x02 89 90 /* default delay of autosuspend: 2000 ms */ 91 #define RPM_AUTOSUSPEND_DELAY_MS 2000 92 93 /* Default delay of RPM device flush delayed work */ 94 #define RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS 5000 95 96 /* Default value of wait time before gating device ref clock */ 97 #define UFSHCD_REF_CLK_GATING_WAIT_US 0xFF /* microsecs */ 98 99 /* Polling time to wait for fDeviceInit */ 100 #define FDEVICEINIT_COMPL_TIMEOUT 1500 /* millisecs */ 101 102 /* Default RTC update every 10 seconds */ 103 #define UFS_RTC_UPDATE_INTERVAL_MS (10 * MSEC_PER_SEC) 104 105 /* UFSHC 4.0 compliant HC support this mode. */ 106 static bool use_mcq_mode = true; 107 108 static bool is_mcq_supported(struct ufs_hba *hba) 109 { 110 return hba->mcq_sup && use_mcq_mode; 111 } 112 113 module_param(use_mcq_mode, bool, 0644); 114 MODULE_PARM_DESC(use_mcq_mode, "Control MCQ mode for controllers starting from UFSHCI 4.0. 1 - enable MCQ, 0 - disable MCQ. MCQ is enabled by default"); 115 116 #define ufshcd_toggle_vreg(_dev, _vreg, _on) \ 117 ({ \ 118 int _ret; \ 119 if (_on) \ 120 _ret = ufshcd_enable_vreg(_dev, _vreg); \ 121 else \ 122 _ret = ufshcd_disable_vreg(_dev, _vreg); \ 123 _ret; \ 124 }) 125 126 #define ufshcd_hex_dump(prefix_str, buf, len) do { \ 127 size_t __len = (len); \ 128 print_hex_dump(KERN_ERR, prefix_str, \ 129 __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\ 130 16, 4, buf, __len, false); \ 131 } while (0) 132 133 int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len, 134 const char *prefix) 135 { 136 u32 *regs; 137 size_t pos; 138 139 if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */ 140 return -EINVAL; 141 142 regs = kzalloc(len, GFP_ATOMIC); 143 if (!regs) 144 return -ENOMEM; 145 146 for (pos = 0; pos < len; pos += 4) { 147 if (offset == 0 && 148 pos >= REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER && 149 pos <= REG_UIC_ERROR_CODE_DME) 150 continue; 151 regs[pos / 4] = ufshcd_readl(hba, offset + pos); 152 } 153 154 ufshcd_hex_dump(prefix, regs, len); 155 kfree(regs); 156 157 return 0; 158 } 159 EXPORT_SYMBOL_GPL(ufshcd_dump_regs); 160 161 enum { 162 UFSHCD_MAX_CHANNEL = 0, 163 UFSHCD_MAX_ID = 1, 164 UFSHCD_CMD_PER_LUN = 32 - UFSHCD_NUM_RESERVED, 165 UFSHCD_CAN_QUEUE = 32 - UFSHCD_NUM_RESERVED, 166 }; 167 168 static const char *const ufshcd_state_name[] = { 169 [UFSHCD_STATE_RESET] = "reset", 170 [UFSHCD_STATE_OPERATIONAL] = "operational", 171 [UFSHCD_STATE_ERROR] = "error", 172 [UFSHCD_STATE_EH_SCHEDULED_FATAL] = "eh_fatal", 173 [UFSHCD_STATE_EH_SCHEDULED_NON_FATAL] = "eh_non_fatal", 174 }; 175 176 /* UFSHCD error handling flags */ 177 enum { 178 UFSHCD_EH_IN_PROGRESS = (1 << 0), 179 }; 180 181 /* UFSHCD UIC layer error flags */ 182 enum { 183 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */ 184 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */ 185 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */ 186 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */ 187 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */ 188 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */ 189 UFSHCD_UIC_PA_GENERIC_ERROR = (1 << 6), /* Generic PA error */ 190 }; 191 192 #define ufshcd_set_eh_in_progress(h) \ 193 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS) 194 #define ufshcd_eh_in_progress(h) \ 195 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS) 196 #define ufshcd_clear_eh_in_progress(h) \ 197 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS) 198 199 const struct ufs_pm_lvl_states ufs_pm_lvl_states[] = { 200 [UFS_PM_LVL_0] = {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE}, 201 [UFS_PM_LVL_1] = {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE}, 202 [UFS_PM_LVL_2] = {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE}, 203 [UFS_PM_LVL_3] = {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE}, 204 [UFS_PM_LVL_4] = {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE}, 205 [UFS_PM_LVL_5] = {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE}, 206 /* 207 * For DeepSleep, the link is first put in hibern8 and then off. 208 * Leaving the link in hibern8 is not supported. 209 */ 210 [UFS_PM_LVL_6] = {UFS_DEEPSLEEP_PWR_MODE, UIC_LINK_OFF_STATE}, 211 }; 212 213 static inline enum ufs_dev_pwr_mode 214 ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl) 215 { 216 return ufs_pm_lvl_states[lvl].dev_state; 217 } 218 219 static inline enum uic_link_state 220 ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl) 221 { 222 return ufs_pm_lvl_states[lvl].link_state; 223 } 224 225 static inline enum ufs_pm_level 226 ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state, 227 enum uic_link_state link_state) 228 { 229 enum ufs_pm_level lvl; 230 231 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) { 232 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) && 233 (ufs_pm_lvl_states[lvl].link_state == link_state)) 234 return lvl; 235 } 236 237 /* if no match found, return the level 0 */ 238 return UFS_PM_LVL_0; 239 } 240 241 static bool ufshcd_is_ufs_dev_busy(struct ufs_hba *hba) 242 { 243 return (hba->clk_gating.active_reqs || hba->outstanding_reqs || hba->outstanding_tasks || 244 hba->active_uic_cmd || hba->uic_async_done); 245 } 246 247 static const struct ufs_dev_quirk ufs_fixups[] = { 248 /* UFS cards deviations table */ 249 { .wmanufacturerid = UFS_VENDOR_MICRON, 250 .model = UFS_ANY_MODEL, 251 .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM }, 252 { .wmanufacturerid = UFS_VENDOR_SAMSUNG, 253 .model = UFS_ANY_MODEL, 254 .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM | 255 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE | 256 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS }, 257 { .wmanufacturerid = UFS_VENDOR_SKHYNIX, 258 .model = UFS_ANY_MODEL, 259 .quirk = UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME }, 260 { .wmanufacturerid = UFS_VENDOR_SKHYNIX, 261 .model = "hB8aL1" /*H28U62301AMR*/, 262 .quirk = UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME }, 263 { .wmanufacturerid = UFS_VENDOR_TOSHIBA, 264 .model = UFS_ANY_MODEL, 265 .quirk = UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM }, 266 { .wmanufacturerid = UFS_VENDOR_TOSHIBA, 267 .model = "THGLF2G9C8KBADG", 268 .quirk = UFS_DEVICE_QUIRK_PA_TACTIVATE }, 269 { .wmanufacturerid = UFS_VENDOR_TOSHIBA, 270 .model = "THGLF2G9D8KBADG", 271 .quirk = UFS_DEVICE_QUIRK_PA_TACTIVATE }, 272 {} 273 }; 274 275 static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba); 276 static void ufshcd_async_scan(void *data, async_cookie_t cookie); 277 static int ufshcd_reset_and_restore(struct ufs_hba *hba); 278 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd); 279 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag); 280 static void ufshcd_hba_exit(struct ufs_hba *hba); 281 static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params); 282 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on); 283 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba); 284 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba); 285 static void ufshcd_resume_clkscaling(struct ufs_hba *hba); 286 static void ufshcd_suspend_clkscaling(struct ufs_hba *hba); 287 static int ufshcd_scale_clks(struct ufs_hba *hba, unsigned long freq, 288 bool scale_up); 289 static irqreturn_t ufshcd_intr(int irq, void *__hba); 290 static int ufshcd_change_power_mode(struct ufs_hba *hba, 291 struct ufs_pa_layer_attr *pwr_mode); 292 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on); 293 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on); 294 static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, 295 struct ufs_vreg *vreg); 296 static void ufshcd_wb_toggle_buf_flush_during_h8(struct ufs_hba *hba, 297 bool enable); 298 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba); 299 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba); 300 301 void ufshcd_enable_irq(struct ufs_hba *hba) 302 { 303 if (!hba->is_irq_enabled) { 304 enable_irq(hba->irq); 305 hba->is_irq_enabled = true; 306 } 307 } 308 EXPORT_SYMBOL_GPL(ufshcd_enable_irq); 309 310 void ufshcd_disable_irq(struct ufs_hba *hba) 311 { 312 if (hba->is_irq_enabled) { 313 disable_irq(hba->irq); 314 hba->is_irq_enabled = false; 315 } 316 } 317 EXPORT_SYMBOL_GPL(ufshcd_disable_irq); 318 319 static void ufshcd_configure_wb(struct ufs_hba *hba) 320 { 321 if (!ufshcd_is_wb_allowed(hba)) 322 return; 323 324 ufshcd_wb_toggle(hba, true); 325 326 ufshcd_wb_toggle_buf_flush_during_h8(hba, true); 327 328 if (ufshcd_is_wb_buf_flush_allowed(hba)) 329 ufshcd_wb_toggle_buf_flush(hba, true); 330 } 331 332 static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) 333 { 334 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt)) 335 scsi_unblock_requests(hba->host); 336 } 337 338 static void ufshcd_scsi_block_requests(struct ufs_hba *hba) 339 { 340 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1) 341 scsi_block_requests(hba->host); 342 } 343 344 static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag, 345 enum ufs_trace_str_t str_t) 346 { 347 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr; 348 struct utp_upiu_header *header; 349 350 if (!trace_ufshcd_upiu_enabled()) 351 return; 352 353 if (str_t == UFS_CMD_SEND) 354 header = &rq->header; 355 else 356 header = &hba->lrb[tag].ucd_rsp_ptr->header; 357 358 trace_ufshcd_upiu(dev_name(hba->dev), str_t, header, &rq->sc.cdb, 359 UFS_TSF_CDB); 360 } 361 362 static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba, 363 enum ufs_trace_str_t str_t, 364 struct utp_upiu_req *rq_rsp) 365 { 366 if (!trace_ufshcd_upiu_enabled()) 367 return; 368 369 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq_rsp->header, 370 &rq_rsp->qr, UFS_TSF_OSF); 371 } 372 373 static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag, 374 enum ufs_trace_str_t str_t) 375 { 376 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[tag]; 377 378 if (!trace_ufshcd_upiu_enabled()) 379 return; 380 381 if (str_t == UFS_TM_SEND) 382 trace_ufshcd_upiu(dev_name(hba->dev), str_t, 383 &descp->upiu_req.req_header, 384 &descp->upiu_req.input_param1, 385 UFS_TSF_TM_INPUT); 386 else 387 trace_ufshcd_upiu(dev_name(hba->dev), str_t, 388 &descp->upiu_rsp.rsp_header, 389 &descp->upiu_rsp.output_param1, 390 UFS_TSF_TM_OUTPUT); 391 } 392 393 static void ufshcd_add_uic_command_trace(struct ufs_hba *hba, 394 const struct uic_command *ucmd, 395 enum ufs_trace_str_t str_t) 396 { 397 u32 cmd; 398 399 if (!trace_ufshcd_uic_command_enabled()) 400 return; 401 402 if (str_t == UFS_CMD_SEND) 403 cmd = ucmd->command; 404 else 405 cmd = ufshcd_readl(hba, REG_UIC_COMMAND); 406 407 trace_ufshcd_uic_command(dev_name(hba->dev), str_t, cmd, 408 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1), 409 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2), 410 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3)); 411 } 412 413 static void ufshcd_add_command_trace(struct ufs_hba *hba, unsigned int tag, 414 enum ufs_trace_str_t str_t) 415 { 416 u64 lba = 0; 417 u8 opcode = 0, group_id = 0; 418 u32 doorbell = 0; 419 u32 intr; 420 int hwq_id = -1; 421 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; 422 struct scsi_cmnd *cmd = lrbp->cmd; 423 struct request *rq = scsi_cmd_to_rq(cmd); 424 int transfer_len = -1; 425 426 if (!cmd) 427 return; 428 429 /* trace UPIU also */ 430 ufshcd_add_cmd_upiu_trace(hba, tag, str_t); 431 if (!trace_ufshcd_command_enabled()) 432 return; 433 434 opcode = cmd->cmnd[0]; 435 436 if (opcode == READ_10 || opcode == WRITE_10) { 437 /* 438 * Currently we only fully trace read(10) and write(10) commands 439 */ 440 transfer_len = 441 be32_to_cpu(lrbp->ucd_req_ptr->sc.exp_data_transfer_len); 442 lba = scsi_get_lba(cmd); 443 if (opcode == WRITE_10) 444 group_id = lrbp->cmd->cmnd[6]; 445 } else if (opcode == UNMAP) { 446 /* 447 * The number of Bytes to be unmapped beginning with the lba. 448 */ 449 transfer_len = blk_rq_bytes(rq); 450 lba = scsi_get_lba(cmd); 451 } 452 453 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); 454 455 if (is_mcq_enabled(hba)) { 456 struct ufs_hw_queue *hwq = ufshcd_mcq_req_to_hwq(hba, rq); 457 458 hwq_id = hwq->id; 459 } else { 460 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); 461 } 462 trace_ufshcd_command(cmd->device, str_t, tag, doorbell, hwq_id, 463 transfer_len, intr, lba, opcode, group_id); 464 } 465 466 static void ufshcd_print_clk_freqs(struct ufs_hba *hba) 467 { 468 struct ufs_clk_info *clki; 469 struct list_head *head = &hba->clk_list_head; 470 471 if (list_empty(head)) 472 return; 473 474 list_for_each_entry(clki, head, list) { 475 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && 476 clki->max_freq) 477 dev_err(hba->dev, "clk: %s, rate: %u\n", 478 clki->name, clki->curr_freq); 479 } 480 } 481 482 static void ufshcd_print_evt(struct ufs_hba *hba, u32 id, 483 const char *err_name) 484 { 485 int i; 486 bool found = false; 487 const struct ufs_event_hist *e; 488 489 if (id >= UFS_EVT_CNT) 490 return; 491 492 e = &hba->ufs_stats.event[id]; 493 494 for (i = 0; i < UFS_EVENT_HIST_LENGTH; i++) { 495 int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH; 496 497 if (e->tstamp[p] == 0) 498 continue; 499 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p, 500 e->val[p], div_u64(e->tstamp[p], 1000)); 501 found = true; 502 } 503 504 if (!found) 505 dev_err(hba->dev, "No record of %s\n", err_name); 506 else 507 dev_err(hba->dev, "%s: total cnt=%llu\n", err_name, e->cnt); 508 } 509 510 static void ufshcd_print_evt_hist(struct ufs_hba *hba) 511 { 512 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); 513 514 ufshcd_print_evt(hba, UFS_EVT_PA_ERR, "pa_err"); 515 ufshcd_print_evt(hba, UFS_EVT_DL_ERR, "dl_err"); 516 ufshcd_print_evt(hba, UFS_EVT_NL_ERR, "nl_err"); 517 ufshcd_print_evt(hba, UFS_EVT_TL_ERR, "tl_err"); 518 ufshcd_print_evt(hba, UFS_EVT_DME_ERR, "dme_err"); 519 ufshcd_print_evt(hba, UFS_EVT_AUTO_HIBERN8_ERR, 520 "auto_hibern8_err"); 521 ufshcd_print_evt(hba, UFS_EVT_FATAL_ERR, "fatal_err"); 522 ufshcd_print_evt(hba, UFS_EVT_LINK_STARTUP_FAIL, 523 "link_startup_fail"); 524 ufshcd_print_evt(hba, UFS_EVT_RESUME_ERR, "resume_fail"); 525 ufshcd_print_evt(hba, UFS_EVT_SUSPEND_ERR, 526 "suspend_fail"); 527 ufshcd_print_evt(hba, UFS_EVT_WL_RES_ERR, "wlun resume_fail"); 528 ufshcd_print_evt(hba, UFS_EVT_WL_SUSP_ERR, 529 "wlun suspend_fail"); 530 ufshcd_print_evt(hba, UFS_EVT_DEV_RESET, "dev_reset"); 531 ufshcd_print_evt(hba, UFS_EVT_HOST_RESET, "host_reset"); 532 ufshcd_print_evt(hba, UFS_EVT_ABORT, "task_abort"); 533 534 ufshcd_vops_dbg_register_dump(hba); 535 } 536 537 static 538 void ufshcd_print_tr(struct ufs_hba *hba, int tag, bool pr_prdt) 539 { 540 const struct ufshcd_lrb *lrbp; 541 int prdt_length; 542 543 lrbp = &hba->lrb[tag]; 544 545 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n", 546 tag, div_u64(lrbp->issue_time_stamp_local_clock, 1000)); 547 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n", 548 tag, div_u64(lrbp->compl_time_stamp_local_clock, 1000)); 549 dev_err(hba->dev, 550 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n", 551 tag, (u64)lrbp->utrd_dma_addr); 552 553 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, 554 sizeof(struct utp_transfer_req_desc)); 555 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag, 556 (u64)lrbp->ucd_req_dma_addr); 557 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, 558 sizeof(struct utp_upiu_req)); 559 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag, 560 (u64)lrbp->ucd_rsp_dma_addr); 561 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, 562 sizeof(struct utp_upiu_rsp)); 563 564 prdt_length = le16_to_cpu( 565 lrbp->utr_descriptor_ptr->prd_table_length); 566 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) 567 prdt_length /= ufshcd_sg_entry_size(hba); 568 569 dev_err(hba->dev, 570 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n", 571 tag, prdt_length, 572 (u64)lrbp->ucd_prdt_dma_addr); 573 574 if (pr_prdt) 575 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, 576 ufshcd_sg_entry_size(hba) * prdt_length); 577 } 578 579 static bool ufshcd_print_tr_iter(struct request *req, void *priv) 580 { 581 struct scsi_device *sdev = req->q->queuedata; 582 struct Scsi_Host *shost = sdev->host; 583 struct ufs_hba *hba = shost_priv(shost); 584 585 ufshcd_print_tr(hba, req->tag, *(bool *)priv); 586 587 return true; 588 } 589 590 /** 591 * ufshcd_print_trs_all - print trs for all started requests. 592 * @hba: per-adapter instance. 593 * @pr_prdt: need to print prdt or not. 594 */ 595 static void ufshcd_print_trs_all(struct ufs_hba *hba, bool pr_prdt) 596 { 597 blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_print_tr_iter, &pr_prdt); 598 } 599 600 static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) 601 { 602 int tag; 603 604 for_each_set_bit(tag, &bitmap, hba->nutmrs) { 605 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag]; 606 607 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag); 608 ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp)); 609 } 610 } 611 612 static void ufshcd_print_host_state(struct ufs_hba *hba) 613 { 614 const struct scsi_device *sdev_ufs = hba->ufs_device_wlun; 615 616 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); 617 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n", 618 hba->outstanding_reqs, hba->outstanding_tasks); 619 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n", 620 hba->saved_err, hba->saved_uic_err); 621 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", 622 hba->curr_dev_pwr_mode, hba->uic_link_state); 623 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", 624 hba->pm_op_in_progress, hba->is_sys_suspended); 625 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", 626 hba->auto_bkops_enabled, hba->host->host_self_blocked); 627 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state); 628 dev_err(hba->dev, 629 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt=%d\n", 630 div_u64(hba->ufs_stats.last_hibern8_exit_tstamp, 1000), 631 hba->ufs_stats.hibern8_exit_cnt); 632 dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n", 633 div_u64(hba->ufs_stats.last_intr_ts, 1000), 634 hba->ufs_stats.last_intr_status); 635 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", 636 hba->eh_flags, hba->req_abort_count); 637 dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n", 638 hba->ufs_version, hba->capabilities, hba->caps); 639 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, 640 hba->dev_quirks); 641 if (sdev_ufs) 642 dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n", 643 sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev); 644 645 ufshcd_print_clk_freqs(hba); 646 } 647 648 /** 649 * ufshcd_print_pwr_info - print power params as saved in hba 650 * power info 651 * @hba: per-adapter instance 652 */ 653 static void ufshcd_print_pwr_info(struct ufs_hba *hba) 654 { 655 static const char * const names[] = { 656 "INVALID MODE", 657 "FAST MODE", 658 "SLOW_MODE", 659 "INVALID MODE", 660 "FASTAUTO_MODE", 661 "SLOWAUTO_MODE", 662 "INVALID MODE", 663 }; 664 665 /* 666 * Using dev_dbg to avoid messages during runtime PM to avoid 667 * never-ending cycles of messages written back to storage by user space 668 * causing runtime resume, causing more messages and so on. 669 */ 670 dev_dbg(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", 671 __func__, 672 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, 673 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, 674 names[hba->pwr_info.pwr_rx], 675 names[hba->pwr_info.pwr_tx], 676 hba->pwr_info.hs_rate); 677 } 678 679 static void ufshcd_device_reset(struct ufs_hba *hba) 680 { 681 int err; 682 683 err = ufshcd_vops_device_reset(hba); 684 685 if (!err) { 686 ufshcd_set_ufs_dev_active(hba); 687 if (ufshcd_is_wb_allowed(hba)) { 688 hba->dev_info.wb_enabled = false; 689 hba->dev_info.wb_buf_flush_enabled = false; 690 } 691 if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE) 692 hba->dev_info.rtc_time_baseline = 0; 693 } 694 if (err != -EOPNOTSUPP) 695 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err); 696 } 697 698 void ufshcd_delay_us(unsigned long us, unsigned long tolerance) 699 { 700 if (!us) 701 return; 702 703 if (us < 10) 704 udelay(us); 705 else 706 usleep_range(us, us + tolerance); 707 } 708 EXPORT_SYMBOL_GPL(ufshcd_delay_us); 709 710 /** 711 * ufshcd_wait_for_register - wait for register value to change 712 * @hba: per-adapter interface 713 * @reg: mmio register offset 714 * @mask: mask to apply to the read register value 715 * @val: value to wait for 716 * @interval_us: polling interval in microseconds 717 * @timeout_ms: timeout in milliseconds 718 * 719 * Return: -ETIMEDOUT on error, zero on success. 720 */ 721 static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, 722 u32 val, unsigned long interval_us, 723 unsigned long timeout_ms) 724 { 725 int err = 0; 726 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); 727 728 /* ignore bits that we don't intend to wait on */ 729 val = val & mask; 730 731 while ((ufshcd_readl(hba, reg) & mask) != val) { 732 usleep_range(interval_us, interval_us + 50); 733 if (time_after(jiffies, timeout)) { 734 if ((ufshcd_readl(hba, reg) & mask) != val) 735 err = -ETIMEDOUT; 736 break; 737 } 738 } 739 740 return err; 741 } 742 743 /** 744 * ufshcd_get_intr_mask - Get the interrupt bit mask 745 * @hba: Pointer to adapter instance 746 * 747 * Return: interrupt bit mask per version 748 */ 749 static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) 750 { 751 if (hba->ufs_version <= ufshci_version(2, 0)) 752 return INTERRUPT_MASK_ALL_VER_11; 753 754 return INTERRUPT_MASK_ALL_VER_21; 755 } 756 757 /** 758 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA 759 * @hba: Pointer to adapter instance 760 * 761 * Return: UFSHCI version supported by the controller 762 */ 763 static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) 764 { 765 u32 ufshci_ver; 766 767 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) 768 ufshci_ver = ufshcd_vops_get_ufs_hci_version(hba); 769 else 770 ufshci_ver = ufshcd_readl(hba, REG_UFS_VERSION); 771 772 /* 773 * UFSHCI v1.x uses a different version scheme, in order 774 * to allow the use of comparisons with the ufshci_version 775 * function, we convert it to the same scheme as ufs 2.0+. 776 */ 777 if (ufshci_ver & 0x00010000) 778 return ufshci_version(1, ufshci_ver & 0x00000100); 779 780 return ufshci_ver; 781 } 782 783 /** 784 * ufshcd_is_device_present - Check if any device connected to 785 * the host controller 786 * @hba: pointer to adapter instance 787 * 788 * Return: true if device present, false if no device detected 789 */ 790 static inline bool ufshcd_is_device_present(struct ufs_hba *hba) 791 { 792 return ufshcd_readl(hba, REG_CONTROLLER_STATUS) & DEVICE_PRESENT; 793 } 794 795 /** 796 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status 797 * @lrbp: pointer to local command reference block 798 * @cqe: pointer to the completion queue entry 799 * 800 * This function is used to get the OCS field from UTRD 801 * 802 * Return: the OCS field in the UTRD. 803 */ 804 static enum utp_ocs ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp, 805 struct cq_entry *cqe) 806 { 807 if (cqe) 808 return le32_to_cpu(cqe->status) & MASK_OCS; 809 810 return lrbp->utr_descriptor_ptr->header.ocs & MASK_OCS; 811 } 812 813 /** 814 * ufshcd_utrl_clear() - Clear requests from the controller request list. 815 * @hba: per adapter instance 816 * @mask: mask with one bit set for each request to be cleared 817 */ 818 static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 mask) 819 { 820 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) 821 mask = ~mask; 822 /* 823 * From the UFSHCI specification: "UTP Transfer Request List CLear 824 * Register (UTRLCLR): This field is bit significant. Each bit 825 * corresponds to a slot in the UTP Transfer Request List, where bit 0 826 * corresponds to request slot 0. A bit in this field is set to ‘0’ 827 * by host software to indicate to the host controller that a transfer 828 * request slot is cleared. The host controller 829 * shall free up any resources associated to the request slot 830 * immediately, and shall set the associated bit in UTRLDBR to ‘0’. The 831 * host software indicates no change to request slots by setting the 832 * associated bits in this field to ‘1’. Bits in this field shall only 833 * be set ‘1’ or ‘0’ by host software when UTRLRSR is set to ‘1’." 834 */ 835 ufshcd_writel(hba, ~mask, REG_UTP_TRANSFER_REQ_LIST_CLEAR); 836 } 837 838 /** 839 * ufshcd_utmrl_clear - Clear a bit in UTMRLCLR register 840 * @hba: per adapter instance 841 * @pos: position of the bit to be cleared 842 */ 843 static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos) 844 { 845 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) 846 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); 847 else 848 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); 849 } 850 851 /** 852 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY 853 * @reg: Register value of host controller status 854 * 855 * Return: 0 on success; a positive value if failed. 856 */ 857 static inline int ufshcd_get_lists_status(u32 reg) 858 { 859 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY); 860 } 861 862 /** 863 * ufshcd_get_uic_cmd_result - Get the UIC command result 864 * @hba: Pointer to adapter instance 865 * 866 * This function gets the result of UIC command completion 867 * 868 * Return: 0 on success; non-zero value on error. 869 */ 870 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) 871 { 872 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & 873 MASK_UIC_COMMAND_RESULT; 874 } 875 876 /** 877 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command 878 * @hba: Pointer to adapter instance 879 * 880 * This function gets UIC command argument3 881 * 882 * Return: 0 on success; non-zero value on error. 883 */ 884 static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) 885 { 886 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); 887 } 888 889 /** 890 * ufshcd_get_req_rsp - returns the TR response transaction type 891 * @ucd_rsp_ptr: pointer to response UPIU 892 * 893 * Return: UPIU type. 894 */ 895 static inline enum upiu_response_transaction 896 ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) 897 { 898 return ucd_rsp_ptr->header.transaction_code; 899 } 900 901 /** 902 * ufshcd_is_exception_event - Check if the device raised an exception event 903 * @ucd_rsp_ptr: pointer to response UPIU 904 * 905 * The function checks if the device raised an exception event indicated in 906 * the Device Information field of response UPIU. 907 * 908 * Return: true if exception is raised, false otherwise. 909 */ 910 static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr) 911 { 912 return ucd_rsp_ptr->header.device_information & 1; 913 } 914 915 /** 916 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. 917 * @hba: per adapter instance 918 */ 919 static inline void 920 ufshcd_reset_intr_aggr(struct ufs_hba *hba) 921 { 922 ufshcd_writel(hba, INT_AGGR_ENABLE | 923 INT_AGGR_COUNTER_AND_TIMER_RESET, 924 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); 925 } 926 927 /** 928 * ufshcd_config_intr_aggr - Configure interrupt aggregation values. 929 * @hba: per adapter instance 930 * @cnt: Interrupt aggregation counter threshold 931 * @tmout: Interrupt aggregation timeout value 932 */ 933 static inline void 934 ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) 935 { 936 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | 937 INT_AGGR_COUNTER_THLD_VAL(cnt) | 938 INT_AGGR_TIMEOUT_VAL(tmout), 939 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); 940 } 941 942 /** 943 * ufshcd_disable_intr_aggr - Disables interrupt aggregation. 944 * @hba: per adapter instance 945 */ 946 static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) 947 { 948 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); 949 } 950 951 /** 952 * ufshcd_enable_run_stop_reg - Enable run-stop registers, 953 * When run-stop registers are set to 1, it indicates the 954 * host controller that it can process the requests 955 * @hba: per adapter instance 956 */ 957 static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) 958 { 959 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, 960 REG_UTP_TASK_REQ_LIST_RUN_STOP); 961 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, 962 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP); 963 } 964 965 /** 966 * ufshcd_hba_start - Start controller initialization sequence 967 * @hba: per adapter instance 968 */ 969 static inline void ufshcd_hba_start(struct ufs_hba *hba) 970 { 971 u32 val = CONTROLLER_ENABLE; 972 973 if (ufshcd_crypto_enable(hba)) 974 val |= CRYPTO_GENERAL_ENABLE; 975 976 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE); 977 } 978 979 /** 980 * ufshcd_is_hba_active - Get controller state 981 * @hba: per adapter instance 982 * 983 * Return: true if and only if the controller is active. 984 */ 985 bool ufshcd_is_hba_active(struct ufs_hba *hba) 986 { 987 return ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE; 988 } 989 EXPORT_SYMBOL_GPL(ufshcd_is_hba_active); 990 991 /** 992 * ufshcd_pm_qos_init - initialize PM QoS request 993 * @hba: per adapter instance 994 */ 995 void ufshcd_pm_qos_init(struct ufs_hba *hba) 996 { 997 998 if (hba->pm_qos_enabled) 999 return; 1000 1001 cpu_latency_qos_add_request(&hba->pm_qos_req, PM_QOS_DEFAULT_VALUE); 1002 1003 if (cpu_latency_qos_request_active(&hba->pm_qos_req)) 1004 hba->pm_qos_enabled = true; 1005 } 1006 1007 /** 1008 * ufshcd_pm_qos_exit - remove request from PM QoS 1009 * @hba: per adapter instance 1010 */ 1011 void ufshcd_pm_qos_exit(struct ufs_hba *hba) 1012 { 1013 if (!hba->pm_qos_enabled) 1014 return; 1015 1016 cpu_latency_qos_remove_request(&hba->pm_qos_req); 1017 hba->pm_qos_enabled = false; 1018 } 1019 1020 /** 1021 * ufshcd_pm_qos_update - update PM QoS request 1022 * @hba: per adapter instance 1023 * @on: If True, vote for perf PM QoS mode otherwise power save mode 1024 */ 1025 static void ufshcd_pm_qos_update(struct ufs_hba *hba, bool on) 1026 { 1027 if (!hba->pm_qos_enabled) 1028 return; 1029 1030 cpu_latency_qos_update_request(&hba->pm_qos_req, on ? 0 : PM_QOS_DEFAULT_VALUE); 1031 } 1032 1033 /** 1034 * ufshcd_set_clk_freq - set UFS controller clock frequencies 1035 * @hba: per adapter instance 1036 * @scale_up: If True, set max possible frequency othewise set low frequency 1037 * 1038 * Return: 0 if successful; < 0 upon failure. 1039 */ 1040 static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up) 1041 { 1042 int ret = 0; 1043 struct ufs_clk_info *clki; 1044 struct list_head *head = &hba->clk_list_head; 1045 1046 if (list_empty(head)) 1047 goto out; 1048 1049 list_for_each_entry(clki, head, list) { 1050 if (!IS_ERR_OR_NULL(clki->clk)) { 1051 if (scale_up && clki->max_freq) { 1052 if (clki->curr_freq == clki->max_freq) 1053 continue; 1054 1055 ret = clk_set_rate(clki->clk, clki->max_freq); 1056 if (ret) { 1057 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", 1058 __func__, clki->name, 1059 clki->max_freq, ret); 1060 break; 1061 } 1062 trace_ufshcd_clk_scaling(dev_name(hba->dev), 1063 "scaled up", clki->name, 1064 clki->curr_freq, 1065 clki->max_freq); 1066 1067 clki->curr_freq = clki->max_freq; 1068 1069 } else if (!scale_up && clki->min_freq) { 1070 if (clki->curr_freq == clki->min_freq) 1071 continue; 1072 1073 ret = clk_set_rate(clki->clk, clki->min_freq); 1074 if (ret) { 1075 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", 1076 __func__, clki->name, 1077 clki->min_freq, ret); 1078 break; 1079 } 1080 trace_ufshcd_clk_scaling(dev_name(hba->dev), 1081 "scaled down", clki->name, 1082 clki->curr_freq, 1083 clki->min_freq); 1084 clki->curr_freq = clki->min_freq; 1085 } 1086 } 1087 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, 1088 clki->name, clk_get_rate(clki->clk)); 1089 } 1090 1091 out: 1092 return ret; 1093 } 1094 1095 int ufshcd_opp_config_clks(struct device *dev, struct opp_table *opp_table, 1096 struct dev_pm_opp *opp, void *data, 1097 bool scaling_down) 1098 { 1099 struct ufs_hba *hba = dev_get_drvdata(dev); 1100 struct list_head *head = &hba->clk_list_head; 1101 struct ufs_clk_info *clki; 1102 unsigned long freq; 1103 u8 idx = 0; 1104 int ret; 1105 1106 list_for_each_entry(clki, head, list) { 1107 if (!IS_ERR_OR_NULL(clki->clk)) { 1108 freq = dev_pm_opp_get_freq_indexed(opp, idx++); 1109 1110 /* Do not set rate for clocks having frequency as 0 */ 1111 if (!freq) 1112 continue; 1113 1114 ret = clk_set_rate(clki->clk, freq); 1115 if (ret) { 1116 dev_err(dev, "%s: %s clk set rate(%ldHz) failed, %d\n", 1117 __func__, clki->name, freq, ret); 1118 return ret; 1119 } 1120 1121 trace_ufshcd_clk_scaling(dev_name(dev), 1122 (scaling_down ? "scaled down" : "scaled up"), 1123 clki->name, hba->clk_scaling.target_freq, freq); 1124 } 1125 } 1126 1127 return 0; 1128 } 1129 EXPORT_SYMBOL_GPL(ufshcd_opp_config_clks); 1130 1131 static int ufshcd_opp_set_rate(struct ufs_hba *hba, unsigned long freq) 1132 { 1133 struct dev_pm_opp *opp; 1134 int ret; 1135 1136 opp = dev_pm_opp_find_freq_floor_indexed(hba->dev, 1137 &freq, 0); 1138 if (IS_ERR(opp)) 1139 return PTR_ERR(opp); 1140 1141 ret = dev_pm_opp_set_opp(hba->dev, opp); 1142 dev_pm_opp_put(opp); 1143 1144 return ret; 1145 } 1146 1147 /** 1148 * ufshcd_scale_clks - scale up or scale down UFS controller clocks 1149 * @hba: per adapter instance 1150 * @freq: frequency to scale 1151 * @scale_up: True if scaling up and false if scaling down 1152 * 1153 * Return: 0 if successful; < 0 upon failure. 1154 */ 1155 static int ufshcd_scale_clks(struct ufs_hba *hba, unsigned long freq, 1156 bool scale_up) 1157 { 1158 int ret = 0; 1159 ktime_t start = ktime_get(); 1160 1161 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); 1162 if (ret) 1163 goto out; 1164 1165 if (hba->use_pm_opp) 1166 ret = ufshcd_opp_set_rate(hba, freq); 1167 else 1168 ret = ufshcd_set_clk_freq(hba, scale_up); 1169 if (ret) 1170 goto out; 1171 1172 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); 1173 if (ret) { 1174 if (hba->use_pm_opp) 1175 ufshcd_opp_set_rate(hba, 1176 hba->devfreq->previous_freq); 1177 else 1178 ufshcd_set_clk_freq(hba, !scale_up); 1179 goto out; 1180 } 1181 1182 ufshcd_pm_qos_update(hba, scale_up); 1183 1184 out: 1185 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), 1186 (scale_up ? "up" : "down"), 1187 ktime_to_us(ktime_sub(ktime_get(), start)), ret); 1188 return ret; 1189 } 1190 1191 /** 1192 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not 1193 * @hba: per adapter instance 1194 * @freq: frequency to scale 1195 * @scale_up: True if scaling up and false if scaling down 1196 * 1197 * Return: true if scaling is required, false otherwise. 1198 */ 1199 static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, 1200 unsigned long freq, bool scale_up) 1201 { 1202 struct ufs_clk_info *clki; 1203 struct list_head *head = &hba->clk_list_head; 1204 1205 if (list_empty(head)) 1206 return false; 1207 1208 if (hba->use_pm_opp) 1209 return freq != hba->clk_scaling.target_freq; 1210 1211 list_for_each_entry(clki, head, list) { 1212 if (!IS_ERR_OR_NULL(clki->clk)) { 1213 if (scale_up && clki->max_freq) { 1214 if (clki->curr_freq == clki->max_freq) 1215 continue; 1216 return true; 1217 } else if (!scale_up && clki->min_freq) { 1218 if (clki->curr_freq == clki->min_freq) 1219 continue; 1220 return true; 1221 } 1222 } 1223 } 1224 1225 return false; 1226 } 1227 1228 /* 1229 * Determine the number of pending commands by counting the bits in the SCSI 1230 * device budget maps. This approach has been selected because a bit is set in 1231 * the budget map before scsi_host_queue_ready() checks the host_self_blocked 1232 * flag. The host_self_blocked flag can be modified by calling 1233 * scsi_block_requests() or scsi_unblock_requests(). 1234 */ 1235 static u32 ufshcd_pending_cmds(struct ufs_hba *hba) 1236 { 1237 const struct scsi_device *sdev; 1238 u32 pending = 0; 1239 1240 lockdep_assert_held(hba->host->host_lock); 1241 __shost_for_each_device(sdev, hba->host) 1242 pending += sbitmap_weight(&sdev->budget_map); 1243 1244 return pending; 1245 } 1246 1247 /* 1248 * Wait until all pending SCSI commands and TMFs have finished or the timeout 1249 * has expired. 1250 * 1251 * Return: 0 upon success; -EBUSY upon timeout. 1252 */ 1253 static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, 1254 u64 wait_timeout_us) 1255 { 1256 unsigned long flags; 1257 int ret = 0; 1258 u32 tm_doorbell; 1259 u32 tr_pending; 1260 bool timeout = false, do_last_check = false; 1261 ktime_t start; 1262 1263 ufshcd_hold(hba); 1264 spin_lock_irqsave(hba->host->host_lock, flags); 1265 /* 1266 * Wait for all the outstanding tasks/transfer requests. 1267 * Verify by checking the doorbell registers are clear. 1268 */ 1269 start = ktime_get(); 1270 do { 1271 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { 1272 ret = -EBUSY; 1273 goto out; 1274 } 1275 1276 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); 1277 tr_pending = ufshcd_pending_cmds(hba); 1278 if (!tm_doorbell && !tr_pending) { 1279 timeout = false; 1280 break; 1281 } else if (do_last_check) { 1282 break; 1283 } 1284 1285 spin_unlock_irqrestore(hba->host->host_lock, flags); 1286 io_schedule_timeout(msecs_to_jiffies(20)); 1287 if (ktime_to_us(ktime_sub(ktime_get(), start)) > 1288 wait_timeout_us) { 1289 timeout = true; 1290 /* 1291 * We might have scheduled out for long time so make 1292 * sure to check if doorbells are cleared by this time 1293 * or not. 1294 */ 1295 do_last_check = true; 1296 } 1297 spin_lock_irqsave(hba->host->host_lock, flags); 1298 } while (tm_doorbell || tr_pending); 1299 1300 if (timeout) { 1301 dev_err(hba->dev, 1302 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n", 1303 __func__, tm_doorbell, tr_pending); 1304 ret = -EBUSY; 1305 } 1306 out: 1307 spin_unlock_irqrestore(hba->host->host_lock, flags); 1308 ufshcd_release(hba); 1309 return ret; 1310 } 1311 1312 /** 1313 * ufshcd_scale_gear - scale up/down UFS gear 1314 * @hba: per adapter instance 1315 * @scale_up: True for scaling up gear and false for scaling down 1316 * 1317 * Return: 0 for success; -EBUSY if scaling can't happen at this time; 1318 * non-zero for any other errors. 1319 */ 1320 static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) 1321 { 1322 int ret = 0; 1323 struct ufs_pa_layer_attr new_pwr_info; 1324 1325 if (scale_up) { 1326 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info, 1327 sizeof(struct ufs_pa_layer_attr)); 1328 } else { 1329 memcpy(&new_pwr_info, &hba->pwr_info, 1330 sizeof(struct ufs_pa_layer_attr)); 1331 1332 if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear || 1333 hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) { 1334 /* save the current power mode */ 1335 memcpy(&hba->clk_scaling.saved_pwr_info, 1336 &hba->pwr_info, 1337 sizeof(struct ufs_pa_layer_attr)); 1338 1339 /* scale down gear */ 1340 new_pwr_info.gear_tx = hba->clk_scaling.min_gear; 1341 new_pwr_info.gear_rx = hba->clk_scaling.min_gear; 1342 } 1343 } 1344 1345 /* check if the power mode needs to be changed or not? */ 1346 ret = ufshcd_config_pwr_mode(hba, &new_pwr_info); 1347 if (ret) 1348 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)", 1349 __func__, ret, 1350 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, 1351 new_pwr_info.gear_tx, new_pwr_info.gear_rx); 1352 1353 return ret; 1354 } 1355 1356 /* 1357 * Wait until all pending SCSI commands and TMFs have finished or the timeout 1358 * has expired. 1359 * 1360 * Return: 0 upon success; -EBUSY upon timeout. 1361 */ 1362 static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba, u64 timeout_us) 1363 { 1364 int ret = 0; 1365 /* 1366 * make sure that there are no outstanding requests when 1367 * clock scaling is in progress 1368 */ 1369 ufshcd_scsi_block_requests(hba); 1370 mutex_lock(&hba->wb_mutex); 1371 down_write(&hba->clk_scaling_lock); 1372 1373 if (!hba->clk_scaling.is_allowed || 1374 ufshcd_wait_for_doorbell_clr(hba, timeout_us)) { 1375 ret = -EBUSY; 1376 up_write(&hba->clk_scaling_lock); 1377 mutex_unlock(&hba->wb_mutex); 1378 ufshcd_scsi_unblock_requests(hba); 1379 goto out; 1380 } 1381 1382 /* let's not get into low power until clock scaling is completed */ 1383 ufshcd_hold(hba); 1384 1385 out: 1386 return ret; 1387 } 1388 1389 static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, int err, bool scale_up) 1390 { 1391 up_write(&hba->clk_scaling_lock); 1392 1393 /* Enable Write Booster if we have scaled up else disable it */ 1394 if (ufshcd_enable_wb_if_scaling_up(hba) && !err) 1395 ufshcd_wb_toggle(hba, scale_up); 1396 1397 mutex_unlock(&hba->wb_mutex); 1398 1399 ufshcd_scsi_unblock_requests(hba); 1400 ufshcd_release(hba); 1401 } 1402 1403 /** 1404 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear 1405 * @hba: per adapter instance 1406 * @freq: frequency to scale 1407 * @scale_up: True for scaling up and false for scalin down 1408 * 1409 * Return: 0 for success; -EBUSY if scaling can't happen at this time; non-zero 1410 * for any other errors. 1411 */ 1412 static int ufshcd_devfreq_scale(struct ufs_hba *hba, unsigned long freq, 1413 bool scale_up) 1414 { 1415 int ret = 0; 1416 1417 ret = ufshcd_clock_scaling_prepare(hba, 1 * USEC_PER_SEC); 1418 if (ret) 1419 return ret; 1420 1421 /* scale down the gear before scaling down clocks */ 1422 if (!scale_up) { 1423 ret = ufshcd_scale_gear(hba, false); 1424 if (ret) 1425 goto out_unprepare; 1426 } 1427 1428 ret = ufshcd_scale_clks(hba, freq, scale_up); 1429 if (ret) { 1430 if (!scale_up) 1431 ufshcd_scale_gear(hba, true); 1432 goto out_unprepare; 1433 } 1434 1435 /* scale up the gear after scaling up clocks */ 1436 if (scale_up) { 1437 ret = ufshcd_scale_gear(hba, true); 1438 if (ret) { 1439 ufshcd_scale_clks(hba, hba->devfreq->previous_freq, 1440 false); 1441 goto out_unprepare; 1442 } 1443 } 1444 1445 out_unprepare: 1446 ufshcd_clock_scaling_unprepare(hba, ret, scale_up); 1447 return ret; 1448 } 1449 1450 static void ufshcd_clk_scaling_suspend_work(struct work_struct *work) 1451 { 1452 struct ufs_hba *hba = container_of(work, struct ufs_hba, 1453 clk_scaling.suspend_work); 1454 unsigned long irq_flags; 1455 1456 spin_lock_irqsave(hba->host->host_lock, irq_flags); 1457 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { 1458 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1459 return; 1460 } 1461 hba->clk_scaling.is_suspended = true; 1462 hba->clk_scaling.window_start_t = 0; 1463 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1464 1465 devfreq_suspend_device(hba->devfreq); 1466 } 1467 1468 static void ufshcd_clk_scaling_resume_work(struct work_struct *work) 1469 { 1470 struct ufs_hba *hba = container_of(work, struct ufs_hba, 1471 clk_scaling.resume_work); 1472 unsigned long irq_flags; 1473 1474 spin_lock_irqsave(hba->host->host_lock, irq_flags); 1475 if (!hba->clk_scaling.is_suspended) { 1476 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1477 return; 1478 } 1479 hba->clk_scaling.is_suspended = false; 1480 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1481 1482 devfreq_resume_device(hba->devfreq); 1483 } 1484 1485 static int ufshcd_devfreq_target(struct device *dev, 1486 unsigned long *freq, u32 flags) 1487 { 1488 int ret = 0; 1489 struct ufs_hba *hba = dev_get_drvdata(dev); 1490 ktime_t start; 1491 bool scale_up = false, sched_clk_scaling_suspend_work = false; 1492 struct list_head *clk_list = &hba->clk_list_head; 1493 struct ufs_clk_info *clki; 1494 unsigned long irq_flags; 1495 1496 if (!ufshcd_is_clkscaling_supported(hba)) 1497 return -EINVAL; 1498 1499 if (hba->use_pm_opp) { 1500 struct dev_pm_opp *opp; 1501 1502 /* Get the recommended frequency from OPP framework */ 1503 opp = devfreq_recommended_opp(dev, freq, flags); 1504 if (IS_ERR(opp)) 1505 return PTR_ERR(opp); 1506 1507 dev_pm_opp_put(opp); 1508 } else { 1509 /* Override with the closest supported frequency */ 1510 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, 1511 list); 1512 *freq = (unsigned long) clk_round_rate(clki->clk, *freq); 1513 } 1514 1515 spin_lock_irqsave(hba->host->host_lock, irq_flags); 1516 if (ufshcd_eh_in_progress(hba)) { 1517 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1518 return 0; 1519 } 1520 1521 /* Skip scaling clock when clock scaling is suspended */ 1522 if (hba->clk_scaling.is_suspended) { 1523 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1524 dev_warn(hba->dev, "clock scaling is suspended, skip"); 1525 return 0; 1526 } 1527 1528 if (!hba->clk_scaling.active_reqs) 1529 sched_clk_scaling_suspend_work = true; 1530 1531 if (list_empty(clk_list)) { 1532 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1533 goto out; 1534 } 1535 1536 /* Decide based on the target or rounded-off frequency and update */ 1537 if (hba->use_pm_opp) 1538 scale_up = *freq > hba->clk_scaling.target_freq; 1539 else 1540 scale_up = *freq == clki->max_freq; 1541 1542 if (!hba->use_pm_opp && !scale_up) 1543 *freq = clki->min_freq; 1544 1545 /* Update the frequency */ 1546 if (!ufshcd_is_devfreq_scaling_required(hba, *freq, scale_up)) { 1547 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1548 ret = 0; 1549 goto out; /* no state change required */ 1550 } 1551 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); 1552 1553 start = ktime_get(); 1554 ret = ufshcd_devfreq_scale(hba, *freq, scale_up); 1555 if (!ret) 1556 hba->clk_scaling.target_freq = *freq; 1557 1558 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), 1559 (scale_up ? "up" : "down"), 1560 ktime_to_us(ktime_sub(ktime_get(), start)), ret); 1561 1562 out: 1563 if (sched_clk_scaling_suspend_work && !scale_up) 1564 queue_work(hba->clk_scaling.workq, 1565 &hba->clk_scaling.suspend_work); 1566 1567 return ret; 1568 } 1569 1570 static int ufshcd_devfreq_get_dev_status(struct device *dev, 1571 struct devfreq_dev_status *stat) 1572 { 1573 struct ufs_hba *hba = dev_get_drvdata(dev); 1574 struct ufs_clk_scaling *scaling = &hba->clk_scaling; 1575 unsigned long flags; 1576 ktime_t curr_t; 1577 1578 if (!ufshcd_is_clkscaling_supported(hba)) 1579 return -EINVAL; 1580 1581 memset(stat, 0, sizeof(*stat)); 1582 1583 spin_lock_irqsave(hba->host->host_lock, flags); 1584 curr_t = ktime_get(); 1585 if (!scaling->window_start_t) 1586 goto start_window; 1587 1588 /* 1589 * If current frequency is 0, then the ondemand governor considers 1590 * there's no initial frequency set. And it always requests to set 1591 * to max. frequency. 1592 */ 1593 if (hba->use_pm_opp) { 1594 stat->current_frequency = hba->clk_scaling.target_freq; 1595 } else { 1596 struct list_head *clk_list = &hba->clk_list_head; 1597 struct ufs_clk_info *clki; 1598 1599 clki = list_first_entry(clk_list, struct ufs_clk_info, list); 1600 stat->current_frequency = clki->curr_freq; 1601 } 1602 1603 if (scaling->is_busy_started) 1604 scaling->tot_busy_t += ktime_us_delta(curr_t, 1605 scaling->busy_start_t); 1606 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t); 1607 stat->busy_time = scaling->tot_busy_t; 1608 start_window: 1609 scaling->window_start_t = curr_t; 1610 scaling->tot_busy_t = 0; 1611 1612 if (scaling->active_reqs) { 1613 scaling->busy_start_t = curr_t; 1614 scaling->is_busy_started = true; 1615 } else { 1616 scaling->busy_start_t = 0; 1617 scaling->is_busy_started = false; 1618 } 1619 spin_unlock_irqrestore(hba->host->host_lock, flags); 1620 return 0; 1621 } 1622 1623 static int ufshcd_devfreq_init(struct ufs_hba *hba) 1624 { 1625 struct list_head *clk_list = &hba->clk_list_head; 1626 struct ufs_clk_info *clki; 1627 struct devfreq *devfreq; 1628 int ret; 1629 1630 /* Skip devfreq if we don't have any clocks in the list */ 1631 if (list_empty(clk_list)) 1632 return 0; 1633 1634 if (!hba->use_pm_opp) { 1635 clki = list_first_entry(clk_list, struct ufs_clk_info, list); 1636 dev_pm_opp_add(hba->dev, clki->min_freq, 0); 1637 dev_pm_opp_add(hba->dev, clki->max_freq, 0); 1638 } 1639 1640 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile, 1641 &hba->vps->ondemand_data); 1642 devfreq = devfreq_add_device(hba->dev, 1643 &hba->vps->devfreq_profile, 1644 DEVFREQ_GOV_SIMPLE_ONDEMAND, 1645 &hba->vps->ondemand_data); 1646 if (IS_ERR(devfreq)) { 1647 ret = PTR_ERR(devfreq); 1648 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret); 1649 1650 if (!hba->use_pm_opp) { 1651 dev_pm_opp_remove(hba->dev, clki->min_freq); 1652 dev_pm_opp_remove(hba->dev, clki->max_freq); 1653 } 1654 return ret; 1655 } 1656 1657 hba->devfreq = devfreq; 1658 1659 return 0; 1660 } 1661 1662 static void ufshcd_devfreq_remove(struct ufs_hba *hba) 1663 { 1664 struct list_head *clk_list = &hba->clk_list_head; 1665 1666 if (!hba->devfreq) 1667 return; 1668 1669 devfreq_remove_device(hba->devfreq); 1670 hba->devfreq = NULL; 1671 1672 if (!hba->use_pm_opp) { 1673 struct ufs_clk_info *clki; 1674 1675 clki = list_first_entry(clk_list, struct ufs_clk_info, list); 1676 dev_pm_opp_remove(hba->dev, clki->min_freq); 1677 dev_pm_opp_remove(hba->dev, clki->max_freq); 1678 } 1679 } 1680 1681 static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) 1682 { 1683 unsigned long flags; 1684 bool suspend = false; 1685 1686 cancel_work_sync(&hba->clk_scaling.suspend_work); 1687 cancel_work_sync(&hba->clk_scaling.resume_work); 1688 1689 spin_lock_irqsave(hba->host->host_lock, flags); 1690 if (!hba->clk_scaling.is_suspended) { 1691 suspend = true; 1692 hba->clk_scaling.is_suspended = true; 1693 hba->clk_scaling.window_start_t = 0; 1694 } 1695 spin_unlock_irqrestore(hba->host->host_lock, flags); 1696 1697 if (suspend) 1698 devfreq_suspend_device(hba->devfreq); 1699 } 1700 1701 static void ufshcd_resume_clkscaling(struct ufs_hba *hba) 1702 { 1703 unsigned long flags; 1704 bool resume = false; 1705 1706 spin_lock_irqsave(hba->host->host_lock, flags); 1707 if (hba->clk_scaling.is_suspended) { 1708 resume = true; 1709 hba->clk_scaling.is_suspended = false; 1710 } 1711 spin_unlock_irqrestore(hba->host->host_lock, flags); 1712 1713 if (resume) 1714 devfreq_resume_device(hba->devfreq); 1715 } 1716 1717 static ssize_t ufshcd_clkscale_enable_show(struct device *dev, 1718 struct device_attribute *attr, char *buf) 1719 { 1720 struct ufs_hba *hba = dev_get_drvdata(dev); 1721 1722 return sysfs_emit(buf, "%d\n", hba->clk_scaling.is_enabled); 1723 } 1724 1725 static ssize_t ufshcd_clkscale_enable_store(struct device *dev, 1726 struct device_attribute *attr, const char *buf, size_t count) 1727 { 1728 struct ufs_hba *hba = dev_get_drvdata(dev); 1729 u32 value; 1730 int err = 0; 1731 1732 if (kstrtou32(buf, 0, &value)) 1733 return -EINVAL; 1734 1735 down(&hba->host_sem); 1736 if (!ufshcd_is_user_access_allowed(hba)) { 1737 err = -EBUSY; 1738 goto out; 1739 } 1740 1741 value = !!value; 1742 if (value == hba->clk_scaling.is_enabled) 1743 goto out; 1744 1745 ufshcd_rpm_get_sync(hba); 1746 ufshcd_hold(hba); 1747 1748 hba->clk_scaling.is_enabled = value; 1749 1750 if (value) { 1751 ufshcd_resume_clkscaling(hba); 1752 } else { 1753 ufshcd_suspend_clkscaling(hba); 1754 err = ufshcd_devfreq_scale(hba, ULONG_MAX, true); 1755 if (err) 1756 dev_err(hba->dev, "%s: failed to scale clocks up %d\n", 1757 __func__, err); 1758 } 1759 1760 ufshcd_release(hba); 1761 ufshcd_rpm_put_sync(hba); 1762 out: 1763 up(&hba->host_sem); 1764 return err ? err : count; 1765 } 1766 1767 static void ufshcd_init_clk_scaling_sysfs(struct ufs_hba *hba) 1768 { 1769 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; 1770 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; 1771 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); 1772 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; 1773 hba->clk_scaling.enable_attr.attr.mode = 0644; 1774 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) 1775 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); 1776 } 1777 1778 static void ufshcd_remove_clk_scaling_sysfs(struct ufs_hba *hba) 1779 { 1780 if (hba->clk_scaling.enable_attr.attr.name) 1781 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); 1782 } 1783 1784 static void ufshcd_init_clk_scaling(struct ufs_hba *hba) 1785 { 1786 char wq_name[sizeof("ufs_clkscaling_00")]; 1787 1788 if (!ufshcd_is_clkscaling_supported(hba)) 1789 return; 1790 1791 if (!hba->clk_scaling.min_gear) 1792 hba->clk_scaling.min_gear = UFS_HS_G1; 1793 1794 INIT_WORK(&hba->clk_scaling.suspend_work, 1795 ufshcd_clk_scaling_suspend_work); 1796 INIT_WORK(&hba->clk_scaling.resume_work, 1797 ufshcd_clk_scaling_resume_work); 1798 1799 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d", 1800 hba->host->host_no); 1801 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); 1802 1803 hba->clk_scaling.is_initialized = true; 1804 } 1805 1806 static void ufshcd_exit_clk_scaling(struct ufs_hba *hba) 1807 { 1808 if (!hba->clk_scaling.is_initialized) 1809 return; 1810 1811 ufshcd_remove_clk_scaling_sysfs(hba); 1812 destroy_workqueue(hba->clk_scaling.workq); 1813 ufshcd_devfreq_remove(hba); 1814 hba->clk_scaling.is_initialized = false; 1815 } 1816 1817 static void ufshcd_ungate_work(struct work_struct *work) 1818 { 1819 int ret; 1820 unsigned long flags; 1821 struct ufs_hba *hba = container_of(work, struct ufs_hba, 1822 clk_gating.ungate_work); 1823 1824 cancel_delayed_work_sync(&hba->clk_gating.gate_work); 1825 1826 spin_lock_irqsave(hba->host->host_lock, flags); 1827 if (hba->clk_gating.state == CLKS_ON) { 1828 spin_unlock_irqrestore(hba->host->host_lock, flags); 1829 return; 1830 } 1831 1832 spin_unlock_irqrestore(hba->host->host_lock, flags); 1833 ufshcd_hba_vreg_set_hpm(hba); 1834 ufshcd_setup_clocks(hba, true); 1835 1836 ufshcd_enable_irq(hba); 1837 1838 /* Exit from hibern8 */ 1839 if (ufshcd_can_hibern8_during_gating(hba)) { 1840 /* Prevent gating in this path */ 1841 hba->clk_gating.is_suspended = true; 1842 if (ufshcd_is_link_hibern8(hba)) { 1843 ret = ufshcd_uic_hibern8_exit(hba); 1844 if (ret) 1845 dev_err(hba->dev, "%s: hibern8 exit failed %d\n", 1846 __func__, ret); 1847 else 1848 ufshcd_set_link_active(hba); 1849 } 1850 hba->clk_gating.is_suspended = false; 1851 } 1852 } 1853 1854 /** 1855 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release. 1856 * Also, exit from hibern8 mode and set the link as active. 1857 * @hba: per adapter instance 1858 */ 1859 void ufshcd_hold(struct ufs_hba *hba) 1860 { 1861 bool flush_result; 1862 unsigned long flags; 1863 1864 if (!ufshcd_is_clkgating_allowed(hba) || 1865 !hba->clk_gating.is_initialized) 1866 return; 1867 spin_lock_irqsave(hba->host->host_lock, flags); 1868 hba->clk_gating.active_reqs++; 1869 1870 start: 1871 switch (hba->clk_gating.state) { 1872 case CLKS_ON: 1873 /* 1874 * Wait for the ungate work to complete if in progress. 1875 * Though the clocks may be in ON state, the link could 1876 * still be in hibner8 state if hibern8 is allowed 1877 * during clock gating. 1878 * Make sure we exit hibern8 state also in addition to 1879 * clocks being ON. 1880 */ 1881 if (ufshcd_can_hibern8_during_gating(hba) && 1882 ufshcd_is_link_hibern8(hba)) { 1883 spin_unlock_irqrestore(hba->host->host_lock, flags); 1884 flush_result = flush_work(&hba->clk_gating.ungate_work); 1885 if (hba->clk_gating.is_suspended && !flush_result) 1886 return; 1887 spin_lock_irqsave(hba->host->host_lock, flags); 1888 goto start; 1889 } 1890 break; 1891 case REQ_CLKS_OFF: 1892 if (cancel_delayed_work(&hba->clk_gating.gate_work)) { 1893 hba->clk_gating.state = CLKS_ON; 1894 trace_ufshcd_clk_gating(dev_name(hba->dev), 1895 hba->clk_gating.state); 1896 break; 1897 } 1898 /* 1899 * If we are here, it means gating work is either done or 1900 * currently running. Hence, fall through to cancel gating 1901 * work and to enable clocks. 1902 */ 1903 fallthrough; 1904 case CLKS_OFF: 1905 hba->clk_gating.state = REQ_CLKS_ON; 1906 trace_ufshcd_clk_gating(dev_name(hba->dev), 1907 hba->clk_gating.state); 1908 queue_work(hba->clk_gating.clk_gating_workq, 1909 &hba->clk_gating.ungate_work); 1910 /* 1911 * fall through to check if we should wait for this 1912 * work to be done or not. 1913 */ 1914 fallthrough; 1915 case REQ_CLKS_ON: 1916 spin_unlock_irqrestore(hba->host->host_lock, flags); 1917 flush_work(&hba->clk_gating.ungate_work); 1918 /* Make sure state is CLKS_ON before returning */ 1919 spin_lock_irqsave(hba->host->host_lock, flags); 1920 goto start; 1921 default: 1922 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", 1923 __func__, hba->clk_gating.state); 1924 break; 1925 } 1926 spin_unlock_irqrestore(hba->host->host_lock, flags); 1927 } 1928 EXPORT_SYMBOL_GPL(ufshcd_hold); 1929 1930 static void ufshcd_gate_work(struct work_struct *work) 1931 { 1932 struct ufs_hba *hba = container_of(work, struct ufs_hba, 1933 clk_gating.gate_work.work); 1934 unsigned long flags; 1935 int ret; 1936 1937 spin_lock_irqsave(hba->host->host_lock, flags); 1938 /* 1939 * In case you are here to cancel this work the gating state 1940 * would be marked as REQ_CLKS_ON. In this case save time by 1941 * skipping the gating work and exit after changing the clock 1942 * state to CLKS_ON. 1943 */ 1944 if (hba->clk_gating.is_suspended || 1945 (hba->clk_gating.state != REQ_CLKS_OFF)) { 1946 hba->clk_gating.state = CLKS_ON; 1947 trace_ufshcd_clk_gating(dev_name(hba->dev), 1948 hba->clk_gating.state); 1949 goto rel_lock; 1950 } 1951 1952 if (ufshcd_is_ufs_dev_busy(hba) || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) 1953 goto rel_lock; 1954 1955 spin_unlock_irqrestore(hba->host->host_lock, flags); 1956 1957 /* put the link into hibern8 mode before turning off clocks */ 1958 if (ufshcd_can_hibern8_during_gating(hba)) { 1959 ret = ufshcd_uic_hibern8_enter(hba); 1960 if (ret) { 1961 hba->clk_gating.state = CLKS_ON; 1962 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", 1963 __func__, ret); 1964 trace_ufshcd_clk_gating(dev_name(hba->dev), 1965 hba->clk_gating.state); 1966 goto out; 1967 } 1968 ufshcd_set_link_hibern8(hba); 1969 } 1970 1971 ufshcd_disable_irq(hba); 1972 1973 ufshcd_setup_clocks(hba, false); 1974 1975 /* Put the host controller in low power mode if possible */ 1976 ufshcd_hba_vreg_set_lpm(hba); 1977 /* 1978 * In case you are here to cancel this work the gating state 1979 * would be marked as REQ_CLKS_ON. In this case keep the state 1980 * as REQ_CLKS_ON which would anyway imply that clocks are off 1981 * and a request to turn them on is pending. By doing this way, 1982 * we keep the state machine in tact and this would ultimately 1983 * prevent from doing cancel work multiple times when there are 1984 * new requests arriving before the current cancel work is done. 1985 */ 1986 spin_lock_irqsave(hba->host->host_lock, flags); 1987 if (hba->clk_gating.state == REQ_CLKS_OFF) { 1988 hba->clk_gating.state = CLKS_OFF; 1989 trace_ufshcd_clk_gating(dev_name(hba->dev), 1990 hba->clk_gating.state); 1991 } 1992 rel_lock: 1993 spin_unlock_irqrestore(hba->host->host_lock, flags); 1994 out: 1995 return; 1996 } 1997 1998 /* host lock must be held before calling this variant */ 1999 static void __ufshcd_release(struct ufs_hba *hba) 2000 { 2001 if (!ufshcd_is_clkgating_allowed(hba)) 2002 return; 2003 2004 hba->clk_gating.active_reqs--; 2005 2006 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended || 2007 hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL || 2008 hba->outstanding_tasks || !hba->clk_gating.is_initialized || 2009 hba->active_uic_cmd || hba->uic_async_done || 2010 hba->clk_gating.state == CLKS_OFF) 2011 return; 2012 2013 hba->clk_gating.state = REQ_CLKS_OFF; 2014 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); 2015 queue_delayed_work(hba->clk_gating.clk_gating_workq, 2016 &hba->clk_gating.gate_work, 2017 msecs_to_jiffies(hba->clk_gating.delay_ms)); 2018 } 2019 2020 void ufshcd_release(struct ufs_hba *hba) 2021 { 2022 unsigned long flags; 2023 2024 spin_lock_irqsave(hba->host->host_lock, flags); 2025 __ufshcd_release(hba); 2026 spin_unlock_irqrestore(hba->host->host_lock, flags); 2027 } 2028 EXPORT_SYMBOL_GPL(ufshcd_release); 2029 2030 static ssize_t ufshcd_clkgate_delay_show(struct device *dev, 2031 struct device_attribute *attr, char *buf) 2032 { 2033 struct ufs_hba *hba = dev_get_drvdata(dev); 2034 2035 return sysfs_emit(buf, "%lu\n", hba->clk_gating.delay_ms); 2036 } 2037 2038 void ufshcd_clkgate_delay_set(struct device *dev, unsigned long value) 2039 { 2040 struct ufs_hba *hba = dev_get_drvdata(dev); 2041 unsigned long flags; 2042 2043 spin_lock_irqsave(hba->host->host_lock, flags); 2044 hba->clk_gating.delay_ms = value; 2045 spin_unlock_irqrestore(hba->host->host_lock, flags); 2046 } 2047 EXPORT_SYMBOL_GPL(ufshcd_clkgate_delay_set); 2048 2049 static ssize_t ufshcd_clkgate_delay_store(struct device *dev, 2050 struct device_attribute *attr, const char *buf, size_t count) 2051 { 2052 unsigned long value; 2053 2054 if (kstrtoul(buf, 0, &value)) 2055 return -EINVAL; 2056 2057 ufshcd_clkgate_delay_set(dev, value); 2058 return count; 2059 } 2060 2061 static ssize_t ufshcd_clkgate_enable_show(struct device *dev, 2062 struct device_attribute *attr, char *buf) 2063 { 2064 struct ufs_hba *hba = dev_get_drvdata(dev); 2065 2066 return sysfs_emit(buf, "%d\n", hba->clk_gating.is_enabled); 2067 } 2068 2069 static ssize_t ufshcd_clkgate_enable_store(struct device *dev, 2070 struct device_attribute *attr, const char *buf, size_t count) 2071 { 2072 struct ufs_hba *hba = dev_get_drvdata(dev); 2073 unsigned long flags; 2074 u32 value; 2075 2076 if (kstrtou32(buf, 0, &value)) 2077 return -EINVAL; 2078 2079 value = !!value; 2080 2081 spin_lock_irqsave(hba->host->host_lock, flags); 2082 if (value == hba->clk_gating.is_enabled) 2083 goto out; 2084 2085 if (value) 2086 __ufshcd_release(hba); 2087 else 2088 hba->clk_gating.active_reqs++; 2089 2090 hba->clk_gating.is_enabled = value; 2091 out: 2092 spin_unlock_irqrestore(hba->host->host_lock, flags); 2093 return count; 2094 } 2095 2096 static void ufshcd_init_clk_gating_sysfs(struct ufs_hba *hba) 2097 { 2098 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; 2099 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; 2100 sysfs_attr_init(&hba->clk_gating.delay_attr.attr); 2101 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; 2102 hba->clk_gating.delay_attr.attr.mode = 0644; 2103 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) 2104 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); 2105 2106 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show; 2107 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store; 2108 sysfs_attr_init(&hba->clk_gating.enable_attr.attr); 2109 hba->clk_gating.enable_attr.attr.name = "clkgate_enable"; 2110 hba->clk_gating.enable_attr.attr.mode = 0644; 2111 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr)) 2112 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); 2113 } 2114 2115 static void ufshcd_remove_clk_gating_sysfs(struct ufs_hba *hba) 2116 { 2117 if (hba->clk_gating.delay_attr.attr.name) 2118 device_remove_file(hba->dev, &hba->clk_gating.delay_attr); 2119 if (hba->clk_gating.enable_attr.attr.name) 2120 device_remove_file(hba->dev, &hba->clk_gating.enable_attr); 2121 } 2122 2123 static void ufshcd_init_clk_gating(struct ufs_hba *hba) 2124 { 2125 char wq_name[sizeof("ufs_clk_gating_00")]; 2126 2127 if (!ufshcd_is_clkgating_allowed(hba)) 2128 return; 2129 2130 hba->clk_gating.state = CLKS_ON; 2131 2132 hba->clk_gating.delay_ms = 150; 2133 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); 2134 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); 2135 2136 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d", 2137 hba->host->host_no); 2138 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name, 2139 WQ_MEM_RECLAIM | WQ_HIGHPRI); 2140 2141 ufshcd_init_clk_gating_sysfs(hba); 2142 2143 hba->clk_gating.is_enabled = true; 2144 hba->clk_gating.is_initialized = true; 2145 } 2146 2147 static void ufshcd_exit_clk_gating(struct ufs_hba *hba) 2148 { 2149 if (!hba->clk_gating.is_initialized) 2150 return; 2151 2152 ufshcd_remove_clk_gating_sysfs(hba); 2153 2154 /* Ungate the clock if necessary. */ 2155 ufshcd_hold(hba); 2156 hba->clk_gating.is_initialized = false; 2157 ufshcd_release(hba); 2158 2159 destroy_workqueue(hba->clk_gating.clk_gating_workq); 2160 } 2161 2162 static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) 2163 { 2164 bool queue_resume_work = false; 2165 ktime_t curr_t = ktime_get(); 2166 unsigned long flags; 2167 2168 if (!ufshcd_is_clkscaling_supported(hba)) 2169 return; 2170 2171 spin_lock_irqsave(hba->host->host_lock, flags); 2172 if (!hba->clk_scaling.active_reqs++) 2173 queue_resume_work = true; 2174 2175 if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress) { 2176 spin_unlock_irqrestore(hba->host->host_lock, flags); 2177 return; 2178 } 2179 2180 if (queue_resume_work) 2181 queue_work(hba->clk_scaling.workq, 2182 &hba->clk_scaling.resume_work); 2183 2184 if (!hba->clk_scaling.window_start_t) { 2185 hba->clk_scaling.window_start_t = curr_t; 2186 hba->clk_scaling.tot_busy_t = 0; 2187 hba->clk_scaling.is_busy_started = false; 2188 } 2189 2190 if (!hba->clk_scaling.is_busy_started) { 2191 hba->clk_scaling.busy_start_t = curr_t; 2192 hba->clk_scaling.is_busy_started = true; 2193 } 2194 spin_unlock_irqrestore(hba->host->host_lock, flags); 2195 } 2196 2197 static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) 2198 { 2199 struct ufs_clk_scaling *scaling = &hba->clk_scaling; 2200 unsigned long flags; 2201 2202 if (!ufshcd_is_clkscaling_supported(hba)) 2203 return; 2204 2205 spin_lock_irqsave(hba->host->host_lock, flags); 2206 hba->clk_scaling.active_reqs--; 2207 if (!scaling->active_reqs && scaling->is_busy_started) { 2208 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), 2209 scaling->busy_start_t)); 2210 scaling->busy_start_t = 0; 2211 scaling->is_busy_started = false; 2212 } 2213 spin_unlock_irqrestore(hba->host->host_lock, flags); 2214 } 2215 2216 static inline int ufshcd_monitor_opcode2dir(u8 opcode) 2217 { 2218 if (opcode == READ_6 || opcode == READ_10 || opcode == READ_16) 2219 return READ; 2220 else if (opcode == WRITE_6 || opcode == WRITE_10 || opcode == WRITE_16) 2221 return WRITE; 2222 else 2223 return -EINVAL; 2224 } 2225 2226 static inline bool ufshcd_should_inform_monitor(struct ufs_hba *hba, 2227 struct ufshcd_lrb *lrbp) 2228 { 2229 const struct ufs_hba_monitor *m = &hba->monitor; 2230 2231 return (m->enabled && lrbp && lrbp->cmd && 2232 (!m->chunk_size || m->chunk_size == lrbp->cmd->sdb.length) && 2233 ktime_before(hba->monitor.enabled_ts, lrbp->issue_time_stamp)); 2234 } 2235 2236 static void ufshcd_start_monitor(struct ufs_hba *hba, 2237 const struct ufshcd_lrb *lrbp) 2238 { 2239 int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd); 2240 unsigned long flags; 2241 2242 spin_lock_irqsave(hba->host->host_lock, flags); 2243 if (dir >= 0 && hba->monitor.nr_queued[dir]++ == 0) 2244 hba->monitor.busy_start_ts[dir] = ktime_get(); 2245 spin_unlock_irqrestore(hba->host->host_lock, flags); 2246 } 2247 2248 static void ufshcd_update_monitor(struct ufs_hba *hba, const struct ufshcd_lrb *lrbp) 2249 { 2250 int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd); 2251 unsigned long flags; 2252 2253 spin_lock_irqsave(hba->host->host_lock, flags); 2254 if (dir >= 0 && hba->monitor.nr_queued[dir] > 0) { 2255 const struct request *req = scsi_cmd_to_rq(lrbp->cmd); 2256 struct ufs_hba_monitor *m = &hba->monitor; 2257 ktime_t now, inc, lat; 2258 2259 now = lrbp->compl_time_stamp; 2260 inc = ktime_sub(now, m->busy_start_ts[dir]); 2261 m->total_busy[dir] = ktime_add(m->total_busy[dir], inc); 2262 m->nr_sec_rw[dir] += blk_rq_sectors(req); 2263 2264 /* Update latencies */ 2265 m->nr_req[dir]++; 2266 lat = ktime_sub(now, lrbp->issue_time_stamp); 2267 m->lat_sum[dir] += lat; 2268 if (m->lat_max[dir] < lat || !m->lat_max[dir]) 2269 m->lat_max[dir] = lat; 2270 if (m->lat_min[dir] > lat || !m->lat_min[dir]) 2271 m->lat_min[dir] = lat; 2272 2273 m->nr_queued[dir]--; 2274 /* Push forward the busy start of monitor */ 2275 m->busy_start_ts[dir] = now; 2276 } 2277 spin_unlock_irqrestore(hba->host->host_lock, flags); 2278 } 2279 2280 /** 2281 * ufshcd_send_command - Send SCSI or device management commands 2282 * @hba: per adapter instance 2283 * @task_tag: Task tag of the command 2284 * @hwq: pointer to hardware queue instance 2285 */ 2286 static inline 2287 void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag, 2288 struct ufs_hw_queue *hwq) 2289 { 2290 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag]; 2291 unsigned long flags; 2292 2293 lrbp->issue_time_stamp = ktime_get(); 2294 lrbp->issue_time_stamp_local_clock = local_clock(); 2295 lrbp->compl_time_stamp = ktime_set(0, 0); 2296 lrbp->compl_time_stamp_local_clock = 0; 2297 ufshcd_add_command_trace(hba, task_tag, UFS_CMD_SEND); 2298 if (lrbp->cmd) 2299 ufshcd_clk_scaling_start_busy(hba); 2300 if (unlikely(ufshcd_should_inform_monitor(hba, lrbp))) 2301 ufshcd_start_monitor(hba, lrbp); 2302 2303 if (is_mcq_enabled(hba)) { 2304 int utrd_size = sizeof(struct utp_transfer_req_desc); 2305 struct utp_transfer_req_desc *src = lrbp->utr_descriptor_ptr; 2306 struct utp_transfer_req_desc *dest; 2307 2308 spin_lock(&hwq->sq_lock); 2309 dest = hwq->sqe_base_addr + hwq->sq_tail_slot; 2310 memcpy(dest, src, utrd_size); 2311 ufshcd_inc_sq_tail(hwq); 2312 spin_unlock(&hwq->sq_lock); 2313 } else { 2314 spin_lock_irqsave(&hba->outstanding_lock, flags); 2315 if (hba->vops && hba->vops->setup_xfer_req) 2316 hba->vops->setup_xfer_req(hba, lrbp->task_tag, 2317 !!lrbp->cmd); 2318 __set_bit(lrbp->task_tag, &hba->outstanding_reqs); 2319 ufshcd_writel(hba, 1 << lrbp->task_tag, 2320 REG_UTP_TRANSFER_REQ_DOOR_BELL); 2321 spin_unlock_irqrestore(&hba->outstanding_lock, flags); 2322 } 2323 } 2324 2325 /** 2326 * ufshcd_copy_sense_data - Copy sense data in case of check condition 2327 * @lrbp: pointer to local reference block 2328 */ 2329 static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) 2330 { 2331 u8 *const sense_buffer = lrbp->cmd->sense_buffer; 2332 u16 resp_len; 2333 int len; 2334 2335 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header.data_segment_length); 2336 if (sense_buffer && resp_len) { 2337 int len_to_copy; 2338 2339 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); 2340 len_to_copy = min_t(int, UFS_SENSE_SIZE, len); 2341 2342 memcpy(sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data, 2343 len_to_copy); 2344 } 2345 } 2346 2347 /** 2348 * ufshcd_copy_query_response() - Copy the Query Response and the data 2349 * descriptor 2350 * @hba: per adapter instance 2351 * @lrbp: pointer to local reference block 2352 * 2353 * Return: 0 upon success; < 0 upon failure. 2354 */ 2355 static 2356 int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) 2357 { 2358 struct ufs_query_res *query_res = &hba->dev_cmd.query.response; 2359 2360 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE); 2361 2362 /* Get the descriptor */ 2363 if (hba->dev_cmd.query.descriptor && 2364 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { 2365 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + 2366 GENERAL_UPIU_REQUEST_SIZE; 2367 u16 resp_len; 2368 u16 buf_len; 2369 2370 /* data segment length */ 2371 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header 2372 .data_segment_length); 2373 buf_len = be16_to_cpu( 2374 hba->dev_cmd.query.request.upiu_req.length); 2375 if (likely(buf_len >= resp_len)) { 2376 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); 2377 } else { 2378 dev_warn(hba->dev, 2379 "%s: rsp size %d is bigger than buffer size %d", 2380 __func__, resp_len, buf_len); 2381 return -EINVAL; 2382 } 2383 } 2384 2385 return 0; 2386 } 2387 2388 /** 2389 * ufshcd_hba_capabilities - Read controller capabilities 2390 * @hba: per adapter instance 2391 * 2392 * Return: 0 on success, negative on error. 2393 */ 2394 static inline int ufshcd_hba_capabilities(struct ufs_hba *hba) 2395 { 2396 int err; 2397 2398 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); 2399 if (hba->quirks & UFSHCD_QUIRK_BROKEN_64BIT_ADDRESS) 2400 hba->capabilities &= ~MASK_64_ADDRESSING_SUPPORT; 2401 2402 /* nutrs and nutmrs are 0 based values */ 2403 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; 2404 hba->nutmrs = 2405 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; 2406 hba->reserved_slot = hba->nutrs - 1; 2407 2408 /* Read crypto capabilities */ 2409 err = ufshcd_hba_init_crypto_capabilities(hba); 2410 if (err) { 2411 dev_err(hba->dev, "crypto setup failed\n"); 2412 return err; 2413 } 2414 2415 hba->mcq_sup = FIELD_GET(MASK_MCQ_SUPPORT, hba->capabilities); 2416 if (!hba->mcq_sup) 2417 return 0; 2418 2419 hba->mcq_capabilities = ufshcd_readl(hba, REG_MCQCAP); 2420 hba->ext_iid_sup = FIELD_GET(MASK_EXT_IID_SUPPORT, 2421 hba->mcq_capabilities); 2422 2423 return 0; 2424 } 2425 2426 /** 2427 * ufshcd_ready_for_uic_cmd - Check if controller is ready 2428 * to accept UIC commands 2429 * @hba: per adapter instance 2430 * 2431 * Return: true on success, else false. 2432 */ 2433 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) 2434 { 2435 u32 val; 2436 int ret = read_poll_timeout(ufshcd_readl, val, val & UIC_COMMAND_READY, 2437 500, UIC_CMD_TIMEOUT * 1000, false, hba, 2438 REG_CONTROLLER_STATUS); 2439 return ret == 0; 2440 } 2441 2442 /** 2443 * ufshcd_get_upmcrs - Get the power mode change request status 2444 * @hba: Pointer to adapter instance 2445 * 2446 * This function gets the UPMCRS field of HCS register 2447 * 2448 * Return: value of UPMCRS field. 2449 */ 2450 static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) 2451 { 2452 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; 2453 } 2454 2455 /** 2456 * ufshcd_dispatch_uic_cmd - Dispatch an UIC command to the Unipro layer 2457 * @hba: per adapter instance 2458 * @uic_cmd: UIC command 2459 */ 2460 static inline void 2461 ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) 2462 { 2463 lockdep_assert_held(&hba->uic_cmd_mutex); 2464 2465 WARN_ON(hba->active_uic_cmd); 2466 2467 hba->active_uic_cmd = uic_cmd; 2468 2469 /* Write Args */ 2470 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); 2471 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); 2472 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); 2473 2474 ufshcd_add_uic_command_trace(hba, uic_cmd, UFS_CMD_SEND); 2475 2476 /* Write UIC Cmd */ 2477 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, 2478 REG_UIC_COMMAND); 2479 } 2480 2481 /** 2482 * ufshcd_wait_for_uic_cmd - Wait for completion of an UIC command 2483 * @hba: per adapter instance 2484 * @uic_cmd: UIC command 2485 * 2486 * Return: 0 only if success. 2487 */ 2488 static int 2489 ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) 2490 { 2491 int ret; 2492 unsigned long flags; 2493 2494 lockdep_assert_held(&hba->uic_cmd_mutex); 2495 2496 if (wait_for_completion_timeout(&uic_cmd->done, 2497 msecs_to_jiffies(UIC_CMD_TIMEOUT))) { 2498 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; 2499 } else { 2500 ret = -ETIMEDOUT; 2501 dev_err(hba->dev, 2502 "uic cmd 0x%x with arg3 0x%x completion timeout\n", 2503 uic_cmd->command, uic_cmd->argument3); 2504 2505 if (!uic_cmd->cmd_active) { 2506 dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n", 2507 __func__); 2508 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; 2509 } 2510 } 2511 2512 spin_lock_irqsave(hba->host->host_lock, flags); 2513 hba->active_uic_cmd = NULL; 2514 spin_unlock_irqrestore(hba->host->host_lock, flags); 2515 2516 return ret; 2517 } 2518 2519 /** 2520 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result 2521 * @hba: per adapter instance 2522 * @uic_cmd: UIC command 2523 * @completion: initialize the completion only if this is set to true 2524 * 2525 * Return: 0 only if success. 2526 */ 2527 static int 2528 __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd, 2529 bool completion) 2530 { 2531 lockdep_assert_held(&hba->uic_cmd_mutex); 2532 2533 if (!ufshcd_ready_for_uic_cmd(hba)) { 2534 dev_err(hba->dev, 2535 "Controller not ready to accept UIC commands\n"); 2536 return -EIO; 2537 } 2538 2539 if (completion) 2540 init_completion(&uic_cmd->done); 2541 2542 uic_cmd->cmd_active = 1; 2543 ufshcd_dispatch_uic_cmd(hba, uic_cmd); 2544 2545 return 0; 2546 } 2547 2548 /** 2549 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result 2550 * @hba: per adapter instance 2551 * @uic_cmd: UIC command 2552 * 2553 * Return: 0 only if success. 2554 */ 2555 int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) 2556 { 2557 int ret; 2558 2559 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD) 2560 return 0; 2561 2562 ufshcd_hold(hba); 2563 mutex_lock(&hba->uic_cmd_mutex); 2564 ufshcd_add_delay_before_dme_cmd(hba); 2565 2566 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true); 2567 if (!ret) 2568 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); 2569 2570 mutex_unlock(&hba->uic_cmd_mutex); 2571 2572 ufshcd_release(hba); 2573 return ret; 2574 } 2575 2576 /** 2577 * ufshcd_sgl_to_prdt - SG list to PRTD (Physical Region Description Table, 4DW format) 2578 * @hba: per-adapter instance 2579 * @lrbp: pointer to local reference block 2580 * @sg_entries: The number of sg lists actually used 2581 * @sg_list: Pointer to SG list 2582 */ 2583 static void ufshcd_sgl_to_prdt(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, int sg_entries, 2584 struct scatterlist *sg_list) 2585 { 2586 struct ufshcd_sg_entry *prd; 2587 struct scatterlist *sg; 2588 int i; 2589 2590 if (sg_entries) { 2591 2592 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) 2593 lrbp->utr_descriptor_ptr->prd_table_length = 2594 cpu_to_le16(sg_entries * ufshcd_sg_entry_size(hba)); 2595 else 2596 lrbp->utr_descriptor_ptr->prd_table_length = cpu_to_le16(sg_entries); 2597 2598 prd = lrbp->ucd_prdt_ptr; 2599 2600 for_each_sg(sg_list, sg, sg_entries, i) { 2601 const unsigned int len = sg_dma_len(sg); 2602 2603 /* 2604 * From the UFSHCI spec: "Data Byte Count (DBC): A '0' 2605 * based value that indicates the length, in bytes, of 2606 * the data block. A maximum of length of 256KB may 2607 * exist for any entry. Bits 1:0 of this field shall be 2608 * 11b to indicate Dword granularity. A value of '3' 2609 * indicates 4 bytes, '7' indicates 8 bytes, etc." 2610 */ 2611 WARN_ONCE(len > SZ_256K, "len = %#x\n", len); 2612 prd->size = cpu_to_le32(len - 1); 2613 prd->addr = cpu_to_le64(sg->dma_address); 2614 prd->reserved = 0; 2615 prd = (void *)prd + ufshcd_sg_entry_size(hba); 2616 } 2617 } else { 2618 lrbp->utr_descriptor_ptr->prd_table_length = 0; 2619 } 2620 } 2621 2622 /** 2623 * ufshcd_map_sg - Map scatter-gather list to prdt 2624 * @hba: per adapter instance 2625 * @lrbp: pointer to local reference block 2626 * 2627 * Return: 0 in case of success, non-zero value in case of failure. 2628 */ 2629 static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) 2630 { 2631 struct scsi_cmnd *cmd = lrbp->cmd; 2632 int sg_segments = scsi_dma_map(cmd); 2633 2634 if (sg_segments < 0) 2635 return sg_segments; 2636 2637 ufshcd_sgl_to_prdt(hba, lrbp, sg_segments, scsi_sglist(cmd)); 2638 2639 return 0; 2640 } 2641 2642 /** 2643 * ufshcd_enable_intr - enable interrupts 2644 * @hba: per adapter instance 2645 * @intrs: interrupt bits 2646 */ 2647 static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) 2648 { 2649 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); 2650 2651 set |= intrs; 2652 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); 2653 } 2654 2655 /** 2656 * ufshcd_disable_intr - disable interrupts 2657 * @hba: per adapter instance 2658 * @intrs: interrupt bits 2659 */ 2660 static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) 2661 { 2662 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); 2663 2664 set &= ~intrs; 2665 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); 2666 } 2667 2668 /** 2669 * ufshcd_prepare_req_desc_hdr - Fill UTP Transfer request descriptor header according to request 2670 * descriptor according to request 2671 * @hba: per adapter instance 2672 * @lrbp: pointer to local reference block 2673 * @upiu_flags: flags required in the header 2674 * @cmd_dir: requests data direction 2675 * @ehs_length: Total EHS Length (in 32‐bytes units of all Extra Header Segments) 2676 */ 2677 static void 2678 ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, 2679 u8 *upiu_flags, enum dma_data_direction cmd_dir, 2680 int ehs_length) 2681 { 2682 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; 2683 struct request_desc_header *h = &req_desc->header; 2684 enum utp_data_direction data_direction; 2685 2686 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; 2687 2688 *h = (typeof(*h)){ }; 2689 2690 if (cmd_dir == DMA_FROM_DEVICE) { 2691 data_direction = UTP_DEVICE_TO_HOST; 2692 *upiu_flags = UPIU_CMD_FLAGS_READ; 2693 } else if (cmd_dir == DMA_TO_DEVICE) { 2694 data_direction = UTP_HOST_TO_DEVICE; 2695 *upiu_flags = UPIU_CMD_FLAGS_WRITE; 2696 } else { 2697 data_direction = UTP_NO_DATA_TRANSFER; 2698 *upiu_flags = UPIU_CMD_FLAGS_NONE; 2699 } 2700 2701 h->command_type = lrbp->command_type; 2702 h->data_direction = data_direction; 2703 h->ehs_length = ehs_length; 2704 2705 if (lrbp->intr_cmd) 2706 h->interrupt = 1; 2707 2708 /* Prepare crypto related dwords */ 2709 ufshcd_prepare_req_desc_hdr_crypto(lrbp, h); 2710 2711 /* 2712 * assigning invalid value for command status. Controller 2713 * updates OCS on command completion, with the command 2714 * status 2715 */ 2716 h->ocs = OCS_INVALID_COMMAND_STATUS; 2717 2718 req_desc->prd_table_length = 0; 2719 } 2720 2721 /** 2722 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc, 2723 * for scsi commands 2724 * @lrbp: local reference block pointer 2725 * @upiu_flags: flags 2726 */ 2727 static 2728 void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u8 upiu_flags) 2729 { 2730 struct scsi_cmnd *cmd = lrbp->cmd; 2731 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; 2732 unsigned short cdb_len; 2733 2734 ucd_req_ptr->header = (struct utp_upiu_header){ 2735 .transaction_code = UPIU_TRANSACTION_COMMAND, 2736 .flags = upiu_flags, 2737 .lun = lrbp->lun, 2738 .task_tag = lrbp->task_tag, 2739 .command_set_type = UPIU_COMMAND_SET_TYPE_SCSI, 2740 }; 2741 2742 WARN_ON_ONCE(ucd_req_ptr->header.task_tag != lrbp->task_tag); 2743 2744 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length); 2745 2746 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE); 2747 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE); 2748 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len); 2749 2750 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); 2751 } 2752 2753 /** 2754 * ufshcd_prepare_utp_query_req_upiu() - fill the utp_transfer_req_desc for query request 2755 * @hba: UFS hba 2756 * @lrbp: local reference block pointer 2757 * @upiu_flags: flags 2758 */ 2759 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, 2760 struct ufshcd_lrb *lrbp, u8 upiu_flags) 2761 { 2762 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; 2763 struct ufs_query *query = &hba->dev_cmd.query; 2764 u16 len = be16_to_cpu(query->request.upiu_req.length); 2765 2766 /* Query request header */ 2767 ucd_req_ptr->header = (struct utp_upiu_header){ 2768 .transaction_code = UPIU_TRANSACTION_QUERY_REQ, 2769 .flags = upiu_flags, 2770 .lun = lrbp->lun, 2771 .task_tag = lrbp->task_tag, 2772 .query_function = query->request.query_func, 2773 /* Data segment length only need for WRITE_DESC */ 2774 .data_segment_length = 2775 query->request.upiu_req.opcode == 2776 UPIU_QUERY_OPCODE_WRITE_DESC ? 2777 cpu_to_be16(len) : 2778 0, 2779 }; 2780 2781 /* Copy the Query Request buffer as is */ 2782 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, 2783 QUERY_OSF_SIZE); 2784 2785 /* Copy the Descriptor */ 2786 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) 2787 memcpy(ucd_req_ptr + 1, query->descriptor, len); 2788 2789 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); 2790 } 2791 2792 static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp) 2793 { 2794 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; 2795 2796 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req)); 2797 2798 ucd_req_ptr->header = (struct utp_upiu_header){ 2799 .transaction_code = UPIU_TRANSACTION_NOP_OUT, 2800 .task_tag = lrbp->task_tag, 2801 }; 2802 2803 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); 2804 } 2805 2806 /** 2807 * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU) 2808 * for Device Management Purposes 2809 * @hba: per adapter instance 2810 * @lrbp: pointer to local reference block 2811 * 2812 * Return: 0 upon success; < 0 upon failure. 2813 */ 2814 static int ufshcd_compose_devman_upiu(struct ufs_hba *hba, 2815 struct ufshcd_lrb *lrbp) 2816 { 2817 u8 upiu_flags; 2818 int ret = 0; 2819 2820 ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, DMA_NONE, 0); 2821 2822 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) 2823 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags); 2824 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) 2825 ufshcd_prepare_utp_nop_upiu(lrbp); 2826 else 2827 ret = -EINVAL; 2828 2829 return ret; 2830 } 2831 2832 /** 2833 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU) 2834 * for SCSI Purposes 2835 * @hba: per adapter instance 2836 * @lrbp: pointer to local reference block 2837 */ 2838 static void ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) 2839 { 2840 struct request *rq = scsi_cmd_to_rq(lrbp->cmd); 2841 unsigned int ioprio_class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq)); 2842 u8 upiu_flags; 2843 2844 ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, lrbp->cmd->sc_data_direction, 0); 2845 if (ioprio_class == IOPRIO_CLASS_RT) 2846 upiu_flags |= UPIU_CMD_FLAGS_CP; 2847 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags); 2848 } 2849 2850 /** 2851 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID 2852 * @upiu_wlun_id: UPIU W-LUN id 2853 * 2854 * Return: SCSI W-LUN id. 2855 */ 2856 static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id) 2857 { 2858 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE; 2859 } 2860 2861 static inline bool is_device_wlun(struct scsi_device *sdev) 2862 { 2863 return sdev->lun == 2864 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN); 2865 } 2866 2867 /* 2868 * Associate the UFS controller queue with the default and poll HCTX types. 2869 * Initialize the mq_map[] arrays. 2870 */ 2871 static void ufshcd_map_queues(struct Scsi_Host *shost) 2872 { 2873 struct ufs_hba *hba = shost_priv(shost); 2874 int i, queue_offset = 0; 2875 2876 if (!is_mcq_supported(hba)) { 2877 hba->nr_queues[HCTX_TYPE_DEFAULT] = 1; 2878 hba->nr_queues[HCTX_TYPE_READ] = 0; 2879 hba->nr_queues[HCTX_TYPE_POLL] = 1; 2880 hba->nr_hw_queues = 1; 2881 } 2882 2883 for (i = 0; i < shost->nr_maps; i++) { 2884 struct blk_mq_queue_map *map = &shost->tag_set.map[i]; 2885 2886 map->nr_queues = hba->nr_queues[i]; 2887 if (!map->nr_queues) 2888 continue; 2889 map->queue_offset = queue_offset; 2890 if (i == HCTX_TYPE_POLL && !is_mcq_supported(hba)) 2891 map->queue_offset = 0; 2892 2893 blk_mq_map_queues(map); 2894 queue_offset += map->nr_queues; 2895 } 2896 } 2897 2898 static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) 2899 { 2900 struct utp_transfer_cmd_desc *cmd_descp = (void *)hba->ucdl_base_addr + 2901 i * ufshcd_get_ucd_size(hba); 2902 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr; 2903 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr + 2904 i * ufshcd_get_ucd_size(hba); 2905 u16 response_offset = offsetof(struct utp_transfer_cmd_desc, 2906 response_upiu); 2907 u16 prdt_offset = offsetof(struct utp_transfer_cmd_desc, prd_table); 2908 2909 lrb->utr_descriptor_ptr = utrdlp + i; 2910 lrb->utrd_dma_addr = hba->utrdl_dma_addr + 2911 i * sizeof(struct utp_transfer_req_desc); 2912 lrb->ucd_req_ptr = (struct utp_upiu_req *)cmd_descp->command_upiu; 2913 lrb->ucd_req_dma_addr = cmd_desc_element_addr; 2914 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp->response_upiu; 2915 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset; 2916 lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp->prd_table; 2917 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset; 2918 } 2919 2920 /** 2921 * ufshcd_queuecommand - main entry point for SCSI requests 2922 * @host: SCSI host pointer 2923 * @cmd: command from SCSI Midlayer 2924 * 2925 * Return: 0 for success, non-zero in case of failure. 2926 */ 2927 static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) 2928 { 2929 struct ufs_hba *hba = shost_priv(host); 2930 int tag = scsi_cmd_to_rq(cmd)->tag; 2931 struct ufshcd_lrb *lrbp; 2932 int err = 0; 2933 struct ufs_hw_queue *hwq = NULL; 2934 2935 switch (hba->ufshcd_state) { 2936 case UFSHCD_STATE_OPERATIONAL: 2937 break; 2938 case UFSHCD_STATE_EH_SCHEDULED_NON_FATAL: 2939 /* 2940 * SCSI error handler can call ->queuecommand() while UFS error 2941 * handler is in progress. Error interrupts could change the 2942 * state from UFSHCD_STATE_RESET to 2943 * UFSHCD_STATE_EH_SCHEDULED_NON_FATAL. Prevent requests 2944 * being issued in that case. 2945 */ 2946 if (ufshcd_eh_in_progress(hba)) { 2947 err = SCSI_MLQUEUE_HOST_BUSY; 2948 goto out; 2949 } 2950 break; 2951 case UFSHCD_STATE_EH_SCHEDULED_FATAL: 2952 /* 2953 * pm_runtime_get_sync() is used at error handling preparation 2954 * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's 2955 * PM ops, it can never be finished if we let SCSI layer keep 2956 * retrying it, which gets err handler stuck forever. Neither 2957 * can we let the scsi cmd pass through, because UFS is in bad 2958 * state, the scsi cmd may eventually time out, which will get 2959 * err handler blocked for too long. So, just fail the scsi cmd 2960 * sent from PM ops, err handler can recover PM error anyways. 2961 */ 2962 if (hba->pm_op_in_progress) { 2963 hba->force_reset = true; 2964 set_host_byte(cmd, DID_BAD_TARGET); 2965 scsi_done(cmd); 2966 goto out; 2967 } 2968 fallthrough; 2969 case UFSHCD_STATE_RESET: 2970 err = SCSI_MLQUEUE_HOST_BUSY; 2971 goto out; 2972 case UFSHCD_STATE_ERROR: 2973 set_host_byte(cmd, DID_ERROR); 2974 scsi_done(cmd); 2975 goto out; 2976 } 2977 2978 hba->req_abort_count = 0; 2979 2980 ufshcd_hold(hba); 2981 2982 lrbp = &hba->lrb[tag]; 2983 lrbp->cmd = cmd; 2984 lrbp->task_tag = tag; 2985 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); 2986 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba); 2987 2988 ufshcd_prepare_lrbp_crypto(scsi_cmd_to_rq(cmd), lrbp); 2989 2990 lrbp->req_abort_skip = false; 2991 2992 ufshcd_comp_scsi_upiu(hba, lrbp); 2993 2994 err = ufshcd_map_sg(hba, lrbp); 2995 if (err) { 2996 ufshcd_release(hba); 2997 goto out; 2998 } 2999 3000 if (is_mcq_enabled(hba)) 3001 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(cmd)); 3002 3003 ufshcd_send_command(hba, tag, hwq); 3004 3005 out: 3006 if (ufs_trigger_eh(hba)) { 3007 unsigned long flags; 3008 3009 spin_lock_irqsave(hba->host->host_lock, flags); 3010 ufshcd_schedule_eh_work(hba); 3011 spin_unlock_irqrestore(hba->host->host_lock, flags); 3012 } 3013 3014 return err; 3015 } 3016 3017 static void ufshcd_setup_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, 3018 enum dev_cmd_type cmd_type, u8 lun, int tag) 3019 { 3020 lrbp->cmd = NULL; 3021 lrbp->task_tag = tag; 3022 lrbp->lun = lun; 3023 lrbp->intr_cmd = true; /* No interrupt aggregation */ 3024 ufshcd_prepare_lrbp_crypto(NULL, lrbp); 3025 hba->dev_cmd.type = cmd_type; 3026 } 3027 3028 static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, 3029 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag) 3030 { 3031 ufshcd_setup_dev_cmd(hba, lrbp, cmd_type, 0, tag); 3032 3033 return ufshcd_compose_devman_upiu(hba, lrbp); 3034 } 3035 3036 /* 3037 * Check with the block layer if the command is inflight 3038 * @cmd: command to check. 3039 * 3040 * Return: true if command is inflight; false if not. 3041 */ 3042 bool ufshcd_cmd_inflight(struct scsi_cmnd *cmd) 3043 { 3044 return cmd && blk_mq_rq_state(scsi_cmd_to_rq(cmd)) == MQ_RQ_IN_FLIGHT; 3045 } 3046 3047 /* 3048 * Clear the pending command in the controller and wait until 3049 * the controller confirms that the command has been cleared. 3050 * @hba: per adapter instance 3051 * @task_tag: The tag number of the command to be cleared. 3052 */ 3053 static int ufshcd_clear_cmd(struct ufs_hba *hba, u32 task_tag) 3054 { 3055 u32 mask; 3056 unsigned long flags; 3057 int err; 3058 3059 if (is_mcq_enabled(hba)) { 3060 /* 3061 * MCQ mode. Clean up the MCQ resources similar to 3062 * what the ufshcd_utrl_clear() does for SDB mode. 3063 */ 3064 err = ufshcd_mcq_sq_cleanup(hba, task_tag); 3065 if (err) { 3066 dev_err(hba->dev, "%s: failed tag=%d. err=%d\n", 3067 __func__, task_tag, err); 3068 return err; 3069 } 3070 return 0; 3071 } 3072 3073 mask = 1U << task_tag; 3074 3075 /* clear outstanding transaction before retry */ 3076 spin_lock_irqsave(hba->host->host_lock, flags); 3077 ufshcd_utrl_clear(hba, mask); 3078 spin_unlock_irqrestore(hba->host->host_lock, flags); 3079 3080 /* 3081 * wait for h/w to clear corresponding bit in door-bell. 3082 * max. wait is 1 sec. 3083 */ 3084 return ufshcd_wait_for_register(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL, 3085 mask, ~mask, 1000, 1000); 3086 } 3087 3088 /** 3089 * ufshcd_dev_cmd_completion() - handles device management command responses 3090 * @hba: per adapter instance 3091 * @lrbp: pointer to local reference block 3092 * 3093 * Return: 0 upon success; < 0 upon failure. 3094 */ 3095 static int 3096 ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) 3097 { 3098 enum upiu_response_transaction resp; 3099 int err = 0; 3100 3101 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); 3102 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); 3103 3104 switch (resp) { 3105 case UPIU_TRANSACTION_NOP_IN: 3106 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { 3107 err = -EINVAL; 3108 dev_err(hba->dev, "%s: unexpected response %x\n", 3109 __func__, resp); 3110 } 3111 break; 3112 case UPIU_TRANSACTION_QUERY_RSP: { 3113 u8 response = lrbp->ucd_rsp_ptr->header.response; 3114 3115 if (response == 0) 3116 err = ufshcd_copy_query_response(hba, lrbp); 3117 break; 3118 } 3119 case UPIU_TRANSACTION_REJECT_UPIU: 3120 /* TODO: handle Reject UPIU Response */ 3121 err = -EPERM; 3122 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", 3123 __func__); 3124 break; 3125 case UPIU_TRANSACTION_RESPONSE: 3126 if (hba->dev_cmd.type != DEV_CMD_TYPE_RPMB) { 3127 err = -EINVAL; 3128 dev_err(hba->dev, "%s: unexpected response %x\n", __func__, resp); 3129 } 3130 break; 3131 default: 3132 err = -EINVAL; 3133 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", 3134 __func__, resp); 3135 break; 3136 } 3137 3138 return err; 3139 } 3140 3141 static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, 3142 struct ufshcd_lrb *lrbp, int max_timeout) 3143 { 3144 unsigned long time_left = msecs_to_jiffies(max_timeout); 3145 unsigned long flags; 3146 bool pending; 3147 int err; 3148 3149 retry: 3150 time_left = wait_for_completion_timeout(hba->dev_cmd.complete, 3151 time_left); 3152 3153 if (likely(time_left)) { 3154 /* 3155 * The completion handler called complete() and the caller of 3156 * this function still owns the @lrbp tag so the code below does 3157 * not trigger any race conditions. 3158 */ 3159 hba->dev_cmd.complete = NULL; 3160 err = ufshcd_get_tr_ocs(lrbp, NULL); 3161 if (!err) 3162 err = ufshcd_dev_cmd_completion(hba, lrbp); 3163 } else { 3164 err = -ETIMEDOUT; 3165 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", 3166 __func__, lrbp->task_tag); 3167 3168 /* MCQ mode */ 3169 if (is_mcq_enabled(hba)) { 3170 /* successfully cleared the command, retry if needed */ 3171 if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0) 3172 err = -EAGAIN; 3173 hba->dev_cmd.complete = NULL; 3174 return err; 3175 } 3176 3177 /* SDB mode */ 3178 if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0) { 3179 /* successfully cleared the command, retry if needed */ 3180 err = -EAGAIN; 3181 /* 3182 * Since clearing the command succeeded we also need to 3183 * clear the task tag bit from the outstanding_reqs 3184 * variable. 3185 */ 3186 spin_lock_irqsave(&hba->outstanding_lock, flags); 3187 pending = test_bit(lrbp->task_tag, 3188 &hba->outstanding_reqs); 3189 if (pending) { 3190 hba->dev_cmd.complete = NULL; 3191 __clear_bit(lrbp->task_tag, 3192 &hba->outstanding_reqs); 3193 } 3194 spin_unlock_irqrestore(&hba->outstanding_lock, flags); 3195 3196 if (!pending) { 3197 /* 3198 * The completion handler ran while we tried to 3199 * clear the command. 3200 */ 3201 time_left = 1; 3202 goto retry; 3203 } 3204 } else { 3205 dev_err(hba->dev, "%s: failed to clear tag %d\n", 3206 __func__, lrbp->task_tag); 3207 3208 spin_lock_irqsave(&hba->outstanding_lock, flags); 3209 pending = test_bit(lrbp->task_tag, 3210 &hba->outstanding_reqs); 3211 if (pending) 3212 hba->dev_cmd.complete = NULL; 3213 spin_unlock_irqrestore(&hba->outstanding_lock, flags); 3214 3215 if (!pending) { 3216 /* 3217 * The completion handler ran while we tried to 3218 * clear the command. 3219 */ 3220 time_left = 1; 3221 goto retry; 3222 } 3223 } 3224 } 3225 3226 return err; 3227 } 3228 3229 static void ufshcd_dev_man_lock(struct ufs_hba *hba) 3230 { 3231 ufshcd_hold(hba); 3232 mutex_lock(&hba->dev_cmd.lock); 3233 down_read(&hba->clk_scaling_lock); 3234 } 3235 3236 static void ufshcd_dev_man_unlock(struct ufs_hba *hba) 3237 { 3238 up_read(&hba->clk_scaling_lock); 3239 mutex_unlock(&hba->dev_cmd.lock); 3240 ufshcd_release(hba); 3241 } 3242 3243 static int ufshcd_issue_dev_cmd(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, 3244 const u32 tag, int timeout) 3245 { 3246 DECLARE_COMPLETION_ONSTACK(wait); 3247 int err; 3248 3249 hba->dev_cmd.complete = &wait; 3250 3251 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr); 3252 3253 ufshcd_send_command(hba, tag, hba->dev_cmd_queue); 3254 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); 3255 3256 ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP, 3257 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr); 3258 3259 return err; 3260 } 3261 3262 /** 3263 * ufshcd_exec_dev_cmd - API for sending device management requests 3264 * @hba: UFS hba 3265 * @cmd_type: specifies the type (NOP, Query...) 3266 * @timeout: timeout in milliseconds 3267 * 3268 * Return: 0 upon success; < 0 upon failure. 3269 * 3270 * NOTE: Since there is only one available tag for device management commands, 3271 * it is expected you hold the hba->dev_cmd.lock mutex. 3272 */ 3273 static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, 3274 enum dev_cmd_type cmd_type, int timeout) 3275 { 3276 const u32 tag = hba->reserved_slot; 3277 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; 3278 int err; 3279 3280 /* Protects use of hba->reserved_slot. */ 3281 lockdep_assert_held(&hba->dev_cmd.lock); 3282 3283 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); 3284 if (unlikely(err)) 3285 return err; 3286 3287 return ufshcd_issue_dev_cmd(hba, lrbp, tag, timeout); 3288 } 3289 3290 /** 3291 * ufshcd_init_query() - init the query response and request parameters 3292 * @hba: per-adapter instance 3293 * @request: address of the request pointer to be initialized 3294 * @response: address of the response pointer to be initialized 3295 * @opcode: operation to perform 3296 * @idn: flag idn to access 3297 * @index: LU number to access 3298 * @selector: query/flag/descriptor further identification 3299 */ 3300 static inline void ufshcd_init_query(struct ufs_hba *hba, 3301 struct ufs_query_req **request, struct ufs_query_res **response, 3302 enum query_opcode opcode, u8 idn, u8 index, u8 selector) 3303 { 3304 *request = &hba->dev_cmd.query.request; 3305 *response = &hba->dev_cmd.query.response; 3306 memset(*request, 0, sizeof(struct ufs_query_req)); 3307 memset(*response, 0, sizeof(struct ufs_query_res)); 3308 (*request)->upiu_req.opcode = opcode; 3309 (*request)->upiu_req.idn = idn; 3310 (*request)->upiu_req.index = index; 3311 (*request)->upiu_req.selector = selector; 3312 } 3313 3314 static int ufshcd_query_flag_retry(struct ufs_hba *hba, 3315 enum query_opcode opcode, enum flag_idn idn, u8 index, bool *flag_res) 3316 { 3317 int ret; 3318 int retries; 3319 3320 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) { 3321 ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res); 3322 if (ret) 3323 dev_dbg(hba->dev, 3324 "%s: failed with error %d, retries %d\n", 3325 __func__, ret, retries); 3326 else 3327 break; 3328 } 3329 3330 if (ret) 3331 dev_err(hba->dev, 3332 "%s: query flag, opcode %d, idn %d, failed with error %d after %d retries\n", 3333 __func__, opcode, idn, ret, retries); 3334 return ret; 3335 } 3336 3337 /** 3338 * ufshcd_query_flag() - API function for sending flag query requests 3339 * @hba: per-adapter instance 3340 * @opcode: flag query to perform 3341 * @idn: flag idn to access 3342 * @index: flag index to access 3343 * @flag_res: the flag value after the query request completes 3344 * 3345 * Return: 0 for success, non-zero in case of failure. 3346 */ 3347 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, 3348 enum flag_idn idn, u8 index, bool *flag_res) 3349 { 3350 struct ufs_query_req *request = NULL; 3351 struct ufs_query_res *response = NULL; 3352 int err, selector = 0; 3353 int timeout = QUERY_REQ_TIMEOUT; 3354 3355 BUG_ON(!hba); 3356 3357 ufshcd_dev_man_lock(hba); 3358 3359 ufshcd_init_query(hba, &request, &response, opcode, idn, index, 3360 selector); 3361 3362 switch (opcode) { 3363 case UPIU_QUERY_OPCODE_SET_FLAG: 3364 case UPIU_QUERY_OPCODE_CLEAR_FLAG: 3365 case UPIU_QUERY_OPCODE_TOGGLE_FLAG: 3366 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; 3367 break; 3368 case UPIU_QUERY_OPCODE_READ_FLAG: 3369 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; 3370 if (!flag_res) { 3371 /* No dummy reads */ 3372 dev_err(hba->dev, "%s: Invalid argument for read request\n", 3373 __func__); 3374 err = -EINVAL; 3375 goto out_unlock; 3376 } 3377 break; 3378 default: 3379 dev_err(hba->dev, 3380 "%s: Expected query flag opcode but got = %d\n", 3381 __func__, opcode); 3382 err = -EINVAL; 3383 goto out_unlock; 3384 } 3385 3386 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); 3387 3388 if (err) { 3389 dev_err(hba->dev, 3390 "%s: Sending flag query for idn %d failed, err = %d\n", 3391 __func__, idn, err); 3392 goto out_unlock; 3393 } 3394 3395 if (flag_res) 3396 *flag_res = (be32_to_cpu(response->upiu_res.value) & 3397 MASK_QUERY_UPIU_FLAG_LOC) & 0x1; 3398 3399 out_unlock: 3400 ufshcd_dev_man_unlock(hba); 3401 return err; 3402 } 3403 3404 /** 3405 * ufshcd_query_attr - API function for sending attribute requests 3406 * @hba: per-adapter instance 3407 * @opcode: attribute opcode 3408 * @idn: attribute idn to access 3409 * @index: index field 3410 * @selector: selector field 3411 * @attr_val: the attribute value after the query request completes 3412 * 3413 * Return: 0 for success, non-zero in case of failure. 3414 */ 3415 int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, 3416 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) 3417 { 3418 struct ufs_query_req *request = NULL; 3419 struct ufs_query_res *response = NULL; 3420 int err; 3421 3422 BUG_ON(!hba); 3423 3424 if (!attr_val) { 3425 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", 3426 __func__, opcode); 3427 return -EINVAL; 3428 } 3429 3430 ufshcd_dev_man_lock(hba); 3431 3432 ufshcd_init_query(hba, &request, &response, opcode, idn, index, 3433 selector); 3434 3435 switch (opcode) { 3436 case UPIU_QUERY_OPCODE_WRITE_ATTR: 3437 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; 3438 request->upiu_req.value = cpu_to_be32(*attr_val); 3439 break; 3440 case UPIU_QUERY_OPCODE_READ_ATTR: 3441 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; 3442 break; 3443 default: 3444 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", 3445 __func__, opcode); 3446 err = -EINVAL; 3447 goto out_unlock; 3448 } 3449 3450 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); 3451 3452 if (err) { 3453 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", 3454 __func__, opcode, idn, index, err); 3455 goto out_unlock; 3456 } 3457 3458 *attr_val = be32_to_cpu(response->upiu_res.value); 3459 3460 out_unlock: 3461 ufshcd_dev_man_unlock(hba); 3462 return err; 3463 } 3464 3465 /** 3466 * ufshcd_query_attr_retry() - API function for sending query 3467 * attribute with retries 3468 * @hba: per-adapter instance 3469 * @opcode: attribute opcode 3470 * @idn: attribute idn to access 3471 * @index: index field 3472 * @selector: selector field 3473 * @attr_val: the attribute value after the query request 3474 * completes 3475 * 3476 * Return: 0 for success, non-zero in case of failure. 3477 */ 3478 int ufshcd_query_attr_retry(struct ufs_hba *hba, 3479 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector, 3480 u32 *attr_val) 3481 { 3482 int ret = 0; 3483 u32 retries; 3484 3485 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { 3486 ret = ufshcd_query_attr(hba, opcode, idn, index, 3487 selector, attr_val); 3488 if (ret) 3489 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", 3490 __func__, ret, retries); 3491 else 3492 break; 3493 } 3494 3495 if (ret) 3496 dev_err(hba->dev, 3497 "%s: query attribute, idn %d, failed with error %d after %d retries\n", 3498 __func__, idn, ret, QUERY_REQ_RETRIES); 3499 return ret; 3500 } 3501 3502 static int __ufshcd_query_descriptor(struct ufs_hba *hba, 3503 enum query_opcode opcode, enum desc_idn idn, u8 index, 3504 u8 selector, u8 *desc_buf, int *buf_len) 3505 { 3506 struct ufs_query_req *request = NULL; 3507 struct ufs_query_res *response = NULL; 3508 int err; 3509 3510 BUG_ON(!hba); 3511 3512 if (!desc_buf) { 3513 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", 3514 __func__, opcode); 3515 return -EINVAL; 3516 } 3517 3518 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) { 3519 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", 3520 __func__, *buf_len); 3521 return -EINVAL; 3522 } 3523 3524 ufshcd_dev_man_lock(hba); 3525 3526 ufshcd_init_query(hba, &request, &response, opcode, idn, index, 3527 selector); 3528 hba->dev_cmd.query.descriptor = desc_buf; 3529 request->upiu_req.length = cpu_to_be16(*buf_len); 3530 3531 switch (opcode) { 3532 case UPIU_QUERY_OPCODE_WRITE_DESC: 3533 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; 3534 break; 3535 case UPIU_QUERY_OPCODE_READ_DESC: 3536 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; 3537 break; 3538 default: 3539 dev_err(hba->dev, 3540 "%s: Expected query descriptor opcode but got = 0x%.2x\n", 3541 __func__, opcode); 3542 err = -EINVAL; 3543 goto out_unlock; 3544 } 3545 3546 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); 3547 3548 if (err) { 3549 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", 3550 __func__, opcode, idn, index, err); 3551 goto out_unlock; 3552 } 3553 3554 *buf_len = be16_to_cpu(response->upiu_res.length); 3555 3556 out_unlock: 3557 hba->dev_cmd.query.descriptor = NULL; 3558 ufshcd_dev_man_unlock(hba); 3559 return err; 3560 } 3561 3562 /** 3563 * ufshcd_query_descriptor_retry - API function for sending descriptor requests 3564 * @hba: per-adapter instance 3565 * @opcode: attribute opcode 3566 * @idn: attribute idn to access 3567 * @index: index field 3568 * @selector: selector field 3569 * @desc_buf: the buffer that contains the descriptor 3570 * @buf_len: length parameter passed to the device 3571 * 3572 * The buf_len parameter will contain, on return, the length parameter 3573 * received on the response. 3574 * 3575 * Return: 0 for success, non-zero in case of failure. 3576 */ 3577 int ufshcd_query_descriptor_retry(struct ufs_hba *hba, 3578 enum query_opcode opcode, 3579 enum desc_idn idn, u8 index, 3580 u8 selector, 3581 u8 *desc_buf, int *buf_len) 3582 { 3583 int err; 3584 int retries; 3585 3586 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { 3587 err = __ufshcd_query_descriptor(hba, opcode, idn, index, 3588 selector, desc_buf, buf_len); 3589 if (!err || err == -EINVAL) 3590 break; 3591 } 3592 3593 return err; 3594 } 3595 3596 /** 3597 * ufshcd_read_desc_param - read the specified descriptor parameter 3598 * @hba: Pointer to adapter instance 3599 * @desc_id: descriptor idn value 3600 * @desc_index: descriptor index 3601 * @param_offset: offset of the parameter to read 3602 * @param_read_buf: pointer to buffer where parameter would be read 3603 * @param_size: sizeof(param_read_buf) 3604 * 3605 * Return: 0 in case of success, non-zero otherwise. 3606 */ 3607 int ufshcd_read_desc_param(struct ufs_hba *hba, 3608 enum desc_idn desc_id, 3609 int desc_index, 3610 u8 param_offset, 3611 u8 *param_read_buf, 3612 u8 param_size) 3613 { 3614 int ret; 3615 u8 *desc_buf; 3616 int buff_len = QUERY_DESC_MAX_SIZE; 3617 bool is_kmalloc = true; 3618 3619 /* Safety check */ 3620 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size) 3621 return -EINVAL; 3622 3623 /* Check whether we need temp memory */ 3624 if (param_offset != 0 || param_size < buff_len) { 3625 desc_buf = kzalloc(buff_len, GFP_KERNEL); 3626 if (!desc_buf) 3627 return -ENOMEM; 3628 } else { 3629 desc_buf = param_read_buf; 3630 is_kmalloc = false; 3631 } 3632 3633 /* Request for full descriptor */ 3634 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, 3635 desc_id, desc_index, 0, 3636 desc_buf, &buff_len); 3637 if (ret) { 3638 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d\n", 3639 __func__, desc_id, desc_index, param_offset, ret); 3640 goto out; 3641 } 3642 3643 /* Update descriptor length */ 3644 buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET]; 3645 3646 if (param_offset >= buff_len) { 3647 dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n", 3648 __func__, param_offset, desc_id, buff_len); 3649 ret = -EINVAL; 3650 goto out; 3651 } 3652 3653 /* Sanity check */ 3654 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) { 3655 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n", 3656 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]); 3657 ret = -EINVAL; 3658 goto out; 3659 } 3660 3661 if (is_kmalloc) { 3662 /* Make sure we don't copy more data than available */ 3663 if (param_offset >= buff_len) 3664 ret = -EINVAL; 3665 else 3666 memcpy(param_read_buf, &desc_buf[param_offset], 3667 min_t(u32, param_size, buff_len - param_offset)); 3668 } 3669 out: 3670 if (is_kmalloc) 3671 kfree(desc_buf); 3672 return ret; 3673 } 3674 3675 /** 3676 * struct uc_string_id - unicode string 3677 * 3678 * @len: size of this descriptor inclusive 3679 * @type: descriptor type 3680 * @uc: unicode string character 3681 */ 3682 struct uc_string_id { 3683 u8 len; 3684 u8 type; 3685 wchar_t uc[]; 3686 } __packed; 3687 3688 /* replace non-printable or non-ASCII characters with spaces */ 3689 static inline char ufshcd_remove_non_printable(u8 ch) 3690 { 3691 return (ch >= 0x20 && ch <= 0x7e) ? ch : ' '; 3692 } 3693 3694 /** 3695 * ufshcd_read_string_desc - read string descriptor 3696 * @hba: pointer to adapter instance 3697 * @desc_index: descriptor index 3698 * @buf: pointer to buffer where descriptor would be read, 3699 * the caller should free the memory. 3700 * @ascii: if true convert from unicode to ascii characters 3701 * null terminated string. 3702 * 3703 * Return: 3704 * * string size on success. 3705 * * -ENOMEM: on allocation failure 3706 * * -EINVAL: on a wrong parameter 3707 */ 3708 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, 3709 u8 **buf, bool ascii) 3710 { 3711 struct uc_string_id *uc_str; 3712 u8 *str; 3713 int ret; 3714 3715 if (!buf) 3716 return -EINVAL; 3717 3718 uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); 3719 if (!uc_str) 3720 return -ENOMEM; 3721 3722 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0, 3723 (u8 *)uc_str, QUERY_DESC_MAX_SIZE); 3724 if (ret < 0) { 3725 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n", 3726 QUERY_REQ_RETRIES, ret); 3727 str = NULL; 3728 goto out; 3729 } 3730 3731 if (uc_str->len <= QUERY_DESC_HDR_SIZE) { 3732 dev_dbg(hba->dev, "String Desc is of zero length\n"); 3733 str = NULL; 3734 ret = 0; 3735 goto out; 3736 } 3737 3738 if (ascii) { 3739 ssize_t ascii_len; 3740 int i; 3741 /* remove header and divide by 2 to move from UTF16 to UTF8 */ 3742 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1; 3743 str = kzalloc(ascii_len, GFP_KERNEL); 3744 if (!str) { 3745 ret = -ENOMEM; 3746 goto out; 3747 } 3748 3749 /* 3750 * the descriptor contains string in UTF16 format 3751 * we need to convert to utf-8 so it can be displayed 3752 */ 3753 ret = utf16s_to_utf8s(uc_str->uc, 3754 uc_str->len - QUERY_DESC_HDR_SIZE, 3755 UTF16_BIG_ENDIAN, str, ascii_len - 1); 3756 3757 /* replace non-printable or non-ASCII characters with spaces */ 3758 for (i = 0; i < ret; i++) 3759 str[i] = ufshcd_remove_non_printable(str[i]); 3760 3761 str[ret++] = '\0'; 3762 3763 } else { 3764 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL); 3765 if (!str) { 3766 ret = -ENOMEM; 3767 goto out; 3768 } 3769 ret = uc_str->len; 3770 } 3771 out: 3772 *buf = str; 3773 kfree(uc_str); 3774 return ret; 3775 } 3776 3777 /** 3778 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter 3779 * @hba: Pointer to adapter instance 3780 * @lun: lun id 3781 * @param_offset: offset of the parameter to read 3782 * @param_read_buf: pointer to buffer where parameter would be read 3783 * @param_size: sizeof(param_read_buf) 3784 * 3785 * Return: 0 in case of success, non-zero otherwise. 3786 */ 3787 static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, 3788 int lun, 3789 enum unit_desc_param param_offset, 3790 u8 *param_read_buf, 3791 u32 param_size) 3792 { 3793 /* 3794 * Unit descriptors are only available for general purpose LUs (LUN id 3795 * from 0 to 7) and RPMB Well known LU. 3796 */ 3797 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun)) 3798 return -EOPNOTSUPP; 3799 3800 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, 3801 param_offset, param_read_buf, param_size); 3802 } 3803 3804 static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba) 3805 { 3806 int err = 0; 3807 u32 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US; 3808 3809 if (hba->dev_info.wspecversion >= 0x300) { 3810 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 3811 QUERY_ATTR_IDN_REF_CLK_GATING_WAIT_TIME, 0, 0, 3812 &gating_wait); 3813 if (err) 3814 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n", 3815 err, gating_wait); 3816 3817 if (gating_wait == 0) { 3818 gating_wait = UFSHCD_REF_CLK_GATING_WAIT_US; 3819 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n", 3820 gating_wait); 3821 } 3822 3823 hba->dev_info.clk_gating_wait_us = gating_wait; 3824 } 3825 3826 return err; 3827 } 3828 3829 /** 3830 * ufshcd_memory_alloc - allocate memory for host memory space data structures 3831 * @hba: per adapter instance 3832 * 3833 * 1. Allocate DMA memory for Command Descriptor array 3834 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT 3835 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). 3836 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List 3837 * (UTMRDL) 3838 * 4. Allocate memory for local reference block(lrb). 3839 * 3840 * Return: 0 for success, non-zero in case of failure. 3841 */ 3842 static int ufshcd_memory_alloc(struct ufs_hba *hba) 3843 { 3844 size_t utmrdl_size, utrdl_size, ucdl_size; 3845 3846 /* Allocate memory for UTP command descriptors */ 3847 ucdl_size = ufshcd_get_ucd_size(hba) * hba->nutrs; 3848 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, 3849 ucdl_size, 3850 &hba->ucdl_dma_addr, 3851 GFP_KERNEL); 3852 3853 /* 3854 * UFSHCI requires UTP command descriptor to be 128 byte aligned. 3855 */ 3856 if (!hba->ucdl_base_addr || 3857 WARN_ON(hba->ucdl_dma_addr & (128 - 1))) { 3858 dev_err(hba->dev, 3859 "Command Descriptor Memory allocation failed\n"); 3860 goto out; 3861 } 3862 3863 /* 3864 * Allocate memory for UTP Transfer descriptors 3865 * UFSHCI requires 1KB alignment of UTRD 3866 */ 3867 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); 3868 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, 3869 utrdl_size, 3870 &hba->utrdl_dma_addr, 3871 GFP_KERNEL); 3872 if (!hba->utrdl_base_addr || 3873 WARN_ON(hba->utrdl_dma_addr & (SZ_1K - 1))) { 3874 dev_err(hba->dev, 3875 "Transfer Descriptor Memory allocation failed\n"); 3876 goto out; 3877 } 3878 3879 /* 3880 * Skip utmrdl allocation; it may have been 3881 * allocated during first pass and not released during 3882 * MCQ memory allocation. 3883 * See ufshcd_release_sdb_queue() and ufshcd_config_mcq() 3884 */ 3885 if (hba->utmrdl_base_addr) 3886 goto skip_utmrdl; 3887 /* 3888 * Allocate memory for UTP Task Management descriptors 3889 * UFSHCI requires 1KB alignment of UTMRD 3890 */ 3891 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; 3892 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, 3893 utmrdl_size, 3894 &hba->utmrdl_dma_addr, 3895 GFP_KERNEL); 3896 if (!hba->utmrdl_base_addr || 3897 WARN_ON(hba->utmrdl_dma_addr & (SZ_1K - 1))) { 3898 dev_err(hba->dev, 3899 "Task Management Descriptor Memory allocation failed\n"); 3900 goto out; 3901 } 3902 3903 skip_utmrdl: 3904 /* Allocate memory for local reference block */ 3905 hba->lrb = devm_kcalloc(hba->dev, 3906 hba->nutrs, sizeof(struct ufshcd_lrb), 3907 GFP_KERNEL); 3908 if (!hba->lrb) { 3909 dev_err(hba->dev, "LRB Memory allocation failed\n"); 3910 goto out; 3911 } 3912 return 0; 3913 out: 3914 return -ENOMEM; 3915 } 3916 3917 /** 3918 * ufshcd_host_memory_configure - configure local reference block with 3919 * memory offsets 3920 * @hba: per adapter instance 3921 * 3922 * Configure Host memory space 3923 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA 3924 * address. 3925 * 2. Update each UTRD with Response UPIU offset, Response UPIU length 3926 * and PRDT offset. 3927 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT 3928 * into local reference block. 3929 */ 3930 static void ufshcd_host_memory_configure(struct ufs_hba *hba) 3931 { 3932 struct utp_transfer_req_desc *utrdlp; 3933 dma_addr_t cmd_desc_dma_addr; 3934 dma_addr_t cmd_desc_element_addr; 3935 u16 response_offset; 3936 u16 prdt_offset; 3937 int cmd_desc_size; 3938 int i; 3939 3940 utrdlp = hba->utrdl_base_addr; 3941 3942 response_offset = 3943 offsetof(struct utp_transfer_cmd_desc, response_upiu); 3944 prdt_offset = 3945 offsetof(struct utp_transfer_cmd_desc, prd_table); 3946 3947 cmd_desc_size = ufshcd_get_ucd_size(hba); 3948 cmd_desc_dma_addr = hba->ucdl_dma_addr; 3949 3950 for (i = 0; i < hba->nutrs; i++) { 3951 /* Configure UTRD with command descriptor base address */ 3952 cmd_desc_element_addr = 3953 (cmd_desc_dma_addr + (cmd_desc_size * i)); 3954 utrdlp[i].command_desc_base_addr = 3955 cpu_to_le64(cmd_desc_element_addr); 3956 3957 /* Response upiu and prdt offset should be in double words */ 3958 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { 3959 utrdlp[i].response_upiu_offset = 3960 cpu_to_le16(response_offset); 3961 utrdlp[i].prd_table_offset = 3962 cpu_to_le16(prdt_offset); 3963 utrdlp[i].response_upiu_length = 3964 cpu_to_le16(ALIGNED_UPIU_SIZE); 3965 } else { 3966 utrdlp[i].response_upiu_offset = 3967 cpu_to_le16(response_offset >> 2); 3968 utrdlp[i].prd_table_offset = 3969 cpu_to_le16(prdt_offset >> 2); 3970 utrdlp[i].response_upiu_length = 3971 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); 3972 } 3973 3974 ufshcd_init_lrb(hba, &hba->lrb[i], i); 3975 } 3976 } 3977 3978 /** 3979 * ufshcd_dme_link_startup - Notify Unipro to perform link startup 3980 * @hba: per adapter instance 3981 * 3982 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, 3983 * in order to initialize the Unipro link startup procedure. 3984 * Once the Unipro links are up, the device connected to the controller 3985 * is detected. 3986 * 3987 * Return: 0 on success, non-zero value on failure. 3988 */ 3989 static int ufshcd_dme_link_startup(struct ufs_hba *hba) 3990 { 3991 struct uic_command uic_cmd = {0}; 3992 int ret; 3993 3994 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; 3995 3996 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); 3997 if (ret) 3998 dev_dbg(hba->dev, 3999 "dme-link-startup: error code %d\n", ret); 4000 return ret; 4001 } 4002 /** 4003 * ufshcd_dme_reset - UIC command for DME_RESET 4004 * @hba: per adapter instance 4005 * 4006 * DME_RESET command is issued in order to reset UniPro stack. 4007 * This function now deals with cold reset. 4008 * 4009 * Return: 0 on success, non-zero value on failure. 4010 */ 4011 static int ufshcd_dme_reset(struct ufs_hba *hba) 4012 { 4013 struct uic_command uic_cmd = {0}; 4014 int ret; 4015 4016 uic_cmd.command = UIC_CMD_DME_RESET; 4017 4018 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); 4019 if (ret) 4020 dev_err(hba->dev, 4021 "dme-reset: error code %d\n", ret); 4022 4023 return ret; 4024 } 4025 4026 int ufshcd_dme_configure_adapt(struct ufs_hba *hba, 4027 int agreed_gear, 4028 int adapt_val) 4029 { 4030 int ret; 4031 4032 if (agreed_gear < UFS_HS_G4) 4033 adapt_val = PA_NO_ADAPT; 4034 4035 ret = ufshcd_dme_set(hba, 4036 UIC_ARG_MIB(PA_TXHSADAPTTYPE), 4037 adapt_val); 4038 return ret; 4039 } 4040 EXPORT_SYMBOL_GPL(ufshcd_dme_configure_adapt); 4041 4042 /** 4043 * ufshcd_dme_enable - UIC command for DME_ENABLE 4044 * @hba: per adapter instance 4045 * 4046 * DME_ENABLE command is issued in order to enable UniPro stack. 4047 * 4048 * Return: 0 on success, non-zero value on failure. 4049 */ 4050 static int ufshcd_dme_enable(struct ufs_hba *hba) 4051 { 4052 struct uic_command uic_cmd = {0}; 4053 int ret; 4054 4055 uic_cmd.command = UIC_CMD_DME_ENABLE; 4056 4057 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); 4058 if (ret) 4059 dev_err(hba->dev, 4060 "dme-enable: error code %d\n", ret); 4061 4062 return ret; 4063 } 4064 4065 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) 4066 { 4067 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000 4068 unsigned long min_sleep_time_us; 4069 4070 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) 4071 return; 4072 4073 /* 4074 * last_dme_cmd_tstamp will be 0 only for 1st call to 4075 * this function 4076 */ 4077 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { 4078 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US; 4079 } else { 4080 unsigned long delta = 4081 (unsigned long) ktime_to_us( 4082 ktime_sub(ktime_get(), 4083 hba->last_dme_cmd_tstamp)); 4084 4085 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US) 4086 min_sleep_time_us = 4087 MIN_DELAY_BEFORE_DME_CMDS_US - delta; 4088 else 4089 return; /* no more delay required */ 4090 } 4091 4092 /* allow sleep for extra 50us if needed */ 4093 usleep_range(min_sleep_time_us, min_sleep_time_us + 50); 4094 } 4095 4096 /** 4097 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET 4098 * @hba: per adapter instance 4099 * @attr_sel: uic command argument1 4100 * @attr_set: attribute set type as uic command argument2 4101 * @mib_val: setting value as uic command argument3 4102 * @peer: indicate whether peer or local 4103 * 4104 * Return: 0 on success, non-zero value on failure. 4105 */ 4106 int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, 4107 u8 attr_set, u32 mib_val, u8 peer) 4108 { 4109 struct uic_command uic_cmd = {0}; 4110 static const char *const action[] = { 4111 "dme-set", 4112 "dme-peer-set" 4113 }; 4114 const char *set = action[!!peer]; 4115 int ret; 4116 int retries = UFS_UIC_COMMAND_RETRIES; 4117 4118 uic_cmd.command = peer ? 4119 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET; 4120 uic_cmd.argument1 = attr_sel; 4121 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set); 4122 uic_cmd.argument3 = mib_val; 4123 4124 do { 4125 /* for peer attributes we retry upon failure */ 4126 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); 4127 if (ret) 4128 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", 4129 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret); 4130 } while (ret && peer && --retries); 4131 4132 if (ret) 4133 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n", 4134 set, UIC_GET_ATTR_ID(attr_sel), mib_val, 4135 UFS_UIC_COMMAND_RETRIES - retries); 4136 4137 return ret; 4138 } 4139 EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr); 4140 4141 /** 4142 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET 4143 * @hba: per adapter instance 4144 * @attr_sel: uic command argument1 4145 * @mib_val: the value of the attribute as returned by the UIC command 4146 * @peer: indicate whether peer or local 4147 * 4148 * Return: 0 on success, non-zero value on failure. 4149 */ 4150 int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, 4151 u32 *mib_val, u8 peer) 4152 { 4153 struct uic_command uic_cmd = {0}; 4154 static const char *const action[] = { 4155 "dme-get", 4156 "dme-peer-get" 4157 }; 4158 const char *get = action[!!peer]; 4159 int ret; 4160 int retries = UFS_UIC_COMMAND_RETRIES; 4161 struct ufs_pa_layer_attr orig_pwr_info; 4162 struct ufs_pa_layer_attr temp_pwr_info; 4163 bool pwr_mode_change = false; 4164 4165 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { 4166 orig_pwr_info = hba->pwr_info; 4167 temp_pwr_info = orig_pwr_info; 4168 4169 if (orig_pwr_info.pwr_tx == FAST_MODE || 4170 orig_pwr_info.pwr_rx == FAST_MODE) { 4171 temp_pwr_info.pwr_tx = FASTAUTO_MODE; 4172 temp_pwr_info.pwr_rx = FASTAUTO_MODE; 4173 pwr_mode_change = true; 4174 } else if (orig_pwr_info.pwr_tx == SLOW_MODE || 4175 orig_pwr_info.pwr_rx == SLOW_MODE) { 4176 temp_pwr_info.pwr_tx = SLOWAUTO_MODE; 4177 temp_pwr_info.pwr_rx = SLOWAUTO_MODE; 4178 pwr_mode_change = true; 4179 } 4180 if (pwr_mode_change) { 4181 ret = ufshcd_change_power_mode(hba, &temp_pwr_info); 4182 if (ret) 4183 goto out; 4184 } 4185 } 4186 4187 uic_cmd.command = peer ? 4188 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET; 4189 uic_cmd.argument1 = attr_sel; 4190 4191 do { 4192 /* for peer attributes we retry upon failure */ 4193 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); 4194 if (ret) 4195 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", 4196 get, UIC_GET_ATTR_ID(attr_sel), ret); 4197 } while (ret && peer && --retries); 4198 4199 if (ret) 4200 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n", 4201 get, UIC_GET_ATTR_ID(attr_sel), 4202 UFS_UIC_COMMAND_RETRIES - retries); 4203 4204 if (mib_val && !ret) 4205 *mib_val = uic_cmd.argument3; 4206 4207 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) 4208 && pwr_mode_change) 4209 ufshcd_change_power_mode(hba, &orig_pwr_info); 4210 out: 4211 return ret; 4212 } 4213 EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); 4214 4215 /** 4216 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power 4217 * state) and waits for it to take effect. 4218 * 4219 * @hba: per adapter instance 4220 * @cmd: UIC command to execute 4221 * 4222 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER & 4223 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host 4224 * and device UniPro link and hence it's final completion would be indicated by 4225 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in 4226 * addition to normal UIC command completion Status (UCCS). This function only 4227 * returns after the relevant status bits indicate the completion. 4228 * 4229 * Return: 0 on success, non-zero value on failure. 4230 */ 4231 static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) 4232 { 4233 DECLARE_COMPLETION_ONSTACK(uic_async_done); 4234 unsigned long flags; 4235 u8 status; 4236 int ret; 4237 bool reenable_intr = false; 4238 4239 mutex_lock(&hba->uic_cmd_mutex); 4240 ufshcd_add_delay_before_dme_cmd(hba); 4241 4242 spin_lock_irqsave(hba->host->host_lock, flags); 4243 if (ufshcd_is_link_broken(hba)) { 4244 ret = -ENOLINK; 4245 goto out_unlock; 4246 } 4247 hba->uic_async_done = &uic_async_done; 4248 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { 4249 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); 4250 /* 4251 * Make sure UIC command completion interrupt is disabled before 4252 * issuing UIC command. 4253 */ 4254 ufshcd_readl(hba, REG_INTERRUPT_ENABLE); 4255 reenable_intr = true; 4256 } 4257 spin_unlock_irqrestore(hba->host->host_lock, flags); 4258 ret = __ufshcd_send_uic_cmd(hba, cmd, false); 4259 if (ret) { 4260 dev_err(hba->dev, 4261 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n", 4262 cmd->command, cmd->argument3, ret); 4263 goto out; 4264 } 4265 4266 if (!wait_for_completion_timeout(hba->uic_async_done, 4267 msecs_to_jiffies(UIC_CMD_TIMEOUT))) { 4268 dev_err(hba->dev, 4269 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n", 4270 cmd->command, cmd->argument3); 4271 4272 if (!cmd->cmd_active) { 4273 dev_err(hba->dev, "%s: Power Mode Change operation has been completed, go check UPMCRS\n", 4274 __func__); 4275 goto check_upmcrs; 4276 } 4277 4278 ret = -ETIMEDOUT; 4279 goto out; 4280 } 4281 4282 check_upmcrs: 4283 status = ufshcd_get_upmcrs(hba); 4284 if (status != PWR_LOCAL) { 4285 dev_err(hba->dev, 4286 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n", 4287 cmd->command, status); 4288 ret = (status != PWR_OK) ? status : -1; 4289 } 4290 out: 4291 if (ret) { 4292 ufshcd_print_host_state(hba); 4293 ufshcd_print_pwr_info(hba); 4294 ufshcd_print_evt_hist(hba); 4295 } 4296 4297 spin_lock_irqsave(hba->host->host_lock, flags); 4298 hba->active_uic_cmd = NULL; 4299 hba->uic_async_done = NULL; 4300 if (reenable_intr) 4301 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); 4302 if (ret) { 4303 ufshcd_set_link_broken(hba); 4304 ufshcd_schedule_eh_work(hba); 4305 } 4306 out_unlock: 4307 spin_unlock_irqrestore(hba->host->host_lock, flags); 4308 mutex_unlock(&hba->uic_cmd_mutex); 4309 4310 return ret; 4311 } 4312 4313 /** 4314 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage 4315 * using DME_SET primitives. 4316 * @hba: per adapter instance 4317 * @mode: powr mode value 4318 * 4319 * Return: 0 on success, non-zero value on failure. 4320 */ 4321 int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode) 4322 { 4323 struct uic_command uic_cmd = {0}; 4324 int ret; 4325 4326 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { 4327 ret = ufshcd_dme_set(hba, 4328 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1); 4329 if (ret) { 4330 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", 4331 __func__, ret); 4332 goto out; 4333 } 4334 } 4335 4336 uic_cmd.command = UIC_CMD_DME_SET; 4337 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE); 4338 uic_cmd.argument3 = mode; 4339 ufshcd_hold(hba); 4340 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); 4341 ufshcd_release(hba); 4342 4343 out: 4344 return ret; 4345 } 4346 EXPORT_SYMBOL_GPL(ufshcd_uic_change_pwr_mode); 4347 4348 int ufshcd_link_recovery(struct ufs_hba *hba) 4349 { 4350 int ret; 4351 unsigned long flags; 4352 4353 spin_lock_irqsave(hba->host->host_lock, flags); 4354 hba->ufshcd_state = UFSHCD_STATE_RESET; 4355 ufshcd_set_eh_in_progress(hba); 4356 spin_unlock_irqrestore(hba->host->host_lock, flags); 4357 4358 /* Reset the attached device */ 4359 ufshcd_device_reset(hba); 4360 4361 ret = ufshcd_host_reset_and_restore(hba); 4362 4363 spin_lock_irqsave(hba->host->host_lock, flags); 4364 if (ret) 4365 hba->ufshcd_state = UFSHCD_STATE_ERROR; 4366 ufshcd_clear_eh_in_progress(hba); 4367 spin_unlock_irqrestore(hba->host->host_lock, flags); 4368 4369 if (ret) 4370 dev_err(hba->dev, "%s: link recovery failed, err %d", 4371 __func__, ret); 4372 4373 return ret; 4374 } 4375 EXPORT_SYMBOL_GPL(ufshcd_link_recovery); 4376 4377 int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) 4378 { 4379 int ret; 4380 struct uic_command uic_cmd = {0}; 4381 ktime_t start = ktime_get(); 4382 4383 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE); 4384 4385 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER; 4386 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); 4387 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", 4388 ktime_to_us(ktime_sub(ktime_get(), start)), ret); 4389 4390 if (ret) 4391 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", 4392 __func__, ret); 4393 else 4394 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, 4395 POST_CHANGE); 4396 4397 return ret; 4398 } 4399 EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_enter); 4400 4401 int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) 4402 { 4403 struct uic_command uic_cmd = {0}; 4404 int ret; 4405 ktime_t start = ktime_get(); 4406 4407 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE); 4408 4409 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT; 4410 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); 4411 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", 4412 ktime_to_us(ktime_sub(ktime_get(), start)), ret); 4413 4414 if (ret) { 4415 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", 4416 __func__, ret); 4417 } else { 4418 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, 4419 POST_CHANGE); 4420 hba->ufs_stats.last_hibern8_exit_tstamp = local_clock(); 4421 hba->ufs_stats.hibern8_exit_cnt++; 4422 } 4423 4424 return ret; 4425 } 4426 EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit); 4427 4428 static void ufshcd_configure_auto_hibern8(struct ufs_hba *hba) 4429 { 4430 if (!ufshcd_is_auto_hibern8_supported(hba)) 4431 return; 4432 4433 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER); 4434 } 4435 4436 void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit) 4437 { 4438 const u32 cur_ahit = READ_ONCE(hba->ahit); 4439 4440 if (!ufshcd_is_auto_hibern8_supported(hba) || cur_ahit == ahit) 4441 return; 4442 4443 WRITE_ONCE(hba->ahit, ahit); 4444 if (!pm_runtime_suspended(&hba->ufs_device_wlun->sdev_gendev)) { 4445 ufshcd_rpm_get_sync(hba); 4446 ufshcd_hold(hba); 4447 ufshcd_configure_auto_hibern8(hba); 4448 ufshcd_release(hba); 4449 ufshcd_rpm_put_sync(hba); 4450 } 4451 } 4452 EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update); 4453 4454 /** 4455 * ufshcd_init_pwr_info - setting the POR (power on reset) 4456 * values in hba power info 4457 * @hba: per-adapter instance 4458 */ 4459 static void ufshcd_init_pwr_info(struct ufs_hba *hba) 4460 { 4461 hba->pwr_info.gear_rx = UFS_PWM_G1; 4462 hba->pwr_info.gear_tx = UFS_PWM_G1; 4463 hba->pwr_info.lane_rx = UFS_LANE_1; 4464 hba->pwr_info.lane_tx = UFS_LANE_1; 4465 hba->pwr_info.pwr_rx = SLOWAUTO_MODE; 4466 hba->pwr_info.pwr_tx = SLOWAUTO_MODE; 4467 hba->pwr_info.hs_rate = 0; 4468 } 4469 4470 /** 4471 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device 4472 * @hba: per-adapter instance 4473 * 4474 * Return: 0 upon success; < 0 upon failure. 4475 */ 4476 static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) 4477 { 4478 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; 4479 4480 if (hba->max_pwr_info.is_valid) 4481 return 0; 4482 4483 if (hba->quirks & UFSHCD_QUIRK_HIBERN_FASTAUTO) { 4484 pwr_info->pwr_tx = FASTAUTO_MODE; 4485 pwr_info->pwr_rx = FASTAUTO_MODE; 4486 } else { 4487 pwr_info->pwr_tx = FAST_MODE; 4488 pwr_info->pwr_rx = FAST_MODE; 4489 } 4490 pwr_info->hs_rate = PA_HS_MODE_B; 4491 4492 /* Get the connected lane count */ 4493 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), 4494 &pwr_info->lane_rx); 4495 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), 4496 &pwr_info->lane_tx); 4497 4498 if (!pwr_info->lane_rx || !pwr_info->lane_tx) { 4499 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", 4500 __func__, 4501 pwr_info->lane_rx, 4502 pwr_info->lane_tx); 4503 return -EINVAL; 4504 } 4505 4506 /* 4507 * First, get the maximum gears of HS speed. 4508 * If a zero value, it means there is no HSGEAR capability. 4509 * Then, get the maximum gears of PWM speed. 4510 */ 4511 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); 4512 if (!pwr_info->gear_rx) { 4513 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), 4514 &pwr_info->gear_rx); 4515 if (!pwr_info->gear_rx) { 4516 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", 4517 __func__, pwr_info->gear_rx); 4518 return -EINVAL; 4519 } 4520 pwr_info->pwr_rx = SLOW_MODE; 4521 } 4522 4523 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), 4524 &pwr_info->gear_tx); 4525 if (!pwr_info->gear_tx) { 4526 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), 4527 &pwr_info->gear_tx); 4528 if (!pwr_info->gear_tx) { 4529 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", 4530 __func__, pwr_info->gear_tx); 4531 return -EINVAL; 4532 } 4533 pwr_info->pwr_tx = SLOW_MODE; 4534 } 4535 4536 hba->max_pwr_info.is_valid = true; 4537 return 0; 4538 } 4539 4540 static int ufshcd_change_power_mode(struct ufs_hba *hba, 4541 struct ufs_pa_layer_attr *pwr_mode) 4542 { 4543 int ret; 4544 4545 /* if already configured to the requested pwr_mode */ 4546 if (!hba->force_pmc && 4547 pwr_mode->gear_rx == hba->pwr_info.gear_rx && 4548 pwr_mode->gear_tx == hba->pwr_info.gear_tx && 4549 pwr_mode->lane_rx == hba->pwr_info.lane_rx && 4550 pwr_mode->lane_tx == hba->pwr_info.lane_tx && 4551 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && 4552 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && 4553 pwr_mode->hs_rate == hba->pwr_info.hs_rate) { 4554 dev_dbg(hba->dev, "%s: power already configured\n", __func__); 4555 return 0; 4556 } 4557 4558 /* 4559 * Configure attributes for power mode change with below. 4560 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, 4561 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, 4562 * - PA_HSSERIES 4563 */ 4564 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); 4565 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), 4566 pwr_mode->lane_rx); 4567 if (pwr_mode->pwr_rx == FASTAUTO_MODE || 4568 pwr_mode->pwr_rx == FAST_MODE) 4569 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), true); 4570 else 4571 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), false); 4572 4573 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); 4574 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), 4575 pwr_mode->lane_tx); 4576 if (pwr_mode->pwr_tx == FASTAUTO_MODE || 4577 pwr_mode->pwr_tx == FAST_MODE) 4578 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), true); 4579 else 4580 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), false); 4581 4582 if (pwr_mode->pwr_rx == FASTAUTO_MODE || 4583 pwr_mode->pwr_tx == FASTAUTO_MODE || 4584 pwr_mode->pwr_rx == FAST_MODE || 4585 pwr_mode->pwr_tx == FAST_MODE) 4586 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), 4587 pwr_mode->hs_rate); 4588 4589 if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) { 4590 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), 4591 DL_FC0ProtectionTimeOutVal_Default); 4592 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), 4593 DL_TC0ReplayTimeOutVal_Default); 4594 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), 4595 DL_AFC0ReqTimeOutVal_Default); 4596 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3), 4597 DL_FC1ProtectionTimeOutVal_Default); 4598 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4), 4599 DL_TC1ReplayTimeOutVal_Default); 4600 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5), 4601 DL_AFC1ReqTimeOutVal_Default); 4602 4603 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal), 4604 DL_FC0ProtectionTimeOutVal_Default); 4605 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal), 4606 DL_TC0ReplayTimeOutVal_Default); 4607 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal), 4608 DL_AFC0ReqTimeOutVal_Default); 4609 } 4610 4611 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 4612 | pwr_mode->pwr_tx); 4613 4614 if (ret) { 4615 dev_err(hba->dev, 4616 "%s: power mode change failed %d\n", __func__, ret); 4617 } else { 4618 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, 4619 pwr_mode); 4620 4621 memcpy(&hba->pwr_info, pwr_mode, 4622 sizeof(struct ufs_pa_layer_attr)); 4623 } 4624 4625 return ret; 4626 } 4627 4628 /** 4629 * ufshcd_config_pwr_mode - configure a new power mode 4630 * @hba: per-adapter instance 4631 * @desired_pwr_mode: desired power configuration 4632 * 4633 * Return: 0 upon success; < 0 upon failure. 4634 */ 4635 int ufshcd_config_pwr_mode(struct ufs_hba *hba, 4636 struct ufs_pa_layer_attr *desired_pwr_mode) 4637 { 4638 struct ufs_pa_layer_attr final_params = { 0 }; 4639 int ret; 4640 4641 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, 4642 desired_pwr_mode, &final_params); 4643 4644 if (ret) 4645 memcpy(&final_params, desired_pwr_mode, sizeof(final_params)); 4646 4647 ret = ufshcd_change_power_mode(hba, &final_params); 4648 4649 return ret; 4650 } 4651 EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode); 4652 4653 /** 4654 * ufshcd_complete_dev_init() - checks device readiness 4655 * @hba: per-adapter instance 4656 * 4657 * Set fDeviceInit flag and poll until device toggles it. 4658 * 4659 * Return: 0 upon success; < 0 upon failure. 4660 */ 4661 static int ufshcd_complete_dev_init(struct ufs_hba *hba) 4662 { 4663 int err; 4664 bool flag_res = true; 4665 ktime_t timeout; 4666 4667 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, 4668 QUERY_FLAG_IDN_FDEVICEINIT, 0, NULL); 4669 if (err) { 4670 dev_err(hba->dev, 4671 "%s: setting fDeviceInit flag failed with error %d\n", 4672 __func__, err); 4673 goto out; 4674 } 4675 4676 /* Poll fDeviceInit flag to be cleared */ 4677 timeout = ktime_add_ms(ktime_get(), FDEVICEINIT_COMPL_TIMEOUT); 4678 do { 4679 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG, 4680 QUERY_FLAG_IDN_FDEVICEINIT, 0, &flag_res); 4681 if (!flag_res) 4682 break; 4683 usleep_range(500, 1000); 4684 } while (ktime_before(ktime_get(), timeout)); 4685 4686 if (err) { 4687 dev_err(hba->dev, 4688 "%s: reading fDeviceInit flag failed with error %d\n", 4689 __func__, err); 4690 } else if (flag_res) { 4691 dev_err(hba->dev, 4692 "%s: fDeviceInit was not cleared by the device\n", 4693 __func__); 4694 err = -EBUSY; 4695 } 4696 out: 4697 return err; 4698 } 4699 4700 /** 4701 * ufshcd_make_hba_operational - Make UFS controller operational 4702 * @hba: per adapter instance 4703 * 4704 * To bring UFS host controller to operational state, 4705 * 1. Enable required interrupts 4706 * 2. Configure interrupt aggregation 4707 * 3. Program UTRL and UTMRL base address 4708 * 4. Configure run-stop-registers 4709 * 4710 * Return: 0 on success, non-zero value on failure. 4711 */ 4712 int ufshcd_make_hba_operational(struct ufs_hba *hba) 4713 { 4714 int err = 0; 4715 u32 reg; 4716 4717 /* Enable required interrupts */ 4718 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); 4719 4720 /* Configure interrupt aggregation */ 4721 if (ufshcd_is_intr_aggr_allowed(hba)) 4722 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); 4723 else 4724 ufshcd_disable_intr_aggr(hba); 4725 4726 /* Configure UTRL and UTMRL base address registers */ 4727 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), 4728 REG_UTP_TRANSFER_REQ_LIST_BASE_L); 4729 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), 4730 REG_UTP_TRANSFER_REQ_LIST_BASE_H); 4731 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), 4732 REG_UTP_TASK_REQ_LIST_BASE_L); 4733 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), 4734 REG_UTP_TASK_REQ_LIST_BASE_H); 4735 4736 /* 4737 * UCRDY, UTMRLDY and UTRLRDY bits must be 1 4738 */ 4739 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); 4740 if (!(ufshcd_get_lists_status(reg))) { 4741 ufshcd_enable_run_stop_reg(hba); 4742 } else { 4743 dev_err(hba->dev, 4744 "Host controller not ready to process requests"); 4745 err = -EIO; 4746 } 4747 4748 return err; 4749 } 4750 EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational); 4751 4752 /** 4753 * ufshcd_hba_stop - Send controller to reset state 4754 * @hba: per adapter instance 4755 */ 4756 void ufshcd_hba_stop(struct ufs_hba *hba) 4757 { 4758 unsigned long flags; 4759 int err; 4760 4761 /* 4762 * Obtain the host lock to prevent that the controller is disabled 4763 * while the UFS interrupt handler is active on another CPU. 4764 */ 4765 spin_lock_irqsave(hba->host->host_lock, flags); 4766 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); 4767 spin_unlock_irqrestore(hba->host->host_lock, flags); 4768 4769 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, 4770 CONTROLLER_ENABLE, CONTROLLER_DISABLE, 4771 10, 1); 4772 if (err) 4773 dev_err(hba->dev, "%s: Controller disable failed\n", __func__); 4774 } 4775 EXPORT_SYMBOL_GPL(ufshcd_hba_stop); 4776 4777 /** 4778 * ufshcd_hba_execute_hce - initialize the controller 4779 * @hba: per adapter instance 4780 * 4781 * The controller resets itself and controller firmware initialization 4782 * sequence kicks off. When controller is ready it will set 4783 * the Host Controller Enable bit to 1. 4784 * 4785 * Return: 0 on success, non-zero value on failure. 4786 */ 4787 static int ufshcd_hba_execute_hce(struct ufs_hba *hba) 4788 { 4789 int retry_outer = 3; 4790 int retry_inner; 4791 4792 start: 4793 if (ufshcd_is_hba_active(hba)) 4794 /* change controller state to "reset state" */ 4795 ufshcd_hba_stop(hba); 4796 4797 /* UniPro link is disabled at this point */ 4798 ufshcd_set_link_off(hba); 4799 4800 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); 4801 4802 /* start controller initialization sequence */ 4803 ufshcd_hba_start(hba); 4804 4805 /* 4806 * To initialize a UFS host controller HCE bit must be set to 1. 4807 * During initialization the HCE bit value changes from 1->0->1. 4808 * When the host controller completes initialization sequence 4809 * it sets the value of HCE bit to 1. The same HCE bit is read back 4810 * to check if the controller has completed initialization sequence. 4811 * So without this delay the value HCE = 1, set in the previous 4812 * instruction might be read back. 4813 * This delay can be changed based on the controller. 4814 */ 4815 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100); 4816 4817 /* wait for the host controller to complete initialization */ 4818 retry_inner = 50; 4819 while (!ufshcd_is_hba_active(hba)) { 4820 if (retry_inner) { 4821 retry_inner--; 4822 } else { 4823 dev_err(hba->dev, 4824 "Controller enable failed\n"); 4825 if (retry_outer) { 4826 retry_outer--; 4827 goto start; 4828 } 4829 return -EIO; 4830 } 4831 usleep_range(1000, 1100); 4832 } 4833 4834 /* enable UIC related interrupts */ 4835 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); 4836 4837 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); 4838 4839 return 0; 4840 } 4841 4842 int ufshcd_hba_enable(struct ufs_hba *hba) 4843 { 4844 int ret; 4845 4846 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) { 4847 ufshcd_set_link_off(hba); 4848 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); 4849 4850 /* enable UIC related interrupts */ 4851 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); 4852 ret = ufshcd_dme_reset(hba); 4853 if (ret) { 4854 dev_err(hba->dev, "DME_RESET failed\n"); 4855 return ret; 4856 } 4857 4858 ret = ufshcd_dme_enable(hba); 4859 if (ret) { 4860 dev_err(hba->dev, "Enabling DME failed\n"); 4861 return ret; 4862 } 4863 4864 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); 4865 } else { 4866 ret = ufshcd_hba_execute_hce(hba); 4867 } 4868 4869 return ret; 4870 } 4871 EXPORT_SYMBOL_GPL(ufshcd_hba_enable); 4872 4873 static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) 4874 { 4875 int tx_lanes = 0, i, err = 0; 4876 4877 if (!peer) 4878 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), 4879 &tx_lanes); 4880 else 4881 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), 4882 &tx_lanes); 4883 for (i = 0; i < tx_lanes; i++) { 4884 if (!peer) 4885 err = ufshcd_dme_set(hba, 4886 UIC_ARG_MIB_SEL(TX_LCC_ENABLE, 4887 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), 4888 0); 4889 else 4890 err = ufshcd_dme_peer_set(hba, 4891 UIC_ARG_MIB_SEL(TX_LCC_ENABLE, 4892 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)), 4893 0); 4894 if (err) { 4895 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", 4896 __func__, peer, i, err); 4897 break; 4898 } 4899 } 4900 4901 return err; 4902 } 4903 4904 static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) 4905 { 4906 return ufshcd_disable_tx_lcc(hba, true); 4907 } 4908 4909 void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val) 4910 { 4911 struct ufs_event_hist *e; 4912 4913 if (id >= UFS_EVT_CNT) 4914 return; 4915 4916 e = &hba->ufs_stats.event[id]; 4917 e->val[e->pos] = val; 4918 e->tstamp[e->pos] = local_clock(); 4919 e->cnt += 1; 4920 e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH; 4921 4922 ufshcd_vops_event_notify(hba, id, &val); 4923 } 4924 EXPORT_SYMBOL_GPL(ufshcd_update_evt_hist); 4925 4926 /** 4927 * ufshcd_link_startup - Initialize unipro link startup 4928 * @hba: per adapter instance 4929 * 4930 * Return: 0 for success, non-zero in case of failure. 4931 */ 4932 static int ufshcd_link_startup(struct ufs_hba *hba) 4933 { 4934 int ret; 4935 int retries = DME_LINKSTARTUP_RETRIES; 4936 bool link_startup_again = false; 4937 4938 /* 4939 * If UFS device isn't active then we will have to issue link startup 4940 * 2 times to make sure the device state move to active. 4941 */ 4942 if (!ufshcd_is_ufs_dev_active(hba)) 4943 link_startup_again = true; 4944 4945 link_startup: 4946 do { 4947 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); 4948 4949 ret = ufshcd_dme_link_startup(hba); 4950 4951 /* check if device is detected by inter-connect layer */ 4952 if (!ret && !ufshcd_is_device_present(hba)) { 4953 ufshcd_update_evt_hist(hba, 4954 UFS_EVT_LINK_STARTUP_FAIL, 4955 0); 4956 dev_err(hba->dev, "%s: Device not present\n", __func__); 4957 ret = -ENXIO; 4958 goto out; 4959 } 4960 4961 /* 4962 * DME link lost indication is only received when link is up, 4963 * but we can't be sure if the link is up until link startup 4964 * succeeds. So reset the local Uni-Pro and try again. 4965 */ 4966 if (ret && retries && ufshcd_hba_enable(hba)) { 4967 ufshcd_update_evt_hist(hba, 4968 UFS_EVT_LINK_STARTUP_FAIL, 4969 (u32)ret); 4970 goto out; 4971 } 4972 } while (ret && retries--); 4973 4974 if (ret) { 4975 /* failed to get the link up... retire */ 4976 ufshcd_update_evt_hist(hba, 4977 UFS_EVT_LINK_STARTUP_FAIL, 4978 (u32)ret); 4979 goto out; 4980 } 4981 4982 if (link_startup_again) { 4983 link_startup_again = false; 4984 retries = DME_LINKSTARTUP_RETRIES; 4985 goto link_startup; 4986 } 4987 4988 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ 4989 ufshcd_init_pwr_info(hba); 4990 ufshcd_print_pwr_info(hba); 4991 4992 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { 4993 ret = ufshcd_disable_device_tx_lcc(hba); 4994 if (ret) 4995 goto out; 4996 } 4997 4998 /* Include any host controller configuration via UIC commands */ 4999 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); 5000 if (ret) 5001 goto out; 5002 5003 /* Clear UECPA once due to LINERESET has happened during LINK_STARTUP */ 5004 ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); 5005 ret = ufshcd_make_hba_operational(hba); 5006 out: 5007 if (ret) { 5008 dev_err(hba->dev, "link startup failed %d\n", ret); 5009 ufshcd_print_host_state(hba); 5010 ufshcd_print_pwr_info(hba); 5011 ufshcd_print_evt_hist(hba); 5012 } 5013 return ret; 5014 } 5015 5016 /** 5017 * ufshcd_verify_dev_init() - Verify device initialization 5018 * @hba: per-adapter instance 5019 * 5020 * Send NOP OUT UPIU and wait for NOP IN response to check whether the 5021 * device Transport Protocol (UTP) layer is ready after a reset. 5022 * If the UTP layer at the device side is not initialized, it may 5023 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT 5024 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations. 5025 * 5026 * Return: 0 upon success; < 0 upon failure. 5027 */ 5028 static int ufshcd_verify_dev_init(struct ufs_hba *hba) 5029 { 5030 int err = 0; 5031 int retries; 5032 5033 ufshcd_dev_man_lock(hba); 5034 5035 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { 5036 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, 5037 hba->nop_out_timeout); 5038 5039 if (!err || err == -ETIMEDOUT) 5040 break; 5041 5042 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); 5043 } 5044 5045 ufshcd_dev_man_unlock(hba); 5046 5047 if (err) 5048 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); 5049 return err; 5050 } 5051 5052 /** 5053 * ufshcd_setup_links - associate link b/w device wlun and other luns 5054 * @sdev: pointer to SCSI device 5055 * @hba: pointer to ufs hba 5056 */ 5057 static void ufshcd_setup_links(struct ufs_hba *hba, struct scsi_device *sdev) 5058 { 5059 struct device_link *link; 5060 5061 /* 5062 * Device wlun is the supplier & rest of the luns are consumers. 5063 * This ensures that device wlun suspends after all other luns. 5064 */ 5065 if (hba->ufs_device_wlun) { 5066 link = device_link_add(&sdev->sdev_gendev, 5067 &hba->ufs_device_wlun->sdev_gendev, 5068 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE); 5069 if (!link) { 5070 dev_err(&sdev->sdev_gendev, "Failed establishing link - %s\n", 5071 dev_name(&hba->ufs_device_wlun->sdev_gendev)); 5072 return; 5073 } 5074 hba->luns_avail--; 5075 /* Ignore REPORT_LUN wlun probing */ 5076 if (hba->luns_avail == 1) { 5077 ufshcd_rpm_put(hba); 5078 return; 5079 } 5080 } else { 5081 /* 5082 * Device wlun is probed. The assumption is that WLUNs are 5083 * scanned before other LUNs. 5084 */ 5085 hba->luns_avail--; 5086 } 5087 } 5088 5089 /** 5090 * ufshcd_lu_init - Initialize the relevant parameters of the LU 5091 * @hba: per-adapter instance 5092 * @sdev: pointer to SCSI device 5093 */ 5094 static void ufshcd_lu_init(struct ufs_hba *hba, struct scsi_device *sdev) 5095 { 5096 int len = QUERY_DESC_MAX_SIZE; 5097 u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun); 5098 u8 lun_qdepth = hba->nutrs; 5099 u8 *desc_buf; 5100 int ret; 5101 5102 desc_buf = kzalloc(len, GFP_KERNEL); 5103 if (!desc_buf) 5104 goto set_qdepth; 5105 5106 ret = ufshcd_read_unit_desc_param(hba, lun, 0, desc_buf, len); 5107 if (ret < 0) { 5108 if (ret == -EOPNOTSUPP) 5109 /* If LU doesn't support unit descriptor, its queue depth is set to 1 */ 5110 lun_qdepth = 1; 5111 kfree(desc_buf); 5112 goto set_qdepth; 5113 } 5114 5115 if (desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH]) { 5116 /* 5117 * In per-LU queueing architecture, bLUQueueDepth will not be 0, then we will 5118 * use the smaller between UFSHCI CAP.NUTRS and UFS LU bLUQueueDepth 5119 */ 5120 lun_qdepth = min_t(int, desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH], hba->nutrs); 5121 } 5122 /* 5123 * According to UFS device specification, the write protection mode is only supported by 5124 * normal LU, not supported by WLUN. 5125 */ 5126 if (hba->dev_info.f_power_on_wp_en && lun < hba->dev_info.max_lu_supported && 5127 !hba->dev_info.is_lu_power_on_wp && 5128 desc_buf[UNIT_DESC_PARAM_LU_WR_PROTECT] == UFS_LU_POWER_ON_WP) 5129 hba->dev_info.is_lu_power_on_wp = true; 5130 5131 /* In case of RPMB LU, check if advanced RPMB mode is enabled */ 5132 if (desc_buf[UNIT_DESC_PARAM_UNIT_INDEX] == UFS_UPIU_RPMB_WLUN && 5133 desc_buf[RPMB_UNIT_DESC_PARAM_REGION_EN] & BIT(4)) 5134 hba->dev_info.b_advanced_rpmb_en = true; 5135 5136 5137 kfree(desc_buf); 5138 set_qdepth: 5139 /* 5140 * For WLUNs that don't support unit descriptor, queue depth is set to 1. For LUs whose 5141 * bLUQueueDepth == 0, the queue depth is set to a maximum value that host can queue. 5142 */ 5143 dev_dbg(hba->dev, "Set LU %x queue depth %d\n", lun, lun_qdepth); 5144 scsi_change_queue_depth(sdev, lun_qdepth); 5145 } 5146 5147 /** 5148 * ufshcd_slave_alloc - handle initial SCSI device configurations 5149 * @sdev: pointer to SCSI device 5150 * 5151 * Return: success. 5152 */ 5153 static int ufshcd_slave_alloc(struct scsi_device *sdev) 5154 { 5155 struct ufs_hba *hba; 5156 5157 hba = shost_priv(sdev->host); 5158 5159 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ 5160 sdev->use_10_for_ms = 1; 5161 5162 /* DBD field should be set to 1 in mode sense(10) */ 5163 sdev->set_dbd_for_ms = 1; 5164 5165 /* allow SCSI layer to restart the device in case of errors */ 5166 sdev->allow_restart = 1; 5167 5168 /* REPORT SUPPORTED OPERATION CODES is not supported */ 5169 sdev->no_report_opcodes = 1; 5170 5171 /* WRITE_SAME command is not supported */ 5172 sdev->no_write_same = 1; 5173 5174 ufshcd_lu_init(hba, sdev); 5175 5176 ufshcd_setup_links(hba, sdev); 5177 5178 return 0; 5179 } 5180 5181 /** 5182 * ufshcd_change_queue_depth - change queue depth 5183 * @sdev: pointer to SCSI device 5184 * @depth: required depth to set 5185 * 5186 * Change queue depth and make sure the max. limits are not crossed. 5187 * 5188 * Return: new queue depth. 5189 */ 5190 static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth) 5191 { 5192 return scsi_change_queue_depth(sdev, min(depth, sdev->host->can_queue)); 5193 } 5194 5195 /** 5196 * ufshcd_slave_configure - adjust SCSI device configurations 5197 * @sdev: pointer to SCSI device 5198 * 5199 * Return: 0 (success). 5200 */ 5201 static int ufshcd_slave_configure(struct scsi_device *sdev) 5202 { 5203 struct ufs_hba *hba = shost_priv(sdev->host); 5204 struct request_queue *q = sdev->request_queue; 5205 5206 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); 5207 5208 /* 5209 * Block runtime-pm until all consumers are added. 5210 * Refer ufshcd_setup_links(). 5211 */ 5212 if (is_device_wlun(sdev)) 5213 pm_runtime_get_noresume(&sdev->sdev_gendev); 5214 else if (ufshcd_is_rpm_autosuspend_allowed(hba)) 5215 sdev->rpm_autosuspend = 1; 5216 /* 5217 * Do not print messages during runtime PM to avoid never-ending cycles 5218 * of messages written back to storage by user space causing runtime 5219 * resume, causing more messages and so on. 5220 */ 5221 sdev->silence_suspend = 1; 5222 5223 ufshcd_crypto_register(hba, q); 5224 5225 return 0; 5226 } 5227 5228 /** 5229 * ufshcd_slave_destroy - remove SCSI device configurations 5230 * @sdev: pointer to SCSI device 5231 */ 5232 static void ufshcd_slave_destroy(struct scsi_device *sdev) 5233 { 5234 struct ufs_hba *hba; 5235 unsigned long flags; 5236 5237 hba = shost_priv(sdev->host); 5238 5239 /* Drop the reference as it won't be needed anymore */ 5240 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { 5241 spin_lock_irqsave(hba->host->host_lock, flags); 5242 hba->ufs_device_wlun = NULL; 5243 spin_unlock_irqrestore(hba->host->host_lock, flags); 5244 } else if (hba->ufs_device_wlun) { 5245 struct device *supplier = NULL; 5246 5247 /* Ensure UFS Device WLUN exists and does not disappear */ 5248 spin_lock_irqsave(hba->host->host_lock, flags); 5249 if (hba->ufs_device_wlun) { 5250 supplier = &hba->ufs_device_wlun->sdev_gendev; 5251 get_device(supplier); 5252 } 5253 spin_unlock_irqrestore(hba->host->host_lock, flags); 5254 5255 if (supplier) { 5256 /* 5257 * If a LUN fails to probe (e.g. absent BOOT WLUN), the 5258 * device will not have been registered but can still 5259 * have a device link holding a reference to the device. 5260 */ 5261 device_link_remove(&sdev->sdev_gendev, supplier); 5262 put_device(supplier); 5263 } 5264 } 5265 } 5266 5267 /** 5268 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status 5269 * @lrbp: pointer to local reference block of completed command 5270 * @scsi_status: SCSI command status 5271 * 5272 * Return: value base on SCSI command status. 5273 */ 5274 static inline int 5275 ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) 5276 { 5277 int result = 0; 5278 5279 switch (scsi_status) { 5280 case SAM_STAT_CHECK_CONDITION: 5281 ufshcd_copy_sense_data(lrbp); 5282 fallthrough; 5283 case SAM_STAT_GOOD: 5284 result |= DID_OK << 16 | scsi_status; 5285 break; 5286 case SAM_STAT_TASK_SET_FULL: 5287 case SAM_STAT_BUSY: 5288 case SAM_STAT_TASK_ABORTED: 5289 ufshcd_copy_sense_data(lrbp); 5290 result |= scsi_status; 5291 break; 5292 default: 5293 result |= DID_ERROR << 16; 5294 break; 5295 } /* end of switch */ 5296 5297 return result; 5298 } 5299 5300 /** 5301 * ufshcd_transfer_rsp_status - Get overall status of the response 5302 * @hba: per adapter instance 5303 * @lrbp: pointer to local reference block of completed command 5304 * @cqe: pointer to the completion queue entry 5305 * 5306 * Return: result of the command to notify SCSI midlayer. 5307 */ 5308 static inline int 5309 ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, 5310 struct cq_entry *cqe) 5311 { 5312 int result = 0; 5313 int scsi_status; 5314 enum utp_ocs ocs; 5315 u8 upiu_flags; 5316 u32 resid; 5317 5318 upiu_flags = lrbp->ucd_rsp_ptr->header.flags; 5319 resid = be32_to_cpu(lrbp->ucd_rsp_ptr->sr.residual_transfer_count); 5320 /* 5321 * Test !overflow instead of underflow to support UFS devices that do 5322 * not set either flag. 5323 */ 5324 if (resid && !(upiu_flags & UPIU_RSP_FLAG_OVERFLOW)) 5325 scsi_set_resid(lrbp->cmd, resid); 5326 5327 /* overall command status of utrd */ 5328 ocs = ufshcd_get_tr_ocs(lrbp, cqe); 5329 5330 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) { 5331 if (lrbp->ucd_rsp_ptr->header.response || 5332 lrbp->ucd_rsp_ptr->header.status) 5333 ocs = OCS_SUCCESS; 5334 } 5335 5336 switch (ocs) { 5337 case OCS_SUCCESS: 5338 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); 5339 switch (ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr)) { 5340 case UPIU_TRANSACTION_RESPONSE: 5341 /* 5342 * get the result based on SCSI status response 5343 * to notify the SCSI midlayer of the command status 5344 */ 5345 scsi_status = lrbp->ucd_rsp_ptr->header.status; 5346 result = ufshcd_scsi_cmd_status(lrbp, scsi_status); 5347 5348 /* 5349 * Currently we are only supporting BKOPs exception 5350 * events hence we can ignore BKOPs exception event 5351 * during power management callbacks. BKOPs exception 5352 * event is not expected to be raised in runtime suspend 5353 * callback as it allows the urgent bkops. 5354 * During system suspend, we are anyway forcefully 5355 * disabling the bkops and if urgent bkops is needed 5356 * it will be enabled on system resume. Long term 5357 * solution could be to abort the system suspend if 5358 * UFS device needs urgent BKOPs. 5359 */ 5360 if (!hba->pm_op_in_progress && 5361 !ufshcd_eh_in_progress(hba) && 5362 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) 5363 /* Flushed in suspend */ 5364 schedule_work(&hba->eeh_work); 5365 break; 5366 case UPIU_TRANSACTION_REJECT_UPIU: 5367 /* TODO: handle Reject UPIU Response */ 5368 result = DID_ERROR << 16; 5369 dev_err(hba->dev, 5370 "Reject UPIU not fully implemented\n"); 5371 break; 5372 default: 5373 dev_err(hba->dev, 5374 "Unexpected request response code = %x\n", 5375 result); 5376 result = DID_ERROR << 16; 5377 break; 5378 } 5379 break; 5380 case OCS_ABORTED: 5381 result |= DID_ABORT << 16; 5382 break; 5383 case OCS_INVALID_COMMAND_STATUS: 5384 result |= DID_REQUEUE << 16; 5385 break; 5386 case OCS_INVALID_CMD_TABLE_ATTR: 5387 case OCS_INVALID_PRDT_ATTR: 5388 case OCS_MISMATCH_DATA_BUF_SIZE: 5389 case OCS_MISMATCH_RESP_UPIU_SIZE: 5390 case OCS_PEER_COMM_FAILURE: 5391 case OCS_FATAL_ERROR: 5392 case OCS_DEVICE_FATAL_ERROR: 5393 case OCS_INVALID_CRYPTO_CONFIG: 5394 case OCS_GENERAL_CRYPTO_ERROR: 5395 default: 5396 result |= DID_ERROR << 16; 5397 dev_err(hba->dev, 5398 "OCS error from controller = %x for tag %d\n", 5399 ocs, lrbp->task_tag); 5400 ufshcd_print_evt_hist(hba); 5401 ufshcd_print_host_state(hba); 5402 break; 5403 } /* end of switch */ 5404 5405 if ((host_byte(result) != DID_OK) && 5406 (host_byte(result) != DID_REQUEUE) && !hba->silence_err_logs) 5407 ufshcd_print_tr(hba, lrbp->task_tag, true); 5408 return result; 5409 } 5410 5411 static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba, 5412 u32 intr_mask) 5413 { 5414 if (!ufshcd_is_auto_hibern8_supported(hba) || 5415 !ufshcd_is_auto_hibern8_enabled(hba)) 5416 return false; 5417 5418 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK)) 5419 return false; 5420 5421 if (hba->active_uic_cmd && 5422 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER || 5423 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT)) 5424 return false; 5425 5426 return true; 5427 } 5428 5429 /** 5430 * ufshcd_uic_cmd_compl - handle completion of uic command 5431 * @hba: per adapter instance 5432 * @intr_status: interrupt status generated by the controller 5433 * 5434 * Return: 5435 * IRQ_HANDLED - If interrupt is valid 5436 * IRQ_NONE - If invalid interrupt 5437 */ 5438 static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status) 5439 { 5440 irqreturn_t retval = IRQ_NONE; 5441 5442 spin_lock(hba->host->host_lock); 5443 if (ufshcd_is_auto_hibern8_error(hba, intr_status)) 5444 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status); 5445 5446 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { 5447 hba->active_uic_cmd->argument2 |= 5448 ufshcd_get_uic_cmd_result(hba); 5449 hba->active_uic_cmd->argument3 = 5450 ufshcd_get_dme_attr_val(hba); 5451 if (!hba->uic_async_done) 5452 hba->active_uic_cmd->cmd_active = 0; 5453 complete(&hba->active_uic_cmd->done); 5454 retval = IRQ_HANDLED; 5455 } 5456 5457 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) { 5458 hba->active_uic_cmd->cmd_active = 0; 5459 complete(hba->uic_async_done); 5460 retval = IRQ_HANDLED; 5461 } 5462 5463 if (retval == IRQ_HANDLED) 5464 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd, 5465 UFS_CMD_COMP); 5466 spin_unlock(hba->host->host_lock); 5467 return retval; 5468 } 5469 5470 /* Release the resources allocated for processing a SCSI command. */ 5471 void ufshcd_release_scsi_cmd(struct ufs_hba *hba, 5472 struct ufshcd_lrb *lrbp) 5473 { 5474 struct scsi_cmnd *cmd = lrbp->cmd; 5475 5476 scsi_dma_unmap(cmd); 5477 ufshcd_release(hba); 5478 ufshcd_clk_scaling_update_busy(hba); 5479 } 5480 5481 /** 5482 * ufshcd_compl_one_cqe - handle a completion queue entry 5483 * @hba: per adapter instance 5484 * @task_tag: the task tag of the request to be completed 5485 * @cqe: pointer to the completion queue entry 5486 */ 5487 void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag, 5488 struct cq_entry *cqe) 5489 { 5490 struct ufshcd_lrb *lrbp; 5491 struct scsi_cmnd *cmd; 5492 enum utp_ocs ocs; 5493 5494 lrbp = &hba->lrb[task_tag]; 5495 lrbp->compl_time_stamp = ktime_get(); 5496 cmd = lrbp->cmd; 5497 if (cmd) { 5498 if (unlikely(ufshcd_should_inform_monitor(hba, lrbp))) 5499 ufshcd_update_monitor(hba, lrbp); 5500 ufshcd_add_command_trace(hba, task_tag, UFS_CMD_COMP); 5501 cmd->result = ufshcd_transfer_rsp_status(hba, lrbp, cqe); 5502 ufshcd_release_scsi_cmd(hba, lrbp); 5503 /* Do not touch lrbp after scsi done */ 5504 scsi_done(cmd); 5505 } else if (hba->dev_cmd.complete) { 5506 if (cqe) { 5507 ocs = le32_to_cpu(cqe->status) & MASK_OCS; 5508 lrbp->utr_descriptor_ptr->header.ocs = ocs; 5509 } 5510 complete(hba->dev_cmd.complete); 5511 } 5512 } 5513 5514 /** 5515 * __ufshcd_transfer_req_compl - handle SCSI and query command completion 5516 * @hba: per adapter instance 5517 * @completed_reqs: bitmask that indicates which requests to complete 5518 */ 5519 static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, 5520 unsigned long completed_reqs) 5521 { 5522 int tag; 5523 5524 for_each_set_bit(tag, &completed_reqs, hba->nutrs) 5525 ufshcd_compl_one_cqe(hba, tag, NULL); 5526 } 5527 5528 /* Any value that is not an existing queue number is fine for this constant. */ 5529 enum { 5530 UFSHCD_POLL_FROM_INTERRUPT_CONTEXT = -1 5531 }; 5532 5533 static void ufshcd_clear_polled(struct ufs_hba *hba, 5534 unsigned long *completed_reqs) 5535 { 5536 int tag; 5537 5538 for_each_set_bit(tag, completed_reqs, hba->nutrs) { 5539 struct scsi_cmnd *cmd = hba->lrb[tag].cmd; 5540 5541 if (!cmd) 5542 continue; 5543 if (scsi_cmd_to_rq(cmd)->cmd_flags & REQ_POLLED) 5544 __clear_bit(tag, completed_reqs); 5545 } 5546 } 5547 5548 /* 5549 * Return: > 0 if one or more commands have been completed or 0 if no 5550 * requests have been completed. 5551 */ 5552 static int ufshcd_poll(struct Scsi_Host *shost, unsigned int queue_num) 5553 { 5554 struct ufs_hba *hba = shost_priv(shost); 5555 unsigned long completed_reqs, flags; 5556 u32 tr_doorbell; 5557 struct ufs_hw_queue *hwq; 5558 5559 if (is_mcq_enabled(hba)) { 5560 hwq = &hba->uhq[queue_num]; 5561 5562 return ufshcd_mcq_poll_cqe_lock(hba, hwq); 5563 } 5564 5565 spin_lock_irqsave(&hba->outstanding_lock, flags); 5566 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); 5567 completed_reqs = ~tr_doorbell & hba->outstanding_reqs; 5568 WARN_ONCE(completed_reqs & ~hba->outstanding_reqs, 5569 "completed: %#lx; outstanding: %#lx\n", completed_reqs, 5570 hba->outstanding_reqs); 5571 if (queue_num == UFSHCD_POLL_FROM_INTERRUPT_CONTEXT) { 5572 /* Do not complete polled requests from interrupt context. */ 5573 ufshcd_clear_polled(hba, &completed_reqs); 5574 } 5575 hba->outstanding_reqs &= ~completed_reqs; 5576 spin_unlock_irqrestore(&hba->outstanding_lock, flags); 5577 5578 if (completed_reqs) 5579 __ufshcd_transfer_req_compl(hba, completed_reqs); 5580 5581 return completed_reqs != 0; 5582 } 5583 5584 /** 5585 * ufshcd_mcq_compl_pending_transfer - MCQ mode function. It is 5586 * invoked from the error handler context or ufshcd_host_reset_and_restore() 5587 * to complete the pending transfers and free the resources associated with 5588 * the scsi command. 5589 * 5590 * @hba: per adapter instance 5591 * @force_compl: This flag is set to true when invoked 5592 * from ufshcd_host_reset_and_restore() in which case it requires special 5593 * handling because the host controller has been reset by ufshcd_hba_stop(). 5594 */ 5595 static void ufshcd_mcq_compl_pending_transfer(struct ufs_hba *hba, 5596 bool force_compl) 5597 { 5598 struct ufs_hw_queue *hwq; 5599 struct ufshcd_lrb *lrbp; 5600 struct scsi_cmnd *cmd; 5601 unsigned long flags; 5602 int tag; 5603 5604 for (tag = 0; tag < hba->nutrs; tag++) { 5605 lrbp = &hba->lrb[tag]; 5606 cmd = lrbp->cmd; 5607 if (!ufshcd_cmd_inflight(cmd) || 5608 test_bit(SCMD_STATE_COMPLETE, &cmd->state)) 5609 continue; 5610 5611 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(cmd)); 5612 5613 if (force_compl) { 5614 ufshcd_mcq_compl_all_cqes_lock(hba, hwq); 5615 /* 5616 * For those cmds of which the cqes are not present 5617 * in the cq, complete them explicitly. 5618 */ 5619 spin_lock_irqsave(&hwq->cq_lock, flags); 5620 if (cmd && !test_bit(SCMD_STATE_COMPLETE, &cmd->state)) { 5621 set_host_byte(cmd, DID_REQUEUE); 5622 ufshcd_release_scsi_cmd(hba, lrbp); 5623 scsi_done(cmd); 5624 } 5625 spin_unlock_irqrestore(&hwq->cq_lock, flags); 5626 } else { 5627 ufshcd_mcq_poll_cqe_lock(hba, hwq); 5628 } 5629 } 5630 } 5631 5632 /** 5633 * ufshcd_transfer_req_compl - handle SCSI and query command completion 5634 * @hba: per adapter instance 5635 * 5636 * Return: 5637 * IRQ_HANDLED - If interrupt is valid 5638 * IRQ_NONE - If invalid interrupt 5639 */ 5640 static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba) 5641 { 5642 /* Resetting interrupt aggregation counters first and reading the 5643 * DOOR_BELL afterward allows us to handle all the completed requests. 5644 * In order to prevent other interrupts starvation the DB is read once 5645 * after reset. The down side of this solution is the possibility of 5646 * false interrupt if device completes another request after resetting 5647 * aggregation and before reading the DB. 5648 */ 5649 if (ufshcd_is_intr_aggr_allowed(hba) && 5650 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR)) 5651 ufshcd_reset_intr_aggr(hba); 5652 5653 if (ufs_fail_completion(hba)) 5654 return IRQ_HANDLED; 5655 5656 /* 5657 * Ignore the ufshcd_poll() return value and return IRQ_HANDLED since we 5658 * do not want polling to trigger spurious interrupt complaints. 5659 */ 5660 ufshcd_poll(hba->host, UFSHCD_POLL_FROM_INTERRUPT_CONTEXT); 5661 5662 return IRQ_HANDLED; 5663 } 5664 5665 int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask) 5666 { 5667 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, 5668 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, 5669 &ee_ctrl_mask); 5670 } 5671 5672 int ufshcd_write_ee_control(struct ufs_hba *hba) 5673 { 5674 int err; 5675 5676 mutex_lock(&hba->ee_ctrl_mutex); 5677 err = __ufshcd_write_ee_control(hba, hba->ee_ctrl_mask); 5678 mutex_unlock(&hba->ee_ctrl_mutex); 5679 if (err) 5680 dev_err(hba->dev, "%s: failed to write ee control %d\n", 5681 __func__, err); 5682 return err; 5683 } 5684 5685 int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask, 5686 const u16 *other_mask, u16 set, u16 clr) 5687 { 5688 u16 new_mask, ee_ctrl_mask; 5689 int err = 0; 5690 5691 mutex_lock(&hba->ee_ctrl_mutex); 5692 new_mask = (*mask & ~clr) | set; 5693 ee_ctrl_mask = new_mask | *other_mask; 5694 if (ee_ctrl_mask != hba->ee_ctrl_mask) 5695 err = __ufshcd_write_ee_control(hba, ee_ctrl_mask); 5696 /* Still need to update 'mask' even if 'ee_ctrl_mask' was unchanged */ 5697 if (!err) { 5698 hba->ee_ctrl_mask = ee_ctrl_mask; 5699 *mask = new_mask; 5700 } 5701 mutex_unlock(&hba->ee_ctrl_mutex); 5702 return err; 5703 } 5704 5705 /** 5706 * ufshcd_disable_ee - disable exception event 5707 * @hba: per-adapter instance 5708 * @mask: exception event to disable 5709 * 5710 * Disables exception event in the device so that the EVENT_ALERT 5711 * bit is not set. 5712 * 5713 * Return: zero on success, non-zero error value on failure. 5714 */ 5715 static inline int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) 5716 { 5717 return ufshcd_update_ee_drv_mask(hba, 0, mask); 5718 } 5719 5720 /** 5721 * ufshcd_enable_ee - enable exception event 5722 * @hba: per-adapter instance 5723 * @mask: exception event to enable 5724 * 5725 * Enable corresponding exception event in the device to allow 5726 * device to alert host in critical scenarios. 5727 * 5728 * Return: zero on success, non-zero error value on failure. 5729 */ 5730 static inline int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) 5731 { 5732 return ufshcd_update_ee_drv_mask(hba, mask, 0); 5733 } 5734 5735 /** 5736 * ufshcd_enable_auto_bkops - Allow device managed BKOPS 5737 * @hba: per-adapter instance 5738 * 5739 * Allow device to manage background operations on its own. Enabling 5740 * this might lead to inconsistent latencies during normal data transfers 5741 * as the device is allowed to manage its own way of handling background 5742 * operations. 5743 * 5744 * Return: zero on success, non-zero on failure. 5745 */ 5746 static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) 5747 { 5748 int err = 0; 5749 5750 if (hba->auto_bkops_enabled) 5751 goto out; 5752 5753 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, 5754 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL); 5755 if (err) { 5756 dev_err(hba->dev, "%s: failed to enable bkops %d\n", 5757 __func__, err); 5758 goto out; 5759 } 5760 5761 hba->auto_bkops_enabled = true; 5762 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled"); 5763 5764 /* No need of URGENT_BKOPS exception from the device */ 5765 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); 5766 if (err) 5767 dev_err(hba->dev, "%s: failed to disable exception event %d\n", 5768 __func__, err); 5769 out: 5770 return err; 5771 } 5772 5773 /** 5774 * ufshcd_disable_auto_bkops - block device in doing background operations 5775 * @hba: per-adapter instance 5776 * 5777 * Disabling background operations improves command response latency but 5778 * has drawback of device moving into critical state where the device is 5779 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the 5780 * host is idle so that BKOPS are managed effectively without any negative 5781 * impacts. 5782 * 5783 * Return: zero on success, non-zero on failure. 5784 */ 5785 static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) 5786 { 5787 int err = 0; 5788 5789 if (!hba->auto_bkops_enabled) 5790 goto out; 5791 5792 /* 5793 * If host assisted BKOPs is to be enabled, make sure 5794 * urgent bkops exception is allowed. 5795 */ 5796 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); 5797 if (err) { 5798 dev_err(hba->dev, "%s: failed to enable exception event %d\n", 5799 __func__, err); 5800 goto out; 5801 } 5802 5803 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, 5804 QUERY_FLAG_IDN_BKOPS_EN, 0, NULL); 5805 if (err) { 5806 dev_err(hba->dev, "%s: failed to disable bkops %d\n", 5807 __func__, err); 5808 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); 5809 goto out; 5810 } 5811 5812 hba->auto_bkops_enabled = false; 5813 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled"); 5814 hba->is_urgent_bkops_lvl_checked = false; 5815 out: 5816 return err; 5817 } 5818 5819 /** 5820 * ufshcd_force_reset_auto_bkops - force reset auto bkops state 5821 * @hba: per adapter instance 5822 * 5823 * After a device reset the device may toggle the BKOPS_EN flag 5824 * to default value. The s/w tracking variables should be updated 5825 * as well. This function would change the auto-bkops state based on 5826 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND. 5827 */ 5828 static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) 5829 { 5830 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { 5831 hba->auto_bkops_enabled = false; 5832 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; 5833 ufshcd_enable_auto_bkops(hba); 5834 } else { 5835 hba->auto_bkops_enabled = true; 5836 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; 5837 ufshcd_disable_auto_bkops(hba); 5838 } 5839 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; 5840 hba->is_urgent_bkops_lvl_checked = false; 5841 } 5842 5843 static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) 5844 { 5845 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 5846 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); 5847 } 5848 5849 /** 5850 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status 5851 * @hba: per-adapter instance 5852 * @status: bkops_status value 5853 * 5854 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn 5855 * flag in the device to permit background operations if the device 5856 * bkops_status is greater than or equal to "status" argument passed to 5857 * this function, disable otherwise. 5858 * 5859 * Return: 0 for success, non-zero in case of failure. 5860 * 5861 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag 5862 * to know whether auto bkops is enabled or disabled after this function 5863 * returns control to it. 5864 */ 5865 static int ufshcd_bkops_ctrl(struct ufs_hba *hba, 5866 enum bkops_status status) 5867 { 5868 int err; 5869 u32 curr_status = 0; 5870 5871 err = ufshcd_get_bkops_status(hba, &curr_status); 5872 if (err) { 5873 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", 5874 __func__, err); 5875 goto out; 5876 } else if (curr_status > BKOPS_STATUS_MAX) { 5877 dev_err(hba->dev, "%s: invalid BKOPS status %d\n", 5878 __func__, curr_status); 5879 err = -EINVAL; 5880 goto out; 5881 } 5882 5883 if (curr_status >= status) 5884 err = ufshcd_enable_auto_bkops(hba); 5885 else 5886 err = ufshcd_disable_auto_bkops(hba); 5887 out: 5888 return err; 5889 } 5890 5891 /** 5892 * ufshcd_urgent_bkops - handle urgent bkops exception event 5893 * @hba: per-adapter instance 5894 * 5895 * Enable fBackgroundOpsEn flag in the device to permit background 5896 * operations. 5897 * 5898 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled 5899 * and negative error value for any other failure. 5900 * 5901 * Return: 0 upon success; < 0 upon failure. 5902 */ 5903 static int ufshcd_urgent_bkops(struct ufs_hba *hba) 5904 { 5905 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); 5906 } 5907 5908 static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) 5909 { 5910 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 5911 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); 5912 } 5913 5914 static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) 5915 { 5916 int err; 5917 u32 curr_status = 0; 5918 5919 if (hba->is_urgent_bkops_lvl_checked) 5920 goto enable_auto_bkops; 5921 5922 err = ufshcd_get_bkops_status(hba, &curr_status); 5923 if (err) { 5924 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", 5925 __func__, err); 5926 goto out; 5927 } 5928 5929 /* 5930 * We are seeing that some devices are raising the urgent bkops 5931 * exception events even when BKOPS status doesn't indicate performace 5932 * impacted or critical. Handle these device by determining their urgent 5933 * bkops status at runtime. 5934 */ 5935 if (curr_status < BKOPS_STATUS_PERF_IMPACT) { 5936 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", 5937 __func__, curr_status); 5938 /* update the current status as the urgent bkops level */ 5939 hba->urgent_bkops_lvl = curr_status; 5940 hba->is_urgent_bkops_lvl_checked = true; 5941 } 5942 5943 enable_auto_bkops: 5944 err = ufshcd_enable_auto_bkops(hba); 5945 out: 5946 if (err < 0) 5947 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", 5948 __func__, err); 5949 } 5950 5951 static void ufshcd_temp_exception_event_handler(struct ufs_hba *hba, u16 status) 5952 { 5953 u32 value; 5954 5955 if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 5956 QUERY_ATTR_IDN_CASE_ROUGH_TEMP, 0, 0, &value)) 5957 return; 5958 5959 dev_info(hba->dev, "exception Tcase %d\n", value - 80); 5960 5961 ufs_hwmon_notify_event(hba, status & MASK_EE_URGENT_TEMP); 5962 5963 /* 5964 * A placeholder for the platform vendors to add whatever additional 5965 * steps required 5966 */ 5967 } 5968 5969 static int __ufshcd_wb_toggle(struct ufs_hba *hba, bool set, enum flag_idn idn) 5970 { 5971 u8 index; 5972 enum query_opcode opcode = set ? UPIU_QUERY_OPCODE_SET_FLAG : 5973 UPIU_QUERY_OPCODE_CLEAR_FLAG; 5974 5975 index = ufshcd_wb_get_query_index(hba); 5976 return ufshcd_query_flag_retry(hba, opcode, idn, index, NULL); 5977 } 5978 5979 int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable) 5980 { 5981 int ret; 5982 5983 if (!ufshcd_is_wb_allowed(hba) || 5984 hba->dev_info.wb_enabled == enable) 5985 return 0; 5986 5987 ret = __ufshcd_wb_toggle(hba, enable, QUERY_FLAG_IDN_WB_EN); 5988 if (ret) { 5989 dev_err(hba->dev, "%s: Write Booster %s failed %d\n", 5990 __func__, enable ? "enabling" : "disabling", ret); 5991 return ret; 5992 } 5993 5994 hba->dev_info.wb_enabled = enable; 5995 dev_dbg(hba->dev, "%s: Write Booster %s\n", 5996 __func__, enable ? "enabled" : "disabled"); 5997 5998 return ret; 5999 } 6000 6001 static void ufshcd_wb_toggle_buf_flush_during_h8(struct ufs_hba *hba, 6002 bool enable) 6003 { 6004 int ret; 6005 6006 ret = __ufshcd_wb_toggle(hba, enable, 6007 QUERY_FLAG_IDN_WB_BUFF_FLUSH_DURING_HIBERN8); 6008 if (ret) { 6009 dev_err(hba->dev, "%s: WB-Buf Flush during H8 %s failed %d\n", 6010 __func__, enable ? "enabling" : "disabling", ret); 6011 return; 6012 } 6013 dev_dbg(hba->dev, "%s: WB-Buf Flush during H8 %s\n", 6014 __func__, enable ? "enabled" : "disabled"); 6015 } 6016 6017 int ufshcd_wb_toggle_buf_flush(struct ufs_hba *hba, bool enable) 6018 { 6019 int ret; 6020 6021 if (!ufshcd_is_wb_allowed(hba) || 6022 hba->dev_info.wb_buf_flush_enabled == enable) 6023 return 0; 6024 6025 ret = __ufshcd_wb_toggle(hba, enable, QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN); 6026 if (ret) { 6027 dev_err(hba->dev, "%s: WB-Buf Flush %s failed %d\n", 6028 __func__, enable ? "enabling" : "disabling", ret); 6029 return ret; 6030 } 6031 6032 hba->dev_info.wb_buf_flush_enabled = enable; 6033 dev_dbg(hba->dev, "%s: WB-Buf Flush %s\n", 6034 __func__, enable ? "enabled" : "disabled"); 6035 6036 return ret; 6037 } 6038 6039 static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba, 6040 u32 avail_buf) 6041 { 6042 u32 cur_buf; 6043 int ret; 6044 u8 index; 6045 6046 index = ufshcd_wb_get_query_index(hba); 6047 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 6048 QUERY_ATTR_IDN_CURR_WB_BUFF_SIZE, 6049 index, 0, &cur_buf); 6050 if (ret) { 6051 dev_err(hba->dev, "%s: dCurWriteBoosterBufferSize read failed %d\n", 6052 __func__, ret); 6053 return false; 6054 } 6055 6056 if (!cur_buf) { 6057 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n", 6058 cur_buf); 6059 return false; 6060 } 6061 /* Let it continue to flush when available buffer exceeds threshold */ 6062 return avail_buf < hba->vps->wb_flush_threshold; 6063 } 6064 6065 static void ufshcd_wb_force_disable(struct ufs_hba *hba) 6066 { 6067 if (ufshcd_is_wb_buf_flush_allowed(hba)) 6068 ufshcd_wb_toggle_buf_flush(hba, false); 6069 6070 ufshcd_wb_toggle_buf_flush_during_h8(hba, false); 6071 ufshcd_wb_toggle(hba, false); 6072 hba->caps &= ~UFSHCD_CAP_WB_EN; 6073 6074 dev_info(hba->dev, "%s: WB force disabled\n", __func__); 6075 } 6076 6077 static bool ufshcd_is_wb_buf_lifetime_available(struct ufs_hba *hba) 6078 { 6079 u32 lifetime; 6080 int ret; 6081 u8 index; 6082 6083 index = ufshcd_wb_get_query_index(hba); 6084 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 6085 QUERY_ATTR_IDN_WB_BUFF_LIFE_TIME_EST, 6086 index, 0, &lifetime); 6087 if (ret) { 6088 dev_err(hba->dev, 6089 "%s: bWriteBoosterBufferLifeTimeEst read failed %d\n", 6090 __func__, ret); 6091 return false; 6092 } 6093 6094 if (lifetime == UFS_WB_EXCEED_LIFETIME) { 6095 dev_err(hba->dev, "%s: WB buf lifetime is exhausted 0x%02X\n", 6096 __func__, lifetime); 6097 return false; 6098 } 6099 6100 dev_dbg(hba->dev, "%s: WB buf lifetime is 0x%02X\n", 6101 __func__, lifetime); 6102 6103 return true; 6104 } 6105 6106 static bool ufshcd_wb_need_flush(struct ufs_hba *hba) 6107 { 6108 int ret; 6109 u32 avail_buf; 6110 u8 index; 6111 6112 if (!ufshcd_is_wb_allowed(hba)) 6113 return false; 6114 6115 if (!ufshcd_is_wb_buf_lifetime_available(hba)) { 6116 ufshcd_wb_force_disable(hba); 6117 return false; 6118 } 6119 6120 /* 6121 * The ufs device needs the vcc to be ON to flush. 6122 * With user-space reduction enabled, it's enough to enable flush 6123 * by checking only the available buffer. The threshold 6124 * defined here is > 90% full. 6125 * With user-space preserved enabled, the current-buffer 6126 * should be checked too because the wb buffer size can reduce 6127 * when disk tends to be full. This info is provided by current 6128 * buffer (dCurrentWriteBoosterBufferSize). There's no point in 6129 * keeping vcc on when current buffer is empty. 6130 */ 6131 index = ufshcd_wb_get_query_index(hba); 6132 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 6133 QUERY_ATTR_IDN_AVAIL_WB_BUFF_SIZE, 6134 index, 0, &avail_buf); 6135 if (ret) { 6136 dev_warn(hba->dev, "%s: dAvailableWriteBoosterBufferSize read failed %d\n", 6137 __func__, ret); 6138 return false; 6139 } 6140 6141 if (!hba->dev_info.b_presrv_uspc_en) 6142 return avail_buf <= UFS_WB_BUF_REMAIN_PERCENT(10); 6143 6144 return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf); 6145 } 6146 6147 static void ufshcd_rpm_dev_flush_recheck_work(struct work_struct *work) 6148 { 6149 struct ufs_hba *hba = container_of(to_delayed_work(work), 6150 struct ufs_hba, 6151 rpm_dev_flush_recheck_work); 6152 /* 6153 * To prevent unnecessary VCC power drain after device finishes 6154 * WriteBooster buffer flush or Auto BKOPs, force runtime resume 6155 * after a certain delay to recheck the threshold by next runtime 6156 * suspend. 6157 */ 6158 ufshcd_rpm_get_sync(hba); 6159 ufshcd_rpm_put_sync(hba); 6160 } 6161 6162 /** 6163 * ufshcd_exception_event_handler - handle exceptions raised by device 6164 * @work: pointer to work data 6165 * 6166 * Read bExceptionEventStatus attribute from the device and handle the 6167 * exception event accordingly. 6168 */ 6169 static void ufshcd_exception_event_handler(struct work_struct *work) 6170 { 6171 struct ufs_hba *hba; 6172 int err; 6173 u32 status = 0; 6174 hba = container_of(work, struct ufs_hba, eeh_work); 6175 6176 ufshcd_scsi_block_requests(hba); 6177 err = ufshcd_get_ee_status(hba, &status); 6178 if (err) { 6179 dev_err(hba->dev, "%s: failed to get exception status %d\n", 6180 __func__, err); 6181 goto out; 6182 } 6183 6184 trace_ufshcd_exception_event(dev_name(hba->dev), status); 6185 6186 if (status & hba->ee_drv_mask & MASK_EE_URGENT_BKOPS) 6187 ufshcd_bkops_exception_event_handler(hba); 6188 6189 if (status & hba->ee_drv_mask & MASK_EE_URGENT_TEMP) 6190 ufshcd_temp_exception_event_handler(hba, status); 6191 6192 ufs_debugfs_exception_event(hba, status); 6193 out: 6194 ufshcd_scsi_unblock_requests(hba); 6195 } 6196 6197 /* Complete requests that have door-bell cleared */ 6198 static void ufshcd_complete_requests(struct ufs_hba *hba, bool force_compl) 6199 { 6200 if (is_mcq_enabled(hba)) 6201 ufshcd_mcq_compl_pending_transfer(hba, force_compl); 6202 else 6203 ufshcd_transfer_req_compl(hba); 6204 6205 ufshcd_tmc_handler(hba); 6206 } 6207 6208 /** 6209 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is 6210 * to recover from the DL NAC errors or not. 6211 * @hba: per-adapter instance 6212 * 6213 * Return: true if error handling is required, false otherwise. 6214 */ 6215 static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) 6216 { 6217 unsigned long flags; 6218 bool err_handling = true; 6219 6220 spin_lock_irqsave(hba->host->host_lock, flags); 6221 /* 6222 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the 6223 * device fatal error and/or DL NAC & REPLAY timeout errors. 6224 */ 6225 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) 6226 goto out; 6227 6228 if ((hba->saved_err & DEVICE_FATAL_ERROR) || 6229 ((hba->saved_err & UIC_ERROR) && 6230 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) 6231 goto out; 6232 6233 if ((hba->saved_err & UIC_ERROR) && 6234 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { 6235 int err; 6236 /* 6237 * wait for 50ms to see if we can get any other errors or not. 6238 */ 6239 spin_unlock_irqrestore(hba->host->host_lock, flags); 6240 msleep(50); 6241 spin_lock_irqsave(hba->host->host_lock, flags); 6242 6243 /* 6244 * now check if we have got any other severe errors other than 6245 * DL NAC error? 6246 */ 6247 if ((hba->saved_err & INT_FATAL_ERRORS) || 6248 ((hba->saved_err & UIC_ERROR) && 6249 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) 6250 goto out; 6251 6252 /* 6253 * As DL NAC is the only error received so far, send out NOP 6254 * command to confirm if link is still active or not. 6255 * - If we don't get any response then do error recovery. 6256 * - If we get response then clear the DL NAC error bit. 6257 */ 6258 6259 spin_unlock_irqrestore(hba->host->host_lock, flags); 6260 err = ufshcd_verify_dev_init(hba); 6261 spin_lock_irqsave(hba->host->host_lock, flags); 6262 6263 if (err) 6264 goto out; 6265 6266 /* Link seems to be alive hence ignore the DL NAC errors */ 6267 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) 6268 hba->saved_err &= ~UIC_ERROR; 6269 /* clear NAC error */ 6270 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; 6271 if (!hba->saved_uic_err) 6272 err_handling = false; 6273 } 6274 out: 6275 spin_unlock_irqrestore(hba->host->host_lock, flags); 6276 return err_handling; 6277 } 6278 6279 /* host lock must be held before calling this func */ 6280 static inline bool ufshcd_is_saved_err_fatal(struct ufs_hba *hba) 6281 { 6282 return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) || 6283 (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)); 6284 } 6285 6286 void ufshcd_schedule_eh_work(struct ufs_hba *hba) 6287 { 6288 lockdep_assert_held(hba->host->host_lock); 6289 6290 /* handle fatal errors only when link is not in error state */ 6291 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) { 6292 if (hba->force_reset || ufshcd_is_link_broken(hba) || 6293 ufshcd_is_saved_err_fatal(hba)) 6294 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL; 6295 else 6296 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL; 6297 queue_work(hba->eh_wq, &hba->eh_work); 6298 } 6299 } 6300 6301 static void ufshcd_force_error_recovery(struct ufs_hba *hba) 6302 { 6303 spin_lock_irq(hba->host->host_lock); 6304 hba->force_reset = true; 6305 ufshcd_schedule_eh_work(hba); 6306 spin_unlock_irq(hba->host->host_lock); 6307 } 6308 6309 static void ufshcd_clk_scaling_allow(struct ufs_hba *hba, bool allow) 6310 { 6311 mutex_lock(&hba->wb_mutex); 6312 down_write(&hba->clk_scaling_lock); 6313 hba->clk_scaling.is_allowed = allow; 6314 up_write(&hba->clk_scaling_lock); 6315 mutex_unlock(&hba->wb_mutex); 6316 } 6317 6318 static void ufshcd_clk_scaling_suspend(struct ufs_hba *hba, bool suspend) 6319 { 6320 if (suspend) { 6321 if (hba->clk_scaling.is_enabled) 6322 ufshcd_suspend_clkscaling(hba); 6323 ufshcd_clk_scaling_allow(hba, false); 6324 } else { 6325 ufshcd_clk_scaling_allow(hba, true); 6326 if (hba->clk_scaling.is_enabled) 6327 ufshcd_resume_clkscaling(hba); 6328 } 6329 } 6330 6331 static void ufshcd_err_handling_prepare(struct ufs_hba *hba) 6332 { 6333 ufshcd_rpm_get_sync(hba); 6334 if (pm_runtime_status_suspended(&hba->ufs_device_wlun->sdev_gendev) || 6335 hba->is_sys_suspended) { 6336 enum ufs_pm_op pm_op; 6337 6338 /* 6339 * Don't assume anything of resume, if 6340 * resume fails, irq and clocks can be OFF, and powers 6341 * can be OFF or in LPM. 6342 */ 6343 ufshcd_setup_hba_vreg(hba, true); 6344 ufshcd_enable_irq(hba); 6345 ufshcd_setup_vreg(hba, true); 6346 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); 6347 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); 6348 ufshcd_hold(hba); 6349 if (!ufshcd_is_clkgating_allowed(hba)) 6350 ufshcd_setup_clocks(hba, true); 6351 pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM; 6352 ufshcd_vops_resume(hba, pm_op); 6353 } else { 6354 ufshcd_hold(hba); 6355 if (ufshcd_is_clkscaling_supported(hba) && 6356 hba->clk_scaling.is_enabled) 6357 ufshcd_suspend_clkscaling(hba); 6358 ufshcd_clk_scaling_allow(hba, false); 6359 } 6360 ufshcd_scsi_block_requests(hba); 6361 /* Wait for ongoing ufshcd_queuecommand() calls to finish. */ 6362 blk_mq_wait_quiesce_done(&hba->host->tag_set); 6363 cancel_work_sync(&hba->eeh_work); 6364 } 6365 6366 static void ufshcd_err_handling_unprepare(struct ufs_hba *hba) 6367 { 6368 ufshcd_scsi_unblock_requests(hba); 6369 ufshcd_release(hba); 6370 if (ufshcd_is_clkscaling_supported(hba)) 6371 ufshcd_clk_scaling_suspend(hba, false); 6372 ufshcd_rpm_put(hba); 6373 } 6374 6375 static inline bool ufshcd_err_handling_should_stop(struct ufs_hba *hba) 6376 { 6377 return (!hba->is_powered || hba->shutting_down || 6378 !hba->ufs_device_wlun || 6379 hba->ufshcd_state == UFSHCD_STATE_ERROR || 6380 (!(hba->saved_err || hba->saved_uic_err || hba->force_reset || 6381 ufshcd_is_link_broken(hba)))); 6382 } 6383 6384 #ifdef CONFIG_PM 6385 static void ufshcd_recover_pm_error(struct ufs_hba *hba) 6386 { 6387 struct Scsi_Host *shost = hba->host; 6388 struct scsi_device *sdev; 6389 struct request_queue *q; 6390 int ret; 6391 6392 hba->is_sys_suspended = false; 6393 /* 6394 * Set RPM status of wlun device to RPM_ACTIVE, 6395 * this also clears its runtime error. 6396 */ 6397 ret = pm_runtime_set_active(&hba->ufs_device_wlun->sdev_gendev); 6398 6399 /* hba device might have a runtime error otherwise */ 6400 if (ret) 6401 ret = pm_runtime_set_active(hba->dev); 6402 /* 6403 * If wlun device had runtime error, we also need to resume those 6404 * consumer scsi devices in case any of them has failed to be 6405 * resumed due to supplier runtime resume failure. This is to unblock 6406 * blk_queue_enter in case there are bios waiting inside it. 6407 */ 6408 if (!ret) { 6409 shost_for_each_device(sdev, shost) { 6410 q = sdev->request_queue; 6411 if (q->dev && (q->rpm_status == RPM_SUSPENDED || 6412 q->rpm_status == RPM_SUSPENDING)) 6413 pm_request_resume(q->dev); 6414 } 6415 } 6416 } 6417 #else 6418 static inline void ufshcd_recover_pm_error(struct ufs_hba *hba) 6419 { 6420 } 6421 #endif 6422 6423 static bool ufshcd_is_pwr_mode_restore_needed(struct ufs_hba *hba) 6424 { 6425 struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info; 6426 u32 mode; 6427 6428 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode); 6429 6430 if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK)) 6431 return true; 6432 6433 if (pwr_info->pwr_tx != (mode & PWRMODE_MASK)) 6434 return true; 6435 6436 return false; 6437 } 6438 6439 static bool ufshcd_abort_one(struct request *rq, void *priv) 6440 { 6441 int *ret = priv; 6442 u32 tag = rq->tag; 6443 struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq); 6444 struct scsi_device *sdev = cmd->device; 6445 struct Scsi_Host *shost = sdev->host; 6446 struct ufs_hba *hba = shost_priv(shost); 6447 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; 6448 struct ufs_hw_queue *hwq; 6449 unsigned long flags; 6450 6451 *ret = ufshcd_try_to_abort_task(hba, tag); 6452 dev_err(hba->dev, "Aborting tag %d / CDB %#02x %s\n", tag, 6453 hba->lrb[tag].cmd ? hba->lrb[tag].cmd->cmnd[0] : -1, 6454 *ret ? "failed" : "succeeded"); 6455 6456 /* Release cmd in MCQ mode if abort succeeds */ 6457 if (is_mcq_enabled(hba) && (*ret == 0)) { 6458 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(lrbp->cmd)); 6459 spin_lock_irqsave(&hwq->cq_lock, flags); 6460 if (ufshcd_cmd_inflight(lrbp->cmd)) 6461 ufshcd_release_scsi_cmd(hba, lrbp); 6462 spin_unlock_irqrestore(&hwq->cq_lock, flags); 6463 } 6464 6465 return *ret == 0; 6466 } 6467 6468 /** 6469 * ufshcd_abort_all - Abort all pending commands. 6470 * @hba: Host bus adapter pointer. 6471 * 6472 * Return: true if and only if the host controller needs to be reset. 6473 */ 6474 static bool ufshcd_abort_all(struct ufs_hba *hba) 6475 { 6476 int tag, ret = 0; 6477 6478 blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_abort_one, &ret); 6479 if (ret) 6480 goto out; 6481 6482 /* Clear pending task management requests */ 6483 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { 6484 ret = ufshcd_clear_tm_cmd(hba, tag); 6485 if (ret) 6486 goto out; 6487 } 6488 6489 out: 6490 /* Complete the requests that are cleared by s/w */ 6491 ufshcd_complete_requests(hba, false); 6492 6493 return ret != 0; 6494 } 6495 6496 /** 6497 * ufshcd_err_handler - handle UFS errors that require s/w attention 6498 * @work: pointer to work structure 6499 */ 6500 static void ufshcd_err_handler(struct work_struct *work) 6501 { 6502 int retries = MAX_ERR_HANDLER_RETRIES; 6503 struct ufs_hba *hba; 6504 unsigned long flags; 6505 bool needs_restore; 6506 bool needs_reset; 6507 int pmc_err; 6508 6509 hba = container_of(work, struct ufs_hba, eh_work); 6510 6511 dev_info(hba->dev, 6512 "%s started; HBA state %s; powered %d; shutting down %d; saved_err = %d; saved_uic_err = %d; force_reset = %d%s\n", 6513 __func__, ufshcd_state_name[hba->ufshcd_state], 6514 hba->is_powered, hba->shutting_down, hba->saved_err, 6515 hba->saved_uic_err, hba->force_reset, 6516 ufshcd_is_link_broken(hba) ? "; link is broken" : ""); 6517 6518 down(&hba->host_sem); 6519 spin_lock_irqsave(hba->host->host_lock, flags); 6520 if (ufshcd_err_handling_should_stop(hba)) { 6521 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) 6522 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; 6523 spin_unlock_irqrestore(hba->host->host_lock, flags); 6524 up(&hba->host_sem); 6525 return; 6526 } 6527 ufshcd_set_eh_in_progress(hba); 6528 spin_unlock_irqrestore(hba->host->host_lock, flags); 6529 ufshcd_err_handling_prepare(hba); 6530 /* Complete requests that have door-bell cleared by h/w */ 6531 ufshcd_complete_requests(hba, false); 6532 spin_lock_irqsave(hba->host->host_lock, flags); 6533 again: 6534 needs_restore = false; 6535 needs_reset = false; 6536 6537 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) 6538 hba->ufshcd_state = UFSHCD_STATE_RESET; 6539 /* 6540 * A full reset and restore might have happened after preparation 6541 * is finished, double check whether we should stop. 6542 */ 6543 if (ufshcd_err_handling_should_stop(hba)) 6544 goto skip_err_handling; 6545 6546 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { 6547 bool ret; 6548 6549 spin_unlock_irqrestore(hba->host->host_lock, flags); 6550 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */ 6551 ret = ufshcd_quirk_dl_nac_errors(hba); 6552 spin_lock_irqsave(hba->host->host_lock, flags); 6553 if (!ret && ufshcd_err_handling_should_stop(hba)) 6554 goto skip_err_handling; 6555 } 6556 6557 if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) || 6558 (hba->saved_uic_err && 6559 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) { 6560 bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR); 6561 6562 spin_unlock_irqrestore(hba->host->host_lock, flags); 6563 ufshcd_print_host_state(hba); 6564 ufshcd_print_pwr_info(hba); 6565 ufshcd_print_evt_hist(hba); 6566 ufshcd_print_tmrs(hba, hba->outstanding_tasks); 6567 ufshcd_print_trs_all(hba, pr_prdt); 6568 spin_lock_irqsave(hba->host->host_lock, flags); 6569 } 6570 6571 /* 6572 * if host reset is required then skip clearing the pending 6573 * transfers forcefully because they will get cleared during 6574 * host reset and restore 6575 */ 6576 if (hba->force_reset || ufshcd_is_link_broken(hba) || 6577 ufshcd_is_saved_err_fatal(hba) || 6578 ((hba->saved_err & UIC_ERROR) && 6579 (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | 6580 UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))) { 6581 needs_reset = true; 6582 goto do_reset; 6583 } 6584 6585 /* 6586 * If LINERESET was caught, UFS might have been put to PWM mode, 6587 * check if power mode restore is needed. 6588 */ 6589 if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) { 6590 hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR; 6591 if (!hba->saved_uic_err) 6592 hba->saved_err &= ~UIC_ERROR; 6593 spin_unlock_irqrestore(hba->host->host_lock, flags); 6594 if (ufshcd_is_pwr_mode_restore_needed(hba)) 6595 needs_restore = true; 6596 spin_lock_irqsave(hba->host->host_lock, flags); 6597 if (!hba->saved_err && !needs_restore) 6598 goto skip_err_handling; 6599 } 6600 6601 hba->silence_err_logs = true; 6602 /* release lock as clear command might sleep */ 6603 spin_unlock_irqrestore(hba->host->host_lock, flags); 6604 6605 needs_reset = ufshcd_abort_all(hba); 6606 6607 spin_lock_irqsave(hba->host->host_lock, flags); 6608 hba->silence_err_logs = false; 6609 if (needs_reset) 6610 goto do_reset; 6611 6612 /* 6613 * After all reqs and tasks are cleared from doorbell, 6614 * now it is safe to retore power mode. 6615 */ 6616 if (needs_restore) { 6617 spin_unlock_irqrestore(hba->host->host_lock, flags); 6618 /* 6619 * Hold the scaling lock just in case dev cmds 6620 * are sent via bsg and/or sysfs. 6621 */ 6622 down_write(&hba->clk_scaling_lock); 6623 hba->force_pmc = true; 6624 pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info)); 6625 if (pmc_err) { 6626 needs_reset = true; 6627 dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n", 6628 __func__, pmc_err); 6629 } 6630 hba->force_pmc = false; 6631 ufshcd_print_pwr_info(hba); 6632 up_write(&hba->clk_scaling_lock); 6633 spin_lock_irqsave(hba->host->host_lock, flags); 6634 } 6635 6636 do_reset: 6637 /* Fatal errors need reset */ 6638 if (needs_reset) { 6639 int err; 6640 6641 hba->force_reset = false; 6642 spin_unlock_irqrestore(hba->host->host_lock, flags); 6643 err = ufshcd_reset_and_restore(hba); 6644 if (err) 6645 dev_err(hba->dev, "%s: reset and restore failed with err %d\n", 6646 __func__, err); 6647 else 6648 ufshcd_recover_pm_error(hba); 6649 spin_lock_irqsave(hba->host->host_lock, flags); 6650 } 6651 6652 skip_err_handling: 6653 if (!needs_reset) { 6654 if (hba->ufshcd_state == UFSHCD_STATE_RESET) 6655 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; 6656 if (hba->saved_err || hba->saved_uic_err) 6657 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", 6658 __func__, hba->saved_err, hba->saved_uic_err); 6659 } 6660 /* Exit in an operational state or dead */ 6661 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL && 6662 hba->ufshcd_state != UFSHCD_STATE_ERROR) { 6663 if (--retries) 6664 goto again; 6665 hba->ufshcd_state = UFSHCD_STATE_ERROR; 6666 } 6667 ufshcd_clear_eh_in_progress(hba); 6668 spin_unlock_irqrestore(hba->host->host_lock, flags); 6669 ufshcd_err_handling_unprepare(hba); 6670 up(&hba->host_sem); 6671 6672 dev_info(hba->dev, "%s finished; HBA state %s\n", __func__, 6673 ufshcd_state_name[hba->ufshcd_state]); 6674 } 6675 6676 /** 6677 * ufshcd_update_uic_error - check and set fatal UIC error flags. 6678 * @hba: per-adapter instance 6679 * 6680 * Return: 6681 * IRQ_HANDLED - If interrupt is valid 6682 * IRQ_NONE - If invalid interrupt 6683 */ 6684 static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba) 6685 { 6686 u32 reg; 6687 irqreturn_t retval = IRQ_NONE; 6688 6689 /* PHY layer error */ 6690 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); 6691 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) && 6692 (reg & UIC_PHY_ADAPTER_LAYER_ERROR_CODE_MASK)) { 6693 ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg); 6694 /* 6695 * To know whether this error is fatal or not, DB timeout 6696 * must be checked but this error is handled separately. 6697 */ 6698 if (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK) 6699 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", 6700 __func__); 6701 6702 /* Got a LINERESET indication. */ 6703 if (reg & UIC_PHY_ADAPTER_LAYER_GENERIC_ERROR) { 6704 struct uic_command *cmd = NULL; 6705 6706 hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR; 6707 if (hba->uic_async_done && hba->active_uic_cmd) 6708 cmd = hba->active_uic_cmd; 6709 /* 6710 * Ignore the LINERESET during power mode change 6711 * operation via DME_SET command. 6712 */ 6713 if (cmd && (cmd->command == UIC_CMD_DME_SET)) 6714 hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR; 6715 } 6716 retval |= IRQ_HANDLED; 6717 } 6718 6719 /* PA_INIT_ERROR is fatal and needs UIC reset */ 6720 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); 6721 if ((reg & UIC_DATA_LINK_LAYER_ERROR) && 6722 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) { 6723 ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg); 6724 6725 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) 6726 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; 6727 else if (hba->dev_quirks & 6728 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) { 6729 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED) 6730 hba->uic_error |= 6731 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; 6732 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT) 6733 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; 6734 } 6735 retval |= IRQ_HANDLED; 6736 } 6737 6738 /* UIC NL/TL/DME errors needs software retry */ 6739 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); 6740 if ((reg & UIC_NETWORK_LAYER_ERROR) && 6741 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) { 6742 ufshcd_update_evt_hist(hba, UFS_EVT_NL_ERR, reg); 6743 hba->uic_error |= UFSHCD_UIC_NL_ERROR; 6744 retval |= IRQ_HANDLED; 6745 } 6746 6747 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); 6748 if ((reg & UIC_TRANSPORT_LAYER_ERROR) && 6749 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) { 6750 ufshcd_update_evt_hist(hba, UFS_EVT_TL_ERR, reg); 6751 hba->uic_error |= UFSHCD_UIC_TL_ERROR; 6752 retval |= IRQ_HANDLED; 6753 } 6754 6755 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); 6756 if ((reg & UIC_DME_ERROR) && 6757 (reg & UIC_DME_ERROR_CODE_MASK)) { 6758 ufshcd_update_evt_hist(hba, UFS_EVT_DME_ERR, reg); 6759 hba->uic_error |= UFSHCD_UIC_DME_ERROR; 6760 retval |= IRQ_HANDLED; 6761 } 6762 6763 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", 6764 __func__, hba->uic_error); 6765 return retval; 6766 } 6767 6768 /** 6769 * ufshcd_check_errors - Check for errors that need s/w attention 6770 * @hba: per-adapter instance 6771 * @intr_status: interrupt status generated by the controller 6772 * 6773 * Return: 6774 * IRQ_HANDLED - If interrupt is valid 6775 * IRQ_NONE - If invalid interrupt 6776 */ 6777 static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba, u32 intr_status) 6778 { 6779 bool queue_eh_work = false; 6780 irqreturn_t retval = IRQ_NONE; 6781 6782 spin_lock(hba->host->host_lock); 6783 hba->errors |= UFSHCD_ERROR_MASK & intr_status; 6784 6785 if (hba->errors & INT_FATAL_ERRORS) { 6786 ufshcd_update_evt_hist(hba, UFS_EVT_FATAL_ERR, 6787 hba->errors); 6788 queue_eh_work = true; 6789 } 6790 6791 if (hba->errors & UIC_ERROR) { 6792 hba->uic_error = 0; 6793 retval = ufshcd_update_uic_error(hba); 6794 if (hba->uic_error) 6795 queue_eh_work = true; 6796 } 6797 6798 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) { 6799 dev_err(hba->dev, 6800 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n", 6801 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ? 6802 "Enter" : "Exit", 6803 hba->errors, ufshcd_get_upmcrs(hba)); 6804 ufshcd_update_evt_hist(hba, UFS_EVT_AUTO_HIBERN8_ERR, 6805 hba->errors); 6806 ufshcd_set_link_broken(hba); 6807 queue_eh_work = true; 6808 } 6809 6810 if (queue_eh_work) { 6811 /* 6812 * update the transfer error masks to sticky bits, let's do this 6813 * irrespective of current ufshcd_state. 6814 */ 6815 hba->saved_err |= hba->errors; 6816 hba->saved_uic_err |= hba->uic_error; 6817 6818 /* dump controller state before resetting */ 6819 if ((hba->saved_err & 6820 (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) || 6821 (hba->saved_uic_err && 6822 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) { 6823 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n", 6824 __func__, hba->saved_err, 6825 hba->saved_uic_err); 6826 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, 6827 "host_regs: "); 6828 ufshcd_print_pwr_info(hba); 6829 } 6830 ufshcd_schedule_eh_work(hba); 6831 retval |= IRQ_HANDLED; 6832 } 6833 /* 6834 * if (!queue_eh_work) - 6835 * Other errors are either non-fatal where host recovers 6836 * itself without s/w intervention or errors that will be 6837 * handled by the SCSI core layer. 6838 */ 6839 hba->errors = 0; 6840 hba->uic_error = 0; 6841 spin_unlock(hba->host->host_lock); 6842 return retval; 6843 } 6844 6845 /** 6846 * ufshcd_tmc_handler - handle task management function completion 6847 * @hba: per adapter instance 6848 * 6849 * Return: 6850 * IRQ_HANDLED - If interrupt is valid 6851 * IRQ_NONE - If invalid interrupt 6852 */ 6853 static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba) 6854 { 6855 unsigned long flags, pending, issued; 6856 irqreturn_t ret = IRQ_NONE; 6857 int tag; 6858 6859 spin_lock_irqsave(hba->host->host_lock, flags); 6860 pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); 6861 issued = hba->outstanding_tasks & ~pending; 6862 for_each_set_bit(tag, &issued, hba->nutmrs) { 6863 struct request *req = hba->tmf_rqs[tag]; 6864 struct completion *c = req->end_io_data; 6865 6866 complete(c); 6867 ret = IRQ_HANDLED; 6868 } 6869 spin_unlock_irqrestore(hba->host->host_lock, flags); 6870 6871 return ret; 6872 } 6873 6874 /** 6875 * ufshcd_handle_mcq_cq_events - handle MCQ completion queue events 6876 * @hba: per adapter instance 6877 * 6878 * Return: IRQ_HANDLED if interrupt is handled. 6879 */ 6880 static irqreturn_t ufshcd_handle_mcq_cq_events(struct ufs_hba *hba) 6881 { 6882 struct ufs_hw_queue *hwq; 6883 unsigned long outstanding_cqs; 6884 unsigned int nr_queues; 6885 int i, ret; 6886 u32 events; 6887 6888 ret = ufshcd_vops_get_outstanding_cqs(hba, &outstanding_cqs); 6889 if (ret) 6890 outstanding_cqs = (1U << hba->nr_hw_queues) - 1; 6891 6892 /* Exclude the poll queues */ 6893 nr_queues = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL]; 6894 for_each_set_bit(i, &outstanding_cqs, nr_queues) { 6895 hwq = &hba->uhq[i]; 6896 6897 events = ufshcd_mcq_read_cqis(hba, i); 6898 if (events) 6899 ufshcd_mcq_write_cqis(hba, events, i); 6900 6901 if (events & UFSHCD_MCQ_CQIS_TAIL_ENT_PUSH_STS) 6902 ufshcd_mcq_poll_cqe_lock(hba, hwq); 6903 } 6904 6905 return IRQ_HANDLED; 6906 } 6907 6908 /** 6909 * ufshcd_sl_intr - Interrupt service routine 6910 * @hba: per adapter instance 6911 * @intr_status: contains interrupts generated by the controller 6912 * 6913 * Return: 6914 * IRQ_HANDLED - If interrupt is valid 6915 * IRQ_NONE - If invalid interrupt 6916 */ 6917 static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) 6918 { 6919 irqreturn_t retval = IRQ_NONE; 6920 6921 if (intr_status & UFSHCD_UIC_MASK) 6922 retval |= ufshcd_uic_cmd_compl(hba, intr_status); 6923 6924 if (intr_status & UFSHCD_ERROR_MASK || hba->errors) 6925 retval |= ufshcd_check_errors(hba, intr_status); 6926 6927 if (intr_status & UTP_TASK_REQ_COMPL) 6928 retval |= ufshcd_tmc_handler(hba); 6929 6930 if (intr_status & UTP_TRANSFER_REQ_COMPL) 6931 retval |= ufshcd_transfer_req_compl(hba); 6932 6933 if (intr_status & MCQ_CQ_EVENT_STATUS) 6934 retval |= ufshcd_handle_mcq_cq_events(hba); 6935 6936 return retval; 6937 } 6938 6939 /** 6940 * ufshcd_intr - Main interrupt service routine 6941 * @irq: irq number 6942 * @__hba: pointer to adapter instance 6943 * 6944 * Return: 6945 * IRQ_HANDLED - If interrupt is valid 6946 * IRQ_NONE - If invalid interrupt 6947 */ 6948 static irqreturn_t ufshcd_intr(int irq, void *__hba) 6949 { 6950 u32 intr_status, enabled_intr_status = 0; 6951 irqreturn_t retval = IRQ_NONE; 6952 struct ufs_hba *hba = __hba; 6953 int retries = hba->nutrs; 6954 6955 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); 6956 hba->ufs_stats.last_intr_status = intr_status; 6957 hba->ufs_stats.last_intr_ts = local_clock(); 6958 6959 /* 6960 * There could be max of hba->nutrs reqs in flight and in worst case 6961 * if the reqs get finished 1 by 1 after the interrupt status is 6962 * read, make sure we handle them by checking the interrupt status 6963 * again in a loop until we process all of the reqs before returning. 6964 */ 6965 while (intr_status && retries--) { 6966 enabled_intr_status = 6967 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); 6968 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); 6969 if (enabled_intr_status) 6970 retval |= ufshcd_sl_intr(hba, enabled_intr_status); 6971 6972 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); 6973 } 6974 6975 if (enabled_intr_status && retval == IRQ_NONE && 6976 (!(enabled_intr_status & UTP_TRANSFER_REQ_COMPL) || 6977 hba->outstanding_reqs) && !ufshcd_eh_in_progress(hba)) { 6978 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x (0x%08x, 0x%08x)\n", 6979 __func__, 6980 intr_status, 6981 hba->ufs_stats.last_intr_status, 6982 enabled_intr_status); 6983 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); 6984 } 6985 6986 return retval; 6987 } 6988 6989 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) 6990 { 6991 int err = 0; 6992 u32 mask = 1 << tag; 6993 unsigned long flags; 6994 6995 if (!test_bit(tag, &hba->outstanding_tasks)) 6996 goto out; 6997 6998 spin_lock_irqsave(hba->host->host_lock, flags); 6999 ufshcd_utmrl_clear(hba, tag); 7000 spin_unlock_irqrestore(hba->host->host_lock, flags); 7001 7002 /* poll for max. 1 sec to clear door bell register by h/w */ 7003 err = ufshcd_wait_for_register(hba, 7004 REG_UTP_TASK_REQ_DOOR_BELL, 7005 mask, 0, 1000, 1000); 7006 7007 dev_err(hba->dev, "Clearing task management function with tag %d %s\n", 7008 tag, err < 0 ? "failed" : "succeeded"); 7009 7010 out: 7011 return err; 7012 } 7013 7014 static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, 7015 struct utp_task_req_desc *treq, u8 tm_function) 7016 { 7017 struct request_queue *q = hba->tmf_queue; 7018 struct Scsi_Host *host = hba->host; 7019 DECLARE_COMPLETION_ONSTACK(wait); 7020 struct request *req; 7021 unsigned long flags; 7022 int task_tag, err; 7023 7024 /* 7025 * blk_mq_alloc_request() is used here only to get a free tag. 7026 */ 7027 req = blk_mq_alloc_request(q, REQ_OP_DRV_OUT, 0); 7028 if (IS_ERR(req)) 7029 return PTR_ERR(req); 7030 7031 req->end_io_data = &wait; 7032 ufshcd_hold(hba); 7033 7034 spin_lock_irqsave(host->host_lock, flags); 7035 7036 task_tag = req->tag; 7037 hba->tmf_rqs[req->tag] = req; 7038 treq->upiu_req.req_header.task_tag = task_tag; 7039 7040 memcpy(hba->utmrdl_base_addr + task_tag, treq, sizeof(*treq)); 7041 ufshcd_vops_setup_task_mgmt(hba, task_tag, tm_function); 7042 7043 /* send command to the controller */ 7044 __set_bit(task_tag, &hba->outstanding_tasks); 7045 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TASK_REQ_DOOR_BELL); 7046 7047 spin_unlock_irqrestore(host->host_lock, flags); 7048 7049 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_SEND); 7050 7051 /* wait until the task management command is completed */ 7052 err = wait_for_completion_io_timeout(&wait, 7053 msecs_to_jiffies(TM_CMD_TIMEOUT)); 7054 if (!err) { 7055 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR); 7056 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", 7057 __func__, tm_function); 7058 if (ufshcd_clear_tm_cmd(hba, task_tag)) 7059 dev_WARN(hba->dev, "%s: unable to clear tm cmd (slot %d) after timeout\n", 7060 __func__, task_tag); 7061 err = -ETIMEDOUT; 7062 } else { 7063 err = 0; 7064 memcpy(treq, hba->utmrdl_base_addr + task_tag, sizeof(*treq)); 7065 7066 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_COMP); 7067 } 7068 7069 spin_lock_irqsave(hba->host->host_lock, flags); 7070 hba->tmf_rqs[req->tag] = NULL; 7071 __clear_bit(task_tag, &hba->outstanding_tasks); 7072 spin_unlock_irqrestore(hba->host->host_lock, flags); 7073 7074 ufshcd_release(hba); 7075 blk_mq_free_request(req); 7076 7077 return err; 7078 } 7079 7080 /** 7081 * ufshcd_issue_tm_cmd - issues task management commands to controller 7082 * @hba: per adapter instance 7083 * @lun_id: LUN ID to which TM command is sent 7084 * @task_id: task ID to which the TM command is applicable 7085 * @tm_function: task management function opcode 7086 * @tm_response: task management service response return value 7087 * 7088 * Return: non-zero value on error, zero on success. 7089 */ 7090 static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, 7091 u8 tm_function, u8 *tm_response) 7092 { 7093 struct utp_task_req_desc treq = { }; 7094 enum utp_ocs ocs_value; 7095 int err; 7096 7097 /* Configure task request descriptor */ 7098 treq.header.interrupt = 1; 7099 treq.header.ocs = OCS_INVALID_COMMAND_STATUS; 7100 7101 /* Configure task request UPIU */ 7102 treq.upiu_req.req_header.transaction_code = UPIU_TRANSACTION_TASK_REQ; 7103 treq.upiu_req.req_header.lun = lun_id; 7104 treq.upiu_req.req_header.tm_function = tm_function; 7105 7106 /* 7107 * The host shall provide the same value for LUN field in the basic 7108 * header and for Input Parameter. 7109 */ 7110 treq.upiu_req.input_param1 = cpu_to_be32(lun_id); 7111 treq.upiu_req.input_param2 = cpu_to_be32(task_id); 7112 7113 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function); 7114 if (err == -ETIMEDOUT) 7115 return err; 7116 7117 ocs_value = treq.header.ocs & MASK_OCS; 7118 if (ocs_value != OCS_SUCCESS) 7119 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", 7120 __func__, ocs_value); 7121 else if (tm_response) 7122 *tm_response = be32_to_cpu(treq.upiu_rsp.output_param1) & 7123 MASK_TM_SERVICE_RESP; 7124 return err; 7125 } 7126 7127 /** 7128 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests 7129 * @hba: per-adapter instance 7130 * @req_upiu: upiu request 7131 * @rsp_upiu: upiu reply 7132 * @desc_buff: pointer to descriptor buffer, NULL if NA 7133 * @buff_len: descriptor size, 0 if NA 7134 * @cmd_type: specifies the type (NOP, Query...) 7135 * @desc_op: descriptor operation 7136 * 7137 * Those type of requests uses UTP Transfer Request Descriptor - utrd. 7138 * Therefore, it "rides" the device management infrastructure: uses its tag and 7139 * tasks work queues. 7140 * 7141 * Since there is only one available tag for device management commands, 7142 * the caller is expected to hold the hba->dev_cmd.lock mutex. 7143 * 7144 * Return: 0 upon success; < 0 upon failure. 7145 */ 7146 static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, 7147 struct utp_upiu_req *req_upiu, 7148 struct utp_upiu_req *rsp_upiu, 7149 u8 *desc_buff, int *buff_len, 7150 enum dev_cmd_type cmd_type, 7151 enum query_opcode desc_op) 7152 { 7153 const u32 tag = hba->reserved_slot; 7154 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; 7155 int err = 0; 7156 u8 upiu_flags; 7157 7158 /* Protects use of hba->reserved_slot. */ 7159 lockdep_assert_held(&hba->dev_cmd.lock); 7160 7161 ufshcd_setup_dev_cmd(hba, lrbp, cmd_type, 0, tag); 7162 7163 ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, DMA_NONE, 0); 7164 7165 /* update the task tag in the request upiu */ 7166 req_upiu->header.task_tag = tag; 7167 7168 /* just copy the upiu request as it is */ 7169 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); 7170 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) { 7171 /* The Data Segment Area is optional depending upon the query 7172 * function value. for WRITE DESCRIPTOR, the data segment 7173 * follows right after the tsf. 7174 */ 7175 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len); 7176 *buff_len = 0; 7177 } 7178 7179 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); 7180 7181 /* 7182 * ignore the returning value here - ufshcd_check_query_response is 7183 * bound to fail since dev_cmd.query and dev_cmd.type were left empty. 7184 * read the response directly ignoring all errors. 7185 */ 7186 ufshcd_issue_dev_cmd(hba, lrbp, tag, QUERY_REQ_TIMEOUT); 7187 7188 /* just copy the upiu response as it is */ 7189 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); 7190 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) { 7191 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu); 7192 u16 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header 7193 .data_segment_length); 7194 7195 if (*buff_len >= resp_len) { 7196 memcpy(desc_buff, descp, resp_len); 7197 *buff_len = resp_len; 7198 } else { 7199 dev_warn(hba->dev, 7200 "%s: rsp size %d is bigger than buffer size %d", 7201 __func__, resp_len, *buff_len); 7202 *buff_len = 0; 7203 err = -EINVAL; 7204 } 7205 } 7206 ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP, 7207 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr); 7208 7209 return err; 7210 } 7211 7212 /** 7213 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands 7214 * @hba: per-adapter instance 7215 * @req_upiu: upiu request 7216 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands 7217 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target 7218 * @desc_buff: pointer to descriptor buffer, NULL if NA 7219 * @buff_len: descriptor size, 0 if NA 7220 * @desc_op: descriptor operation 7221 * 7222 * Supports UTP Transfer requests (nop and query), and UTP Task 7223 * Management requests. 7224 * It is up to the caller to fill the upiu conent properly, as it will 7225 * be copied without any further input validations. 7226 * 7227 * Return: 0 upon success; < 0 upon failure. 7228 */ 7229 int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, 7230 struct utp_upiu_req *req_upiu, 7231 struct utp_upiu_req *rsp_upiu, 7232 enum upiu_request_transaction msgcode, 7233 u8 *desc_buff, int *buff_len, 7234 enum query_opcode desc_op) 7235 { 7236 int err; 7237 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY; 7238 struct utp_task_req_desc treq = { }; 7239 enum utp_ocs ocs_value; 7240 u8 tm_f = req_upiu->header.tm_function; 7241 7242 switch (msgcode) { 7243 case UPIU_TRANSACTION_NOP_OUT: 7244 cmd_type = DEV_CMD_TYPE_NOP; 7245 fallthrough; 7246 case UPIU_TRANSACTION_QUERY_REQ: 7247 ufshcd_dev_man_lock(hba); 7248 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu, 7249 desc_buff, buff_len, 7250 cmd_type, desc_op); 7251 ufshcd_dev_man_unlock(hba); 7252 7253 break; 7254 case UPIU_TRANSACTION_TASK_REQ: 7255 treq.header.interrupt = 1; 7256 treq.header.ocs = OCS_INVALID_COMMAND_STATUS; 7257 7258 memcpy(&treq.upiu_req, req_upiu, sizeof(*req_upiu)); 7259 7260 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f); 7261 if (err == -ETIMEDOUT) 7262 break; 7263 7264 ocs_value = treq.header.ocs & MASK_OCS; 7265 if (ocs_value != OCS_SUCCESS) { 7266 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__, 7267 ocs_value); 7268 break; 7269 } 7270 7271 memcpy(rsp_upiu, &treq.upiu_rsp, sizeof(*rsp_upiu)); 7272 7273 break; 7274 default: 7275 err = -EINVAL; 7276 7277 break; 7278 } 7279 7280 return err; 7281 } 7282 7283 /** 7284 * ufshcd_advanced_rpmb_req_handler - handle advanced RPMB request 7285 * @hba: per adapter instance 7286 * @req_upiu: upiu request 7287 * @rsp_upiu: upiu reply 7288 * @req_ehs: EHS field which contains Advanced RPMB Request Message 7289 * @rsp_ehs: EHS field which returns Advanced RPMB Response Message 7290 * @sg_cnt: The number of sg lists actually used 7291 * @sg_list: Pointer to SG list when DATA IN/OUT UPIU is required in ARPMB operation 7292 * @dir: DMA direction 7293 * 7294 * Return: zero on success, non-zero on failure. 7295 */ 7296 int ufshcd_advanced_rpmb_req_handler(struct ufs_hba *hba, struct utp_upiu_req *req_upiu, 7297 struct utp_upiu_req *rsp_upiu, struct ufs_ehs *req_ehs, 7298 struct ufs_ehs *rsp_ehs, int sg_cnt, struct scatterlist *sg_list, 7299 enum dma_data_direction dir) 7300 { 7301 const u32 tag = hba->reserved_slot; 7302 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; 7303 int err = 0; 7304 int result; 7305 u8 upiu_flags; 7306 u8 *ehs_data; 7307 u16 ehs_len; 7308 int ehs = (hba->capabilities & MASK_EHSLUTRD_SUPPORTED) ? 2 : 0; 7309 7310 /* Protects use of hba->reserved_slot. */ 7311 ufshcd_dev_man_lock(hba); 7312 7313 ufshcd_setup_dev_cmd(hba, lrbp, DEV_CMD_TYPE_RPMB, UFS_UPIU_RPMB_WLUN, tag); 7314 7315 ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags, DMA_NONE, ehs); 7316 7317 /* update the task tag */ 7318 req_upiu->header.task_tag = tag; 7319 7320 /* copy the UPIU(contains CDB) request as it is */ 7321 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); 7322 /* Copy EHS, starting with byte32, immediately after the CDB package */ 7323 memcpy(lrbp->ucd_req_ptr + 1, req_ehs, sizeof(*req_ehs)); 7324 7325 if (dir != DMA_NONE && sg_list) 7326 ufshcd_sgl_to_prdt(hba, lrbp, sg_cnt, sg_list); 7327 7328 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); 7329 7330 err = ufshcd_issue_dev_cmd(hba, lrbp, tag, ADVANCED_RPMB_REQ_TIMEOUT); 7331 7332 if (!err) { 7333 /* Just copy the upiu response as it is */ 7334 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); 7335 /* Get the response UPIU result */ 7336 result = (lrbp->ucd_rsp_ptr->header.response << 8) | 7337 lrbp->ucd_rsp_ptr->header.status; 7338 7339 ehs_len = lrbp->ucd_rsp_ptr->header.ehs_length; 7340 /* 7341 * Since the bLength in EHS indicates the total size of the EHS Header and EHS Data 7342 * in 32 Byte units, the value of the bLength Request/Response for Advanced RPMB 7343 * Message is 02h 7344 */ 7345 if (ehs_len == 2 && rsp_ehs) { 7346 /* 7347 * ucd_rsp_ptr points to a buffer with a length of 512 bytes 7348 * (ALIGNED_UPIU_SIZE = 512), and the EHS data just starts from byte32 7349 */ 7350 ehs_data = (u8 *)lrbp->ucd_rsp_ptr + EHS_OFFSET_IN_RESPONSE; 7351 memcpy(rsp_ehs, ehs_data, ehs_len * 32); 7352 } 7353 } 7354 7355 ufshcd_dev_man_unlock(hba); 7356 7357 return err ? : result; 7358 } 7359 7360 /** 7361 * ufshcd_eh_device_reset_handler() - Reset a single logical unit. 7362 * @cmd: SCSI command pointer 7363 * 7364 * Return: SUCCESS or FAILED. 7365 */ 7366 static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) 7367 { 7368 unsigned long flags, pending_reqs = 0, not_cleared = 0; 7369 struct Scsi_Host *host; 7370 struct ufs_hba *hba; 7371 struct ufs_hw_queue *hwq; 7372 struct ufshcd_lrb *lrbp; 7373 u32 pos, not_cleared_mask = 0; 7374 int err; 7375 u8 resp = 0xF, lun; 7376 7377 host = cmd->device->host; 7378 hba = shost_priv(host); 7379 7380 lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); 7381 err = ufshcd_issue_tm_cmd(hba, lun, 0, UFS_LOGICAL_RESET, &resp); 7382 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { 7383 if (!err) 7384 err = resp; 7385 goto out; 7386 } 7387 7388 if (is_mcq_enabled(hba)) { 7389 for (pos = 0; pos < hba->nutrs; pos++) { 7390 lrbp = &hba->lrb[pos]; 7391 if (ufshcd_cmd_inflight(lrbp->cmd) && 7392 lrbp->lun == lun) { 7393 ufshcd_clear_cmd(hba, pos); 7394 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(lrbp->cmd)); 7395 ufshcd_mcq_poll_cqe_lock(hba, hwq); 7396 } 7397 } 7398 err = 0; 7399 goto out; 7400 } 7401 7402 /* clear the commands that were pending for corresponding LUN */ 7403 spin_lock_irqsave(&hba->outstanding_lock, flags); 7404 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) 7405 if (hba->lrb[pos].lun == lun) 7406 __set_bit(pos, &pending_reqs); 7407 hba->outstanding_reqs &= ~pending_reqs; 7408 spin_unlock_irqrestore(&hba->outstanding_lock, flags); 7409 7410 for_each_set_bit(pos, &pending_reqs, hba->nutrs) { 7411 if (ufshcd_clear_cmd(hba, pos) < 0) { 7412 spin_lock_irqsave(&hba->outstanding_lock, flags); 7413 not_cleared = 1U << pos & 7414 ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); 7415 hba->outstanding_reqs |= not_cleared; 7416 not_cleared_mask |= not_cleared; 7417 spin_unlock_irqrestore(&hba->outstanding_lock, flags); 7418 7419 dev_err(hba->dev, "%s: failed to clear request %d\n", 7420 __func__, pos); 7421 } 7422 } 7423 __ufshcd_transfer_req_compl(hba, pending_reqs & ~not_cleared_mask); 7424 7425 out: 7426 hba->req_abort_count = 0; 7427 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, (u32)err); 7428 if (!err) { 7429 err = SUCCESS; 7430 } else { 7431 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); 7432 err = FAILED; 7433 } 7434 return err; 7435 } 7436 7437 static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) 7438 { 7439 struct ufshcd_lrb *lrbp; 7440 int tag; 7441 7442 for_each_set_bit(tag, &bitmap, hba->nutrs) { 7443 lrbp = &hba->lrb[tag]; 7444 lrbp->req_abort_skip = true; 7445 } 7446 } 7447 7448 /** 7449 * ufshcd_try_to_abort_task - abort a specific task 7450 * @hba: Pointer to adapter instance 7451 * @tag: Task tag/index to be aborted 7452 * 7453 * Abort the pending command in device by sending UFS_ABORT_TASK task management 7454 * command, and in host controller by clearing the door-bell register. There can 7455 * be race between controller sending the command to the device while abort is 7456 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is 7457 * really issued and then try to abort it. 7458 * 7459 * Return: zero on success, non-zero on failure. 7460 */ 7461 int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag) 7462 { 7463 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; 7464 int err = 0; 7465 int poll_cnt; 7466 u8 resp = 0xF; 7467 u32 reg; 7468 7469 for (poll_cnt = 100; poll_cnt; poll_cnt--) { 7470 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, 7471 UFS_QUERY_TASK, &resp); 7472 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { 7473 /* cmd pending in the device */ 7474 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n", 7475 __func__, tag); 7476 break; 7477 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { 7478 /* 7479 * cmd not pending in the device, check if it is 7480 * in transition. 7481 */ 7482 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n", 7483 __func__, tag); 7484 if (is_mcq_enabled(hba)) { 7485 /* MCQ mode */ 7486 if (ufshcd_cmd_inflight(lrbp->cmd)) { 7487 /* sleep for max. 200us same delay as in SDB mode */ 7488 usleep_range(100, 200); 7489 continue; 7490 } 7491 /* command completed already */ 7492 dev_err(hba->dev, "%s: cmd at tag=%d is cleared.\n", 7493 __func__, tag); 7494 goto out; 7495 } 7496 7497 /* Single Doorbell Mode */ 7498 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); 7499 if (reg & (1 << tag)) { 7500 /* sleep for max. 200us to stabilize */ 7501 usleep_range(100, 200); 7502 continue; 7503 } 7504 /* command completed already */ 7505 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n", 7506 __func__, tag); 7507 goto out; 7508 } else { 7509 dev_err(hba->dev, 7510 "%s: no response from device. tag = %d, err %d\n", 7511 __func__, tag, err); 7512 if (!err) 7513 err = resp; /* service response error */ 7514 goto out; 7515 } 7516 } 7517 7518 if (!poll_cnt) { 7519 err = -EBUSY; 7520 goto out; 7521 } 7522 7523 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, 7524 UFS_ABORT_TASK, &resp); 7525 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { 7526 if (!err) { 7527 err = resp; /* service response error */ 7528 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n", 7529 __func__, tag, err); 7530 } 7531 goto out; 7532 } 7533 7534 err = ufshcd_clear_cmd(hba, tag); 7535 if (err) 7536 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", 7537 __func__, tag, err); 7538 7539 out: 7540 return err; 7541 } 7542 7543 /** 7544 * ufshcd_abort - scsi host template eh_abort_handler callback 7545 * @cmd: SCSI command pointer 7546 * 7547 * Return: SUCCESS or FAILED. 7548 */ 7549 static int ufshcd_abort(struct scsi_cmnd *cmd) 7550 { 7551 struct Scsi_Host *host = cmd->device->host; 7552 struct ufs_hba *hba = shost_priv(host); 7553 int tag = scsi_cmd_to_rq(cmd)->tag; 7554 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; 7555 unsigned long flags; 7556 int err = FAILED; 7557 bool outstanding; 7558 u32 reg; 7559 7560 ufshcd_hold(hba); 7561 7562 if (!is_mcq_enabled(hba)) { 7563 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); 7564 if (!test_bit(tag, &hba->outstanding_reqs)) { 7565 /* If command is already aborted/completed, return FAILED. */ 7566 dev_err(hba->dev, 7567 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n", 7568 __func__, tag, hba->outstanding_reqs, reg); 7569 goto release; 7570 } 7571 } 7572 7573 /* Print Transfer Request of aborted task */ 7574 dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag); 7575 7576 /* 7577 * Print detailed info about aborted request. 7578 * As more than one request might get aborted at the same time, 7579 * print full information only for the first aborted request in order 7580 * to reduce repeated printouts. For other aborted requests only print 7581 * basic details. 7582 */ 7583 scsi_print_command(cmd); 7584 if (!hba->req_abort_count) { 7585 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag); 7586 ufshcd_print_evt_hist(hba); 7587 ufshcd_print_host_state(hba); 7588 ufshcd_print_pwr_info(hba); 7589 ufshcd_print_tr(hba, tag, true); 7590 } else { 7591 ufshcd_print_tr(hba, tag, false); 7592 } 7593 hba->req_abort_count++; 7594 7595 if (!is_mcq_enabled(hba) && !(reg & (1 << tag))) { 7596 /* only execute this code in single doorbell mode */ 7597 dev_err(hba->dev, 7598 "%s: cmd was completed, but without a notifying intr, tag = %d", 7599 __func__, tag); 7600 __ufshcd_transfer_req_compl(hba, 1UL << tag); 7601 goto release; 7602 } 7603 7604 /* 7605 * Task abort to the device W-LUN is illegal. When this command 7606 * will fail, due to spec violation, scsi err handling next step 7607 * will be to send LU reset which, again, is a spec violation. 7608 * To avoid these unnecessary/illegal steps, first we clean up 7609 * the lrb taken by this cmd and re-set it in outstanding_reqs, 7610 * then queue the eh_work and bail. 7611 */ 7612 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) { 7613 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun); 7614 7615 spin_lock_irqsave(host->host_lock, flags); 7616 hba->force_reset = true; 7617 ufshcd_schedule_eh_work(hba); 7618 spin_unlock_irqrestore(host->host_lock, flags); 7619 goto release; 7620 } 7621 7622 if (is_mcq_enabled(hba)) { 7623 /* MCQ mode. Branch off to handle abort for mcq mode */ 7624 err = ufshcd_mcq_abort(cmd); 7625 goto release; 7626 } 7627 7628 /* Skip task abort in case previous aborts failed and report failure */ 7629 if (lrbp->req_abort_skip) { 7630 dev_err(hba->dev, "%s: skipping abort\n", __func__); 7631 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); 7632 goto release; 7633 } 7634 7635 err = ufshcd_try_to_abort_task(hba, tag); 7636 if (err) { 7637 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); 7638 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); 7639 err = FAILED; 7640 goto release; 7641 } 7642 7643 /* 7644 * Clear the corresponding bit from outstanding_reqs since the command 7645 * has been aborted successfully. 7646 */ 7647 spin_lock_irqsave(&hba->outstanding_lock, flags); 7648 outstanding = __test_and_clear_bit(tag, &hba->outstanding_reqs); 7649 spin_unlock_irqrestore(&hba->outstanding_lock, flags); 7650 7651 if (outstanding) 7652 ufshcd_release_scsi_cmd(hba, lrbp); 7653 7654 err = SUCCESS; 7655 7656 release: 7657 /* Matches the ufshcd_hold() call at the start of this function. */ 7658 ufshcd_release(hba); 7659 return err; 7660 } 7661 7662 /** 7663 * ufshcd_host_reset_and_restore - reset and restore host controller 7664 * @hba: per-adapter instance 7665 * 7666 * Note that host controller reset may issue DME_RESET to 7667 * local and remote (device) Uni-Pro stack and the attributes 7668 * are reset to default state. 7669 * 7670 * Return: zero on success, non-zero on failure. 7671 */ 7672 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) 7673 { 7674 int err; 7675 7676 /* 7677 * Stop the host controller and complete the requests 7678 * cleared by h/w 7679 */ 7680 ufshcd_hba_stop(hba); 7681 hba->silence_err_logs = true; 7682 ufshcd_complete_requests(hba, true); 7683 hba->silence_err_logs = false; 7684 7685 /* scale up clocks to max frequency before full reinitialization */ 7686 ufshcd_scale_clks(hba, ULONG_MAX, true); 7687 7688 err = ufshcd_hba_enable(hba); 7689 7690 /* Establish the link again and restore the device */ 7691 if (!err) 7692 err = ufshcd_probe_hba(hba, false); 7693 7694 if (err) 7695 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); 7696 ufshcd_update_evt_hist(hba, UFS_EVT_HOST_RESET, (u32)err); 7697 return err; 7698 } 7699 7700 /** 7701 * ufshcd_reset_and_restore - reset and re-initialize host/device 7702 * @hba: per-adapter instance 7703 * 7704 * Reset and recover device, host and re-establish link. This 7705 * is helpful to recover the communication in fatal error conditions. 7706 * 7707 * Return: zero on success, non-zero on failure. 7708 */ 7709 static int ufshcd_reset_and_restore(struct ufs_hba *hba) 7710 { 7711 u32 saved_err = 0; 7712 u32 saved_uic_err = 0; 7713 int err = 0; 7714 unsigned long flags; 7715 int retries = MAX_HOST_RESET_RETRIES; 7716 7717 spin_lock_irqsave(hba->host->host_lock, flags); 7718 do { 7719 /* 7720 * This is a fresh start, cache and clear saved error first, 7721 * in case new error generated during reset and restore. 7722 */ 7723 saved_err |= hba->saved_err; 7724 saved_uic_err |= hba->saved_uic_err; 7725 hba->saved_err = 0; 7726 hba->saved_uic_err = 0; 7727 hba->force_reset = false; 7728 hba->ufshcd_state = UFSHCD_STATE_RESET; 7729 spin_unlock_irqrestore(hba->host->host_lock, flags); 7730 7731 /* Reset the attached device */ 7732 ufshcd_device_reset(hba); 7733 7734 err = ufshcd_host_reset_and_restore(hba); 7735 7736 spin_lock_irqsave(hba->host->host_lock, flags); 7737 if (err) 7738 continue; 7739 /* Do not exit unless operational or dead */ 7740 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL && 7741 hba->ufshcd_state != UFSHCD_STATE_ERROR && 7742 hba->ufshcd_state != UFSHCD_STATE_EH_SCHEDULED_NON_FATAL) 7743 err = -EAGAIN; 7744 } while (err && --retries); 7745 7746 /* 7747 * Inform scsi mid-layer that we did reset and allow to handle 7748 * Unit Attention properly. 7749 */ 7750 scsi_report_bus_reset(hba->host, 0); 7751 if (err) { 7752 hba->ufshcd_state = UFSHCD_STATE_ERROR; 7753 hba->saved_err |= saved_err; 7754 hba->saved_uic_err |= saved_uic_err; 7755 } 7756 spin_unlock_irqrestore(hba->host->host_lock, flags); 7757 7758 return err; 7759 } 7760 7761 /** 7762 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer 7763 * @cmd: SCSI command pointer 7764 * 7765 * Return: SUCCESS or FAILED. 7766 */ 7767 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) 7768 { 7769 int err = SUCCESS; 7770 unsigned long flags; 7771 struct ufs_hba *hba; 7772 7773 hba = shost_priv(cmd->device->host); 7774 7775 /* 7776 * If runtime PM sent SSU and got a timeout, scsi_error_handler is 7777 * stuck in this function waiting for flush_work(&hba->eh_work). And 7778 * ufshcd_err_handler(eh_work) is stuck waiting for runtime PM. Do 7779 * ufshcd_link_recovery instead of eh_work to prevent deadlock. 7780 */ 7781 if (hba->pm_op_in_progress) { 7782 if (ufshcd_link_recovery(hba)) 7783 err = FAILED; 7784 7785 return err; 7786 } 7787 7788 spin_lock_irqsave(hba->host->host_lock, flags); 7789 hba->force_reset = true; 7790 ufshcd_schedule_eh_work(hba); 7791 dev_err(hba->dev, "%s: reset in progress - 1\n", __func__); 7792 spin_unlock_irqrestore(hba->host->host_lock, flags); 7793 7794 flush_work(&hba->eh_work); 7795 7796 spin_lock_irqsave(hba->host->host_lock, flags); 7797 if (hba->ufshcd_state == UFSHCD_STATE_ERROR) 7798 err = FAILED; 7799 spin_unlock_irqrestore(hba->host->host_lock, flags); 7800 7801 return err; 7802 } 7803 7804 /** 7805 * ufshcd_get_max_icc_level - calculate the ICC level 7806 * @sup_curr_uA: max. current supported by the regulator 7807 * @start_scan: row at the desc table to start scan from 7808 * @buff: power descriptor buffer 7809 * 7810 * Return: calculated max ICC level for specific regulator. 7811 */ 7812 static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, 7813 const char *buff) 7814 { 7815 int i; 7816 int curr_uA; 7817 u16 data; 7818 u16 unit; 7819 7820 for (i = start_scan; i >= 0; i--) { 7821 data = get_unaligned_be16(&buff[2 * i]); 7822 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >> 7823 ATTR_ICC_LVL_UNIT_OFFSET; 7824 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK; 7825 switch (unit) { 7826 case UFSHCD_NANO_AMP: 7827 curr_uA = curr_uA / 1000; 7828 break; 7829 case UFSHCD_MILI_AMP: 7830 curr_uA = curr_uA * 1000; 7831 break; 7832 case UFSHCD_AMP: 7833 curr_uA = curr_uA * 1000 * 1000; 7834 break; 7835 case UFSHCD_MICRO_AMP: 7836 default: 7837 break; 7838 } 7839 if (sup_curr_uA >= curr_uA) 7840 break; 7841 } 7842 if (i < 0) { 7843 i = 0; 7844 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i); 7845 } 7846 7847 return (u32)i; 7848 } 7849 7850 /** 7851 * ufshcd_find_max_sup_active_icc_level - calculate the max ICC level 7852 * In case regulators are not initialized we'll return 0 7853 * @hba: per-adapter instance 7854 * @desc_buf: power descriptor buffer to extract ICC levels from. 7855 * 7856 * Return: calculated ICC level. 7857 */ 7858 static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, 7859 const u8 *desc_buf) 7860 { 7861 u32 icc_level = 0; 7862 7863 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || 7864 !hba->vreg_info.vccq2) { 7865 /* 7866 * Using dev_dbg to avoid messages during runtime PM to avoid 7867 * never-ending cycles of messages written back to storage by 7868 * user space causing runtime resume, causing more messages and 7869 * so on. 7870 */ 7871 dev_dbg(hba->dev, 7872 "%s: Regulator capability was not set, actvIccLevel=%d", 7873 __func__, icc_level); 7874 goto out; 7875 } 7876 7877 if (hba->vreg_info.vcc->max_uA) 7878 icc_level = ufshcd_get_max_icc_level( 7879 hba->vreg_info.vcc->max_uA, 7880 POWER_DESC_MAX_ACTV_ICC_LVLS - 1, 7881 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]); 7882 7883 if (hba->vreg_info.vccq->max_uA) 7884 icc_level = ufshcd_get_max_icc_level( 7885 hba->vreg_info.vccq->max_uA, 7886 icc_level, 7887 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]); 7888 7889 if (hba->vreg_info.vccq2->max_uA) 7890 icc_level = ufshcd_get_max_icc_level( 7891 hba->vreg_info.vccq2->max_uA, 7892 icc_level, 7893 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]); 7894 out: 7895 return icc_level; 7896 } 7897 7898 static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba) 7899 { 7900 int ret; 7901 u8 *desc_buf; 7902 u32 icc_level; 7903 7904 desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); 7905 if (!desc_buf) 7906 return; 7907 7908 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0, 7909 desc_buf, QUERY_DESC_MAX_SIZE); 7910 if (ret) { 7911 dev_err(hba->dev, 7912 "%s: Failed reading power descriptor ret = %d", 7913 __func__, ret); 7914 goto out; 7915 } 7916 7917 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf); 7918 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level); 7919 7920 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, 7921 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level); 7922 7923 if (ret) 7924 dev_err(hba->dev, 7925 "%s: Failed configuring bActiveICCLevel = %d ret = %d", 7926 __func__, icc_level, ret); 7927 7928 out: 7929 kfree(desc_buf); 7930 } 7931 7932 static inline void ufshcd_blk_pm_runtime_init(struct scsi_device *sdev) 7933 { 7934 struct Scsi_Host *shost = sdev->host; 7935 7936 scsi_autopm_get_device(sdev); 7937 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev); 7938 if (sdev->rpm_autosuspend) 7939 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev, 7940 shost->rpm_autosuspend_delay); 7941 scsi_autopm_put_device(sdev); 7942 } 7943 7944 /** 7945 * ufshcd_scsi_add_wlus - Adds required W-LUs 7946 * @hba: per-adapter instance 7947 * 7948 * UFS device specification requires the UFS devices to support 4 well known 7949 * logical units: 7950 * "REPORT_LUNS" (address: 01h) 7951 * "UFS Device" (address: 50h) 7952 * "RPMB" (address: 44h) 7953 * "BOOT" (address: 30h) 7954 * UFS device's power management needs to be controlled by "POWER CONDITION" 7955 * field of SSU (START STOP UNIT) command. But this "power condition" field 7956 * will take effect only when its sent to "UFS device" well known logical unit 7957 * hence we require the scsi_device instance to represent this logical unit in 7958 * order for the UFS host driver to send the SSU command for power management. 7959 * 7960 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory 7961 * Block) LU so user space process can control this LU. User space may also 7962 * want to have access to BOOT LU. 7963 * 7964 * This function adds scsi device instances for each of all well known LUs 7965 * (except "REPORT LUNS" LU). 7966 * 7967 * Return: zero on success (all required W-LUs are added successfully), 7968 * non-zero error value on failure (if failed to add any of the required W-LU). 7969 */ 7970 static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) 7971 { 7972 int ret = 0; 7973 struct scsi_device *sdev_boot, *sdev_rpmb; 7974 7975 hba->ufs_device_wlun = __scsi_add_device(hba->host, 0, 0, 7976 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL); 7977 if (IS_ERR(hba->ufs_device_wlun)) { 7978 ret = PTR_ERR(hba->ufs_device_wlun); 7979 hba->ufs_device_wlun = NULL; 7980 goto out; 7981 } 7982 scsi_device_put(hba->ufs_device_wlun); 7983 7984 sdev_rpmb = __scsi_add_device(hba->host, 0, 0, 7985 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL); 7986 if (IS_ERR(sdev_rpmb)) { 7987 ret = PTR_ERR(sdev_rpmb); 7988 goto remove_ufs_device_wlun; 7989 } 7990 ufshcd_blk_pm_runtime_init(sdev_rpmb); 7991 scsi_device_put(sdev_rpmb); 7992 7993 sdev_boot = __scsi_add_device(hba->host, 0, 0, 7994 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL); 7995 if (IS_ERR(sdev_boot)) { 7996 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__); 7997 } else { 7998 ufshcd_blk_pm_runtime_init(sdev_boot); 7999 scsi_device_put(sdev_boot); 8000 } 8001 goto out; 8002 8003 remove_ufs_device_wlun: 8004 scsi_remove_device(hba->ufs_device_wlun); 8005 out: 8006 return ret; 8007 } 8008 8009 static void ufshcd_wb_probe(struct ufs_hba *hba, const u8 *desc_buf) 8010 { 8011 struct ufs_dev_info *dev_info = &hba->dev_info; 8012 u8 lun; 8013 u32 d_lu_wb_buf_alloc; 8014 u32 ext_ufs_feature; 8015 8016 if (!ufshcd_is_wb_allowed(hba)) 8017 return; 8018 8019 /* 8020 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or 8021 * UFS devices with quirk UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES 8022 * enabled 8023 */ 8024 if (!(dev_info->wspecversion >= 0x310 || 8025 dev_info->wspecversion == 0x220 || 8026 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES))) 8027 goto wb_disabled; 8028 8029 ext_ufs_feature = get_unaligned_be32(desc_buf + 8030 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP); 8031 8032 if (!(ext_ufs_feature & UFS_DEV_WRITE_BOOSTER_SUP)) 8033 goto wb_disabled; 8034 8035 /* 8036 * WB may be supported but not configured while provisioning. The spec 8037 * says, in dedicated wb buffer mode, a max of 1 lun would have wb 8038 * buffer configured. 8039 */ 8040 dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE]; 8041 8042 dev_info->b_presrv_uspc_en = 8043 desc_buf[DEVICE_DESC_PARAM_WB_PRESRV_USRSPC_EN]; 8044 8045 if (dev_info->wb_buffer_type == WB_BUF_MODE_SHARED) { 8046 if (!get_unaligned_be32(desc_buf + 8047 DEVICE_DESC_PARAM_WB_SHARED_ALLOC_UNITS)) 8048 goto wb_disabled; 8049 } else { 8050 for (lun = 0; lun < UFS_UPIU_MAX_WB_LUN_ID; lun++) { 8051 d_lu_wb_buf_alloc = 0; 8052 ufshcd_read_unit_desc_param(hba, 8053 lun, 8054 UNIT_DESC_PARAM_WB_BUF_ALLOC_UNITS, 8055 (u8 *)&d_lu_wb_buf_alloc, 8056 sizeof(d_lu_wb_buf_alloc)); 8057 if (d_lu_wb_buf_alloc) { 8058 dev_info->wb_dedicated_lu = lun; 8059 break; 8060 } 8061 } 8062 8063 if (!d_lu_wb_buf_alloc) 8064 goto wb_disabled; 8065 } 8066 8067 if (!ufshcd_is_wb_buf_lifetime_available(hba)) 8068 goto wb_disabled; 8069 8070 return; 8071 8072 wb_disabled: 8073 hba->caps &= ~UFSHCD_CAP_WB_EN; 8074 } 8075 8076 static void ufshcd_temp_notif_probe(struct ufs_hba *hba, const u8 *desc_buf) 8077 { 8078 struct ufs_dev_info *dev_info = &hba->dev_info; 8079 u32 ext_ufs_feature; 8080 u8 mask = 0; 8081 8082 if (!(hba->caps & UFSHCD_CAP_TEMP_NOTIF) || dev_info->wspecversion < 0x300) 8083 return; 8084 8085 ext_ufs_feature = get_unaligned_be32(desc_buf + DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP); 8086 8087 if (ext_ufs_feature & UFS_DEV_LOW_TEMP_NOTIF) 8088 mask |= MASK_EE_TOO_LOW_TEMP; 8089 8090 if (ext_ufs_feature & UFS_DEV_HIGH_TEMP_NOTIF) 8091 mask |= MASK_EE_TOO_HIGH_TEMP; 8092 8093 if (mask) { 8094 ufshcd_enable_ee(hba, mask); 8095 ufs_hwmon_probe(hba, mask); 8096 } 8097 } 8098 8099 static void ufshcd_ext_iid_probe(struct ufs_hba *hba, u8 *desc_buf) 8100 { 8101 struct ufs_dev_info *dev_info = &hba->dev_info; 8102 u32 ext_ufs_feature; 8103 u32 ext_iid_en = 0; 8104 int err; 8105 8106 /* Only UFS-4.0 and above may support EXT_IID */ 8107 if (dev_info->wspecversion < 0x400) 8108 goto out; 8109 8110 ext_ufs_feature = get_unaligned_be32(desc_buf + 8111 DEVICE_DESC_PARAM_EXT_UFS_FEATURE_SUP); 8112 if (!(ext_ufs_feature & UFS_DEV_EXT_IID_SUP)) 8113 goto out; 8114 8115 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 8116 QUERY_ATTR_IDN_EXT_IID_EN, 0, 0, &ext_iid_en); 8117 if (err) 8118 dev_err(hba->dev, "failed reading bEXTIIDEn. err = %d\n", err); 8119 8120 out: 8121 dev_info->b_ext_iid_en = ext_iid_en; 8122 } 8123 8124 void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, 8125 const struct ufs_dev_quirk *fixups) 8126 { 8127 const struct ufs_dev_quirk *f; 8128 struct ufs_dev_info *dev_info = &hba->dev_info; 8129 8130 if (!fixups) 8131 return; 8132 8133 for (f = fixups; f->quirk; f++) { 8134 if ((f->wmanufacturerid == dev_info->wmanufacturerid || 8135 f->wmanufacturerid == UFS_ANY_VENDOR) && 8136 ((dev_info->model && 8137 STR_PRFX_EQUAL(f->model, dev_info->model)) || 8138 !strcmp(f->model, UFS_ANY_MODEL))) 8139 hba->dev_quirks |= f->quirk; 8140 } 8141 } 8142 EXPORT_SYMBOL_GPL(ufshcd_fixup_dev_quirks); 8143 8144 static void ufs_fixup_device_setup(struct ufs_hba *hba) 8145 { 8146 /* fix by general quirk table */ 8147 ufshcd_fixup_dev_quirks(hba, ufs_fixups); 8148 8149 /* allow vendors to fix quirks */ 8150 ufshcd_vops_fixup_dev_quirks(hba); 8151 } 8152 8153 static void ufshcd_update_rtc(struct ufs_hba *hba) 8154 { 8155 struct timespec64 ts64; 8156 int err; 8157 u32 val; 8158 8159 ktime_get_real_ts64(&ts64); 8160 8161 if (ts64.tv_sec < hba->dev_info.rtc_time_baseline) { 8162 dev_warn_once(hba->dev, "%s: Current time precedes previous setting!\n", __func__); 8163 return; 8164 } 8165 8166 /* 8167 * The Absolute RTC mode has a 136-year limit, spanning from 2010 to 2146. If a time beyond 8168 * 2146 is required, it is recommended to choose the relative RTC mode. 8169 */ 8170 val = ts64.tv_sec - hba->dev_info.rtc_time_baseline; 8171 8172 ufshcd_rpm_get_sync(hba); 8173 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, QUERY_ATTR_IDN_SECONDS_PASSED, 8174 0, 0, &val); 8175 ufshcd_rpm_put_sync(hba); 8176 8177 if (err) 8178 dev_err(hba->dev, "%s: Failed to update rtc %d\n", __func__, err); 8179 else if (hba->dev_info.rtc_type == UFS_RTC_RELATIVE) 8180 hba->dev_info.rtc_time_baseline = ts64.tv_sec; 8181 } 8182 8183 static void ufshcd_rtc_work(struct work_struct *work) 8184 { 8185 struct ufs_hba *hba; 8186 8187 hba = container_of(to_delayed_work(work), struct ufs_hba, ufs_rtc_update_work); 8188 8189 /* Update RTC only when there are no requests in progress and UFSHCI is operational */ 8190 if (!ufshcd_is_ufs_dev_busy(hba) && hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) 8191 ufshcd_update_rtc(hba); 8192 8193 if (ufshcd_is_ufs_dev_active(hba) && hba->dev_info.rtc_update_period) 8194 schedule_delayed_work(&hba->ufs_rtc_update_work, 8195 msecs_to_jiffies(hba->dev_info.rtc_update_period)); 8196 } 8197 8198 static void ufs_init_rtc(struct ufs_hba *hba, u8 *desc_buf) 8199 { 8200 u16 periodic_rtc_update = get_unaligned_be16(&desc_buf[DEVICE_DESC_PARAM_FRQ_RTC]); 8201 struct ufs_dev_info *dev_info = &hba->dev_info; 8202 8203 if (periodic_rtc_update & UFS_RTC_TIME_BASELINE) { 8204 dev_info->rtc_type = UFS_RTC_ABSOLUTE; 8205 8206 /* 8207 * The concept of measuring time in Linux as the number of seconds elapsed since 8208 * 00:00:00 UTC on January 1, 1970, and UFS ABS RTC is elapsed from January 1st 8209 * 2010 00:00, here we need to adjust ABS baseline. 8210 */ 8211 dev_info->rtc_time_baseline = mktime64(2010, 1, 1, 0, 0, 0) - 8212 mktime64(1970, 1, 1, 0, 0, 0); 8213 } else { 8214 dev_info->rtc_type = UFS_RTC_RELATIVE; 8215 dev_info->rtc_time_baseline = 0; 8216 } 8217 8218 /* 8219 * We ignore TIME_PERIOD defined in wPeriodicRTCUpdate because Spec does not clearly state 8220 * how to calculate the specific update period for each time unit. And we disable periodic 8221 * RTC update work, let user configure by sysfs node according to specific circumstance. 8222 */ 8223 dev_info->rtc_update_period = 0; 8224 } 8225 8226 static int ufs_get_device_desc(struct ufs_hba *hba) 8227 { 8228 int err; 8229 u8 model_index; 8230 u8 *desc_buf; 8231 struct ufs_dev_info *dev_info = &hba->dev_info; 8232 8233 desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); 8234 if (!desc_buf) { 8235 err = -ENOMEM; 8236 goto out; 8237 } 8238 8239 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf, 8240 QUERY_DESC_MAX_SIZE); 8241 if (err) { 8242 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", 8243 __func__, err); 8244 goto out; 8245 } 8246 8247 /* 8248 * getting vendor (manufacturerID) and Bank Index in big endian 8249 * format 8250 */ 8251 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | 8252 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1]; 8253 8254 /* getting Specification Version in big endian format */ 8255 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 | 8256 desc_buf[DEVICE_DESC_PARAM_SPEC_VER + 1]; 8257 dev_info->bqueuedepth = desc_buf[DEVICE_DESC_PARAM_Q_DPTH]; 8258 8259 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME]; 8260 8261 err = ufshcd_read_string_desc(hba, model_index, 8262 &dev_info->model, SD_ASCII_STD); 8263 if (err < 0) { 8264 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", 8265 __func__, err); 8266 goto out; 8267 } 8268 8269 hba->luns_avail = desc_buf[DEVICE_DESC_PARAM_NUM_LU] + 8270 desc_buf[DEVICE_DESC_PARAM_NUM_WLU]; 8271 8272 ufs_fixup_device_setup(hba); 8273 8274 ufshcd_wb_probe(hba, desc_buf); 8275 8276 ufshcd_temp_notif_probe(hba, desc_buf); 8277 8278 ufs_init_rtc(hba, desc_buf); 8279 8280 if (hba->ext_iid_sup) 8281 ufshcd_ext_iid_probe(hba, desc_buf); 8282 8283 /* 8284 * ufshcd_read_string_desc returns size of the string 8285 * reset the error value 8286 */ 8287 err = 0; 8288 8289 out: 8290 kfree(desc_buf); 8291 return err; 8292 } 8293 8294 static void ufs_put_device_desc(struct ufs_hba *hba) 8295 { 8296 struct ufs_dev_info *dev_info = &hba->dev_info; 8297 8298 kfree(dev_info->model); 8299 dev_info->model = NULL; 8300 } 8301 8302 /** 8303 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is 8304 * less than device PA_TACTIVATE time. 8305 * @hba: per-adapter instance 8306 * 8307 * Some UFS devices require host PA_TACTIVATE to be lower than device 8308 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk 8309 * for such devices. 8310 * 8311 * Return: zero on success, non-zero error value on failure. 8312 */ 8313 static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) 8314 { 8315 int ret = 0; 8316 u32 granularity, peer_granularity; 8317 u32 pa_tactivate, peer_pa_tactivate; 8318 u32 pa_tactivate_us, peer_pa_tactivate_us; 8319 static const u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100}; 8320 8321 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), 8322 &granularity); 8323 if (ret) 8324 goto out; 8325 8326 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), 8327 &peer_granularity); 8328 if (ret) 8329 goto out; 8330 8331 if ((granularity < PA_GRANULARITY_MIN_VAL) || 8332 (granularity > PA_GRANULARITY_MAX_VAL)) { 8333 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", 8334 __func__, granularity); 8335 return -EINVAL; 8336 } 8337 8338 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) || 8339 (peer_granularity > PA_GRANULARITY_MAX_VAL)) { 8340 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", 8341 __func__, peer_granularity); 8342 return -EINVAL; 8343 } 8344 8345 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); 8346 if (ret) 8347 goto out; 8348 8349 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), 8350 &peer_pa_tactivate); 8351 if (ret) 8352 goto out; 8353 8354 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; 8355 peer_pa_tactivate_us = peer_pa_tactivate * 8356 gran_to_us_table[peer_granularity - 1]; 8357 8358 if (pa_tactivate_us >= peer_pa_tactivate_us) { 8359 u32 new_peer_pa_tactivate; 8360 8361 new_peer_pa_tactivate = pa_tactivate_us / 8362 gran_to_us_table[peer_granularity - 1]; 8363 new_peer_pa_tactivate++; 8364 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 8365 new_peer_pa_tactivate); 8366 } 8367 8368 out: 8369 return ret; 8370 } 8371 8372 static void ufshcd_tune_unipro_params(struct ufs_hba *hba) 8373 { 8374 ufshcd_vops_apply_dev_quirks(hba); 8375 8376 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) 8377 /* set 1ms timeout for PA_TACTIVATE */ 8378 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); 8379 8380 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) 8381 ufshcd_quirk_tune_host_pa_tactivate(hba); 8382 } 8383 8384 static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) 8385 { 8386 hba->ufs_stats.hibern8_exit_cnt = 0; 8387 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); 8388 hba->req_abort_count = 0; 8389 } 8390 8391 static int ufshcd_device_geo_params_init(struct ufs_hba *hba) 8392 { 8393 int err; 8394 u8 *desc_buf; 8395 8396 desc_buf = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL); 8397 if (!desc_buf) { 8398 err = -ENOMEM; 8399 goto out; 8400 } 8401 8402 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0, 8403 desc_buf, QUERY_DESC_MAX_SIZE); 8404 if (err) { 8405 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n", 8406 __func__, err); 8407 goto out; 8408 } 8409 8410 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1) 8411 hba->dev_info.max_lu_supported = 32; 8412 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0) 8413 hba->dev_info.max_lu_supported = 8; 8414 8415 out: 8416 kfree(desc_buf); 8417 return err; 8418 } 8419 8420 struct ufs_ref_clk { 8421 unsigned long freq_hz; 8422 enum ufs_ref_clk_freq val; 8423 }; 8424 8425 static const struct ufs_ref_clk ufs_ref_clk_freqs[] = { 8426 {19200000, REF_CLK_FREQ_19_2_MHZ}, 8427 {26000000, REF_CLK_FREQ_26_MHZ}, 8428 {38400000, REF_CLK_FREQ_38_4_MHZ}, 8429 {52000000, REF_CLK_FREQ_52_MHZ}, 8430 {0, REF_CLK_FREQ_INVAL}, 8431 }; 8432 8433 static enum ufs_ref_clk_freq 8434 ufs_get_bref_clk_from_hz(unsigned long freq) 8435 { 8436 int i; 8437 8438 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++) 8439 if (ufs_ref_clk_freqs[i].freq_hz == freq) 8440 return ufs_ref_clk_freqs[i].val; 8441 8442 return REF_CLK_FREQ_INVAL; 8443 } 8444 8445 void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk) 8446 { 8447 unsigned long freq; 8448 8449 freq = clk_get_rate(refclk); 8450 8451 hba->dev_ref_clk_freq = 8452 ufs_get_bref_clk_from_hz(freq); 8453 8454 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) 8455 dev_err(hba->dev, 8456 "invalid ref_clk setting = %ld\n", freq); 8457 } 8458 8459 static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba) 8460 { 8461 int err; 8462 u32 ref_clk; 8463 u32 freq = hba->dev_ref_clk_freq; 8464 8465 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, 8466 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk); 8467 8468 if (err) { 8469 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n", 8470 err); 8471 goto out; 8472 } 8473 8474 if (ref_clk == freq) 8475 goto out; /* nothing to update */ 8476 8477 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, 8478 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq); 8479 8480 if (err) { 8481 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n", 8482 ufs_ref_clk_freqs[freq].freq_hz); 8483 goto out; 8484 } 8485 8486 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n", 8487 ufs_ref_clk_freqs[freq].freq_hz); 8488 8489 out: 8490 return err; 8491 } 8492 8493 static int ufshcd_device_params_init(struct ufs_hba *hba) 8494 { 8495 bool flag; 8496 int ret; 8497 8498 /* Init UFS geometry descriptor related parameters */ 8499 ret = ufshcd_device_geo_params_init(hba); 8500 if (ret) 8501 goto out; 8502 8503 /* Check and apply UFS device quirks */ 8504 ret = ufs_get_device_desc(hba); 8505 if (ret) { 8506 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", 8507 __func__, ret); 8508 goto out; 8509 } 8510 8511 ufshcd_get_ref_clk_gating_wait(hba); 8512 8513 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, 8514 QUERY_FLAG_IDN_PWR_ON_WPE, 0, &flag)) 8515 hba->dev_info.f_power_on_wp_en = flag; 8516 8517 /* Probe maximum power mode co-supported by both UFS host and device */ 8518 if (ufshcd_get_max_pwr_mode(hba)) 8519 dev_err(hba->dev, 8520 "%s: Failed getting max supported power mode\n", 8521 __func__); 8522 out: 8523 return ret; 8524 } 8525 8526 static void ufshcd_set_timestamp_attr(struct ufs_hba *hba) 8527 { 8528 int err; 8529 struct ufs_query_req *request = NULL; 8530 struct ufs_query_res *response = NULL; 8531 struct ufs_dev_info *dev_info = &hba->dev_info; 8532 struct utp_upiu_query_v4_0 *upiu_data; 8533 8534 if (dev_info->wspecversion < 0x400) 8535 return; 8536 8537 ufshcd_dev_man_lock(hba); 8538 8539 ufshcd_init_query(hba, &request, &response, 8540 UPIU_QUERY_OPCODE_WRITE_ATTR, 8541 QUERY_ATTR_IDN_TIMESTAMP, 0, 0); 8542 8543 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; 8544 8545 upiu_data = (struct utp_upiu_query_v4_0 *)&request->upiu_req; 8546 8547 put_unaligned_be64(ktime_get_real_ns(), &upiu_data->osf3); 8548 8549 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); 8550 8551 if (err) 8552 dev_err(hba->dev, "%s: failed to set timestamp %d\n", 8553 __func__, err); 8554 8555 ufshcd_dev_man_unlock(hba); 8556 } 8557 8558 /** 8559 * ufshcd_add_lus - probe and add UFS logical units 8560 * @hba: per-adapter instance 8561 * 8562 * Return: 0 upon success; < 0 upon failure. 8563 */ 8564 static int ufshcd_add_lus(struct ufs_hba *hba) 8565 { 8566 int ret; 8567 8568 /* Add required well known logical units to scsi mid layer */ 8569 ret = ufshcd_scsi_add_wlus(hba); 8570 if (ret) 8571 goto out; 8572 8573 /* Initialize devfreq after UFS device is detected */ 8574 if (ufshcd_is_clkscaling_supported(hba)) { 8575 memcpy(&hba->clk_scaling.saved_pwr_info, 8576 &hba->pwr_info, 8577 sizeof(struct ufs_pa_layer_attr)); 8578 hba->clk_scaling.is_allowed = true; 8579 8580 ret = ufshcd_devfreq_init(hba); 8581 if (ret) 8582 goto out; 8583 8584 hba->clk_scaling.is_enabled = true; 8585 ufshcd_init_clk_scaling_sysfs(hba); 8586 } 8587 8588 ufs_bsg_probe(hba); 8589 scsi_scan_host(hba->host); 8590 8591 out: 8592 return ret; 8593 } 8594 8595 /* SDB - Single Doorbell */ 8596 static void ufshcd_release_sdb_queue(struct ufs_hba *hba, int nutrs) 8597 { 8598 size_t ucdl_size, utrdl_size; 8599 8600 ucdl_size = ufshcd_get_ucd_size(hba) * nutrs; 8601 dmam_free_coherent(hba->dev, ucdl_size, hba->ucdl_base_addr, 8602 hba->ucdl_dma_addr); 8603 8604 utrdl_size = sizeof(struct utp_transfer_req_desc) * nutrs; 8605 dmam_free_coherent(hba->dev, utrdl_size, hba->utrdl_base_addr, 8606 hba->utrdl_dma_addr); 8607 8608 devm_kfree(hba->dev, hba->lrb); 8609 } 8610 8611 static int ufshcd_alloc_mcq(struct ufs_hba *hba) 8612 { 8613 int ret; 8614 int old_nutrs = hba->nutrs; 8615 8616 ret = ufshcd_mcq_decide_queue_depth(hba); 8617 if (ret < 0) 8618 return ret; 8619 8620 hba->nutrs = ret; 8621 ret = ufshcd_mcq_init(hba); 8622 if (ret) 8623 goto err; 8624 8625 /* 8626 * Previously allocated memory for nutrs may not be enough in MCQ mode. 8627 * Number of supported tags in MCQ mode may be larger than SDB mode. 8628 */ 8629 if (hba->nutrs != old_nutrs) { 8630 ufshcd_release_sdb_queue(hba, old_nutrs); 8631 ret = ufshcd_memory_alloc(hba); 8632 if (ret) 8633 goto err; 8634 ufshcd_host_memory_configure(hba); 8635 } 8636 8637 ret = ufshcd_mcq_memory_alloc(hba); 8638 if (ret) 8639 goto err; 8640 8641 return 0; 8642 err: 8643 hba->nutrs = old_nutrs; 8644 return ret; 8645 } 8646 8647 static void ufshcd_config_mcq(struct ufs_hba *hba) 8648 { 8649 int ret; 8650 u32 intrs; 8651 8652 ret = ufshcd_mcq_vops_config_esi(hba); 8653 dev_info(hba->dev, "ESI %sconfigured\n", ret ? "is not " : ""); 8654 8655 intrs = UFSHCD_ENABLE_MCQ_INTRS; 8656 if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_INTR) 8657 intrs &= ~MCQ_CQ_EVENT_STATUS; 8658 ufshcd_enable_intr(hba, intrs); 8659 ufshcd_mcq_make_queues_operational(hba); 8660 ufshcd_mcq_config_mac(hba, hba->nutrs); 8661 8662 hba->host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED; 8663 hba->reserved_slot = hba->nutrs - UFSHCD_NUM_RESERVED; 8664 8665 ufshcd_mcq_enable(hba); 8666 hba->mcq_enabled = true; 8667 8668 dev_info(hba->dev, "MCQ configured, nr_queues=%d, io_queues=%d, read_queue=%d, poll_queues=%d, queue_depth=%d\n", 8669 hba->nr_hw_queues, hba->nr_queues[HCTX_TYPE_DEFAULT], 8670 hba->nr_queues[HCTX_TYPE_READ], hba->nr_queues[HCTX_TYPE_POLL], 8671 hba->nutrs); 8672 } 8673 8674 static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params) 8675 { 8676 int ret; 8677 struct Scsi_Host *host = hba->host; 8678 8679 hba->ufshcd_state = UFSHCD_STATE_RESET; 8680 8681 ret = ufshcd_link_startup(hba); 8682 if (ret) 8683 return ret; 8684 8685 if (hba->quirks & UFSHCD_QUIRK_SKIP_PH_CONFIGURATION) 8686 return ret; 8687 8688 /* Debug counters initialization */ 8689 ufshcd_clear_dbg_ufs_stats(hba); 8690 8691 /* UniPro link is active now */ 8692 ufshcd_set_link_active(hba); 8693 8694 /* Reconfigure MCQ upon reset */ 8695 if (is_mcq_enabled(hba) && !init_dev_params) 8696 ufshcd_config_mcq(hba); 8697 8698 /* Verify device initialization by sending NOP OUT UPIU */ 8699 ret = ufshcd_verify_dev_init(hba); 8700 if (ret) 8701 return ret; 8702 8703 /* Initiate UFS initialization, and waiting until completion */ 8704 ret = ufshcd_complete_dev_init(hba); 8705 if (ret) 8706 return ret; 8707 8708 /* 8709 * Initialize UFS device parameters used by driver, these 8710 * parameters are associated with UFS descriptors. 8711 */ 8712 if (init_dev_params) { 8713 ret = ufshcd_device_params_init(hba); 8714 if (ret) 8715 return ret; 8716 if (is_mcq_supported(hba) && !hba->scsi_host_added) { 8717 ret = ufshcd_alloc_mcq(hba); 8718 if (!ret) { 8719 ufshcd_config_mcq(hba); 8720 } else { 8721 /* Continue with SDB mode */ 8722 use_mcq_mode = false; 8723 dev_err(hba->dev, "MCQ mode is disabled, err=%d\n", 8724 ret); 8725 } 8726 ret = scsi_add_host(host, hba->dev); 8727 if (ret) { 8728 dev_err(hba->dev, "scsi_add_host failed\n"); 8729 return ret; 8730 } 8731 hba->scsi_host_added = true; 8732 } else if (is_mcq_supported(hba)) { 8733 /* UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH is set */ 8734 ufshcd_config_mcq(hba); 8735 } 8736 } 8737 8738 ufshcd_tune_unipro_params(hba); 8739 8740 /* UFS device is also active now */ 8741 ufshcd_set_ufs_dev_active(hba); 8742 ufshcd_force_reset_auto_bkops(hba); 8743 8744 ufshcd_set_timestamp_attr(hba); 8745 schedule_delayed_work(&hba->ufs_rtc_update_work, 8746 msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS)); 8747 8748 /* Gear up to HS gear if supported */ 8749 if (hba->max_pwr_info.is_valid) { 8750 /* 8751 * Set the right value to bRefClkFreq before attempting to 8752 * switch to HS gears. 8753 */ 8754 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL) 8755 ufshcd_set_dev_ref_clk(hba); 8756 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); 8757 if (ret) { 8758 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", 8759 __func__, ret); 8760 return ret; 8761 } 8762 } 8763 8764 return 0; 8765 } 8766 8767 /** 8768 * ufshcd_probe_hba - probe hba to detect device and initialize it 8769 * @hba: per-adapter instance 8770 * @init_dev_params: whether or not to call ufshcd_device_params_init(). 8771 * 8772 * Execute link-startup and verify device initialization 8773 * 8774 * Return: 0 upon success; < 0 upon failure. 8775 */ 8776 static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params) 8777 { 8778 ktime_t start = ktime_get(); 8779 unsigned long flags; 8780 int ret; 8781 8782 ret = ufshcd_device_init(hba, init_dev_params); 8783 if (ret) 8784 goto out; 8785 8786 if (!hba->pm_op_in_progress && 8787 (hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) { 8788 /* Reset the device and controller before doing reinit */ 8789 ufshcd_device_reset(hba); 8790 ufshcd_hba_stop(hba); 8791 ufshcd_vops_reinit_notify(hba); 8792 ret = ufshcd_hba_enable(hba); 8793 if (ret) { 8794 dev_err(hba->dev, "Host controller enable failed\n"); 8795 ufshcd_print_evt_hist(hba); 8796 ufshcd_print_host_state(hba); 8797 goto out; 8798 } 8799 8800 /* Reinit the device */ 8801 ret = ufshcd_device_init(hba, init_dev_params); 8802 if (ret) 8803 goto out; 8804 } 8805 8806 ufshcd_print_pwr_info(hba); 8807 8808 /* 8809 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec) 8810 * and for removable UFS card as well, hence always set the parameter. 8811 * Note: Error handler may issue the device reset hence resetting 8812 * bActiveICCLevel as well so it is always safe to set this here. 8813 */ 8814 ufshcd_set_active_icc_lvl(hba); 8815 8816 /* Enable UFS Write Booster if supported */ 8817 ufshcd_configure_wb(hba); 8818 8819 if (hba->ee_usr_mask) 8820 ufshcd_write_ee_control(hba); 8821 ufshcd_configure_auto_hibern8(hba); 8822 8823 out: 8824 spin_lock_irqsave(hba->host->host_lock, flags); 8825 if (ret) 8826 hba->ufshcd_state = UFSHCD_STATE_ERROR; 8827 else if (hba->ufshcd_state == UFSHCD_STATE_RESET) 8828 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; 8829 spin_unlock_irqrestore(hba->host->host_lock, flags); 8830 8831 trace_ufshcd_init(dev_name(hba->dev), ret, 8832 ktime_to_us(ktime_sub(ktime_get(), start)), 8833 hba->curr_dev_pwr_mode, hba->uic_link_state); 8834 return ret; 8835 } 8836 8837 /** 8838 * ufshcd_async_scan - asynchronous execution for probing hba 8839 * @data: data pointer to pass to this function 8840 * @cookie: cookie data 8841 */ 8842 static void ufshcd_async_scan(void *data, async_cookie_t cookie) 8843 { 8844 struct ufs_hba *hba = (struct ufs_hba *)data; 8845 int ret; 8846 8847 down(&hba->host_sem); 8848 /* Initialize hba, detect and initialize UFS device */ 8849 ret = ufshcd_probe_hba(hba, true); 8850 up(&hba->host_sem); 8851 if (ret) 8852 goto out; 8853 8854 /* Probe and add UFS logical units */ 8855 ret = ufshcd_add_lus(hba); 8856 8857 out: 8858 pm_runtime_put_sync(hba->dev); 8859 8860 if (ret) 8861 dev_err(hba->dev, "%s failed: %d\n", __func__, ret); 8862 } 8863 8864 static enum scsi_timeout_action ufshcd_eh_timed_out(struct scsi_cmnd *scmd) 8865 { 8866 struct ufs_hba *hba = shost_priv(scmd->device->host); 8867 8868 if (!hba->system_suspending) { 8869 /* Activate the error handler in the SCSI core. */ 8870 return SCSI_EH_NOT_HANDLED; 8871 } 8872 8873 /* 8874 * If we get here we know that no TMFs are outstanding and also that 8875 * the only pending command is a START STOP UNIT command. Handle the 8876 * timeout of that command directly to prevent a deadlock between 8877 * ufshcd_set_dev_pwr_mode() and ufshcd_err_handler(). 8878 */ 8879 ufshcd_link_recovery(hba); 8880 dev_info(hba->dev, "%s() finished; outstanding_tasks = %#lx.\n", 8881 __func__, hba->outstanding_tasks); 8882 8883 return hba->outstanding_reqs ? SCSI_EH_RESET_TIMER : SCSI_EH_DONE; 8884 } 8885 8886 static const struct attribute_group *ufshcd_driver_groups[] = { 8887 &ufs_sysfs_unit_descriptor_group, 8888 &ufs_sysfs_lun_attributes_group, 8889 NULL, 8890 }; 8891 8892 static struct ufs_hba_variant_params ufs_hba_vps = { 8893 .hba_enable_delay_us = 1000, 8894 .wb_flush_threshold = UFS_WB_BUF_REMAIN_PERCENT(40), 8895 .devfreq_profile.polling_ms = 100, 8896 .devfreq_profile.target = ufshcd_devfreq_target, 8897 .devfreq_profile.get_dev_status = ufshcd_devfreq_get_dev_status, 8898 .ondemand_data.upthreshold = 70, 8899 .ondemand_data.downdifferential = 5, 8900 }; 8901 8902 static const struct scsi_host_template ufshcd_driver_template = { 8903 .module = THIS_MODULE, 8904 .name = UFSHCD, 8905 .proc_name = UFSHCD, 8906 .map_queues = ufshcd_map_queues, 8907 .queuecommand = ufshcd_queuecommand, 8908 .mq_poll = ufshcd_poll, 8909 .slave_alloc = ufshcd_slave_alloc, 8910 .slave_configure = ufshcd_slave_configure, 8911 .slave_destroy = ufshcd_slave_destroy, 8912 .change_queue_depth = ufshcd_change_queue_depth, 8913 .eh_abort_handler = ufshcd_abort, 8914 .eh_device_reset_handler = ufshcd_eh_device_reset_handler, 8915 .eh_host_reset_handler = ufshcd_eh_host_reset_handler, 8916 .eh_timed_out = ufshcd_eh_timed_out, 8917 .this_id = -1, 8918 .sg_tablesize = SG_ALL, 8919 .cmd_per_lun = UFSHCD_CMD_PER_LUN, 8920 .can_queue = UFSHCD_CAN_QUEUE, 8921 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX, 8922 .max_sectors = SZ_1M / SECTOR_SIZE, 8923 .max_host_blocked = 1, 8924 .track_queue_depth = 1, 8925 .skip_settle_delay = 1, 8926 .sdev_groups = ufshcd_driver_groups, 8927 }; 8928 8929 static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg, 8930 int ua) 8931 { 8932 int ret; 8933 8934 if (!vreg) 8935 return 0; 8936 8937 /* 8938 * "set_load" operation shall be required on those regulators 8939 * which specifically configured current limitation. Otherwise 8940 * zero max_uA may cause unexpected behavior when regulator is 8941 * enabled or set as high power mode. 8942 */ 8943 if (!vreg->max_uA) 8944 return 0; 8945 8946 ret = regulator_set_load(vreg->reg, ua); 8947 if (ret < 0) { 8948 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n", 8949 __func__, vreg->name, ua, ret); 8950 } 8951 8952 return ret; 8953 } 8954 8955 static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, 8956 struct ufs_vreg *vreg) 8957 { 8958 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA); 8959 } 8960 8961 static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, 8962 struct ufs_vreg *vreg) 8963 { 8964 if (!vreg) 8965 return 0; 8966 8967 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); 8968 } 8969 8970 static int ufshcd_config_vreg(struct device *dev, 8971 struct ufs_vreg *vreg, bool on) 8972 { 8973 if (regulator_count_voltages(vreg->reg) <= 0) 8974 return 0; 8975 8976 return ufshcd_config_vreg_load(dev, vreg, on ? vreg->max_uA : 0); 8977 } 8978 8979 static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg) 8980 { 8981 int ret = 0; 8982 8983 if (!vreg || vreg->enabled) 8984 goto out; 8985 8986 ret = ufshcd_config_vreg(dev, vreg, true); 8987 if (!ret) 8988 ret = regulator_enable(vreg->reg); 8989 8990 if (!ret) 8991 vreg->enabled = true; 8992 else 8993 dev_err(dev, "%s: %s enable failed, err=%d\n", 8994 __func__, vreg->name, ret); 8995 out: 8996 return ret; 8997 } 8998 8999 static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg) 9000 { 9001 int ret = 0; 9002 9003 if (!vreg || !vreg->enabled || vreg->always_on) 9004 goto out; 9005 9006 ret = regulator_disable(vreg->reg); 9007 9008 if (!ret) { 9009 /* ignore errors on applying disable config */ 9010 ufshcd_config_vreg(dev, vreg, false); 9011 vreg->enabled = false; 9012 } else { 9013 dev_err(dev, "%s: %s disable failed, err=%d\n", 9014 __func__, vreg->name, ret); 9015 } 9016 out: 9017 return ret; 9018 } 9019 9020 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) 9021 { 9022 int ret = 0; 9023 struct device *dev = hba->dev; 9024 struct ufs_vreg_info *info = &hba->vreg_info; 9025 9026 ret = ufshcd_toggle_vreg(dev, info->vcc, on); 9027 if (ret) 9028 goto out; 9029 9030 ret = ufshcd_toggle_vreg(dev, info->vccq, on); 9031 if (ret) 9032 goto out; 9033 9034 ret = ufshcd_toggle_vreg(dev, info->vccq2, on); 9035 9036 out: 9037 if (ret) { 9038 ufshcd_toggle_vreg(dev, info->vccq2, false); 9039 ufshcd_toggle_vreg(dev, info->vccq, false); 9040 ufshcd_toggle_vreg(dev, info->vcc, false); 9041 } 9042 return ret; 9043 } 9044 9045 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) 9046 { 9047 struct ufs_vreg_info *info = &hba->vreg_info; 9048 9049 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); 9050 } 9051 9052 int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg) 9053 { 9054 int ret = 0; 9055 9056 if (!vreg) 9057 goto out; 9058 9059 vreg->reg = devm_regulator_get(dev, vreg->name); 9060 if (IS_ERR(vreg->reg)) { 9061 ret = PTR_ERR(vreg->reg); 9062 dev_err(dev, "%s: %s get failed, err=%d\n", 9063 __func__, vreg->name, ret); 9064 } 9065 out: 9066 return ret; 9067 } 9068 EXPORT_SYMBOL_GPL(ufshcd_get_vreg); 9069 9070 static int ufshcd_init_vreg(struct ufs_hba *hba) 9071 { 9072 int ret = 0; 9073 struct device *dev = hba->dev; 9074 struct ufs_vreg_info *info = &hba->vreg_info; 9075 9076 ret = ufshcd_get_vreg(dev, info->vcc); 9077 if (ret) 9078 goto out; 9079 9080 ret = ufshcd_get_vreg(dev, info->vccq); 9081 if (!ret) 9082 ret = ufshcd_get_vreg(dev, info->vccq2); 9083 out: 9084 return ret; 9085 } 9086 9087 static int ufshcd_init_hba_vreg(struct ufs_hba *hba) 9088 { 9089 struct ufs_vreg_info *info = &hba->vreg_info; 9090 9091 return ufshcd_get_vreg(hba->dev, info->vdd_hba); 9092 } 9093 9094 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on) 9095 { 9096 int ret = 0; 9097 struct ufs_clk_info *clki; 9098 struct list_head *head = &hba->clk_list_head; 9099 unsigned long flags; 9100 ktime_t start = ktime_get(); 9101 bool clk_state_changed = false; 9102 9103 if (list_empty(head)) 9104 goto out; 9105 9106 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE); 9107 if (ret) 9108 return ret; 9109 9110 list_for_each_entry(clki, head, list) { 9111 if (!IS_ERR_OR_NULL(clki->clk)) { 9112 /* 9113 * Don't disable clocks which are needed 9114 * to keep the link active. 9115 */ 9116 if (ufshcd_is_link_active(hba) && 9117 clki->keep_link_active) 9118 continue; 9119 9120 clk_state_changed = on ^ clki->enabled; 9121 if (on && !clki->enabled) { 9122 ret = clk_prepare_enable(clki->clk); 9123 if (ret) { 9124 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", 9125 __func__, clki->name, ret); 9126 goto out; 9127 } 9128 } else if (!on && clki->enabled) { 9129 clk_disable_unprepare(clki->clk); 9130 } 9131 clki->enabled = on; 9132 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, 9133 clki->name, on ? "en" : "dis"); 9134 } 9135 } 9136 9137 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE); 9138 if (ret) 9139 return ret; 9140 9141 if (!ufshcd_is_clkscaling_supported(hba)) 9142 ufshcd_pm_qos_update(hba, on); 9143 out: 9144 if (ret) { 9145 list_for_each_entry(clki, head, list) { 9146 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) 9147 clk_disable_unprepare(clki->clk); 9148 } 9149 } else if (!ret && on) { 9150 spin_lock_irqsave(hba->host->host_lock, flags); 9151 hba->clk_gating.state = CLKS_ON; 9152 trace_ufshcd_clk_gating(dev_name(hba->dev), 9153 hba->clk_gating.state); 9154 spin_unlock_irqrestore(hba->host->host_lock, flags); 9155 } 9156 9157 if (clk_state_changed) 9158 trace_ufshcd_profile_clk_gating(dev_name(hba->dev), 9159 (on ? "on" : "off"), 9160 ktime_to_us(ktime_sub(ktime_get(), start)), ret); 9161 return ret; 9162 } 9163 9164 static enum ufs_ref_clk_freq ufshcd_parse_ref_clk_property(struct ufs_hba *hba) 9165 { 9166 u32 freq; 9167 int ret = device_property_read_u32(hba->dev, "ref-clk-freq", &freq); 9168 9169 if (ret) { 9170 dev_dbg(hba->dev, "Cannot query 'ref-clk-freq' property = %d", ret); 9171 return REF_CLK_FREQ_INVAL; 9172 } 9173 9174 return ufs_get_bref_clk_from_hz(freq); 9175 } 9176 9177 static int ufshcd_init_clocks(struct ufs_hba *hba) 9178 { 9179 int ret = 0; 9180 struct ufs_clk_info *clki; 9181 struct device *dev = hba->dev; 9182 struct list_head *head = &hba->clk_list_head; 9183 9184 if (list_empty(head)) 9185 goto out; 9186 9187 list_for_each_entry(clki, head, list) { 9188 if (!clki->name) 9189 continue; 9190 9191 clki->clk = devm_clk_get(dev, clki->name); 9192 if (IS_ERR(clki->clk)) { 9193 ret = PTR_ERR(clki->clk); 9194 dev_err(dev, "%s: %s clk get failed, %d\n", 9195 __func__, clki->name, ret); 9196 goto out; 9197 } 9198 9199 /* 9200 * Parse device ref clk freq as per device tree "ref_clk". 9201 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL 9202 * in ufshcd_alloc_host(). 9203 */ 9204 if (!strcmp(clki->name, "ref_clk")) 9205 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk); 9206 9207 if (clki->max_freq) { 9208 ret = clk_set_rate(clki->clk, clki->max_freq); 9209 if (ret) { 9210 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", 9211 __func__, clki->name, 9212 clki->max_freq, ret); 9213 goto out; 9214 } 9215 clki->curr_freq = clki->max_freq; 9216 } 9217 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__, 9218 clki->name, clk_get_rate(clki->clk)); 9219 } 9220 9221 /* Set Max. frequency for all clocks */ 9222 if (hba->use_pm_opp) { 9223 ret = ufshcd_opp_set_rate(hba, ULONG_MAX); 9224 if (ret) { 9225 dev_err(hba->dev, "%s: failed to set OPP: %d", __func__, 9226 ret); 9227 goto out; 9228 } 9229 } 9230 9231 out: 9232 return ret; 9233 } 9234 9235 static int ufshcd_variant_hba_init(struct ufs_hba *hba) 9236 { 9237 int err = 0; 9238 9239 if (!hba->vops) 9240 goto out; 9241 9242 err = ufshcd_vops_init(hba); 9243 if (err) 9244 dev_err_probe(hba->dev, err, 9245 "%s: variant %s init failed with err %d\n", 9246 __func__, ufshcd_get_var_name(hba), err); 9247 out: 9248 return err; 9249 } 9250 9251 static void ufshcd_variant_hba_exit(struct ufs_hba *hba) 9252 { 9253 if (!hba->vops) 9254 return; 9255 9256 ufshcd_vops_exit(hba); 9257 } 9258 9259 static int ufshcd_hba_init(struct ufs_hba *hba) 9260 { 9261 int err; 9262 9263 /* 9264 * Handle host controller power separately from the UFS device power 9265 * rails as it will help controlling the UFS host controller power 9266 * collapse easily which is different than UFS device power collapse. 9267 * Also, enable the host controller power before we go ahead with rest 9268 * of the initialization here. 9269 */ 9270 err = ufshcd_init_hba_vreg(hba); 9271 if (err) 9272 goto out; 9273 9274 err = ufshcd_setup_hba_vreg(hba, true); 9275 if (err) 9276 goto out; 9277 9278 err = ufshcd_init_clocks(hba); 9279 if (err) 9280 goto out_disable_hba_vreg; 9281 9282 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) 9283 hba->dev_ref_clk_freq = ufshcd_parse_ref_clk_property(hba); 9284 9285 err = ufshcd_setup_clocks(hba, true); 9286 if (err) 9287 goto out_disable_hba_vreg; 9288 9289 err = ufshcd_init_vreg(hba); 9290 if (err) 9291 goto out_disable_clks; 9292 9293 err = ufshcd_setup_vreg(hba, true); 9294 if (err) 9295 goto out_disable_clks; 9296 9297 err = ufshcd_variant_hba_init(hba); 9298 if (err) 9299 goto out_disable_vreg; 9300 9301 ufs_debugfs_hba_init(hba); 9302 ufs_fault_inject_hba_init(hba); 9303 9304 hba->is_powered = true; 9305 goto out; 9306 9307 out_disable_vreg: 9308 ufshcd_setup_vreg(hba, false); 9309 out_disable_clks: 9310 ufshcd_setup_clocks(hba, false); 9311 out_disable_hba_vreg: 9312 ufshcd_setup_hba_vreg(hba, false); 9313 out: 9314 return err; 9315 } 9316 9317 static void ufshcd_hba_exit(struct ufs_hba *hba) 9318 { 9319 if (hba->is_powered) { 9320 ufshcd_pm_qos_exit(hba); 9321 ufshcd_exit_clk_scaling(hba); 9322 ufshcd_exit_clk_gating(hba); 9323 if (hba->eh_wq) 9324 destroy_workqueue(hba->eh_wq); 9325 ufs_debugfs_hba_exit(hba); 9326 ufshcd_variant_hba_exit(hba); 9327 ufshcd_setup_vreg(hba, false); 9328 ufshcd_setup_clocks(hba, false); 9329 ufshcd_setup_hba_vreg(hba, false); 9330 hba->is_powered = false; 9331 ufs_put_device_desc(hba); 9332 } 9333 } 9334 9335 static int ufshcd_execute_start_stop(struct scsi_device *sdev, 9336 enum ufs_dev_pwr_mode pwr_mode, 9337 struct scsi_sense_hdr *sshdr) 9338 { 9339 const unsigned char cdb[6] = { START_STOP, 0, 0, 0, pwr_mode << 4, 0 }; 9340 struct scsi_failure failure_defs[] = { 9341 { 9342 .allowed = 2, 9343 .result = SCMD_FAILURE_RESULT_ANY, 9344 }, 9345 }; 9346 struct scsi_failures failures = { 9347 .failure_definitions = failure_defs, 9348 }; 9349 const struct scsi_exec_args args = { 9350 .failures = &failures, 9351 .sshdr = sshdr, 9352 .req_flags = BLK_MQ_REQ_PM, 9353 .scmd_flags = SCMD_FAIL_IF_RECOVERING, 9354 }; 9355 9356 return scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, /*buffer=*/NULL, 9357 /*bufflen=*/0, /*timeout=*/10 * HZ, /*retries=*/0, 9358 &args); 9359 } 9360 9361 /** 9362 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device 9363 * power mode 9364 * @hba: per adapter instance 9365 * @pwr_mode: device power mode to set 9366 * 9367 * Return: 0 if requested power mode is set successfully; 9368 * < 0 if failed to set the requested power mode. 9369 */ 9370 static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, 9371 enum ufs_dev_pwr_mode pwr_mode) 9372 { 9373 struct scsi_sense_hdr sshdr; 9374 struct scsi_device *sdp; 9375 unsigned long flags; 9376 int ret; 9377 9378 spin_lock_irqsave(hba->host->host_lock, flags); 9379 sdp = hba->ufs_device_wlun; 9380 if (sdp && scsi_device_online(sdp)) 9381 ret = scsi_device_get(sdp); 9382 else 9383 ret = -ENODEV; 9384 spin_unlock_irqrestore(hba->host->host_lock, flags); 9385 9386 if (ret) 9387 return ret; 9388 9389 /* 9390 * If scsi commands fail, the scsi mid-layer schedules scsi error- 9391 * handling, which would wait for host to be resumed. Since we know 9392 * we are functional while we are here, skip host resume in error 9393 * handling context. 9394 */ 9395 hba->host->eh_noresume = 1; 9396 9397 /* 9398 * Current function would be generally called from the power management 9399 * callbacks hence set the RQF_PM flag so that it doesn't resume the 9400 * already suspended childs. 9401 */ 9402 ret = ufshcd_execute_start_stop(sdp, pwr_mode, &sshdr); 9403 if (ret) { 9404 sdev_printk(KERN_WARNING, sdp, 9405 "START_STOP failed for power mode: %d, result %x\n", 9406 pwr_mode, ret); 9407 if (ret > 0) { 9408 if (scsi_sense_valid(&sshdr)) 9409 scsi_print_sense_hdr(sdp, NULL, &sshdr); 9410 ret = -EIO; 9411 } 9412 } else { 9413 hba->curr_dev_pwr_mode = pwr_mode; 9414 } 9415 9416 scsi_device_put(sdp); 9417 hba->host->eh_noresume = 0; 9418 return ret; 9419 } 9420 9421 static int ufshcd_link_state_transition(struct ufs_hba *hba, 9422 enum uic_link_state req_link_state, 9423 bool check_for_bkops) 9424 { 9425 int ret = 0; 9426 9427 if (req_link_state == hba->uic_link_state) 9428 return 0; 9429 9430 if (req_link_state == UIC_LINK_HIBERN8_STATE) { 9431 ret = ufshcd_uic_hibern8_enter(hba); 9432 if (!ret) { 9433 ufshcd_set_link_hibern8(hba); 9434 } else { 9435 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", 9436 __func__, ret); 9437 goto out; 9438 } 9439 } 9440 /* 9441 * If autobkops is enabled, link can't be turned off because 9442 * turning off the link would also turn off the device, except in the 9443 * case of DeepSleep where the device is expected to remain powered. 9444 */ 9445 else if ((req_link_state == UIC_LINK_OFF_STATE) && 9446 (!check_for_bkops || !hba->auto_bkops_enabled)) { 9447 /* 9448 * Let's make sure that link is in low power mode, we are doing 9449 * this currently by putting the link in Hibern8. Otherway to 9450 * put the link in low power mode is to send the DME end point 9451 * to device and then send the DME reset command to local 9452 * unipro. But putting the link in hibern8 is much faster. 9453 * 9454 * Note also that putting the link in Hibern8 is a requirement 9455 * for entering DeepSleep. 9456 */ 9457 ret = ufshcd_uic_hibern8_enter(hba); 9458 if (ret) { 9459 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", 9460 __func__, ret); 9461 goto out; 9462 } 9463 /* 9464 * Change controller state to "reset state" which 9465 * should also put the link in off/reset state 9466 */ 9467 ufshcd_hba_stop(hba); 9468 /* 9469 * TODO: Check if we need any delay to make sure that 9470 * controller is reset 9471 */ 9472 ufshcd_set_link_off(hba); 9473 } 9474 9475 out: 9476 return ret; 9477 } 9478 9479 static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) 9480 { 9481 bool vcc_off = false; 9482 9483 /* 9484 * It seems some UFS devices may keep drawing more than sleep current 9485 * (atleast for 500us) from UFS rails (especially from VCCQ rail). 9486 * To avoid this situation, add 2ms delay before putting these UFS 9487 * rails in LPM mode. 9488 */ 9489 if (!ufshcd_is_link_active(hba) && 9490 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) 9491 usleep_range(2000, 2100); 9492 9493 /* 9494 * If UFS device is either in UFS_Sleep turn off VCC rail to save some 9495 * power. 9496 * 9497 * If UFS device and link is in OFF state, all power supplies (VCC, 9498 * VCCQ, VCCQ2) can be turned off if power on write protect is not 9499 * required. If UFS link is inactive (Hibern8 or OFF state) and device 9500 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode. 9501 * 9502 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway 9503 * in low power state which would save some power. 9504 * 9505 * If Write Booster is enabled and the device needs to flush the WB 9506 * buffer OR if bkops status is urgent for WB, keep Vcc on. 9507 */ 9508 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && 9509 !hba->dev_info.is_lu_power_on_wp) { 9510 ufshcd_setup_vreg(hba, false); 9511 vcc_off = true; 9512 } else if (!ufshcd_is_ufs_dev_active(hba)) { 9513 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); 9514 vcc_off = true; 9515 if (ufshcd_is_link_hibern8(hba) || ufshcd_is_link_off(hba)) { 9516 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); 9517 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); 9518 } 9519 } 9520 9521 /* 9522 * Some UFS devices require delay after VCC power rail is turned-off. 9523 */ 9524 if (vcc_off && hba->vreg_info.vcc && 9525 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM) 9526 usleep_range(5000, 5100); 9527 } 9528 9529 #ifdef CONFIG_PM 9530 static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) 9531 { 9532 int ret = 0; 9533 9534 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && 9535 !hba->dev_info.is_lu_power_on_wp) { 9536 ret = ufshcd_setup_vreg(hba, true); 9537 } else if (!ufshcd_is_ufs_dev_active(hba)) { 9538 if (!ufshcd_is_link_active(hba)) { 9539 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); 9540 if (ret) 9541 goto vcc_disable; 9542 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); 9543 if (ret) 9544 goto vccq_lpm; 9545 } 9546 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); 9547 } 9548 goto out; 9549 9550 vccq_lpm: 9551 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); 9552 vcc_disable: 9553 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); 9554 out: 9555 return ret; 9556 } 9557 #endif /* CONFIG_PM */ 9558 9559 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) 9560 { 9561 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba)) 9562 ufshcd_setup_hba_vreg(hba, false); 9563 } 9564 9565 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) 9566 { 9567 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba)) 9568 ufshcd_setup_hba_vreg(hba, true); 9569 } 9570 9571 static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) 9572 { 9573 int ret = 0; 9574 bool check_for_bkops; 9575 enum ufs_pm_level pm_lvl; 9576 enum ufs_dev_pwr_mode req_dev_pwr_mode; 9577 enum uic_link_state req_link_state; 9578 9579 hba->pm_op_in_progress = true; 9580 if (pm_op != UFS_SHUTDOWN_PM) { 9581 pm_lvl = pm_op == UFS_RUNTIME_PM ? 9582 hba->rpm_lvl : hba->spm_lvl; 9583 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl); 9584 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl); 9585 } else { 9586 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE; 9587 req_link_state = UIC_LINK_OFF_STATE; 9588 } 9589 9590 /* 9591 * If we can't transition into any of the low power modes 9592 * just gate the clocks. 9593 */ 9594 ufshcd_hold(hba); 9595 hba->clk_gating.is_suspended = true; 9596 9597 if (ufshcd_is_clkscaling_supported(hba)) 9598 ufshcd_clk_scaling_suspend(hba, true); 9599 9600 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE && 9601 req_link_state == UIC_LINK_ACTIVE_STATE) { 9602 goto vops_suspend; 9603 } 9604 9605 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && 9606 (req_link_state == hba->uic_link_state)) 9607 goto enable_scaling; 9608 9609 /* UFS device & link must be active before we enter in this function */ 9610 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { 9611 /* Wait err handler finish or trigger err recovery */ 9612 if (!ufshcd_eh_in_progress(hba)) 9613 ufshcd_force_error_recovery(hba); 9614 ret = -EBUSY; 9615 goto enable_scaling; 9616 } 9617 9618 if (pm_op == UFS_RUNTIME_PM) { 9619 if (ufshcd_can_autobkops_during_suspend(hba)) { 9620 /* 9621 * The device is idle with no requests in the queue, 9622 * allow background operations if bkops status shows 9623 * that performance might be impacted. 9624 */ 9625 ret = ufshcd_urgent_bkops(hba); 9626 if (ret) { 9627 /* 9628 * If return err in suspend flow, IO will hang. 9629 * Trigger error handler and break suspend for 9630 * error recovery. 9631 */ 9632 ufshcd_force_error_recovery(hba); 9633 ret = -EBUSY; 9634 goto enable_scaling; 9635 } 9636 } else { 9637 /* make sure that auto bkops is disabled */ 9638 ufshcd_disable_auto_bkops(hba); 9639 } 9640 /* 9641 * If device needs to do BKOP or WB buffer flush during 9642 * Hibern8, keep device power mode as "active power mode" 9643 * and VCC supply. 9644 */ 9645 hba->dev_info.b_rpm_dev_flush_capable = 9646 hba->auto_bkops_enabled || 9647 (((req_link_state == UIC_LINK_HIBERN8_STATE) || 9648 ((req_link_state == UIC_LINK_ACTIVE_STATE) && 9649 ufshcd_is_auto_hibern8_enabled(hba))) && 9650 ufshcd_wb_need_flush(hba)); 9651 } 9652 9653 flush_work(&hba->eeh_work); 9654 9655 ret = ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE); 9656 if (ret) 9657 goto enable_scaling; 9658 9659 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) { 9660 if (pm_op != UFS_RUNTIME_PM) 9661 /* ensure that bkops is disabled */ 9662 ufshcd_disable_auto_bkops(hba); 9663 9664 if (!hba->dev_info.b_rpm_dev_flush_capable) { 9665 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); 9666 if (ret && pm_op != UFS_SHUTDOWN_PM) { 9667 /* 9668 * If return err in suspend flow, IO will hang. 9669 * Trigger error handler and break suspend for 9670 * error recovery. 9671 */ 9672 ufshcd_force_error_recovery(hba); 9673 ret = -EBUSY; 9674 } 9675 if (ret) 9676 goto enable_scaling; 9677 } 9678 } 9679 9680 /* 9681 * In the case of DeepSleep, the device is expected to remain powered 9682 * with the link off, so do not check for bkops. 9683 */ 9684 check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba); 9685 ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops); 9686 if (ret && pm_op != UFS_SHUTDOWN_PM) { 9687 /* 9688 * If return err in suspend flow, IO will hang. 9689 * Trigger error handler and break suspend for 9690 * error recovery. 9691 */ 9692 ufshcd_force_error_recovery(hba); 9693 ret = -EBUSY; 9694 } 9695 if (ret) 9696 goto set_dev_active; 9697 9698 vops_suspend: 9699 /* 9700 * Call vendor specific suspend callback. As these callbacks may access 9701 * vendor specific host controller register space call them before the 9702 * host clocks are ON. 9703 */ 9704 ret = ufshcd_vops_suspend(hba, pm_op, POST_CHANGE); 9705 if (ret) 9706 goto set_link_active; 9707 9708 cancel_delayed_work_sync(&hba->ufs_rtc_update_work); 9709 goto out; 9710 9711 set_link_active: 9712 /* 9713 * Device hardware reset is required to exit DeepSleep. Also, for 9714 * DeepSleep, the link is off so host reset and restore will be done 9715 * further below. 9716 */ 9717 if (ufshcd_is_ufs_dev_deepsleep(hba)) { 9718 ufshcd_device_reset(hba); 9719 WARN_ON(!ufshcd_is_link_off(hba)); 9720 } 9721 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) 9722 ufshcd_set_link_active(hba); 9723 else if (ufshcd_is_link_off(hba)) 9724 ufshcd_host_reset_and_restore(hba); 9725 set_dev_active: 9726 /* Can also get here needing to exit DeepSleep */ 9727 if (ufshcd_is_ufs_dev_deepsleep(hba)) { 9728 ufshcd_device_reset(hba); 9729 ufshcd_host_reset_and_restore(hba); 9730 } 9731 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) 9732 ufshcd_disable_auto_bkops(hba); 9733 enable_scaling: 9734 if (ufshcd_is_clkscaling_supported(hba)) 9735 ufshcd_clk_scaling_suspend(hba, false); 9736 9737 hba->dev_info.b_rpm_dev_flush_capable = false; 9738 out: 9739 if (hba->dev_info.b_rpm_dev_flush_capable) { 9740 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work, 9741 msecs_to_jiffies(RPM_DEV_FLUSH_RECHECK_WORK_DELAY_MS)); 9742 } 9743 9744 if (ret) { 9745 ufshcd_update_evt_hist(hba, UFS_EVT_WL_SUSP_ERR, (u32)ret); 9746 hba->clk_gating.is_suspended = false; 9747 ufshcd_release(hba); 9748 } 9749 hba->pm_op_in_progress = false; 9750 return ret; 9751 } 9752 9753 #ifdef CONFIG_PM 9754 static int __ufshcd_wl_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) 9755 { 9756 int ret; 9757 enum uic_link_state old_link_state = hba->uic_link_state; 9758 9759 hba->pm_op_in_progress = true; 9760 9761 /* 9762 * Call vendor specific resume callback. As these callbacks may access 9763 * vendor specific host controller register space call them when the 9764 * host clocks are ON. 9765 */ 9766 ret = ufshcd_vops_resume(hba, pm_op); 9767 if (ret) 9768 goto out; 9769 9770 /* For DeepSleep, the only supported option is to have the link off */ 9771 WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba)); 9772 9773 if (ufshcd_is_link_hibern8(hba)) { 9774 ret = ufshcd_uic_hibern8_exit(hba); 9775 if (!ret) { 9776 ufshcd_set_link_active(hba); 9777 } else { 9778 dev_err(hba->dev, "%s: hibern8 exit failed %d\n", 9779 __func__, ret); 9780 goto vendor_suspend; 9781 } 9782 } else if (ufshcd_is_link_off(hba)) { 9783 /* 9784 * A full initialization of the host and the device is 9785 * required since the link was put to off during suspend. 9786 * Note, in the case of DeepSleep, the device will exit 9787 * DeepSleep due to device reset. 9788 */ 9789 ret = ufshcd_reset_and_restore(hba); 9790 /* 9791 * ufshcd_reset_and_restore() should have already 9792 * set the link state as active 9793 */ 9794 if (ret || !ufshcd_is_link_active(hba)) 9795 goto vendor_suspend; 9796 } 9797 9798 if (!ufshcd_is_ufs_dev_active(hba)) { 9799 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); 9800 if (ret) 9801 goto set_old_link_state; 9802 ufshcd_set_timestamp_attr(hba); 9803 schedule_delayed_work(&hba->ufs_rtc_update_work, 9804 msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS)); 9805 } 9806 9807 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) 9808 ufshcd_enable_auto_bkops(hba); 9809 else 9810 /* 9811 * If BKOPs operations are urgently needed at this moment then 9812 * keep auto-bkops enabled or else disable it. 9813 */ 9814 ufshcd_urgent_bkops(hba); 9815 9816 if (hba->ee_usr_mask) 9817 ufshcd_write_ee_control(hba); 9818 9819 if (ufshcd_is_clkscaling_supported(hba)) 9820 ufshcd_clk_scaling_suspend(hba, false); 9821 9822 if (hba->dev_info.b_rpm_dev_flush_capable) { 9823 hba->dev_info.b_rpm_dev_flush_capable = false; 9824 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work); 9825 } 9826 9827 ufshcd_configure_auto_hibern8(hba); 9828 9829 goto out; 9830 9831 set_old_link_state: 9832 ufshcd_link_state_transition(hba, old_link_state, 0); 9833 vendor_suspend: 9834 ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE); 9835 ufshcd_vops_suspend(hba, pm_op, POST_CHANGE); 9836 out: 9837 if (ret) 9838 ufshcd_update_evt_hist(hba, UFS_EVT_WL_RES_ERR, (u32)ret); 9839 hba->clk_gating.is_suspended = false; 9840 ufshcd_release(hba); 9841 hba->pm_op_in_progress = false; 9842 return ret; 9843 } 9844 9845 static int ufshcd_wl_runtime_suspend(struct device *dev) 9846 { 9847 struct scsi_device *sdev = to_scsi_device(dev); 9848 struct ufs_hba *hba; 9849 int ret; 9850 ktime_t start = ktime_get(); 9851 9852 hba = shost_priv(sdev->host); 9853 9854 ret = __ufshcd_wl_suspend(hba, UFS_RUNTIME_PM); 9855 if (ret) 9856 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); 9857 9858 trace_ufshcd_wl_runtime_suspend(dev_name(dev), ret, 9859 ktime_to_us(ktime_sub(ktime_get(), start)), 9860 hba->curr_dev_pwr_mode, hba->uic_link_state); 9861 9862 return ret; 9863 } 9864 9865 static int ufshcd_wl_runtime_resume(struct device *dev) 9866 { 9867 struct scsi_device *sdev = to_scsi_device(dev); 9868 struct ufs_hba *hba; 9869 int ret = 0; 9870 ktime_t start = ktime_get(); 9871 9872 hba = shost_priv(sdev->host); 9873 9874 ret = __ufshcd_wl_resume(hba, UFS_RUNTIME_PM); 9875 if (ret) 9876 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); 9877 9878 trace_ufshcd_wl_runtime_resume(dev_name(dev), ret, 9879 ktime_to_us(ktime_sub(ktime_get(), start)), 9880 hba->curr_dev_pwr_mode, hba->uic_link_state); 9881 9882 return ret; 9883 } 9884 #endif 9885 9886 #ifdef CONFIG_PM_SLEEP 9887 static int ufshcd_wl_suspend(struct device *dev) 9888 { 9889 struct scsi_device *sdev = to_scsi_device(dev); 9890 struct ufs_hba *hba; 9891 int ret = 0; 9892 ktime_t start = ktime_get(); 9893 9894 hba = shost_priv(sdev->host); 9895 down(&hba->host_sem); 9896 hba->system_suspending = true; 9897 9898 if (pm_runtime_suspended(dev)) 9899 goto out; 9900 9901 ret = __ufshcd_wl_suspend(hba, UFS_SYSTEM_PM); 9902 if (ret) { 9903 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); 9904 up(&hba->host_sem); 9905 } 9906 9907 out: 9908 if (!ret) 9909 hba->is_sys_suspended = true; 9910 trace_ufshcd_wl_suspend(dev_name(dev), ret, 9911 ktime_to_us(ktime_sub(ktime_get(), start)), 9912 hba->curr_dev_pwr_mode, hba->uic_link_state); 9913 9914 return ret; 9915 } 9916 9917 static int ufshcd_wl_resume(struct device *dev) 9918 { 9919 struct scsi_device *sdev = to_scsi_device(dev); 9920 struct ufs_hba *hba; 9921 int ret = 0; 9922 ktime_t start = ktime_get(); 9923 9924 hba = shost_priv(sdev->host); 9925 9926 if (pm_runtime_suspended(dev)) 9927 goto out; 9928 9929 ret = __ufshcd_wl_resume(hba, UFS_SYSTEM_PM); 9930 if (ret) 9931 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); 9932 out: 9933 trace_ufshcd_wl_resume(dev_name(dev), ret, 9934 ktime_to_us(ktime_sub(ktime_get(), start)), 9935 hba->curr_dev_pwr_mode, hba->uic_link_state); 9936 if (!ret) 9937 hba->is_sys_suspended = false; 9938 hba->system_suspending = false; 9939 up(&hba->host_sem); 9940 return ret; 9941 } 9942 #endif 9943 9944 /** 9945 * ufshcd_suspend - helper function for suspend operations 9946 * @hba: per adapter instance 9947 * 9948 * This function will put disable irqs, turn off clocks 9949 * and set vreg and hba-vreg in lpm mode. 9950 * 9951 * Return: 0 upon success; < 0 upon failure. 9952 */ 9953 static int ufshcd_suspend(struct ufs_hba *hba) 9954 { 9955 int ret; 9956 9957 if (!hba->is_powered) 9958 return 0; 9959 /* 9960 * Disable the host irq as host controller as there won't be any 9961 * host controller transaction expected till resume. 9962 */ 9963 ufshcd_disable_irq(hba); 9964 ret = ufshcd_setup_clocks(hba, false); 9965 if (ret) { 9966 ufshcd_enable_irq(hba); 9967 return ret; 9968 } 9969 if (ufshcd_is_clkgating_allowed(hba)) { 9970 hba->clk_gating.state = CLKS_OFF; 9971 trace_ufshcd_clk_gating(dev_name(hba->dev), 9972 hba->clk_gating.state); 9973 } 9974 9975 ufshcd_vreg_set_lpm(hba); 9976 /* Put the host controller in low power mode if possible */ 9977 ufshcd_hba_vreg_set_lpm(hba); 9978 ufshcd_pm_qos_update(hba, false); 9979 return ret; 9980 } 9981 9982 #ifdef CONFIG_PM 9983 /** 9984 * ufshcd_resume - helper function for resume operations 9985 * @hba: per adapter instance 9986 * 9987 * This function basically turns on the regulators, clocks and 9988 * irqs of the hba. 9989 * 9990 * Return: 0 for success and non-zero for failure. 9991 */ 9992 static int ufshcd_resume(struct ufs_hba *hba) 9993 { 9994 int ret; 9995 9996 if (!hba->is_powered) 9997 return 0; 9998 9999 ufshcd_hba_vreg_set_hpm(hba); 10000 ret = ufshcd_vreg_set_hpm(hba); 10001 if (ret) 10002 goto out; 10003 10004 /* Make sure clocks are enabled before accessing controller */ 10005 ret = ufshcd_setup_clocks(hba, true); 10006 if (ret) 10007 goto disable_vreg; 10008 10009 /* enable the host irq as host controller would be active soon */ 10010 ufshcd_enable_irq(hba); 10011 10012 goto out; 10013 10014 disable_vreg: 10015 ufshcd_vreg_set_lpm(hba); 10016 out: 10017 if (ret) 10018 ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)ret); 10019 return ret; 10020 } 10021 #endif /* CONFIG_PM */ 10022 10023 #ifdef CONFIG_PM_SLEEP 10024 /** 10025 * ufshcd_system_suspend - system suspend callback 10026 * @dev: Device associated with the UFS controller. 10027 * 10028 * Executed before putting the system into a sleep state in which the contents 10029 * of main memory are preserved. 10030 * 10031 * Return: 0 for success and non-zero for failure. 10032 */ 10033 int ufshcd_system_suspend(struct device *dev) 10034 { 10035 struct ufs_hba *hba = dev_get_drvdata(dev); 10036 int ret = 0; 10037 ktime_t start = ktime_get(); 10038 10039 if (pm_runtime_suspended(hba->dev)) 10040 goto out; 10041 10042 ret = ufshcd_suspend(hba); 10043 out: 10044 trace_ufshcd_system_suspend(dev_name(hba->dev), ret, 10045 ktime_to_us(ktime_sub(ktime_get(), start)), 10046 hba->curr_dev_pwr_mode, hba->uic_link_state); 10047 return ret; 10048 } 10049 EXPORT_SYMBOL(ufshcd_system_suspend); 10050 10051 /** 10052 * ufshcd_system_resume - system resume callback 10053 * @dev: Device associated with the UFS controller. 10054 * 10055 * Executed after waking the system up from a sleep state in which the contents 10056 * of main memory were preserved. 10057 * 10058 * Return: 0 for success and non-zero for failure. 10059 */ 10060 int ufshcd_system_resume(struct device *dev) 10061 { 10062 struct ufs_hba *hba = dev_get_drvdata(dev); 10063 ktime_t start = ktime_get(); 10064 int ret = 0; 10065 10066 if (pm_runtime_suspended(hba->dev)) 10067 goto out; 10068 10069 ret = ufshcd_resume(hba); 10070 10071 out: 10072 trace_ufshcd_system_resume(dev_name(hba->dev), ret, 10073 ktime_to_us(ktime_sub(ktime_get(), start)), 10074 hba->curr_dev_pwr_mode, hba->uic_link_state); 10075 10076 return ret; 10077 } 10078 EXPORT_SYMBOL(ufshcd_system_resume); 10079 #endif /* CONFIG_PM_SLEEP */ 10080 10081 #ifdef CONFIG_PM 10082 /** 10083 * ufshcd_runtime_suspend - runtime suspend callback 10084 * @dev: Device associated with the UFS controller. 10085 * 10086 * Check the description of ufshcd_suspend() function for more details. 10087 * 10088 * Return: 0 for success and non-zero for failure. 10089 */ 10090 int ufshcd_runtime_suspend(struct device *dev) 10091 { 10092 struct ufs_hba *hba = dev_get_drvdata(dev); 10093 int ret; 10094 ktime_t start = ktime_get(); 10095 10096 ret = ufshcd_suspend(hba); 10097 10098 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, 10099 ktime_to_us(ktime_sub(ktime_get(), start)), 10100 hba->curr_dev_pwr_mode, hba->uic_link_state); 10101 return ret; 10102 } 10103 EXPORT_SYMBOL(ufshcd_runtime_suspend); 10104 10105 /** 10106 * ufshcd_runtime_resume - runtime resume routine 10107 * @dev: Device associated with the UFS controller. 10108 * 10109 * This function basically brings controller 10110 * to active state. Following operations are done in this function: 10111 * 10112 * 1. Turn on all the controller related clocks 10113 * 2. Turn ON VCC rail 10114 * 10115 * Return: 0 upon success; < 0 upon failure. 10116 */ 10117 int ufshcd_runtime_resume(struct device *dev) 10118 { 10119 struct ufs_hba *hba = dev_get_drvdata(dev); 10120 int ret; 10121 ktime_t start = ktime_get(); 10122 10123 ret = ufshcd_resume(hba); 10124 10125 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, 10126 ktime_to_us(ktime_sub(ktime_get(), start)), 10127 hba->curr_dev_pwr_mode, hba->uic_link_state); 10128 return ret; 10129 } 10130 EXPORT_SYMBOL(ufshcd_runtime_resume); 10131 #endif /* CONFIG_PM */ 10132 10133 static void ufshcd_wl_shutdown(struct device *dev) 10134 { 10135 struct scsi_device *sdev = to_scsi_device(dev); 10136 struct ufs_hba *hba = shost_priv(sdev->host); 10137 10138 down(&hba->host_sem); 10139 hba->shutting_down = true; 10140 up(&hba->host_sem); 10141 10142 /* Turn on everything while shutting down */ 10143 ufshcd_rpm_get_sync(hba); 10144 scsi_device_quiesce(sdev); 10145 shost_for_each_device(sdev, hba->host) { 10146 if (sdev == hba->ufs_device_wlun) 10147 continue; 10148 scsi_device_quiesce(sdev); 10149 } 10150 __ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM); 10151 10152 /* 10153 * Next, turn off the UFS controller and the UFS regulators. Disable 10154 * clocks. 10155 */ 10156 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) 10157 ufshcd_suspend(hba); 10158 10159 hba->is_powered = false; 10160 } 10161 10162 /** 10163 * ufshcd_remove - de-allocate SCSI host and host memory space 10164 * data structure memory 10165 * @hba: per adapter instance 10166 */ 10167 void ufshcd_remove(struct ufs_hba *hba) 10168 { 10169 if (hba->ufs_device_wlun) 10170 ufshcd_rpm_get_sync(hba); 10171 ufs_hwmon_remove(hba); 10172 ufs_bsg_remove(hba); 10173 ufs_sysfs_remove_nodes(hba->dev); 10174 blk_mq_destroy_queue(hba->tmf_queue); 10175 blk_put_queue(hba->tmf_queue); 10176 blk_mq_free_tag_set(&hba->tmf_tag_set); 10177 scsi_remove_host(hba->host); 10178 /* disable interrupts */ 10179 ufshcd_disable_intr(hba, hba->intr_mask); 10180 ufshcd_hba_stop(hba); 10181 ufshcd_hba_exit(hba); 10182 } 10183 EXPORT_SYMBOL_GPL(ufshcd_remove); 10184 10185 #ifdef CONFIG_PM_SLEEP 10186 int ufshcd_system_freeze(struct device *dev) 10187 { 10188 10189 return ufshcd_system_suspend(dev); 10190 10191 } 10192 EXPORT_SYMBOL_GPL(ufshcd_system_freeze); 10193 10194 int ufshcd_system_restore(struct device *dev) 10195 { 10196 10197 struct ufs_hba *hba = dev_get_drvdata(dev); 10198 int ret; 10199 10200 ret = ufshcd_system_resume(dev); 10201 if (ret) 10202 return ret; 10203 10204 /* Configure UTRL and UTMRL base address registers */ 10205 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), 10206 REG_UTP_TRANSFER_REQ_LIST_BASE_L); 10207 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), 10208 REG_UTP_TRANSFER_REQ_LIST_BASE_H); 10209 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), 10210 REG_UTP_TASK_REQ_LIST_BASE_L); 10211 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), 10212 REG_UTP_TASK_REQ_LIST_BASE_H); 10213 /* 10214 * Make sure that UTRL and UTMRL base address registers 10215 * are updated with the latest queue addresses. Only after 10216 * updating these addresses, we can queue the new commands. 10217 */ 10218 ufshcd_readl(hba, REG_UTP_TASK_REQ_LIST_BASE_H); 10219 10220 /* Resuming from hibernate, assume that link was OFF */ 10221 ufshcd_set_link_off(hba); 10222 10223 return 0; 10224 10225 } 10226 EXPORT_SYMBOL_GPL(ufshcd_system_restore); 10227 10228 int ufshcd_system_thaw(struct device *dev) 10229 { 10230 return ufshcd_system_resume(dev); 10231 } 10232 EXPORT_SYMBOL_GPL(ufshcd_system_thaw); 10233 #endif /* CONFIG_PM_SLEEP */ 10234 10235 /** 10236 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA) 10237 * @hba: pointer to Host Bus Adapter (HBA) 10238 */ 10239 void ufshcd_dealloc_host(struct ufs_hba *hba) 10240 { 10241 scsi_host_put(hba->host); 10242 } 10243 EXPORT_SYMBOL_GPL(ufshcd_dealloc_host); 10244 10245 /** 10246 * ufshcd_set_dma_mask - Set dma mask based on the controller 10247 * addressing capability 10248 * @hba: per adapter instance 10249 * 10250 * Return: 0 for success, non-zero for failure. 10251 */ 10252 static int ufshcd_set_dma_mask(struct ufs_hba *hba) 10253 { 10254 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { 10255 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) 10256 return 0; 10257 } 10258 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); 10259 } 10260 10261 /** 10262 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA) 10263 * @dev: pointer to device handle 10264 * @hba_handle: driver private handle 10265 * 10266 * Return: 0 on success, non-zero value on failure. 10267 */ 10268 int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle) 10269 { 10270 struct Scsi_Host *host; 10271 struct ufs_hba *hba; 10272 int err = 0; 10273 10274 if (!dev) { 10275 dev_err(dev, 10276 "Invalid memory reference for dev is NULL\n"); 10277 err = -ENODEV; 10278 goto out_error; 10279 } 10280 10281 host = scsi_host_alloc(&ufshcd_driver_template, 10282 sizeof(struct ufs_hba)); 10283 if (!host) { 10284 dev_err(dev, "scsi_host_alloc failed\n"); 10285 err = -ENOMEM; 10286 goto out_error; 10287 } 10288 host->nr_maps = HCTX_TYPE_POLL + 1; 10289 hba = shost_priv(host); 10290 hba->host = host; 10291 hba->dev = dev; 10292 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL; 10293 hba->nop_out_timeout = NOP_OUT_TIMEOUT; 10294 ufshcd_set_sg_entry_size(hba, sizeof(struct ufshcd_sg_entry)); 10295 INIT_LIST_HEAD(&hba->clk_list_head); 10296 spin_lock_init(&hba->outstanding_lock); 10297 10298 *hba_handle = hba; 10299 10300 out_error: 10301 return err; 10302 } 10303 EXPORT_SYMBOL(ufshcd_alloc_host); 10304 10305 /* This function exists because blk_mq_alloc_tag_set() requires this. */ 10306 static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx, 10307 const struct blk_mq_queue_data *qd) 10308 { 10309 WARN_ON_ONCE(true); 10310 return BLK_STS_NOTSUPP; 10311 } 10312 10313 static const struct blk_mq_ops ufshcd_tmf_ops = { 10314 .queue_rq = ufshcd_queue_tmf, 10315 }; 10316 10317 /** 10318 * ufshcd_init - Driver initialization routine 10319 * @hba: per-adapter instance 10320 * @mmio_base: base register address 10321 * @irq: Interrupt line of device 10322 * 10323 * Return: 0 on success, non-zero value on failure. 10324 */ 10325 int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) 10326 { 10327 int err; 10328 struct Scsi_Host *host = hba->host; 10329 struct device *dev = hba->dev; 10330 char eh_wq_name[sizeof("ufs_eh_wq_00")]; 10331 10332 /* 10333 * dev_set_drvdata() must be called before any callbacks are registered 10334 * that use dev_get_drvdata() (frequency scaling, clock scaling, hwmon, 10335 * sysfs). 10336 */ 10337 dev_set_drvdata(dev, hba); 10338 10339 if (!mmio_base) { 10340 dev_err(hba->dev, 10341 "Invalid memory reference for mmio_base is NULL\n"); 10342 err = -ENODEV; 10343 goto out_error; 10344 } 10345 10346 hba->mmio_base = mmio_base; 10347 hba->irq = irq; 10348 hba->vps = &ufs_hba_vps; 10349 10350 err = ufshcd_hba_init(hba); 10351 if (err) 10352 goto out_error; 10353 10354 /* Read capabilities registers */ 10355 err = ufshcd_hba_capabilities(hba); 10356 if (err) 10357 goto out_disable; 10358 10359 /* Get UFS version supported by the controller */ 10360 hba->ufs_version = ufshcd_get_ufs_version(hba); 10361 10362 /* Get Interrupt bit mask per version */ 10363 hba->intr_mask = ufshcd_get_intr_mask(hba); 10364 10365 err = ufshcd_set_dma_mask(hba); 10366 if (err) { 10367 dev_err(hba->dev, "set dma mask failed\n"); 10368 goto out_disable; 10369 } 10370 10371 /* Allocate memory for host memory space */ 10372 err = ufshcd_memory_alloc(hba); 10373 if (err) { 10374 dev_err(hba->dev, "Memory allocation failed\n"); 10375 goto out_disable; 10376 } 10377 10378 /* Configure LRB */ 10379 ufshcd_host_memory_configure(hba); 10380 10381 host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED; 10382 host->cmd_per_lun = hba->nutrs - UFSHCD_NUM_RESERVED; 10383 host->max_id = UFSHCD_MAX_ID; 10384 host->max_lun = UFS_MAX_LUNS; 10385 host->max_channel = UFSHCD_MAX_CHANNEL; 10386 host->unique_id = host->host_no; 10387 host->max_cmd_len = UFS_CDB_SIZE; 10388 host->queuecommand_may_block = !!(hba->caps & UFSHCD_CAP_CLK_GATING); 10389 10390 /* Use default RPM delay if host not set */ 10391 if (host->rpm_autosuspend_delay == 0) 10392 host->rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS; 10393 10394 hba->max_pwr_info.is_valid = false; 10395 10396 /* Initialize work queues */ 10397 snprintf(eh_wq_name, sizeof(eh_wq_name), "ufs_eh_wq_%d", 10398 hba->host->host_no); 10399 hba->eh_wq = create_singlethread_workqueue(eh_wq_name); 10400 if (!hba->eh_wq) { 10401 dev_err(hba->dev, "%s: failed to create eh workqueue\n", 10402 __func__); 10403 err = -ENOMEM; 10404 goto out_disable; 10405 } 10406 INIT_WORK(&hba->eh_work, ufshcd_err_handler); 10407 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); 10408 10409 sema_init(&hba->host_sem, 1); 10410 10411 /* Initialize UIC command mutex */ 10412 mutex_init(&hba->uic_cmd_mutex); 10413 10414 /* Initialize mutex for device management commands */ 10415 mutex_init(&hba->dev_cmd.lock); 10416 10417 /* Initialize mutex for exception event control */ 10418 mutex_init(&hba->ee_ctrl_mutex); 10419 10420 mutex_init(&hba->wb_mutex); 10421 init_rwsem(&hba->clk_scaling_lock); 10422 10423 ufshcd_init_clk_gating(hba); 10424 10425 ufshcd_init_clk_scaling(hba); 10426 10427 /* 10428 * In order to avoid any spurious interrupt immediately after 10429 * registering UFS controller interrupt handler, clear any pending UFS 10430 * interrupt status and disable all the UFS interrupts. 10431 */ 10432 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), 10433 REG_INTERRUPT_STATUS); 10434 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); 10435 /* 10436 * Make sure that UFS interrupts are disabled and any pending interrupt 10437 * status is cleared before registering UFS interrupt handler. 10438 */ 10439 ufshcd_readl(hba, REG_INTERRUPT_ENABLE); 10440 10441 /* IRQ registration */ 10442 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); 10443 if (err) { 10444 dev_err(hba->dev, "request irq failed\n"); 10445 goto out_disable; 10446 } else { 10447 hba->is_irq_enabled = true; 10448 } 10449 10450 if (!is_mcq_supported(hba)) { 10451 err = scsi_add_host(host, hba->dev); 10452 if (err) { 10453 dev_err(hba->dev, "scsi_add_host failed\n"); 10454 goto out_disable; 10455 } 10456 } 10457 10458 hba->tmf_tag_set = (struct blk_mq_tag_set) { 10459 .nr_hw_queues = 1, 10460 .queue_depth = hba->nutmrs, 10461 .ops = &ufshcd_tmf_ops, 10462 .flags = BLK_MQ_F_NO_SCHED, 10463 }; 10464 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set); 10465 if (err < 0) 10466 goto out_remove_scsi_host; 10467 hba->tmf_queue = blk_mq_alloc_queue(&hba->tmf_tag_set, NULL, NULL); 10468 if (IS_ERR(hba->tmf_queue)) { 10469 err = PTR_ERR(hba->tmf_queue); 10470 goto free_tmf_tag_set; 10471 } 10472 hba->tmf_rqs = devm_kcalloc(hba->dev, hba->nutmrs, 10473 sizeof(*hba->tmf_rqs), GFP_KERNEL); 10474 if (!hba->tmf_rqs) { 10475 err = -ENOMEM; 10476 goto free_tmf_queue; 10477 } 10478 10479 /* Reset the attached device */ 10480 ufshcd_device_reset(hba); 10481 10482 ufshcd_init_crypto(hba); 10483 10484 /* Host controller enable */ 10485 err = ufshcd_hba_enable(hba); 10486 if (err) { 10487 dev_err(hba->dev, "Host controller enable failed\n"); 10488 ufshcd_print_evt_hist(hba); 10489 ufshcd_print_host_state(hba); 10490 goto free_tmf_queue; 10491 } 10492 10493 /* 10494 * Set the default power management level for runtime and system PM. 10495 * Default power saving mode is to keep UFS link in Hibern8 state 10496 * and UFS device in sleep state. 10497 */ 10498 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( 10499 UFS_SLEEP_PWR_MODE, 10500 UIC_LINK_HIBERN8_STATE); 10501 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( 10502 UFS_SLEEP_PWR_MODE, 10503 UIC_LINK_HIBERN8_STATE); 10504 10505 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work, ufshcd_rpm_dev_flush_recheck_work); 10506 INIT_DELAYED_WORK(&hba->ufs_rtc_update_work, ufshcd_rtc_work); 10507 10508 /* Set the default auto-hiberate idle timer value to 150 ms */ 10509 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) { 10510 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) | 10511 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3); 10512 } 10513 10514 /* Hold auto suspend until async scan completes */ 10515 pm_runtime_get_sync(dev); 10516 atomic_set(&hba->scsi_block_reqs_cnt, 0); 10517 /* 10518 * We are assuming that device wasn't put in sleep/power-down 10519 * state exclusively during the boot stage before kernel. 10520 * This assumption helps avoid doing link startup twice during 10521 * ufshcd_probe_hba(). 10522 */ 10523 ufshcd_set_ufs_dev_active(hba); 10524 10525 async_schedule(ufshcd_async_scan, hba); 10526 ufs_sysfs_add_nodes(hba->dev); 10527 10528 device_enable_async_suspend(dev); 10529 ufshcd_pm_qos_init(hba); 10530 return 0; 10531 10532 free_tmf_queue: 10533 blk_mq_destroy_queue(hba->tmf_queue); 10534 blk_put_queue(hba->tmf_queue); 10535 free_tmf_tag_set: 10536 blk_mq_free_tag_set(&hba->tmf_tag_set); 10537 out_remove_scsi_host: 10538 scsi_remove_host(hba->host); 10539 out_disable: 10540 hba->is_irq_enabled = false; 10541 ufshcd_hba_exit(hba); 10542 out_error: 10543 return err; 10544 } 10545 EXPORT_SYMBOL_GPL(ufshcd_init); 10546 10547 void ufshcd_resume_complete(struct device *dev) 10548 { 10549 struct ufs_hba *hba = dev_get_drvdata(dev); 10550 10551 if (hba->complete_put) { 10552 ufshcd_rpm_put(hba); 10553 hba->complete_put = false; 10554 } 10555 } 10556 EXPORT_SYMBOL_GPL(ufshcd_resume_complete); 10557 10558 static bool ufshcd_rpm_ok_for_spm(struct ufs_hba *hba) 10559 { 10560 struct device *dev = &hba->ufs_device_wlun->sdev_gendev; 10561 enum ufs_dev_pwr_mode dev_pwr_mode; 10562 enum uic_link_state link_state; 10563 unsigned long flags; 10564 bool res; 10565 10566 spin_lock_irqsave(&dev->power.lock, flags); 10567 dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl); 10568 link_state = ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl); 10569 res = pm_runtime_suspended(dev) && 10570 hba->curr_dev_pwr_mode == dev_pwr_mode && 10571 hba->uic_link_state == link_state && 10572 !hba->dev_info.b_rpm_dev_flush_capable; 10573 spin_unlock_irqrestore(&dev->power.lock, flags); 10574 10575 return res; 10576 } 10577 10578 int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm) 10579 { 10580 struct ufs_hba *hba = dev_get_drvdata(dev); 10581 int ret; 10582 10583 /* 10584 * SCSI assumes that runtime-pm and system-pm for scsi drivers 10585 * are same. And it doesn't wake up the device for system-suspend 10586 * if it's runtime suspended. But ufs doesn't follow that. 10587 * Refer ufshcd_resume_complete() 10588 */ 10589 if (hba->ufs_device_wlun) { 10590 /* Prevent runtime suspend */ 10591 ufshcd_rpm_get_noresume(hba); 10592 /* 10593 * Check if already runtime suspended in same state as system 10594 * suspend would be. 10595 */ 10596 if (!rpm_ok_for_spm || !ufshcd_rpm_ok_for_spm(hba)) { 10597 /* RPM state is not ok for SPM, so runtime resume */ 10598 ret = ufshcd_rpm_resume(hba); 10599 if (ret < 0 && ret != -EACCES) { 10600 ufshcd_rpm_put(hba); 10601 return ret; 10602 } 10603 } 10604 hba->complete_put = true; 10605 } 10606 return 0; 10607 } 10608 EXPORT_SYMBOL_GPL(__ufshcd_suspend_prepare); 10609 10610 int ufshcd_suspend_prepare(struct device *dev) 10611 { 10612 return __ufshcd_suspend_prepare(dev, true); 10613 } 10614 EXPORT_SYMBOL_GPL(ufshcd_suspend_prepare); 10615 10616 #ifdef CONFIG_PM_SLEEP 10617 static int ufshcd_wl_poweroff(struct device *dev) 10618 { 10619 struct scsi_device *sdev = to_scsi_device(dev); 10620 struct ufs_hba *hba = shost_priv(sdev->host); 10621 10622 __ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM); 10623 return 0; 10624 } 10625 #endif 10626 10627 static int ufshcd_wl_probe(struct device *dev) 10628 { 10629 struct scsi_device *sdev = to_scsi_device(dev); 10630 10631 if (!is_device_wlun(sdev)) 10632 return -ENODEV; 10633 10634 blk_pm_runtime_init(sdev->request_queue, dev); 10635 pm_runtime_set_autosuspend_delay(dev, 0); 10636 pm_runtime_allow(dev); 10637 10638 return 0; 10639 } 10640 10641 static int ufshcd_wl_remove(struct device *dev) 10642 { 10643 pm_runtime_forbid(dev); 10644 return 0; 10645 } 10646 10647 static const struct dev_pm_ops ufshcd_wl_pm_ops = { 10648 #ifdef CONFIG_PM_SLEEP 10649 .suspend = ufshcd_wl_suspend, 10650 .resume = ufshcd_wl_resume, 10651 .freeze = ufshcd_wl_suspend, 10652 .thaw = ufshcd_wl_resume, 10653 .poweroff = ufshcd_wl_poweroff, 10654 .restore = ufshcd_wl_resume, 10655 #endif 10656 SET_RUNTIME_PM_OPS(ufshcd_wl_runtime_suspend, ufshcd_wl_runtime_resume, NULL) 10657 }; 10658 10659 static void ufshcd_check_header_layout(void) 10660 { 10661 /* 10662 * gcc compilers before version 10 cannot do constant-folding for 10663 * sub-byte bitfields. Hence skip the layout checks for gcc 9 and 10664 * before. 10665 */ 10666 if (IS_ENABLED(CONFIG_CC_IS_GCC) && CONFIG_GCC_VERSION < 100000) 10667 return; 10668 10669 BUILD_BUG_ON(((u8 *)&(struct request_desc_header){ 10670 .cci = 3})[0] != 3); 10671 10672 BUILD_BUG_ON(((u8 *)&(struct request_desc_header){ 10673 .ehs_length = 2})[1] != 2); 10674 10675 BUILD_BUG_ON(((u8 *)&(struct request_desc_header){ 10676 .enable_crypto = 1})[2] 10677 != 0x80); 10678 10679 BUILD_BUG_ON((((u8 *)&(struct request_desc_header){ 10680 .command_type = 5, 10681 .data_direction = 3, 10682 .interrupt = 1, 10683 })[3]) != ((5 << 4) | (3 << 1) | 1)); 10684 10685 BUILD_BUG_ON(((__le32 *)&(struct request_desc_header){ 10686 .dunl = cpu_to_le32(0xdeadbeef)})[1] != 10687 cpu_to_le32(0xdeadbeef)); 10688 10689 BUILD_BUG_ON(((u8 *)&(struct request_desc_header){ 10690 .ocs = 4})[8] != 4); 10691 10692 BUILD_BUG_ON(((u8 *)&(struct request_desc_header){ 10693 .cds = 5})[9] != 5); 10694 10695 BUILD_BUG_ON(((__le32 *)&(struct request_desc_header){ 10696 .dunu = cpu_to_le32(0xbadcafe)})[3] != 10697 cpu_to_le32(0xbadcafe)); 10698 10699 BUILD_BUG_ON(((u8 *)&(struct utp_upiu_header){ 10700 .iid = 0xf })[4] != 0xf0); 10701 10702 BUILD_BUG_ON(((u8 *)&(struct utp_upiu_header){ 10703 .command_set_type = 0xf })[4] != 0xf); 10704 } 10705 10706 /* 10707 * ufs_dev_wlun_template - describes ufs device wlun 10708 * ufs-device wlun - used to send pm commands 10709 * All luns are consumers of ufs-device wlun. 10710 * 10711 * Currently, no sd driver is present for wluns. 10712 * Hence the no specific pm operations are performed. 10713 * With ufs design, SSU should be sent to ufs-device wlun. 10714 * Hence register a scsi driver for ufs wluns only. 10715 */ 10716 static struct scsi_driver ufs_dev_wlun_template = { 10717 .gendrv = { 10718 .name = "ufs_device_wlun", 10719 .probe = ufshcd_wl_probe, 10720 .remove = ufshcd_wl_remove, 10721 .pm = &ufshcd_wl_pm_ops, 10722 .shutdown = ufshcd_wl_shutdown, 10723 }, 10724 }; 10725 10726 static int __init ufshcd_core_init(void) 10727 { 10728 int ret; 10729 10730 ufshcd_check_header_layout(); 10731 10732 ufs_debugfs_init(); 10733 10734 ret = scsi_register_driver(&ufs_dev_wlun_template.gendrv); 10735 if (ret) 10736 ufs_debugfs_exit(); 10737 return ret; 10738 } 10739 10740 static void __exit ufshcd_core_exit(void) 10741 { 10742 ufs_debugfs_exit(); 10743 scsi_unregister_driver(&ufs_dev_wlun_template.gendrv); 10744 } 10745 10746 module_init(ufshcd_core_init); 10747 module_exit(ufshcd_core_exit); 10748 10749 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); 10750 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); 10751 MODULE_DESCRIPTION("Generic UFS host controller driver Core"); 10752 MODULE_SOFTDEP("pre: governor_simpleondemand"); 10753 MODULE_LICENSE("GPL"); 10754