From e89a04fc575f50fd51bf5047d830a288e58e3ada Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 27 Nov 2026 16:42:58 +1100 Subject: [PATCH 1/4] wifi: apple: centauri: t6050: let the system sleep after a failed N1 startup On a s2idle path (no firmware sleep), centauri_t6050_suspend() returns -EBUSY unless the N1 reached CEN_BOOT_READY. That holds off a sleep while the boot worker is in the middle of the ROM, cycle or IPC stage, which is the point. But it also refuses every sleep for good once the startup has given up (CEN_BOOT_FAILED: missing firmware, say) or was stopped: the worker does not run again, nothing runs on the N1, and there is no state to keep. A MacBook Pro whose installed firmware lacks the N1's FTAB therefore could never suspend: every attempt ended with "pci_pm_suspend(): centauri_t6050_suspend returns -16". Return 0 for CEN_BOOT_FAILED and CEN_BOOT_STOPPED and keep -EBUSY for a startup in progress. The PCI core saves the config space as for any driver that does not. Signed-off-by: Ryan Murray --- .../apple/centauri/t6050/centauri_pci.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c index 2586efa6b6d6f..76f5805e704e1 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c @@ -1138,8 +1138,23 @@ static int centauri_t6050_suspend(struct device *dev) bool beta_fallback; int ret; - if (!apple_centauri_sleep_reboot(pdev)) - return c->boot_phase == CEN_BOOT_READY ? pci_save_state(pdev) : -EBUSY; + if (!apple_centauri_sleep_reboot(pdev)) { + switch (READ_ONCE(c->boot_phase)) { + case CEN_BOOT_READY: + return pci_save_state(pdev); + case CEN_BOOT_FAILED: + case CEN_BOOT_STOPPED: + /* + * The startup gave up (no firmware, say) or was stopped: nothing + * runs on the N1 and nothing will, so it must not hold off a + * system sleep. The PCI core saves the config space. + */ + return 0; + default: + /* A startup in progress finishes first. */ + return -EBUSY; + } + } mutex_lock(&cen_recovery_lock); beta_fallback = c->recovery && c->recovery->beta_fallback; From a4eb6e8a2fd486ab4a16326387e9b929fc7d533f Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 30 Nov 2026 09:54:38 +1100 Subject: [PATCH 2/4] wifi: apple: centauri: t6050: stop boot DMA before sleeping after a failed start A failed or stopped N1 startup no longer holds off system sleep, but a failure is not always DMA-free. When the ROM image response times out, the ROM boot has already enabled bus mastering and published the boot descriptor, and it returns without stopping DMA. A stopped startup sets CEN_BOOT_STOPPED before quiesce fences its DMA, and that fence can fail. Either way, PCI noirq could put the function in D3hot with transactions outstanding. Before allowing the sleep, release the boot DMA the same verified way teardown does (free_boot only frees buffers after a confirmed bus-master stop), then check that nothing is left: no IPC, firmware or descriptor buffers and bus mastering off. A function that already reads all ones is off the bus and cannot master. If the DMA cannot be confirmed stopped, refuse the sleep with -EBUSY, as before this series. A startup that failed before any DMA (missing firmware) passes the check at once, so the K114s case that motivated the change still sleeps. Signed-off-by: Ryan Murray --- .../apple/centauri/t6050/centauri_pci.c | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c index 76f5805e704e1..d28834fe87e9d 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c @@ -277,6 +277,26 @@ static void centauri_t6050_free_boot(struct centauri_t6050 *c) c->descriptor = NULL; } +/* + * True when no boot-time DMA can be in flight: every buffer the ROM boot or + * the IPC published is released (free_boot only releases them after a + * verified bus-master stop) and bus mastering is off. + */ +static bool centauri_t6050_boot_dma_idle(struct centauri_t6050 *c) +{ + u16 command; + + lockdep_assert_held(&c->boot_lock); + if (c->ipc || c->firmware || c->descriptor) + return false; + if (pci_read_config_word(c->pdev, PCI_COMMAND, &command)) + return false; + /* All ones: the function is off the bus and cannot master. */ + if (command == (u16)~0) + return true; + return !(command & PCI_COMMAND_MASTER); +} + static int centauri_t6050_rom_boot(struct centauri_t6050 *c) { const struct firmware *fw; @@ -1135,7 +1155,7 @@ static int centauri_t6050_suspend(struct device *dev) struct pci_dev *pdev = to_pci_dev(dev); struct centauri_t6050 *c = pci_get_drvdata(pdev); struct pci_dev *fn1; - bool beta_fallback; + bool beta_fallback, idle; int ret; if (!apple_centauri_sleep_reboot(pdev)) { @@ -1145,10 +1165,24 @@ static int centauri_t6050_suspend(struct device *dev) case CEN_BOOT_FAILED: case CEN_BOOT_STOPPED: /* - * The startup gave up (no firmware, say) or was stopped: nothing - * runs on the N1 and nothing will, so it must not hold off a - * system sleep. The PCI core saves the config space. + * The startup gave up (no firmware, say) or was stopped, so + * nothing will run on the N1 and it must not hold off a system + * sleep. A ROM boot that timed out may still have bus mastering + * on and its descriptor published, though, and a stopped startup + * may not have fenced its DMA. Release the boot DMA the usual + * verified way first; if that cannot be confirmed, keep the + * function out of D3 by refusing the sleep. */ + mutex_lock(&c->boot_lock); + if (!centauri_t6050_boot_dma_idle(c)) + centauri_t6050_free_boot(c); + idle = centauri_t6050_boot_dma_idle(c); + mutex_unlock(&c->boot_lock); + if (!idle) { + dev_warn(dev, "N1 boot DMA still live after a failed startup: refusing sleep\n"); + return -EBUSY; + } + /* The PCI core saves the config space. */ return 0; default: /* A startup in progress finishes first. */ From 8b304a49a508d4e23a1b553d2a8d460994354df9 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 30 Nov 2026 10:10:23 +1100 Subject: [PATCH 3/4] wifi: apple: centauri: t6050: require a verified fence before a failed-start sleep The previous change ran free_boot() and then re-checked the DMA state, but free_boot() returns void. When the boot DMA state was "no buffers left but MASTER still set", free_boot() could clear MASTER and then time out draining transactions; the second check only saw the cleared bit and let the system sleep with transactions possibly outstanding. When cleanup is needed, release the buffers as before and then run an explicit bus-master fence with centauri_t6050_stop_dma(), whose result says whether MASTER is clear and the pending transactions drained. Allow the sleep only if every buffer is gone and that fence succeeded. The decision is a small helper with KUnit coverage (no N1 hardware needed), including the drain-timeout case: ./tools/testing/kunit/kunit.py run --arch=arm64 --make_options LLVM=1 \ --kunitconfig drivers/net/wireless/apple/centauri/t6050 Signed-off-by: Ryan Murray --- drivers/net/wireless/apple/Kconfig | 11 +++++ .../apple/centauri/t6050/.kunitconfig | 16 +++++++ .../apple/centauri/t6050/centauri_pci.c | 30 +++++++++++- .../apple/centauri/t6050/centauri_pci_test.c | 48 +++++++++++++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 drivers/net/wireless/apple/centauri/t6050/.kunitconfig create mode 100644 drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c diff --git a/drivers/net/wireless/apple/Kconfig b/drivers/net/wireless/apple/Kconfig index d1dcc1211f155..65d90b162892a 100644 --- a/drivers/net/wireless/apple/Kconfig +++ b/drivers/net/wireless/apple/Kconfig @@ -46,6 +46,17 @@ config APPLE_CENTAURI_T6050 platform and protocol properties. It binds only behind a T6050 host; the J813/J873 transport remains available independently. +config APPLE_CENTAURI_T6050_KUNIT_TEST + bool "KUnit tests for the T6050 N1 transport sleep checks" if !KUNIT_ALL_TESTS + depends on APPLE_CENTAURI_T6050 && KUNIT=y + default KUNIT_ALL_TESTS + help + Build tests for the decision that lets a failed or stopped N1 + startup allow system sleep only when no boot DMA can be in flight. + They need no N1 hardware. + + If unsure, say N. + config APPLE_CENTAURI_T6050_WIFI bool "T6050 N1 Centauri cfg80211 station interface" depends on APPLE_CENTAURI_T6050 diff --git a/drivers/net/wireless/apple/centauri/t6050/.kunitconfig b/drivers/net/wireless/apple/centauri/t6050/.kunitconfig new file mode 100644 index 0000000000000..17175cb560b34 --- /dev/null +++ b/drivers/net/wireless/apple/centauri/t6050/.kunitconfig @@ -0,0 +1,16 @@ +CONFIG_KUNIT=y +CONFIG_PCI=y +CONFIG_ARCH_APPLE=y +CONFIG_OF=y +CONFIG_NET=y +CONFIG_WIRELESS=y +CONFIG_CFG80211=y +CONFIG_PCIE_APPLE=y +CONFIG_WLAN=y +CONFIG_WLAN_VENDOR_APPLE=y +CONFIG_APPLE_CENTAURI_T6050=y +CONFIG_APPLE_CENTAURI_T6050_WIFI=n +CONFIG_APPLE_CENTAURI_T6050_KUNIT_TEST=y +CONFIG_NETDEVICES=y +CONFIG_PCI_MSI=y +CONFIG_COMPILE_TEST=y diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c index d28834fe87e9d..e88cfd51eb9c2 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c @@ -297,6 +297,16 @@ static bool centauri_t6050_boot_dma_idle(struct centauri_t6050 *c) return !(command & PCI_COMMAND_MASTER); } +/* + * A failed or stopped startup may sleep only with no boot DMA possible: + * either nothing was live to begin with, or every buffer was released and + * a fresh bus-master fence (clear MASTER and drain) was verified. + */ +static bool centauri_t6050_failed_sleep_ok(bool dma_idle, bool released, bool fenced) +{ + return dma_idle || (released && fenced); +} + static int centauri_t6050_rom_boot(struct centauri_t6050 *c) { const struct firmware *fw; @@ -1174,9 +1184,21 @@ static int centauri_t6050_suspend(struct device *dev) * function out of D3 by refusing the sleep. */ mutex_lock(&c->boot_lock); - if (!centauri_t6050_boot_dma_idle(c)) - centauri_t6050_free_boot(c); idle = centauri_t6050_boot_dma_idle(c); + if (!idle) { + /* + * free_boot() returns void and keeps the buffers if + * its fence fails, but it can clear MASTER and still + * time out draining. Re-fence explicitly and only + * accept a verified stop with nothing left published. + */ + bool released, fenced; + + centauri_t6050_free_boot(c); + released = !c->ipc && !c->firmware && !c->descriptor; + fenced = released && centauri_t6050_stop_dma(c->pdev); + idle = centauri_t6050_failed_sleep_ok(false, released, fenced); + } mutex_unlock(&c->boot_lock); if (!idle) { dev_warn(dev, "N1 boot DMA still live after a failed startup: refusing sleep\n"); @@ -1441,3 +1463,7 @@ static void __exit centauri_t6050_exit(void) module_exit(centauri_t6050_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Apple N1 Centauri PCI firmware and ACIPC transport"); + +#if IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050_KUNIT_TEST) +#include "centauri_pci_test.c" +#endif diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c new file mode 100644 index 0000000000000..fb6474bd9be8e --- /dev/null +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * KUnit tests for when a failed or stopped T6050 N1 startup may let the + * system sleep. Included from centauri_pci.c. + */ +#include + +static void centauri_t6050_sleep_nothing_live(struct kunit *test) +{ + /* Missing firmware: no DMA was ever started. */ + KUNIT_EXPECT_TRUE(test, centauri_t6050_failed_sleep_ok(true, false, false)); +} + +static void centauri_t6050_sleep_released_and_fenced(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, centauri_t6050_failed_sleep_ok(false, true, true)); +} + +static void centauri_t6050_sleep_drain_timeout(struct kunit *test) +{ + /* + * Buffers released and MASTER cleared, but the transaction drain timed + * out: the fence is not verified, so the sleep must be refused. + */ + KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(false, true, false)); +} + +static void centauri_t6050_sleep_buffers_retained(struct kunit *test) +{ + /* free_boot() kept the DMA buffers (failed link transition or fence). */ + KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(false, false, false)); + KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(false, false, true)); +} + +static struct kunit_case centauri_t6050_sleep_cases[] = { + KUNIT_CASE(centauri_t6050_sleep_nothing_live), + KUNIT_CASE(centauri_t6050_sleep_released_and_fenced), + KUNIT_CASE(centauri_t6050_sleep_drain_timeout), + KUNIT_CASE(centauri_t6050_sleep_buffers_retained), + {} +}; + +static struct kunit_suite centauri_t6050_sleep_suite = { + .name = "apple-centauri-t6050-sleep", + .test_cases = centauri_t6050_sleep_cases, +}; + +kunit_test_suite(centauri_t6050_sleep_suite); From 357bf2be98ad9d63ea86e2b80f6ec131e90a3f95 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 30 Nov 2026 10:13:15 +1100 Subject: [PATCH 4/4] wifi: apple: centauri: t6050: fence on every failed-start sleep attempt After cleanup, the previous version accepted a sleep only if stop_dma() verified the drain, but it kept a fast path: a state with no buffers and MASTER clear was taken as idle without draining. A timed-out fence leaves exactly that state, since stop_dma() clears MASTER before it waits, so a second suspend attempt could skip the drain and sleep with a transaction still outstanding. Drop the fast path. A failed or stopped startup now releases any boot buffers and then runs a fresh fence on every suspend attempt; the sleep is allowed only if all buffers are gone and that fence drained. A function that reads all ones is off the bus and cannot master. A start that failed before any DMA (missing firmware) drains at once and still sleeps. The KUnit cases use a fake function with the same contract as stop_dma(), covering two consecutive timed-out attempts with the transaction still pending and a later attempt that drains. Signed-off-by: Ryan Murray --- .../apple/centauri/t6050/centauri_pci.c | 59 +++++++----------- .../apple/centauri/t6050/centauri_pci_test.c | 61 +++++++++++++++---- 2 files changed, 70 insertions(+), 50 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c index e88cfd51eb9c2..805475d86509e 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c @@ -278,33 +278,26 @@ static void centauri_t6050_free_boot(struct centauri_t6050 *c) } /* - * True when no boot-time DMA can be in flight: every buffer the ROM boot or - * the IPC published is released (free_boot only releases them after a - * verified bus-master stop) and bus mastering is off. + * A failed or stopped startup may sleep only with no boot DMA possible: every + * buffer released and a fence verified on THIS attempt. There is no fast path + * on a cleared MASTER bit: an earlier fence can clear MASTER and still time + * out draining, so each suspend attempt drains again. */ -static bool centauri_t6050_boot_dma_idle(struct centauri_t6050 *c) +static bool centauri_t6050_failed_sleep_ok(bool released, bool (*fence)(void *ctx), void *ctx) { + return released && fence(ctx); +} + +/* Fence for the failed-start sleep: clear MASTER and drain, on a live function. */ +static bool centauri_t6050_failed_sleep_fence(void *ctx) +{ + struct pci_dev *pdev = ctx; u16 command; - lockdep_assert_held(&c->boot_lock); - if (c->ipc || c->firmware || c->descriptor) - return false; - if (pci_read_config_word(c->pdev, PCI_COMMAND, &command)) - return false; /* All ones: the function is off the bus and cannot master. */ - if (command == (u16)~0) + if (!pci_read_config_word(pdev, PCI_COMMAND, &command) && command == (u16)~0) return true; - return !(command & PCI_COMMAND_MASTER); -} - -/* - * A failed or stopped startup may sleep only with no boot DMA possible: - * either nothing was live to begin with, or every buffer was released and - * a fresh bus-master fence (clear MASTER and drain) was verified. - */ -static bool centauri_t6050_failed_sleep_ok(bool dma_idle, bool released, bool fenced) -{ - return dma_idle || (released && fenced); + return centauri_t6050_stop_dma(pdev); } static int centauri_t6050_rom_boot(struct centauri_t6050 *c) @@ -1180,25 +1173,17 @@ static int centauri_t6050_suspend(struct device *dev) * sleep. A ROM boot that timed out may still have bus mastering * on and its descriptor published, though, and a stopped startup * may not have fenced its DMA. Release the boot DMA the usual - * verified way first; if that cannot be confirmed, keep the - * function out of D3 by refusing the sleep. + * verified way, then fence again on every attempt; if no fence + * is verified now, keep the function out of D3 by refusing the + * sleep (a retry drains again, it never trusts a cleared bit). */ mutex_lock(&c->boot_lock); - idle = centauri_t6050_boot_dma_idle(c); - if (!idle) { - /* - * free_boot() returns void and keeps the buffers if - * its fence fails, but it can clear MASTER and still - * time out draining. Re-fence explicitly and only - * accept a verified stop with nothing left published. - */ - bool released, fenced; - + if (c->ipc || c->firmware || c->descriptor) centauri_t6050_free_boot(c); - released = !c->ipc && !c->firmware && !c->descriptor; - fenced = released && centauri_t6050_stop_dma(c->pdev); - idle = centauri_t6050_failed_sleep_ok(false, released, fenced); - } + idle = centauri_t6050_failed_sleep_ok(!c->ipc && !c->firmware && + !c->descriptor, + centauri_t6050_failed_sleep_fence, + c->pdev); mutex_unlock(&c->boot_lock); if (!idle) { dev_warn(dev, "N1 boot DMA still live after a failed startup: refusing sleep\n"); diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c index fb6474bd9be8e..2c94fc277b848 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c @@ -5,37 +5,72 @@ */ #include +/* A function as the fence sees it: MASTER and outstanding transactions. */ +struct cen_fake_fn { + bool master; + bool pending; + unsigned int fences; +}; + +/* Same contract as stop_dma(): clear MASTER, then report whether it drained. */ +static bool cen_fake_fence(void *ctx) +{ + struct cen_fake_fn *fn = ctx; + + fn->fences++; + fn->master = false; + return !fn->pending; +} + static void centauri_t6050_sleep_nothing_live(struct kunit *test) { - /* Missing firmware: no DMA was ever started. */ - KUNIT_EXPECT_TRUE(test, centauri_t6050_failed_sleep_ok(true, false, false)); + /* Missing firmware: no DMA was ever started; the drain passes at once. */ + struct cen_fake_fn fn = {}; + + KUNIT_EXPECT_TRUE(test, centauri_t6050_failed_sleep_ok(true, cen_fake_fence, &fn)); + KUNIT_EXPECT_EQ(test, fn.fences, 1U); } -static void centauri_t6050_sleep_released_and_fenced(struct kunit *test) +static void centauri_t6050_sleep_released_and_drained(struct kunit *test) { - KUNIT_EXPECT_TRUE(test, centauri_t6050_failed_sleep_ok(false, true, true)); + struct cen_fake_fn fn = { .master = true }; + + KUNIT_EXPECT_TRUE(test, centauri_t6050_failed_sleep_ok(true, cen_fake_fence, &fn)); + KUNIT_EXPECT_FALSE(test, fn.master); } -static void centauri_t6050_sleep_drain_timeout(struct kunit *test) +static void centauri_t6050_sleep_drain_timeout_twice(struct kunit *test) { /* - * Buffers released and MASTER cleared, but the transaction drain timed - * out: the fence is not verified, so the sleep must be refused. + * Buffers released, MASTER set, a transaction stuck. The first attempt + * clears MASTER and times out. The retry sees MASTER already clear with + * the transaction still pending: it must drain again and still refuse. */ - KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(false, true, false)); + struct cen_fake_fn fn = { .master = true, .pending = true }; + + KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(true, cen_fake_fence, &fn)); + KUNIT_EXPECT_FALSE(test, fn.master); + KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(true, cen_fake_fence, &fn)); + KUNIT_EXPECT_EQ(test, fn.fences, 2U); + + /* Once the transaction completes, the next attempt verifies and sleeps. */ + fn.pending = false; + KUNIT_EXPECT_TRUE(test, centauri_t6050_failed_sleep_ok(true, cen_fake_fence, &fn)); + KUNIT_EXPECT_EQ(test, fn.fences, 3U); } static void centauri_t6050_sleep_buffers_retained(struct kunit *test) { - /* free_boot() kept the DMA buffers (failed link transition or fence). */ - KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(false, false, false)); - KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(false, false, true)); + /* free_boot() kept the DMA buffers: never sleep, whatever the fence says. */ + struct cen_fake_fn fn = {}; + + KUNIT_EXPECT_FALSE(test, centauri_t6050_failed_sleep_ok(false, cen_fake_fence, &fn)); } static struct kunit_case centauri_t6050_sleep_cases[] = { KUNIT_CASE(centauri_t6050_sleep_nothing_live), - KUNIT_CASE(centauri_t6050_sleep_released_and_fenced), - KUNIT_CASE(centauri_t6050_sleep_drain_timeout), + KUNIT_CASE(centauri_t6050_sleep_released_and_drained), + KUNIT_CASE(centauri_t6050_sleep_drain_timeout_twice), KUNIT_CASE(centauri_t6050_sleep_buffers_retained), {} };