vhost: make vDPA framework bus agnostic
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa.c
index 9f7353d..7b5ae62 100644 (file)
@@ -2,12 +2,18 @@
  * Copyright 2019 Mellanox Technologies, Ltd
  */
 #include <unistd.h>
+#include <net/if.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <fcntl.h>
+#include <netinet/in.h>
 
 #include <rte_malloc.h>
 #include <rte_log.h>
 #include <rte_errno.h>
 #include <rte_bus_pci.h>
 #include <rte_pci.h>
+#include <rte_string_fns.h>
 
 #include <mlx5_glue.h>
 #include <mlx5_common.h>
                            (1ULL << VIRTIO_NET_F_MQ) | \
                            (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
                            (1ULL << VIRTIO_F_ORDER_PLATFORM) | \
-                           (1ULL << VHOST_F_LOG_ALL))
+                           (1ULL << VHOST_F_LOG_ALL) | \
+                           (1ULL << VIRTIO_NET_F_MTU))
 
 #define MLX5_VDPA_PROTOCOL_FEATURES \
                            ((1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
                             (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
                             (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
                             (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD) | \
-                            (1ULL << VHOST_USER_PROTOCOL_F_MQ))
+                            (1ULL << VHOST_USER_PROTOCOL_F_MQ) | \
+                            (1ULL << VHOST_USER_PROTOCOL_F_NET_MTU))
+
+#define MLX5_VDPA_MAX_RETRIES 20
+#define MLX5_VDPA_USEC 1000
 
 TAILQ_HEAD(mlx5_vdpa_privs, mlx5_vdpa_priv) priv_list =
                                              TAILQ_HEAD_INITIALIZER(priv_list);
@@ -190,6 +201,86 @@ mlx5_vdpa_features_set(int vid)
        return 0;
 }
 
+static int
+mlx5_vdpa_pd_create(struct mlx5_vdpa_priv *priv)
+{
+#ifdef HAVE_IBV_FLOW_DV_SUPPORT
+       priv->pd = mlx5_glue->alloc_pd(priv->ctx);
+       if (priv->pd == NULL) {
+               DRV_LOG(ERR, "Failed to allocate PD.");
+               return errno ? -errno : -ENOMEM;
+       }
+       struct mlx5dv_obj obj;
+       struct mlx5dv_pd pd_info;
+       int ret = 0;
+
+       obj.pd.in = priv->pd;
+       obj.pd.out = &pd_info;
+       ret = mlx5_glue->dv_init_obj(&obj, MLX5DV_OBJ_PD);
+       if (ret) {
+               DRV_LOG(ERR, "Fail to get PD object info.");
+               mlx5_glue->dealloc_pd(priv->pd);
+               priv->pd = NULL;
+               return -errno;
+       }
+       priv->pdn = pd_info.pdn;
+       return 0;
+#else
+       (void)priv;
+       DRV_LOG(ERR, "Cannot get pdn - no DV support.");
+       return -ENOTSUP;
+#endif /* HAVE_IBV_FLOW_DV_SUPPORT */
+}
+
+static int
+mlx5_vdpa_mtu_set(struct mlx5_vdpa_priv *priv)
+{
+       struct ifreq request;
+       uint16_t vhost_mtu = 0;
+       uint16_t kern_mtu = 0;
+       int ret = rte_vhost_get_mtu(priv->vid, &vhost_mtu);
+       int sock;
+       int retries = MLX5_VDPA_MAX_RETRIES;
+
+       if (ret) {
+               DRV_LOG(DEBUG, "Cannot get vhost MTU - %d.", ret);
+               return ret;
+       }
+       if (!vhost_mtu) {
+               DRV_LOG(DEBUG, "Vhost MTU is 0.");
+               return ret;
+       }
+       ret = mlx5_get_ifname_sysfs(priv->ctx->device->ibdev_path,
+                                   request.ifr_name);
+       if (ret) {
+               DRV_LOG(DEBUG, "Cannot get kernel IF name - %d.", ret);
+               return ret;
+       }
+       sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
+       if (sock == -1) {
+               DRV_LOG(DEBUG, "Cannot open IF socket.");
+               return sock;
+       }
+       while (retries--) {
+               ret = ioctl(sock, SIOCGIFMTU, &request);
+               if (ret == -1)
+                       break;
+               kern_mtu = request.ifr_mtu;
+               DRV_LOG(DEBUG, "MTU: current %d requested %d.", (int)kern_mtu,
+                       (int)vhost_mtu);
+               if (kern_mtu == vhost_mtu)
+                       break;
+               request.ifr_mtu = vhost_mtu;
+               ret = ioctl(sock, SIOCSIFMTU, &request);
+               if (ret == -1)
+                       break;
+               request.ifr_mtu = 0;
+               usleep(MLX5_VDPA_USEC);
+       }
+       close(sock);
+       return kern_mtu == vhost_mtu ? 0 : -1;
+}
+
 static int
 mlx5_vdpa_dev_close(int vid)
 {
@@ -208,6 +299,10 @@ mlx5_vdpa_dev_close(int vid)
        mlx5_vdpa_virtqs_release(priv);
        mlx5_vdpa_event_qp_global_release(priv);
        mlx5_vdpa_mem_dereg(priv);
+       if (priv->pd) {
+               claim_zero(mlx5_glue->dealloc_pd(priv->pd));
+               priv->pd = NULL;
+       }
        priv->configured = 0;
        priv->vid = 0;
        DRV_LOG(INFO, "vDPA device %d was closed.", vid);
@@ -229,7 +324,10 @@ mlx5_vdpa_dev_config(int vid)
                return -1;
        }
        priv->vid = vid;
-       if (mlx5_vdpa_mem_register(priv) || mlx5_vdpa_direct_db_prepare(priv) ||
+       if (mlx5_vdpa_mtu_set(priv))
+               DRV_LOG(WARNING, "MTU cannot be set on device %d.", did);
+       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) ||
            mlx5_vdpa_cqe_event_setup(priv)) {
                mlx5_vdpa_dev_close(vid);
@@ -274,6 +372,85 @@ mlx5_vdpa_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
        return 0;
 }
 
+static int
+mlx5_vdpa_get_stats_names(int did, struct rte_vdpa_stat_name *stats_names,
+                         unsigned int size)
+{
+       static const char *mlx5_vdpa_stats_names[MLX5_VDPA_STATS_MAX] = {
+               "received_descriptors",
+               "completed_descriptors",
+               "bad descriptor errors",
+               "exceed max chain",
+               "invalid buffer",
+               "completion errors",
+       };
+       struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+       unsigned int i;
+
+       if (priv == NULL) {
+               DRV_LOG(ERR, "Invalid device id: %d.", did);
+               return -ENODEV;
+       }
+       if (!stats_names)
+               return MLX5_VDPA_STATS_MAX;
+       size = RTE_MIN(size, (unsigned int)MLX5_VDPA_STATS_MAX);
+       for (i = 0; i < size; ++i)
+               strlcpy(stats_names[i].name, mlx5_vdpa_stats_names[i],
+                       RTE_VDPA_STATS_NAME_SIZE);
+       return size;
+}
+
+static int
+mlx5_vdpa_get_stats(int did, int qid, struct rte_vdpa_stat *stats,
+                   unsigned int n)
+{
+       struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+
+       if (priv == NULL) {
+               DRV_LOG(ERR, "Invalid device id: %d.", did);
+               return -ENODEV;
+       }
+       if (!priv->configured) {
+               DRV_LOG(ERR, "Device %d was not configured.", did);
+               return -ENODATA;
+       }
+       if (qid >= (int)priv->nr_virtqs) {
+               DRV_LOG(ERR, "Too big vring id: %d.", qid);
+               return -E2BIG;
+       }
+       if (!priv->caps.queue_counters_valid) {
+               DRV_LOG(ERR, "Virtq statistics is not supported for device %d.",
+                       did);
+               return -ENOTSUP;
+       }
+       return mlx5_vdpa_virtq_stats_get(priv, qid, stats, n);
+}
+
+static int
+mlx5_vdpa_reset_stats(int did, int qid)
+{
+       struct mlx5_vdpa_priv *priv = mlx5_vdpa_find_priv_resource_by_did(did);
+
+       if (priv == NULL) {
+               DRV_LOG(ERR, "Invalid device id: %d.", did);
+               return -ENODEV;
+       }
+       if (!priv->configured) {
+               DRV_LOG(ERR, "Device %d was not configured.", did);
+               return -ENODATA;
+       }
+       if (qid >= (int)priv->nr_virtqs) {
+               DRV_LOG(ERR, "Too big vring id: %d.", qid);
+               return -E2BIG;
+       }
+       if (!priv->caps.queue_counters_valid) {
+               DRV_LOG(ERR, "Virtq statistics is not supported for device %d.",
+                       did);
+               return -ENOTSUP;
+       }
+       return mlx5_vdpa_virtq_stats_reset(priv, qid);
+}
+
 static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
        .get_queue_num = mlx5_vdpa_get_queue_num,
        .get_features = mlx5_vdpa_get_vdpa_features,
@@ -286,6 +463,9 @@ static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
        .get_vfio_group_fd = NULL,
        .get_vfio_device_fd = mlx5_vdpa_get_device_fd,
        .get_notify_area = mlx5_vdpa_get_notify_area,
+       .get_stats_names = mlx5_vdpa_get_stats_names,
+       .get_stats = mlx5_vdpa_get_stats,
+       .reset_stats = mlx5_vdpa_reset_stats,
 };
 
 static struct ibv_device *
@@ -305,7 +485,7 @@ mlx5_vdpa_get_ib_device_match(struct rte_pci_addr *addr)
                DRV_LOG(DEBUG, "Checking device \"%s\"..", ibv_list[n]->name);
                if (mlx5_dev_to_pci_addr(ibv_list[n]->ibdev_path, &pci_addr))
                        continue;
-               if (memcmp(addr, &pci_addr, sizeof(pci_addr)))
+               if (rte_pci_addr_cmp(addr, &pci_addr))
                        continue;
                ibv_match = ibv_list[n];
                break;
@@ -394,8 +574,6 @@ close:
        return ret;
 }
 
-#define MLX5_VDPA_MAX_RETRIES 20
-#define MLX5_VDPA_USEC 1000
 static int
 mlx5_vdpa_roce_disable(struct rte_pci_addr *addr, struct ibv_device **ibv)
 {
@@ -489,6 +667,8 @@ mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
                rte_errno = ENOTSUP;
                goto error;
        }
+       if (!attr.vdpa.queue_counters_valid)
+               DRV_LOG(DEBUG, "No capability to support virtq statistics.");
        priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv) +
                           sizeof(struct mlx5_vdpa_virtq) *
                           attr.vdpa.max_num_virtio_queues * 2,
@@ -501,14 +681,13 @@ mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
        priv->caps = attr.vdpa;
        priv->log_max_rqt_size = attr.log_max_rqt_size;
        priv->ctx = ctx;
-       priv->dev_addr.pci_addr = pci_dev->addr;
-       priv->dev_addr.type = VDPA_ADDR_PCI;
+       priv->pci_dev = pci_dev;
        priv->var = mlx5_glue->dv_alloc_var(ctx, 0);
        if (!priv->var) {
                DRV_LOG(ERR, "Failed to allocate VAR %u.\n", errno);
                goto error;
        }
-       priv->id = rte_vdpa_register_device(&priv->dev_addr, &mlx5_vdpa_ops);
+       priv->id = rte_vdpa_register_device(&pci_dev->device, &mlx5_vdpa_ops);
        if (priv->id < 0) {
                DRV_LOG(ERR, "Failed to register vDPA device.");
                rte_errno = rte_errno ? rte_errno : EINVAL;
@@ -550,8 +729,7 @@ mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
 
        pthread_mutex_lock(&priv_list_lock);
        TAILQ_FOREACH(priv, &priv_list, next) {
-               if (memcmp(&priv->dev_addr.pci_addr, &pci_dev->addr,
-                          sizeof(pci_dev->addr)) == 0) {
+               if (!rte_pci_addr_cmp(&priv->pci_dev->addr, &pci_dev->addr)) {
                        found = 1;
                        break;
                }