queue.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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(mq);
  96. return BLK_EH_RESET_TIMER;
  97. }
  98. /* No timeout (XXX: huh? comment doesn't make much sense) */
  99. blk_mq_complete_request(req);
  100. return BLK_EH_DONE;
  101. default:
  102. /* Timeout is handled by mmc core */
  103. return BLK_EH_RESET_TIMER;
  104. }
  105. }
  106. static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
  107. bool reserved)
  108. {
  109. struct request_queue *q = req->q;
  110. struct mmc_queue *mq = q->queuedata;
  111. unsigned long flags;
  112. int ret;
  113. spin_lock_irqsave(q->queue_lock, flags);
  114. if (mq->recovery_needed || !mq->use_cqe)
  115. ret = BLK_EH_RESET_TIMER;
  116. else
  117. ret = mmc_cqe_timed_out(req);
  118. spin_unlock_irqrestore(q->queue_lock, flags);
  119. return ret;
  120. }
  121. static void mmc_mq_recovery_handler(struct work_struct *work)
  122. {
  123. struct mmc_queue *mq = container_of(work, struct mmc_queue,
  124. recovery_work);
  125. struct request_queue *q = mq->queue;
  126. mmc_get_card(mq->card, &mq->ctx);
  127. mq->in_recovery = true;
  128. if (mq->use_cqe)
  129. mmc_blk_cqe_recovery(mq);
  130. else
  131. mmc_blk_mq_recovery(mq);
  132. mq->in_recovery = false;
  133. spin_lock_irq(q->queue_lock);
  134. mq->recovery_needed = false;
  135. spin_unlock_irq(q->queue_lock);
  136. mmc_put_card(mq->card, &mq->ctx);
  137. blk_mq_run_hw_queues(q, true);
  138. }
  139. static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
  140. {
  141. struct scatterlist *sg;
  142. sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
  143. if (sg)
  144. sg_init_table(sg, sg_len);
  145. return sg;
  146. }
  147. static void mmc_queue_setup_discard(struct request_queue *q,
  148. struct mmc_card *card)
  149. {
  150. unsigned max_discard;
  151. max_discard = mmc_calc_max_discard(card);
  152. if (!max_discard)
  153. return;
  154. blk_queue_flag_set(QUEUE_FLAG_DISCARD, q);
  155. blk_queue_max_discard_sectors(q, max_discard);
  156. q->limits.discard_granularity = card->pref_erase << 9;
  157. /* granularity must not be greater than max. discard */
  158. if (card->pref_erase > max_discard)
  159. q->limits.discard_granularity = 0;
  160. if (mmc_can_secure_erase_trim(card))
  161. blk_queue_flag_set(QUEUE_FLAG_SECERASE, q);
  162. }
  163. /**
  164. * mmc_init_request() - initialize the MMC-specific per-request data
  165. * @q: the request queue
  166. * @req: the request
  167. * @gfp: memory allocation policy
  168. */
  169. static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
  170. gfp_t gfp)
  171. {
  172. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  173. struct mmc_card *card = mq->card;
  174. struct mmc_host *host = card->host;
  175. mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
  176. if (!mq_rq->sg)
  177. return -ENOMEM;
  178. return 0;
  179. }
  180. static void mmc_exit_request(struct request_queue *q, struct request *req)
  181. {
  182. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  183. kfree(mq_rq->sg);
  184. mq_rq->sg = NULL;
  185. }
  186. static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
  187. unsigned int hctx_idx, unsigned int numa_node)
  188. {
  189. return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
  190. }
  191. static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
  192. unsigned int hctx_idx)
  193. {
  194. struct mmc_queue *mq = set->driver_data;
  195. mmc_exit_request(mq->queue, req);
  196. }
  197. static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
  198. const struct blk_mq_queue_data *bd)
  199. {
  200. struct request *req = bd->rq;
  201. struct request_queue *q = req->q;
  202. struct mmc_queue *mq = q->queuedata;
  203. struct mmc_card *card = mq->card;
  204. struct mmc_host *host = card->host;
  205. enum mmc_issue_type issue_type;
  206. enum mmc_issued issued;
  207. bool get_card, cqe_retune_ok;
  208. int ret;
  209. if (mmc_card_removed(mq->card)) {
  210. req->rq_flags |= RQF_QUIET;
  211. return BLK_STS_IOERR;
  212. }
  213. issue_type = mmc_issue_type(mq, req);
  214. spin_lock_irq(q->queue_lock);
  215. if (mq->recovery_needed || mq->busy) {
  216. spin_unlock_irq(q->queue_lock);
  217. return BLK_STS_RESOURCE;
  218. }
  219. switch (issue_type) {
  220. case MMC_ISSUE_DCMD:
  221. if (mmc_cqe_dcmd_busy(mq)) {
  222. mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
  223. spin_unlock_irq(q->queue_lock);
  224. return BLK_STS_RESOURCE;
  225. }
  226. break;
  227. case MMC_ISSUE_ASYNC:
  228. break;
  229. default:
  230. /*
  231. * Timeouts are handled by mmc core, and we don't have a host
  232. * API to abort requests, so we can't handle the timeout anyway.
  233. * However, when the timeout happens, blk_mq_complete_request()
  234. * no longer works (to stop the request disappearing under us).
  235. * To avoid racing with that, set a large timeout.
  236. */
  237. req->timeout = 600 * HZ;
  238. break;
  239. }
  240. /* Parallel dispatch of requests is not supported at the moment */
  241. mq->busy = true;
  242. mq->in_flight[issue_type] += 1;
  243. get_card = (mmc_tot_in_flight(mq) == 1);
  244. cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
  245. spin_unlock_irq(q->queue_lock);
  246. if (!(req->rq_flags & RQF_DONTPREP)) {
  247. req_to_mmc_queue_req(req)->retries = 0;
  248. req->rq_flags |= RQF_DONTPREP;
  249. }
  250. if (get_card)
  251. mmc_get_card(card, &mq->ctx);
  252. if (mq->use_cqe) {
  253. host->retune_now = host->need_retune && cqe_retune_ok &&
  254. !host->hold_retune;
  255. }
  256. blk_mq_start_request(req);
  257. issued = mmc_blk_mq_issue_rq(mq, req);
  258. switch (issued) {
  259. case MMC_REQ_BUSY:
  260. ret = BLK_STS_RESOURCE;
  261. break;
  262. case MMC_REQ_FAILED_TO_START:
  263. ret = BLK_STS_IOERR;
  264. break;
  265. default:
  266. ret = BLK_STS_OK;
  267. break;
  268. }
  269. if (issued != MMC_REQ_STARTED) {
  270. bool put_card = false;
  271. spin_lock_irq(q->queue_lock);
  272. mq->in_flight[issue_type] -= 1;
  273. if (mmc_tot_in_flight(mq) == 0)
  274. put_card = true;
  275. mq->busy = false;
  276. spin_unlock_irq(q->queue_lock);
  277. if (put_card)
  278. mmc_put_card(card, &mq->ctx);
  279. } else {
  280. WRITE_ONCE(mq->busy, false);
  281. }
  282. return ret;
  283. }
  284. static const struct blk_mq_ops mmc_mq_ops = {
  285. .queue_rq = mmc_mq_queue_rq,
  286. .init_request = mmc_mq_init_request,
  287. .exit_request = mmc_mq_exit_request,
  288. .complete = mmc_blk_mq_complete,
  289. .timeout = mmc_mq_timed_out,
  290. };
  291. static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
  292. {
  293. struct mmc_host *host = card->host;
  294. u64 limit = BLK_BOUNCE_HIGH;
  295. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  296. limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
  297. blk_queue_flag_set(QUEUE_FLAG_NONROT, mq->queue);
  298. blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, mq->queue);
  299. if (mmc_can_erase(card))
  300. mmc_queue_setup_discard(mq->queue, card);
  301. blk_queue_bounce_limit(mq->queue, limit);
  302. blk_queue_max_hw_sectors(mq->queue,
  303. min(host->max_blk_count, host->max_req_size / 512));
  304. blk_queue_max_segments(mq->queue, host->max_segs);
  305. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  306. INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
  307. INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
  308. mutex_init(&mq->complete_lock);
  309. init_waitqueue_head(&mq->wait);
  310. }
  311. static int mmc_mq_init_queue(struct mmc_queue *mq, int q_depth,
  312. const struct blk_mq_ops *mq_ops, spinlock_t *lock)
  313. {
  314. int ret;
  315. memset(&mq->tag_set, 0, sizeof(mq->tag_set));
  316. mq->tag_set.ops = mq_ops;
  317. mq->tag_set.queue_depth = q_depth;
  318. mq->tag_set.numa_node = NUMA_NO_NODE;
  319. mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE |
  320. BLK_MQ_F_BLOCKING;
  321. mq->tag_set.nr_hw_queues = 1;
  322. mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
  323. mq->tag_set.driver_data = mq;
  324. ret = blk_mq_alloc_tag_set(&mq->tag_set);
  325. if (ret)
  326. return ret;
  327. mq->queue = blk_mq_init_queue(&mq->tag_set);
  328. if (IS_ERR(mq->queue)) {
  329. ret = PTR_ERR(mq->queue);
  330. goto free_tag_set;
  331. }
  332. mq->queue->queue_lock = lock;
  333. mq->queue->queuedata = mq;
  334. return 0;
  335. free_tag_set:
  336. blk_mq_free_tag_set(&mq->tag_set);
  337. return ret;
  338. }
  339. /* Set queue depth to get a reasonable value for q->nr_requests */
  340. #define MMC_QUEUE_DEPTH 64
  341. static int mmc_mq_init(struct mmc_queue *mq, struct mmc_card *card,
  342. spinlock_t *lock)
  343. {
  344. struct mmc_host *host = card->host;
  345. int q_depth;
  346. int ret;
  347. /*
  348. * The queue depth for CQE must match the hardware because the request
  349. * tag is used to index the hardware queue.
  350. */
  351. if (mq->use_cqe)
  352. q_depth = min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
  353. else
  354. q_depth = MMC_QUEUE_DEPTH;
  355. ret = mmc_mq_init_queue(mq, q_depth, &mmc_mq_ops, lock);
  356. if (ret)
  357. return ret;
  358. blk_queue_rq_timeout(mq->queue, 60 * HZ);
  359. mmc_setup_queue(mq, card);
  360. return 0;
  361. }
  362. /**
  363. * mmc_init_queue - initialise a queue structure.
  364. * @mq: mmc queue
  365. * @card: mmc card to attach this queue
  366. * @lock: queue lock
  367. * @subname: partition subname
  368. *
  369. * Initialise a MMC card request queue.
  370. */
  371. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
  372. spinlock_t *lock, const char *subname)
  373. {
  374. struct mmc_host *host = card->host;
  375. mq->card = card;
  376. mq->use_cqe = host->cqe_enabled;
  377. return mmc_mq_init(mq, card, lock);
  378. }
  379. void mmc_queue_suspend(struct mmc_queue *mq)
  380. {
  381. blk_mq_quiesce_queue(mq->queue);
  382. /*
  383. * The host remains claimed while there are outstanding requests, so
  384. * simply claiming and releasing here ensures there are none.
  385. */
  386. mmc_claim_host(mq->card->host);
  387. mmc_release_host(mq->card->host);
  388. }
  389. void mmc_queue_resume(struct mmc_queue *mq)
  390. {
  391. blk_mq_unquiesce_queue(mq->queue);
  392. }
  393. void mmc_cleanup_queue(struct mmc_queue *mq)
  394. {
  395. struct request_queue *q = mq->queue;
  396. /*
  397. * The legacy code handled the possibility of being suspended,
  398. * so do that here too.
  399. */
  400. if (blk_queue_quiesced(q))
  401. blk_mq_unquiesce_queue(q);
  402. blk_cleanup_queue(q);
  403. /*
  404. * A request can be completed before the next request, potentially
  405. * leaving a complete_work with nothing to do. Such a work item might
  406. * still be queued at this point. Flush it.
  407. */
  408. flush_work(&mq->complete_work);
  409. mq->card = NULL;
  410. }
  411. /*
  412. * Prepare the sg list(s) to be handed of to the host driver
  413. */
  414. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  415. {
  416. struct request *req = mmc_queue_req_to_req(mqrq);
  417. return blk_rq_map_sg(mq->queue, req, mqrq->sg);
  418. }