diff mbox series

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

Message ID 20200624122701.1369327-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/checkpatch success coding style OK

Commit Message

Maxime Coquelin June 24, 2020, 12:26 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 | 134 ++++++++++++++++++++--------------
 drivers/vdpa/mlx5/mlx5_vdpa.h |   2 +-
 examples/vdpa/main.c          |   4 +-
 lib/librte_vhost/rte_vdpa.h   |  28 ++++---
 lib/librte_vhost/socket.c     |   6 +-
 lib/librte_vhost/vdpa.c       | 101 ++++++++++++-------------
 7 files changed, 195 insertions(+), 159 deletions(-)

Comments

Adrian Moreno June 26, 2020, 10:34 a.m. UTC | #1
On 6/24/20 2:26 PM, Maxime Coquelin wrote:
> 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 | 134 ++++++++++++++++++++--------------
>  drivers/vdpa/mlx5/mlx5_vdpa.h |   2 +-
>  examples/vdpa/main.c          |   4 +-
>  lib/librte_vhost/rte_vdpa.h   |  28 ++++---
>  lib/librte_vhost/socket.c     |   6 +-
>  lib/librte_vhost/vdpa.c       | 101 ++++++++++++-------------
>  7 files changed, 195 insertions(+), 159 deletions(-)
> 
> diff --git a/drivers/vdpa/ifc/ifcvf_vdpa.c b/drivers/vdpa/ifc/ifcvf_vdpa.c
> index ac9e218c23..0418f9a07f 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;
>  		}
> @@ -877,14 +877,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;
>  	}
>  
> @@ -894,7 +894,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;
>  }
> @@ -902,14 +903,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;
>  	}
>  
> @@ -941,15 +942,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;
>  	}
>  
> @@ -974,13 +975,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;
>  	}
>  
> @@ -990,13 +991,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;
>  	}
>  
> @@ -1006,16 +1007,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;
>  	}
>  
> @@ -1036,13 +1037,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;
>  	}
>  
> @@ -1052,13 +1053,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;
>  	}
>  
> @@ -1074,8 +1075,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;
>  }
> @@ -1186,8 +1189,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;
>  	}
> @@ -1230,7 +1233,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 7b5ae62bdc..9ea032d57b 100644
> --- a/drivers/vdpa/mlx5/mlx5_vdpa.c
> +++ b/drivers/vdpa/mlx5/mlx5_vdpa.c
> @@ -50,21 +50,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: %s.", vdev->device->name);
>  		rte_errno = EINVAL;
>  		return NULL;
>  	}
> @@ -72,12 +72,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: %s.", vdev->device->name);
>  		return -1;
>  	}
>  	*queue_num = priv->caps.max_num_virtio_queues;
> @@ -85,12 +86,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: %s.", vdev->device->name);
>  		return -1;
>  	}
>  	*features = MLX5_VDPA_DEFAULT_FEATURES;
> @@ -110,12 +112,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: %s.", vdev->device->name);
>  		return -1;
>  	}
>  	*features = MLX5_VDPA_PROTOCOL_FEATURES;
> @@ -125,11 +129,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: %s.", vdev->device->name);
>  		return -EINVAL;
>  	}
>  	if (vring >= (int)priv->caps.max_num_virtio_queues * 2) {
> @@ -165,14 +171,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: %s.", vdev->device->name);
>  		return -EINVAL;
>  	}
>  	ret = rte_vhost_get_negotiated_features(vid, &features);
> @@ -284,12 +292,14 @@ mlx5_vdpa_mtu_set(struct mlx5_vdpa_priv *priv)
>  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: %s.", vdev->device->name);
>  		return -1;
>  	}
>  	if (priv->configured)
> @@ -312,11 +322,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: %s.", vdev->device->name);
>  		return -EINVAL;
>  	}
>  	if (priv->configured && mlx5_vdpa_dev_close(vid)) {
> @@ -325,7 +337,8 @@ mlx5_vdpa_dev_config(int vid)
>  	}
>  	priv->vid = vid;
>  	if (mlx5_vdpa_mtu_set(priv))
> -		DRV_LOG(WARNING, "MTU cannot be set on device %d.", did);
> +		DRV_LOG(WARNING, "MTU cannot be set on device %s.",
> +				vdev->device->name);
>  	if (mlx5_vdpa_pd_create(priv) || mlx5_vdpa_mem_register(priv) ||
>  	    mlx5_vdpa_direct_db_prepare(priv) ||
>  	    mlx5_vdpa_virtqs_prepare(priv) || mlx5_vdpa_steer_setup(priv) ||
> @@ -341,11 +354,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: %s.", vdev->device->name);
>  		return -EINVAL;
>  	}
>  	return priv->ctx->cmd_fd;
> @@ -354,17 +369,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: %s.", vdev->device->name);
>  		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 %s, is the device"
> +			" configured?.", vdev->device->name);
>  		return -EINVAL;
>  	}
>  	*offset = priv->var->mmap_off;
> @@ -373,8 +390,9 @@ mlx5_vdpa_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
>  }
>  
>  static int
> -mlx5_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
> -			  unsigned int size)
> +mlx5_vdpa_get_stats_names(struct rte_vdpa_device *vdev,
> +		struct rte_vdpa_stat_name *stats_names,
> +		unsigned int size)
>  {
>  	static const char *mlx5_vdpa_stats_names[MLX5_VDPA_STATS_MAX] = {
>  		"received_descriptors",
> @@ -384,11 +402,12 @@ mlx5_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
>  		"invalid buffer",
>  		"completion errors",
>  	};
> -	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);
>  	unsigned int i;
>  
>  	if (priv == NULL) {
> -		DRV_LOG(ERR, "Invalid device id: %d.", did);
> +		DRV_LOG(ERR, "Invalid device: %s.", vdev->device->name);
>  		return -ENODEV;
>  	}
>  	if (!stats_names)
> @@ -401,51 +420,57 @@ mlx5_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
>  }
>  
>  static int
> -mlx5_vdpa_get_stats(int did, int qid, struct rte_vdpa_stat *stats,
> -		    unsigned int n)
> +mlx5_vdpa_get_stats(struct rte_vdpa_device *vdev, int qid,
> +		struct rte_vdpa_stat *stats, unsigned int n)
>  {
> -	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 device: %s.", vdev->device->name);
>  		return -ENODEV;
>  	}
>  	if (!priv->configured) {
> -		DRV_LOG(ERR, "Device %d was not configured.", did);
> +		DRV_LOG(ERR, "Device %s was not configured.",
> +				vdev->device->name);
>  		return -ENODATA;
>  	}
>  	if (qid >= (int)priv->nr_virtqs) {
> -		DRV_LOG(ERR, "Too big vring id: %d.", qid);
> +		DRV_LOG(ERR, "Too big vring id: %d for device %s.", qid,
> +				vdev->device->name);
>  		return -E2BIG;
>  	}
>  	if (!priv->caps.queue_counters_valid) {
> -		DRV_LOG(ERR, "Virtq statistics is not supported for device %d.",
> -			did);
> +		DRV_LOG(ERR, "Virtq statistics is not supported for device %s.",
> +			vdev->device->name);
>  		return -ENOTSUP;
>  	}
>  	return mlx5_vdpa_virtq_stats_get(priv, qid, stats, n);
>  }
>  
>  static int
> -mlx5_vdpa_reset_stats(int did, int qid)
> +mlx5_vdpa_reset_stats(struct rte_vdpa_device *vdev, int qid)
>  {
> -	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 device: %s.", vdev->device->name);
>  		return -ENODEV;
>  	}
>  	if (!priv->configured) {
> -		DRV_LOG(ERR, "Device %d was not configured.", did);
> +		DRV_LOG(ERR, "Device %s was not configured.",
> +				vdev->device->name);
>  		return -ENODATA;
>  	}
>  	if (qid >= (int)priv->nr_virtqs) {
> -		DRV_LOG(ERR, "Too big vring id: %d.", qid);
> +		DRV_LOG(ERR, "Too big vring id: %d for device %s.", qid,
> +				vdev->device->name);
>  		return -E2BIG;
>  	}
>  	if (!priv->caps.queue_counters_valid) {
> -		DRV_LOG(ERR, "Virtq statistics is not supported for device %d.",
> -			did);
> +		DRV_LOG(ERR, "Virtq statistics is not supported for device %s.",
> +			vdev->device->name);
>  		return -ENOTSUP;
>  	}
>  	return mlx5_vdpa_virtq_stats_reset(priv, qid);
> @@ -687,8 +712,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 5fc801eff3..ea09576c5a 100644
> --- a/drivers/vdpa/mlx5/mlx5_vdpa.h
> +++ b/drivers/vdpa/mlx5/mlx5_vdpa.h
> @@ -103,7 +103,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 e72f6646e7..bdb00603cc 100644
> --- a/examples/vdpa/main.c
> +++ b/examples/vdpa/main.c
> @@ -291,13 +291,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 b752dfeb96..109cf83b8f 100644
> --- a/lib/librte_vhost/rte_vdpa.h
> +++ b/lib/librte_vhost/rte_vdpa.h
> @@ -21,6 +21,8 @@
>  /** Maximum name length for statistics counters */
>  #define RTE_VDPA_STATS_NAME_SIZE 64
>  
> +struct rte_vdpa_device;
> +
>  /**
>   * A vDPA device statistic structure
>   *
> @@ -51,13 +53,14 @@ struct rte_vdpa_stat_name {
>   */
>  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);
> @@ -83,15 +86,16 @@ struct rte_vdpa_dev_ops {
>  			uint64_t *offset, uint64_t *size);
>  
>  	/** Get statistics name */
> -	int (*get_stats_names)(int did, struct rte_vdpa_stat_name *stats_names,
> -			       unsigned int size);
> +	int (*get_stats_names)(struct rte_vdpa_device *dev,
> +			struct rte_vdpa_stat_name *stats_names,
> +			unsigned int size);
>  
>  	/** Get statistics of the queue */
> -	int (*get_stats)(int did, int qid, struct rte_vdpa_stat *stats,
> -			 unsigned int n);
> +	int (*get_stats)(struct rte_vdpa_device *dev, int qid,
> +			struct rte_vdpa_stat *stats, unsigned int n);
>  
>  	/** Reset statistics of the queue */
> -	int (*reset_stats)(int did, int qid);
> +	int (*reset_stats)(struct rte_vdpa_device *dev, int qid);
>  
>  	/** Reserved for future extension */
>  	void *reserved[2];
> @@ -118,10 +122,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);
>  
> @@ -132,13 +136,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 07db5b9e46..ed20082f0f 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)
>  {
> @@ -236,7 +239,7 @@ rte_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
>  
>  	RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_stats_names, -ENOTSUP);
>  
> -	return vdpa_dev->ops->get_stats_names(did, stats_names, size);
> +	return vdpa_dev->ops->get_stats_names(vdpa_dev, stats_names, size);
>  }
>  
>  int
> @@ -254,7 +257,7 @@ rte_vdpa_get_stats(int did, uint16_t qid, struct rte_vdpa_stat *stats,
>  
>  	RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_stats, -ENOTSUP);
>  
> -	return vdpa_dev->ops->get_stats(did, qid, stats, n);
> +	return vdpa_dev->ops->get_stats(vdpa_dev, qid, stats, n);
>  }
>  
>  int
> @@ -268,7 +271,7 @@ rte_vdpa_reset_stats(int did, uint16_t qid)
>  
>  	RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->reset_stats, -ENOTSUP);
>  
> -	return vdpa_dev->ops->reset_stats(did, qid);
> +	return vdpa_dev->ops->reset_stats(vdpa_dev, qid);
>  }
>  
>  static uint16_t
> 

Acked-by: Adrián Moreno <amorenoz@redhat.com>
diff mbox series

Patch

diff --git a/drivers/vdpa/ifc/ifcvf_vdpa.c b/drivers/vdpa/ifc/ifcvf_vdpa.c
index ac9e218c23..0418f9a07f 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;
 		}
@@ -877,14 +877,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;
 	}
 
@@ -894,7 +894,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;
 }
@@ -902,14 +903,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;
 	}
 
@@ -941,15 +942,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;
 	}
 
@@ -974,13 +975,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;
 	}
 
@@ -990,13 +991,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;
 	}
 
@@ -1006,16 +1007,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;
 	}
 
@@ -1036,13 +1037,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;
 	}
 
@@ -1052,13 +1053,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;
 	}
 
@@ -1074,8 +1075,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;
 }
@@ -1186,8 +1189,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;
 	}
@@ -1230,7 +1233,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 7b5ae62bdc..9ea032d57b 100644
--- a/drivers/vdpa/mlx5/mlx5_vdpa.c
+++ b/drivers/vdpa/mlx5/mlx5_vdpa.c
@@ -50,21 +50,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: %s.", vdev->device->name);
 		rte_errno = EINVAL;
 		return NULL;
 	}
@@ -72,12 +72,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: %s.", vdev->device->name);
 		return -1;
 	}
 	*queue_num = priv->caps.max_num_virtio_queues;
@@ -85,12 +86,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: %s.", vdev->device->name);
 		return -1;
 	}
 	*features = MLX5_VDPA_DEFAULT_FEATURES;
@@ -110,12 +112,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: %s.", vdev->device->name);
 		return -1;
 	}
 	*features = MLX5_VDPA_PROTOCOL_FEATURES;
@@ -125,11 +129,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: %s.", vdev->device->name);
 		return -EINVAL;
 	}
 	if (vring >= (int)priv->caps.max_num_virtio_queues * 2) {
@@ -165,14 +171,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: %s.", vdev->device->name);
 		return -EINVAL;
 	}
 	ret = rte_vhost_get_negotiated_features(vid, &features);
@@ -284,12 +292,14 @@  mlx5_vdpa_mtu_set(struct mlx5_vdpa_priv *priv)
 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: %s.", vdev->device->name);
 		return -1;
 	}
 	if (priv->configured)
@@ -312,11 +322,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: %s.", vdev->device->name);
 		return -EINVAL;
 	}
 	if (priv->configured && mlx5_vdpa_dev_close(vid)) {
@@ -325,7 +337,8 @@  mlx5_vdpa_dev_config(int vid)
 	}
 	priv->vid = vid;
 	if (mlx5_vdpa_mtu_set(priv))
-		DRV_LOG(WARNING, "MTU cannot be set on device %d.", did);
+		DRV_LOG(WARNING, "MTU cannot be set on device %s.",
+				vdev->device->name);
 	if (mlx5_vdpa_pd_create(priv) || mlx5_vdpa_mem_register(priv) ||
 	    mlx5_vdpa_direct_db_prepare(priv) ||
 	    mlx5_vdpa_virtqs_prepare(priv) || mlx5_vdpa_steer_setup(priv) ||
@@ -341,11 +354,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: %s.", vdev->device->name);
 		return -EINVAL;
 	}
 	return priv->ctx->cmd_fd;
@@ -354,17 +369,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: %s.", vdev->device->name);
 		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 %s, is the device"
+			" configured?.", vdev->device->name);
 		return -EINVAL;
 	}
 	*offset = priv->var->mmap_off;
@@ -373,8 +390,9 @@  mlx5_vdpa_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
 }
 
 static int
-mlx5_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
-			  unsigned int size)
+mlx5_vdpa_get_stats_names(struct rte_vdpa_device *vdev,
+		struct rte_vdpa_stat_name *stats_names,
+		unsigned int size)
 {
 	static const char *mlx5_vdpa_stats_names[MLX5_VDPA_STATS_MAX] = {
 		"received_descriptors",
@@ -384,11 +402,12 @@  mlx5_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
 		"invalid buffer",
 		"completion errors",
 	};
-	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);
 	unsigned int i;
 
 	if (priv == NULL) {
-		DRV_LOG(ERR, "Invalid device id: %d.", did);
+		DRV_LOG(ERR, "Invalid device: %s.", vdev->device->name);
 		return -ENODEV;
 	}
 	if (!stats_names)
@@ -401,51 +420,57 @@  mlx5_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
 }
 
 static int
-mlx5_vdpa_get_stats(int did, int qid, struct rte_vdpa_stat *stats,
-		    unsigned int n)
+mlx5_vdpa_get_stats(struct rte_vdpa_device *vdev, int qid,
+		struct rte_vdpa_stat *stats, unsigned int n)
 {
-	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 device: %s.", vdev->device->name);
 		return -ENODEV;
 	}
 	if (!priv->configured) {
-		DRV_LOG(ERR, "Device %d was not configured.", did);
+		DRV_LOG(ERR, "Device %s was not configured.",
+				vdev->device->name);
 		return -ENODATA;
 	}
 	if (qid >= (int)priv->nr_virtqs) {
-		DRV_LOG(ERR, "Too big vring id: %d.", qid);
+		DRV_LOG(ERR, "Too big vring id: %d for device %s.", qid,
+				vdev->device->name);
 		return -E2BIG;
 	}
 	if (!priv->caps.queue_counters_valid) {
-		DRV_LOG(ERR, "Virtq statistics is not supported for device %d.",
-			did);
+		DRV_LOG(ERR, "Virtq statistics is not supported for device %s.",
+			vdev->device->name);
 		return -ENOTSUP;
 	}
 	return mlx5_vdpa_virtq_stats_get(priv, qid, stats, n);
 }
 
 static int
-mlx5_vdpa_reset_stats(int did, int qid)
+mlx5_vdpa_reset_stats(struct rte_vdpa_device *vdev, int qid)
 {
-	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 device: %s.", vdev->device->name);
 		return -ENODEV;
 	}
 	if (!priv->configured) {
-		DRV_LOG(ERR, "Device %d was not configured.", did);
+		DRV_LOG(ERR, "Device %s was not configured.",
+				vdev->device->name);
 		return -ENODATA;
 	}
 	if (qid >= (int)priv->nr_virtqs) {
-		DRV_LOG(ERR, "Too big vring id: %d.", qid);
+		DRV_LOG(ERR, "Too big vring id: %d for device %s.", qid,
+				vdev->device->name);
 		return -E2BIG;
 	}
 	if (!priv->caps.queue_counters_valid) {
-		DRV_LOG(ERR, "Virtq statistics is not supported for device %d.",
-			did);
+		DRV_LOG(ERR, "Virtq statistics is not supported for device %s.",
+			vdev->device->name);
 		return -ENOTSUP;
 	}
 	return mlx5_vdpa_virtq_stats_reset(priv, qid);
@@ -687,8 +712,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 5fc801eff3..ea09576c5a 100644
--- a/drivers/vdpa/mlx5/mlx5_vdpa.h
+++ b/drivers/vdpa/mlx5/mlx5_vdpa.h
@@ -103,7 +103,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 e72f6646e7..bdb00603cc 100644
--- a/examples/vdpa/main.c
+++ b/examples/vdpa/main.c
@@ -291,13 +291,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 b752dfeb96..109cf83b8f 100644
--- a/lib/librte_vhost/rte_vdpa.h
+++ b/lib/librte_vhost/rte_vdpa.h
@@ -21,6 +21,8 @@ 
 /** Maximum name length for statistics counters */
 #define RTE_VDPA_STATS_NAME_SIZE 64
 
+struct rte_vdpa_device;
+
 /**
  * A vDPA device statistic structure
  *
@@ -51,13 +53,14 @@  struct rte_vdpa_stat_name {
  */
 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);
@@ -83,15 +86,16 @@  struct rte_vdpa_dev_ops {
 			uint64_t *offset, uint64_t *size);
 
 	/** Get statistics name */
-	int (*get_stats_names)(int did, struct rte_vdpa_stat_name *stats_names,
-			       unsigned int size);
+	int (*get_stats_names)(struct rte_vdpa_device *dev,
+			struct rte_vdpa_stat_name *stats_names,
+			unsigned int size);
 
 	/** Get statistics of the queue */
-	int (*get_stats)(int did, int qid, struct rte_vdpa_stat *stats,
-			 unsigned int n);
+	int (*get_stats)(struct rte_vdpa_device *dev, int qid,
+			struct rte_vdpa_stat *stats, unsigned int n);
 
 	/** Reset statistics of the queue */
-	int (*reset_stats)(int did, int qid);
+	int (*reset_stats)(struct rte_vdpa_device *dev, int qid);
 
 	/** Reserved for future extension */
 	void *reserved[2];
@@ -118,10 +122,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);
 
@@ -132,13 +136,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 07db5b9e46..ed20082f0f 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)
 {
@@ -236,7 +239,7 @@  rte_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
 
 	RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_stats_names, -ENOTSUP);
 
-	return vdpa_dev->ops->get_stats_names(did, stats_names, size);
+	return vdpa_dev->ops->get_stats_names(vdpa_dev, stats_names, size);
 }
 
 int
@@ -254,7 +257,7 @@  rte_vdpa_get_stats(int did, uint16_t qid, struct rte_vdpa_stat *stats,
 
 	RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->get_stats, -ENOTSUP);
 
-	return vdpa_dev->ops->get_stats(did, qid, stats, n);
+	return vdpa_dev->ops->get_stats(vdpa_dev, qid, stats, n);
 }
 
 int
@@ -268,7 +271,7 @@  rte_vdpa_reset_stats(int did, uint16_t qid)
 
 	RTE_FUNC_PTR_OR_ERR_RET(vdpa_dev->ops->reset_stats, -ENOTSUP);
 
-	return vdpa_dev->ops->reset_stats(did, qid);
+	return vdpa_dev->ops->reset_stats(vdpa_dev, qid);
 }
 
 static uint16_t