queue.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * linux/drivers/mmc/card/queue.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  5. * Copyright 2006-2007 Pierre Ossman
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. */
  12. #include <linux/slab.h>
  13. #include <linux/module.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/freezer.h>
  16. #include <linux/kthread.h>
  17. #include <linux/scatterlist.h>
  18. #include <linux/mmc/card.h>
  19. #include <linux/mmc/host.h>
  20. #include "queue.h"
  21. #define MMC_QUEUE_BOUNCESZ 65536
  22. #define MMC_QUEUE_SUSPENDED (1 << 0)
  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. /*
  29. * We only like normal block requests and discards.
  30. */
  31. if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
  32. blk_dump_rq_flags(req, "MMC bad request");
  33. return BLKPREP_KILL;
  34. }
  35. req->cmd_flags |= REQ_DONTPREP;
  36. return BLKPREP_OK;
  37. }
  38. static int mmc_queue_thread(void *d)
  39. {
  40. struct mmc_queue *mq = d;
  41. struct request_queue *q = mq->queue;
  42. current->flags |= PF_MEMALLOC;
  43. down(&mq->thread_sem);
  44. do {
  45. struct request *req = NULL;
  46. spin_lock_irq(q->queue_lock);
  47. set_current_state(TASK_INTERRUPTIBLE);
  48. req = blk_fetch_request(q);
  49. mq->req = req;
  50. spin_unlock_irq(q->queue_lock);
  51. if (!req) {
  52. if (kthread_should_stop()) {
  53. set_current_state(TASK_RUNNING);
  54. break;
  55. }
  56. up(&mq->thread_sem);
  57. schedule();
  58. down(&mq->thread_sem);
  59. continue;
  60. }
  61. set_current_state(TASK_RUNNING);
  62. mq->issue_fn(mq, req);
  63. } while (1);
  64. up(&mq->thread_sem);
  65. return 0;
  66. }
  67. /*
  68. * Generic MMC request handler. This is called for any queue on a
  69. * particular host. When the host is not busy, we look for a request
  70. * on any queue on this host, and attempt to issue it. This may
  71. * not be the queue we were asked to process.
  72. */
  73. static void mmc_request(struct request_queue *q)
  74. {
  75. struct mmc_queue *mq = q->queuedata;
  76. struct request *req;
  77. if (!mq) {
  78. while ((req = blk_fetch_request(q)) != NULL) {
  79. req->cmd_flags |= REQ_QUIET;
  80. __blk_end_request_all(req, -EIO);
  81. }
  82. return;
  83. }
  84. if (!mq->req)
  85. wake_up_process(mq->thread);
  86. }
  87. static void mmc_queue_setup_discard(struct request_queue *q,
  88. struct mmc_card *card)
  89. {
  90. unsigned max_discard;
  91. max_discard = mmc_calc_max_discard(card);
  92. if (!max_discard)
  93. return;
  94. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  95. q->limits.max_discard_sectors = max_discard;
  96. if (card->erased_byte == 0)
  97. q->limits.discard_zeroes_data = 1;
  98. q->limits.discard_granularity = card->pref_erase << 9;
  99. /* granularity must not be greater than max. discard */
  100. if (card->pref_erase > max_discard)
  101. q->limits.discard_granularity = 0;
  102. if (mmc_can_secure_erase_trim(card))
  103. queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD, q);
  104. }
  105. /**
  106. * mmc_init_queue - initialise a queue structure.
  107. * @mq: mmc queue
  108. * @card: mmc card to attach this queue
  109. * @lock: queue lock
  110. * @subname: partition subname
  111. *
  112. * Initialise a MMC card request queue.
  113. */
  114. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
  115. spinlock_t *lock, const char *subname)
  116. {
  117. struct mmc_host *host = card->host;
  118. u64 limit = BLK_BOUNCE_HIGH;
  119. int ret;
  120. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  121. limit = *mmc_dev(host)->dma_mask;
  122. mq->card = card;
  123. mq->queue = blk_init_queue(mmc_request, lock);
  124. if (!mq->queue)
  125. return -ENOMEM;
  126. mq->queue->queuedata = mq;
  127. mq->req = NULL;
  128. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  129. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  130. if (mmc_can_erase(card))
  131. mmc_queue_setup_discard(mq->queue, card);
  132. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  133. if (host->max_segs == 1) {
  134. unsigned int bouncesz;
  135. bouncesz = MMC_QUEUE_BOUNCESZ;
  136. if (bouncesz > host->max_req_size)
  137. bouncesz = host->max_req_size;
  138. if (bouncesz > host->max_seg_size)
  139. bouncesz = host->max_seg_size;
  140. if (bouncesz > (host->max_blk_count * 512))
  141. bouncesz = host->max_blk_count * 512;
  142. if (bouncesz > 512) {
  143. mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  144. if (!mq->bounce_buf) {
  145. printk(KERN_WARNING "%s: unable to "
  146. "allocate bounce buffer\n",
  147. mmc_card_name(card));
  148. }
  149. }
  150. if (mq->bounce_buf) {
  151. blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
  152. blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
  153. blk_queue_max_segments(mq->queue, bouncesz / 512);
  154. blk_queue_max_segment_size(mq->queue, bouncesz);
  155. mq->sg = kmalloc(sizeof(struct scatterlist),
  156. GFP_KERNEL);
  157. if (!mq->sg) {
  158. ret = -ENOMEM;
  159. goto cleanup_queue;
  160. }
  161. sg_init_table(mq->sg, 1);
  162. mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
  163. bouncesz / 512, GFP_KERNEL);
  164. if (!mq->bounce_sg) {
  165. ret = -ENOMEM;
  166. goto cleanup_queue;
  167. }
  168. sg_init_table(mq->bounce_sg, bouncesz / 512);
  169. }
  170. }
  171. #endif
  172. if (!mq->bounce_buf) {
  173. blk_queue_bounce_limit(mq->queue, limit);
  174. blk_queue_max_hw_sectors(mq->queue,
  175. min(host->max_blk_count, host->max_req_size / 512));
  176. blk_queue_max_segments(mq->queue, host->max_segs);
  177. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  178. mq->sg = kmalloc(sizeof(struct scatterlist) *
  179. host->max_segs, GFP_KERNEL);
  180. if (!mq->sg) {
  181. ret = -ENOMEM;
  182. goto cleanup_queue;
  183. }
  184. sg_init_table(mq->sg, host->max_segs);
  185. }
  186. sema_init(&mq->thread_sem, 1);
  187. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
  188. host->index, subname ? subname : "");
  189. if (IS_ERR(mq->thread)) {
  190. ret = PTR_ERR(mq->thread);
  191. goto free_bounce_sg;
  192. }
  193. return 0;
  194. free_bounce_sg:
  195. if (mq->bounce_sg)
  196. kfree(mq->bounce_sg);
  197. mq->bounce_sg = NULL;
  198. cleanup_queue:
  199. if (mq->sg)
  200. kfree(mq->sg);
  201. mq->sg = NULL;
  202. if (mq->bounce_buf)
  203. kfree(mq->bounce_buf);
  204. mq->bounce_buf = NULL;
  205. blk_cleanup_queue(mq->queue);
  206. return ret;
  207. }
  208. void mmc_cleanup_queue(struct mmc_queue *mq)
  209. {
  210. struct request_queue *q = mq->queue;
  211. unsigned long flags;
  212. /* Make sure the queue isn't suspended, as that will deadlock */
  213. mmc_queue_resume(mq);
  214. /* Then terminate our worker thread */
  215. kthread_stop(mq->thread);
  216. /* Empty the queue */
  217. spin_lock_irqsave(q->queue_lock, flags);
  218. q->queuedata = NULL;
  219. blk_start_queue(q);
  220. spin_unlock_irqrestore(q->queue_lock, flags);
  221. if (mq->bounce_sg)
  222. kfree(mq->bounce_sg);
  223. mq->bounce_sg = NULL;
  224. kfree(mq->sg);
  225. mq->sg = NULL;
  226. if (mq->bounce_buf)
  227. kfree(mq->bounce_buf);
  228. mq->bounce_buf = NULL;
  229. mq->card = NULL;
  230. }
  231. EXPORT_SYMBOL(mmc_cleanup_queue);
  232. /**
  233. * mmc_queue_suspend - suspend a MMC request queue
  234. * @mq: MMC queue to suspend
  235. *
  236. * Stop the block request queue, and wait for our thread to
  237. * complete any outstanding requests. This ensures that we
  238. * won't suspend while a request is being processed.
  239. */
  240. void mmc_queue_suspend(struct mmc_queue *mq)
  241. {
  242. struct request_queue *q = mq->queue;
  243. unsigned long flags;
  244. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  245. mq->flags |= MMC_QUEUE_SUSPENDED;
  246. spin_lock_irqsave(q->queue_lock, flags);
  247. blk_stop_queue(q);
  248. spin_unlock_irqrestore(q->queue_lock, flags);
  249. down(&mq->thread_sem);
  250. }
  251. }
  252. /**
  253. * mmc_queue_resume - resume a previously suspended MMC request queue
  254. * @mq: MMC queue to resume
  255. */
  256. void mmc_queue_resume(struct mmc_queue *mq)
  257. {
  258. struct request_queue *q = mq->queue;
  259. unsigned long flags;
  260. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  261. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  262. up(&mq->thread_sem);
  263. spin_lock_irqsave(q->queue_lock, flags);
  264. blk_start_queue(q);
  265. spin_unlock_irqrestore(q->queue_lock, flags);
  266. }
  267. }
  268. /*
  269. * Prepare the sg list(s) to be handed of to the host driver
  270. */
  271. unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
  272. {
  273. unsigned int sg_len;
  274. size_t buflen;
  275. struct scatterlist *sg;
  276. int i;
  277. if (!mq->bounce_buf)
  278. return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
  279. BUG_ON(!mq->bounce_sg);
  280. sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
  281. mq->bounce_sg_len = sg_len;
  282. buflen = 0;
  283. for_each_sg(mq->bounce_sg, sg, sg_len, i)
  284. buflen += sg->length;
  285. sg_init_one(mq->sg, mq->bounce_buf, buflen);
  286. return 1;
  287. }
  288. /*
  289. * If writing, bounce the data to the buffer before the request
  290. * is sent to the host driver
  291. */
  292. void mmc_queue_bounce_pre(struct mmc_queue *mq)
  293. {
  294. if (!mq->bounce_buf)
  295. return;
  296. if (rq_data_dir(mq->req) != WRITE)
  297. return;
  298. sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
  299. mq->bounce_buf, mq->sg[0].length);
  300. }
  301. /*
  302. * If reading, bounce the data from the buffer after the request
  303. * has been handled by the host driver
  304. */
  305. void mmc_queue_bounce_post(struct mmc_queue *mq)
  306. {
  307. if (!mq->bounce_buf)
  308. return;
  309. if (rq_data_dir(mq->req) != READ)
  310. return;
  311. sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
  312. mq->bounce_buf, mq->sg[0].length);
  313. }