blob: 74c663b1c0a7418c53ef0d7fa4ae27e2499d1ff7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 2003 Russell King, All Rights Reserved.
Pierre Ossman98ac2162006-12-23 20:03:02 +01003 * Copyright 2006-2007 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/blkdev.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070013#include <linux/freezer.h>
Christoph Hellwig87598a22006-11-13 20:23:52 +010014#include <linux/kthread.h>
Jens Axboe45711f12007-10-22 21:19:53 +020015#include <linux/scatterlist.h>
Santosh Shilimkar8e0cb8a2013-07-29 14:20:15 +010016#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/mmc/card.h>
19#include <linux/mmc/host.h>
Linus Walleij29eb7bd2016-09-20 11:34:38 +020020
Pierre Ossman98ac2162006-12-23 20:03:02 +010021#include "queue.h"
Linus Walleij29eb7bd2016-09-20 11:34:38 +020022#include "block.h"
Ulf Hansson55244c52017-01-13 14:14:08 +010023#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010024#include "card.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Pierre Ossman98ccf142007-05-12 00:26:16 +020026#define MMC_QUEUE_BOUNCESZ 65536
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020029 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 */
31static int mmc_prep_request(struct request_queue *q, struct request *req)
32{
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053033 struct mmc_queue *mq = q->queuedata;
34
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +080035 if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053036 return BLKPREP_KILL;
37
Christoph Hellwige8064022016-10-20 15:12:13 +020038 req->rq_flags |= RQF_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020040 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43static int mmc_queue_thread(void *d)
44{
45 struct mmc_queue *mq = d;
46 struct request_queue *q = mq->queue;
Adrian Huntere0097cf2016-11-29 12:09:10 +020047 struct mmc_context_info *cntx = &mq->card->host->context_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Rafael J. Wysocki83144182007-07-17 04:03:35 -070049 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 do {
Adrian Huntercdf8a6f2017-03-13 14:36:35 +020053 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 spin_lock_irq(q->queue_lock);
56 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010057 req = blk_fetch_request(q);
Adrian Huntere0097cf2016-11-29 12:09:10 +020058 mq->asleep = false;
59 cntx->is_waiting_last_req = false;
60 cntx->is_new_req = false;
61 if (!req) {
62 /*
63 * Dispatch queue is empty so set flags for
64 * mmc_request_fn() to wake us up.
65 */
Adrian Huntercdf8a6f2017-03-13 14:36:35 +020066 if (mq->qcnt)
Adrian Huntere0097cf2016-11-29 12:09:10 +020067 cntx->is_waiting_last_req = true;
68 else
69 mq->asleep = true;
70 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 spin_unlock_irq(q->queue_lock);
72
Adrian Huntercdf8a6f2017-03-13 14:36:35 +020073 if (req || mq->qcnt) {
Per Forlinee8a43a2011-07-01 18:55:33 +020074 set_current_state(TASK_RUNNING);
Linus Walleij29eb7bd2016-09-20 11:34:38 +020075 mmc_blk_issue_rq(mq, req);
Rabin Vincenta8c27c02015-06-14 19:26:11 +020076 cond_resched();
Per Forlinee8a43a2011-07-01 18:55:33 +020077 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010078 if (kthread_should_stop()) {
79 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010081 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 up(&mq->thread_sem);
83 schedule();
84 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 up(&mq->thread_sem);
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 return 0;
90}
91
92/*
93 * Generic MMC request handler. This is called for any queue on a
94 * particular host. When the host is not busy, we look for a request
95 * on any queue on this host, and attempt to issue it. This may
96 * not be the queue we were asked to process.
97 */
Venkatraman S1b50f5f2012-04-13 17:54:11 +053098static void mmc_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
100 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100101 struct request *req;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500102 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100103
104 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800105 while ((req = blk_fetch_request(q)) != NULL) {
Christoph Hellwige8064022016-10-20 15:12:13 +0200106 req->rq_flags |= RQF_QUIET;
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200107 __blk_end_request_all(req, BLK_STS_IOERR);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800108 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100109 return;
110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500112 cntx = &mq->card->host->context_info;
Adrian Huntere0097cf2016-11-29 12:09:10 +0200113
114 if (cntx->is_waiting_last_req) {
115 cntx->is_new_req = true;
116 wake_up_interruptible(&cntx->wait);
117 }
118
119 if (mq->asleep)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100120 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Linus Walleij304419d2017-05-18 11:29:32 +0200123static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
Per Forlin97868a22011-07-09 17:12:36 -0400124{
125 struct scatterlist *sg;
126
Linus Walleij304419d2017-05-18 11:29:32 +0200127 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
Adrian Hunter7b410d02017-03-13 14:36:36 +0200128 if (sg)
Per Forlin97868a22011-07-09 17:12:36 -0400129 sg_init_table(sg, sg_len);
Per Forlin97868a22011-07-09 17:12:36 -0400130
131 return sg;
132}
133
Adrian Huntere056a1b2011-06-28 17:16:02 +0300134static void mmc_queue_setup_discard(struct request_queue *q,
135 struct mmc_card *card)
136{
137 unsigned max_discard;
138
139 max_discard = mmc_calc_max_discard(card);
140 if (!max_discard)
141 return;
142
143 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600144 blk_queue_max_discard_sectors(q, max_discard);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300145 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;
Maya Erez775a9362013-04-18 15:41:55 +0300149 if (mmc_can_secure_erase_trim(card))
Christoph Hellwig288dab82016-06-09 16:00:36 +0200150 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300151}
152
Adrian Hunter7b410d02017-03-13 14:36:36 +0200153static unsigned int mmc_queue_calc_bouncesz(struct mmc_host *host)
154{
155 unsigned int bouncesz = MMC_QUEUE_BOUNCESZ;
156
Linus Walleijc3dccb72017-05-18 11:29:31 +0200157 if (host->max_segs != 1 || (host->caps & MMC_CAP_NO_BOUNCE_BUFF))
Adrian Hunter7b410d02017-03-13 14:36:36 +0200158 return 0;
159
160 if (bouncesz > host->max_req_size)
161 bouncesz = host->max_req_size;
162 if (bouncesz > host->max_seg_size)
163 bouncesz = host->max_seg_size;
164 if (bouncesz > host->max_blk_count * 512)
165 bouncesz = host->max_blk_count * 512;
166
167 if (bouncesz <= 512)
168 return 0;
169
170 return bouncesz;
171}
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200172
Linus Walleij304419d2017-05-18 11:29:32 +0200173/**
174 * mmc_init_request() - initialize the MMC-specific per-request data
175 * @q: the request queue
176 * @req: the request
177 * @gfp: memory allocation policy
178 */
179static int mmc_init_request(struct request_queue *q, struct request *req,
180 gfp_t gfp)
Adrian Hunterf2b8b522016-11-29 12:09:12 +0200181{
Linus Walleij304419d2017-05-18 11:29:32 +0200182 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
183 struct mmc_queue *mq = q->queuedata;
184 struct mmc_card *card = mq->card;
185 struct mmc_host *host = card->host;
Adrian Hunterc8539822016-11-29 12:09:11 +0200186
Linus Walleij304419d2017-05-18 11:29:32 +0200187 if (card->bouncesz) {
188 mq_rq->bounce_buf = kmalloc(card->bouncesz, gfp);
189 if (!mq_rq->bounce_buf)
190 return -ENOMEM;
191 if (card->bouncesz > 512) {
192 mq_rq->sg = mmc_alloc_sg(1, gfp);
193 if (!mq_rq->sg)
194 return -ENOMEM;
195 mq_rq->bounce_sg = mmc_alloc_sg(card->bouncesz / 512,
196 gfp);
197 if (!mq_rq->bounce_sg)
198 return -ENOMEM;
199 }
200 } else {
201 mq_rq->bounce_buf = NULL;
202 mq_rq->bounce_sg = NULL;
203 mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
204 if (!mq_rq->sg)
Adrian Hunter7b410d02017-03-13 14:36:36 +0200205 return -ENOMEM;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200206 }
Adrian Hunter64e29e422016-11-29 12:09:13 +0200207
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200208 return 0;
209}
Adrian Hunter64e29e422016-11-29 12:09:13 +0200210
Linus Walleij304419d2017-05-18 11:29:32 +0200211static void mmc_exit_request(struct request_queue *q, struct request *req)
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200212{
Linus Walleij304419d2017-05-18 11:29:32 +0200213 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
Adrian Hunter64e29e422016-11-29 12:09:13 +0200214
Linus Walleij304419d2017-05-18 11:29:32 +0200215 /* It is OK to kfree(NULL) so this will be smooth */
216 kfree(mq_rq->bounce_sg);
217 mq_rq->bounce_sg = NULL;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200218
Linus Walleij304419d2017-05-18 11:29:32 +0200219 kfree(mq_rq->bounce_buf);
220 mq_rq->bounce_buf = NULL;
Adrian Hunter7b410d02017-03-13 14:36:36 +0200221
Linus Walleij304419d2017-05-18 11:29:32 +0200222 kfree(mq_rq->sg);
223 mq_rq->sg = NULL;
Adrian Hunterc09949c2016-11-29 12:09:14 +0200224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/**
227 * mmc_init_queue - initialise a queue structure.
228 * @mq: mmc queue
229 * @card: mmc card to attach this queue
230 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300231 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 *
233 * Initialise a MMC card request queue.
234 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300235int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
236 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct mmc_host *host = card->host;
239 u64 limit = BLK_BOUNCE_HIGH;
Adrian Hunterc5bda0c2016-11-29 12:09:15 +0200240 int ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200242 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000243 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
Adrian Hunter01f5bbd2017-09-07 10:40:35 +0300245 /*
246 * mmc_init_request() depends on card->bouncesz so it must be calculated
247 * before blk_init_allocated_queue() starts allocating requests.
248 */
249 card->bouncesz = mmc_queue_calc_bouncesz(host);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 mq->card = card;
Linus Walleij304419d2017-05-18 11:29:32 +0200252 mq->queue = blk_alloc_queue(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 if (!mq->queue)
254 return -ENOMEM;
Linus Walleij304419d2017-05-18 11:29:32 +0200255 mq->queue->queue_lock = lock;
256 mq->queue->request_fn = mmc_request_fn;
257 mq->queue->init_rq_fn = mmc_init_request;
258 mq->queue->exit_rq_fn = mmc_exit_request;
259 mq->queue->cmd_size = sizeof(struct mmc_queue_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 mq->queue->queuedata = mq;
Linus Walleij304419d2017-05-18 11:29:32 +0200261 mq->qcnt = 0;
262 ret = blk_init_allocated_queue(mq->queue);
263 if (ret) {
264 blk_cleanup_queue(mq->queue);
265 return ret;
266 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Pierre Ossman98ccf142007-05-12 00:26:16 +0200268 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200269 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600270 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300271 if (mmc_can_erase(card))
272 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200273
Adrian Hunter7b410d02017-03-13 14:36:36 +0200274 if (card->bouncesz) {
Adrian Hunter7b410d02017-03-13 14:36:36 +0200275 blk_queue_max_hw_sectors(mq->queue, card->bouncesz / 512);
276 blk_queue_max_segments(mq->queue, card->bouncesz / 512);
277 blk_queue_max_segment_size(mq->queue, card->bouncesz);
278 } else {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200279 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500280 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200281 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400282 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200283 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
285
Thomas Gleixner632cf922010-09-14 07:12:35 -0400286 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Adrian Hunterd09408a2011-06-23 13:40:28 +0300288 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
289 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400290
Christoph Hellwig87598a22006-11-13 20:23:52 +0100291 if (IS_ERR(mq->thread)) {
292 ret = PTR_ERR(mq->thread);
Adrian Hunterc09949c2016-11-29 12:09:14 +0200293 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Christoph Hellwig87598a22006-11-13 20:23:52 +0100296 return 0;
Per Forlin97868a22011-07-09 17:12:36 -0400297
Adrian Hunter7b410d02017-03-13 14:36:36 +0200298cleanup_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return ret;
301}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303void mmc_cleanup_queue(struct mmc_queue *mq)
304{
Jens Axboe165125e2007-07-24 09:28:11 +0200305 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100306 unsigned long flags;
307
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200308 /* Make sure the queue isn't suspended, as that will deadlock */
309 mmc_queue_resume(mq);
310
Pierre Ossman89b4e132006-11-14 22:08:16 +0100311 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100312 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800314 /* Empty the queue */
315 spin_lock_irqsave(q->queue_lock, flags);
316 q->queuedata = NULL;
317 blk_start_queue(q);
318 spin_unlock_irqrestore(q->queue_lock, flags);
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
Linus Walleij9491be52017-02-01 13:47:56 +0100337 if (!mq->suspended) {
338 mq->suspended |= true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
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
Linus Walleij9491be52017-02-01 13:47:56 +0100357 if (mq->suspended) {
358 mq->suspended = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
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;
Linus Walleij67e69d52017-05-19 15:37:27 +0200376 struct request *req = mmc_queue_req_to_req(mqrq);
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200377 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200378
Linus Walleij03d640a2016-11-25 10:35:00 +0100379 if (!mqrq->bounce_buf)
Linus Walleij67e69d52017-05-19 15:37:27 +0200380 return blk_rq_map_sg(mq->queue, req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200381
Linus Walleij67e69d52017-05-19 15:37:27 +0200382 sg_len = blk_rq_map_sg(mq->queue, req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200383
Per Forlin97868a22011-07-09 17:12:36 -0400384 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200385
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200386 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400387 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200388 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200389
Per Forlin97868a22011-07-09 17:12:36 -0400390 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200391
392 return 1;
393}
394
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200395/*
396 * If writing, bounce the data to the buffer before the request
397 * is sent to the host driver
398 */
Per Forlin97868a22011-07-09 17:12:36 -0400399void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200400{
Per Forlin97868a22011-07-09 17:12:36 -0400401 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200402 return;
403
Linus Walleij67e69d52017-05-19 15:37:27 +0200404 if (rq_data_dir(mmc_queue_req_to_req(mqrq)) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200405 return;
406
Per Forlin97868a22011-07-09 17:12:36 -0400407 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
408 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200409}
410
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200411/*
412 * If reading, bounce the data from the buffer after the request
413 * has been handled by the host driver
414 */
Per Forlin97868a22011-07-09 17:12:36 -0400415void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200416{
Per Forlin97868a22011-07-09 17:12:36 -0400417 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200418 return;
419
Linus Walleij67e69d52017-05-19 15:37:27 +0200420 if (rq_data_dir(mmc_queue_req_to_req(mqrq)) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200421 return;
422
Per Forlin97868a22011-07-09 17:12:36 -0400423 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
424 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200425}