• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  linux/drivers/mmc/card/queue.c
3  *
4  *  Copyright (C) 2003 Russell King, All Rights Reserved.
5  *  Copyright 2006-2007 Pierre Ossman
6  *
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  */
12 #include <linux/slab.h>
13 #include <linux/module.h>
14 #include <linux/blkdev.h>
15 #include <linux/freezer.h>
16 #include <linux/kthread.h>
17 #include <linux/scatterlist.h>
18 
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/host.h>
21 #include <linux/sched/rt.h>
22 #include "queue.h"
23 
24 #define MMC_QUEUE_BOUNCESZ	65536
25 
26 /*
27  * Prepare a MMC request. This just filters out odd stuff.
28  */
mmc_prep_request(struct request_queue * q,struct request * req)29 static int mmc_prep_request(struct request_queue *q, struct request *req)
30 {
31 	struct mmc_queue *mq = q->queuedata;
32 
33 	/*
34 	 * We only like normal block requests and discards.
35 	 */
36 	if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
37 		blk_dump_rq_flags(req, "MMC bad request");
38 		return BLKPREP_KILL;
39 	}
40 
41 	if (mq && mmc_card_removed(mq->card))
42 		return BLKPREP_KILL;
43 
44 	req->cmd_flags |= REQ_DONTPREP;
45 
46 	return BLKPREP_OK;
47 }
48 
mmc_queue_thread(void * d)49 static int mmc_queue_thread(void *d)
50 {
51 	struct mmc_queue *mq = d;
52 	struct request_queue *q = mq->queue;
53 
54 	struct sched_param scheduler_params = {0};
55 	scheduler_params.sched_priority = 1;
56 
57 	sched_setscheduler(current, SCHED_FIFO, &scheduler_params);
58 
59 	current->flags |= PF_MEMALLOC;
60 
61 	down(&mq->thread_sem);
62 	do {
63 		struct request *req = NULL;
64 		struct mmc_queue_req *tmp;
65 		unsigned int cmd_flags = 0;
66 
67 		spin_lock_irq(q->queue_lock);
68 		set_current_state(TASK_INTERRUPTIBLE);
69 		req = blk_fetch_request(q);
70 		mq->mqrq_cur->req = req;
71 		spin_unlock_irq(q->queue_lock);
72 
73 		if (req || mq->mqrq_prev->req) {
74 			set_current_state(TASK_RUNNING);
75 			cmd_flags = req ? req->cmd_flags : 0;
76 			mq->issue_fn(mq, req);
77 			if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
78 				mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
79 				continue; /* fetch again */
80 			}
81 
82 			/*
83 			 * Current request becomes previous request
84 			 * and vice versa.
85 			 * In case of special requests, current request
86 			 * has been finished. Do not assign it to previous
87 			 * request.
88 			 */
89 			if (cmd_flags & MMC_REQ_SPECIAL_MASK)
90 				mq->mqrq_cur->req = NULL;
91 
92 			mq->mqrq_prev->brq.mrq.data = NULL;
93 			mq->mqrq_prev->req = NULL;
94 			tmp = mq->mqrq_prev;
95 			mq->mqrq_prev = mq->mqrq_cur;
96 			mq->mqrq_cur = tmp;
97 		} else {
98 			if (kthread_should_stop()) {
99 				set_current_state(TASK_RUNNING);
100 				break;
101 			}
102 			up(&mq->thread_sem);
103 			schedule();
104 			down(&mq->thread_sem);
105 		}
106 	} while (1);
107 	up(&mq->thread_sem);
108 
109 	return 0;
110 }
111 
112 /*
113  * Generic MMC request handler.  This is called for any queue on a
114  * particular host.  When the host is not busy, we look for a request
115  * on any queue on this host, and attempt to issue it.  This may
116  * not be the queue we were asked to process.
117  */
mmc_request_fn(struct request_queue * q)118 static void mmc_request_fn(struct request_queue *q)
119 {
120 	struct mmc_queue *mq = q->queuedata;
121 	struct request *req;
122 	unsigned long flags;
123 	struct mmc_context_info *cntx;
124 
125 	if (!mq) {
126 		while ((req = blk_fetch_request(q)) != NULL) {
127 			req->cmd_flags |= REQ_QUIET;
128 			__blk_end_request_all(req, -EIO);
129 		}
130 		return;
131 	}
132 
133 	cntx = &mq->card->host->context_info;
134 	if (!mq->mqrq_cur->req && mq->mqrq_prev->req) {
135 		/*
136 		 * New MMC request arrived when MMC thread may be
137 		 * blocked on the previous request to be complete
138 		 * with no current request fetched
139 		 */
140 		spin_lock_irqsave(&cntx->lock, flags);
141 		if (cntx->is_waiting_last_req) {
142 			cntx->is_new_req = true;
143 			wake_up_interruptible(&cntx->wait);
144 		}
145 		spin_unlock_irqrestore(&cntx->lock, flags);
146 	} else if (!mq->mqrq_cur->req && !mq->mqrq_prev->req)
147 		wake_up_process(mq->thread);
148 }
149 
mmc_alloc_sg(int sg_len,int * err)150 static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
151 {
152 	struct scatterlist *sg;
153 
154 	sg = kmalloc(sizeof(struct scatterlist)*sg_len, GFP_KERNEL);
155 	if (!sg)
156 		*err = -ENOMEM;
157 	else {
158 		*err = 0;
159 		sg_init_table(sg, sg_len);
160 	}
161 
162 	return sg;
163 }
164 
mmc_queue_setup_discard(struct request_queue * q,struct mmc_card * card)165 static void mmc_queue_setup_discard(struct request_queue *q,
166 				    struct mmc_card *card)
167 {
168 	unsigned max_discard;
169 
170 	max_discard = mmc_calc_max_discard(card);
171 	if (!max_discard)
172 		return;
173 
174 	queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
175 	q->limits.max_discard_sectors = max_discard;
176 	if (card->erased_byte == 0 && !mmc_can_discard(card))
177 		q->limits.discard_zeroes_data = 1;
178 	q->limits.discard_granularity = card->pref_erase << 9;
179 	/* granularity must not be greater than max. discard */
180 	if (card->pref_erase > max_discard)
181 		q->limits.discard_granularity = 0;
182 	if (mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))
183 		queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
184 }
185 
186 /**
187  * mmc_init_queue - initialise a queue structure.
188  * @mq: mmc queue
189  * @card: mmc card to attach this queue
190  * @lock: queue lock
191  * @subname: partition subname
192  *
193  * Initialise a MMC card request queue.
194  */
mmc_init_queue(struct mmc_queue * mq,struct mmc_card * card,spinlock_t * lock,const char * subname)195 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
196 		   spinlock_t *lock, const char *subname)
197 {
198 	struct mmc_host *host = card->host;
199 	u64 limit = BLK_BOUNCE_HIGH;
200 	int ret;
201 	struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
202 	struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
203 
204 	if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
205 		limit = *mmc_dev(host)->dma_mask;
206 
207 	mq->card = card;
208 	mq->queue = blk_init_queue(mmc_request_fn, lock);
209 	if (!mq->queue)
210 		return -ENOMEM;
211 
212 	mq->mqrq_cur = mqrq_cur;
213 	mq->mqrq_prev = mqrq_prev;
214 	mq->queue->queuedata = mq;
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 	if (mmc_can_erase(card))
220 		mmc_queue_setup_discard(mq->queue, card);
221 
222 #ifdef CONFIG_MMC_BLOCK_BOUNCE
223 	if (host->max_segs == 1) {
224 		unsigned int bouncesz;
225 
226 		bouncesz = MMC_QUEUE_BOUNCESZ;
227 
228 		if (bouncesz > host->max_req_size)
229 			bouncesz = host->max_req_size;
230 		if (bouncesz > host->max_seg_size)
231 			bouncesz = host->max_seg_size;
232 		if (bouncesz > (host->max_blk_count * 512))
233 			bouncesz = host->max_blk_count * 512;
234 
235 		if (bouncesz > 512) {
236 			mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
237 			if (!mqrq_cur->bounce_buf) {
238 				pr_warning("%s: unable to "
239 					"allocate bounce cur buffer\n",
240 					mmc_card_name(card));
241 			}
242 			mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
243 			if (!mqrq_prev->bounce_buf) {
244 				pr_warning("%s: unable to "
245 					"allocate bounce prev buffer\n",
246 					mmc_card_name(card));
247 				kfree(mqrq_cur->bounce_buf);
248 				mqrq_cur->bounce_buf = NULL;
249 			}
250 		}
251 
252 		if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
253 			blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
254 			blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
255 			blk_queue_max_segments(mq->queue, bouncesz / 512);
256 			blk_queue_max_segment_size(mq->queue, bouncesz);
257 
258 			mqrq_cur->sg = mmc_alloc_sg(1, &ret);
259 			if (ret)
260 				goto cleanup_queue;
261 
262 			mqrq_cur->bounce_sg =
263 				mmc_alloc_sg(bouncesz / 512, &ret);
264 			if (ret)
265 				goto cleanup_queue;
266 
267 			mqrq_prev->sg = mmc_alloc_sg(1, &ret);
268 			if (ret)
269 				goto cleanup_queue;
270 
271 			mqrq_prev->bounce_sg =
272 				mmc_alloc_sg(bouncesz / 512, &ret);
273 			if (ret)
274 				goto cleanup_queue;
275 		}
276 	}
277 #endif
278 
279 	if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
280 		blk_queue_bounce_limit(mq->queue, limit);
281 		blk_queue_max_hw_sectors(mq->queue,
282 			min(host->max_blk_count, host->max_req_size / 512));
283 		blk_queue_max_segments(mq->queue, host->max_segs);
284 		blk_queue_max_segment_size(mq->queue, host->max_seg_size);
285 
286 		mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
287 		if (ret)
288 			goto cleanup_queue;
289 
290 
291 		mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
292 		if (ret)
293 			goto cleanup_queue;
294 	}
295 
296 	sema_init(&mq->thread_sem, 1);
297 
298 	mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
299 		host->index, subname ? subname : "");
300 
301 	if (IS_ERR(mq->thread)) {
302 		ret = PTR_ERR(mq->thread);
303 		goto free_bounce_sg;
304 	}
305 
306 	return 0;
307  free_bounce_sg:
308 	kfree(mqrq_cur->bounce_sg);
309 	mqrq_cur->bounce_sg = NULL;
310 	kfree(mqrq_prev->bounce_sg);
311 	mqrq_prev->bounce_sg = NULL;
312 
313  cleanup_queue:
314 	kfree(mqrq_cur->sg);
315 	mqrq_cur->sg = NULL;
316 	kfree(mqrq_cur->bounce_buf);
317 	mqrq_cur->bounce_buf = NULL;
318 
319 	kfree(mqrq_prev->sg);
320 	mqrq_prev->sg = NULL;
321 	kfree(mqrq_prev->bounce_buf);
322 	mqrq_prev->bounce_buf = NULL;
323 
324 	blk_cleanup_queue(mq->queue);
325 	return ret;
326 }
327 
mmc_cleanup_queue(struct mmc_queue * mq)328 void mmc_cleanup_queue(struct mmc_queue *mq)
329 {
330 	struct request_queue *q = mq->queue;
331 	unsigned long flags;
332 	struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
333 	struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
334 
335 	/* Make sure the queue isn't suspended, as that will deadlock */
336 	mmc_queue_resume(mq);
337 
338 	/* Then terminate our worker thread */
339 	kthread_stop(mq->thread);
340 
341 	/* Empty the queue */
342 	spin_lock_irqsave(q->queue_lock, flags);
343 	q->queuedata = NULL;
344 	blk_start_queue(q);
345 	spin_unlock_irqrestore(q->queue_lock, flags);
346 
347 	kfree(mqrq_cur->bounce_sg);
348 	mqrq_cur->bounce_sg = NULL;
349 
350 	kfree(mqrq_cur->sg);
351 	mqrq_cur->sg = NULL;
352 
353 	kfree(mqrq_cur->bounce_buf);
354 	mqrq_cur->bounce_buf = NULL;
355 
356 	kfree(mqrq_prev->bounce_sg);
357 	mqrq_prev->bounce_sg = NULL;
358 
359 	kfree(mqrq_prev->sg);
360 	mqrq_prev->sg = NULL;
361 
362 	kfree(mqrq_prev->bounce_buf);
363 	mqrq_prev->bounce_buf = NULL;
364 
365 	mq->card = NULL;
366 }
367 EXPORT_SYMBOL(mmc_cleanup_queue);
368 
mmc_packed_init(struct mmc_queue * mq,struct mmc_card * card)369 int mmc_packed_init(struct mmc_queue *mq, struct mmc_card *card)
370 {
371 	struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
372 	struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
373 	int ret = 0;
374 
375 
376 	mqrq_cur->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
377 	if (!mqrq_cur->packed) {
378 		pr_warn("%s: unable to allocate packed cmd for mqrq_cur\n",
379 			mmc_card_name(card));
380 		ret = -ENOMEM;
381 		goto out;
382 	}
383 
384 	mqrq_prev->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
385 	if (!mqrq_prev->packed) {
386 		pr_warn("%s: unable to allocate packed cmd for mqrq_prev\n",
387 			mmc_card_name(card));
388 		kfree(mqrq_cur->packed);
389 		mqrq_cur->packed = NULL;
390 		ret = -ENOMEM;
391 		goto out;
392 	}
393 
394 	INIT_LIST_HEAD(&mqrq_cur->packed->list);
395 	INIT_LIST_HEAD(&mqrq_prev->packed->list);
396 
397 out:
398 	return ret;
399 }
400 
mmc_packed_clean(struct mmc_queue * mq)401 void mmc_packed_clean(struct mmc_queue *mq)
402 {
403 	struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
404 	struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
405 
406 	kfree(mqrq_cur->packed);
407 	mqrq_cur->packed = NULL;
408 	kfree(mqrq_prev->packed);
409 	mqrq_prev->packed = NULL;
410 }
411 
412 /**
413  * mmc_queue_suspend - suspend a MMC request queue
414  * @mq: MMC queue to suspend
415  *
416  * Stop the block request queue, and wait for our thread to
417  * complete any outstanding requests.  This ensures that we
418  * won't suspend while a request is being processed.
419  */
mmc_queue_suspend(struct mmc_queue * mq)420 void mmc_queue_suspend(struct mmc_queue *mq)
421 {
422 	struct request_queue *q = mq->queue;
423 	unsigned long flags;
424 
425 	if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
426 		mq->flags |= MMC_QUEUE_SUSPENDED;
427 
428 		spin_lock_irqsave(q->queue_lock, flags);
429 		blk_stop_queue(q);
430 		spin_unlock_irqrestore(q->queue_lock, flags);
431 
432 		down(&mq->thread_sem);
433 	}
434 }
435 
436 /**
437  * mmc_queue_resume - resume a previously suspended MMC request queue
438  * @mq: MMC queue to resume
439  */
mmc_queue_resume(struct mmc_queue * mq)440 void mmc_queue_resume(struct mmc_queue *mq)
441 {
442 	struct request_queue *q = mq->queue;
443 	unsigned long flags;
444 
445 	if (mq->flags & MMC_QUEUE_SUSPENDED) {
446 		mq->flags &= ~MMC_QUEUE_SUSPENDED;
447 
448 		up(&mq->thread_sem);
449 
450 		spin_lock_irqsave(q->queue_lock, flags);
451 		blk_start_queue(q);
452 		spin_unlock_irqrestore(q->queue_lock, flags);
453 	}
454 }
455 
mmc_queue_packed_map_sg(struct mmc_queue * mq,struct mmc_packed * packed,struct scatterlist * sg,enum mmc_packed_type cmd_type)456 static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
457 					    struct mmc_packed *packed,
458 					    struct scatterlist *sg,
459 					    enum mmc_packed_type cmd_type)
460 {
461 	struct scatterlist *__sg = sg;
462 	unsigned int sg_len = 0;
463 	struct request *req;
464 
465 	if (mmc_packed_wr(cmd_type)) {
466 		unsigned int hdr_sz = mmc_large_sector(mq->card) ? 4096 : 512;
467 		unsigned int max_seg_sz = queue_max_segment_size(mq->queue);
468 		unsigned int len, remain, offset = 0;
469 		u8 *buf = (u8 *)packed->cmd_hdr;
470 
471 		remain = hdr_sz;
472 		do {
473 			len = min(remain, max_seg_sz);
474 			sg_set_buf(__sg, buf + offset, len);
475 			offset += len;
476 			remain -= len;
477 			(__sg++)->page_link &= ~0x02;
478 			sg_len++;
479 		} while (remain);
480 	}
481 
482 	list_for_each_entry(req, &packed->list, queuelist) {
483 		sg_len += blk_rq_map_sg(mq->queue, req, __sg);
484 		__sg = sg + (sg_len - 1);
485 		(__sg++)->page_link &= ~0x02;
486 	}
487 	sg_mark_end(sg + (sg_len - 1));
488 	return sg_len;
489 }
490 
491 /*
492  * Prepare the sg list(s) to be handed of to the host driver
493  */
mmc_queue_map_sg(struct mmc_queue * mq,struct mmc_queue_req * mqrq)494 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
495 {
496 	unsigned int sg_len;
497 	size_t buflen;
498 	struct scatterlist *sg;
499 	enum mmc_packed_type cmd_type;
500 	int i;
501 
502 	cmd_type = mqrq->cmd_type;
503 
504 	if (!mqrq->bounce_buf) {
505 		if (mmc_packed_cmd(cmd_type))
506 			return mmc_queue_packed_map_sg(mq, mqrq->packed,
507 						       mqrq->sg, cmd_type);
508 		else
509 			return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
510 	}
511 
512 	BUG_ON(!mqrq->bounce_sg);
513 
514 	if (mmc_packed_cmd(cmd_type))
515 		sg_len = mmc_queue_packed_map_sg(mq, mqrq->packed,
516 						 mqrq->bounce_sg, cmd_type);
517 	else
518 		sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
519 
520 	mqrq->bounce_sg_len = sg_len;
521 
522 	buflen = 0;
523 	for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
524 		buflen += sg->length;
525 
526 	sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
527 
528 	return 1;
529 }
530 
531 /*
532  * If writing, bounce the data to the buffer before the request
533  * is sent to the host driver
534  */
mmc_queue_bounce_pre(struct mmc_queue_req * mqrq)535 void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
536 {
537 	if (!mqrq->bounce_buf)
538 		return;
539 
540 	if (rq_data_dir(mqrq->req) != WRITE)
541 		return;
542 
543 	sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
544 		mqrq->bounce_buf, mqrq->sg[0].length);
545 }
546 
547 /*
548  * If reading, bounce the data from the buffer after the request
549  * has been handled by the host driver
550  */
mmc_queue_bounce_post(struct mmc_queue_req * mqrq)551 void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
552 {
553 	if (!mqrq->bounce_buf)
554 		return;
555 
556 	if (rq_data_dir(mqrq->req) != READ)
557 		return;
558 
559 	sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
560 		mqrq->bounce_buf, mqrq->sg[0].length);
561 }
562