block.c 15 KB

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