blob: 2a9d62ae0688a0be909c083a87d5432f77019222 [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 {
Per Forlin91fd00b2011-07-01 18:55:33 +020060 struct mmc_queue_req *tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 spin_lock_irq(q->queue_lock);
64 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010065 req = blk_fetch_request(q);
Per Forlincb86e7b2011-07-09 17:12:36 -040066 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 spin_unlock_irq(q->queue_lock);
68
Per Forlin91fd00b2011-07-01 18:55:33 +020069 if (req || mq->mqrq_prev->req) {
70 set_current_state(TASK_RUNNING);
71 mq->issue_fn(mq, req);
72 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010073 if (kthread_should_stop()) {
74 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 up(&mq->thread_sem);
78 schedule();
79 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Per Forlin91fd00b2011-07-01 18:55:33 +020081
82 /* Current request becomes previous request and vice versa. */
83 mq->mqrq_prev->brq.mrq.data = NULL;
84 mq->mqrq_prev->req = NULL;
85 tmp = mq->mqrq_prev;
86 mq->mqrq_prev = mq->mqrq_cur;
87 mq->mqrq_cur = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 up(&mq->thread_sem);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92}
93
94/*
95 * Generic MMC request handler. This is called for any queue on a
96 * particular host. When the host is not busy, we look for a request
97 * on any queue on this host, and attempt to issue it. This may
98 * not be the queue we were asked to process.
99 */
Jens Axboe165125e2007-07-24 09:28:11 +0200100static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100103 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100104
105 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800106 while ((req = blk_fetch_request(q)) != NULL) {
107 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900108 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800109 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100110 return;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Per Forlin91fd00b2011-07-01 18:55:33 +0200113 if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100114 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Per Forlincb86e7b2011-07-09 17:12:36 -0400117struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
118{
119 struct scatterlist *sg;
120
121 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
122 if (!sg)
123 *err = -ENOMEM;
124 else {
125 *err = 0;
126 sg_init_table(sg, sg_len);
127 }
128
129 return sg;
130}
131
Adrian Hunter81306ad2011-06-28 17:16:02 +0300132static void mmc_queue_setup_discard(struct request_queue *q,
133 struct mmc_card *card)
134{
135 unsigned max_discard;
136
137 max_discard = mmc_calc_max_discard(card);
138 if (!max_discard)
139 return;
140
141 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
142 q->limits.max_discard_sectors = max_discard;
143 if (card->erased_byte == 0)
144 q->limits.discard_zeroes_data = 1;
145 q->limits.discard_granularity = card->pref_erase << 9;
146 /* granularity must not be greater than max. discard */
147 if (card->pref_erase > max_discard)
148 q->limits.discard_granularity = 0;
149 if (mmc_can_secure_erase_trim(card))
150 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/**
154 * mmc_init_queue - initialise a queue structure.
155 * @mq: mmc queue
156 * @card: mmc card to attach this queue
157 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300158 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 *
160 * Initialise a MMC card request queue.
161 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300162int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
163 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 struct mmc_host *host = card->host;
166 u64 limit = BLK_BOUNCE_HIGH;
167 int ret;
Per Forlincb86e7b2011-07-09 17:12:36 -0400168 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlind07424b2011-07-01 18:55:31 +0200169 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200171 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
172 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 mq->card = card;
175 mq->queue = blk_init_queue(mmc_request, lock);
176 if (!mq->queue)
177 return -ENOMEM;
178
Per Forlincb86e7b2011-07-09 17:12:36 -0400179 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
Per Forlind07424b2011-07-01 18:55:31 +0200180 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
Per Forlincb86e7b2011-07-09 17:12:36 -0400181 mq->mqrq_cur = mqrq_cur;
Per Forlind07424b2011-07-01 18:55:31 +0200182 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Pierre Ossman98ccf142007-05-12 00:26:16 +0200185 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200186 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Hunter81306ad2011-06-28 17:16:02 +0300187 if (mmc_can_erase(card))
188 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200189
190#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400191 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200192 unsigned int bouncesz;
193
Pierre Ossman98ccf142007-05-12 00:26:16 +0200194 bouncesz = MMC_QUEUE_BOUNCESZ;
195
196 if (bouncesz > host->max_req_size)
197 bouncesz = host->max_req_size;
198 if (bouncesz > host->max_seg_size)
199 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200200 if (bouncesz > (host->max_blk_count * 512))
201 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200202
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200203 if (bouncesz > 512) {
Per Forlincb86e7b2011-07-09 17:12:36 -0400204 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
205 if (!mqrq_cur->bounce_buf) {
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200206 printk(KERN_WARNING "%s: unable to "
Per Forlincb86e7b2011-07-09 17:12:36 -0400207 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200208 mmc_card_name(card));
209 }
Per Forlind07424b2011-07-01 18:55:31 +0200210 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
211 if (!mqrq_prev->bounce_buf) {
212 printk(KERN_WARNING "%s: unable to "
213 "allocate bounce prev buffer\n",
214 mmc_card_name(card));
215 kfree(mqrq_cur->bounce_buf);
216 mqrq_cur->bounce_buf = NULL;
217 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200218 }
219
Per Forlind07424b2011-07-01 18:55:31 +0200220 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200221 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500222 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500223 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200224 blk_queue_max_segment_size(mq->queue, bouncesz);
225
Per Forlincb86e7b2011-07-09 17:12:36 -0400226 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
227 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200228 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200229
Per Forlincb86e7b2011-07-09 17:12:36 -0400230 mqrq_cur->bounce_sg =
231 mmc_alloc_sg(bouncesz / 512, &ret);
232 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200233 goto cleanup_queue;
Per Forlincb86e7b2011-07-09 17:12:36 -0400234
Per Forlind07424b2011-07-01 18:55:31 +0200235 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
236 if (ret)
237 goto cleanup_queue;
238
239 mqrq_prev->bounce_sg =
240 mmc_alloc_sg(bouncesz / 512, &ret);
241 if (ret)
242 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200243 }
244 }
245#endif
246
Per Forlind07424b2011-07-01 18:55:31 +0200247 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200248 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500249 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200250 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400251 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200252 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
253
Per Forlincb86e7b2011-07-09 17:12:36 -0400254 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
255 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200256 goto cleanup_queue;
Per Forlincb86e7b2011-07-09 17:12:36 -0400257
Per Forlind07424b2011-07-01 18:55:31 +0200258
259 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
260 if (ret)
261 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Thomas Gleixner632cf922010-09-14 07:12:35 -0400264 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Adrian Hunterd09408a2011-06-23 13:40:28 +0300266 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
267 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400268
Christoph Hellwig87598a22006-11-13 20:23:52 +0100269 if (IS_ERR(mq->thread)) {
270 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200271 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
Christoph Hellwig87598a22006-11-13 20:23:52 +0100274 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200275 free_bounce_sg:
Per Forlincb86e7b2011-07-09 17:12:36 -0400276 kfree(mqrq_cur->bounce_sg);
277 mqrq_cur->bounce_sg = NULL;
Per Forlind07424b2011-07-01 18:55:31 +0200278 kfree(mqrq_prev->bounce_sg);
279 mqrq_prev->bounce_sg = NULL;
Per Forlincb86e7b2011-07-09 17:12:36 -0400280
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200281 cleanup_queue:
Per Forlincb86e7b2011-07-09 17:12:36 -0400282 kfree(mqrq_cur->sg);
283 mqrq_cur->sg = NULL;
284 kfree(mqrq_cur->bounce_buf);
285 mqrq_cur->bounce_buf = NULL;
286
Per Forlind07424b2011-07-01 18:55:31 +0200287 kfree(mqrq_prev->sg);
288 mqrq_prev->sg = NULL;
289 kfree(mqrq_prev->bounce_buf);
290 mqrq_prev->bounce_buf = NULL;
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 return ret;
294}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295
296void mmc_cleanup_queue(struct mmc_queue *mq)
297{
Jens Axboe165125e2007-07-24 09:28:11 +0200298 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100299 unsigned long flags;
Per Forlincb86e7b2011-07-09 17:12:36 -0400300 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlind07424b2011-07-01 18:55:31 +0200301 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100302
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200303 /* Make sure the queue isn't suspended, as that will deadlock */
304 mmc_queue_resume(mq);
305
Pierre Ossman89b4e132006-11-14 22:08:16 +0100306 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100307 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800309 /* Empty the queue */
310 spin_lock_irqsave(q->queue_lock, flags);
311 q->queuedata = NULL;
312 blk_start_queue(q);
313 spin_unlock_irqrestore(q->queue_lock, flags);
314
Per Forlincb86e7b2011-07-09 17:12:36 -0400315 kfree(mqrq_cur->bounce_sg);
316 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200317
Per Forlincb86e7b2011-07-09 17:12:36 -0400318 kfree(mqrq_cur->sg);
319 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Per Forlincb86e7b2011-07-09 17:12:36 -0400321 kfree(mqrq_cur->bounce_buf);
322 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200323
Per Forlind07424b2011-07-01 18:55:31 +0200324 kfree(mqrq_prev->bounce_sg);
325 mqrq_prev->bounce_sg = NULL;
326
327 kfree(mqrq_prev->sg);
328 mqrq_prev->sg = NULL;
329
330 kfree(mqrq_prev->bounce_buf);
331 mqrq_prev->bounce_buf = NULL;
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 mq->card = NULL;
334}
335EXPORT_SYMBOL(mmc_cleanup_queue);
336
337/**
338 * mmc_queue_suspend - suspend a MMC request queue
339 * @mq: MMC queue to suspend
340 *
341 * Stop the block request queue, and wait for our thread to
342 * complete any outstanding requests. This ensures that we
343 * won't suspend while a request is being processed.
344 */
345void mmc_queue_suspend(struct mmc_queue *mq)
346{
Jens Axboe165125e2007-07-24 09:28:11 +0200347 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 unsigned long flags;
349
350 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
351 mq->flags |= MMC_QUEUE_SUSPENDED;
352
353 spin_lock_irqsave(q->queue_lock, flags);
354 blk_stop_queue(q);
355 spin_unlock_irqrestore(q->queue_lock, flags);
356
357 down(&mq->thread_sem);
358 }
359}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
361/**
362 * mmc_queue_resume - resume a previously suspended MMC request queue
363 * @mq: MMC queue to resume
364 */
365void mmc_queue_resume(struct mmc_queue *mq)
366{
Jens Axboe165125e2007-07-24 09:28:11 +0200367 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 unsigned long flags;
369
370 if (mq->flags & MMC_QUEUE_SUSPENDED) {
371 mq->flags &= ~MMC_QUEUE_SUSPENDED;
372
373 up(&mq->thread_sem);
374
375 spin_lock_irqsave(q->queue_lock, flags);
376 blk_start_queue(q);
377 spin_unlock_irqrestore(q->queue_lock, flags);
378 }
379}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100380
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200381/*
382 * Prepare the sg list(s) to be handed of to the host driver
383 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400384unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200385{
386 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200387 size_t buflen;
388 struct scatterlist *sg;
389 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200390
Per Forlincb86e7b2011-07-09 17:12:36 -0400391 if (!mqrq->bounce_buf)
392 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200393
Per Forlincb86e7b2011-07-09 17:12:36 -0400394 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200395
Per Forlincb86e7b2011-07-09 17:12:36 -0400396 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200397
Per Forlincb86e7b2011-07-09 17:12:36 -0400398 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200399
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200400 buflen = 0;
Per Forlincb86e7b2011-07-09 17:12:36 -0400401 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200402 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200403
Per Forlincb86e7b2011-07-09 17:12:36 -0400404 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200405
406 return 1;
407}
408
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200409/*
410 * If writing, bounce the data to the buffer before the request
411 * is sent to the host driver
412 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400413void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200414{
Per Forlincb86e7b2011-07-09 17:12:36 -0400415 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200416 return;
417
Per Forlincb86e7b2011-07-09 17:12:36 -0400418 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200419 return;
420
Per Forlincb86e7b2011-07-09 17:12:36 -0400421 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
422 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200423}
424
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200425/*
426 * If reading, bounce the data from the buffer after the request
427 * has been handled by the host driver
428 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400429void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200430{
Per Forlincb86e7b2011-07-09 17:12:36 -0400431 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200432 return;
433
Per Forlincb86e7b2011-07-09 17:12:36 -0400434 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200435 return;
436
Per Forlincb86e7b2011-07-09 17:12:36 -0400437 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
438 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200439}