blob: f0aca7794c6558f7d280fe1ffb195b3b2ac0f4b1 [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 {
Lee Susmand50afb52013-01-09 14:48:47 +020067 struct mmc_queue_req *tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 spin_lock_irq(q->queue_lock);
71 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010072 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040073 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 spin_unlock_irq(q->queue_lock);
75
Per Forlinee8a43a2011-07-01 18:55:33 +020076 if (req || mq->mqrq_prev->req) {
77 set_current_state(TASK_RUNNING);
78 mq->issue_fn(mq, req);
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +020079 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
80 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
81 continue; /* fetch again */
82 }
Seungwon Jeon09f21532012-09-28 19:12:53 +090083
84 /*
85 * Current request becomes previous request
86 * and vice versa.
87 */
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;
Per Forlinee8a43a2011-07-01 18:55:33 +020093 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010094 if (kthread_should_stop()) {
95 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010097 }
Maya Erez5f360692012-10-10 03:47:54 +020098 mmc_start_delayed_bkops(card);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 up(&mq->thread_sem);
100 schedule();
101 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 up(&mq->thread_sem);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return 0;
107}
108
109/*
110 * Generic MMC request handler. This is called for any queue on a
111 * particular host. When the host is not busy, we look for a request
112 * on any queue on this host, and attempt to issue it. This may
113 * not be the queue we were asked to process.
114 */
Jens Axboe165125e2007-07-24 09:28:11 +0200115static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
117 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100118 struct request *req;
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200119 unsigned long flags;
120 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100121
122 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800123 while ((req = blk_fetch_request(q)) != NULL) {
124 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900125 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800126 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100127 return;
128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200130 cntx = &mq->card->host->context_info;
131 if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
132 /*
133 * New MMC request arrived when MMC thread may be
134 * blocked on the previous request to be complete
135 * with no current request fetched
136 */
137 spin_lock_irqsave(&cntx->lock, flags);
138 if (cntx->is_waiting_last_req) {
139 cntx->is_new_req = true;
140 wake_up_interruptible(&cntx->wait);
Lee Susmand50afb52013-01-09 14:48:47 +0200141 }
Konstantin Dorfman69bd0fb2012-12-12 16:12:08 +0200142 spin_unlock_irqrestore(&cntx->lock, flags);
Lee Susmand50afb52013-01-09 14:48:47 +0200143 } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100144 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Venkatraman S7513cd72011-08-23 21:16:02 +0530147static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400148{
149 struct scatterlist *sg;
150
151 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
152 if (!sg)
153 *err = -ENOMEM;
154 else {
155 *err = 0;
156 sg_init_table(sg, sg_len);
157 }
158
159 return sg;
160}
161
Adrian Huntere056a1b2011-06-28 17:16:02 +0300162static void mmc_queue_setup_discard(struct request_queue *q,
163 struct mmc_card *card)
164{
165 unsigned max_discard;
166
167 max_discard = mmc_calc_max_discard(card);
168 if (!max_discard)
169 return;
170
171 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
172 q->limits.max_discard_sectors = max_discard;
Adrian Hunter7194efb2012-04-05 14:45:47 +0300173 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300174 q->limits.discard_zeroes_data = 1;
175 q->limits.discard_granularity = card->pref_erase << 9;
176 /* granularity must not be greater than max. discard */
177 if (card->pref_erase > max_discard)
178 q->limits.discard_granularity = 0;
Maya Erez463bb952012-05-24 23:46:29 +0300179 if (mmc_can_secure_erase_trim(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300180 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
181}
182
Maya Erez463bb952012-05-24 23:46:29 +0300183static void mmc_queue_setup_sanitize(struct request_queue *q)
184{
185 queue_flag_set_unlocked(QUEUE_FLAG_SANITIZE, q);
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/**
189 * mmc_init_queue - initialise a queue structure.
190 * @mq: mmc queue
191 * @card: mmc card to attach this queue
192 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300193 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * Initialise a MMC card request queue.
196 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300197int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
198 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct mmc_host *host = card->host;
201 u64 limit = BLK_BOUNCE_HIGH;
202 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400203 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200204 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200206 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
207 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 mq->card = card;
210 mq->queue = blk_init_queue(mmc_request, lock);
211 if (!mq->queue)
212 return -ENOMEM;
213
Maya Erez47b37922012-10-29 20:19:01 +0200214 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
215 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
216
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200217 INIT_LIST_HEAD(&mqrq_cur->packed_list);
218 INIT_LIST_HEAD(&mqrq_prev->packed_list);
219
Per Forlin97868a22011-07-09 17:12:36 -0400220 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200221 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 mq->queue->queuedata = mq;
Yaniv Gardiec7a9ac2012-11-28 14:52:52 +0200223 mq->num_wr_reqs_to_start_packing =
224 min_t(int, (int)card->ext_csd.max_packed_writes,
225 DEFAULT_NUM_REQS_TO_START_PACK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Pierre Ossman98ccf142007-05-12 00:26:16 +0200227 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200228 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300229 if (mmc_can_erase(card))
230 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200231
Maya Erez463bb952012-05-24 23:46:29 +0300232 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
233 mmc_queue_setup_sanitize(mq->queue);
234
Pierre Ossman98ccf142007-05-12 00:26:16 +0200235#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400236 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200237 unsigned int bouncesz;
238
Pierre Ossman98ccf142007-05-12 00:26:16 +0200239 bouncesz = MMC_QUEUE_BOUNCESZ;
240
241 if (bouncesz > host->max_req_size)
242 bouncesz = host->max_req_size;
243 if (bouncesz > host->max_seg_size)
244 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200245 if (bouncesz > (host->max_blk_count * 512))
246 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200247
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200248 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400249 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
250 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530251 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400252 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200253 mmc_card_name(card));
254 }
Per Forlin04296b72011-07-01 18:55:31 +0200255 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
256 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530257 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200258 "allocate bounce prev buffer\n",
259 mmc_card_name(card));
260 kfree(mqrq_cur->bounce_buf);
261 mqrq_cur->bounce_buf = NULL;
262 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200263 }
264
Per Forlin04296b72011-07-01 18:55:31 +0200265 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200266 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500267 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500268 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200269 blk_queue_max_segment_size(mq->queue, bouncesz);
270
Per Forlin97868a22011-07-09 17:12:36 -0400271 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
272 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200273 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200274
Per Forlin97868a22011-07-09 17:12:36 -0400275 mqrq_cur->bounce_sg =
276 mmc_alloc_sg(bouncesz / 512, &ret);
277 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200278 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400279
Per Forlin04296b72011-07-01 18:55:31 +0200280 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
281 if (ret)
282 goto cleanup_queue;
283
284 mqrq_prev->bounce_sg =
285 mmc_alloc_sg(bouncesz / 512, &ret);
286 if (ret)
287 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200288 }
289 }
290#endif
291
Per Forlin04296b72011-07-01 18:55:31 +0200292 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200293 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500294 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200295 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400296 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200297 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
298
Per Forlin97868a22011-07-09 17:12:36 -0400299 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
300 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200301 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400302
Per Forlin04296b72011-07-01 18:55:31 +0200303
304 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
305 if (ret)
306 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 }
308
Thomas Gleixner632cf922010-09-14 07:12:35 -0400309 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Adrian Hunterd09408a2011-06-23 13:40:28 +0300311 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
312 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400313
Christoph Hellwig87598a22006-11-13 20:23:52 +0100314 if (IS_ERR(mq->thread)) {
315 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200316 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
Christoph Hellwig87598a22006-11-13 20:23:52 +0100319 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200320 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400321 kfree(mqrq_cur->bounce_sg);
322 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200323 kfree(mqrq_prev->bounce_sg);
324 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400325
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200326 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400327 kfree(mqrq_cur->sg);
328 mqrq_cur->sg = NULL;
329 kfree(mqrq_cur->bounce_buf);
330 mqrq_cur->bounce_buf = NULL;
331
Per Forlin04296b72011-07-01 18:55:31 +0200332 kfree(mqrq_prev->sg);
333 mqrq_prev->sg = NULL;
334 kfree(mqrq_prev->bounce_buf);
335 mqrq_prev->bounce_buf = NULL;
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return ret;
339}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341void mmc_cleanup_queue(struct mmc_queue *mq)
342{
Jens Axboe165125e2007-07-24 09:28:11 +0200343 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100344 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400345 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200346 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100347
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200348 /* Make sure the queue isn't suspended, as that will deadlock */
349 mmc_queue_resume(mq);
350
Pierre Ossman89b4e132006-11-14 22:08:16 +0100351 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100352 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800354 /* Empty the queue */
355 spin_lock_irqsave(q->queue_lock, flags);
356 q->queuedata = NULL;
357 blk_start_queue(q);
358 spin_unlock_irqrestore(q->queue_lock, flags);
359
Per Forlin97868a22011-07-09 17:12:36 -0400360 kfree(mqrq_cur->bounce_sg);
361 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200362
Per Forlin97868a22011-07-09 17:12:36 -0400363 kfree(mqrq_cur->sg);
364 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Per Forlin97868a22011-07-09 17:12:36 -0400366 kfree(mqrq_cur->bounce_buf);
367 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200368
Per Forlin04296b72011-07-01 18:55:31 +0200369 kfree(mqrq_prev->bounce_sg);
370 mqrq_prev->bounce_sg = NULL;
371
372 kfree(mqrq_prev->sg);
373 mqrq_prev->sg = NULL;
374
375 kfree(mqrq_prev->bounce_buf);
376 mqrq_prev->bounce_buf = NULL;
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 mq->card = NULL;
379}
380EXPORT_SYMBOL(mmc_cleanup_queue);
381
382/**
383 * mmc_queue_suspend - suspend a MMC request queue
384 * @mq: MMC queue to suspend
385 *
386 * Stop the block request queue, and wait for our thread to
387 * complete any outstanding requests. This ensures that we
388 * won't suspend while a request is being processed.
389 */
Subhash Jadavanid1c87f12013-02-26 17:32:58 +0530390int mmc_queue_suspend(struct mmc_queue *mq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Jens Axboe165125e2007-07-24 09:28:11 +0200392 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 unsigned long flags;
Subhash Jadavanid1c87f12013-02-26 17:32:58 +0530394 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
396 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
397 mq->flags |= MMC_QUEUE_SUSPENDED;
398
399 spin_lock_irqsave(q->queue_lock, flags);
400 blk_stop_queue(q);
401 spin_unlock_irqrestore(q->queue_lock, flags);
402
Subhash Jadavanid1c87f12013-02-26 17:32:58 +0530403 rc = down_trylock(&mq->thread_sem);
404 if (rc) {
405 /*
406 * Failed to take the lock so better to abort the
407 * suspend because mmcqd thread is processing requests.
408 */
409 mq->flags &= ~MMC_QUEUE_SUSPENDED;
410 spin_lock_irqsave(q->queue_lock, flags);
411 blk_start_queue(q);
412 spin_unlock_irqrestore(q->queue_lock, flags);
413 rc = -EBUSY;
414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
Subhash Jadavanid1c87f12013-02-26 17:32:58 +0530416 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
419/**
420 * mmc_queue_resume - resume a previously suspended MMC request queue
421 * @mq: MMC queue to resume
422 */
423void mmc_queue_resume(struct mmc_queue *mq)
424{
Jens Axboe165125e2007-07-24 09:28:11 +0200425 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 unsigned long flags;
427
428 if (mq->flags & MMC_QUEUE_SUSPENDED) {
429 mq->flags &= ~MMC_QUEUE_SUSPENDED;
430
431 up(&mq->thread_sem);
432
433 spin_lock_irqsave(q->queue_lock, flags);
434 blk_start_queue(q);
435 spin_unlock_irqrestore(q->queue_lock, flags);
436 }
437}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100438
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200439static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
440 struct mmc_queue_req *mqrq,
441 struct scatterlist *sg)
442{
443 struct scatterlist *__sg;
444 unsigned int sg_len = 0;
445 struct request *req;
446 enum mmc_packed_cmd cmd;
447
448 cmd = mqrq->packed_cmd;
449
450 if (cmd == MMC_PACKED_WRITE) {
451 __sg = sg;
452 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
453 sizeof(mqrq->packed_cmd_hdr));
454 sg_len++;
455 __sg->page_link &= ~0x02;
456 }
457
458 __sg = sg + sg_len;
459 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
460 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
461 __sg = sg + (sg_len - 1);
462 (__sg++)->page_link &= ~0x02;
463 }
464 sg_mark_end(sg + (sg_len - 1));
465 return sg_len;
466}
467
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200468/*
469 * Prepare the sg list(s) to be handed of to the host driver
470 */
Per Forlin97868a22011-07-09 17:12:36 -0400471unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200472{
473 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200474 size_t buflen;
475 struct scatterlist *sg;
476 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200477
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200478 if (!mqrq->bounce_buf) {
479 if (!list_empty(&mqrq->packed_list))
480 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
481 else
482 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
483 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200484
Per Forlin97868a22011-07-09 17:12:36 -0400485 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200487 if (!list_empty(&mqrq->packed_list))
488 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
489 else
490 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200491
Per Forlin97868a22011-07-09 17:12:36 -0400492 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200493
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200494 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400495 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200496 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200497
Per Forlin97868a22011-07-09 17:12:36 -0400498 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200499
500 return 1;
501}
502
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200503/*
504 * If writing, bounce the data to the buffer before the request
505 * is sent to the host driver
506 */
Per Forlin97868a22011-07-09 17:12:36 -0400507void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200508{
Per Forlin97868a22011-07-09 17:12:36 -0400509 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200510 return;
511
Per Forlin97868a22011-07-09 17:12:36 -0400512 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200513 return;
514
Per Forlin97868a22011-07-09 17:12:36 -0400515 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
516 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200517}
518
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200519/*
520 * If reading, bounce the data from the buffer after the request
521 * has been handled by the host driver
522 */
Per Forlin97868a22011-07-09 17:12:36 -0400523void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200524{
Per Forlin97868a22011-07-09 17:12:36 -0400525 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200526 return;
527
Per Forlin97868a22011-07-09 17:12:36 -0400528 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200529 return;
530
Per Forlin97868a22011-07-09 17:12:36 -0400531 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
532 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200533}