Skip to content

usb: typec: tipd: close the USB4 session before a reboot or power off - #2101

Merged
DjDeveloperr merged 3 commits into
aurora-silicon:integration/aurorafrom
joshuaswarren:w88/tipd-reboot-notifier
Nov 20, 2026
Merged

DjDeveloperr merged 3 commits into
aurora-silicon:integration/aurorafrom
joshuaswarren:w88/tipd-reboot-notifier

Conversation

@joshuaswarren

@joshuaswarren joshuaswarren commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

[Joshua's agent] Fixes a plain systemctl reboot that hangs on a 14-inch M5 Max MacBook Pro with a Studio Display on the Thunderbolt port. One fix, one file group (drivers/usb/typec/tipd/).

Cause

On the hanging boots the last thing the journal shows is a clean Finished System Reboot, and the machine stays off until a reset. In a photo of the stuck panel from an earlier boot, the WARN trace ran typec_thunderbolt_switch_set from cd321x_update_work. The late OFF reaches apple_cio_tbt_switch_set, then apple_cio_stop, which destroys the NHI and waits for the Thunderbolt domain to be released while holding acio->lock. By then the PCIe-C controller has started to shut down, so the quiesce handshake times out (failed to quiesce PCIe-C before NHI shutdown: -110) and device_shutdown() waits behind it.

Change

A reboot notifier on every CD321x/SN201202x port. It runs in kernel_restart_prepare(), before system_state changes and before device_shutdown(), for SYS_RESTART, SYS_HALT and SYS_POWER_OFF. It disables the port's interrupt, stops its update work, and sends TYPEC_THUNDERBOLT_SWITCH_OFF to the router. The session closes while the lanes still carry the handshake, and no cable event can reach the router afterwards. It logs once before and once after the close. It does not change the USB role or the mux. A runtime unplug and suspend are unchanged.

Tests

  • KUnit, arm64 under qemu, two new tests (cd321x_tbt_reboot_closes_session_test, cd321x_tbt_reboot_other_action_test). With the OFF call removed: 121 of 122 pass and the closes-session test fails. With it: 122 of 122 pass.
  • Hardware: 14-inch M5 Max, Studio Display docked, three plain systemctl reboot cycles on a kernel with this change plus one unrelated display brightness commit, on top of the tree that hung before. The host came back in 33, 32 and 37 seconds. Netconsole, same order in all three (cycle 1 shown):
sn201202x 2-0c: closing the USB4/Thunderbolt session before shutdown
pcie-apple 1c70000000.pcie: PCIe-C hierarchy quiesced before NHI shutdown
sn201202x 2-0c: closed the USB4/Thunderbolt session before shutdown: 0
macsmc-reboot macsmc-reboot: Preparing for reboot (rest (0x72657374))

After each boot: /dev/dri had card1, card2, renderD128; hyprctl monitors listed eDP and DP-1 at 5120x2880; thunderbolt 0-1 enumerated the Studio Display.

Baseline, and what is not proven

  • Before this change, plain reboots on this machine failed to come back in 11 of 11 cases in one count since 2026-11-15, but one plain reboot from the same kernel did come back, and there is no per-boot list for the 11. Treat the baseline as a count, not a measurement. Three clean cycles also do not rule out a rare stall: an earlier candidate returned in 4 of 5 cycles and took 140 s in the fifth, with no log line that explains it.
  • The notifier's proof is the log order above: the close runs before Preparing for reboot. Netconsole stops answering inside device_shutdown(), so a missing line there would not have shown the cause before this change.
  • The first cycle started from the first boot of the new kernel (how that boot was reached is not recorded); cycles 2 and 3 started from boots reached by a plain reboot.
  • A different cause for the failed restarts has been proposed from reading the code (unverified PMU scratchpad cell offsets in t6050-die0.dtsi). It is not tested and not excluded. These cycles show that closing the session first clears the docked hang; they do not show it is the only cause of a failed restart.
  • NOT CHECKED: kexec; docked sleep and wake; any machine other than the 14-inch M5 Max; a Thunderbolt device other than the Studio Display.

On a 14-inch M5 Max with a Studio Display on the Thunderbolt port, a
reboot could hang for minutes. systemd's last step, device_shutdown(),
blocked in the Thunderbolt NHI teardown. A cable event closed the ACIO
session from the CD321x update work after the PCIe-C controller had
begun to shut down, so the PCIe-C reset handshake of the tunnel timed
out ("failed to quiesce PCIe-C before NHI shutdown: -110") and
device_shutdown() waited behind it.

Close the session earlier, while the system still runs and the lanes
carry the handshake. A reboot notifier runs before system_state changes
and before device_shutdown(). For SYS_RESTART, SYS_HALT and
SYS_POWER_OFF it disables the port's interrupt, stops its work, and
sends TYPEC_THUNDERBOLT_SWITCH_OFF to the router. After that no cable
event can reach the router, so the late close never starts.

The notifier logs once before and once after the close, so a reboot log
shows whether the session was closed in order.

Add two KUnit tests: a reboot sends OFF and stops the port's update
work, and any other notifier action leaves the port alone.

Tested: KUnit (cd321x suites on arm64 under qemu): 122 of 122 pass, and
with the OFF call removed the new reboot test fails. On a 14-inch M5 Max
with a Studio Display docked, three plain reboots with this change
returned in 33, 32 and 37 seconds. Each log shows "closing the
USB4/Thunderbolt session before shutdown", the PCIe-C quiesce, "closed
the USB4/Thunderbolt session before shutdown: 0", then "Preparing for
reboot", and the display and Thunderbolt device were back after each
boot. Not tested: kexec.

Signed-off-by: Joshua Warren <816217+joshuaswarren@users.noreply.github.com>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ❌ Failed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ❌ Failed 2026-11-16T21:01:45.282461Z
We couldn’t complete this review. Comment @codex review to try again.
b3d63e0 PR opened
🔒 Security Review ❌ Failed 2026-11-16T21:01:56.060661Z
We couldn’t complete this review. Comment @codex security review to try again.
b3d63e0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.
To continue using code reviews, add credits to your account and enable them for code reviews in your settings.

@chatgpt-codex-connector

Copy link
Copy Markdown

The account paying for this security review has reached its Codex usage limits. The payer can check the Codex usage dashboard. For personal accounts, using credits requires enabling “Use credits for security reviews” in Code review settings. If you do not manage the paying account, contact this repository's admins.

@joshuaswarren

Copy link
Copy Markdown
Contributor Author

[Joshua's agent] Docked sleep and wake on the same kernel as the three reboots (this change plus an unrelated display brightness commit), 14-inch M5 Max with the Studio Display docked: one s2idle cycle with an RTC alarm passed.

  • Suspend entry 15:03:46, exit 15:05:01 (75 s). The host answered on ssh again at 15:05:01.
  • Before and after: both screens connected and enabled with modes (eDP at 3024x1986@120, the Studio Display at 5120x2880@60), renderD128 present, the Studio Display's USB audio device still listed, same connectors and status, thunderbolt 0-1 still enumerated.
  • One warning source after wake: 189 WARN lines at drivers/thunderbolt/ctl.c:1138 (tb_cfg_read: -107, -ENOTCONN) within the same second. Every one comes from the work item apple_dpin_event_fn calling apple_dpin_diag(), a local debugging sampler that reads the DP IN adapter registers. That function is not in this PR and not in integration/aurora (checked: no match in drivers/thunderbolt/apple.c there). So the warnings do not come from this change. The notifier runs only for reboot, halt and power off.
  • INFERENCE: the reads return -ENOTCONN because the Thunderbolt control channel is not running yet at that point of the wake. Not verified.
  • One cycle only. NOT CHECKED: repeated cycles, a wake by key, and an undocked wake.

cd321x_register_port() now sets reboot_nb up and registers it, so the extracted probe no longer compiled in the typec-probe and probe-mux-pm-dispatch units. Give the fixture's struct cd321x the two new members, give notifier_block a priority, and mock register and unregister. Check that probe registers the notifier with cd321x_reboot_notify and that every case unregisters it.
@joshuaswarren

joshuaswarren commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

GREEN on 367a01b
Model: devin/swe-2-flash-medium

[Joshua's agent]

Reviewed: reboot notifier (priority INT_MAX) on reboot_notifier_list — verified in kernel/reboot.c that this list runs via blocking_notifier_call_chain BEFORE device_shutdown(), so disable_irq and typec_thunderbolt_switch_set sleeping is legal and the session closes while the ACIO is still up — exactly the stated intent. Unregister ordering in cd321x_unregister_port is correct; ret is logged not propagated (right for shutdown). Other actions return NOTIFY_DONE; KUnit pins both paths plus the pm.py selftest mock updates. Note the request described 'before unplug' — the diff is reboot/halt/power-off only; no hot-unplug path is touched, so the block-on-error-path concern does not apply here.

@joshuaswarren

joshuaswarren commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

GREEN on 367a01b
Model: cognition/swe-2-flash

[Joshua's agent]

Reviewed the full diff at that head plus the surrounding core.c/mux.c paths it calls.

What I verified in source:

  • cd321x_reboot_close is NULL-safe: typec_thunderbolt_switch_set returns 0 on IS_ERR_OR_NULL(sw) (mux.c), so ports with no bound router (the common case — tbt_switch can be NULL after cd321x_tbt_switch_get) cannot fault at reboot. The dev_info lines are also gated on tbt_switch, so non-TBT ports stay quiet.
  • cd321x_stop_work (called first inside cd321x_reboot_close) drains every producer: cancels tbt_attach_work, resume_work, update_work, rearm_work, unregisters the tbt switch notifier and the late watch. After it returns, no cable event can reach the router — matches the commit's claim.
  • Registration sits after the last goto err_* in cd321x_register_port, so a failed register_reboot_notifier only warns and leaves reboot_nb_registered false; cd321x_unregister_port unregisters only when registered. No leak, no double-free.
  • Notifier context is sound: reboot notifiers run in kernel_restart_prepare() (blocking chain, system_state still SYSTEM_RUNNING), so the sleeping I2C/MMIO path inside ->set is legal — which is exactly why doing it here beats device_shutdown().
  • disable_irq(tps->irq) is guarded on tps->irq and disable_irq (not nosync) so an in-flight handler completes before the close.
  • Tests: the two new KUnit cases cover SYS_RESTART->OFF and non-shutdown action->NOTIFY_DONE; pm.py mocks mirror the new fields and assert register/unregister balance. Mutation evidence in the body (OFF call removed -> closes-session test fails) is the right shape.
  • Hardware evidence is honestly scoped: 3 clean cycles with netconsole log order proving close-before-Preparing for reboot, explicit NOT-CHECKED list (kexec, docked suspend, other machines, other TBT devices), and the baseline stated as a count not a measurement.

Nits, non-blocking: no KUnit case for reboot with tbt_switch == NULL (safe by inspection via the mux.c guard, but a one-line case would pin it); the notifier returns NOTIFY_OK even when the close fails, which is fine for shutdown but means a failed close is only visible in the log line.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks. This is a clean fix. It closes the session in kernel_restart_prepare() before device_shutdown(), it's NULL-safe for ports without a router, and the netconsole order plus the KUnit mutant prove what it does. It runs on every CD321x/SN201202x port, so there's no gate to discuss. The Codex runs failed on quota, so there are no bot findings to answer.

Nothing is missing from your side. Because the notifier now runs on every Apple Type-C port, we'll run no-regression rows before it goes on: systemctl reboot x3 and poweroff x1 on our M1 Pro (T6000) and M4 Pro (T6040) with a Thunderbolt dock attached, and on our M2 (T8112) and A18 Pro (T8140) with a USB-C hub. We'll post the results here.

Optional: one KUnit case for a reboot with tbt_switch == NULL would pin the no-router path.

@joshuaswarren

Copy link
Copy Markdown
Contributor Author

Note

🤖 OpenAI Codex responding on behalf of joshuaswarren

Optional: one KUnit case for a reboot with tbt_switch == NULL would pin the no-router path.

All three reboot runs with a Studio Display passed on the 14-inch M5 Max MacBook Pro. The logs put the USB4 close before reboot prep. The test kernel also had a patch for screen brightness. Those runs don't cover the other Macs or power-off.

typec_thunderbolt_switch_set() returns 0 for a NULL switch. There is no KUnit case for that path on this head.

@RyanTheTide

Copy link
Copy Markdown
Contributor

Thanks for the three M5 Max reboot runs with the USB4 close logged before reboot prep. That covers the T6050 side, and you don't owe a power-off row there.

The other Macs and power-off are on us: systemctl reboot x3 and poweroff x1 on our M1 Pro and M4 Pro with a Thunderbolt dock, and on our M2 and A18 Pro with a USB-C hub. Those are queued with our lab, and we'll post the results here. The NULL-switch KUnit case stays optional and doesn't block this.

@DjDeveloperr DjDeveloperr 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.

Thanks. This is a clean fix, and I agree with Ryan and the two GREENs. The notifier runs from kernel_restart_prepare() (which kernel_kexec() also goes through) before device_shutdown(), so the close can sleep and happens while ACIO is still up. typec_thunderbolt_switch_set() returns 0 for a port without a router, so ports without one stay quiet.

Two things I checked against the tip:

  • cd321x_stop_work() is safe to run twice, first from the notifier and then from a later unbind: each unregister sits behind its *_registered flag, and the cancels are idempotent.
  • integration/aurora has changed core.c and tps6598x.h since your base: 78ad8ef (#1889, SN201202x idle sleep) and d5d1f33 (the J714s replug). A local three-way merge of both files is clean. cd321x_stop_work() doesn't cancel the new J714s replug_work, but with the port's IRQ off, a replug that runs after the notifier can't reach the router, so nothing is needed here.

host-tests is green on 367a01b. Approving. I'll update the branch and merge once host-tests is green on the merged head.

Ryan's rows come after the merge, with a revert ready: systemctl reboot x3 and poweroff x1 on the M1 Pro and M4 Pro with a Thunderbolt dock, and on the M2 and A18 Pro with a USB-C hub. The NULL-switch KUnit case stays optional.

@DjDeveloperr
DjDeveloperr merged commit e45e3fd into aurora-silicon:integration/aurora Nov 20, 2026
1 check passed
@DjDeveloperr

Copy link
Copy Markdown
Contributor

Merged as e45e3fd (host-tests green on the updated head 8720092). Post-merge verification as in my review: Ryan's systemctl reboot x3 and poweroff x1 rows on the M1 Pro and M4 Pro with a Thunderbolt dock, and on the M2 and A18 Pro with a USB-C hub. A revert is ready if it doesn't hold.

@joshuaswarren

Copy link
Copy Markdown
Contributor Author

Thanks for the merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants