2  * Block driver for media (i.e., flash cards)
 
   4  * Copyright 2002 Hewlett-Packard Company
 
   5  * Copyright 2005-2008 Pierre Ossman
 
   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.
 
  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.
 
  15  * Many thanks to Alessandro Rubini and Jonathan Corbet!
 
  17  * Author:  Andrew Christian
 
  20 #include <linux/moduleparam.h>
 
  21 #include <linux/module.h>
 
  22 #include <linux/init.h>
 
  24 #include <linux/kernel.h>
 
  26 #include <linux/errno.h>
 
  27 #include <linux/hdreg.h>
 
  28 #include <linux/kdev_t.h>
 
  29 #include <linux/blkdev.h>
 
  30 #include <linux/mutex.h>
 
  31 #include <linux/scatterlist.h>
 
  32 #include <linux/string_helpers.h>
 
  34 #include <linux/mmc/card.h>
 
  35 #include <linux/mmc/host.h>
 
  36 #include <linux/mmc/mmc.h>
 
  37 #include <linux/mmc/sd.h>
 
  39 #include <asm/system.h>
 
  40 #include <asm/uaccess.h>
 
  45  * max 8 partitions per card
 
  48 #define MMC_NUM_MINORS  (256 >> MMC_SHIFT)
 
  50 static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
 
  53  * There is one mmc_blk_data per slot.
 
  58         struct mmc_queue queue;
 
  61         unsigned int    block_bits;
 
  62         unsigned int    read_only;
 
  65 static DEFINE_MUTEX(open_lock);
 
  67 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
 
  69         struct mmc_blk_data *md;
 
  71         mutex_lock(&open_lock);
 
  72         md = disk->private_data;
 
  73         if (md && md->usage == 0)
 
  77         mutex_unlock(&open_lock);
 
  82 static void mmc_blk_put(struct mmc_blk_data *md)
 
  84         mutex_lock(&open_lock);
 
  87                 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
 
  88                 __clear_bit(devidx, dev_use);
 
  93         mutex_unlock(&open_lock);
 
  96 static int mmc_blk_open(struct inode *inode, struct file *filp)
 
  98         struct mmc_blk_data *md;
 
 101         md = mmc_blk_get(inode->i_bdev->bd_disk);
 
 104                         check_disk_change(inode->i_bdev);
 
 107                 if ((filp->f_mode & FMODE_WRITE) && md->read_only) {
 
 116 static int mmc_blk_release(struct inode *inode, struct file *filp)
 
 118         struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
 
 125 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
 
 127         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
 
 133 static struct block_device_operations mmc_bdops = {
 
 134         .open                   = mmc_blk_open,
 
 135         .release                = mmc_blk_release,
 
 136         .getgeo                 = mmc_blk_getgeo,
 
 137         .owner                  = THIS_MODULE,
 
 140 struct mmc_blk_request {
 
 141         struct mmc_request      mrq;
 
 142         struct mmc_command      cmd;
 
 143         struct mmc_command      stop;
 
 144         struct mmc_data         data;
 
 147 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
 
 152         struct mmc_request mrq;
 
 153         struct mmc_command cmd;
 
 154         struct mmc_data data;
 
 155         unsigned int timeout_us;
 
 157         struct scatterlist sg;
 
 159         memset(&cmd, 0, sizeof(struct mmc_command));
 
 161         cmd.opcode = MMC_APP_CMD;
 
 162         cmd.arg = card->rca << 16;
 
 163         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
 
 165         err = mmc_wait_for_cmd(card->host, &cmd, 0);
 
 168         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
 
 171         memset(&cmd, 0, sizeof(struct mmc_command));
 
 173         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
 
 175         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
 
 177         memset(&data, 0, sizeof(struct mmc_data));
 
 179         data.timeout_ns = card->csd.tacc_ns * 100;
 
 180         data.timeout_clks = card->csd.tacc_clks * 100;
 
 182         timeout_us = data.timeout_ns / 1000;
 
 183         timeout_us += data.timeout_clks * 1000 /
 
 184                 (card->host->ios.clock / 1000);
 
 186         if (timeout_us > 100000) {
 
 187                 data.timeout_ns = 100000000;
 
 188                 data.timeout_clks = 0;
 
 193         data.flags = MMC_DATA_READ;
 
 197         memset(&mrq, 0, sizeof(struct mmc_request));
 
 202         sg_init_one(&sg, &blocks, 4);
 
 204         mmc_wait_for_req(card->host, &mrq);
 
 206         if (cmd.error || data.error)
 
 209         blocks = ntohl(blocks);
 
 214 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
 
 216         struct mmc_blk_data *md = mq->data;
 
 217         struct mmc_card *card = md->queue.card;
 
 218         struct mmc_blk_request brq;
 
 219         int ret = 1, data_size, i;
 
 220         struct scatterlist *sg;
 
 222         mmc_claim_host(card->host);
 
 225                 struct mmc_command cmd;
 
 226                 u32 readcmd, writecmd;
 
 228                 memset(&brq, 0, sizeof(struct mmc_blk_request));
 
 229                 brq.mrq.cmd = &brq.cmd;
 
 230                 brq.mrq.data = &brq.data;
 
 232                 brq.cmd.arg = req->sector;
 
 233                 if (!mmc_card_blockaddr(card))
 
 235                 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
 
 236                 brq.data.blksz = 1 << md->block_bits;
 
 237                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
 
 239                 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
 
 240                 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
 
 241                 if (brq.data.blocks > card->host->max_blk_count)
 
 242                         brq.data.blocks = card->host->max_blk_count;
 
 244                 if (brq.data.blocks > 1) {
 
 245                         /* SPI multiblock writes terminate using a special
 
 246                          * token, not a STOP_TRANSMISSION request.
 
 248                         if (!mmc_host_is_spi(card->host)
 
 249                                         || rq_data_dir(req) == READ)
 
 250                                 brq.mrq.stop = &brq.stop;
 
 251                         readcmd = MMC_READ_MULTIPLE_BLOCK;
 
 252                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
 
 255                         readcmd = MMC_READ_SINGLE_BLOCK;
 
 256                         writecmd = MMC_WRITE_BLOCK;
 
 259                 if (rq_data_dir(req) == READ) {
 
 260                         brq.cmd.opcode = readcmd;
 
 261                         brq.data.flags |= MMC_DATA_READ;
 
 263                         brq.cmd.opcode = writecmd;
 
 264                         brq.data.flags |= MMC_DATA_WRITE;
 
 267                 mmc_set_data_timeout(&brq.data, card);
 
 269                 brq.data.sg = mq->sg;
 
 270                 brq.data.sg_len = mmc_queue_map_sg(mq);
 
 272                 mmc_queue_bounce_pre(mq);
 
 275                  * Adjust the sg list so it is the same size as the
 
 278                 if (brq.data.blocks !=
 
 279                     (req->nr_sectors >> (md->block_bits - 9))) {
 
 280                         data_size = brq.data.blocks * brq.data.blksz;
 
 281                         for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
 
 282                                 data_size -= sg->length;
 
 283                                 if (data_size <= 0) {
 
 284                                         sg->length += data_size;
 
 292                 mmc_wait_for_req(card->host, &brq.mrq);
 
 294                 mmc_queue_bounce_post(mq);
 
 297                  * Check for errors here, but don't jump to cmd_err
 
 298                  * until later as we need to wait for the card to leave
 
 299                  * programming mode even when things go wrong.
 
 302                         printk(KERN_ERR "%s: error %d sending read/write command\n",
 
 303                                req->rq_disk->disk_name, brq.cmd.error);
 
 306                 if (brq.data.error) {
 
 307                         printk(KERN_ERR "%s: error %d transferring data\n",
 
 308                                req->rq_disk->disk_name, brq.data.error);
 
 311                 if (brq.stop.error) {
 
 312                         printk(KERN_ERR "%s: error %d sending stop command\n",
 
 313                                req->rq_disk->disk_name, brq.stop.error);
 
 316                 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
 
 320                                 cmd.opcode = MMC_SEND_STATUS;
 
 321                                 cmd.arg = card->rca << 16;
 
 322                                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
 
 323                                 err = mmc_wait_for_cmd(card->host, &cmd, 5);
 
 325                                         printk(KERN_ERR "%s: error %d requesting status\n",
 
 326                                                req->rq_disk->disk_name, err);
 
 330                                  * Some cards mishandle the status bits,
 
 331                                  * so make sure to check both the busy
 
 332                                  * indication and the card state.
 
 334                         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
 
 335                                 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
 
 338                         if (cmd.resp[0] & ~0x00000900)
 
 339                                 printk(KERN_ERR "%s: status = %08x\n",
 
 340                                        req->rq_disk->disk_name, cmd.resp[0]);
 
 341                         if (mmc_decode_status(cmd.resp))
 
 346                 if (brq.cmd.error || brq.data.error || brq.stop.error)
 
 350                  * A block was successfully transferred.
 
 352                 spin_lock_irq(&md->lock);
 
 353                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
 
 354                 spin_unlock_irq(&md->lock);
 
 357         mmc_release_host(card->host);
 
 363          * If this is an SD card and we're writing, we can first
 
 364          * mark the known good sectors as ok.
 
 366          * If the card is not SD, we can still ok written sectors
 
 367          * as reported by the controller (which might be less than
 
 368          * the real number of written sectors, but never more).
 
 370          * For reads we just fail the entire chunk as that should
 
 371          * be safe in all cases.
 
 373         if (rq_data_dir(req) != READ) {
 
 374                 if (mmc_card_sd(card)) {
 
 378                         blocks = mmc_sd_num_wr_blocks(card);
 
 379                         if (blocks != (u32)-1) {
 
 380                                 if (card->csd.write_partial)
 
 381                                         bytes = blocks << md->block_bits;
 
 384                                 spin_lock_irq(&md->lock);
 
 385                                 ret = __blk_end_request(req, 0, bytes);
 
 386                                 spin_unlock_irq(&md->lock);
 
 389                         spin_lock_irq(&md->lock);
 
 390                         ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
 
 391                         spin_unlock_irq(&md->lock);
 
 395         mmc_release_host(card->host);
 
 397         spin_lock_irq(&md->lock);
 
 399                 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
 
 400         spin_unlock_irq(&md->lock);
 
 406 static inline int mmc_blk_readonly(struct mmc_card *card)
 
 408         return mmc_card_readonly(card) ||
 
 409                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
 
 412 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
 
 414         struct mmc_blk_data *md;
 
 417         devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
 
 418         if (devidx >= MMC_NUM_MINORS)
 
 419                 return ERR_PTR(-ENOSPC);
 
 420         __set_bit(devidx, dev_use);
 
 422         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
 
 430          * Set the read-only status based on the supported commands
 
 431          * and the write protect switch.
 
 433         md->read_only = mmc_blk_readonly(card);
 
 436          * Both SD and MMC specifications state (although a bit
 
 437          * unclearly in the MMC case) that a block size of 512
 
 438          * bytes must always be supported by the card.
 
 442         md->disk = alloc_disk(1 << MMC_SHIFT);
 
 443         if (md->disk == NULL) {
 
 448         spin_lock_init(&md->lock);
 
 451         ret = mmc_init_queue(&md->queue, card, &md->lock);
 
 455         md->queue.issue_fn = mmc_blk_issue_rq;
 
 458         md->disk->major = MMC_BLOCK_MAJOR;
 
 459         md->disk->first_minor = devidx << MMC_SHIFT;
 
 460         md->disk->fops = &mmc_bdops;
 
 461         md->disk->private_data = md;
 
 462         md->disk->queue = md->queue.queue;
 
 463         md->disk->driverfs_dev = &card->dev;
 
 466          * As discussed on lkml, GENHD_FL_REMOVABLE should:
 
 468          * - be set for removable media with permanent block devices
 
 469          * - be unset for removable block devices with permanent media
 
 471          * Since MMC block devices clearly fall under the second
 
 472          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
 
 473          * should use the block device creation/destruction hotplug
 
 474          * messages to tell when the card is present.
 
 477         sprintf(md->disk->disk_name, "mmcblk%d", devidx);
 
 479         blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
 
 481         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
 
 483                  * The EXT_CSD sector count is in number or 512 byte
 
 486                 set_capacity(md->disk, card->ext_csd.sectors);
 
 489                  * The CSD capacity field is in units of read_blkbits.
 
 490                  * set_capacity takes units of 512 bytes.
 
 492                 set_capacity(md->disk,
 
 493                         card->csd.capacity << (card->csd.read_blkbits - 9));
 
 506 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
 
 508         struct mmc_command cmd;
 
 511         /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
 
 512         if (mmc_card_blockaddr(card))
 
 515         mmc_claim_host(card->host);
 
 516         cmd.opcode = MMC_SET_BLOCKLEN;
 
 517         cmd.arg = 1 << md->block_bits;
 
 518         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
 
 519         err = mmc_wait_for_cmd(card->host, &cmd, 5);
 
 520         mmc_release_host(card->host);
 
 523                 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
 
 524                         md->disk->disk_name, cmd.arg, err);
 
 531 static int mmc_blk_probe(struct mmc_card *card)
 
 533         struct mmc_blk_data *md;
 
 539          * Check that the card supports the command class(es) we need.
 
 541         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
 
 544         md = mmc_blk_alloc(card);
 
 548         err = mmc_blk_set_blksize(md, card);
 
 552         string_get_size(get_capacity(md->disk) << 9, STRING_UNITS_2,
 
 553                         cap_str, sizeof(cap_str));
 
 554         printk(KERN_INFO "%s: %s %s %s %s\n",
 
 555                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
 
 556                 cap_str, md->read_only ? "(ro)" : "");
 
 558         mmc_set_drvdata(card, md);
 
 568 static void mmc_blk_remove(struct mmc_card *card)
 
 570         struct mmc_blk_data *md = mmc_get_drvdata(card);
 
 573                 /* Stop new requests from getting into the queue */
 
 574                 del_gendisk(md->disk);
 
 576                 /* Then flush out any already in there */
 
 577                 mmc_cleanup_queue(&md->queue);
 
 581         mmc_set_drvdata(card, NULL);
 
 585 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
 
 587         struct mmc_blk_data *md = mmc_get_drvdata(card);
 
 590                 mmc_queue_suspend(&md->queue);
 
 595 static int mmc_blk_resume(struct mmc_card *card)
 
 597         struct mmc_blk_data *md = mmc_get_drvdata(card);
 
 600                 mmc_blk_set_blksize(md, card);
 
 601                 mmc_queue_resume(&md->queue);
 
 606 #define mmc_blk_suspend NULL
 
 607 #define mmc_blk_resume  NULL
 
 610 static struct mmc_driver mmc_driver = {
 
 614         .probe          = mmc_blk_probe,
 
 615         .remove         = mmc_blk_remove,
 
 616         .suspend        = mmc_blk_suspend,
 
 617         .resume         = mmc_blk_resume,
 
 620 static int __init mmc_blk_init(void)
 
 624         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
 
 628         res = mmc_register_driver(&mmc_driver);
 
 634         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
 
 639 static void __exit mmc_blk_exit(void)
 
 641         mmc_unregister_driver(&mmc_driver);
 
 642         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
 
 645 module_init(mmc_blk_init);
 
 646 module_exit(mmc_blk_exit);
 
 648 MODULE_LICENSE("GPL");
 
 649 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");