net/bnxt: support Tx loopback, set VF MAC and queues drop
[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
37 #include <rte_dev.h>
38 #include <rte_ethdev.h>
39 #include <rte_malloc.h>
40 #include <rte_cycles.h>
41 #include <rte_byteorder.h>
42
43 #include "bnxt.h"
44 #include "bnxt_filter.h"
45 #include "bnxt_hwrm.h"
46 #include "bnxt_vnic.h"
47 #include "rte_pmd_bnxt.h"
48 #include "hsi_struct_def_dpdk.h"
49
50 int rte_pmd_bnxt_set_tx_loopback(uint8_t port, uint8_t on)
51 {
52         struct rte_eth_dev *eth_dev;
53         struct bnxt *bp;
54         int rc;
55
56         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
57
58         if (on > 1)
59                 return -EINVAL;
60
61         eth_dev = &rte_eth_devices[port];
62         if (!is_bnxt_supported(eth_dev))
63                 return -ENOTSUP;
64
65         bp = (struct bnxt *)eth_dev->data->dev_private;
66
67         if (!BNXT_PF(bp)) {
68                 RTE_LOG(ERR, PMD,
69                         "Attempt to set Tx loopback on non-PF port %d!\n",
70                         port);
71                 return -ENOTSUP;
72         }
73
74         if (on)
75                 bp->pf.evb_mode = BNXT_EVB_MODE_VEB;
76         else
77                 bp->pf.evb_mode = BNXT_EVB_MODE_VEPA;
78
79         rc = bnxt_hwrm_pf_evb_mode(bp);
80
81         return rc;
82 }
83
84 static void
85 rte_pmd_bnxt_set_all_queues_drop_en_cb(struct bnxt_vnic_info *vnic, void *onptr)
86 {
87         uint8_t *on = onptr;
88         vnic->bd_stall = !(*on);
89 }
90
91 int rte_pmd_bnxt_set_all_queues_drop_en(uint8_t port, uint8_t on)
92 {
93         struct rte_eth_dev *eth_dev;
94         struct bnxt *bp;
95         uint32_t i;
96         int rc;
97
98         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
99
100         if (on > 1)
101                 return -EINVAL;
102
103         eth_dev = &rte_eth_devices[port];
104         if (!is_bnxt_supported(eth_dev))
105                 return -ENOTSUP;
106
107         bp = (struct bnxt *)eth_dev->data->dev_private;
108
109         if (!BNXT_PF(bp)) {
110                 RTE_LOG(ERR, PMD,
111                         "Attempt to set all queues drop on non-PF port!\n");
112                 return -ENOTSUP;
113         }
114
115         if (bp->vnic_info == NULL)
116                 return -ENODEV;
117
118         /* Stall PF */
119         for (i = 0; i < bp->nr_vnics; i++) {
120                 bp->vnic_info[i].bd_stall = !on;
121                 rc = bnxt_hwrm_vnic_cfg(bp, &bp->vnic_info[i]);
122                 if (rc) {
123                         RTE_LOG(ERR, PMD, "Failed to update PF VNIC %d.\n", i);
124                         return rc;
125                 }
126         }
127
128         /* Stall all active VFs */
129         for (i = 0; i < bp->pf.active_vfs; i++) {
130                 rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, i,
131                                 rte_pmd_bnxt_set_all_queues_drop_en_cb, &on,
132                                 bnxt_hwrm_vnic_cfg);
133                 if (rc) {
134                         RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", i);
135                         break;
136                 }
137         }
138
139         return rc;
140 }
141
142 int rte_pmd_bnxt_set_vf_mac_addr(uint8_t port, uint16_t vf,
143                                 struct ether_addr *mac_addr)
144 {
145         struct rte_eth_dev *dev;
146         struct rte_eth_dev_info dev_info;
147         struct bnxt *bp;
148         int rc;
149
150         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
151
152         dev = &rte_eth_devices[port];
153         if (!is_bnxt_supported(dev))
154                 return -ENOTSUP;
155
156         rte_eth_dev_info_get(port, &dev_info);
157         bp = (struct bnxt *)dev->data->dev_private;
158
159         if (vf >= dev_info.max_vfs || mac_addr == NULL)
160                 return -EINVAL;
161
162         if (!BNXT_PF(bp)) {
163                 RTE_LOG(ERR, PMD,
164                         "Attempt to set VF %d mac address on non-PF port %d!\n",
165                         vf, port);
166                 return -ENOTSUP;
167         }
168
169         rc = bnxt_hwrm_func_vf_mac(bp, vf, (uint8_t *)mac_addr);
170
171         return rc;
172 }