diff mbox series

[05/14] vhost: replace device ID in vDPA ops

Message ID 20200611213748.1967029-6-maxime.coquelin@redhat.com (mailing list archive)
State Superseded, archived
Delegated to: Maxime Coquelin
Headers show
Series vDPA API and framework rework | expand

Checks

Context Check Description
ci/Intel-compilation success Compilation OK
ci/checkpatch success coding style OK

Commit Message

Maxime Coquelin June 11, 2020, 9:37 p.m. UTC
This patch is a preliminary step to get rid of the
vDPA device ID. It makes vDPA callbacks to use the
vDPA device struct as a reference instead of the ID.

Signed-off-by: Maxime Coquelin <maxime.coquelin@redhat.com>
---
 drivers/vdpa/ifc/ifcvf_vdpa.c | 79 +++++++++++++++--------------
 drivers/vdpa/mlx5/mlx5_vdpa.c | 85 ++++++++++++++++++-------------
 drivers/vdpa/mlx5/mlx5_vdpa.h |  2 +-
 examples/vdpa/main.c          |  4 +-
 lib/librte_vhost/rte_vdpa.h   | 17 ++++---
 lib/librte_vhost/socket.c     |  6 +--
 lib/librte_vhost/vdpa.c       | 95 ++++++++++++++++++-----------------
 7 files changed, 157 insertions(+), 131 deletions(-)
diff mbox series

Patch

diff --git a/drivers/vdpa/ifc/ifcvf_vdpa.c b/drivers/vdpa/ifc/ifcvf_vdpa.c
index 1fec1f1baf..a04da16bbe 100644
--- a/drivers/vdpa/ifc/ifcvf_vdpa.c
+++ b/drivers/vdpa/ifc/ifcvf_vdpa.c
@@ -55,7 +55,7 @@  struct ifcvf_internal {
 	pthread_t tid;	/* thread for notify relay */
 	int epfd;
 	int vid;
-	int did;
+	struct rte_vdpa_device *vdev;
 	uint16_t max_queues;
 	uint64_t features;
 	rte_atomic32_t started;
@@ -84,7 +84,7 @@  static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
 static void update_used_ring(struct ifcvf_internal *internal, uint16_t qid);
 
 static struct internal_list *
-find_internal_resource_by_did(int did)
+find_internal_resource_by_vdev(struct rte_vdpa_device *vdev)
 {
 	int found = 0;
 	struct internal_list *list;
@@ -92,7 +92,7 @@  find_internal_resource_by_did(int did)
 	pthread_mutex_lock(&internal_list_lock);
 
 	TAILQ_FOREACH(list, &internal_list, next) {
-		if (did == list->internal->did) {
+		if (vdev == list->internal->vdev) {
 			found = 1;
 			break;
 		}
@@ -876,14 +876,14 @@  ifcvf_sw_fallback_switchover(struct ifcvf_internal *internal)
 static int
 ifcvf_dev_config(int vid)
 {
-	int did;
+	struct rte_vdpa_device *vdev;
 	struct internal_list *list;
 	struct ifcvf_internal *internal;
 
-	did = rte_vhost_get_vdpa_device_id(vid);
-	list = find_internal_resource_by_did(did);
+	vdev = rte_vdpa_get_device(rte_vhost_get_vdpa_device_id(vid));
+	list = find_internal_resource_by_vdev(vdev);
 	if (list == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
 		return -1;
 	}
 
@@ -893,7 +893,8 @@  ifcvf_dev_config(int vid)
 	update_datapath(internal);
 
 	if (rte_vhost_host_notifier_ctrl(vid, true) != 0)
-		DRV_LOG(NOTICE, "vDPA (%d): software relay is used.", did);
+		DRV_LOG(NOTICE, "vDPA (%s): software relay is used.",
+				vdev->device->name);
 
 	return 0;
 }
@@ -901,14 +902,14 @@  ifcvf_dev_config(int vid)
 static int
 ifcvf_dev_close(int vid)
 {
-	int did;
+	struct rte_vdpa_device *vdev;
 	struct internal_list *list;
 	struct ifcvf_internal *internal;
 
-	did = rte_vhost_get_vdpa_device_id(vid);
-	list = find_internal_resource_by_did(did);
+	vdev = rte_vdpa_get_device(rte_vhost_get_vdpa_device_id(vid));
+	list = find_internal_resource_by_vdev(vdev);
 	if (list == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
 		return -1;
 	}
 
@@ -940,15 +941,15 @@  static int
 ifcvf_set_features(int vid)
 {
 	uint64_t features = 0;
-	int did;
+	struct rte_vdpa_device *vdev;
 	struct internal_list *list;
 	struct ifcvf_internal *internal;
 	uint64_t log_base = 0, log_size = 0;
 
-	did = rte_vhost_get_vdpa_device_id(vid);
-	list = find_internal_resource_by_did(did);
+	vdev = rte_vdpa_get_device(rte_vhost_get_vdpa_device_id(vid));
+	list = find_internal_resource_by_vdev(vdev);
 	if (list == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
 		return -1;
 	}
 
@@ -973,13 +974,13 @@  ifcvf_set_features(int vid)
 static int
 ifcvf_get_vfio_group_fd(int vid)
 {
-	int did;
+	struct rte_vdpa_device *vdev;
 	struct internal_list *list;
 
-	did = rte_vhost_get_vdpa_device_id(vid);
-	list = find_internal_resource_by_did(did);
+	vdev = rte_vdpa_get_device(rte_vhost_get_vdpa_device_id(vid));
+	list = find_internal_resource_by_vdev(vdev);
 	if (list == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
 		return -1;
 	}
 
@@ -989,13 +990,13 @@  ifcvf_get_vfio_group_fd(int vid)
 static int
 ifcvf_get_vfio_device_fd(int vid)
 {
-	int did;
+	struct rte_vdpa_device *vdev;
 	struct internal_list *list;
 
-	did = rte_vhost_get_vdpa_device_id(vid);
-	list = find_internal_resource_by_did(did);
+	vdev = rte_vdpa_get_device(rte_vhost_get_vdpa_device_id(vid));
+	list = find_internal_resource_by_vdev(vdev);
 	if (list == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
 		return -1;
 	}
 
@@ -1005,16 +1006,16 @@  ifcvf_get_vfio_device_fd(int vid)
 static int
 ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
 {
-	int did;
+	struct rte_vdpa_device *vdev;
 	struct internal_list *list;
 	struct ifcvf_internal *internal;
 	struct vfio_region_info reg = { .argsz = sizeof(reg) };
 	int ret;
 
-	did = rte_vhost_get_vdpa_device_id(vid);
-	list = find_internal_resource_by_did(did);
+	vdev = rte_vdpa_get_device(rte_vhost_get_vdpa_device_id(vid));
+	list = find_internal_resource_by_vdev(vdev);
 	if (list == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
 		return -1;
 	}
 
@@ -1035,13 +1036,13 @@  ifcvf_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
 }
 
 static int
-ifcvf_get_queue_num(int did, uint32_t *queue_num)
+ifcvf_get_queue_num(struct rte_vdpa_device *vdev, uint32_t *queue_num)
 {
 	struct internal_list *list;
 
-	list = find_internal_resource_by_did(did);
+	list = find_internal_resource_by_vdev(vdev);
 	if (list == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
 		return -1;
 	}
 
@@ -1051,13 +1052,13 @@  ifcvf_get_queue_num(int did, uint32_t *queue_num)
 }
 
 static int
-ifcvf_get_vdpa_features(int did, uint64_t *features)
+ifcvf_get_vdpa_features(struct rte_vdpa_device *vdev, uint64_t *features)
 {
 	struct internal_list *list;
 
-	list = find_internal_resource_by_did(did);
+	list = find_internal_resource_by_vdev(vdev);
 	if (list == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p", vdev);
 		return -1;
 	}
 
@@ -1073,8 +1074,10 @@  ifcvf_get_vdpa_features(int did, uint64_t *features)
 		 1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER | \
 		 1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD)
 static int
-ifcvf_get_protocol_features(int did __rte_unused, uint64_t *features)
+ifcvf_get_protocol_features(struct rte_vdpa_device *vdev, uint64_t *features)
 {
+	RTE_SET_USED(vdev);
+
 	*features = VDPA_SUPPORTED_PROTOCOL_FEATURES;
 	return 0;
 }
@@ -1185,8 +1188,8 @@  ifcvf_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
 	}
 	internal->sw_lm = sw_fallback_lm;
 
-	internal->did = rte_vdpa_register_device(&pci_dev->device, &ifcvf_ops);
-	if (internal->did < 0) {
+	internal->vdev = rte_vdpa_register_device(&pci_dev->device, &ifcvf_ops);
+	if (internal->vdev == NULL) {
 		DRV_LOG(ERR, "failed to register device %s", pci_dev->name);
 		goto error;
 	}
@@ -1229,7 +1232,7 @@  ifcvf_pci_remove(struct rte_pci_device *pci_dev)
 
 	rte_pci_unmap_device(internal->pdev);
 	rte_vfio_container_destroy(internal->vfio_container_fd);
-	rte_vdpa_unregister_device(internal->did);
+	rte_vdpa_unregister_device(internal->vdev);
 
 	pthread_mutex_lock(&internal_list_lock);
 	TAILQ_REMOVE(&internal_list, list, next);
diff --git a/drivers/vdpa/mlx5/mlx5_vdpa.c b/drivers/vdpa/mlx5/mlx5_vdpa.c
index e8255c7d7e..cbf5c94c78 100644
--- a/drivers/vdpa/mlx5/mlx5_vdpa.c
+++ b/drivers/vdpa/mlx5/mlx5_vdpa.c
@@ -39,21 +39,21 @@  static pthread_mutex_t priv_list_lock = PTHREAD_MUTEX_INITIALIZER;
 int mlx5_vdpa_logtype;
 
 static struct mlx5_vdpa_priv *
-mlx5_vdpa_find_priv_resource_by_did(int did)
+mlx5_vdpa_find_priv_resource_by_vdev(struct rte_vdpa_device *vdev)
 {
 	struct mlx5_vdpa_priv *priv;
 	int found = 0;
 
 	pthread_mutex_lock(&priv_list_lock);
 	TAILQ_FOREACH(priv, &priv_list, next) {
-		if (did == priv->id) {
+		if (vdev == priv->vdev) {
 			found = 1;
 			break;
 		}
 	}
 	pthread_mutex_unlock(&priv_list_lock);
 	if (!found) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		rte_errno = EINVAL;
 		return NULL;
 	}
@@ -61,12 +61,13 @@  mlx5_vdpa_find_priv_resource_by_did(int did)
 }
 
 static int
-mlx5_vdpa_get_queue_num(int did, uint32_t *queue_num)
+mlx5_vdpa_get_queue_num(struct rte_vdpa_device *vdev, uint32_t *queue_num)
 {
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -1;
 	}
 	*queue_num = priv->caps.max_num_virtio_queues;
@@ -74,12 +75,13 @@  mlx5_vdpa_get_queue_num(int did, uint32_t *queue_num)
 }
 
 static int
-mlx5_vdpa_get_vdpa_features(int did, uint64_t *features)
+mlx5_vdpa_get_vdpa_features(struct rte_vdpa_device *vdev, uint64_t *features)
 {
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -1;
 	}
 	*features = MLX5_VDPA_DEFAULT_FEATURES;
@@ -99,12 +101,14 @@  mlx5_vdpa_get_vdpa_features(int did, uint64_t *features)
 }
 
 static int
-mlx5_vdpa_get_protocol_features(int did, uint64_t *features)
+mlx5_vdpa_get_protocol_features(struct rte_vdpa_device *vdev,
+		uint64_t *features)
 {
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -1;
 	}
 	*features = MLX5_VDPA_PROTOCOL_FEATURES;
@@ -114,11 +118,13 @@  mlx5_vdpa_get_protocol_features(int did, uint64_t *features)
 static int
 mlx5_vdpa_set_vring_state(int vid, int vring, int state)
 {
-	int did = rte_vhost_get_vdpa_device_id(vid);
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct rte_vdpa_device *vdev = rte_vdpa_get_device(
+			rte_vhost_get_vdpa_device_id(vid));
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -EINVAL;
 	}
 	if (vring >= (int)priv->caps.max_num_virtio_queues * 2) {
@@ -154,14 +160,16 @@  mlx5_vdpa_direct_db_prepare(struct mlx5_vdpa_priv *priv)
 static int
 mlx5_vdpa_features_set(int vid)
 {
-	int did = rte_vhost_get_vdpa_device_id(vid);
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct rte_vdpa_device *vdev = rte_vdpa_get_device(
+			rte_vhost_get_vdpa_device_id(vid));
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 	uint64_t log_base, log_size;
 	uint64_t features;
 	int ret;
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -EINVAL;
 	}
 	ret = rte_vhost_get_negotiated_features(vid, &features);
@@ -193,12 +201,14 @@  mlx5_vdpa_features_set(int vid)
 static int
 mlx5_vdpa_dev_close(int vid)
 {
-	int did = rte_vhost_get_vdpa_device_id(vid);
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct rte_vdpa_device *vdev = rte_vdpa_get_device(
+			rte_vhost_get_vdpa_device_id(vid));
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 	int ret = 0;
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -1;
 	}
 	if (priv->configured)
@@ -217,11 +227,13 @@  mlx5_vdpa_dev_close(int vid)
 static int
 mlx5_vdpa_dev_config(int vid)
 {
-	int did = rte_vhost_get_vdpa_device_id(vid);
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct rte_vdpa_device *vdev = rte_vdpa_get_device(
+			rte_vhost_get_vdpa_device_id(vid));
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -EINVAL;
 	}
 	if (priv->configured && mlx5_vdpa_dev_close(vid)) {
@@ -243,11 +255,13 @@  mlx5_vdpa_dev_config(int vid)
 static int
 mlx5_vdpa_get_device_fd(int vid)
 {
-	int did = rte_vhost_get_vdpa_device_id(vid);
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct rte_vdpa_device *vdev = rte_vdpa_get_device(
+			rte_vhost_get_vdpa_device_id(vid));
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -EINVAL;
 	}
 	return priv->ctx->cmd_fd;
@@ -256,17 +270,19 @@  mlx5_vdpa_get_device_fd(int vid)
 static int
 mlx5_vdpa_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
 {
-	int did = rte_vhost_get_vdpa_device_id(vid);
-	struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+	struct rte_vdpa_device *vdev = rte_vdpa_get_device(
+			rte_vhost_get_vdpa_device_id(vid));
+	struct mlx5_vdpa_priv *priv =
+		mlx5_vdpa_find_priv_resource_by_vdev(vdev);
 
 	RTE_SET_USED(qid);
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid vDPA device: %p.", vdev);
 		return -EINVAL;
 	}
 	if (!priv->var) {
-		DRV_LOG(ERR, "VAR was not created for device %d, is the device"
-			" configured?.", did);
+		DRV_LOG(ERR, "VAR was not created for device %p, is the device"
+			" configured?.", vdev);
 		return -EINVAL;
 	}
 	*offset = priv->var->mmap_off;
@@ -507,8 +523,9 @@  mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
 		DRV_LOG(ERR, "Failed to allocate VAR %u.\n", errno);
 		goto error;
 	}
-	priv->id = rte_vdpa_register_device(&pci_dev->device, &mlx5_vdpa_ops);
-	if (priv->id < 0) {
+	priv->vdev = rte_vdpa_register_device(&pci_dev->device,
+			&mlx5_vdpa_ops);
+	if (priv->vdev == NULL) {
 		DRV_LOG(ERR, "Failed to register vDPA device.");
 		rte_errno = rte_errno ? rte_errno : EINVAL;
 		goto error;
diff --git a/drivers/vdpa/mlx5/mlx5_vdpa.h b/drivers/vdpa/mlx5/mlx5_vdpa.h
index 50ee3c5870..ec8503f543 100644
--- a/drivers/vdpa/mlx5/mlx5_vdpa.h
+++ b/drivers/vdpa/mlx5/mlx5_vdpa.h
@@ -101,7 +101,7 @@  struct mlx5_vdpa_priv {
 	TAILQ_ENTRY(mlx5_vdpa_priv) next;
 	uint8_t configured;
 	uint8_t direct_notifier; /* Whether direct notifier is on or off. */
-	int id; /* vDPA device id. */
+	struct rte_vdpa_device *vdev; /* vDPA device. */
 	int vid; /* vhost device id. */
 	struct ibv_context *ctx; /* Device context. */
 	struct rte_pci_device *pci_dev;
diff --git a/examples/vdpa/main.c b/examples/vdpa/main.c
index c12da69574..6b92c712e8 100644
--- a/examples/vdpa/main.c
+++ b/examples/vdpa/main.c
@@ -282,13 +282,13 @@  static void cmd_list_vdpa_devices_parsed(
 		vdev = rte_vdpa_get_device(did);
 		if (!vdev)
 			continue;
-		if (vdev->ops->get_queue_num(did, &queue_num) < 0) {
+		if (vdev->ops->get_queue_num(vdev, &queue_num) < 0) {
 			RTE_LOG(ERR, VDPA,
 				"failed to get vdpa queue number "
 				"for device id %d.\n", did);
 			continue;
 		}
-		if (vdev->ops->get_features(did, &features) < 0) {
+		if (vdev->ops->get_features(vdev, &features) < 0) {
 			RTE_LOG(ERR, VDPA,
 				"failed to get vdpa features "
 				"for device id %d.\n", did);
diff --git a/lib/librte_vhost/rte_vdpa.h b/lib/librte_vhost/rte_vdpa.h
index 33037d39ea..e7c785102d 100644
--- a/lib/librte_vhost/rte_vdpa.h
+++ b/lib/librte_vhost/rte_vdpa.h
@@ -18,18 +18,21 @@ 
 
 #define MAX_VDPA_NAME_LEN 128
 
+struct rte_vdpa_device;
+
 /**
  * vdpa device operations
  */
 struct rte_vdpa_dev_ops {
 	/** Get capabilities of this device */
-	int (*get_queue_num)(int did, uint32_t *queue_num);
+	int (*get_queue_num)(struct rte_vdpa_device *dev, uint32_t *queue_num);
 
 	/** Get supported features of this device */
-	int (*get_features)(int did, uint64_t *features);
+	int (*get_features)(struct rte_vdpa_device *dev, uint64_t *features);
 
 	/** Get supported protocol features of this device */
-	int (*get_protocol_features)(int did, uint64_t *protocol_features);
+	int (*get_protocol_features)(struct rte_vdpa_device *dev,
+			uint64_t *protocol_features);
 
 	/** Driver configure/close the device */
 	int (*dev_conf)(int vid);
@@ -79,10 +82,10 @@  struct rte_vdpa_device {
  * @param ops
  *  the vdpa device operations
  * @return
- *  device id on success, -1 on failure
+ *  vDPA device pointer on success, NULL on failure
  */
 __rte_experimental
-int
+struct rte_vdpa_device *
 rte_vdpa_register_device(struct rte_device *rte_dev,
 		struct rte_vdpa_dev_ops *ops);
 
@@ -93,13 +96,13 @@  rte_vdpa_register_device(struct rte_device *rte_dev,
  * Unregister a vdpa device
  *
  * @param did
- *  vdpa device id
+ *  vDPA device pointer
  * @return
  *  device id on success, -1 on failure
  */
 __rte_experimental
 int
-rte_vdpa_unregister_device(int did);
+rte_vdpa_unregister_device(struct rte_vdpa_device *);
 
 /**
  * @warning
diff --git a/lib/librte_vhost/socket.c b/lib/librte_vhost/socket.c
index 0a66ef9767..da575b608c 100644
--- a/lib/librte_vhost/socket.c
+++ b/lib/librte_vhost/socket.c
@@ -712,7 +712,7 @@  rte_vhost_driver_get_features(const char *path, uint64_t *features)
 		goto unlock_exit;
 	}
 
-	if (vdpa_dev->ops->get_features(did, &vdpa_features) < 0) {
+	if (vdpa_dev->ops->get_features(vdpa_dev, &vdpa_features) < 0) {
 		VHOST_LOG_CONFIG(ERR,
 				"failed to get vdpa features "
 				"for socket file %s.\n", path);
@@ -767,7 +767,7 @@  rte_vhost_driver_get_protocol_features(const char *path,
 		goto unlock_exit;
 	}
 
-	if (vdpa_dev->ops->get_protocol_features(did,
+	if (vdpa_dev->ops->get_protocol_features(vdpa_dev,
 				&vdpa_protocol_features) < 0) {
 		VHOST_LOG_CONFIG(ERR,
 				"failed to get vdpa protocol features "
@@ -809,7 +809,7 @@  rte_vhost_driver_get_queue_num(const char *path, uint32_t *queue_num)
 		goto unlock_exit;
 	}
 
-	if (vdpa_dev->ops->get_queue_num(did, &vdpa_queue_num) < 0) {
+	if (vdpa_dev->ops->get_queue_num(vdpa_dev, &vdpa_queue_num) < 0) {
 		VHOST_LOG_CONFIG(ERR,
 				"failed to get vdpa queue number "
 				"for socket file %s.\n", path);
diff --git a/lib/librte_vhost/vdpa.c b/lib/librte_vhost/vdpa.c
index 5abc5a2a7c..5ad96fe8a6 100644
--- a/lib/librte_vhost/vdpa.c
+++ b/lib/librte_vhost/vdpa.c
@@ -18,52 +18,6 @@ 
 static struct rte_vdpa_device vdpa_devices[MAX_VHOST_DEVICE];
 static uint32_t vdpa_device_num;
 
-int
-rte_vdpa_register_device(struct rte_device *rte_dev,
-		struct rte_vdpa_dev_ops *ops)
-{
-	struct rte_vdpa_device *dev;
-	int i;
-
-	if (vdpa_device_num >= MAX_VHOST_DEVICE || ops == NULL)
-		return -1;
-
-	for (i = 0; i < MAX_VHOST_DEVICE; i++) {
-		dev = &vdpa_devices[i];
-		if (dev->ops == NULL)
-			continue;
-
-		if (dev->device == rte_dev)
-			return -1;
-	}
-
-	for (i = 0; i < MAX_VHOST_DEVICE; i++) {
-		if (vdpa_devices[i].ops == NULL)
-			break;
-	}
-
-	if (i == MAX_VHOST_DEVICE)
-		return -1;
-
-	dev = &vdpa_devices[i];
-	dev->device = rte_dev;
-	dev->ops = ops;
-	vdpa_device_num++;
-
-	return i;
-}
-
-int
-rte_vdpa_unregister_device(int did)
-{
-	if (did < 0 || did >= MAX_VHOST_DEVICE || vdpa_devices[did].ops == NULL)
-		return -1;
-
-	memset(&vdpa_devices[did], 0, sizeof(struct rte_vdpa_device));
-	vdpa_device_num--;
-
-	return did;
-}
 
 int
 rte_vdpa_find_device_id(struct rte_vdpa_device *dev)
@@ -116,6 +70,55 @@  rte_vdpa_get_device(int did)
 	return &vdpa_devices[did];
 }
 
+struct rte_vdpa_device *
+rte_vdpa_register_device(struct rte_device *rte_dev,
+		struct rte_vdpa_dev_ops *ops)
+{
+	struct rte_vdpa_device *dev;
+	int i;
+
+	if (vdpa_device_num >= MAX_VHOST_DEVICE || ops == NULL)
+		return NULL;
+
+	for (i = 0; i < MAX_VHOST_DEVICE; i++) {
+		dev = &vdpa_devices[i];
+		if (dev->ops == NULL)
+			continue;
+
+		if (dev->device == rte_dev)
+			return NULL;
+	}
+
+	for (i = 0; i < MAX_VHOST_DEVICE; i++) {
+		if (vdpa_devices[i].ops == NULL)
+			break;
+	}
+
+	if (i == MAX_VHOST_DEVICE)
+		return NULL;
+
+	dev = &vdpa_devices[i];
+	dev->device = rte_dev;
+	dev->ops = ops;
+	vdpa_device_num++;
+
+	return dev;
+}
+
+int
+rte_vdpa_unregister_device(struct rte_vdpa_device *vdev)
+{
+	int did = rte_vdpa_find_device_id(vdev);
+
+	if (did < 0 || vdpa_devices[did].ops == NULL)
+		return -1;
+
+	memset(&vdpa_devices[did], 0, sizeof(struct rte_vdpa_device));
+	vdpa_device_num--;
+
+	return 0;
+}
+
 int
 rte_vdpa_get_device_num(void)
 {