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 return MMC_ISSUE_SYNC; 52 case REQ_OP_FLUSH: 53 return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC; 54 default: 55 return MMC_ISSUE_ASYNC; 56 } 57 } 58 59 enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req) 60 { 61 struct mmc_host *host = mq->card->host; 62 63 if (host->cqe_enabled && !host->hsq_enabled) 64 return mmc_cqe_issue_type(host, req); 65 66 if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE) 67 return MMC_ISSUE_ASYNC; 68 69 return MMC_ISSUE_SYNC; 70 } 71 72 static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq) 73 { 74 if (!mq->recovery_needed) { 75 mq->recovery_needed = true; 76 schedule_work(&mq->recovery_work); 77 } 78 } 79 80 void mmc_cqe_recovery_notifier(struct mmc_request *mrq) 81 { 82 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req, 83 brq.mrq); 84 struct request *req = mmc_queue_req_to_req(mqrq); 85 struct request_queue *q = req->q; 86 struct mmc_queue *mq = q->queuedata; 87 unsigned long flags; 88 89 spin_lock_irqsave(&mq->lock, flags); 90 __mmc_cqe_recovery_notifier(mq); 91 spin_unlock_irqrestore(&mq->lock, flags); 92 } 93 94 static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req) 95 { 96 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req); 97 struct mmc_request *mrq = &mqrq->brq.mrq; 98 struct mmc_queue *mq = req->q->queuedata; 99 struct mmc_host *host = mq->card->host; 100 enum mmc_issue_type issue_type = mmc_issue_type(mq, req); 101 bool recovery_needed = false; 102 103 switch (issue_type) { 104 case MMC_ISSUE_ASYNC: 105 case MMC_ISSUE_DCMD: 106 if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) { 107 if (recovery_needed) 108 mmc_cqe_recovery_notifier(mrq); 109 return BLK_EH_RESET_TIMER; 110 } 111 /* The request has gone already */ 112 return BLK_EH_DONE; 113 default: 114 /* Timeout is handled by mmc core */ 115 return BLK_EH_RESET_TIMER; 116 } 117 } 118 119 static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req, 120 bool reserved) 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_array(sg_len, sizeof(*sg), 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 request_queue *q, 178 struct mmc_card *card) 179 { 180 unsigned max_discard; 181 182 max_discard = mmc_calc_max_discard(card); 183 if (!max_discard) 184 return; 185 186 blk_queue_max_discard_sectors(q, max_discard); 187 q->limits.discard_granularity = card->pref_erase << 9; 188 /* granularity must not be greater than max. discard */ 189 if (card->pref_erase > max_discard) 190 q->limits.discard_granularity = SECTOR_SIZE; 191 if (mmc_can_secure_erase_trim(card)) 192 blk_queue_max_secure_erase_sectors(q, max_discard); 193 } 194 195 static unsigned short mmc_get_max_segments(struct mmc_host *host) 196 { 197 return host->can_dma_map_merge ? MMC_DMA_MAP_MERGE_SEGMENTS : 198 host->max_segs; 199 } 200 201 static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req, 202 unsigned int hctx_idx, unsigned int numa_node) 203 { 204 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req); 205 struct mmc_queue *mq = set->driver_data; 206 struct mmc_card *card = mq->card; 207 struct mmc_host *host = card->host; 208 209 mq_rq->sg = mmc_alloc_sg(mmc_get_max_segments(host), GFP_KERNEL); 210 if (!mq_rq->sg) 211 return -ENOMEM; 212 213 return 0; 214 } 215 216 static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req, 217 unsigned int hctx_idx) 218 { 219 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req); 220 221 kfree(mq_rq->sg); 222 mq_rq->sg = NULL; 223 } 224 225 static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx, 226 const struct blk_mq_queue_data *bd) 227 { 228 struct request *req = bd->rq; 229 struct request_queue *q = req->q; 230 struct mmc_queue *mq = q->queuedata; 231 struct mmc_card *card = mq->card; 232 struct mmc_host *host = card->host; 233 enum mmc_issue_type issue_type; 234 enum mmc_issued issued; 235 bool get_card, cqe_retune_ok; 236 blk_status_t ret; 237 238 if (mmc_card_removed(mq->card)) { 239 req->rq_flags |= RQF_QUIET; 240 return BLK_STS_IOERR; 241 } 242 243 issue_type = mmc_issue_type(mq, req); 244 245 spin_lock_irq(&mq->lock); 246 247 if (mq->recovery_needed || mq->busy) { 248 spin_unlock_irq(&mq->lock); 249 return BLK_STS_RESOURCE; 250 } 251 252 switch (issue_type) { 253 case MMC_ISSUE_DCMD: 254 if (mmc_cqe_dcmd_busy(mq)) { 255 mq->cqe_busy |= MMC_CQE_DCMD_BUSY; 256 spin_unlock_irq(&mq->lock); 257 return BLK_STS_RESOURCE; 258 } 259 break; 260 case MMC_ISSUE_ASYNC: 261 /* 262 * For MMC host software queue, we only allow 2 requests in 263 * flight to avoid a long latency. 264 */ 265 if (host->hsq_enabled && mq->in_flight[issue_type] > 2) { 266 spin_unlock_irq(&mq->lock); 267 return BLK_STS_RESOURCE; 268 } 269 break; 270 default: 271 /* 272 * Timeouts are handled by mmc core, and we don't have a host 273 * API to abort requests, so we can't handle the timeout anyway. 274 * However, when the timeout happens, blk_mq_complete_request() 275 * no longer works (to stop the request disappearing under us). 276 * To avoid racing with that, set a large timeout. 277 */ 278 req->timeout = 600 * HZ; 279 break; 280 } 281 282 /* Parallel dispatch of requests is not supported at the moment */ 283 mq->busy = true; 284 285 mq->in_flight[issue_type] += 1; 286 get_card = (mmc_tot_in_flight(mq) == 1); 287 cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1); 288 289 spin_unlock_irq(&mq->lock); 290 291 if (!(req->rq_flags & RQF_DONTPREP)) { 292 req_to_mmc_queue_req(req)->retries = 0; 293 req->rq_flags |= RQF_DONTPREP; 294 } 295 296 if (get_card) 297 mmc_get_card(card, &mq->ctx); 298 299 if (host->cqe_enabled) { 300 host->retune_now = host->need_retune && cqe_retune_ok && 301 !host->hold_retune; 302 } 303 304 blk_mq_start_request(req); 305 306 issued = mmc_blk_mq_issue_rq(mq, req); 307 308 switch (issued) { 309 case MMC_REQ_BUSY: 310 ret = BLK_STS_RESOURCE; 311 break; 312 case MMC_REQ_FAILED_TO_START: 313 ret = BLK_STS_IOERR; 314 break; 315 default: 316 ret = BLK_STS_OK; 317 break; 318 } 319 320 if (issued != MMC_REQ_STARTED) { 321 bool put_card = false; 322 323 spin_lock_irq(&mq->lock); 324 mq->in_flight[issue_type] -= 1; 325 if (mmc_tot_in_flight(mq) == 0) 326 put_card = true; 327 mq->busy = false; 328 spin_unlock_irq(&mq->lock); 329 if (put_card) 330 mmc_put_card(card, &mq->ctx); 331 } else { 332 WRITE_ONCE(mq->busy, false); 333 } 334 335 return ret; 336 } 337 338 static const struct blk_mq_ops mmc_mq_ops = { 339 .queue_rq = mmc_mq_queue_rq, 340 .init_request = mmc_mq_init_request, 341 .exit_request = mmc_mq_exit_request, 342 .complete = mmc_blk_mq_complete, 343 .timeout = mmc_mq_timed_out, 344 }; 345 346 static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card) 347 { 348 struct mmc_host *host = card->host; 349 unsigned block_size = 512; 350 351 blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue); 352 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue); 353 if (mmc_can_erase(card)) 354 mmc_queue_setup_discard(mq->queue, card); 355 356 if (!mmc_dev(host)->dma_mask || !*mmc_dev(host)->dma_mask) 357 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH); 358 blk_queue_max_hw_sectors(mq->queue, 359 min(host->max_blk_count, host->max_req_size / 512)); 360 if (host->can_dma_map_merge) 361 WARN(!blk_queue_can_use_dma_map_merging(mq->queue, 362 mmc_dev(host)), 363 "merging was advertised but not possible"); 364 blk_queue_max_segments(mq->queue, mmc_get_max_segments(host)); 365 366 if (mmc_card_mmc(card) && card->ext_csd.data_sector_size) { 367 block_size = card->ext_csd.data_sector_size; 368 WARN_ON(block_size != 512 && block_size != 4096); 369 } 370 371 blk_queue_logical_block_size(mq->queue, block_size); 372 /* 373 * After blk_queue_can_use_dma_map_merging() was called with succeed, 374 * since it calls blk_queue_virt_boundary(), the mmc should not call 375 * both blk_queue_max_segment_size(). 376 */ 377 if (!host->can_dma_map_merge) 378 blk_queue_max_segment_size(mq->queue, 379 round_down(host->max_seg_size, block_size)); 380 381 dma_set_max_seg_size(mmc_dev(host), queue_max_segment_size(mq->queue)); 382 383 INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler); 384 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work); 385 386 mutex_init(&mq->complete_lock); 387 388 init_waitqueue_head(&mq->wait); 389 390 mmc_crypto_setup_queue(mq->queue, host); 391 } 392 393 static inline bool mmc_merge_capable(struct mmc_host *host) 394 { 395 return host->caps2 & MMC_CAP2_MERGE_CAPABLE; 396 } 397 398 /* Set queue depth to get a reasonable value for q->nr_requests */ 399 #define MMC_QUEUE_DEPTH 64 400 401 /** 402 * mmc_init_queue - initialise a queue structure. 403 * @mq: mmc queue 404 * @card: mmc card to attach this queue 405 * 406 * Initialise a MMC card request queue. 407 */ 408 struct gendisk *mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card) 409 { 410 struct mmc_host *host = card->host; 411 struct gendisk *disk; 412 int ret; 413 414 mq->card = card; 415 416 spin_lock_init(&mq->lock); 417 418 memset(&mq->tag_set, 0, sizeof(mq->tag_set)); 419 mq->tag_set.ops = &mmc_mq_ops; 420 /* 421 * The queue depth for CQE must match the hardware because the request 422 * tag is used to index the hardware queue. 423 */ 424 if (host->cqe_enabled && !host->hsq_enabled) 425 mq->tag_set.queue_depth = 426 min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth); 427 else 428 mq->tag_set.queue_depth = MMC_QUEUE_DEPTH; 429 mq->tag_set.numa_node = NUMA_NO_NODE; 430 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING; 431 mq->tag_set.nr_hw_queues = 1; 432 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req); 433 mq->tag_set.driver_data = mq; 434 435 /* 436 * Since blk_mq_alloc_tag_set() calls .init_request() of mmc_mq_ops, 437 * the host->can_dma_map_merge should be set before to get max_segs 438 * from mmc_get_max_segments(). 439 */ 440 if (mmc_merge_capable(host) && 441 host->max_segs < MMC_DMA_MAP_MERGE_SEGMENTS && 442 dma_get_merge_boundary(mmc_dev(host))) 443 host->can_dma_map_merge = 1; 444 else 445 host->can_dma_map_merge = 0; 446 447 ret = blk_mq_alloc_tag_set(&mq->tag_set); 448 if (ret) 449 return ERR_PTR(ret); 450 451 452 disk = blk_mq_alloc_disk(&mq->tag_set, mq); 453 if (IS_ERR(disk)) { 454 blk_mq_free_tag_set(&mq->tag_set); 455 return disk; 456 } 457 mq->queue = disk->queue; 458 459 if (mmc_host_is_spi(host) && host->use_spi_crc) 460 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, mq->queue); 461 blk_queue_rq_timeout(mq->queue, 60 * HZ); 462 463 mmc_setup_queue(mq, card); 464 return disk; 465 } 466 467 void mmc_queue_suspend(struct mmc_queue *mq) 468 { 469 blk_mq_quiesce_queue(mq->queue); 470 471 /* 472 * The host remains claimed while there are outstanding requests, so 473 * simply claiming and releasing here ensures there are none. 474 */ 475 mmc_claim_host(mq->card->host); 476 mmc_release_host(mq->card->host); 477 } 478 479 void mmc_queue_resume(struct mmc_queue *mq) 480 { 481 blk_mq_unquiesce_queue(mq->queue); 482 } 483 484 void mmc_cleanup_queue(struct mmc_queue *mq) 485 { 486 struct request_queue *q = mq->queue; 487 488 /* 489 * The legacy code handled the possibility of being suspended, 490 * so do that here too. 491 */ 492 if (blk_queue_quiesced(q)) 493 blk_mq_unquiesce_queue(q); 494 495 blk_cleanup_queue(q); 496 blk_mq_free_tag_set(&mq->tag_set); 497 498 /* 499 * A request can be completed before the next request, potentially 500 * leaving a complete_work with nothing to do. Such a work item might 501 * still be queued at this point. Flush it. 502 */ 503 flush_work(&mq->complete_work); 504 505 mq->card = NULL; 506 } 507 508 /* 509 * Prepare the sg list(s) to be handed of to the host driver 510 */ 511 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq) 512 { 513 struct request *req = mmc_queue_req_to_req(mqrq); 514 515 return blk_rq_map_sg(mq->queue, req, mqrq->sg); 516 } 517