queue.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 and discards.
  30. */
  31. if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
  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. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  111. if (mmc_can_erase(card)) {
  112. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mq->queue);
  113. mq->queue->limits.max_discard_sectors = UINT_MAX;
  114. if (card->erased_byte == 0)
  115. mq->queue->limits.discard_zeroes_data = 1;
  116. if (!mmc_can_trim(card) && is_power_of_2(card->erase_size)) {
  117. mq->queue->limits.discard_granularity =
  118. card->erase_size << 9;
  119. mq->queue->limits.discard_alignment =
  120. card->erase_size << 9;
  121. }
  122. if (mmc_can_secure_erase_trim(card))
  123. queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD,
  124. mq->queue);
  125. }
  126. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  127. if (host->max_segs == 1) {
  128. unsigned int bouncesz;
  129. bouncesz = MMC_QUEUE_BOUNCESZ;
  130. if (bouncesz > host->max_req_size)
  131. bouncesz = host->max_req_size;
  132. if (bouncesz > host->max_seg_size)
  133. bouncesz = host->max_seg_size;
  134. if (bouncesz > (host->max_blk_count * 512))
  135. bouncesz = host->max_blk_count * 512;
  136. if (bouncesz > 512) {
  137. mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  138. if (!mq->bounce_buf) {
  139. printk(KERN_WARNING "%s: unable to "
  140. "allocate bounce buffer\n",
  141. mmc_card_name(card));
  142. }
  143. }
  144. if (mq->bounce_buf) {
  145. blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
  146. blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
  147. blk_queue_max_segments(mq->queue, bouncesz / 512);
  148. blk_queue_max_segment_size(mq->queue, bouncesz);
  149. mq->sg = kmalloc(sizeof(struct scatterlist),
  150. GFP_KERNEL);
  151. if (!mq->sg) {
  152. ret = -ENOMEM;
  153. goto cleanup_queue;
  154. }
  155. sg_init_table(mq->sg, 1);
  156. mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
  157. bouncesz / 512, GFP_KERNEL);
  158. if (!mq->bounce_sg) {
  159. ret = -ENOMEM;
  160. goto cleanup_queue;
  161. }
  162. sg_init_table(mq->bounce_sg, bouncesz / 512);
  163. }
  164. }
  165. #endif
  166. if (!mq->bounce_buf) {
  167. blk_queue_bounce_limit(mq->queue, limit);
  168. blk_queue_max_hw_sectors(mq->queue,
  169. min(host->max_blk_count, host->max_req_size / 512));
  170. blk_queue_max_segments(mq->queue, host->max_segs);
  171. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  172. mq->sg = kmalloc(sizeof(struct scatterlist) *
  173. host->max_segs, GFP_KERNEL);
  174. if (!mq->sg) {
  175. ret = -ENOMEM;
  176. goto cleanup_queue;
  177. }
  178. sg_init_table(mq->sg, host->max_segs);
  179. }
  180. sema_init(&mq->thread_sem, 1);
  181. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d",
  182. host->index);
  183. if (IS_ERR(mq->thread)) {
  184. ret = PTR_ERR(mq->thread);
  185. goto free_bounce_sg;
  186. }
  187. return 0;
  188. free_bounce_sg:
  189. if (mq->bounce_sg)
  190. kfree(mq->bounce_sg);
  191. mq->bounce_sg = NULL;
  192. cleanup_queue:
  193. if (mq->sg)
  194. kfree(mq->sg);
  195. mq->sg = NULL;
  196. if (mq->bounce_buf)
  197. kfree(mq->bounce_buf);
  198. mq->bounce_buf = NULL;
  199. blk_cleanup_queue(mq->queue);
  200. return ret;
  201. }
  202. void mmc_cleanup_queue(struct mmc_queue *mq)
  203. {
  204. struct request_queue *q = mq->queue;
  205. unsigned long flags;
  206. /* Make sure the queue isn't suspended, as that will deadlock */
  207. mmc_queue_resume(mq);
  208. /* Then terminate our worker thread */
  209. kthread_stop(mq->thread);
  210. /* Empty the queue */
  211. spin_lock_irqsave(q->queue_lock, flags);
  212. q->queuedata = NULL;
  213. blk_start_queue(q);
  214. spin_unlock_irqrestore(q->queue_lock, flags);
  215. if (mq->bounce_sg)
  216. kfree(mq->bounce_sg);
  217. mq->bounce_sg = NULL;
  218. kfree(mq->sg);
  219. mq->sg = NULL;
  220. if (mq->bounce_buf)
  221. kfree(mq->bounce_buf);
  222. mq->bounce_buf = NULL;
  223. mq->card = NULL;
  224. }
  225. EXPORT_SYMBOL(mmc_cleanup_queue);
  226. /**
  227. * mmc_queue_suspend - suspend a MMC request queue
  228. * @mq: MMC queue to suspend
  229. *
  230. * Stop the block request queue, and wait for our thread to
  231. * complete any outstanding requests. This ensures that we
  232. * won't suspend while a request is being processed.
  233. */
  234. void mmc_queue_suspend(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. spin_lock_irqsave(q->queue_lock, flags);
  241. blk_stop_queue(q);
  242. spin_unlock_irqrestore(q->queue_lock, flags);
  243. down(&mq->thread_sem);
  244. }
  245. }
  246. /**
  247. * mmc_queue_resume - resume a previously suspended MMC request queue
  248. * @mq: MMC queue to resume
  249. */
  250. void mmc_queue_resume(struct mmc_queue *mq)
  251. {
  252. struct request_queue *q = mq->queue;
  253. unsigned long flags;
  254. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  255. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  256. up(&mq->thread_sem);
  257. spin_lock_irqsave(q->queue_lock, flags);
  258. blk_start_queue(q);
  259. spin_unlock_irqrestore(q->queue_lock, flags);
  260. }
  261. }
  262. /*
  263. * Prepare the sg list(s) to be handed of to the host driver
  264. */
  265. unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
  266. {
  267. unsigned int sg_len;
  268. size_t buflen;
  269. struct scatterlist *sg;
  270. int i;
  271. if (!mq->bounce_buf)
  272. return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
  273. BUG_ON(!mq->bounce_sg);
  274. sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
  275. mq->bounce_sg_len = sg_len;
  276. buflen = 0;
  277. for_each_sg(mq->bounce_sg, sg, sg_len, i)
  278. buflen += sg->length;
  279. sg_init_one(mq->sg, mq->bounce_buf, buflen);
  280. return 1;
  281. }
  282. /*
  283. * If writing, bounce the data to the buffer before the request
  284. * is sent to the host driver
  285. */
  286. void mmc_queue_bounce_pre(struct mmc_queue *mq)
  287. {
  288. unsigned long flags;
  289. if (!mq->bounce_buf)
  290. return;
  291. if (rq_data_dir(mq->req) != WRITE)
  292. return;
  293. local_irq_save(flags);
  294. sg_copy_to_buffer(mq->bounce_sg, mq->bounce_sg_len,
  295. mq->bounce_buf, mq->sg[0].length);
  296. local_irq_restore(flags);
  297. }
  298. /*
  299. * If reading, bounce the data from the buffer after the request
  300. * has been handled by the host driver
  301. */
  302. void mmc_queue_bounce_post(struct mmc_queue *mq)
  303. {
  304. unsigned long flags;
  305. if (!mq->bounce_buf)
  306. return;
  307. if (rq_data_dir(mq->req) != READ)
  308. return;
  309. local_irq_save(flags);
  310. sg_copy_from_buffer(mq->bounce_sg, mq->bounce_sg_len,
  311. mq->bounce_buf, mq->sg[0].length);
  312. local_irq_restore(flags);
  313. }