[v1,03/12] mldev: support device handling functions

Message ID 20221114120238.2143832-4-jerinj@marvell.com (mailing list archive)
State Changes Requested, archived
Delegated to: Thomas Monjalon
Headers
Series mldev: introduce machine learning device library |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Jerin Jacob Kollanukkaran Nov. 14, 2022, 12:02 p.m. UTC
  From: Srikanth Yalavarthi <syalavarthi@marvell.com>

Added device handling APIs. These APIs are used to get device
information, configure, start, stop and close ML devices. Added
function prototypes to PMD layer which are used by the ML driver
implementations in the poll mode driver.

Signed-off-by: Srikanth Yalavarthi <syalavarthi@marvell.com>
Signed-off-by: Jerin Jacob <jerinj@marvell.com>
---
 lib/mldev/rte_mldev.c      | 175 +++++++++++++++++++++++++++++++++++++
 lib/mldev/rte_mldev_core.h | 107 +++++++++++++++++++++++
 lib/mldev/version.map      |  11 +++
 3 files changed, 293 insertions(+)
  

Patch

diff --git a/lib/mldev/rte_mldev.c b/lib/mldev/rte_mldev.c
index a4e0b5f94f..651f8b2f7c 100644
--- a/lib/mldev/rte_mldev.c
+++ b/lib/mldev/rte_mldev.c
@@ -131,3 +131,178 @@  rte_ml_dev_pmd_release(struct rte_ml_dev *dev)
 
 	return ret;
 }
+
+uint16_t
+rte_ml_dev_count(void)
+{
+	return ml_dev_globals.nb_devs;
+}
+
+int
+rte_ml_dev_is_valid_dev(int16_t dev_id)
+{
+	struct rte_ml_dev *dev = NULL;
+
+	if (dev_id >= ml_dev_globals.max_devs || ml_devices[dev_id].data == NULL)
+		return 0;
+
+	dev = rte_ml_dev_pmd_get_dev(dev_id);
+	if (dev->attached != ML_DEV_ATTACHED)
+		return 0;
+	else
+		return 1;
+}
+
+int
+rte_ml_dev_socket_id(int16_t dev_id)
+{
+	struct rte_ml_dev *dev;
+
+	if (!rte_ml_dev_is_valid_dev(dev_id)) {
+		ML_DEV_LOG(ERR, "Invalid dev_id = %d\n", dev_id);
+		return -EINVAL;
+	}
+
+	dev = rte_ml_dev_pmd_get_dev(dev_id);
+
+	return dev->data->socket_id;
+}
+
+int
+rte_ml_dev_info_get(int16_t dev_id, struct rte_ml_dev_info *dev_info)
+{
+	struct rte_ml_dev *dev;
+
+	if (!rte_ml_dev_is_valid_dev(dev_id)) {
+		ML_DEV_LOG(ERR, "Invalid dev_id = %d\n", dev_id);
+		return -EINVAL;
+	}
+
+	dev = rte_ml_dev_pmd_get_dev(dev_id);
+	if (*dev->dev_ops->dev_info_get == NULL)
+		return -ENOTSUP;
+
+	if (dev_info == NULL) {
+		ML_DEV_LOG(ERR, "Dev %d, dev_info cannot be NULL\n", dev_id);
+		return -EINVAL;
+	}
+	memset(dev_info, 0, sizeof(struct rte_ml_dev_info));
+
+	return (*dev->dev_ops->dev_info_get)(dev, dev_info);
+}
+
+int
+rte_ml_dev_configure(int16_t dev_id, const struct rte_ml_dev_config *config)
+{
+	struct rte_ml_dev_info dev_info;
+	struct rte_ml_dev *dev;
+	int ret;
+
+	if (!rte_ml_dev_is_valid_dev(dev_id)) {
+		ML_DEV_LOG(ERR, "Invalid dev_id = %d\n", dev_id);
+		return -EINVAL;
+	}
+
+	dev = rte_ml_dev_pmd_get_dev(dev_id);
+	if (*dev->dev_ops->dev_configure == NULL)
+		return -ENOTSUP;
+
+	if (dev->data->dev_started) {
+		ML_DEV_LOG(ERR, "Device %d must be stopped to allow configuration", dev_id);
+		return -EBUSY;
+	}
+
+	if (config == NULL) {
+		ML_DEV_LOG(ERR, "Dev %d, config cannot be NULL\n", dev_id);
+		return -EINVAL;
+	}
+
+	ret = rte_ml_dev_info_get(dev_id, &dev_info);
+	if (ret < 0)
+		return ret;
+
+	if (config->nb_queue_pairs > dev_info.max_queue_pairs) {
+		ML_DEV_LOG(ERR, "Device %d num of queues %u > %u\n", dev_id, config->nb_queue_pairs,
+			   dev_info.max_queue_pairs);
+		return -EINVAL;
+	}
+
+	return (*dev->dev_ops->dev_configure)(dev, config);
+}
+
+int
+rte_ml_dev_close(int16_t dev_id)
+{
+	struct rte_ml_dev *dev;
+
+	if (!rte_ml_dev_is_valid_dev(dev_id)) {
+		ML_DEV_LOG(ERR, "Invalid dev_id = %d\n", dev_id);
+		return -EINVAL;
+	}
+
+	dev = rte_ml_dev_pmd_get_dev(dev_id);
+	if (*dev->dev_ops->dev_close == NULL)
+		return -ENOTSUP;
+
+	/* Device must be stopped before it can be closed */
+	if (dev->data->dev_started == 1) {
+		ML_DEV_LOG(ERR, "Device %d must be stopped before closing", dev_id);
+		return -EBUSY;
+	}
+
+	return (*dev->dev_ops->dev_close)(dev);
+}
+
+int
+rte_ml_dev_start(int16_t dev_id)
+{
+	struct rte_ml_dev *dev;
+	int ret;
+
+	if (!rte_ml_dev_is_valid_dev(dev_id)) {
+		ML_DEV_LOG(ERR, "Invalid dev_id = %d\n", dev_id);
+		return -EINVAL;
+	}
+
+	dev = rte_ml_dev_pmd_get_dev(dev_id);
+	if (*dev->dev_ops->dev_start == NULL)
+		return -ENOTSUP;
+
+	if (dev->data->dev_started != 0) {
+		ML_DEV_LOG(ERR, "Device %d is already started", dev_id);
+		return -EBUSY;
+	}
+
+	ret = (*dev->dev_ops->dev_start)(dev);
+	if (ret == 0)
+		dev->data->dev_started = 1;
+
+	return ret;
+}
+
+int
+rte_ml_dev_stop(int16_t dev_id)
+{
+	struct rte_ml_dev *dev;
+	int ret;
+
+	if (!rte_ml_dev_is_valid_dev(dev_id)) {
+		ML_DEV_LOG(ERR, "Invalid dev_id = %d\n", dev_id);
+		return -EINVAL;
+	}
+
+	dev = rte_ml_dev_pmd_get_dev(dev_id);
+	if (*dev->dev_ops->dev_stop == NULL)
+		return -ENOTSUP;
+
+	if (dev->data->dev_started == 0) {
+		ML_DEV_LOG(ERR, "Device %d is not started", dev_id);
+		return -EBUSY;
+	}
+
+	ret = (*dev->dev_ops->dev_stop)(dev);
+	if (ret == 0)
+		dev->data->dev_started = 0;
+
+	return ret;
+}
diff --git a/lib/mldev/rte_mldev_core.h b/lib/mldev/rte_mldev_core.h
index b5cb69c5fb..1405cce7f7 100644
--- a/lib/mldev/rte_mldev_core.h
+++ b/lib/mldev/rte_mldev_core.h
@@ -35,6 +35,110 @@  extern "C" {
 #define ML_DEV_DETACHED (0)
 #define ML_DEV_ATTACHED (1)
 
+struct rte_ml_dev;
+
+/**
+ * Definitions of all functions exported by a driver through the generic structure of type
+ * *ml_dev_ops* supplied in the *rte_ml_dev* structure associated with a device.
+ */
+
+/**
+ * @internal
+ *
+ * Function used to get device information.
+ *
+ * @param dev
+ *	ML device pointer.
+ * @param dev_info
+ *	Pointer to info structure.
+ *
+ * @return
+ *	- 0 on success.
+ *	- < 0, error code on failure.
+ */
+typedef int (*mldev_info_get_t)(struct rte_ml_dev *dev, struct rte_ml_dev_info *dev_info);
+
+/**
+ * @internal
+ *
+ * Function used to configure device.
+ *
+ * @param dev
+ *	ML device pointer.
+ * @param config
+ *	ML device configurations.
+ *
+ * @return
+ *	- 0 on success
+ *	- < 0, error code on failure.
+ */
+typedef int (*mldev_configure_t)(struct rte_ml_dev *dev, const struct rte_ml_dev_config *config);
+
+/**
+ * @internal
+ *
+ * Function used to close a configured device.
+ *
+ * @param dev
+ *	ML device pointer.
+ *
+ * @return
+ *	- 0 on success.
+ *	- -EAGAIN if can't close as device is busy.
+ *	- < 0, error code on failure, other than busy.
+ */
+typedef int (*mldev_close_t)(struct rte_ml_dev *dev);
+
+/**
+ * @internal
+ *
+ * Function used to start a configured device.
+ *
+ * @param dev
+ *	ML device pointer.
+ *
+ * @return
+ *	- 0 on success.
+ *	- < 0, error code on failure.
+ */
+typedef int (*mldev_start_t)(struct rte_ml_dev *dev);
+
+/**
+ * @internal
+ *
+ * Function used to stop a configured device.
+ *
+ * @param dev
+ *	ML device pointer.
+ *
+ * @return
+ *	- 0 on success.
+ *	- < 0, error code on failure.
+ */
+typedef int (*mldev_stop_t)(struct rte_ml_dev *dev);
+
+/**
+ * @internal
+ *
+ * ML device operations function pointer table.
+ */
+struct rte_ml_dev_ops {
+	/** Get device information. */
+	mldev_info_get_t dev_info_get;
+
+	/** Configure device. */
+	mldev_configure_t dev_configure;
+
+	/** Close device. */
+	mldev_close_t dev_close;
+
+	/** Start device. */
+	mldev_start_t dev_start;
+
+	/** Stop device. */
+	mldev_stop_t dev_stop;
+};
+
 /**
  * @internal
  *
@@ -82,6 +186,9 @@  struct rte_ml_dev {
 	/** Pointer to device data. */
 	struct rte_ml_dev_data *data;
 
+	/** Functions exported by PMD. */
+	struct rte_ml_dev_ops *dev_ops;
+
 	/** Backing RTE device. */
 	struct rte_device *device;
 
diff --git a/lib/mldev/version.map b/lib/mldev/version.map
index 82eedfada4..1be508ab5f 100644
--- a/lib/mldev/version.map
+++ b/lib/mldev/version.map
@@ -1,4 +1,15 @@ 
 EXPERIMENTAL {
+	global:
+
+	rte_ml_dev_close;
+	rte_ml_dev_configure;
+	rte_ml_dev_count;
+	rte_ml_dev_info_get;
+	rte_ml_dev_is_valid_dev;
+	rte_ml_dev_socket_id;
+	rte_ml_dev_start;
+	rte_ml_dev_stop;
+
 	local: *;
 };