diff --git a/drivers/net/mlx5/linux/mlx5_flow_os.c b/drivers/net/mlx5/linux/mlx5_flow_os.c
index 893f00b824..a5956c255a 100644
--- a/drivers/net/mlx5/linux/mlx5_flow_os.c
+++ b/drivers/net/mlx5/linux/mlx5_flow_os.c
@@ -14,7 +14,8 @@ mlx5_flow_os_init_workspace_once(void)
 {
 	if (rte_thread_key_create(&key_workspace, flow_release_workspace)) {
 		DRV_LOG(ERR, "Can't create flow workspace data thread key.");
-		return -ENOMEM;
+		rte_errno = ENOMEM;
+		return -rte_errno;
 	}
 	return 0;
 }
diff --git a/drivers/net/mlx5/linux/mlx5_os.c b/drivers/net/mlx5/linux/mlx5_os.c
index 69d3e1e3ad..faab310b11 100644
--- a/drivers/net/mlx5/linux/mlx5_os.c
+++ b/drivers/net/mlx5/linux/mlx5_os.c
@@ -138,7 +138,7 @@ mlx5_os_set_nonblock_channel_fd(int fd)
  *   Pointer to mlx5 device attributes.
  *
  * @return
- *   0 on success, non zero error number otherwise
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
  */
 int
 mlx5_os_get_dev_attr(struct mlx5_common_device *cdev,
@@ -150,8 +150,10 @@ mlx5_os_get_dev_attr(struct mlx5_common_device *cdev,
 
 	memset(device_attr, 0, sizeof(*device_attr));
 	err = mlx5_glue->query_device_ex(ctx, NULL, &attr_ex);
-	if (err)
-		return err;
+	if (err) {
+		rte_errno = errno;
+		return -rte_errno;
+	}
 	device_attr->device_cap_flags_ex = attr_ex.device_cap_flags_ex;
 	device_attr->max_qp_wr = attr_ex.orig_attr.max_qp_wr;
 	device_attr->max_sge = attr_ex.orig_attr.max_sge;
@@ -170,8 +172,10 @@ mlx5_os_get_dev_attr(struct mlx5_common_device *cdev,
 
 	struct mlx5dv_context dv_attr = { .comp_mask = 0 };
 	err = mlx5_glue->dv_query_device(ctx, &dv_attr);
-	if (err)
-		return err;
+	if (err) {
+		rte_errno = errno;
+		return -rte_errno;
+	}
 
 	device_attr->flags = dv_attr.flags;
 	device_attr->comp_mask = dv_attr.comp_mask;
@@ -195,7 +199,7 @@ mlx5_os_get_dev_attr(struct mlx5_common_device *cdev,
 	strlcpy(device_attr->fw_ver, attr_ex.orig_attr.fw_ver,
 		sizeof(device_attr->fw_ver));
 
-	return err;
+	return 0;
 }
 
 /**
diff --git a/drivers/net/mlx5/mlx5.c b/drivers/net/mlx5/mlx5.c
index cde8d022cd..0e154b88ed 100644
--- a/drivers/net/mlx5/mlx5.c
+++ b/drivers/net/mlx5/mlx5.c
@@ -1172,12 +1172,11 @@ mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
 	MLX5_ASSERT(spawn->max_port);
 	sh = mlx5_malloc(MLX5_MEM_ZERO | MLX5_MEM_RTE,
 			 sizeof(struct mlx5_dev_ctx_shared) +
-			 spawn->max_port *
-			 sizeof(struct mlx5_dev_shared_port),
+			 spawn->max_port * sizeof(struct mlx5_dev_shared_port),
 			 RTE_CACHE_LINE_SIZE, SOCKET_ID_ANY);
 	if (!sh) {
-		DRV_LOG(ERR, "shared context allocation failure");
-		rte_errno  = ENOMEM;
+		DRV_LOG(ERR, "Shared context allocation failure.");
+		rte_errno = ENOMEM;
 		goto exit;
 	}
 	pthread_mutex_init(&sh->txpp.mutex, NULL);
@@ -1199,9 +1198,8 @@ mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
 	strncpy(sh->ibdev_path, mlx5_os_get_ctx_device_path(sh->cdev->ctx),
 		sizeof(sh->ibdev_path) - 1);
 	/*
-	 * Setting port_id to max unallowed value means
-	 * there is no interrupt subhandler installed for
-	 * the given port index i.
+	 * Setting port_id to max unallowed value means there is no interrupt
+	 * subhandler installed for the given port index i.
 	 */
 	for (i = 0; i < sh->max_port; i++) {
 		sh->port[i].ih_port_id = RTE_MAX_ETHPORTS;
@@ -1211,12 +1209,12 @@ mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
 		sh->td = mlx5_devx_cmd_create_td(sh->cdev->ctx);
 		if (!sh->td) {
 			DRV_LOG(ERR, "TD allocation failure");
-			err = ENOMEM;
+			rte_errno = ENOMEM;
 			goto error;
 		}
 		if (mlx5_setup_tis(sh)) {
 			DRV_LOG(ERR, "TIS allocation failure");
-			err = ENOMEM;
+			rte_errno = ENOMEM;
 			goto error;
 		}
 		err = mlx5_rxtx_uars_prepare(sh);
@@ -1246,19 +1244,19 @@ mlx5_alloc_shared_dev_ctx(const struct mlx5_dev_spawn_data *spawn,
 	pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
 	return sh;
 error:
+	err = rte_errno;
 	pthread_mutex_destroy(&sh->txpp.mutex);
 	pthread_mutex_unlock(&mlx5_dev_ctx_list_mutex);
 	MLX5_ASSERT(sh);
-	if (sh->td)
-		claim_zero(mlx5_devx_cmd_destroy(sh->td));
+	mlx5_rxtx_uars_release(sh);
 	i = 0;
 	do {
 		if (sh->tis[i])
 			claim_zero(mlx5_devx_cmd_destroy(sh->tis[i]));
 	} while (++i < (uint32_t)sh->bond.n_port);
-	mlx5_rxtx_uars_release(sh);
+	if (sh->td)
+		claim_zero(mlx5_devx_cmd_destroy(sh->td));
 	mlx5_free(sh);
-	MLX5_ASSERT(err > 0);
 	rte_errno = err;
 	return NULL;
 }
diff --git a/drivers/net/mlx5/windows/mlx5_flow_os.c b/drivers/net/mlx5/windows/mlx5_flow_os.c
index 7bb4c4590a..f5e3893ed4 100644
--- a/drivers/net/mlx5/windows/mlx5_flow_os.c
+++ b/drivers/net/mlx5/windows/mlx5_flow_os.c
@@ -372,7 +372,7 @@ mlx5_flow_os_init_workspace_once(void)
 
 	if (err) {
 		DRV_LOG(ERR, "Can't create flow workspace data thread key.");
-		return err;
+		return -rte_errno;
 	}
 	pthread_mutex_init(&lock_thread_list, NULL);
 	return 0;
diff --git a/drivers/net/mlx5/windows/mlx5_os.c b/drivers/net/mlx5/windows/mlx5_os.c
index 3db33cd0cf..a9619e5bda 100644
--- a/drivers/net/mlx5/windows/mlx5_os.c
+++ b/drivers/net/mlx5/windows/mlx5_os.c
@@ -152,7 +152,7 @@ mlx5_init_once(void)
  *   Pointer to mlx5 device attributes.
  *
  * @return
- *   0 on success, non zero error number otherwise.
+ *   0 on success, a negative errno value otherwise and rte_errno is set.
  */
 int
 mlx5_os_get_dev_attr(struct mlx5_common_device *cdev,
@@ -161,10 +161,11 @@ mlx5_os_get_dev_attr(struct mlx5_common_device *cdev,
 	struct mlx5_context *mlx5_ctx;
 	void *pv_iseg = NULL;
 	u32 cb_iseg = 0;
-	int err = 0;
 
-	if (!cdev || !cdev->ctx)
-		return -EINVAL;
+	if (!cdev || !cdev->ctx) {
+		rte_errno = EINVAL;
+		return -rte_errno;
+	}
 	mlx5_ctx = (struct mlx5_context *)cdev->ctx;
 	memset(device_attr, 0, sizeof(*device_attr));
 	device_attr->max_cq = 1 << cdev->config.hca_attr.log_max_cq;
@@ -187,15 +188,14 @@ mlx5_os_get_dev_attr(struct mlx5_common_device *cdev,
 	pv_iseg = mlx5_glue->query_hca_iseg(mlx5_ctx, &cb_iseg);
 	if (pv_iseg == NULL) {
 		DRV_LOG(ERR, "Failed to get device hca_iseg");
-		return errno;
-	}
-	if (!err) {
-		snprintf(device_attr->fw_ver, 64, "%x.%x.%04x",
-			MLX5_GET(initial_seg, pv_iseg, fw_rev_major),
-			MLX5_GET(initial_seg, pv_iseg, fw_rev_minor),
-			MLX5_GET(initial_seg, pv_iseg, fw_rev_subminor));
+		rte_errno = errno;
+		return -rte_errno;
 	}
-	return err;
+	snprintf(device_attr->fw_ver, 64, "%x.%x.%04x",
+		 MLX5_GET(initial_seg, pv_iseg, fw_rev_major),
+		 MLX5_GET(initial_seg, pv_iseg, fw_rev_minor),
+		 MLX5_GET(initial_seg, pv_iseg, fw_rev_subminor));
+	return 0;
 }
 
 /**
