queue.c 10 KB

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