queue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (C) 2003 Russell King, All Rights Reserved.
  3. * Copyright 2006-2007 Pierre Ossman
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/freezer.h>
  14. #include <linux/kthread.h>
  15. #include <linux/scatterlist.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/mmc/card.h>
  18. #include <linux/mmc/host.h>
  19. #include "queue.h"
  20. #include "block.h"
  21. #include "core.h"
  22. #include "card.h"
  23. #define MMC_QUEUE_BOUNCESZ 65536
  24. /*
  25. * Prepare a MMC request. This just filters out odd stuff.
  26. */
  27. static int mmc_prep_request(struct request_queue *q, struct request *req)
  28. {
  29. struct mmc_queue *mq = q->queuedata;
  30. if (mq && (mmc_card_removed(mq->card) || mmc_access_rpmb(mq)))
  31. return BLKPREP_KILL;
  32. req->rq_flags |= RQF_DONTPREP;
  33. return BLKPREP_OK;
  34. }
  35. static int mmc_queue_thread(void *d)
  36. {
  37. struct mmc_queue *mq = d;
  38. struct request_queue *q = mq->queue;
  39. struct mmc_context_info *cntx = &mq->card->host->context_info;
  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. req = blk_fetch_request(q);
  47. mq->asleep = false;
  48. cntx->is_waiting_last_req = false;
  49. cntx->is_new_req = false;
  50. if (!req) {
  51. /*
  52. * Dispatch queue is empty so set flags for
  53. * mmc_request_fn() to wake us up.
  54. */
  55. if (mq->mqrq_prev->req)
  56. cntx->is_waiting_last_req = true;
  57. else
  58. mq->asleep = true;
  59. }
  60. mq->mqrq_cur->req = req;
  61. spin_unlock_irq(q->queue_lock);
  62. if (req || mq->mqrq_prev->req) {
  63. bool req_is_special = mmc_req_is_special(req);
  64. set_current_state(TASK_RUNNING);
  65. mmc_blk_issue_rq(mq, req);
  66. cond_resched();
  67. if (mq->new_request) {
  68. mq->new_request = false;
  69. continue; /* fetch again */
  70. }
  71. /*
  72. * Current request becomes previous request
  73. * and vice versa.
  74. * In case of special requests, current request
  75. * has been finished. Do not assign it to previous
  76. * request.
  77. */
  78. if (req_is_special)
  79. mq->mqrq_cur->req = NULL;
  80. mq->mqrq_prev->brq.mrq.data = NULL;
  81. mq->mqrq_prev->req = NULL;
  82. swap(mq->mqrq_prev, mq->mqrq_cur);
  83. } else {
  84. if (kthread_should_stop()) {
  85. set_current_state(TASK_RUNNING);
  86. break;
  87. }
  88. up(&mq->thread_sem);
  89. schedule();
  90. down(&mq->thread_sem);
  91. }
  92. } while (1);
  93. up(&mq->thread_sem);
  94. return 0;
  95. }
  96. /*
  97. * Generic MMC request handler. This is called for any queue on a
  98. * particular host. When the host is not busy, we look for a request
  99. * on any queue on this host, and attempt to issue it. This may
  100. * not be the queue we were asked to process.
  101. */
  102. static void mmc_request_fn(struct request_queue *q)
  103. {
  104. struct mmc_queue *mq = q->queuedata;
  105. struct request *req;
  106. struct mmc_context_info *cntx;
  107. if (!mq) {
  108. while ((req = blk_fetch_request(q)) != NULL) {
  109. req->rq_flags |= RQF_QUIET;
  110. __blk_end_request_all(req, -EIO);
  111. }
  112. return;
  113. }
  114. cntx = &mq->card->host->context_info;
  115. if (cntx->is_waiting_last_req) {
  116. cntx->is_new_req = true;
  117. wake_up_interruptible(&cntx->wait);
  118. }
  119. if (mq->asleep)
  120. wake_up_process(mq->thread);
  121. }
  122. static struct scatterlist *mmc_alloc_sg(int sg_len, int *err)
  123. {
  124. struct scatterlist *sg;
  125. sg = kmalloc_array(sg_len, sizeof(*sg), GFP_KERNEL);
  126. if (!sg)
  127. *err = -ENOMEM;
  128. else {
  129. *err = 0;
  130. sg_init_table(sg, sg_len);
  131. }
  132. return sg;
  133. }
  134. static void mmc_queue_setup_discard(struct request_queue *q,
  135. struct mmc_card *card)
  136. {
  137. unsigned max_discard;
  138. max_discard = mmc_calc_max_discard(card);
  139. if (!max_discard)
  140. return;
  141. queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, q);
  142. blk_queue_max_discard_sectors(q, max_discard);
  143. if (card->erased_byte == 0 && !mmc_can_discard(card))
  144. q->limits.discard_zeroes_data = 1;
  145. q->limits.discard_granularity = card->pref_erase << 9;
  146. /* granularity must not be greater than max. discard */
  147. if (card->pref_erase > max_discard)
  148. q->limits.discard_granularity = 0;
  149. if (mmc_can_secure_erase_trim(card))
  150. queue_flag_set_unlocked(QUEUE_FLAG_SECERASE, q);
  151. }
  152. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  153. static bool mmc_queue_alloc_bounce_bufs(struct mmc_queue *mq,
  154. unsigned int bouncesz)
  155. {
  156. int i;
  157. for (i = 0; i < mq->qdepth; i++) {
  158. mq->mqrq[i].bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  159. if (!mq->mqrq[i].bounce_buf)
  160. goto out_err;
  161. }
  162. return true;
  163. out_err:
  164. while (--i >= 0) {
  165. kfree(mq->mqrq[i].bounce_buf);
  166. mq->mqrq[i].bounce_buf = NULL;
  167. }
  168. pr_warn("%s: unable to allocate bounce buffers\n",
  169. mmc_card_name(mq->card));
  170. return false;
  171. }
  172. static int mmc_queue_alloc_bounce_sgs(struct mmc_queue *mq,
  173. unsigned int bouncesz)
  174. {
  175. int i, ret;
  176. for (i = 0; i < mq->qdepth; i++) {
  177. mq->mqrq[i].sg = mmc_alloc_sg(1, &ret);
  178. if (ret)
  179. return ret;
  180. mq->mqrq[i].bounce_sg = mmc_alloc_sg(bouncesz / 512, &ret);
  181. if (ret)
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. #endif
  187. static int mmc_queue_alloc_sgs(struct mmc_queue *mq, int max_segs)
  188. {
  189. int i, ret;
  190. for (i = 0; i < mq->qdepth; i++) {
  191. mq->mqrq[i].sg = mmc_alloc_sg(max_segs, &ret);
  192. if (ret)
  193. return ret;
  194. }
  195. return 0;
  196. }
  197. static void mmc_queue_req_free_bufs(struct mmc_queue_req *mqrq)
  198. {
  199. kfree(mqrq->bounce_sg);
  200. mqrq->bounce_sg = NULL;
  201. kfree(mqrq->sg);
  202. mqrq->sg = NULL;
  203. kfree(mqrq->bounce_buf);
  204. mqrq->bounce_buf = NULL;
  205. }
  206. static void mmc_queue_reqs_free_bufs(struct mmc_queue *mq)
  207. {
  208. int i;
  209. for (i = 0; i < mq->qdepth; i++)
  210. mmc_queue_req_free_bufs(&mq->mqrq[i]);
  211. }
  212. /**
  213. * mmc_init_queue - initialise a queue structure.
  214. * @mq: mmc queue
  215. * @card: mmc card to attach this queue
  216. * @lock: queue lock
  217. * @subname: partition subname
  218. *
  219. * Initialise a MMC card request queue.
  220. */
  221. int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card,
  222. spinlock_t *lock, const char *subname)
  223. {
  224. struct mmc_host *host = card->host;
  225. u64 limit = BLK_BOUNCE_HIGH;
  226. bool bounce = false;
  227. int ret = -ENOMEM;
  228. if (mmc_dev(host)->dma_mask && *mmc_dev(host)->dma_mask)
  229. limit = (u64)dma_max_pfn(mmc_dev(host)) << PAGE_SHIFT;
  230. mq->card = card;
  231. mq->queue = blk_init_queue(mmc_request_fn, lock);
  232. if (!mq->queue)
  233. return -ENOMEM;
  234. mq->qdepth = 2;
  235. mq->mqrq = kcalloc(mq->qdepth, sizeof(struct mmc_queue_req),
  236. GFP_KERNEL);
  237. if (!mq->mqrq)
  238. goto blk_cleanup;
  239. mq->mqrq_cur = &mq->mqrq[0];
  240. mq->mqrq_prev = &mq->mqrq[1];
  241. mq->queue->queuedata = mq;
  242. blk_queue_prep_rq(mq->queue, mmc_prep_request);
  243. queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
  244. queue_flag_clear_unlocked(QUEUE_FLAG_ADD_RANDOM, mq->queue);
  245. if (mmc_can_erase(card))
  246. mmc_queue_setup_discard(mq->queue, card);
  247. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  248. if (host->max_segs == 1) {
  249. unsigned int bouncesz;
  250. bouncesz = MMC_QUEUE_BOUNCESZ;
  251. if (bouncesz > host->max_req_size)
  252. bouncesz = host->max_req_size;
  253. if (bouncesz > host->max_seg_size)
  254. bouncesz = host->max_seg_size;
  255. if (bouncesz > (host->max_blk_count * 512))
  256. bouncesz = host->max_blk_count * 512;
  257. if (bouncesz > 512 &&
  258. mmc_queue_alloc_bounce_bufs(mq, bouncesz)) {
  259. blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
  260. blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
  261. blk_queue_max_segments(mq->queue, bouncesz / 512);
  262. blk_queue_max_segment_size(mq->queue, bouncesz);
  263. ret = mmc_queue_alloc_bounce_sgs(mq, bouncesz);
  264. if (ret)
  265. goto cleanup_queue;
  266. bounce = true;
  267. }
  268. }
  269. #endif
  270. if (!bounce) {
  271. blk_queue_bounce_limit(mq->queue, limit);
  272. blk_queue_max_hw_sectors(mq->queue,
  273. min(host->max_blk_count, host->max_req_size / 512));
  274. blk_queue_max_segments(mq->queue, host->max_segs);
  275. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  276. ret = mmc_queue_alloc_sgs(mq, host->max_segs);
  277. if (ret)
  278. goto cleanup_queue;
  279. }
  280. sema_init(&mq->thread_sem, 1);
  281. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
  282. host->index, subname ? subname : "");
  283. if (IS_ERR(mq->thread)) {
  284. ret = PTR_ERR(mq->thread);
  285. goto cleanup_queue;
  286. }
  287. return 0;
  288. cleanup_queue:
  289. mmc_queue_reqs_free_bufs(mq);
  290. kfree(mq->mqrq);
  291. mq->mqrq = NULL;
  292. blk_cleanup:
  293. blk_cleanup_queue(mq->queue);
  294. return ret;
  295. }
  296. void mmc_cleanup_queue(struct mmc_queue *mq)
  297. {
  298. struct request_queue *q = mq->queue;
  299. unsigned long flags;
  300. /* Make sure the queue isn't suspended, as that will deadlock */
  301. mmc_queue_resume(mq);
  302. /* Then terminate our worker thread */
  303. kthread_stop(mq->thread);
  304. /* Empty the queue */
  305. spin_lock_irqsave(q->queue_lock, flags);
  306. q->queuedata = NULL;
  307. blk_start_queue(q);
  308. spin_unlock_irqrestore(q->queue_lock, flags);
  309. mmc_queue_reqs_free_bufs(mq);
  310. kfree(mq->mqrq);
  311. mq->mqrq = NULL;
  312. mq->card = NULL;
  313. }
  314. EXPORT_SYMBOL(mmc_cleanup_queue);
  315. /**
  316. * mmc_queue_suspend - suspend a MMC request queue
  317. * @mq: MMC queue to suspend
  318. *
  319. * Stop the block request queue, and wait for our thread to
  320. * complete any outstanding requests. This ensures that we
  321. * won't suspend while a request is being processed.
  322. */
  323. void mmc_queue_suspend(struct mmc_queue *mq)
  324. {
  325. struct request_queue *q = mq->queue;
  326. unsigned long flags;
  327. if (!mq->suspended) {
  328. mq->suspended |= true;
  329. spin_lock_irqsave(q->queue_lock, flags);
  330. blk_stop_queue(q);
  331. spin_unlock_irqrestore(q->queue_lock, flags);
  332. down(&mq->thread_sem);
  333. }
  334. }
  335. /**
  336. * mmc_queue_resume - resume a previously suspended MMC request queue
  337. * @mq: MMC queue to resume
  338. */
  339. void mmc_queue_resume(struct mmc_queue *mq)
  340. {
  341. struct request_queue *q = mq->queue;
  342. unsigned long flags;
  343. if (mq->suspended) {
  344. mq->suspended = false;
  345. up(&mq->thread_sem);
  346. spin_lock_irqsave(q->queue_lock, flags);
  347. blk_start_queue(q);
  348. spin_unlock_irqrestore(q->queue_lock, flags);
  349. }
  350. }
  351. /*
  352. * Prepare the sg list(s) to be handed of to the host driver
  353. */
  354. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  355. {
  356. unsigned int sg_len;
  357. size_t buflen;
  358. struct scatterlist *sg;
  359. int i;
  360. if (!mqrq->bounce_buf)
  361. return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
  362. sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
  363. mqrq->bounce_sg_len = sg_len;
  364. buflen = 0;
  365. for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
  366. buflen += sg->length;
  367. sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
  368. return 1;
  369. }
  370. /*
  371. * If writing, bounce the data to the buffer before the request
  372. * is sent to the host driver
  373. */
  374. void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
  375. {
  376. if (!mqrq->bounce_buf)
  377. return;
  378. if (rq_data_dir(mqrq->req) != WRITE)
  379. return;
  380. sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
  381. mqrq->bounce_buf, mqrq->sg[0].length);
  382. }
  383. /*
  384. * If reading, bounce the data from the buffer after the request
  385. * has been handled by the host driver
  386. */
  387. void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
  388. {
  389. if (!mqrq->bounce_buf)
  390. return;
  391. if (rq_data_dir(mqrq->req) != READ)
  392. return;
  393. sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
  394. mqrq->bounce_buf, mqrq->sg[0].length);
  395. }