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