95c6696b8cc9e9c7f6d124a30e6d0885983a4df3
[dpdk.git] / drivers / net / i40e / i40e_vf_representor.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018 Intel Corporation.
3  */
4
5 #include <rte_bus_pci.h>
6 #include <rte_ethdev.h>
7 #include <rte_pci.h>
8 #include <rte_malloc.h>
9
10 #include "base/i40e_type.h"
11 #include "base/virtchnl.h"
12 #include "i40e_ethdev.h"
13 #include "i40e_rxtx.h"
14 #include "rte_pmd_i40e.h"
15
16 static int
17 i40e_vf_representor_link_update(struct rte_eth_dev *ethdev,
18         int wait_to_complete)
19 {
20         struct i40e_vf_representor *representor = ethdev->data->dev_private;
21
22         return i40e_dev_link_update(representor->adapter->eth_dev,
23                 wait_to_complete);
24 }
25 static void
26 i40e_vf_representor_dev_infos_get(struct rte_eth_dev *ethdev,
27         struct rte_eth_dev_info *dev_info)
28 {
29         struct i40e_vf_representor *representor = ethdev->data->dev_private;
30
31         /* get dev info for the vdev */
32         dev_info->device = ethdev->device;
33
34         dev_info->max_rx_queues = ethdev->data->nb_rx_queues;
35         dev_info->max_tx_queues = ethdev->data->nb_tx_queues;
36
37         dev_info->min_rx_bufsize = I40E_BUF_SIZE_MIN;
38         dev_info->max_rx_pktlen = I40E_FRAME_SIZE_MAX;
39         dev_info->hash_key_size = (I40E_VFQF_HKEY_MAX_INDEX + 1) *
40                 sizeof(uint32_t);
41         dev_info->reta_size = ETH_RSS_RETA_SIZE_64;
42         dev_info->flow_type_rss_offloads = I40E_RSS_OFFLOAD_ALL;
43         dev_info->max_mac_addrs = I40E_NUM_MACADDR_MAX;
44         dev_info->rx_offload_capa =
45                 DEV_RX_OFFLOAD_VLAN_STRIP |
46                 DEV_RX_OFFLOAD_QINQ_STRIP |
47                 DEV_RX_OFFLOAD_IPV4_CKSUM |
48                 DEV_RX_OFFLOAD_UDP_CKSUM |
49                 DEV_RX_OFFLOAD_TCP_CKSUM;
50         dev_info->tx_offload_capa =
51                 DEV_TX_OFFLOAD_VLAN_INSERT |
52                 DEV_TX_OFFLOAD_QINQ_INSERT |
53                 DEV_TX_OFFLOAD_IPV4_CKSUM |
54                 DEV_TX_OFFLOAD_UDP_CKSUM |
55                 DEV_TX_OFFLOAD_TCP_CKSUM |
56                 DEV_TX_OFFLOAD_SCTP_CKSUM |
57                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
58                 DEV_TX_OFFLOAD_TCP_TSO |
59                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
60                 DEV_TX_OFFLOAD_GRE_TNL_TSO |
61                 DEV_TX_OFFLOAD_IPIP_TNL_TSO |
62                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO;
63
64         dev_info->default_rxconf = (struct rte_eth_rxconf) {
65                 .rx_thresh = {
66                         .pthresh = I40E_DEFAULT_RX_PTHRESH,
67                         .hthresh = I40E_DEFAULT_RX_HTHRESH,
68                         .wthresh = I40E_DEFAULT_RX_WTHRESH,
69                 },
70                 .rx_free_thresh = I40E_DEFAULT_RX_FREE_THRESH,
71                 .rx_drop_en = 0,
72                 .offloads = 0,
73         };
74
75         dev_info->default_txconf = (struct rte_eth_txconf) {
76                 .tx_thresh = {
77                         .pthresh = I40E_DEFAULT_TX_PTHRESH,
78                         .hthresh = I40E_DEFAULT_TX_HTHRESH,
79                         .wthresh = I40E_DEFAULT_TX_WTHRESH,
80                 },
81                 .tx_free_thresh = I40E_DEFAULT_TX_FREE_THRESH,
82                 .tx_rs_thresh = I40E_DEFAULT_TX_RSBIT_THRESH,
83                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
84                                 ETH_TXQ_FLAGS_NOOFFLOADS,
85                 .offloads = 0,
86         };
87
88         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
89                 .nb_max = I40E_MAX_RING_DESC,
90                 .nb_min = I40E_MIN_RING_DESC,
91                 .nb_align = I40E_ALIGN_RING_DESC,
92         };
93
94         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
95                 .nb_max = I40E_MAX_RING_DESC,
96                 .nb_min = I40E_MIN_RING_DESC,
97                 .nb_align = I40E_ALIGN_RING_DESC,
98         };
99
100         dev_info->switch_info.name =
101                 representor->adapter->eth_dev->device->name;
102         dev_info->switch_info.domain_id = representor->switch_domain_id;
103         dev_info->switch_info.port_id = representor->vf_id;
104 }
105
106 static int
107 i40e_vf_representor_dev_configure(__rte_unused struct rte_eth_dev *dev)
108 {
109         return 0;
110 }
111
112 static int
113 i40e_vf_representor_dev_start(__rte_unused struct rte_eth_dev *dev)
114 {
115         return 0;
116 }
117
118 static void
119 i40e_vf_representor_dev_stop(__rte_unused struct rte_eth_dev *dev)
120 {
121 }
122
123 static int
124 i40e_vf_representor_rx_queue_setup(__rte_unused struct rte_eth_dev *dev,
125         __rte_unused uint16_t rx_queue_id,
126         __rte_unused uint16_t nb_rx_desc,
127         __rte_unused unsigned int socket_id,
128         __rte_unused const struct rte_eth_rxconf *rx_conf,
129         __rte_unused struct rte_mempool *mb_pool)
130 {
131         return 0;
132 }
133
134 static int
135 i40e_vf_representor_tx_queue_setup(__rte_unused struct rte_eth_dev *dev,
136         __rte_unused uint16_t rx_queue_id,
137         __rte_unused uint16_t nb_rx_desc,
138         __rte_unused unsigned int socket_id,
139         __rte_unused const struct rte_eth_txconf *tx_conf)
140 {
141         return 0;
142 }
143
144 static int
145 i40e_vf_representor_stats_get(struct rte_eth_dev *ethdev,
146                 struct rte_eth_stats *stats)
147 {
148         struct i40e_vf_representor *representor = ethdev->data->dev_private;
149
150         return rte_pmd_i40e_get_vf_stats(
151                 representor->adapter->eth_dev->data->port_id,
152                 representor->vf_id, stats);
153 }
154
155 static void
156 i40e_vf_representor_stats_reset(struct rte_eth_dev *ethdev)
157 {
158         struct i40e_vf_representor *representor = ethdev->data->dev_private;
159
160         rte_pmd_i40e_reset_vf_stats(
161                 representor->adapter->eth_dev->data->port_id,
162                 representor->vf_id);
163 }
164
165 static void
166 i40e_vf_representor_promiscuous_enable(struct rte_eth_dev *ethdev)
167 {
168         struct i40e_vf_representor *representor = ethdev->data->dev_private;
169
170         rte_pmd_i40e_set_vf_unicast_promisc(
171                 representor->adapter->eth_dev->data->port_id,
172                 representor->vf_id, 1);
173 }
174
175 static void
176 i40e_vf_representor_promiscuous_disable(struct rte_eth_dev *ethdev)
177 {
178         struct i40e_vf_representor *representor = ethdev->data->dev_private;
179
180         rte_pmd_i40e_set_vf_unicast_promisc(
181                 representor->adapter->eth_dev->data->port_id,
182                 representor->vf_id, 0);
183 }
184
185 static void
186 i40e_vf_representor_allmulticast_enable(struct rte_eth_dev *ethdev)
187 {
188         struct i40e_vf_representor *representor = ethdev->data->dev_private;
189
190         rte_pmd_i40e_set_vf_multicast_promisc(
191                 representor->adapter->eth_dev->data->port_id,
192                 representor->vf_id,  1);
193 }
194
195 static void
196 i40e_vf_representor_allmulticast_disable(struct rte_eth_dev *ethdev)
197 {
198         struct i40e_vf_representor *representor = ethdev->data->dev_private;
199
200         rte_pmd_i40e_set_vf_multicast_promisc(
201                 representor->adapter->eth_dev->data->port_id,
202                 representor->vf_id,  0);
203 }
204
205 static void
206 i40e_vf_representor_mac_addr_remove(struct rte_eth_dev *ethdev, uint32_t index)
207 {
208         struct i40e_vf_representor *representor = ethdev->data->dev_private;
209
210         rte_pmd_i40e_remove_vf_mac_addr(
211                 representor->adapter->eth_dev->data->port_id,
212                 representor->vf_id, &ethdev->data->mac_addrs[index]);
213 }
214
215 static int
216 i40e_vf_representor_mac_addr_set(struct rte_eth_dev *ethdev,
217                 struct ether_addr *mac_addr)
218 {
219         struct i40e_vf_representor *representor = ethdev->data->dev_private;
220
221         return rte_pmd_i40e_set_vf_mac_addr(
222                 representor->adapter->eth_dev->data->port_id,
223                 representor->vf_id, mac_addr);
224 }
225
226 static int
227 i40e_vf_representor_vlan_filter_set(struct rte_eth_dev *ethdev,
228                 uint16_t vlan_id, int on)
229 {
230         struct i40e_vf_representor *representor = ethdev->data->dev_private;
231         uint64_t vf_mask = 1ULL << representor->vf_id;
232
233         return rte_pmd_i40e_set_vf_vlan_filter(
234                 representor->adapter->eth_dev->data->port_id,
235                 vlan_id, vf_mask, on);
236 }
237
238 static int
239 i40e_vf_representor_vlan_offload_set(struct rte_eth_dev *ethdev, int mask)
240 {
241         struct i40e_vf_representor *representor = ethdev->data->dev_private;
242         struct rte_eth_dev *pdev;
243         struct i40e_pf_vf *vf;
244         struct i40e_vsi *vsi;
245         struct i40e_pf *pf;
246         uint32_t vfid;
247
248         pdev = representor->adapter->eth_dev;
249         vfid = representor->vf_id;
250
251         if (!is_i40e_supported(pdev)) {
252                 PMD_DRV_LOG(ERR, "Invalid PF dev.");
253                 return -EINVAL;
254         }
255
256         pf = I40E_DEV_PRIVATE_TO_PF(pdev->data->dev_private);
257
258         if (vfid >= pf->vf_num || !pf->vfs) {
259                 PMD_DRV_LOG(ERR, "Invalid VF ID.");
260                 return -EINVAL;
261         }
262
263         vf = &pf->vfs[vfid];
264         vsi = vf->vsi;
265         if (!vsi) {
266                 PMD_DRV_LOG(ERR, "Invalid VSI.");
267                 return -EINVAL;
268         }
269
270         if (mask & ETH_VLAN_FILTER_MASK) {
271                 /* Enable or disable VLAN filtering offload */
272                 if (ethdev->data->dev_conf.rxmode.hw_vlan_filter)
273                         return i40e_vsi_config_vlan_filter(vsi, TRUE);
274                 else
275                         return i40e_vsi_config_vlan_filter(vsi, FALSE);
276         }
277
278         if (mask & ETH_VLAN_STRIP_MASK) {
279                 /* Enable or disable VLAN stripping offload */
280                 if (ethdev->data->dev_conf.rxmode.hw_vlan_strip)
281                         return i40e_vsi_config_vlan_stripping(vsi, TRUE);
282                 else
283                         return i40e_vsi_config_vlan_stripping(vsi, FALSE);
284         }
285
286         return -EINVAL;
287 }
288
289 static void
290 i40e_vf_representor_vlan_strip_queue_set(struct rte_eth_dev *ethdev,
291         __rte_unused uint16_t rx_queue_id, int on)
292 {
293         struct i40e_vf_representor *representor = ethdev->data->dev_private;
294
295         rte_pmd_i40e_set_vf_vlan_stripq(
296                 representor->adapter->eth_dev->data->port_id,
297                 representor->vf_id, on);
298 }
299
300 static int
301 i40e_vf_representor_vlan_pvid_set(struct rte_eth_dev *ethdev, uint16_t vlan_id,
302         __rte_unused int on)
303 {
304         struct i40e_vf_representor *representor = ethdev->data->dev_private;
305
306         return rte_pmd_i40e_set_vf_vlan_insert(
307                 representor->adapter->eth_dev->data->port_id,
308                 representor->vf_id, vlan_id);
309 }
310
311 struct eth_dev_ops i40e_representor_dev_ops = {
312         .dev_infos_get        = i40e_vf_representor_dev_infos_get,
313
314         .dev_start            = i40e_vf_representor_dev_start,
315         .dev_configure        = i40e_vf_representor_dev_configure,
316         .dev_stop             = i40e_vf_representor_dev_stop,
317
318         .rx_queue_setup       = i40e_vf_representor_rx_queue_setup,
319         .tx_queue_setup       = i40e_vf_representor_tx_queue_setup,
320
321         .link_update          = i40e_vf_representor_link_update,
322
323         .stats_get            = i40e_vf_representor_stats_get,
324         .stats_reset          = i40e_vf_representor_stats_reset,
325
326         .promiscuous_enable   = i40e_vf_representor_promiscuous_enable,
327         .promiscuous_disable  = i40e_vf_representor_promiscuous_disable,
328
329         .allmulticast_enable  = i40e_vf_representor_allmulticast_enable,
330         .allmulticast_disable = i40e_vf_representor_allmulticast_disable,
331
332         .mac_addr_remove      = i40e_vf_representor_mac_addr_remove,
333         .mac_addr_set         = i40e_vf_representor_mac_addr_set,
334
335         .vlan_filter_set      = i40e_vf_representor_vlan_filter_set,
336         .vlan_offload_set     = i40e_vf_representor_vlan_offload_set,
337         .vlan_strip_queue_set = i40e_vf_representor_vlan_strip_queue_set,
338         .vlan_pvid_set        = i40e_vf_representor_vlan_pvid_set
339
340 };
341
342 int
343 i40e_vf_representor_init(struct rte_eth_dev *ethdev, void *init_params)
344 {
345         struct i40e_vf_representor *representor = ethdev->data->dev_private;
346
347         struct i40e_pf *pf;
348         struct i40e_pf_vf *vf;
349         struct rte_eth_link *link;
350
351         representor->vf_id =
352                 ((struct i40e_vf_representor *)init_params)->vf_id;
353         representor->switch_domain_id =
354                 ((struct i40e_vf_representor *)init_params)->switch_domain_id;
355         representor->adapter =
356                 ((struct i40e_vf_representor *)init_params)->adapter;
357
358         pf = I40E_DEV_PRIVATE_TO_PF(
359                 representor->adapter->eth_dev->data->dev_private);
360
361         if (representor->vf_id >= pf->vf_num)
362                 return -ENODEV;
363
364         /** representor shares the same driver as it's PF device */
365         ethdev->device->driver = representor->adapter->eth_dev->device->driver;
366
367         /* Set representor device ops */
368         ethdev->dev_ops = &i40e_representor_dev_ops;
369
370         /* No data-path so no RX/TX functions */
371         ethdev->rx_pkt_burst = NULL;
372         ethdev->tx_pkt_burst = NULL;
373
374         vf = &pf->vfs[representor->vf_id];
375
376         if (!vf->vsi) {
377                 PMD_DRV_LOG(ERR, "Invalid VSI.");
378                 return -ENODEV;
379         }
380
381         ethdev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
382
383         /* Setting the number queues allocated to the VF */
384         ethdev->data->nb_rx_queues = vf->vsi->nb_qps;
385         ethdev->data->nb_tx_queues = vf->vsi->nb_qps;
386
387         ethdev->data->mac_addrs = &vf->mac_addr;
388
389         /* Link state. Inherited from PF */
390         link = &representor->adapter->eth_dev->data->dev_link;
391
392         ethdev->data->dev_link.link_speed = link->link_speed;
393         ethdev->data->dev_link.link_duplex = link->link_duplex;
394         ethdev->data->dev_link.link_status = link->link_status;
395         ethdev->data->dev_link.link_autoneg = link->link_autoneg;
396
397         return 0;
398 }
399
400 int
401 i40e_vf_representor_uninit(struct rte_eth_dev *ethdev __rte_unused)
402 {
403         return 0;
404 }