diff --git a/drivers/net/wireless/apple/Kconfig b/drivers/net/wireless/apple/Kconfig index d9b13a9785de2..a62e14a17f706 100644 --- a/drivers/net/wireless/apple/Kconfig +++ b/drivers/net/wireless/apple/Kconfig @@ -41,4 +41,12 @@ config APPLE_CENTAURI_T6050_WIFI help Add scanning and station connections for the T6050 protocol profile. +config APPLE_CENTAURI_T6050_RECOVERY_KUNIT + bool "KUnit: Centauri T6050 recovery helpers (built-in, COMPILE_TEST)" + depends on KUNIT && COMPILE_TEST + depends on !APPLE_CENTAURI_T6050 + help + Build only centauri_recovery.c into vmlinux with its KUnit suite so + kunit.py can run centauri_t6050_recovery without the full T6050 driver. + endif diff --git a/drivers/net/wireless/apple/centauri/Makefile b/drivers/net/wireless/apple/centauri/Makefile index 235e1090294dc..be1d286fdb0e9 100644 --- a/drivers/net/wireless/apple/centauri/Makefile +++ b/drivers/net/wireless/apple/centauri/Makefile @@ -2,3 +2,4 @@ obj-$(CONFIG_APPLE_CENTAURI) += apple_centauri.o apple_centauri-y := centauri_pci.o centauri_port.o centauri_ipc.o centauri_wifi.o obj-$(CONFIG_APPLE_CENTAURI_T6050) += t6050/ +obj-$(CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT) += 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..9f673918aaa8b --- /dev/null +++ b/drivers/net/wireless/apple/centauri/t6050/.kunitconfig @@ -0,0 +1,9 @@ +CONFIG_COMPILE_TEST=y +CONFIG_KUNIT=y +CONFIG_NET=y +CONFIG_INET=y +CONFIG_NETDEVICES=y +CONFIG_WIRELESS=y +CONFIG_WLAN=y +CONFIG_WLAN_VENDOR_APPLE=y +CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT=y \ No newline at end of file diff --git a/drivers/net/wireless/apple/centauri/t6050/Makefile b/drivers/net/wireless/apple/centauri/t6050/Makefile index ec75629ea1272..239727c5c57f1 100644 --- a/drivers/net/wireless/apple/centauri/t6050/Makefile +++ b/drivers/net/wireless/apple/centauri/t6050/Makefile @@ -1,4 +1,9 @@ # SPDX-License-Identifier: GPL-2.0-only +obj-$(CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT) += centauri_recovery.o + obj-$(CONFIG_APPLE_CENTAURI_T6050) += apple_centauri_t6050.o apple_centauri_t6050-y := centauri_pci.o centauri_ipc.o apple_centauri_t6050-$(CONFIG_APPLE_CENTAURI_T6050_WIFI) += centauri_wifi.o +ifneq ($(CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT),y) +apple_centauri_t6050-$(CONFIG_APPLE_CENTAURI_T6050_WIFI) += centauri_recovery.o +endif \ No newline at end of file diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c index a656bc89b8ed8..1bb40c7e99bf1 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_pci.c @@ -23,6 +23,7 @@ #include #include "centauri_t6050.h" +#include "centauri_recovery.h" /* Registry owns one reference, registrations and queued work own additional ones. */ struct centauri_t6050_rewake { @@ -31,7 +32,7 @@ struct centauri_t6050_rewake { struct work_struct work; struct pci_dev *bridge; unsigned int slot; - bool active, rerun; + bool active, rerun, force_reset; unsigned int generation, work_generation; int error; struct pci_dev *control; @@ -158,7 +159,7 @@ void apple_centauri_sleep_unregister(struct pci_dev *fn) } EXPORT_SYMBOL_GPL(apple_centauri_sleep_unregister); -void apple_centauri_sleep_recovery_needed(struct pci_dev *fn, int error) +void centauri_t6050_request_rewake(struct pci_dev *fn, int error, bool force_reset) { struct centauri_t6050_rewake *rw; @@ -170,6 +171,7 @@ void apple_centauri_sleep_recovery_needed(struct pci_dev *fn, int error) continue; if (error && !rw->error) rw->error = error; + rw->force_reset |= force_reset; /* One owner and one work reference even while the work is running. */ if (rw->active && rw->generation != rw->work_generation) rw->rerun = true; @@ -182,6 +184,11 @@ void apple_centauri_sleep_recovery_needed(struct pci_dev *fn, int error) return; } } + +void apple_centauri_sleep_recovery_needed(struct pci_dev *fn, int error) +{ + centauri_t6050_request_rewake(fn, error, false); +} EXPORT_SYMBOL_GPL(apple_centauri_sleep_recovery_needed); static struct pci_driver centauri_t6050_driver; @@ -472,8 +479,10 @@ static void centauri_t6050_boot_worker(struct work_struct *work) break; case CEN_BOOT_IPC: ret = centauri_t6050_start_ipc(c); - if (!ret) + if (!ret) { c->boot_phase = CEN_BOOT_READY; + c->transport_recover_count = 0; + } break; default: goto out; @@ -519,6 +528,8 @@ static int centauri_t6050_probe(struct pci_dev *pdev, const struct pci_device_id c->link_ready = true; mutex_init(&c->boot_lock); INIT_DELAYED_WORK(&c->boot_work, centauri_t6050_boot_worker); + if (IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050_WIFI)) + centauri_t6050_transport_recover_init(c); c->boot_phase = CEN_BOOT_ROM; ret = device_property_read_string(&pdev->dev, "firmware-name", &c->firmware_name); if (ret) @@ -775,6 +786,8 @@ static int centauri_t6050_quiesce(struct centauri_t6050 *c) WRITE_ONCE(c->removing, true); /* Pairs with control_ready: no new consumer can attach. */ smp_store_release(&c->ready, false); + if (IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050_WIFI)) + centauri_t6050_transport_recover_cancel(c); cancel_delayed_work_sync(&c->boot_work); /* Never hold boot_lock over an HCI close or Bluetooth work cancellation. */ ret = centauri_t6050_beta_call(c->recovery, false); @@ -866,6 +879,19 @@ static int centauri_t6050_rewake_teardown(struct centauri_t6050_rewake *rw) return ret; } +static bool centauri_t6050_rewake_already_reset(struct centauri_t6050_rewake *rw) +{ + bool force_reset; + + mutex_lock(&cen_recovery_lock); + force_reset = rw->force_reset; + rw->force_reset = false; + mutex_unlock(&cen_recovery_lock); + + /* A prior wake's reset does not recover a subsequent runtime wedge. */ + return !force_reset && apple_pcie_t6050_warm_reset_done(rw->bridge); +} + /* * The functions are removed while their link is still up, then the warm reset, then the link * (if it went down, until it is back), then a new enumeration, all under the rescan lock: no @@ -908,7 +934,7 @@ static void centauri_t6050_rewake_worker(struct work_struct *work) * here, on a trained link, would take the link down for good: after a suspend that kept the * link the host takes the port down and up around the reset instead. */ - if (apple_pcie_t6050_warm_reset_done(bridge)) { + if (centauri_t6050_rewake_already_reset(rw)) { pci_dbg(bridge, "N1 after the sleep: reset by the host before its port came up\n"); ret = 0; } else { @@ -987,6 +1013,7 @@ static void centauri_t6050_rewake_worker(struct work_struct *work) } rw->active = false; rw->error = 0; + rw->force_reset = false; mutex_unlock(&cen_recovery_lock); kref_put(&rw->ref, centauri_t6050_owner_free); } diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_recovery.c b/drivers/net/wireless/apple/centauri/t6050/centauri_recovery.c new file mode 100644 index 0000000000000..7e8d888cacf5a --- /dev/null +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_recovery.c @@ -0,0 +1,229 @@ +// SPDX-License-Identifier: GPL-2.0-only + +#include +#include +#include +#include +#include +#include + +#include "centauri_recovery.h" + +bool centauri_control_idle_ready(bool event_valid, u32 stage_first, + u32 stage_second, bool ring_drained) +{ + if (!event_valid) + return true; + /* Settled: the control stage reads running twice in a row and the + * command ring has drained, so no slot is in flight from a pre-event + * command. */ + return stage_first == 2 && stage_first == stage_second && ring_drained; +} +EXPORT_SYMBOL_GPL(centauri_control_idle_ready); + +int centauri_t6050_boot_phase_for_control_stage(u32 control_stage, + enum centauri_t6050_boot_phase *phase) +{ + if (!phase) + return -EINVAL; + + switch (control_stage) { + case 0: + *phase = CEN_BOOT_ROM; + return 0; + case 1: + *phase = CEN_BOOT_IPC; + return 0; + case 4: + *phase = CEN_BOOT_CYCLE; + return 0; + case 2: + case 3: + *phase = CEN_BOOT_REWAKE; + return 0; + default: + return -EINVAL; + } +} +EXPORT_SYMBOL_GPL(centauri_t6050_boot_phase_for_control_stage); + +#if IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050) + +#define CEN_TRANSPORT_RECOVER_MAX 3 + +static void centauri_t6050_transport_recover_worker(struct work_struct *work) +{ + struct centauri_t6050 *c = + container_of(work, struct centauri_t6050, transport_recover_work); + + c->transport_recover_count++; + centauri_t6050_schedule_transport_recover(c); +} + +void centauri_t6050_transport_recover_init(struct centauri_t6050 *c) +{ + INIT_WORK(&c->transport_recover_work, centauri_t6050_transport_recover_worker); + c->transport_recover_count = 0; +} + +void centauri_t6050_transport_recover_cancel(struct centauri_t6050 *c) +{ + cancel_work_sync(&c->transport_recover_work); +} + +void centauri_t6050_request_transport_recover(struct centauri_t6050 *c) +{ + if (!c || READ_ONCE(c->removing)) + return; + if (c->transport_recover_count >= CEN_TRANSPORT_RECOVER_MAX) { + dev_err(&c->pdev->dev, + "Wi-Fi transport recovery limit (%u) reached\n", + CEN_TRANSPORT_RECOVER_MAX); + return; + } + schedule_work(&c->transport_recover_work); +} +EXPORT_SYMBOL_GPL(centauri_t6050_request_transport_recover); + +void centauri_t6050_schedule_transport_recover(struct centauri_t6050 *c) +{ + enum centauri_t6050_boot_phase phase; + u32 control; + int ret; + + if (!c || READ_ONCE(c->removing)) + return; + + mutex_lock(&c->boot_lock); + if (READ_ONCE(c->removing) || c->boot_phase == CEN_BOOT_STOPPED || + c->boot_phase == CEN_BOOT_FAILED) + goto out_unlock; + + control = readl(c->bar + 0x8000); + ret = centauri_t6050_boot_phase_for_control_stage(control, &phase); + if (ret) { + dev_err(&c->pdev->dev, + "Wi-Fi transport recovery unsupported (control stage=%#x): %d\n", + control, ret); + c->boot_phase = CEN_BOOT_FAILED; + goto out_unlock; + } + if (phase == CEN_BOOT_REWAKE) { + mutex_unlock(&c->boot_lock); + centauri_t6050_request_rewake(c->pdev, -EIO, true); + return; + } + + WRITE_ONCE(c->ready, false); + centauri_t6050_free_ipc(c); + if (phase == CEN_BOOT_ROM) + c->boot_attempted = false; + c->boot_phase = phase; + mutex_unlock(&c->boot_lock); + queue_delayed_work(system_long_wq, &c->boot_work, 0); + return; + +out_unlock: + mutex_unlock(&c->boot_lock); +} +EXPORT_SYMBOL_GPL(centauri_t6050_schedule_transport_recover); + +#endif /* CONFIG_APPLE_CENTAURI_T6050 */ + +#if IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT) + +#include + +static void recover_phase_rom(struct kunit *test) +{ + enum centauri_t6050_boot_phase phase = CEN_BOOT_FAILED; + + KUNIT_EXPECT_EQ(test, centauri_t6050_boot_phase_for_control_stage(0, &phase), 0); + KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_ROM); +} + +static void recover_phase_ipc(struct kunit *test) +{ + enum centauri_t6050_boot_phase phase = CEN_BOOT_FAILED; + + KUNIT_EXPECT_EQ(test, centauri_t6050_boot_phase_for_control_stage(1, &phase), 0); + KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_IPC); +} + +static void recover_phase_cycle(struct kunit *test) +{ + enum centauri_t6050_boot_phase phase = CEN_BOOT_FAILED; + + KUNIT_EXPECT_EQ(test, centauri_t6050_boot_phase_for_control_stage(4, &phase), 0); + KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_CYCLE); +} + +static void recover_phase_rewake23(struct kunit *test) +{ + enum centauri_t6050_boot_phase phase = CEN_BOOT_READY; + + KUNIT_ASSERT_EQ(test, centauri_t6050_boot_phase_for_control_stage(2, &phase), 0); + KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_REWAKE); + phase = CEN_BOOT_READY; + KUNIT_ASSERT_EQ(test, centauri_t6050_boot_phase_for_control_stage(3, &phase), 0); + KUNIT_EXPECT_EQ(test, phase, CEN_BOOT_REWAKE); +} + +static void recover_phase_null(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, + centauri_t6050_boot_phase_for_control_stage(1, NULL), + -EINVAL); +} + +static void control_idle_no_event(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, + centauri_control_idle_ready(false, 0, 0, false)); +} + +static void control_idle_settled(struct kunit *test) +{ + KUNIT_EXPECT_TRUE(test, + centauri_control_idle_ready(true, 2, 2, true)); +} + +static void control_idle_unstable_stage(struct kunit *test) +{ + KUNIT_EXPECT_FALSE(test, + centauri_control_idle_ready(true, 2, 3, true)); +} + +static void control_idle_transitional_stage(struct kunit *test) +{ + KUNIT_EXPECT_FALSE(test, + centauri_control_idle_ready(true, 3, 3, true)); +} + +static void control_idle_slot_in_flight(struct kunit *test) +{ + KUNIT_EXPECT_FALSE(test, + centauri_control_idle_ready(true, 2, 2, false)); +} + +static struct kunit_case centauri_recovery_cases[] = { + KUNIT_CASE(recover_phase_rom), + KUNIT_CASE(recover_phase_ipc), + KUNIT_CASE(recover_phase_cycle), + KUNIT_CASE(recover_phase_rewake23), + KUNIT_CASE(recover_phase_null), + KUNIT_CASE(control_idle_no_event), + KUNIT_CASE(control_idle_settled), + KUNIT_CASE(control_idle_unstable_stage), + KUNIT_CASE(control_idle_transitional_stage), + KUNIT_CASE(control_idle_slot_in_flight), + {} +}; + +static struct kunit_suite centauri_recovery_suite = { + .name = "centauri_t6050_recovery", + .test_cases = centauri_recovery_cases, +}; +kunit_test_suite(centauri_recovery_suite); + +#endif /* CONFIG_KUNIT */ diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_recovery.h b/drivers/net/wireless/apple/centauri/t6050/centauri_recovery.h new file mode 100644 index 0000000000000..e55db30655305 --- /dev/null +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_recovery.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef AURORA_CENTAURI_RECOVERY_H +#define AURORA_CENTAURI_RECOVERY_H + +#include +#include +#include "centauri_t6050.h" + +int centauri_t6050_boot_phase_for_control_stage(u32 control_stage, + enum centauri_t6050_boot_phase *phase); + +void centauri_t6050_transport_recover_init(struct centauri_t6050 *c); +void centauri_t6050_transport_recover_cancel(struct centauri_t6050 *c); +void centauri_t6050_request_transport_recover(struct centauri_t6050 *c); +void centauri_t6050_schedule_transport_recover(struct centauri_t6050 *c); + +bool centauri_control_idle_ready(bool event_valid, u32 stage_first, + u32 stage_second, bool ring_drained); + +#endif diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_t6050.h b/drivers/net/wireless/apple/centauri/t6050/centauri_t6050.h index 3701dd5619472..f6e653034a782 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_t6050.h +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_t6050.h @@ -35,6 +35,7 @@ enum centauri_t6050_boot_phase { CEN_BOOT_READY, CEN_BOOT_FAILED, CEN_BOOT_STOPPED, + CEN_BOOT_REWAKE, /* Dispatch to the recovery owner, not boot_work. */ }; struct centauri_t6050 { @@ -48,6 +49,8 @@ struct centauri_t6050 { struct dentry *debug; struct mutex boot_lock; struct delayed_work boot_work; + struct work_struct transport_recover_work; + unsigned int transport_recover_count; enum centauri_t6050_boot_phase boot_phase; bool removing, ready; void *firmware, *descriptor; @@ -74,6 +77,7 @@ bool centauri_t6050_ipc_fenced(struct centauri_t6050_ipc *ipc); void centauri_t6050_detach_ipc(struct centauri_t6050_ipc *ipc); int centauri_t6050_alpha_resources(struct centauri_t6050 *c, struct cen_alpha_resources *resources); bool centauri_t6050_stop_dma(struct pci_dev *pdev); +void centauri_t6050_request_rewake(struct pci_dev *fn, int error, bool force_reset); #if IS_ENABLED(CONFIG_APPLE_CENTAURI_T6050_WIFI) irqreturn_t centauri_t6050_alpha_irq(int irq, void *data); diff --git a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c index f5c7ca87a6230..034390675a201 100644 --- a/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/t6050/centauri_wifi.c @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -12,6 +13,7 @@ #include #include #include "centauri_t6050.h" +#include "centauri_recovery.h" #define CR_BASE 0x100000 #define TR_BASE 0x400000 @@ -58,6 +60,8 @@ struct centauri_t6050_wifi { struct work_struct scan_work; struct work_struct connect_work; struct work_struct link_work; + ktime_t last_fw_event_at; + bool last_fw_event_valid; bool irq_ready; struct cfg80211_scan_request *scan_request; struct mutex lock; /* Serializes firmware commands and ring consumers. */ @@ -113,6 +117,33 @@ struct centauri_t6050_wifi { }; static int query_link_status(struct centauri_t6050_wifi *wifi); +static void note_fw_event(struct centauri_t6050_wifi *wifi); +static void command_fault(struct centauri_t6050_wifi *wifi, u32 id, + u32 wifi_stage, const char *reason); + +static ssize_t force_transport_fault_write(struct file *file, + const char __user *user_buf, + size_t count, loff_t *ppos) +{ + struct centauri_t6050_wifi *wifi = file->private_data; + + if (count == 0) + return 0; + mutex_lock(&wifi->lock); + if (!smp_load_acquire(&wifi->irq_ready)) { + mutex_unlock(&wifi->lock); + return -ENODEV; + } + command_fault(wifi, 0, readl(wifi->a.bar + 8), "debugfs forced"); + mutex_unlock(&wifi->lock); + return count; +} + +static const struct file_operations force_transport_fault_fops = { + .write = force_transport_fault_write, + .open = simple_open, + .llseek = noop_llseek, +}; static const u32 rx_buffer[5] = { 0, 0, 0x620000, 0x680000, 0x700000 }; static const u16 ring_size[5] = { 0, 64, 192, 96, 192 }; @@ -604,8 +635,10 @@ static void drain(struct centauri_t6050_wifi *wifi) if (ring == 4) { report_beacon(wifi, packet + 24, size - 24); } else if (command & BIT(31)) { - if (command == 0x80000030) + if (command == 0x80000030) { wifi->channels_seen++; + note_fw_event(wifi); + } if (command == 0x80000005) { wifi->scans_done++; wifi->scan_status = status; @@ -618,6 +651,7 @@ static void drain(struct centauri_t6050_wifi *wifi) command == 0x80000004) { wifi->disconnect_event = true; wifi->disconnect_status = status; + note_fw_event(wifi); } if (command == 0x80000004) wifi->disconnect_pending = false; @@ -652,15 +686,78 @@ static void drain(struct centauri_t6050_wifi *wifi) drain_data_tx(wifi); } +static void note_fw_event(struct centauri_t6050_wifi *wifi) +{ + wifi->last_fw_event_at = ktime_get(); + wifi->last_fw_event_valid = true; +} + +static s64 fw_event_gap_ms(struct centauri_t6050_wifi *wifi) +{ + if (!wifi->last_fw_event_valid) + return -1; + + return ktime_to_ms(ktime_sub(ktime_get(), wifi->last_fw_event_at)); +} + +/* Firmware events have no separate completion reply; sample the control and command ring. */ +static int centauri_wait_control_idle(struct centauri_t6050_wifi *wifi) +{ + unsigned long deadline = jiffies + msecs_to_jiffies(1500); + + for (;;) { + u32 first, stage; + + if (READ_ONCE(wifi->stopping) || READ_ONCE(wifi->control->removing)) + return -ECANCELED; + if (!wifi->last_fw_event_valid) + return 0; + first = readl(wifi->control->bar + 0x8000); + usleep_range(1000, 2000); + if (READ_ONCE(wifi->stopping) || READ_ONCE(wifi->control->removing)) + return -ECANCELED; + stage = readl(wifi->control->bar + 0x8000); + if (centauri_control_idle_ready(true, first, stage, + index_read(wifi, 1, 1) == wifi->command_head)) + return 0; + if (time_after_eq(jiffies, deadline)) + return -ETIMEDOUT; + } +} + +static void log_transport_fault(struct centauri_t6050_wifi *wifi, u32 id, + u32 wifi_stage, const char *reason) +{ + u32 control = wifi->control->ipc ? + readl(wifi->control->bar + 0x8000) : ~0U; + s64 gap_ms = fw_event_gap_ms(wifi); + + if (gap_ms >= 0) { + dev_err(&wifi->control->pdev->dev, + "Wi-Fi transport fault (%s) cmd=%#x wifi_stage=%#x control_stage=%#x gap_ms=%lld rx=%u\n", + reason, id, wifi_stage, control, gap_ms, + wifi->rx_seen[2]); + } else { + dev_err(&wifi->control->pdev->dev, + "Wi-Fi transport fault (%s) cmd=%#x wifi_stage=%#x control_stage=%#x gap_ms=unknown rx=%u\n", + reason, id, wifi_stage, control, wifi->rx_seen[2]); + } +} + /* Caller holds wifi->lock. Unknown completion keeps DMA payloads owned. */ -static void command_fault(struct centauri_t6050_wifi *wifi) +static void command_fault(struct centauri_t6050_wifi *wifi, u32 id, + u32 wifi_stage, const char *reason) { + log_transport_fault(wifi, id, wifi_stage, reason); wifi->command_failed = true; wifi->configured = false; wifi->data_failed = true; - netif_stop_queue(wifi->netdev); + if (wifi->netdev) + netif_stop_queue(wifi->netdev); if (smp_load_acquire(&wifi->irq_ready)) schedule_work(&wifi->link_work); + if (!READ_ONCE(wifi->stopping) && !READ_ONCE(wifi->control->removing)) + centauri_t6050_request_transport_recover(wifi->control); } static bool command_payload_consumed(struct centauri_t6050_wifi *wifi, u16 slot) @@ -682,11 +779,11 @@ static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, const void *payload, size_t size, void *reply, size_t *reply_size, bool *submitted) { - u16 slot = wifi->command_head, sequence = ++wifi->sequence; + u16 slot = wifi->command_head, sequence; int ret; u32 stage; u8 *packet, *descriptor; - unsigned long deadline = jiffies + msecs_to_jiffies(1500); + unsigned long deadline; if (submitted) *submitted = false; @@ -694,8 +791,16 @@ static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, return -EIO; if (size > BUFFER_SIZE - 24) return -EMSGSIZE; + ret = centauri_wait_control_idle(wifi); + if (ret) { + if (ret == -ETIMEDOUT) + command_fault(wifi, id, readl(wifi->a.bar + 8), "control idle timeout"); + return ret; + } if ((slot + 1) % ring_size[1] == index_read(wifi, 1, 1)) return -ENOSPC; + sequence = ++wifi->sequence; + deadline = jiffies + msecs_to_jiffies(1500); packet = wifi->a.window + TX_BUFFER + slot * BUFFER_SIZE; memset(packet, 0, 24 + size); put_unaligned_le16(24 + size, packet + 0x10); @@ -724,17 +829,14 @@ static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, do { if (READ_ONCE(wifi->stopping) || READ_ONCE(wifi->control->removing)) { - command_fault(wifi); + command_fault(wifi, id, readl(wifi->a.bar + 8), "cancel"); ret = -ECANCELED; goto out; } drain(wifi); stage = readl(wifi->a.bar + 8); if (stage != 2) { - dev_err(&wifi->control->pdev->dev, - "Wi-Fi command %#x lost transport: stage=%#x sequence=%u rx=%u\n", - id, stage, sequence, wifi->rx_seen[2]); - command_fault(wifi); + command_fault(wifi, id, stage, "lost transport"); ret = -EIO; goto out; } @@ -754,9 +856,7 @@ static int command_reply(struct centauri_t6050_wifi *wifi, u32 id, } usleep_range(500, 1000); } while (time_before(jiffies, deadline)); - dev_err(&wifi->control->pdev->dev, - "Wi-Fi command %#x timed out (rx=%u)\n", id, wifi->rx_seen[2]); - command_fault(wifi); + command_fault(wifi, id, readl(wifi->a.bar + 8), "timeout"); ret = -ETIMEDOUT; out: wifi->reply_buffer = NULL; @@ -887,6 +987,7 @@ static void channels_apply(struct centauri_t6050_wifi *wifi) if (!(channel->flags & IEEE80211_CHAN_DISABLED)) counted[band]++; } + note_fw_event(wifi); dev_info(&wifi->control->pdev->dev, "Wi-Fi firmware channels: 2.4 GHz %u, 5 GHz %u, 6 GHz %u (%u entries)\n", counted[0], counted[1], counted[2], list[0]); @@ -2484,6 +2585,8 @@ int centauri_t6050_wifi_register(struct centauri_t6050 *c) if (ret) goto failed; wifi->registered = true; + debugfs_create_file("force_transport_fault", 0200, wifi->wiphy->debugfsdir, + wifi, &force_transport_fault_fops); debugfs_create_blob("hw_channels", 0400, wifi->wiphy->debugfsdir, &wifi->hw_channels_blob); debugfs_create_blob("system_caps", 0400, wifi->wiphy->debugfsdir, diff --git a/tools/asahi/centauri-physical-recovery-tests.c b/tools/asahi/centauri-physical-recovery-tests.c index 4c765125fe737..8a5dd7c51a8f8 100644 --- a/tools/asahi/centauri-physical-recovery-tests.c +++ b/tools/asahi/centauri-physical-recovery-tests.c @@ -20,6 +20,8 @@ #define PCI_COMMAND_MASTER 4 #define PCI_EXP_LNKSTA 18 #define PCI_EXP_LNKSTA_DLLLA 0x2000 +#define CONFIG_APPLE_CENTAURI_T6050_WIFI 1 +#define IS_ENABLED(option) (option) #define CEN_BOOT_STOPPED 5 #define SZ_16K 16384 #define CEN_ARENA_SIZE 65536 @@ -37,7 +39,7 @@ struct pci_dev { int devfn, refs, command, reads, writes; bool visible, pending; }; -struct work_struct { int unused; }; +struct work_struct { bool pending; }; struct kref { int count; }; struct centauri_t6050_ipc { bool owned[3]; struct pci_dev *function[3]; void *buffer; @@ -51,7 +53,7 @@ struct centauri_t6050_rewake { struct work_struct work; struct kref ref; struct pci_dev *bridge, *control, *beta; unsigned int slot, generation, work_generation; - bool active, rerun, control_lease, beta_lease; + bool active, rerun, force_reset, control_lease, beta_lease; int error; struct centauri_t6050_ipc *ipc; void *firmware, *descriptor, *arena, *data; @@ -69,6 +71,7 @@ struct centauri_t6050 { size_t firmware_size; bool removing, ready, firmware_sleep, link_ready, boot_attempted; int boot_lock, boot_work, boot_phase; + struct work_struct transport_recover_work; struct centauri_t6050_irqs irqs, alpha_irqs; }; struct cen_bt { @@ -116,9 +119,11 @@ static int pcibios_err_to_errno(int x) { return x ? -EIO : 0; } static bool pci_wait_for_pending_transaction(struct pci_dev *p) { return can_config(p) && !p->pending && !force_pending; } static int work_busy(int *p) { return *p; } +static void cancel_work_sync(struct work_struct *p) +{ assert(control.removing && !control.ready && !control.boot_lock); p->pending = false; } static void cancel_delayed_work_sync(int *p) { *p = 0; boot_users = 0; } static void centauri_t6050_wifi_unregister(struct centauri_t6050 *c) -{ assert(c->removing); wifi_users = 0; } +{ assert(c->removing && !c->transport_recover_work.pending); wifi_users = 0; } static int pci_irq_vector(struct pci_dev *p, int n) { (void)p; return n; } static void free_irq(int n, void *c) { (void)n; (void)c; irq_users = 0; } static void centauri_t6050_free_irqs(struct centauri_t6050_irqs *p) @@ -230,6 +235,7 @@ static void fixture(void) owner.ops = &ops; owner.data = β control.pdev = &devs[0]; control.recovery = &owner; control.link_ready = true; control.boot_attempted = control.ready = true; + control.transport_recover_work.pending = true; control.irqs.pdev = &devs[0]; control.alpha_irqs.pdev = &devs[1]; control.irqs.count = control.alpha_irqs.count = 1; devs[0].dev.driver = ¢auri_t6050_driver.driver; devs[0].drvdata = &control; diff --git a/tools/asahi/centauri-physical-recovery-tests.py b/tools/asahi/centauri-physical-recovery-tests.py index d9c843d666c5b..34b731305e80b 100644 --- a/tools/asahi/centauri-physical-recovery-tests.py +++ b/tools/asahi/centauri-physical-recovery-tests.py @@ -20,6 +20,7 @@ ROOT = Path(__file__).resolve().parents[2] PCI = 'drivers/net/wireless/apple/centauri/t6050/centauri_pci.c' IPC = 'drivers/net/wireless/apple/centauri/t6050/centauri_ipc.c' +RECOVERY = 'drivers/net/wireless/apple/centauri/t6050/centauri_recovery.c' BT = 'drivers/bluetooth/btcentauri_t6050.c' GUARDS = { 'bound': '(c->ipc || c->firmware || c->descriptor || c->recovery->arena) &&\n\t ', @@ -73,12 +74,14 @@ def generate(sources, mutation): function(sources[BT], 'cen_retained'), function(sources[BT], 'cen_stop'), function(sources[IPC], 'centauri_t6050_ipc_fenced'), - function(sources[IPC], 'centauri_t6050_free_ipc')] + function(sources[IPC], 'centauri_t6050_free_ipc'), + function(sources[RECOVERY], 'centauri_t6050_transport_recover_cancel')] chunks += [function(source, name) for name in ( 'centauri_t6050_free_boot', 'centauri_t6050_beta_call', 'centauri_t6050_domain_fence', 'centauri_t6050_quiesce', 'centauri_t6050_remove', 'centauri_t6050_rewake_teardown', - 'centauri_t6050_link_active', 'centauri_t6050_rewake_worker')] + 'centauri_t6050_link_active', 'centauri_t6050_rewake_already_reset', + 'centauri_t6050_rewake_worker')] fixture = Path(__file__).with_suffix('.c').read_text() marker = '/* ACTUAL_FUNCTIONS */' if fixture.count(marker) != 1: @@ -88,7 +91,7 @@ def generate(sources, mutation): def run(args, out): out.mkdir(parents=True, exist_ok=True) - sources = {path: (ROOT / path).read_text() for path in (PCI, IPC, BT)} + sources = {path: (ROOT / path).read_text() for path in (PCI, IPC, BT, RECOVERY)} c = out / 'centauri-recovery.c' binary = out / 'centauri-recovery' c.write_text(generate(sources, args.mutation)) @@ -102,7 +105,7 @@ def run(args, out): (out / 'model.log').write_text(result.stdout + result.stderr) (out / 'result.json').write_text(json.dumps({ 'mutation': args.mutation, 'exit': result.returncode, - 'extracted_functions': 15, 'compiler_command': command, + 'extracted_functions': 17, 'compiler_command': command, 'sources': {path: hashlib.sha256(text.encode()).hexdigest() for path, text in sources.items()}, }, indent=2) + '\n') diff --git a/tools/asahi/centauri-recovery-tests.py b/tools/asahi/centauri-recovery-tests.py old mode 100644 new mode 100755 index dd9f9cf43aebe..88221db0d40a2 --- a/tools/asahi/centauri-recovery-tests.py +++ b/tools/asahi/centauri-recovery-tests.py @@ -16,18 +16,20 @@ ROOT = Path(__file__).resolve().parents[2] SOURCE = (ROOT / 'drivers/net/wireless/apple/centauri/t6050/centauri_pci.c').read_text() +RECOVERY = (ROOT / 'drivers/net/wireless/apple/centauri/t6050/centauri_recovery.c').read_text() +HEADER = (ROOT / 'drivers/net/wireless/apple/centauri/t6050/centauri_t6050.h').read_text() -def block(name): - match = re.search(r'^(?:static )?(?:int|void|bool) ' + name + r'\([^;]*?\)\n\{', SOURCE, re.M) +def block(name, source=SOURCE): + match = re.search(r'^(?:static )?(?:int|void|bool) ' + name + r'\([^;]*?\)\n\{', source, re.M) if not match: raise RuntimeError(f'Missing production function: {name}') - brace = SOURCE.index('{', match.start()) + brace = source.index('{', match.start()) depth, end = 1, brace + 1 while depth: - depth += (SOURCE[end] == '{') - (SOURCE[end] == '}') + depth += (source[end] == '{') - (source[end] == '}') end += 1 - return SOURCE[match.start():end] + return source[match.start():end] PRELUDE = r''' @@ -37,11 +39,14 @@ def block(name): #include #include #include +#define CONFIG_APPLE_CENTAURI_T6050_WIFI 1 +#define IS_ENABLED(option) ((option) ? 1 : 0) #define WRITE_ONCE(x,v) ((x)=(v)) +#define READ_ONCE(x) (x) +#define dev_err(...) ((void)0) #define smp_store_release(p,v) (*(p)=(v)) #define PCI_DEVFN(s,f) (((s)<<3)|(f)) #define PCI_SLOT(f) ((f)>>3) -#define CEN_BOOT_STOPPED 5 #define guard(x) mock_guard #define list_for_each_entry(v,h,n) for ((v)=owner; (v); (v)=NULL) struct device { int lock; void *data; }; @@ -54,7 +59,7 @@ def block(name): struct centauri_t6050_rewake { struct pci_dev *bridge, *control, *beta; unsigned slot, generation, work_generation; - bool active, rerun; + bool active, rerun, force_reset; int error, node; void *arena; struct kref ref; @@ -65,11 +70,22 @@ def block(name): struct pci_dev *pdev; struct centauri_t6050_ipc *ipc; bool removing, ready, firmware_sleep, pm_prepared; + bool boot_attempted; + unsigned char *bar; int boot_work, boot_lock, alpha_irqs, irqs, boot_phase; void *firmware, *descriptor; }; static int cen_recovery_lock, cen_recovery_owners, cen_rewake_wq; static bool cen_exiting, supported_sleep=true; +static int system_long_wq; +static unsigned control_stage; +static bool wake_reset_done; +static unsigned readl(void *bar) { (void)bar; return control_stage; } +static void queue_delayed_work(int wq, int *work, int delay) +{ (void)wq; (void)delay; (*work)++; } +static void centauri_t6050_free_ipc(struct centauri_t6050 *c) { c->ipc = NULL; } +static bool apple_pcie_t6050_warm_reset_done(struct pci_dev *p) +{ (void)p; return wake_reset_done; } static struct centauri_t6050_rewake *owner; static struct pci_dev *slots[3]; static int events[100], nevents, stop_error, beta_error, release_error; @@ -93,6 +109,8 @@ def block(name): static bool centauri_t6050_ipc_fenced(struct centauri_t6050_ipc *p) { return !p || p->fenced; } static void cancel_delayed_work_sync(int *p) { (void)p; event(STOP_BOOT); } +static void centauri_t6050_transport_recover_cancel(struct centauri_t6050 *c) +{ (void)c; } static int centauri_t6050_beta_call(struct centauri_t6050_rewake *r, bool release) { struct centauri_t6050 *c = r->control->dev.data; @@ -144,6 +162,17 @@ def block(name): struct pci_dev fn[3], bridge; struct pci_bus bus; struct centauri_t6050_ipc ipc={.fenced=true}; + static unsigned char bar[0x8004]; + for (unsigned stage=2; stage<=3; stage++) { + reset(&c,&rw,fn,&bridge,&bus); + c.bar=bar; c.boot_phase=CEN_BOOT_READY; c.ipc=&ipc; + control_stage=stage; wake_reset_done=true; + centauri_t6050_schedule_transport_recover(&c); + apple_centauri_sleep_recovery_needed(&fn[2], -EIO); + assert(queues==1 && c.ipc==&ipc && !c.boot_work && !c.boot_lock); + assert(!centauri_t6050_rewake_already_reset(&rw)); + assert(centauri_t6050_rewake_already_reset(&rw)); /* Once per runtime request. */ + } reset(&c,&rw,fn,&bridge,&bus); c.ipc=&ipc; c.firmware=&c; c.descriptor=&rw; assert(!centauri_t6050_quiesce(&c)); @@ -477,10 +506,18 @@ def run(): parser.add_argument('--out', type=Path) parser.add_argument('--negative-controls', action='store_true') args = parser.parse_args() - names = ['apple_centauri_sleep_recovery_needed', 'centauri_t6050_domain_fence', + names = ['centauri_t6050_request_rewake', 'apple_centauri_sleep_recovery_needed', + 'centauri_t6050_rewake_already_reset', 'centauri_t6050_domain_fence', 'centauri_t6050_quiesce', 'centauri_t6050_prepare', 'centauri_t6050_complete'] bodies = '\n'.join(block(name) for name in names) + boot_phases = re.search(r'enum centauri_t6050_boot_phase \{.*?\n\};', HEADER, re.DOTALL) + if boot_phases is None: + raise RuntimeError('Production boot phase enum missing') + bodies = 'typedef unsigned u32;\n' + boot_phases.group() + '\n' + block( + 'centauri_t6050_boot_phase_for_control_stage', RECOVERY) + '\n' + bodies + '\n' + block( + 'centauri_t6050_schedule_transport_recover', RECOVERY) variants = [('baseline', bodies), + ('negative-reuse-old-wake-reset', bodies.replace('rw->force_reset |= force_reset;', '(void)force_reset;')), ('negative-skip-fence', bodies.replace('!centauri_t6050_domain_fence(c->recovery)', 'false')), ('negative-skip-release', bodies.replace('ret = centauri_t6050_beta_call(c->recovery, true);', 'ret = 0;')), ('negative-lost-pm-error', bodies.replace('error = centauri_t6050_wifi_pm_error(c);', 'error = 0;')), @@ -498,6 +535,7 @@ def run(): '\tif (!ret)\n\t\tret = centauri_t6050_beta_call(rw, true);', '\tdevice_unlock(&fn->dev);\n\tif (!ret)\n\t\tret = centauri_t6050_beta_call(rw, true);'))] failures = { + 'negative-reuse-old-wake-reset': '!centauri_t6050_rewake_already_reset(&rw)', 'negative-unlock-before-tuples': '!remove_done && original->dev.locked && rw->control==original', 'negative-skip-fence': 'first(WIFI_STOP)first(FENCE) && first(FREE)>first(RELEASE)', diff --git a/tools/asahi/centauri-wifi-pm-tests.py b/tools/asahi/centauri-wifi-pm-tests.py old mode 100644 new mode 100755 index 930c08839bbaf..a360e317a5df8 --- a/tools/asahi/centauri-wifi-pm-tests.py +++ b/tools/asahi/centauri-wifi-pm-tests.py @@ -17,7 +17,7 @@ def block(source, name): - match = re.search(r"^(?:static )?(?:int|void) " + re.escape(name) + r"\(", source, re.M) + match = re.search(r"^(?:static )?(?:bool|int|void) " + re.escape(name) + r"\([^;]*?\)\n\{", source, re.M) if not match: raise RuntimeError(f"Production function {name} missing") brace = source.index("{", match.start()) @@ -54,7 +54,7 @@ def block(source, name): #define time_after_eq(a, b) (!time_before(a, b)) static unsigned long jiffies; struct centauri_t6050_wifi; -struct centauri_t6050 { struct centauri_t6050_wifi *wifi; bool removing; }; +struct centauri_t6050 { struct centauri_t6050_wifi *wifi; u8 *bar; bool removing; }; struct wiphy { struct centauri_t6050_wifi *wifi; }; struct net_device { int unused; }; struct cfg80211_wowlan { int unused; }; @@ -66,7 +66,9 @@ def block(source, name): struct { u8 *window, *bar; uint64_t window_dma; } a; int lock, connect_work, link_work, data_tx_queue; bool command_failed, stopping, connect_abort, disconnect_pending, connected; - bool reply_valid, pm_prepared; + bool reply_valid, pm_prepared, configured, data_failed, irq_ready; + bool last_fw_event_valid; + struct net_device *netdev; u16 command_head, sequence, reply_capture, reply_sequence, reply_status; u32 reply_command; u8 *reply_buffer; @@ -96,7 +98,14 @@ def block(source, name): static void put_unaligned_le16(u16 value, void *p) { memcpy(p, &value, 2); } static void put_unaligned_le32(u32 value, void *p) { memcpy(p, &value, 4); } static void put_unaligned_le64(uint64_t value, void *p) { memcpy(p, &value, 8); } -static void command_fault(struct centauri_t6050_wifi *wifi) { wifi->command_failed = true; } +static unsigned int recovery_requests; +#define smp_load_acquire(p) (*(p)) +static void log_transport_fault(struct centauri_t6050_wifi *wifi, u32 id, + u32 stage, const char *reason) +{ (void)wifi; (void)id; (void)stage; (void)reason; } +static void schedule_work(int *work) { (void)work; } +static void centauri_t6050_request_transport_recover(struct centauri_t6050 *control) +{ (void)control; recovery_requests++; } static void drain(struct centauri_t6050_wifi *wifi) { assert(wifi->lock); @@ -128,12 +137,12 @@ def block(source, name): static struct centauri_t6050_wifi wifi; static struct wiphy wiphy; static struct net_device netdev; -static u8 window[16384], bar[8192]; +static u8 window[16384], bar[8192], control_bar[0x8004]; static void reset(void) { memset(&wifi, 0, sizeof(wifi)); memset(window, 0, sizeof(window)); - control = (struct centauri_t6050) { .wifi = &wifi }; + control = (struct centauri_t6050) { .wifi = &wifi, .bar = control_bar }; wifi.control = &control; wifi.a.window = window; wifi.a.bar = bar; @@ -239,6 +248,16 @@ def block(source, name): assert(!wifi.pm_disconnect.submitted && !wifi.pm_disconnect.replied); expect_error(-ENOSPC); + prepare(); + recovery_requests = 0; + wifi.stopping = true; + wifi.configured = true; + mutex_lock(&wifi.lock); + command_fault(&wifi, 0, 2, "cancel"); + mutex_unlock(&wifi.lock); + assert(wifi.command_failed && wifi.data_failed && !wifi.configured); + assert(recovery_requests == 0); + prepare(); transport_mode = LOST; assert(disconnect() == -EIO); @@ -285,12 +304,23 @@ def block(source, name): centauri_t6050_wifi_pm_complete(&control); prepare(); bool published = false; + wifi.last_fw_event_valid = true; submission_probe = &published; mutex_lock(&wifi.lock); assert(command_reply(&wifi, 131073, NULL, 0, NULL, NULL, &published) == 0); mutex_unlock(&wifi.lock); assert(published); submission_probe = NULL; + reset(); + unsigned int requests_before = recovery_requests; + wifi.last_fw_event_valid = true; + consumer = 1; /* Ring is not empty, but has room for another command. */ + wifi.command_head = 2; + mutex_lock(&wifi.lock); + assert(command_reply(&wifi, 9, NULL, 0, NULL, NULL, &published) == -ETIMEDOUT); + mutex_unlock(&wifi.lock); + assert(!published && !doorbells && wifi.command_head == 2); + assert(recovery_requests == requests_before + 1); puts("PASS: production disconnect/transport/PM callbacks and first-error retention"); return 0; } @@ -310,13 +340,15 @@ def main(): receipt = re.search(r"struct centauri_wifi_disconnect_receipt \{.*?\n\};", source, re.S) if not receipt: raise RuntimeError("Production receipt structure missing") - names = ["command_reply", "wait_disconnect", "wifi_disconnect", + names = ["centauri_wait_control_idle", "command_fault", "command_reply", "wait_disconnect", "wifi_disconnect", "centauri_t6050_wifi_pm_prepare", "centauri_t6050_wifi_pm_error", "centauri_t6050_wifi_pm_complete", "wifi_suspend"] has_resume = bool(re.search(r"^static int wifi_resume\(", source, re.M)) if has_resume: names.append("wifi_resume") production = "\n\n".join(block(source, name) for name in names) + recovery = (root / "drivers/net/wireless/apple/centauri/t6050/centauri_recovery.c").read_text() + production = block(recovery, "centauri_control_idle_ready") + "\n\n" + production ops = re.search(r"static const struct cfg80211_ops wifi_ops = \{(.*?)\n\};", source, re.S) registration = re.search(r"\.suspend\s*=\s*(\w+)", ops.group(1)) if ops else None suspend = registration.group(1) if registration else "NULL"