@@ -2344,18 +2344,20 @@ static void vfio_unmap_bars(VFIOPCIDevice *vdev)
/*
* General setup
*/
-static uint8_t vfio_std_cap_max_size(PCIDevice *pdev, uint8_t pos)
+static uint8_t vfio_std_cap_max_size(VFIOPCIDevice *vdev, uint8_t cap)
{
- uint8_t tmp, next = 0xff;
+ struct vfio_pci_cap_info reg_info = {
+ .argsz = sizeof(reg_info),
+ .index = VFIO_PCI_CAP_GET_SIZE,
+ .cap = cap
+ };
+ int ret;
- for (tmp = pdev->config[PCI_CAPABILITY_LIST]; tmp;
- tmp = pdev->config[tmp + 1]) {
- if (tmp > pos && tmp < next) {
- next = tmp;
- }
- }
+ ret = ioctl(vdev->vbasedev.fd, VFIO_GET_PCI_CAP_INFO, ®_info);
+ if (ret || reg_info.size == 0)
+ error_report("vfio: Failed to find free PCI config reg: %m\n");
- return next - pos;
+ return reg_info.size;
}
static void vfio_set_word_bits(uint8_t *buf, uint16_t val, uint16_t mask)
@@ -2521,7 +2523,7 @@ static int vfio_add_std_cap(VFIOPCIDevice *vdev, uint8_t pos)
* Since QEMU doesn't actually handle many of the config accesses,
* exact size doesn't seem worthwhile.
*/
- size = vfio_std_cap_max_size(pdev, pos);
+ size = vfio_std_cap_max_size(vdev, cap_id);
/*
* pci_add_capability always inserts the new capability at the head
Use new ioctl cmd VFIO_GET_PCI_CAP_INFO to get PCI cap table size. This helps to get accurate table size and faciliate to find free PCI config space regs for faked PCI capability. Current code assigns PCI config space regs from the start of last PCI capability table to pos 0xff to the last capability and occupy some free PCI config space regs. Signed-off-by: Lan Tianyu <tianyu.lan@intel.com> --- hw/vfio/pci.c | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-)