blob: 6f4323c6d6536c8855ca2a8bef8f8087bb5d252e [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 */
Adrian Hunterbd788c92010-08-11 14:17:47 -070036 if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_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;
Seungwon Jeon369d3212012-12-26 10:40:17 +090059 unsigned int cmd_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 spin_lock_irq(q->queue_lock);
62 set_current_state(TASK_INTERRUPTIBLE);
Jens Axboe7eaceac2011-03-10 08:52:07 +010063 req = blk_fetch_request(q);
Per Forlin97868a22011-07-09 17:12:36 -040064 mq->mqrq_cur->req = req;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 spin_unlock_irq(q->queue_lock);
66
Per Forlinee8a43a2011-07-01 18:55:33 +020067 if (req || mq->mqrq_prev->req) {
68 set_current_state(TASK_RUNNING);
Seungwon Jeon369d3212012-12-26 10:40:17 +090069 cmd_flags = req ? req->cmd_flags : 0;
Per Forlinee8a43a2011-07-01 18:55:33 +020070 mq->issue_fn(mq, req);
Rabin Vincenta8c27c02015-06-14 19:26:11 +020071 cond_resched();
Konstantin Dorfman2220eed2013-01-14 14:28:17 -050072 if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
73 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
74 continue; /* fetch again */
75 }
Seungwon Jeon45c5a912012-09-28 19:12:53 +090076
77 /*
78 * Current request becomes previous request
79 * and vice versa.
Seungwon Jeon369d3212012-12-26 10:40:17 +090080 * In case of special requests, current request
81 * has been finished. Do not assign it to previous
82 * request.
Seungwon Jeon45c5a912012-09-28 19:12:53 +090083 */
Seungwon Jeon369d3212012-12-26 10:40:17 +090084 if (cmd_flags & MMC_REQ_SPECIAL_MASK)
85 mq->mqrq_cur->req = NULL;
86
Seungwon Jeon45c5a912012-09-28 19:12:53 +090087 mq->mqrq_prev->brq.mrq.data = NULL;
88 mq->mqrq_prev->req = NULL;
Fabian Frederick75518472015-06-10 18:30:53 +020089 swap(mq->mqrq_prev, mq->mqrq_cur);
Per Forlinee8a43a2011-07-01 18:55:33 +020090 } else {
Vitaly Wool7b30d282006-12-07 20:08:02 +010091 if (kthread_should_stop()) {
92 set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 break;
Vitaly Wool7b30d282006-12-07 20:08:02 +010094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 up(&mq->thread_sem);
96 schedule();
97 down(&mq->thread_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 } while (1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 up(&mq->thread_sem);
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return 0;
103}
104
105/*
106 * Generic MMC request handler. This is called for any queue on a
107 * particular host. When the host is not busy, we look for a request
108 * on any queue on this host, and attempt to issue it. This may
109 * not be the queue we were asked to process.
110 */
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530111static void mmc_request_fn(struct request_queue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct mmc_queue *mq = q->queuedata;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100114 struct request *req;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500115 unsigned long flags;
116 struct mmc_context_info *cntx;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100117
118 if (!mq) {
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800119 while ((req = blk_fetch_request(q)) != NULL) {
120 req->cmd_flags |= REQ_QUIET;
Tejun Heo296b2f62009-05-08 11:54:15 +0900121 __blk_end_request_all(req, -EIO);
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800122 }
Pierre Ossman89b4e132006-11-14 22:08:16 +0100123 return;
124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500126 cntx = &mq->card->host->context_info;
127 if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
128 /*
129 * New MMC request arrived when MMC thread may be
130 * blocked on the previous request to be complete
131 * with no current request fetched
132 */
133 spin_lock_irqsave(&cntx->lock, flags);
134 if (cntx->is_waiting_last_req) {
135 cntx->is_new_req = true;
136 wake_up_interruptible(&cntx->wait);
137 }
138 spin_unlock_irqrestore(&cntx->lock, flags);
139 } else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
Christoph Hellwig87598a22006-11-13 20:23:52 +0100140 wake_up_process(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
Venkatraman S7513cd72011-08-23 21:16:02 +0530143static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
Per Forlin97868a22011-07-09 17:12:36 -0400144{
145 struct scatterlist *sg;
146
147 sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
148 if (!sg)
149 *err = -ENOMEM;
150 else {
151 *err = 0;
152 sg_init_table(sg, sg_len);
153 }
154
155 return sg;
156}
157
Adrian Huntere056a1b2011-06-28 17:16:02 +0300158static void mmc_queue_setup_discard(struct request_queue *q,
159 struct mmc_card *card)
160{
161 unsigned max_discard;
162
163 max_discard = mmc_calc_max_discard(card);
164 if (!max_discard)
165 return;
166
167 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
Jens Axboe2bb4cd52015-07-14 08:15:12 -0600168 blk_queue_max_discard_sectors(q, max_discard);
Adrian Hunter7194efb2012-04-05 14:45:47 +0300169 if (card->erased_byte == 0 && !mmc_can_discard(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300170 q->limits.discard_zeroes_data = 1;
171 q->limits.discard_granularity = card->pref_erase << 9;
172 /* granularity must not be greater than max. discard */
173 if (card->pref_erase > max_discard)
174 q->limits.discard_granularity = 0;
Maya Erez775a9362013-04-18 15:41:55 +0300175 if (mmc_can_secure_erase_trim(card))
Adrian Huntere056a1b2011-06-28 17:16:02 +0300176 queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/**
180 * mmc_init_queue - initialise a queue structure.
181 * @mq: mmc queue
182 * @card: mmc card to attach this queue
183 * @lock: queue lock
Adrian Hunterd09408a2011-06-23 13:40:28 +0300184 * @subname: partition subname
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 *
186 * Initialise a MMC card request queue.
187 */
Adrian Hunterd09408a2011-06-23 13:40:28 +0300188int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
189 spinlock_t *lock, const char *subname)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct mmc_host *host = card->host;
192 u64 limit = BLK_BOUNCE_HIGH;
193 int ret;
Per Forlin97868a22011-07-09 17:12:36 -0400194 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
Per Forlin04296b72011-07-01 18:55:31 +0200195 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Greg Kroah-Hartmanfcaf71f2006-09-12 17:00:10 +0200197 if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
Russell Kinge83b3662014-02-11 17:11:04 +0000198 limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 mq->card = card;
Venkatraman S1b50f5f2012-04-13 17:54:11 +0530201 mq->queue = blk_init_queue(mmc_request_fn, lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (!mq->queue)
203 return -ENOMEM;
204
Per Forlin97868a22011-07-09 17:12:36 -0400205 mq->mqrq_cur = mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200206 mq->mqrq_prev = mqrq_prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 mq->queue->queuedata = mq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Pierre Ossman98ccf142007-05-12 00:26:16 +0200209 blk_queue_prep_rq(mq->queue, mmc_prep_request);
Pierre Ossman8dddfe12008-10-14 20:04:46 +0200210 queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
Mike Snitzerb277da02014-10-04 10:55:32 -0600211 queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
Adrian Huntere056a1b2011-06-28 17:16:02 +0300212 if (mmc_can_erase(card))
213 mmc_queue_setup_discard(mq->queue, card);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200214
215#ifdef CONFIG_MMC_BLOCK_BOUNCE
Martin K. Petersena36274e2010-09-10 01:33:59 -0400216 if (host->max_segs == 1) {
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200217 unsigned int bouncesz;
218
Pierre Ossman98ccf142007-05-12 00:26:16 +0200219 bouncesz = MMC_QUEUE_BOUNCESZ;
220
221 if (bouncesz > host->max_req_size)
222 bouncesz = host->max_req_size;
223 if (bouncesz > host->max_seg_size)
224 bouncesz = host->max_seg_size;
Pierre Ossmanf3eb0aaa2008-08-16 21:34:02 +0200225 if (bouncesz > (host->max_blk_count * 512))
226 bouncesz = host->max_blk_count * 512;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200227
Pierre Ossmanf3eb0aaa2008-08-16 21:34:02 +0200228 if (bouncesz > 512) {
Per Forlin97868a22011-07-09 17:12:36 -0400229 mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
230 if (!mqrq_cur->bounce_buf) {
Joe Perches66061102014-09-12 14:56:56 -0700231 pr_warn("%s: unable to allocate bounce cur buffer\n",
Pierre Ossmanf3eb0aaa2008-08-16 21:34:02 +0200232 mmc_card_name(card));
Bhuvanesh Suracharifdb409f2014-12-01 02:23:02 -0500233 } else {
234 mqrq_prev->bounce_buf =
235 kmalloc(bouncesz, GFP_KERNEL);
236 if (!mqrq_prev->bounce_buf) {
237 pr_warn("%s: unable to allocate bounce prev buffer\n",
238 mmc_card_name(card));
239 kfree(mqrq_cur->bounce_buf);
240 mqrq_cur->bounce_buf = NULL;
241 }
Per Forlin04296b72011-07-01 18:55:31 +0200242 }
Pierre Ossmanf3eb0aaa2008-08-16 21:34:02 +0200243 }
244
Per Forlin04296b72011-07-01 18:55:31 +0200245 if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200246 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500247 blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
Martin K. Petersen8a783622010-02-26 00:20:39 -0500248 blk_queue_max_segments(mq->queue, bouncesz / 512);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200249 blk_queue_max_segment_size(mq->queue, bouncesz);
250
Per Forlin97868a22011-07-09 17:12:36 -0400251 mqrq_cur->sg = mmc_alloc_sg(1, &ret);
252 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200253 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200254
Per Forlin97868a22011-07-09 17:12:36 -0400255 mqrq_cur->bounce_sg =
256 mmc_alloc_sg(bouncesz / 512, &ret);
257 if (ret)
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200258 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400259
Per Forlin04296b72011-07-01 18:55:31 +0200260 mqrq_prev->sg = mmc_alloc_sg(1, &ret);
261 if (ret)
262 goto cleanup_queue;
263
264 mqrq_prev->bounce_sg =
265 mmc_alloc_sg(bouncesz / 512, &ret);
266 if (ret)
267 goto cleanup_queue;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200268 }
269 }
270#endif
271
Per Forlin04296b72011-07-01 18:55:31 +0200272 if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
Pierre Ossman98ccf142007-05-12 00:26:16 +0200273 blk_queue_bounce_limit(mq->queue, limit);
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500274 blk_queue_max_hw_sectors(mq->queue,
Pierre Ossmanf3eb0aaa2008-08-16 21:34:02 +0200275 min(host->max_blk_count, host->max_req_size / 512));
Martin K. Petersena36274e2010-09-10 01:33:59 -0400276 blk_queue_max_segments(mq->queue, host->max_segs);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200277 blk_queue_max_segment_size(mq->queue, host->max_seg_size);
278
Per Forlin97868a22011-07-09 17:12:36 -0400279 mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
280 if (ret)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200281 goto cleanup_queue;
Per Forlin97868a22011-07-09 17:12:36 -0400282
Per Forlin04296b72011-07-01 18:55:31 +0200283
284 mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
285 if (ret)
286 goto cleanup_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Thomas Gleixner632cf922010-09-14 07:12:35 -0400289 sema_init(&mq->thread_sem, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
Adrian Hunterd09408a2011-06-23 13:40:28 +0300291 mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
292 host->index, subname ? subname : "");
Ethan Dude528fa2010-09-30 18:40:27 -0400293
Christoph Hellwig87598a22006-11-13 20:23:52 +0100294 if (IS_ERR(mq->thread)) {
295 ret = PTR_ERR(mq->thread);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200296 goto free_bounce_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 }
298
Christoph Hellwig87598a22006-11-13 20:23:52 +0100299 return 0;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200300 free_bounce_sg:
Per Forlin97868a22011-07-09 17:12:36 -0400301 kfree(mqrq_cur->bounce_sg);
302 mqrq_cur->bounce_sg = NULL;
Per Forlin04296b72011-07-01 18:55:31 +0200303 kfree(mqrq_prev->bounce_sg);
304 mqrq_prev->bounce_sg = NULL;
Per Forlin97868a22011-07-09 17:12:36 -0400305
Pierre Ossmanaafabfa2007-08-09 14:28:02 +0200306 cleanup_queue:
Per Forlin97868a22011-07-09 17:12:36 -0400307 kfree(mqrq_cur->sg);
308 mqrq_cur->sg = NULL;
309 kfree(mqrq_cur->bounce_buf);
310 mqrq_cur->bounce_buf = NULL;
311
Per Forlin04296b72011-07-01 18:55:31 +0200312 kfree(mqrq_prev->sg);
313 mqrq_prev->sg = NULL;
314 kfree(mqrq_prev->bounce_buf);
315 mqrq_prev->bounce_buf = NULL;
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 blk_cleanup_queue(mq->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return ret;
319}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321void mmc_cleanup_queue(struct mmc_queue *mq)
322{
Jens Axboe165125e2007-07-24 09:28:11 +0200323 struct request_queue *q = mq->queue;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100324 unsigned long flags;
Per Forlin97868a22011-07-09 17:12:36 -0400325 struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
Per Forlin04296b72011-07-01 18:55:31 +0200326 struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
Pierre Ossman89b4e132006-11-14 22:08:16 +0100327
Pierre Ossmand2b46f62007-04-28 16:52:12 +0200328 /* Make sure the queue isn't suspended, as that will deadlock */
329 mmc_queue_resume(mq);
330
Pierre Ossman89b4e132006-11-14 22:08:16 +0100331 /* Then terminate our worker thread */
Christoph Hellwig87598a22006-11-13 20:23:52 +0100332 kthread_stop(mq->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Adrian Hunter5fa83ce2010-01-08 14:43:00 -0800334 /* Empty the queue */
335 spin_lock_irqsave(q->queue_lock, flags);
336 q->queuedata = NULL;
337 blk_start_queue(q);
338 spin_unlock_irqrestore(q->queue_lock, flags);
339
Per Forlin97868a22011-07-09 17:12:36 -0400340 kfree(mqrq_cur->bounce_sg);
341 mqrq_cur->bounce_sg = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200342
Per Forlin97868a22011-07-09 17:12:36 -0400343 kfree(mqrq_cur->sg);
344 mqrq_cur->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Per Forlin97868a22011-07-09 17:12:36 -0400346 kfree(mqrq_cur->bounce_buf);
347 mqrq_cur->bounce_buf = NULL;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200348
Per Forlin04296b72011-07-01 18:55:31 +0200349 kfree(mqrq_prev->bounce_sg);
350 mqrq_prev->bounce_sg = NULL;
351
352 kfree(mqrq_prev->sg);
353 mqrq_prev->sg = NULL;
354
355 kfree(mqrq_prev->bounce_buf);
356 mqrq_prev->bounce_buf = NULL;
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 mq->card = NULL;
359}
360EXPORT_SYMBOL(mmc_cleanup_queue);
361
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900362int mmc_packed_init(struct mmc_queue *mq, struct mmc_card *card)
363{
364 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
365 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
366 int ret = 0;
367
368
369 mqrq_cur->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
370 if (!mqrq_cur->packed) {
371 pr_warn("%s: unable to allocate packed cmd for mqrq_cur\n",
372 mmc_card_name(card));
373 ret = -ENOMEM;
374 goto out;
375 }
376
377 mqrq_prev->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
378 if (!mqrq_prev->packed) {
379 pr_warn("%s: unable to allocate packed cmd for mqrq_prev\n",
380 mmc_card_name(card));
381 kfree(mqrq_cur->packed);
382 mqrq_cur->packed = NULL;
383 ret = -ENOMEM;
384 goto out;
385 }
386
387 INIT_LIST_HEAD(&mqrq_cur->packed->list);
388 INIT_LIST_HEAD(&mqrq_prev->packed->list);
389
390out:
391 return ret;
392}
393
394void mmc_packed_clean(struct mmc_queue *mq)
395{
396 struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
397 struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
398
399 kfree(mqrq_cur->packed);
400 mqrq_cur->packed = NULL;
401 kfree(mqrq_prev->packed);
402 mqrq_prev->packed = NULL;
403}
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405/**
406 * mmc_queue_suspend - suspend a MMC request queue
407 * @mq: MMC queue to suspend
408 *
409 * Stop the block request queue, and wait for our thread to
410 * complete any outstanding requests. This ensures that we
411 * won't suspend while a request is being processed.
412 */
413void mmc_queue_suspend(struct mmc_queue *mq)
414{
Jens Axboe165125e2007-07-24 09:28:11 +0200415 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 unsigned long flags;
417
418 if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
419 mq->flags |= MMC_QUEUE_SUSPENDED;
420
421 spin_lock_irqsave(q->queue_lock, flags);
422 blk_stop_queue(q);
423 spin_unlock_irqrestore(q->queue_lock, flags);
424
425 down(&mq->thread_sem);
426 }
427}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429/**
430 * mmc_queue_resume - resume a previously suspended MMC request queue
431 * @mq: MMC queue to resume
432 */
433void mmc_queue_resume(struct mmc_queue *mq)
434{
Jens Axboe165125e2007-07-24 09:28:11 +0200435 struct request_queue *q = mq->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unsigned long flags;
437
438 if (mq->flags & MMC_QUEUE_SUSPENDED) {
439 mq->flags &= ~MMC_QUEUE_SUSPENDED;
440
441 up(&mq->thread_sem);
442
443 spin_lock_irqsave(q->queue_lock, flags);
444 blk_start_queue(q);
445 spin_unlock_irqrestore(q->queue_lock, flags);
446 }
447}
Pierre Ossman98ac2162006-12-23 20:03:02 +0100448
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900449static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
450 struct mmc_packed *packed,
451 struct scatterlist *sg,
452 enum mmc_packed_type cmd_type)
453{
454 struct scatterlist *__sg = sg;
455 unsigned int sg_len = 0;
456 struct request *req;
457
458 if (mmc_packed_wr(cmd_type)) {
459 unsigned int hdr_sz = mmc_large_sector(mq->card) ? 4096 : 512;
460 unsigned int max_seg_sz = queue_max_segment_size(mq->queue);
461 unsigned int len, remain, offset = 0;
462 u8 *buf = (u8 *)packed->cmd_hdr;
463
464 remain = hdr_sz;
465 do {
466 len = min(remain, max_seg_sz);
467 sg_set_buf(__sg, buf + offset, len);
468 offset += len;
469 remain -= len;
Dan Williamsda81ed12015-08-07 18:15:14 +0200470 sg_unmark_end(__sg++);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900471 sg_len++;
472 } while (remain);
473 }
474
475 list_for_each_entry(req, &packed->list, queuelist) {
476 sg_len += blk_rq_map_sg(mq->queue, req, __sg);
477 __sg = sg + (sg_len - 1);
Dan Williamsda81ed12015-08-07 18:15:14 +0200478 sg_unmark_end(__sg++);
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900479 }
480 sg_mark_end(sg + (sg_len - 1));
481 return sg_len;
482}
483
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200484/*
485 * Prepare the sg list(s) to be handed of to the host driver
486 */
Per Forlin97868a22011-07-09 17:12:36 -0400487unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200488{
489 unsigned int sg_len;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200490 size_t buflen;
491 struct scatterlist *sg;
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900492 enum mmc_packed_type cmd_type;
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200493 int i;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200494
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900495 cmd_type = mqrq->cmd_type;
496
497 if (!mqrq->bounce_buf) {
498 if (mmc_packed_cmd(cmd_type))
499 return mmc_queue_packed_map_sg(mq, mqrq->packed,
500 mqrq->sg, cmd_type);
501 else
502 return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
503 }
Pierre Ossman98ccf142007-05-12 00:26:16 +0200504
Per Forlin97868a22011-07-09 17:12:36 -0400505 BUG_ON(!mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200506
Seungwon Jeonce39f9d2013-02-06 17:02:46 +0900507 if (mmc_packed_cmd(cmd_type))
508 sg_len = mmc_queue_packed_map_sg(mq, mqrq->packed,
509 mqrq->bounce_sg, cmd_type);
510 else
511 sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200512
Per Forlin97868a22011-07-09 17:12:36 -0400513 mqrq->bounce_sg_len = sg_len;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200514
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200515 buflen = 0;
Per Forlin97868a22011-07-09 17:12:36 -0400516 for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200517 buflen += sg->length;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200518
Per Forlin97868a22011-07-09 17:12:36 -0400519 sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200520
521 return 1;
522}
523
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200524/*
525 * If writing, bounce the data to the buffer before the request
526 * is sent to the host driver
527 */
Per Forlin97868a22011-07-09 17:12:36 -0400528void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200529{
Per Forlin97868a22011-07-09 17:12:36 -0400530 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200531 return;
532
Per Forlin97868a22011-07-09 17:12:36 -0400533 if (rq_data_dir(mqrq->req) != WRITE)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200534 return;
535
Per Forlin97868a22011-07-09 17:12:36 -0400536 sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
537 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200538}
539
Pierre Ossman2ff1fa62008-07-22 14:35:42 +0200540/*
541 * If reading, bounce the data from the buffer after the request
542 * has been handled by the host driver
543 */
Per Forlin97868a22011-07-09 17:12:36 -0400544void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200545{
Per Forlin97868a22011-07-09 17:12:36 -0400546 if (!mqrq->bounce_buf)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200547 return;
548
Per Forlin97868a22011-07-09 17:12:36 -0400549 if (rq_data_dir(mqrq->req) != READ)
Pierre Ossman98ccf142007-05-12 00:26:16 +0200550 return;
551
Per Forlin97868a22011-07-09 17:12:36 -0400552 sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
553 mqrq->bounce_buf, mqrq->sg[0].length);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200554}