diff --git a/drivers/net/wireless/apple/Kconfig b/drivers/net/wireless/apple/Kconfig index d9b13a9785de2..1564b663d9d4f 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 Centauri 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..2c16b4b92099c 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 && @@ -1280,9 +1289,10 @@ 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->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 +2286,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) { @@ -2293,10 +2304,11 @@ 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->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 +2826,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);