aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorAlex Williamson <alex.williamson@redhat.com>2013-02-14 16:02:12 -0500
committerAlex Williamson <alex.williamson@redhat.com>2013-02-14 16:02:12 -0500
commit5b279a11d32998aad1e45fe9de225302b6a8e8ba (patch)
treedf22b9de21b3183175ac393d410d15a45bda8a30 /drivers
parent5641ade41f7c7d16e614e25ce3315e04f1bacd33 (diff)
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>
Diffstat (limited to 'drivers')
-rw-r--r--drivers/vfio/pci/vfio_pci.c59
1 files changed, 29 insertions, 30 deletions
diff --git a/drivers/vfio/pci/vfio_pci.c b/drivers/vfio/pci/vfio_pci.c
index b28e66c4376a..469e110d7ea6 100644
--- a/drivers/vfio/pci/vfio_pci.c
+++ b/drivers/vfio/pci/vfio_pci.c
@@ -366,8 +366,8 @@ static long vfio_pci_ioctl(void *device_data,
366 return -ENOTTY; 366 return -ENOTTY;
367} 367}
368 368
369static ssize_t vfio_pci_read(void *device_data, char __user *buf, 369static ssize_t vfio_pci_rw(void *device_data, char __user *buf,
370 size_t count, loff_t *ppos) 370 size_t count, loff_t *ppos, bool iswrite)
371{ 371{
372 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); 372 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos);
373 struct vfio_pci_device *vdev = device_data; 373 struct vfio_pci_device *vdev = device_data;
@@ -376,42 +376,41 @@ static ssize_t vfio_pci_read(void *device_data, char __user *buf,
376 if (index >= VFIO_PCI_NUM_REGIONS) 376 if (index >= VFIO_PCI_NUM_REGIONS)
377 return -EINVAL; 377 return -EINVAL;
378 378
379 if (index == VFIO_PCI_CONFIG_REGION_INDEX) 379 switch (index) {
380 return vfio_pci_config_readwrite(vdev, buf, count, ppos, false); 380 case VFIO_PCI_CONFIG_REGION_INDEX:
381 else if (index == VFIO_PCI_ROM_REGION_INDEX) 381 return vfio_pci_config_readwrite(vdev, buf, count,
382 return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false); 382 ppos, iswrite);
383 else if (pci_resource_flags(pdev, index) & IORESOURCE_IO) 383 case VFIO_PCI_ROM_REGION_INDEX:
384 return vfio_pci_io_readwrite(vdev, buf, count, ppos, false); 384 if (iswrite)
385 else if (pci_resource_flags(pdev, index) & IORESOURCE_MEM) 385 return -EINVAL;
386 return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false); 386 return vfio_pci_mem_readwrite(vdev, buf, count, ppos, false);
387 387
388 case VFIO_PCI_BAR0_REGION_INDEX ... VFIO_PCI_BAR5_REGION_INDEX:
389 {
390 unsigned long flags = pci_resource_flags(pdev, index);
391
392 if (flags & IORESOURCE_IO)
393 return vfio_pci_io_readwrite(vdev, buf, count,
394 ppos, iswrite);
395 if (flags & IORESOURCE_MEM)
396 return vfio_pci_mem_readwrite(vdev, buf, count,
397 ppos, iswrite);
398 }
399 }
400
388 return -EINVAL; 401 return -EINVAL;
389} 402}
390 403
404static ssize_t vfio_pci_read(void *device_data, char __user *buf,
405 size_t count, loff_t *ppos)
406{
407 return vfio_pci_rw(device_data, buf, count, ppos, false);
408}
409
391static ssize_t vfio_pci_write(void *device_data, const char __user *buf, 410static ssize_t vfio_pci_write(void *device_data, const char __user *buf,
392 size_t count, loff_t *ppos) 411 size_t count, loff_t *ppos)
393{ 412{
394 unsigned int index = VFIO_PCI_OFFSET_TO_INDEX(*ppos); 413 return vfio_pci_rw(device_data, buf, count, ppos, true);
395 struct vfio_pci_device *vdev = device_data;
396 struct pci_dev *pdev = vdev->pdev;
397
398 if (index >= VFIO_PCI_NUM_REGIONS)
399 return -EINVAL;
400
401 if (index == VFIO_PCI_CONFIG_REGION_INDEX)
402 return vfio_pci_config_readwrite(vdev, (char __user *)buf,
403 count, ppos, true);
404 else if (index == VFIO_PCI_ROM_REGION_INDEX)
405 return -EINVAL;
406 else if (pci_resource_flags(pdev, index) & IORESOURCE_IO)
407 return vfio_pci_io_readwrite(vdev, (char __user *)buf,
408 count, ppos, true);
409 else if (pci_resource_flags(pdev, index) & IORESOURCE_MEM) {
410 return vfio_pci_mem_readwrite(vdev, (char __user *)buf,
411 count, ppos, true);
412 }
413
414 return -EINVAL;
415} 414}
416 415
417static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma) 416static int vfio_pci_mmap(void *device_data, struct vm_area_struct *vma)