blob: a38d310f503028f3c2094192fcd8fe434a37a4e6 [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;
55
56 spin_lock_irq(q->queue_lock);
57 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010058 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040059 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 spin_unlock_irq(q->queue_lock);
61
62 if (!req) {
Vitaly Wool7b30d282006-12-07 20:08:02 +010063 if (kthread_should_stop()) {
64 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 up(&mq->thread_sem);
68 schedule();
69 down(&mq->thread_sem);
70 continue;
71 }
72 set_current_state(TASK_RUNNING);
73
74 mq->issue_fn(mq, req);
75 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 up(&mq->thread_sem);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 return 0;
79}
80
81/*
82 * Generic MMC request handler. This is called for any queue on a
83 * particular host. When the host is not busy, we look for a request
84 * on any queue on this host, and attempt to issue it. This may
85 * not be the queue we were asked to process.
86 */
Jens Axboe165125e2007-07-24 09:28:11 +020087static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +010090 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +010091
92 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -080093 while ((req = blk_fetch_request(q)) != NULL) {
94 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +090095 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -080096 }
Pierre Ossman89b4e132006-11-14 22:08:16 +010097 return;
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Per Forlin97868a22011-07-09 17:12:36 -0400100 if (!mq->mqrq_cur->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100101 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Per Forlin97868a22011-07-09 17:12:36 -0400104struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
105{
106 struct scatterlist *sg;
107
108 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
109 if (!sg)
110 *err = -ENOMEM;
111 else {
112 *err = 0;
113 sg_init_table(sg, sg_len);
114 }
115
116 return sg;
117}
118
Adrian Huntere056a1b2011-06-28 17:16:02 +0300119static void mmc_queue_setup_discard(struct request_queue *q,
120 struct mmc_card *card)
121{
122 unsigned max_discard;
123
124 max_discard = mmc_calc_max_discard(card);
125 if (!max_discard)
126 return;
127
128 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
129 q->limits.max_discard_sectors = max_discard;
130 if (card->erased_byte == 0)
131 q->limits.discard_zeroes_data = 1;
132 q->limits.discard_granularity = card->pref_erase << 9;
133 /* granularity must not be greater than max. discard */
134 if (card->pref_erase > max_discard)
135 q->limits.discard_granularity = 0;
136 if (mmc_can_secure_erase_trim(card))
137 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/**
141 * mmc_init_queue - initialise a queue structure.
142 * @mq: mmc queue
143 * @card: mmc card to attach this queue
144 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300145 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 *
147 * Initialise a MMC card request queue.
148 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300149int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
150 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct mmc_host *host = card->host;
153 u64 limit = BLK_BOUNCE_HIGH;
154 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400155 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200156 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200158 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
159 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 mq->card = card;
162 mq->queue = blk_init_queue(mmc_request, lock);
163 if (!mq->queue)
164 return -ENOMEM;
165
Per Forlin97868a22011-07-09 17:12:36 -0400166 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
Per Forlin04296b72011-07-01 18:55:31 +0200167 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
Per Forlin97868a22011-07-09 17:12:36 -0400168 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200169 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Pierre Ossman98ccf142007-05-12 00:26:16 +0200172 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200173 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300174 if (mmc_can_erase(card))
175 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200176
177#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400178 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200179 unsigned int bouncesz;
180
Pierre Ossman98ccf142007-05-12 00:26:16 +0200181 bouncesz = MMC_QUEUE_BOUNCESZ;
182
183 if (bouncesz > host->max_req_size)
184 bouncesz = host->max_req_size;
185 if (bouncesz > host->max_seg_size)
186 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200187 if (bouncesz > (host->max_blk_count * 512))
188 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200189
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200190 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400191 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
192 if (!mqrq_cur->bounce_buf) {
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200193 printk(KERN_WARNING "%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400194 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200195 mmc_card_name(card));
196 }
Per Forlin04296b72011-07-01 18:55:31 +0200197 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
198 if (!mqrq_prev->bounce_buf) {
199 printk(KERN_WARNING "%s: unable to "
200 "allocate bounce prev buffer\n",
201 mmc_card_name(card));
202 kfree(mqrq_cur->bounce_buf);
203 mqrq_cur->bounce_buf = NULL;
204 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200205 }
206
Per Forlin04296b72011-07-01 18:55:31 +0200207 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200208 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500209 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500210 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200211 blk_queue_max_segment_size(mq->queue, bouncesz);
212
Per Forlin97868a22011-07-09 17:12:36 -0400213 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
214 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200215 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200216
Per Forlin97868a22011-07-09 17:12:36 -0400217 mqrq_cur->bounce_sg =
218 mmc_alloc_sg(bouncesz / 512, &ret);
219 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200220 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400221
Per Forlin04296b72011-07-01 18:55:31 +0200222 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
223 if (ret)
224 goto cleanup_queue;
225
226 mqrq_prev->bounce_sg =
227 mmc_alloc_sg(bouncesz / 512, &ret);
228 if (ret)
229 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200230 }
231 }
232#endif
233
Per Forlin04296b72011-07-01 18:55:31 +0200234 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200235 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500236 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200237 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400238 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200239 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
240
Per Forlin97868a22011-07-09 17:12:36 -0400241 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
242 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200243 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400244
Per Forlin04296b72011-07-01 18:55:31 +0200245
246 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
247 if (ret)
248 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
Thomas Gleixner632cf922010-09-14 07:12:35 -0400251 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Adrian Hunterd09408a2011-06-23 13:40:28 +0300253 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
254 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400255
Christoph Hellwig87598a22006-11-13 20:23:52 +0100256 if (IS_ERR(mq->thread)) {
257 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200258 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
Christoph Hellwig87598a22006-11-13 20:23:52 +0100261 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200262 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400263 kfree(mqrq_cur->bounce_sg);
264 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200265 kfree(mqrq_prev->bounce_sg);
266 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400267
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200268 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400269 kfree(mqrq_cur->sg);
270 mqrq_cur->sg = NULL;
271 kfree(mqrq_cur->bounce_buf);
272 mqrq_cur->bounce_buf = NULL;
273
Per Forlin04296b72011-07-01 18:55:31 +0200274 kfree(mqrq_prev->sg);
275 mqrq_prev->sg = NULL;
276 kfree(mqrq_prev->bounce_buf);
277 mqrq_prev->bounce_buf = NULL;
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return ret;
281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283void mmc_cleanup_queue(struct mmc_queue *mq)
284{
Jens Axboe165125e2007-07-24 09:28:11 +0200285 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100286 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400287 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200288 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100289
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200290 /* Make sure the queue isn't suspended, as that will deadlock */
291 mmc_queue_resume(mq);
292
Pierre Ossman89b4e132006-11-14 22:08:16 +0100293 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100294 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800296 /* Empty the queue */
297 spin_lock_irqsave(q->queue_lock, flags);
298 q->queuedata = NULL;
299 blk_start_queue(q);
300 spin_unlock_irqrestore(q->queue_lock, flags);
301
Per Forlin97868a22011-07-09 17:12:36 -0400302 kfree(mqrq_cur->bounce_sg);
303 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200304
Per Forlin97868a22011-07-09 17:12:36 -0400305 kfree(mqrq_cur->sg);
306 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Per Forlin97868a22011-07-09 17:12:36 -0400308 kfree(mqrq_cur->bounce_buf);
309 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200310
Per Forlin04296b72011-07-01 18:55:31 +0200311 kfree(mqrq_prev->bounce_sg);
312 mqrq_prev->bounce_sg = NULL;
313
314 kfree(mqrq_prev->sg);
315 mqrq_prev->sg = NULL;
316
317 kfree(mqrq_prev->bounce_buf);
318 mqrq_prev->bounce_buf = NULL;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 mq->card = NULL;
321}
322EXPORT_SYMBOL(mmc_cleanup_queue);
323
324/**
325 * mmc_queue_suspend - suspend a MMC request queue
326 * @mq: MMC queue to suspend
327 *
328 * Stop the block request queue, and wait for our thread to
329 * complete any outstanding requests. This ensures that we
330 * won't suspend while a request is being processed.
331 */
332void mmc_queue_suspend(struct mmc_queue *mq)
333{
Jens Axboe165125e2007-07-24 09:28:11 +0200334 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 unsigned long flags;
336
337 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
338 mq->flags |= MMC_QUEUE_SUSPENDED;
339
340 spin_lock_irqsave(q->queue_lock, flags);
341 blk_stop_queue(q);
342 spin_unlock_irqrestore(q->queue_lock, flags);
343
344 down(&mq->thread_sem);
345 }
346}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348/**
349 * mmc_queue_resume - resume a previously suspended MMC request queue
350 * @mq: MMC queue to resume
351 */
352void mmc_queue_resume(struct mmc_queue *mq)
353{
Jens Axboe165125e2007-07-24 09:28:11 +0200354 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 unsigned long flags;
356
357 if (mq->flags & MMC_QUEUE_SUSPENDED) {
358 mq->flags &= ~MMC_QUEUE_SUSPENDED;
359
360 up(&mq->thread_sem);
361
362 spin_lock_irqsave(q->queue_lock, flags);
363 blk_start_queue(q);
364 spin_unlock_irqrestore(q->queue_lock, flags);
365 }
366}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100367
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200368/*
369 * Prepare the sg list(s) to be handed of to the host driver
370 */
Per Forlin97868a22011-07-09 17:12:36 -0400371unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200372{
373 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200374 size_t buflen;
375 struct scatterlist *sg;
376 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200377
Per Forlin97868a22011-07-09 17:12:36 -0400378 if (!mqrq->bounce_buf)
379 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200380
Per Forlin97868a22011-07-09 17:12:36 -0400381 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200382
Per Forlin97868a22011-07-09 17:12:36 -0400383 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200384
Per Forlin97868a22011-07-09 17:12:36 -0400385 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200386
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200387 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400388 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200389 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200390
Per Forlin97868a22011-07-09 17:12:36 -0400391 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200392
393 return 1;
394}
395
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200396/*
397 * If writing, bounce the data to the buffer before the request
398 * is sent to the host driver
399 */
Per Forlin97868a22011-07-09 17:12:36 -0400400void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200401{
Per Forlin97868a22011-07-09 17:12:36 -0400402 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200403 return;
404
Per Forlin97868a22011-07-09 17:12:36 -0400405 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200406 return;
407
Per Forlin97868a22011-07-09 17:12:36 -0400408 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
409 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200410}
411
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200412/*
413 * If reading, bounce the data from the buffer after the request
414 * has been handled by the host driver
415 */
Per Forlin97868a22011-07-09 17:12:36 -0400416void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200417{
Per Forlin97868a22011-07-09 17:12:36 -0400418 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200419 return;
420
Per Forlin97868a22011-07-09 17:12:36 -0400421 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200422 return;
423
Per Forlin97868a22011-07-09 17:12:36 -0400424 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
425 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200426}