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