1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020-2021 Xilinx, Inc.
6 #include <rte_malloc.h>
10 #include <vdpa_driver.h>
13 #include "sfc_vdpa_ops.h"
16 /* These protocol features are needed to enable notifier ctrl */
17 #define SFC_VDPA_PROTOCOL_FEATURES \
18 ((1ULL << VHOST_USER_PROTOCOL_F_REPLY_ACK) | \
19 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_REQ) | \
20 (1ULL << VHOST_USER_PROTOCOL_F_SLAVE_SEND_FD) | \
21 (1ULL << VHOST_USER_PROTOCOL_F_HOST_NOTIFIER) | \
22 (1ULL << VHOST_USER_PROTOCOL_F_LOG_SHMFD))
25 * Set of features which are enabled by default.
26 * Protocol feature bit is needed to enable notification notifier ctrl.
28 #define SFC_VDPA_DEFAULT_FEATURES \
29 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
32 sfc_vdpa_get_queue_num(struct rte_vdpa_device *vdpa_dev, uint32_t *queue_num)
34 struct sfc_vdpa_ops_data *ops_data;
37 ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
41 dev = ops_data->dev_handle;
42 *queue_num = sfc_vdpa_adapter_by_dev_handle(dev)->max_queue_count;
44 sfc_vdpa_info(dev, "vDPA ops get_queue_num :: supported queue num : %u",
51 sfc_vdpa_get_device_features(struct sfc_vdpa_ops_data *ops_data)
54 uint64_t dev_features;
57 nic = sfc_vdpa_adapter_by_dev_handle(ops_data->dev_handle)->nic;
59 rc = efx_virtio_get_features(nic, EFX_VIRTIO_DEVICE_TYPE_NET,
62 sfc_vdpa_err(ops_data->dev_handle,
63 "could not read device feature: %s",
68 ops_data->dev_features = dev_features;
70 sfc_vdpa_info(ops_data->dev_handle,
71 "device supported virtio features : 0x%" PRIx64,
72 ops_data->dev_features);
78 sfc_vdpa_get_features(struct rte_vdpa_device *vdpa_dev, uint64_t *features)
80 struct sfc_vdpa_ops_data *ops_data;
82 ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
86 *features = ops_data->drv_features;
88 sfc_vdpa_info(ops_data->dev_handle,
89 "vDPA ops get_feature :: features : 0x%" PRIx64,
96 sfc_vdpa_get_protocol_features(struct rte_vdpa_device *vdpa_dev,
99 struct sfc_vdpa_ops_data *ops_data;
101 ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
102 if (ops_data == NULL)
105 *features = SFC_VDPA_PROTOCOL_FEATURES;
107 sfc_vdpa_info(ops_data->dev_handle,
108 "vDPA ops get_protocol_feature :: features : 0x%" PRIx64,
115 sfc_vdpa_dev_config(int vid)
123 sfc_vdpa_dev_close(int vid)
131 sfc_vdpa_set_vring_state(int vid, int vring, int state)
141 sfc_vdpa_set_features(int vid)
149 sfc_vdpa_get_vfio_device_fd(int vid)
151 struct rte_vdpa_device *vdpa_dev;
152 struct sfc_vdpa_ops_data *ops_data;
156 vdpa_dev = rte_vhost_get_vdpa_device(vid);
158 ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
159 if (ops_data == NULL)
162 dev = ops_data->dev_handle;
163 vfio_dev_fd = sfc_vdpa_adapter_by_dev_handle(dev)->vfio_dev_fd;
165 sfc_vdpa_info(dev, "vDPA ops get_vfio_device_fd :: vfio fd : %d",
171 static struct rte_vdpa_dev_ops sfc_vdpa_ops = {
172 .get_queue_num = sfc_vdpa_get_queue_num,
173 .get_features = sfc_vdpa_get_features,
174 .get_protocol_features = sfc_vdpa_get_protocol_features,
175 .dev_conf = sfc_vdpa_dev_config,
176 .dev_close = sfc_vdpa_dev_close,
177 .set_vring_state = sfc_vdpa_set_vring_state,
178 .set_features = sfc_vdpa_set_features,
179 .get_vfio_device_fd = sfc_vdpa_get_vfio_device_fd,
182 struct sfc_vdpa_ops_data *
183 sfc_vdpa_device_init(void *dev_handle, enum sfc_vdpa_context context)
185 struct sfc_vdpa_ops_data *ops_data;
186 struct rte_pci_device *pci_dev;
189 /* Create vDPA ops context */
190 ops_data = rte_zmalloc("vdpa", sizeof(struct sfc_vdpa_ops_data), 0);
191 if (ops_data == NULL)
194 ops_data->vdpa_context = context;
195 ops_data->dev_handle = dev_handle;
197 pci_dev = sfc_vdpa_adapter_by_dev_handle(dev_handle)->pdev;
199 /* Register vDPA Device */
200 sfc_vdpa_log_init(dev_handle, "register vDPA device");
202 rte_vdpa_register_device(&pci_dev->device, &sfc_vdpa_ops);
203 if (ops_data->vdpa_dev == NULL) {
204 sfc_vdpa_err(dev_handle, "vDPA device registration failed");
205 goto fail_register_device;
208 /* Read supported device features */
209 sfc_vdpa_log_init(dev_handle, "get device feature");
210 rc = sfc_vdpa_get_device_features(ops_data);
212 goto fail_get_dev_feature;
214 /* Driver features are superset of device supported feature
215 * and any additional features supported by the driver.
217 ops_data->drv_features =
218 ops_data->dev_features | SFC_VDPA_DEFAULT_FEATURES;
220 ops_data->state = SFC_VDPA_STATE_INITIALIZED;
224 fail_get_dev_feature:
225 rte_vdpa_unregister_device(ops_data->vdpa_dev);
227 fail_register_device:
233 sfc_vdpa_device_fini(struct sfc_vdpa_ops_data *ops_data)
235 rte_vdpa_unregister_device(ops_data->vdpa_dev);