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 devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
89 __clear_bit(devidx, dev_use);
94 mutex_unlock(&open_lock);
97 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
99 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
104 check_disk_change(bdev);
107 if ((mode & FMODE_WRITE) && md->read_only) {
116 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
118 struct mmc_blk_data *md = 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 return ntohl(blocks);
212 static u32 get_card_status(struct mmc_card *card, struct request *req)
214 struct mmc_command cmd;
217 memset(&cmd, 0, sizeof(struct mmc_command));
218 cmd.opcode = MMC_SEND_STATUS;
219 if (!mmc_host_is_spi(card->host))
220 cmd.arg = card->rca << 16;
221 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
222 err = mmc_wait_for_cmd(card->host, &cmd, 0);
224 printk(KERN_ERR "%s: error %d sending status comand",
225 req->rq_disk->disk_name, err);
229 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
231 struct mmc_blk_data *md = mq->data;
232 struct mmc_card *card = md->queue.card;
233 struct mmc_blk_request brq;
234 int ret = 1, disable_multi = 0;
236 mmc_claim_host(card->host);
239 struct mmc_command cmd;
240 u32 readcmd, writecmd, status = 0;
242 memset(&brq, 0, sizeof(struct mmc_blk_request));
243 brq.mrq.cmd = &brq.cmd;
244 brq.mrq.data = &brq.data;
246 brq.cmd.arg = blk_rq_pos(req);
247 if (!mmc_card_blockaddr(card))
249 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
250 brq.data.blksz = 512;
251 brq.stop.opcode = MMC_STOP_TRANSMISSION;
253 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
254 brq.data.blocks = blk_rq_sectors(req);
257 * The block layer doesn't support all sector count
258 * restrictions, so we need to be prepared for too big
261 if (brq.data.blocks > card->host->max_blk_count)
262 brq.data.blocks = card->host->max_blk_count;
265 * After a read error, we redo the request one sector at a time
266 * in order to accurately determine which sectors can be read
269 if (disable_multi && brq.data.blocks > 1)
272 if (brq.data.blocks > 1) {
273 /* SPI multiblock writes terminate using a special
274 * token, not a STOP_TRANSMISSION request.
276 if (!mmc_host_is_spi(card->host)
277 || rq_data_dir(req) == READ)
278 brq.mrq.stop = &brq.stop;
279 readcmd = MMC_READ_MULTIPLE_BLOCK;
280 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
283 readcmd = MMC_READ_SINGLE_BLOCK;
284 writecmd = MMC_WRITE_BLOCK;
287 if (rq_data_dir(req) == READ) {
288 brq.cmd.opcode = readcmd;
289 brq.data.flags |= MMC_DATA_READ;
291 brq.cmd.opcode = writecmd;
292 brq.data.flags |= MMC_DATA_WRITE;
295 mmc_set_data_timeout(&brq.data, card);
297 brq.data.sg = mq->sg;
298 brq.data.sg_len = mmc_queue_map_sg(mq);
301 * Adjust the sg list so it is the same size as the
304 if (brq.data.blocks != blk_rq_sectors(req)) {
305 int i, data_size = brq.data.blocks << 9;
306 struct scatterlist *sg;
308 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
309 data_size -= sg->length;
310 if (data_size <= 0) {
311 sg->length += data_size;
319 mmc_queue_bounce_pre(mq);
321 mmc_wait_for_req(card->host, &brq.mrq);
323 mmc_queue_bounce_post(mq);
326 * Check for errors here, but don't jump to cmd_err
327 * until later as we need to wait for the card to leave
328 * programming mode even when things go wrong.
330 if (brq.cmd.error || brq.data.error || brq.stop.error) {
331 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
332 /* Redo read one sector at a time */
333 printk(KERN_WARNING "%s: retrying using single "
334 "block read\n", req->rq_disk->disk_name);
338 status = get_card_status(card, req);
342 printk(KERN_ERR "%s: error %d sending read/write "
343 "command, response %#x, card status %#x\n",
344 req->rq_disk->disk_name, brq.cmd.error,
345 brq.cmd.resp[0], status);
348 if (brq.data.error) {
349 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
350 /* 'Stop' response contains card status */
351 status = brq.mrq.stop->resp[0];
352 printk(KERN_ERR "%s: error %d transferring data,"
353 " sector %u, nr %u, card status %#x\n",
354 req->rq_disk->disk_name, brq.data.error,
355 (unsigned)blk_rq_pos(req),
356 (unsigned)blk_rq_sectors(req), status);
359 if (brq.stop.error) {
360 printk(KERN_ERR "%s: error %d sending stop command, "
361 "response %#x, card status %#x\n",
362 req->rq_disk->disk_name, brq.stop.error,
363 brq.stop.resp[0], status);
366 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
370 cmd.opcode = MMC_SEND_STATUS;
371 cmd.arg = card->rca << 16;
372 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
373 err = mmc_wait_for_cmd(card->host, &cmd, 5);
375 printk(KERN_ERR "%s: error %d requesting status\n",
376 req->rq_disk->disk_name, err);
380 * Some cards mishandle the status bits,
381 * so make sure to check both the busy
382 * indication and the card state.
384 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
385 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
388 if (cmd.resp[0] & ~0x00000900)
389 printk(KERN_ERR "%s: status = %08x\n",
390 req->rq_disk->disk_name, cmd.resp[0]);
391 if (mmc_decode_status(cmd.resp))
396 if (brq.cmd.error || brq.stop.error || brq.data.error) {
397 if (rq_data_dir(req) == READ) {
399 * After an error, we redo I/O one sector at a
400 * time, so we only reach here after trying to
401 * read a single sector.
403 spin_lock_irq(&md->lock);
404 ret = __blk_end_request(req, -EIO, brq.data.blksz);
405 spin_unlock_irq(&md->lock);
412 * A block was successfully transferred.
414 spin_lock_irq(&md->lock);
415 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
416 spin_unlock_irq(&md->lock);
419 mmc_release_host(card->host);
425 * If this is an SD card and we're writing, we can first
426 * mark the known good sectors as ok.
428 * If the card is not SD, we can still ok written sectors
429 * as reported by the controller (which might be less than
430 * the real number of written sectors, but never more).
432 if (mmc_card_sd(card)) {
435 blocks = mmc_sd_num_wr_blocks(card);
436 if (blocks != (u32)-1) {
437 spin_lock_irq(&md->lock);
438 ret = __blk_end_request(req, 0, blocks << 9);
439 spin_unlock_irq(&md->lock);
442 spin_lock_irq(&md->lock);
443 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
444 spin_unlock_irq(&md->lock);
447 mmc_release_host(card->host);
449 spin_lock_irq(&md->lock);
451 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
452 spin_unlock_irq(&md->lock);
458 static inline int mmc_blk_readonly(struct mmc_card *card)
460 return mmc_card_readonly(card) ||
461 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
464 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
466 struct mmc_blk_data *md;
469 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
470 if (devidx >= MMC_NUM_MINORS)
471 return ERR_PTR(-ENOSPC);
472 __set_bit(devidx, dev_use);
474 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
482 * Set the read-only status based on the supported commands
483 * and the write protect switch.
485 md->read_only = mmc_blk_readonly(card);
487 md->disk = alloc_disk(1 << MMC_SHIFT);
488 if (md->disk == NULL) {
493 spin_lock_init(&md->lock);
496 ret = mmc_init_queue(&md->queue, card, &md->lock);
500 md->queue.issue_fn = mmc_blk_issue_rq;
503 md->disk->major = MMC_BLOCK_MAJOR;
504 md->disk->first_minor = devidx << MMC_SHIFT;
505 md->disk->fops = &mmc_bdops;
506 md->disk->private_data = md;
507 md->disk->queue = md->queue.queue;
508 md->disk->driverfs_dev = &card->dev;
511 * As discussed on lkml, GENHD_FL_REMOVABLE should:
513 * - be set for removable media with permanent block devices
514 * - be unset for removable block devices with permanent media
516 * Since MMC block devices clearly fall under the second
517 * case, we do not set GENHD_FL_REMOVABLE. Userspace
518 * should use the block device creation/destruction hotplug
519 * messages to tell when the card is present.
522 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
524 blk_queue_logical_block_size(md->queue.queue, 512);
526 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
528 * The EXT_CSD sector count is in number or 512 byte
531 set_capacity(md->disk, card->ext_csd.sectors);
534 * The CSD capacity field is in units of read_blkbits.
535 * set_capacity takes units of 512 bytes.
537 set_capacity(md->disk,
538 card->csd.capacity << (card->csd.read_blkbits - 9));
551 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
553 struct mmc_command cmd;
556 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
557 if (mmc_card_blockaddr(card))
560 mmc_claim_host(card->host);
561 cmd.opcode = MMC_SET_BLOCKLEN;
563 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
564 err = mmc_wait_for_cmd(card->host, &cmd, 5);
565 mmc_release_host(card->host);
568 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
569 md->disk->disk_name, cmd.arg, err);
576 static int mmc_blk_probe(struct mmc_card *card)
578 struct mmc_blk_data *md;
584 * Check that the card supports the command class(es) we need.
586 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
589 md = mmc_blk_alloc(card);
593 err = mmc_blk_set_blksize(md, card);
597 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
598 cap_str, sizeof(cap_str));
599 printk(KERN_INFO "%s: %s %s %s %s\n",
600 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
601 cap_str, md->read_only ? "(ro)" : "");
603 mmc_set_drvdata(card, md);
613 static void mmc_blk_remove(struct mmc_card *card)
615 struct mmc_blk_data *md = mmc_get_drvdata(card);
618 /* Stop new requests from getting into the queue */
619 del_gendisk(md->disk);
621 /* Then flush out any already in there */
622 mmc_cleanup_queue(&md->queue);
626 mmc_set_drvdata(card, NULL);
630 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
632 struct mmc_blk_data *md = mmc_get_drvdata(card);
635 mmc_queue_suspend(&md->queue);
640 static int mmc_blk_resume(struct mmc_card *card)
642 struct mmc_blk_data *md = mmc_get_drvdata(card);
645 mmc_blk_set_blksize(md, card);
646 mmc_queue_resume(&md->queue);
651 #define mmc_blk_suspend NULL
652 #define mmc_blk_resume NULL
655 static struct mmc_driver mmc_driver = {
659 .probe = mmc_blk_probe,
660 .remove = mmc_blk_remove,
661 .suspend = mmc_blk_suspend,
662 .resume = mmc_blk_resume,
665 static int __init mmc_blk_init(void)
669 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
673 res = mmc_register_driver(&mmc_driver);
679 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
684 static void __exit mmc_blk_exit(void)
686 mmc_unregister_driver(&mmc_driver);
687 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
690 module_init(mmc_blk_init);
691 module_exit(mmc_blk_exit);
693 MODULE_LICENSE("GPL");
694 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");