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