vfio-pci: Cleanup read/write functions
The read and write functions are nearly identical, combine them and convert to a switch statement. This also makes it easy to narrow the scope of when we use the io/mem accessors in case new regions are added. Signed-off-by: Alex Williamson <alex.williamson@redhat.com>
This commit is contained in:
parent
5641ade41f
commit
5b279a11d3
1 changed files with 29 additions and 30 deletions
|
@ -366,8 +366,8 @@ static long vfio_pci_ioctl(void *device_data,
|
||||||
return -ENOTTY;
|
return -ENOTTY;
|
||||||
}
|
}
|
||||||
|
|
||||||
static ssize_t vfio_pci_read(void *device_data, char __user *buf,
|
static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
|
||||||
size_t count, loff_t *ppos)
|
size_t count, loff_t *ppos, bool iswrite)
|
||||||
{
|
{
|
||||||
unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
|
unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
|
||||||
struct vfio_pci_device *vdev = device_data;
|
struct vfio_pci_device *vdev = device_data;
|
||||||
|
@ -376,42 +376,41 @@ static ssize_t vfio_pci_read(void *device_data, char __user *buf,
|
||||||
if (index >= VFIO_PCI_NUM_REGIONS)
|
if (index >= VFIO_PCI_NUM_REGIONS)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (index == VFIO_PCI_CONFIG_REGION_INDEX)
|
switch (index) {
|
||||||
return vfio_pci_config_readwrite(vdev, buf, count, ppos, false);
|
case VFIO_PCI_CONFIG_REGION_INDEX:
|
||||||
else if (index == VFIO_PCI_ROM_REGION_INDEX)
|
return vfio_pci_config_readwrite(vdev, buf, count,
|
||||||
return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false);
|
ppos, iswrite);
|
||||||
else if (pci_resource_flags(pdev, index) & IORESOURCE_IO)
|
case VFIO_PCI_ROM_REGION_INDEX:
|
||||||
return vfio_pci_io_readwrite(vdev, buf, count, ppos, false);
|
if (iswrite)
|
||||||
else if (pci_resource_flags(pdev, index) & IORESOURCE_MEM)
|
return -EINVAL;
|
||||||
return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false);
|
return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false);
|
||||||
|
|
||||||
|
case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
|
||||||
|
{
|
||||||
|
unsigned long flags = pci_resource_flags(pdev, index);
|
||||||
|
|
||||||
|
if (flags & IORESOURCE_IO)
|
||||||
|
return vfio_pci_io_readwrite(vdev, buf, count,
|
||||||
|
ppos, iswrite);
|
||||||
|
if (flags & IORESOURCE_MEM)
|
||||||
|
return vfio_pci_mem_readwrite(vdev, buf, count,
|
||||||
|
ppos, iswrite);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static ssize_t vfio_pci_read(void *device_data, char __user *buf,
|
||||||
|
size_t count, loff_t *ppos)
|
||||||
|
{
|
||||||
|
return vfio_pci_rw(device_data, buf, count, ppos, false);
|
||||||
|
}
|
||||||
|
|
||||||
static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
|
static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
|
||||||
size_t count, loff_t *ppos)
|
size_t count, loff_t *ppos)
|
||||||
{
|
{
|
||||||
unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
|
return vfio_pci_rw(device_data, buf, count, ppos, true);
|
||||||
struct vfio_pci_device *vdev = device_data;
|
|
||||||
struct pci_dev *pdev = vdev->pdev;
|
|
||||||
|
|
||||||
if (index >= VFIO_PCI_NUM_REGIONS)
|
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
if (index == VFIO_PCI_CONFIG_REGION_INDEX)
|
|
||||||
return vfio_pci_config_readwrite(vdev, (char __user *)buf,
|
|
||||||
count, ppos, true);
|
|
||||||
else if (index == VFIO_PCI_ROM_REGION_INDEX)
|
|
||||||
return -EINVAL;
|
|
||||||
else if (pci_resource_flags(pdev, index) & IORESOURCE_IO)
|
|
||||||
return vfio_pci_io_readwrite(vdev, (char __user *)buf,
|
|
||||||
count, ppos, true);
|
|
||||||
else if (pci_resource_flags(pdev, index) & IORESOURCE_MEM) {
|
|
||||||
return vfio_pci_mem_readwrite(vdev, (char __user *)buf,
|
|
||||||
count, ppos, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return -EINVAL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
|
static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)
|
||||||
|
|
Loading…
Add table
Reference in a new issue