]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mmc/mmc_queue.c
mmc: remove kernel_thread()
[karo-tx-linux.git] / drivers / mmc / mmc_queue.c
1 /*
2  *  linux/drivers/mmc/mmc_queue.c
3  *
4  *  Copyright (C) 2003 Russell King, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  */
11 #include <linux/module.h>
12 #include <linux/blkdev.h>
13 #include <linux/kthread.h>
14
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/host.h>
17 #include "mmc_queue.h"
18
19 #define MMC_QUEUE_SUSPENDED     (1 << 0)
20
21 /*
22  * Prepare a MMC request.  Essentially, this means passing the
23  * preparation off to the media driver.  The media driver will
24  * create a mmc_io_request in req->special.
25  */
26 static int mmc_prep_request(struct request_queue *q, struct request *req)
27 {
28         struct mmc_queue *mq = q->queuedata;
29         int ret = BLKPREP_KILL;
30
31         if (blk_special_request(req)) {
32                 /*
33                  * Special commands already have the command
34                  * blocks already setup in req->special.
35                  */
36                 BUG_ON(!req->special);
37
38                 ret = BLKPREP_OK;
39         } else if (blk_fs_request(req) || blk_pc_request(req)) {
40                 /*
41                  * Block I/O requests need translating according
42                  * to the protocol.
43                  */
44                 ret = mq->prep_fn(mq, req);
45         } else {
46                 /*
47                  * Everything else is invalid.
48                  */
49                 blk_dump_rq_flags(req, "MMC bad request");
50         }
51
52         if (ret == BLKPREP_OK)
53                 req->cmd_flags |= REQ_DONTPREP;
54
55         return ret;
56 }
57
58 static int mmc_queue_thread(void *d)
59 {
60         struct mmc_queue *mq = d;
61         struct request_queue *q = mq->queue;
62
63         /*
64          * Set iothread to ensure that we aren't put to sleep by
65          * the process freezing.  We handle suspension ourselves.
66          */
67         current->flags |= PF_MEMALLOC|PF_NOFREEZE;
68
69         down(&mq->thread_sem);
70         do {
71                 struct request *req = NULL;
72
73                 spin_lock_irq(q->queue_lock);
74                 set_current_state(TASK_INTERRUPTIBLE);
75                 if (!blk_queue_plugged(q))
76                         req = elv_next_request(q);
77                 mq->req = req;
78                 spin_unlock_irq(q->queue_lock);
79
80                 if (!req) {
81                         if (kthread_should_stop())
82                                 break;
83                         up(&mq->thread_sem);
84                         schedule();
85                         down(&mq->thread_sem);
86                         continue;
87                 }
88                 set_current_state(TASK_RUNNING);
89
90                 mq->issue_fn(mq, req);
91         } while (1);
92         up(&mq->thread_sem);
93
94         return 0;
95 }
96
97 /*
98  * Generic MMC request handler.  This is called for any queue on a
99  * particular host.  When the host is not busy, we look for a request
100  * on any queue on this host, and attempt to issue it.  This may
101  * not be the queue we were asked to process.
102  */
103 static void mmc_request(request_queue_t *q)
104 {
105         struct mmc_queue *mq = q->queuedata;
106
107         if (!mq->req)
108                 wake_up_process(mq->thread);
109 }
110
111 /**
112  * mmc_init_queue - initialise a queue structure.
113  * @mq: mmc queue
114  * @card: mmc card to attach this queue
115  * @lock: queue lock
116  *
117  * Initialise a MMC card request queue.
118  */
119 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
120 {
121         struct mmc_host *host = card->host;
122         u64 limit = BLK_BOUNCE_HIGH;
123         int ret;
124
125         if (host->dev->dma_mask && *host->dev->dma_mask)
126                 limit = *host->dev->dma_mask;
127
128         mq->card = card;
129         mq->queue = blk_init_queue(mmc_request, lock);
130         if (!mq->queue)
131                 return -ENOMEM;
132
133         blk_queue_prep_rq(mq->queue, mmc_prep_request);
134         blk_queue_bounce_limit(mq->queue, limit);
135         blk_queue_max_sectors(mq->queue, host->max_sectors);
136         blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
137         blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
138         blk_queue_max_segment_size(mq->queue, host->max_seg_size);
139
140         mq->queue->queuedata = mq;
141         mq->req = NULL;
142
143         mq->sg = kmalloc(sizeof(struct scatterlist) * host->max_phys_segs,
144                          GFP_KERNEL);
145         if (!mq->sg) {
146                 ret = -ENOMEM;
147                 goto cleanup_queue;
148         }
149
150         init_MUTEX(&mq->thread_sem);
151
152         mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
153         if (IS_ERR(mq->thread)) {
154                 ret = PTR_ERR(mq->thread);
155                 goto free_sg;
156         }
157
158         return 0;
159
160  free_sg:
161         kfree(mq->sg);
162         mq->sg = NULL;
163  cleanup_queue:
164         blk_cleanup_queue(mq->queue);
165         return ret;
166 }
167 EXPORT_SYMBOL(mmc_init_queue);
168
169 void mmc_cleanup_queue(struct mmc_queue *mq)
170 {
171         kthread_stop(mq->thread);
172
173         kfree(mq->sg);
174         mq->sg = NULL;
175
176         blk_cleanup_queue(mq->queue);
177
178         mq->card = NULL;
179 }
180 EXPORT_SYMBOL(mmc_cleanup_queue);
181
182 /**
183  * mmc_queue_suspend - suspend a MMC request queue
184  * @mq: MMC queue to suspend
185  *
186  * Stop the block request queue, and wait for our thread to
187  * complete any outstanding requests.  This ensures that we
188  * won't suspend while a request is being processed.
189  */
190 void mmc_queue_suspend(struct mmc_queue *mq)
191 {
192         request_queue_t *q = mq->queue;
193         unsigned long flags;
194
195         if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
196                 mq->flags |= MMC_QUEUE_SUSPENDED;
197
198                 spin_lock_irqsave(q->queue_lock, flags);
199                 blk_stop_queue(q);
200                 spin_unlock_irqrestore(q->queue_lock, flags);
201
202                 down(&mq->thread_sem);
203         }
204 }
205 EXPORT_SYMBOL(mmc_queue_suspend);
206
207 /**
208  * mmc_queue_resume - resume a previously suspended MMC request queue
209  * @mq: MMC queue to resume
210  */
211 void mmc_queue_resume(struct mmc_queue *mq)
212 {
213         request_queue_t *q = mq->queue;
214         unsigned long flags;
215
216         if (mq->flags & MMC_QUEUE_SUSPENDED) {
217                 mq->flags &= ~MMC_QUEUE_SUSPENDED;
218
219                 up(&mq->thread_sem);
220
221                 spin_lock_irqsave(q->queue_lock, flags);
222                 blk_start_queue(q);
223                 spin_unlock_irqrestore(q->queue_lock, flags);
224         }
225 }
226 EXPORT_SYMBOL(mmc_queue_resume);