queue.c 8.7 KB

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