Skip to content

Commit

Permalink
Merge pull request #1403 from cagatay-y/cap-bar
Browse files Browse the repository at this point in the history
virtio-pci: map BAR with the information from capabilities
  • Loading branch information
mkroening authored Jan 30, 2025
2 parents 0a0148a + 8d53f5a commit 3595c0d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 74 deletions.
53 changes: 0 additions & 53 deletions src/drivers/virtio/env.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/drivers/virtio/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! A module containing virtios core infrastructure for hermit-rs.
//!
//! The module contains virtios transport mechanisms, virtqueues and virtio specific errors
pub mod env;
pub mod transport;
pub mod virtqueue;

Expand Down
31 changes: 11 additions & 20 deletions src/drivers/virtio/transport/pci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ use crate::drivers::net::virtio::VirtioNetDriver;
use crate::drivers::pci::PciDevice;
use crate::drivers::pci::error::PciError;
use crate::drivers::virtio::error::VirtioError;
use crate::drivers::virtio::transport::pci::PciBar as VirtioPciBar;
#[cfg(feature = "vsock")]
use crate::drivers::vsock::VirtioVsockDriver;

Expand Down Expand Up @@ -672,10 +673,7 @@ impl PciBar {
///
/// Returns ONLY Virtio specific capabilities, which allow to locate the actual capability
/// structures inside the memory areas, indicated by the BaseAddressRegisters (BAR's).
fn read_caps(
device: &PciDevice<PciConfigRegion>,
bars: &[PciBar],
) -> Result<Vec<PciCap>, PciError> {
fn read_caps(device: &PciDevice<PciConfigRegion>) -> Result<Vec<PciCap>, PciError> {
let device_id = device.device_id();

let capabilities = device
Expand All @@ -687,10 +685,14 @@ fn read_caps(
})
.map(|addr| CapData::read(addr, device.access()).unwrap())
.filter(|cap| cap.cfg_type != CapCfgType::Pci)
.map(|cap| PciCap {
bar: *bars.iter().find(|bar| bar.index == cap.bar).unwrap(),
dev_id: device_id,
cap,
.map(|cap| {
let slot = cap.bar;
let (addr, size) = device.memory_map_bar(slot, true).unwrap();
PciCap {
bar: VirtioPciBar::new(slot, addr.as_u64(), size.try_into().unwrap()),
dev_id: device_id,
cap,
}
})
.collect::<Vec<_>>();

Expand All @@ -702,11 +704,6 @@ fn read_caps(
}
}

/// Maps memory areas indicated by devices BAR's into virtual address space.
fn map_bars(device: &PciDevice<PciConfigRegion>) -> Result<Vec<PciBar>, PciError> {
crate::drivers::virtio::env::pci::map_bar_mem(device)
}

pub(crate) fn map_caps(device: &PciDevice<PciConfigRegion>) -> Result<UniCapsColl, VirtioError> {
let device_id = device.device_id();

Expand All @@ -716,14 +713,8 @@ pub(crate) fn map_caps(device: &PciDevice<PciConfigRegion>) -> Result<UniCapsCol
return Err(VirtioError::FromPci(PciError::NoCapPtr(device_id)));
}

// Mapped memory areas are reachable through PciBar structs.
let bar_list = match map_bars(device) {
Ok(list) => list,
Err(pci_error) => return Err(VirtioError::FromPci(pci_error)),
};

// Get list of PciCaps pointing to capabilities
let cap_list = match read_caps(device, &bar_list) {
let cap_list = match read_caps(device) {
Ok(list) => list,
Err(pci_error) => return Err(VirtioError::FromPci(pci_error)),
};
Expand Down

0 comments on commit 3595c0d

Please sign in to comment.