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