block.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. /*
  2. * Block driver for media (i.e., flash cards)
  3. *
  4. * Copyright 2002 Hewlett-Packard Company
  5. * Copyright 2005-2007 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 unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
  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. ret = -EROFS;
  91. }
  92. return ret;
  93. }
  94. static int mmc_blk_release(struct inode *inode, struct file *filp)
  95. {
  96. struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
  97. mmc_blk_put(md);
  98. return 0;
  99. }
  100. static int
  101. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  102. {
  103. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  104. geo->heads = 4;
  105. geo->sectors = 16;
  106. return 0;
  107. }
  108. static struct block_device_operations mmc_bdops = {
  109. .open = mmc_blk_open,
  110. .release = mmc_blk_release,
  111. .getgeo = mmc_blk_getgeo,
  112. .owner = THIS_MODULE,
  113. };
  114. struct mmc_blk_request {
  115. struct mmc_request mrq;
  116. struct mmc_command cmd;
  117. struct mmc_command stop;
  118. struct mmc_data data;
  119. };
  120. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  121. {
  122. int err;
  123. u32 blocks;
  124. struct mmc_request mrq;
  125. struct mmc_command cmd;
  126. struct mmc_data data;
  127. unsigned int timeout_us;
  128. struct scatterlist sg;
  129. memset(&cmd, 0, sizeof(struct mmc_command));
  130. cmd.opcode = MMC_APP_CMD;
  131. cmd.arg = card->rca << 16;
  132. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  133. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  134. if (err)
  135. return (u32)-1;
  136. if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
  137. return (u32)-1;
  138. memset(&cmd, 0, sizeof(struct mmc_command));
  139. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  140. cmd.arg = 0;
  141. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  142. memset(&data, 0, sizeof(struct mmc_data));
  143. data.timeout_ns = card->csd.tacc_ns * 100;
  144. data.timeout_clks = card->csd.tacc_clks * 100;
  145. timeout_us = data.timeout_ns / 1000;
  146. timeout_us += data.timeout_clks * 1000 /
  147. (card->host->ios.clock / 1000);
  148. if (timeout_us > 100000) {
  149. data.timeout_ns = 100000000;
  150. data.timeout_clks = 0;
  151. }
  152. data.blksz = 4;
  153. data.blocks = 1;
  154. data.flags = MMC_DATA_READ;
  155. data.sg = &sg;
  156. data.sg_len = 1;
  157. memset(&mrq, 0, sizeof(struct mmc_request));
  158. mrq.cmd = &cmd;
  159. mrq.data = &data;
  160. sg_init_one(&sg, &blocks, 4);
  161. mmc_wait_for_req(card->host, &mrq);
  162. if (cmd.error || data.error)
  163. return (u32)-1;
  164. blocks = ntohl(blocks);
  165. return blocks;
  166. }
  167. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  168. {
  169. struct mmc_blk_data *md = mq->data;
  170. struct mmc_card *card = md->queue.card;
  171. struct mmc_blk_request brq;
  172. int ret = 1, sg_pos, data_size;
  173. mmc_claim_host(card->host);
  174. do {
  175. struct mmc_command cmd;
  176. u32 readcmd, writecmd;
  177. memset(&brq, 0, sizeof(struct mmc_blk_request));
  178. brq.mrq.cmd = &brq.cmd;
  179. brq.mrq.data = &brq.data;
  180. brq.cmd.arg = req->sector;
  181. if (!mmc_card_blockaddr(card))
  182. brq.cmd.arg <<= 9;
  183. brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
  184. brq.data.blksz = 1 << md->block_bits;
  185. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  186. brq.stop.arg = 0;
  187. brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
  188. brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
  189. if (brq.data.blocks > card->host->max_blk_count)
  190. brq.data.blocks = card->host->max_blk_count;
  191. /*
  192. * If the host doesn't support multiple block writes, force
  193. * block writes to single block. SD cards are excepted from
  194. * this rule as they support querying the number of
  195. * successfully written sectors.
  196. */
  197. if (rq_data_dir(req) != READ &&
  198. !(card->host->caps & MMC_CAP_MULTIWRITE) &&
  199. !mmc_card_sd(card))
  200. brq.data.blocks = 1;
  201. if (brq.data.blocks > 1) {
  202. /* SPI multiblock writes terminate using a special
  203. * token, not a STOP_TRANSMISSION request.
  204. */
  205. if (!mmc_host_is_spi(card->host)
  206. || rq_data_dir(req) == READ)
  207. brq.mrq.stop = &brq.stop;
  208. readcmd = MMC_READ_MULTIPLE_BLOCK;
  209. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  210. } else {
  211. brq.mrq.stop = NULL;
  212. readcmd = MMC_READ_SINGLE_BLOCK;
  213. writecmd = MMC_WRITE_BLOCK;
  214. }
  215. if (rq_data_dir(req) == READ) {
  216. brq.cmd.opcode = readcmd;
  217. brq.data.flags |= MMC_DATA_READ;
  218. } else {
  219. brq.cmd.opcode = writecmd;
  220. brq.data.flags |= MMC_DATA_WRITE;
  221. }
  222. mmc_set_data_timeout(&brq.data, card);
  223. brq.data.sg = mq->sg;
  224. brq.data.sg_len = mmc_queue_map_sg(mq);
  225. mmc_queue_bounce_pre(mq);
  226. if (brq.data.blocks !=
  227. (req->nr_sectors >> (md->block_bits - 9))) {
  228. data_size = brq.data.blocks * brq.data.blksz;
  229. for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
  230. data_size -= mq->sg[sg_pos].length;
  231. if (data_size <= 0) {
  232. mq->sg[sg_pos].length += data_size;
  233. sg_pos++;
  234. break;
  235. }
  236. }
  237. brq.data.sg_len = sg_pos;
  238. }
  239. mmc_wait_for_req(card->host, &brq.mrq);
  240. mmc_queue_bounce_post(mq);
  241. if (brq.cmd.error) {
  242. printk(KERN_ERR "%s: error %d sending read/write command\n",
  243. req->rq_disk->disk_name, brq.cmd.error);
  244. goto cmd_err;
  245. }
  246. if (brq.data.error) {
  247. printk(KERN_ERR "%s: error %d transferring data\n",
  248. req->rq_disk->disk_name, brq.data.error);
  249. goto cmd_err;
  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. goto cmd_err;
  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. /*
  284. * A block was successfully transferred.
  285. */
  286. spin_lock_irq(&md->lock);
  287. ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
  288. if (!ret) {
  289. /*
  290. * The whole request completed successfully.
  291. */
  292. add_disk_randomness(req->rq_disk);
  293. blkdev_dequeue_request(req);
  294. end_that_request_last(req, 1);
  295. }
  296. spin_unlock_irq(&md->lock);
  297. } while (ret);
  298. mmc_release_host(card->host);
  299. return 1;
  300. cmd_err:
  301. /*
  302. * If this is an SD card and we're writing, we can first
  303. * mark the known good sectors as ok.
  304. *
  305. * If the card is not SD, we can still ok written sectors
  306. * if the controller can do proper error reporting.
  307. *
  308. * For reads we just fail the entire chunk as that should
  309. * be safe in all cases.
  310. */
  311. if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
  312. u32 blocks;
  313. unsigned int bytes;
  314. blocks = mmc_sd_num_wr_blocks(card);
  315. if (blocks != (u32)-1) {
  316. if (card->csd.write_partial)
  317. bytes = blocks << md->block_bits;
  318. else
  319. bytes = blocks << 9;
  320. spin_lock_irq(&md->lock);
  321. ret = end_that_request_chunk(req, 1, bytes);
  322. spin_unlock_irq(&md->lock);
  323. }
  324. } else if (rq_data_dir(req) != READ &&
  325. (card->host->caps & MMC_CAP_MULTIWRITE)) {
  326. spin_lock_irq(&md->lock);
  327. ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
  328. spin_unlock_irq(&md->lock);
  329. }
  330. mmc_release_host(card->host);
  331. spin_lock_irq(&md->lock);
  332. while (ret) {
  333. ret = end_that_request_chunk(req, 0,
  334. req->current_nr_sectors << 9);
  335. }
  336. add_disk_randomness(req->rq_disk);
  337. blkdev_dequeue_request(req);
  338. end_that_request_last(req, 0);
  339. spin_unlock_irq(&md->lock);
  340. return 0;
  341. }
  342. static inline int mmc_blk_readonly(struct mmc_card *card)
  343. {
  344. return mmc_card_readonly(card) ||
  345. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  346. }
  347. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  348. {
  349. struct mmc_blk_data *md;
  350. int devidx, ret;
  351. devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
  352. if (devidx >= MMC_NUM_MINORS)
  353. return ERR_PTR(-ENOSPC);
  354. __set_bit(devidx, dev_use);
  355. md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  356. if (!md) {
  357. ret = -ENOMEM;
  358. goto out;
  359. }
  360. /*
  361. * Set the read-only status based on the supported commands
  362. * and the write protect switch.
  363. */
  364. md->read_only = mmc_blk_readonly(card);
  365. /*
  366. * Both SD and MMC specifications state (although a bit
  367. * unclearly in the MMC case) that a block size of 512
  368. * bytes must always be supported by the card.
  369. */
  370. md->block_bits = 9;
  371. md->disk = alloc_disk(1 << MMC_SHIFT);
  372. if (md->disk == NULL) {
  373. ret = -ENOMEM;
  374. goto err_kfree;
  375. }
  376. spin_lock_init(&md->lock);
  377. md->usage = 1;
  378. ret = mmc_init_queue(&md->queue, card, &md->lock);
  379. if (ret)
  380. goto err_putdisk;
  381. md->queue.issue_fn = mmc_blk_issue_rq;
  382. md->queue.data = md;
  383. md->disk->major = MMC_BLOCK_MAJOR;
  384. md->disk->first_minor = devidx << MMC_SHIFT;
  385. md->disk->fops = &mmc_bdops;
  386. md->disk->private_data = md;
  387. md->disk->queue = md->queue.queue;
  388. md->disk->driverfs_dev = &card->dev;
  389. /*
  390. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  391. *
  392. * - be set for removable media with permanent block devices
  393. * - be unset for removable block devices with permanent media
  394. *
  395. * Since MMC block devices clearly fall under the second
  396. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  397. * should use the block device creation/destruction hotplug
  398. * messages to tell when the card is present.
  399. */
  400. sprintf(md->disk->disk_name, "mmcblk%d", devidx);
  401. blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
  402. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  403. /*
  404. * The EXT_CSD sector count is in number or 512 byte
  405. * sectors.
  406. */
  407. set_capacity(md->disk, card->ext_csd.sectors);
  408. } else {
  409. /*
  410. * The CSD capacity field is in units of read_blkbits.
  411. * set_capacity takes units of 512 bytes.
  412. */
  413. set_capacity(md->disk,
  414. card->csd.capacity << (card->csd.read_blkbits - 9));
  415. }
  416. return md;
  417. err_putdisk:
  418. put_disk(md->disk);
  419. err_kfree:
  420. kfree(md);
  421. out:
  422. return ERR_PTR(ret);
  423. }
  424. static int
  425. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  426. {
  427. struct mmc_command cmd;
  428. int err;
  429. /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
  430. if (mmc_card_blockaddr(card))
  431. return 0;
  432. mmc_claim_host(card->host);
  433. cmd.opcode = MMC_SET_BLOCKLEN;
  434. cmd.arg = 1 << md->block_bits;
  435. cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
  436. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  437. mmc_release_host(card->host);
  438. if (err) {
  439. printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
  440. md->disk->disk_name, cmd.arg, err);
  441. return -EINVAL;
  442. }
  443. return 0;
  444. }
  445. static int mmc_blk_probe(struct mmc_card *card)
  446. {
  447. struct mmc_blk_data *md;
  448. int err;
  449. /*
  450. * Check that the card supports the command class(es) we need.
  451. */
  452. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  453. return -ENODEV;
  454. md = mmc_blk_alloc(card);
  455. if (IS_ERR(md))
  456. return PTR_ERR(md);
  457. err = mmc_blk_set_blksize(md, card);
  458. if (err)
  459. goto out;
  460. printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
  461. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  462. (unsigned long long)(get_capacity(md->disk) >> 1),
  463. md->read_only ? "(ro)" : "");
  464. mmc_set_drvdata(card, md);
  465. add_disk(md->disk);
  466. return 0;
  467. out:
  468. mmc_blk_put(md);
  469. return err;
  470. }
  471. static void mmc_blk_remove(struct mmc_card *card)
  472. {
  473. struct mmc_blk_data *md = mmc_get_drvdata(card);
  474. if (md) {
  475. /* Stop new requests from getting into the queue */
  476. del_gendisk(md->disk);
  477. /* Then flush out any already in there */
  478. mmc_cleanup_queue(&md->queue);
  479. mmc_blk_put(md);
  480. }
  481. mmc_set_drvdata(card, NULL);
  482. }
  483. #ifdef CONFIG_PM
  484. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  485. {
  486. struct mmc_blk_data *md = mmc_get_drvdata(card);
  487. if (md) {
  488. mmc_queue_suspend(&md->queue);
  489. }
  490. return 0;
  491. }
  492. static int mmc_blk_resume(struct mmc_card *card)
  493. {
  494. struct mmc_blk_data *md = mmc_get_drvdata(card);
  495. if (md) {
  496. mmc_blk_set_blksize(md, card);
  497. mmc_queue_resume(&md->queue);
  498. }
  499. return 0;
  500. }
  501. #else
  502. #define mmc_blk_suspend NULL
  503. #define mmc_blk_resume NULL
  504. #endif
  505. static struct mmc_driver mmc_driver = {
  506. .drv = {
  507. .name = "mmcblk",
  508. },
  509. .probe = mmc_blk_probe,
  510. .remove = mmc_blk_remove,
  511. .suspend = mmc_blk_suspend,
  512. .resume = mmc_blk_resume,
  513. };
  514. static int __init mmc_blk_init(void)
  515. {
  516. int res = -ENOMEM;
  517. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  518. if (res)
  519. goto out;
  520. return mmc_register_driver(&mmc_driver);
  521. out:
  522. return res;
  523. }
  524. static void __exit mmc_blk_exit(void)
  525. {
  526. mmc_unregister_driver(&mmc_driver);
  527. unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
  528. }
  529. module_init(mmc_blk_init);
  530. module_exit(mmc_blk_exit);
  531. MODULE_LICENSE("GPL");
  532. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");