queue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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/mmc/card.h>
  19. #include <linux/mmc/host.h>
  20. #include "queue.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->cmd_flags & REQ_DISCARD)) {
  32. blk_dump_rq_flags(req, "MMC bad request");
  33. return BLKPREP_KILL;
  34. }
  35. if (mq && mmc_card_removed(mq->card))
  36. return BLKPREP_KILL;
  37. req->cmd_flags |= REQ_DONTPREP;
  38. return BLKPREP_OK;
  39. }
  40. static int mmc_queue_thread(void *d)
  41. {
  42. struct mmc_queue *mq = d;
  43. struct request_queue *q = mq->queue;
  44. current->flags |= PF_MEMALLOC;
  45. down(&mq->thread_sem);
  46. do {
  47. struct request *req = NULL;
  48. struct mmc_queue_req *tmp;
  49. unsigned int cmd_flags = 0;
  50. spin_lock_irq(q->queue_lock);
  51. set_current_state(TASK_INTERRUPTIBLE);
  52. req = blk_fetch_request(q);
  53. mq->mqrq_cur->req = req;
  54. spin_unlock_irq(q->queue_lock);
  55. if (req || mq->mqrq_prev->req) {
  56. set_current_state(TASK_RUNNING);
  57. cmd_flags = req ? req->cmd_flags : 0;
  58. mq->issue_fn(mq, req);
  59. if (mq->flags & MMC_QUEUE_NEW_REQUEST) {
  60. mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
  61. continue; /* fetch again */
  62. }
  63. /*
  64. * Current request becomes previous request
  65. * and vice versa.
  66. * In case of special requests, current request
  67. * has been finished. Do not assign it to previous
  68. * request.
  69. */
  70. if (cmd_flags & MMC_REQ_SPECIAL_MASK)
  71. mq->mqrq_cur->req = NULL;
  72. mq->mqrq_prev->brq.mrq.data = NULL;
  73. mq->mqrq_prev->req = NULL;
  74. tmp = mq->mqrq_prev;
  75. mq->mqrq_prev = mq->mqrq_cur;
  76. mq->mqrq_cur = tmp;
  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. q->limits.max_discard_sectors = 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_SECDISCARD, 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 = *mmc_dev(host)->dma_mask;
  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. if (mmc_can_erase(card))
  184. mmc_queue_setup_discard(mq->queue, card);
  185. #ifdef CONFIG_MMC_BLOCK_BOUNCE
  186. if (host->max_segs == 1) {
  187. unsigned int bouncesz;
  188. bouncesz = MMC_QUEUE_BOUNCESZ;
  189. if (bouncesz > host->max_req_size)
  190. bouncesz = host->max_req_size;
  191. if (bouncesz > host->max_seg_size)
  192. bouncesz = host->max_seg_size;
  193. if (bouncesz > (host->max_blk_count * 512))
  194. bouncesz = host->max_blk_count * 512;
  195. if (bouncesz > 512) {
  196. mqrq_cur->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  197. if (!mqrq_cur->bounce_buf) {
  198. pr_warning("%s: unable to "
  199. "allocate bounce cur buffer\n",
  200. mmc_card_name(card));
  201. }
  202. mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  203. if (!mqrq_prev->bounce_buf) {
  204. pr_warning("%s: unable to "
  205. "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. if (mqrq_cur->bounce_buf && mqrq_prev->bounce_buf) {
  212. blk_queue_bounce_limit(mq->queue, BLK_BOUNCE_ANY);
  213. blk_queue_max_hw_sectors(mq->queue, bouncesz / 512);
  214. blk_queue_max_segments(mq->queue, bouncesz / 512);
  215. blk_queue_max_segment_size(mq->queue, bouncesz);
  216. mqrq_cur->sg = mmc_alloc_sg(1, &ret);
  217. if (ret)
  218. goto cleanup_queue;
  219. mqrq_cur->bounce_sg =
  220. mmc_alloc_sg(bouncesz / 512, &ret);
  221. if (ret)
  222. goto cleanup_queue;
  223. mqrq_prev->sg = mmc_alloc_sg(1, &ret);
  224. if (ret)
  225. goto cleanup_queue;
  226. mqrq_prev->bounce_sg =
  227. mmc_alloc_sg(bouncesz / 512, &ret);
  228. if (ret)
  229. goto cleanup_queue;
  230. }
  231. }
  232. #endif
  233. if (!mqrq_cur->bounce_buf && !mqrq_prev->bounce_buf) {
  234. blk_queue_bounce_limit(mq->queue, limit);
  235. blk_queue_max_hw_sectors(mq->queue,
  236. min(host->max_blk_count, host->max_req_size / 512));
  237. blk_queue_max_segments(mq->queue, host->max_segs);
  238. blk_queue_max_segment_size(mq->queue, host->max_seg_size);
  239. mqrq_cur->sg = mmc_alloc_sg(host->max_segs, &ret);
  240. if (ret)
  241. goto cleanup_queue;
  242. mqrq_prev->sg = mmc_alloc_sg(host->max_segs, &ret);
  243. if (ret)
  244. goto cleanup_queue;
  245. }
  246. sema_init(&mq->thread_sem, 1);
  247. mq->thread = kthread_run(mmc_queue_thread, mq, "mmcqd/%d%s",
  248. host->index, subname ? subname : "");
  249. if (IS_ERR(mq->thread)) {
  250. ret = PTR_ERR(mq->thread);
  251. goto free_bounce_sg;
  252. }
  253. return 0;
  254. free_bounce_sg:
  255. kfree(mqrq_cur->bounce_sg);
  256. mqrq_cur->bounce_sg = NULL;
  257. kfree(mqrq_prev->bounce_sg);
  258. mqrq_prev->bounce_sg = NULL;
  259. cleanup_queue:
  260. kfree(mqrq_cur->sg);
  261. mqrq_cur->sg = NULL;
  262. kfree(mqrq_cur->bounce_buf);
  263. mqrq_cur->bounce_buf = NULL;
  264. kfree(mqrq_prev->sg);
  265. mqrq_prev->sg = NULL;
  266. kfree(mqrq_prev->bounce_buf);
  267. mqrq_prev->bounce_buf = NULL;
  268. blk_cleanup_queue(mq->queue);
  269. return ret;
  270. }
  271. void mmc_cleanup_queue(struct mmc_queue *mq)
  272. {
  273. struct request_queue *q = mq->queue;
  274. unsigned long flags;
  275. struct mmc_queue_req *mqrq_cur = mq->mqrq_cur;
  276. struct mmc_queue_req *mqrq_prev = mq->mqrq_prev;
  277. /* Make sure the queue isn't suspended, as that will deadlock */
  278. mmc_queue_resume(mq);
  279. /* Then terminate our worker thread */
  280. kthread_stop(mq->thread);
  281. /* Empty the queue */
  282. spin_lock_irqsave(q->queue_lock, flags);
  283. q->queuedata = NULL;
  284. blk_start_queue(q);
  285. spin_unlock_irqrestore(q->queue_lock, flags);
  286. kfree(mqrq_cur->bounce_sg);
  287. mqrq_cur->bounce_sg = NULL;
  288. kfree(mqrq_cur->sg);
  289. mqrq_cur->sg = NULL;
  290. kfree(mqrq_cur->bounce_buf);
  291. mqrq_cur->bounce_buf = NULL;
  292. kfree(mqrq_prev->bounce_sg);
  293. mqrq_prev->bounce_sg = NULL;
  294. kfree(mqrq_prev->sg);
  295. mqrq_prev->sg = NULL;
  296. kfree(mqrq_prev->bounce_buf);
  297. mqrq_prev->bounce_buf = NULL;
  298. mq->card = NULL;
  299. }
  300. EXPORT_SYMBOL(mmc_cleanup_queue);
  301. int mmc_packed_init(struct mmc_queue *mq, struct mmc_card *card)
  302. {
  303. struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
  304. struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
  305. int ret = 0;
  306. mqrq_cur->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
  307. if (!mqrq_cur->packed) {
  308. pr_warn("%s: unable to allocate packed cmd for mqrq_cur\n",
  309. mmc_card_name(card));
  310. ret = -ENOMEM;
  311. goto out;
  312. }
  313. mqrq_prev->packed = kzalloc(sizeof(struct mmc_packed), GFP_KERNEL);
  314. if (!mqrq_prev->packed) {
  315. pr_warn("%s: unable to allocate packed cmd for mqrq_prev\n",
  316. mmc_card_name(card));
  317. kfree(mqrq_cur->packed);
  318. mqrq_cur->packed = NULL;
  319. ret = -ENOMEM;
  320. goto out;
  321. }
  322. INIT_LIST_HEAD(&mqrq_cur->packed->list);
  323. INIT_LIST_HEAD(&mqrq_prev->packed->list);
  324. out:
  325. return ret;
  326. }
  327. void mmc_packed_clean(struct mmc_queue *mq)
  328. {
  329. struct mmc_queue_req *mqrq_cur = &mq->mqrq[0];
  330. struct mmc_queue_req *mqrq_prev = &mq->mqrq[1];
  331. kfree(mqrq_cur->packed);
  332. mqrq_cur->packed = NULL;
  333. kfree(mqrq_prev->packed);
  334. mqrq_prev->packed = NULL;
  335. }
  336. /**
  337. * mmc_queue_suspend - suspend a MMC request queue
  338. * @mq: MMC queue to suspend
  339. *
  340. * Stop the block request queue, and wait for our thread to
  341. * complete any outstanding requests. This ensures that we
  342. * won't suspend while a request is being processed.
  343. */
  344. void mmc_queue_suspend(struct mmc_queue *mq)
  345. {
  346. struct request_queue *q = mq->queue;
  347. unsigned long flags;
  348. if (!(mq->flags & MMC_QUEUE_SUSPENDED)) {
  349. mq->flags |= MMC_QUEUE_SUSPENDED;
  350. spin_lock_irqsave(q->queue_lock, flags);
  351. blk_stop_queue(q);
  352. spin_unlock_irqrestore(q->queue_lock, flags);
  353. down(&mq->thread_sem);
  354. }
  355. }
  356. /**
  357. * mmc_queue_resume - resume a previously suspended MMC request queue
  358. * @mq: MMC queue to resume
  359. */
  360. void mmc_queue_resume(struct mmc_queue *mq)
  361. {
  362. struct request_queue *q = mq->queue;
  363. unsigned long flags;
  364. if (mq->flags & MMC_QUEUE_SUSPENDED) {
  365. mq->flags &= ~MMC_QUEUE_SUSPENDED;
  366. up(&mq->thread_sem);
  367. spin_lock_irqsave(q->queue_lock, flags);
  368. blk_start_queue(q);
  369. spin_unlock_irqrestore(q->queue_lock, flags);
  370. }
  371. }
  372. static unsigned int mmc_queue_packed_map_sg(struct mmc_queue *mq,
  373. struct mmc_packed *packed,
  374. struct scatterlist *sg,
  375. enum mmc_packed_type cmd_type)
  376. {
  377. struct scatterlist *__sg = sg;
  378. unsigned int sg_len = 0;
  379. struct request *req;
  380. if (mmc_packed_wr(cmd_type)) {
  381. unsigned int hdr_sz = mmc_large_sector(mq->card) ? 4096 : 512;
  382. unsigned int max_seg_sz = queue_max_segment_size(mq->queue);
  383. unsigned int len, remain, offset = 0;
  384. u8 *buf = (u8 *)packed->cmd_hdr;
  385. remain = hdr_sz;
  386. do {
  387. len = min(remain, max_seg_sz);
  388. sg_set_buf(__sg, buf + offset, len);
  389. offset += len;
  390. remain -= len;
  391. (__sg++)->page_link &= ~0x02;
  392. sg_len++;
  393. } while (remain);
  394. }
  395. list_for_each_entry(req, &packed->list, queuelist) {
  396. sg_len += blk_rq_map_sg(mq->queue, req, __sg);
  397. __sg = sg + (sg_len - 1);
  398. (__sg++)->page_link &= ~0x02;
  399. }
  400. sg_mark_end(sg + (sg_len - 1));
  401. return sg_len;
  402. }
  403. /*
  404. * Prepare the sg list(s) to be handed of to the host driver
  405. */
  406. unsigned int mmc_queue_map_sg(struct mmc_queue *mq, struct mmc_queue_req *mqrq)
  407. {
  408. unsigned int sg_len;
  409. size_t buflen;
  410. struct scatterlist *sg;
  411. enum mmc_packed_type cmd_type;
  412. int i;
  413. cmd_type = mqrq->cmd_type;
  414. if (!mqrq->bounce_buf) {
  415. if (mmc_packed_cmd(cmd_type))
  416. return mmc_queue_packed_map_sg(mq, mqrq->packed,
  417. mqrq->sg, cmd_type);
  418. else
  419. return blk_rq_map_sg(mq->queue, mqrq->req, mqrq->sg);
  420. }
  421. BUG_ON(!mqrq->bounce_sg);
  422. if (mmc_packed_cmd(cmd_type))
  423. sg_len = mmc_queue_packed_map_sg(mq, mqrq->packed,
  424. mqrq->bounce_sg, cmd_type);
  425. else
  426. sg_len = blk_rq_map_sg(mq->queue, mqrq->req, mqrq->bounce_sg);
  427. mqrq->bounce_sg_len = sg_len;
  428. buflen = 0;
  429. for_each_sg(mqrq->bounce_sg, sg, sg_len, i)
  430. buflen += sg->length;
  431. sg_init_one(mqrq->sg, mqrq->bounce_buf, buflen);
  432. return 1;
  433. }
  434. /*
  435. * If writing, bounce the data to the buffer before the request
  436. * is sent to the host driver
  437. */
  438. void mmc_queue_bounce_pre(struct mmc_queue_req *mqrq)
  439. {
  440. if (!mqrq->bounce_buf)
  441. return;
  442. if (rq_data_dir(mqrq->req) != WRITE)
  443. return;
  444. sg_copy_to_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
  445. mqrq->bounce_buf, mqrq->sg[0].length);
  446. }
  447. /*
  448. * If reading, bounce the data from the buffer after the request
  449. * has been handled by the host driver
  450. */
  451. void mmc_queue_bounce_post(struct mmc_queue_req *mqrq)
  452. {
  453. if (!mqrq->bounce_buf)
  454. return;
  455. if (rq_data_dir(mqrq->req) != READ)
  456. return;
  457. sg_copy_from_buffer(mqrq->bounce_sg, mqrq->bounce_sg_len,
  458. mqrq->bounce_buf, mqrq->sg[0].length);
  459. }