queue.c 7.4 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"
23
#include "core.h"
24
#include "card.h"
L
Linus Torvalds 已提交
25 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
	if (mq && mmc_card_removed(mq->card))
34 35
		return BLKPREP_KILL;

36
	req->rq_flags |= RQF_DONTPREP;
L
Linus Torvalds 已提交
37

38
	return BLKPREP_OK;
L
Linus Torvalds 已提交
39 40 41 42 43 44
}

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

47
	current->flags |= PF_MEMALLOC;
L
Linus Torvalds 已提交
48 49 50

	down(&mq->thread_sem);
	do {
51
		struct request *req;
L
Linus Torvalds 已提交
52 53 54

		spin_lock_irq(q->queue_lock);
		set_current_state(TASK_INTERRUPTIBLE);
J
Jens Axboe 已提交
55
		req = blk_fetch_request(q);
56 57 58 59 60 61 62 63
		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.
			 */
64
			if (mq->qcnt)
65 66 67 68
				cntx->is_waiting_last_req = true;
			else
				mq->asleep = true;
		}
L
Linus Torvalds 已提交
69 70
		spin_unlock_irq(q->queue_lock);

71
		if (req || mq->qcnt) {
72
			set_current_state(TASK_RUNNING);
73
			mmc_blk_issue_rq(mq, req);
74
			cond_resched();
75
		} else {
76 77
			if (kthread_should_stop()) {
				set_current_state(TASK_RUNNING);
L
Linus Torvalds 已提交
78
				break;
79
			}
L
Linus Torvalds 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
			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.
 */
96
static void mmc_request_fn(struct request_queue *q)
L
Linus Torvalds 已提交
97 98
{
	struct mmc_queue *mq = q->queuedata;
99
	struct request *req;
100
	struct mmc_context_info *cntx;
101 102

	if (!mq) {
A
Adrian Hunter 已提交
103
		while ((req = blk_fetch_request(q)) != NULL) {
104
			req->rq_flags |= RQF_QUIET;
105
			__blk_end_request_all(req, BLK_STS_IOERR);
A
Adrian Hunter 已提交
106
		}
107 108
		return;
	}
L
Linus Torvalds 已提交
109

110
	cntx = &mq->card->host->context_info;
111 112 113 114 115 116 117

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

	if (mq->asleep)
C
Christoph Hellwig 已提交
118
		wake_up_process(mq->thread);
L
Linus Torvalds 已提交
119 120
}

121
static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
122 123 124
{
	struct scatterlist *sg;

125
	sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
126
	if (sg)
127 128 129 130 131
		sg_init_table(sg, sg_len);

	return sg;
}

132 133 134 135 136 137 138 139 140 141
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);
142
	blk_queue_max_discard_sectors(q, max_discard);
143 144 145 146
	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;
147
	if (mmc_can_secure_erase_trim(card))
148
		queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
149 150
}

151 152 153 154 155 156 157 158
/**
 * mmc_init_request() - initialize the MMC-specific per-request data
 * @q: the request queue
 * @req: the request
 * @gfp: memory allocation policy
 */
static int mmc_init_request(struct request_queue *q, struct request *req,
			    gfp_t gfp)
159
{
160 161 162 163
	struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
	struct mmc_queue *mq = q->queuedata;
	struct mmc_card *card = mq->card;
	struct mmc_host *host = card->host;
164

L
Linus Walleij 已提交
165 166 167
	mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
	if (!mq_rq->sg)
		return -ENOMEM;
168

169 170
	return 0;
}
171

172
static void mmc_exit_request(struct request_queue *q, struct request *req)
173
{
174
	struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
175

176 177
	kfree(mq_rq->sg);
	mq_rq->sg = NULL;
178 179
}

L
Linus Torvalds 已提交
180 181 182 183 184
/**
 * mmc_init_queue - initialise a queue structure.
 * @mq: mmc queue
 * @card: mmc card to attach this queue
 * @lock: queue lock
185
 * @subname: partition subname
L
Linus Torvalds 已提交
186 187 188
 *
 * Initialise a MMC card request queue.
 */
189 190
int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
		   spinlock_t *lock, const char *subname)
L
Linus Torvalds 已提交
191 192 193
{
	struct mmc_host *host = card->host;
	u64 limit = BLK_BOUNCE_HIGH;
194
	int ret = -ENOMEM;
L
Linus Torvalds 已提交
195

196
	if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
197
		limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
L
Linus Torvalds 已提交
198 199

	mq->card = card;
200
	mq->queue = blk_alloc_queue(GFP_KERNEL);
L
Linus Torvalds 已提交
201 202
	if (!mq->queue)
		return -ENOMEM;
203 204 205 206 207
	mq->queue->queue_lock = lock;
	mq->queue->request_fn = mmc_request_fn;
	mq->queue->init_rq_fn = mmc_init_request;
	mq->queue->exit_rq_fn = mmc_exit_request;
	mq->queue->cmd_size = sizeof(struct mmc_queue_req);
L
Linus Torvalds 已提交
208
	mq->queue->queuedata = mq;
209 210 211 212 213 214
	mq->qcnt = 0;
	ret = blk_init_allocated_queue(mq->queue);
	if (ret) {
		blk_cleanup_queue(mq->queue);
		return ret;
	}
L
Linus Torvalds 已提交
215

216
	blk_queue_prep_rq(mq->queue, mmc_prep_request);
217
	queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
218
	queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
219 220
	if (mmc_can_erase(card))
		mmc_queue_setup_discard(mq->queue, card);
221

L
Linus Walleij 已提交
222 223 224 225 226
	blk_queue_bounce_limit(mq->queue, limit);
	blk_queue_max_hw_sectors(mq->queue,
		min(host->max_blk_count, host->max_req_size / 512));
	blk_queue_max_segments(mq->queue, host->max_segs);
	blk_queue_max_segment_size(mq->queue, host->max_seg_size);
L
Linus Torvalds 已提交
227

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

230 231
	mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
		host->index, subname ? subname : "");
232

C
Christoph Hellwig 已提交
233 234
	if (IS_ERR(mq->thread)) {
		ret = PTR_ERR(mq->thread);
235
		goto cleanup_queue;
L
Linus Torvalds 已提交
236 237
	}

C
Christoph Hellwig 已提交
238
	return 0;
239

240
cleanup_queue:
L
Linus Torvalds 已提交
241 242 243 244 245 246
	blk_cleanup_queue(mq->queue);
	return ret;
}

void mmc_cleanup_queue(struct mmc_queue *mq)
{
247
	struct request_queue *q = mq->queue;
248 249
	unsigned long flags;

250 251 252
	/* Make sure the queue isn't suspended, as that will deadlock */
	mmc_queue_resume(mq);

253
	/* Then terminate our worker thread */
C
Christoph Hellwig 已提交
254
	kthread_stop(mq->thread);
L
Linus Torvalds 已提交
255

A
Adrian Hunter 已提交
256 257 258 259 260 261
	/* Empty the queue */
	spin_lock_irqsave(q->queue_lock, flags);
	q->queuedata = NULL;
	blk_start_queue(q);
	spin_unlock_irqrestore(q->queue_lock, flags);

L
Linus Torvalds 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275
	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)
{
276
	struct request_queue *q = mq->queue;
L
Linus Torvalds 已提交
277 278
	unsigned long flags;

279 280
	if (!mq->suspended) {
		mq->suspended |= true;
L
Linus Torvalds 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

		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)
{
296
	struct request_queue *q = mq->queue;
L
Linus Torvalds 已提交
297 298
	unsigned long flags;

299 300
	if (mq->suspended) {
		mq->suspended = false;
L
Linus Torvalds 已提交
301 302 303 304 305 306 307 308

		up(&mq->thread_sem);

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

310 311 312
/*
 * Prepare the sg list(s) to be handed of to the host driver
 */
313
unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
314
{
315
	struct request *req = mmc_queue_req_to_req(mqrq);
316

L
Linus Walleij 已提交
317
	return blk_rq_map_sg(mq->queue, req, mqrq->sg);
318
}