Skip to content

Commit

Permalink
ioctls: improve error codes for IRQ operations
Browse files Browse the repository at this point in the history
Currently all IRQ operations share a single error code, it's inconvient
for debugging. So introduce dedicated error codes for each IRQ
operation.

Signed-off-by: Liu Jiang <[email protected]>
  • Loading branch information
jiangliu committed Nov 30, 2021
1 parent 7cebfc8 commit da94b5a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 8 additions & 0 deletions crates/vfio-ioctls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ pub enum VfioError {
VfioDeviceGetIrqInfo,
#[error("failed to set vfio device irq")]
VfioDeviceSetIrq,
#[error("failed to enable vfio device irq")]
VfioDeviceEnableIrq,
#[error("failed to disable vfio device irq")]
VfioDeviceDisableIrq,
#[error("failed to unmask vfio device irq")]
VfioDeviceUnmaskIrq,
#[error("failed to trigger vfio device irq")]
VfioDeviceTriggerIrq,
}

/// Specialized version of `Result` for VFIO subsystem.
Expand Down
20 changes: 12 additions & 8 deletions crates/vfio-ioctls/src/vfio_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,9 @@ impl VfioDevice {
let irq = self
.irqs
.get(&irq_index)
.ok_or(VfioError::VfioDeviceSetIrq)?;
.ok_or(VfioError::VfioDeviceTriggerIrq)?;
if irq.count <= vector {
return Err(VfioError::VfioDeviceSetIrq);
return Err(VfioError::VfioDeviceTriggerIrq);
}

let mut irq_set = vec_with_array_field::<vfio_irq_set, u32>(0);
Expand All @@ -804,6 +804,7 @@ impl VfioDevice {
irq_set[0].count = 1;

vfio_syscall::set_device_irqs(self, irq_set.as_slice())
.map_err(|_| VfioError::VfioDeviceTriggerIrq)
}

/// Enables a VFIO device IRQs.
Expand All @@ -818,9 +819,9 @@ impl VfioDevice {
let irq = self
.irqs
.get(&irq_index)
.ok_or(VfioError::VfioDeviceSetIrq)?;
.ok_or(VfioError::VfioDeviceEnableIrq)?;
if irq.count == 0 || (irq.count as usize) < event_fds.len() {
return Err(VfioError::VfioDeviceSetIrq);
return Err(VfioError::VfioDeviceEnableIrq);
}

let mut irq_set = vec_with_array_field::<vfio_irq_set, u32>(event_fds.len());
Expand Down Expand Up @@ -850,6 +851,7 @@ impl VfioDevice {
}

vfio_syscall::set_device_irqs(self, irq_set.as_slice())
.map_err(|_| VfioError::VfioDeviceEnableIrq)
}

/// Disables a VFIO device IRQs
Expand All @@ -860,10 +862,10 @@ impl VfioDevice {
let irq = self
.irqs
.get(&irq_index)
.ok_or(VfioError::VfioDeviceSetIrq)?;
.ok_or(VfioError::VfioDeviceDisableIrq)?;
// Currently the VFIO driver only support MASK/UNMASK INTX, so count is hard-coded to 1.
if irq.count == 0 {
return Err(VfioError::VfioDeviceSetIrq);
return Err(VfioError::VfioDeviceDisableIrq);
}

// Individual subindex interrupts can be disabled using the -1 value for DATA_EVENTFD or
Expand All @@ -876,6 +878,7 @@ impl VfioDevice {
irq_set[0].count = 0;

vfio_syscall::set_device_irqs(self, irq_set.as_slice())
.map_err(|_| VfioError::VfioDeviceDisableIrq)
}

/// Unmask IRQ
Expand All @@ -886,10 +889,10 @@ impl VfioDevice {
let irq = self
.irqs
.get(&irq_index)
.ok_or(VfioError::VfioDeviceSetIrq)?;
.ok_or(VfioError::VfioDeviceUnmaskIrq)?;
// Currently the VFIO driver only support MASK/UNMASK INTX, so count is hard-coded to 1.
if irq.count == 0 || irq.count != 1 || irq.index != VFIO_PCI_INTX_IRQ_INDEX {
return Err(VfioError::VfioDeviceSetIrq);
return Err(VfioError::VfioDeviceUnmaskIrq);
}

let mut irq_set = vec_with_array_field::<vfio_irq_set, u32>(0);
Expand All @@ -900,6 +903,7 @@ impl VfioDevice {
irq_set[0].count = 1;

vfio_syscall::set_device_irqs(self, irq_set.as_slice())
.map_err(|_| VfioError::VfioDeviceUnmaskIrq)
}

/// Wrapper to enable MSI IRQs.
Expand Down

0 comments on commit da94b5a

Please sign in to comment.