From 2a76a589c31852c93df50fae6547d5bd0fa8e2b0 Mon Sep 17 00:00:00 2001 From: Rob Bradford Date: Tue, 6 Jul 2021 14:14:20 +0000 Subject: [PATCH] pci: vfio: Move parse_msi(x)_capabilities to VfioCommon This capability parsing logic will be useful in the vfio-user implementation. Signed-off-by: Rob Bradford --- pci/src/vfio.rs | 136 ++++++++++++++++++++++++------------------------ 1 file changed, 69 insertions(+), 67 deletions(-) diff --git a/pci/src/vfio.rs b/pci/src/vfio.rs index 83aa11702..16e4f2ceb 100644 --- a/pci/src/vfio.rs +++ b/pci/src/vfio.rs @@ -494,6 +494,65 @@ impl VfioCommon { } Ok(()) } + + fn parse_msix_capabilities( + &mut self, + cap: u8, + interrupt_manager: &Arc>, + vfio_pci_config: &dyn VfioPciConfig, + ) { + let msg_ctl = vfio_pci_config.read_config_word((cap + 2).into()); + + let table = vfio_pci_config.read_config_dword((cap + 4).into()); + + let pba = vfio_pci_config.read_config_dword((cap + 8).into()); + + let msix_cap = MsixCap { + msg_ctl, + table, + pba, + }; + + let interrupt_source_group = interrupt_manager + .create_group(MsiIrqGroupConfig { + base: 0, + count: msix_cap.table_size() as InterruptIndex, + }) + .unwrap(); + + let msix_config = MsixConfig::new(msix_cap.table_size(), interrupt_source_group.clone(), 0); + + self.interrupt.msix = Some(VfioMsix { + bar: msix_config, + cap: msix_cap, + cap_offset: cap.into(), + interrupt_source_group, + }); + } + + fn parse_msi_capabilities( + &mut self, + cap: u8, + interrupt_manager: &Arc>, + vfio_pci_config: &dyn VfioPciConfig, + ) { + let msg_ctl = vfio_pci_config.read_config_word((cap + 2).into()); + + let interrupt_source_group = interrupt_manager + .create_group(MsiIrqGroupConfig { + base: 0, + count: msi_num_enabled_vectors(msg_ctl) as InterruptIndex, + }) + .unwrap(); + + let msi_config = MsiConfig::new(msg_ctl, interrupt_source_group.clone()); + + self.interrupt.msi = Some(VfioMsi { + cfg: msi_config, + cap_offset: cap.into(), + interrupt_source_group, + }); + } } /// VfioPciDevice represents a VFIO PCI device. @@ -671,71 +730,6 @@ impl VfioPciDevice { Ok(()) } - fn parse_msix_capabilities( - &mut self, - cap: u8, - interrupt_manager: &Arc>, - ) { - let msg_ctl = self - .vfio_pci_configuration - .read_config_word((cap + 2).into()); - - let table = self - .vfio_pci_configuration - .read_config_dword((cap + 4).into()); - - let pba = self - .vfio_pci_configuration - .read_config_dword((cap + 8).into()); - - let msix_cap = MsixCap { - msg_ctl, - table, - pba, - }; - - let interrupt_source_group = interrupt_manager - .create_group(MsiIrqGroupConfig { - base: 0, - count: msix_cap.table_size() as InterruptIndex, - }) - .unwrap(); - - let msix_config = MsixConfig::new(msix_cap.table_size(), interrupt_source_group.clone(), 0); - - self.common.interrupt.msix = Some(VfioMsix { - bar: msix_config, - cap: msix_cap, - cap_offset: cap.into(), - interrupt_source_group, - }); - } - - fn parse_msi_capabilities( - &mut self, - cap: u8, - interrupt_manager: &Arc>, - ) { - let msg_ctl = self - .vfio_pci_configuration - .read_config_word((cap + 2).into()); - - let interrupt_source_group = interrupt_manager - .create_group(MsiIrqGroupConfig { - base: 0, - count: msi_num_enabled_vectors(msg_ctl) as InterruptIndex, - }) - .unwrap(); - - let msi_config = MsiConfig::new(msg_ctl, interrupt_source_group.clone()); - - self.common.interrupt.msi = Some(VfioMsi { - cfg: msi_config, - cap_offset: cap.into(), - interrupt_source_group, - }); - } - fn parse_capabilities( &mut self, interrupt_manager: &Arc>, @@ -755,7 +749,11 @@ impl VfioPciDevice { if irq_info.count > 0 { // Parse capability only if the VFIO device // supports MSI. - self.parse_msi_capabilities(cap_next, interrupt_manager); + self.common.parse_msi_capabilities( + cap_next, + interrupt_manager, + &self.vfio_pci_configuration, + ); } } } @@ -764,7 +762,11 @@ impl VfioPciDevice { if irq_info.count > 0 { // Parse capability only if the VFIO device // supports MSI-X. - self.parse_msix_capabilities(cap_next, interrupt_manager); + self.common.parse_msix_capabilities( + cap_next, + interrupt_manager, + &self.vfio_pci_configuration, + ); } } }