From a789b5ca16130b8e46998cb4f73311c6cf6270d6 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:45:42 +1100 Subject: [PATCH 1/2] wifi: apple: centauri: keep the station counter length with its record Every reply to the statistics command overwrites stats_length, while station_stats_valid is only set by a station counter record of a supported length. A later reply carrying another record leaves the counters marked valid with a length that no longer describes them, and the debugfs and connection readers then iterate past station_stats. Record the length of the accepted counter record in its own field, set together with station_stats_valid, and iterate over that. Move the statistics reply handling into a helper so that it can be tested, and add a KUnit case for it (CONFIG_APPLE_CENTAURI_KUNIT_TEST). Signed-off-by: Ryan Murray --- drivers/net/wireless/apple/Kconfig | 11 +++ .../wireless/apple/centauri/centauri_wifi.c | 80 +++++++++++-------- .../apple/centauri/centauri_wifi_test.c | 52 ++++++++++++ 3 files changed, 110 insertions(+), 33 deletions(-) create mode 100644 drivers/net/wireless/apple/centauri/centauri_wifi_test.c diff --git a/drivers/net/wireless/apple/Kconfig b/drivers/net/wireless/apple/Kconfig index d9b13a9785de2..67bf179e87485 100644 --- a/drivers/net/wireless/apple/Kconfig +++ b/drivers/net/wireless/apple/Kconfig @@ -23,6 +23,17 @@ config APPLE_CENTAURI To build as a module, choose M; the module is apple_centauri. +config APPLE_CENTAURI_KUNIT_TEST + bool "KUnit tests for the N1 Centauri Wi-Fi reply parsing" if !KUNIT_ALL_TESTS + depends on APPLE_CENTAURI && KUNIT=y + default KUNIT_ALL_TESTS + help + Build tests that pass statistics replies to the J813 Wi-Fi reply + handler and check the recorded counter lengths. They need no N1 + hardware. + + If unsure, say N. + config APPLE_CENTAURI_T6050 tristate "Apple N1 Centauri T6050 firmware and IPC transport" depends on ARCH_APPLE || COMPILE_TEST diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index de0d4d872932a..5d255a1874323 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -160,6 +160,7 @@ struct centauri_wifi { u8 country[3]; bool country_valid; u8 station_stats[0x378]; + u16 station_stats_len; bool station_stats_valid; u8 rx_crypto_stats[64]; bool rx_crypto_stats_valid; @@ -549,6 +550,43 @@ static void drain_data_tx(struct centauri_wifi *wifi) netif_wake_queue(wifi->netdev); } +/* Record a reply to the statistics command 0xb0001. */ +static void record_stats_reply(struct centauri_wifi *wifi, const u8 *packet, + u16 size, u16 status) +{ + wifi->stats_reply_size = size; + wifi->stats_tag = size >= 28 ? get_unaligned_le16(packet + 24) : 0; + wifi->stats_length = size >= 28 ? get_unaligned_le16(packet + 26) : 0; + if (!status && wifi->stats_tag == 0xf928 && + (wifi->stats_length == 752 || wifi->stats_length == 888) && + size >= 28 + wifi->stats_length) { + /* Native firmware returns the older 752-byte counter record. */ + memset(wifi->station_stats, 0, sizeof(wifi->station_stats)); + memcpy(wifi->station_stats, packet + 28, wifi->stats_length); + /* A later reply can change stats_length; keep this record's own. */ + wifi->station_stats_len = wifi->stats_length; + wifi->station_stats_valid = true; + } + if (!status && wifi->stats_tag == 0xf921 && + wifi->stats_length == sizeof(wifi->rx_crypto_stats) && + size >= 28 + sizeof(wifi->rx_crypto_stats)) { + memcpy(wifi->rx_crypto_stats, packet + 28, sizeof(wifi->rx_crypto_stats)); + wifi->rx_crypto_stats_valid = true; + } + if (!status && wifi->stats_tag == 0xf91b && + wifi->stats_length == sizeof(wifi->dot11_mib_stats) && + size >= 28 + sizeof(wifi->dot11_mib_stats)) { + memcpy(wifi->dot11_mib_stats, packet + 28, sizeof(wifi->dot11_mib_stats)); + wifi->dot11_mib_stats_valid = true; + } + if (!status && wifi->stats_tag == 0xf920 && + wifi->stats_length == sizeof(wifi->tx_power_report) && + size >= 28 + sizeof(wifi->tx_power_report)) { + memcpy(wifi->tx_power_report, packet + 28, sizeof(wifi->tx_power_report)); + wifi->tx_power_report_valid = true; + } +} + static void drain_control(struct centauri_wifi *wifi, bool pm_command) { unsigned int ring; @@ -673,37 +711,8 @@ static void drain_control(struct centauri_wifi *wifi, bool pm_command) pos += 4 + length; } } - if (command == 0xb0001) { - wifi->stats_reply_size = size; - wifi->stats_tag = size >= 28 ? get_unaligned_le16(packet + 24) : 0; - wifi->stats_length = size >= 28 ? get_unaligned_le16(packet + 26) : 0; - } - if (command == 0xb0001 && !status && wifi->stats_tag == 0xf928 && - (wifi->stats_length == 752 || wifi->stats_length == 888) && - size >= 28 + wifi->stats_length) { - /* Native firmware returns the older 752-byte counter record. */ - memset(wifi->station_stats, 0, sizeof(wifi->station_stats)); - memcpy(wifi->station_stats, packet + 28, wifi->stats_length); - wifi->station_stats_valid = true; - } - if (command == 0xb0001 && !status && wifi->stats_tag == 0xf921 && - wifi->stats_length == sizeof(wifi->rx_crypto_stats) && - size >= 28 + sizeof(wifi->rx_crypto_stats)) { - memcpy(wifi->rx_crypto_stats, packet + 28, sizeof(wifi->rx_crypto_stats)); - wifi->rx_crypto_stats_valid = true; - } - if (command == 0xb0001 && !status && wifi->stats_tag == 0xf91b && - wifi->stats_length == sizeof(wifi->dot11_mib_stats) && - size >= 28 + sizeof(wifi->dot11_mib_stats)) { - memcpy(wifi->dot11_mib_stats, packet + 28, sizeof(wifi->dot11_mib_stats)); - wifi->dot11_mib_stats_valid = true; - } - if (command == 0xb0001 && !status && wifi->stats_tag == 0xf920 && - wifi->stats_length == sizeof(wifi->tx_power_report) && - size >= 28 + sizeof(wifi->tx_power_report)) { - memcpy(wifi->tx_power_report, packet + 28, sizeof(wifi->tx_power_report)); - wifi->tx_power_report_valid = true; - } + if (command == 0xb0001) + record_stats_reply(wifi, packet, size, status); if (command == 0xf0025) { wifi->pmk_seen = !status && size >= 60; wifi->pmk_matches = wifi->pmk_seen && @@ -1282,7 +1291,7 @@ static void connect_worker(struct work_struct *work) "connection counters: complete=%d rc=%d valid=%d length=%u\n", wifi->connect_event, probe_ret, wifi->station_stats_valid, wifi->stats_length); if (!probe_ret && wifi->station_stats_valid) - for (i = 0; i < wifi->stats_length; i += 4) { + for (i = 0; i < wifi->station_stats_len; i += 4) { u32 value = get_unaligned_le32(wifi->station_stats + i); if (value) @@ -2276,6 +2285,7 @@ static int query_station_stats(struct centauri_wifi *wifi) put_unaligned_le16(363, payload + 9); put_unaligned_le16(9, payload + 11); wifi->station_stats_valid = false; + wifi->station_stats_len = 0; wifi->stats_reply_size = wifi->stats_tag = wifi->stats_length = 0; ret = wifi->configured ? command(wifi, 0xb0001, payload, sizeof(payload)) : -ENETDOWN; if (!ret) { @@ -2296,7 +2306,7 @@ static int station_stats_show(struct seq_file *seq, void *unused) seq_printf(seq, "rc=%d valid=%d size=%u tag=%#x length=%u\n", ret, wifi->station_stats_valid, wifi->stats_reply_size, wifi->stats_tag, wifi->stats_length); if (!ret && wifi->station_stats_valid) - for (i = 0; i < wifi->stats_length; i += 4) { + for (i = 0; i < wifi->station_stats_len; i += 4) { u32 value = get_unaligned_le32(wifi->station_stats + i); if (value) @@ -2814,3 +2824,7 @@ void centauri_wifi_unregister(struct centauri *c) kfree(wifi); c->wifi = NULL; } + +#if IS_ENABLED(CONFIG_APPLE_CENTAURI_KUNIT_TEST) +#include "centauri_wifi_test.c" +#endif diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi_test.c b/drivers/net/wireless/apple/centauri/centauri_wifi_test.c new file mode 100644 index 0000000000000..9602e219a61e3 --- /dev/null +++ b/drivers/net/wireless/apple/centauri/centauri_wifi_test.c @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Included by centauri_wifi.c to exercise the statistics reply handler. */ +#include + +/* A reply to command 0xb0001 carrying one TLV of @length bytes. */ +static void centauri_stats_reply(struct centauri_wifi *wifi, u8 *packet, + u16 size, u16 tag, u16 length) +{ + memset(packet, 0, size); + put_unaligned_le16(tag, packet + 24); + put_unaligned_le16(length, packet + 26); + record_stats_reply(wifi, packet, size, 0); +} + +static void centauri_station_stats_len_test(struct kunit *test) +{ + struct centauri_wifi *wifi = kunit_kzalloc(test, sizeof(*wifi), GFP_KERNEL); + u8 *packet = kunit_kzalloc(test, BUFFER_SIZE, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, wifi); + KUNIT_ASSERT_NOT_NULL(test, packet); + + centauri_stats_reply(wifi, packet, 28 + 752, 0xf928, 752); + KUNIT_ASSERT_TRUE(test, wifi->station_stats_valid); + KUNIT_EXPECT_EQ(test, wifi->station_stats_len, 752); + + /* A later reply with another record leaves the counters as they were. */ + centauri_stats_reply(wifi, packet, 28, 0xf921, U16_MAX); + KUNIT_EXPECT_EQ(test, wifi->stats_length, U16_MAX); + KUNIT_EXPECT_TRUE(test, wifi->station_stats_valid); + KUNIT_EXPECT_EQ(test, wifi->station_stats_len, 752); + KUNIT_EXPECT_LE(test, wifi->station_stats_len, sizeof(wifi->station_stats)); + + centauri_stats_reply(wifi, packet, 28 + 888, 0xf928, 888); + KUNIT_EXPECT_EQ(test, wifi->station_stats_len, 888); + + /* An unsupported counter record length is not recorded. */ + centauri_stats_reply(wifi, packet, 28 + 900, 0xf928, 900); + KUNIT_EXPECT_EQ(test, wifi->station_stats_len, 888); +} + +static struct kunit_case centauri_wifi_cases[] = { + KUNIT_CASE(centauri_station_stats_len_test), + {} +}; + +static struct kunit_suite centauri_wifi_suite = { + .name = "apple-centauri-wifi", + .test_cases = centauri_wifi_cases, +}; + +kunit_test_suite(centauri_wifi_suite); From 6c2b4388b5d82338ffa11105928cf3d4bfa418bb Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 13:23:17 +1100 Subject: [PATCH 2/2] wifi: apple: centauri: report the kept station counter length The connection log line printed the last statistics reply's length next to the station counters, which no longer has to be the counters' own length. Print the kept counter length there, and add it to the debugfs station_stats header as counters=, after the reply's own fields. Word the KUnit option's help for the driver, not one board. Signed-off-by: Ryan Murray --- drivers/net/wireless/apple/Kconfig | 6 +++--- drivers/net/wireless/apple/centauri/centauri_wifi.c | 8 +++++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/drivers/net/wireless/apple/Kconfig b/drivers/net/wireless/apple/Kconfig index 67bf179e87485..1564b663d9d4f 100644 --- a/drivers/net/wireless/apple/Kconfig +++ b/drivers/net/wireless/apple/Kconfig @@ -28,9 +28,9 @@ config APPLE_CENTAURI_KUNIT_TEST depends on APPLE_CENTAURI && KUNIT=y default KUNIT_ALL_TESTS help - Build tests that pass statistics replies to the J813 Wi-Fi reply - handler and check the recorded counter lengths. They need no N1 - hardware. + Build tests that pass statistics replies to the Centauri Wi-Fi + reply handler and check the recorded counter lengths. They need no + N1 hardware. If unsure, say N. diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index 5d255a1874323..2c16b4b92099c 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -1289,7 +1289,8 @@ static void connect_worker(struct work_struct *work) probe_ret = query_station_stats(wifi); dev_info(&wifi->control->pdev->dev, "connection counters: complete=%d rc=%d valid=%d length=%u\n", - wifi->connect_event, probe_ret, wifi->station_stats_valid, wifi->stats_length); + wifi->connect_event, probe_ret, wifi->station_stats_valid, + wifi->station_stats_len); if (!probe_ret && wifi->station_stats_valid) for (i = 0; i < wifi->station_stats_len; i += 4) { u32 value = get_unaligned_le32(wifi->station_stats + i); @@ -2303,8 +2304,9 @@ static int station_stats_show(struct seq_file *seq, void *unused) mutex_lock(&wifi->lock); ret = query_station_stats(wifi); - seq_printf(seq, "rc=%d valid=%d size=%u tag=%#x length=%u\n", ret, - wifi->station_stats_valid, wifi->stats_reply_size, wifi->stats_tag, wifi->stats_length); + seq_printf(seq, "rc=%d valid=%d size=%u tag=%#x length=%u counters=%u\n", ret, + wifi->station_stats_valid, wifi->stats_reply_size, wifi->stats_tag, + wifi->stats_length, wifi->station_stats_len); if (!ret && wifi->station_stats_valid) for (i = 0; i < wifi->station_stats_len; i += 4) { u32 value = get_unaligned_le32(wifi->station_stats + i);