queue.c 12 KB

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