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> |
Santosh Shilimkar | 8e0cb8a | 2013-07-29 14:20:15 +0100 | [diff] [blame] | 18 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
| 20 | #include <linux/mmc/card.h> |
| 21 | #include <linux/mmc/host.h> |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 22 | #include "queue.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 24 | #define MMC_QUEUE_BOUNCESZ 65536 |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | /* |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 27 | * Prepare a MMC request. This just filters out odd stuff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | */ |
| 29 | static int mmc_prep_request(struct request_queue *q, struct request *req) |
| 30 | { |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 31 | struct mmc_queue *mq = q->queuedata; |
| 32 | |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 33 | /* |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 34 | * We only like normal block requests and discards. |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 35 | */ |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 36 | if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | blk_dump_rq_flags(req, "MMC bad request"); |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 38 | return BLKPREP_KILL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 41 | if (mq && mmc_card_removed(mq->card)) |
| 42 | return BLKPREP_KILL; |
| 43 | |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 44 | req->cmd_flags |= REQ_DONTPREP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 46 | return BLKPREP_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static int mmc_queue_thread(void *d) |
| 50 | { |
| 51 | struct mmc_queue *mq = d; |
| 52 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 54 | current->flags |= PF_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | down(&mq->thread_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | do { |
| 58 | struct request *req = NULL; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 59 | struct mmc_queue_req *tmp; |
Seungwon Jeon | 369d321 | 2012-12-26 10:40:17 +0900 | [diff] [blame] | 60 | unsigned int cmd_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | |
| 62 | spin_lock_irq(q->queue_lock); |
| 63 | set_current_state(TASK_INTERRUPTIBLE); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 64 | req = blk_fetch_request(q); |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 65 | mq->mqrq_cur->req = req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | spin_unlock_irq(q->queue_lock); |
| 67 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 68 | if (req || mq->mqrq_prev->req) { |
| 69 | set_current_state(TASK_RUNNING); |
Seungwon Jeon | 369d321 | 2012-12-26 10:40:17 +0900 | [diff] [blame] | 70 | cmd_flags = req ? req->cmd_flags : 0; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 71 | mq->issue_fn(mq, req); |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 72 | if (mq->flags & MMC_QUEUE_NEW_REQUEST) { |
| 73 | mq->flags &= ~MMC_QUEUE_NEW_REQUEST; |
| 74 | continue; /* fetch again */ |
| 75 | } |
Seungwon Jeon | 45c5a91 | 2012-09-28 19:12:53 +0900 | [diff] [blame] | 76 | |
| 77 | /* |
| 78 | * Current request becomes previous request |
| 79 | * and vice versa. |
Seungwon Jeon | 369d321 | 2012-12-26 10:40:17 +0900 | [diff] [blame] | 80 | * In case of special requests, current request |
| 81 | * has been finished. Do not assign it to previous |
| 82 | * request. |
Seungwon Jeon | 45c5a91 | 2012-09-28 19:12:53 +0900 | [diff] [blame] | 83 | */ |
Seungwon Jeon | 369d321 | 2012-12-26 10:40:17 +0900 | [diff] [blame] | 84 | if (cmd_flags & MMC_REQ_SPECIAL_MASK) |
| 85 | mq->mqrq_cur->req = NULL; |
| 86 | |
Seungwon Jeon | 45c5a91 | 2012-09-28 19:12:53 +0900 | [diff] [blame] | 87 | mq->mqrq_prev->brq.mrq.data = NULL; |
| 88 | mq->mqrq_prev->req = NULL; |
| 89 | tmp = mq->mqrq_prev; |
| 90 | mq->mqrq_prev = mq->mqrq_cur; |
| 91 | mq->mqrq_cur = tmp; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 92 | } else { |
Vitaly Wool | 7b30d28 | 2006-12-07 20:08:02 +0100 | [diff] [blame] | 93 | if (kthread_should_stop()) { |
| 94 | set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | break; |
Vitaly Wool | 7b30d28 | 2006-12-07 20:08:02 +0100 | [diff] [blame] | 96 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | up(&mq->thread_sem); |
| 98 | schedule(); |
| 99 | down(&mq->thread_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } while (1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | up(&mq->thread_sem); |
| 103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Generic MMC request handler. This is called for any queue on a |
| 109 | * particular host. When the host is not busy, we look for a request |
| 110 | * on any queue on this host, and attempt to issue it. This may |
| 111 | * not be the queue we were asked to process. |
| 112 | */ |
Venkatraman S | 1b50f5f | 2012-04-13 17:54:11 +0530 | [diff] [blame] | 113 | static void mmc_request_fn(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | struct mmc_queue *mq = q->queuedata; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 116 | struct request *req; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 117 | unsigned long flags; |
| 118 | struct mmc_context_info *cntx; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 119 | |
| 120 | if (!mq) { |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 121 | while ((req = blk_fetch_request(q)) != NULL) { |
| 122 | req->cmd_flags |= REQ_QUIET; |
Tejun Heo | 296b2f6 | 2009-05-08 11:54:15 +0900 | [diff] [blame] | 123 | __blk_end_request_all(req, -EIO); |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 124 | } |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 125 | return; |
| 126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 128 | cntx = &mq->card->host->context_info; |
| 129 | if (!mq->mqrq_cur->req && mq->mqrq_prev->req) { |
| 130 | /* |
| 131 | * New MMC request arrived when MMC thread may be |
| 132 | * blocked on the previous request to be complete |
| 133 | * with no current request fetched |
| 134 | */ |
| 135 | spin_lock_irqsave(&cntx->lock, flags); |
| 136 | if (cntx->is_waiting_last_req) { |
| 137 | cntx->is_new_req = true; |
| 138 | wake_up_interruptible(&cntx->wait); |
| 139 | } |
| 140 | spin_unlock_irqrestore(&cntx->lock, flags); |
| 141 | } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req) |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 142 | wake_up_process(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Venkatraman S | 7513cd7 | 2011-08-23 21:16:02 +0530 | [diff] [blame] | 145 | static struct scatterlist *mmc_alloc_sg(int sg_len, int *err) |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 146 | { |
| 147 | struct scatterlist *sg; |
| 148 | |
| 149 | sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL); |
| 150 | if (!sg) |
| 151 | *err = -ENOMEM; |
| 152 | else { |
| 153 | *err = 0; |
| 154 | sg_init_table(sg, sg_len); |
| 155 | } |
| 156 | |
| 157 | return sg; |
| 158 | } |
| 159 | |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 160 | static void mmc_queue_setup_discard(struct request_queue *q, |
| 161 | struct mmc_card *card) |
| 162 | { |
| 163 | unsigned max_discard; |
| 164 | |
| 165 | max_discard = mmc_calc_max_discard(card); |
| 166 | if (!max_discard) |
| 167 | return; |
| 168 | |
| 169 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
| 170 | q->limits.max_discard_sectors = max_discard; |
Adrian Hunter | 7194efb | 2012-04-05 14:45:47 +0300 | [diff] [blame] | 171 | if (card->erased_byte == 0 && !mmc_can_discard(card)) |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 172 | q->limits.discard_zeroes_data = 1; |
| 173 | q->limits.discard_granularity = card->pref_erase << 9; |
| 174 | /* granularity must not be greater than max. discard */ |
| 175 | if (card->pref_erase > max_discard) |
| 176 | q->limits.discard_granularity = 0; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 177 | if (mmc_can_secure_erase_trim(card)) |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 178 | queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q); |
| 179 | } |
| 180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | /** |
| 182 | * mmc_init_queue - initialise a queue structure. |
| 183 | * @mq: mmc queue |
| 184 | * @card: mmc card to attach this queue |
| 185 | * @lock: queue lock |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 186 | * @subname: partition subname |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | * |
| 188 | * Initialise a MMC card request queue. |
| 189 | */ |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 190 | int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, |
| 191 | spinlock_t *lock, const char *subname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
| 193 | struct mmc_host *host = card->host; |
| 194 | u64 limit = BLK_BOUNCE_HIGH; |
| 195 | int ret; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 196 | struct mmc_queue_req *mqrq_cur = &mq->mqrq[0]; |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 197 | struct mmc_queue_req *mqrq_prev = &mq->mqrq[1]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Greg Kroah-Hartman | fcaf71f | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 199 | if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask) |
Russell King | e83b366 | 2014-02-11 17:11:04 +0000 | [diff] [blame] | 200 | limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | mq->card = card; |
Venkatraman S | 1b50f5f | 2012-04-13 17:54:11 +0530 | [diff] [blame] | 203 | mq->queue = blk_init_queue(mmc_request_fn, lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | if (!mq->queue) |
| 205 | return -ENOMEM; |
| 206 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 207 | mq->mqrq_cur = mqrq_cur; |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 208 | mq->mqrq_prev = mqrq_prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | mq->queue->queuedata = mq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 211 | blk_queue_prep_rq(mq->queue, mmc_prep_request); |
Pierre Ossman | 8dddfe1 | 2008-10-14 20:04:46 +0200 | [diff] [blame] | 212 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue); |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 213 | if (mmc_can_erase(card)) |
| 214 | mmc_queue_setup_discard(mq->queue, card); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 215 | |
| 216 | #ifdef CONFIG_MMC_BLOCK_BOUNCE |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 217 | if (host->max_segs == 1) { |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 218 | unsigned int bouncesz; |
| 219 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 220 | bouncesz = MMC_QUEUE_BOUNCESZ; |
| 221 | |
| 222 | if (bouncesz > host->max_req_size) |
| 223 | bouncesz = host->max_req_size; |
| 224 | if (bouncesz > host->max_seg_size) |
| 225 | bouncesz = host->max_seg_size; |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 226 | if (bouncesz > (host->max_blk_count * 512)) |
| 227 | bouncesz = host->max_blk_count * 512; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 228 | |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 229 | if (bouncesz > 512) { |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 230 | mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); |
| 231 | if (!mqrq_cur->bounce_buf) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame^] | 232 | pr_warn("%s: unable to allocate bounce cur buffer\n", |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 233 | mmc_card_name(card)); |
| 234 | } |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 235 | mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL); |
| 236 | if (!mqrq_prev->bounce_buf) { |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame^] | 237 | pr_warn("%s: unable to allocate bounce prev buffer\n", |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 238 | mmc_card_name(card)); |
| 239 | kfree(mqrq_cur->bounce_buf); |
| 240 | mqrq_cur->bounce_buf = NULL; |
| 241 | } |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 244 | if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) { |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 245 | blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 246 | blk_queue_max_hw_sectors(mq->queue, bouncesz / 512); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 247 | blk_queue_max_segments(mq->queue, bouncesz / 512); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 248 | blk_queue_max_segment_size(mq->queue, bouncesz); |
| 249 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 250 | mqrq_cur->sg = mmc_alloc_sg(1, &ret); |
| 251 | if (ret) |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 252 | goto cleanup_queue; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 253 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 254 | mqrq_cur->bounce_sg = |
| 255 | mmc_alloc_sg(bouncesz / 512, &ret); |
| 256 | if (ret) |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 257 | goto cleanup_queue; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 258 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 259 | mqrq_prev->sg = mmc_alloc_sg(1, &ret); |
| 260 | if (ret) |
| 261 | goto cleanup_queue; |
| 262 | |
| 263 | mqrq_prev->bounce_sg = |
| 264 | mmc_alloc_sg(bouncesz / 512, &ret); |
| 265 | if (ret) |
| 266 | goto cleanup_queue; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | #endif |
| 270 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 271 | if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) { |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 272 | blk_queue_bounce_limit(mq->queue, limit); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 273 | blk_queue_max_hw_sectors(mq->queue, |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 274 | min(host->max_blk_count, host->max_req_size / 512)); |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 275 | blk_queue_max_segments(mq->queue, host->max_segs); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 276 | blk_queue_max_segment_size(mq->queue, host->max_seg_size); |
| 277 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 278 | mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret); |
| 279 | if (ret) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 280 | goto cleanup_queue; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 281 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 282 | |
| 283 | mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret); |
| 284 | if (ret) |
| 285 | goto cleanup_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Thomas Gleixner | 632cf92 | 2010-09-14 07:12:35 -0400 | [diff] [blame] | 288 | sema_init(&mq->thread_sem, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 290 | mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s", |
| 291 | host->index, subname ? subname : ""); |
Ethan Du | de528fa | 2010-09-30 18:40:27 -0400 | [diff] [blame] | 292 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 293 | if (IS_ERR(mq->thread)) { |
| 294 | ret = PTR_ERR(mq->thread); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 295 | goto free_bounce_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
| 297 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 298 | return 0; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 299 | free_bounce_sg: |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 300 | kfree(mqrq_cur->bounce_sg); |
| 301 | mqrq_cur->bounce_sg = NULL; |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 302 | kfree(mqrq_prev->bounce_sg); |
| 303 | mqrq_prev->bounce_sg = NULL; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 304 | |
Pierre Ossman | aafabfa | 2007-08-09 14:28:02 +0200 | [diff] [blame] | 305 | cleanup_queue: |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 306 | kfree(mqrq_cur->sg); |
| 307 | mqrq_cur->sg = NULL; |
| 308 | kfree(mqrq_cur->bounce_buf); |
| 309 | mqrq_cur->bounce_buf = NULL; |
| 310 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 311 | kfree(mqrq_prev->sg); |
| 312 | mqrq_prev->sg = NULL; |
| 313 | kfree(mqrq_prev->bounce_buf); |
| 314 | mqrq_prev->bounce_buf = NULL; |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | blk_cleanup_queue(mq->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | return ret; |
| 318 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | void mmc_cleanup_queue(struct mmc_queue *mq) |
| 321 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 322 | struct request_queue *q = mq->queue; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 323 | unsigned long flags; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 324 | struct mmc_queue_req *mqrq_cur = mq->mqrq_cur; |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 325 | struct mmc_queue_req *mqrq_prev = mq->mqrq_prev; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 326 | |
Pierre Ossman | d2b46f6 | 2007-04-28 16:52:12 +0200 | [diff] [blame] | 327 | /* Make sure the queue isn't suspended, as that will deadlock */ |
| 328 | mmc_queue_resume(mq); |
| 329 | |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 330 | /* Then terminate our worker thread */ |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 331 | kthread_stop(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 333 | /* Empty the queue */ |
| 334 | spin_lock_irqsave(q->queue_lock, flags); |
| 335 | q->queuedata = NULL; |
| 336 | blk_start_queue(q); |
| 337 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 338 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 339 | kfree(mqrq_cur->bounce_sg); |
| 340 | mqrq_cur->bounce_sg = NULL; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 341 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 342 | kfree(mqrq_cur->sg); |
| 343 | mqrq_cur->sg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 345 | kfree(mqrq_cur->bounce_buf); |
| 346 | mqrq_cur->bounce_buf = NULL; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 347 | |
Per Forlin | 04296b7 | 2011-07-01 18:55:31 +0200 | [diff] [blame] | 348 | kfree(mqrq_prev->bounce_sg); |
| 349 | mqrq_prev->bounce_sg = NULL; |
| 350 | |
| 351 | kfree(mqrq_prev->sg); |
| 352 | mqrq_prev->sg = NULL; |
| 353 | |
| 354 | kfree(mqrq_prev->bounce_buf); |
| 355 | mqrq_prev->bounce_buf = NULL; |
| 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | mq->card = NULL; |
| 358 | } |
| 359 | EXPORT_SYMBOL(mmc_cleanup_queue); |
| 360 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 361 | int mmc_packed_init(struct mmc_queue *mq, struct mmc_card *card) |
| 362 | { |
| 363 | struct mmc_queue_req *mqrq_cur = &mq->mqrq[0]; |
| 364 | struct mmc_queue_req *mqrq_prev = &mq->mqrq[1]; |
| 365 | int ret = 0; |
| 366 | |
| 367 | |
| 368 | mqrq_cur->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL); |
| 369 | if (!mqrq_cur->packed) { |
| 370 | pr_warn("%s: unable to allocate packed cmd for mqrq_cur\n", |
| 371 | mmc_card_name(card)); |
| 372 | ret = -ENOMEM; |
| 373 | goto out; |
| 374 | } |
| 375 | |
| 376 | mqrq_prev->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL); |
| 377 | if (!mqrq_prev->packed) { |
| 378 | pr_warn("%s: unable to allocate packed cmd for mqrq_prev\n", |
| 379 | mmc_card_name(card)); |
| 380 | kfree(mqrq_cur->packed); |
| 381 | mqrq_cur->packed = NULL; |
| 382 | ret = -ENOMEM; |
| 383 | goto out; |
| 384 | } |
| 385 | |
| 386 | INIT_LIST_HEAD(&mqrq_cur->packed->list); |
| 387 | INIT_LIST_HEAD(&mqrq_prev->packed->list); |
| 388 | |
| 389 | out: |
| 390 | return ret; |
| 391 | } |
| 392 | |
| 393 | void mmc_packed_clean(struct mmc_queue *mq) |
| 394 | { |
| 395 | struct mmc_queue_req *mqrq_cur = &mq->mqrq[0]; |
| 396 | struct mmc_queue_req *mqrq_prev = &mq->mqrq[1]; |
| 397 | |
| 398 | kfree(mqrq_cur->packed); |
| 399 | mqrq_cur->packed = NULL; |
| 400 | kfree(mqrq_prev->packed); |
| 401 | mqrq_prev->packed = NULL; |
| 402 | } |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | /** |
| 405 | * mmc_queue_suspend - suspend a MMC request queue |
| 406 | * @mq: MMC queue to suspend |
| 407 | * |
| 408 | * Stop the block request queue, and wait for our thread to |
| 409 | * complete any outstanding requests. This ensures that we |
| 410 | * won't suspend while a request is being processed. |
| 411 | */ |
| 412 | void mmc_queue_suspend(struct mmc_queue *mq) |
| 413 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 414 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | unsigned long flags; |
| 416 | |
| 417 | if (!(mq->flags & MMC_QUEUE_SUSPENDED)) { |
| 418 | mq->flags |= MMC_QUEUE_SUSPENDED; |
| 419 | |
| 420 | spin_lock_irqsave(q->queue_lock, flags); |
| 421 | blk_stop_queue(q); |
| 422 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 423 | |
| 424 | down(&mq->thread_sem); |
| 425 | } |
| 426 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
| 428 | /** |
| 429 | * mmc_queue_resume - resume a previously suspended MMC request queue |
| 430 | * @mq: MMC queue to resume |
| 431 | */ |
| 432 | void mmc_queue_resume(struct mmc_queue *mq) |
| 433 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 434 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | unsigned long flags; |
| 436 | |
| 437 | if (mq->flags & MMC_QUEUE_SUSPENDED) { |
| 438 | mq->flags &= ~MMC_QUEUE_SUSPENDED; |
| 439 | |
| 440 | up(&mq->thread_sem); |
| 441 | |
| 442 | spin_lock_irqsave(q->queue_lock, flags); |
| 443 | blk_start_queue(q); |
| 444 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 445 | } |
| 446 | } |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 447 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 448 | static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq, |
| 449 | struct mmc_packed *packed, |
| 450 | struct scatterlist *sg, |
| 451 | enum mmc_packed_type cmd_type) |
| 452 | { |
| 453 | struct scatterlist *__sg = sg; |
| 454 | unsigned int sg_len = 0; |
| 455 | struct request *req; |
| 456 | |
| 457 | if (mmc_packed_wr(cmd_type)) { |
| 458 | unsigned int hdr_sz = mmc_large_sector(mq->card) ? 4096 : 512; |
| 459 | unsigned int max_seg_sz = queue_max_segment_size(mq->queue); |
| 460 | unsigned int len, remain, offset = 0; |
| 461 | u8 *buf = (u8 *)packed->cmd_hdr; |
| 462 | |
| 463 | remain = hdr_sz; |
| 464 | do { |
| 465 | len = min(remain, max_seg_sz); |
| 466 | sg_set_buf(__sg, buf + offset, len); |
| 467 | offset += len; |
| 468 | remain -= len; |
| 469 | (__sg++)->page_link &= ~0x02; |
| 470 | sg_len++; |
| 471 | } while (remain); |
| 472 | } |
| 473 | |
| 474 | list_for_each_entry(req, &packed->list, queuelist) { |
| 475 | sg_len += blk_rq_map_sg(mq->queue, req, __sg); |
| 476 | __sg = sg + (sg_len - 1); |
| 477 | (__sg++)->page_link &= ~0x02; |
| 478 | } |
| 479 | sg_mark_end(sg + (sg_len - 1)); |
| 480 | return sg_len; |
| 481 | } |
| 482 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 483 | /* |
| 484 | * Prepare the sg list(s) to be handed of to the host driver |
| 485 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 486 | 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] | 487 | { |
| 488 | unsigned int sg_len; |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 489 | size_t buflen; |
| 490 | struct scatterlist *sg; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 491 | enum mmc_packed_type cmd_type; |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 492 | int i; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 493 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 494 | cmd_type = mqrq->cmd_type; |
| 495 | |
| 496 | if (!mqrq->bounce_buf) { |
| 497 | if (mmc_packed_cmd(cmd_type)) |
| 498 | return mmc_queue_packed_map_sg(mq, mqrq->packed, |
| 499 | mqrq->sg, cmd_type); |
| 500 | else |
| 501 | return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg); |
| 502 | } |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 503 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 504 | BUG_ON(!mqrq->bounce_sg); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 505 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 506 | if (mmc_packed_cmd(cmd_type)) |
| 507 | sg_len = mmc_queue_packed_map_sg(mq, mqrq->packed, |
| 508 | mqrq->bounce_sg, cmd_type); |
| 509 | else |
| 510 | 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] | 511 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 512 | mqrq->bounce_sg_len = sg_len; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 513 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 514 | buflen = 0; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 515 | for_each_sg(mqrq->bounce_sg, sg, sg_len, i) |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 516 | buflen += sg->length; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 517 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 518 | sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 519 | |
| 520 | return 1; |
| 521 | } |
| 522 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 523 | /* |
| 524 | * If writing, bounce the data to the buffer before the request |
| 525 | * is sent to the host driver |
| 526 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 527 | void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 528 | { |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 529 | if (!mqrq->bounce_buf) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 530 | return; |
| 531 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 532 | if (rq_data_dir(mqrq->req) != WRITE) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 533 | return; |
| 534 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 535 | sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, |
| 536 | mqrq->bounce_buf, mqrq->sg[0].length); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 537 | } |
| 538 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 539 | /* |
| 540 | * If reading, bounce the data from the buffer after the request |
| 541 | * has been handled by the host driver |
| 542 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 543 | void mmc_queue_bounce_post(struct mmc_queue_req *mqrq) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 544 | { |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 545 | if (!mqrq->bounce_buf) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 546 | return; |
| 547 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 548 | if (rq_data_dir(mqrq->req) != READ) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 549 | return; |
| 550 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 551 | sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, |
| 552 | mqrq->bounce_buf, mqrq->sg[0].length); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 553 | } |