queue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. #include <linux/mmc/card.h>
  14. #include <linux/mmc/host.h>
  15. #include "queue.h"
  16. #include "block.h"
  17. #include "core.h"
  18. #include "card.h"
  19. #include "crypto.h"
  20. #include "host.h"
  21. #define MMC_DMA_MAP_MERGE_SEGMENTS 512
  22. static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
  23. {
  24. /* Allow only 1 DCMD at a time */
  25. return mq->in_flight[MMC_ISSUE_DCMD];
  26. }
  27. void mmc_cqe_check_busy(struct mmc_queue *mq)
  28. {
  29. if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
  30. mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
  31. }
  32. static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
  33. {
  34. return host->caps2 & MMC_CAP2_CQE_DCMD;
  35. }
  36. static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
  37. struct request *req)
  38. {
  39. switch (req_op(req)) {
  40. case REQ_OP_DRV_IN:
  41. case REQ_OP_DRV_OUT:
  42. case REQ_OP_DISCARD:
  43. case REQ_OP_SECURE_ERASE:
  44. case REQ_OP_WRITE_ZEROES:
  45. return MMC_ISSUE_SYNC;
  46. case REQ_OP_FLUSH:
  47. return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
  48. default:
  49. return MMC_ISSUE_ASYNC;
  50. }
  51. }
  52. enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
  53. {
  54. struct mmc_host *host = mq->card->host;
  55. if (host->cqe_enabled && !host->hsq_enabled)
  56. return mmc_cqe_issue_type(host, req);
  57. if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
  58. return MMC_ISSUE_ASYNC;
  59. return MMC_ISSUE_SYNC;
  60. }
  61. static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
  62. {
  63. if (!mq->recovery_needed) {
  64. mq->recovery_needed = true;
  65. schedule_work(&mq->recovery_work);
  66. }
  67. }
  68. void mmc_cqe_recovery_notifier(struct mmc_request *mrq)
  69. {
  70. struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
  71. brq.mrq);
  72. struct request *req = mmc_queue_req_to_req(mqrq);
  73. struct request_queue *q = req->q;
  74. struct mmc_queue *mq = q->queuedata;
  75. unsigned long flags;
  76. spin_lock_irqsave(&mq->lock, flags);
  77. __mmc_cqe_recovery_notifier(mq);
  78. spin_unlock_irqrestore(&mq->lock, flags);
  79. }
  80. static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
  81. {
  82. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  83. struct mmc_request *mrq = &mqrq->brq.mrq;
  84. struct mmc_queue *mq = req->q->queuedata;
  85. struct mmc_host *host = mq->card->host;
  86. enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
  87. bool recovery_needed = false;
  88. switch (issue_type) {
  89. case MMC_ISSUE_ASYNC:
  90. case MMC_ISSUE_DCMD:
  91. if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
  92. if (recovery_needed)
  93. mmc_cqe_recovery_notifier(mrq);
  94. return BLK_EH_RESET_TIMER;
  95. }
  96. /* The request has gone already */
  97. return BLK_EH_DONE;
  98. default:
  99. /* Timeout is handled by mmc core */
  100. return BLK_EH_RESET_TIMER;
  101. }
  102. }
  103. static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req)
  104. {
  105. struct request_queue *q = req->q;
  106. struct mmc_queue *mq = q->queuedata;
  107. struct mmc_card *card = mq->card;
  108. struct mmc_host *host = card->host;
  109. unsigned long flags;
  110. bool ignore_tout;
  111. spin_lock_irqsave(&mq->lock, flags);
  112. ignore_tout = mq->recovery_needed || !host->cqe_enabled || host->hsq_enabled;
  113. spin_unlock_irqrestore(&mq->lock, flags);
  114. return ignore_tout ? BLK_EH_RESET_TIMER : mmc_cqe_timed_out(req);
  115. }
  116. static void mmc_mq_recovery_handler(struct work_struct *work)
  117. {
  118. struct mmc_queue *mq = container_of(work, struct mmc_queue,
  119. recovery_work);
  120. struct request_queue *q = mq->queue;
  121. struct mmc_host *host = mq->card->host;
  122. mmc_get_card(mq->card, &mq->ctx);
  123. mq->in_recovery = true;
  124. if (host->cqe_enabled && !host->hsq_enabled)
  125. mmc_blk_cqe_recovery(mq);
  126. else
  127. mmc_blk_mq_recovery(mq);
  128. mq->in_recovery = false;
  129. spin_lock_irq(&mq->lock);
  130. mq->recovery_needed = false;
  131. spin_unlock_irq(&mq->lock);
  132. if (host->hsq_enabled)
  133. host->cqe_ops->cqe_recovery_finish(host);
  134. mmc_put_card(mq->card, &mq->ctx);
  135. blk_mq_run_hw_queues(q, true);
  136. }
  137. static struct scatterlist *mmc_alloc_sg(unsigned short sg_len, gfp_t gfp)
  138. {
  139. struct scatterlist *sg;
  140. sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
  141. if (sg)
  142. sg_init_table(sg, sg_len);
  143. return sg;
  144. }
  145. static void mmc_queue_setup_discard(struct mmc_card *card,
  146. struct queue_limits *lim)
  147. {
  148. unsigned max_discard;
  149. max_discard = mmc_calc_max_discard(card);
  150. if (!max_discard)
  151. return;
  152. lim->max_hw_discard_sectors = max_discard;
  153. if (mmc_can_secure_erase_trim(card))
  154. lim->max_secure_erase_sectors = max_discard;
  155. if (mmc_can_trim(card) && card->erased_byte == 0)
  156. lim->max_write_zeroes_sectors = max_discard;
  157. /* granularity must not be greater than max. discard */
  158. if (card->pref_erase > max_discard)
  159. lim->discard_granularity = SECTOR_SIZE;
  160. else
  161. lim->discard_granularity = card->pref_erase << 9;
  162. }
  163. static unsigned short mmc_get_max_segments(struct mmc_host *host)
  164. {
  165. return host->can_dma_map_merge ? MMC_DMA_MAP_MERGE_SEGMENTS :
  166. host->max_segs;
  167. }
  168. static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
  169. unsigned int hctx_idx, unsigned int numa_node)
  170. {
  171. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  172. struct mmc_queue *mq = set->driver_data;
  173. struct mmc_card *card = mq->card;
  174. struct mmc_host *host = card->host;
  175. mq_rq->sg = mmc_alloc_sg(mmc_get_max_segments(host), GFP_KERNEL);
  176. if (!mq_rq->sg)
  177. return -ENOMEM;
  178. return 0;
  179. }
  180. static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
  181. unsigned int hctx_idx)
  182. {
  183. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  184. kfree(mq_rq->sg);
  185. mq_rq->sg = NULL;
  186. }
  187. static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
  188. const struct blk_mq_queue_data *bd)
  189. {
  190. struct request *req = bd->rq;
  191. struct request_queue *q = req->q;
  192. struct mmc_queue *mq = q->queuedata;
  193. struct mmc_card *card = mq->card;
  194. struct mmc_host *host = card->host;
  195. enum mmc_issue_type issue_type;
  196. enum mmc_issued issued;
  197. bool get_card, cqe_retune_ok;
  198. blk_status_t ret;
  199. if (mmc_card_removed(mq->card)) {
  200. req->rq_flags |= RQF_QUIET;
  201. return BLK_STS_IOERR;
  202. }
  203. issue_type = mmc_issue_type(mq, req);
  204. spin_lock_irq(&mq->lock);
  205. if (mq->recovery_needed || mq->busy) {
  206. spin_unlock_irq(&mq->lock);
  207. return BLK_STS_RESOURCE;
  208. }
  209. switch (issue_type) {
  210. case MMC_ISSUE_DCMD:
  211. if (mmc_cqe_dcmd_busy(mq)) {
  212. mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
  213. spin_unlock_irq(&mq->lock);
  214. return BLK_STS_RESOURCE;
  215. }
  216. break;
  217. case MMC_ISSUE_ASYNC:
  218. if (host->hsq_enabled && mq->in_flight[issue_type] > host->hsq_depth) {
  219. spin_unlock_irq(&mq->lock);
  220. return BLK_STS_RESOURCE;
  221. }
  222. break;
  223. default:
  224. /*
  225. * Timeouts are handled by mmc core, and we don't have a host
  226. * API to abort requests, so we can't handle the timeout anyway.
  227. * However, when the timeout happens, blk_mq_complete_request()
  228. * no longer works (to stop the request disappearing under us).
  229. * To avoid racing with that, set a large timeout.
  230. */
  231. req->timeout = 600 * HZ;
  232. break;
  233. }
  234. /* Parallel dispatch of requests is not supported at the moment */
  235. mq->busy = true;
  236. mq->in_flight[issue_type] += 1;
  237. get_card = (mmc_tot_in_flight(mq) == 1);
  238. cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
  239. spin_unlock_irq(&mq->lock);
  240. if (!(req->rq_flags & RQF_DONTPREP)) {
  241. req_to_mmc_queue_req(req)->retries = 0;
  242. req->rq_flags |= RQF_DONTPREP;
  243. }
  244. if (get_card)
  245. mmc_get_card(card, &mq->ctx);
  246. if (host->cqe_enabled) {
  247. host->retune_now = host->need_retune && cqe_retune_ok &&
  248. !host->hold_retune;
  249. }
  250. blk_mq_start_request(req);
  251. issued = mmc_blk_mq_issue_rq(mq, req);
  252. switch (issued) {
  253. case MMC_REQ_BUSY:
  254. ret = BLK_STS_RESOURCE;
  255. break;
  256. case MMC_REQ_FAILED_TO_START:
  257. ret = BLK_STS_IOERR;
  258. break;
  259. default:
  260. ret = BLK_STS_OK;
  261. break;
  262. }
  263. if (issued != MMC_REQ_STARTED) {
  264. bool put_card = false;
  265. spin_lock_irq(&mq->lock);
  266. mq->in_flight[issue_type] -= 1;
  267. if (mmc_tot_in_flight(mq) == 0)
  268. put_card = true;
  269. mq->busy = false;
  270. spin_unlock_irq(&mq->lock);
  271. if (put_card)
  272. mmc_put_card(card, &mq->ctx);
  273. } else {
  274. WRITE_ONCE(mq->busy, false);
  275. }
  276. return ret;
  277. }
  278. static const struct blk_mq_ops mmc_mq_ops = {
  279. .queue_rq = mmc_mq_queue_rq,
  280. .init_request = mmc_mq_init_request,
  281. .exit_request = mmc_mq_exit_request,
  282. .complete = mmc_blk_mq_complete,
  283. .timeout = mmc_mq_timed_out,
  284. };
  285. static struct gendisk *mmc_alloc_disk(struct mmc_queue *mq,
  286. struct mmc_card *card, unsigned int features)
  287. {
  288. struct mmc_host *host = card->host;
  289. struct queue_limits lim = {
  290. .features = features,
  291. };
  292. struct gendisk *disk;
  293. if (mmc_can_erase(card))
  294. mmc_queue_setup_discard(card, &lim);
  295. lim.max_hw_sectors = min(host->max_blk_count, host->max_req_size / 512);
  296. if (mmc_card_mmc(card) && card->ext_csd.data_sector_size)
  297. lim.logical_block_size = card->ext_csd.data_sector_size;
  298. else
  299. lim.logical_block_size = 512;
  300. WARN_ON_ONCE(lim.logical_block_size != 512 &&
  301. lim.logical_block_size != 4096);
  302. /*
  303. * Setting a virt_boundary implicity sets a max_segment_size, so try
  304. * to set the hardware one here.
  305. */
  306. if (host->can_dma_map_merge) {
  307. lim.virt_boundary_mask = dma_get_merge_boundary(mmc_dev(host));
  308. lim.max_segments = MMC_DMA_MAP_MERGE_SEGMENTS;
  309. } else {
  310. lim.max_segment_size =
  311. round_down(host->max_seg_size, lim.logical_block_size);
  312. lim.max_segments = host->max_segs;
  313. }
  314. if (mmc_host_is_spi(host) && host->use_spi_crc)
  315. lim.features |= BLK_FEAT_STABLE_WRITES;
  316. disk = blk_mq_alloc_disk(&mq->tag_set, &lim, mq);
  317. if (IS_ERR(disk))
  318. return disk;
  319. mq->queue = disk->queue;
  320. blk_queue_rq_timeout(mq->queue, 60 * HZ);
  321. if (mmc_dev(host)->dma_parms)
  322. dma_set_max_seg_size(mmc_dev(host), queue_max_segment_size(mq->queue));
  323. INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
  324. INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
  325. mutex_init(&mq->complete_lock);
  326. init_waitqueue_head(&mq->wait);
  327. mmc_crypto_setup_queue(mq->queue, host);
  328. return disk;
  329. }
  330. static inline bool mmc_merge_capable(struct mmc_host *host)
  331. {
  332. return host->caps2 & MMC_CAP2_MERGE_CAPABLE;
  333. }
  334. /* Set queue depth to get a reasonable value for q->nr_requests */
  335. #define MMC_QUEUE_DEPTH 64
  336. /**
  337. * mmc_init_queue - initialise a queue structure.
  338. * @mq: mmc queue
  339. * @card: mmc card to attach this queue
  340. * @features: block layer features (BLK_FEAT_*)
  341. *
  342. * Initialise a MMC card request queue.
  343. */
  344. struct gendisk *mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
  345. unsigned int features)
  346. {
  347. struct mmc_host *host = card->host;
  348. struct gendisk *disk;
  349. int ret;
  350. mq->card = card;
  351. spin_lock_init(&mq->lock);
  352. memset(&mq->tag_set, 0, sizeof(mq->tag_set));
  353. mq->tag_set.ops = &mmc_mq_ops;
  354. /*
  355. * The queue depth for CQE must match the hardware because the request
  356. * tag is used to index the hardware queue.
  357. */
  358. if (host->cqe_enabled && !host->hsq_enabled)
  359. mq->tag_set.queue_depth =
  360. min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
  361. else
  362. mq->tag_set.queue_depth = MMC_QUEUE_DEPTH;
  363. mq->tag_set.numa_node = NUMA_NO_NODE;
  364. mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
  365. mq->tag_set.nr_hw_queues = 1;
  366. mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
  367. mq->tag_set.driver_data = mq;
  368. /*
  369. * Since blk_mq_alloc_tag_set() calls .init_request() of mmc_mq_ops,
  370. * the host->can_dma_map_merge should be set before to get max_segs
  371. * from mmc_get_max_segments().
  372. */
  373. if (mmc_merge_capable(host) &&
  374. host->max_segs < MMC_DMA_MAP_MERGE_SEGMENTS &&
  375. dma_get_merge_boundary(mmc_dev(host)))
  376. host->can_dma_map_merge = 1;
  377. else
  378. host->can_dma_map_merge = 0;
  379. ret = blk_mq_alloc_tag_set(&mq->tag_set);
  380. if (ret)
  381. return ERR_PTR(ret);
  382. disk = mmc_alloc_disk(mq, card, features);
  383. if (IS_ERR(disk))
  384. blk_mq_free_tag_set(&mq->tag_set);
  385. return disk;
  386. }
  387. void mmc_queue_suspend(struct mmc_queue *mq)
  388. {
  389. blk_mq_quiesce_queue(mq->queue);
  390. /*
  391. * The host remains claimed while there are outstanding requests, so
  392. * simply claiming and releasing here ensures there are none.
  393. */
  394. mmc_claim_host(mq->card->host);
  395. mmc_release_host(mq->card->host);
  396. }
  397. void mmc_queue_resume(struct mmc_queue *mq)
  398. {
  399. blk_mq_unquiesce_queue(mq->queue);
  400. }
  401. void mmc_cleanup_queue(struct mmc_queue *mq)
  402. {
  403. struct request_queue *q = mq->queue;
  404. /*
  405. * The legacy code handled the possibility of being suspended,
  406. * so do that here too.
  407. */
  408. if (blk_queue_quiesced(q))
  409. blk_mq_unquiesce_queue(q);
  410. /*
  411. * If the recovery completes the last (and only remaining) request in
  412. * the queue, and the card has been removed, we could end up here with
  413. * the recovery not quite finished yet, so cancel it.
  414. */
  415. cancel_work_sync(&mq->recovery_work);
  416. blk_mq_free_tag_set(&mq->tag_set);
  417. /*
  418. * A request can be completed before the next request, potentially
  419. * leaving a complete_work with nothing to do. Such a work item might
  420. * still be queued at this point. Flush it.
  421. */
  422. flush_work(&mq->complete_work);
  423. mq->card = NULL;
  424. }
  425. /*
  426. * Prepare the sg list(s) to be handed of to the host driver
  427. */
  428. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  429. {
  430. struct request *req = mmc_queue_req_to_req(mqrq);
  431. return blk_rq_map_sg(mq->queue, req, mqrq->sg);
  432. }