net/bnxt: support to set VF rxmode
[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_rxmode(uint8_t port, uint16_t vf,
381                                 uint16_t rx_mask, uint8_t on)
382 {
383         struct rte_eth_dev *dev;
384         struct rte_eth_dev_info dev_info;
385         uint16_t flag = 0;
386         struct bnxt *bp;
387         int rc;
388
389         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
390
391         dev = &rte_eth_devices[port];
392         if (!is_bnxt_supported(dev))
393                 return -ENOTSUP;
394
395         rte_eth_dev_info_get(port, &dev_info);
396         bp = (struct bnxt *)dev->data->dev_private;
397
398         if (!bp->pf.vf_info)
399                 return -EINVAL;
400
401         if (vf >= bp->pdev->max_vfs)
402                 return -EINVAL;
403
404         if (rx_mask & (ETH_VMDQ_ACCEPT_UNTAG | ETH_VMDQ_ACCEPT_HASH_MC)) {
405                 RTE_LOG(ERR, PMD, "Currently cannot toggle this setting\n");
406                 return -ENOTSUP;
407         }
408
409         if (rx_mask & ETH_VMDQ_ACCEPT_HASH_UC && !on) {
410                 RTE_LOG(ERR, PMD, "Currently cannot disable UC Rx\n");
411                 return -ENOTSUP;
412         }
413
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;
418
419         if (on)
420                 bp->pf.vf_info[vf].l2_rx_mask |= flag;
421         else
422                 bp->pf.vf_info[vf].l2_rx_mask &= ~flag;
423
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);
428         if (rc)
429                 RTE_LOG(ERR, PMD, "bnxt_hwrm_func_vf_vnic_set_rxmask failed\n");
430
431         return rc;
432 }
433
434 int rte_pmd_bnxt_set_vf_vlan_filter(uint8_t port, uint16_t vlan,
435                                     uint64_t vf_mask, uint8_t vlan_on)
436 {
437         struct bnxt_vlan_table_entry *ve;
438         struct rte_eth_dev *dev;
439         struct bnxt *bp;
440         uint16_t cnt;
441         int rc = 0;
442         int i, j;
443
444         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
445
446         dev = &rte_eth_devices[port];
447         if (!is_bnxt_supported(dev))
448                 return -ENOTSUP;
449
450         bp = (struct bnxt *)dev->data->dev_private;
451         if (!bp->pf.vf_info)
452                 return -EINVAL;
453
454         for (i = 0; vf_mask; i++, vf_mask >>= 1) {
455                 cnt = bp->pf.vf_info[i].vlan_count;
456                 if (vf_mask & 1) {
457                         if (bp->pf.vf_info[i].vlan_table == NULL) {
458                                 rc = -1;
459                                 continue;
460                         }
461                         if (vlan_on) {
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) ==
466                                             vlan)
467                                                 break;
468                                 }
469                                 if (j == cnt) {
470                                         /* Now check that there's space */
471                                         if (cnt == getpagesize() /
472                                          sizeof(struct bnxt_vlan_table_entry)) {
473                                                 RTE_LOG(ERR, PMD,
474                                                   "VF %d VLAN table is full\n",
475                                                   i);
476                                                 RTE_LOG(ERR, PMD,
477                                                         "cannot add VLAN %u\n",
478                                                         vlan);
479                                                 rc = -1;
480                                                 continue;
481                                         }
482
483                                         cnt = bp->pf.vf_info[i].vlan_count++;
484                                         /*
485                                          * And finally, add to the
486                                          * end of the table
487                                          */
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);
492                                 }
493                         } else {
494                                 for (j = 0; cnt; j++) {
495                                         if (rte_be_to_cpu_16(
496                                         bp->pf.vf_info[i].vlan_table[j].vid) !=
497                                             vlan)
498                                                 continue;
499                                         memmove(
500                                          &bp->pf.vf_info[i].vlan_table[j],
501                                          &bp->pf.vf_info[i].vlan_table[j + 1],
502                                          getpagesize() -
503                                          ((j + 1) *
504                                          sizeof(struct bnxt_vlan_table_entry)));
505                                         j--;
506                                         cnt = bp->pf.vf_info[i].vlan_count--;
507                                 }
508                         }
509                         rte_pmd_bnxt_set_vf_vlan_anti_spoof(dev->data->port_id,
510                                          i, bp->pf.vf_info[i].vlan_spoof_en);
511                 }
512         }
513
514         return rc;
515 }
516
517 int rte_pmd_bnxt_get_vf_stats(uint8_t port,
518                               uint16_t vf_id,
519                               struct rte_eth_stats *stats)
520 {
521         struct rte_eth_dev *dev;
522         struct rte_eth_dev_info dev_info;
523         struct bnxt *bp;
524
525         dev = &rte_eth_devices[port];
526         if (!is_bnxt_supported(dev))
527                 return -ENOTSUP;
528
529         rte_eth_dev_info_get(port, &dev_info);
530         bp = (struct bnxt *)dev->data->dev_private;
531
532         if (vf_id >= dev_info.max_vfs)
533                 return -EINVAL;
534
535         if (!BNXT_PF(bp)) {
536                 RTE_LOG(ERR, PMD,
537                         "Attempt to get VF %d stats on non-PF port %d!\n",
538                         vf_id, port);
539                 return -ENOTSUP;
540         }
541
542         return bnxt_hwrm_func_qstats(bp, bp->pf.first_vf_id + vf_id, stats);
543 }
544
545 int rte_pmd_bnxt_reset_vf_stats(uint8_t port,
546                                 uint16_t vf_id)
547 {
548         struct rte_eth_dev *dev;
549         struct rte_eth_dev_info dev_info;
550         struct bnxt *bp;
551
552         dev = &rte_eth_devices[port];
553         if (!is_bnxt_supported(dev))
554                 return -ENOTSUP;
555
556         rte_eth_dev_info_get(port, &dev_info);
557         bp = (struct bnxt *)dev->data->dev_private;
558
559         if (vf_id >= dev_info.max_vfs)
560                 return -EINVAL;
561
562         if (!BNXT_PF(bp)) {
563                 RTE_LOG(ERR, PMD,
564                         "Attempt to reset VF %d stats on non-PF port %d!\n",
565                         vf_id, port);
566                 return -ENOTSUP;
567         }
568
569         return bnxt_hwrm_func_clr_stats(bp, bp->pf.first_vf_id + vf_id);
570 }
571
572 int rte_pmd_bnxt_get_vf_rx_status(uint8_t port, uint16_t vf_id)
573 {
574         struct rte_eth_dev *dev;
575         struct rte_eth_dev_info dev_info;
576         struct bnxt *bp;
577
578         dev = &rte_eth_devices[port];
579         if (!is_bnxt_supported(dev))
580                 return -ENOTSUP;
581
582         rte_eth_dev_info_get(port, &dev_info);
583         bp = (struct bnxt *)dev->data->dev_private;
584
585         if (vf_id >= dev_info.max_vfs)
586                 return -EINVAL;
587
588         if (!BNXT_PF(bp)) {
589                 RTE_LOG(ERR, PMD,
590                         "Attempt to query VF %d RX stats on non-PF port %d!\n",
591                         vf_id, port);
592                 return -ENOTSUP;
593         }
594
595         return bnxt_vf_default_vnic_count(bp, vf_id);
596 }
597
598 int rte_pmd_bnxt_get_vf_tx_drop_count(uint8_t port, uint16_t vf_id,
599                                       uint64_t *count)
600 {
601         struct rte_eth_dev *dev;
602         struct rte_eth_dev_info dev_info;
603         struct bnxt *bp;
604
605         dev = &rte_eth_devices[port];
606         if (!is_bnxt_supported(dev))
607                 return -ENOTSUP;
608
609         rte_eth_dev_info_get(port, &dev_info);
610         bp = (struct bnxt *)dev->data->dev_private;
611
612         if (vf_id >= dev_info.max_vfs)
613                 return -EINVAL;
614
615         if (!BNXT_PF(bp)) {
616                 RTE_LOG(ERR, PMD,
617                         "Attempt to query VF %d TX drops on non-PF port %d!\n",
618                         vf_id, port);
619                 return -ENOTSUP;
620         }
621
622         return bnxt_hwrm_func_qstats_tx_drop(bp, bp->pf.first_vf_id + vf_id,
623                                              count);
624 }
625
626 int rte_pmd_bnxt_mac_addr_add(uint8_t port, struct ether_addr *addr,
627                                 uint32_t vf_id)
628 {
629         struct rte_eth_dev *dev;
630         struct rte_eth_dev_info dev_info;
631         struct bnxt *bp;
632         struct bnxt_filter_info *filter;
633         struct bnxt_vnic_info vnic;
634         struct ether_addr dflt_mac;
635         int rc;
636
637         dev = &rte_eth_devices[port];
638         if (!is_bnxt_supported(dev))
639                 return -ENOTSUP;
640
641         rte_eth_dev_info_get(port, &dev_info);
642         bp = (struct bnxt *)dev->data->dev_private;
643
644         if (vf_id >= dev_info.max_vfs)
645                 return -EINVAL;
646
647         if (!BNXT_PF(bp)) {
648                 RTE_LOG(ERR, PMD,
649                         "Attempt to config VF %d MAC on non-PF port %d!\n",
650                         vf_id, port);
651                 return -ENOTSUP;
652         }
653
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);
658         }
659
660         /* query the default VNIC id used by the function */
661         rc = bnxt_hwrm_func_qcfg_vf_dflt_vnic_id(bp, vf_id);
662         if (rc < 0)
663                 goto exit;
664
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);
668         if (rc < 0)
669                 goto exit;
670
671         STAILQ_FOREACH(filter, &bp->pf.vf_info[vf_id].filter, next) {
672                 if (filter->flags ==
673                     HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX &&
674                     filter->enables ==
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);
679                         break;
680                 }
681         }
682
683         if (filter == NULL)
684                 filter = bnxt_alloc_vf_filter(bp, vf_id);
685
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);
692
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);
697
698 exit:
699         return rc;
700 }
701
702 int
703 rte_pmd_bnxt_set_vf_vlan_insert(uint8_t port, uint16_t vf,
704                 uint16_t vlan_id)
705 {
706         struct rte_eth_dev *dev;
707         struct rte_eth_dev_info dev_info;
708         struct bnxt *bp;
709         int rc;
710
711         RTE_ETH_VALID_PORTID_OR_ERR_RET(port, -ENODEV);
712
713         dev = &rte_eth_devices[port];
714         if (!is_bnxt_supported(dev))
715                 return -ENOTSUP;
716
717         rte_eth_dev_info_get(port, &dev_info);
718         bp = (struct bnxt *)dev->data->dev_private;
719
720         if (vf >= dev_info.max_vfs)
721                 return -EINVAL;
722
723         if (!BNXT_PF(bp)) {
724                 RTE_LOG(ERR, PMD,
725                         "Attempt to set VF %d vlan insert on non-PF port %d!\n",
726                         vf, port);
727                 return -ENOTSUP;
728         }
729
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)
733                 return 0;
734
735         rc = bnxt_hwrm_set_vf_vlan(bp, vf);
736
737         return rc;
738 }