blob: 2d2576cc07330c30148927aeafa8eda127f5f801 [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{
Sujit Reddy Thummacfefa142011-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 Hunterbd788c92010-08-11 14:17:47 -070037 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020039 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 }
41
Sujit Reddy Thummacfefa142011-12-08 14:05:50 +053042 if (mq && mmc_card_removed(mq->card))
43 return BLKPREP_KILL;
44
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020045 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020047 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50static int mmc_queue_thread(void *d)
51{
52 struct mmc_queue *mq = d;
53 struct request_queue *q = mq->queue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054 struct request *req;
55
Rafael J. Wysocki83144182007-07-17 04:03:35 -070056 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 spin_lock_irq(q->queue_lock);
63 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010064 req = blk_fetch_request(q);
Per Forlincb86e7b2011-07-09 17:12:36 -040065 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 spin_unlock_irq(q->queue_lock);
67
68 if (!req) {
Vitaly Wool7b30d282006-12-07 20:08:02 +010069 if (kthread_should_stop()) {
70 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 up(&mq->thread_sem);
74 schedule();
75 down(&mq->thread_sem);
76 continue;
77 }
78 set_current_state(TASK_RUNNING);
Per Forlincb86e7b2011-07-09 17:12:36 -040079 mq->issue_fn(mq, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 up(&mq->thread_sem);
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 return 0;
84}
85
86/*
87 * Generic MMC request handler. This is called for any queue on a
88 * particular host. When the host is not busy, we look for a request
89 * on any queue on this host, and attempt to issue it. This may
90 * not be the queue we were asked to process.
91 */
Jens Axboe165125e2007-07-24 09:28:11 +020092static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +010095 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +010096
97 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -080098 while ((req = blk_fetch_request(q)) != NULL) {
99 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900100 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800101 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100102 return;
103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Per Forlincb86e7b2011-07-09 17:12:36 -0400105 if (!mq->mqrq_cur->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100106 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Per Forlincb86e7b2011-07-09 17:12:36 -0400109struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
110{
111 struct scatterlist *sg;
112
113 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
114 if (!sg)
115 *err = -ENOMEM;
116 else {
117 *err = 0;
118 sg_init_table(sg, sg_len);
119 }
120
121 return sg;
122}
123
Adrian Hunter81306ad2011-06-28 17:16:02 +0300124static void mmc_queue_setup_discard(struct request_queue *q,
125 struct mmc_card *card)
126{
127 unsigned max_discard;
128
129 max_discard = mmc_calc_max_discard(card);
130 if (!max_discard)
131 return;
132
133 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
134 q->limits.max_discard_sectors = max_discard;
135 if (card->erased_byte == 0)
136 q->limits.discard_zeroes_data = 1;
137 q->limits.discard_granularity = card->pref_erase << 9;
138 /* granularity must not be greater than max. discard */
139 if (card->pref_erase > max_discard)
140 q->limits.discard_granularity = 0;
141 if (mmc_can_secure_erase_trim(card))
142 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/**
146 * mmc_init_queue - initialise a queue structure.
147 * @mq: mmc queue
148 * @card: mmc card to attach this queue
149 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300150 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 *
152 * Initialise a MMC card request queue.
153 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300154int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
155 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct mmc_host *host = card->host;
158 u64 limit = BLK_BOUNCE_HIGH;
159 int ret;
Per Forlincb86e7b2011-07-09 17:12:36 -0400160 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlind07424b2011-07-01 18:55:31 +0200161 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200163 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
164 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 mq->card = card;
167 mq->queue = blk_init_queue(mmc_request, lock);
168 if (!mq->queue)
169 return -ENOMEM;
170
Per Forlincb86e7b2011-07-09 17:12:36 -0400171 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
Per Forlind07424b2011-07-01 18:55:31 +0200172 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
Per Forlincb86e7b2011-07-09 17:12:36 -0400173 mq->mqrq_cur = mqrq_cur;
Per Forlind07424b2011-07-01 18:55:31 +0200174 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Pierre Ossman98ccf142007-05-12 00:26:16 +0200177 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200178 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Hunter81306ad2011-06-28 17:16:02 +0300179 if (mmc_can_erase(card))
180 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200181
182#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400183 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200184 unsigned int bouncesz;
185
Pierre Ossman98ccf142007-05-12 00:26:16 +0200186 bouncesz = MMC_QUEUE_BOUNCESZ;
187
188 if (bouncesz > host->max_req_size)
189 bouncesz = host->max_req_size;
190 if (bouncesz > host->max_seg_size)
191 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200192 if (bouncesz > (host->max_blk_count * 512))
193 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200194
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200195 if (bouncesz > 512) {
Per Forlincb86e7b2011-07-09 17:12:36 -0400196 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
197 if (!mqrq_cur->bounce_buf) {
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200198 printk(KERN_WARNING "%s: unable to "
Per Forlincb86e7b2011-07-09 17:12:36 -0400199 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200200 mmc_card_name(card));
201 }
Per Forlind07424b2011-07-01 18:55:31 +0200202 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
203 if (!mqrq_prev->bounce_buf) {
204 printk(KERN_WARNING "%s: unable to "
205 "allocate bounce prev buffer\n",
206 mmc_card_name(card));
207 kfree(mqrq_cur->bounce_buf);
208 mqrq_cur->bounce_buf = NULL;
209 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200210 }
211
Per Forlind07424b2011-07-01 18:55:31 +0200212 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200213 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500214 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500215 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200216 blk_queue_max_segment_size(mq->queue, bouncesz);
217
Per Forlincb86e7b2011-07-09 17:12:36 -0400218 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
219 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200220 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200221
Per Forlincb86e7b2011-07-09 17:12:36 -0400222 mqrq_cur->bounce_sg =
223 mmc_alloc_sg(bouncesz / 512, &ret);
224 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200225 goto cleanup_queue;
Per Forlincb86e7b2011-07-09 17:12:36 -0400226
Per Forlind07424b2011-07-01 18:55:31 +0200227 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
228 if (ret)
229 goto cleanup_queue;
230
231 mqrq_prev->bounce_sg =
232 mmc_alloc_sg(bouncesz / 512, &ret);
233 if (ret)
234 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200235 }
236 }
237#endif
238
Per Forlind07424b2011-07-01 18:55:31 +0200239 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200240 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500241 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200242 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400243 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200244 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
245
Per Forlincb86e7b2011-07-09 17:12:36 -0400246 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
247 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200248 goto cleanup_queue;
Per Forlincb86e7b2011-07-09 17:12:36 -0400249
Per Forlind07424b2011-07-01 18:55:31 +0200250
251 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
252 if (ret)
253 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
Thomas Gleixner632cf922010-09-14 07:12:35 -0400256 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Adrian Hunterd09408a2011-06-23 13:40:28 +0300258 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
259 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400260
Christoph Hellwig87598a22006-11-13 20:23:52 +0100261 if (IS_ERR(mq->thread)) {
262 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200263 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 }
265
Christoph Hellwig87598a22006-11-13 20:23:52 +0100266 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200267 free_bounce_sg:
Per Forlincb86e7b2011-07-09 17:12:36 -0400268 kfree(mqrq_cur->bounce_sg);
269 mqrq_cur->bounce_sg = NULL;
Per Forlind07424b2011-07-01 18:55:31 +0200270 kfree(mqrq_prev->bounce_sg);
271 mqrq_prev->bounce_sg = NULL;
Per Forlincb86e7b2011-07-09 17:12:36 -0400272
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200273 cleanup_queue:
Per Forlincb86e7b2011-07-09 17:12:36 -0400274 kfree(mqrq_cur->sg);
275 mqrq_cur->sg = NULL;
276 kfree(mqrq_cur->bounce_buf);
277 mqrq_cur->bounce_buf = NULL;
278
Per Forlind07424b2011-07-01 18:55:31 +0200279 kfree(mqrq_prev->sg);
280 mqrq_prev->sg = NULL;
281 kfree(mqrq_prev->bounce_buf);
282 mqrq_prev->bounce_buf = NULL;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return ret;
286}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288void mmc_cleanup_queue(struct mmc_queue *mq)
289{
Jens Axboe165125e2007-07-24 09:28:11 +0200290 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100291 unsigned long flags;
Per Forlincb86e7b2011-07-09 17:12:36 -0400292 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlind07424b2011-07-01 18:55:31 +0200293 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100294
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200295 /* Make sure the queue isn't suspended, as that will deadlock */
296 mmc_queue_resume(mq);
297
Pierre Ossman89b4e132006-11-14 22:08:16 +0100298 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100299 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800301 /* Empty the queue */
302 spin_lock_irqsave(q->queue_lock, flags);
303 q->queuedata = NULL;
304 blk_start_queue(q);
305 spin_unlock_irqrestore(q->queue_lock, flags);
306
Per Forlincb86e7b2011-07-09 17:12:36 -0400307 kfree(mqrq_cur->bounce_sg);
308 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200309
Per Forlincb86e7b2011-07-09 17:12:36 -0400310 kfree(mqrq_cur->sg);
311 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Per Forlincb86e7b2011-07-09 17:12:36 -0400313 kfree(mqrq_cur->bounce_buf);
314 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200315
Per Forlind07424b2011-07-01 18:55:31 +0200316 kfree(mqrq_prev->bounce_sg);
317 mqrq_prev->bounce_sg = NULL;
318
319 kfree(mqrq_prev->sg);
320 mqrq_prev->sg = NULL;
321
322 kfree(mqrq_prev->bounce_buf);
323 mqrq_prev->bounce_buf = NULL;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 mq->card = NULL;
326}
327EXPORT_SYMBOL(mmc_cleanup_queue);
328
329/**
330 * mmc_queue_suspend - suspend a MMC request queue
331 * @mq: MMC queue to suspend
332 *
333 * Stop the block request queue, and wait for our thread to
334 * complete any outstanding requests. This ensures that we
335 * won't suspend while a request is being processed.
336 */
337void mmc_queue_suspend(struct mmc_queue *mq)
338{
Jens Axboe165125e2007-07-24 09:28:11 +0200339 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 unsigned long flags;
341
342 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
343 mq->flags |= MMC_QUEUE_SUSPENDED;
344
345 spin_lock_irqsave(q->queue_lock, flags);
346 blk_stop_queue(q);
347 spin_unlock_irqrestore(q->queue_lock, flags);
348
349 down(&mq->thread_sem);
350 }
351}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353/**
354 * mmc_queue_resume - resume a previously suspended MMC request queue
355 * @mq: MMC queue to resume
356 */
357void mmc_queue_resume(struct mmc_queue *mq)
358{
Jens Axboe165125e2007-07-24 09:28:11 +0200359 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 unsigned long flags;
361
362 if (mq->flags & MMC_QUEUE_SUSPENDED) {
363 mq->flags &= ~MMC_QUEUE_SUSPENDED;
364
365 up(&mq->thread_sem);
366
367 spin_lock_irqsave(q->queue_lock, flags);
368 blk_start_queue(q);
369 spin_unlock_irqrestore(q->queue_lock, flags);
370 }
371}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100372
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200373/*
374 * Prepare the sg list(s) to be handed of to the host driver
375 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400376unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200377{
378 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200379 size_t buflen;
380 struct scatterlist *sg;
381 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200382
Per Forlincb86e7b2011-07-09 17:12:36 -0400383 if (!mqrq->bounce_buf)
384 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200385
Per Forlincb86e7b2011-07-09 17:12:36 -0400386 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200387
Per Forlincb86e7b2011-07-09 17:12:36 -0400388 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200389
Per Forlincb86e7b2011-07-09 17:12:36 -0400390 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200391
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200392 buflen = 0;
Per Forlincb86e7b2011-07-09 17:12:36 -0400393 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200394 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200395
Per Forlincb86e7b2011-07-09 17:12:36 -0400396 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200397
398 return 1;
399}
400
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200401/*
402 * If writing, bounce the data to the buffer before the request
403 * is sent to the host driver
404 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400405void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200406{
Per Forlincb86e7b2011-07-09 17:12:36 -0400407 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200408 return;
409
Per Forlincb86e7b2011-07-09 17:12:36 -0400410 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200411 return;
412
Per Forlincb86e7b2011-07-09 17:12:36 -0400413 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
414 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200415}
416
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200417/*
418 * If reading, bounce the data from the buffer after the request
419 * has been handled by the host driver
420 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400421void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200422{
Per Forlincb86e7b2011-07-09 17:12:36 -0400423 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200424 return;
425
Per Forlincb86e7b2011-07-09 17:12:36 -0400426 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200427 return;
428
Per Forlincb86e7b2011-07-09 17:12:36 -0400429 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
430 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200431}