[PATCH i-g-t 1/6] lib/igt_sriov_device: add helper for opening SR-IOV device sysfs

Marcin Bernatowicz marcin.bernatowicz at linux.intel.com
Wed Oct 9 11:30:13 UTC 2024


Helper allows to open sysfs directory corresponding to SR-IOV device.

Signed-off-by: Marcin Bernatowicz <marcin.bernatowicz at linux.intel.com>
Cc: Adam Miszczak <adam.miszczak at linux.intel.com>
Cc: Jakub Kolakowski <jakub1.kolakowski at intel.com>
Cc: Lukasz Laguna <lukasz.laguna at intel.com>
Cc: Michał Wajdeczko <michal.wajdeczko at intel.com>
Cc: Michał Winiarski <michal.winiarski at intel.com>
Cc: Narasimha C V <narasimha.c.v at intel.com>
Cc: Piotr Piórkowski <piotr.piorkowski at intel.com>
Cc: Satyanarayana K V P <satyanarayana.k.v.p at intel.com>
Cc: Tomasz Lis <tomasz.lis at intel.com>
---
 lib/igt_sriov_device.c | 32 ++++++++++++++++++++++++++++++++
 lib/igt_sriov_device.h |  1 +
 2 files changed, 33 insertions(+)

diff --git a/lib/igt_sriov_device.c b/lib/igt_sriov_device.c
index 7c3814bf2..d20c74823 100644
--- a/lib/igt_sriov_device.c
+++ b/lib/igt_sriov_device.c
@@ -5,6 +5,7 @@
 
 #include <dirent.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <pciaccess.h>
 
@@ -381,3 +382,34 @@ void igt_sriov_unbind_vf_drm_driver(int pf, unsigned int vf_num)
 {
 	igt_assert(__igt_sriov_bind_vf_drm_driver(pf, vf_num, false));
 }
+
+/**
+ * igt_sriov_device_sysfs_open:
+ * @pf: PF device file descriptor
+ * @vf_num: VF number (1-based to identify single VF) or 0 for PF
+ *
+ * Open the sysfs directory corresponding to SR-IOV device.
+ *
+ * Returns:
+ * The SR-IOV device sysfs directory fd, -1 on failure.
+ */
+int igt_sriov_device_sysfs_open(int pf, unsigned int vf_num)
+{
+	char path[PATH_MAX];
+	int sysfs, fd;
+
+	sysfs = igt_sysfs_open(pf);
+	if (sysfs < 0)
+		return -1;
+
+	if (!vf_num)
+		snprintf(path, sizeof(path), "device");
+	else
+		/* vf_num is 1-based, but virtfn is 0-based */
+		snprintf(path, sizeof(path), "device/virtfn%u", vf_num - 1);
+
+	fd = openat(sysfs, path, O_DIRECTORY | O_RDONLY);
+	close(sysfs);
+
+	return fd;
+}
diff --git a/lib/igt_sriov_device.h b/lib/igt_sriov_device.h
index 20ffc515f..dc95a4c78 100644
--- a/lib/igt_sriov_device.h
+++ b/lib/igt_sriov_device.h
@@ -30,6 +30,7 @@ int igt_sriov_open_vf_drm_device(int pf, unsigned int vf_num);
 bool igt_sriov_is_vf_drm_driver_probed(int pf, unsigned int vf_num);
 void igt_sriov_bind_vf_drm_driver(int pf, unsigned int vf_num);
 void igt_sriov_unbind_vf_drm_driver(int pf, unsigned int vf_num);
+int igt_sriov_device_sysfs_open(int pf, unsigned int vf_num);
 
 /**
  * for_each_sriov_vf - Helper for running code on each VF
-- 
2.31.1



More information about the igt-dev mailing list