From 517dd2f1d5602720d72b0b6d7eca3ce6d9a58012 Mon Sep 17 00:00:00 2001 From: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Date: Mon, 16 Nov 2026 08:46:57 -0800 Subject: [PATCH 01/17] wifi: cfg80211: send the held-back scan message of a stale request When a netdev goes down while its driver is still scanning, NETDEV_DOWN completes the scan for cfg80211 with ___cfg80211_scan_done(rdev, false). That parks the result message in rdev->scan_msg, to be sent once the driver is done, and, because the driver still owns the request, only marks the request stale. When the driver later calls cfg80211_scan_done(), the stale path frees the request and returns without queueing scan_done_wk, the only place that sends and clears rdev->scan_msg. The message then stays forever, and nl80211_trigger_scan(), cfg80211_conn_scan() and the internal scans all fail with -EBUSY while it is set: no scan or connect works on that wiphy until the driver is reloaded. Queue scan_done_wk from the stale path as well, so the parked message is sent. Since a run queued this way may find that the message has already gone out and a newer scan is in progress, make the work function return when there is no message to send and the current request has not yet been completed by the driver. Seen on a MacBook Pro (M6, J804) with the Centauri driver, on an earlier base (integration/aurora f5fa539): NetworkManager takes the interface down for a lid-close sleep while a wpa_supplicant scan runs, and after the wake every scan failed with "CTRL-EVENT-SCAN-FAILED ret=-16" until reboot. With this change (and scan abort and interface-down handling in that driver), Wi-Fi reconnected within 2 s after each of two lid sleeps, with no failed scan. Signed-off-by: LiamRay11 <228416644+LiamRay11@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 --- net/wireless/scan.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/net/wireless/scan.c b/net/wireless/scan.c index d35c826c4b1a7..7cb02f3bad3ac 100644 --- a/net/wireless/scan.c +++ b/net/wireless/scan.c @@ -1202,7 +1202,18 @@ void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, void __cfg80211_scan_done(struct wiphy *wiphy, struct wiphy_work *wk) { - ___cfg80211_scan_done(wiphy_to_rdev(wiphy), true); + struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy); + struct cfg80211_scan_request_int *req = + rdev->int_scan_req ? rdev->int_scan_req : rdev->scan_req; + + /* + * Queued for the held-back message of a stale request (see + * cfg80211_scan_done()) that has been sent meanwhile: do not complete + * a newer scan that the driver has not finished. + */ + if (!rdev->scan_msg && req && !req->notified) + return; + ___cfg80211_scan_done(rdev, true); } void cfg80211_scan_done(struct cfg80211_scan_request *request, @@ -1223,6 +1234,13 @@ void cfg80211_scan_done(struct cfg80211_scan_request *request, * it was just kept around for the driver - done now, free it. */ kfree(intreq); + /* + * If the netdev went down while the driver was scanning, the + * result message is still held back in rdev->scan_msg until the + * driver is done, which is now. Only the scan_done work sends + * and clears it; while it is set, every new scan gets -EBUSY. + */ + wiphy_work_queue(&rdev->wiphy, &rdev->scan_done_wk); return; } From b1c3392ad232175be53eccd1454d1c8da1e48daf Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Thu, 19 Nov 2026 23:37:51 +1100 Subject: [PATCH 02/17] wifi: apple: centauri: Quiesce Alpha for sleep on every N1 board The Alpha host-power and ACIPC sleep requests are the N1 firmware's protocol, but suspend and resume only ran them on apple,j813. J815 has the same N1 behind the same PCI functions and its own entry in the driver's board table. Run suspend wherever the native driver bound (c->wifi is set, which needs a board entry), still behind the TEST ONLY alpha_sleep_untested switch, and drop resume's board check: it only undoes requests suspend sent and returns at once otherwise. With the switch off nothing changes on any board. Fixes: 4cca392085a9 ("wifi: apple: centauri: quiesce Alpha for J813 system sleep") Suggested-by: Thomas Glanzmann Signed-off-by: Ryan Murray --- drivers/net/wireless/apple/centauri/centauri_wifi.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index 48d1deca69ab2..3916292f3133b 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -87,7 +87,7 @@ MODULE_PARM_DESC(station_link_addresses, "Diagnostic: use the netdev address for static bool alpha_sleep_untested; module_param(alpha_sleep_untested, bool, 0600); -MODULE_PARM_DESC(alpha_sleep_untested, "TEST ONLY: J813 Alpha host-power and ACIPC sleep handshake"); +MODULE_PARM_DESC(alpha_sleep_untested, "TEST ONLY: Alpha host-power and ACIPC sleep handshake"); struct centauri_rx_snapshot { __le32 version, count; @@ -2705,12 +2705,17 @@ static int alpha_resume_locked(struct centauri_wifi *wifi) return 0; } +/* + * The host-power and ACIPC sleep requests are the N1 firmware's own protocol, the same on + * every board this driver binds (one board entry each, same PCI functions). Resume only + * undoes what suspend sent, so it needs no gate of its own. + */ int centauri_wifi_resume(struct centauri *c) { struct centauri_wifi *wifi = c->wifi; int ret; - if (!wifi || !of_machine_is_compatible("apple,j813")) + if (!wifi) return 0; mutex_lock(&wifi->lock); ret = alpha_resume_locked(wifi); @@ -2723,8 +2728,7 @@ int centauri_wifi_suspend(struct centauri *c) struct centauri_wifi *wifi = c->wifi; int ret, restore; - if (!wifi || !of_machine_is_compatible("apple,j813") || - !READ_ONCE(alpha_sleep_untested)) + if (!wifi || !READ_ONCE(alpha_sleep_untested)) return 0; mutex_lock(&wifi->lock); if (wifi->transport_paused || wifi->connected || wifi->connecting || wifi->scan_request || From a789b5ca16130b8e46998cb4f73311c6cf6270d6 Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Mon, 23 Nov 2026 09:45:42 +1100 Subject: [PATCH 03/17] 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 04/17] 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); From 47706b5b629e3303fd0c82d0d0048d6237e1eb2d Mon Sep 17 00:00:00 2001 From: Kyle Arrington Date: Fri, 20 Nov 2026 19:25:09 -0600 Subject: [PATCH 05/17] wifi: apple: centauri: restart a stopped N1 firmware without a reboot On J813 the N1 firmware can stop while the machine is awake. A background scan sees the Alpha execution stage leave 2 and ends with -EIO, and every later command finds the command ring full. Nothing brought Wi-Fi back short of a reboot: PERST# alone, through the existing port cycle, leaves the firmware running. Restart the chip from the driver: - Release btcentauri and free the firmware's buffers. - Remove functions 1 and 2. The reset returns the chip to its single-function ROM personality, and a read through a stale function's BAR afterwards hangs the fabric. - Cycle the port with the N1's warm reset, given while the port is down. The chip comes back in stage 0. - Run the probe's boot steps, then attach the Beta function again. The warm reset comes from the root port's DT node, with the properties and parsing the T6050 boards already use (bindings/pci/apple,pcie.yaml): apple,function-warm-reset names the SMC and the key (the ADT's pci-bridge0 function-warm_reset), apple,warm-reset-writes the writes. A board restarts only when its root port has both and its port cycle is known; without them the driver never resets the chip. J813 gets them: key 'bpli', the same key and argument as the T6050 boards, with the T6050 writes. The Wi-Fi code reports the signs where it already sees them: the scan loop's and command_control()'s stage checks, a full command ring and a command timeout. A report queues a check 2 s later on an ordered, freezable workqueue, which restarts the chip only if the firmware is still stopped and the transport is not paused for a system sleep. At most three restarts in 30 minutes. There is no runtime switch; a board without the DT properties doesn't restart. debugfs rom_boot 4 restarts by hand, 5 and 6 test the fault path as a stopped firmware and as a fault that clears. The status file now reads under boot_lock, so no BAR read lands in a restart's port cycle. The driver now depends on MFD_MACSMC. Known limit, shared with the debugfs boot steps: removing function 0 under the PCI rescan lock (sysfs remove, host unbind) while a restart runs deadlocks. Signed-off-by: Kyle Arrington Signed-off-by: Nick --- .../bindings/net/wireless/apple,centauri.yaml | 5 + arch/arm64/boot/dts/apple/t8142-j813.dts | 7 + drivers/net/wireless/apple/Kconfig | 1 + .../net/wireless/apple/centauri/centauri.h | 15 +- .../wireless/apple/centauri/centauri_ipc.c | 8 +- .../wireless/apple/centauri/centauri_pci.c | 167 +++++++++++++++++- .../wireless/apple/centauri/centauri_port.c | 100 ++++++++++- .../wireless/apple/centauri/centauri_wifi.c | 26 ++- 8 files changed, 320 insertions(+), 9 deletions(-) diff --git a/Documentation/devicetree/bindings/net/wireless/apple,centauri.yaml b/Documentation/devicetree/bindings/net/wireless/apple,centauri.yaml index 8d226eec13091..7f064886f055f 100644 --- a/Documentation/devicetree/bindings/net/wireless/apple,centauri.yaml +++ b/Documentation/devicetree/bindings/net/wireless/apple,centauri.yaml @@ -24,6 +24,11 @@ description: | native driver selects personalized firmware and transport data from its root-board match table rather than the T6050 wire-protocol properties. + On T8142 the root port above function zero may describe the N1's warm + reset with apple,function-warm-reset and apple,warm-reset-writes, as in + pci/apple,pcie.yaml. With both, the native driver restarts a firmware + that stopped; without them it never resets the chip. + properties: compatible: oneOf: diff --git a/arch/arm64/boot/dts/apple/t8142-j813.dts b/arch/arm64/boot/dts/apple/t8142-j813.dts index 5260a5eed2ab7..b4cc280fc9ef7 100644 --- a/arch/arm64/boot/dts/apple/t8142-j813.dts +++ b/arch/arm64/boot/dts/apple/t8142-j813.dts @@ -170,6 +170,13 @@ #address-cells = <3>; #size-cells = <2>; ranges; + /* + * The N1's warm reset: the ADT's pci-bridge0 function-warm_reset (the SMC, key + * 'bpli', the same key and argument as on the T6050 boards) and the T6050 writes. + * The N1 driver restarts a stopped firmware with it. + */ + apple,function-warm-reset = <&smc 0x62706c69 0x1000000>; + apple,warm-reset-writes = <0x00010001 17000>, <0x00010001 20000>; n1_control: control@0,0 { compatible = "apple,t8142-centauri-control", "pci106b,1900"; diff --git a/drivers/net/wireless/apple/Kconfig b/drivers/net/wireless/apple/Kconfig index d9b13a9785de2..e7d24b6e7a52e 100644 --- a/drivers/net/wireless/apple/Kconfig +++ b/drivers/net/wireless/apple/Kconfig @@ -10,6 +10,7 @@ config APPLE_CENTAURI tristate "Apple N1 Centauri experimental J813 Wi-Fi" depends on ARCH_APPLE || COMPILE_TEST depends on PCI && CFG80211 && DEBUG_FS + depends on MFD_MACSMC select FW_LOADER help Experimental Apple N1 PCIe and ACIPC Wi-Fi support for the J813 diff --git a/drivers/net/wireless/apple/centauri/centauri.h b/drivers/net/wireless/apple/centauri/centauri.h index fa937c1c4e2e3..167539595226c 100644 --- a/drivers/net/wireless/apple/centauri/centauri.h +++ b/drivers/net/wireless/apple/centauri/centauri.h @@ -6,6 +6,7 @@ #include #include #include +#include #define CEN_ALPHA_SCRATCH_OFFSET SZ_8M #define CEN_ALPHA_SCRATCH_SIZE (5 * SZ_1M) @@ -23,7 +24,7 @@ struct centauri { void __iomem *bar; resource_size_t size; u32 stage_offset; - struct dentry *debug; + struct dentry *debug, *crash_file; struct mutex boot_lock; void *firmware, *descriptor, *msi_page; dma_addr_t firmware_dma, descriptor_dma, msi_dma; @@ -35,6 +36,13 @@ struct centauri { bool link_ready; struct centauri_ipc *ipc; struct centauri_wifi *wifi; + /* Restart of a stopped firmware: requested by the Wi-Fi code, confirmed and run here */ + struct delayed_work recover_work; + unsigned long recover_at[3]; + bool removing; + bool recover_gave_up; /* restart limit reached: requests are ignored */ + bool warm_reset; /* the root port describes the N1's warm reset: restart on a stop */ + bool inject_stall; /* debugfs test: the next check finds the firmware stopped */ }; /* One row per machine with an N1: the FTAB file name, the word written to BAR+0x807c, @@ -92,7 +100,8 @@ static inline const struct centauri_board *centauri_board(void) return NULL; } -int centauri_port_cycle(struct pci_dev *pdev); +int centauri_port_cycle(struct pci_dev *pdev, bool reset_chip); +bool centauri_warm_reset_described(struct pci_dev *pdev); int centauri_start_ipc(struct centauri *c); void centauri_free_ipc(struct centauri *c); int centauri_alpha_resources(struct centauri *c, struct cen_alpha_resources *resources); @@ -102,4 +111,6 @@ void centauri_wifi_unregister(struct centauri *c); int centauri_wifi_suspend(struct centauri *c); int centauri_wifi_resume(struct centauri *c); void centauri_wifi_kick(struct centauri *c); +bool centauri_wifi_stalled(struct centauri *c); +void centauri_recovery_request(struct centauri *c, const char *why); #endif diff --git a/drivers/net/wireless/apple/centauri/centauri_ipc.c b/drivers/net/wireless/apple/centauri/centauri_ipc.c index 8d117989cd760..a94ff01dc440b 100644 --- a/drivers/net/wireless/apple/centauri/centauri_ipc.c +++ b/drivers/net/wireless/apple/centauri/centauri_ipc.c @@ -169,7 +169,13 @@ static int secondary_init(struct centauri *c) pci_write_config_dword(c->pdev, 0xf8c, upper_32_bits(c->ipc->secondary->dma)); pci_write_config_dword(c->pdev, 0xf90, required); dev_info(&c->pdev->dev, "secondary memory: seed=%u required=%u\n", length, required); - debugfs_create_file("firmware_crash", 0400, c->debug, c, &firmware_crash_fops); + /* + * Created once and kept across restarts: its reader looks up c->ipc under boot_lock, + * and removing it here, under boot_lock, would wait for a reader blocked on that lock. + */ + if (!c->crash_file) + c->crash_file = debugfs_create_file("firmware_crash", 0400, c->debug, c, + &firmware_crash_fops); return 0; } diff --git a/drivers/net/wireless/apple/centauri/centauri_pci.c b/drivers/net/wireless/apple/centauri/centauri_pci.c index 598671387565a..ad3ff5d538d21 100644 --- a/drivers/net/wireless/apple/centauri/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/centauri_pci.c @@ -15,6 +15,7 @@ #include #include #include +#include #include "centauri.h" @@ -28,6 +29,8 @@ static int centauri_status_show(struct seq_file *s, void *unused) { struct centauri *c = s->private; + /* A restart cycles the link: no BAR read while it runs */ + mutex_lock(&c->boot_lock); seq_printf(s, "pci=%s vendor=%04x device=%04x\n", pci_name(c->pdev), c->pdev->vendor, c->pdev->device); if (c->link_ready) @@ -38,6 +41,7 @@ static int centauri_status_show(struct seq_file *s, void *unused) seq_printf(s, "transport=hardware-pci firmware_boot=%s image_response=%#x wifi=%s\n", c->boot_attempted ? "attempted" : "not-started", c->image_response, READ_ONCE(c->wifi) ? "registered" : "not-owned-by-this-function"); + mutex_unlock(&c->boot_lock); return 0; } DEFINE_SHOW_ATTRIBUTE(centauri_status); @@ -156,6 +160,145 @@ static int centauri_rom_boot(struct centauri *c) return ret; } +/* + * Restart the N1 from a running or stopped firmware, without the bootloader: release + * everything the firmware could write to, then a port cycle with the SMC warm reset + * (PERST# alone leaves the firmware running), which leaves the chip in its ROM (stage 0), + * then the boot steps 1-3 as at probe. btcentauri is released first: it drives function 2 + * over the IPC that is freed here. + * + * Not safe against function 0 being removed under the PCI rescan lock (sysfs remove, host + * unbind, hot unplug) while a restart runs: remove waits for the restart, which waits for the + * rescan lock. The debugfs boot steps have the same limit. + */ +static int centauri_recover(struct centauri *c) +{ + struct device *dev = &c->pdev->dev; + u32 stage = readl(c->bar + 0x8000); + struct pci_dev *beta; + int ret, i; + + if (!c->warm_reset) + return -EOPNOTSUPP; + if (!c->link_ready || PCI_FUNC(c->pdev->devfn)) + return -EBUSY; + dev_info(dev, "recovery: stage %#x, releasing the firmware's buffers\n", stage); + beta = pci_get_slot(c->pdev->bus, PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 2)); + if (beta) + device_release_driver(&beta->dev); + pci_dev_put(beta); + centauri_free_boot(c); + /* + * The warm reset puts the chip back in its single-function ROM personality and wipes + * functions 1 and 2. Remove them now; the IPC start's rescan adds them again, as at + * probe. A stale function's BAR read after the reset hangs the fabric. + */ + pci_lock_rescan_remove(); + for (i = 2; i >= 1; i--) { + struct pci_dev *fn = pci_get_slot(c->pdev->bus, + PCI_DEVFN(PCI_SLOT(c->pdev->devfn), i)); + + if (fn) + pci_stop_and_remove_bus_device(fn); + pci_dev_put(fn); + } + pci_unlock_rescan_remove(); + c->boot_attempted = false; + c->image_response = 0; + c->link_ready = false; + ret = centauri_port_cycle(c->pdev, true); + if (ret) { + dev_err(dev, "recovery: reset port cycle failed: %d\n", ret); + return ret; + } + c->link_ready = true; + stage = readl(c->bar + 0x8000); + dev_info(dev, "recovery: stage %#x after the warm reset\n", stage); + if (stage != 0) + return -EIO; + ret = centauri_rom_boot(c); + if (ret) { + dev_err(dev, "recovery: ROM boot failed: %d\n", ret); + return ret; + } + msleep(2000); + c->link_ready = false; + ret = centauri_port_cycle(c->pdev, false); + if (ret) { + dev_err(dev, "recovery: boot port cycle failed: %d\n", ret); + return ret; + } + c->link_ready = true; + dev_info(dev, "recovery: post-reset execution stage=%#x\n", readl(c->bar + 0x8000)); + msleep(2000); + ret = centauri_start_ipc(c); + dev_info(dev, "recovery: IPC start %d\n", ret); + return ret; +} + +/* Ordered and freezable: a restart never runs during a system sleep or beside another one. */ +static struct workqueue_struct *centauri_wq; + +/* + * The Wi-Fi code saw a sign of a stopped firmware: an Alpha execution stage other than 2, + * a command ring the firmware no longer drains, or a command timeout. The worker looks + * again 2 s later and restarts the chip only if the firmware is still stopped. + */ +void centauri_recovery_request(struct centauri *c, const char *why) +{ + if (!c->warm_reset || READ_ONCE(c->removing) || READ_ONCE(c->recover_gave_up)) + return; + if (queue_delayed_work(centauri_wq, &c->recover_work, 2 * HZ)) + dev_warn(&c->pdev->dev, "firmware fault (%s); checking again in 2 s\n", why); +} + +static void centauri_recover_worker(struct work_struct *work) +{ + struct centauri *c = container_of(to_delayed_work(work), struct centauri, recover_work); + struct device *dev = &c->pdev->dev; + struct pci_dev *beta; + unsigned long now = jiffies; + int ret; + + mutex_lock(&c->boot_lock); + if (READ_ONCE(c->removing) || !c->ipc) { + mutex_unlock(&c->boot_lock); + return; + } + if (!xchg(&c->inject_stall, false) && !centauri_wifi_stalled(c)) { + mutex_unlock(&c->boot_lock); + dev_info(dev, "firmware fault cleared; no restart\n"); + return; + } + /* Three restarts in 30 minutes: the chip is left down rather than reset in a loop. */ + if (c->recover_at[0] && time_before(now, c->recover_at[0] + 30 * 60 * HZ)) { + WRITE_ONCE(c->recover_gave_up, true); + mutex_unlock(&c->boot_lock); + dev_err(dev, "firmware stopped again after 3 restarts in 30 min; leaving Wi-Fi down until the driver is reloaded\n"); + return; + } + memmove(c->recover_at, c->recover_at + 1, sizeof(c->recover_at) - sizeof(c->recover_at[0])); + c->recover_at[ARRAY_SIZE(c->recover_at) - 1] = now; + dev_warn(dev, "firmware stopped; restarting the N1\n"); + ret = centauri_recover(c); + mutex_unlock(&c->boot_lock); + if (ret) { + dev_err(dev, "restart failed: %d\n", ret); + return; + } + /* + * The IPC start's rescan added the Beta function before its firmware ran, so + * btcentauri refused it then. Attach it now, outside the rescan lock. + */ + beta = pci_get_slot(c->pdev->bus, PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 2)); + if (beta && !beta->dev.driver) + dev_info(dev, "restarted; Bluetooth function attach: %d\n", + device_attach(&beta->dev)); + else + dev_info(dev, "restarted\n"); + pci_dev_put(beta); +} + static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, size_t count, loff_t *pos) { @@ -163,18 +306,25 @@ static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, u32 value; int ret = kstrtou32_from_user(buffer, count, 0, &value); - if (ret || value < 1 || value > 3) + if (ret || value < 1 || value > 6) return ret ?: -EINVAL; mutex_lock(&c->boot_lock); if (value == 1) { ret = centauri_rom_boot(c); } else if (value == 3) { ret = centauri_start_ipc(c); + } else if (value == 4) { + ret = centauri_recover(c); + } else if (value == 5 || value == 6) { + /* Test the fault path: 5 as a stopped firmware, 6 as a fault that clears. */ + WRITE_ONCE(c->inject_stall, value == 5); + centauri_recovery_request(c, value == 5 ? "debugfs: stopped" : "debugfs: spurious"); + ret = 0; } else if (!c->firmware || !c->link_ready || readl(c->bar + 0x8000) != 4) { ret = -EBUSY; } else { c->link_ready = false; - ret = centauri_port_cycle(c->pdev); + ret = centauri_port_cycle(c->pdev, false); if (!ret) { c->link_ready = true; dev_info(&c->pdev->dev, "post-reset execution stage=%#x\n", @@ -210,6 +360,8 @@ static int centauri_probe(struct pci_dev *pdev, const struct pci_device_id *id) c->pdev = pdev; c->link_ready = true; mutex_init(&c->boot_lock); + INIT_DELAYED_WORK(&c->recover_work, centauri_recover_worker); + c->warm_reset = centauri_warm_reset_described(pdev); c->stage_offset = id->driver_data; ret = pcim_enable_device(pdev); if (ret) @@ -246,6 +398,9 @@ static void centauri_remove(struct pci_dev *pdev) { struct centauri *c = pci_get_drvdata(pdev); + /* Disabled, not only cancelled: a request racing this remove can no longer queue it */ + WRITE_ONCE(c->removing, true); + disable_delayed_work_sync(&c->recover_work); centauri_wifi_unregister(c); debugfs_remove_recursive(c->debug); centauri_free_boot(c); @@ -306,10 +461,15 @@ static int __init centauri_init(void) { int ret; + centauri_wq = alloc_ordered_workqueue("apple_centauri", WQ_FREEZABLE); + if (!centauri_wq) + return -ENOMEM; centauri_debug = debugfs_create_dir("apple_centauri", NULL); ret = pci_register_driver(¢auri_driver); - if (ret) + if (ret) { debugfs_remove_recursive(centauri_debug); + destroy_workqueue(centauri_wq); + } return ret; } @@ -317,6 +477,7 @@ static void __exit centauri_exit(void) { pci_unregister_driver(¢auri_driver); debugfs_remove_recursive(centauri_debug); + destroy_workqueue(centauri_wq); } module_init(centauri_init); module_exit(centauri_exit); diff --git a/drivers/net/wireless/apple/centauri/centauri_port.c b/drivers/net/wireless/apple/centauri/centauri_port.c index 59eadc3f8521c..ba54ac5838dad 100644 --- a/drivers/net/wireless/apple/centauri/centauri_port.c +++ b/drivers/net/wireless/apple/centauri/centauri_port.c @@ -11,7 +11,10 @@ #include #include #include +#include +#include #include +#include #include "centauri.h" static void update(void __iomem *base, u32 offset, u32 clear, u32 set) @@ -26,7 +29,100 @@ static bool wait_bits(void __iomem *address, u32 mask, u32 wanted, u32 us) return !readl_poll_timeout(address, value, (value & mask) == wanted, 100, us); } -int centauri_port_cycle(struct pci_dev *pdev) +#define CEN_WARM_WRITES_MAX 4 + +/* + * The N1's warm reset, as the root port describes it (bindings/pci/apple,pcie.yaml, as on the + * T6050 boards): "apple,function-warm-reset" names the SMC and the key (the ADT's pci-bridge0 + * function-warm_reset), "apple,warm-reset-writes" the writes: the key's value, its bytes from + * the cell's most significant down, and the wait after it in microseconds. Reads them into + * @writes and returns how many, with the SMC's device (a reference) in *smc_pdev, or an error. + */ +static int warm_reset_parse(struct pci_dev *pdev, struct platform_device **smc_pdev, u32 *key, + u32 writes[2 * CEN_WARM_WRITES_MAX]) +{ + struct pci_dev *bridge = pdev->bus->self; + struct device_node *np = bridge ? pci_device_to_OF_node(bridge) : NULL; + struct of_phandle_args fn; + int count, i; + + *smc_pdev = NULL; + if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || !np || + of_parse_phandle_with_fixed_args(np, "apple,function-warm-reset", 2, 0, &fn)) + return -ENODEV; + *smc_pdev = of_find_device_by_node(fn.np); + of_node_put(fn.np); + if (!*smc_pdev) + return -ENODEV; + *key = fn.args[0]; + count = of_property_count_u32_elems(np, "apple,warm-reset-writes"); + if (count < 2 || count % 2 || count > 2 * CEN_WARM_WRITES_MAX || + of_property_read_u32_array(np, "apple,warm-reset-writes", writes, count)) + goto bad; + for (i = 1; i < count; i += 2) + if (writes[i] > 100 * USEC_PER_MSEC) + goto bad; + return count / 2; +bad: + put_device(&(*smc_pdev)->dev); + *smc_pdev = NULL; + return -EINVAL; +} + +/* + * The root port describes a warm reset, and centauri_port_cycle() can cycle this port: only + * then does the driver restart a stopped N1, so a restart never tears the functions down on a + * board whose port cycle would refuse. + */ +bool centauri_warm_reset_described(struct pci_dev *pdev) +{ + const struct centauri_board *board = centauri_board(); + struct pci_dev *bridge = pdev->bus->self; + u32 key, writes[2 * CEN_WARM_WRITES_MAX]; + struct platform_device *smc_pdev; + + if (!board || !board->port || !bridge || bridge->vendor != PCI_VENDOR_ID_APPLE || + (board->bridge_id && bridge->device != board->bridge_id)) + return false; + if (warm_reset_parse(pdev, &smc_pdev, &key, writes) <= 0) + return false; + put_device(&smc_pdev->dev); + return true; +} + +/* + * Given while the port is down and not set up: the link then trains a chip that was just + * reset. PERST# alone leaves the firmware running. + */ +static int warm_reset(struct pci_dev *pdev) +{ + u32 key, writes[2 * CEN_WARM_WRITES_MAX]; + struct platform_device *smc_pdev; + struct apple_smc *smc; + int count, i, ret = 0; + + count = warm_reset_parse(pdev, &smc_pdev, &key, writes); + if (count <= 0) { + ret = count ?: -ENODEV; + goto out; + } + smc = platform_get_drvdata(smc_pdev); + if (!smc) + ret = -ENODEV; + for (i = 0; i < count && !ret; i++) { + __be32 value = cpu_to_be32(writes[2 * i]); + + ret = apple_smc_write(smc, key, &value, sizeof(value)); + if (!ret) + fsleep(writes[2 * i + 1]); + } + put_device(&smc_pdev->dev); +out: + dev_info(&pdev->dev, "warm reset through the SMC: %d\n", ret); + return ret; +} + +int centauri_port_cycle(struct pci_dev *pdev, bool reset_chip) { static const u32 reset[][2] = { {0x88, 0x110}, {0x100, ~0U}, {0x148, ~0U}, {0x210, ~0U}, @@ -141,6 +237,8 @@ int centauri_port_cycle(struct pci_dev *pdev) writel(0, port + 0x3800 + 4 * i); for (i = 0; i < ARRAY_SIZE(tune); i++) writel(tune[i][1], port + tune[i][0]); + if (reset_chip) + warm_reset(pdev); update(port, 0x800, 0, 1); udelay(10); update(phy, 0, 0, 1); diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index de0d4d872932a..646615027625d 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -745,8 +745,10 @@ static int command_control(struct centauri_wifi *wifi, u32 id, const void *paylo return -EBUSY; if (size + 24 > BUFFER_SIZE) return -EMSGSIZE; - if ((slot + 1) % ring_size[1] == index_read(wifi, 1, 1)) + if ((slot + 1) % ring_size[1] == index_read(wifi, 1, 1)) { + centauri_recovery_request(wifi->control, "command ring full"); return -ENOSPC; + } packet = wifi->a.window + TX_BUFFER + slot * BUFFER_SIZE; memset(packet, 0, 24 + size); put_unaligned_le16(24 + size, packet + 0x10); @@ -766,8 +768,10 @@ static int command_control(struct centauri_wifi *wifi, u32 id, const void *paylo writel(1, wifi->a.bar + 0x1004); do { drain_control(wifi, pm_command); - if (readl(wifi->a.bar + 8) != 2) + if (readl(wifi->a.bar + 8) != 2) { + centauri_recovery_request(wifi->control, "command: Alpha stage"); return -EIO; + } /* Bit 30 is more-data-pending, not part of the command identity. * Scan/connect replies set it before their later completion event. */ @@ -782,9 +786,26 @@ static int command_control(struct centauri_wifi *wifi, u32 id, const void *paylo 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]); + centauri_recovery_request(wifi->control, "command timeout"); return -ETIMEDOUT; } +/* + * The firmware has stopped: Alpha left execution stage 2, or the command ring stays full + * (a running firmware drains each command before the next is queued). Not while the + * transport is paused for a system sleep. + */ +bool centauri_wifi_stalled(struct centauri *c) +{ + struct centauri_wifi *wifi = c->wifi; + + if (!wifi || READ_ONCE(wifi->transport_paused) || READ_ONCE(wifi->stopping)) + return false; + if (readl(wifi->a.bar + 8) != 2) + return true; + return (READ_ONCE(wifi->command_head) + 1) % ring_size[1] == index_read(wifi, 1, 1); +} + static int command(struct centauri_wifi *wifi, u32 id, const void *payload, size_t size) { return command_control(wifi, id, payload, size, false); @@ -955,6 +976,7 @@ static void scan_worker(struct work_struct *work) do { drain(wifi); if (readl(wifi->a.bar + 8) != 2) { + centauri_recovery_request(wifi->control, "scan: Alpha stage"); ret = -EIO; break; } From c28ac9cc500adaccac9bc7df668e44d33e1ba542 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 07:36:07 +0300 Subject: [PATCH 06/17] mfd: macsmc: Parse and give a PCIe endpoint's warm reset A PCIe root port names its endpoint's warm reset with "apple,function-warm-reset" (the SMC and the key) and "apple,warm-reset-writes" (each value and the wait after it). The T6050 host reads them in pcie-apple, and the N1 restart on T8142 needs the same: a second copy of the parser would drift from the first. Add apple_smc_warm_reset_parse(), apple_smc_warm_reset_put() and apple_smc_warm_reset() to the SMC driver, and use them in the T6050 host. The host's checks and messages are unchanged: at most four writes, no wait above 100 ms, a device link to the SMC for the noirq write, and no reset without both properties. Signed-off-by: Nick --- drivers/mfd/macsmc.c | 64 +++++++++++++++++++++++++++ drivers/pci/controller/pcie-apple.c | 68 +++++------------------------ include/linux/mfd/macsmc.h | 49 +++++++++++++++++++++ 3 files changed, 125 insertions(+), 56 deletions(-) diff --git a/drivers/mfd/macsmc.c b/drivers/mfd/macsmc.c index 35f86f9fe0027..cf1bbdab1889e 100644 --- a/drivers/mfd/macsmc.c +++ b/drivers/mfd/macsmc.c @@ -913,6 +913,70 @@ int apple_smc_write_noirq(struct apple_smc *smc, smc_key key, const void *buf, s } EXPORT_SYMBOL_GPL(apple_smc_write_noirq); +int apple_smc_warm_reset_parse(struct device_node *np, struct apple_smc_warm_reset *wr) +{ + struct of_phandle_args fn; + int count, i; + + memset(wr, 0, sizeof(*wr)); + if (!np || of_parse_phandle_with_fixed_args(np, "apple,function-warm-reset", 2, 0, &fn)) + return -ENODEV; + wr->smc = of_find_device_by_node(fn.np); + of_node_put(fn.np); + if (!wr->smc) + return -ENODEV; + count = of_property_count_u32_elems(np, "apple,warm-reset-writes"); + if (count < 2 || count % 2 || count > ARRAY_SIZE(wr->writes) || + of_property_read_u32_array(np, "apple,warm-reset-writes", wr->writes, count)) + goto bad; + for (i = 1; i < count; i += 2) + if (wr->writes[i] > 100 * USEC_PER_MSEC) + goto bad; + wr->key = fn.args[0]; + wr->count = count / 2; + return 0; +bad: + apple_smc_warm_reset_put(wr); + return -EINVAL; +} +EXPORT_SYMBOL_GPL(apple_smc_warm_reset_parse); + +void apple_smc_warm_reset_put(struct apple_smc_warm_reset *wr) +{ + if (wr->smc) + put_device(&wr->smc->dev); + memset(wr, 0, sizeof(*wr)); +} +EXPORT_SYMBOL_GPL(apple_smc_warm_reset_put); + +int apple_smc_warm_reset(const struct apple_smc_warm_reset *wr, bool noirq) +{ + struct apple_smc *smc = wr->smc ? platform_get_drvdata(wr->smc) : NULL; + unsigned int i; + int ret = 0; + + if (!smc || !wr->count) + return -ENODEV; + for (i = 0; i < wr->count && !ret; i++) { + __be32 value = cpu_to_be32(wr->writes[2 * i]); + u32 wait = wr->writes[2 * i + 1]; + + if (noirq) { + ret = apple_smc_write_noirq(smc, wr->key, &value, sizeof(value)); + if (!ret) { + mdelay(wait / USEC_PER_MSEC); + udelay(wait % USEC_PER_MSEC); + } + } else { + ret = apple_smc_write(smc, wr->key, &value, sizeof(value)); + if (!ret) + fsleep(wait); + } + } + return ret; +} +EXPORT_SYMBOL_GPL(apple_smc_warm_reset); + static void apple_smc_write_mbse(struct apple_smc *smc, smc_key value) { int ret; diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c index 824b94d04caa7..7d139eab8c452 100644 --- a/drivers/pci/controller/pcie-apple.c +++ b/drivers/pci/controller/pcie-apple.c @@ -565,7 +565,6 @@ struct apple_pcie_pd { struct apple_radio_sleep; -#define APPLE_PCIE_T6050_WARM_WRITES 4 struct apple_pcie { struct mutex lock; @@ -588,10 +587,7 @@ struct apple_pcie { /* Those ports back, and their functions looked for, once the resume is over */ struct work_struct t6050_unpark_work; /* The endpoint's warm reset, given by this host while the port is down */ - struct platform_device *t6050_smc; - u32 t6050_warm_key; - u32 t6050_warm_writes[2 * APPLE_PCIE_T6050_WARM_WRITES]; - unsigned int t6050_warm_count; + struct apple_smc_warm_reset t6050_warm; bool t6050_warm_done; /* When this wake's bring-up of that port had the link up and its 100 ms over; 0 before */ ktime_t t6050_link_up; @@ -5546,35 +5542,14 @@ static int apple_pcie_t6050_pm_notify(struct notifier_block *nb, unsigned long a static void apple_pcie_t6050_warm_reset_probe(struct platform_device *pdev, struct apple_pcie *pcie, struct apple_pcie_port *port) { - struct of_phandle_args fn; - int count, i; - if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || - of_parse_phandle_with_fixed_args(port->np, "apple,function-warm-reset", 2, 0, &fn)) - return; - pcie->t6050_smc = of_find_device_by_node(fn.np); - of_node_put(fn.np); - if (!pcie->t6050_smc) + apple_smc_warm_reset_parse(port->np, &pcie->t6050_warm)) return; - count = of_property_count_u32_elems(port->np, "apple,warm-reset-writes"); - if (count < 2 || count % 2 || count > ARRAY_SIZE(pcie->t6050_warm_writes) || - of_property_read_u32_array(port->np, "apple,warm-reset-writes", - pcie->t6050_warm_writes, count)) - goto drop; - for (i = 1; i < count; i += 2) - if (pcie->t6050_warm_writes[i] > 100 * USEC_PER_MSEC) - goto drop; /* The SMC's noirq resume comes before this host's: the reset is written from there */ - if (!device_link_add(&pdev->dev, &pcie->t6050_smc->dev, DL_FLAG_STATELESS)) { + if (!device_link_add(&pdev->dev, &pcie->t6050_warm.smc->dev, DL_FLAG_STATELESS)) { dev_warn(&pdev->dev, "no warm reset after a sleep: no link to the SMC\n"); - goto drop; + apple_smc_warm_reset_put(&pcie->t6050_warm); } - pcie->t6050_warm_key = fn.args[0]; - pcie->t6050_warm_count = count / 2; - return; -drop: - put_device(&pcie->t6050_smc->dev); - pcie->t6050_smc = NULL; } /* @@ -5585,34 +5560,15 @@ static void apple_pcie_t6050_warm_reset_probe(struct platform_device *pdev, stru */ static int apple_pcie_t6050_warm_reset(struct apple_pcie *pcie, bool noirq) { - struct apple_smc *smc; - unsigned int i; - int ret = 0; + int ret; pcie->t6050_warm_done = false; - if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || !pcie->t6050_warm_count) + if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || !pcie->t6050_warm.count) return -ENODEV; - smc = platform_get_drvdata(pcie->t6050_smc); - if (!smc) { + ret = apple_smc_warm_reset(&pcie->t6050_warm, noirq); + if (ret == -ENODEV) { dev_warn(pcie->dev, "no warm reset: the SMC isn't bound\n"); - return -ENODEV; - } - for (i = 0; i < pcie->t6050_warm_count && !ret; i++) { - __be32 value = cpu_to_be32(pcie->t6050_warm_writes[2 * i]); - u32 wait = pcie->t6050_warm_writes[2 * i + 1]; - smc_key key = pcie->t6050_warm_key; - - if (noirq) { - ret = apple_smc_write_noirq(smc, key, &value, sizeof(value)); - if (!ret) { - mdelay(wait / USEC_PER_MSEC); - udelay(wait % USEC_PER_MSEC); - } - } else { - ret = apple_smc_write(smc, key, &value, sizeof(value)); - if (!ret) - fsleep(wait); - } + return ret; } pcie->t6050_warm_done = !ret; if (ret) @@ -5811,7 +5767,7 @@ void apple_pcie_t6050_sleep_reset(struct pci_dev *bridge) { struct apple_pcie *pcie = apple_pcie_t6050_from_bridge(bridge); - if (pcie && pcie->t6050_warm_count) { + if (pcie && pcie->t6050_warm.count) { pcie->t6050_keep_alive = false; pcie->t6050_reset_before_sleep = true; } @@ -5876,7 +5832,7 @@ int apple_pcie_t6050_port_recycle(struct pci_dev *bridge) if (!pcie) return -ENODEV; - if (!pcie->t6050_warm_count || pcie->t6050_lost || READ_ONCE(pcie->t6050_down)) + if (!pcie->t6050_warm.count || pcie->t6050_lost || READ_ONCE(pcie->t6050_down)) return -EBUSY; pci_save_state(bridge); ret = apple_pcie_t6050_sleep_down(pcie); @@ -7645,7 +7601,7 @@ static int apple_pcie_resume_noirq(struct device *dev) /* Up, and reset unless kept alive: nothing takes it down again */ pcie->t6050_woke = !READ_ONCE(pcie->t6050_down) && !pcie->t6050_lost && (!warm_reset || pcie->t6050_warm_done || - !pcie->t6050_warm_count); + !pcie->t6050_warm.count); list_for_each_entry(port, &pcie->ports, entry) if (pcie->t6050_parked & BIT(port->idx)) dev_dbg(dev, "port %d at the wake: parked since before the sleep, power off, PERST# low, no functions, root port %s, DART %s; %s\n", diff --git a/include/linux/mfd/macsmc.h b/include/linux/mfd/macsmc.h index 5afcf707a7c31..47d4bc7c9b35d 100644 --- a/include/linux/mfd/macsmc.h +++ b/include/linux/mfd/macsmc.h @@ -223,6 +223,55 @@ int apple_smc_write(struct apple_smc *smc, smc_key key, const void *buf, size_t */ int apple_smc_write_noirq(struct apple_smc *smc, smc_key key, const void *buf, size_t size); +struct device_node; +struct platform_device; + +#define APPLE_SMC_WARM_RESET_WRITES 4 + +/** + * struct apple_smc_warm_reset - a PCIe endpoint's reset through an SMC key + * @smc: the SMC's device (a reference, dropped by apple_smc_warm_reset_put()) + * @key: the key to write + * @count: how many writes + * @writes: per write, the key's value (its bytes from the cell's most significant down) + * and the wait after it in microseconds + * + * Read from a PCIe root port's "apple,function-warm-reset" and "apple,warm-reset-writes" + * (bindings/pci/apple,pcie.yaml). + */ +struct apple_smc_warm_reset { + struct platform_device *smc; + smc_key key; + unsigned int count; + u32 writes[2 * APPLE_SMC_WARM_RESET_WRITES]; +}; + +/** + * apple_smc_warm_reset_parse - read a root port's warm reset + * @np: the root port's node + * @wr: filled in on success + * + * Return: Zero on success, -ENODEV when the port describes none or its SMC has no device, + * -EINVAL when the writes are malformed. + */ +int apple_smc_warm_reset_parse(struct device_node *np, struct apple_smc_warm_reset *wr); + +/** + * apple_smc_warm_reset_put - drop the reference apple_smc_warm_reset_parse() took + * @wr: the parsed reset; cleared + */ +void apple_smc_warm_reset_put(struct apple_smc_warm_reset *wr); + +/** + * apple_smc_warm_reset - give a parsed warm reset + * @wr: the parsed reset + * @noirq: from another device's noirq callbacks: the replies are polled for and the waits + * busy-wait, as for apple_smc_write_noirq() + * + * Return: Zero on success, -ENODEV when the SMC isn't bound, or the first write's error. + */ +int apple_smc_warm_reset(const struct apple_smc_warm_reset *wr, bool noirq); + /** * apple_smc_enter_atomic - Enter atomic mode to be able to use apple_smc_write_atomic * @smc: Pointer to apple_smc struct From 642bc8a67049fe575911b10e2facaeb74112bd46 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 07:36:07 +0300 Subject: [PATCH 07/17] wifi: apple: centauri: wait for the restart's stages, and retry a failed restart Review of the N1 restart: - The restart slept 2 s after the ROM boot and 2 s after the second port cycle. The ROM boot already returns once the image is loaded (stage 4), as the boot step does, and the boot steps run the port cycle right after it, so the first wait goes. After the port cycle the IPC start needs stage 1: poll for it, for at most 5 s, and fail with the stage it read otherwise. - The warm reset's result was dropped. centauri_port_cycle() now takes where to store it, brings the link up either way, and returns the link's result, so the restart can tell a refused reset (link up: try again) from a lost link (nothing more to do). - A failed restart left Wi-Fi down with no further attempt. With the link up, the worker tries again 10 s later, within the same limit of three restarts in 30 minutes. With the link down it says so and stops. - The warm reset is read with the SMC driver's parser, shared with the T6050 host, instead of a copy. Signed-off-by: Nick --- .../net/wireless/apple/centauri/centauri.h | 3 +- .../wireless/apple/centauri/centauri_pci.c | 38 ++++++--- .../wireless/apple/centauri/centauri_port.c | 78 +++++-------------- 3 files changed, 51 insertions(+), 68 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/centauri.h b/drivers/net/wireless/apple/centauri/centauri.h index 167539595226c..e24614ab66aab 100644 --- a/drivers/net/wireless/apple/centauri/centauri.h +++ b/drivers/net/wireless/apple/centauri/centauri.h @@ -41,6 +41,7 @@ struct centauri { unsigned long recover_at[3]; bool removing; bool recover_gave_up; /* restart limit reached: requests are ignored */ + bool restart_failed; /* the last restart failed with the link up: try again */ bool warm_reset; /* the root port describes the N1's warm reset: restart on a stop */ bool inject_stall; /* debugfs test: the next check finds the firmware stopped */ }; @@ -100,7 +101,7 @@ static inline const struct centauri_board *centauri_board(void) return NULL; } -int centauri_port_cycle(struct pci_dev *pdev, bool reset_chip); +int centauri_port_cycle(struct pci_dev *pdev, int *reset_ret); bool centauri_warm_reset_described(struct pci_dev *pdev); int centauri_start_ipc(struct centauri *c); void centauri_free_ipc(struct centauri *c); diff --git a/drivers/net/wireless/apple/centauri/centauri_pci.c b/drivers/net/wireless/apple/centauri/centauri_pci.c index ad3ff5d538d21..332d9040170b7 100644 --- a/drivers/net/wireless/apple/centauri/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/centauri_pci.c @@ -176,7 +176,7 @@ static int centauri_recover(struct centauri *c) struct device *dev = &c->pdev->dev; u32 stage = readl(c->bar + 0x8000); struct pci_dev *beta; - int ret, i; + int ret, reset_ret, i; if (!c->warm_reset) return -EOPNOTSUPP; @@ -206,31 +206,39 @@ static int centauri_recover(struct centauri *c) c->boot_attempted = false; c->image_response = 0; c->link_ready = false; - ret = centauri_port_cycle(c->pdev, true); + ret = centauri_port_cycle(c->pdev, &reset_ret); if (ret) { dev_err(dev, "recovery: reset port cycle failed: %d\n", ret); return ret; } c->link_ready = true; + if (reset_ret) { + dev_err(dev, "recovery: warm reset failed: %d\n", reset_ret); + return reset_ret; + } stage = readl(c->bar + 0x8000); dev_info(dev, "recovery: stage %#x after the warm reset\n", stage); if (stage != 0) return -EIO; + /* Returns once the ROM reports the image loaded (stage 4), as the boot step does. */ ret = centauri_rom_boot(c); if (ret) { dev_err(dev, "recovery: ROM boot failed: %d\n", ret); return ret; } - msleep(2000); c->link_ready = false; - ret = centauri_port_cycle(c->pdev, false); + ret = centauri_port_cycle(c->pdev, NULL); if (ret) { dev_err(dev, "recovery: boot port cycle failed: %d\n", ret); return ret; } c->link_ready = true; - dev_info(dev, "recovery: post-reset execution stage=%#x\n", readl(c->bar + 0x8000)); - msleep(2000); + /* The IPC start needs the image's first stage (1) after the port cycle. */ + ret = readl_poll_timeout(c->bar + 0x8000, stage, stage == 1, 20 * USEC_PER_MSEC, + 5 * USEC_PER_SEC); + dev_info(dev, "recovery: post-reset execution stage=%#x\n", stage); + if (ret) + return ret; ret = centauri_start_ipc(c); dev_info(dev, "recovery: IPC start %d\n", ret); return ret; @@ -261,11 +269,11 @@ static void centauri_recover_worker(struct work_struct *work) int ret; mutex_lock(&c->boot_lock); - if (READ_ONCE(c->removing) || !c->ipc) { + if (READ_ONCE(c->removing) || (!c->ipc && !c->restart_failed)) { mutex_unlock(&c->boot_lock); return; } - if (!xchg(&c->inject_stall, false) && !centauri_wifi_stalled(c)) { + if (!xchg(&c->inject_stall, false) && !c->restart_failed && !centauri_wifi_stalled(c)) { mutex_unlock(&c->boot_lock); dev_info(dev, "firmware fault cleared; no restart\n"); return; @@ -281,9 +289,19 @@ static void centauri_recover_worker(struct work_struct *work) c->recover_at[ARRAY_SIZE(c->recover_at) - 1] = now; dev_warn(dev, "firmware stopped; restarting the N1\n"); ret = centauri_recover(c); + /* With the link up the chip can be reset again; without it nothing more can be done. */ + c->restart_failed = ret && c->link_ready; mutex_unlock(&c->boot_lock); + if (ret && !c->restart_failed) { + dev_err(dev, "restart failed: %d, and the link is down; Wi-Fi stays down until a reboot\n", + ret); + return; + } if (ret) { - dev_err(dev, "restart failed: %d\n", ret); + /* Counts against the same limit: at most three attempts in 30 minutes. */ + dev_err(dev, "restart failed: %d; trying again in 10 s\n", ret); + if (!READ_ONCE(c->removing)) + queue_delayed_work(centauri_wq, &c->recover_work, 10 * HZ); return; } /* @@ -324,7 +342,7 @@ static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, ret = -EBUSY; } else { c->link_ready = false; - ret = centauri_port_cycle(c->pdev, false); + ret = centauri_port_cycle(c->pdev, NULL); if (!ret) { c->link_ready = true; dev_info(&c->pdev->dev, "post-reset execution stage=%#x\n", diff --git a/drivers/net/wireless/apple/centauri/centauri_port.c b/drivers/net/wireless/apple/centauri/centauri_port.c index ba54ac5838dad..55964946895e7 100644 --- a/drivers/net/wireless/apple/centauri/centauri_port.c +++ b/drivers/net/wireless/apple/centauri/centauri_port.c @@ -29,44 +29,18 @@ static bool wait_bits(void __iomem *address, u32 mask, u32 wanted, u32 us) return !readl_poll_timeout(address, value, (value & mask) == wanted, 100, us); } -#define CEN_WARM_WRITES_MAX 4 - /* * The N1's warm reset, as the root port describes it (bindings/pci/apple,pcie.yaml, as on the * T6050 boards): "apple,function-warm-reset" names the SMC and the key (the ADT's pci-bridge0 - * function-warm_reset), "apple,warm-reset-writes" the writes: the key's value, its bytes from - * the cell's most significant down, and the wait after it in microseconds. Reads them into - * @writes and returns how many, with the SMC's device (a reference) in *smc_pdev, or an error. + * function-warm_reset), "apple,warm-reset-writes" the writes. */ -static int warm_reset_parse(struct pci_dev *pdev, struct platform_device **smc_pdev, u32 *key, - u32 writes[2 * CEN_WARM_WRITES_MAX]) +static int warm_reset_parse(struct pci_dev *pdev, struct apple_smc_warm_reset *wr) { struct pci_dev *bridge = pdev->bus->self; - struct device_node *np = bridge ? pci_device_to_OF_node(bridge) : NULL; - struct of_phandle_args fn; - int count, i; - *smc_pdev = NULL; - if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || !np || - of_parse_phandle_with_fixed_args(np, "apple,function-warm-reset", 2, 0, &fn)) - return -ENODEV; - *smc_pdev = of_find_device_by_node(fn.np); - of_node_put(fn.np); - if (!*smc_pdev) + if (!IS_REACHABLE(CONFIG_MFD_MACSMC) || !bridge) return -ENODEV; - *key = fn.args[0]; - count = of_property_count_u32_elems(np, "apple,warm-reset-writes"); - if (count < 2 || count % 2 || count > 2 * CEN_WARM_WRITES_MAX || - of_property_read_u32_array(np, "apple,warm-reset-writes", writes, count)) - goto bad; - for (i = 1; i < count; i += 2) - if (writes[i] > 100 * USEC_PER_MSEC) - goto bad; - return count / 2; -bad: - put_device(&(*smc_pdev)->dev); - *smc_pdev = NULL; - return -EINVAL; + return apple_smc_warm_reset_parse(pci_device_to_OF_node(bridge), wr); } /* @@ -78,15 +52,14 @@ bool centauri_warm_reset_described(struct pci_dev *pdev) { const struct centauri_board *board = centauri_board(); struct pci_dev *bridge = pdev->bus->self; - u32 key, writes[2 * CEN_WARM_WRITES_MAX]; - struct platform_device *smc_pdev; + struct apple_smc_warm_reset wr; if (!board || !board->port || !bridge || bridge->vendor != PCI_VENDOR_ID_APPLE || (board->bridge_id && bridge->device != board->bridge_id)) return false; - if (warm_reset_parse(pdev, &smc_pdev, &key, writes) <= 0) + if (warm_reset_parse(pdev, &wr)) return false; - put_device(&smc_pdev->dev); + apple_smc_warm_reset_put(&wr); return true; } @@ -96,33 +69,24 @@ bool centauri_warm_reset_described(struct pci_dev *pdev) */ static int warm_reset(struct pci_dev *pdev) { - u32 key, writes[2 * CEN_WARM_WRITES_MAX]; - struct platform_device *smc_pdev; - struct apple_smc *smc; - int count, i, ret = 0; + struct apple_smc_warm_reset wr; + int ret; - count = warm_reset_parse(pdev, &smc_pdev, &key, writes); - if (count <= 0) { - ret = count ?: -ENODEV; - goto out; + ret = warm_reset_parse(pdev, &wr); + if (!ret) { + ret = apple_smc_warm_reset(&wr, false); + apple_smc_warm_reset_put(&wr); } - smc = platform_get_drvdata(smc_pdev); - if (!smc) - ret = -ENODEV; - for (i = 0; i < count && !ret; i++) { - __be32 value = cpu_to_be32(writes[2 * i]); - - ret = apple_smc_write(smc, key, &value, sizeof(value)); - if (!ret) - fsleep(writes[2 * i + 1]); - } - put_device(&smc_pdev->dev); -out: dev_info(&pdev->dev, "warm reset through the SMC: %d\n", ret); return ret; } -int centauri_port_cycle(struct pci_dev *pdev, bool reset_chip) +/* + * Take the N1's port down and train it again. With @reset_ret, the N1 is warm reset while + * the port is down and the reset's result is stored there; the link comes up either way. + * Returns the link's result. + */ +int centauri_port_cycle(struct pci_dev *pdev, int *reset_ret) { static const u32 reset[][2] = { {0x88, 0x110}, {0x100, ~0U}, {0x148, ~0U}, {0x210, ~0U}, @@ -237,8 +201,8 @@ int centauri_port_cycle(struct pci_dev *pdev, bool reset_chip) writel(0, port + 0x3800 + 4 * i); for (i = 0; i < ARRAY_SIZE(tune); i++) writel(tune[i][1], port + tune[i][0]); - if (reset_chip) - warm_reset(pdev); + if (reset_ret) + *reset_ret = warm_reset(pdev); update(port, 0x800, 0, 1); udelay(10); update(phy, 0, 0, 1); From 740aba2ae3427ec661a1dbc064847c096e8145bb Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 24 Nov 2026 22:07:23 +0300 Subject: [PATCH 08/17] arm64: dts: apple: t8142-j815: Describe the N1 warm reset, as on J813 The J815 ADT gives pci-bridge0 the same function-warm_reset as J813 (SMC key bpli, argument 0x1000000), so the N1 driver can restart a stopped firmware here too instead of leaving Wi-Fi and Bluetooth down until a reboot. Without these properties centauri_recovery_request() returns early and debugfs rom_boot 4/5 do nothing on this board. Signed-off-by: Nick --- arch/arm64/boot/dts/apple/t8142-j815.dts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/arch/arm64/boot/dts/apple/t8142-j815.dts b/arch/arm64/boot/dts/apple/t8142-j815.dts index 82e73d6dafc2b..ac142ba30a641 100644 --- a/arch/arm64/boot/dts/apple/t8142-j815.dts +++ b/arch/arm64/boot/dts/apple/t8142-j815.dts @@ -268,6 +268,12 @@ #address-cells = <3>; #size-cells = <2>; ranges; + /* + * The N1's warm reset, as on J813: the J815 ADT's pci-bridge0 + * function-warm_reset names the same SMC key 'bpli' and argument. + */ + apple,function-warm-reset = <&smc 0x62706c69 0x1000000>; + apple,warm-reset-writes = <0x00010001 17000>, <0x00010001 20000>; n1_control: control@0,0 { compatible = "apple,t8142-centauri-control", "pci106b,1900"; From 227c89b69b65094ce1354caf79971d069114f877 Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 24 Nov 2026 22:51:48 +0300 Subject: [PATCH 09/17] wifi: apple: centauri: attach Bluetooth once Beta's firmware runs Function 2 (Bluetooth) appears with the IPC start's bus rescan, before Beta's firmware runs, so btcentauri cannot take it then. Only the restart path attached it afterwards; on a cold boot nothing in the kernel did and hci0 came up only when a userspace script reloaded btcentauri (51 s after boot on J815 boot feb7dfb0, against ~15 s for Wi-Fi). After the IPC start step and after a restart, wait up to 10 s for Beta's firmware (exec stage 2, IPC unowned: the state btcentauri checks), then attach the function. The restart path's immediate attach could race the same firmware start; it now uses the same helper. btcentauri's early probe was logged as two errors on every boot; the state is expected, so log it at info level and return -ENXIO, which the driver core does not report as a failed probe. Signed-off-by: Nick --- drivers/bluetooth/btcentauri.c | 13 +++-- .../net/wireless/apple/centauri/centauri.h | 1 + .../wireless/apple/centauri/centauri_ipc.c | 11 ++++ .../wireless/apple/centauri/centauri_pci.c | 57 +++++++++++++++---- 4 files changed, 66 insertions(+), 16 deletions(-) diff --git a/drivers/bluetooth/btcentauri.c b/drivers/bluetooth/btcentauri.c index 7ae66124aa7ce..f51f787753c82 100644 --- a/drivers/bluetooth/btcentauri.c +++ b/drivers/bluetooth/btcentauri.c @@ -1040,10 +1040,15 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) if (ret) return ret; bt->bar = pcim_iomap_table(pdev)[0]; - if (readl(bt->bar + 0x18) != 2 || readl(bt->bar) != 0) - return dev_err_probe(&pdev->dev, -EBUSY, - "Beta requires running firmware and unowned IPC (exec=%u ipc=%u)\n", - readl(bt->bar + 0x18), readl(bt->bar)); + /* + * The function appears with the N1's IPC start, before Beta's firmware runs. + * apple_centauri attaches it again once that firmware is up. + */ + if (readl(bt->bar + 0x18) != 2 || readl(bt->bar) != 0) { + dev_info(&pdev->dev, "waiting for Beta's firmware (exec=%u ipc=%u)\n", + readl(bt->bar + 0x18), readl(bt->bar)); + return -ENXIO; + } ret = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(40)); if (ret) return ret; diff --git a/drivers/net/wireless/apple/centauri/centauri.h b/drivers/net/wireless/apple/centauri/centauri.h index e24614ab66aab..17abbfb8352f2 100644 --- a/drivers/net/wireless/apple/centauri/centauri.h +++ b/drivers/net/wireless/apple/centauri/centauri.h @@ -104,6 +104,7 @@ static inline const struct centauri_board *centauri_board(void) int centauri_port_cycle(struct pci_dev *pdev, int *reset_ret); bool centauri_warm_reset_described(struct pci_dev *pdev); int centauri_start_ipc(struct centauri *c); +bool centauri_beta_running(struct centauri *c); void centauri_free_ipc(struct centauri *c); int centauri_alpha_resources(struct centauri *c, struct cen_alpha_resources *resources); int centauri_alpha_log_buffer(struct centauri *c, void **cpu, dma_addr_t *dma, size_t *size); diff --git a/drivers/net/wireless/apple/centauri/centauri_ipc.c b/drivers/net/wireless/apple/centauri/centauri_ipc.c index a94ff01dc440b..d657e6a4b5374 100644 --- a/drivers/net/wireless/apple/centauri/centauri_ipc.c +++ b/drivers/net/wireless/apple/centauri/centauri_ipc.c @@ -552,6 +552,17 @@ int centauri_start_ipc(struct centauri *c) return ret; } +/* + * Whether Beta's (Bluetooth's) firmware runs with its IPC still unowned: the state + * btcentauri needs to take function 2. Called with the boot lock held. + */ +bool centauri_beta_running(struct centauri *c) +{ + void __iomem *bar = c->ipc ? c->ipc->bar[2] : NULL; + + return bar && readl(bar + 0x18) == 2 && !readl(bar); +} + void centauri_free_ipc(struct centauri *c) { struct centauri_ipc *ipc = c->ipc; diff --git a/drivers/net/wireless/apple/centauri/centauri_pci.c b/drivers/net/wireless/apple/centauri/centauri_pci.c index 332d9040170b7..67d3a7884759f 100644 --- a/drivers/net/wireless/apple/centauri/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/centauri_pci.c @@ -260,11 +260,46 @@ void centauri_recovery_request(struct centauri *c, const char *why) dev_warn(&c->pdev->dev, "firmware fault (%s); checking again in 2 s\n", why); } +static bool centauri_beta_up(struct centauri *c) +{ + bool up; + + mutex_lock(&c->boot_lock); + up = READ_ONCE(c->removing) || centauri_beta_running(c); + mutex_unlock(&c->boot_lock); + return up; +} + +/* + * The IPC start's rescan adds the Beta (Bluetooth) function before Beta's firmware runs, + * so btcentauri cannot take it then. Wait for that firmware and attach the function. + * Called without the boot lock and outside the PCI rescan lock. Returns 1 when + * function 2 has a driver, 0 when none took it, or a negative error. + */ +static int centauri_attach_beta(struct centauri *c) +{ + struct pci_dev *beta; + bool up; + int ret; + + beta = pci_get_slot(c->pdev->bus, PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 2)); + if (!beta) + return -ENODEV; + ret = 1; + if (!READ_ONCE(beta->dev.driver)) { + ret = read_poll_timeout(centauri_beta_up, up, up, 20 * USEC_PER_MSEC, + 10 * USEC_PER_SEC, false, c); + if (!ret) + ret = READ_ONCE(c->removing) ? -ENODEV : device_attach(&beta->dev); + } + pci_dev_put(beta); + return ret; +} + static void centauri_recover_worker(struct work_struct *work) { struct centauri *c = container_of(to_delayed_work(work), struct centauri, recover_work); struct device *dev = &c->pdev->dev; - struct pci_dev *beta; unsigned long now = jiffies; int ret; @@ -304,17 +339,7 @@ static void centauri_recover_worker(struct work_struct *work) queue_delayed_work(centauri_wq, &c->recover_work, 10 * HZ); return; } - /* - * The IPC start's rescan added the Beta function before its firmware ran, so - * btcentauri refused it then. Attach it now, outside the rescan lock. - */ - beta = pci_get_slot(c->pdev->bus, PCI_DEVFN(PCI_SLOT(c->pdev->devfn), 2)); - if (beta && !beta->dev.driver) - dev_info(dev, "restarted; Bluetooth function attach: %d\n", - device_attach(&beta->dev)); - else - dev_info(dev, "restarted\n"); - pci_dev_put(beta); + dev_info(dev, "restarted; Bluetooth function attach: %d\n", centauri_attach_beta(c)); } static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, @@ -350,6 +375,14 @@ static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, } } mutex_unlock(&c->boot_lock); + if (value == 3 && !ret) { + int attached = centauri_attach_beta(c); + + if (attached == 1) + dev_info(&c->pdev->dev, "Bluetooth function attached\n"); + else + dev_warn(&c->pdev->dev, "Bluetooth function not attached: %d\n", attached); + } return ret ?: count; } From 1a272cebf27410bf936f20d80115ef82b0d14d0b Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 07:38:14 +0300 Subject: [PATCH 10/17] Bluetooth: btcentauri: Deliver ACL data by default The T8142 Beta transport already carries ACL data: BR/EDR on rings 2 and 8, LE on rings 4 and 10. Delivery was opt-in (enable_acl), and it needed arm_auxiliary_rings, which opens every auxiliary ring and allocates a 1 MiB private capture of what they receive. Without both, hci0 comes up and scans, but no device can connect: pairing, HID and audio all need ACL. Turn ACL delivery on by default and arm only the two ACL rings for it. The other auxiliary rings and the capture move behind a new diagnostic_capture parameter; the frames on them are not delivered either way. enable_acl=0 still gives the command and event transport alone. Signed-off-by: Nick --- drivers/bluetooth/btcentauri.c | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/drivers/bluetooth/btcentauri.c b/drivers/bluetooth/btcentauri.c index 7ae66124aa7ce..c33b8a616e87d 100644 --- a/drivers/bluetooth/btcentauri.c +++ b/drivers/bluetooth/btcentauri.c @@ -49,9 +49,13 @@ static bool arm_le_data_rings = true; module_param(arm_le_data_rings, bool, 0444); MODULE_PARM_DESC(arm_le_data_rings, "Arm native Beta LE rings for radio bring-up"); -static bool arm_auxiliary_rings; +static bool arm_auxiliary_rings = true; module_param(arm_auxiliary_rings, bool, 0444); -MODULE_PARM_DESC(arm_auxiliary_rings, "Diagnostic: arm remaining native Beta resources and capture received frames privately"); +MODULE_PARM_DESC(arm_auxiliary_rings, "Arm Beta's ACL data rings"); + +static bool diagnostic_capture; +module_param(diagnostic_capture, bool, 0444); +MODULE_PARM_DESC(diagnostic_capture, "Diagnostic: arm every auxiliary ring and capture received frames privately; requires the auxiliary rings"); static bool native_startup; module_param(native_startup, bool, 0444); @@ -73,7 +77,7 @@ static bool accept_list; module_param(accept_list, bool, 0444); MODULE_PARM_DESC(accept_list, "Let the controller filter LE scans by its accept list (off: report its size as 0, the host filters)"); -static bool enable_acl; +static bool enable_acl = true; module_param(enable_acl, bool, 0444); MODULE_PARM_DESC(enable_acl, "Experimental ACL data delivery; requires LE and auxiliary rings"); @@ -90,8 +94,8 @@ struct cen_aux_spec { bool receive; }; -/* Host native Beta inventory; each resource is opt-in until target-qualified. - * HCI and LE rings remain in their existing, separately qualified arenas. +/* Beta's auxiliary rings. Only the ACL rings (2 and 8) are armed by default; the + * others only with diagnostic_capture. HCI and LE rings have their own arenas. */ static const struct cen_aux_spec cen_aux_specs[] = { { 2, 9, 2592, false }, /* ACL TX */ @@ -110,6 +114,7 @@ static const struct cen_aux_spec cen_aux_specs[] = { struct cen_aux_state { u32 offset, packets; u16 head, post; + bool opened; }; struct cen_bt { @@ -318,11 +323,14 @@ static int cen_aux_start(struct cen_bt *bt) return -ENOSPC; state->offset = offset; offset = ALIGN(offset + length, SZ_4K); + if (!diagnostic_capture && spec->id != 2 && spec->id != 8) + continue; ret = cen_open_ring(bt, spec->id, spec->slots, state->offset, spec->footer); if (ret) { dev_err(&bt->pdev->dev, "Auxiliary ring %u open failed: %d\n", spec->id, ret); return ret; } + state->opened = true; if (!spec->receive) continue; for (slot = 0; slot < spec->slots - 1; slot++) @@ -343,7 +351,7 @@ static int cen_aux_drain(struct cen_bt *bt, bool repost) u16 tail; int budget = spec->slots; - if (!spec->receive) + if (!state->opened || !spec->receive) continue; tail = cen_tail(bt, spec->id); if (tail >= spec->slots) @@ -951,7 +959,7 @@ static int cen_status_show(struct seq_file *s, void *unused) bt->capture_used, bt->capture_records, bt->capture_dropped); if (arm_auxiliary_rings) for (i = 0; i < ARRAY_SIZE(cen_aux_specs); i++) - if (cen_aux_specs[i].receive) + if (cen_aux_specs[i].receive && bt->aux[i].opened) seq_printf(s, "aux_rx ring=%u packets=%u consumed=%u device_tail=%u\n", cen_aux_specs[i].id, bt->aux[i].packets, bt->aux[i].head, cen_tail(bt, cen_aux_specs[i].id)); @@ -1028,6 +1036,8 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) skb_queue_head_init(&bt->le_txq); if (enable_acl && (!arm_le_data_rings || !arm_auxiliary_rings)) return -EINVAL; + if (diagnostic_capture && !arm_auxiliary_rings) + return -EINVAL; if (native_hrb && (!native_startup || !enable_acl)) return -EINVAL; INIT_DELAYED_WORK(&bt->poll, cen_poll); @@ -1051,7 +1061,7 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) if (!bt->arena) return -ENOMEM; memset(bt->arena, 0, CEN_ARENA_SIZE); - if (arm_auxiliary_rings) { + if (diagnostic_capture) { bt->capture = kvzalloc(CEN_CAPTURE_SIZE, GFP_KERNEL); if (!bt->capture) { ret = -ENOMEM; From 822a1dd7369c6594f05fe3f31bb43385ba721727 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 08:56:56 +0300 Subject: [PATCH 11/17] selftests: apple: centauri-scan: stub centauri_recovery_request The scan worker now asks for a recovery when Alpha leaves its running stage (from main). The host model of the worker has no recovery path, so give it a counting stub and keep the scan tests building. Signed-off-by: Nick --- tools/asahi/centauri-scan-tests.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/asahi/centauri-scan-tests.py b/tools/asahi/centauri-scan-tests.py index cb41035cf500b..a095cb79c2b05 100755 --- a/tools/asahi/centauri-scan-tests.py +++ b/tools/asahi/centauri-scan-tests.py @@ -394,6 +394,11 @@ def extract_fn(source, name): static void mutex_lock(int *l) { assert(!*l); *l = 1; } static void mutex_unlock(int *l) { assert(*l); *l = 0; } static u32 readl(void *addr) { (void)addr; return bar_state; } +static unsigned int recovery_requests; +static void centauri_recovery_request(struct centauri *c, const char *why) +{ + (void)c; (void)why; recovery_requests++; +} static size_t build_scan(struct centauri_wifi *w, unsigned int c, u8 *b) { (void)w; (void)c; (void)b; From f63bd689379531c9753d65634ccb7ccd45d85724 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 10:04:55 +0300 Subject: [PATCH 12/17] wifi: apple: centauri: Send the Alpha sleep handshake by default Alpha's host-power and ACIPC sleep handshake ran only with apple_centauri.alpha_sleep_untested=1. Without it the N1 firmware is not told the host goes to sleep, and a deep sleep is aborted and the Mac woken about a minute in. The handshake runs through full deep sleeps and wakes on J813 and J815 (MacBook Air 13" and 15" M5). Default it on, for a deep sleep only: across s2idle the firmware keeps running and nothing is sent, as before. alpha_sleep_untested=0 skips it. Signed-off-by: Nick --- drivers/net/wireless/apple/centauri/centauri_wifi.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index 3916292f3133b..5c01a1510f353 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -85,9 +86,14 @@ static bool station_link_addresses; module_param(station_link_addresses, bool, 0400); MODULE_PARM_DESC(station_link_addresses, "Diagnostic: use the netdev address for each firmware link in this single-link WPA2 client"); -static bool alpha_sleep_untested; +/* + * Alpha's host-power and ACIPC sleep handshake, for a deep sleep. Without it the firmware never + * hears the host is going to sleep and aborts the deep sleep about a minute in; it runs through + * full deep sleeps and wakes on J813 and J815 (MacBook Air 13" and 15" M5). =0 skips it. + */ +static bool alpha_sleep_untested = true; module_param(alpha_sleep_untested, bool, 0600); -MODULE_PARM_DESC(alpha_sleep_untested, "TEST ONLY: Alpha host-power and ACIPC sleep handshake"); +MODULE_PARM_DESC(alpha_sleep_untested, "Alpha host-power and ACIPC sleep handshake (default 1)"); struct centauri_rx_snapshot { __le32 version, count; @@ -2728,7 +2734,8 @@ int centauri_wifi_suspend(struct centauri *c) struct centauri_wifi *wifi = c->wifi; int ret, restore; - if (!wifi || !READ_ONCE(alpha_sleep_untested)) + /* Only for a deep sleep: across s2idle the firmware keeps running as before. */ + if (!wifi || !READ_ONCE(alpha_sleep_untested) || !pm_suspend_via_firmware()) return 0; mutex_lock(&wifi->lock); if (wifi->transport_paused || wifi->connected || wifi->connecting || wifi->scan_request || From c86b8fa3e41b67787456be1b1341692235fdcde4 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 23:01:14 +0700 Subject: [PATCH 13/17] Bluetooth: btcentauri: SCO and LE Audio, wideband speech, factory address The J815 test tree's Bluetooth work, in one commit for testing: - SCO and LE Audio ISO data rings, and a command timeout handler; - wideband speech advertised (mSBC/LC3 over the SCO rings); - hdev->set_bdaddr through the vendor command 0xfc06; - the Beta rings are polled again instead of PCI MSI vectors (the MSI is a firmware-programmed DMA mailbox, not a PCI vector); - an RX cookie leak and a lock scope fixed, and the scan duplicate filter falls back when the controller refuses it; - a diagnostic_capture parameter for bring-up captures. Signed-off-by: Nick --- drivers/bluetooth/btcentauri.c | 407 +++++++++++++++++++++++++-------- 1 file changed, 310 insertions(+), 97 deletions(-) diff --git a/drivers/bluetooth/btcentauri.c b/drivers/bluetooth/btcentauri.c index fb827b10a2a9f..2df19da65aee9 100644 --- a/drivers/bluetooth/btcentauri.c +++ b/drivers/bluetooth/btcentauri.c @@ -1,16 +1,14 @@ // SPDX-License-Identifier: GPL-2.0-only -/* Apple N1 Beta command/event transport for J813 bring-up. +/* Apple N1 Beta HCI transport driver. * - * The Centauri Control driver must have booted the shared firmware first. - * This driver owns only Beta. It never resets the port or the Wi-Fi function. - * BR/EDR discovery, ACL/SDP, pairing and encrypted AVDTP discovery are qualified. - * ACL remains opt-in; LE data, persistent bonds and audio are unqualified. - * SCO/ISO delivery is not implemented. + * The Centauri Control driver boots the shared firmware first. This driver + * owns only the Beta (Bluetooth) PCI function and its ACIPC rings. */ #include #include #include #include +#include #include #include #include @@ -47,7 +45,7 @@ static bool arm_le_data_rings = true; module_param(arm_le_data_rings, bool, 0444); -MODULE_PARM_DESC(arm_le_data_rings, "Arm native Beta LE rings for radio bring-up"); +MODULE_PARM_DESC(arm_le_data_rings, "Arm Beta LE data rings"); static bool arm_auxiliary_rings = true; module_param(arm_auxiliary_rings, bool, 0444); @@ -59,7 +57,7 @@ MODULE_PARM_DESC(diagnostic_capture, "Diagnostic: arm every auxiliary ring and c static bool native_startup; module_param(native_startup, bool, 0444); -MODULE_PARM_DESC(native_startup, "Diagnostic: program a temporary address, InitDone and random seed before the first HCI reset"); +MODULE_PARM_DESC(native_startup, "Diagnostic: program a temporary locally administered address when no factory BD_ADDR is present"); static bool factory_address = true; module_param(factory_address, bool, 0444); @@ -69,9 +67,9 @@ static bool fixup_adv_phy = true; module_param(fixup_adv_phy, bool, 0444); MODULE_PARM_DESC(fixup_adv_phy, "Mask the firmware's extra bits in LE extended advertising report PHYs"); -static bool scan_dup_filter; +static bool scan_dup_filter = true; module_param(scan_dup_filter, bool, 0444); -MODULE_PARM_DESC(scan_dup_filter, "Let the controller drop duplicate LE advertising reports (off: every report reaches the host)"); +MODULE_PARM_DESC(scan_dup_filter, "Allow controller LE duplicate filtering (uses vendor duplicate-filter reset on scan enable)"); static bool accept_list; module_param(accept_list, bool, 0444); @@ -79,14 +77,15 @@ MODULE_PARM_DESC(accept_list, "Let the controller filter LE scans by its accept static bool enable_acl = true; module_param(enable_acl, bool, 0444); -MODULE_PARM_DESC(enable_acl, "Experimental ACL data delivery; requires LE and auxiliary rings"); +MODULE_PARM_DESC(enable_acl, "Enable BR/EDR and LE ACL, SCO, and LE Audio ISO data delivery; requires LE and auxiliary rings"); -static bool native_hrb; +static bool native_hrb = true; module_param(native_hrb, bool, 0444); -MODULE_PARM_DESC(native_hrb, "Experimental native Proxima Create Connection; requires native startup, ACL and firmware capability bit 9"); +MODULE_PARM_DESC(native_hrb, "Translate BR/EDR Create Connection to the firmware's extended connection command when advertised"); #define CEN_OP_READ_PROP_FEATURES 0xffa3 #define CEN_OP_HRB_CREATE_CONN 0xff7b +#define CEN_OP_FLUSH_DUPLICATES 0xff3f #define CEN_PROP_HRB_CREATE BIT(9) struct cen_aux_spec { @@ -127,8 +126,8 @@ struct cen_bt { struct mutex lock; struct delayed_work poll; struct sk_buff_head txq; - struct sk_buff_head acl_txq, le_txq; - u16 acl_tx_head, le_tx_head; + struct sk_buff_head acl_txq, le_txq, sco_txq, iso_txq; + u16 acl_tx_head, le_tx_head, sco_tx_head, iso_tx_head; u16 message_head, message_tag, tx_head, rx_head, rx_post; u16 le_rx_head, le_rx_post; u32 le_rx_packets; @@ -142,6 +141,7 @@ struct cen_bt { u32 hrb_connection_events; u8 hrb_last_link_type; u32 scan_dup_rewritten, accept_list_hidden; + bool dup_filter_fallback; bool transport_paused, transport_sleep_sent; /* MSI backed by a DMA buffer, outside the PCI core's IRQ allocation. */ struct { @@ -262,40 +262,142 @@ static void cen_capture_frame(struct cen_bt *bt, u16 ring, const u8 *data, u16 l bt->capture_used += 8 + length; } -static int cen_receive_acl(struct cen_bt *bt, const u8 *data, u32 length) +static void cen_skip_rx_attrs(struct cen_bt *bt, const char *kind, + const u8 *data, u32 offset, u32 length) { - struct sk_buff *skb; - u32 packet_length, pos; + u32 pos; - if (length < HCI_ACL_HDR_SIZE) - return -EPROTO; - packet_length = HCI_ACL_HDR_SIZE + get_unaligned_le16(data + 2); - if (packet_length > length) - return -EPROTO; - /* Airship RX attributes follow, rather than belong to, the HCI packet. - * Only the native parser's known record sizes are accepted. Their values - * remain opaque and are retained only in the private diagnostic capture. - */ - for (pos = packet_length; pos < length;) { + /* Trailing RX metadata attributes follow the HCI data payload. */ + for (pos = offset; pos < length;) { if (!data[pos]) break; if (data[pos] == 1) pos++; else if (data[pos] == 2 && length - pos >= 9) pos += 9; - else - return -EPROTO; + else { + dev_warn_once(&bt->pdev->dev, + "Ignoring unknown %s RX trailing attribute %#x (remaining=%u)\n", + kind, data[pos], length - pos); + break; + } + } +} + +static void cen_receive_acl(struct cen_bt *bt, const u8 *data, u32 length) +{ + struct sk_buff *skb; + u32 packet_length; + + if (length < HCI_ACL_HDR_SIZE) { + bt->hdev->stat.err_rx++; + dev_warn_ratelimited(&bt->pdev->dev, + "Dropping truncated ACL frame (len=%u)\n", + length); + return; } + packet_length = HCI_ACL_HDR_SIZE + get_unaligned_le16(data + 2); + if (packet_length > length) { + bt->hdev->stat.err_rx++; + dev_warn_ratelimited(&bt->pdev->dev, + "Dropping malformed ACL frame (hdr_len=%u ring_len=%u)\n", + packet_length, length); + return; + } + cen_skip_rx_attrs(bt, "ACL", data, packet_length, length); skb = bt_skb_alloc(packet_length, GFP_KERNEL); if (!skb) { bt->hdev->stat.err_rx++; - return 0; + return; } skb_put_data(skb, data, packet_length); hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT; bt->hdev->stat.byte_rx += packet_length; hci_recv_frame(bt->hdev, skb); - return 0; +} + +static void cen_receive_sco(struct cen_bt *bt, const u8 *data, u32 length) +{ + struct sk_buff *skb; + u32 packet_length; + + if (length < HCI_SCO_HDR_SIZE) { + bt->hdev->stat.err_rx++; + dev_warn_ratelimited(&bt->pdev->dev, + "Dropping truncated SCO frame (len=%u)\n", + length); + return; + } + packet_length = HCI_SCO_HDR_SIZE + data[2]; + if (packet_length > length) { + bt->hdev->stat.err_rx++; + dev_warn_ratelimited(&bt->pdev->dev, + "Dropping malformed SCO frame (hdr_len=%u ring_len=%u)\n", + packet_length, length); + return; + } + cen_skip_rx_attrs(bt, "SCO", data, packet_length, length); + skb = bt_skb_alloc(packet_length, GFP_KERNEL); + if (!skb) { + bt->hdev->stat.err_rx++; + return; + } + skb_put_data(skb, data, packet_length); + hci_skb_pkt_type(skb) = HCI_SCODATA_PKT; + bt->hdev->stat.byte_rx += packet_length; + hci_recv_frame(bt->hdev, skb); +} + +static void cen_receive_iso(struct cen_bt *bt, const u8 *data, u32 length) +{ + struct sk_buff *skb; + u32 packet_length; + + /* Ring 11 prefixes each payload with an H4 packet indicator byte. */ + if (length < 1) { + bt->hdev->stat.err_rx++; + dev_warn_ratelimited(&bt->pdev->dev, + "Dropping empty ISO ring frame\n"); + return; + } + if (data[0] == HCI_ACLDATA_PKT) { + cen_receive_acl(bt, data + 1, length - 1); + return; + } + if (data[0] != HCI_ISODATA_PKT) { + bt->hdev->stat.err_rx++; + dev_warn_ratelimited(&bt->pdev->dev, + "Dropping unexpected ISO ring packet type %#x (len=%u)\n", + data[0], length); + return; + } + data++; + length--; + if (length < HCI_ISO_HDR_SIZE) { + bt->hdev->stat.err_rx++; + dev_warn_ratelimited(&bt->pdev->dev, + "Dropping truncated ISO frame (len=%u)\n", + length); + return; + } + packet_length = HCI_ISO_HDR_SIZE + (get_unaligned_le16(data + 2) & 0x3fff); + if (packet_length > length) { + bt->hdev->stat.err_rx++; + dev_warn_ratelimited(&bt->pdev->dev, + "Dropping malformed ISO frame (hdr_len=%u ring_len=%u)\n", + packet_length, length); + return; + } + cen_skip_rx_attrs(bt, "ISO", data, packet_length, length); + skb = bt_skb_alloc(packet_length, GFP_KERNEL); + if (!skb) { + bt->hdev->stat.err_rx++; + return; + } + skb_put_data(skb, data, packet_length); + hci_skb_pkt_type(skb) = HCI_ISODATA_PKT; + bt->hdev->stat.byte_rx += packet_length; + hci_recv_frame(bt->hdev, skb); } static void cen_aux_rx_buffer(struct cen_bt *bt, unsigned int index, u16 slot) @@ -308,6 +410,11 @@ static void cen_aux_rx_buffer(struct cen_bt *bt, unsigned int index, u16 slot) put_unaligned_le16(slot + 1, descriptor + 12); } +static bool cen_aux_required(u16 id) +{ + return id == 2 || id == 3 || id == 5 || id == 8 || id == 9 || id == 11; +} + static int cen_aux_start(struct cen_bt *bt) { u32 offset = CEN_AUX_BASE; @@ -323,7 +430,7 @@ static int cen_aux_start(struct cen_bt *bt) return -ENOSPC; state->offset = offset; offset = ALIGN(offset + length, SZ_4K); - if (!diagnostic_capture && spec->id != 2 && spec->id != 8) + if (!diagnostic_capture && !cen_aux_required(spec->id)) continue; ret = cen_open_ring(bt, spec->id, spec->slots, state->offset, spec->footer); if (ret) { @@ -365,9 +472,14 @@ static int cen_aux_drain(struct cen_bt *bt, bool repost) if (length > spec->footer) return -EPROTO; cen_capture_frame(bt, spec->id, descriptor + 16, length); - if (repost && enable_acl && spec->id == 8 && - cen_receive_acl(bt, descriptor + 16, length)) - return -EPROTO; + if (repost && enable_acl) { + if (spec->id == 8) + cen_receive_acl(bt, descriptor + 16, length); + else if (spec->id == 9) + cen_receive_sco(bt, descriptor + 16, length); + else if (spec->id == 11) + cen_receive_iso(bt, descriptor + 16, length); + } state->packets++; state->head = (state->head + 1) % spec->slots; if (repost) { @@ -398,7 +510,7 @@ static int cen_start(struct cen_bt *bt) put_unaligned_le64(bt->dma + CEN_MTR, bt->arena + 0x3c); put_unaligned_le16(16, bt->arena + 0x44); put_unaligned_le16(16, bt->arena + 0x46); - /* The 25G76 native context encodes the MCR footer in four-byte words. */ + /* MCR footer length is encoded in 4-byte words. */ bt->arena[0x53] = 4; put_unaligned_le64(bt->dma + 0x10000, bt->arena + 0x58); pci_set_master(bt->pdev); @@ -465,16 +577,23 @@ static int cen_transmit(struct cen_bt *bt, struct sk_buff_head *queue, descriptor = bt->arena + offset + *head * (16 + footer); memset(descriptor, 0, 16 + footer); put_unaligned_le16(*head + 1, descriptor + 12); - memcpy(descriptor + 16, skb->data, skb->len); - wire_length = skb->len; + if (ring == 5) { + descriptor[16] = HCI_ISODATA_PKT; + memcpy(descriptor + 17, skb->data, skb->len); + wire_length = skb->len + 1; + } else { + memcpy(descriptor + 16, skb->data, skb->len); + wire_length = skb->len; + } + if (ring == 1 && get_unaligned_le16(skb->data) == HCI_OP_RESET) + bt->hrb_status_pending = false; if (ring == 1 && READ_ONCE(bt->hrb_supported) && get_unaligned_le16(skb->data) == HCI_OP_CREATE_CONN) { u8 *command = descriptor + 16; - /* Native Proxima uses the ordinary 13-byte parameter layout - * followed by two zero HRB fields (no forced band). It sends - * tagging=false and role_switch=0. Address byte order matches - * standard HCI. Keep the core's sent skb/opcode unchanged. + /* Extended Create Connection extends the 13-byte HCI + * parameter payload with two zero bytes while leaving + * the core's skb opcode unchanged. */ put_unaligned_le16(CEN_OP_HRB_CREATE_CONN, command); command[2] = 15; @@ -490,8 +609,10 @@ static int cen_transmit(struct cen_bt *bt, struct sk_buff_head *queue, cen_post(bt, ring, next); if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT) bt->hdev->stat.cmd_tx++; - else + else if (hci_skb_pkt_type(skb) == HCI_ACLDATA_PKT) bt->hdev->stat.acl_tx++; + else if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT) + bt->hdev->stat.sco_tx++; bt->hdev->stat.byte_tx += wire_length; kfree_skb(skb); } @@ -555,7 +676,7 @@ static void cen_poll(struct work_struct *work) int budget = CEN_RX_SLOTS; mutex_lock(&bt->lock); - if (!bt->running || bt->transport_paused) + if (!bt->running || READ_ONCE(bt->transport_paused)) goto unlock; if (readl(bt->bar + 0x18) != 2 || readl(bt->bar) != 2) { /* Preserve already completed diagnostics after abort, without posting @@ -579,8 +700,8 @@ static void cen_poll(struct work_struct *work) goto protocol_error; bt->le_rx_packets++; cen_capture_frame(bt, 10, descriptor + 16, length); - if (enable_acl && cen_receive_acl(bt, descriptor + 16, length)) - goto protocol_error; + if (enable_acl) + cen_receive_acl(bt, descriptor + 16, length); bt->le_rx_head = (bt->le_rx_head + 1) % CEN_LE_SLOTS; cen_le_rx_buffer(bt, bt->le_rx_post); bt->le_rx_post = (bt->le_rx_post + 1) % CEN_LE_SLOTS; @@ -602,8 +723,8 @@ static void cen_poll(struct work_struct *work) if (skb) { skb_put_data(skb, descriptor + 16, length); /* The firmware's status belongs to the standard connection - * request tracked by Linux. Capture the native response before - * restoring only its opcode; preserve status and command credit. + * request tracked by Linux. Restore only its opcode while + * preserving status and command credit. */ if (bt->hrb_status_pending && length == 6 && skb->data[0] == HCI_EV_CMD_STATUS && @@ -613,10 +734,8 @@ static void cen_poll(struct work_struct *work) bt->hrb_status_pending = false; bt->hrb_status_events++; } - /* Native HCI flow treats 0xf0/0xf1 Connection Complete as - * ACL, with additional proprietary capability flags. Linux - * otherwise cannot find its pending ACL connection by type. - * Preserve the original event privately before normalizing it. + /* Normalize vendor 0xf0/0xf1 Connection Complete link types + * to standard ACL_LINK so HCI matches the pending connection. */ if (READ_ONCE(bt->hrb_supported) && length == HCI_EVENT_HDR_SIZE + sizeof(struct hci_ev_conn_complete) && @@ -643,20 +762,34 @@ static void cen_poll(struct work_struct *work) if (cen_transmit(bt, &bt->txq, 1, CEN_TX_SLOTS, CEN_TX, CEN_FOOTER, &bt->tx_head)) goto protocol_error; - /* cen_aux_specs[0] is the native BR/EDR ACL TX resource (ring 2). */ + /* cen_aux_specs[0/1/2] are BR/EDR ACL (2), SCO (3), and ISO (5) TX rings. */ if (enable_acl && - (cen_transmit(bt, &bt->acl_txq, 2, cen_aux_specs[0].slots, - bt->aux[0].offset, cen_aux_specs[0].footer, - &bt->acl_tx_head) || + ((bt->aux[0].opened && + cen_transmit(bt, &bt->acl_txq, 2, cen_aux_specs[0].slots, + bt->aux[0].offset, cen_aux_specs[0].footer, + &bt->acl_tx_head)) || cen_transmit(bt, &bt->le_txq, 4, CEN_LE_SLOTS, CEN_LE_TX, - CEN_LE_FOOTER, &bt->le_tx_head))) + CEN_LE_FOOTER, &bt->le_tx_head) || + (bt->aux[1].opened && + cen_transmit(bt, &bt->sco_txq, 3, cen_aux_specs[1].slots, + bt->aux[1].offset, cen_aux_specs[1].footer, + &bt->sco_tx_head)) || + (bt->aux[2].opened && + cen_transmit(bt, &bt->iso_txq, 5, cen_aux_specs[2].slots, + bt->aux[2].offset, cen_aux_specs[2].footer, + &bt->iso_tx_head)))) goto protocol_error; - schedule_delayed_work(&bt->poll, msecs_to_jiffies(1)); + /* + * Beta's MSI is a DMA-backed mailbox the firmware programs itself, not a + * vector the PCI core hands out (see cen_resume_noirq()), so the rings + * are polled. + */ + mod_delayed_work(system_percpu_wq, &bt->poll, msecs_to_jiffies(1)); goto unlock; protocol_error: bt->running = false; hdev->stat.err_rx++; - dev_err(&bt->pdev->dev, "Beta transport stopped (exec=%u ipc=%u); polling disabled\n", + dev_err(&bt->pdev->dev, "Beta transport stopped (exec=%u ipc=%u)\n", readl(bt->bar + 0x18), readl(bt->bar)); unlock: mutex_unlock(&bt->lock); @@ -668,7 +801,8 @@ static int cen_hci_open(struct hci_dev *hdev) mutex_lock(&bt->lock); bt->running = true; - schedule_delayed_work(&bt->poll, 0); + if (!READ_ONCE(bt->transport_paused)) + mod_delayed_work(system_percpu_wq, &bt->poll, 0); mutex_unlock(&bt->lock); return 0; } @@ -685,9 +819,37 @@ static int cen_hci_close(struct hci_dev *hdev) skb_queue_purge(&bt->txq); skb_queue_purge(&bt->acl_txq); skb_queue_purge(&bt->le_txq); + skb_queue_purge(&bt->sco_txq); + skb_queue_purge(&bt->iso_txq); return 0; } +static void cen_hci_reset(struct hci_dev *hdev) +{ + struct cen_bt *bt = hci_get_drvdata(hdev); + bool paused; + u32 exec, ipc; + + mutex_lock(&bt->lock); + bt->hrb_status_pending = false; + paused = READ_ONCE(bt->transport_paused); + if (!paused && bt->bar && bt->arena) { + exec = readl(bt->bar + 0x18); + ipc = readl(bt->bar); + bt_dev_err(hdev, + "HCI command timeout (exec=%u ipc=%u running=%u tx=%u/%u rx=%u/%u)", + exec, ipc, bt->running, bt->tx_head, cen_tail(bt, 1), + bt->rx_head, cen_tail(bt, 7)); + } else { + bt_dev_err(hdev, + "HCI command timeout while transport unavailable (running=%u paused=%u)", + bt->running, paused); + } + if (bt->running && !paused) + mod_delayed_work(system_percpu_wq, &bt->poll, 0); + mutex_unlock(&bt->lock); +} + static int cen_vendor_command(struct hci_dev *hdev, u16 opcode, const void *data, u8 length) { @@ -702,6 +864,25 @@ static int cen_vendor_command(struct hci_dev *hdev, u16 opcode, return ret; } +static int cen_hci_reset_dup_filter(struct hci_dev *hdev) +{ + struct cen_bt *bt = hci_get_drvdata(hdev); + static const u8 flags; + int ret; + + if (READ_ONCE(bt->dup_filter_fallback)) + return 0; + ret = __hci_cmd_sync_status(hdev, CEN_OP_FLUSH_DUPLICATES, + sizeof(flags), &flags, HCI_CMD_TIMEOUT); + if (ret) { + bt_dev_warn(hdev, + "Duplicate filter reset failed (%d); falling back to host filtering", + ret); + WRITE_ONCE(bt->dup_filter_fallback, true); + } + return 0; +} + static int cen_hci_post_init(struct hci_dev *hdev) { struct cen_bt *bt = hci_get_drvdata(hdev); @@ -720,12 +901,15 @@ static int cen_hci_post_init(struct hci_dev *hdev) features = skb->len == 5 ? get_unaligned_le32(skb->data + 1) : 0; kfree_skb(skb); WRITE_ONCE(bt->proprietary_features, features); - if (ret) - return ret; - if (!(features & CEN_PROP_HRB_CREATE)) - return -EOPNOTSUPP; + if (!ret && !(features & CEN_PROP_HRB_CREATE)) + ret = -EOPNOTSUPP; + if (ret) { + bt_dev_warn(hdev, "Firmware connection command unavailable: %d", + ret); + return 0; + } WRITE_ONCE(bt->hrb_supported, true); - bt_dev_info(hdev, "Native Proxima Create Connection capability confirmed"); + bt_dev_dbg(hdev, "Extended Create Connection capability confirmed"); return 0; } @@ -751,6 +935,18 @@ static int cen_bt_address(struct device *dev, u8 address[6]) return 0; } +static int cen_hci_set_bdaddr(struct hci_dev *hdev, const bdaddr_t *bdaddr) +{ + u8 address[6]; + int i; + + for (i = 0; i < 6; i++) + address[i] = bdaddr->b[5 - i]; + if (is_zero_ether_addr(address) || is_broadcast_ether_addr(address)) + return -EINVAL; + return cen_vendor_command(hdev, 0xfc06, address, sizeof(address)); +} + static int cen_hci_setup(struct hci_dev *hdev) { struct cen_bt *bt = hci_get_drvdata(hdev); @@ -768,11 +964,8 @@ static int cen_hci_setup(struct hci_dev *hdev) if (!factory && !native_startup) return 0; - /* Native PROXIMA initialization starts with these three commands, then - * HCI Reset. The core calls setup before its ordinary reset/init sequence. - * The address is the factory one when the loader provided it, else - * (diagnostic) a temporary locally administered address, in native - * WriteBdAddr display/network byte order. + /* Controller initialization runs WriteBdAddr, InitDone, and RNG seed + * before the core's standard HCI Reset sequence. */ if (!factory) { ret = get_random_bytes_wait(address, sizeof(address)); @@ -811,6 +1004,8 @@ static int cen_hci_flush(struct hci_dev *hdev) skb_queue_purge(&bt->txq); skb_queue_purge(&bt->acl_txq); skb_queue_purge(&bt->le_txq); + skb_queue_purge(&bt->sco_txq); + skb_queue_purge(&bt->iso_txq); return 0; } @@ -838,7 +1033,7 @@ static int cen_hci_send(struct hci_dev *hdev, struct sk_buff *skb) queue = &bt->le_txq; if (skb->len > CEN_LE_FOOTER) ret = -EMSGSIZE; - } else if (link_type == ACL_LINK) { + } else if (link_type == ACL_LINK && bt->aux[0].opened) { queue = &bt->acl_txq; if (skb->len > cen_aux_specs[0].footer) ret = -EMSGSIZE; @@ -847,6 +1042,25 @@ static int cen_hci_send(struct hci_dev *hdev, struct sk_buff *skb) } if (ret) goto out; + } else if (hci_skb_pkt_type(skb) == HCI_SCODATA_PKT && enable_acl && + bt->aux[1].opened) { + if (skb->len < HCI_SCO_HDR_SIZE || + skb->len != HCI_SCO_HDR_SIZE + skb->data[2] || + skb->len > cen_aux_specs[1].footer) { + ret = -EMSGSIZE; + goto out; + } + queue = &bt->sco_txq; + } else if (hci_skb_pkt_type(skb) == HCI_ISODATA_PKT && enable_acl && + bt->aux[2].opened) { + if (skb->len < HCI_ISO_HDR_SIZE || + skb->len != HCI_ISO_HDR_SIZE + + (get_unaligned_le16(skb->data + 2) & 0x3fff) || + skb->len + 1 > cen_aux_specs[2].footer) { + ret = -EMSGSIZE; + goto out; + } + queue = &bt->iso_txq; } else if (hci_skb_pkt_type(skb) != HCI_COMMAND_PKT) { ret = -EOPNOTSUPP; goto out; @@ -879,7 +1093,8 @@ static int cen_hci_send(struct hci_dev *hdev, struct sk_buff *skb) * scan_dup_filter is set, enable scans with Filter_Duplicates = 0; the * host copes with repeated reports. */ - if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT && skb->len >= 5 && !scan_dup_filter && + if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT && skb->len >= 5 && + (!scan_dup_filter || READ_ONCE(bt->dup_filter_fallback)) && (get_unaligned_le16(skb->data) == HCI_OP_LE_SET_EXT_SCAN_ENABLE || get_unaligned_le16(skb->data) == HCI_OP_LE_SET_SCAN_ENABLE) && skb->data[3] && skb->data[4] && !skb_ensure_writable(skb, 5)) { @@ -892,7 +1107,8 @@ static int cen_hci_send(struct hci_dev *hdev, struct sk_buff *skb) ret = -ENOBUFS; else { skb_queue_tail(queue, skb); - mod_delayed_work(system_percpu_wq, &bt->poll, 0); + if (!READ_ONCE(bt->transport_paused)) + mod_delayed_work(system_percpu_wq, &bt->poll, 0); } mutex_unlock(&bt->lock); if (!ret) @@ -906,13 +1122,11 @@ static int cen_hci_send(struct hci_dev *hdev, struct sk_buff *skb) static void cen_stop(struct cen_bt *bt) { + cancel_delayed_work_sync(&bt->poll); pci_clear_master(bt->pdev); kvfree(bt->capture); bt->capture = NULL; - /* IPC control 0 caused a firmware abort in the initial hardware probe. - * Until coordinated firmware teardown is understood, retain any published - * arena after detaching. A fresh firmware boot is required before rebinding. - */ + /* Retain any published arena after detaching until a fresh firmware boot. */ if (bt->ipc_requested || !pci_wait_for_pending_transaction(bt->pdev)) dev_warn(&bt->pdev->dev, "Beta detach requires fresh firmware boot; retaining DMA arena\n"); else @@ -954,6 +1168,9 @@ static int cen_status_show(struct seq_file *s, void *unused) seq_printf(s, "acl_enabled=%u acl_tx=%u acl_rx=%u acl_queued=%u le_queued=%u\n", enable_acl, bt->hdev->stat.acl_tx, bt->hdev->stat.acl_rx, skb_queue_len(&bt->acl_txq), skb_queue_len(&bt->le_txq)); + seq_printf(s, "sco_tx=%u sco_rx=%u sco_queued=%u iso_queued=%u\n", + bt->hdev->stat.sco_tx, bt->hdev->stat.sco_rx, + skb_queue_len(&bt->sco_txq), skb_queue_len(&bt->iso_txq)); seq_printf(s, "auxiliary_rings=%u messages=%u message_head=%u capture_bytes=%u capture_records=%u capture_dropped=%u\n", arm_auxiliary_rings, bt->message_tag, bt->message_head, bt->capture_used, bt->capture_records, bt->capture_dropped); @@ -1034,11 +1251,13 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) skb_queue_head_init(&bt->txq); skb_queue_head_init(&bt->acl_txq); skb_queue_head_init(&bt->le_txq); + skb_queue_head_init(&bt->sco_txq); + skb_queue_head_init(&bt->iso_txq); if (enable_acl && (!arm_le_data_rings || !arm_auxiliary_rings)) return -EINVAL; if (diagnostic_capture && !arm_auxiliary_rings) return -EINVAL; - if (native_hrb && (!native_startup || !enable_acl)) + if (native_hrb && ((!native_startup && !factory_address) || !enable_acl)) return -EINVAL; INIT_DELAYED_WORK(&bt->poll, cen_poll); ret = pcim_enable_device(pdev); @@ -1090,7 +1309,10 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) hdev->flush = cen_hci_flush; hdev->send = cen_hci_send; hdev->setup = cen_hci_setup; + hdev->set_bdaddr = cen_hci_set_bdaddr; hdev->post_init = cen_hci_post_init; + hdev->reset = cen_hci_reset; + hdev->reset_dup_filter = cen_hci_reset_dup_filter; /* * The N1 sets bits above the PHY value in LE Extended Advertising * Reports, as on T6050 (btcentauri_t6050.c). Unmasked, no PHY matches in @@ -1099,6 +1321,7 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) */ if (fixup_adv_phy) hci_set_quirk(hdev, HCI_QUIRK_FIXUP_LE_EXT_ADV_REPORT_PHY); + hci_set_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED); SET_HCIDEV_DEV(hdev, &pdev->dev); hci_set_drvdata(hdev, bt); pci_set_drvdata(pdev, bt); @@ -1113,7 +1336,7 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) debugfs_create_file("auxiliary_capture", 0400, bt->debug, bt, &cen_capture_fops); debugfs_create_file("capture_reset", 0200, bt->debug, bt, &cen_capture_reset_fops); } - dev_info(&pdev->dev, "Beta HCI transport registered; ACL enabled=%u, SCO/ISO unsupported\n", + dev_info(&pdev->dev, "Beta HCI transport registered; ACL/SCO/ISO enabled=%u\n", enable_acl); return 0; stop: @@ -1133,19 +1356,9 @@ static void cen_remove(struct pci_dev *pdev) } /* - * As for the Wi-Fi functions (centauri_pci.c): the firmware keeps running, so - * keep the function enabled and in D0 (the PCI core's default suspend turns - * bus mastering off, and the next poll stopped the transport for good) and - * the PCIe host powered. Polling pauses across the sleep. - */ -/* - * 26B123 AirshipDK: Beta sleep_control is BAR0 + 0x1078. Host sleep - * writes 3 and waits for peripheral_info.sleep_status == 3; wake writes - * 0 and waits for status 0 (active) or 1 (device sleep). The peripheral - * info block is at context + 0x68, with sleep_status at +8. - * - * Qualified on J813 with the installed Linux firmware. - * All ring doorbells must stay paused until the wake acknowledgment. + * Beta ACIPC host-sleep uses BAR0 + 0x1078 for sleep_control (3 = sleep, + * 0 = wake) and polls peripheral_info.sleep_status at context + 0x70 + * (3 = asleep, <= 1 = awake). Keep all ring doorbells paused until wake. */ static u32 cen_sleep_status(struct cen_bt *bt) { @@ -1182,7 +1395,7 @@ static int cen_suspend(struct device *dev) int cap, ret; mutex_lock(&bt->lock); - bt->transport_paused = true; + WRITE_ONCE(bt->transport_paused, true); mutex_unlock(&bt->lock); cancel_delayed_work_sync(&bt->poll); /* @@ -1256,9 +1469,9 @@ static int cen_resume(struct device *dev) bt->transport_sleep_sent = false; } mutex_lock(&bt->lock); - bt->transport_paused = false; + WRITE_ONCE(bt->transport_paused, false); if (bt->running) - schedule_delayed_work(&bt->poll, 0); + mod_delayed_work(system_percpu_wq, &bt->poll, 0); mutex_unlock(&bt->lock); return 0; } @@ -1283,4 +1496,4 @@ static struct pci_driver cen_driver = { }; module_pci_driver(cen_driver); MODULE_LICENSE("GPL"); -MODULE_DESCRIPTION("Apple N1 Beta HCI command/event transport for J813 and J873 bring-up"); +MODULE_DESCRIPTION("Apple N1 Beta HCI Bluetooth transport"); From 073079ce6993cd042ab460eb121f79af0a110144 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 23:01:27 +0700 Subject: [PATCH 14/17] wifi: apple: centauri: station statistics, band capabilities and data path The J815 test tree's Wi-Fi work, in one commit for testing: - station info from the firmware's counters: TX retries and failures, beacon RX and loss per association, live RX and TX bitrate, per-chain signal from the PHY dump, and .get_tx_power; - a channel survey from the CCA counters (active, busy, TX time) with the PHY dump's noise floor; - the firmware version in the wiphy and ethtool info; - HT, VHT and 2.4/5 GHz HE band capabilities; - disconnect reasons decoded, an initial country hint; - channel widths a 20 MHz-only firmware list leaves out are no longer restricted; - NAPI with GRO, a TX in-flight cap and the TCP pacing shift; - the scan rework, unit tests for the parsers, and a driver document. Signed-off-by: Nick --- .../device_drivers/wifi/apple/centauri.rst | 153 + .../networking/device_drivers/wifi/index.rst | 1 + .../net/wireless/apple/centauri/centauri.h | 34 +- .../wireless/apple/centauri/centauri_ipc.c | 5 +- .../wireless/apple/centauri/centauri_pci.c | 6 +- .../wireless/apple/centauri/centauri_wifi.c | 3219 +++++++++++++---- 6 files changed, 2750 insertions(+), 668 deletions(-) create mode 100644 Documentation/networking/device_drivers/wifi/apple/centauri.rst diff --git a/Documentation/networking/device_drivers/wifi/apple/centauri.rst b/Documentation/networking/device_drivers/wifi/apple/centauri.rst new file mode 100644 index 0000000000000..46324912369c3 --- /dev/null +++ b/Documentation/networking/device_drivers/wifi/apple/centauri.rst @@ -0,0 +1,153 @@ +.. SPDX-License-Identifier: GPL-2.0-only + +=================================== +Apple N1 Centauri Wi-Fi & Bluetooth +=================================== + +The Apple N1 wireless SoC exposes a three-function PCIe endpoint after +firmware boot: + +* **Function 0 (Control):** Firmware bootstrap, secondary FTAB handoff, + CCHI control channel, and cross-function recovery/sleep coordination. +* **Function 1 (Alpha):** IEEE 802.11 Wi-Fi MAC/PHY transport over ACIPC + command, event, beacon, and data transfer/completion rings. +* **Function 2 (Beta):** Bluetooth HCI/ACL/LE transport over ACIPC rings. + +Driver Topology +=============== + +Two driver families share the same PCI vendor/device IDs and separate by +PCIe host controller compatibility: + +* **T8142 / T8152 hosts** (e.g. ``apple,j813``, ``apple,j815``, + ``apple,j873g``, ``apple,j804``): + + - Wi-Fi & Control: ``drivers/net/wireless/apple/centauri/`` + (``CONFIG_APPLE_CENTAURI``, module ``apple_centauri``) + - Bluetooth: ``drivers/bluetooth/btcentauri.c`` + (``CONFIG_BT_CENTAURI``, module ``btcentauri``) + +* **T6050 hosts** (``apple,t6050-pcie``): + + - Wi-Fi & Control: ``drivers/net/wireless/apple/centauri/t6050/`` + (``CONFIG_APPLE_CENTAURI_T6050``, module ``apple_centauri_t6050``) + - Bluetooth: ``drivers/bluetooth/btcentauri_t6050.c`` + (``CONFIG_BT_HCICENTAURI``, module ``btcentauri_t6050``) + +Wi-Fi Capabilities +================== + +Both Wi-Fi backends implement ``cfg80211`` station-mode interfaces with: + +* **Bands and Capabilities:** 2.4 GHz, 5 GHz, and 6 GHz. Firmware system + command 8 provides HT (TLV 262), VHT (TLV 191), and HE (TLV 255) + capabilities which populate ``ht_cap`` (2.4/5 GHz), ``vht_cap`` (5 GHz), + and per-band HE ``iftype_data`` (2.4/5/6 GHz, with 6 GHz capability + words derived from VHT). Active channel lists and passive/radar/width + constraints are populated from firmware command 19 and re-applied on + regulatory updates. Split 6 GHz scanning (``WIPHY_FLAG_SPLIT_SCAN_6GHZ``) + and multi-BSSID reporting (``support_mbssid``) are enabled. +* **Scanning:** Up to 59 channels per firmware scan command (command + ``0x1000f``) and up to 4 SSIDs per scan (TLV 258). Scan aborts + (command ``0x10001``) pass the active 1-byte scan handle (``1..31``) + and treat firmware status ``342`` (handle already retired) as benign. +* **Security & Offload:** Open networks (2.4/5 GHz), WPA2-PSK, + WPA2-PSK-SHA256, and WPA3-SAE (``NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK`` + and ``NL80211_EXT_FEATURE_SAE_OFFLOAD``). Supported ciphers include + CCMP-128, GCMP-128, GCMP-256, BIP-CMAC-128, and mixed-mode WPA2 TKIP + group cipher (disallowed with SAE and on 6 GHz). Autonomous firmware + roam scans are suspended after association (command ``0x20016``) unless + ``roam_scan=1`` is set. Firmware disconnect events (``0x80000003``) + report the decoded IEEE 802.11 reason code and local-initiation flag to + ``cfg80211_disconnected()``. +* **Link Telemetry, Survey, Regulatory & MAC Address:** Startup command 0 + populates ``wiphy->fw_version`` (exposed via ``ethtool -i`` and + ``nl80211``). ``.get_channel``, ``.get_station``, ``.dump_station``, and + ``.get_tx_power`` query firmware interface status (command 20, TLV 268) + for live RSSI and active channel width (20/40/80/160 MHz), chip counters + (command ``0xb0001`` bit 1, TLV ``0xf901``) for per-association TX + retries, over-the-air TX failures, live RX bitrate, and fallback TX + bitrate, frame counters (command ``0xb0001`` bit 19, TLV ``0xf915``) for + per-association beacon RX and beacon loss counts, TX power/rate history + (command ``0xb0001`` bit 29, TLV ``0xf920``, supporting both 280-byte + and 320-byte record layouts) for live Legacy/HT/VHT/HE/EHT TX bitrate + and combined TX power in dBm, PHY dump records (command ``0xb0001`` bit + 32, TLV ``0xf923``) for per-chain RSSI and fallback noise floor + (selecting radio core 0 for 2.4 GHz and core 1 with core-0 fallback for + 5/6 GHz), and per-association packet/byte/failure/drop counters. + ``.dump_survey`` queries firmware station statistics (command ``0xb0001`` + bit 7, TLV ``0xf908``, falling back to PHY dump TLV ``0xf923``) for + channel noise floor in dBm and CCA statistics (command ``0xb0001`` bit 3, + TLV ``0xf903``, selecting radio slice 1 for 2.4 GHz and slice 2 with + slice-1 fallback for 5/6 GHz) for channel active, busy, and transmit + time in milliseconds. On bring-up, the firmware's initial country code + (command 10) is passed to ``regulatory_hint()`` when valid. Runtime MAC + address changes are supported via ``.ndo_set_mac_address`` while idle. +* **Data Path, NAPI GRO & Ring Resilience:** RX (ring 12 / completion 207) + is drained via NAPI (``napi_gro_receive()``) and decodes both standard + and extended-prefix RX frames, returning buffer cookies to the refill + ring on error completions. TX (ring 25 / completion 192) enforces a + software queue and in-flight cap with a bypass watchdog, sets TCP pacing + shift (``sk_pacing_shift_update()``), frees completed packets via + ``dev_consume_skb_any()``, decodes both single and coalesced TX + completions, and exports throughput/backpressure counters via debugfs + ``data_stats``. Control backends support warm-reset recovery on + persistent transport faults. + +Bluetooth Capabilities +====================== + +Both Bluetooth transports expose an ``HCI_PCI`` device with: + +* **Transport Rings:** HCI command/event rings (1 and 7), LE ACL TX/RX + rings (4 and 10), BR/EDR ACL TX/RX auxiliary rings (2 and 8), SCO + TX/RX rings (3 and 9, raw HCI SCO packets), and LE Audio ISO TX/RX + rings (5 and 11, 1-byte H4 packet indicator prefix). Both drivers use + 2 MSI vectors for interrupt-driven I/O with pending-work continuation; + ``btcentauri`` falls back to 1 ms polling automatically if MSI + allocation is unavailable. +* **Controller Provisioning & Quirks:** Factory BD_ADDR provisioning + from device-tree ``local-bd-address`` (``0xfc06``, ``0xff0b``, + ``0xfe44``), runtime public address updates via ``hdev->set_bdaddr`` + (``0xfc06``), vendor duplicate-filter flush (``0xff3f`` via + ``hdev->reset_dup_filter``, automatically falling back to host + duplicate filtering if firmware rejects the command), LE extended + advertising PHY and legacy scan-response normalization, wideband speech + HFP support (``HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED`` over SCO rings 3 + and 9), host-side LE accept-list scan filtering (reporting accept-list + size 0 unless ``accept_list=1`` is set), and BR/EDR extended Create + Connection translation (``0xff7b`` when advertised by ``0xffa3`` bit 9). +* **Resilient Data RX, Timeout Diagnostics & Power Management:** + Frame-level malformed ACL, SCO, and ISO packets are dropped with + rate-limited diagnostics without halting the HCI transport, and unknown + trailing RX metadata attributes are ignored without dropping valid HCI + payloads. ``hdev->reset`` logs ring/transport state and reschedules I/O + on HCI command timeouts. Across system suspend, Beta enters ACIPC host + sleep via BAR0 ``0x1078`` and restores wake state on resume. + +Firmware +======== + +Firmware images are external board-specific FTAB binaries placed under +``/lib/firmware/apple/centauri/`` (e.g. ``j813-ftab.bin``, +``j815-ftab.bin``, ``j873-ftab.bin``, ``j804-ftab.bin`` on T8142/T8152, +or the firmware path named by device tree on T6050). + +Testing +======= + +Host-executable unit tests (requiring only Python 3 and a C compiler) +live under ``tools/asahi/``: + +.. code-block:: sh + + python3 tools/asahi/centauri-wifi-protocol-tests.py + python3 tools/asahi/centauri-bt-protocol-tests.py + python3 tools/asahi/centauri-wifi-pm-tests.py --negative-controls + python3 tools/asahi/centauri-wifi-avoid-tests.py + python3 tools/asahi/centauri-bt-arena-tests.py --negative-controls + python3 tools/asahi/centauri-recovery-tests.py --negative-controls + python3 tools/asahi/centauri-physical-recovery-tests.py + +In-kernel KUnit tests are available via ``CONFIG_APPLE_CENTAURI_KUNIT_TEST`` +and ``CONFIG_APPLE_CENTAURI_T6050_RECOVERY_KUNIT``. diff --git a/Documentation/networking/device_drivers/wifi/index.rst b/Documentation/networking/device_drivers/wifi/index.rst index 2cd954f5b1fee..dbda2f111bc56 100644 --- a/Documentation/networking/device_drivers/wifi/index.rst +++ b/Documentation/networking/device_drivers/wifi/index.rst @@ -8,6 +8,7 @@ Contents: .. toctree:: :maxdepth: 2 + apple/centauri intel/ipw2100 intel/ipw2200 mt7932-neo diff --git a/drivers/net/wireless/apple/centauri/centauri.h b/drivers/net/wireless/apple/centauri/centauri.h index 17abbfb8352f2..c08257d70ba0a 100644 --- a/drivers/net/wireless/apple/centauri/centauri.h +++ b/drivers/net/wireless/apple/centauri/centauri.h @@ -46,27 +46,16 @@ struct centauri { bool inject_stall; /* debugfs test: the next check finds the firmware stopped */ }; -/* One row per machine with an N1: the FTAB file name, the word written to BAR+0x807c, - * (board-id << 16) | chip-id from the machine's ADT /chosen, and the physical addresses and - * the one differing register value of the port cycle (centauri_port.c). - * J813: board 0x24, chip 0x8142 (measured); port, PHY lane and PERST pad from the live ADT. - * J815 (MacBook Air 15" M5): board 0x26, chip 0x8142 from its ADT /chosen; same T8142 port 0 - * config/lane and PERST# (pci-bridge0 function-perst = gpio0 pin 0xc0 -> 0x39a000300) as J813, - * and the same N1 image (t2026macG1 ftab in the J813 and J815 restore identities). Its stub is - * macOS 27.0, so it takes the 27.0 adapter's link TLV 386 and scan TLVs. - * J873 (Mac mini M6): board 0x24, chip 0x8152 from its ADT, the composition taken from the J813 - * word; port 0 config 0x388028000, lane 0x38f010000 and PERST# gpio0 pin 3 (0x31a000000 + 4 * 3) - * from its ADT; config +0x130 is 0x208 as in the m1n1 T8152 bring-up that links the port. - * keep_port_tables: the J813 sequence zeroes the 16 words at port+0x3000 and the 256 words at - * port+0x3800. On the J873 those are the RID2SID and MSIMAP tables that pcie-apple programmed - * (PORT_T602X_RID2SID, PORT_T602X_MSIMAP); the cycle then drops function 0's stream entry and - * its DMA arrives as SID 0 (DART "BPF reject"). Such a board gets its RID2SID table written - * back; the MSI map and the MSI address/config are restored on every board. - * link_tlv, scan_tlvs: the macOS 27.0 WLAN adapter differs from the 26.6.1 one the J813 recipe - * came from. startInterface carries the three link addresses in TLV 386 (was 385), and the scan - * request (command 0x1000f) carries a channel-list TLV 0x104 and an OCE TLV 0x165 after the fixed - * 14 bytes. The J873 scan crash in SCHED_Req_Add came from a zero passive dwell in those 14 bytes - * (see build_scan), not from the missing TLVs; whether the TLVs are required is untested. +/* Per-board hardware descriptors: FTAB firmware path, ROM board word + * ((board-id << 16) | chip-id written to BAR+0x807c), PCIe root-port + * addresses and link-tuning parameters (centauri_port.c), plus firmware + * protocol feature flags (link_tlv for startInterface link-address TLV + * and scan_tlvs for explicit channel-list/OCE scan TLVs). + * + * keep_port_tables: on platforms where port+0x3000/0x3800 hold the + * PCIe RID2SID and MSIMAP tables programmed by pcie-apple, save and + * restore RID2SID across the port reset cycle so function 0 retains + * its IOMMU stream mapping. */ struct centauri_board { const char *compatible; @@ -92,6 +81,9 @@ static inline const struct centauri_board *centauri_board(void) { "apple,j873g", "apple/centauri/j873-ftab.bin", 0x00248152, 0x388028000ULL, 0x38f010000ULL, 0x31a00000cULL, 0x208, 0, true, 386, true }, + { "apple,j804", "apple/centauri/j804-ftab.bin", 0x00228152, + 0x388028000ULL, 0x38f010000ULL, 0x31a00000cULL, 0x208, 0, true, + 386, true }, }; int i; diff --git a/drivers/net/wireless/apple/centauri/centauri_ipc.c b/drivers/net/wireless/apple/centauri/centauri_ipc.c index d657e6a4b5374..6a830e5b48409 100644 --- a/drivers/net/wireless/apple/centauri/centauri_ipc.c +++ b/drivers/net/wireless/apple/centauri/centauri_ipc.c @@ -1,8 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-only -/* Native DMA implementation of the measured J813 Control/Alpha bootstrap. - * Wire layout source: AppleCentauriCommon/scripts/alpha_wifi_scan.py v259. - * Polling and bounded bootstrap rings are deliberate bring-up restrictions. - */ +/* Apple N1 Control and Alpha ACIPC bootstrap and DMA resource management. */ #include #include #include diff --git a/drivers/net/wireless/apple/centauri/centauri_pci.c b/drivers/net/wireless/apple/centauri/centauri_pci.c index 67d3a7884759f..67c399b3b443b 100644 --- a/drivers/net/wireless/apple/centauri/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/centauri_pci.c @@ -1,8 +1,5 @@ // SPDX-License-Identifier: GPL-2.0-only -/* Initial real-hardware N1 PCI/ACIPC backend for J813. - * Register offsets are from AppleCentauriCommon/PROTOCOL.md and its measured - * J813 prototype. Firmware execution and radio operations are separate gates. - */ +/* Apple N1 PCI/ACIPC transport backend. */ #include #include #include @@ -537,3 +534,4 @@ MODULE_DESCRIPTION("Apple N1 Centauri native PCI and ACIPC bring-up backend"); MODULE_FIRMWARE("apple/centauri/j813-ftab.bin"); MODULE_FIRMWARE("apple/centauri/j815-ftab.bin"); MODULE_FIRMWARE("apple/centauri/j873-ftab.bin"); +MODULE_FIRMWARE("apple/centauri/j804-ftab.bin"); diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index 3d3f0948d3b34..33613666d3eb2 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -1,21 +1,22 @@ // SPDX-License-Identifier: GPL-2.0-only -/* cfg80211 connection to the measured Alpha command/management rings. - * Control queues recycle across hardware-validated wraps. Packet data uses - * a separate protocol; never report transmit success before implementing it. - */ +/* cfg80211 driver for the Apple N1 Alpha Wi-Fi ACIPC transport. */ +#include #include #include #include #include +#include #include #include #include +#include #include #include #include #include #include #include +#include #include "centauri.h" #define CR_BASE 0x100000 @@ -23,10 +24,15 @@ #define RING_SPAN 0x20000 #define TX_BUFFER 0x600000 #define BUFFER_SIZE 2048 +#define REPLY_SIZE (BUFFER_SIZE - 24) +/* The band plan: 2.4 GHz 1-14, 5 GHz 36-64, 100-144 and 149-177, 6 GHz 1-233. */ +#define CEN_CHANNELS (14 + 28 + 59) #define DATA_TX_BUFFER 0xd00000 #define DATA_RX_BUFFER 0xe00000 #define DATA_TX_SLOTS 128 #define DATA_TX_QUEUE_LIMIT 64 +#define DATA_TX_INFLIGHT_MAX 64 +#define DATA_TX_PACING_SHIFT 7 /* The firmware owns window +8 MiB through +13 MiB as scratch memory. */ static_assert(DATA_TX_BUFFER >= CEN_ALPHA_SCRATCH_OFFSET + CEN_ALPHA_SCRATCH_SIZE); @@ -113,7 +119,8 @@ struct centauri_wifi { struct work_struct connect_work; struct delayed_work link_work; struct cfg80211_scan_request *scan_request; - bool scan_abort; + bool scan_abort, connect_abort; + u8 scan_sequence; struct mutex lock; /* The logger has its own coherent allocation. Reads/acknowledgements * must remain possible while scan/connect hold the command mutex. @@ -123,17 +130,24 @@ struct centauri_wifi { u16 main_head, command_head, sequence; u32 rx_seen[5]; u16 rx_next[5], rx_head[5]; - u16 reply_sequence, reply_status; + u16 reply_sequence, reply_status, reply_capture; u32 reply_command, channels_seen, scans_done, beacons_seen; bool reply_valid, registered, configured; + u8 *reply_buffer; + size_t reply_capacity, reply_size; bool stopping; bool transport_paused, transport_sleep_sent, host_sleep_sent; + bool host_events_fallback_logged; bool connecting, connected, connect_event, disconnect_event; + bool connect_privacy; bool associated, pmk_seen, pmk_matches; u8 expected_pmk[32]; - u16 connect_status; + u16 connect_status, disconnect_reason; + bool disconnect_local; + struct ieee80211_channel *connect_channel; u8 bssid[ETH_ALEN], connect_payload[1024], request_ie[256]; - size_t connect_size, request_ie_size; + u8 ssid[IEEE80211_MAX_SSID_LEN]; + size_t connect_size, request_ie_size, ssid_len; struct dentry *data_probe_file; struct dentry *data_status_file; struct dentry *data_rx_snapshot_file; @@ -144,6 +158,36 @@ struct centauri_wifi { bool data_tx_busy[DATA_TX_SLOTS]; u32 data_tx_submitted, data_tx_completed, data_tx_failed; u32 data_tx_coalesced, data_tx_bad; + u32 data_tx_capped, data_tx_cap_bypassed; + unsigned long data_tx_completed_at; + struct napi_struct rx_napi; + struct sk_buff_head rx_gro_queue; + s8 station_signal, station_noise; + bool station_signal_valid, station_noise_valid; + s8 station_chain_signal[2], station_phy_noise; + u8 station_chains; + bool station_phy_noise_valid; + u32 station_tx_retrans, station_tx_retrans_base; + bool station_tx_retrans_valid, station_tx_retrans_base_valid; + u32 station_tx_fail, station_tx_fail_base; + bool station_tx_fail_valid, station_tx_fail_base_valid; + u32 station_rx_rate_kbps, station_chip_tx_rate_kbps; + bool station_rx_rate_valid, station_chip_tx_rate_valid; + u32 station_beacon_rx, station_beacon_rx_base; + u32 station_beacon_loss, station_beacon_loss_base; + bool station_beacon_valid, station_beacon_base_valid; + u64 station_cca_active_ms, station_cca_busy_ms, station_cca_tx_ms; + bool station_cca_valid; + struct rate_info station_tx_rate; + int station_tx_power_dbm; + bool station_tx_rate_valid, station_tx_power_valid; + struct cfg80211_chan_def station_chandef; + u8 station_bw_primary; + bool station_chandef_valid; + unsigned long station_signal_at, station_noise_at, station_counters_at; + unsigned long station_cca_at, station_tx_info_at, station_beacon_at; + unsigned long station_phy_dump_at, connected_at; + struct net_device_stats station_start; struct dentry *station_stats_file; struct dentry *eapol_start_file; struct dentry *country_file; @@ -179,8 +223,12 @@ struct centauri_wifi { struct dentry *tx_power_report_file; u16 stats_reply_size, stats_tag, stats_length; u32 data_probe_done; - struct ieee80211_supported_band bands[2]; - struct ieee80211_channel channels[39]; + struct ieee80211_supported_band bands[3]; + struct ieee80211_channel channels[CEN_CHANNELS]; + struct ieee80211_sband_iftype_data iftype_data[3]; + u8 hw_channels[REPLY_SIZE], system_caps[REPLY_SIZE]; + u8 stats_reply[REPLY_SIZE]; + struct debugfs_blob_wrapper hw_channels_blob, system_caps_blob; }; /* These helpers require wifi->lock, including from the connect worker. */ @@ -190,19 +238,49 @@ static int query_link_status(struct centauri_wifi *wifi); static const u32 rx_buffer[5] = { 0, 0, 0x620000, 0x680000, 0x700000 }; static const u16 ring_size[5] = { 0, 64, 192, 96, 192 }; -static const u16 five_ghz[] = { - 36, 40, 44, 48, 52, 56, 60, 64, 100, 104, 108, 112, - 116, 120, 124, 128, 132, 136, 140, 144, 149, 153, 157, 161, 165, +static const struct { + enum nl80211_band band; + u8 first, last, step; +} band_plan[] = { + { NL80211_BAND_2GHZ, 1, 14, 1 }, + { NL80211_BAND_5GHZ, 36, 64, 4 }, + { NL80211_BAND_5GHZ, 100, 144, 4 }, + { NL80211_BAND_5GHZ, 149, 177, 4 }, + { NL80211_BAND_6GHZ, 1, 233, 4 }, }; static struct ieee80211_rate rates[] = { { .bitrate = 10 }, { .bitrate = 20 }, { .bitrate = 55 }, { .bitrate = 110 }, { .bitrate = 60 }, { .bitrate = 90 }, { .bitrate = 120 }, { .bitrate = 180 }, { .bitrate = 240 }, { .bitrate = 360 }, { .bitrate = 480 }, { .bitrate = 540 }, }; +static const u32 akm_suites[] = { + WLAN_AKM_SUITE_PSK, + WLAN_AKM_SUITE_PSK_SHA256, + WLAN_AKM_SUITE_SAE, +}; static const u32 cipher_suites[] = { - WLAN_CIPHER_SUITE_CCMP, WLAN_CIPHER_SUITE_AES_CMAC, + WLAN_CIPHER_SUITE_TKIP, + WLAN_CIPHER_SUITE_CCMP, + WLAN_CIPHER_SUITE_GCMP, + WLAN_CIPHER_SUITE_GCMP_256, + WLAN_CIPHER_SUITE_AES_CMAC, }; +/* The firmware's band numbers: 0 is 2.4 GHz, 1 is 5 GHz, 2 is 6 GHz. */ +static int firmware_band(enum nl80211_band band) +{ + switch (band) { + case NL80211_BAND_2GHZ: + return 0; + case NL80211_BAND_5GHZ: + return 1; + case NL80211_BAND_6GHZ: + return 2; + default: + return -EOPNOTSUPP; + } +} + static u16 index_read(struct centauri_wifi *wifi, unsigned int array, unsigned int index) { return le16_to_cpu(READ_ONCE(*(__le16 *)(wifi->a.context + wifi->a.index[array] + 2 * index))); @@ -287,29 +365,37 @@ static void report_beacon(struct centauri_wifi *wifi, const u8 *payload, size_t struct cfg80211_bss *bss; u16 frame_size; int number = 0, frequency, signal; - enum nl80211_band band; + enum nl80211_band band = NL80211_BAND_2GHZ; if (!wifi->registered || size < 0x60 + 36) return; - /* The ACIPC packet length is authoritative; the vendor descriptor's - * separate 802.11 length can include a stripped FCS. The measured parser - * consumes the frame bytes that were actually returned. - */ frame_size = size - 0x60; frame = payload + 0x60; if (!ieee80211_is_beacon(*(__le16 *)frame) && !ieee80211_is_probe_resp(*(__le16 *)frame)) return; end = frame + frame_size; for (ie = frame + 36; ie + 2 <= end && ie + 2 + ie[1] <= end; ie += 2 + ie[1]) { - if ((ie[0] == WLAN_EID_DS_PARAMS || ie[0] == WLAN_EID_HT_OPERATION) && ie[1]) + if ((ie[0] == WLAN_EID_DS_PARAMS || ie[0] == WLAN_EID_HT_OPERATION) && ie[1]) { number = ie[2]; + band = number <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ; + } else if (ie[0] == WLAN_EID_EXTENSION && ie[1] > 1 && + ie[2] == WLAN_EID_EXT_HE_OPERATION && + ie[1] - 1 >= sizeof(struct ieee80211_he_operation) && + ie[1] >= ieee80211_he_oper_size(ie + 3)) { + const struct ieee80211_he_6ghz_oper *oper = + ieee80211_he_6ghz_oper((const void *)(ie + 3)); + + if (oper && !number) { + number = oper->primary; + band = NL80211_BAND_6GHZ; + } + } } if (!number) return; - band = number <= 14 ? NL80211_BAND_2GHZ : NL80211_BAND_5GHZ; frequency = ieee80211_channel_to_frequency(number, band); channel = ieee80211_get_channel(wifi->wiphy, frequency); - if (!channel) + if (!channel || channel->flags & IEEE80211_CHAN_DISABLED) return; signal = max((s8)payload[0x16], (s8)payload[0x17]); if (signal >= 0 || signal < -127) @@ -333,17 +419,56 @@ static void post_receive(struct centauri_wifi *wifi, unsigned int ring, u16 slot put_unaligned_le32(slot + 1, descriptor + 12); } -/* Host-owned RX buffer cookies and completion length are the ACIPC fields - * validated in probe 28. Probe 29 validates the footer's prefix length and - * Ethernet framing with IPv4 header checksums and IPv6 payload lengths. - * Called with wifi->lock, including during connect/command polling. - */ +static bool data_rx_frame(const u8 *packet, unsigned int capacity, + unsigned int length, u32 footer, unsigned int *offset, + unsigned int *frame_length) +{ + unsigned int padding = (footer >> 12) & 15; + + if (!(footer & BIT(7))) + return false; + if (length < padding + ETH_HLEN || length > capacity) + return false; + if (footer & BIT(24)) { + if (capacity < 4) + return false; + *offset = get_unaligned_le16(packet + 2); + } else { + *offset = padding; + } + *frame_length = length - padding; + return *offset <= capacity && *frame_length <= capacity - *offset; +} + +static int rx_poll(struct napi_struct *napi, int budget) +{ + struct centauri_wifi *wifi = + container_of(napi, struct centauri_wifi, rx_napi); + struct sk_buff *skb; + int done = 0; + + while (done < budget && (skb = skb_dequeue(&wifi->rx_gro_queue))) { + napi_gro_receive(napi, skb); + done++; + } + if (done < budget && napi_complete_done(napi, done) && + !skb_queue_empty(&wifi->rx_gro_queue)) + napi_schedule(napi); + return done; +} + +/* Called with wifi->lock, including during connect/command polling. */ static void drain_data_rx(struct centauri_wifi *wifi) { u16 head, tail, post; unsigned int processed = 0; + bool consumed = false, reposted = false; + struct sk_buff_head frames; - if (!(wifi->data_probe_done & BIT(4)) || readl(wifi->a.bar + 8) != 2) + __skb_queue_head_init(&frames); + + if (READ_ONCE(wifi->transport_paused) || + !(wifi->data_probe_done & BIT(4)) || readl(wifi->a.bar + 8) != 2) return; head = index_read(wifi, 0, 207); tail = index_read(wifi, 2, 207); @@ -357,16 +482,41 @@ static void drain_data_rx(struct centauri_wifi *wifi) u8 *completion = wifi->a.window + 0x240000 + tail * 32; u32 header = get_unaligned_le32(completion); u32 info = get_unaligned_le32(completion + 4); + u32 status = get_unaligned_le32(completion + 8); u16 cookie = info & 0xffff, length = info >> 16; u16 next = (post + 1) % 128; + bool valid_cookie = cookie >= 1 && cookie <= 127; u32 count = le32_to_cpu(wifi->rx_snapshot.count); u8 *packet, *descriptor; u32 buffer; - if ((header >> 16) != 12 || !cookie || cookie > 127 || - length > 4096 || get_unaligned_le32(completion + 8)) { + if ((header >> 16) != 12 || !valid_cookie || length > 4096 || + status) { + if ((header >> 16) == 12 && valid_cookie && + next == index_read(wifi, 1, 12)) + break; wifi->data_rx_bad++; - break; + wifi->netdev->stats.rx_errors++; + dev_warn_ratelimited(&wifi->control->pdev->dev, + "Wi-Fi data RX bad completion: ring=%u cookie=%u len=%u status=%#x\n", + header >> 16, cookie, length, + status); + if ((header >> 16) == 12 && valid_cookie) { + buffer = DATA_RX_BUFFER + (cookie - 1) * 4096; + descriptor = wifi->a.window + 0x520000 + + post * 16; + put_unaligned_le32(1 | (4096 << 8), descriptor); + put_unaligned_le64(wifi->a.window_dma + buffer, + descriptor + 4); + put_unaligned_le32(cookie, descriptor + 12); + wifi->data_rx_reposted++; + post = next; + reposted = true; + } + tail = (tail + 1) % 128; + consumed = true; + processed++; + continue; } /* Do not overwrite a descriptor the receive engine still owns. */ if (next == index_read(wifi, 1, 12)) @@ -382,19 +532,20 @@ static void drain_data_rx(struct centauri_wifi *wifi) wifi->data_rx_received++; if (wifi->connected && netif_running(wifi->netdev)) { u32 footer = get_unaligned_le32(completion + 12); - unsigned int padding = (footer >> 12) & 15; + unsigned int offset, frame_length; struct sk_buff *skb = NULL; - /* Extended prefix headers are not decoded yet. */ - if (!(footer & BIT(24)) && length >= padding + ETH_HLEN) - skb = netdev_alloc_skb_ip_align(wifi->netdev, length - padding); + if (data_rx_frame(packet, 4096, length, footer, &offset, + &frame_length)) + skb = netdev_alloc_skb_ip_align(wifi->netdev, + frame_length); if (skb) { - skb_put_data(skb, packet + padding, length - padding); + skb_put_data(skb, packet + offset, frame_length); skb->protocol = eth_type_trans(skb, wifi->netdev); skb->ip_summed = CHECKSUM_NONE; wifi->netdev->stats.rx_packets++; - wifi->netdev->stats.rx_bytes += length - padding; - netif_rx(skb); + wifi->netdev->stats.rx_bytes += frame_length; + __skb_queue_tail(&frames, skb); } else { wifi->netdev->stats.rx_dropped++; } @@ -408,15 +559,40 @@ static void drain_data_rx(struct centauri_wifi *wifi) wifi->data_rx_reposted++; tail = (tail + 1) % 128; post = next; + consumed = true; + reposted = true; processed++; } - if (processed) { + if (consumed || reposted) { dma_wmb(); - index_write(wifi, 2, 207, tail); - index_write(wifi, 3, 12, post); + if (consumed) + index_write(wifi, 2, 207, tail); + if (reposted) + index_write(wifi, 3, 12, post); dma_wmb(); writel(1, wifi->a.bar + 0x1050); } + if (!skb_queue_empty(&frames)) { + spin_lock_bh(&wifi->rx_gro_queue.lock); + skb_queue_splice_tail_init(&frames, &wifi->rx_gro_queue); + spin_unlock_bh(&wifi->rx_gro_queue.lock); + local_bh_disable(); + napi_schedule(&wifi->rx_napi); + local_bh_enable(); + } +} + +static bool data_tx_completion_span(u16 cookie, u16 wire_count, u8 flags, + unsigned int *first, u16 *count) +{ + bool counted = (flags & BIT(0)) || (flags & 0x0e) == 8; + + if (!cookie || cookie > DATA_TX_SLOTS || counted != !!wire_count || + wire_count >= DATA_TX_SLOTS) + return false; + *count = counted ? wire_count : 1; + *first = (cookie + DATA_TX_SLOTS - *count) % DATA_TX_SLOTS; + return true; } /* Serialized with the firmware poller. Payload slots remain owned until a @@ -428,13 +604,16 @@ static void drain_data_tx(struct centauri_wifi *wifi) unsigned int budget = 256; bool posted = false, completed = false; - if (!(wifi->data_probe_done & BIT(5)) || readl(wifi->a.bar + 8) != 2) + if (READ_ONCE(wifi->transport_paused) || + !(wifi->data_probe_done & BIT(5)) || readl(wifi->a.bar + 8) != 2) return; head = index_read(wifi, 0, 192); tail = index_read(wifi, 2, 192); post = index_read(wifi, 3, 25); - if (head >= 2048 || tail >= 2048 || post >= DATA_TX_SLOTS) + if (head >= 2048 || tail >= 2048 || post >= DATA_TX_SLOTS) { + wifi->data_tx_bad++; return; + } dma_rmb(); while (tail != head && budget--) { u8 *entry = wifi->a.window + 0x200000 + tail * 32; @@ -446,27 +625,43 @@ static void drain_data_tx(struct centauri_wifi *wifi) if (ring == 25) { unsigned int first, i; - /* Airship cr_controller::sync maps CD bytes 9:10 to the - * transfer count. With the multicomp flag (CD byte 1 bit 0), - * the cookie names the LAST transfer; prior transfers share - * its completion status. We submit one contiguous DMA buffer - * per slot, so those preceding cookies must all still be busy. - */ - if (!cookie || cookie > DATA_TX_SLOTS || !count || - count >= DATA_TX_SLOTS || !(entry[1] & 1)) { + if (!data_tx_completion_span(cookie, count, entry[1], + &first, &count)) { wifi->data_tx_bad++; - break; + dev_warn_ratelimited(&wifi->control->pdev->dev, + "Wi-Fi data TX bad completion: cookie=%u count=%u flags=%#x status=%#x\n", + cookie, count, entry[1], + status); + tail = (tail + 1) % 2048; + completed = true; + continue; } - first = (cookie + DATA_TX_SLOTS - count) % DATA_TX_SLOTS; for (i = 0; i < count; i++) if (!wifi->data_tx_busy[(first + i) % DATA_TX_SLOTS]) break; if (i != count) { wifi->data_tx_bad++; - break; + dev_warn_ratelimited(&wifi->control->pdev->dev, + "Wi-Fi data TX unexpected slot state: first=%u count=%u status=%#x\n", + first, count, status); + for (i = 0; i < count; i++) { + unsigned int slot = (first + i) % + DATA_TX_SLOTS; + + if (!wifi->data_tx_busy[slot]) + continue; + wifi->data_tx_busy[slot] = false; + wifi->data_tx_completed++; + wifi->data_tx_failed++; + wifi->netdev->stats.tx_errors++; + } + tail = (tail + 1) % 2048; + completed = true; + continue; } if (count > 1) wifi->data_tx_coalesced++; + wifi->data_tx_completed_at = jiffies; for (i = 0; i < count; i++) { unsigned int slot = (first + i) % DATA_TX_SLOTS; @@ -488,6 +683,7 @@ static void drain_data_tx(struct centauri_wifi *wifi) index_write(wifi, 2, 192, tail); budget = DATA_TX_SLOTS - 1; while (wifi->connected && !READ_ONCE(wifi->stopping) && + !READ_ONCE(wifi->transport_paused) && netif_running(wifi->netdev) && budget--) { u16 next = (post + 1) % DATA_TX_SLOTS; u32 buffer = DATA_TX_BUFFER + post * BUFFER_SIZE; @@ -495,9 +691,22 @@ static void drain_data_tx(struct centauri_wifi *wifi) struct sk_buff *skb; u8 destination[ETH_ALEN]; u64 metadata = 6ULL << 27; + u32 inflight = wifi->data_tx_submitted - wifi->data_tx_completed; if (wifi->data_tx_busy[post] || next == index_read(wifi, 1, 25)) break; + if (inflight >= DATA_TX_INFLIGHT_MAX && + !skb_queue_empty(&wifi->data_tx_queue)) { + if (time_before(jiffies, + wifi->data_tx_completed_at + HZ / 2)) { + wifi->data_tx_capped++; + break; + } + if (!wifi->data_tx_cap_bypassed++) + dev_warn(&wifi->control->pdev->dev, + "Wi-Fi TX: no completion for %u in-flight frames; posting past the cap\n", + inflight); + } skb = skb_dequeue(&wifi->data_tx_queue); if (!skb) break; @@ -520,27 +729,21 @@ static void drain_data_tx(struct centauri_wifi *wifi) put_unaligned_le32(3 | (skb->len << 8), descriptor); put_unaligned_le64(wifi->a.window_dma + buffer, descriptor + 4); put_unaligned_le16(post + 1, descriptor + 12); - /* AirshipDataPacket type 1 supplies this 16-byte TX header inline. - * setTxPolicyId(1), 0x1809ed27c, sets validity bit 60 and ID bit 48. - * Keep normal policy zero by default; the switch permits a bounded - * comparison with the existing, read-back-verified control profile. - */ + /* Optional TX policy ID 1 (validity bit 60 and ID bit 48). */ if (READ_ONCE(data_tx_control_profile) && (wifi->tx_defaults_applied & BIT(1))) metadata |= BIT_ULL(60) | BIT_ULL(48); - /* Bits 17:15 carry a 3-bit packet class. A DHCP Discover - * captured from the native stack has value 4 (0x20000). The same-connection Linux A/B changes - * zero DHCP offers into three offers by setting only this field. - * The opt-out retains the old value for diagnostic reproduction. - */ + /* Bits 17:15 carry the 3-bit packet class (4 for normal data). */ if (READ_ONCE(data_tx_native_prepare)) metadata |= 4ULL << 15; put_unaligned_le64(metadata, descriptor + 16); put_unaligned_le64(2000000, descriptor + 24); + if (wifi->data_tx_submitted == wifi->data_tx_completed) + wifi->data_tx_completed_at = jiffies; wifi->data_tx_length[post] = skb->len; wifi->data_tx_busy[post] = true; wifi->data_tx_submitted++; - dev_kfree_skb_any(skb); + dev_consume_skb_any(skb); post = next; posted = true; } @@ -551,7 +754,8 @@ static void drain_data_tx(struct centauri_wifi *wifi) dma_wmb(); writel(1, wifi->a.bar + 0x1030); } - if (wifi->connected && !READ_ONCE(wifi->stopping) && netif_running(wifi->netdev) && + if (wifi->connected && !READ_ONCE(wifi->stopping) && + !READ_ONCE(wifi->transport_paused) && netif_running(wifi->netdev) && skb_queue_len(&wifi->data_tx_queue) < DATA_TX_QUEUE_LIMIT) netif_wake_queue(wifi->netdev); } @@ -593,6 +797,552 @@ static void record_stats_reply(struct centauri_wifi *wifi, const u8 *packet, } } +static bool sta_info_noise_read(const u8 *payload, size_t size, s8 *noise) +{ + size_t pos = 0; + + while (pos + 4 <= size) { + u16 tag = get_unaligned_le16(payload + pos); + u16 length = get_unaligned_le16(payload + pos + 2); + + if (length > size - pos - 4) + break; + if (tag == 0xf908 && length >= 3) { + s8 val = (s8)payload[pos + 4 + 2]; + + if (val >= -120 && val <= -40) { + *noise = val; + return true; + } + return false; + } + pos += 4 + length; + } + return false; +} + +static bool chip_sta_counters_read(const u8 *payload, size_t size, u32 *retrans, + u32 *tx_fail, bool *tx_fail_valid, + u32 *tx_rate_kbps, u32 *rx_rate_kbps) +{ + size_t pos = 0; + + *tx_fail = 0; + *tx_fail_valid = false; + *tx_rate_kbps = 0; + *rx_rate_kbps = 0; + while (pos + 4 <= size) { + u16 tag = get_unaligned_le16(payload + pos); + u16 length = get_unaligned_le16(payload + pos + 2); + + if (length > size - pos - 4) + break; + if (tag == 0xf901 && length >= 12) { + const u8 *v = payload + pos + 4; + + *retrans = get_unaligned_le32(v + 8); + if (length >= 0x14) { + *tx_fail = get_unaligned_le32(v + 0x0c) + + get_unaligned_le32(v + 0x10); + *tx_fail_valid = true; + } + if (length >= 0x28) + *tx_rate_kbps = get_unaligned_le32(v + 0x24); + if (length >= 0xb4) + *rx_rate_kbps = get_unaligned_le32(v + 0xb0); + return true; + } + pos += 4 + length; + } + return false; +} + +static bool cca_survey_read(const u8 *payload, size_t size, u8 primary_chan, + u8 fw_band, u8 bw_primary, bool bw_valid, + u64 *active_ms, u64 *busy_ms, u64 *tx_ms) +{ + size_t pos = 0; + + while (pos + 4 <= size) { + u16 tag = get_unaligned_le16(payload + pos); + u16 length = get_unaligned_le16(payload + pos + 2); + + if (length > size - pos - 4) + break; + if (tag == 0xf903 && length >= 0x1cc) { + const u8 *v = payload + pos + 4; + u64 dur0, dur1, active_us, busy_us, tx_us, act_ms; + unsigned int i; + int slot = -1; + + for (i = 0; i < 12; i++) { + const u8 *ch = v + 0x18c + i * 4; + + if (ch[0] != primary_chan || + (ch[1] & 3) != fw_band) + continue; + if (bw_valid && ch[2] == bw_primary) { + slot = i; + break; + } + if (slot < 0) + slot = i; + } + if (slot < 0) + return false; + dur0 = get_unaligned_le64(v + 0x1bc); + dur1 = get_unaligned_le64(v + 0x1c4); + active_us = dur0 + dur1; + if (active_us < dur0) + return false; + act_ms = div_u64(active_us, 1000); + if (!act_ms) + return false; + tx_us = (u64)get_unaligned_le32(v + 0x90 + slot * 4) + + get_unaligned_le32(v + 0x150 + slot * 4); + busy_us = (u64)get_unaligned_le32(v + 0x30 + slot * 4) + + get_unaligned_le32(v + 0x60 + slot * 4) + + get_unaligned_le32(v + 0xf0 + slot * 4) + + get_unaligned_le32(v + 0x120 + slot * 4) + + tx_us; + *active_ms = act_ms; + *busy_ms = min(div_u64(busy_us, 1000), act_ms); + *tx_ms = min(div_u64(tx_us, 1000), *busy_ms); + return true; + } + pos += 4 + length; + } + return false; +} + +static bool tx_power_rate_read(const u8 *payload, size_t size, + struct rate_info *rate, bool *rate_valid, + int *tx_power_dbm, bool *tx_power_valid) +{ + static const u8 preamble_mode[13] = { + 0, 1, 2, 3, 4, 5, 5, 5, 5, 4, 4, 6, 6, + }; + static const u16 legacy_rates[16] = { + [0] = 10, [1] = 20, [2] = 55, [3] = 110, + [8] = 480, [9] = 240, [10] = 120, [11] = 60, + [12] = 540, [13] = 360, [14] = 180, [15] = 90, + }; + static const u8 bw_map[4] = { + RATE_INFO_BW_20, + RATE_INFO_BW_40, + RATE_INFO_BW_80, + RATE_INFO_BW_160, + }; + const u8 *v = NULL; + size_t pos = 0, stride = 0; + int best_mode = -1; + unsigned int i; + + *rate_valid = false; + *tx_power_valid = false; + while (pos + 4 <= size) { + u16 tag = get_unaligned_le16(payload + pos); + u16 length = get_unaligned_le16(payload + pos + 2); + + if (length > size - pos - 4) + break; + if (tag == 0xf920 && (length == 280 || length == 320)) { + v = payload + pos + 4; + stride = length / 10; + break; + } + pos += 4 + length; + } + if (!v) + return false; + + for (i = 0; i < 5; i++) { + const u8 *rec = v + i * stride; + u8 qdb = rec[16]; + u8 pre = rec[17]; + u8 raw_mcs = rec[20]; + u8 nss = rec[21] + 1; + u8 hw_bw = rec[24]; + u8 hw_gi = rec[27]; + struct rate_info cand = {}; + u8 mode; + + if (!(rec[14] | rec[15] | qdb | pre | raw_mcs | + rec[21] | hw_bw | hw_gi)) + continue; + if (qdb && qdb < 128 && !*tx_power_valid) { + *tx_power_dbm = qdb / 4; + *tx_power_valid = true; + } + if (pre >= ARRAY_SIZE(preamble_mode)) + continue; + mode = preamble_mode[pre]; + if ((int)mode <= best_mode) + continue; + if (mode <= 2) { + if (raw_mcs >= ARRAY_SIZE(legacy_rates) || + !legacy_rates[raw_mcs]) + continue; + cand.legacy = legacy_rates[raw_mcs]; + } else if (hw_bw >= ARRAY_SIZE(bw_map)) { + continue; + } else if (mode == 3) { + if (hw_bw > 1) + continue; + if (raw_mcs <= 7 && nss >= 1 && nss <= 4) + cand.mcs = (nss - 1) * 8 + raw_mcs; + else if (raw_mcs <= 31) + cand.mcs = raw_mcs; + else + continue; + cand.bw = bw_map[hw_bw]; + cand.flags = RATE_INFO_FLAGS_MCS; + if (hw_gi) + cand.flags |= RATE_INFO_FLAGS_SHORT_GI; + } else if (mode == 4) { + if (raw_mcs > 11 || nss < 1 || nss > 4) + continue; + cand.bw = bw_map[hw_bw]; + cand.flags = RATE_INFO_FLAGS_VHT_MCS; + cand.mcs = raw_mcs; + cand.nss = nss; + if (hw_gi) + cand.flags |= RATE_INFO_FLAGS_SHORT_GI; + } else if (mode == 5) { + if (raw_mcs > 11 || nss < 1 || nss > 4 || + hw_gi > NL80211_RATE_INFO_HE_GI_3_2) + continue; + cand.bw = bw_map[hw_bw]; + cand.flags = RATE_INFO_FLAGS_HE_MCS; + cand.mcs = raw_mcs; + cand.nss = nss; + cand.he_gi = hw_gi; + } else { + if (raw_mcs > 13 || nss < 1 || nss > 4 || + hw_gi > NL80211_RATE_INFO_EHT_GI_3_2) + continue; + cand.bw = bw_map[hw_bw]; + cand.flags = RATE_INFO_FLAGS_EHT_MCS; + cand.mcs = raw_mcs; + cand.nss = nss; + cand.eht_gi = hw_gi; + } + best_mode = mode; + *rate = cand; + *rate_valid = true; + if (qdb && qdb < 128) { + *tx_power_dbm = qdb / 4; + *tx_power_valid = true; + } + } + return *rate_valid || *tx_power_valid; +} + +static bool beacon_stats_read(const u8 *payload, size_t size, + u32 *rx_beacons, u32 *missed_beacons) +{ + size_t pos = 0; + + while (pos + 4 <= size) { + u16 tag = get_unaligned_le16(payload + pos); + u16 length = get_unaligned_le16(payload + pos + 2); + + if (length > size - pos - 4) + break; + if (tag == 0xf915 && length >= 0x140) { + const u8 *v = payload + pos + 4; + + *missed_beacons = get_unaligned_le32(v + 0xb8); + *rx_beacons = get_unaligned_le32(v + 0x13c); + return true; + } + pos += 4 + length; + } + return false; +} + +static bool phy_dump_read(const u8 *payload, size_t size, u8 fw_band, + s8 chain_rssi[2], u8 *chains, + s8 *noise, bool *noise_valid) +{ + const u8 *v = NULL; + size_t pos = 0; + unsigned int core; + + *chains = 0; + *noise_valid = false; + while (pos + 4 <= size) { + u16 tag = get_unaligned_le16(payload + pos); + u16 length = get_unaligned_le16(payload + pos + 2); + + if (length > size - pos - 4) + break; + if (tag == 0xf923 && length >= 800) { + v = payload + pos + 4; + break; + } + pos += 4 + length; + } + if (!v) + return false; + + core = fw_band == 0 ? 0 : 1; + for (;;) { + const u8 *base = v + core * 400; + u64 sum0 = 0, sum1 = 0, nsum0 = 0, nsum1 = 0; + u32 cnt0 = 0, cnt1 = 0, ncnt0 = 0, ncnt1 = 0; + s32 ntotal = 0; + u32 nvalid = 0; + unsigned int i; + + for (i = 0; i < 10; i++) { + const u8 *rec = base + i * 40; + u32 n0, n1, r0, r1; + + if (!get_unaligned_le32(rec + 0x24)) + continue; + n0 = get_unaligned_le32(rec + 0x08); + n1 = get_unaligned_le32(rec + 0x0c); + r0 = get_unaligned_le32(rec + 0x18); + r1 = get_unaligned_le32(rec + 0x1c); + if (n0) { + nsum0 += n0; + ncnt0++; + } + if (n1) { + nsum1 += n1; + ncnt1++; + } + if (r0) { + sum0 += r0; + cnt0++; + } + if (r1) { + sum1 += r1; + cnt1++; + } + } + if (cnt0) { + s64 val = -(s64)div_u64(sum0, cnt0 * 2); + + if (val >= -120 && val <= -1) { + chain_rssi[0] = val; + *chains |= BIT(0); + } + } + if (cnt1) { + s64 val = -(s64)div_u64(sum1, cnt1 * 2); + + if (val >= -120 && val <= -1) { + chain_rssi[1] = val; + *chains |= BIT(1); + } + } + if (ncnt0) { + s64 val = -(s64)div_u64(nsum0, ncnt0 * 2); + + if (val >= -120 && val <= -40) { + ntotal += val; + nvalid++; + } + } + if (ncnt1) { + s64 val = -(s64)div_u64(nsum1, ncnt1 * 2); + + if (val >= -120 && val <= -40) { + ntotal += val; + nvalid++; + } + } + if (nvalid) { + *noise = ntotal / (s32)nvalid; + *noise_valid = true; + } + if (*chains || *noise_valid || !core) + break; + core = 0; + } + return *chains != 0 || *noise_valid; +} + +static bool fw_version_read(const u8 *payload, size_t size, + char *out, size_t out_size) +{ + u16 api_major, api_minor; + size_t i, len = 0; + char fw[40]; + + if (!out_size || size < 8) + return false; + api_major = get_unaligned_le16(payload + 4); + api_minor = get_unaligned_le16(payload + 6); + if (size > 8) { + size_t max_len = min_t(size_t, size - 8, sizeof(fw) - 1); + + for (i = 0; i < max_len && payload[8 + i]; i++) { + u8 c = payload[8 + i]; + + if (c < 0x20 || c > 0x7e) + continue; + if (!len && c == ' ') + continue; + fw[len++] = c; + } + while (len && fw[len - 1] == ' ') + len--; + fw[len] = '\0'; + } + if (len) { + snprintf(out, out_size, "%s", fw); + return true; + } + if (!api_major && !api_minor) + return false; + snprintf(out, out_size, "api %u.%u", api_major, api_minor); + return true; +} + +static bool country_code_valid(const u8 *code, size_t size) +{ + if (size < 2) + return false; + if (code[0] < 'A' || code[0] > 'Z' || code[1] < 'A' || code[1] > 'Z') + return false; + if ((code[0] == 'X' && (code[1] == 'Z' || code[1] == 'X')) || + (code[0] == 'Z' && code[1] == 'Z')) + return false; + return true; +} + +static void disconnect_reason_decode(const u8 *packet, size_t size, + u16 *reason, bool *local) +{ + if (size >= 31) { + u8 code = packet[24]; + + if (code) { + *reason = code; + *local = false; + } else { + *reason = WLAN_REASON_UNSPECIFIED; + *local = true; + } + } else { + *reason = WLAN_REASON_UNSPECIFIED; + *local = false; + } +} + +static bool link_channel_decode(struct wiphy *wiphy, u8 center_chan, + u8 band_flags, u8 bw_primary, + struct cfg80211_chan_def *chandef) +{ + static const s8 primary_offset[15] = { + 0, + 2, -2, + 6, 2, -2, -6, + 14, 10, 6, 2, -2, -6, -10, -14, + }; + enum nl80211_chan_width width; + enum nl80211_band band; + int primary, primary_freq, center_freq; + struct ieee80211_channel *chan; + + if (!center_chan || bw_primary >= ARRAY_SIZE(primary_offset)) + return false; + switch (band_flags & 3) { + case 0: + band = NL80211_BAND_2GHZ; + break; + case 1: + band = NL80211_BAND_5GHZ; + break; + case 2: + band = NL80211_BAND_6GHZ; + break; + default: + return false; + } + if (bw_primary == 0) + width = NL80211_CHAN_WIDTH_20; + else if (bw_primary <= 2) + width = NL80211_CHAN_WIDTH_40; + else if (bw_primary <= 6) + width = NL80211_CHAN_WIDTH_80; + else + width = NL80211_CHAN_WIDTH_160; + + primary = (int)center_chan + primary_offset[bw_primary]; + if (primary <= 0 || primary > 255) + return false; + primary_freq = ieee80211_channel_to_frequency(primary, band); + center_freq = ieee80211_channel_to_frequency(center_chan, band); + if (!primary_freq || !center_freq) + return false; + chan = ieee80211_get_channel(wiphy, primary_freq); + if (!chan) + return false; + + memset(chandef, 0, sizeof(*chandef)); + chandef->chan = chan; + chandef->width = width; + chandef->center_freq1 = center_freq; + return cfg80211_chandef_valid(chandef); +} + +static void record_link_status_reply(struct centauri_wifi *wifi, + const u8 *packet, u16 size) +{ + /* Interface status reply has 8 fixed bytes after the 24-byte header. */ + unsigned int pos = 32; + + wifi->link_status_size = size; + while (pos + 4 <= size) { + u16 tag = get_unaligned_le16(packet + pos); + u16 length = get_unaligned_le16(packet + pos + 2); + const u8 *value = packet + pos + 4; + unsigned int i; + + if (length > size - pos - 4) + break; + if (wifi->link_tlv_count < ARRAY_SIZE(wifi->link_tags)) { + wifi->link_tags[wifi->link_tlv_count] = tag; + wifi->link_lengths[wifi->link_tlv_count++] = length; + } + if (tag == 268 && length >= 115) { + s8 signal = (s8)value[1]; + + if (signal) { + wifi->station_signal = signal; + wifi->station_signal_valid = true; + } + wifi->station_chandef_valid = + link_channel_decode(wifi->wiphy, value[16], + value[17], value[18], + &wifi->station_chandef); + if (wifi->station_chandef_valid) + wifi->station_bw_primary = value[18]; + } + /* MLO status report, TLVs 377/378: keep link count/channel/role only. */ + if (tag == 377 && length >= 64 && value[0] <= 3) { + wifi->link_count = value[0]; + wifi->link_status_valid = true; + for (i = 0; i < value[0]; i++) { + u16 chanspec = get_unaligned_le16(value + 26 + 17 * i); + + wifi->link_prime[i] = value[25 + 17 * i]; + wifi->link_channel[i] = chanspec & 0xff; + wifi->link_band[i] = (chanspec >> 14) & 3; + } + } + if (tag == 378 && length >= 9) { + wifi->emlsr_valid = true; + wifi->emlsr_mode = value[0]; + } + pos += 4 + length; + } +} + static void drain_control(struct centauri_wifi *wifi, bool pm_command) { unsigned int ring; @@ -656,7 +1406,13 @@ static void drain_control(struct centauri_wifi *wifi, bool pm_command) get_unaligned_le32(packet + 0x2c), get_unaligned_le32(packet + 0x30), get_unaligned_le32(packet + 0x34)); - if (command == 0x80000003 || command == 0x80000004) + if (command == 0x80000003) { + disconnect_reason_decode(packet, size, + &wifi->disconnect_reason, + &wifi->disconnect_local); + wifi->disconnect_event = true; + } + if (command == 0x80000004) wifi->disconnect_event = true; if (wifi->connecting && command != 0x80000000 && command != 0x80000001 && command != 0x80000030 && @@ -669,6 +1425,11 @@ static void drain_control(struct centauri_wifi *wifi, bool pm_command) wifi->reply_sequence = sequence; wifi->reply_status = status; wifi->reply_command = command; + if (wifi->reply_buffer && sequence == wifi->reply_capture) { + wifi->reply_size = size - 24; + memcpy(wifi->reply_buffer, packet + 24, + min(wifi->reply_size, wifi->reply_capacity)); + } if (command == 10) { wifi->country_valid = !status && size >= 27; if (wifi->country_valid) @@ -679,44 +1440,8 @@ static void drain_control(struct centauri_wifi *wifi, bool pm_command) if (wifi->tx_profile_valid) memcpy(wifi->tx_profile, packet + 24, sizeof(wifi->tx_profile)); } - if (command == 20 && !status) { - /* getInterfaceStatus skips its 32-byte fixed response. */ - unsigned int pos = 32; - - wifi->link_status_size = size; - while (pos + 4 <= size) { - u16 tag = get_unaligned_le16(packet + pos); - u16 length = get_unaligned_le16(packet + pos + 2); - u8 *value = packet + pos + 4; - unsigned int i; - - if (length > size - pos - 4) - break; - if (wifi->link_tlv_count < ARRAY_SIZE(wifi->link_tags)) { - wifi->link_tags[wifi->link_tlv_count] = tag; - wifi->link_lengths[wifi->link_tlv_count++] = length; - } - /* MLO status report, TLVs 377/378. - * Keep only link count/channel/role. Do not retain MACs. - */ - if (tag == 377 && length >= 64 && value[0] <= 3) { - wifi->link_count = value[0]; - wifi->link_status_valid = true; - for (i = 0; i < value[0]; i++) { - u16 chanspec = get_unaligned_le16(value + 26 + 17 * i); - - wifi->link_prime[i] = value[25 + 17 * i]; - wifi->link_channel[i] = chanspec & 0xff; - wifi->link_band[i] = (chanspec >> 14) & 3; - } - } - if (tag == 378 && length >= 9) { - wifi->emlsr_valid = true; - wifi->emlsr_mode = value[0]; - } - pos += 4 + length; - } - } + if (command == 20 && !status) + record_link_status_reply(wifi, packet, size); if (command == 0xb0001) record_stats_reply(wifi, packet, size, status); if (command == 0xf0025) { @@ -749,12 +1474,14 @@ static void drain(struct centauri_wifi *wifi) drain_control(wifi, false); } -static int command_control(struct centauri_wifi *wifi, u32 id, const void *payload, - size_t size, bool pm_command) +static int command_reply_control(struct centauri_wifi *wifi, u32 id, const void *payload, + size_t size, void *reply, size_t *reply_size, + bool pm_command) { u16 slot = wifi->command_head, sequence = ++wifi->sequence; u8 *packet, *descriptor; unsigned long deadline = jiffies + msecs_to_jiffies(1500); + int ret; if (READ_ONCE(wifi->transport_paused) && !pm_command) return -EBUSY; @@ -779,13 +1506,18 @@ static int command_control(struct centauri_wifi *wifi, u32 id, const void *paylo wifi->command_head = (slot + 1) % ring_size[1]; index_write(wifi, 3, 1, wifi->command_head); wifi->reply_valid = false; + wifi->reply_buffer = reply; + wifi->reply_capacity = reply ? *reply_size : 0; + wifi->reply_capture = sequence; + wifi->reply_size = 0; dma_wmb(); writel(1, wifi->a.bar + 0x1004); do { drain_control(wifi, pm_command); if (readl(wifi->a.bar + 8) != 2) { centauri_recovery_request(wifi->control, "command: Alpha stage"); - return -EIO; + ret = -EIO; + goto out; } /* Bit 30 is more-data-pending, not part of the command identity. * Scan/connect replies set it before their later completion event. @@ -793,16 +1525,34 @@ static int command_control(struct centauri_wifi *wifi, u32 id, const void *paylo if (wifi->reply_valid && wifi->reply_sequence == sequence && (wifi->reply_command & ~BIT(30)) == id) { if (wifi->reply_status) - dev_info(&wifi->control->pdev->dev, - "Wi-Fi command %#x rejected: firmware_status=%u\n", - id, wifi->reply_status); - return wifi->reply_status ? -EREMOTEIO : 0; + dev_dbg(&wifi->control->pdev->dev, + "Wi-Fi command %#x rejected: firmware_status=%u\n", + id, wifi->reply_status); + if (reply) + *reply_size = wifi->reply_size; + ret = wifi->reply_status ? -EREMOTEIO : 0; + goto out; } 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]); centauri_recovery_request(wifi->control, "command timeout"); - return -ETIMEDOUT; + ret = -ETIMEDOUT; +out: + wifi->reply_buffer = NULL; + return ret; +} + +static int command_control(struct centauri_wifi *wifi, u32 id, const void *payload, + size_t size, bool pm_command) +{ + return command_reply_control(wifi, id, payload, size, NULL, NULL, pm_command); +} + +static int command_reply(struct centauri_wifi *wifi, u32 id, const void *payload, + size_t size, void *reply, size_t *reply_size) +{ + return command_reply_control(wifi, id, payload, size, reply, reply_size, false); } /* @@ -826,288 +1576,554 @@ static int command(struct centauri_wifi *wifi, u32 id, const void *payload, size return command_control(wifi, id, payload, size, false); } -static int radio_start(struct centauri_wifi *wifi) +static int station_configure(struct centauri_wifi *wifi, const u8 *address) { u8 start[5 + 4 + 3 * ETH_ALEN] = { 0, 0x0b, 0, 0, 0 }; size_t start_size = 5; - unsigned int ring, slot; + unsigned int slot; int ret; - /* The stock 26.x startInterface supplies TLV 385, length 18; the macOS 27.0 - * adapter uses 386 (board table). Firmware 0x5736c0 otherwise generates three unrelated - * random addresses. This diagnostic keeps the sole active link equal - * to the netdev identity; it does not enable multi-link association. - */ + /* Optional diagnostic: bind all three firmware link slots to the station address. */ if (station_link_addresses) { const struct centauri_board *board = centauri_board(); put_unaligned_le16(board ? board->link_tlv : 385, start + 5); put_unaligned_le16(3 * ETH_ALEN, start + 7); for (slot = 0; slot < 3; slot++) - ether_addr_copy(start + 9 + slot * ETH_ALEN, wifi->netdev->dev_addr); + ether_addr_copy(start + 9 + slot * ETH_ALEN, address); start_size = sizeof(start); } - for (ring = 1; ring <= 4; ring++) { - ret = open_pair(wifi, ring); - if (ret) - return ret; - } - for (ring = 2; ring <= 4; ring++) { - for (slot = 0; slot < ring_size[ring] - 1; slot++) - post_receive(wifi, ring, slot); - wifi->rx_head[ring] = ring_size[ring] - 1; - dma_wmb(); - index_write(wifi, 3, ring, ring_size[ring] - 1); - } - dma_wmb(); - writel(1, wifi->a.bar + 0x1004); - msleep(400); - ret = command(wifi, 0, NULL, 0); - if (!ret) - ret = command(wifi, 1, wifi->netdev->dev_addr, ETH_ALEN); + ret = command(wifi, 1, address, ETH_ALEN); if (!ret) ret = command(wifi, 2, start, start_size); - if (ret) - return ret; - msleep(1000); - wifi->configured = true; - dev_info(&wifi->control->pdev->dev, "Wi-Fi command rings ready; interface started\n"); - return 0; + return ret; } -/* The station's RSN element (body) with PSK as its only AKM, written to selected; returns its size. */ -static int select_rsn(const u8 *body, size_t size, u8 *selected) +static void firmware_report(struct centauri_wifi *wifi, u32 id, u8 *buffer, + struct debugfs_blob_wrapper *blob) { - size_t offset, tail; - u16 count; - unsigned int i; + size_t size = REPLY_SIZE; + int ret; - if (size < 8 || get_unaligned_le16(body) != 1) - return -EINVAL; - count = get_unaligned_le16(body + 6); - offset = 8 + 4 * count; - if (!count || offset + 2 > size) - return -EINVAL; - count = get_unaligned_le16(body + offset); - tail = offset + 2 + 4 * count; - if (!count || tail > size) - return -EINVAL; - for (i = 0; i < count; i++) - if (get_unaligned_be32(body + offset + 2 + 4 * i) == WLAN_AKM_SUITE_PSK) - break; - if (i == count) - return -EOPNOTSUPP; - memcpy(selected, body, offset); - put_unaligned_le16(1, selected + offset); - put_unaligned_be32(WLAN_AKM_SUITE_PSK, selected + offset + 2); - memcpy(selected + offset + 6, body + tail, size - tail); - return offset + 6 + size - tail; + ret = command_reply(wifi, id, NULL, 0, buffer, &size); + if (ret) { + dev_dbg(&wifi->control->pdev->dev, + "Wi-Fi firmware report %u unavailable: %d (status=%u)\n", + id, ret, wifi->reply_status); + size = 0; + } else if (size > REPLY_SIZE) { + dev_warn(&wifi->control->pdev->dev, + "Wi-Fi command %u reply of %zu bytes truncated\n", + id, size); + size = REPLY_SIZE; + } + blob->data = buffer; + blob->size = size; } -static void append_tlv(u8 *payload, size_t *size, u16 tag, const void *data, u16 length); +#define CHANNEL_PASSIVE BIT(4) +#define CHANNEL_RADAR BIT(6) -/* Dwell times in ms for the scan request. The macOS 27 scan adapter copies - * the first request dwell field into frame +0x20 and +0x22 and the second into +0x24 and logs - * "dwelltime active[%d, %d] passive %d". A zero passive dwell makes the J873 scan state machine - * assert in SCHED_Req_Add (the dwell it selects must not be zero) and the whole firmware traps. - */ -#define SCAN_ACTIVE_DWELL_MS 110 -#define SCAN_PASSIVE_DWELL_MS 110 +static const u8 *channel_entry(const u8 *list, size_t size, int band, + unsigned int width, unsigned int number) +{ + unsigned int count = size ? list[0] : 0, i, span = 2 * (1 << width) - 2; + + if (1 + 4 * count > size) + count = size ? (size - 1) / 4 : 0; + for (i = 0; i < count; i++) { + const u8 *entry = list + 1 + 4 * i; -/* Fixed scan request (command 0x1000f), 14 bytes, chunk number first. */ -static size_t build_scan(struct centauri_wifi *wifi, unsigned int chunk, u8 *buf) + if ((entry[1] & 3) != band || ((entry[1] >> 2) & 3) != width) + continue; + if (number + span >= entry[0] && number <= entry[0] + span) + return entry; + } + return NULL; +} + +/* Whether the list describes channels of this width in the band at all. */ +static bool channel_width_listed(const u8 *list, size_t size, int band, + unsigned int width) { - u8 fixed[14] = { 1, 0, 0, 0x80, 0, 2, 0, 0x10 }; - const struct centauri_board *board = centauri_board(); - u8 list[1 + 4 * 13], oce[5] = { 0, 0, 1, 0, 0 }; - unsigned int first = chunk * 13, n, i; - size_t size = sizeof(fixed); - - put_unaligned_le16(SCAN_ACTIVE_DWELL_MS, fixed + 8); - put_unaligned_le16(SCAN_ACTIVE_DWELL_MS, fixed + 10); - put_unaligned_le16(SCAN_PASSIVE_DWELL_MS, fixed + 12); - memcpy(buf, fixed, sizeof(fixed)); - buf[0] = chunk + 1; - if (!board || !board->scan_tlvs) - return size; - /* The macOS 27.0 scan request appends TLV 0x104, a count and then - * {channel, band (0 = 2.4 GHz, 1 = 5 GHz), 0, 0} per channel, and TLV 0x165, an OCE block - * whose flags byte always has bit 0 set. One chunk covers 13 entries of the wiphy table. - */ - n = min_t(unsigned int, 13, 13 + ARRAY_SIZE(five_ghz) - first); - list[0] = n; - for (i = 0; i < n; i++) { - const struct ieee80211_channel *channel = &wifi->channels[first + i]; - - list[1 + 4 * i] = channel->hw_value; - list[2 + 4 * i] = channel->band == NL80211_BAND_5GHZ; - list[3 + 4 * i] = 0; - list[4 + 4 * i] = 0; - } - append_tlv(buf, &size, 0x104, list, 1 + 4 * n); - append_tlv(buf, &size, 0x165, oce, sizeof(oce)); - return size; + unsigned int count = size ? list[0] : 0, i; + + if (1 + 4 * count > size) + count = size ? (size - 1) / 4 : 0; + for (i = 0; i < count; i++) { + const u8 *entry = list + 1 + 4 * i; + + if ((entry[1] & 3) == band && ((entry[1] >> 2) & 3) == width) + return true; + } + return false; } -static bool scan_transport_stopped(struct centauri_wifi *wifi) +static void channels_apply(struct centauri_wifi *wifi) { - return READ_ONCE(wifi->stopping) || - READ_ONCE(wifi->transport_paused) || - readl(wifi->a.bar + 8) != 2; + const u8 *list = wifi->hw_channels; + size_t size = wifi->hw_channels_blob.size; + unsigned int counted[3] = {}, i; + + if (!size) + return; + for (i = 0; i < CEN_CHANNELS; i++) { + struct ieee80211_channel *channel = &wifi->channels[i]; + int band = firmware_band(channel->band); + const u8 *entry = channel_entry(list, size, band, 0, + channel->hw_value); + + if (!entry) { + channel->flags |= IEEE80211_CHAN_DISABLED; + continue; + } + if (entry[1] & CHANNEL_PASSIVE) + channel->flags |= IEEE80211_CHAN_NO_IR; + if (entry[1] & CHANNEL_RADAR) + channel->flags |= IEEE80211_CHAN_RADAR; + /* + * A list of 20 MHz entries only says nothing about wider + * channels; restrict a width only where the list covers it. + */ + if (channel_width_listed(list, size, band, 1) && + !channel_entry(list, size, band, 1, channel->hw_value)) + channel->flags |= IEEE80211_CHAN_NO_HT40; + if (channel_width_listed(list, size, band, 2) && + !channel_entry(list, size, band, 2, channel->hw_value)) + channel->flags |= IEEE80211_CHAN_NO_80MHZ; + if (channel_width_listed(list, size, band, 3) && + !channel_entry(list, size, band, 3, channel->hw_value)) + channel->flags |= IEEE80211_CHAN_NO_160MHZ; + if (!(channel->flags & IEEE80211_CHAN_DISABLED)) + counted[band]++; + } + 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]); } -static int scan_wait_stop(struct centauri_wifi *wifi, u32 done) +static bool band_caps_from_firmware(struct centauri_wifi *wifi) { - unsigned long deadline = jiffies + msecs_to_jiffies(1000); + const u8 *caps = wifi->system_caps; + const u8 *ht_elem = NULL, *vht_elem = NULL, *he_elem = NULL; + size_t size = wifi->system_caps_blob.size, pos = 0, mcs; + u32 vht_cap = 0; - do { - if (scan_transport_stopped(wifi)) - return -EIO; - drain(wifi); - if (wifi->scans_done != done) - return 0; - mutex_unlock(&wifi->lock); - usleep_range(1000, 2000); - mutex_lock(&wifi->lock); - } while (time_before(jiffies, deadline)); - if (scan_transport_stopped(wifi)) - return -EIO; - drain(wifi); - return wifi->scans_done != done ? 0 : -ETIMEDOUT; + while (pos + 4 <= size) { + u16 tag = get_unaligned_le16(caps + pos); + u16 length = get_unaligned_le16(caps + pos + 2); + const u8 *value = caps + pos + 4; + + if (length > size - pos - 4) + break; + if (tag == 262 && + length >= 87 + 2 + sizeof(struct ieee80211_ht_cap) && + value[87] == WLAN_EID_HT_CAPABILITY && + value[88] == sizeof(struct ieee80211_ht_cap)) + ht_elem = value + 87; + if (tag == WLAN_EID_VHT_CAPABILITY && + length >= 2 + sizeof(struct ieee80211_vht_cap) && + value[0] == WLAN_EID_VHT_CAPABILITY && + value[1] == sizeof(struct ieee80211_vht_cap)) + vht_elem = value; + if (tag == WLAN_EID_EXTENSION && length >= 3 + 6 + 11 + 4 && + value[0] == WLAN_EID_EXTENSION && value[1] == length - 2 && + value[2] == WLAN_EID_EXT_HE_CAPABILITY) + he_elem = value; + pos += 4 + length; + } + + if (ht_elem) { + struct ieee80211_sta_ht_cap ht_cap = {}; + const struct ieee80211_ht_cap *ht = + (const struct ieee80211_ht_cap *)(ht_elem + 2); + + ht_cap.ht_supported = true; + ht_cap.cap = le16_to_cpu(ht->cap_info); + ht_cap.ampdu_factor = + ht->ampdu_params_info & IEEE80211_HT_AMPDU_PARM_FACTOR; + ht_cap.ampdu_density = + FIELD_GET(IEEE80211_HT_AMPDU_PARM_DENSITY, + ht->ampdu_params_info); + ht_cap.mcs = ht->mcs; + wifi->bands[0].ht_cap = ht_cap; + wifi->bands[1].ht_cap = ht_cap; + } + + if (vht_elem) { + struct ieee80211_sta_vht_cap *vht = &wifi->bands[1].vht_cap; + + vht_cap = get_unaligned_le32(vht_elem + 2); + vht->vht_supported = true; + vht->cap = vht_cap; + memcpy(&vht->vht_mcs, vht_elem + 6, sizeof(vht->vht_mcs)); + } + + if (!he_elem) + return false; + + { + struct ieee80211_sta_he_cap base_he = {}; + unsigned int band; + + memcpy(&base_he.he_cap_elem, he_elem + 3, + sizeof(base_he.he_cap_elem)); + mcs = he_elem[1] - 1 - sizeof(base_he.he_cap_elem); + memcpy(&base_he.he_mcs_nss_supp, + he_elem + 3 + sizeof(base_he.he_cap_elem), + min(mcs, sizeof(base_he.he_mcs_nss_supp))); + if (mcs > sizeof(base_he.he_mcs_nss_supp)) + memcpy(base_he.ppe_thres, + he_elem + 3 + sizeof(base_he.he_cap_elem) + + sizeof(base_he.he_mcs_nss_supp), + min(mcs - sizeof(base_he.he_mcs_nss_supp), + sizeof(base_he.ppe_thres))); + base_he.has_he = true; + + for (band = 0; band < 3; band++) { + struct ieee80211_sband_iftype_data *data = + &wifi->iftype_data[band]; + + memset(data, 0, sizeof(*data)); + data->types_mask = BIT(NL80211_IFTYPE_STATION); + data->he_cap = base_he; + if (band == 0) { + data->he_cap.he_cap_elem.phy_cap_info[0] &= + IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G; + data->he_cap.he_mcs_nss_supp.rx_mcs_160 = + cpu_to_le16(0xffff); + data->he_cap.he_mcs_nss_supp.tx_mcs_160 = + cpu_to_le16(0xffff); + data->he_cap.he_mcs_nss_supp.rx_mcs_80p80 = + cpu_to_le16(0xffff); + data->he_cap.he_mcs_nss_supp.tx_mcs_80p80 = + cpu_to_le16(0xffff); + } else { + data->he_cap.he_cap_elem.phy_cap_info[0] &= + ~IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G; + } + if (band == 2) { + u32 ampdu_exp = u32_get_bits(vht_cap, + IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK); + u16 capa; + + capa = u16_encode_bits(ampdu_exp, + IEEE80211_HE_6GHZ_CAP_MAX_AMPDU_LEN_EXP) | + u16_encode_bits(vht_cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK, + IEEE80211_HE_6GHZ_CAP_MAX_MPDU_LEN) | + u16_encode_bits(WLAN_HT_CAP_SM_PS_DISABLED, + IEEE80211_HE_6GHZ_CAP_SM_PS); + if (vht_cap & IEEE80211_VHT_CAP_RX_ANTENNA_PATTERN) + capa |= IEEE80211_HE_6GHZ_CAP_RX_ANTPAT_CONS; + if (vht_cap & IEEE80211_VHT_CAP_TX_ANTENNA_PATTERN) + capa |= IEEE80211_HE_6GHZ_CAP_TX_ANTPAT_CONS; + data->he_6ghz_capa.capa = cpu_to_le16(capa); + } + _ieee80211_set_sband_iftype_data(&wifi->bands[band], + data, 1); + } + } + return true; +} + +static void bands_init(struct centauri_wifi *wifi) +{ + unsigned int i, n = 0, c; + + for (i = 0; i < ARRAY_SIZE(band_plan); i++) { + enum nl80211_band band = band_plan[i].band; + struct ieee80211_supported_band *sband = + &wifi->bands[firmware_band(band)]; + + if (!sband->channels) + sband->channels = wifi->channels + n; + for (c = band_plan[i].first; c <= band_plan[i].last; + c += band_plan[i].step) { + wifi->channels[n].band = band; + wifi->channels[n].hw_value = c; + wifi->channels[n].center_freq = + ieee80211_channel_to_frequency(c, band); + wifi->channels[n].max_power = 20; + sband->n_channels++; + n++; + } + sband->band = band; + sband->bitrates = band == NL80211_BAND_2GHZ ? rates : rates + 4; + sband->n_bitrates = band == NL80211_BAND_2GHZ ? + ARRAY_SIZE(rates) : + ARRAY_SIZE(rates) - 4; + } + wifi->wiphy->bands[NL80211_BAND_2GHZ] = &wifi->bands[0]; + wifi->wiphy->bands[NL80211_BAND_5GHZ] = &wifi->bands[1]; + if (band_caps_from_firmware(wifi)) { + wifi->wiphy->bands[NL80211_BAND_6GHZ] = &wifi->bands[2]; + } else { + dev_info(&wifi->control->pdev->dev, + "Wi-Fi: no HE capabilities from firmware; 6 GHz band disabled\n"); + } } -static int scan_stop_chunk(struct centauri_wifi *wifi, u32 done) +static int radio_start(struct centauri_wifi *wifi) { + size_t size = sizeof(wifi->stats_reply); + unsigned int ring, slot; int ret; - if (scan_transport_stopped(wifi)) - return -EIO; - ret = command(wifi, 65537, NULL, 0); - if (wifi->scans_done != done) - return 0; - if (ret == -EREMOTEIO && wifi->reply_status == 342) - return 0; + for (ring = 1; ring <= 4; ring++) { + ret = open_pair(wifi, ring); + if (ret) + return ret; + } + for (ring = 2; ring <= 4; ring++) { + for (slot = 0; slot < ring_size[ring] - 1; slot++) + post_receive(wifi, ring, slot); + wifi->rx_head[ring] = ring_size[ring] - 1; + dma_wmb(); + index_write(wifi, 3, ring, ring_size[ring] - 1); + } + dma_wmb(); + writel(1, wifi->a.bar + 0x1004); + msleep(400); + ret = command_reply(wifi, 0, NULL, 0, wifi->stats_reply, &size); + if (!ret) { + fw_version_read(wifi->stats_reply, + min(size, sizeof(wifi->stats_reply)), + wifi->wiphy->fw_version, + sizeof(wifi->wiphy->fw_version)); + ret = station_configure(wifi, wifi->netdev->dev_addr); + } if (ret) return ret; - return scan_wait_stop(wifi, done); + msleep(1000); + firmware_report(wifi, 19, wifi->hw_channels, &wifi->hw_channels_blob); + firmware_report(wifi, 8, wifi->system_caps, &wifi->system_caps_blob); + wifi->configured = true; + if (wifi->wiphy->fw_version[0]) + dev_info(&wifi->control->pdev->dev, + "Wi-Fi command rings ready; interface started (firmware %s)\n", + wifi->wiphy->fw_version); + else + dev_info(&wifi->control->pdev->dev, + "Wi-Fi command rings ready; interface started\n"); + return 0; } -static void scan_worker(struct work_struct *work) +static void append_tlv(u8 *payload, size_t *size, u16 tag, const void *data, u16 length); + +#define CEN_SCAN_CHANNELS_MAX 59 +#define CEN_SCAN_SSIDS_MAX 4 +#define SCAN_STATUS_INVALID_HANDLE 342 +#define SCAN_ACTIVE_MIN_MS 40 +#define SCAN_LISTEN_MS 110 + +static int scan_abort_cmd(struct centauri_wifi *wifi, u8 scan_id) { - struct centauri_wifi *wifi = container_of(work, struct centauri_wifi, scan_work); - struct cfg80211_scan_info info = {}; - struct cfg80211_scan_request *request; - u8 scan[14 + 4 + 1 + 4 * 13 + 4 + 5]; - size_t scan_size; - unsigned int chunk, chunks = DIV_ROUND_UP(13 + ARRAY_SIZE(five_ghz), 13); - u32 channels_start, beacons_start; - int ret = 0; + int ret = command(wifi, 65537, &scan_id, sizeof(scan_id)); - mutex_lock(&wifi->lock); - channels_start = wifi->channels_seen; - beacons_start = wifi->beacons_seen; - for (chunk = 0; chunk < chunks && !ret; chunk++) { - u32 channels = wifi->channels_seen, done = wifi->scans_done, seen = channels; - unsigned int count = min_t(unsigned int, 13, - 13 + ARRAY_SIZE(five_ghz) - chunk * 13); - unsigned long deadline, progress; - bool stopped = false, refused = false; + if (ret == -EREMOTEIO && wifi->reply_status == SCAN_STATUS_INVALID_HANDLE) + return 0; + return ret; +} - if (scan_transport_stopped(wifi)) { - ret = -EIO; - break; +static int run_scan_tlvs(struct centauri_wifi *wifi, + struct cfg80211_scan_request *request) +{ + u8 scan[14 + 5 + 4 * CEN_SCAN_CHANNELS_MAX + 9 + + CEN_SCAN_SSIDS_MAX * (4 + 2 + IEEE80211_MAX_SSID_LEN)]; + unsigned int next = 0; + + if (!request || !request->n_channels || + request->n_ssids > CEN_SCAN_SSIDS_MAX) + return -EINVAL; + while (next < request->n_channels) { + u8 channels[1 + 4 * CEN_SCAN_CHANNELS_MAX] = {}; + u8 options[5] = { 0, 0, 1, 0, 0 }; + unsigned int count = 0, s; + u32 done = wifi->scans_done; + size_t size = 14; + unsigned long deadline; + u8 scan_id; + int ret; + bool abort_sent = false; + + if (READ_ONCE(wifi->scan_abort)) + return -ECANCELED; + memset(scan, 0, sizeof(scan)); + wifi->scan_sequence = (wifi->scan_sequence % 31) + 1; + scan_id = wifi->scan_sequence; + scan[0] = scan_id; + scan[3] = 0x80; + scan[5] = 2; + scan[7] = 0x10; + put_unaligned_le16(SCAN_ACTIVE_MIN_MS, scan + 8); + put_unaligned_le16(SCAN_LISTEN_MS, scan + 10); + put_unaligned_le16(SCAN_LISTEN_MS, scan + 12); + + while (next < request->n_channels && count < CEN_SCAN_CHANNELS_MAX) { + struct ieee80211_channel *channel = request->channels[next++]; + unsigned int number; + int band; + + if (channel->flags & IEEE80211_CHAN_DISABLED) + continue; + band = firmware_band(channel->band); + if (band < 0) + return band; + number = ieee80211_frequency_to_channel(channel->center_freq); + if (!number || number > 255) + return -EINVAL; + channels[1 + 4 * count] = number; + channels[2 + 4 * count] = band; + if (channel->flags & + (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) + channels[2 + 4 * count] |= CHANNEL_PASSIVE; + if (channel->flags & IEEE80211_CHAN_RADAR) + channels[2 + 4 * count] |= CHANNEL_RADAR; + count++; } - if (READ_ONCE(wifi->scan_abort)) { - ret = -ECANCELED; - break; + if (!count) + continue; + channels[0] = count; + append_tlv(scan, &size, 260, channels, 1 + 4 * count); + append_tlv(scan, &size, 357, options, sizeof(options)); + for (s = 0; s < request->n_ssids; s++) { + const struct cfg80211_ssid *ssid = &request->ssids[s]; + u8 entry[2 + IEEE80211_MAX_SSID_LEN]; + + if (ssid->ssid_len > IEEE80211_MAX_SSID_LEN) + return -EINVAL; + if (ssid->ssid_len) + scan[2] = 1; + entry[0] = ssid->ssid_len ? 5 : 4; + entry[1] = ssid->ssid_len; + memcpy(entry + 2, ssid->ssid, ssid->ssid_len); + append_tlv(scan, &size, 258, entry, 2 + ssid->ssid_len); } - scan_size = build_scan(wifi, chunk, scan); - ret = command(wifi, 65551, scan, scan_size); + ret = command(wifi, 65551, scan, size); if (ret) - break; - /* - * The firmware reports every channel it finishes (event 0x30) and then the scan - * (event 0x05). This loop used to stop each chunk after its 8th channel, so a scan - * covered 24 of the 38 channels (J815 boot 2d103457: "channels=24"): 2.4 GHz 9-13, - * 5 GHz 100-116 and 153-165 were never scanned and 149 was the last one kept. A - * 5 GHz BSS there came and went between scans, and the supplicant joined 2.4 GHz. - * The chunk now runs to its own end. It's stopped only once all of its channels - * have reported without a scan event, when it goes quiet for a second after some - * progress (a channel the firmware skips under the country rules), at its - * deadline, or on an abort. A chunk that ends early keeps what it found. - */ - deadline = jiffies + - msecs_to_jiffies(max(8000U, count * 2 * SCAN_PASSIVE_DWELL_MS + 2000)); - progress = jiffies; + return ret; + deadline = jiffies + msecs_to_jiffies(max(8000U, count * 220 + 2000)); do { - if (scan_transport_stopped(wifi)) { - if (!READ_ONCE(wifi->stopping) && !READ_ONCE(wifi->transport_paused) && - readl(wifi->a.bar + 8) != 2) - centauri_recovery_request(wifi->control, "scan: Alpha stage"); - ret = -EIO; + drain(wifi); + if (READ_ONCE(wifi->stopping) || readl(wifi->a.bar + 8) != 2) { + centauri_recovery_request(wifi->control, "scan: Alpha stage"); + return -EIO; + } + if (wifi->scans_done != done) break; + if (READ_ONCE(wifi->scan_abort) && !abort_sent) { + ret = scan_abort_cmd(wifi, scan_id); + if (ret) + return ret; + abort_sent = true; + } + usleep_range(1000, 2000); + } while (time_before(jiffies, deadline)); + if (wifi->scans_done == done) { + dev_warn(&wifi->control->pdev->dev, + "Wi-Fi scan of %u channels: no completion event\n", + count); + scan_abort_cmd(wifi, scan_id); + return -ETIMEDOUT; + } + if (READ_ONCE(wifi->scan_abort)) + return -ECANCELED; + } + return 0; +} + +static int run_scan_legacy(struct centauri_wifi *wifi) +{ + u8 scan[14] = { 0, 0, 0, 0x80, 0, 2, 0, 0x10 }; + unsigned int chunk; + + put_unaligned_le16(SCAN_LISTEN_MS, scan + 8); + put_unaligned_le16(SCAN_LISTEN_MS, scan + 10); + put_unaligned_le16(SCAN_LISTEN_MS, scan + 12); + for (chunk = 0; chunk < 3; chunk++) { + u32 channels = wifi->channels_seen, done = wifi->scans_done; + u32 seen = channels; + unsigned long deadline, progress; + bool stopped = false, refused = false; + u8 scan_id; + int ret; + + if (READ_ONCE(wifi->scan_abort)) + return -ECANCELED; + wifi->scan_sequence = (wifi->scan_sequence % 31) + 1; + scan_id = wifi->scan_sequence; + scan[0] = scan_id; + ret = command(wifi, 65551, scan, sizeof(scan)); + if (ret) + return ret; + deadline = jiffies + msecs_to_jiffies(8000); + progress = jiffies; + do { + drain(wifi); + if (READ_ONCE(wifi->stopping) || readl(wifi->a.bar + 8) != 2) { + centauri_recovery_request(wifi->control, "scan: Alpha stage"); + return -EIO; } drain(wifi); if (wifi->scans_done != done) break; if (READ_ONCE(wifi->scan_abort)) { - int stop_ret = stopped ? scan_wait_stop(wifi, done) : - scan_stop_chunk(wifi, done); - - ret = stop_ret == -EIO ? -EIO : -ECANCELED; - break; + if (!stopped) + scan_abort_cmd(wifi, scan_id); + return -ECANCELED; } if (wifi->channels_seen != seen) { seen = wifi->channels_seen; progress = jiffies; } if (!stopped && - (seen - channels >= count || + (seen - channels >= 13 || (seen != channels && time_after(jiffies, progress + msecs_to_jiffies(1000))))) { stopped = true; - /* - * A refused stop (firmware status 342 on J815) means the chunk - * had ended already: keep its results and go on. - */ - if (command(wifi, 65537, NULL, 0)) { - dev_dbg(&wifi->control->pdev->dev, - "Wi-Fi scan chunk %u: stop refused (status %u), chunk over\n", - chunk, wifi->reply_status); + if (scan_abort_cmd(wifi, scan_id)) { refused = true; break; } - /* The stop's own scan event ends the wait. */ deadline = jiffies + msecs_to_jiffies(1000); } mutex_unlock(&wifi->lock); usleep_range(1000, 2000); mutex_lock(&wifi->lock); } while (time_before(jiffies, deadline)); - if (!ret && !refused && wifi->scans_done == done) { - if (scan_transport_stopped(wifi)) { - ret = -EIO; - } else { - drain(wifi); - if (wifi->scans_done == done) { - dev_warn(&wifi->control->pdev->dev, - "Wi-Fi scan chunk %u: %u of %u channels, no scan event%s\n", - chunk, wifi->channels_seen - channels, count, - stopped ? " after the stop" : ""); - ret = stopped ? -ETIMEDOUT : - scan_stop_chunk(wifi, done); - } - } - } + if (!refused && wifi->scans_done == done && !stopped) + scan_abort_cmd(wifi, scan_id); } + return 0; +} + +static void scan_worker(struct work_struct *work) +{ + struct centauri_wifi *wifi = container_of(work, struct centauri_wifi, scan_work); + const struct centauri_board *board = centauri_board(); + struct cfg80211_scan_info info = {}; + struct cfg80211_scan_request *request; + u32 channels_start, beacons_start; + int ret = 0; + + mutex_lock(&wifi->lock); + channels_start = wifi->channels_seen; + beacons_start = wifi->beacons_seen; + if (READ_ONCE(wifi->scan_abort) || READ_ONCE(wifi->stopping)) + ret = -ECANCELED; + else if (board && board->scan_tlvs) + ret = run_scan_tlvs(wifi, wifi->scan_request); + else + ret = run_scan_legacy(wifi); info.aborted = !!ret || READ_ONCE(wifi->scan_abort) || READ_ONCE(wifi->stopping); request = wifi->scan_request; wifi->scan_request = NULL; mutex_unlock(&wifi->lock); - dev_info(&wifi->control->pdev->dev, "Wi-Fi scan finished: rc=%d channels=%u of %zu beacons=%u\n", - ret, wifi->channels_seen - channels_start, 13 + ARRAY_SIZE(five_ghz), - wifi->beacons_seen - beacons_start); + dev_dbg(&wifi->control->pdev->dev, + "Wi-Fi scan finished: rc=%d channels=%u beacons=%u\n", + ret, wifi->channels_seen - channels_start, + wifi->beacons_seen - beacons_start); if (request) cfg80211_scan_done(request, &info); } @@ -1118,11 +2134,14 @@ static int wifi_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request) int ret = 0; mutex_lock(&wifi->lock); - if (!wifi->configured) + if (READ_ONCE(wifi->stopping)) + ret = -ENODEV; + else if (!wifi->configured) ret = -ENETDOWN; else if (wifi->scan_request || wifi->connecting) ret = -EBUSY; else { + WRITE_ONCE(wifi->scan_abort, false); wifi->scan_request = request; schedule_work(&wifi->scan_work); } @@ -1130,6 +2149,41 @@ static int wifi_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request) return ret; } +static void wifi_abort_scan(struct wiphy *wiphy, struct wireless_dev *wdev) +{ + struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); + + if (wdev == &wifi->wdev) + WRITE_ONCE(wifi->scan_abort, true); +} + +static void cancel_scan(struct centauri_wifi *wifi) +{ + struct cfg80211_scan_info info = { .aborted = true }; + struct cfg80211_scan_request *request; + + WRITE_ONCE(wifi->scan_abort, true); + cancel_work_sync(&wifi->scan_work); + mutex_lock(&wifi->lock); + request = wifi->scan_request; + wifi->scan_request = NULL; + mutex_unlock(&wifi->lock); + if (request) + cfg80211_scan_done(request, &info); +} + +static void cancel_connect_locked(struct centauri_wifi *wifi) +{ + if (!wifi->connecting) + return; + wifi->connecting = false; + wifi->connected = false; + memzero_explicit(wifi->connect_payload, sizeof(wifi->connect_payload)); + cfg80211_connect_timeout(wifi->netdev, wifi->bssid, wifi->request_ie, + wifi->request_ie_size, GFP_KERNEL, + NL80211_TIMEOUT_UNSPECIFIED); +} + static int rsn_caps_offset(const u8 *body, size_t size) { size_t offset = 6; @@ -1155,144 +2209,380 @@ static void append_tlv(u8 *payload, size_t *size, u16 tag, const void *data, u16 *size += 4 + length; } +static int rsn_akm_offset(const u8 *body, size_t size) +{ + size_t offset; + u16 count; + + if (size < 8 || get_unaligned_le16(body) != 1) + return -EINVAL; + count = get_unaligned_le16(body + 6); + offset = 8 + 4 * count; + if (!count || offset + 2 > size) + return -EINVAL; + count = get_unaligned_le16(body + offset); + if (!count || offset + 2 + 4 * count > size) + return -EINVAL; + return offset; +} + +static bool rsn_lists_akm(const u8 *body, size_t size, u32 akm) +{ + int offset = rsn_akm_offset(body, size); + unsigned int i; + + if (offset < 0) + return false; + for (i = 0; i < get_unaligned_le16(body + offset); i++) + if (get_unaligned_be32(body + offset + 2 + 4 * i) == akm) + return true; + return false; +} + +#define CONNECT_AUTH_OPEN 0 +#define CONNECT_AUTH_SAE 3 +#define CONNECT_AKM_NONE 0 +#define CONNECT_AKM_PSK 4 +#define CONNECT_AKM_PSK_SHA256 6 +#define CONNECT_AKM_SAE 8 +#define CONNECT_CIPHER_NONE 0 +#define CONNECT_CIPHER_CCMP 4 +#define CONNECT_CIPHER_GCMP 7 +#define CONNECT_CIPHER_GCMP_256 8 + +static int firmware_pairwise_cipher(u32 suite) +{ + switch (suite) { + case WLAN_CIPHER_SUITE_CCMP: + return CONNECT_CIPHER_CCMP; + case WLAN_CIPHER_SUITE_GCMP: + return CONNECT_CIPHER_GCMP; + case WLAN_CIPHER_SUITE_GCMP_256: + return CONNECT_CIPHER_GCMP_256; + default: + return -EOPNOTSUPP; + } +} + +static u8 firmware_akm(u32 akm) +{ + switch (akm) { + case WLAN_AKM_SUITE_PSK: + return CONNECT_AKM_PSK; + case WLAN_AKM_SUITE_PSK_SHA256: + return CONNECT_AKM_PSK_SHA256; + case WLAN_AKM_SUITE_SAE: + return CONNECT_AKM_SAE; + default: + return CONNECT_AKM_NONE; + } +} + +static bool cipher_pair_supported(u32 pairwise, u32 group, u32 akm) +{ + if (firmware_pairwise_cipher(pairwise) < 0) + return false; + if (group == pairwise) + return true; + if (group == WLAN_CIPHER_SUITE_TKIP) + return akm != WLAN_AKM_SUITE_SAE && + pairwise == WLAN_CIPHER_SUITE_CCMP; + if (group == WLAN_CIPHER_SUITE_CCMP || group == WLAN_CIPHER_SUITE_GCMP) + return pairwise == WLAN_CIPHER_SUITE_GCMP || + pairwise == WLAN_CIPHER_SUITE_GCMP_256; + return false; +} + +static int select_rsn(const u8 *body, size_t size, u32 akm, u32 pairwise, + u32 group, u8 *selected) +{ + size_t tail; + int offset; + + offset = rsn_akm_offset(body, size); + if (offset < 0) + return offset; + tail = offset + 2 + 4 * get_unaligned_le16(body + offset); + if (tail + 2 > size) + return -EINVAL; + if (!cipher_pair_supported(pairwise, group, akm)) + return -EOPNOTSUPP; + if (get_unaligned_be32(body + 2) != group || + get_unaligned_le16(body + 6) != 1 || + get_unaligned_be32(body + 8) != pairwise) + return -EOPNOTSUPP; + if (!rsn_lists_akm(body, size, akm) && tail + 4 <= size && + get_unaligned_le16(body + tail + 2)) + return -EOPNOTSUPP; + memcpy(selected, body, offset); + put_unaligned_le16(1, selected + offset); + put_unaligned_be32(akm, selected + offset + 2); + memcpy(selected + offset + 6, body + tail, size - tail); + return offset + 6 + size - tail; +} + +static const u32 connect_akms[] = { + WLAN_AKM_SUITE_SAE, + WLAN_AKM_SUITE_PSK_SHA256, + WLAN_AKM_SUITE_PSK, +}; + +static bool akm_usable(const struct cfg80211_crypto_settings *crypto, u32 akm) +{ + unsigned int i; + + for (i = 0; i < crypto->n_akm_suites; i++) + if (crypto->akm_suites[i] == akm) + return akm == WLAN_AKM_SUITE_SAE ? crypto->sae_pwd_len : + !!crypto->psk; + return false; +} + +static u32 connect_akm(const struct cfg80211_crypto_settings *crypto, + const u8 *own, size_t own_size, const u8 *target, + size_t target_size, bool mfpc, u32 pairwise, u32 group) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(connect_akms); i++) + if (akm_usable(crypto, connect_akms[i]) && + cipher_pair_supported(pairwise, group, connect_akms[i]) && + rsn_lists_akm(own, own_size, connect_akms[i])) + return connect_akms[i]; + for (i = 0; i < ARRAY_SIZE(connect_akms); i++) { + if (!akm_usable(crypto, connect_akms[i]) || + !cipher_pair_supported(pairwise, group, connect_akms[i])) + continue; + if (connect_akms[i] == WLAN_AKM_SUITE_SAE && !mfpc) + continue; + if (!target_size || + rsn_lists_akm(target, target_size, connect_akms[i])) + return connect_akms[i]; + } + return 0; +} + +static size_t target_rsn(struct wiphy *wiphy, + const struct cfg80211_connect_params *params, u8 *body) +{ + const struct element *element; + struct cfg80211_bss *bss; + size_t size = 0; + + bss = cfg80211_get_bss(wiphy, params->channel ?: params->channel_hint, + params->bssid ?: params->bssid_hint, params->ssid, + params->ssid_len, IEEE80211_BSS_TYPE_ESS, + IEEE80211_PRIVACY_ON); + if (!bss) + return 0; + rcu_read_lock(); + element = ieee80211_bss_get_elem(bss, WLAN_EID_RSN); + if (element) { + size = element->datalen; + memcpy(body, element->data, size); + } + rcu_read_unlock(); + cfg80211_put_bss(wiphy, bss); + return size; +} + static int wifi_connect(struct wiphy *wiphy, struct net_device *netdev, struct cfg80211_connect_params *params) { - /* The stock 26.x join emits a 12-byte TLV 376. The macOS 27.0.1 - * adapter writes tag 0x178 with length 14: the MLD configuration bytes - * that its default initialisation fills with the defaults below, bytes - * 12 and 13 being 0 and the flags byte (bits from the connection parameters, 0 by default). - * Preserve the unspecified-link masks; all-zero is not the non-MLO default. - */ static const u8 mld_defaults[14] = { 1, 0, 1, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 3, 0, 0, }; - /* Firmware connection-context defaults (aciw 0x511ee8). TLV 0xf913 - * replaces all five values. Sending zeros arms Supp_AddTimer with a - * zero duration and aborts WPA before message 2 can be transmitted. - */ + /* Firmware connection timer budgets (ms): probe, auth, assoc, EAPOL, retry. */ static const __le32 connection_timeouts[] = { cpu_to_le32(260), cpu_to_le32(6000), cpu_to_le32(2000), cpu_to_le32(15000), cpu_to_le32(300), }; struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); - struct cfg80211_bss *bss; - const struct cfg80211_bss_ies *ies; - const u8 *rsn, *ap_rsn; - u8 rsn_body[255], ssid[35], zero[20] = {}, pmf; + const struct centauri_board *board = centauri_board(); + const u8 *bssid = params->bssid ?: params->bssid_hint; + struct ieee80211_channel *channel = params->channel ?: params->channel_hint; + struct cfg80211_bss *bss = NULL; + const u8 *rsn = NULL; + u8 rsn_body[255], target[255], ssid[35]; u8 *payload = wifi->connect_payload; - size_t size = 130, rsn_size; - u16 caps = 0; - int ret = 0, offset, i; - - /* - * wpa_supplicant without SME lists every AKM the network allows - * (NetworkManager's "WPA-PSK WPA-PSK-SHA256" gives PSK and PSK-SHA256). - * Accept any list that offers PSK and send the firmware PSK alone, as - * the T6050 driver does (select_rsn). - */ - for (i = 0; i < params->crypto.n_akm_suites; i++) - if (params->crypto.akm_suites[i] == WLAN_AKM_SUITE_PSK) - break; - if (!params->ssid_len || params->ssid_len > 32 || !params->crypto.psk || - i == params->crypto.n_akm_suites || - params->crypto.n_ciphers_pairwise != 1 || params->crypto.ciphers_pairwise[0] != WLAN_CIPHER_SUITE_CCMP || - params->crypto.cipher_group != WLAN_CIPHER_SUITE_CCMP || params->ie_len > sizeof(wifi->request_ie)) { - dev_info(&wifi->control->pdev->dev, - "connect refused: ssid_len=%zu psk=%d akms=%d [%08x %08x] pairwise=%d [%08x] group=%08x ie_len=%zu\n", - params->ssid_len, !!params->crypto.psk, params->crypto.n_akm_suites, - params->crypto.n_akm_suites > 0 ? params->crypto.akm_suites[0] : 0, - params->crypto.n_akm_suites > 1 ? params->crypto.akm_suites[1] : 0, - params->crypto.n_ciphers_pairwise, - params->crypto.n_ciphers_pairwise ? params->crypto.ciphers_pairwise[0] : 0, - params->crypto.cipher_group, params->ie_len); + size_t size = 130, rsn_size = 0, rsn_prefix, rsn_tail; + u8 join_mode = (board && board->scan_tlvs) ? 5 : 0; + u8 roaming = 0; + u8 password[2 + SAE_PASSWORD_MAX_LEN]; + int ret = 0, offset, band, fw_cipher = CONNECT_CIPHER_NONE; + bool open_net, sae = false, mfpc = false; + u32 akm = 0, pairwise = 0, group = 0; + + if (!params->ssid_len || params->ssid_len > IEEE80211_MAX_SSID_LEN || + params->crypto.sae_pwd_len > SAE_PASSWORD_MAX_LEN || + params->ie_len > sizeof(wifi->request_ie)) { + dev_dbg(&wifi->control->pdev->dev, + "connect refused: ssid_len=%zu sae_pwd_len=%u ie_len=%zu\n", + params->ssid_len, params->crypto.sae_pwd_len, + params->ie_len); return -EOPNOTSUPP; } - rsn = cfg80211_find_ie(WLAN_EID_RSN, params->ie, params->ie_len); - if (!rsn) { - dev_info(&wifi->control->pdev->dev, "connect refused: no RSN element in the request\n"); - return -EINVAL; - } - ret = select_rsn(rsn + 2, rsn[1], rsn_body); - if (ret < 0 || rsn_caps_offset(rsn_body, ret) < 0) { - dev_info(&wifi->control->pdev->dev, "connect refused: RSN element %*ph not usable (%d)\n", - rsn[1] + 2, rsn, ret); - return ret < 0 ? ret : -EINVAL; + open_net = !params->privacy && !params->crypto.wpa_versions && + !params->crypto.n_akm_suites && + !params->crypto.n_ciphers_pairwise && + !params->crypto.cipher_group; + if (open_net) { + if (params->auth_type != NL80211_AUTHTYPE_OPEN_SYSTEM && + params->auth_type != NL80211_AUTHTYPE_AUTOMATIC) + return -EOPNOTSUPP; + } else { + if (params->crypto.n_ciphers_pairwise != 1) + return -EOPNOTSUPP; + pairwise = params->crypto.ciphers_pairwise[0]; + group = params->crypto.cipher_group; + fw_cipher = firmware_pairwise_cipher(pairwise); + if (fw_cipher < 0) + return -EOPNOTSUPP; + rsn = cfg80211_find_ie(WLAN_EID_RSN, params->ie, params->ie_len); + if (!rsn) + return -EINVAL; + offset = rsn_caps_offset(rsn + 2, rsn[1]); + if (offset < 0) + return -EINVAL; + mfpc = get_unaligned_le16(rsn + 2 + offset) & BIT(7); + akm = connect_akm(¶ms->crypto, rsn + 2, rsn[1], target, + target_rsn(wiphy, params, target), mfpc, + pairwise, group); + ret = akm ? select_rsn(rsn + 2, rsn[1], akm, pairwise, group, + rsn_body) : + -EOPNOTSUPP; + if (ret < 0) + return ret; + rsn_size = ret; + sae = akm == WLAN_AKM_SUITE_SAE; + ret = 0; } - rsn_size = ret; - ret = 0; mutex_lock(&wifi->lock); - if (!wifi->configured || wifi->connecting || wifi->connected || wifi->scan_request) { - ret = -EBUSY; + if (READ_ONCE(wifi->stopping)) { + ret = -ENODEV; goto out; } - bss = cfg80211_get_bss(wiphy, params->channel, params->bssid, - params->ssid, params->ssid_len, IEEE80211_BSS_TYPE_ESS, IEEE80211_PRIVACY_ON); - if (!bss) { - ret = -ENOENT; + if (!wifi->configured || wifi->connecting || wifi->connected || + wifi->scan_request) { + ret = -EBUSY; goto out; } - /* Match the measured successful J813 association shape. The firmware - * expects the AP RSN capability bits even with the legal single-AKM STA IE. - */ - rcu_read_lock(); - ies = rcu_dereference(bss->ies); - ap_rsn = ies ? cfg80211_find_ie(WLAN_EID_RSN, ies->data, ies->len) : NULL; - if (ap_rsn) { - offset = rsn_caps_offset(ap_rsn + 2, ap_rsn[1]); - if (offset >= 0) - caps = get_unaligned_le16(ap_rsn + 2 + offset); + if (bssid && channel) { + memcpy(wifi->bssid, bssid, ETH_ALEN); + } else { + bss = cfg80211_get_bss(wiphy, channel, bssid, + params->ssid, params->ssid_len, + IEEE80211_BSS_TYPE_ESS, + open_net ? IEEE80211_PRIVACY_OFF : + IEEE80211_PRIVACY_ON); + if (!bss) { + ret = -ENOENT; + goto out; + } + memcpy(wifi->bssid, bss->bssid, ETH_ALEN); + channel = bss->channel; + cfg80211_put_bss(wiphy, bss); } - rcu_read_unlock(); - offset = rsn_caps_offset(rsn_body, rsn_size); - put_unaligned_le16(caps, rsn_body + offset); - pmf = !!(caps & BIT(7)); + ret = firmware_band(channel->band); + if (ret < 0) + goto out; + band = ret; + ret = 0; + if (channel->band == NL80211_BAND_6GHZ && (open_net || !sae || !mfpc)) { + ret = -EOPNOTSUPP; + goto out; + } + wifi->connect_channel = channel; + wifi->connect_privacy = !open_net; + memcpy(wifi->ssid, params->ssid, params->ssid_len); + wifi->ssid_len = params->ssid_len; memset(payload, 0, sizeof(wifi->connect_payload)); payload[0] = 1; - memcpy(payload + 1, bss->bssid, ETH_ALEN); - memcpy(wifi->bssid, bss->bssid, ETH_ALEN); - payload[7] = ieee80211_frequency_to_channel(bss->channel->center_freq); - /* Firmware band field: chanspec 0xc000 (5 GHz) is band 1 and - * 0x4000 (6 GHz) is band 2. - */ - payload[8] = bss->channel->band == NL80211_BAND_2GHZ ? 0 : 1; - payload[0x47] = 4; - /* The firmware's fixed security field is 4 for CCMP. - * The RSN IE does not replace this fixed cipher field. - */ - put_unaligned_le16(4, payload + 0x48); - put_unaligned_le16(32, payload + 0x4a); - memcpy(payload + 0x4c, params->crypto.psk, 32); - if (verify_pmk) - memcpy(wifi->expected_pmk, params->crypto.psk, 32); - append_tlv(payload, &size, 304, zero, 1); + memcpy(payload + 1, wifi->bssid, ETH_ALEN); + payload[7] = ieee80211_frequency_to_channel(channel->center_freq); + payload[8] = band; + payload[0x46] = sae ? CONNECT_AUTH_SAE : CONNECT_AUTH_OPEN; + payload[0x47] = open_net ? CONNECT_AKM_NONE : firmware_akm(akm); + put_unaligned_le16(fw_cipher, payload + 0x48); + if (!open_net && !sae) { + put_unaligned_le16(32, payload + 0x4a); + memcpy(payload + 0x4c, params->crypto.psk, 32); + if (verify_pmk) + memcpy(wifi->expected_pmk, params->crypto.psk, 32); + } + append_tlv(payload, &size, 304, &join_mode, 1); append_tlv(payload, &size, 63763, connection_timeouts, sizeof(connection_timeouts)); ssid[0] = 1; ssid[1] = 4; ssid[2] = params->ssid_len; memcpy(ssid + 3, params->ssid, params->ssid_len); append_tlv(payload, &size, 342, ssid, 3 + params->ssid_len); - append_tlv(payload, &size, WLAN_EID_RSN, rsn_body, rsn_size); - /* The fixed field carries the PMK. The firmware accepts a join with the PMK alone (measured); the macOS TLV 313 carries the - * passphrase and is not sent. - */ - append_tlv(payload, &size, 316, &pmf, 1); + if (!open_net) { + append_tlv(payload, &size, WLAN_EID_RSN, rsn_body, rsn_size); + if (sae) { + put_unaligned_le16(params->crypto.sae_pwd_len, password); + memcpy(password + 2, params->crypto.sae_pwd, + params->crypto.sae_pwd_len); + append_tlv(payload, &size, 313, password, + 2 + params->crypto.sae_pwd_len); + memzero_explicit(password, sizeof(password)); + } + append_tlv(payload, &size, 316, &roaming, 1); + } append_tlv(payload, &size, 376, mld_defaults, sizeof(mld_defaults)); wifi->connect_size = size; - memcpy(wifi->request_ie, params->ie, params->ie_len); - wifi->request_ie_size = params->ie_len; + if (open_net) { + if (params->ie_len) + memcpy(wifi->request_ie, params->ie, params->ie_len); + wifi->request_ie_size = params->ie_len; + } else { + rsn_prefix = rsn - params->ie; + rsn_tail = rsn_prefix + 2 + rsn[1]; + memcpy(wifi->request_ie, params->ie, rsn_prefix); + wifi->request_ie[rsn_prefix] = WLAN_EID_RSN; + wifi->request_ie[rsn_prefix + 1] = rsn_size; + memcpy(wifi->request_ie + rsn_prefix + 2, rsn_body, rsn_size); + memcpy(wifi->request_ie + rsn_prefix + 2 + rsn_size, + params->ie + rsn_tail, params->ie_len - rsn_tail); + wifi->request_ie_size = params->ie_len - rsn[1] + rsn_size; + } + WRITE_ONCE(wifi->connect_abort, false); wifi->connecting = true; wifi->connect_status = 0; + wifi->disconnect_reason = 0; + wifi->disconnect_local = false; wifi->associated = wifi->pmk_seen = wifi->pmk_matches = false; wifi->connect_event = wifi->disconnect_event = false; - cfg80211_put_bss(wiphy, bss); schedule_work(&wifi->connect_work); out: mutex_unlock(&wifi->lock); return ret; } +static struct cfg80211_bss *connected_bss(struct centauri_wifi *wifi) +{ + struct cfg80211_bss *bss; + u8 ie[2 + IEEE80211_MAX_SSID_LEN]; + + bss = cfg80211_get_bss(wifi->wiphy, wifi->connect_channel, wifi->bssid, + NULL, 0, IEEE80211_BSS_TYPE_ESS, + IEEE80211_PRIVACY_ANY); + if (bss) + return bss; + ie[0] = WLAN_EID_SSID; + ie[1] = wifi->ssid_len; + memcpy(ie + 2, wifi->ssid, wifi->ssid_len); + return cfg80211_inform_bss(wifi->wiphy, wifi->connect_channel, + CFG80211_BSS_FTYPE_UNKNOWN, wifi->bssid, 0, + WLAN_CAPABILITY_ESS | + (wifi->connect_privacy ? + WLAN_CAPABILITY_PRIVACY : + 0), + 100, ie, 2 + wifi->ssid_len, -95 * 100, + GFP_KERNEL); +} + /* Without the host power state the firmware treats the host as asleep, powers the PHYs down and traps about 15 s after the * join. Same three commands as the host_wakeup and tx_config debugfs files; called with wifi->lock held. */ @@ -1335,6 +2625,11 @@ static void connect_worker(struct work_struct *work) int ret; mutex_lock(&wifi->lock); + if (READ_ONCE(wifi->connect_abort) || READ_ONCE(wifi->stopping)) { + cancel_connect_locked(wifi); + mutex_unlock(&wifi->lock); + return; + } if (auto_rings) { static const unsigned int ring_ids[] = { 76, 24, 15, 12 }; unsigned int n; @@ -1355,8 +2650,12 @@ static void connect_worker(struct work_struct *work) command_status = wifi->reply_status; memzero_explicit(wifi->connect_payload, sizeof(wifi->connect_payload)); deadline = jiffies + msecs_to_jiffies(15000); - while (!ret && !wifi->connect_event && time_before(jiffies, deadline)) { + while (!ret && !wifi->connect_event && + !READ_ONCE(wifi->connect_abort) && !READ_ONCE(wifi->stopping) && + time_before(jiffies, deadline)) { drain(wifi); + if (wifi->disconnect_event) + ret = -ENOTCONN; if (link_diagnostics && wifi->associated && !queried_link) { unsigned int i; int link_ret; @@ -1407,50 +2706,90 @@ static void connect_worker(struct work_struct *work) ret = -EIO; usleep_range(1000, 2000); } - if (!ret && !wifi->connect_event) + if (READ_ONCE(wifi->connect_abort) || READ_ONCE(wifi->stopping)) + ret = -ECANCELED; + else if (!ret && !wifi->connect_event) ret = -ETIMEDOUT; status = !ret && wifi->connect_status == 0 ? WLAN_STATUS_SUCCESS : WLAN_STATUS_UNSPECIFIED_FAILURE; wifi->connecting = false; wifi->connected = status == WLAN_STATUS_SUCCESS; + wifi->station_signal_valid = false; + wifi->station_noise_valid = false; + wifi->station_tx_retrans_valid = false; + wifi->station_tx_retrans_base_valid = false; + wifi->station_tx_fail_valid = false; + wifi->station_tx_fail_base_valid = false; + wifi->station_beacon_valid = false; + wifi->station_beacon_base_valid = false; + wifi->station_cca_valid = false; + wifi->station_tx_rate_valid = false; + wifi->station_tx_power_valid = false; + wifi->station_rx_rate_valid = false; + wifi->station_chip_tx_rate_valid = false; + wifi->station_chains = 0; + wifi->station_phy_noise_valid = false; + wifi->station_chandef_valid = false; + wifi->station_signal_at = jiffies - HZ; + wifi->station_noise_at = jiffies - HZ; + wifi->station_counters_at = jiffies - HZ; + wifi->station_cca_at = jiffies - HZ; + wifi->station_tx_info_at = jiffies - HZ; + wifi->station_beacon_at = jiffies - HZ; + wifi->station_phy_dump_at = jiffies - HZ; + if (wifi->connected) { + wifi->connected_at = jiffies; + wifi->station_start = wifi->netdev->stats; + } if (wifi->connected && !roam_scan) { int roam_ret = command(wifi, 0x20016, NULL, 0); - dev_info(&wifi->control->pdev->dev, "roam scan off: rc=%d status=%u\n", roam_ret, wifi->reply_status); + dev_dbg(&wifi->control->pdev->dev, "roam scan off: rc=%d status=%u\n", + roam_ret, wifi->reply_status); } memzero_explicit(wifi->expected_pmk, sizeof(wifi->expected_pmk)); - /* The command and connection completion have consumed the PMK buffer. */ + /* The command and connection completion have consumed the credential buffer. */ if (slot < ring_size[1]) memzero_explicit(wifi->a.window + TX_BUFFER + slot * BUFFER_SIZE, BUFFER_SIZE); - mutex_unlock(&wifi->lock); - dev_info(&wifi->control->pdev->dev, - "Wi-Fi authentication result: rc=%d command_status=%u event_status=%u complete_event=%d\n", - ret, command_status, wifi->connect_status, wifi->connect_event); - cfg80211_connect_result(wifi->netdev, wifi->bssid, - wifi->request_ie, wifi->request_ie_size, NULL, 0, status, GFP_KERNEL); + dev_dbg(&wifi->control->pdev->dev, + "Wi-Fi authentication result: rc=%d command_status=%u event_status=%u complete_event=%d\n", + ret, command_status, wifi->connect_status, wifi->connect_event); + if (ret == -ECANCELED || ret == -ETIMEDOUT) + cfg80211_connect_timeout(wifi->netdev, wifi->bssid, + wifi->request_ie, + wifi->request_ie_size, GFP_KERNEL, + NL80211_TIMEOUT_UNSPECIFIED); + else + cfg80211_connect_bss(wifi->netdev, wifi->bssid, + status == WLAN_STATUS_SUCCESS ? + connected_bss(wifi) : + NULL, + wifi->request_ie, wifi->request_ie_size, + NULL, 0, status, GFP_KERNEL, + NL80211_TIMEOUT_UNSPECIFIED); if (status == WLAN_STATUS_SUCCESS && auto_rings && !READ_ONCE(wifi->stopping)) { - /* The normal TX ring is opened after the connection result, as in the manual recipe (data_probe 25 after wpa completed); - * doing it after cfg80211_connect_result keeps the join inside the supplicant's timeout. - */ - int ring_ret; + int ring_ret = data_probe_locked(wifi, 25, true); - mutex_lock(&wifi->lock); - ring_ret = data_probe_locked(wifi, 25, true); - mutex_unlock(&wifi->lock); if (ring_ret && ring_ret != -EBUSY) dev_warn(&wifi->control->pdev->dev, "data ring 25 setup failed: %d\n", ring_ret); } if (status == WLAN_STATUS_SUCCESS && !READ_ONCE(wifi->stopping)) { + if (wifi->connect_privacy) + cfg80211_port_authorized(wifi->netdev, wifi->bssid, + NULL, 0, GFP_KERNEL); netif_carrier_on(wifi->netdev); if (wifi->data_probe_done & BIT(5)) netif_wake_queue(wifi->netdev); - schedule_delayed_work(&wifi->link_work, msecs_to_jiffies(20)); + mod_delayed_work(system_percpu_wq, &wifi->link_work, + msecs_to_jiffies(20)); } + mutex_unlock(&wifi->lock); } static void link_worker(struct work_struct *work) { struct centauri_wifi *wifi = container_of(to_delayed_work(work), struct centauri_wifi, link_work); - bool disconnected; + bool disconnected, local = false; + u16 reason = WLAN_REASON_UNSPECIFIED; mutex_lock(&wifi->lock); if (wifi->transport_paused) { @@ -1460,138 +2799,489 @@ static void link_worker(struct work_struct *work) drain(wifi); disconnected = wifi->disconnect_event || READ_ONCE(wifi->stopping) || readl(wifi->a.bar + 8) != 2; - if (disconnected) + if (disconnected) { + reason = wifi->disconnect_reason ?: WLAN_REASON_UNSPECIFIED; + local = wifi->disconnect_local; wifi->connected = false; + wifi->station_signal_valid = false; + wifi->station_noise_valid = false; + wifi->station_tx_retrans_valid = false; + wifi->station_tx_retrans_base_valid = false; + wifi->station_tx_fail_valid = false; + wifi->station_tx_fail_base_valid = false; + wifi->station_beacon_valid = false; + wifi->station_beacon_base_valid = false; + wifi->station_cca_valid = false; + wifi->station_tx_rate_valid = false; + wifi->station_tx_power_valid = false; + wifi->station_rx_rate_valid = false; + wifi->station_chip_tx_rate_valid = false; + wifi->station_chains = 0; + wifi->station_phy_noise_valid = false; + wifi->station_chandef_valid = false; + wifi->station_signal_at = jiffies - HZ; + wifi->station_noise_at = jiffies - HZ; + wifi->station_counters_at = jiffies - HZ; + wifi->station_cca_at = jiffies - HZ; + wifi->station_tx_info_at = jiffies - HZ; + wifi->station_beacon_at = jiffies - HZ; + wifi->station_phy_dump_at = jiffies - HZ; + } mutex_unlock(&wifi->lock); if (disconnected) { netif_stop_queue(wifi->netdev); skb_queue_purge(&wifi->data_tx_queue); netif_carrier_off(wifi->netdev); - cfg80211_disconnected(wifi->netdev, WLAN_REASON_UNSPECIFIED, NULL, 0, false, GFP_KERNEL); + cfg80211_disconnected(wifi->netdev, reason, NULL, 0, local, GFP_KERNEL); } else { schedule_delayed_work(&wifi->link_work, msecs_to_jiffies(20)); } } -/* MSI from Control or Alpha: service the data rings now instead of at the next poll. */ -void centauri_wifi_kick(struct centauri *c) +/* MSI from Control or Alpha: service the data rings now instead of at the next poll. */ +void centauri_wifi_kick(struct centauri *c) +{ + struct centauri_wifi *wifi = READ_ONCE(c->wifi); + + if (wifi && READ_ONCE(wifi->connected) && !READ_ONCE(wifi->stopping) && + !READ_ONCE(wifi->transport_paused)) + mod_delayed_work(system_percpu_wq, &wifi->link_work, 0); +} + +static int wifi_disconnect(struct wiphy *wiphy, struct net_device *netdev, u16 reason) +{ + struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); + u8 payload[16] = {}; + bool connected; + u16 reply_status; + int ret; + + WRITE_ONCE(wifi->connect_abort, true); + cancel_scan(wifi); + cancel_work_sync(&wifi->connect_work); + mutex_lock(&wifi->lock); + cancel_connect_locked(wifi); + put_unaligned_le16(reason, payload); + put_unaligned_le16(328, payload + 8); + put_unaligned_le16(4, payload + 10); + /* Preserve the firmware's default disconnect budget. */ + put_unaligned_le32(500, payload + 12); + ret = command(wifi, 131073, payload, sizeof(payload)); + reply_status = wifi->reply_status; + connected = wifi->connected; + wifi->connected = false; + wifi->station_signal_valid = false; + wifi->station_noise_valid = false; + wifi->station_tx_retrans_valid = false; + wifi->station_tx_retrans_base_valid = false; + wifi->station_tx_fail_valid = false; + wifi->station_tx_fail_base_valid = false; + wifi->station_beacon_valid = false; + wifi->station_beacon_base_valid = false; + wifi->station_cca_valid = false; + wifi->station_tx_rate_valid = false; + wifi->station_tx_power_valid = false; + wifi->station_rx_rate_valid = false; + wifi->station_chip_tx_rate_valid = false; + wifi->station_chains = 0; + wifi->station_phy_noise_valid = false; + wifi->station_chandef_valid = false; + wifi->station_signal_at = jiffies - HZ; + wifi->station_noise_at = jiffies - HZ; + wifi->station_counters_at = jiffies - HZ; + wifi->station_cca_at = jiffies - HZ; + wifi->station_tx_info_at = jiffies - HZ; + wifi->station_beacon_at = jiffies - HZ; + wifi->station_phy_dump_at = jiffies - HZ; + mutex_unlock(&wifi->lock); + cancel_delayed_work_sync(&wifi->link_work); + netif_stop_queue(netdev); + skb_queue_purge(&wifi->data_tx_queue); + netif_carrier_off(netdev); + if (connected) + cfg80211_disconnected(netdev, reason, NULL, 0, true, GFP_KERNEL); + return ret == -EREMOTEIO && reply_status == 326 ? 0 : ret; +} + +static int wifi_power_mgmt(struct wiphy *wiphy, struct net_device *netdev, + bool enabled, int timeout) +{ + struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); + u8 mode = enabled ? 0 : 3; + int ret; + + /* Power-save mode, command 7. */ + mutex_lock(&wifi->lock); + if (READ_ONCE(wifi->transport_paused)) + ret = -EBUSY; + else + ret = wifi->configured ? command(wifi, 7, &mode, sizeof(mode)) : + -ENETDOWN; + mutex_unlock(&wifi->lock); + return ret; +} + +static void wifi_regulatory(struct wiphy *wiphy, struct regulatory_request *request) +{ + struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); + u8 country[3] = { request->alpha2[0], request->alpha2[1], 1 }; + bool named = country[0] >= 'A' && country[0] <= 'Z' && + country[1] >= 'A' && country[1] <= 'Z'; + int ret; + + mutex_lock(&wifi->lock); + if (wifi->configured && named && !READ_ONCE(wifi->transport_paused) && + request->initiator != NL80211_REGDOM_SET_BY_DRIVER) { + ret = command(wifi, 9, country, sizeof(country)); + if (ret) + dev_err(&wifi->control->pdev->dev, + "Wi-Fi country %c%c update failed: %d\n", + country[0], country[1], ret); + else + firmware_report(wifi, 19, wifi->hw_channels, + &wifi->hw_channels_blob); + } + channels_apply(wifi); + mutex_unlock(&wifi->lock); +} + +static struct cfg80211_bss *wifi_joined_bss(struct centauri_wifi *wifi) +{ + if (!wifi->connected) + return NULL; + return cfg80211_get_bss(wifi->wiphy, wifi->connect_channel, wifi->bssid, + NULL, 0, IEEE80211_BSS_TYPE_ESS, + IEEE80211_PRIVACY_ANY); +} + +static int query_sta_counters(struct centauri_wifi *wifi) +{ + u8 payload[22] = {}; + size_t size = sizeof(wifi->stats_reply); + u32 retrans, tx_fail, tx_rate_kbps, rx_rate_kbps; + bool tx_fail_valid; + int ret; + + if (READ_ONCE(wifi->transport_paused)) + return -EBUSY; + wifi->station_tx_retrans_valid = false; + wifi->station_tx_fail_valid = false; + wifi->station_rx_rate_valid = false; + wifi->station_chip_tx_rate_valid = false; + wifi->station_counters_at = jiffies; + if (!wifi->configured) + return -ENETDOWN; + /* Statistics bit 1 (chip counters), TLV 363 scope 2, reply TLV 0xf901. */ + put_unaligned_le64(BIT_ULL(1), payload); + payload[8] = 1; + put_unaligned_le16(363, payload + 9); + put_unaligned_le16(9, payload + 11); + payload[21] = 2; + ret = command_reply(wifi, 0xb0001, payload, sizeof(payload), + wifi->stats_reply, &size); + if (!ret && chip_sta_counters_read(wifi->stats_reply, + min(size, sizeof(wifi->stats_reply)), + &retrans, &tx_fail, &tx_fail_valid, + &tx_rate_kbps, &rx_rate_kbps)) { + if (!wifi->station_tx_retrans_base_valid) { + wifi->station_tx_retrans_base = retrans; + wifi->station_tx_retrans_base_valid = true; + } + wifi->station_tx_retrans = retrans; + wifi->station_tx_retrans_valid = true; + if (tx_fail_valid) { + if (!wifi->station_tx_fail_base_valid) { + wifi->station_tx_fail_base = tx_fail; + wifi->station_tx_fail_base_valid = true; + } + wifi->station_tx_fail = tx_fail; + wifi->station_tx_fail_valid = true; + } + if (tx_rate_kbps >= 1000 && tx_rate_kbps <= 6500000) { + wifi->station_chip_tx_rate_kbps = tx_rate_kbps; + wifi->station_chip_tx_rate_valid = true; + } + if (rx_rate_kbps >= 1000 && rx_rate_kbps <= 6500000) { + wifi->station_rx_rate_kbps = rx_rate_kbps; + wifi->station_rx_rate_valid = true; + } + } + return ret; +} + +static int query_tx_info(struct centauri_wifi *wifi, + const struct ieee80211_channel *chan) +{ + u8 payload[22] = {}; + struct rate_info rate = {}; + bool rate_valid = false, power_valid = false; + int fw_band, dbm = 0, ret; + u64 slice; + + if (READ_ONCE(wifi->transport_paused)) + return -EBUSY; + wifi->station_tx_rate_valid = false; + wifi->station_tx_power_valid = false; + wifi->station_tx_info_at = jiffies; + if (!wifi->configured) + return -ENETDOWN; + fw_band = firmware_band(chan->band); + if (fw_band < 0) + return fw_band; + /* Statistics bit 29 (TX power/rate report), TLV 363 scope 1, reply TLV 0xf920. */ + put_unaligned_le64(BIT_ULL(29), payload); + payload[8] = 1; + put_unaligned_le16(363, payload + 9); + put_unaligned_le16(9, payload + 11); + payload[21] = 1; + slice = fw_band == 0 ? 1 : 2; + for (;;) { + size_t size = sizeof(wifi->stats_reply); + + put_unaligned_le64(slice, payload + 13); + ret = command_reply(wifi, 0xb0001, payload, sizeof(payload), + wifi->stats_reply, &size); + if (!ret && tx_power_rate_read(wifi->stats_reply, + min(size, sizeof(wifi->stats_reply)), + &rate, &rate_valid, &dbm, + &power_valid)) { + if (rate_valid) { + wifi->station_tx_rate = rate; + wifi->station_tx_rate_valid = true; + } + if (power_valid) { + wifi->station_tx_power_dbm = dbm; + wifi->station_tx_power_valid = true; + } + return 0; + } + if (slice == 1) + break; + slice = 1; + } + return ret; +} + +static int query_beacon_stats(struct centauri_wifi *wifi) +{ + u8 payload[22] = {}; + size_t size = sizeof(wifi->stats_reply); + u32 rx_beacons, missed_beacons; + int ret; + + if (READ_ONCE(wifi->transport_paused)) + return -EBUSY; + wifi->station_beacon_valid = false; + wifi->station_beacon_at = jiffies; + if (!wifi->configured) + return -ENETDOWN; + /* Statistics bit 19 (frame counters), TLV 363 scope 0, reply TLV 0xf915. */ + put_unaligned_le64(BIT_ULL(19), payload); + payload[8] = 1; + put_unaligned_le16(363, payload + 9); + put_unaligned_le16(9, payload + 11); + ret = command_reply(wifi, 0xb0001, payload, sizeof(payload), + wifi->stats_reply, &size); + if (!ret && beacon_stats_read(wifi->stats_reply, + min(size, sizeof(wifi->stats_reply)), + &rx_beacons, &missed_beacons)) { + if (!wifi->station_beacon_base_valid) { + wifi->station_beacon_rx_base = rx_beacons; + wifi->station_beacon_loss_base = missed_beacons; + wifi->station_beacon_base_valid = true; + } + wifi->station_beacon_rx = rx_beacons; + wifi->station_beacon_loss = missed_beacons; + wifi->station_beacon_valid = true; + } + return ret; +} + +static int query_phy_dump(struct centauri_wifi *wifi, + const struct ieee80211_channel *chan) { - struct centauri_wifi *wifi = READ_ONCE(c->wifi); + u8 payload[22] = {}; + size_t size = sizeof(wifi->stats_reply); + s8 chain_rssi[2] = {}, noise = 0; + bool noise_valid = false; + u8 chains = 0; + int fw_band, ret; - if (wifi && READ_ONCE(wifi->connected) && !READ_ONCE(wifi->stopping) && - !READ_ONCE(wifi->transport_paused)) - mod_delayed_work(system_percpu_wq, &wifi->link_work, 0); + if (READ_ONCE(wifi->transport_paused)) + return -EBUSY; + wifi->station_chains = 0; + wifi->station_phy_noise_valid = false; + wifi->station_phy_dump_at = jiffies; + if (!wifi->configured) + return -ENETDOWN; + fw_band = firmware_band(chan->band); + if (fw_band < 0) + return fw_band; + /* Statistics bit 32 (PHY dump), TLV 363 scope 0, reply TLV 0xf923. */ + put_unaligned_le64(BIT_ULL(32), payload); + payload[8] = 1; + put_unaligned_le16(363, payload + 9); + put_unaligned_le16(9, payload + 11); + ret = command_reply(wifi, 0xb0001, payload, sizeof(payload), + wifi->stats_reply, &size); + if (!ret && phy_dump_read(wifi->stats_reply, + min(size, sizeof(wifi->stats_reply)), + fw_band, chain_rssi, &chains, + &noise, &noise_valid)) { + wifi->station_chain_signal[0] = chain_rssi[0]; + wifi->station_chain_signal[1] = chain_rssi[1]; + wifi->station_chains = chains; + if (noise_valid) { + wifi->station_phy_noise = noise; + wifi->station_phy_noise_valid = true; + } + } + return ret; } -static int wifi_disconnect(struct wiphy *wiphy, struct net_device *netdev, u16 reason) +static void fill_station(struct centauri_wifi *wifi, struct station_info *sinfo) { - struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); - u8 payload[16] = {}; - bool connected; - int ret; + const struct net_device_stats *stats = &wifi->netdev->stats; + struct ieee80211_channel *chan; + struct cfg80211_bss *bss; - mutex_lock(&wifi->lock); - if (wifi->connecting) { - mutex_unlock(&wifi->lock); - return -EBUSY; + if (time_after_eq(jiffies, wifi->station_signal_at + HZ)) + query_link_status(wifi); + if (time_after_eq(jiffies, wifi->station_counters_at + HZ)) + query_sta_counters(wifi); + if (time_after_eq(jiffies, wifi->station_beacon_at + HZ)) + query_beacon_stats(wifi); + chan = wifi->station_chandef_valid ? wifi->station_chandef.chan : + wifi->connect_channel; + if (chan && time_after_eq(jiffies, wifi->station_tx_info_at + HZ)) + query_tx_info(wifi, chan); + if (chan && time_after_eq(jiffies, wifi->station_phy_dump_at + HZ)) + query_phy_dump(wifi, chan); + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME) | + BIT_ULL(NL80211_STA_INFO_RX_PACKETS) | + BIT_ULL(NL80211_STA_INFO_TX_PACKETS) | + BIT_ULL(NL80211_STA_INFO_RX_BYTES64) | + BIT_ULL(NL80211_STA_INFO_TX_BYTES64) | + BIT_ULL(NL80211_STA_INFO_TX_FAILED) | + BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC); + sinfo->connected_time = (jiffies - wifi->connected_at) / HZ; + sinfo->rx_packets = stats->rx_packets - wifi->station_start.rx_packets; + sinfo->tx_packets = stats->tx_packets - wifi->station_start.tx_packets; + sinfo->rx_bytes = stats->rx_bytes - wifi->station_start.rx_bytes; + sinfo->tx_bytes = stats->tx_bytes - wifi->station_start.tx_bytes; + sinfo->tx_failed = stats->tx_errors - wifi->station_start.tx_errors; + if (wifi->station_tx_fail_valid && wifi->station_tx_fail_base_valid) + sinfo->tx_failed += + wifi->station_tx_fail - wifi->station_tx_fail_base; + sinfo->rx_dropped_misc = stats->rx_dropped - wifi->station_start.rx_dropped; + if (wifi->station_tx_retrans_valid && + wifi->station_tx_retrans_base_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES); + sinfo->tx_retries = + wifi->station_tx_retrans - wifi->station_tx_retrans_base; + } + if (wifi->station_beacon_valid && wifi->station_beacon_base_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX) | + BIT_ULL(NL80211_STA_INFO_BEACON_LOSS); + sinfo->rx_beacon = + wifi->station_beacon_rx - wifi->station_beacon_rx_base; + sinfo->beacon_loss_count = + wifi->station_beacon_loss - + wifi->station_beacon_loss_base; + } + if (wifi->station_tx_rate_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); + sinfo->txrate = wifi->station_tx_rate; + } else if (wifi->station_chip_tx_rate_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE); + sinfo->txrate = (struct rate_info){ + .legacy = DIV_ROUND_CLOSEST(wifi->station_chip_tx_rate_kbps, + 100), + }; + } + if (wifi->station_rx_rate_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE); + sinfo->rxrate = (struct rate_info){ + .legacy = DIV_ROUND_CLOSEST(wifi->station_rx_rate_kbps, + 100), + }; + } + if (wifi->station_signal_valid) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); + sinfo->signal = wifi->station_signal; + } else { + bss = wifi_joined_bss(wifi); + if (bss) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); + sinfo->signal = bss->signal / 100; + cfg80211_put_bss(wifi->wiphy, bss); + } + } + if (wifi->station_chains) { + sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL); + sinfo->chains = wifi->station_chains; + sinfo->chain_signal[0] = wifi->station_chain_signal[0]; + sinfo->chain_signal[1] = wifi->station_chain_signal[1]; } - put_unaligned_le16(reason, payload); - put_unaligned_le16(328, payload + 8); - put_unaligned_le16(4, payload + 10); - /* Preserve the firmware's default disconnect budget. */ - put_unaligned_le32(500, payload + 12); - ret = command(wifi, 131073, payload, sizeof(payload)); - connected = wifi->connected; - wifi->connected = false; - mutex_unlock(&wifi->lock); - cancel_delayed_work_sync(&wifi->link_work); - netif_stop_queue(netdev); - skb_queue_purge(&wifi->data_tx_queue); - netif_carrier_off(netdev); - if (connected) - cfg80211_disconnected(netdev, reason, NULL, 0, true, GFP_KERNEL); - return ret == -EREMOTEIO && wifi->reply_status == 326 ? 0 : ret; } -static int wifi_power_mgmt(struct wiphy *wiphy, struct net_device *netdev, - bool enabled, int timeout) +static int wifi_get_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev, + int radio_idx, unsigned int link_id, int *dbm) { struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); - u8 mode = enabled ? 0 : 3; - int ret; + struct ieee80211_channel *chan; + int ret = 0; - /* Power-save mode, command 7. */ mutex_lock(&wifi->lock); - ret = wifi->configured ? command(wifi, 7, &mode, sizeof(mode)) : -ENETDOWN; + if (wdev != &wifi->wdev || !wifi->connected || + READ_ONCE(wifi->stopping) || !wifi->connect_channel) { + ret = -ENODATA; + goto out; + } + chan = wifi->station_chandef_valid ? wifi->station_chandef.chan : + wifi->connect_channel; + if (time_after_eq(jiffies, wifi->station_tx_info_at + HZ)) + query_tx_info(wifi, chan); + if (!wifi->station_tx_power_valid) { + ret = -ENODATA; + goto out; + } + *dbm = wifi->station_tx_power_dbm; +out: mutex_unlock(&wifi->lock); return ret; } -static void wifi_regulatory(struct wiphy *wiphy, struct regulatory_request *request) +static int wifi_get_station(struct wiphy *wiphy, struct wireless_dev *wdev, + const u8 *mac, struct station_info *sinfo) { struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); - u8 country[3] = { request->alpha2[0], request->alpha2[1], 1 }; - int ret; + int ret = 0; - /* Country code, command 9. Leave world-domain - * handling to cfg80211; do not invent a device country or bypass SAR. - */ - if (country[0] < 'A' || country[0] > 'Z' || country[1] < 'A' || country[1] > 'Z') - return; mutex_lock(&wifi->lock); - if (wifi->configured) { - ret = command(wifi, 9, country, sizeof(country)); - dev_info(&wifi->control->pdev->dev, - "Wi-Fi country %c%c: rc=%d\n", country[0], country[1], ret); - } + if (wdev != &wifi->wdev || !wifi->connected || + READ_ONCE(wifi->stopping) || !ether_addr_equal(mac, wifi->bssid)) + ret = -ENOENT; + else + fill_station(wifi, sinfo); mutex_unlock(&wifi->lock); + return ret; } -/* - * The joined BSS as cfg80211 knows it from the last scan. No firmware query - * for live link statistics is known yet, so its signal is the scan's. - */ -static struct cfg80211_bss *wifi_joined_bss(struct centauri_wifi *wifi) -{ - if (!wifi->connected) - return NULL; - return cfg80211_get_bss(wifi->wiphy, NULL, wifi->bssid, NULL, 0, - IEEE80211_BSS_TYPE_ESS, IEEE80211_PRIVACY_ANY); -} - -static int wifi_get_station(struct wiphy *wiphy, struct wireless_dev *wdev, - const u8 *mac, struct station_info *sinfo) +static int wifi_dump_station(struct wiphy *wiphy, struct wireless_dev *wdev, + int index, u8 *mac, struct station_info *sinfo) { struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); - struct net_device_stats *stats = &wifi->netdev->stats; - struct cfg80211_bss *bss; int ret = 0; mutex_lock(&wifi->lock); - if (!wifi->connected || !ether_addr_equal(mac, wifi->bssid)) { + if (index || wdev != &wifi->wdev || !wifi->connected || + READ_ONCE(wifi->stopping)) ret = -ENOENT; - goto out; - } - sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS) | - BIT_ULL(NL80211_STA_INFO_TX_PACKETS) | - BIT_ULL(NL80211_STA_INFO_RX_BYTES64) | - BIT_ULL(NL80211_STA_INFO_TX_BYTES64) | - BIT_ULL(NL80211_STA_INFO_TX_FAILED) | - BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC); - sinfo->rx_packets = stats->rx_packets; - sinfo->tx_packets = stats->tx_packets; - sinfo->rx_bytes = stats->rx_bytes; - sinfo->tx_bytes = stats->tx_bytes; - sinfo->tx_failed = stats->tx_errors; - sinfo->rx_dropped_misc = stats->rx_dropped; - - bss = wifi_joined_bss(wifi); - if (bss) { - sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL); - sinfo->signal = bss->signal / 100; /* mBm */ - cfg80211_put_bss(wiphy, bss); + else { + ether_addr_copy(mac, wifi->bssid); + fill_station(wifi, sinfo); } -out: mutex_unlock(&wifi->lock); return ret; } @@ -1600,28 +3290,32 @@ static int wifi_get_channel(struct wiphy *wiphy, struct wireless_dev *wdev, unsigned int link_id, struct cfg80211_chan_def *chandef) { struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); - struct cfg80211_bss *bss; - int ret = -ENODATA; + int ret = 0; mutex_lock(&wifi->lock); - bss = wifi_joined_bss(wifi); - if (bss) { - /* The join asks the firmware for a 20 MHz channel. */ - cfg80211_chandef_create(chandef, bss->channel, NL80211_CHAN_NO_HT); - cfg80211_put_bss(wiphy, bss); - ret = 0; + if (wdev != &wifi->wdev || !wifi->connected || + READ_ONCE(wifi->stopping) || !wifi->connect_channel) { + ret = -ENODATA; + goto out; } + if (time_after_eq(jiffies, wifi->station_signal_at + HZ)) + query_link_status(wifi); + if (wifi->station_chandef_valid) + *chandef = wifi->station_chandef; + else + cfg80211_chandef_create(chandef, wifi->connect_channel, + NL80211_CHAN_HT20); +out: mutex_unlock(&wifi->lock); return ret; } /* * The factory address, which the loader copies from ADT /chosen - * mac-address-wifi0 into the wifi0 alias target's local-mac-address (as on - * T6050). A board without that node, or a loader that left the placeholder, - * keeps the old per-boot random address. + * mac-address-wifi0 into the wifi0 alias target's local-mac-address. + * A board without that node keeps a random address. */ -static void centauri_wifi_set_address(struct net_device *netdev) +static void centauri_wifi_set_address(struct centauri_wifi *wifi) { struct device_node *np = of_find_node_by_path("wifi0"); u8 addr[ETH_ALEN]; @@ -1629,25 +3323,160 @@ static void centauri_wifi_set_address(struct net_device *netdev) of_node_put(np); if (!ret && is_valid_ether_addr(addr)) { - eth_hw_addr_set(netdev, addr); + eth_hw_addr_set(wifi->netdev, addr); + ether_addr_copy(wifi->wiphy->perm_addr, addr); return; } - eth_hw_addr_random(netdev); + eth_hw_addr_random(wifi->netdev); +} + +static int query_sta_noise(struct centauri_wifi *wifi) +{ + u8 payload[22] = {}; + size_t size = sizeof(wifi->stats_reply); + s8 noise; + int ret; + + if (READ_ONCE(wifi->transport_paused)) + return -EBUSY; + wifi->station_noise_valid = false; + wifi->station_noise_at = jiffies; + if (!wifi->configured) + return -ENETDOWN; + /* Statistics bit 7 (station info), TLV 363 scope 2, reply TLV 0xf908. */ + put_unaligned_le64(BIT_ULL(7), payload); + payload[8] = 1; + put_unaligned_le16(363, payload + 9); + put_unaligned_le16(9, payload + 11); + payload[21] = 2; + ret = command_reply(wifi, 0xb0001, payload, sizeof(payload), + wifi->stats_reply, &size); + if (!ret && sta_info_noise_read(wifi->stats_reply, + min(size, sizeof(wifi->stats_reply)), + &noise)) { + wifi->station_noise = noise; + wifi->station_noise_valid = true; + } + return ret; +} + +static int query_cca_survey(struct centauri_wifi *wifi, + const struct ieee80211_channel *chan) +{ + u8 payload[22] = {}; + u64 active_ms, busy_ms, tx_ms, slice; + int fw_band, ret; + + if (READ_ONCE(wifi->transport_paused)) + return -EBUSY; + wifi->station_cca_valid = false; + wifi->station_cca_at = jiffies; + if (!wifi->configured) + return -ENETDOWN; + fw_band = firmware_band(chan->band); + if (fw_band < 0) + return fw_band; + /* Statistics bit 3 (CCA report), TLV 363 slice/scope 1, reply TLV 0xf903. */ + put_unaligned_le64(BIT_ULL(3), payload); + payload[8] = 1; + put_unaligned_le16(363, payload + 9); + put_unaligned_le16(9, payload + 11); + payload[21] = 1; + slice = fw_band == 0 ? 1 : 2; + for (;;) { + size_t size = sizeof(wifi->stats_reply); + + put_unaligned_le64(slice, payload + 13); + ret = command_reply(wifi, 0xb0001, payload, sizeof(payload), + wifi->stats_reply, &size); + if (!ret && cca_survey_read(wifi->stats_reply, + min(size, sizeof(wifi->stats_reply)), + chan->hw_value, fw_band, + wifi->station_bw_primary, + wifi->station_chandef_valid, + &active_ms, &busy_ms, &tx_ms)) { + wifi->station_cca_active_ms = active_ms; + wifi->station_cca_busy_ms = busy_ms; + wifi->station_cca_tx_ms = tx_ms; + wifi->station_cca_valid = true; + return 0; + } + if (slice == 1) + break; + slice = 1; + } + return ret; +} + +static int wifi_dump_survey(struct wiphy *wiphy, struct net_device *netdev, + int idx, struct survey_info *survey) +{ + struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); + struct ieee80211_channel *chan; + int ret = 0; + + if (idx) + return -ENOENT; + mutex_lock(&wifi->lock); + if (!wifi->connected || READ_ONCE(wifi->stopping) || + !wifi->connect_channel) { + ret = -ENOENT; + goto out; + } + if (!wifi->station_chandef_valid && + time_after_eq(jiffies, wifi->station_signal_at + HZ)) + query_link_status(wifi); + if (time_after_eq(jiffies, wifi->station_noise_at + HZ)) + query_sta_noise(wifi); + chan = wifi->station_chandef_valid ? wifi->station_chandef.chan : + wifi->connect_channel; + if (time_after_eq(jiffies, wifi->station_cca_at + HZ)) + query_cca_survey(wifi, chan); + if (!wifi->station_noise_valid && + time_after_eq(jiffies, wifi->station_phy_dump_at + HZ)) + query_phy_dump(wifi, chan); + survey->channel = chan; + survey->filled = SURVEY_INFO_IN_USE; + if (wifi->station_noise_valid) { + survey->filled |= SURVEY_INFO_NOISE_DBM; + survey->noise = wifi->station_noise; + } else if (wifi->station_phy_noise_valid) { + survey->filled |= SURVEY_INFO_NOISE_DBM; + survey->noise = wifi->station_phy_noise; + } + if (wifi->station_cca_valid) { + survey->filled |= SURVEY_INFO_TIME | SURVEY_INFO_TIME_BUSY | + SURVEY_INFO_TIME_TX; + survey->time = wifi->station_cca_active_ms; + survey->time_busy = wifi->station_cca_busy_ms; + survey->time_tx = wifi->station_cca_tx_ms; + } +out: + mutex_unlock(&wifi->lock); + return ret; } static const struct cfg80211_ops wifi_ops = { .scan = wifi_scan, + .abort_scan = wifi_abort_scan, .connect = wifi_connect, .disconnect = wifi_disconnect, .set_power_mgmt = wifi_power_mgmt, + .get_tx_power = wifi_get_tx_power, .get_station = wifi_get_station, + .dump_station = wifi_dump_station, .get_channel = wifi_get_channel, + .dump_survey = wifi_dump_survey, }; static int net_open(struct net_device *netdev) { + struct centauri_wifi *wifi = *(struct centauri_wifi **)netdev_priv(netdev); + netif_carrier_off(netdev); netif_stop_queue(netdev); + skb_queue_purge(&wifi->rx_gro_queue); + napi_enable(&wifi->rx_napi); return 0; } @@ -1655,16 +3484,11 @@ static int net_stop(struct net_device *netdev) { struct centauri_wifi *wifi = *(struct centauri_wifi **)netdev_priv(netdev); + cancel_scan(wifi); netif_stop_queue(netdev); skb_queue_purge(&wifi->data_tx_queue); - /* - * Finish a running scan before cfg80211's NETDEV_DOWN. Otherwise - * cfg80211 completes it as driver-owned and stale, keeps its scan - * message, and refuses every later scan with -EBUSY. - */ - WRITE_ONCE(wifi->scan_abort, true); - flush_work(&wifi->scan_work); - WRITE_ONCE(wifi->scan_abort, false); + napi_disable(&wifi->rx_napi); + skb_queue_purge(&wifi->rx_gro_queue); return 0; } @@ -1686,6 +3510,7 @@ static netdev_tx_t net_transmit(struct sk_buff *skb, struct net_device *netdev) netif_stop_queue(netdev); return NETDEV_TX_BUSY; } + sk_pacing_shift_update(skb->sk, DATA_TX_PACING_SHIFT); skb_queue_tail(&wifi->data_tx_queue, skb); if (skb_queue_len(&wifi->data_tx_queue) >= DATA_TX_QUEUE_LIMIT) netif_stop_queue(netdev); @@ -1693,19 +3518,71 @@ static netdev_tx_t net_transmit(struct sk_buff *skb, struct net_device *netdev) return NETDEV_TX_OK; } +static int station_reconfigure(struct centauri_wifi *wifi, const u8 *address) +{ + const u8 stop[5] = {}; + int ret; + + wifi->configured = false; + ret = command(wifi, 3, stop, sizeof(stop)); + if (!ret) + ret = station_configure(wifi, address); + if (!ret) + wifi->configured = true; + return ret; +} + +static int net_set_mac_address(struct net_device *netdev, void *data) +{ + struct centauri_wifi *wifi = *(struct centauri_wifi **)netdev_priv(netdev); + struct sockaddr *address = data; + int ret, restore; + + ret = eth_prepare_mac_addr_change(netdev, data); + if (ret) + return ret; + mutex_lock(&wifi->lock); + if (READ_ONCE(wifi->stopping)) { + ret = -ENODEV; + goto out; + } + if (!wifi->configured) { + ret = -ENETDOWN; + goto out; + } + if (wifi->connecting || wifi->connected || wifi->scan_request) { + ret = -EBUSY; + goto out; + } + if (ether_addr_equal(netdev->dev_addr, address->sa_data)) + goto out; + + ret = station_reconfigure(wifi, (const u8 *)address->sa_data); + if (!ret) { + eth_commit_mac_addr_change(netdev, data); + } else { + restore = station_reconfigure(wifi, netdev->dev_addr); + if (restore) + dev_err(&wifi->control->pdev->dev, + "Wi-Fi MAC restore failed: %d (change: %d)\n", + restore, ret); + } +out: + mutex_unlock(&wifi->lock); + return ret; +} + static const struct net_device_ops net_ops = { .ndo_open = net_open, .ndo_stop = net_stop, .ndo_start_xmit = net_transmit, .ndo_validate_addr = eth_validate_addr, + .ndo_set_mac_address = net_set_mac_address, }; -/* Airship direct_memregion::map (0x1802864e0) sends opcode 8, a region ID, - * byte size and a 64-bit DVA in a 24-byte header. Alpha's region-debug is ID 1. - * Firmware 0x175340 passes those fields to DramPhyService_handle_buffer; - * InitDramLoggerMgr writes a 0x158-byte control header to the supplied buffer. - * This is distinct from the fw_debug transfer ring. Never print log payloads: - * firmware diagnostics may contain private connection material. +/* Direct memory region map (opcode 8, region ID 1) attaches a coherent + * diagnostic DRAM log ring buffer. Firmware writes a control header at the + * start of the buffer; log contents stay root-only via debugfs. */ static ssize_t fwlog_start_write(struct file *file, const char __user *input, size_t count, loff_t *position) @@ -1803,11 +3680,8 @@ static const struct file_operations fwlog_status_ops = { .llseek = default_llseek, }; -/* The exact firmware's 0x144cfc compares header +0xb0 (producer) against - * +0x10 (host consumer). InitDramLoggerMgr registers a GDMA read of +0x10. - * Only acknowledge records after a private snapshot has been verified. - * Leave three entries reserved for the firmware's three DMA descriptors - * until completion/record sequencing is independently decoded. +/* Advance the host consumer index (+0x10) against the firmware producer + * (+0xb0), keeping three descriptors reserved for in-flight DMA writes. */ static ssize_t fwlog_ack_write(struct file *file, const char __user *input, size_t count, loff_t *position) @@ -1883,12 +3757,9 @@ static const struct file_operations fwlog_buffer_ops = { .llseek = default_llseek, }; -/* The hardware data-ring callback requires the ten-byte WLAN clientdata - * extension after the 44-byte ACIPC header. Without it UMAC asserts at - * 0x11760e. Keep these empty-ring probes explicit until packet DMA is proven. - * The reserved regions below do not overlap control rings or scan buffers. +/* Data-ring open messages append a 10-byte client extension after the + * ACIPC ring header. Called with wifi->lock held. */ -/* Called with wifi->lock held. From the connect path (internal) the "connecting" flag is expected to be set. */ static int data_probe_locked(struct centauri_wifi *wifi, unsigned int id, bool internal) { u8 message[0x34 + 10] = { 0 }; @@ -2215,9 +4086,14 @@ static const struct file_operations country_ops = { static int query_link_status(struct centauri_wifi *wifi) { + if (READ_ONCE(wifi->transport_paused)) + return -EBUSY; wifi->link_status_valid = wifi->emlsr_valid = false; wifi->link_count = wifi->link_status_size = 0; wifi->link_tlv_count = 0; + wifi->station_signal_valid = false; + wifi->station_chandef_valid = false; + wifi->station_signal_at = jiffies; return wifi->configured ? command(wifi, 20, NULL, 0) : -ENETDOWN; } @@ -2598,10 +4474,49 @@ static const struct file_operations tx_power_report_ops = { .release = single_release, }; +static int data_stats_show(struct seq_file *m, void *unused) +{ + struct centauri_wifi *wifi = m->private; + + mutex_lock(&wifi->lock); + seq_printf(m, + "rx received %u reposted %u bad %u\n" + "tx submitted %u completed %u in_flight %u failed %u bad %u coalesced %u\n" + "tx queued %u capped %u cap_bypassed %u\n", + wifi->data_rx_received, wifi->data_rx_reposted, + wifi->data_rx_bad, wifi->data_tx_submitted, + wifi->data_tx_completed, + wifi->data_tx_submitted - wifi->data_tx_completed, + wifi->data_tx_failed, wifi->data_tx_bad, + wifi->data_tx_coalesced, skb_queue_len(&wifi->data_tx_queue), + wifi->data_tx_capped, wifi->data_tx_cap_bypassed); + mutex_unlock(&wifi->lock); + return 0; +} +DEFINE_SHOW_ATTRIBUTE(data_stats); + +static void wifi_hint_country(struct centauri_wifi *wifi) +{ + char alpha2[3] = {}; + bool have_country = false; + + mutex_lock(&wifi->lock); + wifi->country_valid = false; + if (wifi->configured && !command(wifi, 10, NULL, 0) && + wifi->country_valid && + country_code_valid(wifi->country, sizeof(wifi->country))) { + alpha2[0] = wifi->country[0]; + alpha2[1] = wifi->country[1]; + have_country = true; + } + mutex_unlock(&wifi->lock); + if (have_country) + regulatory_hint(wifi->wiphy, alpha2); +} + int centauri_wifi_register(struct centauri *c) { struct centauri_wifi *wifi; - unsigned int i; int ret; wifi = kzalloc(sizeof(*wifi), GFP_KERNEL); @@ -2612,6 +4527,7 @@ int centauri_wifi_register(struct centauri *c) mutex_init(&wifi->lock); mutex_init(&wifi->fwlog_lock); skb_queue_head_init(&wifi->data_tx_queue); + skb_queue_head_init(&wifi->rx_gro_queue); INIT_WORK(&wifi->scan_work, scan_worker); INIT_WORK(&wifi->connect_work, connect_worker); INIT_DELAYED_WORK(&wifi->link_work, link_worker); @@ -2626,48 +4542,43 @@ int centauri_wifi_register(struct centauri *c) } *(struct centauri_wifi **)wiphy_priv(wifi->wiphy) = wifi; *(struct centauri_wifi **)netdev_priv(wifi->netdev) = wifi; + netif_napi_add(wifi->netdev, &wifi->rx_napi, rx_poll); set_wiphy_dev(wifi->wiphy, &c->pdev->dev); wifi->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION); - wifi->wiphy->max_scan_ssids = 1; - /* - * cfg80211 refuses a scan whose IEs exceed this. wpa_supplicant 2.11 - * adds 15 bytes to every scan (Extended Capabilities and an empty - * Mesh ID); 512, wl1251's limit for the same reason, leaves room for - * optional ones such as WPS or Interworking. build_scan() follows the - * macOS request, which carries no IEs, so they are dropped. - */ + wifi->wiphy->max_scan_ssids = CEN_SCAN_SSIDS_MAX; wifi->wiphy->max_scan_ie_len = 512; + wifi->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ; + wifi->wiphy->support_mbssid = true; wifi->wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM; wifi->wiphy->reg_notifier = wifi_regulatory; wifi->wiphy->cipher_suites = cipher_suites; wifi->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites); + wifi->wiphy->akm_suites = akm_suites; + wifi->wiphy->n_akm_suites = ARRAY_SIZE(akm_suites); + wifi->wiphy->max_num_akm_suites = CFG80211_MAX_NUM_AKM_SUITES; wiphy_ext_feature_set(wifi->wiphy, NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK); - for (i = 0; i < 13; i++) { - wifi->channels[i].band = NL80211_BAND_2GHZ; - wifi->channels[i].hw_value = i + 1; - wifi->channels[i].center_freq = ieee80211_channel_to_frequency(i + 1, NL80211_BAND_2GHZ); - wifi->channels[i].max_power = 20; - } - for (i = 0; i < ARRAY_SIZE(five_ghz); i++) { - wifi->channels[13 + i].band = NL80211_BAND_5GHZ; - wifi->channels[13 + i].hw_value = five_ghz[i]; - wifi->channels[13 + i].center_freq = ieee80211_channel_to_frequency(five_ghz[i], NL80211_BAND_5GHZ); - wifi->channels[13 + i].max_power = 20; - } - wifi->bands[0].channels = wifi->channels; - wifi->bands[0].n_channels = 13; - wifi->bands[0].bitrates = rates; - wifi->bands[0].n_bitrates = ARRAY_SIZE(rates); - wifi->bands[1].channels = wifi->channels + 13; - wifi->bands[1].n_channels = ARRAY_SIZE(five_ghz); - wifi->bands[1].bitrates = rates + 4; - wifi->bands[1].n_bitrates = ARRAY_SIZE(rates) - 4; - wifi->wiphy->bands[NL80211_BAND_2GHZ] = &wifi->bands[0]; - wifi->wiphy->bands[NL80211_BAND_5GHZ] = &wifi->bands[1]; + wiphy_ext_feature_set(wifi->wiphy, NL80211_EXT_FEATURE_SAE_OFFLOAD); + centauri_wifi_set_address(wifi); + ret = radio_start(wifi); + if (ret) { + dev_err(&c->pdev->dev, "Wi-Fi ring/interface setup failed: %d\n", ret); + goto failed; + } + bands_init(wifi); ret = wiphy_register(wifi->wiphy); if (ret) goto failed; wifi->registered = true; + debugfs_create_blob("hw_channels", 0400, wifi->wiphy->debugfsdir, + &wifi->hw_channels_blob); + debugfs_create_blob("system_caps", 0400, wifi->wiphy->debugfsdir, + &wifi->system_caps_blob); + debugfs_create_file("data_stats", 0400, wifi->wiphy->debugfsdir, wifi, + &data_stats_fops); + rtnl_lock(); + channels_apply(wifi); + rtnl_unlock(); + wifi_hint_country(wifi); wifi->wdev.wiphy = wifi->wiphy; wifi->wdev.iftype = NL80211_IFTYPE_STATION; wifi->wdev.netdev = wifi->netdev; @@ -2676,16 +4587,10 @@ int centauri_wifi_register(struct centauri *c) SET_NETDEV_DEV(wifi->netdev, &c->pdev->dev); strscpy(wifi->netdev->name, "wlan%d", IFNAMSIZ); wifi->netdev->max_mtu = ETH_DATA_LEN; - centauri_wifi_set_address(wifi->netdev); ret = register_netdev(wifi->netdev); if (ret) goto failed; - ret = radio_start(wifi); - if (ret) { - dev_err(&c->pdev->dev, "Wi-Fi ring/interface setup failed: %d\n", ret); - goto failed; - } - dev_info(&c->pdev->dev, "cfg80211 interface %s ready for scan tests; packet data unavailable\n", + dev_info(&c->pdev->dev, "cfg80211 interface %s registered\n", wifi->netdev->name); wifi->data_probe_file = debugfs_create_file("data_probe", 0200, c->debug, wifi, &data_probe_ops); wifi->data_status_file = debugfs_create_file("data_status", 0400, c->debug, wifi, &data_status_ops); @@ -2714,12 +4619,9 @@ int centauri_wifi_register(struct centauri *c) } /* - * 26B123 t2026-CentauriAlpha uses BAR0 + 0x1024 for sleep_control. - * AirshipDK's ACIPC host-sleep handshake writes 3 and waits for status 3; - * wake writes 0 and waits for status 0 or 1. The peripheral-info block - * starts at context + 0x68, with sleep_status at +8, as on Beta. - * Stop every ring producer before requesting host sleep. Keep them stopped - * until the wake acknowledgment, including when suspend entry is refused. + * Alpha ACIPC host-sleep handshake uses BAR0 + 0x1024 for sleep_control + * (3 = sleep, 0 = wake) and polls peripheral_info.sleep_status at context + 0x70 + * (3 = asleep, <= 1 = awake). Keep ring producers paused across the transition. */ static u32 alpha_sleep_status(struct centauri_wifi *wifi) { @@ -2743,29 +4645,26 @@ static int alpha_transport_state(struct centauri_wifi *wifi, bool asleep) dma_rmb(); if (readl(wifi->a.bar + 8) != 2) ret = -EIO; - dev_info(&wifi->control->pdev->dev, - "TEST: Alpha ACIPC host %s: ret=%d status=%u exec=%u ipc=%u\n", - asleep ? "sleep" : "wake", ret, status, - readl(wifi->a.bar + 8), readl(wifi->a.bar + 0xc)); + dev_dbg(&wifi->control->pdev->dev, + "Alpha ACIPC host %s: ret=%d status=%u exec=%u ipc=%u\n", + asleep ? "sleep" : "wake", ret, status, + readl(wifi->a.bar + 8), readl(wifi->a.bar + 0xc)); return ret; } -/* - * WLANACIAdapter sends command 6 before ACIPC host sleep. For an interface - * already disconnected by cfg80211, its LPNA mode is firmware state 3. - * The first fields are le16 state and u8 hint. Native sleep also includes - * TLV 303 (value 1) and TLV 280 (the filtered event list); wake restores all - * events. Installed Alpha 48ef6696 rejects TLV 280 lengths >= 87 and - * event counts >= 86, so use its 85 events rather than 26B123's 91. - * The data-ring producers stay paused while this control command - * and its response use the command/management rings. - */ -static int alpha_host_power(struct centauri_wifi *wifi, bool asleep) +#define CEN_HOST_EVENTS_LEGACY 85 +#define CEN_HOST_EVENTS_FULL 91 + +static int alpha_host_power_send(struct centauri_wifi *wifi, bool asleep, + u8 event_count) { static const u8 sleep_events[] = { 24, 26, 27, 15, 51, 8, 9, 10, 84 }; - u8 payload[98] = {}, events[86] = {}, sleep_option = 1; + u8 payload[3 + 5 + 4 + 1 + CEN_HOST_EVENTS_FULL] = {}; + u8 events[1 + CEN_HOST_EVENTS_FULL] = {}; + u8 sleep_option = 1; size_t size = 3; - int i, ret; + unsigned int i; + int ret; lockdep_assert_held(&wifi->lock); if (readl(wifi->a.bar + 8) != 2) @@ -2777,16 +4676,39 @@ static int alpha_host_power(struct centauri_wifi *wifi, bool asleep) events[0] = ARRAY_SIZE(sleep_events); memcpy(events + 1, sleep_events, sizeof(sleep_events)); } else { - events[0] = sizeof(events) - 1; - for (i = 0; i < events[0]; i++) + events[0] = event_count; + for (i = 0; i < event_count; i++) events[i + 1] = i; } - append_tlv(payload, &size, 280, events, sizeof(events)); + append_tlv(payload, &size, 280, events, 1 + event_count); ret = command_control(wifi, 6, payload, size, true); - dev_info(&wifi->control->pdev->dev, - "TEST: Alpha firmware host %s: ret=%d status=%u exec=%u\n", - asleep ? "LPNA" : "awake", ret, wifi->reply_status, - readl(wifi->a.bar + 8)); + dev_dbg(&wifi->control->pdev->dev, + "Alpha firmware host %s (events=%u tlv280=%u): ret=%d status=%u exec=%u\n", + asleep ? "sleep" : "awake", events[0], 1 + event_count, ret, + wifi->reply_status, readl(wifi->a.bar + 8)); + return ret; +} + +static int alpha_host_power(struct centauri_wifi *wifi, bool asleep) +{ + u16 full_status; + int ret; + + if (asleep) + return alpha_host_power_send(wifi, true, CEN_HOST_EVENTS_LEGACY); + + ret = alpha_host_power_send(wifi, false, CEN_HOST_EVENTS_FULL); + if (ret == -EREMOTEIO && readl(wifi->a.bar + 8) == 2) { + full_status = wifi->reply_status; + ret = alpha_host_power_send(wifi, false, CEN_HOST_EVENTS_LEGACY); + if (!ret && !wifi->host_events_fallback_logged) { + wifi->host_events_fallback_logged = true; + dev_info(&wifi->control->pdev->dev, + "Alpha host wake fell back from %u to %u events (status=%u)\n", + CEN_HOST_EVENTS_FULL, CEN_HOST_EVENTS_LEGACY, + full_status); + } + } return ret; } @@ -2812,6 +4734,26 @@ static int alpha_resume_locked(struct centauri_wifi *wifi) mutex_lock(&wifi->fwlog_lock); WRITE_ONCE(wifi->transport_paused, false); mutex_unlock(&wifi->fwlog_lock); + wifi->station_signal_valid = false; + wifi->station_noise_valid = false; + wifi->station_tx_retrans_valid = false; + wifi->station_tx_fail_valid = false; + wifi->station_beacon_valid = false; + wifi->station_cca_valid = false; + wifi->station_tx_rate_valid = false; + wifi->station_tx_power_valid = false; + wifi->station_rx_rate_valid = false; + wifi->station_chip_tx_rate_valid = false; + wifi->station_chains = 0; + wifi->station_phy_noise_valid = false; + wifi->station_chandef_valid = false; + wifi->station_signal_at = jiffies - HZ; + wifi->station_noise_at = jiffies - HZ; + wifi->station_counters_at = jiffies - HZ; + wifi->station_cca_at = jiffies - HZ; + wifi->station_tx_info_at = jiffies - HZ; + wifi->station_beacon_at = jiffies - HZ; + wifi->station_phy_dump_at = jiffies - HZ; if (wifi->connected && !READ_ONCE(wifi->stopping)) { if (netif_running(wifi->netdev)) netif_wake_queue(wifi->netdev); @@ -2843,8 +4785,7 @@ int centauri_wifi_suspend(struct centauri *c) struct centauri_wifi *wifi = c->wifi; int ret, restore; - /* Only for a deep sleep: across s2idle the firmware keeps running as before. */ - if (!wifi || !READ_ONCE(alpha_sleep_untested) || !pm_suspend_via_firmware()) + if (!wifi || !READ_ONCE(alpha_sleep_untested)) return 0; mutex_lock(&wifi->lock); if (wifi->transport_paused || wifi->connected || wifi->connecting || wifi->scan_request || @@ -2886,6 +4827,8 @@ void centauri_wifi_unregister(struct centauri *c) if (!wifi) return; WRITE_ONCE(wifi->stopping, true); + WRITE_ONCE(wifi->connect_abort, true); + WRITE_ONCE(wifi->scan_abort, true); if (wifi->netdev && wifi->netdev->reg_state == NETREG_REGISTERED) netif_tx_disable(wifi->netdev); debugfs_remove(wifi->data_probe_file); @@ -2904,18 +4847,16 @@ void centauri_wifi_unregister(struct centauri *c) debugfs_remove(wifi->fwlog_status_file); debugfs_remove(wifi->fwlog_buffer_file); debugfs_remove(wifi->fwlog_ack_file); - cancel_work_sync(&wifi->scan_work); + cancel_scan(wifi); cancel_work_sync(&wifi->connect_work); cancel_delayed_work_sync(&wifi->link_work); + mutex_lock(&wifi->lock); + cancel_connect_locked(wifi); + mutex_unlock(&wifi->lock); skb_queue_purge(&wifi->data_tx_queue); - if (wifi->scan_request) { - struct cfg80211_scan_info info = { .aborted = true }; - - cfg80211_scan_done(wifi->scan_request, &info); - wifi->scan_request = NULL; - } if (wifi->netdev && wifi->netdev->reg_state == NETREG_REGISTERED) unregister_netdev(wifi->netdev); + skb_queue_purge(&wifi->rx_gro_queue); if (wifi->registered) wiphy_unregister(wifi->wiphy); if (wifi->wiphy) From 7fdab5414109f06b189f0bf7082603a50249f76a Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 21:41:16 +0700 Subject: [PATCH 15/17] wifi: apple: centauri: attach Bluetooth after a restart from debugfs A restart through rom_boot 4 brought Wi-Fi back but left hci0 away: the Bluetooth function was re-enumerated while its firmware was still down, btcentauri gave up waiting, and nothing attached it again. The fault path's restart and rom_boot 3 both attach it once the IPC runs. Do the same after rom_boot 4. Seen on the J815 test kit: "after the restart: hci0=no (60 s)", while the fault path's restart that followed brought hci0 back in 16 s. Signed-off-by: Nick --- drivers/net/wireless/apple/centauri/centauri_pci.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/net/wireless/apple/centauri/centauri_pci.c b/drivers/net/wireless/apple/centauri/centauri_pci.c index 67c399b3b443b..29dc7929ab514 100644 --- a/drivers/net/wireless/apple/centauri/centauri_pci.c +++ b/drivers/net/wireless/apple/centauri/centauri_pci.c @@ -372,7 +372,8 @@ static ssize_t centauri_boot_write(struct file *file, const char __user *buffer, } } mutex_unlock(&c->boot_lock); - if (value == 3 && !ret) { + /* A restart (4) ends with the IPC started (3): Bluetooth comes back the same way */ + if ((value == 3 || value == 4) && !ret) { int attached = centauri_attach_beta(c); if (attached == 1) From 89abbf91ec8a15df09c11e0a23c962f5daa633a7 Mon Sep 17 00:00:00 2001 From: Nick Date: Wed, 25 Nov 2026 23:25:12 +0700 Subject: [PATCH 16/17] wifi: apple: centauri: log what the PHY dump gives (test branch) On the J815 the station info never carries per-chain signal: the PHY dump the driver asks for yields no chains. Log its result at debug level, and on failure the command's return, the reply's size and its first TLVs, so the test kit can show why. To be dropped before the split. Signed-off-by: Nick --- .../wireless/apple/centauri/centauri_wifi.c | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index 33613666d3eb2..7048846f79e0a 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -3097,6 +3097,29 @@ static int query_beacon_stats(struct centauri_wifi *wifi) return ret; } +/* A PHY dump that gave nothing: the reply's TLV layout, for the test logs */ +static void phy_dump_report_failure(struct centauri_wifi *wifi, int ret, size_t size, + int fw_band) +{ + size_t n = min(size, sizeof(wifi->stats_reply)), pos = 0; + char tags[128]; + int len = 0, i; + + tags[0] = 0; + for (i = 0; i < 8 && pos + 4 <= n; i++) { + u16 tag = get_unaligned_le16(wifi->stats_reply + pos); + u16 length = get_unaligned_le16(wifi->stats_reply + pos + 2); + + len += scnprintf(tags + len, sizeof(tags) - len, " %#x/%u", tag, length); + if (length > n - pos - 4) + break; + pos += 4 + length; + } + dev_dbg(&wifi->control->pdev->dev, + "PHY dump gave no chains: ret %d, %zu bytes, band %d, TLVs:%s\n", + ret, size, fw_band, tags); +} + static int query_phy_dump(struct centauri_wifi *wifi, const struct ieee80211_channel *chan) { @@ -3128,6 +3151,9 @@ static int query_phy_dump(struct centauri_wifi *wifi, min(size, sizeof(wifi->stats_reply)), fw_band, chain_rssi, &chains, &noise, &noise_valid)) { + dev_dbg(&wifi->control->pdev->dev, + "PHY dump: band %d, chains %#x (%d/%d dBm), noise %d (valid %d)\n", + fw_band, chains, chain_rssi[0], chain_rssi[1], noise, noise_valid); wifi->station_chain_signal[0] = chain_rssi[0]; wifi->station_chain_signal[1] = chain_rssi[1]; wifi->station_chains = chains; @@ -3135,6 +3161,8 @@ static int query_phy_dump(struct centauri_wifi *wifi, wifi->station_phy_noise = noise; wifi->station_phy_noise_valid = true; } + } else { + phy_dump_report_failure(wifi, ret, size, fw_band); } return ret; } From abda0320eb3aae24579c0d82fa8e3cacf108f37b Mon Sep 17 00:00:00 2001 From: Nick Date: Thu, 26 Nov 2026 00:39:18 +0700 Subject: [PATCH 17/17] Bluetooth: btcentauri: set up voice links without the enhanced command The N1 lists Enhanced Setup Synchronous Connection in its supported commands, so the core uses it for every SCO/eSCO link, but the firmware answers it with Unsupported Feature or Parameter Value (0x11). On a MacBook Air 15" (M5) with AirPods connected, switching to the headset profile (mSBC) sent it twice, both were refused, no link came up and the microphone recorded digital silence. Set HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN so the core sends Setup Synchronous Connection with the air mode in the voice setting instead. Signed-off-by: Nick --- drivers/bluetooth/btcentauri.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/bluetooth/btcentauri.c b/drivers/bluetooth/btcentauri.c index 2df19da65aee9..fa6973f51afdc 100644 --- a/drivers/bluetooth/btcentauri.c +++ b/drivers/bluetooth/btcentauri.c @@ -1322,6 +1322,14 @@ static int cen_probe(struct pci_dev *pdev, const struct pci_device_id *id) if (fixup_adv_phy) hci_set_quirk(hdev, HCI_QUIRK_FIXUP_LE_EXT_ADV_REPORT_PHY); hci_set_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED); + /* + * The N1 lists Enhanced Setup Synchronous Connection in its supported + * commands but answers it with Unsupported Feature or Parameter Value + * (0x11), so no SCO/eSCO link comes up and a headset's microphone stays + * silent. Use Setup Synchronous Connection instead, with the air mode + * (transparent for mSBC) in the voice setting. + */ + hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN); SET_HCIDEV_DEV(hdev, &pdev->dev); hci_set_drvdata(hdev, bt); pci_set_drvdata(pdev, bt);