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/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/smp_lock.h>
33 #include <linux/scatterlist.h>
34 #include <linux/string_helpers.h>
36 #include <linux/mmc/card.h>
37 #include <linux/mmc/host.h>
38 #include <linux/mmc/mmc.h>
39 #include <linux/mmc/sd.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
46 MODULE_ALIAS("mmc:block");
49 * max 8 partitions per card
52 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
54 static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
57 * There is one mmc_blk_data per slot.
62 struct mmc_queue queue;
65 unsigned int read_only;
68 static DEFINE_MUTEX(open_lock);
70 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
72 struct mmc_blk_data *md;
74 mutex_lock(&open_lock);
75 md = disk->private_data;
76 if (md && md->usage == 0)
80 mutex_unlock(&open_lock);
85 static void mmc_blk_put(struct mmc_blk_data *md)
87 mutex_lock(&open_lock);
90 int devmaj = MAJOR(disk_devt(md->disk));
91 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
94 devidx = md->disk->first_minor >> MMC_SHIFT;
96 blk_cleanup_queue(md->queue.queue);
98 __clear_bit(devidx, dev_use);
103 mutex_unlock(&open_lock);
106 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
108 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
114 check_disk_change(bdev);
117 if ((mode & FMODE_WRITE) && md->read_only) {
127 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
129 struct mmc_blk_data *md = disk->private_data;
138 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
140 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
146 static const struct block_device_operations mmc_bdops = {
147 .open = mmc_blk_open,
148 .release = mmc_blk_release,
149 .getgeo = mmc_blk_getgeo,
150 .owner = THIS_MODULE,
153 struct mmc_blk_request {
154 struct mmc_request mrq;
155 struct mmc_command cmd;
156 struct mmc_command stop;
157 struct mmc_data data;
160 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
166 struct mmc_request mrq;
167 struct mmc_command cmd;
168 struct mmc_data data;
169 unsigned int timeout_us;
171 struct scatterlist sg;
173 memset(&cmd, 0, sizeof(struct mmc_command));
175 cmd.opcode = MMC_APP_CMD;
176 cmd.arg = card->rca << 16;
177 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
179 err = mmc_wait_for_cmd(card->host, &cmd, 0);
182 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
185 memset(&cmd, 0, sizeof(struct mmc_command));
187 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
189 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
191 memset(&data, 0, sizeof(struct mmc_data));
193 data.timeout_ns = card->csd.tacc_ns * 100;
194 data.timeout_clks = card->csd.tacc_clks * 100;
196 timeout_us = data.timeout_ns / 1000;
197 timeout_us += data.timeout_clks * 1000 /
198 (card->host->ios.clock / 1000);
200 if (timeout_us > 100000) {
201 data.timeout_ns = 100000000;
202 data.timeout_clks = 0;
207 data.flags = MMC_DATA_READ;
211 memset(&mrq, 0, sizeof(struct mmc_request));
216 blocks = kmalloc(4, GFP_KERNEL);
220 sg_init_one(&sg, blocks, 4);
222 mmc_wait_for_req(card->host, &mrq);
224 result = ntohl(*blocks);
227 if (cmd.error || data.error)
233 static u32 get_card_status(struct mmc_card *card, struct request *req)
235 struct mmc_command cmd;
238 memset(&cmd, 0, sizeof(struct mmc_command));
239 cmd.opcode = MMC_SEND_STATUS;
240 if (!mmc_host_is_spi(card->host))
241 cmd.arg = card->rca << 16;
242 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
243 err = mmc_wait_for_cmd(card->host, &cmd, 0);
245 printk(KERN_ERR "%s: error %d sending status comand",
246 req->rq_disk->disk_name, err);
250 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
252 struct mmc_blk_data *md = mq->data;
253 struct mmc_card *card = md->queue.card;
254 unsigned int from, nr, arg;
257 mmc_claim_host(card->host);
259 if (!mmc_can_erase(card)) {
264 from = blk_rq_pos(req);
265 nr = blk_rq_sectors(req);
267 if (mmc_can_trim(card))
272 err = mmc_erase(card, from, nr, arg);
274 spin_lock_irq(&md->lock);
275 __blk_end_request(req, err, blk_rq_bytes(req));
276 spin_unlock_irq(&md->lock);
278 mmc_release_host(card->host);
283 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
285 struct mmc_blk_data *md = mq->data;
286 struct mmc_card *card = md->queue.card;
287 struct mmc_blk_request brq;
288 int ret = 1, disable_multi = 0;
290 mmc_claim_host(card->host);
293 struct mmc_command cmd;
294 u32 readcmd, writecmd, status = 0;
296 memset(&brq, 0, sizeof(struct mmc_blk_request));
297 brq.mrq.cmd = &brq.cmd;
298 brq.mrq.data = &brq.data;
300 brq.cmd.arg = blk_rq_pos(req);
301 if (!mmc_card_blockaddr(card))
303 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
304 brq.data.blksz = 512;
305 brq.stop.opcode = MMC_STOP_TRANSMISSION;
307 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
308 brq.data.blocks = blk_rq_sectors(req);
311 * The block layer doesn't support all sector count
312 * restrictions, so we need to be prepared for too big
315 if (brq.data.blocks > card->host->max_blk_count)
316 brq.data.blocks = card->host->max_blk_count;
319 * After a read error, we redo the request one sector at a time
320 * in order to accurately determine which sectors can be read
323 if (disable_multi && brq.data.blocks > 1)
326 if (brq.data.blocks > 1) {
327 /* SPI multiblock writes terminate using a special
328 * token, not a STOP_TRANSMISSION request.
330 if (!mmc_host_is_spi(card->host)
331 || rq_data_dir(req) == READ)
332 brq.mrq.stop = &brq.stop;
333 readcmd = MMC_READ_MULTIPLE_BLOCK;
334 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
337 readcmd = MMC_READ_SINGLE_BLOCK;
338 writecmd = MMC_WRITE_BLOCK;
341 if (rq_data_dir(req) == READ) {
342 brq.cmd.opcode = readcmd;
343 brq.data.flags |= MMC_DATA_READ;
345 brq.cmd.opcode = writecmd;
346 brq.data.flags |= MMC_DATA_WRITE;
349 mmc_set_data_timeout(&brq.data, card);
351 brq.data.sg = mq->sg;
352 brq.data.sg_len = mmc_queue_map_sg(mq);
355 * Adjust the sg list so it is the same size as the
358 if (brq.data.blocks != blk_rq_sectors(req)) {
359 int i, data_size = brq.data.blocks << 9;
360 struct scatterlist *sg;
362 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
363 data_size -= sg->length;
364 if (data_size <= 0) {
365 sg->length += data_size;
373 mmc_queue_bounce_pre(mq);
375 mmc_wait_for_req(card->host, &brq.mrq);
377 mmc_queue_bounce_post(mq);
380 * Check for errors here, but don't jump to cmd_err
381 * until later as we need to wait for the card to leave
382 * programming mode even when things go wrong.
384 if (brq.cmd.error || brq.data.error || brq.stop.error) {
385 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
386 /* Redo read one sector at a time */
387 printk(KERN_WARNING "%s: retrying using single "
388 "block read\n", req->rq_disk->disk_name);
392 status = get_card_status(card, req);
396 printk(KERN_ERR "%s: error %d sending read/write "
397 "command, response %#x, card status %#x\n",
398 req->rq_disk->disk_name, brq.cmd.error,
399 brq.cmd.resp[0], status);
402 if (brq.data.error) {
403 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
404 /* 'Stop' response contains card status */
405 status = brq.mrq.stop->resp[0];
406 printk(KERN_ERR "%s: error %d transferring data,"
407 " sector %u, nr %u, card status %#x\n",
408 req->rq_disk->disk_name, brq.data.error,
409 (unsigned)blk_rq_pos(req),
410 (unsigned)blk_rq_sectors(req), status);
413 if (brq.stop.error) {
414 printk(KERN_ERR "%s: error %d sending stop command, "
415 "response %#x, card status %#x\n",
416 req->rq_disk->disk_name, brq.stop.error,
417 brq.stop.resp[0], status);
420 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
424 cmd.opcode = MMC_SEND_STATUS;
425 cmd.arg = card->rca << 16;
426 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
427 err = mmc_wait_for_cmd(card->host, &cmd, 5);
429 printk(KERN_ERR "%s: error %d requesting status\n",
430 req->rq_disk->disk_name, err);
434 * Some cards mishandle the status bits,
435 * so make sure to check both the busy
436 * indication and the card state.
438 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
439 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
442 if (cmd.resp[0] & ~0x00000900)
443 printk(KERN_ERR "%s: status = %08x\n",
444 req->rq_disk->disk_name, cmd.resp[0]);
445 if (mmc_decode_status(cmd.resp))
450 if (brq.cmd.error || brq.stop.error || brq.data.error) {
451 if (rq_data_dir(req) == READ) {
453 * After an error, we redo I/O one sector at a
454 * time, so we only reach here after trying to
455 * read a single sector.
457 spin_lock_irq(&md->lock);
458 ret = __blk_end_request(req, -EIO, brq.data.blksz);
459 spin_unlock_irq(&md->lock);
466 * A block was successfully transferred.
468 spin_lock_irq(&md->lock);
469 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
470 spin_unlock_irq(&md->lock);
473 mmc_release_host(card->host);
479 * If this is an SD card and we're writing, we can first
480 * mark the known good sectors as ok.
482 * If the card is not SD, we can still ok written sectors
483 * as reported by the controller (which might be less than
484 * the real number of written sectors, but never more).
486 if (mmc_card_sd(card)) {
489 blocks = mmc_sd_num_wr_blocks(card);
490 if (blocks != (u32)-1) {
491 spin_lock_irq(&md->lock);
492 ret = __blk_end_request(req, 0, blocks << 9);
493 spin_unlock_irq(&md->lock);
496 spin_lock_irq(&md->lock);
497 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
498 spin_unlock_irq(&md->lock);
501 mmc_release_host(card->host);
503 spin_lock_irq(&md->lock);
505 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
506 spin_unlock_irq(&md->lock);
511 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
513 if (req->cmd_flags & REQ_DISCARD)
514 return mmc_blk_issue_discard_rq(mq, req);
516 return mmc_blk_issue_rw_rq(mq, req);
519 static inline int mmc_blk_readonly(struct mmc_card *card)
521 return mmc_card_readonly(card) ||
522 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
525 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
527 struct mmc_blk_data *md;
530 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
531 if (devidx >= MMC_NUM_MINORS)
532 return ERR_PTR(-ENOSPC);
533 __set_bit(devidx, dev_use);
535 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
543 * Set the read-only status based on the supported commands
544 * and the write protect switch.
546 md->read_only = mmc_blk_readonly(card);
548 md->disk = alloc_disk(1 << MMC_SHIFT);
549 if (md->disk == NULL) {
554 spin_lock_init(&md->lock);
557 ret = mmc_init_queue(&md->queue, card, &md->lock);
561 md->queue.issue_fn = mmc_blk_issue_rq;
564 md->disk->major = MMC_BLOCK_MAJOR;
565 md->disk->first_minor = devidx << MMC_SHIFT;
566 md->disk->fops = &mmc_bdops;
567 md->disk->private_data = md;
568 md->disk->queue = md->queue.queue;
569 md->disk->driverfs_dev = &card->dev;
572 * As discussed on lkml, GENHD_FL_REMOVABLE should:
574 * - be set for removable media with permanent block devices
575 * - be unset for removable block devices with permanent media
577 * Since MMC block devices clearly fall under the second
578 * case, we do not set GENHD_FL_REMOVABLE. Userspace
579 * should use the block device creation/destruction hotplug
580 * messages to tell when the card is present.
583 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
585 blk_queue_logical_block_size(md->queue.queue, 512);
587 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
589 * The EXT_CSD sector count is in number or 512 byte
592 set_capacity(md->disk, card->ext_csd.sectors);
595 * The CSD capacity field is in units of read_blkbits.
596 * set_capacity takes units of 512 bytes.
598 set_capacity(md->disk,
599 card->csd.capacity << (card->csd.read_blkbits - 9));
612 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
614 struct mmc_command cmd;
617 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
618 if (mmc_card_blockaddr(card))
621 mmc_claim_host(card->host);
622 cmd.opcode = MMC_SET_BLOCKLEN;
624 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
625 err = mmc_wait_for_cmd(card->host, &cmd, 5);
626 mmc_release_host(card->host);
629 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
630 md->disk->disk_name, cmd.arg, err);
637 static int mmc_blk_probe(struct mmc_card *card)
639 struct mmc_blk_data *md;
645 * Check that the card supports the command class(es) we need.
647 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
650 md = mmc_blk_alloc(card);
654 err = mmc_blk_set_blksize(md, card);
658 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
659 cap_str, sizeof(cap_str));
660 printk(KERN_INFO "%s: %s %s %s %s\n",
661 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
662 cap_str, md->read_only ? "(ro)" : "");
664 mmc_set_drvdata(card, md);
669 mmc_cleanup_queue(&md->queue);
675 static void mmc_blk_remove(struct mmc_card *card)
677 struct mmc_blk_data *md = mmc_get_drvdata(card);
680 /* Stop new requests from getting into the queue */
681 del_gendisk(md->disk);
683 /* Then flush out any already in there */
684 mmc_cleanup_queue(&md->queue);
688 mmc_set_drvdata(card, NULL);
692 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
694 struct mmc_blk_data *md = mmc_get_drvdata(card);
697 mmc_queue_suspend(&md->queue);
702 static int mmc_blk_resume(struct mmc_card *card)
704 struct mmc_blk_data *md = mmc_get_drvdata(card);
707 mmc_blk_set_blksize(md, card);
708 mmc_queue_resume(&md->queue);
713 #define mmc_blk_suspend NULL
714 #define mmc_blk_resume NULL
717 static struct mmc_driver mmc_driver = {
721 .probe = mmc_blk_probe,
722 .remove = mmc_blk_remove,
723 .suspend = mmc_blk_suspend,
724 .resume = mmc_blk_resume,
727 static int __init mmc_blk_init(void)
731 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
735 res = mmc_register_driver(&mmc_driver);
741 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
746 static void __exit mmc_blk_exit(void)
748 mmc_unregister_driver(&mmc_driver);
749 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
752 module_init(mmc_blk_init);
753 module_exit(mmc_blk_exit);
755 MODULE_LICENSE("GPL");
756 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");