vdpa/sfc: get max supported queue count
[dpdk.git] / drivers / vdpa / sfc / sfc_vdpa_ops.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2020-2021 Xilinx, Inc.
3  */
4
5 #include <rte_errno.h>
6 #include <rte_malloc.h>
7 #include <rte_vdpa.h>
8 #include <rte_vhost.h>
9
10 #include <vdpa_driver.h>
11
12 #include "efx.h"
13 #include "sfc_vdpa_ops.h"
14 #include "sfc_vdpa.h"
15
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))
23
24 /*
25  * Set of features which are enabled by default.
26  * Protocol feature bit is needed to enable notification notifier ctrl.
27  */
28 #define SFC_VDPA_DEFAULT_FEATURES \
29                 (1ULL << VHOST_USER_F_PROTOCOL_FEATURES)
30
31 static int
32 sfc_vdpa_get_queue_num(struct rte_vdpa_device *vdpa_dev, uint32_t *queue_num)
33 {
34         struct sfc_vdpa_ops_data *ops_data;
35         void *dev;
36
37         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
38         if (ops_data == NULL)
39                 return -1;
40
41         dev = ops_data->dev_handle;
42         *queue_num = sfc_vdpa_adapter_by_dev_handle(dev)->max_queue_count;
43
44         sfc_vdpa_info(dev, "vDPA ops get_queue_num :: supported queue num : %u",
45                       *queue_num);
46
47         return 0;
48 }
49
50 static int
51 sfc_vdpa_get_device_features(struct sfc_vdpa_ops_data *ops_data)
52 {
53         int rc;
54         uint64_t dev_features;
55         efx_nic_t *nic;
56
57         nic = sfc_vdpa_adapter_by_dev_handle(ops_data->dev_handle)->nic;
58
59         rc = efx_virtio_get_features(nic, EFX_VIRTIO_DEVICE_TYPE_NET,
60                                      &dev_features);
61         if (rc != 0) {
62                 sfc_vdpa_err(ops_data->dev_handle,
63                              "could not read device feature: %s",
64                              rte_strerror(rc));
65                 return rc;
66         }
67
68         ops_data->dev_features = dev_features;
69
70         sfc_vdpa_info(ops_data->dev_handle,
71                       "device supported virtio features : 0x%" PRIx64,
72                       ops_data->dev_features);
73
74         return 0;
75 }
76
77 static int
78 sfc_vdpa_get_features(struct rte_vdpa_device *vdpa_dev, uint64_t *features)
79 {
80         struct sfc_vdpa_ops_data *ops_data;
81
82         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
83         if (ops_data == NULL)
84                 return -1;
85
86         *features = ops_data->drv_features;
87
88         sfc_vdpa_info(ops_data->dev_handle,
89                       "vDPA ops get_feature :: features : 0x%" PRIx64,
90                       *features);
91
92         return 0;
93 }
94
95 static int
96 sfc_vdpa_get_protocol_features(struct rte_vdpa_device *vdpa_dev,
97                                uint64_t *features)
98 {
99         struct sfc_vdpa_ops_data *ops_data;
100
101         ops_data = sfc_vdpa_get_data_by_dev(vdpa_dev);
102         if (ops_data == NULL)
103                 return -1;
104
105         *features = SFC_VDPA_PROTOCOL_FEATURES;
106
107         sfc_vdpa_info(ops_data->dev_handle,
108                       "vDPA ops get_protocol_feature :: features : 0x%" PRIx64,
109                       *features);
110
111         return 0;
112 }
113
114 static int
115 sfc_vdpa_dev_config(int vid)
116 {
117         RTE_SET_USED(vid);
118
119         return -1;
120 }
121
122 static int
123 sfc_vdpa_dev_close(int vid)
124 {
125         RTE_SET_USED(vid);
126
127         return -1;
128 }
129
130 static int
131 sfc_vdpa_set_vring_state(int vid, int vring, int state)
132 {
133         RTE_SET_USED(vid);
134         RTE_SET_USED(vring);
135         RTE_SET_USED(state);
136
137         return -1;
138 }
139
140 static int
141 sfc_vdpa_set_features(int vid)
142 {
143         RTE_SET_USED(vid);
144
145         return -1;
146 }
147
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,
156 };
157
158 struct sfc_vdpa_ops_data *
159 sfc_vdpa_device_init(void *dev_handle, enum sfc_vdpa_context context)
160 {
161         struct sfc_vdpa_ops_data *ops_data;
162         struct rte_pci_device *pci_dev;
163         int rc;
164
165         /* Create vDPA ops context */
166         ops_data = rte_zmalloc("vdpa", sizeof(struct sfc_vdpa_ops_data), 0);
167         if (ops_data == NULL)
168                 return NULL;
169
170         ops_data->vdpa_context = context;
171         ops_data->dev_handle = dev_handle;
172
173         pci_dev = sfc_vdpa_adapter_by_dev_handle(dev_handle)->pdev;
174
175         /* Register vDPA Device */
176         sfc_vdpa_log_init(dev_handle, "register vDPA device");
177         ops_data->vdpa_dev =
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;
182         }
183
184         /* Read supported device features */
185         sfc_vdpa_log_init(dev_handle, "get device feature");
186         rc = sfc_vdpa_get_device_features(ops_data);
187         if (rc != 0)
188                 goto fail_get_dev_feature;
189
190         /* Driver features are superset of device supported feature
191          * and any additional features supported by the driver.
192          */
193         ops_data->drv_features =
194                 ops_data->dev_features | SFC_VDPA_DEFAULT_FEATURES;
195
196         ops_data->state = SFC_VDPA_STATE_INITIALIZED;
197
198         return ops_data;
199
200 fail_get_dev_feature:
201         rte_vdpa_unregister_device(ops_data->vdpa_dev);
202
203 fail_register_device:
204         rte_free(ops_data);
205         return NULL;
206 }
207
208 void
209 sfc_vdpa_device_fini(struct sfc_vdpa_ops_data *ops_data)
210 {
211         rte_vdpa_unregister_device(ops_data->vdpa_dev);
212
213         rte_free(ops_data);
214 }