queue.c 10 KB

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