From 17379d1a45da3bc48a8ceeecede3fe6b0afafdfb Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 1 Dec 2026 23:10:46 +0700 Subject: [PATCH] wifi: apple: centauri: keep the association through a deep sleep (WoWLAN) A deep sleep refused to start while the station was associated, so cfg80211 left the network first and the machine came back offline until the supplicant joined again. The firmware runs on through the sleep, and its host sleep request has a state that keeps the association: 1, where 3 is the unassociated sleep. Advertise WoWLAN "any". When userspace arms it, the wiphy suspend keeps the association for a deep sleep (on s2idle nothing services the link, so it still leaves first, as without WoWLAN), and the device suspend sends the associated sleep state. It also lets the connection's loss, the disconnect, the scan quota and the keepalive and service wake events through to the host. At resume the events that came during the sleep are drained, so a link lost in the sleep is reported at once, and the log says whether the association was kept. No packet patterns, magic packet or disconnect triggers are advertised: the firmware is not given any. Signed-off-by: Nick --- .../wireless/apple/centauri/centauri_wifi.c | 99 ++++++++++++++++--- 1 file changed, 88 insertions(+), 11 deletions(-) diff --git a/drivers/net/wireless/apple/centauri/centauri_wifi.c b/drivers/net/wireless/apple/centauri/centauri_wifi.c index bb8ff60fa866f..2d69f217334ad 100644 --- a/drivers/net/wireless/apple/centauri/centauri_wifi.c +++ b/drivers/net/wireless/apple/centauri/centauri_wifi.c @@ -129,6 +129,8 @@ struct centauri_wifi { size_t reply_capacity, reply_size; bool stopping; bool transport_paused, transport_sleep_sent, host_sleep_sent; + /* WoWLAN: armed by cfg80211 for this sleep; the sleep kept the association */ + bool wowlan_armed, wowlan_sleep; bool host_events_fallback_logged; bool connecting, connected, connect_event, disconnect_event; bool connect_privacy; @@ -3599,6 +3601,46 @@ static int wifi_dump_survey(struct wiphy *wiphy, struct net_device *netdev, return ret; } +#ifdef CONFIG_PM +/* + * WoWLAN "any": keep the association through a deep sleep. The firmware runs + * on through it, and is told the host sleeps while associated, so it goes on + * reporting the connection's loss. No packet patterns are programmed. + */ +static const struct wiphy_wowlan_support centauri_wowlan_support = { + .flags = WIPHY_WOWLAN_ANY, +}; + +/* + * Returning 1 has cfg80211 leave the network first, as without WoWLAN. Only a + * deep sleep tells the firmware the host sleeps (centauri_wifi_suspend()); across + * s2idle nothing would service the link, so it is left as before. + */ +static int wifi_suspend(struct wiphy *wiphy, struct cfg80211_wowlan *wowlan) +{ + struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); + + mutex_lock(&wifi->lock); + wifi->wowlan_armed = wowlan && wowlan->any && pm_suspend_via_firmware() && + wifi->connected && !wifi->connecting && + !wifi->scan_request && !wifi->transport_paused && + !READ_ONCE(wifi->stopping) && + readl(wifi->a.bar + 8) == 2; + mutex_unlock(&wifi->lock); + return wowlan && !wifi->wowlan_armed ? 1 : 0; +} + +static int wifi_resume(struct wiphy *wiphy) +{ + struct centauri_wifi *wifi = *(struct centauri_wifi **)wiphy_priv(wiphy); + + mutex_lock(&wifi->lock); + wifi->wowlan_armed = false; + mutex_unlock(&wifi->lock); + return 0; +} +#endif + static const struct cfg80211_ops wifi_ops = { .scan = wifi_scan, .abort_scan = wifi_abort_scan, @@ -3610,6 +3652,10 @@ static const struct cfg80211_ops wifi_ops = { .dump_station = wifi_dump_station, .get_channel = wifi_get_channel, .dump_survey = wifi_dump_survey, +#ifdef CONFIG_PM + .suspend = wifi_suspend, + .resume = wifi_resume, +#endif }; static int net_open(struct net_device *netdev) @@ -4704,6 +4750,9 @@ int centauri_wifi_register(struct centauri *c) 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); +#ifdef CONFIG_PM + wifi->wiphy->wowlan = ¢auri_wowlan_support; +#endif wiphy_ext_feature_set(wifi->wiphy, NL80211_EXT_FEATURE_SAE_OFFLOAD); centauri_wifi_set_address(wifi); ret = radio_start(wifi); @@ -4803,13 +4852,23 @@ static int alpha_transport_state(struct centauri_wifi *wifi, bool asleep) #define CEN_HOST_EVENTS_LEGACY 85 #define CEN_HOST_EVENTS_FULL 91 +/* + * The events the firmware still passes to a sleeping host: unassociated, the + * ones the host acts on at the wake; associated (WoWLAN), also the connection's + * loss, the disconnect, the scan quota and the keepalive and service wakes. + */ +static const u8 host_sleep_events[] = { 24, 26, 27, 15, 51, 8, 9, 10, 84 }; +static const u8 host_sleep_events_wowlan[] = { + 79, 83, 3, 4, 33, 24, 26, 27, 15, 51, 8, 9, 10, 84, +}; + 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 }; + const u8 *sleep_events = host_sleep_events; u8 payload[3 + 5 + 4 + 1 + CEN_HOST_EVENTS_FULL] = {}; u8 events[1 + CEN_HOST_EVENTS_FULL] = {}; - u8 sleep_option = 1; + u8 sleep_option = 1, state = 3; size_t size = 3; unsigned int i; int ret; @@ -4818,11 +4877,17 @@ static int alpha_host_power_send(struct centauri_wifi *wifi, bool asleep, if (readl(wifi->a.bar + 8) != 2) return -EIO; if (asleep) { - put_unaligned_le16(3, payload); - payload[2] = 3; + /* Sleep state 1 keeps the association, 3 is the unassociated sleep. */ + events[0] = ARRAY_SIZE(host_sleep_events); + if (wifi->connected) { + state = 1; + sleep_events = host_sleep_events_wowlan; + events[0] = ARRAY_SIZE(host_sleep_events_wowlan); + } + put_unaligned_le16(state, payload); + payload[2] = state; append_tlv(payload, &size, 303, &sleep_option, sizeof(sleep_option)); - events[0] = ARRAY_SIZE(sleep_events); - memcpy(events + 1, sleep_events, sizeof(sleep_events)); + memcpy(events + 1, sleep_events, events[0]); } else { events[0] = event_count; for (i = 0; i < event_count; i++) @@ -4831,9 +4896,10 @@ static int alpha_host_power_send(struct centauri_wifi *wifi, bool asleep, append_tlv(payload, &size, 280, events, 1 + event_count); ret = command_control(wifi, 6, payload, size, true); 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)); + "Alpha firmware host %s (state=%u events=%u tlv280=%u): ret=%d status=%u exec=%u\n", + asleep ? "sleep" : "awake", asleep ? state : 0, events[0], + 1 + event_count, ret, wifi->reply_status, + readl(wifi->a.bar + 8)); return ret; } @@ -4924,6 +4990,14 @@ int centauri_wifi_resume(struct centauri *c) return 0; mutex_lock(&wifi->lock); ret = alpha_resume_locked(wifi); + if (wifi->wowlan_sleep && !ret) + drain(wifi); + if (wifi->wowlan_sleep) + dev_info(&c->pdev->dev, + "Wi-Fi slept associated (WoWLAN): resume rc=%d, %s\n", ret, + wifi->connected && !wifi->disconnect_event ? + "association kept" : "disconnected"); + wifi->wowlan_sleep = false; mutex_unlock(&wifi->lock); return ret; } @@ -4942,11 +5016,13 @@ int centauri_wifi_suspend(struct centauri *c) if (!wifi || !pm_suspend_via_firmware()) return 0; mutex_lock(&wifi->lock); - if (wifi->transport_paused || wifi->connected || wifi->connecting || wifi->scan_request || - READ_ONCE(wifi->stopping)) { + /* Associated only when cfg80211 armed WoWLAN for this sleep (wifi_suspend()). */ + if (wifi->transport_paused || (wifi->connected && !wifi->wowlan_armed) || + wifi->connecting || wifi->scan_request || READ_ONCE(wifi->stopping)) { mutex_unlock(&wifi->lock); return -EBUSY; } + wifi->wowlan_sleep = wifi->connected; mutex_lock(&wifi->fwlog_lock); WRITE_ONCE(wifi->transport_paused, true); mutex_unlock(&wifi->fwlog_lock); @@ -4964,6 +5040,7 @@ int centauri_wifi_suspend(struct centauri *c) ret = alpha_transport_state(wifi, true); } if (ret) { + wifi->wowlan_sleep = false; restore = alpha_resume_locked(wifi); if (restore) dev_err(&c->pdev->dev,