blob: d6ded247d941197734c289e36f665a2bdeb96737 [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{
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020032 /*
33 * We only like normal block requests.
34 */
Pierre Ossmand6d8de32008-08-16 20:43:48 +020035 if (!blk_fs_request(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020037 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 }
39
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020040 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020042 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043}
44
45static int mmc_queue_thread(void *d)
46{
47 struct mmc_queue *mq = d;
48 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Rafael J. Wysocki83144182007-07-17 04:03:35 -070050 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 do {
54 struct request *req = NULL;
55
56 spin_lock_irq(q->queue_lock);
57 set_current_state(TASK_INTERRUPTIBLE);
Tejun Heo9934c8c2009-05-08 11:54:16 +090058 if (!blk_queue_plugged(q))
59 req = blk_fetch_request(q);
Juha [êöläc723e08a2006-08-06 09:58:22 +010060 mq->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 spin_unlock_irq(q->queue_lock);
62
63 if (!req) {
Vitaly Wool7b30d282006-12-07 20:08:02 +010064 if (kthread_should_stop()) {
65 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 up(&mq->thread_sem);
69 schedule();
70 down(&mq->thread_sem);
71 continue;
72 }
73 set_current_state(TASK_RUNNING);
74
75 mq->issue_fn(mq, req);
76 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 up(&mq->thread_sem);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return 0;
80}
81
82/*
83 * Generic MMC request handler. This is called for any queue on a
84 * particular host. When the host is not busy, we look for a request
85 * on any queue on this host, and attempt to issue it. This may
86 * not be the queue we were asked to process.
87 */
Jens Axboe165125e2007-07-24 09:28:11 +020088static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +010091 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +010092
93 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -080094 while ((req = blk_fetch_request(q)) != NULL) {
95 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +090096 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -080097 }
Pierre Ossman89b4e132006-11-14 22:08:16 +010098 return;
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101 if (!mq->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100102 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
105/**
106 * mmc_init_queue - initialise a queue structure.
107 * @mq: mmc queue
108 * @card: mmc card to attach this queue
109 * @lock: queue lock
110 *
111 * Initialise a MMC card request queue.
112 */
113int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
114{
115 struct mmc_host *host = card->host;
116 u64 limit = BLK_BOUNCE_HIGH;
117 int ret;
118
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200119 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
120 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 mq->card = card;
123 mq->queue = blk_init_queue(mmc_request, lock);
124 if (!mq->queue)
125 return -ENOMEM;
126
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 mq->queue->queuedata = mq;
128 mq->req = NULL;
129
Pierre Ossman98ccf142007-05-12 00:26:16 +0200130 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman91028952008-08-16 21:15:50 +0200131 blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN, NULL);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200132 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200133
134#ifdef CONFIG_MMC_BLOCK_BOUNCE
135 if (host->max_hw_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200136 unsigned int bouncesz;
137
Pierre Ossman98ccf142007-05-12 00:26:16 +0200138 bouncesz = MMC_QUEUE_BOUNCESZ;
139
140 if (bouncesz > host->max_req_size)
141 bouncesz = host->max_req_size;
142 if (bouncesz > host->max_seg_size)
143 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200144 if (bouncesz > (host->max_blk_count * 512))
145 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200146
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200147 if (bouncesz > 512) {
148 mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
149 if (!mq->bounce_buf) {
150 printk(KERN_WARNING "%s: unable to "
151 "allocate bounce buffer\n",
152 mmc_card_name(card));
153 }
154 }
155
156 if (mq->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200157 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500158 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500159 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200160 blk_queue_max_segment_size(mq->queue, bouncesz);
161
Jens Axboe45711f12007-10-22 21:19:53 +0200162 mq->sg = kmalloc(sizeof(struct scatterlist),
Pierre Ossman98ccf142007-05-12 00:26:16 +0200163 GFP_KERNEL);
164 if (!mq->sg) {
165 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200166 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200167 }
Jens Axboe45711f12007-10-22 21:19:53 +0200168 sg_init_table(mq->sg, 1);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200169
Jens Axboe45711f12007-10-22 21:19:53 +0200170 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
Pierre Ossman98ccf142007-05-12 00:26:16 +0200171 bouncesz / 512, GFP_KERNEL);
172 if (!mq->bounce_sg) {
173 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200174 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200175 }
Jens Axboe45711f12007-10-22 21:19:53 +0200176 sg_init_table(mq->bounce_sg, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200177 }
178 }
179#endif
180
181 if (!mq->bounce_buf) {
182 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500183 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200184 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersen8a783622010-02-26 00:20:39 -0500185 blk_queue_max_segments(mq->queue, host->max_hw_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200186 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
187
Haavard Skinnemoen05e5b132007-11-23 10:19:00 +0100188 mq->sg = kmalloc(sizeof(struct scatterlist) *
Pierre Ossman98ccf142007-05-12 00:26:16 +0200189 host->max_phys_segs, GFP_KERNEL);
190 if (!mq->sg) {
191 ret = -ENOMEM;
192 goto cleanup_queue;
193 }
Haavard Skinnemoen05e5b132007-11-23 10:19:00 +0100194 sg_init_table(mq->sg, host->max_phys_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 init_MUTEX(&mq->thread_sem);
198
Christoph Hellwig87598a22006-11-13 20:23:52 +0100199 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
200 if (IS_ERR(mq->thread)) {
201 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200202 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204
Christoph Hellwig87598a22006-11-13 20:23:52 +0100205 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200206 free_bounce_sg:
207 if (mq->bounce_sg)
208 kfree(mq->bounce_sg);
209 mq->bounce_sg = NULL;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200210 cleanup_queue:
211 if (mq->sg)
212 kfree(mq->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 mq->sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200214 if (mq->bounce_buf)
215 kfree(mq->bounce_buf);
216 mq->bounce_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return ret;
219}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221void mmc_cleanup_queue(struct mmc_queue *mq)
222{
Jens Axboe165125e2007-07-24 09:28:11 +0200223 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100224 unsigned long flags;
225
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200226 /* Make sure the queue isn't suspended, as that will deadlock */
227 mmc_queue_resume(mq);
228
Pierre Ossman89b4e132006-11-14 22:08:16 +0100229 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100230 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800232 /* Empty the queue */
233 spin_lock_irqsave(q->queue_lock, flags);
234 q->queuedata = NULL;
235 blk_start_queue(q);
236 spin_unlock_irqrestore(q->queue_lock, flags);
237
Pierre Ossman98ccf142007-05-12 00:26:16 +0200238 if (mq->bounce_sg)
239 kfree(mq->bounce_sg);
240 mq->bounce_sg = NULL;
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 kfree(mq->sg);
243 mq->sg = NULL;
244
Pierre Ossman98ccf142007-05-12 00:26:16 +0200245 if (mq->bounce_buf)
246 kfree(mq->bounce_buf);
247 mq->bounce_buf = NULL;
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 mq->card = NULL;
250}
251EXPORT_SYMBOL(mmc_cleanup_queue);
252
253/**
254 * mmc_queue_suspend - suspend a MMC request queue
255 * @mq: MMC queue to suspend
256 *
257 * Stop the block request queue, and wait for our thread to
258 * complete any outstanding requests. This ensures that we
259 * won't suspend while a request is being processed.
260 */
261void mmc_queue_suspend(struct mmc_queue *mq)
262{
Jens Axboe165125e2007-07-24 09:28:11 +0200263 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 unsigned long flags;
265
266 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
267 mq->flags |= MMC_QUEUE_SUSPENDED;
268
269 spin_lock_irqsave(q->queue_lock, flags);
270 blk_stop_queue(q);
271 spin_unlock_irqrestore(q->queue_lock, flags);
272
273 down(&mq->thread_sem);
274 }
275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277/**
278 * mmc_queue_resume - resume a previously suspended MMC request queue
279 * @mq: MMC queue to resume
280 */
281void mmc_queue_resume(struct mmc_queue *mq)
282{
Jens Axboe165125e2007-07-24 09:28:11 +0200283 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 unsigned long flags;
285
286 if (mq->flags & MMC_QUEUE_SUSPENDED) {
287 mq->flags &= ~MMC_QUEUE_SUSPENDED;
288
289 up(&mq->thread_sem);
290
291 spin_lock_irqsave(q->queue_lock, flags);
292 blk_start_queue(q);
293 spin_unlock_irqrestore(q->queue_lock, flags);
294 }
295}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100296
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200297/*
298 * Prepare the sg list(s) to be handed of to the host driver
299 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200300unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
301{
302 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200303 size_t buflen;
304 struct scatterlist *sg;
305 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200306
307 if (!mq->bounce_buf)
308 return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
309
310 BUG_ON(!mq->bounce_sg);
311
312 sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
313
314 mq->bounce_sg_len = sg_len;
315
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200316 buflen = 0;
317 for_each_sg(mq->bounce_sg, sg, sg_len, i)
318 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200319
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200320 sg_init_one(mq->sg, mq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200321
322 return 1;
323}
324
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200325/*
326 * If writing, bounce the data to the buffer before the request
327 * is sent to the host driver
328 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200329void mmc_queue_bounce_pre(struct mmc_queue *mq)
330{
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200331 unsigned long flags;
332
Pierre Ossman98ccf142007-05-12 00:26:16 +0200333 if (!mq->bounce_buf)
334 return;
335
Pierre Ossman98ccf142007-05-12 00:26:16 +0200336 if (rq_data_dir(mq->req) != WRITE)
337 return;
338
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200339 local_irq_save(flags);
340 sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
341 mq->bounce_buf, mq->sg[0].length);
342 local_irq_restore(flags);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200343}
344
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200345/*
346 * If reading, bounce the data from the buffer after the request
347 * has been handled by the host driver
348 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200349void mmc_queue_bounce_post(struct mmc_queue *mq)
350{
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200351 unsigned long flags;
352
Pierre Ossman98ccf142007-05-12 00:26:16 +0200353 if (!mq->bounce_buf)
354 return;
355
Pierre Ossman98ccf142007-05-12 00:26:16 +0200356 if (rq_data_dir(mq->req) != READ)
357 return;
358
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200359 local_irq_save(flags);
360 sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
361 mq->bounce_buf, mq->sg[0].length);
362 local_irq_restore(flags);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200363}
364