diff mbox

[v4,05/10] vfio: extending function vfio_pci_host_match to support mask func number

Message ID 1458554926-7844-6-git-send-email-caoj.fnst@cn.fujitsu.com (mailing list archive)
State New, archived
Headers show

Commit Message

Cao jin March 21, 2016, 10:08 a.m. UTC
From: Chen Fan <chen.fan.fnst@cn.fujitsu.com>

Signed-off-by: Chen Fan <chen.fan.fnst@cn.fujitsu.com>
---
 hw/vfio/pci.c | 29 ++++++++++++++++++++---------
 1 file changed, 20 insertions(+), 9 deletions(-)

Comments

Alex Williamson March 21, 2016, 9:39 p.m. UTC | #1
On Mon, 21 Mar 2016 18:08:41 +0800
Cao jin <caoj.fnst@cn.fujitsu.com> wrote:

> From: Chen Fan <chen.fan.fnst@cn.fujitsu.com>
> 
> Signed-off-by: Chen Fan <chen.fan.fnst@cn.fujitsu.com>
> ---
>  hw/vfio/pci.c | 29 ++++++++++++++++++++---------
>  1 file changed, 20 insertions(+), 9 deletions(-)
> 
> diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
> index 0516d94..8842b7f 100644
> --- a/hw/vfio/pci.c
> +++ b/hw/vfio/pci.c
> @@ -2060,14 +2060,25 @@ static void vfio_pci_post_reset(VFIOPCIDevice *vdev)
>      vfio_intx_enable(vdev);
>  }
>  
> -static bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name)
> +#define HOST_CMP_FUNC_MASK       (1 << 0)
> +static bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name,
> +                                uint8_t mask)
>  {
> -    char tmp[13];
> +    PCIHostDeviceAddress tmp;
>  
> -    sprintf(tmp, "%04x:%02x:%02x.%1x", addr->domain,
> -            addr->bus, addr->slot, addr->function);
> +    if (strlen(name) != 12) {
> +        return false;
> +    }
> +
> +    if (sscanf(name, "%04x:%02x:%02x.%1x", &tmp.domain,
> +               &tmp.bus, &tmp.slot, &tmp.function) != 4) {
> +        return false;
> +    }
>  
> -    return (strcmp(tmp, name) == 0);
> +    return (tmp.domain == addr->domain && tmp.bus == addr->bus &&
> +            tmp.slot == addr->slot &&
> +            ((mask & HOST_CMP_FUNC_MASK) ?
> +                1 : (tmp.function == addr->function)));
>  }

I'd probably go for something like:

static int vfio_pci_name_to_addr(const char *name, PCIHostDeviceAddress *addr)
{
    if (strlen(name) != 12 ||
        sscanf(name, "%04x:%02x:%02x.%1x", &addr->domain,
               &addr->bus, &addr->slot, &addr->function) != 4 ) {
        return -EINVAL;
    }

    return 0;
}

static bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name)
{
    PCIHostDeviceAddress tmp;

    if (vfio_pci_name_to_addr(name, &tmp)) {
        return false;
    }

    return (tmp.domain == addr->domain && tmp.bus == addr->bus &&
            tmp.slot == addr->slot && tmp.function == addr->function);
}

Then a _slot version that avoids skips the function comparison.  The
mask argument doesn't make much sense for such a simple function when
the code duplication is trivial.
diff mbox

Patch

diff --git a/hw/vfio/pci.c b/hw/vfio/pci.c
index 0516d94..8842b7f 100644
--- a/hw/vfio/pci.c
+++ b/hw/vfio/pci.c
@@ -2060,14 +2060,25 @@  static void vfio_pci_post_reset(VFIOPCIDevice *vdev)
     vfio_intx_enable(vdev);
 }
 
-static bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name)
+#define HOST_CMP_FUNC_MASK       (1 << 0)
+static bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name,
+                                uint8_t mask)
 {
-    char tmp[13];
+    PCIHostDeviceAddress tmp;
 
-    sprintf(tmp, "%04x:%02x:%02x.%1x", addr->domain,
-            addr->bus, addr->slot, addr->function);
+    if (strlen(name) != 12) {
+        return false;
+    }
+
+    if (sscanf(name, "%04x:%02x:%02x.%1x", &tmp.domain,
+               &tmp.bus, &tmp.slot, &tmp.function) != 4) {
+        return false;
+    }
 
-    return (strcmp(tmp, name) == 0);
+    return (tmp.domain == addr->domain && tmp.bus == addr->bus &&
+            tmp.slot == addr->slot &&
+            ((mask & HOST_CMP_FUNC_MASK) ?
+                1 : (tmp.function == addr->function)));
 }
 
 static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single)
@@ -2109,7 +2120,7 @@  static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single)
         trace_vfio_pci_hot_reset_dep_devices(host.domain,
                 host.bus, host.slot, host.function, devices[i].group_id);
 
-        if (vfio_pci_host_match(&host, vdev->vbasedev.name)) {
+        if (vfio_pci_host_match(&host, vdev->vbasedev.name, 0)) {
             continue;
         }
 
@@ -2135,7 +2146,7 @@  static int vfio_pci_hot_reset(VFIOPCIDevice *vdev, bool single)
                 continue;
             }
             tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
-            if (vfio_pci_host_match(&host, tmp->vbasedev.name)) {
+            if (vfio_pci_host_match(&host, tmp->vbasedev.name, 0)) {
                 if (single) {
                     ret = -EINVAL;
                     goto out_single;
@@ -2170,7 +2181,7 @@  out:
         host.slot = PCI_SLOT(devices[i].devfn);
         host.function = PCI_FUNC(devices[i].devfn);
 
-        if (vfio_pci_host_match(&host, vdev->vbasedev.name)) {
+        if (vfio_pci_host_match(&host, vdev->vbasedev.name, 0)) {
             continue;
         }
 
@@ -2189,7 +2200,7 @@  out:
                 continue;
             }
             tmp = container_of(vbasedev_iter, VFIOPCIDevice, vbasedev);
-            if (vfio_pci_host_match(&host, tmp->vbasedev.name)) {
+            if (vfio_pci_host_match(&host, tmp->vbasedev.name, 0)) {
                 vfio_pci_post_reset(tmp);
                 break;
             }