• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (C) 2003 Russell King, All Rights Reserved.
3  *  Copyright 2006-2007 Pierre Ossman
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  */
10 #include <linux/slab.h>
11 #include <linux/module.h>
12 #include <linux/blkdev.h>
13 #include <linux/freezer.h>
14 #include <linux/kthread.h>
15 #include <linux/scatterlist.h>
16 #include <linux/dma-mapping.h>
17 
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/host.h>
20 
21 #include "queue.h"
22 #include "block.h"
23 #include "core.h"
24 #include "card.h"
25 #include "host.h"
26 
mmc_cqe_dcmd_busy(struct mmc_queue * mq)27 static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
28 {
29 	/* Allow only 1 DCMD at a time */
30 	return mq->in_flight[MMC_ISSUE_DCMD];
31 }
32 
mmc_cqe_check_busy(struct mmc_queue * mq)33 void mmc_cqe_check_busy(struct mmc_queue *mq)
34 {
35 	if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
36 		mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
37 
38 	mq->cqe_busy &= ~MMC_CQE_QUEUE_FULL;
39 }
40 
mmc_cqe_can_dcmd(struct mmc_host * host)41 static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
42 {
43 	return host->caps2 & MMC_CAP2_CQE_DCMD;
44 }
45 
mmc_cqe_issue_type(struct mmc_host * host,struct request * req)46 static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
47 					      struct request *req)
48 {
49 	switch (req_op(req)) {
50 	case REQ_OP_DRV_IN:
51 	case REQ_OP_DRV_OUT:
52 	case REQ_OP_DISCARD:
53 	case REQ_OP_SECURE_ERASE:
54 		return MMC_ISSUE_SYNC;
55 	case REQ_OP_FLUSH:
56 		return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
57 	default:
58 		return MMC_ISSUE_ASYNC;
59 	}
60 }
61 
mmc_issue_type(struct mmc_queue * mq,struct request * req)62 enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
63 {
64 	struct mmc_host *host = mq->card->host;
65 
66 	if (mq->use_cqe)
67 		return mmc_cqe_issue_type(host, req);
68 
69 	if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
70 		return MMC_ISSUE_ASYNC;
71 
72 	return MMC_ISSUE_SYNC;
73 }
74 
__mmc_cqe_recovery_notifier(struct mmc_queue * mq)75 static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
76 {
77 	if (!mq->recovery_needed) {
78 		mq->recovery_needed = true;
79 		schedule_work(&mq->recovery_work);
80 	}
81 }
82 
mmc_cqe_recovery_notifier(struct mmc_request * mrq)83 void mmc_cqe_recovery_notifier(struct mmc_request *mrq)
84 {
85 	struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
86 						  brq.mrq);
87 	struct request *req = mmc_queue_req_to_req(mqrq);
88 	struct request_queue *q = req->q;
89 	struct mmc_queue *mq = q->queuedata;
90 	unsigned long flags;
91 
92 	spin_lock_irqsave(q->queue_lock, flags);
93 	__mmc_cqe_recovery_notifier(mq);
94 	spin_unlock_irqrestore(q->queue_lock, flags);
95 }
96 
mmc_cqe_timed_out(struct request * req)97 static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
98 {
99 	struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
100 	struct mmc_request *mrq = &mqrq->brq.mrq;
101 	struct mmc_queue *mq = req->q->queuedata;
102 	struct mmc_host *host = mq->card->host;
103 	enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
104 	bool recovery_needed = false;
105 
106 	switch (issue_type) {
107 	case MMC_ISSUE_ASYNC:
108 	case MMC_ISSUE_DCMD:
109 		if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
110 			if (recovery_needed)
111 				mmc_cqe_recovery_notifier(mrq);
112 			return BLK_EH_RESET_TIMER;
113 		}
114 		/* The request has gone already */
115 		return BLK_EH_DONE;
116 	default:
117 		/* Timeout is handled by mmc core */
118 		return BLK_EH_RESET_TIMER;
119 	}
120 }
121 
mmc_mq_timed_out(struct request * req,bool reserved)122 static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
123 						 bool reserved)
124 {
125 	struct request_queue *q = req->q;
126 	struct mmc_queue *mq = q->queuedata;
127 	unsigned long flags;
128 	bool ignore_tout;
129 
130 	spin_lock_irqsave(q->queue_lock, flags);
131 	ignore_tout = mq->recovery_needed || !mq->use_cqe;
132 	spin_unlock_irqrestore(q->queue_lock, flags);
133 
134 	return ignore_tout ? BLK_EH_RESET_TIMER : mmc_cqe_timed_out(req);
135 }
136 
mmc_mq_recovery_handler(struct work_struct * work)137 static void mmc_mq_recovery_handler(struct work_struct *work)
138 {
139 	struct mmc_queue *mq = container_of(work, struct mmc_queue,
140 					    recovery_work);
141 	struct request_queue *q = mq->queue;
142 
143 	mmc_get_card(mq->card, &mq->ctx);
144 
145 	mq->in_recovery = true;
146 
147 	if (mq->use_cqe)
148 		mmc_blk_cqe_recovery(mq);
149 	else
150 		mmc_blk_mq_recovery(mq);
151 
152 	mq->in_recovery = false;
153 
154 	spin_lock_irq(q->queue_lock);
155 	mq->recovery_needed = false;
156 	spin_unlock_irq(q->queue_lock);
157 
158 	mmc_put_card(mq->card, &mq->ctx);
159 
160 	blk_mq_run_hw_queues(q, true);
161 }
162 
mmc_alloc_sg(int sg_len,gfp_t gfp)163 static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
164 {
165 	struct scatterlist *sg;
166 
167 	sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
168 	if (sg)
169 		sg_init_table(sg, sg_len);
170 
171 	return sg;
172 }
173 
mmc_queue_setup_discard(struct request_queue * q,struct mmc_card * card)174 static void mmc_queue_setup_discard(struct request_queue *q,
175 				    struct mmc_card *card)
176 {
177 	unsigned max_discard;
178 
179 	max_discard = mmc_calc_max_discard(card);
180 	if (!max_discard)
181 		return;
182 
183 	blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
184 	blk_queue_max_discard_sectors(q, max_discard);
185 	q->limits.discard_granularity = card->pref_erase << 9;
186 	/* granularity must not be greater than max. discard */
187 	if (card->pref_erase > max_discard)
188 		q->limits.discard_granularity = SECTOR_SIZE;
189 	if (mmc_can_secure_erase_trim(card))
190 		blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
191 }
192 
193 /**
194  * mmc_init_request() - initialize the MMC-specific per-request data
195  * @q: the request queue
196  * @req: the request
197  * @gfp: memory allocation policy
198  */
__mmc_init_request(struct mmc_queue * mq,struct request * req,gfp_t gfp)199 static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
200 			      gfp_t gfp)
201 {
202 	struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
203 	struct mmc_card *card = mq->card;
204 	struct mmc_host *host = card->host;
205 
206 	mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
207 	if (!mq_rq->sg)
208 		return -ENOMEM;
209 
210 	return 0;
211 }
212 
mmc_exit_request(struct request_queue * q,struct request * req)213 static void mmc_exit_request(struct request_queue *q, struct request *req)
214 {
215 	struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
216 
217 	kfree(mq_rq->sg);
218 	mq_rq->sg = NULL;
219 }
220 
mmc_mq_init_request(struct blk_mq_tag_set * set,struct request * req,unsigned int hctx_idx,unsigned int numa_node)221 static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
222 			       unsigned int hctx_idx, unsigned int numa_node)
223 {
224 	return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
225 }
226 
mmc_mq_exit_request(struct blk_mq_tag_set * set,struct request * req,unsigned int hctx_idx)227 static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
228 				unsigned int hctx_idx)
229 {
230 	struct mmc_queue *mq = set->driver_data;
231 
232 	mmc_exit_request(mq->queue, req);
233 }
234 
mmc_mq_queue_rq(struct blk_mq_hw_ctx * hctx,const struct blk_mq_queue_data * bd)235 static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
236 				    const struct blk_mq_queue_data *bd)
237 {
238 	struct request *req = bd->rq;
239 	struct request_queue *q = req->q;
240 	struct mmc_queue *mq = q->queuedata;
241 	struct mmc_card *card = mq->card;
242 	struct mmc_host *host = card->host;
243 	enum mmc_issue_type issue_type;
244 	enum mmc_issued issued;
245 	bool get_card, cqe_retune_ok;
246 	int ret;
247 
248 	if (mmc_card_removed(mq->card)) {
249 		req->rq_flags |= RQF_QUIET;
250 		return BLK_STS_IOERR;
251 	}
252 
253 	issue_type = mmc_issue_type(mq, req);
254 
255 	spin_lock_irq(q->queue_lock);
256 
257 	if (mq->recovery_needed || mq->busy) {
258 		spin_unlock_irq(q->queue_lock);
259 		return BLK_STS_RESOURCE;
260 	}
261 
262 	switch (issue_type) {
263 	case MMC_ISSUE_DCMD:
264 		if (mmc_cqe_dcmd_busy(mq)) {
265 			mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
266 			spin_unlock_irq(q->queue_lock);
267 			return BLK_STS_RESOURCE;
268 		}
269 		break;
270 	case MMC_ISSUE_ASYNC:
271 		break;
272 	default:
273 		/*
274 		 * Timeouts are handled by mmc core, and we don't have a host
275 		 * API to abort requests, so we can't handle the timeout anyway.
276 		 * However, when the timeout happens, blk_mq_complete_request()
277 		 * no longer works (to stop the request disappearing under us).
278 		 * To avoid racing with that, set a large timeout.
279 		 */
280 		req->timeout = 600 * HZ;
281 		break;
282 	}
283 
284 	/* Parallel dispatch of requests is not supported at the moment */
285 	mq->busy = true;
286 
287 	mq->in_flight[issue_type] += 1;
288 	get_card = (mmc_tot_in_flight(mq) == 1);
289 	cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
290 
291 	spin_unlock_irq(q->queue_lock);
292 
293 	if (!(req->rq_flags & RQF_DONTPREP)) {
294 		req_to_mmc_queue_req(req)->retries = 0;
295 		req->rq_flags |= RQF_DONTPREP;
296 	}
297 
298 	if (get_card)
299 		mmc_get_card(card, &mq->ctx);
300 
301 	if (mq->use_cqe) {
302 		host->retune_now = host->need_retune && cqe_retune_ok &&
303 				   !host->hold_retune;
304 	}
305 
306 	blk_mq_start_request(req);
307 
308 	issued = mmc_blk_mq_issue_rq(mq, req);
309 
310 	switch (issued) {
311 	case MMC_REQ_BUSY:
312 		ret = BLK_STS_RESOURCE;
313 		break;
314 	case MMC_REQ_FAILED_TO_START:
315 		ret = BLK_STS_IOERR;
316 		break;
317 	default:
318 		ret = BLK_STS_OK;
319 		break;
320 	}
321 
322 	if (issued != MMC_REQ_STARTED) {
323 		bool put_card = false;
324 
325 		spin_lock_irq(q->queue_lock);
326 		mq->in_flight[issue_type] -= 1;
327 		if (mmc_tot_in_flight(mq) == 0)
328 			put_card = true;
329 		mq->busy = false;
330 		spin_unlock_irq(q->queue_lock);
331 		if (put_card)
332 			mmc_put_card(card, &mq->ctx);
333 	} else {
334 		WRITE_ONCE(mq->busy, false);
335 	}
336 
337 	return ret;
338 }
339 
340 static const struct blk_mq_ops mmc_mq_ops = {
341 	.queue_rq	= mmc_mq_queue_rq,
342 	.init_request	= mmc_mq_init_request,
343 	.exit_request	= mmc_mq_exit_request,
344 	.complete	= mmc_blk_mq_complete,
345 	.timeout	= mmc_mq_timed_out,
346 };
347 
mmc_setup_queue(struct mmc_queue * mq,struct mmc_card * card)348 static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
349 {
350 	struct mmc_host *host = card->host;
351 	u64 limit = BLK_BOUNCE_HIGH;
352 	unsigned block_size = 512;
353 
354 	if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
355 		limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
356 
357 	blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
358 	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
359 	if (mmc_can_erase(card))
360 		mmc_queue_setup_discard(mq->queue, card);
361 
362 	blk_queue_bounce_limit(mq->queue, limit);
363 	blk_queue_max_hw_sectors(mq->queue,
364 		min(host->max_blk_count, host->max_req_size / 512));
365 	blk_queue_max_segments(mq->queue, host->max_segs);
366 
367 	if (mmc_card_mmc(card))
368 		block_size = card->ext_csd.data_sector_size;
369 
370 	blk_queue_logical_block_size(mq->queue, block_size);
371 	blk_queue_max_segment_size(mq->queue,
372 			round_down(host->max_seg_size, block_size));
373 
374 	INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
375 	INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
376 
377 	mutex_init(&mq->complete_lock);
378 
379 	init_waitqueue_head(&mq->wait);
380 }
381 
mmc_mq_init_queue(struct mmc_queue * mq,int q_depth,const struct blk_mq_ops * mq_ops,spinlock_t * lock)382 static int mmc_mq_init_queue(struct mmc_queue *mq, int q_depth,
383 			     const struct blk_mq_ops *mq_ops, spinlock_t *lock)
384 {
385 	int ret;
386 
387 	memset(&mq->tag_set, 0, sizeof(mq->tag_set));
388 	mq->tag_set.ops = mq_ops;
389 	mq->tag_set.queue_depth = q_depth;
390 	mq->tag_set.numa_node = NUMA_NO_NODE;
391 	mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE |
392 			    BLK_MQ_F_BLOCKING;
393 	mq->tag_set.nr_hw_queues = 1;
394 	mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
395 	mq->tag_set.driver_data = mq;
396 
397 	ret = blk_mq_alloc_tag_set(&mq->tag_set);
398 	if (ret)
399 		return ret;
400 
401 	mq->queue = blk_mq_init_queue(&mq->tag_set);
402 	if (IS_ERR(mq->queue)) {
403 		ret = PTR_ERR(mq->queue);
404 		goto free_tag_set;
405 	}
406 
407 	mq->queue->queue_lock = lock;
408 	mq->queue->queuedata = mq;
409 
410 	return 0;
411 
412 free_tag_set:
413 	blk_mq_free_tag_set(&mq->tag_set);
414 
415 	return ret;
416 }
417 
418 /* Set queue depth to get a reasonable value for q->nr_requests */
419 #define MMC_QUEUE_DEPTH 64
420 
mmc_mq_init(struct mmc_queue * mq,struct mmc_card * card,spinlock_t * lock)421 static int mmc_mq_init(struct mmc_queue *mq, struct mmc_card *card,
422 			 spinlock_t *lock)
423 {
424 	struct mmc_host *host = card->host;
425 	int q_depth;
426 	int ret;
427 
428 	/*
429 	 * The queue depth for CQE must match the hardware because the request
430 	 * tag is used to index the hardware queue.
431 	 */
432 	if (mq->use_cqe)
433 		q_depth = min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
434 	else
435 		q_depth = MMC_QUEUE_DEPTH;
436 
437 	ret = mmc_mq_init_queue(mq, q_depth, &mmc_mq_ops, lock);
438 	if (ret)
439 		return ret;
440 
441 	blk_queue_rq_timeout(mq->queue, 60 * HZ);
442 
443 	mmc_setup_queue(mq, card);
444 
445 	return 0;
446 }
447 
448 /**
449  * mmc_init_queue - initialise a queue structure.
450  * @mq: mmc queue
451  * @card: mmc card to attach this queue
452  * @lock: queue lock
453  * @subname: partition subname
454  *
455  * Initialise a MMC card request queue.
456  */
mmc_init_queue(struct mmc_queue * mq,struct mmc_card * card,spinlock_t * lock,const char * subname)457 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
458 		   spinlock_t *lock, const char *subname)
459 {
460 	struct mmc_host *host = card->host;
461 
462 	mq->card = card;
463 
464 	mq->use_cqe = host->cqe_enabled;
465 
466 	return mmc_mq_init(mq, card, lock);
467 }
468 
mmc_queue_suspend(struct mmc_queue * mq)469 void mmc_queue_suspend(struct mmc_queue *mq)
470 {
471 	blk_mq_quiesce_queue(mq->queue);
472 
473 	/*
474 	 * The host remains claimed while there are outstanding requests, so
475 	 * simply claiming and releasing here ensures there are none.
476 	 */
477 	mmc_claim_host(mq->card->host);
478 	mmc_release_host(mq->card->host);
479 }
480 
mmc_queue_resume(struct mmc_queue * mq)481 void mmc_queue_resume(struct mmc_queue *mq)
482 {
483 	blk_mq_unquiesce_queue(mq->queue);
484 }
485 
mmc_cleanup_queue(struct mmc_queue * mq)486 void mmc_cleanup_queue(struct mmc_queue *mq)
487 {
488 	struct request_queue *q = mq->queue;
489 
490 	/*
491 	 * The legacy code handled the possibility of being suspended,
492 	 * so do that here too.
493 	 */
494 	if (blk_queue_quiesced(q))
495 		blk_mq_unquiesce_queue(q);
496 
497 	blk_cleanup_queue(q);
498 	blk_mq_free_tag_set(&mq->tag_set);
499 
500 	/*
501 	 * A request can be completed before the next request, potentially
502 	 * leaving a complete_work with nothing to do. Such a work item might
503 	 * still be queued at this point. Flush it.
504 	 */
505 	flush_work(&mq->complete_work);
506 
507 	mq->card = NULL;
508 }
509 
510 /*
511  * Prepare the sg list(s) to be handed of to the host driver
512  */
mmc_queue_map_sg(struct mmc_queue * mq,struct mmc_queue_req * mqrq)513 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
514 {
515 	struct request *req = mmc_queue_req_to_req(mqrq);
516 
517 	return blk_rq_map_sg(mq->queue, req, mqrq->sg);
518 }
519