Skip to contentSkip to conversation

pci: Compare function number and ARI next function number

PATCH In review Akihiko Odaki 3 participants 17 messages Jul 1, 2023 – Jul 11, 2023

lore
Share & tools
lore
#0 lore raw

cover pci: Compare function number and ARI next function number

The function number must be lower than the next function number advertised with ARI. Add a check to enforce this.

I suggested this change at:
https://lore.kernel.org/qemu-devel/[email protected]/

Implementing this change, I found the devices implementing ARI do not set the correct next function numbers, which is also fixed in this series.

Akihiko Odaki (4):
docs: Fix next function numbers in SR/IOV documentation hw/nvme: Fix ARI next function numbers
igb: Fix ARI next function numbers
pci: Compare function number and ARI next function number

 docs/pcie_sriov.txt |  5 +++--
 hw/net/igb_core.h   |  3 +++
 hw/net/igb.c        |  4 +---
 hw/net/igbvf.c      |  5 ++++-
 hw/nvme/ctrl.c      |  7 ++++++-
 hw/pci/pci.c        | 15 +++++++++++++++
 6 files changed, 32 insertions(+), 7 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/4 docs: Fix next function numbers in SR/IOV documentation

The next function numbers are expected to form a linked list ending with 0.

Fixes 2503461691 ("pcie: Add some SR/IOV API documentation in docs/pcie_sriov.txt")
---
 docs/pcie_sriov.txt | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
docs/pcie_sriov.txt +3 -2
index 7eff7f2703..cc4232e49a 100644
@@ -48,7 +48,7 @@ setting up a BAR for a VF.
       ...
       int ret = pcie_endpoint_cap_init(d, 0x70);
       ...
-      pcie_ari_init(d, 0x100, 1);
+      pcie_ari_init(d, 0x100, fun_offset);
       ...
 
       /* Add and initialize the SR/IOV capability */
@@ -76,9 +76,10 @@ setting up a BAR for a VF.
    pci_your_vf_dev_realize( ... )
    {
       ...
+      uint16_t nextvfn = pcie_sriov_vf_number(dev) + 1;
       int ret = pcie_endpoint_cap_init(d, 0x60);
       ...
-      pcie_ari_init(d, 0x100, 1);
+      pcie_ari_init(d, 0x100, nextvfn < total_vfs ? fun_offset + nextvfn * stride : 0);
       ...
       memory_region_init(mr, ... )
       pcie_sriov_vf_register_bar(d, bar_nr, mr);
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH 1/4] docs: Fix next function numbers in SR/IOV documentation

Quoted 27 lines · Akihiko Odaki

The next function numbers are expected to form a linked list ending with 0.

Fixes 2503461691 ("pcie: Add some SR/IOV API documentation in docs/pcie_sriov.txt")

---
docs/pcie_sriov.txt | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
docs/pcie_sriov.txt +1 -1
index 7eff7f2703..cc4232e49a 100644
@@ -48,7 +48,7 @@ setting up a BAR for a VF.
...
      int ret = pcie_endpoint_cap_init(d, 0x70);
...
-      pcie_ari_init(d, 0x100, 1);
+      pcie_ari_init(d, 0x100, fun_offset);
...
 
      /* Add and initialize the SR/IOV capability */

docs/pcie_sriov.txt setting up a BAR for a VF. · line 82

Quoted 8 lines
docs/pcie_sriov.txt +2 -1
@@ -76,9 +76,10 @@ setting up a BAR for a VF.
   pci_your_vf_dev_realize( ... )
   {
...
+      uint16_t nextvfn = pcie_sriov_vf_number(dev) + 1;
      int ret = pcie_endpoint_cap_init(d, 0x60);
...
-      pcie_ari_init(d, 0x100, 1);
+      pcie_ari_init(d, 0x100, nextvfn < total_vfs ? fun_offset + nextvfn * stride : 0);
                                                                                                                                                    ^^^^

I think this will be fun_offset and not just 0
Same with the other patches ..

Quoted 6 lines
      ...
      memory_region_init(mr, ... )
      pcie_sriov_vf_register_bar(d, bar_nr, mr);
Signature · 1 line
2.41.0
#3 lore raw

Re: [PATCH 1/4] docs: Fix next function numbers in SR/IOV documentation

Quoted 36 lines · Ani Sinha
Quoted 27 lines · Akihiko Odaki

The next function numbers are expected to form a linked list ending with 0.

Fixes 2503461691 ("pcie: Add some SR/IOV API documentation in docs/pcie_sriov.txt")

---
docs/pcie_sriov.txt | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
docs/pcie_sriov.txt +1 -1
index 7eff7f2703..cc4232e49a 100644
@@ -48,7 +48,7 @@ setting up a BAR for a VF.
...
       int ret = pcie_endpoint_cap_init(d, 0x70);
...
-      pcie_ari_init(d, 0x100, 1);
+      pcie_ari_init(d, 0x100, fun_offset);
...
 
       /* Add and initialize the SR/IOV capability */

docs/pcie_sriov.txt setting up a BAR for a VF. · line 82

Quoted 8 lines
docs/pcie_sriov.txt +2 -1
@@ -76,9 +76,10 @@ setting up a BAR for a VF.
    pci_your_vf_dev_realize( ... )
    {
...
+      uint16_t nextvfn = pcie_sriov_vf_number(dev) + 1;
       int ret = pcie_endpoint_cap_init(d, 0x60);
...
-      pcie_ari_init(d, 0x100, 1);
+      pcie_ari_init(d, 0x100, nextvfn < total_vfs ? fun_offset + nextvfn * stride : 0);
                                                                                                                                                     ^^^^
Quoted 2 lines

I think this will be fun_offset and not just 0
Same with the other patches ..

It is intended to point to the PF. fun_offset points to the first VF.

Quoted 8 lines
Quoted 7 lines
       ...
       memory_region_init(mr, ... )
       pcie_sriov_vf_register_bar(d, bar_nr, mr);
Signature · 1 line
2.41.0
#4 lore raw

PATCH 2/4 hw/nvme: Fix ARI next function numbers

In reply to Akihiko Odaki

The next function numbers are expected to form a linked list ending with 0.

Fixes 44c2c09488 ("hw/nvme: Add support for SR-IOV")
---
 hw/nvme/ctrl.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
hw/nvme/ctrl.c +6 -1
index fd917fcda1..12500dc80b 100644
@@ -8088,7 +8088,12 @@ static bool nvme_init_pci(NvmeCtrl *n, PCIDevice *pci_dev, Error **errp)
     pcie_endpoint_cap_init(pci_dev, 0x80);
     pcie_cap_flr_init(pci_dev);
     if (n->params.sriov_max_vfs) {
-        pcie_ari_init(pci_dev, 0x100, 1);
+        uint16_t nextvfn = pci_is_vf(pci_dev) ?
+                           pcie_sriov_vf_number(pci_dev) + 1 : 0;
+        uint16_t nextfn = nextvfn < n->params.sriov_max_vfs ?
+                          NVME_VF_OFFSET + nextvfn * NVME_VF_STRIDE : 0;
+
+        pcie_ari_init(pci_dev, 0x100, nextfn);
     }
 
     /* add one to max_ioqpairs to account for the admin queue pair */
Signature · 1 line
2.41.0
#5 lore raw

PATCH 3/4 igb: Fix ARI next function numbers

In reply to Akihiko Odaki

The next function numbers are expected to form a linked list ending with 0.

Fixes 3a977deebe ("Intrdocue igb device emulation")
---
 hw/net/igb_core.h | 3 +++
 hw/net/igb.c      | 4 +---
 hw/net/igbvf.c    | 5 ++++-
 3 files changed, 8 insertions(+), 4 deletions(-)
hw/net/igb_core.h +3 -0
index 9cbbfd516b..e1dab76995 100644
@@ -49,6 +49,9 @@
 #define IGB_NUM_QUEUES          (16)
 #define IGB_NUM_VM_POOLS        (8)
 
+#define IGB_VF_OFFSET           (0x80)
+#define IGB_VF_STRIDE           (2)
+
 typedef struct IGBCore IGBCore;
 
 enum { PHY_R = BIT(0),
hw/net/igb.c +1 -3
index 1c989d7677..543ca0114a 100644
@@ -81,8 +81,6 @@ struct IGBState {
 };
 
 #define IGB_CAP_SRIOV_OFFSET    (0x160)
-#define IGB_VF_OFFSET           (0x80)
-#define IGB_VF_STRIDE           (2)
 
 #define E1000E_MMIO_IDX     0
 #define E1000E_FLASH_IDX    1
@@ -431,7 +429,7 @@ static void igb_pci_realize(PCIDevice *pci_dev, Error **errp)
         hw_error("Failed to initialize AER capability");
     }
 
-    pcie_ari_init(pci_dev, 0x150, 1);
+    pcie_ari_init(pci_dev, 0x150, IGB_VF_OFFSET);
 
     pcie_sriov_pf_init(pci_dev, IGB_CAP_SRIOV_OFFSET, TYPE_IGBVF,
         IGB_82576_VF_DEV_ID, IGB_MAX_VF_FUNCTIONS, IGB_MAX_VF_FUNCTIONS,
hw/net/igbvf.c +4 -1
index 284ea61184..bf2f237ab5 100644
@@ -240,6 +240,9 @@ static const MemoryRegionOps mmio_ops = {
 static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
 {
     IgbVfState *s = IGBVF(dev);
+    uint16_t nextvfn = pcie_sriov_vf_number(dev) + 1;
+    uint16_t nextfn = nextvfn < IGB_MAX_VF_FUNCTIONS ?
+                      IGB_VF_OFFSET + nextvfn * IGB_VF_STRIDE : 0;
     int ret;
     int i;
 
@@ -270,7 +273,7 @@ static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
         hw_error("Failed to initialize AER capability");
     }
 
-    pcie_ari_init(dev, 0x150, 1);
+    pcie_ari_init(dev, 0x150, nextfn);
 }
 
 static void igbvf_pci_uninit(PCIDevice *dev)
Signature · 1 line
2.41.0
#6 lore raw

Re: [PATCH 3/4] igb: Fix ARI next function numbers

Quoted 39 lines · Akihiko Odaki

The next function numbers are expected to form a linked list ending with 0.

Fixes 3a977deebe ("Intrdocue igb device emulation")
---
 hw/net/igb_core.h | 3 +++
 hw/net/igb.c      | 4 +---
 hw/net/igbvf.c    | 5 ++++-
 3 files changed, 8 insertions(+), 4 deletions(-)
hw/net/igb_core.h +3 -0
index 9cbbfd516b..e1dab76995 100644
@@ -49,6 +49,9 @@
 #define IGB_NUM_QUEUES          (16)
 #define IGB_NUM_VM_POOLS        (8)
 
+#define IGB_VF_OFFSET           (0x80)
+#define IGB_VF_STRIDE           (2)
+
 typedef struct IGBCore IGBCore;
 
 enum { PHY_R = BIT(0),
hw/net/igb.c +0 -2
index 1c989d7677..543ca0114a 100644
@@ -81,8 +81,6 @@ struct IGBState {
 };
 
 #define IGB_CAP_SRIOV_OFFSET    (0x160)
-#define IGB_VF_OFFSET           (0x80)
-#define IGB_VF_STRIDE           (2)
 
 #define E1000E_MMIO_IDX     0
 #define E1000E_FLASH_IDX    1

hw/net/igb.c static void igb_pci_realize(PCIDevice *pci_dev, Error **errp) · line 435

Quoted 8 lines
hw/net/igb.c +1 -1
@@ -431,7 +429,7 @@ static void igb_pci_realize(PCIDevice *pci_dev, Error **errp)
         hw_error("Failed to initialize AER capability");
     }
 
-    pcie_ari_init(pci_dev, 0x150, 1);
+    pcie_ari_init(pci_dev, 0x150, IGB_VF_OFFSET);
 
     pcie_sriov_pf_init(pci_dev, IGB_CAP_SRIOV_OFFSET, TYPE_IGBVF,
         IGB_82576_VF_DEV_ID, IGB_MAX_VF_FUNCTIONS, IGB_MAX_VF_FUNCTIONS,

I think this change would break migrations from 8.0. No?

More importantly your commit log says linked list should end with 0, but you make it point at a VF instead.

Quoted 15 lines
hw/net/igbvf.c +3 -0
index 284ea61184..bf2f237ab5 100644
@@ -240,6 +240,9 @@ static const MemoryRegionOps mmio_ops = {
 static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
 {
     IgbVfState *s = IGBVF(dev);
+    uint16_t nextvfn = pcie_sriov_vf_number(dev) + 1;
+    uint16_t nextfn = nextvfn < IGB_MAX_VF_FUNCTIONS ?
+                      IGB_VF_OFFSET + nextvfn * IGB_VF_STRIDE : 0;
     int ret;
     int i;
 

hw/net/igbvf.c static void igbvf_pci_realize(PCIDevice *dev, Error **errp) · line 276

Quoted 5 lines
hw/net/igbvf.c +1 -1
@@ -270,7 +273,7 @@ static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
         hw_error("Failed to initialize AER capability");
     }
 
-    pcie_ari_init(dev, 0x150, 1);
+    pcie_ari_init(dev, 0x150, nextfn);

For this one I don't see why it matters at all:

The presence of Shadow Functions does not affect this field.
For VFs, this field is undefined since VFs are located using First VF Offset (see § Section 9.3.3.9 ) and VF Stride (see § Section 9.3.3.10 ).

Quoted 5 lines
hw/net/igbvf.c +0 -0
 }
 
 static void igbvf_pci_uninit(PCIDevice *dev)
Signature · 1 line
2.41.0
#7 lore raw

Re: [PATCH 3/4] igb: Fix ARI next function numbers

Quoted 43 lines · Michael S. Tsirkin
Quoted 42 lines · Akihiko Odaki

The next function numbers are expected to form a linked list ending with 0.

Fixes 3a977deebe ("Intrdocue igb device emulation")
---
  hw/net/igb_core.h | 3 +++
  hw/net/igb.c      | 4 +---
  hw/net/igbvf.c    | 5 ++++-
  3 files changed, 8 insertions(+), 4 deletions(-)
hw/net/igb_core.h +3 -0
index 9cbbfd516b..e1dab76995 100644
@@ -49,6 +49,9 @@
  #define IGB_NUM_QUEUES          (16)
  #define IGB_NUM_VM_POOLS        (8)
 
+#define IGB_VF_OFFSET           (0x80)
+#define IGB_VF_STRIDE           (2)
+
  typedef struct IGBCore IGBCore;
 
  enum { PHY_R = BIT(0),
hw/net/igb.c +0 -2
index 1c989d7677..543ca0114a 100644
@@ -81,8 +81,6 @@ struct IGBState {
  };
 
  #define IGB_CAP_SRIOV_OFFSET    (0x160)
-#define IGB_VF_OFFSET           (0x80)
-#define IGB_VF_STRIDE           (2)
 
  #define E1000E_MMIO_IDX     0
  #define E1000E_FLASH_IDX    1
@@ -431,7 +429,7 @@ static void igb_pci_realize(PCIDevice *pci_dev, Error **errp)
          hw_error("Failed to initialize AER capability");
      }
 
Quoted 8 lines

hw/net/igb.c static void igb_pci_realize(PCIDevice *pci_dev, Error **errp) · line 435

Quoted 5 lines
hw/net/igb.c +1 -1
-    pcie_ari_init(pci_dev, 0x150, 1);
+    pcie_ari_init(pci_dev, 0x150, IGB_VF_OFFSET);
 
      pcie_sriov_pf_init(pci_dev, IGB_CAP_SRIOV_OFFSET, TYPE_IGBVF,
          IGB_82576_VF_DEV_ID, IGB_MAX_VF_FUNCTIONS, IGB_MAX_VF_FUNCTIONS,

I think this change would break migrations from 8.0. No?

Well, I don't have a reason to concern more for this change than other bug fixes with behavioral changes observable from the guest.

Quoted 31 lines

More importantly your commit log says linked list should end with 0, but you make it point at a VF instead.

Quoted 15 lines
hw/net/igbvf.c +3 -0
index 284ea61184..bf2f237ab5 100644
@@ -240,6 +240,9 @@ static const MemoryRegionOps mmio_ops = {
  static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
  {
      IgbVfState *s = IGBVF(dev);
+    uint16_t nextvfn = pcie_sriov_vf_number(dev) + 1;
+    uint16_t nextfn = nextvfn < IGB_MAX_VF_FUNCTIONS ?
+                      IGB_VF_OFFSET + nextvfn * IGB_VF_STRIDE : 0;
      int ret;
      int i;
 

hw/net/igbvf.c static void igbvf_pci_realize(PCIDevice *dev, Error **errp) · line 276

Quoted 5 lines
hw/net/igbvf.c +1 -1
@@ -270,7 +273,7 @@ static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
          hw_error("Failed to initialize AER capability");
      }
 
-    pcie_ari_init(dev, 0x150, 1);
+    pcie_ari_init(dev, 0x150, nextfn);

For this one I don't see why it matters at all:

Quoted 3 lines

The presence of Shadow Functions does not affect this field.
For VFs, this field is undefined since VFs are located using First VF Offset (see § Section 9.3.3.9 ) and VF Stride (see § Section 9.3.3.10 ).

I missed the statements saying the field is undefined for VFs. I posted an alternative series ("[PATCH 0/3] pci: Fix ARI next function numbers") so please review it.

Quoted 10 lines
Quoted 9 lines
hw/net/igbvf.c +0 -0
  }
 
  static void igbvf_pci_uninit(PCIDevice *dev)
Signature · 1 line
2.41.0
#8 lore raw

Re: [PATCH 3/4] igb: Fix ARI next function numbers

Quoted 47 lines · Akihiko Odaki
Quoted 37 lines · Michael S. Tsirkin
Quoted 36 lines · Akihiko Odaki

The next function numbers are expected to form a linked list ending with 0.

Fixes 3a977deebe ("Intrdocue igb device emulation")
---
  hw/net/igb_core.h | 3 +++
  hw/net/igb.c      | 4 +---
  hw/net/igbvf.c    | 5 ++++-
  3 files changed, 8 insertions(+), 4 deletions(-)
hw/net/igb_core.h +3 -0
index 9cbbfd516b..e1dab76995 100644
@@ -49,6 +49,9 @@
  #define IGB_NUM_QUEUES          (16)
  #define IGB_NUM_VM_POOLS        (8)
+#define IGB_VF_OFFSET           (0x80)
+#define IGB_VF_STRIDE           (2)
+
  typedef struct IGBCore IGBCore;
  enum { PHY_R = BIT(0),
hw/net/igb.c +0 -2
index 1c989d7677..543ca0114a 100644
@@ -81,8 +81,6 @@ struct IGBState {
  };
  #define IGB_CAP_SRIOV_OFFSET    (0x160)
-#define IGB_VF_OFFSET           (0x80)
-#define IGB_VF_STRIDE           (2)
  #define E1000E_MMIO_IDX     0
  #define E1000E_FLASH_IDX    1
@@ -431,7 +429,7 @@ static void igb_pci_realize(PCIDevice *pci_dev, Error **errp)
          hw_error("Failed to initialize AER capability");
Quoted 8 lines

hw/net/igb.c static void igb_pci_realize(PCIDevice *pci_dev, Error **errp) · line 435

Quoted 5 lines
hw/net/igb.c +1 -1
      }
-    pcie_ari_init(pci_dev, 0x150, 1);
+    pcie_ari_init(pci_dev, 0x150, IGB_VF_OFFSET);
      pcie_sriov_pf_init(pci_dev, IGB_CAP_SRIOV_OFFSET, TYPE_IGBVF,
          IGB_82576_VF_DEV_ID, IGB_MAX_VF_FUNCTIONS, IGB_MAX_VF_FUNCTIONS,

I think this change would break migrations from 8.0. No?

Quoted 2 lines

Well, I don't have a reason to concern more for this change than other bug fixes with behavioral changes observable from the guest.

Try it and see it fail.

Quoted 46 lines
Quoted 29 lines

More importantly your commit log says linked list should end with 0, but you make it point at a VF instead.

Quoted 14 lines
hw/net/igbvf.c +3 -0
index 284ea61184..bf2f237ab5 100644
@@ -240,6 +240,9 @@ static const MemoryRegionOps mmio_ops = {
  static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
  {
      IgbVfState *s = IGBVF(dev);
+    uint16_t nextvfn = pcie_sriov_vf_number(dev) + 1;
+    uint16_t nextfn = nextvfn < IGB_MAX_VF_FUNCTIONS ?
+                      IGB_VF_OFFSET + nextvfn * IGB_VF_STRIDE : 0;
      int ret;
      int i;

hw/net/igbvf.c static void igbvf_pci_realize(PCIDevice *dev, Error **errp) · line 275

Quoted 4 lines
hw/net/igbvf.c +1 -1
@@ -270,7 +273,7 @@ static void igbvf_pci_realize(PCIDevice *dev, Error **errp)
          hw_error("Failed to initialize AER capability");
      }
-    pcie_ari_init(dev, 0x150, 1);
+    pcie_ari_init(dev, 0x150, nextfn);

For this one I don't see why it matters at all:

Quoted 3 lines

The presence of Shadow Functions does not affect this field.
For VFs, this field is undefined since VFs are located using First VF Offset (see § Section 9.3.3.9 ) and VF Stride (see § Section 9.3.3.10 ).

I missed the statements saying the field is undefined for VFs. I posted an alternative series ("[PATCH 0/3] pci: Fix ARI next function numbers") so please review it.

Quoted 9 lines
Quoted 8 lines
hw/net/igbvf.c +0 -0
  }
  static void igbvf_pci_uninit(PCIDevice *dev)
Signature · 1 line
2.41.0
#9 lore raw

PATCH 4/4 pci: Compare function number and ARI next function number

In reply to Akihiko Odaki

The function number must be lower than the next function number advertised with ARI.

---
 hw/pci/pci.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
hw/pci/pci.c +15 -0
index e2eb4c3b4a..568665ee42 100644
@@ -2059,6 +2059,8 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
     Error *local_err = NULL;
     bool is_default_rom;
     uint16_t class_id;
+    uint16_t ari;
+    uint16_t nextfn;
 
     /*
      * capped by systemd (see: udev-builtin-net_id.c)
@@ -2121,6 +2123,19 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
         }
     }
 
+    if (pci_is_express(pci_dev)) {
+        ari = pcie_find_capability(pci_dev, PCI_EXT_CAP_ID_ARI);
+        if (ari) {
+            nextfn = (pci_get_long(pci_dev->config + ari + PCI_ARI_CAP) >> 8) & 0xff;
+            if (nextfn && (pci_dev->devfn & 0xff) >= nextfn) {
+                error_setg(errp, "PCI: function number %u is not lower than ARI next function number %u",
+                           pci_dev->devfn & 0xff, nextfn);
+                pci_qdev_unrealize(DEVICE(pci_dev));
+                return;
+            }
+        }
+    }
+
     if (pci_dev->failover_pair_id) {
         if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
             error_setg(errp, "failover primary device must be on "
Signature · 1 line
2.41.0
#10 lore raw
Quoted 4 lines · Akihiko Odaki

The function number must be lower than the next function number advertised with ARI.

I don't get this logic at all - where is the limitation coming from?

All I see in the spec is:
Next Function Number - With non-VFs, this field indicates the Function Number of the next higher numbered Function in the Device, or 00h if there are no higher numbered Functions. Function 0 starts this linked list of Functions.
The presence of Shadow Functions does not affect this field.
For VFs, this field is undefined since VFs are located using First VF Offset (see § Section 9.3.3.9 ) and VF Stride (see § Section 9.3.3.10 ).

and

To improve the enumeration performance and create a more deterministic solution, software can
enumerate Functions through a linked list of Function Numbers. The next linked list element is communicated through each Function’s ARI Capability Register.
i. Function 0 acts as the head of a linked list of Function Numbers. Software detects a non-Zero Next Function Number field within the ARI Capability Register as the next Function within the linked list. Software issues a configuration probe using the Bus Number captured by the Device and the Function Number derived from the ARI Capability Register to locate the next associated Function’s configuration space.
ii. Function Numbers may be sparse and non-sequential in their consumption by an ARI Device.

Quoted 39 lines
---
 hw/pci/pci.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
hw/pci/pci.c +15 -0
index e2eb4c3b4a..568665ee42 100644
@@ -2059,6 +2059,8 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
     Error *local_err = NULL;
     bool is_default_rom;
     uint16_t class_id;
+    uint16_t ari;
+    uint16_t nextfn;
 
     /*
      * capped by systemd (see: udev-builtin-net_id.c)
@@ -2121,6 +2123,19 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
         }
     }
 
+    if (pci_is_express(pci_dev)) {
+        ari = pcie_find_capability(pci_dev, PCI_EXT_CAP_ID_ARI);
+        if (ari) {
+            nextfn = (pci_get_long(pci_dev->config + ari + PCI_ARI_CAP) >> 8) & 0xff;
+            if (nextfn && (pci_dev->devfn & 0xff) >= nextfn) {
+                error_setg(errp, "PCI: function number %u is not lower than ARI next function number %u",
+                           pci_dev->devfn & 0xff, nextfn);
+                pci_qdev_unrealize(DEVICE(pci_dev));
+                return;
+            }
+        }
+    }
+
     if (pci_dev->failover_pair_id) {
         if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
             error_setg(errp, "failover primary device must be on "
Signature · 1 line
2.41.0
#11 lore raw
Quoted 18 lines · Michael S. Tsirkin
Quoted 4 lines · Akihiko Odaki

The function number must be lower than the next function number advertised with ARI.

I don't get this logic at all - where is the limitation coming from?

All I see in the spec is:
Next Function Number - With non-VFs, this field indicates the Function Number of the next higher numbered Function in the Device, or 00h if there are no higher numbered Functions. Function 0 starts this linked list of Functions.
The presence of Shadow Functions does not affect this field.
For VFs, this field is undefined since VFs are located using First VF Offset (see § Section 9.3.3.9 ) and VF Stride (see § Section 9.3.3.10 ).

and

Quoted 10 lines

To improve the enumeration performance and create a more deterministic solution, software can
enumerate Functions through a linked list of Function Numbers. The next linked list element is communicated through each Function’s ARI Capability Register.
i. Function 0 acts as the head of a linked list of Function Numbers. Software detects a non-Zero Next Function Number field within the ARI Capability Register as the next Function within the linked list. Software issues a configuration probe using the Bus Number captured by the Device and the Function Number derived from the ARI Capability Register to locate the next associated Function’s configuration space.
ii. Function Numbers may be sparse and non-sequential in their consumption by an ARI Device.

The statement "With non-VFs, this field indicates the Function Number of the next higher numbered Function in the Device, or 00h if there are no higher numbered Functions." implies the Function Number of the device should be lower than the value advertised by the field (for non-VFs; this patch does not check if it's VF or not.)

Quoted 45 lines
Quoted 44 lines




---
  hw/pci/pci.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
hw/pci/pci.c +15 -0
index e2eb4c3b4a..568665ee42 100644
@@ -2059,6 +2059,8 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
      Error *local_err = NULL;
      bool is_default_rom;
      uint16_t class_id;
+    uint16_t ari;
+    uint16_t nextfn;
 
      /*
       * capped by systemd (see: udev-builtin-net_id.c)
@@ -2121,6 +2123,19 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
          }
      }
 
+    if (pci_is_express(pci_dev)) {
+        ari = pcie_find_capability(pci_dev, PCI_EXT_CAP_ID_ARI);
+        if (ari) {
+            nextfn = (pci_get_long(pci_dev->config + ari + PCI_ARI_CAP) >> 8) & 0xff;
+            if (nextfn && (pci_dev->devfn & 0xff) >= nextfn) {
+                error_setg(errp, "PCI: function number %u is not lower than ARI next function number %u",
+                           pci_dev->devfn & 0xff, nextfn);
+                pci_qdev_unrealize(DEVICE(pci_dev));
+                return;
+            }
+        }
+    }
+
      if (pci_dev->failover_pair_id) {
          if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
              error_setg(errp, "failover primary device must be on "
Signature · 1 line
2.41.0
#12 lore raw
Quoted 30 lines · Akihiko Odaki
Quoted 18 lines · Michael S. Tsirkin
Quoted 4 lines · Akihiko Odaki

The function number must be lower than the next function number advertised with ARI.

I don't get this logic at all - where is the limitation coming from?

All I see in the spec is:
Next Function Number - With non-VFs, this field indicates the Function Number of the next higher numbered Function in the Device, or 00h if there are no higher numbered Functions. Function 0 starts this linked list of Functions.
The presence of Shadow Functions does not affect this field.
For VFs, this field is undefined since VFs are located using First VF Offset (see § Section 9.3.3.9 ) and VF Stride (see § Section 9.3.3.10 ).

and

Quoted 10 lines

To improve the enumeration performance and create a more deterministic solution, software can
enumerate Functions through a linked list of Function Numbers. The next linked list element is communicated through each Function’s ARI Capability Register.
i. Function 0 acts as the head of a linked list of Function Numbers. Software detects a non-Zero Next Function Number field within the ARI Capability Register as the next Function within the linked list. Software issues a configuration probe using the Bus Number captured by the Device and the Function Number derived from the ARI Capability Register to locate the next associated Function’s configuration space.
ii. Function Numbers may be sparse and non-sequential in their consumption by an ARI Device.

Quoted 5 lines

The statement "With non-VFs, this field indicates the Function Number of the next higher numbered Function in the Device, or 00h if there are no higher numbered Functions." implies the Function Number of the device should be lower than the value advertised by the field (for non-VFs; this patch does not check if it's VF or not.)

Now I get it. Good point! I'd say if we want this check we should add it in pcie_ari_init, making that return int.
But for now it's dead code since your are changing it to 0. So maybe a comment in pcie_ari_init is enough:

/*
* Note: nextfn must be the Function Number of the
* next higher numbered Function in the Device, or 00h if there are no higher
* numbered Functions.
* TODO: validate this.
*/

Quoted 43 lines
Quoted 43 lines
Quoted 42 lines




---
  hw/pci/pci.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
hw/pci/pci.c +15 -0
index e2eb4c3b4a..568665ee42 100644
@@ -2059,6 +2059,8 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
      Error *local_err = NULL;
      bool is_default_rom;
      uint16_t class_id;
+    uint16_t ari;
+    uint16_t nextfn;
      /*
       * capped by systemd (see: udev-builtin-net_id.c)
@@ -2121,6 +2123,19 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
          }
      }
+    if (pci_is_express(pci_dev)) {
+        ari = pcie_find_capability(pci_dev, PCI_EXT_CAP_ID_ARI);
+        if (ari) {
+            nextfn = (pci_get_long(pci_dev->config + ari + PCI_ARI_CAP) >> 8) & 0xff;
+            if (nextfn && (pci_dev->devfn & 0xff) >= nextfn) {
+                error_setg(errp, "PCI: function number %u is not lower than ARI next function number %u",
+                           pci_dev->devfn & 0xff, nextfn);
+                pci_qdev_unrealize(DEVICE(pci_dev));
+                return;
+            }
+        }
+    }
+
      if (pci_dev->failover_pair_id) {
          if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
              error_setg(errp, "failover primary device must be on "
Signature · 1 line
2.41.0
#13 lore raw
Quoted 43 lines · Michael S. Tsirkin
Quoted 30 lines · Akihiko Odaki
Quoted 18 lines · Michael S. Tsirkin
Quoted 4 lines · Akihiko Odaki

The function number must be lower than the next function number advertised with ARI.

I don't get this logic at all - where is the limitation coming from?

All I see in the spec is:
Next Function Number - With non-VFs, this field indicates the Function Number of the next higher numbered Function in the Device, or 00h if there are no higher numbered Functions. Function 0 starts this linked list of Functions.
The presence of Shadow Functions does not affect this field.
For VFs, this field is undefined since VFs are located using First VF Offset (see § Section 9.3.3.9 ) and VF Stride (see § Section 9.3.3.10 ).

and

Quoted 10 lines

To improve the enumeration performance and create a more deterministic solution, software can
enumerate Functions through a linked list of Function Numbers. The next linked list element is communicated through each Function’s ARI Capability Register.
i. Function 0 acts as the head of a linked list of Function Numbers. Software detects a non-Zero Next Function Number field within the ARI Capability Register as the next Function within the linked list. Software issues a configuration probe using the Bus Number captured by the Device and the Function Number derived from the ARI Capability Register to locate the next associated Function’s configuration space.
ii. Function Numbers may be sparse and non-sequential in their consumption by an ARI Device.

Quoted 5 lines

The statement "With non-VFs, this field indicates the Function Number of the next higher numbered Function in the Device, or 00h if there are no higher numbered Functions." implies the Function Number of the device should be lower than the value advertised by the field (for non-VFs; this patch does not check if it's VF or not.)

Now I get it. Good point! I'd say if we want this check we should add it in pcie_ari_init, making that return int.
But for now it's dead code since your are changing it to 0. So maybe a comment in pcie_ari_init is enough:

Quoted 6 lines

/*
* Note: nextfn must be the Function Number of the
* next higher numbered Function in the Device, or 00h if there are no higher
* numbered Functions.
* TODO: validate this.
*/

Or add an assert, and
TODO: in case this can ever come from command line, we'll have to replace the assert below with a runtime check.

Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
Quoted 42 lines




---
  hw/pci/pci.c | 15 +++++++++++++++
  1 file changed, 15 insertions(+)
hw/pci/pci.c +15 -0
index e2eb4c3b4a..568665ee42 100644
@@ -2059,6 +2059,8 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
      Error *local_err = NULL;
      bool is_default_rom;
      uint16_t class_id;
+    uint16_t ari;
+    uint16_t nextfn;
      /*
       * capped by systemd (see: udev-builtin-net_id.c)
@@ -2121,6 +2123,19 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
          }
      }
+    if (pci_is_express(pci_dev)) {
+        ari = pcie_find_capability(pci_dev, PCI_EXT_CAP_ID_ARI);
+        if (ari) {
+            nextfn = (pci_get_long(pci_dev->config + ari + PCI_ARI_CAP) >> 8) & 0xff;
+            if (nextfn && (pci_dev->devfn & 0xff) >= nextfn) {
+                error_setg(errp, "PCI: function number %u is not lower than ARI next function number %u",
+                           pci_dev->devfn & 0xff, nextfn);
+                pci_qdev_unrealize(DEVICE(pci_dev));
+                return;
+            }
+        }
+    }
+
      if (pci_dev->failover_pair_id) {
          if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
              error_setg(errp, "failover primary device must be on "
Signature · 1 line
2.41.0
#14 lore raw
In reply to Akihiko Odaki
Quoted 32 lines · Akihiko Odaki

The function number must be lower than the next function number advertised with ARI.

---
hw/pci/pci.c | 15 +++++++++++++++

1 file changed, 15 insertions(+)
hw/pci/pci.c +5 -0
index e2eb4c3b4a..568665ee42 100644
@@ -2059,6 +2059,8 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
    Error *local_err = NULL;
    bool is_default_rom;
    uint16_t class_id;
+    uint16_t ari;
+    uint16_t nextfn;
 
    /*
     * capped by systemd (see: udev-builtin-net_id.c)
@@ -2121,6 +2123,19 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
        }
    }
 
+    if (pci_is_express(pci_dev)) {
+        ari = pcie_find_capability(pci_dev, PCI_EXT_CAP_ID_ARI);
+        if (ari) {

hw/pci/pci.c static void pci_qdev_realize(DeviceState *qdev, Error **errp) · line 2138

Quoted 10 lines
hw/pci/pci.c +10 -0
+            nextfn = (pci_get_long(pci_dev->config + ari + PCI_ARI_CAP) >> 8) & 0xff;
+            if (nextfn && (pci_dev->devfn & 0xff) >= nextfn) {
+                error_setg(errp, "PCI: function number %u is not lower than ARI next function number %u",
+                           pci_dev->devfn & 0xff, nextfn);
+                pci_qdev_unrealize(DEVICE(pci_dev));
+                return;
+            }
+        }
+    }
+

So I kind of got lost in all the patches. What was the ultimate decision regarding checking this?

Quoted 6 lines
hw/pci/pci.c +0 -0
    if (pci_dev->failover_pair_id) {
        if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
            error_setg(errp, "failover primary device must be on "
Signature · 1 line
2.41.0
#15 lore raw
Quoted 29 lines · Ani Sinha
Quoted 29 lines · Akihiko Odaki

The function number must be lower than the next function number advertised with ARI.

---
hw/pci/pci.c | 15 +++++++++++++++

1 file changed, 15 insertions(+)
hw/pci/pci.c +2 -0
index e2eb4c3b4a..568665ee42 100644
@@ -2059,6 +2059,8 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
    Error *local_err = NULL;
    bool is_default_rom;
    uint16_t class_id;
+    uint16_t ari;
+    uint16_t nextfn;
 
    /*
     * capped by systemd (see: udev-builtin-net_id.c)
@@ -2121,6 +2123,19 @@ static void pci_qdev_realize(DeviceState *qdev, Error **errp)
        }
    }
 
Quoted 15 lines

hw/pci/pci.c static void pci_qdev_realize(DeviceState *qdev, Error **errp) · line 2138

Quoted 13 lines
hw/pci/pci.c +13 -0
+    if (pci_is_express(pci_dev)) {
+        ari = pcie_find_capability(pci_dev, PCI_EXT_CAP_ID_ARI);
+        if (ari) {
+            nextfn = (pci_get_long(pci_dev->config + ari + PCI_ARI_CAP) >> 8) & 0xff;
+            if (nextfn && (pci_dev->devfn & 0xff) >= nextfn) {
+                error_setg(errp, "PCI: function number %u is not lower than ARI next function number %u",
+                           pci_dev->devfn & 0xff, nextfn);
+                pci_qdev_unrealize(DEVICE(pci_dev));
+                return;
+            }
+        }
+    }
+

So I kind of got lost in all the patches. What was the ultimate decision regarding checking this?

We still need to fix ARI for multi-function PFs.
I feel the right thing to do is to init Next Function in the ARI capability, automatically.
For now, we have merely changed ARI setting next function to 0. At least that's more correct for the common case of ARI PF with VFs.

Quoted 6 lines
Quoted 6 lines
hw/pci/pci.c +0 -0
    if (pci_dev->failover_pair_id) {
        if (!pci_bus_is_express(pci_get_bus(pci_dev))) {
            error_setg(errp, "failover primary device must be on "
Signature · 1 line
2.41.0
#16 lore raw
In reply to Akihiko Odaki
Quoted 8 lines · Akihiko Odaki

The function number must be lower than the next function number advertised with ARI. Add a check to enforce this.

I suggested this change at:
https://lore.kernel.org/qemu-devel/[email protected]/

Implementing this change, I found the devices implementing ARI do not set the correct next function numbers, which is also fixed in this series.

This isn't going to be merged with more in the way of motivation.

Analysis of at least linux guest behavious, documentation about testing, addressing migration concerns are all not there either.

Quoted 16 lines

Akihiko Odaki (4):
docs: Fix next function numbers in SR/IOV documentation hw/nvme: Fix ARI next function numbers
igb: Fix ARI next function numbers
pci: Compare function number and ARI next function number

 docs/pcie_sriov.txt |  5 +++--
 hw/net/igb_core.h   |  3 +++
 hw/net/igb.c        |  4 +---
 hw/net/igbvf.c      |  5 ++++-
 hw/nvme/ctrl.c      |  7 ++++++-
 hw/pci/pci.c        | 15 +++++++++++++++
 6 files changed, 32 insertions(+), 7 deletions(-)
Signature · 1 line
2.41.0