From 5f71ecd325593892dc6793412d49e66b4e657e8f Mon Sep 17 00:00:00 2001 From: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> Date: Sun, 22 Nov 2026 01:10:51 +0000 Subject: [PATCH] mfd: macsmc: send the final power command after a protocol fault On the T6050 and J714s profiles, the saved-state guard refuses every SMC command once an unexpected reply sets saved_fault. Nothing clears the fault. apple_smc_enter_atomic() and apple_smc_write_atomic() used the same guard, so after a fault the power-off and restart handlers could not send MBSE = off1 or phra. The machine then stayed on, and macsmc-reboot only logged the failure without its cause. Only the power-off and restart handlers enter atomic mode, and only the final MBSE write follows. So apple_smc_enter_atomic() now still enters atomic mode when the guard refuses commands. It skips the NTAP write and warns. apple_smc_write_atomic() sends the final command and warns. It stops routing replies to a saved-state command that is still marked pending, so this command's reply is taken as this command's. Every other command still goes through the guard. Atomic mode already refuses them all. macsmc-reboot now prints the error code. It also says that the next power-off or restart handler is tried, which the notifier chain does on NOTIFY_OK. A KUnit case in the macsmc suite checks the guard paths. Without a fault, notifications are turned off before atomic mode. With saved_fault set, atomic mode is still entered and no command is sent. An ordinary write is still refused with -EPROTO. The kernel and the KUnit case were not compiled or run here (macOS host, no arm64 kernel toolchain). A power-off and a reboot after a real SMC fault, on a T6050 or J714s, are needed before this is merged. Signed-off-by: Alyx-ML <256263590+Alyx-ML@users.noreply.github.com> --- drivers/mfd/macsmc-test.c | 27 +++++++++++++++++++++++ drivers/mfd/macsmc.c | 33 ++++++++++++++++++++++++----- drivers/power/reset/macsmc-reboot.c | 28 ++++++++++++++++++------ include/linux/mfd/macsmc.h | 4 +++- 4 files changed, 79 insertions(+), 13 deletions(-) diff --git a/drivers/mfd/macsmc-test.c b/drivers/mfd/macsmc-test.c index 2dec0920f982d..a187247b79919 100644 --- a/drivers/mfd/macsmc-test.c +++ b/drivers/mfd/macsmc-test.c @@ -395,6 +395,32 @@ static void macsmc_port_reasons_test(struct kunit *test) KUNIT_EXPECT_FALSE(test, apple_smc_port_reasons_only(&ctx->smc, BIT_ULL(43))); } +static void macsmc_atomic_after_fault_test(struct kunit *test) +{ + struct macsmc_test_context *ctx = test->priv; + + ctx->result = 1; + + /* Without a fault: notifications are turned off, then atomic mode. */ + KUNIT_EXPECT_EQ(test, apple_smc_enter_atomic(&ctx->smc), 0); + KUNIT_EXPECT_TRUE(test, ctx->smc.atomic_mode); + KUNIT_ASSERT_EQ(test, ctx->seen_count, 1U); + KUNIT_EXPECT_EQ(test, ctx->seen[0], (u32)0); + + /* A protocol fault refuses commands, but power-off still gets atomic mode. */ + ctx->smc.atomic_mode = false; + ctx->seen_count = 0; + ctx->smc.saved_enabled = true; + ctx->smc.saved_fault = -EPROTO; + KUNIT_EXPECT_EQ(test, apple_smc_enter_atomic(&ctx->smc), 0); + KUNIT_EXPECT_TRUE(test, ctx->smc.atomic_mode); + KUNIT_EXPECT_EQ(test, ctx->seen_count, 0U); + /* Ordinary commands stay refused. */ + KUNIT_EXPECT_EQ(test, apple_smc_write_u32(&ctx->smc, SMC_KEY(MBSE), SMC_KEY(offw)), + -EPROTO); + KUNIT_EXPECT_EQ(test, ctx->seen_count, 0U); +} + static struct kunit_case macsmc_parser_cases[] = { KUNIT_CASE(macsmc_key_info_lock_test), KUNIT_CASE(macsmc_key_index_test), @@ -412,6 +438,7 @@ static struct kunit_case macsmc_parser_cases[] = { KUNIT_CASE(macsmc_lid_wake_test), KUNIT_CASE(macsmc_wake_reason_test), KUNIT_CASE(macsmc_port_reasons_test), + KUNIT_CASE(macsmc_atomic_after_fault_test), {} }; diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index cb32577a3bf66..9532b0eb48ff6 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -371,8 +371,21 @@ int apple_smc_enter_atomic(struct apple_smc *smc) guard(mutex)(&smc->mutex); int ret = apple_smc_saved_guard(smc); - if (ret) - return ret; + /* + * Only the power-off and restart handlers enter atomic mode, and the + * final MBSE command is all that follows. When the saved-state guard + * refuses commands (a protocol fault, say), refusing this one too + * would leave the machine running with no way to turn off or restart. + * Skip the notification write and still enter atomic mode, so that + * apple_smc_write_atomic() can send the final command. + */ + if (ret) { + dev_warn(smc->dev, + "atomic: commands are refused (%d); only the final power command is sent\n", + ret); + smc->atomic_mode = true; + return 0; + } /* * Disable notifications since this is called before shutdown and no @@ -401,9 +414,6 @@ int apple_smc_write_atomic(struct apple_smc *smc, smc_key key, const void *buf, ktime_t t0; s64 us; - ret = apple_smc_saved_guard(smc); - if (ret) - return ret; if (size > SMC_MAX_SIZE || size == 0) return -EINVAL; @@ -411,6 +421,19 @@ int apple_smc_write_atomic(struct apple_smc *smc, smc_key key, const void *buf, return -EIO; if (!smc->atomic_mode) return -EIO; + /* + * Atomic mode only carries the final power-off or restart command (see + * apple_smc_enter_atomic()), so the saved-state guard does not stop it. + * A saved-state command still marked pending will not be answered in + * a way anything waits for now: stop routing replies to it, so that + * this command's reply is taken as this command's. + */ + ret = apple_smc_saved_guard(smc); + if (ret) { + dev_warn(smc->dev, "atomic: sending key %p4ch although commands are refused (%d)\n", + &key, ret); + smp_store_release(&smc->saved_pending, false); /* pairs with the reply path */ + } /* * A poll timeout or a mismatched reply does not cancel the command or * release its SRAM. Its late reply does: take it if it has arrived. diff --git a/drivers/power/reset/macsmc-reboot.c b/drivers/power/reset/macsmc-reboot.c index a2e1422e05bd8..04a6448cbdaa1 100644 --- a/drivers/power/reset/macsmc-reboot.c +++ b/drivers/power/reset/macsmc-reboot.c @@ -117,21 +117,28 @@ static int nvmem_cell_set_u8(struct nvmem_cell *cell, u8 val) static int macsmc_prepare_atomic(struct sys_off_data *data) { struct macsmc_reboot *reboot = data->cb_data; + int ret; dev_info(reboot->dev, "Preparing SMC for atomic mode\n"); - apple_smc_enter_atomic(reboot->smc); + ret = apple_smc_enter_atomic(reboot->smc); + if (ret) + dev_err(reboot->dev, "Failed to enter atomic mode: %d\n", ret); return NOTIFY_OK; } static int macsmc_power_off(struct sys_off_data *data) { struct macsmc_reboot *reboot = data->cb_data; + int ret; dev_info(reboot->dev, "Issuing power off (off1)\n"); - if (apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(off1)) < 0) { - dev_err(reboot->dev, "Failed to issue MBSE = off1 (power_off)\n"); + ret = apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(off1)); + if (ret < 0) { + dev_err(reboot->dev, + "Failed to issue MBSE = off1 (power_off): %d; trying the next power-off handler\n", + ret); } else { mdelay(100); WARN_ONCE(1, "Unable to power off system\n"); @@ -143,11 +150,15 @@ static int macsmc_power_off(struct sys_off_data *data) static int macsmc_restart(struct sys_off_data *data) { struct macsmc_reboot *reboot = data->cb_data; + int ret; dev_info(reboot->dev, "Issuing restart (phra)\n"); - if (apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(phra)) < 0) { - dev_err(reboot->dev, "Failed to issue MBSE = phra (restart)\n"); + ret = apple_smc_write_u32_atomic(reboot->smc, SMC_KEY(MBSE), SMC_KEY(phra)); + if (ret < 0) { + dev_err(reboot->dev, + "Failed to issue MBSE = phra (restart): %d; trying the next restart handler\n", + ret); } else { mdelay(100); WARN_ONCE(1, "Unable to restart system\n"); @@ -161,6 +172,7 @@ static int macsmc_reboot_notify(struct notifier_block *this, unsigned long actio struct macsmc_reboot *reboot = container_of(this, struct macsmc_reboot, reboot_notify); u8 shutdown_flag; u32 val; + int ret; switch (action) { case SYS_RESTART: @@ -178,8 +190,10 @@ static int macsmc_reboot_notify(struct notifier_block *this, unsigned long actio dev_info(reboot->dev, "Preparing for reboot (%p4ch)\n", &val); /* On the Mac Mini, this will turn off the LED for power off */ - if (apple_smc_write_u32(reboot->smc, SMC_KEY(MBSE), val) < 0) - dev_err(reboot->dev, "Failed to issue MBSE = %p4ch (reboot_prepare)\n", &val); + ret = apple_smc_write_u32(reboot->smc, SMC_KEY(MBSE), val); + if (ret < 0) + dev_err(reboot->dev, "Failed to issue MBSE = %p4ch (reboot_prepare): %d\n", + &val, ret); /* Set the boot_stage to 0, which means we're doing a clean shutdown/reboot. */ if (reboot->nvm.boot_stage && diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index 0bfd2079d0b96..5bc872e734509 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -220,7 +220,9 @@ int apple_smc_write_noirq(struct apple_smc *smc, smc_key key, const void *buf, s * This function switches the SMC backend to atomic mode which allows the * use of apple_smc_write_atomic while disabling *all* other functions. * This is only used for shutdown/reboot which requires writing to a SMC - * key from atomic context. + * key from atomic context. Atomic mode is entered even when ordinary commands + * are refused after a protocol fault, so that the final power command can + * still be sent. * * Return: Zero on success, negative errno on error */