blob: 54bec4c6c9bd9fa0339416883a4d2ca17624e6b0 [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 Hunter81196972017-11-29 15:41:03 +020043enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
44{
45 if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
46 return MMC_ISSUE_ASYNC;
47
48 return MMC_ISSUE_SYNC;
49}
50
51static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
52 bool reserved)
53{
54 return BLK_EH_RESET_TIMER;
55}
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057static int mmc_queue_thread(void *d)
58{
59 struct mmc_queue *mq = d;
60 struct request_queue *q = mq->queue;
Adrian Huntere0097cf2016-11-29 12:09:10 +020061 struct mmc_context_info *cntx = &mq->card->host->context_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Rafael J. Wysocki83144182007-07-17 04:03:35 -070063 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 do {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +020067 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 spin_lock_irq(q->queue_lock);
70 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010071 req = blk_fetch_request(q);
Adrian Huntere0097cf2016-11-29 12:09:10 +020072 mq->asleep = false;
73 cntx->is_waiting_last_req = false;
74 cntx->is_new_req = false;
75 if (!req) {
76 /*
77 * Dispatch queue is empty so set flags for
78 * mmc_request_fn() to wake us up.
79 */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +020080 if (mq->qcnt)
Adrian Huntere0097cf2016-11-29 12:09:10 +020081 cntx->is_waiting_last_req = true;
82 else
83 mq->asleep = true;
84 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 spin_unlock_irq(q->queue_lock);
86
Adrian Huntercdf8a6f2017-03-13 14:36:35 +020087 if (req || mq->qcnt) {
Per Forlinee8a43a2011-07-01 18:55:33 +020088 set_current_state(TASK_RUNNING);
Linus Walleij29eb7bd2016-09-20 11:34:38 +020089 mmc_blk_issue_rq(mq, req);
Rabin Vincenta8c27c02015-06-14 19:26:11 +020090 cond_resched();
Per Forlinee8a43a2011-07-01 18:55:33 +020091 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010092 if (kthread_should_stop()) {
93 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 up(&mq->thread_sem);
97 schedule();
98 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 up(&mq->thread_sem);
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 return 0;
104}
105
106/*
107 * Generic MMC request handler. This is called for any queue on a
108 * particular host. When the host is not busy, we look for a request
109 * on any queue on this host, and attempt to issue it. This may
110 * not be the queue we were asked to process.
111 */
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530112static void mmc_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100115 struct request *req;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500116 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100117
118 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800119 while ((req = blk_fetch_request(q)) != NULL) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200120 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200121 __blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800122 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100123 return;
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500126 cntx = &mq->card->host->context_info;
Adrian Huntere0097cf2016-11-29 12:09:10 +0200127
128 if (cntx->is_waiting_last_req) {
129 cntx->is_new_req = true;
130 wake_up_interruptible(&cntx->wait);
131 }
132
133 if (mq->asleep)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100134 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Linus Walleij304419d2017-05-18 11:29:32 +0200137static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
Per Forlin97868a22011-07-09 17:12:36 -0400138{
139 struct scatterlist *sg;
140
Linus Walleij304419d2017-05-18 11:29:32 +0200141 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
Adrian Hunter7b410d02017-03-13 14:36:36 +0200142 if (sg)
Per Forlin97868a22011-07-09 17:12:36 -0400143 sg_init_table(sg, sg_len);
Per Forlin97868a22011-07-09 17:12:36 -0400144
145 return sg;
146}
147
Adrian Huntere056a1b2011-06-28 17:16:02 +0300148static void mmc_queue_setup_discard(struct request_queue *q,
149 struct mmc_card *card)
150{
151 unsigned max_discard;
152
153 max_discard = mmc_calc_max_discard(card);
154 if (!max_discard)
155 return;
156
157 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600158 blk_queue_max_discard_sectors(q, max_discard);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300159 q->limits.discard_granularity = card->pref_erase << 9;
160 /* granularity must not be greater than max. discard */
161 if (card->pref_erase > max_discard)
162 q->limits.discard_granularity = 0;
Maya Erez775a9362013-04-18 15:41:55 +0300163 if (mmc_can_secure_erase_trim(card))
Christoph Hellwig288dab82016-06-09 16:00:36 +0200164 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300165}
166
Linus Walleij304419d2017-05-18 11:29:32 +0200167/**
168 * mmc_init_request() - initialize the MMC-specific per-request data
169 * @q: the request queue
170 * @req: the request
171 * @gfp: memory allocation policy
172 */
Adrian Hunter81196972017-11-29 15:41:03 +0200173static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
174 gfp_t gfp)
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200175{
Linus Walleij304419d2017-05-18 11:29:32 +0200176 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
Linus Walleij304419d2017-05-18 11:29:32 +0200177 struct mmc_card *card = mq->card;
178 struct mmc_host *host = card->host;
Adrian Hunterc8539822016-11-29 12:09:11 +0200179
Linus Walleijde3ee992017-09-20 10:56:14 +0200180 mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
181 if (!mq_rq->sg)
182 return -ENOMEM;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200183
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200184 return 0;
185}
Adrian Hunter64e29e422016-11-29 12:09:13 +0200186
Adrian Hunter81196972017-11-29 15:41:03 +0200187static int mmc_init_request(struct request_queue *q, struct request *req,
188 gfp_t gfp)
189{
190 return __mmc_init_request(q->queuedata, req, gfp);
191}
192
Linus Walleij304419d2017-05-18 11:29:32 +0200193static void mmc_exit_request(struct request_queue *q, struct request *req)
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200194{
Linus Walleij304419d2017-05-18 11:29:32 +0200195 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
Adrian Hunter64e29e422016-11-29 12:09:13 +0200196
Linus Walleij304419d2017-05-18 11:29:32 +0200197 kfree(mq_rq->sg);
198 mq_rq->sg = NULL;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200199}
200
Adrian Hunter81196972017-11-29 15:41:03 +0200201static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
202 unsigned int hctx_idx, unsigned int numa_node)
203{
204 return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
205}
206
207static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
208 unsigned int hctx_idx)
209{
210 struct mmc_queue *mq = set->driver_data;
211
212 mmc_exit_request(mq->queue, req);
213}
214
215/*
216 * We use BLK_MQ_F_BLOCKING and have only 1 hardware queue, which means requests
217 * will not be dispatched in parallel.
218 */
219static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
220 const struct blk_mq_queue_data *bd)
221{
222 struct request *req = bd->rq;
223 struct request_queue *q = req->q;
224 struct mmc_queue *mq = q->queuedata;
225 struct mmc_card *card = mq->card;
226 enum mmc_issue_type issue_type;
227 enum mmc_issued issued;
228 bool get_card;
229 int ret;
230
231 if (mmc_card_removed(mq->card)) {
232 req->rq_flags |= RQF_QUIET;
233 return BLK_STS_IOERR;
234 }
235
236 issue_type = mmc_issue_type(mq, req);
237
238 spin_lock_irq(q->queue_lock);
239
240 switch (issue_type) {
241 case MMC_ISSUE_ASYNC:
242 break;
243 default:
244 /*
245 * Timeouts are handled by mmc core, and we don't have a host
246 * API to abort requests, so we can't handle the timeout anyway.
247 * However, when the timeout happens, blk_mq_complete_request()
248 * no longer works (to stop the request disappearing under us).
249 * To avoid racing with that, set a large timeout.
250 */
251 req->timeout = 600 * HZ;
252 break;
253 }
254
255 mq->in_flight[issue_type] += 1;
256 get_card = (mmc_tot_in_flight(mq) == 1);
257
258 spin_unlock_irq(q->queue_lock);
259
260 if (!(req->rq_flags & RQF_DONTPREP)) {
261 req_to_mmc_queue_req(req)->retries = 0;
262 req->rq_flags |= RQF_DONTPREP;
263 }
264
265 if (get_card)
266 mmc_get_card(card, &mq->ctx);
267
268 blk_mq_start_request(req);
269
270 issued = mmc_blk_mq_issue_rq(mq, req);
271
272 switch (issued) {
273 case MMC_REQ_BUSY:
274 ret = BLK_STS_RESOURCE;
275 break;
276 case MMC_REQ_FAILED_TO_START:
277 ret = BLK_STS_IOERR;
278 break;
279 default:
280 ret = BLK_STS_OK;
281 break;
282 }
283
284 if (issued != MMC_REQ_STARTED) {
285 bool put_card = false;
286
287 spin_lock_irq(q->queue_lock);
288 mq->in_flight[issue_type] -= 1;
289 if (mmc_tot_in_flight(mq) == 0)
290 put_card = true;
291 spin_unlock_irq(q->queue_lock);
292 if (put_card)
293 mmc_put_card(card, &mq->ctx);
294 }
295
296 return ret;
297}
298
299static const struct blk_mq_ops mmc_mq_ops = {
300 .queue_rq = mmc_mq_queue_rq,
301 .init_request = mmc_mq_init_request,
302 .exit_request = mmc_mq_exit_request,
303 .complete = mmc_blk_mq_complete,
304 .timeout = mmc_mq_timed_out,
305};
306
Adrian Hunterc8b5fd02017-09-22 15:36:57 +0300307static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
308{
309 struct mmc_host *host = card->host;
310 u64 limit = BLK_BOUNCE_HIGH;
311
312 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
313 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
314
315 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
316 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
317 if (mmc_can_erase(card))
318 mmc_queue_setup_discard(mq->queue, card);
319
320 blk_queue_bounce_limit(mq->queue, limit);
321 blk_queue_max_hw_sectors(mq->queue,
322 min(host->max_blk_count, host->max_req_size / 512));
323 blk_queue_max_segments(mq->queue, host->max_segs);
324 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
325
326 /* Initialize thread_sem even if it is not used */
327 sema_init(&mq->thread_sem, 1);
Adrian Hunter81196972017-11-29 15:41:03 +0200328
329 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
330
331 mutex_init(&mq->complete_lock);
332
333 init_waitqueue_head(&mq->wait);
334}
335
336static int mmc_mq_init_queue(struct mmc_queue *mq, int q_depth,
337 const struct blk_mq_ops *mq_ops, spinlock_t *lock)
338{
339 int ret;
340
341 memset(&mq->tag_set, 0, sizeof(mq->tag_set));
342 mq->tag_set.ops = mq_ops;
343 mq->tag_set.queue_depth = q_depth;
344 mq->tag_set.numa_node = NUMA_NO_NODE;
345 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE |
346 BLK_MQ_F_BLOCKING;
347 mq->tag_set.nr_hw_queues = 1;
348 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
349 mq->tag_set.driver_data = mq;
350
351 ret = blk_mq_alloc_tag_set(&mq->tag_set);
352 if (ret)
353 return ret;
354
355 mq->queue = blk_mq_init_queue(&mq->tag_set);
356 if (IS_ERR(mq->queue)) {
357 ret = PTR_ERR(mq->queue);
358 goto free_tag_set;
359 }
360
361 mq->queue->queue_lock = lock;
362 mq->queue->queuedata = mq;
363
364 return 0;
365
366free_tag_set:
367 blk_mq_free_tag_set(&mq->tag_set);
368
369 return ret;
370}
371
372/* Set queue depth to get a reasonable value for q->nr_requests */
373#define MMC_QUEUE_DEPTH 64
374
375static int mmc_mq_init(struct mmc_queue *mq, struct mmc_card *card,
376 spinlock_t *lock)
377{
378 int q_depth;
379 int ret;
380
381 q_depth = MMC_QUEUE_DEPTH;
382
383 ret = mmc_mq_init_queue(mq, q_depth, &mmc_mq_ops, lock);
384 if (ret)
385 return ret;
386
387 blk_queue_rq_timeout(mq->queue, 60 * HZ);
388
389 mmc_setup_queue(mq, card);
390
391 return 0;
Adrian Hunterc8b5fd02017-09-22 15:36:57 +0300392}
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394/**
395 * mmc_init_queue - initialise a queue structure.
396 * @mq: mmc queue
397 * @card: mmc card to attach this queue
398 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300399 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 *
401 * Initialise a MMC card request queue.
402 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300403int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
404 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct mmc_host *host = card->host;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200407 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 mq->card = card;
Adrian Hunter81196972017-11-29 15:41:03 +0200410
411 if (mmc_host_use_blk_mq(host))
412 return mmc_mq_init(mq, card, lock);
413
Linus Walleij304419d2017-05-18 11:29:32 +0200414 mq->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 if (!mq->queue)
416 return -ENOMEM;
Linus Walleij304419d2017-05-18 11:29:32 +0200417 mq->queue->queue_lock = lock;
418 mq->queue->request_fn = mmc_request_fn;
419 mq->queue->init_rq_fn = mmc_init_request;
420 mq->queue->exit_rq_fn = mmc_exit_request;
421 mq->queue->cmd_size = sizeof(struct mmc_queue_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 mq->queue->queuedata = mq;
Linus Walleij304419d2017-05-18 11:29:32 +0200423 mq->qcnt = 0;
424 ret = blk_init_allocated_queue(mq->queue);
425 if (ret) {
426 blk_cleanup_queue(mq->queue);
427 return ret;
428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Pierre Ossman98ccf142007-05-12 00:26:16 +0200430 blk_queue_prep_rq(mq->queue, mmc_prep_request);
431
Adrian Hunterc8b5fd02017-09-22 15:36:57 +0300432 mmc_setup_queue(mq, card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Adrian Hunterd09408a2011-06-23 13:40:28 +0300434 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
435 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400436
Christoph Hellwig87598a22006-11-13 20:23:52 +0100437 if (IS_ERR(mq->thread)) {
438 ret = PTR_ERR(mq->thread);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200439 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 }
441
Christoph Hellwig87598a22006-11-13 20:23:52 +0100442 return 0;
Per Forlin97868a22011-07-09 17:12:36 -0400443
Adrian Hunter7b410d02017-03-13 14:36:36 +0200444cleanup_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return ret;
447}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Adrian Hunter81196972017-11-29 15:41:03 +0200449static void mmc_mq_queue_suspend(struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Adrian Hunter81196972017-11-29 15:41:03 +0200451 blk_mq_quiesce_queue(mq->queue);
Pierre Ossman89b4e132006-11-14 22:08:16 +0100452
Adrian Hunter81196972017-11-29 15:41:03 +0200453 /*
454 * The host remains claimed while there are outstanding requests, so
455 * simply claiming and releasing here ensures there are none.
456 */
457 mmc_claim_host(mq->card->host);
458 mmc_release_host(mq->card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Adrian Hunter81196972017-11-29 15:41:03 +0200461static void mmc_mq_queue_resume(struct mmc_queue *mq)
462{
463 blk_mq_unquiesce_queue(mq->queue);
464}
465
466static void __mmc_queue_suspend(struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Jens Axboe165125e2007-07-24 09:28:11 +0200468 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 unsigned long flags;
470
Linus Walleij9491be52017-02-01 13:47:56 +0100471 if (!mq->suspended) {
472 mq->suspended |= true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 spin_lock_irqsave(q->queue_lock, flags);
475 blk_stop_queue(q);
476 spin_unlock_irqrestore(q->queue_lock, flags);
477
478 down(&mq->thread_sem);
479 }
480}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Adrian Hunter81196972017-11-29 15:41:03 +0200482static void __mmc_queue_resume(struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Jens Axboe165125e2007-07-24 09:28:11 +0200484 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 unsigned long flags;
486
Linus Walleij9491be52017-02-01 13:47:56 +0100487 if (mq->suspended) {
488 mq->suspended = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 up(&mq->thread_sem);
491
492 spin_lock_irqsave(q->queue_lock, flags);
493 blk_start_queue(q);
494 spin_unlock_irqrestore(q->queue_lock, flags);
495 }
496}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100497
Adrian Hunter81196972017-11-29 15:41:03 +0200498void mmc_cleanup_queue(struct mmc_queue *mq)
499{
500 struct request_queue *q = mq->queue;
501 unsigned long flags;
502
503 if (q->mq_ops) {
504 /*
505 * The legacy code handled the possibility of being suspended,
506 * so do that here too.
507 */
508 if (blk_queue_quiesced(q))
509 blk_mq_unquiesce_queue(q);
510 goto out_cleanup;
511 }
512
513 /* Make sure the queue isn't suspended, as that will deadlock */
514 mmc_queue_resume(mq);
515
516 /* Then terminate our worker thread */
517 kthread_stop(mq->thread);
518
519 /* Empty the queue */
520 spin_lock_irqsave(q->queue_lock, flags);
521 q->queuedata = NULL;
522 blk_start_queue(q);
523 spin_unlock_irqrestore(q->queue_lock, flags);
524
525out_cleanup:
526 blk_cleanup_queue(q);
527
528 /*
529 * A request can be completed before the next request, potentially
530 * leaving a complete_work with nothing to do. Such a work item might
531 * still be queued at this point. Flush it.
532 */
533 flush_work(&mq->complete_work);
534
535 mq->card = NULL;
536}
537
538/**
539 * mmc_queue_suspend - suspend a MMC request queue
540 * @mq: MMC queue to suspend
541 *
542 * Stop the block request queue, and wait for our thread to
543 * complete any outstanding requests. This ensures that we
544 * won't suspend while a request is being processed.
545 */
546void mmc_queue_suspend(struct mmc_queue *mq)
547{
548 struct request_queue *q = mq->queue;
549
550 if (q->mq_ops)
551 mmc_mq_queue_suspend(mq);
552 else
553 __mmc_queue_suspend(mq);
554}
555
556/**
557 * mmc_queue_resume - resume a previously suspended MMC request queue
558 * @mq: MMC queue to resume
559 */
560void mmc_queue_resume(struct mmc_queue *mq)
561{
562 struct request_queue *q = mq->queue;
563
564 if (q->mq_ops)
565 mmc_mq_queue_resume(mq);
566 else
567 __mmc_queue_resume(mq);
568}
569
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200570/*
571 * Prepare the sg list(s) to be handed of to the host driver
572 */
Per Forlin97868a22011-07-09 17:12:36 -0400573unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200574{
Linus Walleij67e69d52017-05-19 15:37:27 +0200575 struct request *req = mmc_queue_req_to_req(mqrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200576
Linus Walleijde3ee992017-09-20 10:56:14 +0200577 return blk_rq_map_sg(mq->queue, req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200578}