blob: 70ba7f94c7067c14bb1945a94ba136d71a16a052 [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
Adrian Hunter7b410d02017-03-13 14:36:36 +0200152static struct scatterlist *mmc_alloc_sg(int sg_len)
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);
Adrian Hunter7b410d02017-03-13 14:36:36 +0200157 if (sg)
Per Forlin97868a22011-07-09 17:12:36 -0400158 sg_init_table(sg, sg_len);
Per Forlin97868a22011-07-09 17:12:36 -0400159
160 return sg;
161}
162
Adrian Huntere056a1b2011-06-28 17:16:02 +0300163static void mmc_queue_setup_discard(struct request_queue *q,
164 struct mmc_card *card)
165{
166 unsigned max_discard;
167
168 max_discard = mmc_calc_max_discard(card);
169 if (!max_discard)
170 return;
171
172 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600173 blk_queue_max_discard_sectors(q, max_discard);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300174 q->limits.discard_granularity = card->pref_erase << 9;
175 /* granularity must not be greater than max. discard */
176 if (card->pref_erase > max_discard)
177 q->limits.discard_granularity = 0;
Maya Erez775a9362013-04-18 15:41:55 +0300178 if (mmc_can_secure_erase_trim(card))
Christoph Hellwig288dab82016-06-09 16:00:36 +0200179 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300180}
181
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200182static void mmc_queue_req_free_bufs(struct mmc_queue_req *mqrq)
183{
184 kfree(mqrq->bounce_sg);
185 mqrq->bounce_sg = NULL;
186
187 kfree(mqrq->sg);
188 mqrq->sg = NULL;
189
190 kfree(mqrq->bounce_buf);
191 mqrq->bounce_buf = NULL;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200192}
193
Adrian Hunter7b410d02017-03-13 14:36:36 +0200194static void mmc_queue_reqs_free_bufs(struct mmc_queue_req *mqrq, int qdepth)
Adrian Hunterc09949c2016-11-29 12:09:14 +0200195{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200196 int i;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200197
Adrian Hunter7b410d02017-03-13 14:36:36 +0200198 for (i = 0; i < qdepth; i++)
199 mmc_queue_req_free_bufs(&mqrq[i]);
200}
201
202static void mmc_queue_free_mqrqs(struct mmc_queue_req *mqrq, int qdepth)
203{
204 mmc_queue_reqs_free_bufs(mqrq, qdepth);
205 kfree(mqrq);
206}
207
Adrian Huntercdf8a6f2017-03-13 14:36:35 +0200208static struct mmc_queue_req *mmc_queue_alloc_mqrqs(int qdepth)
209{
210 struct mmc_queue_req *mqrq;
211 int i;
212
213 mqrq = kcalloc(qdepth, sizeof(*mqrq), GFP_KERNEL);
214 if (mqrq) {
215 for (i = 0; i < qdepth; i++)
216 mqrq[i].task_id = i;
217 }
218
219 return mqrq;
220}
221
Adrian Hunter7b410d02017-03-13 14:36:36 +0200222static int mmc_queue_alloc_bounce_bufs(struct mmc_queue_req *mqrq, int qdepth,
223 unsigned int bouncesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 int i;
226
Adrian Hunter7b410d02017-03-13 14:36:36 +0200227 for (i = 0; i < qdepth; i++) {
228 mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
229 if (!mqrq[i].bounce_buf)
230 return -ENOMEM;
231
232 mqrq[i].sg = mmc_alloc_sg(1);
233 if (!mqrq[i].sg)
234 return -ENOMEM;
235
236 mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512);
237 if (!mqrq[i].bounce_sg)
238 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Adrian Hunter7b410d02017-03-13 14:36:36 +0200241 return 0;
242}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Adrian Hunter7b410d02017-03-13 14:36:36 +0200244static bool mmc_queue_alloc_bounce(struct mmc_queue_req *mqrq, int qdepth,
245 unsigned int bouncesz)
246{
247 int ret;
248
249 ret = mmc_queue_alloc_bounce_bufs(mqrq, qdepth, bouncesz);
250 if (ret)
251 mmc_queue_reqs_free_bufs(mqrq, qdepth);
252
253 return !ret;
254}
255
256static unsigned int mmc_queue_calc_bouncesz(struct mmc_host *host)
257{
258 unsigned int bouncesz = MMC_QUEUE_BOUNCESZ;
259
Linus Walleijc3dccb72017-05-18 11:29:31 +0200260 if (host->max_segs != 1 || (host->caps & MMC_CAP_NO_BOUNCE_BUFF))
Adrian Hunter7b410d02017-03-13 14:36:36 +0200261 return 0;
262
263 if (bouncesz > host->max_req_size)
264 bouncesz = host->max_req_size;
265 if (bouncesz > host->max_seg_size)
266 bouncesz = host->max_seg_size;
267 if (bouncesz > host->max_blk_count * 512)
268 bouncesz = host->max_blk_count * 512;
269
270 if (bouncesz <= 512)
271 return 0;
272
273 return bouncesz;
274}
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200275
Adrian Hunter7b410d02017-03-13 14:36:36 +0200276static int mmc_queue_alloc_sgs(struct mmc_queue_req *mqrq, int qdepth,
277 int max_segs)
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200278{
Adrian Hunter7b410d02017-03-13 14:36:36 +0200279 int i;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200280
Adrian Hunter7b410d02017-03-13 14:36:36 +0200281 for (i = 0; i < qdepth; i++) {
282 mqrq[i].sg = mmc_alloc_sg(max_segs);
283 if (!mqrq[i].sg)
284 return -ENOMEM;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200285 }
286
287 return 0;
288}
289
Adrian Hunter7b410d02017-03-13 14:36:36 +0200290void mmc_queue_free_shared_queue(struct mmc_card *card)
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200291{
Adrian Hunter7b410d02017-03-13 14:36:36 +0200292 if (card->mqrq) {
293 mmc_queue_free_mqrqs(card->mqrq, card->qdepth);
294 card->mqrq = NULL;
295 }
Adrian Hunterc8539822016-11-29 12:09:11 +0200296}
Adrian Hunterc09949c2016-11-29 12:09:14 +0200297
Adrian Hunter7b410d02017-03-13 14:36:36 +0200298static int __mmc_queue_alloc_shared_queue(struct mmc_card *card, int qdepth)
Adrian Hunterc09949c2016-11-29 12:09:14 +0200299{
Adrian Hunter7b410d02017-03-13 14:36:36 +0200300 struct mmc_host *host = card->host;
301 struct mmc_queue_req *mqrq;
302 unsigned int bouncesz;
303 int ret = 0;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200304
Adrian Hunter7b410d02017-03-13 14:36:36 +0200305 if (card->mqrq)
306 return -EINVAL;
307
308 mqrq = mmc_queue_alloc_mqrqs(qdepth);
309 if (!mqrq)
310 return -ENOMEM;
311
312 card->mqrq = mqrq;
313 card->qdepth = qdepth;
314
315 bouncesz = mmc_queue_calc_bouncesz(host);
316
317 if (bouncesz && !mmc_queue_alloc_bounce(mqrq, qdepth, bouncesz)) {
318 bouncesz = 0;
319 pr_warn("%s: unable to allocate bounce buffers\n",
320 mmc_card_name(card));
321 }
322
323 card->bouncesz = bouncesz;
324
325 if (!bouncesz) {
326 ret = mmc_queue_alloc_sgs(mqrq, qdepth, host->max_segs);
327 if (ret)
328 goto out_err;
329 }
330
331 return ret;
332
333out_err:
334 mmc_queue_free_shared_queue(card);
335 return ret;
336}
337
338int mmc_queue_alloc_shared_queue(struct mmc_card *card)
339{
340 return __mmc_queue_alloc_shared_queue(card, 2);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200341}
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343/**
344 * mmc_init_queue - initialise a queue structure.
345 * @mq: mmc queue
346 * @card: mmc card to attach this queue
347 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300348 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 *
350 * Initialise a MMC card request queue.
351 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300352int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
353 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 struct mmc_host *host = card->host;
356 u64 limit = BLK_BOUNCE_HIGH;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200357 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200359 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000360 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530363 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (!mq->queue)
365 return -ENOMEM;
366
Adrian Hunter7b410d02017-03-13 14:36:36 +0200367 mq->mqrq = card->mqrq;
368 mq->qdepth = card->qdepth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Pierre Ossman98ccf142007-05-12 00:26:16 +0200371 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200372 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600373 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300374 if (mmc_can_erase(card))
375 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200376
Adrian Hunter7b410d02017-03-13 14:36:36 +0200377 if (card->bouncesz) {
378 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
379 blk_queue_max_hw_sectors(mq->queue, card->bouncesz / 512);
380 blk_queue_max_segments(mq->queue, card->bouncesz / 512);
381 blk_queue_max_segment_size(mq->queue, card->bouncesz);
382 } else {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200383 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500384 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200385 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400386 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200387 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 }
389
Thomas Gleixner632cf922010-09-14 07:12:35 -0400390 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Adrian Hunterd09408a2011-06-23 13:40:28 +0300392 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
393 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400394
Christoph Hellwig87598a22006-11-13 20:23:52 +0100395 if (IS_ERR(mq->thread)) {
396 ret = PTR_ERR(mq->thread);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200397 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399
Christoph Hellwig87598a22006-11-13 20:23:52 +0100400 return 0;
Per Forlin97868a22011-07-09 17:12:36 -0400401
Adrian Hunter7b410d02017-03-13 14:36:36 +0200402cleanup_queue:
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200403 mq->mqrq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return ret;
406}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408void mmc_cleanup_queue(struct mmc_queue *mq)
409{
Jens Axboe165125e2007-07-24 09:28:11 +0200410 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100411 unsigned long flags;
412
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200413 /* Make sure the queue isn't suspended, as that will deadlock */
414 mmc_queue_resume(mq);
415
Pierre Ossman89b4e132006-11-14 22:08:16 +0100416 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100417 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800419 /* Empty the queue */
420 spin_lock_irqsave(q->queue_lock, flags);
421 q->queuedata = NULL;
422 blk_start_queue(q);
423 spin_unlock_irqrestore(q->queue_lock, flags);
424
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200425 mq->mqrq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 mq->card = NULL;
427}
428EXPORT_SYMBOL(mmc_cleanup_queue);
429
430/**
431 * mmc_queue_suspend - suspend a MMC request queue
432 * @mq: MMC queue to suspend
433 *
434 * Stop the block request queue, and wait for our thread to
435 * complete any outstanding requests. This ensures that we
436 * won't suspend while a request is being processed.
437 */
438void mmc_queue_suspend(struct mmc_queue *mq)
439{
Jens Axboe165125e2007-07-24 09:28:11 +0200440 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 unsigned long flags;
442
Linus Walleij9491be52017-02-01 13:47:56 +0100443 if (!mq->suspended) {
444 mq->suspended |= true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 spin_lock_irqsave(q->queue_lock, flags);
447 blk_stop_queue(q);
448 spin_unlock_irqrestore(q->queue_lock, flags);
449
450 down(&mq->thread_sem);
451 }
452}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454/**
455 * mmc_queue_resume - resume a previously suspended MMC request queue
456 * @mq: MMC queue to resume
457 */
458void mmc_queue_resume(struct mmc_queue *mq)
459{
Jens Axboe165125e2007-07-24 09:28:11 +0200460 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 unsigned long flags;
462
Linus Walleij9491be52017-02-01 13:47:56 +0100463 if (mq->suspended) {
464 mq->suspended = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 up(&mq->thread_sem);
467
468 spin_lock_irqsave(q->queue_lock, flags);
469 blk_start_queue(q);
470 spin_unlock_irqrestore(q->queue_lock, flags);
471 }
472}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100473
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200474/*
475 * Prepare the sg list(s) to be handed of to the host driver
476 */
Per Forlin97868a22011-07-09 17:12:36 -0400477unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200478{
479 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200480 size_t buflen;
481 struct scatterlist *sg;
482 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200483
Linus Walleij03d640a2016-11-25 10:35:00 +0100484 if (!mqrq->bounce_buf)
485 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486
Linus Walleij03d640a2016-11-25 10:35:00 +0100487 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200488
Per Forlin97868a22011-07-09 17:12:36 -0400489 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200490
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200491 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400492 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200493 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200494
Per Forlin97868a22011-07-09 17:12:36 -0400495 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200496
497 return 1;
498}
499
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200500/*
501 * If writing, bounce the data to the buffer before the request
502 * is sent to the host driver
503 */
Per Forlin97868a22011-07-09 17:12:36 -0400504void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200505{
Per Forlin97868a22011-07-09 17:12:36 -0400506 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200507 return;
508
Per Forlin97868a22011-07-09 17:12:36 -0400509 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200510 return;
511
Per Forlin97868a22011-07-09 17:12:36 -0400512 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
513 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200514}
515
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200516/*
517 * If reading, bounce the data from the buffer after the request
518 * has been handled by the host driver
519 */
Per Forlin97868a22011-07-09 17:12:36 -0400520void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200521{
Per Forlin97868a22011-07-09 17:12:36 -0400522 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200523 return;
524
Per Forlin97868a22011-07-09 17:12:36 -0400525 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200526 return;
527
Per Forlin97868a22011-07-09 17:12:36 -0400528 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
529 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200530}