queue.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. static inline bool mmc_cqe_dcmd_busy(struct mmc_queue *mq)
  37. {
  38. /* Allow only 1 DCMD at a time */
  39. return mq->in_flight[MMC_ISSUE_DCMD];
  40. }
  41. void mmc_cqe_check_busy(struct mmc_queue *mq)
  42. {
  43. if ((mq->cqe_busy & MMC_CQE_DCMD_BUSY) && !mmc_cqe_dcmd_busy(mq))
  44. mq->cqe_busy &= ~MMC_CQE_DCMD_BUSY;
  45. mq->cqe_busy &= ~MMC_CQE_QUEUE_FULL;
  46. }
  47. static inline bool mmc_cqe_can_dcmd(struct mmc_host *host)
  48. {
  49. return host->caps2 & MMC_CAP2_CQE_DCMD;
  50. }
  51. static enum mmc_issue_type mmc_cqe_issue_type(struct mmc_host *host,
  52. struct request *req)
  53. {
  54. switch (req_op(req)) {
  55. case REQ_OP_DRV_IN:
  56. case REQ_OP_DRV_OUT:
  57. case REQ_OP_DISCARD:
  58. case REQ_OP_SECURE_ERASE:
  59. return MMC_ISSUE_SYNC;
  60. case REQ_OP_FLUSH:
  61. return mmc_cqe_can_dcmd(host) ? MMC_ISSUE_DCMD : MMC_ISSUE_SYNC;
  62. default:
  63. return MMC_ISSUE_ASYNC;
  64. }
  65. }
  66. enum mmc_issue_type mmc_issue_type(struct mmc_queue *mq, struct request *req)
  67. {
  68. struct mmc_host *host = mq->card->host;
  69. if (mq->use_cqe)
  70. return mmc_cqe_issue_type(host, req);
  71. if (req_op(req) == REQ_OP_READ || req_op(req) == REQ_OP_WRITE)
  72. return MMC_ISSUE_ASYNC;
  73. return MMC_ISSUE_SYNC;
  74. }
  75. static void __mmc_cqe_recovery_notifier(struct mmc_queue *mq)
  76. {
  77. if (!mq->recovery_needed) {
  78. mq->recovery_needed = true;
  79. schedule_work(&mq->recovery_work);
  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. spin_lock_irqsave(q->queue_lock, flags);
  91. __mmc_cqe_recovery_notifier(mq);
  92. spin_unlock_irqrestore(q->queue_lock, flags);
  93. }
  94. static enum blk_eh_timer_return mmc_cqe_timed_out(struct request *req)
  95. {
  96. struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
  97. struct mmc_request *mrq = &mqrq->brq.mrq;
  98. struct mmc_queue *mq = req->q->queuedata;
  99. struct mmc_host *host = mq->card->host;
  100. enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
  101. bool recovery_needed = false;
  102. switch (issue_type) {
  103. case MMC_ISSUE_ASYNC:
  104. case MMC_ISSUE_DCMD:
  105. if (host->cqe_ops->cqe_timeout(host, mrq, &recovery_needed)) {
  106. if (recovery_needed)
  107. __mmc_cqe_recovery_notifier(mq);
  108. return BLK_EH_RESET_TIMER;
  109. }
  110. /* No timeout */
  111. return BLK_EH_HANDLED;
  112. default:
  113. /* Timeout is handled by mmc core */
  114. return BLK_EH_RESET_TIMER;
  115. }
  116. }
  117. static enum blk_eh_timer_return mmc_mq_timed_out(struct request *req,
  118. bool reserved)
  119. {
  120. struct request_queue *q = req->q;
  121. struct mmc_queue *mq = q->queuedata;
  122. unsigned long flags;
  123. int ret;
  124. spin_lock_irqsave(q->queue_lock, flags);
  125. if (mq->recovery_needed || !mq->use_cqe)
  126. ret = BLK_EH_RESET_TIMER;
  127. else
  128. ret = mmc_cqe_timed_out(req);
  129. spin_unlock_irqrestore(q->queue_lock, flags);
  130. return ret;
  131. }
  132. static void mmc_mq_recovery_handler(struct work_struct *work)
  133. {
  134. struct mmc_queue *mq = container_of(work, struct mmc_queue,
  135. recovery_work);
  136. struct request_queue *q = mq->queue;
  137. mmc_get_card(mq->card, &mq->ctx);
  138. mq->in_recovery = true;
  139. if (mq->use_cqe)
  140. mmc_blk_cqe_recovery(mq);
  141. else
  142. mmc_blk_mq_recovery(mq);
  143. mq->in_recovery = false;
  144. spin_lock_irq(q->queue_lock);
  145. mq->recovery_needed = false;
  146. spin_unlock_irq(q->queue_lock);
  147. mmc_put_card(mq->card, &mq->ctx);
  148. blk_mq_run_hw_queues(q, true);
  149. }
  150. static int mmc_queue_thread(void *d)
  151. {
  152. struct mmc_queue *mq = d;
  153. struct request_queue *q = mq->queue;
  154. struct mmc_context_info *cntx = &mq->card->host->context_info;
  155. current->flags |= PF_MEMALLOC;
  156. down(&mq->thread_sem);
  157. do {
  158. struct request *req;
  159. spin_lock_irq(q->queue_lock);
  160. set_current_state(TASK_INTERRUPTIBLE);
  161. req = blk_fetch_request(q);
  162. mq->asleep = false;
  163. cntx->is_waiting_last_req = false;
  164. cntx->is_new_req = false;
  165. if (!req) {
  166. /*
  167. * Dispatch queue is empty so set flags for
  168. * mmc_request_fn() to wake us up.
  169. */
  170. if (mq->qcnt)
  171. cntx->is_waiting_last_req = true;
  172. else
  173. mq->asleep = true;
  174. }
  175. spin_unlock_irq(q->queue_lock);
  176. if (req || mq->qcnt) {
  177. set_current_state(TASK_RUNNING);
  178. mmc_blk_issue_rq(mq, req);
  179. cond_resched();
  180. } else {
  181. if (kthread_should_stop()) {
  182. set_current_state(TASK_RUNNING);
  183. break;
  184. }
  185. up(&mq->thread_sem);
  186. schedule();
  187. down(&mq->thread_sem);
  188. }
  189. } while (1);
  190. up(&mq->thread_sem);
  191. return 0;
  192. }
  193. /*
  194. * Generic MMC request handler. This is called for any queue on a
  195. * particular host. When the host is not busy, we look for a request
  196. * on any queue on this host, and attempt to issue it. This may
  197. * not be the queue we were asked to process.
  198. */
  199. static void mmc_request_fn(struct request_queue *q)
  200. {
  201. struct mmc_queue *mq = q->queuedata;
  202. struct request *req;
  203. struct mmc_context_info *cntx;
  204. if (!mq) {
  205. while ((req = blk_fetch_request(q)) != NULL) {
  206. req->rq_flags |= RQF_QUIET;
  207. __blk_end_request_all(req, BLK_STS_IOERR);
  208. }
  209. return;
  210. }
  211. cntx = &mq->card->host->context_info;
  212. if (cntx->is_waiting_last_req) {
  213. cntx->is_new_req = true;
  214. wake_up_interruptible(&cntx->wait);
  215. }
  216. if (mq->asleep)
  217. wake_up_process(mq->thread);
  218. }
  219. static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
  220. {
  221. struct scatterlist *sg;
  222. sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
  223. if (sg)
  224. sg_init_table(sg, sg_len);
  225. return sg;
  226. }
  227. static void mmc_queue_setup_discard(struct request_queue *q,
  228. struct mmc_card *card)
  229. {
  230. unsigned max_discard;
  231. max_discard = mmc_calc_max_discard(card);
  232. if (!max_discard)
  233. return;
  234. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  235. blk_queue_max_discard_sectors(q, max_discard);
  236. q->limits.discard_granularity = card->pref_erase << 9;
  237. /* granularity must not be greater than max. discard */
  238. if (card->pref_erase > max_discard)
  239. q->limits.discard_granularity = 0;
  240. if (mmc_can_secure_erase_trim(card))
  241. queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
  242. }
  243. /**
  244. * mmc_init_request() - initialize the MMC-specific per-request data
  245. * @q: the request queue
  246. * @req: the request
  247. * @gfp: memory allocation policy
  248. */
  249. static int __mmc_init_request(struct mmc_queue *mq, struct request *req,
  250. gfp_t gfp)
  251. {
  252. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  253. struct mmc_card *card = mq->card;
  254. struct mmc_host *host = card->host;
  255. mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
  256. if (!mq_rq->sg)
  257. return -ENOMEM;
  258. return 0;
  259. }
  260. static int mmc_init_request(struct request_queue *q, struct request *req,
  261. gfp_t gfp)
  262. {
  263. return __mmc_init_request(q->queuedata, req, gfp);
  264. }
  265. static void mmc_exit_request(struct request_queue *q, struct request *req)
  266. {
  267. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  268. kfree(mq_rq->sg);
  269. mq_rq->sg = NULL;
  270. }
  271. static int mmc_mq_init_request(struct blk_mq_tag_set *set, struct request *req,
  272. unsigned int hctx_idx, unsigned int numa_node)
  273. {
  274. return __mmc_init_request(set->driver_data, req, GFP_KERNEL);
  275. }
  276. static void mmc_mq_exit_request(struct blk_mq_tag_set *set, struct request *req,
  277. unsigned int hctx_idx)
  278. {
  279. struct mmc_queue *mq = set->driver_data;
  280. mmc_exit_request(mq->queue, req);
  281. }
  282. /*
  283. * We use BLK_MQ_F_BLOCKING and have only 1 hardware queue, which means requests
  284. * will not be dispatched in parallel.
  285. */
  286. static blk_status_t mmc_mq_queue_rq(struct blk_mq_hw_ctx *hctx,
  287. const struct blk_mq_queue_data *bd)
  288. {
  289. struct request *req = bd->rq;
  290. struct request_queue *q = req->q;
  291. struct mmc_queue *mq = q->queuedata;
  292. struct mmc_card *card = mq->card;
  293. struct mmc_host *host = card->host;
  294. enum mmc_issue_type issue_type;
  295. enum mmc_issued issued;
  296. bool get_card, cqe_retune_ok;
  297. int ret;
  298. if (mmc_card_removed(mq->card)) {
  299. req->rq_flags |= RQF_QUIET;
  300. return BLK_STS_IOERR;
  301. }
  302. issue_type = mmc_issue_type(mq, req);
  303. spin_lock_irq(q->queue_lock);
  304. if (mq->recovery_needed) {
  305. spin_unlock_irq(q->queue_lock);
  306. return BLK_STS_RESOURCE;
  307. }
  308. switch (issue_type) {
  309. case MMC_ISSUE_DCMD:
  310. if (mmc_cqe_dcmd_busy(mq)) {
  311. mq->cqe_busy |= MMC_CQE_DCMD_BUSY;
  312. spin_unlock_irq(q->queue_lock);
  313. return BLK_STS_RESOURCE;
  314. }
  315. break;
  316. case MMC_ISSUE_ASYNC:
  317. break;
  318. default:
  319. /*
  320. * Timeouts are handled by mmc core, and we don't have a host
  321. * API to abort requests, so we can't handle the timeout anyway.
  322. * However, when the timeout happens, blk_mq_complete_request()
  323. * no longer works (to stop the request disappearing under us).
  324. * To avoid racing with that, set a large timeout.
  325. */
  326. req->timeout = 600 * HZ;
  327. break;
  328. }
  329. mq->in_flight[issue_type] += 1;
  330. get_card = (mmc_tot_in_flight(mq) == 1);
  331. cqe_retune_ok = (mmc_cqe_qcnt(mq) == 1);
  332. spin_unlock_irq(q->queue_lock);
  333. if (!(req->rq_flags & RQF_DONTPREP)) {
  334. req_to_mmc_queue_req(req)->retries = 0;
  335. req->rq_flags |= RQF_DONTPREP;
  336. }
  337. if (get_card)
  338. mmc_get_card(card, &mq->ctx);
  339. if (mq->use_cqe) {
  340. host->retune_now = host->need_retune && cqe_retune_ok &&
  341. !host->hold_retune;
  342. }
  343. blk_mq_start_request(req);
  344. issued = mmc_blk_mq_issue_rq(mq, req);
  345. switch (issued) {
  346. case MMC_REQ_BUSY:
  347. ret = BLK_STS_RESOURCE;
  348. break;
  349. case MMC_REQ_FAILED_TO_START:
  350. ret = BLK_STS_IOERR;
  351. break;
  352. default:
  353. ret = BLK_STS_OK;
  354. break;
  355. }
  356. if (issued != MMC_REQ_STARTED) {
  357. bool put_card = false;
  358. spin_lock_irq(q->queue_lock);
  359. mq->in_flight[issue_type] -= 1;
  360. if (mmc_tot_in_flight(mq) == 0)
  361. put_card = true;
  362. spin_unlock_irq(q->queue_lock);
  363. if (put_card)
  364. mmc_put_card(card, &mq->ctx);
  365. }
  366. return ret;
  367. }
  368. static const struct blk_mq_ops mmc_mq_ops = {
  369. .queue_rq = mmc_mq_queue_rq,
  370. .init_request = mmc_mq_init_request,
  371. .exit_request = mmc_mq_exit_request,
  372. .complete = mmc_blk_mq_complete,
  373. .timeout = mmc_mq_timed_out,
  374. };
  375. static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
  376. {
  377. struct mmc_host *host = card->host;
  378. u64 limit = BLK_BOUNCE_HIGH;
  379. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  380. limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
  381. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  382. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
  383. if (mmc_can_erase(card))
  384. mmc_queue_setup_discard(mq->queue, card);
  385. blk_queue_bounce_limit(mq->queue, limit);
  386. blk_queue_max_hw_sectors(mq->queue,
  387. min(host->max_blk_count, host->max_req_size / 512));
  388. blk_queue_max_segments(mq->queue, host->max_segs);
  389. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  390. /* Initialize thread_sem even if it is not used */
  391. sema_init(&mq->thread_sem, 1);
  392. INIT_WORK(&mq->recovery_work, mmc_mq_recovery_handler);
  393. INIT_WORK(&mq->complete_work, mmc_blk_mq_complete_work);
  394. mutex_init(&mq->complete_lock);
  395. init_waitqueue_head(&mq->wait);
  396. }
  397. static int mmc_mq_init_queue(struct mmc_queue *mq, int q_depth,
  398. const struct blk_mq_ops *mq_ops, spinlock_t *lock)
  399. {
  400. int ret;
  401. memset(&mq->tag_set, 0, sizeof(mq->tag_set));
  402. mq->tag_set.ops = mq_ops;
  403. mq->tag_set.queue_depth = q_depth;
  404. mq->tag_set.numa_node = NUMA_NO_NODE;
  405. mq->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE |
  406. BLK_MQ_F_BLOCKING;
  407. mq->tag_set.nr_hw_queues = 1;
  408. mq->tag_set.cmd_size = sizeof(struct mmc_queue_req);
  409. mq->tag_set.driver_data = mq;
  410. ret = blk_mq_alloc_tag_set(&mq->tag_set);
  411. if (ret)
  412. return ret;
  413. mq->queue = blk_mq_init_queue(&mq->tag_set);
  414. if (IS_ERR(mq->queue)) {
  415. ret = PTR_ERR(mq->queue);
  416. goto free_tag_set;
  417. }
  418. mq->queue->queue_lock = lock;
  419. mq->queue->queuedata = mq;
  420. return 0;
  421. free_tag_set:
  422. blk_mq_free_tag_set(&mq->tag_set);
  423. return ret;
  424. }
  425. /* Set queue depth to get a reasonable value for q->nr_requests */
  426. #define MMC_QUEUE_DEPTH 64
  427. static int mmc_mq_init(struct mmc_queue *mq, struct mmc_card *card,
  428. spinlock_t *lock)
  429. {
  430. struct mmc_host *host = card->host;
  431. int q_depth;
  432. int ret;
  433. /*
  434. * The queue depth for CQE must match the hardware because the request
  435. * tag is used to index the hardware queue.
  436. */
  437. if (mq->use_cqe)
  438. q_depth = min_t(int, card->ext_csd.cmdq_depth, host->cqe_qdepth);
  439. else
  440. q_depth = MMC_QUEUE_DEPTH;
  441. ret = mmc_mq_init_queue(mq, q_depth, &mmc_mq_ops, lock);
  442. if (ret)
  443. return ret;
  444. blk_queue_rq_timeout(mq->queue, 60 * HZ);
  445. mmc_setup_queue(mq, card);
  446. return 0;
  447. }
  448. /**
  449. * mmc_init_queue - initialise a queue structure.
  450. * @mq: mmc queue
  451. * @card: mmc card to attach this queue
  452. * @lock: queue lock
  453. * @subname: partition subname
  454. *
  455. * Initialise a MMC card request queue.
  456. */
  457. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
  458. spinlock_t *lock, const char *subname)
  459. {
  460. struct mmc_host *host = card->host;
  461. int ret = -ENOMEM;
  462. mq->card = card;
  463. mq->use_cqe = host->cqe_enabled;
  464. if (mq->use_cqe || mmc_host_use_blk_mq(host))
  465. return mmc_mq_init(mq, card, lock);
  466. mq->queue = blk_alloc_queue(GFP_KERNEL);
  467. if (!mq->queue)
  468. return -ENOMEM;
  469. mq->queue->queue_lock = lock;
  470. mq->queue->request_fn = mmc_request_fn;
  471. mq->queue->init_rq_fn = mmc_init_request;
  472. mq->queue->exit_rq_fn = mmc_exit_request;
  473. mq->queue->cmd_size = sizeof(struct mmc_queue_req);
  474. mq->queue->queuedata = mq;
  475. mq->qcnt = 0;
  476. ret = blk_init_allocated_queue(mq->queue);
  477. if (ret) {
  478. blk_cleanup_queue(mq->queue);
  479. return ret;
  480. }
  481. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  482. mmc_setup_queue(mq, card);
  483. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
  484. host->index, subname ? subname : "");
  485. if (IS_ERR(mq->thread)) {
  486. ret = PTR_ERR(mq->thread);
  487. goto cleanup_queue;
  488. }
  489. return 0;
  490. cleanup_queue:
  491. blk_cleanup_queue(mq->queue);
  492. return ret;
  493. }
  494. static void mmc_mq_queue_suspend(struct mmc_queue *mq)
  495. {
  496. blk_mq_quiesce_queue(mq->queue);
  497. /*
  498. * The host remains claimed while there are outstanding requests, so
  499. * simply claiming and releasing here ensures there are none.
  500. */
  501. mmc_claim_host(mq->card->host);
  502. mmc_release_host(mq->card->host);
  503. }
  504. static void mmc_mq_queue_resume(struct mmc_queue *mq)
  505. {
  506. blk_mq_unquiesce_queue(mq->queue);
  507. }
  508. static void __mmc_queue_suspend(struct mmc_queue *mq)
  509. {
  510. struct request_queue *q = mq->queue;
  511. unsigned long flags;
  512. if (!mq->suspended) {
  513. mq->suspended |= true;
  514. spin_lock_irqsave(q->queue_lock, flags);
  515. blk_stop_queue(q);
  516. spin_unlock_irqrestore(q->queue_lock, flags);
  517. down(&mq->thread_sem);
  518. }
  519. }
  520. static void __mmc_queue_resume(struct mmc_queue *mq)
  521. {
  522. struct request_queue *q = mq->queue;
  523. unsigned long flags;
  524. if (mq->suspended) {
  525. mq->suspended = false;
  526. up(&mq->thread_sem);
  527. spin_lock_irqsave(q->queue_lock, flags);
  528. blk_start_queue(q);
  529. spin_unlock_irqrestore(q->queue_lock, flags);
  530. }
  531. }
  532. void mmc_cleanup_queue(struct mmc_queue *mq)
  533. {
  534. struct request_queue *q = mq->queue;
  535. unsigned long flags;
  536. if (q->mq_ops) {
  537. /*
  538. * The legacy code handled the possibility of being suspended,
  539. * so do that here too.
  540. */
  541. if (blk_queue_quiesced(q))
  542. blk_mq_unquiesce_queue(q);
  543. goto out_cleanup;
  544. }
  545. /* Make sure the queue isn't suspended, as that will deadlock */
  546. mmc_queue_resume(mq);
  547. /* Then terminate our worker thread */
  548. kthread_stop(mq->thread);
  549. /* Empty the queue */
  550. spin_lock_irqsave(q->queue_lock, flags);
  551. q->queuedata = NULL;
  552. blk_start_queue(q);
  553. spin_unlock_irqrestore(q->queue_lock, flags);
  554. out_cleanup:
  555. blk_cleanup_queue(q);
  556. /*
  557. * A request can be completed before the next request, potentially
  558. * leaving a complete_work with nothing to do. Such a work item might
  559. * still be queued at this point. Flush it.
  560. */
  561. flush_work(&mq->complete_work);
  562. mq->card = NULL;
  563. }
  564. /**
  565. * mmc_queue_suspend - suspend a MMC request queue
  566. * @mq: MMC queue to suspend
  567. *
  568. * Stop the block request queue, and wait for our thread to
  569. * complete any outstanding requests. This ensures that we
  570. * won't suspend while a request is being processed.
  571. */
  572. void mmc_queue_suspend(struct mmc_queue *mq)
  573. {
  574. struct request_queue *q = mq->queue;
  575. if (q->mq_ops)
  576. mmc_mq_queue_suspend(mq);
  577. else
  578. __mmc_queue_suspend(mq);
  579. }
  580. /**
  581. * mmc_queue_resume - resume a previously suspended MMC request queue
  582. * @mq: MMC queue to resume
  583. */
  584. void mmc_queue_resume(struct mmc_queue *mq)
  585. {
  586. struct request_queue *q = mq->queue;
  587. if (q->mq_ops)
  588. mmc_mq_queue_resume(mq);
  589. else
  590. __mmc_queue_resume(mq);
  591. }
  592. /*
  593. * Prepare the sg list(s) to be handed of to the host driver
  594. */
  595. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  596. {
  597. struct request *req = mmc_queue_req_to_req(mqrq);
  598. return blk_rq_map_sg(mq->queue, req, mqrq->sg);
  599. }