Skip to contentSkip to conversation

pcie: Add support for Single Root I/O Virtualization

PATCH Superseded Knut Omang 2 participants 9 messages Aug 29, 2014 – Sep 1, 2014

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Knut Omang

author
#0 lore raw

cover pcie: Add support for Single Root I/O Virtualization

This patch set consists of two parts:

- The two first patches implements SR/IOV building blocks in pcie/pci. I have held this patch back for a while because I haven't had a good example test case to accompany it, but in the light of the latest developments such as the discussion we have had around ARI and downstream switches and root ports, I think it would be valuable to get this in, to make it easier for people to experiment with creating devices with many functions. Hopefully, I can also get some help to fix the hotplug issues described below.

- The two last patches contains an example to illustrate the usage of the new SR/IOV support. The example leverages the e1000
code and the fact that registers between E1000 and the PCIe based Intel 82576 Gigabit Ethernet Adapter (which supports SR/IOV) are quite similar, but so far without considering much of the differences beyond the bare minimum needed to trick the igb driver into loading to the point where VFs can be enabled.

So you cannot yet use these PF or VF Ethernet devices to send ethernet packets, and the implementation do not in any way attempt to model the internals of igb such as the multiple queues or any multiplexing onto the same device, it only instantiates the VFs as well as the PFs mostly directly by inheritance (and as separate devices) from E1000, but it should hopefully be relatively easy to understand how to proceed to make "true" VFs.
It was also a nice exercise in using QOM.

The changes to E1000 to accommodate igb (patch 3) should be fairly non-intrusive, nevertheless I suppose it should not be applied unless it will eventually lead to a new derived device which is enough different from E1000 to qualify for a separate set of source files. So if someone with more detailed knowledge of the internal differences between igb and e1000 on the functional level might have input, that would be great, an alternative of course be to only apply the two first patches, and leave any usage examples for the future.

To test and see how it plays out, you can add something like this to the command line:

     -device ioh3420,slot=0,id=pcie_port.0
     -device igb,mac=DE:AD:BE:EE:04:18,vlan=1,bus=pcie_port.0
     -net tap,vlan=1

The Linux igb driver does not yet support changing the VF setup via sysfs (the preferred way since kernel v.3.8) so to see some VFs on the guest, you need to set up a modprobe file with something like this:

options igb max_vfs=4

For some reason I dont yet understand, removal of the igb driver does not cause a 0 write to sr/iov vf_enable to disable the VFs again, eg.

       # lspci | grep '^01:00'
       01:00.0 Ethernet controller: Intel Corporation 82576 Gigabit Network Connection (rev 01)
       01:00.1 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.2 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.3 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.4 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)

Writing directly to vf_enable using setpci works though, but after the later hotplug changes, a little too well, as after the VF removals have succeded, some way a slot power down gets triggered, also removing the PF:

      # setpci -s 01:00.0 168.b
      0x19
      # setpci -s 01:00.0 168.b=18
      # lspci | grep '^01:00'
      <nothing>

For similar reasons attaching the igb device directly on the root complex (just remove bus parameter above) attempts to enable VFs will fail with

      qdev.c:89: bus_add_child: Assertion `bus->allow_hotplug' failed

I imagine this could for instance be a matter of defining a property "subfunction" or similar that allows qdev (QOM?) to discriminate between main devices and devices representing subfunctions of another main device. Suggestions on how to proceed on this welcome.

This patch depends (by diff context only) on my patch:

[PATCH v2 1/4] pcie: Fix incorrect write to the ari capability next function field

and for stability on Paolo's

[PATCH] pci_bridge: manually destroy memory regions within PCIBridgeWindows

both of which Michael has pulled, but which are not in master yet.

Thanks,

Knut Omang (4):
pci: Update pci_regs header
pcie: Add support for Single Root I/O Virtualization (SR/IOV)
e1000: Refactor to allow subclassing from other source file
igb: Example code to illustrate the SR/IOV support.

 hw/i386/kvm/pci-assign.c  |   4 +-
 hw/misc/vfio.c            |   8 +-
 hw/net/Makefile.objs      |   2 +-
 hw/net/e1000.c            | 126 ++--------------
 hw/net/e1000.h            | 139 +++++++++++++++++
 hw/net/igb.c              | 293 ++++++++++++++++++++++++++++++++++++
 hw/net/igb_regs.h         |  27 ++++
 hw/pci/msi.c              |   4 -
 hw/pci/msix.c             |   2 +-
 hw/pci/pci.c              | 107 +++++++++----
 hw/pci/pcie.c             | 205 ++++++++++++++++++++++++-
 include/hw/pci/pci.h      |   6 +-
 include/hw/pci/pci_regs.h | 371 ++++++++++++++++++++++++++++++++++------------
 include/hw/pci/pcie.h     |  26 ++++
 14 files changed, 1072 insertions(+), 248 deletions(-)
 create mode 100644 hw/net/e1000.h
 create mode 100644 hw/net/igb.c
 create mode 100644 hw/net/igb_regs.h
Signature · 1 line
1.9.0
author
#2 lore raw

PATCH 2/4 pcie: Add support for Single Root I/O Virtualization (SR/IOV)

This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and creating and removing SR/IOV Virtual Functions.

---
 hw/pci/pci.c          | 107 +++++++++++++++++++-------
 hw/pci/pcie.c         | 205 +++++++++++++++++++++++++++++++++++++++++++++++++-
 include/hw/pci/pci.h  |   6 +-
 include/hw/pci/pcie.h |  26 +++++++
 4 files changed, 311 insertions(+), 33 deletions(-)
hw/pci/pci.c +80 -27
index daeaeac..071ab81 100644
@@ -35,7 +35,6 @@
 #include "hw/pci/msi.h"
 #include "hw/pci/msix.h"
 #include "exec/address-spaces.h"
-#include "hw/hotplug.h"
 
 //#define DEBUG_PCI
 #ifdef DEBUG_PCI
@@ -126,6 +125,9 @@ static int pci_bar(PCIDevice *d, int reg)
 {
     uint8_t type;
 
+    /* PCIe virtual functions do not have their own BARs */
+    assert(!d->exp.is_vf);
+
     if (reg != PCI_ROM_SLOT)
         return PCI_BASE_ADDRESS_0 + reg * 4;
 
@@ -184,22 +186,13 @@ void pci_device_deassert_intx(PCIDevice *dev)
     }
 }
 
-static void pci_do_device_reset(PCIDevice *dev)
+static void pci_reset_regions(PCIDevice *dev)
 {
     int r;
+    if (dev->exp.is_vf) {
+        return;
+    }
 
-    pci_device_deassert_intx(dev);
-    assert(dev->irq_state == 0);
-
-    /* Clear all writable bits */
-    pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
-                                 pci_get_word(dev->wmask + PCI_COMMAND) |
-                                 pci_get_word(dev->w1cmask + PCI_COMMAND));
-    pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
-                                 pci_get_word(dev->wmask + PCI_STATUS) |
-                                 pci_get_word(dev->w1cmask + PCI_STATUS));
-    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
-    dev->config[PCI_INTERRUPT_LINE] = 0x0;
     for (r = 0; r < PCI_NUM_REGIONS; ++r) {
         PCIIORegion *region = &dev->io_regions[r];
         if (!region->size) {
@@ -213,6 +206,27 @@ static void pci_do_device_reset(PCIDevice *dev)
             pci_set_long(dev->config + pci_bar(dev, r), region->type);
         }
     }
+}
+
+static void pci_do_device_reset(PCIDevice *dev)
+{
+    qdev_reset_all(&dev->qdev);
+
+    dev->irq_state = 0;
+    pci_update_irq_status(dev);
+    pci_device_deassert_intx(dev);
+    assert(dev->irq_state == 0);
+
+    /* Clear all writable bits */
+    pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
+                                 pci_get_word(dev->wmask + PCI_COMMAND) |
+                                 pci_get_word(dev->w1cmask + PCI_COMMAND));
+    pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
+                                 pci_get_word(dev->wmask + PCI_STATUS) |
+                                 pci_get_word(dev->w1cmask + PCI_STATUS));
+    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
+    dev->config[PCI_INTERRUPT_LINE] = 0x0;
+    pci_reset_regions(dev);
     pci_update_mappings(dev);
 
     msi_reset(dev);
@@ -734,6 +748,14 @@ static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev)
         dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
     }
 
+    /* With SR/IOV and ARI, subsequent function 0's are only
+     * another VF which the physical function is placed in the initial
+     * function (0.0)
+     */
+    if (dev->exp.pf && dev->exp.pf->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
+        return 0;
+    }
+
     /*
      * multifunction bit is interpreted in two ways as follows.
      *   - all functions must set the bit to 1.
@@ -920,6 +942,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,
     uint64_t wmask;
     pcibus_t size = memory_region_size(memory);
 
+    assert(!pci_dev->exp.is_vf); /* VFs must use pcie_register_vf_bar */
     assert(region_num >= 0);
     assert(region_num < PCI_NUM_REGIONS);
     if (size & (size-1)) {
@@ -1018,18 +1041,51 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
     return pci_dev->io_regions[region_num].addr;
 }
 
-static pcibus_t pci_bar_address(PCIDevice *d,
-				int reg, uint8_t type, pcibus_t size)
+
+static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg,
+                                        uint8_t type, pcibus_t size)
+{
+    pcibus_t new_addr;
+    if (!d->exp.is_vf) {
+        int bar = pci_bar(d, reg);
+        if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
+            new_addr = pci_get_quad(d->config + bar);
+        } else {
+            new_addr = pci_get_long(d->config + bar);
+        }
+    } else {
+        int bar = d->exp.pf->exp.sriov_cap + PCI_SRIOV_BAR + reg * 4;
+        uint32_t vf_num = d->devfn - (d->exp.pf->devfn + 1);
+        if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
+            new_addr = pci_get_quad(d->exp.pf->config + bar);
+        } else {
+            new_addr = pci_get_long(d->exp.pf->config + bar);
+        }
+        new_addr += vf_num * size;
+
+        PCI_DPRINTF("%02d:%02d.%d: (vf %d) found config reg %d, "
+                    "size 0x%lx addr 0x%lx\n",
+                    pci_bus_num(d->bus), d->devfn >> 3, d->devfn & 7,
+                    vf_num, reg, size, new_addr);
+    }
+    if (reg != PCI_ROM_SLOT) {
+        /* Preserve the rom enable bit */
+        new_addr &= ~(size - 1);
+    }
+    return new_addr;
+}
+
+pcibus_t pci_bar_address(PCIDevice *d,
+                         int reg, uint8_t type, pcibus_t size)
 {
     pcibus_t new_addr, last_addr;
-    int bar = pci_bar(d, reg);
     uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
 
     if (type & PCI_BASE_ADDRESS_SPACE_IO) {
         if (!(cmd & PCI_COMMAND_IO)) {
             return PCI_BAR_UNMAPPED;
         }
-        new_addr = pci_get_long(d->config + bar) & ~(size - 1);
+        new_addr = pci_config_get_bar_addr(d, reg, type, size);
         last_addr = new_addr + size - 1;
         /* Check if 32 bit BAR wraps around explicitly.
          * TODO: make priorities correct and remove this work around.
@@ -1043,11 +1099,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,
     if (!(cmd & PCI_COMMAND_MEMORY)) {
         return PCI_BAR_UNMAPPED;
     }
-    if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
-        new_addr = pci_get_quad(d->config + bar);
-    } else {
-        new_addr = pci_get_long(d->config + bar);
-    }
+    new_addr = pci_config_get_bar_addr(d, reg, type, size);
     /* the ROM slot has a specific enable bit */
     if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
         return PCI_BAR_UNMAPPED;
@@ -1146,9 +1198,10 @@ uint32_t pci_default_read_config(PCIDevice *d,
     return le32_to_cpu(val);
 }
 
-void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
+void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
 {
     int i, was_irq_disabled = pci_irq_disabled(d);
+    uint32_t val = val_in;
 
     for (i = 0; i < l; val >>= 8, ++i) {
         uint8_t wmask = d->wmask[addr + i];
@@ -1170,8 +1223,9 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
                                     & PCI_COMMAND_MASTER);
     }
 
-    msi_write_config(d, addr, val, l);
-    msix_write_config(d, addr, val, l);
+    msi_write_config(d, addr, val_in, l);
+    msix_write_config(d, addr, val_in, l);
+    pcie_sriov_config_write(d, addr, val_in, l);
 }
 
 /***********************************************************/
@@ -1776,7 +1830,6 @@ static int pci_qdev_init(DeviceState *qdev)
         is_default_rom = true;
     }
     pci_add_option_rom(pci_dev, is_default_rom);
-
     return 0;
 }
 
hw/pci/pcie.c +201 -4

228 lines not on this page · raw message

include/hw/pci/pci.h +4 -2
index c352c7b..d36c68d 100644
@@ -11,8 +11,6 @@
 /* PCI includes legacy ISA access.  */
 #include "hw/isa/isa.h"
 
-#include "hw/pci/pcie.h"
-
 /* PCI bus */
 
 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
@@ -127,6 +125,7 @@ enum {
 #define QEMU_PCI_VGA_IO_HI_SIZE 0x20
 
 #include "hw/pci/pci_regs.h"
+#include "hw/pci/pcie.h"
 
 /* PCI HEADER_TYPE */
 #define  PCI_HEADER_TYPE_MULTI_FUNCTION 0x80
@@ -413,6 +412,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
 void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque);
 
+pcibus_t pci_bar_address(PCIDevice *d,
+                         int reg, uint8_t type, pcibus_t size);
+
 static inline void
 pci_set_byte(uint8_t *config, uint8_t val)
 {
include/hw/pci/pcie.h +26 -0
index d139d58..b34d831 100644
@@ -74,6 +74,15 @@ struct PCIExpressDevice {
     /* AER */
     uint16_t aer_cap;
     PCIEAERLog aer_log;
+
+    /* SR/IOV */
+    uint16_t sriov_cap;
+    uint16_t num_vfs;           /* Number of virtual functions created */
+    bool is_vf;                 /* Set if this device is a virtual function */
+    const char *vfname;         /* Reference to the device type used for the VFs */
+    PCIDevice **vf;             /* Pointer to an array of num_vfs VF devices */
+    PCIDevice *pf;              /* Pointer back to owner physical function */
+    uint8_t vf_bar_type[PCI_NUM_REGIONS];  /* Store type for each VF bar */
 };
 
 #define COMPAT_PROP_PCP "power_controller_present"
@@ -115,6 +124,23 @@ void pcie_add_capability(PCIDevice *dev,
                          uint16_t offset, uint16_t size);
 
 void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn);
+void pcie_sriov_init(PCIDevice *dev, uint16_t offset,
+                     const char *vfname, uint16_t vf_dev_id,
+                     uint16_t init_vfs, uint16_t total_vfs);
+void pcie_sriov_exit(PCIDevice *dev);
+
+/* Set up a VF bar in the SR/IOV bar area */
+void pcie_sriov_init_bar(PCIDevice *s, int region_num,
+                         uint8_t type, dma_addr_t size);
+
+/* Instantiate a bar for a VF */
+void pcie_register_vf_bar(PCIDevice *pci_dev, int region_num,
+                          MemoryRegion *memory);
+
+void pcie_sriov_create_vfs(PCIDevice *dev);
+void pcie_sriov_reset_vfs(PCIDevice *dev);
+void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
+                             uint32_t val, int len);
 
 extern const VMStateDescription vmstate_pcie_device;
 
Signature · 1 line
1.9.0
author
#1 lore raw

PATCH 1/4 pci: Update pci_regs header

In reply to Knut Omang

Pulls in latest version from kernel v3.16
msi: Removed local definitions now in pci_regs.h

Also replace usage of PCI_MSIX_FLAGS_BIRMASK with PCI_MSIX_TABLE_BIR to keep in sync with the kernel for future updates.

---
 hw/i386/kvm/pci-assign.c  |   4 +-
 hw/misc/vfio.c            |   8 +-
 hw/pci/msi.c              |   4 -
 hw/pci/msix.c             |   2 +-
 include/hw/pci/pci_regs.h | 371 ++++++++++++++++++++++++++++++++++------------
 5 files changed, 285 insertions(+), 104 deletions(-)
hw/i386/kvm/pci-assign.c +2 -2
index 17c7d6dc..a2285c4 100644
@@ -1321,8 +1321,8 @@ static int assigned_device_pci_cap_init(PCIDevice *pci_dev, Error **errp)
                      PCI_MSIX_FLAGS_ENABLE | PCI_MSIX_FLAGS_MASKALL);
 
         msix_table_entry = pci_get_long(pci_dev->config + pos + PCI_MSIX_TABLE);
-        bar_nr = msix_table_entry & PCI_MSIX_FLAGS_BIRMASK;
-        msix_table_entry &= ~PCI_MSIX_FLAGS_BIRMASK;
+        bar_nr = msix_table_entry & PCI_MSIX_TABLE_BIR;
+        msix_table_entry &= ~PCI_MSIX_TABLE_BIR;
         dev->msix_table_addr = pci_region[bar_nr].base_addr + msix_table_entry;
         dev->msix_max = msix_max;
     }
hw/misc/vfio.c +4 -4
index 0617b70..984c9e2 100644
@@ -2795,10 +2795,10 @@ static int vfio_early_setup_msix(VFIODevice *vdev)
     pba = le32_to_cpu(pba);
 
     vdev->msix = g_malloc0(sizeof(*(vdev->msix)));
-    vdev->msix->table_bar = table & PCI_MSIX_FLAGS_BIRMASK;
-    vdev->msix->table_offset = table & ~PCI_MSIX_FLAGS_BIRMASK;
-    vdev->msix->pba_bar = pba & PCI_MSIX_FLAGS_BIRMASK;
-    vdev->msix->pba_offset = pba & ~PCI_MSIX_FLAGS_BIRMASK;
+    vdev->msix->table_bar = table & PCI_MSIX_TABLE_BIR;
+    vdev->msix->table_offset = table & ~PCI_MSIX_TABLE_BIR;
+    vdev->msix->pba_bar = pba & PCI_MSIX_TABLE_BIR;
+    vdev->msix->pba_offset = pba & ~PCI_MSIX_TABLE_BIR;
     vdev->msix->entries = (ctrl & PCI_MSIX_FLAGS_QSIZE) + 1;
 
     DPRINTF("%04x:%02x:%02x.%x "
hw/pci/msi.c +0 -4
index 52d2313..1d25b62 100644
@@ -21,10 +21,6 @@
 #include "hw/pci/msi.h"
 #include "qemu/range.h"
 
-/* Eventually those constants should go to Linux pci_regs.h */
-#define PCI_MSI_PENDING_32      0x10
-#define PCI_MSI_PENDING_64      0x14
-
 /* PCI_MSI_ADDRESS_LO */
 #define PCI_MSI_ADDRESS_LO_MASK         (~0x3)
 
hw/pci/msix.c +1 -1
index 24de260..8206264 100644
@@ -250,7 +250,7 @@ int msix_init(struct PCIDevice *dev, unsigned short nentries,
          ranges_overlap(table_offset, table_size, pba_offset, pba_size)) ||
         table_offset + table_size > memory_region_size(table_bar) ||
         pba_offset + pba_size > memory_region_size(pba_bar) ||
-        (table_offset | pba_offset) & PCI_MSIX_FLAGS_BIRMASK) {
+        (table_offset | pba_offset) & PCI_MSIX_TABLE_BIR) {
         return -EINVAL;
     }
 
include/hw/pci/pci_regs.h +278 -93

612 lines not on this page · raw message

Signature · 1 line
1.9.0
author
#6 lore raw

PATCH 4/4 igb: Example code to illustrate the SR/IOV support.

In reply to Knut Omang

This patch contains a fairly naive attempt to emulate an Intel 82576 Gigabit Ethernet Adapter (igb/igbvf) for the purpose of illustrating the use of the SR/IOV emulation features.

This first shot at it does not completely work as an Ethernet device, as it is basically a simple subclassing of the e1000 base class where the emphasis has been on adding the minimal functionality needed to fool the igb/igvf Linux drivers to load and allow VFs to show up, but it should be possible to extend it into something working for someone familiar with the details of the 82576.

The current status is that the VFs (if enabled vith a nonzero value for the num_vfs parameter to the igb driver) will load and present ethernet devices, but the PF fails to load because it fails to detect a valid flash content in BAR 1.

---
 hw/net/Makefile.objs |   2 +-
 hw/net/igb.c         | 293 +++++++++++++++++++++++++++++++++++++++++++++++++++
 hw/net/igb_regs.h    |  27 +++++
 3 files changed, 321 insertions(+), 1 deletion(-)
 create mode 100644 hw/net/igb.c
 create mode 100644 hw/net/igb_regs.h
hw/net/Makefile.objs +1 -1
index ea93293..6832f12 100644
@@ -6,7 +6,7 @@ common-obj-$(CONFIG_NE2000_PCI) += ne2000.o
 common-obj-$(CONFIG_EEPRO100_PCI) += eepro100.o
 common-obj-$(CONFIG_PCNET_PCI) += pcnet-pci.o
 common-obj-$(CONFIG_PCNET_COMMON) += pcnet.o
-common-obj-$(CONFIG_E1000_PCI) += e1000.o
+common-obj-$(CONFIG_E1000_PCI) += e1000.o igb.o
 common-obj-$(CONFIG_RTL8139_PCI) += rtl8139.o
 common-obj-$(CONFIG_VMXNET3_PCI) += vmxnet_tx_pkt.o vmxnet_rx_pkt.o
 common-obj-$(CONFIG_VMXNET3_PCI) += vmxnet3.o
hw/net/igb.c new file +293 -0

293 lines not on this page · raw message

hw/net/igb_regs.h new file +27 -0
new file mode 100644
index 0000000..496eb42
@@ -0,0 +1,27 @@
+/*
+ * igb_regs.h
+ *
+ * Intel 82576 Gigabit Ethernet Adapter
+ * (SR/IOV capable PCIe ethernet device) emulation
+ *
+ * Partly derived from kernel/drivers/net/ethernet/intel/igb/igb.h
+ *
+ * Copyright (c) 2014 Knut Omang <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#include "e1000_regs.h"
+
+/* Experimental igb device (with SR/IOV) IDs for PF and VF: */
+#define E1000_DEV_ID_82576                      0x10C9
+#define IGB_82576_VF_DEV_ID                     0x10CA
+
+#define I210_I_PHY_ID2 0x0c00
+#define IGB_MSIX_BAR 3
+#define IGB_TOTAL_VFS 8
+#define IGB_CAP_SRIOV_OFFSET 0x160
+#define IGB_MSIX_VECTORS_PF 3
+#define IGB_MSIX_VECTORS_VF 3
Signature · 1 line
1.9.0
author
#5 lore raw

PATCH 3/4 e1000: Refactor to allow subclassing from other source file

In reply to Knut Omang
---
 hw/net/e1000.c | 126 +++++++--------------------------------------------
 hw/net/e1000.h | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 155 insertions(+), 110 deletions(-)
 create mode 100644 hw/net/e1000.h
hw/net/e1000.c +16 -110
index 272df00..7afdcad 100644
@@ -35,6 +35,7 @@
 #include "qemu/iov.h"
 
 #include "e1000_regs.h"
+#include "e1000.h"
 
 #define E1000_DEBUG
 
@@ -75,101 +76,6 @@ static int debugflags = DBGBIT(TXERR) | DBGBIT(GENERAL);
  *  Others never tested
  */
 
-typedef struct E1000State_st {
-    /*< private >*/
-    PCIDevice parent_obj;
-    /*< public >*/
-
-    NICState *nic;
-    NICConf conf;
-    MemoryRegion mmio;
-    MemoryRegion io;
-
-    uint32_t mac_reg[0x8000];
-    uint16_t phy_reg[0x20];
-    uint16_t eeprom_data[64];
-
-    uint32_t rxbuf_size;
-    uint32_t rxbuf_min_shift;
-    struct e1000_tx {
-        unsigned char header[256];
-        unsigned char vlan_header[4];
-        /* Fields vlan and data must not be reordered or separated. */
-        unsigned char vlan[4];
-        unsigned char data[0x10000];
-        uint16_t size;
-        unsigned char sum_needed;
-        unsigned char vlan_needed;
-        uint8_t ipcss;
-        uint8_t ipcso;
-        uint16_t ipcse;
-        uint8_t tucss;
-        uint8_t tucso;
-        uint16_t tucse;
-        uint8_t hdr_len;
-        uint16_t mss;
-        uint32_t paylen;
-        uint16_t tso_frames;
-        char tse;
-        int8_t ip;
-        int8_t tcp;
-        char cptse;     // current packet tse bit
-    } tx;
-
-    struct {
-        uint32_t val_in;	// shifted in from guest driver
-        uint16_t bitnum_in;
-        uint16_t bitnum_out;
-        uint16_t reading;
-        uint32_t old_eecd;
-    } eecd_state;
-
-    QEMUTimer *autoneg_timer;
-
-    QEMUTimer *mit_timer;      /* Mitigation timer. */
-    bool mit_timer_on;         /* Mitigation timer is running. */
-    bool mit_irq_level;        /* Tracks interrupt pin level. */
-    uint32_t mit_ide;          /* Tracks E1000_TXD_CMD_IDE bit. */
-
-/* Compatibility flags for migration to/from qemu 1.3.0 and older */
-#define E1000_FLAG_AUTONEG_BIT 0
-#define E1000_FLAG_MIT_BIT 1
-#define E1000_FLAG_AUTONEG (1 << E1000_FLAG_AUTONEG_BIT)
-#define E1000_FLAG_MIT (1 << E1000_FLAG_MIT_BIT)
-    uint32_t compat_flags;
-} E1000State;
-
-typedef struct E1000BaseClass {
-    PCIDeviceClass parent_class;
-    uint16_t phy_id2;
-} E1000BaseClass;
-
-#define TYPE_E1000_BASE "e1000-base"
-
-#define E1000(obj) \
-    OBJECT_CHECK(E1000State, (obj), TYPE_E1000_BASE)
-
-#define E1000_DEVICE_CLASS(klass) \
-     OBJECT_CLASS_CHECK(E1000BaseClass, (klass), TYPE_E1000_BASE)
-#define E1000_DEVICE_GET_CLASS(obj) \
-    OBJECT_GET_CLASS(E1000BaseClass, (obj), TYPE_E1000_BASE)
-
-#define	defreg(x)	x = (E1000_##x>>2)
-enum {
-    defreg(CTRL),	defreg(EECD),	defreg(EERD),	defreg(GPRC),
-    defreg(GPTC),	defreg(ICR),	defreg(ICS),	defreg(IMC),
-    defreg(IMS),	defreg(LEDCTL),	defreg(MANC),	defreg(MDIC),
-    defreg(MPC),	defreg(PBA),	defreg(RCTL),	defreg(RDBAH),
-    defreg(RDBAL),	defreg(RDH),	defreg(RDLEN),	defreg(RDT),
-    defreg(STATUS),	defreg(SWSM),	defreg(TCTL),	defreg(TDBAH),
-    defreg(TDBAL),	defreg(TDH),	defreg(TDLEN),	defreg(TDT),
-    defreg(TORH),	defreg(TORL),	defreg(TOTH),	defreg(TOTL),
-    defreg(TPR),	defreg(TPT),	defreg(TXDCTL),	defreg(WUFC),
-    defreg(RA),		defreg(MTA),	defreg(CRCERRS),defreg(VFTA),
-    defreg(VET),        defreg(RDTR),   defreg(RADV),   defreg(TADV),
-    defreg(ITR),
-};
-
 static void
 e1000_link_down(E1000State *s)
 {
@@ -398,7 +304,7 @@ rxbufsize(uint32_t v)
     return 2048;
 }
 
-static void e1000_reset(void *opaque)
+void e1000_reset(void *opaque)
 {
     E1000State *d = opaque;
     E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d);
@@ -1507,8 +1413,7 @@ e1000_cleanup(NetClientState *nc)
     s->nic = NULL;
 }
 
-static void
-pci_e1000_uninit(PCIDevice *dev)
+void pci_e1000_uninit(PCIDevice *dev)
 {
     E1000State *d = E1000(dev);
 
@@ -1529,10 +1434,11 @@ static NetClientInfo net_e1000_info = {
     .link_status_changed = e1000_set_link_status,
 };
 
-static int pci_e1000_init(PCIDevice *pci_dev)
+int pci_e1000_init(PCIDevice *pci_dev)
 {
     DeviceState *dev = DEVICE(pci_dev);
     E1000State *d = E1000(pci_dev);
+    E1000BaseClass *edc = E1000_DEVICE_GET_CLASS(d);
     PCIDeviceClass *pdc = PCI_DEVICE_GET_CLASS(pci_dev);
     uint8_t *pci_conf;
     uint16_t checksum = 0;
@@ -1548,9 +1454,12 @@ static int pci_e1000_init(PCIDevice *pci_dev)
 
     e1000_mmio_setup(d);
 
-    pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
-
-    pci_register_bar(pci_dev, 1, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
+    if (pci_dev->exp.is_vf) {
+        pcie_register_vf_bar(pci_dev, 0, &d->mmio);
+    } else {
+        pci_register_bar(pci_dev, 0, PCI_BASE_ADDRESS_SPACE_MEMORY, &d->mmio);
+        pci_register_bar(pci_dev, edc->io_bar, PCI_BASE_ADDRESS_SPACE_IO, &d->io);
+    }
 
     memmove(d->eeprom_data, e1000_eeprom_template,
         sizeof e1000_eeprom_template);
@@ -1592,14 +1501,7 @@ static Property e1000_properties[] = {
     DEFINE_PROP_END_OF_LIST(),
 };
 
-typedef struct E1000Info {
-    const char *name;
-    uint16_t   device_id;
-    uint8_t    revision;
-    uint16_t   phy_id2;
-} E1000Info;
-
-static void e1000_class_init(ObjectClass *klass, void *data)
+void e1000_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
     PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
@@ -1613,6 +1515,7 @@ static void e1000_class_init(ObjectClass *klass, void *data)
     k->device_id = info->device_id;
     k->revision = info->revision;
     e->phy_id2 = info->phy_id2;
+    e->io_bar = info->io_bar;
     k->class_id = PCI_CLASS_NETWORK_ETHERNET;
     set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
     dc->desc = "Intel Gigabit Ethernet";
@@ -1634,18 +1537,21 @@ static const E1000Info e1000_devices[] = {
         .name      = "e1000-82540em",
         .device_id = E1000_DEV_ID_82540EM,
         .revision  = 0x03,
+        .io_bar    = 1,
         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
     },
     {
         .name      = "e1000-82544gc",
         .device_id = E1000_DEV_ID_82544GC_COPPER,
         .revision  = 0x03,
+        .io_bar    = 1,
         .phy_id2   = E1000_PHY_ID2_82544x,
     },
     {
         .name      = "e1000-82545em",
         .device_id = E1000_DEV_ID_82545EM_COPPER,
         .revision  = 0x03,
+        .io_bar    = 1,
         .phy_id2   = E1000_PHY_ID2_8254xx_DEFAULT,
     },
 };
hw/net/e1000.h new file +139 -0

139 lines not on this page · raw message

Signature · 1 line
1.9.0
#7 lore raw
In reply to Knut Omang
Quoted 82 lines · Knut Omang

This patch set consists of two parts:

- The two first patches implements SR/IOV building blocks in pcie/pci. I have held this patch back for a while because I haven't had a good example test case to accompany it, but in the light of the latest developments such as the discussion we have had around ARI and downstream switches and root ports, I think it would be valuable to get this in, to make it easier for people to experiment with creating devices with many functions. Hopefully, I can also get some help to fix the hotplug issues described below.

- The two last patches contains an example to illustrate the usage of the new SR/IOV support. The example leverages the e1000
code and the fact that registers between E1000 and the PCIe based Intel 82576 Gigabit Ethernet Adapter (which supports SR/IOV) are quite similar, but so far without considering much of the differences beyond the bare minimum needed to trick the igb driver into loading to the point where VFs can be enabled.

So you cannot yet use these PF or VF Ethernet devices to send ethernet packets, and the implementation do not in any way attempt to model the internals of igb such as the multiple queues or any multiplexing onto the same device, it only instantiates the VFs as well as the PFs mostly directly by inheritance (and as separate devices) from E1000, but it should hopefully be relatively easy to understand how to proceed to make "true" VFs.
It was also a nice exercise in using QOM.

The changes to E1000 to accommodate igb (patch 3) should be fairly non-intrusive, nevertheless I suppose it should not be applied unless it will eventually lead to a new derived device which is enough different from E1000 to qualify for a separate set of source files. So if someone with more detailed knowledge of the internal differences between igb and e1000 on the functional level might have input, that would be great, an alternative of course be to only apply the two first patches, and leave any usage examples for the future.

To test and see how it plays out, you can add something like this to the command line:

     -device ioh3420,slot=0,id=pcie_port.0
     -device igb,mac=DE:AD:BE:EE:04:18,vlan=1,bus=pcie_port.0
     -net tap,vlan=1

The Linux igb driver does not yet support changing the VF setup via sysfs (the preferred way since kernel v.3.8) so to see some VFs on the guest, you need to set up a modprobe file with something like this:

options igb max_vfs=4

For some reason I dont yet understand, removal of the igb driver does not cause a 0 write to sr/iov vf_enable to disable the VFs again, eg.

       # lspci | grep '^01:00'
       01:00.0 Ethernet controller: Intel Corporation 82576 Gigabit Network Connection (rev 01)
       01:00.1 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.2 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.3 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.4 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)

Writing directly to vf_enable using setpci works though, but after the later hotplug changes, a little too well, as after the VF removals have succeded, some way a slot power down gets triggered, also removing the PF:

      # setpci -s 01:00.0 168.b
      0x19
      # setpci -s 01:00.0 168.b=18
      # lspci | grep '^01:00'
      <nothing>

For similar reasons attaching the igb device directly on the root complex (just remove bus parameter above) attempts to enable VFs will fail with

      qdev.c:89: bus_add_child: Assertion `bus->allow_hotplug' failed

I imagine this could for instance be a matter of defining a property "subfunction" or similar that allows qdev (QOM?) to discriminate between main devices and devices representing subfunctions of another main device. Suggestions on how to proceed on this welcome.

This patch depends (by diff context only) on my patch:

[PATCH v2 1/4] pcie: Fix incorrect write to the ari capability next function field

Quoted 7 lines

and for stability on Paolo's

[PATCH] pci_bridge: manually destroy memory regions within PCIBridgeWindows

both of which Michael has pulled, but which are not in master yet.

Thanks,

So practically you would like patches 1 and 2 applied, and 3 and 4 are RFC?

Quoted 27 lines

Knut Omang (4):
pci: Update pci_regs header
pcie: Add support for Single Root I/O Virtualization (SR/IOV)
e1000: Refactor to allow subclassing from other source file
igb: Example code to illustrate the SR/IOV support.

 hw/i386/kvm/pci-assign.c  |   4 +-
 hw/misc/vfio.c            |   8 +-
 hw/net/Makefile.objs      |   2 +-
 hw/net/e1000.c            | 126 ++--------------
 hw/net/e1000.h            | 139 +++++++++++++++++
 hw/net/igb.c              | 293 ++++++++++++++++++++++++++++++++++++
 hw/net/igb_regs.h         |  27 ++++
 hw/pci/msi.c              |   4 -
 hw/pci/msix.c             |   2 +-
 hw/pci/pci.c              | 107 +++++++++----
 hw/pci/pcie.c             | 205 ++++++++++++++++++++++++-
 include/hw/pci/pci.h      |   6 +-
 include/hw/pci/pci_regs.h | 371 ++++++++++++++++++++++++++++++++++------------
 include/hw/pci/pcie.h     |  26 ++++
 14 files changed, 1072 insertions(+), 248 deletions(-)
 create mode 100644 hw/net/e1000.h
 create mode 100644 hw/net/igb.c
 create mode 100644 hw/net/igb_regs.h
Signature · 1 line
1.9.0
author
#8 lore raw
Quoted 85 lines · Michael S. Tsirkin
Quoted 84 lines · Knut Omang

This patch set consists of two parts:

- The two first patches implements SR/IOV building blocks in pcie/pci. I have held this patch back for a while because I haven't had a good example test case to accompany it, but in the light of the latest developments such as the discussion we have had around ARI and downstream switches and root ports, I think it would be valuable to get this in, to make it easier for people to experiment with creating devices with many functions. Hopefully, I can also get some help to fix the hotplug issues described below.

- The two last patches contains an example to illustrate the usage of the new SR/IOV support. The example leverages the e1000
code and the fact that registers between E1000 and the PCIe based Intel 82576 Gigabit Ethernet Adapter (which supports SR/IOV) are quite similar, but so far without considering much of the differences beyond the bare minimum needed to trick the igb driver into loading to the point where VFs can be enabled.

So you cannot yet use these PF or VF Ethernet devices to send ethernet packets, and the implementation do not in any way attempt to model the internals of igb such as the multiple queues or any multiplexing onto the same device, it only instantiates the VFs as well as the PFs mostly directly by inheritance (and as separate devices) from E1000, but it should hopefully be relatively easy to understand how to proceed to make "true" VFs.
It was also a nice exercise in using QOM.

The changes to E1000 to accommodate igb (patch 3) should be fairly non-intrusive, nevertheless I suppose it should not be applied unless it will eventually lead to a new derived device which is enough different from E1000 to qualify for a separate set of source files. So if someone with more detailed knowledge of the internal differences between igb and e1000 on the functional level might have input, that would be great, an alternative of course be to only apply the two first patches, and leave any usage examples for the future.

To test and see how it plays out, you can add something like this to the command line:

     -device ioh3420,slot=0,id=pcie_port.0
     -device igb,mac=DE:AD:BE:EE:04:18,vlan=1,bus=pcie_port.0
     -net tap,vlan=1

The Linux igb driver does not yet support changing the VF setup via sysfs (the preferred way since kernel v.3.8) so to see some VFs on the guest, you need to set up a modprobe file with something like this:

options igb max_vfs=4

For some reason I dont yet understand, removal of the igb driver does not cause a 0 write to sr/iov vf_enable to disable the VFs again, eg.

       # lspci | grep '^01:00'
       01:00.0 Ethernet controller: Intel Corporation 82576 Gigabit Network Connection (rev 01)
       01:00.1 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.2 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.3 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)
       01:00.4 Ethernet controller: Intel Corporation 82576 Virtual Function (rev 01)

Writing directly to vf_enable using setpci works though, but after the later hotplug changes, a little too well, as after the VF removals have succeded, some way a slot power down gets triggered, also removing the PF:

      # setpci -s 01:00.0 168.b
      0x19
      # setpci -s 01:00.0 168.b=18
      # lspci | grep '^01:00'
      <nothing>

For similar reasons attaching the igb device directly on the root complex (just remove bus parameter above) attempts to enable VFs will fail with

      qdev.c:89: bus_add_child: Assertion `bus->allow_hotplug' failed

I imagine this could for instance be a matter of defining a property "subfunction" or similar that allows qdev (QOM?) to discriminate between main devices and devices representing subfunctions of another main device. Suggestions on how to proceed on this welcome.

This patch depends (by diff context only) on my patch:

[PATCH v2 1/4] pcie: Fix incorrect write to the ari capability next function field

and for stability on Paolo's

Quoted 8 lines
Quoted 5 lines

[PATCH] pci_bridge: manually destroy memory regions within PCIBridgeWindows

both of which Michael has pulled, but which are not in master yet.

Thanks,

So practically you would like patches 1 and 2 applied, and 3 and 4 are RFC?

Yes, I suppose that's a better way of formulating my intentions, thanks!

Knut

Quoted 27 lines
Quoted 27 lines

Knut Omang (4):
pci: Update pci_regs header
pcie: Add support for Single Root I/O Virtualization (SR/IOV)
e1000: Refactor to allow subclassing from other source file
igb: Example code to illustrate the SR/IOV support.

 hw/i386/kvm/pci-assign.c  |   4 +-
 hw/misc/vfio.c            |   8 +-
 hw/net/Makefile.objs      |   2 +-
 hw/net/e1000.c            | 126 ++--------------
 hw/net/e1000.h            | 139 +++++++++++++++++
 hw/net/igb.c              | 293 ++++++++++++++++++++++++++++++++++++
 hw/net/igb_regs.h         |  27 ++++
 hw/pci/msi.c              |   4 -
 hw/pci/msix.c             |   2 +-
 hw/pci/pci.c              | 107 +++++++++----
 hw/pci/pcie.c             | 205 ++++++++++++++++++++++++-
 include/hw/pci/pci.h      |   6 +-
 include/hw/pci/pci_regs.h | 371 ++++++++++++++++++++++++++++++++++------------
 include/hw/pci/pcie.h     |  26 ++++
 14 files changed, 1072 insertions(+), 248 deletions(-)
 create mode 100644 hw/net/e1000.h
 create mode 100644 hw/net/igb.c
 create mode 100644 hw/net/igb_regs.h
Signature · 1 line
1.9.0
#3 lore raw

Re: [Qemu-devel] [PATCH 2/4] pcie: Add support for Single Root I/O Virtualization (SR/IOV)

In reply to Knut Omang
Quoted 90 lines · Knut Omang

This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and creating and removing SR/IOV Virtual Functions.

---
 hw/pci/pci.c          | 107 +++++++++++++++++++-------
 hw/pci/pcie.c         | 205 +++++++++++++++++++++++++++++++++++++++++++++++++-
 include/hw/pci/pci.h  |   6 +-
 include/hw/pci/pcie.h |  26 +++++++
 4 files changed, 311 insertions(+), 33 deletions(-)
hw/pci/pci.c +28 -14
index daeaeac..071ab81 100644
@@ -35,7 +35,6 @@
 #include "hw/pci/msi.h"
 #include "hw/pci/msix.h"
 #include "exec/address-spaces.h"
-#include "hw/hotplug.h"
 
 //#define DEBUG_PCI
 #ifdef DEBUG_PCI
@@ -126,6 +125,9 @@ static int pci_bar(PCIDevice *d, int reg)
 {
     uint8_t type;
 
+    /* PCIe virtual functions do not have their own BARs */
+    assert(!d->exp.is_vf);
+
     if (reg != PCI_ROM_SLOT)
         return PCI_BASE_ADDRESS_0 + reg * 4;
 
@@ -184,22 +186,13 @@ void pci_device_deassert_intx(PCIDevice *dev)
     }
 }
 
-static void pci_do_device_reset(PCIDevice *dev)
+static void pci_reset_regions(PCIDevice *dev)
 {
     int r;
+    if (dev->exp.is_vf) {
+        return;
+    }
 
-    pci_device_deassert_intx(dev);
-    assert(dev->irq_state == 0);
-
-    /* Clear all writable bits */
-    pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
-                                 pci_get_word(dev->wmask + PCI_COMMAND) |
-                                 pci_get_word(dev->w1cmask + PCI_COMMAND));
-    pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
-                                 pci_get_word(dev->wmask + PCI_STATUS) |
-                                 pci_get_word(dev->w1cmask + PCI_STATUS));
-    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
-    dev->config[PCI_INTERRUPT_LINE] = 0x0;
     for (r = 0; r < PCI_NUM_REGIONS; ++r) {
         PCIIORegion *region = &dev->io_regions[r];
         if (!region->size) {
@@ -213,6 +206,27 @@ static void pci_do_device_reset(PCIDevice *dev)
             pci_set_long(dev->config + pci_bar(dev, r), region->type);
         }
     }
+}
+
+static void pci_do_device_reset(PCIDevice *dev)
+{
+    qdev_reset_all(&dev->qdev);
+
+    dev->irq_state = 0;
+    pci_update_irq_status(dev);
+    pci_device_deassert_intx(dev);
+    assert(dev->irq_state == 0);
+
+    /* Clear all writable bits */
+    pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
+                                 pci_get_word(dev->wmask + PCI_COMMAND) |
+                                 pci_get_word(dev->w1cmask + PCI_COMMAND));
+    pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
+                                 pci_get_word(dev->wmask + PCI_STATUS) |
+                                 pci_get_word(dev->w1cmask + PCI_STATUS));
+    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
+    dev->config[PCI_INTERRUPT_LINE] = 0x0;
+    pci_reset_regions(dev);
     pci_update_mappings(dev);
 
     msi_reset(dev);

hw/pci/pci.c static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev) · line 753

Quoted 6 lines
hw/pci/pci.c +3 -0
@@ -734,6 +748,14 @@ static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev)
         dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
     }
 
+    /* With SR/IOV and ARI, subsequent function 0's are only
+     * another VF which the physical function is placed in the initial
+     * function (0.0)

Couldn't parse this. Could you please reword?

Quoted 38 lines
hw/pci/pci.c +22 -2
+     */
+    if (dev->exp.pf && dev->exp.pf->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
+        return 0;
+    }
+
     /*
      * multifunction bit is interpreted in two ways as follows.
      *   - all functions must set the bit to 1.
@@ -920,6 +942,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,
     uint64_t wmask;
     pcibus_t size = memory_region_size(memory);
 
+    assert(!pci_dev->exp.is_vf); /* VFs must use pcie_register_vf_bar */
     assert(region_num >= 0);
     assert(region_num < PCI_NUM_REGIONS);
     if (size & (size-1)) {
@@ -1018,18 +1041,51 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
     return pci_dev->io_regions[region_num].addr;
 }
 
-static pcibus_t pci_bar_address(PCIDevice *d,
-				int reg, uint8_t type, pcibus_t size)
+
+static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg,
+                                        uint8_t type, pcibus_t size)
+{
+    pcibus_t new_addr;
+    if (!d->exp.is_vf) {
+        int bar = pci_bar(d, reg);
+        if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
+            new_addr = pci_get_quad(d->config + bar);
+        } else {
+            new_addr = pci_get_long(d->config + bar);
+        }
+    } else {
+        int bar = d->exp.pf->exp.sriov_cap + PCI_SRIOV_BAR + reg * 4;
+        uint32_t vf_num = d->devfn - (d->exp.pf->devfn + 1);
+        if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {

hw/pci/pci.c pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num) · line 1069

Quoted 10 lines
hw/pci/pci.c +10 -0
+            new_addr = pci_get_quad(d->exp.pf->config + bar);
+        } else {
+            new_addr = pci_get_long(d->exp.pf->config + bar);
+        }
+        new_addr += vf_num * size;
+
+        PCI_DPRINTF("%02d:%02d.%d: (vf %d) found config reg %d, "
+                    "size 0x%lx addr 0x%lx\n",
+                    pci_bus_num(d->bus), d->devfn >> 3, d->devfn & 7,
+                    vf_num, reg, size, new_addr);

I don't think this is needed, assuming it is, don't we want this for all functions, bot just vfs?

Quoted 11 lines
hw/pci/pci.c +10 -0
+    }
+    if (reg != PCI_ROM_SLOT) {
+        /* Preserve the rom enable bit */
+        new_addr &= ~(size - 1);
+    }
+    return new_addr;
+}
+
+pcibus_t pci_bar_address(PCIDevice *d,
+                         int reg, uint8_t type, pcibus_t size)
 {

hw/pci/pci.c pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num) · line 1088

Quoted 10 lines
hw/pci/pci.c +1 -2
     pcibus_t new_addr, last_addr;
-    int bar = pci_bar(d, reg);
     uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
 
     if (type & PCI_BASE_ADDRESS_SPACE_IO) {
         if (!(cmd & PCI_COMMAND_IO)) {
             return PCI_BAR_UNMAPPED;
         }
-        new_addr = pci_get_long(d->config + bar) & ~(size - 1);
+        new_addr = pci_config_get_bar_addr(d, reg, type, size);

Hmm this is IO, VFs don't have IO, do they? Change really necessary?

Quoted 29 lines
hw/pci/pci.c +3 -6
         last_addr = new_addr + size - 1;
         /* Check if 32 bit BAR wraps around explicitly.
          * TODO: make priorities correct and remove this work around.
@@ -1043,11 +1099,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,
     if (!(cmd & PCI_COMMAND_MEMORY)) {
         return PCI_BAR_UNMAPPED;
     }
-    if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
-        new_addr = pci_get_quad(d->config + bar);
-    } else {
-        new_addr = pci_get_long(d->config + bar);
-    }
+    new_addr = pci_config_get_bar_addr(d, reg, type, size);
     /* the ROM slot has a specific enable bit */
     if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
         return PCI_BAR_UNMAPPED;
@@ -1146,9 +1198,10 @@ uint32_t pci_default_read_config(PCIDevice *d,
     return le32_to_cpu(val);
 }
 
-void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
+void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
 {
     int i, was_irq_disabled = pci_irq_disabled(d);
+    uint32_t val = val_in;
 
     for (i = 0; i < l; val >>= 8, ++i) {
         uint8_t wmask = d->wmask[addr + i];

hw/pci/pci.c void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) · line 1229

Quoted 9 lines
hw/pci/pci.c +3 -2
@@ -1170,8 +1223,9 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
                                     & PCI_COMMAND_MASTER);
     }
 
-    msi_write_config(d, addr, val, l);
-    msix_write_config(d, addr, val, l);
+    msi_write_config(d, addr, val_in, l);
+    msix_write_config(d, addr, val_in, l);
+    pcie_sriov_config_write(d, addr, val_in, l);
 }

Not sure I get this chunk.
A code comment might be helpful.

hw/pci/pci.c static int pci_qdev_init(DeviceState *qdev) · line 1835

Quoted 10 lines
hw/pci/pci.c +0 -1
 /***********************************************************/
@@ -1776,7 +1830,6 @@ static int pci_qdev_init(DeviceState *qdev)
         is_default_rom = true;
     }
     pci_add_option_rom(pci_dev, is_default_rom);
-
     return 0;
 }
 

generally, don't make unrelated changes, review is hard enough.

Quoted 37 lines
hw/pci/pcie.c +5 -4
index 6cb6e0c..b7af693 100644
@@ -254,7 +254,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
      * Right now, only a device of function = 0 is allowed to be
      * hot plugged/unplugged.
      */
-    assert(PCI_FUNC(pci_dev->devfn) == 0);
+    assert(PCI_FUNC(pci_dev->devfn) == 0 || pci_dev->exp.is_vf);
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
@@ -266,10 +266,11 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
                                  Error **errp)
 {
     uint8_t *exp_cap;
+    PCIDevice *pdev = PCI_DEVICE(hotplug_dev);
 
-    pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
+    pcie_cap_slot_hotplug_common(pdev, dev, &exp_cap, errp);
 
-    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
+    pcie_cap_slot_push_attention_button(pdev);
 }
 
 /* pci express slot for pci express root/downstream port
@@ -409,7 +410,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
     }
 
     /*
-     * If the slot is polulated, power indicator is off and power
+     * If the slot is populated, power indicator is off and power
      * controller is off, it is safe to detach the devices.
      */
     if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 639

Quoted 6 lines
hw/pci/pcie.c +3 -0
@@ -633,3 +634,199 @@ void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
                         offset, PCI_ARI_SIZEOF);
     pci_set_long(dev->config + offset + PCI_ARI_CAP, (nextfn & 0xff) << 8);
 }
+
+
+

don't add > 1 empty line in a row.

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 640

Quoted 1 line
hw/pci/pcie.c +1 -0
+/* SR/IOV */

this comment is really useless, function name makes it clear.

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 646

Quoted 6 lines
hw/pci/pcie.c +6 -0
+void pcie_sriov_init(PCIDevice *dev, uint16_t offset,
+                     const char *vfname, uint16_t vf_dev_id,
+                     uint16_t init_vfs, uint16_t total_vfs)
+{
+    uint8_t *cfg = dev->config + offset;
+    uint8_t *wmask;

empty line here after variables.

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 651

Quoted 5 lines
hw/pci/pcie.c +5 -0
+    pcie_add_capability(dev, PCI_EXT_CAP_ID_SRIOV, 1,
+                        offset, PCI_EXT_CAP_SRIOV_SIZEOF);
+    dev->exp.sriov_cap = offset;
+    dev->exp.num_vfs = 0;
+    dev->exp.vfname = g_strdup(vfname);

I don't see a free for this field anywhere.

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 654

Quoted 3 lines
hw/pci/pcie.c +3 -0
+    dev->exp.vf = NULL;
+
+    /* set some sensible defaults - devices can override later */

do they in fact override?

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 657

Quoted 3 lines
hw/pci/pcie.c +3 -0
+    pci_set_word(cfg + PCI_SRIOV_VF_OFFSET, 0x1);
+    pci_set_word(cfg + PCI_SRIOV_VF_STRIDE, 0x1);
+    pci_set_word(cfg + PCI_SRIOV_SUP_PGSIZE, 0x553);

what's this magic constant?
needs a comment.

Quoted 7 lines
hw/pci/pcie.c +7 -0
+    pci_set_word(cfg + PCI_SRIOV_SYS_PGSIZE, 0x1);
+
+    /* Set up device ID and initial/total number of VFs available */
+    pci_set_word(cfg + PCI_SRIOV_VF_DID, vf_dev_id);
+    pci_set_word(cfg + PCI_SRIOV_INITIAL_VF, init_vfs);
+    pci_set_word(cfg + PCI_SRIOV_TOTAL_VF, total_vfs);
+

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 674

Quoted 10 lines
hw/pci/pcie.c +10 -0
+    /* Write enable control bits */
+    wmask = dev->wmask + offset;
+    pci_set_word(wmask + PCI_SRIOV_CTRL,
+                 PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE | PCI_SRIOV_CTRL_ARI);
+    pci_set_word(wmask + PCI_SRIOV_NUM_VF, 0xffff);
+
+    qdev_prop_set_bit(&dev->qdev, "multifunction", true);
+}
+
+

2 empty lines again

Quoted 37 lines
hw/pci/pcie.c +37 -0
+void pcie_sriov_exit(PCIDevice *dev)
+{
+    PCIE_DPRINTF("\n");
+    pcie_sriov_reset_vfs(dev);
+}
+
+void pcie_sriov_init_bar(PCIDevice *dev, int region_num,
+                         uint8_t type, dma_addr_t size)
+{
+    uint32_t addr;
+    uint64_t wmask;
+    uint16_t sriov_cap = dev->exp.sriov_cap;
+
+    assert(sriov_cap > 0);
+    assert(region_num >= 0);
+    assert(region_num < PCI_NUM_REGIONS);
+    assert(region_num != PCI_ROM_SLOT);
+
+    wmask = ~(size - 1);
+    addr = sriov_cap + PCI_SRIOV_BAR + region_num * 4;
+
+    pci_set_long(dev->config + addr, type);
+    if (!(type & PCI_BASE_ADDRESS_SPACE_IO) &&
+        type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
+        pci_set_quad(dev->wmask + addr, wmask);
+        pci_set_quad(dev->cmask + addr, ~0ULL);
+    } else {
+        pci_set_long(dev->wmask + addr, wmask & 0xffffffff);
+        pci_set_long(dev->cmask + addr, 0xffffffff);
+    }
+    dev->exp.vf_bar_type[region_num] = type;
+}
+
+void pcie_register_vf_bar(PCIDevice *dev, int region_num,
+                          MemoryRegion *memory)
+{
+    PCIIORegion *r;

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 721

Quoted 10 lines
hw/pci/pcie.c +10 -0
+    uint8_t type;
+    pcibus_t size = memory_region_size(memory);
+
+    assert(dev->exp.is_vf); /* PFs must use pci_register_bar */
+    assert(region_num >= 0);
+    assert(region_num < PCI_NUM_REGIONS);
+    type = dev->exp.pf->exp.vf_bar_type[region_num];
+
+    if (size & (size-1)) {
+        fprintf(stderr, "ERROR: PCI region size must be pow2 "

power of 2

eschew abbreviation

Quoted 40 lines
hw/pci/pcie.c +40 -0
+                    "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
+        exit(1);
+    }
+
+    r = &dev->io_regions[region_num];
+    r->memory = memory;
+    r->address_space =
+        type & PCI_BASE_ADDRESS_SPACE_IO
+        ? dev->bus->address_space_io
+        : dev->bus->address_space_mem;
+    r->size = size;
+    r->type = type;
+
+    r->addr = pci_bar_address(dev, region_num, r->type, r->size);
+    if (r->addr != PCI_BAR_UNMAPPED) {
+        memory_region_add_subregion_overlap(r->address_space,
+                                            r->addr, r->memory, 1);
+    }
+}
+
+
+static PCIDevice *pcie_create_vf(PCIDevice *pf, int devfn, const char *name)
+{
+    int ret;
+    PCIDevice *dev = pci_create(pf->bus, devfn, name);
+    dev->exp.is_vf = true;
+    dev->exp.pf = pf;
+
+    ret = qdev_init(&dev->qdev);
+    if (ret)
+        return NULL;
+
+    /* set vid/did according to sr/iov spec - they are not used */
+    pci_config_set_vendor_id(dev->config, 0xffff);
+    pci_config_set_device_id(dev->config, 0xffff);
+    return dev;
+}
+
+
+void pcie_sriov_create_vfs(PCIDevice *dev)

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 771

Quoted 10 lines
hw/pci/pcie.c +10 -0
+{
+    uint16_t num_vfs;
+    uint16_t i;
+    int32_t devfn = dev->devfn + 1;
+    uint16_t sriov_cap = dev->exp.sriov_cap;
+
+    assert(sriov_cap > 0);
+    num_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF);
+
+    dev->exp.vf = g_malloc(sizeof(PCIDevice*) * num_vfs);

Don't see a free to match this malloc call.

Quoted 75 lines
hw/pci/pcie.c +61 -0
+    assert(dev->exp.vf);
+
+    PCIE_DEV_PRINTF(dev, "creating %d vf devs\n", num_vfs);
+    for (i = 0; i < num_vfs; i++) {
+        dev->exp.vf[i] = pcie_create_vf(dev, devfn++, dev->exp.vfname);
+        if (!dev->exp.vf[i]) {
+            PCIE_DEV_PRINTF(dev, "Failed to create VF %d\n", i);
+            num_vfs = i;
+            break;
+        }
+    }
+    dev->exp.num_vfs = num_vfs;
+}
+
+
+void pcie_sriov_reset_vfs(PCIDevice *dev)
+{
+    Error *local_err = NULL;
+    uint16_t num_vfs = dev->exp.num_vfs;
+    uint16_t i;
+    PCIE_DEV_PRINTF(dev, "Resetting %d vf devs\n", num_vfs);
+    for (i = 0; i < num_vfs; i++) {
+        qdev_unplug(&dev->exp.vf[i]->qdev, &local_err);
+        if (local_err) {
+            fprintf(stderr, "Failed to unplug: %s\n",
+                    error_get_pretty(local_err));
+            error_free(local_err);
+        }
+    }
+    dev->exp.num_vfs = 0;
+}
+
+
+void pcie_sriov_config_write(PCIDevice *dev, uint32_t address, uint32_t val, int len)
+{
+    uint32_t off;
+    uint16_t sriov_cap = dev->exp.sriov_cap;
+
+    if (!sriov_cap || address < sriov_cap) {
+        return;
+    }
+    off = address - sriov_cap;
+    if (off >= PCI_EXT_CAP_SRIOV_SIZEOF) {
+        return;
+    }
+
+    PCIE_DEV_PRINTF(dev, "cap at 0x%x sriov offset 0x%x val 0x%x len %d\n",
+                 sriov_cap, off, val, len);
+
+    if (range_covers_byte(off, len, PCI_SRIOV_CTRL)) {
+        if (dev->exp.num_vfs) {
+            if (!(val & PCI_SRIOV_CTRL_VFE)) {
+                pcie_sriov_reset_vfs(dev);
+            }
+        } else {
+            if (val & PCI_SRIOV_CTRL_VFE) {
+                pcie_sriov_create_vfs(dev);
+            }
+        }
+    }
+}
include/hw/pci/pci.h +0 -2
index c352c7b..d36c68d 100644
@@ -11,8 +11,6 @@
 /* PCI includes legacy ISA access.  */
 #include "hw/isa/isa.h"
 
-#include "hw/pci/pcie.h"
-
 /* PCI bus */
 
 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))

include/hw/pci/pci.h enum { · line 131

Quoted 7 lines
include/hw/pci/pci.h +1 -0
@@ -127,6 +125,7 @@ enum {
 #define QEMU_PCI_VGA_IO_HI_SIZE 0x20
 
 #include "hw/pci/pci_regs.h"
+#include "hw/pci/pcie.h"
 
 /* PCI HEADER_TYPE */
 #define  PCI_HEADER_TYPE_MULTI_FUNCTION 0x80

why move it here?

Quoted 15 lines
include/hw/pci/pci.h +3 -0
@@ -413,6 +412,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
 void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque);
 
+pcibus_t pci_bar_address(PCIDevice *d,
+                         int reg, uint8_t type, pcibus_t size);
+
 static inline void
 pci_set_byte(uint8_t *config, uint8_t val)
 {
include/hw/pci/pcie.h +0 -0
index d139d58..b34d831 100644

include/hw/pci/pcie.h struct PCIExpressDevice { · line 81

Quoted 8 lines
include/hw/pci/pcie.h +5 -0
@@ -74,6 +74,15 @@ struct PCIExpressDevice {
     /* AER */
     uint16_t aer_cap;
     PCIEAERLog aer_log;
+
+    /* SR/IOV */
+    uint16_t sriov_cap;
+    uint16_t num_vfs;           /* Number of virtual functions created */
+    bool is_vf;                 /* Set if this device is a virtual function */

Checking pf pointer not enough?

include/hw/pci/pcie.h struct PCIExpressDevice { · line 85

Quoted 4 lines
include/hw/pci/pcie.h +4 -0
+    const char *vfname;         /* Reference to the device type used for the VFs */
+    PCIDevice **vf;             /* Pointer to an array of num_vfs VF devices */
+    PCIDevice *pf;              /* Pointer back to owner physical function */
+    uint8_t vf_bar_type[PCI_NUM_REGIONS];  /* Store type for each VF bar */

There are two types of fields here: some are per-vf, some are sriov fields in pf.
Pls make this clearer:
- create structures for each group
- name fields sriov_pf and sriov_vf accordingly.

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 131

Quoted 12 lines
include/hw/pci/pcie.h +5 -0
 };
 
 #define COMPAT_PROP_PCP "power_controller_present"
@@ -115,6 +124,23 @@ void pcie_add_capability(PCIDevice *dev,
                          uint16_t offset, uint16_t size);
 
 void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn);
+void pcie_sriov_init(PCIDevice *dev, uint16_t offset,
+                     const char *vfname, uint16_t vf_dev_id,
+                     uint16_t init_vfs, uint16_t total_vfs);
+void pcie_sriov_exit(PCIDevice *dev);
+

Assumption is, this applies to pf only?

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 139

Quoted 8 lines
include/hw/pci/pcie.h +8 -0
+/* Set up a VF bar in the SR/IOV bar area */
+void pcie_sriov_init_bar(PCIDevice *s, int region_num,
+                         uint8_t type, dma_addr_t size);
+
+/* Instantiate a bar for a VF */
+void pcie_register_vf_bar(PCIDevice *pci_dev, int region_num,
+                          MemoryRegion *memory);
+

Again, this isn't very clear. prefix everything with pcie_sriov_pf_ and pcie_sriov_vf_ to make it clear what each applies to.

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 140

Quoted 1 line
include/hw/pci/pcie.h +1 -0
+void pcie_sriov_create_vfs(PCIDevice *dev);

Do we need to export this one?

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 141

Quoted 1 line
include/hw/pci/pcie.h +1 -0
+void pcie_sriov_reset_vfs(PCIDevice *dev);

How about pcie_sriov_reset? Callers don't need to know what you do internally I think.

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 145

Quoted 4 lines
include/hw/pci/pcie.h +2 -0
+void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
+                             uint32_t val, int len);
 
 extern const VMStateDescription vmstate_pcie_device;

I'm not sure which functions apply to pfs,vfs, and which to both pfs and vfs. function names should make this clear.

Quoted 3 lines
Signature · 2 lines
-- 
1.9.0
author
#4 lore raw

Re: [Qemu-devel] [PATCH 2/4] pcie: Add support for Single Root I/O Virtualization (SR/IOV)

Quoted 94 lines · Michael S. Tsirkin
Quoted 93 lines · Knut Omang

This patch provides the building blocks for creating an SR/IOV PCIe Extended Capability header and creating and removing SR/IOV Virtual Functions.

---
 hw/pci/pci.c          | 107 +++++++++++++++++++-------
 hw/pci/pcie.c         | 205 +++++++++++++++++++++++++++++++++++++++++++++++++-
 include/hw/pci/pci.h  |   6 +-
 include/hw/pci/pcie.h |  26 +++++++
 4 files changed, 311 insertions(+), 33 deletions(-)
hw/pci/pci.c +28 -14
index daeaeac..071ab81 100644
@@ -35,7 +35,6 @@
 #include "hw/pci/msi.h"
 #include "hw/pci/msix.h"
 #include "exec/address-spaces.h"
-#include "hw/hotplug.h"
 
 //#define DEBUG_PCI
 #ifdef DEBUG_PCI
@@ -126,6 +125,9 @@ static int pci_bar(PCIDevice *d, int reg)
 {
     uint8_t type;
 
+    /* PCIe virtual functions do not have their own BARs */
+    assert(!d->exp.is_vf);
+
     if (reg != PCI_ROM_SLOT)
         return PCI_BASE_ADDRESS_0 + reg * 4;
 
@@ -184,22 +186,13 @@ void pci_device_deassert_intx(PCIDevice *dev)
     }
 }
 
-static void pci_do_device_reset(PCIDevice *dev)
+static void pci_reset_regions(PCIDevice *dev)
 {
     int r;
+    if (dev->exp.is_vf) {
+        return;
+    }
 
-    pci_device_deassert_intx(dev);
-    assert(dev->irq_state == 0);
-
-    /* Clear all writable bits */
-    pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
-                                 pci_get_word(dev->wmask + PCI_COMMAND) |
-                                 pci_get_word(dev->w1cmask + PCI_COMMAND));
-    pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
-                                 pci_get_word(dev->wmask + PCI_STATUS) |
-                                 pci_get_word(dev->w1cmask + PCI_STATUS));
-    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
-    dev->config[PCI_INTERRUPT_LINE] = 0x0;
     for (r = 0; r < PCI_NUM_REGIONS; ++r) {
         PCIIORegion *region = &dev->io_regions[r];
         if (!region->size) {
@@ -213,6 +206,27 @@ static void pci_do_device_reset(PCIDevice *dev)
             pci_set_long(dev->config + pci_bar(dev, r), region->type);
         }
     }
+}
+
+static void pci_do_device_reset(PCIDevice *dev)
+{
+    qdev_reset_all(&dev->qdev);
+
+    dev->irq_state = 0;
+    pci_update_irq_status(dev);
+    pci_device_deassert_intx(dev);
+    assert(dev->irq_state == 0);
+
+    /* Clear all writable bits */
+    pci_word_test_and_clear_mask(dev->config + PCI_COMMAND,
+                                 pci_get_word(dev->wmask + PCI_COMMAND) |
+                                 pci_get_word(dev->w1cmask + PCI_COMMAND));
+    pci_word_test_and_clear_mask(dev->config + PCI_STATUS,
+                                 pci_get_word(dev->wmask + PCI_STATUS) |
+                                 pci_get_word(dev->w1cmask + PCI_STATUS));
+    dev->config[PCI_CACHE_LINE_SIZE] = 0x0;
+    dev->config[PCI_INTERRUPT_LINE] = 0x0;
+    pci_reset_regions(dev);
     pci_update_mappings(dev);
 
     msi_reset(dev);
@@ -734,6 +748,14 @@ static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev)
         dev->config[PCI_HEADER_TYPE] |= PCI_HEADER_TYPE_MULTI_FUNCTION;
     }
 
Quoted 5 lines

hw/pci/pci.c static int pci_init_multifunction(PCIBus *bus, PCIDevice *dev) · line 753

Quoted 3 lines
hw/pci/pci.c +3 -0
+    /* With SR/IOV and ARI, subsequent function 0's are only
+     * another VF which the physical function is placed in the initial
+     * function (0.0)

Couldn't parse this. Could you please reword?

The main point is that if this is a VF (it references a pf), the below checks for multifunction on function 0 does not apply, as it is really a virtual function which happens to get a devfn which yields a function number 0 in the legacy pci interpretation.

I realize I would need (at least for the purpose of documentation) to do some more on the VF_OFFSET and VF_STRIDE,
which I have assumed are both 1 for simplicity, see comment below,

Quoted 43 lines
Quoted 43 lines
hw/pci/pci.c +27 -2
+     */
+    if (dev->exp.pf && dev->exp.pf->cap_present & QEMU_PCI_CAP_MULTIFUNCTION) {
+        return 0;
+    }
+
     /*
      * multifunction bit is interpreted in two ways as follows.
      *   - all functions must set the bit to 1.
@@ -920,6 +942,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,
     uint64_t wmask;
     pcibus_t size = memory_region_size(memory);
 
+    assert(!pci_dev->exp.is_vf); /* VFs must use pcie_register_vf_bar */
     assert(region_num >= 0);
     assert(region_num < PCI_NUM_REGIONS);
     if (size & (size-1)) {
@@ -1018,18 +1041,51 @@ pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num)
     return pci_dev->io_regions[region_num].addr;
 }
 
-static pcibus_t pci_bar_address(PCIDevice *d,
-				int reg, uint8_t type, pcibus_t size)
+
+static pcibus_t pci_config_get_bar_addr(PCIDevice *d, int reg,
+                                        uint8_t type, pcibus_t size)
+{
+    pcibus_t new_addr;
+    if (!d->exp.is_vf) {
+        int bar = pci_bar(d, reg);
+        if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
+            new_addr = pci_get_quad(d->config + bar);
+        } else {
+            new_addr = pci_get_long(d->config + bar);
+        }
+    } else {
+        int bar = d->exp.pf->exp.sriov_cap + PCI_SRIOV_BAR + reg * 4;
+        uint32_t vf_num = d->devfn - (d->exp.pf->devfn + 1);
+        if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
+            new_addr = pci_get_quad(d->exp.pf->config + bar);
+        } else {
+            new_addr = pci_get_long(d->exp.pf->config + bar);
+        }
+        new_addr += vf_num * size;
Quoted 8 lines

hw/pci/pci.c pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num) · line 1069

Quoted 5 lines
hw/pci/pci.c +5 -0
+
+        PCI_DPRINTF("%02d:%02d.%d: (vf %d) found config reg %d, "
+                    "size 0x%lx addr 0x%lx\n",
+                    pci_bus_num(d->bus), d->devfn >> 3, d->devfn & 7,
+                    vf_num, reg, size, new_addr);

I don't think this is needed, assuming it is, don't we want this for all functions, bot just vfs?

It was useful for debugging the size and address calculations, which are less obvious than in the plain PCI case since they are derived from the VF bars in the PF, but I can remove it.

Quoted 15 lines
Quoted 15 lines
hw/pci/pci.c +10 -1
+    }
+    if (reg != PCI_ROM_SLOT) {
+        /* Preserve the rom enable bit */
+        new_addr &= ~(size - 1);
+    }
+    return new_addr;
+}
+
+pcibus_t pci_bar_address(PCIDevice *d,
+                         int reg, uint8_t type, pcibus_t size)
 {
     pcibus_t new_addr, last_addr;
-    int bar = pci_bar(d, reg);
     uint16_t cmd = pci_get_word(d->config + PCI_COMMAND);
 
Quoted 8 lines

hw/pci/pci.c pcibus_t pci_get_bar_addr(PCIDevice *pci_dev, int region_num) · line 1088

Quoted 6 lines
hw/pci/pci.c +1 -1
     if (type & PCI_BASE_ADDRESS_SPACE_IO) {
         if (!(cmd & PCI_COMMAND_IO)) {
             return PCI_BAR_UNMAPPED;
         }
-        new_addr = pci_get_long(d->config + bar) & ~(size - 1);
+        new_addr = pci_config_get_bar_addr(d, reg, type, size);

Hmm this is IO, VFs don't have IO, do they? Change really necessary?

I think it simplifies the code, notice that the outer bar variable is not needed elsewhere, so I would have had to change something in that if() anyway.

Quoted 32 lines
Quoted 32 lines
hw/pci/pci.c +3 -6
         last_addr = new_addr + size - 1;
         /* Check if 32 bit BAR wraps around explicitly.
          * TODO: make priorities correct and remove this work around.
@@ -1043,11 +1099,7 @@ static pcibus_t pci_bar_address(PCIDevice *d,
     if (!(cmd & PCI_COMMAND_MEMORY)) {
         return PCI_BAR_UNMAPPED;
     }
-    if (type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
-        new_addr = pci_get_quad(d->config + bar);
-    } else {
-        new_addr = pci_get_long(d->config + bar);
-    }
+    new_addr = pci_config_get_bar_addr(d, reg, type, size);
     /* the ROM slot has a specific enable bit */
     if (reg == PCI_ROM_SLOT && !(new_addr & PCI_ROM_ADDRESS_ENABLE)) {
         return PCI_BAR_UNMAPPED;
@@ -1146,9 +1198,10 @@ uint32_t pci_default_read_config(PCIDevice *d,
     return le32_to_cpu(val);
 }
 
-void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
+void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val_in, int l)
 {
     int i, was_irq_disabled = pci_irq_disabled(d);
+    uint32_t val = val_in;
 
     for (i = 0; i < l; val >>= 8, ++i) {
         uint8_t wmask = d->wmask[addr + i];
@@ -1170,8 +1223,9 @@ void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l)
                                     & PCI_COMMAND_MASTER);
     }
 
Quoted 10 lines

hw/pci/pci.c void pci_default_write_config(PCIDevice *d, uint32_t addr, uint32_t val, int l) · line 1229

Quoted 6 lines
hw/pci/pci.c +3 -2
-    msi_write_config(d, addr, val, l);
-    msix_write_config(d, addr, val, l);
+    msi_write_config(d, addr, val_in, l);
+    msix_write_config(d, addr, val_in, l);
+    pcie_sriov_config_write(d, addr, val_in, l);
 }

Not sure I get this chunk.
A code comment might be helpful.

I believe I fix a bug here - should perhaps have been a separate patch (should I factor it out, if you agree?)
This fix lost my attention as things started to work and I moved forward:

The old code would never propagate any set bits through to the msi/msix write functions as they get lost in the shift inside for(..) above, which is why I introduced a new local val and renamed the argument to val_in.

Otherwise it is just another capability subscribing for config writes, eg. pcie_sriov_config_write would only have any effect if the config address matches a valid sriov capability, which is similar usage as for the msi and msix calls.

Quoted 13 lines

hw/pci/pci.c static int pci_qdev_init(DeviceState *qdev) · line 1835

Quoted 10 lines
hw/pci/pci.c +0 -1
 /***********************************************************/
@@ -1776,7 +1830,6 @@ static int pci_qdev_init(DeviceState *qdev)
         is_default_rom = true;
     }
     pci_add_option_rom(pci_dev, is_default_rom);
-
     return 0;
 }
 

generally, don't make unrelated changes, review is hard enough.

Sorry about that - I tried to look for unintended white space changes but some seems to have slipped by anyway - the code went through several rebases on the way..

Quoted 30 lines
Quoted 30 lines
hw/pci/pcie.c +4 -3
index 6cb6e0c..b7af693 100644
@@ -254,7 +254,7 @@ void pcie_cap_slot_hotplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
      * Right now, only a device of function = 0 is allowed to be
      * hot plugged/unplugged.
      */
-    assert(PCI_FUNC(pci_dev->devfn) == 0);
+    assert(PCI_FUNC(pci_dev->devfn) == 0 || pci_dev->exp.is_vf);
 
     pci_word_test_and_set_mask(exp_cap + PCI_EXP_SLTSTA,
                                PCI_EXP_SLTSTA_PDS);
@@ -266,10 +266,11 @@ void pcie_cap_slot_hot_unplug_cb(HotplugHandler *hotplug_dev, DeviceState *dev,
                                  Error **errp)
 {
     uint8_t *exp_cap;
+    PCIDevice *pdev = PCI_DEVICE(hotplug_dev);
 
-    pcie_cap_slot_hotplug_common(PCI_DEVICE(hotplug_dev), dev, &exp_cap, errp);
+    pcie_cap_slot_hotplug_common(pdev, dev, &exp_cap, errp);
 
-    pcie_cap_slot_push_attention_button(PCI_DEVICE(hotplug_dev));
+    pcie_cap_slot_push_attention_button(pdev);
 }
 
 /* pci express slot for pci express root/downstream port
@@ -409,7 +410,7 @@ void pcie_cap_slot_write_config(PCIDevice *dev,
     }
 
Quoted 15 lines

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 639

Quoted 13 lines
hw/pci/pcie.c +4 -1
     /*
-     * If the slot is polulated, power indicator is off and power
+     * If the slot is populated, power indicator is off and power
      * controller is off, it is safe to detach the devices.
      */
     if ((sltsta & PCI_EXP_SLTSTA_PDS) && (val & PCI_EXP_SLTCTL_PCC) &&
@@ -633,3 +634,199 @@ void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn)
                         offset, PCI_ARI_SIZEOF);
     pci_set_long(dev->config + offset + PCI_ARI_CAP, (nextfn & 0xff) << 8);
 }
+
+
+

don't add > 1 empty line in a row.

Get it - will clean up - same for the rest of the unintended white space changes..

Quoted 3 lines

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 640

Quoted 1 line
hw/pci/pcie.c +1 -0
+/* SR/IOV */

this comment is really useless, function name makes it clear.

I just tried to follow the style in the source file - eg. /* ARI */ above,

Continuing with your suggestions for better datatypes below, I ended up factoring the sriov code out into a separate source/header file pair simular to the aer code instead, hope that's ok.

Quoted 8 lines

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 646

Quoted 6 lines
hw/pci/pcie.c +6 -0
+void pcie_sriov_init(PCIDevice *dev, uint16_t offset,
+                     const char *vfname, uint16_t vf_dev_id,
+                     uint16_t init_vfs, uint16_t total_vfs)
+{
+    uint8_t *cfg = dev->config + offset;
+    uint8_t *wmask;

empty line here after variables.

Good, I like that myself too,
the existing code uses both 0 and 1 lines,

Quoted 7 lines

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 651

Quoted 5 lines
hw/pci/pcie.c +5 -0
+    pcie_add_capability(dev, PCI_EXT_CAP_ID_SRIOV, 1,
+                        offset, PCI_EXT_CAP_SRIOV_SIZEOF);
+    dev->exp.sriov_cap = offset;
+    dev->exp.num_vfs = 0;
+    dev->exp.vfname = g_strdup(vfname);

I don't see a free for this field anywhere.

Ouch - will fix.

Quoted 5 lines

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 654

Quoted 3 lines
hw/pci/pcie.c +3 -0
+    dev->exp.vf = NULL;
+
+    /* set some sensible defaults - devices can override later */

do they in fact override?

Hmm - I realize I need to handle this better, at present it is hard coded to work for stride 1, offset 1 - it should be relatively easy to be more generic.

If a device implementing SR/IOV wants stride > 1 or for instance has more than 1 PF, it needs to start at offset > 1 or supports more pages sizes, it would have to set other values accordingly.

The igb I have experimented with in the lab actually sets stride 2 - it also sets offset 0x80 that is, it implements two PFs at function 0 and 1 and the VFs from each PF ends
up interleaved at every second function, starting at devfn 10.0. In my igb example code, I deliberately just used the hard coded stride 1, offset 1 - will fix that instead of documenting the misfeature..

Quoted 6 lines

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 657

Quoted 3 lines
hw/pci/pcie.c +3 -0
+    pci_set_word(cfg + PCI_SRIOV_VF_OFFSET, 0x1);
+    pci_set_word(cfg + PCI_SRIOV_VF_STRIDE, 0x1);
+    pci_set_word(cfg + PCI_SRIOV_SUP_PGSIZE, 0x553);

what's this magic constant?
needs a comment.

I agree, will add it - the mandatory set of page sizes to support.

Quoted 12 lines
Quoted 12 lines
hw/pci/pcie.c +12 -0
+    pci_set_word(cfg + PCI_SRIOV_SYS_PGSIZE, 0x1);
+
+    /* Set up device ID and initial/total number of VFs available */
+    pci_set_word(cfg + PCI_SRIOV_VF_DID, vf_dev_id);
+    pci_set_word(cfg + PCI_SRIOV_INITIAL_VF, init_vfs);
+    pci_set_word(cfg + PCI_SRIOV_TOTAL_VF, total_vfs);
+
+    /* Write enable control bits */
+    wmask = dev->wmask + offset;
+    pci_set_word(wmask + PCI_SRIOV_CTRL,
+                 PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE | PCI_SRIOV_CTRL_ARI);
+    pci_set_word(wmask + PCI_SRIOV_NUM_VF, 0xffff);
Quoted 8 lines

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 674

Quoted 5 lines
hw/pci/pcie.c +5 -0
+
+    qdev_prop_set_bit(&dev->qdev, "multifunction", true);
+}
+
+

2 empty lines again

sorry, old habits...

Quoted 50 lines
Quoted 37 lines
hw/pci/pcie.c +37 -0
+void pcie_sriov_exit(PCIDevice *dev)
+{
+    PCIE_DPRINTF("\n");
+    pcie_sriov_reset_vfs(dev);
+}
+
+void pcie_sriov_init_bar(PCIDevice *dev, int region_num,
+                         uint8_t type, dma_addr_t size)
+{
+    uint32_t addr;
+    uint64_t wmask;
+    uint16_t sriov_cap = dev->exp.sriov_cap;
+
+    assert(sriov_cap > 0);
+    assert(region_num >= 0);
+    assert(region_num < PCI_NUM_REGIONS);
+    assert(region_num != PCI_ROM_SLOT);
+
+    wmask = ~(size - 1);
+    addr = sriov_cap + PCI_SRIOV_BAR + region_num * 4;
+
+    pci_set_long(dev->config + addr, type);
+    if (!(type & PCI_BASE_ADDRESS_SPACE_IO) &&
+        type & PCI_BASE_ADDRESS_MEM_TYPE_64) {
+        pci_set_quad(dev->wmask + addr, wmask);
+        pci_set_quad(dev->cmask + addr, ~0ULL);
+    } else {
+        pci_set_long(dev->wmask + addr, wmask & 0xffffffff);
+        pci_set_long(dev->cmask + addr, 0xffffffff);
+    }
+    dev->exp.vf_bar_type[region_num] = type;
+}
+
+void pcie_register_vf_bar(PCIDevice *dev, int region_num,
+                          MemoryRegion *memory)
+{
+    PCIIORegion *r;

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 721

Quoted 10 lines
hw/pci/pcie.c +10 -0
+    uint8_t type;
+    pcibus_t size = memory_region_size(memory);
+
+    assert(dev->exp.is_vf); /* PFs must use pci_register_bar */
+    assert(region_num >= 0);
+    assert(region_num < PCI_NUM_REGIONS);
+    type = dev->exp.pf->exp.vf_bar_type[region_num];
+
+    if (size & (size-1)) {
+        fprintf(stderr, "ERROR: PCI region size must be pow2 "

power of 2

Quoted 1 line

eschew abbreviation

agree - cut'n paste from pci.c:pci_register_bar() but nevertheless...

Quoted 44 lines
Quoted 44 lines
hw/pci/pcie.c +44 -0
+                    "type=0x%x, size=0x%"FMT_PCIBUS"\n", type, size);
+        exit(1);
+    }
+
+    r = &dev->io_regions[region_num];
+    r->memory = memory;
+    r->address_space =
+        type & PCI_BASE_ADDRESS_SPACE_IO
+        ? dev->bus->address_space_io
+        : dev->bus->address_space_mem;
+    r->size = size;
+    r->type = type;
+
+    r->addr = pci_bar_address(dev, region_num, r->type, r->size);
+    if (r->addr != PCI_BAR_UNMAPPED) {
+        memory_region_add_subregion_overlap(r->address_space,
+                                            r->addr, r->memory, 1);
+    }
+}
+
+
+static PCIDevice *pcie_create_vf(PCIDevice *pf, int devfn, const char *name)
+{
+    int ret;
+    PCIDevice *dev = pci_create(pf->bus, devfn, name);
+    dev->exp.is_vf = true;
+    dev->exp.pf = pf;
+
+    ret = qdev_init(&dev->qdev);
+    if (ret)
+        return NULL;
+
+    /* set vid/did according to sr/iov spec - they are not used */
+    pci_config_set_vendor_id(dev->config, 0xffff);
+    pci_config_set_device_id(dev->config, 0xffff);
+    return dev;
+}
+
+
+void pcie_sriov_create_vfs(PCIDevice *dev)
+{
+    uint16_t num_vfs;
+    uint16_t i;
+    int32_t devfn = dev->devfn + 1;
Quoted 8 lines

hw/pci/pcie.c void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn) · line 771

Quoted 6 lines
hw/pci/pcie.c +6 -0
+    uint16_t sriov_cap = dev->exp.sriov_cap;
+
+    assert(sriov_cap > 0);
+    num_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF);
+
+    dev->exp.vf = g_malloc(sizeof(PCIDevice*) * num_vfs);

Don't see a free to match this malloc call.

You're quite right - will fix, thanks,

Quoted 77 lines
Quoted 77 lines
hw/pci/pcie.c +61 -0
+    assert(dev->exp.vf);
+
+    PCIE_DEV_PRINTF(dev, "creating %d vf devs\n", num_vfs);
+    for (i = 0; i < num_vfs; i++) {
+        dev->exp.vf[i] = pcie_create_vf(dev, devfn++, dev->exp.vfname);
+        if (!dev->exp.vf[i]) {
+            PCIE_DEV_PRINTF(dev, "Failed to create VF %d\n", i);
+            num_vfs = i;
+            break;
+        }
+    }
+    dev->exp.num_vfs = num_vfs;
+}
+
+
+void pcie_sriov_reset_vfs(PCIDevice *dev)
+{
+    Error *local_err = NULL;
+    uint16_t num_vfs = dev->exp.num_vfs;
+    uint16_t i;
+    PCIE_DEV_PRINTF(dev, "Resetting %d vf devs\n", num_vfs);
+    for (i = 0; i < num_vfs; i++) {
+        qdev_unplug(&dev->exp.vf[i]->qdev, &local_err);
+        if (local_err) {
+            fprintf(stderr, "Failed to unplug: %s\n",
+                    error_get_pretty(local_err));
+            error_free(local_err);
+        }
+    }
+    dev->exp.num_vfs = 0;
+}
+
+
+void pcie_sriov_config_write(PCIDevice *dev, uint32_t address, uint32_t val, int len)
+{
+    uint32_t off;
+    uint16_t sriov_cap = dev->exp.sriov_cap;
+
+    if (!sriov_cap || address < sriov_cap) {
+        return;
+    }
+    off = address - sriov_cap;
+    if (off >= PCI_EXT_CAP_SRIOV_SIZEOF) {
+        return;
+    }
+
+    PCIE_DEV_PRINTF(dev, "cap at 0x%x sriov offset 0x%x val 0x%x len %d\n",
+                 sriov_cap, off, val, len);
+
+    if (range_covers_byte(off, len, PCI_SRIOV_CTRL)) {
+        if (dev->exp.num_vfs) {
+            if (!(val & PCI_SRIOV_CTRL_VFE)) {
+                pcie_sriov_reset_vfs(dev);
+            }
+        } else {
+            if (val & PCI_SRIOV_CTRL_VFE) {
+                pcie_sriov_create_vfs(dev);
+            }
+        }
+    }
+}
include/hw/pci/pci.h +0 -2
index c352c7b..d36c68d 100644
@@ -11,8 +11,6 @@
 /* PCI includes legacy ISA access.  */
 #include "hw/isa/isa.h"
 
-#include "hw/pci/pcie.h"
-
 /* PCI bus */
 
 #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
@@ -127,6 +125,7 @@ enum {
 #define QEMU_PCI_VGA_IO_HI_SIZE 0x20
 
Quoted 7 lines

include/hw/pci/pci.h enum { · line 131

Quoted 5 lines
include/hw/pci/pci.h +1 -0
 #include "hw/pci/pci_regs.h"
+#include "hw/pci/pcie.h"
 
 /* PCI HEADER_TYPE */
 #define  PCI_HEADER_TYPE_MULTI_FUNCTION 0x80

why move it here?

pcie.h now depends on PCI_NUM_REGIONS which are declared in pci.h after the old pcie.h include.

Quoted 17 lines
Quoted 17 lines
include/hw/pci/pci.h +3 -0
@@ -413,6 +412,9 @@ typedef AddressSpace *(*PCIIOMMUFunc)(PCIBus *, void *, int);
 AddressSpace *pci_device_iommu_address_space(PCIDevice *dev);
 void pci_setup_iommu(PCIBus *bus, PCIIOMMUFunc fn, void *opaque);
 
+pcibus_t pci_bar_address(PCIDevice *d,
+                         int reg, uint8_t type, pcibus_t size);
+
 static inline void
 pci_set_byte(uint8_t *config, uint8_t val)
 {
include/hw/pci/pcie.h +0 -0
index d139d58..b34d831 100644
@@ -74,6 +74,15 @@ struct PCIExpressDevice {
     /* AER */
     uint16_t aer_cap;
Quoted 8 lines

include/hw/pci/pcie.h struct PCIExpressDevice { · line 81

Quoted 6 lines
include/hw/pci/pcie.h +5 -0
     PCIEAERLog aer_log;
+
+    /* SR/IOV */
+    uint16_t sriov_cap;
+    uint16_t num_vfs;           /* Number of virtual functions created */
+    bool is_vf;                 /* Set if this device is a virtual function */

Checking pf pointer not enough?

Should be - was considering it, but kept it to
be coherent with is_express and is_bridge.
Will eliminate and add+use a pci_is_vf()
instead,

Quoted 10 lines

include/hw/pci/pcie.h struct PCIExpressDevice { · line 85

Quoted 4 lines
include/hw/pci/pcie.h +4 -0
+    const char *vfname;         /* Reference to the device type used for the VFs */
+    PCIDevice **vf;             /* Pointer to an array of num_vfs VF devices */
+    PCIDevice *pf;              /* Pointer back to owner physical function */
+    uint8_t vf_bar_type[PCI_NUM_REGIONS];  /* Store type for each VF bar */

There are two types of fields here: some are per-vf, some are sriov fields in pf.
Pls make this clearer:
- create structures for each group
- name fields sriov_pf and sriov_vf accordingly.

Was unsure about desired number of levels vs abstraction for readability - will do as you suggest,

Quoted 6 lines
Quoted 6 lines
include/hw/pci/pcie.h +0 -0
 };
 
 #define COMPAT_PROP_PCP "power_controller_present"
@@ -115,6 +124,23 @@ void pcie_add_capability(PCIDevice *dev,
                          uint16_t offset, uint16_t size);
 
Quoted 8 lines

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 131

Quoted 6 lines
include/hw/pci/pcie.h +5 -0
 void pcie_ari_init(PCIDevice *dev, uint16_t offset, uint16_t nextfn);
+void pcie_sriov_init(PCIDevice *dev, uint16_t offset,
+                     const char *vfname, uint16_t vf_dev_id,
+                     uint16_t init_vfs, uint16_t total_vfs);
+void pcie_sriov_exit(PCIDevice *dev);
+

Assumption is, this applies to pf only?

I see your concern - similar to the one I had with ARIfwd vs ARI cap. will make it clearer!

Quoted 4 lines
Quoted 4 lines
include/hw/pci/pcie.h +4 -0
+/* Set up a VF bar in the SR/IOV bar area */
+void pcie_sriov_init_bar(PCIDevice *s, int region_num,
+                         uint8_t type, dma_addr_t size);
+
Quoted 8 lines

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 139

Quoted 4 lines
include/hw/pci/pcie.h +4 -0
+/* Instantiate a bar for a VF */
+void pcie_register_vf_bar(PCIDevice *pci_dev, int region_num,
+                          MemoryRegion *memory);
+

Again, this isn't very clear. prefix everything with pcie_sriov_pf_ and pcie_sriov_vf_ to make it clear what each applies to.

Yes

Quoted 6 lines

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 140

Quoted 1 line
include/hw/pci/pcie.h +1 -0
+void pcie_sriov_create_vfs(PCIDevice *dev);

Do we need to export this one?

Quoted 1 line
include/hw/pci/pcie.h +1 -0
+void pcie_sriov_reset_vfs(PCIDevice *dev);

Hmm, it is needed both as a result of PF reset and during destruction of a device (which does not necessarily yield a PCI reset) and I call it from the igb code,

but your comment makes me realize:

1) I should probably rename the create/reset_vfs functions to pcie_sriov_pf_register/unregister_vfs() or similar as that would better reflect what it does.

2) call reset (unregister) unconditionally for SR/IOV devices (PFs) from pci_unregister_device()

Quoted 2 lines

How about pcie_sriov_reset? Callers don't need to know what you do internally I think.

Actually, it seems the external API can be made a lot simpler, working on that,...

Quoted 8 lines

include/hw/pci/pcie.h void pcie_add_capability(PCIDevice *dev, · line 145

Quoted 4 lines
include/hw/pci/pcie.h +2 -0
+void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
+                             uint32_t val, int len);
 
 extern const VMStateDescription vmstate_pcie_device;

I'm not sure which functions apply to pfs,vfs, and which to both pfs and vfs. function names should make this clear.

I get it,..

Thanks a lot for your thorough review,

Knut

Quoted 2 lines
Quoted 2 lines
Signature · 1 line
1.9.0