queue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. /*
  25. * Prepare a MMC request. This just filters out odd stuff.
  26. */
  27. static int mmc_prep_request(struct request_queue *q, struct request *req)
  28. {
  29. struct mmc_queue *mq = q->queuedata;
  30. if (mq && mmc_card_removed(mq->card))
  31. return BLKPREP_KILL;
  32. req->rq_flags |= RQF_DONTPREP;
  33. req_to_mmc_queue_req(req)->retries = 0;
  34. return BLKPREP_OK;
  35. }
  36. enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
  37. {
  38. if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
  39. return MMC_ISSUE_ASYNC;
  40. return MMC_ISSUE_SYNC;
  41. }
  42. static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
  43. bool reserved)
  44. {
  45. return BLK_EH_RESET_TIMER;
  46. }
  47. static int mmc_queue_thread(void *d)
  48. {
  49. struct mmc_queue *mq = d;
  50. struct request_queue *q = mq->queue;
  51. struct mmc_context_info *cntx = &mq->card->host->context_info;
  52. current->flags |= PF_MEMALLOC;
  53. down(&mq->thread_sem);
  54. do {
  55. struct request *req;
  56. spin_lock_irq(q->queue_lock);
  57. set_current_state(TASK_INTERRUPTIBLE);
  58. req = blk_fetch_request(q);
  59. mq->asleep = false;
  60. cntx->is_waiting_last_req = false;
  61. cntx->is_new_req = false;
  62. if (!req) {
  63. /*
  64. * Dispatch queue is empty so set flags for
  65. * mmc_request_fn() to wake us up.
  66. */
  67. if (mq->qcnt)
  68. cntx->is_waiting_last_req = true;
  69. else
  70. mq->asleep = true;
  71. }
  72. spin_unlock_irq(q->queue_lock);
  73. if (req || mq->qcnt) {
  74. set_current_state(TASK_RUNNING);
  75. mmc_blk_issue_rq(mq, req);
  76. cond_resched();
  77. } else {
  78. if (kthread_should_stop()) {
  79. set_current_state(TASK_RUNNING);
  80. break;
  81. }
  82. up(&mq->thread_sem);
  83. schedule();
  84. down(&mq->thread_sem);
  85. }
  86. } while (1);
  87. up(&mq->thread_sem);
  88. return 0;
  89. }
  90. /*
  91. * Generic MMC request handler. This is called for any queue on a
  92. * particular host. When the host is not busy, we look for a request
  93. * on any queue on this host, and attempt to issue it. This may
  94. * not be the queue we were asked to process.
  95. */
  96. static void mmc_request_fn(struct request_queue *q)
  97. {
  98. struct mmc_queue *mq = q->queuedata;
  99. struct request *req;
  100. struct mmc_context_info *cntx;
  101. if (!mq) {
  102. while ((req = blk_fetch_request(q)) != NULL) {
  103. req->rq_flags |= RQF_QUIET;
  104. __blk_end_request_all(req, BLK_STS_IOERR);
  105. }
  106. return;
  107. }
  108. cntx = &mq->card->host->context_info;
  109. if (cntx->is_waiting_last_req) {
  110. cntx->is_new_req = true;
  111. wake_up_interruptible(&cntx->wait);
  112. }
  113. if (mq->asleep)
  114. wake_up_process(mq->thread);
  115. }
  116. static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
  117. {
  118. struct scatterlist *sg;
  119. sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
  120. if (sg)
  121. sg_init_table(sg, sg_len);
  122. return sg;
  123. }
  124. static void mmc_queue_setup_discard(struct request_queue *q,
  125. struct mmc_card *card)
  126. {
  127. unsigned max_discard;
  128. max_discard = mmc_calc_max_discard(card);
  129. if (!max_discard)
  130. return;
  131. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  132. blk_queue_max_discard_sectors(q, max_discard);
  133. q->limits.discard_granularity = card->pref_erase << 9;
  134. /* granularity must not be greater than max. discard */
  135. if (card->pref_erase > max_discard)
  136. q->limits.discard_granularity = 0;
  137. if (mmc_can_secure_erase_trim(card))
  138. queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
  139. }
  140. /**
  141. * mmc_init_request() - initialize the MMC-specific per-request data
  142. * @q: the request queue
  143. * @req: the request
  144. * @gfp: memory allocation policy
  145. */
  146. static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
  147. gfp_t gfp)
  148. {
  149. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  150. struct mmc_card *card = mq->card;
  151. struct mmc_host *host = card->host;
  152. mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
  153. if (!mq_rq->sg)
  154. return -ENOMEM;
  155. return 0;
  156. }
  157. static int mmc_init_request(struct request_queue *q, struct request *req,
  158. gfp_t gfp)
  159. {
  160. return __mmc_init_request(q->queuedata, req, gfp);
  161. }
  162. static void mmc_exit_request(struct request_queue *q, struct request *req)
  163. {
  164. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  165. kfree(mq_rq->sg);
  166. mq_rq->sg = NULL;
  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. return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
  172. }
  173. static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
  174. unsigned int hctx_idx)
  175. {
  176. struct mmc_queue *mq = set->driver_data;
  177. mmc_exit_request(mq->queue, req);
  178. }
  179. /*
  180. * We use BLK_MQ_F_BLOCKING and have only 1 hardware queue, which means requests
  181. * will not be dispatched in parallel.
  182. */
  183. static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
  184. const struct blk_mq_queue_data *bd)
  185. {
  186. struct request *req = bd->rq;
  187. struct request_queue *q = req->q;
  188. struct mmc_queue *mq = q->queuedata;
  189. struct mmc_card *card = mq->card;
  190. enum mmc_issue_type issue_type;
  191. enum mmc_issued issued;
  192. bool get_card;
  193. int ret;
  194. if (mmc_card_removed(mq->card)) {
  195. req->rq_flags |= RQF_QUIET;
  196. return BLK_STS_IOERR;
  197. }
  198. issue_type = mmc_issue_type(mq, req);
  199. spin_lock_irq(q->queue_lock);
  200. switch (issue_type) {
  201. case MMC_ISSUE_ASYNC:
  202. break;
  203. default:
  204. /*
  205. * Timeouts are handled by mmc core, and we don't have a host
  206. * API to abort requests, so we can't handle the timeout anyway.
  207. * However, when the timeout happens, blk_mq_complete_request()
  208. * no longer works (to stop the request disappearing under us).
  209. * To avoid racing with that, set a large timeout.
  210. */
  211. req->timeout = 600 * HZ;
  212. break;
  213. }
  214. mq->in_flight[issue_type] += 1;
  215. get_card = (mmc_tot_in_flight(mq) == 1);
  216. spin_unlock_irq(q->queue_lock);
  217. if (!(req->rq_flags & RQF_DONTPREP)) {
  218. req_to_mmc_queue_req(req)->retries = 0;
  219. req->rq_flags |= RQF_DONTPREP;
  220. }
  221. if (get_card)
  222. mmc_get_card(card, &mq->ctx);
  223. blk_mq_start_request(req);
  224. issued = mmc_blk_mq_issue_rq(mq, req);
  225. switch (issued) {
  226. case MMC_REQ_BUSY:
  227. ret = BLK_STS_RESOURCE;
  228. break;
  229. case MMC_REQ_FAILED_TO_START:
  230. ret = BLK_STS_IOERR;
  231. break;
  232. default:
  233. ret = BLK_STS_OK;
  234. break;
  235. }
  236. if (issued != MMC_REQ_STARTED) {
  237. bool put_card = false;
  238. spin_lock_irq(q->queue_lock);
  239. mq->in_flight[issue_type] -= 1;
  240. if (mmc_tot_in_flight(mq) == 0)
  241. put_card = true;
  242. spin_unlock_irq(q->queue_lock);
  243. if (put_card)
  244. mmc_put_card(card, &mq->ctx);
  245. }
  246. return ret;
  247. }
  248. static const struct blk_mq_ops mmc_mq_ops = {
  249. .queue_rq = mmc_mq_queue_rq,
  250. .init_request = mmc_mq_init_request,
  251. .exit_request = mmc_mq_exit_request,
  252. .complete = mmc_blk_mq_complete,
  253. .timeout = mmc_mq_timed_out,
  254. };
  255. static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
  256. {
  257. struct mmc_host *host = card->host;
  258. u64 limit = BLK_BOUNCE_HIGH;
  259. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  260. limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
  261. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  262. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
  263. if (mmc_can_erase(card))
  264. mmc_queue_setup_discard(mq->queue, card);
  265. blk_queue_bounce_limit(mq->queue, limit);
  266. blk_queue_max_hw_sectors(mq->queue,
  267. min(host->max_blk_count, host->max_req_size / 512));
  268. blk_queue_max_segments(mq->queue, host->max_segs);
  269. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  270. /* Initialize thread_sem even if it is not used */
  271. sema_init(&mq->thread_sem, 1);
  272. INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
  273. mutex_init(&mq->complete_lock);
  274. init_waitqueue_head(&mq->wait);
  275. }
  276. static int mmc_mq_init_queue(struct mmc_queue *mq, int q_depth,
  277. const struct blk_mq_ops *mq_ops, spinlock_t *lock)
  278. {
  279. int ret;
  280. memset(&mq->tag_set, 0, sizeof(mq->tag_set));
  281. mq->tag_set.ops = mq_ops;
  282. mq->tag_set.queue_depth = q_depth;
  283. mq->tag_set.numa_node = NUMA_NO_NODE;
  284. mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE |
  285. BLK_MQ_F_BLOCKING;
  286. mq->tag_set.nr_hw_queues = 1;
  287. mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
  288. mq->tag_set.driver_data = mq;
  289. ret = blk_mq_alloc_tag_set(&mq->tag_set);
  290. if (ret)
  291. return ret;
  292. mq->queue = blk_mq_init_queue(&mq->tag_set);
  293. if (IS_ERR(mq->queue)) {
  294. ret = PTR_ERR(mq->queue);
  295. goto free_tag_set;
  296. }
  297. mq->queue->queue_lock = lock;
  298. mq->queue->queuedata = mq;
  299. return 0;
  300. free_tag_set:
  301. blk_mq_free_tag_set(&mq->tag_set);
  302. return ret;
  303. }
  304. /* Set queue depth to get a reasonable value for q->nr_requests */
  305. #define MMC_QUEUE_DEPTH 64
  306. static int mmc_mq_init(struct mmc_queue *mq, struct mmc_card *card,
  307. spinlock_t *lock)
  308. {
  309. int q_depth;
  310. int ret;
  311. q_depth = MMC_QUEUE_DEPTH;
  312. ret = mmc_mq_init_queue(mq, q_depth, &mmc_mq_ops, lock);
  313. if (ret)
  314. return ret;
  315. blk_queue_rq_timeout(mq->queue, 60 * HZ);
  316. mmc_setup_queue(mq, card);
  317. return 0;
  318. }
  319. /**
  320. * mmc_init_queue - initialise a queue structure.
  321. * @mq: mmc queue
  322. * @card: mmc card to attach this queue
  323. * @lock: queue lock
  324. * @subname: partition subname
  325. *
  326. * Initialise a MMC card request queue.
  327. */
  328. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
  329. spinlock_t *lock, const char *subname)
  330. {
  331. struct mmc_host *host = card->host;
  332. int ret = -ENOMEM;
  333. mq->card = card;
  334. if (mmc_host_use_blk_mq(host))
  335. return mmc_mq_init(mq, card, lock);
  336. mq->queue = blk_alloc_queue(GFP_KERNEL);
  337. if (!mq->queue)
  338. return -ENOMEM;
  339. mq->queue->queue_lock = lock;
  340. mq->queue->request_fn = mmc_request_fn;
  341. mq->queue->init_rq_fn = mmc_init_request;
  342. mq->queue->exit_rq_fn = mmc_exit_request;
  343. mq->queue->cmd_size = sizeof(struct mmc_queue_req);
  344. mq->queue->queuedata = mq;
  345. mq->qcnt = 0;
  346. ret = blk_init_allocated_queue(mq->queue);
  347. if (ret) {
  348. blk_cleanup_queue(mq->queue);
  349. return ret;
  350. }
  351. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  352. mmc_setup_queue(mq, card);
  353. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
  354. host->index, subname ? subname : "");
  355. if (IS_ERR(mq->thread)) {
  356. ret = PTR_ERR(mq->thread);
  357. goto cleanup_queue;
  358. }
  359. return 0;
  360. cleanup_queue:
  361. blk_cleanup_queue(mq->queue);
  362. return ret;
  363. }
  364. static void mmc_mq_queue_suspend(struct mmc_queue *mq)
  365. {
  366. blk_mq_quiesce_queue(mq->queue);
  367. /*
  368. * The host remains claimed while there are outstanding requests, so
  369. * simply claiming and releasing here ensures there are none.
  370. */
  371. mmc_claim_host(mq->card->host);
  372. mmc_release_host(mq->card->host);
  373. }
  374. static void mmc_mq_queue_resume(struct mmc_queue *mq)
  375. {
  376. blk_mq_unquiesce_queue(mq->queue);
  377. }
  378. static void __mmc_queue_suspend(struct mmc_queue *mq)
  379. {
  380. struct request_queue *q = mq->queue;
  381. unsigned long flags;
  382. if (!mq->suspended) {
  383. mq->suspended |= true;
  384. spin_lock_irqsave(q->queue_lock, flags);
  385. blk_stop_queue(q);
  386. spin_unlock_irqrestore(q->queue_lock, flags);
  387. down(&mq->thread_sem);
  388. }
  389. }
  390. static void __mmc_queue_resume(struct mmc_queue *mq)
  391. {
  392. struct request_queue *q = mq->queue;
  393. unsigned long flags;
  394. if (mq->suspended) {
  395. mq->suspended = false;
  396. up(&mq->thread_sem);
  397. spin_lock_irqsave(q->queue_lock, flags);
  398. blk_start_queue(q);
  399. spin_unlock_irqrestore(q->queue_lock, flags);
  400. }
  401. }
  402. void mmc_cleanup_queue(struct mmc_queue *mq)
  403. {
  404. struct request_queue *q = mq->queue;
  405. unsigned long flags;
  406. if (q->mq_ops) {
  407. /*
  408. * The legacy code handled the possibility of being suspended,
  409. * so do that here too.
  410. */
  411. if (blk_queue_quiesced(q))
  412. blk_mq_unquiesce_queue(q);
  413. goto out_cleanup;
  414. }
  415. /* Make sure the queue isn't suspended, as that will deadlock */
  416. mmc_queue_resume(mq);
  417. /* Then terminate our worker thread */
  418. kthread_stop(mq->thread);
  419. /* Empty the queue */
  420. spin_lock_irqsave(q->queue_lock, flags);
  421. q->queuedata = NULL;
  422. blk_start_queue(q);
  423. spin_unlock_irqrestore(q->queue_lock, flags);
  424. out_cleanup:
  425. blk_cleanup_queue(q);
  426. /*
  427. * A request can be completed before the next request, potentially
  428. * leaving a complete_work with nothing to do. Such a work item might
  429. * still be queued at this point. Flush it.
  430. */
  431. flush_work(&mq->complete_work);
  432. mq->card = NULL;
  433. }
  434. /**
  435. * mmc_queue_suspend - suspend a MMC request queue
  436. * @mq: MMC queue to suspend
  437. *
  438. * Stop the block request queue, and wait for our thread to
  439. * complete any outstanding requests. This ensures that we
  440. * won't suspend while a request is being processed.
  441. */
  442. void mmc_queue_suspend(struct mmc_queue *mq)
  443. {
  444. struct request_queue *q = mq->queue;
  445. if (q->mq_ops)
  446. mmc_mq_queue_suspend(mq);
  447. else
  448. __mmc_queue_suspend(mq);
  449. }
  450. /**
  451. * mmc_queue_resume - resume a previously suspended MMC request queue
  452. * @mq: MMC queue to resume
  453. */
  454. void mmc_queue_resume(struct mmc_queue *mq)
  455. {
  456. struct request_queue *q = mq->queue;
  457. if (q->mq_ops)
  458. mmc_mq_queue_resume(mq);
  459. else
  460. __mmc_queue_resume(mq);
  461. }
  462. /*
  463. * Prepare the sg list(s) to be handed of to the host driver
  464. */
  465. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  466. {
  467. struct request *req = mmc_queue_req_to_req(mqrq);
  468. return blk_rq_map_sg(mq->queue, req, mqrq->sg);
  469. }