queue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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(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)
  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, lock);
  148. if (!mq->queue)
  149. return -ENOMEM;
  150. memset(&mq->mqrq_cur, 0, sizeof(mq->mqrq_cur));
  151. memset(&mq->mqrq_prev, 0, sizeof(mq->mqrq_prev));
  152. mq->mqrq_cur = mqrq_cur;
  153. mq->mqrq_prev = mqrq_prev;
  154. mq->queue->queuedata = mq;
  155. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  156. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  157. if (mmc_can_erase(card))
  158. mmc_queue_setup_discard(mq->queue, card);
  159. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  160. if (host->max_segs == 1) {
  161. unsigned int bouncesz;
  162. bouncesz = MMC_QUEUE_BOUNCESZ;
  163. if (bouncesz > host->max_req_size)
  164. bouncesz = host->max_req_size;
  165. if (bouncesz > host->max_seg_size)
  166. bouncesz = host->max_seg_size;
  167. if (bouncesz > (host->max_blk_count * 512))
  168. bouncesz = host->max_blk_count * 512;
  169. if (bouncesz > 512) {
  170. mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  171. if (!mqrq_cur->bounce_buf) {
  172. pr_warning("%s: unable to "
  173. "allocate bounce cur buffer\n",
  174. mmc_card_name(card));
  175. }
  176. mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  177. if (!mqrq_prev->bounce_buf) {
  178. pr_warning("%s: unable to "
  179. "allocate bounce prev buffer\n",
  180. mmc_card_name(card));
  181. kfree(mqrq_cur->bounce_buf);
  182. mqrq_cur->bounce_buf = NULL;
  183. }
  184. }
  185. if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
  186. blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
  187. blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
  188. blk_queue_max_segments(mq->queue, bouncesz / 512);
  189. blk_queue_max_segment_size(mq->queue, bouncesz);
  190. mqrq_cur->sg = mmc_alloc_sg(1, &ret);
  191. if (ret)
  192. goto cleanup_queue;
  193. mqrq_cur->bounce_sg =
  194. mmc_alloc_sg(bouncesz / 512, &ret);
  195. if (ret)
  196. goto cleanup_queue;
  197. mqrq_prev->sg = mmc_alloc_sg(1, &ret);
  198. if (ret)
  199. goto cleanup_queue;
  200. mqrq_prev->bounce_sg =
  201. mmc_alloc_sg(bouncesz / 512, &ret);
  202. if (ret)
  203. goto cleanup_queue;
  204. }
  205. }
  206. #endif
  207. if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
  208. blk_queue_bounce_limit(mq->queue, limit);
  209. blk_queue_max_hw_sectors(mq->queue,
  210. min(host->max_blk_count, host->max_req_size / 512));
  211. blk_queue_max_segments(mq->queue, host->max_segs);
  212. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  213. mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
  214. if (ret)
  215. goto cleanup_queue;
  216. mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
  217. if (ret)
  218. goto cleanup_queue;
  219. }
  220. sema_init(&mq->thread_sem, 1);
  221. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
  222. host->index, subname ? subname : "");
  223. if (IS_ERR(mq->thread)) {
  224. ret = PTR_ERR(mq->thread);
  225. goto free_bounce_sg;
  226. }
  227. return 0;
  228. free_bounce_sg:
  229. kfree(mqrq_cur->bounce_sg);
  230. mqrq_cur->bounce_sg = NULL;
  231. kfree(mqrq_prev->bounce_sg);
  232. mqrq_prev->bounce_sg = NULL;
  233. cleanup_queue:
  234. kfree(mqrq_cur->sg);
  235. mqrq_cur->sg = NULL;
  236. kfree(mqrq_cur->bounce_buf);
  237. mqrq_cur->bounce_buf = NULL;
  238. kfree(mqrq_prev->sg);
  239. mqrq_prev->sg = NULL;
  240. kfree(mqrq_prev->bounce_buf);
  241. mqrq_prev->bounce_buf = NULL;
  242. blk_cleanup_queue(mq->queue);
  243. return ret;
  244. }
  245. void mmc_cleanup_queue(struct mmc_queue *mq)
  246. {
  247. struct request_queue *q = mq->queue;
  248. unsigned long flags;
  249. struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
  250. struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
  251. /* Make sure the queue isn't suspended, as that will deadlock */
  252. mmc_queue_resume(mq);
  253. /* Then terminate our worker thread */
  254. kthread_stop(mq->thread);
  255. /* Empty the queue */
  256. spin_lock_irqsave(q->queue_lock, flags);
  257. q->queuedata = NULL;
  258. blk_start_queue(q);
  259. spin_unlock_irqrestore(q->queue_lock, flags);
  260. kfree(mqrq_cur->bounce_sg);
  261. mqrq_cur->bounce_sg = NULL;
  262. kfree(mqrq_cur->sg);
  263. mqrq_cur->sg = NULL;
  264. kfree(mqrq_cur->bounce_buf);
  265. mqrq_cur->bounce_buf = NULL;
  266. kfree(mqrq_prev->bounce_sg);
  267. mqrq_prev->bounce_sg = NULL;
  268. kfree(mqrq_prev->sg);
  269. mqrq_prev->sg = NULL;
  270. kfree(mqrq_prev->bounce_buf);
  271. mqrq_prev->bounce_buf = NULL;
  272. mq->card = NULL;
  273. }
  274. EXPORT_SYMBOL(mmc_cleanup_queue);
  275. /**
  276. * mmc_queue_suspend - suspend a MMC request queue
  277. * @mq: MMC queue to suspend
  278. *
  279. * Stop the block request queue, and wait for our thread to
  280. * complete any outstanding requests. This ensures that we
  281. * won't suspend while a request is being processed.
  282. */
  283. void mmc_queue_suspend(struct mmc_queue *mq)
  284. {
  285. struct request_queue *q = mq->queue;
  286. unsigned long flags;
  287. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  288. mq->flags |= MMC_QUEUE_SUSPENDED;
  289. spin_lock_irqsave(q->queue_lock, flags);
  290. blk_stop_queue(q);
  291. spin_unlock_irqrestore(q->queue_lock, flags);
  292. down(&mq->thread_sem);
  293. }
  294. }
  295. /**
  296. * mmc_queue_resume - resume a previously suspended MMC request queue
  297. * @mq: MMC queue to resume
  298. */
  299. void mmc_queue_resume(struct mmc_queue *mq)
  300. {
  301. struct request_queue *q = mq->queue;
  302. unsigned long flags;
  303. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  304. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  305. up(&mq->thread_sem);
  306. spin_lock_irqsave(q->queue_lock, flags);
  307. blk_start_queue(q);
  308. spin_unlock_irqrestore(q->queue_lock, flags);
  309. }
  310. }
  311. /*
  312. * Prepare the sg list(s) to be handed of to the host driver
  313. */
  314. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  315. {
  316. unsigned int sg_len;
  317. size_t buflen;
  318. struct scatterlist *sg;
  319. int i;
  320. if (!mqrq->bounce_buf)
  321. return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
  322. BUG_ON(!mqrq->bounce_sg);
  323. sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
  324. mqrq->bounce_sg_len = sg_len;
  325. buflen = 0;
  326. for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
  327. buflen += sg->length;
  328. sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
  329. return 1;
  330. }
  331. /*
  332. * If writing, bounce the data to the buffer before the request
  333. * is sent to the host driver
  334. */
  335. void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
  336. {
  337. if (!mqrq->bounce_buf)
  338. return;
  339. if (rq_data_dir(mqrq->req) != WRITE)
  340. return;
  341. sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
  342. mqrq->bounce_buf, mqrq->sg[0].length);
  343. }
  344. /*
  345. * If reading, bounce the data from the buffer after the request
  346. * has been handled by the host driver
  347. */
  348. void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
  349. {
  350. if (!mqrq->bounce_buf)
  351. return;
  352. if (rq_data_dir(mqrq->req) != READ)
  353. return;
  354. sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
  355. mqrq->bounce_buf, mqrq->sg[0].length);
  356. }