[PATCH v3 i-g-t 1/7] lib/igt_sriov_device: Add helper to get VF PCI slot address

Marcin Bernatowicz
2026-02-04T16:32:35Z

Add igt_sriov_get_vf_pci_slot_alloc() to resolve the PCI slot address
of a VF by following the device/virtfnX symlink in the PF sysfs
directory, returning a newly allocated string or NULL on failure.

Signed-off-by: Marcin Bernatowicz <marcin.bernatowicz@linux.intel.com>
Reviewed-by: Lukasz Laguna <lukasz.laguna@intel.com>
Cc: Adam Miszczak <adam.miszczak@linux.intel.com>
Cc: Jakub Kolakowski <jakub1.kolakowski@intel.com>
Cc: Kamil Konieczny <kamil.konieczny@linux.intel.com>
Cc: Lukasz Laguna <lukasz.laguna@intel.com>
---
lib/igt_sriov_device.c | 25 +++++++++++++++++++++++++
lib/igt_sriov_device.h | 1 +
2 files changed, 26 insertions(+)

diff --git a/lib/igt_sriov_device.c b/lib/igt_sriov_device.c
index 788ffbfa1..1f4c3ac04 100644
--- a/lib/igt_sriov_device.c
+++ b/lib/igt_sriov_device.c
@@ -354,6 +354,31 @@ static char *__igt_sriov_get_vf_pci_slot_alloc(int pf_sysfs, unsigned int vf_num
return pci_slot_addr ? strdup(pci_slot_addr) : NULL;
}

+/**
+ * igt_sriov_get_vf_pci_slot_alloc:
+ * @pf: PF device file descriptor
+ * @vf_num: VF number (1-based); 0 means PF
+ *
+ * Resolves the symlink under the PF sysfs directory (device/virtfnX) to obtain
+ * the PCI slot address of the VF (or PF if @vf_num is 0).
+ *
+ * Returns: newly allocated PCI slot address string (e.g. "0000:3b:00.4"),
+ * or NULL on failure. Caller must free().
+ */
+char *igt_sriov_get_vf_pci_slot_alloc(int pf, unsigned int vf_num)
+{
+ char *pci_slot;
+ int sysfs;
+
+ sysfs = igt_sysfs_open(pf);
+ igt_assert_fd(sysfs);
+
+ pci_slot = __igt_sriov_get_vf_pci_slot_alloc(sysfs, vf_num);
+ close(sysfs);
+
+ return pci_slot;
+}
+
static bool __igt_sriov_bind_vf_drm_driver(int pf, unsigned int vf_num, bool bind)
{
char *pci_slot;
diff --git a/lib/igt_sriov_device.h b/lib/igt_sriov_device.h
index 84d29b1bb..a417b30d8 100644
--- a/lib/igt_sriov_device.h
+++ b/lib/igt_sriov_device.h
@@ -28,6 +28,7 @@ void igt_sriov_enable_driver_autoprobe(int pf);
void igt_sriov_disable_driver_autoprobe(int pf);
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);
+char *igt_sriov_get_vf_pci_slot_alloc(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);
--
2.43.0