mmc_block.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  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.stop.opcode = MMC_STOP_TRANSMISSION;
  196. brq.stop.arg = 0;
  197. brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
  198. brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
  199. if (brq.data.blocks > card->host->max_blk_count)
  200. brq.data.blocks = card->host->max_blk_count;
  201. mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
  202. /*
  203. * If the host doesn't support multiple block writes, force
  204. * block writes to single block. SD cards are excepted from
  205. * this rule as they support querying the number of
  206. * successfully written sectors.
  207. */
  208. if (rq_data_dir(req) != READ &&
  209. !(card->host->caps & MMC_CAP_MULTIWRITE) &&
  210. !mmc_card_sd(card))
  211. brq.data.blocks = 1;
  212. if (brq.data.blocks > 1) {
  213. brq.data.flags |= MMC_DATA_MULTI;
  214. brq.mrq.stop = &brq.stop;
  215. readcmd = MMC_READ_MULTIPLE_BLOCK;
  216. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  217. } else {
  218. brq.mrq.stop = NULL;
  219. readcmd = MMC_READ_SINGLE_BLOCK;
  220. writecmd = MMC_WRITE_BLOCK;
  221. }
  222. if (rq_data_dir(req) == READ) {
  223. brq.cmd.opcode = readcmd;
  224. brq.data.flags |= MMC_DATA_READ;
  225. } else {
  226. brq.cmd.opcode = writecmd;
  227. brq.data.flags |= MMC_DATA_WRITE;
  228. }
  229. brq.data.sg = mq->sg;
  230. brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
  231. mmc_wait_for_req(card->host, &brq.mrq);
  232. if (brq.cmd.error) {
  233. printk(KERN_ERR "%s: error %d sending read/write command\n",
  234. req->rq_disk->disk_name, brq.cmd.error);
  235. goto cmd_err;
  236. }
  237. if (brq.data.error) {
  238. printk(KERN_ERR "%s: error %d transferring data\n",
  239. req->rq_disk->disk_name, brq.data.error);
  240. goto cmd_err;
  241. }
  242. if (brq.stop.error) {
  243. printk(KERN_ERR "%s: error %d sending stop command\n",
  244. req->rq_disk->disk_name, brq.stop.error);
  245. goto cmd_err;
  246. }
  247. if (rq_data_dir(req) != READ) {
  248. do {
  249. int err;
  250. cmd.opcode = MMC_SEND_STATUS;
  251. cmd.arg = card->rca << 16;
  252. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  253. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  254. if (err) {
  255. printk(KERN_ERR "%s: error %d requesting status\n",
  256. req->rq_disk->disk_name, err);
  257. goto cmd_err;
  258. }
  259. } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
  260. #if 0
  261. if (cmd.resp[0] & ~0x00000900)
  262. printk(KERN_ERR "%s: status = %08x\n",
  263. req->rq_disk->disk_name, cmd.resp[0]);
  264. if (mmc_decode_status(cmd.resp))
  265. goto cmd_err;
  266. #endif
  267. }
  268. /*
  269. * A block was successfully transferred.
  270. */
  271. spin_lock_irq(&md->lock);
  272. ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
  273. if (!ret) {
  274. /*
  275. * The whole request completed successfully.
  276. */
  277. add_disk_randomness(req->rq_disk);
  278. blkdev_dequeue_request(req);
  279. end_that_request_last(req, 1);
  280. }
  281. spin_unlock_irq(&md->lock);
  282. } while (ret);
  283. mmc_card_release_host(card);
  284. return 1;
  285. cmd_err:
  286. /*
  287. * If this is an SD card and we're writing, we can first
  288. * mark the known good sectors as ok.
  289. *
  290. * If the card is not SD, we can still ok written sectors
  291. * if the controller can do proper error reporting.
  292. *
  293. * For reads we just fail the entire chunk as that should
  294. * be safe in all cases.
  295. */
  296. if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
  297. u32 blocks;
  298. unsigned int bytes;
  299. blocks = mmc_sd_num_wr_blocks(card);
  300. if (blocks != (u32)-1) {
  301. if (card->csd.write_partial)
  302. bytes = blocks << md->block_bits;
  303. else
  304. bytes = blocks << 9;
  305. spin_lock_irq(&md->lock);
  306. ret = end_that_request_chunk(req, 1, bytes);
  307. spin_unlock_irq(&md->lock);
  308. }
  309. } else if (rq_data_dir(req) != READ &&
  310. (card->host->caps & MMC_CAP_MULTIWRITE)) {
  311. spin_lock_irq(&md->lock);
  312. ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
  313. spin_unlock_irq(&md->lock);
  314. }
  315. flush_queue:
  316. mmc_card_release_host(card);
  317. spin_lock_irq(&md->lock);
  318. while (ret) {
  319. ret = end_that_request_chunk(req, 0,
  320. req->current_nr_sectors << 9);
  321. }
  322. add_disk_randomness(req->rq_disk);
  323. blkdev_dequeue_request(req);
  324. end_that_request_last(req, 0);
  325. spin_unlock_irq(&md->lock);
  326. return 0;
  327. }
  328. #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
  329. static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
  330. static inline int mmc_blk_readonly(struct mmc_card *card)
  331. {
  332. return mmc_card_readonly(card) ||
  333. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  334. }
  335. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  336. {
  337. struct mmc_blk_data *md;
  338. int devidx, ret;
  339. devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
  340. if (devidx >= MMC_NUM_MINORS)
  341. return ERR_PTR(-ENOSPC);
  342. __set_bit(devidx, dev_use);
  343. md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  344. if (!md) {
  345. ret = -ENOMEM;
  346. goto out;
  347. }
  348. memset(md, 0, sizeof(struct mmc_blk_data));
  349. /*
  350. * Set the read-only status based on the supported commands
  351. * and the write protect switch.
  352. */
  353. md->read_only = mmc_blk_readonly(card);
  354. /*
  355. * Both SD and MMC specifications state (although a bit
  356. * unclearly in the MMC case) that a block size of 512
  357. * bytes must always be supported by the card.
  358. */
  359. md->block_bits = 9;
  360. md->disk = alloc_disk(1 << MMC_SHIFT);
  361. if (md->disk == NULL) {
  362. ret = -ENOMEM;
  363. goto err_kfree;
  364. }
  365. spin_lock_init(&md->lock);
  366. md->usage = 1;
  367. ret = mmc_init_queue(&md->queue, card, &md->lock);
  368. if (ret)
  369. goto err_putdisk;
  370. md->queue.prep_fn = mmc_blk_prep_rq;
  371. md->queue.issue_fn = mmc_blk_issue_rq;
  372. md->queue.data = md;
  373. md->disk->major = major;
  374. md->disk->first_minor = devidx << MMC_SHIFT;
  375. md->disk->fops = &mmc_bdops;
  376. md->disk->private_data = md;
  377. md->disk->queue = md->queue.queue;
  378. md->disk->driverfs_dev = &card->dev;
  379. /*
  380. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  381. *
  382. * - be set for removable media with permanent block devices
  383. * - be unset for removable block devices with permanent media
  384. *
  385. * Since MMC block devices clearly fall under the second
  386. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  387. * should use the block device creation/destruction hotplug
  388. * messages to tell when the card is present.
  389. */
  390. sprintf(md->disk->disk_name, "mmcblk%d", devidx);
  391. blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
  392. /*
  393. * The CSD capacity field is in units of read_blkbits.
  394. * set_capacity takes units of 512 bytes.
  395. */
  396. set_capacity(md->disk, card->csd.capacity << (card->csd.read_blkbits - 9));
  397. return md;
  398. err_putdisk:
  399. put_disk(md->disk);
  400. err_kfree:
  401. kfree(md);
  402. out:
  403. return ERR_PTR(ret);
  404. }
  405. static int
  406. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  407. {
  408. struct mmc_command cmd;
  409. int err;
  410. /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
  411. if (mmc_card_blockaddr(card))
  412. return 0;
  413. mmc_card_claim_host(card);
  414. cmd.opcode = MMC_SET_BLOCKLEN;
  415. cmd.arg = 1 << md->block_bits;
  416. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  417. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  418. mmc_card_release_host(card);
  419. if (err) {
  420. printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
  421. md->disk->disk_name, cmd.arg, err);
  422. return -EINVAL;
  423. }
  424. return 0;
  425. }
  426. static int mmc_blk_probe(struct mmc_card *card)
  427. {
  428. struct mmc_blk_data *md;
  429. int err;
  430. /*
  431. * Check that the card supports the command class(es) we need.
  432. */
  433. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  434. return -ENODEV;
  435. md = mmc_blk_alloc(card);
  436. if (IS_ERR(md))
  437. return PTR_ERR(md);
  438. err = mmc_blk_set_blksize(md, card);
  439. if (err)
  440. goto out;
  441. printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
  442. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  443. (unsigned long long)(get_capacity(md->disk) >> 1),
  444. md->read_only ? "(ro)" : "");
  445. mmc_set_drvdata(card, md);
  446. add_disk(md->disk);
  447. return 0;
  448. out:
  449. mmc_blk_put(md);
  450. return err;
  451. }
  452. static void mmc_blk_remove(struct mmc_card *card)
  453. {
  454. struct mmc_blk_data *md = mmc_get_drvdata(card);
  455. if (md) {
  456. int devidx;
  457. /* Stop new requests from getting into the queue */
  458. del_gendisk(md->disk);
  459. /* Then flush out any already in there */
  460. mmc_cleanup_queue(&md->queue);
  461. devidx = md->disk->first_minor >> MMC_SHIFT;
  462. __clear_bit(devidx, dev_use);
  463. mmc_blk_put(md);
  464. }
  465. mmc_set_drvdata(card, NULL);
  466. }
  467. #ifdef CONFIG_PM
  468. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  469. {
  470. struct mmc_blk_data *md = mmc_get_drvdata(card);
  471. if (md) {
  472. mmc_queue_suspend(&md->queue);
  473. }
  474. return 0;
  475. }
  476. static int mmc_blk_resume(struct mmc_card *card)
  477. {
  478. struct mmc_blk_data *md = mmc_get_drvdata(card);
  479. if (md) {
  480. mmc_blk_set_blksize(md, card);
  481. mmc_queue_resume(&md->queue);
  482. }
  483. return 0;
  484. }
  485. #else
  486. #define mmc_blk_suspend NULL
  487. #define mmc_blk_resume NULL
  488. #endif
  489. static struct mmc_driver mmc_driver = {
  490. .drv = {
  491. .name = "mmcblk",
  492. },
  493. .probe = mmc_blk_probe,
  494. .remove = mmc_blk_remove,
  495. .suspend = mmc_blk_suspend,
  496. .resume = mmc_blk_resume,
  497. };
  498. static int __init mmc_blk_init(void)
  499. {
  500. int res = -ENOMEM;
  501. res = register_blkdev(major, "mmc");
  502. if (res < 0) {
  503. printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
  504. major, res);
  505. goto out;
  506. }
  507. if (major == 0)
  508. major = res;
  509. return mmc_register_driver(&mmc_driver);
  510. out:
  511. return res;
  512. }
  513. static void __exit mmc_blk_exit(void)
  514. {
  515. mmc_unregister_driver(&mmc_driver);
  516. unregister_blkdev(major, "mmc");
  517. }
  518. module_init(mmc_blk_init);
  519. module_exit(mmc_blk_exit);
  520. MODULE_LICENSE("GPL");
  521. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
  522. module_param(major, int, 0444);
  523. MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");