blob: 5e0971016ac5fb0043f8bda840eebce4ab68310a [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Seungwon Jeon369d3212012-12-26 10:40:17 +090026#define MMC_REQ_SPECIAL_MASK (REQ_DISCARD | REQ_FLUSH)
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
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020035 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070036 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020037 */
Adrian Hunterbd788c92010-08-11 14:17:47 -070038 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020040 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 }
42
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053043 if (mq && mmc_card_removed(mq->card))
44 return BLKPREP_KILL;
45
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020046 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020048 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51static int mmc_queue_thread(void *d)
52{
53 struct mmc_queue *mq = d;
54 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
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 {
60 struct request *req = NULL;
Per Forlinee8a43a2011-07-01 18:55:33 +020061 struct mmc_queue_req *tmp;
Seungwon Jeon369d3212012-12-26 10:40:17 +090062 unsigned int cmd_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 spin_lock_irq(q->queue_lock);
65 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010066 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040067 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 spin_unlock_irq(q->queue_lock);
69
Per Forlinee8a43a2011-07-01 18:55:33 +020070 if (req || mq->mqrq_prev->req) {
71 set_current_state(TASK_RUNNING);
Seungwon Jeon369d3212012-12-26 10:40:17 +090072 cmd_flags = req ? req->cmd_flags : 0;
Per Forlinee8a43a2011-07-01 18:55:33 +020073 mq->issue_fn(mq, req);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -050074 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
75 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
76 continue; /* fetch again */
77 }
Seungwon Jeon45c5a912012-09-28 19:12:53 +090078
79 /*
80 * Current request becomes previous request
81 * and vice versa.
Seungwon Jeon369d3212012-12-26 10:40:17 +090082 * In case of special requests, current request
83 * has been finished. Do not assign it to previous
84 * request.
Seungwon Jeon45c5a912012-09-28 19:12:53 +090085 */
Seungwon Jeon369d3212012-12-26 10:40:17 +090086 if (cmd_flags & MMC_REQ_SPECIAL_MASK)
87 mq->mqrq_cur->req = NULL;
88
Seungwon Jeon45c5a912012-09-28 19:12:53 +090089 mq->mqrq_prev->brq.mrq.data = NULL;
90 mq->mqrq_prev->req = NULL;
91 tmp = mq->mqrq_prev;
92 mq->mqrq_prev = mq->mqrq_cur;
93 mq->mqrq_cur = tmp;
Per Forlinee8a43a2011-07-01 18:55:33 +020094 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010095 if (kthread_should_stop()) {
96 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 up(&mq->thread_sem);
100 schedule();
101 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 up(&mq->thread_sem);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
107}
108
109/*
110 * Generic MMC request handler. This is called for any queue on a
111 * particular host. When the host is not busy, we look for a request
112 * on any queue on this host, and attempt to issue it. This may
113 * not be the queue we were asked to process.
114 */
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530115static void mmc_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100118 struct request *req;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500119 unsigned long flags;
120 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100121
122 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800123 while ((req = blk_fetch_request(q)) != NULL) {
124 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900125 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800126 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100127 return;
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500130 cntx = &mq->card->host->context_info;
131 if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
132 /*
133 * New MMC request arrived when MMC thread may be
134 * blocked on the previous request to be complete
135 * with no current request fetched
136 */
137 spin_lock_irqsave(&cntx->lock, flags);
138 if (cntx->is_waiting_last_req) {
139 cntx->is_new_req = true;
140 wake_up_interruptible(&cntx->wait);
141 }
142 spin_unlock_irqrestore(&cntx->lock, flags);
143 } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100144 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Venkatraman S7513cd72011-08-23 21:16:02 +0530147static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400148{
149 struct scatterlist *sg;
150
151 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
152 if (!sg)
153 *err = -ENOMEM;
154 else {
155 *err = 0;
156 sg_init_table(sg, sg_len);
157 }
158
159 return sg;
160}
161
Adrian Huntere056a1b2011-06-28 17:16:02 +0300162static void mmc_queue_setup_discard(struct request_queue *q,
163 struct mmc_card *card)
164{
165 unsigned max_discard;
166
167 max_discard = mmc_calc_max_discard(card);
168 if (!max_discard)
169 return;
170
171 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
172 q->limits.max_discard_sectors = max_discard;
Adrian Hunter7194efb2012-04-05 14:45:47 +0300173 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300174 q->limits.discard_zeroes_data = 1;
175 q->limits.discard_granularity = card->pref_erase << 9;
176 /* granularity must not be greater than max. discard */
177 if (card->pref_erase > max_discard)
178 q->limits.discard_granularity = 0;
Kyungmin Parkd9ddd622011-10-14 14:15:48 +0900179 if (mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300180 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
181}
182
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183/**
184 * mmc_init_queue - initialise a queue structure.
185 * @mq: mmc queue
186 * @card: mmc card to attach this queue
187 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300188 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
190 * Initialise a MMC card request queue.
191 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300192int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
193 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct mmc_host *host = card->host;
196 u64 limit = BLK_BOUNCE_HIGH;
197 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400198 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200199 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200201 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
202 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530205 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (!mq->queue)
207 return -ENOMEM;
208
Per Forlin97868a22011-07-09 17:12:36 -0400209 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200210 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Pierre Ossman98ccf142007-05-12 00:26:16 +0200213 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200214 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300215 if (mmc_can_erase(card))
216 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200217
218#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400219 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200220 unsigned int bouncesz;
221
Pierre Ossman98ccf142007-05-12 00:26:16 +0200222 bouncesz = MMC_QUEUE_BOUNCESZ;
223
224 if (bouncesz > host->max_req_size)
225 bouncesz = host->max_req_size;
226 if (bouncesz > host->max_seg_size)
227 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200228 if (bouncesz > (host->max_blk_count * 512))
229 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200230
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200231 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400232 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
233 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530234 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400235 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200236 mmc_card_name(card));
237 }
Per Forlin04296b72011-07-01 18:55:31 +0200238 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
239 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530240 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200241 "allocate bounce prev buffer\n",
242 mmc_card_name(card));
243 kfree(mqrq_cur->bounce_buf);
244 mqrq_cur->bounce_buf = NULL;
245 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200246 }
247
Per Forlin04296b72011-07-01 18:55:31 +0200248 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200249 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500250 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500251 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200252 blk_queue_max_segment_size(mq->queue, bouncesz);
253
Per Forlin97868a22011-07-09 17:12:36 -0400254 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
255 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200256 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200257
Per Forlin97868a22011-07-09 17:12:36 -0400258 mqrq_cur->bounce_sg =
259 mmc_alloc_sg(bouncesz / 512, &ret);
260 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200261 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400262
Per Forlin04296b72011-07-01 18:55:31 +0200263 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
264 if (ret)
265 goto cleanup_queue;
266
267 mqrq_prev->bounce_sg =
268 mmc_alloc_sg(bouncesz / 512, &ret);
269 if (ret)
270 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200271 }
272 }
273#endif
274
Per Forlin04296b72011-07-01 18:55:31 +0200275 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200276 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500277 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200278 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400279 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200280 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
281
Per Forlin97868a22011-07-09 17:12:36 -0400282 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
283 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200284 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400285
Per Forlin04296b72011-07-01 18:55:31 +0200286
287 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
288 if (ret)
289 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Thomas Gleixner632cf922010-09-14 07:12:35 -0400292 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Adrian Hunterd09408a2011-06-23 13:40:28 +0300294 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
295 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400296
Christoph Hellwig87598a22006-11-13 20:23:52 +0100297 if (IS_ERR(mq->thread)) {
298 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200299 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 }
301
Christoph Hellwig87598a22006-11-13 20:23:52 +0100302 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200303 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400304 kfree(mqrq_cur->bounce_sg);
305 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200306 kfree(mqrq_prev->bounce_sg);
307 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400308
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200309 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400310 kfree(mqrq_cur->sg);
311 mqrq_cur->sg = NULL;
312 kfree(mqrq_cur->bounce_buf);
313 mqrq_cur->bounce_buf = NULL;
314
Per Forlin04296b72011-07-01 18:55:31 +0200315 kfree(mqrq_prev->sg);
316 mqrq_prev->sg = NULL;
317 kfree(mqrq_prev->bounce_buf);
318 mqrq_prev->bounce_buf = NULL;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return ret;
322}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324void mmc_cleanup_queue(struct mmc_queue *mq)
325{
Jens Axboe165125e2007-07-24 09:28:11 +0200326 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100327 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400328 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200329 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100330
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200331 /* Make sure the queue isn't suspended, as that will deadlock */
332 mmc_queue_resume(mq);
333
Pierre Ossman89b4e132006-11-14 22:08:16 +0100334 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100335 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800337 /* Empty the queue */
338 spin_lock_irqsave(q->queue_lock, flags);
339 q->queuedata = NULL;
340 blk_start_queue(q);
341 spin_unlock_irqrestore(q->queue_lock, flags);
342
Per Forlin97868a22011-07-09 17:12:36 -0400343 kfree(mqrq_cur->bounce_sg);
344 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200345
Per Forlin97868a22011-07-09 17:12:36 -0400346 kfree(mqrq_cur->sg);
347 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Per Forlin97868a22011-07-09 17:12:36 -0400349 kfree(mqrq_cur->bounce_buf);
350 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200351
Per Forlin04296b72011-07-01 18:55:31 +0200352 kfree(mqrq_prev->bounce_sg);
353 mqrq_prev->bounce_sg = NULL;
354
355 kfree(mqrq_prev->sg);
356 mqrq_prev->sg = NULL;
357
358 kfree(mqrq_prev->bounce_buf);
359 mqrq_prev->bounce_buf = NULL;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 mq->card = NULL;
362}
363EXPORT_SYMBOL(mmc_cleanup_queue);
364
365/**
366 * mmc_queue_suspend - suspend a MMC request queue
367 * @mq: MMC queue to suspend
368 *
369 * Stop the block request queue, and wait for our thread to
370 * complete any outstanding requests. This ensures that we
371 * won't suspend while a request is being processed.
372 */
373void mmc_queue_suspend(struct mmc_queue *mq)
374{
Jens Axboe165125e2007-07-24 09:28:11 +0200375 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 unsigned long flags;
377
378 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
379 mq->flags |= MMC_QUEUE_SUSPENDED;
380
381 spin_lock_irqsave(q->queue_lock, flags);
382 blk_stop_queue(q);
383 spin_unlock_irqrestore(q->queue_lock, flags);
384
385 down(&mq->thread_sem);
386 }
387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389/**
390 * mmc_queue_resume - resume a previously suspended MMC request queue
391 * @mq: MMC queue to resume
392 */
393void mmc_queue_resume(struct mmc_queue *mq)
394{
Jens Axboe165125e2007-07-24 09:28:11 +0200395 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 unsigned long flags;
397
398 if (mq->flags & MMC_QUEUE_SUSPENDED) {
399 mq->flags &= ~MMC_QUEUE_SUSPENDED;
400
401 up(&mq->thread_sem);
402
403 spin_lock_irqsave(q->queue_lock, flags);
404 blk_start_queue(q);
405 spin_unlock_irqrestore(q->queue_lock, flags);
406 }
407}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100408
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200409/*
410 * Prepare the sg list(s) to be handed of to the host driver
411 */
Per Forlin97868a22011-07-09 17:12:36 -0400412unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200413{
414 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200415 size_t buflen;
416 struct scatterlist *sg;
417 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200418
Per Forlin97868a22011-07-09 17:12:36 -0400419 if (!mqrq->bounce_buf)
420 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200421
Per Forlin97868a22011-07-09 17:12:36 -0400422 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200423
Per Forlin97868a22011-07-09 17:12:36 -0400424 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200425
Per Forlin97868a22011-07-09 17:12:36 -0400426 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200427
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200428 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400429 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200430 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200431
Per Forlin97868a22011-07-09 17:12:36 -0400432 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200433
434 return 1;
435}
436
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200437/*
438 * If writing, bounce the data to the buffer before the request
439 * is sent to the host driver
440 */
Per Forlin97868a22011-07-09 17:12:36 -0400441void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200442{
Per Forlin97868a22011-07-09 17:12:36 -0400443 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200444 return;
445
Per Forlin97868a22011-07-09 17:12:36 -0400446 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200447 return;
448
Per Forlin97868a22011-07-09 17:12:36 -0400449 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
450 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200451}
452
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200453/*
454 * If reading, bounce the data from the buffer after the request
455 * has been handled by the host driver
456 */
Per Forlin97868a22011-07-09 17:12:36 -0400457void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200458{
Per Forlin97868a22011-07-09 17:12:36 -0400459 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200460 return;
461
Per Forlin97868a22011-07-09 17:12:36 -0400462 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200463 return;
464
Per Forlin97868a22011-07-09 17:12:36 -0400465 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
466 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200467}