Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions bin/omarchy-wifi-resume-fix
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#!/bin/bash

# omarchy:summary=Reload brcmfmac if Wi-Fi does not return after resume
# omarchy:group=system
# omarchy:hidden=true
# omarchy:requires-sudo=true

# The Broadcom firmware on Apple Silicon Macs (BCM4378/BCM4387) can wedge
# across s2idle: scans fail with -52 and every association is rejected with
# status_code=16, which NetworkManager surfaces as a wrong password. Toggling
# the radio does not reset the chip firmware - only a driver reload does.
# Upstream: https://github.com/AsahiLinux/linux/issues/439
#
# Run by omarchy-wifi-resume-fix.service after each resume (installed by
# install/hardware/apple/fix-wifi-resume.sh). Output goes to the journal:
# journalctl -u omarchy-wifi-resume-fix

WAIT_BEFORE=12 # backstop: reload anyway if wifi is still not up after this
WAIT_AFTER=30 # seconds to wait for reconnect after a reload
REJECTS=2 # ASSOC-REJECT status_code=16 events that confirm a wedge

START=$(date '+%Y-%m-%d %H:%M:%S')

# Journal position at start: entries after this cursor are post-resume no
# matter what the wall clock says. On some Apple Silicon kernels the clock
# steps backwards across resume, which would leave a --since window empty; an
# unwindowed read is worse, counting stale rejects from the last genuine
# wedge. The cursor is immune to both.
CURSOR=$(journalctl -q -n 0 --show-cursor 2>/dev/null | sed -n 's/^-- cursor: //p')

wifi_iface() {
nmcli -t -f DEVICE,TYPE device status 2>/dev/null |
awk -F: '$2 == "wifi" { print $1; exit }'
}

wifi_state() {
nmcli -t -f DEVICE,STATE device status 2>/dev/null |
awk -F: -v d="$1" '$1 == d { print $2; exit }'
}

# The wedged firmware rejects every association with status_code=16, repeating
# every few seconds. A healthy association never does this, so seeing it more
# than once confirms the wedge and we can reload immediately - typically ~7s
# after resume - rather than sitting out the full backstop timer.
# The \b keeps status_code=160-169 from counting as the signature.
REJECT_SIGNATURE='CTRL-EVENT-ASSOC-REJECT.*status_code=16\b'

wedged() {
local n
if [[ -n $CURSOR ]]; then
n=$(journalctl -q --after-cursor "$CURSOR" -t wpa_supplicant --no-pager 2>/dev/null |
grep -c "$REJECT_SIGNATURE")
else
# No cursor (unreadable journal), so a --since window is the best left. If
# the clock stepped backwards across resume it comes back empty and the
# backstop recovers instead - history from before suspend must not count.
n=$(journalctl -q --since "$START" -t wpa_supplicant --no-pager 2>/dev/null |
grep -c "$REJECT_SIGNATURE")
fi
((${n:-0} >= REJECTS))
}

# Respect a deliberately disabled radio - do not fight the user or rfkill.
if [[ $(nmcli radio wifi 2>/dev/null) == "disabled" ]]; then
echo "wifi radio is disabled, nothing to do"
exit 0
fi

# These are runtime invariants, but this runs from a bare systemd unit right
# after resume; a missing tool should say so in the journal rather than
# surface as a command-not-found mid-recovery.
for cmd in nmcli journalctl modprobe; do
if omarchy-cmd-missing "$cmd"; then
echo "missing $cmd - cannot recover Wi-Fi"
exit 1
fi
done

IFACE=$(wifi_iface)
: "${IFACE:=wlan0}"

i=0
while ((i < WAIT_BEFORE)); do
state=$(wifi_state "$IFACE")
if [[ $state == "connected" ]]; then
echo "wifi back after ${i}s on $IFACE - no reload needed"
exit 0
fi
if wedged; then
echo "wedged firmware confirmed after ${i}s (iface=$IFACE state=${state:-none}) - reloading brcmfmac"
break
fi
i=$((i + 1))
sleep 1
done

if ((i >= WAIT_BEFORE)); then
echo "wifi not back after ${WAIT_BEFORE}s (iface=$IFACE state=${state:-none}) - reloading brcmfmac"
fi

if ! modprobe -r brcmfmac_wcc brcmfmac; then
echo "failed to unload brcmfmac - giving up, a reboot is needed"
exit 1
fi
sleep 1
if ! modprobe brcmfmac; then
echo "failed to reload brcmfmac - giving up, a reboot is needed"
exit 1
fi
echo "brcmfmac reloaded, waiting for NetworkManager to reconnect"

i=0
while ((i < WAIT_AFTER)); do
IFACE=$(wifi_iface)
: "${IFACE:=wlan0}"
state=$(wifi_state "$IFACE")
if [[ $state == "connected" ]]; then
echo "reconnected ${i}s after reload on $IFACE"
exit 0
fi
i=$((i + 1))
sleep 1
done

echo "still not connected ${WAIT_AFTER}s after reload (iface=$IFACE state=${state:-none})"
exit 1
1 change: 1 addition & 0 deletions install/hardware/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ run_logged "$OMARCHY_INSTALL/hardware/apple/fix-spi-keyboard.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-suspend-nvme.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-t2.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-brcmfmac-supplicant.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-wifi-resume.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/fix-asahi-hid-race.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/enable-notch.sh"
run_logged "$OMARCHY_INSTALL/hardware/apple/audio.sh"
Expand Down
42 changes: 42 additions & 0 deletions install/hardware/apple/fix-wifi-resume.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# The Broadcom firmware on Apple Silicon Macs wedges across s2idle: scans fail
# with -52 and every association is rejected with status_code=16, which
# NetworkManager reports as a wrong password. Toggling the radio does not reset
# the chip firmware - only a driver reload clears it.
# Upstream: https://github.com/AsahiLinux/linux/issues/439
#
# Recover with omarchy-wifi-resume-fix from a service ordered after
# suspend.target, so it runs on resume without delaying it (a system-sleep
# hook would run synchronously and block every resume). The command reloads
# the driver when the wedge signature appears, or when Wi-Fi stays down past a
# short backstop, so it stays a no-op on machines and kernels where the
# firmware bug does not bite - and stops acting early by itself if the bug is
# ever fixed.
#
# BCM4378 (14e4:4425) and BCM4387 (14e4:4433) also appear in T2 Intel Macs,
# where suspend takes a different path - hence the architecture gate.
#
# BCM4388 (14e4:4434) in M2 Max/Ultra Macs is deliberately absent, not an
# oversight: one rode out a six-minute s2idle with zero ASSOC-REJECT
# status_code=16 events and zero scan -52 errors (verified on an M2 Max in
# the PR #255 review), so its firmware does not wedge.

if [[ $(uname -m) == "aarch64" ]] && lspci -nn | grep -E "14e4:(4425|4433)" >/dev/null; then
echo "Detected Apple Silicon Broadcom Wi-Fi; installing resume recovery"

cat > /etc/systemd/system/omarchy-wifi-resume-fix.service <<'EOF'
[Unit]
Description=Reload brcmfmac if Wi-Fi does not return after resume
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
After=NetworkManager.service

[Service]
Type=oneshot
ExecStart=/usr/bin/omarchy-wifi-resume-fix
TimeoutStartSec=120

[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
EOF

systemctl enable omarchy-wifi-resume-fix.service
fi
20 changes: 20 additions & 0 deletions migrations/1787753224.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
echo "Recover Wi-Fi after resume on Apple Silicon Macs"

# The Broadcom firmware on Apple Silicon Macs (BCM4378/BCM4387) can wedge
# across s2idle, leaving Wi-Fi able to scan but rejecting every association
# until the driver is reloaded (#197, upstream AsahiLinux/linux#439). Fresh
# installs get the recovery service from the hardware leaf; run the same leaf
# here for existing installs.

[[ $(uname -m) == "aarch64" ]] || exit 0
# grep without -q reads all of lspci's output: this runs under pipefail, where
# an early -q exit would kill a chatty lspci with SIGPIPE and read the failed
# pipeline as "no such hardware" (#6608).
lspci -nn | grep -E "14e4:(4425|4433)" >/dev/null || exit 0

# Another user on this machine may already have applied the repair.
if systemctl is-enabled --quiet omarchy-wifi-resume-fix.service 2>/dev/null; then
exit 0
fi

sudo bash "$OMARCHY_PATH/install/hardware/apple/fix-wifi-resume.sh"
Loading