[RFC 3/4] pci/sriov: add sriov_numfs_no_scan interface

From: Longpeng(Mike)
Date: Fri Nov 11 2022 - 09:28:02 EST


From: Longpeng <longpeng2@xxxxxxxxxx>

Add sriov_numfs_no_scan interface, we can enable the sriov without
adding all VFs immediately.

For example:
echo 16 > /sys/bus/pci/devices/0000\:65\:00.0/sriov_numvfs_no_scan
( Enable 16 VFs, but not add it to the system. )

Signed-off-by: Longpeng <longpeng2@xxxxxxxxxx>
---
drivers/pci/iov.c | 64 +++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 64 insertions(+)

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index 886489aae0de..cb24d4b60e0d 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -491,6 +491,68 @@ static ssize_t sriov_numvfs_store(struct device *dev,
return count;
}

+static ssize_t sriov_numvfs_no_scan_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct pci_dev *pdev = to_pci_dev(dev);
+ int ret;
+ u16 num_vfs;
+
+ if (kstrtou16(buf, 0, &num_vfs) < 0)
+ return -EINVAL;
+
+ device_lock(&pdev->dev);
+ /* Clear the flag when disabling VFs */
+ pdev->no_vf_scan = num_vfs ? 1 : 0;
+ device_unlock(&pdev->dev);
+
+ ret = sriov_numvfs_common(pdev, num_vfs);
+ if (ret < 0)
+ goto fail;
+
+ if (!num_vfs)
+ return count;
+
+ /*
+ * Special case: set the same num_vfs again.
+ */
+ if (test_bit(0, pdev->sriov->vf_bitmap))
+ return count;
+
+ /*
+ * Default to enabling VF0, we need to read some config registers that are the
+ * same across all associated VFs. See pci_read_vf_config_common().
+ */
+ ret = pci_iov_add_virtfn(pdev, 0);
+ if (ret < 0) {
+ int rc;
+
+ rc = sriov_numvfs_common(pdev, 0);
+ if (rc < 0)
+ pci_warn(pdev, "Roll back to SR-IOV disabled state failed, %d\n", rc);
+
+ goto fail;
+ }
+
+ return count;
+
+fail:
+ device_lock(&pdev->dev);
+ if (pdev->no_vf_scan)
+ pdev->no_vf_scan = 0;
+ device_unlock(&pdev->dev);
+
+ return ret;
+}
+
+static ssize_t sriov_numvfs_no_scan_show(struct device *dev,
+ struct device_attribute *attr,
+ char *buf)
+{
+ return sriov_numvfs_show(dev, attr, buf);
+}
+
static ssize_t sriov_offset_show(struct device *dev,
struct device_attribute *attr,
char *buf)
@@ -544,6 +606,7 @@ static ssize_t sriov_drivers_autoprobe_store(struct device *dev,

static DEVICE_ATTR_RO(sriov_totalvfs);
static DEVICE_ATTR_RW(sriov_numvfs);
+static DEVICE_ATTR_RW(sriov_numvfs_no_scan);
static DEVICE_ATTR_RO(sriov_offset);
static DEVICE_ATTR_RO(sriov_stride);
static DEVICE_ATTR_RO(sriov_vf_device);
@@ -552,6 +615,7 @@ static DEVICE_ATTR_RW(sriov_drivers_autoprobe);
static struct attribute *sriov_pf_dev_attrs[] = {
&dev_attr_sriov_totalvfs.attr,
&dev_attr_sriov_numvfs.attr,
+ &dev_attr_sriov_numvfs_no_scan.attr,
&dev_attr_sriov_offset.attr,
&dev_attr_sriov_stride.attr,
&dev_attr_sriov_vf_device.attr,
--
2.23.0