queue.c 10 KB

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