From a77387ea5b200fb0242c11fd81022869fd04a0da Mon Sep 17 00:00:00 2001 From: Jonathan Bell Date: Tue, 26 Mar 2024 13:17:10 +0000 Subject: [PATCH 1/5] drivers: mmc: cqhci: clear CQHCI_CTL if halt fails The eMMC spec says that in certain circumstances the controller can't respond to a halt request - in practice, this occurs if a CMD timeout happens (card went away/crashed). Clear the halt request by writing 0 to CQHCI_CTL. Also fix a logic error testing for halt in cqhci_request. Signed-off-by: Jonathan Bell --- drivers/mmc/host/cqhci-core.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/drivers/mmc/host/cqhci-core.c b/drivers/mmc/host/cqhci-core.c index 41e94cd1410980..5da5bf8ad03840 100644 --- a/drivers/mmc/host/cqhci-core.c +++ b/drivers/mmc/host/cqhci-core.c @@ -383,9 +383,11 @@ static void cqhci_off(struct mmc_host *mmc) err = readx_poll_timeout(cqhci_read_ctl, cq_host, reg, reg & CQHCI_HALT, 0, CQHCI_OFF_TIMEOUT); - if (err < 0) + if (err < 0) { pr_err("%s: cqhci: CQE stuck on\n", mmc_hostname(mmc)); - else + /* eMMC v5.1 B.2.8 recommends writing 0 to CQHCI_CTL if stuck */ + cqhci_writel(cq_host, 0, CQHCI_CTL); + } else pr_debug("%s: cqhci: CQE off\n", mmc_hostname(mmc)); if (cq_host->ops->post_disable) @@ -612,7 +614,7 @@ static int cqhci_request(struct mmc_host *mmc, struct mmc_request *mrq) cqhci_writel(cq_host, 0, CQHCI_CTL); mmc->cqe_on = true; pr_debug("%s: cqhci: CQE on\n", mmc_hostname(mmc)); - if (cqhci_readl(cq_host, CQHCI_CTL) && CQHCI_HALT) { + if (cqhci_readl(cq_host, CQHCI_CTL) & CQHCI_HALT) { pr_err("%s: cqhci: CQE failed to exit halt state\n", mmc_hostname(mmc)); } @@ -975,8 +977,11 @@ static bool cqhci_halt(struct mmc_host *mmc, unsigned int timeout) ret = cqhci_halted(cq_host); - if (!ret) + if (!ret) { pr_warn("%s: cqhci: Failed to halt\n", mmc_hostname(mmc)); + /* eMMC v5.1 B.2.8 recommends writing 0 to CQHCI_CTL if stuck */ + cqhci_writel(cq_host, 0, CQHCI_CTL); + } return ret; } From 1ac9f791a634dfd12fe557041cd90b9fb341c751 Mon Sep 17 00:00:00 2001 From: Jonathan Bell Date: Tue, 26 Mar 2024 13:25:01 +0000 Subject: [PATCH 2/5] drivers: mmc: export SD extension register read/write functions Certain status bits in these registers may need polling outside of SD-specific code. Export in sd_ops.h Signed-off-by: Jonathan Bell --- drivers/mmc/core/sd.c | 97 ++++----------------------------------- drivers/mmc/core/sd_ops.c | 83 +++++++++++++++++++++++++++++++-- drivers/mmc/core/sd_ops.h | 4 ++ 3 files changed, 93 insertions(+), 91 deletions(-) diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c index d7587a03a4b540..91dd97b2264764 100644 --- a/drivers/mmc/core/sd.c +++ b/drivers/mmc/core/sd.c @@ -1015,83 +1015,6 @@ static bool mmc_sd_card_using_v18(struct mmc_card *card) (SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50); } -int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset, - u8 reg_data) -{ - struct mmc_host *host = card->host; - struct mmc_request mrq = {}; - struct mmc_command cmd = {}; - struct mmc_data data = {}; - struct scatterlist sg; - u8 *reg_buf; - - reg_buf = card->ext_reg_buf; - memset(reg_buf, 0, 512); - - mrq.cmd = &cmd; - mrq.data = &data; - - /* - * Arguments of CMD49: - * [31:31] MIO (0 = memory). - * [30:27] FNO (function number). - * [26:26] MW - mask write mode (0 = disable). - * [25:18] page number. - * [17:9] offset address. - * [8:0] length (0 = 1 byte). - */ - cmd.arg = fno << 27 | page << 18 | offset << 9; - - /* The first byte in the buffer is the data to be written. */ - reg_buf[0] = reg_data; - - data.flags = MMC_DATA_WRITE; - data.blksz = 512; - data.blocks = 1; - data.sg = &sg; - data.sg_len = 1; - sg_init_one(&sg, reg_buf, 512); - - cmd.opcode = SD_WRITE_EXTR_SINGLE; - cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; - - mmc_set_data_timeout(&data, card); - mmc_wait_for_req(host, &mrq); - - /* - * Note that, the SD card is allowed to signal busy on DAT0 up to 1s - * after the CMD49. Although, let's leave this to be managed by the - * caller. - */ - - if (cmd.error) - return cmd.error; - if (data.error) - return data.error; - - return 0; -} - -static int sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page, - u16 offset, u16 len, u8 *reg_buf) -{ - u32 cmd_args; - - /* - * Command arguments of CMD48: - * [31:31] MIO (0 = memory). - * [30:27] FNO (function number). - * [26:26] reserved (0). - * [25:18] page number. - * [17:9] offset address. - * [8:0] length (0 = 1 byte, 1ff = 512 bytes). - */ - cmd_args = fno << 27 | page << 18 | offset << 9 | (len -1); - - return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE, - cmd_args, reg_buf, 512); -} - static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page, u16 offset) { @@ -1101,7 +1024,7 @@ static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page, reg_buf = card->ext_reg_buf; /* Read the extension register for power management function. */ - err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf); + err = mmc_sd_read_ext_reg(card, fno, page, offset, 512, reg_buf); if (err) { pr_warn("%s: error %d reading PM func of ext reg\n", mmc_hostname(card->host), err); @@ -1139,7 +1062,7 @@ static int sd_parse_ext_reg_perf(struct mmc_card *card, u8 fno, u8 page, reg_buf = card->ext_reg_buf; - err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf); + err = mmc_sd_read_ext_reg(card, fno, page, offset, 512, reg_buf); if (err) { pr_warn("%s: error %d reading PERF func of ext reg\n", mmc_hostname(card->host), err); @@ -1234,7 +1157,7 @@ static int sd_parse_ext_reg(struct mmc_card *card, u8 *gen_info_buf, return 0; } -static int sd_read_ext_regs(struct mmc_card *card) +static int mmc_sd_read_ext_regs(struct mmc_card *card) { int err, i; u8 num_ext, *gen_info_buf; @@ -1260,7 +1183,7 @@ static int sd_read_ext_regs(struct mmc_card *card) * Read 512 bytes of general info, which is found at function number 0, * at page 0 and with no offset. */ - err = sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf); + err = mmc_sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf); if (err) { pr_err("%s: error %d reading general info of SD ext reg\n", mmc_hostname(card->host), err); @@ -1332,7 +1255,7 @@ static int sd_flush_cache(struct mmc_host *host) page = card->ext_perf.page; offset = card->ext_perf.offset + 261; - err = sd_write_ext_reg(card, fno, page, offset, BIT(0)); + err = mmc_sd_write_ext_reg(card, fno, page, offset, BIT(0)); if (err) { pr_warn("%s: error %d writing Cache Flush bit\n", mmc_hostname(host), err); @@ -1348,7 +1271,7 @@ static int sd_flush_cache(struct mmc_host *host) * Read the Flush Cache bit. The card shall reset it, to confirm that * it's has completed the flushing of the cache. */ - err = sd_read_ext_reg(card, fno, page, offset, 1, reg_buf); + err = mmc_sd_read_ext_reg(card, fno, page, offset, 1, reg_buf); if (err) { pr_warn("%s: error %d reading Cache Flush bit\n", mmc_hostname(host), err); @@ -1371,7 +1294,7 @@ static int sd_enable_cache(struct mmc_card *card) * Set Cache Enable at bit 0 in the performance enhancement register at * 260 bytes offset. */ - err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page, + err = mmc_sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page, card->ext_perf.offset + 260, BIT(0)); if (err) { pr_warn("%s: error %d writing Cache Enable bit\n", @@ -1541,7 +1464,7 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, cont: if (!oldcard) { /* Read/parse the extension registers. */ - err = sd_read_ext_regs(card); + err = mmc_sd_read_ext_regs(card); if (err) goto free_card; } @@ -1668,7 +1591,7 @@ static int sd_busy_poweroff_notify_cb(void *cb_data, bool *busy) * one byte offset and is one byte long. The Power Off Notification * Ready is bit 0. */ - err = sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page, + err = mmc_sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page, card->ext_power.offset + 1, 1, data->reg_buf); if (err) { pr_warn("%s: error %d reading status reg of PM func\n", @@ -1694,7 +1617,7 @@ static int sd_poweroff_notify(struct mmc_card *card) * Set the Power Off Notification bit in the power management settings * register at 2 bytes offset. */ - err = sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page, + err = mmc_sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page, card->ext_power.offset + 2, BIT(0)); if (err) { pr_warn("%s: error %d writing Power Off Notify bit\n", diff --git a/drivers/mmc/core/sd_ops.c b/drivers/mmc/core/sd_ops.c index 86976b0d647016..3c4bcf9277c62d 100644 --- a/drivers/mmc/core/sd_ops.c +++ b/drivers/mmc/core/sd_ops.c @@ -366,8 +366,83 @@ int mmc_app_sd_status(struct mmc_card *card, void *ssr) return 0; } -int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset, - u8 reg_data); + +int mmc_sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset, + u8 reg_data) +{ + struct mmc_host *host = card->host; + struct mmc_request mrq = {}; + struct mmc_command cmd = {}; + struct mmc_data data = {}; + struct scatterlist sg; + u8 *reg_buf; + + reg_buf = card->ext_reg_buf; + memset(reg_buf, 0, 512); + + mrq.cmd = &cmd; + mrq.data = &data; + + /* + * Arguments of CMD49: + * [31:31] MIO (0 = memory). + * [30:27] FNO (function number). + * [26:26] MW - mask write mode (0 = disable). + * [25:18] page number. + * [17:9] offset address. + * [8:0] length (0 = 1 byte). + */ + cmd.arg = fno << 27 | page << 18 | offset << 9; + + /* The first byte in the buffer is the data to be written. */ + reg_buf[0] = reg_data; + + data.flags = MMC_DATA_WRITE; + data.blksz = 512; + data.blocks = 1; + data.sg = &sg; + data.sg_len = 1; + sg_init_one(&sg, reg_buf, 512); + + cmd.opcode = SD_WRITE_EXTR_SINGLE; + cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC; + + mmc_set_data_timeout(&data, card); + mmc_wait_for_req(host, &mrq); + + /* + * Note that, the SD card is allowed to signal busy on DAT0 up to 1s + * after the CMD49. Although, let's leave this to be managed by the + * caller. + */ + + if (cmd.error) + return cmd.error; + if (data.error) + return data.error; + + return 0; +} + +int mmc_sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page, + u16 offset, u16 len, u8 *reg_buf) +{ + u32 cmd_args; + + /* + * Command arguments of CMD48: + * [31:31] MIO (0 = memory). + * [30:27] FNO (function number). + * [26:26] reserved (0). + * [25:18] page number. + * [17:9] offset address. + * [8:0] length (0 = 1 byte, 1ff = 512 bytes). + */ + cmd_args = fno << 27 | page << 18 | offset << 9 | (len - 1); + + return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE, + cmd_args, reg_buf, 512); +} static int mmc_sd_cmdq_switch(struct mmc_card *card, bool enable) { @@ -383,8 +458,8 @@ static int mmc_sd_cmdq_switch(struct mmc_card *card, bool enable) reg = BIT(0); /* Performance enhancement register byte 262 controls command queueing */ - err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page, - card->ext_perf.offset + 262, reg); + err = mmc_sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page, + card->ext_perf.offset + 262, reg); if (!err) card->ext_csd.cmdq_en = enable; diff --git a/drivers/mmc/core/sd_ops.h b/drivers/mmc/core/sd_ops.h index e16c7f2e8dfde6..1b8138368a81db 100644 --- a/drivers/mmc/core/sd_ops.h +++ b/drivers/mmc/core/sd_ops.h @@ -23,6 +23,10 @@ int mmc_app_sd_status(struct mmc_card *card, void *ssr); int mmc_app_cmd(struct mmc_host *host, struct mmc_card *card); int mmc_sd_cmdq_enable(struct mmc_card *card); int mmc_sd_cmdq_disable(struct mmc_card *card); +int mmc_sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset, + u8 reg_data); +int mmc_sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page, + u16 offset, u16 len, u8 *reg_buf); #endif From c7a205fa8867eb57c8e347a11f6de17fc6a8b253 Mon Sep 17 00:00:00 2001 From: Jonathan Bell Date: Tue, 26 Mar 2024 13:36:23 +0000 Subject: [PATCH 3/5] drivers: mmc: be more cautious when manipulating Command Queue enable Don't attempt to turn on CQ if the other mandatory features are not indicated as supported by the card. Also make sure that the register write actually stuck, as some cards claim support but never report back that the queue engine is enabled. Signed-off-by: Jonathan Bell --- drivers/mmc/core/sd.c | 8 ++++++-- drivers/mmc/core/sd_ops.c | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c index 91dd97b2264764..aa93377d68c130 100644 --- a/drivers/mmc/core/sd.c +++ b/drivers/mmc/core/sd.c @@ -1088,8 +1088,12 @@ static int sd_parse_ext_reg_perf(struct mmc_card *card, u8 fno, u8 page, if ((reg_buf[4] & BIT(0)) && !mmc_card_broken_sd_cache(card)) card->ext_perf.feature_support |= SD_EXT_PERF_CACHE; - /* Command queue support indicated via queue depth bits (0 to 4). */ - if (reg_buf[6] & 0x1f) { + /* + * Command queue support indicated via queue depth bits (0 to 4). + * Qualify this with the other mandatory required features. + */ + if (reg_buf[6] & 0x1f && card->ext_power.feature_support & SD_EXT_POWER_OFF_NOTIFY && + card->ext_perf.feature_support & SD_EXT_PERF_CACHE) { card->ext_perf.feature_support |= SD_EXT_PERF_CMD_QUEUE; card->ext_csd.cmdq_depth = reg_buf[6] & 0x1f; card->ext_csd.cmdq_support = true; diff --git a/drivers/mmc/core/sd_ops.c b/drivers/mmc/core/sd_ops.c index 3c4bcf9277c62d..4371112537fbb4 100644 --- a/drivers/mmc/core/sd_ops.c +++ b/drivers/mmc/core/sd_ops.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -448,6 +449,8 @@ static int mmc_sd_cmdq_switch(struct mmc_card *card, bool enable) { int err; u8 reg = 0; + u8 *reg_buf = card->ext_reg_buf; + ktime_t timeout; /* * SD offers two command queueing modes - sequential (in-order) and * voluntary (out-of-order). Apps Class A2 performance is only @@ -460,6 +463,25 @@ static int mmc_sd_cmdq_switch(struct mmc_card *card, bool enable) /* Performance enhancement register byte 262 controls command queueing */ err = mmc_sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page, card->ext_perf.offset + 262, reg); + if (err) + goto out; + + /* Poll the register - cards may have a lazy init/deinit sequence. */ + timeout = ktime_add_ms(ktime_get(), 10); + while (1) { + err = mmc_sd_read_ext_reg(card, card->ext_perf.fno, card->ext_perf.page, + card->ext_perf.offset + 262, 1, reg_buf); + if (err) + break; + if ((reg_buf[0] & BIT(0)) == reg) + break; + if (ktime_after(ktime_get(), timeout)) { + err = -EBADMSG; + break; + } + usleep_range(100, 200); + } +out: if (!err) card->ext_csd.cmdq_en = enable; From 9adca504ab28a5eea68986483a642faace956e7f Mon Sep 17 00:00:00 2001 From: Jonathan Bell Date: Tue, 26 Mar 2024 13:39:47 +0000 Subject: [PATCH 4/5] drivers: mmc: add debugfs entries for SD extension registers Also report the card's supported queue depth in the message log. Signed-off-by: Jonathan Bell --- drivers/mmc/core/mmc.c | 4 ++-- drivers/mmc/core/sd.c | 9 ++++++--- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c index 3a927452a6501b..4506836461967c 100644 --- a/drivers/mmc/core/mmc.c +++ b/drivers/mmc/core/mmc.c @@ -1910,8 +1910,8 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr, host->cqe_enabled = true; if (card->ext_csd.cmdq_en) { - pr_info("%s: Command Queue Engine enabled\n", - mmc_hostname(host)); + pr_info("%s: Command Queue Engine enabled, %u tags\n", + mmc_hostname(host), card->ext_csd.cmdq_depth); } else { host->hsq_enabled = true; pr_info("%s: Host Software Queue enabled\n", diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c index aa93377d68c130..697ab55d6190e5 100644 --- a/drivers/mmc/core/sd.c +++ b/drivers/mmc/core/sd.c @@ -714,7 +714,8 @@ MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid); MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial); MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr); MMC_DEV_ATTR(rca, "0x%04x\n", card->rca); - +MMC_DEV_ATTR(ext_perf, "%02x\n", card->ext_perf.feature_support); +MMC_DEV_ATTR(ext_power, "%02x\n", card->ext_power.feature_support); static ssize_t mmc_dsr_show(struct device *dev, struct device_attribute *attr, char *buf) @@ -776,6 +777,8 @@ static struct attribute *sd_std_attrs[] = { &dev_attr_ocr.attr, &dev_attr_rca.attr, &dev_attr_dsr.attr, + &dev_attr_ext_perf.attr, + &dev_attr_ext_power.attr, NULL, }; @@ -1508,8 +1511,8 @@ static int mmc_sd_init_card(struct mmc_host *host, u32 ocr, host->cqe_enabled = true; if (card->ext_csd.cmdq_en) { - pr_info("%s: Command Queue Engine enabled\n", - mmc_hostname(host)); + pr_info("%s: Command Queue Engine enabled, %u tags\n", + mmc_hostname(host), card->ext_csd.cmdq_depth); } else { host->hsq_enabled = true; pr_info("%s: Host Software Queue enabled\n", From 7b7c863a2b1831b7f050b36f36285b8e598628f8 Mon Sep 17 00:00:00 2001 From: Jonathan Bell Date: Tue, 26 Mar 2024 14:58:58 +0000 Subject: [PATCH 5/5] drivers: mmc: handle 1024-byte SD General Info lengths The spec allows for up to two 512-byte pages to be allocated for the Extension Register General Info block, so allocate accordingly. Signed-off-by: Jonathan Bell --- drivers/mmc/core/sd.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c index 697ab55d6190e5..45035b7314c93c 100644 --- a/drivers/mmc/core/sd.c +++ b/drivers/mmc/core/sd.c @@ -1176,7 +1176,7 @@ static int mmc_sd_read_ext_regs(struct mmc_card *card) if (!(card->scr.cmds & SD_SCR_CMD48_SUPPORT)) return 0; - gen_info_buf = kzalloc(512, GFP_KERNEL); + gen_info_buf = kzalloc(1024, GFP_KERNEL); if (!gen_info_buf) return -ENOMEM; @@ -1207,14 +1207,23 @@ static int mmc_sd_read_ext_regs(struct mmc_card *card) num_ext = gen_info_buf[4]; /* - * We only support revision 0 and limit it to 512 bytes for simplicity. + * We only support revision 0 and up to the spec-defined maximum of 1K. * No matter what, let's return zero to allow us to continue using the * card, even if we can't support the features from the SD function * extensions registers. */ - if (rev != 0 || len > 512) { - pr_warn("%s: non-supported SD ext reg layout\n", - mmc_hostname(card->host)); + if (rev != 0 || len > 1024) { + pr_warn("%s: non-supported SD ext reg layout rev %u length %u\n", + mmc_hostname(card->host), rev, len); + goto out; + } + + /* If the General Information block spills into the next page, read the rest */ + if (len > 512) + err = mmc_sd_read_ext_reg(card, 0, 1, 0, 512, &gen_info_buf[512]); + if (err) { + pr_err("%s: error %d reading page 1 of general info of SD ext reg\n", + mmc_hostname(card->host), err); goto out; }