blob: 5196312bb55e437d60bdc7b1d72813fa5f4498c7 [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
Christoph Hellwig87598a22006-11-13 20:23:52 +010025#define MMC_QUEUE_SUSPENDED (1 << 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
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{
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020032 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070033 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020034 */
Adrian Hunterbd788c92010-08-11 14:17:47 -070035 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020037 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020040 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020042 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45static int mmc_queue_thread(void *d)
46{
47 struct mmc_queue *mq = d;
48 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Rafael J. Wysocki83144182007-07-17 04:03:35 -070050 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 do {
54 struct request *req = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +020055 struct mmc_queue_req *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57 spin_lock_irq(q->queue_lock);
58 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010059 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040060 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 spin_unlock_irq(q->queue_lock);
62
Per Forlinee8a43a2011-07-01 18:55:33 +020063 if (req || mq->mqrq_prev->req) {
64 set_current_state(TASK_RUNNING);
65 mq->issue_fn(mq, req);
66 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010067 if (kthread_should_stop()) {
68 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 up(&mq->thread_sem);
72 schedule();
73 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Per Forlinee8a43a2011-07-01 18:55:33 +020076 /* Current request becomes previous request and vice versa. */
77 mq->mqrq_prev->brq.mrq.data = NULL;
78 mq->mqrq_prev->req = NULL;
79 tmp = mq->mqrq_prev;
80 mq->mqrq_prev = mq->mqrq_cur;
81 mq->mqrq_cur = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 up(&mq->thread_sem);
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return 0;
86}
87
88/*
89 * Generic MMC request handler. This is called for any queue on a
90 * particular host. When the host is not busy, we look for a request
91 * on any queue on this host, and attempt to issue it. This may
92 * not be the queue we were asked to process.
93 */
Jens Axboe165125e2007-07-24 09:28:11 +020094static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +010097 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +010098
99 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800100 while ((req = blk_fetch_request(q)) != NULL) {
101 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900102 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800103 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100104 return;
105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Per Forlinee8a43a2011-07-01 18:55:33 +0200107 if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100108 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Venkatraman S7513cd72011-08-23 21:16:02 +0530111static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400112{
113 struct scatterlist *sg;
114
115 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
116 if (!sg)
117 *err = -ENOMEM;
118 else {
119 *err = 0;
120 sg_init_table(sg, sg_len);
121 }
122
123 return sg;
124}
125
Adrian Huntere056a1b2011-06-28 17:16:02 +0300126static void mmc_queue_setup_discard(struct request_queue *q,
127 struct mmc_card *card)
128{
129 unsigned max_discard;
130
131 max_discard = mmc_calc_max_discard(card);
132 if (!max_discard)
133 return;
134
135 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
136 q->limits.max_discard_sectors = max_discard;
137 if (card->erased_byte == 0)
138 q->limits.discard_zeroes_data = 1;
139 q->limits.discard_granularity = card->pref_erase << 9;
140 /* granularity must not be greater than max. discard */
141 if (card->pref_erase > max_discard)
142 q->limits.discard_granularity = 0;
143 if (mmc_can_secure_erase_trim(card))
144 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/**
148 * mmc_init_queue - initialise a queue structure.
149 * @mq: mmc queue
150 * @card: mmc card to attach this queue
151 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300152 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 *
154 * Initialise a MMC card request queue.
155 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300156int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
157 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct mmc_host *host = card->host;
160 u64 limit = BLK_BOUNCE_HIGH;
161 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400162 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200163 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200165 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
166 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 mq->card = card;
169 mq->queue = blk_init_queue(mmc_request, lock);
170 if (!mq->queue)
171 return -ENOMEM;
172
Per Forlin97868a22011-07-09 17:12:36 -0400173 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
Per Forlin04296b72011-07-01 18:55:31 +0200174 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
Per Forlin97868a22011-07-09 17:12:36 -0400175 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200176 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
Pierre Ossman98ccf142007-05-12 00:26:16 +0200179 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200180 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300181 if (mmc_can_erase(card))
182 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200183
184#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400185 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200186 unsigned int bouncesz;
187
Pierre Ossman98ccf142007-05-12 00:26:16 +0200188 bouncesz = MMC_QUEUE_BOUNCESZ;
189
190 if (bouncesz > host->max_req_size)
191 bouncesz = host->max_req_size;
192 if (bouncesz > host->max_seg_size)
193 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200194 if (bouncesz > (host->max_blk_count * 512))
195 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200196
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200197 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400198 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
199 if (!mqrq_cur->bounce_buf) {
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200200 printk(KERN_WARNING "%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400201 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200202 mmc_card_name(card));
203 }
Per Forlin04296b72011-07-01 18:55:31 +0200204 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
205 if (!mqrq_prev->bounce_buf) {
206 printk(KERN_WARNING "%s: unable to "
207 "allocate bounce prev buffer\n",
208 mmc_card_name(card));
209 kfree(mqrq_cur->bounce_buf);
210 mqrq_cur->bounce_buf = NULL;
211 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200212 }
213
Per Forlin04296b72011-07-01 18:55:31 +0200214 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200215 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500216 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500217 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200218 blk_queue_max_segment_size(mq->queue, bouncesz);
219
Per Forlin97868a22011-07-09 17:12:36 -0400220 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
221 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200222 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200223
Per Forlin97868a22011-07-09 17:12:36 -0400224 mqrq_cur->bounce_sg =
225 mmc_alloc_sg(bouncesz / 512, &ret);
226 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200227 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400228
Per Forlin04296b72011-07-01 18:55:31 +0200229 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
230 if (ret)
231 goto cleanup_queue;
232
233 mqrq_prev->bounce_sg =
234 mmc_alloc_sg(bouncesz / 512, &ret);
235 if (ret)
236 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200237 }
238 }
239#endif
240
Per Forlin04296b72011-07-01 18:55:31 +0200241 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200242 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500243 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200244 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400245 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200246 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
247
Per Forlin97868a22011-07-09 17:12:36 -0400248 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
249 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200250 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400251
Per Forlin04296b72011-07-01 18:55:31 +0200252
253 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
254 if (ret)
255 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Thomas Gleixner632cf922010-09-14 07:12:35 -0400258 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Adrian Hunterd09408a2011-06-23 13:40:28 +0300260 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
261 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400262
Christoph Hellwig87598a22006-11-13 20:23:52 +0100263 if (IS_ERR(mq->thread)) {
264 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200265 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267
Christoph Hellwig87598a22006-11-13 20:23:52 +0100268 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200269 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400270 kfree(mqrq_cur->bounce_sg);
271 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200272 kfree(mqrq_prev->bounce_sg);
273 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400274
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200275 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400276 kfree(mqrq_cur->sg);
277 mqrq_cur->sg = NULL;
278 kfree(mqrq_cur->bounce_buf);
279 mqrq_cur->bounce_buf = NULL;
280
Per Forlin04296b72011-07-01 18:55:31 +0200281 kfree(mqrq_prev->sg);
282 mqrq_prev->sg = NULL;
283 kfree(mqrq_prev->bounce_buf);
284 mqrq_prev->bounce_buf = NULL;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return ret;
288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290void mmc_cleanup_queue(struct mmc_queue *mq)
291{
Jens Axboe165125e2007-07-24 09:28:11 +0200292 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100293 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400294 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200295 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100296
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200297 /* Make sure the queue isn't suspended, as that will deadlock */
298 mmc_queue_resume(mq);
299
Pierre Ossman89b4e132006-11-14 22:08:16 +0100300 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100301 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800303 /* Empty the queue */
304 spin_lock_irqsave(q->queue_lock, flags);
305 q->queuedata = NULL;
306 blk_start_queue(q);
307 spin_unlock_irqrestore(q->queue_lock, flags);
308
Per Forlin97868a22011-07-09 17:12:36 -0400309 kfree(mqrq_cur->bounce_sg);
310 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200311
Per Forlin97868a22011-07-09 17:12:36 -0400312 kfree(mqrq_cur->sg);
313 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Per Forlin97868a22011-07-09 17:12:36 -0400315 kfree(mqrq_cur->bounce_buf);
316 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200317
Per Forlin04296b72011-07-01 18:55:31 +0200318 kfree(mqrq_prev->bounce_sg);
319 mqrq_prev->bounce_sg = NULL;
320
321 kfree(mqrq_prev->sg);
322 mqrq_prev->sg = NULL;
323
324 kfree(mqrq_prev->bounce_buf);
325 mqrq_prev->bounce_buf = NULL;
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 mq->card = NULL;
328}
329EXPORT_SYMBOL(mmc_cleanup_queue);
330
331/**
332 * mmc_queue_suspend - suspend a MMC request queue
333 * @mq: MMC queue to suspend
334 *
335 * Stop the block request queue, and wait for our thread to
336 * complete any outstanding requests. This ensures that we
337 * won't suspend while a request is being processed.
338 */
339void mmc_queue_suspend(struct mmc_queue *mq)
340{
Jens Axboe165125e2007-07-24 09:28:11 +0200341 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 unsigned long flags;
343
344 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
345 mq->flags |= MMC_QUEUE_SUSPENDED;
346
347 spin_lock_irqsave(q->queue_lock, flags);
348 blk_stop_queue(q);
349 spin_unlock_irqrestore(q->queue_lock, flags);
350
351 down(&mq->thread_sem);
352 }
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355/**
356 * mmc_queue_resume - resume a previously suspended MMC request queue
357 * @mq: MMC queue to resume
358 */
359void mmc_queue_resume(struct mmc_queue *mq)
360{
Jens Axboe165125e2007-07-24 09:28:11 +0200361 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 unsigned long flags;
363
364 if (mq->flags & MMC_QUEUE_SUSPENDED) {
365 mq->flags &= ~MMC_QUEUE_SUSPENDED;
366
367 up(&mq->thread_sem);
368
369 spin_lock_irqsave(q->queue_lock, flags);
370 blk_start_queue(q);
371 spin_unlock_irqrestore(q->queue_lock, flags);
372 }
373}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100374
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200375/*
376 * Prepare the sg list(s) to be handed of to the host driver
377 */
Per Forlin97868a22011-07-09 17:12:36 -0400378unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200379{
380 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200381 size_t buflen;
382 struct scatterlist *sg;
383 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200384
Per Forlin97868a22011-07-09 17:12:36 -0400385 if (!mqrq->bounce_buf)
386 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200387
Per Forlin97868a22011-07-09 17:12:36 -0400388 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200389
Per Forlin97868a22011-07-09 17:12:36 -0400390 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200391
Per Forlin97868a22011-07-09 17:12:36 -0400392 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200393
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200394 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400395 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200396 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200397
Per Forlin97868a22011-07-09 17:12:36 -0400398 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200399
400 return 1;
401}
402
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200403/*
404 * If writing, bounce the data to the buffer before the request
405 * is sent to the host driver
406 */
Per Forlin97868a22011-07-09 17:12:36 -0400407void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200408{
Per Forlin97868a22011-07-09 17:12:36 -0400409 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200410 return;
411
Per Forlin97868a22011-07-09 17:12:36 -0400412 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200413 return;
414
Per Forlin97868a22011-07-09 17:12:36 -0400415 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
416 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200417}
418
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200419/*
420 * If reading, bounce the data from the buffer after the request
421 * has been handled by the host driver
422 */
Per Forlin97868a22011-07-09 17:12:36 -0400423void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200424{
Per Forlin97868a22011-07-09 17:12:36 -0400425 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200426 return;
427
Per Forlin97868a22011-07-09 17:12:36 -0400428 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200429 return;
430
Per Forlin97868a22011-07-09 17:12:36 -0400431 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
432 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200433}