block.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Block driver for media (i.e., flash cards)
  3. *
  4. * Copyright 2002 Hewlett-Packard Company
  5. * Copyright 2005-2008 Pierre Ossman
  6. *
  7. * Use consistent with the GNU GPL is permitted,
  8. * provided that this copyright notice is
  9. * preserved in its entirety in all copies and derived works.
  10. *
  11. * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
  12. * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
  13. * FITNESS FOR ANY PARTICULAR PURPOSE.
  14. *
  15. * Many thanks to Alessandro Rubini and Jonathan Corbet!
  16. *
  17. * Author: Andrew Christian
  18. * 28 May 2002
  19. */
  20. #include <linux/moduleparam.h>
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/fs.h>
  25. #include <linux/errno.h>
  26. #include <linux/hdreg.h>
  27. #include <linux/kdev_t.h>
  28. #include <linux/blkdev.h>
  29. #include <linux/mutex.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/mmc/card.h>
  32. #include <linux/mmc/host.h>
  33. #include <linux/mmc/mmc.h>
  34. #include <linux/mmc/sd.h>
  35. #include <asm/system.h>
  36. #include <asm/uaccess.h>
  37. #include "queue.h"
  38. /*
  39. * max 8 partitions per card
  40. */
  41. #define MMC_SHIFT 3
  42. #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
  43. static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
  44. /*
  45. * There is one mmc_blk_data per slot.
  46. */
  47. struct mmc_blk_data {
  48. spinlock_t lock;
  49. struct gendisk *disk;
  50. struct mmc_queue queue;
  51. unsigned int usage;
  52. unsigned int block_bits;
  53. unsigned int read_only;
  54. };
  55. static DEFINE_MUTEX(open_lock);
  56. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  57. {
  58. struct mmc_blk_data *md;
  59. mutex_lock(&open_lock);
  60. md = disk->private_data;
  61. if (md && md->usage == 0)
  62. md = NULL;
  63. if (md)
  64. md->usage++;
  65. mutex_unlock(&open_lock);
  66. return md;
  67. }
  68. static void mmc_blk_put(struct mmc_blk_data *md)
  69. {
  70. mutex_lock(&open_lock);
  71. md->usage--;
  72. if (md->usage == 0) {
  73. int devidx = md->disk->first_minor >> MMC_SHIFT;
  74. __clear_bit(devidx, dev_use);
  75. put_disk(md->disk);
  76. kfree(md);
  77. }
  78. mutex_unlock(&open_lock);
  79. }
  80. static int mmc_blk_open(struct inode *inode, struct file *filp)
  81. {
  82. struct mmc_blk_data *md;
  83. int ret = -ENXIO;
  84. md = mmc_blk_get(inode->i_bdev->bd_disk);
  85. if (md) {
  86. if (md->usage == 2)
  87. check_disk_change(inode->i_bdev);
  88. ret = 0;
  89. if ((filp->f_mode & FMODE_WRITE) && md->read_only) {
  90. mmc_blk_put(md);
  91. ret = -EROFS;
  92. }
  93. }
  94. return ret;
  95. }
  96. static int mmc_blk_release(struct inode *inode, struct file *filp)
  97. {
  98. struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
  99. mmc_blk_put(md);
  100. return 0;
  101. }
  102. static int
  103. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  104. {
  105. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  106. geo->heads = 4;
  107. geo->sectors = 16;
  108. return 0;
  109. }
  110. static struct block_device_operations mmc_bdops = {
  111. .open = mmc_blk_open,
  112. .release = mmc_blk_release,
  113. .getgeo = mmc_blk_getgeo,
  114. .owner = THIS_MODULE,
  115. };
  116. struct mmc_blk_request {
  117. struct mmc_request mrq;
  118. struct mmc_command cmd;
  119. struct mmc_command stop;
  120. struct mmc_data data;
  121. };
  122. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  123. {
  124. int err;
  125. u32 blocks;
  126. struct mmc_request mrq;
  127. struct mmc_command cmd;
  128. struct mmc_data data;
  129. unsigned int timeout_us;
  130. struct scatterlist sg;
  131. memset(&cmd, 0, sizeof(struct mmc_command));
  132. cmd.opcode = MMC_APP_CMD;
  133. cmd.arg = card->rca << 16;
  134. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  135. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  136. if (err)
  137. return (u32)-1;
  138. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  139. return (u32)-1;
  140. memset(&cmd, 0, sizeof(struct mmc_command));
  141. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  142. cmd.arg = 0;
  143. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  144. memset(&data, 0, sizeof(struct mmc_data));
  145. data.timeout_ns = card->csd.tacc_ns * 100;
  146. data.timeout_clks = card->csd.tacc_clks * 100;
  147. timeout_us = data.timeout_ns / 1000;
  148. timeout_us += data.timeout_clks * 1000 /
  149. (card->host->ios.clock / 1000);
  150. if (timeout_us > 100000) {
  151. data.timeout_ns = 100000000;
  152. data.timeout_clks = 0;
  153. }
  154. data.blksz = 4;
  155. data.blocks = 1;
  156. data.flags = MMC_DATA_READ;
  157. data.sg = &sg;
  158. data.sg_len = 1;
  159. memset(&mrq, 0, sizeof(struct mmc_request));
  160. mrq.cmd = &cmd;
  161. mrq.data = &data;
  162. sg_init_one(&sg, &blocks, 4);
  163. mmc_wait_for_req(card->host, &mrq);
  164. if (cmd.error || data.error)
  165. return (u32)-1;
  166. blocks = ntohl(blocks);
  167. return blocks;
  168. }
  169. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  170. {
  171. struct mmc_blk_data *md = mq->data;
  172. struct mmc_card *card = md->queue.card;
  173. struct mmc_blk_request brq;
  174. int ret = 1, data_size, i;
  175. struct scatterlist *sg;
  176. mmc_claim_host(card->host);
  177. do {
  178. struct mmc_command cmd;
  179. u32 readcmd, writecmd;
  180. memset(&brq, 0, sizeof(struct mmc_blk_request));
  181. brq.mrq.cmd = &brq.cmd;
  182. brq.mrq.data = &brq.data;
  183. brq.cmd.arg = req->sector;
  184. if (!mmc_card_blockaddr(card))
  185. brq.cmd.arg <<= 9;
  186. brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  187. brq.data.blksz = 1 << md->block_bits;
  188. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  189. brq.stop.arg = 0;
  190. brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  191. brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
  192. if (brq.data.blocks > card->host->max_blk_count)
  193. brq.data.blocks = card->host->max_blk_count;
  194. if (brq.data.blocks > 1) {
  195. /* SPI multiblock writes terminate using a special
  196. * token, not a STOP_TRANSMISSION request.
  197. */
  198. if (!mmc_host_is_spi(card->host)
  199. || rq_data_dir(req) == READ)
  200. brq.mrq.stop = &brq.stop;
  201. readcmd = MMC_READ_MULTIPLE_BLOCK;
  202. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  203. } else {
  204. brq.mrq.stop = NULL;
  205. readcmd = MMC_READ_SINGLE_BLOCK;
  206. writecmd = MMC_WRITE_BLOCK;
  207. }
  208. if (rq_data_dir(req) == READ) {
  209. brq.cmd.opcode = readcmd;
  210. brq.data.flags |= MMC_DATA_READ;
  211. } else {
  212. brq.cmd.opcode = writecmd;
  213. brq.data.flags |= MMC_DATA_WRITE;
  214. }
  215. mmc_set_data_timeout(&brq.data, card);
  216. brq.data.sg = mq->sg;
  217. brq.data.sg_len = mmc_queue_map_sg(mq);
  218. mmc_queue_bounce_pre(mq);
  219. /*
  220. * Adjust the sg list so it is the same size as the
  221. * request.
  222. */
  223. if (brq.data.blocks !=
  224. (req->nr_sectors >> (md->block_bits - 9))) {
  225. data_size = brq.data.blocks * brq.data.blksz;
  226. for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
  227. data_size -= sg->length;
  228. if (data_size <= 0) {
  229. sg->length += data_size;
  230. i++;
  231. break;
  232. }
  233. }
  234. brq.data.sg_len = i;
  235. }
  236. mmc_wait_for_req(card->host, &brq.mrq);
  237. mmc_queue_bounce_post(mq);
  238. /*
  239. * Check for errors here, but don't jump to cmd_err
  240. * until later as we need to wait for the card to leave
  241. * programming mode even when things go wrong.
  242. */
  243. if (brq.cmd.error) {
  244. printk(KERN_ERR "%s: error %d sending read/write command\n",
  245. req->rq_disk->disk_name, brq.cmd.error);
  246. }
  247. if (brq.data.error) {
  248. printk(KERN_ERR "%s: error %d transferring data\n",
  249. req->rq_disk->disk_name, brq.data.error);
  250. }
  251. if (brq.stop.error) {
  252. printk(KERN_ERR "%s: error %d sending stop command\n",
  253. req->rq_disk->disk_name, brq.stop.error);
  254. }
  255. if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
  256. do {
  257. int err;
  258. cmd.opcode = MMC_SEND_STATUS;
  259. cmd.arg = card->rca << 16;
  260. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  261. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  262. if (err) {
  263. printk(KERN_ERR "%s: error %d requesting status\n",
  264. req->rq_disk->disk_name, err);
  265. goto cmd_err;
  266. }
  267. /*
  268. * Some cards mishandle the status bits,
  269. * so make sure to check both the busy
  270. * indication and the card state.
  271. */
  272. } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
  273. (R1_CURRENT_STATE(cmd.resp[0]) == 7));
  274. #if 0
  275. if (cmd.resp[0] & ~0x00000900)
  276. printk(KERN_ERR "%s: status = %08x\n",
  277. req->rq_disk->disk_name, cmd.resp[0]);
  278. if (mmc_decode_status(cmd.resp))
  279. goto cmd_err;
  280. #endif
  281. }
  282. if (brq.cmd.error || brq.data.error || brq.stop.error)
  283. goto cmd_err;
  284. /*
  285. * A block was successfully transferred.
  286. */
  287. spin_lock_irq(&md->lock);
  288. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  289. spin_unlock_irq(&md->lock);
  290. } while (ret);
  291. mmc_release_host(card->host);
  292. return 1;
  293. cmd_err:
  294. /*
  295. * If this is an SD card and we're writing, we can first
  296. * mark the known good sectors as ok.
  297. *
  298. * If the card is not SD, we can still ok written sectors
  299. * as reported by the controller (which might be less than
  300. * the real number of written sectors, but never more).
  301. *
  302. * For reads we just fail the entire chunk as that should
  303. * be safe in all cases.
  304. */
  305. if (rq_data_dir(req) != READ) {
  306. if (mmc_card_sd(card)) {
  307. u32 blocks;
  308. unsigned int bytes;
  309. blocks = mmc_sd_num_wr_blocks(card);
  310. if (blocks != (u32)-1) {
  311. if (card->csd.write_partial)
  312. bytes = blocks << md->block_bits;
  313. else
  314. bytes = blocks << 9;
  315. spin_lock_irq(&md->lock);
  316. ret = __blk_end_request(req, 0, bytes);
  317. spin_unlock_irq(&md->lock);
  318. }
  319. } else {
  320. spin_lock_irq(&md->lock);
  321. ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
  322. spin_unlock_irq(&md->lock);
  323. }
  324. }
  325. mmc_release_host(card->host);
  326. spin_lock_irq(&md->lock);
  327. while (ret)
  328. ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
  329. spin_unlock_irq(&md->lock);
  330. return 0;
  331. }
  332. static inline int mmc_blk_readonly(struct mmc_card *card)
  333. {
  334. return mmc_card_readonly(card) ||
  335. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  336. }
  337. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  338. {
  339. struct mmc_blk_data *md;
  340. int devidx, ret;
  341. devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
  342. if (devidx >= MMC_NUM_MINORS)
  343. return ERR_PTR(-ENOSPC);
  344. __set_bit(devidx, dev_use);
  345. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  346. if (!md) {
  347. ret = -ENOMEM;
  348. goto out;
  349. }
  350. /*
  351. * Set the read-only status based on the supported commands
  352. * and the write protect switch.
  353. */
  354. md->read_only = mmc_blk_readonly(card);
  355. /*
  356. * Both SD and MMC specifications state (although a bit
  357. * unclearly in the MMC case) that a block size of 512
  358. * bytes must always be supported by the card.
  359. */
  360. md->block_bits = 9;
  361. md->disk = alloc_disk(1 << MMC_SHIFT);
  362. if (md->disk == NULL) {
  363. ret = -ENOMEM;
  364. goto err_kfree;
  365. }
  366. spin_lock_init(&md->lock);
  367. md->usage = 1;
  368. ret = mmc_init_queue(&md->queue, card, &md->lock);
  369. if (ret)
  370. goto err_putdisk;
  371. md->queue.issue_fn = mmc_blk_issue_rq;
  372. md->queue.data = md;
  373. md->disk->major = MMC_BLOCK_MAJOR;
  374. md->disk->first_minor = devidx << MMC_SHIFT;
  375. md->disk->fops = &mmc_bdops;
  376. md->disk->private_data = md;
  377. md->disk->queue = md->queue.queue;
  378. md->disk->driverfs_dev = &card->dev;
  379. /*
  380. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  381. *
  382. * - be set for removable media with permanent block devices
  383. * - be unset for removable block devices with permanent media
  384. *
  385. * Since MMC block devices clearly fall under the second
  386. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  387. * should use the block device creation/destruction hotplug
  388. * messages to tell when the card is present.
  389. */
  390. sprintf(md->disk->disk_name, "mmcblk%d", devidx);
  391. blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
  392. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  393. /*
  394. * The EXT_CSD sector count is in number or 512 byte
  395. * sectors.
  396. */
  397. set_capacity(md->disk, card->ext_csd.sectors);
  398. } else {
  399. /*
  400. * The CSD capacity field is in units of read_blkbits.
  401. * set_capacity takes units of 512 bytes.
  402. */
  403. set_capacity(md->disk,
  404. card->csd.capacity << (card->csd.read_blkbits - 9));
  405. }
  406. return md;
  407. err_putdisk:
  408. put_disk(md->disk);
  409. err_kfree:
  410. kfree(md);
  411. out:
  412. return ERR_PTR(ret);
  413. }
  414. static int
  415. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  416. {
  417. struct mmc_command cmd;
  418. int err;
  419. /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
  420. if (mmc_card_blockaddr(card))
  421. return 0;
  422. mmc_claim_host(card->host);
  423. cmd.opcode = MMC_SET_BLOCKLEN;
  424. cmd.arg = 1 << md->block_bits;
  425. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  426. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  427. mmc_release_host(card->host);
  428. if (err) {
  429. printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
  430. md->disk->disk_name, cmd.arg, err);
  431. return -EINVAL;
  432. }
  433. return 0;
  434. }
  435. static int mmc_blk_probe(struct mmc_card *card)
  436. {
  437. struct mmc_blk_data *md;
  438. int err;
  439. /*
  440. * Check that the card supports the command class(es) we need.
  441. */
  442. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  443. return -ENODEV;
  444. md = mmc_blk_alloc(card);
  445. if (IS_ERR(md))
  446. return PTR_ERR(md);
  447. err = mmc_blk_set_blksize(md, card);
  448. if (err)
  449. goto out;
  450. printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
  451. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  452. (unsigned long long)(get_capacity(md->disk) >> 1),
  453. md->read_only ? "(ro)" : "");
  454. mmc_set_drvdata(card, md);
  455. add_disk(md->disk);
  456. return 0;
  457. out:
  458. mmc_blk_put(md);
  459. return err;
  460. }
  461. static void mmc_blk_remove(struct mmc_card *card)
  462. {
  463. struct mmc_blk_data *md = mmc_get_drvdata(card);
  464. if (md) {
  465. /* Stop new requests from getting into the queue */
  466. del_gendisk(md->disk);
  467. /* Then flush out any already in there */
  468. mmc_cleanup_queue(&md->queue);
  469. mmc_blk_put(md);
  470. }
  471. mmc_set_drvdata(card, NULL);
  472. }
  473. #ifdef CONFIG_PM
  474. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  475. {
  476. struct mmc_blk_data *md = mmc_get_drvdata(card);
  477. if (md) {
  478. mmc_queue_suspend(&md->queue);
  479. }
  480. return 0;
  481. }
  482. static int mmc_blk_resume(struct mmc_card *card)
  483. {
  484. struct mmc_blk_data *md = mmc_get_drvdata(card);
  485. if (md) {
  486. mmc_blk_set_blksize(md, card);
  487. mmc_queue_resume(&md->queue);
  488. }
  489. return 0;
  490. }
  491. #else
  492. #define mmc_blk_suspend NULL
  493. #define mmc_blk_resume NULL
  494. #endif
  495. static struct mmc_driver mmc_driver = {
  496. .drv = {
  497. .name = "mmcblk",
  498. },
  499. .probe = mmc_blk_probe,
  500. .remove = mmc_blk_remove,
  501. .suspend = mmc_blk_suspend,
  502. .resume = mmc_blk_resume,
  503. };
  504. static int __init mmc_blk_init(void)
  505. {
  506. int res;
  507. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  508. if (res)
  509. goto out;
  510. res = mmc_register_driver(&mmc_driver);
  511. if (res)
  512. goto out2;
  513. return 0;
  514. out2:
  515. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  516. out:
  517. return res;
  518. }
  519. static void __exit mmc_blk_exit(void)
  520. {
  521. mmc_unregister_driver(&mmc_driver);
  522. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  523. }
  524. module_init(mmc_blk_init);
  525. module_exit(mmc_blk_exit);
  526. MODULE_LICENSE("GPL");
  527. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");