blob: 4a2045527b62d4aba76d62bc766ff42a016e16dd [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Pierre Ossman98ccf142007-05-12 00:26:16 +020026#define MMC_QUEUE_BOUNCESZ 65536
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020029 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31static int mmc_prep_request(struct request_queue *q, struct request *req)
32{
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053033 struct mmc_queue *mq = q->queuedata;
34
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +080035 if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053036 return BLKPREP_KILL;
37
Christoph Hellwige8064022016-10-20 15:12:13 +020038 req->rq_flags |= RQF_DONTPREP;
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 Huntercdf8a6f2017-03-13 14:36:35 +020043struct mmc_queue_req *mmc_queue_req_find(struct mmc_queue *mq,
44 struct request *req)
45{
46 struct mmc_queue_req *mqrq;
47 int i = ffz(mq->qslots);
48
49 if (i >= mq->qdepth)
50 return NULL;
51
52 mqrq = &mq->mqrq[i];
53 WARN_ON(mqrq->req || mq->qcnt >= mq->qdepth ||
54 test_bit(mqrq->task_id, &mq->qslots));
55 mqrq->req = req;
56 mq->qcnt += 1;
57 __set_bit(mqrq->task_id, &mq->qslots);
58
59 return mqrq;
60}
61
62void mmc_queue_req_free(struct mmc_queue *mq,
63 struct mmc_queue_req *mqrq)
64{
65 WARN_ON(!mqrq->req || mq->qcnt < 1 ||
66 !test_bit(mqrq->task_id, &mq->qslots));
67 mqrq->req = NULL;
68 mq->qcnt -= 1;
69 __clear_bit(mqrq->task_id, &mq->qslots);
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int mmc_queue_thread(void *d)
73{
74 struct mmc_queue *mq = d;
75 struct request_queue *q = mq->queue;
Adrian Huntere0097cf2016-11-29 12:09:10 +020076 struct mmc_context_info *cntx = &mq->card->host->context_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Rafael J. Wysocki83144182007-07-17 04:03:35 -070078 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 do {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +020082 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
84 spin_lock_irq(q->queue_lock);
85 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010086 req = blk_fetch_request(q);
Adrian Huntere0097cf2016-11-29 12:09:10 +020087 mq->asleep = false;
88 cntx->is_waiting_last_req = false;
89 cntx->is_new_req = false;
90 if (!req) {
91 /*
92 * Dispatch queue is empty so set flags for
93 * mmc_request_fn() to wake us up.
94 */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +020095 if (mq->qcnt)
Adrian Huntere0097cf2016-11-29 12:09:10 +020096 cntx->is_waiting_last_req = true;
97 else
98 mq->asleep = true;
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 spin_unlock_irq(q->queue_lock);
101
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200102 if (req || mq->qcnt) {
Per Forlinee8a43a2011-07-01 18:55:33 +0200103 set_current_state(TASK_RUNNING);
Linus Walleij29eb7bd2016-09-20 11:34:38 +0200104 mmc_blk_issue_rq(mq, req);
Rabin Vincenta8c27c02015-06-14 19:26:11 +0200105 cond_resched();
Per Forlinee8a43a2011-07-01 18:55:33 +0200106 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +0100107 if (kthread_should_stop()) {
108 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +0100110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 up(&mq->thread_sem);
112 schedule();
113 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 up(&mq->thread_sem);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119}
120
121/*
122 * Generic MMC request handler. This is called for any queue on a
123 * particular host. When the host is not busy, we look for a request
124 * on any queue on this host, and attempt to issue it. This may
125 * not be the queue we were asked to process.
126 */
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530127static void mmc_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100130 struct request *req;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500131 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100132
133 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800134 while ((req = blk_fetch_request(q)) != NULL) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200135 req->rq_flags |= RQF_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900136 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800137 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100138 return;
139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500141 cntx = &mq->card->host->context_info;
Adrian Huntere0097cf2016-11-29 12:09:10 +0200142
143 if (cntx->is_waiting_last_req) {
144 cntx->is_new_req = true;
145 wake_up_interruptible(&cntx->wait);
146 }
147
148 if (mq->asleep)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100149 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Venkatraman S7513cd72011-08-23 21:16:02 +0530152static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400153{
154 struct scatterlist *sg;
155
Markus Elfring63928d42017-01-08 22:10:40 +0100156 sg = kmalloc_array(sg_len, sizeof(*sg), GFP_KERNEL);
Per Forlin97868a22011-07-09 17:12:36 -0400157 if (!sg)
158 *err = -ENOMEM;
159 else {
160 *err = 0;
161 sg_init_table(sg, sg_len);
162 }
163
164 return sg;
165}
166
Adrian Huntere056a1b2011-06-28 17:16:02 +0300167static void mmc_queue_setup_discard(struct request_queue *q,
168 struct mmc_card *card)
169{
170 unsigned max_discard;
171
172 max_discard = mmc_calc_max_discard(card);
173 if (!max_discard)
174 return;
175
176 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600177 blk_queue_max_discard_sectors(q, max_discard);
Adrian Hunter7194efb2012-04-05 14:45:47 +0300178 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300179 q->limits.discard_zeroes_data = 1;
180 q->limits.discard_granularity = card->pref_erase << 9;
181 /* granularity must not be greater than max. discard */
182 if (card->pref_erase > max_discard)
183 q->limits.discard_granularity = 0;
Maya Erez775a9362013-04-18 15:41:55 +0300184 if (mmc_can_secure_erase_trim(card))
Christoph Hellwig288dab82016-06-09 16:00:36 +0200185 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300186}
187
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200188static struct mmc_queue_req *mmc_queue_alloc_mqrqs(int qdepth)
189{
190 struct mmc_queue_req *mqrq;
191 int i;
192
193 mqrq = kcalloc(qdepth, sizeof(*mqrq), GFP_KERNEL);
194 if (mqrq) {
195 for (i = 0; i < qdepth; i++)
196 mqrq[i].task_id = i;
197 }
198
199 return mqrq;
200}
201
Adrian Hunterc8539822016-11-29 12:09:11 +0200202#ifdef CONFIG_MMC_BLOCK_BOUNCE
203static bool mmc_queue_alloc_bounce_bufs(struct mmc_queue *mq,
204 unsigned int bouncesz)
205{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200206 int i;
Adrian Hunterc8539822016-11-29 12:09:11 +0200207
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200208 for (i = 0; i < mq->qdepth; i++) {
209 mq->mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
210 if (!mq->mqrq[i].bounce_buf)
211 goto out_err;
Adrian Hunterc8539822016-11-29 12:09:11 +0200212 }
213
214 return true;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200215
216out_err:
217 while (--i >= 0) {
218 kfree(mq->mqrq[i].bounce_buf);
219 mq->mqrq[i].bounce_buf = NULL;
220 }
221 pr_warn("%s: unable to allocate bounce buffers\n",
222 mmc_card_name(mq->card));
223 return false;
Adrian Hunterc8539822016-11-29 12:09:11 +0200224}
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200225
226static int mmc_queue_alloc_bounce_sgs(struct mmc_queue *mq,
227 unsigned int bouncesz)
228{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200229 int i, ret;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200230
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200231 for (i = 0; i < mq->qdepth; i++) {
232 mq->mqrq[i].sg = mmc_alloc_sg(1, &ret);
233 if (ret)
234 return ret;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200235
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200236 mq->mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret);
237 if (ret)
238 return ret;
239 }
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200240
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200241 return 0;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200242}
Adrian Hunterc8539822016-11-29 12:09:11 +0200243#endif
244
Adrian Hunter64e29e422016-11-29 12:09:13 +0200245static int mmc_queue_alloc_sgs(struct mmc_queue *mq, int max_segs)
246{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200247 int i, ret;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200248
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200249 for (i = 0; i < mq->qdepth; i++) {
250 mq->mqrq[i].sg = mmc_alloc_sg(max_segs, &ret);
251 if (ret)
252 return ret;
253 }
Adrian Hunter64e29e422016-11-29 12:09:13 +0200254
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200255 return 0;
256}
Adrian Hunter64e29e422016-11-29 12:09:13 +0200257
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200258static void mmc_queue_req_free_bufs(struct mmc_queue_req *mqrq)
259{
260 kfree(mqrq->bounce_sg);
261 mqrq->bounce_sg = NULL;
262
263 kfree(mqrq->sg);
264 mqrq->sg = NULL;
265
266 kfree(mqrq->bounce_buf);
267 mqrq->bounce_buf = NULL;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200268}
269
Adrian Hunterc09949c2016-11-29 12:09:14 +0200270static void mmc_queue_reqs_free_bufs(struct mmc_queue *mq)
271{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200272 int i;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200273
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200274 for (i = 0; i < mq->qdepth; i++)
275 mmc_queue_req_free_bufs(&mq->mqrq[i]);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200276}
277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278/**
279 * mmc_init_queue - initialise a queue structure.
280 * @mq: mmc queue
281 * @card: mmc card to attach this queue
282 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300283 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 *
285 * Initialise a MMC card request queue.
286 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300287int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
288 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289{
290 struct mmc_host *host = card->host;
291 u64 limit = BLK_BOUNCE_HIGH;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200292 bool bounce = false;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200293 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200295 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000296 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530299 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 if (!mq->queue)
301 return -ENOMEM;
302
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200303 mq->qdepth = 2;
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200304 mq->mqrq = mmc_queue_alloc_mqrqs(mq->qdepth);
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200305 if (!mq->mqrq)
306 goto blk_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Pierre Ossman98ccf142007-05-12 00:26:16 +0200309 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200310 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600311 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300312 if (mmc_can_erase(card))
313 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200314
315#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400316 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200317 unsigned int bouncesz;
318
Pierre Ossman98ccf142007-05-12 00:26:16 +0200319 bouncesz = MMC_QUEUE_BOUNCESZ;
320
321 if (bouncesz > host->max_req_size)
322 bouncesz = host->max_req_size;
323 if (bouncesz > host->max_seg_size)
324 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200325 if (bouncesz > (host->max_blk_count * 512))
326 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200327
Adrian Hunterc8539822016-11-29 12:09:11 +0200328 if (bouncesz > 512 &&
329 mmc_queue_alloc_bounce_bufs(mq, bouncesz)) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200330 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500331 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500332 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200333 blk_queue_max_segment_size(mq->queue, bouncesz);
334
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200335 ret = mmc_queue_alloc_bounce_sgs(mq, bouncesz);
Per Forlin97868a22011-07-09 17:12:36 -0400336 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200337 goto cleanup_queue;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200338 bounce = true;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200339 }
340 }
341#endif
342
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200343 if (!bounce) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200344 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500345 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200346 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400347 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200348 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
349
Adrian Hunter64e29e422016-11-29 12:09:13 +0200350 ret = mmc_queue_alloc_sgs(mq, host->max_segs);
Per Forlin04296b72011-07-01 18:55:31 +0200351 if (ret)
352 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Thomas Gleixner632cf922010-09-14 07:12:35 -0400355 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Adrian Hunterd09408a2011-06-23 13:40:28 +0300357 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
358 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400359
Christoph Hellwig87598a22006-11-13 20:23:52 +0100360 if (IS_ERR(mq->thread)) {
361 ret = PTR_ERR(mq->thread);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200362 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
Christoph Hellwig87598a22006-11-13 20:23:52 +0100365 return 0;
Per Forlin97868a22011-07-09 17:12:36 -0400366
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200367 cleanup_queue:
Adrian Hunterc09949c2016-11-29 12:09:14 +0200368 mmc_queue_reqs_free_bufs(mq);
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200369 kfree(mq->mqrq);
370 mq->mqrq = NULL;
371blk_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return ret;
374}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376void mmc_cleanup_queue(struct mmc_queue *mq)
377{
Jens Axboe165125e2007-07-24 09:28:11 +0200378 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100379 unsigned long flags;
380
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200381 /* Make sure the queue isn't suspended, as that will deadlock */
382 mmc_queue_resume(mq);
383
Pierre Ossman89b4e132006-11-14 22:08:16 +0100384 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100385 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800387 /* Empty the queue */
388 spin_lock_irqsave(q->queue_lock, flags);
389 q->queuedata = NULL;
390 blk_start_queue(q);
391 spin_unlock_irqrestore(q->queue_lock, flags);
392
Adrian Hunterc09949c2016-11-29 12:09:14 +0200393 mmc_queue_reqs_free_bufs(mq);
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200394 kfree(mq->mqrq);
395 mq->mqrq = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 mq->card = NULL;
398}
399EXPORT_SYMBOL(mmc_cleanup_queue);
400
401/**
402 * mmc_queue_suspend - suspend a MMC request queue
403 * @mq: MMC queue to suspend
404 *
405 * Stop the block request queue, and wait for our thread to
406 * complete any outstanding requests. This ensures that we
407 * won't suspend while a request is being processed.
408 */
409void mmc_queue_suspend(struct mmc_queue *mq)
410{
Jens Axboe165125e2007-07-24 09:28:11 +0200411 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unsigned long flags;
413
Linus Walleij9491be52017-02-01 13:47:56 +0100414 if (!mq->suspended) {
415 mq->suspended |= true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 spin_lock_irqsave(q->queue_lock, flags);
418 blk_stop_queue(q);
419 spin_unlock_irqrestore(q->queue_lock, flags);
420
421 down(&mq->thread_sem);
422 }
423}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
425/**
426 * mmc_queue_resume - resume a previously suspended MMC request queue
427 * @mq: MMC queue to resume
428 */
429void mmc_queue_resume(struct mmc_queue *mq)
430{
Jens Axboe165125e2007-07-24 09:28:11 +0200431 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 unsigned long flags;
433
Linus Walleij9491be52017-02-01 13:47:56 +0100434 if (mq->suspended) {
435 mq->suspended = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 up(&mq->thread_sem);
438
439 spin_lock_irqsave(q->queue_lock, flags);
440 blk_start_queue(q);
441 spin_unlock_irqrestore(q->queue_lock, flags);
442 }
443}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100444
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200445/*
446 * Prepare the sg list(s) to be handed of to the host driver
447 */
Per Forlin97868a22011-07-09 17:12:36 -0400448unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200449{
450 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200451 size_t buflen;
452 struct scatterlist *sg;
453 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200454
Linus Walleij03d640a2016-11-25 10:35:00 +0100455 if (!mqrq->bounce_buf)
456 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200457
Linus Walleij03d640a2016-11-25 10:35:00 +0100458 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200459
Per Forlin97868a22011-07-09 17:12:36 -0400460 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200461
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200462 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400463 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200464 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200465
Per Forlin97868a22011-07-09 17:12:36 -0400466 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200467
468 return 1;
469}
470
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200471/*
472 * If writing, bounce the data to the buffer before the request
473 * is sent to the host driver
474 */
Per Forlin97868a22011-07-09 17:12:36 -0400475void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200476{
Per Forlin97868a22011-07-09 17:12:36 -0400477 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200478 return;
479
Per Forlin97868a22011-07-09 17:12:36 -0400480 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200481 return;
482
Per Forlin97868a22011-07-09 17:12:36 -0400483 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
484 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200485}
486
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200487/*
488 * If reading, bounce the data from the buffer after the request
489 * has been handled by the host driver
490 */
Per Forlin97868a22011-07-09 17:12:36 -0400491void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200492{
Per Forlin97868a22011-07-09 17:12:36 -0400493 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200494 return;
495
Per Forlin97868a22011-07-09 17:12:36 -0400496 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200497 return;
498
Per Forlin97868a22011-07-09 17:12:36 -0400499 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
500 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200501}