[RFC 4/4] pci/sriov: add sriov_scan_vf_id interface

From: Longpeng(Mike)
Date: Fri Nov 11 2022 - 09:27:51 EST


From: Longpeng <longpeng2@xxxxxxxxxx>

The users can add a specific VF through the sriov_scan_vf_id interface.

For example:
echo 4 > /sys/bus/pci/devices/0000\:65\:00.0/sriov_scan_vf_id
( Add VF4 into the system. )

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

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index cb24d4b60e0d..2b585b3a9ad8 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -553,6 +553,51 @@ static ssize_t sriov_numvfs_no_scan_show(struct device *dev,
return sriov_numvfs_show(dev, attr, buf);
}

+static ssize_t sriov_scan_vf_id_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf, size_t count)
+{
+ struct pci_dev *pdev = to_pci_dev(dev);
+ u16 vf_id;
+ ssize_t ret;
+
+ if (kstrtou16(buf, 0, &vf_id) < 0)
+ return -EINVAL;
+
+ device_lock(&pdev->dev);
+
+ if (!pdev->sriov->num_VFs) {
+ ret = -ENOENT;
+ goto exit;
+ }
+
+ if (vf_id >= pdev->sriov->num_VFs) {
+ ret = -ENODEV;
+ goto exit;
+ }
+
+ if (test_bit(vf_id, pdev->sriov->vf_bitmap)) {
+ ret = -EEXIST;
+ goto exit;
+ }
+
+ device_unlock(&pdev->dev);
+
+ /* Already scanned VF0 when enabling VFs */
+ if (vf_id == 0)
+ return count;
+
+ ret = pci_iov_add_virtfn(pdev, vf_id);
+ if (ret < 0)
+ return ret;
+
+ return count;
+
+exit:
+ device_unlock(&pdev->dev);
+ return ret;
+}
+
static ssize_t sriov_offset_show(struct device *dev,
struct device_attribute *attr,
char *buf)
@@ -607,6 +652,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_WO(sriov_scan_vf_id);
static DEVICE_ATTR_RO(sriov_offset);
static DEVICE_ATTR_RO(sriov_stride);
static DEVICE_ATTR_RO(sriov_vf_device);
@@ -616,6 +662,7 @@ 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_scan_vf_id.attr,
&dev_attr_sriov_offset.attr,
&dev_attr_sriov_stride.attr,
&dev_attr_sriov_vf_device.attr,
--
2.23.0