From 020a3ef63101fc85b01e6f35e2c4cb8045412e31 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 9 Jun 2024 13:38:34 +0200 Subject: [PATCH 1/6] pcid: Update to pci_types 0.9.0 --- Cargo.lock | 4 ++-- pcid/Cargo.toml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 56c5b23ca3..d84e864362 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -907,9 +907,9 @@ checksum = "7f0b59668fe80c5afe998f0c0bf93322bf2cd66cafeeb80581f291716f3467f2" [[package]] name = "pci_types" -version = "0.6.2" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ebac2b2ee11791f721a51184b632a916b3044f2ee7b2374e7fdcfdf3eaf29c79" +checksum = "6f966dfb3dde50a726cc62ae44dc48efd10a211db15296c00cd88550ad8ef24c" dependencies = [ "bit_field", "bitflags 2.5.0", diff --git a/pcid/Cargo.toml b/pcid/Cargo.toml index 17d8061c36..705daf00fa 100644 --- a/pcid/Cargo.toml +++ b/pcid/Cargo.toml @@ -20,7 +20,7 @@ fdt = { git = "https://gitlab.redox-os.org/rosehuds/fdt.git" } libc = "0.2" log = "0.4" paw = "1.0" -pci_types = "0.6.1" +pci_types = "0.9.0" plain = "0.2" redox-log = "0.1" redox_syscall = "0.5" From 38a729bc76e2e7613ccd2b0d765218eeef83535f Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 9 Jun 2024 13:39:39 +0200 Subject: [PATCH 2/6] Handle some stabilized rustc features --- pcid/src/main.rs | 3 --- storage/virtio-blkd/src/main.rs | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/pcid/src/main.rs b/pcid/src/main.rs index e2915272cb..56f78191bf 100644 --- a/pcid/src/main.rs +++ b/pcid/src/main.rs @@ -1,6 +1,3 @@ -// Already stabilized, TODO: remove when Redox's rustc is updated -#![feature(result_option_inspect)] - use std::fs::{File, metadata, read_dir}; use std::io::prelude::*; use std::os::unix::io::{FromRawFd, RawFd}; diff --git a/storage/virtio-blkd/src/main.rs b/storage/virtio-blkd/src/main.rs index 8c5313c8b6..1c13166341 100644 --- a/storage/virtio-blkd/src/main.rs +++ b/storage/virtio-blkd/src/main.rs @@ -1,5 +1,5 @@ #![deny(trivial_numeric_casts, unused_allocation)] -#![feature(int_roundings, async_fn_in_trait)] +#![feature(int_roundings)] // TODO(andypython): driver panic with an empty disk. 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 3/6] 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() } From 6825d9b2ba2bb7820edbe8db07091b21842edbc0 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 9 Jun 2024 13:57:35 +0200 Subject: [PATCH 4/6] pcid: Remove support for connecting to non-default server --- pcid/src/driver_interface/mod.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pcid/src/driver_interface/mod.rs b/pcid/src/driver_interface/mod.rs index 0595fee914..4949a748c2 100644 --- a/pcid/src/driver_interface/mod.rs +++ b/pcid/src/driver_interface/mod.rs @@ -255,17 +255,14 @@ pub(crate) fn recv(r: &mut R) -> Result { } impl PcidServerHandle { - pub fn connect(pcid_to_client: RawFd, pcid_from_client: RawFd) -> Result { - Ok(Self { - pcid_to_client: unsafe { File::from_raw_fd(pcid_to_client) }, - pcid_from_client: unsafe { File::from_raw_fd(pcid_from_client) }, - }) - } pub fn connect_default() -> Result { let pcid_to_client_fd = env::var("PCID_TO_CLIENT_FD")?.parse::().map_err(PcidClientHandleError::EnvValidityError)?; let pcid_from_client_fd = env::var("PCID_FROM_CLIENT_FD")?.parse::().map_err(PcidClientHandleError::EnvValidityError)?; - Self::connect(pcid_to_client_fd, pcid_from_client_fd) + Ok(Self { + pcid_to_client: unsafe { File::from_raw_fd(pcid_to_client_fd) }, + pcid_from_client: unsafe { File::from_raw_fd(pcid_from_client_fd) }, + }) } pub(crate) fn send(&mut self, req: &PcidClientRequest) -> Result<()> { send(&mut self.pcid_from_client, req) From 3737a70cc15fc4af700605206da80362fddfaf55 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 9 Jun 2024 14:00:30 +0200 Subject: [PATCH 5/6] pcid: Reduce visibility of server handle send/recv --- pcid/src/driver_interface/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pcid/src/driver_interface/mod.rs b/pcid/src/driver_interface/mod.rs index 4949a748c2..aa03a9d587 100644 --- a/pcid/src/driver_interface/mod.rs +++ b/pcid/src/driver_interface/mod.rs @@ -264,10 +264,10 @@ impl PcidServerHandle { pcid_from_client: unsafe { File::from_raw_fd(pcid_from_client_fd) }, }) } - pub(crate) fn send(&mut self, req: &PcidClientRequest) -> Result<()> { + fn send(&mut self, req: &PcidClientRequest) -> Result<()> { send(&mut self.pcid_from_client, req) } - pub(crate) fn recv(&mut self) -> Result { + fn recv(&mut self) -> Result { recv(&mut self.pcid_to_client) } pub fn fetch_config(&mut self) -> Result { From 7ddae5fa03f7babf8aa301007390f90804660525 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 9 Jun 2024 14:03:32 +0200 Subject: [PATCH 6/6] pcid: Add fixme --- pcid/src/main.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/pcid/src/main.rs b/pcid/src/main.rs index 984c108ad1..4d0cf1e94f 100644 --- a/pcid/src/main.rs +++ b/pcid/src/main.rs @@ -269,6 +269,7 @@ fn handle_parsed_header(state: Arc, config: &Config, header: PciEndpointH let mut irq; let interrupt_pin; + // FIXME use pci_types' update_interrupt once it is released to crates.io unsafe { let mut data = state.pcie.read(header.address(), 0x3C); irq = (data & 0xFF) as u8;