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