mmc_block.c 14 KB

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