blob: bf14642a576a515a50b51de61daf8656b8cfc443 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/card/queue.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Russell King, All Rights Reserved.
Pierre Ossman98ac2162006-12-23 20:03:02 +01005 * Copyright 2006-2007 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/blkdev.h>
Rafael J. Wysocki83144182007-07-17 04:03:35 -070015#include <linux/freezer.h>
Christoph Hellwig87598a22006-11-13 20:23:52 +010016#include <linux/kthread.h>
Jens Axboe45711f12007-10-22 21:19:53 +020017#include <linux/scatterlist.h>
Santosh Shilimkar8e0cb8a2013-07-29 14:20:15 +010018#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#include <linux/mmc/card.h>
21#include <linux/mmc/host.h>
Pierre Ossman98ac2162006-12-23 20:03:02 +010022#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Pierre Ossman98ccf142007-05-12 00:26:16 +020024#define MMC_QUEUE_BOUNCESZ 65536
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/*
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020027 * Prepare a MMC request. This just filters out odd stuff.
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 */
29static int mmc_prep_request(struct request_queue *q, struct request *req)
30{
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053031 struct mmc_queue *mq = q->queuedata;
32
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020033 /*
Adrian Hunterbd788c92010-08-11 14:17:47 -070034 * We only like normal block requests and discards.
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020035 */
Mike Christiec2df40d2016-06-05 14:32:17 -050036 if (req->cmd_type != REQ_TYPE_FS && req_op(req) != REQ_OP_DISCARD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 blk_dump_rq_flags(req, "MMC bad request");
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020038 return BLKPREP_KILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
40
Chuanxiao Dong4e93b9a2014-08-12 12:01:30 +080041 if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
Sujit Reddy Thummaa8ad82cc2011-12-08 14:05:50 +053042 return BLKPREP_KILL;
43
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020044 req->cmd_flags |= REQ_DONTPREP;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Pierre Ossman9c9f2d62007-05-16 17:29:21 +020046 return BLKPREP_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49static int mmc_queue_thread(void *d)
50{
51 struct mmc_queue *mq = d;
52 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Rafael J. Wysocki83144182007-07-17 04:03:35 -070054 current->flags |= PF_MEMALLOC;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 do {
58 struct request *req = NULL;
59
60 spin_lock_irq(q->queue_lock);
61 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010062 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040063 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 spin_unlock_irq(q->queue_lock);
65
Per Forlinee8a43a2011-07-01 18:55:33 +020066 if (req || mq->mqrq_prev->req) {
67 set_current_state(TASK_RUNNING);
68 mq->issue_fn(mq, req);
Rabin Vincenta8c27c02015-06-14 19:26:11 +020069 cond_resched();
Konstantin Dorfman2220eed2013-01-14 14:28:17 -050070 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
71 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
72 continue; /* fetch again */
73 }
Seungwon Jeon45c5a912012-09-28 19:12:53 +090074
75 /*
76 * Current request becomes previous request
77 * and vice versa.
Seungwon Jeon369d3212012-12-26 10:40:17 +090078 * In case of special requests, current request
79 * has been finished. Do not assign it to previous
80 * request.
Seungwon Jeon45c5a912012-09-28 19:12:53 +090081 */
Mike Christiec2df40d2016-06-05 14:32:17 -050082 if (mmc_req_is_special(req))
Seungwon Jeon369d3212012-12-26 10:40:17 +090083 mq->mqrq_cur->req = NULL;
84
Seungwon Jeon45c5a912012-09-28 19:12:53 +090085 mq->mqrq_prev->brq.mrq.data = NULL;
86 mq->mqrq_prev->req = NULL;
Fabian Frederick75518472015-06-10 18:30:53 +020087 swap(mq->mqrq_prev, mq->mqrq_cur);
Per Forlinee8a43a2011-07-01 18:55:33 +020088 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010089 if (kthread_should_stop()) {
90 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 up(&mq->thread_sem);
94 schedule();
95 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 up(&mq->thread_sem);
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101}
102
103/*
104 * Generic MMC request handler. This is called for any queue on a
105 * particular host. When the host is not busy, we look for a request
106 * on any queue on this host, and attempt to issue it. This may
107 * not be the queue we were asked to process.
108 */
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530109static void mmc_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
111 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100112 struct request *req;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500113 unsigned long flags;
114 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100115
116 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800117 while ((req = blk_fetch_request(q)) != NULL) {
118 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900119 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800120 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100121 return;
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500124 cntx = &mq->card->host->context_info;
125 if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
126 /*
127 * New MMC request arrived when MMC thread may be
128 * blocked on the previous request to be complete
129 * with no current request fetched
130 */
131 spin_lock_irqsave(&cntx->lock, flags);
132 if (cntx->is_waiting_last_req) {
133 cntx->is_new_req = true;
134 wake_up_interruptible(&cntx->wait);
135 }
136 spin_unlock_irqrestore(&cntx->lock, flags);
137 } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100138 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Venkatraman S7513cd72011-08-23 21:16:02 +0530141static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400142{
143 struct scatterlist *sg;
144
145 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
146 if (!sg)
147 *err = -ENOMEM;
148 else {
149 *err = 0;
150 sg_init_table(sg, sg_len);
151 }
152
153 return sg;
154}
155
Adrian Huntere056a1b2011-06-28 17:16:02 +0300156static void mmc_queue_setup_discard(struct request_queue *q,
157 struct mmc_card *card)
158{
159 unsigned max_discard;
160
161 max_discard = mmc_calc_max_discard(card);
162 if (!max_discard)
163 return;
164
165 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600166 blk_queue_max_discard_sectors(q, max_discard);
Adrian Hunter7194efb2012-04-05 14:45:47 +0300167 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300168 q->limits.discard_zeroes_data = 1;
169 q->limits.discard_granularity = card->pref_erase << 9;
170 /* granularity must not be greater than max. discard */
171 if (card->pref_erase > max_discard)
172 q->limits.discard_granularity = 0;
Maya Erez775a9362013-04-18 15:41:55 +0300173 if (mmc_can_secure_erase_trim(card))
Christoph Hellwig288dab82016-06-09 16:00:36 +0200174 queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300175}
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/**
178 * mmc_init_queue - initialise a queue structure.
179 * @mq: mmc queue
180 * @card: mmc card to attach this queue
181 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300182 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
184 * Initialise a MMC card request queue.
185 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300186int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
187 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct mmc_host *host = card->host;
190 u64 limit = BLK_BOUNCE_HIGH;
191 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400192 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200193 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200195 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000196 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530199 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (!mq->queue)
201 return -ENOMEM;
202
Per Forlin97868a22011-07-09 17:12:36 -0400203 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200204 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Pierre Ossman98ccf142007-05-12 00:26:16 +0200207 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200208 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600209 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300210 if (mmc_can_erase(card))
211 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200212
213#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400214 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200215 unsigned int bouncesz;
216
Pierre Ossman98ccf142007-05-12 00:26:16 +0200217 bouncesz = MMC_QUEUE_BOUNCESZ;
218
219 if (bouncesz > host->max_req_size)
220 bouncesz = host->max_req_size;
221 if (bouncesz > host->max_seg_size)
222 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200223 if (bouncesz > (host->max_blk_count * 512))
224 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200225
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200226 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400227 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
228 if (!mqrq_cur->bounce_buf) {
Joe Perches66061102014-09-12 14:56:56 -0700229 pr_warn("%s: unable to allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200230 mmc_card_name(card));
Bhuvanesh Suracharifdb409f2014-12-01 02:23:02 -0500231 } else {
232 mqrq_prev->bounce_buf =
233 kmalloc(bouncesz, GFP_KERNEL);
234 if (!mqrq_prev->bounce_buf) {
235 pr_warn("%s: unable to allocate bounce prev buffer\n",
236 mmc_card_name(card));
237 kfree(mqrq_cur->bounce_buf);
238 mqrq_cur->bounce_buf = NULL;
239 }
Per Forlin04296b72011-07-01 18:55:31 +0200240 }
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200241 }
242
Per Forlin04296b72011-07-01 18:55:31 +0200243 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200244 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500245 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500246 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200247 blk_queue_max_segment_size(mq->queue, bouncesz);
248
Per Forlin97868a22011-07-09 17:12:36 -0400249 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
250 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200251 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200252
Per Forlin97868a22011-07-09 17:12:36 -0400253 mqrq_cur->bounce_sg =
254 mmc_alloc_sg(bouncesz / 512, &ret);
255 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200256 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400257
Per Forlin04296b72011-07-01 18:55:31 +0200258 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
259 if (ret)
260 goto cleanup_queue;
261
262 mqrq_prev->bounce_sg =
263 mmc_alloc_sg(bouncesz / 512, &ret);
264 if (ret)
265 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200266 }
267 }
268#endif
269
Per Forlin04296b72011-07-01 18:55:31 +0200270 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200271 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500272 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aa2008-08-16 21:34:02 +0200273 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400274 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200275 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
276
Per Forlin97868a22011-07-09 17:12:36 -0400277 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
278 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200279 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400280
Per Forlin04296b72011-07-01 18:55:31 +0200281
282 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
283 if (ret)
284 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Thomas Gleixner632cf922010-09-14 07:12:35 -0400287 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
Adrian Hunterd09408a2011-06-23 13:40:28 +0300289 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
290 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400291
Christoph Hellwig87598a22006-11-13 20:23:52 +0100292 if (IS_ERR(mq->thread)) {
293 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200294 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Christoph Hellwig87598a22006-11-13 20:23:52 +0100297 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200298 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400299 kfree(mqrq_cur->bounce_sg);
300 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200301 kfree(mqrq_prev->bounce_sg);
302 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400303
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200304 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400305 kfree(mqrq_cur->sg);
306 mqrq_cur->sg = NULL;
307 kfree(mqrq_cur->bounce_buf);
308 mqrq_cur->bounce_buf = NULL;
309
Per Forlin04296b72011-07-01 18:55:31 +0200310 kfree(mqrq_prev->sg);
311 mqrq_prev->sg = NULL;
312 kfree(mqrq_prev->bounce_buf);
313 mqrq_prev->bounce_buf = NULL;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 return ret;
317}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319void mmc_cleanup_queue(struct mmc_queue *mq)
320{
Jens Axboe165125e2007-07-24 09:28:11 +0200321 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100322 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400323 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200324 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100325
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200326 /* Make sure the queue isn't suspended, as that will deadlock */
327 mmc_queue_resume(mq);
328
Pierre Ossman89b4e132006-11-14 22:08:16 +0100329 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100330 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800332 /* Empty the queue */
333 spin_lock_irqsave(q->queue_lock, flags);
334 q->queuedata = NULL;
335 blk_start_queue(q);
336 spin_unlock_irqrestore(q->queue_lock, flags);
337
Per Forlin97868a22011-07-09 17:12:36 -0400338 kfree(mqrq_cur->bounce_sg);
339 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200340
Per Forlin97868a22011-07-09 17:12:36 -0400341 kfree(mqrq_cur->sg);
342 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Per Forlin97868a22011-07-09 17:12:36 -0400344 kfree(mqrq_cur->bounce_buf);
345 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200346
Per Forlin04296b72011-07-01 18:55:31 +0200347 kfree(mqrq_prev->bounce_sg);
348 mqrq_prev->bounce_sg = NULL;
349
350 kfree(mqrq_prev->sg);
351 mqrq_prev->sg = NULL;
352
353 kfree(mqrq_prev->bounce_buf);
354 mqrq_prev->bounce_buf = NULL;
355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 mq->card = NULL;
357}
358EXPORT_SYMBOL(mmc_cleanup_queue);
359
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900360int mmc_packed_init(struct mmc_queue *mq, struct mmc_card *card)
361{
362 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
363 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
364 int ret = 0;
365
366
367 mqrq_cur->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
368 if (!mqrq_cur->packed) {
369 pr_warn("%s: unable to allocate packed cmd for mqrq_cur\n",
370 mmc_card_name(card));
371 ret = -ENOMEM;
372 goto out;
373 }
374
375 mqrq_prev->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
376 if (!mqrq_prev->packed) {
377 pr_warn("%s: unable to allocate packed cmd for mqrq_prev\n",
378 mmc_card_name(card));
379 kfree(mqrq_cur->packed);
380 mqrq_cur->packed = NULL;
381 ret = -ENOMEM;
382 goto out;
383 }
384
385 INIT_LIST_HEAD(&mqrq_cur->packed->list);
386 INIT_LIST_HEAD(&mqrq_prev->packed->list);
387
388out:
389 return ret;
390}
391
392void mmc_packed_clean(struct mmc_queue *mq)
393{
394 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
395 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
396
397 kfree(mqrq_cur->packed);
398 mqrq_cur->packed = NULL;
399 kfree(mqrq_prev->packed);
400 mqrq_prev->packed = NULL;
401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/**
404 * mmc_queue_suspend - suspend a MMC request queue
405 * @mq: MMC queue to suspend
406 *
407 * Stop the block request queue, and wait for our thread to
408 * complete any outstanding requests. This ensures that we
409 * won't suspend while a request is being processed.
410 */
411void mmc_queue_suspend(struct mmc_queue *mq)
412{
Jens Axboe165125e2007-07-24 09:28:11 +0200413 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 unsigned long flags;
415
416 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
417 mq->flags |= MMC_QUEUE_SUSPENDED;
418
419 spin_lock_irqsave(q->queue_lock, flags);
420 blk_stop_queue(q);
421 spin_unlock_irqrestore(q->queue_lock, flags);
422
423 down(&mq->thread_sem);
424 }
425}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
427/**
428 * mmc_queue_resume - resume a previously suspended MMC request queue
429 * @mq: MMC queue to resume
430 */
431void mmc_queue_resume(struct mmc_queue *mq)
432{
Jens Axboe165125e2007-07-24 09:28:11 +0200433 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 unsigned long flags;
435
436 if (mq->flags & MMC_QUEUE_SUSPENDED) {
437 mq->flags &= ~MMC_QUEUE_SUSPENDED;
438
439 up(&mq->thread_sem);
440
441 spin_lock_irqsave(q->queue_lock, flags);
442 blk_start_queue(q);
443 spin_unlock_irqrestore(q->queue_lock, flags);
444 }
445}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100446
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900447static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
448 struct mmc_packed *packed,
449 struct scatterlist *sg,
450 enum mmc_packed_type cmd_type)
451{
452 struct scatterlist *__sg = sg;
453 unsigned int sg_len = 0;
454 struct request *req;
455
456 if (mmc_packed_wr(cmd_type)) {
457 unsigned int hdr_sz = mmc_large_sector(mq->card) ? 4096 : 512;
458 unsigned int max_seg_sz = queue_max_segment_size(mq->queue);
459 unsigned int len, remain, offset = 0;
460 u8 *buf = (u8 *)packed->cmd_hdr;
461
462 remain = hdr_sz;
463 do {
464 len = min(remain, max_seg_sz);
465 sg_set_buf(__sg, buf + offset, len);
466 offset += len;
467 remain -= len;
Dan Williamsda81ed12015-08-07 18:15:14 +0200468 sg_unmark_end(__sg++);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900469 sg_len++;
470 } while (remain);
471 }
472
473 list_for_each_entry(req, &packed->list, queuelist) {
474 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
475 __sg = sg + (sg_len - 1);
Dan Williamsda81ed12015-08-07 18:15:14 +0200476 sg_unmark_end(__sg++);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900477 }
478 sg_mark_end(sg + (sg_len - 1));
479 return sg_len;
480}
481
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200482/*
483 * Prepare the sg list(s) to be handed of to the host driver
484 */
Per Forlin97868a22011-07-09 17:12:36 -0400485unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200486{
487 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200488 size_t buflen;
489 struct scatterlist *sg;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900490 enum mmc_packed_type cmd_type;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200491 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200492
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900493 cmd_type = mqrq->cmd_type;
494
495 if (!mqrq->bounce_buf) {
496 if (mmc_packed_cmd(cmd_type))
497 return mmc_queue_packed_map_sg(mq, mqrq->packed,
498 mqrq->sg, cmd_type);
499 else
500 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
501 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200502
Per Forlin97868a22011-07-09 17:12:36 -0400503 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200504
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900505 if (mmc_packed_cmd(cmd_type))
506 sg_len = mmc_queue_packed_map_sg(mq, mqrq->packed,
507 mqrq->bounce_sg, cmd_type);
508 else
509 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200510
Per Forlin97868a22011-07-09 17:12:36 -0400511 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200512
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200513 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400514 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200515 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200516
Per Forlin97868a22011-07-09 17:12:36 -0400517 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200518
519 return 1;
520}
521
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200522/*
523 * If writing, bounce the data to the buffer before the request
524 * is sent to the host driver
525 */
Per Forlin97868a22011-07-09 17:12:36 -0400526void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200527{
Per Forlin97868a22011-07-09 17:12:36 -0400528 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200529 return;
530
Per Forlin97868a22011-07-09 17:12:36 -0400531 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200532 return;
533
Per Forlin97868a22011-07-09 17:12:36 -0400534 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
535 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200536}
537
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200538/*
539 * If reading, bounce the data from the buffer after the request
540 * has been handled by the host driver
541 */
Per Forlin97868a22011-07-09 17:12:36 -0400542void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200543{
Per Forlin97868a22011-07-09 17:12:36 -0400544 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200545 return;
546
Per Forlin97868a22011-07-09 17:12:36 -0400547 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200548 return;
549
Per Forlin97868a22011-07-09 17:12:36 -0400550 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
551 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200552}