Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 2 | * linux/drivers/mmc/queue.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003 Russell King, All Rights Reserved. |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 5 | * Copyright 2006-2007 Pierre Ossman |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/module.h> |
| 13 | #include <linux/blkdev.h> |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 14 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
| 16 | #include <linux/mmc/card.h> |
| 17 | #include <linux/mmc/host.h> |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 18 | #include "queue.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 20 | #define MMC_QUEUE_SUSPENDED (1 << 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | /* |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 23 | * Prepare a MMC request. This just filters out odd stuff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | static int mmc_prep_request(struct request_queue *q, struct request *req) |
| 26 | { |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 27 | /* |
| 28 | * We only like normal block requests. |
| 29 | */ |
| 30 | if (!blk_fs_request(req) && !blk_pc_request(req)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | blk_dump_rq_flags(req, "MMC bad request"); |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 32 | return BLKPREP_KILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 35 | req->cmd_flags |= REQ_DONTPREP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 37 | return BLKPREP_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static int mmc_queue_thread(void *d) |
| 41 | { |
| 42 | struct mmc_queue *mq = d; |
| 43 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | down(&mq->thread_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | 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ä | c723e08a | 2006-08-06 09:58:22 +0100 | [diff] [blame] | 58 | req = elv_next_request(q); |
| 59 | mq->req = req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | spin_unlock_irq(q->queue_lock); |
| 61 | |
| 62 | if (!req) { |
Vitaly Wool | 7b30d28 | 2006-12-07 20:08:02 +0100 | [diff] [blame] | 63 | if (kthread_should_stop()) { |
| 64 | set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | break; |
Vitaly Wool | 7b30d28 | 2006-12-07 20:08:02 +0100 | [diff] [blame] | 66 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | up(&mq->thread_sem); |
| 77 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | 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 | */ |
| 87 | static void mmc_request(request_queue_t *q) |
| 88 | { |
| 89 | struct mmc_queue *mq = q->queuedata; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 90 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
| 104 | if (!mq->req) |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 105 | wake_up_process(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | } |
| 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 | */ |
| 116 | int 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-Hartman | fcaf71f | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 122 | if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask) |
| 123 | limit = *mmc_dev(host)->dma_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
| 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 Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 132 | blk_queue_max_sectors(mq->queue, host->max_req_size / 512); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 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 Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 144 | goto cleanup_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | init_MUTEX(&mq->thread_sem); |
| 148 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 155 | return 0; |
| 156 | |
| 157 | free_sg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | kfree(mq->sg); |
| 159 | mq->sg = NULL; |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 160 | cleanup_queue: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | blk_cleanup_queue(mq->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return ret; |
| 163 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
| 165 | void mmc_cleanup_queue(struct mmc_queue *mq) |
| 166 | { |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 167 | 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 Ossman | d2b46f6 | 2007-04-28 16:52:12 +0200 | [diff] [blame] | 175 | /* Make sure the queue isn't suspended, as that will deadlock */ |
| 176 | mmc_queue_resume(mq); |
| 177 | |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 178 | /* Then terminate our worker thread */ |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 179 | kthread_stop(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
| 181 | kfree(mq->sg); |
| 182 | mq->sg = NULL; |
| 183 | |
| 184 | blk_cleanup_queue(mq->queue); |
| 185 | |
| 186 | mq->card = NULL; |
| 187 | } |
| 188 | EXPORT_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 | */ |
| 198 | void 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * mmc_queue_resume - resume a previously suspended MMC request queue |
| 216 | * @mq: MMC queue to resume |
| 217 | */ |
| 218 | void 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 Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 233 | |