blob: d818fc4bb6a731d441358da75cbf7bd651240a75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/card/queue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
Pierre Ossman98ac2162006-12-23 20:03:02 +01005 * Copyright 2006-2007 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 */
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/blkdev.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070015#include <linux/freezer.h>
Christoph Hellwig87598a22006-11-13 20:23:52 +010016#include <linux/kthread.h>
Jens Axboe45711f12007-10-22 21:19:53 +020017#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include <linux/mmc/card.h>
20#include <linux/mmc/host.h>
Pierre Ossman98ac2162006-12-23 20:03:02 +010021#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Pierre Ossman98ccf142007-05-12 00:26:16 +020023#define MMC_QUEUE_BOUNCESZ 65536
24
Christoph Hellwig87598a22006-11-13 20:23:52 +010025#define MMC_QUEUE_SUSPENDED (1 << 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020028 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 */
30static int mmc_prep_request(struct request_queue *q, struct request *req)
31{
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +053032 struct mmc_queue *mq = q->queuedata;
33
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020034 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070035 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020036 */
Adrian Hunterbd788c92010-08-11 14:17:47 -070037 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020039 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 }
41
Sujit Reddy Thummaa8ad82c2011-12-08 14:05:50 +053042 if (mq && mmc_card_removed(mq->card))
43 return BLKPREP_KILL;
44
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020045 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020047 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
50static int mmc_queue_thread(void *d)
51{
52 struct mmc_queue *mq = d;
53 struct request_queue *q = mq->queue;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070054 struct request *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Rafael J. Wysocki83144182007-07-17 04:03:35 -070056 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 do {
Per Forlinee8a43a2011-07-01 18:55:33 +020060 struct mmc_queue_req *tmp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070061 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 spin_lock_irq(q->queue_lock);
64 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010065 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040066 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 spin_unlock_irq(q->queue_lock);
68
Per Forlinee8a43a2011-07-01 18:55:33 +020069 if (req || mq->mqrq_prev->req) {
70 set_current_state(TASK_RUNNING);
71 mq->issue_fn(mq, req);
72 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010073 if (kthread_should_stop()) {
74 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 up(&mq->thread_sem);
78 schedule();
79 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
Per Forlinee8a43a2011-07-01 18:55:33 +020082 /* Current request becomes previous request and vice versa. */
83 mq->mqrq_prev->brq.mrq.data = NULL;
84 mq->mqrq_prev->req = NULL;
85 tmp = mq->mqrq_prev;
86 mq->mqrq_prev = mq->mqrq_cur;
87 mq->mqrq_cur = tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 up(&mq->thread_sem);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 return 0;
92}
93
94/*
95 * Generic MMC request handler. This is called for any queue on a
96 * particular host. When the host is not busy, we look for a request
97 * on any queue on this host, and attempt to issue it. This may
98 * not be the queue we were asked to process.
99 */
Jens Axboe165125e2007-07-24 09:28:11 +0200100static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100103 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100104
105 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800106 while ((req = blk_fetch_request(q)) != NULL) {
107 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900108 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800109 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100110 return;
111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Per Forlinee8a43a2011-07-01 18:55:33 +0200113 if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100114 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
116
Venkatraman S7513cd72011-08-23 21:16:02 +0530117static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400118{
119 struct scatterlist *sg;
120
121 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
122 if (!sg)
123 *err = -ENOMEM;
124 else {
125 *err = 0;
126 sg_init_table(sg, sg_len);
127 }
128
129 return sg;
130}
131
Adrian Huntere056a1b2011-06-28 17:16:02 +0300132static void mmc_queue_setup_discard(struct request_queue *q,
133 struct mmc_card *card)
134{
135 unsigned max_discard;
136
137 max_discard = mmc_calc_max_discard(card);
138 if (!max_discard)
139 return;
140
141 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
142 q->limits.max_discard_sectors = max_discard;
Adrian Hunter7194efb2012-04-05 14:45:47 +0300143 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300144 q->limits.discard_zeroes_data = 1;
145 q->limits.discard_granularity = card->pref_erase << 9;
146 /* granularity must not be greater than max. discard */
147 if (card->pref_erase > max_discard)
148 q->limits.discard_granularity = 0;
Maya Erez463bb952012-05-24 23:46:29 +0300149 if (mmc_can_secure_erase_trim(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300150 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
151}
152
Maya Erez463bb952012-05-24 23:46:29 +0300153static void mmc_queue_setup_sanitize(struct request_queue *q)
154{
155 queue_flag_set_unlocked(QUEUE_FLAG_SANITIZE, q);
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/**
159 * mmc_init_queue - initialise a queue structure.
160 * @mq: mmc queue
161 * @card: mmc card to attach this queue
162 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300163 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
165 * Initialise a MMC card request queue.
166 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300167int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
168 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 struct mmc_host *host = card->host;
171 u64 limit = BLK_BOUNCE_HIGH;
172 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400173 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200174 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200176 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
177 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178
179 mq->card = card;
180 mq->queue = blk_init_queue(mmc_request, lock);
181 if (!mq->queue)
182 return -ENOMEM;
183
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200184 INIT_LIST_HEAD(&mqrq_cur->packed_list);
185 INIT_LIST_HEAD(&mqrq_prev->packed_list);
186
Per Forlin97868a22011-07-09 17:12:36 -0400187 memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
Per Forlin04296b72011-07-01 18:55:31 +0200188 memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
Per Forlin97868a22011-07-09 17:12:36 -0400189 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200190 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Pierre Ossman98ccf142007-05-12 00:26:16 +0200193 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200194 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300195 if (mmc_can_erase(card))
196 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200197
Maya Erez463bb952012-05-24 23:46:29 +0300198 if ((mmc_can_sanitize(card) && (host->caps2 & MMC_CAP2_SANITIZE)))
199 mmc_queue_setup_sanitize(mq->queue);
200
Pierre Ossman98ccf142007-05-12 00:26:16 +0200201#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400202 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200203 unsigned int bouncesz;
204
Pierre Ossman98ccf142007-05-12 00:26:16 +0200205 bouncesz = MMC_QUEUE_BOUNCESZ;
206
207 if (bouncesz > host->max_req_size)
208 bouncesz = host->max_req_size;
209 if (bouncesz > host->max_seg_size)
210 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200211 if (bouncesz > (host->max_blk_count * 512))
212 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200213
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200214 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400215 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
216 if (!mqrq_cur->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530217 pr_warning("%s: unable to "
Per Forlin97868a22011-07-09 17:12:36 -0400218 "allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200219 mmc_card_name(card));
220 }
Per Forlin04296b72011-07-01 18:55:31 +0200221 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
222 if (!mqrq_prev->bounce_buf) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530223 pr_warning("%s: unable to "
Per Forlin04296b72011-07-01 18:55:31 +0200224 "allocate bounce prev buffer\n",
225 mmc_card_name(card));
226 kfree(mqrq_cur->bounce_buf);
227 mqrq_cur->bounce_buf = NULL;
228 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200229 }
230
Per Forlin04296b72011-07-01 18:55:31 +0200231 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200232 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500233 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500234 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200235 blk_queue_max_segment_size(mq->queue, bouncesz);
236
Per Forlin97868a22011-07-09 17:12:36 -0400237 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
238 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200239 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200240
Per Forlin97868a22011-07-09 17:12:36 -0400241 mqrq_cur->bounce_sg =
242 mmc_alloc_sg(bouncesz / 512, &ret);
243 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200244 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400245
Per Forlin04296b72011-07-01 18:55:31 +0200246 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
247 if (ret)
248 goto cleanup_queue;
249
250 mqrq_prev->bounce_sg =
251 mmc_alloc_sg(bouncesz / 512, &ret);
252 if (ret)
253 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200254 }
255 }
256#endif
257
Per Forlin04296b72011-07-01 18:55:31 +0200258 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200259 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500260 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200261 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400262 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200263 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
264
Per Forlin97868a22011-07-09 17:12:36 -0400265 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
266 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200267 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400268
Per Forlin04296b72011-07-01 18:55:31 +0200269
270 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
271 if (ret)
272 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Thomas Gleixner632cf922010-09-14 07:12:35 -0400275 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Adrian Hunterd09408a2011-06-23 13:40:28 +0300277 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
278 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400279
Christoph Hellwig87598a22006-11-13 20:23:52 +0100280 if (IS_ERR(mq->thread)) {
281 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200282 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
Christoph Hellwig87598a22006-11-13 20:23:52 +0100285 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200286 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400287 kfree(mqrq_cur->bounce_sg);
288 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200289 kfree(mqrq_prev->bounce_sg);
290 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400291
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200292 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400293 kfree(mqrq_cur->sg);
294 mqrq_cur->sg = NULL;
295 kfree(mqrq_cur->bounce_buf);
296 mqrq_cur->bounce_buf = NULL;
297
Per Forlin04296b72011-07-01 18:55:31 +0200298 kfree(mqrq_prev->sg);
299 mqrq_prev->sg = NULL;
300 kfree(mqrq_prev->bounce_buf);
301 mqrq_prev->bounce_buf = NULL;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return ret;
305}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307void mmc_cleanup_queue(struct mmc_queue *mq)
308{
Jens Axboe165125e2007-07-24 09:28:11 +0200309 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100310 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400311 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200312 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100313
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200314 /* Make sure the queue isn't suspended, as that will deadlock */
315 mmc_queue_resume(mq);
316
Pierre Ossman89b4e132006-11-14 22:08:16 +0100317 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100318 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800320 /* Empty the queue */
321 spin_lock_irqsave(q->queue_lock, flags);
322 q->queuedata = NULL;
323 blk_start_queue(q);
324 spin_unlock_irqrestore(q->queue_lock, flags);
325
Per Forlin97868a22011-07-09 17:12:36 -0400326 kfree(mqrq_cur->bounce_sg);
327 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200328
Per Forlin97868a22011-07-09 17:12:36 -0400329 kfree(mqrq_cur->sg);
330 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Per Forlin97868a22011-07-09 17:12:36 -0400332 kfree(mqrq_cur->bounce_buf);
333 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200334
Per Forlin04296b72011-07-01 18:55:31 +0200335 kfree(mqrq_prev->bounce_sg);
336 mqrq_prev->bounce_sg = NULL;
337
338 kfree(mqrq_prev->sg);
339 mqrq_prev->sg = NULL;
340
341 kfree(mqrq_prev->bounce_buf);
342 mqrq_prev->bounce_buf = NULL;
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 mq->card = NULL;
345}
346EXPORT_SYMBOL(mmc_cleanup_queue);
347
348/**
349 * mmc_queue_suspend - suspend a MMC request queue
350 * @mq: MMC queue to suspend
351 *
352 * Stop the block request queue, and wait for our thread to
353 * complete any outstanding requests. This ensures that we
354 * won't suspend while a request is being processed.
355 */
356void mmc_queue_suspend(struct mmc_queue *mq)
357{
Jens Axboe165125e2007-07-24 09:28:11 +0200358 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 unsigned long flags;
360
361 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
362 mq->flags |= MMC_QUEUE_SUSPENDED;
363
364 spin_lock_irqsave(q->queue_lock, flags);
365 blk_stop_queue(q);
366 spin_unlock_irqrestore(q->queue_lock, flags);
367
368 down(&mq->thread_sem);
369 }
370}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/**
373 * mmc_queue_resume - resume a previously suspended MMC request queue
374 * @mq: MMC queue to resume
375 */
376void mmc_queue_resume(struct mmc_queue *mq)
377{
Jens Axboe165125e2007-07-24 09:28:11 +0200378 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 unsigned long flags;
380
381 if (mq->flags & MMC_QUEUE_SUSPENDED) {
382 mq->flags &= ~MMC_QUEUE_SUSPENDED;
383
384 up(&mq->thread_sem);
385
386 spin_lock_irqsave(q->queue_lock, flags);
387 blk_start_queue(q);
388 spin_unlock_irqrestore(q->queue_lock, flags);
389 }
390}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100391
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200392static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
393 struct mmc_queue_req *mqrq,
394 struct scatterlist *sg)
395{
396 struct scatterlist *__sg;
397 unsigned int sg_len = 0;
398 struct request *req;
399 enum mmc_packed_cmd cmd;
400
401 cmd = mqrq->packed_cmd;
402
403 if (cmd == MMC_PACKED_WRITE) {
404 __sg = sg;
405 sg_set_buf(__sg, mqrq->packed_cmd_hdr,
406 sizeof(mqrq->packed_cmd_hdr));
407 sg_len++;
408 __sg->page_link &= ~0x02;
409 }
410
411 __sg = sg + sg_len;
412 list_for_each_entry(req, &mqrq->packed_list, queuelist) {
413 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
414 __sg = sg + (sg_len - 1);
415 (__sg++)->page_link &= ~0x02;
416 }
417 sg_mark_end(sg + (sg_len - 1));
418 return sg_len;
419}
420
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200421/*
422 * Prepare the sg list(s) to be handed of to the host driver
423 */
Per Forlin97868a22011-07-09 17:12:36 -0400424unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200425{
426 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200427 size_t buflen;
428 struct scatterlist *sg;
429 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200430
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200431 if (!mqrq->bounce_buf) {
432 if (!list_empty(&mqrq->packed_list))
433 return mmc_queue_packed_map_sg(mq, mqrq, mqrq->sg);
434 else
435 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
436 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200437
Per Forlin97868a22011-07-09 17:12:36 -0400438 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200439
Seungwon Jeon53f8f572012-09-27 15:00:26 +0200440 if (!list_empty(&mqrq->packed_list))
441 sg_len = mmc_queue_packed_map_sg(mq, mqrq, mqrq->bounce_sg);
442 else
443 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200444
Per Forlin97868a22011-07-09 17:12:36 -0400445 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200446
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200447 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400448 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200449 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200450
Per Forlin97868a22011-07-09 17:12:36 -0400451 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200452
453 return 1;
454}
455
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200456/*
457 * If writing, bounce the data to the buffer before the request
458 * is sent to the host driver
459 */
Per Forlin97868a22011-07-09 17:12:36 -0400460void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200461{
Per Forlin97868a22011-07-09 17:12:36 -0400462 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200463 return;
464
Per Forlin97868a22011-07-09 17:12:36 -0400465 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200466 return;
467
Per Forlin97868a22011-07-09 17:12:36 -0400468 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
469 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200470}
471
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200472/*
473 * If reading, bounce the data from the buffer after the request
474 * has been handled by the host driver
475 */
Per Forlin97868a22011-07-09 17:12:36 -0400476void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200477{
Per Forlin97868a22011-07-09 17:12:36 -0400478 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200479 return;
480
Per Forlin97868a22011-07-09 17:12:36 -0400481 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200482 return;
483
Per Forlin97868a22011-07-09 17:12:36 -0400484 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
485 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486}