From 7336a3294e14023c96c7eeb9a0589967b32215ba Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 4 Dec 2026 08:34:30 +0100 Subject: [PATCH 1/4] wifi: apple: centauri: return -EALREADY while connected and wait for disconnect Like the T6050 Centauri driver (commit 019e4b0e0946), the base Centauri Wi-Fi firmware has no in-place reassociation, yet wifi_connect() refused a connect while connected with -EBUSY. On a roam attempt wpa_supplicant then marks the target BSS failed ("Association request to the driver failed"), adds it to its ignore list for 10 s, and falls back to another AP. In addition, drain_control() stripped BIT(30) (more-data-pending) before recording wifi->reply_command and wifi_disconnect() did not wait for the firmware's asynchronous disconnect completion event (0x80000004). When wpa_supplicant immediately disconnects and retries a connect, command 0x20000 races with the in-flight teardown and is rejected by firmware with status 333 (CTRL-EVENT-ASSOC-REJECT status_code=1). Fix both halves so wpa_supplicant roams cleanly: - Return -EALREADY from wifi_connect() when wifi->connected is set, so wpa_supplicant explicitly disconnects and retries the same target BSS. - Preserve wire_command (including BIT(30)) in wifi->reply_command, track wifi->disconnect_pending until event 0x80000004 arrives, wait for it in wifi_disconnect() when BIT(30) is set, and guard scan/connect/MAC/PM paths while disconnect_pending is set. - Guard link_worker() with was_connected so a worker racing with wifi_disconnect() does not emit a duplicate cfg80211_disconnected() or re-arm itself. - Add a KUnit test covering the wifi_connect()/wifi_scan() state guards. Signed-off-by: Thomas Glanzmann --- .../wireless/apple/centauri/centauri_wifi.c | 104 ++++++++++++++---- .../apple/centauri/centauri_wifi_test.c | 37 +++++++ 2 files changed, 119 insertions(+), 22 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index ab78886dcd47b..3511f616bcc00 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -131,6 +131,7 @@ struct centauri_wifi { bool transport_paused, transport_sleep_sent, host_sleep_sent; bool host_events_fallback_logged; bool connecting, connected, connect_event, disconnect_event; + bool disconnect_pending; bool connect_privacy; bool associated, pmk_seen, pmk_matches; u8 expected_pmk[32]; @@ -1411,7 +1412,7 @@ static void drain_control(struct centauri_wifi *wifi, bool pm_command) while (budget--) { u16 slot = wifi->rx_next[ring], size, status, sequence; - u32 command; + u32 wire_command, command; u8 *descriptor = wifi->a.window + TR_BASE + (ring - 1) * RING_SPAN + slot * 16; u8 *packet = wifi->a.window + rx_buffer[ring] + slot * BUFFER_SIZE; @@ -1421,7 +1422,8 @@ static void drain_control(struct centauri_wifi *wifi, bool pm_command) size = get_unaligned_le16(packet + 0x10); status = get_unaligned_le16(packet + 0xe); sequence = get_unaligned_le16(packet + 0x12); - command = get_unaligned_le32(packet + 0x14) & ~BIT(30); + wire_command = get_unaligned_le32(packet + 0x14); + command = wire_command & ~BIT(30); wifi->rx_seen[ring]++; wifi->rx_next[ring] = (slot + 1) % ring_size[ring]; if (size < 24 || size > BUFFER_SIZE) @@ -1468,8 +1470,10 @@ static void drain_control(struct centauri_wifi *wifi, bool pm_command) &wifi->disconnect_local); wifi->disconnect_event = true; } - if (command == 0x80000004) + if (command == 0x80000004) { wifi->disconnect_event = true; + wifi->disconnect_pending = false; + } if (wifi->connecting && command != 0x80000000 && command != 0x80000001 && command != 0x80000030 && command != 0x80000005 && command != 0x80000050) @@ -1480,7 +1484,7 @@ static void drain_control(struct centauri_wifi *wifi, bool pm_command) wifi->reply_valid = true; wifi->reply_sequence = sequence; wifi->reply_status = status; - wifi->reply_command = command; + wifi->reply_command = wire_command; if (wifi->reply_buffer && sequence == wifi->reply_capture) { wifi->reply_size = size - 24; memcpy(wifi->reply_buffer, packet + 24, @@ -2290,7 +2294,7 @@ static int wifi_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request) ret = -ENODEV; } else if (!wifi->configured) { ret = -ENETDOWN; - } else if (wifi->scan_request || wifi->connecting) { + } else if (wifi->scan_request || wifi->connecting || wifi->disconnect_pending) { ret = -EBUSY; } else { WRITE_ONCE(wifi->scan_abort, false); @@ -2615,8 +2619,17 @@ static int wifi_connect(struct wiphy *wiphy, struct net_device *netdev, ret = -ENODEV; goto out; } - if (!wifi->configured || wifi->connecting || wifi->connected || - wifi->scan_request) { + /* + * The firmware cannot reassociate in place. -EALREADY makes + * wpa_supplicant disconnect and retry the same BSS, rather than + * fail the roam and fall back to another one. + */ + if (wifi->connected) { + ret = -EALREADY; + goto out; + } + if (!wifi->configured || wifi->connecting || + wifi->scan_request || wifi->disconnect_pending) { ret = -EBUSY; goto out; } @@ -2940,7 +2953,7 @@ static void connect_worker(struct work_struct *work) 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, alpha_halted, local = false; + bool was_connected, disconnected, alpha_halted, local = false; u16 reason = WLAN_REASON_UNSPECIFIED; mutex_lock(&wifi->lock); @@ -2948,6 +2961,7 @@ static void link_worker(struct work_struct *work) mutex_unlock(&wifi->lock); return; } + was_connected = wifi->connected; drain(wifi); alpha_halted = !READ_ONCE(wifi->stopping) && readl(wifi->a.bar + 8) != 2; disconnected = wifi->disconnect_event || READ_ONCE(wifi->stopping) || @@ -2982,11 +2996,13 @@ static void link_worker(struct work_struct *work) } 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, reason, NULL, 0, local, GFP_KERNEL); - } else { + if (was_connected) { + netif_stop_queue(wifi->netdev); + skb_queue_purge(&wifi->data_tx_queue); + netif_carrier_off(wifi->netdev); + cfg80211_disconnected(wifi->netdev, reason, NULL, 0, local, GFP_KERNEL); + } + } else if (was_connected) { schedule_delayed_work(&wifi->link_work, msecs_to_jiffies(20)); } if (alpha_halted) { @@ -3007,13 +3023,39 @@ void centauri_wifi_kick(struct centauri *c) mod_delayed_work(system_percpu_wq, &wifi->link_work, 0); } +static int wait_disconnect(struct centauri_wifi *wifi) +{ + unsigned long deadline = jiffies + msecs_to_jiffies(5000); + + while (wifi->disconnect_pending) { + u32 stage; + + drain(wifi); + if (READ_ONCE(wifi->stopping)) + return -EIO; + stage = readl(wifi->a.bar + 8); + if (stage != 2) { + dev_warn_ratelimited(&wifi->control->pdev->dev, + "Alpha execution stage %#x\n", stage); + centauri_recovery_request(wifi->control, "disconnect: Alpha stage"); + return -EIO; + } + if (!wifi->disconnect_pending) + break; + if (time_after_eq(jiffies, deadline)) + return -ETIMEDOUT; + usleep_range(500, 1000); + } + return 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; + bool connected, stopping; + u16 reply_status = 0; + int ret = 0; WRITE_ONCE(wifi->connect_abort, true); cancel_scan(wifi); @@ -3025,8 +3067,23 @@ static int wifi_disconnect(struct wiphy *wiphy, struct net_device *netdev, u16 r 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; + stopping = READ_ONCE(wifi->stopping); + wifi->disconnect_pending = !stopping; + if (!stopping) { + ret = command(wifi, 131073, payload, sizeof(payload)); + reply_status = wifi->reply_status; + } + if (!ret && wifi->disconnect_pending && (wifi->reply_command & BIT(30))) { + ret = wait_disconnect(wifi); + if (ret == -ETIMEDOUT) + dev_err(&wifi->control->pdev->dev, + "Wi-Fi disconnect completion timed out\n"); + } else if (ret != -EIO && ret != -ETIMEDOUT) { + wifi->disconnect_pending = false; + } + dev_dbg(&wifi->control->pdev->dev, + "Wi-Fi disconnect: rc=%d status=%u reply_cmd=%#x pending=%d\n", + ret, reply_status, wifi->reply_command, wifi->disconnect_pending); connected = wifi->connected; wifi->connected = false; wifi->station_signal_valid = false; @@ -3709,7 +3766,8 @@ static int net_set_mac_address(struct net_device *netdev, void *data) ret = -ENETDOWN; goto out; } - if (wifi->connecting || wifi->connected || wifi->scan_request) { + if (wifi->connecting || wifi->connected || wifi->scan_request || + wifi->disconnect_pending) { ret = -EBUSY; goto out; } @@ -3761,7 +3819,8 @@ static ssize_t fwlog_start_write(struct file *file, const char __user *input, return ret ?: -EINVAL; mutex_lock(&wifi->lock); mutex_lock(&wifi->fwlog_lock); - if (!wifi->configured || wifi->connecting || wifi->connected || wifi->scan_request) { + if (!wifi->configured || wifi->connecting || wifi->connected || + wifi->scan_request || wifi->disconnect_pending) { ret = -EBUSY; goto out; } @@ -4389,7 +4448,8 @@ static ssize_t host_wakeup_write(struct file *file, const char __user *input, if (ret || action != 1) return ret ?: -EINVAL; mutex_lock(&wifi->lock); - if (!wifi->configured || wifi->connecting || wifi->scan_request || wifi->connected) { + if (!wifi->configured || wifi->connecting || wifi->scan_request || + wifi->connected || wifi->disconnect_pending) { ret = -EBUSY; goto out; } @@ -4959,7 +5019,7 @@ int centauri_wifi_suspend(struct centauri *c) return 0; mutex_lock(&wifi->lock); if (wifi->transport_paused || wifi->connected || wifi->connecting || wifi->scan_request || - READ_ONCE(wifi->stopping)) { + wifi->disconnect_pending || READ_ONCE(wifi->stopping)) { mutex_unlock(&wifi->lock); return -EBUSY; } diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi_test.c b/drivers/net/wireless/apple/centauri/centauri_wifi_test.c index 9602e219a61e3..3d3a59d1dbc79 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi_test.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi_test.c @@ -39,8 +39,45 @@ static void centauri_station_stats_len_test(struct kunit *test) KUNIT_EXPECT_EQ(test, wifi->station_stats_len, 888); } +static void centauri_connect_state_guards_test(struct kunit *test) +{ + struct centauri_wifi *wifi = kunit_kzalloc(test, sizeof(*wifi), GFP_KERNEL); + struct wiphy *wiphy = kunit_kzalloc(test, sizeof(*wiphy) + sizeof(wifi), GFP_KERNEL); + struct cfg80211_connect_params params = { + .ssid = "test", + .ssid_len = 4, + .auth_type = NL80211_AUTHTYPE_OPEN_SYSTEM, + }; + + KUNIT_ASSERT_NOT_NULL(test, wifi); + KUNIT_ASSERT_NOT_NULL(test, wiphy); + mutex_init(&wifi->lock); + *(struct centauri_wifi **)wiphy_priv(wiphy) = wifi; + + wifi->configured = true; + wifi->connected = true; + KUNIT_EXPECT_EQ(test, wifi_connect(wiphy, NULL, ¶ms), -EALREADY); + + wifi->connected = false; + wifi->disconnect_pending = true; + KUNIT_EXPECT_EQ(test, wifi_connect(wiphy, NULL, ¶ms), -EBUSY); + KUNIT_EXPECT_EQ(test, wifi_scan(wiphy, NULL), -EBUSY); + + wifi->disconnect_pending = false; + wifi->connecting = true; + KUNIT_EXPECT_EQ(test, wifi_connect(wiphy, NULL, ¶ms), -EBUSY); + + wifi->connecting = false; + wifi->configured = false; + KUNIT_EXPECT_EQ(test, wifi_connect(wiphy, NULL, ¶ms), -EBUSY); + + wifi->stopping = true; + KUNIT_EXPECT_EQ(test, wifi_connect(wiphy, NULL, ¶ms), -ENODEV); +} + static struct kunit_case centauri_wifi_cases[] = { KUNIT_CASE(centauri_station_stats_len_test), + KUNIT_CASE(centauri_connect_state_guards_test), {} }; From c448d9ad8e11c41f057b401a3737eb878d69711f Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 4 Dec 2026 08:46:15 +0100 Subject: [PATCH 2/4] wifi: cfg80211: drain queued disconnect events and always restore SSID on connect When a FullMAC driver reports disconnect synchronously from ops->disconnect() via cfg80211_disconnected(), EVENT_DISCONNECTED is queued asynchronously onto wdev->event_list. If userspace (such as wpa_supplicant handling -EALREADY on a roam) immediately calls NL80211_CMD_CONNECT before cfg80211_wq runs, the stale EVENT_DISCONNECTED fires after the new connect has started, emitting a spurious disconnect for the new target and clearing wdev->u.client.ssid_len. When that new connect completes, __cfg80211_connect_result() only restored wdev->u.client.ssid_len after checking for a Country IE; on any AP without a Country IE it returned early, leaving wdev->connected == true with wdev->u.client.ssid_len == 0. Subsequent cfg80211_disconnect() calls then skipped rdev_disconnect() completely because ssid_len was 0, permanently wedging the interface in a connected state. Fix both bugs: - In __cfg80211_connect_result(), restore wdev->u.client.ssid_len before the optional Country IE handling so it is never skipped. - In cfg80211_disconnect(), check wdev->connected alongside wdev->u.client.ssid_len and call cfg80211_process_wdev_events(wdev) after rdev_disconnect() so synchronously queued disconnect events are delivered before nl80211_disconnect() returns. Signed-off-by: Thomas Glanzmann --- net/wireless/sme.c | 48 +++++++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/net/wireless/sme.c b/net/wireless/sme.c index 76b5dffcd29e5..bcf2d612a3526 100644 --- a/net/wireless/sme.c +++ b/net/wireless/sme.c @@ -872,6 +872,25 @@ void __cfg80211_connect_result(struct net_device *dev, cfg80211_upload_connect_keys(wdev); + if (!wdev->u.client.ssid_len) { + rcu_read_lock(); + for_each_valid_link(cr, link) { + u32 ssid_len; + + ssid = ieee80211_bss_get_elem(cr->links[link].bss, + WLAN_EID_SSID); + + if (!ssid || !ssid->datalen) + continue; + + ssid_len = min(ssid->datalen, IEEE80211_MAX_SSID_LEN); + memcpy(wdev->u.client.ssid, ssid->data, ssid_len); + wdev->u.client.ssid_len = ssid_len; + break; + } + rcu_read_unlock(); + } + rcu_read_lock(); for_each_valid_link(cr, link) { country_elem = @@ -897,25 +916,6 @@ void __cfg80211_connect_result(struct net_device *dev, country_data, country_datalen); kfree(country_data); - if (!wdev->u.client.ssid_len) { - rcu_read_lock(); - for_each_valid_link(cr, link) { - u32 ssid_len; - - ssid = ieee80211_bss_get_elem(cr->links[link].bss, - WLAN_EID_SSID); - - if (!ssid || !ssid->datalen) - continue; - - ssid_len = min(ssid->datalen, IEEE80211_MAX_SSID_LEN); - memcpy(wdev->u.client.ssid, ssid->data, ssid_len); - wdev->u.client.ssid_len = ssid_len; - break; - } - rcu_read_unlock(); - } - return; out: for_each_valid_link(cr, link) @@ -1564,8 +1564,16 @@ int cfg80211_disconnect(struct cfg80211_registered_device *rdev, err = cfg80211_sme_disconnect(wdev, reason); else if (!rdev->ops->disconnect) cfg80211_mlme_down(rdev, dev); - else if (wdev->u.client.ssid_len) + else if (wdev->u.client.ssid_len || wdev->connected) { err = rdev_disconnect(rdev, dev, reason); + /* + * Drain any disconnect event queued synchronously by + * ops->disconnect() so an immediate connect retry (such as + * wpa_supplicant handling -EALREADY on a roam) does not race + * with a stale EVENT_DISCONNECTED clearing wdev state. + */ + cfg80211_process_wdev_events(wdev); + } /* * Clear ssid_len unless we actually were fully connected, From 875311ad644f4e14c3113821791c8a9fccbec9ae Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 4 Dec 2026 09:21:18 +0100 Subject: [PATCH 3/4] wifi: apple: centauri: clear disconnect_pending before wifi_disconnect returns If command(131073) or wait_disconnect() times out (or hits a transient fault) while Alpha stays at stage 2, nothing drains the control ring while the interface is disconnected. Leaving disconnect_pending latched would permanently wedge wifi_scan(), wifi_connect(), net_set_mac_address(), and centauri_wifi_suspend() until module rebind. Log the pre-clear pending state in dev_dbg() and clear wifi->disconnect_pending before releasing wifi->lock. Also note why wait_disconnect() uses a 5000 ms deadline alongside the 500 ms firmware payload budget. Signed-off-by: Thomas Glanzmann --- drivers/net/wireless/apple/centauri/centauri_wifi.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index 3511f616bcc00..e8072da01517f 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -3025,6 +3025,7 @@ void centauri_wifi_kick(struct centauri *c) static int wait_disconnect(struct centauri_wifi *wifi) { + /* Matches T6050's wait, with headroom over the 500 ms payload budget. */ unsigned long deadline = jiffies + msecs_to_jiffies(5000); while (wifi->disconnect_pending) { @@ -3084,6 +3085,12 @@ static int wifi_disconnect(struct wiphy *wiphy, struct net_device *netdev, u16 r dev_dbg(&wifi->control->pdev->dev, "Wi-Fi disconnect: rc=%d status=%u reply_cmd=%#x pending=%d\n", ret, reply_status, wifi->reply_command, wifi->disconnect_pending); + /* + * Clear disconnect_pending before releasing the lock: nothing drains + * the control ring while disconnected, so leaving it set after a + * timeout or fault would wedge scan, connect, and suspend until rebind. + */ + wifi->disconnect_pending = false; connected = wifi->connected; wifi->connected = false; wifi->station_signal_valid = false; From 18740d1236344c63cac074601637c8cacb0daae7 Mon Sep 17 00:00:00 2001 From: Thomas Glanzmann Date: Fri, 4 Dec 2026 09:21:19 +0100 Subject: [PATCH 4/4] wifi: apple: centauri: register centauri KUnit suite in apple-kunit.py Add drivers/net/wireless/apple/centauri/centauri.kunitconfig and wire it into scripts/ci/apple-kunit.py so hosted arm64 KUnit CI runs apple-centauri-wifi* (centauri_station_stats_len_test and centauri_connect_state_guards_test) whenever centauri files change. Signed-off-by: Thomas Glanzmann --- .../apple/centauri/centauri.kunitconfig | 17 +++++++++++++++++ scripts/ci/apple-kunit.py | 2 ++ 2 files changed, 19 insertions(+) create mode 100644 drivers/net/wireless/apple/centauri/centauri.kunitconfig diff --git a/drivers/net/wireless/apple/centauri/centauri.kunitconfig b/drivers/net/wireless/apple/centauri/centauri.kunitconfig new file mode 100644 index 0000000000000..1162ef58a14b5 --- /dev/null +++ b/drivers/net/wireless/apple/centauri/centauri.kunitconfig @@ -0,0 +1,17 @@ +CONFIG_KUNIT=y +CONFIG_ARCH_APPLE=y +CONFIG_OF=y +CONFIG_PCI=y +CONFIG_MAILBOX=y +CONFIG_APPLE_MAILBOX=y +CONFIG_APPLE_RTKIT=y +CONFIG_MFD_MACSMC=y +CONFIG_DEBUG_FS=y +CONFIG_NET=y +CONFIG_WIRELESS=y +CONFIG_CFG80211=y +CONFIG_NETDEVICES=y +CONFIG_WLAN=y +CONFIG_WLAN_VENDOR_APPLE=y +CONFIG_APPLE_CENTAURI=y +CONFIG_APPLE_CENTAURI_KUNIT_TEST=y diff --git a/scripts/ci/apple-kunit.py b/scripts/ci/apple-kunit.py index 90249c7e0b530..6ff6d62860075 100755 --- a/scripts/ci/apple-kunit.py +++ b/scripts/ci/apple-kunit.py @@ -51,6 +51,8 @@ ('scaler', MEDIA + 'scaler/.kunitconfig', False, [MEDIA + 'scaler/'], []), ('isp', MEDIA + 'isp/.kunitconfig', False, [MEDIA + 'isp/'], []), ('apple-sio', 'drivers/dma/apple-sio.kunitconfig', False, ['drivers/dma/apple-sio*'], []), + ('centauri', 'drivers/net/wireless/apple/centauri/centauri.kunitconfig', False, + ['drivers/net/wireless/apple/centauri/'], ['apple-centauri-wifi*']), ('mt7932', 'drivers/net/wireless/mediatek/mt7932/mt7932.kunitconfig', False, ['drivers/net/wireless/mediatek/mt7932/', 'drivers/pci/controller/pcie-apple*', 'include/linux/mt7932.h', 'include/linux/soc/apple/pci-apple-piodma.h', 'net/wireless/', 'include/net/cfg80211.h'],