blob: 652e13acc6ad24cee4f41eb6afaf35e9f5e7946d [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) {
Jaehoon Chungf886c802012-05-28 10:33:35 +030070 if (mmc_card_doing_bkops(mq->card))
71 mmc_interrupt_bkops(mq->card);
72
Per Forlin91fd00b2011-07-01 18:55:33 +020073 set_current_state(TASK_RUNNING);
74 mq->issue_fn(mq, req);
75 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010076 if (kthread_should_stop()) {
77 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010079 }
Jaehoon Chungf886c802012-05-28 10:33:35 +030080
81 mmc_start_bkops(mq->card);
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 }
Per Forlin91fd00b2011-07-01 18:55:33 +020086
87 /* Current request becomes previous request and vice versa. */
88 mq->mqrq_prev->brq.mrq.data = NULL;
89 mq->mqrq_prev->req = NULL;
90 tmp = mq->mqrq_prev;
91 mq->mqrq_prev = mq->mqrq_cur;
92 mq->mqrq_cur = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 up(&mq->thread_sem);
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 return 0;
97}
98
99/*
100 * Generic MMC request handler. This is called for any queue on a
101 * particular host. When the host is not busy, we look for a request
102 * on any queue on this host, and attempt to issue it. This may
103 * not be the queue we were asked to process.
104 */
Jens Axboe165125e2007-07-24 09:28:11 +0200105static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100108 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100109
110 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800111 while ((req = blk_fetch_request(q)) != NULL) {
112 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900113 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800114 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100115 return;
116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Per Forlin91fd00b2011-07-01 18:55:33 +0200118 if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100119 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
Per Forlincb86e7b2011-07-09 17:12:36 -0400122struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
123{
124 struct scatterlist *sg;
125
126 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
127 if (!sg)
128 *err = -ENOMEM;
129 else {
130 *err = 0;
131 sg_init_table(sg, sg_len);
132 }
133
134 return sg;
135}
136
Adrian Hunter81306ad2011-06-28 17:16:02 +0300137static void mmc_queue_setup_discard(struct request_queue *q,
138 struct mmc_card *card)
139{
140 unsigned max_discard;
141
142 max_discard = mmc_calc_max_discard(card);
143 if (!max_discard)
144 return;
145
146 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
147 q->limits.max_discard_sectors = max_discard;
148 if (card->erased_byte == 0)
149 q->limits.discard_zeroes_data = 1;
150 q->limits.discard_granularity = card->pref_erase << 9;
151 /* granularity must not be greater than max. discard */
152 if (card->pref_erase > max_discard)
153 q->limits.discard_granularity = 0;
Maya Erez463bb952012-05-24 23:46:29 +0300154 if (mmc_can_secure_erase_trim(card))
Adrian Hunter81306ad2011-06-28 17:16:02 +0300155 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
156}
157
Maya Erez463bb952012-05-24 23:46:29 +0300158static void mmc_queue_setup_sanitize(struct request_queue *q)
159{
160 queue_flag_set_unlocked(QUEUE_FLAG_SANITIZE, q);
161}
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163/**
164 * mmc_init_queue - initialise a queue structure.
165 * @mq: mmc queue
166 * @card: mmc card to attach this queue
167 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300168 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
170 * Initialise a MMC card request queue.
171 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300172int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
173 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 struct mmc_host *host = card->host;
176 u64 limit = BLK_BOUNCE_HIGH;
177 int ret;
Per Forlincb86e7b2011-07-09 17:12:36 -0400178 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlind07424b2011-07-01 18:55:31 +0200179 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200181 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
182 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
184 mq->card = card;
185 mq->queue = blk_init_queue(mmc_request, lock);
186 if (!mq->queue)
187 return -ENOMEM;
188
Per Forlincb86e7b2011-07-09 17:12:36 -0400189 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
Per Forlind07424b2011-07-01 18:55:31 +0200190 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
Seungwon Jeon968c7742012-05-31 11:54:47 +0300191 INIT_LIST_HEAD(&mqrq_cur->packed_list);
192 INIT_LIST_HEAD(&mqrq_prev->packed_list);
Per Forlincb86e7b2011-07-09 17:12:36 -0400193 mq->mqrq_cur = mqrq_cur;
Per Forlind07424b2011-07-01 18:55:31 +0200194 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Pierre Ossman98ccf142007-05-12 00:26:16 +0200197 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200198 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Hunter81306ad2011-06-28 17:16:02 +0300199 if (mmc_can_erase(card))
200 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200201
Maya Erez463bb952012-05-24 23:46:29 +0300202 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
203 mmc_queue_setup_sanitize(mq->queue);
204
Pierre Ossman98ccf142007-05-12 00:26:16 +0200205#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400206 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200207 unsigned int bouncesz;
208
Pierre Ossman98ccf142007-05-12 00:26:16 +0200209 bouncesz = MMC_QUEUE_BOUNCESZ;
210
211 if (bouncesz > host->max_req_size)
212 bouncesz = host->max_req_size;
213 if (bouncesz > host->max_seg_size)
214 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200215 if (bouncesz > (host->max_blk_count * 512))
216 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200217
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200218 if (bouncesz > 512) {
Per Forlincb86e7b2011-07-09 17:12:36 -0400219 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
220 if (!mqrq_cur->bounce_buf) {
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200221 printk(KERN_WARNING "%s: unable to "
Per Forlincb86e7b2011-07-09 17:12:36 -0400222 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200223 mmc_card_name(card));
224 }
Per Forlind07424b2011-07-01 18:55:31 +0200225 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
226 if (!mqrq_prev->bounce_buf) {
227 printk(KERN_WARNING "%s: unable to "
228 "allocate bounce prev buffer\n",
229 mmc_card_name(card));
230 kfree(mqrq_cur->bounce_buf);
231 mqrq_cur->bounce_buf = NULL;
232 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200233 }
234
Per Forlind07424b2011-07-01 18:55:31 +0200235 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200236 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500237 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500238 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200239 blk_queue_max_segment_size(mq->queue, bouncesz);
240
Per Forlincb86e7b2011-07-09 17:12:36 -0400241 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
242 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200243 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200244
Per Forlincb86e7b2011-07-09 17:12:36 -0400245 mqrq_cur->bounce_sg =
246 mmc_alloc_sg(bouncesz / 512, &ret);
247 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200248 goto cleanup_queue;
Per Forlincb86e7b2011-07-09 17:12:36 -0400249
Per Forlind07424b2011-07-01 18:55:31 +0200250 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
251 if (ret)
252 goto cleanup_queue;
253
254 mqrq_prev->bounce_sg =
255 mmc_alloc_sg(bouncesz / 512, &ret);
256 if (ret)
257 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200258 }
259 }
260#endif
261
Per Forlind07424b2011-07-01 18:55:31 +0200262 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200263 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500264 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200265 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400266 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200267 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
268
Per Forlincb86e7b2011-07-09 17:12:36 -0400269 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
270 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200271 goto cleanup_queue;
Per Forlincb86e7b2011-07-09 17:12:36 -0400272
Per Forlind07424b2011-07-01 18:55:31 +0200273
274 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
275 if (ret)
276 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
Thomas Gleixner632cf922010-09-14 07:12:35 -0400279 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Adrian Hunterd09408a2011-06-23 13:40:28 +0300281 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
282 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400283
Christoph Hellwig87598a22006-11-13 20:23:52 +0100284 if (IS_ERR(mq->thread)) {
285 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200286 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Christoph Hellwig87598a22006-11-13 20:23:52 +0100289 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200290 free_bounce_sg:
Per Forlincb86e7b2011-07-09 17:12:36 -0400291 kfree(mqrq_cur->bounce_sg);
292 mqrq_cur->bounce_sg = NULL;
Per Forlind07424b2011-07-01 18:55:31 +0200293 kfree(mqrq_prev->bounce_sg);
294 mqrq_prev->bounce_sg = NULL;
Per Forlincb86e7b2011-07-09 17:12:36 -0400295
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200296 cleanup_queue:
Per Forlincb86e7b2011-07-09 17:12:36 -0400297 kfree(mqrq_cur->sg);
298 mqrq_cur->sg = NULL;
299 kfree(mqrq_cur->bounce_buf);
300 mqrq_cur->bounce_buf = NULL;
301
Per Forlind07424b2011-07-01 18:55:31 +0200302 kfree(mqrq_prev->sg);
303 mqrq_prev->sg = NULL;
304 kfree(mqrq_prev->bounce_buf);
305 mqrq_prev->bounce_buf = NULL;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return ret;
309}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
311void mmc_cleanup_queue(struct mmc_queue *mq)
312{
Jens Axboe165125e2007-07-24 09:28:11 +0200313 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100314 unsigned long flags;
Per Forlincb86e7b2011-07-09 17:12:36 -0400315 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlind07424b2011-07-01 18:55:31 +0200316 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100317
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200318 /* Make sure the queue isn't suspended, as that will deadlock */
319 mmc_queue_resume(mq);
320
Pierre Ossman89b4e132006-11-14 22:08:16 +0100321 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100322 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800324 /* Empty the queue */
325 spin_lock_irqsave(q->queue_lock, flags);
326 q->queuedata = NULL;
327 blk_start_queue(q);
328 spin_unlock_irqrestore(q->queue_lock, flags);
329
Per Forlincb86e7b2011-07-09 17:12:36 -0400330 kfree(mqrq_cur->bounce_sg);
331 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200332
Per Forlincb86e7b2011-07-09 17:12:36 -0400333 kfree(mqrq_cur->sg);
334 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Per Forlincb86e7b2011-07-09 17:12:36 -0400336 kfree(mqrq_cur->bounce_buf);
337 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200338
Per Forlind07424b2011-07-01 18:55:31 +0200339 kfree(mqrq_prev->bounce_sg);
340 mqrq_prev->bounce_sg = NULL;
341
342 kfree(mqrq_prev->sg);
343 mqrq_prev->sg = NULL;
344
345 kfree(mqrq_prev->bounce_buf);
346 mqrq_prev->bounce_buf = NULL;
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 mq->card = NULL;
349}
350EXPORT_SYMBOL(mmc_cleanup_queue);
351
352/**
353 * mmc_queue_suspend - suspend a MMC request queue
354 * @mq: MMC queue to suspend
355 *
356 * Stop the block request queue, and wait for our thread to
357 * complete any outstanding requests. This ensures that we
358 * won't suspend while a request is being processed.
359 */
360void mmc_queue_suspend(struct mmc_queue *mq)
361{
Jens Axboe165125e2007-07-24 09:28:11 +0200362 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 unsigned long flags;
364
365 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
366 mq->flags |= MMC_QUEUE_SUSPENDED;
367
368 spin_lock_irqsave(q->queue_lock, flags);
369 blk_stop_queue(q);
370 spin_unlock_irqrestore(q->queue_lock, flags);
371
372 down(&mq->thread_sem);
373 }
374}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
376/**
377 * mmc_queue_resume - resume a previously suspended MMC request queue
378 * @mq: MMC queue to resume
379 */
380void mmc_queue_resume(struct mmc_queue *mq)
381{
Jens Axboe165125e2007-07-24 09:28:11 +0200382 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 unsigned long flags;
384
385 if (mq->flags & MMC_QUEUE_SUSPENDED) {
386 mq->flags &= ~MMC_QUEUE_SUSPENDED;
387
388 up(&mq->thread_sem);
389
390 spin_lock_irqsave(q->queue_lock, flags);
391 blk_start_queue(q);
392 spin_unlock_irqrestore(q->queue_lock, flags);
393 }
394}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100395
Seungwon Jeon968c7742012-05-31 11:54:47 +0300396static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
397 struct mmc_queue_req *mqrq,
398 struct scatterlist *sg)
399{
400 struct scatterlist *__sg;
401 unsigned int sg_len = 0;
402 struct request *req;
403 enum mmc_packed_cmd cmd;
404
405 cmd = mqrq->packed_cmd;
406
407 if (cmd == MMC_PACKED_WRITE) {
408 __sg = sg;
409 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
410 sizeof(mqrq->packed_cmd_hdr));
411 sg_len++;
412 __sg->page_link &= ~0x02;
413 }
414
415 __sg = sg + sg_len;
416 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
417 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
418 __sg = sg + (sg_len - 1);
419 (__sg++)->page_link &= ~0x02;
420 }
421 sg_mark_end(sg + (sg_len - 1));
422 return sg_len;
423}
424
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200425/*
426 * Prepare the sg list(s) to be handed of to the host driver
427 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400428unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200429{
430 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200431 size_t buflen;
432 struct scatterlist *sg;
433 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200434
Seungwon Jeon968c7742012-05-31 11:54:47 +0300435 if (!mqrq->bounce_buf) {
436 if (!list_empty(&mqrq->packed_list))
437 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
438 else
439 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
440 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200441
Per Forlincb86e7b2011-07-09 17:12:36 -0400442 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200443
Seungwon Jeon968c7742012-05-31 11:54:47 +0300444 if (!list_empty(&mqrq->packed_list))
445 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
446 else
447 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200448
Per Forlincb86e7b2011-07-09 17:12:36 -0400449 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200450
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200451 buflen = 0;
Per Forlincb86e7b2011-07-09 17:12:36 -0400452 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200453 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200454
Per Forlincb86e7b2011-07-09 17:12:36 -0400455 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200456
457 return 1;
458}
459
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200460/*
461 * If writing, bounce the data to the buffer before the request
462 * is sent to the host driver
463 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400464void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200465{
Per Forlincb86e7b2011-07-09 17:12:36 -0400466 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200467 return;
468
Per Forlincb86e7b2011-07-09 17:12:36 -0400469 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200470 return;
471
Per Forlincb86e7b2011-07-09 17:12:36 -0400472 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
473 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200474}
475
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200476/*
477 * If reading, bounce the data from the buffer after the request
478 * has been handled by the host driver
479 */
Per Forlincb86e7b2011-07-09 17:12:36 -0400480void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200481{
Per Forlincb86e7b2011-07-09 17:12:36 -0400482 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200483 return;
484
Per Forlincb86e7b2011-07-09 17:12:36 -0400485 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486 return;
487
Per Forlincb86e7b2011-07-09 17:12:36 -0400488 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
489 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200490}