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