queue.c 11 KB

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