diff mbox

[RFC,1/2] pci: add support for direct usage of bdf for capability lookup

Message ID 1500470414-7911-2-git-send-email-zuban32s@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Aleksandr Bezzubikov July 19, 2017, 1:20 p.m. UTC
Add a capability lookup function which gets bdf instead of pci_device
as its first argument. It may be useful when we have bdf,
but don't have the whole pci_device structure.

Signed-off-by: Aleksandr Bezzubikov <zuban32s@gmail.com>
---
 src/hw/pcidevice.c | 24 ++++++++++++++++++++++++
 src/hw/pcidevice.h |  1 +
 2 files changed, 25 insertions(+)

Comments

Marcel Apfelbaum July 19, 2017, 2:24 p.m. UTC | #1
On 19/07/2017 16:20, Aleksandr Bezzubikov wrote:
> Add a capability lookup function which gets bdf instead of pci_device
> as its first argument. It may be useful when we have bdf,
> but don't have the whole pci_device structure.
> 
> Signed-off-by: Aleksandr Bezzubikov <zuban32s@gmail.com>
> ---
>   src/hw/pcidevice.c | 24 ++++++++++++++++++++++++
>   src/hw/pcidevice.h |  1 +
>   2 files changed, 25 insertions(+)
> 
> diff --git a/src/hw/pcidevice.c b/src/hw/pcidevice.c
> index cfebf66..3fa240e 100644
> --- a/src/hw/pcidevice.c
> +++ b/src/hw/pcidevice.c
> @@ -158,6 +158,30 @@ u8 pci_find_capability(struct pci_device *pci, u8 cap_id, u8 cap)
>       return 0;
>   }
>   

Hi Aleksandr,

> +u8 pci_find_capability_bdf(int bdf, u8 cap_id, u8 cap)
> +{

Please do not duplicate the code for the 'pci_find_capability'.
In this case you can reuse the code by
making 'pci_find_capability' call 'pci_find_capability_bdf',
or better, instead of a new function, change the signature
as you proposed and make the calling code use bdf
instead of the pci device, since is the only info needed.

Thanks,
Marcel

> +    int i;
> +    u16 status = pci_config_readw(bdf, PCI_STATUS);
> +
> +    if (!(status & PCI_STATUS_CAP_LIST))
> +        return 0;
> +
> +    if (cap == 0) {
> +        /* find first */
> +        cap = pci_config_readb(bdf, PCI_CAPABILITY_LIST);
> +    } else {
> +        /* find next */
> +        cap = pci_config_readb(bdf, cap + PCI_CAP_LIST_NEXT);
> +    }
> +    for (i = 0; cap && i <= 0xff; i++) {
> +        if (pci_config_readb(bdf, cap + PCI_CAP_LIST_ID) == cap_id)
> +            return cap;
> +        cap = pci_config_readb(bdf, cap + PCI_CAP_LIST_NEXT);
> +    }
> +
> +    return 0;
> +}
> +
>   // Enable PCI bus-mastering (ie, DMA) support on a pci device
>   void
>   pci_enable_busmaster(struct pci_device *pci)
> diff --git a/src/hw/pcidevice.h b/src/hw/pcidevice.h
> index 354b549..e4ed5cf 100644
> --- a/src/hw/pcidevice.h
> +++ b/src/hw/pcidevice.h
> @@ -70,6 +70,7 @@ int pci_init_device(const struct pci_device_id *ids
>   struct pci_device *pci_find_init_device(const struct pci_device_id *ids
>                                           , void *arg);
>   u8 pci_find_capability(struct pci_device *pci, u8 cap_id, u8 cap);
> +u8 pci_find_capability_bdf(int bdf, u8 cap_id, u8 cap);
>   void pci_enable_busmaster(struct pci_device *pci);
>   u16 pci_enable_iobar(struct pci_device *pci, u32 addr);
>   void *pci_enable_membar(struct pci_device *pci, u32 addr);
>
diff mbox

Patch

diff --git a/src/hw/pcidevice.c b/src/hw/pcidevice.c
index cfebf66..3fa240e 100644
--- a/src/hw/pcidevice.c
+++ b/src/hw/pcidevice.c
@@ -158,6 +158,30 @@  u8 pci_find_capability(struct pci_device *pci, u8 cap_id, u8 cap)
     return 0;
 }
 
+u8 pci_find_capability_bdf(int bdf, u8 cap_id, u8 cap)
+{
+    int i;
+    u16 status = pci_config_readw(bdf, PCI_STATUS);
+
+    if (!(status & PCI_STATUS_CAP_LIST))
+        return 0;
+
+    if (cap == 0) {
+        /* find first */
+        cap = pci_config_readb(bdf, PCI_CAPABILITY_LIST);
+    } else {
+        /* find next */
+        cap = pci_config_readb(bdf, cap + PCI_CAP_LIST_NEXT);
+    }
+    for (i = 0; cap && i <= 0xff; i++) {
+        if (pci_config_readb(bdf, cap + PCI_CAP_LIST_ID) == cap_id)
+            return cap;
+        cap = pci_config_readb(bdf, cap + PCI_CAP_LIST_NEXT);
+    }
+
+    return 0;
+}
+
 // Enable PCI bus-mastering (ie, DMA) support on a pci device
 void
 pci_enable_busmaster(struct pci_device *pci)
diff --git a/src/hw/pcidevice.h b/src/hw/pcidevice.h
index 354b549..e4ed5cf 100644
--- a/src/hw/pcidevice.h
+++ b/src/hw/pcidevice.h
@@ -70,6 +70,7 @@  int pci_init_device(const struct pci_device_id *ids
 struct pci_device *pci_find_init_device(const struct pci_device_id *ids
                                         , void *arg);
 u8 pci_find_capability(struct pci_device *pci, u8 cap_id, u8 cap);
+u8 pci_find_capability_bdf(int bdf, u8 cap_id, u8 cap);
 void pci_enable_busmaster(struct pci_device *pci);
 u16 pci_enable_iobar(struct pci_device *pci, u32 addr);
 void *pci_enable_membar(struct pci_device *pci, u32 addr);