]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
70f10482 | 2 | * linux/drivers/mmc/card/queue.c |
1da177e4 LT |
3 | * |
4 | * Copyright (C) 2003 Russell King, All Rights Reserved. | |
98ac2162 | 5 | * Copyright 2006-2007 Pierre Ossman |
1da177e4 LT |
6 | * |
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. | |
10 | * | |
11 | */ | |
5a0e3ad6 | 12 | #include <linux/slab.h> |
1da177e4 LT |
13 | #include <linux/module.h> |
14 | #include <linux/blkdev.h> | |
83144186 | 15 | #include <linux/freezer.h> |
87598a2b | 16 | #include <linux/kthread.h> |
45711f1a | 17 | #include <linux/scatterlist.h> |
1da177e4 LT |
18 | |
19 | #include <linux/mmc/card.h> | |
20 | #include <linux/mmc/host.h> | |
98ac2162 | 21 | #include "queue.h" |
1da177e4 | 22 | |
98ccf149 PO |
23 | #define MMC_QUEUE_BOUNCESZ 65536 |
24 | ||
87598a2b | 25 | #define MMC_QUEUE_SUSPENDED (1 << 0) |
1da177e4 LT |
26 | |
27 | /* | |
9c9f2d63 | 28 | * Prepare a MMC request. This just filters out odd stuff. |
1da177e4 LT |
29 | */ |
30 | static int mmc_prep_request(struct request_queue *q, struct request *req) | |
31 | { | |
a8ad82cc SRT |
32 | struct mmc_queue *mq = q->queuedata; |
33 | ||
9c9f2d63 | 34 | /* |
bd788c96 | 35 | * We only like normal block requests and discards. |
9c9f2d63 | 36 | */ |
bd788c96 | 37 | if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) { |
1da177e4 | 38 | blk_dump_rq_flags(req, "MMC bad request"); |
9c9f2d63 | 39 | return BLKPREP_KILL; |
1da177e4 LT |
40 | } |
41 | ||
a8ad82cc SRT |
42 | if (mq && mmc_card_removed(mq->card)) |
43 | return BLKPREP_KILL; | |
44 | ||
9c9f2d63 | 45 | req->cmd_flags |= REQ_DONTPREP; |
1da177e4 | 46 | |
9c9f2d63 | 47 | return BLKPREP_OK; |
1da177e4 LT |
48 | } |
49 | ||
50 | static int mmc_queue_thread(void *d) | |
51 | { | |
52 | struct mmc_queue *mq = d; | |
53 | struct request_queue *q = mq->queue; | |
1da177e4 | 54 | |
83144186 | 55 | current->flags |= PF_MEMALLOC; |
1da177e4 | 56 | |
1da177e4 | 57 | down(&mq->thread_sem); |
1da177e4 LT |
58 | do { |
59 | struct request *req = NULL; | |
ee8a43a5 | 60 | struct mmc_queue_req *tmp; |
1da177e4 LT |
61 | |
62 | spin_lock_irq(q->queue_lock); | |
63 | set_current_state(TASK_INTERRUPTIBLE); | |
7eaceacc | 64 | req = blk_fetch_request(q); |
97868a2b | 65 | mq->mqrq_cur->req = req; |
1da177e4 LT |
66 | spin_unlock_irq(q->queue_lock); |
67 | ||
ee8a43a5 PF |
68 | if (req || mq->mqrq_prev->req) { |
69 | set_current_state(TASK_RUNNING); | |
70 | mq->issue_fn(mq, req); | |
45c5a914 SJ |
71 | |
72 | /* | |
73 | * Current request becomes previous request | |
74 | * and vice versa. | |
75 | */ | |
76 | mq->mqrq_prev->brq.mrq.data = NULL; | |
77 | mq->mqrq_prev->req = NULL; | |
78 | tmp = mq->mqrq_prev; | |
79 | mq->mqrq_prev = mq->mqrq_cur; | |
80 | mq->mqrq_cur = tmp; | |
ee8a43a5 | 81 | } else { |
7b30d281 VW |
82 | if (kthread_should_stop()) { |
83 | set_current_state(TASK_RUNNING); | |
1da177e4 | 84 | break; |
7b30d281 | 85 | } |
1da177e4 LT |
86 | up(&mq->thread_sem); |
87 | schedule(); | |
88 | down(&mq->thread_sem); | |
1da177e4 | 89 | } |
1da177e4 | 90 | } while (1); |
1da177e4 LT |
91 | up(&mq->thread_sem); |
92 | ||
1da177e4 LT |
93 | return 0; |
94 | } | |
95 | ||
96 | /* | |
97 | * Generic MMC request handler. This is called for any queue on a | |
98 | * particular host. When the host is not busy, we look for a request | |
99 | * on any queue on this host, and attempt to issue it. This may | |
100 | * not be the queue we were asked to process. | |
101 | */ | |
1b50f5f3 | 102 | static void mmc_request_fn(struct request_queue *q) |
1da177e4 LT |
103 | { |
104 | struct mmc_queue *mq = q->queuedata; | |
89b4e133 | 105 | struct request *req; |
89b4e133 PO |
106 | |
107 | if (!mq) { | |
5fa83ce2 AH |
108 | while ((req = blk_fetch_request(q)) != NULL) { |
109 | req->cmd_flags |= REQ_QUIET; | |
296b2f6a | 110 | __blk_end_request_all(req, -EIO); |
5fa83ce2 | 111 | } |
89b4e133 PO |
112 | return; |
113 | } | |
1da177e4 | 114 | |
ee8a43a5 | 115 | if (!mq->mqrq_cur->req && !mq->mqrq_prev->req) |
87598a2b | 116 | wake_up_process(mq->thread); |
1da177e4 LT |
117 | } |
118 | ||
7513cd7a | 119 | static struct scatterlist *mmc_alloc_sg(int sg_len, int *err) |
97868a2b PF |
120 | { |
121 | struct scatterlist *sg; | |
122 | ||
123 | sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL); | |
124 | if (!sg) | |
125 | *err = -ENOMEM; | |
126 | else { | |
127 | *err = 0; | |
128 | sg_init_table(sg, sg_len); | |
129 | } | |
130 | ||
131 | return sg; | |
132 | } | |
133 | ||
e056a1b5 AH |
134 | static void mmc_queue_setup_discard(struct request_queue *q, |
135 | struct mmc_card *card) | |
136 | { | |
137 | unsigned max_discard; | |
138 | ||
139 | max_discard = mmc_calc_max_discard(card); | |
140 | if (!max_discard) | |
141 | return; | |
142 | ||
143 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); | |
144 | q->limits.max_discard_sectors = max_discard; | |
7194efb8 | 145 | if (card->erased_byte == 0 && !mmc_can_discard(card)) |
e056a1b5 AH |
146 | q->limits.discard_zeroes_data = 1; |
147 | q->limits.discard_granularity = card->pref_erase << 9; | |
148 | /* granularity must not be greater than max. discard */ | |
149 | if (card->pref_erase > max_discard) | |
150 | q->limits.discard_granularity = 0; | |
d9ddd629 | 151 | if (mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card)) |
e056a1b5 AH |
152 | queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q); |
153 | } | |
154 | ||
1da177e4 LT |
155 | /** |
156 | * mmc_init_queue - initialise a queue structure. | |
157 | * @mq: mmc queue | |
158 | * @card: mmc card to attach this queue | |
159 | * @lock: queue lock | |
d09408ad | 160 | * @subname: partition subname |
1da177e4 LT |
161 | * |
162 | * Initialise a MMC card request queue. | |
163 | */ | |
d09408ad AH |
164 | int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, |
165 | spinlock_t *lock, const char *subname) | |
1da177e4 LT |
166 | { |
167 | struct mmc_host *host = card->host; | |
168 | u64 limit = BLK_BOUNCE_HIGH; | |
169 | int ret; | |
97868a2b | 170 | struct mmc_queue_req *mqrq_cur = &mq->mqrq[0]; |
04296b7b | 171 | struct mmc_queue_req *mqrq_prev = &mq->mqrq[1]; |
1da177e4 | 172 | |
fcaf71fd GKH |
173 | if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask) |
174 | limit = *mmc_dev(host)->dma_mask; | |
1da177e4 LT |
175 | |
176 | mq->card = card; | |
1b50f5f3 | 177 | mq->queue = blk_init_queue(mmc_request_fn, lock); |
1da177e4 LT |
178 | if (!mq->queue) |
179 | return -ENOMEM; | |
180 | ||
97868a2b | 181 | mq->mqrq_cur = mqrq_cur; |
04296b7b | 182 | mq->mqrq_prev = mqrq_prev; |
1da177e4 | 183 | mq->queue->queuedata = mq; |
1da177e4 | 184 | |
98ccf149 | 185 | blk_queue_prep_rq(mq->queue, mmc_prep_request); |
8dddfe19 | 186 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue); |
e056a1b5 AH |
187 | if (mmc_can_erase(card)) |
188 | mmc_queue_setup_discard(mq->queue, card); | |
98ccf149 PO |
189 | |
190 | #ifdef CONFIG_MMC_BLOCK_BOUNCE | |
a36274e0 | 191 | if (host->max_segs == 1) { |
aafabfab PO |
192 | unsigned int bouncesz; |
193 | ||
98ccf149 PO |
194 | bouncesz = MMC_QUEUE_BOUNCESZ; |
195 | ||
196 | if (bouncesz > host->max_req_size) | |
197 | bouncesz = host->max_req_size; | |
198 | if (bouncesz > host->max_seg_size) | |
199 | bouncesz = host->max_seg_size; | |
f3eb0aaa PO |
200 | if (bouncesz > (host->max_blk_count * 512)) |
201 | bouncesz = host->max_blk_count * 512; | |
202 | ||
203 | if (bouncesz > 512) { | |
97868a2b PF |
204 | mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); |
205 | if (!mqrq_cur->bounce_buf) { | |
a3c76eb9 | 206 | pr_warning("%s: unable to " |
97868a2b | 207 | "allocate bounce cur buffer\n", |
f3eb0aaa PO |
208 | mmc_card_name(card)); |
209 | } | |
04296b7b PF |
210 | mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); |
211 | if (!mqrq_prev->bounce_buf) { | |
a3c76eb9 | 212 | pr_warning("%s: unable to " |
04296b7b PF |
213 | "allocate bounce prev buffer\n", |
214 | mmc_card_name(card)); | |
215 | kfree(mqrq_cur->bounce_buf); | |
216 | mqrq_cur->bounce_buf = NULL; | |
217 | } | |
f3eb0aaa | 218 | } |
98ccf149 | 219 | |
04296b7b | 220 | if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) { |
2ff1fa67 | 221 | blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY); |
086fa5ff | 222 | blk_queue_max_hw_sectors(mq->queue, bouncesz / 512); |
8a78362c | 223 | blk_queue_max_segments(mq->queue, bouncesz / 512); |
98ccf149 PO |
224 | blk_queue_max_segment_size(mq->queue, bouncesz); |
225 | ||
97868a2b PF |
226 | mqrq_cur->sg = mmc_alloc_sg(1, &ret); |
227 | if (ret) | |
aafabfab | 228 | goto cleanup_queue; |
98ccf149 | 229 | |
97868a2b PF |
230 | mqrq_cur->bounce_sg = |
231 | mmc_alloc_sg(bouncesz / 512, &ret); | |
232 | if (ret) | |
aafabfab | 233 | goto cleanup_queue; |
97868a2b | 234 | |
04296b7b PF |
235 | mqrq_prev->sg = mmc_alloc_sg(1, &ret); |
236 | if (ret) | |
237 | goto cleanup_queue; | |
238 | ||
239 | mqrq_prev->bounce_sg = | |
240 | mmc_alloc_sg(bouncesz / 512, &ret); | |
241 | if (ret) | |
242 | goto cleanup_queue; | |
98ccf149 PO |
243 | } |
244 | } | |
245 | #endif | |
246 | ||
04296b7b | 247 | if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) { |
98ccf149 | 248 | blk_queue_bounce_limit(mq->queue, limit); |
086fa5ff | 249 | blk_queue_max_hw_sectors(mq->queue, |
f3eb0aaa | 250 | min(host->max_blk_count, host->max_req_size / 512)); |
a36274e0 | 251 | blk_queue_max_segments(mq->queue, host->max_segs); |
98ccf149 PO |
252 | blk_queue_max_segment_size(mq->queue, host->max_seg_size); |
253 | ||
97868a2b PF |
254 | mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret); |
255 | if (ret) | |
98ccf149 | 256 | goto cleanup_queue; |
97868a2b | 257 | |
04296b7b PF |
258 | |
259 | mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret); | |
260 | if (ret) | |
261 | goto cleanup_queue; | |
1da177e4 LT |
262 | } |
263 | ||
632cf92a | 264 | sema_init(&mq->thread_sem, 1); |
1da177e4 | 265 | |
d09408ad AH |
266 | mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s", |
267 | host->index, subname ? subname : ""); | |
de528fa3 | 268 | |
87598a2b CH |
269 | if (IS_ERR(mq->thread)) { |
270 | ret = PTR_ERR(mq->thread); | |
98ccf149 | 271 | goto free_bounce_sg; |
1da177e4 LT |
272 | } |
273 | ||
87598a2b | 274 | return 0; |
98ccf149 | 275 | free_bounce_sg: |
97868a2b PF |
276 | kfree(mqrq_cur->bounce_sg); |
277 | mqrq_cur->bounce_sg = NULL; | |
04296b7b PF |
278 | kfree(mqrq_prev->bounce_sg); |
279 | mqrq_prev->bounce_sg = NULL; | |
97868a2b | 280 | |
aafabfab | 281 | cleanup_queue: |
97868a2b PF |
282 | kfree(mqrq_cur->sg); |
283 | mqrq_cur->sg = NULL; | |
284 | kfree(mqrq_cur->bounce_buf); | |
285 | mqrq_cur->bounce_buf = NULL; | |
286 | ||
04296b7b PF |
287 | kfree(mqrq_prev->sg); |
288 | mqrq_prev->sg = NULL; | |
289 | kfree(mqrq_prev->bounce_buf); | |
290 | mqrq_prev->bounce_buf = NULL; | |
291 | ||
1da177e4 | 292 | blk_cleanup_queue(mq->queue); |
1da177e4 LT |
293 | return ret; |
294 | } | |
1da177e4 LT |
295 | |
296 | void mmc_cleanup_queue(struct mmc_queue *mq) | |
297 | { | |
165125e1 | 298 | struct request_queue *q = mq->queue; |
89b4e133 | 299 | unsigned long flags; |
97868a2b | 300 | struct mmc_queue_req *mqrq_cur = mq->mqrq_cur; |
04296b7b | 301 | struct mmc_queue_req *mqrq_prev = mq->mqrq_prev; |
89b4e133 | 302 | |
d2b46f66 PO |
303 | /* Make sure the queue isn't suspended, as that will deadlock */ |
304 | mmc_queue_resume(mq); | |
305 | ||
89b4e133 | 306 | /* Then terminate our worker thread */ |
87598a2b | 307 | kthread_stop(mq->thread); |
1da177e4 | 308 | |
5fa83ce2 AH |
309 | /* Empty the queue */ |
310 | spin_lock_irqsave(q->queue_lock, flags); | |
311 | q->queuedata = NULL; | |
312 | blk_start_queue(q); | |
313 | spin_unlock_irqrestore(q->queue_lock, flags); | |
314 | ||
97868a2b PF |
315 | kfree(mqrq_cur->bounce_sg); |
316 | mqrq_cur->bounce_sg = NULL; | |
98ccf149 | 317 | |
97868a2b PF |
318 | kfree(mqrq_cur->sg); |
319 | mqrq_cur->sg = NULL; | |
1da177e4 | 320 | |
97868a2b PF |
321 | kfree(mqrq_cur->bounce_buf); |
322 | mqrq_cur->bounce_buf = NULL; | |
98ccf149 | 323 | |
04296b7b PF |
324 | kfree(mqrq_prev->bounce_sg); |
325 | mqrq_prev->bounce_sg = NULL; | |
326 | ||
327 | kfree(mqrq_prev->sg); | |
328 | mqrq_prev->sg = NULL; | |
329 | ||
330 | kfree(mqrq_prev->bounce_buf); | |
331 | mqrq_prev->bounce_buf = NULL; | |
332 | ||
1da177e4 LT |
333 | mq->card = NULL; |
334 | } | |
335 | EXPORT_SYMBOL(mmc_cleanup_queue); | |
336 | ||
337 | /** | |
338 | * mmc_queue_suspend - suspend a MMC request queue | |
339 | * @mq: MMC queue to suspend | |
340 | * | |
341 | * Stop the block request queue, and wait for our thread to | |
342 | * complete any outstanding requests. This ensures that we | |
343 | * won't suspend while a request is being processed. | |
344 | */ | |
345 | void mmc_queue_suspend(struct mmc_queue *mq) | |
346 | { | |
165125e1 | 347 | struct request_queue *q = mq->queue; |
1da177e4 LT |
348 | unsigned long flags; |
349 | ||
350 | if (!(mq->flags & MMC_QUEUE_SUSPENDED)) { | |
351 | mq->flags |= MMC_QUEUE_SUSPENDED; | |
352 | ||
353 | spin_lock_irqsave(q->queue_lock, flags); | |
354 | blk_stop_queue(q); | |
355 | spin_unlock_irqrestore(q->queue_lock, flags); | |
356 | ||
357 | down(&mq->thread_sem); | |
358 | } | |
359 | } | |
1da177e4 LT |
360 | |
361 | /** | |
362 | * mmc_queue_resume - resume a previously suspended MMC request queue | |
363 | * @mq: MMC queue to resume | |
364 | */ | |
365 | void mmc_queue_resume(struct mmc_queue *mq) | |
366 | { | |
165125e1 | 367 | struct request_queue *q = mq->queue; |
1da177e4 LT |
368 | unsigned long flags; |
369 | ||
370 | if (mq->flags & MMC_QUEUE_SUSPENDED) { | |
371 | mq->flags &= ~MMC_QUEUE_SUSPENDED; | |
372 | ||
373 | up(&mq->thread_sem); | |
374 | ||
375 | spin_lock_irqsave(q->queue_lock, flags); | |
376 | blk_start_queue(q); | |
377 | spin_unlock_irqrestore(q->queue_lock, flags); | |
378 | } | |
379 | } | |
98ac2162 | 380 | |
2ff1fa67 PO |
381 | /* |
382 | * Prepare the sg list(s) to be handed of to the host driver | |
383 | */ | |
97868a2b | 384 | unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq) |
98ccf149 PO |
385 | { |
386 | unsigned int sg_len; | |
2ff1fa67 PO |
387 | size_t buflen; |
388 | struct scatterlist *sg; | |
389 | int i; | |
98ccf149 | 390 | |
97868a2b PF |
391 | if (!mqrq->bounce_buf) |
392 | return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg); | |
98ccf149 | 393 | |
97868a2b | 394 | BUG_ON(!mqrq->bounce_sg); |
98ccf149 | 395 | |
97868a2b | 396 | sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg); |
98ccf149 | 397 | |
97868a2b | 398 | mqrq->bounce_sg_len = sg_len; |
98ccf149 | 399 | |
2ff1fa67 | 400 | buflen = 0; |
97868a2b | 401 | for_each_sg(mqrq->bounce_sg, sg, sg_len, i) |
2ff1fa67 | 402 | buflen += sg->length; |
98ccf149 | 403 | |
97868a2b | 404 | sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen); |
98ccf149 PO |
405 | |
406 | return 1; | |
407 | } | |
408 | ||
2ff1fa67 PO |
409 | /* |
410 | * If writing, bounce the data to the buffer before the request | |
411 | * is sent to the host driver | |
412 | */ | |
97868a2b | 413 | void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq) |
98ccf149 | 414 | { |
97868a2b | 415 | if (!mqrq->bounce_buf) |
98ccf149 PO |
416 | return; |
417 | ||
97868a2b | 418 | if (rq_data_dir(mqrq->req) != WRITE) |
98ccf149 PO |
419 | return; |
420 | ||
97868a2b PF |
421 | sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, |
422 | mqrq->bounce_buf, mqrq->sg[0].length); | |
98ccf149 PO |
423 | } |
424 | ||
2ff1fa67 PO |
425 | /* |
426 | * If reading, bounce the data from the buffer after the request | |
427 | * has been handled by the host driver | |
428 | */ | |
97868a2b | 429 | void mmc_queue_bounce_post(struct mmc_queue_req *mqrq) |
98ccf149 | 430 | { |
97868a2b | 431 | if (!mqrq->bounce_buf) |
98ccf149 PO |
432 | return; |
433 | ||
97868a2b | 434 | if (rq_data_dir(mqrq->req) != READ) |
98ccf149 PO |
435 | return; |
436 | ||
97868a2b PF |
437 | sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, |
438 | mqrq->bounce_buf, mqrq->sg[0].length); | |
98ccf149 | 439 | } |