2 * linux/drivers/mmc/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/module.h>
13 #include <linux/blkdev.h>
14 #include <linux/kthread.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/host.h>
20 #define MMC_QUEUE_SUSPENDED (1 << 0)
23 * Prepare a MMC request. This just filters out odd stuff.
25 static int mmc_prep_request(struct request_queue
*q
, struct request
*req
)
28 * We only like normal block requests.
30 if (!blk_fs_request(req
) && !blk_pc_request(req
)) {
31 blk_dump_rq_flags(req
, "MMC bad request");
35 req
->cmd_flags
|= REQ_DONTPREP
;
40 static int mmc_queue_thread(void *d
)
42 struct mmc_queue
*mq
= d
;
43 struct request_queue
*q
= mq
->queue
;
46 * Set iothread to ensure that we aren't put to sleep by
47 * the process freezing. We handle suspension ourselves.
49 current
->flags
|= PF_MEMALLOC
|PF_NOFREEZE
;
51 down(&mq
->thread_sem
);
53 struct request
*req
= NULL
;
55 spin_lock_irq(q
->queue_lock
);
56 set_current_state(TASK_INTERRUPTIBLE
);
57 if (!blk_queue_plugged(q
))
58 req
= elv_next_request(q
);
60 spin_unlock_irq(q
->queue_lock
);
63 if (kthread_should_stop()) {
64 set_current_state(TASK_RUNNING
);
69 down(&mq
->thread_sem
);
72 set_current_state(TASK_RUNNING
);
74 mq
->issue_fn(mq
, req
);
82 * Generic MMC request handler. This is called for any queue on a
83 * particular host. When the host is not busy, we look for a request
84 * on any queue on this host, and attempt to issue it. This may
85 * not be the queue we were asked to process.
87 static void mmc_request(request_queue_t
*q
)
89 struct mmc_queue
*mq
= q
->queuedata
;
94 printk(KERN_ERR
"MMC: killing requests for dead queue\n");
95 while ((req
= elv_next_request(q
)) != NULL
) {
97 ret
= __blk_end_request(req
, -EIO
,
98 blk_rq_cur_bytes(req
));
105 wake_up_process(mq
->thread
);
109 * mmc_init_queue - initialise a queue structure.
111 * @card: mmc card to attach this queue
114 * Initialise a MMC card request queue.
116 int mmc_init_queue(struct mmc_queue
*mq
, struct mmc_card
*card
, spinlock_t
*lock
)
118 struct mmc_host
*host
= card
->host
;
119 u64 limit
= BLK_BOUNCE_HIGH
;
122 if (mmc_dev(host
)->dma_mask
&& *mmc_dev(host
)->dma_mask
)
123 limit
= *mmc_dev(host
)->dma_mask
;
126 mq
->queue
= blk_init_queue(mmc_request
, lock
);
130 blk_queue_prep_rq(mq
->queue
, mmc_prep_request
);
131 blk_queue_bounce_limit(mq
->queue
, limit
);
132 blk_queue_max_sectors(mq
->queue
, host
->max_req_size
/ 512);
133 blk_queue_max_phys_segments(mq
->queue
, host
->max_phys_segs
);
134 blk_queue_max_hw_segments(mq
->queue
, host
->max_hw_segs
);
135 blk_queue_max_segment_size(mq
->queue
, host
->max_seg_size
);
137 mq
->queue
->queuedata
= mq
;
140 mq
->sg
= kmalloc(sizeof(struct scatterlist
) * host
->max_phys_segs
,
147 init_MUTEX(&mq
->thread_sem
);
149 mq
->thread
= kthread_run(mmc_queue_thread
, mq
, "mmcqd");
150 if (IS_ERR(mq
->thread
)) {
151 ret
= PTR_ERR(mq
->thread
);
161 blk_cleanup_queue(mq
->queue
);
165 void mmc_cleanup_queue(struct mmc_queue
*mq
)
167 request_queue_t
*q
= mq
->queue
;
170 /* Mark that we should start throwing out stragglers */
171 spin_lock_irqsave(q
->queue_lock
, flags
);
173 spin_unlock_irqrestore(q
->queue_lock
, flags
);
175 /* Make sure the queue isn't suspended, as that will deadlock */
176 mmc_queue_resume(mq
);
178 /* Then terminate our worker thread */
179 kthread_stop(mq
->thread
);
184 blk_cleanup_queue(mq
->queue
);
188 EXPORT_SYMBOL(mmc_cleanup_queue
);
191 * mmc_queue_suspend - suspend a MMC request queue
192 * @mq: MMC queue to suspend
194 * Stop the block request queue, and wait for our thread to
195 * complete any outstanding requests. This ensures that we
196 * won't suspend while a request is being processed.
198 void mmc_queue_suspend(struct mmc_queue
*mq
)
200 request_queue_t
*q
= mq
->queue
;
203 if (!(mq
->flags
& MMC_QUEUE_SUSPENDED
)) {
204 mq
->flags
|= MMC_QUEUE_SUSPENDED
;
206 spin_lock_irqsave(q
->queue_lock
, flags
);
208 spin_unlock_irqrestore(q
->queue_lock
, flags
);
210 down(&mq
->thread_sem
);
215 * mmc_queue_resume - resume a previously suspended MMC request queue
216 * @mq: MMC queue to resume
218 void mmc_queue_resume(struct mmc_queue
*mq
)
220 request_queue_t
*q
= mq
->queue
;
223 if (mq
->flags
& MMC_QUEUE_SUSPENDED
) {
224 mq
->flags
&= ~MMC_QUEUE_SUSPENDED
;
228 spin_lock_irqsave(q
->queue_lock
, flags
);
230 spin_unlock_irqrestore(q
->queue_lock
, flags
);