block.c 14 KB

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