blob: 010888d8d97c230874948d350e51056cd4f1cc4d [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/**
190 * mmc_init_queue - initialise a queue structure.
191 * @mq: mmc queue
192 * @card: mmc card to attach this queue
193 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300194 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * Initialise a MMC card request queue.
197 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300198int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
199 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 struct mmc_host *host = card->host;
202 u64 limit = BLK_BOUNCE_HIGH;
203 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400204 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200205 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200207 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000208 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
210 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530211 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!mq->queue)
213 return -ENOMEM;
214
Per Forlin97868a22011-07-09 17:12:36 -0400215 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200216 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Pierre Ossman98ccf142007-05-12 00:26:16 +0200219 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200220 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600221 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300222 if (mmc_can_erase(card))
223 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200224
225#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400226 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200227 unsigned int bouncesz;
228
Pierre Ossman98ccf142007-05-12 00:26:16 +0200229 bouncesz = MMC_QUEUE_BOUNCESZ;
230
231 if (bouncesz > host->max_req_size)
232 bouncesz = host->max_req_size;
233 if (bouncesz > host->max_seg_size)
234 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200235 if (bouncesz > (host->max_blk_count * 512))
236 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200237
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200238 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400239 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
240 if (!mqrq_cur->bounce_buf) {
Joe Perches66061102014-09-12 14:56:56 -0700241 pr_warn("%s: unable to allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200242 mmc_card_name(card));
Bhuvanesh Suracharifdb409f2014-12-01 02:23:02 -0500243 } else {
244 mqrq_prev->bounce_buf =
245 kmalloc(bouncesz, GFP_KERNEL);
246 if (!mqrq_prev->bounce_buf) {
247 pr_warn("%s: unable to allocate bounce prev buffer\n",
248 mmc_card_name(card));
249 kfree(mqrq_cur->bounce_buf);
250 mqrq_cur->bounce_buf = NULL;
251 }
Per Forlin04296b72011-07-01 18:55:31 +0200252 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200253 }
254
Per Forlin04296b72011-07-01 18:55:31 +0200255 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200256 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500257 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500258 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200259 blk_queue_max_segment_size(mq->queue, bouncesz);
260
Per Forlin97868a22011-07-09 17:12:36 -0400261 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
262 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200263 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200264
Per Forlin97868a22011-07-09 17:12:36 -0400265 mqrq_cur->bounce_sg =
266 mmc_alloc_sg(bouncesz / 512, &ret);
267 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200268 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400269
Per Forlin04296b72011-07-01 18:55:31 +0200270 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
271 if (ret)
272 goto cleanup_queue;
273
274 mqrq_prev->bounce_sg =
275 mmc_alloc_sg(bouncesz / 512, &ret);
276 if (ret)
277 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200278 }
279 }
280#endif
281
Per Forlin04296b72011-07-01 18:55:31 +0200282 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200283 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500284 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200285 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400286 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200287 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
288
Per Forlin97868a22011-07-09 17:12:36 -0400289 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
290 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200291 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400292
Per Forlin04296b72011-07-01 18:55:31 +0200293
294 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
295 if (ret)
296 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Thomas Gleixner632cf922010-09-14 07:12:35 -0400299 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Adrian Hunterd09408a2011-06-23 13:40:28 +0300301 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
302 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400303
Christoph Hellwig87598a22006-11-13 20:23:52 +0100304 if (IS_ERR(mq->thread)) {
305 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200306 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Christoph Hellwig87598a22006-11-13 20:23:52 +0100309 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200310 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400311 kfree(mqrq_cur->bounce_sg);
312 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200313 kfree(mqrq_prev->bounce_sg);
314 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400315
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200316 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400317 kfree(mqrq_cur->sg);
318 mqrq_cur->sg = NULL;
319 kfree(mqrq_cur->bounce_buf);
320 mqrq_cur->bounce_buf = NULL;
321
Per Forlin04296b72011-07-01 18:55:31 +0200322 kfree(mqrq_prev->sg);
323 mqrq_prev->sg = NULL;
324 kfree(mqrq_prev->bounce_buf);
325 mqrq_prev->bounce_buf = NULL;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return ret;
329}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331void mmc_cleanup_queue(struct mmc_queue *mq)
332{
Jens Axboe165125e2007-07-24 09:28:11 +0200333 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100334 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400335 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200336 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100337
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200338 /* Make sure the queue isn't suspended, as that will deadlock */
339 mmc_queue_resume(mq);
340
Pierre Ossman89b4e132006-11-14 22:08:16 +0100341 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100342 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800344 /* Empty the queue */
345 spin_lock_irqsave(q->queue_lock, flags);
346 q->queuedata = NULL;
347 blk_start_queue(q);
348 spin_unlock_irqrestore(q->queue_lock, flags);
349
Per Forlin97868a22011-07-09 17:12:36 -0400350 kfree(mqrq_cur->bounce_sg);
351 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200352
Per Forlin97868a22011-07-09 17:12:36 -0400353 kfree(mqrq_cur->sg);
354 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Per Forlin97868a22011-07-09 17:12:36 -0400356 kfree(mqrq_cur->bounce_buf);
357 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200358
Per Forlin04296b72011-07-01 18:55:31 +0200359 kfree(mqrq_prev->bounce_sg);
360 mqrq_prev->bounce_sg = NULL;
361
362 kfree(mqrq_prev->sg);
363 mqrq_prev->sg = NULL;
364
365 kfree(mqrq_prev->bounce_buf);
366 mqrq_prev->bounce_buf = NULL;
367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 mq->card = NULL;
369}
370EXPORT_SYMBOL(mmc_cleanup_queue);
371
372/**
373 * mmc_queue_suspend - suspend a MMC request queue
374 * @mq: MMC queue to suspend
375 *
376 * Stop the block request queue, and wait for our thread to
377 * complete any outstanding requests. This ensures that we
378 * won't suspend while a request is being processed.
379 */
380void mmc_queue_suspend(struct mmc_queue *mq)
381{
Jens Axboe165125e2007-07-24 09:28:11 +0200382 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned long flags;
384
385 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
386 mq->flags |= MMC_QUEUE_SUSPENDED;
387
388 spin_lock_irqsave(q->queue_lock, flags);
389 blk_stop_queue(q);
390 spin_unlock_irqrestore(q->queue_lock, flags);
391
392 down(&mq->thread_sem);
393 }
394}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396/**
397 * mmc_queue_resume - resume a previously suspended MMC request queue
398 * @mq: MMC queue to resume
399 */
400void mmc_queue_resume(struct mmc_queue *mq)
401{
Jens Axboe165125e2007-07-24 09:28:11 +0200402 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 unsigned long flags;
404
405 if (mq->flags & MMC_QUEUE_SUSPENDED) {
406 mq->flags &= ~MMC_QUEUE_SUSPENDED;
407
408 up(&mq->thread_sem);
409
410 spin_lock_irqsave(q->queue_lock, flags);
411 blk_start_queue(q);
412 spin_unlock_irqrestore(q->queue_lock, flags);
413 }
414}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100415
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200416/*
417 * Prepare the sg list(s) to be handed of to the host driver
418 */
Per Forlin97868a22011-07-09 17:12:36 -0400419unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200420{
421 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200422 size_t buflen;
423 struct scatterlist *sg;
424 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200425
Linus Walleij03d640a2016-11-25 10:35:00 +0100426 if (!mqrq->bounce_buf)
427 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200428
Per Forlin97868a22011-07-09 17:12:36 -0400429 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200430
Linus Walleij03d640a2016-11-25 10:35:00 +0100431 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200432
Per Forlin97868a22011-07-09 17:12:36 -0400433 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200434
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200435 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400436 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200437 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200438
Per Forlin97868a22011-07-09 17:12:36 -0400439 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200440
441 return 1;
442}
443
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200444/*
445 * If writing, bounce the data to the buffer before the request
446 * is sent to the host driver
447 */
Per Forlin97868a22011-07-09 17:12:36 -0400448void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200449{
Per Forlin97868a22011-07-09 17:12:36 -0400450 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200451 return;
452
Per Forlin97868a22011-07-09 17:12:36 -0400453 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200454 return;
455
Per Forlin97868a22011-07-09 17:12:36 -0400456 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
457 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200458}
459
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200460/*
461 * If reading, bounce the data from the buffer after the request
462 * has been handled by the host driver
463 */
Per Forlin97868a22011-07-09 17:12:36 -0400464void mmc_queue_bounce_post(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) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200470 return;
471
Per Forlin97868a22011-07-09 17:12:36 -0400472 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
473 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200474}