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); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 58 | req = blk_fetch_request(q); |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 59 | mq->mqrq_cur->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 | */ |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 87 | static void mmc_request(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { |
| 89 | struct mmc_queue *mq = q->queuedata; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 90 | struct request *req; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 91 | |
| 92 | if (!mq) { |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 93 | while ((req = blk_fetch_request(q)) != NULL) { |
| 94 | req->cmd_flags |= REQ_QUIET; |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 95 | __blk_end_request_all(req, -EIO); |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 96 | } |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 97 | return; |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 100 | if (!mq->mqrq_cur->req) |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 101 | wake_up_process(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 104 | struct scatterlist *mmc_alloc_sg(int sg_len, int *err) |
| 105 | { |
| 106 | struct scatterlist *sg; |
| 107 | |
| 108 | sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL); |
| 109 | if (!sg) |
| 110 | *err = -ENOMEM; |
| 111 | else { |
| 112 | *err = 0; |
| 113 | sg_init_table(sg, sg_len); |
| 114 | } |
| 115 | |
| 116 | return sg; |
| 117 | } |
| 118 | |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 119 | static void mmc_queue_setup_discard(struct request_queue *q, |
| 120 | struct mmc_card *card) |
| 121 | { |
| 122 | unsigned max_discard; |
| 123 | |
| 124 | max_discard = mmc_calc_max_discard(card); |
| 125 | if (!max_discard) |
| 126 | return; |
| 127 | |
| 128 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 129 | q->limits.max_discard_sectors = max_discard; |
| 130 | if (card->erased_byte == 0) |
| 131 | q->limits.discard_zeroes_data = 1; |
| 132 | q->limits.discard_granularity = card->pref_erase << 9; |
| 133 | /* granularity must not be greater than max. discard */ |
| 134 | if (card->pref_erase > max_discard) |
| 135 | q->limits.discard_granularity = 0; |
| 136 | if (mmc_can_secure_erase_trim(card)) |
| 137 | queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q); |
| 138 | } |
| 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /** |
| 141 | * mmc_init_queue - initialise a queue structure. |
| 142 | * @mq: mmc queue |
| 143 | * @card: mmc card to attach this queue |
| 144 | * @lock: queue lock |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 145 | * @subname: partition subname |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | * |
| 147 | * Initialise a MMC card request queue. |
| 148 | */ |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 149 | int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, |
| 150 | spinlock_t *lock, const char *subname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | { |
| 152 | struct mmc_host *host = card->host; |
| 153 | u64 limit = BLK_BOUNCE_HIGH; |
| 154 | int ret; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 155 | struct mmc_queue_req *mqrq_cur = &mq->mqrq[0]; |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 156 | struct mmc_queue_req *mqrq_prev = &mq->mqrq[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | |
Greg Kroah-Hartman | fcaf71f | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 158 | if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask) |
| 159 | limit = *mmc_dev(host)->dma_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | mq->card = card; |
| 162 | mq->queue = blk_init_queue(mmc_request, lock); |
| 163 | if (!mq->queue) |
| 164 | return -ENOMEM; |
| 165 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 166 | memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur)); |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 167 | memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev)); |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 168 | mq->mqrq_cur = mqrq_cur; |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 169 | mq->mqrq_prev = mqrq_prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | mq->queue->queuedata = mq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 172 | blk_queue_prep_rq(mq->queue, mmc_prep_request); |
Pierre Ossman | 8dddfe1 | 2008-10-14 20:04:46 +0200 | [diff] [blame] | 173 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue); |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 174 | if (mmc_can_erase(card)) |
| 175 | mmc_queue_setup_discard(mq->queue, card); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 176 | |
| 177 | #ifdef CONFIG_MMC_BLOCK_BOUNCE |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 178 | if (host->max_segs == 1) { |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 179 | unsigned int bouncesz; |
| 180 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 181 | bouncesz = MMC_QUEUE_BOUNCESZ; |
| 182 | |
| 183 | if (bouncesz > host->max_req_size) |
| 184 | bouncesz = host->max_req_size; |
| 185 | if (bouncesz > host->max_seg_size) |
| 186 | bouncesz = host->max_seg_size; |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 187 | if (bouncesz > (host->max_blk_count * 512)) |
| 188 | bouncesz = host->max_blk_count * 512; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 189 | |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 190 | if (bouncesz > 512) { |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 191 | mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); |
| 192 | if (!mqrq_cur->bounce_buf) { |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 193 | printk(KERN_WARNING "%s: unable to " |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 194 | "allocate bounce cur buffer\n", |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 195 | mmc_card_name(card)); |
| 196 | } |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 197 | mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); |
| 198 | if (!mqrq_prev->bounce_buf) { |
| 199 | printk(KERN_WARNING "%s: unable to " |
| 200 | "allocate bounce prev buffer\n", |
| 201 | mmc_card_name(card)); |
| 202 | kfree(mqrq_cur->bounce_buf); |
| 203 | mqrq_cur->bounce_buf = NULL; |
| 204 | } |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 207 | if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) { |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 208 | blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 209 | blk_queue_max_hw_sectors(mq->queue, bouncesz / 512); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 210 | blk_queue_max_segments(mq->queue, bouncesz / 512); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 211 | blk_queue_max_segment_size(mq->queue, bouncesz); |
| 212 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 213 | mqrq_cur->sg = mmc_alloc_sg(1, &ret); |
| 214 | if (ret) |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 215 | goto cleanup_queue; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 216 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 217 | mqrq_cur->bounce_sg = |
| 218 | mmc_alloc_sg(bouncesz / 512, &ret); |
| 219 | if (ret) |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 220 | goto cleanup_queue; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 221 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 222 | mqrq_prev->sg = mmc_alloc_sg(1, &ret); |
| 223 | if (ret) |
| 224 | goto cleanup_queue; |
| 225 | |
| 226 | mqrq_prev->bounce_sg = |
| 227 | mmc_alloc_sg(bouncesz / 512, &ret); |
| 228 | if (ret) |
| 229 | goto cleanup_queue; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | #endif |
| 233 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 234 | if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) { |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 235 | blk_queue_bounce_limit(mq->queue, limit); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 236 | blk_queue_max_hw_sectors(mq->queue, |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 237 | min(host->max_blk_count, host->max_req_size / 512)); |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 238 | blk_queue_max_segments(mq->queue, host->max_segs); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 239 | blk_queue_max_segment_size(mq->queue, host->max_seg_size); |
| 240 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 241 | mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret); |
| 242 | if (ret) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 243 | goto cleanup_queue; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 244 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 245 | |
| 246 | mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret); |
| 247 | if (ret) |
| 248 | goto cleanup_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Thomas Gleixner | 632cf92 | 2010-09-14 07:12:35 -0400 | [diff] [blame] | 251 | sema_init(&mq->thread_sem, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 253 | mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s", |
| 254 | host->index, subname ? subname : ""); |
Ethan Du | de528fa | 2010-09-30 18:40:27 -0400 | [diff] [blame] | 255 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 256 | if (IS_ERR(mq->thread)) { |
| 257 | ret = PTR_ERR(mq->thread); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 258 | goto free_bounce_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 261 | return 0; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 262 | free_bounce_sg: |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 263 | kfree(mqrq_cur->bounce_sg); |
| 264 | mqrq_cur->bounce_sg = NULL; |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 265 | kfree(mqrq_prev->bounce_sg); |
| 266 | mqrq_prev->bounce_sg = NULL; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 267 | |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 268 | cleanup_queue: |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 269 | kfree(mqrq_cur->sg); |
| 270 | mqrq_cur->sg = NULL; |
| 271 | kfree(mqrq_cur->bounce_buf); |
| 272 | mqrq_cur->bounce_buf = NULL; |
| 273 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 274 | kfree(mqrq_prev->sg); |
| 275 | mqrq_prev->sg = NULL; |
| 276 | kfree(mqrq_prev->bounce_buf); |
| 277 | mqrq_prev->bounce_buf = NULL; |
| 278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | blk_cleanup_queue(mq->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | return ret; |
| 281 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | void mmc_cleanup_queue(struct mmc_queue *mq) |
| 284 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 285 | struct request_queue *q = mq->queue; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 286 | unsigned long flags; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 287 | struct mmc_queue_req *mqrq_cur = mq->mqrq_cur; |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 288 | struct mmc_queue_req *mqrq_prev = mq->mqrq_prev; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 289 | |
Pierre Ossman | d2b46f6 | 2007-04-28 16:52:12 +0200 | [diff] [blame] | 290 | /* Make sure the queue isn't suspended, as that will deadlock */ |
| 291 | mmc_queue_resume(mq); |
| 292 | |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 293 | /* Then terminate our worker thread */ |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 294 | kthread_stop(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 296 | /* Empty the queue */ |
| 297 | spin_lock_irqsave(q->queue_lock, flags); |
| 298 | q->queuedata = NULL; |
| 299 | blk_start_queue(q); |
| 300 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 301 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 302 | kfree(mqrq_cur->bounce_sg); |
| 303 | mqrq_cur->bounce_sg = NULL; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 304 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 305 | kfree(mqrq_cur->sg); |
| 306 | mqrq_cur->sg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 308 | kfree(mqrq_cur->bounce_buf); |
| 309 | mqrq_cur->bounce_buf = NULL; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 310 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame^] | 311 | kfree(mqrq_prev->bounce_sg); |
| 312 | mqrq_prev->bounce_sg = NULL; |
| 313 | |
| 314 | kfree(mqrq_prev->sg); |
| 315 | mqrq_prev->sg = NULL; |
| 316 | |
| 317 | kfree(mqrq_prev->bounce_buf); |
| 318 | mqrq_prev->bounce_buf = NULL; |
| 319 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | mq->card = NULL; |
| 321 | } |
| 322 | EXPORT_SYMBOL(mmc_cleanup_queue); |
| 323 | |
| 324 | /** |
| 325 | * mmc_queue_suspend - suspend a MMC request queue |
| 326 | * @mq: MMC queue to suspend |
| 327 | * |
| 328 | * Stop the block request queue, and wait for our thread to |
| 329 | * complete any outstanding requests. This ensures that we |
| 330 | * won't suspend while a request is being processed. |
| 331 | */ |
| 332 | void mmc_queue_suspend(struct mmc_queue *mq) |
| 333 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 334 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | unsigned long flags; |
| 336 | |
| 337 | if (!(mq->flags & MMC_QUEUE_SUSPENDED)) { |
| 338 | mq->flags |= MMC_QUEUE_SUSPENDED; |
| 339 | |
| 340 | spin_lock_irqsave(q->queue_lock, flags); |
| 341 | blk_stop_queue(q); |
| 342 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 343 | |
| 344 | down(&mq->thread_sem); |
| 345 | } |
| 346 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | /** |
| 349 | * mmc_queue_resume - resume a previously suspended MMC request queue |
| 350 | * @mq: MMC queue to resume |
| 351 | */ |
| 352 | void mmc_queue_resume(struct mmc_queue *mq) |
| 353 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 354 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | unsigned long flags; |
| 356 | |
| 357 | if (mq->flags & MMC_QUEUE_SUSPENDED) { |
| 358 | mq->flags &= ~MMC_QUEUE_SUSPENDED; |
| 359 | |
| 360 | up(&mq->thread_sem); |
| 361 | |
| 362 | spin_lock_irqsave(q->queue_lock, flags); |
| 363 | blk_start_queue(q); |
| 364 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 365 | } |
| 366 | } |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 367 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 368 | /* |
| 369 | * Prepare the sg list(s) to be handed of to the host driver |
| 370 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 371 | unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 372 | { |
| 373 | unsigned int sg_len; |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 374 | size_t buflen; |
| 375 | struct scatterlist *sg; |
| 376 | int i; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 377 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 378 | if (!mqrq->bounce_buf) |
| 379 | return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 380 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 381 | BUG_ON(!mqrq->bounce_sg); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 382 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 383 | sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 384 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 385 | mqrq->bounce_sg_len = sg_len; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 386 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 387 | buflen = 0; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 388 | for_each_sg(mqrq->bounce_sg, sg, sg_len, i) |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 389 | buflen += sg->length; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 390 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 391 | sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 392 | |
| 393 | return 1; |
| 394 | } |
| 395 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 396 | /* |
| 397 | * If writing, bounce the data to the buffer before the request |
| 398 | * is sent to the host driver |
| 399 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 400 | void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 401 | { |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 402 | if (!mqrq->bounce_buf) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 403 | return; |
| 404 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 405 | if (rq_data_dir(mqrq->req) != WRITE) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 406 | return; |
| 407 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 408 | sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, |
| 409 | mqrq->bounce_buf, mqrq->sg[0].length); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 412 | /* |
| 413 | * If reading, bounce the data from the buffer after the request |
| 414 | * has been handled by the host driver |
| 415 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 416 | void mmc_queue_bounce_post(struct mmc_queue_req *mqrq) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 417 | { |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 418 | if (!mqrq->bounce_buf) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 419 | return; |
| 420 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 421 | if (rq_data_dir(mqrq->req) != READ) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 422 | return; |
| 423 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 424 | sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, |
| 425 | mqrq->bounce_buf, mqrq->sg[0].length); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 426 | } |