queue.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.
  30. */
  31. if (!blk_fs_request(req)) {
  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. if (!blk_queue_plugged(q))
  49. req = blk_fetch_request(q);
  50. mq->req = req;
  51. spin_unlock_irq(q->queue_lock);
  52. if (!req) {
  53. if (kthread_should_stop()) {
  54. set_current_state(TASK_RUNNING);
  55. break;
  56. }
  57. up(&mq->thread_sem);
  58. schedule();
  59. down(&mq->thread_sem);
  60. continue;
  61. }
  62. set_current_state(TASK_RUNNING);
  63. mq->issue_fn(mq, req);
  64. } while (1);
  65. up(&mq->thread_sem);
  66. return 0;
  67. }
  68. /*
  69. * Generic MMC request handler. This is called for any queue on a
  70. * particular host. When the host is not busy, we look for a request
  71. * on any queue on this host, and attempt to issue it. This may
  72. * not be the queue we were asked to process.
  73. */
  74. static void mmc_request(struct request_queue *q)
  75. {
  76. struct mmc_queue *mq = q->queuedata;
  77. struct request *req;
  78. if (!mq) {
  79. while ((req = blk_fetch_request(q)) != NULL) {
  80. req->cmd_flags |= REQ_QUIET;
  81. __blk_end_request_all(req, -EIO);
  82. }
  83. return;
  84. }
  85. if (!mq->req)
  86. wake_up_process(mq->thread);
  87. }
  88. /**
  89. * mmc_init_queue - initialise a queue structure.
  90. * @mq: mmc queue
  91. * @card: mmc card to attach this queue
  92. * @lock: queue lock
  93. *
  94. * Initialise a MMC card request queue.
  95. */
  96. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
  97. {
  98. struct mmc_host *host = card->host;
  99. u64 limit = BLK_BOUNCE_HIGH;
  100. int ret;
  101. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  102. limit = *mmc_dev(host)->dma_mask;
  103. mq->card = card;
  104. mq->queue = blk_init_queue(mmc_request, lock);
  105. if (!mq->queue)
  106. return -ENOMEM;
  107. mq->queue->queuedata = mq;
  108. mq->req = NULL;
  109. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  110. blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN, NULL);
  111. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  112. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  113. if (host->max_hw_segs == 1) {
  114. unsigned int bouncesz;
  115. bouncesz = MMC_QUEUE_BOUNCESZ;
  116. if (bouncesz > host->max_req_size)
  117. bouncesz = host->max_req_size;
  118. if (bouncesz > host->max_seg_size)
  119. bouncesz = host->max_seg_size;
  120. if (bouncesz > (host->max_blk_count * 512))
  121. bouncesz = host->max_blk_count * 512;
  122. if (bouncesz > 512) {
  123. mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  124. if (!mq->bounce_buf) {
  125. printk(KERN_WARNING "%s: unable to "
  126. "allocate bounce buffer\n",
  127. mmc_card_name(card));
  128. }
  129. }
  130. if (mq->bounce_buf) {
  131. blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
  132. blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
  133. blk_queue_max_segments(mq->queue, bouncesz / 512);
  134. blk_queue_max_segment_size(mq->queue, bouncesz);
  135. mq->sg = kmalloc(sizeof(struct scatterlist),
  136. GFP_KERNEL);
  137. if (!mq->sg) {
  138. ret = -ENOMEM;
  139. goto cleanup_queue;
  140. }
  141. sg_init_table(mq->sg, 1);
  142. mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
  143. bouncesz / 512, GFP_KERNEL);
  144. if (!mq->bounce_sg) {
  145. ret = -ENOMEM;
  146. goto cleanup_queue;
  147. }
  148. sg_init_table(mq->bounce_sg, bouncesz / 512);
  149. }
  150. }
  151. #endif
  152. if (!mq->bounce_buf) {
  153. blk_queue_bounce_limit(mq->queue, limit);
  154. blk_queue_max_hw_sectors(mq->queue,
  155. min(host->max_blk_count, host->max_req_size / 512));
  156. blk_queue_max_segments(mq->queue, host->max_hw_segs);
  157. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  158. mq->sg = kmalloc(sizeof(struct scatterlist) *
  159. host->max_phys_segs, GFP_KERNEL);
  160. if (!mq->sg) {
  161. ret = -ENOMEM;
  162. goto cleanup_queue;
  163. }
  164. sg_init_table(mq->sg, host->max_phys_segs);
  165. }
  166. init_MUTEX(&mq->thread_sem);
  167. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
  168. if (IS_ERR(mq->thread)) {
  169. ret = PTR_ERR(mq->thread);
  170. goto free_bounce_sg;
  171. }
  172. return 0;
  173. free_bounce_sg:
  174. if (mq->bounce_sg)
  175. kfree(mq->bounce_sg);
  176. mq->bounce_sg = NULL;
  177. cleanup_queue:
  178. if (mq->sg)
  179. kfree(mq->sg);
  180. mq->sg = NULL;
  181. if (mq->bounce_buf)
  182. kfree(mq->bounce_buf);
  183. mq->bounce_buf = NULL;
  184. blk_cleanup_queue(mq->queue);
  185. return ret;
  186. }
  187. void mmc_cleanup_queue(struct mmc_queue *mq)
  188. {
  189. struct request_queue *q = mq->queue;
  190. unsigned long flags;
  191. /* Make sure the queue isn't suspended, as that will deadlock */
  192. mmc_queue_resume(mq);
  193. /* Then terminate our worker thread */
  194. kthread_stop(mq->thread);
  195. /* Empty the queue */
  196. spin_lock_irqsave(q->queue_lock, flags);
  197. q->queuedata = NULL;
  198. blk_start_queue(q);
  199. spin_unlock_irqrestore(q->queue_lock, flags);
  200. if (mq->bounce_sg)
  201. kfree(mq->bounce_sg);
  202. mq->bounce_sg = NULL;
  203. kfree(mq->sg);
  204. mq->sg = NULL;
  205. if (mq->bounce_buf)
  206. kfree(mq->bounce_buf);
  207. mq->bounce_buf = NULL;
  208. mq->card = NULL;
  209. }
  210. EXPORT_SYMBOL(mmc_cleanup_queue);
  211. /**
  212. * mmc_queue_suspend - suspend a MMC request queue
  213. * @mq: MMC queue to suspend
  214. *
  215. * Stop the block request queue, and wait for our thread to
  216. * complete any outstanding requests. This ensures that we
  217. * won't suspend while a request is being processed.
  218. */
  219. void mmc_queue_suspend(struct mmc_queue *mq)
  220. {
  221. struct request_queue *q = mq->queue;
  222. unsigned long flags;
  223. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  224. mq->flags |= MMC_QUEUE_SUSPENDED;
  225. spin_lock_irqsave(q->queue_lock, flags);
  226. blk_stop_queue(q);
  227. spin_unlock_irqrestore(q->queue_lock, flags);
  228. down(&mq->thread_sem);
  229. }
  230. }
  231. /**
  232. * mmc_queue_resume - resume a previously suspended MMC request queue
  233. * @mq: MMC queue to resume
  234. */
  235. void mmc_queue_resume(struct mmc_queue *mq)
  236. {
  237. struct request_queue *q = mq->queue;
  238. unsigned long flags;
  239. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  240. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  241. up(&mq->thread_sem);
  242. spin_lock_irqsave(q->queue_lock, flags);
  243. blk_start_queue(q);
  244. spin_unlock_irqrestore(q->queue_lock, flags);
  245. }
  246. }
  247. /*
  248. * Prepare the sg list(s) to be handed of to the host driver
  249. */
  250. unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
  251. {
  252. unsigned int sg_len;
  253. size_t buflen;
  254. struct scatterlist *sg;
  255. int i;
  256. if (!mq->bounce_buf)
  257. return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
  258. BUG_ON(!mq->bounce_sg);
  259. sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
  260. mq->bounce_sg_len = sg_len;
  261. buflen = 0;
  262. for_each_sg(mq->bounce_sg, sg, sg_len, i)
  263. buflen += sg->length;
  264. sg_init_one(mq->sg, mq->bounce_buf, buflen);
  265. return 1;
  266. }
  267. /*
  268. * If writing, bounce the data to the buffer before the request
  269. * is sent to the host driver
  270. */
  271. void mmc_queue_bounce_pre(struct mmc_queue *mq)
  272. {
  273. unsigned long flags;
  274. if (!mq->bounce_buf)
  275. return;
  276. if (rq_data_dir(mq->req) != WRITE)
  277. return;
  278. local_irq_save(flags);
  279. sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
  280. mq->bounce_buf, mq->sg[0].length);
  281. local_irq_restore(flags);
  282. }
  283. /*
  284. * If reading, bounce the data from the buffer after the request
  285. * has been handled by the host driver
  286. */
  287. void mmc_queue_bounce_post(struct mmc_queue *mq)
  288. {
  289. unsigned long flags;
  290. if (!mq->bounce_buf)
  291. return;
  292. if (rq_data_dir(mq->req) != READ)
  293. return;
  294. local_irq_save(flags);
  295. sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
  296. mq->bounce_buf, mq->sg[0].length);
  297. local_irq_restore(flags);
  298. }