4 * Copyright(c) 2017 Broadcom Limited.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
39 #include <rte_ethdev.h>
40 #include <rte_malloc.h>
41 #include <rte_cycles.h>
42 #include <rte_byteorder.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"
51 int rte_pmd_bnxt_set_tx_loopback(uint8_t port, uint8_t on)
53 struct rte_eth_dev *eth_dev;
57 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
62 eth_dev = &rte_eth_devices[port];
63 if (!is_bnxt_supported(eth_dev))
66 bp = (struct bnxt *)eth_dev->data->dev_private;
70 "Attempt to set Tx loopback on non-PF port %d!\n",
76 bp->pf.evb_mode = BNXT_EVB_MODE_VEB;
78 bp->pf.evb_mode = BNXT_EVB_MODE_VEPA;
80 rc = bnxt_hwrm_pf_evb_mode(bp);
86 rte_pmd_bnxt_set_all_queues_drop_en_cb(struct bnxt_vnic_info *vnic, void *onptr)
89 vnic->bd_stall = !(*on);
92 int rte_pmd_bnxt_set_all_queues_drop_en(uint8_t port, uint8_t on)
94 struct rte_eth_dev *eth_dev;
99 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
104 eth_dev = &rte_eth_devices[port];
105 if (!is_bnxt_supported(eth_dev))
108 bp = (struct bnxt *)eth_dev->data->dev_private;
112 "Attempt to set all queues drop on non-PF port!\n");
116 if (bp->vnic_info == NULL)
120 for (i = 0; i < bp->nr_vnics; i++) {
121 bp->vnic_info[i].bd_stall = !on;
122 rc = bnxt_hwrm_vnic_cfg(bp, &bp->vnic_info[i]);
124 RTE_LOG(ERR, PMD, "Failed to update PF VNIC %d.\n", i);
129 /* Stall all active VFs */
130 for (i = 0; i < bp->pf.active_vfs; i++) {
131 rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, i,
132 rte_pmd_bnxt_set_all_queues_drop_en_cb, &on,
135 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", i);
143 int rte_pmd_bnxt_set_vf_mac_addr(uint8_t port, uint16_t vf,
144 struct ether_addr *mac_addr)
146 struct rte_eth_dev *dev;
147 struct rte_eth_dev_info dev_info;
151 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
153 dev = &rte_eth_devices[port];
154 if (!is_bnxt_supported(dev))
157 rte_eth_dev_info_get(port, &dev_info);
158 bp = (struct bnxt *)dev->data->dev_private;
160 if (vf >= dev_info.max_vfs || mac_addr == NULL)
165 "Attempt to set VF %d mac address on non-PF port %d!\n",
170 rc = bnxt_hwrm_func_vf_mac(bp, vf, (uint8_t *)mac_addr);
175 int rte_pmd_bnxt_set_vf_rate_limit(uint8_t port, uint16_t vf,
176 uint16_t tx_rate, uint64_t q_msk)
178 struct rte_eth_dev *eth_dev;
179 struct rte_eth_dev_info dev_info;
181 uint16_t tot_rate = 0;
185 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
187 eth_dev = &rte_eth_devices[port];
188 if (!is_bnxt_supported(eth_dev))
191 rte_eth_dev_info_get(port, &dev_info);
192 bp = (struct bnxt *)eth_dev->data->dev_private;
194 if (!bp->pf.active_vfs)
197 if (vf >= bp->pf.max_vfs)
200 /* Add up the per queue BW and configure MAX BW of the VF */
201 for (idx = 0; idx < 64; idx++) {
202 if ((1ULL << idx) & q_msk)
206 /* Requested BW can't be greater than link speed */
207 if (tot_rate > eth_dev->data->dev_link.link_speed) {
208 RTE_LOG(ERR, PMD, "Rate > Link speed. Set to %d\n", tot_rate);
212 /* Requested BW already configured */
213 if (tot_rate == bp->pf.vf_info[vf].max_tx_rate)
216 rc = bnxt_hwrm_func_bw_cfg(bp, vf, tot_rate,
217 HWRM_FUNC_CFG_INPUT_ENABLES_MAX_BW);
220 bp->pf.vf_info[vf].max_tx_rate = tot_rate;
225 int rte_pmd_bnxt_set_vf_mac_anti_spoof(uint8_t port, uint16_t vf, uint8_t on)
227 struct rte_eth_dev_info dev_info;
228 struct rte_eth_dev *dev;
233 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
238 dev = &rte_eth_devices[port];
239 if (!is_bnxt_supported(dev))
242 rte_eth_dev_info_get(port, &dev_info);
243 bp = (struct bnxt *)dev->data->dev_private;
247 "Attempt to set mac spoof on non-PF port %d!\n", port);
251 if (vf >= dev_info.max_vfs)
254 /* Prev setting same as new setting. */
255 if (on == bp->pf.vf_info[vf].mac_spoof_en)
258 func_flags = bp->pf.vf_info[vf].func_cfg_flags;
262 HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE;
265 HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE;
267 bp->pf.vf_info[vf].func_cfg_flags = func_flags;
269 rc = bnxt_hwrm_func_cfg_vf_set_flags(bp, vf);
271 bp->pf.vf_info[vf].mac_spoof_en = on;
276 int rte_pmd_bnxt_set_vf_vlan_anti_spoof(uint8_t port, uint16_t vf, uint8_t on)
278 struct rte_eth_dev_info dev_info;
279 struct rte_eth_dev *dev;
283 struct bnxt_vnic_info vnic;
285 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
290 dev = &rte_eth_devices[port];
291 if (!is_bnxt_supported(dev))
294 rte_eth_dev_info_get(port, &dev_info);
295 bp = (struct bnxt *)dev->data->dev_private;
299 "Attempt to set mac spoof on non-PF port %d!\n", port);
303 if (vf >= dev_info.max_vfs)
306 rc = bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(bp, vf, on);
308 bp->pf.vf_info[vf].vlan_spoof_en = on;
310 dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf);
313 * This simply indicates there's no driver
314 * loaded. This is not an error.
317 "Unable to get default VNIC for VF %d\n",
320 vnic.fw_vnic_id = dflt_vnic;
321 if (bnxt_hwrm_vnic_qcfg(bp,
322 &vnic, bp->pf.first_vf_id + vf) == 0) {
323 if (bnxt_hwrm_cfa_l2_set_rx_mask(bp,
324 &vnic, bp->pf.vf_info[vf].vlan_count,
325 bp->pf.vf_info[vf].vlan_table))
331 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", vf);
338 rte_pmd_bnxt_set_vf_vlan_stripq_cb(struct bnxt_vnic_info *vnic, void *onptr)
341 vnic->vlan_strip = *on;
345 rte_pmd_bnxt_set_vf_vlan_stripq(uint8_t port, uint16_t vf, uint8_t on)
347 struct rte_eth_dev *dev;
348 struct rte_eth_dev_info dev_info;
352 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
354 dev = &rte_eth_devices[port];
355 if (!is_bnxt_supported(dev))
358 rte_eth_dev_info_get(port, &dev_info);
359 bp = (struct bnxt *)dev->data->dev_private;
361 if (vf >= dev_info.max_vfs)
366 "Attempt to set VF %d stripq on non-PF port %d!\n",
371 rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf,
372 rte_pmd_bnxt_set_vf_vlan_stripq_cb, &on,
375 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", vf);
380 int rte_pmd_bnxt_set_vf_rxmode(uint8_t port, uint16_t vf,
381 uint16_t rx_mask, uint8_t on)
383 struct rte_eth_dev *dev;
384 struct rte_eth_dev_info dev_info;
389 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
391 dev = &rte_eth_devices[port];
392 if (!is_bnxt_supported(dev))
395 rte_eth_dev_info_get(port, &dev_info);
396 bp = (struct bnxt *)dev->data->dev_private;
401 if (vf >= bp->pdev->max_vfs)
404 if (rx_mask & (ETH_VMDQ_ACCEPT_UNTAG | ETH_VMDQ_ACCEPT_HASH_MC)) {
405 RTE_LOG(ERR, PMD, "Currently cannot toggle this setting\n");
409 if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC && !on) {
410 RTE_LOG(ERR, PMD, "Currently cannot disable UC Rx\n");
414 if (rx_mask & ETH_VMDQ_ACCEPT_BROADCAST)
415 flag |= BNXT_VNIC_INFO_BCAST;
416 if (rx_mask & ETH_VMDQ_ACCEPT_MULTICAST)
417 flag |= BNXT_VNIC_INFO_ALLMULTI;
420 bp->pf.vf_info[vf].l2_rx_mask |= flag;
422 bp->pf.vf_info[vf].l2_rx_mask &= ~flag;
424 rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf,
425 vf_vnic_set_rxmask_cb,
426 &bp->pf.vf_info[vf].l2_rx_mask,
427 bnxt_set_rx_mask_no_vlan);
429 RTE_LOG(ERR, PMD, "bnxt_hwrm_func_vf_vnic_set_rxmask failed\n");
434 int rte_pmd_bnxt_set_vf_vlan_filter(uint8_t port, uint16_t vlan,
435 uint64_t vf_mask, uint8_t vlan_on)
437 struct bnxt_vlan_table_entry *ve;
438 struct rte_eth_dev *dev;
444 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
446 dev = &rte_eth_devices[port];
447 if (!is_bnxt_supported(dev))
450 bp = (struct bnxt *)dev->data->dev_private;
454 for (i = 0; vf_mask; i++, vf_mask >>= 1) {
455 cnt = bp->pf.vf_info[i].vlan_count;
457 if (bp->pf.vf_info[i].vlan_table == NULL) {
462 /* First, search for a duplicate... */
463 for (j = 0; j < cnt; j++) {
464 if (rte_be_to_cpu_16(
465 bp->pf.vf_info[i].vlan_table[j].vid) ==
470 /* Now check that there's space */
471 if (cnt == getpagesize() /
472 sizeof(struct bnxt_vlan_table_entry)) {
474 "VF %d VLAN table is full\n",
477 "cannot add VLAN %u\n",
483 cnt = bp->pf.vf_info[i].vlan_count++;
485 * And finally, add to the
488 ve = &bp->pf.vf_info[i].vlan_table[cnt];
489 /* TODO: Hardcoded TPID */
490 ve->tpid = rte_cpu_to_be_16(0x8100);
491 ve->vid = rte_cpu_to_be_16(vlan);
494 for (j = 0; cnt; j++) {
495 if (rte_be_to_cpu_16(
496 bp->pf.vf_info[i].vlan_table[j].vid) !=
500 &bp->pf.vf_info[i].vlan_table[j],
501 &bp->pf.vf_info[i].vlan_table[j + 1],
504 sizeof(struct bnxt_vlan_table_entry)));
506 cnt = bp->pf.vf_info[i].vlan_count--;
509 rte_pmd_bnxt_set_vf_vlan_anti_spoof(dev->data->port_id,
510 i, bp->pf.vf_info[i].vlan_spoof_en);
517 int rte_pmd_bnxt_get_vf_stats(uint8_t port,
519 struct rte_eth_stats *stats)
521 struct rte_eth_dev *dev;
522 struct rte_eth_dev_info dev_info;
525 dev = &rte_eth_devices[port];
526 if (!is_bnxt_supported(dev))
529 rte_eth_dev_info_get(port, &dev_info);
530 bp = (struct bnxt *)dev->data->dev_private;
532 if (vf_id >= dev_info.max_vfs)
537 "Attempt to get VF %d stats on non-PF port %d!\n",
542 return bnxt_hwrm_func_qstats(bp, bp->pf.first_vf_id + vf_id, stats);
545 int rte_pmd_bnxt_reset_vf_stats(uint8_t port,
548 struct rte_eth_dev *dev;
549 struct rte_eth_dev_info dev_info;
552 dev = &rte_eth_devices[port];
553 if (!is_bnxt_supported(dev))
556 rte_eth_dev_info_get(port, &dev_info);
557 bp = (struct bnxt *)dev->data->dev_private;
559 if (vf_id >= dev_info.max_vfs)
564 "Attempt to reset VF %d stats on non-PF port %d!\n",
569 return bnxt_hwrm_func_clr_stats(bp, bp->pf.first_vf_id + vf_id);
572 int rte_pmd_bnxt_get_vf_rx_status(uint8_t port, uint16_t vf_id)
574 struct rte_eth_dev *dev;
575 struct rte_eth_dev_info dev_info;
578 dev = &rte_eth_devices[port];
579 if (!is_bnxt_supported(dev))
582 rte_eth_dev_info_get(port, &dev_info);
583 bp = (struct bnxt *)dev->data->dev_private;
585 if (vf_id >= dev_info.max_vfs)
590 "Attempt to query VF %d RX stats on non-PF port %d!\n",
595 return bnxt_vf_default_vnic_count(bp, vf_id);
598 int rte_pmd_bnxt_get_vf_tx_drop_count(uint8_t port, uint16_t vf_id,
601 struct rte_eth_dev *dev;
602 struct rte_eth_dev_info dev_info;
605 dev = &rte_eth_devices[port];
606 if (!is_bnxt_supported(dev))
609 rte_eth_dev_info_get(port, &dev_info);
610 bp = (struct bnxt *)dev->data->dev_private;
612 if (vf_id >= dev_info.max_vfs)
617 "Attempt to query VF %d TX drops on non-PF port %d!\n",
622 return bnxt_hwrm_func_qstats_tx_drop(bp, bp->pf.first_vf_id + vf_id,
626 int rte_pmd_bnxt_mac_addr_add(uint8_t port, struct ether_addr *addr,
629 struct rte_eth_dev *dev;
630 struct rte_eth_dev_info dev_info;
632 struct bnxt_filter_info *filter;
633 struct bnxt_vnic_info vnic;
634 struct ether_addr dflt_mac;
637 dev = &rte_eth_devices[port];
638 if (!is_bnxt_supported(dev))
641 rte_eth_dev_info_get(port, &dev_info);
642 bp = (struct bnxt *)dev->data->dev_private;
644 if (vf_id >= dev_info.max_vfs)
649 "Attempt to config VF %d MAC on non-PF port %d!\n",
654 /* If the VF currently uses a random MAC, update default to this one */
655 if (bp->pf.vf_info[vf_id].random_mac) {
656 if (rte_pmd_bnxt_get_vf_rx_status(port, vf_id) <= 0)
657 rc = bnxt_hwrm_func_vf_mac(bp, vf_id, (uint8_t *)addr);
660 /* query the default VNIC id used by the function */
661 rc = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf_id);
665 memset(&vnic, 0, sizeof(struct bnxt_vnic_info));
666 vnic.fw_vnic_id = rte_le_to_cpu_16(rc);
667 rc = bnxt_hwrm_vnic_qcfg(bp, &vnic, bp->pf.first_vf_id + vf_id);
671 STAILQ_FOREACH(filter, &bp->pf.vf_info[vf_id].filter, next) {
673 HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX &&
675 (HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
676 HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK) &&
677 memcmp(addr, filter->l2_addr, ETHER_ADDR_LEN) == 0) {
678 bnxt_hwrm_clear_filter(bp, filter);
684 filter = bnxt_alloc_vf_filter(bp, vf_id);
686 filter->fw_l2_filter_id = UINT64_MAX;
687 filter->flags = HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX;
688 filter->enables = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
689 HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK;
690 memcpy(filter->l2_addr, addr, ETHER_ADDR_LEN);
691 memset(filter->l2_addr_mask, 0xff, ETHER_ADDR_LEN);
693 /* Do not add a filter for the default MAC */
694 if (bnxt_hwrm_func_qcfg_vf_default_mac(bp, vf_id, &dflt_mac) ||
695 memcmp(filter->l2_addr, dflt_mac.addr_bytes, ETHER_ADDR_LEN))
696 rc = bnxt_hwrm_set_filter(bp, vnic.fw_vnic_id, filter);
703 rte_pmd_bnxt_set_vf_vlan_insert(uint8_t port, uint16_t vf,
706 struct rte_eth_dev *dev;
707 struct rte_eth_dev_info dev_info;
711 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
713 dev = &rte_eth_devices[port];
714 if (!is_bnxt_supported(dev))
717 rte_eth_dev_info_get(port, &dev_info);
718 bp = (struct bnxt *)dev->data->dev_private;
720 if (vf >= dev_info.max_vfs)
725 "Attempt to set VF %d vlan insert on non-PF port %d!\n",
730 bp->pf.vf_info[vf].dflt_vlan = vlan_id;
731 if (bnxt_hwrm_func_qcfg_current_vf_vlan(bp, vf) ==
732 bp->pf.vf_info[vf].dflt_vlan)
735 rc = bnxt_hwrm_set_vf_vlan(bp, vf);