mmc_block.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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/mmc/card.h>
  31. #include <linux/mmc/host.h>
  32. #include <linux/mmc/protocol.h>
  33. #include <asm/system.h>
  34. #include <asm/uaccess.h>
  35. #include "mmc_queue.h"
  36. /*
  37. * max 8 partitions per card
  38. */
  39. #define MMC_SHIFT 3
  40. static int major;
  41. /*
  42. * There is one mmc_blk_data per slot.
  43. */
  44. struct mmc_blk_data {
  45. spinlock_t lock;
  46. struct gendisk *disk;
  47. struct mmc_queue queue;
  48. unsigned int usage;
  49. unsigned int block_bits;
  50. unsigned int read_only;
  51. };
  52. static DEFINE_MUTEX(open_lock);
  53. static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
  54. {
  55. struct mmc_blk_data *md;
  56. mutex_lock(&open_lock);
  57. md = disk->private_data;
  58. if (md && md->usage == 0)
  59. md = NULL;
  60. if (md)
  61. md->usage++;
  62. mutex_unlock(&open_lock);
  63. return md;
  64. }
  65. static void mmc_blk_put(struct mmc_blk_data *md)
  66. {
  67. mutex_lock(&open_lock);
  68. md->usage--;
  69. if (md->usage == 0) {
  70. put_disk(md->disk);
  71. mmc_cleanup_queue(&md->queue);
  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 int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
  131. {
  132. struct mmc_blk_data *md = mq->data;
  133. struct mmc_card *card = md->queue.card;
  134. int ret;
  135. if (mmc_card_claim_host(card))
  136. goto cmd_err;
  137. do {
  138. struct mmc_blk_request brq;
  139. struct mmc_command cmd;
  140. memset(&brq, 0, sizeof(struct mmc_blk_request));
  141. brq.mrq.cmd = &brq.cmd;
  142. brq.mrq.data = &brq.data;
  143. brq.cmd.arg = req->sector << 9;
  144. brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
  145. brq.data.blksz_bits = md->block_bits;
  146. brq.data.blksz = 1 << md->block_bits;
  147. brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
  148. brq.stop.opcode = MMC_STOP_TRANSMISSION;
  149. brq.stop.arg = 0;
  150. brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
  151. mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
  152. if (rq_data_dir(req) == READ) {
  153. brq.cmd.opcode = brq.data.blocks > 1 ? MMC_READ_MULTIPLE_BLOCK : MMC_READ_SINGLE_BLOCK;
  154. brq.data.flags |= MMC_DATA_READ;
  155. } else {
  156. brq.cmd.opcode = MMC_WRITE_BLOCK;
  157. brq.data.flags |= MMC_DATA_WRITE;
  158. brq.data.blocks = 1;
  159. }
  160. if (brq.data.blocks > 1) {
  161. brq.data.flags |= MMC_DATA_MULTI;
  162. brq.mrq.stop = &brq.stop;
  163. } else {
  164. brq.mrq.stop = NULL;
  165. }
  166. brq.data.sg = mq->sg;
  167. brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
  168. mmc_wait_for_req(card->host, &brq.mrq);
  169. if (brq.cmd.error) {
  170. printk(KERN_ERR "%s: error %d sending read/write command\n",
  171. req->rq_disk->disk_name, brq.cmd.error);
  172. goto cmd_err;
  173. }
  174. if (brq.data.error) {
  175. printk(KERN_ERR "%s: error %d transferring data\n",
  176. req->rq_disk->disk_name, brq.data.error);
  177. goto cmd_err;
  178. }
  179. if (brq.stop.error) {
  180. printk(KERN_ERR "%s: error %d sending stop command\n",
  181. req->rq_disk->disk_name, brq.stop.error);
  182. goto cmd_err;
  183. }
  184. do {
  185. int err;
  186. cmd.opcode = MMC_SEND_STATUS;
  187. cmd.arg = card->rca << 16;
  188. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  189. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  190. if (err) {
  191. printk(KERN_ERR "%s: error %d requesting status\n",
  192. req->rq_disk->disk_name, err);
  193. goto cmd_err;
  194. }
  195. } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
  196. #if 0
  197. if (cmd.resp[0] & ~0x00000900)
  198. printk(KERN_ERR "%s: status = %08x\n",
  199. req->rq_disk->disk_name, cmd.resp[0]);
  200. if (mmc_decode_status(cmd.resp))
  201. goto cmd_err;
  202. #endif
  203. /*
  204. * A block was successfully transferred.
  205. */
  206. spin_lock_irq(&md->lock);
  207. ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
  208. if (!ret) {
  209. /*
  210. * The whole request completed successfully.
  211. */
  212. add_disk_randomness(req->rq_disk);
  213. blkdev_dequeue_request(req);
  214. end_that_request_last(req, 1);
  215. }
  216. spin_unlock_irq(&md->lock);
  217. } while (ret);
  218. mmc_card_release_host(card);
  219. return 1;
  220. cmd_err:
  221. mmc_card_release_host(card);
  222. /*
  223. * This is a little draconian, but until we get proper
  224. * error handling sorted out here, its the best we can
  225. * do - especially as some hosts have no idea how much
  226. * data was transferred before the error occurred.
  227. */
  228. spin_lock_irq(&md->lock);
  229. do {
  230. ret = end_that_request_chunk(req, 0,
  231. req->current_nr_sectors << 9);
  232. } while (ret);
  233. add_disk_randomness(req->rq_disk);
  234. blkdev_dequeue_request(req);
  235. end_that_request_last(req, 0);
  236. spin_unlock_irq(&md->lock);
  237. return 0;
  238. }
  239. #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
  240. static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
  241. static inline int mmc_blk_readonly(struct mmc_card *card)
  242. {
  243. return mmc_card_readonly(card) ||
  244. !(card->csd.cmdclass & CCC_BLOCK_WRITE);
  245. }
  246. static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
  247. {
  248. struct mmc_blk_data *md;
  249. int devidx, ret;
  250. devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
  251. if (devidx >= MMC_NUM_MINORS)
  252. return ERR_PTR(-ENOSPC);
  253. __set_bit(devidx, dev_use);
  254. md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
  255. if (!md) {
  256. ret = -ENOMEM;
  257. goto out;
  258. }
  259. memset(md, 0, sizeof(struct mmc_blk_data));
  260. /*
  261. * Set the read-only status based on the supported commands
  262. * and the write protect switch.
  263. */
  264. md->read_only = mmc_blk_readonly(card);
  265. /*
  266. * Both SD and MMC specifications state (although a bit
  267. * unclearly in the MMC case) that a block size of 512
  268. * bytes must always be supported by the card.
  269. */
  270. md->block_bits = 9;
  271. md->disk = alloc_disk(1 << MMC_SHIFT);
  272. if (md->disk == NULL) {
  273. ret = -ENOMEM;
  274. goto err_kfree;
  275. }
  276. spin_lock_init(&md->lock);
  277. md->usage = 1;
  278. ret = mmc_init_queue(&md->queue, card, &md->lock);
  279. if (ret)
  280. goto err_putdisk;
  281. md->queue.prep_fn = mmc_blk_prep_rq;
  282. md->queue.issue_fn = mmc_blk_issue_rq;
  283. md->queue.data = md;
  284. md->disk->major = major;
  285. md->disk->first_minor = devidx << MMC_SHIFT;
  286. md->disk->fops = &mmc_bdops;
  287. md->disk->private_data = md;
  288. md->disk->queue = md->queue.queue;
  289. md->disk->driverfs_dev = &card->dev;
  290. /*
  291. * As discussed on lkml, GENHD_FL_REMOVABLE should:
  292. *
  293. * - be set for removable media with permanent block devices
  294. * - be unset for removable block devices with permanent media
  295. *
  296. * Since MMC block devices clearly fall under the second
  297. * case, we do not set GENHD_FL_REMOVABLE. Userspace
  298. * should use the block device creation/destruction hotplug
  299. * messages to tell when the card is present.
  300. */
  301. sprintf(md->disk->disk_name, "mmcblk%d", devidx);
  302. blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
  303. /*
  304. * The CSD capacity field is in units of read_blkbits.
  305. * set_capacity takes units of 512 bytes.
  306. */
  307. set_capacity(md->disk, card->csd.capacity << (card->csd.read_blkbits - 9));
  308. return md;
  309. err_putdisk:
  310. put_disk(md->disk);
  311. err_kfree:
  312. kfree(md);
  313. out:
  314. return ERR_PTR(ret);
  315. }
  316. static int
  317. mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
  318. {
  319. struct mmc_command cmd;
  320. int err;
  321. mmc_card_claim_host(card);
  322. cmd.opcode = MMC_SET_BLOCKLEN;
  323. cmd.arg = 1 << md->block_bits;
  324. cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
  325. err = mmc_wait_for_cmd(card->host, &cmd, 5);
  326. mmc_card_release_host(card);
  327. if (err) {
  328. printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
  329. md->disk->disk_name, cmd.arg, err);
  330. return -EINVAL;
  331. }
  332. return 0;
  333. }
  334. static int mmc_blk_probe(struct mmc_card *card)
  335. {
  336. struct mmc_blk_data *md;
  337. int err;
  338. /*
  339. * Check that the card supports the command class(es) we need.
  340. */
  341. if (!(card->csd.cmdclass & CCC_BLOCK_READ))
  342. return -ENODEV;
  343. md = mmc_blk_alloc(card);
  344. if (IS_ERR(md))
  345. return PTR_ERR(md);
  346. err = mmc_blk_set_blksize(md, card);
  347. if (err)
  348. goto out;
  349. printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
  350. md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
  351. (unsigned long long)(get_capacity(md->disk) >> 1),
  352. md->read_only ? "(ro)" : "");
  353. mmc_set_drvdata(card, md);
  354. add_disk(md->disk);
  355. return 0;
  356. out:
  357. mmc_blk_put(md);
  358. return err;
  359. }
  360. static void mmc_blk_remove(struct mmc_card *card)
  361. {
  362. struct mmc_blk_data *md = mmc_get_drvdata(card);
  363. if (md) {
  364. int devidx;
  365. del_gendisk(md->disk);
  366. /*
  367. * I think this is needed.
  368. */
  369. md->disk->queue = NULL;
  370. devidx = md->disk->first_minor >> MMC_SHIFT;
  371. __clear_bit(devidx, dev_use);
  372. mmc_blk_put(md);
  373. }
  374. mmc_set_drvdata(card, NULL);
  375. }
  376. #ifdef CONFIG_PM
  377. static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
  378. {
  379. struct mmc_blk_data *md = mmc_get_drvdata(card);
  380. if (md) {
  381. mmc_queue_suspend(&md->queue);
  382. }
  383. return 0;
  384. }
  385. static int mmc_blk_resume(struct mmc_card *card)
  386. {
  387. struct mmc_blk_data *md = mmc_get_drvdata(card);
  388. if (md) {
  389. mmc_blk_set_blksize(md, card);
  390. mmc_queue_resume(&md->queue);
  391. }
  392. return 0;
  393. }
  394. #else
  395. #define mmc_blk_suspend NULL
  396. #define mmc_blk_resume NULL
  397. #endif
  398. static struct mmc_driver mmc_driver = {
  399. .drv = {
  400. .name = "mmcblk",
  401. },
  402. .probe = mmc_blk_probe,
  403. .remove = mmc_blk_remove,
  404. .suspend = mmc_blk_suspend,
  405. .resume = mmc_blk_resume,
  406. };
  407. static int __init mmc_blk_init(void)
  408. {
  409. int res = -ENOMEM;
  410. res = register_blkdev(major, "mmc");
  411. if (res < 0) {
  412. printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
  413. major, res);
  414. goto out;
  415. }
  416. if (major == 0)
  417. major = res;
  418. return mmc_register_driver(&mmc_driver);
  419. out:
  420. return res;
  421. }
  422. static void __exit mmc_blk_exit(void)
  423. {
  424. mmc_unregister_driver(&mmc_driver);
  425. unregister_blkdev(major, "mmc");
  426. }
  427. module_init(mmc_blk_init);
  428. module_exit(mmc_blk_exit);
  429. MODULE_LICENSE("GPL");
  430. MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
  431. module_param(major, int, 0444);
  432. MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");