mmc_queue.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 (blk_special_request(req)) {
  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 (blk_fs_request(req) || blk_pc_request(req)) {
  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->cmd_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. req = elv_next_request(q);
  70. mq->req = req;
  71. spin_unlock_irq(q->queue_lock);
  72. if (!req) {
  73. if (mq->flags & MMC_QUEUE_EXIT)
  74. break;
  75. up(&mq->thread_sem);
  76. schedule();
  77. down(&mq->thread_sem);
  78. continue;
  79. }
  80. set_current_state(TASK_RUNNING);
  81. mq->issue_fn(mq, req);
  82. } while (1);
  83. remove_wait_queue(&mq->thread_wq, &wait);
  84. up(&mq->thread_sem);
  85. complete_and_exit(&mq->thread_complete, 0);
  86. return 0;
  87. }
  88. /*
  89. * Generic MMC request handler. This is called for any queue on a
  90. * particular host. When the host is not busy, we look for a request
  91. * on any queue on this host, and attempt to issue it. This may
  92. * not be the queue we were asked to process.
  93. */
  94. static void mmc_request(request_queue_t *q)
  95. {
  96. struct mmc_queue *mq = q->queuedata;
  97. if (!mq->req)
  98. wake_up(&mq->thread_wq);
  99. }
  100. /**
  101. * mmc_init_queue - initialise a queue structure.
  102. * @mq: mmc queue
  103. * @card: mmc card to attach this queue
  104. * @lock: queue lock
  105. *
  106. * Initialise a MMC card request queue.
  107. */
  108. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
  109. {
  110. struct mmc_host *host = card->host;
  111. u64 limit = BLK_BOUNCE_HIGH;
  112. int ret;
  113. if (host->dev->dma_mask && *host->dev->dma_mask)
  114. limit = *host->dev->dma_mask;
  115. mq->card = card;
  116. mq->queue = blk_init_queue(mmc_request, lock);
  117. if (!mq->queue)
  118. return -ENOMEM;
  119. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  120. blk_queue_bounce_limit(mq->queue, limit);
  121. blk_queue_max_sectors(mq->queue, host->max_sectors);
  122. blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
  123. blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
  124. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  125. mq->queue->queuedata = mq;
  126. mq->req = NULL;
  127. mq->sg = kmalloc(sizeof(struct scatterlist) * host->max_phys_segs,
  128. GFP_KERNEL);
  129. if (!mq->sg) {
  130. ret = -ENOMEM;
  131. goto cleanup;
  132. }
  133. init_completion(&mq->thread_complete);
  134. init_waitqueue_head(&mq->thread_wq);
  135. init_MUTEX(&mq->thread_sem);
  136. ret = kernel_thread(mmc_queue_thread, mq, CLONE_KERNEL);
  137. if (ret >= 0) {
  138. wait_for_completion(&mq->thread_complete);
  139. init_completion(&mq->thread_complete);
  140. ret = 0;
  141. goto out;
  142. }
  143. cleanup:
  144. kfree(mq->sg);
  145. mq->sg = NULL;
  146. blk_cleanup_queue(mq->queue);
  147. out:
  148. return ret;
  149. }
  150. EXPORT_SYMBOL(mmc_init_queue);
  151. void mmc_cleanup_queue(struct mmc_queue *mq)
  152. {
  153. mq->flags |= MMC_QUEUE_EXIT;
  154. wake_up(&mq->thread_wq);
  155. wait_for_completion(&mq->thread_complete);
  156. kfree(mq->sg);
  157. mq->sg = NULL;
  158. blk_cleanup_queue(mq->queue);
  159. mq->card = NULL;
  160. }
  161. EXPORT_SYMBOL(mmc_cleanup_queue);
  162. /**
  163. * mmc_queue_suspend - suspend a MMC request queue
  164. * @mq: MMC queue to suspend
  165. *
  166. * Stop the block request queue, and wait for our thread to
  167. * complete any outstanding requests. This ensures that we
  168. * won't suspend while a request is being processed.
  169. */
  170. void mmc_queue_suspend(struct mmc_queue *mq)
  171. {
  172. request_queue_t *q = mq->queue;
  173. unsigned long flags;
  174. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  175. mq->flags |= MMC_QUEUE_SUSPENDED;
  176. spin_lock_irqsave(q->queue_lock, flags);
  177. blk_stop_queue(q);
  178. spin_unlock_irqrestore(q->queue_lock, flags);
  179. down(&mq->thread_sem);
  180. }
  181. }
  182. EXPORT_SYMBOL(mmc_queue_suspend);
  183. /**
  184. * mmc_queue_resume - resume a previously suspended MMC request queue
  185. * @mq: MMC queue to resume
  186. */
  187. void mmc_queue_resume(struct mmc_queue *mq)
  188. {
  189. request_queue_t *q = mq->queue;
  190. unsigned long flags;
  191. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  192. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  193. up(&mq->thread_sem);
  194. spin_lock_irqsave(q->queue_lock, flags);
  195. blk_start_queue(q);
  196. spin_unlock_irqrestore(q->queue_lock, flags);
  197. }
  198. }
  199. EXPORT_SYMBOL(mmc_queue_resume);