[RFC 1/4] pci/sriov: extract sriov_numvfs common helper

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


From: Longpeng <longpeng2@xxxxxxxxxx>

Extract a common helper to set sriov nums.

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

diff --git a/drivers/pci/iov.c b/drivers/pci/iov.c
index 952217572113..3b441cd92960 100644
--- a/drivers/pci/iov.c
+++ b/drivers/pci/iov.c
@@ -406,23 +406,9 @@ static ssize_t sriov_numvfs_show(struct device *dev,
return sysfs_emit(buf, "%u\n", num_vfs);
}

-/*
- * num_vfs > 0; number of VFs to enable
- * num_vfs = 0; disable all VFs
- *
- * Note: SRIOV spec does not allow partial VF
- * disable, so it's all or none.
- */
-static ssize_t sriov_numvfs_store(struct device *dev,
- struct device_attribute *attr,
- const char *buf, size_t count)
+static int sriov_numvfs_common(struct pci_dev *pdev, u16 num_vfs)
{
- struct pci_dev *pdev = to_pci_dev(dev);
int ret = 0;
- u16 num_vfs;
-
- if (kstrtou16(buf, 0, &num_vfs) < 0)
- return -EINVAL;

if (num_vfs > pci_sriov_get_totalvfs(pdev))
return -ERANGE;
@@ -471,6 +457,28 @@ static ssize_t sriov_numvfs_store(struct device *dev,
exit:
device_unlock(&pdev->dev);

+ return ret;
+}
+
+/*
+ * num_vfs > 0; number of VFs to enable
+ * num_vfs = 0; disable all VFs
+ *
+ * Note: SRIOV spec does not allow partial VF
+ * disable, so it's all or none.
+ */
+static ssize_t sriov_numvfs_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;
+
+ ret = sriov_numvfs_common(pdev, num_vfs);
if (ret < 0)
return ret;

--
2.23.0