fix(net): stop an AQtion owner's queues, link, bus master and posted writes within a bounded wait when it is dropped unretired, as aq_nic_stop does, keeping its pages and binding

This commit is contained in:
Tyler Smith committed 2026-11-14 01:33:58 -05:00
1 parent 4371287681
commit d3ea7c7d16
8 files changed
+98 -1

No files matched your search

+26
View File
@@ -181,10 +181,15 @@ unsafe impl Registers for MappedRegisters {
}
}
pub use crate::bus::pci::power::Timer;
/// The register polls of a stop at drop share this budget: the drop can
/// run inside a processor's exit, which must end well inside half a second.
pub(crate) const STOP_BUDGET_US: u64 = 200_000;
struct Hardware<R, T> {
registers: R,
timer: T,
reset_ownership: Cell<firmware::ResetOwnership>,
/// What polls may still wait in all, when bounded ([`STOP_BUDGET_US`]).
budget: Cell<Option<u64>>,
}
impl<R: Registers, T: Timer> Hardware<R, T> {
fn check_timing(&self) -> Result<()> {
@@ -234,6 +239,12 @@ impl<R: Registers, T: Timer> Hardware<R, T> {
if ready(word) {
return Ok(word);
}
if let Some(left) = self.budget.get() {
let left = left
.checked_sub(interval)
.ok_or(Error::Timeout { register })?;
self.budget.set(Some(left));
}
self.delay(interval)?;
}
Err(Error::Timeout { register })
@@ -448,6 +459,21 @@ impl<R: Registers, T: Timer, C: ConfigSpace> Aqtion<R, T, C> {
self.state = State::Stopped;
Ok(())
}
/// [`Self::stop`] with every register poll drawn from one budget of
/// `microseconds`, for a stop inside a processor's exit. A function that
/// left the bus reads all ones and gets no write.
pub(crate) fn stop_within(&mut self, microseconds: u64) -> Result<()> {
let Some(function) = self.function.as_mut() else {
return Err(Error::Stopped);
};
if function.config().read16(4)? == u16::MAX {
return Err(Error::Removed);
}
self.hw().budget.set(Some(microseconds));
let result = self.stop();
self.hw().budget.set(None);
result
}
/// Stop/drain the real queue epoch before requesting PCI D3hot. The
/// configuration owner and DMA domain remain held for ordinary resume.
pub fn suspend(&mut self) -> Result<()> {
@@ -54,6 +54,7 @@ pub(super) unsafe fn prepare<R: RetirableRegisters, T: Timer + 'static>(
registers,
timer,
reset_ownership: Cell::new(firmware::ResetOwnership::default()),
budget: Cell::new(None),
},
})));
let result = (|| {
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-only
//! One AQtion owner keeps its stopped-root receipt and native retirement graph.
use super::{Error, Failure, RetirableRegisters, cold::Bound};
use crate::aqtion::{Aqtion, Mac, PciPower, State, Timer, memory::Memory};
use crate::aqtion::{Aqtion, Mac, PciPower, STOP_BUDGET_US, State, Timer, memory::Memory};
use crate::{
Link, LinkError,
bus::{
@@ -28,8 +28,20 @@ pub struct Attached<
failure: Option<Error>,
}
impl<R: RetirableRegisters, T: Timer + 'static> Drop for Attached<R, T> {
/// An owner that was not retired is stopped first, as Linux's close
/// (`aq_nic_stop`) stops it: interrupts masked, every queue off, the
/// link dropped through firmware, bus mastering cleared and posted
/// writes drained, its register polls within [`STOP_BUDGET_US`]. It is
/// then kept whole: only [`Self::retire`] releases its pages, binding
/// and registers. A function that left the bus gets no write.
///
/// This drives the controller's registers, so the owner drops it where
/// its MMIO window is mapped; anywhere else the first read faults.
fn drop(&mut self) {
if !self.completed {
if self.live().is_ok() {
let _ = self.core_mut().stop_within(STOP_BUDGET_US);
}
core::mem::forget(self.resources.take());
}
}
@@ -696,6 +696,7 @@ fn hardware(peer: &Rc<Peer>) -> Hardware<Rc<Peer>, Rc<Peer>> {
registers: peer.clone(),
timer: peer.clone(),
reset_ownership: Cell::new(firmware::ResetOwnership::default()),
budget: Cell::new(None),
}
}
type Fixture = (
@@ -2616,6 +2617,7 @@ mod registers_oracle {
},
timer: NoDelay,
reset_ownership: Cell::new(firmware::ResetOwnership::default()),
budget: Cell::new(None),
};
match spec.access {
Access::Get(_) => assert_eq!(
@@ -140,6 +140,7 @@ fn unchanged_linux_live_read_data_flags_and_exact_independent_alive_access_match
},
timer: NoWait,
reset_ownership: Cell::new(firmware::ResetOwnership::default()),
budget: Cell::new(None),
};
let actual = hw.read(32);
// Native error is the explicit analogue of the original unplug flag;
@@ -129,7 +129,20 @@ fn abandoning_an_active_native_owner_retains_pages_binding_and_registers() {
for device in [0x07b1, 0x04c0] {
let fixture = inputs(device);
let owner = attach(fixture.inputs, &fixture.pages).unwrap();
fixture.state.events.lock().unwrap().clear();
drop(owner);
// The drop stopped it: every queue off, bus mastering cleared and
// posted writes drained.
fixture.state.stopped();
for queue in 0..32 {
for field in [registers::TDM_TX_DESC_EN_SET, registers::RDM_RX_DESC_EN_SET] {
assert_eq!(
fixture.peer.get(field.address + queue * field.stride) & field.mask,
0
);
}
}
assert_eq!(*fixture.state.events.lock().unwrap(), ["bound drain"]);
assert!(fixture.state.binding.lock().unwrap().is_some());
assert_eq!(fixture.state.base.maps.lock().unwrap().len(), 4);
assert_eq!(fixture.dropped.load(Ordering::Relaxed), 0);
@@ -137,6 +150,46 @@ fn abandoning_an_active_native_owner_retains_pages_binding_and_registers() {
assert_eq!(fixture.state.pm.unmaps.load(Ordering::Relaxed), 0);
}
}
/// A queue that never stops ends the drop's stop within its budget, before
/// bus mastering is cleared or any drain, and the owner stays whole; a
/// function that left the bus gets no register write.
#[test]
fn a_dropped_owner_that_cannot_stop_stays_whole_within_its_budget() {
if crate::test_process::isolate_model() {
return;
}
for device in [0x07b1, 0x04c0] {
let fixture = inputs(device);
let owner = attach(fixture.inputs, &fixture.pages).unwrap();
fixture.peer.fault.set(Fault::QueueStop);
fixture.state.events.lock().unwrap().clear();
let before = fixture.peer.elapsed.get();
drop(owner);
let waited = fixture.peer.elapsed.get() - before;
assert!(waited <= STOP_BUDGET_US, "{waited}");
assert_ne!(fixture.state.pm.command() & 4, 0);
assert!(fixture.state.events.lock().unwrap().is_empty());
assert_eq!(fixture.state.base.maps.lock().unwrap().len(), 4);
assert_eq!(fixture.state.registers_dropped.load(Ordering::Relaxed), 0);
let fixture = inputs(device);
let owner = attach(fixture.inputs, &fixture.pages).unwrap();
// SAFETY: the model's own configuration RAM; nothing else accesses
// it while the owner is dropped.
unsafe {
fixture
.state
.pm
.pointer()
.as_ptr()
.add(4)
.cast::<u16>()
.write_volatile(u16::MAX)
};
let writes = fixture.peer.writes.borrow().len();
drop(owner);
assert_eq!(fixture.peer.writes.borrow().len(), writes);
}
}
#[test]
fn each_native_retirement_failure_is_sticky_and_keeps_remaining_resources() {
if crate::test_process::isolate_model() {
@@ -50,6 +50,7 @@ fn reset(flb: bool, gsr: u32, spi: bool, seed: bool) -> Vec<(u32, u32, u32)> {
registers: boundary.clone(),
timer: boundary.clone(),
reset_ownership: Cell::new(firmware::ResetOwnership::default()),
budget: Cell::new(None),
};
hw.reset(peer.profile).unwrap();
boundary.effects.take()
@@ -15,6 +15,7 @@ fn refused_minimum(
registers: hw.registers,
timer: crate::timing::refused_model(clock),
reset_ownership: hw.reset_ownership,
budget: hw.budget,
}),
domain: old.domain.take(),
memory: old.memory.take(),