queue.c 11.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
P
Pierre Ossman 已提交
2
 *  linux/drivers/mmc/card/queue.c
L
Linus Torvalds 已提交
3 4
 *
 *  Copyright (C) 2003 Russell King, All Rights Reserved.
5
 *  Copyright 2006-2007 Pierre Ossman
L
Linus Torvalds 已提交
6 7 8 9 10 11
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 */
12
#include <linux/slab.h>
L
Linus Torvalds 已提交
13 14
#include <linux/module.h>
#include <linux/blkdev.h>
15
#include <linux/freezer.h>
C
Christoph Hellwig 已提交
16
#include <linux/kthread.h>
J
Jens Axboe 已提交
17
#include <linux/scatterlist.h>
18
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
19 20 21

#include <linux/mmc/card.h>
#include <linux/mmc/host.h>
22

23
#include "queue.h"
24
#include "block.h"
L
Linus Torvalds 已提交
25

26 27
#define MMC_QUEUE_BOUNCESZ	65536

L
Linus Torvalds 已提交
28
/*
29
 * Prepare a MMC request. This just filters out odd stuff.
L
Linus Torvalds 已提交
30 31 32
 */
static int mmc_prep_request(struct request_queue *q, struct request *req)
{
33 34
	struct mmc_queue *mq = q->queuedata;

35
	/*
A
Adrian Hunter 已提交
36
	 * We only like normal block requests and discards.
37
	 */
A
Adrian Hunter 已提交
38 39
	if (req->cmd_type != REQ_TYPE_FS && req_op(req) != REQ_OP_DISCARD &&
	    req_op(req) != REQ_OP_SECURE_ERASE) {
L
Linus Torvalds 已提交
40
		blk_dump_rq_flags(req, "MMC bad request");
41
		return BLKPREP_KILL;
L
Linus Torvalds 已提交
42 43
	}

44
	if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
45 46
		return BLKPREP_KILL;

47
	req->cmd_flags |= REQ_DONTPREP;
L
Linus Torvalds 已提交
48

49
	return BLKPREP_OK;
L
Linus Torvalds 已提交
50 51 52 53 54 55
}

static int mmc_queue_thread(void *d)
{
	struct mmc_queue *mq = d;
	struct request_queue *q = mq->queue;
56
	struct mmc_context_info *cntx = &mq->card->host->context_info;
L
Linus Torvalds 已提交
57

58
	current->flags |= PF_MEMALLOC;
L
Linus Torvalds 已提交
59 60 61 62 63 64 65

	down(&mq->thread_sem);
	do {
		struct request *req = NULL;

		spin_lock_irq(q->queue_lock);
		set_current_state(TASK_INTERRUPTIBLE);
J
Jens Axboe 已提交
66
		req = blk_fetch_request(q);
67 68 69 70 71 72 73 74 75 76 77 78 79
		mq->asleep = false;
		cntx->is_waiting_last_req = false;
		cntx->is_new_req = false;
		if (!req) {
			/*
			 * Dispatch queue is empty so set flags for
			 * mmc_request_fn() to wake us up.
			 */
			if (mq->mqrq_prev->req)
				cntx->is_waiting_last_req = true;
			else
				mq->asleep = true;
		}
80
		mq->mqrq_cur->req = req;
L
Linus Torvalds 已提交
81 82
		spin_unlock_irq(q->queue_lock);

83
		if (req || mq->mqrq_prev->req) {
84 85
			bool req_is_special = mmc_req_is_special(req);

86
			set_current_state(TASK_RUNNING);
87
			mmc_blk_issue_rq(mq, req);
88
			cond_resched();
89 90 91 92
			if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
				mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
				continue; /* fetch again */
			}
93 94 95 96

			/*
			 * Current request becomes previous request
			 * and vice versa.
97 98 99
			 * In case of special requests, current request
			 * has been finished. Do not assign it to previous
			 * request.
100
			 */
101
			if (req_is_special)
102 103
				mq->mqrq_cur->req = NULL;

104 105
			mq->mqrq_prev->brq.mrq.data = NULL;
			mq->mqrq_prev->req = NULL;
106
			swap(mq->mqrq_prev, mq->mqrq_cur);
107
		} else {
108 109
			if (kthread_should_stop()) {
				set_current_state(TASK_RUNNING);
L
Linus Torvalds 已提交
110
				break;
111
			}
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
			up(&mq->thread_sem);
			schedule();
			down(&mq->thread_sem);
		}
	} while (1);
	up(&mq->thread_sem);

	return 0;
}

/*
 * Generic MMC request handler.  This is called for any queue on a
 * particular host.  When the host is not busy, we look for a request
 * on any queue on this host, and attempt to issue it.  This may
 * not be the queue we were asked to process.
 */
128
static void mmc_request_fn(struct request_queue *q)
L
Linus Torvalds 已提交
129 130
{
	struct mmc_queue *mq = q->queuedata;
131
	struct request *req;
132
	struct mmc_context_info *cntx;
133 134

	if (!mq) {
A
Adrian Hunter 已提交
135 136
		while ((req = blk_fetch_request(q)) != NULL) {
			req->cmd_flags |= REQ_QUIET;
137
			__blk_end_request_all(req, -EIO);
A
Adrian Hunter 已提交
138
		}
139 140
		return;
	}
L
Linus Torvalds 已提交
141

142
	cntx = &mq->card->host->context_info;
143 144 145 146 147 148 149

	if (cntx->is_waiting_last_req) {
		cntx->is_new_req = true;
		wake_up_interruptible(&cntx->wait);
	}

	if (mq->asleep)
C
Christoph Hellwig 已提交
150
		wake_up_process(mq->thread);
L
Linus Torvalds 已提交
151 152
}

153
static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
154 155 156 157 158 159 160 161 162 163 164 165 166 167
{
	struct scatterlist *sg;

	sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
	if (!sg)
		*err = -ENOMEM;
	else {
		*err = 0;
		sg_init_table(sg, sg_len);
	}

	return sg;
}

168 169 170 171 172 173 174 175 176 177
static void mmc_queue_setup_discard(struct request_queue *q,
				    struct mmc_card *card)
{
	unsigned max_discard;

	max_discard = mmc_calc_max_discard(card);
	if (!max_discard)
		return;

	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
178
	blk_queue_max_discard_sectors(q, max_discard);
179
	if (card->erased_byte == 0 && !mmc_can_discard(card))
180 181 182 183 184
		q->limits.discard_zeroes_data = 1;
	q->limits.discard_granularity = card->pref_erase << 9;
	/* granularity must not be greater than max. discard */
	if (card->pref_erase > max_discard)
		q->limits.discard_granularity = 0;
185
	if (mmc_can_secure_erase_trim(card))
186
		queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
187 188
}

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
#ifdef CONFIG_MMC_BLOCK_BOUNCE
static bool mmc_queue_alloc_bounce_bufs(struct mmc_queue *mq,
					unsigned int bouncesz)
{
	struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
	struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;

	mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
	if (!mqrq_cur->bounce_buf) {
		pr_warn("%s: unable to allocate bounce cur buffer\n",
			mmc_card_name(mq->card));
		return false;
	}

	mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
	if (!mqrq_prev->bounce_buf) {
		pr_warn("%s: unable to allocate bounce prev buffer\n",
			mmc_card_name(mq->card));
		kfree(mqrq_cur->bounce_buf);
		mqrq_cur->bounce_buf = NULL;
		return false;
	}

	return true;
}
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

static int mmc_queue_alloc_bounce_sgs(struct mmc_queue *mq,
				      unsigned int bouncesz)
{
	struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
	struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
	int ret;

	mqrq_cur->sg = mmc_alloc_sg(1, &ret);
	if (ret)
		return ret;

	mqrq_cur->bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret);
	if (ret)
		return ret;

	mqrq_prev->sg = mmc_alloc_sg(1, &ret);
	if (ret)
		return ret;

	mqrq_prev->bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret);

	return ret;
}
238 239
#endif

L
Linus Torvalds 已提交
240 241 242 243 244
/**
 * mmc_init_queue - initialise a queue structure.
 * @mq: mmc queue
 * @card: mmc card to attach this queue
 * @lock: queue lock
245
 * @subname: partition subname
L
Linus Torvalds 已提交
246 247 248
 *
 * Initialise a MMC card request queue.
 */
249 250
int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
		   spinlock_t *lock, const char *subname)
L
Linus Torvalds 已提交
251 252 253
{
	struct mmc_host *host = card->host;
	u64 limit = BLK_BOUNCE_HIGH;
254
	bool bounce = false;
L
Linus Torvalds 已提交
255
	int ret;
256
	struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
257
	struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
L
Linus Torvalds 已提交
258

259
	if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
260
		limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
L
Linus Torvalds 已提交
261 262

	mq->card = card;
263
	mq->queue = blk_init_queue(mmc_request_fn, lock);
L
Linus Torvalds 已提交
264 265 266
	if (!mq->queue)
		return -ENOMEM;

267
	mq->mqrq_cur = mqrq_cur;
268
	mq->mqrq_prev = mqrq_prev;
L
Linus Torvalds 已提交
269 270
	mq->queue->queuedata = mq;

271
	blk_queue_prep_rq(mq->queue, mmc_prep_request);
272
	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
273
	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
274 275
	if (mmc_can_erase(card))
		mmc_queue_setup_discard(mq->queue, card);
276 277

#ifdef CONFIG_MMC_BLOCK_BOUNCE
278
	if (host->max_segs == 1) {
279 280
		unsigned int bouncesz;

281 282 283 284 285 286
		bouncesz = MMC_QUEUE_BOUNCESZ;

		if (bouncesz > host->max_req_size)
			bouncesz = host->max_req_size;
		if (bouncesz > host->max_seg_size)
			bouncesz = host->max_seg_size;
287 288 289
		if (bouncesz > (host->max_blk_count * 512))
			bouncesz = host->max_blk_count * 512;

290 291
		if (bouncesz > 512 &&
		    mmc_queue_alloc_bounce_bufs(mq, bouncesz)) {
292
			blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
293
			blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
294
			blk_queue_max_segments(mq->queue, bouncesz / 512);
295 296
			blk_queue_max_segment_size(mq->queue, bouncesz);

297
			ret = mmc_queue_alloc_bounce_sgs(mq, bouncesz);
298 299
			if (ret)
				goto cleanup_queue;
300
			bounce = true;
301 302 303 304
		}
	}
#endif

305
	if (!bounce) {
306
		blk_queue_bounce_limit(mq->queue, limit);
307
		blk_queue_max_hw_sectors(mq->queue,
308
			min(host->max_blk_count, host->max_req_size / 512));
309
		blk_queue_max_segments(mq->queue, host->max_segs);
310 311
		blk_queue_max_segment_size(mq->queue, host->max_seg_size);

312 313
		mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
		if (ret)
314
			goto cleanup_queue;
315

316 317 318 319

		mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
		if (ret)
			goto cleanup_queue;
L
Linus Torvalds 已提交
320 321
	}

322
	sema_init(&mq->thread_sem, 1);
L
Linus Torvalds 已提交
323

324 325
	mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
		host->index, subname ? subname : "");
326

C
Christoph Hellwig 已提交
327 328
	if (IS_ERR(mq->thread)) {
		ret = PTR_ERR(mq->thread);
329
		goto free_bounce_sg;
L
Linus Torvalds 已提交
330 331
	}

C
Christoph Hellwig 已提交
332
	return 0;
333
 free_bounce_sg:
334 335
	kfree(mqrq_cur->bounce_sg);
	mqrq_cur->bounce_sg = NULL;
336 337
	kfree(mqrq_prev->bounce_sg);
	mqrq_prev->bounce_sg = NULL;
338

339
 cleanup_queue:
340 341 342 343 344
	kfree(mqrq_cur->sg);
	mqrq_cur->sg = NULL;
	kfree(mqrq_cur->bounce_buf);
	mqrq_cur->bounce_buf = NULL;

345 346 347 348 349
	kfree(mqrq_prev->sg);
	mqrq_prev->sg = NULL;
	kfree(mqrq_prev->bounce_buf);
	mqrq_prev->bounce_buf = NULL;

L
Linus Torvalds 已提交
350 351 352 353 354 355
	blk_cleanup_queue(mq->queue);
	return ret;
}

void mmc_cleanup_queue(struct mmc_queue *mq)
{
356
	struct request_queue *q = mq->queue;
357
	unsigned long flags;
358
	struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
359
	struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
360

361 362 363
	/* Make sure the queue isn't suspended, as that will deadlock */
	mmc_queue_resume(mq);

364
	/* Then terminate our worker thread */
C
Christoph Hellwig 已提交
365
	kthread_stop(mq->thread);
L
Linus Torvalds 已提交
366

A
Adrian Hunter 已提交
367 368 369 370 371 372
	/* Empty the queue */
	spin_lock_irqsave(q->queue_lock, flags);
	q->queuedata = NULL;
	blk_start_queue(q);
	spin_unlock_irqrestore(q->queue_lock, flags);

373 374
	kfree(mqrq_cur->bounce_sg);
	mqrq_cur->bounce_sg = NULL;
375

376 377
	kfree(mqrq_cur->sg);
	mqrq_cur->sg = NULL;
L
Linus Torvalds 已提交
378

379 380
	kfree(mqrq_cur->bounce_buf);
	mqrq_cur->bounce_buf = NULL;
381

382 383 384 385 386 387 388 389 390
	kfree(mqrq_prev->bounce_sg);
	mqrq_prev->bounce_sg = NULL;

	kfree(mqrq_prev->sg);
	mqrq_prev->sg = NULL;

	kfree(mqrq_prev->bounce_buf);
	mqrq_prev->bounce_buf = NULL;

L
Linus Torvalds 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404
	mq->card = NULL;
}
EXPORT_SYMBOL(mmc_cleanup_queue);

/**
 * mmc_queue_suspend - suspend a MMC request queue
 * @mq: MMC queue to suspend
 *
 * Stop the block request queue, and wait for our thread to
 * complete any outstanding requests.  This ensures that we
 * won't suspend while a request is being processed.
 */
void mmc_queue_suspend(struct mmc_queue *mq)
{
405
	struct request_queue *q = mq->queue;
L
Linus Torvalds 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
	unsigned long flags;

	if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
		mq->flags |= MMC_QUEUE_SUSPENDED;

		spin_lock_irqsave(q->queue_lock, flags);
		blk_stop_queue(q);
		spin_unlock_irqrestore(q->queue_lock, flags);

		down(&mq->thread_sem);
	}
}

/**
 * mmc_queue_resume - resume a previously suspended MMC request queue
 * @mq: MMC queue to resume
 */
void mmc_queue_resume(struct mmc_queue *mq)
{
425
	struct request_queue *q = mq->queue;
L
Linus Torvalds 已提交
426 427 428 429 430 431 432 433 434 435 436 437
	unsigned long flags;

	if (mq->flags & MMC_QUEUE_SUSPENDED) {
		mq->flags &= ~MMC_QUEUE_SUSPENDED;

		up(&mq->thread_sem);

		spin_lock_irqsave(q->queue_lock, flags);
		blk_start_queue(q);
		spin_unlock_irqrestore(q->queue_lock, flags);
	}
}
438

439 440 441
/*
 * Prepare the sg list(s) to be handed of to the host driver
 */
442
unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
443 444
{
	unsigned int sg_len;
445 446 447
	size_t buflen;
	struct scatterlist *sg;
	int i;
448

449 450
	if (!mqrq->bounce_buf)
		return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
451

452
	BUG_ON(!mqrq->bounce_sg);
453

454
	sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
455

456
	mqrq->bounce_sg_len = sg_len;
457

458
	buflen = 0;
459
	for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
460
		buflen += sg->length;
461

462
	sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
463 464 465 466

	return 1;
}

467 468 469 470
/*
 * If writing, bounce the data to the buffer before the request
 * is sent to the host driver
 */
471
void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
472
{
473
	if (!mqrq->bounce_buf)
474 475
		return;

476
	if (rq_data_dir(mqrq->req) != WRITE)
477 478
		return;

479 480
	sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
		mqrq->bounce_buf, mqrq->sg[0].length);
481 482
}

483 484 485 486
/*
 * If reading, bounce the data from the buffer after the request
 * has been handled by the host driver
 */
487
void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
488
{
489
	if (!mqrq->bounce_buf)
490 491
		return;

492
	if (rq_data_dir(mqrq->req) != READ)
493 494
		return;

495 496
	sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
		mqrq->bounce_buf, mqrq->sg[0].length);
497
}