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 __clear_bit(devidx, dev_use);
99 mutex_unlock(&open_lock);
102 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
104 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
109 check_disk_change(bdev);
112 if ((mode & FMODE_WRITE) && md->read_only) {
121 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
123 struct mmc_blk_data *md = disk->private_data;
130 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
132 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
138 static const struct block_device_operations mmc_bdops = {
139 .open = mmc_blk_open,
140 .release = mmc_blk_release,
141 .getgeo = mmc_blk_getgeo,
142 .owner = THIS_MODULE,
145 struct mmc_blk_request {
146 struct mmc_request mrq;
147 struct mmc_command cmd;
148 struct mmc_command stop;
149 struct mmc_data data;
152 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
158 struct mmc_request mrq;
159 struct mmc_command cmd;
160 struct mmc_data data;
161 unsigned int timeout_us;
163 struct scatterlist sg;
165 memset(&cmd, 0, sizeof(struct mmc_command));
167 cmd.opcode = MMC_APP_CMD;
168 cmd.arg = card->rca << 16;
169 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
171 err = mmc_wait_for_cmd(card->host, &cmd, 0);
174 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
177 memset(&cmd, 0, sizeof(struct mmc_command));
179 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
181 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
183 memset(&data, 0, sizeof(struct mmc_data));
185 data.timeout_ns = card->csd.tacc_ns * 100;
186 data.timeout_clks = card->csd.tacc_clks * 100;
188 timeout_us = data.timeout_ns / 1000;
189 timeout_us += data.timeout_clks * 1000 /
190 (card->host->ios.clock / 1000);
192 if (timeout_us > 100000) {
193 data.timeout_ns = 100000000;
194 data.timeout_clks = 0;
199 data.flags = MMC_DATA_READ;
203 memset(&mrq, 0, sizeof(struct mmc_request));
208 blocks = kmalloc(4, GFP_KERNEL);
212 sg_init_one(&sg, blocks, 4);
214 mmc_wait_for_req(card->host, &mrq);
216 result = ntohl(*blocks);
219 if (cmd.error || data.error)
225 static u32 get_card_status(struct mmc_card *card, struct request *req)
227 struct mmc_command cmd;
230 memset(&cmd, 0, sizeof(struct mmc_command));
231 cmd.opcode = MMC_SEND_STATUS;
232 if (!mmc_host_is_spi(card->host))
233 cmd.arg = card->rca << 16;
234 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
235 err = mmc_wait_for_cmd(card->host, &cmd, 0);
237 printk(KERN_ERR "%s: error %d sending status comand",
238 req->rq_disk->disk_name, err);
243 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
245 struct mmc_command cmd;
248 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
249 if (mmc_card_blockaddr(card))
252 mmc_claim_host(card->host);
253 cmd.opcode = MMC_SET_BLOCKLEN;
255 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
256 err = mmc_wait_for_cmd(card->host, &cmd, 5);
257 mmc_release_host(card->host);
260 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
261 md->disk->disk_name, cmd.arg, err);
269 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
271 struct mmc_blk_data *md = mq->data;
272 struct mmc_card *card = md->queue.card;
273 struct mmc_blk_request brq;
274 int ret = 1, disable_multi = 0;
276 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
277 if (mmc_bus_needs_resume(card->host)) {
278 mmc_resume_bus(card->host);
279 mmc_blk_set_blksize(md, card);
283 mmc_claim_host(card->host);
286 struct mmc_command cmd;
287 u32 readcmd, writecmd, status = 0;
289 memset(&brq, 0, sizeof(struct mmc_blk_request));
290 brq.mrq.cmd = &brq.cmd;
291 brq.mrq.data = &brq.data;
293 brq.cmd.arg = blk_rq_pos(req);
294 if (!mmc_card_blockaddr(card))
296 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
297 brq.data.blksz = 512;
298 brq.stop.opcode = MMC_STOP_TRANSMISSION;
300 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
301 brq.data.blocks = blk_rq_sectors(req);
304 * The block layer doesn't support all sector count
305 * restrictions, so we need to be prepared for too big
308 if (brq.data.blocks > card->host->max_blk_count)
309 brq.data.blocks = card->host->max_blk_count;
312 * After a read error, we redo the request one sector at a time
313 * in order to accurately determine which sectors can be read
316 if (disable_multi && brq.data.blocks > 1)
319 if (brq.data.blocks > 1) {
320 /* SPI multiblock writes terminate using a special
321 * token, not a STOP_TRANSMISSION request.
323 if (!mmc_host_is_spi(card->host)
324 || rq_data_dir(req) == READ)
325 brq.mrq.stop = &brq.stop;
326 readcmd = MMC_READ_MULTIPLE_BLOCK;
327 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
330 readcmd = MMC_READ_SINGLE_BLOCK;
331 writecmd = MMC_WRITE_BLOCK;
334 if (rq_data_dir(req) == READ) {
335 brq.cmd.opcode = readcmd;
336 brq.data.flags |= MMC_DATA_READ;
338 brq.cmd.opcode = writecmd;
339 brq.data.flags |= MMC_DATA_WRITE;
342 mmc_set_data_timeout(&brq.data, card);
344 brq.data.sg = mq->sg;
345 brq.data.sg_len = mmc_queue_map_sg(mq);
348 * Adjust the sg list so it is the same size as the
351 if (brq.data.blocks != blk_rq_sectors(req)) {
352 int i, data_size = brq.data.blocks << 9;
353 struct scatterlist *sg;
355 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
356 data_size -= sg->length;
357 if (data_size <= 0) {
358 sg->length += data_size;
366 mmc_queue_bounce_pre(mq);
368 mmc_wait_for_req(card->host, &brq.mrq);
370 mmc_queue_bounce_post(mq);
373 * Check for errors here, but don't jump to cmd_err
374 * until later as we need to wait for the card to leave
375 * programming mode even when things go wrong.
377 if (brq.cmd.error || brq.data.error || brq.stop.error) {
378 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
379 /* Redo read one sector at a time */
380 printk(KERN_WARNING "%s: retrying using single "
381 "block read\n", req->rq_disk->disk_name);
385 status = get_card_status(card, req);
386 } else if (disable_multi == 1) {
391 printk(KERN_ERR "%s: error %d sending read/write "
392 "command, response %#x, card status %#x\n",
393 req->rq_disk->disk_name, brq.cmd.error,
394 brq.cmd.resp[0], status);
397 if (brq.data.error) {
398 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
399 /* 'Stop' response contains card status */
400 status = brq.mrq.stop->resp[0];
401 printk(KERN_ERR "%s: error %d transferring data,"
402 " sector %u, nr %u, card status %#x\n",
403 req->rq_disk->disk_name, brq.data.error,
404 (unsigned)blk_rq_pos(req),
405 (unsigned)blk_rq_sectors(req), status);
408 if (brq.stop.error) {
409 printk(KERN_ERR "%s: error %d sending stop command, "
410 "response %#x, card status %#x\n",
411 req->rq_disk->disk_name, brq.stop.error,
412 brq.stop.resp[0], status);
415 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
419 cmd.opcode = MMC_SEND_STATUS;
420 cmd.arg = card->rca << 16;
421 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
422 err = mmc_wait_for_cmd(card->host, &cmd, 5);
424 printk(KERN_ERR "%s: error %d requesting status\n",
425 req->rq_disk->disk_name, err);
429 * Some cards mishandle the status bits,
430 * so make sure to check both the busy
431 * indication and the card state.
433 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
434 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
437 if (cmd.resp[0] & ~0x00000900)
438 printk(KERN_ERR "%s: status = %08x\n",
439 req->rq_disk->disk_name, cmd.resp[0]);
440 if (mmc_decode_status(cmd.resp))
445 if (brq.cmd.error || brq.stop.error || brq.data.error) {
446 if (rq_data_dir(req) == READ) {
448 * After an error, we redo I/O one sector at a
449 * time, so we only reach here after trying to
450 * read a single sector.
452 spin_lock_irq(&md->lock);
453 ret = __blk_end_request(req, -EIO, brq.data.blksz);
454 spin_unlock_irq(&md->lock);
461 * A block was successfully transferred.
463 spin_lock_irq(&md->lock);
464 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
465 spin_unlock_irq(&md->lock);
468 mmc_release_host(card->host);
474 * If this is an SD card and we're writing, we can first
475 * mark the known good sectors as ok.
477 * If the card is not SD, we can still ok written sectors
478 * as reported by the controller (which might be less than
479 * the real number of written sectors, but never more).
481 if (mmc_card_sd(card)) {
484 blocks = mmc_sd_num_wr_blocks(card);
485 if (blocks != (u32)-1) {
486 spin_lock_irq(&md->lock);
487 ret = __blk_end_request(req, 0, blocks << 9);
488 spin_unlock_irq(&md->lock);
491 spin_lock_irq(&md->lock);
492 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
493 spin_unlock_irq(&md->lock);
496 mmc_release_host(card->host);
498 spin_lock_irq(&md->lock);
500 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
501 spin_unlock_irq(&md->lock);
507 static inline int mmc_blk_readonly(struct mmc_card *card)
509 return mmc_card_readonly(card) ||
510 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
513 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
515 struct mmc_blk_data *md;
518 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
519 if (devidx >= MMC_NUM_MINORS)
520 return ERR_PTR(-ENOSPC);
521 __set_bit(devidx, dev_use);
523 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
531 * Set the read-only status based on the supported commands
532 * and the write protect switch.
534 md->read_only = mmc_blk_readonly(card);
536 md->disk = alloc_disk(1 << MMC_SHIFT);
537 if (md->disk == NULL) {
542 spin_lock_init(&md->lock);
545 ret = mmc_init_queue(&md->queue, card, &md->lock);
549 md->queue.issue_fn = mmc_blk_issue_rq;
552 md->disk->major = MMC_BLOCK_MAJOR;
553 md->disk->first_minor = devidx << MMC_SHIFT;
554 md->disk->fops = &mmc_bdops;
555 md->disk->private_data = md;
556 md->disk->queue = md->queue.queue;
557 md->disk->driverfs_dev = &card->dev;
560 * As discussed on lkml, GENHD_FL_REMOVABLE should:
562 * - be set for removable media with permanent block devices
563 * - be unset for removable block devices with permanent media
565 * Since MMC block devices clearly fall under the second
566 * case, we do not set GENHD_FL_REMOVABLE. Userspace
567 * should use the block device creation/destruction hotplug
568 * messages to tell when the card is present.
571 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
573 blk_queue_logical_block_size(md->queue.queue, 512);
575 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
577 * The EXT_CSD sector count is in number or 512 byte
580 set_capacity(md->disk, card->ext_csd.sectors);
583 * The CSD capacity field is in units of read_blkbits.
584 * set_capacity takes units of 512 bytes.
586 set_capacity(md->disk,
587 card->csd.capacity << (card->csd.read_blkbits - 9));
599 static int mmc_blk_probe(struct mmc_card *card)
601 struct mmc_blk_data *md;
607 * Check that the card supports the command class(es) we need.
609 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
612 md = mmc_blk_alloc(card);
616 err = mmc_blk_set_blksize(md, card);
620 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
621 cap_str, sizeof(cap_str));
622 printk(KERN_INFO "%s: %s %s %s %s\n",
623 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
624 cap_str, md->read_only ? "(ro)" : "");
626 mmc_set_drvdata(card, md);
627 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
628 mmc_set_bus_resume_policy(card->host, 1);
634 mmc_cleanup_queue(&md->queue);
640 static void mmc_blk_remove(struct mmc_card *card)
642 struct mmc_blk_data *md = mmc_get_drvdata(card);
645 /* Stop new requests from getting into the queue */
646 del_gendisk(md->disk);
648 /* Then flush out any already in there */
649 mmc_cleanup_queue(&md->queue);
653 mmc_set_drvdata(card, NULL);
654 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
655 mmc_set_bus_resume_policy(card->host, 0);
660 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
662 struct mmc_blk_data *md = mmc_get_drvdata(card);
665 mmc_queue_suspend(&md->queue);
670 static int mmc_blk_resume(struct mmc_card *card)
672 struct mmc_blk_data *md = mmc_get_drvdata(card);
675 #ifndef CONFIG_MMC_BLOCK_DEFERRED_RESUME
676 mmc_blk_set_blksize(md, card);
678 mmc_queue_resume(&md->queue);
683 #define mmc_blk_suspend NULL
684 #define mmc_blk_resume NULL
687 static struct mmc_driver mmc_driver = {
691 .probe = mmc_blk_probe,
692 .remove = mmc_blk_remove,
693 .suspend = mmc_blk_suspend,
694 .resume = mmc_blk_resume,
697 static int __init mmc_blk_init(void)
701 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
705 res = mmc_register_driver(&mmc_driver);
711 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
716 static void __exit mmc_blk_exit(void)
718 mmc_unregister_driver(&mmc_driver);
719 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
722 module_init(mmc_blk_init);
723 module_exit(mmc_blk_exit);
725 MODULE_LICENSE("GPL");
726 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");