queue.c 13 KB

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