xref: /openbmc/linux/drivers/mmc/core/queue.c (revision 6d99a79c)
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 
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 
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 
41 static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
42 {
43 	return host->caps2 & MMC_CAP2_CQE_DCMD;
44 }
45 
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 
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 
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 
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 
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(mq);
112 			return BLK_EH_RESET_TIMER;
113 		}
114 		/* No timeout (XXX: huh? comment doesn't make much sense) */
115 		blk_mq_complete_request(req);
116 		return BLK_EH_DONE;
117 	default:
118 		/* Timeout is handled by mmc core */
119 		return BLK_EH_RESET_TIMER;
120 	}
121 }
122 
123 static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
124 						 bool reserved)
125 {
126 	struct request_queue *q = req->q;
127 	struct mmc_queue *mq = q->queuedata;
128 	unsigned long flags;
129 	int ret;
130 
131 	spin_lock_irqsave(q->queue_lock, flags);
132 
133 	if (mq->recovery_needed || !mq->use_cqe)
134 		ret = BLK_EH_RESET_TIMER;
135 	else
136 		ret = mmc_cqe_timed_out(req);
137 
138 	spin_unlock_irqrestore(q->queue_lock, flags);
139 
140 	return ret;
141 }
142 
143 static void mmc_mq_recovery_handler(struct work_struct *work)
144 {
145 	struct mmc_queue *mq = container_of(work, struct mmc_queue,
146 					    recovery_work);
147 	struct request_queue *q = mq->queue;
148 
149 	mmc_get_card(mq->card, &mq->ctx);
150 
151 	mq->in_recovery = true;
152 
153 	if (mq->use_cqe)
154 		mmc_blk_cqe_recovery(mq);
155 	else
156 		mmc_blk_mq_recovery(mq);
157 
158 	mq->in_recovery = false;
159 
160 	spin_lock_irq(q->queue_lock);
161 	mq->recovery_needed = false;
162 	spin_unlock_irq(q->queue_lock);
163 
164 	mmc_put_card(mq->card, &mq->ctx);
165 
166 	blk_mq_run_hw_queues(q, true);
167 }
168 
169 static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
170 {
171 	struct scatterlist *sg;
172 
173 	sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
174 	if (sg)
175 		sg_init_table(sg, sg_len);
176 
177 	return sg;
178 }
179 
180 static void mmc_queue_setup_discard(struct request_queue *q,
181 				    struct mmc_card *card)
182 {
183 	unsigned max_discard;
184 
185 	max_discard = mmc_calc_max_discard(card);
186 	if (!max_discard)
187 		return;
188 
189 	blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
190 	blk_queue_max_discard_sectors(q, max_discard);
191 	q->limits.discard_granularity = card->pref_erase << 9;
192 	/* granularity must not be greater than max. discard */
193 	if (card->pref_erase > max_discard)
194 		q->limits.discard_granularity = 0;
195 	if (mmc_can_secure_erase_trim(card))
196 		blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
197 }
198 
199 /**
200  * mmc_init_request() - initialize the MMC-specific per-request data
201  * @q: the request queue
202  * @req: the request
203  * @gfp: memory allocation policy
204  */
205 static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
206 			      gfp_t gfp)
207 {
208 	struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
209 	struct mmc_card *card = mq->card;
210 	struct mmc_host *host = card->host;
211 
212 	mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
213 	if (!mq_rq->sg)
214 		return -ENOMEM;
215 
216 	return 0;
217 }
218 
219 static void mmc_exit_request(struct request_queue *q, struct request *req)
220 {
221 	struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
222 
223 	kfree(mq_rq->sg);
224 	mq_rq->sg = NULL;
225 }
226 
227 static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
228 			       unsigned int hctx_idx, unsigned int numa_node)
229 {
230 	return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
231 }
232 
233 static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
234 				unsigned int hctx_idx)
235 {
236 	struct mmc_queue *mq = set->driver_data;
237 
238 	mmc_exit_request(mq->queue, req);
239 }
240 
241 static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
242 				    const struct blk_mq_queue_data *bd)
243 {
244 	struct request *req = bd->rq;
245 	struct request_queue *q = req->q;
246 	struct mmc_queue *mq = q->queuedata;
247 	struct mmc_card *card = mq->card;
248 	struct mmc_host *host = card->host;
249 	enum mmc_issue_type issue_type;
250 	enum mmc_issued issued;
251 	bool get_card, cqe_retune_ok;
252 	int ret;
253 
254 	if (mmc_card_removed(mq->card)) {
255 		req->rq_flags |= RQF_QUIET;
256 		return BLK_STS_IOERR;
257 	}
258 
259 	issue_type = mmc_issue_type(mq, req);
260 
261 	spin_lock_irq(q->queue_lock);
262 
263 	if (mq->recovery_needed || mq->busy) {
264 		spin_unlock_irq(q->queue_lock);
265 		return BLK_STS_RESOURCE;
266 	}
267 
268 	switch (issue_type) {
269 	case MMC_ISSUE_DCMD:
270 		if (mmc_cqe_dcmd_busy(mq)) {
271 			mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
272 			spin_unlock_irq(q->queue_lock);
273 			return BLK_STS_RESOURCE;
274 		}
275 		break;
276 	case MMC_ISSUE_ASYNC:
277 		break;
278 	default:
279 		/*
280 		 * Timeouts are handled by mmc core, and we don't have a host
281 		 * API to abort requests, so we can't handle the timeout anyway.
282 		 * However, when the timeout happens, blk_mq_complete_request()
283 		 * no longer works (to stop the request disappearing under us).
284 		 * To avoid racing with that, set a large timeout.
285 		 */
286 		req->timeout = 600 * HZ;
287 		break;
288 	}
289 
290 	/* Parallel dispatch of requests is not supported at the moment */
291 	mq->busy = true;
292 
293 	mq->in_flight[issue_type] += 1;
294 	get_card = (mmc_tot_in_flight(mq) == 1);
295 	cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
296 
297 	spin_unlock_irq(q->queue_lock);
298 
299 	if (!(req->rq_flags & RQF_DONTPREP)) {
300 		req_to_mmc_queue_req(req)->retries = 0;
301 		req->rq_flags |= RQF_DONTPREP;
302 	}
303 
304 	if (get_card)
305 		mmc_get_card(card, &mq->ctx);
306 
307 	if (mq->use_cqe) {
308 		host->retune_now = host->need_retune && cqe_retune_ok &&
309 				   !host->hold_retune;
310 	}
311 
312 	blk_mq_start_request(req);
313 
314 	issued = mmc_blk_mq_issue_rq(mq, req);
315 
316 	switch (issued) {
317 	case MMC_REQ_BUSY:
318 		ret = BLK_STS_RESOURCE;
319 		break;
320 	case MMC_REQ_FAILED_TO_START:
321 		ret = BLK_STS_IOERR;
322 		break;
323 	default:
324 		ret = BLK_STS_OK;
325 		break;
326 	}
327 
328 	if (issued != MMC_REQ_STARTED) {
329 		bool put_card = false;
330 
331 		spin_lock_irq(q->queue_lock);
332 		mq->in_flight[issue_type] -= 1;
333 		if (mmc_tot_in_flight(mq) == 0)
334 			put_card = true;
335 		mq->busy = false;
336 		spin_unlock_irq(q->queue_lock);
337 		if (put_card)
338 			mmc_put_card(card, &mq->ctx);
339 	} else {
340 		WRITE_ONCE(mq->busy, false);
341 	}
342 
343 	return ret;
344 }
345 
346 static const struct blk_mq_ops mmc_mq_ops = {
347 	.queue_rq	= mmc_mq_queue_rq,
348 	.init_request	= mmc_mq_init_request,
349 	.exit_request	= mmc_mq_exit_request,
350 	.complete	= mmc_blk_mq_complete,
351 	.timeout	= mmc_mq_timed_out,
352 };
353 
354 static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
355 {
356 	struct mmc_host *host = card->host;
357 	u64 limit = BLK_BOUNCE_HIGH;
358 
359 	if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
360 		limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
361 
362 	blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
363 	blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
364 	if (mmc_can_erase(card))
365 		mmc_queue_setup_discard(mq->queue, card);
366 
367 	blk_queue_bounce_limit(mq->queue, limit);
368 	blk_queue_max_hw_sectors(mq->queue,
369 		min(host->max_blk_count, host->max_req_size / 512));
370 	blk_queue_max_segments(mq->queue, host->max_segs);
371 	blk_queue_max_segment_size(mq->queue, host->max_seg_size);
372 
373 	INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
374 	INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
375 
376 	mutex_init(&mq->complete_lock);
377 
378 	init_waitqueue_head(&mq->wait);
379 }
380 
381 static int mmc_mq_init_queue(struct mmc_queue *mq, int q_depth,
382 			     const struct blk_mq_ops *mq_ops, spinlock_t *lock)
383 {
384 	int ret;
385 
386 	memset(&mq->tag_set, 0, sizeof(mq->tag_set));
387 	mq->tag_set.ops = mq_ops;
388 	mq->tag_set.queue_depth = q_depth;
389 	mq->tag_set.numa_node = NUMA_NO_NODE;
390 	mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE |
391 			    BLK_MQ_F_BLOCKING;
392 	mq->tag_set.nr_hw_queues = 1;
393 	mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
394 	mq->tag_set.driver_data = mq;
395 
396 	ret = blk_mq_alloc_tag_set(&mq->tag_set);
397 	if (ret)
398 		return ret;
399 
400 	mq->queue = blk_mq_init_queue(&mq->tag_set);
401 	if (IS_ERR(mq->queue)) {
402 		ret = PTR_ERR(mq->queue);
403 		goto free_tag_set;
404 	}
405 
406 	mq->queue->queue_lock = lock;
407 	mq->queue->queuedata = mq;
408 
409 	return 0;
410 
411 free_tag_set:
412 	blk_mq_free_tag_set(&mq->tag_set);
413 
414 	return ret;
415 }
416 
417 /* Set queue depth to get a reasonable value for q->nr_requests */
418 #define MMC_QUEUE_DEPTH 64
419 
420 static int mmc_mq_init(struct mmc_queue *mq, struct mmc_card *card,
421 			 spinlock_t *lock)
422 {
423 	struct mmc_host *host = card->host;
424 	int q_depth;
425 	int ret;
426 
427 	/*
428 	 * The queue depth for CQE must match the hardware because the request
429 	 * tag is used to index the hardware queue.
430 	 */
431 	if (mq->use_cqe)
432 		q_depth = min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
433 	else
434 		q_depth = MMC_QUEUE_DEPTH;
435 
436 	ret = mmc_mq_init_queue(mq, q_depth, &mmc_mq_ops, lock);
437 	if (ret)
438 		return ret;
439 
440 	blk_queue_rq_timeout(mq->queue, 60 * HZ);
441 
442 	mmc_setup_queue(mq, card);
443 
444 	return 0;
445 }
446 
447 /**
448  * mmc_init_queue - initialise a queue structure.
449  * @mq: mmc queue
450  * @card: mmc card to attach this queue
451  * @lock: queue lock
452  * @subname: partition subname
453  *
454  * Initialise a MMC card request queue.
455  */
456 int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
457 		   spinlock_t *lock, const char *subname)
458 {
459 	struct mmc_host *host = card->host;
460 
461 	mq->card = card;
462 
463 	mq->use_cqe = host->cqe_enabled;
464 
465 	return mmc_mq_init(mq, card, lock);
466 }
467 
468 void mmc_queue_suspend(struct mmc_queue *mq)
469 {
470 	blk_mq_quiesce_queue(mq->queue);
471 
472 	/*
473 	 * The host remains claimed while there are outstanding requests, so
474 	 * simply claiming and releasing here ensures there are none.
475 	 */
476 	mmc_claim_host(mq->card->host);
477 	mmc_release_host(mq->card->host);
478 }
479 
480 void mmc_queue_resume(struct mmc_queue *mq)
481 {
482 	blk_mq_unquiesce_queue(mq->queue);
483 }
484 
485 void mmc_cleanup_queue(struct mmc_queue *mq)
486 {
487 	struct request_queue *q = mq->queue;
488 
489 	/*
490 	 * The legacy code handled the possibility of being suspended,
491 	 * so do that here too.
492 	 */
493 	if (blk_queue_quiesced(q))
494 		blk_mq_unquiesce_queue(q);
495 
496 	blk_cleanup_queue(q);
497 
498 	/*
499 	 * A request can be completed before the next request, potentially
500 	 * leaving a complete_work with nothing to do. Such a work item might
501 	 * still be queued at this point. Flush it.
502 	 */
503 	flush_work(&mq->complete_work);
504 
505 	mq->card = NULL;
506 }
507 
508 /*
509  * Prepare the sg list(s) to be handed of to the host driver
510  */
511 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
512 {
513 	struct request *req = mmc_queue_req_to_req(mqrq);
514 
515 	return blk_rq_map_sg(mq->queue, req, mqrq->sg);
516 }
517