blob: e1e4408bb2c1fd8a9079ae226b06854fa389872c [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/*
Maya Erez63c61d62012-05-31 21:00:18 +030028 * Based on benchmark tests the default num of requests to trigger the write
29 * packing was determined, to keep the read latency as low as possible and
30 * manage to keep the high write throughput.
31 */
32#define DEFAULT_NUM_REQS_TO_START_PACK 17
33
34/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020035 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
37static int mmc_prep_request(struct request_queue *q, struct request *req)
38{
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +053039 struct mmc_queue *mq = q->queuedata;
40
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020041 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070042 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020043 */
Adrian Hunterbd788c92010-08-11 14:17:47 -070044 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020046 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 }
48
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +053049 if (mq && mmc_card_removed(mq->card))
50 return BLKPREP_KILL;
51
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020052 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020054 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055}
56
57static int mmc_queue_thread(void *d)
58{
59 struct mmc_queue *mq = d;
60 struct request_queue *q = mq->queue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Rafael J. Wysocki83144182007-07-17 04:03:35 -070063 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 do {
Per Forlinee8a43a2011-07-01 18:55:33 +020067 struct mmc_queue_req *tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 spin_lock_irq(q->queue_lock);
71 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010072 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040073 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 spin_unlock_irq(q->queue_lock);
75
Per Forlinee8a43a2011-07-01 18:55:33 +020076 if (req || mq->mqrq_prev->req) {
77 set_current_state(TASK_RUNNING);
78 mq->issue_fn(mq, req);
79 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010080 if (kthread_should_stop()) {
81 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 up(&mq->thread_sem);
85 schedule();
86 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Per Forlinee8a43a2011-07-01 18:55:33 +020089 /* Current request becomes previous request and vice versa. */
90 mq->mqrq_prev->brq.mrq.data = NULL;
91 mq->mqrq_prev->req = NULL;
92 tmp = mq->mqrq_prev;
93 mq->mqrq_prev = mq->mqrq_cur;
94 mq->mqrq_cur = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 up(&mq->thread_sem);
97
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 return 0;
99}
100
101/*
102 * Generic MMC request handler. This is called for any queue on a
103 * particular host. When the host is not busy, we look for a request
104 * on any queue on this host, and attempt to issue it. This may
105 * not be the queue we were asked to process.
106 */
Jens Axboe165125e2007-07-24 09:28:11 +0200107static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100110 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100111
112 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800113 while ((req = blk_fetch_request(q)) != NULL) {
114 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900115 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800116 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100117 return;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Per Forlinee8a43a2011-07-01 18:55:33 +0200120 if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100121 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Venkatraman S7513cd72011-08-23 21:16:02 +0530124static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400125{
126 struct scatterlist *sg;
127
128 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
129 if (!sg)
130 *err = -ENOMEM;
131 else {
132 *err = 0;
133 sg_init_table(sg, sg_len);
134 }
135
136 return sg;
137}
138
Adrian Huntere056a1b2011-06-28 17:16:02 +0300139static void mmc_queue_setup_discard(struct request_queue *q,
140 struct mmc_card *card)
141{
142 unsigned max_discard;
143
144 max_discard = mmc_calc_max_discard(card);
145 if (!max_discard)
146 return;
147
148 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
149 q->limits.max_discard_sectors = max_discard;
Adrian Hunter7194efb2012-04-05 14:45:47 +0300150 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300151 q->limits.discard_zeroes_data = 1;
152 q->limits.discard_granularity = card->pref_erase << 9;
153 /* granularity must not be greater than max. discard */
154 if (card->pref_erase > max_discard)
155 q->limits.discard_granularity = 0;
Maya Erez463bb952012-05-24 23:46:29 +0300156 if (mmc_can_secure_erase_trim(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300157 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
158}
159
Maya Erez463bb952012-05-24 23:46:29 +0300160static void mmc_queue_setup_sanitize(struct request_queue *q)
161{
162 queue_flag_set_unlocked(QUEUE_FLAG_SANITIZE, q);
163}
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/**
166 * mmc_init_queue - initialise a queue structure.
167 * @mq: mmc queue
168 * @card: mmc card to attach this queue
169 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300170 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 *
172 * Initialise a MMC card request queue.
173 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300174int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
175 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 struct mmc_host *host = card->host;
178 u64 limit = BLK_BOUNCE_HIGH;
179 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400180 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200181 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200183 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
184 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 mq->card = card;
187 mq->queue = blk_init_queue(mmc_request, lock);
188 if (!mq->queue)
189 return -ENOMEM;
190
Per Forlin97868a22011-07-09 17:12:36 -0400191 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
Per Forlin04296b72011-07-01 18:55:31 +0200192 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
Seungwon Jeon968c7742012-05-31 11:54:47 +0300193 INIT_LIST_HEAD(&mqrq_cur->packed_list);
194 INIT_LIST_HEAD(&mqrq_prev->packed_list);
Per Forlin97868a22011-07-09 17:12:36 -0400195 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200196 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 mq->queue->queuedata = mq;
Maya Erez63c61d62012-05-31 21:00:18 +0300198 mq->num_wr_reqs_to_start_packing = DEFAULT_NUM_REQS_TO_START_PACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Pierre Ossman98ccf142007-05-12 00:26:16 +0200200 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200201 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300202 if (mmc_can_erase(card))
203 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200204
Maya Erez463bb952012-05-24 23:46:29 +0300205 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
206 mmc_queue_setup_sanitize(mq->queue);
207
Pierre Ossman98ccf142007-05-12 00:26:16 +0200208#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400209 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200210 unsigned int bouncesz;
211
Pierre Ossman98ccf142007-05-12 00:26:16 +0200212 bouncesz = MMC_QUEUE_BOUNCESZ;
213
214 if (bouncesz > host->max_req_size)
215 bouncesz = host->max_req_size;
216 if (bouncesz > host->max_seg_size)
217 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200218 if (bouncesz > (host->max_blk_count * 512))
219 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200220
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200221 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400222 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
223 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530224 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400225 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200226 mmc_card_name(card));
227 }
Per Forlin04296b72011-07-01 18:55:31 +0200228 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
229 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530230 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200231 "allocate bounce prev buffer\n",
232 mmc_card_name(card));
233 kfree(mqrq_cur->bounce_buf);
234 mqrq_cur->bounce_buf = NULL;
235 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200236 }
237
Per Forlin04296b72011-07-01 18:55:31 +0200238 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200239 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500240 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500241 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200242 blk_queue_max_segment_size(mq->queue, bouncesz);
243
Per Forlin97868a22011-07-09 17:12:36 -0400244 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
245 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200246 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200247
Per Forlin97868a22011-07-09 17:12:36 -0400248 mqrq_cur->bounce_sg =
249 mmc_alloc_sg(bouncesz / 512, &ret);
250 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200251 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400252
Per Forlin04296b72011-07-01 18:55:31 +0200253 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
254 if (ret)
255 goto cleanup_queue;
256
257 mqrq_prev->bounce_sg =
258 mmc_alloc_sg(bouncesz / 512, &ret);
259 if (ret)
260 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200261 }
262 }
263#endif
264
Per Forlin04296b72011-07-01 18:55:31 +0200265 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200266 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500267 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200268 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400269 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200270 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
271
Per Forlin97868a22011-07-09 17:12:36 -0400272 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
273 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200274 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400275
Per Forlin04296b72011-07-01 18:55:31 +0200276
277 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
278 if (ret)
279 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281
Thomas Gleixner632cf922010-09-14 07:12:35 -0400282 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Adrian Hunterd09408a2011-06-23 13:40:28 +0300284 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
285 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400286
Christoph Hellwig87598a22006-11-13 20:23:52 +0100287 if (IS_ERR(mq->thread)) {
288 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200289 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 }
291
Christoph Hellwig87598a22006-11-13 20:23:52 +0100292 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200293 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400294 kfree(mqrq_cur->bounce_sg);
295 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200296 kfree(mqrq_prev->bounce_sg);
297 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400298
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200299 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400300 kfree(mqrq_cur->sg);
301 mqrq_cur->sg = NULL;
302 kfree(mqrq_cur->bounce_buf);
303 mqrq_cur->bounce_buf = NULL;
304
Per Forlin04296b72011-07-01 18:55:31 +0200305 kfree(mqrq_prev->sg);
306 mqrq_prev->sg = NULL;
307 kfree(mqrq_prev->bounce_buf);
308 mqrq_prev->bounce_buf = NULL;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 return ret;
312}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
314void mmc_cleanup_queue(struct mmc_queue *mq)
315{
Jens Axboe165125e2007-07-24 09:28:11 +0200316 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100317 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400318 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200319 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100320
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200321 /* Make sure the queue isn't suspended, as that will deadlock */
322 mmc_queue_resume(mq);
323
Pierre Ossman89b4e132006-11-14 22:08:16 +0100324 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100325 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800327 /* Empty the queue */
328 spin_lock_irqsave(q->queue_lock, flags);
329 q->queuedata = NULL;
330 blk_start_queue(q);
331 spin_unlock_irqrestore(q->queue_lock, flags);
332
Per Forlin97868a22011-07-09 17:12:36 -0400333 kfree(mqrq_cur->bounce_sg);
334 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200335
Per Forlin97868a22011-07-09 17:12:36 -0400336 kfree(mqrq_cur->sg);
337 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Per Forlin97868a22011-07-09 17:12:36 -0400339 kfree(mqrq_cur->bounce_buf);
340 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200341
Per Forlin04296b72011-07-01 18:55:31 +0200342 kfree(mqrq_prev->bounce_sg);
343 mqrq_prev->bounce_sg = NULL;
344
345 kfree(mqrq_prev->sg);
346 mqrq_prev->sg = NULL;
347
348 kfree(mqrq_prev->bounce_buf);
349 mqrq_prev->bounce_buf = NULL;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 mq->card = NULL;
352}
353EXPORT_SYMBOL(mmc_cleanup_queue);
354
355/**
356 * mmc_queue_suspend - suspend a MMC request queue
357 * @mq: MMC queue to suspend
358 *
359 * Stop the block request queue, and wait for our thread to
360 * complete any outstanding requests. This ensures that we
361 * won't suspend while a request is being processed.
362 */
363void mmc_queue_suspend(struct mmc_queue *mq)
364{
Jens Axboe165125e2007-07-24 09:28:11 +0200365 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned long flags;
367
368 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
369 mq->flags |= MMC_QUEUE_SUSPENDED;
370
371 spin_lock_irqsave(q->queue_lock, flags);
372 blk_stop_queue(q);
373 spin_unlock_irqrestore(q->queue_lock, flags);
374
375 down(&mq->thread_sem);
376 }
377}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379/**
380 * mmc_queue_resume - resume a previously suspended MMC request queue
381 * @mq: MMC queue to resume
382 */
383void mmc_queue_resume(struct mmc_queue *mq)
384{
Jens Axboe165125e2007-07-24 09:28:11 +0200385 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned long flags;
387
388 if (mq->flags & MMC_QUEUE_SUSPENDED) {
389 mq->flags &= ~MMC_QUEUE_SUSPENDED;
390
391 up(&mq->thread_sem);
392
393 spin_lock_irqsave(q->queue_lock, flags);
394 blk_start_queue(q);
395 spin_unlock_irqrestore(q->queue_lock, flags);
396 }
397}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100398
Seungwon Jeon968c7742012-05-31 11:54:47 +0300399static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
400 struct mmc_queue_req *mqrq,
401 struct scatterlist *sg)
402{
403 struct scatterlist *__sg;
404 unsigned int sg_len = 0;
405 struct request *req;
406 enum mmc_packed_cmd cmd;
407
408 cmd = mqrq->packed_cmd;
409
410 if (cmd == MMC_PACKED_WRITE) {
411 __sg = sg;
412 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
413 sizeof(mqrq->packed_cmd_hdr));
414 sg_len++;
415 __sg->page_link &= ~0x02;
416 }
417
418 __sg = sg + sg_len;
419 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
420 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
421 __sg = sg + (sg_len - 1);
422 (__sg++)->page_link &= ~0x02;
423 }
424 sg_mark_end(sg + (sg_len - 1));
425 return sg_len;
426}
427
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200428/*
429 * Prepare the sg list(s) to be handed of to the host driver
430 */
Per Forlin97868a22011-07-09 17:12:36 -0400431unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200432{
433 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200434 size_t buflen;
435 struct scatterlist *sg;
436 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200437
Seungwon Jeon968c7742012-05-31 11:54:47 +0300438 if (!mqrq->bounce_buf) {
439 if (!list_empty(&mqrq->packed_list))
440 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
441 else
442 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
443 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200444
Per Forlin97868a22011-07-09 17:12:36 -0400445 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200446
Seungwon Jeon968c7742012-05-31 11:54:47 +0300447 if (!list_empty(&mqrq->packed_list))
448 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
449 else
450 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200451
Per Forlin97868a22011-07-09 17:12:36 -0400452 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200453
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200454 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400455 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200456 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200457
Per Forlin97868a22011-07-09 17:12:36 -0400458 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200459
460 return 1;
461}
462
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200463/*
464 * If writing, bounce the data to the buffer before the request
465 * is sent to the host driver
466 */
Per Forlin97868a22011-07-09 17:12:36 -0400467void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200468{
Per Forlin97868a22011-07-09 17:12:36 -0400469 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200470 return;
471
Per Forlin97868a22011-07-09 17:12:36 -0400472 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200473 return;
474
Per Forlin97868a22011-07-09 17:12:36 -0400475 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
476 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200477}
478
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200479/*
480 * If reading, bounce the data from the buffer after the request
481 * has been handled by the host driver
482 */
Per Forlin97868a22011-07-09 17:12:36 -0400483void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200484{
Per Forlin97868a22011-07-09 17:12:36 -0400485 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486 return;
487
Per Forlin97868a22011-07-09 17:12:36 -0400488 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200489 return;
490
Per Forlin97868a22011-07-09 17:12:36 -0400491 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
492 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200493}