blob: d8394007bc99652a5142a5a9b8e75f939298ea26 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2003 Russell King, All Rights Reserved.
Pierre Ossman98ac2162006-12-23 20:03:02 +01003 * Copyright 2006-2007 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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.
8 *
9 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/blkdev.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070013#include <linux/freezer.h>
Christoph Hellwig87598a22006-11-13 20:23:52 +010014#include <linux/kthread.h>
Jens Axboe45711f12007-10-22 21:19:53 +020015#include <linux/scatterlist.h>
Santosh Shilimkar8e0cb8a2013-07-29 14:20:15 +010016#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/mmc/card.h>
19#include <linux/mmc/host.h>
Linus Walleij29eb7bd2016-09-20 11:34:38 +020020
Pierre Ossman98ac2162006-12-23 20:03:02 +010021#include "queue.h"
Linus Walleij29eb7bd2016-09-20 11:34:38 +020022#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010023#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010024#include "card.h"
Adrian Hunter81196972017-11-29 15:41:03 +020025#include "host.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020028 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30static int mmc_prep_request(struct request_queue *q, struct request *req)
31{
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053032 struct mmc_queue *mq = q->queuedata;
33
Linus Walleij14f4ca72017-09-20 10:02:01 +020034 if (mq && mmc_card_removed(mq->card))
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053035 return BLKPREP_KILL;
36
Christoph Hellwige8064022016-10-20 15:12:13 +020037 req->rq_flags |= RQF_DONTPREP;
Adrian Hunter81196972017-11-29 15:41:03 +020038 req_to_mmc_queue_req(req)->retries = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020040 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Adrian Hunter1e8e55b2017-11-29 15:41:04 +020043static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
44{
45 /* Allow only 1 DCMD at a time */
46 return mq->in_flight[MMC_ISSUE_DCMD];
47}
48
49void mmc_cqe_check_busy(struct mmc_queue *mq)
50{
51 if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
52 mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
53
54 mq->cqe_busy &= ~MMC_CQE_QUEUE_FULL;
55}
56
57static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
58{
59 return host->caps2 & MMC_CAP2_CQE_DCMD;
60}
61
62enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
63 struct request *req)
64{
65 switch (req_op(req)) {
66 case REQ_OP_DRV_IN:
67 case REQ_OP_DRV_OUT:
68 case REQ_OP_DISCARD:
69 case REQ_OP_SECURE_ERASE:
70 return MMC_ISSUE_SYNC;
71 case REQ_OP_FLUSH:
72 return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
73 default:
74 return MMC_ISSUE_ASYNC;
75 }
76}
77
Adrian Hunter81196972017-11-29 15:41:03 +020078enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
79{
Adrian Hunter1e8e55b2017-11-29 15:41:04 +020080 struct mmc_host *host = mq->card->host;
81
82 if (mq->use_cqe)
83 return mmc_cqe_issue_type(host, req);
84
Adrian Hunter81196972017-11-29 15:41:03 +020085 if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
86 return MMC_ISSUE_ASYNC;
87
88 return MMC_ISSUE_SYNC;
89}
90
Adrian Hunter1e8e55b2017-11-29 15:41:04 +020091static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
92{
93 if (!mq->recovery_needed) {
94 mq->recovery_needed = true;
95 schedule_work(&mq->recovery_work);
96 }
97}
98
99void mmc_cqe_recovery_notifier(struct mmc_request *mrq)
100{
101 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
102 brq.mrq);
103 struct request *req = mmc_queue_req_to_req(mqrq);
104 struct request_queue *q = req->q;
105 struct mmc_queue *mq = q->queuedata;
106 unsigned long flags;
107
108 spin_lock_irqsave(q->queue_lock, flags);
109 __mmc_cqe_recovery_notifier(mq);
110 spin_unlock_irqrestore(q->queue_lock, flags);
111}
112
113static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
114{
115 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
116 struct mmc_request *mrq = &mqrq->brq.mrq;
117 struct mmc_queue *mq = req->q->queuedata;
118 struct mmc_host *host = mq->card->host;
119 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
120 bool recovery_needed = false;
121
122 switch (issue_type) {
123 case MMC_ISSUE_ASYNC:
124 case MMC_ISSUE_DCMD:
125 if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
126 if (recovery_needed)
127 __mmc_cqe_recovery_notifier(mq);
128 return BLK_EH_RESET_TIMER;
129 }
130 /* No timeout */
131 return BLK_EH_HANDLED;
132 default:
133 /* Timeout is handled by mmc core */
134 return BLK_EH_RESET_TIMER;
135 }
136}
137
Adrian Hunter81196972017-11-29 15:41:03 +0200138static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
139 bool reserved)
140{
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200141 struct request_queue *q = req->q;
142 struct mmc_queue *mq = q->queuedata;
143 unsigned long flags;
144 int ret;
145
146 spin_lock_irqsave(q->queue_lock, flags);
147
148 if (mq->recovery_needed || !mq->use_cqe)
149 ret = BLK_EH_RESET_TIMER;
150 else
151 ret = mmc_cqe_timed_out(req);
152
153 spin_unlock_irqrestore(q->queue_lock, flags);
154
155 return ret;
156}
157
158static void mmc_mq_recovery_handler(struct work_struct *work)
159{
160 struct mmc_queue *mq = container_of(work, struct mmc_queue,
161 recovery_work);
162 struct request_queue *q = mq->queue;
163
164 mmc_get_card(mq->card, &mq->ctx);
165
166 mq->in_recovery = true;
167
Adrian Hunter10f21df42017-11-29 15:41:07 +0200168 if (mq->use_cqe)
169 mmc_blk_cqe_recovery(mq);
170 else
171 mmc_blk_mq_recovery(mq);
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200172
173 mq->in_recovery = false;
174
175 spin_lock_irq(q->queue_lock);
176 mq->recovery_needed = false;
177 spin_unlock_irq(q->queue_lock);
178
179 mmc_put_card(mq->card, &mq->ctx);
180
181 blk_mq_run_hw_queues(q, true);
Adrian Hunter81196972017-11-29 15:41:03 +0200182}
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184static int mmc_queue_thread(void *d)
185{
186 struct mmc_queue *mq = d;
187 struct request_queue *q = mq->queue;
Adrian Huntere0097cf2016-11-29 12:09:10 +0200188 struct mmc_context_info *cntx = &mq->card->host->context_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Rafael J. Wysocki83144182007-07-17 04:03:35 -0700190 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 do {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200194 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 spin_lock_irq(q->queue_lock);
197 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +0100198 req = blk_fetch_request(q);
Adrian Huntere0097cf2016-11-29 12:09:10 +0200199 mq->asleep = false;
200 cntx->is_waiting_last_req = false;
201 cntx->is_new_req = false;
202 if (!req) {
203 /*
204 * Dispatch queue is empty so set flags for
205 * mmc_request_fn() to wake us up.
206 */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200207 if (mq->qcnt)
Adrian Huntere0097cf2016-11-29 12:09:10 +0200208 cntx->is_waiting_last_req = true;
209 else
210 mq->asleep = true;
211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 spin_unlock_irq(q->queue_lock);
213
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200214 if (req || mq->qcnt) {
Per Forlinee8a43a2011-07-01 18:55:33 +0200215 set_current_state(TASK_RUNNING);
Linus Walleij29eb7bd2016-09-20 11:34:38 +0200216 mmc_blk_issue_rq(mq, req);
Rabin Vincenta8c27c02015-06-14 19:26:11 +0200217 cond_resched();
Per Forlinee8a43a2011-07-01 18:55:33 +0200218 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +0100219 if (kthread_should_stop()) {
220 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +0100222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 up(&mq->thread_sem);
224 schedule();
225 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 up(&mq->thread_sem);
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return 0;
231}
232
233/*
234 * Generic MMC request handler. This is called for any queue on a
235 * particular host. When the host is not busy, we look for a request
236 * on any queue on this host, and attempt to issue it. This may
237 * not be the queue we were asked to process.
238 */
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530239static void mmc_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100242 struct request *req;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500243 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100244
245 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800246 while ((req = blk_fetch_request(q)) != NULL) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200247 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200248 __blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800249 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100250 return;
251 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500253 cntx = &mq->card->host->context_info;
Adrian Huntere0097cf2016-11-29 12:09:10 +0200254
255 if (cntx->is_waiting_last_req) {
256 cntx->is_new_req = true;
257 wake_up_interruptible(&cntx->wait);
258 }
259
260 if (mq->asleep)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100261 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Linus Walleij304419d2017-05-18 11:29:32 +0200264static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
Per Forlin97868a22011-07-09 17:12:36 -0400265{
266 struct scatterlist *sg;
267
Linus Walleij304419d2017-05-18 11:29:32 +0200268 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
Adrian Hunter7b410d02017-03-13 14:36:36 +0200269 if (sg)
Per Forlin97868a22011-07-09 17:12:36 -0400270 sg_init_table(sg, sg_len);
Per Forlin97868a22011-07-09 17:12:36 -0400271
272 return sg;
273}
274
Adrian Huntere056a1b2011-06-28 17:16:02 +0300275static void mmc_queue_setup_discard(struct request_queue *q,
276 struct mmc_card *card)
277{
278 unsigned max_discard;
279
280 max_discard = mmc_calc_max_discard(card);
281 if (!max_discard)
282 return;
283
284 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600285 blk_queue_max_discard_sectors(q, max_discard);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300286 q->limits.discard_granularity = card->pref_erase << 9;
287 /* granularity must not be greater than max. discard */
288 if (card->pref_erase > max_discard)
289 q->limits.discard_granularity = 0;
Maya Erez775a9362013-04-18 15:41:55 +0300290 if (mmc_can_secure_erase_trim(card))
Christoph Hellwig288dab82016-06-09 16:00:36 +0200291 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300292}
293
Linus Walleij304419d2017-05-18 11:29:32 +0200294/**
295 * mmc_init_request() - initialize the MMC-specific per-request data
296 * @q: the request queue
297 * @req: the request
298 * @gfp: memory allocation policy
299 */
Adrian Hunter81196972017-11-29 15:41:03 +0200300static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
301 gfp_t gfp)
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200302{
Linus Walleij304419d2017-05-18 11:29:32 +0200303 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
Linus Walleij304419d2017-05-18 11:29:32 +0200304 struct mmc_card *card = mq->card;
305 struct mmc_host *host = card->host;
Adrian Hunterc8539822016-11-29 12:09:11 +0200306
Linus Walleijde3ee992017-09-20 10:56:14 +0200307 mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
308 if (!mq_rq->sg)
309 return -ENOMEM;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200310
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200311 return 0;
312}
Adrian Hunter64e29e422016-11-29 12:09:13 +0200313
Adrian Hunter81196972017-11-29 15:41:03 +0200314static int mmc_init_request(struct request_queue *q, struct request *req,
315 gfp_t gfp)
316{
317 return __mmc_init_request(q->queuedata, req, gfp);
318}
319
Linus Walleij304419d2017-05-18 11:29:32 +0200320static void mmc_exit_request(struct request_queue *q, struct request *req)
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200321{
Linus Walleij304419d2017-05-18 11:29:32 +0200322 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
Adrian Hunter64e29e422016-11-29 12:09:13 +0200323
Linus Walleij304419d2017-05-18 11:29:32 +0200324 kfree(mq_rq->sg);
325 mq_rq->sg = NULL;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200326}
327
Adrian Hunter81196972017-11-29 15:41:03 +0200328static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
329 unsigned int hctx_idx, unsigned int numa_node)
330{
331 return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
332}
333
334static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
335 unsigned int hctx_idx)
336{
337 struct mmc_queue *mq = set->driver_data;
338
339 mmc_exit_request(mq->queue, req);
340}
341
342/*
343 * We use BLK_MQ_F_BLOCKING and have only 1 hardware queue, which means requests
344 * will not be dispatched in parallel.
345 */
346static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
347 const struct blk_mq_queue_data *bd)
348{
349 struct request *req = bd->rq;
350 struct request_queue *q = req->q;
351 struct mmc_queue *mq = q->queuedata;
352 struct mmc_card *card = mq->card;
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200353 struct mmc_host *host = card->host;
Adrian Hunter81196972017-11-29 15:41:03 +0200354 enum mmc_issue_type issue_type;
355 enum mmc_issued issued;
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200356 bool get_card, cqe_retune_ok;
Adrian Hunter81196972017-11-29 15:41:03 +0200357 int ret;
358
359 if (mmc_card_removed(mq->card)) {
360 req->rq_flags |= RQF_QUIET;
361 return BLK_STS_IOERR;
362 }
363
364 issue_type = mmc_issue_type(mq, req);
365
366 spin_lock_irq(q->queue_lock);
367
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200368 if (mq->recovery_needed) {
369 spin_unlock_irq(q->queue_lock);
370 return BLK_STS_RESOURCE;
371 }
372
Adrian Hunter81196972017-11-29 15:41:03 +0200373 switch (issue_type) {
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200374 case MMC_ISSUE_DCMD:
375 if (mmc_cqe_dcmd_busy(mq)) {
376 mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
377 spin_unlock_irq(q->queue_lock);
378 return BLK_STS_RESOURCE;
379 }
380 break;
Adrian Hunter81196972017-11-29 15:41:03 +0200381 case MMC_ISSUE_ASYNC:
382 break;
383 default:
384 /*
385 * Timeouts are handled by mmc core, and we don't have a host
386 * API to abort requests, so we can't handle the timeout anyway.
387 * However, when the timeout happens, blk_mq_complete_request()
388 * no longer works (to stop the request disappearing under us).
389 * To avoid racing with that, set a large timeout.
390 */
391 req->timeout = 600 * HZ;
392 break;
393 }
394
395 mq->in_flight[issue_type] += 1;
396 get_card = (mmc_tot_in_flight(mq) == 1);
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200397 cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
Adrian Hunter81196972017-11-29 15:41:03 +0200398
399 spin_unlock_irq(q->queue_lock);
400
401 if (!(req->rq_flags & RQF_DONTPREP)) {
402 req_to_mmc_queue_req(req)->retries = 0;
403 req->rq_flags |= RQF_DONTPREP;
404 }
405
406 if (get_card)
407 mmc_get_card(card, &mq->ctx);
408
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200409 if (mq->use_cqe) {
410 host->retune_now = host->need_retune && cqe_retune_ok &&
411 !host->hold_retune;
412 }
413
Adrian Hunter81196972017-11-29 15:41:03 +0200414 blk_mq_start_request(req);
415
416 issued = mmc_blk_mq_issue_rq(mq, req);
417
418 switch (issued) {
419 case MMC_REQ_BUSY:
420 ret = BLK_STS_RESOURCE;
421 break;
422 case MMC_REQ_FAILED_TO_START:
423 ret = BLK_STS_IOERR;
424 break;
425 default:
426 ret = BLK_STS_OK;
427 break;
428 }
429
430 if (issued != MMC_REQ_STARTED) {
431 bool put_card = false;
432
433 spin_lock_irq(q->queue_lock);
434 mq->in_flight[issue_type] -= 1;
435 if (mmc_tot_in_flight(mq) == 0)
436 put_card = true;
437 spin_unlock_irq(q->queue_lock);
438 if (put_card)
439 mmc_put_card(card, &mq->ctx);
440 }
441
442 return ret;
443}
444
445static const struct blk_mq_ops mmc_mq_ops = {
446 .queue_rq = mmc_mq_queue_rq,
447 .init_request = mmc_mq_init_request,
448 .exit_request = mmc_mq_exit_request,
449 .complete = mmc_blk_mq_complete,
450 .timeout = mmc_mq_timed_out,
451};
452
Adrian Hunterc8b5fd02017-09-22 15:36:57 +0300453static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
454{
455 struct mmc_host *host = card->host;
456 u64 limit = BLK_BOUNCE_HIGH;
457
458 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
459 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
460
461 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
462 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
463 if (mmc_can_erase(card))
464 mmc_queue_setup_discard(mq->queue, card);
465
466 blk_queue_bounce_limit(mq->queue, limit);
467 blk_queue_max_hw_sectors(mq->queue,
468 min(host->max_blk_count, host->max_req_size / 512));
469 blk_queue_max_segments(mq->queue, host->max_segs);
470 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
471
472 /* Initialize thread_sem even if it is not used */
473 sema_init(&mq->thread_sem, 1);
Adrian Hunter81196972017-11-29 15:41:03 +0200474
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200475 INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
Adrian Hunter81196972017-11-29 15:41:03 +0200476 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
477
478 mutex_init(&mq->complete_lock);
479
480 init_waitqueue_head(&mq->wait);
481}
482
483static int mmc_mq_init_queue(struct mmc_queue *mq, int q_depth,
484 const struct blk_mq_ops *mq_ops, spinlock_t *lock)
485{
486 int ret;
487
488 memset(&mq->tag_set, 0, sizeof(mq->tag_set));
489 mq->tag_set.ops = mq_ops;
490 mq->tag_set.queue_depth = q_depth;
491 mq->tag_set.numa_node = NUMA_NO_NODE;
492 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE |
493 BLK_MQ_F_BLOCKING;
494 mq->tag_set.nr_hw_queues = 1;
495 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
496 mq->tag_set.driver_data = mq;
497
498 ret = blk_mq_alloc_tag_set(&mq->tag_set);
499 if (ret)
500 return ret;
501
502 mq->queue = blk_mq_init_queue(&mq->tag_set);
503 if (IS_ERR(mq->queue)) {
504 ret = PTR_ERR(mq->queue);
505 goto free_tag_set;
506 }
507
508 mq->queue->queue_lock = lock;
509 mq->queue->queuedata = mq;
510
511 return 0;
512
513free_tag_set:
514 blk_mq_free_tag_set(&mq->tag_set);
515
516 return ret;
517}
518
519/* Set queue depth to get a reasonable value for q->nr_requests */
520#define MMC_QUEUE_DEPTH 64
521
522static int mmc_mq_init(struct mmc_queue *mq, struct mmc_card *card,
523 spinlock_t *lock)
524{
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200525 struct mmc_host *host = card->host;
Adrian Hunter81196972017-11-29 15:41:03 +0200526 int q_depth;
527 int ret;
528
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200529 /*
530 * The queue depth for CQE must match the hardware because the request
531 * tag is used to index the hardware queue.
532 */
533 if (mq->use_cqe)
534 q_depth = min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
535 else
536 q_depth = MMC_QUEUE_DEPTH;
Adrian Hunter81196972017-11-29 15:41:03 +0200537
538 ret = mmc_mq_init_queue(mq, q_depth, &mmc_mq_ops, lock);
539 if (ret)
540 return ret;
541
542 blk_queue_rq_timeout(mq->queue, 60 * HZ);
543
544 mmc_setup_queue(mq, card);
545
546 return 0;
Adrian Hunterc8b5fd02017-09-22 15:36:57 +0300547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
550 * mmc_init_queue - initialise a queue structure.
551 * @mq: mmc queue
552 * @card: mmc card to attach this queue
553 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300554 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *
556 * Initialise a MMC card request queue.
557 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300558int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
559 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
561 struct mmc_host *host = card->host;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200562 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 mq->card = card;
Adrian Hunter81196972017-11-29 15:41:03 +0200565
Adrian Hunter1e8e55b2017-11-29 15:41:04 +0200566 mq->use_cqe = host->cqe_enabled;
567
568 if (mq->use_cqe || mmc_host_use_blk_mq(host))
Adrian Hunter81196972017-11-29 15:41:03 +0200569 return mmc_mq_init(mq, card, lock);
570
Linus Walleij304419d2017-05-18 11:29:32 +0200571 mq->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 if (!mq->queue)
573 return -ENOMEM;
Linus Walleij304419d2017-05-18 11:29:32 +0200574 mq->queue->queue_lock = lock;
575 mq->queue->request_fn = mmc_request_fn;
576 mq->queue->init_rq_fn = mmc_init_request;
577 mq->queue->exit_rq_fn = mmc_exit_request;
578 mq->queue->cmd_size = sizeof(struct mmc_queue_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 mq->queue->queuedata = mq;
Linus Walleij304419d2017-05-18 11:29:32 +0200580 mq->qcnt = 0;
581 ret = blk_init_allocated_queue(mq->queue);
582 if (ret) {
583 blk_cleanup_queue(mq->queue);
584 return ret;
585 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Pierre Ossman98ccf142007-05-12 00:26:16 +0200587 blk_queue_prep_rq(mq->queue, mmc_prep_request);
588
Adrian Hunterc8b5fd02017-09-22 15:36:57 +0300589 mmc_setup_queue(mq, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Adrian Hunterd09408a2011-06-23 13:40:28 +0300591 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
592 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400593
Christoph Hellwig87598a22006-11-13 20:23:52 +0100594 if (IS_ERR(mq->thread)) {
595 ret = PTR_ERR(mq->thread);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200596 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Christoph Hellwig87598a22006-11-13 20:23:52 +0100599 return 0;
Per Forlin97868a22011-07-09 17:12:36 -0400600
Adrian Hunter7b410d02017-03-13 14:36:36 +0200601cleanup_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return ret;
604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Adrian Hunter81196972017-11-29 15:41:03 +0200606static void mmc_mq_queue_suspend(struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Adrian Hunter81196972017-11-29 15:41:03 +0200608 blk_mq_quiesce_queue(mq->queue);
Pierre Ossman89b4e132006-11-14 22:08:16 +0100609
Adrian Hunter81196972017-11-29 15:41:03 +0200610 /*
611 * The host remains claimed while there are outstanding requests, so
612 * simply claiming and releasing here ensures there are none.
613 */
614 mmc_claim_host(mq->card->host);
615 mmc_release_host(mq->card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
Adrian Hunter81196972017-11-29 15:41:03 +0200618static void mmc_mq_queue_resume(struct mmc_queue *mq)
619{
620 blk_mq_unquiesce_queue(mq->queue);
621}
622
623static void __mmc_queue_suspend(struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
Jens Axboe165125e2007-07-24 09:28:11 +0200625 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 unsigned long flags;
627
Linus Walleij9491be52017-02-01 13:47:56 +0100628 if (!mq->suspended) {
629 mq->suspended |= true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
631 spin_lock_irqsave(q->queue_lock, flags);
632 blk_stop_queue(q);
633 spin_unlock_irqrestore(q->queue_lock, flags);
634
635 down(&mq->thread_sem);
636 }
637}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Adrian Hunter81196972017-11-29 15:41:03 +0200639static void __mmc_queue_resume(struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640{
Jens Axboe165125e2007-07-24 09:28:11 +0200641 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 unsigned long flags;
643
Linus Walleij9491be52017-02-01 13:47:56 +0100644 if (mq->suspended) {
645 mq->suspended = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 up(&mq->thread_sem);
648
649 spin_lock_irqsave(q->queue_lock, flags);
650 blk_start_queue(q);
651 spin_unlock_irqrestore(q->queue_lock, flags);
652 }
653}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100654
Adrian Hunter81196972017-11-29 15:41:03 +0200655void mmc_cleanup_queue(struct mmc_queue *mq)
656{
657 struct request_queue *q = mq->queue;
658 unsigned long flags;
659
660 if (q->mq_ops) {
661 /*
662 * The legacy code handled the possibility of being suspended,
663 * so do that here too.
664 */
665 if (blk_queue_quiesced(q))
666 blk_mq_unquiesce_queue(q);
667 goto out_cleanup;
668 }
669
670 /* Make sure the queue isn't suspended, as that will deadlock */
671 mmc_queue_resume(mq);
672
673 /* Then terminate our worker thread */
674 kthread_stop(mq->thread);
675
676 /* Empty the queue */
677 spin_lock_irqsave(q->queue_lock, flags);
678 q->queuedata = NULL;
679 blk_start_queue(q);
680 spin_unlock_irqrestore(q->queue_lock, flags);
681
682out_cleanup:
683 blk_cleanup_queue(q);
684
685 /*
686 * A request can be completed before the next request, potentially
687 * leaving a complete_work with nothing to do. Such a work item might
688 * still be queued at this point. Flush it.
689 */
690 flush_work(&mq->complete_work);
691
692 mq->card = NULL;
693}
694
695/**
696 * mmc_queue_suspend - suspend a MMC request queue
697 * @mq: MMC queue to suspend
698 *
699 * Stop the block request queue, and wait for our thread to
700 * complete any outstanding requests. This ensures that we
701 * won't suspend while a request is being processed.
702 */
703void mmc_queue_suspend(struct mmc_queue *mq)
704{
705 struct request_queue *q = mq->queue;
706
707 if (q->mq_ops)
708 mmc_mq_queue_suspend(mq);
709 else
710 __mmc_queue_suspend(mq);
711}
712
713/**
714 * mmc_queue_resume - resume a previously suspended MMC request queue
715 * @mq: MMC queue to resume
716 */
717void mmc_queue_resume(struct mmc_queue *mq)
718{
719 struct request_queue *q = mq->queue;
720
721 if (q->mq_ops)
722 mmc_mq_queue_resume(mq);
723 else
724 __mmc_queue_resume(mq);
725}
726
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200727/*
728 * Prepare the sg list(s) to be handed of to the host driver
729 */
Per Forlin97868a22011-07-09 17:12:36 -0400730unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200731{
Linus Walleij67e69d52017-05-19 15:37:27 +0200732 struct request *req = mmc_queue_req_to_req(mqrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200733
Linus Walleijde3ee992017-09-20 10:56:14 +0200734 return blk_rq_map_sg(mq->queue, req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200735}