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

This commit is contained in:
Tyler Smith committed 2026-11-14 01:34:00 -05:00
1 parent 814c5f6ab6
commit f6c74a6f16
3 files changed
+43 -10

No files matched your search

+16 -5
View File
@@ -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,
+6 -1
View File
@@ -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() {
@@ -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]