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>
39 #include <linux/mmc/ioctl.h>
40 #include <linux/mmc/card.h>
41 #include <linux/mmc/host.h>
42 #include <linux/mmc/mmc.h>
43 #include <linux/mmc/sd.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
61 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
62 #define MMC_SANITIZE_REQ_TIMEOUT 240000
63 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
65 #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
66 (req->cmd_flags & REQ_META)) && \
67 (rq_data_dir(req) == WRITE))
68 #define PACKED_CMD_VER 0x01
69 #define PACKED_CMD_WR 0x02
71 static DEFINE_MUTEX(block_mutex
);
74 * The defaults come from config options but can be overriden by module
77 static int perdev_minors
= CONFIG_MMC_BLOCK_MINORS
;
80 * We've only got one major, so number of mmcblk devices is
81 * limited to 256 / number of minors per device.
83 static int max_devices
;
85 /* 256 minors, so at most 256 separate devices */
86 static DECLARE_BITMAP(dev_use
, 256);
87 static DECLARE_BITMAP(name_use
, 256);
90 * There is one mmc_blk_data per slot.
95 struct mmc_queue queue
;
96 struct list_head part
;
99 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
100 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
101 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
104 unsigned int read_only
;
105 unsigned int part_type
;
106 unsigned int name_idx
;
107 unsigned int reset_done
;
108 #define MMC_BLK_READ BIT(0)
109 #define MMC_BLK_WRITE BIT(1)
110 #define MMC_BLK_DISCARD BIT(2)
111 #define MMC_BLK_SECDISCARD BIT(3)
114 * Only set in main mmc_blk_data associated
115 * with mmc_card with mmc_set_drvdata, and keeps
116 * track of the current selected device partition.
118 unsigned int part_curr
;
119 struct device_attribute force_ro
;
120 struct device_attribute power_ro_lock
;
124 static DEFINE_MUTEX(open_lock
);
127 MMC_PACKED_NR_IDX
= -1,
129 MMC_PACKED_NR_SINGLE
,
132 module_param(perdev_minors
, int, 0444);
133 MODULE_PARM_DESC(perdev_minors
, "Minors numbers to allocate per device");
135 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
136 struct mmc_blk_data
*md
);
137 static int get_card_status(struct mmc_card
*card
, u32
*status
, int retries
);
139 static inline void mmc_blk_clear_packed(struct mmc_queue_req
*mqrq
)
141 struct mmc_packed
*packed
= mqrq
->packed
;
145 mqrq
->cmd_type
= MMC_PACKED_NONE
;
146 packed
->nr_entries
= MMC_PACKED_NR_ZERO
;
147 packed
->idx_failure
= MMC_PACKED_NR_IDX
;
152 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
154 struct mmc_blk_data
*md
;
156 mutex_lock(&open_lock
);
157 md
= disk
->private_data
;
158 if (md
&& md
->usage
== 0)
162 mutex_unlock(&open_lock
);
167 static inline int mmc_get_devidx(struct gendisk
*disk
)
169 int devmaj
= MAJOR(disk_devt(disk
));
170 int devidx
= MINOR(disk_devt(disk
)) / perdev_minors
;
173 devidx
= disk
->first_minor
/ perdev_minors
;
177 static void mmc_blk_put(struct mmc_blk_data
*md
)
179 mutex_lock(&open_lock
);
181 if (md
->usage
== 0) {
182 int devidx
= mmc_get_devidx(md
->disk
);
183 blk_cleanup_queue(md
->queue
.queue
);
185 __clear_bit(devidx
, dev_use
);
190 mutex_unlock(&open_lock
);
193 static ssize_t
power_ro_lock_show(struct device
*dev
,
194 struct device_attribute
*attr
, char *buf
)
197 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
198 struct mmc_card
*card
= md
->queue
.card
;
201 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PERM_WP_EN
)
203 else if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_EN
)
206 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", locked
);
211 static ssize_t
power_ro_lock_store(struct device
*dev
,
212 struct device_attribute
*attr
, const char *buf
, size_t count
)
215 struct mmc_blk_data
*md
, *part_md
;
216 struct mmc_card
*card
;
219 if (kstrtoul(buf
, 0, &set
))
225 md
= mmc_blk_get(dev_to_disk(dev
));
226 card
= md
->queue
.card
;
230 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BOOT_WP
,
231 card
->ext_csd
.boot_ro_lock
|
232 EXT_CSD_BOOT_WP_B_PWR_WP_EN
,
233 card
->ext_csd
.part_time
);
235 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md
->disk
->disk_name
, ret
);
237 card
->ext_csd
.boot_ro_lock
|= EXT_CSD_BOOT_WP_B_PWR_WP_EN
;
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md
->disk
->disk_name
);
244 set_disk_ro(md
->disk
, 1);
246 list_for_each_entry(part_md
, &md
->part
, part
)
247 if (part_md
->area_type
== MMC_BLK_DATA_AREA_BOOT
) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md
->disk
->disk_name
);
249 set_disk_ro(part_md
->disk
, 1);
257 static ssize_t
force_ro_show(struct device
*dev
, struct device_attribute
*attr
,
261 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
263 ret
= snprintf(buf
, PAGE_SIZE
, "%d",
264 get_disk_ro(dev_to_disk(dev
)) ^
270 static ssize_t
force_ro_store(struct device
*dev
, struct device_attribute
*attr
,
271 const char *buf
, size_t count
)
275 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
276 unsigned long set
= simple_strtoul(buf
, &end
, 0);
282 set_disk_ro(dev_to_disk(dev
), set
|| md
->read_only
);
289 static int mmc_blk_open(struct block_device
*bdev
, fmode_t mode
)
291 struct mmc_blk_data
*md
= mmc_blk_get(bdev
->bd_disk
);
294 mutex_lock(&block_mutex
);
297 check_disk_change(bdev
);
300 if ((mode
& FMODE_WRITE
) && md
->read_only
) {
305 mutex_unlock(&block_mutex
);
310 static void mmc_blk_release(struct gendisk
*disk
, fmode_t mode
)
312 struct mmc_blk_data
*md
= disk
->private_data
;
314 mutex_lock(&block_mutex
);
316 mutex_unlock(&block_mutex
);
320 mmc_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
322 geo
->cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
328 struct mmc_blk_ioc_data
{
329 struct mmc_ioc_cmd ic
;
334 static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
335 struct mmc_ioc_cmd __user
*user
)
337 struct mmc_blk_ioc_data
*idata
;
340 idata
= kzalloc(sizeof(*idata
), GFP_KERNEL
);
346 if (copy_from_user(&idata
->ic
, user
, sizeof(idata
->ic
))) {
351 idata
->buf_bytes
= (u64
) idata
->ic
.blksz
* idata
->ic
.blocks
;
352 if (idata
->buf_bytes
> MMC_IOC_MAX_BYTES
) {
357 if (!idata
->buf_bytes
)
360 idata
->buf
= kzalloc(idata
->buf_bytes
, GFP_KERNEL
);
366 if (copy_from_user(idata
->buf
, (void __user
*)(unsigned long)
367 idata
->ic
.data_ptr
, idata
->buf_bytes
)) {
382 static int ioctl_rpmb_card_status_poll(struct mmc_card
*card
, u32
*status
,
388 if (!status
|| !retries_max
)
392 err
= get_card_status(card
, status
, 5);
396 if (!R1_STATUS(*status
) &&
397 (R1_CURRENT_STATE(*status
) != R1_STATE_PRG
))
398 break; /* RPMB programming operation complete */
401 * Rechedule to give the MMC device a chance to continue
402 * processing the previous command without being polled too
405 usleep_range(1000, 5000);
406 } while (++retry_count
< retries_max
);
408 if (retry_count
== retries_max
)
414 static int ioctl_do_sanitize(struct mmc_card
*card
)
418 if (!(mmc_can_sanitize(card
) &&
419 (card
->host
->caps2
& MMC_CAP2_SANITIZE
))) {
420 pr_warn("%s: %s - SANITIZE is not supported\n",
421 mmc_hostname(card
->host
), __func__
);
426 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
427 mmc_hostname(card
->host
), __func__
);
429 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
430 EXT_CSD_SANITIZE_START
, 1,
431 MMC_SANITIZE_REQ_TIMEOUT
);
434 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
435 mmc_hostname(card
->host
), __func__
, err
);
437 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card
->host
),
443 static int mmc_blk_ioctl_cmd(struct block_device
*bdev
,
444 struct mmc_ioc_cmd __user
*ic_ptr
)
446 struct mmc_blk_ioc_data
*idata
;
447 struct mmc_blk_data
*md
;
448 struct mmc_card
*card
;
449 struct mmc_command cmd
= {0};
450 struct mmc_data data
= {0};
451 struct mmc_request mrq
= {NULL
};
452 struct scatterlist sg
;
458 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
459 * whole block device, not on a partition. This prevents overspray
460 * between sibling partitions.
462 if ((!capable(CAP_SYS_RAWIO
)) || (bdev
!= bdev
->bd_contains
))
465 idata
= mmc_blk_ioctl_copy_from_user(ic_ptr
);
467 return PTR_ERR(idata
);
469 md
= mmc_blk_get(bdev
->bd_disk
);
475 if (md
->area_type
& MMC_BLK_DATA_AREA_RPMB
)
478 card
= md
->queue
.card
;
484 cmd
.opcode
= idata
->ic
.opcode
;
485 cmd
.arg
= idata
->ic
.arg
;
486 cmd
.flags
= idata
->ic
.flags
;
488 if (idata
->buf_bytes
) {
491 data
.blksz
= idata
->ic
.blksz
;
492 data
.blocks
= idata
->ic
.blocks
;
494 sg_init_one(data
.sg
, idata
->buf
, idata
->buf_bytes
);
496 if (idata
->ic
.write_flag
)
497 data
.flags
= MMC_DATA_WRITE
;
499 data
.flags
= MMC_DATA_READ
;
501 /* data.flags must already be set before doing this. */
502 mmc_set_data_timeout(&data
, card
);
504 /* Allow overriding the timeout_ns for empirical tuning. */
505 if (idata
->ic
.data_timeout_ns
)
506 data
.timeout_ns
= idata
->ic
.data_timeout_ns
;
508 if ((cmd
.flags
& MMC_RSP_R1B
) == MMC_RSP_R1B
) {
510 * Pretend this is a data transfer and rely on the
511 * host driver to compute timeout. When all host
512 * drivers support cmd.cmd_timeout for R1B, this
516 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
518 data
.timeout_ns
= idata
->ic
.cmd_timeout_ms
* 1000000;
528 err
= mmc_blk_part_switch(card
, md
);
532 if (idata
->ic
.is_acmd
) {
533 err
= mmc_app_cmd(card
->host
, card
);
539 err
= mmc_set_blockcount(card
, data
.blocks
,
540 idata
->ic
.write_flag
& (1 << 31));
545 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd
.arg
) == EXT_CSD_SANITIZE_START
) &&
546 (cmd
.opcode
== MMC_SWITCH
)) {
547 err
= ioctl_do_sanitize(card
);
550 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
556 mmc_wait_for_req(card
->host
, &mrq
);
559 dev_err(mmc_dev(card
->host
), "%s: cmd error %d\n",
560 __func__
, cmd
.error
);
565 dev_err(mmc_dev(card
->host
), "%s: data error %d\n",
566 __func__
, data
.error
);
572 * According to the SD specs, some commands require a delay after
573 * issuing the command.
575 if (idata
->ic
.postsleep_min_us
)
576 usleep_range(idata
->ic
.postsleep_min_us
, idata
->ic
.postsleep_max_us
);
578 if (copy_to_user(&(ic_ptr
->response
), cmd
.resp
, sizeof(cmd
.resp
))) {
583 if (!idata
->ic
.write_flag
) {
584 if (copy_to_user((void __user
*)(unsigned long) idata
->ic
.data_ptr
,
585 idata
->buf
, idata
->buf_bytes
)) {
593 * Ensure RPMB command has completed by polling CMD13
596 err
= ioctl_rpmb_card_status_poll(card
, &status
, 5);
598 dev_err(mmc_dev(card
->host
),
599 "%s: Card Status=0x%08X, error %d\n",
600 __func__
, status
, err
);
614 static int mmc_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
615 unsigned int cmd
, unsigned long arg
)
618 if (cmd
== MMC_IOC_CMD
)
619 ret
= mmc_blk_ioctl_cmd(bdev
, (struct mmc_ioc_cmd __user
*)arg
);
624 static int mmc_blk_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
625 unsigned int cmd
, unsigned long arg
)
627 return mmc_blk_ioctl(bdev
, mode
, cmd
, (unsigned long) compat_ptr(arg
));
631 static const struct block_device_operations mmc_bdops
= {
632 .open
= mmc_blk_open
,
633 .release
= mmc_blk_release
,
634 .getgeo
= mmc_blk_getgeo
,
635 .owner
= THIS_MODULE
,
636 .ioctl
= mmc_blk_ioctl
,
638 .compat_ioctl
= mmc_blk_compat_ioctl
,
642 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
643 struct mmc_blk_data
*md
)
646 struct mmc_blk_data
*main_md
= mmc_get_drvdata(card
);
648 if (main_md
->part_curr
== md
->part_type
)
651 if (mmc_card_mmc(card
)) {
652 u8 part_config
= card
->ext_csd
.part_config
;
654 part_config
&= ~EXT_CSD_PART_CONFIG_ACC_MASK
;
655 part_config
|= md
->part_type
;
657 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
658 EXT_CSD_PART_CONFIG
, part_config
,
659 card
->ext_csd
.part_time
);
663 card
->ext_csd
.part_config
= part_config
;
666 main_md
->part_curr
= md
->part_type
;
670 static u32
mmc_sd_num_wr_blocks(struct mmc_card
*card
)
676 struct mmc_request mrq
= {NULL
};
677 struct mmc_command cmd
= {0};
678 struct mmc_data data
= {0};
680 struct scatterlist sg
;
682 cmd
.opcode
= MMC_APP_CMD
;
683 cmd
.arg
= card
->rca
<< 16;
684 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
686 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
689 if (!mmc_host_is_spi(card
->host
) && !(cmd
.resp
[0] & R1_APP_CMD
))
692 memset(&cmd
, 0, sizeof(struct mmc_command
));
694 cmd
.opcode
= SD_APP_SEND_NUM_WR_BLKS
;
696 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
700 data
.flags
= MMC_DATA_READ
;
703 mmc_set_data_timeout(&data
, card
);
708 blocks
= kmalloc(4, GFP_KERNEL
);
712 sg_init_one(&sg
, blocks
, 4);
714 mmc_wait_for_req(card
->host
, &mrq
);
716 result
= ntohl(*blocks
);
719 if (cmd
.error
|| data
.error
)
725 static int send_stop(struct mmc_card
*card
, u32
*status
)
727 struct mmc_command cmd
= {0};
730 cmd
.opcode
= MMC_STOP_TRANSMISSION
;
731 cmd
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
732 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
734 *status
= cmd
.resp
[0];
738 static int get_card_status(struct mmc_card
*card
, u32
*status
, int retries
)
740 struct mmc_command cmd
= {0};
743 cmd
.opcode
= MMC_SEND_STATUS
;
744 if (!mmc_host_is_spi(card
->host
))
745 cmd
.arg
= card
->rca
<< 16;
746 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
747 err
= mmc_wait_for_cmd(card
->host
, &cmd
, retries
);
749 *status
= cmd
.resp
[0];
753 #define ERR_NOMEDIUM 3
756 #define ERR_CONTINUE 0
758 static int mmc_blk_cmd_error(struct request
*req
, const char *name
, int error
,
759 bool status_valid
, u32 status
)
763 /* response crc error, retry the r/w cmd */
764 pr_err("%s: %s sending %s command, card status %#x\n",
765 req
->rq_disk
->disk_name
, "response CRC error",
770 pr_err("%s: %s sending %s command, card status %#x\n",
771 req
->rq_disk
->disk_name
, "timed out", name
, status
);
773 /* If the status cmd initially failed, retry the r/w cmd */
778 * If it was a r/w cmd crc error, or illegal command
779 * (eg, issued in wrong state) then retry - we should
780 * have corrected the state problem above.
782 if (status
& (R1_COM_CRC_ERROR
| R1_ILLEGAL_COMMAND
))
785 /* Otherwise abort the command */
789 /* We don't understand the error code the driver gave us */
790 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
791 req
->rq_disk
->disk_name
, error
, status
);
797 * Initial r/w and stop cmd error recovery.
798 * We don't know whether the card received the r/w cmd or not, so try to
799 * restore things back to a sane state. Essentially, we do this as follows:
800 * - Obtain card status. If the first attempt to obtain card status fails,
801 * the status word will reflect the failed status cmd, not the failed
802 * r/w cmd. If we fail to obtain card status, it suggests we can no
803 * longer communicate with the card.
804 * - Check the card state. If the card received the cmd but there was a
805 * transient problem with the response, it might still be in a data transfer
806 * mode. Try to send it a stop command. If this fails, we can't recover.
807 * - If the r/w cmd failed due to a response CRC error, it was probably
808 * transient, so retry the cmd.
809 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
810 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
811 * illegal cmd, retry.
812 * Otherwise we don't understand what happened, so abort.
814 static int mmc_blk_cmd_recovery(struct mmc_card
*card
, struct request
*req
,
815 struct mmc_blk_request
*brq
, int *ecc_err
)
817 bool prev_cmd_status_valid
= true;
818 u32 status
, stop_status
= 0;
821 if (mmc_card_removed(card
))
825 * Try to get card status which indicates both the card state
826 * and why there was no response. If the first attempt fails,
827 * we can't be sure the returned status is for the r/w command.
829 for (retry
= 2; retry
>= 0; retry
--) {
830 err
= get_card_status(card
, &status
, 0);
834 prev_cmd_status_valid
= false;
835 pr_err("%s: error %d sending status command, %sing\n",
836 req
->rq_disk
->disk_name
, err
, retry
? "retry" : "abort");
839 /* We couldn't get a response from the card. Give up. */
841 /* Check if the card is removed */
842 if (mmc_detect_card_removed(card
->host
))
847 /* Flag ECC errors */
848 if ((status
& R1_CARD_ECC_FAILED
) ||
849 (brq
->stop
.resp
[0] & R1_CARD_ECC_FAILED
) ||
850 (brq
->cmd
.resp
[0] & R1_CARD_ECC_FAILED
))
854 * Check the current card state. If it is in some data transfer
855 * mode, tell it to stop (and hopefully transition back to TRAN.)
857 if (R1_CURRENT_STATE(status
) == R1_STATE_DATA
||
858 R1_CURRENT_STATE(status
) == R1_STATE_RCV
) {
859 err
= send_stop(card
, &stop_status
);
861 pr_err("%s: error %d sending stop command\n",
862 req
->rq_disk
->disk_name
, err
);
865 * If the stop cmd also timed out, the card is probably
866 * not present, so abort. Other errors are bad news too.
870 if (stop_status
& R1_CARD_ECC_FAILED
)
874 /* Check for set block count errors */
876 return mmc_blk_cmd_error(req
, "SET_BLOCK_COUNT", brq
->sbc
.error
,
877 prev_cmd_status_valid
, status
);
879 /* Check for r/w command errors */
881 return mmc_blk_cmd_error(req
, "r/w cmd", brq
->cmd
.error
,
882 prev_cmd_status_valid
, status
);
885 if (!brq
->stop
.error
)
888 /* Now for stop errors. These aren't fatal to the transfer. */
889 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
890 req
->rq_disk
->disk_name
, brq
->stop
.error
,
891 brq
->cmd
.resp
[0], status
);
894 * Subsitute in our own stop status as this will give the error
895 * state which happened during the execution of the r/w command.
898 brq
->stop
.resp
[0] = stop_status
;
904 static int mmc_blk_reset(struct mmc_blk_data
*md
, struct mmc_host
*host
,
909 if (md
->reset_done
& type
)
912 md
->reset_done
|= type
;
913 err
= mmc_hw_reset(host
);
914 /* Ensure we switch back to the correct partition */
915 if (err
!= -EOPNOTSUPP
) {
916 struct mmc_blk_data
*main_md
= mmc_get_drvdata(host
->card
);
919 main_md
->part_curr
= main_md
->part_type
;
920 part_err
= mmc_blk_part_switch(host
->card
, md
);
923 * We have failed to get back into the correct
924 * partition, so we need to abort the whole request.
932 static inline void mmc_blk_reset_success(struct mmc_blk_data
*md
, int type
)
934 md
->reset_done
&= ~type
;
937 static int mmc_blk_issue_discard_rq(struct mmc_queue
*mq
, struct request
*req
)
939 struct mmc_blk_data
*md
= mq
->data
;
940 struct mmc_card
*card
= md
->queue
.card
;
941 unsigned int from
, nr
, arg
;
942 int err
= 0, type
= MMC_BLK_DISCARD
;
944 if (!mmc_can_erase(card
)) {
949 from
= blk_rq_pos(req
);
950 nr
= blk_rq_sectors(req
);
952 if (mmc_can_discard(card
))
953 arg
= MMC_DISCARD_ARG
;
954 else if (mmc_can_trim(card
))
959 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
960 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
961 INAND_CMD38_ARG_EXT_CSD
,
962 arg
== MMC_TRIM_ARG
?
963 INAND_CMD38_ARG_TRIM
:
964 INAND_CMD38_ARG_ERASE
,
969 err
= mmc_erase(card
, from
, nr
, arg
);
971 if (err
== -EIO
&& !mmc_blk_reset(md
, card
->host
, type
))
974 mmc_blk_reset_success(md
, type
);
975 blk_end_request(req
, err
, blk_rq_bytes(req
));
980 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue
*mq
,
983 struct mmc_blk_data
*md
= mq
->data
;
984 struct mmc_card
*card
= md
->queue
.card
;
985 unsigned int from
, nr
, arg
;
986 int err
= 0, type
= MMC_BLK_SECDISCARD
;
988 if (!(mmc_can_secure_erase_trim(card
))) {
993 from
= blk_rq_pos(req
);
994 nr
= blk_rq_sectors(req
);
996 if (mmc_can_trim(card
) && !mmc_erase_group_aligned(card
, from
, nr
))
997 arg
= MMC_SECURE_TRIM1_ARG
;
999 arg
= MMC_SECURE_ERASE_ARG
;
1002 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1003 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1004 INAND_CMD38_ARG_EXT_CSD
,
1005 arg
== MMC_SECURE_TRIM1_ARG
?
1006 INAND_CMD38_ARG_SECTRIM1
:
1007 INAND_CMD38_ARG_SECERASE
,
1013 err
= mmc_erase(card
, from
, nr
, arg
);
1019 if (arg
== MMC_SECURE_TRIM1_ARG
) {
1020 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1021 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1022 INAND_CMD38_ARG_EXT_CSD
,
1023 INAND_CMD38_ARG_SECTRIM2
,
1029 err
= mmc_erase(card
, from
, nr
, MMC_SECURE_TRIM2_ARG
);
1037 if (err
&& !mmc_blk_reset(md
, card
->host
, type
))
1040 mmc_blk_reset_success(md
, type
);
1042 blk_end_request(req
, err
, blk_rq_bytes(req
));
1047 static int mmc_blk_issue_flush(struct mmc_queue
*mq
, struct request
*req
)
1049 struct mmc_blk_data
*md
= mq
->data
;
1050 struct mmc_card
*card
= md
->queue
.card
;
1053 ret
= mmc_flush_cache(card
);
1057 blk_end_request_all(req
, ret
);
1063 * Reformat current write as a reliable write, supporting
1064 * both legacy and the enhanced reliable write MMC cards.
1065 * In each transfer we'll handle only as much as a single
1066 * reliable write can handle, thus finish the request in
1067 * partial completions.
1069 static inline void mmc_apply_rel_rw(struct mmc_blk_request
*brq
,
1070 struct mmc_card
*card
,
1071 struct request
*req
)
1073 if (!(card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
)) {
1074 /* Legacy mode imposes restrictions on transfers. */
1075 if (!IS_ALIGNED(brq
->cmd
.arg
, card
->ext_csd
.rel_sectors
))
1076 brq
->data
.blocks
= 1;
1078 if (brq
->data
.blocks
> card
->ext_csd
.rel_sectors
)
1079 brq
->data
.blocks
= card
->ext_csd
.rel_sectors
;
1080 else if (brq
->data
.blocks
< card
->ext_csd
.rel_sectors
)
1081 brq
->data
.blocks
= 1;
1085 #define CMD_ERRORS \
1086 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1087 R1_ADDRESS_ERROR | /* Misaligned address */ \
1088 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1089 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1090 R1_CC_ERROR | /* Card controller error */ \
1091 R1_ERROR) /* General/unknown error */
1093 static int mmc_blk_err_check(struct mmc_card
*card
,
1094 struct mmc_async_req
*areq
)
1096 struct mmc_queue_req
*mq_mrq
= container_of(areq
, struct mmc_queue_req
,
1098 struct mmc_blk_request
*brq
= &mq_mrq
->brq
;
1099 struct request
*req
= mq_mrq
->req
;
1103 * sbc.error indicates a problem with the set block count
1104 * command. No data will have been transferred.
1106 * cmd.error indicates a problem with the r/w command. No
1107 * data will have been transferred.
1109 * stop.error indicates a problem with the stop command. Data
1110 * may have been transferred, or may still be transferring.
1112 if (brq
->sbc
.error
|| brq
->cmd
.error
|| brq
->stop
.error
||
1114 switch (mmc_blk_cmd_recovery(card
, req
, brq
, &ecc_err
)) {
1116 return MMC_BLK_RETRY
;
1118 return MMC_BLK_ABORT
;
1120 return MMC_BLK_NOMEDIUM
;
1127 * Check for errors relating to the execution of the
1128 * initial command - such as address errors. No data
1129 * has been transferred.
1131 if (brq
->cmd
.resp
[0] & CMD_ERRORS
) {
1132 pr_err("%s: r/w command failed, status = %#x\n",
1133 req
->rq_disk
->disk_name
, brq
->cmd
.resp
[0]);
1134 return MMC_BLK_ABORT
;
1138 * Everything else is either success, or a data error of some
1139 * kind. If it was a write, we may have transitioned to
1140 * program mode, which we have to wait for it to complete.
1142 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
) {
1144 unsigned long timeout
;
1146 timeout
= jiffies
+ msecs_to_jiffies(MMC_BLK_TIMEOUT_MS
);
1148 int err
= get_card_status(card
, &status
, 5);
1150 pr_err("%s: error %d requesting status\n",
1151 req
->rq_disk
->disk_name
, err
);
1152 return MMC_BLK_CMD_ERR
;
1155 /* Timeout if the device never becomes ready for data
1156 * and never leaves the program state.
1158 if (time_after(jiffies
, timeout
)) {
1159 pr_err("%s: Card stuck in programming state!"\
1160 " %s %s\n", mmc_hostname(card
->host
),
1161 req
->rq_disk
->disk_name
, __func__
);
1163 return MMC_BLK_CMD_ERR
;
1166 * Some cards mishandle the status bits,
1167 * so make sure to check both the busy
1168 * indication and the card state.
1170 } while (!(status
& R1_READY_FOR_DATA
) ||
1171 (R1_CURRENT_STATE(status
) == R1_STATE_PRG
));
1174 if (brq
->data
.error
) {
1175 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1176 req
->rq_disk
->disk_name
, brq
->data
.error
,
1177 (unsigned)blk_rq_pos(req
),
1178 (unsigned)blk_rq_sectors(req
),
1179 brq
->cmd
.resp
[0], brq
->stop
.resp
[0]);
1181 if (rq_data_dir(req
) == READ
) {
1183 return MMC_BLK_ECC_ERR
;
1184 return MMC_BLK_DATA_ERR
;
1186 return MMC_BLK_CMD_ERR
;
1190 if (!brq
->data
.bytes_xfered
)
1191 return MMC_BLK_RETRY
;
1193 if (mmc_packed_cmd(mq_mrq
->cmd_type
)) {
1194 if (unlikely(brq
->data
.blocks
<< 9 != brq
->data
.bytes_xfered
))
1195 return MMC_BLK_PARTIAL
;
1197 return MMC_BLK_SUCCESS
;
1200 if (blk_rq_bytes(req
) != brq
->data
.bytes_xfered
)
1201 return MMC_BLK_PARTIAL
;
1203 return MMC_BLK_SUCCESS
;
1206 static int mmc_blk_packed_err_check(struct mmc_card
*card
,
1207 struct mmc_async_req
*areq
)
1209 struct mmc_queue_req
*mq_rq
= container_of(areq
, struct mmc_queue_req
,
1211 struct request
*req
= mq_rq
->req
;
1212 struct mmc_packed
*packed
= mq_rq
->packed
;
1213 int err
, check
, status
;
1219 check
= mmc_blk_err_check(card
, areq
);
1220 err
= get_card_status(card
, &status
, 0);
1222 pr_err("%s: error %d sending status command\n",
1223 req
->rq_disk
->disk_name
, err
);
1224 return MMC_BLK_ABORT
;
1227 if (status
& R1_EXCEPTION_EVENT
) {
1228 ext_csd
= kzalloc(512, GFP_KERNEL
);
1230 pr_err("%s: unable to allocate buffer for ext_csd\n",
1231 req
->rq_disk
->disk_name
);
1235 err
= mmc_send_ext_csd(card
, ext_csd
);
1237 pr_err("%s: error %d sending ext_csd\n",
1238 req
->rq_disk
->disk_name
, err
);
1239 check
= MMC_BLK_ABORT
;
1243 if ((ext_csd
[EXT_CSD_EXP_EVENTS_STATUS
] &
1244 EXT_CSD_PACKED_FAILURE
) &&
1245 (ext_csd
[EXT_CSD_PACKED_CMD_STATUS
] &
1246 EXT_CSD_PACKED_GENERIC_ERROR
)) {
1247 if (ext_csd
[EXT_CSD_PACKED_CMD_STATUS
] &
1248 EXT_CSD_PACKED_INDEXED_ERROR
) {
1249 packed
->idx_failure
=
1250 ext_csd
[EXT_CSD_PACKED_FAILURE_INDEX
] - 1;
1251 check
= MMC_BLK_PARTIAL
;
1253 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1254 "failure index: %d\n",
1255 req
->rq_disk
->disk_name
, packed
->nr_entries
,
1256 packed
->blocks
, packed
->idx_failure
);
1265 static void mmc_blk_rw_rq_prep(struct mmc_queue_req
*mqrq
,
1266 struct mmc_card
*card
,
1268 struct mmc_queue
*mq
)
1270 u32 readcmd
, writecmd
;
1271 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1272 struct request
*req
= mqrq
->req
;
1273 struct mmc_blk_data
*md
= mq
->data
;
1277 * Reliable writes are used to implement Forced Unit Access and
1278 * REQ_META accesses, and are supported only on MMCs.
1280 * XXX: this really needs a good explanation of why REQ_META
1281 * is treated special.
1283 bool do_rel_wr
= ((req
->cmd_flags
& REQ_FUA
) ||
1284 (req
->cmd_flags
& REQ_META
)) &&
1285 (rq_data_dir(req
) == WRITE
) &&
1286 (md
->flags
& MMC_BLK_REL_WR
);
1288 memset(brq
, 0, sizeof(struct mmc_blk_request
));
1289 brq
->mrq
.cmd
= &brq
->cmd
;
1290 brq
->mrq
.data
= &brq
->data
;
1292 brq
->cmd
.arg
= blk_rq_pos(req
);
1293 if (!mmc_card_blockaddr(card
))
1295 brq
->cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
1296 brq
->data
.blksz
= 512;
1297 brq
->stop
.opcode
= MMC_STOP_TRANSMISSION
;
1299 brq
->stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1300 brq
->data
.blocks
= blk_rq_sectors(req
);
1303 * The block layer doesn't support all sector count
1304 * restrictions, so we need to be prepared for too big
1307 if (brq
->data
.blocks
> card
->host
->max_blk_count
)
1308 brq
->data
.blocks
= card
->host
->max_blk_count
;
1310 if (brq
->data
.blocks
> 1) {
1312 * After a read error, we redo the request one sector
1313 * at a time in order to accurately determine which
1314 * sectors can be read successfully.
1317 brq
->data
.blocks
= 1;
1319 /* Some controllers can't do multiblock reads due to hw bugs */
1320 if (card
->host
->caps2
& MMC_CAP2_NO_MULTI_READ
&&
1321 rq_data_dir(req
) == READ
)
1322 brq
->data
.blocks
= 1;
1325 if (brq
->data
.blocks
> 1 || do_rel_wr
) {
1326 /* SPI multiblock writes terminate using a special
1327 * token, not a STOP_TRANSMISSION request.
1329 if (!mmc_host_is_spi(card
->host
) ||
1330 rq_data_dir(req
) == READ
)
1331 brq
->mrq
.stop
= &brq
->stop
;
1332 readcmd
= MMC_READ_MULTIPLE_BLOCK
;
1333 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
1335 brq
->mrq
.stop
= NULL
;
1336 readcmd
= MMC_READ_SINGLE_BLOCK
;
1337 writecmd
= MMC_WRITE_BLOCK
;
1339 if (rq_data_dir(req
) == READ
) {
1340 brq
->cmd
.opcode
= readcmd
;
1341 brq
->data
.flags
|= MMC_DATA_READ
;
1343 brq
->cmd
.opcode
= writecmd
;
1344 brq
->data
.flags
|= MMC_DATA_WRITE
;
1348 mmc_apply_rel_rw(brq
, card
, req
);
1351 * Data tag is used only during writing meta data to speed
1352 * up write and any subsequent read of this meta data
1354 do_data_tag
= (card
->ext_csd
.data_tag_unit_size
) &&
1355 (req
->cmd_flags
& REQ_META
) &&
1356 (rq_data_dir(req
) == WRITE
) &&
1357 ((brq
->data
.blocks
* brq
->data
.blksz
) >=
1358 card
->ext_csd
.data_tag_unit_size
);
1361 * Pre-defined multi-block transfers are preferable to
1362 * open ended-ones (and necessary for reliable writes).
1363 * However, it is not sufficient to just send CMD23,
1364 * and avoid the final CMD12, as on an error condition
1365 * CMD12 (stop) needs to be sent anyway. This, coupled
1366 * with Auto-CMD23 enhancements provided by some
1367 * hosts, means that the complexity of dealing
1368 * with this is best left to the host. If CMD23 is
1369 * supported by card and host, we'll fill sbc in and let
1370 * the host deal with handling it correctly. This means
1371 * that for hosts that don't expose MMC_CAP_CMD23, no
1372 * change of behavior will be observed.
1374 * N.B: Some MMC cards experience perf degradation.
1375 * We'll avoid using CMD23-bounded multiblock writes for
1376 * these, while retaining features like reliable writes.
1378 if ((md
->flags
& MMC_BLK_CMD23
) && mmc_op_multi(brq
->cmd
.opcode
) &&
1379 (do_rel_wr
|| !(card
->quirks
& MMC_QUIRK_BLK_NO_CMD23
) ||
1381 brq
->sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
1382 brq
->sbc
.arg
= brq
->data
.blocks
|
1383 (do_rel_wr
? (1 << 31) : 0) |
1384 (do_data_tag
? (1 << 29) : 0);
1385 brq
->sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1386 brq
->mrq
.sbc
= &brq
->sbc
;
1389 mmc_set_data_timeout(&brq
->data
, card
);
1391 brq
->data
.sg
= mqrq
->sg
;
1392 brq
->data
.sg_len
= mmc_queue_map_sg(mq
, mqrq
);
1395 * Adjust the sg list so it is the same size as the
1398 if (brq
->data
.blocks
!= blk_rq_sectors(req
)) {
1399 int i
, data_size
= brq
->data
.blocks
<< 9;
1400 struct scatterlist
*sg
;
1402 for_each_sg(brq
->data
.sg
, sg
, brq
->data
.sg_len
, i
) {
1403 data_size
-= sg
->length
;
1404 if (data_size
<= 0) {
1405 sg
->length
+= data_size
;
1410 brq
->data
.sg_len
= i
;
1413 mqrq
->mmc_active
.mrq
= &brq
->mrq
;
1414 mqrq
->mmc_active
.err_check
= mmc_blk_err_check
;
1416 mmc_queue_bounce_pre(mqrq
);
1419 static inline u8
mmc_calc_packed_hdr_segs(struct request_queue
*q
,
1420 struct mmc_card
*card
)
1422 unsigned int hdr_sz
= mmc_large_sector(card
) ? 4096 : 512;
1423 unsigned int max_seg_sz
= queue_max_segment_size(q
);
1424 unsigned int len
, nr_segs
= 0;
1427 len
= min(hdr_sz
, max_seg_sz
);
1435 static u8
mmc_blk_prep_packed_list(struct mmc_queue
*mq
, struct request
*req
)
1437 struct request_queue
*q
= mq
->queue
;
1438 struct mmc_card
*card
= mq
->card
;
1439 struct request
*cur
= req
, *next
= NULL
;
1440 struct mmc_blk_data
*md
= mq
->data
;
1441 struct mmc_queue_req
*mqrq
= mq
->mqrq_cur
;
1442 bool en_rel_wr
= card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
;
1443 unsigned int req_sectors
= 0, phys_segments
= 0;
1444 unsigned int max_blk_count
, max_phys_segs
;
1445 bool put_back
= true;
1446 u8 max_packed_rw
= 0;
1449 if (!(md
->flags
& MMC_BLK_PACKED_CMD
))
1452 if ((rq_data_dir(cur
) == WRITE
) &&
1453 mmc_host_packed_wr(card
->host
))
1454 max_packed_rw
= card
->ext_csd
.max_packed_writes
;
1456 if (max_packed_rw
== 0)
1459 if (mmc_req_rel_wr(cur
) &&
1460 (md
->flags
& MMC_BLK_REL_WR
) && !en_rel_wr
)
1463 if (mmc_large_sector(card
) &&
1464 !IS_ALIGNED(blk_rq_sectors(cur
), 8))
1467 mmc_blk_clear_packed(mqrq
);
1469 max_blk_count
= min(card
->host
->max_blk_count
,
1470 card
->host
->max_req_size
>> 9);
1471 if (unlikely(max_blk_count
> 0xffff))
1472 max_blk_count
= 0xffff;
1474 max_phys_segs
= queue_max_segments(q
);
1475 req_sectors
+= blk_rq_sectors(cur
);
1476 phys_segments
+= cur
->nr_phys_segments
;
1478 if (rq_data_dir(cur
) == WRITE
) {
1479 req_sectors
+= mmc_large_sector(card
) ? 8 : 1;
1480 phys_segments
+= mmc_calc_packed_hdr_segs(q
, card
);
1484 if (reqs
>= max_packed_rw
- 1) {
1489 spin_lock_irq(q
->queue_lock
);
1490 next
= blk_fetch_request(q
);
1491 spin_unlock_irq(q
->queue_lock
);
1497 if (mmc_large_sector(card
) &&
1498 !IS_ALIGNED(blk_rq_sectors(next
), 8))
1501 if (next
->cmd_flags
& REQ_DISCARD
||
1502 next
->cmd_flags
& REQ_FLUSH
)
1505 if (rq_data_dir(cur
) != rq_data_dir(next
))
1508 if (mmc_req_rel_wr(next
) &&
1509 (md
->flags
& MMC_BLK_REL_WR
) && !en_rel_wr
)
1512 req_sectors
+= blk_rq_sectors(next
);
1513 if (req_sectors
> max_blk_count
)
1516 phys_segments
+= next
->nr_phys_segments
;
1517 if (phys_segments
> max_phys_segs
)
1520 list_add_tail(&next
->queuelist
, &mqrq
->packed
->list
);
1526 spin_lock_irq(q
->queue_lock
);
1527 blk_requeue_request(q
, next
);
1528 spin_unlock_irq(q
->queue_lock
);
1532 list_add(&req
->queuelist
, &mqrq
->packed
->list
);
1533 mqrq
->packed
->nr_entries
= ++reqs
;
1534 mqrq
->packed
->retries
= reqs
;
1539 mqrq
->cmd_type
= MMC_PACKED_NONE
;
1543 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req
*mqrq
,
1544 struct mmc_card
*card
,
1545 struct mmc_queue
*mq
)
1547 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1548 struct request
*req
= mqrq
->req
;
1549 struct request
*prq
;
1550 struct mmc_blk_data
*md
= mq
->data
;
1551 struct mmc_packed
*packed
= mqrq
->packed
;
1552 bool do_rel_wr
, do_data_tag
;
1553 u32
*packed_cmd_hdr
;
1559 mqrq
->cmd_type
= MMC_PACKED_WRITE
;
1561 packed
->idx_failure
= MMC_PACKED_NR_IDX
;
1563 packed_cmd_hdr
= packed
->cmd_hdr
;
1564 memset(packed_cmd_hdr
, 0, sizeof(packed
->cmd_hdr
));
1565 packed_cmd_hdr
[0] = (packed
->nr_entries
<< 16) |
1566 (PACKED_CMD_WR
<< 8) | PACKED_CMD_VER
;
1567 hdr_blocks
= mmc_large_sector(card
) ? 8 : 1;
1570 * Argument for each entry of packed group
1572 list_for_each_entry(prq
, &packed
->list
, queuelist
) {
1573 do_rel_wr
= mmc_req_rel_wr(prq
) && (md
->flags
& MMC_BLK_REL_WR
);
1574 do_data_tag
= (card
->ext_csd
.data_tag_unit_size
) &&
1575 (prq
->cmd_flags
& REQ_META
) &&
1576 (rq_data_dir(prq
) == WRITE
) &&
1577 ((brq
->data
.blocks
* brq
->data
.blksz
) >=
1578 card
->ext_csd
.data_tag_unit_size
);
1579 /* Argument of CMD23 */
1580 packed_cmd_hdr
[(i
* 2)] =
1581 (do_rel_wr
? MMC_CMD23_ARG_REL_WR
: 0) |
1582 (do_data_tag
? MMC_CMD23_ARG_TAG_REQ
: 0) |
1583 blk_rq_sectors(prq
);
1584 /* Argument of CMD18 or CMD25 */
1585 packed_cmd_hdr
[((i
* 2)) + 1] =
1586 mmc_card_blockaddr(card
) ?
1587 blk_rq_pos(prq
) : blk_rq_pos(prq
) << 9;
1588 packed
->blocks
+= blk_rq_sectors(prq
);
1592 memset(brq
, 0, sizeof(struct mmc_blk_request
));
1593 brq
->mrq
.cmd
= &brq
->cmd
;
1594 brq
->mrq
.data
= &brq
->data
;
1595 brq
->mrq
.sbc
= &brq
->sbc
;
1596 brq
->mrq
.stop
= &brq
->stop
;
1598 brq
->sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
1599 brq
->sbc
.arg
= MMC_CMD23_ARG_PACKED
| (packed
->blocks
+ hdr_blocks
);
1600 brq
->sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1602 brq
->cmd
.opcode
= MMC_WRITE_MULTIPLE_BLOCK
;
1603 brq
->cmd
.arg
= blk_rq_pos(req
);
1604 if (!mmc_card_blockaddr(card
))
1606 brq
->cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
1608 brq
->data
.blksz
= 512;
1609 brq
->data
.blocks
= packed
->blocks
+ hdr_blocks
;
1610 brq
->data
.flags
|= MMC_DATA_WRITE
;
1612 brq
->stop
.opcode
= MMC_STOP_TRANSMISSION
;
1614 brq
->stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1616 mmc_set_data_timeout(&brq
->data
, card
);
1618 brq
->data
.sg
= mqrq
->sg
;
1619 brq
->data
.sg_len
= mmc_queue_map_sg(mq
, mqrq
);
1621 mqrq
->mmc_active
.mrq
= &brq
->mrq
;
1622 mqrq
->mmc_active
.err_check
= mmc_blk_packed_err_check
;
1624 mmc_queue_bounce_pre(mqrq
);
1627 static int mmc_blk_cmd_err(struct mmc_blk_data
*md
, struct mmc_card
*card
,
1628 struct mmc_blk_request
*brq
, struct request
*req
,
1631 struct mmc_queue_req
*mq_rq
;
1632 mq_rq
= container_of(brq
, struct mmc_queue_req
, brq
);
1635 * If this is an SD card and we're writing, we can first
1636 * mark the known good sectors as ok.
1638 * If the card is not SD, we can still ok written sectors
1639 * as reported by the controller (which might be less than
1640 * the real number of written sectors, but never more).
1642 if (mmc_card_sd(card
)) {
1645 blocks
= mmc_sd_num_wr_blocks(card
);
1646 if (blocks
!= (u32
)-1) {
1647 ret
= blk_end_request(req
, 0, blocks
<< 9);
1650 if (!mmc_packed_cmd(mq_rq
->cmd_type
))
1651 ret
= blk_end_request(req
, 0, brq
->data
.bytes_xfered
);
1656 static int mmc_blk_end_packed_req(struct mmc_queue_req
*mq_rq
)
1658 struct request
*prq
;
1659 struct mmc_packed
*packed
= mq_rq
->packed
;
1660 int idx
= packed
->idx_failure
, i
= 0;
1665 while (!list_empty(&packed
->list
)) {
1666 prq
= list_entry_rq(packed
->list
.next
);
1668 /* retry from error index */
1669 packed
->nr_entries
-= idx
;
1673 if (packed
->nr_entries
== MMC_PACKED_NR_SINGLE
) {
1674 list_del_init(&prq
->queuelist
);
1675 mmc_blk_clear_packed(mq_rq
);
1679 list_del_init(&prq
->queuelist
);
1680 blk_end_request(prq
, 0, blk_rq_bytes(prq
));
1684 mmc_blk_clear_packed(mq_rq
);
1688 static void mmc_blk_abort_packed_req(struct mmc_queue_req
*mq_rq
)
1690 struct request
*prq
;
1691 struct mmc_packed
*packed
= mq_rq
->packed
;
1695 while (!list_empty(&packed
->list
)) {
1696 prq
= list_entry_rq(packed
->list
.next
);
1697 list_del_init(&prq
->queuelist
);
1698 blk_end_request(prq
, -EIO
, blk_rq_bytes(prq
));
1701 mmc_blk_clear_packed(mq_rq
);
1704 static void mmc_blk_revert_packed_req(struct mmc_queue
*mq
,
1705 struct mmc_queue_req
*mq_rq
)
1707 struct request
*prq
;
1708 struct request_queue
*q
= mq
->queue
;
1709 struct mmc_packed
*packed
= mq_rq
->packed
;
1713 while (!list_empty(&packed
->list
)) {
1714 prq
= list_entry_rq(packed
->list
.prev
);
1715 if (prq
->queuelist
.prev
!= &packed
->list
) {
1716 list_del_init(&prq
->queuelist
);
1717 spin_lock_irq(q
->queue_lock
);
1718 blk_requeue_request(mq
->queue
, prq
);
1719 spin_unlock_irq(q
->queue_lock
);
1721 list_del_init(&prq
->queuelist
);
1725 mmc_blk_clear_packed(mq_rq
);
1728 static int mmc_blk_issue_rw_rq(struct mmc_queue
*mq
, struct request
*rqc
)
1730 struct mmc_blk_data
*md
= mq
->data
;
1731 struct mmc_card
*card
= md
->queue
.card
;
1732 struct mmc_blk_request
*brq
= &mq
->mqrq_cur
->brq
;
1733 int ret
= 1, disable_multi
= 0, retry
= 0, type
;
1734 enum mmc_blk_status status
;
1735 struct mmc_queue_req
*mq_rq
;
1736 struct request
*req
= rqc
;
1737 struct mmc_async_req
*areq
;
1738 const u8 packed_nr
= 2;
1741 if (!rqc
&& !mq
->mqrq_prev
->req
)
1745 reqs
= mmc_blk_prep_packed_list(mq
, rqc
);
1750 * When 4KB native sector is enabled, only 8 blocks
1751 * multiple read or write is allowed
1753 if ((brq
->data
.blocks
& 0x07) &&
1754 (card
->ext_csd
.data_sector_size
== 4096)) {
1755 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1756 req
->rq_disk
->disk_name
);
1757 mq_rq
= mq
->mqrq_cur
;
1761 if (reqs
>= packed_nr
)
1762 mmc_blk_packed_hdr_wrq_prep(mq
->mqrq_cur
,
1765 mmc_blk_rw_rq_prep(mq
->mqrq_cur
, card
, 0, mq
);
1766 areq
= &mq
->mqrq_cur
->mmc_active
;
1769 areq
= mmc_start_req(card
->host
, areq
, (int *) &status
);
1771 if (status
== MMC_BLK_NEW_REQUEST
)
1772 mq
->flags
|= MMC_QUEUE_NEW_REQUEST
;
1776 mq_rq
= container_of(areq
, struct mmc_queue_req
, mmc_active
);
1779 type
= rq_data_dir(req
) == READ
? MMC_BLK_READ
: MMC_BLK_WRITE
;
1780 mmc_queue_bounce_post(mq_rq
);
1783 case MMC_BLK_SUCCESS
:
1784 case MMC_BLK_PARTIAL
:
1786 * A block was successfully transferred.
1788 mmc_blk_reset_success(md
, type
);
1790 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
1791 ret
= mmc_blk_end_packed_req(mq_rq
);
1794 ret
= blk_end_request(req
, 0,
1795 brq
->data
.bytes_xfered
);
1799 * If the blk_end_request function returns non-zero even
1800 * though all data has been transferred and no errors
1801 * were returned by the host controller, it's a bug.
1803 if (status
== MMC_BLK_SUCCESS
&& ret
) {
1804 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1805 __func__
, blk_rq_bytes(req
),
1806 brq
->data
.bytes_xfered
);
1811 case MMC_BLK_CMD_ERR
:
1812 ret
= mmc_blk_cmd_err(md
, card
, brq
, req
, ret
);
1813 if (!mmc_blk_reset(md
, card
->host
, type
))
1821 if (!mmc_blk_reset(md
, card
->host
, type
))
1824 case MMC_BLK_DATA_ERR
: {
1827 err
= mmc_blk_reset(md
, card
->host
, type
);
1830 if (err
== -ENODEV
||
1831 mmc_packed_cmd(mq_rq
->cmd_type
))
1835 case MMC_BLK_ECC_ERR
:
1836 if (brq
->data
.blocks
> 1) {
1837 /* Redo read one sector at a time */
1838 pr_warning("%s: retrying using single block read\n",
1839 req
->rq_disk
->disk_name
);
1844 * After an error, we redo I/O one sector at a
1845 * time, so we only reach here after trying to
1846 * read a single sector.
1848 ret
= blk_end_request(req
, -EIO
,
1853 case MMC_BLK_NOMEDIUM
:
1856 pr_err("%s: Unhandled return value (%d)",
1857 req
->rq_disk
->disk_name
, status
);
1862 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
1863 if (!mq_rq
->packed
->retries
)
1865 mmc_blk_packed_hdr_wrq_prep(mq_rq
, card
, mq
);
1866 mmc_start_req(card
->host
,
1867 &mq_rq
->mmc_active
, NULL
);
1871 * In case of a incomplete request
1872 * prepare it again and resend.
1874 mmc_blk_rw_rq_prep(mq_rq
, card
,
1876 mmc_start_req(card
->host
,
1877 &mq_rq
->mmc_active
, NULL
);
1885 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
1886 mmc_blk_abort_packed_req(mq_rq
);
1888 if (mmc_card_removed(card
))
1889 req
->cmd_flags
|= REQ_QUIET
;
1891 ret
= blk_end_request(req
, -EIO
,
1892 blk_rq_cur_bytes(req
));
1897 if (mmc_card_removed(card
)) {
1898 rqc
->cmd_flags
|= REQ_QUIET
;
1899 blk_end_request_all(rqc
, -EIO
);
1902 * If current request is packed, it needs to put back.
1904 if (mmc_packed_cmd(mq
->mqrq_cur
->cmd_type
))
1905 mmc_blk_revert_packed_req(mq
, mq
->mqrq_cur
);
1907 mmc_blk_rw_rq_prep(mq
->mqrq_cur
, card
, 0, mq
);
1908 mmc_start_req(card
->host
,
1909 &mq
->mqrq_cur
->mmc_active
, NULL
);
1916 static int mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
1919 struct mmc_blk_data
*md
= mq
->data
;
1920 struct mmc_card
*card
= md
->queue
.card
;
1921 struct mmc_host
*host
= card
->host
;
1922 unsigned long flags
;
1924 if (req
&& !mq
->mqrq_prev
->req
)
1925 /* claim host only for the first request */
1928 ret
= mmc_blk_part_switch(card
, md
);
1931 blk_end_request_all(req
, -EIO
);
1937 mq
->flags
&= ~MMC_QUEUE_NEW_REQUEST
;
1938 if (req
&& req
->cmd_flags
& REQ_DISCARD
) {
1939 /* complete ongoing async transfer before issuing discard */
1940 if (card
->host
->areq
)
1941 mmc_blk_issue_rw_rq(mq
, NULL
);
1942 if (req
->cmd_flags
& REQ_SECURE
&&
1943 !(card
->quirks
& MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
))
1944 ret
= mmc_blk_issue_secdiscard_rq(mq
, req
);
1946 ret
= mmc_blk_issue_discard_rq(mq
, req
);
1947 } else if (req
&& req
->cmd_flags
& REQ_FLUSH
) {
1948 /* complete ongoing async transfer before issuing flush */
1949 if (card
->host
->areq
)
1950 mmc_blk_issue_rw_rq(mq
, NULL
);
1951 ret
= mmc_blk_issue_flush(mq
, req
);
1953 if (!req
&& host
->areq
) {
1954 spin_lock_irqsave(&host
->context_info
.lock
, flags
);
1955 host
->context_info
.is_waiting_last_req
= true;
1956 spin_unlock_irqrestore(&host
->context_info
.lock
, flags
);
1958 ret
= mmc_blk_issue_rw_rq(mq
, req
);
1962 if ((!req
&& !(mq
->flags
& MMC_QUEUE_NEW_REQUEST
)) ||
1963 (req
&& (req
->cmd_flags
& MMC_REQ_SPECIAL_MASK
)))
1965 * Release host when there are no more requests
1966 * and after special request(discard, flush) is done.
1967 * In case sepecial request, there is no reentry to
1968 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1974 static inline int mmc_blk_readonly(struct mmc_card
*card
)
1976 return mmc_card_readonly(card
) ||
1977 !(card
->csd
.cmdclass
& CCC_BLOCK_WRITE
);
1980 static struct mmc_blk_data
*mmc_blk_alloc_req(struct mmc_card
*card
,
1981 struct device
*parent
,
1984 const char *subname
,
1987 struct mmc_blk_data
*md
;
1990 devidx
= find_first_zero_bit(dev_use
, max_devices
);
1991 if (devidx
>= max_devices
)
1992 return ERR_PTR(-ENOSPC
);
1993 __set_bit(devidx
, dev_use
);
1995 md
= kzalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
2002 * !subname implies we are creating main mmc_blk_data that will be
2003 * associated with mmc_card with mmc_set_drvdata. Due to device
2004 * partitions, devidx will not coincide with a per-physical card
2005 * index anymore so we keep track of a name index.
2008 md
->name_idx
= find_first_zero_bit(name_use
, max_devices
);
2009 __set_bit(md
->name_idx
, name_use
);
2011 md
->name_idx
= ((struct mmc_blk_data
*)
2012 dev_to_disk(parent
)->private_data
)->name_idx
;
2014 md
->area_type
= area_type
;
2017 * Set the read-only status based on the supported commands
2018 * and the write protect switch.
2020 md
->read_only
= mmc_blk_readonly(card
);
2022 md
->disk
= alloc_disk(perdev_minors
);
2023 if (md
->disk
== NULL
) {
2028 spin_lock_init(&md
->lock
);
2029 INIT_LIST_HEAD(&md
->part
);
2032 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
, subname
);
2036 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
2037 md
->queue
.data
= md
;
2039 md
->disk
->major
= MMC_BLOCK_MAJOR
;
2040 md
->disk
->first_minor
= devidx
* perdev_minors
;
2041 md
->disk
->fops
= &mmc_bdops
;
2042 md
->disk
->private_data
= md
;
2043 md
->disk
->queue
= md
->queue
.queue
;
2044 md
->disk
->driverfs_dev
= parent
;
2045 set_disk_ro(md
->disk
, md
->read_only
|| default_ro
);
2046 if (area_type
& MMC_BLK_DATA_AREA_RPMB
)
2047 md
->disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
2050 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2052 * - be set for removable media with permanent block devices
2053 * - be unset for removable block devices with permanent media
2055 * Since MMC block devices clearly fall under the second
2056 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2057 * should use the block device creation/destruction hotplug
2058 * messages to tell when the card is present.
2061 snprintf(md
->disk
->disk_name
, sizeof(md
->disk
->disk_name
),
2062 "mmcblk%d%s", md
->name_idx
, subname
? subname
: "");
2064 if (mmc_card_mmc(card
))
2065 blk_queue_logical_block_size(md
->queue
.queue
,
2066 card
->ext_csd
.data_sector_size
);
2068 blk_queue_logical_block_size(md
->queue
.queue
, 512);
2070 set_capacity(md
->disk
, size
);
2072 if (mmc_host_cmd23(card
->host
)) {
2073 if (mmc_card_mmc(card
) ||
2074 (mmc_card_sd(card
) &&
2075 card
->scr
.cmds
& SD_SCR_CMD23_SUPPORT
))
2076 md
->flags
|= MMC_BLK_CMD23
;
2079 if (mmc_card_mmc(card
) &&
2080 md
->flags
& MMC_BLK_CMD23
&&
2081 ((card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
) ||
2082 card
->ext_csd
.rel_sectors
)) {
2083 md
->flags
|= MMC_BLK_REL_WR
;
2084 blk_queue_flush(md
->queue
.queue
, REQ_FLUSH
| REQ_FUA
);
2087 if (mmc_card_mmc(card
) &&
2088 (area_type
== MMC_BLK_DATA_AREA_MAIN
) &&
2089 (md
->flags
& MMC_BLK_CMD23
) &&
2090 card
->ext_csd
.packed_event_en
) {
2091 if (!mmc_packed_init(&md
->queue
, card
))
2092 md
->flags
|= MMC_BLK_PACKED_CMD
;
2102 return ERR_PTR(ret
);
2105 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
2108 struct mmc_blk_data
*md
;
2110 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
)) {
2112 * The EXT_CSD sector count is in number or 512 byte
2115 size
= card
->ext_csd
.sectors
;
2118 * The CSD capacity field is in units of read_blkbits.
2119 * set_capacity takes units of 512 bytes.
2121 size
= card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9);
2124 md
= mmc_blk_alloc_req(card
, &card
->dev
, size
, false, NULL
,
2125 MMC_BLK_DATA_AREA_MAIN
);
2129 static int mmc_blk_alloc_part(struct mmc_card
*card
,
2130 struct mmc_blk_data
*md
,
2131 unsigned int part_type
,
2134 const char *subname
,
2138 struct mmc_blk_data
*part_md
;
2140 part_md
= mmc_blk_alloc_req(card
, disk_to_dev(md
->disk
), size
, default_ro
,
2141 subname
, area_type
);
2142 if (IS_ERR(part_md
))
2143 return PTR_ERR(part_md
);
2144 part_md
->part_type
= part_type
;
2145 list_add(&part_md
->part
, &md
->part
);
2147 string_get_size((u64
)get_capacity(part_md
->disk
) << 9, STRING_UNITS_2
,
2148 cap_str
, sizeof(cap_str
));
2149 pr_info("%s: %s %s partition %u %s\n",
2150 part_md
->disk
->disk_name
, mmc_card_id(card
),
2151 mmc_card_name(card
), part_md
->part_type
, cap_str
);
2155 /* MMC Physical partitions consist of two boot partitions and
2156 * up to four general purpose partitions.
2157 * For each partition enabled in EXT_CSD a block device will be allocatedi
2158 * to provide access to the partition.
2161 static int mmc_blk_alloc_parts(struct mmc_card
*card
, struct mmc_blk_data
*md
)
2165 if (!mmc_card_mmc(card
))
2168 for (idx
= 0; idx
< card
->nr_parts
; idx
++) {
2169 if (card
->part
[idx
].size
) {
2170 ret
= mmc_blk_alloc_part(card
, md
,
2171 card
->part
[idx
].part_cfg
,
2172 card
->part
[idx
].size
>> 9,
2173 card
->part
[idx
].force_ro
,
2174 card
->part
[idx
].name
,
2175 card
->part
[idx
].area_type
);
2184 static void mmc_blk_remove_req(struct mmc_blk_data
*md
)
2186 struct mmc_card
*card
;
2190 * Flush remaining requests and free queues. It
2191 * is freeing the queue that stops new requests
2192 * from being accepted.
2194 mmc_cleanup_queue(&md
->queue
);
2195 if (md
->flags
& MMC_BLK_PACKED_CMD
)
2196 mmc_packed_clean(&md
->queue
);
2197 card
= md
->queue
.card
;
2198 if (md
->disk
->flags
& GENHD_FL_UP
) {
2199 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2200 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2201 card
->ext_csd
.boot_ro_lockable
)
2202 device_remove_file(disk_to_dev(md
->disk
),
2203 &md
->power_ro_lock
);
2205 del_gendisk(md
->disk
);
2211 static void mmc_blk_remove_parts(struct mmc_card
*card
,
2212 struct mmc_blk_data
*md
)
2214 struct list_head
*pos
, *q
;
2215 struct mmc_blk_data
*part_md
;
2217 __clear_bit(md
->name_idx
, name_use
);
2218 list_for_each_safe(pos
, q
, &md
->part
) {
2219 part_md
= list_entry(pos
, struct mmc_blk_data
, part
);
2221 mmc_blk_remove_req(part_md
);
2225 static int mmc_add_disk(struct mmc_blk_data
*md
)
2228 struct mmc_card
*card
= md
->queue
.card
;
2231 md
->force_ro
.show
= force_ro_show
;
2232 md
->force_ro
.store
= force_ro_store
;
2233 sysfs_attr_init(&md
->force_ro
.attr
);
2234 md
->force_ro
.attr
.name
= "force_ro";
2235 md
->force_ro
.attr
.mode
= S_IRUGO
| S_IWUSR
;
2236 ret
= device_create_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2240 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2241 card
->ext_csd
.boot_ro_lockable
) {
2244 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_DIS
)
2247 mode
= S_IRUGO
| S_IWUSR
;
2249 md
->power_ro_lock
.show
= power_ro_lock_show
;
2250 md
->power_ro_lock
.store
= power_ro_lock_store
;
2251 sysfs_attr_init(&md
->power_ro_lock
.attr
);
2252 md
->power_ro_lock
.attr
.mode
= mode
;
2253 md
->power_ro_lock
.attr
.name
=
2254 "ro_lock_until_next_power_on";
2255 ret
= device_create_file(disk_to_dev(md
->disk
),
2256 &md
->power_ro_lock
);
2258 goto power_ro_lock_fail
;
2263 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2265 del_gendisk(md
->disk
);
2270 #define CID_MANFID_SANDISK 0x2
2271 #define CID_MANFID_TOSHIBA 0x11
2272 #define CID_MANFID_MICRON 0x13
2273 #define CID_MANFID_SAMSUNG 0x15
2275 static const struct mmc_fixup blk_fixups
[] =
2277 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
2278 MMC_QUIRK_INAND_CMD38
),
2279 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
2280 MMC_QUIRK_INAND_CMD38
),
2281 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
2282 MMC_QUIRK_INAND_CMD38
),
2283 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
2284 MMC_QUIRK_INAND_CMD38
),
2285 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
2286 MMC_QUIRK_INAND_CMD38
),
2289 * Some MMC cards experience performance degradation with CMD23
2290 * instead of CMD12-bounded multiblock transfers. For now we'll
2291 * black list what's bad...
2292 * - Certain Toshiba cards.
2294 * N.B. This doesn't affect SD cards.
2296 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
2297 MMC_QUIRK_BLK_NO_CMD23
),
2298 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
2299 MMC_QUIRK_BLK_NO_CMD23
),
2300 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
2301 MMC_QUIRK_BLK_NO_CMD23
),
2304 * Some Micron MMC cards needs longer data read timeout than
2307 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_MICRON
, 0x200, add_quirk_mmc
,
2308 MMC_QUIRK_LONG_READ_TIME
),
2311 * On these Samsung MoviNAND parts, performing secure erase or
2312 * secure trim can result in unrecoverable corruption due to a
2315 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
2316 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
2317 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
2318 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
2319 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
2320 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
2321 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
2322 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
2323 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
2324 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
2325 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
2326 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
2327 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
2328 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
2329 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
2330 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
2335 static int mmc_blk_probe(struct mmc_card
*card
)
2337 struct mmc_blk_data
*md
, *part_md
;
2341 * Check that the card supports the command class(es) we need.
2343 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
2346 md
= mmc_blk_alloc(card
);
2350 string_get_size((u64
)get_capacity(md
->disk
) << 9, STRING_UNITS_2
,
2351 cap_str
, sizeof(cap_str
));
2352 pr_info("%s: %s %s %s %s\n",
2353 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
2354 cap_str
, md
->read_only
? "(ro)" : "");
2356 if (mmc_blk_alloc_parts(card
, md
))
2359 mmc_set_drvdata(card
, md
);
2360 mmc_fixup_device(card
, blk_fixups
);
2362 if (mmc_add_disk(md
))
2365 list_for_each_entry(part_md
, &md
->part
, part
) {
2366 if (mmc_add_disk(part_md
))
2370 pm_runtime_set_autosuspend_delay(&card
->dev
, 3000);
2371 pm_runtime_use_autosuspend(&card
->dev
);
2374 * Don't enable runtime PM for SD-combo cards here. Leave that
2375 * decision to be taken during the SDIO init sequence instead.
2377 if (card
->type
!= MMC_TYPE_SD_COMBO
) {
2378 pm_runtime_set_active(&card
->dev
);
2379 pm_runtime_enable(&card
->dev
);
2385 mmc_blk_remove_parts(card
, md
);
2386 mmc_blk_remove_req(md
);
2390 static void mmc_blk_remove(struct mmc_card
*card
)
2392 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
2394 mmc_blk_remove_parts(card
, md
);
2395 pm_runtime_get_sync(&card
->dev
);
2396 mmc_claim_host(card
->host
);
2397 mmc_blk_part_switch(card
, md
);
2398 mmc_release_host(card
->host
);
2399 if (card
->type
!= MMC_TYPE_SD_COMBO
)
2400 pm_runtime_disable(&card
->dev
);
2401 pm_runtime_put_noidle(&card
->dev
);
2402 mmc_blk_remove_req(md
);
2403 mmc_set_drvdata(card
, NULL
);
2406 static int _mmc_blk_suspend(struct mmc_card
*card
)
2408 struct mmc_blk_data
*part_md
;
2409 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
2412 pm_runtime_get_sync(&card
->dev
);
2413 mmc_queue_suspend(&md
->queue
);
2414 list_for_each_entry(part_md
, &md
->part
, part
) {
2415 mmc_queue_suspend(&part_md
->queue
);
2421 static void mmc_blk_shutdown(struct mmc_card
*card
)
2423 _mmc_blk_suspend(card
);
2427 static int mmc_blk_suspend(struct mmc_card
*card
)
2429 return _mmc_blk_suspend(card
);
2432 static int mmc_blk_resume(struct mmc_card
*card
)
2434 struct mmc_blk_data
*part_md
;
2435 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
2439 * Resume involves the card going into idle state,
2440 * so current partition is always the main one.
2442 md
->part_curr
= md
->part_type
;
2443 mmc_queue_resume(&md
->queue
);
2444 list_for_each_entry(part_md
, &md
->part
, part
) {
2445 mmc_queue_resume(&part_md
->queue
);
2447 pm_runtime_put(&card
->dev
);
2452 #define mmc_blk_suspend NULL
2453 #define mmc_blk_resume NULL
2456 static struct mmc_driver mmc_driver
= {
2460 .probe
= mmc_blk_probe
,
2461 .remove
= mmc_blk_remove
,
2462 .suspend
= mmc_blk_suspend
,
2463 .resume
= mmc_blk_resume
,
2464 .shutdown
= mmc_blk_shutdown
,
2467 static int __init
mmc_blk_init(void)
2471 if (perdev_minors
!= CONFIG_MMC_BLOCK_MINORS
)
2472 pr_info("mmcblk: using %d minors per device\n", perdev_minors
);
2474 max_devices
= 256 / perdev_minors
;
2476 res
= register_blkdev(MMC_BLOCK_MAJOR
, "mmc");
2480 res
= mmc_register_driver(&mmc_driver
);
2486 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
2491 static void __exit
mmc_blk_exit(void)
2493 mmc_unregister_driver(&mmc_driver
);
2494 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
2497 module_init(mmc_blk_init
);
2498 module_exit(mmc_blk_exit
);
2500 MODULE_LICENSE("GPL");
2501 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");