ethdev: remove legacy Rx descriptor done API
[dpdk.git] / drivers / net / virtio / virtio_user / vhost_vdpa.c
index d9bc213..82f25a8 100644 (file)
 #include "vhost.h"
 #include "virtio_user_dev.h"
 
+struct vhost_vdpa_data {
+       int vhostfd;
+       uint64_t protocol_features;
+};
+
+#define VHOST_VDPA_SUPPORTED_BACKEND_FEATURES          \
+       (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2   |       \
+       1ULL << VHOST_BACKEND_F_IOTLB_BATCH)
+
 /* vhost kernel & vdpa ioctls */
 #define VHOST_VIRTIO 0xAF
 #define VHOST_GET_FEATURES _IOR(VHOST_VIRTIO, 0x00, __u64)
 #define VHOST_SET_FEATURES _IOW(VHOST_VIRTIO, 0x00, __u64)
 #define VHOST_SET_OWNER _IO(VHOST_VIRTIO, 0x01)
 #define VHOST_RESET_OWNER _IO(VHOST_VIRTIO, 0x02)
-#define VHOST_SET_MEM_TABLE _IOW(VHOST_VIRTIO, 0x03, void *)
 #define VHOST_SET_LOG_BASE _IOW(VHOST_VIRTIO, 0x04, __u64)
 #define VHOST_SET_LOG_FD _IOW(VHOST_VIRTIO, 0x07, int)
 #define VHOST_SET_VRING_NUM _IOW(VHOST_VIRTIO, 0x10, struct vhost_vring_state)
 #define VHOST_VDPA_GET_DEVICE_ID _IOR(VHOST_VIRTIO, 0x70, __u32)
 #define VHOST_VDPA_GET_STATUS _IOR(VHOST_VIRTIO, 0x71, __u8)
 #define VHOST_VDPA_SET_STATUS _IOW(VHOST_VIRTIO, 0x72, __u8)
-#define VHOST_VDPA_SET_VRING_ENABLE    _IOW(VHOST_VIRTIO, 0x75, \
-                                            struct vhost_vring_state)
+#define VHOST_VDPA_GET_CONFIG _IOR(VHOST_VIRTIO, 0x73, struct vhost_vdpa_config)
+#define VHOST_VDPA_SET_CONFIG _IOW(VHOST_VIRTIO, 0x74, struct vhost_vdpa_config)
+#define VHOST_VDPA_SET_VRING_ENABLE _IOW(VHOST_VIRTIO, 0x75, struct vhost_vring_state)
 #define VHOST_SET_BACKEND_FEATURES _IOW(VHOST_VIRTIO, 0x25, __u64)
 #define VHOST_GET_BACKEND_FEATURES _IOR(VHOST_VIRTIO, 0x26, __u64)
 
-static uint64_t vhost_req_user_to_vdpa[] = {
-       [VHOST_USER_RESET_OWNER] = VHOST_RESET_OWNER,
-       [VHOST_USER_SET_FEATURES] = VHOST_SET_FEATURES,
-       [VHOST_USER_GET_FEATURES] = VHOST_GET_FEATURES,
-       [VHOST_USER_SET_VRING_CALL] = VHOST_SET_VRING_CALL,
-       [VHOST_USER_SET_VRING_NUM] = VHOST_SET_VRING_NUM,
-       [VHOST_USER_SET_VRING_BASE] = VHOST_SET_VRING_BASE,
-       [VHOST_USER_GET_VRING_BASE] = VHOST_GET_VRING_BASE,
-       [VHOST_USER_SET_VRING_ADDR] = VHOST_SET_VRING_ADDR,
-       [VHOST_USER_SET_VRING_KICK] = VHOST_SET_VRING_KICK,
-       [VHOST_USER_SET_MEM_TABLE] = VHOST_SET_MEM_TABLE,
-       [VHOST_USER_SET_STATUS] = VHOST_VDPA_SET_STATUS,
-       [VHOST_USER_GET_STATUS] = VHOST_VDPA_GET_STATUS,
-       [VHOST_USER_SET_VRING_ENABLE] = VHOST_VDPA_SET_VRING_ENABLE,
-       [VHOST_USER_GET_PROTOCOL_FEATURES] = VHOST_GET_BACKEND_FEATURES,
-       [VHOST_USER_SET_PROTOCOL_FEATURES] = VHOST_SET_BACKEND_FEATURES,
-};
-
 /* no alignment requirement */
 struct vhost_iotlb_msg {
        uint64_t iova;
@@ -76,6 +67,12 @@ struct vhost_iotlb_msg {
 
 #define VHOST_IOTLB_MSG_V2 0x2
 
+struct vhost_vdpa_config {
+       uint32_t off;
+       uint32_t len;
+       uint8_t buf[0];
+};
+
 struct vhost_msg {
        uint32_t type;
        uint32_t reserved;
@@ -104,18 +101,81 @@ vhost_vdpa_ioctl(int fd, uint64_t request, void *arg)
 static int
 vhost_vdpa_set_owner(struct virtio_user_dev *dev)
 {
-       return vhost_vdpa_ioctl(dev->vhostfd, VHOST_SET_OWNER, NULL);
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_OWNER, NULL);
+}
+
+static int
+vhost_vdpa_get_protocol_features(struct virtio_user_dev *dev, uint64_t *features)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_BACKEND_FEATURES, features);
+}
+
+static int
+vhost_vdpa_set_protocol_features(struct virtio_user_dev *dev, uint64_t features)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_BACKEND_FEATURES, &features);
+}
+
+static int
+vhost_vdpa_get_features(struct virtio_user_dev *dev, uint64_t *features)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+       int ret;
+
+       ret = vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_FEATURES, features);
+       if (ret) {
+               PMD_DRV_LOG(ERR, "Failed to get features");
+               return -1;
+       }
+
+       /* Multiqueue not supported for now */
+       *features &= ~(1ULL << VIRTIO_NET_F_MQ);
+
+       /* Negotiated vDPA backend features */
+       ret = vhost_vdpa_get_protocol_features(dev, &data->protocol_features);
+       if (ret < 0) {
+               PMD_DRV_LOG(ERR, "Failed to get backend features");
+               return -1;
+       }
+
+       data->protocol_features &= VHOST_VDPA_SUPPORTED_BACKEND_FEATURES;
+
+       ret = vhost_vdpa_set_protocol_features(dev, data->protocol_features);
+       if (ret < 0) {
+               PMD_DRV_LOG(ERR, "Failed to set backend features");
+               return -1;
+       }
+
+       return 0;
+}
+
+static int
+vhost_vdpa_set_features(struct virtio_user_dev *dev, uint64_t features)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       /* WORKAROUND */
+       features |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_FEATURES, &features);
 }
 
 static int
 vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
 {
+       struct vhost_vdpa_data *data = dev->backend_data;
        struct vhost_msg msg = {};
 
-       if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
+       if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
                return 0;
 
-       if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
+       if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
                PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
                return -1;
        }
@@ -123,7 +183,7 @@ vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
        msg.type = VHOST_IOTLB_MSG_V2;
        msg.iotlb.type = VHOST_IOTLB_BATCH_BEGIN;
 
-       if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+       if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
                PMD_DRV_LOG(ERR, "Failed to send IOTLB batch begin (%s)",
                                strerror(errno));
                return -1;
@@ -135,12 +195,13 @@ vhost_vdpa_iotlb_batch_begin(struct virtio_user_dev *dev)
 static int
 vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
 {
+       struct vhost_vdpa_data *data = dev->backend_data;
        struct vhost_msg msg = {};
 
-       if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
+       if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_BATCH)))
                return 0;
 
-       if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
+       if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
                PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
                return -1;
        }
@@ -148,7 +209,7 @@ vhost_vdpa_iotlb_batch_end(struct virtio_user_dev *dev)
        msg.type = VHOST_IOTLB_MSG_V2;
        msg.iotlb.type = VHOST_IOTLB_BATCH_END;
 
-       if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+       if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
                PMD_DRV_LOG(ERR, "Failed to send IOTLB batch end (%s)",
                                strerror(errno));
                return -1;
@@ -161,9 +222,10 @@ static int
 vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
                                  uint64_t iova, size_t len)
 {
+       struct vhost_vdpa_data *data = dev->backend_data;
        struct vhost_msg msg = {};
 
-       if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
+       if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
                PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
                return -1;
        }
@@ -178,7 +240,7 @@ vhost_vdpa_dma_map(struct virtio_user_dev *dev, void *addr,
        PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", addr: %p, len: 0x%zx",
                        __func__, iova, addr, len);
 
-       if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+       if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
                PMD_DRV_LOG(ERR, "Failed to send IOTLB update (%s)",
                                strerror(errno));
                return -1;
@@ -191,9 +253,10 @@ static int
 vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
                                  uint64_t iova, size_t len)
 {
+       struct vhost_vdpa_data *data = dev->backend_data;
        struct vhost_msg msg = {};
 
-       if (!(dev->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
+       if (!(data->protocol_features & (1ULL << VHOST_BACKEND_F_IOTLB_MSG_V2))) {
                PMD_DRV_LOG(ERR, "IOTLB_MSG_V2 not supported by the backend.");
                return -1;
        }
@@ -206,7 +269,7 @@ vhost_vdpa_dma_unmap(struct virtio_user_dev *dev, __rte_unused void *addr,
        PMD_DRV_LOG(DEBUG, "%s: iova: 0x%" PRIx64 ", len: 0x%zx",
                        __func__, iova, len);
 
-       if (write(dev->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
+       if (write(data->vhostfd, &msg, sizeof(msg)) != sizeof(msg)) {
                PMD_DRV_LOG(ERR, "Failed to send IOTLB invalidate (%s)",
                                strerror(errno));
                return -1;
@@ -283,7 +346,7 @@ vhost_vdpa_map(const struct rte_memseg_list *msl, const struct rte_memseg *ms,
 }
 
 static int
-vhost_vdpa_dma_map_all(struct virtio_user_dev *dev)
+vhost_vdpa_set_memory_table(struct virtio_user_dev *dev)
 {
        int ret;
 
@@ -313,62 +376,133 @@ batch_end:
        return ret;
 }
 
-/* with below features, vhost vdpa does not need to do the checksum and TSO,
- * these info will be passed to virtio_user through virtio net header.
- */
-#define VHOST_VDPA_GUEST_OFFLOADS_MASK \
-       ((1ULL << VIRTIO_NET_F_GUEST_CSUM) |    \
-        (1ULL << VIRTIO_NET_F_GUEST_TSO4) |    \
-        (1ULL << VIRTIO_NET_F_GUEST_TSO6) |    \
-        (1ULL << VIRTIO_NET_F_GUEST_ECN)  |    \
-        (1ULL << VIRTIO_NET_F_GUEST_UFO))
+static int
+vhost_vdpa_set_vring_enable(struct virtio_user_dev *dev, struct vhost_vring_state *state)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_SET_VRING_ENABLE, state);
+}
+
+static int
+vhost_vdpa_set_vring_num(struct virtio_user_dev *dev, struct vhost_vring_state *state)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_NUM, state);
+}
+
+static int
+vhost_vdpa_set_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_BASE, state);
+}
+
+static int
+vhost_vdpa_get_vring_base(struct virtio_user_dev *dev, struct vhost_vring_state *state)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_GET_VRING_BASE, state);
+}
+
+static int
+vhost_vdpa_set_vring_call(struct virtio_user_dev *dev, struct vhost_vring_file *file)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
 
-#define VHOST_VDPA_HOST_OFFLOADS_MASK          \
-       ((1ULL << VIRTIO_NET_F_HOST_TSO4) |     \
-        (1ULL << VIRTIO_NET_F_HOST_TSO6) |     \
-        (1ULL << VIRTIO_NET_F_CSUM))
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_CALL, file);
+}
 
 static int
-vhost_vdpa_send_request(struct virtio_user_dev *dev,
-                  enum vhost_user_request req,
-                  void *arg)
+vhost_vdpa_set_vring_kick(struct virtio_user_dev *dev, struct vhost_vring_file *file)
 {
-       int ret = -1;
-       uint64_t req_vdpa;
+       struct vhost_vdpa_data *data = dev->backend_data;
 
-       PMD_DRV_LOG(INFO, "%s", vhost_msg_strings[req]);
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_KICK, file);
+}
 
-       req_vdpa = vhost_req_user_to_vdpa[req];
+static int
+vhost_vdpa_set_vring_addr(struct virtio_user_dev *dev, struct vhost_vring_addr *addr)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
 
-       if (req_vdpa == VHOST_SET_MEM_TABLE)
-               return vhost_vdpa_dma_map_all(dev);
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_SET_VRING_ADDR, addr);
+}
 
-       if (req_vdpa == VHOST_SET_FEATURES) {
-               /* WORKAROUND */
-               *(uint64_t *)arg |= 1ULL << VIRTIO_F_IOMMU_PLATFORM;
+static int
+vhost_vdpa_get_status(struct virtio_user_dev *dev, uint8_t *status)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
 
-               /* Multiqueue not supported for now */
-               *(uint64_t *)arg &= ~(1ULL << VIRTIO_NET_F_MQ);
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_GET_STATUS, status);
+}
+
+static int
+vhost_vdpa_set_status(struct virtio_user_dev *dev, uint8_t status)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       return vhost_vdpa_ioctl(data->vhostfd, VHOST_VDPA_SET_STATUS, &status);
+}
+
+static int
+vhost_vdpa_get_config(struct virtio_user_dev *dev, uint8_t *data, uint32_t off, uint32_t len)
+{
+       struct vhost_vdpa_data *vdpa_data = dev->backend_data;
+       struct vhost_vdpa_config *config;
+       int ret = 0;
+
+       config = malloc(sizeof(*config) + len);
+       if (!config) {
+               PMD_DRV_LOG(ERR, "Failed to allocate vDPA config data");
+               return -1;
        }
 
-       switch (req_vdpa) {
-       case VHOST_SET_VRING_NUM:
-       case VHOST_SET_VRING_ADDR:
-       case VHOST_SET_VRING_BASE:
-       case VHOST_GET_VRING_BASE:
-       case VHOST_SET_VRING_KICK:
-       case VHOST_SET_VRING_CALL:
-               PMD_DRV_LOG(DEBUG, "vhostfd=%d, index=%u",
-                           dev->vhostfd, *(unsigned int *)arg);
-               break;
-       default:
-               break;
+       config->off = off;
+       config->len = len;
+
+       ret = vhost_vdpa_ioctl(vdpa_data->vhostfd, VHOST_VDPA_GET_CONFIG, config);
+       if (ret) {
+               PMD_DRV_LOG(ERR, "Failed to get vDPA config (offset 0x%x, len 0x%x)", off, len);
+               ret = -1;
+               goto out;
        }
 
-       ret = ioctl(dev->vhostfd, req_vdpa, arg);
-       if (ret < 0)
-               PMD_DRV_LOG(ERR, "%s failed: %s",
-                           vhost_msg_strings[req], strerror(errno));
+       memcpy(data, config->buf, len);
+out:
+       free(config);
+
+       return ret;
+}
+
+static int
+vhost_vdpa_set_config(struct virtio_user_dev *dev, const uint8_t *data, uint32_t off, uint32_t len)
+{
+       struct vhost_vdpa_data *vdpa_data = dev->backend_data;
+       struct vhost_vdpa_config *config;
+       int ret = 0;
+
+       config = malloc(sizeof(*config) + len);
+       if (!config) {
+               PMD_DRV_LOG(ERR, "Failed to allocate vDPA config data");
+               return -1;
+       }
+
+       config->off = off;
+       config->len = len;
+
+       memcpy(config->buf, data, len);
+
+       ret = vhost_vdpa_ioctl(vdpa_data->vhostfd, VHOST_VDPA_SET_CONFIG, config);
+       if (ret) {
+               PMD_DRV_LOG(ERR, "Failed to set vDPA config (offset 0x%x, len 0x%x)", off, len);
+               ret = -1;
+       }
+
+       free(config);
 
        return ret;
 }
@@ -383,21 +517,49 @@ vhost_vdpa_send_request(struct virtio_user_dev *dev,
 static int
 vhost_vdpa_setup(struct virtio_user_dev *dev)
 {
+       struct vhost_vdpa_data *data;
        uint32_t did = (uint32_t)-1;
 
-       dev->vhostfd = open(dev->path, O_RDWR);
-       if (dev->vhostfd < 0) {
-               PMD_DRV_LOG(ERR, "Failed to open %s: %s\n",
+       data = malloc(sizeof(*data));
+       if (!data) {
+               PMD_DRV_LOG(ERR, "(%s) Faidle to allocate backend data", dev->path);
+               return -1;
+       }
+
+       data->vhostfd = open(dev->path, O_RDWR);
+       if (data->vhostfd < 0) {
+               PMD_DRV_LOG(ERR, "Failed to open %s: %s",
                                dev->path, strerror(errno));
+               free(data);
                return -1;
        }
 
-       if (ioctl(dev->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
+       if (ioctl(data->vhostfd, VHOST_VDPA_GET_DEVICE_ID, &did) < 0 ||
                        did != VIRTIO_ID_NETWORK) {
-               PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u\n", did);
+               PMD_DRV_LOG(ERR, "Invalid vdpa device ID: %u", did);
+               close(data->vhostfd);
+               free(data);
                return -1;
        }
 
+       dev->backend_data = data;
+
+       return 0;
+}
+
+static int
+vhost_vdpa_destroy(struct virtio_user_dev *dev)
+{
+       struct vhost_vdpa_data *data = dev->backend_data;
+
+       if (!data)
+               return 0;
+
+       close(data->vhostfd);
+
+       free(data);
+       dev->backend_data = NULL;
+
        return 0;
 }
 
@@ -417,7 +579,7 @@ vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
                        .num   = enable,
                };
 
-               if (vhost_vdpa_send_request(dev, VHOST_USER_SET_VRING_ENABLE, &state))
+               if (vhost_vdpa_set_vring_enable(dev, &state))
                        return -1;
        }
 
@@ -426,11 +588,49 @@ vhost_vdpa_enable_queue_pair(struct virtio_user_dev *dev,
        return 0;
 }
 
+static int
+vhost_vdpa_get_backend_features(uint64_t *features)
+{
+       *features = 0;
+
+       return 0;
+}
+
+static int
+vhost_vdpa_update_link_state(struct virtio_user_dev *dev __rte_unused)
+{
+       /* Nothing to update (for now?) */
+       return 0;
+}
+
+static int
+vhost_vdpa_get_intr_fd(struct virtio_user_dev *dev __rte_unused)
+{
+       /* No link state interrupt with Vhost-vDPA */
+       return -1;
+}
+
 struct virtio_user_backend_ops virtio_ops_vdpa = {
        .setup = vhost_vdpa_setup,
+       .destroy = vhost_vdpa_destroy,
+       .get_backend_features = vhost_vdpa_get_backend_features,
        .set_owner = vhost_vdpa_set_owner,
-       .send_request = vhost_vdpa_send_request,
+       .get_features = vhost_vdpa_get_features,
+       .set_features = vhost_vdpa_set_features,
+       .set_memory_table = vhost_vdpa_set_memory_table,
+       .set_vring_num = vhost_vdpa_set_vring_num,
+       .set_vring_base = vhost_vdpa_set_vring_base,
+       .get_vring_base = vhost_vdpa_get_vring_base,
+       .set_vring_call = vhost_vdpa_set_vring_call,
+       .set_vring_kick = vhost_vdpa_set_vring_kick,
+       .set_vring_addr = vhost_vdpa_set_vring_addr,
+       .get_status = vhost_vdpa_get_status,
+       .set_status = vhost_vdpa_set_status,
+       .get_config = vhost_vdpa_get_config,
+       .set_config = vhost_vdpa_set_config,
        .enable_qp = vhost_vdpa_enable_queue_pair,
        .dma_map = vhost_vdpa_dma_map_batch,
        .dma_unmap = vhost_vdpa_dma_unmap_batch,
+       .update_link_state = vhost_vdpa_update_link_state,
+       .get_intr_fd = vhost_vdpa_get_intr_fd,
 };