1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2003 Russell King, All Rights Reserved. 4 * Copyright 2006-2007 Pierre Ossman 5 */ 6 #include <linux/slab.h> 7 #include <linux/module.h> 8 #include <linux/blkdev.h> 9 #include <linux/freezer.h> 10 #include <linux/kthread.h> 11 #include <linux/scatterlist.h> 12 #include <linux/dma-mapping.h> 13 #include <linux/backing-dev.h> 14 15 #include <linux/mmc/card.h> 16 #include <linux/mmc/host.h> 17 18 #include "queue.h" 19 #include "block.h" 20 #include "core.h" 21 #include "card.h" 22 #include "host.h" 23 24 #define MMC_DMA_MAP_MERGE_SEGMENTS 512 25 26 static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq) 27 { 28 /* Allow only 1 DCMD at a time */ 29 return mq->in_flight[MMC_ISSUE_DCMD]; 30 } 31 32 void mmc_cqe_check_busy(struct mmc_queue *mq) 33 { 34 if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq)) 35 mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY; 36 37 mq->cqe_busy &= ~MMC_CQE_QUEUE_FULL; 38 } 39 40 static inline bool mmc_cqe_can_dcmd(struct mmc_host *host) 41 { 42 return host->caps2 & MMC_CAP2_CQE_DCMD; 43 } 44 45 static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host, 46 struct request *req) 47 { 48 switch (req_op(req)) { 49 case REQ_OP_DRV_IN: 50 case REQ_OP_DRV_OUT: 51 case REQ_OP_DISCARD: 52 case REQ_OP_SECURE_ERASE: 53 return MMC_ISSUE_SYNC; 54 case REQ_OP_FLUSH: 55 return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC; 56 default: 57 return MMC_ISSUE_ASYNC; 58 } 59 } 60 61 enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req) 62 { 63 struct mmc_host *host = mq->card->host; 64 65 if (mq->use_cqe) 66 return mmc_cqe_issue_type(host, req); 67 68 if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE) 69 return MMC_ISSUE_ASYNC; 70 71 return MMC_ISSUE_SYNC; 72 } 73 74 static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq) 75 { 76 if (!mq->recovery_needed) { 77 mq->recovery_needed = true; 78 schedule_work(&mq->recovery_work); 79 } 80 } 81 82 void mmc_cqe_recovery_notifier(struct mmc_request *mrq) 83 { 84 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, 85 brq.mrq); 86 struct request *req = mmc_queue_req_to_req(mqrq); 87 struct request_queue *q = req->q; 88 struct mmc_queue *mq = q->queuedata; 89 unsigned long flags; 90 91 spin_lock_irqsave(&mq->lock, flags); 92 __mmc_cqe_recovery_notifier(mq); 93 spin_unlock_irqrestore(&mq->lock, flags); 94 } 95 96 static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req) 97 { 98 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 99 struct mmc_request *mrq = &mqrq->brq.mrq; 100 struct mmc_queue *mq = req->q->queuedata; 101 struct mmc_host *host = mq->card->host; 102 enum mmc_issue_type issue_type = mmc_issue_type(mq, req); 103 bool recovery_needed = false; 104 105 switch (issue_type) { 106 case MMC_ISSUE_ASYNC: 107 case MMC_ISSUE_DCMD: 108 if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) { 109 if (recovery_needed) 110 __mmc_cqe_recovery_notifier(mq); 111 return BLK_EH_RESET_TIMER; 112 } 113 /* No timeout (XXX: huh? comment doesn't make much sense) */ 114 blk_mq_complete_request(req); 115 return BLK_EH_DONE; 116 default: 117 /* Timeout is handled by mmc core */ 118 return BLK_EH_RESET_TIMER; 119 } 120 } 121 122 static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req, 123 bool reserved) 124 { 125 struct request_queue *q = req->q; 126 struct mmc_queue *mq = q->queuedata; 127 unsigned long flags; 128 int ret; 129 130 spin_lock_irqsave(&mq->lock, flags); 131 132 if (mq->recovery_needed || !mq->use_cqe) 133 ret = BLK_EH_RESET_TIMER; 134 else 135 ret = mmc_cqe_timed_out(req); 136 137 spin_unlock_irqrestore(&mq->lock, flags); 138 139 return ret; 140 } 141 142 static void mmc_mq_recovery_handler(struct work_struct *work) 143 { 144 struct mmc_queue *mq = container_of(work, struct mmc_queue, 145 recovery_work); 146 struct request_queue *q = mq->queue; 147 148 mmc_get_card(mq->card, &mq->ctx); 149 150 mq->in_recovery = true; 151 152 if (mq->use_cqe) 153 mmc_blk_cqe_recovery(mq); 154 else 155 mmc_blk_mq_recovery(mq); 156 157 mq->in_recovery = false; 158 159 spin_lock_irq(&mq->lock); 160 mq->recovery_needed = false; 161 spin_unlock_irq(&mq->lock); 162 163 mmc_put_card(mq->card, &mq->ctx); 164 165 blk_mq_run_hw_queues(q, true); 166 } 167 168 static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp) 169 { 170 struct scatterlist *sg; 171 172 sg = kmalloc_array(sg_len, sizeof(*sg), gfp); 173 if (sg) 174 sg_init_table(sg, sg_len); 175 176 return sg; 177 } 178 179 static void mmc_queue_setup_discard(struct request_queue *q, 180 struct mmc_card *card) 181 { 182 unsigned max_discard; 183 184 max_discard = mmc_calc_max_discard(card); 185 if (!max_discard) 186 return; 187 188 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q); 189 blk_queue_max_discard_sectors(q, max_discard); 190 q->limits.discard_granularity = card->pref_erase << 9; 191 /* granularity must not be greater than max. discard */ 192 if (card->pref_erase > max_discard) 193 q->limits.discard_granularity = 0; 194 if (mmc_can_secure_erase_trim(card)) 195 blk_queue_flag_set(QUEUE_FLAG_SECERASE, q); 196 } 197 198 static unsigned int mmc_get_max_segments(struct mmc_host *host) 199 { 200 return host->can_dma_map_merge ? MMC_DMA_MAP_MERGE_SEGMENTS : 201 host->max_segs; 202 } 203 204 /** 205 * mmc_init_request() - initialize the MMC-specific per-request data 206 * @q: the request queue 207 * @req: the request 208 * @gfp: memory allocation policy 209 */ 210 static int __mmc_init_request(struct mmc_queue *mq, struct request *req, 211 gfp_t gfp) 212 { 213 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req); 214 struct mmc_card *card = mq->card; 215 struct mmc_host *host = card->host; 216 217 mq_rq->sg = mmc_alloc_sg(mmc_get_max_segments(host), gfp); 218 if (!mq_rq->sg) 219 return -ENOMEM; 220 221 return 0; 222 } 223 224 static void mmc_exit_request(struct request_queue *q, struct request *req) 225 { 226 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req); 227 228 kfree(mq_rq->sg); 229 mq_rq->sg = NULL; 230 } 231 232 static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req, 233 unsigned int hctx_idx, unsigned int numa_node) 234 { 235 return __mmc_init_request(set->driver_data, req, GFP_KERNEL); 236 } 237 238 static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req, 239 unsigned int hctx_idx) 240 { 241 struct mmc_queue *mq = set->driver_data; 242 243 mmc_exit_request(mq->queue, req); 244 } 245 246 static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx, 247 const struct blk_mq_queue_data *bd) 248 { 249 struct request *req = bd->rq; 250 struct request_queue *q = req->q; 251 struct mmc_queue *mq = q->queuedata; 252 struct mmc_card *card = mq->card; 253 struct mmc_host *host = card->host; 254 enum mmc_issue_type issue_type; 255 enum mmc_issued issued; 256 bool get_card, cqe_retune_ok; 257 int ret; 258 259 if (mmc_card_removed(mq->card)) { 260 req->rq_flags |= RQF_QUIET; 261 return BLK_STS_IOERR; 262 } 263 264 issue_type = mmc_issue_type(mq, req); 265 266 spin_lock_irq(&mq->lock); 267 268 if (mq->recovery_needed || mq->busy) { 269 spin_unlock_irq(&mq->lock); 270 return BLK_STS_RESOURCE; 271 } 272 273 switch (issue_type) { 274 case MMC_ISSUE_DCMD: 275 if (mmc_cqe_dcmd_busy(mq)) { 276 mq->cqe_busy |= MMC_CQE_DCMD_BUSY; 277 spin_unlock_irq(&mq->lock); 278 return BLK_STS_RESOURCE; 279 } 280 break; 281 case MMC_ISSUE_ASYNC: 282 break; 283 default: 284 /* 285 * Timeouts are handled by mmc core, and we don't have a host 286 * API to abort requests, so we can't handle the timeout anyway. 287 * However, when the timeout happens, blk_mq_complete_request() 288 * no longer works (to stop the request disappearing under us). 289 * To avoid racing with that, set a large timeout. 290 */ 291 req->timeout = 600 * HZ; 292 break; 293 } 294 295 /* Parallel dispatch of requests is not supported at the moment */ 296 mq->busy = true; 297 298 mq->in_flight[issue_type] += 1; 299 get_card = (mmc_tot_in_flight(mq) == 1); 300 cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1); 301 302 spin_unlock_irq(&mq->lock); 303 304 if (!(req->rq_flags & RQF_DONTPREP)) { 305 req_to_mmc_queue_req(req)->retries = 0; 306 req->rq_flags |= RQF_DONTPREP; 307 } 308 309 if (get_card) 310 mmc_get_card(card, &mq->ctx); 311 312 if (mq->use_cqe) { 313 host->retune_now = host->need_retune && cqe_retune_ok && 314 !host->hold_retune; 315 } 316 317 blk_mq_start_request(req); 318 319 issued = mmc_blk_mq_issue_rq(mq, req); 320 321 switch (issued) { 322 case MMC_REQ_BUSY: 323 ret = BLK_STS_RESOURCE; 324 break; 325 case MMC_REQ_FAILED_TO_START: 326 ret = BLK_STS_IOERR; 327 break; 328 default: 329 ret = BLK_STS_OK; 330 break; 331 } 332 333 if (issued != MMC_REQ_STARTED) { 334 bool put_card = false; 335 336 spin_lock_irq(&mq->lock); 337 mq->in_flight[issue_type] -= 1; 338 if (mmc_tot_in_flight(mq) == 0) 339 put_card = true; 340 mq->busy = false; 341 spin_unlock_irq(&mq->lock); 342 if (put_card) 343 mmc_put_card(card, &mq->ctx); 344 } else { 345 WRITE_ONCE(mq->busy, false); 346 } 347 348 return ret; 349 } 350 351 static const struct blk_mq_ops mmc_mq_ops = { 352 .queue_rq = mmc_mq_queue_rq, 353 .init_request = mmc_mq_init_request, 354 .exit_request = mmc_mq_exit_request, 355 .complete = mmc_blk_mq_complete, 356 .timeout = mmc_mq_timed_out, 357 }; 358 359 static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card) 360 { 361 struct mmc_host *host = card->host; 362 unsigned block_size = 512; 363 364 blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue); 365 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue); 366 if (mmc_can_erase(card)) 367 mmc_queue_setup_discard(mq->queue, card); 368 369 if (!mmc_dev(host)->dma_mask || !*mmc_dev(host)->dma_mask) 370 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH); 371 blk_queue_max_hw_sectors(mq->queue, 372 min(host->max_blk_count, host->max_req_size / 512)); 373 if (host->can_dma_map_merge) 374 WARN(!blk_queue_can_use_dma_map_merging(mq->queue, 375 mmc_dev(host)), 376 "merging was advertised but not possible"); 377 blk_queue_max_segments(mq->queue, mmc_get_max_segments(host)); 378 379 if (mmc_card_mmc(card)) 380 block_size = card->ext_csd.data_sector_size; 381 382 blk_queue_logical_block_size(mq->queue, block_size); 383 /* 384 * After blk_queue_can_use_dma_map_merging() was called with succeed, 385 * since it calls blk_queue_virt_boundary(), the mmc should not call 386 * both blk_queue_max_segment_size(). 387 */ 388 if (!host->can_dma_map_merge) 389 blk_queue_max_segment_size(mq->queue, 390 round_down(host->max_seg_size, block_size)); 391 392 dma_set_max_seg_size(mmc_dev(host), queue_max_segment_size(mq->queue)); 393 394 INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler); 395 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work); 396 397 mutex_init(&mq->complete_lock); 398 399 init_waitqueue_head(&mq->wait); 400 } 401 402 static inline bool mmc_merge_capable(struct mmc_host *host) 403 { 404 return host->caps2 & MMC_CAP2_MERGE_CAPABLE; 405 } 406 407 /* Set queue depth to get a reasonable value for q->nr_requests */ 408 #define MMC_QUEUE_DEPTH 64 409 410 /** 411 * mmc_init_queue - initialise a queue structure. 412 * @mq: mmc queue 413 * @card: mmc card to attach this queue 414 * 415 * Initialise a MMC card request queue. 416 */ 417 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card) 418 { 419 struct mmc_host *host = card->host; 420 int ret; 421 422 mq->card = card; 423 mq->use_cqe = host->cqe_enabled; 424 425 spin_lock_init(&mq->lock); 426 427 memset(&mq->tag_set, 0, sizeof(mq->tag_set)); 428 mq->tag_set.ops = &mmc_mq_ops; 429 /* 430 * The queue depth for CQE must match the hardware because the request 431 * tag is used to index the hardware queue. 432 */ 433 if (mq->use_cqe) 434 mq->tag_set.queue_depth = 435 min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth); 436 else 437 mq->tag_set.queue_depth = MMC_QUEUE_DEPTH; 438 mq->tag_set.numa_node = NUMA_NO_NODE; 439 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING; 440 mq->tag_set.nr_hw_queues = 1; 441 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req); 442 mq->tag_set.driver_data = mq; 443 444 /* 445 * Since blk_mq_alloc_tag_set() calls .init_request() of mmc_mq_ops, 446 * the host->can_dma_map_merge should be set before to get max_segs 447 * from mmc_get_max_segments(). 448 */ 449 if (mmc_merge_capable(host) && 450 host->max_segs < MMC_DMA_MAP_MERGE_SEGMENTS && 451 dma_get_merge_boundary(mmc_dev(host))) 452 host->can_dma_map_merge = 1; 453 else 454 host->can_dma_map_merge = 0; 455 456 ret = blk_mq_alloc_tag_set(&mq->tag_set); 457 if (ret) 458 return ret; 459 460 mq->queue = blk_mq_init_queue(&mq->tag_set); 461 if (IS_ERR(mq->queue)) { 462 ret = PTR_ERR(mq->queue); 463 goto free_tag_set; 464 } 465 466 if (mmc_host_is_spi(host) && host->use_spi_crc) 467 mq->queue->backing_dev_info->capabilities |= 468 BDI_CAP_STABLE_WRITES; 469 470 mq->queue->queuedata = mq; 471 blk_queue_rq_timeout(mq->queue, 60 * HZ); 472 473 mmc_setup_queue(mq, card); 474 return 0; 475 476 free_tag_set: 477 blk_mq_free_tag_set(&mq->tag_set); 478 return ret; 479 } 480 481 void mmc_queue_suspend(struct mmc_queue *mq) 482 { 483 blk_mq_quiesce_queue(mq->queue); 484 485 /* 486 * The host remains claimed while there are outstanding requests, so 487 * simply claiming and releasing here ensures there are none. 488 */ 489 mmc_claim_host(mq->card->host); 490 mmc_release_host(mq->card->host); 491 } 492 493 void mmc_queue_resume(struct mmc_queue *mq) 494 { 495 blk_mq_unquiesce_queue(mq->queue); 496 } 497 498 void mmc_cleanup_queue(struct mmc_queue *mq) 499 { 500 struct request_queue *q = mq->queue; 501 502 /* 503 * The legacy code handled the possibility of being suspended, 504 * so do that here too. 505 */ 506 if (blk_queue_quiesced(q)) 507 blk_mq_unquiesce_queue(q); 508 509 blk_cleanup_queue(q); 510 blk_mq_free_tag_set(&mq->tag_set); 511 512 /* 513 * A request can be completed before the next request, potentially 514 * leaving a complete_work with nothing to do. Such a work item might 515 * still be queued at this point. Flush it. 516 */ 517 flush_work(&mq->complete_work); 518 519 mq->card = NULL; 520 } 521 522 /* 523 * Prepare the sg list(s) to be handed of to the host driver 524 */ 525 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq) 526 { 527 struct request *req = mmc_queue_req_to_req(mqrq); 528 529 return blk_rq_map_sg(mq->queue, req, mqrq->sg); 530 } 531