queue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. #define MMC_REQ_SPECIAL_MASK (REQ_DISCARD | REQ_FLUSH)
  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))
  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) || mmc_can_sanitize(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 = *mmc_dev(host)->dma_mask;
  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. 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_warning("%s: unable to "
  200. "allocate bounce cur buffer\n",
  201. mmc_card_name(card));
  202. }
  203. mqrq_prev->bounce_buf = kmalloc(bouncesz, GFP_KERNEL);
  204. if (!mqrq_prev->bounce_buf) {
  205. pr_warning("%s: unable to "
  206. "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. 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++)->page_link &= ~0x02;
  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++)->page_link &= ~0x02;
  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. }