queue.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * linux/drivers/mmc/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/kthread.h>
  15. #include <linux/mmc/card.h>
  16. #include <linux/mmc/host.h>
  17. #include "queue.h"
  18. #define MMC_QUEUE_SUSPENDED (1 << 0)
  19. /*
  20. * Prepare a MMC request. Essentially, this means passing the
  21. * preparation off to the media driver. The media driver will
  22. * create a mmc_io_request in req->special.
  23. */
  24. static int mmc_prep_request(struct request_queue *q, struct request *req)
  25. {
  26. struct mmc_queue *mq = q->queuedata;
  27. int ret = BLKPREP_KILL;
  28. if (blk_special_request(req)) {
  29. /*
  30. * Special commands already have the command
  31. * blocks already setup in req->special.
  32. */
  33. BUG_ON(!req->special);
  34. ret = BLKPREP_OK;
  35. } else if (blk_fs_request(req) || blk_pc_request(req)) {
  36. /*
  37. * Block I/O requests need translating according
  38. * to the protocol.
  39. */
  40. ret = mq->prep_fn(mq, req);
  41. } else {
  42. /*
  43. * Everything else is invalid.
  44. */
  45. blk_dump_rq_flags(req, "MMC bad request");
  46. }
  47. if (ret == BLKPREP_OK)
  48. req->cmd_flags |= REQ_DONTPREP;
  49. return ret;
  50. }
  51. static int mmc_queue_thread(void *d)
  52. {
  53. struct mmc_queue *mq = d;
  54. struct request_queue *q = mq->queue;
  55. /*
  56. * Set iothread to ensure that we aren't put to sleep by
  57. * the process freezing. We handle suspension ourselves.
  58. */
  59. current->flags |= PF_MEMALLOC|PF_NOFREEZE;
  60. down(&mq->thread_sem);
  61. do {
  62. struct request *req = NULL;
  63. spin_lock_irq(q->queue_lock);
  64. set_current_state(TASK_INTERRUPTIBLE);
  65. if (!blk_queue_plugged(q))
  66. req = elv_next_request(q);
  67. mq->req = req;
  68. spin_unlock_irq(q->queue_lock);
  69. if (!req) {
  70. if (kthread_should_stop()) {
  71. set_current_state(TASK_RUNNING);
  72. break;
  73. }
  74. up(&mq->thread_sem);
  75. schedule();
  76. down(&mq->thread_sem);
  77. continue;
  78. }
  79. set_current_state(TASK_RUNNING);
  80. mq->issue_fn(mq, req);
  81. } while (1);
  82. up(&mq->thread_sem);
  83. return 0;
  84. }
  85. /*
  86. * Generic MMC request handler. This is called for any queue on a
  87. * particular host. When the host is not busy, we look for a request
  88. * on any queue on this host, and attempt to issue it. This may
  89. * not be the queue we were asked to process.
  90. */
  91. static void mmc_request(request_queue_t *q)
  92. {
  93. struct mmc_queue *mq = q->queuedata;
  94. struct request *req;
  95. int ret;
  96. if (!mq) {
  97. printk(KERN_ERR "MMC: killing requests for dead queue\n");
  98. while ((req = elv_next_request(q)) != NULL) {
  99. do {
  100. ret = end_that_request_chunk(req, 0,
  101. req->current_nr_sectors << 9);
  102. } while (ret);
  103. }
  104. return;
  105. }
  106. if (!mq->req)
  107. wake_up_process(mq->thread);
  108. }
  109. /**
  110. * mmc_init_queue - initialise a queue structure.
  111. * @mq: mmc queue
  112. * @card: mmc card to attach this queue
  113. * @lock: queue lock
  114. *
  115. * Initialise a MMC card request queue.
  116. */
  117. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
  118. {
  119. struct mmc_host *host = card->host;
  120. u64 limit = BLK_BOUNCE_HIGH;
  121. int ret;
  122. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  123. limit = *mmc_dev(host)->dma_mask;
  124. mq->card = card;
  125. mq->queue = blk_init_queue(mmc_request, lock);
  126. if (!mq->queue)
  127. return -ENOMEM;
  128. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  129. blk_queue_bounce_limit(mq->queue, limit);
  130. blk_queue_max_sectors(mq->queue, host->max_req_size / 512);
  131. blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
  132. blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
  133. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  134. mq->queue->queuedata = mq;
  135. mq->req = NULL;
  136. mq->sg = kmalloc(sizeof(struct scatterlist) * host->max_phys_segs,
  137. GFP_KERNEL);
  138. if (!mq->sg) {
  139. ret = -ENOMEM;
  140. goto cleanup_queue;
  141. }
  142. init_MUTEX(&mq->thread_sem);
  143. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
  144. if (IS_ERR(mq->thread)) {
  145. ret = PTR_ERR(mq->thread);
  146. goto free_sg;
  147. }
  148. return 0;
  149. free_sg:
  150. kfree(mq->sg);
  151. mq->sg = NULL;
  152. cleanup_queue:
  153. blk_cleanup_queue(mq->queue);
  154. return ret;
  155. }
  156. void mmc_cleanup_queue(struct mmc_queue *mq)
  157. {
  158. request_queue_t *q = mq->queue;
  159. unsigned long flags;
  160. /* Mark that we should start throwing out stragglers */
  161. spin_lock_irqsave(q->queue_lock, flags);
  162. q->queuedata = NULL;
  163. spin_unlock_irqrestore(q->queue_lock, flags);
  164. /* Make sure the queue isn't suspended, as that will deadlock */
  165. mmc_queue_resume(mq);
  166. /* Then terminate our worker thread */
  167. kthread_stop(mq->thread);
  168. kfree(mq->sg);
  169. mq->sg = NULL;
  170. blk_cleanup_queue(mq->queue);
  171. mq->card = NULL;
  172. }
  173. EXPORT_SYMBOL(mmc_cleanup_queue);
  174. /**
  175. * mmc_queue_suspend - suspend a MMC request queue
  176. * @mq: MMC queue to suspend
  177. *
  178. * Stop the block request queue, and wait for our thread to
  179. * complete any outstanding requests. This ensures that we
  180. * won't suspend while a request is being processed.
  181. */
  182. void mmc_queue_suspend(struct mmc_queue *mq)
  183. {
  184. request_queue_t *q = mq->queue;
  185. unsigned long flags;
  186. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  187. mq->flags |= MMC_QUEUE_SUSPENDED;
  188. spin_lock_irqsave(q->queue_lock, flags);
  189. blk_stop_queue(q);
  190. spin_unlock_irqrestore(q->queue_lock, flags);
  191. down(&mq->thread_sem);
  192. }
  193. }
  194. /**
  195. * mmc_queue_resume - resume a previously suspended MMC request queue
  196. * @mq: MMC queue to resume
  197. */
  198. void mmc_queue_resume(struct mmc_queue *mq)
  199. {
  200. request_queue_t *q = mq->queue;
  201. unsigned long flags;
  202. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  203. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  204. up(&mq->thread_sem);
  205. spin_lock_irqsave(q->queue_lock, flags);
  206. blk_start_queue(q);
  207. spin_unlock_irqrestore(q->queue_lock, flags);
  208. }
  209. }