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>
37 #include <linux/pm_runtime.h>
38 #include <linux/idr.h>
40 #include <linux/mmc/ioctl.h>
41 #include <linux/mmc/card.h>
42 #include <linux/mmc/host.h>
43 #include <linux/mmc/mmc.h>
44 #include <linux/mmc/sd.h>
46 #include <linux/uaccess.h>
58 MODULE_ALIAS("mmc:block");
59 #ifdef MODULE_PARAM_PREFIX
60 #undef MODULE_PARAM_PREFIX
62 #define MODULE_PARAM_PREFIX "mmcblk."
64 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
65 #define MMC_SANITIZE_REQ_TIMEOUT 240000
66 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
68 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
69 (rq_data_dir(req) == WRITE))
70 static DEFINE_MUTEX(block_mutex
);
73 * The defaults come from config options but can be overriden by module
76 static int perdev_minors
= CONFIG_MMC_BLOCK_MINORS
;
79 * We've only got one major, so number of mmcblk devices is
80 * limited to (1 << 20) / number of minors per device. It is also
81 * limited by the MAX_DEVICES below.
83 static int max_devices
;
85 #define MAX_DEVICES 256
87 static DEFINE_IDA(mmc_blk_ida
);
90 * There is one mmc_blk_data per slot.
94 struct device
*parent
;
96 struct mmc_queue queue
;
97 struct list_head part
;
100 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
101 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
104 unsigned int read_only
;
105 unsigned int part_type
;
106 unsigned int reset_done
;
107 #define MMC_BLK_READ BIT(0)
108 #define MMC_BLK_WRITE BIT(1)
109 #define MMC_BLK_DISCARD BIT(2)
110 #define MMC_BLK_SECDISCARD BIT(3)
113 * Only set in main mmc_blk_data associated
114 * with mmc_card with dev_set_drvdata, and keeps
115 * track of the current selected device partition.
117 unsigned int part_curr
;
118 struct device_attribute force_ro
;
119 struct device_attribute power_ro_lock
;
123 static DEFINE_MUTEX(open_lock
);
125 module_param(perdev_minors
, int, 0444);
126 MODULE_PARM_DESC(perdev_minors
, "Minors numbers to allocate per device");
128 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
129 struct mmc_blk_data
*md
);
130 static int get_card_status(struct mmc_card
*card
, u32
*status
, int retries
);
132 static void mmc_blk_requeue(struct request_queue
*q
, struct request
*req
)
134 spin_lock_irq(q
->queue_lock
);
135 blk_requeue_request(q
, req
);
136 spin_unlock_irq(q
->queue_lock
);
139 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
141 struct mmc_blk_data
*md
;
143 mutex_lock(&open_lock
);
144 md
= disk
->private_data
;
145 if (md
&& md
->usage
== 0)
149 mutex_unlock(&open_lock
);
154 static inline int mmc_get_devidx(struct gendisk
*disk
)
156 int devidx
= disk
->first_minor
/ perdev_minors
;
160 static void mmc_blk_put(struct mmc_blk_data
*md
)
162 mutex_lock(&open_lock
);
164 if (md
->usage
== 0) {
165 int devidx
= mmc_get_devidx(md
->disk
);
166 blk_cleanup_queue(md
->queue
.queue
);
167 ida_simple_remove(&mmc_blk_ida
, devidx
);
171 mutex_unlock(&open_lock
);
174 static ssize_t
power_ro_lock_show(struct device
*dev
,
175 struct device_attribute
*attr
, char *buf
)
178 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
179 struct mmc_card
*card
= md
->queue
.card
;
182 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PERM_WP_EN
)
184 else if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_EN
)
187 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", locked
);
194 static ssize_t
power_ro_lock_store(struct device
*dev
,
195 struct device_attribute
*attr
, const char *buf
, size_t count
)
198 struct mmc_blk_data
*md
, *part_md
;
199 struct mmc_card
*card
;
202 if (kstrtoul(buf
, 0, &set
))
208 md
= mmc_blk_get(dev_to_disk(dev
));
209 card
= md
->queue
.card
;
213 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BOOT_WP
,
214 card
->ext_csd
.boot_ro_lock
|
215 EXT_CSD_BOOT_WP_B_PWR_WP_EN
,
216 card
->ext_csd
.part_time
);
218 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md
->disk
->disk_name
, ret
);
220 card
->ext_csd
.boot_ro_lock
|= EXT_CSD_BOOT_WP_B_PWR_WP_EN
;
225 pr_info("%s: Locking boot partition ro until next power on\n",
226 md
->disk
->disk_name
);
227 set_disk_ro(md
->disk
, 1);
229 list_for_each_entry(part_md
, &md
->part
, part
)
230 if (part_md
->area_type
== MMC_BLK_DATA_AREA_BOOT
) {
231 pr_info("%s: Locking boot partition ro until next power on\n", part_md
->disk
->disk_name
);
232 set_disk_ro(part_md
->disk
, 1);
240 static ssize_t
force_ro_show(struct device
*dev
, struct device_attribute
*attr
,
244 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
246 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n",
247 get_disk_ro(dev_to_disk(dev
)) ^
253 static ssize_t
force_ro_store(struct device
*dev
, struct device_attribute
*attr
,
254 const char *buf
, size_t count
)
258 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
259 unsigned long set
= simple_strtoul(buf
, &end
, 0);
265 set_disk_ro(dev_to_disk(dev
), set
|| md
->read_only
);
272 static int mmc_blk_open(struct block_device
*bdev
, fmode_t mode
)
274 struct mmc_blk_data
*md
= mmc_blk_get(bdev
->bd_disk
);
277 mutex_lock(&block_mutex
);
280 check_disk_change(bdev
);
283 if ((mode
& FMODE_WRITE
) && md
->read_only
) {
288 mutex_unlock(&block_mutex
);
293 static void mmc_blk_release(struct gendisk
*disk
, fmode_t mode
)
295 struct mmc_blk_data
*md
= disk
->private_data
;
297 mutex_lock(&block_mutex
);
299 mutex_unlock(&block_mutex
);
303 mmc_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
305 geo
->cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
311 struct mmc_blk_ioc_data
{
312 struct mmc_ioc_cmd ic
;
317 static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
318 struct mmc_ioc_cmd __user
*user
)
320 struct mmc_blk_ioc_data
*idata
;
323 idata
= kmalloc(sizeof(*idata
), GFP_KERNEL
);
329 if (copy_from_user(&idata
->ic
, user
, sizeof(idata
->ic
))) {
334 idata
->buf_bytes
= (u64
) idata
->ic
.blksz
* idata
->ic
.blocks
;
335 if (idata
->buf_bytes
> MMC_IOC_MAX_BYTES
) {
340 if (!idata
->buf_bytes
) {
345 idata
->buf
= kmalloc(idata
->buf_bytes
, GFP_KERNEL
);
351 if (copy_from_user(idata
->buf
, (void __user
*)(unsigned long)
352 idata
->ic
.data_ptr
, idata
->buf_bytes
)) {
367 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user
*ic_ptr
,
368 struct mmc_blk_ioc_data
*idata
)
370 struct mmc_ioc_cmd
*ic
= &idata
->ic
;
372 if (copy_to_user(&(ic_ptr
->response
), ic
->response
,
373 sizeof(ic
->response
)))
376 if (!idata
->ic
.write_flag
) {
377 if (copy_to_user((void __user
*)(unsigned long)ic
->data_ptr
,
378 idata
->buf
, idata
->buf_bytes
))
385 static int ioctl_rpmb_card_status_poll(struct mmc_card
*card
, u32
*status
,
391 if (!status
|| !retries_max
)
395 err
= get_card_status(card
, status
, 5);
399 if (!R1_STATUS(*status
) &&
400 (R1_CURRENT_STATE(*status
) != R1_STATE_PRG
))
401 break; /* RPMB programming operation complete */
404 * Rechedule to give the MMC device a chance to continue
405 * processing the previous command without being polled too
408 usleep_range(1000, 5000);
409 } while (++retry_count
< retries_max
);
411 if (retry_count
== retries_max
)
417 static int ioctl_do_sanitize(struct mmc_card
*card
)
421 if (!mmc_can_sanitize(card
)) {
422 pr_warn("%s: %s - SANITIZE is not supported\n",
423 mmc_hostname(card
->host
), __func__
);
428 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
429 mmc_hostname(card
->host
), __func__
);
431 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
432 EXT_CSD_SANITIZE_START
, 1,
433 MMC_SANITIZE_REQ_TIMEOUT
);
436 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
437 mmc_hostname(card
->host
), __func__
, err
);
439 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card
->host
),
445 static int __mmc_blk_ioctl_cmd(struct mmc_card
*card
, struct mmc_blk_data
*md
,
446 struct mmc_blk_ioc_data
*idata
)
448 struct mmc_command cmd
= {};
449 struct mmc_data data
= {};
450 struct mmc_request mrq
= {};
451 struct scatterlist sg
;
456 if (!card
|| !md
|| !idata
)
459 if (md
->area_type
& MMC_BLK_DATA_AREA_RPMB
)
462 cmd
.opcode
= idata
->ic
.opcode
;
463 cmd
.arg
= idata
->ic
.arg
;
464 cmd
.flags
= idata
->ic
.flags
;
466 if (idata
->buf_bytes
) {
469 data
.blksz
= idata
->ic
.blksz
;
470 data
.blocks
= idata
->ic
.blocks
;
472 sg_init_one(data
.sg
, idata
->buf
, idata
->buf_bytes
);
474 if (idata
->ic
.write_flag
)
475 data
.flags
= MMC_DATA_WRITE
;
477 data
.flags
= MMC_DATA_READ
;
479 /* data.flags must already be set before doing this. */
480 mmc_set_data_timeout(&data
, card
);
482 /* Allow overriding the timeout_ns for empirical tuning. */
483 if (idata
->ic
.data_timeout_ns
)
484 data
.timeout_ns
= idata
->ic
.data_timeout_ns
;
486 if ((cmd
.flags
& MMC_RSP_R1B
) == MMC_RSP_R1B
) {
488 * Pretend this is a data transfer and rely on the
489 * host driver to compute timeout. When all host
490 * drivers support cmd.cmd_timeout for R1B, this
494 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
496 data
.timeout_ns
= idata
->ic
.cmd_timeout_ms
* 1000000;
504 err
= mmc_blk_part_switch(card
, md
);
508 if (idata
->ic
.is_acmd
) {
509 err
= mmc_app_cmd(card
->host
, card
);
515 err
= mmc_set_blockcount(card
, data
.blocks
,
516 idata
->ic
.write_flag
& (1 << 31));
521 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd
.arg
) == EXT_CSD_SANITIZE_START
) &&
522 (cmd
.opcode
== MMC_SWITCH
)) {
523 err
= ioctl_do_sanitize(card
);
526 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
532 mmc_wait_for_req(card
->host
, &mrq
);
535 dev_err(mmc_dev(card
->host
), "%s: cmd error %d\n",
536 __func__
, cmd
.error
);
540 dev_err(mmc_dev(card
->host
), "%s: data error %d\n",
541 __func__
, data
.error
);
546 * According to the SD specs, some commands require a delay after
547 * issuing the command.
549 if (idata
->ic
.postsleep_min_us
)
550 usleep_range(idata
->ic
.postsleep_min_us
, idata
->ic
.postsleep_max_us
);
552 memcpy(&(idata
->ic
.response
), cmd
.resp
, sizeof(cmd
.resp
));
556 * Ensure RPMB command has completed by polling CMD13
559 err
= ioctl_rpmb_card_status_poll(card
, &status
, 5);
561 dev_err(mmc_dev(card
->host
),
562 "%s: Card Status=0x%08X, error %d\n",
563 __func__
, status
, err
);
569 static int mmc_blk_ioctl_cmd(struct block_device
*bdev
,
570 struct mmc_ioc_cmd __user
*ic_ptr
)
572 struct mmc_blk_ioc_data
*idata
;
573 struct mmc_blk_data
*md
;
574 struct mmc_card
*card
;
575 int err
= 0, ioc_err
= 0;
578 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
579 * whole block device, not on a partition. This prevents overspray
580 * between sibling partitions.
582 if ((!capable(CAP_SYS_RAWIO
)) || (bdev
!= bdev
->bd_contains
))
585 idata
= mmc_blk_ioctl_copy_from_user(ic_ptr
);
587 return PTR_ERR(idata
);
589 md
= mmc_blk_get(bdev
->bd_disk
);
595 card
= md
->queue
.card
;
603 ioc_err
= __mmc_blk_ioctl_cmd(card
, md
, idata
);
605 /* Always switch back to main area after RPMB access */
606 if (md
->area_type
& MMC_BLK_DATA_AREA_RPMB
)
607 mmc_blk_part_switch(card
, dev_get_drvdata(&card
->dev
));
611 err
= mmc_blk_ioctl_copy_to_user(ic_ptr
, idata
);
618 return ioc_err
? ioc_err
: err
;
621 static int mmc_blk_ioctl_multi_cmd(struct block_device
*bdev
,
622 struct mmc_ioc_multi_cmd __user
*user
)
624 struct mmc_blk_ioc_data
**idata
= NULL
;
625 struct mmc_ioc_cmd __user
*cmds
= user
->cmds
;
626 struct mmc_card
*card
;
627 struct mmc_blk_data
*md
;
628 int i
, err
= 0, ioc_err
= 0;
632 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
633 * whole block device, not on a partition. This prevents overspray
634 * between sibling partitions.
636 if ((!capable(CAP_SYS_RAWIO
)) || (bdev
!= bdev
->bd_contains
))
639 if (copy_from_user(&num_of_cmds
, &user
->num_of_cmds
,
640 sizeof(num_of_cmds
)))
643 if (num_of_cmds
> MMC_IOC_MAX_CMDS
)
646 idata
= kcalloc(num_of_cmds
, sizeof(*idata
), GFP_KERNEL
);
650 for (i
= 0; i
< num_of_cmds
; i
++) {
651 idata
[i
] = mmc_blk_ioctl_copy_from_user(&cmds
[i
]);
652 if (IS_ERR(idata
[i
])) {
653 err
= PTR_ERR(idata
[i
]);
659 md
= mmc_blk_get(bdev
->bd_disk
);
665 card
= md
->queue
.card
;
673 for (i
= 0; i
< num_of_cmds
&& !ioc_err
; i
++)
674 ioc_err
= __mmc_blk_ioctl_cmd(card
, md
, idata
[i
]);
676 /* Always switch back to main area after RPMB access */
677 if (md
->area_type
& MMC_BLK_DATA_AREA_RPMB
)
678 mmc_blk_part_switch(card
, dev_get_drvdata(&card
->dev
));
682 /* copy to user if data and response */
683 for (i
= 0; i
< num_of_cmds
&& !err
; i
++)
684 err
= mmc_blk_ioctl_copy_to_user(&cmds
[i
], idata
[i
]);
689 for (i
= 0; i
< num_of_cmds
; i
++) {
690 kfree(idata
[i
]->buf
);
694 return ioc_err
? ioc_err
: err
;
697 static int mmc_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
698 unsigned int cmd
, unsigned long arg
)
702 return mmc_blk_ioctl_cmd(bdev
,
703 (struct mmc_ioc_cmd __user
*)arg
);
704 case MMC_IOC_MULTI_CMD
:
705 return mmc_blk_ioctl_multi_cmd(bdev
,
706 (struct mmc_ioc_multi_cmd __user
*)arg
);
713 static int mmc_blk_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
714 unsigned int cmd
, unsigned long arg
)
716 return mmc_blk_ioctl(bdev
, mode
, cmd
, (unsigned long) compat_ptr(arg
));
720 static const struct block_device_operations mmc_bdops
= {
721 .open
= mmc_blk_open
,
722 .release
= mmc_blk_release
,
723 .getgeo
= mmc_blk_getgeo
,
724 .owner
= THIS_MODULE
,
725 .ioctl
= mmc_blk_ioctl
,
727 .compat_ioctl
= mmc_blk_compat_ioctl
,
731 static int mmc_blk_part_switch_pre(struct mmc_card
*card
,
732 unsigned int part_type
)
736 if (part_type
== EXT_CSD_PART_CONFIG_ACC_RPMB
) {
737 if (card
->ext_csd
.cmdq_en
) {
738 ret
= mmc_cmdq_disable(card
);
742 mmc_retune_pause(card
->host
);
748 static int mmc_blk_part_switch_post(struct mmc_card
*card
,
749 unsigned int part_type
)
753 if (part_type
== EXT_CSD_PART_CONFIG_ACC_RPMB
) {
754 mmc_retune_unpause(card
->host
);
755 if (card
->reenable_cmdq
&& !card
->ext_csd
.cmdq_en
)
756 ret
= mmc_cmdq_enable(card
);
762 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
763 struct mmc_blk_data
*md
)
766 struct mmc_blk_data
*main_md
= dev_get_drvdata(&card
->dev
);
768 if (main_md
->part_curr
== md
->part_type
)
771 if (mmc_card_mmc(card
)) {
772 u8 part_config
= card
->ext_csd
.part_config
;
774 ret
= mmc_blk_part_switch_pre(card
, md
->part_type
);
778 part_config
&= ~EXT_CSD_PART_CONFIG_ACC_MASK
;
779 part_config
|= md
->part_type
;
781 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
782 EXT_CSD_PART_CONFIG
, part_config
,
783 card
->ext_csd
.part_time
);
785 mmc_blk_part_switch_post(card
, md
->part_type
);
789 card
->ext_csd
.part_config
= part_config
;
791 ret
= mmc_blk_part_switch_post(card
, main_md
->part_curr
);
794 main_md
->part_curr
= md
->part_type
;
798 static int mmc_sd_num_wr_blocks(struct mmc_card
*card
, u32
*written_blocks
)
804 struct mmc_request mrq
= {};
805 struct mmc_command cmd
= {};
806 struct mmc_data data
= {};
808 struct scatterlist sg
;
810 cmd
.opcode
= MMC_APP_CMD
;
811 cmd
.arg
= card
->rca
<< 16;
812 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
814 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
817 if (!mmc_host_is_spi(card
->host
) && !(cmd
.resp
[0] & R1_APP_CMD
))
820 memset(&cmd
, 0, sizeof(struct mmc_command
));
822 cmd
.opcode
= SD_APP_SEND_NUM_WR_BLKS
;
824 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
828 data
.flags
= MMC_DATA_READ
;
831 mmc_set_data_timeout(&data
, card
);
836 blocks
= kmalloc(4, GFP_KERNEL
);
840 sg_init_one(&sg
, blocks
, 4);
842 mmc_wait_for_req(card
->host
, &mrq
);
844 result
= ntohl(*blocks
);
847 if (cmd
.error
|| data
.error
)
850 *written_blocks
= result
;
855 static int get_card_status(struct mmc_card
*card
, u32
*status
, int retries
)
857 struct mmc_command cmd
= {};
860 cmd
.opcode
= MMC_SEND_STATUS
;
861 if (!mmc_host_is_spi(card
->host
))
862 cmd
.arg
= card
->rca
<< 16;
863 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
864 err
= mmc_wait_for_cmd(card
->host
, &cmd
, retries
);
866 *status
= cmd
.resp
[0];
870 static int card_busy_detect(struct mmc_card
*card
, unsigned int timeout_ms
,
871 bool hw_busy_detect
, struct request
*req
, bool *gen_err
)
873 unsigned long timeout
= jiffies
+ msecs_to_jiffies(timeout_ms
);
878 err
= get_card_status(card
, &status
, 5);
880 pr_err("%s: error %d requesting status\n",
881 req
->rq_disk
->disk_name
, err
);
885 if (status
& R1_ERROR
) {
886 pr_err("%s: %s: error sending status cmd, status %#x\n",
887 req
->rq_disk
->disk_name
, __func__
, status
);
891 /* We may rely on the host hw to handle busy detection.*/
892 if ((card
->host
->caps
& MMC_CAP_WAIT_WHILE_BUSY
) &&
897 * Timeout if the device never becomes ready for data and never
898 * leaves the program state.
900 if (time_after(jiffies
, timeout
)) {
901 pr_err("%s: Card stuck in programming state! %s %s\n",
902 mmc_hostname(card
->host
),
903 req
->rq_disk
->disk_name
, __func__
);
908 * Some cards mishandle the status bits,
909 * so make sure to check both the busy
910 * indication and the card state.
912 } while (!(status
& R1_READY_FOR_DATA
) ||
913 (R1_CURRENT_STATE(status
) == R1_STATE_PRG
));
918 static int send_stop(struct mmc_card
*card
, unsigned int timeout_ms
,
919 struct request
*req
, bool *gen_err
, u32
*stop_status
)
921 struct mmc_host
*host
= card
->host
;
922 struct mmc_command cmd
= {};
924 bool use_r1b_resp
= rq_data_dir(req
) == WRITE
;
927 * Normally we use R1B responses for WRITE, but in cases where the host
928 * has specified a max_busy_timeout we need to validate it. A failure
929 * means we need to prevent the host from doing hw busy detection, which
930 * is done by converting to a R1 response instead.
932 if (host
->max_busy_timeout
&& (timeout_ms
> host
->max_busy_timeout
))
933 use_r1b_resp
= false;
935 cmd
.opcode
= MMC_STOP_TRANSMISSION
;
937 cmd
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
938 cmd
.busy_timeout
= timeout_ms
;
940 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
943 err
= mmc_wait_for_cmd(host
, &cmd
, 5);
947 *stop_status
= cmd
.resp
[0];
949 /* No need to check card status in case of READ. */
950 if (rq_data_dir(req
) == READ
)
953 if (!mmc_host_is_spi(host
) &&
954 (*stop_status
& R1_ERROR
)) {
955 pr_err("%s: %s: general error sending stop command, resp %#x\n",
956 req
->rq_disk
->disk_name
, __func__
, *stop_status
);
960 return card_busy_detect(card
, timeout_ms
, use_r1b_resp
, req
, gen_err
);
963 #define ERR_NOMEDIUM 3
966 #define ERR_CONTINUE 0
968 static int mmc_blk_cmd_error(struct request
*req
, const char *name
, int error
,
969 bool status_valid
, u32 status
)
973 /* response crc error, retry the r/w cmd */
974 pr_err("%s: %s sending %s command, card status %#x\n",
975 req
->rq_disk
->disk_name
, "response CRC error",
980 pr_err("%s: %s sending %s command, card status %#x\n",
981 req
->rq_disk
->disk_name
, "timed out", name
, status
);
983 /* If the status cmd initially failed, retry the r/w cmd */
985 pr_err("%s: status not valid, retrying timeout\n",
986 req
->rq_disk
->disk_name
);
991 * If it was a r/w cmd crc error, or illegal command
992 * (eg, issued in wrong state) then retry - we should
993 * have corrected the state problem above.
995 if (status
& (R1_COM_CRC_ERROR
| R1_ILLEGAL_COMMAND
)) {
996 pr_err("%s: command error, retrying timeout\n",
997 req
->rq_disk
->disk_name
);
1001 /* Otherwise abort the command */
1005 /* We don't understand the error code the driver gave us */
1006 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1007 req
->rq_disk
->disk_name
, error
, status
);
1013 * Initial r/w and stop cmd error recovery.
1014 * We don't know whether the card received the r/w cmd or not, so try to
1015 * restore things back to a sane state. Essentially, we do this as follows:
1016 * - Obtain card status. If the first attempt to obtain card status fails,
1017 * the status word will reflect the failed status cmd, not the failed
1018 * r/w cmd. If we fail to obtain card status, it suggests we can no
1019 * longer communicate with the card.
1020 * - Check the card state. If the card received the cmd but there was a
1021 * transient problem with the response, it might still be in a data transfer
1022 * mode. Try to send it a stop command. If this fails, we can't recover.
1023 * - If the r/w cmd failed due to a response CRC error, it was probably
1024 * transient, so retry the cmd.
1025 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1026 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1027 * illegal cmd, retry.
1028 * Otherwise we don't understand what happened, so abort.
1030 static int mmc_blk_cmd_recovery(struct mmc_card
*card
, struct request
*req
,
1031 struct mmc_blk_request
*brq
, bool *ecc_err
, bool *gen_err
)
1033 bool prev_cmd_status_valid
= true;
1034 u32 status
, stop_status
= 0;
1037 if (mmc_card_removed(card
))
1038 return ERR_NOMEDIUM
;
1041 * Try to get card status which indicates both the card state
1042 * and why there was no response. If the first attempt fails,
1043 * we can't be sure the returned status is for the r/w command.
1045 for (retry
= 2; retry
>= 0; retry
--) {
1046 err
= get_card_status(card
, &status
, 0);
1050 /* Re-tune if needed */
1051 mmc_retune_recheck(card
->host
);
1053 prev_cmd_status_valid
= false;
1054 pr_err("%s: error %d sending status command, %sing\n",
1055 req
->rq_disk
->disk_name
, err
, retry
? "retry" : "abort");
1058 /* We couldn't get a response from the card. Give up. */
1060 /* Check if the card is removed */
1061 if (mmc_detect_card_removed(card
->host
))
1062 return ERR_NOMEDIUM
;
1066 /* Flag ECC errors */
1067 if ((status
& R1_CARD_ECC_FAILED
) ||
1068 (brq
->stop
.resp
[0] & R1_CARD_ECC_FAILED
) ||
1069 (brq
->cmd
.resp
[0] & R1_CARD_ECC_FAILED
))
1072 /* Flag General errors */
1073 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
)
1074 if ((status
& R1_ERROR
) ||
1075 (brq
->stop
.resp
[0] & R1_ERROR
)) {
1076 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1077 req
->rq_disk
->disk_name
, __func__
,
1078 brq
->stop
.resp
[0], status
);
1083 * Check the current card state. If it is in some data transfer
1084 * mode, tell it to stop (and hopefully transition back to TRAN.)
1086 if (R1_CURRENT_STATE(status
) == R1_STATE_DATA
||
1087 R1_CURRENT_STATE(status
) == R1_STATE_RCV
) {
1088 err
= send_stop(card
,
1089 DIV_ROUND_UP(brq
->data
.timeout_ns
, 1000000),
1090 req
, gen_err
, &stop_status
);
1092 pr_err("%s: error %d sending stop command\n",
1093 req
->rq_disk
->disk_name
, err
);
1095 * If the stop cmd also timed out, the card is probably
1096 * not present, so abort. Other errors are bad news too.
1101 if (stop_status
& R1_CARD_ECC_FAILED
)
1105 /* Check for set block count errors */
1107 return mmc_blk_cmd_error(req
, "SET_BLOCK_COUNT", brq
->sbc
.error
,
1108 prev_cmd_status_valid
, status
);
1110 /* Check for r/w command errors */
1112 return mmc_blk_cmd_error(req
, "r/w cmd", brq
->cmd
.error
,
1113 prev_cmd_status_valid
, status
);
1116 if (!brq
->stop
.error
)
1117 return ERR_CONTINUE
;
1119 /* Now for stop errors. These aren't fatal to the transfer. */
1120 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1121 req
->rq_disk
->disk_name
, brq
->stop
.error
,
1122 brq
->cmd
.resp
[0], status
);
1125 * Subsitute in our own stop status as this will give the error
1126 * state which happened during the execution of the r/w command.
1129 brq
->stop
.resp
[0] = stop_status
;
1130 brq
->stop
.error
= 0;
1132 return ERR_CONTINUE
;
1135 static int mmc_blk_reset(struct mmc_blk_data
*md
, struct mmc_host
*host
,
1140 if (md
->reset_done
& type
)
1143 md
->reset_done
|= type
;
1144 err
= mmc_hw_reset(host
);
1145 /* Ensure we switch back to the correct partition */
1146 if (err
!= -EOPNOTSUPP
) {
1147 struct mmc_blk_data
*main_md
=
1148 dev_get_drvdata(&host
->card
->dev
);
1151 main_md
->part_curr
= main_md
->part_type
;
1152 part_err
= mmc_blk_part_switch(host
->card
, md
);
1155 * We have failed to get back into the correct
1156 * partition, so we need to abort the whole request.
1164 static inline void mmc_blk_reset_success(struct mmc_blk_data
*md
, int type
)
1166 md
->reset_done
&= ~type
;
1169 int mmc_access_rpmb(struct mmc_queue
*mq
)
1171 struct mmc_blk_data
*md
= mq
->blkdata
;
1173 * If this is a RPMB partition access, return ture
1175 if (md
&& md
->part_type
== EXT_CSD_PART_CONFIG_ACC_RPMB
)
1181 static void mmc_blk_issue_discard_rq(struct mmc_queue
*mq
, struct request
*req
)
1183 struct mmc_blk_data
*md
= mq
->blkdata
;
1184 struct mmc_card
*card
= md
->queue
.card
;
1185 unsigned int from
, nr
, arg
;
1186 int err
= 0, type
= MMC_BLK_DISCARD
;
1187 blk_status_t status
= BLK_STS_OK
;
1189 if (!mmc_can_erase(card
)) {
1190 status
= BLK_STS_NOTSUPP
;
1194 from
= blk_rq_pos(req
);
1195 nr
= blk_rq_sectors(req
);
1197 if (mmc_can_discard(card
))
1198 arg
= MMC_DISCARD_ARG
;
1199 else if (mmc_can_trim(card
))
1202 arg
= MMC_ERASE_ARG
;
1205 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1206 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1207 INAND_CMD38_ARG_EXT_CSD
,
1208 arg
== MMC_TRIM_ARG
?
1209 INAND_CMD38_ARG_TRIM
:
1210 INAND_CMD38_ARG_ERASE
,
1214 err
= mmc_erase(card
, from
, nr
, arg
);
1215 } while (err
== -EIO
&& !mmc_blk_reset(md
, card
->host
, type
));
1217 status
= BLK_STS_IOERR
;
1219 mmc_blk_reset_success(md
, type
);
1221 blk_end_request(req
, status
, blk_rq_bytes(req
));
1224 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue
*mq
,
1225 struct request
*req
)
1227 struct mmc_blk_data
*md
= mq
->blkdata
;
1228 struct mmc_card
*card
= md
->queue
.card
;
1229 unsigned int from
, nr
, arg
;
1230 int err
= 0, type
= MMC_BLK_SECDISCARD
;
1231 blk_status_t status
= BLK_STS_OK
;
1233 if (!(mmc_can_secure_erase_trim(card
))) {
1234 status
= BLK_STS_NOTSUPP
;
1238 from
= blk_rq_pos(req
);
1239 nr
= blk_rq_sectors(req
);
1241 if (mmc_can_trim(card
) && !mmc_erase_group_aligned(card
, from
, nr
))
1242 arg
= MMC_SECURE_TRIM1_ARG
;
1244 arg
= MMC_SECURE_ERASE_ARG
;
1247 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1248 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1249 INAND_CMD38_ARG_EXT_CSD
,
1250 arg
== MMC_SECURE_TRIM1_ARG
?
1251 INAND_CMD38_ARG_SECTRIM1
:
1252 INAND_CMD38_ARG_SECERASE
,
1258 err
= mmc_erase(card
, from
, nr
, arg
);
1262 status
= BLK_STS_IOERR
;
1266 if (arg
== MMC_SECURE_TRIM1_ARG
) {
1267 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1268 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1269 INAND_CMD38_ARG_EXT_CSD
,
1270 INAND_CMD38_ARG_SECTRIM2
,
1276 err
= mmc_erase(card
, from
, nr
, MMC_SECURE_TRIM2_ARG
);
1280 status
= BLK_STS_IOERR
;
1286 if (err
&& !mmc_blk_reset(md
, card
->host
, type
))
1289 mmc_blk_reset_success(md
, type
);
1291 blk_end_request(req
, status
, blk_rq_bytes(req
));
1294 static void mmc_blk_issue_flush(struct mmc_queue
*mq
, struct request
*req
)
1296 struct mmc_blk_data
*md
= mq
->blkdata
;
1297 struct mmc_card
*card
= md
->queue
.card
;
1300 ret
= mmc_flush_cache(card
);
1301 blk_end_request_all(req
, ret
? BLK_STS_IOERR
: BLK_STS_OK
);
1305 * Reformat current write as a reliable write, supporting
1306 * both legacy and the enhanced reliable write MMC cards.
1307 * In each transfer we'll handle only as much as a single
1308 * reliable write can handle, thus finish the request in
1309 * partial completions.
1311 static inline void mmc_apply_rel_rw(struct mmc_blk_request
*brq
,
1312 struct mmc_card
*card
,
1313 struct request
*req
)
1315 if (!(card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
)) {
1316 /* Legacy mode imposes restrictions on transfers. */
1317 if (!IS_ALIGNED(blk_rq_pos(req
), card
->ext_csd
.rel_sectors
))
1318 brq
->data
.blocks
= 1;
1320 if (brq
->data
.blocks
> card
->ext_csd
.rel_sectors
)
1321 brq
->data
.blocks
= card
->ext_csd
.rel_sectors
;
1322 else if (brq
->data
.blocks
< card
->ext_csd
.rel_sectors
)
1323 brq
->data
.blocks
= 1;
1327 #define CMD_ERRORS \
1328 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1329 R1_ADDRESS_ERROR | /* Misaligned address */ \
1330 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1331 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1332 R1_CC_ERROR | /* Card controller error */ \
1333 R1_ERROR) /* General/unknown error */
1335 static enum mmc_blk_status
mmc_blk_err_check(struct mmc_card
*card
,
1336 struct mmc_async_req
*areq
)
1338 struct mmc_queue_req
*mq_mrq
= container_of(areq
, struct mmc_queue_req
,
1340 struct mmc_blk_request
*brq
= &mq_mrq
->brq
;
1341 struct request
*req
= mq_mrq
->req
;
1342 int need_retune
= card
->host
->need_retune
;
1343 bool ecc_err
= false;
1344 bool gen_err
= false;
1347 * sbc.error indicates a problem with the set block count
1348 * command. No data will have been transferred.
1350 * cmd.error indicates a problem with the r/w command. No
1351 * data will have been transferred.
1353 * stop.error indicates a problem with the stop command. Data
1354 * may have been transferred, or may still be transferring.
1356 if (brq
->sbc
.error
|| brq
->cmd
.error
|| brq
->stop
.error
||
1358 switch (mmc_blk_cmd_recovery(card
, req
, brq
, &ecc_err
, &gen_err
)) {
1360 return MMC_BLK_RETRY
;
1362 return MMC_BLK_ABORT
;
1364 return MMC_BLK_NOMEDIUM
;
1371 * Check for errors relating to the execution of the
1372 * initial command - such as address errors. No data
1373 * has been transferred.
1375 if (brq
->cmd
.resp
[0] & CMD_ERRORS
) {
1376 pr_err("%s: r/w command failed, status = %#x\n",
1377 req
->rq_disk
->disk_name
, brq
->cmd
.resp
[0]);
1378 return MMC_BLK_ABORT
;
1382 * Everything else is either success, or a data error of some
1383 * kind. If it was a write, we may have transitioned to
1384 * program mode, which we have to wait for it to complete.
1386 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
) {
1389 /* Check stop command response */
1390 if (brq
->stop
.resp
[0] & R1_ERROR
) {
1391 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1392 req
->rq_disk
->disk_name
, __func__
,
1397 err
= card_busy_detect(card
, MMC_BLK_TIMEOUT_MS
, false, req
,
1400 return MMC_BLK_CMD_ERR
;
1403 /* if general error occurs, retry the write operation. */
1405 pr_warn("%s: retrying write for general error\n",
1406 req
->rq_disk
->disk_name
);
1407 return MMC_BLK_RETRY
;
1410 if (brq
->data
.error
) {
1411 if (need_retune
&& !brq
->retune_retry_done
) {
1412 pr_debug("%s: retrying because a re-tune was needed\n",
1413 req
->rq_disk
->disk_name
);
1414 brq
->retune_retry_done
= 1;
1415 return MMC_BLK_RETRY
;
1417 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1418 req
->rq_disk
->disk_name
, brq
->data
.error
,
1419 (unsigned)blk_rq_pos(req
),
1420 (unsigned)blk_rq_sectors(req
),
1421 brq
->cmd
.resp
[0], brq
->stop
.resp
[0]);
1423 if (rq_data_dir(req
) == READ
) {
1425 return MMC_BLK_ECC_ERR
;
1426 return MMC_BLK_DATA_ERR
;
1428 return MMC_BLK_CMD_ERR
;
1432 if (!brq
->data
.bytes_xfered
)
1433 return MMC_BLK_RETRY
;
1435 if (blk_rq_bytes(req
) != brq
->data
.bytes_xfered
)
1436 return MMC_BLK_PARTIAL
;
1438 return MMC_BLK_SUCCESS
;
1441 static void mmc_blk_data_prep(struct mmc_queue
*mq
, struct mmc_queue_req
*mqrq
,
1442 int disable_multi
, bool *do_rel_wr
,
1445 struct mmc_blk_data
*md
= mq
->blkdata
;
1446 struct mmc_card
*card
= md
->queue
.card
;
1447 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1448 struct request
*req
= mqrq
->req
;
1451 * Reliable writes are used to implement Forced Unit Access and
1452 * are supported only on MMCs.
1454 *do_rel_wr
= (req
->cmd_flags
& REQ_FUA
) &&
1455 rq_data_dir(req
) == WRITE
&&
1456 (md
->flags
& MMC_BLK_REL_WR
);
1458 memset(brq
, 0, sizeof(struct mmc_blk_request
));
1460 brq
->mrq
.data
= &brq
->data
;
1462 brq
->stop
.opcode
= MMC_STOP_TRANSMISSION
;
1465 if (rq_data_dir(req
) == READ
) {
1466 brq
->data
.flags
= MMC_DATA_READ
;
1467 brq
->stop
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
1469 brq
->data
.flags
= MMC_DATA_WRITE
;
1470 brq
->stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1473 brq
->data
.blksz
= 512;
1474 brq
->data
.blocks
= blk_rq_sectors(req
);
1477 * The block layer doesn't support all sector count
1478 * restrictions, so we need to be prepared for too big
1481 if (brq
->data
.blocks
> card
->host
->max_blk_count
)
1482 brq
->data
.blocks
= card
->host
->max_blk_count
;
1484 if (brq
->data
.blocks
> 1) {
1486 * After a read error, we redo the request one sector
1487 * at a time in order to accurately determine which
1488 * sectors can be read successfully.
1491 brq
->data
.blocks
= 1;
1494 * Some controllers have HW issues while operating
1495 * in multiple I/O mode
1497 if (card
->host
->ops
->multi_io_quirk
)
1498 brq
->data
.blocks
= card
->host
->ops
->multi_io_quirk(card
,
1499 (rq_data_dir(req
) == READ
) ?
1500 MMC_DATA_READ
: MMC_DATA_WRITE
,
1505 mmc_apply_rel_rw(brq
, card
, req
);
1508 * Data tag is used only during writing meta data to speed
1509 * up write and any subsequent read of this meta data
1511 *do_data_tag
= card
->ext_csd
.data_tag_unit_size
&&
1512 (req
->cmd_flags
& REQ_META
) &&
1513 (rq_data_dir(req
) == WRITE
) &&
1514 ((brq
->data
.blocks
* brq
->data
.blksz
) >=
1515 card
->ext_csd
.data_tag_unit_size
);
1517 mmc_set_data_timeout(&brq
->data
, card
);
1519 brq
->data
.sg
= mqrq
->sg
;
1520 brq
->data
.sg_len
= mmc_queue_map_sg(mq
, mqrq
);
1523 * Adjust the sg list so it is the same size as the
1526 if (brq
->data
.blocks
!= blk_rq_sectors(req
)) {
1527 int i
, data_size
= brq
->data
.blocks
<< 9;
1528 struct scatterlist
*sg
;
1530 for_each_sg(brq
->data
.sg
, sg
, brq
->data
.sg_len
, i
) {
1531 data_size
-= sg
->length
;
1532 if (data_size
<= 0) {
1533 sg
->length
+= data_size
;
1538 brq
->data
.sg_len
= i
;
1541 mqrq
->areq
.mrq
= &brq
->mrq
;
1543 mmc_queue_bounce_pre(mqrq
);
1546 static void mmc_blk_rw_rq_prep(struct mmc_queue_req
*mqrq
,
1547 struct mmc_card
*card
,
1549 struct mmc_queue
*mq
)
1551 u32 readcmd
, writecmd
;
1552 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1553 struct request
*req
= mqrq
->req
;
1554 struct mmc_blk_data
*md
= mq
->blkdata
;
1555 bool do_rel_wr
, do_data_tag
;
1557 mmc_blk_data_prep(mq
, mqrq
, disable_multi
, &do_rel_wr
, &do_data_tag
);
1559 brq
->mrq
.cmd
= &brq
->cmd
;
1561 brq
->cmd
.arg
= blk_rq_pos(req
);
1562 if (!mmc_card_blockaddr(card
))
1564 brq
->cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
1566 if (brq
->data
.blocks
> 1 || do_rel_wr
) {
1567 /* SPI multiblock writes terminate using a special
1568 * token, not a STOP_TRANSMISSION request.
1570 if (!mmc_host_is_spi(card
->host
) ||
1571 rq_data_dir(req
) == READ
)
1572 brq
->mrq
.stop
= &brq
->stop
;
1573 readcmd
= MMC_READ_MULTIPLE_BLOCK
;
1574 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
1576 brq
->mrq
.stop
= NULL
;
1577 readcmd
= MMC_READ_SINGLE_BLOCK
;
1578 writecmd
= MMC_WRITE_BLOCK
;
1580 brq
->cmd
.opcode
= rq_data_dir(req
) == READ
? readcmd
: writecmd
;
1583 * Pre-defined multi-block transfers are preferable to
1584 * open ended-ones (and necessary for reliable writes).
1585 * However, it is not sufficient to just send CMD23,
1586 * and avoid the final CMD12, as on an error condition
1587 * CMD12 (stop) needs to be sent anyway. This, coupled
1588 * with Auto-CMD23 enhancements provided by some
1589 * hosts, means that the complexity of dealing
1590 * with this is best left to the host. If CMD23 is
1591 * supported by card and host, we'll fill sbc in and let
1592 * the host deal with handling it correctly. This means
1593 * that for hosts that don't expose MMC_CAP_CMD23, no
1594 * change of behavior will be observed.
1596 * N.B: Some MMC cards experience perf degradation.
1597 * We'll avoid using CMD23-bounded multiblock writes for
1598 * these, while retaining features like reliable writes.
1600 if ((md
->flags
& MMC_BLK_CMD23
) && mmc_op_multi(brq
->cmd
.opcode
) &&
1601 (do_rel_wr
|| !(card
->quirks
& MMC_QUIRK_BLK_NO_CMD23
) ||
1603 brq
->sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
1604 brq
->sbc
.arg
= brq
->data
.blocks
|
1605 (do_rel_wr
? (1 << 31) : 0) |
1606 (do_data_tag
? (1 << 29) : 0);
1607 brq
->sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1608 brq
->mrq
.sbc
= &brq
->sbc
;
1611 mqrq
->areq
.err_check
= mmc_blk_err_check
;
1614 static bool mmc_blk_rw_cmd_err(struct mmc_blk_data
*md
, struct mmc_card
*card
,
1615 struct mmc_blk_request
*brq
, struct request
*req
,
1616 bool old_req_pending
)
1621 * If this is an SD card and we're writing, we can first
1622 * mark the known good sectors as ok.
1624 * If the card is not SD, we can still ok written sectors
1625 * as reported by the controller (which might be less than
1626 * the real number of written sectors, but never more).
1628 if (mmc_card_sd(card
)) {
1632 err
= mmc_sd_num_wr_blocks(card
, &blocks
);
1634 req_pending
= old_req_pending
;
1636 req_pending
= blk_end_request(req
, 0, blocks
<< 9);
1638 req_pending
= blk_end_request(req
, 0, brq
->data
.bytes_xfered
);
1643 static void mmc_blk_rw_cmd_abort(struct mmc_queue
*mq
, struct mmc_card
*card
,
1644 struct request
*req
,
1645 struct mmc_queue_req
*mqrq
)
1647 if (mmc_card_removed(card
))
1648 req
->rq_flags
|= RQF_QUIET
;
1649 while (blk_end_request(req
, BLK_STS_IOERR
, blk_rq_cur_bytes(req
)));
1650 mmc_queue_req_free(mq
, mqrq
);
1654 * mmc_blk_rw_try_restart() - tries to restart the current async request
1655 * @mq: the queue with the card and host to restart
1656 * @req: a new request that want to be started after the current one
1658 static void mmc_blk_rw_try_restart(struct mmc_queue
*mq
, struct request
*req
,
1659 struct mmc_queue_req
*mqrq
)
1665 * If the card was removed, just cancel everything and return.
1667 if (mmc_card_removed(mq
->card
)) {
1668 req
->rq_flags
|= RQF_QUIET
;
1669 blk_end_request_all(req
, BLK_STS_IOERR
);
1670 mmc_queue_req_free(mq
, mqrq
);
1673 /* Else proceed and try to restart the current async request */
1674 mmc_blk_rw_rq_prep(mqrq
, mq
->card
, 0, mq
);
1675 mmc_start_areq(mq
->card
->host
, &mqrq
->areq
, NULL
);
1678 static void mmc_blk_issue_rw_rq(struct mmc_queue
*mq
, struct request
*new_req
)
1680 struct mmc_blk_data
*md
= mq
->blkdata
;
1681 struct mmc_card
*card
= md
->queue
.card
;
1682 struct mmc_blk_request
*brq
;
1683 int disable_multi
= 0, retry
= 0, type
, retune_retry_done
= 0;
1684 enum mmc_blk_status status
;
1685 struct mmc_queue_req
*mqrq_cur
= NULL
;
1686 struct mmc_queue_req
*mq_rq
;
1687 struct request
*old_req
;
1688 struct mmc_async_req
*new_areq
;
1689 struct mmc_async_req
*old_areq
;
1690 bool req_pending
= true;
1693 mqrq_cur
= mmc_queue_req_find(mq
, new_req
);
1696 mmc_blk_requeue(mq
->queue
, new_req
);
1707 * When 4KB native sector is enabled, only 8 blocks
1708 * multiple read or write is allowed
1710 if (mmc_large_sector(card
) &&
1711 !IS_ALIGNED(blk_rq_sectors(new_req
), 8)) {
1712 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1713 new_req
->rq_disk
->disk_name
);
1714 mmc_blk_rw_cmd_abort(mq
, card
, new_req
, mqrq_cur
);
1718 mmc_blk_rw_rq_prep(mqrq_cur
, card
, 0, mq
);
1719 new_areq
= &mqrq_cur
->areq
;
1723 old_areq
= mmc_start_areq(card
->host
, new_areq
, &status
);
1726 * We have just put the first request into the pipeline
1727 * and there is nothing more to do until it is
1734 * An asynchronous request has been completed and we proceed
1735 * to handle the result of it.
1737 mq_rq
= container_of(old_areq
, struct mmc_queue_req
, areq
);
1739 old_req
= mq_rq
->req
;
1740 type
= rq_data_dir(old_req
) == READ
? MMC_BLK_READ
: MMC_BLK_WRITE
;
1741 mmc_queue_bounce_post(mq_rq
);
1744 case MMC_BLK_SUCCESS
:
1745 case MMC_BLK_PARTIAL
:
1747 * A block was successfully transferred.
1749 mmc_blk_reset_success(md
, type
);
1751 req_pending
= blk_end_request(old_req
, BLK_STS_OK
,
1752 brq
->data
.bytes_xfered
);
1754 * If the blk_end_request function returns non-zero even
1755 * though all data has been transferred and no errors
1756 * were returned by the host controller, it's a bug.
1758 if (status
== MMC_BLK_SUCCESS
&& req_pending
) {
1759 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1760 __func__
, blk_rq_bytes(old_req
),
1761 brq
->data
.bytes_xfered
);
1762 mmc_blk_rw_cmd_abort(mq
, card
, old_req
, mq_rq
);
1766 case MMC_BLK_CMD_ERR
:
1767 req_pending
= mmc_blk_rw_cmd_err(md
, card
, brq
, old_req
, req_pending
);
1768 if (mmc_blk_reset(md
, card
->host
, type
)) {
1770 mmc_blk_rw_cmd_abort(mq
, card
, old_req
, mq_rq
);
1772 mmc_queue_req_free(mq
, mq_rq
);
1773 mmc_blk_rw_try_restart(mq
, new_req
, mqrq_cur
);
1777 mmc_queue_req_free(mq
, mq_rq
);
1778 mmc_blk_rw_try_restart(mq
, new_req
, mqrq_cur
);
1783 retune_retry_done
= brq
->retune_retry_done
;
1788 if (!mmc_blk_reset(md
, card
->host
, type
))
1790 mmc_blk_rw_cmd_abort(mq
, card
, old_req
, mq_rq
);
1791 mmc_blk_rw_try_restart(mq
, new_req
, mqrq_cur
);
1793 case MMC_BLK_DATA_ERR
: {
1796 err
= mmc_blk_reset(md
, card
->host
, type
);
1799 if (err
== -ENODEV
) {
1800 mmc_blk_rw_cmd_abort(mq
, card
, old_req
, mq_rq
);
1801 mmc_blk_rw_try_restart(mq
, new_req
, mqrq_cur
);
1806 case MMC_BLK_ECC_ERR
:
1807 if (brq
->data
.blocks
> 1) {
1808 /* Redo read one sector at a time */
1809 pr_warn("%s: retrying using single block read\n",
1810 old_req
->rq_disk
->disk_name
);
1815 * After an error, we redo I/O one sector at a
1816 * time, so we only reach here after trying to
1817 * read a single sector.
1819 req_pending
= blk_end_request(old_req
, BLK_STS_IOERR
,
1822 mmc_queue_req_free(mq
, mq_rq
);
1823 mmc_blk_rw_try_restart(mq
, new_req
, mqrq_cur
);
1827 case MMC_BLK_NOMEDIUM
:
1828 mmc_blk_rw_cmd_abort(mq
, card
, old_req
, mq_rq
);
1829 mmc_blk_rw_try_restart(mq
, new_req
, mqrq_cur
);
1832 pr_err("%s: Unhandled return value (%d)",
1833 old_req
->rq_disk
->disk_name
, status
);
1834 mmc_blk_rw_cmd_abort(mq
, card
, old_req
, mq_rq
);
1835 mmc_blk_rw_try_restart(mq
, new_req
, mqrq_cur
);
1841 * In case of a incomplete request
1842 * prepare it again and resend.
1844 mmc_blk_rw_rq_prep(mq_rq
, card
,
1846 mmc_start_areq(card
->host
,
1847 &mq_rq
->areq
, NULL
);
1848 mq_rq
->brq
.retune_retry_done
= retune_retry_done
;
1850 } while (req_pending
);
1852 mmc_queue_req_free(mq
, mq_rq
);
1855 void mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
1858 struct mmc_blk_data
*md
= mq
->blkdata
;
1859 struct mmc_card
*card
= md
->queue
.card
;
1861 if (req
&& !mq
->qcnt
)
1862 /* claim host only for the first request */
1865 ret
= mmc_blk_part_switch(card
, md
);
1868 blk_end_request_all(req
, BLK_STS_IOERR
);
1873 if (req
&& req_op(req
) == REQ_OP_DISCARD
) {
1874 /* complete ongoing async transfer before issuing discard */
1876 mmc_blk_issue_rw_rq(mq
, NULL
);
1877 mmc_blk_issue_discard_rq(mq
, req
);
1878 } else if (req
&& req_op(req
) == REQ_OP_SECURE_ERASE
) {
1879 /* complete ongoing async transfer before issuing secure erase*/
1881 mmc_blk_issue_rw_rq(mq
, NULL
);
1882 mmc_blk_issue_secdiscard_rq(mq
, req
);
1883 } else if (req
&& req_op(req
) == REQ_OP_FLUSH
) {
1884 /* complete ongoing async transfer before issuing flush */
1886 mmc_blk_issue_rw_rq(mq
, NULL
);
1887 mmc_blk_issue_flush(mq
, req
);
1889 mmc_blk_issue_rw_rq(mq
, req
);
1890 card
->host
->context_info
.is_waiting_last_req
= false;
1898 static inline int mmc_blk_readonly(struct mmc_card
*card
)
1900 return mmc_card_readonly(card
) ||
1901 !(card
->csd
.cmdclass
& CCC_BLOCK_WRITE
);
1904 static struct mmc_blk_data
*mmc_blk_alloc_req(struct mmc_card
*card
,
1905 struct device
*parent
,
1908 const char *subname
,
1911 struct mmc_blk_data
*md
;
1914 devidx
= ida_simple_get(&mmc_blk_ida
, 0, max_devices
, GFP_KERNEL
);
1916 return ERR_PTR(devidx
);
1918 md
= kzalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
1924 md
->area_type
= area_type
;
1927 * Set the read-only status based on the supported commands
1928 * and the write protect switch.
1930 md
->read_only
= mmc_blk_readonly(card
);
1932 md
->disk
= alloc_disk(perdev_minors
);
1933 if (md
->disk
== NULL
) {
1938 spin_lock_init(&md
->lock
);
1939 INIT_LIST_HEAD(&md
->part
);
1942 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
, subname
);
1946 md
->queue
.blkdata
= md
;
1948 md
->disk
->major
= MMC_BLOCK_MAJOR
;
1949 md
->disk
->first_minor
= devidx
* perdev_minors
;
1950 md
->disk
->fops
= &mmc_bdops
;
1951 md
->disk
->private_data
= md
;
1952 md
->disk
->queue
= md
->queue
.queue
;
1953 md
->parent
= parent
;
1954 set_disk_ro(md
->disk
, md
->read_only
|| default_ro
);
1955 md
->disk
->flags
= GENHD_FL_EXT_DEVT
;
1956 if (area_type
& (MMC_BLK_DATA_AREA_RPMB
| MMC_BLK_DATA_AREA_BOOT
))
1957 md
->disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
1960 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1962 * - be set for removable media with permanent block devices
1963 * - be unset for removable block devices with permanent media
1965 * Since MMC block devices clearly fall under the second
1966 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1967 * should use the block device creation/destruction hotplug
1968 * messages to tell when the card is present.
1971 snprintf(md
->disk
->disk_name
, sizeof(md
->disk
->disk_name
),
1972 "mmcblk%u%s", card
->host
->index
, subname
? subname
: "");
1974 if (mmc_card_mmc(card
))
1975 blk_queue_logical_block_size(md
->queue
.queue
,
1976 card
->ext_csd
.data_sector_size
);
1978 blk_queue_logical_block_size(md
->queue
.queue
, 512);
1980 set_capacity(md
->disk
, size
);
1982 if (mmc_host_cmd23(card
->host
)) {
1983 if ((mmc_card_mmc(card
) &&
1984 card
->csd
.mmca_vsn
>= CSD_SPEC_VER_3
) ||
1985 (mmc_card_sd(card
) &&
1986 card
->scr
.cmds
& SD_SCR_CMD23_SUPPORT
))
1987 md
->flags
|= MMC_BLK_CMD23
;
1990 if (mmc_card_mmc(card
) &&
1991 md
->flags
& MMC_BLK_CMD23
&&
1992 ((card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
) ||
1993 card
->ext_csd
.rel_sectors
)) {
1994 md
->flags
|= MMC_BLK_REL_WR
;
1995 blk_queue_write_cache(md
->queue
.queue
, true, true);
2005 ida_simple_remove(&mmc_blk_ida
, devidx
);
2006 return ERR_PTR(ret
);
2009 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
2013 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
)) {
2015 * The EXT_CSD sector count is in number or 512 byte
2018 size
= card
->ext_csd
.sectors
;
2021 * The CSD capacity field is in units of read_blkbits.
2022 * set_capacity takes units of 512 bytes.
2024 size
= (typeof(sector_t
))card
->csd
.capacity
2025 << (card
->csd
.read_blkbits
- 9);
2028 return mmc_blk_alloc_req(card
, &card
->dev
, size
, false, NULL
,
2029 MMC_BLK_DATA_AREA_MAIN
);
2032 static int mmc_blk_alloc_part(struct mmc_card
*card
,
2033 struct mmc_blk_data
*md
,
2034 unsigned int part_type
,
2037 const char *subname
,
2041 struct mmc_blk_data
*part_md
;
2043 part_md
= mmc_blk_alloc_req(card
, disk_to_dev(md
->disk
), size
, default_ro
,
2044 subname
, area_type
);
2045 if (IS_ERR(part_md
))
2046 return PTR_ERR(part_md
);
2047 part_md
->part_type
= part_type
;
2048 list_add(&part_md
->part
, &md
->part
);
2050 string_get_size((u64
)get_capacity(part_md
->disk
), 512, STRING_UNITS_2
,
2051 cap_str
, sizeof(cap_str
));
2052 pr_info("%s: %s %s partition %u %s\n",
2053 part_md
->disk
->disk_name
, mmc_card_id(card
),
2054 mmc_card_name(card
), part_md
->part_type
, cap_str
);
2058 /* MMC Physical partitions consist of two boot partitions and
2059 * up to four general purpose partitions.
2060 * For each partition enabled in EXT_CSD a block device will be allocatedi
2061 * to provide access to the partition.
2064 static int mmc_blk_alloc_parts(struct mmc_card
*card
, struct mmc_blk_data
*md
)
2068 if (!mmc_card_mmc(card
))
2071 for (idx
= 0; idx
< card
->nr_parts
; idx
++) {
2072 if (card
->part
[idx
].size
) {
2073 ret
= mmc_blk_alloc_part(card
, md
,
2074 card
->part
[idx
].part_cfg
,
2075 card
->part
[idx
].size
>> 9,
2076 card
->part
[idx
].force_ro
,
2077 card
->part
[idx
].name
,
2078 card
->part
[idx
].area_type
);
2087 static void mmc_blk_remove_req(struct mmc_blk_data
*md
)
2089 struct mmc_card
*card
;
2093 * Flush remaining requests and free queues. It
2094 * is freeing the queue that stops new requests
2095 * from being accepted.
2097 card
= md
->queue
.card
;
2098 mmc_cleanup_queue(&md
->queue
);
2099 if (md
->disk
->flags
& GENHD_FL_UP
) {
2100 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2101 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2102 card
->ext_csd
.boot_ro_lockable
)
2103 device_remove_file(disk_to_dev(md
->disk
),
2104 &md
->power_ro_lock
);
2106 del_gendisk(md
->disk
);
2112 static void mmc_blk_remove_parts(struct mmc_card
*card
,
2113 struct mmc_blk_data
*md
)
2115 struct list_head
*pos
, *q
;
2116 struct mmc_blk_data
*part_md
;
2118 list_for_each_safe(pos
, q
, &md
->part
) {
2119 part_md
= list_entry(pos
, struct mmc_blk_data
, part
);
2121 mmc_blk_remove_req(part_md
);
2125 static int mmc_add_disk(struct mmc_blk_data
*md
)
2128 struct mmc_card
*card
= md
->queue
.card
;
2130 device_add_disk(md
->parent
, md
->disk
);
2131 md
->force_ro
.show
= force_ro_show
;
2132 md
->force_ro
.store
= force_ro_store
;
2133 sysfs_attr_init(&md
->force_ro
.attr
);
2134 md
->force_ro
.attr
.name
= "force_ro";
2135 md
->force_ro
.attr
.mode
= S_IRUGO
| S_IWUSR
;
2136 ret
= device_create_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2140 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2141 card
->ext_csd
.boot_ro_lockable
) {
2144 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_DIS
)
2147 mode
= S_IRUGO
| S_IWUSR
;
2149 md
->power_ro_lock
.show
= power_ro_lock_show
;
2150 md
->power_ro_lock
.store
= power_ro_lock_store
;
2151 sysfs_attr_init(&md
->power_ro_lock
.attr
);
2152 md
->power_ro_lock
.attr
.mode
= mode
;
2153 md
->power_ro_lock
.attr
.name
=
2154 "ro_lock_until_next_power_on";
2155 ret
= device_create_file(disk_to_dev(md
->disk
),
2156 &md
->power_ro_lock
);
2158 goto power_ro_lock_fail
;
2163 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2165 del_gendisk(md
->disk
);
2170 static int mmc_blk_probe(struct mmc_card
*card
)
2172 struct mmc_blk_data
*md
, *part_md
;
2177 * Check that the card supports the command class(es) we need.
2179 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
2182 mmc_fixup_device(card
, mmc_blk_fixups
);
2184 ret
= mmc_queue_alloc_shared_queue(card
);
2188 md
= mmc_blk_alloc(card
);
2190 mmc_queue_free_shared_queue(card
);
2194 string_get_size((u64
)get_capacity(md
->disk
), 512, STRING_UNITS_2
,
2195 cap_str
, sizeof(cap_str
));
2196 pr_info("%s: %s %s %s %s\n",
2197 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
2198 cap_str
, md
->read_only
? "(ro)" : "");
2200 if (mmc_blk_alloc_parts(card
, md
))
2203 dev_set_drvdata(&card
->dev
, md
);
2205 if (mmc_add_disk(md
))
2208 list_for_each_entry(part_md
, &md
->part
, part
) {
2209 if (mmc_add_disk(part_md
))
2213 pm_runtime_set_autosuspend_delay(&card
->dev
, 3000);
2214 pm_runtime_use_autosuspend(&card
->dev
);
2217 * Don't enable runtime PM for SD-combo cards here. Leave that
2218 * decision to be taken during the SDIO init sequence instead.
2220 if (card
->type
!= MMC_TYPE_SD_COMBO
) {
2221 pm_runtime_set_active(&card
->dev
);
2222 pm_runtime_enable(&card
->dev
);
2228 mmc_blk_remove_parts(card
, md
);
2229 mmc_blk_remove_req(md
);
2230 mmc_queue_free_shared_queue(card
);
2234 static void mmc_blk_remove(struct mmc_card
*card
)
2236 struct mmc_blk_data
*md
= dev_get_drvdata(&card
->dev
);
2238 mmc_blk_remove_parts(card
, md
);
2239 pm_runtime_get_sync(&card
->dev
);
2240 mmc_claim_host(card
->host
);
2241 mmc_blk_part_switch(card
, md
);
2242 mmc_release_host(card
->host
);
2243 if (card
->type
!= MMC_TYPE_SD_COMBO
)
2244 pm_runtime_disable(&card
->dev
);
2245 pm_runtime_put_noidle(&card
->dev
);
2246 mmc_blk_remove_req(md
);
2247 dev_set_drvdata(&card
->dev
, NULL
);
2248 mmc_queue_free_shared_queue(card
);
2251 static int _mmc_blk_suspend(struct mmc_card
*card
)
2253 struct mmc_blk_data
*part_md
;
2254 struct mmc_blk_data
*md
= dev_get_drvdata(&card
->dev
);
2257 mmc_queue_suspend(&md
->queue
);
2258 list_for_each_entry(part_md
, &md
->part
, part
) {
2259 mmc_queue_suspend(&part_md
->queue
);
2265 static void mmc_blk_shutdown(struct mmc_card
*card
)
2267 _mmc_blk_suspend(card
);
2270 #ifdef CONFIG_PM_SLEEP
2271 static int mmc_blk_suspend(struct device
*dev
)
2273 struct mmc_card
*card
= mmc_dev_to_card(dev
);
2275 return _mmc_blk_suspend(card
);
2278 static int mmc_blk_resume(struct device
*dev
)
2280 struct mmc_blk_data
*part_md
;
2281 struct mmc_blk_data
*md
= dev_get_drvdata(dev
);
2285 * Resume involves the card going into idle state,
2286 * so current partition is always the main one.
2288 md
->part_curr
= md
->part_type
;
2289 mmc_queue_resume(&md
->queue
);
2290 list_for_each_entry(part_md
, &md
->part
, part
) {
2291 mmc_queue_resume(&part_md
->queue
);
2298 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops
, mmc_blk_suspend
, mmc_blk_resume
);
2300 static struct mmc_driver mmc_driver
= {
2303 .pm
= &mmc_blk_pm_ops
,
2305 .probe
= mmc_blk_probe
,
2306 .remove
= mmc_blk_remove
,
2307 .shutdown
= mmc_blk_shutdown
,
2310 static int __init
mmc_blk_init(void)
2314 if (perdev_minors
!= CONFIG_MMC_BLOCK_MINORS
)
2315 pr_info("mmcblk: using %d minors per device\n", perdev_minors
);
2317 max_devices
= min(MAX_DEVICES
, (1 << MINORBITS
) / perdev_minors
);
2319 res
= register_blkdev(MMC_BLOCK_MAJOR
, "mmc");
2323 res
= mmc_register_driver(&mmc_driver
);
2329 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
2334 static void __exit
mmc_blk_exit(void)
2336 mmc_unregister_driver(&mmc_driver
);
2337 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
2340 module_init(mmc_blk_init
);
2341 module_exit(mmc_blk_exit
);
2343 MODULE_LICENSE("GPL");
2344 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");