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 2586efa6b6d6f..805475d86509e 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c @@ -277,6 +277,29 @@ static void centauri_t6050_free_boot(struct centauri_t6050 *c) c->descriptor = NULL; } +/* + * 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_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; + + /* All ones: the function is off the bus and cannot master. */ + if (!pci_read_config_word(pdev, PCI_COMMAND, &command) && command == (u16)~0) + return true; + return centauri_t6050_stop_dma(pdev); +} + static int centauri_t6050_rom_boot(struct centauri_t6050 *c) { const struct firmware *fw; @@ -1135,11 +1158,44 @@ 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)) - 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, 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, 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); + if (c->ipc || c->firmware || c->descriptor) + centauri_t6050_free_boot(c); + 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"); + return -EBUSY; + } + /* 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; @@ -1392,3 +1448,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..2c94fc277b848 --- /dev/null +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci_test.c @@ -0,0 +1,83 @@ +// 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 + +/* 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; 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_drained(struct kunit *test) +{ + 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_twice(struct kunit *test) +{ + /* + * 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. + */ + 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: 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_drained), + KUNIT_CASE(centauri_t6050_sleep_drain_timeout_twice), + 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);