9c3e0f9f36968210799c400f9cc05198be0b8d64
[dpdk.git] / drivers / net / bnxt / bnxt_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2014-2021 Broadcom
3  * All rights reserved.
4  */
5
6 #include <inttypes.h>
7 #include <stdbool.h>
8
9 #include <rte_dev.h>
10 #include <ethdev_driver.h>
11 #include <ethdev_pci.h>
12 #include <rte_malloc.h>
13 #include <rte_cycles.h>
14 #include <rte_alarm.h>
15 #include <rte_kvargs.h>
16 #include <rte_vect.h>
17
18 #include "bnxt.h"
19 #include "bnxt_filter.h"
20 #include "bnxt_hwrm.h"
21 #include "bnxt_irq.h"
22 #include "bnxt_reps.h"
23 #include "bnxt_ring.h"
24 #include "bnxt_rxq.h"
25 #include "bnxt_rxr.h"
26 #include "bnxt_stats.h"
27 #include "bnxt_txq.h"
28 #include "bnxt_txr.h"
29 #include "bnxt_vnic.h"
30 #include "hsi_struct_def_dpdk.h"
31 #include "bnxt_nvm_defs.h"
32 #include "bnxt_tf_common.h"
33 #include "ulp_flow_db.h"
34 #include "rte_pmd_bnxt.h"
35
36 #define DRV_MODULE_NAME         "bnxt"
37 static const char bnxt_version[] =
38         "Broadcom NetXtreme driver " DRV_MODULE_NAME;
39
40 /*
41  * The set of PCI devices this driver supports
42  */
43 static const struct rte_pci_id bnxt_pci_id_map[] = {
44         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM,
45                          BROADCOM_DEV_ID_STRATUS_NIC_VF1) },
46         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM,
47                          BROADCOM_DEV_ID_STRATUS_NIC_VF2) },
48         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_STRATUS_NIC) },
49         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414_VF) },
50         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57304_VF) },
51         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_NS2) },
52         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57406_VF) },
53         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57407_MF) },
54         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_5741X_VF) },
55         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_5731X_VF) },
56         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_MF) },
57         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57412) },
58         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414) },
59         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_RJ45) },
60         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_RJ45) },
61         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57412_MF) },
62         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57317_RJ45) },
63         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57417_SFP) },
64         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_SFP) },
65         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57317_SFP) },
66         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57414_MF) },
67         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57416_MF) },
68         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_58802) },
69         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_58804) },
70         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_58808) },
71         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_58802_VF) },
72         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57508) },
73         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57504) },
74         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57502) },
75         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57500_VF1) },
76         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57500_VF2) },
77         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57508_MF1) },
78         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57504_MF1) },
79         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57502_MF1) },
80         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57508_MF2) },
81         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57504_MF2) },
82         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_57502_MF2) },
83         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_58812) },
84         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_58814) },
85         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_58818) },
86         { RTE_PCI_DEVICE(PCI_VENDOR_ID_BROADCOM, BROADCOM_DEV_ID_58818_VF) },
87         { .vendor_id = 0, /* sentinel */ },
88 };
89
90 #define BNXT_DEVARG_TRUFLOW     "host-based-truflow"
91 #define BNXT_DEVARG_FLOW_XSTAT  "flow-xstat"
92 #define BNXT_DEVARG_MAX_NUM_KFLOWS  "max-num-kflows"
93 #define BNXT_DEVARG_REPRESENTOR "representor"
94 #define BNXT_DEVARG_REP_BASED_PF  "rep-based-pf"
95 #define BNXT_DEVARG_REP_IS_PF  "rep-is-pf"
96 #define BNXT_DEVARG_REP_Q_R2F  "rep-q-r2f"
97 #define BNXT_DEVARG_REP_Q_F2R  "rep-q-f2r"
98 #define BNXT_DEVARG_REP_FC_R2F  "rep-fc-r2f"
99 #define BNXT_DEVARG_REP_FC_F2R  "rep-fc-f2r"
100
101 static const char *const bnxt_dev_args[] = {
102         BNXT_DEVARG_REPRESENTOR,
103         BNXT_DEVARG_TRUFLOW,
104         BNXT_DEVARG_FLOW_XSTAT,
105         BNXT_DEVARG_MAX_NUM_KFLOWS,
106         BNXT_DEVARG_REP_BASED_PF,
107         BNXT_DEVARG_REP_IS_PF,
108         BNXT_DEVARG_REP_Q_R2F,
109         BNXT_DEVARG_REP_Q_F2R,
110         BNXT_DEVARG_REP_FC_R2F,
111         BNXT_DEVARG_REP_FC_F2R,
112         NULL
113 };
114
115 /*
116  * truflow == false to disable the feature
117  * truflow == true to enable the feature
118  */
119 #define BNXT_DEVARG_TRUFLOW_INVALID(truflow)    ((truflow) > 1)
120
121 /*
122  * flow_xstat == false to disable the feature
123  * flow_xstat == true to enable the feature
124  */
125 #define BNXT_DEVARG_FLOW_XSTAT_INVALID(flow_xstat)      ((flow_xstat) > 1)
126
127 /*
128  * rep_is_pf == false to indicate VF representor
129  * rep_is_pf == true to indicate PF representor
130  */
131 #define BNXT_DEVARG_REP_IS_PF_INVALID(rep_is_pf)        ((rep_is_pf) > 1)
132
133 /*
134  * rep_based_pf == Physical index of the PF
135  */
136 #define BNXT_DEVARG_REP_BASED_PF_INVALID(rep_based_pf)  ((rep_based_pf) > 15)
137 /*
138  * rep_q_r2f == Logical COS Queue index for the rep to endpoint direction
139  */
140 #define BNXT_DEVARG_REP_Q_R2F_INVALID(rep_q_r2f)        ((rep_q_r2f) > 3)
141
142 /*
143  * rep_q_f2r == Logical COS Queue index for the endpoint to rep direction
144  */
145 #define BNXT_DEVARG_REP_Q_F2R_INVALID(rep_q_f2r)        ((rep_q_f2r) > 3)
146
147 /*
148  * rep_fc_r2f == Flow control for the representor to endpoint direction
149  */
150 #define BNXT_DEVARG_REP_FC_R2F_INVALID(rep_fc_r2f)      ((rep_fc_r2f) > 1)
151
152 /*
153  * rep_fc_f2r == Flow control for the endpoint to representor direction
154  */
155 #define BNXT_DEVARG_REP_FC_F2R_INVALID(rep_fc_f2r)      ((rep_fc_f2r) > 1)
156
157 int bnxt_cfa_code_dynfield_offset = -1;
158
159 /*
160  * max_num_kflows must be >= 32
161  * and must be a power-of-2 supported value
162  * return: 1 -> invalid
163  *         0 -> valid
164  */
165 static int bnxt_devarg_max_num_kflow_invalid(uint16_t max_num_kflows)
166 {
167         if (max_num_kflows < 32 || !rte_is_power_of_2(max_num_kflows))
168                 return 1;
169         return 0;
170 }
171
172 static int bnxt_vlan_offload_set_op(struct rte_eth_dev *dev, int mask);
173 static int bnxt_dev_uninit(struct rte_eth_dev *eth_dev);
174 static int bnxt_init_resources(struct bnxt *bp, bool reconfig_dev);
175 static int bnxt_uninit_resources(struct bnxt *bp, bool reconfig_dev);
176 static void bnxt_cancel_fw_health_check(struct bnxt *bp);
177 static int bnxt_restore_vlan_filters(struct bnxt *bp);
178 static void bnxt_dev_recover(void *arg);
179 static void bnxt_free_error_recovery_info(struct bnxt *bp);
180 static void bnxt_free_rep_info(struct bnxt *bp);
181
182 int is_bnxt_in_error(struct bnxt *bp)
183 {
184         if (bp->flags & BNXT_FLAG_FATAL_ERROR)
185                 return -EIO;
186         if (bp->flags & BNXT_FLAG_FW_RESET)
187                 return -EBUSY;
188
189         return 0;
190 }
191
192 /***********************/
193
194 /*
195  * High level utility functions
196  */
197
198 static uint16_t bnxt_rss_ctxts(const struct bnxt *bp)
199 {
200         unsigned int num_rss_rings = RTE_MIN(bp->rx_nr_rings,
201                                              BNXT_RSS_TBL_SIZE_P5);
202
203         if (!BNXT_CHIP_P5(bp))
204                 return 1;
205
206         return RTE_ALIGN_MUL_CEIL(num_rss_rings,
207                                   BNXT_RSS_ENTRIES_PER_CTX_P5) /
208                                   BNXT_RSS_ENTRIES_PER_CTX_P5;
209 }
210
211 uint16_t bnxt_rss_hash_tbl_size(const struct bnxt *bp)
212 {
213         if (!BNXT_CHIP_P5(bp))
214                 return HW_HASH_INDEX_SIZE;
215
216         return bnxt_rss_ctxts(bp) * BNXT_RSS_ENTRIES_PER_CTX_P5;
217 }
218
219 static void bnxt_free_parent_info(struct bnxt *bp)
220 {
221         rte_free(bp->parent);
222         bp->parent = NULL;
223 }
224
225 static void bnxt_free_pf_info(struct bnxt *bp)
226 {
227         rte_free(bp->pf);
228         bp->pf = NULL;
229 }
230
231 static void bnxt_free_link_info(struct bnxt *bp)
232 {
233         rte_free(bp->link_info);
234         bp->link_info = NULL;
235 }
236
237 static void bnxt_free_leds_info(struct bnxt *bp)
238 {
239         if (BNXT_VF(bp))
240                 return;
241
242         rte_free(bp->leds);
243         bp->leds = NULL;
244 }
245
246 static void bnxt_free_flow_stats_info(struct bnxt *bp)
247 {
248         rte_free(bp->flow_stat);
249         bp->flow_stat = NULL;
250 }
251
252 static void bnxt_free_cos_queues(struct bnxt *bp)
253 {
254         rte_free(bp->rx_cos_queue);
255         bp->rx_cos_queue = NULL;
256         rte_free(bp->tx_cos_queue);
257         bp->tx_cos_queue = NULL;
258 }
259
260 static void bnxt_free_mem(struct bnxt *bp, bool reconfig)
261 {
262         bnxt_free_filter_mem(bp);
263         bnxt_free_vnic_attributes(bp);
264         bnxt_free_vnic_mem(bp);
265
266         /* tx/rx rings are configured as part of *_queue_setup callbacks.
267          * If the number of rings change across fw update,
268          * we don't have much choice except to warn the user.
269          */
270         if (!reconfig) {
271                 bnxt_free_stats(bp);
272                 bnxt_free_tx_rings(bp);
273                 bnxt_free_rx_rings(bp);
274         }
275         bnxt_free_async_cp_ring(bp);
276         bnxt_free_rxtx_nq_ring(bp);
277
278         rte_free(bp->grp_info);
279         bp->grp_info = NULL;
280 }
281
282 static int bnxt_alloc_parent_info(struct bnxt *bp)
283 {
284         bp->parent = rte_zmalloc("bnxt_parent_info",
285                                  sizeof(struct bnxt_parent_info), 0);
286         if (bp->parent == NULL)
287                 return -ENOMEM;
288
289         return 0;
290 }
291
292 static int bnxt_alloc_pf_info(struct bnxt *bp)
293 {
294         bp->pf = rte_zmalloc("bnxt_pf_info", sizeof(struct bnxt_pf_info), 0);
295         if (bp->pf == NULL)
296                 return -ENOMEM;
297
298         return 0;
299 }
300
301 static int bnxt_alloc_link_info(struct bnxt *bp)
302 {
303         bp->link_info =
304                 rte_zmalloc("bnxt_link_info", sizeof(struct bnxt_link_info), 0);
305         if (bp->link_info == NULL)
306                 return -ENOMEM;
307
308         return 0;
309 }
310
311 static int bnxt_alloc_leds_info(struct bnxt *bp)
312 {
313         if (BNXT_VF(bp))
314                 return 0;
315
316         bp->leds = rte_zmalloc("bnxt_leds",
317                                BNXT_MAX_LED * sizeof(struct bnxt_led_info),
318                                0);
319         if (bp->leds == NULL)
320                 return -ENOMEM;
321
322         return 0;
323 }
324
325 static int bnxt_alloc_cos_queues(struct bnxt *bp)
326 {
327         bp->rx_cos_queue =
328                 rte_zmalloc("bnxt_rx_cosq",
329                             BNXT_COS_QUEUE_COUNT *
330                             sizeof(struct bnxt_cos_queue_info),
331                             0);
332         if (bp->rx_cos_queue == NULL)
333                 return -ENOMEM;
334
335         bp->tx_cos_queue =
336                 rte_zmalloc("bnxt_tx_cosq",
337                             BNXT_COS_QUEUE_COUNT *
338                             sizeof(struct bnxt_cos_queue_info),
339                             0);
340         if (bp->tx_cos_queue == NULL)
341                 return -ENOMEM;
342
343         return 0;
344 }
345
346 static int bnxt_alloc_flow_stats_info(struct bnxt *bp)
347 {
348         bp->flow_stat = rte_zmalloc("bnxt_flow_xstat",
349                                     sizeof(struct bnxt_flow_stat_info), 0);
350         if (bp->flow_stat == NULL)
351                 return -ENOMEM;
352
353         return 0;
354 }
355
356 static int bnxt_alloc_mem(struct bnxt *bp, bool reconfig)
357 {
358         int rc;
359
360         rc = bnxt_alloc_ring_grps(bp);
361         if (rc)
362                 goto alloc_mem_err;
363
364         rc = bnxt_alloc_async_ring_struct(bp);
365         if (rc)
366                 goto alloc_mem_err;
367
368         rc = bnxt_alloc_vnic_mem(bp);
369         if (rc)
370                 goto alloc_mem_err;
371
372         rc = bnxt_alloc_vnic_attributes(bp);
373         if (rc)
374                 goto alloc_mem_err;
375
376         rc = bnxt_alloc_filter_mem(bp);
377         if (rc)
378                 goto alloc_mem_err;
379
380         rc = bnxt_alloc_async_cp_ring(bp);
381         if (rc)
382                 goto alloc_mem_err;
383
384         rc = bnxt_alloc_rxtx_nq_ring(bp);
385         if (rc)
386                 goto alloc_mem_err;
387
388         if (BNXT_FLOW_XSTATS_EN(bp)) {
389                 rc = bnxt_alloc_flow_stats_info(bp);
390                 if (rc)
391                         goto alloc_mem_err;
392         }
393
394         return 0;
395
396 alloc_mem_err:
397         bnxt_free_mem(bp, reconfig);
398         return rc;
399 }
400
401 static int bnxt_setup_one_vnic(struct bnxt *bp, uint16_t vnic_id)
402 {
403         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
404         struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
405         uint64_t rx_offloads = dev_conf->rxmode.offloads;
406         struct bnxt_rx_queue *rxq;
407         unsigned int j;
408         int rc;
409
410         rc = bnxt_vnic_grp_alloc(bp, vnic);
411         if (rc)
412                 goto err_out;
413
414         PMD_DRV_LOG(DEBUG, "vnic[%d] = %p vnic->fw_grp_ids = %p\n",
415                     vnic_id, vnic, vnic->fw_grp_ids);
416
417         rc = bnxt_hwrm_vnic_alloc(bp, vnic);
418         if (rc)
419                 goto err_out;
420
421         /* Alloc RSS context only if RSS mode is enabled */
422         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS) {
423                 int j, nr_ctxs = bnxt_rss_ctxts(bp);
424
425                 /* RSS table size in Thor is 512.
426                  * Cap max Rx rings to same value
427                  */
428                 if (bp->rx_nr_rings > BNXT_RSS_TBL_SIZE_P5) {
429                         PMD_DRV_LOG(ERR, "RxQ cnt %d > reta_size %d\n",
430                                     bp->rx_nr_rings, BNXT_RSS_TBL_SIZE_P5);
431                         goto err_out;
432                 }
433
434                 rc = 0;
435                 for (j = 0; j < nr_ctxs; j++) {
436                         rc = bnxt_hwrm_vnic_ctx_alloc(bp, vnic, j);
437                         if (rc)
438                                 break;
439                 }
440                 if (rc) {
441                         PMD_DRV_LOG(ERR,
442                                     "HWRM vnic %d ctx %d alloc failure rc: %x\n",
443                                     vnic_id, j, rc);
444                         goto err_out;
445                 }
446                 vnic->num_lb_ctxts = nr_ctxs;
447         }
448
449         /*
450          * Firmware sets pf pair in default vnic cfg. If the VLAN strip
451          * setting is not available at this time, it will not be
452          * configured correctly in the CFA.
453          */
454         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
455                 vnic->vlan_strip = true;
456         else
457                 vnic->vlan_strip = false;
458
459         rc = bnxt_hwrm_vnic_cfg(bp, vnic);
460         if (rc)
461                 goto err_out;
462
463         rc = bnxt_set_hwrm_vnic_filters(bp, vnic);
464         if (rc)
465                 goto err_out;
466
467         for (j = 0; j < bp->rx_num_qs_per_vnic; j++) {
468                 rxq = bp->eth_dev->data->rx_queues[j];
469
470                 PMD_DRV_LOG(DEBUG,
471                             "rxq[%d]->vnic=%p vnic->fw_grp_ids=%p\n",
472                             j, rxq->vnic, rxq->vnic->fw_grp_ids);
473
474                 if (BNXT_HAS_RING_GRPS(bp) && rxq->rx_deferred_start)
475                         rxq->vnic->fw_grp_ids[j] = INVALID_HW_RING_ID;
476                 else
477                         vnic->rx_queue_cnt++;
478         }
479
480         PMD_DRV_LOG(DEBUG, "vnic->rx_queue_cnt = %d\n", vnic->rx_queue_cnt);
481
482         rc = bnxt_vnic_rss_configure(bp, vnic);
483         if (rc)
484                 goto err_out;
485
486         bnxt_hwrm_vnic_plcmode_cfg(bp, vnic);
487
488         rc = bnxt_hwrm_vnic_tpa_cfg(bp, vnic,
489                                     (rx_offloads & DEV_RX_OFFLOAD_TCP_LRO) ?
490                                     true : false);
491         if (rc)
492                 goto err_out;
493
494         return 0;
495 err_out:
496         PMD_DRV_LOG(ERR, "HWRM vnic %d cfg failure rc: %x\n",
497                     vnic_id, rc);
498         return rc;
499 }
500
501 static int bnxt_register_fc_ctx_mem(struct bnxt *bp)
502 {
503         int rc = 0;
504
505         rc = bnxt_hwrm_ctx_rgtr(bp, bp->flow_stat->rx_fc_in_tbl.dma,
506                                 &bp->flow_stat->rx_fc_in_tbl.ctx_id);
507         if (rc)
508                 return rc;
509
510         PMD_DRV_LOG(DEBUG,
511                     "rx_fc_in_tbl.va = %p rx_fc_in_tbl.dma = %p"
512                     " rx_fc_in_tbl.ctx_id = %d\n",
513                     bp->flow_stat->rx_fc_in_tbl.va,
514                     (void *)((uintptr_t)bp->flow_stat->rx_fc_in_tbl.dma),
515                     bp->flow_stat->rx_fc_in_tbl.ctx_id);
516
517         rc = bnxt_hwrm_ctx_rgtr(bp, bp->flow_stat->rx_fc_out_tbl.dma,
518                                 &bp->flow_stat->rx_fc_out_tbl.ctx_id);
519         if (rc)
520                 return rc;
521
522         PMD_DRV_LOG(DEBUG,
523                     "rx_fc_out_tbl.va = %p rx_fc_out_tbl.dma = %p"
524                     " rx_fc_out_tbl.ctx_id = %d\n",
525                     bp->flow_stat->rx_fc_out_tbl.va,
526                     (void *)((uintptr_t)bp->flow_stat->rx_fc_out_tbl.dma),
527                     bp->flow_stat->rx_fc_out_tbl.ctx_id);
528
529         rc = bnxt_hwrm_ctx_rgtr(bp, bp->flow_stat->tx_fc_in_tbl.dma,
530                                 &bp->flow_stat->tx_fc_in_tbl.ctx_id);
531         if (rc)
532                 return rc;
533
534         PMD_DRV_LOG(DEBUG,
535                     "tx_fc_in_tbl.va = %p tx_fc_in_tbl.dma = %p"
536                     " tx_fc_in_tbl.ctx_id = %d\n",
537                     bp->flow_stat->tx_fc_in_tbl.va,
538                     (void *)((uintptr_t)bp->flow_stat->tx_fc_in_tbl.dma),
539                     bp->flow_stat->tx_fc_in_tbl.ctx_id);
540
541         rc = bnxt_hwrm_ctx_rgtr(bp, bp->flow_stat->tx_fc_out_tbl.dma,
542                                 &bp->flow_stat->tx_fc_out_tbl.ctx_id);
543         if (rc)
544                 return rc;
545
546         PMD_DRV_LOG(DEBUG,
547                     "tx_fc_out_tbl.va = %p tx_fc_out_tbl.dma = %p"
548                     " tx_fc_out_tbl.ctx_id = %d\n",
549                     bp->flow_stat->tx_fc_out_tbl.va,
550                     (void *)((uintptr_t)bp->flow_stat->tx_fc_out_tbl.dma),
551                     bp->flow_stat->tx_fc_out_tbl.ctx_id);
552
553         memset(bp->flow_stat->rx_fc_out_tbl.va,
554                0,
555                bp->flow_stat->rx_fc_out_tbl.size);
556         rc = bnxt_hwrm_cfa_counter_cfg(bp, BNXT_DIR_RX,
557                                        CFA_COUNTER_CFG_IN_COUNTER_TYPE_FC,
558                                        bp->flow_stat->rx_fc_out_tbl.ctx_id,
559                                        bp->flow_stat->max_fc,
560                                        true);
561         if (rc)
562                 return rc;
563
564         memset(bp->flow_stat->tx_fc_out_tbl.va,
565                0,
566                bp->flow_stat->tx_fc_out_tbl.size);
567         rc = bnxt_hwrm_cfa_counter_cfg(bp, BNXT_DIR_TX,
568                                        CFA_COUNTER_CFG_IN_COUNTER_TYPE_FC,
569                                        bp->flow_stat->tx_fc_out_tbl.ctx_id,
570                                        bp->flow_stat->max_fc,
571                                        true);
572
573         return rc;
574 }
575
576 static int bnxt_alloc_ctx_mem_buf(char *type, size_t size,
577                                   struct bnxt_ctx_mem_buf_info *ctx)
578 {
579         if (!ctx)
580                 return -EINVAL;
581
582         ctx->va = rte_zmalloc(type, size, 0);
583         if (ctx->va == NULL)
584                 return -ENOMEM;
585         rte_mem_lock_page(ctx->va);
586         ctx->size = size;
587         ctx->dma = rte_mem_virt2iova(ctx->va);
588         if (ctx->dma == RTE_BAD_IOVA)
589                 return -ENOMEM;
590
591         return 0;
592 }
593
594 static int bnxt_init_fc_ctx_mem(struct bnxt *bp)
595 {
596         struct rte_pci_device *pdev = bp->pdev;
597         char type[RTE_MEMZONE_NAMESIZE];
598         uint16_t max_fc;
599         int rc = 0;
600
601         max_fc = bp->flow_stat->max_fc;
602
603         sprintf(type, "bnxt_rx_fc_in_" PCI_PRI_FMT, pdev->addr.domain,
604                 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
605         /* 4 bytes for each counter-id */
606         rc = bnxt_alloc_ctx_mem_buf(type,
607                                     max_fc * 4,
608                                     &bp->flow_stat->rx_fc_in_tbl);
609         if (rc)
610                 return rc;
611
612         sprintf(type, "bnxt_rx_fc_out_" PCI_PRI_FMT, pdev->addr.domain,
613                 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
614         /* 16 bytes for each counter - 8 bytes pkt_count, 8 bytes byte_count */
615         rc = bnxt_alloc_ctx_mem_buf(type,
616                                     max_fc * 16,
617                                     &bp->flow_stat->rx_fc_out_tbl);
618         if (rc)
619                 return rc;
620
621         sprintf(type, "bnxt_tx_fc_in_" PCI_PRI_FMT, pdev->addr.domain,
622                 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
623         /* 4 bytes for each counter-id */
624         rc = bnxt_alloc_ctx_mem_buf(type,
625                                     max_fc * 4,
626                                     &bp->flow_stat->tx_fc_in_tbl);
627         if (rc)
628                 return rc;
629
630         sprintf(type, "bnxt_tx_fc_out_" PCI_PRI_FMT, pdev->addr.domain,
631                 pdev->addr.bus, pdev->addr.devid, pdev->addr.function);
632         /* 16 bytes for each counter - 8 bytes pkt_count, 8 bytes byte_count */
633         rc = bnxt_alloc_ctx_mem_buf(type,
634                                     max_fc * 16,
635                                     &bp->flow_stat->tx_fc_out_tbl);
636         if (rc)
637                 return rc;
638
639         rc = bnxt_register_fc_ctx_mem(bp);
640
641         return rc;
642 }
643
644 static int bnxt_init_ctx_mem(struct bnxt *bp)
645 {
646         int rc = 0;
647
648         if (!(bp->fw_cap & BNXT_FW_CAP_ADV_FLOW_COUNTERS) ||
649             !(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp)) ||
650             !BNXT_FLOW_XSTATS_EN(bp))
651                 return 0;
652
653         rc = bnxt_hwrm_cfa_counter_qcaps(bp, &bp->flow_stat->max_fc);
654         if (rc)
655                 return rc;
656
657         rc = bnxt_init_fc_ctx_mem(bp);
658
659         return rc;
660 }
661
662 static int bnxt_update_phy_setting(struct bnxt *bp)
663 {
664         struct rte_eth_link new;
665         int rc;
666
667         rc = bnxt_get_hwrm_link_config(bp, &new);
668         if (rc) {
669                 PMD_DRV_LOG(ERR, "Failed to get link settings\n");
670                 return rc;
671         }
672
673         /*
674          * On BCM957508-N2100 adapters, FW will not allow any user other
675          * than BMC to shutdown the port. bnxt_get_hwrm_link_config() call
676          * always returns link up. Force phy update always in that case.
677          */
678         if (!new.link_status || IS_BNXT_DEV_957508_N2100(bp)) {
679                 rc = bnxt_set_hwrm_link_config(bp, true);
680                 if (rc) {
681                         PMD_DRV_LOG(ERR, "Failed to update PHY settings\n");
682                         return rc;
683                 }
684         }
685
686         return rc;
687 }
688
689 static int bnxt_start_nic(struct bnxt *bp)
690 {
691         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(bp->eth_dev);
692         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
693         uint32_t intr_vector = 0;
694         uint32_t queue_id, base = BNXT_MISC_VEC_ID;
695         uint32_t vec = BNXT_MISC_VEC_ID;
696         unsigned int i, j;
697         int rc;
698
699         if (bp->eth_dev->data->mtu > RTE_ETHER_MTU) {
700                 bp->eth_dev->data->dev_conf.rxmode.offloads |=
701                         DEV_RX_OFFLOAD_JUMBO_FRAME;
702                 bp->flags |= BNXT_FLAG_JUMBO;
703         } else {
704                 bp->eth_dev->data->dev_conf.rxmode.offloads &=
705                         ~DEV_RX_OFFLOAD_JUMBO_FRAME;
706                 bp->flags &= ~BNXT_FLAG_JUMBO;
707         }
708
709         /* THOR does not support ring groups.
710          * But we will use the array to save RSS context IDs.
711          */
712         if (BNXT_CHIP_P5(bp))
713                 bp->max_ring_grps = BNXT_MAX_RSS_CTXTS_P5;
714
715         rc = bnxt_alloc_all_hwrm_stat_ctxs(bp);
716         if (rc) {
717                 PMD_DRV_LOG(ERR, "HWRM stat ctx alloc failure rc: %x\n", rc);
718                 goto err_out;
719         }
720
721         rc = bnxt_alloc_hwrm_rings(bp);
722         if (rc) {
723                 PMD_DRV_LOG(ERR, "HWRM ring alloc failure rc: %x\n", rc);
724                 goto err_out;
725         }
726
727         rc = bnxt_alloc_all_hwrm_ring_grps(bp);
728         if (rc) {
729                 PMD_DRV_LOG(ERR, "HWRM ring grp alloc failure: %x\n", rc);
730                 goto err_out;
731         }
732
733         if (!(bp->vnic_cap_flags & BNXT_VNIC_CAP_COS_CLASSIFY))
734                 goto skip_cosq_cfg;
735
736         for (j = 0, i = 0; i < BNXT_COS_QUEUE_COUNT; i++) {
737                 if (bp->rx_cos_queue[i].id != 0xff) {
738                         struct bnxt_vnic_info *vnic = &bp->vnic_info[j++];
739
740                         if (!vnic) {
741                                 PMD_DRV_LOG(ERR,
742                                             "Num pools more than FW profile\n");
743                                 rc = -EINVAL;
744                                 goto err_out;
745                         }
746                         vnic->cos_queue_id = bp->rx_cos_queue[i].id;
747                         bp->rx_cosq_cnt++;
748                 }
749         }
750
751 skip_cosq_cfg:
752         rc = bnxt_mq_rx_configure(bp);
753         if (rc) {
754                 PMD_DRV_LOG(ERR, "MQ mode configure failure rc: %x\n", rc);
755                 goto err_out;
756         }
757
758         /* default vnic 0 */
759         rc = bnxt_setup_one_vnic(bp, 0);
760         if (rc)
761                 goto err_out;
762         /* VNIC configuration */
763         if (BNXT_RFS_NEEDS_VNIC(bp)) {
764                 for (i = 1; i < bp->nr_vnics; i++) {
765                         rc = bnxt_setup_one_vnic(bp, i);
766                         if (rc)
767                                 goto err_out;
768                 }
769         }
770
771         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, &bp->vnic_info[0], 0, NULL);
772         if (rc) {
773                 PMD_DRV_LOG(ERR,
774                         "HWRM cfa l2 rx mask failure rc: %x\n", rc);
775                 goto err_out;
776         }
777
778         /* check and configure queue intr-vector mapping */
779         if ((rte_intr_cap_multiple(intr_handle) ||
780              !RTE_ETH_DEV_SRIOV(bp->eth_dev).active) &&
781             bp->eth_dev->data->dev_conf.intr_conf.rxq != 0) {
782                 intr_vector = bp->eth_dev->data->nb_rx_queues;
783                 PMD_DRV_LOG(DEBUG, "intr_vector = %d\n", intr_vector);
784                 if (intr_vector > bp->rx_cp_nr_rings) {
785                         PMD_DRV_LOG(ERR, "At most %d intr queues supported",
786                                         bp->rx_cp_nr_rings);
787                         return -ENOTSUP;
788                 }
789                 rc = rte_intr_efd_enable(intr_handle, intr_vector);
790                 if (rc)
791                         return rc;
792         }
793
794         if (rte_intr_dp_is_en(intr_handle) && !intr_handle->intr_vec) {
795                 intr_handle->intr_vec =
796                         rte_zmalloc("intr_vec",
797                                     bp->eth_dev->data->nb_rx_queues *
798                                     sizeof(int), 0);
799                 if (intr_handle->intr_vec == NULL) {
800                         PMD_DRV_LOG(ERR, "Failed to allocate %d rx_queues"
801                                 " intr_vec", bp->eth_dev->data->nb_rx_queues);
802                         rc = -ENOMEM;
803                         goto err_out;
804                 }
805                 PMD_DRV_LOG(DEBUG, "intr_handle->intr_vec = %p "
806                         "intr_handle->nb_efd = %d intr_handle->max_intr = %d\n",
807                          intr_handle->intr_vec, intr_handle->nb_efd,
808                         intr_handle->max_intr);
809                 for (queue_id = 0; queue_id < bp->eth_dev->data->nb_rx_queues;
810                      queue_id++) {
811                         intr_handle->intr_vec[queue_id] =
812                                                         vec + BNXT_RX_VEC_START;
813                         if (vec < base + intr_handle->nb_efd - 1)
814                                 vec++;
815                 }
816         }
817
818         /* enable uio/vfio intr/eventfd mapping */
819         rc = rte_intr_enable(intr_handle);
820 #ifndef RTE_EXEC_ENV_FREEBSD
821         /* In FreeBSD OS, nic_uio driver does not support interrupts */
822         if (rc)
823                 goto err_out;
824 #endif
825
826         rc = bnxt_update_phy_setting(bp);
827         if (rc)
828                 goto err_out;
829
830         bp->mark_table = rte_zmalloc("bnxt_mark_table", BNXT_MARK_TABLE_SZ, 0);
831         if (!bp->mark_table)
832                 PMD_DRV_LOG(ERR, "Allocation of mark table failed\n");
833
834         return 0;
835
836 err_out:
837         /* Some of the error status returned by FW may not be from errno.h */
838         if (rc > 0)
839                 rc = -EIO;
840
841         return rc;
842 }
843
844 static int bnxt_shutdown_nic(struct bnxt *bp)
845 {
846         bnxt_free_all_hwrm_resources(bp);
847         bnxt_free_all_filters(bp);
848         bnxt_free_all_vnics(bp);
849         return 0;
850 }
851
852 /*
853  * Device configuration and status function
854  */
855
856 uint32_t bnxt_get_speed_capabilities(struct bnxt *bp)
857 {
858         uint32_t link_speed = 0;
859         uint32_t speed_capa = 0;
860
861         if (bp->link_info == NULL)
862                 return 0;
863
864         link_speed = bp->link_info->support_speeds;
865
866         /* If PAM4 is configured, use PAM4 supported speed */
867         if (link_speed == 0 && bp->link_info->support_pam4_speeds > 0)
868                 link_speed = bp->link_info->support_pam4_speeds;
869
870         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB)
871                 speed_capa |= ETH_LINK_SPEED_100M;
872         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100MBHD)
873                 speed_capa |= ETH_LINK_SPEED_100M_HD;
874         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_1GB)
875                 speed_capa |= ETH_LINK_SPEED_1G;
876         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_2_5GB)
877                 speed_capa |= ETH_LINK_SPEED_2_5G;
878         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
879                 speed_capa |= ETH_LINK_SPEED_10G;
880         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_20GB)
881                 speed_capa |= ETH_LINK_SPEED_20G;
882         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_25GB)
883                 speed_capa |= ETH_LINK_SPEED_25G;
884         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
885                 speed_capa |= ETH_LINK_SPEED_40G;
886         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_50GB)
887                 speed_capa |= ETH_LINK_SPEED_50G;
888         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
889                 speed_capa |= ETH_LINK_SPEED_100G;
890         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_PAM4_SPEEDS_50G)
891                 speed_capa |= ETH_LINK_SPEED_50G;
892         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_PAM4_SPEEDS_100G)
893                 speed_capa |= ETH_LINK_SPEED_100G;
894         if (link_speed & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_PAM4_SPEEDS_200G)
895                 speed_capa |= ETH_LINK_SPEED_200G;
896
897         if (bp->link_info->auto_mode ==
898             HWRM_PORT_PHY_QCFG_OUTPUT_AUTO_MODE_NONE)
899                 speed_capa |= ETH_LINK_SPEED_FIXED;
900
901         return speed_capa;
902 }
903
904 static int bnxt_dev_info_get_op(struct rte_eth_dev *eth_dev,
905                                 struct rte_eth_dev_info *dev_info)
906 {
907         struct rte_pci_device *pdev = RTE_DEV_TO_PCI(eth_dev->device);
908         struct bnxt *bp = eth_dev->data->dev_private;
909         uint16_t max_vnics, i, j, vpool, vrxq;
910         unsigned int max_rx_rings;
911         int rc;
912
913         rc = is_bnxt_in_error(bp);
914         if (rc)
915                 return rc;
916
917         /* MAC Specifics */
918         dev_info->max_mac_addrs = bp->max_l2_ctx;
919         dev_info->max_hash_mac_addrs = 0;
920
921         /* PF/VF specifics */
922         if (BNXT_PF(bp))
923                 dev_info->max_vfs = pdev->max_vfs;
924
925         max_rx_rings = bnxt_max_rings(bp);
926         /* For the sake of symmetry, max_rx_queues = max_tx_queues */
927         dev_info->max_rx_queues = max_rx_rings;
928         dev_info->max_tx_queues = max_rx_rings;
929         dev_info->reta_size = bnxt_rss_hash_tbl_size(bp);
930         dev_info->hash_key_size = 40;
931         max_vnics = bp->max_vnics;
932
933         /* MTU specifics */
934         dev_info->min_mtu = RTE_ETHER_MIN_MTU;
935         dev_info->max_mtu = BNXT_MAX_MTU;
936
937         /* Fast path specifics */
938         dev_info->min_rx_bufsize = 1;
939         dev_info->max_rx_pktlen = BNXT_MAX_PKT_LEN;
940
941         dev_info->rx_offload_capa = BNXT_DEV_RX_OFFLOAD_SUPPORT;
942         if (bp->flags & BNXT_FLAG_PTP_SUPPORTED)
943                 dev_info->rx_offload_capa |= DEV_RX_OFFLOAD_TIMESTAMP;
944         dev_info->tx_queue_offload_capa = DEV_TX_OFFLOAD_MBUF_FAST_FREE;
945         dev_info->tx_offload_capa = BNXT_DEV_TX_OFFLOAD_SUPPORT |
946                                     dev_info->tx_queue_offload_capa;
947         dev_info->flow_type_rss_offloads = BNXT_ETH_RSS_SUPPORT;
948
949         dev_info->speed_capa = bnxt_get_speed_capabilities(bp);
950
951         /* *INDENT-OFF* */
952         dev_info->default_rxconf = (struct rte_eth_rxconf) {
953                 .rx_thresh = {
954                         .pthresh = 8,
955                         .hthresh = 8,
956                         .wthresh = 0,
957                 },
958                 .rx_free_thresh = 32,
959                 .rx_drop_en = BNXT_DEFAULT_RX_DROP_EN,
960         };
961
962         dev_info->default_txconf = (struct rte_eth_txconf) {
963                 .tx_thresh = {
964                         .pthresh = 32,
965                         .hthresh = 0,
966                         .wthresh = 0,
967                 },
968                 .tx_free_thresh = 32,
969                 .tx_rs_thresh = 32,
970         };
971         eth_dev->data->dev_conf.intr_conf.lsc = 1;
972
973         eth_dev->data->dev_conf.intr_conf.rxq = 1;
974         dev_info->rx_desc_lim.nb_min = BNXT_MIN_RING_DESC;
975         dev_info->rx_desc_lim.nb_max = BNXT_MAX_RX_RING_DESC;
976         dev_info->tx_desc_lim.nb_min = BNXT_MIN_RING_DESC;
977         dev_info->tx_desc_lim.nb_max = BNXT_MAX_TX_RING_DESC;
978
979         if (BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp)) {
980                 dev_info->switch_info.name = eth_dev->device->name;
981                 dev_info->switch_info.domain_id = bp->switch_domain_id;
982                 dev_info->switch_info.port_id =
983                                 BNXT_PF(bp) ? BNXT_SWITCH_PORT_ID_PF :
984                                     BNXT_SWITCH_PORT_ID_TRUSTED_VF;
985         }
986
987         /* *INDENT-ON* */
988
989         /*
990          * TODO: default_rxconf, default_txconf, rx_desc_lim, and tx_desc_lim
991          *       need further investigation.
992          */
993
994         /* VMDq resources */
995         vpool = 64; /* ETH_64_POOLS */
996         vrxq = 128; /* ETH_VMDQ_DCB_NUM_QUEUES */
997         for (i = 0; i < 4; vpool >>= 1, i++) {
998                 if (max_vnics > vpool) {
999                         for (j = 0; j < 5; vrxq >>= 1, j++) {
1000                                 if (dev_info->max_rx_queues > vrxq) {
1001                                         if (vpool > vrxq)
1002                                                 vpool = vrxq;
1003                                         goto found;
1004                                 }
1005                         }
1006                         /* Not enough resources to support VMDq */
1007                         break;
1008                 }
1009         }
1010         /* Not enough resources to support VMDq */
1011         vpool = 0;
1012         vrxq = 0;
1013 found:
1014         dev_info->max_vmdq_pools = vpool;
1015         dev_info->vmdq_queue_num = vrxq;
1016
1017         dev_info->vmdq_pool_base = 0;
1018         dev_info->vmdq_queue_base = 0;
1019
1020         return 0;
1021 }
1022
1023 /* Configure the device based on the configuration provided */
1024 static int bnxt_dev_configure_op(struct rte_eth_dev *eth_dev)
1025 {
1026         struct bnxt *bp = eth_dev->data->dev_private;
1027         uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
1028         int rc;
1029
1030         bp->rx_queues = (void *)eth_dev->data->rx_queues;
1031         bp->tx_queues = (void *)eth_dev->data->tx_queues;
1032         bp->tx_nr_rings = eth_dev->data->nb_tx_queues;
1033         bp->rx_nr_rings = eth_dev->data->nb_rx_queues;
1034
1035         rc = is_bnxt_in_error(bp);
1036         if (rc)
1037                 return rc;
1038
1039         if (BNXT_VF(bp) && (bp->flags & BNXT_FLAG_NEW_RM)) {
1040                 rc = bnxt_hwrm_check_vf_rings(bp);
1041                 if (rc) {
1042                         PMD_DRV_LOG(ERR, "HWRM insufficient resources\n");
1043                         return -ENOSPC;
1044                 }
1045
1046                 /* If a resource has already been allocated - in this case
1047                  * it is the async completion ring, free it. Reallocate it after
1048                  * resource reservation. This will ensure the resource counts
1049                  * are calculated correctly.
1050                  */
1051
1052                 pthread_mutex_lock(&bp->def_cp_lock);
1053
1054                 if (!BNXT_HAS_NQ(bp) && bp->async_cp_ring) {
1055                         bnxt_disable_int(bp);
1056                         bnxt_free_cp_ring(bp, bp->async_cp_ring);
1057                 }
1058
1059                 rc = bnxt_hwrm_func_reserve_vf_resc(bp, false);
1060                 if (rc) {
1061                         PMD_DRV_LOG(ERR, "HWRM resource alloc fail:%x\n", rc);
1062                         pthread_mutex_unlock(&bp->def_cp_lock);
1063                         return -ENOSPC;
1064                 }
1065
1066                 if (!BNXT_HAS_NQ(bp) && bp->async_cp_ring) {
1067                         rc = bnxt_alloc_async_cp_ring(bp);
1068                         if (rc) {
1069                                 pthread_mutex_unlock(&bp->def_cp_lock);
1070                                 return rc;
1071                         }
1072                         bnxt_enable_int(bp);
1073                 }
1074
1075                 pthread_mutex_unlock(&bp->def_cp_lock);
1076         }
1077
1078         /* Inherit new configurations */
1079         if (eth_dev->data->nb_rx_queues > bp->max_rx_rings ||
1080             eth_dev->data->nb_tx_queues > bp->max_tx_rings ||
1081             eth_dev->data->nb_rx_queues + eth_dev->data->nb_tx_queues
1082                 + BNXT_NUM_ASYNC_CPR(bp) > bp->max_cp_rings ||
1083             eth_dev->data->nb_rx_queues + eth_dev->data->nb_tx_queues >
1084             bp->max_stat_ctx)
1085                 goto resource_error;
1086
1087         if (BNXT_HAS_RING_GRPS(bp) &&
1088             (uint32_t)(eth_dev->data->nb_rx_queues) > bp->max_ring_grps)
1089                 goto resource_error;
1090
1091         if (!(eth_dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS) &&
1092             bp->max_vnics < eth_dev->data->nb_rx_queues)
1093                 goto resource_error;
1094
1095         bp->rx_cp_nr_rings = bp->rx_nr_rings;
1096         bp->tx_cp_nr_rings = bp->tx_nr_rings;
1097
1098         if (eth_dev->data->dev_conf.rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG)
1099                 rx_offloads |= DEV_RX_OFFLOAD_RSS_HASH;
1100         eth_dev->data->dev_conf.rxmode.offloads = rx_offloads;
1101
1102         if (rx_offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
1103                 eth_dev->data->mtu =
1104                         eth_dev->data->dev_conf.rxmode.max_rx_pkt_len -
1105                         RTE_ETHER_HDR_LEN - RTE_ETHER_CRC_LEN - VLAN_TAG_SIZE *
1106                         BNXT_NUM_VLANS;
1107                 bnxt_mtu_set_op(eth_dev, eth_dev->data->mtu);
1108         }
1109         return 0;
1110
1111 resource_error:
1112         PMD_DRV_LOG(ERR,
1113                     "Insufficient resources to support requested config\n");
1114         PMD_DRV_LOG(ERR,
1115                     "Num Queues Requested: Tx %d, Rx %d\n",
1116                     eth_dev->data->nb_tx_queues,
1117                     eth_dev->data->nb_rx_queues);
1118         PMD_DRV_LOG(ERR,
1119                     "MAX: TxQ %d, RxQ %d, CQ %d Stat %d, Grp %d, Vnic %d\n",
1120                     bp->max_tx_rings, bp->max_rx_rings, bp->max_cp_rings,
1121                     bp->max_stat_ctx, bp->max_ring_grps, bp->max_vnics);
1122         return -ENOSPC;
1123 }
1124
1125 void bnxt_print_link_info(struct rte_eth_dev *eth_dev)
1126 {
1127         struct rte_eth_link *link = &eth_dev->data->dev_link;
1128
1129         if (link->link_status)
1130                 PMD_DRV_LOG(INFO, "Port %d Link Up - speed %u Mbps - %s\n",
1131                         eth_dev->data->port_id,
1132                         (uint32_t)link->link_speed,
1133                         (link->link_duplex == ETH_LINK_FULL_DUPLEX) ?
1134                         ("full-duplex") : ("half-duplex\n"));
1135         else
1136                 PMD_DRV_LOG(INFO, "Port %d Link Down\n",
1137                         eth_dev->data->port_id);
1138 }
1139
1140 /*
1141  * Determine whether the current configuration requires support for scattered
1142  * receive; return 1 if scattered receive is required and 0 if not.
1143  */
1144 static int bnxt_scattered_rx(struct rte_eth_dev *eth_dev)
1145 {
1146         uint16_t buf_size;
1147         int i;
1148
1149         if (eth_dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_SCATTER)
1150                 return 1;
1151
1152         if (eth_dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_TCP_LRO)
1153                 return 1;
1154
1155         for (i = 0; i < eth_dev->data->nb_rx_queues; i++) {
1156                 struct bnxt_rx_queue *rxq = eth_dev->data->rx_queues[i];
1157
1158                 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rxq->mb_pool) -
1159                                       RTE_PKTMBUF_HEADROOM);
1160                 if (eth_dev->data->dev_conf.rxmode.max_rx_pkt_len > buf_size)
1161                         return 1;
1162         }
1163         return 0;
1164 }
1165
1166 static eth_rx_burst_t
1167 bnxt_receive_function(struct rte_eth_dev *eth_dev)
1168 {
1169         struct bnxt *bp = eth_dev->data->dev_private;
1170
1171         /* Disable vector mode RX for Stingray2 for now */
1172         if (BNXT_CHIP_SR2(bp)) {
1173                 bp->flags &= ~BNXT_FLAG_RX_VECTOR_PKT_MODE;
1174                 return bnxt_recv_pkts;
1175         }
1176
1177 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
1178 #ifndef RTE_LIBRTE_IEEE1588
1179         /*
1180          * Vector mode receive can be enabled only if scatter rx is not
1181          * in use and rx offloads are limited to VLAN stripping and
1182          * CRC stripping.
1183          */
1184         if (!eth_dev->data->scattered_rx &&
1185             !(eth_dev->data->dev_conf.rxmode.offloads &
1186               ~(DEV_RX_OFFLOAD_VLAN_STRIP |
1187                 DEV_RX_OFFLOAD_KEEP_CRC |
1188                 DEV_RX_OFFLOAD_JUMBO_FRAME |
1189                 DEV_RX_OFFLOAD_IPV4_CKSUM |
1190                 DEV_RX_OFFLOAD_UDP_CKSUM |
1191                 DEV_RX_OFFLOAD_TCP_CKSUM |
1192                 DEV_RX_OFFLOAD_OUTER_IPV4_CKSUM |
1193                 DEV_RX_OFFLOAD_OUTER_UDP_CKSUM |
1194                 DEV_RX_OFFLOAD_RSS_HASH |
1195                 DEV_RX_OFFLOAD_VLAN_FILTER)) &&
1196             !BNXT_TRUFLOW_EN(bp) && BNXT_NUM_ASYNC_CPR(bp) &&
1197             rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128) {
1198                 PMD_DRV_LOG(INFO, "Using vector mode receive for port %d\n",
1199                             eth_dev->data->port_id);
1200                 bp->flags |= BNXT_FLAG_RX_VECTOR_PKT_MODE;
1201                 return bnxt_recv_pkts_vec;
1202         }
1203         PMD_DRV_LOG(INFO, "Vector mode receive disabled for port %d\n",
1204                     eth_dev->data->port_id);
1205         PMD_DRV_LOG(INFO,
1206                     "Port %d scatter: %d rx offload: %" PRIX64 "\n",
1207                     eth_dev->data->port_id,
1208                     eth_dev->data->scattered_rx,
1209                     eth_dev->data->dev_conf.rxmode.offloads);
1210 #endif
1211 #endif
1212         bp->flags &= ~BNXT_FLAG_RX_VECTOR_PKT_MODE;
1213         return bnxt_recv_pkts;
1214 }
1215
1216 static eth_tx_burst_t
1217 bnxt_transmit_function(struct rte_eth_dev *eth_dev)
1218 {
1219         struct bnxt *bp = eth_dev->data->dev_private;
1220
1221         /* Disable vector mode TX for Stingray2 for now */
1222         if (BNXT_CHIP_SR2(bp))
1223                 return bnxt_xmit_pkts;
1224
1225 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
1226 #ifndef RTE_LIBRTE_IEEE1588
1227         uint64_t offloads = eth_dev->data->dev_conf.txmode.offloads;
1228
1229         /*
1230          * Vector mode transmit can be enabled only if not using scatter rx
1231          * or tx offloads.
1232          */
1233         if (!eth_dev->data->scattered_rx &&
1234             !(offloads & ~DEV_TX_OFFLOAD_MBUF_FAST_FREE) &&
1235             !BNXT_TRUFLOW_EN(bp) &&
1236             rte_vect_get_max_simd_bitwidth() >= RTE_VECT_SIMD_128) {
1237                 PMD_DRV_LOG(INFO, "Using vector mode transmit for port %d\n",
1238                             eth_dev->data->port_id);
1239                 return bnxt_xmit_pkts_vec;
1240         }
1241         PMD_DRV_LOG(INFO, "Vector mode transmit disabled for port %d\n",
1242                     eth_dev->data->port_id);
1243         PMD_DRV_LOG(INFO,
1244                     "Port %d scatter: %d tx offload: %" PRIX64 "\n",
1245                     eth_dev->data->port_id,
1246                     eth_dev->data->scattered_rx,
1247                     offloads);
1248 #endif
1249 #endif
1250         return bnxt_xmit_pkts;
1251 }
1252
1253 static int bnxt_handle_if_change_status(struct bnxt *bp)
1254 {
1255         int rc;
1256
1257         /* Since fw has undergone a reset and lost all contexts,
1258          * set fatal flag to not issue hwrm during cleanup
1259          */
1260         bp->flags |= BNXT_FLAG_FATAL_ERROR;
1261         bnxt_uninit_resources(bp, true);
1262
1263         /* clear fatal flag so that re-init happens */
1264         bp->flags &= ~BNXT_FLAG_FATAL_ERROR;
1265         rc = bnxt_init_resources(bp, true);
1266
1267         bp->flags &= ~BNXT_FLAG_IF_CHANGE_HOT_FW_RESET_DONE;
1268
1269         return rc;
1270 }
1271
1272 static int bnxt_dev_set_link_up_op(struct rte_eth_dev *eth_dev)
1273 {
1274         struct bnxt *bp = eth_dev->data->dev_private;
1275         int rc = 0;
1276
1277         if (!BNXT_SINGLE_PF(bp))
1278                 return -ENOTSUP;
1279
1280         if (!bp->link_info->link_up)
1281                 rc = bnxt_set_hwrm_link_config(bp, true);
1282         if (!rc)
1283                 eth_dev->data->dev_link.link_status = 1;
1284
1285         bnxt_print_link_info(eth_dev);
1286         return rc;
1287 }
1288
1289 static int bnxt_dev_set_link_down_op(struct rte_eth_dev *eth_dev)
1290 {
1291         struct bnxt *bp = eth_dev->data->dev_private;
1292
1293         if (!BNXT_SINGLE_PF(bp))
1294                 return -ENOTSUP;
1295
1296         eth_dev->data->dev_link.link_status = 0;
1297         bnxt_set_hwrm_link_config(bp, false);
1298         bp->link_info->link_up = 0;
1299
1300         return 0;
1301 }
1302
1303 static void bnxt_free_switch_domain(struct bnxt *bp)
1304 {
1305         int rc = 0;
1306
1307         if (!(BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp)))
1308                 return;
1309
1310         rc = rte_eth_switch_domain_free(bp->switch_domain_id);
1311         if (rc)
1312                 PMD_DRV_LOG(ERR, "free switch domain:%d fail: %d\n",
1313                             bp->switch_domain_id, rc);
1314 }
1315
1316 static void bnxt_ptp_get_current_time(void *arg)
1317 {
1318         struct bnxt *bp = arg;
1319         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1320         int rc;
1321
1322         rc = is_bnxt_in_error(bp);
1323         if (rc)
1324                 return;
1325
1326         if (!ptp)
1327                 return;
1328
1329         bnxt_hwrm_port_ts_query(bp, BNXT_PTP_FLAGS_CURRENT_TIME,
1330                                 &ptp->current_time);
1331
1332         rc = rte_eal_alarm_set(US_PER_S, bnxt_ptp_get_current_time, (void *)bp);
1333         if (rc != 0) {
1334                 PMD_DRV_LOG(ERR, "Failed to re-schedule PTP alarm\n");
1335                 bp->flags2 &= ~BNXT_FLAGS2_PTP_ALARM_SCHEDULED;
1336         }
1337 }
1338
1339 static int bnxt_schedule_ptp_alarm(struct bnxt *bp)
1340 {
1341         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
1342         int rc;
1343
1344         if (bp->flags2 & BNXT_FLAGS2_PTP_ALARM_SCHEDULED)
1345                 return 0;
1346
1347         bnxt_hwrm_port_ts_query(bp, BNXT_PTP_FLAGS_CURRENT_TIME,
1348                                 &ptp->current_time);
1349
1350         rc = rte_eal_alarm_set(US_PER_S, bnxt_ptp_get_current_time, (void *)bp);
1351         return rc;
1352 }
1353
1354 static void bnxt_cancel_ptp_alarm(struct bnxt *bp)
1355 {
1356         if (bp->flags2 & BNXT_FLAGS2_PTP_ALARM_SCHEDULED) {
1357                 rte_eal_alarm_cancel(bnxt_ptp_get_current_time, (void *)bp);
1358                 bp->flags2 &= ~BNXT_FLAGS2_PTP_ALARM_SCHEDULED;
1359         }
1360 }
1361
1362 static void bnxt_ptp_stop(struct bnxt *bp)
1363 {
1364         bnxt_cancel_ptp_alarm(bp);
1365         bp->flags2 &= ~BNXT_FLAGS2_PTP_TIMESYNC_ENABLED;
1366 }
1367
1368 static int bnxt_ptp_start(struct bnxt *bp)
1369 {
1370         int rc;
1371
1372         rc = bnxt_schedule_ptp_alarm(bp);
1373         if (rc != 0) {
1374                 PMD_DRV_LOG(ERR, "Failed to schedule PTP alarm\n");
1375         } else {
1376                 bp->flags2 |= BNXT_FLAGS2_PTP_TIMESYNC_ENABLED;
1377                 bp->flags2 |= BNXT_FLAGS2_PTP_ALARM_SCHEDULED;
1378         }
1379
1380         return rc;
1381 }
1382
1383 static int bnxt_dev_stop(struct rte_eth_dev *eth_dev)
1384 {
1385         struct bnxt *bp = eth_dev->data->dev_private;
1386         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
1387         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1388         struct rte_eth_link link;
1389         int ret;
1390
1391         eth_dev->data->dev_started = 0;
1392         eth_dev->data->scattered_rx = 0;
1393
1394         /* Prevent crashes when queues are still in use */
1395         eth_dev->rx_pkt_burst = &bnxt_dummy_recv_pkts;
1396         eth_dev->tx_pkt_burst = &bnxt_dummy_xmit_pkts;
1397
1398         bnxt_disable_int(bp);
1399
1400         /* disable uio/vfio intr/eventfd mapping */
1401         rte_intr_disable(intr_handle);
1402
1403         /* Stop the child representors for this device */
1404         ret = bnxt_rep_stop_all(bp);
1405         if (ret != 0)
1406                 return ret;
1407
1408         /* delete the bnxt ULP port details */
1409         bnxt_ulp_port_deinit(bp);
1410
1411         bnxt_cancel_fw_health_check(bp);
1412
1413         if (BNXT_P5_PTP_TIMESYNC_ENABLED(bp))
1414                 bnxt_cancel_ptp_alarm(bp);
1415
1416         /* Do not bring link down during reset recovery */
1417         if (!is_bnxt_in_error(bp)) {
1418                 bnxt_dev_set_link_down_op(eth_dev);
1419                 /* Wait for link to be reset */
1420                 if (BNXT_SINGLE_PF(bp))
1421                         rte_delay_ms(500);
1422                 /* clear the recorded link status */
1423                 memset(&link, 0, sizeof(link));
1424                 rte_eth_linkstatus_set(eth_dev, &link);
1425         }
1426
1427         /* Clean queue intr-vector mapping */
1428         rte_intr_efd_disable(intr_handle);
1429         if (intr_handle->intr_vec != NULL) {
1430                 rte_free(intr_handle->intr_vec);
1431                 intr_handle->intr_vec = NULL;
1432         }
1433
1434         bnxt_hwrm_port_clr_stats(bp);
1435         bnxt_free_tx_mbufs(bp);
1436         bnxt_free_rx_mbufs(bp);
1437         /* Process any remaining notifications in default completion queue */
1438         bnxt_int_handler(eth_dev);
1439         bnxt_shutdown_nic(bp);
1440         bnxt_hwrm_if_change(bp, false);
1441
1442         rte_free(bp->mark_table);
1443         bp->mark_table = NULL;
1444
1445         bp->flags &= ~BNXT_FLAG_RX_VECTOR_PKT_MODE;
1446         bp->rx_cosq_cnt = 0;
1447         /* All filters are deleted on a port stop. */
1448         if (BNXT_FLOW_XSTATS_EN(bp))
1449                 bp->flow_stat->flow_count = 0;
1450
1451         return 0;
1452 }
1453
1454 /* Unload the driver, release resources */
1455 static int bnxt_dev_stop_op(struct rte_eth_dev *eth_dev)
1456 {
1457         struct bnxt *bp = eth_dev->data->dev_private;
1458
1459         pthread_mutex_lock(&bp->err_recovery_lock);
1460         if (bp->flags & BNXT_FLAG_FW_RESET) {
1461                 PMD_DRV_LOG(ERR,
1462                             "Adapter recovering from error..Please retry\n");
1463                 pthread_mutex_unlock(&bp->err_recovery_lock);
1464                 return -EAGAIN;
1465         }
1466         pthread_mutex_unlock(&bp->err_recovery_lock);
1467
1468         return bnxt_dev_stop(eth_dev);
1469 }
1470
1471 static int bnxt_dev_start_op(struct rte_eth_dev *eth_dev)
1472 {
1473         struct bnxt *bp = eth_dev->data->dev_private;
1474         uint64_t rx_offloads = eth_dev->data->dev_conf.rxmode.offloads;
1475         int vlan_mask = 0;
1476         int rc, retry_cnt = BNXT_IF_CHANGE_RETRY_COUNT;
1477
1478         if (!eth_dev->data->nb_tx_queues || !eth_dev->data->nb_rx_queues) {
1479                 PMD_DRV_LOG(ERR, "Queues are not configured yet!\n");
1480                 return -EINVAL;
1481         }
1482
1483         if (bp->rx_cp_nr_rings > RTE_ETHDEV_QUEUE_STAT_CNTRS)
1484                 PMD_DRV_LOG(ERR,
1485                             "RxQ cnt %d > RTE_ETHDEV_QUEUE_STAT_CNTRS %d\n",
1486                             bp->rx_cp_nr_rings, RTE_ETHDEV_QUEUE_STAT_CNTRS);
1487
1488         do {
1489                 rc = bnxt_hwrm_if_change(bp, true);
1490                 if (rc == 0 || rc != -EAGAIN)
1491                         break;
1492
1493                 rte_delay_ms(BNXT_IF_CHANGE_RETRY_INTERVAL);
1494         } while (retry_cnt--);
1495
1496         if (rc)
1497                 return rc;
1498
1499         if (bp->flags & BNXT_FLAG_IF_CHANGE_HOT_FW_RESET_DONE) {
1500                 rc = bnxt_handle_if_change_status(bp);
1501                 if (rc)
1502                         return rc;
1503         }
1504
1505         bnxt_enable_int(bp);
1506
1507         eth_dev->data->scattered_rx = bnxt_scattered_rx(eth_dev);
1508
1509         rc = bnxt_start_nic(bp);
1510         if (rc)
1511                 goto error;
1512
1513         eth_dev->data->dev_started = 1;
1514
1515         bnxt_link_update_op(eth_dev, 1);
1516
1517         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)
1518                 vlan_mask |= ETH_VLAN_FILTER_MASK;
1519         if (rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
1520                 vlan_mask |= ETH_VLAN_STRIP_MASK;
1521         rc = bnxt_vlan_offload_set_op(eth_dev, vlan_mask);
1522         if (rc)
1523                 goto error;
1524
1525         /* Initialize bnxt ULP port details */
1526         rc = bnxt_ulp_port_init(bp);
1527         if (rc)
1528                 goto error;
1529
1530         eth_dev->rx_pkt_burst = bnxt_receive_function(eth_dev);
1531         eth_dev->tx_pkt_burst = bnxt_transmit_function(eth_dev);
1532
1533         bnxt_schedule_fw_health_check(bp);
1534
1535         if (BNXT_P5_PTP_TIMESYNC_ENABLED(bp))
1536                 bnxt_schedule_ptp_alarm(bp);
1537
1538         return 0;
1539
1540 error:
1541         bnxt_dev_stop(eth_dev);
1542         return rc;
1543 }
1544
1545 static void
1546 bnxt_uninit_locks(struct bnxt *bp)
1547 {
1548         pthread_mutex_destroy(&bp->flow_lock);
1549         pthread_mutex_destroy(&bp->def_cp_lock);
1550         pthread_mutex_destroy(&bp->health_check_lock);
1551         pthread_mutex_destroy(&bp->err_recovery_lock);
1552         if (bp->rep_info) {
1553                 pthread_mutex_destroy(&bp->rep_info->vfr_lock);
1554                 pthread_mutex_destroy(&bp->rep_info->vfr_start_lock);
1555         }
1556 }
1557
1558 static void bnxt_drv_uninit(struct bnxt *bp)
1559 {
1560         bnxt_free_leds_info(bp);
1561         bnxt_free_cos_queues(bp);
1562         bnxt_free_link_info(bp);
1563         bnxt_free_parent_info(bp);
1564         bnxt_uninit_locks(bp);
1565
1566         rte_memzone_free((const struct rte_memzone *)bp->tx_mem_zone);
1567         bp->tx_mem_zone = NULL;
1568         rte_memzone_free((const struct rte_memzone *)bp->rx_mem_zone);
1569         bp->rx_mem_zone = NULL;
1570
1571         bnxt_free_vf_info(bp);
1572         bnxt_free_pf_info(bp);
1573
1574         rte_free(bp->grp_info);
1575         bp->grp_info = NULL;
1576 }
1577
1578 static int bnxt_dev_close_op(struct rte_eth_dev *eth_dev)
1579 {
1580         struct bnxt *bp = eth_dev->data->dev_private;
1581         int ret = 0;
1582
1583         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
1584                 return 0;
1585
1586         pthread_mutex_lock(&bp->err_recovery_lock);
1587         if (bp->flags & BNXT_FLAG_FW_RESET) {
1588                 PMD_DRV_LOG(ERR,
1589                             "Adapter recovering from error...Please retry\n");
1590                 pthread_mutex_unlock(&bp->err_recovery_lock);
1591                 return -EAGAIN;
1592         }
1593         pthread_mutex_unlock(&bp->err_recovery_lock);
1594
1595         /* cancel the recovery handler before remove dev */
1596         rte_eal_alarm_cancel(bnxt_dev_reset_and_resume, (void *)bp);
1597         rte_eal_alarm_cancel(bnxt_dev_recover, (void *)bp);
1598         bnxt_cancel_fc_thread(bp);
1599
1600         if (eth_dev->data->dev_started)
1601                 ret = bnxt_dev_stop(eth_dev);
1602
1603         bnxt_uninit_resources(bp, false);
1604
1605         bnxt_drv_uninit(bp);
1606
1607         return ret;
1608 }
1609
1610 static void bnxt_mac_addr_remove_op(struct rte_eth_dev *eth_dev,
1611                                     uint32_t index)
1612 {
1613         struct bnxt *bp = eth_dev->data->dev_private;
1614         uint64_t pool_mask = eth_dev->data->mac_pool_sel[index];
1615         struct bnxt_vnic_info *vnic;
1616         struct bnxt_filter_info *filter, *temp_filter;
1617         uint32_t i;
1618
1619         if (is_bnxt_in_error(bp))
1620                 return;
1621
1622         /*
1623          * Loop through all VNICs from the specified filter flow pools to
1624          * remove the corresponding MAC addr filter
1625          */
1626         for (i = 0; i < bp->nr_vnics; i++) {
1627                 if (!(pool_mask & (1ULL << i)))
1628                         continue;
1629
1630                 vnic = &bp->vnic_info[i];
1631                 filter = STAILQ_FIRST(&vnic->filter);
1632                 while (filter) {
1633                         temp_filter = STAILQ_NEXT(filter, next);
1634                         if (filter->mac_index == index) {
1635                                 STAILQ_REMOVE(&vnic->filter, filter,
1636                                                 bnxt_filter_info, next);
1637                                 bnxt_hwrm_clear_l2_filter(bp, filter);
1638                                 bnxt_free_filter(bp, filter);
1639                         }
1640                         filter = temp_filter;
1641                 }
1642         }
1643 }
1644
1645 static int bnxt_add_mac_filter(struct bnxt *bp, struct bnxt_vnic_info *vnic,
1646                                struct rte_ether_addr *mac_addr, uint32_t index,
1647                                uint32_t pool)
1648 {
1649         struct bnxt_filter_info *filter;
1650         int rc = 0;
1651
1652         /* Attach requested MAC address to the new l2_filter */
1653         STAILQ_FOREACH(filter, &vnic->filter, next) {
1654                 if (filter->mac_index == index) {
1655                         PMD_DRV_LOG(DEBUG,
1656                                     "MAC addr already existed for pool %d\n",
1657                                     pool);
1658                         return 0;
1659                 }
1660         }
1661
1662         filter = bnxt_alloc_filter(bp);
1663         if (!filter) {
1664                 PMD_DRV_LOG(ERR, "L2 filter alloc failed\n");
1665                 return -ENODEV;
1666         }
1667
1668         /* bnxt_alloc_filter copies default MAC to filter->l2_addr. So,
1669          * if the MAC that's been programmed now is a different one, then,
1670          * copy that addr to filter->l2_addr
1671          */
1672         if (mac_addr)
1673                 memcpy(filter->l2_addr, mac_addr, RTE_ETHER_ADDR_LEN);
1674         filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
1675
1676         rc = bnxt_hwrm_set_l2_filter(bp, vnic->fw_vnic_id, filter);
1677         if (!rc) {
1678                 filter->mac_index = index;
1679                 if (filter->mac_index == 0)
1680                         STAILQ_INSERT_HEAD(&vnic->filter, filter, next);
1681                 else
1682                         STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
1683         } else {
1684                 bnxt_free_filter(bp, filter);
1685         }
1686
1687         return rc;
1688 }
1689
1690 static int bnxt_mac_addr_add_op(struct rte_eth_dev *eth_dev,
1691                                 struct rte_ether_addr *mac_addr,
1692                                 uint32_t index, uint32_t pool)
1693 {
1694         struct bnxt *bp = eth_dev->data->dev_private;
1695         struct bnxt_vnic_info *vnic = &bp->vnic_info[pool];
1696         int rc = 0;
1697
1698         rc = is_bnxt_in_error(bp);
1699         if (rc)
1700                 return rc;
1701
1702         if (BNXT_VF(bp) && !BNXT_VF_IS_TRUSTED(bp)) {
1703                 PMD_DRV_LOG(ERR, "Cannot add MAC address to a VF interface\n");
1704                 return -ENOTSUP;
1705         }
1706
1707         if (!vnic) {
1708                 PMD_DRV_LOG(ERR, "VNIC not found for pool %d!\n", pool);
1709                 return -EINVAL;
1710         }
1711
1712         /* Filter settings will get applied when port is started */
1713         if (!eth_dev->data->dev_started)
1714                 return 0;
1715
1716         rc = bnxt_add_mac_filter(bp, vnic, mac_addr, index, pool);
1717
1718         return rc;
1719 }
1720
1721 int bnxt_link_update_op(struct rte_eth_dev *eth_dev, int wait_to_complete)
1722 {
1723         int rc = 0;
1724         struct bnxt *bp = eth_dev->data->dev_private;
1725         struct rte_eth_link new;
1726         int cnt = wait_to_complete ? BNXT_MAX_LINK_WAIT_CNT :
1727                         BNXT_MIN_LINK_WAIT_CNT;
1728
1729         rc = is_bnxt_in_error(bp);
1730         if (rc)
1731                 return rc;
1732
1733         memset(&new, 0, sizeof(new));
1734
1735         if (bp->link_info == NULL)
1736                 goto out;
1737
1738         do {
1739                 /* Retrieve link info from hardware */
1740                 rc = bnxt_get_hwrm_link_config(bp, &new);
1741                 if (rc) {
1742                         new.link_speed = ETH_LINK_SPEED_100M;
1743                         new.link_duplex = ETH_LINK_FULL_DUPLEX;
1744                         PMD_DRV_LOG(ERR,
1745                                 "Failed to retrieve link rc = 0x%x!\n", rc);
1746                         goto out;
1747                 }
1748
1749                 if (!wait_to_complete || new.link_status)
1750                         break;
1751
1752                 rte_delay_ms(BNXT_LINK_WAIT_INTERVAL);
1753         } while (cnt--);
1754
1755         /* Only single function PF can bring phy down.
1756          * When port is stopped, report link down for VF/MH/NPAR functions.
1757          */
1758         if (!BNXT_SINGLE_PF(bp) && !eth_dev->data->dev_started)
1759                 memset(&new, 0, sizeof(new));
1760
1761 out:
1762         /* Timed out or success */
1763         if (new.link_status != eth_dev->data->dev_link.link_status ||
1764             new.link_speed != eth_dev->data->dev_link.link_speed) {
1765                 rte_eth_linkstatus_set(eth_dev, &new);
1766
1767                 rte_eth_dev_callback_process(eth_dev,
1768                                              RTE_ETH_EVENT_INTR_LSC,
1769                                              NULL);
1770
1771                 bnxt_print_link_info(eth_dev);
1772         }
1773
1774         return rc;
1775 }
1776
1777 static int bnxt_promiscuous_enable_op(struct rte_eth_dev *eth_dev)
1778 {
1779         struct bnxt *bp = eth_dev->data->dev_private;
1780         struct bnxt_vnic_info *vnic;
1781         uint32_t old_flags;
1782         int rc;
1783
1784         rc = is_bnxt_in_error(bp);
1785         if (rc)
1786                 return rc;
1787
1788         /* Filter settings will get applied when port is started */
1789         if (!eth_dev->data->dev_started)
1790                 return 0;
1791
1792         if (bp->vnic_info == NULL)
1793                 return 0;
1794
1795         vnic = BNXT_GET_DEFAULT_VNIC(bp);
1796
1797         old_flags = vnic->flags;
1798         vnic->flags |= BNXT_VNIC_INFO_PROMISC;
1799         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
1800         if (rc != 0)
1801                 vnic->flags = old_flags;
1802
1803         return rc;
1804 }
1805
1806 static int bnxt_promiscuous_disable_op(struct rte_eth_dev *eth_dev)
1807 {
1808         struct bnxt *bp = eth_dev->data->dev_private;
1809         struct bnxt_vnic_info *vnic;
1810         uint32_t old_flags;
1811         int rc;
1812
1813         rc = is_bnxt_in_error(bp);
1814         if (rc)
1815                 return rc;
1816
1817         /* Filter settings will get applied when port is started */
1818         if (!eth_dev->data->dev_started)
1819                 return 0;
1820
1821         if (bp->vnic_info == NULL)
1822                 return 0;
1823
1824         vnic = BNXT_GET_DEFAULT_VNIC(bp);
1825
1826         old_flags = vnic->flags;
1827         vnic->flags &= ~BNXT_VNIC_INFO_PROMISC;
1828         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
1829         if (rc != 0)
1830                 vnic->flags = old_flags;
1831
1832         return rc;
1833 }
1834
1835 static int bnxt_allmulticast_enable_op(struct rte_eth_dev *eth_dev)
1836 {
1837         struct bnxt *bp = eth_dev->data->dev_private;
1838         struct bnxt_vnic_info *vnic;
1839         uint32_t old_flags;
1840         int rc;
1841
1842         rc = is_bnxt_in_error(bp);
1843         if (rc)
1844                 return rc;
1845
1846         /* Filter settings will get applied when port is started */
1847         if (!eth_dev->data->dev_started)
1848                 return 0;
1849
1850         if (bp->vnic_info == NULL)
1851                 return 0;
1852
1853         vnic = BNXT_GET_DEFAULT_VNIC(bp);
1854
1855         old_flags = vnic->flags;
1856         vnic->flags |= BNXT_VNIC_INFO_ALLMULTI;
1857         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
1858         if (rc != 0)
1859                 vnic->flags = old_flags;
1860
1861         return rc;
1862 }
1863
1864 static int bnxt_allmulticast_disable_op(struct rte_eth_dev *eth_dev)
1865 {
1866         struct bnxt *bp = eth_dev->data->dev_private;
1867         struct bnxt_vnic_info *vnic;
1868         uint32_t old_flags;
1869         int rc;
1870
1871         rc = is_bnxt_in_error(bp);
1872         if (rc)
1873                 return rc;
1874
1875         /* Filter settings will get applied when port is started */
1876         if (!eth_dev->data->dev_started)
1877                 return 0;
1878
1879         if (bp->vnic_info == NULL)
1880                 return 0;
1881
1882         vnic = BNXT_GET_DEFAULT_VNIC(bp);
1883
1884         old_flags = vnic->flags;
1885         vnic->flags &= ~BNXT_VNIC_INFO_ALLMULTI;
1886         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
1887         if (rc != 0)
1888                 vnic->flags = old_flags;
1889
1890         return rc;
1891 }
1892
1893 /* Return bnxt_rx_queue pointer corresponding to a given rxq. */
1894 static struct bnxt_rx_queue *bnxt_qid_to_rxq(struct bnxt *bp, uint16_t qid)
1895 {
1896         if (qid >= bp->rx_nr_rings)
1897                 return NULL;
1898
1899         return bp->eth_dev->data->rx_queues[qid];
1900 }
1901
1902 /* Return rxq corresponding to a given rss table ring/group ID. */
1903 static uint16_t bnxt_rss_to_qid(struct bnxt *bp, uint16_t fwr)
1904 {
1905         struct bnxt_rx_queue *rxq;
1906         unsigned int i;
1907
1908         if (!BNXT_HAS_RING_GRPS(bp)) {
1909                 for (i = 0; i < bp->rx_nr_rings; i++) {
1910                         rxq = bp->eth_dev->data->rx_queues[i];
1911                         if (rxq->rx_ring->rx_ring_struct->fw_ring_id == fwr)
1912                                 return rxq->index;
1913                 }
1914         } else {
1915                 for (i = 0; i < bp->rx_nr_rings; i++) {
1916                         if (bp->grp_info[i].fw_grp_id == fwr)
1917                                 return i;
1918                 }
1919         }
1920
1921         return INVALID_HW_RING_ID;
1922 }
1923
1924 static int bnxt_reta_update_op(struct rte_eth_dev *eth_dev,
1925                             struct rte_eth_rss_reta_entry64 *reta_conf,
1926                             uint16_t reta_size)
1927 {
1928         struct bnxt *bp = eth_dev->data->dev_private;
1929         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
1930         struct bnxt_vnic_info *vnic = BNXT_GET_DEFAULT_VNIC(bp);
1931         uint16_t tbl_size = bnxt_rss_hash_tbl_size(bp);
1932         uint16_t idx, sft;
1933         int i, rc;
1934
1935         rc = is_bnxt_in_error(bp);
1936         if (rc)
1937                 return rc;
1938
1939         if (!vnic->rss_table)
1940                 return -EINVAL;
1941
1942         if (!(dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG))
1943                 return -EINVAL;
1944
1945         if (reta_size != tbl_size) {
1946                 PMD_DRV_LOG(ERR, "The configured hash table lookup size "
1947                         "(%d) must equal the size supported by the hardware "
1948                         "(%d)\n", reta_size, tbl_size);
1949                 return -EINVAL;
1950         }
1951
1952         for (i = 0; i < reta_size; i++) {
1953                 struct bnxt_rx_queue *rxq;
1954
1955                 idx = i / RTE_RETA_GROUP_SIZE;
1956                 sft = i % RTE_RETA_GROUP_SIZE;
1957
1958                 if (!(reta_conf[idx].mask & (1ULL << sft)))
1959                         continue;
1960
1961                 rxq = bnxt_qid_to_rxq(bp, reta_conf[idx].reta[sft]);
1962                 if (!rxq) {
1963                         PMD_DRV_LOG(ERR, "Invalid ring in reta_conf.\n");
1964                         return -EINVAL;
1965                 }
1966
1967                 if (BNXT_CHIP_P5(bp)) {
1968                         vnic->rss_table[i * 2] =
1969                                 rxq->rx_ring->rx_ring_struct->fw_ring_id;
1970                         vnic->rss_table[i * 2 + 1] =
1971                                 rxq->cp_ring->cp_ring_struct->fw_ring_id;
1972                 } else {
1973                         vnic->rss_table[i] =
1974                             vnic->fw_grp_ids[reta_conf[idx].reta[sft]];
1975                 }
1976         }
1977
1978         rc = bnxt_hwrm_vnic_rss_cfg(bp, vnic);
1979         return rc;
1980 }
1981
1982 static int bnxt_reta_query_op(struct rte_eth_dev *eth_dev,
1983                               struct rte_eth_rss_reta_entry64 *reta_conf,
1984                               uint16_t reta_size)
1985 {
1986         struct bnxt *bp = eth_dev->data->dev_private;
1987         struct bnxt_vnic_info *vnic = BNXT_GET_DEFAULT_VNIC(bp);
1988         uint16_t tbl_size = bnxt_rss_hash_tbl_size(bp);
1989         uint16_t idx, sft, i;
1990         int rc;
1991
1992         rc = is_bnxt_in_error(bp);
1993         if (rc)
1994                 return rc;
1995
1996         /* Retrieve from the default VNIC */
1997         if (!vnic)
1998                 return -EINVAL;
1999         if (!vnic->rss_table)
2000                 return -EINVAL;
2001
2002         if (reta_size != tbl_size) {
2003                 PMD_DRV_LOG(ERR, "The configured hash table lookup size "
2004                         "(%d) must equal the size supported by the hardware "
2005                         "(%d)\n", reta_size, tbl_size);
2006                 return -EINVAL;
2007         }
2008
2009         for (idx = 0, i = 0; i < reta_size; i++) {
2010                 idx = i / RTE_RETA_GROUP_SIZE;
2011                 sft = i % RTE_RETA_GROUP_SIZE;
2012
2013                 if (reta_conf[idx].mask & (1ULL << sft)) {
2014                         uint16_t qid;
2015
2016                         if (BNXT_CHIP_P5(bp))
2017                                 qid = bnxt_rss_to_qid(bp,
2018                                                       vnic->rss_table[i * 2]);
2019                         else
2020                                 qid = bnxt_rss_to_qid(bp, vnic->rss_table[i]);
2021
2022                         if (qid == INVALID_HW_RING_ID) {
2023                                 PMD_DRV_LOG(ERR, "Inv. entry in rss table.\n");
2024                                 return -EINVAL;
2025                         }
2026                         reta_conf[idx].reta[sft] = qid;
2027                 }
2028         }
2029
2030         return 0;
2031 }
2032
2033 static int bnxt_rss_hash_update_op(struct rte_eth_dev *eth_dev,
2034                                    struct rte_eth_rss_conf *rss_conf)
2035 {
2036         struct bnxt *bp = eth_dev->data->dev_private;
2037         struct rte_eth_conf *dev_conf = &bp->eth_dev->data->dev_conf;
2038         struct bnxt_vnic_info *vnic;
2039         int rc;
2040
2041         rc = is_bnxt_in_error(bp);
2042         if (rc)
2043                 return rc;
2044
2045         /*
2046          * If RSS enablement were different than dev_configure,
2047          * then return -EINVAL
2048          */
2049         if (dev_conf->rxmode.mq_mode & ETH_MQ_RX_RSS_FLAG) {
2050                 if (!rss_conf->rss_hf)
2051                         PMD_DRV_LOG(ERR, "Hash type NONE\n");
2052         } else {
2053                 if (rss_conf->rss_hf & BNXT_ETH_RSS_SUPPORT)
2054                         return -EINVAL;
2055         }
2056
2057         bp->flags |= BNXT_FLAG_UPDATE_HASH;
2058         memcpy(&eth_dev->data->dev_conf.rx_adv_conf.rss_conf,
2059                rss_conf,
2060                sizeof(*rss_conf));
2061
2062         /* Update the default RSS VNIC(s) */
2063         vnic = BNXT_GET_DEFAULT_VNIC(bp);
2064         vnic->hash_type = bnxt_rte_to_hwrm_hash_types(rss_conf->rss_hf);
2065         vnic->hash_mode =
2066                 bnxt_rte_to_hwrm_hash_level(bp, rss_conf->rss_hf,
2067                                             ETH_RSS_LEVEL(rss_conf->rss_hf));
2068
2069         /*
2070          * If hashkey is not specified, use the previously configured
2071          * hashkey
2072          */
2073         if (!rss_conf->rss_key)
2074                 goto rss_config;
2075
2076         if (rss_conf->rss_key_len != HW_HASH_KEY_SIZE) {
2077                 PMD_DRV_LOG(ERR,
2078                             "Invalid hashkey length, should be 16 bytes\n");
2079                 return -EINVAL;
2080         }
2081         memcpy(vnic->rss_hash_key, rss_conf->rss_key, rss_conf->rss_key_len);
2082
2083 rss_config:
2084         rc = bnxt_hwrm_vnic_rss_cfg(bp, vnic);
2085         return rc;
2086 }
2087
2088 static int bnxt_rss_hash_conf_get_op(struct rte_eth_dev *eth_dev,
2089                                      struct rte_eth_rss_conf *rss_conf)
2090 {
2091         struct bnxt *bp = eth_dev->data->dev_private;
2092         struct bnxt_vnic_info *vnic = BNXT_GET_DEFAULT_VNIC(bp);
2093         int len, rc;
2094         uint32_t hash_types;
2095
2096         rc = is_bnxt_in_error(bp);
2097         if (rc)
2098                 return rc;
2099
2100         /* RSS configuration is the same for all VNICs */
2101         if (vnic && vnic->rss_hash_key) {
2102                 if (rss_conf->rss_key) {
2103                         len = rss_conf->rss_key_len <= HW_HASH_KEY_SIZE ?
2104                               rss_conf->rss_key_len : HW_HASH_KEY_SIZE;
2105                         memcpy(rss_conf->rss_key, vnic->rss_hash_key, len);
2106                 }
2107
2108                 hash_types = vnic->hash_type;
2109                 rss_conf->rss_hf = 0;
2110                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4) {
2111                         rss_conf->rss_hf |= ETH_RSS_IPV4;
2112                         hash_types &= ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4;
2113                 }
2114                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4) {
2115                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_TCP;
2116                         hash_types &=
2117                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4;
2118                 }
2119                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4) {
2120                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV4_UDP;
2121                         hash_types &=
2122                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4;
2123                 }
2124                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6) {
2125                         rss_conf->rss_hf |= ETH_RSS_IPV6;
2126                         hash_types &= ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6;
2127                 }
2128                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6) {
2129                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_TCP;
2130                         hash_types &=
2131                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6;
2132                 }
2133                 if (hash_types & HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6) {
2134                         rss_conf->rss_hf |= ETH_RSS_NONFRAG_IPV6_UDP;
2135                         hash_types &=
2136                                 ~HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
2137                 }
2138
2139                 rss_conf->rss_hf |=
2140                         bnxt_hwrm_to_rte_rss_level(bp, vnic->hash_mode);
2141
2142                 if (hash_types) {
2143                         PMD_DRV_LOG(ERR,
2144                                 "Unknown RSS config from firmware (%08x), RSS disabled",
2145                                 vnic->hash_type);
2146                         return -ENOTSUP;
2147                 }
2148         } else {
2149                 rss_conf->rss_hf = 0;
2150         }
2151         return 0;
2152 }
2153
2154 static int bnxt_flow_ctrl_get_op(struct rte_eth_dev *dev,
2155                                struct rte_eth_fc_conf *fc_conf)
2156 {
2157         struct bnxt *bp = dev->data->dev_private;
2158         struct rte_eth_link link_info;
2159         int rc;
2160
2161         rc = is_bnxt_in_error(bp);
2162         if (rc)
2163                 return rc;
2164
2165         rc = bnxt_get_hwrm_link_config(bp, &link_info);
2166         if (rc)
2167                 return rc;
2168
2169         memset(fc_conf, 0, sizeof(*fc_conf));
2170         if (bp->link_info->auto_pause)
2171                 fc_conf->autoneg = 1;
2172         switch (bp->link_info->pause) {
2173         case 0:
2174                 fc_conf->mode = RTE_FC_NONE;
2175                 break;
2176         case HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX:
2177                 fc_conf->mode = RTE_FC_TX_PAUSE;
2178                 break;
2179         case HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX:
2180                 fc_conf->mode = RTE_FC_RX_PAUSE;
2181                 break;
2182         case (HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2183                         HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX):
2184                 fc_conf->mode = RTE_FC_FULL;
2185                 break;
2186         }
2187         return 0;
2188 }
2189
2190 static int bnxt_flow_ctrl_set_op(struct rte_eth_dev *dev,
2191                                struct rte_eth_fc_conf *fc_conf)
2192 {
2193         struct bnxt *bp = dev->data->dev_private;
2194         int rc;
2195
2196         rc = is_bnxt_in_error(bp);
2197         if (rc)
2198                 return rc;
2199
2200         if (!BNXT_SINGLE_PF(bp)) {
2201                 PMD_DRV_LOG(ERR,
2202                             "Flow Control Settings cannot be modified on VF or on shared PF\n");
2203                 return -ENOTSUP;
2204         }
2205
2206         switch (fc_conf->mode) {
2207         case RTE_FC_NONE:
2208                 bp->link_info->auto_pause = 0;
2209                 bp->link_info->force_pause = 0;
2210                 break;
2211         case RTE_FC_RX_PAUSE:
2212                 if (fc_conf->autoneg) {
2213                         bp->link_info->auto_pause =
2214                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_RX;
2215                         bp->link_info->force_pause = 0;
2216                 } else {
2217                         bp->link_info->auto_pause = 0;
2218                         bp->link_info->force_pause =
2219                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_RX;
2220                 }
2221                 break;
2222         case RTE_FC_TX_PAUSE:
2223                 if (fc_conf->autoneg) {
2224                         bp->link_info->auto_pause =
2225                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_TX;
2226                         bp->link_info->force_pause = 0;
2227                 } else {
2228                         bp->link_info->auto_pause = 0;
2229                         bp->link_info->force_pause =
2230                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_TX;
2231                 }
2232                 break;
2233         case RTE_FC_FULL:
2234                 if (fc_conf->autoneg) {
2235                         bp->link_info->auto_pause =
2236                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_TX |
2237                                         HWRM_PORT_PHY_CFG_INPUT_AUTO_PAUSE_RX;
2238                         bp->link_info->force_pause = 0;
2239                 } else {
2240                         bp->link_info->auto_pause = 0;
2241                         bp->link_info->force_pause =
2242                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_TX |
2243                                         HWRM_PORT_PHY_CFG_INPUT_FORCE_PAUSE_RX;
2244                 }
2245                 break;
2246         }
2247         return bnxt_set_hwrm_link_config(bp, true);
2248 }
2249
2250 /* Add UDP tunneling port */
2251 static int
2252 bnxt_udp_tunnel_port_add_op(struct rte_eth_dev *eth_dev,
2253                          struct rte_eth_udp_tunnel *udp_tunnel)
2254 {
2255         struct bnxt *bp = eth_dev->data->dev_private;
2256         uint16_t tunnel_type = 0;
2257         int rc = 0;
2258
2259         rc = is_bnxt_in_error(bp);
2260         if (rc)
2261                 return rc;
2262
2263         switch (udp_tunnel->prot_type) {
2264         case RTE_TUNNEL_TYPE_VXLAN:
2265                 if (bp->vxlan_port_cnt) {
2266                         PMD_DRV_LOG(ERR, "Tunnel Port %d already programmed\n",
2267                                 udp_tunnel->udp_port);
2268                         if (bp->vxlan_port != udp_tunnel->udp_port) {
2269                                 PMD_DRV_LOG(ERR, "Only one port allowed\n");
2270                                 return -ENOSPC;
2271                         }
2272                         bp->vxlan_port_cnt++;
2273                         return 0;
2274                 }
2275                 tunnel_type =
2276                         HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_VXLAN;
2277                 bp->vxlan_port_cnt++;
2278                 break;
2279         case RTE_TUNNEL_TYPE_GENEVE:
2280                 if (bp->geneve_port_cnt) {
2281                         PMD_DRV_LOG(ERR, "Tunnel Port %d already programmed\n",
2282                                 udp_tunnel->udp_port);
2283                         if (bp->geneve_port != udp_tunnel->udp_port) {
2284                                 PMD_DRV_LOG(ERR, "Only one port allowed\n");
2285                                 return -ENOSPC;
2286                         }
2287                         bp->geneve_port_cnt++;
2288                         return 0;
2289                 }
2290                 tunnel_type =
2291                         HWRM_TUNNEL_DST_PORT_ALLOC_INPUT_TUNNEL_TYPE_GENEVE;
2292                 bp->geneve_port_cnt++;
2293                 break;
2294         default:
2295                 PMD_DRV_LOG(ERR, "Tunnel type is not supported\n");
2296                 return -ENOTSUP;
2297         }
2298         rc = bnxt_hwrm_tunnel_dst_port_alloc(bp, udp_tunnel->udp_port,
2299                                              tunnel_type);
2300         return rc;
2301 }
2302
2303 static int
2304 bnxt_udp_tunnel_port_del_op(struct rte_eth_dev *eth_dev,
2305                          struct rte_eth_udp_tunnel *udp_tunnel)
2306 {
2307         struct bnxt *bp = eth_dev->data->dev_private;
2308         uint16_t tunnel_type = 0;
2309         uint16_t port = 0;
2310         int rc = 0;
2311
2312         rc = is_bnxt_in_error(bp);
2313         if (rc)
2314                 return rc;
2315
2316         switch (udp_tunnel->prot_type) {
2317         case RTE_TUNNEL_TYPE_VXLAN:
2318                 if (!bp->vxlan_port_cnt) {
2319                         PMD_DRV_LOG(ERR, "No Tunnel port configured yet\n");
2320                         return -EINVAL;
2321                 }
2322                 if (bp->vxlan_port != udp_tunnel->udp_port) {
2323                         PMD_DRV_LOG(ERR, "Req Port: %d. Configured port: %d\n",
2324                                 udp_tunnel->udp_port, bp->vxlan_port);
2325                         return -EINVAL;
2326                 }
2327                 if (--bp->vxlan_port_cnt)
2328                         return 0;
2329
2330                 tunnel_type =
2331                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_VXLAN;
2332                 port = bp->vxlan_fw_dst_port_id;
2333                 break;
2334         case RTE_TUNNEL_TYPE_GENEVE:
2335                 if (!bp->geneve_port_cnt) {
2336                         PMD_DRV_LOG(ERR, "No Tunnel port configured yet\n");
2337                         return -EINVAL;
2338                 }
2339                 if (bp->geneve_port != udp_tunnel->udp_port) {
2340                         PMD_DRV_LOG(ERR, "Req Port: %d. Configured port: %d\n",
2341                                 udp_tunnel->udp_port, bp->geneve_port);
2342                         return -EINVAL;
2343                 }
2344                 if (--bp->geneve_port_cnt)
2345                         return 0;
2346
2347                 tunnel_type =
2348                         HWRM_TUNNEL_DST_PORT_FREE_INPUT_TUNNEL_TYPE_GENEVE;
2349                 port = bp->geneve_fw_dst_port_id;
2350                 break;
2351         default:
2352                 PMD_DRV_LOG(ERR, "Tunnel type is not supported\n");
2353                 return -ENOTSUP;
2354         }
2355
2356         rc = bnxt_hwrm_tunnel_dst_port_free(bp, port, tunnel_type);
2357         return rc;
2358 }
2359
2360 static int bnxt_del_vlan_filter(struct bnxt *bp, uint16_t vlan_id)
2361 {
2362         struct bnxt_filter_info *filter;
2363         struct bnxt_vnic_info *vnic;
2364         int rc = 0;
2365         uint32_t chk = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN;
2366
2367         vnic = BNXT_GET_DEFAULT_VNIC(bp);
2368         filter = STAILQ_FIRST(&vnic->filter);
2369         while (filter) {
2370                 /* Search for this matching MAC+VLAN filter */
2371                 if (bnxt_vlan_filter_exists(bp, filter, chk, vlan_id)) {
2372                         /* Delete the filter */
2373                         rc = bnxt_hwrm_clear_l2_filter(bp, filter);
2374                         if (rc)
2375                                 return rc;
2376                         STAILQ_REMOVE(&vnic->filter, filter,
2377                                       bnxt_filter_info, next);
2378                         bnxt_free_filter(bp, filter);
2379                         PMD_DRV_LOG(INFO,
2380                                     "Deleted vlan filter for %d\n",
2381                                     vlan_id);
2382                         return 0;
2383                 }
2384                 filter = STAILQ_NEXT(filter, next);
2385         }
2386         return -ENOENT;
2387 }
2388
2389 static int bnxt_add_vlan_filter(struct bnxt *bp, uint16_t vlan_id)
2390 {
2391         struct bnxt_filter_info *filter;
2392         struct bnxt_vnic_info *vnic;
2393         int rc = 0;
2394         uint32_t en = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN |
2395                 HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN_MASK;
2396         uint32_t chk = HWRM_CFA_L2_FILTER_ALLOC_INPUT_ENABLES_L2_IVLAN;
2397
2398         /* Implementation notes on the use of VNIC in this command:
2399          *
2400          * By default, these filters belong to default vnic for the function.
2401          * Once these filters are set up, only destination VNIC can be modified.
2402          * If the destination VNIC is not specified in this command,
2403          * then the HWRM shall only create an l2 context id.
2404          */
2405
2406         vnic = BNXT_GET_DEFAULT_VNIC(bp);
2407         filter = STAILQ_FIRST(&vnic->filter);
2408         /* Check if the VLAN has already been added */
2409         while (filter) {
2410                 if (bnxt_vlan_filter_exists(bp, filter, chk, vlan_id))
2411                         return -EEXIST;
2412
2413                 filter = STAILQ_NEXT(filter, next);
2414         }
2415
2416         /* No match found. Alloc a fresh filter and issue the L2_FILTER_ALLOC
2417          * command to create MAC+VLAN filter with the right flags, enables set.
2418          */
2419         filter = bnxt_alloc_filter(bp);
2420         if (!filter) {
2421                 PMD_DRV_LOG(ERR,
2422                             "MAC/VLAN filter alloc failed\n");
2423                 return -ENOMEM;
2424         }
2425         /* MAC + VLAN ID filter */
2426         /* If l2_ivlan == 0 and l2_ivlan_mask != 0, only
2427          * untagged packets are received
2428          *
2429          * If l2_ivlan != 0 and l2_ivlan_mask != 0, untagged
2430          * packets and only the programmed vlan's packets are received
2431          */
2432         filter->l2_ivlan = vlan_id;
2433         filter->l2_ivlan_mask = 0x0FFF;
2434         filter->enables |= en;
2435         filter->flags |= HWRM_CFA_L2_FILTER_ALLOC_INPUT_FLAGS_OUTERMOST;
2436
2437         rc = bnxt_hwrm_set_l2_filter(bp, vnic->fw_vnic_id, filter);
2438         if (rc) {
2439                 /* Free the newly allocated filter as we were
2440                  * not able to create the filter in hardware.
2441                  */
2442                 bnxt_free_filter(bp, filter);
2443                 return rc;
2444         }
2445
2446         filter->mac_index = 0;
2447         /* Add this new filter to the list */
2448         if (vlan_id == 0)
2449                 STAILQ_INSERT_HEAD(&vnic->filter, filter, next);
2450         else
2451                 STAILQ_INSERT_TAIL(&vnic->filter, filter, next);
2452
2453         PMD_DRV_LOG(INFO,
2454                     "Added Vlan filter for %d\n", vlan_id);
2455         return rc;
2456 }
2457
2458 static int bnxt_vlan_filter_set_op(struct rte_eth_dev *eth_dev,
2459                 uint16_t vlan_id, int on)
2460 {
2461         struct bnxt *bp = eth_dev->data->dev_private;
2462         int rc;
2463
2464         rc = is_bnxt_in_error(bp);
2465         if (rc)
2466                 return rc;
2467
2468         if (!eth_dev->data->dev_started) {
2469                 PMD_DRV_LOG(ERR, "port must be started before setting vlan\n");
2470                 return -EINVAL;
2471         }
2472
2473         /* These operations apply to ALL existing MAC/VLAN filters */
2474         if (on)
2475                 return bnxt_add_vlan_filter(bp, vlan_id);
2476         else
2477                 return bnxt_del_vlan_filter(bp, vlan_id);
2478 }
2479
2480 static int bnxt_del_dflt_mac_filter(struct bnxt *bp,
2481                                     struct bnxt_vnic_info *vnic)
2482 {
2483         struct bnxt_filter_info *filter;
2484         int rc;
2485
2486         filter = STAILQ_FIRST(&vnic->filter);
2487         while (filter) {
2488                 if (filter->mac_index == 0 &&
2489                     !memcmp(filter->l2_addr, bp->mac_addr,
2490                             RTE_ETHER_ADDR_LEN)) {
2491                         rc = bnxt_hwrm_clear_l2_filter(bp, filter);
2492                         if (!rc) {
2493                                 STAILQ_REMOVE(&vnic->filter, filter,
2494                                               bnxt_filter_info, next);
2495                                 bnxt_free_filter(bp, filter);
2496                         }
2497                         return rc;
2498                 }
2499                 filter = STAILQ_NEXT(filter, next);
2500         }
2501         return 0;
2502 }
2503
2504 static int
2505 bnxt_config_vlan_hw_filter(struct bnxt *bp, uint64_t rx_offloads)
2506 {
2507         struct bnxt_vnic_info *vnic;
2508         unsigned int i;
2509         int rc;
2510
2511         vnic = BNXT_GET_DEFAULT_VNIC(bp);
2512         if (!(rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER)) {
2513                 /* Remove any VLAN filters programmed */
2514                 for (i = 0; i < RTE_ETHER_MAX_VLAN_ID; i++)
2515                         bnxt_del_vlan_filter(bp, i);
2516
2517                 rc = bnxt_add_mac_filter(bp, vnic, NULL, 0, 0);
2518                 if (rc)
2519                         return rc;
2520         } else {
2521                 /* Default filter will allow packets that match the
2522                  * dest mac. So, it has to be deleted, otherwise, we
2523                  * will endup receiving vlan packets for which the
2524                  * filter is not programmed, when hw-vlan-filter
2525                  * configuration is ON
2526                  */
2527                 bnxt_del_dflt_mac_filter(bp, vnic);
2528                 /* This filter will allow only untagged packets */
2529                 bnxt_add_vlan_filter(bp, 0);
2530         }
2531         PMD_DRV_LOG(DEBUG, "VLAN Filtering: %d\n",
2532                     !!(rx_offloads & DEV_RX_OFFLOAD_VLAN_FILTER));
2533
2534         return 0;
2535 }
2536
2537 static int bnxt_free_one_vnic(struct bnxt *bp, uint16_t vnic_id)
2538 {
2539         struct bnxt_vnic_info *vnic = &bp->vnic_info[vnic_id];
2540         unsigned int i;
2541         int rc;
2542
2543         /* Destroy vnic filters and vnic */
2544         if (bp->eth_dev->data->dev_conf.rxmode.offloads &
2545             DEV_RX_OFFLOAD_VLAN_FILTER) {
2546                 for (i = 0; i < RTE_ETHER_MAX_VLAN_ID; i++)
2547                         bnxt_del_vlan_filter(bp, i);
2548         }
2549         bnxt_del_dflt_mac_filter(bp, vnic);
2550
2551         rc = bnxt_hwrm_vnic_ctx_free(bp, vnic);
2552         if (rc)
2553                 return rc;
2554
2555         rc = bnxt_hwrm_vnic_free(bp, vnic);
2556         if (rc)
2557                 return rc;
2558
2559         rte_free(vnic->fw_grp_ids);
2560         vnic->fw_grp_ids = NULL;
2561
2562         vnic->rx_queue_cnt = 0;
2563
2564         return 0;
2565 }
2566
2567 static int
2568 bnxt_config_vlan_hw_stripping(struct bnxt *bp, uint64_t rx_offloads)
2569 {
2570         struct bnxt_vnic_info *vnic = BNXT_GET_DEFAULT_VNIC(bp);
2571         int rc;
2572
2573         /* Destroy, recreate and reconfigure the default vnic */
2574         rc = bnxt_free_one_vnic(bp, 0);
2575         if (rc)
2576                 return rc;
2577
2578         /* default vnic 0 */
2579         rc = bnxt_setup_one_vnic(bp, 0);
2580         if (rc)
2581                 return rc;
2582
2583         if (bp->eth_dev->data->dev_conf.rxmode.offloads &
2584             DEV_RX_OFFLOAD_VLAN_FILTER) {
2585                 rc = bnxt_add_vlan_filter(bp, 0);
2586                 if (rc)
2587                         return rc;
2588                 rc = bnxt_restore_vlan_filters(bp);
2589                 if (rc)
2590                         return rc;
2591         } else {
2592                 rc = bnxt_add_mac_filter(bp, vnic, NULL, 0, 0);
2593                 if (rc)
2594                         return rc;
2595         }
2596
2597         rc = bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
2598         if (rc)
2599                 return rc;
2600
2601         PMD_DRV_LOG(DEBUG, "VLAN Strip Offload: %d\n",
2602                     !!(rx_offloads & DEV_RX_OFFLOAD_VLAN_STRIP));
2603
2604         return rc;
2605 }
2606
2607 static int
2608 bnxt_vlan_offload_set_op(struct rte_eth_dev *dev, int mask)
2609 {
2610         uint64_t rx_offloads = dev->data->dev_conf.rxmode.offloads;
2611         struct bnxt *bp = dev->data->dev_private;
2612         int rc;
2613
2614         rc = is_bnxt_in_error(bp);
2615         if (rc)
2616                 return rc;
2617
2618         /* Filter settings will get applied when port is started */
2619         if (!dev->data->dev_started)
2620                 return 0;
2621
2622         if (mask & ETH_VLAN_FILTER_MASK) {
2623                 /* Enable or disable VLAN filtering */
2624                 rc = bnxt_config_vlan_hw_filter(bp, rx_offloads);
2625                 if (rc)
2626                         return rc;
2627         }
2628
2629         if (mask & ETH_VLAN_STRIP_MASK) {
2630                 /* Enable or disable VLAN stripping */
2631                 rc = bnxt_config_vlan_hw_stripping(bp, rx_offloads);
2632                 if (rc)
2633                         return rc;
2634         }
2635
2636         if (mask & ETH_VLAN_EXTEND_MASK) {
2637                 if (rx_offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)
2638                         PMD_DRV_LOG(DEBUG, "Extend VLAN supported\n");
2639                 else
2640                         PMD_DRV_LOG(INFO, "Extend VLAN unsupported\n");
2641         }
2642
2643         return 0;
2644 }
2645
2646 static int
2647 bnxt_vlan_tpid_set_op(struct rte_eth_dev *dev, enum rte_vlan_type vlan_type,
2648                       uint16_t tpid)
2649 {
2650         struct bnxt *bp = dev->data->dev_private;
2651         int qinq = dev->data->dev_conf.rxmode.offloads &
2652                    DEV_RX_OFFLOAD_VLAN_EXTEND;
2653
2654         if (vlan_type != ETH_VLAN_TYPE_INNER &&
2655             vlan_type != ETH_VLAN_TYPE_OUTER) {
2656                 PMD_DRV_LOG(ERR,
2657                             "Unsupported vlan type.");
2658                 return -EINVAL;
2659         }
2660         if (!qinq) {
2661                 PMD_DRV_LOG(ERR,
2662                             "QinQ not enabled. Needs to be ON as we can "
2663                             "accelerate only outer vlan\n");
2664                 return -EINVAL;
2665         }
2666
2667         if (vlan_type == ETH_VLAN_TYPE_OUTER) {
2668                 switch (tpid) {
2669                 case RTE_ETHER_TYPE_QINQ:
2670                         bp->outer_tpid_bd =
2671                                 TX_BD_LONG_CFA_META_VLAN_TPID_TPID88A8;
2672                                 break;
2673                 case RTE_ETHER_TYPE_VLAN:
2674                         bp->outer_tpid_bd =
2675                                 TX_BD_LONG_CFA_META_VLAN_TPID_TPID8100;
2676                                 break;
2677                 case RTE_ETHER_TYPE_QINQ1:
2678                         bp->outer_tpid_bd =
2679                                 TX_BD_LONG_CFA_META_VLAN_TPID_TPID9100;
2680                                 break;
2681                 case RTE_ETHER_TYPE_QINQ2:
2682                         bp->outer_tpid_bd =
2683                                 TX_BD_LONG_CFA_META_VLAN_TPID_TPID9200;
2684                                 break;
2685                 case RTE_ETHER_TYPE_QINQ3:
2686                         bp->outer_tpid_bd =
2687                                  TX_BD_LONG_CFA_META_VLAN_TPID_TPID9300;
2688                                 break;
2689                 default:
2690                         PMD_DRV_LOG(ERR, "Invalid TPID: %x\n", tpid);
2691                         return -EINVAL;
2692                 }
2693                 bp->outer_tpid_bd |= tpid;
2694                 PMD_DRV_LOG(INFO, "outer_tpid_bd = %x\n", bp->outer_tpid_bd);
2695         } else if (vlan_type == ETH_VLAN_TYPE_INNER) {
2696                 PMD_DRV_LOG(ERR,
2697                             "Can accelerate only outer vlan in QinQ\n");
2698                 return -EINVAL;
2699         }
2700
2701         return 0;
2702 }
2703
2704 static int
2705 bnxt_set_default_mac_addr_op(struct rte_eth_dev *dev,
2706                              struct rte_ether_addr *addr)
2707 {
2708         struct bnxt *bp = dev->data->dev_private;
2709         /* Default Filter is tied to VNIC 0 */
2710         struct bnxt_vnic_info *vnic = BNXT_GET_DEFAULT_VNIC(bp);
2711         int rc;
2712
2713         rc = is_bnxt_in_error(bp);
2714         if (rc)
2715                 return rc;
2716
2717         if (BNXT_VF(bp) && !BNXT_VF_IS_TRUSTED(bp))
2718                 return -EPERM;
2719
2720         if (rte_is_zero_ether_addr(addr))
2721                 return -EINVAL;
2722
2723         /* Filter settings will get applied when port is started */
2724         if (!dev->data->dev_started)
2725                 return 0;
2726
2727         /* Check if the requested MAC is already added */
2728         if (memcmp(addr, bp->mac_addr, RTE_ETHER_ADDR_LEN) == 0)
2729                 return 0;
2730
2731         /* Destroy filter and re-create it */
2732         bnxt_del_dflt_mac_filter(bp, vnic);
2733
2734         memcpy(bp->mac_addr, addr, RTE_ETHER_ADDR_LEN);
2735         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_VLAN_FILTER) {
2736                 /* This filter will allow only untagged packets */
2737                 rc = bnxt_add_vlan_filter(bp, 0);
2738         } else {
2739                 rc = bnxt_add_mac_filter(bp, vnic, addr, 0, 0);
2740         }
2741
2742         PMD_DRV_LOG(DEBUG, "Set MAC addr\n");
2743         return rc;
2744 }
2745
2746 static int
2747 bnxt_dev_set_mc_addr_list_op(struct rte_eth_dev *eth_dev,
2748                           struct rte_ether_addr *mc_addr_set,
2749                           uint32_t nb_mc_addr)
2750 {
2751         struct bnxt *bp = eth_dev->data->dev_private;
2752         char *mc_addr_list = (char *)mc_addr_set;
2753         struct bnxt_vnic_info *vnic;
2754         uint32_t off = 0, i = 0;
2755         int rc;
2756
2757         rc = is_bnxt_in_error(bp);
2758         if (rc)
2759                 return rc;
2760
2761         vnic = BNXT_GET_DEFAULT_VNIC(bp);
2762
2763         if (nb_mc_addr > BNXT_MAX_MC_ADDRS) {
2764                 vnic->flags |= BNXT_VNIC_INFO_ALLMULTI;
2765                 goto allmulti;
2766         }
2767
2768         /* TODO Check for Duplicate mcast addresses */
2769         vnic->flags &= ~BNXT_VNIC_INFO_ALLMULTI;
2770         for (i = 0; i < nb_mc_addr; i++) {
2771                 memcpy(vnic->mc_list + off, &mc_addr_list[i],
2772                         RTE_ETHER_ADDR_LEN);
2773                 off += RTE_ETHER_ADDR_LEN;
2774         }
2775
2776         vnic->mc_addr_cnt = i;
2777         if (vnic->mc_addr_cnt)
2778                 vnic->flags |= BNXT_VNIC_INFO_MCAST;
2779         else
2780                 vnic->flags &= ~BNXT_VNIC_INFO_MCAST;
2781
2782 allmulti:
2783         return bnxt_hwrm_cfa_l2_set_rx_mask(bp, vnic, 0, NULL);
2784 }
2785
2786 static int
2787 bnxt_fw_version_get(struct rte_eth_dev *dev, char *fw_version, size_t fw_size)
2788 {
2789         struct bnxt *bp = dev->data->dev_private;
2790         uint8_t fw_major = (bp->fw_ver >> 24) & 0xff;
2791         uint8_t fw_minor = (bp->fw_ver >> 16) & 0xff;
2792         uint8_t fw_updt = (bp->fw_ver >> 8) & 0xff;
2793         uint8_t fw_rsvd = bp->fw_ver & 0xff;
2794         int ret;
2795
2796         ret = snprintf(fw_version, fw_size, "%d.%d.%d.%d",
2797                         fw_major, fw_minor, fw_updt, fw_rsvd);
2798         if (ret < 0)
2799                 return -EINVAL;
2800
2801         ret += 1; /* add the size of '\0' */
2802         if (fw_size < (size_t)ret)
2803                 return ret;
2804         else
2805                 return 0;
2806 }
2807
2808 static void
2809 bnxt_rxq_info_get_op(struct rte_eth_dev *dev, uint16_t queue_id,
2810         struct rte_eth_rxq_info *qinfo)
2811 {
2812         struct bnxt *bp = dev->data->dev_private;
2813         struct bnxt_rx_queue *rxq;
2814
2815         if (is_bnxt_in_error(bp))
2816                 return;
2817
2818         rxq = dev->data->rx_queues[queue_id];
2819
2820         qinfo->mp = rxq->mb_pool;
2821         qinfo->scattered_rx = dev->data->scattered_rx;
2822         qinfo->nb_desc = rxq->nb_rx_desc;
2823
2824         qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
2825         qinfo->conf.rx_drop_en = rxq->drop_en;
2826         qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
2827         qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
2828 }
2829
2830 static void
2831 bnxt_txq_info_get_op(struct rte_eth_dev *dev, uint16_t queue_id,
2832         struct rte_eth_txq_info *qinfo)
2833 {
2834         struct bnxt *bp = dev->data->dev_private;
2835         struct bnxt_tx_queue *txq;
2836
2837         if (is_bnxt_in_error(bp))
2838                 return;
2839
2840         txq = dev->data->tx_queues[queue_id];
2841
2842         qinfo->nb_desc = txq->nb_tx_desc;
2843
2844         qinfo->conf.tx_thresh.pthresh = txq->pthresh;
2845         qinfo->conf.tx_thresh.hthresh = txq->hthresh;
2846         qinfo->conf.tx_thresh.wthresh = txq->wthresh;
2847
2848         qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
2849         qinfo->conf.tx_rs_thresh = 0;
2850         qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
2851         qinfo->conf.offloads = txq->offloads;
2852 }
2853
2854 static const struct {
2855         eth_rx_burst_t pkt_burst;
2856         const char *info;
2857 } bnxt_rx_burst_info[] = {
2858         {bnxt_recv_pkts,        "Scalar"},
2859 #if defined(RTE_ARCH_X86)
2860         {bnxt_recv_pkts_vec,    "Vector SSE"},
2861 #elif defined(RTE_ARCH_ARM64)
2862         {bnxt_recv_pkts_vec,    "Vector Neon"},
2863 #endif
2864 };
2865
2866 static int
2867 bnxt_rx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
2868                        struct rte_eth_burst_mode *mode)
2869 {
2870         eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
2871         size_t i;
2872
2873         for (i = 0; i < RTE_DIM(bnxt_rx_burst_info); i++) {
2874                 if (pkt_burst == bnxt_rx_burst_info[i].pkt_burst) {
2875                         snprintf(mode->info, sizeof(mode->info), "%s",
2876                                  bnxt_rx_burst_info[i].info);
2877                         return 0;
2878                 }
2879         }
2880
2881         return -EINVAL;
2882 }
2883
2884 static const struct {
2885         eth_tx_burst_t pkt_burst;
2886         const char *info;
2887 } bnxt_tx_burst_info[] = {
2888         {bnxt_xmit_pkts,        "Scalar"},
2889 #if defined(RTE_ARCH_X86)
2890         {bnxt_xmit_pkts_vec,    "Vector SSE"},
2891 #elif defined(RTE_ARCH_ARM64)
2892         {bnxt_xmit_pkts_vec,    "Vector Neon"},
2893 #endif
2894 };
2895
2896 static int
2897 bnxt_tx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
2898                        struct rte_eth_burst_mode *mode)
2899 {
2900         eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
2901         size_t i;
2902
2903         for (i = 0; i < RTE_DIM(bnxt_tx_burst_info); i++) {
2904                 if (pkt_burst == bnxt_tx_burst_info[i].pkt_burst) {
2905                         snprintf(mode->info, sizeof(mode->info), "%s",
2906                                  bnxt_tx_burst_info[i].info);
2907                         return 0;
2908                 }
2909         }
2910
2911         return -EINVAL;
2912 }
2913
2914 int bnxt_mtu_set_op(struct rte_eth_dev *eth_dev, uint16_t new_mtu)
2915 {
2916         struct bnxt *bp = eth_dev->data->dev_private;
2917         uint32_t new_pkt_size;
2918         uint32_t rc = 0;
2919         uint32_t i;
2920
2921         rc = is_bnxt_in_error(bp);
2922         if (rc)
2923                 return rc;
2924
2925         /* Exit if receive queues are not configured yet */
2926         if (!eth_dev->data->nb_rx_queues)
2927                 return rc;
2928
2929         new_pkt_size = new_mtu + RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN +
2930                        VLAN_TAG_SIZE * BNXT_NUM_VLANS;
2931
2932         /*
2933          * Disallow any MTU change that would require scattered receive support
2934          * if it is not already enabled.
2935          */
2936         if (eth_dev->data->dev_started &&
2937             !eth_dev->data->scattered_rx &&
2938             (new_pkt_size >
2939              eth_dev->data->min_rx_buf_size - RTE_PKTMBUF_HEADROOM)) {
2940                 PMD_DRV_LOG(ERR,
2941                             "MTU change would require scattered rx support. ");
2942                 PMD_DRV_LOG(ERR, "Stop port before changing MTU.\n");
2943                 return -EINVAL;
2944         }
2945
2946         if (new_mtu > RTE_ETHER_MTU) {
2947                 bp->flags |= BNXT_FLAG_JUMBO;
2948                 bp->eth_dev->data->dev_conf.rxmode.offloads |=
2949                         DEV_RX_OFFLOAD_JUMBO_FRAME;
2950         } else {
2951                 bp->eth_dev->data->dev_conf.rxmode.offloads &=
2952                         ~DEV_RX_OFFLOAD_JUMBO_FRAME;
2953                 bp->flags &= ~BNXT_FLAG_JUMBO;
2954         }
2955
2956         /* Is there a change in mtu setting? */
2957         if (eth_dev->data->dev_conf.rxmode.max_rx_pkt_len == new_pkt_size)
2958                 return rc;
2959
2960         for (i = 0; i < bp->nr_vnics; i++) {
2961                 struct bnxt_vnic_info *vnic = &bp->vnic_info[i];
2962                 uint16_t size = 0;
2963
2964                 vnic->mru = BNXT_VNIC_MRU(new_mtu);
2965                 rc = bnxt_hwrm_vnic_cfg(bp, vnic);
2966                 if (rc)
2967                         break;
2968
2969                 size = rte_pktmbuf_data_room_size(bp->rx_queues[0]->mb_pool);
2970                 size -= RTE_PKTMBUF_HEADROOM;
2971
2972                 if (size < new_mtu) {
2973                         rc = bnxt_hwrm_vnic_plcmode_cfg(bp, vnic);
2974                         if (rc)
2975                                 return rc;
2976                 }
2977         }
2978
2979         if (!rc)
2980                 eth_dev->data->dev_conf.rxmode.max_rx_pkt_len = new_pkt_size;
2981
2982         PMD_DRV_LOG(INFO, "New MTU is %d\n", new_mtu);
2983
2984         return rc;
2985 }
2986
2987 static int
2988 bnxt_vlan_pvid_set_op(struct rte_eth_dev *dev, uint16_t pvid, int on)
2989 {
2990         struct bnxt *bp = dev->data->dev_private;
2991         uint16_t vlan = bp->vlan;
2992         int rc;
2993
2994         rc = is_bnxt_in_error(bp);
2995         if (rc)
2996                 return rc;
2997
2998         if (!BNXT_SINGLE_PF(bp)) {
2999                 PMD_DRV_LOG(ERR, "PVID cannot be modified on VF or on shared PF\n");
3000                 return -ENOTSUP;
3001         }
3002         bp->vlan = on ? pvid : 0;
3003
3004         rc = bnxt_hwrm_set_default_vlan(bp, 0, 0);
3005         if (rc)
3006                 bp->vlan = vlan;
3007         return rc;
3008 }
3009
3010 static int
3011 bnxt_dev_led_on_op(struct rte_eth_dev *dev)
3012 {
3013         struct bnxt *bp = dev->data->dev_private;
3014         int rc;
3015
3016         rc = is_bnxt_in_error(bp);
3017         if (rc)
3018                 return rc;
3019
3020         return bnxt_hwrm_port_led_cfg(bp, true);
3021 }
3022
3023 static int
3024 bnxt_dev_led_off_op(struct rte_eth_dev *dev)
3025 {
3026         struct bnxt *bp = dev->data->dev_private;
3027         int rc;
3028
3029         rc = is_bnxt_in_error(bp);
3030         if (rc)
3031                 return rc;
3032
3033         return bnxt_hwrm_port_led_cfg(bp, false);
3034 }
3035
3036 static uint32_t
3037 bnxt_rx_queue_count_op(struct rte_eth_dev *dev, uint16_t rx_queue_id)
3038 {
3039         struct bnxt *bp = (struct bnxt *)dev->data->dev_private;
3040         struct bnxt_cp_ring_info *cpr;
3041         uint32_t desc = 0, raw_cons;
3042         struct bnxt_rx_queue *rxq;
3043         struct rx_pkt_cmpl *rxcmp;
3044         int rc;
3045
3046         rc = is_bnxt_in_error(bp);
3047         if (rc)
3048                 return rc;
3049
3050         rxq = dev->data->rx_queues[rx_queue_id];
3051         cpr = rxq->cp_ring;
3052         raw_cons = cpr->cp_raw_cons;
3053
3054         while (1) {
3055                 uint32_t agg_cnt, cons, cmpl_type;
3056
3057                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
3058                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
3059
3060                 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
3061                         break;
3062
3063                 cmpl_type = CMP_TYPE(rxcmp);
3064
3065                 switch (cmpl_type) {
3066                 case CMPL_BASE_TYPE_RX_L2:
3067                 case CMPL_BASE_TYPE_RX_L2_V2:
3068                         agg_cnt = BNXT_RX_L2_AGG_BUFS(rxcmp);
3069                         raw_cons = raw_cons + CMP_LEN(cmpl_type) + agg_cnt;
3070                         desc++;
3071                         break;
3072
3073                 case CMPL_BASE_TYPE_RX_TPA_END:
3074                         if (BNXT_CHIP_P5(rxq->bp)) {
3075                                 struct rx_tpa_v2_end_cmpl_hi *p5_tpa_end;
3076
3077                                 p5_tpa_end = (void *)rxcmp;
3078                                 agg_cnt = BNXT_TPA_END_AGG_BUFS_TH(p5_tpa_end);
3079                         } else {
3080                                 struct rx_tpa_end_cmpl *tpa_end;
3081
3082                                 tpa_end = (void *)rxcmp;
3083                                 agg_cnt = BNXT_TPA_END_AGG_BUFS(tpa_end);
3084                         }
3085
3086                         raw_cons = raw_cons + CMP_LEN(cmpl_type) + agg_cnt;
3087                         desc++;
3088                         break;
3089
3090                 default:
3091                         raw_cons += CMP_LEN(cmpl_type);
3092                 }
3093         }
3094
3095         return desc;
3096 }
3097
3098 static int
3099 bnxt_rx_descriptor_status_op(void *rx_queue, uint16_t offset)
3100 {
3101         struct bnxt_rx_queue *rxq = rx_queue;
3102         struct bnxt_cp_ring_info *cpr;
3103         struct bnxt_rx_ring_info *rxr;
3104         uint32_t desc, raw_cons;
3105         struct bnxt *bp = rxq->bp;
3106         struct rx_pkt_cmpl *rxcmp;
3107         int rc;
3108
3109         rc = is_bnxt_in_error(bp);
3110         if (rc)
3111                 return rc;
3112
3113         if (offset >= rxq->nb_rx_desc)
3114                 return -EINVAL;
3115
3116         rxr = rxq->rx_ring;
3117         cpr = rxq->cp_ring;
3118
3119         /*
3120          * For the vector receive case, the completion at the requested
3121          * offset can be indexed directly.
3122          */
3123 #if defined(RTE_ARCH_X86) || defined(RTE_ARCH_ARM64)
3124         if (bp->flags & BNXT_FLAG_RX_VECTOR_PKT_MODE) {
3125                 struct rx_pkt_cmpl *rxcmp;
3126                 uint32_t cons;
3127
3128                 /* Check status of completion descriptor. */
3129                 raw_cons = cpr->cp_raw_cons +
3130                            offset * CMP_LEN(CMPL_BASE_TYPE_RX_L2);
3131                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
3132                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
3133
3134                 if (CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
3135                         return RTE_ETH_RX_DESC_DONE;
3136
3137                 /* Check whether rx desc has an mbuf attached. */
3138                 cons = RING_CMP(rxr->rx_ring_struct, raw_cons / 2);
3139                 if (cons >= rxq->rxrearm_start &&
3140                     cons < rxq->rxrearm_start + rxq->rxrearm_nb) {
3141                         return RTE_ETH_RX_DESC_UNAVAIL;
3142                 }
3143
3144                 return RTE_ETH_RX_DESC_AVAIL;
3145         }
3146 #endif
3147
3148         /*
3149          * For the non-vector receive case, scan the completion ring to
3150          * locate the completion descriptor for the requested offset.
3151          */
3152         raw_cons = cpr->cp_raw_cons;
3153         desc = 0;
3154         while (1) {
3155                 uint32_t agg_cnt, cons, cmpl_type;
3156
3157                 cons = RING_CMP(cpr->cp_ring_struct, raw_cons);
3158                 rxcmp = (struct rx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
3159
3160                 if (!CMP_VALID(rxcmp, raw_cons, cpr->cp_ring_struct))
3161                         break;
3162
3163                 cmpl_type = CMP_TYPE(rxcmp);
3164
3165                 switch (cmpl_type) {
3166                 case CMPL_BASE_TYPE_RX_L2:
3167                 case CMPL_BASE_TYPE_RX_L2_V2:
3168                         if (desc == offset) {
3169                                 cons = rxcmp->opaque;
3170                                 if (rxr->rx_buf_ring[cons])
3171                                         return RTE_ETH_RX_DESC_DONE;
3172                                 else
3173                                         return RTE_ETH_RX_DESC_UNAVAIL;
3174                         }
3175                         agg_cnt = BNXT_RX_L2_AGG_BUFS(rxcmp);
3176                         raw_cons = raw_cons + CMP_LEN(cmpl_type) + agg_cnt;
3177                         desc++;
3178                         break;
3179
3180                 case CMPL_BASE_TYPE_RX_TPA_END:
3181                         if (desc == offset)
3182                                 return RTE_ETH_RX_DESC_DONE;
3183
3184                         if (BNXT_CHIP_P5(rxq->bp)) {
3185                                 struct rx_tpa_v2_end_cmpl_hi *p5_tpa_end;
3186
3187                                 p5_tpa_end = (void *)rxcmp;
3188                                 agg_cnt = BNXT_TPA_END_AGG_BUFS_TH(p5_tpa_end);
3189                         } else {
3190                                 struct rx_tpa_end_cmpl *tpa_end;
3191
3192                                 tpa_end = (void *)rxcmp;
3193                                 agg_cnt = BNXT_TPA_END_AGG_BUFS(tpa_end);
3194                         }
3195
3196                         raw_cons = raw_cons + CMP_LEN(cmpl_type) + agg_cnt;
3197                         desc++;
3198                         break;
3199
3200                 default:
3201                         raw_cons += CMP_LEN(cmpl_type);
3202                 }
3203         }
3204
3205         return RTE_ETH_RX_DESC_AVAIL;
3206 }
3207
3208 static int
3209 bnxt_tx_descriptor_status_op(void *tx_queue, uint16_t offset)
3210 {
3211         struct bnxt_tx_queue *txq = (struct bnxt_tx_queue *)tx_queue;
3212         struct bnxt_tx_ring_info *txr;
3213         struct bnxt_cp_ring_info *cpr;
3214         struct rte_mbuf **tx_buf;
3215         struct tx_pkt_cmpl *txcmp;
3216         uint32_t cons, cp_cons;
3217         int rc;
3218
3219         if (!txq)
3220                 return -EINVAL;
3221
3222         rc = is_bnxt_in_error(txq->bp);
3223         if (rc)
3224                 return rc;
3225
3226         cpr = txq->cp_ring;
3227         txr = txq->tx_ring;
3228
3229         if (offset >= txq->nb_tx_desc)
3230                 return -EINVAL;
3231
3232         cons = RING_CMP(cpr->cp_ring_struct, offset);
3233         txcmp = (struct tx_pkt_cmpl *)&cpr->cp_desc_ring[cons];
3234         cp_cons = cpr->cp_raw_cons;
3235
3236         if (cons > cp_cons) {
3237                 if (CMPL_VALID(txcmp, cpr->valid))
3238                         return RTE_ETH_TX_DESC_UNAVAIL;
3239         } else {
3240                 if (CMPL_VALID(txcmp, !cpr->valid))
3241                         return RTE_ETH_TX_DESC_UNAVAIL;
3242         }
3243         tx_buf = &txr->tx_buf_ring[cons];
3244         if (*tx_buf == NULL)
3245                 return RTE_ETH_TX_DESC_DONE;
3246
3247         return RTE_ETH_TX_DESC_FULL;
3248 }
3249
3250 int
3251 bnxt_flow_ops_get_op(struct rte_eth_dev *dev,
3252                      const struct rte_flow_ops **ops)
3253 {
3254         struct bnxt *bp = dev->data->dev_private;
3255         int ret = 0;
3256
3257         if (!bp)
3258                 return -EIO;
3259
3260         if (BNXT_ETH_DEV_IS_REPRESENTOR(dev)) {
3261                 struct bnxt_representor *vfr = dev->data->dev_private;
3262                 bp = vfr->parent_dev->data->dev_private;
3263                 /* parent is deleted while children are still valid */
3264                 if (!bp) {
3265                         PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR Error\n",
3266                                     dev->data->port_id);
3267                         return -EIO;
3268                 }
3269         }
3270
3271         ret = is_bnxt_in_error(bp);
3272         if (ret)
3273                 return ret;
3274
3275         /* PMD supports thread-safe flow operations.  rte_flow API
3276          * functions can avoid mutex for multi-thread safety.
3277          */
3278         dev->data->dev_flags |= RTE_ETH_DEV_FLOW_OPS_THREAD_SAFE;
3279
3280         if (BNXT_TRUFLOW_EN(bp))
3281                 *ops = &bnxt_ulp_rte_flow_ops;
3282         else
3283                 *ops = &bnxt_flow_ops;
3284
3285         return ret;
3286 }
3287
3288 static const uint32_t *
3289 bnxt_dev_supported_ptypes_get_op(struct rte_eth_dev *dev)
3290 {
3291         static const uint32_t ptypes[] = {
3292                 RTE_PTYPE_L2_ETHER_VLAN,
3293                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
3294                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
3295                 RTE_PTYPE_L4_ICMP,
3296                 RTE_PTYPE_L4_TCP,
3297                 RTE_PTYPE_L4_UDP,
3298                 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
3299                 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
3300                 RTE_PTYPE_INNER_L4_ICMP,
3301                 RTE_PTYPE_INNER_L4_TCP,
3302                 RTE_PTYPE_INNER_L4_UDP,
3303                 RTE_PTYPE_UNKNOWN
3304         };
3305
3306         if (!dev->rx_pkt_burst)
3307                 return NULL;
3308
3309         return ptypes;
3310 }
3311
3312 static int bnxt_map_regs(struct bnxt *bp, uint32_t *reg_arr, int count,
3313                          int reg_win)
3314 {
3315         uint32_t reg_base = *reg_arr & 0xfffff000;
3316         uint32_t win_off;
3317         int i;
3318
3319         for (i = 0; i < count; i++) {
3320                 if ((reg_arr[i] & 0xfffff000) != reg_base)
3321                         return -ERANGE;
3322         }
3323         win_off = BNXT_GRCPF_REG_WINDOW_BASE_OUT + (reg_win - 1) * 4;
3324         rte_write32(reg_base, (uint8_t *)bp->bar0 + win_off);
3325         return 0;
3326 }
3327
3328 static int bnxt_map_ptp_regs(struct bnxt *bp)
3329 {
3330         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3331         uint32_t *reg_arr;
3332         int rc, i;
3333
3334         reg_arr = ptp->rx_regs;
3335         rc = bnxt_map_regs(bp, reg_arr, BNXT_PTP_RX_REGS, 5);
3336         if (rc)
3337                 return rc;
3338
3339         reg_arr = ptp->tx_regs;
3340         rc = bnxt_map_regs(bp, reg_arr, BNXT_PTP_TX_REGS, 6);
3341         if (rc)
3342                 return rc;
3343
3344         for (i = 0; i < BNXT_PTP_RX_REGS; i++)
3345                 ptp->rx_mapped_regs[i] = 0x5000 + (ptp->rx_regs[i] & 0xfff);
3346
3347         for (i = 0; i < BNXT_PTP_TX_REGS; i++)
3348                 ptp->tx_mapped_regs[i] = 0x6000 + (ptp->tx_regs[i] & 0xfff);
3349
3350         return 0;
3351 }
3352
3353 static void bnxt_unmap_ptp_regs(struct bnxt *bp)
3354 {
3355         rte_write32(0, (uint8_t *)bp->bar0 +
3356                          BNXT_GRCPF_REG_WINDOW_BASE_OUT + 16);
3357         rte_write32(0, (uint8_t *)bp->bar0 +
3358                          BNXT_GRCPF_REG_WINDOW_BASE_OUT + 20);
3359 }
3360
3361 static uint64_t bnxt_cc_read(struct bnxt *bp)
3362 {
3363         uint64_t ns;
3364
3365         ns = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3366                               BNXT_GRCPF_REG_SYNC_TIME));
3367         ns |= (uint64_t)(rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3368                                           BNXT_GRCPF_REG_SYNC_TIME + 4))) << 32;
3369         return ns;
3370 }
3371
3372 static int bnxt_get_tx_ts(struct bnxt *bp, uint64_t *ts)
3373 {
3374         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3375         uint32_t fifo;
3376
3377         fifo = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3378                                 ptp->tx_mapped_regs[BNXT_PTP_TX_FIFO]));
3379         if (fifo & BNXT_PTP_TX_FIFO_EMPTY)
3380                 return -EAGAIN;
3381
3382         fifo = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3383                                 ptp->tx_mapped_regs[BNXT_PTP_TX_FIFO]));
3384         *ts = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3385                                 ptp->tx_mapped_regs[BNXT_PTP_TX_TS_L]));
3386         *ts |= (uint64_t)rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3387                                 ptp->tx_mapped_regs[BNXT_PTP_TX_TS_H])) << 32;
3388         rte_read32((uint8_t *)bp->bar0 + ptp->tx_mapped_regs[BNXT_PTP_TX_SEQ]);
3389
3390         return 0;
3391 }
3392
3393 static int bnxt_clr_rx_ts(struct bnxt *bp, uint64_t *last_ts)
3394 {
3395         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3396         struct bnxt_pf_info *pf = bp->pf;
3397         uint16_t port_id;
3398         int i = 0;
3399         uint32_t fifo;
3400
3401         if (!ptp || (bp->flags & BNXT_FLAG_CHIP_P5))
3402                 return -EINVAL;
3403
3404         port_id = pf->port_id;
3405         fifo = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3406                                 ptp->rx_mapped_regs[BNXT_PTP_RX_FIFO]));
3407         while ((fifo & BNXT_PTP_RX_FIFO_PENDING) && (i < BNXT_PTP_RX_PND_CNT)) {
3408                 rte_write32(1 << port_id, (uint8_t *)bp->bar0 +
3409                             ptp->rx_mapped_regs[BNXT_PTP_RX_FIFO_ADV]);
3410                 fifo = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3411                                         ptp->rx_mapped_regs[BNXT_PTP_RX_FIFO]));
3412                 *last_ts = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3413                                         ptp->rx_mapped_regs[BNXT_PTP_RX_TS_L]));
3414                 *last_ts |= (uint64_t)rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3415                                         ptp->rx_mapped_regs[BNXT_PTP_RX_TS_H])) << 32;
3416                 i++;
3417         }
3418
3419         if (i >= BNXT_PTP_RX_PND_CNT)
3420                 return -EBUSY;
3421
3422         return 0;
3423 }
3424
3425 static int bnxt_get_rx_ts(struct bnxt *bp, uint64_t *ts)
3426 {
3427         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3428         struct bnxt_pf_info *pf = bp->pf;
3429         uint16_t port_id;
3430         uint32_t fifo;
3431
3432         fifo = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3433                                 ptp->rx_mapped_regs[BNXT_PTP_RX_FIFO]));
3434         if (!(fifo & BNXT_PTP_RX_FIFO_PENDING))
3435                 return -EAGAIN;
3436
3437         port_id = pf->port_id;
3438         rte_write32(1 << port_id, (uint8_t *)bp->bar0 +
3439                ptp->rx_mapped_regs[BNXT_PTP_RX_FIFO_ADV]);
3440
3441         fifo = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3442                                    ptp->rx_mapped_regs[BNXT_PTP_RX_FIFO]));
3443         if (fifo & BNXT_PTP_RX_FIFO_PENDING)
3444                 return bnxt_clr_rx_ts(bp, ts);
3445
3446         *ts = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3447                                 ptp->rx_mapped_regs[BNXT_PTP_RX_TS_L]));
3448         *ts |= (uint64_t)rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
3449                                 ptp->rx_mapped_regs[BNXT_PTP_RX_TS_H])) << 32;
3450
3451         return 0;
3452 }
3453
3454 static int
3455 bnxt_timesync_write_time(struct rte_eth_dev *dev, const struct timespec *ts)
3456 {
3457         uint64_t ns;
3458         struct bnxt *bp = dev->data->dev_private;
3459         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3460
3461         if (!ptp)
3462                 return -ENOTSUP;
3463
3464         ns = rte_timespec_to_ns(ts);
3465         /* Set the timecounters to a new value. */
3466         ptp->tc.nsec = ns;
3467         ptp->tx_tstamp_tc.nsec = ns;
3468         ptp->rx_tstamp_tc.nsec = ns;
3469
3470         return 0;
3471 }
3472
3473 static int
3474 bnxt_timesync_read_time(struct rte_eth_dev *dev, struct timespec *ts)
3475 {
3476         struct bnxt *bp = dev->data->dev_private;
3477         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3478         uint64_t ns, systime_cycles = 0;
3479         int rc = 0;
3480
3481         if (!ptp)
3482                 return -ENOTSUP;
3483
3484         if (BNXT_CHIP_P5(bp))
3485                 rc = bnxt_hwrm_port_ts_query(bp, BNXT_PTP_FLAGS_CURRENT_TIME,
3486                                              &systime_cycles);
3487         else
3488                 systime_cycles = bnxt_cc_read(bp);
3489
3490         ns = rte_timecounter_update(&ptp->tc, systime_cycles);
3491         *ts = rte_ns_to_timespec(ns);
3492
3493         return rc;
3494 }
3495 static int
3496 bnxt_timesync_enable(struct rte_eth_dev *dev)
3497 {
3498         struct bnxt *bp = dev->data->dev_private;
3499         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3500         uint32_t shift = 0;
3501         int rc;
3502
3503         if (!ptp)
3504                 return -ENOTSUP;
3505
3506         ptp->rx_filter = 1;
3507         ptp->tx_tstamp_en = 1;
3508         ptp->rxctl = BNXT_PTP_MSG_EVENTS;
3509
3510         rc = bnxt_hwrm_ptp_cfg(bp);
3511         if (rc)
3512                 return rc;
3513
3514         memset(&ptp->tc, 0, sizeof(struct rte_timecounter));
3515         memset(&ptp->rx_tstamp_tc, 0, sizeof(struct rte_timecounter));
3516         memset(&ptp->tx_tstamp_tc, 0, sizeof(struct rte_timecounter));
3517
3518         ptp->tc.cc_mask = BNXT_CYCLECOUNTER_MASK;
3519         ptp->tc.cc_shift = shift;
3520         ptp->tc.nsec_mask = (1ULL << shift) - 1;
3521
3522         ptp->rx_tstamp_tc.cc_mask = BNXT_CYCLECOUNTER_MASK;
3523         ptp->rx_tstamp_tc.cc_shift = shift;
3524         ptp->rx_tstamp_tc.nsec_mask = (1ULL << shift) - 1;
3525
3526         ptp->tx_tstamp_tc.cc_mask = BNXT_CYCLECOUNTER_MASK;
3527         ptp->tx_tstamp_tc.cc_shift = shift;
3528         ptp->tx_tstamp_tc.nsec_mask = (1ULL << shift) - 1;
3529
3530         if (!BNXT_CHIP_P5(bp))
3531                 bnxt_map_ptp_regs(bp);
3532         else
3533                 rc = bnxt_ptp_start(bp);
3534
3535         return rc;
3536 }
3537
3538 static int
3539 bnxt_timesync_disable(struct rte_eth_dev *dev)
3540 {
3541         struct bnxt *bp = dev->data->dev_private;
3542         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3543
3544         if (!ptp)
3545                 return -ENOTSUP;
3546
3547         ptp->rx_filter = 0;
3548         ptp->tx_tstamp_en = 0;
3549         ptp->rxctl = 0;
3550
3551         bnxt_hwrm_ptp_cfg(bp);
3552
3553         if (!BNXT_CHIP_P5(bp))
3554                 bnxt_unmap_ptp_regs(bp);
3555         else
3556                 bnxt_ptp_stop(bp);
3557
3558         return 0;
3559 }
3560
3561 static int
3562 bnxt_timesync_read_rx_timestamp(struct rte_eth_dev *dev,
3563                                  struct timespec *timestamp,
3564                                  uint32_t flags __rte_unused)
3565 {
3566         struct bnxt *bp = dev->data->dev_private;
3567         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3568         uint64_t rx_tstamp_cycles = 0;
3569         uint64_t ns;
3570
3571         if (!ptp)
3572                 return -ENOTSUP;
3573
3574         if (BNXT_CHIP_P5(bp))
3575                 rx_tstamp_cycles = ptp->rx_timestamp;
3576         else
3577                 bnxt_get_rx_ts(bp, &rx_tstamp_cycles);
3578
3579         ns = rte_timecounter_update(&ptp->rx_tstamp_tc, rx_tstamp_cycles);
3580         *timestamp = rte_ns_to_timespec(ns);
3581         return  0;
3582 }
3583
3584 static int
3585 bnxt_timesync_read_tx_timestamp(struct rte_eth_dev *dev,
3586                                  struct timespec *timestamp)
3587 {
3588         struct bnxt *bp = dev->data->dev_private;
3589         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3590         uint64_t tx_tstamp_cycles = 0;
3591         uint64_t ns;
3592         int rc = 0;
3593
3594         if (!ptp)
3595                 return -ENOTSUP;
3596
3597         if (BNXT_CHIP_P5(bp))
3598                 rc = bnxt_hwrm_port_ts_query(bp, BNXT_PTP_FLAGS_PATH_TX,
3599                                              &tx_tstamp_cycles);
3600         else
3601                 rc = bnxt_get_tx_ts(bp, &tx_tstamp_cycles);
3602
3603         ns = rte_timecounter_update(&ptp->tx_tstamp_tc, tx_tstamp_cycles);
3604         *timestamp = rte_ns_to_timespec(ns);
3605
3606         return rc;
3607 }
3608
3609 static int
3610 bnxt_timesync_adjust_time(struct rte_eth_dev *dev, int64_t delta)
3611 {
3612         struct bnxt *bp = dev->data->dev_private;
3613         struct bnxt_ptp_cfg *ptp = bp->ptp_cfg;
3614
3615         if (!ptp)
3616                 return -ENOTSUP;
3617
3618         ptp->tc.nsec += delta;
3619         ptp->tx_tstamp_tc.nsec += delta;
3620         ptp->rx_tstamp_tc.nsec += delta;
3621
3622         return 0;
3623 }
3624
3625 static int
3626 bnxt_get_eeprom_length_op(struct rte_eth_dev *dev)
3627 {
3628         struct bnxt *bp = dev->data->dev_private;
3629         int rc;
3630         uint32_t dir_entries;
3631         uint32_t entry_length;
3632
3633         rc = is_bnxt_in_error(bp);
3634         if (rc)
3635                 return rc;
3636
3637         PMD_DRV_LOG(INFO, PCI_PRI_FMT "\n",
3638                     bp->pdev->addr.domain, bp->pdev->addr.bus,
3639                     bp->pdev->addr.devid, bp->pdev->addr.function);
3640
3641         rc = bnxt_hwrm_nvm_get_dir_info(bp, &dir_entries, &entry_length);
3642         if (rc != 0)
3643                 return rc;
3644
3645         return dir_entries * entry_length;
3646 }
3647
3648 static int
3649 bnxt_get_eeprom_op(struct rte_eth_dev *dev,
3650                 struct rte_dev_eeprom_info *in_eeprom)
3651 {
3652         struct bnxt *bp = dev->data->dev_private;
3653         uint32_t index;
3654         uint32_t offset;
3655         int rc;
3656
3657         rc = is_bnxt_in_error(bp);
3658         if (rc)
3659                 return rc;
3660
3661         PMD_DRV_LOG(INFO, PCI_PRI_FMT " in_eeprom->offset = %d len = %d\n",
3662                     bp->pdev->addr.domain, bp->pdev->addr.bus,
3663                     bp->pdev->addr.devid, bp->pdev->addr.function,
3664                     in_eeprom->offset, in_eeprom->length);
3665
3666         if (in_eeprom->offset == 0) /* special offset value to get directory */
3667                 return bnxt_get_nvram_directory(bp, in_eeprom->length,
3668                                                 in_eeprom->data);
3669
3670         index = in_eeprom->offset >> 24;
3671         offset = in_eeprom->offset & 0xffffff;
3672
3673         if (index != 0)
3674                 return bnxt_hwrm_get_nvram_item(bp, index - 1, offset,
3675                                            in_eeprom->length, in_eeprom->data);
3676
3677         return 0;
3678 }
3679
3680 static bool bnxt_dir_type_is_ape_bin_format(uint16_t dir_type)
3681 {
3682         switch (dir_type) {
3683         case BNX_DIR_TYPE_CHIMP_PATCH:
3684         case BNX_DIR_TYPE_BOOTCODE:
3685         case BNX_DIR_TYPE_BOOTCODE_2:
3686         case BNX_DIR_TYPE_APE_FW:
3687         case BNX_DIR_TYPE_APE_PATCH:
3688         case BNX_DIR_TYPE_KONG_FW:
3689         case BNX_DIR_TYPE_KONG_PATCH:
3690         case BNX_DIR_TYPE_BONO_FW:
3691         case BNX_DIR_TYPE_BONO_PATCH:
3692                 /* FALLTHROUGH */
3693                 return true;
3694         }
3695
3696         return false;
3697 }
3698
3699 static bool bnxt_dir_type_is_other_exec_format(uint16_t dir_type)
3700 {
3701         switch (dir_type) {
3702         case BNX_DIR_TYPE_AVS:
3703         case BNX_DIR_TYPE_EXP_ROM_MBA:
3704         case BNX_DIR_TYPE_PCIE:
3705         case BNX_DIR_TYPE_TSCF_UCODE:
3706         case BNX_DIR_TYPE_EXT_PHY:
3707         case BNX_DIR_TYPE_CCM:
3708         case BNX_DIR_TYPE_ISCSI_BOOT:
3709         case BNX_DIR_TYPE_ISCSI_BOOT_IPV6:
3710         case BNX_DIR_TYPE_ISCSI_BOOT_IPV4N6:
3711                 /* FALLTHROUGH */
3712                 return true;
3713         }
3714
3715         return false;
3716 }
3717
3718 static bool bnxt_dir_type_is_executable(uint16_t dir_type)
3719 {
3720         return bnxt_dir_type_is_ape_bin_format(dir_type) ||
3721                 bnxt_dir_type_is_other_exec_format(dir_type);
3722 }
3723
3724 static int
3725 bnxt_set_eeprom_op(struct rte_eth_dev *dev,
3726                 struct rte_dev_eeprom_info *in_eeprom)
3727 {
3728         struct bnxt *bp = dev->data->dev_private;
3729         uint8_t index, dir_op;
3730         uint16_t type, ext, ordinal, attr;
3731         int rc;
3732
3733         rc = is_bnxt_in_error(bp);
3734         if (rc)
3735                 return rc;
3736
3737         PMD_DRV_LOG(INFO, PCI_PRI_FMT " in_eeprom->offset = %d len = %d\n",
3738                     bp->pdev->addr.domain, bp->pdev->addr.bus,
3739                     bp->pdev->addr.devid, bp->pdev->addr.function,
3740                     in_eeprom->offset, in_eeprom->length);
3741
3742         if (!BNXT_PF(bp)) {
3743                 PMD_DRV_LOG(ERR, "NVM write not supported from a VF\n");
3744                 return -EINVAL;
3745         }
3746
3747         type = in_eeprom->magic >> 16;
3748
3749         if (type == 0xffff) { /* special value for directory operations */
3750                 index = in_eeprom->magic & 0xff;
3751                 dir_op = in_eeprom->magic >> 8;
3752                 if (index == 0)
3753                         return -EINVAL;
3754                 switch (dir_op) {
3755                 case 0x0e: /* erase */
3756                         if (in_eeprom->offset != ~in_eeprom->magic)
3757                                 return -EINVAL;
3758                         return bnxt_hwrm_erase_nvram_directory(bp, index - 1);
3759                 default:
3760                         return -EINVAL;
3761                 }
3762         }
3763
3764         /* Create or re-write an NVM item: */
3765         if (bnxt_dir_type_is_executable(type) == true)
3766                 return -EOPNOTSUPP;
3767         ext = in_eeprom->magic & 0xffff;
3768         ordinal = in_eeprom->offset >> 16;
3769         attr = in_eeprom->offset & 0xffff;
3770
3771         return bnxt_hwrm_flash_nvram(bp, type, ordinal, ext, attr,
3772                                      in_eeprom->data, in_eeprom->length);
3773 }
3774
3775 /*
3776  * Initialization
3777  */
3778
3779 static const struct eth_dev_ops bnxt_dev_ops = {
3780         .dev_infos_get = bnxt_dev_info_get_op,
3781         .dev_close = bnxt_dev_close_op,
3782         .dev_configure = bnxt_dev_configure_op,
3783         .dev_start = bnxt_dev_start_op,
3784         .dev_stop = bnxt_dev_stop_op,
3785         .dev_set_link_up = bnxt_dev_set_link_up_op,
3786         .dev_set_link_down = bnxt_dev_set_link_down_op,
3787         .stats_get = bnxt_stats_get_op,
3788         .stats_reset = bnxt_stats_reset_op,
3789         .rx_queue_setup = bnxt_rx_queue_setup_op,
3790         .rx_queue_release = bnxt_rx_queue_release_op,
3791         .tx_queue_setup = bnxt_tx_queue_setup_op,
3792         .tx_queue_release = bnxt_tx_queue_release_op,
3793         .rx_queue_intr_enable = bnxt_rx_queue_intr_enable_op,
3794         .rx_queue_intr_disable = bnxt_rx_queue_intr_disable_op,
3795         .reta_update = bnxt_reta_update_op,
3796         .reta_query = bnxt_reta_query_op,
3797         .rss_hash_update = bnxt_rss_hash_update_op,
3798         .rss_hash_conf_get = bnxt_rss_hash_conf_get_op,
3799         .link_update = bnxt_link_update_op,
3800         .promiscuous_enable = bnxt_promiscuous_enable_op,
3801         .promiscuous_disable = bnxt_promiscuous_disable_op,
3802         .allmulticast_enable = bnxt_allmulticast_enable_op,
3803         .allmulticast_disable = bnxt_allmulticast_disable_op,
3804         .mac_addr_add = bnxt_mac_addr_add_op,
3805         .mac_addr_remove = bnxt_mac_addr_remove_op,
3806         .flow_ctrl_get = bnxt_flow_ctrl_get_op,
3807         .flow_ctrl_set = bnxt_flow_ctrl_set_op,
3808         .udp_tunnel_port_add  = bnxt_udp_tunnel_port_add_op,
3809         .udp_tunnel_port_del  = bnxt_udp_tunnel_port_del_op,
3810         .vlan_filter_set = bnxt_vlan_filter_set_op,
3811         .vlan_offload_set = bnxt_vlan_offload_set_op,
3812         .vlan_tpid_set = bnxt_vlan_tpid_set_op,
3813         .vlan_pvid_set = bnxt_vlan_pvid_set_op,
3814         .mtu_set = bnxt_mtu_set_op,
3815         .mac_addr_set = bnxt_set_default_mac_addr_op,
3816         .xstats_get = bnxt_dev_xstats_get_op,
3817         .xstats_get_names = bnxt_dev_xstats_get_names_op,
3818         .xstats_reset = bnxt_dev_xstats_reset_op,
3819         .fw_version_get = bnxt_fw_version_get,
3820         .set_mc_addr_list = bnxt_dev_set_mc_addr_list_op,
3821         .rxq_info_get = bnxt_rxq_info_get_op,
3822         .txq_info_get = bnxt_txq_info_get_op,
3823         .rx_burst_mode_get = bnxt_rx_burst_mode_get,
3824         .tx_burst_mode_get = bnxt_tx_burst_mode_get,
3825         .dev_led_on = bnxt_dev_led_on_op,
3826         .dev_led_off = bnxt_dev_led_off_op,
3827         .rx_queue_start = bnxt_rx_queue_start,
3828         .rx_queue_stop = bnxt_rx_queue_stop,
3829         .tx_queue_start = bnxt_tx_queue_start,
3830         .tx_queue_stop = bnxt_tx_queue_stop,
3831         .flow_ops_get = bnxt_flow_ops_get_op,
3832         .dev_supported_ptypes_get = bnxt_dev_supported_ptypes_get_op,
3833         .get_eeprom_length    = bnxt_get_eeprom_length_op,
3834         .get_eeprom           = bnxt_get_eeprom_op,
3835         .set_eeprom           = bnxt_set_eeprom_op,
3836         .timesync_enable      = bnxt_timesync_enable,
3837         .timesync_disable     = bnxt_timesync_disable,
3838         .timesync_read_time   = bnxt_timesync_read_time,
3839         .timesync_write_time   = bnxt_timesync_write_time,
3840         .timesync_adjust_time = bnxt_timesync_adjust_time,
3841         .timesync_read_rx_timestamp = bnxt_timesync_read_rx_timestamp,
3842         .timesync_read_tx_timestamp = bnxt_timesync_read_tx_timestamp,
3843 };
3844
3845 static uint32_t bnxt_map_reset_regs(struct bnxt *bp, uint32_t reg)
3846 {
3847         uint32_t offset;
3848
3849         /* Only pre-map the reset GRC registers using window 3 */
3850         rte_write32(reg & 0xfffff000, (uint8_t *)bp->bar0 +
3851                     BNXT_GRCPF_REG_WINDOW_BASE_OUT + 8);
3852
3853         offset = BNXT_GRCP_WINDOW_3_BASE + (reg & 0xffc);
3854
3855         return offset;
3856 }
3857
3858 int bnxt_map_fw_health_status_regs(struct bnxt *bp)
3859 {
3860         struct bnxt_error_recovery_info *info = bp->recovery_info;
3861         uint32_t reg_base = 0xffffffff;
3862         int i;
3863
3864         /* Only pre-map the monitoring GRC registers using window 2 */
3865         for (i = 0; i < BNXT_FW_STATUS_REG_CNT; i++) {
3866                 uint32_t reg = info->status_regs[i];
3867
3868                 if (BNXT_FW_STATUS_REG_TYPE(reg) != BNXT_FW_STATUS_REG_TYPE_GRC)
3869                         continue;
3870
3871                 if (reg_base == 0xffffffff)
3872                         reg_base = reg & 0xfffff000;
3873                 if ((reg & 0xfffff000) != reg_base)
3874                         return -ERANGE;
3875
3876                 /* Use mask 0xffc as the Lower 2 bits indicates
3877                  * address space location
3878                  */
3879                 info->mapped_status_regs[i] = BNXT_GRCP_WINDOW_2_BASE +
3880                                                 (reg & 0xffc);
3881         }
3882
3883         if (reg_base == 0xffffffff)
3884                 return 0;
3885
3886         rte_write32(reg_base, (uint8_t *)bp->bar0 +
3887                     BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
3888
3889         return 0;
3890 }
3891
3892 static void bnxt_write_fw_reset_reg(struct bnxt *bp, uint32_t index)
3893 {
3894         struct bnxt_error_recovery_info *info = bp->recovery_info;
3895         uint32_t delay = info->delay_after_reset[index];
3896         uint32_t val = info->reset_reg_val[index];
3897         uint32_t reg = info->reset_reg[index];
3898         uint32_t type, offset;
3899         int ret;
3900
3901         type = BNXT_FW_STATUS_REG_TYPE(reg);
3902         offset = BNXT_FW_STATUS_REG_OFF(reg);
3903
3904         switch (type) {
3905         case BNXT_FW_STATUS_REG_TYPE_CFG:
3906                 ret = rte_pci_write_config(bp->pdev, &val, sizeof(val), offset);
3907                 if (ret < 0) {
3908                         PMD_DRV_LOG(ERR, "Failed to write %#x at PCI offset %#x",
3909                                     val, offset);
3910                         return;
3911                 }
3912                 break;
3913         case BNXT_FW_STATUS_REG_TYPE_GRC:
3914                 offset = bnxt_map_reset_regs(bp, offset);
3915                 rte_write32(val, (uint8_t *)bp->bar0 + offset);
3916                 break;
3917         case BNXT_FW_STATUS_REG_TYPE_BAR0:
3918                 rte_write32(val, (uint8_t *)bp->bar0 + offset);
3919                 break;
3920         }
3921         /* wait on a specific interval of time until core reset is complete */
3922         if (delay)
3923                 rte_delay_ms(delay);
3924 }
3925
3926 static void bnxt_dev_cleanup(struct bnxt *bp)
3927 {
3928         bp->eth_dev->data->dev_link.link_status = 0;
3929         bp->link_info->link_up = 0;
3930         if (bp->eth_dev->data->dev_started)
3931                 bnxt_dev_stop(bp->eth_dev);
3932
3933         bnxt_uninit_resources(bp, true);
3934 }
3935
3936 static int
3937 bnxt_check_fw_reset_done(struct bnxt *bp)
3938 {
3939         int timeout = bp->fw_reset_max_msecs;
3940         uint16_t val = 0;
3941         int rc;
3942
3943         do {
3944                 rc = rte_pci_read_config(bp->pdev, &val, sizeof(val), PCI_SUBSYSTEM_ID_OFFSET);
3945                 if (rc < 0) {
3946                         PMD_DRV_LOG(ERR, "Failed to read PCI offset 0x%x", PCI_SUBSYSTEM_ID_OFFSET);
3947                         return rc;
3948                 }
3949                 if (val != 0xffff)
3950                         break;
3951                 rte_delay_ms(1);
3952         } while (timeout--);
3953
3954         if (val == 0xffff) {
3955                 PMD_DRV_LOG(ERR, "Firmware reset aborted, PCI config space invalid\n");
3956                 return -1;
3957         }
3958
3959         return 0;
3960 }
3961
3962 static int bnxt_restore_vlan_filters(struct bnxt *bp)
3963 {
3964         struct rte_eth_dev *dev = bp->eth_dev;
3965         struct rte_vlan_filter_conf *vfc;
3966         int vidx, vbit, rc;
3967         uint16_t vlan_id;
3968
3969         for (vlan_id = 1; vlan_id <= RTE_ETHER_MAX_VLAN_ID; vlan_id++) {
3970                 vfc = &dev->data->vlan_filter_conf;
3971                 vidx = vlan_id / 64;
3972                 vbit = vlan_id % 64;
3973
3974                 /* Each bit corresponds to a VLAN id */
3975                 if (vfc->ids[vidx] & (UINT64_C(1) << vbit)) {
3976                         rc = bnxt_add_vlan_filter(bp, vlan_id);
3977                         if (rc)
3978                                 return rc;
3979                 }
3980         }
3981
3982         return 0;
3983 }
3984
3985 static int bnxt_restore_mac_filters(struct bnxt *bp)
3986 {
3987         struct rte_eth_dev *dev = bp->eth_dev;
3988         struct rte_eth_dev_info dev_info;
3989         struct rte_ether_addr *addr;
3990         uint64_t pool_mask;
3991         uint32_t pool = 0;
3992         uint16_t i;
3993         int rc;
3994
3995         if (BNXT_VF(bp) && !BNXT_VF_IS_TRUSTED(bp))
3996                 return 0;
3997
3998         rc = bnxt_dev_info_get_op(dev, &dev_info);
3999         if (rc)
4000                 return rc;
4001
4002         /* replay MAC address configuration */
4003         for (i = 1; i < dev_info.max_mac_addrs; i++) {
4004                 addr = &dev->data->mac_addrs[i];
4005
4006                 /* skip zero address */
4007                 if (rte_is_zero_ether_addr(addr))
4008                         continue;
4009
4010                 pool = 0;
4011                 pool_mask = dev->data->mac_pool_sel[i];
4012
4013                 do {
4014                         if (pool_mask & 1ULL) {
4015                                 rc = bnxt_mac_addr_add_op(dev, addr, i, pool);
4016                                 if (rc)
4017                                         return rc;
4018                         }
4019                         pool_mask >>= 1;
4020                         pool++;
4021                 } while (pool_mask);
4022         }
4023
4024         return 0;
4025 }
4026
4027 static int bnxt_restore_filters(struct bnxt *bp)
4028 {
4029         struct rte_eth_dev *dev = bp->eth_dev;
4030         int ret = 0;
4031
4032         if (dev->data->all_multicast) {
4033                 ret = bnxt_allmulticast_enable_op(dev);
4034                 if (ret)
4035                         return ret;
4036         }
4037         if (dev->data->promiscuous) {
4038                 ret = bnxt_promiscuous_enable_op(dev);
4039                 if (ret)
4040                         return ret;
4041         }
4042
4043         ret = bnxt_restore_mac_filters(bp);
4044         if (ret)
4045                 return ret;
4046
4047         ret = bnxt_restore_vlan_filters(bp);
4048         /* TODO restore other filters as well */
4049         return ret;
4050 }
4051
4052 static int bnxt_check_fw_ready(struct bnxt *bp)
4053 {
4054         int timeout = bp->fw_reset_max_msecs;
4055         int rc = 0;
4056
4057         do {
4058                 rc = bnxt_hwrm_poll_ver_get(bp);
4059                 if (rc == 0)
4060                         break;
4061                 rte_delay_ms(BNXT_FW_READY_WAIT_INTERVAL);
4062                 timeout -= BNXT_FW_READY_WAIT_INTERVAL;
4063         } while (rc && timeout > 0);
4064
4065         if (rc)
4066                 PMD_DRV_LOG(ERR, "FW is not Ready after reset\n");
4067
4068         return rc;
4069 }
4070
4071 static void bnxt_dev_recover(void *arg)
4072 {
4073         struct bnxt *bp = arg;
4074         int rc = 0;
4075
4076         pthread_mutex_lock(&bp->err_recovery_lock);
4077
4078         if (!bp->fw_reset_min_msecs) {
4079                 rc = bnxt_check_fw_reset_done(bp);
4080                 if (rc)
4081                         goto err;
4082         }
4083
4084         /* Clear Error flag so that device re-init should happen */
4085         bp->flags &= ~BNXT_FLAG_FATAL_ERROR;
4086
4087         rc = bnxt_check_fw_ready(bp);
4088         if (rc)
4089                 goto err;
4090
4091         rc = bnxt_init_resources(bp, true);
4092         if (rc) {
4093                 PMD_DRV_LOG(ERR,
4094                             "Failed to initialize resources after reset\n");
4095                 goto err;
4096         }
4097         /* clear reset flag as the device is initialized now */
4098         bp->flags &= ~BNXT_FLAG_FW_RESET;
4099
4100         rc = bnxt_dev_start_op(bp->eth_dev);
4101         if (rc) {
4102                 PMD_DRV_LOG(ERR, "Failed to start port after reset\n");
4103                 goto err_start;
4104         }
4105
4106         rc = bnxt_restore_filters(bp);
4107         if (rc)
4108                 goto err_start;
4109
4110         PMD_DRV_LOG(INFO, "Recovered from FW reset\n");
4111         pthread_mutex_unlock(&bp->err_recovery_lock);
4112
4113         return;
4114 err_start:
4115         bnxt_dev_stop(bp->eth_dev);
4116 err:
4117         bp->flags |= BNXT_FLAG_FATAL_ERROR;
4118         bnxt_uninit_resources(bp, false);
4119         pthread_mutex_unlock(&bp->err_recovery_lock);
4120         PMD_DRV_LOG(ERR, "Failed to recover from FW reset\n");
4121 }
4122
4123 void bnxt_dev_reset_and_resume(void *arg)
4124 {
4125         struct bnxt *bp = arg;
4126         uint32_t us = US_PER_MS * bp->fw_reset_min_msecs;
4127         uint16_t val = 0;
4128         int rc;
4129
4130         bnxt_dev_cleanup(bp);
4131
4132         bnxt_wait_for_device_shutdown(bp);
4133
4134         /* During some fatal firmware error conditions, the PCI config space
4135          * register 0x2e which normally contains the subsystem ID will become
4136          * 0xffff. This register will revert back to the normal value after
4137          * the chip has completed core reset. If we detect this condition,
4138          * we can poll this config register immediately for the value to revert.
4139          */
4140         if (bp->flags & BNXT_FLAG_FATAL_ERROR) {
4141                 rc = rte_pci_read_config(bp->pdev, &val, sizeof(val), PCI_SUBSYSTEM_ID_OFFSET);
4142                 if (rc < 0) {
4143                         PMD_DRV_LOG(ERR, "Failed to read PCI offset 0x%x", PCI_SUBSYSTEM_ID_OFFSET);
4144                         return;
4145                 }
4146                 if (val == 0xffff) {
4147                         bp->fw_reset_min_msecs = 0;
4148                         us = 1;
4149                 }
4150         }
4151
4152         rc = rte_eal_alarm_set(us, bnxt_dev_recover, (void *)bp);
4153         if (rc)
4154                 PMD_DRV_LOG(ERR, "Error setting recovery alarm");
4155 }
4156
4157 uint32_t bnxt_read_fw_status_reg(struct bnxt *bp, uint32_t index)
4158 {
4159         struct bnxt_error_recovery_info *info = bp->recovery_info;
4160         uint32_t reg = info->status_regs[index];
4161         uint32_t type, offset, val = 0;
4162
4163         type = BNXT_FW_STATUS_REG_TYPE(reg);
4164         offset = BNXT_FW_STATUS_REG_OFF(reg);
4165
4166         switch (type) {
4167         case BNXT_FW_STATUS_REG_TYPE_CFG:
4168                 rte_pci_read_config(bp->pdev, &val, sizeof(val), offset);
4169                 break;
4170         case BNXT_FW_STATUS_REG_TYPE_GRC:
4171                 offset = info->mapped_status_regs[index];
4172                 /* FALLTHROUGH */
4173         case BNXT_FW_STATUS_REG_TYPE_BAR0:
4174                 val = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
4175                                        offset));
4176                 break;
4177         }
4178
4179         return val;
4180 }
4181
4182 static int bnxt_fw_reset_all(struct bnxt *bp)
4183 {
4184         struct bnxt_error_recovery_info *info = bp->recovery_info;
4185         uint32_t i;
4186         int rc = 0;
4187
4188         if (info->flags & BNXT_FLAG_ERROR_RECOVERY_HOST) {
4189                 /* Reset through master function driver */
4190                 for (i = 0; i < info->reg_array_cnt; i++)
4191                         bnxt_write_fw_reset_reg(bp, i);
4192                 /* Wait for time specified by FW after triggering reset */
4193                 rte_delay_ms(info->master_func_wait_period_after_reset);
4194         } else if (info->flags & BNXT_FLAG_ERROR_RECOVERY_CO_CPU) {
4195                 /* Reset with the help of Kong processor */
4196                 rc = bnxt_hwrm_fw_reset(bp);
4197                 if (rc)
4198                         PMD_DRV_LOG(ERR, "Failed to reset FW\n");
4199         }
4200
4201         return rc;
4202 }
4203
4204 static void bnxt_fw_reset_cb(void *arg)
4205 {
4206         struct bnxt *bp = arg;
4207         struct bnxt_error_recovery_info *info = bp->recovery_info;
4208         int rc = 0;
4209
4210         /* Only Master function can do FW reset */
4211         if (bnxt_is_master_func(bp) &&
4212             bnxt_is_recovery_enabled(bp)) {
4213                 rc = bnxt_fw_reset_all(bp);
4214                 if (rc) {
4215                         PMD_DRV_LOG(ERR, "Adapter recovery failed\n");
4216                         return;
4217                 }
4218         }
4219
4220         /* if recovery method is ERROR_RECOVERY_CO_CPU, KONG will send
4221          * EXCEPTION_FATAL_ASYNC event to all the functions
4222          * (including MASTER FUNC). After receiving this Async, all the active
4223          * drivers should treat this case as FW initiated recovery
4224          */
4225         if (info->flags & BNXT_FLAG_ERROR_RECOVERY_HOST) {
4226                 bp->fw_reset_min_msecs = BNXT_MIN_FW_READY_TIMEOUT;
4227                 bp->fw_reset_max_msecs = BNXT_MAX_FW_RESET_TIMEOUT;
4228
4229                 /* To recover from error */
4230                 rte_eal_alarm_set(US_PER_MS, bnxt_dev_reset_and_resume,
4231                                   (void *)bp);
4232         }
4233 }
4234
4235 /* Driver should poll FW heartbeat, reset_counter with the frequency
4236  * advertised by FW in HWRM_ERROR_RECOVERY_QCFG.
4237  * When the driver detects heartbeat stop or change in reset_counter,
4238  * it has to trigger a reset to recover from the error condition.
4239  * A “master PF” is the function who will have the privilege to
4240  * initiate the chimp reset. The master PF will be elected by the
4241  * firmware and will be notified through async message.
4242  */
4243 static void bnxt_check_fw_health(void *arg)
4244 {
4245         struct bnxt *bp = arg;
4246         struct bnxt_error_recovery_info *info = bp->recovery_info;
4247         uint32_t val = 0, wait_msec;
4248
4249         if (!info || !bnxt_is_recovery_enabled(bp) ||
4250             is_bnxt_in_error(bp))
4251                 return;
4252
4253         val = bnxt_read_fw_status_reg(bp, BNXT_FW_HEARTBEAT_CNT_REG);
4254         if (val == info->last_heart_beat)
4255                 goto reset;
4256
4257         info->last_heart_beat = val;
4258
4259         val = bnxt_read_fw_status_reg(bp, BNXT_FW_RECOVERY_CNT_REG);
4260         if (val != info->last_reset_counter)
4261                 goto reset;
4262
4263         info->last_reset_counter = val;
4264
4265         rte_eal_alarm_set(US_PER_MS * info->driver_polling_freq,
4266                           bnxt_check_fw_health, (void *)bp);
4267
4268         return;
4269 reset:
4270         /* Stop DMA to/from device */
4271         bp->flags |= BNXT_FLAG_FATAL_ERROR;
4272         bp->flags |= BNXT_FLAG_FW_RESET;
4273
4274         PMD_DRV_LOG(ERR, "Detected FW dead condition\n");
4275
4276         if (bnxt_is_master_func(bp))
4277                 wait_msec = info->master_func_wait_period;
4278         else
4279                 wait_msec = info->normal_func_wait_period;
4280
4281         rte_eal_alarm_set(US_PER_MS * wait_msec,
4282                           bnxt_fw_reset_cb, (void *)bp);
4283 }
4284
4285 void bnxt_schedule_fw_health_check(struct bnxt *bp)
4286 {
4287         uint32_t polling_freq;
4288
4289         pthread_mutex_lock(&bp->health_check_lock);
4290
4291         if (!bnxt_is_recovery_enabled(bp))
4292                 goto done;
4293
4294         if (bp->flags & BNXT_FLAG_FW_HEALTH_CHECK_SCHEDULED)
4295                 goto done;
4296
4297         polling_freq = bp->recovery_info->driver_polling_freq;
4298
4299         rte_eal_alarm_set(US_PER_MS * polling_freq,
4300                           bnxt_check_fw_health, (void *)bp);
4301         bp->flags |= BNXT_FLAG_FW_HEALTH_CHECK_SCHEDULED;
4302
4303 done:
4304         pthread_mutex_unlock(&bp->health_check_lock);
4305 }
4306
4307 static void bnxt_cancel_fw_health_check(struct bnxt *bp)
4308 {
4309         rte_eal_alarm_cancel(bnxt_check_fw_health, (void *)bp);
4310         bp->flags &= ~BNXT_FLAG_FW_HEALTH_CHECK_SCHEDULED;
4311 }
4312
4313 static bool bnxt_vf_pciid(uint16_t device_id)
4314 {
4315         switch (device_id) {
4316         case BROADCOM_DEV_ID_57304_VF:
4317         case BROADCOM_DEV_ID_57406_VF:
4318         case BROADCOM_DEV_ID_5731X_VF:
4319         case BROADCOM_DEV_ID_5741X_VF:
4320         case BROADCOM_DEV_ID_57414_VF:
4321         case BROADCOM_DEV_ID_STRATUS_NIC_VF1:
4322         case BROADCOM_DEV_ID_STRATUS_NIC_VF2:
4323         case BROADCOM_DEV_ID_58802_VF:
4324         case BROADCOM_DEV_ID_57500_VF1:
4325         case BROADCOM_DEV_ID_57500_VF2:
4326         case BROADCOM_DEV_ID_58818_VF:
4327                 /* FALLTHROUGH */
4328                 return true;
4329         default:
4330                 return false;
4331         }
4332 }
4333
4334 /* Phase 5 device */
4335 static bool bnxt_p5_device(uint16_t device_id)
4336 {
4337         switch (device_id) {
4338         case BROADCOM_DEV_ID_57508:
4339         case BROADCOM_DEV_ID_57504:
4340         case BROADCOM_DEV_ID_57502:
4341         case BROADCOM_DEV_ID_57508_MF1:
4342         case BROADCOM_DEV_ID_57504_MF1:
4343         case BROADCOM_DEV_ID_57502_MF1:
4344         case BROADCOM_DEV_ID_57508_MF2:
4345         case BROADCOM_DEV_ID_57504_MF2:
4346         case BROADCOM_DEV_ID_57502_MF2:
4347         case BROADCOM_DEV_ID_57500_VF1:
4348         case BROADCOM_DEV_ID_57500_VF2:
4349         case BROADCOM_DEV_ID_58812:
4350         case BROADCOM_DEV_ID_58814:
4351         case BROADCOM_DEV_ID_58818:
4352         case BROADCOM_DEV_ID_58818_VF:
4353                 /* FALLTHROUGH */
4354                 return true;
4355         default:
4356                 return false;
4357         }
4358 }
4359
4360 bool bnxt_stratus_device(struct bnxt *bp)
4361 {
4362         uint16_t device_id = bp->pdev->id.device_id;
4363
4364         switch (device_id) {
4365         case BROADCOM_DEV_ID_STRATUS_NIC:
4366         case BROADCOM_DEV_ID_STRATUS_NIC_VF1:
4367         case BROADCOM_DEV_ID_STRATUS_NIC_VF2:
4368                 /* FALLTHROUGH */
4369                 return true;
4370         default:
4371                 return false;
4372         }
4373 }
4374
4375 static int bnxt_map_pci_bars(struct rte_eth_dev *eth_dev)
4376 {
4377         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
4378         struct bnxt *bp = eth_dev->data->dev_private;
4379
4380         /* enable device (incl. PCI PM wakeup), and bus-mastering */
4381         bp->bar0 = (void *)pci_dev->mem_resource[0].addr;
4382         bp->doorbell_base = (void *)pci_dev->mem_resource[2].addr;
4383         if (!bp->bar0 || !bp->doorbell_base) {
4384                 PMD_DRV_LOG(ERR, "Unable to access Hardware\n");
4385                 return -ENODEV;
4386         }
4387
4388         bp->eth_dev = eth_dev;
4389         bp->pdev = pci_dev;
4390
4391         return 0;
4392 }
4393
4394 static int bnxt_alloc_ctx_mem_blk(struct bnxt *bp,
4395                                   struct bnxt_ctx_pg_info *ctx_pg,
4396                                   uint32_t mem_size,
4397                                   const char *suffix,
4398                                   uint16_t idx)
4399 {
4400         struct bnxt_ring_mem_info *rmem = &ctx_pg->ring_mem;
4401         const struct rte_memzone *mz = NULL;
4402         char mz_name[RTE_MEMZONE_NAMESIZE];
4403         rte_iova_t mz_phys_addr;
4404         uint64_t valid_bits = 0;
4405         uint32_t sz;
4406         int i;
4407
4408         if (!mem_size)
4409                 return 0;
4410
4411         rmem->nr_pages = RTE_ALIGN_MUL_CEIL(mem_size, BNXT_PAGE_SIZE) /
4412                          BNXT_PAGE_SIZE;
4413         rmem->page_size = BNXT_PAGE_SIZE;
4414         rmem->pg_arr = ctx_pg->ctx_pg_arr;
4415         rmem->dma_arr = ctx_pg->ctx_dma_arr;
4416         rmem->flags = BNXT_RMEM_VALID_PTE_FLAG;
4417
4418         valid_bits = PTU_PTE_VALID;
4419
4420         if (rmem->nr_pages > 1) {
4421                 snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
4422                          "bnxt_ctx_pg_tbl%s_%x_%d",
4423                          suffix, idx, bp->eth_dev->data->port_id);
4424                 mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
4425                 mz = rte_memzone_lookup(mz_name);
4426                 if (!mz) {
4427                         mz = rte_memzone_reserve_aligned(mz_name,
4428                                                 rmem->nr_pages * 8,
4429                                                 SOCKET_ID_ANY,
4430                                                 RTE_MEMZONE_2MB |
4431                                                 RTE_MEMZONE_SIZE_HINT_ONLY |
4432                                                 RTE_MEMZONE_IOVA_CONTIG,
4433                                                 BNXT_PAGE_SIZE);
4434                         if (mz == NULL)
4435                                 return -ENOMEM;
4436                 }
4437
4438                 memset(mz->addr, 0, mz->len);
4439                 mz_phys_addr = mz->iova;
4440
4441                 rmem->pg_tbl = mz->addr;
4442                 rmem->pg_tbl_map = mz_phys_addr;
4443                 rmem->pg_tbl_mz = mz;
4444         }
4445
4446         snprintf(mz_name, RTE_MEMZONE_NAMESIZE, "bnxt_ctx_%s_%x_%d",
4447                  suffix, idx, bp->eth_dev->data->port_id);
4448         mz = rte_memzone_lookup(mz_name);
4449         if (!mz) {
4450                 mz = rte_memzone_reserve_aligned(mz_name,
4451                                                  mem_size,
4452                                                  SOCKET_ID_ANY,
4453                                                  RTE_MEMZONE_1GB |
4454                                                  RTE_MEMZONE_SIZE_HINT_ONLY |
4455                                                  RTE_MEMZONE_IOVA_CONTIG,
4456                                                  BNXT_PAGE_SIZE);
4457                 if (mz == NULL)
4458                         return -ENOMEM;
4459         }
4460
4461         memset(mz->addr, 0, mz->len);
4462         mz_phys_addr = mz->iova;
4463
4464         for (sz = 0, i = 0; sz < mem_size; sz += BNXT_PAGE_SIZE, i++) {
4465                 rmem->pg_arr[i] = ((char *)mz->addr) + sz;
4466                 rmem->dma_arr[i] = mz_phys_addr + sz;
4467
4468                 if (rmem->nr_pages > 1) {
4469                         if (i == rmem->nr_pages - 2 &&
4470                             (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
4471                                 valid_bits |= PTU_PTE_NEXT_TO_LAST;
4472                         else if (i == rmem->nr_pages - 1 &&
4473                                  (rmem->flags & BNXT_RMEM_RING_PTE_FLAG))
4474                                 valid_bits |= PTU_PTE_LAST;
4475
4476                         rmem->pg_tbl[i] = rte_cpu_to_le_64(rmem->dma_arr[i] |
4477                                                            valid_bits);
4478                 }
4479         }
4480
4481         rmem->mz = mz;
4482         if (rmem->vmem_size)
4483                 rmem->vmem = (void **)mz->addr;
4484         rmem->dma_arr[0] = mz_phys_addr;
4485         return 0;
4486 }
4487
4488 static void bnxt_free_ctx_mem(struct bnxt *bp)
4489 {
4490         int i;
4491
4492         if (!bp->ctx || !(bp->ctx->flags & BNXT_CTX_FLAG_INITED))
4493                 return;
4494
4495         bp->ctx->flags &= ~BNXT_CTX_FLAG_INITED;
4496         rte_memzone_free(bp->ctx->qp_mem.ring_mem.mz);
4497         rte_memzone_free(bp->ctx->srq_mem.ring_mem.mz);
4498         rte_memzone_free(bp->ctx->cq_mem.ring_mem.mz);
4499         rte_memzone_free(bp->ctx->vnic_mem.ring_mem.mz);
4500         rte_memzone_free(bp->ctx->stat_mem.ring_mem.mz);
4501         rte_memzone_free(bp->ctx->qp_mem.ring_mem.pg_tbl_mz);
4502         rte_memzone_free(bp->ctx->srq_mem.ring_mem.pg_tbl_mz);
4503         rte_memzone_free(bp->ctx->cq_mem.ring_mem.pg_tbl_mz);
4504         rte_memzone_free(bp->ctx->vnic_mem.ring_mem.pg_tbl_mz);
4505         rte_memzone_free(bp->ctx->stat_mem.ring_mem.pg_tbl_mz);
4506
4507         for (i = 0; i < bp->ctx->tqm_fp_rings_count + 1; i++) {
4508                 if (bp->ctx->tqm_mem[i])
4509                         rte_memzone_free(bp->ctx->tqm_mem[i]->ring_mem.mz);
4510         }
4511
4512         rte_free(bp->ctx);
4513         bp->ctx = NULL;
4514 }
4515
4516 #define bnxt_roundup(x, y)   ((((x) + ((y) - 1)) / (y)) * (y))
4517
4518 #define min_t(type, x, y) ({                    \
4519         type __min1 = (x);                      \
4520         type __min2 = (y);                      \
4521         __min1 < __min2 ? __min1 : __min2; })
4522
4523 #define max_t(type, x, y) ({                    \
4524         type __max1 = (x);                      \
4525         type __max2 = (y);                      \
4526         __max1 > __max2 ? __max1 : __max2; })
4527
4528 #define clamp_t(type, _x, min, max)     min_t(type, max_t(type, _x, min), max)
4529
4530 int bnxt_alloc_ctx_mem(struct bnxt *bp)
4531 {
4532         struct bnxt_ctx_pg_info *ctx_pg;
4533         struct bnxt_ctx_mem_info *ctx;
4534         uint32_t mem_size, ena, entries;
4535         uint32_t entries_sp, min;
4536         int i, rc;
4537
4538         rc = bnxt_hwrm_func_backing_store_qcaps(bp);
4539         if (rc) {
4540                 PMD_DRV_LOG(ERR, "Query context mem capability failed\n");
4541                 return rc;
4542         }
4543         ctx = bp->ctx;
4544         if (!ctx || (ctx->flags & BNXT_CTX_FLAG_INITED))
4545                 return 0;
4546
4547         ctx_pg = &ctx->qp_mem;
4548         ctx_pg->entries = ctx->qp_min_qp1_entries + ctx->qp_max_l2_entries;
4549         if (ctx->qp_entry_size) {
4550                 mem_size = ctx->qp_entry_size * ctx_pg->entries;
4551                 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size, "qp_mem", 0);
4552                 if (rc)
4553                         return rc;
4554         }
4555
4556         ctx_pg = &ctx->srq_mem;
4557         ctx_pg->entries = ctx->srq_max_l2_entries;
4558         if (ctx->srq_entry_size) {
4559                 mem_size = ctx->srq_entry_size * ctx_pg->entries;
4560                 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size, "srq_mem", 0);
4561                 if (rc)
4562                         return rc;
4563         }
4564
4565         ctx_pg = &ctx->cq_mem;
4566         ctx_pg->entries = ctx->cq_max_l2_entries;
4567         if (ctx->cq_entry_size) {
4568                 mem_size = ctx->cq_entry_size * ctx_pg->entries;
4569                 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size, "cq_mem", 0);
4570                 if (rc)
4571                         return rc;
4572         }
4573
4574         ctx_pg = &ctx->vnic_mem;
4575         ctx_pg->entries = ctx->vnic_max_vnic_entries +
4576                 ctx->vnic_max_ring_table_entries;
4577         if (ctx->vnic_entry_size) {
4578                 mem_size = ctx->vnic_entry_size * ctx_pg->entries;
4579                 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size, "vnic_mem", 0);
4580                 if (rc)
4581                         return rc;
4582         }
4583
4584         ctx_pg = &ctx->stat_mem;
4585         ctx_pg->entries = ctx->stat_max_entries;
4586         if (ctx->stat_entry_size) {
4587                 mem_size = ctx->stat_entry_size * ctx_pg->entries;
4588                 rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size, "stat_mem", 0);
4589                 if (rc)
4590                         return rc;
4591         }
4592
4593         min = ctx->tqm_min_entries_per_ring;
4594
4595         entries_sp = ctx->qp_max_l2_entries +
4596                      ctx->vnic_max_vnic_entries +
4597                      2 * ctx->qp_min_qp1_entries + min;
4598         entries_sp = bnxt_roundup(entries_sp, ctx->tqm_entries_multiple);
4599
4600         entries = ctx->qp_max_l2_entries + ctx->qp_min_qp1_entries;
4601         entries = bnxt_roundup(entries, ctx->tqm_entries_multiple);
4602         entries = clamp_t(uint32_t, entries, min,
4603                           ctx->tqm_max_entries_per_ring);
4604         for (i = 0, ena = 0; i < ctx->tqm_fp_rings_count + 1; i++) {
4605                 /* i=0 is for TQM_SP. i=1 to i=8 applies to RING0 to RING7.
4606                  * i > 8 is other ext rings.
4607                  */
4608                 ctx_pg = ctx->tqm_mem[i];
4609                 ctx_pg->entries = i ? entries : entries_sp;
4610                 if (ctx->tqm_entry_size) {
4611                         mem_size = ctx->tqm_entry_size * ctx_pg->entries;
4612                         rc = bnxt_alloc_ctx_mem_blk(bp, ctx_pg, mem_size,
4613                                                     "tqm_mem", i);
4614                         if (rc)
4615                                 return rc;
4616                 }
4617                 if (i < BNXT_MAX_TQM_LEGACY_RINGS)
4618                         ena |= HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_TQM_SP << i;
4619                 else
4620                         ena |= HWRM_FUNC_BACKING_STORE_CFG_INPUT_ENABLES_TQM_RING8;
4621         }
4622
4623         ena |= FUNC_BACKING_STORE_CFG_INPUT_DFLT_ENABLES;
4624         rc = bnxt_hwrm_func_backing_store_cfg(bp, ena);
4625         if (rc)
4626                 PMD_DRV_LOG(ERR,
4627                             "Failed to configure context mem: rc = %d\n", rc);
4628         else
4629                 ctx->flags |= BNXT_CTX_FLAG_INITED;
4630
4631         return rc;
4632 }
4633
4634 static int bnxt_alloc_stats_mem(struct bnxt *bp)
4635 {
4636         struct rte_pci_device *pci_dev = bp->pdev;
4637         char mz_name[RTE_MEMZONE_NAMESIZE];
4638         const struct rte_memzone *mz = NULL;
4639         uint32_t total_alloc_len;
4640         rte_iova_t mz_phys_addr;
4641
4642         if (pci_dev->id.device_id == BROADCOM_DEV_ID_NS2)
4643                 return 0;
4644
4645         snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
4646                  "bnxt_" PCI_PRI_FMT "-%s", pci_dev->addr.domain,
4647                  pci_dev->addr.bus, pci_dev->addr.devid,
4648                  pci_dev->addr.function, "rx_port_stats");
4649         mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
4650         mz = rte_memzone_lookup(mz_name);
4651         total_alloc_len =
4652                 RTE_CACHE_LINE_ROUNDUP(sizeof(struct rx_port_stats) +
4653                                        sizeof(struct rx_port_stats_ext) + 512);
4654         if (!mz) {
4655                 mz = rte_memzone_reserve(mz_name, total_alloc_len,
4656                                          SOCKET_ID_ANY,
4657                                          RTE_MEMZONE_2MB |
4658                                          RTE_MEMZONE_SIZE_HINT_ONLY |
4659                                          RTE_MEMZONE_IOVA_CONTIG);
4660                 if (mz == NULL)
4661                         return -ENOMEM;
4662         }
4663         memset(mz->addr, 0, mz->len);
4664         mz_phys_addr = mz->iova;
4665
4666         bp->rx_mem_zone = (const void *)mz;
4667         bp->hw_rx_port_stats = mz->addr;
4668         bp->hw_rx_port_stats_map = mz_phys_addr;
4669
4670         snprintf(mz_name, RTE_MEMZONE_NAMESIZE,
4671                  "bnxt_" PCI_PRI_FMT "-%s", pci_dev->addr.domain,
4672                  pci_dev->addr.bus, pci_dev->addr.devid,
4673                  pci_dev->addr.function, "tx_port_stats");
4674         mz_name[RTE_MEMZONE_NAMESIZE - 1] = 0;
4675         mz = rte_memzone_lookup(mz_name);
4676         total_alloc_len =
4677                 RTE_CACHE_LINE_ROUNDUP(sizeof(struct tx_port_stats) +
4678                                        sizeof(struct tx_port_stats_ext) + 512);
4679         if (!mz) {
4680                 mz = rte_memzone_reserve(mz_name,
4681                                          total_alloc_len,
4682                                          SOCKET_ID_ANY,
4683                                          RTE_MEMZONE_2MB |
4684                                          RTE_MEMZONE_SIZE_HINT_ONLY |
4685                                          RTE_MEMZONE_IOVA_CONTIG);
4686                 if (mz == NULL)
4687                         return -ENOMEM;
4688         }
4689         memset(mz->addr, 0, mz->len);
4690         mz_phys_addr = mz->iova;
4691
4692         bp->tx_mem_zone = (const void *)mz;
4693         bp->hw_tx_port_stats = mz->addr;
4694         bp->hw_tx_port_stats_map = mz_phys_addr;
4695         bp->flags |= BNXT_FLAG_PORT_STATS;
4696
4697         /* Display extended statistics if FW supports it */
4698         if (bp->hwrm_spec_code < HWRM_SPEC_CODE_1_8_4 ||
4699             bp->hwrm_spec_code == HWRM_SPEC_CODE_1_9_0 ||
4700             !(bp->flags & BNXT_FLAG_EXT_STATS_SUPPORTED))
4701                 return 0;
4702
4703         bp->hw_rx_port_stats_ext = (void *)
4704                 ((uint8_t *)bp->hw_rx_port_stats +
4705                  sizeof(struct rx_port_stats));
4706         bp->hw_rx_port_stats_ext_map = bp->hw_rx_port_stats_map +
4707                 sizeof(struct rx_port_stats);
4708         bp->flags |= BNXT_FLAG_EXT_RX_PORT_STATS;
4709
4710         if (bp->hwrm_spec_code < HWRM_SPEC_CODE_1_9_2 ||
4711             bp->flags & BNXT_FLAG_EXT_STATS_SUPPORTED) {
4712                 bp->hw_tx_port_stats_ext = (void *)
4713                         ((uint8_t *)bp->hw_tx_port_stats +
4714                          sizeof(struct tx_port_stats));
4715                 bp->hw_tx_port_stats_ext_map =
4716                         bp->hw_tx_port_stats_map +
4717                         sizeof(struct tx_port_stats);
4718                 bp->flags |= BNXT_FLAG_EXT_TX_PORT_STATS;
4719         }
4720
4721         return 0;
4722 }
4723
4724 static int bnxt_setup_mac_addr(struct rte_eth_dev *eth_dev)
4725 {
4726         struct bnxt *bp = eth_dev->data->dev_private;
4727         int rc = 0;
4728
4729         eth_dev->data->mac_addrs = rte_zmalloc("bnxt_mac_addr_tbl",
4730                                                RTE_ETHER_ADDR_LEN *
4731                                                bp->max_l2_ctx,
4732                                                0);
4733         if (eth_dev->data->mac_addrs == NULL) {
4734                 PMD_DRV_LOG(ERR, "Failed to alloc MAC addr tbl\n");
4735                 return -ENOMEM;
4736         }
4737
4738         if (!BNXT_HAS_DFLT_MAC_SET(bp)) {
4739                 if (BNXT_PF(bp))
4740                         return -EINVAL;
4741
4742                 /* Generate a random MAC address, if none was assigned by PF */
4743                 PMD_DRV_LOG(INFO, "VF MAC address not assigned by Host PF\n");
4744                 bnxt_eth_hw_addr_random(bp->mac_addr);
4745                 PMD_DRV_LOG(INFO,
4746                             "Assign random MAC:%02X:%02X:%02X:%02X:%02X:%02X\n",
4747                             bp->mac_addr[0], bp->mac_addr[1], bp->mac_addr[2],
4748                             bp->mac_addr[3], bp->mac_addr[4], bp->mac_addr[5]);
4749
4750                 rc = bnxt_hwrm_set_mac(bp);
4751                 if (rc)
4752                         return rc;
4753         }
4754
4755         /* Copy the permanent MAC from the FUNC_QCAPS response */
4756         memcpy(&eth_dev->data->mac_addrs[0], bp->mac_addr, RTE_ETHER_ADDR_LEN);
4757
4758         return rc;
4759 }
4760
4761 static int bnxt_restore_dflt_mac(struct bnxt *bp)
4762 {
4763         int rc = 0;
4764
4765         /* MAC is already configured in FW */
4766         if (BNXT_HAS_DFLT_MAC_SET(bp))
4767                 return 0;
4768
4769         /* Restore the old MAC configured */
4770         rc = bnxt_hwrm_set_mac(bp);
4771         if (rc)
4772                 PMD_DRV_LOG(ERR, "Failed to restore MAC address\n");
4773
4774         return rc;
4775 }
4776
4777 static void bnxt_config_vf_req_fwd(struct bnxt *bp)
4778 {
4779         if (!BNXT_PF(bp))
4780                 return;
4781
4782         memset(bp->pf->vf_req_fwd, 0, sizeof(bp->pf->vf_req_fwd));
4783
4784         if (!(bp->fw_cap & BNXT_FW_CAP_LINK_ADMIN))
4785                 BNXT_HWRM_CMD_TO_FORWARD(HWRM_PORT_PHY_QCFG);
4786         BNXT_HWRM_CMD_TO_FORWARD(HWRM_FUNC_CFG);
4787         BNXT_HWRM_CMD_TO_FORWARD(HWRM_FUNC_VF_CFG);
4788         BNXT_HWRM_CMD_TO_FORWARD(HWRM_CFA_L2_FILTER_ALLOC);
4789         BNXT_HWRM_CMD_TO_FORWARD(HWRM_OEM_CMD);
4790 }
4791
4792 uint16_t
4793 bnxt_get_svif(uint16_t port_id, bool func_svif,
4794               enum bnxt_ulp_intf_type type)
4795 {
4796         struct rte_eth_dev *eth_dev;
4797         struct bnxt *bp;
4798
4799         eth_dev = &rte_eth_devices[port_id];
4800         if (BNXT_ETH_DEV_IS_REPRESENTOR(eth_dev)) {
4801                 struct bnxt_representor *vfr = eth_dev->data->dev_private;
4802                 if (!vfr)
4803                         return 0;
4804
4805                 if (type == BNXT_ULP_INTF_TYPE_VF_REP)
4806                         return vfr->svif;
4807
4808                 eth_dev = vfr->parent_dev;
4809         }
4810
4811         bp = eth_dev->data->dev_private;
4812
4813         return func_svif ? bp->func_svif : bp->port_svif;
4814 }
4815
4816 uint16_t
4817 bnxt_get_vnic_id(uint16_t port, enum bnxt_ulp_intf_type type)
4818 {
4819         struct rte_eth_dev *eth_dev;
4820         struct bnxt_vnic_info *vnic;
4821         struct bnxt *bp;
4822
4823         eth_dev = &rte_eth_devices[port];
4824         if (BNXT_ETH_DEV_IS_REPRESENTOR(eth_dev)) {
4825                 struct bnxt_representor *vfr = eth_dev->data->dev_private;
4826                 if (!vfr)
4827                         return 0;
4828
4829                 if (type == BNXT_ULP_INTF_TYPE_VF_REP)
4830                         return vfr->dflt_vnic_id;
4831
4832                 eth_dev = vfr->parent_dev;
4833         }
4834
4835         bp = eth_dev->data->dev_private;
4836
4837         vnic = BNXT_GET_DEFAULT_VNIC(bp);
4838
4839         return vnic->fw_vnic_id;
4840 }
4841
4842 uint16_t
4843 bnxt_get_fw_func_id(uint16_t port, enum bnxt_ulp_intf_type type)
4844 {
4845         struct rte_eth_dev *eth_dev;
4846         struct bnxt *bp;
4847
4848         eth_dev = &rte_eth_devices[port];
4849         if (BNXT_ETH_DEV_IS_REPRESENTOR(eth_dev)) {
4850                 struct bnxt_representor *vfr = eth_dev->data->dev_private;
4851                 if (!vfr)
4852                         return 0;
4853
4854                 if (type == BNXT_ULP_INTF_TYPE_VF_REP)
4855                         return vfr->fw_fid;
4856
4857                 eth_dev = vfr->parent_dev;
4858         }
4859
4860         bp = eth_dev->data->dev_private;
4861
4862         return bp->fw_fid;
4863 }
4864
4865 enum bnxt_ulp_intf_type
4866 bnxt_get_interface_type(uint16_t port)
4867 {
4868         struct rte_eth_dev *eth_dev;
4869         struct bnxt *bp;
4870
4871         eth_dev = &rte_eth_devices[port];
4872         if (BNXT_ETH_DEV_IS_REPRESENTOR(eth_dev))
4873                 return BNXT_ULP_INTF_TYPE_VF_REP;
4874
4875         bp = eth_dev->data->dev_private;
4876         if (BNXT_PF(bp))
4877                 return BNXT_ULP_INTF_TYPE_PF;
4878         else if (BNXT_VF_IS_TRUSTED(bp))
4879                 return BNXT_ULP_INTF_TYPE_TRUSTED_VF;
4880         else if (BNXT_VF(bp))
4881                 return BNXT_ULP_INTF_TYPE_VF;
4882
4883         return BNXT_ULP_INTF_TYPE_INVALID;
4884 }
4885
4886 uint16_t
4887 bnxt_get_phy_port_id(uint16_t port_id)
4888 {
4889         struct bnxt_representor *vfr;
4890         struct rte_eth_dev *eth_dev;
4891         struct bnxt *bp;
4892
4893         eth_dev = &rte_eth_devices[port_id];
4894         if (BNXT_ETH_DEV_IS_REPRESENTOR(eth_dev)) {
4895                 vfr = eth_dev->data->dev_private;
4896                 if (!vfr)
4897                         return 0;
4898
4899                 eth_dev = vfr->parent_dev;
4900         }
4901
4902         bp = eth_dev->data->dev_private;
4903
4904         return BNXT_PF(bp) ? bp->pf->port_id : bp->parent->port_id;
4905 }
4906
4907 uint16_t
4908 bnxt_get_parif(uint16_t port_id, enum bnxt_ulp_intf_type type)
4909 {
4910         struct rte_eth_dev *eth_dev;
4911         struct bnxt *bp;
4912
4913         eth_dev = &rte_eth_devices[port_id];
4914         if (BNXT_ETH_DEV_IS_REPRESENTOR(eth_dev)) {
4915                 struct bnxt_representor *vfr = eth_dev->data->dev_private;
4916                 if (!vfr)
4917                         return 0;
4918
4919                 if (type == BNXT_ULP_INTF_TYPE_VF_REP)
4920                         return vfr->fw_fid - 1;
4921
4922                 eth_dev = vfr->parent_dev;
4923         }
4924
4925         bp = eth_dev->data->dev_private;
4926
4927         return BNXT_PF(bp) ? bp->fw_fid - 1 : bp->parent->fid - 1;
4928 }
4929
4930 uint16_t
4931 bnxt_get_vport(uint16_t port_id)
4932 {
4933         return (1 << bnxt_get_phy_port_id(port_id));
4934 }
4935
4936 static void bnxt_alloc_error_recovery_info(struct bnxt *bp)
4937 {
4938         struct bnxt_error_recovery_info *info = bp->recovery_info;
4939
4940         if (info) {
4941                 if (!(bp->fw_cap & BNXT_FW_CAP_HCOMM_FW_STATUS))
4942                         memset(info, 0, sizeof(*info));
4943                 return;
4944         }
4945
4946         if (!(bp->fw_cap & BNXT_FW_CAP_ERROR_RECOVERY))
4947                 return;
4948
4949         info = rte_zmalloc("bnxt_hwrm_error_recovery_qcfg",
4950                            sizeof(*info), 0);
4951         if (!info)
4952                 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
4953
4954         bp->recovery_info = info;
4955 }
4956
4957 static void bnxt_check_fw_status(struct bnxt *bp)
4958 {
4959         uint32_t fw_status;
4960
4961         if (!(bp->recovery_info &&
4962               (bp->fw_cap & BNXT_FW_CAP_HCOMM_FW_STATUS)))
4963                 return;
4964
4965         fw_status = bnxt_read_fw_status_reg(bp, BNXT_FW_STATUS_REG);
4966         if (fw_status != BNXT_FW_STATUS_HEALTHY)
4967                 PMD_DRV_LOG(ERR, "Firmware not responding, status: %#x\n",
4968                             fw_status);
4969 }
4970
4971 static int bnxt_map_hcomm_fw_status_reg(struct bnxt *bp)
4972 {
4973         struct bnxt_error_recovery_info *info = bp->recovery_info;
4974         uint32_t status_loc;
4975         uint32_t sig_ver;
4976
4977         rte_write32(HCOMM_STATUS_STRUCT_LOC, (uint8_t *)bp->bar0 +
4978                     BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
4979         sig_ver = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
4980                                    BNXT_GRCP_WINDOW_2_BASE +
4981                                    offsetof(struct hcomm_status,
4982                                             sig_ver)));
4983         /* If the signature is absent, then FW does not support this feature */
4984         if ((sig_ver & HCOMM_STATUS_SIGNATURE_MASK) !=
4985             HCOMM_STATUS_SIGNATURE_VAL)
4986                 return 0;
4987
4988         if (!info) {
4989                 info = rte_zmalloc("bnxt_hwrm_error_recovery_qcfg",
4990                                    sizeof(*info), 0);
4991                 if (!info)
4992                         return -ENOMEM;
4993                 bp->recovery_info = info;
4994         } else {
4995                 memset(info, 0, sizeof(*info));
4996         }
4997
4998         status_loc = rte_le_to_cpu_32(rte_read32((uint8_t *)bp->bar0 +
4999                                       BNXT_GRCP_WINDOW_2_BASE +
5000                                       offsetof(struct hcomm_status,
5001                                                fw_status_loc)));
5002
5003         /* Only pre-map the FW health status GRC register */
5004         if (BNXT_FW_STATUS_REG_TYPE(status_loc) != BNXT_FW_STATUS_REG_TYPE_GRC)
5005                 return 0;
5006
5007         info->status_regs[BNXT_FW_STATUS_REG] = status_loc;
5008         info->mapped_status_regs[BNXT_FW_STATUS_REG] =
5009                 BNXT_GRCP_WINDOW_2_BASE + (status_loc & BNXT_GRCP_OFFSET_MASK);
5010
5011         rte_write32((status_loc & BNXT_GRCP_BASE_MASK), (uint8_t *)bp->bar0 +
5012                     BNXT_GRCPF_REG_WINDOW_BASE_OUT + 4);
5013
5014         bp->fw_cap |= BNXT_FW_CAP_HCOMM_FW_STATUS;
5015
5016         return 0;
5017 }
5018
5019 /* This function gets the FW version along with the
5020  * capabilities(MAX and current) of the function, vnic,
5021  * error recovery, phy and other chip related info
5022  */
5023 static int bnxt_get_config(struct bnxt *bp)
5024 {
5025         uint16_t mtu;
5026         int rc = 0;
5027
5028         bp->fw_cap = 0;
5029
5030         rc = bnxt_map_hcomm_fw_status_reg(bp);
5031         if (rc)
5032                 return rc;
5033
5034         rc = bnxt_hwrm_ver_get(bp, DFLT_HWRM_CMD_TIMEOUT);
5035         if (rc) {
5036                 bnxt_check_fw_status(bp);
5037                 return rc;
5038         }
5039
5040         rc = bnxt_hwrm_func_reset(bp);
5041         if (rc)
5042                 return -EIO;
5043
5044         rc = bnxt_hwrm_vnic_qcaps(bp);
5045         if (rc)
5046                 return rc;
5047
5048         rc = bnxt_hwrm_queue_qportcfg(bp);
5049         if (rc)
5050                 return rc;
5051
5052         /* Get the MAX capabilities for this function.
5053          * This function also allocates context memory for TQM rings and
5054          * informs the firmware about this allocated backing store memory.
5055          */
5056         rc = bnxt_hwrm_func_qcaps(bp);
5057         if (rc)
5058                 return rc;
5059
5060         rc = bnxt_hwrm_func_qcfg(bp, &mtu);
5061         if (rc)
5062                 return rc;
5063
5064         rc = bnxt_hwrm_cfa_adv_flow_mgmt_qcaps(bp);
5065         if (rc)
5066                 return rc;
5067
5068         bnxt_hwrm_port_mac_qcfg(bp);
5069
5070         bnxt_hwrm_parent_pf_qcfg(bp);
5071
5072         bnxt_hwrm_port_phy_qcaps(bp);
5073
5074         bnxt_alloc_error_recovery_info(bp);
5075         /* Get the adapter error recovery support info */
5076         rc = bnxt_hwrm_error_recovery_qcfg(bp);
5077         if (rc)
5078                 bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
5079
5080         bnxt_hwrm_port_led_qcaps(bp);
5081
5082         return 0;
5083 }
5084
5085 static int
5086 bnxt_init_locks(struct bnxt *bp)
5087 {
5088         int err;
5089
5090         err = pthread_mutex_init(&bp->flow_lock, NULL);
5091         if (err) {
5092                 PMD_DRV_LOG(ERR, "Unable to initialize flow_lock\n");
5093                 return err;
5094         }
5095
5096         err = pthread_mutex_init(&bp->def_cp_lock, NULL);
5097         if (err) {
5098                 PMD_DRV_LOG(ERR, "Unable to initialize def_cp_lock\n");
5099                 return err;
5100         }
5101
5102         err = pthread_mutex_init(&bp->health_check_lock, NULL);
5103         if (err) {
5104                 PMD_DRV_LOG(ERR, "Unable to initialize health_check_lock\n");
5105                 return err;
5106         }
5107
5108         err = pthread_mutex_init(&bp->err_recovery_lock, NULL);
5109         if (err)
5110                 PMD_DRV_LOG(ERR, "Unable to initialize err_recovery_lock\n");
5111
5112         return err;
5113 }
5114
5115 static int bnxt_init_resources(struct bnxt *bp, bool reconfig_dev)
5116 {
5117         int rc = 0;
5118
5119         rc = bnxt_get_config(bp);
5120         if (rc)
5121                 return rc;
5122
5123         if (!reconfig_dev) {
5124                 rc = bnxt_setup_mac_addr(bp->eth_dev);
5125                 if (rc)
5126                         return rc;
5127         } else {
5128                 rc = bnxt_restore_dflt_mac(bp);
5129                 if (rc)
5130                         return rc;
5131         }
5132
5133         bnxt_config_vf_req_fwd(bp);
5134
5135         rc = bnxt_hwrm_func_driver_register(bp);
5136         if (rc) {
5137                 PMD_DRV_LOG(ERR, "Failed to register driver");
5138                 return -EBUSY;
5139         }
5140
5141         if (BNXT_PF(bp)) {
5142                 if (bp->pdev->max_vfs) {
5143                         rc = bnxt_hwrm_allocate_vfs(bp, bp->pdev->max_vfs);
5144                         if (rc) {
5145                                 PMD_DRV_LOG(ERR, "Failed to allocate VFs\n");
5146                                 return rc;
5147                         }
5148                 } else {
5149                         rc = bnxt_hwrm_allocate_pf_only(bp);
5150                         if (rc) {
5151                                 PMD_DRV_LOG(ERR,
5152                                             "Failed to allocate PF resources");
5153                                 return rc;
5154                         }
5155                 }
5156         }
5157
5158         rc = bnxt_alloc_mem(bp, reconfig_dev);
5159         if (rc)
5160                 return rc;
5161
5162         rc = bnxt_setup_int(bp);
5163         if (rc)
5164                 return rc;
5165
5166         rc = bnxt_request_int(bp);
5167         if (rc)
5168                 return rc;
5169
5170         rc = bnxt_init_ctx_mem(bp);
5171         if (rc) {
5172                 PMD_DRV_LOG(ERR, "Failed to init adv_flow_counters\n");
5173                 return rc;
5174         }
5175
5176         return 0;
5177 }
5178
5179 static int
5180 bnxt_parse_devarg_truflow(__rte_unused const char *key,
5181                           const char *value, void *opaque_arg)
5182 {
5183         struct bnxt *bp = opaque_arg;
5184         unsigned long truflow;
5185         char *end = NULL;
5186
5187         if (!value || !opaque_arg) {
5188                 PMD_DRV_LOG(ERR,
5189                             "Invalid parameter passed to truflow devargs.\n");
5190                 return -EINVAL;
5191         }
5192
5193         truflow = strtoul(value, &end, 10);
5194         if (end == NULL || *end != '\0' ||
5195             (truflow == ULONG_MAX && errno == ERANGE)) {
5196                 PMD_DRV_LOG(ERR,
5197                             "Invalid parameter passed to truflow devargs.\n");
5198                 return -EINVAL;
5199         }
5200
5201         if (BNXT_DEVARG_TRUFLOW_INVALID(truflow)) {
5202                 PMD_DRV_LOG(ERR,
5203                             "Invalid value passed to truflow devargs.\n");
5204                 return -EINVAL;
5205         }
5206
5207         if (truflow) {
5208                 bp->flags |= BNXT_FLAG_TRUFLOW_EN;
5209                 PMD_DRV_LOG(INFO, "Host-based truflow feature enabled.\n");
5210         } else {
5211                 bp->flags &= ~BNXT_FLAG_TRUFLOW_EN;
5212                 PMD_DRV_LOG(INFO, "Host-based truflow feature disabled.\n");
5213         }
5214
5215         return 0;
5216 }
5217
5218 static int
5219 bnxt_parse_devarg_flow_xstat(__rte_unused const char *key,
5220                              const char *value, void *opaque_arg)
5221 {
5222         struct bnxt *bp = opaque_arg;
5223         unsigned long flow_xstat;
5224         char *end = NULL;
5225
5226         if (!value || !opaque_arg) {
5227                 PMD_DRV_LOG(ERR,
5228                             "Invalid parameter passed to flow_xstat devarg.\n");
5229                 return -EINVAL;
5230         }
5231
5232         flow_xstat = strtoul(value, &end, 10);
5233         if (end == NULL || *end != '\0' ||
5234             (flow_xstat == ULONG_MAX && errno == ERANGE)) {
5235                 PMD_DRV_LOG(ERR,
5236                             "Invalid parameter passed to flow_xstat devarg.\n");
5237                 return -EINVAL;
5238         }
5239
5240         if (BNXT_DEVARG_FLOW_XSTAT_INVALID(flow_xstat)) {
5241                 PMD_DRV_LOG(ERR,
5242                             "Invalid value passed to flow_xstat devarg.\n");
5243                 return -EINVAL;
5244         }
5245
5246         bp->flags |= BNXT_FLAG_FLOW_XSTATS_EN;
5247         if (BNXT_FLOW_XSTATS_EN(bp))
5248                 PMD_DRV_LOG(INFO, "flow_xstat feature enabled.\n");
5249
5250         return 0;
5251 }
5252
5253 static int
5254 bnxt_parse_devarg_max_num_kflows(__rte_unused const char *key,
5255                                         const char *value, void *opaque_arg)
5256 {
5257         struct bnxt *bp = opaque_arg;
5258         unsigned long max_num_kflows;
5259         char *end = NULL;
5260
5261         if (!value || !opaque_arg) {
5262                 PMD_DRV_LOG(ERR,
5263                         "Invalid parameter passed to max_num_kflows devarg.\n");
5264                 return -EINVAL;
5265         }
5266
5267         max_num_kflows = strtoul(value, &end, 10);
5268         if (end == NULL || *end != '\0' ||
5269                 (max_num_kflows == ULONG_MAX && errno == ERANGE)) {
5270                 PMD_DRV_LOG(ERR,
5271                         "Invalid parameter passed to max_num_kflows devarg.\n");
5272                 return -EINVAL;
5273         }
5274
5275         if (bnxt_devarg_max_num_kflow_invalid(max_num_kflows)) {
5276                 PMD_DRV_LOG(ERR,
5277                         "Invalid value passed to max_num_kflows devarg.\n");
5278                 return -EINVAL;
5279         }
5280
5281         bp->max_num_kflows = max_num_kflows;
5282         if (bp->max_num_kflows)
5283                 PMD_DRV_LOG(INFO, "max_num_kflows set as %ldK.\n",
5284                                 max_num_kflows);
5285
5286         return 0;
5287 }
5288
5289 static int
5290 bnxt_parse_devarg_rep_is_pf(__rte_unused const char *key,
5291                             const char *value, void *opaque_arg)
5292 {
5293         struct bnxt_representor *vfr_bp = opaque_arg;
5294         unsigned long rep_is_pf;
5295         char *end = NULL;
5296
5297         if (!value || !opaque_arg) {
5298                 PMD_DRV_LOG(ERR,
5299                             "Invalid parameter passed to rep_is_pf devargs.\n");
5300                 return -EINVAL;
5301         }
5302
5303         rep_is_pf = strtoul(value, &end, 10);
5304         if (end == NULL || *end != '\0' ||
5305             (rep_is_pf == ULONG_MAX && errno == ERANGE)) {
5306                 PMD_DRV_LOG(ERR,
5307                             "Invalid parameter passed to rep_is_pf devargs.\n");
5308                 return -EINVAL;
5309         }
5310
5311         if (BNXT_DEVARG_REP_IS_PF_INVALID(rep_is_pf)) {
5312                 PMD_DRV_LOG(ERR,
5313                             "Invalid value passed to rep_is_pf devargs.\n");
5314                 return -EINVAL;
5315         }
5316
5317         vfr_bp->flags |= rep_is_pf;
5318         if (BNXT_REP_PF(vfr_bp))
5319                 PMD_DRV_LOG(INFO, "PF representor\n");
5320         else
5321                 PMD_DRV_LOG(INFO, "VF representor\n");
5322
5323         return 0;
5324 }
5325
5326 static int
5327 bnxt_parse_devarg_rep_based_pf(__rte_unused const char *key,
5328                                const char *value, void *opaque_arg)
5329 {
5330         struct bnxt_representor *vfr_bp = opaque_arg;
5331         unsigned long rep_based_pf;
5332         char *end = NULL;
5333
5334         if (!value || !opaque_arg) {
5335                 PMD_DRV_LOG(ERR,
5336                             "Invalid parameter passed to rep_based_pf "
5337                             "devargs.\n");
5338                 return -EINVAL;
5339         }
5340
5341         rep_based_pf = strtoul(value, &end, 10);
5342         if (end == NULL || *end != '\0' ||
5343             (rep_based_pf == ULONG_MAX && errno == ERANGE)) {
5344                 PMD_DRV_LOG(ERR,
5345                             "Invalid parameter passed to rep_based_pf "
5346                             "devargs.\n");
5347                 return -EINVAL;
5348         }
5349
5350         if (BNXT_DEVARG_REP_BASED_PF_INVALID(rep_based_pf)) {
5351                 PMD_DRV_LOG(ERR,
5352                             "Invalid value passed to rep_based_pf devargs.\n");
5353                 return -EINVAL;
5354         }
5355
5356         vfr_bp->rep_based_pf = rep_based_pf;
5357         vfr_bp->flags |= BNXT_REP_BASED_PF_VALID;
5358
5359         PMD_DRV_LOG(INFO, "rep-based-pf = %d\n", vfr_bp->rep_based_pf);
5360
5361         return 0;
5362 }
5363
5364 static int
5365 bnxt_parse_devarg_rep_q_r2f(__rte_unused const char *key,
5366                             const char *value, void *opaque_arg)
5367 {
5368         struct bnxt_representor *vfr_bp = opaque_arg;
5369         unsigned long rep_q_r2f;
5370         char *end = NULL;
5371
5372         if (!value || !opaque_arg) {
5373                 PMD_DRV_LOG(ERR,
5374                             "Invalid parameter passed to rep_q_r2f "
5375                             "devargs.\n");
5376                 return -EINVAL;
5377         }
5378
5379         rep_q_r2f = strtoul(value, &end, 10);
5380         if (end == NULL || *end != '\0' ||
5381             (rep_q_r2f == ULONG_MAX && errno == ERANGE)) {
5382                 PMD_DRV_LOG(ERR,
5383                             "Invalid parameter passed to rep_q_r2f "
5384                             "devargs.\n");
5385                 return -EINVAL;
5386         }
5387
5388         if (BNXT_DEVARG_REP_Q_R2F_INVALID(rep_q_r2f)) {
5389                 PMD_DRV_LOG(ERR,
5390                             "Invalid value passed to rep_q_r2f devargs.\n");
5391                 return -EINVAL;
5392         }
5393
5394         vfr_bp->rep_q_r2f = rep_q_r2f;
5395         vfr_bp->flags |= BNXT_REP_Q_R2F_VALID;
5396         PMD_DRV_LOG(INFO, "rep-q-r2f = %d\n", vfr_bp->rep_q_r2f);
5397
5398         return 0;
5399 }
5400
5401 static int
5402 bnxt_parse_devarg_rep_q_f2r(__rte_unused const char *key,
5403                             const char *value, void *opaque_arg)
5404 {
5405         struct bnxt_representor *vfr_bp = opaque_arg;
5406         unsigned long rep_q_f2r;
5407         char *end = NULL;
5408
5409         if (!value || !opaque_arg) {
5410                 PMD_DRV_LOG(ERR,
5411                             "Invalid parameter passed to rep_q_f2r "
5412                             "devargs.\n");
5413                 return -EINVAL;
5414         }
5415
5416         rep_q_f2r = strtoul(value, &end, 10);
5417         if (end == NULL || *end != '\0' ||
5418             (rep_q_f2r == ULONG_MAX && errno == ERANGE)) {
5419                 PMD_DRV_LOG(ERR,
5420                             "Invalid parameter passed to rep_q_f2r "
5421                             "devargs.\n");
5422                 return -EINVAL;
5423         }
5424
5425         if (BNXT_DEVARG_REP_Q_F2R_INVALID(rep_q_f2r)) {
5426                 PMD_DRV_LOG(ERR,
5427                             "Invalid value passed to rep_q_f2r devargs.\n");
5428                 return -EINVAL;
5429         }
5430
5431         vfr_bp->rep_q_f2r = rep_q_f2r;
5432         vfr_bp->flags |= BNXT_REP_Q_F2R_VALID;
5433         PMD_DRV_LOG(INFO, "rep-q-f2r = %d\n", vfr_bp->rep_q_f2r);
5434
5435         return 0;
5436 }
5437
5438 static int
5439 bnxt_parse_devarg_rep_fc_r2f(__rte_unused const char *key,
5440                              const char *value, void *opaque_arg)
5441 {
5442         struct bnxt_representor *vfr_bp = opaque_arg;
5443         unsigned long rep_fc_r2f;
5444         char *end = NULL;
5445
5446         if (!value || !opaque_arg) {
5447                 PMD_DRV_LOG(ERR,
5448                             "Invalid parameter passed to rep_fc_r2f "
5449                             "devargs.\n");
5450                 return -EINVAL;
5451         }
5452
5453         rep_fc_r2f = strtoul(value, &end, 10);
5454         if (end == NULL || *end != '\0' ||
5455             (rep_fc_r2f == ULONG_MAX && errno == ERANGE)) {
5456                 PMD_DRV_LOG(ERR,
5457                             "Invalid parameter passed to rep_fc_r2f "
5458                             "devargs.\n");
5459                 return -EINVAL;
5460         }
5461
5462         if (BNXT_DEVARG_REP_FC_R2F_INVALID(rep_fc_r2f)) {
5463                 PMD_DRV_LOG(ERR,
5464                             "Invalid value passed to rep_fc_r2f devargs.\n");
5465                 return -EINVAL;
5466         }
5467
5468         vfr_bp->flags |= BNXT_REP_FC_R2F_VALID;
5469         vfr_bp->rep_fc_r2f = rep_fc_r2f;
5470         PMD_DRV_LOG(INFO, "rep-fc-r2f = %lu\n", rep_fc_r2f);
5471
5472         return 0;
5473 }
5474
5475 static int
5476 bnxt_parse_devarg_rep_fc_f2r(__rte_unused const char *key,
5477                              const char *value, void *opaque_arg)
5478 {
5479         struct bnxt_representor *vfr_bp = opaque_arg;
5480         unsigned long rep_fc_f2r;
5481         char *end = NULL;
5482
5483         if (!value || !opaque_arg) {
5484                 PMD_DRV_LOG(ERR,
5485                             "Invalid parameter passed to rep_fc_f2r "
5486                             "devargs.\n");
5487                 return -EINVAL;
5488         }
5489
5490         rep_fc_f2r = strtoul(value, &end, 10);
5491         if (end == NULL || *end != '\0' ||
5492             (rep_fc_f2r == ULONG_MAX && errno == ERANGE)) {
5493                 PMD_DRV_LOG(ERR,
5494                             "Invalid parameter passed to rep_fc_f2r "
5495                             "devargs.\n");
5496                 return -EINVAL;
5497         }
5498
5499         if (BNXT_DEVARG_REP_FC_F2R_INVALID(rep_fc_f2r)) {
5500                 PMD_DRV_LOG(ERR,
5501                             "Invalid value passed to rep_fc_f2r devargs.\n");
5502                 return -EINVAL;
5503         }
5504
5505         vfr_bp->flags |= BNXT_REP_FC_F2R_VALID;
5506         vfr_bp->rep_fc_f2r = rep_fc_f2r;
5507         PMD_DRV_LOG(INFO, "rep-fc-f2r = %lu\n", rep_fc_f2r);
5508
5509         return 0;
5510 }
5511
5512 static int
5513 bnxt_parse_dev_args(struct bnxt *bp, struct rte_devargs *devargs)
5514 {
5515         struct rte_kvargs *kvlist;
5516         int ret;
5517
5518         if (devargs == NULL)
5519                 return 0;
5520
5521         kvlist = rte_kvargs_parse(devargs->args, bnxt_dev_args);
5522         if (kvlist == NULL)
5523                 return -EINVAL;
5524
5525         /*
5526          * Handler for "truflow" devarg.
5527          * Invoked as for ex: "-a 0000:00:0d.0,host-based-truflow=1"
5528          */
5529         ret = rte_kvargs_process(kvlist, BNXT_DEVARG_TRUFLOW,
5530                                  bnxt_parse_devarg_truflow, bp);
5531         if (ret)
5532                 goto err;
5533
5534         /*
5535          * Handler for "flow_xstat" devarg.
5536          * Invoked as for ex: "-a 0000:00:0d.0,flow_xstat=1"
5537          */
5538         ret = rte_kvargs_process(kvlist, BNXT_DEVARG_FLOW_XSTAT,
5539                                  bnxt_parse_devarg_flow_xstat, bp);
5540         if (ret)
5541                 goto err;
5542
5543         /*
5544          * Handler for "max_num_kflows" devarg.
5545          * Invoked as for ex: "-a 000:00:0d.0,max_num_kflows=32"
5546          */
5547         ret = rte_kvargs_process(kvlist, BNXT_DEVARG_MAX_NUM_KFLOWS,
5548                                  bnxt_parse_devarg_max_num_kflows, bp);
5549         if (ret)
5550                 goto err;
5551
5552 err:
5553         rte_kvargs_free(kvlist);
5554         return ret;
5555 }
5556
5557 static int bnxt_alloc_switch_domain(struct bnxt *bp)
5558 {
5559         int rc = 0;
5560
5561         if (BNXT_PF(bp) || BNXT_VF_IS_TRUSTED(bp)) {
5562                 rc = rte_eth_switch_domain_alloc(&bp->switch_domain_id);
5563                 if (rc)
5564                         PMD_DRV_LOG(ERR,
5565                                     "Failed to alloc switch domain: %d\n", rc);
5566                 else
5567                         PMD_DRV_LOG(INFO,
5568                                     "Switch domain allocated %d\n",
5569                                     bp->switch_domain_id);
5570         }
5571
5572         return rc;
5573 }
5574
5575 /* Allocate and initialize various fields in bnxt struct that
5576  * need to be allocated/destroyed only once in the lifetime of the driver
5577  */
5578 static int bnxt_drv_init(struct rte_eth_dev *eth_dev)
5579 {
5580         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
5581         struct bnxt *bp = eth_dev->data->dev_private;
5582         int rc = 0;
5583
5584         bp->flags &= ~BNXT_FLAG_RX_VECTOR_PKT_MODE;
5585
5586         if (bnxt_vf_pciid(pci_dev->id.device_id))
5587                 bp->flags |= BNXT_FLAG_VF;
5588
5589         if (bnxt_p5_device(pci_dev->id.device_id))
5590                 bp->flags |= BNXT_FLAG_CHIP_P5;
5591
5592         if (pci_dev->id.device_id == BROADCOM_DEV_ID_58802 ||
5593             pci_dev->id.device_id == BROADCOM_DEV_ID_58804 ||
5594             pci_dev->id.device_id == BROADCOM_DEV_ID_58808 ||
5595             pci_dev->id.device_id == BROADCOM_DEV_ID_58802_VF)
5596                 bp->flags |= BNXT_FLAG_STINGRAY;
5597
5598         if (BNXT_TRUFLOW_EN(bp)) {
5599                 /* extra mbuf field is required to store CFA code from mark */
5600                 static const struct rte_mbuf_dynfield bnxt_cfa_code_dynfield_desc = {
5601                         .name = RTE_PMD_BNXT_CFA_CODE_DYNFIELD_NAME,
5602                         .size = sizeof(bnxt_cfa_code_dynfield_t),
5603                         .align = __alignof__(bnxt_cfa_code_dynfield_t),
5604                 };
5605                 bnxt_cfa_code_dynfield_offset =
5606                         rte_mbuf_dynfield_register(&bnxt_cfa_code_dynfield_desc);
5607                 if (bnxt_cfa_code_dynfield_offset < 0) {
5608                         PMD_DRV_LOG(ERR,
5609                             "Failed to register mbuf field for TruFlow mark\n");
5610                         return -rte_errno;
5611                 }
5612         }
5613
5614         rc = bnxt_map_pci_bars(eth_dev);
5615         if (rc) {
5616                 PMD_DRV_LOG(ERR,
5617                             "Failed to initialize board rc: %x\n", rc);
5618                 return rc;
5619         }
5620
5621         rc = bnxt_alloc_pf_info(bp);
5622         if (rc)
5623                 return rc;
5624
5625         rc = bnxt_alloc_link_info(bp);
5626         if (rc)
5627                 return rc;
5628
5629         rc = bnxt_alloc_parent_info(bp);
5630         if (rc)
5631                 return rc;
5632
5633         rc = bnxt_alloc_hwrm_resources(bp);
5634         if (rc) {
5635                 PMD_DRV_LOG(ERR,
5636                             "Failed to allocate response buffer rc: %x\n", rc);
5637                 return rc;
5638         }
5639         rc = bnxt_alloc_leds_info(bp);
5640         if (rc)
5641                 return rc;
5642
5643         rc = bnxt_alloc_cos_queues(bp);
5644         if (rc)
5645                 return rc;
5646
5647         rc = bnxt_init_locks(bp);
5648         if (rc)
5649                 return rc;
5650
5651         rc = bnxt_alloc_switch_domain(bp);
5652         if (rc)
5653                 return rc;
5654
5655         return rc;
5656 }
5657
5658 static int
5659 bnxt_dev_init(struct rte_eth_dev *eth_dev, void *params __rte_unused)
5660 {
5661         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
5662         static int version_printed;
5663         struct bnxt *bp;
5664         int rc;
5665
5666         if (version_printed++ == 0)
5667                 PMD_DRV_LOG(INFO, "%s\n", bnxt_version);
5668
5669         eth_dev->dev_ops = &bnxt_dev_ops;
5670         eth_dev->rx_queue_count = bnxt_rx_queue_count_op;
5671         eth_dev->rx_descriptor_status = bnxt_rx_descriptor_status_op;
5672         eth_dev->tx_descriptor_status = bnxt_tx_descriptor_status_op;
5673         eth_dev->rx_pkt_burst = &bnxt_recv_pkts;
5674         eth_dev->tx_pkt_burst = &bnxt_xmit_pkts;
5675
5676         /*
5677          * For secondary processes, we don't initialise any further
5678          * as primary has already done this work.
5679          */
5680         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5681                 return 0;
5682
5683         rte_eth_copy_pci_info(eth_dev, pci_dev);
5684         eth_dev->data->dev_flags |= RTE_ETH_DEV_AUTOFILL_QUEUE_XSTATS;
5685
5686         bp = eth_dev->data->dev_private;
5687
5688         /* Parse dev arguments passed on when starting the DPDK application. */
5689         rc = bnxt_parse_dev_args(bp, pci_dev->device.devargs);
5690         if (rc)
5691                 goto error_free;
5692
5693         rc = bnxt_drv_init(eth_dev);
5694         if (rc)
5695                 goto error_free;
5696
5697         rc = bnxt_init_resources(bp, false);
5698         if (rc)
5699                 goto error_free;
5700
5701         rc = bnxt_alloc_stats_mem(bp);
5702         if (rc)
5703                 goto error_free;
5704
5705         PMD_DRV_LOG(INFO,
5706                     DRV_MODULE_NAME "found at mem %" PRIX64 ", node addr %pM\n",
5707                     pci_dev->mem_resource[0].phys_addr,
5708                     pci_dev->mem_resource[0].addr);
5709
5710         return 0;
5711
5712 error_free:
5713         bnxt_dev_uninit(eth_dev);
5714         return rc;
5715 }
5716
5717
5718 static void bnxt_free_ctx_mem_buf(struct bnxt_ctx_mem_buf_info *ctx)
5719 {
5720         if (!ctx)
5721                 return;
5722
5723         if (ctx->va)
5724                 rte_free(ctx->va);
5725
5726         ctx->va = NULL;
5727         ctx->dma = RTE_BAD_IOVA;
5728         ctx->ctx_id = BNXT_CTX_VAL_INVAL;
5729 }
5730
5731 static void bnxt_unregister_fc_ctx_mem(struct bnxt *bp)
5732 {
5733         bnxt_hwrm_cfa_counter_cfg(bp, BNXT_DIR_RX,
5734                                   CFA_COUNTER_CFG_IN_COUNTER_TYPE_FC,
5735                                   bp->flow_stat->rx_fc_out_tbl.ctx_id,
5736                                   bp->flow_stat->max_fc,
5737                                   false);
5738
5739         bnxt_hwrm_cfa_counter_cfg(bp, BNXT_DIR_TX,
5740                                   CFA_COUNTER_CFG_IN_COUNTER_TYPE_FC,
5741                                   bp->flow_stat->tx_fc_out_tbl.ctx_id,
5742                                   bp->flow_stat->max_fc,
5743                                   false);
5744
5745         if (bp->flow_stat->rx_fc_in_tbl.ctx_id != BNXT_CTX_VAL_INVAL)
5746                 bnxt_hwrm_ctx_unrgtr(bp, bp->flow_stat->rx_fc_in_tbl.ctx_id);
5747         bp->flow_stat->rx_fc_in_tbl.ctx_id = BNXT_CTX_VAL_INVAL;
5748
5749         if (bp->flow_stat->rx_fc_out_tbl.ctx_id != BNXT_CTX_VAL_INVAL)
5750                 bnxt_hwrm_ctx_unrgtr(bp, bp->flow_stat->rx_fc_out_tbl.ctx_id);
5751         bp->flow_stat->rx_fc_out_tbl.ctx_id = BNXT_CTX_VAL_INVAL;
5752
5753         if (bp->flow_stat->tx_fc_in_tbl.ctx_id != BNXT_CTX_VAL_INVAL)
5754                 bnxt_hwrm_ctx_unrgtr(bp, bp->flow_stat->tx_fc_in_tbl.ctx_id);
5755         bp->flow_stat->tx_fc_in_tbl.ctx_id = BNXT_CTX_VAL_INVAL;
5756
5757         if (bp->flow_stat->tx_fc_out_tbl.ctx_id != BNXT_CTX_VAL_INVAL)
5758                 bnxt_hwrm_ctx_unrgtr(bp, bp->flow_stat->tx_fc_out_tbl.ctx_id);
5759         bp->flow_stat->tx_fc_out_tbl.ctx_id = BNXT_CTX_VAL_INVAL;
5760 }
5761
5762 static void bnxt_uninit_fc_ctx_mem(struct bnxt *bp)
5763 {
5764         bnxt_unregister_fc_ctx_mem(bp);
5765
5766         bnxt_free_ctx_mem_buf(&bp->flow_stat->rx_fc_in_tbl);
5767         bnxt_free_ctx_mem_buf(&bp->flow_stat->rx_fc_out_tbl);
5768         bnxt_free_ctx_mem_buf(&bp->flow_stat->tx_fc_in_tbl);
5769         bnxt_free_ctx_mem_buf(&bp->flow_stat->tx_fc_out_tbl);
5770 }
5771
5772 static void bnxt_uninit_ctx_mem(struct bnxt *bp)
5773 {
5774         if (BNXT_FLOW_XSTATS_EN(bp))
5775                 bnxt_uninit_fc_ctx_mem(bp);
5776 }
5777
5778 static void
5779 bnxt_free_error_recovery_info(struct bnxt *bp)
5780 {
5781         rte_free(bp->recovery_info);
5782         bp->recovery_info = NULL;
5783         bp->fw_cap &= ~BNXT_FW_CAP_ERROR_RECOVERY;
5784 }
5785
5786 static int
5787 bnxt_uninit_resources(struct bnxt *bp, bool reconfig_dev)
5788 {
5789         int rc;
5790
5791         bnxt_free_int(bp);
5792         bnxt_free_mem(bp, reconfig_dev);
5793
5794         bnxt_hwrm_func_buf_unrgtr(bp);
5795         if (bp->pf != NULL) {
5796                 rte_free(bp->pf->vf_req_buf);
5797                 bp->pf->vf_req_buf = NULL;
5798         }
5799
5800         rc = bnxt_hwrm_func_driver_unregister(bp, 0);
5801         bp->flags &= ~BNXT_FLAG_REGISTERED;
5802         bnxt_free_ctx_mem(bp);
5803         if (!reconfig_dev) {
5804                 bnxt_free_hwrm_resources(bp);
5805                 bnxt_free_error_recovery_info(bp);
5806         }
5807
5808         bnxt_uninit_ctx_mem(bp);
5809
5810         bnxt_free_flow_stats_info(bp);
5811         if (bp->rep_info != NULL)
5812                 bnxt_free_switch_domain(bp);
5813         bnxt_free_rep_info(bp);
5814         rte_free(bp->ptp_cfg);
5815         bp->ptp_cfg = NULL;
5816         return rc;
5817 }
5818
5819 static int
5820 bnxt_dev_uninit(struct rte_eth_dev *eth_dev)
5821 {
5822         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
5823                 return -EPERM;
5824
5825         PMD_DRV_LOG(DEBUG, "Calling Device uninit\n");
5826
5827         if (eth_dev->state != RTE_ETH_DEV_UNUSED)
5828                 bnxt_dev_close_op(eth_dev);
5829
5830         return 0;
5831 }
5832
5833 static int bnxt_pci_remove_dev_with_reps(struct rte_eth_dev *eth_dev)
5834 {
5835         struct bnxt *bp = eth_dev->data->dev_private;
5836         struct rte_eth_dev *vf_rep_eth_dev;
5837         int ret = 0, i;
5838
5839         if (!bp)
5840                 return -EINVAL;
5841
5842         for (i = 0; i < bp->num_reps; i++) {
5843                 vf_rep_eth_dev = bp->rep_info[i].vfr_eth_dev;
5844                 if (!vf_rep_eth_dev)
5845                         continue;
5846                 PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR pci remove\n",
5847                             vf_rep_eth_dev->data->port_id);
5848                 rte_eth_dev_destroy(vf_rep_eth_dev, bnxt_representor_uninit);
5849         }
5850         PMD_DRV_LOG(DEBUG, "BNXT Port:%d pci remove\n",
5851                     eth_dev->data->port_id);
5852         ret = rte_eth_dev_destroy(eth_dev, bnxt_dev_uninit);
5853
5854         return ret;
5855 }
5856
5857 static void bnxt_free_rep_info(struct bnxt *bp)
5858 {
5859         rte_free(bp->rep_info);
5860         bp->rep_info = NULL;
5861         rte_free(bp->cfa_code_map);
5862         bp->cfa_code_map = NULL;
5863 }
5864
5865 static int bnxt_init_rep_info(struct bnxt *bp)
5866 {
5867         int i = 0, rc;
5868
5869         if (bp->rep_info)
5870                 return 0;
5871
5872         bp->rep_info = rte_zmalloc("bnxt_rep_info",
5873                                    sizeof(bp->rep_info[0]) * BNXT_MAX_VF_REPS,
5874                                    0);
5875         if (!bp->rep_info) {
5876                 PMD_DRV_LOG(ERR, "Failed to alloc memory for rep info\n");
5877                 return -ENOMEM;
5878         }
5879         bp->cfa_code_map = rte_zmalloc("bnxt_cfa_code_map",
5880                                        sizeof(*bp->cfa_code_map) *
5881                                        BNXT_MAX_CFA_CODE, 0);
5882         if (!bp->cfa_code_map) {
5883                 PMD_DRV_LOG(ERR, "Failed to alloc memory for cfa_code_map\n");
5884                 bnxt_free_rep_info(bp);
5885                 return -ENOMEM;
5886         }
5887
5888         for (i = 0; i < BNXT_MAX_CFA_CODE; i++)
5889                 bp->cfa_code_map[i] = BNXT_VF_IDX_INVALID;
5890
5891         rc = pthread_mutex_init(&bp->rep_info->vfr_lock, NULL);
5892         if (rc) {
5893                 PMD_DRV_LOG(ERR, "Unable to initialize vfr_lock\n");
5894                 bnxt_free_rep_info(bp);
5895                 return rc;
5896         }
5897
5898         rc = pthread_mutex_init(&bp->rep_info->vfr_start_lock, NULL);
5899         if (rc) {
5900                 PMD_DRV_LOG(ERR, "Unable to initialize vfr_start_lock\n");
5901                 bnxt_free_rep_info(bp);
5902                 return rc;
5903         }
5904
5905         return rc;
5906 }
5907
5908 static int bnxt_rep_port_probe(struct rte_pci_device *pci_dev,
5909                                struct rte_eth_devargs *eth_da,
5910                                struct rte_eth_dev *backing_eth_dev,
5911                                const char *dev_args)
5912 {
5913         struct rte_eth_dev *vf_rep_eth_dev;
5914         char name[RTE_ETH_NAME_MAX_LEN];
5915         struct bnxt *backing_bp;
5916         uint16_t num_rep;
5917         int i, ret = 0;
5918         struct rte_kvargs *kvlist = NULL;
5919
5920         if (eth_da->type == RTE_ETH_REPRESENTOR_NONE)
5921                 return 0;
5922         if (eth_da->type != RTE_ETH_REPRESENTOR_VF) {
5923                 PMD_DRV_LOG(ERR, "unsupported representor type %d\n",
5924                             eth_da->type);
5925                 return -ENOTSUP;
5926         }
5927         num_rep = eth_da->nb_representor_ports;
5928         if (num_rep > BNXT_MAX_VF_REPS) {
5929                 PMD_DRV_LOG(ERR, "nb_representor_ports = %d > %d MAX VF REPS\n",
5930                             num_rep, BNXT_MAX_VF_REPS);
5931                 return -EINVAL;
5932         }
5933
5934         if (num_rep >= RTE_MAX_ETHPORTS) {
5935                 PMD_DRV_LOG(ERR,
5936                             "nb_representor_ports = %d > %d MAX ETHPORTS\n",
5937                             num_rep, RTE_MAX_ETHPORTS);
5938                 return -EINVAL;
5939         }
5940
5941         backing_bp = backing_eth_dev->data->dev_private;
5942
5943         if (!(BNXT_PF(backing_bp) || BNXT_VF_IS_TRUSTED(backing_bp))) {
5944                 PMD_DRV_LOG(ERR,
5945                             "Not a PF or trusted VF. No Representor support\n");
5946                 /* Returning an error is not an option.
5947                  * Applications are not handling this correctly
5948                  */
5949                 return 0;
5950         }
5951
5952         if (bnxt_init_rep_info(backing_bp))
5953                 return 0;
5954
5955         for (i = 0; i < num_rep; i++) {
5956                 struct bnxt_representor representor = {
5957                         .vf_id = eth_da->representor_ports[i],
5958                         .switch_domain_id = backing_bp->switch_domain_id,
5959                         .parent_dev = backing_eth_dev
5960                 };
5961
5962                 if (representor.vf_id >= BNXT_MAX_VF_REPS) {
5963                         PMD_DRV_LOG(ERR, "VF-Rep id %d >= %d MAX VF ID\n",
5964                                     representor.vf_id, BNXT_MAX_VF_REPS);
5965                         continue;
5966                 }
5967
5968                 /* representor port net_bdf_port */
5969                 snprintf(name, sizeof(name), "net_%s_representor_%d",
5970                          pci_dev->device.name, eth_da->representor_ports[i]);
5971
5972                 kvlist = rte_kvargs_parse(dev_args, bnxt_dev_args);
5973                 if (kvlist) {
5974                         /*
5975                          * Handler for "rep_is_pf" devarg.
5976                          * Invoked as for ex: "-a 000:00:0d.0,
5977                          * rep-based-pf=<pf index> rep-is-pf=<VF=0 or PF=1>"
5978                          */
5979                         ret = rte_kvargs_process(kvlist, BNXT_DEVARG_REP_IS_PF,
5980                                                  bnxt_parse_devarg_rep_is_pf,
5981                                                  (void *)&representor);
5982                         if (ret) {
5983                                 ret = -EINVAL;
5984                                 goto err;
5985                         }
5986                         /*
5987                          * Handler for "rep_based_pf" devarg.
5988                          * Invoked as for ex: "-a 000:00:0d.0,
5989                          * rep-based-pf=<pf index> rep-is-pf=<VF=0 or PF=1>"
5990                          */
5991                         ret = rte_kvargs_process(kvlist,
5992                                                  BNXT_DEVARG_REP_BASED_PF,
5993                                                  bnxt_parse_devarg_rep_based_pf,
5994                                                  (void *)&representor);
5995                         if (ret) {
5996                                 ret = -EINVAL;
5997                                 goto err;
5998                         }
5999                         /*
6000                          * Handler for "rep_based_pf" devarg.
6001                          * Invoked as for ex: "-a 000:00:0d.0,
6002                          * rep-based-pf=<pf index> rep-is-pf=<VF=0 or PF=1>"
6003                          */
6004                         ret = rte_kvargs_process(kvlist, BNXT_DEVARG_REP_Q_R2F,
6005                                                  bnxt_parse_devarg_rep_q_r2f,
6006                                                  (void *)&representor);
6007                         if (ret) {
6008                                 ret = -EINVAL;
6009                                 goto err;
6010                         }
6011                         /*
6012                          * Handler for "rep_based_pf" devarg.
6013                          * Invoked as for ex: "-a 000:00:0d.0,
6014                          * rep-based-pf=<pf index> rep-is-pf=<VF=0 or PF=1>"
6015                          */
6016                         ret = rte_kvargs_process(kvlist, BNXT_DEVARG_REP_Q_F2R,
6017                                                  bnxt_parse_devarg_rep_q_f2r,
6018                                                  (void *)&representor);
6019                         if (ret) {
6020                                 ret = -EINVAL;
6021                                 goto err;
6022                         }
6023                         /*
6024                          * Handler for "rep_based_pf" devarg.
6025                          * Invoked as for ex: "-a 000:00:0d.0,
6026                          * rep-based-pf=<pf index> rep-is-pf=<VF=0 or PF=1>"
6027                          */
6028                         ret = rte_kvargs_process(kvlist, BNXT_DEVARG_REP_FC_R2F,
6029                                                  bnxt_parse_devarg_rep_fc_r2f,
6030                                                  (void *)&representor);
6031                         if (ret) {
6032                                 ret = -EINVAL;
6033                                 goto err;
6034                         }
6035                         /*
6036                          * Handler for "rep_based_pf" devarg.
6037                          * Invoked as for ex: "-a 000:00:0d.0,
6038                          * rep-based-pf=<pf index> rep-is-pf=<VF=0 or PF=1>"
6039                          */
6040                         ret = rte_kvargs_process(kvlist, BNXT_DEVARG_REP_FC_F2R,
6041                                                  bnxt_parse_devarg_rep_fc_f2r,
6042                                                  (void *)&representor);
6043                         if (ret) {
6044                                 ret = -EINVAL;
6045                                 goto err;
6046                         }
6047                 }
6048
6049                 ret = rte_eth_dev_create(&pci_dev->device, name,
6050                                          sizeof(struct bnxt_representor),
6051                                          NULL, NULL,
6052                                          bnxt_representor_init,
6053                                          &representor);
6054                 if (ret) {
6055                         PMD_DRV_LOG(ERR, "failed to create bnxt vf "
6056                                     "representor %s.", name);
6057                         goto err;
6058                 }
6059
6060                 vf_rep_eth_dev = rte_eth_dev_allocated(name);
6061                 if (!vf_rep_eth_dev) {
6062                         PMD_DRV_LOG(ERR, "Failed to find the eth_dev"
6063                                     " for VF-Rep: %s.", name);
6064                         ret = -ENODEV;
6065                         goto err;
6066                 }
6067
6068                 PMD_DRV_LOG(DEBUG, "BNXT Port:%d VFR pci probe\n",
6069                             backing_eth_dev->data->port_id);
6070                 backing_bp->rep_info[representor.vf_id].vfr_eth_dev =
6071                                                          vf_rep_eth_dev;
6072                 backing_bp->num_reps++;
6073
6074         }
6075
6076         rte_kvargs_free(kvlist);
6077         return 0;
6078
6079 err:
6080         /* If num_rep > 1, then rollback already created
6081          * ports, since we'll be failing the probe anyway
6082          */
6083         if (num_rep > 1)
6084                 bnxt_pci_remove_dev_with_reps(backing_eth_dev);
6085         rte_errno = -ret;
6086         rte_kvargs_free(kvlist);
6087
6088         return ret;
6089 }
6090
6091 static int bnxt_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
6092                           struct rte_pci_device *pci_dev)
6093 {
6094         struct rte_eth_devargs eth_da = { .nb_representor_ports = 0 };
6095         struct rte_eth_dev *backing_eth_dev;
6096         uint16_t num_rep;
6097         int ret = 0;
6098
6099         if (pci_dev->device.devargs) {
6100                 ret = rte_eth_devargs_parse(pci_dev->device.devargs->args,
6101                                             &eth_da);
6102                 if (ret)
6103                         return ret;
6104         }
6105
6106         num_rep = eth_da.nb_representor_ports;
6107         PMD_DRV_LOG(DEBUG, "nb_representor_ports = %d\n",
6108                     num_rep);
6109
6110         /* We could come here after first level of probe is already invoked
6111          * as part of an application bringup(OVS-DPDK vswitchd), so first check
6112          * for already allocated eth_dev for the backing device (PF/Trusted VF)
6113          */
6114         backing_eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
6115         if (backing_eth_dev == NULL) {
6116                 ret = rte_eth_dev_create(&pci_dev->device, pci_dev->device.name,
6117                                          sizeof(struct bnxt),
6118                                          eth_dev_pci_specific_init, pci_dev,
6119                                          bnxt_dev_init, NULL);
6120
6121                 if (ret || !num_rep)
6122                         return ret;
6123
6124                 backing_eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
6125         }
6126         PMD_DRV_LOG(DEBUG, "BNXT Port:%d pci probe\n",
6127                     backing_eth_dev->data->port_id);
6128
6129         if (!num_rep)
6130                 return ret;
6131
6132         /* probe representor ports now */
6133         ret = bnxt_rep_port_probe(pci_dev, &eth_da, backing_eth_dev,
6134                                   pci_dev->device.devargs->args);
6135
6136         return ret;
6137 }
6138
6139 static int bnxt_pci_remove(struct rte_pci_device *pci_dev)
6140 {
6141         struct rte_eth_dev *eth_dev;
6142
6143         eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
6144         if (!eth_dev)
6145                 return 0; /* Invoked typically only by OVS-DPDK, by the
6146                            * time it comes here the eth_dev is already
6147                            * deleted by rte_eth_dev_close(), so returning
6148                            * +ve value will at least help in proper cleanup
6149                            */
6150
6151         PMD_DRV_LOG(DEBUG, "BNXT Port:%d pci remove\n", eth_dev->data->port_id);
6152         if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
6153                 if (eth_dev->data->dev_flags & RTE_ETH_DEV_REPRESENTOR)
6154                         return rte_eth_dev_destroy(eth_dev,
6155                                                    bnxt_representor_uninit);
6156                 else
6157                         return rte_eth_dev_destroy(eth_dev,
6158                                                    bnxt_dev_uninit);
6159         } else {
6160                 return rte_eth_dev_pci_generic_remove(pci_dev, NULL);
6161         }
6162 }
6163
6164 static struct rte_pci_driver bnxt_rte_pmd = {
6165         .id_table = bnxt_pci_id_map,
6166         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC |
6167                         RTE_PCI_DRV_PROBE_AGAIN, /* Needed in case of VF-REPs
6168                                                   * and OVS-DPDK
6169                                                   */
6170         .probe = bnxt_pci_probe,
6171         .remove = bnxt_pci_remove,
6172 };
6173
6174 static bool
6175 is_device_supported(struct rte_eth_dev *dev, struct rte_pci_driver *drv)
6176 {
6177         if (strcmp(dev->device->driver->name, drv->driver.name))
6178                 return false;
6179
6180         return true;
6181 }
6182
6183 bool is_bnxt_supported(struct rte_eth_dev *dev)
6184 {
6185         return is_device_supported(dev, &bnxt_rte_pmd);
6186 }
6187
6188 RTE_LOG_REGISTER(bnxt_logtype_driver, pmd.net.bnxt.driver, NOTICE);
6189 RTE_PMD_REGISTER_PCI(net_bnxt, bnxt_rte_pmd);
6190 RTE_PMD_REGISTER_PCI_TABLE(net_bnxt, bnxt_pci_id_map);
6191 RTE_PMD_REGISTER_KMOD_DEP(net_bnxt, "* igb_uio | uio_pci_generic | vfio-pci");