0001
0002
0003
0004
0005
0006 #include <linux/slab.h>
0007 #include <linux/module.h>
0008 #include <linux/blkdev.h>
0009 #include <linux/freezer.h>
0010 #include <linux/scatterlist.h>
0011 #include <linux/dma-mapping.h>
0012 #include <linux/backing-dev.h>
0013
0014 #include <linux/mmc/card.h>
0015 #include <linux/mmc/host.h>
0016
0017 #include "queue.h"
0018 #include "block.h"
0019 #include "core.h"
0020 #include "card.h"
0021 #include "crypto.h"
0022 #include "host.h"
0023
0024 #define MMC_DMA_MAP_MERGE_SEGMENTS 512
0025
0026 static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
0027 {
0028
0029 return mq->in_flight[MMC_ISSUE_DCMD];
0030 }
0031
0032 void mmc_cqe_check_busy(struct mmc_queue *mq)
0033 {
0034 if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
0035 mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
0036 }
0037
0038 static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
0039 {
0040 return host->caps2 & MMC_CAP2_CQE_DCMD;
0041 }
0042
0043 static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
0044 struct request *req)
0045 {
0046 switch (req_op(req)) {
0047 case REQ_OP_DRV_IN:
0048 case REQ_OP_DRV_OUT:
0049 case REQ_OP_DISCARD:
0050 case REQ_OP_SECURE_ERASE:
0051 return MMC_ISSUE_SYNC;
0052 case REQ_OP_FLUSH:
0053 return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
0054 default:
0055 return MMC_ISSUE_ASYNC;
0056 }
0057 }
0058
0059 enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
0060 {
0061 struct mmc_host *host = mq->card->host;
0062
0063 if (host->cqe_enabled && !host->hsq_enabled)
0064 return mmc_cqe_issue_type(host, req);
0065
0066 if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
0067 return MMC_ISSUE_ASYNC;
0068
0069 return MMC_ISSUE_SYNC;
0070 }
0071
0072 static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
0073 {
0074 if (!mq->recovery_needed) {
0075 mq->recovery_needed = true;
0076 schedule_work(&mq->recovery_work);
0077 }
0078 }
0079
0080 void mmc_cqe_recovery_notifier(struct mmc_request *mrq)
0081 {
0082 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
0083 brq.mrq);
0084 struct request *req = mmc_queue_req_to_req(mqrq);
0085 struct request_queue *q = req->q;
0086 struct mmc_queue *mq = q->queuedata;
0087 unsigned long flags;
0088
0089 spin_lock_irqsave(&mq->lock, flags);
0090 __mmc_cqe_recovery_notifier(mq);
0091 spin_unlock_irqrestore(&mq->lock, flags);
0092 }
0093
0094 static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
0095 {
0096 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
0097 struct mmc_request *mrq = &mqrq->brq.mrq;
0098 struct mmc_queue *mq = req->q->queuedata;
0099 struct mmc_host *host = mq->card->host;
0100 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
0101 bool recovery_needed = false;
0102
0103 switch (issue_type) {
0104 case MMC_ISSUE_ASYNC:
0105 case MMC_ISSUE_DCMD:
0106 if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
0107 if (recovery_needed)
0108 mmc_cqe_recovery_notifier(mrq);
0109 return BLK_EH_RESET_TIMER;
0110 }
0111
0112 return BLK_EH_DONE;
0113 default:
0114
0115 return BLK_EH_RESET_TIMER;
0116 }
0117 }
0118
0119 static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req)
0120 {
0121 struct request_queue *q = req->q;
0122 struct mmc_queue *mq = q->queuedata;
0123 struct mmc_card *card = mq->card;
0124 struct mmc_host *host = card->host;
0125 unsigned long flags;
0126 bool ignore_tout;
0127
0128 spin_lock_irqsave(&mq->lock, flags);
0129 ignore_tout = mq->recovery_needed || !host->cqe_enabled || host->hsq_enabled;
0130 spin_unlock_irqrestore(&mq->lock, flags);
0131
0132 return ignore_tout ? BLK_EH_RESET_TIMER : mmc_cqe_timed_out(req);
0133 }
0134
0135 static void mmc_mq_recovery_handler(struct work_struct *work)
0136 {
0137 struct mmc_queue *mq = container_of(work, struct mmc_queue,
0138 recovery_work);
0139 struct request_queue *q = mq->queue;
0140 struct mmc_host *host = mq->card->host;
0141
0142 mmc_get_card(mq->card, &mq->ctx);
0143
0144 mq->in_recovery = true;
0145
0146 if (host->cqe_enabled && !host->hsq_enabled)
0147 mmc_blk_cqe_recovery(mq);
0148 else
0149 mmc_blk_mq_recovery(mq);
0150
0151 mq->in_recovery = false;
0152
0153 spin_lock_irq(&mq->lock);
0154 mq->recovery_needed = false;
0155 spin_unlock_irq(&mq->lock);
0156
0157 if (host->hsq_enabled)
0158 host->cqe_ops->cqe_recovery_finish(host);
0159
0160 mmc_put_card(mq->card, &mq->ctx);
0161
0162 blk_mq_run_hw_queues(q, true);
0163 }
0164
0165 static struct scatterlist *mmc_alloc_sg(unsigned short sg_len, gfp_t gfp)
0166 {
0167 struct scatterlist *sg;
0168
0169 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
0170 if (sg)
0171 sg_init_table(sg, sg_len);
0172
0173 return sg;
0174 }
0175
0176 static void mmc_queue_setup_discard(struct request_queue *q,
0177 struct mmc_card *card)
0178 {
0179 unsigned max_discard;
0180
0181 max_discard = mmc_calc_max_discard(card);
0182 if (!max_discard)
0183 return;
0184
0185 blk_queue_max_discard_sectors(q, max_discard);
0186 q->limits.discard_granularity = card->pref_erase << 9;
0187
0188 if (card->pref_erase > max_discard)
0189 q->limits.discard_granularity = SECTOR_SIZE;
0190 if (mmc_can_secure_erase_trim(card))
0191 blk_queue_max_secure_erase_sectors(q, max_discard);
0192 if (mmc_can_trim(card) && card->erased_byte == 0)
0193 blk_queue_max_write_zeroes_sectors(q, max_discard);
0194 }
0195
0196 static unsigned short mmc_get_max_segments(struct mmc_host *host)
0197 {
0198 return host->can_dma_map_merge ? MMC_DMA_MAP_MERGE_SEGMENTS :
0199 host->max_segs;
0200 }
0201
0202 static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
0203 unsigned int hctx_idx, unsigned int numa_node)
0204 {
0205 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
0206 struct mmc_queue *mq = set->driver_data;
0207 struct mmc_card *card = mq->card;
0208 struct mmc_host *host = card->host;
0209
0210 mq_rq->sg = mmc_alloc_sg(mmc_get_max_segments(host), GFP_KERNEL);
0211 if (!mq_rq->sg)
0212 return -ENOMEM;
0213
0214 return 0;
0215 }
0216
0217 static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
0218 unsigned int hctx_idx)
0219 {
0220 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
0221
0222 kfree(mq_rq->sg);
0223 mq_rq->sg = NULL;
0224 }
0225
0226 static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
0227 const struct blk_mq_queue_data *bd)
0228 {
0229 struct request *req = bd->rq;
0230 struct request_queue *q = req->q;
0231 struct mmc_queue *mq = q->queuedata;
0232 struct mmc_card *card = mq->card;
0233 struct mmc_host *host = card->host;
0234 enum mmc_issue_type issue_type;
0235 enum mmc_issued issued;
0236 bool get_card, cqe_retune_ok;
0237 blk_status_t ret;
0238
0239 if (mmc_card_removed(mq->card)) {
0240 req->rq_flags |= RQF_QUIET;
0241 return BLK_STS_IOERR;
0242 }
0243
0244 issue_type = mmc_issue_type(mq, req);
0245
0246 spin_lock_irq(&mq->lock);
0247
0248 if (mq->recovery_needed || mq->busy) {
0249 spin_unlock_irq(&mq->lock);
0250 return BLK_STS_RESOURCE;
0251 }
0252
0253 switch (issue_type) {
0254 case MMC_ISSUE_DCMD:
0255 if (mmc_cqe_dcmd_busy(mq)) {
0256 mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
0257 spin_unlock_irq(&mq->lock);
0258 return BLK_STS_RESOURCE;
0259 }
0260 break;
0261 case MMC_ISSUE_ASYNC:
0262
0263
0264
0265
0266 if (host->hsq_enabled && mq->in_flight[issue_type] > 2) {
0267 spin_unlock_irq(&mq->lock);
0268 return BLK_STS_RESOURCE;
0269 }
0270 break;
0271 default:
0272
0273
0274
0275
0276
0277
0278
0279 req->timeout = 600 * HZ;
0280 break;
0281 }
0282
0283
0284 mq->busy = true;
0285
0286 mq->in_flight[issue_type] += 1;
0287 get_card = (mmc_tot_in_flight(mq) == 1);
0288 cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
0289
0290 spin_unlock_irq(&mq->lock);
0291
0292 if (!(req->rq_flags & RQF_DONTPREP)) {
0293 req_to_mmc_queue_req(req)->retries = 0;
0294 req->rq_flags |= RQF_DONTPREP;
0295 }
0296
0297 if (get_card)
0298 mmc_get_card(card, &mq->ctx);
0299
0300 if (host->cqe_enabled) {
0301 host->retune_now = host->need_retune && cqe_retune_ok &&
0302 !host->hold_retune;
0303 }
0304
0305 blk_mq_start_request(req);
0306
0307 issued = mmc_blk_mq_issue_rq(mq, req);
0308
0309 switch (issued) {
0310 case MMC_REQ_BUSY:
0311 ret = BLK_STS_RESOURCE;
0312 break;
0313 case MMC_REQ_FAILED_TO_START:
0314 ret = BLK_STS_IOERR;
0315 break;
0316 default:
0317 ret = BLK_STS_OK;
0318 break;
0319 }
0320
0321 if (issued != MMC_REQ_STARTED) {
0322 bool put_card = false;
0323
0324 spin_lock_irq(&mq->lock);
0325 mq->in_flight[issue_type] -= 1;
0326 if (mmc_tot_in_flight(mq) == 0)
0327 put_card = true;
0328 mq->busy = false;
0329 spin_unlock_irq(&mq->lock);
0330 if (put_card)
0331 mmc_put_card(card, &mq->ctx);
0332 } else {
0333 WRITE_ONCE(mq->busy, false);
0334 }
0335
0336 return ret;
0337 }
0338
0339 static const struct blk_mq_ops mmc_mq_ops = {
0340 .queue_rq = mmc_mq_queue_rq,
0341 .init_request = mmc_mq_init_request,
0342 .exit_request = mmc_mq_exit_request,
0343 .complete = mmc_blk_mq_complete,
0344 .timeout = mmc_mq_timed_out,
0345 };
0346
0347 static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
0348 {
0349 struct mmc_host *host = card->host;
0350 unsigned block_size = 512;
0351
0352 blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
0353 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
0354 if (mmc_can_erase(card))
0355 mmc_queue_setup_discard(mq->queue, card);
0356
0357 if (!mmc_dev(host)->dma_mask || !*mmc_dev(host)->dma_mask)
0358 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH);
0359 blk_queue_max_hw_sectors(mq->queue,
0360 min(host->max_blk_count, host->max_req_size / 512));
0361 if (host->can_dma_map_merge)
0362 WARN(!blk_queue_can_use_dma_map_merging(mq->queue,
0363 mmc_dev(host)),
0364 "merging was advertised but not possible");
0365 blk_queue_max_segments(mq->queue, mmc_get_max_segments(host));
0366
0367 if (mmc_card_mmc(card) && card->ext_csd.data_sector_size) {
0368 block_size = card->ext_csd.data_sector_size;
0369 WARN_ON(block_size != 512 && block_size != 4096);
0370 }
0371
0372 blk_queue_logical_block_size(mq->queue, block_size);
0373
0374
0375
0376
0377
0378 if (!host->can_dma_map_merge)
0379 blk_queue_max_segment_size(mq->queue,
0380 round_down(host->max_seg_size, block_size));
0381
0382 dma_set_max_seg_size(mmc_dev(host), queue_max_segment_size(mq->queue));
0383
0384 INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
0385 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
0386
0387 mutex_init(&mq->complete_lock);
0388
0389 init_waitqueue_head(&mq->wait);
0390
0391 mmc_crypto_setup_queue(mq->queue, host);
0392 }
0393
0394 static inline bool mmc_merge_capable(struct mmc_host *host)
0395 {
0396 return host->caps2 & MMC_CAP2_MERGE_CAPABLE;
0397 }
0398
0399
0400 #define MMC_QUEUE_DEPTH 64
0401
0402
0403
0404
0405
0406
0407
0408
0409 struct gendisk *mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card)
0410 {
0411 struct mmc_host *host = card->host;
0412 struct gendisk *disk;
0413 int ret;
0414
0415 mq->card = card;
0416
0417 spin_lock_init(&mq->lock);
0418
0419 memset(&mq->tag_set, 0, sizeof(mq->tag_set));
0420 mq->tag_set.ops = &mmc_mq_ops;
0421
0422
0423
0424
0425 if (host->cqe_enabled && !host->hsq_enabled)
0426 mq->tag_set.queue_depth =
0427 min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
0428 else
0429 mq->tag_set.queue_depth = MMC_QUEUE_DEPTH;
0430 mq->tag_set.numa_node = NUMA_NO_NODE;
0431 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
0432 mq->tag_set.nr_hw_queues = 1;
0433 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
0434 mq->tag_set.driver_data = mq;
0435
0436
0437
0438
0439
0440
0441 if (mmc_merge_capable(host) &&
0442 host->max_segs < MMC_DMA_MAP_MERGE_SEGMENTS &&
0443 dma_get_merge_boundary(mmc_dev(host)))
0444 host->can_dma_map_merge = 1;
0445 else
0446 host->can_dma_map_merge = 0;
0447
0448 ret = blk_mq_alloc_tag_set(&mq->tag_set);
0449 if (ret)
0450 return ERR_PTR(ret);
0451
0452
0453 disk = blk_mq_alloc_disk(&mq->tag_set, mq);
0454 if (IS_ERR(disk)) {
0455 blk_mq_free_tag_set(&mq->tag_set);
0456 return disk;
0457 }
0458 mq->queue = disk->queue;
0459
0460 if (mmc_host_is_spi(host) && host->use_spi_crc)
0461 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, mq->queue);
0462 blk_queue_rq_timeout(mq->queue, 60 * HZ);
0463
0464 mmc_setup_queue(mq, card);
0465 return disk;
0466 }
0467
0468 void mmc_queue_suspend(struct mmc_queue *mq)
0469 {
0470 blk_mq_quiesce_queue(mq->queue);
0471
0472
0473
0474
0475
0476 mmc_claim_host(mq->card->host);
0477 mmc_release_host(mq->card->host);
0478 }
0479
0480 void mmc_queue_resume(struct mmc_queue *mq)
0481 {
0482 blk_mq_unquiesce_queue(mq->queue);
0483 }
0484
0485 void mmc_cleanup_queue(struct mmc_queue *mq)
0486 {
0487 struct request_queue *q = mq->queue;
0488
0489
0490
0491
0492
0493 if (blk_queue_quiesced(q))
0494 blk_mq_unquiesce_queue(q);
0495
0496 blk_mq_free_tag_set(&mq->tag_set);
0497
0498
0499
0500
0501
0502
0503 flush_work(&mq->complete_work);
0504
0505 mq->card = NULL;
0506 }
0507
0508
0509
0510
0511 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
0512 {
0513 struct request *req = mmc_queue_req_to_req(mqrq);
0514
0515 return blk_rq_map_sg(mq->queue, req, mqrq->sg);
0516 }