Skip to content

Bluetooth: hci_bcm4377: don't rebind while the firmware boots after a sleep - #4414

Merged
DjDeveloperr merged 3 commits into
aurora-silicon:mainfrom
JJRPF:fix/bcm4377-no-rebind-during-post-sleep-boot
Dec 5, 2026
Merged

DjDeveloperr merged 3 commits into
aurora-silicon:mainfrom
JJRPF:fix/bcm4377-no-rebind-during-post-sleep-boot

Conversation

@JJRPF

@JJRPF JJRPF commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

What: After a sleep that cut the controller's power, power_lost_work boots the firmware again, which takes about 2.5 s on the BCM4387 in a J414s. A command sent meanwhile, here bluetoothd writing the class of device (0x0c24), times out after 2 s, before that boot is done, and bcm4377_hci_reset() queues a rebind. The rebind races the boot: its device_reprobe() can fail with -EBUSY, which leaves the function without a driver and hci0 gone until reboot (a manual bind gets EBUSY too):

Bluetooth: hci0: command 0x0c24 tx timeout
Bluetooth: hci0: controller not responding, rebinding the driver
Bluetooth: hci0: firmware booted again after the sleep
pci 0000:01:00.1: reprobe after command timeout failed: -16

This skips the rebind while power_lost_work is pending or running, and has a boot that fails ask for the rebind itself, so a controller that really doesn't come back is still recovered. The J493 resume guard and #2808 only cover the BCM4378; the BCM4387 and others take the plain rebind this changes. #4269's s2idle power-loss path queues the same work, so it is covered too.

Tested: J414s M2 Pro (BCM4387, 0x5f72), OS firmware 13.5 / system firmware 27.2, on aurora/main efef6707019e + #4313 + #4411, deep sleep:

  • The loss above happened in one test run, and the race came back in 2 of 4 wakes right after a fresh boot, when I sent class writes right after each wake (btmgmt class). Both of those rebinds happened to succeed.
  • With this change, 50+ deep wakes over the evening, including 20 with class writes over the first 3 s after each wake, kept hci0, bound and powered. No timeout hit the boot window in those runs, though, so the new branch itself hasn't run on hardware. I couldn't make the timing happen again with the old driver either (0/8 after a module reload, 0/20 later). If it does, the log shows "not rebinding".
  • On this branch (current main d0684a0604a0, with Bluetooth: hci_bcm4377: restore private PCI windows and reboot firmware after power-loss sleep #4269): hci_bcm4377.o builds clean (LLVM, arm64); tools/asahi bcm4377-completion, -resume (63 production controls), -sleep-config, -suspend-notifier and -id tests pass.

Risk: two-way door · only a command timeout while power_lost_work is pending or running, on any bcm4377 chip; the J493 guarded recovery (hdev->reset = bcm4377_hci_timeout) only reaches this through its non-epoch rebind. A boot that fails now rebinds from the work instead of from the timeout; from a PM callback (pci_locked) it doesn't, as before.

… sleep

After a deep sleep that cuts the controller's power, power_lost_work boots
the firmware again, which takes about 2.5 s on BCM4387 (J414s). A command
sent meanwhile, such as bluetoothd writing the class of device (0x0c24),
times out after 2 s, before that boot is done, and bcm4377_hci_reset()
queues a rebind. The rebind races the boot: its device_reprobe() can fail
with -EBUSY, which leaves the function without a driver and hci0 gone
until reboot:

  Bluetooth: hci0: command 0x0c24 tx timeout
  Bluetooth: hci0: controller not responding, rebinding the driver
  Bluetooth: hci0: firmware booted again after the sleep
  pci 0000:01:00.1: reprobe after command timeout failed: -16

Skip the rebind while power_lost_work is pending or running, and have a
boot that fails ask for the rebind itself, so a controller that really
does not come back is still recovered.

Signed-off-by: Justin Pfister <git@jjrpf.com>
@JJRPF
JJRPF requested review from a team as code owners December 5, 2026 04:49
@JJRPF
JJRPF requested review from Acelogic and scottjones December 5, 2026 04:49

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: REQUEST_CHANGES at 278d6a8.

Failure: If firmware boot succeeds but the HCI event-mask command times out during hci_resume_dev(), the timeout callback sees power_lost_work still running and suppresses the rebind. hci_resume_sync() ignores the event-mask status and returns 0. The boot-failure fallback has already been passed, so nothing queues a rebind. The exact driver and HCI functions replayed this on GCC and Clang with one rebind on base d0684a0 and zero on this head.

Please preserve timeout recovery for commands issued after firmware boot while retaining the boot-time deferral. The offline controls passed for a healthy resume, a failed boot in workqueue context, and a failed boot under the PCI device lock.

The previous change skipped the rebind for any command timeout while
power_lost_work was busy. That work also resumes the HCI core once the
firmware is back, and hci_resume_sync() ignores the status of the
commands it sends (the event mask, for one). A timeout there was then
dropped: the boot had succeeded, so nothing asked for a rebind.

Skip the rebind only while the work is queued or still booting the
firmware, and let a timeout once the firmware is back rebind as before.
A boot that fails still asks for the rebind itself.

Signed-off-by: Justin Pfister <git@jjrpf.com>
@JJRPF
JJRPF requested a review from DjDeveloperr as a code owner December 5, 2026 05:01
@JJRPF

JJRPF commented Dec 5, 2026 •

Copy link
Copy Markdown
Contributor Author

@RyanTheTide thanks, you're right. hci_resume_dev() runs inside the same work, so work_busy() covered it too, and a timeout there was dropped once the boot had succeeded.

Fixed in 2a109b4: the rebind is skipped only while power_lost_work is queued (work_pending()) or still in hci_reset_dev_sync(), i.e. booting the firmware (a power_lost_booting flag set and cleared around it in bcm4377_recover_power_lost()). Once the boot returns, a timeout rebinds as before, so the event-mask case in hci_resume_dev() gets its rebind. A failed boot still asks for the rebind itself from the workqueue, and still doesn't under the PCI device lock.

I replayed bcm4377_hci_reset() and bcm4377_recover_power_lost() as extracted from the three heads, with hci_reset_dev_sync()/hci_resume_dev() stubs that fire the timeout callback at the given point (GCC and Clang):

case base d0684a0 278d6a8 2a109b4
timeout during the post-sleep firmware boot, boot OK 1 rebind (the race) 0 0
timeout in hci_resume_dev() after the boot (your case) 1 0 1
timeout while the work is still queued 1 0 0
timeout during a failed boot, workqueue 1 1 1
failed boot under the PCI device lock 0 0 0
healthy resume / setup / no post-sleep work 0 / 0 / 1 same same

bcm4377-resume-tests.py needed the new field in its fixture's struct bcm4377_data (in the commit); with it the 63 production controls pass, and the completion, sleep-config, suspend-notifier and id tests pass too. The fixture stubs bcm4377_hci_reset(), so it doesn't reach this branch either way.

One gap I left: between the workqueue clearing the pending bit and the first line of the work, neither check is true, so a timeout landing in those few instructions rebinds as it did before this PR. Setting the flag at the queue_work() sites would close it, but then every cancel path would have to clear it. I'm happy to do that if you'd rather.

The new commit is on top of 278d6a8 so the change is easy to read; I'll squash the two before merge if you prefer one commit.

@github-actions github-actions Bot added area:gpu Area from CODEOWNERS (bot) area:wireless Area from CODEOWNERS (bot) changes-requested A reviewer requested changes (bot) door:two-way Revertible or not, from Merge risk (bot) needs-hw-row No hardware row at the PR head (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 5, 2026
@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Merge readiness at 2a109b4b: ready to merge

Check Detail
CI at head ok 1 check passed
Review ok approved by RyanTheTide
Requested changes ok none
Hardware row at head ok comment at 2a109b4b
Conflicts ok none
PR text ok Risk: two-way · only a command timeout while powerlostwork is pending or running, on any bcm4377 chip; the J493 guarded recovery (hdev->reset = bcm4377hcitimeout) only reaches this through its non-epoch rebind. A boot that fails now rebinds from the work instead of from the timeout; from a PM callback (pcilocked) it doesn't, as before.

Areas: area:gpu, area:wireless
Merge: gh pr merge 4414 --merge (Owners and Core through the bypass: add --admin).

Updated by pr-readiness on pushes, edits, reviews and comments, and on a schedule. Rules: merge policy · labels.

RyanTheTide
RyanTheTide previously approved these changes Dec 5, 2026

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 2a109b4

Checked: GCC and Clang replays of the driver and HCI core both queue a rebind for a post-boot HCI event-mask timeout. Healthy boot, boot failure, pending work and in-boot timeout controls retain their expected behavior.

Follow-ups: The workqueue clears PENDING before entering the worker and the driver sets power_lost_booting inside the worker. A concurrent timeout in that short interval can still queue a rebind, as on the base.

@github-actions github-actions Bot removed changes-requested A reviewer requested changes (bot) needs-review No approving review from Core or Omarchy Mac yet (bot) labels Dec 5, 2026
@satchlj

satchlj commented Dec 5, 2026

Copy link
Copy Markdown
Member

Hardware verification at exact head 2a109b4b61cfc2ff2a7921d299cd0726b6b4fa04 on J413 (MacBook Air 13" M2, T8112, macOS 27.2.1, BCM4387 14e4:5f71 rev 07): PASS

  • Build & setup: Built hci_bcm4377.ko from exact head 2a109b4b61cf (LLVM=1 W=1, 0 warnings, sha256: 16963f1185893994a01d8524b09e390e494b52fe93ce9032225aba209957e28e) against the running 7.3.0 arm64 kernel and swapped live on the default command line.
  • 5× 15 s PMU-RTC deep-sleep cycles (rtcwake -d rtc1 -m mem -s 15, suspend_stats/success +5, fail=0) with wake-window btmgmt --index 0 class 1 12 (0x0c24) writes every 0.4 s across the first 2.4 s of each wake:
    • 5/5 cycles executed the full power_lost_work post-sleep recovery path (post-sleep before reset: bootstage 0, RTI 0 → hardware reset recovery completed successfully → reloading calibration and PTB after hardware reset → restoring BD_ADDR ... after hardware reset → Bluetooth: hci0: firmware booted again after the sleep).
    • 5/5 cycles kept 0000:01:00.1 bound to hci_bcm4377, /sys/class/bluetooth/hci0 present, and bluetoothctl show reporting Powered: yes / PowerState: on.
    • 0 command timeouts, 0 spurious rebinds or -EBUSY reprobe failures, 0 DART or kernel faults; stock hci_bcm4377 restored cleanly afterward.
  • Not covered: On this J413 BCM4387, power_lost_work completed firmware re-boot in ~0.7–1.0 s (faster than the 2 s HCI command timeout), so no command timed out inside the power_lost_booting window to hit the "not rebinding" log branch directly on hardware (that branch is verified by the tools/asahi/bcm4377-resume host tests); s2idle power-loss path was not exercised in this deep-sleep run.

@github-actions github-actions Bot added ready-to-merge Meets the merge policy: merge it (bot) and removed needs-hw-row No hardware row at the PR head (bot) labels Dec 5, 2026
@RyanTheTide

Copy link
Copy Markdown
Contributor

Second-family hardware verification at exact head 2a109b4b61cf on J316s (MacBook Pro 16" M1 Pro, T6000, stub 13.5, BCM4387 14e4:5f71 rev 07) running 7.3.0-1675a62307d2-47318133913.1-aurora-edge: hardware PASS, but host test apple/bcm4377-state-wait fails to compile.

  • Hardware result (J316s): Exact-vermagic hci_bcm4377.ko built from 2a109b4b61cf (W=1 0 warnings, 113/113 undefined symbols matching stock) probed cleanly (hci0 powered, live discovery active), completed 2 consecutive s2idle cycles via apple-pmu-rtc (rtcwake -d rtc1 -m freeze -s 10, 0.224 s device suspend, 11 s elapsed each), and resumed with hci0 powered and live Bluetooth discovery active after both wakes (zero timeouts, rebinds, SErrors, or DART faults).
  • Blocking host-test failure found (python3 tools/asahi/run-host-tests.py --only bcm4377): 27/28 passed, 1 FAILED: apple/bcm4377-state-wait (tools/testing/selftests/drivers/apple/bcm4377-state-wait.py).
    • recovery_program() extracts and compiles bcm4377_hci_reset() with -Wall -Wextra -Werror, and its C stubs were not updated for the new references added in 2a109b4b61cf:
      • work_pending (implicit declaration)
      • struct bcm4377_data missing struct work_struct power_lost_work; and bool power_lost_booting;
      • bt_dev_warn (implicit declaration)
    • Note: this was hidden until now because the fork Host tests workflow run was awaiting approval (just approved so CI will reproduce it).
    • Adding power_lost_work/power_lost_booting to struct bcm4377_data plus stubs for work_pending() and bt_dev_warn() in tools/testing/selftests/drivers/apple/bcm4377-state-wait.py (ideally with a quick assertion covering the new power_lost_work/power_lost_booting guard in bcm4377_hci_reset()) will make host-tests green for merge.

Update the recovery harness for the post-sleep boot guard. Check that a timeout keeps the reserved rebind work intact while the boot is queued or in progress, and that the existing recovery path runs once the boot completes.

Signed-off-by: DjDeveloperr <hey@djdev.me>
@DjDeveloperr

Copy link
Copy Markdown
Contributor

I reproduced the affected host check failure on 2a109b4b61cf and pushed the fixture correction as 313badbcd9778c5653b9eff4cf3c4120f68f8da5. It adds checks for queued and in-progress firmware boot, while retaining the existing assertions that legacy recovery queues a rebind afterward.

python3 -I tools/asahi/run-host-tests.py --jobs 6 --only bcm4377 passed 28/28 on the corrected integration. The current main advance from d0684a0604a0 to f4ab0b4f35f3 changes only five unrelated Apple PCIe DTS files. The new integration is d77cb15dfd6b7c296aec674e408ffa300c265417 and passes git diff --check.

The follow-up changes only the host fixture. The driver blob is unchanged from the J413 deep-sleep and J316s s2idle rows. @RyanTheTide Please recheck the fixture and refresh your approval on the new head.

@RyanTheTide RyanTheTide left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict: MERGE at 313badb

Checked: Reproduced the recovery fixture's compile failure on 2a109b4 and ran the repaired exact bcm4377-state-wait fixture with GCC and Clang. The production recovery checks and all three existing negative controls passed. The new pending-work and firmware-boot assertions both defer rebind and the normal timeout still enqueues one. The only changed file is the fixture, and the driver source is byte-identical to the head tested on J413 and J316s.

@DjDeveloperr
DjDeveloperr merged commit ae8cbc5 into aurora-silicon:main Dec 5, 2026
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:gpu Area from CODEOWNERS (bot) area:wireless Area from CODEOWNERS (bot) door:two-way Revertible or not, from Merge risk (bot) ready-to-merge Meets the merge policy: merge it (bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants