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