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