blob: b2fb161b6cb6ccf396d87e16bf7aec757d49ae25 [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
136/**
137 * mmc_init_queue - initialise a queue structure.
138 * @mq: mmc queue
139 * @card: mmc card to attach this queue
140 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300141 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 *
143 * Initialise a MMC card request queue.
144 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300145int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
146 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct mmc_host *host = card->host;
149 u64 limit = BLK_BOUNCE_HIGH;
150 int ret;
151
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200152 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
153 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 mq->card = card;
156 mq->queue = blk_init_queue(mmc_request, lock);
157 if (!mq->queue)
158 return -ENOMEM;
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 mq->queue->queuedata = mq;
161 mq->req = NULL;
162
Pierre Ossman98ccf142007-05-12 00:26:16 +0200163 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200164 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Adrian Hunterbd788c92010-08-11 14:17:47 -0700165 if (mmc_can_erase(card)) {
166 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mq->queue);
167 mq->queue->limits.max_discard_sectors = UINT_MAX;
168 if (card->erased_byte == 0)
169 mq->queue->limits.discard_zeroes_data = 1;
Adrian Hunterc31b55c2011-06-23 13:40:29 +0300170 mq->queue->limits.discard_granularity = card->pref_erase << 9;
Adrian Hunter49804542010-08-11 14:17:50 -0700171 if (mmc_can_secure_erase_trim(card))
172 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD,
173 mq->queue);
Adrian Hunterbd788c92010-08-11 14:17:47 -0700174 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200175
176#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400177 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200178 unsigned int bouncesz;
179
Pierre Ossman98ccf142007-05-12 00:26:16 +0200180 bouncesz = MMC_QUEUE_BOUNCESZ;
181
182 if (bouncesz > host->max_req_size)
183 bouncesz = host->max_req_size;
184 if (bouncesz > host->max_seg_size)
185 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200186 if (bouncesz > (host->max_blk_count * 512))
187 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200188
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200189 if (bouncesz > 512) {
190 mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
191 if (!mq->bounce_buf) {
192 printk(KERN_WARNING "%s: unable to "
193 "allocate bounce buffer\n",
194 mmc_card_name(card));
195 }
196 }
197
198 if (mq->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200199 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500200 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500201 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200202 blk_queue_max_segment_size(mq->queue, bouncesz);
203
Jens Axboe45711f12007-10-22 21:19:53 +0200204 mq->sg = kmalloc(sizeof(struct scatterlist),
Pierre Ossman98ccf142007-05-12 00:26:16 +0200205 GFP_KERNEL);
206 if (!mq->sg) {
207 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200208 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200209 }
Jens Axboe45711f12007-10-22 21:19:53 +0200210 sg_init_table(mq->sg, 1);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200211
Jens Axboe45711f12007-10-22 21:19:53 +0200212 mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
Pierre Ossman98ccf142007-05-12 00:26:16 +0200213 bouncesz / 512, GFP_KERNEL);
214 if (!mq->bounce_sg) {
215 ret = -ENOMEM;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200216 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200217 }
Jens Axboe45711f12007-10-22 21:19:53 +0200218 sg_init_table(mq->bounce_sg, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200219 }
220 }
221#endif
222
223 if (!mq->bounce_buf) {
224 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500225 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200226 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400227 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200228 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
229
Haavard Skinnemoen05e5b132007-11-23 10:19:00 +0100230 mq->sg = kmalloc(sizeof(struct scatterlist) *
Martin K. Petersena36274e2010-09-10 01:33:59 -0400231 host->max_segs, GFP_KERNEL);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200232 if (!mq->sg) {
233 ret = -ENOMEM;
234 goto cleanup_queue;
235 }
Martin K. Petersena36274e2010-09-10 01:33:59 -0400236 sg_init_table(mq->sg, host->max_segs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
238
Thomas Gleixner632cf922010-09-14 07:12:35 -0400239 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Adrian Hunterd09408a2011-06-23 13:40:28 +0300241 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
242 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400243
Christoph Hellwig87598a22006-11-13 20:23:52 +0100244 if (IS_ERR(mq->thread)) {
245 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200246 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
Christoph Hellwig87598a22006-11-13 20:23:52 +0100249 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200250 free_bounce_sg:
251 if (mq->bounce_sg)
252 kfree(mq->bounce_sg);
253 mq->bounce_sg = NULL;
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200254 cleanup_queue:
255 if (mq->sg)
256 kfree(mq->sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 mq->sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200258 if (mq->bounce_buf)
259 kfree(mq->bounce_buf);
260 mq->bounce_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return ret;
263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265void mmc_cleanup_queue(struct mmc_queue *mq)
266{
Jens Axboe165125e2007-07-24 09:28:11 +0200267 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100268 unsigned long flags;
269
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200270 /* Make sure the queue isn't suspended, as that will deadlock */
271 mmc_queue_resume(mq);
272
Pierre Ossman89b4e132006-11-14 22:08:16 +0100273 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100274 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800276 /* Empty the queue */
277 spin_lock_irqsave(q->queue_lock, flags);
278 q->queuedata = NULL;
279 blk_start_queue(q);
280 spin_unlock_irqrestore(q->queue_lock, flags);
281
Pierre Ossman98ccf142007-05-12 00:26:16 +0200282 if (mq->bounce_sg)
283 kfree(mq->bounce_sg);
284 mq->bounce_sg = NULL;
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 kfree(mq->sg);
287 mq->sg = NULL;
288
Pierre Ossman98ccf142007-05-12 00:26:16 +0200289 if (mq->bounce_buf)
290 kfree(mq->bounce_buf);
291 mq->bounce_buf = NULL;
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 mq->card = NULL;
294}
295EXPORT_SYMBOL(mmc_cleanup_queue);
296
297/**
298 * mmc_queue_suspend - suspend a MMC request queue
299 * @mq: MMC queue to suspend
300 *
301 * Stop the block request queue, and wait for our thread to
302 * complete any outstanding requests. This ensures that we
303 * won't suspend while a request is being processed.
304 */
305void mmc_queue_suspend(struct mmc_queue *mq)
306{
Jens Axboe165125e2007-07-24 09:28:11 +0200307 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 unsigned long flags;
309
310 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
311 mq->flags |= MMC_QUEUE_SUSPENDED;
312
313 spin_lock_irqsave(q->queue_lock, flags);
314 blk_stop_queue(q);
315 spin_unlock_irqrestore(q->queue_lock, flags);
316
317 down(&mq->thread_sem);
318 }
319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321/**
322 * mmc_queue_resume - resume a previously suspended MMC request queue
323 * @mq: MMC queue to resume
324 */
325void mmc_queue_resume(struct mmc_queue *mq)
326{
Jens Axboe165125e2007-07-24 09:28:11 +0200327 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 unsigned long flags;
329
330 if (mq->flags & MMC_QUEUE_SUSPENDED) {
331 mq->flags &= ~MMC_QUEUE_SUSPENDED;
332
333 up(&mq->thread_sem);
334
335 spin_lock_irqsave(q->queue_lock, flags);
336 blk_start_queue(q);
337 spin_unlock_irqrestore(q->queue_lock, flags);
338 }
339}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100340
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200341/*
342 * Prepare the sg list(s) to be handed of to the host driver
343 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200344unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
345{
346 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200347 size_t buflen;
348 struct scatterlist *sg;
349 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200350
351 if (!mq->bounce_buf)
352 return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
353
354 BUG_ON(!mq->bounce_sg);
355
356 sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
357
358 mq->bounce_sg_len = sg_len;
359
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200360 buflen = 0;
361 for_each_sg(mq->bounce_sg, sg, sg_len, i)
362 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200363
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200364 sg_init_one(mq->sg, mq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200365
366 return 1;
367}
368
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200369/*
370 * If writing, bounce the data to the buffer before the request
371 * is sent to the host driver
372 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200373void mmc_queue_bounce_pre(struct mmc_queue *mq)
374{
375 if (!mq->bounce_buf)
376 return;
377
Pierre Ossman98ccf142007-05-12 00:26:16 +0200378 if (rq_data_dir(mq->req) != WRITE)
379 return;
380
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200381 sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
382 mq->bounce_buf, mq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200383}
384
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200385/*
386 * If reading, bounce the data from the buffer after the request
387 * has been handled by the host driver
388 */
Pierre Ossman98ccf142007-05-12 00:26:16 +0200389void mmc_queue_bounce_post(struct mmc_queue *mq)
390{
391 if (!mq->bounce_buf)
392 return;
393
Pierre Ossman98ccf142007-05-12 00:26:16 +0200394 if (rq_data_dir(mq->req) != READ)
395 return;
396
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200397 sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
398 mq->bounce_buf, mq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200399}
400