mmc_block.c 12 KB

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