From 785f53c70c418eb9f11ca1e9bcd655b17dcd3d63 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 9 Jun 2024 13:41:59 +0200 Subject: [PATCH] pcid: Stop explicitly passing PciAddress to several functions The PciHeader/PciEndpointHeader argument already contains the PciAddress. --- pcid/src/main.rs | 25 ++++++++++--------------- pcid/src/pci_header.rs | 12 ++++++++++++ 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/pcid/src/main.rs b/pcid/src/main.rs index 56f78191bf..984c108ad1 100644 --- a/pcid/src/main.rs +++ b/pcid/src/main.rs @@ -209,9 +209,9 @@ pub struct State { pcie: Pcie, } -fn print_pci_function(addr: PciAddress, header: &PciHeader) { +fn print_pci_function(header: &PciHeader) { let mut string = format!("PCI {} {:>04X}:{:>04X} {:>02X}.{:>02X}.{:>02X}.{:>02X} {:?}", - addr, header.vendor_id(), header.device_id(), header.class(), + header.address(), header.vendor_id(), header.device_id(), header.class(), header.subclass(), header.interface(), header.revision(), header.class()); let device_type = DeviceType::from((header.class(), header.subclass())); match device_type { @@ -234,7 +234,7 @@ fn print_pci_function(addr: PciAddress, header: &PciHeader) { info!("{}", string); } -fn handle_parsed_header(state: Arc, config: &Config, addr: PciAddress, header: PciEndpointHeader) { +fn handle_parsed_header(state: Arc, config: &Config, header: PciEndpointHeader) { for driver in config.drivers.iter() { if !driver.match_function(header.full_device_id()) { continue; @@ -270,14 +270,14 @@ fn handle_parsed_header(state: Arc, config: &Config, addr: PciAddress, he let interrupt_pin; unsafe { - let mut data = state.pcie.read(addr, 0x3C); + let mut data = state.pcie.read(header.address(), 0x3C); irq = (data & 0xFF) as u8; interrupt_pin = ((data & 0x0000_FF00) >> 8) as u8; if irq == 0xFF { irq = 9; } data = (data & 0xFFFFFF00) | irq as u32; - state.pcie.write(addr, 0x3C, data); + state.pcie.write(header.address(), 0x3C, data); }; let legacy_interrupt_enabled = match interrupt_pin { @@ -293,7 +293,7 @@ fn handle_parsed_header(state: Arc, config: &Config, addr: PciAddress, he let capabilities = if endpoint_header.status(&state.pcie).has_capability_list() { let func = PciFunc { pci: &state.pcie, - addr + addr: header.address(), }; crate::pci::cap::CapabilitiesIter { inner: crate::pci::cap::CapabilityOffsetsIter::new(header.cap_pointer(), &func) }.collect::>() } else { @@ -303,7 +303,7 @@ fn handle_parsed_header(state: Arc, config: &Config, addr: PciAddress, he let func = driver_interface::PciFunction { bars, - addr, + addr: header.address(), legacy_interrupt_line: if legacy_interrupt_enabled { Some(LegacyInterruptLine(irq)) } else { @@ -353,7 +353,7 @@ fn handle_parsed_header(state: Arc, config: &Config, addr: PciAddress, he match command.envs(envs).spawn() { Ok(mut child) => { let driver_handler = DriverHandler { - addr, + addr: header.address(), state: Arc::clone(&state), capabilities, }; @@ -472,15 +472,10 @@ fn main(args: Args) { let func_addr = PciAddress::new(0, bus_num, dev_num, func_num); match PciHeader::from_reader(&state.pcie, func_addr) { Ok(header) => { - print_pci_function(func_addr, &header); + print_pci_function(&header); match header { PciHeader::General(endpoint_header) => { - handle_parsed_header( - Arc::clone(&state), - &config, - func_addr, - endpoint_header, - ); + handle_parsed_header(Arc::clone(&state), &config, endpoint_header); } PciHeader::PciToPci { secondary_bus_num, .. diff --git a/pcid/src/pci_header.rs b/pcid/src/pci_header.rs index 2b3a64e4c5..1b884b7849 100644 --- a/pcid/src/pci_header.rs +++ b/pcid/src/pci_header.rs @@ -112,6 +112,14 @@ impl PciHeader { } } + /// Return the PCI address. + pub fn address(&self) -> PciAddress { + match self { + PciHeader::General(header) => header.address(), + PciHeader::PciToPci { shared, .. } => shared.addr, + } + } + /// Return the Vendor ID field. pub fn vendor_id(&self) -> u16 { self.full_device_id().vendor_id @@ -144,6 +152,10 @@ impl PciHeader { } impl PciEndpointHeader { + pub fn address(&self) -> PciAddress { + self.shared.addr + } + pub fn endpoint_header(&self, access: &impl ConfigRegionAccess) -> EndpointHeader { EndpointHeader::from_header(TyPciHeader::new(self.shared.addr), access).unwrap() }