vhost: make vDPA framework bus agnostic
[dpdk.git] / drivers / vdpa / mlx5 / mlx5_vdpa.c
index 0991e5a..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);
@@ -116,20 +127,39 @@ 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 mlx5_vdpa_virtq *virtq = NULL;
 
        if (priv == NULL) {
                DRV_LOG(ERR, "Invalid device id: %d.", did);
                return -EINVAL;
        }
-       SLIST_FOREACH(virtq, &priv->virtq_list, next)
-               if (virtq->index == vring)
-                       break;
-       if (!virtq) {
-               DRV_LOG(ERR, "Invalid or unconfigured vring id: %d.", vring);
-               return -EINVAL;
+       if (vring >= (int)priv->caps.max_num_virtio_queues * 2) {
+               DRV_LOG(ERR, "Too big vring id: %d.", vring);
+               return -E2BIG;
+       }
+       return mlx5_vdpa_virtq_enable(priv, vring, state);
+}
+
+static int
+mlx5_vdpa_direct_db_prepare(struct mlx5_vdpa_priv *priv)
+{
+       int ret;
+
+       if (priv->direct_notifier) {
+               ret = rte_vhost_host_notifier_ctrl(priv->vid, false);
+               if (ret != 0) {
+                       DRV_LOG(INFO, "Direct HW notifier FD cannot be "
+                               "destroyed for device %d: %d.", priv->vid, ret);
+                       return -1;
+               }
+               priv->direct_notifier = 0;
        }
-       return mlx5_vdpa_virtq_enable(virtq, state);
+       ret = rte_vhost_host_notifier_ctrl(priv->vid, true);
+       if (ret != 0)
+               DRV_LOG(INFO, "Direct HW notifier FD cannot be configured for"
+                       " device %d: %d.", priv->vid, ret);
+       else
+               priv->direct_notifier = 1;
+       return 0;
 }
 
 static int
@@ -171,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)
 {
@@ -185,12 +295,17 @@ mlx5_vdpa_dev_close(int vid)
        if (priv->configured)
                ret |= mlx5_vdpa_lm_log(priv);
        mlx5_vdpa_cqe_event_unset(priv);
-       ret |= mlx5_vdpa_steer_unset(priv);
+       mlx5_vdpa_steer_unset(priv);
        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);
        return ret;
 }
 
@@ -209,15 +324,133 @@ mlx5_vdpa_dev_config(int vid)
                return -1;
        }
        priv->vid = vid;
-       if (mlx5_vdpa_mem_register(priv) || mlx5_vdpa_virtqs_prepare(priv) ||
-           mlx5_vdpa_steer_setup(priv) || mlx5_vdpa_cqe_event_setup(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);
                return -1;
        }
        priv->configured = 1;
+       DRV_LOG(INFO, "vDPA device %d was configured.", vid);
        return 0;
 }
 
+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);
+
+       if (priv == NULL) {
+               DRV_LOG(ERR, "Invalid device id: %d.", did);
+               return -EINVAL;
+       }
+       return priv->ctx->cmd_fd;
+}
+
+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);
+
+       RTE_SET_USED(qid);
+       if (priv == NULL) {
+               DRV_LOG(ERR, "Invalid device id: %d.", did);
+               return -EINVAL;
+       }
+       if (!priv->var) {
+               DRV_LOG(ERR, "VAR was not created for device %d, is the device"
+                       " configured?.", did);
+               return -EINVAL;
+       }
+       *offset = priv->var->mmap_off;
+       *size = priv->var->length;
+       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,
@@ -228,8 +461,11 @@ static struct rte_vdpa_dev_ops mlx5_vdpa_ops = {
        .set_features = mlx5_vdpa_features_set,
        .migration_done = NULL,
        .get_vfio_group_fd = NULL,
-       .get_vfio_device_fd = NULL,
-       .get_notify_area = 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 *
@@ -249,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;
@@ -338,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)
 {
@@ -422,44 +656,44 @@ mlx5_vdpa_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
                rte_errno = ENODEV;
                return -rte_errno;
        }
-       priv = rte_zmalloc("mlx5 vDPA device private", sizeof(*priv),
-                          RTE_CACHE_LINE_SIZE);
-       if (!priv) {
-               DRV_LOG(ERR, "Failed to allocate private memory.");
-               rte_errno = ENOMEM;
-               goto error;
-       }
        ret = mlx5_devx_cmd_query_hca_attr(ctx, &attr);
        if (ret) {
                DRV_LOG(ERR, "Unable to read HCA capabilities.");
                rte_errno = ENOTSUP;
                goto error;
-       } else {
-               if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
-                       DRV_LOG(ERR, "Not enough capabilities to support vdpa,"
-                               " maybe old FW/OFED version?");
-                       rte_errno = ENOTSUP;
-                       goto error;
-               }
-               priv->caps = attr.vdpa;
-               priv->log_max_rqt_size = attr.log_max_rqt_size;
+       } else if (!attr.vdpa.valid || !attr.vdpa.max_num_virtio_queues) {
+               DRV_LOG(ERR, "Not enough capabilities to support vdpa, maybe "
+                       "old FW/OFED version?");
+               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,
+                          RTE_CACHE_LINE_SIZE);
+       if (!priv) {
+               DRV_LOG(ERR, "Failed to allocate private memory.");
+               rte_errno = ENOMEM;
+               goto error;
+       }
+       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;
                goto error;
        }
        SLIST_INIT(&priv->mr_list);
-       SLIST_INIT(&priv->virtq_list);
        pthread_mutex_lock(&priv_list_lock);
        TAILQ_INSERT_TAIL(&priv_list, priv, next);
        pthread_mutex_unlock(&priv_list_lock);
@@ -495,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;
                }
@@ -518,14 +751,6 @@ mlx5_vdpa_pci_remove(struct rte_pci_device *pci_dev)
 }
 
 static const struct rte_pci_id mlx5_vdpa_pci_id_map[] = {
-       {
-               RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
-                              PCI_DEVICE_ID_MELLANOX_CONNECTX5BF)
-       },
-       {
-               RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
-                              PCI_DEVICE_ID_MELLANOX_CONNECTX5BFVF)
-       },
        {
                RTE_PCI_DEVICE(PCI_VENDOR_ID_MELLANOX,
                                PCI_DEVICE_ID_MELLANOX_CONNECTX6)