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 #include <trace/hooks/mmc.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
mmc_cqe_can_dcmd(struct mmc_host * host)39 static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
40 {
41 return host->caps2 & MMC_CAP2_CQE_DCMD;
42 }
43
mmc_cqe_issue_type(struct mmc_host * host,struct request * req)44 static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
45 struct request *req)
46 {
47 switch (req_op(req)) {
48 case REQ_OP_DRV_IN:
49 case REQ_OP_DRV_OUT:
50 case REQ_OP_DISCARD:
51 case REQ_OP_SECURE_ERASE:
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,bool reserved)121 static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
122 bool reserved)
123 {
124 struct request_queue *q = req->q;
125 struct mmc_queue *mq = q->queuedata;
126 struct mmc_card *card = mq->card;
127 struct mmc_host *host = card->host;
128 unsigned long flags;
129 bool ignore_tout;
130
131 spin_lock_irqsave(&mq->lock, flags);
132 ignore_tout = mq->recovery_needed || !host->cqe_enabled || host->hsq_enabled;
133 spin_unlock_irqrestore(&mq->lock, flags);
134
135 return ignore_tout ? BLK_EH_RESET_TIMER : mmc_cqe_timed_out(req);
136 }
137
mmc_mq_recovery_handler(struct work_struct * work)138 static void mmc_mq_recovery_handler(struct work_struct *work)
139 {
140 struct mmc_queue *mq = container_of(work, struct mmc_queue,
141 recovery_work);
142 struct request_queue *q = mq->queue;
143 struct mmc_host *host = mq->card->host;
144
145 mmc_get_card(mq->card, &mq->ctx);
146
147 mq->in_recovery = true;
148
149 if (host->cqe_enabled && !host->hsq_enabled)
150 mmc_blk_cqe_recovery(mq);
151 else
152 mmc_blk_mq_recovery(mq);
153
154 mq->in_recovery = false;
155
156 spin_lock_irq(&mq->lock);
157 mq->recovery_needed = false;
158 spin_unlock_irq(&mq->lock);
159
160 if (host->hsq_enabled)
161 host->cqe_ops->cqe_recovery_finish(host);
162
163 mmc_put_card(mq->card, &mq->ctx);
164
165 blk_mq_run_hw_queues(q, true);
166 }
167
mmc_alloc_sg(unsigned short sg_len,gfp_t gfp)168 static struct scatterlist *mmc_alloc_sg(unsigned short sg_len, gfp_t gfp)
169 {
170 struct scatterlist *sg;
171
172 sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
173 if (sg)
174 sg_init_table(sg, sg_len);
175
176 return sg;
177 }
178
mmc_queue_setup_discard(struct request_queue * q,struct mmc_card * card)179 static void mmc_queue_setup_discard(struct request_queue *q,
180 struct mmc_card *card)
181 {
182 unsigned max_discard;
183
184 max_discard = mmc_calc_max_discard(card);
185 if (!max_discard)
186 return;
187
188 blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
189 blk_queue_max_discard_sectors(q, max_discard);
190 q->limits.discard_granularity = card->pref_erase << 9;
191 /* granularity must not be greater than max. discard */
192 if (card->pref_erase > max_discard)
193 q->limits.discard_granularity = SECTOR_SIZE;
194 if (mmc_can_secure_erase_trim(card))
195 blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
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 int ret = 0;
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 trace_android_vh_mmc_check_status(bd, &ret);
269 if (!ret && host->hsq_enabled && mq->in_flight[issue_type] > 2) {
270 spin_unlock_irq(&mq->lock);
271 return BLK_STS_RESOURCE;
272 }
273 break;
274 default:
275 /*
276 * Timeouts are handled by mmc core, and we don't have a host
277 * API to abort requests, so we can't handle the timeout anyway.
278 * However, when the timeout happens, blk_mq_complete_request()
279 * no longer works (to stop the request disappearing under us).
280 * To avoid racing with that, set a large timeout.
281 */
282 req->timeout = 600 * HZ;
283 break;
284 }
285
286 /* Parallel dispatch of requests is not supported at the moment */
287 mq->busy = true;
288
289 mq->in_flight[issue_type] += 1;
290 get_card = (mmc_tot_in_flight(mq) == 1);
291 cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
292
293 spin_unlock_irq(&mq->lock);
294
295 if (!(req->rq_flags & RQF_DONTPREP)) {
296 req_to_mmc_queue_req(req)->retries = 0;
297 req->rq_flags |= RQF_DONTPREP;
298 }
299
300 if (get_card)
301 mmc_get_card(card, &mq->ctx);
302
303 if (host->cqe_enabled) {
304 host->retune_now = host->need_retune && cqe_retune_ok &&
305 !host->hold_retune;
306 }
307
308 blk_mq_start_request(req);
309
310 issued = mmc_blk_mq_issue_rq(mq, req);
311
312 switch (issued) {
313 case MMC_REQ_BUSY:
314 ret = BLK_STS_RESOURCE;
315 break;
316 case MMC_REQ_FAILED_TO_START:
317 ret = BLK_STS_IOERR;
318 break;
319 default:
320 ret = BLK_STS_OK;
321 break;
322 }
323
324 if (issued != MMC_REQ_STARTED) {
325 bool put_card = false;
326
327 spin_lock_irq(&mq->lock);
328 mq->in_flight[issue_type] -= 1;
329 if (mmc_tot_in_flight(mq) == 0)
330 put_card = true;
331 mq->busy = false;
332 spin_unlock_irq(&mq->lock);
333 if (put_card)
334 mmc_put_card(card, &mq->ctx);
335 } else {
336 WRITE_ONCE(mq->busy, false);
337 }
338
339 return ret;
340 }
341
342 static const struct blk_mq_ops mmc_mq_ops = {
343 .queue_rq = mmc_mq_queue_rq,
344 .init_request = mmc_mq_init_request,
345 .exit_request = mmc_mq_exit_request,
346 .complete = mmc_blk_mq_complete,
347 .timeout = mmc_mq_timed_out,
348 };
349
mmc_setup_queue(struct mmc_queue * mq,struct mmc_card * card)350 static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
351 {
352 struct mmc_host *host = card->host;
353 unsigned block_size = 512;
354
355 blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
356 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
357 if (mmc_can_erase(card))
358 mmc_queue_setup_discard(mq->queue, card);
359
360 if (!mmc_dev(host)->dma_mask || !*mmc_dev(host)->dma_mask)
361 blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH);
362 blk_queue_max_hw_sectors(mq->queue,
363 min(host->max_blk_count, host->max_req_size / 512));
364 if (host->can_dma_map_merge)
365 WARN(!blk_queue_can_use_dma_map_merging(mq->queue,
366 mmc_dev(host)),
367 "merging was advertised but not possible");
368 blk_queue_max_segments(mq->queue, mmc_get_max_segments(host));
369
370 if (mmc_card_mmc(card) && card->ext_csd.data_sector_size) {
371 block_size = card->ext_csd.data_sector_size;
372 WARN_ON(block_size != 512 && block_size != 4096);
373 }
374
375 blk_queue_logical_block_size(mq->queue, block_size);
376 /*
377 * After blk_queue_can_use_dma_map_merging() was called with succeed,
378 * since it calls blk_queue_virt_boundary(), the mmc should not call
379 * both blk_queue_max_segment_size().
380 */
381 if (!host->can_dma_map_merge)
382 blk_queue_max_segment_size(mq->queue,
383 round_down(host->max_seg_size, block_size));
384
385 dma_set_max_seg_size(mmc_dev(host), queue_max_segment_size(mq->queue));
386
387 INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
388 INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
389
390 mutex_init(&mq->complete_lock);
391
392 init_waitqueue_head(&mq->wait);
393
394 mmc_crypto_setup_queue(mq->queue, host);
395 }
396
mmc_merge_capable(struct mmc_host * host)397 static inline bool mmc_merge_capable(struct mmc_host *host)
398 {
399 return host->caps2 & MMC_CAP2_MERGE_CAPABLE;
400 }
401
402 /* Set queue depth to get a reasonable value for q->nr_requests */
403 #define MMC_QUEUE_DEPTH 64
404
405 /**
406 * mmc_init_queue - initialise a queue structure.
407 * @mq: mmc queue
408 * @card: mmc card to attach this queue
409 *
410 * Initialise a MMC card request queue.
411 */
mmc_init_queue(struct mmc_queue * mq,struct mmc_card * card)412 struct gendisk *mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card)
413 {
414 struct mmc_host *host = card->host;
415 struct gendisk *disk;
416 int ret;
417
418 mq->card = card;
419
420 spin_lock_init(&mq->lock);
421
422 memset(&mq->tag_set, 0, sizeof(mq->tag_set));
423 mq->tag_set.ops = &mmc_mq_ops;
424 /*
425 * The queue depth for CQE must match the hardware because the request
426 * tag is used to index the hardware queue.
427 */
428 if (host->cqe_enabled && !host->hsq_enabled)
429 mq->tag_set.queue_depth =
430 min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
431 else
432 mq->tag_set.queue_depth = MMC_QUEUE_DEPTH;
433 mq->tag_set.numa_node = NUMA_NO_NODE;
434 mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
435 mq->tag_set.nr_hw_queues = 1;
436 mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
437 mq->tag_set.driver_data = mq;
438
439 /*
440 * Since blk_mq_alloc_tag_set() calls .init_request() of mmc_mq_ops,
441 * the host->can_dma_map_merge should be set before to get max_segs
442 * from mmc_get_max_segments().
443 */
444 if (mmc_merge_capable(host) &&
445 host->max_segs < MMC_DMA_MAP_MERGE_SEGMENTS &&
446 dma_get_merge_boundary(mmc_dev(host)))
447 host->can_dma_map_merge = 1;
448 else
449 host->can_dma_map_merge = 0;
450
451 ret = blk_mq_alloc_tag_set(&mq->tag_set);
452 if (ret)
453 return ERR_PTR(ret);
454
455
456 disk = blk_mq_alloc_disk(&mq->tag_set, mq);
457 if (IS_ERR(disk)) {
458 blk_mq_free_tag_set(&mq->tag_set);
459 return disk;
460 }
461 mq->queue = disk->queue;
462
463 if (mmc_host_is_spi(host) && host->use_spi_crc)
464 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, mq->queue);
465 blk_queue_rq_timeout(mq->queue, 60 * HZ);
466
467 mmc_setup_queue(mq, card);
468 return disk;
469 }
470
mmc_queue_suspend(struct mmc_queue * mq)471 void mmc_queue_suspend(struct mmc_queue *mq)
472 {
473 blk_mq_quiesce_queue(mq->queue);
474
475 /*
476 * The host remains claimed while there are outstanding requests, so
477 * simply claiming and releasing here ensures there are none.
478 */
479 mmc_claim_host(mq->card->host);
480 mmc_release_host(mq->card->host);
481 }
482
mmc_queue_resume(struct mmc_queue * mq)483 void mmc_queue_resume(struct mmc_queue *mq)
484 {
485 blk_mq_unquiesce_queue(mq->queue);
486 }
487
mmc_cleanup_queue(struct mmc_queue * mq)488 void mmc_cleanup_queue(struct mmc_queue *mq)
489 {
490 struct request_queue *q = mq->queue;
491
492 /*
493 * The legacy code handled the possibility of being suspended,
494 * so do that here too.
495 */
496 if (blk_queue_quiesced(q))
497 blk_mq_unquiesce_queue(q);
498
499 blk_cleanup_queue(q);
500 blk_mq_free_tag_set(&mq->tag_set);
501
502 /*
503 * A request can be completed before the next request, potentially
504 * leaving a complete_work with nothing to do. Such a work item might
505 * still be queued at this point. Flush it.
506 */
507 flush_work(&mq->complete_work);
508
509 mq->card = NULL;
510 }
511
512 /*
513 * Prepare the sg list(s) to be handed of to the host driver
514 */
mmc_queue_map_sg(struct mmc_queue * mq,struct mmc_queue_req * mqrq)515 unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
516 {
517 struct request *req = mmc_queue_req_to_req(mqrq);
518
519 return blk_rq_map_sg(mq->queue, req, mqrq->sg);
520 }
521