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/module.h>
13 #include <linux/blkdev.h>
14 #if 0 // mask by Victor Yu. 12-02-2008
15 #include <linux/freezer.h>
17 #include <linux/kthread.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 #if 1 // mask by Victor Yu. 12-03-2008
34 * We only like normal block requests.
36 if (!blk_fs_request(req
) && !blk_pc_request(req
)) {
37 blk_dump_rq_flags(req
, "MMC bad request");
41 #if 0 // mask by Victor Yu. 12-02-2008
42 req
->cmd_flags
|= REQ_DONTPREP
;
44 req
->flags
|= REQ_DONTPREP
;
49 struct mmc_queue
*mq
= q
->queuedata
;
50 int ret
= BLKPREP_KILL
;
52 #if 0 // mask by Victor Yu. 12-02--2008
53 if (blk_special_request(req
)) {
55 if (req
->flags
& REQ_SPECIAL
) {
58 * Special commands already have the command
59 * blocks already setup in req->special.
61 BUG_ON(!req
->special
);
64 #if 0 // mask by Victor Yu. 12-02--2008
65 } else if (blk_fs_request(req
) || blk_pc_request(req
)) {
67 } else if (req
->flags
& (REQ_CMD
| REQ_BLOCK_PC
)) {
70 * Block I/O requests need translating according
73 ret
= mq
->prep_fn(mq
, req
);
76 * Everything else is invalid.
78 blk_dump_rq_flags(req
, "MMC bad request");
81 if (ret
== BLKPREP_OK
) {
82 #if 0 // mask by Victor Yu. 12-02--2008
83 req
->cmd_flags
|= REQ_DONTPREP
;
85 req
->flags
|= REQ_DONTPREP
;
93 static int mmc_queue_thread(void *d
)
95 struct mmc_queue
*mq
= d
;
96 struct request_queue
*q
= mq
->queue
;
98 current
->flags
|= PF_MEMALLOC
;
100 down(&mq
->thread_sem
);
102 struct request
*req
= NULL
;
104 spin_lock_irq(q
->queue_lock
);
105 set_current_state(TASK_INTERRUPTIBLE
);
106 if (!blk_queue_plugged(q
))
107 req
= elv_next_request(q
);
109 spin_unlock_irq(q
->queue_lock
);
112 if (kthread_should_stop()) {
113 set_current_state(TASK_RUNNING
);
118 down(&mq
->thread_sem
);
121 set_current_state(TASK_RUNNING
);
123 mq
->issue_fn(mq
, req
);
131 * Generic MMC request handler. This is called for any queue on a
132 * particular host. When the host is not busy, we look for a request
133 * on any queue on this host, and attempt to issue it. This may
134 * not be the queue we were asked to process.
136 static void mmc_request(struct request_queue
*q
)
138 struct mmc_queue
*mq
= q
->queuedata
;
143 printk(KERN_ERR
"MMC: killing requests for dead queue\n");
144 while ((req
= elv_next_request(q
)) != NULL
) {
146 ret
= end_that_request_chunk(req
, 0,
147 req
->current_nr_sectors
<< 9);
154 wake_up_process(mq
->thread
);
158 * mmc_init_queue - initialise a queue structure.
160 * @card: mmc card to attach this queue
163 * Initialise a MMC card request queue.
165 int mmc_init_queue(struct mmc_queue
*mq
, struct mmc_card
*card
, spinlock_t
*lock
)
167 struct mmc_host
*host
= card
->host
;
168 u64 limit
= BLK_BOUNCE_HIGH
;
171 if (mmc_dev(host
)->dma_mask
&& *mmc_dev(host
)->dma_mask
)
172 limit
= *mmc_dev(host
)->dma_mask
;
175 mq
->queue
= blk_init_queue(mmc_request
, lock
);
179 mq
->queue
->queuedata
= mq
;
182 blk_queue_prep_rq(mq
->queue
, mmc_prep_request
);
184 #ifdef CONFIG_MMC_BLOCK_BOUNCE
185 if (host
->max_hw_segs
== 1) {
186 unsigned int bouncesz
;
188 bouncesz
= MMC_QUEUE_BOUNCESZ
;
190 if (bouncesz
> host
->max_req_size
)
191 bouncesz
= host
->max_req_size
;
192 if (bouncesz
> host
->max_seg_size
)
193 bouncesz
= host
->max_seg_size
;
195 mq
->bounce_buf
= kmalloc(bouncesz
, GFP_KERNEL
);
196 if (!mq
->bounce_buf
) {
197 printk(KERN_WARNING
"%s: unable to allocate "
198 "bounce buffer\n", mmc_card_name(card
));
200 blk_queue_bounce_limit(mq
->queue
, BLK_BOUNCE_HIGH
);
201 blk_queue_max_sectors(mq
->queue
, bouncesz
/ 512);
202 blk_queue_max_phys_segments(mq
->queue
, bouncesz
/ 512);
203 blk_queue_max_hw_segments(mq
->queue
, bouncesz
/ 512);
204 blk_queue_max_segment_size(mq
->queue
, bouncesz
);
206 mq
->sg
= kmalloc(sizeof(struct scatterlist
),
213 mq
->bounce_sg
= kmalloc(sizeof(struct scatterlist
) *
214 bouncesz
/ 512, GFP_KERNEL
);
215 if (!mq
->bounce_sg
) {
223 if (!mq
->bounce_buf
) {
224 blk_queue_bounce_limit(mq
->queue
, limit
);
225 blk_queue_max_sectors(mq
->queue
, host
->max_req_size
/ 512);
226 blk_queue_max_phys_segments(mq
->queue
, host
->max_phys_segs
);
227 blk_queue_max_hw_segments(mq
->queue
, host
->max_hw_segs
);
228 blk_queue_max_segment_size(mq
->queue
, host
->max_seg_size
);
230 mq
->sg
= kmalloc(sizeof(struct scatterlist
) *
231 host
->max_phys_segs
, GFP_KERNEL
);
238 init_MUTEX(&mq
->thread_sem
);
240 mq
->thread
= kthread_run(mmc_queue_thread
, mq
, "mmcqd");
241 if (IS_ERR(mq
->thread
)) {
242 ret
= PTR_ERR(mq
->thread
);
249 kfree(mq
->bounce_sg
);
250 mq
->bounce_sg
= NULL
;
256 kfree(mq
->bounce_buf
);
257 mq
->bounce_buf
= NULL
;
258 blk_cleanup_queue(mq
->queue
);
262 void mmc_cleanup_queue(struct mmc_queue
*mq
)
264 struct request_queue
*q
= mq
->queue
;
267 /* Mark that we should start throwing out stragglers */
268 spin_lock_irqsave(q
->queue_lock
, flags
);
270 spin_unlock_irqrestore(q
->queue_lock
, flags
);
272 /* Make sure the queue isn't suspended, as that will deadlock */
273 mmc_queue_resume(mq
);
275 /* Then terminate our worker thread */
276 kthread_stop(mq
->thread
);
279 kfree(mq
->bounce_sg
);
280 mq
->bounce_sg
= NULL
;
286 kfree(mq
->bounce_buf
);
287 mq
->bounce_buf
= NULL
;
289 blk_cleanup_queue(mq
->queue
);
293 EXPORT_SYMBOL(mmc_cleanup_queue
);
296 * mmc_queue_suspend - suspend a MMC request queue
297 * @mq: MMC queue to suspend
299 * Stop the block request queue, and wait for our thread to
300 * complete any outstanding requests. This ensures that we
301 * won't suspend while a request is being processed.
303 void mmc_queue_suspend(struct mmc_queue
*mq
)
305 struct request_queue
*q
= mq
->queue
;
308 if (!(mq
->flags
& MMC_QUEUE_SUSPENDED
)) {
309 mq
->flags
|= MMC_QUEUE_SUSPENDED
;
311 spin_lock_irqsave(q
->queue_lock
, flags
);
313 spin_unlock_irqrestore(q
->queue_lock
, flags
);
315 down(&mq
->thread_sem
);
320 * mmc_queue_resume - resume a previously suspended MMC request queue
321 * @mq: MMC queue to resume
323 void mmc_queue_resume(struct mmc_queue
*mq
)
325 struct request_queue
*q
= mq
->queue
;
328 if (mq
->flags
& MMC_QUEUE_SUSPENDED
) {
329 mq
->flags
&= ~MMC_QUEUE_SUSPENDED
;
333 spin_lock_irqsave(q
->queue_lock
, flags
);
335 spin_unlock_irqrestore(q
->queue_lock
, flags
);
339 static void copy_sg(struct scatterlist
*dst
, unsigned int dst_len
,
340 struct scatterlist
*src
, unsigned int src_len
)
343 char *dst_buf
, *src_buf
;
344 unsigned int dst_size
, src_size
;
352 BUG_ON(dst_len
== 0);
355 dst_buf
= page_address(dst
->page
) + dst
->offset
;
356 dst_size
= dst
->length
;
360 src_buf
= page_address(src
->page
) + src
->offset
;
361 src_size
= src
->length
;
364 chunk
= min(dst_size
, src_size
);
366 memcpy(dst_buf
, src_buf
, chunk
);
385 unsigned int mmc_queue_map_sg(struct mmc_queue
*mq
)
390 return blk_rq_map_sg(mq
->queue
, mq
->req
, mq
->sg
);
392 BUG_ON(!mq
->bounce_sg
);
394 sg_len
= blk_rq_map_sg(mq
->queue
, mq
->req
, mq
->bounce_sg
);
396 mq
->bounce_sg_len
= sg_len
;
399 * Shortcut in the event we only get a single entry.
402 memcpy(mq
->sg
, mq
->bounce_sg
, sizeof(struct scatterlist
));
406 mq
->sg
[0].page
= virt_to_page(mq
->bounce_buf
);
407 mq
->sg
[0].offset
= offset_in_page(mq
->bounce_buf
);
408 mq
->sg
[0].length
= 0;
411 mq
->sg
[0].length
+= mq
->bounce_sg
[sg_len
- 1].length
;
418 void mmc_queue_bounce_pre(struct mmc_queue
*mq
)
423 if (mq
->bounce_sg_len
== 1)
425 if (rq_data_dir(mq
->req
) != WRITE
)
428 copy_sg(mq
->sg
, 1, mq
->bounce_sg
, mq
->bounce_sg_len
);
431 void mmc_queue_bounce_post(struct mmc_queue
*mq
)
436 if (mq
->bounce_sg_len
== 1)
438 if (rq_data_dir(mq
->req
) != READ
)
441 copy_sg(mq
->bounce_sg
, mq
->bounce_sg_len
, mq
->sg
, 1);