2 * Copyright (C) 2003 Russell King, All Rights Reserved.
3 * Copyright 2006-2007 Pierre Ossman
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/blkdev.h>
13 #include <linux/freezer.h>
14 #include <linux/kthread.h>
15 #include <linux/scatterlist.h>
16 #include <linux/dma-mapping.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/host.h>
27 * Prepare a MMC request. This just filters out odd stuff.
29 static int mmc_prep_request(struct request_queue *q, struct request *req)
31 struct mmc_queue *mq = q->queuedata;
33 if (mq && mmc_card_removed(mq->card))
36 req->rq_flags |= RQF_DONTPREP;
41 static int mmc_queue_thread(void *d)
43 struct mmc_queue *mq = d;
44 struct request_queue *q = mq->queue;
45 struct mmc_context_info *cntx = &mq->card->host->context_info;
47 current->flags |= PF_MEMALLOC;
49 down(&mq->thread_sem);
53 spin_lock_irq(q->queue_lock);
54 set_current_state(TASK_INTERRUPTIBLE);
55 req = blk_fetch_request(q);
57 cntx->is_waiting_last_req = false;
58 cntx->is_new_req = false;
61 * Dispatch queue is empty so set flags for
62 * mmc_request_fn() to wake us up.
65 cntx->is_waiting_last_req = true;
69 spin_unlock_irq(q->queue_lock);
71 if (req || mq->qcnt) {
72 set_current_state(TASK_RUNNING);
73 mmc_blk_issue_rq(mq, req);
76 if (kthread_should_stop()) {
77 set_current_state(TASK_RUNNING);
82 down(&mq->thread_sem);
91 * Generic MMC request handler. This is called for any queue on a
92 * particular host. When the host is not busy, we look for a request
93 * on any queue on this host, and attempt to issue it. This may
94 * not be the queue we were asked to process.
96 static void mmc_request_fn(struct request_queue *q)
98 struct mmc_queue *mq = q->queuedata;
100 struct mmc_context_info *cntx;
103 while ((req = blk_fetch_request(q)) != NULL) {
104 req->rq_flags |= RQF_QUIET;
105 __blk_end_request_all(req, BLK_STS_IOERR);
110 cntx = &mq->card->host->context_info;
112 if (cntx->is_waiting_last_req) {
113 cntx->is_new_req = true;
114 wake_up_interruptible(&cntx->wait);
118 wake_up_process(mq->thread);
121 static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
123 struct scatterlist *sg;
125 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
127 sg_init_table(sg, sg_len);
132 static void mmc_queue_setup_discard(struct request_queue *q,
133 struct mmc_card *card)
135 unsigned max_discard;
137 max_discard = mmc_calc_max_discard(card);
141 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
142 blk_queue_max_discard_sectors(q, max_discard);
143 q->limits.discard_granularity = card->pref_erase << 9;
144 /* granularity must not be greater than max. discard */
145 if (card->pref_erase > max_discard)
146 q->limits.discard_granularity = SECTOR_SIZE;
147 if (mmc_can_secure_erase_trim(card))
148 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
152 * mmc_init_request() - initialize the MMC-specific per-request data
153 * @q: the request queue
155 * @gfp: memory allocation policy
157 static int mmc_init_request(struct request_queue *q, struct request *req,
160 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
161 struct mmc_queue *mq = q->queuedata;
162 struct mmc_card *card = mq->card;
163 struct mmc_host *host = card->host;
165 mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
172 static void mmc_exit_request(struct request_queue *q, struct request *req)
174 struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
181 * mmc_init_queue - initialise a queue structure.
183 * @card: mmc card to attach this queue
185 * @subname: partition subname
187 * Initialise a MMC card request queue.
189 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
190 spinlock_t *lock, const char *subname)
192 struct mmc_host *host = card->host;
193 u64 limit = BLK_BOUNCE_HIGH;
196 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
197 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
200 mq->queue = blk_alloc_queue(GFP_KERNEL);
203 mq->queue->queue_lock = lock;
204 mq->queue->request_fn = mmc_request_fn;
205 mq->queue->init_rq_fn = mmc_init_request;
206 mq->queue->exit_rq_fn = mmc_exit_request;
207 mq->queue->cmd_size = sizeof(struct mmc_queue_req);
208 mq->queue->queuedata = mq;
210 ret = blk_init_allocated_queue(mq->queue);
212 blk_cleanup_queue(mq->queue);
216 blk_queue_prep_rq(mq->queue, mmc_prep_request);
217 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
218 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
219 if (mmc_can_erase(card))
220 mmc_queue_setup_discard(mq->queue, card);
222 blk_queue_bounce_limit(mq->queue, limit);
223 blk_queue_max_hw_sectors(mq->queue,
224 min(host->max_blk_count, host->max_req_size / 512));
225 blk_queue_max_segments(mq->queue, host->max_segs);
226 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
228 sema_init(&mq->thread_sem, 1);
230 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
231 host->index, subname ? subname : "");
233 if (IS_ERR(mq->thread)) {
234 ret = PTR_ERR(mq->thread);
241 blk_cleanup_queue(mq->queue);
245 void mmc_cleanup_queue(struct mmc_queue *mq)
247 struct request_queue *q = mq->queue;
250 /* Make sure the queue isn't suspended, as that will deadlock */
251 mmc_queue_resume(mq);
253 /* Then terminate our worker thread */
254 kthread_stop(mq->thread);
256 /* Empty the queue */
257 spin_lock_irqsave(q->queue_lock, flags);
260 spin_unlock_irqrestore(q->queue_lock, flags);
264 EXPORT_SYMBOL(mmc_cleanup_queue);
267 * mmc_queue_suspend - suspend a MMC request queue
268 * @mq: MMC queue to suspend
270 * Stop the block request queue, and wait for our thread to
271 * complete any outstanding requests. This ensures that we
272 * won't suspend while a request is being processed.
274 void mmc_queue_suspend(struct mmc_queue *mq)
276 struct request_queue *q = mq->queue;
279 if (!mq->suspended) {
280 mq->suspended |= true;
282 spin_lock_irqsave(q->queue_lock, flags);
284 spin_unlock_irqrestore(q->queue_lock, flags);
286 down(&mq->thread_sem);
291 * mmc_queue_resume - resume a previously suspended MMC request queue
292 * @mq: MMC queue to resume
294 void mmc_queue_resume(struct mmc_queue *mq)
296 struct request_queue *q = mq->queue;
300 mq->suspended = false;
304 spin_lock_irqsave(q->queue_lock, flags);
306 spin_unlock_irqrestore(q->queue_lock, flags);
311 * Prepare the sg list(s) to be handed of to the host driver
313 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
315 struct request *req = mmc_queue_req_to_req(mqrq);
317 return blk_rq_map_sg(mq->queue, req, mqrq->sg);