blob: c762ed1768ac2bd8d6bf472c3f2bc7a7a9c1f23b [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
26/*
Tatyana Brokhman0cc76402012-10-07 09:52:16 +020027 * Based on benchmark tests the default num of requests to trigger the write
28 * packing was determined, to keep the read latency as low as possible and
29 * manage to keep the high write throughput.
30 */
31#define DEFAULT_NUM_REQS_TO_START_PACK 17
32
33/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020034 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 */
36static int mmc_prep_request(struct request_queue *q, struct request *req)
37{
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +053038 struct mmc_queue *mq = q->queuedata;
39
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020040 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070041 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020042 */
Adrian Hunterbd788c92010-08-11 14:17:47 -070043 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020045 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
47
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +053048 if (mq && mmc_card_removed(mq->card))
49 return BLKPREP_KILL;
50
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020051 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020053 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
56static int mmc_queue_thread(void *d)
57{
58 struct mmc_queue *mq = d;
59 struct request_queue *q = mq->queue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070060 struct request *req;
Maya Erez5f360692012-10-10 03:47:54 +020061 struct mmc_card *card = mq->card;
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;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +020074 if (!req && mq->mqrq_prev->req &&
75 !(mq->mqrq_prev->req->cmd_flags & REQ_SANITIZE) &&
76 !(mq->mqrq_prev->req->cmd_flags & REQ_FLUSH) &&
77 !(mq->mqrq_prev->req->cmd_flags & REQ_DISCARD))
78 card->host->context_info.is_waiting_last_req = true;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 spin_unlock_irq(q->queue_lock);
81
Per Forlinee8a43a2011-07-01 18:55:33 +020082 if (req || mq->mqrq_prev->req) {
Maya Erez5f360692012-10-10 03:47:54 +020083 /*
84 * If this is the first request, BKOPs might be in
85 * progress and needs to be stopped before issuing the
86 * request
87 */
88 if (card->ext_csd.bkops_en &&
89 card->bkops_info.started_delayed_bkops) {
90 card->bkops_info.started_delayed_bkops = false;
91 mmc_stop_bkops(card);
92 }
93
Per Forlinee8a43a2011-07-01 18:55:33 +020094 set_current_state(TASK_RUNNING);
95 mq->issue_fn(mq, req);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +020096 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
97 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
98 continue; /* fetch again */
99 }
Per Forlinee8a43a2011-07-01 18:55:33 +0200100 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +0100101 if (kthread_should_stop()) {
102 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +0100104 }
Maya Erez5f360692012-10-10 03:47:54 +0200105 mmc_start_delayed_bkops(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 up(&mq->thread_sem);
107 schedule();
108 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Per Forlinee8a43a2011-07-01 18:55:33 +0200111 /* Current request becomes previous request and vice versa. */
112 mq->mqrq_prev->brq.mrq.data = NULL;
113 mq->mqrq_prev->req = NULL;
114 tmp = mq->mqrq_prev;
115 mq->mqrq_prev = mq->mqrq_cur;
116 mq->mqrq_cur = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 up(&mq->thread_sem);
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121}
122
123/*
124 * Generic MMC request handler. This is called for any queue on a
125 * particular host. When the host is not busy, we look for a request
126 * on any queue on this host, and attempt to issue it. This may
127 * not be the queue we were asked to process.
128 */
Jens Axboe165125e2007-07-24 09:28:11 +0200129static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100132 struct request *req;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200133 unsigned long flags;
134 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100135
136 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800137 while ((req = blk_fetch_request(q)) != NULL) {
138 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900139 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800140 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100141 return;
142 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200144 cntx = &mq->card->host->context_info;
145 if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
146 /*
147 * New MMC request arrived when MMC thread may be
148 * blocked on the previous request to be complete
149 * with no current request fetched
150 */
151 spin_lock_irqsave(&cntx->lock, flags);
152 if (cntx->is_waiting_last_req) {
153 cntx->is_new_req = true;
154 wake_up_interruptible(&cntx->wait);
155 }
156 spin_unlock_irqrestore(&cntx->lock, flags);
157 } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100158 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
Venkatraman S7513cd72011-08-23 21:16:02 +0530161static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400162{
163 struct scatterlist *sg;
164
165 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
166 if (!sg)
167 *err = -ENOMEM;
168 else {
169 *err = 0;
170 sg_init_table(sg, sg_len);
171 }
172
173 return sg;
174}
175
Adrian Huntere056a1b2011-06-28 17:16:02 +0300176static void mmc_queue_setup_discard(struct request_queue *q,
177 struct mmc_card *card)
178{
179 unsigned max_discard;
180
181 max_discard = mmc_calc_max_discard(card);
182 if (!max_discard)
183 return;
184
185 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
186 q->limits.max_discard_sectors = max_discard;
Adrian Hunter7194efb2012-04-05 14:45:47 +0300187 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300188 q->limits.discard_zeroes_data = 1;
189 q->limits.discard_granularity = card->pref_erase << 9;
190 /* granularity must not be greater than max. discard */
191 if (card->pref_erase > max_discard)
192 q->limits.discard_granularity = 0;
Maya Erez463bb952012-05-24 23:46:29 +0300193 if (mmc_can_secure_erase_trim(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300194 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
195}
196
Maya Erez463bb952012-05-24 23:46:29 +0300197static void mmc_queue_setup_sanitize(struct request_queue *q)
198{
199 queue_flag_set_unlocked(QUEUE_FLAG_SANITIZE, q);
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/**
203 * mmc_init_queue - initialise a queue structure.
204 * @mq: mmc queue
205 * @card: mmc card to attach this queue
206 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300207 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 *
209 * Initialise a MMC card request queue.
210 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300211int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
212 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct mmc_host *host = card->host;
215 u64 limit = BLK_BOUNCE_HIGH;
216 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400217 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200218 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200220 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
221 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 mq->card = card;
224 mq->queue = blk_init_queue(mmc_request, lock);
225 if (!mq->queue)
226 return -ENOMEM;
227
Maya Erez47b37922012-10-29 20:19:01 +0200228 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
229 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
230
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200231 INIT_LIST_HEAD(&mqrq_cur->packed_list);
232 INIT_LIST_HEAD(&mqrq_prev->packed_list);
233
Per Forlin97868a22011-07-09 17:12:36 -0400234 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200235 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 mq->queue->queuedata = mq;
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200237 mq->num_wr_reqs_to_start_packing =
238 min_t(int, (int)card->ext_csd.max_packed_writes,
239 DEFAULT_NUM_REQS_TO_START_PACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Pierre Ossman98ccf142007-05-12 00:26:16 +0200241 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200242 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300243 if (mmc_can_erase(card))
244 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200245
Maya Erez463bb952012-05-24 23:46:29 +0300246 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
247 mmc_queue_setup_sanitize(mq->queue);
248
Pierre Ossman98ccf142007-05-12 00:26:16 +0200249#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400250 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200251 unsigned int bouncesz;
252
Pierre Ossman98ccf142007-05-12 00:26:16 +0200253 bouncesz = MMC_QUEUE_BOUNCESZ;
254
255 if (bouncesz > host->max_req_size)
256 bouncesz = host->max_req_size;
257 if (bouncesz > host->max_seg_size)
258 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200259 if (bouncesz > (host->max_blk_count * 512))
260 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200261
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200262 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400263 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
264 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530265 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400266 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200267 mmc_card_name(card));
268 }
Per Forlin04296b72011-07-01 18:55:31 +0200269 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
270 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530271 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200272 "allocate bounce prev buffer\n",
273 mmc_card_name(card));
274 kfree(mqrq_cur->bounce_buf);
275 mqrq_cur->bounce_buf = NULL;
276 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200277 }
278
Per Forlin04296b72011-07-01 18:55:31 +0200279 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200280 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500281 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500282 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200283 blk_queue_max_segment_size(mq->queue, bouncesz);
284
Per Forlin97868a22011-07-09 17:12:36 -0400285 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
286 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200287 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200288
Per Forlin97868a22011-07-09 17:12:36 -0400289 mqrq_cur->bounce_sg =
290 mmc_alloc_sg(bouncesz / 512, &ret);
291 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200292 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400293
Per Forlin04296b72011-07-01 18:55:31 +0200294 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
295 if (ret)
296 goto cleanup_queue;
297
298 mqrq_prev->bounce_sg =
299 mmc_alloc_sg(bouncesz / 512, &ret);
300 if (ret)
301 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200302 }
303 }
304#endif
305
Per Forlin04296b72011-07-01 18:55:31 +0200306 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200307 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500308 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200309 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400310 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200311 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
312
Per Forlin97868a22011-07-09 17:12:36 -0400313 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
314 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200315 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400316
Per Forlin04296b72011-07-01 18:55:31 +0200317
318 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
319 if (ret)
320 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 }
322
Thomas Gleixner632cf922010-09-14 07:12:35 -0400323 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Adrian Hunterd09408a2011-06-23 13:40:28 +0300325 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
326 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400327
Christoph Hellwig87598a22006-11-13 20:23:52 +0100328 if (IS_ERR(mq->thread)) {
329 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200330 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
Christoph Hellwig87598a22006-11-13 20:23:52 +0100333 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200334 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400335 kfree(mqrq_cur->bounce_sg);
336 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200337 kfree(mqrq_prev->bounce_sg);
338 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400339
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200340 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400341 kfree(mqrq_cur->sg);
342 mqrq_cur->sg = NULL;
343 kfree(mqrq_cur->bounce_buf);
344 mqrq_cur->bounce_buf = NULL;
345
Per Forlin04296b72011-07-01 18:55:31 +0200346 kfree(mqrq_prev->sg);
347 mqrq_prev->sg = NULL;
348 kfree(mqrq_prev->bounce_buf);
349 mqrq_prev->bounce_buf = NULL;
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return ret;
353}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355void mmc_cleanup_queue(struct mmc_queue *mq)
356{
Jens Axboe165125e2007-07-24 09:28:11 +0200357 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100358 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400359 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200360 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100361
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200362 /* Make sure the queue isn't suspended, as that will deadlock */
363 mmc_queue_resume(mq);
364
Pierre Ossman89b4e132006-11-14 22:08:16 +0100365 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100366 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800368 /* Empty the queue */
369 spin_lock_irqsave(q->queue_lock, flags);
370 q->queuedata = NULL;
371 blk_start_queue(q);
372 spin_unlock_irqrestore(q->queue_lock, flags);
373
Per Forlin97868a22011-07-09 17:12:36 -0400374 kfree(mqrq_cur->bounce_sg);
375 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200376
Per Forlin97868a22011-07-09 17:12:36 -0400377 kfree(mqrq_cur->sg);
378 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Per Forlin97868a22011-07-09 17:12:36 -0400380 kfree(mqrq_cur->bounce_buf);
381 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200382
Per Forlin04296b72011-07-01 18:55:31 +0200383 kfree(mqrq_prev->bounce_sg);
384 mqrq_prev->bounce_sg = NULL;
385
386 kfree(mqrq_prev->sg);
387 mqrq_prev->sg = NULL;
388
389 kfree(mqrq_prev->bounce_buf);
390 mqrq_prev->bounce_buf = NULL;
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 mq->card = NULL;
393}
394EXPORT_SYMBOL(mmc_cleanup_queue);
395
396/**
397 * mmc_queue_suspend - suspend a MMC request queue
398 * @mq: MMC queue to suspend
399 *
400 * Stop the block request queue, and wait for our thread to
401 * complete any outstanding requests. This ensures that we
402 * won't suspend while a request is being processed.
403 */
404void mmc_queue_suspend(struct mmc_queue *mq)
405{
Jens Axboe165125e2007-07-24 09:28:11 +0200406 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 unsigned long flags;
408
409 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
410 mq->flags |= MMC_QUEUE_SUSPENDED;
411
412 spin_lock_irqsave(q->queue_lock, flags);
413 blk_stop_queue(q);
414 spin_unlock_irqrestore(q->queue_lock, flags);
415
416 down(&mq->thread_sem);
417 }
418}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420/**
421 * mmc_queue_resume - resume a previously suspended MMC request queue
422 * @mq: MMC queue to resume
423 */
424void mmc_queue_resume(struct mmc_queue *mq)
425{
Jens Axboe165125e2007-07-24 09:28:11 +0200426 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 unsigned long flags;
428
429 if (mq->flags & MMC_QUEUE_SUSPENDED) {
430 mq->flags &= ~MMC_QUEUE_SUSPENDED;
431
432 up(&mq->thread_sem);
433
434 spin_lock_irqsave(q->queue_lock, flags);
435 blk_start_queue(q);
436 spin_unlock_irqrestore(q->queue_lock, flags);
437 }
438}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100439
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200440static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
441 struct mmc_queue_req *mqrq,
442 struct scatterlist *sg)
443{
444 struct scatterlist *__sg;
445 unsigned int sg_len = 0;
446 struct request *req;
447 enum mmc_packed_cmd cmd;
448
449 cmd = mqrq->packed_cmd;
450
451 if (cmd == MMC_PACKED_WRITE) {
452 __sg = sg;
453 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
454 sizeof(mqrq->packed_cmd_hdr));
455 sg_len++;
456 __sg->page_link &= ~0x02;
457 }
458
459 __sg = sg + sg_len;
460 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
461 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
462 __sg = sg + (sg_len - 1);
463 (__sg++)->page_link &= ~0x02;
464 }
465 sg_mark_end(sg + (sg_len - 1));
466 return sg_len;
467}
468
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200469/*
470 * Prepare the sg list(s) to be handed of to the host driver
471 */
Per Forlin97868a22011-07-09 17:12:36 -0400472unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200473{
474 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200475 size_t buflen;
476 struct scatterlist *sg;
477 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200478
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200479 if (!mqrq->bounce_buf) {
480 if (!list_empty(&mqrq->packed_list))
481 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
482 else
483 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
484 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200485
Per Forlin97868a22011-07-09 17:12:36 -0400486 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200487
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200488 if (!list_empty(&mqrq->packed_list))
489 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
490 else
491 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200492
Per Forlin97868a22011-07-09 17:12:36 -0400493 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200494
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200495 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400496 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200497 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200498
Per Forlin97868a22011-07-09 17:12:36 -0400499 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200500
501 return 1;
502}
503
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200504/*
505 * If writing, bounce the data to the buffer before the request
506 * is sent to the host driver
507 */
Per Forlin97868a22011-07-09 17:12:36 -0400508void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200509{
Per Forlin97868a22011-07-09 17:12:36 -0400510 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200511 return;
512
Per Forlin97868a22011-07-09 17:12:36 -0400513 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200514 return;
515
Per Forlin97868a22011-07-09 17:12:36 -0400516 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
517 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200518}
519
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200520/*
521 * If reading, bounce the data from the buffer after the request
522 * has been handled by the host driver
523 */
Per Forlin97868a22011-07-09 17:12:36 -0400524void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200525{
Per Forlin97868a22011-07-09 17:12:36 -0400526 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200527 return;
528
Per Forlin97868a22011-07-09 17:12:36 -0400529 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200530 return;
531
Per Forlin97868a22011-07-09 17:12:36 -0400532 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
533 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200534}