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)
148 static struct rte_vdpa_dev_ops sfc_vdpa_ops = {
149 .get_queue_num = sfc_vdpa_get_queue_num,
150 .get_features = sfc_vdpa_get_features,
151 .get_protocol_features = sfc_vdpa_get_protocol_features,
152 .dev_conf = sfc_vdpa_dev_config,
153 .dev_close = sfc_vdpa_dev_close,
154 .set_vring_state = sfc_vdpa_set_vring_state,
155 .set_features = sfc_vdpa_set_features,
158 struct sfc_vdpa_ops_data *
159 sfc_vdpa_device_init(void *dev_handle, enum sfc_vdpa_context context)
161 struct sfc_vdpa_ops_data *ops_data;
162 struct rte_pci_device *pci_dev;
165 /* Create vDPA ops context */
166 ops_data = rte_zmalloc("vdpa", sizeof(struct sfc_vdpa_ops_data), 0);
167 if (ops_data == NULL)
170 ops_data->vdpa_context = context;
171 ops_data->dev_handle = dev_handle;
173 pci_dev = sfc_vdpa_adapter_by_dev_handle(dev_handle)->pdev;
175 /* Register vDPA Device */
176 sfc_vdpa_log_init(dev_handle, "register vDPA device");
178 rte_vdpa_register_device(&pci_dev->device, &sfc_vdpa_ops);
179 if (ops_data->vdpa_dev == NULL) {
180 sfc_vdpa_err(dev_handle, "vDPA device registration failed");
181 goto fail_register_device;
184 /* Read supported device features */
185 sfc_vdpa_log_init(dev_handle, "get device feature");
186 rc = sfc_vdpa_get_device_features(ops_data);
188 goto fail_get_dev_feature;
190 /* Driver features are superset of device supported feature
191 * and any additional features supported by the driver.
193 ops_data->drv_features =
194 ops_data->dev_features | SFC_VDPA_DEFAULT_FEATURES;
196 ops_data->state = SFC_VDPA_STATE_INITIALIZED;
200 fail_get_dev_feature:
201 rte_vdpa_unregister_device(ops_data->vdpa_dev);
203 fail_register_device:
209 sfc_vdpa_device_fini(struct sfc_vdpa_ops_data *ops_data)
211 rte_vdpa_unregister_device(ops_data->vdpa_dev);