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