blob: 64ece67b010991d1e608f826d4a3537d544275d2 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/mmc/card.h>
20#include <linux/mmc/host.h>
Pierre Ossman98ac2162006-12-23 20:03:02 +010021#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Pierre Ossman98ccf142007-05-12 00:26:16 +020023#define MMC_QUEUE_BOUNCESZ 65536
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26/*
Tatyana Brokhman0cc76402012-10-07 09:52:16 +020027 * Based on benchmark tests the default num of requests to trigger the write
28 * packing was determined, to keep the read latency as low as possible and
29 * manage to keep the high write throughput.
30 */
31#define DEFAULT_NUM_REQS_TO_START_PACK 17
32
33/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020034 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
36static int mmc_prep_request(struct request_queue *q, struct request *req)
37{
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +053038 struct mmc_queue *mq = q->queuedata;
39
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020040 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070041 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020042 */
Adrian Hunterbd788c92010-08-11 14:17:47 -070043 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020045 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +053048 if (mq && mmc_card_removed(mq->card))
49 return BLKPREP_KILL;
50
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020051 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020053 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56static int mmc_queue_thread(void *d)
57{
58 struct mmc_queue *mq = d;
59 struct request_queue *q = mq->queue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060 struct request *req;
Maya Erez5f360692012-10-10 03:47:54 +020061 struct mmc_card *card = mq->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Rafael J. Wysocki83144182007-07-17 04:03:35 -070063 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 do {
Lee Susmand50afb52013-01-09 14:48:47 +020067 struct mmc_queue_req *tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 spin_lock_irq(q->queue_lock);
71 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010072 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040073 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 spin_unlock_irq(q->queue_lock);
75
Per Forlinee8a43a2011-07-01 18:55:33 +020076 if (req || mq->mqrq_prev->req) {
77 set_current_state(TASK_RUNNING);
78 mq->issue_fn(mq, req);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +020079 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +020080 continue; /* fetch again */
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +020081 } else if (mq->flags & MMC_QUEUE_URGENT_REQUEST) {
82 mq->mqrq_cur->brq.mrq.data = NULL;
83 mq->mqrq_cur->req = NULL;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +020084 }
Seungwon Jeon09f21532012-09-28 19:12:53 +090085
86 /*
87 * Current request becomes previous request
88 * and vice versa.
89 */
90 mq->mqrq_prev->brq.mrq.data = NULL;
91 mq->mqrq_prev->req = NULL;
92 tmp = mq->mqrq_prev;
93 mq->mqrq_prev = mq->mqrq_cur;
94 mq->mqrq_cur = tmp;
Per Forlinee8a43a2011-07-01 18:55:33 +020095 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010096 if (kthread_should_stop()) {
97 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010099 }
Maya Erez5f360692012-10-10 03:47:54 +0200100 mmc_start_delayed_bkops(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 up(&mq->thread_sem);
102 schedule();
103 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 up(&mq->thread_sem);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return 0;
109}
110
111/*
112 * Generic MMC request handler. This is called for any queue on a
113 * particular host. When the host is not busy, we look for a request
114 * on any queue on this host, and attempt to issue it. This may
115 * not be the queue we were asked to process.
116 */
Jens Axboe165125e2007-07-24 09:28:11 +0200117static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100120 struct request *req;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200121 unsigned long flags;
122 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100123
124 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800125 while ((req = blk_fetch_request(q)) != NULL) {
126 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900127 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800128 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100129 return;
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200132 cntx = &mq->card->host->context_info;
133 if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
134 /*
135 * New MMC request arrived when MMC thread may be
136 * blocked on the previous request to be complete
137 * with no current request fetched
138 */
139 spin_lock_irqsave(&cntx->lock, flags);
140 if (cntx->is_waiting_last_req) {
141 cntx->is_new_req = true;
142 wake_up_interruptible(&cntx->wait);
Lee Susmand50afb52013-01-09 14:48:47 +0200143 }
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200144 spin_unlock_irqrestore(&cntx->lock, flags);
Lee Susmand50afb52013-01-09 14:48:47 +0200145 } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100146 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +0200149/*
150 * mmc_urgent_request() - Urgent MMC request handler.
151 * @q: request queue.
152 *
153 * This is called when block layer has urgent request for delivery. When mmc
154 * context is waiting for the current request to complete, it will be awaken,
155 * current request may be interrupted and re-inserted back to block device
156 * request queue. The next fetched request should be urgent request, this
157 * will be ensured by block i/o scheduler.
158 */
159static void mmc_urgent_request(struct request_queue *q)
160{
161 unsigned long flags;
162 struct mmc_queue *mq = q->queuedata;
163 struct mmc_context_info *cntx;
164
165 if (!mq) {
166 mmc_request(q);
167 return;
168 }
169 cntx = &mq->card->host->context_info;
170
171 /* critical section with mmc_wait_data_done() */
172 spin_lock_irqsave(&cntx->lock, flags);
173
174 /* do stop flow only when mmc thread is waiting for done */
175 if (cntx->is_waiting) {
176 /*
177 * Urgent request must be executed alone
178 * so disable the write packing
179 */
180 mmc_blk_disable_wr_packing(mq);
181 cntx->is_urgent = true;
182 spin_unlock_irqrestore(&cntx->lock, flags);
183 wake_up_interruptible(&cntx->wait);
184 } else {
185 spin_unlock_irqrestore(&cntx->lock, flags);
186 mmc_request(q);
187 }
188}
189
Venkatraman S7513cd72011-08-23 21:16:02 +0530190static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400191{
192 struct scatterlist *sg;
193
194 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
195 if (!sg)
196 *err = -ENOMEM;
197 else {
198 *err = 0;
199 sg_init_table(sg, sg_len);
200 }
201
202 return sg;
203}
204
Adrian Huntere056a1b2011-06-28 17:16:02 +0300205static void mmc_queue_setup_discard(struct request_queue *q,
206 struct mmc_card *card)
207{
208 unsigned max_discard;
209
210 max_discard = mmc_calc_max_discard(card);
211 if (!max_discard)
212 return;
213
214 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
215 q->limits.max_discard_sectors = max_discard;
Adrian Hunter7194efb2012-04-05 14:45:47 +0300216 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300217 q->limits.discard_zeroes_data = 1;
218 q->limits.discard_granularity = card->pref_erase << 9;
219 /* granularity must not be greater than max. discard */
220 if (card->pref_erase > max_discard)
221 q->limits.discard_granularity = 0;
Maya Erez463bb952012-05-24 23:46:29 +0300222 if (mmc_can_secure_erase_trim(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300223 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
224}
225
Maya Erez463bb952012-05-24 23:46:29 +0300226static void mmc_queue_setup_sanitize(struct request_queue *q)
227{
228 queue_flag_set_unlocked(QUEUE_FLAG_SANITIZE, q);
229}
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231/**
232 * mmc_init_queue - initialise a queue structure.
233 * @mq: mmc queue
234 * @card: mmc card to attach this queue
235 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300236 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
238 * Initialise a MMC card request queue.
239 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300240int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
241 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 struct mmc_host *host = card->host;
244 u64 limit = BLK_BOUNCE_HIGH;
245 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400246 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200247 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200249 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
250 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 mq->card = card;
253 mq->queue = blk_init_queue(mmc_request, lock);
254 if (!mq->queue)
255 return -ENOMEM;
256
Konstantin Dorfman9b0f5ec2013-02-12 13:19:48 +0200257 if ((host->caps2 & MMC_CAP2_STOP_REQUEST) &&
258 host->ops->stop_request &&
259 mq->card->ext_csd.hpi)
260 blk_urgent_request(mq->queue, mmc_urgent_request);
261
Maya Erez47b37922012-10-29 20:19:01 +0200262 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
263 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
264
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200265 INIT_LIST_HEAD(&mqrq_cur->packed_list);
266 INIT_LIST_HEAD(&mqrq_prev->packed_list);
267
Per Forlin97868a22011-07-09 17:12:36 -0400268 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200269 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 mq->queue->queuedata = mq;
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200271 mq->num_wr_reqs_to_start_packing =
272 min_t(int, (int)card->ext_csd.max_packed_writes,
273 DEFAULT_NUM_REQS_TO_START_PACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Pierre Ossman98ccf142007-05-12 00:26:16 +0200275 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200276 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300277 if (mmc_can_erase(card))
278 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200279
Maya Erez463bb952012-05-24 23:46:29 +0300280 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
281 mmc_queue_setup_sanitize(mq->queue);
282
Pierre Ossman98ccf142007-05-12 00:26:16 +0200283#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400284 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200285 unsigned int bouncesz;
286
Pierre Ossman98ccf142007-05-12 00:26:16 +0200287 bouncesz = MMC_QUEUE_BOUNCESZ;
288
289 if (bouncesz > host->max_req_size)
290 bouncesz = host->max_req_size;
291 if (bouncesz > host->max_seg_size)
292 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200293 if (bouncesz > (host->max_blk_count * 512))
294 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200295
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200296 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400297 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
298 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530299 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400300 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200301 mmc_card_name(card));
302 }
Per Forlin04296b72011-07-01 18:55:31 +0200303 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
304 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530305 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200306 "allocate bounce prev buffer\n",
307 mmc_card_name(card));
308 kfree(mqrq_cur->bounce_buf);
309 mqrq_cur->bounce_buf = NULL;
310 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200311 }
312
Per Forlin04296b72011-07-01 18:55:31 +0200313 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200314 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500315 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500316 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200317 blk_queue_max_segment_size(mq->queue, bouncesz);
318
Per Forlin97868a22011-07-09 17:12:36 -0400319 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
320 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200321 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200322
Per Forlin97868a22011-07-09 17:12:36 -0400323 mqrq_cur->bounce_sg =
324 mmc_alloc_sg(bouncesz / 512, &ret);
325 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200326 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400327
Per Forlin04296b72011-07-01 18:55:31 +0200328 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
329 if (ret)
330 goto cleanup_queue;
331
332 mqrq_prev->bounce_sg =
333 mmc_alloc_sg(bouncesz / 512, &ret);
334 if (ret)
335 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200336 }
337 }
338#endif
339
Per Forlin04296b72011-07-01 18:55:31 +0200340 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200341 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500342 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200343 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400344 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200345 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
346
Per Forlin97868a22011-07-09 17:12:36 -0400347 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
348 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200349 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400350
Per Forlin04296b72011-07-01 18:55:31 +0200351
352 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
353 if (ret)
354 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
Thomas Gleixner632cf922010-09-14 07:12:35 -0400357 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Adrian Hunterd09408a2011-06-23 13:40:28 +0300359 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
360 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400361
Christoph Hellwig87598a22006-11-13 20:23:52 +0100362 if (IS_ERR(mq->thread)) {
363 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200364 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366
Christoph Hellwig87598a22006-11-13 20:23:52 +0100367 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200368 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400369 kfree(mqrq_cur->bounce_sg);
370 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200371 kfree(mqrq_prev->bounce_sg);
372 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400373
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200374 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400375 kfree(mqrq_cur->sg);
376 mqrq_cur->sg = NULL;
377 kfree(mqrq_cur->bounce_buf);
378 mqrq_cur->bounce_buf = NULL;
379
Per Forlin04296b72011-07-01 18:55:31 +0200380 kfree(mqrq_prev->sg);
381 mqrq_prev->sg = NULL;
382 kfree(mqrq_prev->bounce_buf);
383 mqrq_prev->bounce_buf = NULL;
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 return ret;
387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389void mmc_cleanup_queue(struct mmc_queue *mq)
390{
Jens Axboe165125e2007-07-24 09:28:11 +0200391 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100392 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400393 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200394 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100395
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200396 /* Make sure the queue isn't suspended, as that will deadlock */
397 mmc_queue_resume(mq);
398
Pierre Ossman89b4e132006-11-14 22:08:16 +0100399 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100400 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800402 /* Empty the queue */
403 spin_lock_irqsave(q->queue_lock, flags);
404 q->queuedata = NULL;
405 blk_start_queue(q);
406 spin_unlock_irqrestore(q->queue_lock, flags);
407
Per Forlin97868a22011-07-09 17:12:36 -0400408 kfree(mqrq_cur->bounce_sg);
409 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200410
Per Forlin97868a22011-07-09 17:12:36 -0400411 kfree(mqrq_cur->sg);
412 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Per Forlin97868a22011-07-09 17:12:36 -0400414 kfree(mqrq_cur->bounce_buf);
415 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200416
Per Forlin04296b72011-07-01 18:55:31 +0200417 kfree(mqrq_prev->bounce_sg);
418 mqrq_prev->bounce_sg = NULL;
419
420 kfree(mqrq_prev->sg);
421 mqrq_prev->sg = NULL;
422
423 kfree(mqrq_prev->bounce_buf);
424 mqrq_prev->bounce_buf = NULL;
425
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
443 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
444 mq->flags |= MMC_QUEUE_SUSPENDED;
445
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
463 if (mq->flags & MMC_QUEUE_SUSPENDED) {
464 mq->flags &= ~MMC_QUEUE_SUSPENDED;
465
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
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200474static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
475 struct mmc_queue_req *mqrq,
476 struct scatterlist *sg)
477{
478 struct scatterlist *__sg;
479 unsigned int sg_len = 0;
480 struct request *req;
481 enum mmc_packed_cmd cmd;
482
483 cmd = mqrq->packed_cmd;
484
485 if (cmd == MMC_PACKED_WRITE) {
486 __sg = sg;
487 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
488 sizeof(mqrq->packed_cmd_hdr));
489 sg_len++;
490 __sg->page_link &= ~0x02;
491 }
492
493 __sg = sg + sg_len;
494 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
495 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
496 __sg = sg + (sg_len - 1);
497 (__sg++)->page_link &= ~0x02;
498 }
499 sg_mark_end(sg + (sg_len - 1));
500 return sg_len;
501}
502
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200503/*
504 * Prepare the sg list(s) to be handed of to the host driver
505 */
Per Forlin97868a22011-07-09 17:12:36 -0400506unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200507{
508 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200509 size_t buflen;
510 struct scatterlist *sg;
511 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200512
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200513 if (!mqrq->bounce_buf) {
514 if (!list_empty(&mqrq->packed_list))
515 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
516 else
517 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
518 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200519
Per Forlin97868a22011-07-09 17:12:36 -0400520 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200521
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200522 if (!list_empty(&mqrq->packed_list))
523 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
524 else
525 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200526
Per Forlin97868a22011-07-09 17:12:36 -0400527 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200528
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200529 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400530 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200531 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200532
Per Forlin97868a22011-07-09 17:12:36 -0400533 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200534
535 return 1;
536}
537
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200538/*
539 * If writing, bounce the data to the buffer before the request
540 * is sent to the host driver
541 */
Per Forlin97868a22011-07-09 17:12:36 -0400542void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200543{
Per Forlin97868a22011-07-09 17:12:36 -0400544 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200545 return;
546
Per Forlin97868a22011-07-09 17:12:36 -0400547 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200548 return;
549
Per Forlin97868a22011-07-09 17:12:36 -0400550 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
551 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200552}
553
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200554/*
555 * If reading, bounce the data from the buffer after the request
556 * has been handled by the host driver
557 */
Per Forlin97868a22011-07-09 17:12:36 -0400558void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200559{
Per Forlin97868a22011-07-09 17:12:36 -0400560 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200561 return;
562
Per Forlin97868a22011-07-09 17:12:36 -0400563 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200564 return;
565
Per Forlin97868a22011-07-09 17:12:36 -0400566 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
567 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200568}