net: add rte prefix to ether structures
[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_MULTI_SEGS  |
52                 DEV_TX_OFFLOAD_VLAN_INSERT |
53                 DEV_TX_OFFLOAD_QINQ_INSERT |
54                 DEV_TX_OFFLOAD_IPV4_CKSUM |
55                 DEV_TX_OFFLOAD_UDP_CKSUM |
56                 DEV_TX_OFFLOAD_TCP_CKSUM |
57                 DEV_TX_OFFLOAD_SCTP_CKSUM |
58                 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
59                 DEV_TX_OFFLOAD_TCP_TSO |
60                 DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
61                 DEV_TX_OFFLOAD_GRE_TNL_TSO |
62                 DEV_TX_OFFLOAD_IPIP_TNL_TSO |
63                 DEV_TX_OFFLOAD_GENEVE_TNL_TSO;
64
65         dev_info->default_rxconf = (struct rte_eth_rxconf) {
66                 .rx_thresh = {
67                         .pthresh = I40E_DEFAULT_RX_PTHRESH,
68                         .hthresh = I40E_DEFAULT_RX_HTHRESH,
69                         .wthresh = I40E_DEFAULT_RX_WTHRESH,
70                 },
71                 .rx_free_thresh = I40E_DEFAULT_RX_FREE_THRESH,
72                 .rx_drop_en = 0,
73                 .offloads = 0,
74         };
75
76         dev_info->default_txconf = (struct rte_eth_txconf) {
77                 .tx_thresh = {
78                         .pthresh = I40E_DEFAULT_TX_PTHRESH,
79                         .hthresh = I40E_DEFAULT_TX_HTHRESH,
80                         .wthresh = I40E_DEFAULT_TX_WTHRESH,
81                 },
82                 .tx_free_thresh = I40E_DEFAULT_TX_FREE_THRESH,
83                 .tx_rs_thresh = I40E_DEFAULT_TX_RSBIT_THRESH,
84                 .offloads = 0,
85         };
86
87         dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
88                 .nb_max = I40E_MAX_RING_DESC,
89                 .nb_min = I40E_MIN_RING_DESC,
90                 .nb_align = I40E_ALIGN_RING_DESC,
91         };
92
93         dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
94                 .nb_max = I40E_MAX_RING_DESC,
95                 .nb_min = I40E_MIN_RING_DESC,
96                 .nb_align = I40E_ALIGN_RING_DESC,
97         };
98
99         dev_info->switch_info.name =
100                 representor->adapter->eth_dev->device->name;
101         dev_info->switch_info.domain_id = representor->switch_domain_id;
102         dev_info->switch_info.port_id = representor->vf_id;
103 }
104
105 static int
106 i40e_vf_representor_dev_configure(__rte_unused struct rte_eth_dev *dev)
107 {
108         return 0;
109 }
110
111 static int
112 i40e_vf_representor_dev_start(__rte_unused struct rte_eth_dev *dev)
113 {
114         return 0;
115 }
116
117 static void
118 i40e_vf_representor_dev_stop(__rte_unused struct rte_eth_dev *dev)
119 {
120 }
121
122 static int
123 i40e_vf_representor_rx_queue_setup(__rte_unused struct rte_eth_dev *dev,
124         __rte_unused uint16_t rx_queue_id,
125         __rte_unused uint16_t nb_rx_desc,
126         __rte_unused unsigned int socket_id,
127         __rte_unused const struct rte_eth_rxconf *rx_conf,
128         __rte_unused struct rte_mempool *mb_pool)
129 {
130         return 0;
131 }
132
133 static int
134 i40e_vf_representor_tx_queue_setup(__rte_unused struct rte_eth_dev *dev,
135         __rte_unused uint16_t rx_queue_id,
136         __rte_unused uint16_t nb_rx_desc,
137         __rte_unused unsigned int socket_id,
138         __rte_unused const struct rte_eth_txconf *tx_conf)
139 {
140         return 0;
141 }
142
143 static void
144 i40evf_stat_update_48(uint64_t *offset,
145                    uint64_t *stat)
146 {
147         if (*stat >= *offset)
148                 *stat = *stat - *offset;
149         else
150                 *stat = (uint64_t)((*stat +
151                         ((uint64_t)1 << I40E_48_BIT_WIDTH)) - *offset);
152
153         *stat &= I40E_48_BIT_MASK;
154 }
155
156 static void
157 i40evf_stat_update_32(uint64_t *offset,
158                    uint64_t *stat)
159 {
160         if (*stat >= *offset)
161                 *stat = (uint64_t)(*stat - *offset);
162         else
163                 *stat = (uint64_t)((*stat +
164                         ((uint64_t)1 << I40E_32_BIT_WIDTH)) - *offset);
165 }
166
167 static int
168 rte_pmd_i40e_get_vf_native_stats(uint16_t port,
169                           uint16_t vf_id,
170                           struct i40e_eth_stats *stats)
171 {
172         struct rte_eth_dev *dev;
173         struct i40e_pf *pf;
174         struct i40e_vsi *vsi;
175
176         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
177
178         dev = &rte_eth_devices[port];
179
180         if (!is_i40e_supported(dev))
181                 return -ENOTSUP;
182
183         pf = I40E_DEV_PRIVATE_TO_PF(dev->data->dev_private);
184
185         if (vf_id >= pf->vf_num || !pf->vfs) {
186                 PMD_DRV_LOG(ERR, "Invalid VF ID.");
187                 return -EINVAL;
188         }
189
190         vsi = pf->vfs[vf_id].vsi;
191         if (!vsi) {
192                 PMD_DRV_LOG(ERR, "Invalid VSI.");
193                 return -EINVAL;
194         }
195
196         i40e_update_vsi_stats(vsi);
197         memcpy(stats, &vsi->eth_stats, sizeof(vsi->eth_stats));
198
199         return 0;
200 }
201
202 static int
203 i40e_vf_representor_stats_get(struct rte_eth_dev *ethdev,
204                 struct rte_eth_stats *stats)
205 {
206         struct i40e_vf_representor *representor = ethdev->data->dev_private;
207         struct i40e_eth_stats native_stats;
208         int ret;
209
210         ret = rte_pmd_i40e_get_vf_native_stats(
211                 representor->adapter->eth_dev->data->port_id,
212                 representor->vf_id, &native_stats);
213         if (ret == 0) {
214                 i40evf_stat_update_48(
215                         &representor->stats_offset.rx_bytes,
216                         &native_stats.rx_bytes);
217                 i40evf_stat_update_48(
218                         &representor->stats_offset.rx_unicast,
219                         &native_stats.rx_unicast);
220                 i40evf_stat_update_48(
221                         &representor->stats_offset.rx_multicast,
222                         &native_stats.rx_multicast);
223                 i40evf_stat_update_48(
224                         &representor->stats_offset.rx_broadcast,
225                         &native_stats.rx_broadcast);
226                 i40evf_stat_update_32(
227                         &representor->stats_offset.rx_discards,
228                         &native_stats.rx_discards);
229                 i40evf_stat_update_32(
230                         &representor->stats_offset.rx_unknown_protocol,
231                         &native_stats.rx_unknown_protocol);
232                 i40evf_stat_update_48(
233                         &representor->stats_offset.tx_bytes,
234                         &native_stats.tx_bytes);
235                 i40evf_stat_update_48(
236                         &representor->stats_offset.tx_unicast,
237                         &native_stats.tx_unicast);
238                 i40evf_stat_update_48(
239                         &representor->stats_offset.tx_multicast,
240                         &native_stats.tx_multicast);
241                 i40evf_stat_update_48(
242                         &representor->stats_offset.tx_broadcast,
243                         &native_stats.tx_broadcast);
244                 i40evf_stat_update_32(
245                         &representor->stats_offset.tx_errors,
246                         &native_stats.tx_errors);
247                 i40evf_stat_update_32(
248                         &representor->stats_offset.tx_discards,
249                         &native_stats.tx_discards);
250
251                 stats->ipackets = native_stats.rx_unicast +
252                         native_stats.rx_multicast +
253                         native_stats.rx_broadcast;
254                 stats->opackets = native_stats.tx_unicast +
255                         native_stats.tx_multicast +
256                         native_stats.tx_broadcast;
257                 stats->ibytes   = native_stats.rx_bytes;
258                 stats->obytes   = native_stats.tx_bytes;
259                 stats->ierrors  = native_stats.rx_discards;
260                 stats->oerrors  = native_stats.tx_errors + native_stats.tx_discards;
261         }
262         return ret;
263 }
264
265 static void
266 i40e_vf_representor_stats_reset(struct rte_eth_dev *ethdev)
267 {
268         struct i40e_vf_representor *representor = ethdev->data->dev_private;
269
270         rte_pmd_i40e_get_vf_native_stats(
271                 representor->adapter->eth_dev->data->port_id,
272                 representor->vf_id, &representor->stats_offset);
273 }
274
275 static void
276 i40e_vf_representor_promiscuous_enable(struct rte_eth_dev *ethdev)
277 {
278         struct i40e_vf_representor *representor = ethdev->data->dev_private;
279
280         rte_pmd_i40e_set_vf_unicast_promisc(
281                 representor->adapter->eth_dev->data->port_id,
282                 representor->vf_id, 1);
283 }
284
285 static void
286 i40e_vf_representor_promiscuous_disable(struct rte_eth_dev *ethdev)
287 {
288         struct i40e_vf_representor *representor = ethdev->data->dev_private;
289
290         rte_pmd_i40e_set_vf_unicast_promisc(
291                 representor->adapter->eth_dev->data->port_id,
292                 representor->vf_id, 0);
293 }
294
295 static void
296 i40e_vf_representor_allmulticast_enable(struct rte_eth_dev *ethdev)
297 {
298         struct i40e_vf_representor *representor = ethdev->data->dev_private;
299
300         rte_pmd_i40e_set_vf_multicast_promisc(
301                 representor->adapter->eth_dev->data->port_id,
302                 representor->vf_id,  1);
303 }
304
305 static void
306 i40e_vf_representor_allmulticast_disable(struct rte_eth_dev *ethdev)
307 {
308         struct i40e_vf_representor *representor = ethdev->data->dev_private;
309
310         rte_pmd_i40e_set_vf_multicast_promisc(
311                 representor->adapter->eth_dev->data->port_id,
312                 representor->vf_id,  0);
313 }
314
315 static void
316 i40e_vf_representor_mac_addr_remove(struct rte_eth_dev *ethdev, uint32_t index)
317 {
318         struct i40e_vf_representor *representor = ethdev->data->dev_private;
319
320         rte_pmd_i40e_remove_vf_mac_addr(
321                 representor->adapter->eth_dev->data->port_id,
322                 representor->vf_id, &ethdev->data->mac_addrs[index]);
323 }
324
325 static int
326 i40e_vf_representor_mac_addr_set(struct rte_eth_dev *ethdev,
327                 struct rte_ether_addr *mac_addr)
328 {
329         struct i40e_vf_representor *representor = ethdev->data->dev_private;
330
331         return rte_pmd_i40e_set_vf_mac_addr(
332                 representor->adapter->eth_dev->data->port_id,
333                 representor->vf_id, mac_addr);
334 }
335
336 static int
337 i40e_vf_representor_vlan_filter_set(struct rte_eth_dev *ethdev,
338                 uint16_t vlan_id, int on)
339 {
340         struct i40e_vf_representor *representor = ethdev->data->dev_private;
341         uint64_t vf_mask = 1ULL << representor->vf_id;
342
343         return rte_pmd_i40e_set_vf_vlan_filter(
344                 representor->adapter->eth_dev->data->port_id,
345                 vlan_id, vf_mask, on);
346 }
347
348 static int
349 i40e_vf_representor_vlan_offload_set(struct rte_eth_dev *ethdev, int mask)
350 {
351         struct i40e_vf_representor *representor = ethdev->data->dev_private;
352         struct rte_eth_dev *pdev;
353         struct i40e_pf_vf *vf;
354         struct i40e_vsi *vsi;
355         struct i40e_pf *pf;
356         uint32_t vfid;
357
358         pdev = representor->adapter->eth_dev;
359         vfid = representor->vf_id;
360
361         if (!is_i40e_supported(pdev)) {
362                 PMD_DRV_LOG(ERR, "Invalid PF dev.");
363                 return -EINVAL;
364         }
365
366         pf = I40E_DEV_PRIVATE_TO_PF(pdev->data->dev_private);
367
368         if (vfid >= pf->vf_num || !pf->vfs) {
369                 PMD_DRV_LOG(ERR, "Invalid VF ID.");
370                 return -EINVAL;
371         }
372
373         vf = &pf->vfs[vfid];
374         vsi = vf->vsi;
375         if (!vsi) {
376                 PMD_DRV_LOG(ERR, "Invalid VSI.");
377                 return -EINVAL;
378         }
379
380         if (mask & ETH_VLAN_FILTER_MASK) {
381                 /* Enable or disable VLAN filtering offload */
382                 if (ethdev->data->dev_conf.rxmode.offloads &
383                     DEV_RX_OFFLOAD_VLAN_FILTER)
384                         return i40e_vsi_config_vlan_filter(vsi, TRUE);
385                 else
386                         return i40e_vsi_config_vlan_filter(vsi, FALSE);
387         }
388
389         if (mask & ETH_VLAN_STRIP_MASK) {
390                 /* Enable or disable VLAN stripping offload */
391                 if (ethdev->data->dev_conf.rxmode.offloads &
392                     DEV_RX_OFFLOAD_VLAN_STRIP)
393                         return i40e_vsi_config_vlan_stripping(vsi, TRUE);
394                 else
395                         return i40e_vsi_config_vlan_stripping(vsi, FALSE);
396         }
397
398         return -EINVAL;
399 }
400
401 static void
402 i40e_vf_representor_vlan_strip_queue_set(struct rte_eth_dev *ethdev,
403         __rte_unused uint16_t rx_queue_id, int on)
404 {
405         struct i40e_vf_representor *representor = ethdev->data->dev_private;
406
407         rte_pmd_i40e_set_vf_vlan_stripq(
408                 representor->adapter->eth_dev->data->port_id,
409                 representor->vf_id, on);
410 }
411
412 static int
413 i40e_vf_representor_vlan_pvid_set(struct rte_eth_dev *ethdev, uint16_t vlan_id,
414         __rte_unused int on)
415 {
416         struct i40e_vf_representor *representor = ethdev->data->dev_private;
417
418         return rte_pmd_i40e_set_vf_vlan_insert(
419                 representor->adapter->eth_dev->data->port_id,
420                 representor->vf_id, vlan_id);
421 }
422
423 static const struct eth_dev_ops i40e_representor_dev_ops = {
424         .dev_infos_get        = i40e_vf_representor_dev_infos_get,
425
426         .dev_start            = i40e_vf_representor_dev_start,
427         .dev_configure        = i40e_vf_representor_dev_configure,
428         .dev_stop             = i40e_vf_representor_dev_stop,
429
430         .rx_queue_setup       = i40e_vf_representor_rx_queue_setup,
431         .tx_queue_setup       = i40e_vf_representor_tx_queue_setup,
432
433         .link_update          = i40e_vf_representor_link_update,
434
435         .stats_get            = i40e_vf_representor_stats_get,
436         .stats_reset          = i40e_vf_representor_stats_reset,
437
438         .promiscuous_enable   = i40e_vf_representor_promiscuous_enable,
439         .promiscuous_disable  = i40e_vf_representor_promiscuous_disable,
440
441         .allmulticast_enable  = i40e_vf_representor_allmulticast_enable,
442         .allmulticast_disable = i40e_vf_representor_allmulticast_disable,
443
444         .mac_addr_remove      = i40e_vf_representor_mac_addr_remove,
445         .mac_addr_set         = i40e_vf_representor_mac_addr_set,
446
447         .vlan_filter_set      = i40e_vf_representor_vlan_filter_set,
448         .vlan_offload_set     = i40e_vf_representor_vlan_offload_set,
449         .vlan_strip_queue_set = i40e_vf_representor_vlan_strip_queue_set,
450         .vlan_pvid_set        = i40e_vf_representor_vlan_pvid_set
451
452 };
453
454 static uint16_t
455 i40e_vf_representor_rx_burst(__rte_unused void *rx_queue,
456         __rte_unused struct rte_mbuf **rx_pkts, __rte_unused uint16_t nb_pkts)
457 {
458         return 0;
459 }
460
461 static uint16_t
462 i40e_vf_representor_tx_burst(__rte_unused void *tx_queue,
463         __rte_unused struct rte_mbuf **tx_pkts, __rte_unused uint16_t nb_pkts)
464 {
465         return 0;
466 }
467
468 int
469 i40e_vf_representor_init(struct rte_eth_dev *ethdev, void *init_params)
470 {
471         struct i40e_vf_representor *representor = ethdev->data->dev_private;
472
473         struct i40e_pf *pf;
474         struct i40e_pf_vf *vf;
475         struct rte_eth_link *link;
476
477         representor->vf_id =
478                 ((struct i40e_vf_representor *)init_params)->vf_id;
479         representor->switch_domain_id =
480                 ((struct i40e_vf_representor *)init_params)->switch_domain_id;
481         representor->adapter =
482                 ((struct i40e_vf_representor *)init_params)->adapter;
483
484         pf = I40E_DEV_PRIVATE_TO_PF(
485                 representor->adapter->eth_dev->data->dev_private);
486
487         if (representor->vf_id >= pf->vf_num)
488                 return -ENODEV;
489
490         /* Set representor device ops */
491         ethdev->dev_ops = &i40e_representor_dev_ops;
492
493         /* No data-path, but need stub Rx/Tx functions to avoid crash
494          * when testing with the likes of testpmd.
495          */
496         ethdev->rx_pkt_burst = i40e_vf_representor_rx_burst;
497         ethdev->tx_pkt_burst = i40e_vf_representor_tx_burst;
498
499         vf = &pf->vfs[representor->vf_id];
500
501         if (!vf->vsi) {
502                 PMD_DRV_LOG(ERR, "Invalid VSI.");
503                 return -ENODEV;
504         }
505
506         ethdev->data->dev_flags |= RTE_ETH_DEV_REPRESENTOR;
507         ethdev->data->representor_id = representor->vf_id;
508
509         /* Setting the number queues allocated to the VF */
510         ethdev->data->nb_rx_queues = vf->vsi->nb_qps;
511         ethdev->data->nb_tx_queues = vf->vsi->nb_qps;
512
513         ethdev->data->mac_addrs = &vf->mac_addr;
514
515         /* Link state. Inherited from PF */
516         link = &representor->adapter->eth_dev->data->dev_link;
517
518         ethdev->data->dev_link.link_speed = link->link_speed;
519         ethdev->data->dev_link.link_duplex = link->link_duplex;
520         ethdev->data->dev_link.link_status = link->link_status;
521         ethdev->data->dev_link.link_autoneg = link->link_autoneg;
522
523         return 0;
524 }
525
526 int
527 i40e_vf_representor_uninit(struct rte_eth_dev *ethdev)
528 {
529         /* mac_addrs must not be freed because part of i40e_pf_vf */
530         ethdev->data->mac_addrs = NULL;
531
532         return 0;
533 }