From 47c1e35e7cac4bf488d463c6afe0ae34005461e6 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sat, 28 Nov 2026 11:37:57 -0500 Subject: [PATCH 1/5] wifi: cfg80211: preserve pending SSID when connect returns -EBUSY When cfg80211_connect() receives a replacement connect request for the same SSID while an earlier join is still in progress and the driver returns -EBUSY, clearing wdev->u.client.ssid_len drops the lookup key needed when the earlier join completes. Preserve wdev->u.client.ssid_len on -EBUSY when old_ssid_len was already set, matching how a refused reassociation preserves the existing SSID. Signed-off-by: Satya Benson --- net/wireless/sme.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/net/wireless/sme.c b/net/wireless/sme.c index 2a719b5c487e3..76b5dffcd29e5 100644 --- a/net/wireless/sme.c +++ b/net/wireless/sme.c @@ -1442,6 +1442,7 @@ int cfg80211_connect(struct cfg80211_registered_device *rdev, { struct wireless_dev *wdev = dev->ieee80211_ptr; int err; + u8 old_ssid_len = wdev->u.client.ssid_len; lockdep_assert_wiphy(wdev->wiphy); @@ -1533,9 +1534,12 @@ int cfg80211_connect(struct cfg80211_registered_device *rdev, wdev->connect_keys = NULL; /* * This could be reassoc getting refused, don't clear - * ssid_len in that case. + * ssid_len in that case. A busy driver may also still be + * completing the earlier join; the SSID equality check above + * guarantees this request did not change that join's SSID, so + * preserve its lookup key when the replacement is refused. */ - if (!wdev->connected) + if (!wdev->connected && !(err == -EBUSY && old_ssid_len)) wdev->u.client.ssid_len = 0; return err; } From 467db10ec38fd1bc0994f100e16197e46c50b3d6 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sat, 28 Nov 2026 11:38:23 -0500 Subject: [PATCH 2/5] wifi: brcmfmac: serialize firmware-offloaded replacement joins When userspace issues a replacement connect while a firmware-offloaded (SAE/PSK) station join is still in flight, starting a new join without waiting for the prior join to terminate lets the old join's events complete against the replacement request or wedge firmware authentication. Serialize replacement connects by sending BRCMF_C_DISASSOC for the old target, suppressing stale connect/roam callbacks until firmware delivers a terminal event (bounded by BRCMF_JOIN_CANCEL_TIMEOUT while holding the wiphy lock), and returning -EBUSY if cancellation fails or times out so cfg80211 keeps the pending join intact. Clear the cancel flag when the interface goes down. Signed-off-by: Satya Benson --- .../broadcom/brcm80211/brcmfmac/cfg80211.c | 97 +++++++++++++++++++ .../broadcom/brcm80211/brcmfmac/cfg80211.h | 4 + 2 files changed, 101 insertions(+) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c index b732ce47c4d7e..5bb4f5e8cdd45 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c @@ -6,6 +6,7 @@ /* Toplevel file. Relies on dhd_linux.c to send commands to the dongle. */ #include +#include #include #include #include @@ -90,6 +91,12 @@ #define BRCMF_ND_INFO_TIMEOUT msecs_to_jiffies(2000) +/* + * Bound wiphy-lock hold while waiting for firmware's terminal event + * after BRCMF_C_DISASSOC cancels an in-flight offloaded join. + */ +#define BRCMF_JOIN_CANCEL_TIMEOUT msecs_to_jiffies(1000) + #define BRCMF_PS_MAX_TIMEOUT_MS 2000 #define BRCMF_LPAS_UNICAST_FILTER_ID 100 @@ -2476,6 +2483,65 @@ static void brcmf_connect_set_target(struct brcmf_cfg80211_profile *profile, eth_zero_addr(profile->bssid); } +static bool brcmf_join_canceling(struct brcmf_cfg80211_vif *vif) +{ + /* Pairs with brcmf_set_join_canceling(): observe completion setup. */ + return smp_load_acquire(&vif->join_canceling); +} + +static void brcmf_set_join_canceling(struct brcmf_cfg80211_vif *vif, + bool pending) +{ + /* Publish preceding setup/teardown before callbacks observe the flag. */ + smp_store_release(&vif->join_canceling, pending); +} + +/* + * A replacement must not inherit completion of the cancelled firmware join. + * Keep the old profile until its terminal event. The callback signals this + * waiter without taking the wiphy lock held by the connect operation. + */ +static int brcmf_cancel_pending_join(struct brcmf_if *ifp) +{ + struct brcmf_cfg80211_vif *vif = ifp->vif; + struct brcmf_scb_val_le scb = { + .val = cpu_to_le32(WLAN_REASON_DEAUTH_LEAVING), + }; + struct cfg80211_connect_resp_params failed = { + .status = WLAN_STATUS_AUTH_TIMEOUT, + }; + int err; + + if (!brcmf_join_canceling(vif)) { + reinit_completion(&vif->join_cancel_done); + brcmf_set_join_canceling(vif, true); + memcpy(scb.ea, vif->profile.bssid, ETH_ALEN); + err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC, &scb, + sizeof(scb)); + if (err) { + brcmf_set_join_canceling(vif, false); + return -EBUSY; + } + } + if (!wait_for_completion_timeout(&vif->join_cancel_done, + BRCMF_JOIN_CANCEL_TIMEOUT)) { + /* + * Fail the old host request once, but keep ignoring its firmware + * completions until its terminal event. Never admit a new join yet. + */ + if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING, + &vif->sme_state)) + cfg80211_connect_done(ifp->ndev, &failed, GFP_KERNEL); + return -EBUSY; + } + clear_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state); + clear_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state); + clear_bit(BRCMF_VIF_STATUS_EAP_SUCCESS, &vif->sme_state); + clear_bit(BRCMF_VIF_STATUS_ASSOC_SUCCESS, &vif->sme_state); + brcmf_set_join_canceling(vif, false); + return 0; +} + static s32 brcmf_cfg80211_connect(struct wiphy *wiphy, struct net_device *ndev, struct cfg80211_connect_params *params) @@ -2496,6 +2562,18 @@ brcmf_cfg80211_connect(struct wiphy *wiphy, struct net_device *ndev, s32 err = 0; brcmf_dbg(TRACE, "Enter\n"); + if (ifp->vif->wdev.iftype == NL80211_IFTYPE_STATION && + (test_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state) || + brcmf_join_canceling(ifp->vif))) { + if (ifp->vif->wdev.connected || + (profile->use_fwsup != BRCMF_PROFILE_FWSUP_SAE && + profile->use_fwsup != BRCMF_PROFILE_FWSUP_PSK)) + return -EBUSY; + err = brcmf_cancel_pending_join(ifp); + if (err) + return err; + } + if (!check_vif_up(ifp->vif)) return -EIO; @@ -7125,6 +7203,7 @@ struct brcmf_cfg80211_vif *brcmf_alloc_vif(struct brcmf_cfg80211_info *cfg, vif->wdev.iftype = type; init_completion(&vif->mgmt_tx); init_completion(&vif->link_down); + init_completion(&vif->join_cancel_done); brcmf_init_prof(&vif->profile); @@ -7775,6 +7854,20 @@ brcmf_notify_connect_status(struct brcmf_if *ifp, brcmf_proto_delete_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr); } + if (brcmf_join_canceling(ifp->vif)) { + bool terminal = + (e->event_code == BRCMF_E_SET_SSID && + e->status != BRCMF_E_STATUS_SUCCESS) || + (e->event_code == BRCMF_E_LINK && !e->flags) || + e->event_code == BRCMF_E_DEAUTH || + e->event_code == BRCMF_E_DEAUTH_IND || + e->event_code == BRCMF_E_DISASSOC_IND; + + if (terminal) + complete(&ifp->vif->join_cancel_done); + return 0; + } + if (brcmf_is_apmode(ifp->vif)) { err = brcmf_notify_connect_status_ap(cfg, ndev, e, data); } else if (brcmf_is_linkup(ifp->vif, e)) { @@ -7835,6 +7928,9 @@ brcmf_notify_roaming_status(struct brcmf_if *ifp, u32 event = e->event_code; u32 status = e->status; + if (brcmf_join_canceling(ifp->vif)) + return 0; + if (event == BRCMF_E_ROAM && status == BRCMF_E_STATUS_SUCCESS) { if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state)) { @@ -9343,6 +9439,7 @@ static s32 __brcmf_cfg80211_down(struct brcmf_if *ifp) brcmf_abort_scanning(cfg); clear_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state); + brcmf_set_join_canceling(ifp->vif, false); return 0; } diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.h b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.h index 441daceda278d..a9687deda5ea3 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.h +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.h @@ -235,6 +235,8 @@ struct vif_saved_ie { * @list: linked list. * @mgmt_tx: completion for management frame transmit. * @link_down: completion for the link down event a disconnect before a suspend waits for. + * @join_cancel_done: completion signaled when a cancelled join ends. + * @join_canceling: true while waiting for a cancelled join to end. * @mgmt_tx_status: status of last management frame sent to firmware. * @mgmt_tx_id: * @mgmt_rx_reg: registered rx mgmt frame types. @@ -253,6 +255,8 @@ struct brcmf_cfg80211_vif { struct list_head list; struct completion mgmt_tx; struct completion link_down; + struct completion join_cancel_done; + bool join_canceling; unsigned long mgmt_tx_status; u32 mgmt_tx_id; u16 mgmt_rx_reg; From 2fe1d1ecea00050e2e8f20d82f196a8f38329945 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sat, 28 Nov 2026 11:38:23 -0500 Subject: [PATCH 3/5] wifi: brcmfmac: reinitialize non-WoW station firmware on resume After a non-WoW suspend where the dongle stayed powered across sleep, firmware can retain stale association and supplicant state that causes the first post-resume join to fail with authentication errors. On non-WoW resume of a ready station VIF, preserve the configured power mode across a BRCMF_C_DOWN / BRCMF_C_UP cycle. Extend brcmf-suspend-tests.py (including the restore-pm mutation) and register it in run-host-tests.py. Signed-off-by: Satya Benson --- .../broadcom/brcm80211/brcmfmac/cfg80211.c | 18 ++++++ tools/asahi/brcmf-suspend-tests.py | 62 +++++++++++++++++-- tools/asahi/run-host-tests.py | 2 +- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c index 5bb4f5e8cdd45..a0526225044d1 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c @@ -5178,6 +5178,24 @@ static s32 brcmf_cfg80211_resume(struct wiphy *wiphy) brcmf_notify_sched_scan_results); cfg->wowl.nd_enabled = false; } + } else if (ifp->vif && + ifp->vif->wdev.iftype == NL80211_IFTYPE_STATION && + test_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state)) { + u32 pm_mode; + + /* Keep the configured power mode across wireless reinitialization. */ + err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_PM, &pm_mode); + if (err) + goto out; + err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1); + if (err) + goto out; + err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1); + if (err) + goto out; + err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, pm_mode); + brcmf_dbg(CONN, "wireless reinit pm=%u result=%d\n", pm_mode, + err); } out: cfg->suspended = false; diff --git a/tools/asahi/brcmf-suspend-tests.py b/tools/asahi/brcmf-suspend-tests.py index dcdf5491079fb..2abdbd8fc176f 100644 --- a/tools/asahi/brcmf-suspend-tests.py +++ b/tools/asahi/brcmf-suspend-tests.py @@ -62,7 +62,8 @@ def function(source, name): struct net_device { bool stopped; unsigned drains, wakes; }; struct brcmf_pub; struct completion { bool done; }; -struct brcmf_cfg80211_vif { unsigned long sme_state; int wdev; struct brcmf_cfg80211_vif *next; +struct wireless_dev { int iftype; }; +struct brcmf_cfg80211_vif { unsigned long sme_state; struct wireless_dev wdev; struct brcmf_cfg80211_vif *next; struct completion link_down; }; struct brcmf_if { struct brcmf_cfg80211_vif *vif; struct brcmf_pub *drvr; struct net_device *ndev; unsigned netif_stop; int netif_stop_lock, bsscfgidx; }; @@ -191,7 +192,9 @@ def function(source, name): CALLBACK_STUBS = r''' #define BRCMF_FEAT_WOWL_ARP_ND 1 #define BRCMF_FEAT_PNO 2 -#define BRCMF_C_SET_PM 1 +#define NL80211_IFTYPE_STATION 2 +#define NL80211_IFTYPE_AP 3 +enum { BRCMF_C_GET_PM, BRCMF_C_DOWN, BRCMF_C_UP, BRCMF_C_SET_PM }; #define BRCMF_E_PFN_NET_FOUND 1 #define BRCMF_SCAN_STATUS_BUSY 1 #define BRCMF_VIF_STATUS_READY 2 @@ -204,7 +207,7 @@ def function(source, name): #define BRCMF_FEAT_LPAS 4 #define GFP_KERNEL 0 static unsigned lpas_restores, wake_reports; -static void cfg80211_report_wowlan_wakeup(int *w,void *i,int g) {(void)w;(void)i;(void)g;wake_reports++;} +static void cfg80211_report_wowlan_wakeup(struct wireless_dev *w,void *i,int g) {(void)w;(void)i;(void)g;wake_reports++;} static int brcmf_configure_lpas(struct brcmf_cfg80211_info *c,struct brcmf_if *i,struct cfg80211_wowlan *w) {(void)c;(void)i;(void)w;can_command();return 0;} static int brcmf_restore_lpas(struct brcmf_cfg80211_info *c,struct brcmf_if *i) { (void)i;assert(bus.state==BRCMF_BUS_UP);lpas_restores++;c->wowl.lpas=c->wowl.active=false;return failure==4?-EIO:0; @@ -219,7 +222,25 @@ def function(source, name): static void brcmf_fil_iovar_int_set(struct brcmf_if *i,const char *s,int n) {(void)i;(void)s;(void)n;can_command();} static void brcmf_config_wowl_pattern(struct brcmf_if *i,const char *s,void *p,int n,void *q,int t) {(void)i;(void)s;(void)p;(void)n;(void)q;(void)t;can_command();} static void brcmf_configure_arp_nd_offload(struct brcmf_if *i,bool b) {(void)i;(void)b;can_command();} -static void brcmf_fil_cmd_int_set(struct brcmf_if *i,int c,int n) {(void)i;(void)c;(void)n;can_command();} +static int reinit_step, fail_step, configured_pm; +static int observe_reinit(struct brcmf_if *ifp_arg, int cmd) { + assert(ifp_arg == &ifp[0] && bus.state == BRCMF_BUS_UP); + assert(devinfo.ready && devinfo.intr && cfg.suspended); + assert(cmd == reinit_step); + return ++reinit_step == fail_step ? -EIO : 0; +} +static int brcmf_fil_cmd_int_get(struct brcmf_if *i, int cmd, u32 *value) { + *value = configured_pm; + return observe_reinit(i, cmd); +} +static int brcmf_fil_cmd_int_set(struct brcmf_if *i, int cmd, int value) { + if (cfg.wowl.active) { + can_command(); + return 0; + } + assert(value == (cmd == BRCMF_C_SET_PM ? configured_pm : 1)); + return observe_reinit(i, cmd); +} static void brcmf_cfg80211_sched_scan_stop(struct wiphy *w,struct net_device *n,int i) {(void)w;(void)n;(void)i;can_command();} static void brcmf_fweh_unregister(struct brcmf_pub *p,int e) {(void)p;(void)e;} #define brcmf_fweh_register(p,e,f) ((void)(p),(void)(e)) @@ -259,6 +280,35 @@ def function(source, name): } int main(void) { unsigned cases=0; + /* Non-WoW station resume reinitializes wireless state while preserving PM. */ + for (int fail = 0; fail <= 4; fail++) { + struct brcmf_cfg80211_vif vif = { + .sme_state = BIT(BRCMF_VIF_STATUS_READY), + .wdev.iftype = NL80211_IFTYPE_STATION, + }; + reset(); + ifp[0].vif = &vif; + reinit_step = 0; + fail_step = fail; + configured_pm = 2; + cfg.suspended = true; + assert(brcmf_cfg80211_resume(&wiphy) == (fail ? -EIO : 0)); + assert(reinit_step == (fail ? fail : 4) && !cfg.suspended); + cases++; + } + /* Non-station or not-ready VIF skips non-WoW resume reinitialization. */ + for (int ap = 0; ap < 2; ap++) { + struct brcmf_cfg80211_vif vif = { + .sme_state = ap ? BIT(BRCMF_VIF_STATUS_READY) : 0, + .wdev.iftype = ap ? NL80211_IFTYPE_AP : NL80211_IFTYPE_STATION, + }; + reset(); + ifp[0].vif = &vif; + reinit_step = fail_step = 0; + cfg.suspended = true; + assert(!brcmf_cfg80211_resume(&wiphy) && !reinit_step && !cfg.suspended); + cases++; + } for(int up=0;up<2;up++)for(int fail=0;fail<2;fail++) { reset();vif_up=up; assert(!brcmf_cfg80211_suspend(&wiphy,NULL) && cfg.suspended); @@ -413,7 +463,7 @@ def main(): parser.add_argument('--out', type=Path) parser.add_argument('--mutation', choices=['country-gate', 'queue-reasons', 'worker-gate', 'resume-order', 'tx-drain', 'lpas-resume', - 's2idle-d3', 'wowl-s2idle-only']) + 's2idle-d3', 'wowl-s2idle-only', 'restore-pm']) args = parser.parse_args() sources = {name: (DRIVER / (name + '.c')).read_text() for name in ('cfg80211', 'core', 'pcie', 'msgbuf')} @@ -444,6 +494,8 @@ def main(): elif args.mutation == 'lpas-resume': body = body.replace('err = brcmf_restore_lpas(cfg, ifp);\n\t\t\tgoto out;', 'return brcmf_restore_lpas(cfg, ifp);') + elif args.mutation == 'restore-pm': + body = body.replace('BRCMF_C_SET_PM, pm_mode)', 'BRCMF_C_SET_PM, 0)') elif args.mutation == 's2idle-d3': body = body.replace('if (devinfo->fwseed && pm_suspend_target_state == PM_SUSPEND_TO_IDLE)', 'if (false)') diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 00123f40bd467..6d75f5d275459 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -217,7 +217,7 @@ def mutations(flag, names, base, expect=KILL): 'brcmf-suspend-tests.py': [(out(), PASS)] + mutations('--mutation', ('country-gate', 'queue-reasons', 'worker-gate', 'resume-order', 'tx-drain', 'lpas-resume', - 'wowl-s2idle-only'), out()), + 'wowl-s2idle-only', 'restore-pm'), out()), 'brcmf-wowl-filter-tests.py': [(out(), PASS)] + mutations('--mutation', ('base-order', 'mask-last', 'totsize', 'no-track'), out()), 'brcmfmac-scan-timeout-tests.py': [(out('--output'), PASS)], From a54377bf56a8d0e4e56d837acba0deedd1c8e4b3 Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sat, 28 Nov 2026 21:41:10 -0500 Subject: [PATCH 4/5] wifi: brcmfmac: never leave a cancelled join wedged brcmf_cancel_pending_join() could leave join_canceling set for good: - The event path runs without the wiphy lock. If the old join's last event was handled just before the flag was set, the DISASSOC went to idle firmware, no terminal event followed and the wait timed out. - After any timeout the flag stayed set, and every later connect skipped the DISASSOC, waited the full second again and returned -EBUSY until the interface went down. - A non-WoW resume's DOWN/UP did not clear it either. Now the cancel is armed with a full barrier before CONNECTING is tested, and a join that ended (or connected) through the normal path, before the flag or during the wait, is not cancelled. A connect that finds an earlier cancel still pending sends the DISASSOC again; if that one also sees no terminal event, the join is taken as gone, so one -EBUSY is the most a silent firmware costs. The resume's DOWN ends any join: it fails a pending host request once and clears the flag. tools/asahi/brcmf-join-cancel-tests.py runs the cancel against scripted firmware events, with four negative controls; brcmf-suspend-tests.py checks the resume path. Signed-off-by: Satya Benson --- .../broadcom/brcm80211/brcmfmac/cfg80211.c | 65 ++++-- tools/asahi/brcmf-join-cancel-tests.py | 192 ++++++++++++++++++ tools/asahi/brcmf-suspend-tests.py | 29 ++- tools/asahi/run-host-tests.py | 4 +- 4 files changed, 271 insertions(+), 19 deletions(-) create mode 100755 tools/asahi/brcmf-join-cancel-tests.py diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c index a0526225044d1..faebd92bc83a2 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c @@ -2500,6 +2500,11 @@ static void brcmf_set_join_canceling(struct brcmf_cfg80211_vif *vif, * A replacement must not inherit completion of the cancelled firmware join. * Keep the old profile until its terminal event. The callback signals this * waiter without taking the wiphy lock held by the connect operation. + * + * A first cancel that times out fails the old host request and returns -EBUSY, + * still ignoring the old join's events. The next connect cancels again; if that + * also sees no terminal event, the firmware has had two disassociations and no + * join event for two timeouts, and the join is taken as gone. */ static int brcmf_cancel_pending_join(struct brcmf_if *ifp) { @@ -2510,29 +2515,40 @@ static int brcmf_cancel_pending_join(struct brcmf_if *ifp) struct cfg80211_connect_resp_params failed = { .status = WLAN_STATUS_AUTH_TIMEOUT, }; + bool retry = brcmf_join_canceling(vif); int err; - if (!brcmf_join_canceling(vif)) { - reinit_completion(&vif->join_cancel_done); - brcmf_set_join_canceling(vif, true); - memcpy(scb.ea, vif->profile.bssid, ETH_ALEN); - err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC, &scb, - sizeof(scb)); - if (err) { + reinit_completion(&vif->join_cancel_done); + brcmf_set_join_canceling(vif, true); + /* + * Order the flag before the CONNECTING test. brcmf_notify_connect_status() + * tests the flag before it ends a join and clears CONNECTING, so one of + * the two sees the other. + */ + smp_mb(); + if (!retry && !test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state)) + goto ended; + memcpy(scb.ea, vif->profile.bssid, ETH_ALEN); + err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC, &scb, sizeof(scb)); + if (err) { + if (!retry) brcmf_set_join_canceling(vif, false); - return -EBUSY; - } + return -EBUSY; } if (!wait_for_completion_timeout(&vif->join_cancel_done, BRCMF_JOIN_CANCEL_TIMEOUT)) { - /* - * Fail the old host request once, but keep ignoring its firmware - * completions until its terminal event. Never admit a new join yet. - */ - if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING, - &vif->sme_state)) - cfg80211_connect_done(ifp->ndev, &failed, GFP_KERNEL); - return -EBUSY; + /* The old join ended through the normal path just before the flag. */ + if (!retry && + !test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state)) + goto ended; + if (!retry) { + /* Fail the old host request once; keep ignoring its events. */ + if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING, + &vif->sme_state)) + cfg80211_connect_done(ifp->ndev, &failed, GFP_KERNEL); + return -EBUSY; + } + brcmf_dbg(CONN, "cancelled join sent no terminal event twice: taking it as gone\n"); } clear_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state); clear_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state); @@ -2540,6 +2556,11 @@ static int brcmf_cancel_pending_join(struct brcmf_if *ifp) clear_bit(BRCMF_VIF_STATUS_ASSOC_SUCCESS, &vif->sme_state); brcmf_set_join_canceling(vif, false); return 0; + +ended: + /* Nothing left to cancel; a join that succeeded keeps the link. */ + brcmf_set_join_canceling(vif, false); + return test_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state) ? -EBUSY : 0; } static s32 @@ -5190,6 +5211,16 @@ static s32 brcmf_cfg80211_resume(struct wiphy *wiphy) err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1); if (err) goto out; + /* DOWN ends any firmware join, a cancelled one included. */ + if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING, + &ifp->vif->sme_state)) { + struct cfg80211_connect_resp_params failed = { + .status = WLAN_STATUS_AUTH_TIMEOUT, + }; + + cfg80211_connect_done(ifp->ndev, &failed, GFP_KERNEL); + } + brcmf_set_join_canceling(ifp->vif, false); err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1); if (err) goto out; diff --git a/tools/asahi/brcmf-join-cancel-tests.py b/tools/asahi/brcmf-join-cancel-tests.py new file mode 100755 index 0000000000000..0caf883d32f0a --- /dev/null +++ b/tools/asahi/brcmf-join-cancel-tests.py @@ -0,0 +1,192 @@ +#!/usr/bin/env python3 +# SPDX-License-Identifier: GPL-2.0-only OR MIT +"""Run brcmfmac's replacement-join cancellation against scripted firmware events.""" +import argparse +import json +from pathlib import Path +import re +import subprocess +import tempfile + +ROOT = Path(__file__).resolve().parents[2] +CFG = ROOT / 'drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c' + + +def function(source, name): + match = re.search(r'^(?:static\s+)?(?:void|int|s32|bool)\s+' + name + + r'\([^;]*?\)\s*\{', source, re.M) + if not match: + raise ValueError('Missing function ' + name) + end = source.index('{', match.start()) + 1 + depth = 1 + while depth: + depth += (source[end] == '{') - (source[end] == '}') + end += 1 + return source[match.start():end] + '\n' + + +PREFIX = r''' +#include +#include +#include +#include +#include +#include +typedef uint8_t u8; typedef uint32_t __le32; +#define BIT(n) (1UL << (n)) +#define ETH_ALEN 6 +#define cpu_to_le32(x) (x) +#define smp_load_acquire(p) (*(p)) +#define smp_store_release(p, v) (*(p) = (v)) +#define smp_mb() __sync_synchronize() +#define brcmf_dbg(...) ((void)0) +#define GFP_KERNEL 0 +#define WLAN_REASON_DEAUTH_LEAVING 3 +#define WLAN_STATUS_AUTH_TIMEOUT 16 +#define BRCMF_C_DISASSOC 52 +#define BRCMF_JOIN_CANCEL_TIMEOUT 1000 +enum { BRCMF_VIF_STATUS_READY, BRCMF_VIF_STATUS_CONNECTING, BRCMF_VIF_STATUS_CONNECTED, + BRCMF_VIF_STATUS_EAP_SUCCESS, BRCMF_VIF_STATUS_ASSOC_SUCCESS }; +struct completion { bool done; }; +struct brcmf_cfg80211_profile { u8 bssid[ETH_ALEN]; }; +struct brcmf_cfg80211_vif { unsigned long sme_state; struct brcmf_cfg80211_profile profile; + struct completion join_cancel_done; bool join_canceling; }; +struct net_device { int unused; }; +struct brcmf_if { struct brcmf_cfg80211_vif *vif; struct net_device *ndev; }; +struct brcmf_scb_val_le { __le32 val; u8 ea[ETH_ALEN]; }; +struct cfg80211_connect_resp_params { int status; }; +static bool test_bit(int i, unsigned long *p) { return *p & BIT(i); } +static void set_bit(int i, unsigned long *p) { *p |= BIT(i); } +static void clear_bit(int i, unsigned long *p) { *p &= ~BIT(i); } +static bool test_and_clear_bit(int i, unsigned long *p) { bool o = test_bit(i, p); clear_bit(i, p); return o; } +static void reinit_completion(struct completion *c) { c->done = false; } + +/* Scripted firmware: what the event path does while the DISASSOC is outstanding. */ +enum fw { FW_TERMINAL, FW_SILENT, FW_ENDED_NORMALLY, FW_CONNECTED_NORMALLY }; +static enum fw fw; +static unsigned disassocs, connect_failures; +static struct brcmf_if *cur; +static int brcmf_fil_cmd_data_set(struct brcmf_if *ifp, int cmd, void *d, int len) +{ + (void)d; (void)len; assert(cmd == BRCMF_C_DISASSOC); disassocs++; + struct brcmf_cfg80211_vif *v = ifp->vif; + switch (fw) { + case FW_TERMINAL: + /* brcmf_notify_connect_status(): sees join_canceling, signals the waiter. */ + assert(v->join_canceling); v->join_cancel_done.done = true; break; + case FW_SILENT: break; + case FW_ENDED_NORMALLY: + /* The old join's last event was handled just before the flag: it failed. */ + clear_bit(BRCMF_VIF_STATUS_CONNECTING, &v->sme_state); break; + case FW_CONNECTED_NORMALLY: + clear_bit(BRCMF_VIF_STATUS_CONNECTING, &v->sme_state); + set_bit(BRCMF_VIF_STATUS_CONNECTED, &v->sme_state); break; + } + return 0; +} +static unsigned long wait_for_completion_timeout(struct completion *c, unsigned long t) +{ (void)t; return c->done; } +static void cfg80211_connect_done(struct net_device *n, struct cfg80211_connect_resp_params *r, int g) +{ (void)n; (void)g; assert(r->status == WLAN_STATUS_AUTH_TIMEOUT); connect_failures++; } +''' + +MAIN = r''' +static struct brcmf_cfg80211_vif vif; +static struct net_device nd; +static struct brcmf_if ifp = { &vif, &nd }; +static void reset(unsigned long state, bool canceling) +{ + memset(&vif, 0, sizeof(vif)); vif.sme_state = state | BIT(BRCMF_VIF_STATUS_READY); + vif.join_canceling = canceling; disassocs = connect_failures = 0; +} +#define CONNECTING BIT(BRCMF_VIF_STATUS_CONNECTING) +int main(void) +{ + unsigned cases = 0; + /* A pending join: one DISASSOC, its terminal event, the replacement may join. */ + reset(CONNECTING, false); fw = FW_TERMINAL; + assert(brcmf_cancel_pending_join(&ifp) == 0); + assert(disassocs == 1 && !vif.join_canceling && !test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif.sme_state)); + cases++; + /* The old join ended before the cancel was armed: nothing to cancel, nothing sent. */ + reset(0, false); fw = FW_SILENT; + assert(brcmf_cancel_pending_join(&ifp) == 0); + assert(disassocs == 0 && !vif.join_canceling && connect_failures == 0); + cases++; + /* It ended through the normal path between the test and the flag: no wedge, no extra failure. */ + reset(CONNECTING, false); fw = FW_ENDED_NORMALLY; + assert(brcmf_cancel_pending_join(&ifp) == 0); + assert(!vif.join_canceling && connect_failures == 0); + cases++; + /* Same race, but the old join connected: the link stays, the replacement is refused once. */ + reset(CONNECTING, false); fw = FW_CONNECTED_NORMALLY; + assert(brcmf_cancel_pending_join(&ifp) == -EBUSY); + assert(!vif.join_canceling && test_bit(BRCMF_VIF_STATUS_CONNECTED, &vif.sme_state)); + cases++; + /* Silent firmware: the first cancel fails the old request once and keeps ignoring its events. */ + reset(CONNECTING, false); fw = FW_SILENT; + assert(brcmf_cancel_pending_join(&ifp) == -EBUSY); + assert(vif.join_canceling && connect_failures == 1 && disassocs == 1); + assert(!test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif.sme_state)); + /* The next connect cancels again; still silent: the join is taken as gone, not a permanent -EBUSY. */ + assert(brcmf_cancel_pending_join(&ifp) == 0); + assert(!vif.join_canceling && disassocs == 2 && connect_failures == 1); + cases++; + /* A retry that gets the terminal event also ends the cancel. */ + reset(0, true); fw = FW_TERMINAL; + assert(brcmf_cancel_pending_join(&ifp) == 0); + assert(!vif.join_canceling && disassocs == 1); + cases++; + printf("%u join cancellation controls passed\n", cases); + return 0; +} +''' + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('--out', type=Path) + parser.add_argument('--mutation', choices=['no-recheck', 'skip-retry-disassoc', 'retry-wedge', + 'no-barrier-arm']) + args = parser.parse_args() + source = CFG.read_text() + body = ''.join(function(source, name) for name in + ('brcmf_join_canceling', 'brcmf_set_join_canceling', 'brcmf_cancel_pending_join')) + if args.mutation == 'no-recheck': + # The timeout path no longer sees a join that ended through the normal path. + body = body.replace('if (!retry &&\n\t\t !test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state))\n\t\t\tgoto ended;\n', '') + elif args.mutation == 'skip-retry-disassoc': + # The pre-fix retry: wait again without asking the firmware again. + body = body.replace('\terr = brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC, &scb, sizeof(scb));', + '\terr = retry ? 0 : brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC, &scb, sizeof(scb));') + elif args.mutation == 'retry-wedge': + # The pre-fix timeout: every retry returns -EBUSY. + body = body.replace('\t\tif (!retry) {\n\t\t\t/* Fail the old host request once', + '\t\tif (true) {\n\t\t\t/* Fail the old host request once') + elif args.mutation == 'no-barrier-arm': + # Cancel even when the join already ended before the flag was armed. + body = body.replace('\tif (!retry && !test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state))\n\t\tgoto ended;\n', '') + code = PREFIX + body + MAIN + + def run(out): + out.mkdir(parents=True, exist_ok=True) + src = out / 'join-cancel.c' + src.write_text(code) + exe = out / 'join-cancel' + subprocess.run(['clang', '-std=gnu11', '-O1', '-g', '-fsanitize=address,undefined', + '-fno-sanitize-recover=all', str(src), '-o', str(exe)], check=True) + result = subprocess.run([str(exe)], capture_output=True, text=True) + receipt = {'exit': result.returncode, 'stdout': result.stdout, 'stderr': result.stderr, + 'limits': 'Scripted firmware events, one thread; no firmware timing or lock-order proof.'} + (out / 'results.json').write_text(json.dumps(receipt, indent=2) + '\n') + print(json.dumps(receipt)) + return result.returncode + + if args.out: + raise SystemExit(run(args.out)) + with tempfile.TemporaryDirectory(prefix='brcmf-join-cancel-') as temp: + raise SystemExit(run(Path(temp))) + + +if __name__ == '__main__': + main() diff --git a/tools/asahi/brcmf-suspend-tests.py b/tools/asahi/brcmf-suspend-tests.py index 2abdbd8fc176f..73257246f64de 100644 --- a/tools/asahi/brcmf-suspend-tests.py +++ b/tools/asahi/brcmf-suspend-tests.py @@ -201,6 +201,13 @@ def function(source, name): #define BRCMF_VIF_STATUS_CONNECTED 3 #define WLAN_REASON_UNSPECIFIED 1 #define test_bit(i,p) ((*(p)&BIT(i))!=0) +#define BRCMF_VIF_STATUS_CONNECTING 5 +#define WLAN_STATUS_AUTH_TIMEOUT 16 +static bool test_and_clear_bit(int i, unsigned long *p) {bool o=test_bit(i,p);*p&=~BIT(i);return o;} +struct cfg80211_connect_resp_params {int status;}; +static unsigned connect_failures, cancel_releases; +static void cfg80211_connect_done(struct net_device *n,struct cfg80211_connect_resp_params *r,int g) {(void)n;(void)g;assert(r->status==WLAN_STATUS_AUTH_TIMEOUT);connect_failures++;} +static void brcmf_set_join_canceling(struct brcmf_cfg80211_vif *v,bool b) {(void)v;assert(!b);cancel_releases++;} #define list_for_each_entry(v,h,m) for ((v)=(h)->first;(v);(v)=(v)->next) struct cfg80211_wowlan {bool any, magic_pkt;}; #define BRCMF_FEAT_WOWL 3 @@ -296,6 +303,23 @@ def function(source, name): assert(reinit_step == (fail ? fail : 4) && !cfg.suspended); cases++; } + /* The reinit's DOWN ends a pending (or cancelled) join: fail it once, release the cancel. */ + for (int connecting = 0; connecting < 2; connecting++) { + struct brcmf_cfg80211_vif vif = { + .sme_state = BIT(BRCMF_VIF_STATUS_READY) | (connecting ? BIT(BRCMF_VIF_STATUS_CONNECTING) : 0), + .wdev.iftype = NL80211_IFTYPE_STATION, + }; + reset(); + ifp[0].vif = &vif; + reinit_step = fail_step = 0; + configured_pm = 2; + connect_failures = cancel_releases = 0; + cfg.suspended = true; + assert(brcmf_cfg80211_resume(&wiphy) == 0); + assert(connect_failures == (unsigned)connecting && cancel_releases == 1); + assert(!test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif.sme_state)); + cases++; + } /* Non-station or not-ready VIF skips non-WoW resume reinitialization. */ for (int ap = 0; ap < 2; ap++) { struct brcmf_cfg80211_vif vif = { @@ -463,7 +487,8 @@ def main(): parser.add_argument('--out', type=Path) parser.add_argument('--mutation', choices=['country-gate', 'queue-reasons', 'worker-gate', 'resume-order', 'tx-drain', 'lpas-resume', - 's2idle-d3', 'wowl-s2idle-only', 'restore-pm']) + 's2idle-d3', 'wowl-s2idle-only', 'restore-pm', + 'resume-join']) args = parser.parse_args() sources = {name: (DRIVER / (name + '.c')).read_text() for name in ('cfg80211', 'core', 'pcie', 'msgbuf')} @@ -496,6 +521,8 @@ def main(): 'return brcmf_restore_lpas(cfg, ifp);') elif args.mutation == 'restore-pm': body = body.replace('BRCMF_C_SET_PM, pm_mode)', 'BRCMF_C_SET_PM, 0)') + elif args.mutation == 'resume-join': + body = body.replace('\t\tbrcmf_set_join_canceling(ifp->vif, false);\n', '') elif args.mutation == 's2idle-d3': body = body.replace('if (devinfo->fwseed && pm_suspend_target_state == PM_SUSPEND_TO_IDLE)', 'if (false)') diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index 6d75f5d275459..c79214b533517 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -214,10 +214,12 @@ def mutations(flag, names, base, expect=KILL): 'console-buf', 'console-widx', 'console-reinit'), out()), 'brcmf-sae-vendor-tests.py': [(out(), PASS)] + [(out() + ['--ref', '9260e1f021e075713931f3c23c0a5fa1da936b0b'], KILL)], + 'brcmf-join-cancel-tests.py': [(out(), PASS)] + + mutations('--mutation', ('no-recheck', 'skip-retry-disassoc', 'retry-wedge', 'no-barrier-arm'), out()), 'brcmf-suspend-tests.py': [(out(), PASS)] + mutations('--mutation', ('country-gate', 'queue-reasons', 'worker-gate', 'resume-order', 'tx-drain', 'lpas-resume', - 'wowl-s2idle-only', 'restore-pm'), out()), + 'wowl-s2idle-only', 'restore-pm', 'resume-join'), out()), 'brcmf-wowl-filter-tests.py': [(out(), PASS)] + mutations('--mutation', ('base-order', 'mask-last', 'totsize', 'no-track'), out()), 'brcmfmac-scan-timeout-tests.py': [(out('--output'), PASS)], From 2115ca6e50018336a809c25dab575339be4debde Mon Sep 17 00:00:00 2001 From: Satya Benson Date: Sat, 28 Nov 2026 21:48:56 -0500 Subject: [PATCH 5/5] wifi: brcmfmac: fence a cancelled join behind a reset before replacing it A second cancel that saw no terminal event admitted the replacement at once. A late event of the old join could then still reach it. Now that cancel resets the station firmware (GET_PM, DOWN, UP, SET_PM) with join_canceling still set, so the event path keeps ignoring the old join, and still refuses the connect. The reset ends the join for certain. A later connect admits a replacement only once no firmware event is queued or being handled (brcmf_fweh_idle()), so none of the old join's events can reach it. brcmf_fweh_idle() takes only the event queue's own lock; flushing the worker from connect could deadlock on the rtnl lock an interface event takes. The non-WoW resume's DOWN fences the same way: a pending or cancelled join keeps the flag, the reset is recorded, and the next connect releases it on an idle queue, whatever the profile's supplicant. A DOWN that fails ends no join, so a join that was not already being cancelled gets its events back. brcmf-join-cancel-tests.py adds the reset, a late event still queued after it, and the release on a drained queue, with the admit-unfenced and no-reset negative controls; brcmf-suspend-tests.py checks the resume fence and the failed DOWN, with the down-error-fence negative control. Signed-off-by: Satya Benson --- .../broadcom/brcm80211/brcmfmac/cfg80211.c | 66 ++++++++++++++++--- .../broadcom/brcm80211/brcmfmac/cfg80211.h | 3 + .../broadcom/brcm80211/brcmfmac/fweh.c | 17 +++++ .../broadcom/brcm80211/brcmfmac/fweh.h | 1 + tools/asahi/brcmf-join-cancel-tests.py | 53 ++++++++++++--- tools/asahi/brcmf-suspend-tests.py | 53 +++++++++++---- tools/asahi/run-host-tests.py | 6 +- 7 files changed, 168 insertions(+), 31 deletions(-) diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c index faebd92bc83a2..aa3a06e4a3bd0 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c @@ -2496,15 +2496,33 @@ static void brcmf_set_join_canceling(struct brcmf_cfg80211_vif *vif, smp_store_release(&vif->join_canceling, pending); } +/* Reset the station firmware, which ends any join, keeping its power mode. */ +static int brcmf_station_reset(struct brcmf_if *ifp) +{ + u32 pm_mode; + int err; + + err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_PM, &pm_mode); + if (!err) + err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1); + if (!err) + err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1); + if (!err) + err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, pm_mode); + return err; +} + /* * A replacement must not inherit completion of the cancelled firmware join. * Keep the old profile until its terminal event. The callback signals this * waiter without taking the wiphy lock held by the connect operation. * * A first cancel that times out fails the old host request and returns -EBUSY, - * still ignoring the old join's events. The next connect cancels again; if that - * also sees no terminal event, the firmware has had two disassociations and no - * join event for two timeouts, and the join is taken as gone. + * still ignoring the old join's events. The next connect cancels again. If that + * also sees no terminal event, the station firmware is reset with the flag + * still set, which ends the join for certain, and the connect is refused once + * more. A later connect admits a replacement only once no firmware event is + * still queued, so no event of the old join can reach it. */ static int brcmf_cancel_pending_join(struct brcmf_if *ifp) { @@ -2518,6 +2536,12 @@ static int brcmf_cancel_pending_join(struct brcmf_if *ifp) bool retry = brcmf_join_canceling(vif); int err; + if (retry && vif->join_reset) { + if (!brcmf_fweh_idle(ifp->drvr)) + return -EBUSY; + vif->join_reset = false; + goto cancelled; + } reinit_completion(&vif->join_cancel_done); brcmf_set_join_canceling(vif, true); /* @@ -2548,8 +2572,12 @@ static int brcmf_cancel_pending_join(struct brcmf_if *ifp) cfg80211_connect_done(ifp->ndev, &failed, GFP_KERNEL); return -EBUSY; } - brcmf_dbg(CONN, "cancelled join sent no terminal event twice: taking it as gone\n"); + brcmf_dbg(CONN, "cancelled join sent no terminal event twice: resetting the station\n"); + if (!brcmf_station_reset(ifp)) + vif->join_reset = true; + return -EBUSY; } +cancelled: clear_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state); clear_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state); clear_bit(BRCMF_VIF_STATUS_EAP_SUCCESS, &vif->sme_state); @@ -2586,8 +2614,10 @@ brcmf_cfg80211_connect(struct wiphy *wiphy, struct net_device *ndev, if (ifp->vif->wdev.iftype == NL80211_IFTYPE_STATION && (test_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state) || brcmf_join_canceling(ifp->vif))) { + /* Only a firmware-supplicant join is cancelled; a reset one is fenced for all. */ if (ifp->vif->wdev.connected || - (profile->use_fwsup != BRCMF_PROFILE_FWSUP_SAE && + (!ifp->vif->join_reset && + profile->use_fwsup != BRCMF_PROFILE_FWSUP_SAE && profile->use_fwsup != BRCMF_PROFILE_FWSUP_PSK)) return -EBUSY; err = brcmf_cancel_pending_join(ifp); @@ -5203,16 +5233,34 @@ static s32 brcmf_cfg80211_resume(struct wiphy *wiphy) ifp->vif->wdev.iftype == NL80211_IFTYPE_STATION && test_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state)) { u32 pm_mode; + bool pending, was_canceling; /* Keep the configured power mode across wireless reinitialization. */ err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_PM, &pm_mode); if (err) goto out; + /* + * DOWN ends any firmware join. Ignore that join's events from here, + * and let the next connect admit a replacement only once none is + * still queued (brcmf_cancel_pending_join()). + */ + pending = test_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state); + was_canceling = brcmf_join_canceling(ifp->vif); + if (pending || was_canceling) { + reinit_completion(&ifp->vif->join_cancel_done); + brcmf_set_join_canceling(ifp->vif, true); + } err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1); - if (err) + if (err) { + /* No DOWN, so the join was not ended: stop ignoring its events. */ + if (!was_canceling) + brcmf_set_join_canceling(ifp->vif, false); goto out; - /* DOWN ends any firmware join, a cancelled one included. */ - if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING, + } + if (brcmf_join_canceling(ifp->vif)) + ifp->vif->join_reset = true; + if (pending && + test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state)) { struct cfg80211_connect_resp_params failed = { .status = WLAN_STATUS_AUTH_TIMEOUT, @@ -5220,7 +5268,6 @@ static s32 brcmf_cfg80211_resume(struct wiphy *wiphy) cfg80211_connect_done(ifp->ndev, &failed, GFP_KERNEL); } - brcmf_set_join_canceling(ifp->vif, false); err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1); if (err) goto out; @@ -9488,6 +9535,7 @@ static s32 __brcmf_cfg80211_down(struct brcmf_if *ifp) brcmf_abort_scanning(cfg); clear_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state); + ifp->vif->join_reset = false; brcmf_set_join_canceling(ifp->vif, false); return 0; diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.h b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.h index a9687deda5ea3..09085db92f2d8 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.h +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.h @@ -237,6 +237,8 @@ struct vif_saved_ie { * @link_down: completion for the link down event a disconnect before a suspend waits for. * @join_cancel_done: completion signaled when a cancelled join ends. * @join_canceling: true while waiting for a cancelled join to end. + * @join_reset: the firmware was reset during that wait; the next connect + * admits a replacement once no firmware event is still queued. * @mgmt_tx_status: status of last management frame sent to firmware. * @mgmt_tx_id: * @mgmt_rx_reg: registered rx mgmt frame types. @@ -257,6 +259,7 @@ struct brcmf_cfg80211_vif { struct completion link_down; struct completion join_cancel_done; bool join_canceling; + bool join_reset; unsigned long mgmt_tx_status; u32 mgmt_tx_id; u16 mgmt_rx_reg; diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c index f990757183b9b..1c2363c5dd643 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.c @@ -260,6 +260,23 @@ brcmf_fweh_dequeue_event(struct brcmf_fweh_info *fweh) return event; } +/** + * brcmf_fweh_idle() - no firmware event is queued or being handled. + * + * @drvr: driver information object. + */ +bool brcmf_fweh_idle(struct brcmf_pub *drvr) +{ + struct brcmf_fweh_info *fweh = drvr->fweh; + unsigned long flags; + bool empty; + + spin_lock_irqsave(&fweh->evt_q_lock, flags); + empty = list_empty(&fweh->event_q); + spin_unlock_irqrestore(&fweh->evt_q_lock, flags); + return empty && !work_busy(&fweh->event_work); +} + /** * brcmf_fweh_event_worker() - firmware event worker. * diff --git a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.h b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.h index 53c4b58e6323c..d84da2b2f954e 100644 --- a/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.h +++ b/drivers/net/wireless/broadcom/brcm80211/brcmfmac/fweh.h @@ -357,6 +357,7 @@ int brcmf_fweh_register(struct brcmf_pub *drvr, enum brcmf_fweh_event_code code, void brcmf_fweh_unregister(struct brcmf_pub *drvr, enum brcmf_fweh_event_code code); int brcmf_fweh_init_events(struct brcmf_if *ifp); +bool brcmf_fweh_idle(struct brcmf_pub *drvr); int brcmf_fweh_activate_events(struct brcmf_if *ifp); void brcmf_fweh_process_event(struct brcmf_pub *drvr, struct brcmf_event *event_packet, diff --git a/tools/asahi/brcmf-join-cancel-tests.py b/tools/asahi/brcmf-join-cancel-tests.py index 0caf883d32f0a..9832ae9bf0941 100755 --- a/tools/asahi/brcmf-join-cancel-tests.py +++ b/tools/asahi/brcmf-join-cancel-tests.py @@ -44,15 +44,18 @@ def function(source, name): #define WLAN_REASON_DEAUTH_LEAVING 3 #define WLAN_STATUS_AUTH_TIMEOUT 16 #define BRCMF_C_DISASSOC 52 +enum { BRCMF_C_GET_PM = 85, BRCMF_C_DOWN = 3, BRCMF_C_UP = 2, BRCMF_C_SET_PM = 86 }; +typedef uint32_t u32; #define BRCMF_JOIN_CANCEL_TIMEOUT 1000 enum { BRCMF_VIF_STATUS_READY, BRCMF_VIF_STATUS_CONNECTING, BRCMF_VIF_STATUS_CONNECTED, BRCMF_VIF_STATUS_EAP_SUCCESS, BRCMF_VIF_STATUS_ASSOC_SUCCESS }; struct completion { bool done; }; struct brcmf_cfg80211_profile { u8 bssid[ETH_ALEN]; }; struct brcmf_cfg80211_vif { unsigned long sme_state; struct brcmf_cfg80211_profile profile; - struct completion join_cancel_done; bool join_canceling; }; + struct completion join_cancel_done; bool join_canceling, join_reset; }; +struct brcmf_pub { int unused; }; struct net_device { int unused; }; -struct brcmf_if { struct brcmf_cfg80211_vif *vif; struct net_device *ndev; }; +struct brcmf_if { struct brcmf_cfg80211_vif *vif; struct net_device *ndev; struct brcmf_pub *drvr; }; struct brcmf_scb_val_le { __le32 val; u8 ea[ETH_ALEN]; }; struct cfg80211_connect_resp_params { int status; }; static bool test_bit(int i, unsigned long *p) { return *p & BIT(i); } @@ -64,7 +67,7 @@ def function(source, name): /* Scripted firmware: what the event path does while the DISASSOC is outstanding. */ enum fw { FW_TERMINAL, FW_SILENT, FW_ENDED_NORMALLY, FW_CONNECTED_NORMALLY }; static enum fw fw; -static unsigned disassocs, connect_failures; +static unsigned disassocs, connect_failures, resets, queued; static struct brcmf_if *cur; static int brcmf_fil_cmd_data_set(struct brcmf_if *ifp, int cmd, void *d, int len) { @@ -84,6 +87,20 @@ def function(source, name): } return 0; } +/* Station reset: DOWN ends the firmware join. */ +static unsigned pm_now = 2; +static int brcmf_fil_cmd_int_get(struct brcmf_if *ifp, int cmd, u32 *v) +{ (void)ifp; assert(cmd == BRCMF_C_GET_PM); *v = pm_now; return 0; } +static int brcmf_fil_cmd_int_set(struct brcmf_if *ifp, int cmd, u32 v) +{ + /* Old-join events are ignored throughout the reset. */ + assert(ifp->vif->join_canceling); + if (cmd == BRCMF_C_DOWN) resets++; + if (cmd == BRCMF_C_SET_PM) assert(v == pm_now); + return 0; +} +/* Firmware events still queued for the event worker (a late event of the old join). */ +static bool brcmf_fweh_idle(struct brcmf_pub *d) { (void)d; return !queued; } static unsigned long wait_for_completion_timeout(struct completion *c, unsigned long t) { (void)t; return c->done; } static void cfg80211_connect_done(struct net_device *n, struct cfg80211_connect_resp_params *r, int g) @@ -93,11 +110,12 @@ def function(source, name): MAIN = r''' static struct brcmf_cfg80211_vif vif; static struct net_device nd; -static struct brcmf_if ifp = { &vif, &nd }; +static struct brcmf_pub pub; +static struct brcmf_if ifp = { &vif, &nd, &pub }; static void reset(unsigned long state, bool canceling) { memset(&vif, 0, sizeof(vif)); vif.sme_state = state | BIT(BRCMF_VIF_STATUS_READY); - vif.join_canceling = canceling; disassocs = connect_failures = 0; + vif.join_canceling = canceling; disassocs = connect_failures = resets = queued = 0; } #define CONNECTING BIT(BRCMF_VIF_STATUS_CONNECTING) int main(void) @@ -128,9 +146,18 @@ def function(source, name): assert(brcmf_cancel_pending_join(&ifp) == -EBUSY); assert(vif.join_canceling && connect_failures == 1 && disassocs == 1); assert(!test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif.sme_state)); - /* The next connect cancels again; still silent: the join is taken as gone, not a permanent -EBUSY. */ + /* The next connect cancels again; still silent: the station is reset, the flag kept, still refused. */ + assert(brcmf_cancel_pending_join(&ifp) == -EBUSY); + assert(vif.join_canceling && vif.join_reset && disassocs == 2 && resets == 1 && connect_failures == 1); + /* A late old-join event is still queued: no replacement yet, and no further firmware commands. */ + queued = 1; + assert(brcmf_cancel_pending_join(&ifp) == -EBUSY); + assert(vif.join_canceling && vif.join_reset && disassocs == 2 && resets == 1); + /* The queue drained (the event was ignored under the flag): the replacement is admitted. */ + queued = 0; assert(brcmf_cancel_pending_join(&ifp) == 0); - assert(!vif.join_canceling && disassocs == 2 && connect_failures == 1); + assert(!vif.join_canceling && !vif.join_reset && disassocs == 2 && resets == 1); + assert(!test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif.sme_state)); cases++; /* A retry that gets the terminal event also ends the cancel. */ reset(0, true); fw = FW_TERMINAL; @@ -147,11 +174,12 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument('--out', type=Path) parser.add_argument('--mutation', choices=['no-recheck', 'skip-retry-disassoc', 'retry-wedge', - 'no-barrier-arm']) + 'no-barrier-arm', 'admit-unfenced', 'no-reset']) args = parser.parse_args() source = CFG.read_text() body = ''.join(function(source, name) for name in - ('brcmf_join_canceling', 'brcmf_set_join_canceling', 'brcmf_cancel_pending_join')) + ('brcmf_join_canceling', 'brcmf_set_join_canceling', 'brcmf_station_reset', + 'brcmf_cancel_pending_join')) if args.mutation == 'no-recheck': # The timeout path no longer sees a join that ended through the normal path. body = body.replace('if (!retry &&\n\t\t !test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state))\n\t\t\tgoto ended;\n', '') @@ -166,6 +194,13 @@ def main(): elif args.mutation == 'no-barrier-arm': # Cancel even when the join already ended before the flag was armed. body = body.replace('\tif (!retry && !test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state))\n\t\tgoto ended;\n', '') + elif args.mutation == 'admit-unfenced': + # Admit after the reset without waiting for queued events. + body = body.replace('\t\tif (!brcmf_fweh_idle(ifp->drvr))\n\t\t\treturn -EBUSY;\n', '') + elif args.mutation == 'no-reset': + # The d3b50118 behaviour: a second silent cancel admits at once. + body = body.replace('\t\tif (!brcmf_station_reset(ifp))\n\t\t\tvif->join_reset = true;\n\t\treturn -EBUSY;\n', + '\t\tgoto cancelled;\n') code = PREFIX + body + MAIN def run(out): diff --git a/tools/asahi/brcmf-suspend-tests.py b/tools/asahi/brcmf-suspend-tests.py index 73257246f64de..b7d34f1a3e6ad 100644 --- a/tools/asahi/brcmf-suspend-tests.py +++ b/tools/asahi/brcmf-suspend-tests.py @@ -64,7 +64,7 @@ def function(source, name): struct completion { bool done; }; struct wireless_dev { int iftype; }; struct brcmf_cfg80211_vif { unsigned long sme_state; struct wireless_dev wdev; struct brcmf_cfg80211_vif *next; - struct completion link_down; }; + struct completion link_down; struct completion join_cancel_done; bool join_canceling, join_reset; }; struct brcmf_if { struct brcmf_cfg80211_vif *vif; struct brcmf_pub *drvr; struct net_device *ndev; unsigned netif_stop; int netif_stop_lock, bsscfgidx; }; struct device { void *data; }; @@ -205,9 +205,11 @@ def function(source, name): #define WLAN_STATUS_AUTH_TIMEOUT 16 static bool test_and_clear_bit(int i, unsigned long *p) {bool o=test_bit(i,p);*p&=~BIT(i);return o;} struct cfg80211_connect_resp_params {int status;}; -static unsigned connect_failures, cancel_releases; +static unsigned connect_failures; static void cfg80211_connect_done(struct net_device *n,struct cfg80211_connect_resp_params *r,int g) {(void)n;(void)g;assert(r->status==WLAN_STATUS_AUTH_TIMEOUT);connect_failures++;} -static void brcmf_set_join_canceling(struct brcmf_cfg80211_vif *v,bool b) {(void)v;assert(!b);cancel_releases++;} +static void reinit_completion(struct completion *c) {c->done=false;} +static bool brcmf_join_canceling(struct brcmf_cfg80211_vif *v) {return v->join_canceling;} +static void brcmf_set_join_canceling(struct brcmf_cfg80211_vif *v,bool b) {v->join_canceling=b;} #define list_for_each_entry(v,h,m) for ((v)=(h)->first;(v);(v)=(v)->next) struct cfg80211_wowlan {bool any, magic_pkt;}; #define BRCMF_FEAT_WOWL 3 @@ -257,7 +259,6 @@ def function(source, name): /* A DISASSOC goes out for a connected vif; the firmware's link down event completes its link_down. */ static unsigned link_downs, link_waits; static bool brcmf_link_down(struct brcmf_cfg80211_vif *v,int r,bool b) {bool sent=v->sme_state&BIT(BRCMF_VIF_STATUS_CONNECTED);(void)r;(void)b;can_command();link_downs++;if(sent)v->link_down.done=true;return sent;} -static void reinit_completion(struct completion *c) {c->done=false;} static unsigned long msecs_to_jiffies(unsigned int m) {return m;} static unsigned long wait_for_completion_timeout(struct completion *c,unsigned long t) {assert(t==500);link_waits++;return c->done;} static void brcmf_delay(int t) {(void)t;} @@ -303,23 +304,51 @@ def function(source, name): assert(reinit_step == (fail ? fail : 4) && !cfg.suspended); cases++; } - /* The reinit's DOWN ends a pending (or cancelled) join: fail it once, release the cancel. */ - for (int connecting = 0; connecting < 2; connecting++) { + /* + * The reinit's DOWN ends a pending or cancelled join: its host request fails once, and + * the join stays fenced (flag set, reset recorded) for the next connect to release. + */ + for (int state = 0; state < 3; state++) { struct brcmf_cfg80211_vif vif = { - .sme_state = BIT(BRCMF_VIF_STATUS_READY) | (connecting ? BIT(BRCMF_VIF_STATUS_CONNECTING) : 0), + .sme_state = BIT(BRCMF_VIF_STATUS_READY) | (state == 1 ? BIT(BRCMF_VIF_STATUS_CONNECTING) : 0), .wdev.iftype = NL80211_IFTYPE_STATION, + .join_canceling = state == 2, }; reset(); ifp[0].vif = &vif; reinit_step = fail_step = 0; configured_pm = 2; - connect_failures = cancel_releases = 0; + connect_failures = 0; cfg.suspended = true; assert(brcmf_cfg80211_resume(&wiphy) == 0); - assert(connect_failures == (unsigned)connecting && cancel_releases == 1); + assert(connect_failures == (state == 1)); + assert(vif.join_canceling == (state != 0) && vif.join_reset == (state != 0)); assert(!test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif.sme_state)); cases++; } + /* + * A DOWN that fails ends no join: a pending join is not fenced and keeps its events, + * and a join already being cancelled stays fenced. + */ + for (int state = 1; state < 3; state++) { + struct brcmf_cfg80211_vif vif = { + .sme_state = BIT(BRCMF_VIF_STATUS_READY) | (state == 1 ? BIT(BRCMF_VIF_STATUS_CONNECTING) : 0), + .wdev.iftype = NL80211_IFTYPE_STATION, + .join_canceling = state == 2, + }; + reset(); + ifp[0].vif = &vif; + reinit_step = 0; + fail_step = 2; + configured_pm = 2; + connect_failures = 0; + cfg.suspended = true; + assert(brcmf_cfg80211_resume(&wiphy) == -EIO); + assert(reinit_step == 2 && !cfg.suspended && connect_failures == 0); + assert(vif.join_canceling == (state == 2) && !vif.join_reset); + assert(test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif.sme_state) == (state == 1)); + cases++; + } /* Non-station or not-ready VIF skips non-WoW resume reinitialization. */ for (int ap = 0; ap < 2; ap++) { struct brcmf_cfg80211_vif vif = { @@ -488,7 +517,7 @@ def main(): parser.add_argument('--mutation', choices=['country-gate', 'queue-reasons', 'worker-gate', 'resume-order', 'tx-drain', 'lpas-resume', 's2idle-d3', 'wowl-s2idle-only', 'restore-pm', - 'resume-join']) + 'resume-join', 'down-error-fence']) args = parser.parse_args() sources = {name: (DRIVER / (name + '.c')).read_text() for name in ('cfg80211', 'core', 'pcie', 'msgbuf')} @@ -522,7 +551,9 @@ def main(): elif args.mutation == 'restore-pm': body = body.replace('BRCMF_C_SET_PM, pm_mode)', 'BRCMF_C_SET_PM, 0)') elif args.mutation == 'resume-join': - body = body.replace('\t\tbrcmf_set_join_canceling(ifp->vif, false);\n', '') + body = body.replace('\t\t\tifp->vif->join_reset = true;\n', '') + elif args.mutation == 'down-error-fence': + body = body.replace('\t\t\tif (!was_canceling)\n\t\t\t\tbrcmf_set_join_canceling(ifp->vif, false);\n', '') elif args.mutation == 's2idle-d3': body = body.replace('if (devinfo->fwseed && pm_suspend_target_state == PM_SUSPEND_TO_IDLE)', 'if (false)') diff --git a/tools/asahi/run-host-tests.py b/tools/asahi/run-host-tests.py index c79214b533517..850da52fecf52 100755 --- a/tools/asahi/run-host-tests.py +++ b/tools/asahi/run-host-tests.py @@ -215,11 +215,13 @@ def mutations(flag, names, base, expect=KILL): 'brcmf-sae-vendor-tests.py': [(out(), PASS)] + [(out() + ['--ref', '9260e1f021e075713931f3c23c0a5fa1da936b0b'], KILL)], 'brcmf-join-cancel-tests.py': [(out(), PASS)] - + mutations('--mutation', ('no-recheck', 'skip-retry-disassoc', 'retry-wedge', 'no-barrier-arm'), out()), + + mutations('--mutation', ('no-recheck', 'skip-retry-disassoc', 'retry-wedge', 'no-barrier-arm', + 'admit-unfenced', 'no-reset'), out()), 'brcmf-suspend-tests.py': [(out(), PASS)] + mutations('--mutation', ('country-gate', 'queue-reasons', 'worker-gate', 'resume-order', 'tx-drain', 'lpas-resume', - 'wowl-s2idle-only', 'restore-pm', 'resume-join'), out()), + 'wowl-s2idle-only', 'restore-pm', 'resume-join', + 'down-error-fence'), out()), 'brcmf-wowl-filter-tests.py': [(out(), PASS)] + mutations('--mutation', ('base-order', 'mask-last', 'totsize', 'no-track'), out()), 'brcmfmac-scan-timeout-tests.py': [(out('--output'), PASS)],