blob: dd97bc798409eb0bdec4d5a413c217c173a642e4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman98ac2162006-12-23 20:03:02 +01002 * linux/drivers/mmc/queue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
Pierre Ossman98ac2162006-12-23 20:03:02 +01005 * Copyright 2006-2007 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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/module.h>
13#include <linux/blkdev.h>
Christoph Hellwig87598a22006-11-13 20:23:52 +010014#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include <linux/mmc/card.h>
17#include <linux/mmc/host.h>
Pierre Ossman98ac2162006-12-23 20:03:02 +010018#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Christoph Hellwig87598a22006-11-13 20:23:52 +010020#define MMC_QUEUE_SUSPENDED (1 << 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020023 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25static int mmc_prep_request(struct request_queue *q, struct request *req)
26{
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020027 /*
28 * We only like normal block requests.
29 */
30 if (!blk_fs_request(req) && !blk_pc_request(req)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020032 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 }
34
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020035 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020037 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
40static int mmc_queue_thread(void *d)
41{
42 struct mmc_queue *mq = d;
43 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45 /*
46 * Set iothread to ensure that we aren't put to sleep by
47 * the process freezing. We handle suspension ourselves.
48 */
49 current->flags |= PF_MEMALLOC|PF_NOFREEZE;
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 do {
53 struct request *req = NULL;
54
55 spin_lock_irq(q->queue_lock);
56 set_current_state(TASK_INTERRUPTIBLE);
57 if (!blk_queue_plugged(q))
Juha [êöläc723e08a2006-08-06 09:58:22 +010058 req = elv_next_request(q);
59 mq->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 spin_unlock_irq(q->queue_lock);
61
62 if (!req) {
Vitaly Wool7b30d282006-12-07 20:08:02 +010063 if (kthread_should_stop()) {
64 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 up(&mq->thread_sem);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 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 */
87static void mmc_request(request_queue_t *q)
88{
89 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +010090 struct request *req;
91 int ret;
92
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 = end_that_request_chunk(req, 0,
98 req->current_nr_sectors << 9);
99 } while (ret);
100 }
101 return;
102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (!mq->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100105 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108/**
109 * mmc_init_queue - initialise a queue structure.
110 * @mq: mmc queue
111 * @card: mmc card to attach this queue
112 * @lock: queue lock
113 *
114 * Initialise a MMC card request queue.
115 */
116int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
117{
118 struct mmc_host *host = card->host;
119 u64 limit = BLK_BOUNCE_HIGH;
120 int ret;
121
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200122 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
123 limit = *mmc_dev(host)->dma_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 mq->card = card;
126 mq->queue = blk_init_queue(mmc_request, lock);
127 if (!mq->queue)
128 return -ENOMEM;
129
130 blk_queue_prep_rq(mq->queue, mmc_prep_request);
131 blk_queue_bounce_limit(mq->queue, limit);
Pierre Ossman55db8902006-11-21 17:55:45 +0100132 blk_queue_max_sectors(mq->queue, host->max_req_size / 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 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);
136
137 mq->queue->queuedata = mq;
138 mq->req = NULL;
139
140 mq->sg = kmalloc(sizeof(struct scatterlist) * host->max_phys_segs,
141 GFP_KERNEL);
142 if (!mq->sg) {
143 ret = -ENOMEM;
Christoph Hellwig87598a22006-11-13 20:23:52 +0100144 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 init_MUTEX(&mq->thread_sem);
148
Christoph Hellwig87598a22006-11-13 20:23:52 +0100149 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154
Christoph Hellwig87598a22006-11-13 20:23:52 +0100155 return 0;
156
157 free_sg:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 kfree(mq->sg);
159 mq->sg = NULL;
Christoph Hellwig87598a22006-11-13 20:23:52 +0100160 cleanup_queue:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 return ret;
163}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165void mmc_cleanup_queue(struct mmc_queue *mq)
166{
Pierre Ossman89b4e132006-11-14 22:08:16 +0100167 request_queue_t *q = mq->queue;
168 unsigned long flags;
169
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);
174
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200175 /* Make sure the queue isn't suspended, as that will deadlock */
176 mmc_queue_resume(mq);
177
Pierre Ossman89b4e132006-11-14 22:08:16 +0100178 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100179 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 kfree(mq->sg);
182 mq->sg = NULL;
183
184 blk_cleanup_queue(mq->queue);
185
186 mq->card = NULL;
187}
188EXPORT_SYMBOL(mmc_cleanup_queue);
189
190/**
191 * mmc_queue_suspend - suspend a MMC request queue
192 * @mq: MMC queue to suspend
193 *
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.
197 */
198void mmc_queue_suspend(struct mmc_queue *mq)
199{
200 request_queue_t *q = mq->queue;
201 unsigned long flags;
202
203 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
204 mq->flags |= MMC_QUEUE_SUSPENDED;
205
206 spin_lock_irqsave(q->queue_lock, flags);
207 blk_stop_queue(q);
208 spin_unlock_irqrestore(q->queue_lock, flags);
209
210 down(&mq->thread_sem);
211 }
212}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214/**
215 * mmc_queue_resume - resume a previously suspended MMC request queue
216 * @mq: MMC queue to resume
217 */
218void mmc_queue_resume(struct mmc_queue *mq)
219{
220 request_queue_t *q = mq->queue;
221 unsigned long flags;
222
223 if (mq->flags & MMC_QUEUE_SUSPENDED) {
224 mq->flags &= ~MMC_QUEUE_SUSPENDED;
225
226 up(&mq->thread_sem);
227
228 spin_lock_irqsave(q->queue_lock, flags);
229 blk_start_queue(q);
230 spin_unlock_irqrestore(q->queue_lock, flags);
231 }
232}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100233