queue.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. /*
  24. * Prepare a MMC request. This just filters out odd stuff.
  25. */
  26. static int mmc_prep_request(struct request_queue *q, struct request *req)
  27. {
  28. struct mmc_queue *mq = q->queuedata;
  29. if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
  30. return BLKPREP_KILL;
  31. req->rq_flags |= RQF_DONTPREP;
  32. return BLKPREP_OK;
  33. }
  34. static int mmc_queue_thread(void *d)
  35. {
  36. struct mmc_queue *mq = d;
  37. struct request_queue *q = mq->queue;
  38. struct mmc_context_info *cntx = &mq->card->host->context_info;
  39. current->flags |= PF_MEMALLOC;
  40. down(&mq->thread_sem);
  41. do {
  42. struct request *req;
  43. spin_lock_irq(q->queue_lock);
  44. set_current_state(TASK_INTERRUPTIBLE);
  45. req = blk_fetch_request(q);
  46. mq->asleep = false;
  47. cntx->is_waiting_last_req = false;
  48. cntx->is_new_req = false;
  49. if (!req) {
  50. /*
  51. * Dispatch queue is empty so set flags for
  52. * mmc_request_fn() to wake us up.
  53. */
  54. if (mq->qcnt)
  55. cntx->is_waiting_last_req = true;
  56. else
  57. mq->asleep = true;
  58. }
  59. spin_unlock_irq(q->queue_lock);
  60. if (req || mq->qcnt) {
  61. set_current_state(TASK_RUNNING);
  62. mmc_blk_issue_rq(mq, req);
  63. cond_resched();
  64. } else {
  65. if (kthread_should_stop()) {
  66. set_current_state(TASK_RUNNING);
  67. break;
  68. }
  69. up(&mq->thread_sem);
  70. schedule();
  71. down(&mq->thread_sem);
  72. }
  73. } while (1);
  74. up(&mq->thread_sem);
  75. return 0;
  76. }
  77. /*
  78. * Generic MMC request handler. This is called for any queue on a
  79. * particular host. When the host is not busy, we look for a request
  80. * on any queue on this host, and attempt to issue it. This may
  81. * not be the queue we were asked to process.
  82. */
  83. static void mmc_request_fn(struct request_queue *q)
  84. {
  85. struct mmc_queue *mq = q->queuedata;
  86. struct request *req;
  87. struct mmc_context_info *cntx;
  88. if (!mq) {
  89. while ((req = blk_fetch_request(q)) != NULL) {
  90. req->rq_flags |= RQF_QUIET;
  91. __blk_end_request_all(req, BLK_STS_IOERR);
  92. }
  93. return;
  94. }
  95. cntx = &mq->card->host->context_info;
  96. if (cntx->is_waiting_last_req) {
  97. cntx->is_new_req = true;
  98. wake_up_interruptible(&cntx->wait);
  99. }
  100. if (mq->asleep)
  101. wake_up_process(mq->thread);
  102. }
  103. static struct scatterlist *mmc_alloc_sg(int sg_len, gfp_t gfp)
  104. {
  105. struct scatterlist *sg;
  106. sg = kmalloc_array(sg_len, sizeof(*sg), gfp);
  107. if (sg)
  108. sg_init_table(sg, sg_len);
  109. return sg;
  110. }
  111. static void mmc_queue_setup_discard(struct request_queue *q,
  112. struct mmc_card *card)
  113. {
  114. unsigned max_discard;
  115. max_discard = mmc_calc_max_discard(card);
  116. if (!max_discard)
  117. return;
  118. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  119. blk_queue_max_discard_sectors(q, max_discard);
  120. q->limits.discard_granularity = card->pref_erase << 9;
  121. /* granularity must not be greater than max. discard */
  122. if (card->pref_erase > max_discard)
  123. q->limits.discard_granularity = 0;
  124. if (mmc_can_secure_erase_trim(card))
  125. queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
  126. }
  127. /**
  128. * mmc_init_request() - initialize the MMC-specific per-request data
  129. * @q: the request queue
  130. * @req: the request
  131. * @gfp: memory allocation policy
  132. */
  133. static int mmc_init_request(struct request_queue *q, struct request *req,
  134. gfp_t gfp)
  135. {
  136. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  137. struct mmc_queue *mq = q->queuedata;
  138. struct mmc_card *card = mq->card;
  139. struct mmc_host *host = card->host;
  140. mq_rq->sg = mmc_alloc_sg(host->max_segs, gfp);
  141. if (!mq_rq->sg)
  142. return -ENOMEM;
  143. return 0;
  144. }
  145. static void mmc_exit_request(struct request_queue *q, struct request *req)
  146. {
  147. struct mmc_queue_req *mq_rq = req_to_mmc_queue_req(req);
  148. kfree(mq_rq->sg);
  149. mq_rq->sg = NULL;
  150. }
  151. /**
  152. * mmc_init_queue - initialise a queue structure.
  153. * @mq: mmc queue
  154. * @card: mmc card to attach this queue
  155. * @lock: queue lock
  156. * @subname: partition subname
  157. *
  158. * Initialise a MMC card request queue.
  159. */
  160. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
  161. spinlock_t *lock, const char *subname)
  162. {
  163. struct mmc_host *host = card->host;
  164. u64 limit = BLK_BOUNCE_HIGH;
  165. int ret = -ENOMEM;
  166. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  167. limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
  168. mq->card = card;
  169. mq->queue = blk_alloc_queue(GFP_KERNEL);
  170. if (!mq->queue)
  171. return -ENOMEM;
  172. mq->queue->queue_lock = lock;
  173. mq->queue->request_fn = mmc_request_fn;
  174. mq->queue->init_rq_fn = mmc_init_request;
  175. mq->queue->exit_rq_fn = mmc_exit_request;
  176. mq->queue->cmd_size = sizeof(struct mmc_queue_req);
  177. mq->queue->queuedata = mq;
  178. mq->qcnt = 0;
  179. ret = blk_init_allocated_queue(mq->queue);
  180. if (ret) {
  181. blk_cleanup_queue(mq->queue);
  182. return ret;
  183. }
  184. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  185. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  186. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
  187. if (mmc_can_erase(card))
  188. mmc_queue_setup_discard(mq->queue, card);
  189. blk_queue_bounce_limit(mq->queue, limit);
  190. blk_queue_max_hw_sectors(mq->queue,
  191. min(host->max_blk_count, host->max_req_size / 512));
  192. blk_queue_max_segments(mq->queue, host->max_segs);
  193. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  194. sema_init(&mq->thread_sem, 1);
  195. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
  196. host->index, subname ? subname : "");
  197. if (IS_ERR(mq->thread)) {
  198. ret = PTR_ERR(mq->thread);
  199. goto cleanup_queue;
  200. }
  201. return 0;
  202. cleanup_queue:
  203. blk_cleanup_queue(mq->queue);
  204. return ret;
  205. }
  206. void mmc_cleanup_queue(struct mmc_queue *mq)
  207. {
  208. struct request_queue *q = mq->queue;
  209. unsigned long flags;
  210. /* Make sure the queue isn't suspended, as that will deadlock */
  211. mmc_queue_resume(mq);
  212. /* Then terminate our worker thread */
  213. kthread_stop(mq->thread);
  214. /* Empty the queue */
  215. spin_lock_irqsave(q->queue_lock, flags);
  216. q->queuedata = NULL;
  217. blk_start_queue(q);
  218. spin_unlock_irqrestore(q->queue_lock, flags);
  219. mq->card = NULL;
  220. }
  221. EXPORT_SYMBOL(mmc_cleanup_queue);
  222. /**
  223. * mmc_queue_suspend - suspend a MMC request queue
  224. * @mq: MMC queue to suspend
  225. *
  226. * Stop the block request queue, and wait for our thread to
  227. * complete any outstanding requests. This ensures that we
  228. * won't suspend while a request is being processed.
  229. */
  230. void mmc_queue_suspend(struct mmc_queue *mq)
  231. {
  232. struct request_queue *q = mq->queue;
  233. unsigned long flags;
  234. if (!mq->suspended) {
  235. mq->suspended |= true;
  236. spin_lock_irqsave(q->queue_lock, flags);
  237. blk_stop_queue(q);
  238. spin_unlock_irqrestore(q->queue_lock, flags);
  239. down(&mq->thread_sem);
  240. }
  241. }
  242. /**
  243. * mmc_queue_resume - resume a previously suspended MMC request queue
  244. * @mq: MMC queue to resume
  245. */
  246. void mmc_queue_resume(struct mmc_queue *mq)
  247. {
  248. struct request_queue *q = mq->queue;
  249. unsigned long flags;
  250. if (mq->suspended) {
  251. mq->suspended = false;
  252. up(&mq->thread_sem);
  253. spin_lock_irqsave(q->queue_lock, flags);
  254. blk_start_queue(q);
  255. spin_unlock_irqrestore(q->queue_lock, flags);
  256. }
  257. }
  258. /*
  259. * Prepare the sg list(s) to be handed of to the host driver
  260. */
  261. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  262. {
  263. struct request *req = mmc_queue_req_to_req(mqrq);
  264. return blk_rq_map_sg(mq->queue, req, mqrq->sg);
  265. }