blob: baebe6509e6eabf29014d9731ae53964e4194649 [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 Brokhman98e67812012-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;
Maya Erez5c46dad2012-10-10 03:47:54 +020062 struct mmc_card *card = mq->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Rafael J. Wysocki83144182007-07-17 04:03:35 -070064 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 do {
Per Forlinee8a43a2011-07-01 18:55:33 +020068 struct mmc_queue_req *tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070069 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
71 spin_lock_irq(q->queue_lock);
72 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010073 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040074 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 spin_unlock_irq(q->queue_lock);
76
Per Forlinee8a43a2011-07-01 18:55:33 +020077 if (req || mq->mqrq_prev->req) {
78 set_current_state(TASK_RUNNING);
79 mq->issue_fn(mq, req);
80 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010081 if (kthread_should_stop()) {
82 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010084 }
Maya Erez5c46dad2012-10-10 03:47:54 +020085 mmc_start_delayed_bkops(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 up(&mq->thread_sem);
87 schedule();
88 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Per Forlinee8a43a2011-07-01 18:55:33 +020091 /* Current request becomes previous request and vice versa. */
92 mq->mqrq_prev->brq.mrq.data = NULL;
93 mq->mqrq_prev->req = NULL;
94 tmp = mq->mqrq_prev;
95 mq->mqrq_prev = mq->mqrq_cur;
96 mq->mqrq_cur = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 up(&mq->thread_sem);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
102
103/*
104 * Generic MMC request handler. This is called for any queue on a
105 * particular host. When the host is not busy, we look for a request
106 * on any queue on this host, and attempt to issue it. This may
107 * not be the queue we were asked to process.
108 */
Jens Axboe165125e2007-07-24 09:28:11 +0200109static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100112 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100113
114 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800115 while ((req = blk_fetch_request(q)) != NULL) {
116 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900117 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800118 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100119 return;
120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Per Forlinee8a43a2011-07-01 18:55:33 +0200122 if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100123 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
Venkatraman S7513cd72011-08-23 21:16:02 +0530126static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400127{
128 struct scatterlist *sg;
129
130 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
131 if (!sg)
132 *err = -ENOMEM;
133 else {
134 *err = 0;
135 sg_init_table(sg, sg_len);
136 }
137
138 return sg;
139}
140
Adrian Huntere056a1b2011-06-28 17:16:02 +0300141static void mmc_queue_setup_discard(struct request_queue *q,
142 struct mmc_card *card)
143{
144 unsigned max_discard;
145
146 max_discard = mmc_calc_max_discard(card);
147 if (!max_discard)
148 return;
149
150 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
151 q->limits.max_discard_sectors = max_discard;
Adrian Hunter7194efb2012-04-05 14:45:47 +0300152 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300153 q->limits.discard_zeroes_data = 1;
154 q->limits.discard_granularity = card->pref_erase << 9;
155 /* granularity must not be greater than max. discard */
156 if (card->pref_erase > max_discard)
157 q->limits.discard_granularity = 0;
Maya Erez463bb952012-05-24 23:46:29 +0300158 if (mmc_can_secure_erase_trim(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300159 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
160}
161
Maya Erez463bb952012-05-24 23:46:29 +0300162static void mmc_queue_setup_sanitize(struct request_queue *q)
163{
164 queue_flag_set_unlocked(QUEUE_FLAG_SANITIZE, q);
165}
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/**
168 * mmc_init_queue - initialise a queue structure.
169 * @mq: mmc queue
170 * @card: mmc card to attach this queue
171 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300172 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 *
174 * Initialise a MMC card request queue.
175 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300176int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
177 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
179 struct mmc_host *host = card->host;
180 u64 limit = BLK_BOUNCE_HIGH;
181 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400182 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200183 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200185 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
186 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 mq->card = card;
189 mq->queue = blk_init_queue(mmc_request, lock);
190 if (!mq->queue)
191 return -ENOMEM;
192
Maya Erez867be742012-10-29 20:19:01 +0200193 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
194 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
195
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200196 INIT_LIST_HEAD(&mqrq_cur->packed_list);
197 INIT_LIST_HEAD(&mqrq_prev->packed_list);
198
Per Forlin97868a22011-07-09 17:12:36 -0400199 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200200 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 mq->queue->queuedata = mq;
Tatyana Brokhman98e67812012-10-07 09:52:16 +0200202 mq->num_wr_reqs_to_start_packing = DEFAULT_NUM_REQS_TO_START_PACK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Pierre Ossman98ccf142007-05-12 00:26:16 +0200204 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200205 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300206 if (mmc_can_erase(card))
207 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200208
Maya Erez463bb952012-05-24 23:46:29 +0300209 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
210 mmc_queue_setup_sanitize(mq->queue);
211
Pierre Ossman98ccf142007-05-12 00:26:16 +0200212#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400213 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200214 unsigned int bouncesz;
215
Pierre Ossman98ccf142007-05-12 00:26:16 +0200216 bouncesz = MMC_QUEUE_BOUNCESZ;
217
218 if (bouncesz > host->max_req_size)
219 bouncesz = host->max_req_size;
220 if (bouncesz > host->max_seg_size)
221 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200222 if (bouncesz > (host->max_blk_count * 512))
223 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200224
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200225 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400226 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
227 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530228 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400229 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200230 mmc_card_name(card));
231 }
Per Forlin04296b72011-07-01 18:55:31 +0200232 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
233 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530234 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200235 "allocate bounce prev buffer\n",
236 mmc_card_name(card));
237 kfree(mqrq_cur->bounce_buf);
238 mqrq_cur->bounce_buf = NULL;
239 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200240 }
241
Per Forlin04296b72011-07-01 18:55:31 +0200242 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200243 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500244 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500245 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200246 blk_queue_max_segment_size(mq->queue, bouncesz);
247
Per Forlin97868a22011-07-09 17:12:36 -0400248 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
249 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200250 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200251
Per Forlin97868a22011-07-09 17:12:36 -0400252 mqrq_cur->bounce_sg =
253 mmc_alloc_sg(bouncesz / 512, &ret);
254 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200255 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400256
Per Forlin04296b72011-07-01 18:55:31 +0200257 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
258 if (ret)
259 goto cleanup_queue;
260
261 mqrq_prev->bounce_sg =
262 mmc_alloc_sg(bouncesz / 512, &ret);
263 if (ret)
264 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200265 }
266 }
267#endif
268
Per Forlin04296b72011-07-01 18:55:31 +0200269 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200270 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500271 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200272 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400273 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200274 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
275
Per Forlin97868a22011-07-09 17:12:36 -0400276 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
277 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200278 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400279
Per Forlin04296b72011-07-01 18:55:31 +0200280
281 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
282 if (ret)
283 goto cleanup_queue;
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);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200293 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295
Christoph Hellwig87598a22006-11-13 20:23:52 +0100296 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200297 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400298 kfree(mqrq_cur->bounce_sg);
299 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200300 kfree(mqrq_prev->bounce_sg);
301 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400302
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200303 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400304 kfree(mqrq_cur->sg);
305 mqrq_cur->sg = NULL;
306 kfree(mqrq_cur->bounce_buf);
307 mqrq_cur->bounce_buf = NULL;
308
Per Forlin04296b72011-07-01 18:55:31 +0200309 kfree(mqrq_prev->sg);
310 mqrq_prev->sg = NULL;
311 kfree(mqrq_prev->bounce_buf);
312 mqrq_prev->bounce_buf = NULL;
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return ret;
316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318void mmc_cleanup_queue(struct mmc_queue *mq)
319{
Jens Axboe165125e2007-07-24 09:28:11 +0200320 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100321 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400322 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200323 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100324
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200325 /* Make sure the queue isn't suspended, as that will deadlock */
326 mmc_queue_resume(mq);
327
Pierre Ossman89b4e132006-11-14 22:08:16 +0100328 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100329 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800331 /* Empty the queue */
332 spin_lock_irqsave(q->queue_lock, flags);
333 q->queuedata = NULL;
334 blk_start_queue(q);
335 spin_unlock_irqrestore(q->queue_lock, flags);
336
Per Forlin97868a22011-07-09 17:12:36 -0400337 kfree(mqrq_cur->bounce_sg);
338 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200339
Per Forlin97868a22011-07-09 17:12:36 -0400340 kfree(mqrq_cur->sg);
341 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Per Forlin97868a22011-07-09 17:12:36 -0400343 kfree(mqrq_cur->bounce_buf);
344 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200345
Per Forlin04296b72011-07-01 18:55:31 +0200346 kfree(mqrq_prev->bounce_sg);
347 mqrq_prev->bounce_sg = NULL;
348
349 kfree(mqrq_prev->sg);
350 mqrq_prev->sg = NULL;
351
352 kfree(mqrq_prev->bounce_buf);
353 mqrq_prev->bounce_buf = NULL;
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 mq->card = NULL;
356}
357EXPORT_SYMBOL(mmc_cleanup_queue);
358
359/**
360 * mmc_queue_suspend - suspend a MMC request queue
361 * @mq: MMC queue to suspend
362 *
363 * Stop the block request queue, and wait for our thread to
364 * complete any outstanding requests. This ensures that we
365 * won't suspend while a request is being processed.
366 */
367void mmc_queue_suspend(struct mmc_queue *mq)
368{
Jens Axboe165125e2007-07-24 09:28:11 +0200369 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned long flags;
371
372 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
373 mq->flags |= MMC_QUEUE_SUSPENDED;
374
375 spin_lock_irqsave(q->queue_lock, flags);
376 blk_stop_queue(q);
377 spin_unlock_irqrestore(q->queue_lock, flags);
378
379 down(&mq->thread_sem);
380 }
381}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383/**
384 * mmc_queue_resume - resume a previously suspended MMC request queue
385 * @mq: MMC queue to resume
386 */
387void mmc_queue_resume(struct mmc_queue *mq)
388{
Jens Axboe165125e2007-07-24 09:28:11 +0200389 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 unsigned long flags;
391
392 if (mq->flags & MMC_QUEUE_SUSPENDED) {
393 mq->flags &= ~MMC_QUEUE_SUSPENDED;
394
395 up(&mq->thread_sem);
396
397 spin_lock_irqsave(q->queue_lock, flags);
398 blk_start_queue(q);
399 spin_unlock_irqrestore(q->queue_lock, flags);
400 }
401}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100402
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200403static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
404 struct mmc_queue_req *mqrq,
405 struct scatterlist *sg)
406{
407 struct scatterlist *__sg;
408 unsigned int sg_len = 0;
409 struct request *req;
410 enum mmc_packed_cmd cmd;
411
412 cmd = mqrq->packed_cmd;
413
414 if (cmd == MMC_PACKED_WRITE) {
415 __sg = sg;
416 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
417 sizeof(mqrq->packed_cmd_hdr));
418 sg_len++;
419 __sg->page_link &= ~0x02;
420 }
421
422 __sg = sg + sg_len;
423 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
424 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
425 __sg = sg + (sg_len - 1);
426 (__sg++)->page_link &= ~0x02;
427 }
428 sg_mark_end(sg + (sg_len - 1));
429 return sg_len;
430}
431
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200432/*
433 * Prepare the sg list(s) to be handed of to the host driver
434 */
Per Forlin97868a22011-07-09 17:12:36 -0400435unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200436{
437 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200438 size_t buflen;
439 struct scatterlist *sg;
440 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200441
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200442 if (!mqrq->bounce_buf) {
443 if (!list_empty(&mqrq->packed_list))
444 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
445 else
446 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
447 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200448
Per Forlin97868a22011-07-09 17:12:36 -0400449 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200450
Seungwon Jeon121b8e82012-09-27 15:00:26 +0200451 if (!list_empty(&mqrq->packed_list))
452 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
453 else
454 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200455
Per Forlin97868a22011-07-09 17:12:36 -0400456 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200457
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200458 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400459 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200460 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200461
Per Forlin97868a22011-07-09 17:12:36 -0400462 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200463
464 return 1;
465}
466
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200467/*
468 * If writing, bounce the data to the buffer before the request
469 * is sent to the host driver
470 */
Per Forlin97868a22011-07-09 17:12:36 -0400471void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200472{
Per Forlin97868a22011-07-09 17:12:36 -0400473 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200474 return;
475
Per Forlin97868a22011-07-09 17:12:36 -0400476 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200477 return;
478
Per Forlin97868a22011-07-09 17:12:36 -0400479 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
480 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200481}
482
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200483/*
484 * If reading, bounce the data from the buffer after the request
485 * has been handled by the host driver
486 */
Per Forlin97868a22011-07-09 17:12:36 -0400487void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200488{
Per Forlin97868a22011-07-09 17:12:36 -0400489 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200490 return;
491
Per Forlin97868a22011-07-09 17:12:36 -0400492 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200493 return;
494
Per Forlin97868a22011-07-09 17:12:36 -0400495 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
496 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200497}