queue.c 17 KB

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