fix(drivers): scan a bridge whose capability list is malformed as conventional

This commit is contained in:
Tyler Smith committed 2026-11-10 09:51:59 +00:00
1 parent 1b9fa9fb4e
commit d0f2fd5e64
2 files changed
+61 -8

No files matched your search

+33 -8
View File
@@ -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<C: ConfigSpace> Function<C> {
/// 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<Option<u16>, 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<Option<u16>, 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<Option<(u8, BusScan)>, CapabilityError> {
let read8 = |offset| {
self.config
@@ -902,11 +931,7 @@ impl<C: ConfigSpace> Function<C> {
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;
@@ -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 {