12888e6a9f03cc0d5c7d96d4d03d8fe1496aadee
[dpdk.git] / drivers / net / bnxt / bnxt_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 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_ethdev_pci.h>
40 #include <rte_malloc.h>
41 #include <rte_cycles.h>
42
43 #include "bnxt.h"
44 #include "bnxt_cpr.h"
45 #include "bnxt_filter.h"
46 #include "bnxt_hwrm.h"
47 #include "bnxt_irq.h"
48 #include "bnxt_ring.h"
49 #include "bnxt_rxq.h"
50 #include "bnxt_rxr.h"
51 #include "bnxt_stats.h"
52 #include "bnxt_txq.h"
53 #include "bnxt_txr.h"
54 #include "bnxt_vnic.h"
55 #include "hsi_struct_def_dpdk.h"
56
57 #define DRV_MODULE_NAME         "bnxt"
58 static const char bnxt_version[] =
59         "Broadcom Cumulus driver " DRV_MODULE_NAME "\n";
60
61 #define PCI_VENDOR_ID_BROADCOM 0x14E4
62
63 #define BROADCOM_DEV_ID_STRATUS_NIC_VF 0x1609
64 #define BROADCOM_DEV_ID_STRATUS_NIC 0x1614
65 #define BROADCOM_DEV_ID_57414_VF 0x16c1
66 #define BROADCOM_DEV_ID_57301 0x16c8
67 #define BROADCOM_DEV_ID_57302 0x16c9
68 #define BROADCOM_DEV_ID_57304_PF 0x16ca
69 #define BROADCOM_DEV_ID_57304_VF 0x16cb
70 #define BROADCOM_DEV_ID_57417_MF 0x16cc
71 #define BROADCOM_DEV_ID_NS2 0x16cd
72 #define BROADCOM_DEV_ID_57311 0x16ce
73 #define BROADCOM_DEV_ID_57312 0x16cf
74 #define BROADCOM_DEV_ID_57402 0x16d0
75 #define BROADCOM_DEV_ID_57404 0x16d1
76 #define BROADCOM_DEV_ID_57406_PF 0x16d2
77 #define BROADCOM_DEV_ID_57406_VF 0x16d3
78 #define BROADCOM_DEV_ID_57402_MF 0x16d4
79 #define BROADCOM_DEV_ID_57407_RJ45 0x16d5
80 #define BROADCOM_DEV_ID_57412 0x16d6
81 #define BROADCOM_DEV_ID_57414 0x16d7
82 #define BROADCOM_DEV_ID_57416_RJ45 0x16d8
83 #define BROADCOM_DEV_ID_57417_RJ45 0x16d9
84 #define BROADCOM_DEV_ID_5741X_VF 0x16dc
85 #define BROADCOM_DEV_ID_57412_MF 0x16de
86 #define BROADCOM_DEV_ID_57314 0x16df
87 #define BROADCOM_DEV_ID_57317_RJ45 0x16e0
88 #define BROADCOM_DEV_ID_5731X_VF 0x16e1
89 #define BROADCOM_DEV_ID_57417_SFP 0x16e2
90 #define BROADCOM_DEV_ID_57416_SFP 0x16e3
91 #define BROADCOM_DEV_ID_57317_SFP 0x16e4
92 #define BROADCOM_DEV_ID_57404_MF 0x16e7
93 #define BROADCOM_DEV_ID_57406_MF 0x16e8
94 #define BROADCOM_DEV_ID_57407_SFP 0x16e9
95 #define BROADCOM_DEV_ID_57407_MF 0x16ea
96 #define BROADCOM_DEV_ID_57414_MF 0x16ec
97 #define BROADCOM_DEV_ID_57416_MF 0x16ee
98
99 static const struct rte_pci_id bnxt_pci_id_map[] = {
100         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM,
101                          BROADCOM_DEV_ID_STRATUS_NIC_VF) },
102         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_STRATUS_NIC) },
103         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414_VF) },
104         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57301) },
105         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57302) },
106         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57304_PF) },
107         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57304_VF) },
108         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_NS2) },
109         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57402) },
110         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57404) },
111         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57406_PF) },
112         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57406_VF) },
113         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57402_MF) },
114         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57407_RJ45) },
115         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57404_MF) },
116         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57406_MF) },
117         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57407_SFP) },
118         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57407_MF) },
119         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_5741X_VF) },
120         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_5731X_VF) },
121         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57314) },
122         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_MF) },
123         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57311) },
124         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57312) },
125         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57412) },
126         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414) },
127         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_RJ45) },
128         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_RJ45) },
129         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57412_MF) },
130         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57317_RJ45) },
131         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_SFP) },
132         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_SFP) },
133         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57317_SFP) },
134         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414_MF) },
135         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_MF) },
136         { .vendor_id = 0, /* sentinel */ },
137 };
138
139 #define BNXT_ETH_RSS_SUPPORT (  \
140         ETH_RSS_IPV4 |          \
141         ETH_RSS_NONFRAG_IPV4_TCP |      \
142         ETH_RSS_NONFRAG_IPV4_UDP |      \
143         ETH_RSS_IPV6 |          \
144         ETH_RSS_NONFRAG_IPV6_TCP |      \
145         ETH_RSS_NONFRAG_IPV6_UDP)
146
147 static void bnxt_vlan_offload_set_op(struct rte_eth_dev *dev, int mask);
148
149 /***********************/
150
151 /*
152  * High level utility functions
153  */
154
155 static void bnxt_free_mem(struct bnxt *bp)
156 {
157         bnxt_free_filter_mem(bp);
158         bnxt_free_vnic_attributes(bp);
159         bnxt_free_vnic_mem(bp);
160
161         bnxt_free_stats(bp);
162         bnxt_free_tx_rings(bp);
163         bnxt_free_rx_rings(bp);
164         bnxt_free_def_cp_ring(bp);
165 }
166
167 static int bnxt_alloc_mem(struct bnxt *bp)
168 {
169         int rc;
170
171         /* Default completion ring */
172         rc = bnxt_init_def_ring_struct(bp, SOCKET_ID_ANY);
173         if (rc)
174                 goto alloc_mem_err;
175
176         rc = bnxt_alloc_rings(bp, 0, NULL, NULL,
177                               bp->def_cp_ring, "def_cp");
178         if (rc)
179                 goto alloc_mem_err;
180
181         rc = bnxt_alloc_vnic_mem(bp);
182         if (rc)
183                 goto alloc_mem_err;
184
185         rc = bnxt_alloc_vnic_attributes(bp);
186         if (rc)
187                 goto alloc_mem_err;
188
189         rc = bnxt_alloc_filter_mem(bp);
190         if (rc)
191                 goto alloc_mem_err;
192
193         return 0;
194
195 alloc_mem_err:
196         bnxt_free_mem(bp);
197         return rc;
198 }
199
200 static int bnxt_init_chip(struct bnxt *bp)
201 {
202         unsigned int i, rss_idx, fw_idx;
203         struct rte_eth_link new;
204         int rc;
205
206         if (bp->eth_dev->data->mtu > ETHER_MTU) {
207                 bp->eth_dev->data->dev_conf.rxmode.jumbo_frame = 1;
208                 bp->flags |= BNXT_FLAG_JUMBO;
209         } else {
210                 bp->eth_dev->data->dev_conf.rxmode.jumbo_frame = 0;
211                 bp->flags &= ~BNXT_FLAG_JUMBO;
212         }
213
214         rc = bnxt_alloc_all_hwrm_stat_ctxs(bp);
215         if (rc) {
216                 RTE_LOG(ERR, PMD, "HWRM stat ctx alloc failure rc: %x\n", rc);
217                 goto err_out;
218         }
219
220         rc = bnxt_alloc_hwrm_rings(bp);
221         if (rc) {
222                 RTE_LOG(ERR, PMD, "HWRM ring alloc failure rc: %x\n", rc);
223                 goto err_out;
224         }
225
226         rc = bnxt_alloc_all_hwrm_ring_grps(bp);
227         if (rc) {
228                 RTE_LOG(ERR, PMD, "HWRM ring grp alloc failure: %x\n", rc);
229                 goto err_out;
230         }
231
232         rc = bnxt_mq_rx_configure(bp);
233         if (rc) {
234                 RTE_LOG(ERR, PMD, "MQ mode configure failure rc: %x\n", rc);
235                 goto err_out;
236         }
237
238         /* VNIC configuration */
239         for (i = 0; i < bp->nr_vnics; i++) {
240                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
241
242                 rc = bnxt_hwrm_vnic_alloc(bp, vnic);
243                 if (rc) {
244                         RTE_LOG(ERR, PMD, "HWRM vnic %d alloc failure rc: %x\n",
245                                 i, rc);
246                         goto err_out;
247                 }
248
249                 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic);
250                 if (rc) {
251                         RTE_LOG(ERR, PMD,
252                                 "HWRM vnic %d ctx alloc failure rc: %x\n",
253                                 i, rc);
254                         goto err_out;
255                 }
256
257                 rc = bnxt_hwrm_vnic_cfg(bp, vnic);
258                 if (rc) {
259                         RTE_LOG(ERR, PMD, "HWRM vnic %d cfg failure rc: %x\n",
260                                 i, rc);
261                         goto err_out;
262                 }
263
264                 rc = bnxt_set_hwrm_vnic_filters(bp, vnic);
265                 if (rc) {
266                         RTE_LOG(ERR, PMD,
267                                 "HWRM vnic %d filter failure rc: %x\n",
268                                 i, rc);
269                         goto err_out;
270                 }
271                 if (vnic->rss_table && vnic->hash_type) {
272                         /*
273                          * Fill the RSS hash & redirection table with
274                          * ring group ids for all VNICs
275                          */
276                         for (rss_idx = 0, fw_idx = 0;
277                              rss_idx < HW_HASH_INDEX_SIZE;
278                              rss_idx++, fw_idx++) {
279                                 if (vnic->fw_grp_ids[fw_idx] ==
280                                     INVALID_HW_RING_ID)
281                                         fw_idx = 0;
282                                 vnic->rss_table[rss_idx] =
283                                                 vnic->fw_grp_ids[fw_idx];
284                         }
285                         rc = bnxt_hwrm_vnic_rss_cfg(bp, vnic);
286                         if (rc) {
287                                 RTE_LOG(ERR, PMD,
288                                         "HWRM vnic %d set RSS failure rc: %x\n",
289                                         i, rc);
290                                 goto err_out;
291                         }
292                 }
293
294                 bnxt_hwrm_vnic_plcmode_cfg(bp, vnic);
295
296                 if (bp->eth_dev->data->dev_conf.rxmode.enable_lro)
297                         bnxt_hwrm_vnic_tpa_cfg(bp, vnic, 1);
298                 else
299                         bnxt_hwrm_vnic_tpa_cfg(bp, vnic, 0);
300         }
301         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, &bp->vnic_info[0], 0, NULL);
302         if (rc) {
303                 RTE_LOG(ERR, PMD,
304                         "HWRM cfa l2 rx mask failure rc: %x\n", rc);
305                 goto err_out;
306         }
307
308         rc = bnxt_get_hwrm_link_config(bp, &new);
309         if (rc) {
310                 RTE_LOG(ERR, PMD, "HWRM Get link config failure rc: %x\n", rc);
311                 goto err_out;
312         }
313
314         if (!bp->link_info.link_up) {
315                 rc = bnxt_set_hwrm_link_config(bp, true);
316                 if (rc) {
317                         RTE_LOG(ERR, PMD,
318                                 "HWRM link config failure rc: %x\n", rc);
319                         goto err_out;
320                 }
321         }
322
323         return 0;
324
325 err_out:
326         bnxt_free_all_hwrm_resources(bp);
327
328         return rc;
329 }
330
331 static int bnxt_shutdown_nic(struct bnxt *bp)
332 {
333         bnxt_free_all_hwrm_resources(bp);
334         bnxt_free_all_filters(bp);
335         bnxt_free_all_vnics(bp);
336         return 0;
337 }
338
339 static int bnxt_init_nic(struct bnxt *bp)
340 {
341         int rc;
342
343         bnxt_init_ring_grps(bp);
344         bnxt_init_vnics(bp);
345         bnxt_init_filters(bp);
346
347         rc = bnxt_init_chip(bp);
348         if (rc)
349                 return rc;
350
351         return 0;
352 }
353
354 /*
355  * Device configuration and status function
356  */
357
358 static void bnxt_dev_info_get_op(struct rte_eth_dev *eth_dev,
359                                   struct rte_eth_dev_info *dev_info)
360 {
361         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
362         uint16_t max_vnics, i, j, vpool, vrxq;
363         unsigned int max_rx_rings;
364
365         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
366
367         /* MAC Specifics */
368         dev_info->max_mac_addrs = MAX_NUM_MAC_ADDR;
369         dev_info->max_hash_mac_addrs = 0;
370
371         /* PF/VF specifics */
372         if (BNXT_PF(bp))
373                 dev_info->max_vfs = bp->pdev->max_vfs;
374         max_rx_rings = RTE_MIN(bp->max_vnics, RTE_MIN(bp->max_l2_ctx,
375                                                 RTE_MIN(bp->max_rsscos_ctx,
376                                                 bp->max_stat_ctx)));
377         /* For the sake of symmetry, max_rx_queues = max_tx_queues */
378         dev_info->max_rx_queues = max_rx_rings;
379         dev_info->max_tx_queues = max_rx_rings;
380         dev_info->reta_size = bp->max_rsscos_ctx;
381         dev_info->hash_key_size = 40;
382         max_vnics = bp->max_vnics;
383
384         /* Fast path specifics */
385         dev_info->min_rx_bufsize = 1;
386         dev_info->max_rx_pktlen = BNXT_MAX_MTU + ETHER_HDR_LEN + ETHER_CRC_LEN
387                                   + VLAN_TAG_SIZE;
388         dev_info->rx_offload_capa = 0;
389         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_IPV4_CKSUM |
390                                         DEV_TX_OFFLOAD_TCP_CKSUM |
391                                         DEV_TX_OFFLOAD_UDP_CKSUM |
392                                         DEV_TX_OFFLOAD_TCP_TSO |
393                                         DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
394                                         DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
395                                         DEV_TX_OFFLOAD_GRE_TNL_TSO |
396                                         DEV_TX_OFFLOAD_IPIP_TNL_TSO |
397                                         DEV_TX_OFFLOAD_GENEVE_TNL_TSO;
398
399         /* *INDENT-OFF* */
400         dev_info->default_rxconf = (struct rte_eth_rxconf) {
401                 .rx_thresh = {
402                         .pthresh = 8,
403                         .hthresh = 8,
404                         .wthresh = 0,
405                 },
406                 .rx_free_thresh = 32,
407                 .rx_drop_en = 0,
408         };
409
410         dev_info->default_txconf = (struct rte_eth_txconf) {
411                 .tx_thresh = {
412                         .pthresh = 32,
413                         .hthresh = 0,
414                         .wthresh = 0,
415                 },
416                 .tx_free_thresh = 32,
417                 .tx_rs_thresh = 32,
418                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
419                              ETH_TXQ_FLAGS_NOOFFLOADS,
420         };
421         eth_dev->data->dev_conf.intr_conf.lsc = 1;
422
423         /* *INDENT-ON* */
424
425         /*
426          * TODO: default_rxconf, default_txconf, rx_desc_lim, and tx_desc_lim
427          *       need further investigation.
428          */
429
430         /* VMDq resources */
431         vpool = 64; /* ETH_64_POOLS */
432         vrxq = 128; /* ETH_VMDQ_DCB_NUM_QUEUES */
433         for (i = 0; i < 4; vpool >>= 1, i++) {
434                 if (max_vnics > vpool) {
435                         for (j = 0; j < 5; vrxq >>= 1, j++) {
436                                 if (dev_info->max_rx_queues > vrxq) {
437                                         if (vpool > vrxq)
438                                                 vpool = vrxq;
439                                         goto found;
440                                 }
441                         }
442                         /* Not enough resources to support VMDq */
443                         break;
444                 }
445         }
446         /* Not enough resources to support VMDq */
447         vpool = 0;
448         vrxq = 0;
449 found:
450         dev_info->max_vmdq_pools = vpool;
451         dev_info->vmdq_queue_num = vrxq;
452
453         dev_info->vmdq_pool_base = 0;
454         dev_info->vmdq_queue_base = 0;
455 }
456
457 /* Configure the device based on the configuration provided */
458 static int bnxt_dev_configure_op(struct rte_eth_dev *eth_dev)
459 {
460         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
461
462         bp->rx_queues = (void *)eth_dev->data->rx_queues;
463         bp->tx_queues = (void *)eth_dev->data->tx_queues;
464
465         /* Inherit new configurations */
466         bp->rx_nr_rings = eth_dev->data->nb_rx_queues;
467         bp->tx_nr_rings = eth_dev->data->nb_tx_queues;
468         bp->rx_cp_nr_rings = bp->rx_nr_rings;
469         bp->tx_cp_nr_rings = bp->tx_nr_rings;
470
471         if (eth_dev->data->dev_conf.rxmode.jumbo_frame)
472                 eth_dev->data->mtu =
473                                 eth_dev->data->dev_conf.rxmode.max_rx_pkt_len -
474                                 ETHER_HDR_LEN - ETHER_CRC_LEN - VLAN_TAG_SIZE;
475         return 0;
476 }
477
478 static inline int
479 rte_bnxt_atomic_write_link_status(struct rte_eth_dev *eth_dev,
480                                 struct rte_eth_link *link)
481 {
482         struct rte_eth_link *dst = &eth_dev->data->dev_link;
483         struct rte_eth_link *src = link;
484
485         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
486                                         *(uint64_t *)src) == 0)
487                 return 1;
488
489         return 0;
490 }
491
492 static void bnxt_print_link_info(struct rte_eth_dev *eth_dev)
493 {
494         struct rte_eth_link *link = &eth_dev->data->dev_link;
495
496         if (link->link_status)
497                 RTE_LOG(INFO, PMD, "Port %d Link Up - speed %u Mbps - %s\n",
498                         (uint8_t)(eth_dev->data->port_id),
499                         (uint32_t)link->link_speed,
500                         (link->link_duplex == ETH_LINK_FULL_DUPLEX) ?
501                         ("full-duplex") : ("half-duplex\n"));
502         else
503                 RTE_LOG(INFO, PMD, "Port %d Link Down\n",
504                         (uint8_t)(eth_dev->data->port_id));
505 }
506
507 static int bnxt_dev_lsc_intr_setup(struct rte_eth_dev *eth_dev)
508 {
509         bnxt_print_link_info(eth_dev);
510         return 0;
511 }
512
513 static int bnxt_dev_start_op(struct rte_eth_dev *eth_dev)
514 {
515         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
516         int vlan_mask = 0;
517         int rc;
518
519         bp->dev_stopped = 0;
520
521         rc = bnxt_init_nic(bp);
522         if (rc)
523                 goto error;
524
525         bnxt_link_update_op(eth_dev, 0);
526
527         if (eth_dev->data->dev_conf.rxmode.hw_vlan_filter)
528                 vlan_mask |= ETH_VLAN_FILTER_MASK;
529         if (eth_dev->data->dev_conf.rxmode.hw_vlan_strip)
530                 vlan_mask |= ETH_VLAN_STRIP_MASK;
531         bnxt_vlan_offload_set_op(eth_dev, vlan_mask);
532
533         return 0;
534
535 error:
536         bnxt_shutdown_nic(bp);
537         bnxt_free_tx_mbufs(bp);
538         bnxt_free_rx_mbufs(bp);
539         return rc;
540 }
541
542 static int bnxt_dev_set_link_up_op(struct rte_eth_dev *eth_dev)
543 {
544         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
545
546         eth_dev->data->dev_link.link_status = 1;
547         bnxt_set_hwrm_link_config(bp, true);
548         return 0;
549 }
550
551 static int bnxt_dev_set_link_down_op(struct rte_eth_dev *eth_dev)
552 {
553         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
554
555         eth_dev->data->dev_link.link_status = 0;
556         bnxt_set_hwrm_link_config(bp, false);
557         return 0;
558 }
559
560 /* Unload the driver, release resources */
561 static void bnxt_dev_stop_op(struct rte_eth_dev *eth_dev)
562 {
563         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
564
565         if (bp->eth_dev->data->dev_started) {
566                 /* TBD: STOP HW queues DMA */
567                 eth_dev->data->dev_link.link_status = 0;
568         }
569         bnxt_set_hwrm_link_config(bp, false);
570         bnxt_hwrm_port_clr_stats(bp);
571         bnxt_shutdown_nic(bp);
572         bp->dev_stopped = 1;
573 }
574
575 static void bnxt_dev_close_op(struct rte_eth_dev *eth_dev)
576 {
577         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
578
579         if (bp->dev_stopped == 0)
580                 bnxt_dev_stop_op(eth_dev);
581
582         bnxt_free_tx_mbufs(bp);
583         bnxt_free_rx_mbufs(bp);
584         bnxt_free_mem(bp);
585         if (eth_dev->data->mac_addrs != NULL) {
586                 rte_free(eth_dev->data->mac_addrs);
587                 eth_dev->data->mac_addrs = NULL;
588         }
589         if (bp->grp_info != NULL) {
590                 rte_free(bp->grp_info);
591                 bp->grp_info = NULL;
592         }
593 }
594
595 static void bnxt_mac_addr_remove_op(struct rte_eth_dev *eth_dev,
596                                     uint32_t index)
597 {
598         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
599         uint64_t pool_mask = eth_dev->data->mac_pool_sel[index];
600         struct bnxt_vnic_info *vnic;
601         struct bnxt_filter_info *filter, *temp_filter;
602         int i;
603
604         /*
605          * Loop through all VNICs from the specified filter flow pools to
606          * remove the corresponding MAC addr filter
607          */
608         for (i = 0; i < MAX_FF_POOLS; i++) {
609                 if (!(pool_mask & (1ULL << i)))
610                         continue;
611
612                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
613                         filter = STAILQ_FIRST(&vnic->filter);
614                         while (filter) {
615                                 temp_filter = STAILQ_NEXT(filter, next);
616                                 if (filter->mac_index == index) {
617                                         STAILQ_REMOVE(&vnic->filter, filter,
618                                                       bnxt_filter_info, next);
619                                         bnxt_hwrm_clear_filter(bp, filter);
620                                         filter->mac_index = INVALID_MAC_INDEX;
621                                         memset(&filter->l2_addr, 0,
622                                                ETHER_ADDR_LEN);
623                                         STAILQ_INSERT_TAIL(
624                                                         &bp->free_filter_list,
625                                                         filter, next);
626                                 }
627                                 filter = temp_filter;
628                         }
629                 }
630         }
631 }
632
633 static int bnxt_mac_addr_add_op(struct rte_eth_dev *eth_dev,
634                                 struct ether_addr *mac_addr,
635                                 uint32_t index, uint32_t pool)
636 {
637         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
638         struct bnxt_vnic_info *vnic = STAILQ_FIRST(&bp->ff_pool[pool]);
639         struct bnxt_filter_info *filter;
640
641         if (BNXT_VF(bp)) {
642                 RTE_LOG(ERR, PMD, "Cannot add MAC address to a VF interface\n");
643                 return -ENOTSUP;
644         }
645
646         if (!vnic) {
647                 RTE_LOG(ERR, PMD, "VNIC not found for pool %d!\n", pool);
648                 return -EINVAL;
649         }
650         /* Attach requested MAC address to the new l2_filter */
651         STAILQ_FOREACH(filter, &vnic->filter, next) {
652                 if (filter->mac_index == index) {
653                         RTE_LOG(ERR, PMD,
654                                 "MAC addr already existed for pool %d\n", pool);
655                         return -EINVAL;
656                 }
657         }
658         filter = bnxt_alloc_filter(bp);
659         if (!filter) {
660                 RTE_LOG(ERR, PMD, "L2 filter alloc failed\n");
661                 return -ENODEV;
662         }
663         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
664         filter->mac_index = index;
665         memcpy(filter->l2_addr, mac_addr, ETHER_ADDR_LEN);
666         return bnxt_hwrm_set_filter(bp, vnic->fw_vnic_id, filter);
667 }
668
669 int bnxt_link_update_op(struct rte_eth_dev *eth_dev, int wait_to_complete)
670 {
671         int rc = 0;
672         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
673         struct rte_eth_link new;
674         unsigned int cnt = BNXT_LINK_WAIT_CNT;
675
676         memset(&new, 0, sizeof(new));
677         do {
678                 /* Retrieve link info from hardware */
679                 rc = bnxt_get_hwrm_link_config(bp, &new);
680                 if (rc) {
681                         new.link_speed = ETH_LINK_SPEED_100M;
682                         new.link_duplex = ETH_LINK_FULL_DUPLEX;
683                         RTE_LOG(ERR, PMD,
684                                 "Failed to retrieve link rc = 0x%x!\n", rc);
685                         goto out;
686                 }
687                 rte_delay_ms(BNXT_LINK_WAIT_INTERVAL);
688
689                 if (!wait_to_complete)
690                         break;
691         } while (!new.link_status && cnt--);
692
693 out:
694         /* Timed out or success */
695         if (new.link_status != eth_dev->data->dev_link.link_status ||
696         new.link_speed != eth_dev->data->dev_link.link_speed) {
697                 rte_bnxt_atomic_write_link_status(eth_dev, &new);
698                 bnxt_print_link_info(eth_dev);
699         }
700
701         return rc;
702 }
703
704 static void bnxt_promiscuous_enable_op(struct rte_eth_dev *eth_dev)
705 {
706         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
707         struct bnxt_vnic_info *vnic;
708
709         if (bp->vnic_info == NULL)
710                 return;
711
712         vnic = &bp->vnic_info[0];
713
714         vnic->flags |= BNXT_VNIC_INFO_PROMISC;
715         bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
716 }
717
718 static void bnxt_promiscuous_disable_op(struct rte_eth_dev *eth_dev)
719 {
720         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
721         struct bnxt_vnic_info *vnic;
722
723         if (bp->vnic_info == NULL)
724                 return;
725
726         vnic = &bp->vnic_info[0];
727
728         vnic->flags &= ~BNXT_VNIC_INFO_PROMISC;
729         bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
730 }
731
732 static void bnxt_allmulticast_enable_op(struct rte_eth_dev *eth_dev)
733 {
734         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
735         struct bnxt_vnic_info *vnic;
736
737         if (bp->vnic_info == NULL)
738                 return;
739
740         vnic = &bp->vnic_info[0];
741
742         vnic->flags |= BNXT_VNIC_INFO_ALLMULTI;
743         bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
744 }
745
746 static void bnxt_allmulticast_disable_op(struct rte_eth_dev *eth_dev)
747 {
748         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
749         struct bnxt_vnic_info *vnic;
750
751         if (bp->vnic_info == NULL)
752                 return;
753
754         vnic = &bp->vnic_info[0];
755
756         vnic->flags &= ~BNXT_VNIC_INFO_ALLMULTI;
757         bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
758 }
759
760 static int bnxt_reta_update_op(struct rte_eth_dev *eth_dev,
761                             struct rte_eth_rss_reta_entry64 *reta_conf,
762                             uint16_t reta_size)
763 {
764         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
765         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
766         struct bnxt_vnic_info *vnic;
767         int i;
768
769         if (!(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG))
770                 return -EINVAL;
771
772         if (reta_size != HW_HASH_INDEX_SIZE) {
773                 RTE_LOG(ERR, PMD, "The configured hash table lookup size "
774                         "(%d) must equal the size supported by the hardware "
775                         "(%d)\n", reta_size, HW_HASH_INDEX_SIZE);
776                 return -EINVAL;
777         }
778         /* Update the RSS VNIC(s) */
779         for (i = 0; i < MAX_FF_POOLS; i++) {
780                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
781                         memcpy(vnic->rss_table, reta_conf, reta_size);
782
783                         bnxt_hwrm_vnic_rss_cfg(bp, vnic);
784                 }
785         }
786         return 0;
787 }
788
789 static int bnxt_reta_query_op(struct rte_eth_dev *eth_dev,
790                               struct rte_eth_rss_reta_entry64 *reta_conf,
791                               uint16_t reta_size)
792 {
793         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
794         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
795         struct rte_intr_handle *intr_handle
796                 = &bp->pdev->intr_handle;
797
798         /* Retrieve from the default VNIC */
799         if (!vnic)
800                 return -EINVAL;
801         if (!vnic->rss_table)
802                 return -EINVAL;
803
804         if (reta_size != HW_HASH_INDEX_SIZE) {
805                 RTE_LOG(ERR, PMD, "The configured hash table lookup size "
806                         "(%d) must equal the size supported by the hardware "
807                         "(%d)\n", reta_size, HW_HASH_INDEX_SIZE);
808                 return -EINVAL;
809         }
810         /* EW - need to revisit here copying from u64 to u16 */
811         memcpy(reta_conf, vnic->rss_table, reta_size);
812
813         if (rte_intr_allow_others(intr_handle)) {
814                 if (eth_dev->data->dev_conf.intr_conf.lsc != 0)
815                         bnxt_dev_lsc_intr_setup(eth_dev);
816         }
817
818         return 0;
819 }
820
821 static int bnxt_rss_hash_update_op(struct rte_eth_dev *eth_dev,
822                                    struct rte_eth_rss_conf *rss_conf)
823 {
824         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
825         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
826         struct bnxt_vnic_info *vnic;
827         uint16_t hash_type = 0;
828         int i;
829
830         /*
831          * If RSS enablement were different than dev_configure,
832          * then return -EINVAL
833          */
834         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
835                 if (!rss_conf->rss_hf)
836                         RTE_LOG(ERR, PMD, "Hash type NONE\n");
837         } else {
838                 if (rss_conf->rss_hf & BNXT_ETH_RSS_SUPPORT)
839                         return -EINVAL;
840         }
841
842         bp->flags |= BNXT_FLAG_UPDATE_HASH;
843         memcpy(&bp->rss_conf, rss_conf, sizeof(*rss_conf));
844
845         if (rss_conf->rss_hf & ETH_RSS_IPV4)
846                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
847         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
848                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
849         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
850                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
851         if (rss_conf->rss_hf & ETH_RSS_IPV6)
852                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
853         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
854                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
855         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_UDP)
856                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
857
858         /* Update the RSS VNIC(s) */
859         for (i = 0; i < MAX_FF_POOLS; i++) {
860                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
861                         vnic->hash_type = hash_type;
862
863                         /*
864                          * Use the supplied key if the key length is
865                          * acceptable and the rss_key is not NULL
866                          */
867                         if (rss_conf->rss_key &&
868                             rss_conf->rss_key_len <= HW_HASH_KEY_SIZE)
869                                 memcpy(vnic->rss_hash_key, rss_conf->rss_key,
870                                        rss_conf->rss_key_len);
871
872                         bnxt_hwrm_vnic_rss_cfg(bp, vnic);
873                 }
874         }
875         return 0;
876 }
877
878 static int bnxt_rss_hash_conf_get_op(struct rte_eth_dev *eth_dev,
879                                      struct rte_eth_rss_conf *rss_conf)
880 {
881         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
882         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
883         int len;
884         uint32_t hash_types;
885
886         /* RSS configuration is the same for all VNICs */
887         if (vnic && vnic->rss_hash_key) {
888                 if (rss_conf->rss_key) {
889                         len = rss_conf->rss_key_len <= HW_HASH_KEY_SIZE ?
890                               rss_conf->rss_key_len : HW_HASH_KEY_SIZE;
891                         memcpy(rss_conf->rss_key, vnic->rss_hash_key, len);
892                 }
893
894                 hash_types = vnic->hash_type;
895                 rss_conf->rss_hf = 0;
896                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4) {
897                         rss_conf->rss_hf |= ETH_RSS_IPV4;
898                         hash_types &= ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
899                 }
900                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4) {
901                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
902                         hash_types &=
903                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
904                 }
905                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4) {
906                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
907                         hash_types &=
908                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
909                 }
910                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6) {
911                         rss_conf->rss_hf |= ETH_RSS_IPV6;
912                         hash_types &= ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
913                 }
914                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6) {
915                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
916                         hash_types &=
917                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
918                 }
919                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6) {
920                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP;
921                         hash_types &=
922                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
923                 }
924                 if (hash_types) {
925                         RTE_LOG(ERR, PMD,
926                                 "Unknwon RSS config from firmware (%08x), RSS disabled",
927                                 vnic->hash_type);
928                         return -ENOTSUP;
929                 }
930         } else {
931                 rss_conf->rss_hf = 0;
932         }
933         return 0;
934 }
935
936 static int bnxt_flow_ctrl_get_op(struct rte_eth_dev *dev,
937                                struct rte_eth_fc_conf *fc_conf)
938 {
939         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
940         struct rte_eth_link link_info;
941         int rc;
942
943         rc = bnxt_get_hwrm_link_config(bp, &link_info);
944         if (rc)
945                 return rc;
946
947         memset(fc_conf, 0, sizeof(*fc_conf));
948         if (bp->link_info.auto_pause)
949                 fc_conf->autoneg = 1;
950         switch (bp->link_info.pause) {
951         case 0:
952                 fc_conf->mode = RTE_FC_NONE;
953                 break;
954         case HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX:
955                 fc_conf->mode = RTE_FC_TX_PAUSE;
956                 break;
957         case HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX:
958                 fc_conf->mode = RTE_FC_RX_PAUSE;
959                 break;
960         case (HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
961                         HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX):
962                 fc_conf->mode = RTE_FC_FULL;
963                 break;
964         }
965         return 0;
966 }
967
968 static int bnxt_flow_ctrl_set_op(struct rte_eth_dev *dev,
969                                struct rte_eth_fc_conf *fc_conf)
970 {
971         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
972
973         if (BNXT_NPAR_PF(bp) || BNXT_VF(bp)) {
974                 RTE_LOG(ERR, PMD, "Flow Control Settings cannot be modified\n");
975                 return -ENOTSUP;
976         }
977
978         switch (fc_conf->mode) {
979         case RTE_FC_NONE:
980                 bp->link_info.auto_pause = 0;
981                 bp->link_info.force_pause = 0;
982                 break;
983         case RTE_FC_RX_PAUSE:
984                 if (fc_conf->autoneg) {
985                         bp->link_info.auto_pause =
986                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_RX;
987                         bp->link_info.force_pause = 0;
988                 } else {
989                         bp->link_info.auto_pause = 0;
990                         bp->link_info.force_pause =
991                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_RX;
992                 }
993                 break;
994         case RTE_FC_TX_PAUSE:
995                 if (fc_conf->autoneg) {
996                         bp->link_info.auto_pause =
997                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_TX;
998                         bp->link_info.force_pause = 0;
999                 } else {
1000                         bp->link_info.auto_pause = 0;
1001                         bp->link_info.force_pause =
1002                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_TX;
1003                 }
1004                 break;
1005         case RTE_FC_FULL:
1006                 if (fc_conf->autoneg) {
1007                         bp->link_info.auto_pause =
1008                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_TX |
1009                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_RX;
1010                         bp->link_info.force_pause = 0;
1011                 } else {
1012                         bp->link_info.auto_pause = 0;
1013                         bp->link_info.force_pause =
1014                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_TX |
1015                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_RX;
1016                 }
1017                 break;
1018         }
1019         return bnxt_set_hwrm_link_config(bp, true);
1020 }
1021
1022 /* Add UDP tunneling port */
1023 static int
1024 bnxt_udp_tunnel_port_add_op(struct rte_eth_dev *eth_dev,
1025                          struct rte_eth_udp_tunnel *udp_tunnel)
1026 {
1027         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
1028         uint16_t tunnel_type = 0;
1029         int rc = 0;
1030
1031         switch (udp_tunnel->prot_type) {
1032         case RTE_TUNNEL_TYPE_VXLAN:
1033                 if (bp->vxlan_port_cnt) {
1034                         RTE_LOG(ERR, PMD, "Tunnel Port %d already programmed\n",
1035                                 udp_tunnel->udp_port);
1036                         if (bp->vxlan_port != udp_tunnel->udp_port) {
1037                                 RTE_LOG(ERR, PMD, "Only one port allowed\n");
1038                                 return -ENOSPC;
1039                         }
1040                         bp->vxlan_port_cnt++;
1041                         return 0;
1042                 }
1043                 tunnel_type =
1044                         HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_VXLAN;
1045                 bp->vxlan_port_cnt++;
1046                 break;
1047         case RTE_TUNNEL_TYPE_GENEVE:
1048                 if (bp->geneve_port_cnt) {
1049                         RTE_LOG(ERR, PMD, "Tunnel Port %d already programmed\n",
1050                                 udp_tunnel->udp_port);
1051                         if (bp->geneve_port != udp_tunnel->udp_port) {
1052                                 RTE_LOG(ERR, PMD, "Only one port allowed\n");
1053                                 return -ENOSPC;
1054                         }
1055                         bp->geneve_port_cnt++;
1056                         return 0;
1057                 }
1058                 tunnel_type =
1059                         HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_GENEVE;
1060                 bp->geneve_port_cnt++;
1061                 break;
1062         default:
1063                 RTE_LOG(ERR, PMD, "Tunnel type is not supported\n");
1064                 return -ENOTSUP;
1065         }
1066         rc = bnxt_hwrm_tunnel_dst_port_alloc(bp, udp_tunnel->udp_port,
1067                                              tunnel_type);
1068         return rc;
1069 }
1070
1071 static int
1072 bnxt_udp_tunnel_port_del_op(struct rte_eth_dev *eth_dev,
1073                          struct rte_eth_udp_tunnel *udp_tunnel)
1074 {
1075         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
1076         uint16_t tunnel_type = 0;
1077         uint16_t port = 0;
1078         int rc = 0;
1079
1080         switch (udp_tunnel->prot_type) {
1081         case RTE_TUNNEL_TYPE_VXLAN:
1082                 if (!bp->vxlan_port_cnt) {
1083                         RTE_LOG(ERR, PMD, "No Tunnel port configured yet\n");
1084                         return -EINVAL;
1085                 }
1086                 if (bp->vxlan_port != udp_tunnel->udp_port) {
1087                         RTE_LOG(ERR, PMD, "Req Port: %d. Configured port: %d\n",
1088                                 udp_tunnel->udp_port, bp->vxlan_port);
1089                         return -EINVAL;
1090                 }
1091                 if (--bp->vxlan_port_cnt)
1092                         return 0;
1093
1094                 tunnel_type =
1095                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN;
1096                 port = bp->vxlan_fw_dst_port_id;
1097                 break;
1098         case RTE_TUNNEL_TYPE_GENEVE:
1099                 if (!bp->geneve_port_cnt) {
1100                         RTE_LOG(ERR, PMD, "No Tunnel port configured yet\n");
1101                         return -EINVAL;
1102                 }
1103                 if (bp->geneve_port != udp_tunnel->udp_port) {
1104                         RTE_LOG(ERR, PMD, "Req Port: %d. Configured port: %d\n",
1105                                 udp_tunnel->udp_port, bp->geneve_port);
1106                         return -EINVAL;
1107                 }
1108                 if (--bp->geneve_port_cnt)
1109                         return 0;
1110
1111                 tunnel_type =
1112                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE;
1113                 port = bp->geneve_fw_dst_port_id;
1114                 break;
1115         default:
1116                 RTE_LOG(ERR, PMD, "Tunnel type is not supported\n");
1117                 return -ENOTSUP;
1118         }
1119
1120         rc = bnxt_hwrm_tunnel_dst_port_free(bp, port, tunnel_type);
1121         if (!rc) {
1122                 if (tunnel_type ==
1123                     HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN)
1124                         bp->vxlan_port = 0;
1125                 if (tunnel_type ==
1126                     HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE)
1127                         bp->geneve_port = 0;
1128         }
1129         return rc;
1130 }
1131
1132 static int bnxt_del_vlan_filter(struct bnxt *bp, uint16_t vlan_id)
1133 {
1134         struct bnxt_filter_info *filter, *temp_filter, *new_filter;
1135         struct bnxt_vnic_info *vnic;
1136         unsigned int i;
1137         int rc = 0;
1138         uint32_t chk = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN;
1139
1140         /* Cycle through all VNICs */
1141         for (i = 0; i < bp->nr_vnics; i++) {
1142                 /*
1143                  * For each VNIC and each associated filter(s)
1144                  * if VLAN exists && VLAN matches vlan_id
1145                  *      remove the MAC+VLAN filter
1146                  *      add a new MAC only filter
1147                  * else
1148                  *      VLAN filter doesn't exist, just skip and continue
1149                  */
1150                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
1151                         filter = STAILQ_FIRST(&vnic->filter);
1152                         while (filter) {
1153                                 temp_filter = STAILQ_NEXT(filter, next);
1154
1155                                 if (filter->enables & chk &&
1156                                     filter->l2_ovlan == vlan_id) {
1157                                         /* Must delete the filter */
1158                                         STAILQ_REMOVE(&vnic->filter, filter,
1159                                                       bnxt_filter_info, next);
1160                                         bnxt_hwrm_clear_filter(bp, filter);
1161                                         STAILQ_INSERT_TAIL(
1162                                                         &bp->free_filter_list,
1163                                                         filter, next);
1164
1165                                         /*
1166                                          * Need to examine to see if the MAC
1167                                          * filter already existed or not before
1168                                          * allocating a new one
1169                                          */
1170
1171                                         new_filter = bnxt_alloc_filter(bp);
1172                                         if (!new_filter) {
1173                                                 RTE_LOG(ERR, PMD,
1174                                                         "MAC/VLAN filter alloc failed\n");
1175                                                 rc = -ENOMEM;
1176                                                 goto exit;
1177                                         }
1178                                         STAILQ_INSERT_TAIL(&vnic->filter,
1179                                                            new_filter, next);
1180                                         /* Inherit MAC from previous filter */
1181                                         new_filter->mac_index =
1182                                                         filter->mac_index;
1183                                         memcpy(new_filter->l2_addr,
1184                                                filter->l2_addr, ETHER_ADDR_LEN);
1185                                         /* MAC only filter */
1186                                         rc = bnxt_hwrm_set_filter(bp,
1187                                                         vnic->fw_vnic_id,
1188                                                         new_filter);
1189                                         if (rc)
1190                                                 goto exit;
1191                                         RTE_LOG(INFO, PMD,
1192                                                 "Del Vlan filter for %d\n",
1193                                                 vlan_id);
1194                                 }
1195                                 filter = temp_filter;
1196                         }
1197                 }
1198         }
1199 exit:
1200         return rc;
1201 }
1202
1203 static int bnxt_add_vlan_filter(struct bnxt *bp, uint16_t vlan_id)
1204 {
1205         struct bnxt_filter_info *filter, *temp_filter, *new_filter;
1206         struct bnxt_vnic_info *vnic;
1207         unsigned int i;
1208         int rc = 0;
1209         uint32_t en = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN |
1210                 HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN_MASK;
1211         uint32_t chk = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_OVLAN;
1212
1213         /* Cycle through all VNICs */
1214         for (i = 0; i < bp->nr_vnics; i++) {
1215                 /*
1216                  * For each VNIC and each associated filter(s)
1217                  * if VLAN exists:
1218                  *   if VLAN matches vlan_id
1219                  *      VLAN filter already exists, just skip and continue
1220                  *   else
1221                  *      add a new MAC+VLAN filter
1222                  * else
1223                  *   Remove the old MAC only filter
1224                  *    Add a new MAC+VLAN filter
1225                  */
1226                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
1227                         filter = STAILQ_FIRST(&vnic->filter);
1228                         while (filter) {
1229                                 temp_filter = STAILQ_NEXT(filter, next);
1230
1231                                 if (filter->enables & chk) {
1232                                         if (filter->l2_ovlan == vlan_id)
1233                                                 goto cont;
1234                                 } else {
1235                                         /* Must delete the MAC filter */
1236                                         STAILQ_REMOVE(&vnic->filter, filter,
1237                                                       bnxt_filter_info, next);
1238                                         bnxt_hwrm_clear_filter(bp, filter);
1239                                         filter->l2_ovlan = 0;
1240                                         STAILQ_INSERT_TAIL(
1241                                                         &bp->free_filter_list,
1242                                                         filter, next);
1243                                 }
1244                                 new_filter = bnxt_alloc_filter(bp);
1245                                 if (!new_filter) {
1246                                         RTE_LOG(ERR, PMD,
1247                                                 "MAC/VLAN filter alloc failed\n");
1248                                         rc = -ENOMEM;
1249                                         goto exit;
1250                                 }
1251                                 STAILQ_INSERT_TAIL(&vnic->filter, new_filter,
1252                                                    next);
1253                                 /* Inherit MAC from the previous filter */
1254                                 new_filter->mac_index = filter->mac_index;
1255                                 memcpy(new_filter->l2_addr, filter->l2_addr,
1256                                        ETHER_ADDR_LEN);
1257                                 /* MAC + VLAN ID filter */
1258                                 new_filter->l2_ovlan = vlan_id;
1259                                 new_filter->l2_ovlan_mask = 0xF000;
1260                                 new_filter->enables |= en;
1261                                 rc = bnxt_hwrm_set_filter(bp, vnic->fw_vnic_id,
1262                                                           new_filter);
1263                                 if (rc)
1264                                         goto exit;
1265                                 RTE_LOG(INFO, PMD,
1266                                         "Added Vlan filter for %d\n", vlan_id);
1267 cont:
1268                                 filter = temp_filter;
1269                         }
1270                 }
1271         }
1272 exit:
1273         return rc;
1274 }
1275
1276 static int bnxt_vlan_filter_set_op(struct rte_eth_dev *eth_dev,
1277                                    uint16_t vlan_id, int on)
1278 {
1279         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
1280
1281         /* These operations apply to ALL existing MAC/VLAN filters */
1282         if (on)
1283                 return bnxt_add_vlan_filter(bp, vlan_id);
1284         else
1285                 return bnxt_del_vlan_filter(bp, vlan_id);
1286 }
1287
1288 static void
1289 bnxt_vlan_offload_set_op(struct rte_eth_dev *dev, int mask)
1290 {
1291         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1292         unsigned int i;
1293
1294         if (mask & ETH_VLAN_FILTER_MASK) {
1295                 if (!dev->data->dev_conf.rxmode.hw_vlan_filter) {
1296                         /* Remove any VLAN filters programmed */
1297                         for (i = 0; i < 4095; i++)
1298                                 bnxt_del_vlan_filter(bp, i);
1299                 }
1300                 RTE_LOG(INFO, PMD, "VLAN Filtering: %d\n",
1301                         dev->data->dev_conf.rxmode.hw_vlan_filter);
1302         }
1303
1304         if (mask & ETH_VLAN_STRIP_MASK) {
1305                 /* Enable or disable VLAN stripping */
1306                 for (i = 0; i < bp->nr_vnics; i++) {
1307                         struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
1308                         if (dev->data->dev_conf.rxmode.hw_vlan_strip)
1309                                 vnic->vlan_strip = true;
1310                         else
1311                                 vnic->vlan_strip = false;
1312                         bnxt_hwrm_vnic_cfg(bp, vnic);
1313                 }
1314                 RTE_LOG(INFO, PMD, "VLAN Strip Offload: %d\n",
1315                         dev->data->dev_conf.rxmode.hw_vlan_strip);
1316         }
1317
1318         if (mask & ETH_VLAN_EXTEND_MASK)
1319                 RTE_LOG(ERR, PMD, "Extend VLAN Not supported\n");
1320 }
1321
1322 static void
1323 bnxt_set_default_mac_addr_op(struct rte_eth_dev *dev, struct ether_addr *addr)
1324 {
1325         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1326         /* Default Filter is tied to VNIC 0 */
1327         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
1328         struct bnxt_filter_info *filter;
1329         int rc;
1330
1331         if (BNXT_VF(bp))
1332                 return;
1333
1334         memcpy(bp->mac_addr, addr, sizeof(bp->mac_addr));
1335         memcpy(&dev->data->mac_addrs[0], bp->mac_addr, ETHER_ADDR_LEN);
1336
1337         STAILQ_FOREACH(filter, &vnic->filter, next) {
1338                 /* Default Filter is at Index 0 */
1339                 if (filter->mac_index != 0)
1340                         continue;
1341                 rc = bnxt_hwrm_clear_filter(bp, filter);
1342                 if (rc)
1343                         break;
1344                 memcpy(filter->l2_addr, bp->mac_addr, ETHER_ADDR_LEN);
1345                 memset(filter->l2_addr_mask, 0xff, ETHER_ADDR_LEN);
1346                 filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_PATH_RX;
1347                 filter->enables |=
1348                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR |
1349                         HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_ADDR_MASK;
1350                 rc = bnxt_hwrm_set_filter(bp, vnic->fw_vnic_id, filter);
1351                 if (rc)
1352                         break;
1353                 filter->mac_index = 0;
1354                 RTE_LOG(DEBUG, PMD, "Set MAC addr\n");
1355         }
1356 }
1357
1358 static int
1359 bnxt_dev_set_mc_addr_list_op(struct rte_eth_dev *eth_dev,
1360                           struct ether_addr *mc_addr_set,
1361                           uint32_t nb_mc_addr)
1362 {
1363         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
1364         char *mc_addr_list = (char *)mc_addr_set;
1365         struct bnxt_vnic_info *vnic;
1366         uint32_t off = 0, i = 0;
1367
1368         vnic = &bp->vnic_info[0];
1369
1370         if (nb_mc_addr > BNXT_MAX_MC_ADDRS) {
1371                 vnic->flags |= BNXT_VNIC_INFO_ALLMULTI;
1372                 goto allmulti;
1373         }
1374
1375         /* TODO Check for Duplicate mcast addresses */
1376         vnic->flags &= ~BNXT_VNIC_INFO_ALLMULTI;
1377         for (i = 0; i < nb_mc_addr; i++) {
1378                 memcpy(vnic->mc_list + off, &mc_addr_list[i], ETHER_ADDR_LEN);
1379                 off += ETHER_ADDR_LEN;
1380         }
1381
1382         vnic->mc_addr_cnt = i;
1383
1384 allmulti:
1385         return bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
1386 }
1387
1388 static int
1389 bnxt_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
1390 {
1391         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1392         uint8_t fw_major = (bp->fw_ver >> 24) & 0xff;
1393         uint8_t fw_minor = (bp->fw_ver >> 16) & 0xff;
1394         uint8_t fw_updt = (bp->fw_ver >> 8) & 0xff;
1395         int ret;
1396
1397         ret = snprintf(fw_version, fw_size, "%d.%d.%d",
1398                         fw_major, fw_minor, fw_updt);
1399
1400         ret += 1; /* add the size of '\0' */
1401         if (fw_size < (uint32_t)ret)
1402                 return ret;
1403         else
1404                 return 0;
1405 }
1406
1407 static void
1408 bnxt_rxq_info_get_op(struct rte_eth_dev *dev, uint16_t queue_id,
1409         struct rte_eth_rxq_info *qinfo)
1410 {
1411         struct bnxt_rx_queue *rxq;
1412
1413         rxq = dev->data->rx_queues[queue_id];
1414
1415         qinfo->mp = rxq->mb_pool;
1416         qinfo->scattered_rx = dev->data->scattered_rx;
1417         qinfo->nb_desc = rxq->nb_rx_desc;
1418
1419         qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
1420         qinfo->conf.rx_drop_en = 0;
1421         qinfo->conf.rx_deferred_start = 0;
1422 }
1423
1424 static void
1425 bnxt_txq_info_get_op(struct rte_eth_dev *dev, uint16_t queue_id,
1426         struct rte_eth_txq_info *qinfo)
1427 {
1428         struct bnxt_tx_queue *txq;
1429
1430         txq = dev->data->tx_queues[queue_id];
1431
1432         qinfo->nb_desc = txq->nb_tx_desc;
1433
1434         qinfo->conf.tx_thresh.pthresh = txq->pthresh;
1435         qinfo->conf.tx_thresh.hthresh = txq->hthresh;
1436         qinfo->conf.tx_thresh.wthresh = txq->wthresh;
1437
1438         qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
1439         qinfo->conf.tx_rs_thresh = 0;
1440         qinfo->conf.txq_flags = txq->txq_flags;
1441         qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
1442 }
1443
1444 static int bnxt_mtu_set_op(struct rte_eth_dev *eth_dev, uint16_t new_mtu)
1445 {
1446         struct bnxt *bp = eth_dev->data->dev_private;
1447         struct rte_eth_dev_info dev_info;
1448         uint32_t max_dev_mtu;
1449         uint32_t rc = 0;
1450         uint32_t i;
1451
1452         bnxt_dev_info_get_op(eth_dev, &dev_info);
1453         max_dev_mtu = dev_info.max_rx_pktlen -
1454                       ETHER_HDR_LEN - ETHER_CRC_LEN - VLAN_TAG_SIZE * 2;
1455
1456         if (new_mtu < ETHER_MIN_MTU || new_mtu > max_dev_mtu) {
1457                 RTE_LOG(ERR, PMD, "MTU requested must be within (%d, %d)\n",
1458                         ETHER_MIN_MTU, max_dev_mtu);
1459                 return -EINVAL;
1460         }
1461
1462
1463         if (new_mtu > ETHER_MTU) {
1464                 bp->flags |= BNXT_FLAG_JUMBO;
1465                 eth_dev->data->dev_conf.rxmode.jumbo_frame = 1;
1466         } else {
1467                 eth_dev->data->dev_conf.rxmode.jumbo_frame = 0;
1468                 bp->flags &= ~BNXT_FLAG_JUMBO;
1469         }
1470
1471         eth_dev->data->dev_conf.rxmode.max_rx_pkt_len =
1472                 new_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN + VLAN_TAG_SIZE * 2;
1473
1474         eth_dev->data->mtu = new_mtu;
1475         RTE_LOG(INFO, PMD, "New MTU is %d\n", eth_dev->data->mtu);
1476
1477         for (i = 0; i < bp->nr_vnics; i++) {
1478                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
1479
1480                 vnic->mru = bp->eth_dev->data->mtu + ETHER_HDR_LEN +
1481                                         ETHER_CRC_LEN + VLAN_TAG_SIZE * 2;
1482                 rc = bnxt_hwrm_vnic_cfg(bp, vnic);
1483                 if (rc)
1484                         break;
1485
1486                 rc = bnxt_hwrm_vnic_plcmode_cfg(bp, vnic);
1487                 if (rc)
1488                         return rc;
1489         }
1490
1491         return rc;
1492 }
1493
1494 static int
1495 bnxt_vlan_pvid_set_op(struct rte_eth_dev *dev, uint16_t pvid, int on)
1496 {
1497         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1498         uint16_t vlan = bp->vlan;
1499         int rc;
1500
1501         if (BNXT_NPAR_PF(bp) || BNXT_VF(bp)) {
1502                 RTE_LOG(ERR, PMD,
1503                         "PVID cannot be modified for this function\n");
1504                 return -ENOTSUP;
1505         }
1506         bp->vlan = on ? pvid : 0;
1507
1508         rc = bnxt_hwrm_set_default_vlan(bp, 0, 0);
1509         if (rc)
1510                 bp->vlan = vlan;
1511         return rc;
1512 }
1513
1514 static int
1515 bnxt_dev_led_on_op(struct rte_eth_dev *dev)
1516 {
1517         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1518
1519         return bnxt_hwrm_port_led_cfg(bp, true);
1520 }
1521
1522 static int
1523 bnxt_dev_led_off_op(struct rte_eth_dev *dev)
1524 {
1525         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
1526
1527         return bnxt_hwrm_port_led_cfg(bp, false);
1528 }
1529
1530 static uint32_t
1531 bnxt_rx_queue_count_op(struct rte_eth_dev *dev, uint16_t rx_queue_id)
1532 {
1533         uint32_t desc = 0, raw_cons = 0, cons;
1534         struct bnxt_cp_ring_info *cpr;
1535         struct bnxt_rx_queue *rxq;
1536         struct rx_pkt_cmpl *rxcmp;
1537         uint16_t cmp_type;
1538         uint8_t cmp = 1;
1539         bool valid;
1540
1541         rxq = dev->data->rx_queues[rx_queue_id];
1542         cpr = rxq->cp_ring;
1543         valid = cpr->valid;
1544
1545         while (raw_cons < rxq->nb_rx_desc) {
1546                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
1547                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
1548
1549                 if (!CMPL_VALID(rxcmp, valid))
1550                         goto nothing_to_do;
1551                 valid = FLIP_VALID(cons, cpr->cp_ring_struct->ring_mask, valid);
1552                 cmp_type = CMP_TYPE(rxcmp);
1553                 if (cmp_type == RX_PKT_CMPL_TYPE_RX_L2_TPA_END) {
1554                         cmp = (rte_le_to_cpu_32(
1555                                         ((struct rx_tpa_end_cmpl *)
1556                                          (rxcmp))->agg_bufs_v1) &
1557                                RX_TPA_END_CMPL_AGG_BUFS_MASK) >>
1558                                 RX_TPA_END_CMPL_AGG_BUFS_SFT;
1559                         desc++;
1560                 } else if (cmp_type == 0x11) {
1561                         desc++;
1562                         cmp = (rxcmp->agg_bufs_v1 &
1563                                    RX_PKT_CMPL_AGG_BUFS_MASK) >>
1564                                 RX_PKT_CMPL_AGG_BUFS_SFT;
1565                 } else {
1566                         cmp = 1;
1567                 }
1568 nothing_to_do:
1569                 raw_cons += cmp ? cmp : 2;
1570         }
1571
1572         return desc;
1573 }
1574
1575 static int
1576 bnxt_rx_descriptor_status_op(void *rx_queue, uint16_t offset)
1577 {
1578         struct bnxt_rx_queue *rxq = (struct bnxt_rx_queue *)rx_queue;
1579         struct bnxt_rx_ring_info *rxr;
1580         struct bnxt_cp_ring_info *cpr;
1581         struct bnxt_sw_rx_bd *rx_buf;
1582         struct rx_pkt_cmpl *rxcmp;
1583         uint32_t cons, cp_cons;
1584
1585         if (!rxq)
1586                 return -EINVAL;
1587
1588         cpr = rxq->cp_ring;
1589         rxr = rxq->rx_ring;
1590
1591         if (offset >= rxq->nb_rx_desc)
1592                 return -EINVAL;
1593
1594         cons = RING_CMP(cpr->cp_ring_struct, offset);
1595         cp_cons = cpr->cp_raw_cons;
1596         rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
1597
1598         if (cons > cp_cons) {
1599                 if (CMPL_VALID(rxcmp, cpr->valid))
1600                         return RTE_ETH_RX_DESC_DONE;
1601         } else {
1602                 if (CMPL_VALID(rxcmp, !cpr->valid))
1603                         return RTE_ETH_RX_DESC_DONE;
1604         }
1605         rx_buf = &rxr->rx_buf_ring[cons];
1606         if (rx_buf->mbuf == NULL)
1607                 return RTE_ETH_RX_DESC_UNAVAIL;
1608
1609
1610         return RTE_ETH_RX_DESC_AVAIL;
1611 }
1612
1613 /*
1614  * Initialization
1615  */
1616
1617 static const struct eth_dev_ops bnxt_dev_ops = {
1618         .dev_infos_get = bnxt_dev_info_get_op,
1619         .dev_close = bnxt_dev_close_op,
1620         .dev_configure = bnxt_dev_configure_op,
1621         .dev_start = bnxt_dev_start_op,
1622         .dev_stop = bnxt_dev_stop_op,
1623         .dev_set_link_up = bnxt_dev_set_link_up_op,
1624         .dev_set_link_down = bnxt_dev_set_link_down_op,
1625         .stats_get = bnxt_stats_get_op,
1626         .stats_reset = bnxt_stats_reset_op,
1627         .rx_queue_setup = bnxt_rx_queue_setup_op,
1628         .rx_queue_release = bnxt_rx_queue_release_op,
1629         .tx_queue_setup = bnxt_tx_queue_setup_op,
1630         .tx_queue_release = bnxt_tx_queue_release_op,
1631         .reta_update = bnxt_reta_update_op,
1632         .reta_query = bnxt_reta_query_op,
1633         .rss_hash_update = bnxt_rss_hash_update_op,
1634         .rss_hash_conf_get = bnxt_rss_hash_conf_get_op,
1635         .link_update = bnxt_link_update_op,
1636         .promiscuous_enable = bnxt_promiscuous_enable_op,
1637         .promiscuous_disable = bnxt_promiscuous_disable_op,
1638         .allmulticast_enable = bnxt_allmulticast_enable_op,
1639         .allmulticast_disable = bnxt_allmulticast_disable_op,
1640         .mac_addr_add = bnxt_mac_addr_add_op,
1641         .mac_addr_remove = bnxt_mac_addr_remove_op,
1642         .flow_ctrl_get = bnxt_flow_ctrl_get_op,
1643         .flow_ctrl_set = bnxt_flow_ctrl_set_op,
1644         .udp_tunnel_port_add  = bnxt_udp_tunnel_port_add_op,
1645         .udp_tunnel_port_del  = bnxt_udp_tunnel_port_del_op,
1646         .vlan_filter_set = bnxt_vlan_filter_set_op,
1647         .vlan_offload_set = bnxt_vlan_offload_set_op,
1648         .vlan_pvid_set = bnxt_vlan_pvid_set_op,
1649         .mtu_set = bnxt_mtu_set_op,
1650         .mac_addr_set = bnxt_set_default_mac_addr_op,
1651         .xstats_get = bnxt_dev_xstats_get_op,
1652         .xstats_get_names = bnxt_dev_xstats_get_names_op,
1653         .xstats_reset = bnxt_dev_xstats_reset_op,
1654         .fw_version_get = bnxt_fw_version_get,
1655         .set_mc_addr_list = bnxt_dev_set_mc_addr_list_op,
1656         .rxq_info_get = bnxt_rxq_info_get_op,
1657         .txq_info_get = bnxt_txq_info_get_op,
1658         .dev_led_on = bnxt_dev_led_on_op,
1659         .dev_led_off = bnxt_dev_led_off_op,
1660         .xstats_get_by_id = bnxt_dev_xstats_get_by_id_op,
1661         .xstats_get_names_by_id = bnxt_dev_xstats_get_names_by_id_op,
1662         .rx_queue_count = bnxt_rx_queue_count_op,
1663         .rx_descriptor_status = bnxt_rx_descriptor_status_op,
1664 };
1665
1666 static bool bnxt_vf_pciid(uint16_t id)
1667 {
1668         if (id == BROADCOM_DEV_ID_57304_VF ||
1669             id == BROADCOM_DEV_ID_57406_VF ||
1670             id == BROADCOM_DEV_ID_5731X_VF ||
1671             id == BROADCOM_DEV_ID_5741X_VF ||
1672             id == BROADCOM_DEV_ID_57414_VF ||
1673             id == BROADCOM_DEV_ID_STRATUS_NIC_VF)
1674                 return true;
1675         return false;
1676 }
1677
1678 static int bnxt_init_board(struct rte_eth_dev *eth_dev)
1679 {
1680         struct bnxt *bp = eth_dev->data->dev_private;
1681         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1682         int rc;
1683
1684         /* enable device (incl. PCI PM wakeup), and bus-mastering */
1685         if (!pci_dev->mem_resource[0].addr) {
1686                 RTE_LOG(ERR, PMD,
1687                         "Cannot find PCI device base address, aborting\n");
1688                 rc = -ENODEV;
1689                 goto init_err_disable;
1690         }
1691
1692         bp->eth_dev = eth_dev;
1693         bp->pdev = pci_dev;
1694
1695         bp->bar0 = (void *)pci_dev->mem_resource[0].addr;
1696         if (!bp->bar0) {
1697                 RTE_LOG(ERR, PMD, "Cannot map device registers, aborting\n");
1698                 rc = -ENOMEM;
1699                 goto init_err_release;
1700         }
1701         return 0;
1702
1703 init_err_release:
1704         if (bp->bar0)
1705                 bp->bar0 = NULL;
1706
1707 init_err_disable:
1708
1709         return rc;
1710 }
1711
1712 static int bnxt_dev_uninit(struct rte_eth_dev *eth_dev);
1713
1714 #define ALLOW_FUNC(x)   \
1715         { \
1716                 typeof(x) arg = (x); \
1717                 bp->pf.vf_req_fwd[((arg) >> 5)] &= \
1718                 ~rte_cpu_to_le_32(1 << ((arg) & 0x1f)); \
1719         }
1720 static int
1721 bnxt_dev_init(struct rte_eth_dev *eth_dev)
1722 {
1723         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1724         char mz_name[RTE_MEMZONE_NAMESIZE];
1725         const struct rte_memzone *mz = NULL;
1726         static int version_printed;
1727         uint32_t total_alloc_len;
1728         phys_addr_t mz_phys_addr;
1729         struct bnxt *bp;
1730         int rc;
1731
1732         if (version_printed++ == 0)
1733                 RTE_LOG(INFO, PMD, "%s\n", bnxt_version);
1734
1735         rte_eth_copy_pci_info(eth_dev, pci_dev);
1736         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
1737
1738         bp = eth_dev->data->dev_private;
1739
1740         rte_atomic64_init(&bp->rx_mbuf_alloc_fail);
1741         bp->dev_stopped = 1;
1742
1743         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1744                 goto skip_init;
1745
1746         if (bnxt_vf_pciid(pci_dev->id.device_id))
1747                 bp->flags |= BNXT_FLAG_VF;
1748
1749         rc = bnxt_init_board(eth_dev);
1750         if (rc) {
1751                 RTE_LOG(ERR, PMD,
1752                         "Board initialization failed rc: %x\n", rc);
1753                 goto error;
1754         }
1755 skip_init:
1756         eth_dev->dev_ops = &bnxt_dev_ops;
1757         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1758                 return 0;
1759         eth_dev->rx_pkt_burst = &bnxt_recv_pkts;
1760         eth_dev->tx_pkt_burst = &bnxt_xmit_pkts;
1761
1762         if (BNXT_PF(bp) && pci_dev->id.device_id != BROADCOM_DEV_ID_NS2) {
1763                 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
1764                          "bnxt_%04x:%02x:%02x:%02x-%s", pci_dev->addr.domain,
1765                          pci_dev->addr.bus, pci_dev->addr.devid,
1766                          pci_dev->addr.function, "rx_port_stats");
1767                 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
1768                 mz = rte_memzone_lookup(mz_name);
1769                 total_alloc_len = RTE_CACHE_LINE_ROUNDUP(
1770                                 sizeof(struct rx_port_stats) + 512);
1771                 if (!mz) {
1772                         mz = rte_memzone_reserve(mz_name, total_alloc_len,
1773                                                  SOCKET_ID_ANY,
1774                                                  RTE_MEMZONE_2MB |
1775                                                  RTE_MEMZONE_SIZE_HINT_ONLY);
1776                         if (mz == NULL)
1777                                 return -ENOMEM;
1778                 }
1779                 memset(mz->addr, 0, mz->len);
1780                 mz_phys_addr = mz->phys_addr;
1781                 if ((unsigned long)mz->addr == mz_phys_addr) {
1782                         RTE_LOG(WARNING, PMD,
1783                                 "Memzone physical address same as virtual.\n");
1784                         RTE_LOG(WARNING, PMD,
1785                                 "Using rte_mem_virt2phy()\n");
1786                         mz_phys_addr = rte_mem_virt2phy(mz->addr);
1787                         if (mz_phys_addr == 0) {
1788                                 RTE_LOG(ERR, PMD,
1789                                 "unable to map address to physical memory\n");
1790                                 return -ENOMEM;
1791                         }
1792                 }
1793
1794                 bp->rx_mem_zone = (const void *)mz;
1795                 bp->hw_rx_port_stats = mz->addr;
1796                 bp->hw_rx_port_stats_map = mz_phys_addr;
1797
1798                 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
1799                          "bnxt_%04x:%02x:%02x:%02x-%s", pci_dev->addr.domain,
1800                          pci_dev->addr.bus, pci_dev->addr.devid,
1801                          pci_dev->addr.function, "tx_port_stats");
1802                 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
1803                 mz = rte_memzone_lookup(mz_name);
1804                 total_alloc_len = RTE_CACHE_LINE_ROUNDUP(
1805                                 sizeof(struct tx_port_stats) + 512);
1806                 if (!mz) {
1807                         mz = rte_memzone_reserve(mz_name, total_alloc_len,
1808                                                  SOCKET_ID_ANY,
1809                                                  RTE_MEMZONE_2MB |
1810                                                  RTE_MEMZONE_SIZE_HINT_ONLY);
1811                         if (mz == NULL)
1812                                 return -ENOMEM;
1813                 }
1814                 memset(mz->addr, 0, mz->len);
1815                 mz_phys_addr = mz->phys_addr;
1816                 if ((unsigned long)mz->addr == mz_phys_addr) {
1817                         RTE_LOG(WARNING, PMD,
1818                                 "Memzone physical address same as virtual.\n");
1819                         RTE_LOG(WARNING, PMD,
1820                                 "Using rte_mem_virt2phy()\n");
1821                         mz_phys_addr = rte_mem_virt2phy(mz->addr);
1822                         if (mz_phys_addr == 0) {
1823                                 RTE_LOG(ERR, PMD,
1824                                 "unable to map address to physical memory\n");
1825                                 return -ENOMEM;
1826                         }
1827                 }
1828
1829                 bp->tx_mem_zone = (const void *)mz;
1830                 bp->hw_tx_port_stats = mz->addr;
1831                 bp->hw_tx_port_stats_map = mz_phys_addr;
1832
1833                 bp->flags |= BNXT_FLAG_PORT_STATS;
1834         }
1835
1836         rc = bnxt_alloc_hwrm_resources(bp);
1837         if (rc) {
1838                 RTE_LOG(ERR, PMD,
1839                         "hwrm resource allocation failure rc: %x\n", rc);
1840                 goto error_free;
1841         }
1842         rc = bnxt_hwrm_ver_get(bp);
1843         if (rc)
1844                 goto error_free;
1845         bnxt_hwrm_queue_qportcfg(bp);
1846
1847         bnxt_hwrm_func_qcfg(bp);
1848
1849         /* Get the MAX capabilities for this function */
1850         rc = bnxt_hwrm_func_qcaps(bp);
1851         if (rc) {
1852                 RTE_LOG(ERR, PMD, "hwrm query capability failure rc: %x\n", rc);
1853                 goto error_free;
1854         }
1855         if (bp->max_tx_rings == 0) {
1856                 RTE_LOG(ERR, PMD, "No TX rings available!\n");
1857                 rc = -EBUSY;
1858                 goto error_free;
1859         }
1860         eth_dev->data->mac_addrs = rte_zmalloc("bnxt_mac_addr_tbl",
1861                                         ETHER_ADDR_LEN * MAX_NUM_MAC_ADDR, 0);
1862         if (eth_dev->data->mac_addrs == NULL) {
1863                 RTE_LOG(ERR, PMD,
1864                         "Failed to alloc %u bytes needed to store MAC addr tbl",
1865                         ETHER_ADDR_LEN * MAX_NUM_MAC_ADDR);
1866                 rc = -ENOMEM;
1867                 goto error_free;
1868         }
1869         /* Copy the permanent MAC from the qcap response address now. */
1870         memcpy(bp->mac_addr, bp->dflt_mac_addr, sizeof(bp->mac_addr));
1871         memcpy(&eth_dev->data->mac_addrs[0], bp->mac_addr, ETHER_ADDR_LEN);
1872         bp->grp_info = rte_zmalloc("bnxt_grp_info",
1873                                 sizeof(*bp->grp_info) * bp->max_ring_grps, 0);
1874         if (!bp->grp_info) {
1875                 RTE_LOG(ERR, PMD,
1876                         "Failed to alloc %zu bytes needed to store group info table\n",
1877                         sizeof(*bp->grp_info) * bp->max_ring_grps);
1878                 rc = -ENOMEM;
1879                 goto error_free;
1880         }
1881
1882         /* Forward all requests if firmware is new enough */
1883         if (((bp->fw_ver >= ((20 << 24) | (6 << 16) | (100 << 8))) &&
1884             (bp->fw_ver < ((20 << 24) | (7 << 16)))) ||
1885             ((bp->fw_ver >= ((20 << 24) | (8 << 16))))) {
1886                 memset(bp->pf.vf_req_fwd, 0xff, sizeof(bp->pf.vf_req_fwd));
1887         } else {
1888                 RTE_LOG(WARNING, PMD,
1889                         "Firmware too old for VF mailbox functionality\n");
1890                 memset(bp->pf.vf_req_fwd, 0, sizeof(bp->pf.vf_req_fwd));
1891         }
1892
1893         /*
1894          * The following are used for driver cleanup.  If we disallow these,
1895          * VF drivers can't clean up cleanly.
1896          */
1897         ALLOW_FUNC(HWRM_FUNC_DRV_UNRGTR);
1898         ALLOW_FUNC(HWRM_VNIC_FREE);
1899         ALLOW_FUNC(HWRM_RING_FREE);
1900         ALLOW_FUNC(HWRM_RING_GRP_FREE);
1901         ALLOW_FUNC(HWRM_VNIC_RSS_COS_LB_CTX_FREE);
1902         ALLOW_FUNC(HWRM_CFA_L2_FILTER_FREE);
1903         ALLOW_FUNC(HWRM_STAT_CTX_FREE);
1904         rc = bnxt_hwrm_func_driver_register(bp);
1905         if (rc) {
1906                 RTE_LOG(ERR, PMD,
1907                         "Failed to register driver");
1908                 rc = -EBUSY;
1909                 goto error_free;
1910         }
1911
1912         RTE_LOG(INFO, PMD,
1913                 DRV_MODULE_NAME " found at mem %" PRIx64 ", node addr %pM\n",
1914                 pci_dev->mem_resource[0].phys_addr,
1915                 pci_dev->mem_resource[0].addr);
1916
1917         rc = bnxt_hwrm_func_reset(bp);
1918         if (rc) {
1919                 RTE_LOG(ERR, PMD, "hwrm chip reset failure rc: %x\n", rc);
1920                 rc = -1;
1921                 goto error_free;
1922         }
1923
1924         if (BNXT_PF(bp)) {
1925                 //if (bp->pf.active_vfs) {
1926                         // TODO: Deallocate VF resources?
1927                 //}
1928                 if (bp->pdev->max_vfs) {
1929                         rc = bnxt_hwrm_allocate_vfs(bp, bp->pdev->max_vfs);
1930                         if (rc) {
1931                                 RTE_LOG(ERR, PMD, "Failed to allocate VFs\n");
1932                                 goto error_free;
1933                         }
1934                 } else {
1935                         rc = bnxt_hwrm_allocate_pf_only(bp);
1936                         if (rc) {
1937                                 RTE_LOG(ERR, PMD,
1938                                         "Failed to allocate PF resources\n");
1939                                 goto error_free;
1940                         }
1941                 }
1942         }
1943
1944         bnxt_hwrm_port_led_qcaps(bp);
1945
1946         rc = bnxt_setup_int(bp);
1947         if (rc)
1948                 goto error_free;
1949
1950         rc = bnxt_alloc_mem(bp);
1951         if (rc)
1952                 goto error_free_int;
1953
1954         rc = bnxt_request_int(bp);
1955         if (rc)
1956                 goto error_free_int;
1957
1958         rc = bnxt_alloc_def_cp_ring(bp);
1959         if (rc)
1960                 goto error_free_int;
1961
1962         bnxt_enable_int(bp);
1963
1964         return 0;
1965
1966 error_free_int:
1967         bnxt_disable_int(bp);
1968         bnxt_free_def_cp_ring(bp);
1969         bnxt_hwrm_func_buf_unrgtr(bp);
1970         bnxt_free_int(bp);
1971         bnxt_free_mem(bp);
1972 error_free:
1973         bnxt_dev_uninit(eth_dev);
1974 error:
1975         return rc;
1976 }
1977
1978 static int
1979 bnxt_dev_uninit(struct rte_eth_dev *eth_dev) {
1980         struct bnxt *bp = eth_dev->data->dev_private;
1981         int rc;
1982
1983         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1984                 return -EPERM;
1985
1986         bnxt_disable_int(bp);
1987         bnxt_free_int(bp);
1988         bnxt_free_mem(bp);
1989         if (eth_dev->data->mac_addrs != NULL) {
1990                 rte_free(eth_dev->data->mac_addrs);
1991                 eth_dev->data->mac_addrs = NULL;
1992         }
1993         if (bp->grp_info != NULL) {
1994                 rte_free(bp->grp_info);
1995                 bp->grp_info = NULL;
1996         }
1997         rc = bnxt_hwrm_func_driver_unregister(bp, 0);
1998         bnxt_free_hwrm_resources(bp);
1999         rte_memzone_free((const struct rte_memzone *)bp->tx_mem_zone);
2000         rte_memzone_free((const struct rte_memzone *)bp->rx_mem_zone);
2001         if (bp->dev_stopped == 0)
2002                 bnxt_dev_close_op(eth_dev);
2003         if (bp->pf.vf_info)
2004                 rte_free(bp->pf.vf_info);
2005         eth_dev->dev_ops = NULL;
2006         eth_dev->rx_pkt_burst = NULL;
2007         eth_dev->tx_pkt_burst = NULL;
2008
2009         return rc;
2010 }
2011
2012 static int bnxt_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
2013         struct rte_pci_device *pci_dev)
2014 {
2015         return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct bnxt),
2016                 bnxt_dev_init);
2017 }
2018
2019 static int bnxt_pci_remove(struct rte_pci_device *pci_dev)
2020 {
2021         return rte_eth_dev_pci_generic_remove(pci_dev, bnxt_dev_uninit);
2022 }
2023
2024 static struct rte_pci_driver bnxt_rte_pmd = {
2025         .id_table = bnxt_pci_id_map,
2026         .drv_flags = RTE_PCI_DRV_NEED_MAPPING |
2027                 RTE_PCI_DRV_INTR_LSC,
2028         .probe = bnxt_pci_probe,
2029         .remove = bnxt_pci_remove,
2030 };
2031
2032 static bool
2033 is_device_supported(struct rte_eth_dev *dev, struct rte_pci_driver *drv)
2034 {
2035         if (strcmp(dev->device->driver->name, drv->driver.name))
2036                 return false;
2037
2038         return true;
2039 }
2040
2041 bool is_bnxt_supported(struct rte_eth_dev *dev)
2042 {
2043         return is_device_supported(dev, &bnxt_rte_pmd);
2044 }
2045
2046 RTE_PMD_REGISTER_PCI(net_bnxt, bnxt_rte_pmd);
2047 RTE_PMD_REGISTER_PCI_TABLE(net_bnxt, bnxt_pci_id_map);
2048 RTE_PMD_REGISTER_KMOD_DEP(net_bnxt, "* igb_uio | uio_pci_generic | vfio-pci");