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