queue.c 13 KB

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