2 * linux/drivers/mmc/card/queue.c
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright 2006-2007 Pierre Ossman
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.
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/blkdev.h>
15 #include <linux/freezer.h>
16 #include <linux/kthread.h>
17 #include <linux/scatterlist.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/host.h>
23 #define MMC_QUEUE_BOUNCESZ 65536
25 #define MMC_QUEUE_SUSPENDED (1 << 0)
28 * Prepare a MMC request. This just filters out odd stuff.
30 static int mmc_prep_request(struct request_queue *q, struct request *req)
32 struct mmc_queue *mq = q->queuedata;
35 * We only like normal block requests and discards.
37 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
38 blk_dump_rq_flags(req, "MMC bad request");
42 if (mq && mmc_card_removed(mq->card))
45 req->cmd_flags |= REQ_DONTPREP;
50 static int mmc_queue_thread(void *d)
52 struct mmc_queue *mq = d;
53 struct request_queue *q = mq->queue;
55 current->flags |= PF_MEMALLOC;
57 down(&mq->thread_sem);
59 struct request *req = NULL;
60 struct mmc_queue_req *tmp;
62 spin_lock_irq(q->queue_lock);
63 set_current_state(TASK_INTERRUPTIBLE);
64 req = blk_fetch_request(q);
65 mq->mqrq_cur->req = req;
66 spin_unlock_irq(q->queue_lock);
68 if (req || mq->mqrq_prev->req) {
69 set_current_state(TASK_RUNNING);
70 mq->issue_fn(mq, req);
72 if (kthread_should_stop()) {
73 set_current_state(TASK_RUNNING);
78 down(&mq->thread_sem);
81 /* Current request becomes previous request and vice versa. */
82 mq->mqrq_prev->brq.mrq.data = NULL;
83 mq->mqrq_prev->req = NULL;
85 mq->mqrq_prev = mq->mqrq_cur;
94 * Generic MMC request handler. This is called for any queue on a
95 * particular host. When the host is not busy, we look for a request
96 * on any queue on this host, and attempt to issue it. This may
97 * not be the queue we were asked to process.
99 static void mmc_request_fn(struct request_queue *q)
101 struct mmc_queue *mq = q->queuedata;
105 while ((req = blk_fetch_request(q)) != NULL) {
106 req->cmd_flags |= REQ_QUIET;
107 __blk_end_request_all(req, -EIO);
112 if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
113 wake_up_process(mq->thread);
116 static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
118 struct scatterlist *sg;
120 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
125 sg_init_table(sg, sg_len);
131 static void mmc_queue_setup_discard(struct request_queue *q,
132 struct mmc_card *card)
134 unsigned max_discard;
136 max_discard = mmc_calc_max_discard(card);
140 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
141 q->limits.max_discard_sectors = max_discard;
142 if (card->erased_byte == 0 && !mmc_can_discard(card))
143 q->limits.discard_zeroes_data = 1;
144 q->limits.discard_granularity = card->pref_erase << 9;
145 /* granularity must not be greater than max. discard */
146 if (card->pref_erase > max_discard)
147 q->limits.discard_granularity = 0;
148 if (mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))
149 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
153 * mmc_init_queue - initialise a queue structure.
155 * @card: mmc card to attach this queue
157 * @subname: partition subname
159 * Initialise a MMC card request queue.
161 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
162 spinlock_t *lock, const char *subname)
164 struct mmc_host *host = card->host;
165 u64 limit = BLK_BOUNCE_HIGH;
167 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
168 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
170 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
171 limit = *mmc_dev(host)->dma_mask;
174 mq->queue = blk_init_queue(mmc_request_fn, lock);
178 mq->mqrq_cur = mqrq_cur;
179 mq->mqrq_prev = mqrq_prev;
180 mq->queue->queuedata = mq;
182 blk_queue_prep_rq(mq->queue, mmc_prep_request);
183 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
184 if (mmc_can_erase(card))
185 mmc_queue_setup_discard(mq->queue, card);
187 #ifdef CONFIG_MMC_BLOCK_BOUNCE
188 if (host->max_segs == 1) {
189 unsigned int bouncesz;
191 bouncesz = MMC_QUEUE_BOUNCESZ;
193 if (bouncesz > host->max_req_size)
194 bouncesz = host->max_req_size;
195 if (bouncesz > host->max_seg_size)
196 bouncesz = host->max_seg_size;
197 if (bouncesz > (host->max_blk_count * 512))
198 bouncesz = host->max_blk_count * 512;
200 if (bouncesz > 512) {
201 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
202 if (!mqrq_cur->bounce_buf) {
203 pr_warning("%s: unable to "
204 "allocate bounce cur buffer\n",
205 mmc_card_name(card));
207 mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
208 if (!mqrq_prev->bounce_buf) {
209 pr_warning("%s: unable to "
210 "allocate bounce prev buffer\n",
211 mmc_card_name(card));
212 kfree(mqrq_cur->bounce_buf);
213 mqrq_cur->bounce_buf = NULL;
217 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
218 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
219 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
220 blk_queue_max_segments(mq->queue, bouncesz / 512);
221 blk_queue_max_segment_size(mq->queue, bouncesz);
223 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
227 mqrq_cur->bounce_sg =
228 mmc_alloc_sg(bouncesz / 512, &ret);
232 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
236 mqrq_prev->bounce_sg =
237 mmc_alloc_sg(bouncesz / 512, &ret);
244 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
245 blk_queue_bounce_limit(mq->queue, limit);
246 blk_queue_max_hw_sectors(mq->queue,
247 min(host->max_blk_count, host->max_req_size / 512));
248 blk_queue_max_segments(mq->queue, host->max_segs);
249 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
251 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
256 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
261 sema_init(&mq->thread_sem, 1);
263 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
264 host->index, subname ? subname : "");
266 if (IS_ERR(mq->thread)) {
267 ret = PTR_ERR(mq->thread);
273 kfree(mqrq_cur->bounce_sg);
274 mqrq_cur->bounce_sg = NULL;
275 kfree(mqrq_prev->bounce_sg);
276 mqrq_prev->bounce_sg = NULL;
281 kfree(mqrq_cur->bounce_buf);
282 mqrq_cur->bounce_buf = NULL;
284 kfree(mqrq_prev->sg);
285 mqrq_prev->sg = NULL;
286 kfree(mqrq_prev->bounce_buf);
287 mqrq_prev->bounce_buf = NULL;
289 blk_cleanup_queue(mq->queue);
293 void mmc_cleanup_queue(struct mmc_queue *mq)
295 struct request_queue *q = mq->queue;
297 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
298 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
300 /* Make sure the queue isn't suspended, as that will deadlock */
301 mmc_queue_resume(mq);
303 /* Then terminate our worker thread */
304 kthread_stop(mq->thread);
306 /* Empty the queue */
307 spin_lock_irqsave(q->queue_lock, flags);
310 spin_unlock_irqrestore(q->queue_lock, flags);
312 kfree(mqrq_cur->bounce_sg);
313 mqrq_cur->bounce_sg = NULL;
318 kfree(mqrq_cur->bounce_buf);
319 mqrq_cur->bounce_buf = NULL;
321 kfree(mqrq_prev->bounce_sg);
322 mqrq_prev->bounce_sg = NULL;
324 kfree(mqrq_prev->sg);
325 mqrq_prev->sg = NULL;
327 kfree(mqrq_prev->bounce_buf);
328 mqrq_prev->bounce_buf = NULL;
332 EXPORT_SYMBOL(mmc_cleanup_queue);
335 * mmc_queue_suspend - suspend a MMC request queue
336 * @mq: MMC queue to suspend
338 * Stop the block request queue, and wait for our thread to
339 * complete any outstanding requests. This ensures that we
340 * won't suspend while a request is being processed.
342 void mmc_queue_suspend(struct mmc_queue *mq)
344 struct request_queue *q = mq->queue;
347 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
348 mq->flags |= MMC_QUEUE_SUSPENDED;
350 spin_lock_irqsave(q->queue_lock, flags);
352 spin_unlock_irqrestore(q->queue_lock, flags);
354 down(&mq->thread_sem);
359 * mmc_queue_resume - resume a previously suspended MMC request queue
360 * @mq: MMC queue to resume
362 void mmc_queue_resume(struct mmc_queue *mq)
364 struct request_queue *q = mq->queue;
367 if (mq->flags & MMC_QUEUE_SUSPENDED) {
368 mq->flags &= ~MMC_QUEUE_SUSPENDED;
372 spin_lock_irqsave(q->queue_lock, flags);
374 spin_unlock_irqrestore(q->queue_lock, flags);
379 * Prepare the sg list(s) to be handed of to the host driver
381 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
385 struct scatterlist *sg;
388 if (!mqrq->bounce_buf)
389 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
391 BUG_ON(!mqrq->bounce_sg);
393 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
395 mqrq->bounce_sg_len = sg_len;
398 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
399 buflen += sg->length;
401 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
407 * If writing, bounce the data to the buffer before the request
408 * is sent to the host driver
410 void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
412 if (!mqrq->bounce_buf)
415 if (rq_data_dir(mqrq->req) != WRITE)
418 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
419 mqrq->bounce_buf, mqrq->sg[0].length);
423 * If reading, bounce the data from the buffer after the request
424 * has been handled by the host driver
426 void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
428 if (!mqrq->bounce_buf)
431 if (rq_data_dir(mqrq->req) != READ)
434 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
435 mqrq->bounce_buf, mqrq->sg[0].length);