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>
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
47 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
49 static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
52 * There is one mmc_blk_data per slot.
57 struct mmc_queue queue;
60 unsigned int block_bits;
61 unsigned int read_only;
64 static DEFINE_MUTEX(open_lock);
66 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
68 struct mmc_blk_data *md;
70 mutex_lock(&open_lock);
71 md = disk->private_data;
72 if (md && md->usage == 0)
76 mutex_unlock(&open_lock);
81 static void mmc_blk_put(struct mmc_blk_data *md)
83 mutex_lock(&open_lock);
86 int devidx = md->disk->first_minor >> MMC_SHIFT;
87 __clear_bit(devidx, dev_use);
92 mutex_unlock(&open_lock);
95 static int mmc_blk_open(struct inode *inode, struct file *filp)
97 struct mmc_blk_data *md;
100 md = mmc_blk_get(inode->i_bdev->bd_disk);
103 check_disk_change(inode->i_bdev);
106 if ((filp->f_mode & FMODE_WRITE) && md->read_only)
113 static int mmc_blk_release(struct inode *inode, struct file *filp)
115 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
122 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
124 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
130 static struct block_device_operations mmc_bdops = {
131 .open = mmc_blk_open,
132 .release = mmc_blk_release,
133 .getgeo = mmc_blk_getgeo,
134 .owner = THIS_MODULE,
137 struct mmc_blk_request {
138 struct mmc_request mrq;
139 struct mmc_command cmd;
140 struct mmc_command stop;
141 struct mmc_data data;
144 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
149 struct mmc_request mrq;
150 struct mmc_command cmd;
151 struct mmc_data data;
152 unsigned int timeout_us;
154 struct scatterlist sg;
156 memset(&cmd, 0, sizeof(struct mmc_command));
158 cmd.opcode = MMC_APP_CMD;
159 cmd.arg = card->rca << 16;
160 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
162 err = mmc_wait_for_cmd(card->host, &cmd, 0);
165 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
168 memset(&cmd, 0, sizeof(struct mmc_command));
170 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
172 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
174 memset(&data, 0, sizeof(struct mmc_data));
176 data.timeout_ns = card->csd.tacc_ns * 100;
177 data.timeout_clks = card->csd.tacc_clks * 100;
179 timeout_us = data.timeout_ns / 1000;
180 timeout_us += data.timeout_clks * 1000 /
181 (card->host->ios.clock / 1000);
183 if (timeout_us > 100000) {
184 data.timeout_ns = 100000000;
185 data.timeout_clks = 0;
190 data.flags = MMC_DATA_READ;
194 memset(&mrq, 0, sizeof(struct mmc_request));
199 sg_init_one(&sg, &blocks, 4);
201 mmc_wait_for_req(card->host, &mrq);
203 if (cmd.error || data.error)
206 blocks = ntohl(blocks);
211 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
213 struct mmc_blk_data *md = mq->data;
214 struct mmc_card *card = md->queue.card;
215 struct mmc_blk_request brq;
216 int ret = 1, sg_pos, data_size;
218 mmc_claim_host(card->host);
221 struct mmc_command cmd;
222 u32 readcmd, writecmd;
224 memset(&brq, 0, sizeof(struct mmc_blk_request));
225 brq.mrq.cmd = &brq.cmd;
226 brq.mrq.data = &brq.data;
228 brq.cmd.arg = req->sector;
229 if (!mmc_card_blockaddr(card))
231 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
232 brq.data.blksz = 1 << md->block_bits;
233 brq.stop.opcode = MMC_STOP_TRANSMISSION;
235 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
236 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
237 if (brq.data.blocks > card->host->max_blk_count)
238 brq.data.blocks = card->host->max_blk_count;
240 if (brq.data.blocks > 1) {
241 /* SPI multiblock writes terminate using a special
242 * token, not a STOP_TRANSMISSION request.
244 if (!mmc_host_is_spi(card->host)
245 || rq_data_dir(req) == READ)
246 brq.mrq.stop = &brq.stop;
247 readcmd = MMC_READ_MULTIPLE_BLOCK;
248 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
251 readcmd = MMC_READ_SINGLE_BLOCK;
252 writecmd = MMC_WRITE_BLOCK;
255 if (rq_data_dir(req) == READ) {
256 brq.cmd.opcode = readcmd;
257 brq.data.flags |= MMC_DATA_READ;
259 brq.cmd.opcode = writecmd;
260 brq.data.flags |= MMC_DATA_WRITE;
263 mmc_set_data_timeout(&brq.data, card);
265 brq.data.sg = mq->sg;
266 brq.data.sg_len = mmc_queue_map_sg(mq);
268 mmc_queue_bounce_pre(mq);
270 if (brq.data.blocks !=
271 (req->nr_sectors >> (md->block_bits - 9))) {
272 data_size = brq.data.blocks * brq.data.blksz;
273 for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
274 data_size -= mq->sg[sg_pos].length;
275 if (data_size <= 0) {
276 mq->sg[sg_pos].length += data_size;
281 brq.data.sg_len = sg_pos;
284 mmc_wait_for_req(card->host, &brq.mrq);
286 mmc_queue_bounce_post(mq);
289 * Check for errors here, but don't jump to cmd_err
290 * until later as we need to wait for the card to leave
291 * programming mode even when things go wrong.
294 printk(KERN_ERR "%s: error %d sending read/write command\n",
295 req->rq_disk->disk_name, brq.cmd.error);
298 if (brq.data.error) {
299 printk(KERN_ERR "%s: error %d transferring data\n",
300 req->rq_disk->disk_name, brq.data.error);
303 if (brq.stop.error) {
304 printk(KERN_ERR "%s: error %d sending stop command\n",
305 req->rq_disk->disk_name, brq.stop.error);
308 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
312 cmd.opcode = MMC_SEND_STATUS;
313 cmd.arg = card->rca << 16;
314 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
315 err = mmc_wait_for_cmd(card->host, &cmd, 5);
317 printk(KERN_ERR "%s: error %d requesting status\n",
318 req->rq_disk->disk_name, err);
322 * Some cards mishandle the status bits,
323 * so make sure to check both the busy
324 * indication and the card state.
326 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
327 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
330 if (cmd.resp[0] & ~0x00000900)
331 printk(KERN_ERR "%s: status = %08x\n",
332 req->rq_disk->disk_name, cmd.resp[0]);
333 if (mmc_decode_status(cmd.resp))
338 if (brq.cmd.error || brq.data.error || brq.stop.error)
342 * A block was successfully transferred.
344 spin_lock_irq(&md->lock);
345 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
346 spin_unlock_irq(&md->lock);
349 mmc_release_host(card->host);
355 * If this is an SD card and we're writing, we can first
356 * mark the known good sectors as ok.
358 * If the card is not SD, we can still ok written sectors
359 * as reported by the controller (which might be less than
360 * the real number of written sectors, but never more).
362 * For reads we just fail the entire chunk as that should
363 * be safe in all cases.
365 if (rq_data_dir(req) != READ) {
366 if (mmc_card_sd(card)) {
370 blocks = mmc_sd_num_wr_blocks(card);
371 if (blocks != (u32)-1) {
372 if (card->csd.write_partial)
373 bytes = blocks << md->block_bits;
376 spin_lock_irq(&md->lock);
377 ret = __blk_end_request(req, 0, bytes);
378 spin_unlock_irq(&md->lock);
381 spin_lock_irq(&md->lock);
382 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
383 spin_unlock_irq(&md->lock);
387 mmc_release_host(card->host);
389 spin_lock_irq(&md->lock);
391 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
392 spin_unlock_irq(&md->lock);
398 static inline int mmc_blk_readonly(struct mmc_card *card)
400 return mmc_card_readonly(card) ||
401 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
404 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
406 struct mmc_blk_data *md;
409 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
410 if (devidx >= MMC_NUM_MINORS)
411 return ERR_PTR(-ENOSPC);
412 __set_bit(devidx, dev_use);
414 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
422 * Set the read-only status based on the supported commands
423 * and the write protect switch.
425 md->read_only = mmc_blk_readonly(card);
428 * Both SD and MMC specifications state (although a bit
429 * unclearly in the MMC case) that a block size of 512
430 * bytes must always be supported by the card.
434 md->disk = alloc_disk(1 << MMC_SHIFT);
435 if (md->disk == NULL) {
440 spin_lock_init(&md->lock);
443 ret = mmc_init_queue(&md->queue, card, &md->lock);
447 md->queue.issue_fn = mmc_blk_issue_rq;
450 md->disk->major = MMC_BLOCK_MAJOR;
451 md->disk->first_minor = devidx << MMC_SHIFT;
452 md->disk->fops = &mmc_bdops;
453 md->disk->private_data = md;
454 md->disk->queue = md->queue.queue;
455 md->disk->driverfs_dev = &card->dev;
458 * As discussed on lkml, GENHD_FL_REMOVABLE should:
460 * - be set for removable media with permanent block devices
461 * - be unset for removable block devices with permanent media
463 * Since MMC block devices clearly fall under the second
464 * case, we do not set GENHD_FL_REMOVABLE. Userspace
465 * should use the block device creation/destruction hotplug
466 * messages to tell when the card is present.
469 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
471 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
473 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
475 * The EXT_CSD sector count is in number or 512 byte
478 set_capacity(md->disk, card->ext_csd.sectors);
481 * The CSD capacity field is in units of read_blkbits.
482 * set_capacity takes units of 512 bytes.
484 set_capacity(md->disk,
485 card->csd.capacity << (card->csd.read_blkbits - 9));
498 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
500 struct mmc_command cmd;
503 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
504 if (mmc_card_blockaddr(card))
507 mmc_claim_host(card->host);
508 cmd.opcode = MMC_SET_BLOCKLEN;
509 cmd.arg = 1 << md->block_bits;
510 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
511 err = mmc_wait_for_cmd(card->host, &cmd, 5);
512 mmc_release_host(card->host);
515 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
516 md->disk->disk_name, cmd.arg, err);
523 static int mmc_blk_probe(struct mmc_card *card)
525 struct mmc_blk_data *md;
529 * Check that the card supports the command class(es) we need.
531 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
534 md = mmc_blk_alloc(card);
538 err = mmc_blk_set_blksize(md, card);
542 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
543 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
544 (unsigned long long)(get_capacity(md->disk) >> 1),
545 md->read_only ? "(ro)" : "");
547 mmc_set_drvdata(card, md);
557 static void mmc_blk_remove(struct mmc_card *card)
559 struct mmc_blk_data *md = mmc_get_drvdata(card);
562 /* Stop new requests from getting into the queue */
563 del_gendisk(md->disk);
565 /* Then flush out any already in there */
566 mmc_cleanup_queue(&md->queue);
570 mmc_set_drvdata(card, NULL);
574 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
576 struct mmc_blk_data *md = mmc_get_drvdata(card);
579 mmc_queue_suspend(&md->queue);
584 static int mmc_blk_resume(struct mmc_card *card)
586 struct mmc_blk_data *md = mmc_get_drvdata(card);
589 mmc_blk_set_blksize(md, card);
590 mmc_queue_resume(&md->queue);
595 #define mmc_blk_suspend NULL
596 #define mmc_blk_resume NULL
599 static struct mmc_driver mmc_driver = {
603 .probe = mmc_blk_probe,
604 .remove = mmc_blk_remove,
605 .suspend = mmc_blk_suspend,
606 .resume = mmc_blk_resume,
609 static int __init mmc_blk_init(void)
613 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
617 return mmc_register_driver(&mmc_driver);
623 static void __exit mmc_blk_exit(void)
625 mmc_unregister_driver(&mmc_driver);
626 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
629 module_init(mmc_blk_init);
630 module_exit(mmc_blk_exit);
632 MODULE_LICENSE("GPL");
633 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");