vdpa/mlx5: add task ring for multi-thread management
[dpdk.git] / drivers / vdpa / sfc / sfc_vdpa_ops.c
index 2c2429c..b3d9b6c 100644 (file)
@@ -2,6 +2,8 @@
  * Copyright(c) 2020-2021 Xilinx, Inc.
  */
 
+#include <pthread.h>
+#include <unistd.h>
 #include <sys/ioctl.h>
 
 #include <rte_errno.h>
@@ -419,6 +421,8 @@ sfc_vdpa_stop(struct sfc_vdpa_ops_data *ops_data)
 
        sfc_vdpa_disable_vfio_intr(ops_data);
 
+       sfc_vdpa_filter_remove(ops_data);
+
        ops_data->state = SFC_VDPA_STATE_CONFIGURED;
 }
 
@@ -458,12 +462,27 @@ sfc_vdpa_start(struct sfc_vdpa_ops_data *ops_data)
                        goto fail_vq_start;
        }
 
+       ops_data->vq_count = i;
+
+       sfc_vdpa_log_init(ops_data->dev_handle,
+                         "configure MAC filters");
+       rc = sfc_vdpa_filter_config(ops_data);
+       if (rc != 0) {
+               sfc_vdpa_err(ops_data->dev_handle,
+                            "MAC filter config failed: %s",
+                            rte_strerror(rc));
+               goto fail_filter_cfg;
+       }
+
        ops_data->state = SFC_VDPA_STATE_STARTED;
 
        sfc_vdpa_log_init(ops_data->dev_handle, "done");
 
        return 0;
 
+fail_filter_cfg:
+       /* remove already created filters */
+       sfc_vdpa_filter_remove(ops_data);
 fail_vq_start:
        /* stop already started virtqueues */
        for (j = 0; j < i; j++)
@@ -532,6 +551,55 @@ sfc_vdpa_get_protocol_features(struct rte_vdpa_device *vdpa_dev,
        return 0;
 }
 
+static void *
+sfc_vdpa_notify_ctrl(void *arg)
+{
+       struct sfc_vdpa_ops_data *ops_data;
+       int vid;
+
+       ops_data = arg;
+       if (ops_data == NULL)
+               return NULL;
+
+       sfc_vdpa_adapter_lock(ops_data->dev_handle);
+
+       vid = ops_data->vid;
+
+       if (rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, true) != 0)
+               sfc_vdpa_info(ops_data->dev_handle,
+                             "vDPA (%s): Notifier could not get configured",
+                             ops_data->vdpa_dev->device->name);
+
+       sfc_vdpa_adapter_unlock(ops_data->dev_handle);
+
+       return NULL;
+}
+
+static int
+sfc_vdpa_setup_notify_ctrl(struct sfc_vdpa_ops_data *ops_data)
+{
+       int ret;
+
+       ops_data->is_notify_thread_started = false;
+
+       /*
+        * Use rte_vhost_host_notifier_ctrl in a thread to avoid
+        * dead lock scenario when multiple VFs are used in single vdpa
+        * application and multiple VFs are passed to a single VM.
+        */
+       ret = pthread_create(&ops_data->notify_tid, NULL,
+                            sfc_vdpa_notify_ctrl, ops_data);
+       if (ret != 0) {
+               sfc_vdpa_err(ops_data->dev_handle,
+                            "failed to create notify_ctrl thread: %s",
+                            rte_strerror(ret));
+               return -1;
+       }
+       ops_data->is_notify_thread_started = true;
+
+       return 0;
+}
+
 static int
 sfc_vdpa_dev_config(int vid)
 {
@@ -543,7 +611,7 @@ sfc_vdpa_dev_config(int vid)
 
        ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
        if (ops_data == NULL) {
-               sfc_vdpa_err(ops_data->dev_handle,
+               SFC_VDPA_GENERIC_LOG(ERR,
                             "invalid vDPA device : %p, vid : %d",
                             vdpa_dev, vid);
                return -1;
@@ -565,18 +633,19 @@ sfc_vdpa_dev_config(int vid)
        if (rc != 0)
                goto fail_vdpa_start;
 
-       sfc_vdpa_adapter_unlock(ops_data->dev_handle);
+       rc = sfc_vdpa_setup_notify_ctrl(ops_data);
+       if (rc != 0)
+               goto fail_vdpa_notify;
 
-       sfc_vdpa_log_init(ops_data->dev_handle, "vhost notifier ctrl");
-       if (rte_vhost_host_notifier_ctrl(vid, RTE_VHOST_QUEUE_ALL, true) != 0)
-               sfc_vdpa_info(ops_data->dev_handle,
-                             "vDPA (%s): software relay for notify is used.",
-                             vdpa_dev->device->name);
+       sfc_vdpa_adapter_unlock(ops_data->dev_handle);
 
        sfc_vdpa_log_init(ops_data->dev_handle, "done");
 
        return 0;
 
+fail_vdpa_notify:
+       sfc_vdpa_stop(ops_data);
+
 fail_vdpa_start:
        sfc_vdpa_close(ops_data);
 
@@ -589,6 +658,7 @@ fail_vdpa_config:
 static int
 sfc_vdpa_dev_close(int vid)
 {
+       int ret;
        struct rte_vdpa_device *vdpa_dev;
        struct sfc_vdpa_ops_data *ops_data;
 
@@ -596,13 +666,30 @@ sfc_vdpa_dev_close(int vid)
 
        ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
        if (ops_data == NULL) {
-               sfc_vdpa_err(ops_data->dev_handle,
+               SFC_VDPA_GENERIC_LOG(ERR,
                             "invalid vDPA device : %p, vid : %d",
                             vdpa_dev, vid);
                return -1;
        }
 
        sfc_vdpa_adapter_lock(ops_data->dev_handle);
+       if (ops_data->is_notify_thread_started == true) {
+               void *status;
+               ret = pthread_cancel(ops_data->notify_tid);
+               if (ret != 0) {
+                       sfc_vdpa_err(ops_data->dev_handle,
+                                    "failed to cancel notify_ctrl thread: %s",
+                                    rte_strerror(ret));
+               }
+
+               ret = pthread_join(ops_data->notify_tid, &status);
+               if (ret != 0) {
+                       sfc_vdpa_err(ops_data->dev_handle,
+                                    "failed to join terminated notify_ctrl thread: %s",
+                                    rte_strerror(ret));
+               }
+       }
+       ops_data->is_notify_thread_started = false;
 
        sfc_vdpa_stop(ops_data);
        sfc_vdpa_close(ops_data);
@@ -615,11 +702,57 @@ sfc_vdpa_dev_close(int vid)
 static int
 sfc_vdpa_set_vring_state(int vid, int vring, int state)
 {
-       RTE_SET_USED(vid);
-       RTE_SET_USED(vring);
-       RTE_SET_USED(state);
+       struct sfc_vdpa_ops_data *ops_data;
+       struct rte_vdpa_device *vdpa_dev;
+       efx_rc_t rc;
+       int vring_max;
+       void *dev;
 
-       return -1;
+       vdpa_dev = rte_vhost_get_vdpa_device(vid);
+
+       ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
+       if (ops_data == NULL)
+               return -1;
+
+       dev = ops_data->dev_handle;
+
+       sfc_vdpa_info(dev,
+                     "vDPA ops set_vring_state: vid: %d, vring: %d, state:%d",
+                     vid, vring, state);
+
+       vring_max = (sfc_vdpa_adapter_by_dev_handle(dev)->max_queue_count * 2);
+
+       if (vring < 0 || vring > vring_max) {
+               sfc_vdpa_err(dev, "received invalid vring id : %d to set state",
+                            vring);
+               return -1;
+       }
+
+       /*
+        * Skip if device is not yet started. virtqueues state can be
+        * changed once it is created and other configurations are done.
+        */
+       if (ops_data->state != SFC_VDPA_STATE_STARTED)
+               return 0;
+
+       if (ops_data->vq_cxt[vring].enable == state)
+               return 0;
+
+       if (state == 0) {
+               rc = sfc_vdpa_virtq_stop(ops_data, vring);
+               if (rc != 0) {
+                       sfc_vdpa_err(dev, "virtqueue stop failed: %s",
+                                    rte_strerror(rc));
+               }
+       } else {
+               rc = sfc_vdpa_virtq_start(ops_data, vring);
+               if (rc != 0) {
+                       sfc_vdpa_err(dev, "virtqueue start failed: %s",
+                                    rte_strerror(rc));
+               }
+       }
+
+       return rc;
 }
 
 static int
@@ -653,6 +786,79 @@ sfc_vdpa_get_vfio_device_fd(int vid)
        return vfio_dev_fd;
 }
 
+static int
+sfc_vdpa_get_notify_area(int vid, int qid, uint64_t *offset, uint64_t *size)
+{
+       int ret;
+       efx_nic_t *nic;
+       int vfio_dev_fd;
+       efx_rc_t rc;
+       unsigned int bar_offset;
+       struct rte_vdpa_device *vdpa_dev;
+       struct sfc_vdpa_ops_data *ops_data;
+       struct vfio_region_info reg = { .argsz = sizeof(reg) };
+       const efx_nic_cfg_t *encp;
+       int max_vring_cnt;
+       int64_t len;
+       void *dev;
+
+       vdpa_dev = rte_vhost_get_vdpa_device(vid);
+
+       ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
+       if (ops_data == NULL)
+               return -1;
+
+       dev = ops_data->dev_handle;
+
+       vfio_dev_fd = sfc_vdpa_adapter_by_dev_handle(dev)->vfio_dev_fd;
+       max_vring_cnt =
+               (sfc_vdpa_adapter_by_dev_handle(dev)->max_queue_count * 2);
+
+       nic = sfc_vdpa_adapter_by_dev_handle(ops_data->dev_handle)->nic;
+       encp = efx_nic_cfg_get(nic);
+
+       if (qid >= max_vring_cnt) {
+               sfc_vdpa_err(dev, "invalid qid : %d", qid);
+               return -1;
+       }
+
+       if (ops_data->vq_cxt[qid].enable != B_TRUE) {
+               sfc_vdpa_err(dev, "vq is not enabled");
+               return -1;
+       }
+
+       rc = efx_virtio_get_doorbell_offset(ops_data->vq_cxt[qid].vq,
+                                           &bar_offset);
+       if (rc != 0) {
+               sfc_vdpa_err(dev, "failed to get doorbell offset: %s",
+                            rte_strerror(rc));
+               return rc;
+       }
+
+       reg.index = sfc_vdpa_adapter_by_dev_handle(dev)->mem_bar.esb_rid;
+       ret = ioctl(vfio_dev_fd, VFIO_DEVICE_GET_REGION_INFO, &reg);
+       if (ret != 0) {
+               sfc_vdpa_err(dev, "could not get device region info: %s",
+                            strerror(errno));
+               return ret;
+       }
+
+       *offset = reg.offset + bar_offset;
+
+       len = (1U << encp->enc_vi_window_shift) / 2;
+       if (len >= sysconf(_SC_PAGESIZE)) {
+               *size = sysconf(_SC_PAGESIZE);
+       } else {
+               sfc_vdpa_err(dev, "invalid VI window size : 0x%" PRIx64, len);
+               return -1;
+       }
+
+       sfc_vdpa_info(dev, "vDPA ops get_notify_area :: offset : 0x%" PRIx64,
+                     *offset);
+
+       return 0;
+}
+
 static struct rte_vdpa_dev_ops sfc_vdpa_ops = {
        .get_queue_num = sfc_vdpa_get_queue_num,
        .get_features = sfc_vdpa_get_features,
@@ -662,6 +868,7 @@ static struct rte_vdpa_dev_ops sfc_vdpa_ops = {
        .set_vring_state = sfc_vdpa_set_vring_state,
        .set_features = sfc_vdpa_set_features,
        .get_vfio_device_fd = sfc_vdpa_get_vfio_device_fd,
+       .get_notify_area = sfc_vdpa_get_notify_area,
 };
 
 struct sfc_vdpa_ops_data *