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>
34 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
38 #include <linux/mmc/ioctl.h>
39 #include <linux/mmc/card.h>
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/mmc.h>
42 #include <linux/mmc/sd.h>
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
49 MODULE_ALIAS("mmc:block");
50 #ifdef MODULE_PARAM_PREFIX
51 #undef MODULE_PARAM_PREFIX
53 #define MODULE_PARAM_PREFIX "mmcblk."
55 #define INAND_CMD38_ARG_EXT_CSD 113
56 #define INAND_CMD38_ARG_ERASE 0x00
57 #define INAND_CMD38_ARG_TRIM 0x01
58 #define INAND_CMD38_ARG_SECERASE 0x80
59 #define INAND_CMD38_ARG_SECTRIM1 0x81
60 #define INAND_CMD38_ARG_SECTRIM2 0x88
62 static DEFINE_MUTEX(block_mutex
);
65 * The defaults come from config options but can be overriden by module
68 static int perdev_minors
= CONFIG_MMC_BLOCK_MINORS
;
71 * We've only got one major, so number of mmcblk devices is
72 * limited to 256 / number of minors per device.
74 static int max_devices
;
76 /* 256 minors, so at most 256 separate devices */
77 static DECLARE_BITMAP(dev_use
, 256);
78 static DECLARE_BITMAP(name_use
, 256);
81 * There is one mmc_blk_data per slot.
86 struct mmc_queue queue
;
87 struct list_head part
;
90 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
91 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
94 unsigned int read_only
;
95 unsigned int part_type
;
96 unsigned int name_idx
;
99 * Only set in main mmc_blk_data associated
100 * with mmc_card with mmc_set_drvdata, and keeps
101 * track of the current selected device partition.
103 unsigned int part_curr
;
104 struct device_attribute force_ro
;
107 static DEFINE_MUTEX(open_lock
);
109 module_param(perdev_minors
, int, 0444);
110 MODULE_PARM_DESC(perdev_minors
, "Minors numbers to allocate per device");
112 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
114 struct mmc_blk_data
*md
;
116 mutex_lock(&open_lock
);
117 md
= disk
->private_data
;
118 if (md
&& md
->usage
== 0)
122 mutex_unlock(&open_lock
);
127 static inline int mmc_get_devidx(struct gendisk
*disk
)
129 int devmaj
= MAJOR(disk_devt(disk
));
130 int devidx
= MINOR(disk_devt(disk
)) / perdev_minors
;
133 devidx
= disk
->first_minor
/ perdev_minors
;
137 static void mmc_blk_put(struct mmc_blk_data
*md
)
139 mutex_lock(&open_lock
);
141 if (md
->usage
== 0) {
142 int devidx
= mmc_get_devidx(md
->disk
);
143 blk_cleanup_queue(md
->queue
.queue
);
145 __clear_bit(devidx
, dev_use
);
150 mutex_unlock(&open_lock
);
153 static ssize_t
force_ro_show(struct device
*dev
, struct device_attribute
*attr
,
157 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
159 ret
= snprintf(buf
, PAGE_SIZE
, "%d",
160 get_disk_ro(dev_to_disk(dev
)) ^
166 static ssize_t
force_ro_store(struct device
*dev
, struct device_attribute
*attr
,
167 const char *buf
, size_t count
)
171 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
172 unsigned long set
= simple_strtoul(buf
, &end
, 0);
178 set_disk_ro(dev_to_disk(dev
), set
|| md
->read_only
);
185 static int mmc_blk_open(struct block_device
*bdev
, fmode_t mode
)
187 struct mmc_blk_data
*md
= mmc_blk_get(bdev
->bd_disk
);
190 mutex_lock(&block_mutex
);
193 check_disk_change(bdev
);
196 if ((mode
& FMODE_WRITE
) && md
->read_only
) {
201 mutex_unlock(&block_mutex
);
206 static int mmc_blk_release(struct gendisk
*disk
, fmode_t mode
)
208 struct mmc_blk_data
*md
= disk
->private_data
;
210 mutex_lock(&block_mutex
);
212 mutex_unlock(&block_mutex
);
217 mmc_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
219 geo
->cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
225 struct mmc_blk_ioc_data
{
226 struct mmc_ioc_cmd ic
;
231 static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
232 struct mmc_ioc_cmd __user
*user
)
234 struct mmc_blk_ioc_data
*idata
;
237 idata
= kzalloc(sizeof(*idata
), GFP_KERNEL
);
243 if (copy_from_user(&idata
->ic
, user
, sizeof(idata
->ic
))) {
248 idata
->buf_bytes
= (u64
) idata
->ic
.blksz
* idata
->ic
.blocks
;
249 if (idata
->buf_bytes
> MMC_IOC_MAX_BYTES
) {
254 if (!idata
->buf_bytes
)
257 idata
->buf
= kzalloc(idata
->buf_bytes
, GFP_KERNEL
);
263 if (copy_from_user(idata
->buf
, (void __user
*)(unsigned long)
264 idata
->ic
.data_ptr
, idata
->buf_bytes
)) {
279 static int mmc_blk_ioctl_cmd(struct block_device
*bdev
,
280 struct mmc_ioc_cmd __user
*ic_ptr
)
282 struct mmc_blk_ioc_data
*idata
;
283 struct mmc_blk_data
*md
;
284 struct mmc_card
*card
;
285 struct mmc_command cmd
= {0};
286 struct mmc_data data
= {0};
287 struct mmc_request mrq
= {0};
288 struct scatterlist sg
;
292 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
293 * whole block device, not on a partition. This prevents overspray
294 * between sibling partitions.
296 if ((!capable(CAP_SYS_RAWIO
)) || (bdev
!= bdev
->bd_contains
))
299 idata
= mmc_blk_ioctl_copy_from_user(ic_ptr
);
301 return PTR_ERR(idata
);
303 md
= mmc_blk_get(bdev
->bd_disk
);
309 card
= md
->queue
.card
;
315 cmd
.opcode
= idata
->ic
.opcode
;
316 cmd
.arg
= idata
->ic
.arg
;
317 cmd
.flags
= idata
->ic
.flags
;
319 if (idata
->buf_bytes
) {
322 data
.blksz
= idata
->ic
.blksz
;
323 data
.blocks
= idata
->ic
.blocks
;
325 sg_init_one(data
.sg
, idata
->buf
, idata
->buf_bytes
);
327 if (idata
->ic
.write_flag
)
328 data
.flags
= MMC_DATA_WRITE
;
330 data
.flags
= MMC_DATA_READ
;
332 /* data.flags must already be set before doing this. */
333 mmc_set_data_timeout(&data
, card
);
335 /* Allow overriding the timeout_ns for empirical tuning. */
336 if (idata
->ic
.data_timeout_ns
)
337 data
.timeout_ns
= idata
->ic
.data_timeout_ns
;
339 if ((cmd
.flags
& MMC_RSP_R1B
) == MMC_RSP_R1B
) {
341 * Pretend this is a data transfer and rely on the
342 * host driver to compute timeout. When all host
343 * drivers support cmd.cmd_timeout for R1B, this
347 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
349 data
.timeout_ns
= idata
->ic
.cmd_timeout_ms
* 1000000;
357 mmc_claim_host(card
->host
);
359 if (idata
->ic
.is_acmd
) {
360 err
= mmc_app_cmd(card
->host
, card
);
365 mmc_wait_for_req(card
->host
, &mrq
);
368 dev_err(mmc_dev(card
->host
), "%s: cmd error %d\n",
369 __func__
, cmd
.error
);
374 dev_err(mmc_dev(card
->host
), "%s: data error %d\n",
375 __func__
, data
.error
);
381 * According to the SD specs, some commands require a delay after
382 * issuing the command.
384 if (idata
->ic
.postsleep_min_us
)
385 usleep_range(idata
->ic
.postsleep_min_us
, idata
->ic
.postsleep_max_us
);
387 if (copy_to_user(&(ic_ptr
->response
), cmd
.resp
, sizeof(cmd
.resp
))) {
392 if (!idata
->ic
.write_flag
) {
393 if (copy_to_user((void __user
*)(unsigned long) idata
->ic
.data_ptr
,
394 idata
->buf
, idata
->buf_bytes
)) {
401 mmc_release_host(card
->host
);
410 static int mmc_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
411 unsigned int cmd
, unsigned long arg
)
414 if (cmd
== MMC_IOC_CMD
)
415 ret
= mmc_blk_ioctl_cmd(bdev
, (struct mmc_ioc_cmd __user
*)arg
);
420 static int mmc_blk_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
421 unsigned int cmd
, unsigned long arg
)
423 return mmc_blk_ioctl(bdev
, mode
, cmd
, (unsigned long) compat_ptr(arg
));
427 static const struct block_device_operations mmc_bdops
= {
428 .open
= mmc_blk_open
,
429 .release
= mmc_blk_release
,
430 .getgeo
= mmc_blk_getgeo
,
431 .owner
= THIS_MODULE
,
432 .ioctl
= mmc_blk_ioctl
,
434 .compat_ioctl
= mmc_blk_compat_ioctl
,
438 struct mmc_blk_request
{
439 struct mmc_request mrq
;
440 struct mmc_command sbc
;
441 struct mmc_command cmd
;
442 struct mmc_command stop
;
443 struct mmc_data data
;
446 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
447 struct mmc_blk_data
*md
)
450 struct mmc_blk_data
*main_md
= mmc_get_drvdata(card
);
451 if (main_md
->part_curr
== md
->part_type
)
454 if (mmc_card_mmc(card
)) {
455 card
->ext_csd
.part_config
&= ~EXT_CSD_PART_CONFIG_ACC_MASK
;
456 card
->ext_csd
.part_config
|= md
->part_type
;
458 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
459 EXT_CSD_PART_CONFIG
, card
->ext_csd
.part_config
,
460 card
->ext_csd
.part_time
);
465 main_md
->part_curr
= md
->part_type
;
469 static u32
mmc_sd_num_wr_blocks(struct mmc_card
*card
)
475 struct mmc_request mrq
= {0};
476 struct mmc_command cmd
= {0};
477 struct mmc_data data
= {0};
478 unsigned int timeout_us
;
480 struct scatterlist sg
;
482 cmd
.opcode
= MMC_APP_CMD
;
483 cmd
.arg
= card
->rca
<< 16;
484 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
486 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
489 if (!mmc_host_is_spi(card
->host
) && !(cmd
.resp
[0] & R1_APP_CMD
))
492 memset(&cmd
, 0, sizeof(struct mmc_command
));
494 cmd
.opcode
= SD_APP_SEND_NUM_WR_BLKS
;
496 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
498 data
.timeout_ns
= card
->csd
.tacc_ns
* 100;
499 data
.timeout_clks
= card
->csd
.tacc_clks
* 100;
501 timeout_us
= data
.timeout_ns
/ 1000;
502 timeout_us
+= data
.timeout_clks
* 1000 /
503 (card
->host
->ios
.clock
/ 1000);
505 if (timeout_us
> 100000) {
506 data
.timeout_ns
= 100000000;
507 data
.timeout_clks
= 0;
512 data
.flags
= MMC_DATA_READ
;
519 blocks
= kmalloc(4, GFP_KERNEL
);
523 sg_init_one(&sg
, blocks
, 4);
525 mmc_wait_for_req(card
->host
, &mrq
);
527 result
= ntohl(*blocks
);
530 if (cmd
.error
|| data
.error
)
536 static u32
get_card_status(struct mmc_card
*card
, struct request
*req
)
538 struct mmc_command cmd
= {0};
541 cmd
.opcode
= MMC_SEND_STATUS
;
542 if (!mmc_host_is_spi(card
->host
))
543 cmd
.arg
= card
->rca
<< 16;
544 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
545 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
547 printk(KERN_ERR
"%s: error %d sending status command",
548 req
->rq_disk
->disk_name
, err
);
552 static int mmc_blk_issue_discard_rq(struct mmc_queue
*mq
, struct request
*req
)
554 struct mmc_blk_data
*md
= mq
->data
;
555 struct mmc_card
*card
= md
->queue
.card
;
556 unsigned int from
, nr
, arg
;
559 if (!mmc_can_erase(card
)) {
564 from
= blk_rq_pos(req
);
565 nr
= blk_rq_sectors(req
);
567 if (mmc_can_trim(card
))
572 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
573 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
574 INAND_CMD38_ARG_EXT_CSD
,
575 arg
== MMC_TRIM_ARG
?
576 INAND_CMD38_ARG_TRIM
:
577 INAND_CMD38_ARG_ERASE
,
582 err
= mmc_erase(card
, from
, nr
, arg
);
584 spin_lock_irq(&md
->lock
);
585 __blk_end_request(req
, err
, blk_rq_bytes(req
));
586 spin_unlock_irq(&md
->lock
);
591 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue
*mq
,
594 struct mmc_blk_data
*md
= mq
->data
;
595 struct mmc_card
*card
= md
->queue
.card
;
596 unsigned int from
, nr
, arg
;
599 if (!mmc_can_secure_erase_trim(card
)) {
604 from
= blk_rq_pos(req
);
605 nr
= blk_rq_sectors(req
);
607 if (mmc_can_trim(card
) && !mmc_erase_group_aligned(card
, from
, nr
))
608 arg
= MMC_SECURE_TRIM1_ARG
;
610 arg
= MMC_SECURE_ERASE_ARG
;
612 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
613 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
614 INAND_CMD38_ARG_EXT_CSD
,
615 arg
== MMC_SECURE_TRIM1_ARG
?
616 INAND_CMD38_ARG_SECTRIM1
:
617 INAND_CMD38_ARG_SECERASE
,
622 err
= mmc_erase(card
, from
, nr
, arg
);
623 if (!err
&& arg
== MMC_SECURE_TRIM1_ARG
) {
624 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
625 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
626 INAND_CMD38_ARG_EXT_CSD
,
627 INAND_CMD38_ARG_SECTRIM2
,
632 err
= mmc_erase(card
, from
, nr
, MMC_SECURE_TRIM2_ARG
);
635 spin_lock_irq(&md
->lock
);
636 __blk_end_request(req
, err
, blk_rq_bytes(req
));
637 spin_unlock_irq(&md
->lock
);
642 static int mmc_blk_issue_flush(struct mmc_queue
*mq
, struct request
*req
)
644 struct mmc_blk_data
*md
= mq
->data
;
647 * No-op, only service this because we need REQ_FUA for reliable
650 spin_lock_irq(&md
->lock
);
651 __blk_end_request_all(req
, 0);
652 spin_unlock_irq(&md
->lock
);
658 * Reformat current write as a reliable write, supporting
659 * both legacy and the enhanced reliable write MMC cards.
660 * In each transfer we'll handle only as much as a single
661 * reliable write can handle, thus finish the request in
662 * partial completions.
664 static inline void mmc_apply_rel_rw(struct mmc_blk_request
*brq
,
665 struct mmc_card
*card
,
668 if (!(card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
)) {
669 /* Legacy mode imposes restrictions on transfers. */
670 if (!IS_ALIGNED(brq
->cmd
.arg
, card
->ext_csd
.rel_sectors
))
671 brq
->data
.blocks
= 1;
673 if (brq
->data
.blocks
> card
->ext_csd
.rel_sectors
)
674 brq
->data
.blocks
= card
->ext_csd
.rel_sectors
;
675 else if (brq
->data
.blocks
< card
->ext_csd
.rel_sectors
)
676 brq
->data
.blocks
= 1;
680 static int mmc_blk_issue_rw_rq(struct mmc_queue
*mq
, struct request
*req
)
682 struct mmc_blk_data
*md
= mq
->data
;
683 struct mmc_card
*card
= md
->queue
.card
;
684 struct mmc_blk_request brq
;
685 int ret
= 1, disable_multi
= 0;
688 * Reliable writes are used to implement Forced Unit Access and
689 * REQ_META accesses, and are supported only on MMCs.
691 bool do_rel_wr
= ((req
->cmd_flags
& REQ_FUA
) ||
692 (req
->cmd_flags
& REQ_META
)) &&
693 (rq_data_dir(req
) == WRITE
) &&
694 (md
->flags
& MMC_BLK_REL_WR
);
697 struct mmc_command cmd
= {0};
698 u32 readcmd
, writecmd
, status
= 0;
700 memset(&brq
, 0, sizeof(struct mmc_blk_request
));
701 brq
.mrq
.cmd
= &brq
.cmd
;
702 brq
.mrq
.data
= &brq
.data
;
704 brq
.cmd
.arg
= blk_rq_pos(req
);
705 if (!mmc_card_blockaddr(card
))
707 brq
.cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
708 brq
.data
.blksz
= 512;
709 brq
.stop
.opcode
= MMC_STOP_TRANSMISSION
;
711 brq
.stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
712 brq
.data
.blocks
= blk_rq_sectors(req
);
715 * The block layer doesn't support all sector count
716 * restrictions, so we need to be prepared for too big
719 if (brq
.data
.blocks
> card
->host
->max_blk_count
)
720 brq
.data
.blocks
= card
->host
->max_blk_count
;
723 * After a read error, we redo the request one sector at a time
724 * in order to accurately determine which sectors can be read
727 if (disable_multi
&& brq
.data
.blocks
> 1)
730 if (brq
.data
.blocks
> 1 || do_rel_wr
) {
731 /* SPI multiblock writes terminate using a special
732 * token, not a STOP_TRANSMISSION request.
734 if (!mmc_host_is_spi(card
->host
) ||
735 rq_data_dir(req
) == READ
)
736 brq
.mrq
.stop
= &brq
.stop
;
737 readcmd
= MMC_READ_MULTIPLE_BLOCK
;
738 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
741 readcmd
= MMC_READ_SINGLE_BLOCK
;
742 writecmd
= MMC_WRITE_BLOCK
;
744 if (rq_data_dir(req
) == READ
) {
745 brq
.cmd
.opcode
= readcmd
;
746 brq
.data
.flags
|= MMC_DATA_READ
;
748 brq
.cmd
.opcode
= writecmd
;
749 brq
.data
.flags
|= MMC_DATA_WRITE
;
753 mmc_apply_rel_rw(&brq
, card
, req
);
756 * Pre-defined multi-block transfers are preferable to
757 * open ended-ones (and necessary for reliable writes).
758 * However, it is not sufficient to just send CMD23,
759 * and avoid the final CMD12, as on an error condition
760 * CMD12 (stop) needs to be sent anyway. This, coupled
761 * with Auto-CMD23 enhancements provided by some
762 * hosts, means that the complexity of dealing
763 * with this is best left to the host. If CMD23 is
764 * supported by card and host, we'll fill sbc in and let
765 * the host deal with handling it correctly. This means
766 * that for hosts that don't expose MMC_CAP_CMD23, no
767 * change of behavior will be observed.
769 * N.B: Some MMC cards experience perf degradation.
770 * We'll avoid using CMD23-bounded multiblock writes for
771 * these, while retaining features like reliable writes.
774 if ((md
->flags
& MMC_BLK_CMD23
) &&
775 mmc_op_multi(brq
.cmd
.opcode
) &&
776 (do_rel_wr
|| !(card
->quirks
& MMC_QUIRK_BLK_NO_CMD23
))) {
777 brq
.sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
778 brq
.sbc
.arg
= brq
.data
.blocks
|
779 (do_rel_wr
? (1 << 31) : 0);
780 brq
.sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
781 brq
.mrq
.sbc
= &brq
.sbc
;
784 mmc_set_data_timeout(&brq
.data
, card
);
786 brq
.data
.sg
= mq
->sg
;
787 brq
.data
.sg_len
= mmc_queue_map_sg(mq
);
790 * Adjust the sg list so it is the same size as the
793 if (brq
.data
.blocks
!= blk_rq_sectors(req
)) {
794 int i
, data_size
= brq
.data
.blocks
<< 9;
795 struct scatterlist
*sg
;
797 for_each_sg(brq
.data
.sg
, sg
, brq
.data
.sg_len
, i
) {
798 data_size
-= sg
->length
;
799 if (data_size
<= 0) {
800 sg
->length
+= data_size
;
808 mmc_queue_bounce_pre(mq
);
810 mmc_wait_for_req(card
->host
, &brq
.mrq
);
812 mmc_queue_bounce_post(mq
);
815 * Check for errors here, but don't jump to cmd_err
816 * until later as we need to wait for the card to leave
817 * programming mode even when things go wrong.
819 if (brq
.sbc
.error
|| brq
.cmd
.error
||
820 brq
.data
.error
|| brq
.stop
.error
) {
821 if (brq
.data
.blocks
> 1 && rq_data_dir(req
) == READ
) {
822 /* Redo read one sector at a time */
823 printk(KERN_WARNING
"%s: retrying using single "
824 "block read\n", req
->rq_disk
->disk_name
);
828 status
= get_card_status(card
, req
);
832 printk(KERN_ERR
"%s: error %d sending SET_BLOCK_COUNT "
833 "command, response %#x, card status %#x\n",
834 req
->rq_disk
->disk_name
, brq
.sbc
.error
,
835 brq
.sbc
.resp
[0], status
);
839 printk(KERN_ERR
"%s: error %d sending read/write "
840 "command, response %#x, card status %#x\n",
841 req
->rq_disk
->disk_name
, brq
.cmd
.error
,
842 brq
.cmd
.resp
[0], status
);
845 if (brq
.data
.error
) {
846 if (brq
.data
.error
== -ETIMEDOUT
&& brq
.mrq
.stop
)
847 /* 'Stop' response contains card status */
848 status
= brq
.mrq
.stop
->resp
[0];
849 printk(KERN_ERR
"%s: error %d transferring data,"
850 " sector %u, nr %u, card status %#x\n",
851 req
->rq_disk
->disk_name
, brq
.data
.error
,
852 (unsigned)blk_rq_pos(req
),
853 (unsigned)blk_rq_sectors(req
), status
);
856 if (brq
.stop
.error
) {
857 printk(KERN_ERR
"%s: error %d sending stop command, "
858 "response %#x, card status %#x\n",
859 req
->rq_disk
->disk_name
, brq
.stop
.error
,
860 brq
.stop
.resp
[0], status
);
863 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
) {
867 cmd
.opcode
= MMC_SEND_STATUS
;
868 cmd
.arg
= card
->rca
<< 16;
869 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
870 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
872 printk(KERN_ERR
"%s: error %d requesting status\n",
873 req
->rq_disk
->disk_name
, err
);
877 * Some cards mishandle the status bits,
878 * so make sure to check both the busy
879 * indication and the card state.
881 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
) ||
882 (R1_CURRENT_STATE(cmd
.resp
[0]) == 7));
885 if (cmd
.resp
[0] & ~0x00000900)
886 printk(KERN_ERR
"%s: status = %08x\n",
887 req
->rq_disk
->disk_name
, cmd
.resp
[0]);
888 if (mmc_decode_status(cmd
.resp
))
893 if (brq
.cmd
.error
|| brq
.stop
.error
|| brq
.data
.error
) {
894 if (rq_data_dir(req
) == READ
) {
896 * After an error, we redo I/O one sector at a
897 * time, so we only reach here after trying to
898 * read a single sector.
900 spin_lock_irq(&md
->lock
);
901 ret
= __blk_end_request(req
, -EIO
, brq
.data
.blksz
);
902 spin_unlock_irq(&md
->lock
);
909 * A block was successfully transferred.
911 spin_lock_irq(&md
->lock
);
912 ret
= __blk_end_request(req
, 0, brq
.data
.bytes_xfered
);
913 spin_unlock_irq(&md
->lock
);
920 * If this is an SD card and we're writing, we can first
921 * mark the known good sectors as ok.
923 * If the card is not SD, we can still ok written sectors
924 * as reported by the controller (which might be less than
925 * the real number of written sectors, but never more).
927 if (mmc_card_sd(card
)) {
930 blocks
= mmc_sd_num_wr_blocks(card
);
931 if (blocks
!= (u32
)-1) {
932 spin_lock_irq(&md
->lock
);
933 ret
= __blk_end_request(req
, 0, blocks
<< 9);
934 spin_unlock_irq(&md
->lock
);
937 spin_lock_irq(&md
->lock
);
938 ret
= __blk_end_request(req
, 0, brq
.data
.bytes_xfered
);
939 spin_unlock_irq(&md
->lock
);
942 spin_lock_irq(&md
->lock
);
944 ret
= __blk_end_request(req
, -EIO
, blk_rq_cur_bytes(req
));
945 spin_unlock_irq(&md
->lock
);
950 static int mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
953 struct mmc_blk_data
*md
= mq
->data
;
954 struct mmc_card
*card
= md
->queue
.card
;
956 mmc_claim_host(card
->host
);
957 ret
= mmc_blk_part_switch(card
, md
);
963 if (req
->cmd_flags
& REQ_DISCARD
) {
964 if (req
->cmd_flags
& REQ_SECURE
)
965 ret
= mmc_blk_issue_secdiscard_rq(mq
, req
);
967 ret
= mmc_blk_issue_discard_rq(mq
, req
);
968 } else if (req
->cmd_flags
& REQ_FLUSH
) {
969 ret
= mmc_blk_issue_flush(mq
, req
);
971 ret
= mmc_blk_issue_rw_rq(mq
, req
);
975 mmc_release_host(card
->host
);
979 static inline int mmc_blk_readonly(struct mmc_card
*card
)
981 return mmc_card_readonly(card
) ||
982 !(card
->csd
.cmdclass
& CCC_BLOCK_WRITE
);
985 static struct mmc_blk_data
*mmc_blk_alloc_req(struct mmc_card
*card
,
986 struct device
*parent
,
991 struct mmc_blk_data
*md
;
994 devidx
= find_first_zero_bit(dev_use
, max_devices
);
995 if (devidx
>= max_devices
)
996 return ERR_PTR(-ENOSPC
);
997 __set_bit(devidx
, dev_use
);
999 md
= kzalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
1006 * !subname implies we are creating main mmc_blk_data that will be
1007 * associated with mmc_card with mmc_set_drvdata. Due to device
1008 * partitions, devidx will not coincide with a per-physical card
1009 * index anymore so we keep track of a name index.
1012 md
->name_idx
= find_first_zero_bit(name_use
, max_devices
);
1013 __set_bit(md
->name_idx
, name_use
);
1016 md
->name_idx
= ((struct mmc_blk_data
*)
1017 dev_to_disk(parent
)->private_data
)->name_idx
;
1020 * Set the read-only status based on the supported commands
1021 * and the write protect switch.
1023 md
->read_only
= mmc_blk_readonly(card
);
1025 md
->disk
= alloc_disk(perdev_minors
);
1026 if (md
->disk
== NULL
) {
1031 spin_lock_init(&md
->lock
);
1032 INIT_LIST_HEAD(&md
->part
);
1035 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
, subname
);
1039 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
1040 md
->queue
.data
= md
;
1042 md
->disk
->major
= MMC_BLOCK_MAJOR
;
1043 md
->disk
->first_minor
= devidx
* perdev_minors
;
1044 md
->disk
->fops
= &mmc_bdops
;
1045 md
->disk
->private_data
= md
;
1046 md
->disk
->queue
= md
->queue
.queue
;
1047 md
->disk
->driverfs_dev
= parent
;
1048 set_disk_ro(md
->disk
, md
->read_only
|| default_ro
);
1051 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1053 * - be set for removable media with permanent block devices
1054 * - be unset for removable block devices with permanent media
1056 * Since MMC block devices clearly fall under the second
1057 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1058 * should use the block device creation/destruction hotplug
1059 * messages to tell when the card is present.
1062 snprintf(md
->disk
->disk_name
, sizeof(md
->disk
->disk_name
),
1063 "mmcblk%d%s", md
->name_idx
, subname
? subname
: "");
1065 blk_queue_logical_block_size(md
->queue
.queue
, 512);
1066 set_capacity(md
->disk
, size
);
1068 if (mmc_host_cmd23(card
->host
)) {
1069 if (mmc_card_mmc(card
) ||
1070 (mmc_card_sd(card
) &&
1071 card
->scr
.cmds
& SD_SCR_CMD23_SUPPORT
))
1072 md
->flags
|= MMC_BLK_CMD23
;
1075 if (mmc_card_mmc(card
) &&
1076 md
->flags
& MMC_BLK_CMD23
&&
1077 ((card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
) ||
1078 card
->ext_csd
.rel_sectors
)) {
1079 md
->flags
|= MMC_BLK_REL_WR
;
1080 blk_queue_flush(md
->queue
.queue
, REQ_FLUSH
| REQ_FUA
);
1090 return ERR_PTR(ret
);
1093 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
1096 struct mmc_blk_data
*md
;
1098 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
)) {
1100 * The EXT_CSD sector count is in number or 512 byte
1103 size
= card
->ext_csd
.sectors
;
1106 * The CSD capacity field is in units of read_blkbits.
1107 * set_capacity takes units of 512 bytes.
1109 size
= card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9);
1112 md
= mmc_blk_alloc_req(card
, &card
->dev
, size
, false, NULL
);
1116 static int mmc_blk_alloc_part(struct mmc_card
*card
,
1117 struct mmc_blk_data
*md
,
1118 unsigned int part_type
,
1121 const char *subname
)
1124 struct mmc_blk_data
*part_md
;
1126 part_md
= mmc_blk_alloc_req(card
, disk_to_dev(md
->disk
), size
, default_ro
,
1128 if (IS_ERR(part_md
))
1129 return PTR_ERR(part_md
);
1130 part_md
->part_type
= part_type
;
1131 list_add(&part_md
->part
, &md
->part
);
1133 string_get_size((u64
)get_capacity(part_md
->disk
) << 9, STRING_UNITS_2
,
1134 cap_str
, sizeof(cap_str
));
1135 printk(KERN_INFO
"%s: %s %s partition %u %s\n",
1136 part_md
->disk
->disk_name
, mmc_card_id(card
),
1137 mmc_card_name(card
), part_md
->part_type
, cap_str
);
1141 static int mmc_blk_alloc_parts(struct mmc_card
*card
, struct mmc_blk_data
*md
)
1145 if (!mmc_card_mmc(card
))
1148 if (card
->ext_csd
.boot_size
) {
1149 ret
= mmc_blk_alloc_part(card
, md
, EXT_CSD_PART_CONFIG_ACC_BOOT0
,
1150 card
->ext_csd
.boot_size
>> 9,
1155 ret
= mmc_blk_alloc_part(card
, md
, EXT_CSD_PART_CONFIG_ACC_BOOT1
,
1156 card
->ext_csd
.boot_size
>> 9,
1167 mmc_blk_set_blksize(struct mmc_blk_data
*md
, struct mmc_card
*card
)
1171 mmc_claim_host(card
->host
);
1172 err
= mmc_set_blocklen(card
, 512);
1173 mmc_release_host(card
->host
);
1176 printk(KERN_ERR
"%s: unable to set block size to 512: %d\n",
1177 md
->disk
->disk_name
, err
);
1184 static void mmc_blk_remove_req(struct mmc_blk_data
*md
)
1187 if (md
->disk
->flags
& GENHD_FL_UP
) {
1188 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
1190 /* Stop new requests from getting into the queue */
1191 del_gendisk(md
->disk
);
1194 /* Then flush out any already in there */
1195 mmc_cleanup_queue(&md
->queue
);
1200 static void mmc_blk_remove_parts(struct mmc_card
*card
,
1201 struct mmc_blk_data
*md
)
1203 struct list_head
*pos
, *q
;
1204 struct mmc_blk_data
*part_md
;
1206 __clear_bit(md
->name_idx
, name_use
);
1207 list_for_each_safe(pos
, q
, &md
->part
) {
1208 part_md
= list_entry(pos
, struct mmc_blk_data
, part
);
1210 mmc_blk_remove_req(part_md
);
1214 static int mmc_add_disk(struct mmc_blk_data
*md
)
1219 md
->force_ro
.show
= force_ro_show
;
1220 md
->force_ro
.store
= force_ro_store
;
1221 sysfs_attr_init(&md
->force_ro
.attr
);
1222 md
->force_ro
.attr
.name
= "force_ro";
1223 md
->force_ro
.attr
.mode
= S_IRUGO
| S_IWUSR
;
1224 ret
= device_create_file(disk_to_dev(md
->disk
), &md
->force_ro
);
1226 del_gendisk(md
->disk
);
1231 static const struct mmc_fixup blk_fixups
[] =
1233 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk
, MMC_QUIRK_INAND_CMD38
),
1234 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk
, MMC_QUIRK_INAND_CMD38
),
1235 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk
, MMC_QUIRK_INAND_CMD38
),
1236 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk
, MMC_QUIRK_INAND_CMD38
),
1237 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk
, MMC_QUIRK_INAND_CMD38
),
1240 * Some MMC cards experience performance degradation with CMD23
1241 * instead of CMD12-bounded multiblock transfers. For now we'll
1242 * black list what's bad...
1243 * - Certain Toshiba cards.
1245 * N.B. This doesn't affect SD cards.
1247 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY
, add_quirk_mmc
,
1248 MMC_QUIRK_BLK_NO_CMD23
),
1249 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY
, add_quirk_mmc
,
1250 MMC_QUIRK_BLK_NO_CMD23
),
1251 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY
, add_quirk_mmc
,
1252 MMC_QUIRK_BLK_NO_CMD23
),
1256 static int mmc_blk_probe(struct mmc_card
*card
)
1258 struct mmc_blk_data
*md
, *part_md
;
1263 * Check that the card supports the command class(es) we need.
1265 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
1268 md
= mmc_blk_alloc(card
);
1272 err
= mmc_blk_set_blksize(md
, card
);
1276 string_get_size((u64
)get_capacity(md
->disk
) << 9, STRING_UNITS_2
,
1277 cap_str
, sizeof(cap_str
));
1278 printk(KERN_INFO
"%s: %s %s %s %s\n",
1279 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
1280 cap_str
, md
->read_only
? "(ro)" : "");
1282 if (mmc_blk_alloc_parts(card
, md
))
1285 mmc_set_drvdata(card
, md
);
1286 mmc_fixup_device(card
, blk_fixups
);
1288 if (mmc_add_disk(md
))
1291 list_for_each_entry(part_md
, &md
->part
, part
) {
1292 if (mmc_add_disk(part_md
))
1298 mmc_blk_remove_parts(card
, md
);
1299 mmc_blk_remove_req(md
);
1303 static void mmc_blk_remove(struct mmc_card
*card
)
1305 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
1307 mmc_blk_remove_parts(card
, md
);
1308 mmc_claim_host(card
->host
);
1309 mmc_blk_part_switch(card
, md
);
1310 mmc_release_host(card
->host
);
1311 mmc_blk_remove_req(md
);
1312 mmc_set_drvdata(card
, NULL
);
1316 static int mmc_blk_suspend(struct mmc_card
*card
, pm_message_t state
)
1318 struct mmc_blk_data
*part_md
;
1319 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
1322 mmc_queue_suspend(&md
->queue
);
1323 list_for_each_entry(part_md
, &md
->part
, part
) {
1324 mmc_queue_suspend(&part_md
->queue
);
1330 static int mmc_blk_resume(struct mmc_card
*card
)
1332 struct mmc_blk_data
*part_md
;
1333 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
1336 mmc_blk_set_blksize(md
, card
);
1339 * Resume involves the card going into idle state,
1340 * so current partition is always the main one.
1342 md
->part_curr
= md
->part_type
;
1343 mmc_queue_resume(&md
->queue
);
1344 list_for_each_entry(part_md
, &md
->part
, part
) {
1345 mmc_queue_resume(&part_md
->queue
);
1351 #define mmc_blk_suspend NULL
1352 #define mmc_blk_resume NULL
1355 static struct mmc_driver mmc_driver
= {
1359 .probe
= mmc_blk_probe
,
1360 .remove
= mmc_blk_remove
,
1361 .suspend
= mmc_blk_suspend
,
1362 .resume
= mmc_blk_resume
,
1365 static int __init
mmc_blk_init(void)
1369 if (perdev_minors
!= CONFIG_MMC_BLOCK_MINORS
)
1370 pr_info("mmcblk: using %d minors per device\n", perdev_minors
);
1372 max_devices
= 256 / perdev_minors
;
1374 res
= register_blkdev(MMC_BLOCK_MAJOR
, "mmc");
1378 res
= mmc_register_driver(&mmc_driver
);
1384 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
1389 static void __exit
mmc_blk_exit(void)
1391 mmc_unregister_driver(&mmc_driver
);
1392 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
1395 module_init(mmc_blk_init
);
1396 module_exit(mmc_blk_exit
);
1398 MODULE_LICENSE("GPL");
1399 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");