2 * Copyright (C) 2003 Russell King, All Rights Reserved.
3 * Copyright 2006-2007 Pierre Ossman
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/blkdev.h>
13 #include <linux/freezer.h>
14 #include <linux/kthread.h>
15 #include <linux/scatterlist.h>
16 #include <linux/dma-mapping.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/host.h>
27 static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
29 /* Allow only 1 DCMD at a time */
30 return mq->in_flight[MMC_ISSUE_DCMD];
33 void mmc_cqe_check_busy(struct mmc_queue *mq)
35 if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
36 mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
38 mq->cqe_busy &= ~MMC_CQE_QUEUE_FULL;
41 static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
43 return host->caps2 & MMC_CAP2_CQE_DCMD;
46 static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
49 switch (req_op(req)) {
53 case REQ_OP_SECURE_ERASE:
54 return MMC_ISSUE_SYNC;
56 return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
58 return MMC_ISSUE_ASYNC;
62 enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
64 struct mmc_host *host = mq->card->host;
67 return mmc_cqe_issue_type(host, req);
69 if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
70 return MMC_ISSUE_ASYNC;
72 return MMC_ISSUE_SYNC;
75 static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
77 if (!mq->recovery_needed) {
78 mq->recovery_needed = true;
79 schedule_work(&mq->recovery_work);
83 void mmc_cqe_recovery_notifier(struct mmc_request *mrq)
85 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
87 struct request *req = mmc_queue_req_to_req(mqrq);
88 struct request_queue *q = req->q;
89 struct mmc_queue *mq = q->queuedata;
92 spin_lock_irqsave(q->queue_lock, flags);
93 __mmc_cqe_recovery_notifier(mq);
94 spin_unlock_irqrestore(q->queue_lock, flags);
97 static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
99 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
100 struct mmc_request *mrq = &mqrq->brq.mrq;
101 struct mmc_queue *mq = req->q->queuedata;
102 struct mmc_host *host = mq->card->host;
103 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
104 bool recovery_needed = false;
106 switch (issue_type) {
107 case MMC_ISSUE_ASYNC:
109 if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
111 __mmc_cqe_recovery_notifier(mq);
112 return BLK_EH_RESET_TIMER;
114 /* No timeout (XXX: huh? comment doesn't make much sense) */
115 blk_mq_complete_request(req);
118 /* Timeout is handled by mmc core */
119 return BLK_EH_RESET_TIMER;
123 static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
126 struct request_queue *q = req->q;
127 struct mmc_queue *mq = q->queuedata;
131 spin_lock_irqsave(q->queue_lock, flags);
133 if (mq->recovery_needed || !mq->use_cqe)
134 ret = BLK_EH_RESET_TIMER;
136 ret = mmc_cqe_timed_out(req);
138 spin_unlock_irqrestore(q->queue_lock, flags);
143 static void mmc_mq_recovery_handler(struct work_struct *work)
145 struct mmc_queue *mq = container_of(work, struct mmc_queue,
147 struct request_queue *q = mq->queue;
149 mmc_get_card(mq->card, &mq->ctx);
151 mq->in_recovery = true;
154 mmc_blk_cqe_recovery(mq);
156 mmc_blk_mq_recovery(mq);
158 mq->in_recovery = false;
160 spin_lock_irq(q->queue_lock);
161 mq->recovery_needed = false;
162 spin_unlock_irq(q->queue_lock);
164 mmc_put_card(mq->card, &mq->ctx);
166 blk_mq_run_hw_queues(q, true);
169 static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
171 struct scatterlist *sg;
173 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
175 sg_init_table(sg, sg_len);
180 static void mmc_queue_setup_discard(struct request_queue *q,
181 struct mmc_card *card)
183 unsigned max_discard;
185 max_discard = mmc_calc_max_discard(card);
189 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
190 blk_queue_max_discard_sectors(q, max_discard);
191 q->limits.discard_granularity = card->pref_erase << 9;
192 /* granularity must not be greater than max. discard */
193 if (card->pref_erase > max_discard)
194 q->limits.discard_granularity = 0;
195 if (mmc_can_secure_erase_trim(card))
196 blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
200 * mmc_init_request() - initialize the MMC-specific per-request data
201 * @q: the request queue
203 * @gfp: memory allocation policy
205 static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
208 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
209 struct mmc_card *card = mq->card;
210 struct mmc_host *host = card->host;
212 mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
219 static void mmc_exit_request(struct request_queue *q, struct request *req)
221 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
227 static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
228 unsigned int hctx_idx, unsigned int numa_node)
230 return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
233 static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
234 unsigned int hctx_idx)
236 struct mmc_queue *mq = set->driver_data;
238 mmc_exit_request(mq->queue, req);
242 * We use BLK_MQ_F_BLOCKING and have only 1 hardware queue, which means requests
243 * will not be dispatched in parallel.
245 static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
246 const struct blk_mq_queue_data *bd)
248 struct request *req = bd->rq;
249 struct request_queue *q = req->q;
250 struct mmc_queue *mq = q->queuedata;
251 struct mmc_card *card = mq->card;
252 struct mmc_host *host = card->host;
253 enum mmc_issue_type issue_type;
254 enum mmc_issued issued;
255 bool get_card, cqe_retune_ok;
258 if (mmc_card_removed(mq->card)) {
259 req->rq_flags |= RQF_QUIET;
260 return BLK_STS_IOERR;
263 issue_type = mmc_issue_type(mq, req);
265 spin_lock_irq(q->queue_lock);
267 if (mq->recovery_needed) {
268 spin_unlock_irq(q->queue_lock);
269 return BLK_STS_RESOURCE;
272 switch (issue_type) {
274 if (mmc_cqe_dcmd_busy(mq)) {
275 mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
276 spin_unlock_irq(q->queue_lock);
277 return BLK_STS_RESOURCE;
280 case MMC_ISSUE_ASYNC:
284 * Timeouts are handled by mmc core, and we don't have a host
285 * API to abort requests, so we can't handle the timeout anyway.
286 * However, when the timeout happens, blk_mq_complete_request()
287 * no longer works (to stop the request disappearing under us).
288 * To avoid racing with that, set a large timeout.
290 req->timeout = 600 * HZ;
294 mq->in_flight[issue_type] += 1;
295 get_card = (mmc_tot_in_flight(mq) == 1);
296 cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
298 spin_unlock_irq(q->queue_lock);
300 if (!(req->rq_flags & RQF_DONTPREP)) {
301 req_to_mmc_queue_req(req)->retries = 0;
302 req->rq_flags |= RQF_DONTPREP;
306 mmc_get_card(card, &mq->ctx);
309 host->retune_now = host->need_retune && cqe_retune_ok &&
313 blk_mq_start_request(req);
315 issued = mmc_blk_mq_issue_rq(mq, req);
319 ret = BLK_STS_RESOURCE;
321 case MMC_REQ_FAILED_TO_START:
329 if (issued != MMC_REQ_STARTED) {
330 bool put_card = false;
332 spin_lock_irq(q->queue_lock);
333 mq->in_flight[issue_type] -= 1;
334 if (mmc_tot_in_flight(mq) == 0)
336 spin_unlock_irq(q->queue_lock);
338 mmc_put_card(card, &mq->ctx);
344 static const struct blk_mq_ops mmc_mq_ops = {
345 .queue_rq = mmc_mq_queue_rq,
346 .init_request = mmc_mq_init_request,
347 .exit_request = mmc_mq_exit_request,
348 .complete = mmc_blk_mq_complete,
349 .timeout = mmc_mq_timed_out,
352 static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
354 struct mmc_host *host = card->host;
355 u64 limit = BLK_BOUNCE_HIGH;
357 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
358 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
360 blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
361 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
362 if (mmc_can_erase(card))
363 mmc_queue_setup_discard(mq->queue, card);
365 blk_queue_bounce_limit(mq->queue, limit);
366 blk_queue_max_hw_sectors(mq->queue,
367 min(host->max_blk_count, host->max_req_size / 512));
368 blk_queue_max_segments(mq->queue, host->max_segs);
369 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
371 INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
372 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
374 mutex_init(&mq->complete_lock);
376 init_waitqueue_head(&mq->wait);
379 static int mmc_mq_init_queue(struct mmc_queue *mq, int q_depth,
380 const struct blk_mq_ops *mq_ops, spinlock_t *lock)
384 memset(&mq->tag_set, 0, sizeof(mq->tag_set));
385 mq->tag_set.ops = mq_ops;
386 mq->tag_set.queue_depth = q_depth;
387 mq->tag_set.numa_node = NUMA_NO_NODE;
388 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE |
390 mq->tag_set.nr_hw_queues = 1;
391 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
392 mq->tag_set.driver_data = mq;
394 ret = blk_mq_alloc_tag_set(&mq->tag_set);
398 mq->queue = blk_mq_init_queue(&mq->tag_set);
399 if (IS_ERR(mq->queue)) {
400 ret = PTR_ERR(mq->queue);
404 mq->queue->queue_lock = lock;
405 mq->queue->queuedata = mq;
410 blk_mq_free_tag_set(&mq->tag_set);
415 /* Set queue depth to get a reasonable value for q->nr_requests */
416 #define MMC_QUEUE_DEPTH 64
418 static int mmc_mq_init(struct mmc_queue *mq, struct mmc_card *card,
421 struct mmc_host *host = card->host;
426 * The queue depth for CQE must match the hardware because the request
427 * tag is used to index the hardware queue.
430 q_depth = min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
432 q_depth = MMC_QUEUE_DEPTH;
434 ret = mmc_mq_init_queue(mq, q_depth, &mmc_mq_ops, lock);
438 blk_queue_rq_timeout(mq->queue, 60 * HZ);
440 mmc_setup_queue(mq, card);
446 * mmc_init_queue - initialise a queue structure.
448 * @card: mmc card to attach this queue
450 * @subname: partition subname
452 * Initialise a MMC card request queue.
454 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
455 spinlock_t *lock, const char *subname)
457 struct mmc_host *host = card->host;
461 mq->use_cqe = host->cqe_enabled;
463 return mmc_mq_init(mq, card, lock);
466 void mmc_queue_suspend(struct mmc_queue *mq)
468 blk_mq_quiesce_queue(mq->queue);
471 * The host remains claimed while there are outstanding requests, so
472 * simply claiming and releasing here ensures there are none.
474 mmc_claim_host(mq->card->host);
475 mmc_release_host(mq->card->host);
478 void mmc_queue_resume(struct mmc_queue *mq)
480 blk_mq_unquiesce_queue(mq->queue);
483 void mmc_cleanup_queue(struct mmc_queue *mq)
485 struct request_queue *q = mq->queue;
488 * The legacy code handled the possibility of being suspended,
489 * so do that here too.
491 if (blk_queue_quiesced(q))
492 blk_mq_unquiesce_queue(q);
494 blk_cleanup_queue(q);
497 * A request can be completed before the next request, potentially
498 * leaving a complete_work with nothing to do. Such a work item might
499 * still be queued at this point. Flush it.
501 flush_work(&mq->complete_work);
507 * Prepare the sg list(s) to be handed of to the host driver
509 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
511 struct request *req = mmc_queue_req_to_req(mqrq);
513 return blk_rq_map_sg(mq->queue, req, mqrq->sg);