Skip to content

Commit 4ee8292

Browse files
gurasinghMSCopilotmattkur
committed
pci_core: add infrastructure for devices to advertise pcie support with some basic functionality (microsoft#1930)
Adds the PCI_Express capability option for devices to advertise PCIe support. This change will also us to add FLR support to the nvme_test crate for further perf testing. PciExpressCapability: New capability struct implementing PCI Express Device Capabilities, Device Control, and Device Status registers Spec digarams (From PCIe Base Spec 6.4) for reference: <img width="1481" height="991" alt="image" src="https://github.com/user-attachments/assets/a6f49e62-af03-4573-ab60-17637154aadc" /> <img width="1489" height="919" alt="image" src="https://github.com/user-attachments/assets/21f6996c-eb8b-4fb6-b505-a113c37d9257" /> Being made as a helper PR to microsoft#1858 since that was getting a little too large/complicated --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Matt LaFayette (Kurjanowicz) <[email protected]>
1 parent 87cfe46 commit 4ee8292

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

vm/devices/pci/pci_core/src/capabilities/pci_express.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl PciExpressCapability {
4848
/// Creates a new PCI Express capability with FLR support.
4949
///
5050
/// # Arguments
51-
/// * `flr_handler` - Optional handler to be called when FLR is initiated. FLR support will be inferred if flr_handler = Some(_)
51+
/// * `flr_handler` - Optional handler to be called when FLR is initiated. This emulator will report that FLR is supported if flr_handler = Some(_)
5252
pub fn new(flr_handler: Option<Arc<dyn FlrHandler>>) -> Self {
5353
Self {
5454
device_capabilities: pci_express::DeviceCapabilities::new()
@@ -65,8 +65,9 @@ impl PciExpressCapability {
6565
let old_flr = state.device_control.initiate_function_level_reset();
6666
let new_flr = new_control.initiate_function_level_reset();
6767

68-
69-
// DEVNOTE: It is "safe" to drop a new FLR request if there is still a previous FLR request in progress. The PCIe spec indicates that such behavior is undefined, so we choose to ignore the new FLR request.
68+
// DEVNOTE: It is "safe" to drop a new FLR request if there is still a previous
69+
// FLR request in progress. The PCIe spec indicates that such behavior is undefined,
70+
// so we choose to ignore the new FLR request.
7071
if new_flr && !old_flr {
7172
if let Some(handler) = &self.flr_handler {
7273
handler.initiate_flr();
@@ -201,26 +202,20 @@ mod save_restore {
201202
pub device_control: u16,
202203
#[mesh(2)]
203204
pub device_status: u16,
205+
#[mesh(3)]
206+
pub flr_handler: u16,
204207
}
205208
}
206209

207210
impl SaveRestore for PciExpressCapability {
208211
type SavedState = state::SavedState;
209212

210213
fn save(&mut self) -> Result<Self::SavedState, SaveError> {
211-
let state = self.state.lock();
212-
Ok(state::SavedState {
213-
device_control: state.device_control.into_bits(),
214-
device_status: state.device_status.into_bits(),
215-
})
214+
Err(SaveError::NotSupported)
216215
}
217216

218-
fn restore(&mut self, saved_state: Self::SavedState) -> Result<(), RestoreError> {
219-
let mut state = self.state.lock();
220-
state.device_control =
221-
pci_express::DeviceControl::from_bits(saved_state.device_control);
222-
state.device_status = pci_express::DeviceStatus::from_bits(saved_state.device_status);
223-
Ok(())
217+
fn restore(&mut self, _: Self::SavedState) -> Result<(), RestoreError> {
218+
Err(RestoreError::SavedStateNotSupported)
224219
}
225220
}
226221
}
@@ -297,7 +292,7 @@ mod tests {
297292

298293
// Test Device Capabilities Register (offset 0x04) - FLR should not be set
299294
let device_caps_val = cap.read_u32(0x04);
300-
assert_eq!(device_caps_val & PCI_EXPRESS_DEVICE_CAPS_FLR_BIT_MASK, 0); // FLR bit should not be set
295+
assert_eq!(device_caps_val & PCI_EXPRESS_DEVICE_CAPS_FLR_BIT_MASK, 0);
301296
}
302297

303298
#[test]

vm/devices/pci/pci_core/src/spec.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -391,11 +391,11 @@ pub mod caps {
391391
///
392392
/// Table pulled from PCI Express Base Specification Rev. 3.0
393393
///
394-
/// | Offset | Bits 31-24 | Bits 23-16 | Bits 15-8 | Bits 7-0 |
395-
/// |-----------|------------------|------------------|------------------|------------------|
396-
/// | Cap + 0x0 | PCI Express Capabilities Register | Next Pointer | Capability ID (0x10) |
394+
/// | Offset | Bits 31-24 | Bits 23-16 | Bits 15-8 | Bits 7-0 |
395+
/// |-----------|------------------|----------------- |------------------|----------------------|
396+
/// | Cap + 0x0 | PCI Express Capabilities Register | Next Pointer | Capability ID (0x10) |
397397
/// | Cap + 0x4 | Device Capabilities Register |
398-
/// | Cap + 0x8 | Device Status | Device Control |
398+
/// | Cap + 0x8 | Device Status | Device Control |
399399
pub enum PciExpressCapabilityHeader: u16 {
400400
PCIE_CAPS = 0x00,
401401
DEVICE_CAPS = 0x04,
@@ -419,7 +419,7 @@ pub mod caps {
419419
#[bits(3)]
420420
_reserved1: u32,
421421
pub role_based_error: bool,
422-
pub err_cor_subclass_capabable: bool,
422+
pub err_cor_subclass_capable: bool,
423423
pub rx_mps_fixed: bool,
424424
#[bits(8)]
425425
pub captured_slot_power_limit: u32,

0 commit comments

Comments
 (0)