queue.c 13 KB

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