Skip to content

Commit af82c61

Browse files
committed
pci: fixes in PCI crate
Define thiserror::Error and displaydoc::Display for various error types in the vended PCI crate. This way we can embed them in our error types downstream. Also export a few types and struct fields that were private and we will be needing them. Signed-off-by: Babis Chalios <[email protected]>
1 parent 59ec54e commit af82c61

File tree

7 files changed

+34
-36
lines changed

7 files changed

+34
-36
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/pci/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ default = []
1313

1414
[dependencies]
1515
byteorder = "1.5.0"
16+
displaydoc = "0.2.5"
1617
libc = "0.2.172"
1718
log = "0.4.27"
1819
serde = { version = "1.0.219", features = ["derive"] }

src/pci/src/bus.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const DEVICE_ID_INTEL_VIRT_PCIE_HOST: u16 = 0x0d57;
2424
const NUM_DEVICE_IDS: usize = 32;
2525

2626
/// Errors for device manager.
27-
#[derive(Debug)]
27+
#[derive(Debug, thiserror::Error, displaydoc::Display)]
2828
pub enum PciRootError {
2929
/// Could not allocate device address space for the device.
3030
AllocateDeviceAddrs(PciDeviceError),
@@ -103,7 +103,7 @@ impl PciDevice for PciRoot {
103103
pub struct PciBus {
104104
/// Devices attached to this bus.
105105
/// Device 0 is host bridge.
106-
devices: HashMap<u32, Arc<Mutex<dyn PciDevice>>>,
106+
pub devices: HashMap<u32, Arc<Mutex<dyn PciDevice>>>,
107107
device_reloc: Arc<dyn DeviceRelocation>,
108108
device_ids: Vec<bool>,
109109
}

src/pci/src/configuration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ struct PciBar {
409409
r#type: Option<PciBarRegionType>,
410410
}
411411

412-
#[derive(Serialize, Deserialize)]
412+
#[derive(Debug, Clone, Serialize, Deserialize)]
413413
pub struct PciConfigurationState {
414414
registers: Vec<u32>,
415415
writable_bits: Vec<u32>,
@@ -466,7 +466,7 @@ impl From<PciBarRegionType> for PciBarType {
466466
}
467467
}
468468

469-
#[derive(Copy, Clone)]
469+
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
470470
pub enum PciBarPrefetchable {
471471
NotPrefetchable = 0,
472472
Prefetchable = 0x08,
@@ -481,7 +481,7 @@ impl From<PciBarPrefetchable> for bool {
481481
}
482482
}
483483

484-
#[derive(Copy, Clone)]
484+
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
485485
pub struct PciBarConfiguration {
486486
addr: u64,
487487
size: u64,

src/pci/src/device.rs

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
77

88
use std::any::Any;
9-
use std::fmt::{self, Display};
109
use std::sync::{Arc, Barrier};
1110
use std::{io, result};
1211

@@ -16,39 +15,21 @@ use vm_device::Resource;
1615
use crate::configuration::{self, PciBarRegionType};
1716
use crate::PciBarConfiguration;
1817

19-
#[derive(Debug)]
18+
#[derive(Debug, thiserror::Error, displaydoc::Display)]
2019
pub enum Error {
21-
/// Setup of the device capabilities failed.
20+
/// Setup of the device capabilities failed: {0}.
2221
CapabilitiesSetup(configuration::Error),
23-
/// Allocating space for an IO BAR failed.
22+
/// Allocating space for an IO BAR failed, size={0}.
2423
IoAllocationFailed(u64),
25-
/// Registering an IO BAR failed.
24+
/// Registering an IO BAR at address {0} failed: {1}
2625
IoRegistrationFailed(u64, configuration::Error),
2726
/// Expected resource not found.
2827
MissingResource,
29-
/// Invalid resource.
28+
/// Invalid resource
3029
InvalidResource(Resource),
3130
}
3231
pub type Result<T> = std::result::Result<T, Error>;
3332

34-
impl Display for Error {
35-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36-
use self::Error::*;
37-
38-
match self {
39-
CapabilitiesSetup(e) => write!(f, "failed to add capability {e}"),
40-
IoAllocationFailed(size) => {
41-
write!(f, "failed to allocate space for an IO BAR, size={size}")
42-
}
43-
IoRegistrationFailed(addr, e) => {
44-
write!(f, "failed to register an IO BAR, addr={addr} err={e}")
45-
}
46-
MissingResource => write!(f, "failed to find expected resource"),
47-
InvalidResource(r) => write!(f, "invalid resource {r:?}"),
48-
}
49-
}
50-
}
51-
5233
#[derive(Clone, Copy)]
5334
pub struct BarReprogrammingParams {
5435
pub old_base: u64,

src/pci/src/lib.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,18 @@ use serde::de::Visitor;
2424
pub use self::bus::{PciBus, PciConfigIo, PciConfigMmio, PciRoot, PciRootError};
2525
pub use self::configuration::{
2626
PciBarConfiguration, PciBarPrefetchable, PciBarRegionType, PciCapability, PciCapabilityId,
27-
PciClassCode, PciConfiguration, PciExpressCapabilityId, PciHeaderType, PciMassStorageSubclass,
28-
PciNetworkControllerSubclass, PciProgrammingInterface, PciSerialBusSubClass, PciSubclass,
29-
PCI_CONFIGURATION_ID,
27+
PciClassCode, PciConfiguration, PciConfigurationState, PciExpressCapabilityId, PciHeaderType,
28+
PciMassStorageSubclass, PciNetworkControllerSubclass, PciProgrammingInterface,
29+
PciSerialBusSubClass, PciSubclass, PCI_CONFIGURATION_ID,
3030
};
3131
pub use self::device::{
3232
BarReprogrammingParams, DeviceRelocation, Error as PciDeviceError, PciDevice,
3333
};
3434
pub use self::msi::{msi_num_enabled_vectors, MsiCap, MsiConfig};
35-
pub use self::msix::{MsixCap, MsixConfig, MsixTableEntry, MSIX_CONFIG_ID, MSIX_TABLE_ENTRY_SIZE};
35+
pub use self::msix::{
36+
Error as MsixError, MsixCap, MsixConfig, MsixConfigState, MsixTableEntry, MSIX_CONFIG_ID,
37+
MSIX_TABLE_ENTRY_SIZE,
38+
};
3639

3740
/// PCI has four interrupt pins A->D.
3841
#[derive(Copy, Clone)]

src/pci/src/msix.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const MSIX_ENABLE_MASK: u16 = (1 << MSIX_ENABLE_BIT) as u16;
2626
pub const MSIX_TABLE_ENTRY_SIZE: usize = 16;
2727
pub const MSIX_CONFIG_ID: &str = "msix_config";
2828

29-
#[derive(Debug)]
29+
#[derive(Debug, thiserror::Error, displaydoc::Display)]
3030
pub enum Error {
3131
/// Failed enabling the interrupt route.
3232
EnableInterruptRoute(io::Error),
@@ -59,7 +59,7 @@ impl Default for MsixTableEntry {
5959
}
6060
}
6161

62-
#[derive(Serialize, Deserialize)]
62+
#[derive(Debug, Clone, Serialize, Deserialize)]
6363
pub struct MsixConfigState {
6464
table_entries: Vec<MsixTableEntry>,
6565
pba_entries: Vec<u64>,
@@ -71,11 +71,23 @@ pub struct MsixConfig {
7171
pub table_entries: Vec<MsixTableEntry>,
7272
pub pba_entries: Vec<u64>,
7373
pub devid: u32,
74-
interrupt_source_group: Arc<dyn InterruptSourceGroup>,
74+
pub interrupt_source_group: Arc<dyn InterruptSourceGroup>,
7575
masked: bool,
7676
enabled: bool,
7777
}
7878

79+
impl std::fmt::Debug for MsixConfig {
80+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81+
f.debug_struct("MsixConfig")
82+
.field("table_entries", &self.table_entries)
83+
.field("pba_entries", &self.pba_entries)
84+
.field("devid", &self.devid)
85+
.field("masked", &self.masked)
86+
.field("enabled", &self.enabled)
87+
.finish()
88+
}
89+
}
90+
7991
impl MsixConfig {
8092
pub fn new(
8193
msix_vectors: u16,

0 commit comments

Comments
 (0)