net/bnxt: fix automatic clearing of VF stats
[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         int dflt_vnic;
303         struct bnxt_vnic_info vnic;
304
305         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
306
307         if (on > 1)
308                 return -EINVAL;
309
310         dev = &rte_eth_devices[port];
311         if (!is_bnxt_supported(dev))
312                 return -ENOTSUP;
313
314         rte_eth_dev_info_get(port, &dev_info);
315         bp = (struct bnxt *)dev->data->dev_private;
316
317         if (!BNXT_PF(bp)) {
318                 RTE_LOG(ERR, PMD,
319                         "Attempt to set mac spoof on non-PF port %d!\n", port);
320                 return -EINVAL;
321         }
322
323         if (vf >= dev_info.max_vfs)
324                 return -EINVAL;
325
326         rc = bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(bp, vf, on);
327         if (!rc) {
328                 bp->pf.vf_info[vf].vlan_spoof_en = on;
329                 if (on) {
330                         dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf);
331                         if (dflt_vnic < 0) {
332                                 /*
333                                  * This simply indicates there's no driver
334                                  * loaded.  This is not an error.
335                                  */
336                                 RTE_LOG(ERR, PMD,
337                                       "Unable to get default VNIC for VF %d\n",
338                                         vf);
339                         } else {
340                                 vnic.fw_vnic_id = dflt_vnic;
341                                 if (bnxt_hwrm_vnic_qcfg(bp,
342                                         &vnic, bp->pf.first_vf_id + vf) == 0) {
343                                         if (bnxt_hwrm_cfa_l2_set_rx_mask(bp,
344                                            &vnic, bp->pf.vf_info[vf].vlan_count,
345                                            bp->pf.vf_info[vf].vlan_table))
346                                                 rc = -1;
347                                 }
348                         }
349                 }
350         } else {
351                 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", vf);
352         }
353
354         return rc;
355 }
356
357 static void
358 rte_pmd_bnxt_set_vf_vlan_stripq_cb(struct bnxt_vnic_info *vnic, void *onptr)
359 {
360         uint8_t *on = onptr;
361         vnic->vlan_strip = *on;
362 }
363
364 int
365 rte_pmd_bnxt_set_vf_vlan_stripq(uint8_t port, uint16_t vf, uint8_t on)
366 {
367         struct rte_eth_dev *dev;
368         struct rte_eth_dev_info dev_info;
369         struct bnxt *bp;
370         int rc;
371
372         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
373
374         dev = &rte_eth_devices[port];
375         if (!is_bnxt_supported(dev))
376                 return -ENOTSUP;
377
378         rte_eth_dev_info_get(port, &dev_info);
379         bp = (struct bnxt *)dev->data->dev_private;
380
381         if (vf >= dev_info.max_vfs)
382                 return -EINVAL;
383
384         if (!BNXT_PF(bp)) {
385                 RTE_LOG(ERR, PMD,
386                         "Attempt to set VF %d stripq on non-PF port %d!\n",
387                         vf, port);
388                 return -ENOTSUP;
389         }
390
391         rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf,
392                                 rte_pmd_bnxt_set_vf_vlan_stripq_cb, &on,
393                                 bnxt_hwrm_vnic_cfg);
394         if (rc)
395                 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", vf);
396
397         return rc;
398 }
399
400 int rte_pmd_bnxt_set_vf_rxmode(uint8_t port, uint16_t vf,
401                                 uint16_t rx_mask, uint8_t on)
402 {
403         struct rte_eth_dev *dev;
404         struct rte_eth_dev_info dev_info;
405         uint16_t flag = 0;
406         struct bnxt *bp;
407         int rc;
408
409         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
410
411         dev = &rte_eth_devices[port];
412         if (!is_bnxt_supported(dev))
413                 return -ENOTSUP;
414
415         rte_eth_dev_info_get(port, &dev_info);
416         bp = (struct bnxt *)dev->data->dev_private;
417
418         if (!bp->pf.vf_info)
419                 return -EINVAL;
420
421         if (vf >= bp->pdev->max_vfs)
422                 return -EINVAL;
423
424         if (rx_mask & (ETH_VMDQ_ACCEPT_UNTAG | ETH_VMDQ_ACCEPT_HASH_MC)) {
425                 RTE_LOG(ERR, PMD, "Currently cannot toggle this setting\n");
426                 return -ENOTSUP;
427         }
428
429         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC && !on) {
430                 RTE_LOG(ERR, PMD, "Currently cannot disable UC Rx\n");
431                 return -ENOTSUP;
432         }
433
434         if (rx_mask & ETH_VMDQ_ACCEPT_BROADCAST)
435                 flag |= BNXT_VNIC_INFO_BCAST;
436         if (rx_mask & ETH_VMDQ_ACCEPT_MULTICAST)
437                 flag |= BNXT_VNIC_INFO_ALLMULTI;
438
439         if (on)
440                 bp->pf.vf_info[vf].l2_rx_mask |= flag;
441         else
442                 bp->pf.vf_info[vf].l2_rx_mask &= ~flag;
443
444         rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf,
445                                         vf_vnic_set_rxmask_cb,
446                                         &bp->pf.vf_info[vf].l2_rx_mask,
447                                         bnxt_set_rx_mask_no_vlan);
448         if (rc)
449                 RTE_LOG(ERR, PMD, "bnxt_hwrm_func_vf_vnic_set_rxmask failed\n");
450
451         return rc;
452 }
453
454 int rte_pmd_bnxt_set_vf_vlan_filter(uint8_t port, uint16_t vlan,
455                                     uint64_t vf_mask, uint8_t vlan_on)
456 {
457         struct bnxt_vlan_table_entry *ve;
458         struct rte_eth_dev *dev;
459         struct bnxt *bp;
460         uint16_t cnt;
461         int rc = 0;
462         int i, j;
463
464         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
465
466         dev = &rte_eth_devices[port];
467         if (!is_bnxt_supported(dev))
468                 return -ENOTSUP;
469
470         bp = (struct bnxt *)dev->data->dev_private;
471         if (!bp->pf.vf_info)
472                 return -EINVAL;
473
474         for (i = 0; vf_mask; i++, vf_mask >>= 1) {
475                 cnt = bp->pf.vf_info[i].vlan_count;
476                 if (vf_mask & 1) {
477                         if (bp->pf.vf_info[i].vlan_table == NULL) {
478                                 rc = -1;
479                                 continue;
480                         }
481                         if (vlan_on) {
482                                 /* First, search for a duplicate... */
483                                 for (j = 0; j < cnt; j++) {
484                                         if (rte_be_to_cpu_16(
485                                          bp->pf.vf_info[i].vlan_table[j].vid) ==
486                                             vlan)
487                                                 break;
488                                 }
489                                 if (j == cnt) {
490                                         /* Now check that there's space */
491                                         if (cnt == getpagesize() /
492                                          sizeof(struct bnxt_vlan_table_entry)) {
493                                                 RTE_LOG(ERR, PMD,
494                                                   "VF %d VLAN table is full\n",
495                                                   i);
496                                                 RTE_LOG(ERR, PMD,
497                                                         "cannot add VLAN %u\n",
498                                                         vlan);
499                                                 rc = -1;
500                                                 continue;
501                                         }
502
503                                         cnt = bp->pf.vf_info[i].vlan_count++;
504                                         /*
505                                          * And finally, add to the
506                                          * end of the table
507                                          */
508                                         ve = &bp->pf.vf_info[i].vlan_table[cnt];
509                                         /* TODO: Hardcoded TPID */
510                                         ve->tpid = rte_cpu_to_be_16(0x8100);
511                                         ve->vid = rte_cpu_to_be_16(vlan);
512                                 }
513                         } else {
514                                 for (j = 0; cnt; j++) {
515                                         if (rte_be_to_cpu_16(
516                                         bp->pf.vf_info[i].vlan_table[j].vid) !=
517                                             vlan)
518                                                 continue;
519                                         memmove(
520                                          &bp->pf.vf_info[i].vlan_table[j],
521                                          &bp->pf.vf_info[i].vlan_table[j + 1],
522                                          getpagesize() -
523                                          ((j + 1) *
524                                          sizeof(struct bnxt_vlan_table_entry)));
525                                         j--;
526                                         cnt = bp->pf.vf_info[i].vlan_count--;
527                                 }
528                         }
529                         rte_pmd_bnxt_set_vf_vlan_anti_spoof(dev->data->port_id,
530                                          i, bp->pf.vf_info[i].vlan_spoof_en);
531                 }
532         }
533
534         return rc;
535 }
536
537 int rte_pmd_bnxt_get_vf_stats(uint8_t port,
538                               uint16_t vf_id,
539                               struct rte_eth_stats *stats)
540 {
541         struct rte_eth_dev *dev;
542         struct rte_eth_dev_info dev_info;
543         struct bnxt *bp;
544
545         dev = &rte_eth_devices[port];
546         if (!is_bnxt_supported(dev))
547                 return -ENOTSUP;
548
549         rte_eth_dev_info_get(port, &dev_info);
550         bp = (struct bnxt *)dev->data->dev_private;
551
552         if (vf_id >= dev_info.max_vfs)
553                 return -EINVAL;
554
555         if (!BNXT_PF(bp)) {
556                 RTE_LOG(ERR, PMD,
557                         "Attempt to get VF %d stats on non-PF port %d!\n",
558                         vf_id, port);
559                 return -ENOTSUP;
560         }
561
562         return bnxt_hwrm_func_qstats(bp, bp->pf.first_vf_id + vf_id, stats);
563 }
564
565 int rte_pmd_bnxt_reset_vf_stats(uint8_t port,
566                                 uint16_t vf_id)
567 {
568         struct rte_eth_dev *dev;
569         struct rte_eth_dev_info dev_info;
570         struct bnxt *bp;
571
572         dev = &rte_eth_devices[port];
573         if (!is_bnxt_supported(dev))
574                 return -ENOTSUP;
575
576         rte_eth_dev_info_get(port, &dev_info);
577         bp = (struct bnxt *)dev->data->dev_private;
578
579         if (vf_id >= dev_info.max_vfs)
580                 return -EINVAL;
581
582         if (!BNXT_PF(bp)) {
583                 RTE_LOG(ERR, PMD,
584                         "Attempt to reset VF %d stats on non-PF port %d!\n",
585                         vf_id, port);
586                 return -ENOTSUP;
587         }
588
589         return bnxt_hwrm_func_clr_stats(bp, bp->pf.first_vf_id + vf_id);
590 }
591
592 int rte_pmd_bnxt_get_vf_rx_status(uint8_t port, uint16_t vf_id)
593 {
594         struct rte_eth_dev *dev;
595         struct rte_eth_dev_info dev_info;
596         struct bnxt *bp;
597
598         dev = &rte_eth_devices[port];
599         if (!is_bnxt_supported(dev))
600                 return -ENOTSUP;
601
602         rte_eth_dev_info_get(port, &dev_info);
603         bp = (struct bnxt *)dev->data->dev_private;
604
605         if (vf_id >= dev_info.max_vfs)
606                 return -EINVAL;
607
608         if (!BNXT_PF(bp)) {
609                 RTE_LOG(ERR, PMD,
610                         "Attempt to query VF %d RX stats on non-PF port %d!\n",
611                         vf_id, port);
612                 return -ENOTSUP;
613         }
614
615         return bnxt_vf_vnic_count(bp, vf_id);
616 }
617
618 int rte_pmd_bnxt_get_vf_tx_drop_count(uint8_t port, uint16_t vf_id,
619                                       uint64_t *count)
620 {
621         struct rte_eth_dev *dev;
622         struct rte_eth_dev_info dev_info;
623         struct bnxt *bp;
624
625         dev = &rte_eth_devices[port];
626         if (!is_bnxt_supported(dev))
627                 return -ENOTSUP;
628
629         rte_eth_dev_info_get(port, &dev_info);
630         bp = (struct bnxt *)dev->data->dev_private;
631
632         if (vf_id >= dev_info.max_vfs)
633                 return -EINVAL;
634
635         if (!BNXT_PF(bp)) {
636                 RTE_LOG(ERR, PMD,
637                         "Attempt to query VF %d TX drops on non-PF port %d!\n",
638                         vf_id, port);
639                 return -ENOTSUP;
640         }
641
642         return bnxt_hwrm_func_qstats_tx_drop(bp, bp->pf.first_vf_id + vf_id,
643                                              count);
644 }
645
646 int rte_pmd_bnxt_mac_addr_add(uint8_t port, struct ether_addr *addr,
647                                 uint32_t vf_id)
648 {
649         struct rte_eth_dev *dev;
650         struct rte_eth_dev_info dev_info;
651         struct bnxt *bp;
652         struct bnxt_filter_info *filter;
653         struct bnxt_vnic_info vnic;
654         struct ether_addr dflt_mac;
655         int rc;
656
657         dev = &rte_eth_devices[port];
658         if (!is_bnxt_supported(dev))
659                 return -ENOTSUP;
660
661         rte_eth_dev_info_get(port, &dev_info);
662         bp = (struct bnxt *)dev->data->dev_private;
663
664         if (vf_id >= dev_info.max_vfs)
665                 return -EINVAL;
666
667         if (!BNXT_PF(bp)) {
668                 RTE_LOG(ERR, PMD,
669                         "Attempt to config VF %d MAC on non-PF port %d!\n",
670                         vf_id, port);
671                 return -ENOTSUP;
672         }
673
674         /* If the VF currently uses a random MAC, update default to this one */
675         if (bp->pf.vf_info[vf_id].random_mac) {
676                 if (rte_pmd_bnxt_get_vf_rx_status(port, vf_id) <= 0)
677                         rc = bnxt_hwrm_func_vf_mac(bp, vf_id, (uint8_t *)addr);
678         }
679
680         /* query the default VNIC id used by the function */
681         rc = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf_id);
682         if (rc < 0)
683                 goto exit;
684
685         memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
686         vnic.fw_vnic_id = rte_le_to_cpu_16(rc);
687         rc = bnxt_hwrm_vnic_qcfg(bp, &vnic, bp->pf.first_vf_id + vf_id);
688         if (rc < 0)
689                 goto exit;
690
691         STAILQ_FOREACH(filter, &bp->pf.vf_info[vf_id].filter, next) {
692                 if (filter->flags ==
693                     HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX &&
694                     filter->enables ==
695                     (HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
696                      HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK) &&
697                     memcmp(addr, filter->l2_addr, ETHER_ADDR_LEN) == 0) {
698                         bnxt_hwrm_clear_filter(bp, filter);
699                         break;
700                 }
701         }
702
703         if (filter == NULL)
704                 filter = bnxt_alloc_vf_filter(bp, vf_id);
705
706         filter->fw_l2_filter_id = UINT64_MAX;
707         filter->flags = HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX;
708         filter->enables = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
709                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK;
710         memcpy(filter->l2_addr, addr, ETHER_ADDR_LEN);
711         memset(filter->l2_addr_mask, 0xff, ETHER_ADDR_LEN);
712
713         /* Do not add a filter for the default MAC */
714         if (bnxt_hwrm_func_qcfg_vf_default_mac(bp, vf_id, &dflt_mac) ||
715             memcmp(filter->l2_addr, dflt_mac.addr_bytes, ETHER_ADDR_LEN))
716                 rc = bnxt_hwrm_set_filter(bp, vnic.fw_vnic_id, filter);
717
718 exit:
719         return rc;
720 }
721
722 int
723 rte_pmd_bnxt_set_vf_vlan_insert(uint8_t port, uint16_t vf,
724                 uint16_t vlan_id)
725 {
726         struct rte_eth_dev *dev;
727         struct rte_eth_dev_info dev_info;
728         struct bnxt *bp;
729         int rc;
730
731         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
732
733         dev = &rte_eth_devices[port];
734         if (!is_bnxt_supported(dev))
735                 return -ENOTSUP;
736
737         rte_eth_dev_info_get(port, &dev_info);
738         bp = (struct bnxt *)dev->data->dev_private;
739
740         if (vf >= dev_info.max_vfs)
741                 return -EINVAL;
742
743         if (!BNXT_PF(bp)) {
744                 RTE_LOG(ERR, PMD,
745                         "Attempt to set VF %d vlan insert on non-PF port %d!\n",
746                         vf, port);
747                 return -ENOTSUP;
748         }
749
750         bp->pf.vf_info[vf].dflt_vlan = vlan_id;
751         if (bnxt_hwrm_func_qcfg_current_vf_vlan(bp, vf) ==
752             bp->pf.vf_info[vf].dflt_vlan)
753                 return 0;
754
755         rc = bnxt_hwrm_set_vf_vlan(bp, vf);
756
757         return rc;
758 }
759
760 int rte_pmd_bnxt_set_vf_persist_stats(uint8_t port, uint16_t vf, uint8_t on)
761 {
762         struct rte_eth_dev_info dev_info;
763         struct rte_eth_dev *dev;
764         uint32_t func_flags;
765         struct bnxt *bp;
766         int rc;
767
768         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
769
770         if (on > 1)
771                 return -EINVAL;
772
773         dev = &rte_eth_devices[port];
774         rte_eth_dev_info_get(port, &dev_info);
775         bp = (struct bnxt *)dev->data->dev_private;
776
777         if (!BNXT_PF(bp)) {
778                 RTE_LOG(ERR, PMD,
779                         "Attempt to set persist stats on non-PF port %d!\n",
780                         port);
781                 return -EINVAL;
782         }
783
784         if (vf >= dev_info.max_vfs)
785                 return -EINVAL;
786
787         /* Prev setting same as new setting. */
788         if (on == bp->pf.vf_info[vf].persist_stats)
789                 return 0;
790
791         func_flags = bp->pf.vf_info[vf].func_cfg_flags;
792
793         if (on)
794                 func_flags |=
795                         HWRM_FUNC_CFG_INPUT_FLAGS_NO_AUTOCLEAR_STATISTIC;
796         else
797                 func_flags &=
798                         ~HWRM_FUNC_CFG_INPUT_FLAGS_NO_AUTOCLEAR_STATISTIC;
799
800         rc = bnxt_hwrm_func_cfg_vf_set_flags(bp, vf, func_flags);
801         if (!rc) {
802                 bp->pf.vf_info[vf].persist_stats = on;
803                 bp->pf.vf_info[vf].func_cfg_flags = func_flags;
804         }
805
806         return rc;
807 }