mmc_block.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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. mmc_cleanup_queue(&md->queue);
  74. kfree(md);
  75. }
  76. mutex_unlock(&open_lock);
  77. }
  78. static int mmc_blk_open(struct inode *inode, struct file *filp)
  79. {
  80. struct mmc_blk_data *md;
  81. int ret = -ENXIO;
  82. md = mmc_blk_get(inode->i_bdev->bd_disk);
  83. if (md) {
  84. if (md->usage == 2)
  85. check_disk_change(inode->i_bdev);
  86. ret = 0;
  87. if ((filp->f_mode & FMODE_WRITE) && md->read_only)
  88. ret = -EROFS;
  89. }
  90. return ret;
  91. }
  92. static int mmc_blk_release(struct inode *inode, struct file *filp)
  93. {
  94. struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
  95. mmc_blk_put(md);
  96. return 0;
  97. }
  98. static int
  99. mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  100. {
  101. geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
  102. geo->heads = 4;
  103. geo->sectors = 16;
  104. return 0;
  105. }
  106. static struct block_device_operations mmc_bdops = {
  107. .open = mmc_blk_open,
  108. .release = mmc_blk_release,
  109. .getgeo = mmc_blk_getgeo,
  110. .owner = THIS_MODULE,
  111. };
  112. struct mmc_blk_request {
  113. struct mmc_request mrq;
  114. struct mmc_command cmd;
  115. struct mmc_command stop;
  116. struct mmc_data data;
  117. };
  118. static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
  119. {
  120. struct mmc_blk_data *md = mq->data;
  121. int stat = BLKPREP_OK;
  122. /*
  123. * If we have no device, we haven't finished initialising.
  124. */
  125. if (!md || !mq->card) {
  126. printk(KERN_ERR "%s: killing request - no device/host\n",
  127. req->rq_disk->disk_name);
  128. stat = BLKPREP_KILL;
  129. }
  130. return stat;
  131. }
  132. static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  133. {
  134. int err;
  135. u32 blocks;
  136. struct mmc_request mrq;
  137. struct mmc_command cmd;
  138. struct mmc_data data;
  139. unsigned int timeout_us;
  140. struct scatterlist sg;
  141. memset(&cmd, 0, sizeof(struct mmc_command));
  142. cmd.opcode = MMC_APP_CMD;
  143. cmd.arg = card->rca << 16;
  144. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  145. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  146. if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
  147. return (u32)-1;
  148. memset(&cmd, 0, sizeof(struct mmc_command));
  149. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  150. cmd.arg = 0;
  151. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  152. memset(&data, 0, sizeof(struct mmc_data));
  153. data.timeout_ns = card->csd.tacc_ns * 100;
  154. data.timeout_clks = card->csd.tacc_clks * 100;
  155. timeout_us = data.timeout_ns / 1000;
  156. timeout_us += data.timeout_clks * 1000 /
  157. (card->host->ios.clock / 1000);
  158. if (timeout_us > 100000) {
  159. data.timeout_ns = 100000000;
  160. data.timeout_clks = 0;
  161. }
  162. data.blksz = 4;
  163. data.blocks = 1;
  164. data.flags = MMC_DATA_READ;
  165. data.sg = &sg;
  166. data.sg_len = 1;
  167. memset(&mrq, 0, sizeof(struct mmc_request));
  168. mrq.cmd = &cmd;
  169. mrq.data = &data;
  170. sg_init_one(&sg, &blocks, 4);
  171. mmc_wait_for_req(card->host, &mrq);
  172. if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
  173. return (u32)-1;
  174. blocks = ntohl(blocks);
  175. return blocks;
  176. }
  177. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  178. {
  179. struct mmc_blk_data *md = mq->data;
  180. struct mmc_card *card = md->queue.card;
  181. struct mmc_blk_request brq;
  182. int ret;
  183. if (mmc_card_claim_host(card))
  184. goto cmd_err;
  185. do {
  186. struct mmc_command cmd;
  187. u32 readcmd, writecmd;
  188. memset(&brq, 0, sizeof(struct mmc_blk_request));
  189. brq.mrq.cmd = &brq.cmd;
  190. brq.mrq.data = &brq.data;
  191. brq.cmd.arg = req->sector << 9;
  192. brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  193. brq.data.blksz = 1 << md->block_bits;
  194. brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
  195. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  196. brq.stop.arg = 0;
  197. brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
  198. mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
  199. /*
  200. * If the host doesn't support multiple block writes, force
  201. * block writes to single block. SD cards are excepted from
  202. * this rule as they support querying the number of
  203. * successfully written sectors.
  204. */
  205. if (rq_data_dir(req) != READ &&
  206. !(card->host->caps & MMC_CAP_MULTIWRITE) &&
  207. !mmc_card_sd(card))
  208. brq.data.blocks = 1;
  209. if (brq.data.blocks > 1) {
  210. brq.data.flags |= MMC_DATA_MULTI;
  211. brq.mrq.stop = &brq.stop;
  212. readcmd = MMC_READ_MULTIPLE_BLOCK;
  213. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  214. } else {
  215. brq.mrq.stop = NULL;
  216. readcmd = MMC_READ_SINGLE_BLOCK;
  217. writecmd = MMC_WRITE_BLOCK;
  218. }
  219. if (rq_data_dir(req) == READ) {
  220. brq.cmd.opcode = readcmd;
  221. brq.data.flags |= MMC_DATA_READ;
  222. } else {
  223. brq.cmd.opcode = writecmd;
  224. brq.data.flags |= MMC_DATA_WRITE;
  225. }
  226. brq.data.sg = mq->sg;
  227. brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
  228. mmc_wait_for_req(card->host, &brq.mrq);
  229. if (brq.cmd.error) {
  230. printk(KERN_ERR "%s: error %d sending read/write command\n",
  231. req->rq_disk->disk_name, brq.cmd.error);
  232. goto cmd_err;
  233. }
  234. if (brq.data.error) {
  235. printk(KERN_ERR "%s: error %d transferring data\n",
  236. req->rq_disk->disk_name, brq.data.error);
  237. goto cmd_err;
  238. }
  239. if (brq.stop.error) {
  240. printk(KERN_ERR "%s: error %d sending stop command\n",
  241. req->rq_disk->disk_name, brq.stop.error);
  242. goto cmd_err;
  243. }
  244. if (rq_data_dir(req) != READ) {
  245. do {
  246. int err;
  247. cmd.opcode = MMC_SEND_STATUS;
  248. cmd.arg = card->rca << 16;
  249. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  250. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  251. if (err) {
  252. printk(KERN_ERR "%s: error %d requesting status\n",
  253. req->rq_disk->disk_name, err);
  254. goto cmd_err;
  255. }
  256. } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
  257. #if 0
  258. if (cmd.resp[0] & ~0x00000900)
  259. printk(KERN_ERR "%s: status = %08x\n",
  260. req->rq_disk->disk_name, cmd.resp[0]);
  261. if (mmc_decode_status(cmd.resp))
  262. goto cmd_err;
  263. #endif
  264. }
  265. /*
  266. * A block was successfully transferred.
  267. */
  268. spin_lock_irq(&md->lock);
  269. ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
  270. if (!ret) {
  271. /*
  272. * The whole request completed successfully.
  273. */
  274. add_disk_randomness(req->rq_disk);
  275. blkdev_dequeue_request(req);
  276. end_that_request_last(req, 1);
  277. }
  278. spin_unlock_irq(&md->lock);
  279. } while (ret);
  280. mmc_card_release_host(card);
  281. return 1;
  282. cmd_err:
  283. ret = 1;
  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. spin_lock_irq(&md->lock);
  315. while (ret) {
  316. ret = end_that_request_chunk(req, 0,
  317. req->current_nr_sectors << 9);
  318. }
  319. add_disk_randomness(req->rq_disk);
  320. blkdev_dequeue_request(req);
  321. end_that_request_last(req, 0);
  322. spin_unlock_irq(&md->lock);
  323. return 0;
  324. }
  325. #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
  326. static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
  327. static inline int mmc_blk_readonly(struct mmc_card *card)
  328. {
  329. return mmc_card_readonly(card) ||
  330. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  331. }
  332. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  333. {
  334. struct mmc_blk_data *md;
  335. int devidx, ret;
  336. devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
  337. if (devidx >= MMC_NUM_MINORS)
  338. return ERR_PTR(-ENOSPC);
  339. __set_bit(devidx, dev_use);
  340. md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  341. if (!md) {
  342. ret = -ENOMEM;
  343. goto out;
  344. }
  345. memset(md, 0, sizeof(struct mmc_blk_data));
  346. /*
  347. * Set the read-only status based on the supported commands
  348. * and the write protect switch.
  349. */
  350. md->read_only = mmc_blk_readonly(card);
  351. /*
  352. * Both SD and MMC specifications state (although a bit
  353. * unclearly in the MMC case) that a block size of 512
  354. * bytes must always be supported by the card.
  355. */
  356. md->block_bits = 9;
  357. md->disk = alloc_disk(1 << MMC_SHIFT);
  358. if (md->disk == NULL) {
  359. ret = -ENOMEM;
  360. goto err_kfree;
  361. }
  362. spin_lock_init(&md->lock);
  363. md->usage = 1;
  364. ret = mmc_init_queue(&md->queue, card, &md->lock);
  365. if (ret)
  366. goto err_putdisk;
  367. md->queue.prep_fn = mmc_blk_prep_rq;
  368. md->queue.issue_fn = mmc_blk_issue_rq;
  369. md->queue.data = md;
  370. md->disk->major = major;
  371. md->disk->first_minor = devidx << MMC_SHIFT;
  372. md->disk->fops = &mmc_bdops;
  373. md->disk->private_data = md;
  374. md->disk->queue = md->queue.queue;
  375. md->disk->driverfs_dev = &card->dev;
  376. /*
  377. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  378. *
  379. * - be set for removable media with permanent block devices
  380. * - be unset for removable block devices with permanent media
  381. *
  382. * Since MMC block devices clearly fall under the second
  383. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  384. * should use the block device creation/destruction hotplug
  385. * messages to tell when the card is present.
  386. */
  387. sprintf(md->disk->disk_name, "mmcblk%d", devidx);
  388. blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
  389. /*
  390. * The CSD capacity field is in units of read_blkbits.
  391. * set_capacity takes units of 512 bytes.
  392. */
  393. set_capacity(md->disk, card->csd.capacity << (card->csd.read_blkbits - 9));
  394. return md;
  395. err_putdisk:
  396. put_disk(md->disk);
  397. err_kfree:
  398. kfree(md);
  399. out:
  400. return ERR_PTR(ret);
  401. }
  402. static int
  403. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  404. {
  405. struct mmc_command cmd;
  406. int err;
  407. mmc_card_claim_host(card);
  408. cmd.opcode = MMC_SET_BLOCKLEN;
  409. cmd.arg = 1 << md->block_bits;
  410. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  411. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  412. mmc_card_release_host(card);
  413. if (err) {
  414. printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
  415. md->disk->disk_name, cmd.arg, err);
  416. return -EINVAL;
  417. }
  418. return 0;
  419. }
  420. static int mmc_blk_probe(struct mmc_card *card)
  421. {
  422. struct mmc_blk_data *md;
  423. int err;
  424. /*
  425. * Check that the card supports the command class(es) we need.
  426. */
  427. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  428. return -ENODEV;
  429. md = mmc_blk_alloc(card);
  430. if (IS_ERR(md))
  431. return PTR_ERR(md);
  432. err = mmc_blk_set_blksize(md, card);
  433. if (err)
  434. goto out;
  435. printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
  436. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  437. (unsigned long long)(get_capacity(md->disk) >> 1),
  438. md->read_only ? "(ro)" : "");
  439. mmc_set_drvdata(card, md);
  440. add_disk(md->disk);
  441. return 0;
  442. out:
  443. mmc_blk_put(md);
  444. return err;
  445. }
  446. static void mmc_blk_remove(struct mmc_card *card)
  447. {
  448. struct mmc_blk_data *md = mmc_get_drvdata(card);
  449. if (md) {
  450. int devidx;
  451. del_gendisk(md->disk);
  452. /*
  453. * I think this is needed.
  454. */
  455. md->disk->queue = NULL;
  456. devidx = md->disk->first_minor >> MMC_SHIFT;
  457. __clear_bit(devidx, dev_use);
  458. mmc_blk_put(md);
  459. }
  460. mmc_set_drvdata(card, NULL);
  461. }
  462. #ifdef CONFIG_PM
  463. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  464. {
  465. struct mmc_blk_data *md = mmc_get_drvdata(card);
  466. if (md) {
  467. mmc_queue_suspend(&md->queue);
  468. }
  469. return 0;
  470. }
  471. static int mmc_blk_resume(struct mmc_card *card)
  472. {
  473. struct mmc_blk_data *md = mmc_get_drvdata(card);
  474. if (md) {
  475. mmc_blk_set_blksize(md, card);
  476. mmc_queue_resume(&md->queue);
  477. }
  478. return 0;
  479. }
  480. #else
  481. #define mmc_blk_suspend NULL
  482. #define mmc_blk_resume NULL
  483. #endif
  484. static struct mmc_driver mmc_driver = {
  485. .drv = {
  486. .name = "mmcblk",
  487. },
  488. .probe = mmc_blk_probe,
  489. .remove = mmc_blk_remove,
  490. .suspend = mmc_blk_suspend,
  491. .resume = mmc_blk_resume,
  492. };
  493. static int __init mmc_blk_init(void)
  494. {
  495. int res = -ENOMEM;
  496. res = register_blkdev(major, "mmc");
  497. if (res < 0) {
  498. printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
  499. major, res);
  500. goto out;
  501. }
  502. if (major == 0)
  503. major = res;
  504. return mmc_register_driver(&mmc_driver);
  505. out:
  506. return res;
  507. }
  508. static void __exit mmc_blk_exit(void)
  509. {
  510. mmc_unregister_driver(&mmc_driver);
  511. unregister_blkdev(major, "mmc");
  512. }
  513. module_init(mmc_blk_init);
  514. module_exit(mmc_blk_exit);
  515. MODULE_LICENSE("GPL");
  516. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
  517. module_param(major, int, 0444);
  518. MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");