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 bnxt_rcv_msg_from_vf(struct bnxt *bp, uint16_t vf_id, void *msg)
53 struct rte_pmd_bnxt_mb_event_param ret_param;
55 ret_param.retval = RTE_PMD_BNXT_MB_EVENT_PROCEED;
56 ret_param.vf_id = vf_id;
59 _rte_eth_dev_callback_process(bp->eth_dev, RTE_ETH_EVENT_VF_MBOX,
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;
66 return ret_param.retval == RTE_PMD_BNXT_MB_EVENT_NOOP_ACK ?
70 int rte_pmd_bnxt_set_tx_loopback(uint8_t port, uint8_t on)
72 struct rte_eth_dev *eth_dev;
76 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
81 eth_dev = &rte_eth_devices[port];
82 if (!is_bnxt_supported(eth_dev))
85 bp = (struct bnxt *)eth_dev->data->dev_private;
89 "Attempt to set Tx loopback on non-PF port %d!\n",
95 bp->pf.evb_mode = BNXT_EVB_MODE_VEB;
97 bp->pf.evb_mode = BNXT_EVB_MODE_VEPA;
99 rc = bnxt_hwrm_pf_evb_mode(bp);
105 rte_pmd_bnxt_set_all_queues_drop_en_cb(struct bnxt_vnic_info *vnic, void *onptr)
108 vnic->bd_stall = !(*on);
111 int rte_pmd_bnxt_set_all_queues_drop_en(uint8_t port, uint8_t on)
113 struct rte_eth_dev *eth_dev;
118 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
123 eth_dev = &rte_eth_devices[port];
124 if (!is_bnxt_supported(eth_dev))
127 bp = (struct bnxt *)eth_dev->data->dev_private;
131 "Attempt to set all queues drop on non-PF port!\n");
135 if (bp->vnic_info == NULL)
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]);
143 RTE_LOG(ERR, PMD, "Failed to update PF VNIC %d.\n", i);
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,
154 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", i);
162 int rte_pmd_bnxt_set_vf_mac_addr(uint8_t port, uint16_t vf,
163 struct ether_addr *mac_addr)
165 struct rte_eth_dev *dev;
166 struct rte_eth_dev_info dev_info;
170 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
172 dev = &rte_eth_devices[port];
173 if (!is_bnxt_supported(dev))
176 rte_eth_dev_info_get(port, &dev_info);
177 bp = (struct bnxt *)dev->data->dev_private;
179 if (vf >= dev_info.max_vfs || mac_addr == NULL)
184 "Attempt to set VF %d mac address on non-PF port %d!\n",
189 rc = bnxt_hwrm_func_vf_mac(bp, vf, (uint8_t *)mac_addr);
194 int rte_pmd_bnxt_set_vf_rate_limit(uint8_t port, uint16_t vf,
195 uint16_t tx_rate, uint64_t q_msk)
197 struct rte_eth_dev *eth_dev;
198 struct rte_eth_dev_info dev_info;
200 uint16_t tot_rate = 0;
204 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
206 eth_dev = &rte_eth_devices[port];
207 if (!is_bnxt_supported(eth_dev))
210 rte_eth_dev_info_get(port, &dev_info);
211 bp = (struct bnxt *)eth_dev->data->dev_private;
213 if (!bp->pf.active_vfs)
216 if (vf >= bp->pf.max_vfs)
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)
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);
231 /* Requested BW already configured */
232 if (tot_rate == bp->pf.vf_info[vf].max_tx_rate)
235 rc = bnxt_hwrm_func_bw_cfg(bp, vf, tot_rate,
236 HWRM_FUNC_CFG_INPUT_ENABLES_MAX_BW);
239 bp->pf.vf_info[vf].max_tx_rate = tot_rate;
244 int rte_pmd_bnxt_set_vf_mac_anti_spoof(uint8_t port, uint16_t vf, uint8_t on)
246 struct rte_eth_dev_info dev_info;
247 struct rte_eth_dev *dev;
252 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
257 dev = &rte_eth_devices[port];
258 if (!is_bnxt_supported(dev))
261 rte_eth_dev_info_get(port, &dev_info);
262 bp = (struct bnxt *)dev->data->dev_private;
266 "Attempt to set mac spoof on non-PF port %d!\n", port);
270 if (vf >= dev_info.max_vfs)
273 /* Prev setting same as new setting. */
274 if (on == bp->pf.vf_info[vf].mac_spoof_en)
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);
283 HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE;
286 HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE;
288 rc = bnxt_hwrm_func_cfg_vf_set_flags(bp, vf, func_flags);
290 bp->pf.vf_info[vf].mac_spoof_en = on;
291 bp->pf.vf_info[vf].func_cfg_flags = func_flags;
297 int rte_pmd_bnxt_set_vf_vlan_anti_spoof(uint8_t port, uint16_t vf, uint8_t on)
299 struct rte_eth_dev_info dev_info;
300 struct rte_eth_dev *dev;
304 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
309 dev = &rte_eth_devices[port];
310 if (!is_bnxt_supported(dev))
313 rte_eth_dev_info_get(port, &dev_info);
314 bp = (struct bnxt *)dev->data->dev_private;
318 "Attempt to set VLAN spoof on non-PF port %d!\n", port);
322 if (vf >= dev_info.max_vfs)
325 if (on == bp->pf.vf_info[vf].vlan_spoof_en)
328 rc = bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(bp, vf, on);
330 bp->pf.vf_info[vf].vlan_spoof_en = 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))
339 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", vf);
346 rte_pmd_bnxt_set_vf_vlan_stripq_cb(struct bnxt_vnic_info *vnic, void *onptr)
349 vnic->vlan_strip = *on;
353 rte_pmd_bnxt_set_vf_vlan_stripq(uint8_t port, uint16_t vf, uint8_t on)
355 struct rte_eth_dev *dev;
356 struct rte_eth_dev_info dev_info;
360 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
362 dev = &rte_eth_devices[port];
363 if (!is_bnxt_supported(dev))
366 rte_eth_dev_info_get(port, &dev_info);
367 bp = (struct bnxt *)dev->data->dev_private;
369 if (vf >= dev_info.max_vfs)
374 "Attempt to set VF %d stripq on non-PF port %d!\n",
379 rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf,
380 rte_pmd_bnxt_set_vf_vlan_stripq_cb, &on,
383 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", vf);
388 int rte_pmd_bnxt_set_vf_rxmode(uint8_t port, uint16_t vf,
389 uint16_t rx_mask, uint8_t on)
391 struct rte_eth_dev *dev;
392 struct rte_eth_dev_info dev_info;
397 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
399 dev = &rte_eth_devices[port];
400 if (!is_bnxt_supported(dev))
403 rte_eth_dev_info_get(port, &dev_info);
404 bp = (struct bnxt *)dev->data->dev_private;
409 if (vf >= bp->pdev->max_vfs)
412 if (rx_mask & (ETH_VMDQ_ACCEPT_UNTAG | ETH_VMDQ_ACCEPT_HASH_MC)) {
413 RTE_LOG(ERR, PMD, "Currently cannot toggle this setting\n");
417 if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC && !on) {
418 RTE_LOG(ERR, PMD, "Currently cannot disable UC Rx\n");
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;
428 bp->pf.vf_info[vf].l2_rx_mask |= flag;
430 bp->pf.vf_info[vf].l2_rx_mask &= ~flag;
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);
437 RTE_LOG(ERR, PMD, "bnxt_hwrm_func_vf_vnic_set_rxmask failed\n");
442 static int bnxt_set_vf_table(struct bnxt *bp, uint16_t vf)
446 struct bnxt_vnic_info vnic;
450 "Attempt to set VLAN table on non-PF port!\n");
454 if (vf >= bp->pdev->max_vfs)
457 dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf);
459 /* This simply indicates there's no driver loaded.
460 * This is not an error.
462 RTE_LOG(ERR, PMD, "Unable to get default VNIC for VF %d\n", vf);
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))
480 int rte_pmd_bnxt_set_vf_vlan_filter(uint8_t port, uint16_t vlan,
481 uint64_t vf_mask, uint8_t vlan_on)
483 struct bnxt_vlan_table_entry *ve;
484 struct bnxt_vlan_antispoof_table_entry *vase;
485 struct rte_eth_dev *dev;
491 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
493 dev = &rte_eth_devices[port];
494 if (!is_bnxt_supported(dev))
497 bp = (struct bnxt *)dev->data->dev_private;
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)
506 if (bp->pf.vf_info[i].vlan_table == NULL) {
510 if (bp->pf.vf_info[i].vlan_as_table == NULL) {
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)
522 /* Now check that there's space */
523 if (cnt == getpagesize() / sizeof(struct
524 bnxt_vlan_antispoof_table_entry)) {
526 "VLAN anti-spoof table is full\n");
528 "VF %d cannot add VLAN %u\n",
534 /* cnt is one less than vlan_count */
535 cnt = bp->pf.vf_info[i].vlan_count++;
537 * And finally, add to the
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);
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)
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)));
564 cnt = --bp->pf.vf_info[i].vlan_count;
567 bnxt_set_vf_table(bp, i);
573 int rte_pmd_bnxt_get_vf_stats(uint8_t port,
575 struct rte_eth_stats *stats)
577 struct rte_eth_dev *dev;
578 struct rte_eth_dev_info dev_info;
581 dev = &rte_eth_devices[port];
582 if (!is_bnxt_supported(dev))
585 rte_eth_dev_info_get(port, &dev_info);
586 bp = (struct bnxt *)dev->data->dev_private;
588 if (vf_id >= dev_info.max_vfs)
593 "Attempt to get VF %d stats on non-PF port %d!\n",
598 return bnxt_hwrm_func_qstats(bp, bp->pf.first_vf_id + vf_id, stats);
601 int rte_pmd_bnxt_reset_vf_stats(uint8_t port,
604 struct rte_eth_dev *dev;
605 struct rte_eth_dev_info dev_info;
608 dev = &rte_eth_devices[port];
609 if (!is_bnxt_supported(dev))
612 rte_eth_dev_info_get(port, &dev_info);
613 bp = (struct bnxt *)dev->data->dev_private;
615 if (vf_id >= dev_info.max_vfs)
620 "Attempt to reset VF %d stats on non-PF port %d!\n",
625 return bnxt_hwrm_func_clr_stats(bp, bp->pf.first_vf_id + vf_id);
628 int rte_pmd_bnxt_get_vf_rx_status(uint8_t port, uint16_t vf_id)
630 struct rte_eth_dev *dev;
631 struct rte_eth_dev_info dev_info;
634 dev = &rte_eth_devices[port];
635 if (!is_bnxt_supported(dev))
638 rte_eth_dev_info_get(port, &dev_info);
639 bp = (struct bnxt *)dev->data->dev_private;
641 if (vf_id >= dev_info.max_vfs)
646 "Attempt to query VF %d RX stats on non-PF port %d!\n",
651 return bnxt_vf_vnic_count(bp, vf_id);
654 int rte_pmd_bnxt_get_vf_tx_drop_count(uint8_t port, uint16_t vf_id,
657 struct rte_eth_dev *dev;
658 struct rte_eth_dev_info dev_info;
661 dev = &rte_eth_devices[port];
662 if (!is_bnxt_supported(dev))
665 rte_eth_dev_info_get(port, &dev_info);
666 bp = (struct bnxt *)dev->data->dev_private;
668 if (vf_id >= dev_info.max_vfs)
673 "Attempt to query VF %d TX drops on non-PF port %d!\n",
678 return bnxt_hwrm_func_qstats_tx_drop(bp, bp->pf.first_vf_id + vf_id,
682 int rte_pmd_bnxt_mac_addr_add(uint8_t port, struct ether_addr *addr,
685 struct rte_eth_dev *dev;
686 struct rte_eth_dev_info dev_info;
688 struct bnxt_filter_info *filter;
689 struct bnxt_vnic_info vnic;
690 struct ether_addr dflt_mac;
693 dev = &rte_eth_devices[port];
694 if (!is_bnxt_supported(dev))
697 rte_eth_dev_info_get(port, &dev_info);
698 bp = (struct bnxt *)dev->data->dev_private;
700 if (vf_id >= dev_info.max_vfs)
705 "Attempt to config VF %d MAC on non-PF port %d!\n",
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);
716 /* query the default VNIC id used by the function */
717 rc = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf_id);
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);
727 STAILQ_FOREACH(filter, &bp->pf.vf_info[vf_id].filter, next) {
729 HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX &&
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);
740 filter = bnxt_alloc_vf_filter(bp, vf_id);
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);
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);
759 rte_pmd_bnxt_set_vf_vlan_insert(uint8_t port, uint16_t vf,
762 struct rte_eth_dev *dev;
763 struct rte_eth_dev_info dev_info;
767 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
769 dev = &rte_eth_devices[port];
770 if (!is_bnxt_supported(dev))
773 rte_eth_dev_info_get(port, &dev_info);
774 bp = (struct bnxt *)dev->data->dev_private;
776 if (vf >= dev_info.max_vfs)
781 "Attempt to set VF %d vlan insert on non-PF port %d!\n",
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)
791 rc = bnxt_hwrm_set_vf_vlan(bp, vf);
796 int rte_pmd_bnxt_set_vf_persist_stats(uint8_t port, uint16_t vf, uint8_t on)
798 struct rte_eth_dev_info dev_info;
799 struct rte_eth_dev *dev;
804 RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
809 dev = &rte_eth_devices[port];
810 rte_eth_dev_info_get(port, &dev_info);
811 bp = (struct bnxt *)dev->data->dev_private;
815 "Attempt to set persist stats on non-PF port %d!\n",
820 if (vf >= dev_info.max_vfs)
823 /* Prev setting same as new setting. */
824 if (on == bp->pf.vf_info[vf].persist_stats)
827 func_flags = bp->pf.vf_info[vf].func_cfg_flags;
831 HWRM_FUNC_CFG_INPUT_FLAGS_NO_AUTOCLEAR_STATISTIC;
834 ~HWRM_FUNC_CFG_INPUT_FLAGS_NO_AUTOCLEAR_STATISTIC;
836 rc = bnxt_hwrm_func_cfg_vf_set_flags(bp, vf, func_flags);
838 bp->pf.vf_info[vf].persist_stats = on;
839 bp->pf.vf_info[vf].func_cfg_flags = func_flags;