From f6c74a6f16aa9aa44cb44b3f495c7fb5310bc26e Mon Sep 17 00:00:00 2001 From: Tyler Smith <74526874+tylertms@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:57:27 -0500 Subject: [PATCH] fix(xhci): give each command its own time past a device start budget and the reattached controller a fresh budget, where a hung first transfer ran the budget out and the Stop Endpoint that followed declared the controller dead at its doorbell --- crates/stela-drivers/src/probe.rs | 21 ++++++++++++---- crates/stela-drivers/src/usb/xhci/command.rs | 7 +++++- .../stela-drivers/src/usb/xhci/reset/tests.rs | 25 ++++++++++++++++--- 3 files changed, 43 insertions(+), 10 deletions(-) diff --git a/crates/stela-drivers/src/probe.rs b/crates/stela-drivers/src/probe.rs index 8ad733093d..fa1764ccc3 100644 --- a/crates/stela-drivers/src/probe.rs +++ b/crates/stela-drivers/src/probe.rs @@ -853,6 +853,16 @@ impl<'a> DeviceStart<'a> { } } + /// A fresh budget for a second start of the same function, as a + /// controller reset and attached again gets: the first start's budget, + /// which its failure may have used up, would end every wait of the + /// second at once. + fn renew(&mut self, clock: Clock) { + // Dropped first, so the new budget replaces what the old replaced. + self.budget = None; + self.budget = budget::Budget::start(clock, DEVICE_START_US).ok(); + } + /// Turn off a function whose budget passed while the probe still holds /// it, so a device left half started neither writes memory nor /// interrupts. @@ -1716,7 +1726,7 @@ pub unsafe fn probe_observed( devices.unsupported.push((address, function.class, why)); continue; } - let start = DeviceStart::begin(address, handoff.clock, &mut abandoned); + let mut start = DeviceStart::begin(address, handoff.clock, &mut abandoned); #[cfg(feature = "native-acpi")] if smbus::matches(&function) { smbus_functions.push(address); @@ -1853,7 +1863,7 @@ pub unsafe fn probe_observed( minimum: &minimum, }, dma, - &start, + &mut start, &mut devices, ); continue; @@ -2102,7 +2112,7 @@ pub unsafe fn probe_observed( core::mem::forget(started); continue; } - let start = DeviceStart::begin(address, handoff.clock, &mut abandoned); + let mut start = DeviceStart::begin(address, handoff.clock, &mut abandoned); absorb_xhci( Ok(started.finish(dma, firmware)), &mut function, @@ -2112,7 +2122,7 @@ pub unsafe fn probe_observed( minimum: &minimum, }, dma, - &start, + &mut start, &mut devices, ); } @@ -2161,7 +2171,7 @@ fn absorb_xhci( function: &mut Function, inputs: &XhciInputs<'_>, dma: &mut dyn DmaAllocator, - start: &DeviceStart<'_>, + start: &mut DeviceStart<'_>, devices: &mut Devices, ) { let address = function.address; @@ -2177,6 +2187,7 @@ fn absorb_xhci( ) { core::mem::forget(found); } + start.renew(inputs.handoff.clock); attached = xhci::attach_with_provider( function, inputs.handoff, diff --git a/crates/stela-drivers/src/usb/xhci/command.rs b/crates/stela-drivers/src/usb/xhci/command.rs index da0049d3a7..de0dde4778 100644 --- a/crates/stela-drivers/src/usb/xhci/command.rs +++ b/crates/stela-drivers/src/usb/xhci/command.rs @@ -194,7 +194,12 @@ impl Controller { } let current = self.outstanding.as_ref()?; let (address, control, phase) = (current.address, current.control, current.phase); - let expired = current.deadline.passed(); + // By the command's own time alone: a passed device start + // budget ends transfers' waits, and the commands that then cancel + // them and reset the device each keep their own bound. Counting + // the budget here declared a controller dead microseconds after + // a Stop Endpoint's doorbell. + let expired = current.deadline.passed_at(Clock::ticks()); // Quiet drains read no USBSTS: a controller that died without // an event is reported as such at a deadline. if expired && self.error.is_none() { diff --git a/crates/stela-drivers/src/usb/xhci/reset/tests.rs b/crates/stela-drivers/src/usb/xhci/reset/tests.rs index b2d717ef3d..ba2f942340 100644 --- a/crates/stela-drivers/src/usb/xhci/reset/tests.rs +++ b/crates/stela-drivers/src/usb/xhci/reset/tests.rs @@ -1003,8 +1003,10 @@ fn prepared( (shared, device, endpoints, hardware, dma) } /// A controller that stops running its command ring ends the command in two -/// command timeouts, one restart included, and is then dead; under a passed -/// device start budget the same command ends at once. +/// command timeouts, one restart included, and is then dead. A passed device +/// start budget changes neither: a command keeps its own time, so that the +/// commands that cancel a transfer the budget ended are answered and a +/// working controller is not declared dead at their doorbell. #[test] fn a_controller_that_never_runs_its_command_ring_times_out_and_dies() { let mut memory = vec![0u64; 262_144]; @@ -1031,8 +1033,23 @@ fn a_controller_that_never_runs_its_command_ring_times_out_and_dies() { let result = shared.borrow_mut().command(0, 0, ENABLE_SLOT << 10); let took = Clock::ticks() - started; assert!(matches!(result, Err(BlockError::Timeout)), "{result:?}"); - assert!(took < 100_000 + HALT_US + 10_000, "{took}"); - assert!(budget.expired().unwrap().at.is_some()); + assert!( + (2 * COMMAND_US..2 * COMMAND_US + HALT_US + 1_000_000).contains(&took), + "{took}" + ); + assert!(budget.expired().is_some()); + + // And one that answers, started after the budget passed, succeeds. + let mut memory = vec![0u64; 262_144]; + let (shared, _device, _hardware, _dma) = fixture(&mut memory, HIGH_SPEED, 32, 4); + let budget = stela_driver_base::budget::Budget::start(clock, 1).unwrap(); + assert!(!clock.wait(10, || false)); + assert!(budget.expired().is_some()); + shared + .borrow_mut() + .command(0, 0, ENABLE_SLOT << 10) + .unwrap(); + assert!(!matches!(shared.borrow().hardware, hardware::State::Died)); } #[test]