[Qemu-devel] [PATCH v3 5/5] pci: Fold pci_get_bus_devfn() into its sole caller

David Gibson posted 5 patches 6 years, 9 months ago
Maintainers: David Gibson <david@gibson.dropbear.id.au>, "Michael S. Tsirkin" <mst@redhat.com>, Mark Cave-Ayland <mark.cave-ayland@ilande.co.uk>, Marcel Apfelbaum <marcel.apfelbaum@gmail.com>
There is a newer version of this series
[Qemu-devel] [PATCH v3 5/5] pci: Fold pci_get_bus_devfn() into its sole caller
Posted by David Gibson 6 years, 9 months ago
The only remaining caller of pci_get_bus_devfn() is pci_nic_init_nofail(),
itself an old compatibility function.  Fold the two together to avoid
re-using the stale interface.

While we're there replace the explicit fprintf()s with error_report().

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 hw/pci/pci.c | 61 +++++++++++++++++++++++++---------------------------
 1 file changed, 29 insertions(+), 32 deletions(-)

diff --git a/hw/pci/pci.c b/hw/pci/pci.c
index 7e5f8d001b..90e2743185 100644
--- a/hw/pci/pci.c
+++ b/hw/pci/pci.c
@@ -723,37 +723,6 @@ static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
     return 0;
 }
 
-static PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root,
-                                 const char *devaddr)
-{
-    int dom, bus;
-    unsigned slot;
-
-    if (!root) {
-        fprintf(stderr, "No primary PCI bus\n");
-        return NULL;
-    }
-
-    assert(!root->parent_dev);
-
-    if (!devaddr) {
-        *devfnp = -1;
-        return pci_find_bus_nr(root, 0);
-    }
-
-    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
-        return NULL;
-    }
-
-    if (dom != 0) {
-        fprintf(stderr, "No support for non-zero PCI domains\n");
-        return NULL;
-    }
-
-    *devfnp = PCI_DEVFN(slot, 0);
-    return pci_find_bus_nr(root, bus);
-}
-
 static void pci_init_cmask(PCIDevice *dev)
 {
     pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
@@ -1895,6 +1864,8 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
     DeviceState *dev;
     int devfn;
     int i;
+    int dom, busnr;
+    unsigned slot;
 
     if (nd->model && !strcmp(nd->model, "virtio")) {
         g_free(nd->model);
@@ -1928,7 +1899,33 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
         exit(1);
     }
 
-    bus = pci_get_bus_devfn(&devfn, rootbus, devaddr);
+    if (!rootbus) {
+        error_report("No primary PCI bus");
+        exit(1);
+    }
+
+    assert(!rootbus->parent_dev);
+
+    if (!devaddr) {
+        devfn = -1;
+        busnr = 0;
+        bus = pci_find_bus_nr(rootbus, 0);
+    } else {
+        if (pci_parse_devaddr(devaddr, &dom, &busnr, &slot, NULL) < 0) {
+            error_report("Invalid PCI device address %s for device %s",
+                         devaddr, nd->model);
+            exit(1);
+        }
+
+        if (dom != 0) {
+            error_report("No support for non-zero PCI domains");
+            exit(1);
+        }
+
+        devfn = PCI_DEVFN(slot, 0);
+    }
+
+    bus = pci_find_bus_nr(rootbus, busnr);
     if (!bus) {
         error_report("Invalid PCI device address %s for device %s",
                      devaddr, nd->model);
-- 
2.21.0


Re: [Qemu-devel] [Qemu-ppc] [PATCH v3 5/5] pci: Fold pci_get_bus_devfn() into its sole caller
Posted by Greg Kurz 6 years, 9 months ago
On Tue,  7 May 2019 16:23:16 +1000
David Gibson <david@gibson.dropbear.id.au> wrote:

> The only remaining caller of pci_get_bus_devfn() is pci_nic_init_nofail(),
> itself an old compatibility function.  Fold the two together to avoid
> re-using the stale interface.
> 
> While we're there replace the explicit fprintf()s with error_report().
> 
> Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> ---
>  hw/pci/pci.c | 61 +++++++++++++++++++++++++---------------------------
>  1 file changed, 29 insertions(+), 32 deletions(-)
> 
> diff --git a/hw/pci/pci.c b/hw/pci/pci.c
> index 7e5f8d001b..90e2743185 100644
> --- a/hw/pci/pci.c
> +++ b/hw/pci/pci.c
> @@ -723,37 +723,6 @@ static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
>      return 0;
>  }
>  
> -static PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root,
> -                                 const char *devaddr)
> -{
> -    int dom, bus;
> -    unsigned slot;
> -
> -    if (!root) {
> -        fprintf(stderr, "No primary PCI bus\n");
> -        return NULL;
> -    }
> -
> -    assert(!root->parent_dev);
> -
> -    if (!devaddr) {
> -        *devfnp = -1;
> -        return pci_find_bus_nr(root, 0);
> -    }
> -
> -    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
> -        return NULL;
> -    }
> -
> -    if (dom != 0) {
> -        fprintf(stderr, "No support for non-zero PCI domains\n");
> -        return NULL;
> -    }
> -
> -    *devfnp = PCI_DEVFN(slot, 0);
> -    return pci_find_bus_nr(root, bus);
> -}
> -
>  static void pci_init_cmask(PCIDevice *dev)
>  {
>      pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
> @@ -1895,6 +1864,8 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
>      DeviceState *dev;
>      int devfn;
>      int i;
> +    int dom, busnr;
> +    unsigned slot;
>  
>      if (nd->model && !strcmp(nd->model, "virtio")) {
>          g_free(nd->model);
> @@ -1928,7 +1899,33 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
>          exit(1);
>      }
>  
> -    bus = pci_get_bus_devfn(&devfn, rootbus, devaddr);
> +    if (!rootbus) {
> +        error_report("No primary PCI bus");
> +        exit(1);
> +    }
> +
> +    assert(!rootbus->parent_dev);
> +
> +    if (!devaddr) {
> +        devfn = -1;
> +        busnr = 0;
> +        bus = pci_find_bus_nr(rootbus, 0);

This line isn't needed since it is done below...

> +    } else {
> +        if (pci_parse_devaddr(devaddr, &dom, &busnr, &slot, NULL) < 0) {
> +            error_report("Invalid PCI device address %s for device %s",
> +                         devaddr, nd->model);
> +            exit(1);
> +        }
> +
> +        if (dom != 0) {
> +            error_report("No support for non-zero PCI domains");
> +            exit(1);
> +        }
> +
> +        devfn = PCI_DEVFN(slot, 0);
> +    }
> +
> +    bus = pci_find_bus_nr(rootbus, busnr);

... here.

>      if (!bus) {
>          error_report("Invalid PCI device address %s for device %s",
>                       devaddr, nd->model);

Maybe output a different message from the one for pci_parse_devaddr()
failures ? Here, the address is supposed to be well formatted but we
couldn't find the requested bus.


Re: [Qemu-devel] [Qemu-ppc] [PATCH v3 5/5] pci: Fold pci_get_bus_devfn() into its sole caller
Posted by David Gibson 6 years, 9 months ago
On Tue, May 07, 2019 at 12:21:29PM +0200, Greg Kurz wrote:
> On Tue,  7 May 2019 16:23:16 +1000
> David Gibson <david@gibson.dropbear.id.au> wrote:
> 
> > The only remaining caller of pci_get_bus_devfn() is pci_nic_init_nofail(),
> > itself an old compatibility function.  Fold the two together to avoid
> > re-using the stale interface.
> > 
> > While we're there replace the explicit fprintf()s with error_report().
> > 
> > Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
> > ---
> >  hw/pci/pci.c | 61 +++++++++++++++++++++++++---------------------------
> >  1 file changed, 29 insertions(+), 32 deletions(-)
> > 
> > diff --git a/hw/pci/pci.c b/hw/pci/pci.c
> > index 7e5f8d001b..90e2743185 100644
> > --- a/hw/pci/pci.c
> > +++ b/hw/pci/pci.c
> > @@ -723,37 +723,6 @@ static int pci_parse_devaddr(const char *addr, int *domp, int *busp,
> >      return 0;
> >  }
> >  
> > -static PCIBus *pci_get_bus_devfn(int *devfnp, PCIBus *root,
> > -                                 const char *devaddr)
> > -{
> > -    int dom, bus;
> > -    unsigned slot;
> > -
> > -    if (!root) {
> > -        fprintf(stderr, "No primary PCI bus\n");
> > -        return NULL;
> > -    }
> > -
> > -    assert(!root->parent_dev);
> > -
> > -    if (!devaddr) {
> > -        *devfnp = -1;
> > -        return pci_find_bus_nr(root, 0);
> > -    }
> > -
> > -    if (pci_parse_devaddr(devaddr, &dom, &bus, &slot, NULL) < 0) {
> > -        return NULL;
> > -    }
> > -
> > -    if (dom != 0) {
> > -        fprintf(stderr, "No support for non-zero PCI domains\n");
> > -        return NULL;
> > -    }
> > -
> > -    *devfnp = PCI_DEVFN(slot, 0);
> > -    return pci_find_bus_nr(root, bus);
> > -}
> > -
> >  static void pci_init_cmask(PCIDevice *dev)
> >  {
> >      pci_set_word(dev->cmask + PCI_VENDOR_ID, 0xffff);
> > @@ -1895,6 +1864,8 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
> >      DeviceState *dev;
> >      int devfn;
> >      int i;
> > +    int dom, busnr;
> > +    unsigned slot;
> >  
> >      if (nd->model && !strcmp(nd->model, "virtio")) {
> >          g_free(nd->model);
> > @@ -1928,7 +1899,33 @@ PCIDevice *pci_nic_init_nofail(NICInfo *nd, PCIBus *rootbus,
> >          exit(1);
> >      }
> >  
> > -    bus = pci_get_bus_devfn(&devfn, rootbus, devaddr);
> > +    if (!rootbus) {
> > +        error_report("No primary PCI bus");
> > +        exit(1);
> > +    }
> > +
> > +    assert(!rootbus->parent_dev);
> > +
> > +    if (!devaddr) {
> > +        devfn = -1;
> > +        busnr = 0;
> > +        bus = pci_find_bus_nr(rootbus, 0);
> 
> This line isn't needed since it is done below...

Oops, missed that when I factored the find_bus_nr out of the if.
Fixed now.

> > +    } else {
> > +        if (pci_parse_devaddr(devaddr, &dom, &busnr, &slot, NULL) < 0) {
> > +            error_report("Invalid PCI device address %s for device %s",
> > +                         devaddr, nd->model);
> > +            exit(1);
> > +        }
> > +
> > +        if (dom != 0) {
> > +            error_report("No support for non-zero PCI domains");
> > +            exit(1);
> > +        }
> > +
> > +        devfn = PCI_DEVFN(slot, 0);
> > +    }
> > +
> > +    bus = pci_find_bus_nr(rootbus, busnr);
> 
> ... here.
> 
> >      if (!bus) {
> >          error_report("Invalid PCI device address %s for device %s",
> >                       devaddr, nd->model);
> 
> Maybe output a different message from the one for pci_parse_devaddr()
> failures ? Here, the address is supposed to be well formatted but we
> couldn't find the requested bus.

I thought about that, but couldn't think of a could way of expressing
it.  Since this is only for a legacy option and it was already
ambiguous, I'm not overly concerned by it.

-- 
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson