blob: 8f5bf5f82aa72f123f6c8ad5c59cda2f52cce929 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Pierre Ossman98ccf142007-05-12 00:26:16 +020025#define MMC_QUEUE_BOUNCESZ 65536
26
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
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020034 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070035 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020036 */
Adrian Hunter7afafc82016-08-16 10:59:35 +030037 if (req->cmd_type != REQ_TYPE_FS && req_op(req) != REQ_OP_DISCARD &&
38 req_op(req) != REQ_OP_SECURE_ERASE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020040 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
42
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +080043 if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053044 return BLKPREP_KILL;
45
Christoph Hellwige8064022016-10-20 15:12:13 +020046 req->rq_flags |= RQF_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020048 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51static int mmc_queue_thread(void *d)
52{
53 struct mmc_queue *mq = d;
54 struct request_queue *q = mq->queue;
Adrian Huntere0097cf2016-11-29 12:09:10 +020055 struct mmc_context_info *cntx = &mq->card->host->context_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Rafael J. Wysocki83144182007-07-17 04:03:35 -070057 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 do {
61 struct request *req = NULL;
62
63 spin_lock_irq(q->queue_lock);
64 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010065 req = blk_fetch_request(q);
Adrian Huntere0097cf2016-11-29 12:09:10 +020066 mq->asleep = false;
67 cntx->is_waiting_last_req = false;
68 cntx->is_new_req = false;
69 if (!req) {
70 /*
71 * Dispatch queue is empty so set flags for
72 * mmc_request_fn() to wake us up.
73 */
74 if (mq->mqrq_prev->req)
75 cntx->is_waiting_last_req = true;
76 else
77 mq->asleep = true;
78 }
Per Forlin97868a22011-07-09 17:12:36 -040079 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 spin_unlock_irq(q->queue_lock);
81
Per Forlinee8a43a2011-07-01 18:55:33 +020082 if (req || mq->mqrq_prev->req) {
Adrian Hunter869c5542016-08-25 14:11:43 -060083 bool req_is_special = mmc_req_is_special(req);
84
Per Forlinee8a43a2011-07-01 18:55:33 +020085 set_current_state(TASK_RUNNING);
Linus Walleij29eb7bd2016-09-20 11:34:38 +020086 mmc_blk_issue_rq(mq, req);
Rabin Vincenta8c27c02015-06-14 19:26:11 +020087 cond_resched();
Konstantin Dorfman2220eed2013-01-14 14:28:17 -050088 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
89 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
90 continue; /* fetch again */
91 }
Seungwon Jeon45c5a912012-09-28 19:12:53 +090092
93 /*
94 * Current request becomes previous request
95 * and vice versa.
Seungwon Jeon369d3212012-12-26 10:40:17 +090096 * In case of special requests, current request
97 * has been finished. Do not assign it to previous
98 * request.
Seungwon Jeon45c5a912012-09-28 19:12:53 +090099 */
Adrian Hunter869c5542016-08-25 14:11:43 -0600100 if (req_is_special)
Seungwon Jeon369d3212012-12-26 10:40:17 +0900101 mq->mqrq_cur->req = NULL;
102
Seungwon Jeon45c5a912012-09-28 19:12:53 +0900103 mq->mqrq_prev->brq.mrq.data = NULL;
104 mq->mqrq_prev->req = NULL;
Fabian Frederick75518472015-06-10 18:30:53 +0200105 swap(mq->mqrq_prev, mq->mqrq_cur);
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 Hunterc8539822016-11-29 12:09:11 +0200188#ifdef CONFIG_MMC_BLOCK_BOUNCE
189static bool mmc_queue_alloc_bounce_bufs(struct mmc_queue *mq,
190 unsigned int bouncesz)
191{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200192 int i;
Adrian Hunterc8539822016-11-29 12:09:11 +0200193
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200194 for (i = 0; i < mq->qdepth; i++) {
195 mq->mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
196 if (!mq->mqrq[i].bounce_buf)
197 goto out_err;
Adrian Hunterc8539822016-11-29 12:09:11 +0200198 }
199
200 return true;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200201
202out_err:
203 while (--i >= 0) {
204 kfree(mq->mqrq[i].bounce_buf);
205 mq->mqrq[i].bounce_buf = NULL;
206 }
207 pr_warn("%s: unable to allocate bounce buffers\n",
208 mmc_card_name(mq->card));
209 return false;
Adrian Hunterc8539822016-11-29 12:09:11 +0200210}
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200211
212static int mmc_queue_alloc_bounce_sgs(struct mmc_queue *mq,
213 unsigned int bouncesz)
214{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200215 int i, ret;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200216
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200217 for (i = 0; i < mq->qdepth; i++) {
218 mq->mqrq[i].sg = mmc_alloc_sg(1, &ret);
219 if (ret)
220 return ret;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200221
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200222 mq->mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret);
223 if (ret)
224 return ret;
225 }
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200226
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200227 return 0;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200228}
Adrian Hunterc8539822016-11-29 12:09:11 +0200229#endif
230
Adrian Hunter64e29e422016-11-29 12:09:13 +0200231static int mmc_queue_alloc_sgs(struct mmc_queue *mq, int max_segs)
232{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200233 int i, ret;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200234
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200235 for (i = 0; i < mq->qdepth; i++) {
236 mq->mqrq[i].sg = mmc_alloc_sg(max_segs, &ret);
237 if (ret)
238 return ret;
239 }
Adrian Hunter64e29e422016-11-29 12:09:13 +0200240
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200241 return 0;
242}
Adrian Hunter64e29e422016-11-29 12:09:13 +0200243
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200244static void mmc_queue_req_free_bufs(struct mmc_queue_req *mqrq)
245{
246 kfree(mqrq->bounce_sg);
247 mqrq->bounce_sg = NULL;
248
249 kfree(mqrq->sg);
250 mqrq->sg = NULL;
251
252 kfree(mqrq->bounce_buf);
253 mqrq->bounce_buf = NULL;
Adrian Hunter64e29e422016-11-29 12:09:13 +0200254}
255
Adrian Hunterc09949c2016-11-29 12:09:14 +0200256static void mmc_queue_reqs_free_bufs(struct mmc_queue *mq)
257{
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200258 int i;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200259
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200260 for (i = 0; i < mq->qdepth; i++)
261 mmc_queue_req_free_bufs(&mq->mqrq[i]);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200262}
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264/**
265 * mmc_init_queue - initialise a queue structure.
266 * @mq: mmc queue
267 * @card: mmc card to attach this queue
268 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300269 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 *
271 * Initialise a MMC card request queue.
272 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300273int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
274 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 struct mmc_host *host = card->host;
277 u64 limit = BLK_BOUNCE_HIGH;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200278 bool bounce = false;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200279 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200281 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000282 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530285 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 if (!mq->queue)
287 return -ENOMEM;
288
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200289 mq->qdepth = 2;
290 mq->mqrq = kcalloc(mq->qdepth, sizeof(struct mmc_queue_req),
291 GFP_KERNEL);
292 if (!mq->mqrq)
293 goto blk_cleanup;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200294 mq->mqrq_cur = &mq->mqrq[0];
295 mq->mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Pierre Ossman98ccf142007-05-12 00:26:16 +0200298 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200299 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600300 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300301 if (mmc_can_erase(card))
302 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200303
304#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400305 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200306 unsigned int bouncesz;
307
Pierre Ossman98ccf142007-05-12 00:26:16 +0200308 bouncesz = MMC_QUEUE_BOUNCESZ;
309
310 if (bouncesz > host->max_req_size)
311 bouncesz = host->max_req_size;
312 if (bouncesz > host->max_seg_size)
313 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200314 if (bouncesz > (host->max_blk_count * 512))
315 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200316
Adrian Hunterc8539822016-11-29 12:09:11 +0200317 if (bouncesz > 512 &&
318 mmc_queue_alloc_bounce_bufs(mq, bouncesz)) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200319 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500320 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500321 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200322 blk_queue_max_segment_size(mq->queue, bouncesz);
323
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200324 ret = mmc_queue_alloc_bounce_sgs(mq, bouncesz);
Per Forlin97868a22011-07-09 17:12:36 -0400325 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200326 goto cleanup_queue;
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200327 bounce = true;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200328 }
329 }
330#endif
331
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200332 if (!bounce) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200333 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500334 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200335 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400336 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200337 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
338
Adrian Hunter64e29e422016-11-29 12:09:13 +0200339 ret = mmc_queue_alloc_sgs(mq, host->max_segs);
Per Forlin04296b72011-07-01 18:55:31 +0200340 if (ret)
341 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
Thomas Gleixner632cf922010-09-14 07:12:35 -0400344 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Adrian Hunterd09408a2011-06-23 13:40:28 +0300346 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
347 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400348
Christoph Hellwig87598a22006-11-13 20:23:52 +0100349 if (IS_ERR(mq->thread)) {
350 ret = PTR_ERR(mq->thread);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200351 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Christoph Hellwig87598a22006-11-13 20:23:52 +0100354 return 0;
Per Forlin97868a22011-07-09 17:12:36 -0400355
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200356 cleanup_queue:
Adrian Hunterc09949c2016-11-29 12:09:14 +0200357 mmc_queue_reqs_free_bufs(mq);
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200358 kfree(mq->mqrq);
359 mq->mqrq = NULL;
360blk_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return ret;
363}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365void mmc_cleanup_queue(struct mmc_queue *mq)
366{
Jens Axboe165125e2007-07-24 09:28:11 +0200367 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100368 unsigned long flags;
369
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200370 /* Make sure the queue isn't suspended, as that will deadlock */
371 mmc_queue_resume(mq);
372
Pierre Ossman89b4e132006-11-14 22:08:16 +0100373 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100374 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800376 /* Empty the queue */
377 spin_lock_irqsave(q->queue_lock, flags);
378 q->queuedata = NULL;
379 blk_start_queue(q);
380 spin_unlock_irqrestore(q->queue_lock, flags);
381
Adrian Hunterc09949c2016-11-29 12:09:14 +0200382 mmc_queue_reqs_free_bufs(mq);
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200383 kfree(mq->mqrq);
384 mq->mqrq = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 mq->card = NULL;
387}
388EXPORT_SYMBOL(mmc_cleanup_queue);
389
390/**
391 * mmc_queue_suspend - suspend a MMC request queue
392 * @mq: MMC queue to suspend
393 *
394 * Stop the block request queue, and wait for our thread to
395 * complete any outstanding requests. This ensures that we
396 * won't suspend while a request is being processed.
397 */
398void mmc_queue_suspend(struct mmc_queue *mq)
399{
Jens Axboe165125e2007-07-24 09:28:11 +0200400 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 unsigned long flags;
402
403 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
404 mq->flags |= MMC_QUEUE_SUSPENDED;
405
406 spin_lock_irqsave(q->queue_lock, flags);
407 blk_stop_queue(q);
408 spin_unlock_irqrestore(q->queue_lock, flags);
409
410 down(&mq->thread_sem);
411 }
412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414/**
415 * mmc_queue_resume - resume a previously suspended MMC request queue
416 * @mq: MMC queue to resume
417 */
418void mmc_queue_resume(struct mmc_queue *mq)
419{
Jens Axboe165125e2007-07-24 09:28:11 +0200420 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 unsigned long flags;
422
423 if (mq->flags & MMC_QUEUE_SUSPENDED) {
424 mq->flags &= ~MMC_QUEUE_SUSPENDED;
425
426 up(&mq->thread_sem);
427
428 spin_lock_irqsave(q->queue_lock, flags);
429 blk_start_queue(q);
430 spin_unlock_irqrestore(q->queue_lock, flags);
431 }
432}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100433
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200434/*
435 * Prepare the sg list(s) to be handed of to the host driver
436 */
Per Forlin97868a22011-07-09 17:12:36 -0400437unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200438{
439 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200440 size_t buflen;
441 struct scatterlist *sg;
442 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200443
Linus Walleij03d640a2016-11-25 10:35:00 +0100444 if (!mqrq->bounce_buf)
445 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200446
Linus Walleij03d640a2016-11-25 10:35:00 +0100447 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200448
Per Forlin97868a22011-07-09 17:12:36 -0400449 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200450
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200451 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400452 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200453 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200454
Per Forlin97868a22011-07-09 17:12:36 -0400455 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200456
457 return 1;
458}
459
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200460/*
461 * If writing, bounce the data to the buffer before the request
462 * is sent to the host driver
463 */
Per Forlin97868a22011-07-09 17:12:36 -0400464void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200465{
Per Forlin97868a22011-07-09 17:12:36 -0400466 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200467 return;
468
Per Forlin97868a22011-07-09 17:12:36 -0400469 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200470 return;
471
Per Forlin97868a22011-07-09 17:12:36 -0400472 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
473 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200474}
475
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200476/*
477 * If reading, bounce the data from the buffer after the request
478 * has been handled by the host driver
479 */
Per Forlin97868a22011-07-09 17:12:36 -0400480void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200481{
Per Forlin97868a22011-07-09 17:12:36 -0400482 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200483 return;
484
Per Forlin97868a22011-07-09 17:12:36 -0400485 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486 return;
487
Per Forlin97868a22011-07-09 17:12:36 -0400488 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
489 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200490}