queue.c 8.3 KB

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