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>
26 #define MMC_QUEUE_BOUNCESZ 65536
29 * Prepare a MMC request. This just filters out odd stuff.
31 static int mmc_prep_request(struct request_queue
*q
, struct request
*req
)
33 struct mmc_queue
*mq
= q
->queuedata
;
35 if (mq
&& (mmc_card_removed(mq
->card
) || mmc_access_rpmb(mq
)))
38 req
->rq_flags
|= RQF_DONTPREP
;
43 static int mmc_queue_thread(void *d
)
45 struct mmc_queue
*mq
= d
;
46 struct request_queue
*q
= mq
->queue
;
47 struct mmc_context_info
*cntx
= &mq
->card
->host
->context_info
;
49 current
->flags
|= PF_MEMALLOC
;
51 down(&mq
->thread_sem
);
55 spin_lock_irq(q
->queue_lock
);
56 set_current_state(TASK_INTERRUPTIBLE
);
57 req
= blk_fetch_request(q
);
59 cntx
->is_waiting_last_req
= false;
60 cntx
->is_new_req
= false;
63 * Dispatch queue is empty so set flags for
64 * mmc_request_fn() to wake us up.
67 cntx
->is_waiting_last_req
= true;
71 spin_unlock_irq(q
->queue_lock
);
73 if (req
|| mq
->qcnt
) {
74 set_current_state(TASK_RUNNING
);
75 mmc_blk_issue_rq(mq
, req
);
78 if (kthread_should_stop()) {
79 set_current_state(TASK_RUNNING
);
84 down(&mq
->thread_sem
);
93 * Generic MMC request handler. This is called for any queue on a
94 * particular host. When the host is not busy, we look for a request
95 * on any queue on this host, and attempt to issue it. This may
96 * not be the queue we were asked to process.
98 static void mmc_request_fn(struct request_queue
*q
)
100 struct mmc_queue
*mq
= q
->queuedata
;
102 struct mmc_context_info
*cntx
;
105 while ((req
= blk_fetch_request(q
)) != NULL
) {
106 req
->rq_flags
|= RQF_QUIET
;
107 __blk_end_request_all(req
, BLK_STS_IOERR
);
112 cntx
= &mq
->card
->host
->context_info
;
114 if (cntx
->is_waiting_last_req
) {
115 cntx
->is_new_req
= true;
116 wake_up_interruptible(&cntx
->wait
);
120 wake_up_process(mq
->thread
);
123 static struct scatterlist
*mmc_alloc_sg(int sg_len
, gfp_t gfp
)
125 struct scatterlist
*sg
;
127 sg
= kmalloc_array(sg_len
, sizeof(*sg
), gfp
);
129 sg_init_table(sg
, sg_len
);
134 static void mmc_queue_setup_discard(struct request_queue
*q
,
135 struct mmc_card
*card
)
137 unsigned max_discard
;
139 max_discard
= mmc_calc_max_discard(card
);
143 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, q
);
144 blk_queue_max_discard_sectors(q
, max_discard
);
145 q
->limits
.discard_granularity
= card
->pref_erase
<< 9;
146 /* granularity must not be greater than max. discard */
147 if (card
->pref_erase
> max_discard
)
148 q
->limits
.discard_granularity
= 0;
149 if (mmc_can_secure_erase_trim(card
))
150 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE
, q
);
153 static unsigned int mmc_queue_calc_bouncesz(struct mmc_host
*host
)
155 unsigned int bouncesz
= MMC_QUEUE_BOUNCESZ
;
157 if (host
->max_segs
!= 1 || (host
->caps
& MMC_CAP_NO_BOUNCE_BUFF
))
160 if (bouncesz
> host
->max_req_size
)
161 bouncesz
= host
->max_req_size
;
162 if (bouncesz
> host
->max_seg_size
)
163 bouncesz
= host
->max_seg_size
;
164 if (bouncesz
> host
->max_blk_count
* 512)
165 bouncesz
= host
->max_blk_count
* 512;
174 * mmc_init_request() - initialize the MMC-specific per-request data
175 * @q: the request queue
177 * @gfp: memory allocation policy
179 static int mmc_init_request(struct request_queue
*q
, struct request
*req
,
182 struct mmc_queue_req
*mq_rq
= req_to_mmc_queue_req(req
);
183 struct mmc_queue
*mq
= q
->queuedata
;
184 struct mmc_card
*card
= mq
->card
;
185 struct mmc_host
*host
= card
->host
;
187 if (card
->bouncesz
) {
188 mq_rq
->bounce_buf
= kmalloc(card
->bouncesz
, gfp
);
189 if (!mq_rq
->bounce_buf
)
191 if (card
->bouncesz
> 512) {
192 mq_rq
->sg
= mmc_alloc_sg(1, gfp
);
195 mq_rq
->bounce_sg
= mmc_alloc_sg(card
->bouncesz
/ 512,
197 if (!mq_rq
->bounce_sg
)
201 mq_rq
->bounce_buf
= NULL
;
202 mq_rq
->bounce_sg
= NULL
;
203 mq_rq
->sg
= mmc_alloc_sg(host
->max_segs
, gfp
);
211 static void mmc_exit_request(struct request_queue
*q
, struct request
*req
)
213 struct mmc_queue_req
*mq_rq
= req_to_mmc_queue_req(req
);
215 /* It is OK to kfree(NULL) so this will be smooth */
216 kfree(mq_rq
->bounce_sg
);
217 mq_rq
->bounce_sg
= NULL
;
219 kfree(mq_rq
->bounce_buf
);
220 mq_rq
->bounce_buf
= NULL
;
227 * mmc_init_queue - initialise a queue structure.
229 * @card: mmc card to attach this queue
231 * @subname: partition subname
233 * Initialise a MMC card request queue.
235 int mmc_init_queue(struct mmc_queue
*mq
, struct mmc_card
*card
,
236 spinlock_t
*lock
, const char *subname
)
238 struct mmc_host
*host
= card
->host
;
239 u64 limit
= BLK_BOUNCE_HIGH
;
242 if (mmc_dev(host
)->dma_mask
&& *mmc_dev(host
)->dma_mask
)
243 limit
= (u64
)dma_max_pfn(mmc_dev(host
)) << PAGE_SHIFT
;
246 mq
->queue
= blk_alloc_queue(GFP_KERNEL
);
249 mq
->queue
->queue_lock
= lock
;
250 mq
->queue
->request_fn
= mmc_request_fn
;
251 mq
->queue
->init_rq_fn
= mmc_init_request
;
252 mq
->queue
->exit_rq_fn
= mmc_exit_request
;
253 mq
->queue
->cmd_size
= sizeof(struct mmc_queue_req
);
254 mq
->queue
->queuedata
= mq
;
256 ret
= blk_init_allocated_queue(mq
->queue
);
258 blk_cleanup_queue(mq
->queue
);
262 blk_queue_prep_rq(mq
->queue
, mmc_prep_request
);
263 queue_flag_set_unlocked(QUEUE_FLAG_NONROT
, mq
->queue
);
264 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM
, mq
->queue
);
265 if (mmc_can_erase(card
))
266 mmc_queue_setup_discard(mq
->queue
, card
);
268 card
->bouncesz
= mmc_queue_calc_bouncesz(host
);
269 if (card
->bouncesz
) {
270 blk_queue_max_hw_sectors(mq
->queue
, card
->bouncesz
/ 512);
271 blk_queue_max_segments(mq
->queue
, card
->bouncesz
/ 512);
272 blk_queue_max_segment_size(mq
->queue
, card
->bouncesz
);
274 blk_queue_bounce_limit(mq
->queue
, limit
);
275 blk_queue_max_hw_sectors(mq
->queue
,
276 min(host
->max_blk_count
, host
->max_req_size
/ 512));
277 blk_queue_max_segments(mq
->queue
, host
->max_segs
);
278 blk_queue_max_segment_size(mq
->queue
, host
->max_seg_size
);
281 sema_init(&mq
->thread_sem
, 1);
283 mq
->thread
= kthread_run(mmc_queue_thread
, mq
, "mmcqd/%d%s",
284 host
->index
, subname
? subname
: "");
286 if (IS_ERR(mq
->thread
)) {
287 ret
= PTR_ERR(mq
->thread
);
294 blk_cleanup_queue(mq
->queue
);
298 void mmc_cleanup_queue(struct mmc_queue
*mq
)
300 struct request_queue
*q
= mq
->queue
;
303 /* Make sure the queue isn't suspended, as that will deadlock */
304 mmc_queue_resume(mq
);
306 /* Then terminate our worker thread */
307 kthread_stop(mq
->thread
);
309 /* Empty the queue */
310 spin_lock_irqsave(q
->queue_lock
, flags
);
313 spin_unlock_irqrestore(q
->queue_lock
, flags
);
317 EXPORT_SYMBOL(mmc_cleanup_queue
);
320 * mmc_queue_suspend - suspend a MMC request queue
321 * @mq: MMC queue to suspend
323 * Stop the block request queue, and wait for our thread to
324 * complete any outstanding requests. This ensures that we
325 * won't suspend while a request is being processed.
327 void mmc_queue_suspend(struct mmc_queue
*mq
)
329 struct request_queue
*q
= mq
->queue
;
332 if (!mq
->suspended
) {
333 mq
->suspended
|= true;
335 spin_lock_irqsave(q
->queue_lock
, flags
);
337 spin_unlock_irqrestore(q
->queue_lock
, flags
);
339 down(&mq
->thread_sem
);
344 * mmc_queue_resume - resume a previously suspended MMC request queue
345 * @mq: MMC queue to resume
347 void mmc_queue_resume(struct mmc_queue
*mq
)
349 struct request_queue
*q
= mq
->queue
;
353 mq
->suspended
= false;
357 spin_lock_irqsave(q
->queue_lock
, flags
);
359 spin_unlock_irqrestore(q
->queue_lock
, flags
);
364 * Prepare the sg list(s) to be handed of to the host driver
366 unsigned int mmc_queue_map_sg(struct mmc_queue
*mq
, struct mmc_queue_req
*mqrq
)
370 struct scatterlist
*sg
;
371 struct request
*req
= mmc_queue_req_to_req(mqrq
);
374 if (!mqrq
->bounce_buf
)
375 return blk_rq_map_sg(mq
->queue
, req
, mqrq
->sg
);
377 sg_len
= blk_rq_map_sg(mq
->queue
, req
, mqrq
->bounce_sg
);
379 mqrq
->bounce_sg_len
= sg_len
;
382 for_each_sg(mqrq
->bounce_sg
, sg
, sg_len
, i
)
383 buflen
+= sg
->length
;
385 sg_init_one(mqrq
->sg
, mqrq
->bounce_buf
, buflen
);
391 * If writing, bounce the data to the buffer before the request
392 * is sent to the host driver
394 void mmc_queue_bounce_pre(struct mmc_queue_req
*mqrq
)
396 if (!mqrq
->bounce_buf
)
399 if (rq_data_dir(mmc_queue_req_to_req(mqrq
)) != WRITE
)
402 sg_copy_to_buffer(mqrq
->bounce_sg
, mqrq
->bounce_sg_len
,
403 mqrq
->bounce_buf
, mqrq
->sg
[0].length
);
407 * If reading, bounce the data from the buffer after the request
408 * has been handled by the host driver
410 void mmc_queue_bounce_post(struct mmc_queue_req
*mqrq
)
412 if (!mqrq
->bounce_buf
)
415 if (rq_data_dir(mmc_queue_req_to_req(mqrq
)) != READ
)
418 sg_copy_from_buffer(mqrq
->bounce_sg
, mqrq
->bounce_sg_len
,
419 mqrq
->bounce_buf
, mqrq
->sg
[0].length
);