diff --git a/crates/stela-driver-base/src/bus/pci.rs b/crates/stela-driver-base/src/bus/pci.rs index 9f2dfef3a1..0df0868610 100644 --- a/crates/stela-driver-base/src/bus/pci.rs +++ b/crates/stela-driver-base/src/bus/pci.rs @@ -877,16 +877,45 @@ enum BusScan { Ari, } +/// A capability list's own malformation, as opposed to a configuration +/// read that failed: Linux's `__pci_find_next_cap_ttl` stops at such a link +/// and keeps what it found before it. +fn malformed(error: &CapabilityError) -> bool { + !matches!(error, CapabilityError::Config { .. }) +} + impl Function { + /// The extended capability `id` found before the list ends or turns out + /// malformed. Only a failed configuration read is an error. fn extended_capability(&self, id: u16) -> Result, CapabilityError> { for entry in capability::extended_capabilities(&self.config) { - let entry = entry?; - if entry.id == id { - return Ok(Some(entry.offset)); + match entry { + Ok(entry) if entry.id == id => return Ok(Some(entry.offset)), + Ok(_) => {} + Err(error) if malformed(&error) => return Ok(None), + Err(error) => return Err(error), } } Ok(None) } + /// The PCI Express capability found before the standard list ends or + /// turns out malformed. Only a failed configuration read is an error. + fn express_capability(&self) -> Result, CapabilityError> { + for entry in capability::capabilities(&self.config) { + match entry { + Ok(entry) if entry.id == CAPABILITY_EXPRESS => return Ok(Some(entry.offset)), + Ok(_) => {} + Err(error) if malformed(&error) => return Ok(None), + Err(error) => return Err(error), + } + } + Ok(None) + } + /// A bridge's secondary bus and how to scan it. A malformed capability + /// list hides the port type, so the bus is scanned as below a + /// conventional bridge: every device number, as Linux does without a + /// PCI Express capability. The function's driver meets the same list + /// again and reports it; the rest of the segment is still found. fn secondary(&self) -> Result, CapabilityError> { let read8 = |offset| { self.config @@ -902,11 +931,7 @@ impl Function { return Ok(None); } let bus = read8(SECONDARY_BUS)?; - let express = self - .capabilities()? - .into_iter() - .find(|&(_, id)| id == CAPABILITY_EXPRESS) - .map(|(offset, _)| u16::from(offset)); + let express = self.express_capability()?; let scan = match express { Some(express) => { let kind = (read16(express + EXPRESS_FLAGS)? >> EXPRESS_TYPE_SHIFT) & 0xf; diff --git a/crates/stela-driver-base/src/bus/pci/tests.rs b/crates/stela-driver-base/src/bus/pci/tests.rs index a78ba9ada8..3ba4a87d3b 100644 --- a/crates/stela-driver-base/src/bus/pci/tests.rs +++ b/crates/stela-driver-base/src/bus/pci/tests.rs @@ -764,6 +764,34 @@ fn a_bus_no_bridge_leads_to_is_not_scanned() { assert_eq!(ecam.found(), [(0, 3, 0)]); } +#[test] +fn a_bridge_whose_capabilities_cycle_hides_nothing_behind_or_after_it() { + let mut ecam = Ecam::new(); + ecam.port(EXPRESS_ROOT_PORT, 0); + // A vendor capability at 0x50 whose next pointer leads back to itself, + // ahead of the Express capability: the port type cannot be read. + *ecam.word(0, 1, 0, 0x34) = 0x50; + *ecam.word(0, 1, 0, 0x50) = 0x09 | 0x50 << 8 | 4 << 16; + // Without the port type, every device number below it is scanned. + ecam.present(1, 0, 0, 0); + ecam.present(1, 4, 0, 0); + // A function after the bridge in scan order. + ecam.present(0, 2, 0, 0); + assert_eq!(ecam.found(), [(0, 1, 0), (0, 2, 0), (1, 0, 0), (1, 4, 0)]); +} + +#[test] +fn a_bridge_with_its_express_capability_before_a_bad_link_keeps_its_port_type() { + let mut ecam = Ecam::new(); + ecam.port(EXPRESS_ROOT_PORT, 0); + // The Express capability's next pointer is misaligned. + *ecam.word(0, 1, 0, 0x40) |= 0x53 << 8; + for device in 0..32 { + ecam.present(1, device, 0, 0); + } + assert_eq!(ecam.found(), [(0, 1, 0), (1, 0, 0)]); +} + #[test] fn windows_narrow_to_each_root_bridge_from_its_root_bus() { let window = |segment, start_bus, end_bus| EcamWindow {