queue.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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/module.h>
  13. #include <linux/blkdev.h>
  14. #include <linux/freezer.h>
  15. #include <linux/kthread.h>
  16. #include <linux/scatterlist.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/host.h>
  19. #include "queue.h"
  20. #define MMC_QUEUE_BOUNCESZ 65536
  21. #define MMC_QUEUE_SUSPENDED (1 << 0)
  22. /*
  23. * Prepare a MMC request. This just filters out odd stuff.
  24. */
  25. static int mmc_prep_request(struct request_queue *q, struct request *req)
  26. {
  27. /*
  28. * We only like normal block requests.
  29. */
  30. if (!blk_fs_request(req) && !blk_pc_request(req)) {
  31. blk_dump_rq_flags(req, "MMC bad request");
  32. return BLKPREP_KILL;
  33. }
  34. req->cmd_flags |= REQ_DONTPREP;
  35. return BLKPREP_OK;
  36. }
  37. static int mmc_queue_thread(void *d)
  38. {
  39. struct mmc_queue *mq = d;
  40. struct request_queue *q = mq->queue;
  41. current->flags |= PF_MEMALLOC;
  42. down(&mq->thread_sem);
  43. do {
  44. struct request *req = NULL;
  45. spin_lock_irq(q->queue_lock);
  46. set_current_state(TASK_INTERRUPTIBLE);
  47. if (!blk_queue_plugged(q))
  48. req = elv_next_request(q);
  49. mq->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. int ret;
  78. if (!mq) {
  79. printk(KERN_ERR "MMC: killing requests for dead queue\n");
  80. while ((req = elv_next_request(q)) != NULL) {
  81. do {
  82. ret = end_that_request_chunk(req, 0,
  83. req->current_nr_sectors << 9);
  84. } while (ret);
  85. }
  86. return;
  87. }
  88. if (!mq->req)
  89. wake_up_process(mq->thread);
  90. }
  91. /**
  92. * mmc_init_queue - initialise a queue structure.
  93. * @mq: mmc queue
  94. * @card: mmc card to attach this queue
  95. * @lock: queue lock
  96. *
  97. * Initialise a MMC card request queue.
  98. */
  99. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock)
  100. {
  101. struct mmc_host *host = card->host;
  102. u64 limit = BLK_BOUNCE_HIGH;
  103. int ret;
  104. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  105. limit = *mmc_dev(host)->dma_mask;
  106. mq->card = card;
  107. mq->queue = blk_init_queue(mmc_request, lock);
  108. if (!mq->queue)
  109. return -ENOMEM;
  110. mq->queue->queuedata = mq;
  111. mq->req = NULL;
  112. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  113. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  114. if (host->max_hw_segs == 1) {
  115. unsigned int bouncesz;
  116. bouncesz = MMC_QUEUE_BOUNCESZ;
  117. if (bouncesz > host->max_req_size)
  118. bouncesz = host->max_req_size;
  119. if (bouncesz > host->max_seg_size)
  120. bouncesz = host->max_seg_size;
  121. mq->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  122. if (!mq->bounce_buf) {
  123. printk(KERN_WARNING "%s: unable to allocate "
  124. "bounce buffer\n", mmc_card_name(card));
  125. } else {
  126. blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_HIGH);
  127. blk_queue_max_sectors(mq->queue, bouncesz / 512);
  128. blk_queue_max_phys_segments(mq->queue, bouncesz / 512);
  129. blk_queue_max_hw_segments(mq->queue, bouncesz / 512);
  130. blk_queue_max_segment_size(mq->queue, bouncesz);
  131. mq->sg = kmalloc(sizeof(struct scatterlist),
  132. GFP_KERNEL);
  133. if (!mq->sg) {
  134. ret = -ENOMEM;
  135. goto cleanup_queue;
  136. }
  137. sg_init_table(mq->sg, 1);
  138. mq->bounce_sg = kmalloc(sizeof(struct scatterlist) *
  139. bouncesz / 512, GFP_KERNEL);
  140. if (!mq->bounce_sg) {
  141. ret = -ENOMEM;
  142. goto cleanup_queue;
  143. }
  144. sg_init_table(mq->bounce_sg, bouncesz / 512);
  145. }
  146. }
  147. #endif
  148. if (!mq->bounce_buf) {
  149. blk_queue_bounce_limit(mq->queue, limit);
  150. blk_queue_max_sectors(mq->queue, host->max_req_size / 512);
  151. blk_queue_max_phys_segments(mq->queue, host->max_phys_segs);
  152. blk_queue_max_hw_segments(mq->queue, host->max_hw_segs);
  153. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  154. mq->sg = kzalloc(sizeof(struct scatterlist) *
  155. host->max_phys_segs, GFP_KERNEL);
  156. if (!mq->sg) {
  157. ret = -ENOMEM;
  158. goto cleanup_queue;
  159. }
  160. }
  161. init_MUTEX(&mq->thread_sem);
  162. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd");
  163. if (IS_ERR(mq->thread)) {
  164. ret = PTR_ERR(mq->thread);
  165. goto free_bounce_sg;
  166. }
  167. return 0;
  168. free_bounce_sg:
  169. if (mq->bounce_sg)
  170. kfree(mq->bounce_sg);
  171. mq->bounce_sg = NULL;
  172. cleanup_queue:
  173. if (mq->sg)
  174. kfree(mq->sg);
  175. mq->sg = NULL;
  176. if (mq->bounce_buf)
  177. kfree(mq->bounce_buf);
  178. mq->bounce_buf = NULL;
  179. blk_cleanup_queue(mq->queue);
  180. return ret;
  181. }
  182. void mmc_cleanup_queue(struct mmc_queue *mq)
  183. {
  184. struct request_queue *q = mq->queue;
  185. unsigned long flags;
  186. /* Mark that we should start throwing out stragglers */
  187. spin_lock_irqsave(q->queue_lock, flags);
  188. q->queuedata = NULL;
  189. spin_unlock_irqrestore(q->queue_lock, flags);
  190. /* Make sure the queue isn't suspended, as that will deadlock */
  191. mmc_queue_resume(mq);
  192. /* Then terminate our worker thread */
  193. kthread_stop(mq->thread);
  194. if (mq->bounce_sg)
  195. kfree(mq->bounce_sg);
  196. mq->bounce_sg = NULL;
  197. kfree(mq->sg);
  198. mq->sg = NULL;
  199. if (mq->bounce_buf)
  200. kfree(mq->bounce_buf);
  201. mq->bounce_buf = NULL;
  202. blk_cleanup_queue(mq->queue);
  203. mq->card = NULL;
  204. }
  205. EXPORT_SYMBOL(mmc_cleanup_queue);
  206. /**
  207. * mmc_queue_suspend - suspend a MMC request queue
  208. * @mq: MMC queue to suspend
  209. *
  210. * Stop the block request queue, and wait for our thread to
  211. * complete any outstanding requests. This ensures that we
  212. * won't suspend while a request is being processed.
  213. */
  214. void mmc_queue_suspend(struct mmc_queue *mq)
  215. {
  216. struct request_queue *q = mq->queue;
  217. unsigned long flags;
  218. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  219. mq->flags |= MMC_QUEUE_SUSPENDED;
  220. spin_lock_irqsave(q->queue_lock, flags);
  221. blk_stop_queue(q);
  222. spin_unlock_irqrestore(q->queue_lock, flags);
  223. down(&mq->thread_sem);
  224. }
  225. }
  226. /**
  227. * mmc_queue_resume - resume a previously suspended MMC request queue
  228. * @mq: MMC queue to resume
  229. */
  230. void mmc_queue_resume(struct mmc_queue *mq)
  231. {
  232. struct request_queue *q = mq->queue;
  233. unsigned long flags;
  234. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  235. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  236. up(&mq->thread_sem);
  237. spin_lock_irqsave(q->queue_lock, flags);
  238. blk_start_queue(q);
  239. spin_unlock_irqrestore(q->queue_lock, flags);
  240. }
  241. }
  242. static void copy_sg(struct scatterlist *dst, unsigned int dst_len,
  243. struct scatterlist *src, unsigned int src_len)
  244. {
  245. unsigned int chunk;
  246. char *dst_buf, *src_buf;
  247. unsigned int dst_size, src_size;
  248. dst_buf = NULL;
  249. src_buf = NULL;
  250. dst_size = 0;
  251. src_size = 0;
  252. while (src_len) {
  253. BUG_ON(dst_len == 0);
  254. if (dst_size == 0) {
  255. dst_buf = sg_virt(dst);
  256. dst_size = dst->length;
  257. }
  258. if (src_size == 0) {
  259. src_buf = sg_virt(dst);
  260. src_size = src->length;
  261. }
  262. chunk = min(dst_size, src_size);
  263. memcpy(dst_buf, src_buf, chunk);
  264. dst_buf += chunk;
  265. src_buf += chunk;
  266. dst_size -= chunk;
  267. src_size -= chunk;
  268. if (dst_size == 0) {
  269. dst++;
  270. dst_len--;
  271. }
  272. if (src_size == 0) {
  273. src++;
  274. src_len--;
  275. }
  276. }
  277. }
  278. unsigned int mmc_queue_map_sg(struct mmc_queue *mq)
  279. {
  280. unsigned int sg_len;
  281. if (!mq->bounce_buf)
  282. return blk_rq_map_sg(mq->queue, mq->req, mq->sg);
  283. BUG_ON(!mq->bounce_sg);
  284. sg_len = blk_rq_map_sg(mq->queue, mq->req, mq->bounce_sg);
  285. mq->bounce_sg_len = sg_len;
  286. /*
  287. * Shortcut in the event we only get a single entry.
  288. */
  289. if (sg_len == 1) {
  290. memcpy(mq->sg, mq->bounce_sg, sizeof(struct scatterlist));
  291. return 1;
  292. }
  293. sg_init_one(mq->sg, mq->bounce_buf, 0);
  294. while (sg_len) {
  295. mq->sg[0].length += mq->bounce_sg[sg_len - 1].length;
  296. sg_len--;
  297. }
  298. return 1;
  299. }
  300. void mmc_queue_bounce_pre(struct mmc_queue *mq)
  301. {
  302. if (!mq->bounce_buf)
  303. return;
  304. if (mq->bounce_sg_len == 1)
  305. return;
  306. if (rq_data_dir(mq->req) != WRITE)
  307. return;
  308. copy_sg(mq->sg, 1, mq->bounce_sg, mq->bounce_sg_len);
  309. }
  310. void mmc_queue_bounce_post(struct mmc_queue *mq)
  311. {
  312. if (!mq->bounce_buf)
  313. return;
  314. if (mq->bounce_sg_len == 1)
  315. return;
  316. if (rq_data_dir(mq->req) != READ)
  317. return;
  318. copy_sg(mq->bounce_sg, mq->bounce_sg_len, mq->sg, 1);
  319. }