block.c 13 KB

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