block.c 14 KB

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