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