52cb4380903f941641ab565e46fb2256ef44ef35
[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 0x1614
64 #define BROADCOM_DEV_ID_57414_VF 0x16c1
65 #define BROADCOM_DEV_ID_57301 0x16c8
66 #define BROADCOM_DEV_ID_57302 0x16c9
67 #define BROADCOM_DEV_ID_57304_PF 0x16ca
68 #define BROADCOM_DEV_ID_57304_VF 0x16cb
69 #define BROADCOM_DEV_ID_57417_MF 0x16cc
70 #define BROADCOM_DEV_ID_NS2 0x16cd
71 #define BROADCOM_DEV_ID_57311 0x16ce
72 #define BROADCOM_DEV_ID_57312 0x16cf
73 #define BROADCOM_DEV_ID_57402 0x16d0
74 #define BROADCOM_DEV_ID_57404 0x16d1
75 #define BROADCOM_DEV_ID_57406_PF 0x16d2
76 #define BROADCOM_DEV_ID_57406_VF 0x16d3
77 #define BROADCOM_DEV_ID_57402_MF 0x16d4
78 #define BROADCOM_DEV_ID_57407_RJ45 0x16d5
79 #define BROADCOM_DEV_ID_57412 0x16d6
80 #define BROADCOM_DEV_ID_57414 0x16d7
81 #define BROADCOM_DEV_ID_57416_RJ45 0x16d8
82 #define BROADCOM_DEV_ID_57417_RJ45 0x16d9
83 #define BROADCOM_DEV_ID_5741X_VF 0x16dc
84 #define BROADCOM_DEV_ID_57412_MF 0x16de
85 #define BROADCOM_DEV_ID_57314 0x16df
86 #define BROADCOM_DEV_ID_57317_RJ45 0x16e0
87 #define BROADCOM_DEV_ID_5731X_VF 0x16e1
88 #define BROADCOM_DEV_ID_57417_SFP 0x16e2
89 #define BROADCOM_DEV_ID_57416_SFP 0x16e3
90 #define BROADCOM_DEV_ID_57317_SFP 0x16e4
91 #define BROADCOM_DEV_ID_57404_MF 0x16e7
92 #define BROADCOM_DEV_ID_57406_MF 0x16e8
93 #define BROADCOM_DEV_ID_57407_SFP 0x16e9
94 #define BROADCOM_DEV_ID_57407_MF 0x16ea
95 #define BROADCOM_DEV_ID_57414_MF 0x16ec
96 #define BROADCOM_DEV_ID_57416_MF 0x16ee
97
98 static const struct rte_pci_id bnxt_pci_id_map[] = {
99         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_STRATUS_NIC) },
100         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414_VF) },
101         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57301) },
102         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57302) },
103         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57304_PF) },
104         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57304_VF) },
105         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_NS2) },
106         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57402) },
107         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57404) },
108         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57406_PF) },
109         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57406_VF) },
110         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57402_MF) },
111         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57407_RJ45) },
112         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57404_MF) },
113         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57406_MF) },
114         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57407_SFP) },
115         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57407_MF) },
116         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_5741X_VF) },
117         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_5731X_VF) },
118         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57314) },
119         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_MF) },
120         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57311) },
121         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57312) },
122         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57412) },
123         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414) },
124         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_RJ45) },
125         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_RJ45) },
126         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57412_MF) },
127         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57317_RJ45) },
128         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_SFP) },
129         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_SFP) },
130         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57317_SFP) },
131         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414_MF) },
132         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_MF) },
133         { .vendor_id = 0, /* sentinel */ },
134 };
135
136 #define BNXT_ETH_RSS_SUPPORT (  \
137         ETH_RSS_IPV4 |          \
138         ETH_RSS_NONFRAG_IPV4_TCP |      \
139         ETH_RSS_NONFRAG_IPV4_UDP |      \
140         ETH_RSS_IPV6 |          \
141         ETH_RSS_NONFRAG_IPV6_TCP |      \
142         ETH_RSS_NONFRAG_IPV6_UDP)
143
144 /***********************/
145
146 /*
147  * High level utility functions
148  */
149
150 static void bnxt_free_mem(struct bnxt *bp)
151 {
152         bnxt_free_filter_mem(bp);
153         bnxt_free_vnic_attributes(bp);
154         bnxt_free_vnic_mem(bp);
155
156         bnxt_free_stats(bp);
157         bnxt_free_tx_rings(bp);
158         bnxt_free_rx_rings(bp);
159         bnxt_free_def_cp_ring(bp);
160 }
161
162 static int bnxt_alloc_mem(struct bnxt *bp)
163 {
164         int rc;
165
166         /* Default completion ring */
167         rc = bnxt_init_def_ring_struct(bp, SOCKET_ID_ANY);
168         if (rc)
169                 goto alloc_mem_err;
170
171         rc = bnxt_alloc_rings(bp, 0, NULL, NULL,
172                               bp->def_cp_ring, "def_cp");
173         if (rc)
174                 goto alloc_mem_err;
175
176         rc = bnxt_alloc_vnic_mem(bp);
177         if (rc)
178                 goto alloc_mem_err;
179
180         rc = bnxt_alloc_vnic_attributes(bp);
181         if (rc)
182                 goto alloc_mem_err;
183
184         rc = bnxt_alloc_filter_mem(bp);
185         if (rc)
186                 goto alloc_mem_err;
187
188         return 0;
189
190 alloc_mem_err:
191         bnxt_free_mem(bp);
192         return rc;
193 }
194
195 static int bnxt_init_chip(struct bnxt *bp)
196 {
197         unsigned int i, rss_idx, fw_idx;
198         struct rte_eth_link new;
199         int rc;
200
201         rc = bnxt_alloc_all_hwrm_stat_ctxs(bp);
202         if (rc) {
203                 RTE_LOG(ERR, PMD, "HWRM stat ctx alloc failure rc: %x\n", rc);
204                 goto err_out;
205         }
206
207         rc = bnxt_alloc_hwrm_rings(bp);
208         if (rc) {
209                 RTE_LOG(ERR, PMD, "HWRM ring alloc failure rc: %x\n", rc);
210                 goto err_out;
211         }
212
213         rc = bnxt_alloc_all_hwrm_ring_grps(bp);
214         if (rc) {
215                 RTE_LOG(ERR, PMD, "HWRM ring grp alloc failure: %x\n", rc);
216                 goto err_out;
217         }
218
219         rc = bnxt_mq_rx_configure(bp);
220         if (rc) {
221                 RTE_LOG(ERR, PMD, "MQ mode configure failure rc: %x\n", rc);
222                 goto err_out;
223         }
224
225         /* VNIC configuration */
226         for (i = 0; i < bp->nr_vnics; i++) {
227                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
228
229                 rc = bnxt_hwrm_vnic_alloc(bp, vnic);
230                 if (rc) {
231                         RTE_LOG(ERR, PMD, "HWRM vnic %d alloc failure rc: %x\n",
232                                 i, rc);
233                         goto err_out;
234                 }
235
236                 rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic);
237                 if (rc) {
238                         RTE_LOG(ERR, PMD,
239                                 "HWRM vnic %d ctx alloc failure rc: %x\n",
240                                 i, rc);
241                         goto err_out;
242                 }
243
244                 rc = bnxt_hwrm_vnic_cfg(bp, vnic);
245                 if (rc) {
246                         RTE_LOG(ERR, PMD, "HWRM vnic %d cfg failure rc: %x\n",
247                                 i, rc);
248                         goto err_out;
249                 }
250
251                 rc = bnxt_set_hwrm_vnic_filters(bp, vnic);
252                 if (rc) {
253                         RTE_LOG(ERR, PMD,
254                                 "HWRM vnic %d filter failure rc: %x\n",
255                                 i, rc);
256                         goto err_out;
257                 }
258                 if (vnic->rss_table && vnic->hash_type) {
259                         /*
260                          * Fill the RSS hash & redirection table with
261                          * ring group ids for all VNICs
262                          */
263                         for (rss_idx = 0, fw_idx = 0;
264                              rss_idx < HW_HASH_INDEX_SIZE;
265                              rss_idx++, fw_idx++) {
266                                 if (vnic->fw_grp_ids[fw_idx] ==
267                                     INVALID_HW_RING_ID)
268                                         fw_idx = 0;
269                                 vnic->rss_table[rss_idx] =
270                                                 vnic->fw_grp_ids[fw_idx];
271                         }
272                         rc = bnxt_hwrm_vnic_rss_cfg(bp, vnic);
273                         if (rc) {
274                                 RTE_LOG(ERR, PMD,
275                                         "HWRM vnic %d set RSS failure rc: %x\n",
276                                         i, rc);
277                                 goto err_out;
278                         }
279                 }
280         }
281         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, &bp->vnic_info[0]);
282         if (rc) {
283                 RTE_LOG(ERR, PMD,
284                         "HWRM cfa l2 rx mask failure rc: %x\n", rc);
285                 goto err_out;
286         }
287
288         rc = bnxt_get_hwrm_link_config(bp, &new);
289         if (rc) {
290                 RTE_LOG(ERR, PMD, "HWRM Get link config failure rc: %x\n", rc);
291                 goto err_out;
292         }
293
294         if (!bp->link_info.link_up) {
295                 rc = bnxt_set_hwrm_link_config(bp, true);
296                 if (rc) {
297                         RTE_LOG(ERR, PMD,
298                                 "HWRM link config failure rc: %x\n", rc);
299                         goto err_out;
300                 }
301         }
302
303         return 0;
304
305 err_out:
306         bnxt_free_all_hwrm_resources(bp);
307
308         return rc;
309 }
310
311 static int bnxt_shutdown_nic(struct bnxt *bp)
312 {
313         bnxt_free_all_hwrm_resources(bp);
314         bnxt_free_all_filters(bp);
315         bnxt_free_all_vnics(bp);
316         return 0;
317 }
318
319 static int bnxt_init_nic(struct bnxt *bp)
320 {
321         int rc;
322
323         bnxt_init_ring_grps(bp);
324         bnxt_init_vnics(bp);
325         bnxt_init_filters(bp);
326
327         rc = bnxt_init_chip(bp);
328         if (rc)
329                 return rc;
330
331         return 0;
332 }
333
334 /*
335  * Device configuration and status function
336  */
337
338 static void bnxt_dev_info_get_op(struct rte_eth_dev *eth_dev,
339                                   struct rte_eth_dev_info *dev_info)
340 {
341         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
342         uint16_t max_vnics, i, j, vpool, vrxq;
343
344         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
345
346         /* MAC Specifics */
347         dev_info->max_mac_addrs = MAX_NUM_MAC_ADDR;
348         dev_info->max_hash_mac_addrs = 0;
349
350         /* PF/VF specifics */
351         if (BNXT_PF(bp))
352                 dev_info->max_vfs = bp->pdev->max_vfs;
353         dev_info->max_rx_queues = bp->max_rx_rings;
354         dev_info->max_tx_queues = bp->max_tx_rings;
355         dev_info->reta_size = bp->max_rsscos_ctx;
356         max_vnics = bp->max_vnics;
357
358         /* Fast path specifics */
359         dev_info->min_rx_bufsize = 1;
360         dev_info->max_rx_pktlen = BNXT_MAX_MTU + ETHER_HDR_LEN + ETHER_CRC_LEN
361                                   + VLAN_TAG_SIZE;
362         dev_info->rx_offload_capa = 0;
363         dev_info->tx_offload_capa = DEV_TX_OFFLOAD_IPV4_CKSUM |
364                                         DEV_TX_OFFLOAD_TCP_CKSUM |
365                                         DEV_TX_OFFLOAD_UDP_CKSUM |
366                                         DEV_TX_OFFLOAD_TCP_TSO |
367                                         DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM |
368                                         DEV_TX_OFFLOAD_VXLAN_TNL_TSO |
369                                         DEV_TX_OFFLOAD_GRE_TNL_TSO |
370                                         DEV_TX_OFFLOAD_IPIP_TNL_TSO |
371                                         DEV_TX_OFFLOAD_GENEVE_TNL_TSO;
372
373         /* *INDENT-OFF* */
374         dev_info->default_rxconf = (struct rte_eth_rxconf) {
375                 .rx_thresh = {
376                         .pthresh = 8,
377                         .hthresh = 8,
378                         .wthresh = 0,
379                 },
380                 .rx_free_thresh = 32,
381                 .rx_drop_en = 0,
382         };
383
384         dev_info->default_txconf = (struct rte_eth_txconf) {
385                 .tx_thresh = {
386                         .pthresh = 32,
387                         .hthresh = 0,
388                         .wthresh = 0,
389                 },
390                 .tx_free_thresh = 32,
391                 .tx_rs_thresh = 32,
392                 .txq_flags = ETH_TXQ_FLAGS_NOMULTSEGS |
393                              ETH_TXQ_FLAGS_NOOFFLOADS,
394         };
395         eth_dev->data->dev_conf.intr_conf.lsc = 1;
396
397         /* *INDENT-ON* */
398
399         /*
400          * TODO: default_rxconf, default_txconf, rx_desc_lim, and tx_desc_lim
401          *       need further investigation.
402          */
403
404         /* VMDq resources */
405         vpool = 64; /* ETH_64_POOLS */
406         vrxq = 128; /* ETH_VMDQ_DCB_NUM_QUEUES */
407         for (i = 0; i < 4; vpool >>= 1, i++) {
408                 if (max_vnics > vpool) {
409                         for (j = 0; j < 5; vrxq >>= 1, j++) {
410                                 if (dev_info->max_rx_queues > vrxq) {
411                                         if (vpool > vrxq)
412                                                 vpool = vrxq;
413                                         goto found;
414                                 }
415                         }
416                         /* Not enough resources to support VMDq */
417                         break;
418                 }
419         }
420         /* Not enough resources to support VMDq */
421         vpool = 0;
422         vrxq = 0;
423 found:
424         dev_info->max_vmdq_pools = vpool;
425         dev_info->vmdq_queue_num = vrxq;
426
427         dev_info->vmdq_pool_base = 0;
428         dev_info->vmdq_queue_base = 0;
429 }
430
431 /* Configure the device based on the configuration provided */
432 static int bnxt_dev_configure_op(struct rte_eth_dev *eth_dev)
433 {
434         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
435
436         bp->rx_queues = (void *)eth_dev->data->rx_queues;
437         bp->tx_queues = (void *)eth_dev->data->tx_queues;
438
439         /* Inherit new configurations */
440         bp->rx_nr_rings = eth_dev->data->nb_rx_queues;
441         bp->tx_nr_rings = eth_dev->data->nb_tx_queues;
442         bp->rx_cp_nr_rings = bp->rx_nr_rings;
443         bp->tx_cp_nr_rings = bp->tx_nr_rings;
444
445         if (eth_dev->data->dev_conf.rxmode.jumbo_frame)
446                 eth_dev->data->mtu =
447                                 eth_dev->data->dev_conf.rxmode.max_rx_pkt_len -
448                                 ETHER_HDR_LEN - ETHER_CRC_LEN - VLAN_TAG_SIZE;
449         return 0;
450 }
451
452 static inline int
453 rte_bnxt_atomic_write_link_status(struct rte_eth_dev *eth_dev,
454                                 struct rte_eth_link *link)
455 {
456         struct rte_eth_link *dst = &eth_dev->data->dev_link;
457         struct rte_eth_link *src = link;
458
459         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
460                                         *(uint64_t *)src) == 0)
461                 return 1;
462
463         return 0;
464 }
465
466 static void bnxt_print_link_info(struct rte_eth_dev *eth_dev)
467 {
468         struct rte_eth_link *link = &eth_dev->data->dev_link;
469
470         if (link->link_status)
471                 RTE_LOG(INFO, PMD, "Port %d Link Up - speed %u Mbps - %s\n",
472                         (uint8_t)(eth_dev->data->port_id),
473                         (uint32_t)link->link_speed,
474                         (link->link_duplex == ETH_LINK_FULL_DUPLEX) ?
475                         ("full-duplex") : ("half-duplex\n"));
476         else
477                 RTE_LOG(INFO, PMD, "Port %d Link Down\n",
478                         (uint8_t)(eth_dev->data->port_id));
479 }
480
481 static int bnxt_dev_lsc_intr_setup(struct rte_eth_dev *eth_dev)
482 {
483         bnxt_print_link_info(eth_dev);
484         return 0;
485 }
486
487 static int bnxt_dev_start_op(struct rte_eth_dev *eth_dev)
488 {
489         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
490         int rc;
491
492         bp->dev_stopped = 0;
493
494         rc = bnxt_init_nic(bp);
495         if (rc)
496                 goto error;
497
498         bnxt_link_update_op(eth_dev, 0);
499         return 0;
500
501 error:
502         bnxt_shutdown_nic(bp);
503         bnxt_free_tx_mbufs(bp);
504         bnxt_free_rx_mbufs(bp);
505         return rc;
506 }
507
508 static int bnxt_dev_set_link_up_op(struct rte_eth_dev *eth_dev)
509 {
510         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
511
512         eth_dev->data->dev_link.link_status = 1;
513         bnxt_set_hwrm_link_config(bp, true);
514         return 0;
515 }
516
517 static int bnxt_dev_set_link_down_op(struct rte_eth_dev *eth_dev)
518 {
519         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
520
521         eth_dev->data->dev_link.link_status = 0;
522         bnxt_set_hwrm_link_config(bp, false);
523         return 0;
524 }
525
526 /* Unload the driver, release resources */
527 static void bnxt_dev_stop_op(struct rte_eth_dev *eth_dev)
528 {
529         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
530
531         if (bp->eth_dev->data->dev_started) {
532                 /* TBD: STOP HW queues DMA */
533                 eth_dev->data->dev_link.link_status = 0;
534         }
535         bnxt_set_hwrm_link_config(bp, false);
536         bnxt_hwrm_port_clr_stats(bp);
537         bnxt_shutdown_nic(bp);
538         bp->dev_stopped = 1;
539 }
540
541 static void bnxt_dev_close_op(struct rte_eth_dev *eth_dev)
542 {
543         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
544
545         if (bp->dev_stopped == 0)
546                 bnxt_dev_stop_op(eth_dev);
547
548         bnxt_free_tx_mbufs(bp);
549         bnxt_free_rx_mbufs(bp);
550         bnxt_free_mem(bp);
551         if (eth_dev->data->mac_addrs != NULL) {
552                 rte_free(eth_dev->data->mac_addrs);
553                 eth_dev->data->mac_addrs = NULL;
554         }
555         if (bp->grp_info != NULL) {
556                 rte_free(bp->grp_info);
557                 bp->grp_info = NULL;
558         }
559 }
560
561 static void bnxt_mac_addr_remove_op(struct rte_eth_dev *eth_dev,
562                                     uint32_t index)
563 {
564         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
565         uint64_t pool_mask = eth_dev->data->mac_pool_sel[index];
566         struct bnxt_vnic_info *vnic;
567         struct bnxt_filter_info *filter, *temp_filter;
568         int i;
569
570         /*
571          * Loop through all VNICs from the specified filter flow pools to
572          * remove the corresponding MAC addr filter
573          */
574         for (i = 0; i < MAX_FF_POOLS; i++) {
575                 if (!(pool_mask & (1ULL << i)))
576                         continue;
577
578                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
579                         filter = STAILQ_FIRST(&vnic->filter);
580                         while (filter) {
581                                 temp_filter = STAILQ_NEXT(filter, next);
582                                 if (filter->mac_index == index) {
583                                         STAILQ_REMOVE(&vnic->filter, filter,
584                                                       bnxt_filter_info, next);
585                                         bnxt_hwrm_clear_filter(bp, filter);
586                                         filter->mac_index = INVALID_MAC_INDEX;
587                                         memset(&filter->l2_addr, 0,
588                                                ETHER_ADDR_LEN);
589                                         STAILQ_INSERT_TAIL(
590                                                         &bp->free_filter_list,
591                                                         filter, next);
592                                 }
593                                 filter = temp_filter;
594                         }
595                 }
596         }
597 }
598
599 static int bnxt_mac_addr_add_op(struct rte_eth_dev *eth_dev,
600                                 struct ether_addr *mac_addr,
601                                 uint32_t index, uint32_t pool)
602 {
603         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
604         struct bnxt_vnic_info *vnic = STAILQ_FIRST(&bp->ff_pool[pool]);
605         struct bnxt_filter_info *filter;
606
607         if (BNXT_VF(bp)) {
608                 RTE_LOG(ERR, PMD, "Cannot add MAC address to a VF interface\n");
609                 return -ENOTSUP;
610         }
611
612         if (!vnic) {
613                 RTE_LOG(ERR, PMD, "VNIC not found for pool %d!\n", pool);
614                 return -EINVAL;
615         }
616         /* Attach requested MAC address to the new l2_filter */
617         STAILQ_FOREACH(filter, &vnic->filter, next) {
618                 if (filter->mac_index == index) {
619                         RTE_LOG(ERR, PMD,
620                                 "MAC addr already existed for pool %d\n", pool);
621                         return -EINVAL;
622                 }
623         }
624         filter = bnxt_alloc_filter(bp);
625         if (!filter) {
626                 RTE_LOG(ERR, PMD, "L2 filter alloc failed\n");
627                 return -ENODEV;
628         }
629         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
630         filter->mac_index = index;
631         memcpy(filter->l2_addr, mac_addr, ETHER_ADDR_LEN);
632         return bnxt_hwrm_set_filter(bp, vnic, filter);
633 }
634
635 int bnxt_link_update_op(struct rte_eth_dev *eth_dev, int wait_to_complete)
636 {
637         int rc = 0;
638         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
639         struct rte_eth_link new;
640         unsigned int cnt = BNXT_LINK_WAIT_CNT;
641
642         memset(&new, 0, sizeof(new));
643         do {
644                 /* Retrieve link info from hardware */
645                 rc = bnxt_get_hwrm_link_config(bp, &new);
646                 if (rc) {
647                         new.link_speed = ETH_LINK_SPEED_100M;
648                         new.link_duplex = ETH_LINK_FULL_DUPLEX;
649                         RTE_LOG(ERR, PMD,
650                                 "Failed to retrieve link rc = 0x%x!\n", rc);
651                         goto out;
652                 }
653                 rte_delay_ms(BNXT_LINK_WAIT_INTERVAL);
654
655                 if (!wait_to_complete)
656                         break;
657         } while (!new.link_status && cnt--);
658
659 out:
660         /* Timed out or success */
661         if (new.link_status != eth_dev->data->dev_link.link_status ||
662         new.link_speed != eth_dev->data->dev_link.link_speed) {
663                 rte_bnxt_atomic_write_link_status(eth_dev, &new);
664                 bnxt_print_link_info(eth_dev);
665         }
666
667         return rc;
668 }
669
670 static void bnxt_promiscuous_enable_op(struct rte_eth_dev *eth_dev)
671 {
672         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
673         struct bnxt_vnic_info *vnic;
674
675         if (bp->vnic_info == NULL)
676                 return;
677
678         vnic = &bp->vnic_info[0];
679
680         vnic->flags |= BNXT_VNIC_INFO_PROMISC;
681         bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic);
682 }
683
684 static void bnxt_promiscuous_disable_op(struct rte_eth_dev *eth_dev)
685 {
686         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
687         struct bnxt_vnic_info *vnic;
688
689         if (bp->vnic_info == NULL)
690                 return;
691
692         vnic = &bp->vnic_info[0];
693
694         vnic->flags &= ~BNXT_VNIC_INFO_PROMISC;
695         bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic);
696 }
697
698 static void bnxt_allmulticast_enable_op(struct rte_eth_dev *eth_dev)
699 {
700         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
701         struct bnxt_vnic_info *vnic;
702
703         if (bp->vnic_info == NULL)
704                 return;
705
706         vnic = &bp->vnic_info[0];
707
708         vnic->flags |= BNXT_VNIC_INFO_ALLMULTI;
709         bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic);
710 }
711
712 static void bnxt_allmulticast_disable_op(struct rte_eth_dev *eth_dev)
713 {
714         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
715         struct bnxt_vnic_info *vnic;
716
717         if (bp->vnic_info == NULL)
718                 return;
719
720         vnic = &bp->vnic_info[0];
721
722         vnic->flags &= ~BNXT_VNIC_INFO_ALLMULTI;
723         bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic);
724 }
725
726 static int bnxt_reta_update_op(struct rte_eth_dev *eth_dev,
727                             struct rte_eth_rss_reta_entry64 *reta_conf,
728                             uint16_t reta_size)
729 {
730         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
731         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
732         struct bnxt_vnic_info *vnic;
733         int i;
734
735         if (!(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG))
736                 return -EINVAL;
737
738         if (reta_size != HW_HASH_INDEX_SIZE) {
739                 RTE_LOG(ERR, PMD, "The configured hash table lookup size "
740                         "(%d) must equal the size supported by the hardware "
741                         "(%d)\n", reta_size, HW_HASH_INDEX_SIZE);
742                 return -EINVAL;
743         }
744         /* Update the RSS VNIC(s) */
745         for (i = 0; i < MAX_FF_POOLS; i++) {
746                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
747                         memcpy(vnic->rss_table, reta_conf, reta_size);
748
749                         bnxt_hwrm_vnic_rss_cfg(bp, vnic);
750                 }
751         }
752         return 0;
753 }
754
755 static int bnxt_reta_query_op(struct rte_eth_dev *eth_dev,
756                               struct rte_eth_rss_reta_entry64 *reta_conf,
757                               uint16_t reta_size)
758 {
759         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
760         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
761         struct rte_intr_handle *intr_handle
762                 = &bp->pdev->intr_handle;
763
764         /* Retrieve from the default VNIC */
765         if (!vnic)
766                 return -EINVAL;
767         if (!vnic->rss_table)
768                 return -EINVAL;
769
770         if (reta_size != HW_HASH_INDEX_SIZE) {
771                 RTE_LOG(ERR, PMD, "The configured hash table lookup size "
772                         "(%d) must equal the size supported by the hardware "
773                         "(%d)\n", reta_size, HW_HASH_INDEX_SIZE);
774                 return -EINVAL;
775         }
776         /* EW - need to revisit here copying from u64 to u16 */
777         memcpy(reta_conf, vnic->rss_table, reta_size);
778
779         if (rte_intr_allow_others(intr_handle)) {
780                 if (eth_dev->data->dev_conf.intr_conf.lsc != 0)
781                         bnxt_dev_lsc_intr_setup(eth_dev);
782         }
783
784         return 0;
785 }
786
787 static int bnxt_rss_hash_update_op(struct rte_eth_dev *eth_dev,
788                                    struct rte_eth_rss_conf *rss_conf)
789 {
790         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
791         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
792         struct bnxt_vnic_info *vnic;
793         uint16_t hash_type = 0;
794         int i;
795
796         /*
797          * If RSS enablement were different than dev_configure,
798          * then return -EINVAL
799          */
800         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
801                 if (!rss_conf->rss_hf)
802                         return -EINVAL;
803         } else {
804                 if (rss_conf->rss_hf & BNXT_ETH_RSS_SUPPORT)
805                         return -EINVAL;
806         }
807         if (rss_conf->rss_hf & ETH_RSS_IPV4)
808                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
809         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_TCP)
810                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
811         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
812                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
813         if (rss_conf->rss_hf & ETH_RSS_IPV6)
814                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
815         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_TCP)
816                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
817         if (rss_conf->rss_hf & ETH_RSS_NONFRAG_IPV6_UDP)
818                 hash_type |= HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
819
820         /* Update the RSS VNIC(s) */
821         for (i = 0; i < MAX_FF_POOLS; i++) {
822                 STAILQ_FOREACH(vnic, &bp->ff_pool[i], next) {
823                         vnic->hash_type = hash_type;
824
825                         /*
826                          * Use the supplied key if the key length is
827                          * acceptable and the rss_key is not NULL
828                          */
829                         if (rss_conf->rss_key &&
830                             rss_conf->rss_key_len <= HW_HASH_KEY_SIZE)
831                                 memcpy(vnic->rss_hash_key, rss_conf->rss_key,
832                                        rss_conf->rss_key_len);
833
834                         bnxt_hwrm_vnic_rss_cfg(bp, vnic);
835                 }
836         }
837         return 0;
838 }
839
840 static int bnxt_rss_hash_conf_get_op(struct rte_eth_dev *eth_dev,
841                                      struct rte_eth_rss_conf *rss_conf)
842 {
843         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
844         struct bnxt_vnic_info *vnic = &bp->vnic_info[0];
845         int len;
846         uint32_t hash_types;
847
848         /* RSS configuration is the same for all VNICs */
849         if (vnic && vnic->rss_hash_key) {
850                 if (rss_conf->rss_key) {
851                         len = rss_conf->rss_key_len <= HW_HASH_KEY_SIZE ?
852                               rss_conf->rss_key_len : HW_HASH_KEY_SIZE;
853                         memcpy(rss_conf->rss_key, vnic->rss_hash_key, len);
854                 }
855
856                 hash_types = vnic->hash_type;
857                 rss_conf->rss_hf = 0;
858                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4) {
859                         rss_conf->rss_hf |= ETH_RSS_IPV4;
860                         hash_types &= ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
861                 }
862                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4) {
863                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
864                         hash_types &=
865                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
866                 }
867                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4) {
868                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
869                         hash_types &=
870                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
871                 }
872                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6) {
873                         rss_conf->rss_hf |= ETH_RSS_IPV6;
874                         hash_types &= ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
875                 }
876                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6) {
877                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
878                         hash_types &=
879                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
880                 }
881                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6) {
882                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP;
883                         hash_types &=
884                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
885                 }
886                 if (hash_types) {
887                         RTE_LOG(ERR, PMD,
888                                 "Unknwon RSS config from firmware (%08x), RSS disabled",
889                                 vnic->hash_type);
890                         return -ENOTSUP;
891                 }
892         } else {
893                 rss_conf->rss_hf = 0;
894         }
895         return 0;
896 }
897
898 static int bnxt_flow_ctrl_get_op(struct rte_eth_dev *dev,
899                                struct rte_eth_fc_conf *fc_conf)
900 {
901         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
902         struct rte_eth_link link_info;
903         int rc;
904
905         rc = bnxt_get_hwrm_link_config(bp, &link_info);
906         if (rc)
907                 return rc;
908
909         memset(fc_conf, 0, sizeof(*fc_conf));
910         if (bp->link_info.auto_pause)
911                 fc_conf->autoneg = 1;
912         switch (bp->link_info.pause) {
913         case 0:
914                 fc_conf->mode = RTE_FC_NONE;
915                 break;
916         case HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX:
917                 fc_conf->mode = RTE_FC_TX_PAUSE;
918                 break;
919         case HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX:
920                 fc_conf->mode = RTE_FC_RX_PAUSE;
921                 break;
922         case (HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
923                         HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX):
924                 fc_conf->mode = RTE_FC_FULL;
925                 break;
926         }
927         return 0;
928 }
929
930 static int bnxt_flow_ctrl_set_op(struct rte_eth_dev *dev,
931                                struct rte_eth_fc_conf *fc_conf)
932 {
933         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
934
935         if (BNXT_NPAR_PF(bp) || BNXT_VF(bp)) {
936                 RTE_LOG(ERR, PMD, "Flow Control Settings cannot be modified\n");
937                 return -ENOTSUP;
938         }
939
940         switch (fc_conf->mode) {
941         case RTE_FC_NONE:
942                 bp->link_info.auto_pause = 0;
943                 bp->link_info.force_pause = 0;
944                 break;
945         case RTE_FC_RX_PAUSE:
946                 if (fc_conf->autoneg) {
947                         bp->link_info.auto_pause =
948                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_RX;
949                         bp->link_info.force_pause = 0;
950                 } else {
951                         bp->link_info.auto_pause = 0;
952                         bp->link_info.force_pause =
953                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_RX;
954                 }
955                 break;
956         case RTE_FC_TX_PAUSE:
957                 if (fc_conf->autoneg) {
958                         bp->link_info.auto_pause =
959                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_TX;
960                         bp->link_info.force_pause = 0;
961                 } else {
962                         bp->link_info.auto_pause = 0;
963                         bp->link_info.force_pause =
964                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_TX;
965                 }
966                 break;
967         case RTE_FC_FULL:
968                 if (fc_conf->autoneg) {
969                         bp->link_info.auto_pause =
970                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_TX |
971                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_RX;
972                         bp->link_info.force_pause = 0;
973                 } else {
974                         bp->link_info.auto_pause = 0;
975                         bp->link_info.force_pause =
976                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_TX |
977                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_RX;
978                 }
979                 break;
980         }
981         return bnxt_set_hwrm_link_config(bp, true);
982 }
983
984 /* Add UDP tunneling port */
985 static int
986 bnxt_udp_tunnel_port_add_op(struct rte_eth_dev *eth_dev,
987                          struct rte_eth_udp_tunnel *udp_tunnel)
988 {
989         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
990         uint16_t tunnel_type = 0;
991         int rc = 0;
992
993         switch (udp_tunnel->prot_type) {
994         case RTE_TUNNEL_TYPE_VXLAN:
995                 if (bp->vxlan_port_cnt) {
996                         RTE_LOG(ERR, PMD, "Tunnel Port %d already programmed\n",
997                                 udp_tunnel->udp_port);
998                         if (bp->vxlan_port != udp_tunnel->udp_port) {
999                                 RTE_LOG(ERR, PMD, "Only one port allowed\n");
1000                                 return -ENOSPC;
1001                         }
1002                         bp->vxlan_port_cnt++;
1003                         return 0;
1004                 }
1005                 tunnel_type =
1006                         HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_VXLAN;
1007                 bp->vxlan_port_cnt++;
1008                 break;
1009         case RTE_TUNNEL_TYPE_GENEVE:
1010                 if (bp->geneve_port_cnt) {
1011                         RTE_LOG(ERR, PMD, "Tunnel Port %d already programmed\n",
1012                                 udp_tunnel->udp_port);
1013                         if (bp->geneve_port != udp_tunnel->udp_port) {
1014                                 RTE_LOG(ERR, PMD, "Only one port allowed\n");
1015                                 return -ENOSPC;
1016                         }
1017                         bp->geneve_port_cnt++;
1018                         return 0;
1019                 }
1020                 tunnel_type =
1021                         HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_GENEVE;
1022                 bp->geneve_port_cnt++;
1023                 break;
1024         default:
1025                 RTE_LOG(ERR, PMD, "Tunnel type is not supported\n");
1026                 return -ENOTSUP;
1027         }
1028         rc = bnxt_hwrm_tunnel_dst_port_alloc(bp, udp_tunnel->udp_port,
1029                                              tunnel_type);
1030         return rc;
1031 }
1032
1033 static int
1034 bnxt_udp_tunnel_port_del_op(struct rte_eth_dev *eth_dev,
1035                          struct rte_eth_udp_tunnel *udp_tunnel)
1036 {
1037         struct bnxt *bp = (struct bnxt *)eth_dev->data->dev_private;
1038         uint16_t tunnel_type = 0;
1039         uint16_t port = 0;
1040         int rc = 0;
1041
1042         switch (udp_tunnel->prot_type) {
1043         case RTE_TUNNEL_TYPE_VXLAN:
1044                 if (!bp->vxlan_port_cnt) {
1045                         RTE_LOG(ERR, PMD, "No Tunnel port configured yet\n");
1046                         return -EINVAL;
1047                 }
1048                 if (bp->vxlan_port != udp_tunnel->udp_port) {
1049                         RTE_LOG(ERR, PMD, "Req Port: %d. Configured port: %d\n",
1050                                 udp_tunnel->udp_port, bp->vxlan_port);
1051                         return -EINVAL;
1052                 }
1053                 if (--bp->vxlan_port_cnt)
1054                         return 0;
1055
1056                 tunnel_type =
1057                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN;
1058                 port = bp->vxlan_fw_dst_port_id;
1059                 break;
1060         case RTE_TUNNEL_TYPE_GENEVE:
1061                 if (!bp->geneve_port_cnt) {
1062                         RTE_LOG(ERR, PMD, "No Tunnel port configured yet\n");
1063                         return -EINVAL;
1064                 }
1065                 if (bp->geneve_port != udp_tunnel->udp_port) {
1066                         RTE_LOG(ERR, PMD, "Req Port: %d. Configured port: %d\n",
1067                                 udp_tunnel->udp_port, bp->geneve_port);
1068                         return -EINVAL;
1069                 }
1070                 if (--bp->geneve_port_cnt)
1071                         return 0;
1072
1073                 tunnel_type =
1074                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE;
1075                 port = bp->geneve_fw_dst_port_id;
1076                 break;
1077         default:
1078                 RTE_LOG(ERR, PMD, "Tunnel type is not supported\n");
1079                 return -ENOTSUP;
1080         }
1081
1082         rc = bnxt_hwrm_tunnel_dst_port_free(bp, port, tunnel_type);
1083         if (!rc) {
1084                 if (tunnel_type ==
1085                     HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN)
1086                         bp->vxlan_port = 0;
1087                 if (tunnel_type ==
1088                     HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE)
1089                         bp->geneve_port = 0;
1090         }
1091         return rc;
1092 }
1093
1094 /*
1095  * Initialization
1096  */
1097
1098 static const struct eth_dev_ops bnxt_dev_ops = {
1099         .dev_infos_get = bnxt_dev_info_get_op,
1100         .dev_close = bnxt_dev_close_op,
1101         .dev_configure = bnxt_dev_configure_op,
1102         .dev_start = bnxt_dev_start_op,
1103         .dev_stop = bnxt_dev_stop_op,
1104         .dev_set_link_up = bnxt_dev_set_link_up_op,
1105         .dev_set_link_down = bnxt_dev_set_link_down_op,
1106         .stats_get = bnxt_stats_get_op,
1107         .stats_reset = bnxt_stats_reset_op,
1108         .rx_queue_setup = bnxt_rx_queue_setup_op,
1109         .rx_queue_release = bnxt_rx_queue_release_op,
1110         .tx_queue_setup = bnxt_tx_queue_setup_op,
1111         .tx_queue_release = bnxt_tx_queue_release_op,
1112         .reta_update = bnxt_reta_update_op,
1113         .reta_query = bnxt_reta_query_op,
1114         .rss_hash_update = bnxt_rss_hash_update_op,
1115         .rss_hash_conf_get = bnxt_rss_hash_conf_get_op,
1116         .link_update = bnxt_link_update_op,
1117         .promiscuous_enable = bnxt_promiscuous_enable_op,
1118         .promiscuous_disable = bnxt_promiscuous_disable_op,
1119         .allmulticast_enable = bnxt_allmulticast_enable_op,
1120         .allmulticast_disable = bnxt_allmulticast_disable_op,
1121         .mac_addr_add = bnxt_mac_addr_add_op,
1122         .mac_addr_remove = bnxt_mac_addr_remove_op,
1123         .flow_ctrl_get = bnxt_flow_ctrl_get_op,
1124         .flow_ctrl_set = bnxt_flow_ctrl_set_op,
1125         .udp_tunnel_port_add  = bnxt_udp_tunnel_port_add_op,
1126         .udp_tunnel_port_del  = bnxt_udp_tunnel_port_del_op,
1127         .xstats_get = bnxt_dev_xstats_get_op,
1128         .xstats_get_names = bnxt_dev_xstats_get_names_op,
1129         .xstats_reset = bnxt_dev_xstats_reset_op,
1130 };
1131
1132 static bool bnxt_vf_pciid(uint16_t id)
1133 {
1134         if (id == BROADCOM_DEV_ID_57304_VF ||
1135             id == BROADCOM_DEV_ID_57406_VF ||
1136             id == BROADCOM_DEV_ID_5731X_VF ||
1137             id == BROADCOM_DEV_ID_5741X_VF ||
1138             id == BROADCOM_DEV_ID_57414_VF)
1139                 return true;
1140         return false;
1141 }
1142
1143 static int bnxt_init_board(struct rte_eth_dev *eth_dev)
1144 {
1145         struct bnxt *bp = eth_dev->data->dev_private;
1146         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1147         int rc;
1148
1149         /* enable device (incl. PCI PM wakeup), and bus-mastering */
1150         if (!pci_dev->mem_resource[0].addr) {
1151                 RTE_LOG(ERR, PMD,
1152                         "Cannot find PCI device base address, aborting\n");
1153                 rc = -ENODEV;
1154                 goto init_err_disable;
1155         }
1156
1157         bp->eth_dev = eth_dev;
1158         bp->pdev = pci_dev;
1159
1160         bp->bar0 = (void *)pci_dev->mem_resource[0].addr;
1161         if (!bp->bar0) {
1162                 RTE_LOG(ERR, PMD, "Cannot map device registers, aborting\n");
1163                 rc = -ENOMEM;
1164                 goto init_err_release;
1165         }
1166         return 0;
1167
1168 init_err_release:
1169         if (bp->bar0)
1170                 bp->bar0 = NULL;
1171
1172 init_err_disable:
1173
1174         return rc;
1175 }
1176
1177 static int bnxt_dev_uninit(struct rte_eth_dev *eth_dev);
1178
1179 #define ALLOW_FUNC(x)   \
1180         { \
1181                 typeof(x) arg = (x); \
1182                 bp->pf.vf_req_fwd[((arg) >> 5)] &= \
1183                 ~rte_cpu_to_le_32(1 << ((arg) & 0x1f)); \
1184         }
1185 static int
1186 bnxt_dev_init(struct rte_eth_dev *eth_dev)
1187 {
1188         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1189         char mz_name[RTE_MEMZONE_NAMESIZE];
1190         const struct rte_memzone *mz = NULL;
1191         static int version_printed;
1192         uint32_t total_alloc_len;
1193         phys_addr_t mz_phys_addr;
1194         struct bnxt *bp;
1195         int rc;
1196
1197         if (version_printed++ == 0)
1198                 RTE_LOG(INFO, PMD, "%s\n", bnxt_version);
1199
1200         rte_eth_copy_pci_info(eth_dev, pci_dev);
1201         eth_dev->data->dev_flags |= RTE_ETH_DEV_DETACHABLE;
1202
1203         bp = eth_dev->data->dev_private;
1204         bp->dev_stopped = 1;
1205
1206         if (bnxt_vf_pciid(pci_dev->id.device_id))
1207                 bp->flags |= BNXT_FLAG_VF;
1208
1209         rc = bnxt_init_board(eth_dev);
1210         if (rc) {
1211                 RTE_LOG(ERR, PMD,
1212                         "Board initialization failed rc: %x\n", rc);
1213                 goto error;
1214         }
1215         eth_dev->dev_ops = &bnxt_dev_ops;
1216         eth_dev->rx_pkt_burst = &bnxt_recv_pkts;
1217         eth_dev->tx_pkt_burst = &bnxt_xmit_pkts;
1218
1219         if (BNXT_PF(bp) && pci_dev->id.device_id != BROADCOM_DEV_ID_NS2) {
1220                 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
1221                          "bnxt_%04x:%02x:%02x:%02x-%s", pci_dev->addr.domain,
1222                          pci_dev->addr.bus, pci_dev->addr.devid,
1223                          pci_dev->addr.function, "rx_port_stats");
1224                 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
1225                 mz = rte_memzone_lookup(mz_name);
1226                 total_alloc_len = RTE_CACHE_LINE_ROUNDUP(
1227                                 sizeof(struct rx_port_stats) + 512);
1228                 if (!mz) {
1229                         mz = rte_memzone_reserve(mz_name, total_alloc_len,
1230                                                  SOCKET_ID_ANY,
1231                                                  RTE_MEMZONE_2MB |
1232                                                  RTE_MEMZONE_SIZE_HINT_ONLY);
1233                         if (mz == NULL)
1234                                 return -ENOMEM;
1235                 }
1236                 memset(mz->addr, 0, mz->len);
1237                 mz_phys_addr = mz->phys_addr;
1238                 if ((unsigned long)mz->addr == mz_phys_addr) {
1239                         RTE_LOG(WARNING, PMD,
1240                                 "Memzone physical address same as virtual.\n");
1241                         RTE_LOG(WARNING, PMD,
1242                                 "Using rte_mem_virt2phy()\n");
1243                         mz_phys_addr = rte_mem_virt2phy(mz->addr);
1244                         if (mz_phys_addr == 0) {
1245                                 RTE_LOG(ERR, PMD,
1246                                 "unable to map address to physical memory\n");
1247                                 return -ENOMEM;
1248                         }
1249                 }
1250
1251                 bp->rx_mem_zone = (const void *)mz;
1252                 bp->hw_rx_port_stats = mz->addr;
1253                 bp->hw_rx_port_stats_map = mz_phys_addr;
1254
1255                 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
1256                          "bnxt_%04x:%02x:%02x:%02x-%s", pci_dev->addr.domain,
1257                          pci_dev->addr.bus, pci_dev->addr.devid,
1258                          pci_dev->addr.function, "tx_port_stats");
1259                 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
1260                 mz = rte_memzone_lookup(mz_name);
1261                 total_alloc_len = RTE_CACHE_LINE_ROUNDUP(
1262                                 sizeof(struct tx_port_stats) + 512);
1263                 if (!mz) {
1264                         mz = rte_memzone_reserve(mz_name, total_alloc_len,
1265                                                  SOCKET_ID_ANY,
1266                                                  RTE_MEMZONE_2MB |
1267                                                  RTE_MEMZONE_SIZE_HINT_ONLY);
1268                         if (mz == NULL)
1269                                 return -ENOMEM;
1270                 }
1271                 memset(mz->addr, 0, mz->len);
1272                 mz_phys_addr = mz->phys_addr;
1273                 if ((unsigned long)mz->addr == mz_phys_addr) {
1274                         RTE_LOG(WARNING, PMD,
1275                                 "Memzone physical address same as virtual.\n");
1276                         RTE_LOG(WARNING, PMD,
1277                                 "Using rte_mem_virt2phy()\n");
1278                         mz_phys_addr = rte_mem_virt2phy(mz->addr);
1279                         if (mz_phys_addr == 0) {
1280                                 RTE_LOG(ERR, PMD,
1281                                 "unable to map address to physical memory\n");
1282                                 return -ENOMEM;
1283                         }
1284                 }
1285
1286                 bp->tx_mem_zone = (const void *)mz;
1287                 bp->hw_tx_port_stats = mz->addr;
1288                 bp->hw_tx_port_stats_map = mz_phys_addr;
1289
1290                 bp->flags |= BNXT_FLAG_PORT_STATS;
1291         }
1292
1293         rc = bnxt_alloc_hwrm_resources(bp);
1294         if (rc) {
1295                 RTE_LOG(ERR, PMD,
1296                         "hwrm resource allocation failure rc: %x\n", rc);
1297                 goto error_free;
1298         }
1299         rc = bnxt_hwrm_ver_get(bp);
1300         if (rc)
1301                 goto error_free;
1302         bnxt_hwrm_queue_qportcfg(bp);
1303
1304         bnxt_hwrm_func_qcfg(bp);
1305
1306         /* Get the MAX capabilities for this function */
1307         rc = bnxt_hwrm_func_qcaps(bp);
1308         if (rc) {
1309                 RTE_LOG(ERR, PMD, "hwrm query capability failure rc: %x\n", rc);
1310                 goto error_free;
1311         }
1312         if (bp->max_tx_rings == 0) {
1313                 RTE_LOG(ERR, PMD, "No TX rings available!\n");
1314                 rc = -EBUSY;
1315                 goto error_free;
1316         }
1317         eth_dev->data->mac_addrs = rte_zmalloc("bnxt_mac_addr_tbl",
1318                                         ETHER_ADDR_LEN * MAX_NUM_MAC_ADDR, 0);
1319         if (eth_dev->data->mac_addrs == NULL) {
1320                 RTE_LOG(ERR, PMD,
1321                         "Failed to alloc %u bytes needed to store MAC addr tbl",
1322                         ETHER_ADDR_LEN * MAX_NUM_MAC_ADDR);
1323                 rc = -ENOMEM;
1324                 goto error_free;
1325         }
1326         /* Copy the permanent MAC from the qcap response address now. */
1327         memcpy(bp->mac_addr, bp->dflt_mac_addr, sizeof(bp->mac_addr));
1328         memcpy(&eth_dev->data->mac_addrs[0], bp->mac_addr, ETHER_ADDR_LEN);
1329         bp->grp_info = rte_zmalloc("bnxt_grp_info",
1330                                 sizeof(*bp->grp_info) * bp->max_ring_grps, 0);
1331         if (!bp->grp_info) {
1332                 RTE_LOG(ERR, PMD,
1333                         "Failed to alloc %zu bytes needed to store group info table\n",
1334                         sizeof(*bp->grp_info) * bp->max_ring_grps);
1335                 rc = -ENOMEM;
1336                 goto error_free;
1337         }
1338
1339         /* Forward all requests if firmware is new enough */
1340         if (((bp->fw_ver >= ((20 << 24) | (6 << 16) | (100 << 8))) &&
1341             (bp->fw_ver < ((20 << 24) | (7 << 16)))) ||
1342             ((bp->fw_ver >= ((20 << 24) | (8 << 16))))) {
1343                 memset(bp->pf.vf_req_fwd, 0xff, sizeof(bp->pf.vf_req_fwd));
1344         } else {
1345                 RTE_LOG(WARNING, PMD,
1346                         "Firmware too old for VF mailbox functionality\n");
1347                 memset(bp->pf.vf_req_fwd, 0, sizeof(bp->pf.vf_req_fwd));
1348         }
1349
1350         /*
1351          * The following are used for driver cleanup.  If we disallow these,
1352          * VF drivers can't clean up cleanly.
1353          */
1354         ALLOW_FUNC(HWRM_FUNC_DRV_UNRGTR);
1355         ALLOW_FUNC(HWRM_VNIC_FREE);
1356         ALLOW_FUNC(HWRM_RING_FREE);
1357         ALLOW_FUNC(HWRM_RING_GRP_FREE);
1358         ALLOW_FUNC(HWRM_VNIC_RSS_COS_LB_CTX_FREE);
1359         ALLOW_FUNC(HWRM_CFA_L2_FILTER_FREE);
1360         ALLOW_FUNC(HWRM_STAT_CTX_FREE);
1361         rc = bnxt_hwrm_func_driver_register(bp);
1362         if (rc) {
1363                 RTE_LOG(ERR, PMD,
1364                         "Failed to register driver");
1365                 rc = -EBUSY;
1366                 goto error_free;
1367         }
1368
1369         RTE_LOG(INFO, PMD,
1370                 DRV_MODULE_NAME " found at mem %" PRIx64 ", node addr %pM\n",
1371                 pci_dev->mem_resource[0].phys_addr,
1372                 pci_dev->mem_resource[0].addr);
1373
1374         rc = bnxt_hwrm_func_reset(bp);
1375         if (rc) {
1376                 RTE_LOG(ERR, PMD, "hwrm chip reset failure rc: %x\n", rc);
1377                 rc = -1;
1378                 goto error_free;
1379         }
1380
1381         if (BNXT_PF(bp)) {
1382                 //if (bp->pf.active_vfs) {
1383                         // TODO: Deallocate VF resources?
1384                 //}
1385                 if (bp->pdev->max_vfs) {
1386                         rc = bnxt_hwrm_allocate_vfs(bp, bp->pdev->max_vfs);
1387                         if (rc) {
1388                                 RTE_LOG(ERR, PMD, "Failed to allocate VFs\n");
1389                                 goto error_free;
1390                         }
1391                 } else {
1392                         rc = bnxt_hwrm_allocate_pf_only(bp);
1393                         if (rc) {
1394                                 RTE_LOG(ERR, PMD,
1395                                         "Failed to allocate PF resources\n");
1396                                 goto error_free;
1397                         }
1398                 }
1399         }
1400
1401         rc = bnxt_setup_int(bp);
1402         if (rc)
1403                 goto error_free;
1404
1405         rc = bnxt_alloc_mem(bp);
1406         if (rc)
1407                 goto error_free_int;
1408
1409         rc = bnxt_request_int(bp);
1410         if (rc)
1411                 goto error_free_int;
1412
1413         rc = bnxt_alloc_def_cp_ring(bp);
1414         if (rc)
1415                 goto error_free_int;
1416
1417         bnxt_enable_int(bp);
1418
1419         return 0;
1420
1421 error_free_int:
1422         bnxt_disable_int(bp);
1423         bnxt_free_def_cp_ring(bp);
1424         bnxt_hwrm_func_buf_unrgtr(bp);
1425         bnxt_free_int(bp);
1426         bnxt_free_mem(bp);
1427 error_free:
1428         bnxt_dev_uninit(eth_dev);
1429 error:
1430         return rc;
1431 }
1432
1433 static int
1434 bnxt_dev_uninit(struct rte_eth_dev *eth_dev) {
1435         struct bnxt *bp = eth_dev->data->dev_private;
1436         int rc;
1437
1438         bnxt_disable_int(bp);
1439         bnxt_free_int(bp);
1440         bnxt_free_mem(bp);
1441         if (eth_dev->data->mac_addrs != NULL) {
1442                 rte_free(eth_dev->data->mac_addrs);
1443                 eth_dev->data->mac_addrs = NULL;
1444         }
1445         if (bp->grp_info != NULL) {
1446                 rte_free(bp->grp_info);
1447                 bp->grp_info = NULL;
1448         }
1449         rc = bnxt_hwrm_func_driver_unregister(bp, 0);
1450         bnxt_free_hwrm_resources(bp);
1451         rte_memzone_free((const struct rte_memzone *)bp->tx_mem_zone);
1452         rte_memzone_free((const struct rte_memzone *)bp->rx_mem_zone);
1453         if (bp->dev_stopped == 0)
1454                 bnxt_dev_close_op(eth_dev);
1455         if (bp->pf.vf_info)
1456                 rte_free(bp->pf.vf_info);
1457         eth_dev->dev_ops = NULL;
1458         eth_dev->rx_pkt_burst = NULL;
1459         eth_dev->tx_pkt_burst = NULL;
1460
1461         return rc;
1462 }
1463
1464 int bnxt_rcv_msg_from_vf(struct bnxt *bp, uint16_t vf_id, void *msg)
1465 {
1466         struct rte_pmd_bnxt_mb_event_param cb_param;
1467
1468         cb_param.retval = RTE_PMD_BNXT_MB_EVENT_PROCEED;
1469         cb_param.vf_id = vf_id;
1470         cb_param.msg = msg;
1471
1472         _rte_eth_dev_callback_process(bp->eth_dev, RTE_ETH_EVENT_VF_MBOX,
1473                         &cb_param);
1474
1475         /* Default to approve */
1476         if (cb_param.retval == RTE_PMD_BNXT_MB_EVENT_PROCEED)
1477                 cb_param.retval = RTE_PMD_BNXT_MB_EVENT_NOOP_ACK;
1478
1479         return cb_param.retval == RTE_PMD_BNXT_MB_EVENT_NOOP_ACK ? true : false;
1480 }
1481
1482 static int bnxt_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
1483         struct rte_pci_device *pci_dev)
1484 {
1485         return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct bnxt),
1486                 bnxt_dev_init);
1487 }
1488
1489 static int bnxt_pci_remove(struct rte_pci_device *pci_dev)
1490 {
1491         return rte_eth_dev_pci_generic_remove(pci_dev, bnxt_dev_uninit);
1492 }
1493
1494 static struct rte_pci_driver bnxt_rte_pmd = {
1495         .id_table = bnxt_pci_id_map,
1496         .drv_flags = RTE_PCI_DRV_NEED_MAPPING |
1497                 RTE_PCI_DRV_INTR_LSC,
1498         .probe = bnxt_pci_probe,
1499         .remove = bnxt_pci_remove,
1500 };
1501
1502 RTE_PMD_REGISTER_PCI(net_bnxt, bnxt_rte_pmd);
1503 RTE_PMD_REGISTER_PCI_TABLE(net_bnxt, bnxt_pci_id_map);
1504 RTE_PMD_REGISTER_KMOD_DEP(net_bnxt, "* igb_uio | uio_pci_generic | vfio-pci");