Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (C) 2003 Russell King, All Rights Reserved. |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 3 | * Copyright 2006-2007 Pierre Ossman |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | */ |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/module.h> |
| 12 | #include <linux/blkdev.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 13 | #include <linux/freezer.h> |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 14 | #include <linux/kthread.h> |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 15 | #include <linux/scatterlist.h> |
Santosh Shilimkar | 8e0cb8a | 2013-07-29 14:20:15 +0100 | [diff] [blame] | 16 | #include <linux/dma-mapping.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include <linux/mmc/card.h> |
| 19 | #include <linux/mmc/host.h> |
Linus Walleij | 29eb7bd | 2016-09-20 11:34:38 +0200 | [diff] [blame] | 20 | |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 21 | #include "queue.h" |
Linus Walleij | 29eb7bd | 2016-09-20 11:34:38 +0200 | [diff] [blame] | 22 | #include "block.h" |
Ulf Hansson | 55244c5 | 2017-01-13 14:14:08 +0100 | [diff] [blame] | 23 | #include "core.h" |
Ulf Hansson | 4facdde | 2017-01-13 14:14:14 +0100 | [diff] [blame] | 24 | #include "card.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 26 | #define MMC_QUEUE_BOUNCESZ 65536 |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | /* |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 29 | * Prepare a MMC request. This just filters out odd stuff. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | */ |
| 31 | static int mmc_prep_request(struct request_queue *q, struct request *req) |
| 32 | { |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 33 | struct mmc_queue *mq = q->queuedata; |
| 34 | |
Chuanxiao Dong | 4e93b9a | 2014-08-12 12:01:30 +0800 | [diff] [blame] | 35 | if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq))) |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 36 | return BLKPREP_KILL; |
| 37 | |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 38 | req->rq_flags |= RQF_DONTPREP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Pierre Ossman | 9c9f2d6 | 2007-05-16 17:29:21 +0200 | [diff] [blame] | 40 | return BLKPREP_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 43 | struct mmc_queue_req *mmc_queue_req_find(struct mmc_queue *mq, |
| 44 | struct request *req) |
| 45 | { |
| 46 | struct mmc_queue_req *mqrq; |
| 47 | int i = ffz(mq->qslots); |
| 48 | |
| 49 | if (i >= mq->qdepth) |
| 50 | return NULL; |
| 51 | |
| 52 | mqrq = &mq->mqrq[i]; |
| 53 | WARN_ON(mqrq->req || mq->qcnt >= mq->qdepth || |
| 54 | test_bit(mqrq->task_id, &mq->qslots)); |
| 55 | mqrq->req = req; |
| 56 | mq->qcnt += 1; |
| 57 | __set_bit(mqrq->task_id, &mq->qslots); |
| 58 | |
| 59 | return mqrq; |
| 60 | } |
| 61 | |
| 62 | void mmc_queue_req_free(struct mmc_queue *mq, |
| 63 | struct mmc_queue_req *mqrq) |
| 64 | { |
| 65 | WARN_ON(!mqrq->req || mq->qcnt < 1 || |
| 66 | !test_bit(mqrq->task_id, &mq->qslots)); |
| 67 | mqrq->req = NULL; |
| 68 | mq->qcnt -= 1; |
| 69 | __clear_bit(mqrq->task_id, &mq->qslots); |
| 70 | } |
| 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | static int mmc_queue_thread(void *d) |
| 73 | { |
| 74 | struct mmc_queue *mq = d; |
| 75 | struct request_queue *q = mq->queue; |
Adrian Hunter | e0097cf | 2016-11-29 12:09:10 +0200 | [diff] [blame] | 76 | struct mmc_context_info *cntx = &mq->card->host->context_info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 78 | current->flags |= PF_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | down(&mq->thread_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | do { |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 82 | struct request *req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | spin_lock_irq(q->queue_lock); |
| 85 | set_current_state(TASK_INTERRUPTIBLE); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 86 | req = blk_fetch_request(q); |
Adrian Hunter | e0097cf | 2016-11-29 12:09:10 +0200 | [diff] [blame] | 87 | mq->asleep = false; |
| 88 | cntx->is_waiting_last_req = false; |
| 89 | cntx->is_new_req = false; |
| 90 | if (!req) { |
| 91 | /* |
| 92 | * Dispatch queue is empty so set flags for |
| 93 | * mmc_request_fn() to wake us up. |
| 94 | */ |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 95 | if (mq->qcnt) |
Adrian Hunter | e0097cf | 2016-11-29 12:09:10 +0200 | [diff] [blame] | 96 | cntx->is_waiting_last_req = true; |
| 97 | else |
| 98 | mq->asleep = true; |
| 99 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | spin_unlock_irq(q->queue_lock); |
| 101 | |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 102 | if (req || mq->qcnt) { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 103 | set_current_state(TASK_RUNNING); |
Linus Walleij | 29eb7bd | 2016-09-20 11:34:38 +0200 | [diff] [blame] | 104 | mmc_blk_issue_rq(mq, req); |
Rabin Vincent | a8c27c0 | 2015-06-14 19:26:11 +0200 | [diff] [blame] | 105 | cond_resched(); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 106 | } else { |
Vitaly Wool | 7b30d28 | 2006-12-07 20:08:02 +0100 | [diff] [blame] | 107 | if (kthread_should_stop()) { |
| 108 | set_current_state(TASK_RUNNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | break; |
Vitaly Wool | 7b30d28 | 2006-12-07 20:08:02 +0100 | [diff] [blame] | 110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | up(&mq->thread_sem); |
| 112 | schedule(); |
| 113 | down(&mq->thread_sem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } while (1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | up(&mq->thread_sem); |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * Generic MMC request handler. This is called for any queue on a |
| 123 | * particular host. When the host is not busy, we look for a request |
| 124 | * on any queue on this host, and attempt to issue it. This may |
| 125 | * not be the queue we were asked to process. |
| 126 | */ |
Venkatraman S | 1b50f5f | 2012-04-13 17:54:11 +0530 | [diff] [blame] | 127 | static void mmc_request_fn(struct request_queue *q) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | { |
| 129 | struct mmc_queue *mq = q->queuedata; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 130 | struct request *req; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 131 | struct mmc_context_info *cntx; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 132 | |
| 133 | if (!mq) { |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 134 | while ((req = blk_fetch_request(q)) != NULL) { |
Christoph Hellwig | e806402 | 2016-10-20 15:12:13 +0200 | [diff] [blame] | 135 | req->rq_flags |= RQF_QUIET; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame^] | 136 | __blk_end_request_all(req, BLK_STS_IOERR); |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 137 | } |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 138 | return; |
| 139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 141 | cntx = &mq->card->host->context_info; |
Adrian Hunter | e0097cf | 2016-11-29 12:09:10 +0200 | [diff] [blame] | 142 | |
| 143 | if (cntx->is_waiting_last_req) { |
| 144 | cntx->is_new_req = true; |
| 145 | wake_up_interruptible(&cntx->wait); |
| 146 | } |
| 147 | |
| 148 | if (mq->asleep) |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 149 | wake_up_process(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | } |
| 151 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 152 | static struct scatterlist *mmc_alloc_sg(int sg_len) |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 153 | { |
| 154 | struct scatterlist *sg; |
| 155 | |
Markus Elfring | 63928d4 | 2017-01-08 22:10:40 +0100 | [diff] [blame] | 156 | sg = kmalloc_array(sg_len, sizeof(*sg), GFP_KERNEL); |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 157 | if (sg) |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 158 | sg_init_table(sg, sg_len); |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 159 | |
| 160 | return sg; |
| 161 | } |
| 162 | |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 163 | static void mmc_queue_setup_discard(struct request_queue *q, |
| 164 | struct mmc_card *card) |
| 165 | { |
| 166 | unsigned max_discard; |
| 167 | |
| 168 | max_discard = mmc_calc_max_discard(card); |
| 169 | if (!max_discard) |
| 170 | return; |
| 171 | |
| 172 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q); |
Jens Axboe | 2bb4cd5 | 2015-07-14 08:15:12 -0600 | [diff] [blame] | 173 | blk_queue_max_discard_sectors(q, max_discard); |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 174 | q->limits.discard_granularity = card->pref_erase << 9; |
| 175 | /* granularity must not be greater than max. discard */ |
| 176 | if (card->pref_erase > max_discard) |
| 177 | q->limits.discard_granularity = 0; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 178 | if (mmc_can_secure_erase_trim(card)) |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 179 | queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q); |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 180 | } |
| 181 | |
Adrian Hunter | c5bda0c | 2016-11-29 12:09:15 +0200 | [diff] [blame] | 182 | static void mmc_queue_req_free_bufs(struct mmc_queue_req *mqrq) |
| 183 | { |
| 184 | kfree(mqrq->bounce_sg); |
| 185 | mqrq->bounce_sg = NULL; |
| 186 | |
| 187 | kfree(mqrq->sg); |
| 188 | mqrq->sg = NULL; |
| 189 | |
| 190 | kfree(mqrq->bounce_buf); |
| 191 | mqrq->bounce_buf = NULL; |
Adrian Hunter | 64e29e42 | 2016-11-29 12:09:13 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 194 | static void mmc_queue_reqs_free_bufs(struct mmc_queue_req *mqrq, int qdepth) |
Adrian Hunter | c09949c | 2016-11-29 12:09:14 +0200 | [diff] [blame] | 195 | { |
Adrian Hunter | c5bda0c | 2016-11-29 12:09:15 +0200 | [diff] [blame] | 196 | int i; |
Adrian Hunter | c09949c | 2016-11-29 12:09:14 +0200 | [diff] [blame] | 197 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 198 | for (i = 0; i < qdepth; i++) |
| 199 | mmc_queue_req_free_bufs(&mqrq[i]); |
| 200 | } |
| 201 | |
| 202 | static void mmc_queue_free_mqrqs(struct mmc_queue_req *mqrq, int qdepth) |
| 203 | { |
| 204 | mmc_queue_reqs_free_bufs(mqrq, qdepth); |
| 205 | kfree(mqrq); |
| 206 | } |
| 207 | |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 208 | static struct mmc_queue_req *mmc_queue_alloc_mqrqs(int qdepth) |
| 209 | { |
| 210 | struct mmc_queue_req *mqrq; |
| 211 | int i; |
| 212 | |
| 213 | mqrq = kcalloc(qdepth, sizeof(*mqrq), GFP_KERNEL); |
| 214 | if (mqrq) { |
| 215 | for (i = 0; i < qdepth; i++) |
| 216 | mqrq[i].task_id = i; |
| 217 | } |
| 218 | |
| 219 | return mqrq; |
| 220 | } |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | #ifdef CONFIG_MMC_BLOCK_BOUNCE |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 223 | static int mmc_queue_alloc_bounce_bufs(struct mmc_queue_req *mqrq, int qdepth, |
| 224 | unsigned int bouncesz) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | int i; |
| 227 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 228 | for (i = 0; i < qdepth; i++) { |
| 229 | mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL); |
| 230 | if (!mqrq[i].bounce_buf) |
| 231 | return -ENOMEM; |
| 232 | |
| 233 | mqrq[i].sg = mmc_alloc_sg(1); |
| 234 | if (!mqrq[i].sg) |
| 235 | return -ENOMEM; |
| 236 | |
| 237 | mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512); |
| 238 | if (!mqrq[i].bounce_sg) |
| 239 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 242 | return 0; |
| 243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 245 | static bool mmc_queue_alloc_bounce(struct mmc_queue_req *mqrq, int qdepth, |
| 246 | unsigned int bouncesz) |
| 247 | { |
| 248 | int ret; |
| 249 | |
| 250 | ret = mmc_queue_alloc_bounce_bufs(mqrq, qdepth, bouncesz); |
| 251 | if (ret) |
| 252 | mmc_queue_reqs_free_bufs(mqrq, qdepth); |
| 253 | |
| 254 | return !ret; |
| 255 | } |
| 256 | |
| 257 | static unsigned int mmc_queue_calc_bouncesz(struct mmc_host *host) |
| 258 | { |
| 259 | unsigned int bouncesz = MMC_QUEUE_BOUNCESZ; |
| 260 | |
| 261 | if (host->max_segs != 1) |
| 262 | return 0; |
| 263 | |
| 264 | if (bouncesz > host->max_req_size) |
| 265 | bouncesz = host->max_req_size; |
| 266 | if (bouncesz > host->max_seg_size) |
| 267 | bouncesz = host->max_seg_size; |
| 268 | if (bouncesz > host->max_blk_count * 512) |
| 269 | bouncesz = host->max_blk_count * 512; |
| 270 | |
| 271 | if (bouncesz <= 512) |
| 272 | return 0; |
| 273 | |
| 274 | return bouncesz; |
| 275 | } |
| 276 | #else |
| 277 | static inline bool mmc_queue_alloc_bounce(struct mmc_queue_req *mqrq, |
| 278 | int qdepth, unsigned int bouncesz) |
| 279 | { |
Adrian Hunter | c853982 | 2016-11-29 12:09:11 +0200 | [diff] [blame] | 280 | return false; |
| 281 | } |
| 282 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 283 | static unsigned int mmc_queue_calc_bouncesz(struct mmc_host *host) |
Adrian Hunter | c853982 | 2016-11-29 12:09:11 +0200 | [diff] [blame] | 284 | { |
Adrian Hunter | c853982 | 2016-11-29 12:09:11 +0200 | [diff] [blame] | 285 | return 0; |
| 286 | } |
Adrian Hunter | f2b8b52 | 2016-11-29 12:09:12 +0200 | [diff] [blame] | 287 | #endif |
| 288 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 289 | static int mmc_queue_alloc_sgs(struct mmc_queue_req *mqrq, int qdepth, |
| 290 | int max_segs) |
Adrian Hunter | f2b8b52 | 2016-11-29 12:09:12 +0200 | [diff] [blame] | 291 | { |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 292 | int i; |
Adrian Hunter | f2b8b52 | 2016-11-29 12:09:12 +0200 | [diff] [blame] | 293 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 294 | for (i = 0; i < qdepth; i++) { |
| 295 | mqrq[i].sg = mmc_alloc_sg(max_segs); |
| 296 | if (!mqrq[i].sg) |
| 297 | return -ENOMEM; |
Adrian Hunter | f2b8b52 | 2016-11-29 12:09:12 +0200 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 303 | void mmc_queue_free_shared_queue(struct mmc_card *card) |
Adrian Hunter | f2b8b52 | 2016-11-29 12:09:12 +0200 | [diff] [blame] | 304 | { |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 305 | if (card->mqrq) { |
| 306 | mmc_queue_free_mqrqs(card->mqrq, card->qdepth); |
| 307 | card->mqrq = NULL; |
| 308 | } |
Adrian Hunter | c853982 | 2016-11-29 12:09:11 +0200 | [diff] [blame] | 309 | } |
Adrian Hunter | c09949c | 2016-11-29 12:09:14 +0200 | [diff] [blame] | 310 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 311 | static int __mmc_queue_alloc_shared_queue(struct mmc_card *card, int qdepth) |
Adrian Hunter | c09949c | 2016-11-29 12:09:14 +0200 | [diff] [blame] | 312 | { |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 313 | struct mmc_host *host = card->host; |
| 314 | struct mmc_queue_req *mqrq; |
| 315 | unsigned int bouncesz; |
| 316 | int ret = 0; |
Adrian Hunter | c09949c | 2016-11-29 12:09:14 +0200 | [diff] [blame] | 317 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 318 | if (card->mqrq) |
| 319 | return -EINVAL; |
| 320 | |
| 321 | mqrq = mmc_queue_alloc_mqrqs(qdepth); |
| 322 | if (!mqrq) |
| 323 | return -ENOMEM; |
| 324 | |
| 325 | card->mqrq = mqrq; |
| 326 | card->qdepth = qdepth; |
| 327 | |
| 328 | bouncesz = mmc_queue_calc_bouncesz(host); |
| 329 | |
| 330 | if (bouncesz && !mmc_queue_alloc_bounce(mqrq, qdepth, bouncesz)) { |
| 331 | bouncesz = 0; |
| 332 | pr_warn("%s: unable to allocate bounce buffers\n", |
| 333 | mmc_card_name(card)); |
| 334 | } |
| 335 | |
| 336 | card->bouncesz = bouncesz; |
| 337 | |
| 338 | if (!bouncesz) { |
| 339 | ret = mmc_queue_alloc_sgs(mqrq, qdepth, host->max_segs); |
| 340 | if (ret) |
| 341 | goto out_err; |
| 342 | } |
| 343 | |
| 344 | return ret; |
| 345 | |
| 346 | out_err: |
| 347 | mmc_queue_free_shared_queue(card); |
| 348 | return ret; |
| 349 | } |
| 350 | |
| 351 | int mmc_queue_alloc_shared_queue(struct mmc_card *card) |
| 352 | { |
| 353 | return __mmc_queue_alloc_shared_queue(card, 2); |
Adrian Hunter | c09949c | 2016-11-29 12:09:14 +0200 | [diff] [blame] | 354 | } |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | /** |
| 357 | * mmc_init_queue - initialise a queue structure. |
| 358 | * @mq: mmc queue |
| 359 | * @card: mmc card to attach this queue |
| 360 | * @lock: queue lock |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 361 | * @subname: partition subname |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | * |
| 363 | * Initialise a MMC card request queue. |
| 364 | */ |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 365 | int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, |
| 366 | spinlock_t *lock, const char *subname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
| 368 | struct mmc_host *host = card->host; |
| 369 | u64 limit = BLK_BOUNCE_HIGH; |
Adrian Hunter | c5bda0c | 2016-11-29 12:09:15 +0200 | [diff] [blame] | 370 | int ret = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
Greg Kroah-Hartman | fcaf71f | 2006-09-12 17:00:10 +0200 | [diff] [blame] | 372 | if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask) |
Russell King | e83b366 | 2014-02-11 17:11:04 +0000 | [diff] [blame] | 373 | limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | |
| 375 | mq->card = card; |
Venkatraman S | 1b50f5f | 2012-04-13 17:54:11 +0530 | [diff] [blame] | 376 | mq->queue = blk_init_queue(mmc_request_fn, lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | if (!mq->queue) |
| 378 | return -ENOMEM; |
| 379 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 380 | mq->mqrq = card->mqrq; |
| 381 | mq->qdepth = card->qdepth; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | mq->queue->queuedata = mq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 384 | blk_queue_prep_rq(mq->queue, mmc_prep_request); |
Pierre Ossman | 8dddfe1 | 2008-10-14 20:04:46 +0200 | [diff] [blame] | 385 | queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 386 | queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue); |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 387 | if (mmc_can_erase(card)) |
| 388 | mmc_queue_setup_discard(mq->queue, card); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 389 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 390 | if (card->bouncesz) { |
| 391 | blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY); |
| 392 | blk_queue_max_hw_sectors(mq->queue, card->bouncesz / 512); |
| 393 | blk_queue_max_segments(mq->queue, card->bouncesz / 512); |
| 394 | blk_queue_max_segment_size(mq->queue, card->bouncesz); |
| 395 | } else { |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 396 | blk_queue_bounce_limit(mq->queue, limit); |
Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 397 | blk_queue_max_hw_sectors(mq->queue, |
Pierre Ossman | f3eb0aa | 2008-08-16 21:34:02 +0200 | [diff] [blame] | 398 | min(host->max_blk_count, host->max_req_size / 512)); |
Martin K. Petersen | a36274e | 2010-09-10 01:33:59 -0400 | [diff] [blame] | 399 | blk_queue_max_segments(mq->queue, host->max_segs); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 400 | blk_queue_max_segment_size(mq->queue, host->max_seg_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Thomas Gleixner | 632cf92 | 2010-09-14 07:12:35 -0400 | [diff] [blame] | 403 | sema_init(&mq->thread_sem, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 405 | mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s", |
| 406 | host->index, subname ? subname : ""); |
Ethan Du | de528fa | 2010-09-30 18:40:27 -0400 | [diff] [blame] | 407 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 408 | if (IS_ERR(mq->thread)) { |
| 409 | ret = PTR_ERR(mq->thread); |
Adrian Hunter | c09949c | 2016-11-29 12:09:14 +0200 | [diff] [blame] | 410 | goto cleanup_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 413 | return 0; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 414 | |
Adrian Hunter | 7b410d0 | 2017-03-13 14:36:36 +0200 | [diff] [blame] | 415 | cleanup_queue: |
Adrian Hunter | c5bda0c | 2016-11-29 12:09:15 +0200 | [diff] [blame] | 416 | mq->mqrq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | blk_cleanup_queue(mq->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | return ret; |
| 419 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
| 421 | void mmc_cleanup_queue(struct mmc_queue *mq) |
| 422 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 423 | struct request_queue *q = mq->queue; |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 424 | unsigned long flags; |
| 425 | |
Pierre Ossman | d2b46f6 | 2007-04-28 16:52:12 +0200 | [diff] [blame] | 426 | /* Make sure the queue isn't suspended, as that will deadlock */ |
| 427 | mmc_queue_resume(mq); |
| 428 | |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 429 | /* Then terminate our worker thread */ |
Christoph Hellwig | 87598a2 | 2006-11-13 20:23:52 +0100 | [diff] [blame] | 430 | kthread_stop(mq->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 432 | /* Empty the queue */ |
| 433 | spin_lock_irqsave(q->queue_lock, flags); |
| 434 | q->queuedata = NULL; |
| 435 | blk_start_queue(q); |
| 436 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 437 | |
Adrian Hunter | c5bda0c | 2016-11-29 12:09:15 +0200 | [diff] [blame] | 438 | mq->mqrq = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | mq->card = NULL; |
| 440 | } |
| 441 | EXPORT_SYMBOL(mmc_cleanup_queue); |
| 442 | |
| 443 | /** |
| 444 | * mmc_queue_suspend - suspend a MMC request queue |
| 445 | * @mq: MMC queue to suspend |
| 446 | * |
| 447 | * Stop the block request queue, and wait for our thread to |
| 448 | * complete any outstanding requests. This ensures that we |
| 449 | * won't suspend while a request is being processed. |
| 450 | */ |
| 451 | void mmc_queue_suspend(struct mmc_queue *mq) |
| 452 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 453 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | unsigned long flags; |
| 455 | |
Linus Walleij | 9491be5 | 2017-02-01 13:47:56 +0100 | [diff] [blame] | 456 | if (!mq->suspended) { |
| 457 | mq->suspended |= true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | spin_lock_irqsave(q->queue_lock, flags); |
| 460 | blk_stop_queue(q); |
| 461 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 462 | |
| 463 | down(&mq->thread_sem); |
| 464 | } |
| 465 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
| 467 | /** |
| 468 | * mmc_queue_resume - resume a previously suspended MMC request queue |
| 469 | * @mq: MMC queue to resume |
| 470 | */ |
| 471 | void mmc_queue_resume(struct mmc_queue *mq) |
| 472 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 473 | struct request_queue *q = mq->queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | unsigned long flags; |
| 475 | |
Linus Walleij | 9491be5 | 2017-02-01 13:47:56 +0100 | [diff] [blame] | 476 | if (mq->suspended) { |
| 477 | mq->suspended = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 479 | up(&mq->thread_sem); |
| 480 | |
| 481 | spin_lock_irqsave(q->queue_lock, flags); |
| 482 | blk_start_queue(q); |
| 483 | spin_unlock_irqrestore(q->queue_lock, flags); |
| 484 | } |
| 485 | } |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 486 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 487 | /* |
| 488 | * Prepare the sg list(s) to be handed of to the host driver |
| 489 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 490 | 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] | 491 | { |
| 492 | unsigned int sg_len; |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 493 | size_t buflen; |
| 494 | struct scatterlist *sg; |
| 495 | int i; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 496 | |
Linus Walleij | 03d640a | 2016-11-25 10:35:00 +0100 | [diff] [blame] | 497 | if (!mqrq->bounce_buf) |
| 498 | return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 499 | |
Linus Walleij | 03d640a | 2016-11-25 10:35:00 +0100 | [diff] [blame] | 500 | 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] | 501 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 502 | mqrq->bounce_sg_len = sg_len; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 503 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 504 | buflen = 0; |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 505 | for_each_sg(mqrq->bounce_sg, sg, sg_len, i) |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 506 | buflen += sg->length; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 507 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 508 | sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 509 | |
| 510 | return 1; |
| 511 | } |
| 512 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 513 | /* |
| 514 | * If writing, bounce the data to the buffer before the request |
| 515 | * is sent to the host driver |
| 516 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 517 | void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 518 | { |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 519 | if (!mqrq->bounce_buf) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 520 | return; |
| 521 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 522 | if (rq_data_dir(mqrq->req) != WRITE) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 523 | return; |
| 524 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 525 | sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, |
| 526 | mqrq->bounce_buf, mqrq->sg[0].length); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 527 | } |
| 528 | |
Pierre Ossman | 2ff1fa6 | 2008-07-22 14:35:42 +0200 | [diff] [blame] | 529 | /* |
| 530 | * If reading, bounce the data from the buffer after the request |
| 531 | * has been handled by the host driver |
| 532 | */ |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 533 | void mmc_queue_bounce_post(struct mmc_queue_req *mqrq) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 534 | { |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 535 | if (!mqrq->bounce_buf) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 536 | return; |
| 537 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 538 | if (rq_data_dir(mqrq->req) != READ) |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 539 | return; |
| 540 | |
Per Forlin | 97868a2 | 2011-07-09 17:12:36 -0400 | [diff] [blame] | 541 | sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len, |
| 542 | mqrq->bounce_buf, mqrq->sg[0].length); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 543 | } |