1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2022 Qualcomm Innovation Center. All rights reserved. 4 * 5 * Authors: 6 * Asutosh Das <quic_asutoshd@quicinc.com> 7 * Can Guo <quic_cang@quicinc.com> 8 */ 9 10 #include <linux/unaligned.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/module.h> 13 #include <linux/platform_device.h> 14 #include "ufshcd-priv.h" 15 #include <linux/delay.h> 16 #include <scsi/scsi_cmnd.h> 17 #include <linux/bitfield.h> 18 #include <linux/iopoll.h> 19 20 #define MAX_QUEUE_SUP GENMASK(7, 0) 21 #define QCFGPTR GENMASK(23, 16) 22 #define UFS_MCQ_MIN_RW_QUEUES 2 23 #define UFS_MCQ_MIN_READ_QUEUES 0 24 #define UFS_MCQ_MIN_POLL_QUEUES 0 25 #define QUEUE_EN_OFFSET 31 26 #define QUEUE_ID_OFFSET 16 27 28 #define MCQ_CFG_MAC_MASK GENMASK(16, 8) 29 #define MCQ_ENTRY_SIZE_IN_DWORD 8 30 #define CQE_UCD_BA GENMASK_ULL(63, 7) 31 32 #define UFSHCD_ENABLE_MCQ_INTRS (UTP_TASK_REQ_COMPL |\ 33 UFSHCD_ERROR_MASK |\ 34 MCQ_CQ_EVENT_STATUS) 35 36 /* Max mcq register polling time in microseconds */ 37 #define MCQ_POLL_US 500000 38 39 static int rw_queue_count_set(const char *val, const struct kernel_param *kp) 40 { 41 return param_set_uint_minmax(val, kp, UFS_MCQ_MIN_RW_QUEUES, 42 num_possible_cpus()); 43 } 44 45 static const struct kernel_param_ops rw_queue_count_ops = { 46 .set = rw_queue_count_set, 47 .get = param_get_uint, 48 }; 49 50 static unsigned int rw_queues; 51 module_param_cb(rw_queues, &rw_queue_count_ops, &rw_queues, 0644); 52 MODULE_PARM_DESC(rw_queues, 53 "Number of interrupt driven I/O queues used for rw. Default value is nr_cpus"); 54 55 static int read_queue_count_set(const char *val, const struct kernel_param *kp) 56 { 57 return param_set_uint_minmax(val, kp, UFS_MCQ_MIN_READ_QUEUES, 58 num_possible_cpus()); 59 } 60 61 static const struct kernel_param_ops read_queue_count_ops = { 62 .set = read_queue_count_set, 63 .get = param_get_uint, 64 }; 65 66 static unsigned int read_queues; 67 module_param_cb(read_queues, &read_queue_count_ops, &read_queues, 0644); 68 MODULE_PARM_DESC(read_queues, 69 "Number of interrupt driven read queues used for read. Default value is 0"); 70 71 static int poll_queue_count_set(const char *val, const struct kernel_param *kp) 72 { 73 return param_set_uint_minmax(val, kp, UFS_MCQ_MIN_POLL_QUEUES, 74 num_possible_cpus()); 75 } 76 77 static const struct kernel_param_ops poll_queue_count_ops = { 78 .set = poll_queue_count_set, 79 .get = param_get_uint, 80 }; 81 82 static unsigned int poll_queues = 1; 83 module_param_cb(poll_queues, &poll_queue_count_ops, &poll_queues, 0644); 84 MODULE_PARM_DESC(poll_queues, 85 "Number of poll queues used for r/w. Default value is 1"); 86 87 /** 88 * ufshcd_mcq_config_mac - Set the #Max Activ Cmds. 89 * @hba: per adapter instance 90 * @max_active_cmds: maximum # of active commands to the device at any time. 91 * 92 * The controller won't send more than the max_active_cmds to the device at 93 * any time. 94 */ 95 void ufshcd_mcq_config_mac(struct ufs_hba *hba, u32 max_active_cmds) 96 { 97 u32 val; 98 99 val = ufshcd_readl(hba, REG_UFS_MCQ_CFG); 100 val &= ~MCQ_CFG_MAC_MASK; 101 val |= FIELD_PREP(MCQ_CFG_MAC_MASK, max_active_cmds - 1); 102 ufshcd_writel(hba, val, REG_UFS_MCQ_CFG); 103 } 104 EXPORT_SYMBOL_GPL(ufshcd_mcq_config_mac); 105 106 /** 107 * ufshcd_mcq_req_to_hwq - find the hardware queue on which the 108 * request would be issued. 109 * @hba: per adapter instance 110 * @req: pointer to the request to be issued 111 * 112 * Return: the hardware queue instance on which the request will be or has 113 * been queued. %NULL if the request has already been freed. 114 */ 115 struct ufs_hw_queue *ufshcd_mcq_req_to_hwq(struct ufs_hba *hba, 116 struct request *req) 117 { 118 struct blk_mq_hw_ctx *hctx = READ_ONCE(req->mq_hctx); 119 120 return hctx ? &hba->uhq[hctx->queue_num] : NULL; 121 } 122 123 /** 124 * ufshcd_mcq_queue_cfg_addr - get an start address of the MCQ Queue Config 125 * Registers. 126 * @hba: per adapter instance 127 * 128 * Return: Start address of MCQ Queue Config Registers in HCI 129 */ 130 unsigned int ufshcd_mcq_queue_cfg_addr(struct ufs_hba *hba) 131 { 132 return FIELD_GET(QCFGPTR, hba->mcq_capabilities) * 0x200; 133 } 134 EXPORT_SYMBOL_GPL(ufshcd_mcq_queue_cfg_addr); 135 136 /** 137 * ufshcd_mcq_decide_queue_depth - decide the queue depth 138 * @hba: per adapter instance 139 * 140 * Return: queue-depth on success, non-zero on error 141 * 142 * MAC - Max. Active Command of the Host Controller (HC) 143 * HC wouldn't send more than this commands to the device. 144 * Calculates and adjusts the queue depth based on the depth 145 * supported by the HC and ufs device. 146 */ 147 int ufshcd_mcq_decide_queue_depth(struct ufs_hba *hba) 148 { 149 int mac; 150 151 if (!hba->vops || !hba->vops->get_hba_mac) { 152 /* 153 * Extract the maximum number of active transfer tasks value 154 * from the host controller capabilities register. This value is 155 * 0-based. 156 */ 157 hba->capabilities = 158 ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); 159 mac = hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS_MCQ; 160 mac++; 161 } else { 162 mac = hba->vops->get_hba_mac(hba); 163 } 164 if (mac < 0) 165 goto err; 166 167 WARN_ON_ONCE(!hba->dev_info.bqueuedepth); 168 /* 169 * max. value of bqueuedepth = 256, mac is host dependent. 170 * It is mandatory for UFS device to define bQueueDepth if 171 * shared queuing architecture is enabled. 172 */ 173 return min_t(int, mac, hba->dev_info.bqueuedepth); 174 175 err: 176 dev_err(hba->dev, "Failed to get mac, err=%d\n", mac); 177 return mac; 178 } 179 180 static int ufshcd_mcq_config_nr_queues(struct ufs_hba *hba) 181 { 182 int i; 183 u32 hba_maxq, rem, tot_queues; 184 struct Scsi_Host *host = hba->host; 185 186 /* maxq is 0 based value */ 187 hba_maxq = FIELD_GET(MAX_QUEUE_SUP, hba->mcq_capabilities) + 1; 188 189 tot_queues = read_queues + poll_queues + rw_queues; 190 191 if (hba_maxq < tot_queues) { 192 dev_err(hba->dev, "Total queues (%d) exceeds HC capacity (%d)\n", 193 tot_queues, hba_maxq); 194 return -EOPNOTSUPP; 195 } 196 197 /* 198 * Device should support at least one I/O queue to handle device 199 * commands via hba->dev_cmd_queue. 200 */ 201 if (hba_maxq == poll_queues) { 202 dev_err(hba->dev, "At least one non-poll queue required\n"); 203 return -EOPNOTSUPP; 204 } 205 206 rem = hba_maxq; 207 208 if (rw_queues) { 209 hba->nr_queues[HCTX_TYPE_DEFAULT] = rw_queues; 210 rem -= hba->nr_queues[HCTX_TYPE_DEFAULT]; 211 } else { 212 rw_queues = num_possible_cpus(); 213 } 214 215 if (poll_queues) { 216 hba->nr_queues[HCTX_TYPE_POLL] = poll_queues; 217 rem -= hba->nr_queues[HCTX_TYPE_POLL]; 218 } 219 220 if (read_queues) { 221 hba->nr_queues[HCTX_TYPE_READ] = read_queues; 222 rem -= hba->nr_queues[HCTX_TYPE_READ]; 223 } 224 225 if (!hba->nr_queues[HCTX_TYPE_DEFAULT]) 226 hba->nr_queues[HCTX_TYPE_DEFAULT] = min3(rem, rw_queues, 227 num_possible_cpus()); 228 229 for (i = 0; i < HCTX_MAX_TYPES; i++) 230 host->nr_hw_queues += hba->nr_queues[i]; 231 232 hba->nr_hw_queues = host->nr_hw_queues; 233 return 0; 234 } 235 236 int ufshcd_mcq_memory_alloc(struct ufs_hba *hba) 237 { 238 struct ufs_hw_queue *hwq; 239 size_t utrdl_size, cqe_size; 240 int i; 241 242 for (i = 0; i < hba->nr_hw_queues; i++) { 243 hwq = &hba->uhq[i]; 244 245 utrdl_size = sizeof(struct utp_transfer_req_desc) * 246 hwq->max_entries; 247 hwq->sqe_base_addr = dmam_alloc_coherent(hba->dev, utrdl_size, 248 &hwq->sqe_dma_addr, 249 GFP_KERNEL); 250 if (!hwq->sqe_base_addr) { 251 dev_err(hba->dev, "SQE allocation failed\n"); 252 return -ENOMEM; 253 } 254 255 cqe_size = sizeof(struct cq_entry) * hwq->max_entries; 256 hwq->cqe_base_addr = dmam_alloc_coherent(hba->dev, cqe_size, 257 &hwq->cqe_dma_addr, 258 GFP_KERNEL); 259 if (!hwq->cqe_base_addr) { 260 dev_err(hba->dev, "CQE allocation failed\n"); 261 return -ENOMEM; 262 } 263 } 264 265 return 0; 266 } 267 268 static void __iomem *mcq_opr_base(struct ufs_hba *hba, 269 enum ufshcd_mcq_opr n, int i) 270 { 271 struct ufshcd_mcq_opr_info_t *opr = &hba->mcq_opr[n]; 272 273 return opr->base + opr->stride * i; 274 } 275 276 u32 ufshcd_mcq_read_cqis(struct ufs_hba *hba, int i) 277 { 278 return readl(mcq_opr_base(hba, OPR_CQIS, i) + REG_CQIS); 279 } 280 EXPORT_SYMBOL_GPL(ufshcd_mcq_read_cqis); 281 282 void ufshcd_mcq_write_cqis(struct ufs_hba *hba, u32 val, int i) 283 { 284 writel(val, mcq_opr_base(hba, OPR_CQIS, i) + REG_CQIS); 285 } 286 EXPORT_SYMBOL_GPL(ufshcd_mcq_write_cqis); 287 288 /* 289 * Current MCQ specification doesn't provide a Task Tag or its equivalent in 290 * the Completion Queue Entry. Find the Task Tag using an indirect method. 291 */ 292 static int ufshcd_mcq_get_tag(struct ufs_hba *hba, struct cq_entry *cqe) 293 { 294 u64 addr; 295 296 /* sizeof(struct utp_transfer_cmd_desc) must be a multiple of 128 */ 297 BUILD_BUG_ON(sizeof(struct utp_transfer_cmd_desc) & GENMASK(6, 0)); 298 299 /* Bits 63:7 UCD base address, 6:5 are reserved, 4:0 is SQ ID */ 300 addr = (le64_to_cpu(cqe->command_desc_base_addr) & CQE_UCD_BA) - 301 hba->ucdl_dma_addr; 302 303 return div_u64(addr, ufshcd_get_ucd_size(hba)); 304 } 305 306 static void ufshcd_mcq_process_cqe(struct ufs_hba *hba, 307 struct ufs_hw_queue *hwq) 308 { 309 struct cq_entry *cqe = ufshcd_mcq_cur_cqe(hwq); 310 int tag = ufshcd_mcq_get_tag(hba, cqe); 311 312 if (cqe->command_desc_base_addr) { 313 ufshcd_compl_one_cqe(hba, tag, cqe); 314 /* After processed the cqe, mark it empty (invalid) entry */ 315 cqe->command_desc_base_addr = 0; 316 } 317 } 318 319 void ufshcd_mcq_compl_all_cqes_lock(struct ufs_hba *hba, 320 struct ufs_hw_queue *hwq) 321 { 322 unsigned long flags; 323 u32 entries = hwq->max_entries; 324 325 spin_lock_irqsave(&hwq->cq_lock, flags); 326 while (entries > 0) { 327 ufshcd_mcq_process_cqe(hba, hwq); 328 ufshcd_mcq_inc_cq_head_slot(hwq); 329 entries--; 330 } 331 332 ufshcd_mcq_update_cq_tail_slot(hwq); 333 hwq->cq_head_slot = hwq->cq_tail_slot; 334 spin_unlock_irqrestore(&hwq->cq_lock, flags); 335 } 336 337 unsigned long ufshcd_mcq_poll_cqe_lock(struct ufs_hba *hba, 338 struct ufs_hw_queue *hwq) 339 { 340 unsigned long completed_reqs = 0; 341 unsigned long flags; 342 343 spin_lock_irqsave(&hwq->cq_lock, flags); 344 ufshcd_mcq_update_cq_tail_slot(hwq); 345 while (!ufshcd_mcq_is_cq_empty(hwq)) { 346 ufshcd_mcq_process_cqe(hba, hwq); 347 ufshcd_mcq_inc_cq_head_slot(hwq); 348 completed_reqs++; 349 } 350 351 if (completed_reqs) 352 ufshcd_mcq_update_cq_head(hwq); 353 spin_unlock_irqrestore(&hwq->cq_lock, flags); 354 355 return completed_reqs; 356 } 357 EXPORT_SYMBOL_GPL(ufshcd_mcq_poll_cqe_lock); 358 359 void ufshcd_mcq_make_queues_operational(struct ufs_hba *hba) 360 { 361 struct ufs_hw_queue *hwq; 362 u32 intrs; 363 u16 qsize; 364 int i; 365 366 /* Enable required interrupts */ 367 intrs = UFSHCD_ENABLE_MCQ_INTRS; 368 if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_INTR) 369 intrs &= ~MCQ_CQ_EVENT_STATUS; 370 ufshcd_enable_intr(hba, intrs); 371 372 for (i = 0; i < hba->nr_hw_queues; i++) { 373 hwq = &hba->uhq[i]; 374 hwq->id = i; 375 qsize = hwq->max_entries * MCQ_ENTRY_SIZE_IN_DWORD - 1; 376 377 /* Submission Queue Lower Base Address */ 378 ufsmcq_writelx(hba, lower_32_bits(hwq->sqe_dma_addr), 379 ufshcd_mcq_cfg_offset(REG_SQLBA, i)); 380 /* Submission Queue Upper Base Address */ 381 ufsmcq_writelx(hba, upper_32_bits(hwq->sqe_dma_addr), 382 ufshcd_mcq_cfg_offset(REG_SQUBA, i)); 383 /* Submission Queue Doorbell Address Offset */ 384 ufsmcq_writelx(hba, ufshcd_mcq_opr_offset(hba, OPR_SQD, i), 385 ufshcd_mcq_cfg_offset(REG_SQDAO, i)); 386 /* Submission Queue Interrupt Status Address Offset */ 387 ufsmcq_writelx(hba, ufshcd_mcq_opr_offset(hba, OPR_SQIS, i), 388 ufshcd_mcq_cfg_offset(REG_SQISAO, i)); 389 390 /* Completion Queue Lower Base Address */ 391 ufsmcq_writelx(hba, lower_32_bits(hwq->cqe_dma_addr), 392 ufshcd_mcq_cfg_offset(REG_CQLBA, i)); 393 /* Completion Queue Upper Base Address */ 394 ufsmcq_writelx(hba, upper_32_bits(hwq->cqe_dma_addr), 395 ufshcd_mcq_cfg_offset(REG_CQUBA, i)); 396 /* Completion Queue Doorbell Address Offset */ 397 ufsmcq_writelx(hba, ufshcd_mcq_opr_offset(hba, OPR_CQD, i), 398 ufshcd_mcq_cfg_offset(REG_CQDAO, i)); 399 /* Completion Queue Interrupt Status Address Offset */ 400 ufsmcq_writelx(hba, ufshcd_mcq_opr_offset(hba, OPR_CQIS, i), 401 ufshcd_mcq_cfg_offset(REG_CQISAO, i)); 402 403 /* Save the base addresses for quicker access */ 404 hwq->mcq_sq_head = mcq_opr_base(hba, OPR_SQD, i) + REG_SQHP; 405 hwq->mcq_sq_tail = mcq_opr_base(hba, OPR_SQD, i) + REG_SQTP; 406 hwq->mcq_cq_head = mcq_opr_base(hba, OPR_CQD, i) + REG_CQHP; 407 hwq->mcq_cq_tail = mcq_opr_base(hba, OPR_CQD, i) + REG_CQTP; 408 409 /* Reinitializing is needed upon HC reset */ 410 hwq->sq_tail_slot = hwq->cq_tail_slot = hwq->cq_head_slot = 0; 411 412 /* Enable Tail Entry Push Status interrupt only for non-poll queues */ 413 if (i < hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL]) 414 writel(1, mcq_opr_base(hba, OPR_CQIS, i) + REG_CQIE); 415 416 /* Completion Queue Enable|Size to Completion Queue Attribute */ 417 ufsmcq_writel(hba, (1 << QUEUE_EN_OFFSET) | qsize, 418 ufshcd_mcq_cfg_offset(REG_CQATTR, i)); 419 420 /* 421 * Submission Qeueue Enable|Size|Completion Queue ID to 422 * Submission Queue Attribute 423 */ 424 ufsmcq_writel(hba, (1 << QUEUE_EN_OFFSET) | qsize | 425 (i << QUEUE_ID_OFFSET), 426 ufshcd_mcq_cfg_offset(REG_SQATTR, i)); 427 } 428 } 429 EXPORT_SYMBOL_GPL(ufshcd_mcq_make_queues_operational); 430 431 void ufshcd_mcq_enable(struct ufs_hba *hba) 432 { 433 ufshcd_rmwl(hba, MCQ_MODE_SELECT, MCQ_MODE_SELECT, REG_UFS_MEM_CFG); 434 hba->mcq_enabled = true; 435 } 436 EXPORT_SYMBOL_GPL(ufshcd_mcq_enable); 437 438 void ufshcd_mcq_disable(struct ufs_hba *hba) 439 { 440 ufshcd_rmwl(hba, MCQ_MODE_SELECT, 0, REG_UFS_MEM_CFG); 441 hba->mcq_enabled = false; 442 } 443 444 void ufshcd_mcq_enable_esi(struct ufs_hba *hba) 445 { 446 ufshcd_writel(hba, ufshcd_readl(hba, REG_UFS_MEM_CFG) | 0x2, 447 REG_UFS_MEM_CFG); 448 } 449 EXPORT_SYMBOL_GPL(ufshcd_mcq_enable_esi); 450 451 void ufshcd_mcq_config_esi(struct ufs_hba *hba, struct msi_msg *msg) 452 { 453 ufshcd_writel(hba, msg->address_lo, REG_UFS_ESILBA); 454 ufshcd_writel(hba, msg->address_hi, REG_UFS_ESIUBA); 455 } 456 EXPORT_SYMBOL_GPL(ufshcd_mcq_config_esi); 457 458 int ufshcd_mcq_init(struct ufs_hba *hba) 459 { 460 struct Scsi_Host *host = hba->host; 461 struct ufs_hw_queue *hwq; 462 int ret, i; 463 464 ret = ufshcd_mcq_config_nr_queues(hba); 465 if (ret) 466 return ret; 467 468 ret = ufshcd_vops_mcq_config_resource(hba); 469 if (ret) 470 return ret; 471 472 ret = ufshcd_mcq_vops_op_runtime_config(hba); 473 if (ret) { 474 dev_err(hba->dev, "Operation runtime config failed, ret=%d\n", 475 ret); 476 return ret; 477 } 478 hba->uhq = devm_kzalloc(hba->dev, 479 hba->nr_hw_queues * sizeof(struct ufs_hw_queue), 480 GFP_KERNEL); 481 if (!hba->uhq) { 482 dev_err(hba->dev, "ufs hw queue memory allocation failed\n"); 483 return -ENOMEM; 484 } 485 486 for (i = 0; i < hba->nr_hw_queues; i++) { 487 hwq = &hba->uhq[i]; 488 hwq->max_entries = hba->nutrs + 1; 489 spin_lock_init(&hwq->sq_lock); 490 spin_lock_init(&hwq->cq_lock); 491 mutex_init(&hwq->sq_mutex); 492 } 493 494 /* The very first HW queue serves device commands */ 495 hba->dev_cmd_queue = &hba->uhq[0]; 496 497 host->host_tagset = 1; 498 return 0; 499 } 500 501 static int ufshcd_mcq_sq_stop(struct ufs_hba *hba, struct ufs_hw_queue *hwq) 502 { 503 void __iomem *reg; 504 u32 id = hwq->id, val; 505 int err; 506 507 if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_RTC) 508 return -ETIMEDOUT; 509 510 writel(SQ_STOP, mcq_opr_base(hba, OPR_SQD, id) + REG_SQRTC); 511 reg = mcq_opr_base(hba, OPR_SQD, id) + REG_SQRTS; 512 err = read_poll_timeout(readl, val, val & SQ_STS, 20, 513 MCQ_POLL_US, false, reg); 514 if (err) 515 dev_err(hba->dev, "%s: failed. hwq-id=%d, err=%d\n", 516 __func__, id, err); 517 return err; 518 } 519 520 static int ufshcd_mcq_sq_start(struct ufs_hba *hba, struct ufs_hw_queue *hwq) 521 { 522 void __iomem *reg; 523 u32 id = hwq->id, val; 524 int err; 525 526 if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_RTC) 527 return -ETIMEDOUT; 528 529 writel(SQ_START, mcq_opr_base(hba, OPR_SQD, id) + REG_SQRTC); 530 reg = mcq_opr_base(hba, OPR_SQD, id) + REG_SQRTS; 531 err = read_poll_timeout(readl, val, !(val & SQ_STS), 20, 532 MCQ_POLL_US, false, reg); 533 if (err) 534 dev_err(hba->dev, "%s: failed. hwq-id=%d, err=%d\n", 535 __func__, id, err); 536 return err; 537 } 538 539 /** 540 * ufshcd_mcq_sq_cleanup - Clean up submission queue resources 541 * associated with the pending command. 542 * @hba: per adapter instance. 543 * @task_tag: The command's task tag. 544 * 545 * Return: 0 for success; error code otherwise. 546 */ 547 int ufshcd_mcq_sq_cleanup(struct ufs_hba *hba, int task_tag) 548 { 549 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag]; 550 struct scsi_cmnd *cmd = lrbp->cmd; 551 struct ufs_hw_queue *hwq; 552 void __iomem *reg, *opr_sqd_base; 553 u32 nexus, id, val; 554 int err; 555 556 if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_RTC) 557 return -ETIMEDOUT; 558 559 if (task_tag != hba->nutrs - UFSHCD_NUM_RESERVED) { 560 if (!cmd) 561 return -EINVAL; 562 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(cmd)); 563 if (!hwq) 564 return 0; 565 } else { 566 hwq = hba->dev_cmd_queue; 567 } 568 569 id = hwq->id; 570 571 mutex_lock(&hwq->sq_mutex); 572 573 /* stop the SQ fetching before working on it */ 574 err = ufshcd_mcq_sq_stop(hba, hwq); 575 if (err) 576 goto unlock; 577 578 /* SQCTI = EXT_IID, IID, LUN, Task Tag */ 579 nexus = lrbp->lun << 8 | task_tag; 580 opr_sqd_base = mcq_opr_base(hba, OPR_SQD, id); 581 writel(nexus, opr_sqd_base + REG_SQCTI); 582 583 /* Initiate Cleanup */ 584 writel(readl(opr_sqd_base + REG_SQRTC) | SQ_ICU, 585 opr_sqd_base + REG_SQRTC); 586 587 /* Wait until SQRTSy.CUS = 1. Report SQRTSy.RTC. */ 588 reg = opr_sqd_base + REG_SQRTS; 589 err = read_poll_timeout(readl, val, val & SQ_CUS, 20, 590 MCQ_POLL_US, false, reg); 591 if (err) 592 dev_err(hba->dev, "%s: failed. hwq=%d, tag=%d err=%d\n", 593 __func__, id, task_tag, err); 594 else 595 dev_info(hba->dev, 596 "%s, hwq %d: cleanup return code (RTC) %ld\n", 597 __func__, id, 598 FIELD_GET(SQ_ICU_ERR_CODE_MASK, readl(reg))); 599 600 if (ufshcd_mcq_sq_start(hba, hwq)) 601 err = -ETIMEDOUT; 602 603 unlock: 604 mutex_unlock(&hwq->sq_mutex); 605 return err; 606 } 607 608 /** 609 * ufshcd_mcq_nullify_sqe - Nullify the submission queue entry. 610 * Write the sqe's Command Type to 0xF. The host controller will not 611 * fetch any sqe with Command Type = 0xF. 612 * 613 * @utrd: UTP Transfer Request Descriptor to be nullified. 614 */ 615 static void ufshcd_mcq_nullify_sqe(struct utp_transfer_req_desc *utrd) 616 { 617 utrd->header.command_type = 0xf; 618 } 619 620 /** 621 * ufshcd_mcq_sqe_search - Search for the command in the submission queue 622 * If the command is in the submission queue and not issued to the device yet, 623 * nullify the sqe so the host controller will skip fetching the sqe. 624 * 625 * @hba: per adapter instance. 626 * @hwq: Hardware Queue to be searched. 627 * @task_tag: The command's task tag. 628 * 629 * Return: true if the SQE containing the command is present in the SQ 630 * (not fetched by the controller); returns false if the SQE is not in the SQ. 631 */ 632 static bool ufshcd_mcq_sqe_search(struct ufs_hba *hba, 633 struct ufs_hw_queue *hwq, int task_tag) 634 { 635 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag]; 636 struct utp_transfer_req_desc *utrd; 637 __le64 cmd_desc_base_addr; 638 bool ret = false; 639 u64 addr, match; 640 u32 sq_head_slot; 641 642 if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_RTC) 643 return true; 644 645 mutex_lock(&hwq->sq_mutex); 646 647 ufshcd_mcq_sq_stop(hba, hwq); 648 sq_head_slot = ufshcd_mcq_get_sq_head_slot(hwq); 649 if (sq_head_slot == hwq->sq_tail_slot) 650 goto out; 651 652 cmd_desc_base_addr = lrbp->utr_descriptor_ptr->command_desc_base_addr; 653 addr = le64_to_cpu(cmd_desc_base_addr) & CQE_UCD_BA; 654 655 while (sq_head_slot != hwq->sq_tail_slot) { 656 utrd = hwq->sqe_base_addr + sq_head_slot; 657 match = le64_to_cpu(utrd->command_desc_base_addr) & CQE_UCD_BA; 658 if (addr == match) { 659 ufshcd_mcq_nullify_sqe(utrd); 660 ret = true; 661 goto out; 662 } 663 664 sq_head_slot++; 665 if (sq_head_slot == hwq->max_entries) 666 sq_head_slot = 0; 667 } 668 669 out: 670 ufshcd_mcq_sq_start(hba, hwq); 671 mutex_unlock(&hwq->sq_mutex); 672 return ret; 673 } 674 675 /** 676 * ufshcd_mcq_abort - Abort the command in MCQ. 677 * @cmd: The command to be aborted. 678 * 679 * Return: SUCCESS or FAILED error codes 680 */ 681 int ufshcd_mcq_abort(struct scsi_cmnd *cmd) 682 { 683 struct Scsi_Host *host = cmd->device->host; 684 struct ufs_hba *hba = shost_priv(host); 685 int tag = scsi_cmd_to_rq(cmd)->tag; 686 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; 687 struct ufs_hw_queue *hwq; 688 int err; 689 690 /* Skip task abort in case previous aborts failed and report failure */ 691 if (lrbp->req_abort_skip) { 692 dev_err(hba->dev, "%s: skip abort. tag %d failed earlier\n", 693 __func__, tag); 694 return FAILED; 695 } 696 697 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(cmd)); 698 if (!hwq) { 699 dev_err(hba->dev, "%s: skip abort. cmd at tag %d already completed.\n", 700 __func__, tag); 701 return FAILED; 702 } 703 704 if (ufshcd_mcq_sqe_search(hba, hwq, tag)) { 705 /* 706 * Failure. The command should not be "stuck" in SQ for 707 * a long time which resulted in command being aborted. 708 */ 709 dev_err(hba->dev, "%s: cmd found in sq. hwq=%d, tag=%d\n", 710 __func__, hwq->id, tag); 711 return FAILED; 712 } 713 714 /* 715 * The command is not in the submission queue, and it is not 716 * in the completion queue either. Query the device to see if 717 * the command is being processed in the device. 718 */ 719 err = ufshcd_try_to_abort_task(hba, tag); 720 if (err) { 721 dev_err(hba->dev, "%s: device abort failed %d\n", __func__, err); 722 lrbp->req_abort_skip = true; 723 return FAILED; 724 } 725 726 return SUCCESS; 727 } 728