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