blob: 9babeabfc898157bd066f9f044691246240442a8 [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;
Lee Susman66842b02012-12-19 14:28:03 +020062 struct mmc_async_event_stats *stats;
63 struct mmc_queue_req *tmp;
64
65 if (!card)
66 return 0;
67
68 stats = &mq->card->async_event_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Rafael J. Wysocki83144182007-07-17 04:03:35 -070070 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 do {
Lee Susman66842b02012-12-19 14:28:03 +020074
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070075 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 spin_lock_irq(q->queue_lock);
78 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010079 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040080 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 spin_unlock_irq(q->queue_lock);
82
Per Forlinee8a43a2011-07-01 18:55:33 +020083 if (req || mq->mqrq_prev->req) {
84 set_current_state(TASK_RUNNING);
85 mq->issue_fn(mq, req);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +020086 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
87 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
Lee Susman66842b02012-12-19 14:28:03 +020088 if (stats && stats->enabled)
89 stats->fetch_due_to_new_req++;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +020090 continue; /* fetch again */
91 }
Seungwon Jeon09f21532012-09-28 19:12:53 +090092
93 /*
94 * Current request becomes previous request
95 * and vice versa.
96 */
97 mq->mqrq_prev->brq.mrq.data = NULL;
98 mq->mqrq_prev->req = NULL;
99 tmp = mq->mqrq_prev;
100 mq->mqrq_prev = mq->mqrq_cur;
101 mq->mqrq_cur = tmp;
Per Forlinee8a43a2011-07-01 18:55:33 +0200102 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +0100103 if (kthread_should_stop()) {
104 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +0100106 }
Maya Erez5f360692012-10-10 03:47:54 +0200107 mmc_start_delayed_bkops(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 up(&mq->thread_sem);
109 schedule();
110 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 up(&mq->thread_sem);
114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return 0;
116}
117
118/*
119 * Generic MMC request handler. This is called for any queue on a
120 * particular host. When the host is not busy, we look for a request
121 * on any queue on this host, and attempt to issue it. This may
122 * not be the queue we were asked to process.
123 */
Jens Axboe165125e2007-07-24 09:28:11 +0200124static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct mmc_queue *mq = q->queuedata;
Lee Susman66842b02012-12-19 14:28:03 +0200127 struct mmc_async_event_stats *stats;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100128 struct request *req;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200129 unsigned long flags;
130 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100131
132 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800133 while ((req = blk_fetch_request(q)) != NULL) {
134 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900135 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800136 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100137 return;
138 }
Lee Susman66842b02012-12-19 14:28:03 +0200139 if (mq->card) {
140 cntx = &mq->card->host->context_info;
141 stats = &mq->card->async_event_stats;
142 } else
143 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200145 cntx = &mq->card->host->context_info;
Lee Susman66842b02012-12-19 14:28:03 +0200146 stats = &mq->card->async_event_stats;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200147 if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
148 /*
149 * New MMC request arrived when MMC thread may be
150 * blocked on the previous request to be complete
151 * with no current request fetched
152 */
Lee Susman66842b02012-12-19 14:28:03 +0200153
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200154 spin_lock_irqsave(&cntx->lock, flags);
155 if (cntx->is_waiting_last_req) {
Lee Susman66842b02012-12-19 14:28:03 +0200156 if (stats && stats->enabled)
157 stats->wakeup_new++;
158 if (cntx->is_new_req)
159 if (stats->enabled)
160 stats->new_req_when_new_marked++;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200161 cntx->is_new_req = true;
162 wake_up_interruptible(&cntx->wait);
Lee Susman66842b02012-12-19 14:28:03 +0200163 } else if (stats->enabled)
164 stats->q_no_waiting++;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200165 spin_unlock_irqrestore(&cntx->lock, flags);
Lee Susman66842b02012-12-19 14:28:03 +0200166 } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req) {
Christoph Hellwig87598a22006-11-13 20:23:52 +0100167 wake_up_process(mq->thread);
Lee Susman66842b02012-12-19 14:28:03 +0200168 if (stats->enabled)
169 stats->wakeup_mq_thread++;
170 } else if (stats->enabled)
171 stats->no_mmc_request_action++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
Venkatraman S7513cd72011-08-23 21:16:02 +0530174static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400175{
176 struct scatterlist *sg;
177
178 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
179 if (!sg)
180 *err = -ENOMEM;
181 else {
182 *err = 0;
183 sg_init_table(sg, sg_len);
184 }
185
186 return sg;
187}
188
Adrian Huntere056a1b2011-06-28 17:16:02 +0300189static void mmc_queue_setup_discard(struct request_queue *q,
190 struct mmc_card *card)
191{
192 unsigned max_discard;
193
194 max_discard = mmc_calc_max_discard(card);
195 if (!max_discard)
196 return;
197
198 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
199 q->limits.max_discard_sectors = max_discard;
Adrian Hunter7194efb2012-04-05 14:45:47 +0300200 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300201 q->limits.discard_zeroes_data = 1;
202 q->limits.discard_granularity = card->pref_erase << 9;
203 /* granularity must not be greater than max. discard */
204 if (card->pref_erase > max_discard)
205 q->limits.discard_granularity = 0;
Maya Erez463bb952012-05-24 23:46:29 +0300206 if (mmc_can_secure_erase_trim(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300207 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
208}
209
Maya Erez463bb952012-05-24 23:46:29 +0300210static void mmc_queue_setup_sanitize(struct request_queue *q)
211{
212 queue_flag_set_unlocked(QUEUE_FLAG_SANITIZE, q);
213}
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/**
216 * mmc_init_queue - initialise a queue structure.
217 * @mq: mmc queue
218 * @card: mmc card to attach this queue
219 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300220 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
222 * Initialise a MMC card request queue.
223 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300224int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
225 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
227 struct mmc_host *host = card->host;
228 u64 limit = BLK_BOUNCE_HIGH;
229 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400230 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200231 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200233 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
234 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 mq->card = card;
237 mq->queue = blk_init_queue(mmc_request, lock);
238 if (!mq->queue)
239 return -ENOMEM;
240
Maya Erez47b37922012-10-29 20:19:01 +0200241 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
242 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
243
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200244 INIT_LIST_HEAD(&mqrq_cur->packed_list);
245 INIT_LIST_HEAD(&mqrq_prev->packed_list);
246
Per Forlin97868a22011-07-09 17:12:36 -0400247 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200248 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 mq->queue->queuedata = mq;
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200250 mq->num_wr_reqs_to_start_packing =
251 min_t(int, (int)card->ext_csd.max_packed_writes,
252 DEFAULT_NUM_REQS_TO_START_PACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Pierre Ossman98ccf142007-05-12 00:26:16 +0200254 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200255 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300256 if (mmc_can_erase(card))
257 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200258
Maya Erez463bb952012-05-24 23:46:29 +0300259 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
260 mmc_queue_setup_sanitize(mq->queue);
261
Pierre Ossman98ccf142007-05-12 00:26:16 +0200262#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400263 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200264 unsigned int bouncesz;
265
Pierre Ossman98ccf142007-05-12 00:26:16 +0200266 bouncesz = MMC_QUEUE_BOUNCESZ;
267
268 if (bouncesz > host->max_req_size)
269 bouncesz = host->max_req_size;
270 if (bouncesz > host->max_seg_size)
271 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200272 if (bouncesz > (host->max_blk_count * 512))
273 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200274
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200275 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400276 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
277 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530278 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400279 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200280 mmc_card_name(card));
281 }
Per Forlin04296b72011-07-01 18:55:31 +0200282 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
283 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530284 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200285 "allocate bounce prev buffer\n",
286 mmc_card_name(card));
287 kfree(mqrq_cur->bounce_buf);
288 mqrq_cur->bounce_buf = NULL;
289 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200290 }
291
Per Forlin04296b72011-07-01 18:55:31 +0200292 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200293 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500294 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500295 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200296 blk_queue_max_segment_size(mq->queue, bouncesz);
297
Per Forlin97868a22011-07-09 17:12:36 -0400298 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
299 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200300 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200301
Per Forlin97868a22011-07-09 17:12:36 -0400302 mqrq_cur->bounce_sg =
303 mmc_alloc_sg(bouncesz / 512, &ret);
304 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200305 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400306
Per Forlin04296b72011-07-01 18:55:31 +0200307 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
308 if (ret)
309 goto cleanup_queue;
310
311 mqrq_prev->bounce_sg =
312 mmc_alloc_sg(bouncesz / 512, &ret);
313 if (ret)
314 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200315 }
316 }
317#endif
318
Per Forlin04296b72011-07-01 18:55:31 +0200319 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200320 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500321 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200322 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400323 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200324 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
325
Per Forlin97868a22011-07-09 17:12:36 -0400326 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
327 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200328 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400329
Per Forlin04296b72011-07-01 18:55:31 +0200330
331 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
332 if (ret)
333 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 }
335
Thomas Gleixner632cf922010-09-14 07:12:35 -0400336 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Adrian Hunterd09408a2011-06-23 13:40:28 +0300338 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
339 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400340
Christoph Hellwig87598a22006-11-13 20:23:52 +0100341 if (IS_ERR(mq->thread)) {
342 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200343 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
Christoph Hellwig87598a22006-11-13 20:23:52 +0100346 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200347 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400348 kfree(mqrq_cur->bounce_sg);
349 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200350 kfree(mqrq_prev->bounce_sg);
351 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400352
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200353 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400354 kfree(mqrq_cur->sg);
355 mqrq_cur->sg = NULL;
356 kfree(mqrq_cur->bounce_buf);
357 mqrq_cur->bounce_buf = NULL;
358
Per Forlin04296b72011-07-01 18:55:31 +0200359 kfree(mqrq_prev->sg);
360 mqrq_prev->sg = NULL;
361 kfree(mqrq_prev->bounce_buf);
362 mqrq_prev->bounce_buf = NULL;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 return ret;
366}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368void mmc_cleanup_queue(struct mmc_queue *mq)
369{
Jens Axboe165125e2007-07-24 09:28:11 +0200370 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100371 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400372 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200373 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100374
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200375 /* Make sure the queue isn't suspended, as that will deadlock */
376 mmc_queue_resume(mq);
377
Pierre Ossman89b4e132006-11-14 22:08:16 +0100378 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100379 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800381 /* Empty the queue */
382 spin_lock_irqsave(q->queue_lock, flags);
383 q->queuedata = NULL;
384 blk_start_queue(q);
385 spin_unlock_irqrestore(q->queue_lock, flags);
386
Per Forlin97868a22011-07-09 17:12:36 -0400387 kfree(mqrq_cur->bounce_sg);
388 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200389
Per Forlin97868a22011-07-09 17:12:36 -0400390 kfree(mqrq_cur->sg);
391 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Per Forlin97868a22011-07-09 17:12:36 -0400393 kfree(mqrq_cur->bounce_buf);
394 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200395
Per Forlin04296b72011-07-01 18:55:31 +0200396 kfree(mqrq_prev->bounce_sg);
397 mqrq_prev->bounce_sg = NULL;
398
399 kfree(mqrq_prev->sg);
400 mqrq_prev->sg = NULL;
401
402 kfree(mqrq_prev->bounce_buf);
403 mqrq_prev->bounce_buf = NULL;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 mq->card = NULL;
406}
407EXPORT_SYMBOL(mmc_cleanup_queue);
408
409/**
410 * mmc_queue_suspend - suspend a MMC request queue
411 * @mq: MMC queue to suspend
412 *
413 * Stop the block request queue, and wait for our thread to
414 * complete any outstanding requests. This ensures that we
415 * won't suspend while a request is being processed.
416 */
417void mmc_queue_suspend(struct mmc_queue *mq)
418{
Jens Axboe165125e2007-07-24 09:28:11 +0200419 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 unsigned long flags;
421
422 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
423 mq->flags |= MMC_QUEUE_SUSPENDED;
424
425 spin_lock_irqsave(q->queue_lock, flags);
426 blk_stop_queue(q);
427 spin_unlock_irqrestore(q->queue_lock, flags);
428
429 down(&mq->thread_sem);
430 }
431}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433/**
434 * mmc_queue_resume - resume a previously suspended MMC request queue
435 * @mq: MMC queue to resume
436 */
437void mmc_queue_resume(struct mmc_queue *mq)
438{
Jens Axboe165125e2007-07-24 09:28:11 +0200439 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 unsigned long flags;
441
442 if (mq->flags & MMC_QUEUE_SUSPENDED) {
443 mq->flags &= ~MMC_QUEUE_SUSPENDED;
444
445 up(&mq->thread_sem);
446
447 spin_lock_irqsave(q->queue_lock, flags);
448 blk_start_queue(q);
449 spin_unlock_irqrestore(q->queue_lock, flags);
450 }
451}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100452
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200453static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
454 struct mmc_queue_req *mqrq,
455 struct scatterlist *sg)
456{
457 struct scatterlist *__sg;
458 unsigned int sg_len = 0;
459 struct request *req;
460 enum mmc_packed_cmd cmd;
461
462 cmd = mqrq->packed_cmd;
463
464 if (cmd == MMC_PACKED_WRITE) {
465 __sg = sg;
466 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
467 sizeof(mqrq->packed_cmd_hdr));
468 sg_len++;
469 __sg->page_link &= ~0x02;
470 }
471
472 __sg = sg + sg_len;
473 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
474 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
475 __sg = sg + (sg_len - 1);
476 (__sg++)->page_link &= ~0x02;
477 }
478 sg_mark_end(sg + (sg_len - 1));
479 return sg_len;
480}
481
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200482/*
483 * Prepare the sg list(s) to be handed of to the host driver
484 */
Per Forlin97868a22011-07-09 17:12:36 -0400485unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486{
487 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200488 size_t buflen;
489 struct scatterlist *sg;
490 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200491
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200492 if (!mqrq->bounce_buf) {
493 if (!list_empty(&mqrq->packed_list))
494 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
495 else
496 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
497 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200498
Per Forlin97868a22011-07-09 17:12:36 -0400499 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200500
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200501 if (!list_empty(&mqrq->packed_list))
502 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
503 else
504 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200505
Per Forlin97868a22011-07-09 17:12:36 -0400506 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200507
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200508 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400509 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200510 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200511
Per Forlin97868a22011-07-09 17:12:36 -0400512 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200513
514 return 1;
515}
516
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200517/*
518 * If writing, bounce the data to the buffer before the request
519 * is sent to the host driver
520 */
Per Forlin97868a22011-07-09 17:12:36 -0400521void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200522{
Per Forlin97868a22011-07-09 17:12:36 -0400523 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200524 return;
525
Per Forlin97868a22011-07-09 17:12:36 -0400526 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200527 return;
528
Per Forlin97868a22011-07-09 17:12:36 -0400529 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
530 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200531}
532
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200533/*
534 * If reading, bounce the data from the buffer after the request
535 * has been handled by the host driver
536 */
Per Forlin97868a22011-07-09 17:12:36 -0400537void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200538{
Per Forlin97868a22011-07-09 17:12:36 -0400539 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200540 return;
541
Per Forlin97868a22011-07-09 17:12:36 -0400542 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200543 return;
544
Per Forlin97868a22011-07-09 17:12:36 -0400545 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
546 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200547}