blob: 9b64847421dfdc6b068b0e220de42c5d6695bf5d [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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070049 struct request *req;
50
51#ifdef CONFIG_MMC_PERF_PROFILING
52 ktime_t start, diff;
53 struct mmc_host *host = mq->card->host;
54 unsigned long bytes_xfer;
55#endif
56
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Rafael J. Wysocki83144182007-07-17 04:03:35 -070058 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 do {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070062 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 spin_lock_irq(q->queue_lock);
65 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010066 req = blk_fetch_request(q);
Juha [êöläc723e08a2006-08-06 09:58:22 +010067 mq->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 spin_unlock_irq(q->queue_lock);
69
70 if (!req) {
Vitaly Wool7b30d282006-12-07 20:08:02 +010071 if (kthread_should_stop()) {
72 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 up(&mq->thread_sem);
76 schedule();
77 down(&mq->thread_sem);
78 continue;
79 }
80 set_current_state(TASK_RUNNING);
81
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070082#ifdef CONFIG_MMC_PERF_PROFILING
83 bytes_xfer = blk_rq_bytes(req);
84 if (rq_data_dir(req) == READ) {
85 start = ktime_get();
86 mq->issue_fn(mq, req);
87 diff = ktime_sub(ktime_get(), start);
88 host->perf.rbytes_mmcq += bytes_xfer;
89 host->perf.rtime_mmcq =
90 ktime_add(host->perf.rtime_mmcq, diff);
91 } else {
92 start = ktime_get();
93 mq->issue_fn(mq, req);
94 diff = ktime_sub(ktime_get(), start);
95 host->perf.wbytes_mmcq += bytes_xfer;
96 host->perf.wtime_mmcq =
97 ktime_add(host->perf.wtime_mmcq, diff);
98 }
99#else
100 mq->issue_fn(mq, req);
101#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 up(&mq->thread_sem);
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 return 0;
106}
107
108/*
109 * Generic MMC request handler. This is called for any queue on a
110 * particular host. When the host is not busy, we look for a request
111 * on any queue on this host, and attempt to issue it. This may
112 * not be the queue we were asked to process.
113 */
Jens Axboe165125e2007-07-24 09:28:11 +0200114static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100117 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100118
119 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800120 while ((req = blk_fetch_request(q)) != NULL) {
121 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900122 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800123 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100124 return;
125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 if (!mq->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100128 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131/**
132 * mmc_init_queue - initialise a queue structure.
133 * @mq: mmc queue
134 * @card: mmc card to attach this queue
135 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300136 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 *
138 * Initialise a MMC card request queue.
139 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300140int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
141 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct mmc_host *host = card->host;
144 u64 limit = BLK_BOUNCE_HIGH;
145 int ret;
146
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200147 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
148 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 mq->card = card;
151 mq->queue = blk_init_queue(mmc_request, lock);
152 if (!mq->queue)
153 return -ENOMEM;
154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 mq->queue->queuedata = mq;
156 mq->req = NULL;
157
Pierre Ossman98ccf142007-05-12 00:26:16 +0200158 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200159 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Hunterbd788c92010-08-11 14:17:47 -0700160 if (mmc_can_erase(card)) {
161 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mq->queue);
162 mq->queue->limits.max_discard_sectors = UINT_MAX;
163 if (card->erased_byte == 0)
164 mq->queue->limits.discard_zeroes_data = 1;
Adrian Hunterc31b55c2011-06-23 13:40:29 +0300165 mq->queue->limits.discard_granularity = card->pref_erase << 9;
Adrian Hunter49804542010-08-11 14:17:50 -0700166 if (mmc_can_secure_erase_trim(card))
167 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD,
168 mq->queue);
Adrian Hunterbd788c92010-08-11 14:17:47 -0700169 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200170
171#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400172 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200173 unsigned int bouncesz;
174
Pierre Ossman98ccf142007-05-12 00:26:16 +0200175 bouncesz = MMC_QUEUE_BOUNCESZ;
176
177 if (bouncesz > host->max_req_size)
178 bouncesz = host->max_req_size;
179 if (bouncesz > host->max_seg_size)
180 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200181 if (bouncesz > (host->max_blk_count * 512))
182 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200183
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200184 if (bouncesz > 512) {
185 mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
186 if (!mq->bounce_buf) {
187 printk(KERN_WARNING "%s: unable to "
188 "allocate bounce buffer\n",
189 mmc_card_name(card));
190 }
191 }
192
193 if (mq->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200194 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500195 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500196 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200197 blk_queue_max_segment_size(mq->queue, bouncesz);
198
Jens Axboe45711f12007-10-22 21:19:53 +0200199 mq->sg = kmalloc(sizeof(struct scatterlist),
Pierre Ossman98ccf142007-05-12 00:26:16 +0200200 GFP_KERNEL);
201 if (!mq->sg) {
202 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200203 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200204 }
Jens Axboe45711f12007-10-22 21:19:53 +0200205 sg_init_table(mq->sg, 1);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200206
Jens Axboe45711f12007-10-22 21:19:53 +0200207 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
Pierre Ossman98ccf142007-05-12 00:26:16 +0200208 bouncesz / 512, GFP_KERNEL);
209 if (!mq->bounce_sg) {
210 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200211 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200212 }
Jens Axboe45711f12007-10-22 21:19:53 +0200213 sg_init_table(mq->bounce_sg, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200214 }
215 }
216#endif
217
218 if (!mq->bounce_buf) {
219 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500220 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200221 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400222 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200223 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
224
Haavard Skinnemoen05e5b132007-11-23 10:19:00 +0100225 mq->sg = kmalloc(sizeof(struct scatterlist) *
Martin K. Petersena36274e2010-09-10 01:33:59 -0400226 host->max_segs, GFP_KERNEL);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200227 if (!mq->sg) {
228 ret = -ENOMEM;
229 goto cleanup_queue;
230 }
Martin K. Petersena36274e2010-09-10 01:33:59 -0400231 sg_init_table(mq->sg, host->max_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Thomas Gleixner632cf922010-09-14 07:12:35 -0400234 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Adrian Hunterd09408a2011-06-23 13:40:28 +0300236 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
237 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400238
Christoph Hellwig87598a22006-11-13 20:23:52 +0100239 if (IS_ERR(mq->thread)) {
240 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200241 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 }
243
Christoph Hellwig87598a22006-11-13 20:23:52 +0100244 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200245 free_bounce_sg:
246 if (mq->bounce_sg)
247 kfree(mq->bounce_sg);
248 mq->bounce_sg = NULL;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200249 cleanup_queue:
250 if (mq->sg)
251 kfree(mq->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 mq->sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200253 if (mq->bounce_buf)
254 kfree(mq->bounce_buf);
255 mq->bounce_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return ret;
258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260void mmc_cleanup_queue(struct mmc_queue *mq)
261{
Jens Axboe165125e2007-07-24 09:28:11 +0200262 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100263 unsigned long flags;
264
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200265 /* Make sure the queue isn't suspended, as that will deadlock */
266 mmc_queue_resume(mq);
267
Pierre Ossman89b4e132006-11-14 22:08:16 +0100268 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100269 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800271 /* Empty the queue */
272 spin_lock_irqsave(q->queue_lock, flags);
273 q->queuedata = NULL;
274 blk_start_queue(q);
275 spin_unlock_irqrestore(q->queue_lock, flags);
276
Pierre Ossman98ccf142007-05-12 00:26:16 +0200277 if (mq->bounce_sg)
278 kfree(mq->bounce_sg);
279 mq->bounce_sg = NULL;
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 kfree(mq->sg);
282 mq->sg = NULL;
283
Pierre Ossman98ccf142007-05-12 00:26:16 +0200284 if (mq->bounce_buf)
285 kfree(mq->bounce_buf);
286 mq->bounce_buf = NULL;
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 mq->card = NULL;
289}
290EXPORT_SYMBOL(mmc_cleanup_queue);
291
292/**
293 * mmc_queue_suspend - suspend a MMC request queue
294 * @mq: MMC queue to suspend
295 *
296 * Stop the block request queue, and wait for our thread to
297 * complete any outstanding requests. This ensures that we
298 * won't suspend while a request is being processed.
299 */
300void mmc_queue_suspend(struct mmc_queue *mq)
301{
Jens Axboe165125e2007-07-24 09:28:11 +0200302 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 unsigned long flags;
304
305 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
306 mq->flags |= MMC_QUEUE_SUSPENDED;
307
308 spin_lock_irqsave(q->queue_lock, flags);
309 blk_stop_queue(q);
310 spin_unlock_irqrestore(q->queue_lock, flags);
311
312 down(&mq->thread_sem);
313 }
314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316/**
317 * mmc_queue_resume - resume a previously suspended MMC request queue
318 * @mq: MMC queue to resume
319 */
320void mmc_queue_resume(struct mmc_queue *mq)
321{
Jens Axboe165125e2007-07-24 09:28:11 +0200322 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 unsigned long flags;
324
325 if (mq->flags & MMC_QUEUE_SUSPENDED) {
326 mq->flags &= ~MMC_QUEUE_SUSPENDED;
327
328 up(&mq->thread_sem);
329
330 spin_lock_irqsave(q->queue_lock, flags);
331 blk_start_queue(q);
332 spin_unlock_irqrestore(q->queue_lock, flags);
333 }
334}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100335
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200336/*
337 * Prepare the sg list(s) to be handed of to the host driver
338 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200339unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
340{
341 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200342 size_t buflen;
343 struct scatterlist *sg;
344 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200345
346 if (!mq->bounce_buf)
347 return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
348
349 BUG_ON(!mq->bounce_sg);
350
351 sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
352
353 mq->bounce_sg_len = sg_len;
354
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200355 buflen = 0;
356 for_each_sg(mq->bounce_sg, sg, sg_len, i)
357 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200358
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200359 sg_init_one(mq->sg, mq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200360
361 return 1;
362}
363
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200364/*
365 * If writing, bounce the data to the buffer before the request
366 * is sent to the host driver
367 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200368void mmc_queue_bounce_pre(struct mmc_queue *mq)
369{
370 if (!mq->bounce_buf)
371 return;
372
Pierre Ossman98ccf142007-05-12 00:26:16 +0200373 if (rq_data_dir(mq->req) != WRITE)
374 return;
375
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200376 sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
377 mq->bounce_buf, mq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200378}
379
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200380/*
381 * If reading, bounce the data from the buffer after the request
382 * has been handled by the host driver
383 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200384void mmc_queue_bounce_post(struct mmc_queue *mq)
385{
386 if (!mq->bounce_buf)
387 return;
388
Pierre Ossman98ccf142007-05-12 00:26:16 +0200389 if (rq_data_dir(mq->req) != READ)
390 return;
391
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200392 sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
393 mq->bounce_buf, mq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200394}
395