blob: a17423a4ed8fb9cbcc4b41e3ea0fcd7a4a7cb744 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Christoph Hellwig87598a22006-11-13 20:23:52 +010013#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include <linux/mmc/card.h>
16#include <linux/mmc/host.h>
17#include "mmc_queue.h"
18
Christoph Hellwig87598a22006-11-13 20:23:52 +010019#define MMC_QUEUE_SUSPENDED (1 << 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
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 */
26static 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
Jens Axboe4aff5e22006-08-10 08:44:47 +020031 if (blk_special_request(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 /*
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;
Jens Axboe4aff5e22006-08-10 08:44:47 +020039 } else if (blk_fs_request(req) || blk_pc_request(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 /*
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)
Jens Axboe4aff5e22006-08-10 08:44:47 +020053 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 return ret;
56}
57
58static int mmc_queue_thread(void *d)
59{
60 struct mmc_queue *mq = d;
61 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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))
Juha [êöläc723e08a2006-08-06 09:58:22 +010076 req = elv_next_request(q);
77 mq->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 spin_unlock_irq(q->queue_lock);
79
80 if (!req) {
Christoph Hellwig87598a22006-11-13 20:23:52 +010081 if (kthread_should_stop())
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 up(&mq->thread_sem);
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 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 */
103static void mmc_request(request_queue_t *q)
104{
105 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100106 struct request *req;
107 int ret;
108
109 if (!mq) {
110 printk(KERN_ERR "MMC: killing requests for dead queue\n");
111 while ((req = elv_next_request(q)) != NULL) {
112 do {
113 ret = end_that_request_chunk(req, 0,
114 req->current_nr_sectors << 9);
115 } while (ret);
116 }
117 return;
118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (!mq->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100121 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124/**
125 * mmc_init_queue - initialise a queue structure.
126 * @mq: mmc queue
127 * @card: mmc card to attach this queue
128 * @lock: queue lock
129 *
130 * Initialise a MMC card request queue.
131 */
132int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
133{
134 struct mmc_host *host = card->host;
135 u64 limit = BLK_BOUNCE_HIGH;
136 int ret;
137
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200138 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
139 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 mq->card = card;
142 mq->queue = blk_init_queue(mmc_request, lock);
143 if (!mq->queue)
144 return -ENOMEM;
145
146 blk_queue_prep_rq(mq->queue, mmc_prep_request);
147 blk_queue_bounce_limit(mq->queue, limit);
148 blk_queue_max_sectors(mq->queue, host->max_sectors);
149 blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
150 blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
151 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
152
153 mq->queue->queuedata = mq;
154 mq->req = NULL;
155
156 mq->sg = kmalloc(sizeof(struct scatterlist) * host->max_phys_segs,
157 GFP_KERNEL);
158 if (!mq->sg) {
159 ret = -ENOMEM;
Christoph Hellwig87598a22006-11-13 20:23:52 +0100160 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 init_MUTEX(&mq->thread_sem);
164
Christoph Hellwig87598a22006-11-13 20:23:52 +0100165 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
166 if (IS_ERR(mq->thread)) {
167 ret = PTR_ERR(mq->thread);
168 goto free_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Christoph Hellwig87598a22006-11-13 20:23:52 +0100171 return 0;
172
173 free_sg:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 kfree(mq->sg);
175 mq->sg = NULL;
Christoph Hellwig87598a22006-11-13 20:23:52 +0100176 cleanup_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return ret;
179}
180EXPORT_SYMBOL(mmc_init_queue);
181
182void mmc_cleanup_queue(struct mmc_queue *mq)
183{
Pierre Ossman89b4e132006-11-14 22:08:16 +0100184 request_queue_t *q = mq->queue;
185 unsigned long flags;
186
187 /* Mark that we should start throwing out stragglers */
188 spin_lock_irqsave(q->queue_lock, flags);
189 q->queuedata = NULL;
190 spin_unlock_irqrestore(q->queue_lock, flags);
191
192 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100193 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
195 kfree(mq->sg);
196 mq->sg = NULL;
197
198 blk_cleanup_queue(mq->queue);
199
200 mq->card = NULL;
201}
202EXPORT_SYMBOL(mmc_cleanup_queue);
203
204/**
205 * mmc_queue_suspend - suspend a MMC request queue
206 * @mq: MMC queue to suspend
207 *
208 * Stop the block request queue, and wait for our thread to
209 * complete any outstanding requests. This ensures that we
210 * won't suspend while a request is being processed.
211 */
212void mmc_queue_suspend(struct mmc_queue *mq)
213{
214 request_queue_t *q = mq->queue;
215 unsigned long flags;
216
217 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
218 mq->flags |= MMC_QUEUE_SUSPENDED;
219
220 spin_lock_irqsave(q->queue_lock, flags);
221 blk_stop_queue(q);
222 spin_unlock_irqrestore(q->queue_lock, flags);
223
224 down(&mq->thread_sem);
225 }
226}
227EXPORT_SYMBOL(mmc_queue_suspend);
228
229/**
230 * mmc_queue_resume - resume a previously suspended MMC request queue
231 * @mq: MMC queue to resume
232 */
233void mmc_queue_resume(struct mmc_queue *mq)
234{
235 request_queue_t *q = mq->queue;
236 unsigned long flags;
237
238 if (mq->flags & MMC_QUEUE_SUSPENDED) {
239 mq->flags &= ~MMC_QUEUE_SUSPENDED;
240
241 up(&mq->thread_sem);
242
243 spin_lock_irqsave(q->queue_lock, flags);
244 blk_start_queue(q);
245 spin_unlock_irqrestore(q->queue_lock, flags);
246 }
247}
248EXPORT_SYMBOL(mmc_queue_resume);