blob: 3b15884e435ce08d836a3947f18ee68845ec280b [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 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070033 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020034 */
Adrian Hunterbd788c92010-08-11 14:17:47 -070035 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
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);
FUJITA Tomonori00fff262010-07-03 17:45:40 +0900131 blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200132 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Hunterbd788c92010-08-11 14:17:47 -0700133 if (mmc_can_erase(card)) {
134 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mq->queue);
135 mq->queue->limits.max_discard_sectors = UINT_MAX;
136 if (card->erased_byte == 0)
137 mq->queue->limits.discard_zeroes_data = 1;
138 if (!mmc_can_trim(card) && is_power_of_2(card->erase_size)) {
139 mq->queue->limits.discard_granularity =
140 card->erase_size << 9;
141 mq->queue->limits.discard_alignment =
142 card->erase_size << 9;
143 }
144 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200145
146#ifdef CONFIG_MMC_BLOCK_BOUNCE
147 if (host->max_hw_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200148 unsigned int bouncesz;
149
Pierre Ossman98ccf142007-05-12 00:26:16 +0200150 bouncesz = MMC_QUEUE_BOUNCESZ;
151
152 if (bouncesz > host->max_req_size)
153 bouncesz = host->max_req_size;
154 if (bouncesz > host->max_seg_size)
155 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200156 if (bouncesz > (host->max_blk_count * 512))
157 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200158
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200159 if (bouncesz > 512) {
160 mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
161 if (!mq->bounce_buf) {
162 printk(KERN_WARNING "%s: unable to "
163 "allocate bounce buffer\n",
164 mmc_card_name(card));
165 }
166 }
167
168 if (mq->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200169 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500170 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500171 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200172 blk_queue_max_segment_size(mq->queue, bouncesz);
173
Jens Axboe45711f12007-10-22 21:19:53 +0200174 mq->sg = kmalloc(sizeof(struct scatterlist),
Pierre Ossman98ccf142007-05-12 00:26:16 +0200175 GFP_KERNEL);
176 if (!mq->sg) {
177 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200178 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200179 }
Jens Axboe45711f12007-10-22 21:19:53 +0200180 sg_init_table(mq->sg, 1);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200181
Jens Axboe45711f12007-10-22 21:19:53 +0200182 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
Pierre Ossman98ccf142007-05-12 00:26:16 +0200183 bouncesz / 512, GFP_KERNEL);
184 if (!mq->bounce_sg) {
185 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200186 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200187 }
Jens Axboe45711f12007-10-22 21:19:53 +0200188 sg_init_table(mq->bounce_sg, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200189 }
190 }
191#endif
192
193 if (!mq->bounce_buf) {
194 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500195 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200196 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersen8a783622010-02-26 00:20:39 -0500197 blk_queue_max_segments(mq->queue, host->max_hw_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200198 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
199
Haavard Skinnemoen05e5b132007-11-23 10:19:00 +0100200 mq->sg = kmalloc(sizeof(struct scatterlist) *
Pierre Ossman98ccf142007-05-12 00:26:16 +0200201 host->max_phys_segs, GFP_KERNEL);
202 if (!mq->sg) {
203 ret = -ENOMEM;
204 goto cleanup_queue;
205 }
Haavard Skinnemoen05e5b132007-11-23 10:19:00 +0100206 sg_init_table(mq->sg, host->max_phys_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 init_MUTEX(&mq->thread_sem);
210
Christoph Hellwig87598a22006-11-13 20:23:52 +0100211 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
212 if (IS_ERR(mq->thread)) {
213 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200214 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Christoph Hellwig87598a22006-11-13 20:23:52 +0100217 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200218 free_bounce_sg:
219 if (mq->bounce_sg)
220 kfree(mq->bounce_sg);
221 mq->bounce_sg = NULL;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200222 cleanup_queue:
223 if (mq->sg)
224 kfree(mq->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 mq->sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200226 if (mq->bounce_buf)
227 kfree(mq->bounce_buf);
228 mq->bounce_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return ret;
231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233void mmc_cleanup_queue(struct mmc_queue *mq)
234{
Jens Axboe165125e2007-07-24 09:28:11 +0200235 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100236 unsigned long flags;
237
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200238 /* Make sure the queue isn't suspended, as that will deadlock */
239 mmc_queue_resume(mq);
240
Pierre Ossman89b4e132006-11-14 22:08:16 +0100241 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100242 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800244 /* Empty the queue */
245 spin_lock_irqsave(q->queue_lock, flags);
246 q->queuedata = NULL;
247 blk_start_queue(q);
248 spin_unlock_irqrestore(q->queue_lock, flags);
249
Pierre Ossman98ccf142007-05-12 00:26:16 +0200250 if (mq->bounce_sg)
251 kfree(mq->bounce_sg);
252 mq->bounce_sg = NULL;
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 kfree(mq->sg);
255 mq->sg = NULL;
256
Pierre Ossman98ccf142007-05-12 00:26:16 +0200257 if (mq->bounce_buf)
258 kfree(mq->bounce_buf);
259 mq->bounce_buf = NULL;
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 mq->card = NULL;
262}
263EXPORT_SYMBOL(mmc_cleanup_queue);
264
265/**
266 * mmc_queue_suspend - suspend a MMC request queue
267 * @mq: MMC queue to suspend
268 *
269 * Stop the block request queue, and wait for our thread to
270 * complete any outstanding requests. This ensures that we
271 * won't suspend while a request is being processed.
272 */
273void mmc_queue_suspend(struct mmc_queue *mq)
274{
Jens Axboe165125e2007-07-24 09:28:11 +0200275 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 unsigned long flags;
277
278 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
279 mq->flags |= MMC_QUEUE_SUSPENDED;
280
281 spin_lock_irqsave(q->queue_lock, flags);
282 blk_stop_queue(q);
283 spin_unlock_irqrestore(q->queue_lock, flags);
284
285 down(&mq->thread_sem);
286 }
287}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289/**
290 * mmc_queue_resume - resume a previously suspended MMC request queue
291 * @mq: MMC queue to resume
292 */
293void mmc_queue_resume(struct mmc_queue *mq)
294{
Jens Axboe165125e2007-07-24 09:28:11 +0200295 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 unsigned long flags;
297
298 if (mq->flags & MMC_QUEUE_SUSPENDED) {
299 mq->flags &= ~MMC_QUEUE_SUSPENDED;
300
301 up(&mq->thread_sem);
302
303 spin_lock_irqsave(q->queue_lock, flags);
304 blk_start_queue(q);
305 spin_unlock_irqrestore(q->queue_lock, flags);
306 }
307}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100308
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200309/*
310 * Prepare the sg list(s) to be handed of to the host driver
311 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200312unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
313{
314 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200315 size_t buflen;
316 struct scatterlist *sg;
317 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200318
319 if (!mq->bounce_buf)
320 return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
321
322 BUG_ON(!mq->bounce_sg);
323
324 sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
325
326 mq->bounce_sg_len = sg_len;
327
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200328 buflen = 0;
329 for_each_sg(mq->bounce_sg, sg, sg_len, i)
330 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200331
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200332 sg_init_one(mq->sg, mq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200333
334 return 1;
335}
336
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200337/*
338 * If writing, bounce the data to the buffer before the request
339 * is sent to the host driver
340 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200341void mmc_queue_bounce_pre(struct mmc_queue *mq)
342{
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200343 unsigned long flags;
344
Pierre Ossman98ccf142007-05-12 00:26:16 +0200345 if (!mq->bounce_buf)
346 return;
347
Pierre Ossman98ccf142007-05-12 00:26:16 +0200348 if (rq_data_dir(mq->req) != WRITE)
349 return;
350
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200351 local_irq_save(flags);
352 sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
353 mq->bounce_buf, mq->sg[0].length);
354 local_irq_restore(flags);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200355}
356
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200357/*
358 * If reading, bounce the data from the buffer after the request
359 * has been handled by the host driver
360 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200361void mmc_queue_bounce_post(struct mmc_queue *mq)
362{
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200363 unsigned long flags;
364
Pierre Ossman98ccf142007-05-12 00:26:16 +0200365 if (!mq->bounce_buf)
366 return;
367
Pierre Ossman98ccf142007-05-12 00:26:16 +0200368 if (rq_data_dir(mq->req) != READ)
369 return;
370
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200371 local_irq_save(flags);
372 sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
373 mq->bounce_buf, mq->sg[0].length);
374 local_irq_restore(flags);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200375}
376