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