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>
44 MODULE_ALIAS("mmc:block");
47 * max 8 partitions per card
50 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
52 static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
55 * There is one mmc_blk_data per slot.
60 struct mmc_queue queue;
63 unsigned int read_only;
66 static DEFINE_MUTEX(open_lock);
68 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
70 struct mmc_blk_data *md;
72 mutex_lock(&open_lock);
73 md = disk->private_data;
74 if (md && md->usage == 0)
78 mutex_unlock(&open_lock);
83 static void mmc_blk_put(struct mmc_blk_data *md)
85 mutex_lock(&open_lock);
88 int devmaj = MAJOR(disk_devt(md->disk));
89 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
92 devidx = md->disk->first_minor >> MMC_SHIFT;
94 blk_cleanup_queue(md->queue.queue);
96 __clear_bit(devidx, dev_use);
101 mutex_unlock(&open_lock);
104 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
106 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
111 check_disk_change(bdev);
114 if ((mode & FMODE_WRITE) && md->read_only) {
123 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
125 struct mmc_blk_data *md = disk->private_data;
132 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
134 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
140 static const struct block_device_operations mmc_bdops = {
141 .open = mmc_blk_open,
142 .release = mmc_blk_release,
143 .getgeo = mmc_blk_getgeo,
144 .owner = THIS_MODULE,
147 struct mmc_blk_request {
148 struct mmc_request mrq;
149 struct mmc_command cmd;
150 struct mmc_command stop;
151 struct mmc_data data;
154 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
160 struct mmc_request mrq;
161 struct mmc_command cmd;
162 struct mmc_data data;
163 unsigned int timeout_us;
165 struct scatterlist sg;
167 memset(&cmd, 0, sizeof(struct mmc_command));
169 cmd.opcode = MMC_APP_CMD;
170 cmd.arg = card->rca << 16;
171 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
173 err = mmc_wait_for_cmd(card->host, &cmd, 0);
176 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
179 memset(&cmd, 0, sizeof(struct mmc_command));
181 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
183 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
185 memset(&data, 0, sizeof(struct mmc_data));
187 data.timeout_ns = card->csd.tacc_ns * 100;
188 data.timeout_clks = card->csd.tacc_clks * 100;
190 timeout_us = data.timeout_ns / 1000;
191 timeout_us += data.timeout_clks * 1000 /
192 (card->host->ios.clock / 1000);
194 if (timeout_us > 100000) {
195 data.timeout_ns = 100000000;
196 data.timeout_clks = 0;
201 data.flags = MMC_DATA_READ;
205 memset(&mrq, 0, sizeof(struct mmc_request));
210 blocks = kmalloc(4, GFP_KERNEL);
214 sg_init_one(&sg, blocks, 4);
216 mmc_wait_for_req(card->host, &mrq);
218 result = ntohl(*blocks);
221 if (cmd.error || data.error)
227 static u32 get_card_status(struct mmc_card *card, struct request *req)
229 struct mmc_command cmd;
232 memset(&cmd, 0, sizeof(struct mmc_command));
233 cmd.opcode = MMC_SEND_STATUS;
234 if (!mmc_host_is_spi(card->host))
235 cmd.arg = card->rca << 16;
236 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
237 err = mmc_wait_for_cmd(card->host, &cmd, 0);
238 #if 0 //[xjh] not printk,save time
240 printk(KERN_ERR "%s: error %d sending status comand",
241 req->rq_disk->disk_name, err);
247 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
249 struct mmc_command cmd;
252 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
253 if (mmc_card_blockaddr(card))
256 mmc_claim_host(card->host);
257 cmd.opcode = MMC_SET_BLOCKLEN;
259 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
260 err = mmc_wait_for_cmd(card->host, &cmd, 5);
261 mmc_release_host(card->host);
264 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
265 md->disk->disk_name, cmd.arg, err);
273 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
275 struct mmc_blk_data *md = mq->data;
276 struct mmc_card *card = md->queue.card;
277 struct mmc_blk_request brq;
278 int ret = 1, disable_multi = 0;
280 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
281 if (mmc_bus_needs_resume(card->host)) {
282 mmc_resume_bus(card->host);
283 mmc_blk_set_blksize(md, card);
287 mmc_claim_host(card->host);
290 struct mmc_command cmd;
291 u32 readcmd, writecmd, status = 0;
293 memset(&brq, 0, sizeof(struct mmc_blk_request));
294 brq.mrq.cmd = &brq.cmd;
295 brq.mrq.data = &brq.data;
297 brq.cmd.arg = blk_rq_pos(req);
298 if (!mmc_card_blockaddr(card))
300 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
301 brq.data.blksz = 512;
302 brq.stop.opcode = MMC_STOP_TRANSMISSION;
304 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
305 brq.data.blocks = blk_rq_sectors(req);
308 * The block layer doesn't support all sector count
309 * restrictions, so we need to be prepared for too big
312 if (brq.data.blocks > card->host->max_blk_count)
313 brq.data.blocks = card->host->max_blk_count;
316 * After a read error, we redo the request one sector at a time
317 * in order to accurately determine which sectors can be read
320 if (disable_multi && brq.data.blocks > 1)
323 if (brq.data.blocks > 1) {
324 /* SPI multiblock writes terminate using a special
325 * token, not a STOP_TRANSMISSION request.
327 if (!mmc_host_is_spi(card->host)
328 || rq_data_dir(req) == READ)
329 brq.mrq.stop = &brq.stop;
330 readcmd = MMC_READ_MULTIPLE_BLOCK;
331 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
334 readcmd = MMC_READ_SINGLE_BLOCK;
335 writecmd = MMC_WRITE_BLOCK;
338 if (rq_data_dir(req) == READ) {
339 brq.cmd.opcode = readcmd;
340 brq.data.flags |= MMC_DATA_READ;
342 brq.cmd.opcode = writecmd;
343 brq.data.flags |= MMC_DATA_WRITE;
346 mmc_set_data_timeout(&brq.data, card);
348 brq.data.sg = mq->sg;
349 brq.data.sg_len = mmc_queue_map_sg(mq);
352 * Adjust the sg list so it is the same size as the
355 if (brq.data.blocks != blk_rq_sectors(req)) {
356 int i, data_size = brq.data.blocks << 9;
357 struct scatterlist *sg;
359 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
360 data_size -= sg->length;
361 if (data_size <= 0) {
362 sg->length += data_size;
370 mmc_queue_bounce_pre(mq);
372 mmc_wait_for_req(card->host, &brq.mrq);
374 mmc_queue_bounce_post(mq);
377 * Check for errors here, but don't jump to cmd_err
378 * until later as we need to wait for the card to leave
379 * programming mode even when things go wrong.
381 #if 1/*[xjh] do not retry with cmd17*/
382 if (brq.cmd.error || brq.data.error || brq.stop.error) {
384 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
385 /* Redo read one sector at a time */
386 printk(KERN_WARNING "%s: retrying using single "
387 "block read\n", req->rq_disk->disk_name);
392 status = get_card_status(card, req);
393 } else if (disable_multi == 1) {
398 #if 0 //[xjh] not printk,save time
400 printk(KERN_ERR "%s: error %d sending read/write "
401 "command, response %#x, card status %#x\n",
402 req->rq_disk->disk_name, brq.cmd.error,
403 brq.cmd.resp[0], status);
406 if (brq.data.error) {
407 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
408 /* 'Stop' response contains card status */
409 status = brq.mrq.stop->resp[0];
410 printk(KERN_ERR "%s: error %d transferring data,"
411 " sector %u, nr %u, card status %#x\n",
412 req->rq_disk->disk_name, brq.data.error,
413 (unsigned)blk_rq_pos(req),
414 (unsigned)blk_rq_sectors(req), status);
417 if (brq.stop.error) {
418 printk(KERN_ERR "%s: error %d sending stop command, "
419 "response %#x, card status %#x\n",
420 req->rq_disk->disk_name, brq.stop.error,
421 brq.stop.resp[0], status);
425 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
429 cmd.opcode = MMC_SEND_STATUS;
430 cmd.arg = card->rca << 16;
431 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
432 err = mmc_wait_for_cmd(card->host, &cmd, 5);
434 printk(KERN_ERR "%s: error %d requesting status\n",
435 req->rq_disk->disk_name, err);
439 * Some cards mishandle the status bits,
440 * so make sure to check both the busy
441 * indication and the card state.
443 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
444 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
447 if (cmd.resp[0] & ~0x00000900)
448 printk(KERN_ERR "%s: status = %08x\n",
449 req->rq_disk->disk_name, cmd.resp[0]);
450 if (mmc_decode_status(cmd.resp))
455 if (brq.cmd.error || brq.stop.error || brq.data.error) {
456 #if 1/*[xjh] do not retry with cmd17*/
457 if (rq_data_dir(req) == READ) {
459 * After an error, we redo I/O one sector at a
460 * time, so we only reach here after trying to
461 * read a single sector.
463 spin_lock_irq(&md->lock);
464 ret = __blk_end_request(req, -EIO, brq.data.blksz);
465 spin_unlock_irq(&md->lock);
473 * A block was successfully transferred.
475 spin_lock_irq(&md->lock);
476 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
477 spin_unlock_irq(&md->lock);
480 mmc_release_host(card->host);
486 * If this is an SD card and we're writing, we can first
487 * mark the known good sectors as ok.
489 * If the card is not SD, we can still ok written sectors
490 * as reported by the controller (which might be less than
491 * the real number of written sectors, but never more).
493 if (mmc_card_sd(card)) {
496 blocks = mmc_sd_num_wr_blocks(card);
497 if (blocks != (u32)-1) {
498 spin_lock_irq(&md->lock);
499 ret = __blk_end_request(req, 0, blocks << 9);
500 spin_unlock_irq(&md->lock);
503 spin_lock_irq(&md->lock);
504 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
505 spin_unlock_irq(&md->lock);
508 mmc_release_host(card->host);
510 spin_lock_irq(&md->lock);
512 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
513 spin_unlock_irq(&md->lock);
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));
611 static int mmc_blk_probe(struct mmc_card *card)
613 struct mmc_blk_data *md;
619 * Check that the card supports the command class(es) we need.
621 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
624 md = mmc_blk_alloc(card);
628 err = mmc_blk_set_blksize(md, card);
632 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
633 cap_str, sizeof(cap_str));
634 printk(KERN_INFO "%s: %s %s %s %s\n",
635 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
636 cap_str, md->read_only ? "(ro)" : "");
638 mmc_set_drvdata(card, md);
639 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
640 mmc_set_bus_resume_policy(card->host, 1);
646 mmc_cleanup_queue(&md->queue);
652 static void mmc_blk_remove(struct mmc_card *card)
654 struct mmc_blk_data *md = mmc_get_drvdata(card);
657 /* Stop new requests from getting into the queue */
658 del_gendisk(md->disk);
660 /* Then flush out any already in there */
661 mmc_cleanup_queue(&md->queue);
665 mmc_set_drvdata(card, NULL);
666 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
667 mmc_set_bus_resume_policy(card->host, 0);
672 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
674 struct mmc_blk_data *md = mmc_get_drvdata(card);
677 mmc_queue_suspend(&md->queue);
682 static int mmc_blk_resume(struct mmc_card *card)
684 struct mmc_blk_data *md = mmc_get_drvdata(card);
687 #ifndef CONFIG_MMC_BLOCK_DEFERRED_RESUME
688 mmc_blk_set_blksize(md, card);
690 mmc_queue_resume(&md->queue);
695 #define mmc_blk_suspend NULL
696 #define mmc_blk_resume NULL
699 static struct mmc_driver mmc_driver = {
703 .probe = mmc_blk_probe,
704 .remove = mmc_blk_remove,
705 .suspend = mmc_blk_suspend,
706 .resume = mmc_blk_resume,
709 static int __init mmc_blk_init(void)
713 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
717 res = mmc_register_driver(&mmc_driver);
723 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
728 static void __exit mmc_blk_exit(void)
730 mmc_unregister_driver(&mmc_driver);
731 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
734 module_init(mmc_blk_init);
735 module_exit(mmc_blk_exit);
737 MODULE_LICENSE("GPL");
738 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");