Skip to contentSkip to conversation

pci: Use pci_sbdf_t in pci_add_device()

PATCH In review Teddy Astie 3 participants 37 messages Jun 29 – Jul 7 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v3 1/18 pci: Use pci_sbdf_t in pci_device_detect()

Use a single pci_sbdf_t instead of each of its part as individual parameters.

---
v3: Don't adjust adjacent code.

---
 xen/drivers/char/ehci-dbgp.c       | 4 ++--
 xen/drivers/passthrough/pci.c      | 6 +++---
 xen/drivers/passthrough/vtd/dmar.c | 9 +++++----
 xen/include/xen/pci.h              | 2 +-
 4 files changed, 11 insertions(+), 10 deletions(-)
xen/drivers/char/ehci-dbgp.c +2 -2
index a5c79f56fc..2a58213cab 100644
@@ -706,7 +706,7 @@ static unsigned int __init find_dbgp(struct ehci_dbgp *dbgp,
             {
                 unsigned int cap;
 
-                if ( !pci_device_detect(0, bus, slot, func) )
+                if ( !pci_device_detect(PCI_SBDF(0, bus, slot, func)) )
                 {
                     if ( !func )
                         break;
@@ -1520,7 +1520,7 @@ void __init ehci_dbgp_init(void)
         dbgp->slot = slot;
         dbgp->func = func;
 
-        if ( !pci_device_detect(0, bus, slot, func) )
+        if ( !pci_device_detect(PCI_SBDF(0, bus, slot, func)) )
             return;
 
         dbgp->cap = __find_dbgp(bus, slot, func);
xen/drivers/passthrough/pci.c +3 -3
index d37ceb7b6c..0adf573150 100644
@@ -1166,11 +1166,11 @@ out:
     return ret;
 }
 
-bool __init pci_device_detect(u16 seg, u8 bus, u8 dev, u8 func)
+bool __init pci_device_detect(pci_sbdf_t sbdf)
 {
     u32 vendor;
 
-    vendor = pci_conf_read32(PCI_SBDF(seg, bus, dev, func), PCI_VENDOR_ID);
+    vendor = pci_conf_read32(sbdf, PCI_VENDOR_ID);
     /* some broken boards return 0 or ~0 if a slot is empty: */
     if ( (vendor == 0xffffffffU) || (vendor == 0x00000000U) ||
          (vendor == 0x0000ffffU) || (vendor == 0xffff0000U) )
@@ -1221,7 +1221,7 @@ static int __init cf_check _scan_pci_devices(struct pci_seg *pseg, void *arg)
         {
             for ( func = 0; func < 8; func++ )
             {
-                if ( !pci_device_detect(pseg->nr, bus, dev, func) )
+                if ( !pci_device_detect(PCI_SBDF(pseg->nr, bus, dev, func)) )
                 {
                     if ( !func )
                         break;
xen/drivers/passthrough/vtd/dmar.c +5 -4
index 2a756831a6..8db9591a36 100644
@@ -389,7 +389,8 @@ static int __init acpi_parse_dev_scope(
                 printk(VTDPREFIX " endpoint: %pp\n",
                        &PCI_SBDF(seg, bus, path->dev, path->fn));
 
-            if ( drhd && pci_device_detect(seg, bus, path->dev, path->fn) )
+            if ( drhd && pci_device_detect(PCI_SBDF(seg, bus, path->dev,
+                                                    path->fn)) )
             {
                 if ( pci_conf_read8(PCI_SBDF(seg, bus, path->dev, path->fn),
                                     PCI_CLASS_DEVICE + 1) != 0x03
@@ -537,7 +538,7 @@ acpi_parse_one_drhd(struct acpi_dmar_header *header)
             d = PCI_SLOT(dmaru->scope.devices[i]);
             f = PCI_FUNC(dmaru->scope.devices[i]);
 
-            if ( !pci_device_detect(drhd->segment, b, d, f) )
+            if ( !pci_device_detect(PCI_SBDF(drhd->segment, b, d, f)) )
                 printk(XENLOG_WARNING VTDPREFIX
                        " Non-existent device (%pp) in this DRHD's scope!\n",
                        &PCI_SBDF(drhd->segment, b, d, f));
@@ -573,7 +574,7 @@ static int __init register_one_rmrr(struct acpi_rmrr_unit *rmrru)
         u8 d = PCI_SLOT(rmrru->scope.devices[i]);
         u8 f = PCI_FUNC(rmrru->scope.devices[i]);
 
-        if ( pci_device_detect(rmrru->segment, b, d, f) == 0 )
+        if ( pci_device_detect(PCI_SBDF(rmrru->segment, b, d, f)) == 0 )
         {
             dprintk(XENLOG_WARNING VTDPREFIX,
                     " Non-existent device (%pp) is reported"
@@ -767,7 +768,7 @@ static int __init register_one_satc(struct acpi_satc_unit *satcu)
         uint8_t d = PCI_SLOT(satcu->scope.devices[i]);
         uint8_t f = PCI_FUNC(satcu->scope.devices[i]);
 
-        if ( !pci_device_detect(satcu->segment, b, d, f) )
+        if ( !pci_device_detect(PCI_SBDF(satcu->segment, b, d, f)) )
         {
             dprintk(XENLOG_WARNING VTDPREFIX,
                     " Non-existent device (%pp) is reported in SATC scope!\n",
xen/include/xen/pci.h +1 -1
index afb6bbf50d..ade882caee 100644
@@ -218,7 +218,7 @@ static always_inline bool pcidevs_trylock(void)
 #endif
 
 bool pci_known_segment(u16 seg);
-bool pci_device_detect(u16 seg, u8 bus, u8 dev, u8 func);
+bool pci_device_detect(pci_sbdf_t sbdf);
 int scan_pci_devices(void);
 enum pdev_type pdev_type(u16 seg, u8 bus, u8 devfn);
 int find_upstream_bridge(u16 seg, u8 *bus, u8 *devfn, u8 *secbus);
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
Reviewed-by on v3 1/18
#2 lore raw

Re: [PATCH v3 01/18] pci: Use pci_sbdf_t in pci_device_detect()

Quoted 3 lines · Teddy Astie

Use a single pci_sbdf_t instead of each of its part as individual parameters.

albeit ...

xen/drivers/passthrough/vtd/dmar.c static int __init acpi_parse_dev_scope( · line 393

Quoted 9 lines
xen/drivers/passthrough/vtd/dmar.c +2 -1
@@ -389,7 +389,8 @@ static int __init acpi_parse_dev_scope(
                 printk(VTDPREFIX " endpoint: %pp\n",
                        &PCI_SBDF(seg, bus, path->dev, path->fn));
 
-            if ( drhd && pci_device_detect(seg, bus, path->dev, path->fn) )
+            if ( drhd && pci_device_detect(PCI_SBDF(seg, bus, path->dev,
+                                                    path->fn)) )

... I think this would better be wrapped as

            if ( drhd &&
                 pci_device_detect(PCI_SBDF(seg, bus, path->dev, path->fn)) )

If I end up committing this, I may take the liberty to adjust.

Jan

#3 lore raw

PATCH v3 2/18 pci: Use pci_sbdf_t in pci_add_device()

In reply to a message not in the archive

---
v3: Don't adjust adjacent code.

---
 xen/arch/x86/physdev.c        |  6 +++---
 xen/drivers/passthrough/pci.c | 36 +++++++++++++++++------------------
 xen/drivers/pci/physdev.c     |  3 ++-
 xen/include/xen/pci.h         |  4 ++--
 4 files changed, 24 insertions(+), 25 deletions(-)
xen/arch/x86/physdev.c +3 -3
index 5bbcf3b530..60c02076d6 100644
@@ -472,7 +472,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( copy_from_guest(&manage_pci, arg, 1) != 0 )
             break;
 
-        ret = pci_add_device(0, manage_pci.bus, manage_pci.devfn,
+        ret = pci_add_device(PCI_SBDF(0, manage_pci.bus, manage_pci.devfn),
                              NULL, NUMA_NO_NODE);
         break;
     }
@@ -503,8 +503,8 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         pdev_info.is_virtfn = manage_pci_ext.is_virtfn;
         pdev_info.physfn.bus = manage_pci_ext.physfn.bus;
         pdev_info.physfn.devfn = manage_pci_ext.physfn.devfn;
-        ret = pci_add_device(0, manage_pci_ext.bus,
-                             manage_pci_ext.devfn,
+        ret = pci_add_device(PCI_SBDF(0, manage_pci_ext.bus,
+                                      manage_pci_ext.devfn),
                              &pdev_info, NUMA_NO_NODE);
         break;
     }
xen/drivers/passthrough/pci.c +17 -19
index 0adf573150..3038827e28 100644
@@ -662,12 +662,11 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
     return is64bits ? 2 : 1;
 }
 
-int pci_add_device(u16 seg, u8 bus, u8 devfn,
-                   const struct pci_dev_info *info, nodeid_t node)
+int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info, nodeid_t node)
 {
     struct pci_seg *pseg;
     struct pci_dev *pdev;
-    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
+    unsigned int slot = sbdf.devfn, func = sbdf.fn;
     const char *type;
     int ret;
 
@@ -680,17 +679,17 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
     else
         type = "device";
 
-    ret = xsm_resource_plug_pci(XSM_PRIV, (seg << 16) | (bus << 8) | devfn);
+    ret = xsm_resource_plug_pci(XSM_PRIV, sbdf.sbdf);
     if ( ret )
         return ret;
 
     ret = -ENOMEM;
 
     pcidevs_lock();
-    pseg = alloc_pseg(seg);
+    pseg = alloc_pseg(sbdf.seg);
     if ( !pseg )
         goto out;
-    pdev = alloc_pdev(pseg, bus, devfn);
+    pdev = alloc_pdev(pseg, sbdf.bus, sbdf.devfn);
     if ( !pdev )
         goto out;
 
@@ -701,16 +700,15 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
         pdev->info = *info;
         if ( pdev->info.is_virtfn )
         {
-            struct pci_dev *pf_pdev =
-                pci_get_pdev(NULL, PCI_SBDF(seg, info->physfn.bus,
-                                            info->physfn.devfn));
+            pci_sbdf_t pf_sbdf = PCI_SBDF(sbdf.seg, info->physfn.bus,
+                                          info->physfn.devfn);
+            struct pci_dev *pf_pdev = pci_get_pdev(NULL, pf_sbdf);
 
             if ( !pf_pdev )
             {
                 printk(XENLOG_WARNING
                        "Attempted to add SR-IOV VF %pp without PF %pp\n",
-                       &pdev->sbdf,
-                       &PCI_SBDF(seg, info->physfn.bus, info->physfn.devfn));
+                       &sbdf, &pf_sbdf);
                 free_pdev(pseg, pdev);
                 ret = -ENODEV;
                 goto out;
@@ -729,14 +727,14 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
             if ( !pdev->ext_cfg )
                 printk(XENLOG_WARNING
                        "%pp: VF without extended config space?\n",
-                       &pdev->sbdf);
+                       &sbdf);
         }
     }
 
     if ( !pdev->info.is_virtfn && !pdev->physfn.vf_rlen[0] )
     {
         unsigned int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
-        uint16_t ctrl = pci_conf_read16(pdev->sbdf, pos + PCI_SRIOV_CTRL);
+        uint16_t ctrl = pci_conf_read16(sbdf, pos + PCI_SRIOV_CTRL);
 
         if ( !pos )
             /* Nothing */;
@@ -750,18 +748,18 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
             for ( i = 0; i < PCI_SRIOV_NUM_BARS; )
             {
                 unsigned int idx = pos + PCI_SRIOV_BAR + i * 4;
-                uint32_t bar = pci_conf_read32(pdev->sbdf, idx);
+                uint32_t bar = pci_conf_read32(sbdf, idx);
 
                 if ( (bar & PCI_BASE_ADDRESS_SPACE) ==
                      PCI_BASE_ADDRESS_SPACE_IO )
                 {
                     printk(XENLOG_WARNING
                            "SR-IOV device %pp with vf BAR%u in IO space\n",
-                           &pdev->sbdf, i);
+                           &sbdf, i);
                     ++i;
                     continue;
                 }
-                ret = pci_size_mem_bar(pdev->sbdf, idx, NULL,
+                ret = pci_size_mem_bar(sbdf, idx, NULL,
                                        &pdev->physfn.vf_rlen[i],
                                        PCI_BAR_VF |
                                        ((i == PCI_SRIOV_NUM_BARS - 1) ?
@@ -772,7 +770,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
         }
         else
             printk(XENLOG_WARNING "SR-IOV device %pp has its virtual"
-                   " functions already enabled (%04x)\n", &pdev->sbdf, ctrl);
+                   " functions already enabled (%04x)\n", &sbdf, ctrl);
     }
 
     check_pdev(pdev);
@@ -818,14 +816,14 @@ out:
     pcidevs_unlock();
     if ( !ret )
     {
-        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &pdev->sbdf);
+        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &sbdf);
         while ( pdev->phantom_stride )
         {
             func += pdev->phantom_stride;
             if ( PCI_SLOT(func) )
                 break;
             printk(XENLOG_DEBUG "PCI phantom %pp\n",
-                   &PCI_SBDF(seg, bus, slot, func));
+                   &PCI_SBDF(sbdf.seg, sbdf.bus, slot, func));
         }
     }
     return ret;
xen/drivers/pci/physdev.c +2 -1
index d46501b884..46735a5fc9 100644
@@ -50,7 +50,8 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         }
 #endif
 
-        ret = pci_add_device(add.seg, add.bus, add.devfn, &pdev_info, node);
+        ret = pci_add_device(PCI_SBDF(add.seg, add.bus, add.devfn),
+                             &pdev_info, node);
         break;
     }
 
xen/include/xen/pci.h +2 -2
index ade882caee..26c0c6e449 100644
@@ -229,8 +229,8 @@ void setup_hwdom_pci_devices(struct domain *d,
 int pci_release_devices(struct domain *d);
 int pci_add_segment(u16 seg);
 const unsigned long *pci_get_ro_map(u16 seg);
-int pci_add_device(u16 seg, u8 bus, u8 devfn,
-                   const struct pci_dev_info *info, nodeid_t node);
+int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info,
+                   nodeid_t node);
 int pci_remove_device(u16 seg, u8 bus, u8 devfn);
 int pci_ro_device(int seg, int bus, int devfn);
 int pci_hide_device(unsigned int seg, unsigned int bus, unsigned int devfn);
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#4 lore raw

xen/drivers/passthrough/pci.c unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, · line 665

Quoted 9 lines · Teddy Astie
xen/drivers/passthrough/pci.c +1 -2
@@ -662,12 +662,11 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
     return is64bits ? 2 : 1;
 }
 
-int pci_add_device(u16 seg, u8 bus, u8 devfn,
-                   const struct pci_dev_info *info, nodeid_t node)
+int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info, nodeid_t node)

Nit: Overlong line (like already pointed out for v2).

xen/drivers/passthrough/pci.c unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, · line 669

Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -1
 {
     struct pci_seg *pseg;
     struct pci_dev *pdev;
-    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
+    unsigned int slot = sbdf.devfn, func = sbdf.fn;

Andrew pointed out an issue here for v2, and you addressed only half of his comment.

Quoted 4 lines
xen/drivers/passthrough/pci.c +0 -0
@@ -729,14 +727,14 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
             if ( !pdev->ext_cfg )
                 printk(XENLOG_WARNING
                        "%pp: VF without extended config space?\n",

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 737

Quoted 10 lines
xen/drivers/passthrough/pci.c +2 -2
-                       &pdev->sbdf);
+                       &sbdf);
         }
     }
 
     if ( !pdev->info.is_virtfn && !pdev->physfn.vf_rlen[0] )
     {
         unsigned int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
-        uint16_t ctrl = pci_conf_read16(pdev->sbdf, pos + PCI_SRIOV_CTRL);
+        uint16_t ctrl = pci_conf_read16(sbdf, pos + PCI_SRIOV_CTRL);

Quoting from my v2 reply:

"Are changes like these two actually worthwhile to make? sbdf, being a function
parameter, can be modified in the course of the function. pdev->sbdf, otoh, cannot (for being in a const struct field). If further sbdf, throughout the function, never had its address taken, the compiler may be able to produce better code."

Quoted 4 lines
xen/drivers/passthrough/pci.c +0 -0
@@ -818,14 +816,14 @@ out:
     pcidevs_unlock();
     if ( !ret )
     {

xen/drivers/passthrough/pci.c out: · line 826

Quoted 10 lines
xen/drivers/passthrough/pci.c +2 -2
-        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &pdev->sbdf);
+        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &sbdf);
         while ( pdev->phantom_stride )
         {
             func += pdev->phantom_stride;
             if ( PCI_SLOT(func) )
                 break;
             printk(XENLOG_DEBUG "PCI phantom %pp\n",
-                   &PCI_SBDF(seg, bus, slot, func));
+                   &PCI_SBDF(sbdf.seg, sbdf.bus, slot, func));

My v2 remark here also wasn't addressed. Please can you make sure to address _all_ review comments before sending a new version?

Jan

#5 lore raw

Le 30/06/2026 à 13:44, Jan Beulich a écrit :

Quoted 13 lines · Teddy Astie

xen/drivers/passthrough/pci.c unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, · line 665

Quoted 9 lines
xen/drivers/passthrough/pci.c +1 -2
@@ -662,12 +662,11 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
      return is64bits ? 2 : 1;
  }
 
-int pci_add_device(u16 seg, u8 bus, u8 devfn,
-                   const struct pci_dev_info *info, nodeid_t node)
+int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info, nodeid_t node)

Nit: Overlong line (like already pointed out for v2).

Quoted 9 lines

xen/drivers/passthrough/pci.c unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, · line 669

Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -1
  {
      struct pci_seg *pseg;
      struct pci_dev *pdev;
-    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
+    unsigned int slot = sbdf.devfn, func = sbdf.fn;

Andrew pointed out an issue here for v2, and you addressed only half of his comment.

Ah, I see, I used devfn instead of device.
I guess it would be better to avoid slot vs dev wording as it's easy to get wrong.
In this case, I think it would be even better to drop this variable as it's barely used (only used at the end for display as a alias for sbdf.dev).

Quoted 17 lines
Quoted 4 lines
xen/drivers/passthrough/pci.c +0 -0
@@ -729,14 +727,14 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
              if ( !pdev->ext_cfg )
                  printk(XENLOG_WARNING
                         "%pp: VF without extended config space?\n",

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 737

Quoted 10 lines
xen/drivers/passthrough/pci.c +2 -2
-                       &pdev->sbdf);
+                       &sbdf);
          }
      }
 
      if ( !pdev->info.is_virtfn && !pdev->physfn.vf_rlen[0] )
      {
          unsigned int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
-        uint16_t ctrl = pci_conf_read16(pdev->sbdf, pos + PCI_SRIOV_CTRL);
+        uint16_t ctrl = pci_conf_read16(sbdf, pos + PCI_SRIOV_CTRL);

Quoting from my v2 reply:

Quoted 6 lines

"Are changes like these two actually worthwhile to make? sbdf, being a function
parameter, can be modified in the course of the function. pdev->sbdf, otoh, cannot (for being in a const struct field). If further sbdf, throughout the function, never had its address taken, the compiler may be able to produce better code."

I missed it; though I'm not convinced that would work. While the sbdf field of struct pci_dev is const, the pointer to it isn't, so in the same regard, the compiler can assume that the pdev pointer changes thus can't rely on the const property of the pci_dev::sbdf field.

A better alternative would be to mark the sbdf function param const.

Quoted 18 lines
Quoted 4 lines
xen/drivers/passthrough/pci.c +0 -0
@@ -818,14 +816,14 @@ out:
      pcidevs_unlock();
      if ( !ret )
      {

xen/drivers/passthrough/pci.c out: · line 826

Quoted 10 lines
xen/drivers/passthrough/pci.c +2 -2
-        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &pdev->sbdf);
+        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &sbdf);
          while ( pdev->phantom_stride )
          {
              func += pdev->phantom_stride;
              if ( PCI_SLOT(func) )
                  break;
              printk(XENLOG_DEBUG "PCI phantom %pp\n",
-                   &PCI_SBDF(seg, bus, slot, func));
+                   &PCI_SBDF(sbdf.seg, sbdf.bus, slot, func));

My v2 remark here also wasn't addressed. Please can you make sure to address _all_ review comments before sending a new version?

Quoted 1 line

Jan

Teddy

#6 lore raw
Quoted 20 lines · Teddy Astie

Le 30/06/2026 à 13:44, Jan Beulich a écrit :

Quoted 19 lines · Teddy Astie

xen/drivers/passthrough/pci.c unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos, · line 665

Quoted 9 lines
xen/drivers/passthrough/pci.c +1 -2
@@ -662,12 +662,11 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
      return is64bits ? 2 : 1;
  }
 
-int pci_add_device(u16 seg, u8 bus, u8 devfn,
-                   const struct pci_dev_info *info, nodeid_t node)
+int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info, nodeid_t node)

Nit: Overlong line (like already pointed out for v2).

Quoted 5 lines
xen/drivers/passthrough/pci.c +1 -1
  {
      struct pci_seg *pseg;
      struct pci_dev *pdev;
-    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
+    unsigned int slot = sbdf.devfn, func = sbdf.fn;
Quoted 8 lines
Quoted 2 lines

Andrew pointed out an issue here for v2, and you addressed only half of his comment.

Ah, I see, I used devfn instead of device.
I guess it would be better to avoid slot vs dev wording as it's easy to get wrong.
In this case, I think it would be even better to drop this variable as it's barely used (only used at the end for display as a alias for sbdf.dev).

Indeed (and I think another comment of mine was precisely about that one use).

Quoted 24 lines
Quoted 17 lines
Quoted 4 lines
xen/drivers/passthrough/pci.c +0 -0
@@ -729,14 +727,14 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
              if ( !pdev->ext_cfg )
                  printk(XENLOG_WARNING
                         "%pp: VF without extended config space?\n",

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 737

Quoted 10 lines
xen/drivers/passthrough/pci.c +2 -2
-                       &pdev->sbdf);
+                       &sbdf);
          }
      }
 
      if ( !pdev->info.is_virtfn && !pdev->physfn.vf_rlen[0] )
      {
          unsigned int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
-        uint16_t ctrl = pci_conf_read16(pdev->sbdf, pos + PCI_SRIOV_CTRL);
+        uint16_t ctrl = pci_conf_read16(sbdf, pos + PCI_SRIOV_CTRL);

Quoting from my v2 reply:

Quoted 6 lines

"Are changes like these two actually worthwhile to make? sbdf, being a function
parameter, can be modified in the course of the function. pdev->sbdf, otoh, cannot (for being in a const struct field). If further sbdf, throughout the function, never had its address taken, the compiler may be able to produce better code."

Quoted 4 lines

I missed it; though I'm not convinced that would work. While the sbdf field of struct pci_dev is const, the pointer to it isn't, so in the same regard, the compiler can assume that the pdev pointer changes thus can't rely on the const property of the pci_dev::sbdf field.

If the pointer is a local variable of the function which never has its address taken, the compiler can see all assignments. Hence it can deduce whether (and when) the pointer potentially changes.

Quoted 1 line

A better alternative would be to mark the sbdf function param const.

Not really, we don't normally use const this way.

Jan

#7 lore raw

PATCH 2/18 pci: Use pci_sbdf_t in pci_add_device()

In reply to Teddy Astie

---
v3: Don't adjust adjacent code.
v3.1: Remove slot variable and move func down to its user.

---
 xen/arch/x86/physdev.c        |  6 +++---
 xen/drivers/passthrough/pci.c | 37 +++++++++++++++++------------------
 xen/drivers/pci/physdev.c     |  3 ++-
 xen/include/xen/pci.h         |  4 ++--
 4 files changed, 25 insertions(+), 25 deletions(-)
xen/arch/x86/physdev.c +3 -3
index 5bbcf3b530..60c02076d6 100644
@@ -472,7 +472,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( copy_from_guest(&manage_pci, arg, 1) != 0 )
             break;
 
-        ret = pci_add_device(0, manage_pci.bus, manage_pci.devfn,
+        ret = pci_add_device(PCI_SBDF(0, manage_pci.bus, manage_pci.devfn),
                              NULL, NUMA_NO_NODE);
         break;
     }
@@ -503,8 +503,8 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         pdev_info.is_virtfn = manage_pci_ext.is_virtfn;
         pdev_info.physfn.bus = manage_pci_ext.physfn.bus;
         pdev_info.physfn.devfn = manage_pci_ext.physfn.devfn;
-        ret = pci_add_device(0, manage_pci_ext.bus,
-                             manage_pci_ext.devfn,
+        ret = pci_add_device(PCI_SBDF(0, manage_pci_ext.bus,
+                                      manage_pci_ext.devfn),
                              &pdev_info, NUMA_NO_NODE);
         break;
     }
xen/drivers/passthrough/pci.c +18 -19
index 0adf573150..d7e60e6fbb 100644
@@ -662,12 +662,10 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
     return is64bits ? 2 : 1;
 }
 
-int pci_add_device(u16 seg, u8 bus, u8 devfn,
-                   const struct pci_dev_info *info, nodeid_t node)
+int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info, nodeid_t node)
 {
     struct pci_seg *pseg;
     struct pci_dev *pdev;
-    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
     const char *type;
     int ret;
 
@@ -680,17 +678,17 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
     else
         type = "device";
 
-    ret = xsm_resource_plug_pci(XSM_PRIV, (seg << 16) | (bus << 8) | devfn);
+    ret = xsm_resource_plug_pci(XSM_PRIV, sbdf.sbdf);
     if ( ret )
         return ret;
 
     ret = -ENOMEM;
 
     pcidevs_lock();
-    pseg = alloc_pseg(seg);
+    pseg = alloc_pseg(sbdf.seg);
     if ( !pseg )
         goto out;
-    pdev = alloc_pdev(pseg, bus, devfn);
+    pdev = alloc_pdev(pseg, sbdf.bus, sbdf.devfn);
     if ( !pdev )
         goto out;
 
@@ -701,16 +699,15 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
         pdev->info = *info;
         if ( pdev->info.is_virtfn )
         {
-            struct pci_dev *pf_pdev =
-                pci_get_pdev(NULL, PCI_SBDF(seg, info->physfn.bus,
-                                            info->physfn.devfn));
+            pci_sbdf_t pf_sbdf = PCI_SBDF(sbdf.seg, info->physfn.bus,
+                                          info->physfn.devfn);
+            struct pci_dev *pf_pdev = pci_get_pdev(NULL, pf_sbdf);
 
             if ( !pf_pdev )
             {
                 printk(XENLOG_WARNING
                        "Attempted to add SR-IOV VF %pp without PF %pp\n",
-                       &pdev->sbdf,
-                       &PCI_SBDF(seg, info->physfn.bus, info->physfn.devfn));
+                       &sbdf, &pf_sbdf);
                 free_pdev(pseg, pdev);
                 ret = -ENODEV;
                 goto out;
@@ -729,14 +726,14 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
             if ( !pdev->ext_cfg )
                 printk(XENLOG_WARNING
                        "%pp: VF without extended config space?\n",
-                       &pdev->sbdf);
+                       &sbdf);
         }
     }
 
     if ( !pdev->info.is_virtfn && !pdev->physfn.vf_rlen[0] )
     {
         unsigned int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
-        uint16_t ctrl = pci_conf_read16(pdev->sbdf, pos + PCI_SRIOV_CTRL);
+        uint16_t ctrl = pci_conf_read16(sbdf, pos + PCI_SRIOV_CTRL);
 
         if ( !pos )
             /* Nothing */;
@@ -750,18 +747,18 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
             for ( i = 0; i < PCI_SRIOV_NUM_BARS; )
             {
                 unsigned int idx = pos + PCI_SRIOV_BAR + i * 4;
-                uint32_t bar = pci_conf_read32(pdev->sbdf, idx);
+                uint32_t bar = pci_conf_read32(sbdf, idx);
 
                 if ( (bar & PCI_BASE_ADDRESS_SPACE) ==
                      PCI_BASE_ADDRESS_SPACE_IO )
                 {
                     printk(XENLOG_WARNING
                            "SR-IOV device %pp with vf BAR%u in IO space\n",
-                           &pdev->sbdf, i);
+                           &sbdf, i);
                     ++i;
                     continue;
                 }
-                ret = pci_size_mem_bar(pdev->sbdf, idx, NULL,
+                ret = pci_size_mem_bar(sbdf, idx, NULL,
                                        &pdev->physfn.vf_rlen[i],
                                        PCI_BAR_VF |
                                        ((i == PCI_SRIOV_NUM_BARS - 1) ?
@@ -772,7 +769,7 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
         }
         else
             printk(XENLOG_WARNING "SR-IOV device %pp has its virtual"
-                   " functions already enabled (%04x)\n", &pdev->sbdf, ctrl);
+                   " functions already enabled (%04x)\n", &sbdf, ctrl);
     }
 
     check_pdev(pdev);
@@ -818,14 +815,16 @@ out:
     pcidevs_unlock();
     if ( !ret )
     {
-        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &pdev->sbdf);
+        unsigned int func = sbdf.fn;
+
+        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &sbdf);
         while ( pdev->phantom_stride )
         {
             func += pdev->phantom_stride;
             if ( PCI_SLOT(func) )
                 break;
             printk(XENLOG_DEBUG "PCI phantom %pp\n",
-                   &PCI_SBDF(seg, bus, slot, func));
+                   &PCI_SBDF(sbdf.seg, sbdf.bus, sbdf.dev, func));
         }
     }
     return ret;
xen/drivers/pci/physdev.c +2 -1
index d46501b884..46735a5fc9 100644
@@ -50,7 +50,8 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         }
 #endif
 
-        ret = pci_add_device(add.seg, add.bus, add.devfn, &pdev_info, node);
+        ret = pci_add_device(PCI_SBDF(add.seg, add.bus, add.devfn),
+                             &pdev_info, node);
         break;
     }
 
xen/include/xen/pci.h +2 -2
index ade882caee..26c0c6e449 100644
@@ -229,8 +229,8 @@ void setup_hwdom_pci_devices(struct domain *d,
 int pci_release_devices(struct domain *d);
 int pci_add_segment(u16 seg);
 const unsigned long *pci_get_ro_map(u16 seg);
-int pci_add_device(u16 seg, u8 bus, u8 devfn,
-                   const struct pci_dev_info *info, nodeid_t node);
+int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info,
+                   nodeid_t node);
 int pci_remove_device(u16 seg, u8 bus, u8 devfn);
 int pci_ro_device(int seg, int bus, int devfn);
 int pci_hide_device(unsigned int seg, unsigned int bus, unsigned int devfn);
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#8 lore raw
Quoted 4 lines · Teddy Astie
xen/drivers/passthrough/pci.c +0 -0
@@ -729,14 +726,14 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
             if ( !pdev->ext_cfg )
                 printk(XENLOG_WARNING
                        "%pp: VF without extended config space?\n",

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 736

Quoted 10 lines
xen/drivers/passthrough/pci.c +2 -2
-                       &pdev->sbdf);
+                       &sbdf);
         }
     }
 
     if ( !pdev->info.is_virtfn && !pdev->physfn.vf_rlen[0] )
     {
         unsigned int pos = pci_find_ext_capability(pdev, PCI_EXT_CAP_ID_SRIOV);
-        uint16_t ctrl = pci_conf_read16(pdev->sbdf, pos + PCI_SRIOV_CTRL);
+        uint16_t ctrl = pci_conf_read16(sbdf, pos + PCI_SRIOV_CTRL);

There continue to be unnecessary changes here, like these two. Even if only to limit churn, such changes imo better wouldn't be making unless they offered a clear gain.

Quoted 9 lines
xen/drivers/passthrough/pci.c +3 -1
@@ -818,14 +815,16 @@ out:
     pcidevs_unlock();
     if ( !ret )
     {
-        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &pdev->sbdf);
+        unsigned int func = sbdf.fn;
+
+        printk(XENLOG_DEBUG "PCI add %s %pp\n", type, &sbdf);
         while ( pdev->phantom_stride )

xen/drivers/passthrough/pci.c out: · line 830

Quoted 10 lines
xen/drivers/passthrough/pci.c +1 -1
         {
             func += pdev->phantom_stride;
             if ( PCI_SLOT(func) )
                 break;
             printk(XENLOG_DEBUG "PCI phantom %pp\n",
-                   &PCI_SBDF(seg, bus, slot, func));
+                   &PCI_SBDF(sbdf.seg, sbdf.bus, sbdf.dev, func));
         }
     }
     return ret;

Is there a reason you retain "func" as a variable? As sbdf isn't used any further, you could fiddle directly with sbfd.fn. (Then again I realize some helper variable may be needed anyway, to be able to detect the field having wrapped.)

Jan

Acked-by on v3 2/18
#9 lore raw
In reply to Teddy Astie
Quoted 54 lines · Teddy Astie

---
v3: Don't adjust adjacent code.

---
  xen/arch/x86/physdev.c        |  6 +++---
  xen/drivers/passthrough/pci.c | 36 +++++++++++++++++------------------
  xen/drivers/pci/physdev.c     |  3 ++-
  xen/include/xen/pci.h         |  4 ++--
  4 files changed, 24 insertions(+), 25 deletions(-)
xen/arch/x86/physdev.c +3 -3
index 5bbcf3b530..60c02076d6 100644
@@ -472,7 +472,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
          if ( copy_from_guest(&manage_pci, arg, 1) != 0 )
              break;
 
-        ret = pci_add_device(0, manage_pci.bus, manage_pci.devfn,
+        ret = pci_add_device(PCI_SBDF(0, manage_pci.bus, manage_pci.devfn),
                               NULL, NUMA_NO_NODE);
          break;
      }
@@ -503,8 +503,8 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
          pdev_info.is_virtfn = manage_pci_ext.is_virtfn;
          pdev_info.physfn.bus = manage_pci_ext.physfn.bus;
          pdev_info.physfn.devfn = manage_pci_ext.physfn.devfn;
-        ret = pci_add_device(0, manage_pci_ext.bus,
-                             manage_pci_ext.devfn,
+        ret = pci_add_device(PCI_SBDF(0, manage_pci_ext.bus,
+                                      manage_pci_ext.devfn),
                               &pdev_info, NUMA_NO_NODE);
          break;
      }
xen/drivers/passthrough/pci.c +2 -3
index 0adf573150..3038827e28 100644
@@ -662,12 +662,11 @@ unsigned int pci_size_mem_bar(pci_sbdf_t sbdf, unsigned int pos,
      return is64bits ? 2 : 1;
  }
 
-int pci_add_device(u16 seg, u8 bus, u8 devfn,
-                   const struct pci_dev_info *info, nodeid_t node)
+int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info, nodeid_t node)
  {
      struct pci_seg *pseg;
      struct pci_dev *pdev;
-    unsigned int slot = PCI_SLOT(devfn), func = PCI_FUNC(devfn);
+    unsigned int slot = sbdf.devfn, func = sbdf.fn;
      const char *type;
      int ret;
 

xen/drivers/passthrough/pci.c int pci_add_device(u16 seg, u8 bus, u8 devfn, · line 684

Quoted 7 lines
xen/drivers/passthrough/pci.c +1 -1
@@ -680,17 +679,17 @@ int pci_add_device(u16 seg, u8 bus, u8 devfn,
      else
          type = "device";
 
-    ret = xsm_resource_plug_pci(XSM_PRIV, (seg << 16) | (bus << 8) | devfn);
+    ret = xsm_resource_plug_pci(XSM_PRIV, sbdf.sbdf);
      if ( ret )
          return ret;
#10 lore raw

PATCH v3 3/18 pci: Use pci_sbdf_t in pci_prepare_msix()

In reply to a message not in the archive

Also take the opportunity to clean up a bit PHYSDEVOP_{prepare,release}_msix.

---
v3: Reorganize PHYSDEVOP_{prepare,release}_msix

---
 xen/arch/x86/include/asm/msi.h |  2 +-
 xen/arch/x86/msi.c             |  4 ++--
 xen/arch/x86/physdev.c         | 18 +++++++++++-------
 3 files changed, 14 insertions(+), 10 deletions(-)
xen/arch/x86/include/asm/msi.h +1 -1
index 00059d4a3a..6fb663b2e7 100644
@@ -83,7 +83,7 @@ struct msi_desc;
 extern int pci_enable_msi(struct pci_dev *pdev, struct msi_info *msi,
                           struct msi_desc **desc);
 extern void pci_disable_msi(struct msi_desc *msi_desc);
-extern int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool off);
+extern int pci_prepare_msix(pci_sbdf_t sbdf, bool off);
 extern void pci_cleanup_msi(struct pci_dev *pdev);
 extern void pci_disable_msi_all(void);
 extern int setup_msi_irq(struct irq_desc *desc, struct msi_desc *msidesc);
xen/arch/x86/msi.c +2 -2
index 3fe22b3b6d..8831ce068c 100644
@@ -1153,7 +1153,7 @@ static void __pci_disable_msix(struct msi_desc *entry)
     _pci_cleanup_msix(dev->msix);
 }
 
-int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool off)
+int pci_prepare_msix(pci_sbdf_t sbdf, bool off)
 {
     int rc;
     struct pci_dev *pdev;
@@ -1162,7 +1162,7 @@ int pci_prepare_msix(u16 seg, u8 bus, u8 devfn, bool off)
         return 0;
 
     pcidevs_lock();
-    pdev = pci_get_pdev(NULL, PCI_SBDF(seg, bus, devfn));
+    pdev = pci_get_pdev(NULL, sbdf);
     if ( !pdev )
         rc = -ENODEV;
     else if ( pdev->msix->used_entries != !!off )
xen/arch/x86/physdev.c +11 -7
index 60c02076d6..9f5a9f2dc8 100644
@@ -512,15 +512,19 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_prepare_msix:
     case PHYSDEVOP_release_msix: {
         struct physdev_pci_device dev;
+        pci_sbdf_t sbdf;
 
+        ret = -EFAULT;
         if ( copy_from_guest(&dev, arg, 1) )
-            ret = -EFAULT;
-        else
-            ret = xsm_resource_setup_pci(XSM_PRIV,
-                                         (dev.seg << 16) | (dev.bus << 8) |
-                                         dev.devfn) ?:
-                  pci_prepare_msix(dev.seg, dev.bus, dev.devfn,
-                                   cmd != PHYSDEVOP_prepare_msix);
+            break;
+        
+        sbdf = PCI_SBDF(dev.seg, dev.bus, dev.devfn);
+
+        ret = xsm_resource_setup_pci(XSM_PRIV, sbdf.sbdf);
+        if ( ret )
+            break;
+
+        ret = pci_prepare_msix(sbdf, cmd != PHYSDEVOP_prepare_msix);
         break;
     }
 
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
Acked-by on v3 3/18
#11 lore raw

Re: [PATCH v3 03/18] pci: Use pci_sbdf_t in pci_prepare_msix()

Quoted 19 lines · Teddy Astie
xen/arch/x86/physdev.c +4 -7
@@ -512,15 +512,19 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     case PHYSDEVOP_prepare_msix:
     case PHYSDEVOP_release_msix: {
         struct physdev_pci_device dev;
+        pci_sbdf_t sbdf;
 
+        ret = -EFAULT;
         if ( copy_from_guest(&dev, arg, 1) )
-            ret = -EFAULT;
-        else
-            ret = xsm_resource_setup_pci(XSM_PRIV,
-                                         (dev.seg << 16) | (dev.bus << 8) |
-                                         dev.devfn) ?:
-                  pci_prepare_msix(dev.seg, dev.bus, dev.devfn,
-                                   cmd != PHYSDEVOP_prepare_msix);
+            break;
+        

xen/arch/x86/physdev.c ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg) · line 530

Quoted 10 lines
xen/arch/x86/physdev.c +7 -0
+        sbdf = PCI_SBDF(dev.seg, dev.bus, dev.devfn);
+
+        ret = xsm_resource_setup_pci(XSM_PRIV, sbdf.sbdf);
+        if ( ret )
+            break;
+
+        ret = pci_prepare_msix(sbdf, cmd != PHYSDEVOP_prepare_msix);
         break;
     }
 

Okay, here you took Andrew's suggestion verbatim, ignoring my reply altogether. Minimally what I'd have expected in response (without - imo - violating Andrew's constraint) is

        ret = xsm_resource_setup_pci(XSM_PRIV, sbdf.sbdf);
        if ( !ret )
            ret = pci_prepare_msix(sbdf, cmd != PHYSDEVOP_prepare_msix);

Preferably with that:

Jan

#12 lore raw

PATCH v3 4/18 pci: Use pci_sbdf_t in pci_remove_device()

In reply to a message not in the archive

No functional change intended.

---
 xen/arch/x86/physdev.c        | 2 +-
 xen/drivers/passthrough/pci.c | 8 ++++----
 xen/drivers/pci/physdev.c     | 2 +-
 xen/include/xen/pci.h         | 2 +-
 4 files changed, 7 insertions(+), 7 deletions(-)
xen/arch/x86/physdev.c +1 -1
index 9f5a9f2dc8..712d04c41a 100644
@@ -483,7 +483,7 @@ ret_t do_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( copy_from_guest(&manage_pci, arg, 1) != 0 )
             break;
 
-        ret = pci_remove_device(0, manage_pci.bus, manage_pci.devfn);
+        ret = pci_remove_device(PCI_SBDF(0, manage_pci.bus, manage_pci.devfn));
         break;
     }
 
xen/drivers/passthrough/pci.c +4 -4
index 3038827e28..145acffd99 100644
@@ -829,13 +829,13 @@ out:
     return ret;
 }
 
-int pci_remove_device(u16 seg, u8 bus, u8 devfn)
+int pci_remove_device(pci_sbdf_t sbdf)
 {
-    struct pci_seg *pseg = get_pseg(seg);
+    struct pci_seg *pseg = get_pseg(sbdf.seg);
     struct pci_dev *pdev;
     int ret;
 
-    ret = xsm_resource_unplug_pci(XSM_PRIV, (seg << 16) | (bus << 8) | devfn);
+    ret = xsm_resource_unplug_pci(XSM_PRIV, sbdf.sbdf);
     if ( ret )
         return ret;
 
@@ -846,7 +846,7 @@ int pci_remove_device(u16 seg, u8 bus, u8 devfn)
 
     pcidevs_lock();
     list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list )
-        if ( pdev->bus == bus && pdev->devfn == devfn )
+        if ( pdev->sbdf.bdf == sbdf.bdf )
         {
             if ( !pdev->info.is_virtfn && !list_empty(&pdev->vf_list) )
             {
xen/drivers/pci/physdev.c +1 -1
index 46735a5fc9..374b3ee17e 100644
@@ -65,7 +65,7 @@ ret_t pci_physdev_op(int cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
         if ( copy_from_guest(&dev, arg, 1) != 0 )
             break;
 
-        ret = pci_remove_device(dev.seg, dev.bus, dev.devfn);
+        ret = pci_remove_device(PCI_SBDF(dev.seg, dev.bus, dev.devfn));
         break;
     }
 
xen/include/xen/pci.h +1 -1
index 26c0c6e449..77a1362571 100644
@@ -231,7 +231,7 @@ int pci_add_segment(u16 seg);
 const unsigned long *pci_get_ro_map(u16 seg);
 int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info,
                    nodeid_t node);
-int pci_remove_device(u16 seg, u8 bus, u8 devfn);
+int pci_remove_device(pci_sbdf_t sbdf);
 int pci_ro_device(int seg, int bus, int devfn);
 int pci_hide_device(unsigned int seg, unsigned int bus, unsigned int devfn);
 struct pci_dev *pci_get_pdev(const struct domain *d, pci_sbdf_t sbdf);
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#13 lore raw

PATCH v3 5/18 pci: Use pci_sbdf_t in pci_ro_device()

In reply to a message not in the archive

That has the indirect effect of properly considering segments other than 0 in xhci-dbc.

---
 xen/drivers/char/ns16550.c                 | 3 +--
 xen/drivers/char/xhci-dbc.c                | 4 ++--
 xen/drivers/passthrough/amd/iommu_detect.c | 7 +------
 xen/drivers/passthrough/pci.c              | 8 ++++----
 xen/include/xen/pci.h                      | 2 +-
 5 files changed, 9 insertions(+), 15 deletions(-)
xen/drivers/char/ns16550.c +1 -2
index 878da27f2e..a3d6c46a73 100644
@@ -450,8 +450,7 @@ static void __init cf_check ns16550_init_postirq(struct serial_port *port)
                                 PFN_UP(uart->io_base + uart->io_size) - 1) )
             printk(XENLOG_INFO "Error while adding MMIO range of device to mmio_ro_ranges\n");
 
-        if ( pci_ro_device(uart->pci_device.seg, uart->pci_device.bus,
-                           uart->pci_device.devfn) )
+        if ( pci_ro_device(uart->pci_device) )
             printk(XENLOG_INFO
                    "Could not mark config space of %pp read-only.\n",
                    &uart->pci_device);
xen/drivers/char/xhci-dbc.c +2 -2
index c1ff528de6..7a56dca6dd 100644
@@ -1196,7 +1196,7 @@ static void __init cf_check dbc_uart_init_postirq(struct serial_port *port)
     switch ( uart->dbc.share )
     {
     case XHCI_SHARE_NONE:
-        if ( pci_ro_device(0, uart->dbc.sbdf.bus, uart->dbc.sbdf.devfn) )
+        if ( pci_ro_device(uart->dbc.sbdf) )
             printk(XENLOG_WARNING
                    "Failed to mark read-only %pp used for XHCI console\n",
                    &uart->dbc.sbdf);
@@ -1221,7 +1221,7 @@ static void __init cf_check dbc_uart_init_postirq(struct serial_port *port)
                "Error while marking MMIO range of XHCI console as R/O, "
                "making the whole device R/O (share=no)\n");
         uart->dbc.share = XHCI_SHARE_NONE;
-        if ( pci_ro_device(0, uart->dbc.sbdf.bus, uart->dbc.sbdf.devfn) )
+        if ( pci_ro_device(uart->dbc.sbdf) )
             printk(XENLOG_WARNING
                    "Failed to mark read-only %pp used for XHCI console\n",
                    &uart->dbc.sbdf);
xen/drivers/passthrough/amd/iommu_detect.c +1 -6
index 94ee297424..df75d6fbc0 100644
@@ -136,7 +136,6 @@ int __init amd_iommu_detect_one_acpi(
     const struct acpi_ivrs_hardware *ivhd_block)
 {
     struct amd_iommu *iommu;
-    u8 bus, dev, func;
     int rt = 0;
 
     if ( ivhd_block->header.length < sizeof(*ivhd_block) )
@@ -210,10 +209,6 @@ int __init amd_iommu_detect_one_acpi(
     /* override IOMMU HT flags */
     iommu->ht_flags = ivhd_block->header.flags;
 
-    bus = PCI_BUS(iommu->sbdf.bdf);
-    dev = PCI_SLOT(iommu->sbdf.bdf);
-    func = PCI_FUNC(iommu->sbdf.bdf);
-
     rt = get_iommu_capabilities(iommu->sbdf, iommu->cap_offset, iommu);
     if ( rt )
         goto out;
@@ -227,7 +222,7 @@ int __init amd_iommu_detect_one_acpi(
     if ( !iommu->domid_map )
         goto out;
 
-    rt = pci_ro_device(iommu->sbdf.seg, bus, PCI_DEVFN(dev, func));
+    rt = pci_ro_device(iommu->sbdf);
     if ( rt )
         printk(XENLOG_ERR "Could not mark config space of %pp read-only (%d)\n",
                &iommu->sbdf, rt);
xen/drivers/passthrough/pci.c +4 -4
index 145acffd99..e4357b84a4 100644
@@ -499,14 +499,14 @@ int __init pci_hide_device(unsigned int seg, unsigned int bus,
     return rc;
 }
 
-int __init pci_ro_device(int seg, int bus, int devfn)
+int __init pci_ro_device(pci_sbdf_t sbdf)
 {
-    struct pci_seg *pseg = alloc_pseg(seg);
+    struct pci_seg *pseg = alloc_pseg(sbdf.seg);
     struct pci_dev *pdev;
 
     if ( !pseg )
         return -ENOMEM;
-    pdev = alloc_pdev(pseg, bus, devfn);
+    pdev = alloc_pdev(pseg, sbdf.bus, sbdf.devfn);
     if ( !pdev )
         return -ENOMEM;
 
@@ -520,7 +520,7 @@ int __init pci_ro_device(int seg, int bus, int devfn)
         memset(pseg->ro_map, 0, sz);
     }
 
-    __set_bit(PCI_BDF(bus, devfn), pseg->ro_map);
+    __set_bit(sbdf.bdf, pseg->ro_map);
     _pci_hide_device(pdev);
 
     return 0;
xen/include/xen/pci.h +1 -1
index 77a1362571..d038217607 100644
@@ -232,7 +232,7 @@ const unsigned long *pci_get_ro_map(u16 seg);
 int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info,
                    nodeid_t node);
 int pci_remove_device(pci_sbdf_t sbdf);
-int pci_ro_device(int seg, int bus, int devfn);
+int pci_ro_device(pci_sbdf_t sbdf);
 int pci_hide_device(unsigned int seg, unsigned int bus, unsigned int devfn);
 struct pci_dev *pci_get_pdev(const struct domain *d, pci_sbdf_t sbdf);
 struct pci_dev *pci_get_real_pdev(pci_sbdf_t sbdf);
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#14 lore raw

PATCH v3 6/18 pci: Use pci_sbdf_t in pci_hide_device()

In reply to a message not in the archive

No functional change intended.

---
 xen/drivers/char/ehci-dbgp.c  | 2 +-
 xen/drivers/char/xhci-dbc.c   | 2 +-
 xen/drivers/passthrough/pci.c | 7 +++----
 xen/drivers/video/vga.c       | 2 +-
 xen/include/xen/pci.h         | 2 +-
 5 files changed, 7 insertions(+), 8 deletions(-)
xen/drivers/char/ehci-dbgp.c +1 -1
index 2a58213cab..ab1e3c6de4 100644
@@ -1376,7 +1376,7 @@ static void __init cf_check ehci_dbgp_init_postirq(struct serial_port *port)
 
     ehci_dbgp_setup_postirq(dbgp);
 
-    pci_hide_device(0, dbgp->bus, PCI_DEVFN(dbgp->slot, dbgp->func));
+    pci_hide_device(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func));
 }
 
 static int ehci_dbgp_check_release(struct ehci_dbgp *dbgp)
xen/drivers/char/xhci-dbc.c +1 -1
index 7a56dca6dd..3f41aa7022 100644
@@ -1202,7 +1202,7 @@ static void __init cf_check dbc_uart_init_postirq(struct serial_port *port)
                    &uart->dbc.sbdf);
         break;
     case XHCI_SHARE_HWDOM:
-        if ( pci_hide_device(0, uart->dbc.sbdf.bus, uart->dbc.sbdf.devfn) )
+        if ( pci_hide_device(uart->dbc.sbdf) )
             printk(XENLOG_WARNING
                    "Failed to hide %pp used for XHCI console\n",
                    &uart->dbc.sbdf);
xen/drivers/passthrough/pci.c +3 -4
index e4357b84a4..8c574926cd 100644
@@ -476,18 +476,17 @@ static void __init _pci_hide_device(struct pci_dev *pdev)
     write_unlock(&dom_xen->pci_lock);
 }
 
-int __init pci_hide_device(unsigned int seg, unsigned int bus,
-                           unsigned int devfn)
+int __init pci_hide_device(pci_sbdf_t sbdf)
 {
     struct pci_dev *pdev;
     struct pci_seg *pseg;
     int rc = -ENOMEM;
 
     pcidevs_lock();
-    pseg = alloc_pseg(seg);
+    pseg = alloc_pseg(sbdf.seg);
     if ( pseg )
     {
-        pdev = alloc_pdev(pseg, bus, devfn);
+        pdev = alloc_pdev(pseg, sbdf.bus, sbdf.devfn);
         if ( pdev )
         {
             _pci_hide_device(pdev);
xen/drivers/video/vga.c +1 -1
index b577b24619..b2b9c3680a 100644
@@ -147,7 +147,7 @@ void __init video_endboot(void)
                 {
                     printk(XENLOG_INFO "Boot video device %02x:%02x.%u\n",
                            bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
-                    pci_hide_device(0, bus, devfn);
+                    pci_hide_device(PCI_SBDF(0, bus, devfn));
                 }
             }
     }
xen/include/xen/pci.h +1 -1
index d038217607..03ae389cb7 100644
@@ -233,7 +233,7 @@ int pci_add_device(pci_sbdf_t sbdf, const struct pci_dev_info *info,
                    nodeid_t node);
 int pci_remove_device(pci_sbdf_t sbdf);
 int pci_ro_device(pci_sbdf_t sbdf);
-int pci_hide_device(unsigned int seg, unsigned int bus, unsigned int devfn);
+int pci_hide_device(pci_sbdf_t sbdf);
 struct pci_dev *pci_get_pdev(const struct domain *d, pci_sbdf_t sbdf);
 struct pci_dev *pci_get_real_pdev(pci_sbdf_t sbdf);
 void pci_check_disable_device(u16 seg, u8 bus, u8 devfn);
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#15 lore raw

PATCH v3 7/18 pci: Use pci_sbdf_t pci_check_disable_device()

In reply to a message not in the archive

No functional change intended.

---
 xen/drivers/passthrough/amd/iommu_init.c | 3 +--
 xen/drivers/passthrough/pci.c            | 4 ++--
 xen/drivers/passthrough/vtd/iommu.c      | 3 +--
 xen/include/xen/pci.h                    | 2 +-
 4 files changed, 5 insertions(+), 7 deletions(-)
xen/drivers/passthrough/amd/iommu_init.c +1 -2
index e0c8925c33..c2d7201747 100644
@@ -575,8 +575,7 @@ static void cf_check parse_event_log_entry(struct amd_iommu *iommu, u32 entry[])
 
         for ( bdf = 0; bdf < ivrs_bdf_entries; bdf++ )
             if ( get_dma_requestor_id(iommu->sbdf.seg, bdf) == device_id )
-                pci_check_disable_device(iommu->sbdf.seg, PCI_BUS(bdf),
-                                         PCI_DEVFN(bdf));
+                pci_check_disable_device(PCI_SBDF(iommu->sbdf.seg, bdf));
     }
     else
         printk(XENLOG_ERR "%s %08x %08x %08x %08x\n",
xen/drivers/passthrough/pci.c +2 -2
index 8c574926cd..e8c9dd9b26 100644
@@ -1175,14 +1175,14 @@ bool __init pci_device_detect(pci_sbdf_t sbdf)
     return 1;
 }
 
-void pci_check_disable_device(u16 seg, u8 bus, u8 devfn)
+void pci_check_disable_device(pci_sbdf_t sbdf)
 {
     struct pci_dev *pdev;
     s_time_t now = NOW();
     u16 cword;
 
     pcidevs_lock();
-    pdev = pci_get_real_pdev(PCI_SBDF(seg, bus, devfn));
+    pdev = pci_get_real_pdev(sbdf);
     if ( pdev )
     {
         if ( now < pdev->fault.time ||
xen/drivers/passthrough/vtd/iommu.c +1 -2
index 3d6f787b3c..ee848a6d7e 100644
@@ -1072,8 +1072,7 @@ static void __do_iommu_page_fault(struct vtd_iommu *iommu)
         iommu_page_fault_do_one(iommu, type, fault_reason,
                                 source_id, guest_addr);
 
-        pci_check_disable_device(iommu->drhd->segment,
-                                 PCI_BUS(source_id), PCI_DEVFN(source_id));
+        pci_check_disable_device(PCI_SBDF(iommu->drhd->segment, source_id));
 
         fault_index++;
         if ( fault_index > cap_num_fault_regs(iommu->cap) )
xen/include/xen/pci.h +1 -1
index 03ae389cb7..ed6c95452f 100644
@@ -236,7 +236,7 @@ int pci_ro_device(pci_sbdf_t sbdf);
 int pci_hide_device(pci_sbdf_t sbdf);
 struct pci_dev *pci_get_pdev(const struct domain *d, pci_sbdf_t sbdf);
 struct pci_dev *pci_get_real_pdev(pci_sbdf_t sbdf);
-void pci_check_disable_device(u16 seg, u8 bus, u8 devfn);
+void pci_check_disable_device(pci_sbdf_t sbdf);
 
 /*
  * Iterate without locking or preemption over all PCI devices known by Xen.
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#16 lore raw

PATCH v3 8/18 pci: Introduce parse_pci_sbdf{_seg}()

In reply to a message not in the archive

In many places, we're parsing a PCI string into individual parts (seg, bus, dev, fn) and then transform it into a pci_sbdf_t using PCI_SBDF macro. Rather than converting from parts to pci_sbdf_t and vice versa, introduce a new function that parses a PCI string into a pci_sbdf_t structure directly.

---
v3:
* Address Andrew's changes (add __init)
* Use intermediate parse_pci_split_*().

---
 xen/drivers/pci/pci.c | 22 ++++++++++++++++++++--
 xen/include/xen/pci.h | 20 ++++++++++++++++++--
 2 files changed, 38 insertions(+), 4 deletions(-)
xen/drivers/pci/pci.c +20 -2
index 084be3880c..743e2fd263 100644
@@ -151,7 +151,7 @@ void pci_intx(const struct pci_dev *pdev, bool enable)
     pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
 }
 
-const char *__init parse_pci(const char *s, unsigned int *seg_p,
+const char *__init parse_pci_split(const char *s, unsigned int *seg_p,
                              unsigned int *bus_p, unsigned int *dev_p,
                              unsigned int *func_p)
 {
@@ -160,7 +160,7 @@ const char *__init parse_pci(const char *s, unsigned int *seg_p,
     return parse_pci_seg(s, seg_p, bus_p, dev_p, func_p, &def_seg);
 }
 
-const char *__init parse_pci_seg(const char *s, unsigned int *seg_p,
+const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p,
                                  unsigned int *bus_p, unsigned int *dev_p,
                                  unsigned int *func_p, bool *def_seg)
 {
@@ -202,3 +202,21 @@ const char *__init parse_pci_seg(const char *s, unsigned int *seg_p,
 
     return s;
 }
+
+const char *__init parse_pci_sbdf(const char *s, pci_sbdf_t *sbdf)
+{
+    unsigned int seg, bus, dev, func;
+    const char *out = parse_pci(s, &seg, &bus, &dev, &func);
+
+    *sbdf = PCI_SBDF(seg, bus, dev, func);
+    return out;
+}
+
+const char *__init parse_pci_sbdf_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg)
+{
+    unsigned int seg, bus, dev, func;
+    const char *out = parse_pci_seg(s, &seg, &bus, &dev, &func, def_seg);
+
+    *sbdf = PCI_SBDF(seg, bus, dev, func);
+    return out;
+}
xen/include/xen/pci.h +18 -2
index ed6c95452f..8e2f8a1ab7 100644
@@ -274,12 +274,28 @@ unsigned int pci_find_ext_capability(const struct pci_dev *pdev,
 unsigned int pci_find_next_ext_capability(const struct pci_dev *pdev,
                                           unsigned int start,
                                           unsigned int cap);
-const char *parse_pci(const char *s, unsigned int *seg_p, unsigned int *bus_p,
+const char *parse_pci_split(const char *s, unsigned int *seg_p, unsigned int *bus_p,
                       unsigned int *dev_p, unsigned int *func_p);
-const char *parse_pci_seg(const char *s, unsigned int *seg_p,
+const char *parse_pci_split_seg(const char *s, unsigned int *seg_p,
                           unsigned int *bus_p, unsigned int *dev_p,
                           unsigned int *func_p, bool *def_seg);
 
+const char *parse_pci_sbdf(const char *s, pci_sbdf_t *sbdf);
+const char *parse_pci_sbdf_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg);
+
+#define _parse_pci_split(a, b, c, d, e, ...) parse_pci_split(a, b, c, d, e)
+#define _parse_pci_split_seg(a, b, c, d, e, f, ...) parse_pci_split_seg(a, b, c, d, e, f)
+
+#define parse_pci(a, b, ...)                                            \
+    (count_args(__VA_ARGS__) > 0                                        \
+     ? _parse_pci_split(a, (void *)b, ##__VA_ARGS__, NULL, NULL, NULL)  \
+     :  parse_pci_sbdf(a, (void *)b))
+
+#define parse_pci_seg(a, b, c, ...)                                     \
+    (count_args(__VA_ARGS__) > 0                                        \
+     ? _parse_pci_split_seg(a, (void *)b, (void *)c, ##__VA_ARGS__, NULL, NULL, NULL, NULL) \
+     :  parse_pci_sbdf_seg(a, (void *)b, (void *)c))
+
 #define PCI_BAR_VF      (1u << 0)
 #define PCI_BAR_LAST    (1u << 1)
 #define PCI_BAR_ROM     (1u << 2)
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#17 lore raw

Re: [PATCH v3 08/18] pci: Introduce parse_pci_sbdf{_seg}()

Quoted 7 lines · Teddy Astie

In many places, we're parsing a PCI string into individual parts (seg, bus, dev, fn) and then transform it into a pci_sbdf_t using PCI_SBDF macro. Rather than converting from parts to pci_sbdf_t and vice versa, introduce a new function that parses a PCI string into a pci_sbdf_t structure directly.

Having looked at patch 17 (undoing the transient pieces added here), I can't help the impression that none of the style issues introduced here actually make that patch any easier / smaller. Therefore I'm having a hard time ack-ing this change, which otherwise I would now that (among the committers) we've sorted the issue of transient introduction of unreachable code (permitting it when sorted within a series).

Jan

Quoted 77 lines
xen/drivers/pci/pci.c +20 -2
@@ -151,7 +151,7 @@ void pci_intx(const struct pci_dev *pdev, bool enable)
     pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
 }
 
-const char *__init parse_pci(const char *s, unsigned int *seg_p,
+const char *__init parse_pci_split(const char *s, unsigned int *seg_p,
                              unsigned int *bus_p, unsigned int *dev_p,
                              unsigned int *func_p)
 {
@@ -160,7 +160,7 @@ const char *__init parse_pci(const char *s, unsigned int *seg_p,
     return parse_pci_seg(s, seg_p, bus_p, dev_p, func_p, &def_seg);
 }
 
-const char *__init parse_pci_seg(const char *s, unsigned int *seg_p,
+const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p,
                                  unsigned int *bus_p, unsigned int *dev_p,
                                  unsigned int *func_p, bool *def_seg)
 {
@@ -202,3 +202,21 @@ const char *__init parse_pci_seg(const char *s, unsigned int *seg_p,
 
     return s;
 }
+
+const char *__init parse_pci_sbdf(const char *s, pci_sbdf_t *sbdf)
+{
+    unsigned int seg, bus, dev, func;
+    const char *out = parse_pci(s, &seg, &bus, &dev, &func);
+
+    *sbdf = PCI_SBDF(seg, bus, dev, func);
+    return out;
+}
+
+const char *__init parse_pci_sbdf_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg)
+{
+    unsigned int seg, bus, dev, func;
+    const char *out = parse_pci_seg(s, &seg, &bus, &dev, &func, def_seg);
+
+    *sbdf = PCI_SBDF(seg, bus, dev, func);
+    return out;
+}
xen/include/xen/pci.h +18 -2
index ed6c95452f..8e2f8a1ab7 100644
@@ -274,12 +274,28 @@ unsigned int pci_find_ext_capability(const struct pci_dev *pdev,
 unsigned int pci_find_next_ext_capability(const struct pci_dev *pdev,
                                           unsigned int start,
                                           unsigned int cap);
-const char *parse_pci(const char *s, unsigned int *seg_p, unsigned int *bus_p,
+const char *parse_pci_split(const char *s, unsigned int *seg_p, unsigned int *bus_p,
                       unsigned int *dev_p, unsigned int *func_p);
-const char *parse_pci_seg(const char *s, unsigned int *seg_p,
+const char *parse_pci_split_seg(const char *s, unsigned int *seg_p,
                           unsigned int *bus_p, unsigned int *dev_p,
                           unsigned int *func_p, bool *def_seg);
 
+const char *parse_pci_sbdf(const char *s, pci_sbdf_t *sbdf);
+const char *parse_pci_sbdf_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg);
+
+#define _parse_pci_split(a, b, c, d, e, ...) parse_pci_split(a, b, c, d, e)
+#define _parse_pci_split_seg(a, b, c, d, e, f, ...) parse_pci_split_seg(a, b, c, d, e, f)
+
+#define parse_pci(a, b, ...)                                            \
+    (count_args(__VA_ARGS__) > 0                                        \
+     ? _parse_pci_split(a, (void *)b, ##__VA_ARGS__, NULL, NULL, NULL)  \
+     :  parse_pci_sbdf(a, (void *)b))
+
+#define parse_pci_seg(a, b, c, ...)                                     \
+    (count_args(__VA_ARGS__) > 0                                        \
+     ? _parse_pci_split_seg(a, (void *)b, (void *)c, ##__VA_ARGS__, NULL, NULL, NULL, NULL) \
+     :  parse_pci_sbdf_seg(a, (void *)b, (void *)c))
+
 #define PCI_BAR_VF      (1u << 0)
 #define PCI_BAR_LAST    (1u << 1)
 #define PCI_BAR_ROM     (1u << 2)
#18 lore raw

PATCH v3 9/18 amd/iommu: Parse into pci_sbdf_t directly

In reply to a message not in the archive
---
 xen/drivers/passthrough/amd/iommu_acpi.c | 26 ++++++++++++------------
 1 file changed, 13 insertions(+), 13 deletions(-)
xen/drivers/passthrough/amd/iommu_acpi.c +13 -13
index 39ae637959..33c2c78bdd 100644
@@ -682,8 +682,8 @@ static int __init cf_check parse_ivrs_ioapic(const char *str)
 {
     const char *s = str;
     unsigned long id;
-    unsigned int seg, bus, dev, func;
     unsigned int idx;
+    pci_sbdf_t sbdf;
 
     if ( *s != '[' )
         return -EINVAL;
@@ -692,7 +692,7 @@ static int __init cf_check parse_ivrs_ioapic(const char *str)
     if ( *s != ']' || *++s != '=' )
         return -EINVAL;
 
-    s = parse_pci(s + 1, &seg, &bus, &dev, &func);
+    s = parse_pci(s + 1, &sbdf);
     if ( !s || *s )
         return -EINVAL;
 
@@ -707,7 +707,7 @@ static int __init cf_check parse_ivrs_ioapic(const char *str)
         }
     }
 
-    ioapic_sbdf[idx].sbdf = PCI_SBDF(seg, bus, dev, func);
+    ioapic_sbdf[idx].sbdf = sbdf;
     ioapic_sbdf[idx].id = id;
     ioapic_sbdf[idx].cmdline = true;
 
@@ -719,7 +719,7 @@ static int __init cf_check parse_ivrs_hpet(const char *str)
 {
     const char *s = str;
     unsigned long id;
-    unsigned int seg, bus, dev, func;
+    pci_sbdf_t sbdf;
 
     if ( *s != '[' )
         return -EINVAL;
@@ -728,12 +728,12 @@ static int __init cf_check parse_ivrs_hpet(const char *str)
     if ( id != (typeof(hpet_sbdf.id))id || *s != ']' || *++s != '=' )
         return -EINVAL;
 
-    s = parse_pci(s + 1, &seg, &bus, &dev, &func);
+    s = parse_pci(s + 1, &sbdf);
     if ( !s || *s )
         return -EINVAL;
 
     hpet_sbdf.id = id;
-    hpet_sbdf.sbdf = PCI_SBDF(seg, bus, dev, func);
+    hpet_sbdf.sbdf = sbdf;
     hpet_sbdf.init = HPET_CMDL;
 
     return 0;
@@ -1399,13 +1399,13 @@ static int __init cf_check parse_ivmd_param(const char *s)
         }
 
         do {
-            unsigned int seg, bus, dev, func;
+            pci_sbdf_t sbdf;
 
             if ( nr_ivmd >= ARRAY_SIZE(user_ivmds) )
                 return -E2BIG;
 
-            s = parse_pci(s + 1, &seg, &bus, &dev, &func);
-            if ( !s || seg )
+            s = parse_pci(s + 1, &sbdf);
+            if ( !s || sbdf.seg )
                 return -EINVAL;
 
             user_ivmds[nr_ivmd].start_address = start << PAGE_SHIFT;
@@ -1413,16 +1413,16 @@ static int __init cf_check parse_ivmd_param(const char *s)
             user_ivmds[nr_ivmd].header.flags = ACPI_IVMD_UNITY |
                                                ACPI_IVMD_READ | ACPI_IVMD_WRITE;
             user_ivmds[nr_ivmd].header.length = sizeof(*user_ivmds);
-            user_ivmds[nr_ivmd].header.device_id = PCI_BDF(bus, dev, func);
+            user_ivmds[nr_ivmd].header.device_id = sbdf.bdf;
             user_ivmds[nr_ivmd].header.type = ACPI_IVRS_TYPE_MEMORY_ONE;
 
             if ( *s == '-' )
             {
-                s = parse_pci(s + 1, &seg, &bus, &dev, &func);
-                if ( !s || seg )
+                s = parse_pci(s + 1, &sbdf);
+                if ( !s || sbdf.seg )
                     return -EINVAL;
 
-                user_ivmds[nr_ivmd].aux_data = PCI_BDF(bus, dev, func);
+                user_ivmds[nr_ivmd].aux_data = sbdf.bdf;
                 if ( user_ivmds[nr_ivmd].aux_data <
                      user_ivmds[nr_ivmd].header.device_id )
                     return -EINVAL;
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#20 lore raw

PATCH v3 10/18 vtd: Use pci_sbdf_t in acpi_parse_dev_scope()

In reply to a message not in the archive

Use a dedicated pci_sbdf_t struct that we update instead of recreating one each time we need it.

---
 xen/drivers/passthrough/vtd/dmar.c | 90 ++++++++++++------------------
 1 file changed, 36 insertions(+), 54 deletions(-)
xen/drivers/passthrough/vtd/dmar.c +36 -54
index 8db9591a36..21649fa892 100644
@@ -306,11 +306,11 @@ static int __init scope_device_count(const void *start, const void *end)
 
 static int __init acpi_parse_dev_scope(
     const void *start, const void *end, struct dmar_scope *scope,
-    int type, u16 seg)
+    int type, uint16_t seg)
 {
     struct acpi_ioapic_unit *acpi_ioapic_unit;
     const struct acpi_dmar_device_scope *acpi_scope;
-    u16 bus, sub_bus, sec_bus;
+    uint16_t sub_bus, sec_bus;
     const struct acpi_dmar_pci_path *path;
     struct acpi_drhd_unit *drhd = type == DMAR_TYPE ?
         container_of(scope, struct acpi_drhd_unit, scope) : NULL;
@@ -322,7 +322,7 @@ static int __init acpi_parse_dev_scope(
 
     if ( cnt > 0 )
     {
-        scope->devices = xzalloc_array(u16, cnt);
+        scope->devices = xzalloc_array(uint16_t, cnt);
         if ( !scope->devices )
             return -ENOMEM;
 
@@ -332,29 +332,28 @@ static int __init acpi_parse_dev_scope(
 
     while ( start < end )
     {
+        pci_sbdf_t dev_sbdf;
+
         acpi_scope = start;
         path = (const void *)(acpi_scope + 1);
         depth = (acpi_scope->length - sizeof(*acpi_scope)) / sizeof(*path);
-        bus = acpi_scope->bus;
+        dev_sbdf = PCI_SBDF(seg, acpi_scope->bus, path->dev, path->fn);
 
         while ( --depth > 0 )
         {
-            bus = pci_conf_read8(PCI_SBDF(seg, bus, path->dev, path->fn),
-                                 PCI_SECONDARY_BUS);
+            dev_sbdf.bus = pci_conf_read8(dev_sbdf, PCI_SECONDARY_BUS);
             path++;
+            dev_sbdf.devfn = PCI_DEVFN(path->dev, path->fn);
         }
 
         switch ( acpi_scope->entry_type )
         {
         case ACPI_DMAR_SCOPE_TYPE_BRIDGE:
-            sec_bus = pci_conf_read8(PCI_SBDF(seg, bus, path->dev, path->fn),
-                                     PCI_SECONDARY_BUS);
-            sub_bus = pci_conf_read8(PCI_SBDF(seg, bus, path->dev, path->fn),
-                                     PCI_SUBORDINATE_BUS);
+            sec_bus = pci_conf_read8(dev_sbdf, PCI_SECONDARY_BUS);
+            sub_bus = pci_conf_read8(dev_sbdf, PCI_SUBORDINATE_BUS);
             if ( iommu_verbose )
                 printk(VTDPREFIX " bridge: %pp start=%x sec=%x sub=%x\n",
-                       &PCI_SBDF(seg, bus, path->dev, path->fn),
-                       acpi_scope->bus, sec_bus, sub_bus);
+                       &dev_sbdf, acpi_scope->bus, sec_bus, sub_bus);
 
             dmar_scope_add_buses(scope, sec_bus, sub_bus);
             gfx_only = false;
@@ -362,8 +361,7 @@ static int __init acpi_parse_dev_scope(
 
         case ACPI_DMAR_SCOPE_TYPE_HPET:
             if ( iommu_verbose )
-                printk(VTDPREFIX " MSI HPET: %pp\n",
-                       &PCI_SBDF(seg, bus, path->dev, path->fn));
+                printk(VTDPREFIX " MSI HPET: %pp\n", &dev_sbdf);
 
             if ( drhd )
             {
@@ -374,9 +372,7 @@ static int __init acpi_parse_dev_scope(
                 if ( !acpi_hpet_unit )
                     goto out;
                 acpi_hpet_unit->id = acpi_scope->enumeration_id;
-                acpi_hpet_unit->bus = bus;
-                acpi_hpet_unit->dev = path->dev;
-                acpi_hpet_unit->func = path->fn;
+                acpi_hpet_unit->bdf = dev_sbdf.bdf;
                 list_add(&acpi_hpet_unit->list, &drhd->hpet_list);
 
                 gfx_only = false;
@@ -386,17 +382,14 @@ static int __init acpi_parse_dev_scope(
 
         case ACPI_DMAR_SCOPE_TYPE_ENDPOINT:
             if ( iommu_verbose )
-                printk(VTDPREFIX " endpoint: %pp\n",
-                       &PCI_SBDF(seg, bus, path->dev, path->fn));
+                printk(VTDPREFIX " endpoint: %pp\n", &dev_sbdf);
 
-            if ( drhd && pci_device_detect(PCI_SBDF(seg, bus, path->dev,
-                                                    path->fn)) )
+            if ( drhd && pci_device_detect(dev_sbdf) )
             {
-                if ( pci_conf_read8(PCI_SBDF(seg, bus, path->dev, path->fn),
-                                    PCI_CLASS_DEVICE + 1) != 0x03
-                                    /* PCI_BASE_CLASS_DISPLAY */ )
+                if ( pci_conf_read8(dev_sbdf, PCI_CLASS_DEVICE + 1) !=
+                     0x03 /* PCI_BASE_CLASS_DISPLAY */ )
                     gfx_only = false;
-                else if ( !seg && !bus && path->dev == 2 && !path->fn )
+                else if ( dev_sbdf.sbdf == PCI_SBDF(0, 0, 2, 0).sbdf )
                     igd_drhd_address = drhd->address;
             }
 
@@ -404,8 +397,7 @@ static int __init acpi_parse_dev_scope(
 
         case ACPI_DMAR_SCOPE_TYPE_IOAPIC:
             if ( iommu_verbose )
-                printk(VTDPREFIX " IOAPIC: %pp\n",
-                       &PCI_SBDF(seg, bus, path->dev, path->fn));
+                printk(VTDPREFIX " IOAPIC: %pp\n", &dev_sbdf);
 
             if ( drhd )
             {
@@ -414,9 +406,7 @@ static int __init acpi_parse_dev_scope(
                 if ( !acpi_ioapic_unit )
                     goto out;
                 acpi_ioapic_unit->apic_id = acpi_scope->enumeration_id;
-                acpi_ioapic_unit->ioapic.bdf.bus = bus;
-                acpi_ioapic_unit->ioapic.bdf.dev = path->dev;
-                acpi_ioapic_unit->ioapic.bdf.func = path->fn;
+                acpi_ioapic_unit->ioapic.info = dev_sbdf.bdf;
                 list_add(&acpi_ioapic_unit->list, &drhd->ioapic_list);
 
                 gfx_only = false;
@@ -432,7 +422,7 @@ static int __init acpi_parse_dev_scope(
             gfx_only = false;
             continue;
         }
-        scope->devices[didx++] = PCI_BDF(bus, path->dev, path->fn);
+        scope->devices[didx++] = dev_sbdf.bdf;
         start += acpi_scope->length;
     }
 
@@ -516,7 +506,6 @@ acpi_parse_one_drhd(struct acpi_dmar_header *header)
         acpi_register_drhd_unit(dmaru);
     else
     {
-        u8 b, d, f;
         unsigned int i = 0;
         union {
             const void *raw;
@@ -530,18 +519,16 @@ acpi_parse_one_drhd(struct acpi_dmar_header *header)
         for ( p.raw = dev_scope_start; i < dmaru->scope.devices_cnt;
               i++, p.raw += p.scope->length )
         {
+            pci_sbdf_t sbdf = PCI_SBDF(drhd->segment, dmaru->scope.devices[i]);
+
             if ( p.scope->entry_type == ACPI_DMAR_SCOPE_TYPE_IOAPIC ||
                  p.scope->entry_type == ACPI_DMAR_SCOPE_TYPE_HPET )
                 continue;
 
-            b = PCI_BUS(dmaru->scope.devices[i]);
-            d = PCI_SLOT(dmaru->scope.devices[i]);
-            f = PCI_FUNC(dmaru->scope.devices[i]);
-
-            if ( !pci_device_detect(PCI_SBDF(drhd->segment, b, d, f)) )
+            if ( !pci_device_detect(sbdf) )
                 printk(XENLOG_WARNING VTDPREFIX
                        " Non-existent device (%pp) in this DRHD's scope!\n",
-                       &PCI_SBDF(drhd->segment, b, d, f));
+                       &sbdf);
         }
 
         acpi_register_drhd_unit(dmaru);
@@ -570,17 +557,14 @@ static int __init register_one_rmrr(struct acpi_rmrr_unit *rmrru)
 
     for ( ; i < rmrru->scope.devices_cnt; i++ )
     {
-        u8 b = PCI_BUS(rmrru->scope.devices[i]);
-        u8 d = PCI_SLOT(rmrru->scope.devices[i]);
-        u8 f = PCI_FUNC(rmrru->scope.devices[i]);
+        pci_sbdf_t sbdf = PCI_SBDF(rmrru->segment, rmrru->scope.devices[i]);
 
-        if ( pci_device_detect(PCI_SBDF(rmrru->segment, b, d, f)) == 0 )
+        if ( pci_device_detect(sbdf) == 0 )
         {
             dprintk(XENLOG_WARNING VTDPREFIX,
                     " Non-existent device (%pp) is reported"
                     " in RMRR [%"PRIx64", %"PRIx64"]'s scope!\n",
-                    &PCI_SBDF(rmrru->segment, b, d, f),
-                    rmrru->base_address, rmrru->end_address);
+                    &sbdf, rmrru->base_address, rmrru->end_address);
             ignore = true;
         }
         else
@@ -764,15 +748,13 @@ static int __init register_one_satc(struct acpi_satc_unit *satcu)
 
     for ( ; i < satcu->scope.devices_cnt; i++ )
     {
-        uint8_t b = PCI_BUS(satcu->scope.devices[i]);
-        uint8_t d = PCI_SLOT(satcu->scope.devices[i]);
-        uint8_t f = PCI_FUNC(satcu->scope.devices[i]);
+        pci_sbdf_t sbdf = PCI_SBDF(satcu->segment, satcu->scope.devices[i]);
 
-        if ( !pci_device_detect(PCI_SBDF(satcu->segment, b, d, f)) )
+        if ( !pci_device_detect(sbdf) )
         {
             dprintk(XENLOG_WARNING VTDPREFIX,
                     " Non-existent device (%pp) is reported in SATC scope!\n",
-                    &PCI_SBDF(satcu->segment, b, d, f));
+                    &sbdf);
             ignore = true;
         }
         else
@@ -1195,8 +1177,9 @@ int cf_check intel_iommu_get_reserved_device_memory(
 static int __init cf_check parse_rmrr_param(const char *str)
 {
     const char *s = str, *cur, *stmp;
-    unsigned int seg, bus, dev, func, dev_count;
+    unsigned int dev_count;
     unsigned long start, end;
+    pci_sbdf_t sbdf;
 
     do {
         if ( nr_rmrr >= MAX_USER_RMRR )
@@ -1233,7 +1216,7 @@ static int __init cf_check parse_rmrr_param(const char *str)
         do {
             bool def_seg = false;
 
-            stmp = parse_pci_seg(s + 1, &seg, &bus, &dev, &func, &def_seg);
+            stmp = parse_pci_seg(s + 1, &sbdf, &def_seg);
             if ( !stmp )
                 return -EINVAL;
 
@@ -1242,12 +1225,11 @@ static int __init cf_check parse_rmrr_param(const char *str)
              * Segment will be replaced with one from first device.
              */
             if ( user_rmrrs[nr_rmrr].dev_count && def_seg )
-                seg = PCI_SEG(user_rmrrs[nr_rmrr].sbdf[0]);
+                sbdf.seg = PCI_SEG(user_rmrrs[nr_rmrr].sbdf[0]);
 
             /* Keep sbdf's even if they differ and later report an error. */
             dev_count = user_rmrrs[nr_rmrr].dev_count;
-            user_rmrrs[nr_rmrr].sbdf[dev_count] =
-               PCI_SBDF(seg, bus, dev, func).sbdf;
+            user_rmrrs[nr_rmrr].sbdf[dev_count] = sbdf.sbdf;
 
             user_rmrrs[nr_rmrr].dev_count++;
             s = stmp;
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
Acked-by on v3 10/18
#21 lore raw

Re: [PATCH v3 10/18] vtd: Use pci_sbdf_t in acpi_parse_dev_scope()

Quoted 4 lines · Teddy Astie

Use a dedicated pci_sbdf_t struct that we update instead of recreating one each time we need it.

albeit preferably with ...

xen/drivers/passthrough/vtd/dmar.c int cf_check intel_iommu_get_reserved_device_memory( · line 1182

Quoted 8 lines
xen/drivers/passthrough/vtd/dmar.c +2 -1
@@ -1195,8 +1177,9 @@ int cf_check intel_iommu_get_reserved_device_memory(
 static int __init cf_check parse_rmrr_param(const char *str)
 {
     const char *s = str, *cur, *stmp;
-    unsigned int seg, bus, dev, func, dev_count;
+    unsigned int dev_count;
     unsigned long start, end;
+    pci_sbdf_t sbdf;

... this new variable moved to ...

Quoted 14 lines
xen/drivers/passthrough/vtd/dmar.c +1 -2
@@ -1233,7 +1216,7 @@ static int __init cf_check parse_rmrr_param(const char *str)
         do {
             bool def_seg = false;
 
-            stmp = parse_pci_seg(s + 1, &seg, &bus, &dev, &func, &def_seg);
+            stmp = parse_pci_seg(s + 1, &sbdf, &def_seg);
             if ( !stmp )
                 return -EINVAL;
 
@@ -1242,12 +1225,11 @@ static int __init cf_check parse_rmrr_param(const char *str)
              * Segment will be replaced with one from first device.
              */
             if ( user_rmrrs[nr_rmrr].dev_count && def_seg )
-                seg = PCI_SEG(user_rmrrs[nr_rmrr].sbdf[0]);

xen/drivers/passthrough/vtd/dmar.c static int __init cf_check parse_rmrr_param(const char *str) · line 1235

Quoted 10 lines
xen/drivers/passthrough/vtd/dmar.c +2 -2
+                sbdf.seg = PCI_SEG(user_rmrrs[nr_rmrr].sbdf[0]);
 
             /* Keep sbdf's even if they differ and later report an error. */
             dev_count = user_rmrrs[nr_rmrr].dev_count;
-            user_rmrrs[nr_rmrr].sbdf[dev_count] =
-               PCI_SBDF(seg, bus, dev, func).sbdf;
+            user_rmrrs[nr_rmrr].sbdf[dev_count] = sbdf.sbdf;
 
             user_rmrrs[nr_rmrr].dev_count++;
             s = stmp;

... the narrow-most scope it's used in.

Jan

#22 lore raw

PATCH v3 11/18 drivers/char/xhci: Parse into pci_sbdf_t directly

In reply to a message not in the archive
---
 xen/drivers/char/xhci-dbc.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
xen/drivers/char/xhci-dbc.c +3 -3
index 3f41aa7022..d4961b5f4d 100644
@@ -1357,9 +1357,9 @@ static int __init cf_check xhci_parse_dbgp(const char *opt_dbgp)
     }
     else if ( strncmp(opt_dbgp + 4, "@pci", 4) == 0 )
     {
-        unsigned int bus, slot, func;
+        pci_sbdf_t sbdf;
 
-        e = parse_pci(opt_dbgp + 8, NULL, &bus, &slot, &func);
+        e = parse_pci(opt_dbgp + 8, &sbdf);
         if ( !e || (*e && *e != ',') )
         {
             printk(XENLOG_ERR
@@ -1368,7 +1368,7 @@ static int __init cf_check xhci_parse_dbgp(const char *opt_dbgp)
             return -EINVAL;
         }
 
-        dbc->sbdf = PCI_SBDF(0, bus, slot, func);
+        dbc->sbdf = sbdf;
     }
     opt = e;
 
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#23 lore raw

Re: [PATCH v3 11/18] drivers/char/xhci: Parse into pci_sbdf_t directly

Quoted 1 line · Teddy Astie

If we deem this acceptable despite the behavioral change ...

xen/drivers/char/xhci-dbc.c static int __init cf_check xhci_parse_dbgp(const char *opt_dbgp) · line 1362

Quoted 11 lines
xen/drivers/char/xhci-dbc.c +2 -2
@@ -1357,9 +1357,9 @@ static int __init cf_check xhci_parse_dbgp(const char *opt_dbgp)
     }
     else if ( strncmp(opt_dbgp + 4, "@pci", 4) == 0 )
     {
-        unsigned int bus, slot, func;
+        pci_sbdf_t sbdf;
 
-        e = parse_pci(opt_dbgp + 8, NULL, &bus, &slot, &func);
+        e = parse_pci(opt_dbgp + 8, &sbdf);

... from not permitting a segment to permitting one, this at the very least needs to come with a non-empty description (justifying the change). Also (as I think I has said there) the doc change wants to accompany the functional change right away.

xen/drivers/char/xhci-dbc.c static int __init cf_check xhci_parse_dbgp(const char *opt_dbgp) · line 1371

Quoted 6 lines
xen/drivers/char/xhci-dbc.c +1 -1
@@ -1368,7 +1368,7 @@ static int __init cf_check xhci_parse_dbgp(const char *opt_dbgp)
             return -EINVAL;
         }
 
-        dbc->sbdf = PCI_SBDF(0, bus, slot, func);
+        dbc->sbdf = sbdf;

Why is the extra variable needed? Can't we parse directly into dbc->sbdf?

Jan

#24 lore raw

PATCH v3 12/18 ns16550: Parse into pci_sbdf_t directly

In reply to a message not in the archive
---
 xen/drivers/char/ns16550.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
xen/drivers/char/ns16550.c +12 -12
index a3d6c46a73..cdc9122ff9 100644
@@ -1571,22 +1571,22 @@ static bool __init parse_positional(struct ns16550 *uart, char **str)
 #ifdef CONFIG_HAS_PCI
     if ( *conf == ',' && *++conf != ',' )
     {
-        unsigned int b, d, f;
+        pci_sbdf_t sbdf;
 
-        conf = parse_pci(conf, NULL, &b, &d, &f);
+        conf = parse_pci(conf, &sbdf);
         if ( !conf )
             PARSE_ERR_RET("Bad port PCI coordinates");
-        uart->pci_device = PCI_SBDF(0, b, d, f);
+        uart->pci_device = sbdf;
         uart->ps_bdf_enable = true;
     }
 
     if ( *conf == ',' && *++conf != ',' )
     {
-        unsigned int b, d, f;
+        pci_sbdf_t sbdf;
 
-        if ( !parse_pci(conf, NULL, &b, &d, &f) )
+        if ( !parse_pci(conf, &sbdf) )
             PARSE_ERR_RET("Bad bridge PCI coordinates");
-        uart->pci_bridge = PCI_SBDF(0, b, d, f);
+        uart->pci_bridge = sbdf;
         uart->pb_bdf_enable = true;
     }
 #endif
@@ -1670,22 +1670,22 @@ static bool __init parse_namevalue_pairs(char *str, struct ns16550 *uart)
 
         case port_bdf:
         {
-            unsigned int b, d, f;
+            pci_sbdf_t sbdf;
 
-            if ( !parse_pci(param_value, NULL, &b, &d, &f) )
+            if ( !parse_pci(param_value, &sbdf) )
                 PARSE_ERR_RET("Bad port PCI coordinates\n");
-            uart->pci_device = PCI_SBDF(0, b, d, f);
+            uart->pci_device = sbdf;
             uart->ps_bdf_enable = true;
             break;
         }
 
         case bridge_bdf:
         {
-            unsigned int b, d, f;
+            pci_sbdf_t sbdf;
 
-            if ( !parse_pci(param_value, NULL, &b, &d, &f) )
+            if ( !parse_pci(param_value, &sbdf) )
                 PARSE_ERR_RET("Bad bridge PCI coordinates\n");
-            uart->pci_bridge = PCI_SBDF(0, b, d, f);
+            uart->pci_bridge = sbdf;
             uart->pb_bdf_enable = true;
             break;
         }
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#25 lore raw

Re: [PATCH v3 12/18] ns16550: Parse into pci_sbdf_t directly

Quoted 39 lines · Teddy Astie
---
 xen/drivers/char/ns16550.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
xen/drivers/char/ns16550.c +6 -6
index a3d6c46a73..cdc9122ff9 100644
@@ -1571,22 +1571,22 @@ static bool __init parse_positional(struct ns16550 *uart, char **str)
 #ifdef CONFIG_HAS_PCI
     if ( *conf == ',' && *++conf != ',' )
     {
-        unsigned int b, d, f;
+        pci_sbdf_t sbdf;
 
-        conf = parse_pci(conf, NULL, &b, &d, &f);
+        conf = parse_pci(conf, &sbdf);
         if ( !conf )
             PARSE_ERR_RET("Bad port PCI coordinates");
-        uart->pci_device = PCI_SBDF(0, b, d, f);
+        uart->pci_device = sbdf;
         uart->ps_bdf_enable = true;
     }
 
     if ( *conf == ',' && *++conf != ',' )
     {
-        unsigned int b, d, f;
+        pci_sbdf_t sbdf;
 
-        if ( !parse_pci(conf, NULL, &b, &d, &f) )
+        if ( !parse_pci(conf, &sbdf) )
             PARSE_ERR_RET("Bad bridge PCI coordinates");
-        uart->pci_bridge = PCI_SBDF(0, b, d, f);
+        uart->pci_bridge = sbdf;
         uart->pb_bdf_enable = true;
     }
 #endif

xen/drivers/char/ns16550.c static bool __init parse_namevalue_pairs(char *str, struct ns16550 *uart) · line 1675

Quoted 8 lines
xen/drivers/char/ns16550.c +2 -2
@@ -1670,22 +1670,22 @@ static bool __init parse_namevalue_pairs(char *str, struct ns16550 *uart)
 
         case port_bdf:
         {
-            unsigned int b, d, f;
+            pci_sbdf_t sbdf;
 
-            if ( !parse_pci(param_value, NULL, &b, &d, &f) )
+            if ( !parse_pci(param_value, &sbdf) )

Same as for the earlier change: A functional change of whatever kind wants justifying. And the doc update wants to come right with it.

Jan

#26 lore raw

PATCH v3 13/18 vtd: Rename acpi_ioapic_unit.ioapic.info to acpi_ioapic_unit.bdf

In reply to a message not in the archive

This is actually the bdf, collapse the redundant "ioapic" struct and rename "info" to "bdf" to avoid confusion.

---
 xen/drivers/passthrough/vtd/dmar.c     | 2 +-
 xen/drivers/passthrough/vtd/dmar.h     | 6 +++---
 xen/drivers/passthrough/vtd/intremap.c | 2 +-
 3 files changed, 5 insertions(+), 5 deletions(-)
xen/drivers/passthrough/vtd/dmar.c +1 -1
index 21649fa892..c4243c747b 100644
@@ -406,7 +406,7 @@ static int __init acpi_parse_dev_scope(
                 if ( !acpi_ioapic_unit )
                     goto out;
                 acpi_ioapic_unit->apic_id = acpi_scope->enumeration_id;
-                acpi_ioapic_unit->ioapic.info = dev_sbdf.bdf;
+                acpi_ioapic_unit->bdf = dev_sbdf.bdf;
                 list_add(&acpi_ioapic_unit->list, &drhd->ioapic_list);
 
                 gfx_only = false;
xen/drivers/passthrough/vtd/dmar.h +3 -3
index 47e6918b45..5ab95c68bc 100644
@@ -29,13 +29,13 @@ struct acpi_ioapic_unit {
     struct list_head list;
     int apic_id;
     union {
-        u16 info;
+        uint16_t bdf;
         struct {
             u16 func: 3,
                 dev:  5,
                 bus:  8;
-        }bdf;
-    }ioapic;
+        };
+    };
 };
 
 struct acpi_hpet_unit {
xen/drivers/passthrough/vtd/intremap.c +1 -1
index dd2788efd7..3aa8babdfd 100644
@@ -98,7 +98,7 @@ static u16 apicid_to_bdf(int apic_id)
 
     list_for_each_entry ( acpi_ioapic_unit, &drhd->ioapic_list, list )
         if ( acpi_ioapic_unit->apic_id == apic_id )
-            return acpi_ioapic_unit->ioapic.info;
+            return acpi_ioapic_unit->bdf;
 
     dprintk(XENLOG_ERR VTDPREFIX, "Didn't find the bdf for the apic_id!\n");
     return 0;
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
Acked-by on v3 13/18
#27 lore raw

Re: [PATCH v3 13/18] vtd: Rename acpi_ioapic_unit.ioapic.info to acpi_ioapic_unit.bdf

Quoted 5 lines · Teddy Astie

This is actually the bdf, collapse the redundant "ioapic" struct and rename "info" to "bdf" to avoid confusion.

With the tags flipped to be in chronological order:

Looks like this is independent of earlier patches in this series, and hence could go in right away. Please confirm.

Jan

#28 lore raw

PATCH v3 14/18 pci: Use pci_sbdf_t in _scan_pci_devices

In reply to a message not in the archive

Avoid creating pci_sbdf_t objects each time we need it.

---
 xen/drivers/passthrough/pci.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
xen/drivers/passthrough/pci.c +5 -5
index e8c9dd9b26..621537f3cc 100644
@@ -1218,6 +1218,8 @@ static int __init cf_check _scan_pci_devices(struct pci_seg *pseg, void *arg)
         {
             for ( func = 0; func < 8; func++ )
             {
+                pci_sbdf_t sbdf = PCI_SBDF(pseg->nr, bus, dev, func);
+
                 if ( !pci_device_detect(PCI_SBDF(pseg->nr, bus, dev, func)) )
                 {
                     if ( !func )
@@ -1225,17 +1227,15 @@ static int __init cf_check _scan_pci_devices(struct pci_seg *pseg, void *arg)
                     continue;
                 }
 
-                pdev = alloc_pdev(pseg, bus, PCI_DEVFN(dev, func));
+                pdev = alloc_pdev(pseg, bus, sbdf.devfn);
                 if ( !pdev )
                 {
                     printk(XENLOG_WARNING "%pp: alloc_pdev failed\n",
-                           &PCI_SBDF(pseg->nr, bus, dev, func));
+                           &sbdf);
                     return -ENOMEM;
                 }
 
-                if ( !func && !(pci_conf_read8(PCI_SBDF(pseg->nr, bus, dev,
-                                                        func),
-                                               PCI_HEADER_TYPE) & 0x80) )
+                if ( !func && !(pci_conf_read8(sbdf, PCI_HEADER_TYPE) & 0x80) )
                     break;
             }
         }
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
Acked-by on v3 14/18
#29 lore raw

Re: [PATCH v3 14/18] pci: Use pci_sbdf_t in _scan_pci_devices

xen/drivers/passthrough/pci.c static int __init cf_check _scan_pci_devices(struct pci_seg *pseg, void *arg) · line 1223

Quoted 9 lines · Teddy Astie
xen/drivers/passthrough/pci.c +2 -0
@@ -1218,6 +1218,8 @@ static int __init cf_check _scan_pci_devices(struct pci_seg *pseg, void *arg)
         {
             for ( func = 0; func < 8; func++ )
             {
+                pci_sbdf_t sbdf = PCI_SBDF(pseg->nr, bus, dev, func);
+
                 if ( !pci_device_detect(PCI_SBDF(pseg->nr, bus, dev, func)) )

Why don't you use the new variable right here? With that also converted:

Jan

#30 lore raw

PATCH v3 15/18 pci: Allow ommiting func when parsing with parse_pci_sbdf()

In reply to a message not in the archive

Allow parsing PCI SBDF with the function part omitted (i.e XXXX:YY:ZZ), in such case, the parsed PCI function is zero.

Then use pci_sbdf_t variant of parse_pci() in parse_phantom_dev().

---
 xen/drivers/passthrough/pci.c | 10 +++++-----
 xen/drivers/pci/pci.c         | 12 ++++--------
 2 files changed, 9 insertions(+), 13 deletions(-)
xen/drivers/passthrough/pci.c +5 -5
index 621537f3cc..69c529459c 100644
@@ -148,7 +148,7 @@ static unsigned int nr_phantom_devs;
 static int __init cf_check parse_phantom_dev(const char *str)
 {
     const char *s;
-    unsigned int seg, bus, slot;
+    pci_sbdf_t sbdf;
     struct phantom_dev phantom;
 
     if ( !*str )
@@ -156,13 +156,13 @@ static int __init cf_check parse_phantom_dev(const char *str)
     if ( nr_phantom_devs >= ARRAY_SIZE(phantom_devs) )
         return -E2BIG;
 
-    s = parse_pci(str, &seg, &bus, &slot, NULL);
+    s = parse_pci(str, &sbdf);
     if ( !s || *s != ',' )
         return -EINVAL;
 
-    phantom.seg = seg;
-    phantom.bus = bus;
-    phantom.slot = slot;
+    phantom.seg = sbdf.seg;
+    phantom.bus = sbdf.bus;
+    phantom.slot = sbdf.dev;
 
     switch ( phantom.stride = simple_strtol(s + 1, &s, 0) )
     {
xen/drivers/pci/pci.c +4 -8
index 743e2fd263..793545ae49 100644
@@ -164,7 +164,7 @@ const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p,
                                  unsigned int *bus_p, unsigned int *dev_p,
                                  unsigned int *func_p, bool *def_seg)
 {
-    unsigned long seg = simple_strtoul(s, &s, 16), bus, dev, func;
+    unsigned long seg = simple_strtoul(s, &s, 16), bus, dev, func = 0;
 
     if ( *s != ':' )
         return NULL;
@@ -179,14 +179,10 @@ const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p,
         seg = 0;
         *def_seg = true;
     }
-    if ( func_p )
-    {
-        if ( *s != '.' )
-            return NULL;
+
+    if ( func_p && *s == '.' )
         func = simple_strtoul(s + 1, &s, 0);
-    }
-    else
-        func = 0;
+
     if ( seg != (seg_p ? (u16)seg : 0) ||
          bus != PCI_BUS(PCI_BDF(bus, 0)) ||
          dev != PCI_SLOT(PCI_DEVFN(dev, 0)) ||
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#31 lore raw

Re: [PATCH v3 15/18] pci: Allow ommiting func when parsing with parse_pci_sbdf()

Quoted 4 lines · Teddy Astie

Allow parsing PCI SBDF with the function part omitted (i.e XXXX:YY:ZZ), in such case, the parsed PCI function is zero.

Then use pci_sbdf_t variant of parse_pci() in parse_phantom_dev().

But you allow it to be omitted everywhere. I don't think we want people to omit the .0 when they mean function 0.

Jan

#32 lore raw

PATCH v3 16/18 ehci-dbgp: Use pci_sbdf_t instead of (bus, slot, func)

In reply to a message not in the archive

We also take the opportunity to allow the device to exist outside of segment 0 (only when specified with pci@ syntax), since it's not hardcoded anymore.

---
 xen/drivers/char/ehci-dbgp.c | 100 +++++++++++++----------------------
 1 file changed, 37 insertions(+), 63 deletions(-)
xen/drivers/char/ehci-dbgp.c +37 -63
index ab1e3c6de4..01586bc5b3 100644
@@ -334,8 +334,9 @@ struct ehci_dbgp {
     unsigned long timeout;
     struct timer timer;
     spinlock_t *lock;
+    pci_sbdf_t sbdf;
     bool reset_run;
-    u8 bus, slot, func, bar;
+    u8 bar;
     u16 pci_cr;
     u32 bar_val;
     unsigned int cap;
@@ -681,16 +682,14 @@ static int dbgp_control_msg(struct ehci_dbgp *dbgp, unsigned int devnum,
     return ret;
 }
 
-static unsigned int __init __find_dbgp(u8 bus, u8 slot, u8 func)
+static unsigned int __init __find_dbgp(pci_sbdf_t sbdf)
 {
-    uint32_t class = pci_conf_read32(PCI_SBDF(0, bus, slot, func),
-                                     PCI_CLASS_REVISION);
+    uint32_t class = pci_conf_read32(sbdf, PCI_CLASS_REVISION);
 
     if ( (class >> 8) != PCI_CLASS_SERIAL_USB_EHCI )
         return 0;
 
-    return pci_find_cap_offset(PCI_SBDF(0, bus, slot, func),
-                               PCI_CAP_ID_EHCI_DEBUG);
+    return pci_find_cap_offset(sbdf, PCI_CAP_ID_EHCI_DEBUG);
 }
 
 static unsigned int __init find_dbgp(struct ehci_dbgp *dbgp,
@@ -704,27 +703,25 @@ static unsigned int __init find_dbgp(struct ehci_dbgp *dbgp,
         {
             for ( func = 0; func < 8; func++ )
             {
+                pci_sbdf_t sbdf = PCI_SBDF(0, bus, slot, func);
                 unsigned int cap;
 
-                if ( !pci_device_detect(PCI_SBDF(0, bus, slot, func)) )
+                if ( !pci_device_detect(sbdf) )
                 {
                     if ( !func )
                         break;
                     continue;
                 }
 
-                cap = __find_dbgp(bus, slot, func);
+                cap = __find_dbgp(sbdf);
                 if ( !cap || ehci_num-- )
                 {
-                    if ( !func && !(pci_conf_read8(PCI_SBDF(0, bus, slot, func),
-                                                   PCI_HEADER_TYPE) & 0x80) )
+                    if ( !func && !(pci_conf_read8(sbdf, PCI_HEADER_TYPE) & 0x80) )
                         break;
                     continue;
                 }
 
-                dbgp->bus = bus;
-                dbgp->slot = slot;
-                dbgp->func = func;
+                dbgp->sbdf = sbdf;
                 return cap;
             }
         }
@@ -1011,20 +1008,17 @@ static set_debug_port_t __read_mostly set_debug_port = default_set_debug_port;
 static void cf_check nvidia_set_debug_port(
     struct ehci_dbgp *dbgp, unsigned int port)
 {
-    uint32_t dword = pci_conf_read32(PCI_SBDF(0, dbgp->bus, dbgp->slot,
-                                              dbgp->func), 0x74);
+    uint32_t dword = pci_conf_read32(dbgp->sbdf, 0x74);
 
     dword &= ~(0x0f << 12);
     dword |= (port & 0x0f) << 12;
-    pci_conf_write32(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func), 0x74,
-                     dword);
+    pci_conf_write32(dbgp->sbdf, 0x74, dword);
     dbgp_printk("set debug port to %u\n", port);
 }
 
 static void __init detect_set_debug_port(struct ehci_dbgp *dbgp)
 {
-    if ( pci_conf_read16(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                         PCI_VENDOR_ID) == 0x10de )
+    if ( pci_conf_read16(dbgp->sbdf, PCI_VENDOR_ID) == 0x10de )
     {
         dbgp_printk("using nvidia set_debug_port\n");
         set_debug_port = nvidia_set_debug_port;
@@ -1046,15 +1040,13 @@ static void ehci_dbgp_bios_handoff(struct ehci_dbgp *dbgp, u32 hcc_params)
     if ( !offset )
         return;
 
-    cap = pci_conf_read32(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                          offset);
+    cap = pci_conf_read32(dbgp->sbdf, offset);
     dbgp_printk("dbgp: EHCI BIOS state %08x\n", cap);
 
     if ( (cap & 0xff) == 1 && (cap & EHCI_USBLEGSUP_BIOS) )
     {
         dbgp_printk("dbgp: BIOS handoff\n");
-        pci_conf_write8(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                        offset + 3, 1);
+        pci_conf_write8(dbgp->sbdf, offset + 3, 1);
     }
 
     /* if boot firmware now owns EHCI, spin till it hands it over. */
@@ -1063,8 +1055,7 @@ static void ehci_dbgp_bios_handoff(struct ehci_dbgp *dbgp, u32 hcc_params)
     {
         mdelay(10);
         msec -= 10;
-        cap = pci_conf_read32(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                              offset);
+        cap = pci_conf_read32(dbgp->sbdf, offset);
     }
 
     if ( cap & EHCI_USBLEGSUP_BIOS )
@@ -1072,13 +1063,11 @@ static void ehci_dbgp_bios_handoff(struct ehci_dbgp *dbgp, u32 hcc_params)
         /* well, possibly buggy BIOS... try to shut it down,
          * and hope nothing goes too wrong */
         dbgp_printk("dbgp: BIOS handoff failed: %08x\n", cap);
-        pci_conf_write8(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                        offset + 2, 0);
+        pci_conf_write8(dbgp->sbdf, offset + 2, 0);
     }
 
     /* just in case, always disable EHCI SMIs */
-    pci_conf_write8(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                    offset + EHCI_USBLEGCTLSTS, 0);
+    pci_conf_write8(dbgp->sbdf, offset + EHCI_USBLEGCTLSTS, 0);
 }
 
 static int ehci_dbgp_setup(struct ehci_dbgp *dbgp)
@@ -1320,19 +1309,15 @@ static void __init cf_check ehci_dbgp_init_preirq(struct serial_port *port)
     u32 debug_port, offset;
     void __iomem *ehci_bar;
 
-    debug_port = pci_conf_read32(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                                 dbgp->cap);
+    debug_port = pci_conf_read32(dbgp->sbdf, dbgp->cap);
     offset = (debug_port >> 16) & 0xfff;
 
     /* double check if the mem space is enabled */
-    dbgp->pci_cr = pci_conf_read8(PCI_SBDF(0, dbgp->bus, dbgp->slot,
-                                           dbgp->func),
-                                  PCI_COMMAND);
+    dbgp->pci_cr = pci_conf_read8(dbgp->sbdf, PCI_COMMAND);
     if ( !(dbgp->pci_cr & PCI_COMMAND_MEMORY) )
     {
         dbgp->pci_cr |= PCI_COMMAND_MEMORY;
-        pci_conf_write16(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                         PCI_COMMAND, dbgp->pci_cr);
+        pci_conf_write16(dbgp->sbdf, PCI_COMMAND, dbgp->pci_cr);
         dbgp_printk("MMIO for EHCI enabled\n");
     }
 
@@ -1376,7 +1361,7 @@ static void __init cf_check ehci_dbgp_init_postirq(struct serial_port *port)
 
     ehci_dbgp_setup_postirq(dbgp);
 
-    pci_hide_device(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func));
+    pci_hide_device(dbgp->sbdf);
 }
 
 static int ehci_dbgp_check_release(struct ehci_dbgp *dbgp)
@@ -1397,8 +1382,7 @@ static int ehci_dbgp_check_release(struct ehci_dbgp *dbgp)
      * so as to allow for reuse of the USB device, which means it is time
      * to shutdown the USB debug port.
      */
-    printk(XENLOG_INFO "Releasing EHCI debug port at %02x:%02x.%u\n",
-           dbgp->bus, dbgp->slot, dbgp->func);
+    printk(XENLOG_INFO "Releasing EHCI debug port at %pp\n", &dbgp->sbdf);
 
     if ( dbgp->timer.function )
         kill_timer(&dbgp->timer);
@@ -1431,9 +1415,7 @@ static void cf_check ehci_dbgp_suspend(struct serial_port *port)
     stop_timer(&dbgp->timer);
     dbgp->timer.expires = 0;
 
-    dbgp->pci_cr = pci_conf_read16(PCI_SBDF(0, dbgp->bus, dbgp->slot,
-                                            dbgp->func),
-                                   PCI_COMMAND);
+    dbgp->pci_cr = pci_conf_read16(dbgp->sbdf, PCI_COMMAND);
 
     dbgp->state = dbgp_unsafe;
 }
@@ -1445,10 +1427,8 @@ static void cf_check ehci_dbgp_resume(struct serial_port *port)
     if ( !dbgp->ehci_debug )
         return;
 
-    pci_conf_write32(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func), dbgp->bar,
-                     dbgp->bar_val);
-    pci_conf_write16(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                     PCI_COMMAND, dbgp->pci_cr);
+    pci_conf_write32(dbgp->sbdf, dbgp->bar, dbgp->bar_val);
+    pci_conf_write16(dbgp->sbdf, PCI_COMMAND, dbgp->pci_cr);
 
     ehci_dbgp_setup_preirq(dbgp);
     ehci_dbgp_setup_postirq(dbgp);
@@ -1505,36 +1485,31 @@ void __init ehci_dbgp_init(void)
         if ( !dbgp->cap )
             return;
 
-        dbgp_printk("Found EHCI debug port on %02x:%02x.%u\n",
-                    dbgp->bus, dbgp->slot, dbgp->func);
+        dbgp_printk("Found EHCI debug port on %pp\n", &dbgp->sbdf);
     }
     else if ( strncmp(opt_dbgp + 4, "@pci", 4) == 0 )
     {
-        unsigned int bus, slot, func;
+        pci_sbdf_t sbdf;
 
-        e = parse_pci(opt_dbgp + 8, NULL, &bus, &slot, &func);
+        e = parse_pci(opt_dbgp + 8, &sbdf);
         if ( !e || *e )
             return;
 
-        dbgp->bus = bus;
-        dbgp->slot = slot;
-        dbgp->func = func;
+        dbgp->sbdf = sbdf;
 
-        if ( !pci_device_detect(PCI_SBDF(0, bus, slot, func)) )
+        if ( !pci_device_detect(sbdf) )
             return;
 
-        dbgp->cap = __find_dbgp(bus, slot, func);
+        dbgp->cap = __find_dbgp(sbdf);
         if ( !dbgp->cap )
             return;
 
-        dbgp_printk("Using EHCI debug port on %02x:%02x.%u\n",
-                    bus, slot, func);
+        dbgp_printk("Using EHCI debug port on %pp\n", &sbdf);
     }
     else
         return;
 
-    debug_port = pci_conf_read32(PCI_SBDF(0, dbgp->bus, dbgp->slot, dbgp->func),
-                                 dbgp->cap);
+    debug_port = pci_conf_read32(dbgp->sbdf, dbgp->cap);
     dbgp->bar = (debug_port >> 29) & 0x7;
     dbgp->bar = ((dbgp->bar - 1) * 4) + PCI_BASE_ADDRESS_0;
     offset = (debug_port >> 16) & 0xfff;
@@ -1545,8 +1520,7 @@ void __init ehci_dbgp_init(void)
         return;
     }
 
-    dbgp->bar_val = bar_val = pci_conf_read32(PCI_SBDF(0, dbgp->bus, dbgp->slot,
-                                                       dbgp->func), dbgp->bar);
+    dbgp->bar_val = bar_val = pci_conf_read32(dbgp->sbdf, dbgp->bar);
     dbgp_printk("bar_val: %08x\n", bar_val);
     if ( bar_val & ~PCI_BASE_ADDRESS_MEM_MASK )
     {
@@ -1573,8 +1547,8 @@ int dbgp_op(const struct physdev_dbgp_op *op)
     case PHYSDEVOP_DBGP_BUS_UNKNOWN:
         break;
     case PHYSDEVOP_DBGP_BUS_PCI:
-        if ( op->u.pci.seg || ehci_dbgp.bus != op->u.pci.bus ||
-            PCI_DEVFN(ehci_dbgp.slot, ehci_dbgp.func) != op->u.pci.devfn )
+        if ( PCI_SBDF(op->u.pci.seg, op->u.pci.bus, op->u.pci.devfn).sbdf !=
+             ehci_dbgp.sbdf.sbdf )
     default:
             return 0;
         break;
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#33 lore raw

Re: [PATCH v3 16/18] ehci-dbgp: Use pci_sbdf_t instead of (bus, slot, func)

Quoted 3 lines · Teddy Astie

We also take the opportunity to allow the device to exist outside of segment 0 (only when specified with pci@ syntax), since it's not hardcoded anymore.

While at least the description isn't empty here, the downsides of the behavioral change aren't put out. This is even more so that now we end up with inconsistent behavior (being a downside imo), as ...

xen/drivers/char/ehci-dbgp.c static unsigned int __init find_dbgp(struct ehci_dbgp *dbgp, · line 706

Quoted 5 lines
xen/drivers/char/ehci-dbgp.c +1 -0
@@ -704,27 +703,25 @@ static unsigned int __init find_dbgp(struct ehci_dbgp *dbgp,
         {
             for ( func = 0; func < 8; func++ )
             {
+                pci_sbdf_t sbdf = PCI_SBDF(0, bus, slot, func);

... we're still limiting ourselves to segment 0 here.

Jan

#34 lore raw

PATCH v3 17/18 pci: Drop parse_pci_split{_seg}()

In reply to a message not in the archive

No user of these functions remain, collapse all the logic into the recently introduced pci_sbdf_t variants.

---
 xen/drivers/pci/pci.c | 43 +++++++------------------------------------
 xen/include/xen/pci.h | 23 ++---------------------
 2 files changed, 9 insertions(+), 57 deletions(-)
xen/drivers/pci/pci.c +7 -36
index 793545ae49..0db9ee51f0 100644
@@ -151,18 +151,7 @@ void pci_intx(const struct pci_dev *pdev, bool enable)
     pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
 }
 
-const char *__init parse_pci_split(const char *s, unsigned int *seg_p,
-                             unsigned int *bus_p, unsigned int *dev_p,
-                             unsigned int *func_p)
-{
-    bool def_seg;
-
-    return parse_pci_seg(s, seg_p, bus_p, dev_p, func_p, &def_seg);
-}
-
-const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p,
-                                 unsigned int *bus_p, unsigned int *dev_p,
-                                 unsigned int *func_p, bool *def_seg)
+const char *__init parse_pci_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg)
 {
     unsigned long seg = simple_strtoul(s, &s, 16), bus, dev, func = 0;
 
@@ -180,39 +169,21 @@ const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p,
         *def_seg = true;
     }
 
-    if ( func_p && *s == '.' )
+    if ( *s == '.' )
         func = simple_strtoul(s + 1, &s, 0);
 
-    if ( seg != (seg_p ? (u16)seg : 0) ||
+    if ( seg != (u16)seg ||
          bus != PCI_BUS(PCI_BDF(bus, 0)) ||
          dev != PCI_SLOT(PCI_DEVFN(dev, 0)) ||
          func != PCI_FUNC(PCI_DEVFN(0, func)) )
         return NULL;
 
-    if ( seg_p )
-        *seg_p = seg;
-    *bus_p = bus;
-    *dev_p = dev;
-    if ( func_p )
-        *func_p = func;
-
-    return s;
-}
-
-const char *__init parse_pci_sbdf(const char *s, pci_sbdf_t *sbdf)
-{
-    unsigned int seg, bus, dev, func;
-    const char *out = parse_pci(s, &seg, &bus, &dev, &func);
-
     *sbdf = PCI_SBDF(seg, bus, dev, func);
-    return out;
+    return s;
 }
 
-const char *__init parse_pci_sbdf_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg)
+const char *__init parse_pci(const char *s, pci_sbdf_t *sbdf)
 {
-    unsigned int seg, bus, dev, func;
-    const char *out = parse_pci_seg(s, &seg, &bus, &dev, &func, def_seg);
-
-    *sbdf = PCI_SBDF(seg, bus, dev, func);
-    return out;
+    bool def_seg;
+    return parse_pci_seg(s, sbdf, &def_seg);
 }
xen/include/xen/pci.h +2 -21
index 8e2f8a1ab7..cb14b6a47f 100644
@@ -274,27 +274,8 @@ unsigned int pci_find_ext_capability(const struct pci_dev *pdev,
 unsigned int pci_find_next_ext_capability(const struct pci_dev *pdev,
                                           unsigned int start,
                                           unsigned int cap);
-const char *parse_pci_split(const char *s, unsigned int *seg_p, unsigned int *bus_p,
-                      unsigned int *dev_p, unsigned int *func_p);
-const char *parse_pci_split_seg(const char *s, unsigned int *seg_p,
-                          unsigned int *bus_p, unsigned int *dev_p,
-                          unsigned int *func_p, bool *def_seg);
-
-const char *parse_pci_sbdf(const char *s, pci_sbdf_t *sbdf);
-const char *parse_pci_sbdf_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg);
-
-#define _parse_pci_split(a, b, c, d, e, ...) parse_pci_split(a, b, c, d, e)
-#define _parse_pci_split_seg(a, b, c, d, e, f, ...) parse_pci_split_seg(a, b, c, d, e, f)
-
-#define parse_pci(a, b, ...)                                            \
-    (count_args(__VA_ARGS__) > 0                                        \
-     ? _parse_pci_split(a, (void *)b, ##__VA_ARGS__, NULL, NULL, NULL)  \
-     :  parse_pci_sbdf(a, (void *)b))
-
-#define parse_pci_seg(a, b, c, ...)                                     \
-    (count_args(__VA_ARGS__) > 0                                        \
-     ? _parse_pci_split_seg(a, (void *)b, (void *)c, ##__VA_ARGS__, NULL, NULL, NULL, NULL) \
-     :  parse_pci_sbdf_seg(a, (void *)b, (void *)c))
+const char *parse_pci(const char *s, pci_sbdf_t *sbdf);
+const char *parse_pci_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg);
 
 #define PCI_BAR_VF      (1u << 0)
 #define PCI_BAR_LAST    (1u << 1)
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#35 lore raw

Re: [PATCH v3 17/18] pci: Drop parse_pci_split{_seg}()

Quoted 4 lines · Teddy Astie

No user of these functions remain, collapse all the logic into the recently introduced pci_sbdf_t variants.

In order to see whether the style issues introduced in patch 08 actually help here keeping the diff small, I looked at this patch first. Hence a question and a few nits here right away:

Quoted 23 lines
xen/drivers/pci/pci.c +1 -12
@@ -151,18 +151,7 @@ void pci_intx(const struct pci_dev *pdev, bool enable)
     pci_conf_write16(pdev->sbdf, PCI_COMMAND, cmd);
 }
 
-const char *__init parse_pci_split(const char *s, unsigned int *seg_p,
-                             unsigned int *bus_p, unsigned int *dev_p,
-                             unsigned int *func_p)
-{
-    bool def_seg;
-
-    return parse_pci_seg(s, seg_p, bus_p, dev_p, func_p, &def_seg);
-}
-
-const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p,
-                                 unsigned int *bus_p, unsigned int *dev_p,
-                                 unsigned int *func_p, bool *def_seg)
+const char *__init parse_pci_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg)
 {
     unsigned long seg = simple_strtoul(s, &s, 16), bus, dev, func = 0;
 

xen/drivers/pci/pci.c const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p, · line 173

Quoted 6 lines
xen/drivers/pci/pci.c +1 -1
@@ -180,39 +169,21 @@ const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p,
         *def_seg = true;
     }
 
-    if ( func_p && *s == '.' )
+    if ( *s == '.' )
         func = simple_strtoul(s + 1, &s, 0);

And the function part being optional goes away entirely silently?

xen/drivers/pci/pci.c const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p, · line 175

Quoted 2 lines
xen/drivers/pci/pci.c +1 -1
-    if ( seg != (seg_p ? (u16)seg : 0) ||
+    if ( seg != (u16)seg ||

Nit: uint16_t please when you touch this line anyway.

Quoted 14 lines
xen/drivers/pci/pci.c +0 -9
          bus != PCI_BUS(PCI_BDF(bus, 0)) ||
          dev != PCI_SLOT(PCI_DEVFN(dev, 0)) ||
          func != PCI_FUNC(PCI_DEVFN(0, func)) )
         return NULL;
 
-    if ( seg_p )
-        *seg_p = seg;
-    *bus_p = bus;
-    *dev_p = dev;
-    if ( func_p )
-        *func_p = func;
-
-    return s;
-}

xen/drivers/pci/pci.c const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p, · line 183

Quoted 10 lines
xen/drivers/pci/pci.c +1 -7
-
-const char *__init parse_pci_sbdf(const char *s, pci_sbdf_t *sbdf)
-{
-    unsigned int seg, bus, dev, func;
-    const char *out = parse_pci(s, &seg, &bus, &dev, &func);
-
     *sbdf = PCI_SBDF(seg, bus, dev, func);
-    return out;
+    return s;
 }

Nit: Blank line please ahead of a function's main "return", and ...

xen/drivers/pci/pci.c const char *__init parse_pci_split_seg(const char *s, unsigned int *seg_p, · line 189

Quoted 11 lines
xen/drivers/pci/pci.c +3 -6
-const char *__init parse_pci_sbdf_seg(const char *s, pci_sbdf_t *sbdf, bool *def_seg)
+const char *__init parse_pci(const char *s, pci_sbdf_t *sbdf)
 {
-    unsigned int seg, bus, dev, func;
-    const char *out = parse_pci_seg(s, &seg, &bus, &dev, &func, def_seg);
-
-    *sbdf = PCI_SBDF(seg, bus, dev, func);
-    return out;
+    bool def_seg;
+    return parse_pci_seg(s, sbdf, &def_seg);
 }

... more generally between declartion(s) and statement(s).

Jan

#36 lore raw

RFC PATCH v3 18/18 : docs: Document support for PCI segment in dbgp and comN parameter

In reply to a message not in the archive

Now that we accept full PCI SBDF (with segments), update the documentation accordingly.

---
In principle, it's correct, but I don't think ECAM is usable at the moment EHCI dbgp is initialized, which would prevent the device from being probed correctly if it's not on segment 0.

Same remark for comN.

---
 docs/misc/xen-command-line.pandoc | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
docs/misc/xen-command-line.pandoc +8 -8
index 1c711fa980..3b35a1962c 100644
@@ -349,7 +349,7 @@ ACPI indicating none to be there.
 
 ### com1 (x86)
 ### com2 (x86)
-> `= <baud>[/<base-baud>][,[DPS][,[<io-base>|pci|amt][,[<irq>|msi][,[<port-bdf>][,[<bridge-bdf>]]]]]]`
+> `= <baud>[/<base-baud>][,[DPS][,[<io-base>|pci|amt][,[<irq>|msi][,[<port-sbdf>][,[<bridge-sbdf>]]]]]]`
 
 Both option `com1` and `com2` follow the same format.
 
@@ -371,10 +371,10 @@ Both option `com1` and `com2` follow the same format.
   registers.
 * `<irq>` is the IRQ number to use, or `0` to use the UART in poll
   mode only, or `msi` to set up a Message Signaled Interrupt.
-* `<port-bdf>` is the PCI location of the UART, in
-  `<bus>:<device>.<function>` notation.
-* `<bridge-bdf>` is the PCI bridge behind which is the UART, in
-  `<bus>:<device>.<function>` notation.
+* `<port-sbdf>` is the PCI location of the UART, in
+  `[<seg>:]<bus>:<device>.<function>` notation.
+* `<bridge-sbdf>` is the PCI bridge behind which is the UART, in
+  `[<seg>:]<bus>:<device>.<function>` notation.
 * `pci` indicates that Xen should scan the PCI bus for the UART,
   avoiding Intel AMT devices.
 * `amt` indicated that Xen should scan the PCI bus for the UART,
@@ -731,11 +731,11 @@ for the `all` value. If that isn't intended, raise
 the `sched_credit2_max_cpus_runqueue` value.
 
 ### dbgp
-> `= ehci[ <integer> | @pci<bus>:<slot>.<func> ]`
-> `= xhci[ <integer> | @pci<bus>:<slot>.<func> ][,share=<bool>|hwdom]`
+> `= ehci[ <integer> | @pci[<seg>:]<bus>:<slot>.<func> ]`
+> `= xhci[ <integer> | @pci[<seg>:]<bus>:<slot>.<func> ][,share=<bool>|hwdom]`
 
 Specify the USB controller to use, either by instance number (when going
-over the PCI busses sequentially) or by PCI device (must be on segment 0).
+over the PCI busses sequentially in segment 0) or by PCI device.
 
 Use `ehci` for EHCI debug port, use `xhci` for XHCI debug capability.
 XHCI driver will wait indefinitely for the debug host to connect - make sure
Signature · 10 lines
2.54.0



--
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#37 lore raw

Re: [RFC PATCH v3 18/18]: docs: Document support for PCI segment in dbgp and comN parameter

Quoted 8 lines · Teddy Astie

Now that we accept full PCI SBDF (with segments), update the documentation accordingly.

---
In principle, it's correct, but I don't think ECAM is usable at the moment EHCI dbgp is initialized, which would prevent the device from being probed correctly if it's not on segment 0.

And if there's no other way to access extended config space (like AMD offers, for example). In any event, imo this restriction wants calling out in all affected cases.

The doc updates imo also want merging into respective patches.

Quoted 1 line

Same remark for comN.

And for XHCI.

Quoted 16 lines
docs/misc/xen-command-line.pandoc +1 -2
@@ -349,7 +349,7 @@ ACPI indicating none to be there.
 
 ### com1 (x86)
 ### com2 (x86)
-> `= <baud>[/<base-baud>][,[DPS][,[<io-base>|pci|amt][,[<irq>|msi][,[<port-bdf>][,[<bridge-bdf>]]]]]]`
+> `= <baud>[/<base-baud>][,[DPS][,[<io-base>|pci|amt][,[<irq>|msi][,[<port-sbdf>][,[<bridge-sbdf>]]]]]]`
 
 Both option `com1` and `com2` follow the same format.
 
@@ -371,10 +371,10 @@ Both option `com1` and `com2` follow the same format.
   registers.
 * `<irq>` is the IRQ number to use, or `0` to use the UART in poll
   mode only, or `msi` to set up a Message Signaled Interrupt.
-* `<port-bdf>` is the PCI location of the UART, in

docs/misc/xen-command-line.pandoc Both option `com1` and `com2` follow the same format. · line 380

Quoted 10 lines
docs/misc/xen-command-line.pandoc +4 -3
-  `<bus>:<device>.<function>` notation.
-* `<bridge-bdf>` is the PCI bridge behind which is the UART, in
-  `<bus>:<device>.<function>` notation.
+* `<port-sbdf>` is the PCI location of the UART, in
+  `[<seg>:]<bus>:<device>.<function>` notation.
+* `<bridge-sbdf>` is the PCI bridge behind which is the UART, in
+  `[<seg>:]<bus>:<device>.<function>` notation.
 * `pci` indicates that Xen should scan the PCI bus for the UART,
   avoiding Intel AMT devices.
 * `amt` indicated that Xen should scan the PCI bus for the UART,

Instead of explaining how <sbdf> is spelled both here and ...

docs/misc/xen-command-line.pandoc for the `all` value. If that isn't intended, raise · line 735

Quoted 8 lines
docs/misc/xen-command-line.pandoc +2 -2
@@ -731,11 +731,11 @@ for the `all` value. If that isn't intended, raise
 the `sched_credit2_max_cpus_runqueue` value.
 
 ### dbgp
-> `= ehci[ <integer> | @pci<bus>:<slot>.<func> ]`
-> `= xhci[ <integer> | @pci<bus>:<slot>.<func> ][,share=<bool>|hwdom]`
+> `= ehci[ <integer> | @pci[<seg>:]<bus>:<slot>.<func> ]`
+> `= xhci[ <integer> | @pci[<seg>:]<bus>:<slot>.<func> ][,share=<bool>|hwdom]`

... here, maybe simply say <sbdf> in all places and add text to the top of the file?

Jan