blob: f3692a90deef14ae8ce822503f21589618596494 [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/*
Tatyana Brokhman0cc76402012-10-07 09:52:16 +020028 * 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
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200191 INIT_LIST_HEAD(&mqrq_cur->packed_list);
192 INIT_LIST_HEAD(&mqrq_prev->packed_list);
193
Per Forlin97868a22011-07-09 17:12:36 -0400194 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
Per Forlin04296b72011-07-01 18:55:31 +0200195 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
Per Forlin97868a22011-07-09 17:12:36 -0400196 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200197 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 mq->queue->queuedata = mq;
Tatyana Brokhman0cc76402012-10-07 09:52:16 +0200199 mq->num_wr_reqs_to_start_packing = DEFAULT_NUM_REQS_TO_START_PACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Pierre Ossman98ccf142007-05-12 00:26:16 +0200201 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200202 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300203 if (mmc_can_erase(card))
204 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200205
Maya Erez463bb952012-05-24 23:46:29 +0300206 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
207 mmc_queue_setup_sanitize(mq->queue);
208
Pierre Ossman98ccf142007-05-12 00:26:16 +0200209#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400210 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200211 unsigned int bouncesz;
212
Pierre Ossman98ccf142007-05-12 00:26:16 +0200213 bouncesz = MMC_QUEUE_BOUNCESZ;
214
215 if (bouncesz > host->max_req_size)
216 bouncesz = host->max_req_size;
217 if (bouncesz > host->max_seg_size)
218 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200219 if (bouncesz > (host->max_blk_count * 512))
220 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200221
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200222 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400223 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
224 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530225 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400226 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200227 mmc_card_name(card));
228 }
Per Forlin04296b72011-07-01 18:55:31 +0200229 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
230 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530231 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200232 "allocate bounce prev buffer\n",
233 mmc_card_name(card));
234 kfree(mqrq_cur->bounce_buf);
235 mqrq_cur->bounce_buf = NULL;
236 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200237 }
238
Per Forlin04296b72011-07-01 18:55:31 +0200239 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200240 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500241 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500242 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200243 blk_queue_max_segment_size(mq->queue, bouncesz);
244
Per Forlin97868a22011-07-09 17:12:36 -0400245 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
246 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200247 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200248
Per Forlin97868a22011-07-09 17:12:36 -0400249 mqrq_cur->bounce_sg =
250 mmc_alloc_sg(bouncesz / 512, &ret);
251 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200252 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400253
Per Forlin04296b72011-07-01 18:55:31 +0200254 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
255 if (ret)
256 goto cleanup_queue;
257
258 mqrq_prev->bounce_sg =
259 mmc_alloc_sg(bouncesz / 512, &ret);
260 if (ret)
261 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200262 }
263 }
264#endif
265
Per Forlin04296b72011-07-01 18:55:31 +0200266 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200267 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500268 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200269 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400270 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200271 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
272
Per Forlin97868a22011-07-09 17:12:36 -0400273 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
274 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200275 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400276
Per Forlin04296b72011-07-01 18:55:31 +0200277
278 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
279 if (ret)
280 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282
Thomas Gleixner632cf922010-09-14 07:12:35 -0400283 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Adrian Hunterd09408a2011-06-23 13:40:28 +0300285 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
286 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400287
Christoph Hellwig87598a22006-11-13 20:23:52 +0100288 if (IS_ERR(mq->thread)) {
289 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200290 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Christoph Hellwig87598a22006-11-13 20:23:52 +0100293 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200294 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400295 kfree(mqrq_cur->bounce_sg);
296 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200297 kfree(mqrq_prev->bounce_sg);
298 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400299
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200300 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400301 kfree(mqrq_cur->sg);
302 mqrq_cur->sg = NULL;
303 kfree(mqrq_cur->bounce_buf);
304 mqrq_cur->bounce_buf = NULL;
305
Per Forlin04296b72011-07-01 18:55:31 +0200306 kfree(mqrq_prev->sg);
307 mqrq_prev->sg = NULL;
308 kfree(mqrq_prev->bounce_buf);
309 mqrq_prev->bounce_buf = NULL;
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return ret;
313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315void mmc_cleanup_queue(struct mmc_queue *mq)
316{
Jens Axboe165125e2007-07-24 09:28:11 +0200317 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100318 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400319 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200320 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100321
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200322 /* Make sure the queue isn't suspended, as that will deadlock */
323 mmc_queue_resume(mq);
324
Pierre Ossman89b4e132006-11-14 22:08:16 +0100325 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100326 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800328 /* Empty the queue */
329 spin_lock_irqsave(q->queue_lock, flags);
330 q->queuedata = NULL;
331 blk_start_queue(q);
332 spin_unlock_irqrestore(q->queue_lock, flags);
333
Per Forlin97868a22011-07-09 17:12:36 -0400334 kfree(mqrq_cur->bounce_sg);
335 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200336
Per Forlin97868a22011-07-09 17:12:36 -0400337 kfree(mqrq_cur->sg);
338 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Per Forlin97868a22011-07-09 17:12:36 -0400340 kfree(mqrq_cur->bounce_buf);
341 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200342
Per Forlin04296b72011-07-01 18:55:31 +0200343 kfree(mqrq_prev->bounce_sg);
344 mqrq_prev->bounce_sg = NULL;
345
346 kfree(mqrq_prev->sg);
347 mqrq_prev->sg = NULL;
348
349 kfree(mqrq_prev->bounce_buf);
350 mqrq_prev->bounce_buf = NULL;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 mq->card = NULL;
353}
354EXPORT_SYMBOL(mmc_cleanup_queue);
355
356/**
357 * mmc_queue_suspend - suspend a MMC request queue
358 * @mq: MMC queue to suspend
359 *
360 * Stop the block request queue, and wait for our thread to
361 * complete any outstanding requests. This ensures that we
362 * won't suspend while a request is being processed.
363 */
364void mmc_queue_suspend(struct mmc_queue *mq)
365{
Jens Axboe165125e2007-07-24 09:28:11 +0200366 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 unsigned long flags;
368
369 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
370 mq->flags |= MMC_QUEUE_SUSPENDED;
371
372 spin_lock_irqsave(q->queue_lock, flags);
373 blk_stop_queue(q);
374 spin_unlock_irqrestore(q->queue_lock, flags);
375
376 down(&mq->thread_sem);
377 }
378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
380/**
381 * mmc_queue_resume - resume a previously suspended MMC request queue
382 * @mq: MMC queue to resume
383 */
384void mmc_queue_resume(struct mmc_queue *mq)
385{
Jens Axboe165125e2007-07-24 09:28:11 +0200386 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 unsigned long flags;
388
389 if (mq->flags & MMC_QUEUE_SUSPENDED) {
390 mq->flags &= ~MMC_QUEUE_SUSPENDED;
391
392 up(&mq->thread_sem);
393
394 spin_lock_irqsave(q->queue_lock, flags);
395 blk_start_queue(q);
396 spin_unlock_irqrestore(q->queue_lock, flags);
397 }
398}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100399
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200400static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
401 struct mmc_queue_req *mqrq,
402 struct scatterlist *sg)
403{
404 struct scatterlist *__sg;
405 unsigned int sg_len = 0;
406 struct request *req;
407 enum mmc_packed_cmd cmd;
408
409 cmd = mqrq->packed_cmd;
410
411 if (cmd == MMC_PACKED_WRITE) {
412 __sg = sg;
413 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
414 sizeof(mqrq->packed_cmd_hdr));
415 sg_len++;
416 __sg->page_link &= ~0x02;
417 }
418
419 __sg = sg + sg_len;
420 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
421 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
422 __sg = sg + (sg_len - 1);
423 (__sg++)->page_link &= ~0x02;
424 }
425 sg_mark_end(sg + (sg_len - 1));
426 return sg_len;
427}
428
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200429/*
430 * Prepare the sg list(s) to be handed of to the host driver
431 */
Per Forlin97868a22011-07-09 17:12:36 -0400432unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200433{
434 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200435 size_t buflen;
436 struct scatterlist *sg;
437 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200438
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200439 if (!mqrq->bounce_buf) {
440 if (!list_empty(&mqrq->packed_list))
441 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
442 else
443 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
444 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200445
Per Forlin97868a22011-07-09 17:12:36 -0400446 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200447
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200448 if (!list_empty(&mqrq->packed_list))
449 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
450 else
451 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200452
Per Forlin97868a22011-07-09 17:12:36 -0400453 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200454
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200455 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400456 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200457 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200458
Per Forlin97868a22011-07-09 17:12:36 -0400459 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200460
461 return 1;
462}
463
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200464/*
465 * If writing, bounce the data to the buffer before the request
466 * is sent to the host driver
467 */
Per Forlin97868a22011-07-09 17:12:36 -0400468void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200469{
Per Forlin97868a22011-07-09 17:12:36 -0400470 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200471 return;
472
Per Forlin97868a22011-07-09 17:12:36 -0400473 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200474 return;
475
Per Forlin97868a22011-07-09 17:12:36 -0400476 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
477 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200478}
479
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200480/*
481 * If reading, bounce the data from the buffer after the request
482 * has been handled by the host driver
483 */
Per Forlin97868a22011-07-09 17:12:36 -0400484void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200485{
Per Forlin97868a22011-07-09 17:12:36 -0400486 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200487 return;
488
Per Forlin97868a22011-07-09 17:12:36 -0400489 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200490 return;
491
Per Forlin97868a22011-07-09 17:12:36 -0400492 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
493 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200494}