queue.c 11.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 *  Copyright (C) 2003 Russell King, All Rights Reserved.
3
 *  Copyright 2006-2007 Pierre Ossman
L
Linus Torvalds 已提交
4 5 6 7 8 9
 *
 * 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.
 *
 */
10
#include <linux/slab.h>
L
Linus Torvalds 已提交
11 12
#include <linux/module.h>
#include <linux/blkdev.h>
13
#include <linux/freezer.h>
C
Christoph Hellwig 已提交
14
#include <linux/kthread.h>
J
Jens Axboe 已提交
15
#include <linux/scatterlist.h>
16
#include <linux/dma-mapping.h>
L
Linus Torvalds 已提交
17 18 19

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

21
#include "queue.h"
22
#include "block.h"
L
Linus Torvalds 已提交
23

24 25
#define MMC_QUEUE_BOUNCESZ	65536

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

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

42
	if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
43 44
		return BLKPREP_KILL;

45
	req->rq_flags |= RQF_DONTPREP;
L
Linus Torvalds 已提交
46

47
	return BLKPREP_OK;
L
Linus Torvalds 已提交
48 49 50 51 52 53
}

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

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

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

		spin_lock_irq(q->queue_lock);
		set_current_state(TASK_INTERRUPTIBLE);
J
Jens Axboe 已提交
64
		req = blk_fetch_request(q);
65 66 67 68 69 70 71 72 73 74 75 76 77
		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;
		}
78
		mq->mqrq_cur->req = req;
L
Linus Torvalds 已提交
79 80
		spin_unlock_irq(q->queue_lock);

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

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

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

102 103
			mq->mqrq_prev->brq.mrq.data = NULL;
			mq->mqrq_prev->req = NULL;
104
			swap(mq->mqrq_prev, mq->mqrq_cur);
105
		} else {
106 107
			if (kthread_should_stop()) {
				set_current_state(TASK_RUNNING);
L
Linus Torvalds 已提交
108
				break;
109
			}
L
Linus Torvalds 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
			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.
 */
126
static void mmc_request_fn(struct request_queue *q)
L
Linus Torvalds 已提交
127 128
{
	struct mmc_queue *mq = q->queuedata;
129
	struct request *req;
130
	struct mmc_context_info *cntx;
131 132

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

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

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

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

151
static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
152 153 154
{
	struct scatterlist *sg;

155
	sg = kmalloc_array(sg_len, sizeof(*sg), GFP_KERNEL);
156 157 158 159 160 161 162 163 164 165
	if (!sg)
		*err = -ENOMEM;
	else {
		*err = 0;
		sg_init_table(sg, sg_len);
	}

	return sg;
}

166 167 168 169 170 171 172 173 174 175
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);
176
	blk_queue_max_discard_sectors(q, max_discard);
177
	if (card->erased_byte == 0 && !mmc_can_discard(card))
178 179 180 181 182
		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;
183
	if (mmc_can_secure_erase_trim(card))
184
		queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
185 186
}

187 188 189 190
#ifdef CONFIG_MMC_BLOCK_BOUNCE
static bool mmc_queue_alloc_bounce_bufs(struct mmc_queue *mq,
					unsigned int bouncesz)
{
191
	int i;
192

193 194 195 196
	for (i = 0; i < mq->qdepth; i++) {
		mq->mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
		if (!mq->mqrq[i].bounce_buf)
			goto out_err;
197 198 199
	}

	return true;
200 201 202 203 204 205 206 207 208

out_err:
	while (--i >= 0) {
		kfree(mq->mqrq[i].bounce_buf);
		mq->mqrq[i].bounce_buf = NULL;
	}
	pr_warn("%s: unable to allocate bounce buffers\n",
		mmc_card_name(mq->card));
	return false;
209
}
210 211 212 213

static int mmc_queue_alloc_bounce_sgs(struct mmc_queue *mq,
				      unsigned int bouncesz)
{
214
	int i, ret;
215

216 217 218 219
	for (i = 0; i < mq->qdepth; i++) {
		mq->mqrq[i].sg = mmc_alloc_sg(1, &ret);
		if (ret)
			return ret;
220

221 222 223 224
		mq->mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret);
		if (ret)
			return ret;
	}
225

226
	return 0;
227
}
228 229
#endif

230 231
static int mmc_queue_alloc_sgs(struct mmc_queue *mq, int max_segs)
{
232
	int i, ret;
233

234 235 236 237 238
	for (i = 0; i < mq->qdepth; i++) {
		mq->mqrq[i].sg = mmc_alloc_sg(max_segs, &ret);
		if (ret)
			return ret;
	}
239

240 241
	return 0;
}
242

243 244 245 246 247 248 249 250 251 252
static void mmc_queue_req_free_bufs(struct mmc_queue_req *mqrq)
{
	kfree(mqrq->bounce_sg);
	mqrq->bounce_sg = NULL;

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

	kfree(mqrq->bounce_buf);
	mqrq->bounce_buf = NULL;
253 254
}

255 256
static void mmc_queue_reqs_free_bufs(struct mmc_queue *mq)
{
257 258 259 260
	int i;

	for (i = 0; i < mq->qdepth; i++)
		mmc_queue_req_free_bufs(&mq->mqrq[i]);
261 262
}

L
Linus Torvalds 已提交
263 264 265 266 267
/**
 * mmc_init_queue - initialise a queue structure.
 * @mq: mmc queue
 * @card: mmc card to attach this queue
 * @lock: queue lock
268
 * @subname: partition subname
L
Linus Torvalds 已提交
269 270 271
 *
 * Initialise a MMC card request queue.
 */
272 273
int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
		   spinlock_t *lock, const char *subname)
L
Linus Torvalds 已提交
274 275 276
{
	struct mmc_host *host = card->host;
	u64 limit = BLK_BOUNCE_HIGH;
277
	bool bounce = false;
278
	int ret = -ENOMEM;
L
Linus Torvalds 已提交
279

280
	if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
281
		limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
L
Linus Torvalds 已提交
282 283

	mq->card = card;
284
	mq->queue = blk_init_queue(mmc_request_fn, lock);
L
Linus Torvalds 已提交
285 286 287
	if (!mq->queue)
		return -ENOMEM;

288 289 290 291 292
	mq->qdepth = 2;
	mq->mqrq = kcalloc(mq->qdepth, sizeof(struct mmc_queue_req),
			   GFP_KERNEL);
	if (!mq->mqrq)
		goto blk_cleanup;
293 294
	mq->mqrq_cur = &mq->mqrq[0];
	mq->mqrq_prev = &mq->mqrq[1];
L
Linus Torvalds 已提交
295 296
	mq->queue->queuedata = mq;

297
	blk_queue_prep_rq(mq->queue, mmc_prep_request);
298
	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
299
	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
300 301
	if (mmc_can_erase(card))
		mmc_queue_setup_discard(mq->queue, card);
302 303

#ifdef CONFIG_MMC_BLOCK_BOUNCE
304
	if (host->max_segs == 1) {
305 306
		unsigned int bouncesz;

307 308 309 310 311 312
		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;
313 314 315
		if (bouncesz > (host->max_blk_count * 512))
			bouncesz = host->max_blk_count * 512;

316 317
		if (bouncesz > 512 &&
		    mmc_queue_alloc_bounce_bufs(mq, bouncesz)) {
318
			blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
319
			blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
320
			blk_queue_max_segments(mq->queue, bouncesz / 512);
321 322
			blk_queue_max_segment_size(mq->queue, bouncesz);

323
			ret = mmc_queue_alloc_bounce_sgs(mq, bouncesz);
324 325
			if (ret)
				goto cleanup_queue;
326
			bounce = true;
327 328 329 330
		}
	}
#endif

331
	if (!bounce) {
332
		blk_queue_bounce_limit(mq->queue, limit);
333
		blk_queue_max_hw_sectors(mq->queue,
334
			min(host->max_blk_count, host->max_req_size / 512));
335
		blk_queue_max_segments(mq->queue, host->max_segs);
336 337
		blk_queue_max_segment_size(mq->queue, host->max_seg_size);

338
		ret = mmc_queue_alloc_sgs(mq, host->max_segs);
339 340
		if (ret)
			goto cleanup_queue;
L
Linus Torvalds 已提交
341 342
	}

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

345 346
	mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
		host->index, subname ? subname : "");
347

C
Christoph Hellwig 已提交
348 349
	if (IS_ERR(mq->thread)) {
		ret = PTR_ERR(mq->thread);
350
		goto cleanup_queue;
L
Linus Torvalds 已提交
351 352
	}

C
Christoph Hellwig 已提交
353
	return 0;
354

355
 cleanup_queue:
356
	mmc_queue_reqs_free_bufs(mq);
357 358 359
	kfree(mq->mqrq);
	mq->mqrq = NULL;
blk_cleanup:
L
Linus Torvalds 已提交
360 361 362 363 364 365
	blk_cleanup_queue(mq->queue);
	return ret;
}

void mmc_cleanup_queue(struct mmc_queue *mq)
{
366
	struct request_queue *q = mq->queue;
367 368
	unsigned long flags;

369 370 371
	/* Make sure the queue isn't suspended, as that will deadlock */
	mmc_queue_resume(mq);

372
	/* Then terminate our worker thread */
C
Christoph Hellwig 已提交
373
	kthread_stop(mq->thread);
L
Linus Torvalds 已提交
374

A
Adrian Hunter 已提交
375 376 377 378 379 380
	/* Empty the queue */
	spin_lock_irqsave(q->queue_lock, flags);
	q->queuedata = NULL;
	blk_start_queue(q);
	spin_unlock_irqrestore(q->queue_lock, flags);

381
	mmc_queue_reqs_free_bufs(mq);
382 383
	kfree(mq->mqrq);
	mq->mqrq = NULL;
384

L
Linus Torvalds 已提交
385 386 387 388 389 390 391 392 393 394 395 396 397 398
	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)
{
399
	struct request_queue *q = mq->queue;
L
Linus Torvalds 已提交
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
	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)
{
419
	struct request_queue *q = mq->queue;
L
Linus Torvalds 已提交
420 421 422 423 424 425 426 427 428 429 430 431
	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);
	}
}
432

433 434 435
/*
 * Prepare the sg list(s) to be handed of to the host driver
 */
436
unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
437 438
{
	unsigned int sg_len;
439 440 441
	size_t buflen;
	struct scatterlist *sg;
	int i;
442

443 444
	if (!mqrq->bounce_buf)
		return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
445

446
	sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
447

448
	mqrq->bounce_sg_len = sg_len;
449

450
	buflen = 0;
451
	for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
452
		buflen += sg->length;
453

454
	sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
455 456 457 458

	return 1;
}

459 460 461 462
/*
 * If writing, bounce the data to the buffer before the request
 * is sent to the host driver
 */
463
void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
464
{
465
	if (!mqrq->bounce_buf)
466 467
		return;

468
	if (rq_data_dir(mqrq->req) != WRITE)
469 470
		return;

471 472
	sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
		mqrq->bounce_buf, mqrq->sg[0].length);
473 474
}

475 476 477 478
/*
 * If reading, bounce the data from the buffer after the request
 * has been handled by the host driver
 */
479
void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
480
{
481
	if (!mqrq->bounce_buf)
482 483
		return;

484
	if (rq_data_dir(mqrq->req) != READ)
485 486
		return;

487 488
	sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
		mqrq->bounce_buf, mqrq->sg[0].length);
489
}