mmc_block.c 14 KB

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