2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2007 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>
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/mmc.h>
36 #include <linux/mmc/sd.h>
38 #include <asm/system.h>
39 #include <asm/uaccess.h>
44 * max 8 partitions per card
49 * There is one mmc_blk_data per slot.
54 struct mmc_queue queue;
57 unsigned int block_bits;
58 unsigned int read_only;
61 static DEFINE_MUTEX(open_lock);
63 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
65 struct mmc_blk_data *md;
67 mutex_lock(&open_lock);
68 md = disk->private_data;
69 if (md && md->usage == 0)
73 mutex_unlock(&open_lock);
78 static void mmc_blk_put(struct mmc_blk_data *md)
80 mutex_lock(&open_lock);
86 mutex_unlock(&open_lock);
89 static int mmc_blk_open(struct inode *inode, struct file *filp)
91 struct mmc_blk_data *md;
94 md = mmc_blk_get(inode->i_bdev->bd_disk);
97 check_disk_change(inode->i_bdev);
100 if ((filp->f_mode & FMODE_WRITE) && md->read_only)
107 static int mmc_blk_release(struct inode *inode, struct file *filp)
109 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
116 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
118 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
124 static struct block_device_operations mmc_bdops = {
125 .open = mmc_blk_open,
126 .release = mmc_blk_release,
127 .getgeo = mmc_blk_getgeo,
128 .owner = THIS_MODULE,
131 struct mmc_blk_request {
132 struct mmc_request mrq;
133 struct mmc_command cmd;
134 struct mmc_command stop;
135 struct mmc_data data;
138 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
143 struct mmc_request mrq;
144 struct mmc_command cmd;
145 struct mmc_data data;
146 unsigned int timeout_us;
148 struct scatterlist sg;
150 memset(&cmd, 0, sizeof(struct mmc_command));
152 cmd.opcode = MMC_APP_CMD;
153 cmd.arg = card->rca << 16;
154 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
156 err = mmc_wait_for_cmd(card->host, &cmd, 0);
159 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
162 memset(&cmd, 0, sizeof(struct mmc_command));
164 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
166 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
168 memset(&data, 0, sizeof(struct mmc_data));
170 data.timeout_ns = card->csd.tacc_ns * 100;
171 data.timeout_clks = card->csd.tacc_clks * 100;
173 timeout_us = data.timeout_ns / 1000;
174 timeout_us += data.timeout_clks * 1000 /
175 (card->host->ios.clock / 1000);
177 if (timeout_us > 100000) {
178 data.timeout_ns = 100000000;
179 data.timeout_clks = 0;
184 data.flags = MMC_DATA_READ;
188 memset(&mrq, 0, sizeof(struct mmc_request));
193 sg_init_one(&sg, &blocks, 4);
195 mmc_wait_for_req(card->host, &mrq);
197 if (cmd.error || data.error)
200 blocks = ntohl(blocks);
205 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
207 struct mmc_blk_data *md = mq->data;
208 struct mmc_card *card = md->queue.card;
209 struct mmc_blk_request brq;
210 int ret = 1, sg_pos, data_size;
212 mmc_claim_host(card->host);
215 struct mmc_command cmd;
216 u32 readcmd, writecmd;
218 memset(&brq, 0, sizeof(struct mmc_blk_request));
219 brq.mrq.cmd = &brq.cmd;
220 brq.mrq.data = &brq.data;
222 brq.cmd.arg = req->sector;
223 if (!mmc_card_blockaddr(card))
225 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
226 brq.data.blksz = 1 << md->block_bits;
227 brq.stop.opcode = MMC_STOP_TRANSMISSION;
229 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
230 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
231 if (brq.data.blocks > card->host->max_blk_count)
232 brq.data.blocks = card->host->max_blk_count;
235 * If the host doesn't support multiple block writes, force
236 * block writes to single block. SD cards are excepted from
237 * this rule as they support querying the number of
238 * successfully written sectors.
240 if (rq_data_dir(req) != READ &&
241 !(card->host->caps & MMC_CAP_MULTIWRITE) &&
245 if (brq.data.blocks > 1) {
246 /* SPI multiblock writes terminate using a special
247 * token, not a STOP_TRANSMISSION request.
249 if (!mmc_host_is_spi(card->host)
250 || rq_data_dir(req) == READ)
251 brq.mrq.stop = &brq.stop;
252 readcmd = MMC_READ_MULTIPLE_BLOCK;
253 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
256 readcmd = MMC_READ_SINGLE_BLOCK;
257 writecmd = MMC_WRITE_BLOCK;
260 if (rq_data_dir(req) == READ) {
261 brq.cmd.opcode = readcmd;
262 brq.data.flags |= MMC_DATA_READ;
264 brq.cmd.opcode = writecmd;
265 brq.data.flags |= MMC_DATA_WRITE;
268 mmc_set_data_timeout(&brq.data, card);
270 brq.data.sg = mq->sg;
271 brq.data.sg_len = mmc_queue_map_sg(mq);
273 mmc_queue_bounce_pre(mq);
275 if (brq.data.blocks !=
276 (req->nr_sectors >> (md->block_bits - 9))) {
277 data_size = brq.data.blocks * brq.data.blksz;
278 for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
279 data_size -= mq->sg[sg_pos].length;
280 if (data_size <= 0) {
281 mq->sg[sg_pos].length += data_size;
286 brq.data.sg_len = sg_pos;
289 mmc_wait_for_req(card->host, &brq.mrq);
291 mmc_queue_bounce_post(mq);
294 printk(KERN_ERR "%s: error %d sending read/write command\n",
295 req->rq_disk->disk_name, brq.cmd.error);
299 if (brq.data.error) {
300 printk(KERN_ERR "%s: error %d transferring data\n",
301 req->rq_disk->disk_name, brq.data.error);
305 if (brq.stop.error) {
306 printk(KERN_ERR "%s: error %d sending stop command\n",
307 req->rq_disk->disk_name, brq.stop.error);
311 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
315 cmd.opcode = MMC_SEND_STATUS;
316 cmd.arg = card->rca << 16;
317 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
318 err = mmc_wait_for_cmd(card->host, &cmd, 5);
320 printk(KERN_ERR "%s: error %d requesting status\n",
321 req->rq_disk->disk_name, err);
325 * Some cards mishandle the status bits,
326 * so make sure to check both the busy
327 * indication and the card state.
329 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
330 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
333 if (cmd.resp[0] & ~0x00000900)
334 printk(KERN_ERR "%s: status = %08x\n",
335 req->rq_disk->disk_name, cmd.resp[0]);
336 if (mmc_decode_status(cmd.resp))
342 * A block was successfully transferred.
344 spin_lock_irq(&md->lock);
345 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
348 * The whole request completed successfully.
350 add_disk_randomness(req->rq_disk);
351 blkdev_dequeue_request(req);
352 end_that_request_last(req, 1);
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 * if the controller can do proper error reporting.
369 * For reads we just fail the entire chunk as that should
370 * be safe in all cases.
372 if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
376 blocks = mmc_sd_num_wr_blocks(card);
377 if (blocks != (u32)-1) {
378 if (card->csd.write_partial)
379 bytes = blocks << md->block_bits;
382 spin_lock_irq(&md->lock);
383 ret = end_that_request_chunk(req, 1, bytes);
384 spin_unlock_irq(&md->lock);
386 } else if (rq_data_dir(req) != READ &&
387 (card->host->caps & MMC_CAP_MULTIWRITE)) {
388 spin_lock_irq(&md->lock);
389 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
390 spin_unlock_irq(&md->lock);
393 mmc_release_host(card->host);
395 spin_lock_irq(&md->lock);
397 ret = end_that_request_chunk(req, 0,
398 req->current_nr_sectors << 9);
401 add_disk_randomness(req->rq_disk);
402 blkdev_dequeue_request(req);
403 end_that_request_last(req, 0);
404 spin_unlock_irq(&md->lock);
409 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
411 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
413 static inline int mmc_blk_readonly(struct mmc_card *card)
415 return mmc_card_readonly(card) ||
416 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
419 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
421 struct mmc_blk_data *md;
424 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
425 if (devidx >= MMC_NUM_MINORS)
426 return ERR_PTR(-ENOSPC);
427 __set_bit(devidx, dev_use);
429 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
437 * Set the read-only status based on the supported commands
438 * and the write protect switch.
440 md->read_only = mmc_blk_readonly(card);
443 * Both SD and MMC specifications state (although a bit
444 * unclearly in the MMC case) that a block size of 512
445 * bytes must always be supported by the card.
449 md->disk = alloc_disk(1 << MMC_SHIFT);
450 if (md->disk == NULL) {
455 spin_lock_init(&md->lock);
458 ret = mmc_init_queue(&md->queue, card, &md->lock);
462 md->queue.issue_fn = mmc_blk_issue_rq;
465 md->disk->major = MMC_BLOCK_MAJOR;
466 md->disk->first_minor = devidx << MMC_SHIFT;
467 md->disk->fops = &mmc_bdops;
468 md->disk->private_data = md;
469 md->disk->queue = md->queue.queue;
470 md->disk->driverfs_dev = &card->dev;
473 * As discussed on lkml, GENHD_FL_REMOVABLE should:
475 * - be set for removable media with permanent block devices
476 * - be unset for removable block devices with permanent media
478 * Since MMC block devices clearly fall under the second
479 * case, we do not set GENHD_FL_REMOVABLE. Userspace
480 * should use the block device creation/destruction hotplug
481 * messages to tell when the card is present.
484 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
486 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
488 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
490 * The EXT_CSD sector count is in number or 512 byte
493 set_capacity(md->disk, card->ext_csd.sectors);
496 * The CSD capacity field is in units of read_blkbits.
497 * set_capacity takes units of 512 bytes.
499 set_capacity(md->disk,
500 card->csd.capacity << (card->csd.read_blkbits - 9));
513 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
515 struct mmc_command cmd;
518 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
519 if (mmc_card_blockaddr(card))
522 mmc_claim_host(card->host);
523 cmd.opcode = MMC_SET_BLOCKLEN;
524 cmd.arg = 1 << md->block_bits;
525 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
526 err = mmc_wait_for_cmd(card->host, &cmd, 5);
527 mmc_release_host(card->host);
530 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
531 md->disk->disk_name, cmd.arg, err);
538 static int mmc_blk_probe(struct mmc_card *card)
540 struct mmc_blk_data *md;
544 * Check that the card supports the command class(es) we need.
546 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
549 md = mmc_blk_alloc(card);
553 err = mmc_blk_set_blksize(md, card);
557 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
558 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
559 (unsigned long long)(get_capacity(md->disk) >> 1),
560 md->read_only ? "(ro)" : "");
562 mmc_set_drvdata(card, md);
572 static void mmc_blk_remove(struct mmc_card *card)
574 struct mmc_blk_data *md = mmc_get_drvdata(card);
579 /* Stop new requests from getting into the queue */
580 del_gendisk(md->disk);
582 /* Then flush out any already in there */
583 mmc_cleanup_queue(&md->queue);
585 devidx = md->disk->first_minor >> MMC_SHIFT;
586 __clear_bit(devidx, dev_use);
590 mmc_set_drvdata(card, NULL);
594 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
596 struct mmc_blk_data *md = mmc_get_drvdata(card);
599 mmc_queue_suspend(&md->queue);
604 static int mmc_blk_resume(struct mmc_card *card)
606 struct mmc_blk_data *md = mmc_get_drvdata(card);
609 mmc_blk_set_blksize(md, card);
610 mmc_queue_resume(&md->queue);
615 #define mmc_blk_suspend NULL
616 #define mmc_blk_resume NULL
619 static struct mmc_driver mmc_driver = {
623 .probe = mmc_blk_probe,
624 .remove = mmc_blk_remove,
625 .suspend = mmc_blk_suspend,
626 .resume = mmc_blk_resume,
629 static int __init mmc_blk_init(void)
633 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
637 return mmc_register_driver(&mmc_driver);
643 static void __exit mmc_blk_exit(void)
645 mmc_unregister_driver(&mmc_driver);
646 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
649 module_init(mmc_blk_init);
650 module_exit(mmc_blk_exit);
652 MODULE_LICENSE("GPL");
653 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");