static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev)

{

    struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };

    struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };

    int ret, i;



    ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);

    if (ret < 0) {

        error_report("vfio: error getting device %s from group %d: %m\n",

                     name, group->groupid);

        error_report("Verify all devices in group %d are bound to vfio-pci "

                     "or pci-stub and not already in use\n", group->groupid);

        return ret;

    }



    vdev->fd = ret;

    vdev->group = group;

    QLIST_INSERT_HEAD(&group->device_list, vdev, next);



    /* Sanity check device */

    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &dev_info);

    if (ret) {

        error_report("vfio: error getting device info: %m\n");

        goto error;

    }



    DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", name,

            dev_info.flags, dev_info.num_regions, dev_info.num_irqs);



    if (!(dev_info.flags & VFIO_DEVICE_FLAGS_PCI)) {

        error_report("vfio: Um, this isn't a PCI device\n");

        goto error;

    }



    vdev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);

    if (!vdev->reset_works) {

        error_report("Warning, device %s does not support reset\n", name);

    }



    if (dev_info.num_regions != VFIO_PCI_NUM_REGIONS) {

        error_report("vfio: unexpected number of io regions %u\n",

                     dev_info.num_regions);

        goto error;

    }



    if (dev_info.num_irqs != VFIO_PCI_NUM_IRQS) {

        error_report("vfio: unexpected number of irqs %u\n", dev_info.num_irqs);

        goto error;

    }



    for (i = VFIO_PCI_BAR0_REGION_INDEX; i < VFIO_PCI_ROM_REGION_INDEX; i++) {

        reg_info.index = i;



        ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);

        if (ret) {

            error_report("vfio: Error getting region %d info: %m\n", i);

            goto error;

        }



        DPRINTF("Device %s region %d:\n", name, i);

        DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

                (unsigned long)reg_info.size, (unsigned long)reg_info.offset,

                (unsigned long)reg_info.flags);



        vdev->bars[i].flags = reg_info.flags;

        vdev->bars[i].size = reg_info.size;

        vdev->bars[i].fd_offset = reg_info.offset;

        vdev->bars[i].fd = vdev->fd;

        vdev->bars[i].nr = i;

    }



    reg_info.index = VFIO_PCI_ROM_REGION_INDEX;



    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);

    if (ret) {

        error_report("vfio: Error getting ROM info: %m\n");

        goto error;

    }



    DPRINTF("Device %s ROM:\n", name);

    DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

            (unsigned long)reg_info.size, (unsigned long)reg_info.offset,

            (unsigned long)reg_info.flags);



    vdev->rom_size = reg_info.size;

    vdev->rom_offset = reg_info.offset;



    reg_info.index = VFIO_PCI_CONFIG_REGION_INDEX;



    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);

    if (ret) {

        error_report("vfio: Error getting config info: %m\n");

        goto error;

    }



    DPRINTF("Device %s config:\n", name);

    DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

            (unsigned long)reg_info.size, (unsigned long)reg_info.offset,

            (unsigned long)reg_info.flags);



    vdev->config_size = reg_info.size;

    vdev->config_offset = reg_info.offset;



error:

    if (ret) {

        QLIST_REMOVE(vdev, next);

        vdev->group = NULL;

        close(vdev->fd);

    }

    return ret;

}

static int vfio_get_device(VFIOGroup *group, const char *name, VFIODevice *vdev)

{

    struct vfio_device_info dev_info = { .argsz = sizeof(dev_info) };

    struct vfio_region_info reg_info = { .argsz = sizeof(reg_info) };

    int ret, i;



    ret = ioctl(group->fd, VFIO_GROUP_GET_DEVICE_FD, name);

    if (ret < 0) {

        error_report("vfio: error getting device %s from group %d: %m\n",

                     name, group->groupid);

        error_report("Verify all devices in group %d are bound to vfio-pci "

                     "or pci-stub and not already in use\n", group->groupid);

        return ret;

    }



    vdev->fd = ret;

    vdev->group = group;

    QLIST_INSERT_HEAD(&group->device_list, vdev, next);



    

    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_INFO, &dev_info);

    if (ret) {

        error_report("vfio: error getting device info: %m\n");

        goto error;

    }



    DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", name,

            dev_info.flags, dev_info.num_regions, dev_info.num_irqs);



    if (!(dev_info.flags & VFIO_DEVICE_FLAGS_PCI)) {

        error_report("vfio: Um, this isn't a PCI device\n");

        goto error;

    }



    vdev->reset_works = !!(dev_info.flags & VFIO_DEVICE_FLAGS_RESET);

    if (!vdev->reset_works) {

        error_report("Warning, device %s does not support reset\n", name);

    }



    if (dev_info.num_regions != VFIO_PCI_NUM_REGIONS) {

        error_report("vfio: unexpected number of io regions %u\n",

                     dev_info.num_regions);

        goto error;

    }



    if (dev_info.num_irqs != VFIO_PCI_NUM_IRQS) {

        error_report("vfio: unexpected number of irqs %u\n", dev_info.num_irqs);

        goto error;

    }



    for (i = VFIO_PCI_BAR0_REGION_INDEX; i < VFIO_PCI_ROM_REGION_INDEX; i++) {

        reg_info.index = i;



        ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);

        if (ret) {

            error_report("vfio: Error getting region %d info: %m\n", i);

            goto error;

        }



        DPRINTF("Device %s region %d:\n", name, i);

        DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

                (unsigned long)reg_info.size, (unsigned long)reg_info.offset,

                (unsigned long)reg_info.flags);



        vdev->bars[i].flags = reg_info.flags;

        vdev->bars[i].size = reg_info.size;

        vdev->bars[i].fd_offset = reg_info.offset;

        vdev->bars[i].fd = vdev->fd;

        vdev->bars[i].nr = i;

    }



    reg_info.index = VFIO_PCI_ROM_REGION_INDEX;



    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);

    if (ret) {

        error_report("vfio: Error getting ROM info: %m\n");

        goto error;

    }



    DPRINTF("Device %s ROM:\n", name);

    DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

            (unsigned long)reg_info.size, (unsigned long)reg_info.offset,

            (unsigned long)reg_info.flags);



    vdev->rom_size = reg_info.size;

    vdev->rom_offset = reg_info.offset;



    reg_info.index = VFIO_PCI_CONFIG_REGION_INDEX;



    ret = ioctl(vdev->fd, VFIO_DEVICE_GET_REGION_INFO, &reg_info);

    if (ret) {

        error_report("vfio: Error getting config info: %m\n");

        goto error;

    }



    DPRINTF("Device %s config:\n", name);

    DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

            (unsigned long)reg_info.size, (unsigned long)reg_info.offset,

            (unsigned long)reg_info.flags);



    vdev->config_size = reg_info.size;

    vdev->config_offset = reg_info.offset;



error:

    if (ret) {

        QLIST_REMOVE(vdev, next);

        vdev->group = NULL;

        close(vdev->fd);

    }

    return ret;

}

    if (dev_info.num_regions != VFIO_PCI_NUM_REGIONS) {     if (dev_info.num_irqs != VFIO_PCI_NUM_IRQS) {
static int FUNC_0(VFIOGroup *VAR_0, const char *VAR_1, VFIODevice *VAR_2)

{

    struct vfio_device_info VAR_3 = { .argsz = sizeof(VAR_3) };

    struct vfio_region_info VAR_4 = { .argsz = sizeof(VAR_4) };

    int VAR_5, VAR_6;



    VAR_5 = ioctl(VAR_0->fd, VFIO_GROUP_GET_DEVICE_FD, VAR_1);

    if (VAR_5 < 0) {

        error_report("vfio: error getting device %s from VAR_0 %d: %m\n",

                     VAR_1, VAR_0->groupid);

        error_report("Verify all devices in VAR_0 %d are bound to vfio-pci "

                     "or pci-stub and not already in use\n", VAR_0->groupid);

        return VAR_5;

    }



    VAR_2->fd = VAR_5;

    VAR_2->VAR_0 = VAR_0;

    QLIST_INSERT_HEAD(&VAR_0->device_list, VAR_2, next);



    

    VAR_5 = ioctl(VAR_2->fd, VFIO_DEVICE_GET_INFO, &VAR_3);

    if (VAR_5) {

        error_report("vfio: error getting device info: %m\n");

        goto error;

    }



    DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", VAR_1,

            VAR_3.flags, VAR_3.num_regions, VAR_3.num_irqs);



    if (!(VAR_3.flags & VFIO_DEVICE_FLAGS_PCI)) {

        error_report("vfio: Um, this isn't a PCI device\n");

        goto error;

    }



    VAR_2->reset_works = !!(VAR_3.flags & VFIO_DEVICE_FLAGS_RESET);

    if (!VAR_2->reset_works) {

        error_report("Warning, device %s does not support reset\n", VAR_1);

    }



    if (VAR_3.num_regions != VFIO_PCI_NUM_REGIONS) {

        error_report("vfio: unexpected number of io regions %u\n",

                     VAR_3.num_regions);

        goto error;

    }



    if (VAR_3.num_irqs != VFIO_PCI_NUM_IRQS) {

        error_report("vfio: unexpected number of irqs %u\n", VAR_3.num_irqs);

        goto error;

    }



    for (VAR_6 = VFIO_PCI_BAR0_REGION_INDEX; VAR_6 < VFIO_PCI_ROM_REGION_INDEX; VAR_6++) {

        VAR_4.index = VAR_6;



        VAR_5 = ioctl(VAR_2->fd, VFIO_DEVICE_GET_REGION_INFO, &VAR_4);

        if (VAR_5) {

            error_report("vfio: Error getting region %d info: %m\n", VAR_6);

            goto error;

        }



        DPRINTF("Device %s region %d:\n", VAR_1, VAR_6);

        DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

                (unsigned long)VAR_4.size, (unsigned long)VAR_4.offset,

                (unsigned long)VAR_4.flags);



        VAR_2->bars[VAR_6].flags = VAR_4.flags;

        VAR_2->bars[VAR_6].size = VAR_4.size;

        VAR_2->bars[VAR_6].fd_offset = VAR_4.offset;

        VAR_2->bars[VAR_6].fd = VAR_2->fd;

        VAR_2->bars[VAR_6].nr = VAR_6;

    }



    VAR_4.index = VFIO_PCI_ROM_REGION_INDEX;



    VAR_5 = ioctl(VAR_2->fd, VFIO_DEVICE_GET_REGION_INFO, &VAR_4);

    if (VAR_5) {

        error_report("vfio: Error getting ROM info: %m\n");

        goto error;

    }



    DPRINTF("Device %s ROM:\n", VAR_1);

    DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

            (unsigned long)VAR_4.size, (unsigned long)VAR_4.offset,

            (unsigned long)VAR_4.flags);



    VAR_2->rom_size = VAR_4.size;

    VAR_2->rom_offset = VAR_4.offset;



    VAR_4.index = VFIO_PCI_CONFIG_REGION_INDEX;



    VAR_5 = ioctl(VAR_2->fd, VFIO_DEVICE_GET_REGION_INFO, &VAR_4);

    if (VAR_5) {

        error_report("vfio: Error getting config info: %m\n");

        goto error;

    }



    DPRINTF("Device %s config:\n", VAR_1);

    DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",

            (unsigned long)VAR_4.size, (unsigned long)VAR_4.offset,

            (unsigned long)VAR_4.flags);



    VAR_2->config_size = VAR_4.size;

    VAR_2->config_offset = VAR_4.offset;



error:

    if (VAR_5) {

        QLIST_REMOVE(VAR_2, next);

        VAR_2->VAR_0 = NULL;

        close(VAR_2->fd);

    }

    return VAR_5;

}

static int FUNC_0(VFIOGroup *VAR_0, const char *VAR_1, VFIODevice *VAR_2)
{ struct vfio_device_info VAR_3 = { .argsz = sizeof(VAR_3) }; struct vfio_region_info VAR_4 = { .argsz = sizeof(VAR_4) }; int VAR_5, VAR_6; VAR_5 = ioctl(VAR_0->fd, VFIO_GROUP_GET_DEVICE_FD, VAR_1); if (VAR_5 < 0) { error_report("vfio: error getting device %s from VAR_0 %d: %m\n",
VAR_1, VAR_0->groupid); error_report("Verify all devices in VAR_0 %d are bound to vfio-pci "
"or pci-stub and not already in use\n", VAR_0->groupid); return VAR_5; } VAR_2->fd = VAR_5; VAR_2->VAR_0 = VAR_0; QLIST_INSERT_HEAD(&VAR_0->device_list, VAR_2, next); VAR_5 = ioctl(VAR_2->fd, VFIO_DEVICE_GET_INFO, &VAR_3); if (VAR_5) { error_report("vfio: error getting device info: %m\n"); goto error; } DPRINTF("Device %s flags: %u, regions: %u, irgs: %u\n", VAR_1,
VAR_3.flags, VAR_3.num_regions, VAR_3.num_irqs); if (!(VAR_3.flags & VFIO_DEVICE_FLAGS_PCI)) { error_report("vfio: Um, this isn't a PCI device\n"); goto error; } VAR_2->reset_works = !!(VAR_3.flags & VFIO_DEVICE_FLAGS_RESET); if (!VAR_2->reset_works) { error_report("Warning, device %s does not support reset\n", VAR_1); } if (VAR_3.num_regions != VFIO_PCI_NUM_REGIONS) { error_report("vfio: unexpected number of io regions %u\n",
VAR_3.num_regions); goto error; } if (VAR_3.num_irqs != VFIO_PCI_NUM_IRQS) { error_report("vfio: unexpected number of irqs %u\n", VAR_3.num_irqs); goto error; } for (VAR_6 = VFIO_PCI_BAR0_REGION_INDEX; VAR_6 < VFIO_PCI_ROM_REGION_INDEX; VAR_6++) { VAR_4.index = VAR_6; VAR_5 = ioctl(VAR_2->fd, VFIO_DEVICE_GET_REGION_INFO, &VAR_4); if (VAR_5) { error_report("vfio: Error getting region %d info: %m\n", VAR_6); goto error; } DPRINTF("Device %s region %d:\n", VAR_1, VAR_6); DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
(unsigned long)VAR_4.size, (unsigned long)VAR_4.offset,
(unsigned long)VAR_4.flags); VAR_2->bars[VAR_6].flags = VAR_4.flags; VAR_2->bars[VAR_6].size = VAR_4.size; VAR_2->bars[VAR_6].fd_offset = VAR_4.offset; VAR_2->bars[VAR_6].fd = VAR_2->fd; VAR_2->bars[VAR_6].nr = VAR_6; } VAR_4.index = VFIO_PCI_ROM_REGION_INDEX; VAR_5 = ioctl(VAR_2->fd, VFIO_DEVICE_GET_REGION_INFO, &VAR_4); if (VAR_5) { error_report("vfio: Error getting ROM info: %m\n"); goto error; } DPRINTF("Device %s ROM:\n", VAR_1); DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
(unsigned long)VAR_4.size, (unsigned long)VAR_4.offset,
(unsigned long)VAR_4.flags); VAR_2->rom_size = VAR_4.size; VAR_2->rom_offset = VAR_4.offset; VAR_4.index = VFIO_PCI_CONFIG_REGION_INDEX; VAR_5 = ioctl(VAR_2->fd, VFIO_DEVICE_GET_REGION_INFO, &VAR_4); if (VAR_5) { error_report("vfio: Error getting config info: %m\n"); goto error; } DPRINTF("Device %s config:\n", VAR_1); DPRINTF("  size: 0x%lx, offset: 0x%lx, flags: 0x%lx\n",
(unsigned long)VAR_4.size, (unsigned long)VAR_4.offset,
(unsigned long)VAR_4.flags); VAR_2->config_size = VAR_4.size; VAR_2->config_offset = VAR_4.offset; error:
if (VAR_5) { QLIST_REMOVE(VAR_2, next); VAR_2->VAR_0 = NULL; close(VAR_2->fd); } return VAR_5; }