|
@@ -20,23 +20,37 @@
|
|
|
#include "qapi/error.h"
|
|
|
#include "trace.h"
|
|
|
|
|
|
-static PCIDevice *register_vf(PCIDevice *pf, int devfn,
|
|
|
- const char *name, uint16_t vf_num);
|
|
|
-static void unregister_vfs(PCIDevice *dev);
|
|
|
+static void unparent_vfs(PCIDevice *dev, uint16_t total_vfs)
|
|
|
+{
|
|
|
+ for (uint16_t i = 0; i < total_vfs; i++) {
|
|
|
+ PCIDevice *vf = dev->exp.sriov_pf.vf[i];
|
|
|
+ object_unparent(OBJECT(vf));
|
|
|
+ object_unref(OBJECT(vf));
|
|
|
+ }
|
|
|
+ g_free(dev->exp.sriov_pf.vf);
|
|
|
+ dev->exp.sriov_pf.vf = NULL;
|
|
|
+}
|
|
|
|
|
|
-void pcie_sriov_pf_init(PCIDevice *dev, uint16_t offset,
|
|
|
+bool pcie_sriov_pf_init(PCIDevice *dev, uint16_t offset,
|
|
|
const char *vfname, uint16_t vf_dev_id,
|
|
|
uint16_t init_vfs, uint16_t total_vfs,
|
|
|
- uint16_t vf_offset, uint16_t vf_stride)
|
|
|
+ uint16_t vf_offset, uint16_t vf_stride,
|
|
|
+ Error **errp)
|
|
|
{
|
|
|
+ BusState *bus = qdev_get_parent_bus(&dev->qdev);
|
|
|
+ int32_t devfn = dev->devfn + vf_offset;
|
|
|
uint8_t *cfg = dev->config + offset;
|
|
|
uint8_t *wmask;
|
|
|
|
|
|
+ if (total_vfs &&
|
|
|
+ (uint32_t)devfn + (uint32_t)(total_vfs - 1) * vf_stride >= PCI_DEVFN_MAX) {
|
|
|
+ error_setg(errp, "VF addr overflows");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
pcie_add_capability(dev, PCI_EXT_CAP_ID_SRIOV, 1,
|
|
|
offset, PCI_EXT_CAP_SRIOV_SIZEOF);
|
|
|
dev->exp.sriov_cap = offset;
|
|
|
- dev->exp.sriov_pf.num_vfs = 0;
|
|
|
- dev->exp.sriov_pf.vfname = g_strdup(vfname);
|
|
|
dev->exp.sriov_pf.vf = NULL;
|
|
|
|
|
|
pci_set_word(cfg + PCI_SRIOV_VF_OFFSET, vf_offset);
|
|
@@ -69,13 +83,37 @@ void pcie_sriov_pf_init(PCIDevice *dev, uint16_t offset,
|
|
|
pci_set_word(wmask + PCI_SRIOV_SYS_PGSIZE, 0x553);
|
|
|
|
|
|
qdev_prop_set_bit(&dev->qdev, "multifunction", true);
|
|
|
+
|
|
|
+ dev->exp.sriov_pf.vf = g_new(PCIDevice *, total_vfs);
|
|
|
+
|
|
|
+ for (uint16_t i = 0; i < total_vfs; i++) {
|
|
|
+ PCIDevice *vf = pci_new(devfn, vfname);
|
|
|
+ vf->exp.sriov_vf.pf = dev;
|
|
|
+ vf->exp.sriov_vf.vf_number = i;
|
|
|
+
|
|
|
+ if (!qdev_realize(&vf->qdev, bus, errp)) {
|
|
|
+ object_unparent(OBJECT(vf));
|
|
|
+ object_unref(vf);
|
|
|
+ unparent_vfs(dev, i);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* set vid/did according to sr/iov spec - they are not used */
|
|
|
+ pci_config_set_vendor_id(vf->config, 0xffff);
|
|
|
+ pci_config_set_device_id(vf->config, 0xffff);
|
|
|
+
|
|
|
+ dev->exp.sriov_pf.vf[i] = vf;
|
|
|
+ devfn += vf_stride;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
void pcie_sriov_pf_exit(PCIDevice *dev)
|
|
|
{
|
|
|
- unregister_vfs(dev);
|
|
|
- g_free((char *)dev->exp.sriov_pf.vfname);
|
|
|
- dev->exp.sriov_pf.vfname = NULL;
|
|
|
+ uint8_t *cfg = dev->config + dev->exp.sriov_cap;
|
|
|
+
|
|
|
+ unparent_vfs(dev, pci_get_word(cfg + PCI_SRIOV_TOTAL_VF));
|
|
|
}
|
|
|
|
|
|
void pcie_sriov_pf_init_vf_bar(PCIDevice *dev, int region_num,
|
|
@@ -141,80 +179,36 @@ void pcie_sriov_vf_register_bar(PCIDevice *dev, int region_num,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static PCIDevice *register_vf(PCIDevice *pf, int devfn, const char *name,
|
|
|
- uint16_t vf_num)
|
|
|
-{
|
|
|
- PCIDevice *dev = pci_new(devfn, name);
|
|
|
- dev->exp.sriov_vf.pf = pf;
|
|
|
- dev->exp.sriov_vf.vf_number = vf_num;
|
|
|
- PCIBus *bus = pci_get_bus(pf);
|
|
|
- Error *local_err = NULL;
|
|
|
-
|
|
|
- qdev_realize(&dev->qdev, &bus->qbus, &local_err);
|
|
|
- if (local_err) {
|
|
|
- error_report_err(local_err);
|
|
|
- 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;
|
|
|
-}
|
|
|
-
|
|
|
static void register_vfs(PCIDevice *dev)
|
|
|
{
|
|
|
uint16_t num_vfs;
|
|
|
uint16_t i;
|
|
|
uint16_t sriov_cap = dev->exp.sriov_cap;
|
|
|
- uint16_t vf_offset =
|
|
|
- pci_get_word(dev->config + sriov_cap + PCI_SRIOV_VF_OFFSET);
|
|
|
- uint16_t vf_stride =
|
|
|
- pci_get_word(dev->config + sriov_cap + PCI_SRIOV_VF_STRIDE);
|
|
|
- int32_t devfn = dev->devfn + vf_offset;
|
|
|
|
|
|
assert(sriov_cap > 0);
|
|
|
num_vfs = pci_get_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF);
|
|
|
- if (num_vfs > pci_get_word(dev->config + sriov_cap + PCI_SRIOV_TOTAL_VF)) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- dev->exp.sriov_pf.vf = g_new(PCIDevice *, num_vfs);
|
|
|
|
|
|
trace_sriov_register_vfs(dev->name, PCI_SLOT(dev->devfn),
|
|
|
PCI_FUNC(dev->devfn), num_vfs);
|
|
|
for (i = 0; i < num_vfs; i++) {
|
|
|
- dev->exp.sriov_pf.vf[i] = register_vf(dev, devfn,
|
|
|
- dev->exp.sriov_pf.vfname, i);
|
|
|
- if (!dev->exp.sriov_pf.vf[i]) {
|
|
|
- num_vfs = i;
|
|
|
- break;
|
|
|
- }
|
|
|
- devfn += vf_stride;
|
|
|
+ pci_set_enabled(dev->exp.sriov_pf.vf[i], true);
|
|
|
}
|
|
|
- dev->exp.sriov_pf.num_vfs = num_vfs;
|
|
|
+
|
|
|
+ pci_set_word(dev->wmask + sriov_cap + PCI_SRIOV_NUM_VF, 0);
|
|
|
}
|
|
|
|
|
|
static void unregister_vfs(PCIDevice *dev)
|
|
|
{
|
|
|
- uint16_t num_vfs = dev->exp.sriov_pf.num_vfs;
|
|
|
+ uint8_t *cfg = dev->config + dev->exp.sriov_cap;
|
|
|
uint16_t i;
|
|
|
|
|
|
trace_sriov_unregister_vfs(dev->name, PCI_SLOT(dev->devfn),
|
|
|
- PCI_FUNC(dev->devfn), num_vfs);
|
|
|
- for (i = 0; i < num_vfs; i++) {
|
|
|
- Error *err = NULL;
|
|
|
- PCIDevice *vf = dev->exp.sriov_pf.vf[i];
|
|
|
- if (!object_property_set_bool(OBJECT(vf), "realized", false, &err)) {
|
|
|
- error_reportf_err(err, "Failed to unplug: ");
|
|
|
- }
|
|
|
- object_unparent(OBJECT(vf));
|
|
|
- object_unref(OBJECT(vf));
|
|
|
+ PCI_FUNC(dev->devfn));
|
|
|
+ for (i = 0; i < pci_get_word(cfg + PCI_SRIOV_TOTAL_VF); i++) {
|
|
|
+ pci_set_enabled(dev->exp.sriov_pf.vf[i], false);
|
|
|
}
|
|
|
- g_free(dev->exp.sriov_pf.vf);
|
|
|
- dev->exp.sriov_pf.vf = NULL;
|
|
|
- dev->exp.sriov_pf.num_vfs = 0;
|
|
|
+
|
|
|
+ pci_set_word(dev->wmask + dev->exp.sriov_cap + PCI_SRIOV_NUM_VF, 0xffff);
|
|
|
}
|
|
|
|
|
|
void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
|
|
@@ -235,15 +229,29 @@ void pcie_sriov_config_write(PCIDevice *dev, uint32_t address,
|
|
|
PCI_FUNC(dev->devfn), off, val, len);
|
|
|
|
|
|
if (range_covers_byte(off, len, PCI_SRIOV_CTRL)) {
|
|
|
- if (dev->exp.sriov_pf.num_vfs) {
|
|
|
- if (!(val & PCI_SRIOV_CTRL_VFE)) {
|
|
|
- unregister_vfs(dev);
|
|
|
- }
|
|
|
+ if (val & PCI_SRIOV_CTRL_VFE) {
|
|
|
+ register_vfs(dev);
|
|
|
} else {
|
|
|
- if (val & PCI_SRIOV_CTRL_VFE) {
|
|
|
- register_vfs(dev);
|
|
|
- }
|
|
|
+ unregister_vfs(dev);
|
|
|
}
|
|
|
+ } else if (range_covers_byte(off, len, PCI_SRIOV_NUM_VF)) {
|
|
|
+ uint8_t *cfg = dev->config + sriov_cap;
|
|
|
+ uint8_t *wmask = dev->wmask + sriov_cap;
|
|
|
+ uint16_t num_vfs = pci_get_word(cfg + PCI_SRIOV_NUM_VF);
|
|
|
+ uint16_t wmask_val = PCI_SRIOV_CTRL_MSE | PCI_SRIOV_CTRL_ARI;
|
|
|
+
|
|
|
+ if (num_vfs <= pci_get_word(cfg + PCI_SRIOV_TOTAL_VF)) {
|
|
|
+ wmask_val |= PCI_SRIOV_CTRL_VFE;
|
|
|
+ }
|
|
|
+
|
|
|
+ pci_set_word(wmask + PCI_SRIOV_CTRL, wmask_val);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void pcie_sriov_pf_post_load(PCIDevice *dev)
|
|
|
+{
|
|
|
+ if (dev->exp.sriov_cap) {
|
|
|
+ register_vfs(dev);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -260,6 +268,8 @@ void pcie_sriov_pf_reset(PCIDevice *dev)
|
|
|
unregister_vfs(dev);
|
|
|
|
|
|
pci_set_word(dev->config + sriov_cap + PCI_SRIOV_NUM_VF, 0);
|
|
|
+ pci_set_word(dev->wmask + sriov_cap + PCI_SRIOV_CTRL,
|
|
|
+ PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE | PCI_SRIOV_CTRL_ARI);
|
|
|
|
|
|
/*
|
|
|
* Default is to use 4K pages, software can modify it
|
|
@@ -306,7 +316,7 @@ PCIDevice *pcie_sriov_get_pf(PCIDevice *dev)
|
|
|
PCIDevice *pcie_sriov_get_vf_at_index(PCIDevice *dev, int n)
|
|
|
{
|
|
|
assert(!pci_is_vf(dev));
|
|
|
- if (n < dev->exp.sriov_pf.num_vfs) {
|
|
|
+ if (n < pcie_sriov_num_vfs(dev)) {
|
|
|
return dev->exp.sriov_pf.vf[n];
|
|
|
}
|
|
|
return NULL;
|
|
@@ -314,5 +324,10 @@ PCIDevice *pcie_sriov_get_vf_at_index(PCIDevice *dev, int n)
|
|
|
|
|
|
uint16_t pcie_sriov_num_vfs(PCIDevice *dev)
|
|
|
{
|
|
|
- return dev->exp.sriov_pf.num_vfs;
|
|
|
+ uint16_t sriov_cap = dev->exp.sriov_cap;
|
|
|
+ uint8_t *cfg = dev->config + sriov_cap;
|
|
|
+
|
|
|
+ return sriov_cap &&
|
|
|
+ (pci_get_word(cfg + PCI_SRIOV_CTRL) & PCI_SRIOV_CTRL_VFE) ?
|
|
|
+ pci_get_word(cfg + PCI_SRIOV_NUM_VF) : 0;
|
|
|
}
|