1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2003 Russell King, All Rights Reserved.
4 * Copyright 2006-2007 Pierre Ossman
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>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/host.h>
24 #define MMC_DMA_MAP_MERGE_SEGMENTS 512
26 static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
28 /* Allow only 1 DCMD at a time */
29 return mq->in_flight[MMC_ISSUE_DCMD];
32 void mmc_cqe_check_busy(struct mmc_queue *mq)
34 if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
35 mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
37 mq->cqe_busy &= ~MMC_CQE_QUEUE_FULL;
40 static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
42 return host->caps2 & MMC_CAP2_CQE_DCMD;
45 static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
48 switch (req_op(req)) {
52 case REQ_OP_SECURE_ERASE:
53 return MMC_ISSUE_SYNC;
55 return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
57 return MMC_ISSUE_ASYNC;
61 enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
63 struct mmc_host *host = mq->card->host;
66 return mmc_cqe_issue_type(host, req);
68 if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
69 return MMC_ISSUE_ASYNC;
71 return MMC_ISSUE_SYNC;
74 static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
76 if (!mq->recovery_needed) {
77 mq->recovery_needed = true;
78 schedule_work(&mq->recovery_work);
82 void mmc_cqe_recovery_notifier(struct mmc_request *mrq)
84 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
86 struct request *req = mmc_queue_req_to_req(mqrq);
87 struct request_queue *q = req->q;
88 struct mmc_queue *mq = q->queuedata;
91 spin_lock_irqsave(&mq->lock, flags);
92 __mmc_cqe_recovery_notifier(mq);
93 spin_unlock_irqrestore(&mq->lock, flags);
96 static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
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;
105 switch (issue_type) {
106 case MMC_ISSUE_ASYNC:
108 if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
110 mmc_cqe_recovery_notifier(mrq);
111 return BLK_EH_RESET_TIMER;
113 /* The request has gone already */
116 /* Timeout is handled by mmc core */
117 return BLK_EH_RESET_TIMER;
121 static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
124 struct request_queue *q = req->q;
125 struct mmc_queue *mq = q->queuedata;
129 spin_lock_irqsave(&mq->lock, flags);
130 ignore_tout = mq->recovery_needed || !mq->use_cqe;
131 spin_unlock_irqrestore(&mq->lock, flags);
133 return ignore_tout ? BLK_EH_RESET_TIMER : mmc_cqe_timed_out(req);
136 static void mmc_mq_recovery_handler(struct work_struct *work)
138 struct mmc_queue *mq = container_of(work, struct mmc_queue,
140 struct request_queue *q = mq->queue;
142 mmc_get_card(mq->card, &mq->ctx);
144 mq->in_recovery = true;
147 mmc_blk_cqe_recovery(mq);
149 mmc_blk_mq_recovery(mq);
151 mq->in_recovery = false;
153 spin_lock_irq(&mq->lock);
154 mq->recovery_needed = false;
155 spin_unlock_irq(&mq->lock);
157 mmc_put_card(mq->card, &mq->ctx);
159 blk_mq_run_hw_queues(q, true);
162 static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
164 struct scatterlist *sg;
166 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
168 sg_init_table(sg, sg_len);
173 static void mmc_queue_setup_discard(struct request_queue *q,
174 struct mmc_card *card)
176 unsigned max_discard;
178 max_discard = mmc_calc_max_discard(card);
182 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
183 blk_queue_max_discard_sectors(q, max_discard);
184 q->limits.discard_granularity = card->pref_erase << 9;
185 /* granularity must not be greater than max. discard */
186 if (card->pref_erase > max_discard)
187 q->limits.discard_granularity = SECTOR_SIZE;
188 if (mmc_can_secure_erase_trim(card))
189 blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
192 static unsigned int mmc_get_max_segments(struct mmc_host *host)
194 return host->can_dma_map_merge ? MMC_DMA_MAP_MERGE_SEGMENTS :
199 * mmc_init_request() - initialize the MMC-specific per-request data
200 * @q: the request queue
202 * @gfp: memory allocation policy
204 static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
207 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
208 struct mmc_card *card = mq->card;
209 struct mmc_host *host = card->host;
211 mq_rq->sg = mmc_alloc_sg(mmc_get_max_segments(host), gfp);
218 static void mmc_exit_request(struct request_queue *q, struct request *req)
220 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
226 static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
227 unsigned int hctx_idx, unsigned int numa_node)
229 return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
232 static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
233 unsigned int hctx_idx)
235 struct mmc_queue *mq = set->driver_data;
237 mmc_exit_request(mq->queue, req);
240 static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
241 const struct blk_mq_queue_data *bd)
243 struct request *req = bd->rq;
244 struct request_queue *q = req->q;
245 struct mmc_queue *mq = q->queuedata;
246 struct mmc_card *card = mq->card;
247 struct mmc_host *host = card->host;
248 enum mmc_issue_type issue_type;
249 enum mmc_issued issued;
250 bool get_card, cqe_retune_ok;
253 if (mmc_card_removed(mq->card)) {
254 req->rq_flags |= RQF_QUIET;
255 return BLK_STS_IOERR;
258 issue_type = mmc_issue_type(mq, req);
260 spin_lock_irq(&mq->lock);
262 if (mq->recovery_needed || mq->busy) {
263 spin_unlock_irq(&mq->lock);
264 return BLK_STS_RESOURCE;
267 switch (issue_type) {
269 if (mmc_cqe_dcmd_busy(mq)) {
270 mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
271 spin_unlock_irq(&mq->lock);
272 return BLK_STS_RESOURCE;
275 case MMC_ISSUE_ASYNC:
279 * Timeouts are handled by mmc core, and we don't have a host
280 * API to abort requests, so we can't handle the timeout anyway.
281 * However, when the timeout happens, blk_mq_complete_request()
282 * no longer works (to stop the request disappearing under us).
283 * To avoid racing with that, set a large timeout.
285 req->timeout = 600 * HZ;
289 /* Parallel dispatch of requests is not supported at the moment */
292 mq->in_flight[issue_type] += 1;
293 get_card = (mmc_tot_in_flight(mq) == 1);
294 cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
296 spin_unlock_irq(&mq->lock);
298 if (!(req->rq_flags & RQF_DONTPREP)) {
299 req_to_mmc_queue_req(req)->retries = 0;
300 req->rq_flags |= RQF_DONTPREP;
304 mmc_get_card(card, &mq->ctx);
307 host->retune_now = host->need_retune && cqe_retune_ok &&
311 blk_mq_start_request(req);
313 issued = mmc_blk_mq_issue_rq(mq, req);
317 ret = BLK_STS_RESOURCE;
319 case MMC_REQ_FAILED_TO_START:
327 if (issued != MMC_REQ_STARTED) {
328 bool put_card = false;
330 spin_lock_irq(&mq->lock);
331 mq->in_flight[issue_type] -= 1;
332 if (mmc_tot_in_flight(mq) == 0)
335 spin_unlock_irq(&mq->lock);
337 mmc_put_card(card, &mq->ctx);
339 WRITE_ONCE(mq->busy, false);
345 static const struct blk_mq_ops mmc_mq_ops = {
346 .queue_rq = mmc_mq_queue_rq,
347 .init_request = mmc_mq_init_request,
348 .exit_request = mmc_mq_exit_request,
349 .complete = mmc_blk_mq_complete,
350 .timeout = mmc_mq_timed_out,
353 static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
355 struct mmc_host *host = card->host;
356 unsigned block_size = 512;
358 blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
359 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
360 if (mmc_can_erase(card))
361 mmc_queue_setup_discard(mq->queue, card);
363 if (!mmc_dev(host)->dma_mask || !*mmc_dev(host)->dma_mask)
364 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH);
365 blk_queue_max_hw_sectors(mq->queue,
366 min(host->max_blk_count, host->max_req_size / 512));
367 if (host->can_dma_map_merge)
368 WARN(!blk_queue_can_use_dma_map_merging(mq->queue,
370 "merging was advertised but not possible");
371 blk_queue_max_segments(mq->queue, mmc_get_max_segments(host));
373 if (mmc_card_mmc(card) && card->ext_csd.data_sector_size) {
374 block_size = card->ext_csd.data_sector_size;
375 WARN_ON(block_size != 512 && block_size != 4096);
378 blk_queue_logical_block_size(mq->queue, block_size);
380 * After blk_queue_can_use_dma_map_merging() was called with succeed,
381 * since it calls blk_queue_virt_boundary(), the mmc should not call
382 * both blk_queue_max_segment_size().
384 if (!host->can_dma_map_merge)
385 blk_queue_max_segment_size(mq->queue,
386 round_down(host->max_seg_size, block_size));
388 dma_set_max_seg_size(mmc_dev(host), queue_max_segment_size(mq->queue));
390 INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
391 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
393 mutex_init(&mq->complete_lock);
395 init_waitqueue_head(&mq->wait);
398 static inline bool mmc_merge_capable(struct mmc_host *host)
400 return host->caps2 & MMC_CAP2_MERGE_CAPABLE;
403 /* Set queue depth to get a reasonable value for q->nr_requests */
404 #define MMC_QUEUE_DEPTH 64
407 * mmc_init_queue - initialise a queue structure.
409 * @card: mmc card to attach this queue
411 * Initialise a MMC card request queue.
413 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card)
415 struct mmc_host *host = card->host;
419 mq->use_cqe = host->cqe_enabled;
421 spin_lock_init(&mq->lock);
423 memset(&mq->tag_set, 0, sizeof(mq->tag_set));
424 mq->tag_set.ops = &mmc_mq_ops;
426 * The queue depth for CQE must match the hardware because the request
427 * tag is used to index the hardware queue.
430 mq->tag_set.queue_depth =
431 min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
433 mq->tag_set.queue_depth = MMC_QUEUE_DEPTH;
434 mq->tag_set.numa_node = NUMA_NO_NODE;
435 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
436 mq->tag_set.nr_hw_queues = 1;
437 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
438 mq->tag_set.driver_data = mq;
441 * Since blk_mq_alloc_tag_set() calls .init_request() of mmc_mq_ops,
442 * the host->can_dma_map_merge should be set before to get max_segs
443 * from mmc_get_max_segments().
445 if (mmc_merge_capable(host) &&
446 host->max_segs < MMC_DMA_MAP_MERGE_SEGMENTS &&
447 dma_get_merge_boundary(mmc_dev(host)))
448 host->can_dma_map_merge = 1;
450 host->can_dma_map_merge = 0;
452 ret = blk_mq_alloc_tag_set(&mq->tag_set);
456 mq->queue = blk_mq_init_queue(&mq->tag_set);
457 if (IS_ERR(mq->queue)) {
458 ret = PTR_ERR(mq->queue);
462 if (mmc_host_is_spi(host) && host->use_spi_crc)
463 mq->queue->backing_dev_info->capabilities |=
464 BDI_CAP_STABLE_WRITES;
466 mq->queue->queuedata = mq;
467 blk_queue_rq_timeout(mq->queue, 60 * HZ);
469 mmc_setup_queue(mq, card);
473 blk_mq_free_tag_set(&mq->tag_set);
477 void mmc_queue_suspend(struct mmc_queue *mq)
479 blk_mq_quiesce_queue(mq->queue);
482 * The host remains claimed while there are outstanding requests, so
483 * simply claiming and releasing here ensures there are none.
485 mmc_claim_host(mq->card->host);
486 mmc_release_host(mq->card->host);
489 void mmc_queue_resume(struct mmc_queue *mq)
491 blk_mq_unquiesce_queue(mq->queue);
494 void mmc_cleanup_queue(struct mmc_queue *mq)
496 struct request_queue *q = mq->queue;
499 * The legacy code handled the possibility of being suspended,
500 * so do that here too.
502 if (blk_queue_quiesced(q))
503 blk_mq_unquiesce_queue(q);
505 blk_cleanup_queue(q);
506 blk_mq_free_tag_set(&mq->tag_set);
509 * A request can be completed before the next request, potentially
510 * leaving a complete_work with nothing to do. Such a work item might
511 * still be queued at this point. Flush it.
513 flush_work(&mq->complete_work);
519 * Prepare the sg list(s) to be handed of to the host driver
521 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
523 struct request *req = mmc_queue_req_to_req(mqrq);
525 return blk_rq_map_sg(mq->queue, req, mqrq->sg);