block.c 14 KB

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