039c798e0a069f0c668facdae143d159081c5006
[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 rte_pmd_bnxt_set_tx_loopback(uint8_t port, uint8_t on)
52 {
53         struct rte_eth_dev *eth_dev;
54         struct bnxt *bp;
55         int rc;
56
57         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
58
59         if (on > 1)
60                 return -EINVAL;
61
62         eth_dev = &rte_eth_devices[port];
63         if (!is_bnxt_supported(eth_dev))
64                 return -ENOTSUP;
65
66         bp = (struct bnxt *)eth_dev->data->dev_private;
67
68         if (!BNXT_PF(bp)) {
69                 RTE_LOG(ERR, PMD,
70                         "Attempt to set Tx loopback on non-PF port %d!\n",
71                         port);
72                 return -ENOTSUP;
73         }
74
75         if (on)
76                 bp->pf.evb_mode = BNXT_EVB_MODE_VEB;
77         else
78                 bp->pf.evb_mode = BNXT_EVB_MODE_VEPA;
79
80         rc = bnxt_hwrm_pf_evb_mode(bp);
81
82         return rc;
83 }
84
85 static void
86 rte_pmd_bnxt_set_all_queues_drop_en_cb(struct bnxt_vnic_info *vnic, void *onptr)
87 {
88         uint8_t *on = onptr;
89         vnic->bd_stall = !(*on);
90 }
91
92 int rte_pmd_bnxt_set_all_queues_drop_en(uint8_t port, uint8_t on)
93 {
94         struct rte_eth_dev *eth_dev;
95         struct bnxt *bp;
96         uint32_t i;
97         int rc;
98
99         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
100
101         if (on > 1)
102                 return -EINVAL;
103
104         eth_dev = &rte_eth_devices[port];
105         if (!is_bnxt_supported(eth_dev))
106                 return -ENOTSUP;
107
108         bp = (struct bnxt *)eth_dev->data->dev_private;
109
110         if (!BNXT_PF(bp)) {
111                 RTE_LOG(ERR, PMD,
112                         "Attempt to set all queues drop on non-PF port!\n");
113                 return -ENOTSUP;
114         }
115
116         if (bp->vnic_info == NULL)
117                 return -ENODEV;
118
119         /* Stall PF */
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]);
123                 if (rc) {
124                         RTE_LOG(ERR, PMD, "Failed to update PF VNIC %d.\n", i);
125                         return rc;
126                 }
127         }
128
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,
133                                 bnxt_hwrm_vnic_cfg);
134                 if (rc) {
135                         RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", i);
136                         break;
137                 }
138         }
139
140         return rc;
141 }
142
143 int rte_pmd_bnxt_set_vf_mac_addr(uint8_t port, uint16_t vf,
144                                 struct ether_addr *mac_addr)
145 {
146         struct rte_eth_dev *dev;
147         struct rte_eth_dev_info dev_info;
148         struct bnxt *bp;
149         int rc;
150
151         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
152
153         dev = &rte_eth_devices[port];
154         if (!is_bnxt_supported(dev))
155                 return -ENOTSUP;
156
157         rte_eth_dev_info_get(port, &dev_info);
158         bp = (struct bnxt *)dev->data->dev_private;
159
160         if (vf >= dev_info.max_vfs || mac_addr == NULL)
161                 return -EINVAL;
162
163         if (!BNXT_PF(bp)) {
164                 RTE_LOG(ERR, PMD,
165                         "Attempt to set VF %d mac address on non-PF port %d!\n",
166                         vf, port);
167                 return -ENOTSUP;
168         }
169
170         rc = bnxt_hwrm_func_vf_mac(bp, vf, (uint8_t *)mac_addr);
171
172         return rc;
173 }
174
175 int rte_pmd_bnxt_set_vf_rate_limit(uint8_t port, uint16_t vf,
176                                 uint16_t tx_rate, uint64_t q_msk)
177 {
178         struct rte_eth_dev *eth_dev;
179         struct rte_eth_dev_info dev_info;
180         struct bnxt *bp;
181         uint16_t tot_rate = 0;
182         uint64_t idx;
183         int rc;
184
185         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
186
187         eth_dev = &rte_eth_devices[port];
188         if (!is_bnxt_supported(eth_dev))
189                 return -ENOTSUP;
190
191         rte_eth_dev_info_get(port, &dev_info);
192         bp = (struct bnxt *)eth_dev->data->dev_private;
193
194         if (!bp->pf.active_vfs)
195                 return -EINVAL;
196
197         if (vf >= bp->pf.max_vfs)
198                 return -EINVAL;
199
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)
203                         tot_rate += tx_rate;
204         }
205
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);
209                 return -EINVAL;
210         }
211
212         /* Requested BW already configured */
213         if (tot_rate == bp->pf.vf_info[vf].max_tx_rate)
214                 return 0;
215
216         rc = bnxt_hwrm_func_bw_cfg(bp, vf, tot_rate,
217                                 HWRM_FUNC_CFG_INPUT_ENABLES_MAX_BW);
218
219         if (!rc)
220                 bp->pf.vf_info[vf].max_tx_rate = tot_rate;
221
222         return rc;
223 }
224
225 int rte_pmd_bnxt_set_vf_mac_anti_spoof(uint8_t port, uint16_t vf, uint8_t on)
226 {
227         struct rte_eth_dev_info dev_info;
228         struct rte_eth_dev *dev;
229         uint32_t func_flags;
230         struct bnxt *bp;
231         int rc;
232
233         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
234
235         if (on > 1)
236                 return -EINVAL;
237
238         dev = &rte_eth_devices[port];
239         if (!is_bnxt_supported(dev))
240                 return -ENOTSUP;
241
242         rte_eth_dev_info_get(port, &dev_info);
243         bp = (struct bnxt *)dev->data->dev_private;
244
245         if (!BNXT_PF(bp)) {
246                 RTE_LOG(ERR, PMD,
247                         "Attempt to set mac spoof on non-PF port %d!\n", port);
248                 return -EINVAL;
249         }
250
251         if (vf >= dev_info.max_vfs)
252                 return -EINVAL;
253
254         /* Prev setting same as new setting. */
255         if (on == bp->pf.vf_info[vf].mac_spoof_en)
256                 return 0;
257
258         func_flags = bp->pf.vf_info[vf].func_cfg_flags;
259
260         if (on)
261                 func_flags |=
262                         HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_ENABLE;
263         else
264                 func_flags |=
265                         HWRM_FUNC_CFG_INPUT_FLAGS_SRC_MAC_ADDR_CHECK_DISABLE;
266
267         bp->pf.vf_info[vf].func_cfg_flags = func_flags;
268
269         rc = bnxt_hwrm_func_cfg_vf_set_flags(bp, vf);
270         if (!rc)
271                 bp->pf.vf_info[vf].mac_spoof_en = on;
272
273         return rc;
274 }
275
276 int rte_pmd_bnxt_set_vf_vlan_anti_spoof(uint8_t port, uint16_t vf, uint8_t on)
277 {
278         struct rte_eth_dev_info dev_info;
279         struct rte_eth_dev *dev;
280         struct bnxt *bp;
281         int rc;
282         int dflt_vnic;
283         struct bnxt_vnic_info vnic;
284
285         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
286
287         if (on > 1)
288                 return -EINVAL;
289
290         dev = &rte_eth_devices[port];
291         if (!is_bnxt_supported(dev))
292                 return -ENOTSUP;
293
294         rte_eth_dev_info_get(port, &dev_info);
295         bp = (struct bnxt *)dev->data->dev_private;
296
297         if (!BNXT_PF(bp)) {
298                 RTE_LOG(ERR, PMD,
299                         "Attempt to set mac spoof on non-PF port %d!\n", port);
300                 return -EINVAL;
301         }
302
303         if (vf >= dev_info.max_vfs)
304                 return -EINVAL;
305
306         rc = bnxt_hwrm_func_cfg_vf_set_vlan_anti_spoof(bp, vf, on);
307         if (!rc) {
308                 bp->pf.vf_info[vf].vlan_spoof_en = on;
309                 if (on) {
310                         dflt_vnic = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf);
311                         if (dflt_vnic < 0) {
312                                 /*
313                                  * This simply indicates there's no driver
314                                  * loaded.  This is not an error.
315                                  */
316                                 RTE_LOG(ERR, PMD,
317                                       "Unable to get default VNIC for VF %d\n",
318                                         vf);
319                         } else {
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))
326                                                 rc = -1;
327                                 }
328                         }
329                 }
330         } else {
331                 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", vf);
332         }
333
334         return rc;
335 }
336
337 static void
338 rte_pmd_bnxt_set_vf_vlan_stripq_cb(struct bnxt_vnic_info *vnic, void *onptr)
339 {
340         uint8_t *on = onptr;
341         vnic->vlan_strip = *on;
342 }
343
344 int
345 rte_pmd_bnxt_set_vf_vlan_stripq(uint8_t port, uint16_t vf, uint8_t on)
346 {
347         struct rte_eth_dev *dev;
348         struct rte_eth_dev_info dev_info;
349         struct bnxt *bp;
350         int rc;
351
352         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
353
354         dev = &rte_eth_devices[port];
355         if (!is_bnxt_supported(dev))
356                 return -ENOTSUP;
357
358         rte_eth_dev_info_get(port, &dev_info);
359         bp = (struct bnxt *)dev->data->dev_private;
360
361         if (vf >= dev_info.max_vfs)
362                 return -EINVAL;
363
364         if (!BNXT_PF(bp)) {
365                 RTE_LOG(ERR, PMD,
366                         "Attempt to set VF %d stripq on non-PF port %d!\n",
367                         vf, port);
368                 return -ENOTSUP;
369         }
370
371         rc = bnxt_hwrm_func_vf_vnic_query_and_config(bp, vf,
372                                 rte_pmd_bnxt_set_vf_vlan_stripq_cb, &on,
373                                 bnxt_hwrm_vnic_cfg);
374         if (rc)
375                 RTE_LOG(ERR, PMD, "Failed to update VF VNIC %d.\n", vf);
376
377         return rc;
378 }
379
380 int rte_pmd_bnxt_set_vf_vlan_filter(uint8_t port, uint16_t vlan,
381                                     uint64_t vf_mask, uint8_t vlan_on)
382 {
383         struct bnxt_vlan_table_entry *ve;
384         struct rte_eth_dev *dev;
385         struct bnxt *bp;
386         uint16_t cnt;
387         int rc = 0;
388         int i, j;
389
390         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
391
392         dev = &rte_eth_devices[port];
393         if (!is_bnxt_supported(dev))
394                 return -ENOTSUP;
395
396         bp = (struct bnxt *)dev->data->dev_private;
397         if (!bp->pf.vf_info)
398                 return -EINVAL;
399
400         for (i = 0; vf_mask; i++, vf_mask >>= 1) {
401                 cnt = bp->pf.vf_info[i].vlan_count;
402                 if (vf_mask & 1) {
403                         if (bp->pf.vf_info[i].vlan_table == NULL) {
404                                 rc = -1;
405                                 continue;
406                         }
407                         if (vlan_on) {
408                                 /* First, search for a duplicate... */
409                                 for (j = 0; j < cnt; j++) {
410                                         if (rte_be_to_cpu_16(
411                                          bp->pf.vf_info[i].vlan_table[j].vid) ==
412                                             vlan)
413                                                 break;
414                                 }
415                                 if (j == cnt) {
416                                         /* Now check that there's space */
417                                         if (cnt == getpagesize() /
418                                          sizeof(struct bnxt_vlan_table_entry)) {
419                                                 RTE_LOG(ERR, PMD,
420                                                   "VF %d VLAN table is full\n",
421                                                   i);
422                                                 RTE_LOG(ERR, PMD,
423                                                         "cannot add VLAN %u\n",
424                                                         vlan);
425                                                 rc = -1;
426                                                 continue;
427                                         }
428
429                                         cnt = bp->pf.vf_info[i].vlan_count++;
430                                         /*
431                                          * And finally, add to the
432                                          * end of the table
433                                          */
434                                         ve = &bp->pf.vf_info[i].vlan_table[cnt];
435                                         /* TODO: Hardcoded TPID */
436                                         ve->tpid = rte_cpu_to_be_16(0x8100);
437                                         ve->vid = rte_cpu_to_be_16(vlan);
438                                 }
439                         } else {
440                                 for (j = 0; cnt; j++) {
441                                         if (rte_be_to_cpu_16(
442                                         bp->pf.vf_info[i].vlan_table[j].vid) !=
443                                             vlan)
444                                                 continue;
445                                         memmove(
446                                          &bp->pf.vf_info[i].vlan_table[j],
447                                          &bp->pf.vf_info[i].vlan_table[j + 1],
448                                          getpagesize() -
449                                          ((j + 1) *
450                                          sizeof(struct bnxt_vlan_table_entry)));
451                                         j--;
452                                         cnt = bp->pf.vf_info[i].vlan_count--;
453                                 }
454                         }
455                         rte_pmd_bnxt_set_vf_vlan_anti_spoof(dev->data->port_id,
456                                          i, bp->pf.vf_info[i].vlan_spoof_en);
457                 }
458         }
459
460         return rc;
461 }