queue.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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/module.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/freezer.h>
  15. #include <linux/kthread.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/host.h>
  19. #include "queue.h"
  20. #define MMC_QUEUE_BOUNCESZ 65536
  21. #define MMC_QUEUE_SUSPENDED (1 << 0)
  22. /*
  23. * Prepare a MMC request. This just filters out odd stuff.
  24. */
  25. static int mmc_prep_request(struct request_queue *q, struct request *req)
  26. {
  27. /*
  28. * We only like normal block requests.
  29. */
  30. if (!blk_fs_request(req)) {
  31. blk_dump_rq_flags(req, "MMC bad request");
  32. return BLKPREP_KILL;
  33. }
  34. req->cmd_flags |= REQ_DONTPREP;
  35. return BLKPREP_OK;
  36. }
  37. static int mmc_queue_thread(void *d)
  38. {
  39. struct mmc_queue *mq = d;
  40. struct request_queue *q = mq->queue;
  41. current->flags |= PF_MEMALLOC;
  42. down(&mq->thread_sem);
  43. do {
  44. struct request *req = NULL;
  45. spin_lock_irq(q->queue_lock);
  46. set_current_state(TASK_INTERRUPTIBLE);
  47. if (!blk_queue_plugged(q))
  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. /**
  88. * mmc_init_queue - initialise a queue structure.
  89. * @mq: mmc queue
  90. * @card: mmc card to attach this queue
  91. * @lock: queue lock
  92. *
  93. * Initialise a MMC card request queue.
  94. */
  95. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
  96. {
  97. struct mmc_host *host = card->host;
  98. u64 limit = BLK_BOUNCE_HIGH;
  99. int ret;
  100. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  101. limit = *mmc_dev(host)->dma_mask;
  102. mq->card = card;
  103. mq->queue = blk_init_queue(mmc_request, lock);
  104. if (!mq->queue)
  105. return -ENOMEM;
  106. mq->queue->queuedata = mq;
  107. mq->req = NULL;
  108. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  109. blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN, NULL);
  110. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  111. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  112. if (host->max_hw_segs == 1) {
  113. unsigned int bouncesz;
  114. bouncesz = MMC_QUEUE_BOUNCESZ;
  115. if (bouncesz > host->max_req_size)
  116. bouncesz = host->max_req_size;
  117. if (bouncesz > host->max_seg_size)
  118. bouncesz = host->max_seg_size;
  119. if (bouncesz > (host->max_blk_count * 512))
  120. bouncesz = host->max_blk_count * 512;
  121. if (bouncesz > 512) {
  122. mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  123. if (!mq->bounce_buf) {
  124. printk(KERN_WARNING "%s: unable to "
  125. "allocate bounce buffer\n",
  126. mmc_card_name(card));
  127. }
  128. }
  129. if (mq->bounce_buf) {
  130. blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
  131. blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
  132. blk_queue_max_segments(mq->queue, bouncesz / 512);
  133. blk_queue_max_segment_size(mq->queue, bouncesz);
  134. mq->sg = kmalloc(sizeof(struct scatterlist),
  135. GFP_KERNEL);
  136. if (!mq->sg) {
  137. ret = -ENOMEM;
  138. goto cleanup_queue;
  139. }
  140. sg_init_table(mq->sg, 1);
  141. mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
  142. bouncesz / 512, GFP_KERNEL);
  143. if (!mq->bounce_sg) {
  144. ret = -ENOMEM;
  145. goto cleanup_queue;
  146. }
  147. sg_init_table(mq->bounce_sg, bouncesz / 512);
  148. }
  149. }
  150. #endif
  151. if (!mq->bounce_buf) {
  152. blk_queue_bounce_limit(mq->queue, limit);
  153. blk_queue_max_hw_sectors(mq->queue,
  154. min(host->max_blk_count, host->max_req_size / 512));
  155. blk_queue_max_segments(mq->queue, host->max_hw_segs);
  156. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  157. mq->sg = kmalloc(sizeof(struct scatterlist) *
  158. host->max_phys_segs, GFP_KERNEL);
  159. if (!mq->sg) {
  160. ret = -ENOMEM;
  161. goto cleanup_queue;
  162. }
  163. sg_init_table(mq->sg, host->max_phys_segs);
  164. }
  165. init_MUTEX(&mq->thread_sem);
  166. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
  167. if (IS_ERR(mq->thread)) {
  168. ret = PTR_ERR(mq->thread);
  169. goto free_bounce_sg;
  170. }
  171. return 0;
  172. free_bounce_sg:
  173. if (mq->bounce_sg)
  174. kfree(mq->bounce_sg);
  175. mq->bounce_sg = NULL;
  176. cleanup_queue:
  177. if (mq->sg)
  178. kfree(mq->sg);
  179. mq->sg = NULL;
  180. if (mq->bounce_buf)
  181. kfree(mq->bounce_buf);
  182. mq->bounce_buf = NULL;
  183. blk_cleanup_queue(mq->queue);
  184. return ret;
  185. }
  186. void mmc_cleanup_queue(struct mmc_queue *mq)
  187. {
  188. struct request_queue *q = mq->queue;
  189. unsigned long flags;
  190. /* Make sure the queue isn't suspended, as that will deadlock */
  191. mmc_queue_resume(mq);
  192. /* Then terminate our worker thread */
  193. kthread_stop(mq->thread);
  194. /* Empty the queue */
  195. spin_lock_irqsave(q->queue_lock, flags);
  196. q->queuedata = NULL;
  197. blk_start_queue(q);
  198. spin_unlock_irqrestore(q->queue_lock, flags);
  199. if (mq->bounce_sg)
  200. kfree(mq->bounce_sg);
  201. mq->bounce_sg = NULL;
  202. kfree(mq->sg);
  203. mq->sg = NULL;
  204. if (mq->bounce_buf)
  205. kfree(mq->bounce_buf);
  206. mq->bounce_buf = NULL;
  207. mq->card = NULL;
  208. }
  209. EXPORT_SYMBOL(mmc_cleanup_queue);
  210. /**
  211. * mmc_queue_suspend - suspend a MMC request queue
  212. * @mq: MMC queue to suspend
  213. *
  214. * Stop the block request queue, and wait for our thread to
  215. * complete any outstanding requests. This ensures that we
  216. * won't suspend while a request is being processed.
  217. */
  218. void mmc_queue_suspend(struct mmc_queue *mq)
  219. {
  220. struct request_queue *q = mq->queue;
  221. unsigned long flags;
  222. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  223. mq->flags |= MMC_QUEUE_SUSPENDED;
  224. spin_lock_irqsave(q->queue_lock, flags);
  225. blk_stop_queue(q);
  226. spin_unlock_irqrestore(q->queue_lock, flags);
  227. down(&mq->thread_sem);
  228. }
  229. }
  230. /**
  231. * mmc_queue_resume - resume a previously suspended MMC request queue
  232. * @mq: MMC queue to resume
  233. */
  234. void mmc_queue_resume(struct mmc_queue *mq)
  235. {
  236. struct request_queue *q = mq->queue;
  237. unsigned long flags;
  238. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  239. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  240. up(&mq->thread_sem);
  241. spin_lock_irqsave(q->queue_lock, flags);
  242. blk_start_queue(q);
  243. spin_unlock_irqrestore(q->queue_lock, flags);
  244. }
  245. }
  246. /*
  247. * Prepare the sg list(s) to be handed of to the host driver
  248. */
  249. unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
  250. {
  251. unsigned int sg_len;
  252. size_t buflen;
  253. struct scatterlist *sg;
  254. int i;
  255. if (!mq->bounce_buf)
  256. return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
  257. BUG_ON(!mq->bounce_sg);
  258. sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
  259. mq->bounce_sg_len = sg_len;
  260. buflen = 0;
  261. for_each_sg(mq->bounce_sg, sg, sg_len, i)
  262. buflen += sg->length;
  263. sg_init_one(mq->sg, mq->bounce_buf, buflen);
  264. return 1;
  265. }
  266. /*
  267. * If writing, bounce the data to the buffer before the request
  268. * is sent to the host driver
  269. */
  270. void mmc_queue_bounce_pre(struct mmc_queue *mq)
  271. {
  272. unsigned long flags;
  273. if (!mq->bounce_buf)
  274. return;
  275. if (rq_data_dir(mq->req) != WRITE)
  276. return;
  277. local_irq_save(flags);
  278. sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
  279. mq->bounce_buf, mq->sg[0].length);
  280. local_irq_restore(flags);
  281. }
  282. /*
  283. * If reading, bounce the data from the buffer after the request
  284. * has been handled by the host driver
  285. */
  286. void mmc_queue_bounce_post(struct mmc_queue *mq)
  287. {
  288. unsigned long flags;
  289. if (!mq->bounce_buf)
  290. return;
  291. if (rq_data_dir(mq->req) != READ)
  292. return;
  293. local_irq_save(flags);
  294. sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
  295. mq->bounce_buf, mq->sg[0].length);
  296. local_irq_restore(flags);
  297. }