vfio: Remove PCI dependency

VFIO-PCI device has its own region naming but eventually it starts from 0
(as well as it suppose for others VFIO device types) so simply use 0
instead of VFIO_PCI_BAR0_REGION_INDEX.
At the same time move PCI-specific checks to helper function for clarity.

BUG=b:185504618
TEST=manatee PCI device passthrough boots/works

Change-Id: I404a9585016d230c48a6db7248172d2381a0a3e1
Signed-off-by: Tomasz Nowicki <tn@semihalf.com>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/2961213
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Tomasz Nowicki <tnowicki@google.com>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
This commit is contained in:
Tomasz Nowicki 2021-06-08 17:42:27 +00:00 committed by Commit Bot
parent 82f723fede
commit bea1935867

View file

@ -625,6 +625,19 @@ impl VfioDevice {
}
}
fn validate_dev_info(dev_info: &mut vfio_device_info) -> Result<(), VfioError> {
if (dev_info.flags & VFIO_DEVICE_FLAGS_PCI) != 0 {
if dev_info.num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1
|| dev_info.num_irqs < VFIO_PCI_MSIX_IRQ_INDEX + 1
{
return Err(VfioError::VfioDeviceGetInfo(get_error()));
}
return Ok(());
}
Err(VfioError::VfioDeviceGetInfo(get_error()))
}
#[allow(clippy::cast_ptr_alignment)]
fn get_regions(dev: &File) -> Result<Vec<VfioRegion>, VfioError> {
let mut regions: Vec<VfioRegion> = Vec::new();
@ -637,15 +650,12 @@ impl VfioDevice {
// Safe as we are the owner of dev and dev_info which are valid value,
// and we verify the return value.
let mut ret = unsafe { ioctl_with_mut_ref(dev, VFIO_DEVICE_GET_INFO(), &mut dev_info) };
if ret < 0
|| (dev_info.flags & VFIO_DEVICE_FLAGS_PCI) == 0
|| dev_info.num_regions < VFIO_PCI_CONFIG_REGION_INDEX + 1
|| dev_info.num_irqs < VFIO_PCI_MSIX_IRQ_INDEX + 1
{
if ret < 0 {
return Err(VfioError::VfioDeviceGetInfo(get_error()));
}
for i in VFIO_PCI_BAR0_REGION_INDEX..dev_info.num_regions {
Self::validate_dev_info(&mut dev_info)?;
for i in 0..dev_info.num_regions {
let argsz = mem::size_of::<vfio_region_info>() as u32;
let mut reg_info = vfio_region_info {
argsz,