Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Pierre Ossman | 70f1048 | 2007-07-11 20:04:50 +0200 | [diff] [blame] | 2 | * linux/drivers/mmc/card/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 | */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 12 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/blkdev.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 15 | #include <linux/freezer.h> |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 16 | #include <linux/kthread.h> |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 17 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | |
| 19 | #include <linux/mmc/card.h> |
| 20 | #include <linux/mmc/host.h> |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 21 | #include "queue.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 23 | #define MMC_QUEUE_BOUNCESZ 65536 |
| 24 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 25 | #define MMC_QUEUE_SUSPENDED (1 << 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | /* |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 28 | * Prepare a MMC request. This just filters out odd stuff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | */ |
| 30 | static int mmc_prep_request(struct request_queue *q, struct request *req) |
| 31 | { |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 32 | /* |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 33 | * We only like normal block requests and discards. |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 34 | */ |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 35 | if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | blk_dump_rq_flags(req, "MMC bad request"); |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 37 | return BLKPREP_KILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 40 | req->cmd_flags |= REQ_DONTPREP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 42 | return BLKPREP_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static int mmc_queue_thread(void *d) |
| 46 | { |
| 47 | struct mmc_queue *mq = d; |
| 48 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 50 | current->flags |= PF_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | down(&mq->thread_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | do { |
| 54 | struct request *req = NULL; |
| 55 | |
| 56 | spin_lock_irq(q->queue_lock); |
| 57 | set_current_state(TASK_INTERRUPTIBLE); |
Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 58 | if (!blk_queue_plugged(q)) |
| 59 | req = blk_fetch_request(q); |
Juha [êölä | c723e08a | 2006-08-06 09:58:22 +0100 | [diff] [blame] | 60 | mq->req = req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | spin_unlock_irq(q->queue_lock); |
| 62 | |
| 63 | if (!req) { |
Vitaly Wool | 7b30d28 | 2006-12-07 20:08:02 +0100 | [diff] [blame] | 64 | if (kthread_should_stop()) { |
| 65 | set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | break; |
Vitaly Wool | 7b30d28 | 2006-12-07 20:08:02 +0100 | [diff] [blame] | 67 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | up(&mq->thread_sem); |
| 69 | schedule(); |
| 70 | down(&mq->thread_sem); |
| 71 | continue; |
| 72 | } |
| 73 | set_current_state(TASK_RUNNING); |
| 74 | |
| 75 | mq->issue_fn(mq, req); |
| 76 | } while (1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | up(&mq->thread_sem); |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Generic MMC request handler. This is called for any queue on a |
| 84 | * particular host. When the host is not busy, we look for a request |
| 85 | * on any queue on this host, and attempt to issue it. This may |
| 86 | * not be the queue we were asked to process. |
| 87 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 88 | static void mmc_request(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { |
| 90 | struct mmc_queue *mq = q->queuedata; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 91 | struct request *req; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 92 | |
| 93 | if (!mq) { |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 94 | while ((req = blk_fetch_request(q)) != NULL) { |
| 95 | req->cmd_flags |= REQ_QUIET; |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 96 | __blk_end_request_all(req, -EIO); |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 97 | } |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 98 | return; |
| 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | if (!mq->req) |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 102 | wake_up_process(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * mmc_init_queue - initialise a queue structure. |
| 107 | * @mq: mmc queue |
| 108 | * @card: mmc card to attach this queue |
| 109 | * @lock: queue lock |
| 110 | * |
| 111 | * Initialise a MMC card request queue. |
| 112 | */ |
| 113 | int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock) |
| 114 | { |
| 115 | struct mmc_host *host = card->host; |
| 116 | u64 limit = BLK_BOUNCE_HIGH; |
| 117 | int ret; |
| 118 | |
Greg Kroah-Hartman | fcaf71f | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 119 | if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask) |
| 120 | limit = *mmc_dev(host)->dma_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | |
| 122 | mq->card = card; |
| 123 | mq->queue = blk_init_queue(mmc_request, lock); |
| 124 | if (!mq->queue) |
| 125 | return -ENOMEM; |
| 126 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | mq->queue->queuedata = mq; |
| 128 | mq->req = NULL; |
| 129 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 130 | blk_queue_prep_rq(mq->queue, mmc_prep_request); |
Pierre Ossman | 8dddfe1 | 2008-10-14 20:04:46 +0200 | [diff] [blame] | 131 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue); |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 132 | if (mmc_can_erase(card)) { |
| 133 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mq->queue); |
| 134 | mq->queue->limits.max_discard_sectors = UINT_MAX; |
| 135 | if (card->erased_byte == 0) |
| 136 | mq->queue->limits.discard_zeroes_data = 1; |
| 137 | if (!mmc_can_trim(card) && is_power_of_2(card->erase_size)) { |
| 138 | mq->queue->limits.discard_granularity = |
| 139 | card->erase_size << 9; |
| 140 | mq->queue->limits.discard_alignment = |
| 141 | card->erase_size << 9; |
| 142 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 143 | if (mmc_can_secure_erase_trim(card)) |
| 144 | queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, |
| 145 | mq->queue); |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 146 | } |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 147 | |
| 148 | #ifdef CONFIG_MMC_BLOCK_BOUNCE |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 149 | if (host->max_segs == 1) { |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 150 | unsigned int bouncesz; |
| 151 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 152 | bouncesz = MMC_QUEUE_BOUNCESZ; |
| 153 | |
| 154 | if (bouncesz > host->max_req_size) |
| 155 | bouncesz = host->max_req_size; |
| 156 | if (bouncesz > host->max_seg_size) |
| 157 | bouncesz = host->max_seg_size; |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 158 | if (bouncesz > (host->max_blk_count * 512)) |
| 159 | bouncesz = host->max_blk_count * 512; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 160 | |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 161 | if (bouncesz > 512) { |
| 162 | mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); |
| 163 | if (!mq->bounce_buf) { |
| 164 | printk(KERN_WARNING "%s: unable to " |
| 165 | "allocate bounce buffer\n", |
| 166 | mmc_card_name(card)); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | if (mq->bounce_buf) { |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 171 | blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 172 | blk_queue_max_hw_sectors(mq->queue, bouncesz / 512); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 173 | blk_queue_max_segments(mq->queue, bouncesz / 512); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 174 | blk_queue_max_segment_size(mq->queue, bouncesz); |
| 175 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 176 | mq->sg = kmalloc(sizeof(struct scatterlist), |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 177 | GFP_KERNEL); |
| 178 | if (!mq->sg) { |
| 179 | ret = -ENOMEM; |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 180 | goto cleanup_queue; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 181 | } |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 182 | sg_init_table(mq->sg, 1); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 183 | |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 184 | mq->bounce_sg = kmalloc(sizeof(struct scatterlist) * |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 185 | bouncesz / 512, GFP_KERNEL); |
| 186 | if (!mq->bounce_sg) { |
| 187 | ret = -ENOMEM; |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 188 | goto cleanup_queue; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 189 | } |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 190 | sg_init_table(mq->bounce_sg, bouncesz / 512); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | #endif |
| 194 | |
| 195 | if (!mq->bounce_buf) { |
| 196 | blk_queue_bounce_limit(mq->queue, limit); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 197 | blk_queue_max_hw_sectors(mq->queue, |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 198 | min(host->max_blk_count, host->max_req_size / 512)); |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 199 | blk_queue_max_segments(mq->queue, host->max_segs); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 200 | blk_queue_max_segment_size(mq->queue, host->max_seg_size); |
| 201 | |
Haavard Skinnemoen | 05e5b13 | 2007-11-23 10:19:00 +0100 | [diff] [blame] | 202 | mq->sg = kmalloc(sizeof(struct scatterlist) * |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 203 | host->max_segs, GFP_KERNEL); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 204 | if (!mq->sg) { |
| 205 | ret = -ENOMEM; |
| 206 | goto cleanup_queue; |
| 207 | } |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 208 | sg_init_table(mq->sg, host->max_segs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Thomas Gleixner | 632cf92 | 2010-09-14 07:12:35 -0400 | [diff] [blame] | 211 | sema_init(&mq->thread_sem, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
Ethan Du | de528fa | 2010-09-30 18:40:27 -0400 | [diff] [blame] | 213 | mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d", |
| 214 | host->index); |
| 215 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 216 | if (IS_ERR(mq->thread)) { |
| 217 | ret = PTR_ERR(mq->thread); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 218 | goto free_bounce_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 221 | return 0; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 222 | free_bounce_sg: |
| 223 | if (mq->bounce_sg) |
| 224 | kfree(mq->bounce_sg); |
| 225 | mq->bounce_sg = NULL; |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 226 | cleanup_queue: |
| 227 | if (mq->sg) |
| 228 | kfree(mq->sg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | mq->sg = NULL; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 230 | if (mq->bounce_buf) |
| 231 | kfree(mq->bounce_buf); |
| 232 | mq->bounce_buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | blk_cleanup_queue(mq->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | return ret; |
| 235 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
| 237 | void mmc_cleanup_queue(struct mmc_queue *mq) |
| 238 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 239 | struct request_queue *q = mq->queue; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 240 | unsigned long flags; |
| 241 | |
Pierre Ossman | d2b46f6 | 2007-04-28 16:52:12 +0200 | [diff] [blame] | 242 | /* Make sure the queue isn't suspended, as that will deadlock */ |
| 243 | mmc_queue_resume(mq); |
| 244 | |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 245 | /* Then terminate our worker thread */ |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 246 | kthread_stop(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 248 | /* Empty the queue */ |
| 249 | spin_lock_irqsave(q->queue_lock, flags); |
| 250 | q->queuedata = NULL; |
| 251 | blk_start_queue(q); |
| 252 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 253 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 254 | if (mq->bounce_sg) |
| 255 | kfree(mq->bounce_sg); |
| 256 | mq->bounce_sg = NULL; |
| 257 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | kfree(mq->sg); |
| 259 | mq->sg = NULL; |
| 260 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 261 | if (mq->bounce_buf) |
| 262 | kfree(mq->bounce_buf); |
| 263 | mq->bounce_buf = NULL; |
| 264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | mq->card = NULL; |
| 266 | } |
| 267 | EXPORT_SYMBOL(mmc_cleanup_queue); |
| 268 | |
| 269 | /** |
| 270 | * mmc_queue_suspend - suspend a MMC request queue |
| 271 | * @mq: MMC queue to suspend |
| 272 | * |
| 273 | * Stop the block request queue, and wait for our thread to |
| 274 | * complete any outstanding requests. This ensures that we |
| 275 | * won't suspend while a request is being processed. |
| 276 | */ |
| 277 | void mmc_queue_suspend(struct mmc_queue *mq) |
| 278 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 279 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | unsigned long flags; |
| 281 | |
| 282 | if (!(mq->flags & MMC_QUEUE_SUSPENDED)) { |
| 283 | mq->flags |= MMC_QUEUE_SUSPENDED; |
| 284 | |
| 285 | spin_lock_irqsave(q->queue_lock, flags); |
| 286 | blk_stop_queue(q); |
| 287 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 288 | |
| 289 | down(&mq->thread_sem); |
| 290 | } |
| 291 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
| 293 | /** |
| 294 | * mmc_queue_resume - resume a previously suspended MMC request queue |
| 295 | * @mq: MMC queue to resume |
| 296 | */ |
| 297 | void mmc_queue_resume(struct mmc_queue *mq) |
| 298 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 299 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | unsigned long flags; |
| 301 | |
| 302 | if (mq->flags & MMC_QUEUE_SUSPENDED) { |
| 303 | mq->flags &= ~MMC_QUEUE_SUSPENDED; |
| 304 | |
| 305 | up(&mq->thread_sem); |
| 306 | |
| 307 | spin_lock_irqsave(q->queue_lock, flags); |
| 308 | blk_start_queue(q); |
| 309 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 310 | } |
| 311 | } |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 312 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 313 | /* |
| 314 | * Prepare the sg list(s) to be handed of to the host driver |
| 315 | */ |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 316 | unsigned int mmc_queue_map_sg(struct mmc_queue *mq) |
| 317 | { |
| 318 | unsigned int sg_len; |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 319 | size_t buflen; |
| 320 | struct scatterlist *sg; |
| 321 | int i; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 322 | |
| 323 | if (!mq->bounce_buf) |
| 324 | return blk_rq_map_sg(mq->queue, mq->req, mq->sg); |
| 325 | |
| 326 | BUG_ON(!mq->bounce_sg); |
| 327 | |
| 328 | sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg); |
| 329 | |
| 330 | mq->bounce_sg_len = sg_len; |
| 331 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 332 | buflen = 0; |
| 333 | for_each_sg(mq->bounce_sg, sg, sg_len, i) |
| 334 | buflen += sg->length; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 335 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 336 | sg_init_one(mq->sg, mq->bounce_buf, buflen); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 337 | |
| 338 | return 1; |
| 339 | } |
| 340 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 341 | /* |
| 342 | * If writing, bounce the data to the buffer before the request |
| 343 | * is sent to the host driver |
| 344 | */ |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 345 | void mmc_queue_bounce_pre(struct mmc_queue *mq) |
| 346 | { |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 347 | unsigned long flags; |
| 348 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 349 | if (!mq->bounce_buf) |
| 350 | return; |
| 351 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 352 | if (rq_data_dir(mq->req) != WRITE) |
| 353 | return; |
| 354 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 355 | local_irq_save(flags); |
| 356 | sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len, |
| 357 | mq->bounce_buf, mq->sg[0].length); |
| 358 | local_irq_restore(flags); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 361 | /* |
| 362 | * If reading, bounce the data from the buffer after the request |
| 363 | * has been handled by the host driver |
| 364 | */ |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 365 | void mmc_queue_bounce_post(struct mmc_queue *mq) |
| 366 | { |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 367 | unsigned long flags; |
| 368 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 369 | if (!mq->bounce_buf) |
| 370 | return; |
| 371 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 372 | if (rq_data_dir(mq->req) != READ) |
| 373 | return; |
| 374 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 375 | local_irq_save(flags); |
| 376 | sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len, |
| 377 | mq->bounce_buf, mq->sg[0].length); |
| 378 | local_irq_restore(flags); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 379 | } |
| 380 | |