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