blob: 32ab5a1947b0c58bc1f4d71d1b3445165ada02d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/card/queue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
Pierre Ossman98ac2162006-12-23 20:03:02 +01005 * Copyright 2006-2007 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/blkdev.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070015#include <linux/freezer.h>
Christoph Hellwig87598a22006-11-13 20:23:52 +010016#include <linux/kthread.h>
Jens Axboe45711f12007-10-22 21:19:53 +020017#include <linux/scatterlist.h>
Santosh Shilimkar8e0cb8a2013-07-29 14:20:15 +010018#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <linux/mmc/card.h>
21#include <linux/mmc/host.h>
Linus Walleij29eb7bd2016-09-20 11:34:38 +020022
Pierre Ossman98ac2162006-12-23 20:03:02 +010023#include "queue.h"
Linus Walleij29eb7bd2016-09-20 11:34:38 +020024#include "block.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
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020035 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070036 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020037 */
Adrian Hunter7afafc82016-08-16 10:59:35 +030038 if (req->cmd_type != REQ_TYPE_FS && req_op(req) != REQ_OP_DISCARD &&
39 req_op(req) != REQ_OP_SECURE_ERASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020041 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 }
43
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +080044 if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053045 return BLKPREP_KILL;
46
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020047 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020049 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050}
51
52static int mmc_queue_thread(void *d)
53{
54 struct mmc_queue *mq = d;
55 struct request_queue *q = mq->queue;
Adrian Huntere0097cf2016-11-29 12:09:10 +020056 struct mmc_context_info *cntx = &mq->card->host->context_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Rafael J. Wysocki83144182007-07-17 04:03:35 -070058 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 do {
62 struct request *req = NULL;
63
64 spin_lock_irq(q->queue_lock);
65 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010066 req = blk_fetch_request(q);
Adrian Huntere0097cf2016-11-29 12:09:10 +020067 mq->asleep = false;
68 cntx->is_waiting_last_req = false;
69 cntx->is_new_req = false;
70 if (!req) {
71 /*
72 * Dispatch queue is empty so set flags for
73 * mmc_request_fn() to wake us up.
74 */
75 if (mq->mqrq_prev->req)
76 cntx->is_waiting_last_req = true;
77 else
78 mq->asleep = true;
79 }
Per Forlin97868a22011-07-09 17:12:36 -040080 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 spin_unlock_irq(q->queue_lock);
82
Per Forlinee8a43a2011-07-01 18:55:33 +020083 if (req || mq->mqrq_prev->req) {
Adrian Hunter869c5542016-08-25 14:11:43 -060084 bool req_is_special = mmc_req_is_special(req);
85
Per Forlinee8a43a2011-07-01 18:55:33 +020086 set_current_state(TASK_RUNNING);
Linus Walleij29eb7bd2016-09-20 11:34:38 +020087 mmc_blk_issue_rq(mq, req);
Rabin Vincenta8c27c02015-06-14 19:26:11 +020088 cond_resched();
Konstantin Dorfman2220eed2013-01-14 14:28:17 -050089 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
90 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
91 continue; /* fetch again */
92 }
Seungwon Jeon45c5a912012-09-28 19:12:53 +090093
94 /*
95 * Current request becomes previous request
96 * and vice versa.
Seungwon Jeon369d3212012-12-26 10:40:17 +090097 * In case of special requests, current request
98 * has been finished. Do not assign it to previous
99 * request.
Seungwon Jeon45c5a912012-09-28 19:12:53 +0900100 */
Adrian Hunter869c5542016-08-25 14:11:43 -0600101 if (req_is_special)
Seungwon Jeon369d3212012-12-26 10:40:17 +0900102 mq->mqrq_cur->req = NULL;
103
Seungwon Jeon45c5a912012-09-28 19:12:53 +0900104 mq->mqrq_prev->brq.mrq.data = NULL;
105 mq->mqrq_prev->req = NULL;
Fabian Frederick75518472015-06-10 18:30:53 +0200106 swap(mq->mqrq_prev, mq->mqrq_cur);
Per Forlinee8a43a2011-07-01 18:55:33 +0200107 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +0100108 if (kthread_should_stop()) {
109 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +0100111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 up(&mq->thread_sem);
113 schedule();
114 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 up(&mq->thread_sem);
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 return 0;
120}
121
122/*
123 * Generic MMC request handler. This is called for any queue on a
124 * particular host. When the host is not busy, we look for a request
125 * on any queue on this host, and attempt to issue it. This may
126 * not be the queue we were asked to process.
127 */
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530128static void mmc_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100131 struct request *req;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500132 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100133
134 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800135 while ((req = blk_fetch_request(q)) != NULL) {
136 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900137 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800138 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100139 return;
140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500142 cntx = &mq->card->host->context_info;
Adrian Huntere0097cf2016-11-29 12:09:10 +0200143
144 if (cntx->is_waiting_last_req) {
145 cntx->is_new_req = true;
146 wake_up_interruptible(&cntx->wait);
147 }
148
149 if (mq->asleep)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100150 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Venkatraman S7513cd72011-08-23 21:16:02 +0530153static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400154{
155 struct scatterlist *sg;
156
157 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
158 if (!sg)
159 *err = -ENOMEM;
160 else {
161 *err = 0;
162 sg_init_table(sg, sg_len);
163 }
164
165 return sg;
166}
167
Adrian Huntere056a1b2011-06-28 17:16:02 +0300168static void mmc_queue_setup_discard(struct request_queue *q,
169 struct mmc_card *card)
170{
171 unsigned max_discard;
172
173 max_discard = mmc_calc_max_discard(card);
174 if (!max_discard)
175 return;
176
177 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600178 blk_queue_max_discard_sectors(q, max_discard);
Adrian Hunter7194efb2012-04-05 14:45:47 +0300179 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300180 q->limits.discard_zeroes_data = 1;
181 q->limits.discard_granularity = card->pref_erase << 9;
182 /* granularity must not be greater than max. discard */
183 if (card->pref_erase > max_discard)
184 q->limits.discard_granularity = 0;
Maya Erez775a9362013-04-18 15:41:55 +0300185 if (mmc_can_secure_erase_trim(card))
Christoph Hellwig288dab82016-06-09 16:00:36 +0200186 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300187}
188
Adrian Hunterc8539822016-11-29 12:09:11 +0200189#ifdef CONFIG_MMC_BLOCK_BOUNCE
190static bool mmc_queue_alloc_bounce_bufs(struct mmc_queue *mq,
191 unsigned int bouncesz)
192{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200193 int i;
Adrian Hunterc8539822016-11-29 12:09:11 +0200194
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200195 for (i = 0; i < mq->qdepth; i++) {
196 mq->mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
197 if (!mq->mqrq[i].bounce_buf)
198 goto out_err;
Adrian Hunterc8539822016-11-29 12:09:11 +0200199 }
200
201 return true;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200202
203out_err:
204 while (--i >= 0) {
205 kfree(mq->mqrq[i].bounce_buf);
206 mq->mqrq[i].bounce_buf = NULL;
207 }
208 pr_warn("%s: unable to allocate bounce buffers\n",
209 mmc_card_name(mq->card));
210 return false;
Adrian Hunterc8539822016-11-29 12:09:11 +0200211}
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200212
213static int mmc_queue_alloc_bounce_sgs(struct mmc_queue *mq,
214 unsigned int bouncesz)
215{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200216 int i, ret;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200217
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200218 for (i = 0; i < mq->qdepth; i++) {
219 mq->mqrq[i].sg = mmc_alloc_sg(1, &ret);
220 if (ret)
221 return ret;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200222
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200223 mq->mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret);
224 if (ret)
225 return ret;
226 }
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200227
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200228 return 0;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200229}
Adrian Hunterc8539822016-11-29 12:09:11 +0200230#endif
231
Adrian Hunter64e29e422016-11-29 12:09:13 +0200232static int mmc_queue_alloc_sgs(struct mmc_queue *mq, int max_segs)
233{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200234 int i, ret;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200235
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200236 for (i = 0; i < mq->qdepth; i++) {
237 mq->mqrq[i].sg = mmc_alloc_sg(max_segs, &ret);
238 if (ret)
239 return ret;
240 }
Adrian Hunter64e29e422016-11-29 12:09:13 +0200241
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200242 return 0;
243}
Adrian Hunter64e29e422016-11-29 12:09:13 +0200244
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200245static void mmc_queue_req_free_bufs(struct mmc_queue_req *mqrq)
246{
247 kfree(mqrq->bounce_sg);
248 mqrq->bounce_sg = NULL;
249
250 kfree(mqrq->sg);
251 mqrq->sg = NULL;
252
253 kfree(mqrq->bounce_buf);
254 mqrq->bounce_buf = NULL;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200255}
256
Adrian Hunterc09949c2016-11-29 12:09:14 +0200257static void mmc_queue_reqs_free_bufs(struct mmc_queue *mq)
258{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200259 int i;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200260
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200261 for (i = 0; i < mq->qdepth; i++)
262 mmc_queue_req_free_bufs(&mq->mqrq[i]);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200263}
264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265/**
266 * mmc_init_queue - initialise a queue structure.
267 * @mq: mmc queue
268 * @card: mmc card to attach this queue
269 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300270 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 *
272 * Initialise a MMC card request queue.
273 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300274int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
275 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 struct mmc_host *host = card->host;
278 u64 limit = BLK_BOUNCE_HIGH;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200279 bool bounce = false;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200280 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200282 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000283 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
285 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530286 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 if (!mq->queue)
288 return -ENOMEM;
289
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200290 mq->qdepth = 2;
291 mq->mqrq = kcalloc(mq->qdepth, sizeof(struct mmc_queue_req),
292 GFP_KERNEL);
293 if (!mq->mqrq)
294 goto blk_cleanup;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200295 mq->mqrq_cur = &mq->mqrq[0];
296 mq->mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Pierre Ossman98ccf142007-05-12 00:26:16 +0200299 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200300 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600301 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300302 if (mmc_can_erase(card))
303 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200304
305#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400306 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200307 unsigned int bouncesz;
308
Pierre Ossman98ccf142007-05-12 00:26:16 +0200309 bouncesz = MMC_QUEUE_BOUNCESZ;
310
311 if (bouncesz > host->max_req_size)
312 bouncesz = host->max_req_size;
313 if (bouncesz > host->max_seg_size)
314 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200315 if (bouncesz > (host->max_blk_count * 512))
316 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200317
Adrian Hunterc8539822016-11-29 12:09:11 +0200318 if (bouncesz > 512 &&
319 mmc_queue_alloc_bounce_bufs(mq, bouncesz)) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200320 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500321 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500322 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200323 blk_queue_max_segment_size(mq->queue, bouncesz);
324
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200325 ret = mmc_queue_alloc_bounce_sgs(mq, bouncesz);
Per Forlin97868a22011-07-09 17:12:36 -0400326 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200327 goto cleanup_queue;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200328 bounce = true;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200329 }
330 }
331#endif
332
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200333 if (!bounce) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200334 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500335 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200336 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400337 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200338 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
339
Adrian Hunter64e29e422016-11-29 12:09:13 +0200340 ret = mmc_queue_alloc_sgs(mq, host->max_segs);
Per Forlin04296b72011-07-01 18:55:31 +0200341 if (ret)
342 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Thomas Gleixner632cf922010-09-14 07:12:35 -0400345 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Adrian Hunterd09408a2011-06-23 13:40:28 +0300347 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
348 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400349
Christoph Hellwig87598a22006-11-13 20:23:52 +0100350 if (IS_ERR(mq->thread)) {
351 ret = PTR_ERR(mq->thread);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200352 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Christoph Hellwig87598a22006-11-13 20:23:52 +0100355 return 0;
Per Forlin97868a22011-07-09 17:12:36 -0400356
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200357 cleanup_queue:
Adrian Hunterc09949c2016-11-29 12:09:14 +0200358 mmc_queue_reqs_free_bufs(mq);
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200359 kfree(mq->mqrq);
360 mq->mqrq = NULL;
361blk_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 return ret;
364}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366void mmc_cleanup_queue(struct mmc_queue *mq)
367{
Jens Axboe165125e2007-07-24 09:28:11 +0200368 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100369 unsigned long flags;
370
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200371 /* Make sure the queue isn't suspended, as that will deadlock */
372 mmc_queue_resume(mq);
373
Pierre Ossman89b4e132006-11-14 22:08:16 +0100374 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100375 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800377 /* Empty the queue */
378 spin_lock_irqsave(q->queue_lock, flags);
379 q->queuedata = NULL;
380 blk_start_queue(q);
381 spin_unlock_irqrestore(q->queue_lock, flags);
382
Adrian Hunterc09949c2016-11-29 12:09:14 +0200383 mmc_queue_reqs_free_bufs(mq);
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200384 kfree(mq->mqrq);
385 mq->mqrq = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 mq->card = NULL;
388}
389EXPORT_SYMBOL(mmc_cleanup_queue);
390
391/**
392 * mmc_queue_suspend - suspend a MMC request queue
393 * @mq: MMC queue to suspend
394 *
395 * Stop the block request queue, and wait for our thread to
396 * complete any outstanding requests. This ensures that we
397 * won't suspend while a request is being processed.
398 */
399void mmc_queue_suspend(struct mmc_queue *mq)
400{
Jens Axboe165125e2007-07-24 09:28:11 +0200401 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 unsigned long flags;
403
404 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
405 mq->flags |= MMC_QUEUE_SUSPENDED;
406
407 spin_lock_irqsave(q->queue_lock, flags);
408 blk_stop_queue(q);
409 spin_unlock_irqrestore(q->queue_lock, flags);
410
411 down(&mq->thread_sem);
412 }
413}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415/**
416 * mmc_queue_resume - resume a previously suspended MMC request queue
417 * @mq: MMC queue to resume
418 */
419void mmc_queue_resume(struct mmc_queue *mq)
420{
Jens Axboe165125e2007-07-24 09:28:11 +0200421 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 unsigned long flags;
423
424 if (mq->flags & MMC_QUEUE_SUSPENDED) {
425 mq->flags &= ~MMC_QUEUE_SUSPENDED;
426
427 up(&mq->thread_sem);
428
429 spin_lock_irqsave(q->queue_lock, flags);
430 blk_start_queue(q);
431 spin_unlock_irqrestore(q->queue_lock, flags);
432 }
433}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100434
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200435/*
436 * Prepare the sg list(s) to be handed of to the host driver
437 */
Per Forlin97868a22011-07-09 17:12:36 -0400438unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200439{
440 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200441 size_t buflen;
442 struct scatterlist *sg;
443 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200444
Linus Walleij03d640a2016-11-25 10:35:00 +0100445 if (!mqrq->bounce_buf)
446 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200447
Per Forlin97868a22011-07-09 17:12:36 -0400448 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200449
Linus Walleij03d640a2016-11-25 10:35:00 +0100450 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200451
Per Forlin97868a22011-07-09 17:12:36 -0400452 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200453
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200454 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400455 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200456 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200457
Per Forlin97868a22011-07-09 17:12:36 -0400458 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200459
460 return 1;
461}
462
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200463/*
464 * If writing, bounce the data to the buffer before the request
465 * is sent to the host driver
466 */
Per Forlin97868a22011-07-09 17:12:36 -0400467void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200468{
Per Forlin97868a22011-07-09 17:12:36 -0400469 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200470 return;
471
Per Forlin97868a22011-07-09 17:12:36 -0400472 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200473 return;
474
Per Forlin97868a22011-07-09 17:12:36 -0400475 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
476 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200477}
478
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200479/*
480 * If reading, bounce the data from the buffer after the request
481 * has been handled by the host driver
482 */
Per Forlin97868a22011-07-09 17:12:36 -0400483void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200484{
Per Forlin97868a22011-07-09 17:12:36 -0400485 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486 return;
487
Per Forlin97868a22011-07-09 17:12:36 -0400488 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200489 return;
490
Per Forlin97868a22011-07-09 17:12:36 -0400491 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
492 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200493}