1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2f397c8d8SUlf Hansson /*
3f397c8d8SUlf Hansson * Copyright (C) 2003 Russell King, All Rights Reserved.
4f397c8d8SUlf Hansson * Copyright 2006-2007 Pierre Ossman
5f397c8d8SUlf Hansson */
6f397c8d8SUlf Hansson #include <linux/slab.h>
7f397c8d8SUlf Hansson #include <linux/module.h>
8f397c8d8SUlf Hansson #include <linux/blkdev.h>
9f397c8d8SUlf Hansson #include <linux/freezer.h>
10f397c8d8SUlf Hansson #include <linux/scatterlist.h>
11f397c8d8SUlf Hansson #include <linux/dma-mapping.h>
123a6ffb3cSAndreas Koop #include <linux/backing-dev.h>
13f397c8d8SUlf Hansson
14f397c8d8SUlf Hansson #include <linux/mmc/card.h>
15f397c8d8SUlf Hansson #include <linux/mmc/host.h>
16f397c8d8SUlf Hansson
17f397c8d8SUlf Hansson #include "queue.h"
18f397c8d8SUlf Hansson #include "block.h"
1955244c56SUlf Hansson #include "core.h"
204facdde1SUlf Hansson #include "card.h"
2193f1c150SEric Biggers #include "crypto.h"
2281196976SAdrian Hunter #include "host.h"
23f397c8d8SUlf Hansson
2438c38cb7SYoshihiro Shimoda #define MMC_DMA_MAP_MERGE_SEGMENTS 512
2538c38cb7SYoshihiro Shimoda
mmc_cqe_dcmd_busy(struct mmc_queue * mq)261e8e55b6SAdrian Hunter static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
271e8e55b6SAdrian Hunter {
281e8e55b6SAdrian Hunter /* Allow only 1 DCMD at a time */
291e8e55b6SAdrian Hunter return mq->in_flight[MMC_ISSUE_DCMD];
301e8e55b6SAdrian Hunter }
311e8e55b6SAdrian Hunter
mmc_cqe_check_busy(struct mmc_queue * mq)321e8e55b6SAdrian Hunter void mmc_cqe_check_busy(struct mmc_queue *mq)
331e8e55b6SAdrian Hunter {
341e8e55b6SAdrian Hunter if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
351e8e55b6SAdrian Hunter mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
361e8e55b6SAdrian Hunter }
371e8e55b6SAdrian Hunter
mmc_cqe_can_dcmd(struct mmc_host * host)381e8e55b6SAdrian Hunter static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
391e8e55b6SAdrian Hunter {
401e8e55b6SAdrian Hunter return host->caps2 & MMC_CAP2_CQE_DCMD;
411e8e55b6SAdrian Hunter }
421e8e55b6SAdrian Hunter
mmc_cqe_issue_type(struct mmc_host * host,struct request * req)4315ff2946SColin Ian King static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
441e8e55b6SAdrian Hunter struct request *req)
451e8e55b6SAdrian Hunter {
461e8e55b6SAdrian Hunter switch (req_op(req)) {
471e8e55b6SAdrian Hunter case REQ_OP_DRV_IN:
481e8e55b6SAdrian Hunter case REQ_OP_DRV_OUT:
491e8e55b6SAdrian Hunter case REQ_OP_DISCARD:
501e8e55b6SAdrian Hunter case REQ_OP_SECURE_ERASE:
51*028822b7SVincent Whitchurch case REQ_OP_WRITE_ZEROES:
521e8e55b6SAdrian Hunter return MMC_ISSUE_SYNC;
531e8e55b6SAdrian Hunter case REQ_OP_FLUSH:
541e8e55b6SAdrian Hunter return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
551e8e55b6SAdrian Hunter default:
561e8e55b6SAdrian Hunter return MMC_ISSUE_ASYNC;
571e8e55b6SAdrian Hunter }
581e8e55b6SAdrian Hunter }
591e8e55b6SAdrian Hunter
mmc_issue_type(struct mmc_queue * mq,struct request * req)6081196976SAdrian Hunter enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
6181196976SAdrian Hunter {
621e8e55b6SAdrian Hunter struct mmc_host *host = mq->card->host;
631e8e55b6SAdrian Hunter
64407a1c57SLuca Porzio if (host->cqe_enabled && !host->hsq_enabled)
651e8e55b6SAdrian Hunter return mmc_cqe_issue_type(host, req);
661e8e55b6SAdrian Hunter
6781196976SAdrian Hunter if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
6881196976SAdrian Hunter return MMC_ISSUE_ASYNC;
6981196976SAdrian Hunter
7081196976SAdrian Hunter return MMC_ISSUE_SYNC;
7181196976SAdrian Hunter }
7281196976SAdrian Hunter
__mmc_cqe_recovery_notifier(struct mmc_queue * mq)731e8e55b6SAdrian Hunter static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
741e8e55b6SAdrian Hunter {
751e8e55b6SAdrian Hunter if (!mq->recovery_needed) {
761e8e55b6SAdrian Hunter mq->recovery_needed = true;
771e8e55b6SAdrian Hunter schedule_work(&mq->recovery_work);
781e8e55b6SAdrian Hunter }
791e8e55b6SAdrian Hunter }
801e8e55b6SAdrian Hunter
mmc_cqe_recovery_notifier(struct mmc_request * mrq)811e8e55b6SAdrian Hunter void mmc_cqe_recovery_notifier(struct mmc_request *mrq)
821e8e55b6SAdrian Hunter {
831e8e55b6SAdrian Hunter struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
841e8e55b6SAdrian Hunter brq.mrq);
851e8e55b6SAdrian Hunter struct request *req = mmc_queue_req_to_req(mqrq);
861e8e55b6SAdrian Hunter struct request_queue *q = req->q;
871e8e55b6SAdrian Hunter struct mmc_queue *mq = q->queuedata;
881e8e55b6SAdrian Hunter unsigned long flags;
891e8e55b6SAdrian Hunter
90f5d72c5cSChristoph Hellwig spin_lock_irqsave(&mq->lock, flags);
911e8e55b6SAdrian Hunter __mmc_cqe_recovery_notifier(mq);
92f5d72c5cSChristoph Hellwig spin_unlock_irqrestore(&mq->lock, flags);
931e8e55b6SAdrian Hunter }
941e8e55b6SAdrian Hunter
mmc_cqe_timed_out(struct request * req)951e8e55b6SAdrian Hunter static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
961e8e55b6SAdrian Hunter {
971e8e55b6SAdrian Hunter struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
981e8e55b6SAdrian Hunter struct mmc_request *mrq = &mqrq->brq.mrq;
991e8e55b6SAdrian Hunter struct mmc_queue *mq = req->q->queuedata;
1001e8e55b6SAdrian Hunter struct mmc_host *host = mq->card->host;
1011e8e55b6SAdrian Hunter enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1021e8e55b6SAdrian Hunter bool recovery_needed = false;
1031e8e55b6SAdrian Hunter
1041e8e55b6SAdrian Hunter switch (issue_type) {
1051e8e55b6SAdrian Hunter case MMC_ISSUE_ASYNC:
1061e8e55b6SAdrian Hunter case MMC_ISSUE_DCMD:
1071e8e55b6SAdrian Hunter if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
1081e8e55b6SAdrian Hunter if (recovery_needed)
10939a22f73SSarthak Garg mmc_cqe_recovery_notifier(mrq);
1101e8e55b6SAdrian Hunter return BLK_EH_RESET_TIMER;
1111e8e55b6SAdrian Hunter }
112c077dc5eSAdrian Hunter /* The request has gone already */
113ad73d6feSChristoph Hellwig return BLK_EH_DONE;
1141e8e55b6SAdrian Hunter default:
1151e8e55b6SAdrian Hunter /* Timeout is handled by mmc core */
1161e8e55b6SAdrian Hunter return BLK_EH_RESET_TIMER;
1171e8e55b6SAdrian Hunter }
1181e8e55b6SAdrian Hunter }
1191e8e55b6SAdrian Hunter
mmc_mq_timed_out(struct request * req)1209bdb4833SJohn Garry static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req)
12181196976SAdrian Hunter {
1221e8e55b6SAdrian Hunter struct request_queue *q = req->q;
1231e8e55b6SAdrian Hunter struct mmc_queue *mq = q->queuedata;
124511ce378SBaolin Wang struct mmc_card *card = mq->card;
125511ce378SBaolin Wang struct mmc_host *host = card->host;
1261e8e55b6SAdrian Hunter unsigned long flags;
12739a22f73SSarthak Garg bool ignore_tout;
1281e8e55b6SAdrian Hunter
129f5d72c5cSChristoph Hellwig spin_lock_irqsave(&mq->lock, flags);
130407a1c57SLuca Porzio ignore_tout = mq->recovery_needed || !host->cqe_enabled || host->hsq_enabled;
131f5d72c5cSChristoph Hellwig spin_unlock_irqrestore(&mq->lock, flags);
1321e8e55b6SAdrian Hunter
13339a22f73SSarthak Garg return ignore_tout ? BLK_EH_RESET_TIMER : mmc_cqe_timed_out(req);
1341e8e55b6SAdrian Hunter }
1351e8e55b6SAdrian Hunter
mmc_mq_recovery_handler(struct work_struct * work)1361e8e55b6SAdrian Hunter static void mmc_mq_recovery_handler(struct work_struct *work)
1371e8e55b6SAdrian Hunter {
1381e8e55b6SAdrian Hunter struct mmc_queue *mq = container_of(work, struct mmc_queue,
1391e8e55b6SAdrian Hunter recovery_work);
1401e8e55b6SAdrian Hunter struct request_queue *q = mq->queue;
141511ce378SBaolin Wang struct mmc_host *host = mq->card->host;
1421e8e55b6SAdrian Hunter
1431e8e55b6SAdrian Hunter mmc_get_card(mq->card, &mq->ctx);
1441e8e55b6SAdrian Hunter
1451e8e55b6SAdrian Hunter mq->in_recovery = true;
1461e8e55b6SAdrian Hunter
147407a1c57SLuca Porzio if (host->cqe_enabled && !host->hsq_enabled)
1481e8e55b6SAdrian Hunter mmc_blk_cqe_recovery(mq);
14910f21df4SAdrian Hunter else
15010f21df4SAdrian Hunter mmc_blk_mq_recovery(mq);
1511e8e55b6SAdrian Hunter
1521e8e55b6SAdrian Hunter mq->in_recovery = false;
1531e8e55b6SAdrian Hunter
154f5d72c5cSChristoph Hellwig spin_lock_irq(&mq->lock);
1551e8e55b6SAdrian Hunter mq->recovery_needed = false;
156f5d72c5cSChristoph Hellwig spin_unlock_irq(&mq->lock);
1571e8e55b6SAdrian Hunter
158511ce378SBaolin Wang if (host->hsq_enabled)
159511ce378SBaolin Wang host->cqe_ops->cqe_recovery_finish(host);
160511ce378SBaolin Wang
1611e8e55b6SAdrian Hunter mmc_put_card(mq->card, &mq->ctx);
1621e8e55b6SAdrian Hunter
1631e8e55b6SAdrian Hunter blk_mq_run_hw_queues(q, true);
16481196976SAdrian Hunter }
16581196976SAdrian Hunter
mmc_alloc_sg(unsigned short sg_len,gfp_t gfp)166f6f60707SChanWoo Lee static struct scatterlist *mmc_alloc_sg(unsigned short sg_len, gfp_t gfp)
167f397c8d8SUlf Hansson {
168f397c8d8SUlf Hansson struct scatterlist *sg;
169f397c8d8SUlf Hansson
170304419d8SLinus Walleij sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
1717b410d07SAdrian Hunter if (sg)
172f397c8d8SUlf Hansson sg_init_table(sg, sg_len);
173f397c8d8SUlf Hansson
174f397c8d8SUlf Hansson return sg;
175f397c8d8SUlf Hansson }
176f397c8d8SUlf Hansson
mmc_queue_setup_discard(struct request_queue * q,struct mmc_card * card)177f397c8d8SUlf Hansson static void mmc_queue_setup_discard(struct request_queue *q,
178f397c8d8SUlf Hansson struct mmc_card *card)
179f397c8d8SUlf Hansson {
180f397c8d8SUlf Hansson unsigned max_discard;
181f397c8d8SUlf Hansson
182f397c8d8SUlf Hansson max_discard = mmc_calc_max_discard(card);
183f397c8d8SUlf Hansson if (!max_discard)
184f397c8d8SUlf Hansson return;
185f397c8d8SUlf Hansson
186f397c8d8SUlf Hansson blk_queue_max_discard_sectors(q, max_discard);
187f397c8d8SUlf Hansson q->limits.discard_granularity = card->pref_erase << 9;
188f397c8d8SUlf Hansson /* granularity must not be greater than max. discard */
189f397c8d8SUlf Hansson if (card->pref_erase > max_discard)
19042432191SColy Li q->limits.discard_granularity = SECTOR_SIZE;
191f397c8d8SUlf Hansson if (mmc_can_secure_erase_trim(card))
19244abff2cSChristoph Hellwig blk_queue_max_secure_erase_sectors(q, max_discard);
193f7b6fc32SVincent Whitchurch if (mmc_can_trim(card) && card->erased_byte == 0)
194f7b6fc32SVincent Whitchurch blk_queue_max_write_zeroes_sectors(q, max_discard);
195f397c8d8SUlf Hansson }
196f397c8d8SUlf Hansson
mmc_get_max_segments(struct mmc_host * host)197f6f60707SChanWoo Lee static unsigned short mmc_get_max_segments(struct mmc_host *host)
19838c38cb7SYoshihiro Shimoda {
19938c38cb7SYoshihiro Shimoda return host->can_dma_map_merge ? MMC_DMA_MAP_MERGE_SEGMENTS :
20038c38cb7SYoshihiro Shimoda host->max_segs;
20138c38cb7SYoshihiro Shimoda }
20238c38cb7SYoshihiro Shimoda
mmc_mq_init_request(struct blk_mq_tag_set * set,struct request * req,unsigned int hctx_idx,unsigned int numa_node)203f80c8e68SChanWoo Lee static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
204f80c8e68SChanWoo Lee unsigned int hctx_idx, unsigned int numa_node)
2057b410d07SAdrian Hunter {
206304419d8SLinus Walleij struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
207f80c8e68SChanWoo Lee struct mmc_queue *mq = set->driver_data;
208304419d8SLinus Walleij struct mmc_card *card = mq->card;
2097b410d07SAdrian Hunter struct mmc_host *host = card->host;
210f397c8d8SUlf Hansson
211f80c8e68SChanWoo Lee mq_rq->sg = mmc_alloc_sg(mmc_get_max_segments(host), GFP_KERNEL);
212304419d8SLinus Walleij if (!mq_rq->sg)
213f397c8d8SUlf Hansson return -ENOMEM;
2147b410d07SAdrian Hunter
215f397c8d8SUlf Hansson return 0;
2167b410d07SAdrian Hunter }
2177b410d07SAdrian Hunter
mmc_mq_exit_request(struct blk_mq_tag_set * set,struct request * req,unsigned int hctx_idx)218f80c8e68SChanWoo Lee static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
219f80c8e68SChanWoo Lee unsigned int hctx_idx)
2207b410d07SAdrian Hunter {
221304419d8SLinus Walleij struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
222f397c8d8SUlf Hansson
223304419d8SLinus Walleij kfree(mq_rq->sg);
224304419d8SLinus Walleij mq_rq->sg = NULL;
225f397c8d8SUlf Hansson }
226f397c8d8SUlf Hansson
mmc_mq_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)22781196976SAdrian Hunter static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
22881196976SAdrian Hunter const struct blk_mq_queue_data *bd)
22981196976SAdrian Hunter {
23081196976SAdrian Hunter struct request *req = bd->rq;
23181196976SAdrian Hunter struct request_queue *q = req->q;
23281196976SAdrian Hunter struct mmc_queue *mq = q->queuedata;
23381196976SAdrian Hunter struct mmc_card *card = mq->card;
2341e8e55b6SAdrian Hunter struct mmc_host *host = card->host;
23581196976SAdrian Hunter enum mmc_issue_type issue_type;
23681196976SAdrian Hunter enum mmc_issued issued;
2371e8e55b6SAdrian Hunter bool get_card, cqe_retune_ok;
2381ccaa1bdSJoel Stanley blk_status_t ret;
23981196976SAdrian Hunter
24081196976SAdrian Hunter if (mmc_card_removed(mq->card)) {
24181196976SAdrian Hunter req->rq_flags |= RQF_QUIET;
24281196976SAdrian Hunter return BLK_STS_IOERR;
24381196976SAdrian Hunter }
24481196976SAdrian Hunter
24581196976SAdrian Hunter issue_type = mmc_issue_type(mq, req);
24681196976SAdrian Hunter
247f5d72c5cSChristoph Hellwig spin_lock_irq(&mq->lock);
24881196976SAdrian Hunter
24926caddf2SAdrian Hunter if (mq->recovery_needed || mq->busy) {
250f5d72c5cSChristoph Hellwig spin_unlock_irq(&mq->lock);
2511e8e55b6SAdrian Hunter return BLK_STS_RESOURCE;
2521e8e55b6SAdrian Hunter }
2531e8e55b6SAdrian Hunter
25481196976SAdrian Hunter switch (issue_type) {
2551e8e55b6SAdrian Hunter case MMC_ISSUE_DCMD:
2561e8e55b6SAdrian Hunter if (mmc_cqe_dcmd_busy(mq)) {
2571e8e55b6SAdrian Hunter mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
258f5d72c5cSChristoph Hellwig spin_unlock_irq(&mq->lock);
2591e8e55b6SAdrian Hunter return BLK_STS_RESOURCE;
2601e8e55b6SAdrian Hunter }
2611e8e55b6SAdrian Hunter break;
26281196976SAdrian Hunter case MMC_ISSUE_ASYNC:
263511ce378SBaolin Wang /*
264511ce378SBaolin Wang * For MMC host software queue, we only allow 2 requests in
265511ce378SBaolin Wang * flight to avoid a long latency.
266511ce378SBaolin Wang */
267511ce378SBaolin Wang if (host->hsq_enabled && mq->in_flight[issue_type] > 2) {
268511ce378SBaolin Wang spin_unlock_irq(&mq->lock);
269511ce378SBaolin Wang return BLK_STS_RESOURCE;
270511ce378SBaolin Wang }
27181196976SAdrian Hunter break;
27281196976SAdrian Hunter default:
27381196976SAdrian Hunter /*
27481196976SAdrian Hunter * Timeouts are handled by mmc core, and we don't have a host
27581196976SAdrian Hunter * API to abort requests, so we can't handle the timeout anyway.
27681196976SAdrian Hunter * However, when the timeout happens, blk_mq_complete_request()
27781196976SAdrian Hunter * no longer works (to stop the request disappearing under us).
27881196976SAdrian Hunter * To avoid racing with that, set a large timeout.
27981196976SAdrian Hunter */
28081196976SAdrian Hunter req->timeout = 600 * HZ;
28181196976SAdrian Hunter break;
28281196976SAdrian Hunter }
28381196976SAdrian Hunter
28426caddf2SAdrian Hunter /* Parallel dispatch of requests is not supported at the moment */
28526caddf2SAdrian Hunter mq->busy = true;
28626caddf2SAdrian Hunter
28781196976SAdrian Hunter mq->in_flight[issue_type] += 1;
28881196976SAdrian Hunter get_card = (mmc_tot_in_flight(mq) == 1);
2891e8e55b6SAdrian Hunter cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
29081196976SAdrian Hunter
291f5d72c5cSChristoph Hellwig spin_unlock_irq(&mq->lock);
29281196976SAdrian Hunter
29381196976SAdrian Hunter if (!(req->rq_flags & RQF_DONTPREP)) {
29481196976SAdrian Hunter req_to_mmc_queue_req(req)->retries = 0;
29581196976SAdrian Hunter req->rq_flags |= RQF_DONTPREP;
29681196976SAdrian Hunter }
29781196976SAdrian Hunter
29881196976SAdrian Hunter if (get_card)
29981196976SAdrian Hunter mmc_get_card(card, &mq->ctx);
30081196976SAdrian Hunter
301407a1c57SLuca Porzio if (host->cqe_enabled) {
3021e8e55b6SAdrian Hunter host->retune_now = host->need_retune && cqe_retune_ok &&
3031e8e55b6SAdrian Hunter !host->hold_retune;
3041e8e55b6SAdrian Hunter }
3051e8e55b6SAdrian Hunter
30681196976SAdrian Hunter blk_mq_start_request(req);
30781196976SAdrian Hunter
30881196976SAdrian Hunter issued = mmc_blk_mq_issue_rq(mq, req);
30981196976SAdrian Hunter
31081196976SAdrian Hunter switch (issued) {
31181196976SAdrian Hunter case MMC_REQ_BUSY:
31281196976SAdrian Hunter ret = BLK_STS_RESOURCE;
31381196976SAdrian Hunter break;
31481196976SAdrian Hunter case MMC_REQ_FAILED_TO_START:
31581196976SAdrian Hunter ret = BLK_STS_IOERR;
31681196976SAdrian Hunter break;
31781196976SAdrian Hunter default:
31881196976SAdrian Hunter ret = BLK_STS_OK;
31981196976SAdrian Hunter break;
32081196976SAdrian Hunter }
32181196976SAdrian Hunter
32281196976SAdrian Hunter if (issued != MMC_REQ_STARTED) {
32381196976SAdrian Hunter bool put_card = false;
32481196976SAdrian Hunter
325f5d72c5cSChristoph Hellwig spin_lock_irq(&mq->lock);
32681196976SAdrian Hunter mq->in_flight[issue_type] -= 1;
32781196976SAdrian Hunter if (mmc_tot_in_flight(mq) == 0)
32881196976SAdrian Hunter put_card = true;
32926caddf2SAdrian Hunter mq->busy = false;
330f5d72c5cSChristoph Hellwig spin_unlock_irq(&mq->lock);
33181196976SAdrian Hunter if (put_card)
33281196976SAdrian Hunter mmc_put_card(card, &mq->ctx);
33326caddf2SAdrian Hunter } else {
33426caddf2SAdrian Hunter WRITE_ONCE(mq->busy, false);
33581196976SAdrian Hunter }
33681196976SAdrian Hunter
33781196976SAdrian Hunter return ret;
33881196976SAdrian Hunter }
33981196976SAdrian Hunter
34081196976SAdrian Hunter static const struct blk_mq_ops mmc_mq_ops = {
34181196976SAdrian Hunter .queue_rq = mmc_mq_queue_rq,
34281196976SAdrian Hunter .init_request = mmc_mq_init_request,
34381196976SAdrian Hunter .exit_request = mmc_mq_exit_request,
34481196976SAdrian Hunter .complete = mmc_blk_mq_complete,
34581196976SAdrian Hunter .timeout = mmc_mq_timed_out,
34681196976SAdrian Hunter };
34781196976SAdrian Hunter
mmc_setup_queue(struct mmc_queue * mq,struct mmc_card * card)348c8b5fd03SAdrian Hunter static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
349c8b5fd03SAdrian Hunter {
350c8b5fd03SAdrian Hunter struct mmc_host *host = card->host;
351c53336c8SMing Lei unsigned block_size = 512;
352c8b5fd03SAdrian Hunter
3538b904b5bSBart Van Assche blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
3548b904b5bSBart Van Assche blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
355c8b5fd03SAdrian Hunter if (mmc_can_erase(card))
356c8b5fd03SAdrian Hunter mmc_queue_setup_discard(mq->queue, card);
357c8b5fd03SAdrian Hunter
3587559d612SChristoph Hellwig if (!mmc_dev(host)->dma_mask || !*mmc_dev(host)->dma_mask)
3597559d612SChristoph Hellwig blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH);
360c8b5fd03SAdrian Hunter blk_queue_max_hw_sectors(mq->queue,
361c8b5fd03SAdrian Hunter min(host->max_blk_count, host->max_req_size / 512));
36238c38cb7SYoshihiro Shimoda if (host->can_dma_map_merge)
36338c38cb7SYoshihiro Shimoda WARN(!blk_queue_can_use_dma_map_merging(mq->queue,
36438c38cb7SYoshihiro Shimoda mmc_dev(host)),
36538c38cb7SYoshihiro Shimoda "merging was advertised but not possible");
36638c38cb7SYoshihiro Shimoda blk_queue_max_segments(mq->queue, mmc_get_max_segments(host));
367c53336c8SMing Lei
368b5030874SPeter Collingbourne if (mmc_card_mmc(card) && card->ext_csd.data_sector_size) {
369c53336c8SMing Lei block_size = card->ext_csd.data_sector_size;
370b5030874SPeter Collingbourne WARN_ON(block_size != 512 && block_size != 4096);
371b5030874SPeter Collingbourne }
372c53336c8SMing Lei
373c53336c8SMing Lei blk_queue_logical_block_size(mq->queue, block_size);
37438c38cb7SYoshihiro Shimoda /*
37538c38cb7SYoshihiro Shimoda * After blk_queue_can_use_dma_map_merging() was called with succeed,
37638c38cb7SYoshihiro Shimoda * since it calls blk_queue_virt_boundary(), the mmc should not call
37738c38cb7SYoshihiro Shimoda * both blk_queue_max_segment_size().
37838c38cb7SYoshihiro Shimoda */
37938c38cb7SYoshihiro Shimoda if (!host->can_dma_map_merge)
380c53336c8SMing Lei blk_queue_max_segment_size(mq->queue,
381c53336c8SMing Lei round_down(host->max_seg_size, block_size));
382c8b5fd03SAdrian Hunter
383cf1db7fcSChristoph Hellwig dma_set_max_seg_size(mmc_dev(host), queue_max_segment_size(mq->queue));
384cf1db7fcSChristoph Hellwig
3851e8e55b6SAdrian Hunter INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
38681196976SAdrian Hunter INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
38781196976SAdrian Hunter
38881196976SAdrian Hunter mutex_init(&mq->complete_lock);
38981196976SAdrian Hunter
39081196976SAdrian Hunter init_waitqueue_head(&mq->wait);
39193f1c150SEric Biggers
39293f1c150SEric Biggers mmc_crypto_setup_queue(mq->queue, host);
39381196976SAdrian Hunter }
39481196976SAdrian Hunter
mmc_merge_capable(struct mmc_host * host)395427b0034SYoshihiro Shimoda static inline bool mmc_merge_capable(struct mmc_host *host)
396427b0034SYoshihiro Shimoda {
397427b0034SYoshihiro Shimoda return host->caps2 & MMC_CAP2_MERGE_CAPABLE;
398427b0034SYoshihiro Shimoda }
399427b0034SYoshihiro Shimoda
400b061b326SChristoph Hellwig /* Set queue depth to get a reasonable value for q->nr_requests */
401b061b326SChristoph Hellwig #define MMC_QUEUE_DEPTH 64
402b061b326SChristoph Hellwig
403b061b326SChristoph Hellwig /**
404b061b326SChristoph Hellwig * mmc_init_queue - initialise a queue structure.
405b061b326SChristoph Hellwig * @mq: mmc queue
406b061b326SChristoph Hellwig * @card: mmc card to attach this queue
407b061b326SChristoph Hellwig *
408b061b326SChristoph Hellwig * Initialise a MMC card request queue.
409b061b326SChristoph Hellwig */
mmc_init_queue(struct mmc_queue * mq,struct mmc_card * card)410607d968aSChristoph Hellwig struct gendisk *mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card)
41181196976SAdrian Hunter {
412b061b326SChristoph Hellwig struct mmc_host *host = card->host;
413607d968aSChristoph Hellwig struct gendisk *disk;
41481196976SAdrian Hunter int ret;
41581196976SAdrian Hunter
416b061b326SChristoph Hellwig mq->card = card;
417b061b326SChristoph Hellwig
418f5d72c5cSChristoph Hellwig spin_lock_init(&mq->lock);
419f5d72c5cSChristoph Hellwig
42081196976SAdrian Hunter memset(&mq->tag_set, 0, sizeof(mq->tag_set));
421b061b326SChristoph Hellwig mq->tag_set.ops = &mmc_mq_ops;
422b061b326SChristoph Hellwig /*
423b061b326SChristoph Hellwig * The queue depth for CQE must match the hardware because the request
424b061b326SChristoph Hellwig * tag is used to index the hardware queue.
425b061b326SChristoph Hellwig */
426407a1c57SLuca Porzio if (host->cqe_enabled && !host->hsq_enabled)
427b061b326SChristoph Hellwig mq->tag_set.queue_depth =
428b061b326SChristoph Hellwig min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
429b061b326SChristoph Hellwig else
430b061b326SChristoph Hellwig mq->tag_set.queue_depth = MMC_QUEUE_DEPTH;
43181196976SAdrian Hunter mq->tag_set.numa_node = NUMA_NO_NODE;
43256d18f62SMing Lei mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
43381196976SAdrian Hunter mq->tag_set.nr_hw_queues = 1;
43481196976SAdrian Hunter mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
43581196976SAdrian Hunter mq->tag_set.driver_data = mq;
43681196976SAdrian Hunter
43738c38cb7SYoshihiro Shimoda /*
43838c38cb7SYoshihiro Shimoda * Since blk_mq_alloc_tag_set() calls .init_request() of mmc_mq_ops,
43938c38cb7SYoshihiro Shimoda * the host->can_dma_map_merge should be set before to get max_segs
44038c38cb7SYoshihiro Shimoda * from mmc_get_max_segments().
44138c38cb7SYoshihiro Shimoda */
442427b0034SYoshihiro Shimoda if (mmc_merge_capable(host) &&
443427b0034SYoshihiro Shimoda host->max_segs < MMC_DMA_MAP_MERGE_SEGMENTS &&
44438c38cb7SYoshihiro Shimoda dma_get_merge_boundary(mmc_dev(host)))
44538c38cb7SYoshihiro Shimoda host->can_dma_map_merge = 1;
44638c38cb7SYoshihiro Shimoda else
44738c38cb7SYoshihiro Shimoda host->can_dma_map_merge = 0;
44838c38cb7SYoshihiro Shimoda
44981196976SAdrian Hunter ret = blk_mq_alloc_tag_set(&mq->tag_set);
45081196976SAdrian Hunter if (ret)
451607d968aSChristoph Hellwig return ERR_PTR(ret);
45281196976SAdrian Hunter
453607d968aSChristoph Hellwig
454607d968aSChristoph Hellwig disk = blk_mq_alloc_disk(&mq->tag_set, mq);
455607d968aSChristoph Hellwig if (IS_ERR(disk)) {
456607d968aSChristoph Hellwig blk_mq_free_tag_set(&mq->tag_set);
457607d968aSChristoph Hellwig return disk;
45881196976SAdrian Hunter }
459607d968aSChristoph Hellwig mq->queue = disk->queue;
46081196976SAdrian Hunter
4613a6ffb3cSAndreas Koop if (mmc_host_is_spi(host) && host->use_spi_crc)
4621cb039f3SChristoph Hellwig blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, mq->queue);
463b061b326SChristoph Hellwig blk_queue_rq_timeout(mq->queue, 60 * HZ);
46481196976SAdrian Hunter
465b061b326SChristoph Hellwig mmc_setup_queue(mq, card);
466607d968aSChristoph Hellwig return disk;
46781196976SAdrian Hunter }
46881196976SAdrian Hunter
mmc_queue_suspend(struct mmc_queue * mq)4690fbfd125SAdrian Hunter void mmc_queue_suspend(struct mmc_queue *mq)
470f397c8d8SUlf Hansson {
47181196976SAdrian Hunter blk_mq_quiesce_queue(mq->queue);
472f397c8d8SUlf Hansson
47381196976SAdrian Hunter /*
47481196976SAdrian Hunter * The host remains claimed while there are outstanding requests, so
47581196976SAdrian Hunter * simply claiming and releasing here ensures there are none.
47681196976SAdrian Hunter */
47781196976SAdrian Hunter mmc_claim_host(mq->card->host);
47881196976SAdrian Hunter mmc_release_host(mq->card->host);
479f397c8d8SUlf Hansson }
480f397c8d8SUlf Hansson
mmc_queue_resume(struct mmc_queue * mq)4810fbfd125SAdrian Hunter void mmc_queue_resume(struct mmc_queue *mq)
48281196976SAdrian Hunter {
48381196976SAdrian Hunter blk_mq_unquiesce_queue(mq->queue);
48481196976SAdrian Hunter }
48581196976SAdrian Hunter
mmc_cleanup_queue(struct mmc_queue * mq)48681196976SAdrian Hunter void mmc_cleanup_queue(struct mmc_queue *mq)
48781196976SAdrian Hunter {
48881196976SAdrian Hunter struct request_queue *q = mq->queue;
48981196976SAdrian Hunter
49081196976SAdrian Hunter /*
49181196976SAdrian Hunter * The legacy code handled the possibility of being suspended,
49281196976SAdrian Hunter * so do that here too.
49381196976SAdrian Hunter */
49481196976SAdrian Hunter if (blk_queue_quiesced(q))
49581196976SAdrian Hunter blk_mq_unquiesce_queue(q);
49681196976SAdrian Hunter
497339e3eb1SChristian Löhle /*
498339e3eb1SChristian Löhle * If the recovery completes the last (and only remaining) request in
499339e3eb1SChristian Löhle * the queue, and the card has been removed, we could end up here with
500339e3eb1SChristian Löhle * the recovery not quite finished yet, so cancel it.
501339e3eb1SChristian Löhle */
502339e3eb1SChristian Löhle cancel_work_sync(&mq->recovery_work);
503339e3eb1SChristian Löhle
50443d8dabbSRaul E Rangel blk_mq_free_tag_set(&mq->tag_set);
50581196976SAdrian Hunter
50681196976SAdrian Hunter /*
50781196976SAdrian Hunter * A request can be completed before the next request, potentially
50881196976SAdrian Hunter * leaving a complete_work with nothing to do. Such a work item might
50981196976SAdrian Hunter * still be queued at this point. Flush it.
51081196976SAdrian Hunter */
51181196976SAdrian Hunter flush_work(&mq->complete_work);
51281196976SAdrian Hunter
51381196976SAdrian Hunter mq->card = NULL;
51481196976SAdrian Hunter }
51581196976SAdrian Hunter
516f397c8d8SUlf Hansson /*
517f397c8d8SUlf Hansson * Prepare the sg list(s) to be handed of to the host driver
518f397c8d8SUlf Hansson */
mmc_queue_map_sg(struct mmc_queue * mq,struct mmc_queue_req * mqrq)519f397c8d8SUlf Hansson unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
520f397c8d8SUlf Hansson {
52167e69d52SLinus Walleij struct request *req = mmc_queue_req_to_req(mqrq);
522f397c8d8SUlf Hansson
52367e69d52SLinus Walleij return blk_rq_map_sg(mq->queue, req, mqrq->sg);
524f397c8d8SUlf Hansson }
525