[RFC,02/10] common/mlx5: add an agnostic OS function to open device context

Message ID 20210914053833.7760-3-talshn@nvidia.com (mailing list archive)
State Superseded, archived
Delegated to: akhil goyal
Headers
Series Support MLX5 crypto driver on Windows |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Tal Shnaiderman Sept. 14, 2021, 5:38 a.m. UTC
  Add a function to open device context from a rte_device.

Function mlx5_os_open_device_context can be used both on
Windows and Linux OS.

Signed-off-by: Tal Shnaiderman <talshn@nvidia.com>
---
 drivers/common/mlx5/linux/mlx5_common_os.c   |  28 +++++++
 drivers/common/mlx5/mlx5_common.h            |   4 +
 drivers/common/mlx5/version.map              |   2 +
 drivers/common/mlx5/windows/mlx5_common_os.c | 118 +++++++++++++++++++++++++++
 drivers/common/mlx5/windows/mlx5_common_os.h |   2 +
 drivers/net/mlx5/windows/mlx5_os.c           |  64 +--------------
 6 files changed, 155 insertions(+), 63 deletions(-)
  

Patch

diff --git a/drivers/common/mlx5/linux/mlx5_common_os.c b/drivers/common/mlx5/linux/mlx5_common_os.c
index 9e0c823c97..3ef507944f 100644
--- a/drivers/common/mlx5/linux/mlx5_common_os.c
+++ b/drivers/common/mlx5/linux/mlx5_common_os.c
@@ -428,3 +428,31 @@  mlx5_os_get_ibv_device(const struct rte_pci_addr *addr)
 	mlx5_glue->free_device_list(ibv_list);
 	return ibv_match;
 }
+
+/**
+ * Open device context from a rte_device.
+ *
+ * @param[in] dev
+ *  Pointer to an rte_device struct.
+ * @return
+ *   Pointer to device context or NULL in case context cannot be found.
+ */
+void *
+mlx5_os_open_device_context(struct rte_device *dev)
+{
+	struct ibv_device *ibv;
+	void *ctx;
+
+	ibv = mlx5_os_get_ibv_dev(dev);
+	if (ibv == NULL) {
+		DRV_LOG(ERR, "Failed getting ibv_dev");
+		return NULL;
+	}
+	ctx = mlx5_glue->dv_open_device(ibv);
+	if (ctx == NULL) {
+		DRV_LOG(ERR, "Failed to open IB device \"%s\".", ibv->name);
+		rte_errno = ENODEV;
+		return NULL;
+	}
+	return ctx;
+}
diff --git a/drivers/common/mlx5/mlx5_common.h b/drivers/common/mlx5/mlx5_common.h
index a772371200..249804b00c 100644
--- a/drivers/common/mlx5/mlx5_common.h
+++ b/drivers/common/mlx5/mlx5_common.h
@@ -419,4 +419,8 @@  __rte_internal
 bool
 mlx5_dev_is_pci(const struct rte_device *dev);
 
+__rte_internal
+void *
+mlx5_os_open_device_context(struct rte_device *dev);
+
 #endif /* RTE_PMD_MLX5_COMMON_H_ */
diff --git a/drivers/common/mlx5/version.map b/drivers/common/mlx5/version.map
index e5cb6b7060..6d4258dd25 100644
--- a/drivers/common/mlx5/version.map
+++ b/drivers/common/mlx5/version.map
@@ -141,6 +141,8 @@  INTERNAL {
 	mlx5_os_alloc_pd;
 	mlx5_os_dealloc_pd;
 	mlx5_os_dereg_mr;
+	mlx5_os_match_devx_devices_to_addr;
+	mlx5_os_open_device_context;
 	mlx5_os_get_ibv_dev; # WINDOWS_NO_EXPORT
 	mlx5_os_reg_mr;
 	mlx5_os_umem_dereg;
diff --git a/drivers/common/mlx5/windows/mlx5_common_os.c b/drivers/common/mlx5/windows/mlx5_common_os.c
index 5031bdca26..3b59e57e57 100644
--- a/drivers/common/mlx5/windows/mlx5_common_os.c
+++ b/drivers/common/mlx5/windows/mlx5_common_os.c
@@ -6,6 +6,7 @@ 
 #include <string.h>
 #include <stdio.h>
 
+#include <rte_bus_pci.h>
 #include <rte_mempool.h>
 #include <rte_malloc.h>
 #include <rte_errno.h>
@@ -205,3 +206,120 @@  mlx5_os_dereg_mr(struct mlx5_pmd_mr *pmd_mr)
 		claim_zero(mlx5_os_umem_dereg(pmd_mr->obj));
 	memset(pmd_mr, 0, sizeof(*pmd_mr));
 }
+
+/**
+ * Detect if a devx_device_bdf object has identical DBDF values to the
+ * rte_pci_addr found in bus/pci probing
+ *
+ * @param[in] devx_bdf
+ *   Pointer to the devx_device_bdf structure.
+ * @param[in] addr
+ *   Pointer to the rte_pci_addr structure.
+ *
+ * @return
+ *   1 on Device match, 0 on mismatch.
+ */
+static int
+mlx5_os_match_devx_bdf_to_addr(struct devx_device_bdf *devx_bdf,
+			    struct rte_pci_addr *addr)
+{
+	if (addr->domain != (devx_bdf->bus_id >> 8) ||
+	    addr->bus != (devx_bdf->bus_id & 0xff) ||
+	    addr->devid != devx_bdf->dev_id ||
+	    addr->function != devx_bdf->fnc_id) {
+		return 0;
+	}
+	return 1;
+}
+
+/**
+ * Detect if a devx_device_bdf object matches the rte_pci_addr
+ * found in bus/pci probing
+ * Compare both the Native/PF BDF and the raw_bdf representing a VF BDF.
+ *
+ * @param[in] devx_bdf
+ *   Pointer to the devx_device_bdf structure.
+ * @param[in] addr
+ *   Pointer to the rte_pci_addr structure.
+ *
+ * @return
+ *   1 on Device match, 0 on mismatch, rte_errno code on failure.
+ */
+int
+mlx5_os_match_devx_devices_to_addr(struct devx_device_bdf *devx_bdf,
+				struct rte_pci_addr *addr)
+{
+	int err;
+	struct devx_device mlx5_dev;
+
+	if (mlx5_os_match_devx_bdf_to_addr(devx_bdf, addr))
+		return 1;
+	/**
+	 * Didn't match on Native/PF BDF, could still
+	 * Match a VF BDF, check it next
+	 */
+	err = mlx5_glue->query_device(devx_bdf, &mlx5_dev);
+	if (err) {
+		DRV_LOG(ERR, "query_device failed");
+		rte_errno = err;
+		return rte_errno;
+	}
+	if (mlx5_os_match_devx_bdf_to_addr(&mlx5_dev.raw_bdf, addr))
+		return 1;
+	return 0;
+}
+
+/**
+ * Open device context from a rte_device.
+ *
+ * @param[in] dev
+ *  Pointer to an rte_device struct.
+ * @return
+ *   Pointer to device context or NULL in case context cannot be found.
+ */
+void *
+mlx5_os_open_device_context(struct rte_device *dev)
+{
+	struct devx_device_bdf *devx_bdf_devs, *orig_devx_bdf_devs;
+	struct mlx5_context *devx_ctx_match = NULL;
+	int ret, err;
+	struct rte_pci_device *pci_dev = RTE_DEV_TO_PCI(dev);
+
+	devx_bdf_devs = mlx5_glue->get_device_list(&ret);
+	orig_devx_bdf_devs = devx_bdf_devs;
+	if (!devx_bdf_devs) {
+		rte_errno = errno ? errno : ENOSYS;
+		DRV_LOG(ERR, "cannot list devices, is DevX configured?");
+		return NULL;
+	}
+	while (ret-- > 0) {
+		err = mlx5_os_match_devx_devices_to_addr(devx_bdf_devs,
+		    &pci_dev->addr);
+		if (err == 1) {
+			devx_ctx_match = mlx5_glue->open_device(devx_bdf_devs);
+			if (!devx_ctx_match) {
+				DRV_LOG(ERR, "open_device failed");
+				err = errno;
+				goto exit;
+			}
+			err = mlx5_glue->query_device(devx_bdf_devs,
+				&devx_ctx_match->mlx5_dev);
+			if (err) {
+				DRV_LOG(ERR, "query device context failed.");
+				rte_errno = errno ? errno : ENOSYS;
+				claim_zero(mlx5_glue->close_device(
+					devx_ctx_match));
+				goto exit;
+			}
+		}
+		if (err != 0) {
+			DRV_LOG(ERR, "failed to match addr to rte_device");
+			ret = -err;
+			goto exit;
+		}
+		devx_bdf_devs++;
+	}
+exit:
+	mlx5_glue->free_device_list(orig_devx_bdf_devs);
+	return devx_ctx_match;
+}
diff --git a/drivers/common/mlx5/windows/mlx5_common_os.h b/drivers/common/mlx5/windows/mlx5_common_os.h
index 3756e1959b..c3d74d3b67 100644
--- a/drivers/common/mlx5/windows/mlx5_common_os.h
+++ b/drivers/common/mlx5/windows/mlx5_common_os.h
@@ -261,4 +261,6 @@  int mlx5_os_reg_mr(void *pd,
 		   void *addr, size_t length, struct mlx5_pmd_mr *pmd_mr);
 __rte_internal
 void mlx5_os_dereg_mr(struct mlx5_pmd_mr *pmd_mr);
+int mlx5_os_match_devx_devices_to_addr(struct devx_device_bdf *devx_bdf,
+				struct rte_pci_addr *addr);
 #endif /* RTE_PMD_MLX5_COMMON_OS_H_ */
diff --git a/drivers/net/mlx5/windows/mlx5_os.c b/drivers/net/mlx5/windows/mlx5_os.c
index 26fa927039..9dea6d639e 100644
--- a/drivers/net/mlx5/windows/mlx5_os.c
+++ b/drivers/net/mlx5/windows/mlx5_os.c
@@ -901,68 +901,6 @@  mlx5_os_set_allmulti(struct rte_eth_dev *dev, int enable)
 	return -ENOTSUP;
 }
 
-/**
- * Detect if a devx_device_bdf object has identical DBDF values to the
- * rte_pci_addr found in bus/pci probing
- *
- * @param[in] devx_bdf
- *   Pointer to the devx_device_bdf structure.
- * @param[in] addr
- *   Pointer to the rte_pci_addr structure.
- *
- * @return
- *   1 on Device match, 0 on mismatch.
- */
-static int
-mlx5_match_devx_bdf_to_addr(struct devx_device_bdf *devx_bdf,
-			    struct rte_pci_addr *addr)
-{
-	if (addr->domain != (devx_bdf->bus_id >> 8) ||
-	    addr->bus != (devx_bdf->bus_id & 0xff) ||
-	    addr->devid != devx_bdf->dev_id ||
-	    addr->function != devx_bdf->fnc_id) {
-		return 0;
-	}
-	return 1;
-}
-
-/**
- * Detect if a devx_device_bdf object matches the rte_pci_addr
- * found in bus/pci probing
- * Compare both the Native/PF BDF and the raw_bdf representing a VF BDF.
- *
- * @param[in] devx_bdf
- *   Pointer to the devx_device_bdf structure.
- * @param[in] addr
- *   Pointer to the rte_pci_addr structure.
- *
- * @return
- *   1 on Device match, 0 on mismatch, rte_errno code on failure.
- */
-static int
-mlx5_match_devx_devices_to_addr(struct devx_device_bdf *devx_bdf,
-				struct rte_pci_addr *addr)
-{
-	int err;
-	struct devx_device mlx5_dev;
-
-	if (mlx5_match_devx_bdf_to_addr(devx_bdf, addr))
-		return 1;
-	/**
-	 * Didn't match on Native/PF BDF, could still
-	 * Match a VF BDF, check it next
-	 */
-	err = mlx5_glue->query_device(devx_bdf, &mlx5_dev);
-	if (err) {
-		DRV_LOG(ERR, "query_device failed");
-		rte_errno = err;
-		return rte_errno;
-	}
-	if (mlx5_match_devx_bdf_to_addr(&mlx5_dev.raw_bdf, addr))
-		return 1;
-	return 0;
-}
-
 /**
  * DPDK callback to register a PCI device.
  *
@@ -1036,7 +974,7 @@  mlx5_os_net_probe(struct rte_device *dev)
 	struct devx_device_bdf *devx_bdf_match[ret + 1];
 
 	while (ret-- > 0) {
-		err = mlx5_match_devx_devices_to_addr(devx_bdf_devs,
+		err = mlx5_os_match_devx_devices_to_addr(devx_bdf_devs,
 		    &pci_dev->addr);
 		if (!err) {
 			devx_bdf_devs++;