blob: 0c0b0f7f7a7061ab9b39b550375e324d3602d3a1 [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 Thummacfefa142011-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 Thummacfefa142011-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;
55
56#ifdef CONFIG_MMC_PERF_PROFILING
57 ktime_t start, diff;
58 struct mmc_host *host = mq->card->host;
59 unsigned long bytes_xfer;
60#endif
61
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 {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070067 req = NULL; /* Must be set to NULL at each iteration */
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 spin_lock_irq(q->queue_lock);
70 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010071 req = blk_fetch_request(q);
Juha [êöläc723e08a2006-08-06 09:58:22 +010072 mq->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 spin_unlock_irq(q->queue_lock);
74
75 if (!req) {
Vitaly Wool7b30d282006-12-07 20:08:02 +010076 if (kthread_should_stop()) {
77 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 up(&mq->thread_sem);
81 schedule();
82 down(&mq->thread_sem);
83 continue;
84 }
85 set_current_state(TASK_RUNNING);
86
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070087#ifdef CONFIG_MMC_PERF_PROFILING
88 bytes_xfer = blk_rq_bytes(req);
89 if (rq_data_dir(req) == READ) {
90 start = ktime_get();
91 mq->issue_fn(mq, req);
92 diff = ktime_sub(ktime_get(), start);
93 host->perf.rbytes_mmcq += bytes_xfer;
94 host->perf.rtime_mmcq =
95 ktime_add(host->perf.rtime_mmcq, diff);
96 } else {
97 start = ktime_get();
98 mq->issue_fn(mq, req);
99 diff = ktime_sub(ktime_get(), start);
100 host->perf.wbytes_mmcq += bytes_xfer;
101 host->perf.wtime_mmcq =
102 ktime_add(host->perf.wtime_mmcq, diff);
103 }
104#else
105 mq->issue_fn(mq, req);
106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 up(&mq->thread_sem);
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 return 0;
111}
112
113/*
114 * Generic MMC request handler. This is called for any queue on a
115 * particular host. When the host is not busy, we look for a request
116 * on any queue on this host, and attempt to issue it. This may
117 * not be the queue we were asked to process.
118 */
Jens Axboe165125e2007-07-24 09:28:11 +0200119static void mmc_request(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100122 struct request *req;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100123
124 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800125 while ((req = blk_fetch_request(q)) != NULL) {
126 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900127 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800128 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100129 return;
130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (!mq->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100133 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134}
135
Adrian Hunter81306ad2011-06-28 17:16:02 +0300136static void mmc_queue_setup_discard(struct request_queue *q,
137 struct mmc_card *card)
138{
139 unsigned max_discard;
140
141 max_discard = mmc_calc_max_discard(card);
142 if (!max_discard)
143 return;
144
145 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
146 q->limits.max_discard_sectors = max_discard;
147 if (card->erased_byte == 0)
148 q->limits.discard_zeroes_data = 1;
149 q->limits.discard_granularity = card->pref_erase << 9;
150 /* granularity must not be greater than max. discard */
151 if (card->pref_erase > max_discard)
152 q->limits.discard_granularity = 0;
153 if (mmc_can_secure_erase_trim(card))
154 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
155}
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/**
158 * mmc_init_queue - initialise a queue structure.
159 * @mq: mmc queue
160 * @card: mmc card to attach this queue
161 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300162 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 *
164 * Initialise a MMC card request queue.
165 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300166int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
167 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct mmc_host *host = card->host;
170 u64 limit = BLK_BOUNCE_HIGH;
171 int ret;
172
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200173 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
174 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 mq->card = card;
177 mq->queue = blk_init_queue(mmc_request, lock);
178 if (!mq->queue)
179 return -ENOMEM;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 mq->queue->queuedata = mq;
182 mq->req = NULL;
183
Pierre Ossman98ccf142007-05-12 00:26:16 +0200184 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200185 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Hunter81306ad2011-06-28 17:16:02 +0300186 if (mmc_can_erase(card))
187 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200188
189#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400190 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200191 unsigned int bouncesz;
192
Pierre Ossman98ccf142007-05-12 00:26:16 +0200193 bouncesz = MMC_QUEUE_BOUNCESZ;
194
195 if (bouncesz > host->max_req_size)
196 bouncesz = host->max_req_size;
197 if (bouncesz > host->max_seg_size)
198 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200199 if (bouncesz > (host->max_blk_count * 512))
200 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200201
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200202 if (bouncesz > 512) {
203 mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
204 if (!mq->bounce_buf) {
205 printk(KERN_WARNING "%s: unable to "
206 "allocate bounce buffer\n",
207 mmc_card_name(card));
208 }
209 }
210
211 if (mq->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200212 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500213 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500214 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200215 blk_queue_max_segment_size(mq->queue, bouncesz);
216
Jens Axboe45711f12007-10-22 21:19:53 +0200217 mq->sg = kmalloc(sizeof(struct scatterlist),
Pierre Ossman98ccf142007-05-12 00:26:16 +0200218 GFP_KERNEL);
219 if (!mq->sg) {
220 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200221 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200222 }
Jens Axboe45711f12007-10-22 21:19:53 +0200223 sg_init_table(mq->sg, 1);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200224
Jens Axboe45711f12007-10-22 21:19:53 +0200225 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
Pierre Ossman98ccf142007-05-12 00:26:16 +0200226 bouncesz / 512, GFP_KERNEL);
227 if (!mq->bounce_sg) {
228 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200229 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200230 }
Jens Axboe45711f12007-10-22 21:19:53 +0200231 sg_init_table(mq->bounce_sg, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200232 }
233 }
234#endif
235
236 if (!mq->bounce_buf) {
237 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500238 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200239 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400240 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200241 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
242
Haavard Skinnemoen05e5b132007-11-23 10:19:00 +0100243 mq->sg = kmalloc(sizeof(struct scatterlist) *
Martin K. Petersena36274e2010-09-10 01:33:59 -0400244 host->max_segs, GFP_KERNEL);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200245 if (!mq->sg) {
246 ret = -ENOMEM;
247 goto cleanup_queue;
248 }
Martin K. Petersena36274e2010-09-10 01:33:59 -0400249 sg_init_table(mq->sg, host->max_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
Thomas Gleixner632cf922010-09-14 07:12:35 -0400252 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Adrian Hunterd09408a2011-06-23 13:40:28 +0300254 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
255 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400256
Christoph Hellwig87598a22006-11-13 20:23:52 +0100257 if (IS_ERR(mq->thread)) {
258 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200259 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
Christoph Hellwig87598a22006-11-13 20:23:52 +0100262 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200263 free_bounce_sg:
264 if (mq->bounce_sg)
265 kfree(mq->bounce_sg);
266 mq->bounce_sg = NULL;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200267 cleanup_queue:
268 if (mq->sg)
269 kfree(mq->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 mq->sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200271 if (mq->bounce_buf)
272 kfree(mq->bounce_buf);
273 mq->bounce_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return ret;
276}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278void mmc_cleanup_queue(struct mmc_queue *mq)
279{
Jens Axboe165125e2007-07-24 09:28:11 +0200280 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100281 unsigned long flags;
282
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200283 /* Make sure the queue isn't suspended, as that will deadlock */
284 mmc_queue_resume(mq);
285
Pierre Ossman89b4e132006-11-14 22:08:16 +0100286 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100287 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800289 /* Empty the queue */
290 spin_lock_irqsave(q->queue_lock, flags);
291 q->queuedata = NULL;
292 blk_start_queue(q);
293 spin_unlock_irqrestore(q->queue_lock, flags);
294
Pierre Ossman98ccf142007-05-12 00:26:16 +0200295 if (mq->bounce_sg)
296 kfree(mq->bounce_sg);
297 mq->bounce_sg = NULL;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 kfree(mq->sg);
300 mq->sg = NULL;
301
Pierre Ossman98ccf142007-05-12 00:26:16 +0200302 if (mq->bounce_buf)
303 kfree(mq->bounce_buf);
304 mq->bounce_buf = NULL;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 mq->card = NULL;
307}
308EXPORT_SYMBOL(mmc_cleanup_queue);
309
310/**
311 * mmc_queue_suspend - suspend a MMC request queue
312 * @mq: MMC queue to suspend
313 *
314 * Stop the block request queue, and wait for our thread to
315 * complete any outstanding requests. This ensures that we
316 * won't suspend while a request is being processed.
317 */
318void mmc_queue_suspend(struct mmc_queue *mq)
319{
Jens Axboe165125e2007-07-24 09:28:11 +0200320 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 unsigned long flags;
322
323 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
324 mq->flags |= MMC_QUEUE_SUSPENDED;
325
326 spin_lock_irqsave(q->queue_lock, flags);
327 blk_stop_queue(q);
328 spin_unlock_irqrestore(q->queue_lock, flags);
329
330 down(&mq->thread_sem);
331 }
332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334/**
335 * mmc_queue_resume - resume a previously suspended MMC request queue
336 * @mq: MMC queue to resume
337 */
338void mmc_queue_resume(struct mmc_queue *mq)
339{
Jens Axboe165125e2007-07-24 09:28:11 +0200340 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 unsigned long flags;
342
343 if (mq->flags & MMC_QUEUE_SUSPENDED) {
344 mq->flags &= ~MMC_QUEUE_SUSPENDED;
345
346 up(&mq->thread_sem);
347
348 spin_lock_irqsave(q->queue_lock, flags);
349 blk_start_queue(q);
350 spin_unlock_irqrestore(q->queue_lock, flags);
351 }
352}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100353
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200354/*
355 * Prepare the sg list(s) to be handed of to the host driver
356 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200357unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
358{
359 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200360 size_t buflen;
361 struct scatterlist *sg;
362 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200363
364 if (!mq->bounce_buf)
365 return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
366
367 BUG_ON(!mq->bounce_sg);
368
369 sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
370
371 mq->bounce_sg_len = sg_len;
372
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200373 buflen = 0;
374 for_each_sg(mq->bounce_sg, sg, sg_len, i)
375 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200376
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200377 sg_init_one(mq->sg, mq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200378
379 return 1;
380}
381
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200382/*
383 * If writing, bounce the data to the buffer before the request
384 * is sent to the host driver
385 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200386void mmc_queue_bounce_pre(struct mmc_queue *mq)
387{
388 if (!mq->bounce_buf)
389 return;
390
Pierre Ossman98ccf142007-05-12 00:26:16 +0200391 if (rq_data_dir(mq->req) != WRITE)
392 return;
393
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200394 sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
395 mq->bounce_buf, mq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200396}
397
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200398/*
399 * If reading, bounce the data from the buffer after the request
400 * has been handled by the host driver
401 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200402void mmc_queue_bounce_post(struct mmc_queue *mq)
403{
404 if (!mq->bounce_buf)
405 return;
406
Pierre Ossman98ccf142007-05-12 00:26:16 +0200407 if (rq_data_dir(mq->req) != READ)
408 return;
409
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200410 sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
411 mq->bounce_buf, mq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200412}
413