blob: 7f20298d892b01b0a731f50a914dac89483bb757 [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;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200136 __blk_end_request_all(req, BLK_STS_IOERR);
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222#ifdef CONFIG_MMC_BLOCK_BOUNCE
Adrian Hunter7b410d02017-03-13 14:36:36 +0200223static int mmc_queue_alloc_bounce_bufs(struct mmc_queue_req *mqrq, int qdepth,
224 unsigned int bouncesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225{
226 int i;
227
Adrian Hunter7b410d02017-03-13 14:36:36 +0200228 for (i = 0; i < qdepth; i++) {
229 mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
230 if (!mqrq[i].bounce_buf)
231 return -ENOMEM;
232
233 mqrq[i].sg = mmc_alloc_sg(1);
234 if (!mqrq[i].sg)
235 return -ENOMEM;
236
237 mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512);
238 if (!mqrq[i].bounce_sg)
239 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 }
241
Adrian Hunter7b410d02017-03-13 14:36:36 +0200242 return 0;
243}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Adrian Hunter7b410d02017-03-13 14:36:36 +0200245static bool mmc_queue_alloc_bounce(struct mmc_queue_req *mqrq, int qdepth,
246 unsigned int bouncesz)
247{
248 int ret;
249
250 ret = mmc_queue_alloc_bounce_bufs(mqrq, qdepth, bouncesz);
251 if (ret)
252 mmc_queue_reqs_free_bufs(mqrq, qdepth);
253
254 return !ret;
255}
256
257static unsigned int mmc_queue_calc_bouncesz(struct mmc_host *host)
258{
259 unsigned int bouncesz = MMC_QUEUE_BOUNCESZ;
260
261 if (host->max_segs != 1)
262 return 0;
263
264 if (bouncesz > host->max_req_size)
265 bouncesz = host->max_req_size;
266 if (bouncesz > host->max_seg_size)
267 bouncesz = host->max_seg_size;
268 if (bouncesz > host->max_blk_count * 512)
269 bouncesz = host->max_blk_count * 512;
270
271 if (bouncesz <= 512)
272 return 0;
273
274 return bouncesz;
275}
276#else
277static inline bool mmc_queue_alloc_bounce(struct mmc_queue_req *mqrq,
278 int qdepth, unsigned int bouncesz)
279{
Adrian Hunterc8539822016-11-29 12:09:11 +0200280 return false;
281}
282
Adrian Hunter7b410d02017-03-13 14:36:36 +0200283static unsigned int mmc_queue_calc_bouncesz(struct mmc_host *host)
Adrian Hunterc8539822016-11-29 12:09:11 +0200284{
Adrian Hunterc8539822016-11-29 12:09:11 +0200285 return 0;
286}
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200287#endif
288
Adrian Hunter7b410d02017-03-13 14:36:36 +0200289static int mmc_queue_alloc_sgs(struct mmc_queue_req *mqrq, int qdepth,
290 int max_segs)
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200291{
Adrian Hunter7b410d02017-03-13 14:36:36 +0200292 int i;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200293
Adrian Hunter7b410d02017-03-13 14:36:36 +0200294 for (i = 0; i < qdepth; i++) {
295 mqrq[i].sg = mmc_alloc_sg(max_segs);
296 if (!mqrq[i].sg)
297 return -ENOMEM;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200298 }
299
300 return 0;
301}
302
Adrian Hunter7b410d02017-03-13 14:36:36 +0200303void mmc_queue_free_shared_queue(struct mmc_card *card)
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200304{
Adrian Hunter7b410d02017-03-13 14:36:36 +0200305 if (card->mqrq) {
306 mmc_queue_free_mqrqs(card->mqrq, card->qdepth);
307 card->mqrq = NULL;
308 }
Adrian Hunterc8539822016-11-29 12:09:11 +0200309}
Adrian Hunterc09949c2016-11-29 12:09:14 +0200310
Adrian Hunter7b410d02017-03-13 14:36:36 +0200311static int __mmc_queue_alloc_shared_queue(struct mmc_card *card, int qdepth)
Adrian Hunterc09949c2016-11-29 12:09:14 +0200312{
Adrian Hunter7b410d02017-03-13 14:36:36 +0200313 struct mmc_host *host = card->host;
314 struct mmc_queue_req *mqrq;
315 unsigned int bouncesz;
316 int ret = 0;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200317
Adrian Hunter7b410d02017-03-13 14:36:36 +0200318 if (card->mqrq)
319 return -EINVAL;
320
321 mqrq = mmc_queue_alloc_mqrqs(qdepth);
322 if (!mqrq)
323 return -ENOMEM;
324
325 card->mqrq = mqrq;
326 card->qdepth = qdepth;
327
328 bouncesz = mmc_queue_calc_bouncesz(host);
329
330 if (bouncesz && !mmc_queue_alloc_bounce(mqrq, qdepth, bouncesz)) {
331 bouncesz = 0;
332 pr_warn("%s: unable to allocate bounce buffers\n",
333 mmc_card_name(card));
334 }
335
336 card->bouncesz = bouncesz;
337
338 if (!bouncesz) {
339 ret = mmc_queue_alloc_sgs(mqrq, qdepth, host->max_segs);
340 if (ret)
341 goto out_err;
342 }
343
344 return ret;
345
346out_err:
347 mmc_queue_free_shared_queue(card);
348 return ret;
349}
350
351int mmc_queue_alloc_shared_queue(struct mmc_card *card)
352{
353 return __mmc_queue_alloc_shared_queue(card, 2);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200354}
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356/**
357 * mmc_init_queue - initialise a queue structure.
358 * @mq: mmc queue
359 * @card: mmc card to attach this queue
360 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300361 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 *
363 * Initialise a MMC card request queue.
364 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300365int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
366 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367{
368 struct mmc_host *host = card->host;
369 u64 limit = BLK_BOUNCE_HIGH;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200370 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200372 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000373 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530376 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 if (!mq->queue)
378 return -ENOMEM;
379
Adrian Hunter7b410d02017-03-13 14:36:36 +0200380 mq->mqrq = card->mqrq;
381 mq->qdepth = card->qdepth;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Pierre Ossman98ccf142007-05-12 00:26:16 +0200384 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200385 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600386 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300387 if (mmc_can_erase(card))
388 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200389
Adrian Hunter7b410d02017-03-13 14:36:36 +0200390 if (card->bouncesz) {
391 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
392 blk_queue_max_hw_sectors(mq->queue, card->bouncesz / 512);
393 blk_queue_max_segments(mq->queue, card->bouncesz / 512);
394 blk_queue_max_segment_size(mq->queue, card->bouncesz);
395 } else {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200396 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500397 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200398 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400399 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200400 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
Thomas Gleixner632cf922010-09-14 07:12:35 -0400403 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Adrian Hunterd09408a2011-06-23 13:40:28 +0300405 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
406 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400407
Christoph Hellwig87598a22006-11-13 20:23:52 +0100408 if (IS_ERR(mq->thread)) {
409 ret = PTR_ERR(mq->thread);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200410 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 }
412
Christoph Hellwig87598a22006-11-13 20:23:52 +0100413 return 0;
Per Forlin97868a22011-07-09 17:12:36 -0400414
Adrian Hunter7b410d02017-03-13 14:36:36 +0200415cleanup_queue:
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200416 mq->mqrq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return ret;
419}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421void mmc_cleanup_queue(struct mmc_queue *mq)
422{
Jens Axboe165125e2007-07-24 09:28:11 +0200423 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100424 unsigned long flags;
425
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200426 /* Make sure the queue isn't suspended, as that will deadlock */
427 mmc_queue_resume(mq);
428
Pierre Ossman89b4e132006-11-14 22:08:16 +0100429 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100430 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800432 /* Empty the queue */
433 spin_lock_irqsave(q->queue_lock, flags);
434 q->queuedata = NULL;
435 blk_start_queue(q);
436 spin_unlock_irqrestore(q->queue_lock, flags);
437
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200438 mq->mqrq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 mq->card = NULL;
440}
441EXPORT_SYMBOL(mmc_cleanup_queue);
442
443/**
444 * mmc_queue_suspend - suspend a MMC request queue
445 * @mq: MMC queue to suspend
446 *
447 * Stop the block request queue, and wait for our thread to
448 * complete any outstanding requests. This ensures that we
449 * won't suspend while a request is being processed.
450 */
451void mmc_queue_suspend(struct mmc_queue *mq)
452{
Jens Axboe165125e2007-07-24 09:28:11 +0200453 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 unsigned long flags;
455
Linus Walleij9491be52017-02-01 13:47:56 +0100456 if (!mq->suspended) {
457 mq->suspended |= true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 spin_lock_irqsave(q->queue_lock, flags);
460 blk_stop_queue(q);
461 spin_unlock_irqrestore(q->queue_lock, flags);
462
463 down(&mq->thread_sem);
464 }
465}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467/**
468 * mmc_queue_resume - resume a previously suspended MMC request queue
469 * @mq: MMC queue to resume
470 */
471void mmc_queue_resume(struct mmc_queue *mq)
472{
Jens Axboe165125e2007-07-24 09:28:11 +0200473 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 unsigned long flags;
475
Linus Walleij9491be52017-02-01 13:47:56 +0100476 if (mq->suspended) {
477 mq->suspended = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 up(&mq->thread_sem);
480
481 spin_lock_irqsave(q->queue_lock, flags);
482 blk_start_queue(q);
483 spin_unlock_irqrestore(q->queue_lock, flags);
484 }
485}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100486
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200487/*
488 * Prepare the sg list(s) to be handed of to the host driver
489 */
Per Forlin97868a22011-07-09 17:12:36 -0400490unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200491{
492 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200493 size_t buflen;
494 struct scatterlist *sg;
495 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200496
Linus Walleij03d640a2016-11-25 10:35:00 +0100497 if (!mqrq->bounce_buf)
498 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200499
Linus Walleij03d640a2016-11-25 10:35:00 +0100500 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200501
Per Forlin97868a22011-07-09 17:12:36 -0400502 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200503
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200504 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400505 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200506 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200507
Per Forlin97868a22011-07-09 17:12:36 -0400508 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200509
510 return 1;
511}
512
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200513/*
514 * If writing, bounce the data to the buffer before the request
515 * is sent to the host driver
516 */
Per Forlin97868a22011-07-09 17:12:36 -0400517void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200518{
Per Forlin97868a22011-07-09 17:12:36 -0400519 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200520 return;
521
Per Forlin97868a22011-07-09 17:12:36 -0400522 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200523 return;
524
Per Forlin97868a22011-07-09 17:12:36 -0400525 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
526 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200527}
528
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200529/*
530 * If reading, bounce the data from the buffer after the request
531 * has been handled by the host driver
532 */
Per Forlin97868a22011-07-09 17:12:36 -0400533void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200534{
Per Forlin97868a22011-07-09 17:12:36 -0400535 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200536 return;
537
Per Forlin97868a22011-07-09 17:12:36 -0400538 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200539 return;
540
Per Forlin97868a22011-07-09 17:12:36 -0400541 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
542 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200543}