block.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. /*
  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 u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
  117. {
  118. int err;
  119. u32 blocks;
  120. struct mmc_request mrq;
  121. struct mmc_command cmd;
  122. struct mmc_data data;
  123. unsigned int timeout_us;
  124. struct scatterlist sg;
  125. memset(&cmd, 0, sizeof(struct mmc_command));
  126. cmd.opcode = MMC_APP_CMD;
  127. cmd.arg = card->rca << 16;
  128. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  129. err = mmc_wait_for_cmd(card->host, &cmd, 0);
  130. if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
  131. return (u32)-1;
  132. memset(&cmd, 0, sizeof(struct mmc_command));
  133. cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
  134. cmd.arg = 0;
  135. cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  136. memset(&data, 0, sizeof(struct mmc_data));
  137. data.timeout_ns = card->csd.tacc_ns * 100;
  138. data.timeout_clks = card->csd.tacc_clks * 100;
  139. timeout_us = data.timeout_ns / 1000;
  140. timeout_us += data.timeout_clks * 1000 /
  141. (card->host->ios.clock / 1000);
  142. if (timeout_us > 100000) {
  143. data.timeout_ns = 100000000;
  144. data.timeout_clks = 0;
  145. }
  146. data.blksz = 4;
  147. data.blocks = 1;
  148. data.flags = MMC_DATA_READ;
  149. data.sg = &sg;
  150. data.sg_len = 1;
  151. memset(&mrq, 0, sizeof(struct mmc_request));
  152. mrq.cmd = &cmd;
  153. mrq.data = &data;
  154. sg_init_one(&sg, &blocks, 4);
  155. mmc_wait_for_req(card->host, &mrq);
  156. if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
  157. return (u32)-1;
  158. blocks = ntohl(blocks);
  159. return blocks;
  160. }
  161. static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  162. {
  163. struct mmc_blk_data *md = mq->data;
  164. struct mmc_card *card = md->queue.card;
  165. struct mmc_blk_request brq;
  166. int ret = 1, sg_pos, data_size;
  167. mmc_claim_host(card->host);
  168. do {
  169. struct mmc_command cmd;
  170. u32 readcmd, writecmd;
  171. memset(&brq, 0, sizeof(struct mmc_blk_request));
  172. brq.mrq.cmd = &brq.cmd;
  173. brq.mrq.data = &brq.data;
  174. brq.cmd.arg = req->sector;
  175. if (!mmc_card_blockaddr(card))
  176. brq.cmd.arg <<= 9;
  177. brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  178. brq.data.blksz = 1 << md->block_bits;
  179. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  180. brq.stop.arg = 0;
  181. brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
  182. brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
  183. if (brq.data.blocks > card->host->max_blk_count)
  184. brq.data.blocks = card->host->max_blk_count;
  185. mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
  186. /*
  187. * If the host doesn't support multiple block writes, force
  188. * block writes to single block. SD cards are excepted from
  189. * this rule as they support querying the number of
  190. * successfully written sectors.
  191. */
  192. if (rq_data_dir(req) != READ &&
  193. !(card->host->caps & MMC_CAP_MULTIWRITE) &&
  194. !mmc_card_sd(card))
  195. brq.data.blocks = 1;
  196. if (brq.data.blocks > 1) {
  197. brq.data.flags |= MMC_DATA_MULTI;
  198. brq.mrq.stop = &brq.stop;
  199. readcmd = MMC_READ_MULTIPLE_BLOCK;
  200. writecmd = MMC_WRITE_MULTIPLE_BLOCK;
  201. } else {
  202. brq.mrq.stop = NULL;
  203. readcmd = MMC_READ_SINGLE_BLOCK;
  204. writecmd = MMC_WRITE_BLOCK;
  205. }
  206. if (rq_data_dir(req) == READ) {
  207. brq.cmd.opcode = readcmd;
  208. brq.data.flags |= MMC_DATA_READ;
  209. } else {
  210. brq.cmd.opcode = writecmd;
  211. brq.data.flags |= MMC_DATA_WRITE;
  212. }
  213. brq.data.sg = mq->sg;
  214. brq.data.sg_len = mmc_queue_map_sg(mq);
  215. mmc_queue_bounce_pre(mq);
  216. if (brq.data.blocks !=
  217. (req->nr_sectors >> (md->block_bits - 9))) {
  218. data_size = brq.data.blocks * brq.data.blksz;
  219. for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
  220. data_size -= mq->sg[sg_pos].length;
  221. if (data_size <= 0) {
  222. mq->sg[sg_pos].length += data_size;
  223. sg_pos++;
  224. break;
  225. }
  226. }
  227. brq.data.sg_len = sg_pos;
  228. }
  229. mmc_wait_for_req(card->host, &brq.mrq);
  230. mmc_queue_bounce_post(mq);
  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_release_host(card->host);
  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. mmc_release_host(card->host);
  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 = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  342. if (!md) {
  343. ret = -ENOMEM;
  344. goto out;
  345. }
  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.issue_fn = mmc_blk_issue_rq;
  368. md->queue.data = md;
  369. md->disk->major = MMC_BLOCK_MAJOR;
  370. md->disk->first_minor = devidx << MMC_SHIFT;
  371. md->disk->fops = &mmc_bdops;
  372. md->disk->private_data = md;
  373. md->disk->queue = md->queue.queue;
  374. md->disk->driverfs_dev = &card->dev;
  375. /*
  376. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  377. *
  378. * - be set for removable media with permanent block devices
  379. * - be unset for removable block devices with permanent media
  380. *
  381. * Since MMC block devices clearly fall under the second
  382. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  383. * should use the block device creation/destruction hotplug
  384. * messages to tell when the card is present.
  385. */
  386. sprintf(md->disk->disk_name, "mmcblk%d", devidx);
  387. blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
  388. if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
  389. /*
  390. * The EXT_CSD sector count is in number or 512 byte
  391. * sectors.
  392. */
  393. set_capacity(md->disk, card->ext_csd.sectors);
  394. } else {
  395. /*
  396. * The CSD capacity field is in units of read_blkbits.
  397. * set_capacity takes units of 512 bytes.
  398. */
  399. set_capacity(md->disk,
  400. card->csd.capacity << (card->csd.read_blkbits - 9));
  401. }
  402. return md;
  403. err_putdisk:
  404. put_disk(md->disk);
  405. err_kfree:
  406. kfree(md);
  407. out:
  408. return ERR_PTR(ret);
  409. }
  410. static int
  411. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  412. {
  413. struct mmc_command cmd;
  414. int err;
  415. /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
  416. if (mmc_card_blockaddr(card))
  417. return 0;
  418. mmc_claim_host(card->host);
  419. cmd.opcode = MMC_SET_BLOCKLEN;
  420. cmd.arg = 1 << md->block_bits;
  421. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  422. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  423. mmc_release_host(card->host);
  424. if (err) {
  425. printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
  426. md->disk->disk_name, cmd.arg, err);
  427. return -EINVAL;
  428. }
  429. return 0;
  430. }
  431. static int mmc_blk_probe(struct mmc_card *card)
  432. {
  433. struct mmc_blk_data *md;
  434. int err;
  435. /*
  436. * Check that the card supports the command class(es) we need.
  437. */
  438. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  439. return -ENODEV;
  440. md = mmc_blk_alloc(card);
  441. if (IS_ERR(md))
  442. return PTR_ERR(md);
  443. err = mmc_blk_set_blksize(md, card);
  444. if (err)
  445. goto out;
  446. printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
  447. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  448. (unsigned long long)(get_capacity(md->disk) >> 1),
  449. md->read_only ? "(ro)" : "");
  450. mmc_set_drvdata(card, md);
  451. add_disk(md->disk);
  452. return 0;
  453. out:
  454. mmc_blk_put(md);
  455. return err;
  456. }
  457. static void mmc_blk_remove(struct mmc_card *card)
  458. {
  459. struct mmc_blk_data *md = mmc_get_drvdata(card);
  460. if (md) {
  461. int devidx;
  462. /* Stop new requests from getting into the queue */
  463. del_gendisk(md->disk);
  464. /* Then flush out any already in there */
  465. mmc_cleanup_queue(&md->queue);
  466. devidx = md->disk->first_minor >> MMC_SHIFT;
  467. __clear_bit(devidx, dev_use);
  468. mmc_blk_put(md);
  469. }
  470. mmc_set_drvdata(card, NULL);
  471. }
  472. #ifdef CONFIG_PM
  473. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  474. {
  475. struct mmc_blk_data *md = mmc_get_drvdata(card);
  476. if (md) {
  477. mmc_queue_suspend(&md->queue);
  478. }
  479. return 0;
  480. }
  481. static int mmc_blk_resume(struct mmc_card *card)
  482. {
  483. struct mmc_blk_data *md = mmc_get_drvdata(card);
  484. if (md) {
  485. mmc_blk_set_blksize(md, card);
  486. mmc_queue_resume(&md->queue);
  487. }
  488. return 0;
  489. }
  490. #else
  491. #define mmc_blk_suspend NULL
  492. #define mmc_blk_resume NULL
  493. #endif
  494. static struct mmc_driver mmc_driver = {
  495. .drv = {
  496. .name = "mmcblk",
  497. },
  498. .probe = mmc_blk_probe,
  499. .remove = mmc_blk_remove,
  500. .suspend = mmc_blk_suspend,
  501. .resume = mmc_blk_resume,
  502. };
  503. static int __init mmc_blk_init(void)
  504. {
  505. int res = -ENOMEM;
  506. res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
  507. if (res)
  508. goto out;
  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(MMC_BLOCK_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");