2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
6 * Use consistent with the GNU GPL is permitted,
7 * provided that this copyright notice is
8 * preserved in its entirety in all copies and derived works.
10 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
11 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
12 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 * Author: Andrew Christian
19 #include <linux/moduleparam.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
23 #include <linux/sched.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/devfs_fs_kernel.h>
32 #include <linux/mmc/card.h>
33 #include <linux/mmc/protocol.h>
34 #include <linux/mmc/host.h>
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
39 #include "mmc_queue.h"
42 * 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 suspended;
61 static DECLARE_MUTEX(open_lock);
63 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
65 struct mmc_blk_data *md;
68 md = disk->private_data;
69 if (md && md->usage == 0)
78 static void mmc_blk_put(struct mmc_blk_data *md)
84 mmc_cleanup_queue(&md->queue);
90 static int mmc_blk_open(struct inode *inode, struct file *filp)
92 struct mmc_blk_data *md;
95 md = mmc_blk_get(inode->i_bdev->bd_disk);
98 check_disk_change(inode->i_bdev);
101 if ((filp->f_mode & FMODE_WRITE) &&
102 mmc_card_readonly(md->queue.card))
109 static int mmc_blk_release(struct inode *inode, struct file *filp)
111 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
118 mmc_blk_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
120 struct block_device *bdev = inode->i_bdev;
122 if (cmd == HDIO_GETGEO) {
123 struct hd_geometry geo;
125 memset(&geo, 0, sizeof(struct hd_geometry));
127 geo.cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
130 geo.start = get_start_sect(bdev);
132 return copy_to_user((void __user *)arg, &geo, sizeof(geo))
139 static struct block_device_operations mmc_bdops = {
140 .open = mmc_blk_open,
141 .release = mmc_blk_release,
142 .ioctl = mmc_blk_ioctl,
143 .owner = THIS_MODULE,
146 struct mmc_blk_request {
147 struct mmc_request mrq;
148 struct mmc_command cmd;
149 struct mmc_command stop;
150 struct mmc_data data;
153 static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
155 struct mmc_blk_data *md = mq->data;
156 int stat = BLKPREP_OK;
159 * If we have no device, we haven't finished initialising.
161 if (!md || !mq->card) {
162 printk(KERN_ERR "%s: killing request - no device/host\n",
163 req->rq_disk->disk_name);
168 blk_plug_device(md->queue.queue);
169 stat = BLKPREP_DEFER;
173 * Check for excessive requests.
175 if (req->sector + req->nr_sectors > get_capacity(req->rq_disk)) {
176 printk("bad request size\n");
183 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
185 struct mmc_blk_data *md = mq->data;
186 struct mmc_card *card = md->queue.card;
189 #ifdef CONFIG_MMC_BULKTRANSFER
193 if (mmc_card_claim_host(card))
196 #ifdef CONFIG_MMC_BULKTRANSFER
198 * We first try transfering multiple blocks. If this fails
199 * we fall back to single block transfers.
201 * This gives us good performance when all is well and the
202 * possibility to determine which sector fails when all
209 struct mmc_blk_request brq;
210 struct mmc_command cmd;
212 memset(&brq, 0, sizeof(struct mmc_blk_request));
213 brq.mrq.cmd = &brq.cmd;
214 brq.mrq.data = &brq.data;
216 brq.cmd.arg = req->sector << 9;
217 brq.cmd.flags = MMC_RSP_R1;
218 brq.data.timeout_ns = card->csd.tacc_ns * 10;
219 brq.data.timeout_clks = card->csd.tacc_clks * 10;
220 brq.data.blksz_bits = md->block_bits;
221 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
222 brq.stop.opcode = MMC_STOP_TRANSMISSION;
224 brq.stop.flags = MMC_RSP_R1B;
226 #ifdef CONFIG_MMC_BULKTRANSFER
228 * A multi-block transfer failed. Falling back to single
236 * Writes are done one sector at a time.
238 if (rq_data_dir(req) != READ)
244 if (rq_data_dir(req) == READ) {
245 brq.cmd.opcode = brq.data.blocks > 1 ? MMC_READ_MULTIPLE_BLOCK : MMC_READ_SINGLE_BLOCK;
246 brq.data.flags |= MMC_DATA_READ;
248 brq.cmd.opcode = brq.data.blocks > 1 ? MMC_WRITE_MULTIPLE_BLOCK :
250 brq.cmd.flags = MMC_RSP_R1B;
251 brq.data.flags |= MMC_DATA_WRITE;
253 brq.mrq.stop = brq.data.blocks > 1 ? &brq.stop : NULL;
255 brq.data.sg = mq->sg;
256 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
258 mmc_wait_for_req(card->host, &brq.mrq);
260 printk(KERN_ERR "%s: error %d sending read/write command\n",
261 req->rq_disk->disk_name, brq.cmd.error);
265 if (brq.data.error) {
266 printk(KERN_ERR "%s: error %d transferring data\n",
267 req->rq_disk->disk_name, brq.data.error);
271 if (brq.stop.error) {
272 printk(KERN_ERR "%s: error %d sending stop command\n",
273 req->rq_disk->disk_name, brq.stop.error);
277 /* No need to check card status after a read */
278 if (rq_data_dir(req) == READ)
284 cmd.opcode = MMC_SEND_STATUS;
285 cmd.arg = card->rca << 16;
286 cmd.flags = MMC_RSP_R1;
287 err = mmc_wait_for_cmd(card->host, &cmd, 5);
289 printk(KERN_ERR "%s: error %d requesting status\n",
290 req->rq_disk->disk_name, err);
293 #ifdef CONFIG_MMC_BLOCK_BROKEN_RFD
294 /* Work-around for broken cards setting READY_FOR_DATA
295 * when not actually ready.
297 if (R1_CURRENT_STATE(cmd.resp[0]) == 7)
298 cmd.resp[0] &= ~R1_READY_FOR_DATA;
300 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
303 if (cmd.resp[0] & ~0x00000900)
304 printk(KERN_ERR "%s: status = %08x\n",
305 req->rq_disk->disk_name, cmd.resp[0]);
306 if (mmc_decode_status(cmd.resp))
313 * A block was successfully transferred.
315 spin_lock_irq(&md->lock);
316 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
319 * The whole request completed successfully.
321 add_disk_randomness(req->rq_disk);
322 blkdev_dequeue_request(req);
323 end_that_request_last(req);
325 spin_unlock_irq(&md->lock);
327 #ifdef CONFIG_MMC_BULKTRANSFER
329 * Go back to bulk mode if in failsafe mode.
338 #ifdef CONFIG_MMC_BULKTRANSFER
349 mmc_card_release_host(card);
354 mmc_card_release_host(card);
357 * This is a little draconian, but until we get proper
358 * error handling sorted out here, its the best we can
359 * do - especially as some hosts have no idea how much
360 * data was transferred before the error occurred.
362 spin_lock_irq(&md->lock);
364 ret = end_that_request_chunk(req, 0,
365 req->current_nr_sectors << 9);
368 add_disk_randomness(req->rq_disk);
369 blkdev_dequeue_request(req);
370 end_that_request_last(req);
371 spin_unlock_irq(&md->lock);
373 /* If a command fails, the card might be removed. */
374 mmc_detect_change(card->host);
379 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
381 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
383 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
385 struct mmc_blk_data *md;
388 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
389 if (devidx >= MMC_NUM_MINORS)
390 return ERR_PTR(-ENOSPC);
391 __set_bit(devidx, dev_use);
393 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
395 memset(md, 0, sizeof(struct mmc_blk_data));
397 md->disk = alloc_disk(1 << MMC_SHIFT);
398 if (md->disk == NULL) {
400 md = ERR_PTR(-ENOMEM);
404 spin_lock_init(&md->lock);
407 ret = mmc_init_queue(&md->queue, card, &md->lock);
414 md->queue.prep_fn = mmc_blk_prep_rq;
415 md->queue.issue_fn = mmc_blk_issue_rq;
418 md->disk->major = major;
419 md->disk->first_minor = devidx << MMC_SHIFT;
420 md->disk->fops = &mmc_bdops;
421 md->disk->private_data = md;
422 md->disk->queue = md->queue.queue;
423 md->disk->driverfs_dev = &card->dev;
426 * As discussed on lkml, GENHD_FL_REMOVABLE should:
428 * - be set for removable media with permanent block devices
429 * - be unset for removable block devices with permanent media
431 * Since MMC block devices clearly fall under the second
432 * case, we do not set GENHD_FL_REMOVABLE. Userspace
433 * should use the block device creation/destruction hotplug
434 * messages to tell when the card is present.
437 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
438 sprintf(md->disk->devfs_name, "mmc/blk%d", devidx);
440 md->block_bits = card->csd.read_blkbits;
442 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
443 set_capacity(md->disk, card->csd.capacity);
450 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
452 struct mmc_command cmd;
455 mmc_card_claim_host(card);
456 cmd.opcode = MMC_SET_BLOCKLEN;
457 cmd.arg = 1 << card->csd.read_blkbits;
458 cmd.flags = MMC_RSP_R1;
459 err = mmc_wait_for_cmd(card->host, &cmd, 5);
460 mmc_card_release_host(card);
463 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
464 md->disk->disk_name, cmd.arg, err);
471 static int mmc_blk_probe(struct mmc_card *card)
473 struct mmc_blk_data *md;
477 * Check that the card supports the command class(es) we need.
479 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
482 if (card->csd.read_blkbits < 9) {
483 printk(KERN_WARNING "%s: read blocksize too small (%u)\n",
484 mmc_card_id(card), 1 << card->csd.read_blkbits);
488 md = mmc_blk_alloc(card);
492 err = mmc_blk_set_blksize(md, card);
496 printk(KERN_INFO "%s: %s %s %dKiB %s\n",
497 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
498 (card->csd.capacity << card->csd.read_blkbits) / 1024,
499 mmc_card_readonly(card)?"(ro)":"");
501 mmc_set_drvdata(card, md);
511 static void mmc_blk_remove(struct mmc_card *card)
513 struct mmc_blk_data *md = mmc_get_drvdata(card);
518 del_gendisk(md->disk);
521 * I think this is needed.
523 md->disk->queue = NULL;
525 devidx = md->disk->first_minor >> MMC_SHIFT;
526 __clear_bit(devidx, dev_use);
530 mmc_set_drvdata(card, NULL);
534 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
536 struct mmc_blk_data *md = mmc_get_drvdata(card);
539 mmc_queue_suspend(&md->queue);
544 static int mmc_blk_resume(struct mmc_card *card)
546 struct mmc_blk_data *md = mmc_get_drvdata(card);
549 mmc_blk_set_blksize(md, card);
550 mmc_queue_resume(&md->queue);
555 #define mmc_blk_suspend NULL
556 #define mmc_blk_resume NULL
559 static struct mmc_driver mmc_driver = {
563 .probe = mmc_blk_probe,
564 .remove = mmc_blk_remove,
565 .suspend = mmc_blk_suspend,
566 .resume = mmc_blk_resume,
569 static int __init mmc_blk_init(void)
573 res = register_blkdev(major, "mmc");
575 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
583 return mmc_register_driver(&mmc_driver);
589 static void __exit mmc_blk_exit(void)
591 mmc_unregister_driver(&mmc_driver);
593 unregister_blkdev(major, "mmc");
596 module_init(mmc_blk_init);
597 module_exit(mmc_blk_exit);
599 MODULE_LICENSE("GPL");
600 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
602 module_param(major, int, 0444);
603 MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");