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/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
35 #include <linux/mmc/card.h>
36 #include <linux/mmc/host.h>
37 #include <linux/mmc/mmc.h>
38 #include <linux/mmc/sd.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
45 MODULE_ALIAS("mmc:block");
48 * max 8 partitions per card
51 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
53 static DECLARE_BITMAP(dev_use
, MMC_NUM_MINORS
);
56 * There is one mmc_blk_data per slot.
61 struct mmc_queue queue
;
64 unsigned int read_only
;
67 static DEFINE_MUTEX(open_lock
);
69 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
71 struct mmc_blk_data
*md
;
73 mutex_lock(&open_lock
);
74 md
= disk
->private_data
;
75 if (md
&& md
->usage
== 0)
79 mutex_unlock(&open_lock
);
84 static void mmc_blk_put(struct mmc_blk_data
*md
)
86 mutex_lock(&open_lock
);
89 int devmaj
= MAJOR(disk_devt(md
->disk
));
90 int devidx
= MINOR(disk_devt(md
->disk
)) >> MMC_SHIFT
;
93 devidx
= md
->disk
->first_minor
>> MMC_SHIFT
;
95 blk_cleanup_queue(md
->queue
.queue
);
97 __clear_bit(devidx
, dev_use
);
102 mutex_unlock(&open_lock
);
105 static int mmc_blk_open(struct block_device
*bdev
, fmode_t mode
)
107 struct mmc_blk_data
*md
= mmc_blk_get(bdev
->bd_disk
);
112 check_disk_change(bdev
);
115 if ((mode
& FMODE_WRITE
) && md
->read_only
) {
124 static int mmc_blk_release(struct gendisk
*disk
, fmode_t mode
)
126 struct mmc_blk_data
*md
= disk
->private_data
;
133 mmc_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
135 geo
->cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
141 static const struct block_device_operations mmc_bdops
= {
142 .open
= mmc_blk_open
,
143 .release
= mmc_blk_release
,
144 .getgeo
= mmc_blk_getgeo
,
145 .owner
= THIS_MODULE
,
148 struct mmc_blk_request
{
149 struct mmc_request mrq
;
150 struct mmc_command cmd
;
151 struct mmc_command stop
;
152 struct mmc_data data
;
155 static u32
mmc_sd_num_wr_blocks(struct mmc_card
*card
)
161 struct mmc_request mrq
;
162 struct mmc_command cmd
;
163 struct mmc_data data
;
164 unsigned int timeout_us
;
166 struct scatterlist sg
;
168 memset(&cmd
, 0, sizeof(struct mmc_command
));
170 cmd
.opcode
= MMC_APP_CMD
;
171 cmd
.arg
= card
->rca
<< 16;
172 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
174 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
177 if (!mmc_host_is_spi(card
->host
) && !(cmd
.resp
[0] & R1_APP_CMD
))
180 memset(&cmd
, 0, sizeof(struct mmc_command
));
182 cmd
.opcode
= SD_APP_SEND_NUM_WR_BLKS
;
184 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
186 memset(&data
, 0, sizeof(struct mmc_data
));
188 data
.timeout_ns
= card
->csd
.tacc_ns
* 100;
189 data
.timeout_clks
= card
->csd
.tacc_clks
* 100;
191 timeout_us
= data
.timeout_ns
/ 1000;
192 timeout_us
+= data
.timeout_clks
* 1000 /
193 (card
->host
->ios
.clock
/ 1000);
195 if (timeout_us
> 100000) {
196 data
.timeout_ns
= 100000000;
197 data
.timeout_clks
= 0;
202 data
.flags
= MMC_DATA_READ
;
206 memset(&mrq
, 0, sizeof(struct mmc_request
));
211 blocks
= kmalloc(4, GFP_KERNEL
);
215 sg_init_one(&sg
, blocks
, 4);
217 mmc_wait_for_req(card
->host
, &mrq
);
219 result
= ntohl(*blocks
);
222 if (cmd
.error
|| data
.error
)
228 static u32
get_card_status(struct mmc_card
*card
, struct request
*req
)
230 struct mmc_command cmd
;
233 memset(&cmd
, 0, sizeof(struct mmc_command
));
234 cmd
.opcode
= MMC_SEND_STATUS
;
235 if (!mmc_host_is_spi(card
->host
))
236 cmd
.arg
= card
->rca
<< 16;
237 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
238 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
240 printk(KERN_ERR
"%s: error %d sending status comand",
241 req
->rq_disk
->disk_name
, err
);
245 static int mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
247 struct mmc_blk_data
*md
= mq
->data
;
248 struct mmc_card
*card
= md
->queue
.card
;
249 struct mmc_blk_request brq
;
250 int ret
= 1, disable_multi
= 0;
252 mmc_claim_host(card
->host
);
255 struct mmc_command cmd
;
256 u32 readcmd
, writecmd
, status
= 0;
258 memset(&brq
, 0, sizeof(struct mmc_blk_request
));
259 brq
.mrq
.cmd
= &brq
.cmd
;
260 brq
.mrq
.data
= &brq
.data
;
262 brq
.cmd
.arg
= blk_rq_pos(req
);
263 if (!mmc_card_blockaddr(card
))
265 brq
.cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
266 brq
.data
.blksz
= 512;
267 brq
.stop
.opcode
= MMC_STOP_TRANSMISSION
;
269 brq
.stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
270 brq
.data
.blocks
= blk_rq_sectors(req
);
273 * The block layer doesn't support all sector count
274 * restrictions, so we need to be prepared for too big
277 if (brq
.data
.blocks
> card
->host
->max_blk_count
)
278 brq
.data
.blocks
= card
->host
->max_blk_count
;
281 * After a read error, we redo the request one sector at a time
282 * in order to accurately determine which sectors can be read
285 if (disable_multi
&& brq
.data
.blocks
> 1)
288 if (brq
.data
.blocks
> 1) {
289 /* SPI multiblock writes terminate using a special
290 * token, not a STOP_TRANSMISSION request.
292 if (!mmc_host_is_spi(card
->host
)
293 || rq_data_dir(req
) == READ
)
294 brq
.mrq
.stop
= &brq
.stop
;
295 readcmd
= MMC_READ_MULTIPLE_BLOCK
;
296 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
299 readcmd
= MMC_READ_SINGLE_BLOCK
;
300 writecmd
= MMC_WRITE_BLOCK
;
303 if (rq_data_dir(req
) == READ
) {
304 brq
.cmd
.opcode
= readcmd
;
305 brq
.data
.flags
|= MMC_DATA_READ
;
307 brq
.cmd
.opcode
= writecmd
;
308 brq
.data
.flags
|= MMC_DATA_WRITE
;
311 mmc_set_data_timeout(&brq
.data
, card
);
313 brq
.data
.sg
= mq
->sg
;
314 brq
.data
.sg_len
= mmc_queue_map_sg(mq
);
317 * Adjust the sg list so it is the same size as the
320 if (brq
.data
.blocks
!= blk_rq_sectors(req
)) {
321 int i
, data_size
= brq
.data
.blocks
<< 9;
322 struct scatterlist
*sg
;
324 for_each_sg(brq
.data
.sg
, sg
, brq
.data
.sg_len
, i
) {
325 data_size
-= sg
->length
;
326 if (data_size
<= 0) {
327 sg
->length
+= data_size
;
335 mmc_queue_bounce_pre(mq
);
337 mmc_wait_for_req(card
->host
, &brq
.mrq
);
339 mmc_queue_bounce_post(mq
);
342 * Check for errors here, but don't jump to cmd_err
343 * until later as we need to wait for the card to leave
344 * programming mode even when things go wrong.
346 if (brq
.cmd
.error
|| brq
.data
.error
|| brq
.stop
.error
) {
347 if (brq
.data
.blocks
> 1 && rq_data_dir(req
) == READ
) {
348 /* Redo read one sector at a time */
349 printk(KERN_WARNING
"%s: retrying using single "
350 "block read\n", req
->rq_disk
->disk_name
);
354 status
= get_card_status(card
, req
);
358 printk(KERN_ERR
"%s: error %d sending read/write "
359 "command, response %#x, card status %#x\n",
360 req
->rq_disk
->disk_name
, brq
.cmd
.error
,
361 brq
.cmd
.resp
[0], status
);
364 if (brq
.data
.error
) {
365 if (brq
.data
.error
== -ETIMEDOUT
&& brq
.mrq
.stop
)
366 /* 'Stop' response contains card status */
367 status
= brq
.mrq
.stop
->resp
[0];
368 printk(KERN_ERR
"%s: error %d transferring data,"
369 " sector %u, nr %u, card status %#x\n",
370 req
->rq_disk
->disk_name
, brq
.data
.error
,
371 (unsigned)blk_rq_pos(req
),
372 (unsigned)blk_rq_sectors(req
), status
);
375 if (brq
.stop
.error
) {
376 printk(KERN_ERR
"%s: error %d sending stop command, "
377 "response %#x, card status %#x\n",
378 req
->rq_disk
->disk_name
, brq
.stop
.error
,
379 brq
.stop
.resp
[0], status
);
382 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
) {
386 cmd
.opcode
= MMC_SEND_STATUS
;
387 cmd
.arg
= card
->rca
<< 16;
388 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
389 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
391 printk(KERN_ERR
"%s: error %d requesting status\n",
392 req
->rq_disk
->disk_name
, err
);
396 * Some cards mishandle the status bits,
397 * so make sure to check both the busy
398 * indication and the card state.
400 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
) ||
401 (R1_CURRENT_STATE(cmd
.resp
[0]) == 7));
404 if (cmd
.resp
[0] & ~0x00000900)
405 printk(KERN_ERR
"%s: status = %08x\n",
406 req
->rq_disk
->disk_name
, cmd
.resp
[0]);
407 if (mmc_decode_status(cmd
.resp
))
412 if (brq
.cmd
.error
|| brq
.stop
.error
|| brq
.data
.error
) {
413 if (rq_data_dir(req
) == READ
) {
415 * After an error, we redo I/O one sector at a
416 * time, so we only reach here after trying to
417 * read a single sector.
419 spin_lock_irq(&md
->lock
);
420 ret
= __blk_end_request(req
, -EIO
, brq
.data
.blksz
);
421 spin_unlock_irq(&md
->lock
);
428 * A block was successfully transferred.
430 spin_lock_irq(&md
->lock
);
431 ret
= __blk_end_request(req
, 0, brq
.data
.bytes_xfered
);
432 spin_unlock_irq(&md
->lock
);
435 mmc_release_host(card
->host
);
441 * If this is an SD card and we're writing, we can first
442 * mark the known good sectors as ok.
444 * If the card is not SD, we can still ok written sectors
445 * as reported by the controller (which might be less than
446 * the real number of written sectors, but never more).
448 if (mmc_card_sd(card
)) {
451 blocks
= mmc_sd_num_wr_blocks(card
);
452 if (blocks
!= (u32
)-1) {
453 spin_lock_irq(&md
->lock
);
454 ret
= __blk_end_request(req
, 0, blocks
<< 9);
455 spin_unlock_irq(&md
->lock
);
458 spin_lock_irq(&md
->lock
);
459 ret
= __blk_end_request(req
, 0, brq
.data
.bytes_xfered
);
460 spin_unlock_irq(&md
->lock
);
463 mmc_release_host(card
->host
);
465 spin_lock_irq(&md
->lock
);
467 ret
= __blk_end_request(req
, -EIO
, blk_rq_cur_bytes(req
));
468 spin_unlock_irq(&md
->lock
);
474 static inline int mmc_blk_readonly(struct mmc_card
*card
)
476 return mmc_card_readonly(card
) ||
477 !(card
->csd
.cmdclass
& CCC_BLOCK_WRITE
);
480 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
482 struct mmc_blk_data
*md
;
485 devidx
= find_first_zero_bit(dev_use
, MMC_NUM_MINORS
);
486 if (devidx
>= MMC_NUM_MINORS
)
487 return ERR_PTR(-ENOSPC
);
488 __set_bit(devidx
, dev_use
);
490 md
= kzalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
498 * Set the read-only status based on the supported commands
499 * and the write protect switch.
501 md
->read_only
= mmc_blk_readonly(card
);
503 md
->disk
= alloc_disk(1 << MMC_SHIFT
);
504 if (md
->disk
== NULL
) {
509 spin_lock_init(&md
->lock
);
512 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
);
516 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
519 md
->disk
->major
= MMC_BLOCK_MAJOR
;
520 md
->disk
->first_minor
= devidx
<< MMC_SHIFT
;
521 md
->disk
->fops
= &mmc_bdops
;
522 md
->disk
->private_data
= md
;
523 md
->disk
->queue
= md
->queue
.queue
;
524 md
->disk
->driverfs_dev
= &card
->dev
;
527 * As discussed on lkml, GENHD_FL_REMOVABLE should:
529 * - be set for removable media with permanent block devices
530 * - be unset for removable block devices with permanent media
532 * Since MMC block devices clearly fall under the second
533 * case, we do not set GENHD_FL_REMOVABLE. Userspace
534 * should use the block device creation/destruction hotplug
535 * messages to tell when the card is present.
538 sprintf(md
->disk
->disk_name
, "mmcblk%d", devidx
);
540 blk_queue_logical_block_size(md
->queue
.queue
, 512);
542 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
)) {
544 * The EXT_CSD sector count is in number or 512 byte
547 set_capacity(md
->disk
, card
->ext_csd
.sectors
);
550 * The CSD capacity field is in units of read_blkbits.
551 * set_capacity takes units of 512 bytes.
553 set_capacity(md
->disk
,
554 card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9));
567 mmc_blk_set_blksize(struct mmc_blk_data
*md
, struct mmc_card
*card
)
569 struct mmc_command cmd
;
572 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
573 if (mmc_card_blockaddr(card
))
576 mmc_claim_host(card
->host
);
577 cmd
.opcode
= MMC_SET_BLOCKLEN
;
579 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
580 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
581 mmc_release_host(card
->host
);
584 printk(KERN_ERR
"%s: unable to set block size to %d: %d\n",
585 md
->disk
->disk_name
, cmd
.arg
, err
);
592 static int mmc_blk_probe(struct mmc_card
*card
)
594 struct mmc_blk_data
*md
;
600 * Check that the card supports the command class(es) we need.
602 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
605 md
= mmc_blk_alloc(card
);
609 err
= mmc_blk_set_blksize(md
, card
);
613 string_get_size((u64
)get_capacity(md
->disk
) << 9, STRING_UNITS_2
,
614 cap_str
, sizeof(cap_str
));
615 printk(KERN_INFO
"%s: %s %s %s %s\n",
616 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
617 cap_str
, md
->read_only
? "(ro)" : "");
619 mmc_set_drvdata(card
, md
);
624 mmc_cleanup_queue(&md
->queue
);
630 static void mmc_blk_remove(struct mmc_card
*card
)
632 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
635 /* Stop new requests from getting into the queue */
636 del_gendisk(md
->disk
);
638 /* Then flush out any already in there */
639 mmc_cleanup_queue(&md
->queue
);
643 mmc_set_drvdata(card
, NULL
);
647 static int mmc_blk_suspend(struct mmc_card
*card
, pm_message_t state
)
649 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
652 mmc_queue_suspend(&md
->queue
);
657 static int mmc_blk_resume(struct mmc_card
*card
)
659 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
662 mmc_blk_set_blksize(md
, card
);
663 mmc_queue_resume(&md
->queue
);
668 #define mmc_blk_suspend NULL
669 #define mmc_blk_resume NULL
672 static struct mmc_driver mmc_driver
= {
676 .probe
= mmc_blk_probe
,
677 .remove
= mmc_blk_remove
,
678 .suspend
= mmc_blk_suspend
,
679 .resume
= mmc_blk_resume
,
682 static int __init
mmc_blk_init(void)
686 res
= register_blkdev(MMC_BLOCK_MAJOR
, "mmc");
690 res
= mmc_register_driver(&mmc_driver
);
696 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
701 static void __exit
mmc_blk_exit(void)
703 mmc_unregister_driver(&mmc_driver
);
704 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
707 module_init(mmc_blk_init
);
708 module_exit(mmc_blk_exit
);
710 MODULE_LICENSE("GPL");
711 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");