net: add rte prefix to ether structures
[dpdk.git] / drivers / net / bnxt / rte_pmd_bnxt.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017-2018 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7 #include <stdbool.h>
8 #include <unistd.h>
9
10 #include <rte_dev.h>
11 #include <rte_ethdev_driver.h>
12 #include <rte_malloc.h>
13 #include <rte_cycles.h>
14 #include <rte_byteorder.h>
15
16 #include "bnxt.h"
17 #include "bnxt_filter.h"
18 #include "bnxt_hwrm.h"
19 #include "bnxt_vnic.h"
20 #include "rte_pmd_bnxt.h"
21 #include "hsi_struct_def_dpdk.h"
22
23 int bnxt_rcv_msg_from_vf(struct bnxt *bp, uint16_t vf_id, void *msg)
24 {
25         struct rte_pmd_bnxt_mb_event_param ret_param;
26
27         ret_param.retval = RTE_PMD_BNXT_MB_EVENT_PROCEED;
28         ret_param.vf_id = vf_id;
29         ret_param.msg = msg;
30
31         _rte_eth_dev_callback_process(bp->eth_dev, RTE_ETH_EVENT_VF_MBOX,
32                                       &ret_param);
33
34         /* Default to approve */
35         if (ret_param.retval == RTE_PMD_BNXT_MB_EVENT_PROCEED)
36                 ret_param.retval = RTE_PMD_BNXT_MB_EVENT_NOOP_ACK;
37
38         return ret_param.retval == RTE_PMD_BNXT_MB_EVENT_NOOP_ACK ?
39                 true : false;
40 }
41
42 int rte_pmd_bnxt_set_tx_loopback(uint16_t port, uint8_t on)
43 {
44         struct rte_eth_dev *eth_dev;
45         struct bnxt *bp;
46         int rc;
47
48         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
49
50         if (on > 1)
51                 return -EINVAL;
52
53         eth_dev = &rte_eth_devices[port];
54         if (!is_bnxt_supported(eth_dev))
55                 return -ENOTSUP;
56
57         bp = (struct bnxt *)eth_dev->data->dev_private;
58
59         if (!BNXT_PF(bp)) {
60                 PMD_DRV_LOG(ERR,
61                         "Attempt to set Tx loopback on non-PF port %d!\n",
62                         port);
63                 return -ENOTSUP;
64         }
65
66         if (on)
67                 bp->pf.evb_mode = BNXT_EVB_MODE_VEB;
68         else
69                 bp->pf.evb_mode = BNXT_EVB_MODE_VEPA;
70
71         rc = bnxt_hwrm_pf_evb_mode(bp);
72
73         return rc;
74 }
75
76 static void
77 rte_pmd_bnxt_set_all_queues_drop_en_cb(struct bnxt_vnic_info *vnic, void *onptr)
78 {
79         uint8_t *on = onptr;
80         vnic->bd_stall = !(*on);
81 }
82
83 int rte_pmd_bnxt_set_all_queues_drop_en(uint16_t port, uint8_t on)
84 {
85         struct rte_eth_dev *eth_dev;
86         struct bnxt *bp;
87         uint32_t i;
88         int rc = -EINVAL;
89
90         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
91
92         if (on > 1)
93                 return -EINVAL;
94
95         eth_dev = &rte_eth_devices[port];
96         if (!is_bnxt_supported(eth_dev))
97                 return -ENOTSUP;
98
99         bp = (struct bnxt *)eth_dev->data->dev_private;
100
101         if (!BNXT_PF(bp)) {
102                 PMD_DRV_LOG(ERR,
103                         "Attempt to set all queues drop on non-PF port!\n");
104                 return -ENOTSUP;
105         }
106
107         if (bp->vnic_info == NULL)
108                 return -ENODEV;
109
110         /* Stall PF */
111         for (i = 0; i < bp->nr_vnics; i++) {
112                 bp->vnic_info[i].bd_stall = !on;
113                 rc = bnxt_hwrm_vnic_cfg(bp, &bp->vnic_info[i]);
114                 if (rc) {
115                         PMD_DRV_LOG(ERR, "Failed to update PF VNIC %d.\n", i);
116                         return rc;
117                 }
118         }
119
120         /* Stall all active VFs */
121         for (i = 0; i < bp->pf.active_vfs; i++) {
122                 rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, i,
123                                 rte_pmd_bnxt_set_all_queues_drop_en_cb, &on,
124                                 bnxt_hwrm_vnic_cfg);
125                 if (rc) {
126                         PMD_DRV_LOG(ERR, "Failed to update VF VNIC %d.\n", i);
127                         break;
128                 }
129         }
130
131         return rc;
132 }
133
134 int rte_pmd_bnxt_set_vf_mac_addr(uint16_t port, uint16_t vf,
135                                 struct rte_ether_addr *mac_addr)
136 {
137         struct rte_eth_dev *dev;
138         struct rte_eth_dev_info dev_info;
139         struct bnxt *bp;
140         int rc;
141
142         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
143
144         dev = &rte_eth_devices[port];
145         if (!is_bnxt_supported(dev))
146                 return -ENOTSUP;
147
148         rte_eth_dev_info_get(port, &dev_info);
149         bp = (struct bnxt *)dev->data->dev_private;
150
151         if (vf >= dev_info.max_vfs || mac_addr == NULL)
152                 return -EINVAL;
153
154         if (!BNXT_PF(bp)) {
155                 PMD_DRV_LOG(ERR,
156                         "Attempt to set VF %d mac address on non-PF port %d!\n",
157                         vf, port);
158                 return -ENOTSUP;
159         }
160
161         rc = bnxt_hwrm_func_vf_mac(bp, vf, (uint8_t *)mac_addr);
162
163         return rc;
164 }
165
166 int rte_pmd_bnxt_set_vf_rate_limit(uint16_t port, uint16_t vf,
167                                 uint16_t tx_rate, uint64_t q_msk)
168 {
169         struct rte_eth_dev *eth_dev;
170         struct rte_eth_dev_info dev_info;
171         struct bnxt *bp;
172         uint16_t tot_rate = 0;
173         uint64_t idx;
174         int rc;
175
176         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
177
178         eth_dev = &rte_eth_devices[port];
179         if (!is_bnxt_supported(eth_dev))
180                 return -ENOTSUP;
181
182         rte_eth_dev_info_get(port, &dev_info);
183         bp = (struct bnxt *)eth_dev->data->dev_private;
184
185         if (!bp->pf.active_vfs)
186                 return -EINVAL;
187
188         if (vf >= bp->pf.max_vfs)
189                 return -EINVAL;
190
191         /* Add up the per queue BW and configure MAX BW of the VF */
192         for (idx = 0; idx < 64; idx++) {
193                 if ((1ULL << idx) & q_msk)
194                         tot_rate += tx_rate;
195         }
196
197         /* Requested BW can't be greater than link speed */
198         if (tot_rate > eth_dev->data->dev_link.link_speed) {
199                 PMD_DRV_LOG(ERR, "Rate > Link speed. Set to %d\n", tot_rate);
200                 return -EINVAL;
201         }
202
203         /* Requested BW already configured */
204         if (tot_rate == bp->pf.vf_info[vf].max_tx_rate)
205                 return 0;
206
207         rc = bnxt_hwrm_func_bw_cfg(bp, vf, tot_rate,
208                                 HWRM_FUNC_CFG_INPUT_ENABLES_MAX_BW);
209
210         if (!rc)
211                 bp->pf.vf_info[vf].max_tx_rate = tot_rate;
212
213         return rc;
214 }
215
216 int rte_pmd_bnxt_set_vf_mac_anti_spoof(uint16_t port, uint16_t vf, uint8_t on)
217 {
218         struct rte_eth_dev_info dev_info;
219         struct rte_eth_dev *dev;
220         uint32_t func_flags;
221         struct bnxt *bp;
222         int rc;
223
224         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
225
226         if (on > 1)
227                 return -EINVAL;
228
229         dev = &rte_eth_devices[port];
230         if (!is_bnxt_supported(dev))
231                 return -ENOTSUP;
232
233         rte_eth_dev_info_get(port, &dev_info);
234         bp = (struct bnxt *)dev->data->dev_private;
235
236         if (!BNXT_PF(bp)) {
237                 PMD_DRV_LOG(ERR,
238                         "Attempt to set mac spoof on non-PF port %d!\n", port);
239                 return -EINVAL;
240         }
241
242         if (vf >= dev_info.max_vfs)
243                 return -EINVAL;
244
245         /* Prev setting same as new setting. */
246         if (on == bp->pf.vf_info[vf].mac_spoof_en)
247                 return 0;
248
249         func_flags = bp->pf.vf_info[vf].func_cfg_flags;
250         func_flags &= ~(HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE |
251             HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE);
252
253         if (on)
254                 func_flags |=
255                         HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE;
256         else
257                 func_flags |=
258                         HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE;
259
260         rc = bnxt_hwrm_func_cfg_vf_set_flags(bp, vf, func_flags);
261         if (!rc) {
262                 bp->pf.vf_info[vf].mac_spoof_en = on;
263                 bp->pf.vf_info[vf].func_cfg_flags = func_flags;
264         }
265
266         return rc;
267 }
268
269 int rte_pmd_bnxt_set_vf_vlan_anti_spoof(uint16_t port, uint16_t vf, uint8_t on)
270 {
271         struct rte_eth_dev_info dev_info;
272         struct rte_eth_dev *dev;
273         struct bnxt *bp;
274         int rc;
275
276         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
277
278         if (on > 1)
279                 return -EINVAL;
280
281         dev = &rte_eth_devices[port];
282         if (!is_bnxt_supported(dev))
283                 return -ENOTSUP;
284
285         rte_eth_dev_info_get(port, &dev_info);
286         bp = (struct bnxt *)dev->data->dev_private;
287
288         if (!BNXT_PF(bp)) {
289                 PMD_DRV_LOG(ERR,
290                         "Attempt to set VLAN spoof on non-PF port %d!\n", port);
291                 return -EINVAL;
292         }
293
294         if (vf >= dev_info.max_vfs)
295                 return -EINVAL;
296
297         rc = bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(bp, vf, on);
298         if (!rc) {
299                 bp->pf.vf_info[vf].vlan_spoof_en = on;
300                 if (on) {
301                         if (bnxt_hwrm_cfa_vlan_antispoof_cfg(bp,
302                                 bp->pf.first_vf_id + vf,
303                                 bp->pf.vf_info[vf].vlan_count,
304                                 bp->pf.vf_info[vf].vlan_as_table))
305                                 rc = -1;
306                 }
307         } else {
308                 PMD_DRV_LOG(ERR, "Failed to update VF VNIC %d.\n", vf);
309         }
310
311         return rc;
312 }
313
314 static void
315 rte_pmd_bnxt_set_vf_vlan_stripq_cb(struct bnxt_vnic_info *vnic, void *onptr)
316 {
317         uint8_t *on = onptr;
318         vnic->vlan_strip = *on;
319 }
320
321 int
322 rte_pmd_bnxt_set_vf_vlan_stripq(uint16_t port, uint16_t vf, uint8_t on)
323 {
324         struct rte_eth_dev *dev;
325         struct rte_eth_dev_info dev_info;
326         struct bnxt *bp;
327         int rc;
328
329         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
330
331         dev = &rte_eth_devices[port];
332         if (!is_bnxt_supported(dev))
333                 return -ENOTSUP;
334
335         rte_eth_dev_info_get(port, &dev_info);
336         bp = (struct bnxt *)dev->data->dev_private;
337
338         if (vf >= dev_info.max_vfs)
339                 return -EINVAL;
340
341         if (!BNXT_PF(bp)) {
342                 PMD_DRV_LOG(ERR,
343                         "Attempt to set VF %d stripq on non-PF port %d!\n",
344                         vf, port);
345                 return -ENOTSUP;
346         }
347
348         rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf,
349                                 rte_pmd_bnxt_set_vf_vlan_stripq_cb, &on,
350                                 bnxt_hwrm_vnic_cfg);
351         if (rc)
352                 PMD_DRV_LOG(ERR, "Failed to update VF VNIC %d.\n", vf);
353
354         return rc;
355 }
356
357 int rte_pmd_bnxt_set_vf_rxmode(uint16_t port, uint16_t vf,
358                                 uint16_t rx_mask, uint8_t on)
359 {
360         struct rte_eth_dev *dev;
361         struct rte_eth_dev_info dev_info;
362         uint16_t flag = 0;
363         struct bnxt *bp;
364         int rc;
365
366         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
367
368         dev = &rte_eth_devices[port];
369         if (!is_bnxt_supported(dev))
370                 return -ENOTSUP;
371
372         rte_eth_dev_info_get(port, &dev_info);
373         bp = (struct bnxt *)dev->data->dev_private;
374
375         if (!bp->pf.vf_info)
376                 return -EINVAL;
377
378         if (vf >= bp->pdev->max_vfs)
379                 return -EINVAL;
380
381         if (rx_mask & ETH_VMDQ_ACCEPT_UNTAG) {
382                 PMD_DRV_LOG(ERR, "Currently cannot toggle this setting\n");
383                 return -ENOTSUP;
384         }
385
386         /* Is this really the correct mapping?  VFd seems to think it is. */
387         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC)
388                 flag |= BNXT_VNIC_INFO_PROMISC;
389
390         if (rx_mask & ETH_VMDQ_ACCEPT_BROADCAST)
391                 flag |= BNXT_VNIC_INFO_BCAST;
392         if (rx_mask & ETH_VMDQ_ACCEPT_MULTICAST)
393                 flag |= BNXT_VNIC_INFO_ALLMULTI | BNXT_VNIC_INFO_MCAST;
394
395         if (on)
396                 bp->pf.vf_info[vf].l2_rx_mask |= flag;
397         else
398                 bp->pf.vf_info[vf].l2_rx_mask &= ~flag;
399
400         rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf,
401                                         vf_vnic_set_rxmask_cb,
402                                         &bp->pf.vf_info[vf].l2_rx_mask,
403                                         bnxt_set_rx_mask_no_vlan);
404         if (rc)
405                 PMD_DRV_LOG(ERR, "bnxt_hwrm_func_vf_vnic_set_rxmask failed\n");
406
407         return rc;
408 }
409
410 static int bnxt_set_vf_table(struct bnxt *bp, uint16_t vf)
411 {
412         int rc = 0;
413         int dflt_vnic;
414         struct bnxt_vnic_info vnic;
415
416         if (!BNXT_PF(bp)) {
417                 PMD_DRV_LOG(ERR,
418                         "Attempt to set VLAN table on non-PF port!\n");
419                 return -EINVAL;
420         }
421
422         if (vf >= bp->pdev->max_vfs)
423                 return -EINVAL;
424
425         dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf);
426         if (dflt_vnic < 0) {
427                 /* This simply indicates there's no driver loaded.
428                  * This is not an error.
429                  */
430                 PMD_DRV_LOG(ERR, "Unable to get default VNIC for VF %d\n", vf);
431         } else {
432                 memset(&vnic, 0, sizeof(vnic));
433                 vnic.fw_vnic_id = dflt_vnic;
434                 if (bnxt_hwrm_vnic_qcfg(bp, &vnic,
435                                         bp->pf.first_vf_id + vf) == 0) {
436                         if (bnxt_hwrm_cfa_l2_set_rx_mask(bp, &vnic,
437                                                 bp->pf.vf_info[vf].vlan_count,
438                                                 bp->pf.vf_info[vf].vlan_table))
439                                 rc = -1;
440                 } else {
441                         rc = -1;
442                 }
443         }
444
445         return rc;
446 }
447
448 int rte_pmd_bnxt_set_vf_vlan_filter(uint16_t port, uint16_t vlan,
449                                     uint64_t vf_mask, uint8_t vlan_on)
450 {
451         struct bnxt_vlan_table_entry *ve;
452         struct bnxt_vlan_antispoof_table_entry *vase;
453         struct rte_eth_dev *dev;
454         struct bnxt *bp;
455         uint16_t cnt;
456         int rc = 0;
457         int i, j;
458
459         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
460
461         dev = &rte_eth_devices[port];
462         if (!is_bnxt_supported(dev))
463                 return -ENOTSUP;
464
465         bp = (struct bnxt *)dev->data->dev_private;
466         if (!bp->pf.vf_info)
467                 return -EINVAL;
468
469         for (i = 0; vf_mask; i++, vf_mask >>= 1) {
470                 cnt = bp->pf.vf_info[i].vlan_count;
471                 if ((vf_mask & 1)  == 0)
472                         continue;
473
474                 if (bp->pf.vf_info[i].vlan_table == NULL) {
475                         rc = -1;
476                         continue;
477                 }
478                 if (bp->pf.vf_info[i].vlan_as_table == NULL) {
479                         rc = -1;
480                         continue;
481                 }
482                 if (vlan_on) {
483                         /* First, search for a duplicate... */
484                         for (j = 0; j < cnt; j++) {
485                                 if (rte_be_to_cpu_16(
486                                    bp->pf.vf_info[i].vlan_table[j].vid) == vlan)
487                                         break;
488                         }
489                         if (j == cnt) {
490                                 /* Now check that there's space */
491                                 if (cnt == getpagesize() / sizeof(struct
492                                     bnxt_vlan_antispoof_table_entry)) {
493                                         PMD_DRV_LOG(ERR,
494                                              "VLAN anti-spoof table is full\n");
495                                         PMD_DRV_LOG(ERR,
496                                                 "VF %d cannot add VLAN %u\n",
497                                                 i, vlan);
498                                         rc = -1;
499                                         continue;
500                                 }
501
502                                 /* cnt is one less than vlan_count */
503                                 cnt = bp->pf.vf_info[i].vlan_count++;
504                                 /*
505                                  * And finally, add to the
506                                  * end of the table
507                                  */
508                                 vase = &bp->pf.vf_info[i].vlan_as_table[cnt];
509                                 // TODO: Hardcoded TPID
510                                 vase->tpid = rte_cpu_to_be_16(0x8100);
511                                 vase->vid = rte_cpu_to_be_16(vlan);
512                                 vase->mask = rte_cpu_to_be_16(0xfff);
513                                 ve = &bp->pf.vf_info[i].vlan_table[cnt];
514                                 /* TODO: Hardcoded TPID */
515                                 ve->tpid = rte_cpu_to_be_16(0x8100);
516                                 ve->vid = rte_cpu_to_be_16(vlan);
517                         }
518                 } else {
519                         for (j = 0; j < cnt; j++) {
520                                 if (rte_be_to_cpu_16(
521                                    bp->pf.vf_info[i].vlan_table[j].vid) != vlan)
522                                         continue;
523                                 memmove(&bp->pf.vf_info[i].vlan_table[j],
524                                         &bp->pf.vf_info[i].vlan_table[j + 1],
525                                         getpagesize() - ((j + 1) *
526                                         sizeof(struct bnxt_vlan_table_entry)));
527                                 memmove(&bp->pf.vf_info[i].vlan_as_table[j],
528                                         &bp->pf.vf_info[i].vlan_as_table[j + 1],
529                                         getpagesize() - ((j + 1) * sizeof(struct
530                                         bnxt_vlan_antispoof_table_entry)));
531                                 j--;
532                                 cnt = --bp->pf.vf_info[i].vlan_count;
533                         }
534                 }
535                 bnxt_set_vf_table(bp, i);
536         }
537
538         return rc;
539 }
540
541 int rte_pmd_bnxt_get_vf_stats(uint16_t port,
542                               uint16_t vf_id,
543                               struct rte_eth_stats *stats)
544 {
545         struct rte_eth_dev *dev;
546         struct rte_eth_dev_info dev_info;
547         struct bnxt *bp;
548
549         dev = &rte_eth_devices[port];
550         if (!is_bnxt_supported(dev))
551                 return -ENOTSUP;
552
553         rte_eth_dev_info_get(port, &dev_info);
554         bp = (struct bnxt *)dev->data->dev_private;
555
556         if (vf_id >= dev_info.max_vfs)
557                 return -EINVAL;
558
559         if (!BNXT_PF(bp)) {
560                 PMD_DRV_LOG(ERR,
561                         "Attempt to get VF %d stats on non-PF port %d!\n",
562                         vf_id, port);
563                 return -ENOTSUP;
564         }
565
566         return bnxt_hwrm_func_qstats(bp, bp->pf.first_vf_id + vf_id, stats);
567 }
568
569 int rte_pmd_bnxt_reset_vf_stats(uint16_t port,
570                                 uint16_t vf_id)
571 {
572         struct rte_eth_dev *dev;
573         struct rte_eth_dev_info dev_info;
574         struct bnxt *bp;
575
576         dev = &rte_eth_devices[port];
577         if (!is_bnxt_supported(dev))
578                 return -ENOTSUP;
579
580         rte_eth_dev_info_get(port, &dev_info);
581         bp = (struct bnxt *)dev->data->dev_private;
582
583         if (vf_id >= dev_info.max_vfs)
584                 return -EINVAL;
585
586         if (!BNXT_PF(bp)) {
587                 PMD_DRV_LOG(ERR,
588                         "Attempt to reset VF %d stats on non-PF port %d!\n",
589                         vf_id, port);
590                 return -ENOTSUP;
591         }
592
593         return bnxt_hwrm_func_clr_stats(bp, bp->pf.first_vf_id + vf_id);
594 }
595
596 int rte_pmd_bnxt_get_vf_rx_status(uint16_t port, uint16_t vf_id)
597 {
598         struct rte_eth_dev *dev;
599         struct rte_eth_dev_info dev_info;
600         struct bnxt *bp;
601
602         dev = &rte_eth_devices[port];
603         if (!is_bnxt_supported(dev))
604                 return -ENOTSUP;
605
606         rte_eth_dev_info_get(port, &dev_info);
607         bp = (struct bnxt *)dev->data->dev_private;
608
609         if (vf_id >= dev_info.max_vfs)
610                 return -EINVAL;
611
612         if (!BNXT_PF(bp)) {
613                 PMD_DRV_LOG(ERR,
614                         "Attempt to query VF %d RX stats on non-PF port %d!\n",
615                         vf_id, port);
616                 return -ENOTSUP;
617         }
618
619         return bnxt_vf_vnic_count(bp, vf_id);
620 }
621
622 int rte_pmd_bnxt_get_vf_tx_drop_count(uint16_t port, uint16_t vf_id,
623                                       uint64_t *count)
624 {
625         struct rte_eth_dev *dev;
626         struct rte_eth_dev_info dev_info;
627         struct bnxt *bp;
628
629         dev = &rte_eth_devices[port];
630         if (!is_bnxt_supported(dev))
631                 return -ENOTSUP;
632
633         rte_eth_dev_info_get(port, &dev_info);
634         bp = (struct bnxt *)dev->data->dev_private;
635
636         if (vf_id >= dev_info.max_vfs)
637                 return -EINVAL;
638
639         if (!BNXT_PF(bp)) {
640                 PMD_DRV_LOG(ERR,
641                         "Attempt to query VF %d TX drops on non-PF port %d!\n",
642                         vf_id, port);
643                 return -ENOTSUP;
644         }
645
646         return bnxt_hwrm_func_qstats_tx_drop(bp, bp->pf.first_vf_id + vf_id,
647                                              count);
648 }
649
650 int rte_pmd_bnxt_mac_addr_add(uint16_t port, struct rte_ether_addr *addr,
651                                 uint32_t vf_id)
652 {
653         struct rte_eth_dev *dev;
654         struct rte_eth_dev_info dev_info;
655         struct bnxt *bp;
656         struct bnxt_filter_info *filter;
657         struct bnxt_vnic_info vnic;
658         struct rte_ether_addr dflt_mac;
659         int rc;
660
661         dev = &rte_eth_devices[port];
662         if (!is_bnxt_supported(dev))
663                 return -ENOTSUP;
664
665         rte_eth_dev_info_get(port, &dev_info);
666         bp = (struct bnxt *)dev->data->dev_private;
667
668         if (vf_id >= dev_info.max_vfs)
669                 return -EINVAL;
670
671         if (!BNXT_PF(bp)) {
672                 PMD_DRV_LOG(ERR,
673                         "Attempt to config VF %d MAC on non-PF port %d!\n",
674                         vf_id, port);
675                 return -ENOTSUP;
676         }
677
678         /* If the VF currently uses a random MAC, update default to this one */
679         if (bp->pf.vf_info[vf_id].random_mac) {
680                 if (rte_pmd_bnxt_get_vf_rx_status(port, vf_id) <= 0)
681                         bnxt_hwrm_func_vf_mac(bp, vf_id, (uint8_t *)addr);
682         }
683
684         /* query the default VNIC id used by the function */
685         rc = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf_id);
686         if (rc < 0)
687                 goto exit;
688
689         memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
690         vnic.fw_vnic_id = rte_le_to_cpu_16(rc);
691         rc = bnxt_hwrm_vnic_qcfg(bp, &vnic, bp->pf.first_vf_id + vf_id);
692         if (rc < 0)
693                 goto exit;
694
695         STAILQ_FOREACH(filter, &bp->pf.vf_info[vf_id].filter, next) {
696                 if (filter->flags ==
697                     HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX &&
698                     filter->enables ==
699                     (HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
700                      HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK) &&
701                     memcmp(addr, filter->l2_addr, ETHER_ADDR_LEN) == 0) {
702                         bnxt_hwrm_clear_l2_filter(bp, filter);
703                         break;
704                 }
705         }
706
707         if (filter == NULL)
708                 filter = bnxt_alloc_vf_filter(bp, vf_id);
709
710         filter->fw_l2_filter_id = UINT64_MAX;
711         filter->flags = HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX;
712         filter->enables = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
713                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK;
714         memcpy(filter->l2_addr, addr, ETHER_ADDR_LEN);
715         memset(filter->l2_addr_mask, 0xff, ETHER_ADDR_LEN);
716
717         /* Do not add a filter for the default MAC */
718         if (bnxt_hwrm_func_qcfg_vf_default_mac(bp, vf_id, &dflt_mac) ||
719             memcmp(filter->l2_addr, dflt_mac.addr_bytes, ETHER_ADDR_LEN))
720                 rc = bnxt_hwrm_set_l2_filter(bp, vnic.fw_vnic_id, filter);
721
722 exit:
723         return rc;
724 }
725
726 int
727 rte_pmd_bnxt_set_vf_vlan_insert(uint16_t port, uint16_t vf,
728                 uint16_t vlan_id)
729 {
730         struct rte_eth_dev *dev;
731         struct rte_eth_dev_info dev_info;
732         struct bnxt *bp;
733         int rc;
734
735         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
736
737         dev = &rte_eth_devices[port];
738         if (!is_bnxt_supported(dev))
739                 return -ENOTSUP;
740
741         rte_eth_dev_info_get(port, &dev_info);
742         bp = (struct bnxt *)dev->data->dev_private;
743
744         if (vf >= dev_info.max_vfs)
745                 return -EINVAL;
746
747         if (!BNXT_PF(bp)) {
748                 PMD_DRV_LOG(ERR,
749                         "Attempt to set VF %d vlan insert on non-PF port %d!\n",
750                         vf, port);
751                 return -ENOTSUP;
752         }
753
754         bp->pf.vf_info[vf].dflt_vlan = vlan_id;
755         if (bnxt_hwrm_func_qcfg_current_vf_vlan(bp, vf) ==
756             bp->pf.vf_info[vf].dflt_vlan)
757                 return 0;
758
759         rc = bnxt_hwrm_set_vf_vlan(bp, vf);
760
761         return rc;
762 }
763
764 int rte_pmd_bnxt_set_vf_persist_stats(uint16_t port, uint16_t vf, uint8_t on)
765 {
766         struct rte_eth_dev_info dev_info;
767         struct rte_eth_dev *dev;
768         uint32_t func_flags;
769         struct bnxt *bp;
770         int rc;
771
772         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
773
774         if (on > 1)
775                 return -EINVAL;
776
777         dev = &rte_eth_devices[port];
778         rte_eth_dev_info_get(port, &dev_info);
779         bp = (struct bnxt *)dev->data->dev_private;
780
781         if (!BNXT_PF(bp)) {
782                 PMD_DRV_LOG(ERR,
783                         "Attempt to set persist stats on non-PF port %d!\n",
784                         port);
785                 return -EINVAL;
786         }
787
788         if (vf >= dev_info.max_vfs)
789                 return -EINVAL;
790
791         /* Prev setting same as new setting. */
792         if (on == bp->pf.vf_info[vf].persist_stats)
793                 return 0;
794
795         func_flags = bp->pf.vf_info[vf].func_cfg_flags;
796
797         if (on)
798                 func_flags |=
799                         HWRM_FUNC_CFG_INPUT_FLAGS_NO_AUTOCLEAR_STATISTIC;
800         else
801                 func_flags &=
802                         ~HWRM_FUNC_CFG_INPUT_FLAGS_NO_AUTOCLEAR_STATISTIC;
803
804         rc = bnxt_hwrm_func_cfg_vf_set_flags(bp, vf, func_flags);
805         if (!rc) {
806                 bp->pf.vf_info[vf].persist_stats = on;
807                 bp->pf.vf_info[vf].func_cfg_flags = func_flags;
808         }
809
810         return rc;
811 }