RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / mmc / card / queue.c
blobd8d98bd3a4f7bcc765ef4cbabc58857af673f7f7
1 /*
2 * linux/drivers/mmc/queue.c
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
5 * Copyright 2006-2007 Pierre Ossman
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.
12 #include <linux/module.h>
13 #include <linux/blkdev.h>
14 #include <linux/kthread.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/host.h>
18 #include "queue.h"
20 #define MMC_QUEUE_SUSPENDED (1 << 0)
23 * Prepare a MMC request. This just filters out odd stuff.
25 static int mmc_prep_request(struct request_queue *q, struct request *req)
28 * We only like normal block requests.
30 if (!blk_fs_request(req) && !blk_pc_request(req)) {
31 blk_dump_rq_flags(req, "MMC bad request");
32 return BLKPREP_KILL;
35 req->cmd_flags |= REQ_DONTPREP;
37 return BLKPREP_OK;
40 static int mmc_queue_thread(void *d)
42 struct mmc_queue *mq = d;
43 struct request_queue *q = mq->queue;
46 * Set iothread to ensure that we aren't put to sleep by
47 * the process freezing. We handle suspension ourselves.
49 current->flags |= PF_MEMALLOC|PF_NOFREEZE;
51 down(&mq->thread_sem);
52 do {
53 struct request *req = NULL;
55 spin_lock_irq(q->queue_lock);
56 set_current_state(TASK_INTERRUPTIBLE);
57 if (!blk_queue_plugged(q))
58 req = elv_next_request(q);
59 mq->req = req;
60 spin_unlock_irq(q->queue_lock);
62 if (!req) {
63 if (kthread_should_stop()) {
64 set_current_state(TASK_RUNNING);
65 break;
67 up(&mq->thread_sem);
68 schedule();
69 down(&mq->thread_sem);
70 continue;
72 set_current_state(TASK_RUNNING);
74 mq->issue_fn(mq, req);
75 } while (1);
76 up(&mq->thread_sem);
78 return 0;
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.
87 static void mmc_request(request_queue_t *q)
89 struct mmc_queue *mq = q->queuedata;
90 struct request *req;
91 int ret;
93 if (!mq) {
94 printk(KERN_ERR "MMC: killing requests for dead queue\n");
95 while ((req = elv_next_request(q)) != NULL) {
96 do {
97 ret = __blk_end_request(req, -EIO,
98 blk_rq_cur_bytes(req));
99 } while (ret);
101 return;
104 if (!mq->req)
105 wake_up_process(mq->thread);
109 * mmc_init_queue - initialise a queue structure.
110 * @mq: mmc queue
111 * @card: mmc card to attach this queue
112 * @lock: queue lock
114 * Initialise a MMC card request queue.
116 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
118 struct mmc_host *host = card->host;
119 u64 limit = BLK_BOUNCE_HIGH;
120 int ret;
122 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
123 limit = *mmc_dev(host)->dma_mask;
125 mq->card = card;
126 mq->queue = blk_init_queue(mmc_request, lock);
127 if (!mq->queue)
128 return -ENOMEM;
130 blk_queue_prep_rq(mq->queue, mmc_prep_request);
131 blk_queue_bounce_limit(mq->queue, limit);
132 blk_queue_max_sectors(mq->queue, host->max_req_size / 512);
133 blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
134 blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
135 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
137 mq->queue->queuedata = mq;
138 mq->req = NULL;
140 mq->sg = kmalloc(sizeof(struct scatterlist) * host->max_phys_segs,
141 GFP_KERNEL);
142 if (!mq->sg) {
143 ret = -ENOMEM;
144 goto cleanup_queue;
147 init_MUTEX(&mq->thread_sem);
149 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
150 if (IS_ERR(mq->thread)) {
151 ret = PTR_ERR(mq->thread);
152 goto free_sg;
155 return 0;
157 free_sg:
158 kfree(mq->sg);
159 mq->sg = NULL;
160 cleanup_queue:
161 blk_cleanup_queue(mq->queue);
162 return ret;
165 void mmc_cleanup_queue(struct mmc_queue *mq)
167 request_queue_t *q = mq->queue;
168 unsigned long flags;
170 /* Mark that we should start throwing out stragglers */
171 spin_lock_irqsave(q->queue_lock, flags);
172 q->queuedata = NULL;
173 spin_unlock_irqrestore(q->queue_lock, flags);
175 /* Make sure the queue isn't suspended, as that will deadlock */
176 mmc_queue_resume(mq);
178 /* Then terminate our worker thread */
179 kthread_stop(mq->thread);
181 kfree(mq->sg);
182 mq->sg = NULL;
184 blk_cleanup_queue(mq->queue);
186 mq->card = NULL;
188 EXPORT_SYMBOL(mmc_cleanup_queue);
191 * mmc_queue_suspend - suspend a MMC request queue
192 * @mq: MMC queue to suspend
194 * Stop the block request queue, and wait for our thread to
195 * complete any outstanding requests. This ensures that we
196 * won't suspend while a request is being processed.
198 void mmc_queue_suspend(struct mmc_queue *mq)
200 request_queue_t *q = mq->queue;
201 unsigned long flags;
203 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
204 mq->flags |= MMC_QUEUE_SUSPENDED;
206 spin_lock_irqsave(q->queue_lock, flags);
207 blk_stop_queue(q);
208 spin_unlock_irqrestore(q->queue_lock, flags);
210 down(&mq->thread_sem);
215 * mmc_queue_resume - resume a previously suspended MMC request queue
216 * @mq: MMC queue to resume
218 void mmc_queue_resume(struct mmc_queue *mq)
220 request_queue_t *q = mq->queue;
221 unsigned long flags;
223 if (mq->flags & MMC_QUEUE_SUSPENDED) {
224 mq->flags &= ~MMC_QUEUE_SUSPENDED;
226 up(&mq->thread_sem);
228 spin_lock_irqsave(q->queue_lock, flags);
229 blk_start_queue(q);
230 spin_unlock_irqrestore(q->queue_lock, flags);