queue.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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))
  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. static void mmc_setup_queue(struct mmc_queue *mq, struct mmc_card *card)
  152. {
  153. struct mmc_host *host = card->host;
  154. u64 limit = BLK_BOUNCE_HIGH;
  155. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  156. limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
  157. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  158. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
  159. if (mmc_can_erase(card))
  160. mmc_queue_setup_discard(mq->queue, card);
  161. blk_queue_bounce_limit(mq->queue, limit);
  162. blk_queue_max_hw_sectors(mq->queue,
  163. min(host->max_blk_count, host->max_req_size / 512));
  164. blk_queue_max_segments(mq->queue, host->max_segs);
  165. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  166. /* Initialize thread_sem even if it is not used */
  167. sema_init(&mq->thread_sem, 1);
  168. }
  169. /**
  170. * mmc_init_queue - initialise a queue structure.
  171. * @mq: mmc queue
  172. * @card: mmc card to attach this queue
  173. * @lock: queue lock
  174. * @subname: partition subname
  175. *
  176. * Initialise a MMC card request queue.
  177. */
  178. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
  179. spinlock_t *lock, const char *subname)
  180. {
  181. struct mmc_host *host = card->host;
  182. int ret = -ENOMEM;
  183. mq->card = card;
  184. mq->queue = blk_alloc_queue(GFP_KERNEL);
  185. if (!mq->queue)
  186. return -ENOMEM;
  187. mq->queue->queue_lock = lock;
  188. mq->queue->request_fn = mmc_request_fn;
  189. mq->queue->init_rq_fn = mmc_init_request;
  190. mq->queue->exit_rq_fn = mmc_exit_request;
  191. mq->queue->cmd_size = sizeof(struct mmc_queue_req);
  192. mq->queue->queuedata = mq;
  193. mq->qcnt = 0;
  194. ret = blk_init_allocated_queue(mq->queue);
  195. if (ret) {
  196. blk_cleanup_queue(mq->queue);
  197. return ret;
  198. }
  199. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  200. mmc_setup_queue(mq, card);
  201. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
  202. host->index, subname ? subname : "");
  203. if (IS_ERR(mq->thread)) {
  204. ret = PTR_ERR(mq->thread);
  205. goto cleanup_queue;
  206. }
  207. return 0;
  208. cleanup_queue:
  209. blk_cleanup_queue(mq->queue);
  210. return ret;
  211. }
  212. void mmc_cleanup_queue(struct mmc_queue *mq)
  213. {
  214. struct request_queue *q = mq->queue;
  215. unsigned long flags;
  216. /* Make sure the queue isn't suspended, as that will deadlock */
  217. mmc_queue_resume(mq);
  218. /* Then terminate our worker thread */
  219. kthread_stop(mq->thread);
  220. /* Empty the queue */
  221. spin_lock_irqsave(q->queue_lock, flags);
  222. q->queuedata = NULL;
  223. blk_start_queue(q);
  224. spin_unlock_irqrestore(q->queue_lock, flags);
  225. mq->card = NULL;
  226. }
  227. EXPORT_SYMBOL(mmc_cleanup_queue);
  228. /**
  229. * mmc_queue_suspend - suspend a MMC request queue
  230. * @mq: MMC queue to suspend
  231. *
  232. * Stop the block request queue, and wait for our thread to
  233. * complete any outstanding requests. This ensures that we
  234. * won't suspend while a request is being processed.
  235. */
  236. void mmc_queue_suspend(struct mmc_queue *mq)
  237. {
  238. struct request_queue *q = mq->queue;
  239. unsigned long flags;
  240. if (!mq->suspended) {
  241. mq->suspended |= true;
  242. spin_lock_irqsave(q->queue_lock, flags);
  243. blk_stop_queue(q);
  244. spin_unlock_irqrestore(q->queue_lock, flags);
  245. down(&mq->thread_sem);
  246. }
  247. }
  248. /**
  249. * mmc_queue_resume - resume a previously suspended MMC request queue
  250. * @mq: MMC queue to resume
  251. */
  252. void mmc_queue_resume(struct mmc_queue *mq)
  253. {
  254. struct request_queue *q = mq->queue;
  255. unsigned long flags;
  256. if (mq->suspended) {
  257. mq->suspended = false;
  258. up(&mq->thread_sem);
  259. spin_lock_irqsave(q->queue_lock, flags);
  260. blk_start_queue(q);
  261. spin_unlock_irqrestore(q->queue_lock, flags);
  262. }
  263. }
  264. /*
  265. * Prepare the sg list(s) to be handed of to the host driver
  266. */
  267. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  268. {
  269. struct request *req = mmc_queue_req_to_req(mqrq);
  270. return blk_rq_map_sg(mq->queue, req, mqrq->sg);
  271. }