Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Lukasz Laguna <lukasz.laguna@intel.com>
To: igt-dev@lists.freedesktop.org
Subject: [igt-dev] [PATCH i-g-t v6 1/8] lib/igt_sriov_device: add core SR-IOV helpers
Date: Mon,  4 Dec 2023 18:10:26 +0100	[thread overview]
Message-ID: <20231204171033.2167-2-lukasz.laguna@intel.com> (raw)
In-Reply-To: <20231204171033.2167-1-lukasz.laguna@intel.com>

From: Katarzyna Dec <katarzyna.dec@intel.com>

Create lib for core SR-IOV (Single Root I/O Virtualization) helpers
that allow to manage SR-IOV devices.

v2:
 - change functions description (Michal)
v3:
 - decipher SR-IOV shortcut in commit message (Kamil)
 - add SR-IOV description in header file (Kamil)
 - remove old r-b and add cc (Kamil)
 - return bool result in functions prefixed with "__" (Kamil)
 - add brief description in functions documentation (Michal)
 - add asserts in helpers (Michal)
v4:
 - remove unnecessary include (Kamil)

Cc: Marcin Bernatowicz <marcin.bernatowicz@linux.intel.com>
Cc: Michal Wajdeczko <michal.wajdeczko@intel.com>
Signed-off-by: Katarzyna Dec <katarzyna.dec@intel.com>
Signed-off-by: Lukasz Laguna <lukasz.laguna@intel.com>
Reviewed-by: Kamil Konieczny <kamil.konieczny@linux.intel.com>
---
 lib/igt_sriov_device.c | 213 +++++++++++++++++++++++++++++++++++++++++
 lib/igt_sriov_device.h |  27 ++++++
 lib/meson.build        |   1 +
 3 files changed, 241 insertions(+)
 create mode 100644 lib/igt_sriov_device.c
 create mode 100644 lib/igt_sriov_device.h

diff --git a/lib/igt_sriov_device.c b/lib/igt_sriov_device.c
new file mode 100644
index 000000000..c5c594c2c
--- /dev/null
+++ b/lib/igt_sriov_device.c
@@ -0,0 +1,213 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright(c) 2023 Intel Corporation. All rights reserved.
+ */
+
+#include <errno.h>
+
+#include "igt_core.h"
+#include "igt_sriov_device.h"
+#include "igt_sysfs.h"
+
+/**
+ * igt_sriov_is_pf - Check if device is PF
+ * @device: device file descriptor
+ *
+ * Determine if device is PF by checking existence of sriov_totalvfs file.
+ *
+ * Return:
+ * True if device is PF, false otherwise.
+ */
+bool igt_sriov_is_pf(int device)
+{
+	int sysfs;
+	bool ret;
+
+	sysfs = igt_sysfs_open(device);
+	igt_assert_fd(sysfs);
+
+	ret = igt_sysfs_has_attr(sysfs, "device/sriov_totalvfs");
+	close(sysfs);
+
+	return ret;
+}
+
+static bool __pf_attr_get_u32(int pf, const char *attr, uint32_t *value)
+{
+	int sysfs;
+	bool ret;
+
+	igt_assert(igt_sriov_is_pf(pf));
+
+	sysfs = igt_sysfs_open(pf);
+	igt_assert_fd(sysfs);
+
+	ret = __igt_sysfs_get_u32(sysfs, attr, value);
+	close(sysfs);
+
+	return ret;
+}
+
+static uint32_t pf_attr_get_u32(int pf, const char *attr)
+{
+	uint32_t value;
+
+	igt_assert_f(__pf_attr_get_u32(pf, attr, &value),
+		     "Failed to read %s attribute (%s)\n", attr, strerror(errno));
+
+	return value;
+}
+
+static bool __pf_attr_set_u32(int pf, const char *attr, uint32_t value)
+{
+	int sysfs;
+	bool ret;
+
+	igt_assert(igt_sriov_is_pf(pf));
+
+	sysfs = igt_sysfs_open(pf);
+	igt_assert_fd(sysfs);
+
+	ret = __igt_sysfs_set_u32(sysfs, attr, value);
+	close(sysfs);
+
+	return ret;
+}
+
+static void pf_attr_set_u32(int pf, const char *attr, uint32_t value)
+{
+	igt_assert_f(__pf_attr_set_u32(pf, attr, value),
+		     "Failed to write %u to %s attribute (%s)\n", value, attr, strerror(errno));
+}
+
+/**
+ * igt_sriov_vfs_supported - Check if VFs are supported
+ * @pf: PF device file descriptor
+ *
+ * Determine VFs support by checking if value of sriov_totalvfs attribute
+ * corresponding to @pf device is bigger than 0.
+ *
+ * Return:
+ * True if VFs are supported, false otherwise.
+ */
+bool igt_sriov_vfs_supported(int pf)
+{
+	uint32_t totalvfs;
+
+	if (!__pf_attr_get_u32(pf, "device/sriov_totalvfs", &totalvfs))
+		return false;
+
+	return totalvfs > 0;
+}
+
+/**
+ * igt_sriov_get_totalvfs - Get maximum number of VFs that can be enabled
+ * @pf: PF device file descriptor
+ *
+ * Maximum number of VFs that can be enabled is checked by reading
+ * sriov_totalvfs attribute corresponding to @pf device.
+ *
+ * It asserts on failure.
+ *
+ * Return:
+ * Maximum number of VFs that can be associated with given PF.
+ */
+unsigned int igt_sriov_get_total_vfs(int pf)
+{
+	return pf_attr_get_u32(pf, "device/sriov_totalvfs");
+}
+
+/**
+ * igt_sriov_get_numvfs - Get number of enabled VFs
+ * @pf: PF device file descriptor
+ *
+ * Number of enabled VFs is checked by reading sriov_numvfs attribute
+ * corresponding to @pf device.
+ *
+ * It asserts on failure.
+ *
+ * Return:
+ * Number of VFs enabled by given PF.
+ */
+unsigned int igt_sriov_get_enabled_vfs(int pf)
+{
+	return pf_attr_get_u32(pf, "device/sriov_numvfs");
+}
+
+/**
+ * igt_sriov_enable_vfs - Enable VFs
+ * @pf: PF device file descriptor
+ * @num_vfs: Number of virtual functions to be enabled
+ *
+ * Enable VFs by writing @num_vfs to sriov_numvfs attribute corresponding to
+ * @pf device.
+ * It asserts on failure.
+ */
+void igt_sriov_enable_vfs(int pf, unsigned int num_vfs)
+{
+	igt_assert(num_vfs > 0);
+
+	igt_debug("Enabling %u VFs\n", num_vfs);
+	pf_attr_set_u32(pf, "device/sriov_numvfs", num_vfs);
+}
+
+/**
+ * igt_sriov_disable_vfs - Disable VFs
+ * @pf: PF device file descriptor
+ *
+ * Disable VFs by writing 0 to sriov_numvfs attribute corresponding to @pf
+ * device.
+ * It asserts on failure.
+ */
+void igt_sriov_disable_vfs(int pf)
+{
+	pf_attr_set_u32(pf, "device/sriov_numvfs", 0);
+}
+
+/**
+ * igt_sriov_is_driver_autoprobe_enabled - Get VF driver autoprobe setting
+ * @pf: PF device file descriptor
+ *
+ * Get current VF driver autoprobe setting by reading sriov_drivers_autoprobe
+ * attribute corresponding to @pf device.
+ *
+ * It asserts on failure.
+ *
+ * Return:
+ * True if autoprobe is enabled, false otherwise.
+ */
+bool igt_sriov_is_driver_autoprobe_enabled(int pf)
+{
+	return pf_attr_get_u32(pf, "device/sriov_drivers_autoprobe");
+}
+
+/**
+ * igt_sriov_enable_driver_autoprobe - Enable VF driver autoprobe
+ * @pf: PF device file descriptor
+ *
+ * Enable VF driver autoprobe setting by writing 1 to sriov_drivers_autoprobe
+ * attribute corresponding to @pf device.
+ *
+ * If successful, kernel will automatically bind VFs to a compatible driver
+ * immediately after they are enabled.
+ * It asserts on failure.
+ */
+void igt_sriov_enable_driver_autoprobe(int pf)
+{
+	pf_attr_set_u32(pf,  "device/sriov_drivers_autoprobe", true);
+}
+
+/**
+ * igt_sriov_disable_driver_autoprobe - Disable VF driver autoprobe
+ * @pf: PF device file descriptor
+ *
+ * Disable VF driver autoprobe setting by writing 0 to sriov_drivers_autoprobe
+ * attribute corresponding to @pf device.
+ *
+ * During VFs enabling driver won't be bound to VFs.
+ * It asserts on failure.
+ */
+void igt_sriov_disable_driver_autoprobe(int pf)
+{
+	pf_attr_set_u32(pf,  "device/sriov_drivers_autoprobe", false);
+}
diff --git a/lib/igt_sriov_device.h b/lib/igt_sriov_device.h
new file mode 100644
index 000000000..63359d57c
--- /dev/null
+++ b/lib/igt_sriov_device.h
@@ -0,0 +1,27 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright(c) 2023 Intel Corporation. All rights reserved.
+ */
+
+#ifndef __IGT_SRIOV_DEVICE_H__
+#define __IGT_SRIOV_DEVICE_H__
+
+/* Library for managing SR-IOV (Single Root I/O Virtualization)
+ * devices.
+ *
+ * SR-IOV is a specification that allows a single PCIe physical
+ * device to appear as a physical function (PF) and multiple virtual
+ * functions (VFs) to the operating system.
+ */
+
+bool igt_sriov_is_pf(int device);
+bool igt_sriov_vfs_supported(int pf);
+unsigned int igt_sriov_get_total_vfs(int pf);
+unsigned int igt_sriov_get_enabled_vfs(int pf);
+void igt_sriov_enable_vfs(int pf, unsigned int num_vfs);
+void igt_sriov_disable_vfs(int pf);
+bool igt_sriov_is_driver_autoprobe_enabled(int pf);
+void igt_sriov_enable_driver_autoprobe(int pf);
+void igt_sriov_disable_driver_autoprobe(int pf);
+
+#endif /* __IGT_SRIOV_DEVICE_H__ */
diff --git a/lib/meson.build b/lib/meson.build
index 48466a2e9..0fc11b26c 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -38,6 +38,7 @@ lib_sources = [
 	'igt_primes.c',
 	'igt_pci.c',
 	'igt_rand.c',
+	'igt_sriov_device.c',
 	'igt_stats.c',
 	'igt_syncobj.c',
 	'igt_sysfs.c',
-- 
2.40.0

  reply	other threads:[~2023-12-04 17:11 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-04 17:10 [igt-dev] [PATCH i-g-t v6 0/8] Initial SR-IOV validation Lukasz Laguna
2023-12-04 17:10 ` Lukasz Laguna [this message]
2023-12-04 17:10 ` [igt-dev] [PATCH i-g-t v6 2/8] lib/igt_sriov_device: add helper for opening VF device Lukasz Laguna
2023-12-04 17:10 ` [igt-dev] [PATCH i-g-t v6 3/8] lib/igt_sriov_device: add helper for checking if VF DRM driver is probed Lukasz Laguna
2023-12-04 17:10 ` [igt-dev] [PATCH i-g-t v6 4/8] lib/igt_sriov_device: add helpers for operations in different VFs scenarios Lukasz Laguna
2023-12-04 17:10 ` [igt-dev] [PATCH i-g-t v6 5/8] tests/sriov_basic: add basic tests for enabling SR-IOV VFs Lukasz Laguna
2023-12-04 20:50   ` Michal Wajdeczko
2023-12-05  8:04     ` Laguna, Lukasz
2023-12-04 17:10 ` [igt-dev] [PATCH i-g-t v6 6/8] lib/igt_sriov_device: add helpers for VF DRM driver bind and unbind Lukasz Laguna
2023-12-04 17:10 ` [igt-dev] [PATCH i-g-t v6 7/8] tests/sriov_basic: validate driver binding to VFs Lukasz Laguna
2023-12-04 20:47   ` Michal Wajdeczko
2023-12-04 17:10 ` [igt-dev] [PATCH i-g-t v6 8/8] tests/sriov_basic: add more tests for VF driver binding Lukasz Laguna
2023-12-04 18:15 ` [igt-dev] ✗ Fi.CI.BAT: failure for Initial SR-IOV validation (rev7) Patchwork
2023-12-04 19:00 ` [igt-dev] ✓ CI.xeBAT: success " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231204171033.2167-2-lukasz.laguna@intel.com \
    --to=lukasz.laguna@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox