mmc_queue.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * linux/drivers/mmc/mmc_queue.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/mmc/card.h>
  14. #include <linux/mmc/host.h>
  15. #include "mmc_queue.h"
  16. #define MMC_QUEUE_EXIT (1 << 0)
  17. #define MMC_QUEUE_SUSPENDED (1 << 1)
  18. /*
  19. * Prepare a MMC request. Essentially, this means passing the
  20. * preparation off to the media driver. The media driver will
  21. * create a mmc_io_request in req->special.
  22. */
  23. static int mmc_prep_request(struct request_queue *q, struct request *req)
  24. {
  25. struct mmc_queue *mq = q->queuedata;
  26. int ret = BLKPREP_KILL;
  27. if (req->flags & REQ_SPECIAL) {
  28. /*
  29. * Special commands already have the command
  30. * blocks already setup in req->special.
  31. */
  32. BUG_ON(!req->special);
  33. ret = BLKPREP_OK;
  34. } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  35. /*
  36. * Block I/O requests need translating according
  37. * to the protocol.
  38. */
  39. ret = mq->prep_fn(mq, req);
  40. } else {
  41. /*
  42. * Everything else is invalid.
  43. */
  44. blk_dump_rq_flags(req, "MMC bad request");
  45. }
  46. if (ret == BLKPREP_OK)
  47. req->flags |= REQ_DONTPREP;
  48. return ret;
  49. }
  50. static int mmc_queue_thread(void *d)
  51. {
  52. struct mmc_queue *mq = d;
  53. struct request_queue *q = mq->queue;
  54. DECLARE_WAITQUEUE(wait, current);
  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. daemonize("mmcqd");
  61. complete(&mq->thread_complete);
  62. down(&mq->thread_sem);
  63. add_wait_queue(&mq->thread_wq, &wait);
  64. do {
  65. struct request *req = NULL;
  66. spin_lock_irq(q->queue_lock);
  67. set_current_state(TASK_INTERRUPTIBLE);
  68. if (!blk_queue_plugged(q))
  69. mq->req = req = elv_next_request(q);
  70. spin_unlock_irq(q->queue_lock);
  71. if (!req) {
  72. if (mq->flags & MMC_QUEUE_EXIT)
  73. break;
  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. remove_wait_queue(&mq->thread_wq, &wait);
  83. up(&mq->thread_sem);
  84. complete_and_exit(&mq->thread_complete, 0);
  85. return 0;
  86. }
  87. /*
  88. * Generic MMC request handler. This is called for any queue on a
  89. * particular host. When the host is not busy, we look for a request
  90. * on any queue on this host, and attempt to issue it. This may
  91. * not be the queue we were asked to process.
  92. */
  93. static void mmc_request(request_queue_t *q)
  94. {
  95. struct mmc_queue *mq = q->queuedata;
  96. if (!mq->req)
  97. wake_up(&mq->thread_wq);
  98. }
  99. /**
  100. * mmc_init_queue - initialise a queue structure.
  101. * @mq: mmc queue
  102. * @card: mmc card to attach this queue
  103. * @lock: queue lock
  104. *
  105. * Initialise a MMC card request queue.
  106. */
  107. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
  108. {
  109. struct mmc_host *host = card->host;
  110. u64 limit = BLK_BOUNCE_HIGH;
  111. int ret;
  112. if (host->dev->dma_mask && *host->dev->dma_mask)
  113. limit = *host->dev->dma_mask;
  114. mq->card = card;
  115. mq->queue = blk_init_queue(mmc_request, lock);
  116. if (!mq->queue)
  117. return -ENOMEM;
  118. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  119. blk_queue_bounce_limit(mq->queue, limit);
  120. blk_queue_max_sectors(mq->queue, host->max_sectors);
  121. blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
  122. blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
  123. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  124. mq->queue->queuedata = mq;
  125. mq->req = NULL;
  126. mq->sg = kmalloc(sizeof(struct scatterlist) * host->max_phys_segs,
  127. GFP_KERNEL);
  128. if (!mq->sg) {
  129. ret = -ENOMEM;
  130. goto cleanup;
  131. }
  132. init_completion(&mq->thread_complete);
  133. init_waitqueue_head(&mq->thread_wq);
  134. init_MUTEX(&mq->thread_sem);
  135. ret = kernel_thread(mmc_queue_thread, mq, CLONE_KERNEL);
  136. if (ret >= 0) {
  137. wait_for_completion(&mq->thread_complete);
  138. init_completion(&mq->thread_complete);
  139. ret = 0;
  140. goto out;
  141. }
  142. cleanup:
  143. kfree(mq->sg);
  144. mq->sg = NULL;
  145. blk_cleanup_queue(mq->queue);
  146. out:
  147. return ret;
  148. }
  149. EXPORT_SYMBOL(mmc_init_queue);
  150. void mmc_cleanup_queue(struct mmc_queue *mq)
  151. {
  152. mq->flags |= MMC_QUEUE_EXIT;
  153. wake_up(&mq->thread_wq);
  154. wait_for_completion(&mq->thread_complete);
  155. kfree(mq->sg);
  156. mq->sg = NULL;
  157. blk_cleanup_queue(mq->queue);
  158. mq->card = NULL;
  159. }
  160. EXPORT_SYMBOL(mmc_cleanup_queue);
  161. /**
  162. * mmc_queue_suspend - suspend a MMC request queue
  163. * @mq: MMC queue to suspend
  164. *
  165. * Stop the block request queue, and wait for our thread to
  166. * complete any outstanding requests. This ensures that we
  167. * won't suspend while a request is being processed.
  168. */
  169. void mmc_queue_suspend(struct mmc_queue *mq)
  170. {
  171. request_queue_t *q = mq->queue;
  172. unsigned long flags;
  173. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  174. mq->flags |= MMC_QUEUE_SUSPENDED;
  175. spin_lock_irqsave(q->queue_lock, flags);
  176. blk_stop_queue(q);
  177. spin_unlock_irqrestore(q->queue_lock, flags);
  178. down(&mq->thread_sem);
  179. }
  180. }
  181. EXPORT_SYMBOL(mmc_queue_suspend);
  182. /**
  183. * mmc_queue_resume - resume a previously suspended MMC request queue
  184. * @mq: MMC queue to resume
  185. */
  186. void mmc_queue_resume(struct mmc_queue *mq)
  187. {
  188. request_queue_t *q = mq->queue;
  189. unsigned long flags;
  190. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  191. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  192. up(&mq->thread_sem);
  193. spin_lock_irqsave(q->queue_lock, flags);
  194. blk_start_queue(q);
  195. spin_unlock_irqrestore(q->queue_lock, flags);
  196. }
  197. }
  198. EXPORT_SYMBOL(mmc_queue_resume);