]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bnxt/if_bnxt.c
bnxt: Implement interrupt coalescing options using sysctl.
[FreeBSD/FreeBSD.git] / sys / dev / bnxt / if_bnxt.c
1 /*-
2  * Broadcom NetXtreme-C/E network driver.
3  *
4  * Copyright (c) 2016 Broadcom, All Rights Reserved.
5  * The term Broadcom refers to Broadcom Limited and/or its subsidiaries
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS'
17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31
32 #include <sys/param.h>
33 #include <sys/socket.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 #include <sys/rman.h>
38 #include <sys/endian.h>
39 #include <sys/sockio.h>
40 #include <sys/priv.h>
41
42 #include <machine/bus.h>
43 #include <machine/resource.h>
44
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47
48 #include <net/if.h>
49 #include <net/if_media.h>
50 #include <net/if_var.h>
51 #include <net/ethernet.h>
52 #include <net/iflib.h>
53
54 #include "opt_inet.h"
55 #include "opt_inet6.h"
56 #include "opt_rss.h"
57
58 #include "ifdi_if.h"
59
60 #include "bnxt.h"
61 #include "bnxt_hwrm.h"
62 #include "bnxt_ioctl.h"
63 #include "bnxt_sysctl.h"
64 #include "hsi_struct_def.h"
65
66 /*
67  * PCI Device ID Table
68  */
69
70 static pci_vendor_info_t bnxt_vendor_info_array[] =
71 {
72     PVID(BROADCOM_VENDOR_ID, BCM57301,
73         "Broadcom BCM57301 NetXtreme-C 10Gb Ethernet Controller"),
74     PVID(BROADCOM_VENDOR_ID, BCM57302,
75         "Broadcom BCM57302 NetXtreme-C 10Gb/25Gb Ethernet Controller"),
76     PVID(BROADCOM_VENDOR_ID, BCM57304,
77         "Broadcom BCM57304 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet Controller"),
78     PVID(BROADCOM_VENDOR_ID, BCM57311,
79         "Broadcom BCM57311 NetXtreme-C 10Gb Ethernet"),
80     PVID(BROADCOM_VENDOR_ID, BCM57312,
81         "Broadcom BCM57312 NetXtreme-C 10Gb/25Gb Ethernet"),
82     PVID(BROADCOM_VENDOR_ID, BCM57314,
83         "Broadcom BCM57314 NetXtreme-C 10Gb/25Gb/40Gb/50Gb Ethernet"),
84     PVID(BROADCOM_VENDOR_ID, BCM57402,
85         "Broadcom BCM57402 NetXtreme-E 10Gb Ethernet Controller"),
86     PVID(BROADCOM_VENDOR_ID, BCM57402_NPAR,
87         "Broadcom BCM57402 NetXtreme-E Partition"),
88     PVID(BROADCOM_VENDOR_ID, BCM57404,
89         "Broadcom BCM57404 NetXtreme-E 10Gb/25Gb Ethernet Controller"),
90     PVID(BROADCOM_VENDOR_ID, BCM57404_NPAR,
91         "Broadcom BCM57404 NetXtreme-E Partition"),
92     PVID(BROADCOM_VENDOR_ID, BCM57406,
93         "Broadcom BCM57406 NetXtreme-E 10GBase-T Ethernet Controller"),
94     PVID(BROADCOM_VENDOR_ID, BCM57406_NPAR,
95         "Broadcom BCM57406 NetXtreme-E Partition"),
96     PVID(BROADCOM_VENDOR_ID, BCM57407,
97         "Broadcom BCM57407 NetXtreme-E 10GBase-T Ethernet Controller"),
98     PVID(BROADCOM_VENDOR_ID, BCM57407_NPAR,
99         "Broadcom BCM57407 NetXtreme-E Ethernet Partition"),
100     PVID(BROADCOM_VENDOR_ID, BCM57407_SFP,
101         "Broadcom BCM57407 NetXtreme-E 25Gb Ethernet Controller"),
102     PVID(BROADCOM_VENDOR_ID, BCM57412,
103         "Broadcom BCM57412 NetXtreme-E 10Gb Ethernet"),
104     PVID(BROADCOM_VENDOR_ID, BCM57412_NPAR1,
105         "Broadcom BCM57412 NetXtreme-E Ethernet Partition"),
106     PVID(BROADCOM_VENDOR_ID, BCM57412_NPAR2,
107         "Broadcom BCM57412 NetXtreme-E Ethernet Partition"),
108     PVID(BROADCOM_VENDOR_ID, BCM57414,
109         "Broadcom BCM57414 NetXtreme-E 10Gb/25Gb Ethernet"),
110     PVID(BROADCOM_VENDOR_ID, BCM57414_NPAR1,
111         "Broadcom BCM57414 NetXtreme-E Ethernet Partition"),
112     PVID(BROADCOM_VENDOR_ID, BCM57414_NPAR2,
113         "Broadcom BCM57414 NetXtreme-E Ethernet Partition"),
114     PVID(BROADCOM_VENDOR_ID, BCM57416,
115         "Broadcom BCM57416 NetXtreme-E 10GBase-T Ethernet"),
116     PVID(BROADCOM_VENDOR_ID, BCM57416_NPAR1,
117         "Broadcom BCM57416 NetXtreme-E Ethernet Partition"),
118     PVID(BROADCOM_VENDOR_ID, BCM57416_NPAR2,
119         "Broadcom BCM57416 NetXtreme-E Ethernet Partition"),
120     PVID(BROADCOM_VENDOR_ID, BCM57416_SFP,
121         "Broadcom BCM57416 NetXtreme-E 10Gb Ethernet"),
122     PVID(BROADCOM_VENDOR_ID, BCM57417,
123         "Broadcom BCM57417 NetXtreme-E 10GBase-T Ethernet"),
124     PVID(BROADCOM_VENDOR_ID, BCM57417_NPAR1,
125         "Broadcom BCM57417 NetXtreme-E Ethernet Partition"),
126     PVID(BROADCOM_VENDOR_ID, BCM57417_NPAR2,
127         "Broadcom BCM57417 NetXtreme-E Ethernet Partition"),
128     PVID(BROADCOM_VENDOR_ID, BCM57417_SFP,
129         "Broadcom BCM57417 NetXtreme-E 10Gb/25Gb Ethernet"),
130     PVID(BROADCOM_VENDOR_ID, BCM57454,
131         "Broadcom BCM57454 NetXtreme-E 10Gb/25Gb/40Gb/50Gb/100Gb Ethernet"),
132     PVID(BROADCOM_VENDOR_ID, BCM58700,
133         "Broadcom BCM58700 Nitro 1Gb/2.5Gb/10Gb Ethernet"),
134     PVID(BROADCOM_VENDOR_ID, NETXTREME_C_VF1,
135         "Broadcom NetXtreme-C Ethernet Virtual Function"),
136     PVID(BROADCOM_VENDOR_ID, NETXTREME_C_VF2,
137         "Broadcom NetXtreme-C Ethernet Virtual Function"),
138     PVID(BROADCOM_VENDOR_ID, NETXTREME_C_VF3,
139         "Broadcom NetXtreme-C Ethernet Virtual Function"),
140     PVID(BROADCOM_VENDOR_ID, NETXTREME_E_VF1,
141         "Broadcom NetXtreme-E Ethernet Virtual Function"),
142     PVID(BROADCOM_VENDOR_ID, NETXTREME_E_VF2,
143         "Broadcom NetXtreme-E Ethernet Virtual Function"),
144     PVID(BROADCOM_VENDOR_ID, NETXTREME_E_VF3,
145         "Broadcom NetXtreme-E Ethernet Virtual Function"),
146     /* required last entry */
147
148     PVID_END
149 };
150
151 /*
152  * Function prototypes
153  */
154
155 static void *bnxt_register(device_t dev);
156
157 /* Soft queue setup and teardown */
158 static int bnxt_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
159     uint64_t *paddrs, int ntxqs, int ntxqsets);
160 static int bnxt_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
161     uint64_t *paddrs, int nrxqs, int nrxqsets);
162 static void bnxt_queues_free(if_ctx_t ctx);
163
164 /* Device setup and teardown */
165 static int bnxt_attach_pre(if_ctx_t ctx);
166 static int bnxt_attach_post(if_ctx_t ctx);
167 static int bnxt_detach(if_ctx_t ctx);
168
169 /* Device configuration */
170 static void bnxt_init(if_ctx_t ctx);
171 static void bnxt_stop(if_ctx_t ctx);
172 static void bnxt_multi_set(if_ctx_t ctx);
173 static int bnxt_mtu_set(if_ctx_t ctx, uint32_t mtu);
174 static void bnxt_media_status(if_ctx_t ctx, struct ifmediareq * ifmr);
175 static int bnxt_media_change(if_ctx_t ctx);
176 static int bnxt_promisc_set(if_ctx_t ctx, int flags);
177 static uint64_t bnxt_get_counter(if_ctx_t, ift_counter);
178 static void bnxt_update_admin_status(if_ctx_t ctx);
179
180 /* Interrupt enable / disable */
181 static void bnxt_intr_enable(if_ctx_t ctx);
182 static int bnxt_rx_queue_intr_enable(if_ctx_t ctx, uint16_t qid);
183 static int bnxt_tx_queue_intr_enable(if_ctx_t ctx, uint16_t qid);
184 static void bnxt_disable_intr(if_ctx_t ctx);
185 static int bnxt_msix_intr_assign(if_ctx_t ctx, int msix);
186
187 /* vlan support */
188 static void bnxt_vlan_register(if_ctx_t ctx, uint16_t vtag);
189 static void bnxt_vlan_unregister(if_ctx_t ctx, uint16_t vtag);
190
191 /* ioctl */
192 static int bnxt_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data);
193
194 static int bnxt_shutdown(if_ctx_t ctx);
195 static int bnxt_suspend(if_ctx_t ctx);
196 static int bnxt_resume(if_ctx_t ctx);
197
198 /* Internal support functions */
199 static int bnxt_probe_phy(struct bnxt_softc *softc);
200 static void bnxt_add_media_types(struct bnxt_softc *softc);
201 static int bnxt_pci_mapping(struct bnxt_softc *softc);
202 static void bnxt_pci_mapping_free(struct bnxt_softc *softc);
203 static int bnxt_update_link(struct bnxt_softc *softc, bool chng_link_state);
204 static int bnxt_handle_def_cp(void *arg);
205 static int bnxt_handle_rx_cp(void *arg);
206 static void bnxt_clear_ids(struct bnxt_softc *softc);
207 static void inline bnxt_do_enable_intr(struct bnxt_cp_ring *cpr);
208 static void inline bnxt_do_disable_intr(struct bnxt_cp_ring *cpr);
209 static void bnxt_mark_cpr_invalid(struct bnxt_cp_ring *cpr);
210 static void bnxt_def_cp_task(void *context);
211 static void bnxt_handle_async_event(struct bnxt_softc *softc,
212     struct cmpl_base *cmpl);
213 static uint8_t get_phy_type(struct bnxt_softc *softc);
214 static uint64_t bnxt_get_baudrate(struct bnxt_link_info *link);
215 static void bnxt_get_wol_settings(struct bnxt_softc *softc);
216 static int bnxt_wol_config(if_ctx_t ctx);
217
218 /*
219  * Device Interface Declaration
220  */
221
222 static device_method_t bnxt_methods[] = {
223         /* Device interface */
224         DEVMETHOD(device_register, bnxt_register),
225         DEVMETHOD(device_probe, iflib_device_probe),
226         DEVMETHOD(device_attach, iflib_device_attach),
227         DEVMETHOD(device_detach, iflib_device_detach),
228         DEVMETHOD(device_shutdown, iflib_device_shutdown),
229         DEVMETHOD(device_suspend, iflib_device_suspend),
230         DEVMETHOD(device_resume, iflib_device_resume),
231         DEVMETHOD_END
232 };
233
234 static driver_t bnxt_driver = {
235         "bnxt", bnxt_methods, sizeof(struct bnxt_softc),
236 };
237
238 devclass_t bnxt_devclass;
239 DRIVER_MODULE(bnxt, pci, bnxt_driver, bnxt_devclass, 0, 0);
240
241 MODULE_DEPEND(bnxt, pci, 1, 1, 1);
242 MODULE_DEPEND(bnxt, ether, 1, 1, 1);
243 MODULE_DEPEND(bnxt, iflib, 1, 1, 1);
244
245 static device_method_t bnxt_iflib_methods[] = {
246         DEVMETHOD(ifdi_tx_queues_alloc, bnxt_tx_queues_alloc),
247         DEVMETHOD(ifdi_rx_queues_alloc, bnxt_rx_queues_alloc),
248         DEVMETHOD(ifdi_queues_free, bnxt_queues_free),
249
250         DEVMETHOD(ifdi_attach_pre, bnxt_attach_pre),
251         DEVMETHOD(ifdi_attach_post, bnxt_attach_post),
252         DEVMETHOD(ifdi_detach, bnxt_detach),
253
254         DEVMETHOD(ifdi_init, bnxt_init),
255         DEVMETHOD(ifdi_stop, bnxt_stop),
256         DEVMETHOD(ifdi_multi_set, bnxt_multi_set),
257         DEVMETHOD(ifdi_mtu_set, bnxt_mtu_set),
258         DEVMETHOD(ifdi_media_status, bnxt_media_status),
259         DEVMETHOD(ifdi_media_change, bnxt_media_change),
260         DEVMETHOD(ifdi_promisc_set, bnxt_promisc_set),
261         DEVMETHOD(ifdi_get_counter, bnxt_get_counter),
262         DEVMETHOD(ifdi_update_admin_status, bnxt_update_admin_status),
263
264         DEVMETHOD(ifdi_intr_enable, bnxt_intr_enable),
265         DEVMETHOD(ifdi_tx_queue_intr_enable, bnxt_tx_queue_intr_enable),
266         DEVMETHOD(ifdi_rx_queue_intr_enable, bnxt_rx_queue_intr_enable),
267         DEVMETHOD(ifdi_intr_disable, bnxt_disable_intr),
268         DEVMETHOD(ifdi_msix_intr_assign, bnxt_msix_intr_assign),
269
270         DEVMETHOD(ifdi_vlan_register, bnxt_vlan_register),
271         DEVMETHOD(ifdi_vlan_unregister, bnxt_vlan_unregister),
272
273         DEVMETHOD(ifdi_priv_ioctl, bnxt_priv_ioctl),
274
275         DEVMETHOD(ifdi_suspend, bnxt_suspend),
276         DEVMETHOD(ifdi_shutdown, bnxt_shutdown),
277         DEVMETHOD(ifdi_resume, bnxt_resume),
278
279         DEVMETHOD_END
280 };
281
282 static driver_t bnxt_iflib_driver = {
283         "bnxt", bnxt_iflib_methods, sizeof(struct bnxt_softc)
284 };
285
286 /*
287  * iflib shared context
288  */
289
290 #define BNXT_DRIVER_VERSION     "1.0.0.1"
291 char bnxt_driver_version[] = BNXT_DRIVER_VERSION;
292 extern struct if_txrx bnxt_txrx;
293 static struct if_shared_ctx bnxt_sctx_init = {
294         .isc_magic = IFLIB_MAGIC,
295         .isc_driver = &bnxt_iflib_driver,
296         .isc_nfl = 2,                           // Number of Free Lists
297         .isc_flags = IFLIB_HAS_RXCQ | IFLIB_HAS_TXCQ,
298         .isc_q_align = PAGE_SIZE,
299         .isc_tx_maxsize = BNXT_TSO_SIZE,
300         .isc_tx_maxsegsize = BNXT_TSO_SIZE,
301         .isc_rx_maxsize = BNXT_TSO_SIZE,
302         .isc_rx_maxsegsize = BNXT_TSO_SIZE,
303
304         // Only use a single segment to avoid page size constraints
305         .isc_rx_nsegments = 1,
306         .isc_ntxqs = 2,
307         .isc_nrxqs = 3,
308         .isc_nrxd_min = {16, 16, 16},
309         .isc_nrxd_default = {PAGE_SIZE / sizeof(struct cmpl_base) * 8,
310             PAGE_SIZE / sizeof(struct rx_prod_pkt_bd),
311             PAGE_SIZE / sizeof(struct rx_prod_pkt_bd)},
312         .isc_nrxd_max = {INT32_MAX, INT32_MAX, INT32_MAX},
313         .isc_ntxd_min = {16, 16, 16},
314         .isc_ntxd_default = {PAGE_SIZE / sizeof(struct cmpl_base) * 2,
315             PAGE_SIZE / sizeof(struct tx_bd_short)},
316         .isc_ntxd_max = {INT32_MAX, INT32_MAX, INT32_MAX},
317
318         .isc_admin_intrcnt = 1,
319         .isc_vendor_info = bnxt_vendor_info_array,
320         .isc_driver_version = bnxt_driver_version,
321 };
322
323 if_shared_ctx_t bnxt_sctx = &bnxt_sctx_init;
324
325 /*
326  * Device Methods
327  */
328
329 static void *
330 bnxt_register(device_t dev)
331 {
332         return bnxt_sctx;
333 }
334
335 /*
336  * Device Dependent Configuration Functions
337 */
338
339 /* Soft queue setup and teardown */
340 static int
341 bnxt_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
342     uint64_t *paddrs, int ntxqs, int ntxqsets)
343 {
344         struct bnxt_softc *softc;
345         int i;
346         int rc;
347
348         softc = iflib_get_softc(ctx);
349
350         softc->tx_cp_rings = malloc(sizeof(struct bnxt_cp_ring) * ntxqsets,
351             M_DEVBUF, M_NOWAIT | M_ZERO);
352         if (!softc->tx_cp_rings) {
353                 device_printf(iflib_get_dev(ctx),
354                     "unable to allocate TX completion rings\n");
355                 rc = ENOMEM;
356                 goto cp_alloc_fail;
357         }
358         softc->tx_rings = malloc(sizeof(struct bnxt_ring) * ntxqsets,
359             M_DEVBUF, M_NOWAIT | M_ZERO);
360         if (!softc->tx_rings) {
361                 device_printf(iflib_get_dev(ctx),
362                     "unable to allocate TX rings\n");
363                 rc = ENOMEM;
364                 goto ring_alloc_fail;
365         }
366         rc = iflib_dma_alloc(ctx, sizeof(struct ctx_hw_stats) * ntxqsets,
367             &softc->tx_stats, 0);
368         if (rc)
369                 goto dma_alloc_fail;
370         bus_dmamap_sync(softc->tx_stats.idi_tag, softc->tx_stats.idi_map,
371             BUS_DMASYNC_PREREAD);
372
373         for (i = 0; i < ntxqsets; i++) {
374                 /* Set up the completion ring */
375                 softc->tx_cp_rings[i].stats_ctx_id = HWRM_NA_SIGNATURE;
376                 softc->tx_cp_rings[i].ring.phys_id =
377                     (uint16_t)HWRM_NA_SIGNATURE;
378                 softc->tx_cp_rings[i].ring.softc = softc;
379                 softc->tx_cp_rings[i].ring.id =
380                     (softc->scctx->isc_nrxqsets * 2) + 1 + i;
381                 softc->tx_cp_rings[i].ring.doorbell =
382                     softc->tx_cp_rings[i].ring.id * 0x80;
383                 softc->tx_cp_rings[i].ring.ring_size =
384                     softc->scctx->isc_ntxd[0];
385                 softc->tx_cp_rings[i].ring.vaddr = vaddrs[i * ntxqs];
386                 softc->tx_cp_rings[i].ring.paddr = paddrs[i * ntxqs];
387
388                 /* Set up the TX ring */
389                 softc->tx_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
390                 softc->tx_rings[i].softc = softc;
391                 softc->tx_rings[i].id =
392                     (softc->scctx->isc_nrxqsets * 2) + 1 + i;
393                 softc->tx_rings[i].doorbell = softc->tx_rings[i].id * 0x80;
394                 softc->tx_rings[i].ring_size = softc->scctx->isc_ntxd[1];
395                 softc->tx_rings[i].vaddr = vaddrs[i * ntxqs + 1];
396                 softc->tx_rings[i].paddr = paddrs[i * ntxqs + 1];
397
398                 bnxt_create_tx_sysctls(softc, i);
399         }
400
401         softc->ntxqsets = ntxqsets;
402         return rc;
403
404 dma_alloc_fail:
405         free(softc->tx_rings, M_DEVBUF);
406 ring_alloc_fail:
407         free(softc->tx_cp_rings, M_DEVBUF);
408 cp_alloc_fail:
409         return rc;
410 }
411
412 static void
413 bnxt_queues_free(if_ctx_t ctx)
414 {
415         struct bnxt_softc *softc = iflib_get_softc(ctx);
416
417         // Free TX queues
418         iflib_dma_free(&softc->tx_stats);
419         free(softc->tx_rings, M_DEVBUF);
420         softc->tx_rings = NULL;
421         free(softc->tx_cp_rings, M_DEVBUF);
422         softc->tx_cp_rings = NULL;
423         softc->ntxqsets = 0;
424
425         // Free RX queues
426         iflib_dma_free(&softc->rx_stats);
427         free(softc->grp_info, M_DEVBUF);
428         free(softc->ag_rings, M_DEVBUF);
429         free(softc->rx_rings, M_DEVBUF);
430         free(softc->rx_cp_rings, M_DEVBUF);
431 }
432
433 static int
434 bnxt_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs,
435     uint64_t *paddrs, int nrxqs, int nrxqsets)
436 {
437         struct bnxt_softc *softc;
438         int i;
439         int rc;
440
441         softc = iflib_get_softc(ctx);
442
443         softc->rx_cp_rings = malloc(sizeof(struct bnxt_cp_ring) * nrxqsets,
444             M_DEVBUF, M_NOWAIT | M_ZERO);
445         if (!softc->rx_cp_rings) {
446                 device_printf(iflib_get_dev(ctx),
447                     "unable to allocate RX completion rings\n");
448                 rc = ENOMEM;
449                 goto cp_alloc_fail;
450         }
451         softc->rx_rings = malloc(sizeof(struct bnxt_ring) * nrxqsets,
452             M_DEVBUF, M_NOWAIT | M_ZERO);
453         if (!softc->rx_rings) {
454                 device_printf(iflib_get_dev(ctx),
455                     "unable to allocate RX rings\n");
456                 rc = ENOMEM;
457                 goto ring_alloc_fail;
458         }
459         softc->ag_rings = malloc(sizeof(struct bnxt_ring) * nrxqsets,
460             M_DEVBUF, M_NOWAIT | M_ZERO);
461         if (!softc->ag_rings) {
462                 device_printf(iflib_get_dev(ctx),
463                     "unable to allocate aggregation rings\n");
464                 rc = ENOMEM;
465                 goto ag_alloc_fail;
466         }
467         softc->grp_info = malloc(sizeof(struct bnxt_grp_info) * nrxqsets,
468             M_DEVBUF, M_NOWAIT | M_ZERO);
469         if (!softc->grp_info) {
470                 device_printf(iflib_get_dev(ctx),
471                     "unable to allocate ring groups\n");
472                 rc = ENOMEM;
473                 goto grp_alloc_fail;
474         }
475
476         rc = iflib_dma_alloc(ctx, sizeof(struct ctx_hw_stats) * nrxqsets,
477             &softc->rx_stats, 0);
478         if (rc)
479                 goto hw_stats_alloc_fail;
480         bus_dmamap_sync(softc->rx_stats.idi_tag, softc->rx_stats.idi_map,
481             BUS_DMASYNC_PREREAD);
482
483         for (i = 0; i < nrxqsets; i++) {
484                 /* Allocation the completion ring */
485                 softc->rx_cp_rings[i].stats_ctx_id = HWRM_NA_SIGNATURE;
486                 softc->rx_cp_rings[i].ring.phys_id =
487                     (uint16_t)HWRM_NA_SIGNATURE;
488                 softc->rx_cp_rings[i].ring.softc = softc;
489                 softc->rx_cp_rings[i].ring.id = i + 1;
490                 softc->rx_cp_rings[i].ring.doorbell =
491                     softc->rx_cp_rings[i].ring.id * 0x80;
492                 /*
493                  * If this ring overflows, RX stops working.
494                  */
495                 softc->rx_cp_rings[i].ring.ring_size =
496                     softc->scctx->isc_nrxd[0];
497                 softc->rx_cp_rings[i].ring.vaddr = vaddrs[i * nrxqs];
498                 softc->rx_cp_rings[i].ring.paddr = paddrs[i * nrxqs];
499
500                 /* Allocate the RX ring */
501                 softc->rx_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
502                 softc->rx_rings[i].softc = softc;
503                 softc->rx_rings[i].id = i + 1;
504                 softc->rx_rings[i].doorbell = softc->rx_rings[i].id * 0x80;
505                 softc->rx_rings[i].ring_size = softc->scctx->isc_nrxd[1];
506                 softc->rx_rings[i].vaddr = vaddrs[i * nrxqs + 1];
507                 softc->rx_rings[i].paddr = paddrs[i * nrxqs + 1];
508
509                 /* Allocate the TPA start buffer */
510                 softc->rx_rings[i].tpa_start = malloc(sizeof(struct bnxt_full_tpa_start) *
511                         (RX_TPA_START_CMPL_AGG_ID_MASK >> RX_TPA_START_CMPL_AGG_ID_SFT),
512                         M_DEVBUF, M_NOWAIT | M_ZERO);
513                 if (softc->rx_rings[i].tpa_start == NULL) {
514                         rc = -ENOMEM;
515                         device_printf(softc->dev,
516                                         "Unable to allocate space for TPA\n");
517                         goto tpa_alloc_fail;
518                 }
519
520                 /* Allocate the AG ring */
521                 softc->ag_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
522                 softc->ag_rings[i].softc = softc;
523                 softc->ag_rings[i].id = nrxqsets + i + 1;
524                 softc->ag_rings[i].doorbell = softc->ag_rings[i].id * 0x80;
525                 softc->ag_rings[i].ring_size = softc->scctx->isc_nrxd[2];
526                 softc->ag_rings[i].vaddr = vaddrs[i * nrxqs + 2];
527                 softc->ag_rings[i].paddr = paddrs[i * nrxqs + 2];
528
529                 /* Allocate the ring group */
530                 softc->grp_info[i].grp_id = (uint16_t)HWRM_NA_SIGNATURE;
531                 softc->grp_info[i].stats_ctx =
532                     softc->rx_cp_rings[i].stats_ctx_id;
533                 softc->grp_info[i].rx_ring_id = softc->rx_rings[i].phys_id;
534                 softc->grp_info[i].ag_ring_id = softc->ag_rings[i].phys_id;
535                 softc->grp_info[i].cp_ring_id =
536                     softc->rx_cp_rings[i].ring.phys_id;
537
538                 bnxt_create_rx_sysctls(softc, i);
539         }
540
541         /* And finally, the VNIC */
542         softc->vnic_info.id = (uint16_t)HWRM_NA_SIGNATURE;
543         softc->vnic_info.flow_id = (uint16_t)HWRM_NA_SIGNATURE;
544         softc->vnic_info.filter_id = -1;
545         softc->vnic_info.def_ring_grp = (uint16_t)HWRM_NA_SIGNATURE;
546         softc->vnic_info.cos_rule = (uint16_t)HWRM_NA_SIGNATURE;
547         softc->vnic_info.lb_rule = (uint16_t)HWRM_NA_SIGNATURE;
548         softc->vnic_info.rx_mask = HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_BCAST;
549         softc->vnic_info.mc_list_count = 0;
550         softc->vnic_info.flags = BNXT_VNIC_FLAG_DEFAULT;
551         rc = iflib_dma_alloc(ctx, BNXT_MAX_MC_ADDRS * ETHER_ADDR_LEN,
552             &softc->vnic_info.mc_list, 0);
553         if (rc)
554                 goto mc_list_alloc_fail;
555
556         /* The VNIC RSS Hash Key */
557         rc = iflib_dma_alloc(ctx, HW_HASH_KEY_SIZE,
558             &softc->vnic_info.rss_hash_key_tbl, 0);
559         if (rc)
560                 goto rss_hash_alloc_fail;
561         bus_dmamap_sync(softc->vnic_info.rss_hash_key_tbl.idi_tag,
562             softc->vnic_info.rss_hash_key_tbl.idi_map,
563             BUS_DMASYNC_PREWRITE);
564         memcpy(softc->vnic_info.rss_hash_key_tbl.idi_vaddr,
565             softc->vnic_info.rss_hash_key, HW_HASH_KEY_SIZE);
566
567         /* Allocate the RSS tables */
568         rc = iflib_dma_alloc(ctx, HW_HASH_INDEX_SIZE * sizeof(uint16_t),
569             &softc->vnic_info.rss_grp_tbl, 0);
570         if (rc)
571                 goto rss_grp_alloc_fail;
572         bus_dmamap_sync(softc->vnic_info.rss_grp_tbl.idi_tag,
573             softc->vnic_info.rss_grp_tbl.idi_map,
574             BUS_DMASYNC_PREWRITE);
575         memset(softc->vnic_info.rss_grp_tbl.idi_vaddr, 0xff,
576             softc->vnic_info.rss_grp_tbl.idi_size);
577
578         softc->nrxqsets = nrxqsets;
579         return rc;
580
581 rss_grp_alloc_fail:
582         iflib_dma_free(&softc->vnic_info.rss_hash_key_tbl);
583 rss_hash_alloc_fail:
584         iflib_dma_free(&softc->vnic_info.mc_list);
585 tpa_alloc_fail:
586 mc_list_alloc_fail:
587         for (i = i - 1; i >= 0; i--)
588                 free(softc->rx_rings[i].tpa_start, M_DEVBUF);
589         iflib_dma_free(&softc->rx_stats);
590 hw_stats_alloc_fail:
591         free(softc->grp_info, M_DEVBUF);
592 grp_alloc_fail:
593         free(softc->ag_rings, M_DEVBUF);
594 ag_alloc_fail:
595         free(softc->rx_rings, M_DEVBUF);
596 ring_alloc_fail:
597         free(softc->rx_cp_rings, M_DEVBUF);
598 cp_alloc_fail:
599         return rc;
600 }
601
602 /* Device setup and teardown */
603 static int
604 bnxt_attach_pre(if_ctx_t ctx)
605 {
606         struct bnxt_softc *softc = iflib_get_softc(ctx);
607         if_softc_ctx_t scctx;
608         int rc = 0;
609
610         softc->ctx = ctx;
611         softc->dev = iflib_get_dev(ctx);
612         softc->media = iflib_get_media(ctx);
613         softc->scctx = iflib_get_softc_ctx(ctx);
614         softc->sctx = iflib_get_sctx(ctx);
615         scctx = softc->scctx;
616
617         /* TODO: Better way of detecting NPAR/VF is needed */
618         switch (softc->sctx->isc_vendor_info->pvi_device_id) {
619         case BCM57402_NPAR:
620         case BCM57404_NPAR:
621         case BCM57406_NPAR:
622         case BCM57407_NPAR:
623         case BCM57412_NPAR1:
624         case BCM57412_NPAR2:
625         case BCM57414_NPAR1:
626         case BCM57414_NPAR2:
627         case BCM57416_NPAR1:
628         case BCM57416_NPAR2:
629                 softc->flags |= BNXT_FLAG_NPAR;
630                 break;
631         case NETXTREME_C_VF1:
632         case NETXTREME_C_VF2:
633         case NETXTREME_C_VF3:
634         case NETXTREME_E_VF1:
635         case NETXTREME_E_VF2:
636         case NETXTREME_E_VF3:
637                 softc->flags |= BNXT_FLAG_VF;
638                 break;
639         }
640
641         pci_enable_busmaster(softc->dev);
642
643         if (bnxt_pci_mapping(softc))
644                 return (ENXIO);
645
646         /* HWRM setup/init */
647         BNXT_HWRM_LOCK_INIT(softc, device_get_nameunit(softc->dev));
648         rc = bnxt_alloc_hwrm_dma_mem(softc);
649         if (rc)
650                 goto dma_fail;
651
652
653         /* Get firmware version and compare with driver */
654         softc->ver_info = malloc(sizeof(struct bnxt_ver_info),
655             M_DEVBUF, M_NOWAIT | M_ZERO);
656         if (softc->ver_info == NULL) {
657                 rc = ENOMEM;
658                 device_printf(softc->dev,
659                     "Unable to allocate space for version info\n");
660                 goto ver_alloc_fail;
661         }
662         /* Default minimum required HWRM version */
663         softc->ver_info->hwrm_min_major = 1;
664         softc->ver_info->hwrm_min_minor = 2;
665         softc->ver_info->hwrm_min_update = 2;
666
667         rc = bnxt_hwrm_ver_get(softc);
668         if (rc) {
669                 device_printf(softc->dev, "attach: hwrm ver get failed\n");
670                 goto ver_fail;
671         }
672
673         /* Get NVRAM info */
674         softc->nvm_info = malloc(sizeof(struct bnxt_nvram_info),
675             M_DEVBUF, M_NOWAIT | M_ZERO);
676         if (softc->nvm_info == NULL) {
677                 rc = ENOMEM;
678                 device_printf(softc->dev,
679                     "Unable to allocate space for NVRAM info\n");
680                 goto nvm_alloc_fail;
681         }
682         rc = bnxt_hwrm_nvm_get_dev_info(softc, &softc->nvm_info->mfg_id,
683             &softc->nvm_info->device_id, &softc->nvm_info->sector_size,
684             &softc->nvm_info->size, &softc->nvm_info->reserved_size,
685             &softc->nvm_info->available_size);
686
687         /* Register the driver with the FW */
688         rc = bnxt_hwrm_func_drv_rgtr(softc);
689         if (rc) {
690                 device_printf(softc->dev, "attach: hwrm drv rgtr failed\n");
691                 goto drv_rgtr_fail;
692         }
693
694         rc = bnxt_hwrm_func_rgtr_async_events(softc, NULL, 0);
695         if (rc) {
696                 device_printf(softc->dev, "attach: hwrm rgtr async evts failed\n");
697                 goto drv_rgtr_fail;
698         }
699
700         /* Get the HW capabilities */
701         rc = bnxt_hwrm_func_qcaps(softc);
702         if (rc)
703                 goto failed;
704
705         iflib_set_mac(ctx, softc->func.mac_addr);
706
707         scctx->isc_txrx = &bnxt_txrx;
708         scctx->isc_tx_csum_flags = (CSUM_IP | CSUM_TCP | CSUM_UDP |
709             CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO);
710         scctx->isc_capenable =
711             /* These are translated to hwassit bits */
712             IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 | IFCAP_TSO4 | IFCAP_TSO6 |
713             /* These are checked by iflib */
714             IFCAP_LRO | IFCAP_VLAN_HWFILTER |
715             /* These are part of the iflib mask */
716             IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_VLAN_MTU |
717             IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWTSO |
718             /* These likely get lost... */
719             IFCAP_VLAN_HWCSUM | IFCAP_JUMBO_MTU;
720
721         if (bnxt_wol_supported(softc))
722                 scctx->isc_capenable |= IFCAP_WOL_MAGIC;
723
724         /* Get the queue config */
725         rc = bnxt_hwrm_queue_qportcfg(softc);
726         if (rc) {
727                 device_printf(softc->dev, "attach: hwrm qportcfg failed\n");
728                 goto failed;
729         }
730
731         bnxt_get_wol_settings(softc);
732
733         /* Now perform a function reset */
734         rc = bnxt_hwrm_func_reset(softc);
735         bnxt_clear_ids(softc);
736         if (rc)
737                 goto failed;
738
739         /* Now set up iflib sc */
740         scctx->isc_tx_nsegments = 31,
741         scctx->isc_tx_tso_segments_max = 31;
742         scctx->isc_tx_tso_size_max = BNXT_TSO_SIZE;
743         scctx->isc_tx_tso_segsize_max = BNXT_TSO_SIZE;
744         scctx->isc_vectors = softc->func.max_cp_rings;
745         scctx->isc_txrx = &bnxt_txrx;
746
747         if (scctx->isc_nrxd[0] <
748             ((scctx->isc_nrxd[1] * 4) + scctx->isc_nrxd[2]))
749                 device_printf(softc->dev,
750                     "WARNING: nrxd0 (%d) should be at least 4 * nrxd1 (%d) + nrxd2 (%d).  Driver may be unstable\n",
751                     scctx->isc_nrxd[0], scctx->isc_nrxd[1], scctx->isc_nrxd[2]);
752         if (scctx->isc_ntxd[0] < scctx->isc_ntxd[1] * 2)
753                 device_printf(softc->dev,
754                     "WARNING: ntxd0 (%d) should be at least 2 * ntxd1 (%d).  Driver may be unstable\n",
755                     scctx->isc_ntxd[0], scctx->isc_ntxd[1]);
756         scctx->isc_txqsizes[0] = sizeof(struct cmpl_base) * scctx->isc_ntxd[0];
757         scctx->isc_txqsizes[1] = sizeof(struct tx_bd_short) *
758             scctx->isc_ntxd[1];
759         scctx->isc_rxqsizes[0] = sizeof(struct cmpl_base) * scctx->isc_nrxd[0];
760         scctx->isc_rxqsizes[1] = sizeof(struct rx_prod_pkt_bd) *
761             scctx->isc_nrxd[1];
762         scctx->isc_rxqsizes[2] = sizeof(struct rx_prod_pkt_bd) *
763             scctx->isc_nrxd[2];
764         scctx->isc_max_rxqsets = min(pci_msix_count(softc->dev)-1,
765             softc->func.max_cp_rings - 1);
766         scctx->isc_max_rxqsets = min(scctx->isc_max_rxqsets,
767             softc->func.max_rx_rings);
768         scctx->isc_max_txqsets = min(softc->func.max_rx_rings,
769             softc->func.max_cp_rings - scctx->isc_max_rxqsets - 1);
770         scctx->isc_rss_table_size = HW_HASH_INDEX_SIZE;
771         scctx->isc_rss_table_mask = scctx->isc_rss_table_size - 1;
772
773         /* iflib will map and release this bar */
774         scctx->isc_msix_bar = pci_msix_table_bar(softc->dev);
775
776         /* Allocate the default completion ring */
777         softc->def_cp_ring.stats_ctx_id = HWRM_NA_SIGNATURE;
778         softc->def_cp_ring.ring.phys_id = (uint16_t)HWRM_NA_SIGNATURE;
779         softc->def_cp_ring.ring.softc = softc;
780         softc->def_cp_ring.ring.id = 0;
781         softc->def_cp_ring.ring.doorbell = softc->def_cp_ring.ring.id * 0x80;
782         softc->def_cp_ring.ring.ring_size = PAGE_SIZE /
783             sizeof(struct cmpl_base);
784         rc = iflib_dma_alloc(ctx,
785             sizeof(struct cmpl_base) * softc->def_cp_ring.ring.ring_size,
786             &softc->def_cp_ring_mem, 0);
787         softc->def_cp_ring.ring.vaddr = softc->def_cp_ring_mem.idi_vaddr;
788         softc->def_cp_ring.ring.paddr = softc->def_cp_ring_mem.idi_paddr;
789         iflib_config_gtask_init(ctx, &softc->def_cp_task, bnxt_def_cp_task,
790             "dflt_cp");
791
792         rc = bnxt_init_sysctl_ctx(softc);
793         if (rc)
794                 goto init_sysctl_failed;
795         rc = bnxt_create_nvram_sysctls(softc->nvm_info);
796         if (rc)
797                 goto failed;
798
799         arc4rand(softc->vnic_info.rss_hash_key, HW_HASH_KEY_SIZE, 0);
800         softc->vnic_info.rss_hash_type =
801             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4 |
802             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4 |
803             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4 |
804             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6 |
805             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6 |
806             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
807         rc = bnxt_create_config_sysctls_pre(softc);
808         if (rc)
809                 goto failed;
810
811         /* Initialize the vlan list */
812         SLIST_INIT(&softc->vnic_info.vlan_tags);
813         softc->vnic_info.vlan_tag_list.idi_vaddr = NULL;
814
815         return (rc);
816
817 failed:
818         bnxt_free_sysctl_ctx(softc);
819 init_sysctl_failed:
820         bnxt_hwrm_func_drv_unrgtr(softc, false);
821 drv_rgtr_fail:
822         free(softc->nvm_info, M_DEVBUF);
823 nvm_alloc_fail:
824 ver_fail:
825         free(softc->ver_info, M_DEVBUF);
826 ver_alloc_fail:
827         bnxt_free_hwrm_dma_mem(softc);
828 dma_fail:
829         BNXT_HWRM_LOCK_DESTROY(softc);
830         bnxt_pci_mapping_free(softc);
831         pci_disable_busmaster(softc->dev);
832         return (rc);
833 }
834
835 static int
836 bnxt_attach_post(if_ctx_t ctx)
837 {
838         struct bnxt_softc *softc = iflib_get_softc(ctx);
839         if_t ifp = iflib_get_ifp(ctx);
840         int rc;
841
842         bnxt_create_config_sysctls_post(softc);
843
844         /* Update link state etc... */
845         rc = bnxt_probe_phy(softc);
846         if (rc)
847                 goto failed;
848
849         /* Needs to be done after probing the phy */
850         bnxt_create_ver_sysctls(softc);
851         bnxt_add_media_types(softc);
852         ifmedia_set(softc->media, IFM_ETHER | IFM_AUTO);
853
854         softc->scctx->isc_max_frame_size = ifp->if_mtu + ETHER_HDR_LEN +
855             ETHER_CRC_LEN;
856
857 failed:
858         return rc;
859 }
860
861 static int
862 bnxt_detach(if_ctx_t ctx)
863 {
864         struct bnxt_softc *softc = iflib_get_softc(ctx);
865         struct bnxt_vlan_tag *tag;
866         struct bnxt_vlan_tag *tmp;
867         int i;
868
869         bnxt_wol_config(ctx);
870         bnxt_do_disable_intr(&softc->def_cp_ring);
871         bnxt_free_sysctl_ctx(softc);
872         bnxt_hwrm_func_reset(softc);
873         bnxt_clear_ids(softc);
874         iflib_irq_free(ctx, &softc->def_cp_ring.irq);
875         iflib_config_gtask_deinit(&softc->def_cp_task);
876         /* We need to free() these here... */
877         for (i = softc->nrxqsets-1; i>=0; i--) {
878                 iflib_irq_free(ctx, &softc->rx_cp_rings[i].irq);
879         }
880         iflib_dma_free(&softc->vnic_info.mc_list);
881         iflib_dma_free(&softc->vnic_info.rss_hash_key_tbl);
882         iflib_dma_free(&softc->vnic_info.rss_grp_tbl);
883         if (softc->vnic_info.vlan_tag_list.idi_vaddr)
884                 iflib_dma_free(&softc->vnic_info.vlan_tag_list);
885         SLIST_FOREACH_SAFE(tag, &softc->vnic_info.vlan_tags, next, tmp)
886                 free(tag, M_DEVBUF);
887         iflib_dma_free(&softc->def_cp_ring_mem);
888         for (i = 0; i < softc->nrxqsets; i++)
889                 free(softc->rx_rings[i].tpa_start, M_DEVBUF);
890         free(softc->ver_info, M_DEVBUF);
891         free(softc->nvm_info, M_DEVBUF);
892
893         bnxt_hwrm_func_drv_unrgtr(softc, false);
894         bnxt_free_hwrm_dma_mem(softc);
895         BNXT_HWRM_LOCK_DESTROY(softc);
896
897         pci_disable_busmaster(softc->dev);
898         bnxt_pci_mapping_free(softc);
899
900         return 0;
901 }
902
903 /* Device configuration */
904 static void
905 bnxt_init(if_ctx_t ctx)
906 {
907         struct bnxt_softc *softc = iflib_get_softc(ctx);
908         struct ifmediareq ifmr;
909         int i, j;
910         int rc;
911
912         rc = bnxt_hwrm_func_reset(softc);
913         if (rc)
914                 return;
915         bnxt_clear_ids(softc);
916
917         /* Allocate the default completion ring */
918         softc->def_cp_ring.cons = UINT32_MAX;
919         softc->def_cp_ring.v_bit = 1;
920         bnxt_mark_cpr_invalid(&softc->def_cp_ring);
921         rc = bnxt_hwrm_ring_alloc(softc,
922             HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
923             &softc->def_cp_ring.ring,
924             (uint16_t)HWRM_NA_SIGNATURE,
925             HWRM_NA_SIGNATURE, true);
926         if (rc)
927                 goto fail;
928
929         /* And now set the default CP ring as the async CP ring */
930         rc = bnxt_hwrm_func_cfg(softc);
931         if (rc)
932                 goto fail;
933
934         for (i = 0; i < softc->nrxqsets; i++) {
935                 /* Allocate the statistics context */
936                 rc = bnxt_hwrm_stat_ctx_alloc(softc, &softc->rx_cp_rings[i],
937                     softc->rx_stats.idi_paddr +
938                     (sizeof(struct ctx_hw_stats) * i));
939                 if (rc)
940                         goto fail;
941
942                 /* Allocate the completion ring */
943                 softc->rx_cp_rings[i].cons = UINT32_MAX;
944                 softc->rx_cp_rings[i].v_bit = 1;
945                 softc->rx_cp_rings[i].last_idx = UINT32_MAX;
946                 bnxt_mark_cpr_invalid(&softc->rx_cp_rings[i]);
947                 rc = bnxt_hwrm_ring_alloc(softc,
948                     HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
949                     &softc->rx_cp_rings[i].ring, (uint16_t)HWRM_NA_SIGNATURE,
950                     HWRM_NA_SIGNATURE, true);
951                 if (rc)
952                         goto fail;
953
954                 /* Allocate the RX ring */
955                 rc = bnxt_hwrm_ring_alloc(softc,
956                     HWRM_RING_ALLOC_INPUT_RING_TYPE_RX,
957                     &softc->rx_rings[i], (uint16_t)HWRM_NA_SIGNATURE,
958                     HWRM_NA_SIGNATURE, false);
959                 if (rc)
960                         goto fail;
961                 BNXT_RX_DB(&softc->rx_rings[i], 0);
962                 /* TODO: Cumulus+ doesn't need the double doorbell */
963                 BNXT_RX_DB(&softc->rx_rings[i], 0);
964
965                 /* Allocate the AG ring */
966                 rc = bnxt_hwrm_ring_alloc(softc,
967                     HWRM_RING_ALLOC_INPUT_RING_TYPE_RX,
968                     &softc->ag_rings[i], (uint16_t)HWRM_NA_SIGNATURE,
969                     HWRM_NA_SIGNATURE, false);
970                 if (rc)
971                         goto fail;
972                 BNXT_RX_DB(&softc->rx_rings[i], 0);
973                 /* TODO: Cumulus+ doesn't need the double doorbell */
974                 BNXT_RX_DB(&softc->ag_rings[i], 0);
975
976                 /* Allocate the ring group */
977                 softc->grp_info[i].stats_ctx =
978                     softc->rx_cp_rings[i].stats_ctx_id;
979                 softc->grp_info[i].rx_ring_id = softc->rx_rings[i].phys_id;
980                 softc->grp_info[i].ag_ring_id = softc->ag_rings[i].phys_id;
981                 softc->grp_info[i].cp_ring_id =
982                     softc->rx_cp_rings[i].ring.phys_id;
983                 rc = bnxt_hwrm_ring_grp_alloc(softc, &softc->grp_info[i]);
984                 if (rc)
985                         goto fail;
986
987         }
988
989         /* Allocate the VNIC RSS context */
990         rc = bnxt_hwrm_vnic_ctx_alloc(softc, &softc->vnic_info.rss_id);
991         if (rc)
992                 goto fail;
993
994         /* Allocate the vnic */
995         softc->vnic_info.def_ring_grp = softc->grp_info[0].grp_id;
996         softc->vnic_info.mru = softc->scctx->isc_max_frame_size;
997         rc = bnxt_hwrm_vnic_alloc(softc, &softc->vnic_info);
998         if (rc)
999                 goto fail;
1000         rc = bnxt_hwrm_vnic_cfg(softc, &softc->vnic_info);
1001         if (rc)
1002                 goto fail;
1003         rc = bnxt_hwrm_set_filter(softc, &softc->vnic_info);
1004         if (rc)
1005                 goto fail;
1006
1007         /* Enable RSS on the VNICs */
1008         for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
1009                 ((uint16_t *)
1010                     softc->vnic_info.rss_grp_tbl.idi_vaddr)[i] =
1011                     htole16(softc->grp_info[j].grp_id);
1012                 if (++j == softc->nrxqsets)
1013                         j = 0;
1014         }
1015
1016         rc = bnxt_hwrm_rss_cfg(softc, &softc->vnic_info,
1017             softc->vnic_info.rss_hash_type);
1018         if (rc)
1019                 goto fail;
1020
1021         /* 
1022          * Enable LRO/TPA/GRO 
1023          * TBD: 
1024          *      Enable / Disable HW_LRO based on
1025          *      ifconfig lro / ifconfig -lro setting
1026          */
1027         rc = bnxt_hwrm_vnic_tpa_cfg(softc, &softc->vnic_info,
1028             (if_getcapenable(iflib_get_ifp(ctx)) & IFCAP_LRO) ?
1029             HWRM_VNIC_TPA_CFG_INPUT_FLAGS_TPA : 0);
1030         if (rc)
1031                 goto fail;
1032
1033         for (i = 0; i < softc->ntxqsets; i++) {
1034                 /* Allocate the statistics context */
1035                 rc = bnxt_hwrm_stat_ctx_alloc(softc, &softc->tx_cp_rings[i],
1036                     softc->tx_stats.idi_paddr +
1037                     (sizeof(struct ctx_hw_stats) * i));
1038                 if (rc)
1039                         goto fail;
1040
1041                 /* Allocate the completion ring */
1042                 softc->tx_cp_rings[i].cons = UINT32_MAX;
1043                 softc->tx_cp_rings[i].v_bit = 1;
1044                 bnxt_mark_cpr_invalid(&softc->tx_cp_rings[i]);
1045                 rc = bnxt_hwrm_ring_alloc(softc,
1046                     HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
1047                     &softc->tx_cp_rings[i].ring, (uint16_t)HWRM_NA_SIGNATURE,
1048                     HWRM_NA_SIGNATURE, false);
1049                 if (rc)
1050                         goto fail;
1051
1052                 /* Allocate the TX ring */
1053                 rc = bnxt_hwrm_ring_alloc(softc,
1054                     HWRM_RING_ALLOC_INPUT_RING_TYPE_TX,
1055                     &softc->tx_rings[i], softc->tx_cp_rings[i].ring.phys_id,
1056                     softc->tx_cp_rings[i].stats_ctx_id, false);
1057                 if (rc)
1058                         goto fail;
1059                 BNXT_TX_DB(&softc->tx_rings[i], 0);
1060                 /* TODO: Cumulus+ doesn't need the double doorbell */
1061                 BNXT_TX_DB(&softc->tx_rings[i], 0);
1062         }
1063
1064         bnxt_do_enable_intr(&softc->def_cp_ring);
1065         bnxt_media_status(softc->ctx, &ifmr);
1066         return;
1067
1068 fail:
1069         bnxt_hwrm_func_reset(softc);
1070         bnxt_clear_ids(softc);
1071         return;
1072 }
1073
1074 static void
1075 bnxt_stop(if_ctx_t ctx)
1076 {
1077         struct bnxt_softc *softc = iflib_get_softc(ctx);
1078
1079         bnxt_do_disable_intr(&softc->def_cp_ring);
1080         bnxt_hwrm_func_reset(softc);
1081         bnxt_clear_ids(softc);
1082         return;
1083 }
1084
1085 static void
1086 bnxt_multi_set(if_ctx_t ctx)
1087 {
1088         struct bnxt_softc *softc = iflib_get_softc(ctx);
1089         if_t ifp = iflib_get_ifp(ctx);
1090         uint8_t *mta;
1091         int cnt, mcnt;
1092
1093         mcnt = if_multiaddr_count(ifp, -1);
1094
1095         if (mcnt > BNXT_MAX_MC_ADDRS) {
1096                 softc->vnic_info.rx_mask |=
1097                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
1098                 bnxt_hwrm_cfa_l2_set_rx_mask(softc, &softc->vnic_info);
1099         }
1100         else {
1101                 softc->vnic_info.rx_mask &=
1102                     ~HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
1103                 mta = softc->vnic_info.mc_list.idi_vaddr;
1104                 bzero(mta, softc->vnic_info.mc_list.idi_size);
1105                 if_multiaddr_array(ifp, mta, &cnt, mcnt);
1106                 bus_dmamap_sync(softc->vnic_info.mc_list.idi_tag,
1107                     softc->vnic_info.mc_list.idi_map, BUS_DMASYNC_PREWRITE);
1108                 softc->vnic_info.mc_list_count = cnt;
1109                 softc->vnic_info.rx_mask |=
1110                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_MCAST;
1111                 if (bnxt_hwrm_cfa_l2_set_rx_mask(softc, &softc->vnic_info))
1112                         device_printf(softc->dev,
1113                             "set_multi: rx_mask set failed\n");
1114         }
1115 }
1116
1117 static int
1118 bnxt_mtu_set(if_ctx_t ctx, uint32_t mtu)
1119 {
1120         struct bnxt_softc *softc = iflib_get_softc(ctx);
1121
1122         if (mtu > BNXT_MAX_MTU)
1123                 return EINVAL;
1124
1125         softc->scctx->isc_max_frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1126         return 0;
1127 }
1128
1129 static void
1130 bnxt_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
1131 {
1132         struct bnxt_softc *softc = iflib_get_softc(ctx);
1133         struct bnxt_link_info *link_info = &softc->link_info;
1134         uint8_t phy_type = get_phy_type(softc);
1135
1136         bnxt_update_link(softc, true);
1137
1138         ifmr->ifm_status = IFM_AVALID;
1139         ifmr->ifm_active = IFM_ETHER;
1140
1141         if (link_info->link_up)
1142                 ifmr->ifm_status |= IFM_ACTIVE;
1143         else
1144                 ifmr->ifm_status &= ~IFM_ACTIVE;
1145
1146         if (link_info->duplex == HWRM_PORT_PHY_QCFG_OUTPUT_DUPLEX_FULL)
1147                 ifmr->ifm_active |= IFM_FDX;
1148         else
1149                 ifmr->ifm_active |= IFM_HDX;
1150
1151         switch (link_info->link_speed) {
1152         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB:
1153                 ifmr->ifm_active |= IFM_100_T;
1154                 break;
1155         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_1GB:
1156                 switch (phy_type) {
1157                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX:
1158                         ifmr->ifm_active |= IFM_1000_KX;
1159                         break;
1160                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET:
1161                         ifmr->ifm_active |= IFM_1000_T;
1162                         break;
1163                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_SGMIIEXTPHY:
1164                         ifmr->ifm_active |= IFM_1000_SGMII;
1165                         break;
1166                 default:
1167                         /*
1168                          * Workaround: 
1169                          *    Don't return IFM_UNKNOWN until 
1170                          *    Stratus return proper media_type 
1171                          */  
1172                         ifmr->ifm_active |= IFM_1000_KX;
1173                         break;
1174                 }
1175         break;
1176         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2_5GB:
1177                 switch (phy_type) {
1178                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX:
1179                         ifmr->ifm_active |= IFM_2500_KX;
1180                         break;
1181                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET:
1182                         ifmr->ifm_active |= IFM_2500_T;
1183                         break;
1184                 default:
1185                         ifmr->ifm_active |= IFM_UNKNOWN;
1186                         break;
1187                 }
1188                 break;
1189         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10GB:
1190                 switch (phy_type) {
1191                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1192                         ifmr->ifm_active |= IFM_10G_CR1;
1193                         break;
1194                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1195                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1196                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1197                         ifmr->ifm_active |= IFM_10G_KR;
1198                         break;
1199                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASELR:
1200                         ifmr->ifm_active |= IFM_10G_LR;
1201                         break;
1202                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
1203                         ifmr->ifm_active |= IFM_10G_SR;
1204                         break;
1205                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX:
1206                         ifmr->ifm_active |= IFM_10G_KX4;
1207                         break;
1208                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET:
1209                         ifmr->ifm_active |= IFM_10G_T;
1210                         break;
1211                 default:
1212                         /*
1213                          * Workaround: 
1214                          *    Don't return IFM_UNKNOWN until 
1215                          *    Stratus return proper media_type 
1216                          */  
1217                         ifmr->ifm_active |= IFM_10G_CR1;
1218                         break;
1219                 }
1220                 break;
1221         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_20GB:
1222                 ifmr->ifm_active |= IFM_20G_KR2;
1223                 break;
1224         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_25GB:
1225                 switch (phy_type) {
1226                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1227                         ifmr->ifm_active |= IFM_25G_CR;
1228                         break;
1229                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1230                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1231                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1232                         ifmr->ifm_active |= IFM_25G_KR;
1233                         break;
1234                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
1235                         ifmr->ifm_active |= IFM_25G_SR;
1236                         break;
1237                 default:
1238                         /*
1239                          * Workaround: 
1240                          *    Don't return IFM_UNKNOWN until 
1241                          *    Stratus return proper media_type 
1242                          */  
1243                         ifmr->ifm_active |= IFM_25G_CR;
1244                         break;
1245                 }
1246                 break;
1247         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_40GB:
1248                 switch (phy_type) {
1249                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1250                         ifmr->ifm_active |= IFM_40G_CR4;
1251                         break;
1252                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1253                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1254                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1255                         ifmr->ifm_active |= IFM_40G_KR4;
1256                         break;
1257                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASELR:
1258                         ifmr->ifm_active |= IFM_40G_LR4;
1259                         break;
1260                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
1261                         ifmr->ifm_active |= IFM_40G_SR4;
1262                         break;
1263                 default:
1264                         ifmr->ifm_active |= IFM_UNKNOWN;
1265                         break;
1266                 }
1267                 break;
1268         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_50GB:
1269                 switch (phy_type) {
1270                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1271                         ifmr->ifm_active |= IFM_50G_CR2;
1272                         break;
1273                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1274                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1275                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1276                         ifmr->ifm_active |= IFM_50G_KR2;
1277                         break;
1278                 default:
1279                         /*
1280                          * Workaround: 
1281                          *    Don't return IFM_UNKNOWN until 
1282                          *    Stratus return proper media_type 
1283                          */  
1284                         ifmr->ifm_active |= IFM_50G_CR2;
1285                         break;
1286                 }
1287                 break;
1288         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100GB:
1289                 switch (phy_type) {
1290                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1291                         ifmr->ifm_active |= IFM_100G_CR4;
1292                         break;
1293                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1294                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1295                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1296                         ifmr->ifm_active |= IFM_100G_KR4;
1297                         break;
1298                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASELR:
1299                         ifmr->ifm_active |= IFM_100G_LR4;
1300                         break;
1301                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
1302                         ifmr->ifm_active |= IFM_100G_SR4;
1303                         break;
1304                 default:
1305                         /*
1306                          * Workaround: 
1307                          *    Don't return IFM_UNKNOWN until 
1308                          *    Stratus return proper media_type 
1309                          */  
1310                         ifmr->ifm_active |= IFM_100G_CR4;
1311                         break;
1312                 }
1313         default:
1314                 return;
1315         }
1316
1317         if (link_info->pause == (HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
1318             HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX))
1319                 ifmr->ifm_active |= (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
1320         else if (link_info->pause == HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX)
1321                 ifmr->ifm_active |= IFM_ETH_TXPAUSE;
1322         else if (link_info->pause == HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX)
1323                 ifmr->ifm_active |= IFM_ETH_RXPAUSE;
1324
1325         bnxt_report_link(softc);
1326         return;
1327 }
1328
1329 static int
1330 bnxt_media_change(if_ctx_t ctx)
1331 {
1332         struct bnxt_softc *softc = iflib_get_softc(ctx);
1333         struct ifmedia *ifm = iflib_get_media(ctx);
1334         struct ifmediareq ifmr;
1335         int rc;
1336
1337         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1338                 return EINVAL;
1339
1340         switch (IFM_SUBTYPE(ifm->ifm_media)) {
1341         case IFM_100_T:
1342                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1343                 softc->link_info.req_link_speed =
1344                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_100MB;
1345                 break;
1346         case IFM_1000_KX:
1347         case IFM_1000_T:
1348         case IFM_1000_SGMII:
1349                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1350                 softc->link_info.req_link_speed =
1351                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_1GB;
1352                 break;
1353         case IFM_2500_KX:
1354         case IFM_2500_T:
1355                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1356                 softc->link_info.req_link_speed =
1357                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_2_5GB;
1358                 break;
1359         case IFM_10G_CR1:
1360         case IFM_10G_KR:
1361         case IFM_10G_LR:
1362         case IFM_10G_SR:
1363         case IFM_10G_T:
1364                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1365                 softc->link_info.req_link_speed =
1366                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_10GB;
1367                 break;
1368         case IFM_20G_KR2:
1369                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1370                 softc->link_info.req_link_speed =
1371                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_20GB;
1372                 break;
1373         case IFM_25G_CR:
1374         case IFM_25G_KR:
1375         case IFM_25G_SR:
1376                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1377                 softc->link_info.req_link_speed =
1378                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_25GB;
1379                 break;
1380         case IFM_40G_CR4:
1381         case IFM_40G_KR4:
1382         case IFM_40G_LR4:
1383         case IFM_40G_SR4:
1384                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1385                 softc->link_info.req_link_speed =
1386                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_40GB;
1387                 break;
1388         case IFM_50G_CR2:
1389         case IFM_50G_KR2:
1390                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1391                 softc->link_info.req_link_speed =
1392                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_50GB;
1393                 break;
1394         case IFM_100G_CR4:
1395         case IFM_100G_KR4:
1396         case IFM_100G_LR4:
1397         case IFM_100G_SR4:
1398                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1399                 softc->link_info.req_link_speed =
1400                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_100GB;
1401                 break;
1402         default:
1403                 device_printf(softc->dev,
1404                     "Unsupported media type!  Using auto\n");
1405                 /* Fall-through */
1406         case IFM_AUTO:
1407                 // Auto
1408                 softc->link_info.autoneg |= BNXT_AUTONEG_SPEED;
1409                 break;
1410         }
1411         rc = bnxt_hwrm_set_link_setting(softc, true, true);
1412         bnxt_media_status(softc->ctx, &ifmr);
1413         return rc;
1414 }
1415
1416 static int
1417 bnxt_promisc_set(if_ctx_t ctx, int flags)
1418 {
1419         struct bnxt_softc *softc = iflib_get_softc(ctx);
1420         if_t ifp = iflib_get_ifp(ctx);
1421         int rc;
1422
1423         if (ifp->if_flags & IFF_ALLMULTI ||
1424             if_multiaddr_count(ifp, -1) > BNXT_MAX_MC_ADDRS)
1425                 softc->vnic_info.rx_mask |=
1426                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
1427         else
1428                 softc->vnic_info.rx_mask &=
1429                     ~HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
1430
1431         if (ifp->if_flags & IFF_PROMISC)
1432                 softc->vnic_info.rx_mask |=
1433                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_PROMISCUOUS |
1434                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ANYVLAN_NONVLAN;
1435         else
1436                 softc->vnic_info.rx_mask &=
1437                     ~(HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_PROMISCUOUS |
1438                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ANYVLAN_NONVLAN);
1439
1440         rc = bnxt_hwrm_cfa_l2_set_rx_mask(softc, &softc->vnic_info);
1441
1442         return rc;
1443 }
1444
1445 static uint64_t
1446 bnxt_get_counter(if_ctx_t ctx, ift_counter cnt)
1447 {
1448         if_t ifp = iflib_get_ifp(ctx);
1449
1450         if (cnt < IFCOUNTERS)
1451                 return if_get_counter_default(ifp, cnt);
1452
1453         return 0;
1454 }
1455
1456 static void
1457 bnxt_update_admin_status(if_ctx_t ctx)
1458 {
1459         /* TODO: do we need to do anything here? */
1460         return;
1461 }
1462
1463 static void inline
1464 bnxt_do_enable_intr(struct bnxt_cp_ring *cpr)
1465 {
1466         if (cpr->ring.phys_id != (uint16_t)HWRM_NA_SIGNATURE) {
1467                 /* First time enabling, do not set index */
1468                 if (cpr->cons == UINT32_MAX)
1469                         BNXT_CP_ENABLE_DB(&cpr->ring);
1470                 else
1471                         BNXT_CP_IDX_ENABLE_DB(&cpr->ring, cpr->cons);
1472         }
1473 }
1474
1475 static void inline
1476 bnxt_do_disable_intr(struct bnxt_cp_ring *cpr)
1477 {
1478         if (cpr->ring.phys_id != (uint16_t)HWRM_NA_SIGNATURE)
1479                 BNXT_CP_DISABLE_DB(&cpr->ring);
1480 }
1481
1482 /* Enable all interrupts */
1483 static void
1484 bnxt_intr_enable(if_ctx_t ctx)
1485 {
1486         struct bnxt_softc *softc = iflib_get_softc(ctx);
1487         int i;
1488
1489         bnxt_do_enable_intr(&softc->def_cp_ring);
1490         for (i = 0; i < softc->nrxqsets; i++)
1491                 bnxt_do_enable_intr(&softc->rx_cp_rings[i]);
1492
1493         return;
1494 }
1495
1496 /* Enable interrupt for a single queue */
1497 static int
1498 bnxt_tx_queue_intr_enable(if_ctx_t ctx, uint16_t qid)
1499 {
1500         struct bnxt_softc *softc = iflib_get_softc(ctx);
1501
1502         bnxt_do_enable_intr(&softc->tx_cp_rings[qid]);
1503         return 0;
1504 }
1505
1506 static int
1507 bnxt_rx_queue_intr_enable(if_ctx_t ctx, uint16_t qid)
1508 {
1509         struct bnxt_softc *softc = iflib_get_softc(ctx);
1510
1511         bnxt_do_enable_intr(&softc->rx_cp_rings[qid]);
1512         return 0;
1513 }
1514
1515 /* Disable all interrupts */
1516 static void
1517 bnxt_disable_intr(if_ctx_t ctx)
1518 {
1519         struct bnxt_softc *softc = iflib_get_softc(ctx);
1520         int i;
1521
1522         /*
1523          * NOTE: These TX interrupts should never get enabled, so don't
1524          * update the index
1525          */
1526         for (i = 0; i < softc->ntxqsets; i++)
1527                 bnxt_do_disable_intr(&softc->tx_cp_rings[i]);
1528         for (i = 0; i < softc->nrxqsets; i++)
1529                 bnxt_do_disable_intr(&softc->rx_cp_rings[i]);
1530
1531         return;
1532 }
1533
1534 static int
1535 bnxt_msix_intr_assign(if_ctx_t ctx, int msix)
1536 {
1537         struct bnxt_softc *softc = iflib_get_softc(ctx);
1538         int rc;
1539         int i;
1540         char irq_name[16];
1541
1542         rc = iflib_irq_alloc_generic(ctx, &softc->def_cp_ring.irq,
1543             softc->def_cp_ring.ring.id + 1, IFLIB_INTR_ADMIN,
1544             bnxt_handle_def_cp, softc, 0, "def_cp");
1545         if (rc) {
1546                 device_printf(iflib_get_dev(ctx),
1547                     "Failed to register default completion ring handler\n");
1548                 return rc;
1549         }
1550
1551         for (i=0; i<softc->scctx->isc_nrxqsets; i++) {
1552                 snprintf(irq_name, sizeof(irq_name), "rxq%d", i);
1553                 rc = iflib_irq_alloc_generic(ctx, &softc->rx_cp_rings[i].irq,
1554                     softc->rx_cp_rings[i].ring.id + 1, IFLIB_INTR_RX,
1555                     bnxt_handle_rx_cp, &softc->rx_cp_rings[i], i, irq_name);
1556                 if (rc) {
1557                         device_printf(iflib_get_dev(ctx),
1558                             "Failed to register RX completion ring handler\n");
1559                         i--;
1560                         goto fail;
1561                 }
1562         }
1563
1564         for (i=0; i<softc->scctx->isc_ntxqsets; i++)
1565                 iflib_softirq_alloc_generic(ctx, i + 1, IFLIB_INTR_TX, NULL, i,
1566                     "tx_cp");
1567
1568         return rc;
1569
1570 fail:
1571         for (; i>=0; i--)
1572                 iflib_irq_free(ctx, &softc->rx_cp_rings[i].irq);
1573         iflib_irq_free(ctx, &softc->def_cp_ring.irq);
1574         return rc;
1575 }
1576
1577 /*
1578  * We're explicitly allowing duplicates here.  They will need to be
1579  * removed as many times as they are added.
1580  */
1581 static void
1582 bnxt_vlan_register(if_ctx_t ctx, uint16_t vtag)
1583 {
1584         struct bnxt_softc *softc = iflib_get_softc(ctx);
1585         struct bnxt_vlan_tag *new_tag;
1586
1587         new_tag = malloc(sizeof(struct bnxt_vlan_tag), M_DEVBUF, M_NOWAIT);
1588         if (new_tag == NULL)
1589                 return;
1590         new_tag->tag = vtag;
1591         new_tag->tpid = 8100;
1592         SLIST_INSERT_HEAD(&softc->vnic_info.vlan_tags, new_tag, next);
1593 };
1594
1595 static void
1596 bnxt_vlan_unregister(if_ctx_t ctx, uint16_t vtag)
1597 {
1598         struct bnxt_softc *softc = iflib_get_softc(ctx);
1599         struct bnxt_vlan_tag *vlan_tag;
1600
1601         SLIST_FOREACH(vlan_tag, &softc->vnic_info.vlan_tags, next) {
1602                 if (vlan_tag->tag == vtag) {
1603                         SLIST_REMOVE(&softc->vnic_info.vlan_tags, vlan_tag,
1604                             bnxt_vlan_tag, next);
1605                         free(vlan_tag, M_DEVBUF);
1606                         break;
1607                 }
1608         }
1609 }
1610
1611 static int
1612 bnxt_wol_config(if_ctx_t ctx)
1613 {
1614         struct bnxt_softc *softc = iflib_get_softc(ctx);
1615         if_t ifp = iflib_get_ifp(ctx);
1616
1617         if (!softc)
1618                 return -EBUSY;
1619
1620         if (!bnxt_wol_supported(softc))
1621                 return -ENOTSUP;
1622
1623         if (if_getcapabilities(ifp) & IFCAP_WOL_MAGIC) {
1624                 if (!softc->wol) {
1625                         if (bnxt_hwrm_alloc_wol_fltr(softc))
1626                                 return -EBUSY;
1627                         softc->wol = 1;
1628                 }
1629         } else {
1630                 if (softc->wol) {
1631                         if (bnxt_hwrm_free_wol_fltr(softc))
1632                                 return -EBUSY;
1633                         softc->wol = 0;
1634                 }
1635         }
1636
1637         return 0;
1638 }
1639
1640 static int
1641 bnxt_shutdown(if_ctx_t ctx)
1642 {
1643         bnxt_wol_config(ctx);
1644         return 0;
1645 }
1646
1647 static int
1648 bnxt_suspend(if_ctx_t ctx)
1649 {
1650         bnxt_wol_config(ctx);
1651         return 0;
1652 }
1653
1654 static int
1655 bnxt_resume(if_ctx_t ctx)
1656 {
1657         struct bnxt_softc *softc = iflib_get_softc(ctx);
1658
1659         bnxt_get_wol_settings(softc);
1660         return 0;
1661 }
1662
1663 static int
1664 bnxt_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data)
1665 {
1666         struct bnxt_softc *softc = iflib_get_softc(ctx);
1667         struct ifreq *ifr = (struct ifreq *)data;
1668         struct ifreq_buffer *ifbuf = &ifr->ifr_ifru.ifru_buffer;
1669         struct bnxt_ioctl_header *ioh =
1670             (struct bnxt_ioctl_header *)(ifbuf->buffer);
1671         int rc = ENOTSUP;
1672         struct bnxt_ioctl_data *iod = NULL;
1673
1674         switch (command) {
1675         case SIOCGPRIVATE_0:
1676                 if ((rc = priv_check(curthread, PRIV_DRIVER)) != 0)
1677                         goto exit;
1678
1679                 iod = malloc(ifbuf->length, M_DEVBUF, M_NOWAIT | M_ZERO);
1680                 if (!iod) {
1681                         rc = ENOMEM;
1682                         goto exit;
1683                 }
1684                 copyin(ioh, iod, ifbuf->length);
1685
1686                 switch (ioh->type) {
1687                 case BNXT_HWRM_NVM_FIND_DIR_ENTRY:
1688                 {
1689                         struct bnxt_ioctl_hwrm_nvm_find_dir_entry *find =
1690                             &iod->find;
1691
1692                         rc = bnxt_hwrm_nvm_find_dir_entry(softc, find->type,
1693                             &find->ordinal, find->ext, &find->index,
1694                             find->use_index, find->search_opt,
1695                             &find->data_length, &find->item_length,
1696                             &find->fw_ver);
1697                         if (rc) {
1698                                 iod->hdr.rc = rc;
1699                                 copyout(&iod->hdr.rc, &ioh->rc,
1700                                     sizeof(ioh->rc));
1701                         }
1702                         else {
1703                                 iod->hdr.rc = 0;
1704                                 copyout(iod, ioh, ifbuf->length);
1705                         }
1706
1707                         rc = 0;
1708                         goto exit;
1709                 }
1710                 case BNXT_HWRM_NVM_READ:
1711                 {
1712                         struct bnxt_ioctl_hwrm_nvm_read *rd = &iod->read;
1713                         struct iflib_dma_info dma_data;
1714                         size_t offset;
1715                         size_t remain;
1716                         size_t csize;
1717
1718                         /*
1719                          * Some HWRM versions can't read more than 0x8000 bytes
1720                          */
1721                         rc = iflib_dma_alloc(softc->ctx,
1722                             min(rd->length, 0x8000), &dma_data, BUS_DMA_NOWAIT);
1723                         if (rc)
1724                                 break;
1725                         for (remain = rd->length, offset = 0;
1726                             remain && offset < rd->length; offset += 0x8000) {
1727                                 csize = min(remain, 0x8000);
1728                                 rc = bnxt_hwrm_nvm_read(softc, rd->index,
1729                                     rd->offset + offset, csize, &dma_data);
1730                                 if (rc) {
1731                                         iod->hdr.rc = rc;
1732                                         copyout(&iod->hdr.rc, &ioh->rc,
1733                                             sizeof(ioh->rc));
1734                                         break;
1735                                 }
1736                                 else {
1737                                         copyout(dma_data.idi_vaddr,
1738                                             rd->data + offset, csize);
1739                                         iod->hdr.rc = 0;
1740                                 }
1741                                 remain -= csize;
1742                         }
1743                         if (iod->hdr.rc == 0)
1744                                 copyout(iod, ioh, ifbuf->length);
1745
1746                         iflib_dma_free(&dma_data);
1747                         rc = 0;
1748                         goto exit;
1749                 }
1750                 case BNXT_HWRM_FW_RESET:
1751                 {
1752                         struct bnxt_ioctl_hwrm_fw_reset *rst =
1753                             &iod->reset;
1754
1755                         rc = bnxt_hwrm_fw_reset(softc, rst->processor,
1756                             &rst->selfreset);
1757                         if (rc) {
1758                                 iod->hdr.rc = rc;
1759                                 copyout(&iod->hdr.rc, &ioh->rc,
1760                                     sizeof(ioh->rc));
1761                         }
1762                         else {
1763                                 iod->hdr.rc = 0;
1764                                 copyout(iod, ioh, ifbuf->length);
1765                         }
1766
1767                         rc = 0;
1768                         goto exit;
1769                 }
1770                 case BNXT_HWRM_FW_QSTATUS:
1771                 {
1772                         struct bnxt_ioctl_hwrm_fw_qstatus *qstat =
1773                             &iod->status;
1774
1775                         rc = bnxt_hwrm_fw_qstatus(softc, qstat->processor,
1776                             &qstat->selfreset);
1777                         if (rc) {
1778                                 iod->hdr.rc = rc;
1779                                 copyout(&iod->hdr.rc, &ioh->rc,
1780                                     sizeof(ioh->rc));
1781                         }
1782                         else {
1783                                 iod->hdr.rc = 0;
1784                                 copyout(iod, ioh, ifbuf->length);
1785                         }
1786
1787                         rc = 0;
1788                         goto exit;
1789                 }
1790                 case BNXT_HWRM_NVM_WRITE:
1791                 {
1792                         struct bnxt_ioctl_hwrm_nvm_write *wr =
1793                             &iod->write;
1794
1795                         rc = bnxt_hwrm_nvm_write(softc, wr->data, true,
1796                             wr->type, wr->ordinal, wr->ext, wr->attr,
1797                             wr->option, wr->data_length, wr->keep,
1798                             &wr->item_length, &wr->index);
1799                         if (rc) {
1800                                 iod->hdr.rc = rc;
1801                                 copyout(&iod->hdr.rc, &ioh->rc,
1802                                     sizeof(ioh->rc));
1803                         }
1804                         else {
1805                                 iod->hdr.rc = 0;
1806                                 copyout(iod, ioh, ifbuf->length);
1807                         }
1808
1809                         rc = 0;
1810                         goto exit;
1811                 }
1812                 case BNXT_HWRM_NVM_ERASE_DIR_ENTRY:
1813                 {
1814                         struct bnxt_ioctl_hwrm_nvm_erase_dir_entry *erase =
1815                             &iod->erase;
1816
1817                         rc = bnxt_hwrm_nvm_erase_dir_entry(softc, erase->index);
1818                         if (rc) {
1819                                 iod->hdr.rc = rc;
1820                                 copyout(&iod->hdr.rc, &ioh->rc,
1821                                     sizeof(ioh->rc));
1822                         }
1823                         else {
1824                                 iod->hdr.rc = 0;
1825                                 copyout(iod, ioh, ifbuf->length);
1826                         }
1827
1828                         rc = 0;
1829                         goto exit;
1830                 }
1831                 case BNXT_HWRM_NVM_GET_DIR_INFO:
1832                 {
1833                         struct bnxt_ioctl_hwrm_nvm_get_dir_info *info =
1834                             &iod->dir_info;
1835
1836                         rc = bnxt_hwrm_nvm_get_dir_info(softc, &info->entries,
1837                             &info->entry_length);
1838                         if (rc) {
1839                                 iod->hdr.rc = rc;
1840                                 copyout(&iod->hdr.rc, &ioh->rc,
1841                                     sizeof(ioh->rc));
1842                         }
1843                         else {
1844                                 iod->hdr.rc = 0;
1845                                 copyout(iod, ioh, ifbuf->length);
1846                         }
1847
1848                         rc = 0;
1849                         goto exit;
1850                 }
1851                 case BNXT_HWRM_NVM_GET_DIR_ENTRIES:
1852                 {
1853                         struct bnxt_ioctl_hwrm_nvm_get_dir_entries *get =
1854                             &iod->dir_entries;
1855                         struct iflib_dma_info dma_data;
1856
1857                         rc = iflib_dma_alloc(softc->ctx, get->max_size,
1858                             &dma_data, BUS_DMA_NOWAIT);
1859                         if (rc)
1860                                 break;
1861                         rc = bnxt_hwrm_nvm_get_dir_entries(softc, &get->entries,
1862                             &get->entry_length, &dma_data);
1863                         if (rc) {
1864                                 iod->hdr.rc = rc;
1865                                 copyout(&iod->hdr.rc, &ioh->rc,
1866                                     sizeof(ioh->rc));
1867                         }
1868                         else {
1869                                 copyout(dma_data.idi_vaddr, get->data,
1870                                     get->entry_length * get->entries);
1871                                 iod->hdr.rc = 0;
1872                                 copyout(iod, ioh, ifbuf->length);
1873                         }
1874                         iflib_dma_free(&dma_data);
1875
1876                         rc = 0;
1877                         goto exit;
1878                 }
1879                 case BNXT_HWRM_NVM_VERIFY_UPDATE:
1880                 {
1881                         struct bnxt_ioctl_hwrm_nvm_verify_update *vrfy =
1882                             &iod->verify;
1883
1884                         rc = bnxt_hwrm_nvm_verify_update(softc, vrfy->type,
1885                             vrfy->ordinal, vrfy->ext);
1886                         if (rc) {
1887                                 iod->hdr.rc = rc;
1888                                 copyout(&iod->hdr.rc, &ioh->rc,
1889                                     sizeof(ioh->rc));
1890                         }
1891                         else {
1892                                 iod->hdr.rc = 0;
1893                                 copyout(iod, ioh, ifbuf->length);
1894                         }
1895
1896                         rc = 0;
1897                         goto exit;
1898                 }
1899                 case BNXT_HWRM_NVM_INSTALL_UPDATE:
1900                 {
1901                         struct bnxt_ioctl_hwrm_nvm_install_update *inst =
1902                             &iod->install;
1903
1904                         rc = bnxt_hwrm_nvm_install_update(softc,
1905                             inst->install_type, &inst->installed_items,
1906                             &inst->result, &inst->problem_item,
1907                             &inst->reset_required);
1908                         if (rc) {
1909                                 iod->hdr.rc = rc;
1910                                 copyout(&iod->hdr.rc, &ioh->rc,
1911                                     sizeof(ioh->rc));
1912                         }
1913                         else {
1914                                 iod->hdr.rc = 0;
1915                                 copyout(iod, ioh, ifbuf->length);
1916                         }
1917
1918                         rc = 0;
1919                         goto exit;
1920                 }
1921                 case BNXT_HWRM_NVM_MODIFY:
1922                 {
1923                         struct bnxt_ioctl_hwrm_nvm_modify *mod = &iod->modify;
1924
1925                         rc = bnxt_hwrm_nvm_modify(softc, mod->index,
1926                             mod->offset, mod->data, true, mod->length);
1927                         if (rc) {
1928                                 iod->hdr.rc = rc;
1929                                 copyout(&iod->hdr.rc, &ioh->rc,
1930                                     sizeof(ioh->rc));
1931                         }
1932                         else {
1933                                 iod->hdr.rc = 0;
1934                                 copyout(iod, ioh, ifbuf->length);
1935                         }
1936
1937                         rc = 0;
1938                         goto exit;
1939                 }
1940                 case BNXT_HWRM_FW_GET_TIME:
1941                 {
1942                         struct bnxt_ioctl_hwrm_fw_get_time *gtm =
1943                             &iod->get_time;
1944
1945                         rc = bnxt_hwrm_fw_get_time(softc, &gtm->year,
1946                             &gtm->month, &gtm->day, &gtm->hour, &gtm->minute,
1947                             &gtm->second, &gtm->millisecond, &gtm->zone);
1948                         if (rc) {
1949                                 iod->hdr.rc = rc;
1950                                 copyout(&iod->hdr.rc, &ioh->rc,
1951                                     sizeof(ioh->rc));
1952                         }
1953                         else {
1954                                 iod->hdr.rc = 0;
1955                                 copyout(iod, ioh, ifbuf->length);
1956                         }
1957
1958                         rc = 0;
1959                         goto exit;
1960                 }
1961                 case BNXT_HWRM_FW_SET_TIME:
1962                 {
1963                         struct bnxt_ioctl_hwrm_fw_set_time *stm =
1964                             &iod->set_time;
1965
1966                         rc = bnxt_hwrm_fw_set_time(softc, stm->year,
1967                             stm->month, stm->day, stm->hour, stm->minute,
1968                             stm->second, stm->millisecond, stm->zone);
1969                         if (rc) {
1970                                 iod->hdr.rc = rc;
1971                                 copyout(&iod->hdr.rc, &ioh->rc,
1972                                     sizeof(ioh->rc));
1973                         }
1974                         else {
1975                                 iod->hdr.rc = 0;
1976                                 copyout(iod, ioh, ifbuf->length);
1977                         }
1978
1979                         rc = 0;
1980                         goto exit;
1981                 }
1982                 }
1983                 break;
1984         }
1985
1986 exit:
1987         if (iod)
1988                 free(iod, M_DEVBUF);
1989         return rc;
1990 }
1991
1992 /*
1993  * Support functions
1994  */
1995 static int
1996 bnxt_probe_phy(struct bnxt_softc *softc)
1997 {
1998         struct bnxt_link_info *link_info = &softc->link_info;
1999         int rc = 0;
2000
2001         rc = bnxt_update_link(softc, false);
2002         if (rc) {
2003                 device_printf(softc->dev,
2004                     "Probe phy can't update link (rc: %x)\n", rc);
2005                 return (rc);
2006         }
2007
2008         /*initialize the ethool setting copy with NVM settings */
2009         if (link_info->auto_mode != HWRM_PORT_PHY_QCFG_OUTPUT_AUTO_MODE_NONE)
2010                 link_info->autoneg |= BNXT_AUTONEG_SPEED;
2011
2012         if (link_info->auto_pause & (HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2013             HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX)) {
2014                 if (link_info->auto_pause == (
2015                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2016                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX))
2017                         link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
2018                 link_info->req_flow_ctrl = link_info->auto_pause;
2019         } else if (link_info->force_pause & (
2020             HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2021             HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX)) {
2022                 link_info->req_flow_ctrl = link_info->force_pause;
2023         }
2024         link_info->req_duplex = link_info->duplex_setting;
2025         if (link_info->autoneg & BNXT_AUTONEG_SPEED)
2026                 link_info->req_link_speed = link_info->auto_link_speed;
2027         else
2028                 link_info->req_link_speed = link_info->force_link_speed;
2029         return (rc);
2030 }
2031
2032 static void
2033 bnxt_add_media_types(struct bnxt_softc *softc)
2034 {
2035         struct bnxt_link_info *link_info = &softc->link_info;
2036         uint16_t supported;
2037         uint8_t phy_type = get_phy_type(softc);
2038
2039         supported = link_info->support_speeds;
2040
2041         /* Auto is always supported */
2042         ifmedia_add(softc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2043
2044         if (softc->flags & BNXT_FLAG_NPAR)
2045                 return;
2046
2047         switch (phy_type) {
2048         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
2049                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
2050                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_CR4, 0,
2051                             NULL);
2052                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_50GB)
2053                         ifmedia_add(softc->media, IFM_ETHER | IFM_50G_CR2, 0,
2054                             NULL);
2055                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
2056                         ifmedia_add(softc->media, IFM_ETHER | IFM_40G_CR4, 0,
2057                             NULL);
2058                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_25GB)
2059                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_CR, 0,
2060                             NULL);
2061                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2062                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_CR1, 0,
2063                             NULL);
2064                 break;
2065         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
2066         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
2067         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
2068                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
2069                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_KR4, 0,
2070                             NULL);
2071                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_50GB)
2072                         ifmedia_add(softc->media, IFM_ETHER | IFM_50G_KR2, 0,
2073                             NULL);
2074                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
2075                         ifmedia_add(softc->media, IFM_ETHER | IFM_40G_KR4, 0,
2076                             NULL);
2077                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_25GB)
2078                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_KR, 0,
2079                             NULL);
2080                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_20GB)
2081                         ifmedia_add(softc->media, IFM_ETHER | IFM_20G_KR2, 0,
2082                             NULL);
2083                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2084                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_KR, 0,
2085                             NULL);
2086                 break;
2087         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASELR:
2088                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
2089                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_LR4, 0,
2090                             NULL);
2091                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
2092                         ifmedia_add(softc->media, IFM_ETHER | IFM_40G_LR4, 0,
2093                             NULL);
2094                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2095                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_LR, 0,
2096                             NULL);
2097                 break;
2098         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
2099                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
2100                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_SR4, 0,
2101                             NULL);
2102                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
2103                         ifmedia_add(softc->media, IFM_ETHER | IFM_40G_SR4, 0,
2104                             NULL);
2105                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_25GB)
2106                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_SR, 0,
2107                             NULL);
2108                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2109                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_SR, 0,
2110                             NULL);
2111                 break;
2112         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX:
2113                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2114                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_KX4, 0,
2115                             NULL);
2116                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_2_5GB)
2117                         ifmedia_add(softc->media, IFM_ETHER | IFM_2500_KX, 0,
2118                             NULL);
2119                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_1GB)
2120                         ifmedia_add(softc->media, IFM_ETHER | IFM_1000_KX, 0,
2121                             NULL);
2122                 break;
2123         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET:
2124         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASETE:
2125                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10MB)
2126                         ifmedia_add(softc->media, IFM_ETHER | IFM_10_T, 0,
2127                             NULL);
2128                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100MB)
2129                         ifmedia_add(softc->media, IFM_ETHER | IFM_100_T, 0,
2130                             NULL);
2131                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_1GB)
2132                         ifmedia_add(softc->media, IFM_ETHER | IFM_1000_T, 0,
2133                             NULL);
2134                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_2_5GB)
2135                         ifmedia_add(softc->media, IFM_ETHER | IFM_2500_T, 0,
2136                             NULL);
2137                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2138                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_T, 0,
2139                             NULL);
2140                 break;
2141         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_SGMIIEXTPHY:
2142                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_1GB)
2143                         ifmedia_add(softc->media, IFM_ETHER | IFM_1000_SGMII, 0,
2144                             NULL);
2145                 break;
2146         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_UNKNOWN:
2147         default:
2148                 /*
2149                  * Workaround for Cumulus & Stratus 
2150                  *  For Stratus: 
2151                  *      media_type is being returned as 0x0
2152                  *      Return support speeds as 10G, 25G, 50G & 100G
2153                  *
2154                  *  For Cumulus: 
2155                  *      phy_type is being returned as 0x14 (PHY_TYPE_40G_BASECR4)
2156                  *      Return support speeds as 1G, 10G, 25G & 50G
2157                  */
2158                 if (pci_get_device(softc->dev) == BCM57454) {
2159                         /* For Stratus: 10G, 25G, 50G & 100G */
2160                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_CR4, 0, NULL);
2161                         ifmedia_add(softc->media, IFM_ETHER | IFM_50G_CR2, 0, NULL);
2162                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_CR, 0, NULL);
2163                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_CR1, 0, NULL);
2164                 } else if (pci_get_device(softc->dev) == BCM57414) {
2165                         /* For Cumulus: 1G, 10G, 25G & 50G */
2166                         ifmedia_add(softc->media, IFM_ETHER | IFM_50G_CR2, 0, NULL);
2167                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_CR, 0, NULL);
2168                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_CR1, 0, NULL);
2169                         ifmedia_add(softc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
2170                 } 
2171                 break;
2172         }
2173
2174         return;
2175 }
2176
2177 static int
2178 bnxt_map_bar(struct bnxt_softc *softc, struct bnxt_bar_info *bar, int bar_num, bool shareable)
2179 {
2180         uint32_t        flag;
2181
2182         if (bar->res != NULL) {
2183                 device_printf(softc->dev, "Bar %d already mapped\n", bar_num);
2184                 return EDOOFUS;
2185         }
2186
2187         bar->rid = PCIR_BAR(bar_num);
2188         flag = RF_ACTIVE;
2189         if (shareable)
2190                 flag |= RF_SHAREABLE;
2191
2192         if ((bar->res =
2193                 bus_alloc_resource_any(softc->dev,
2194                            SYS_RES_MEMORY,
2195                            &bar->rid,
2196                            flag)) == NULL) {
2197                 device_printf(softc->dev,
2198                     "PCI BAR%d mapping failure\n", bar_num);
2199                 return (ENXIO);
2200         }
2201         bar->tag = rman_get_bustag(bar->res);
2202         bar->handle = rman_get_bushandle(bar->res);
2203         bar->size = rman_get_size(bar->res);
2204
2205         return 0;
2206 }
2207
2208 static int
2209 bnxt_pci_mapping(struct bnxt_softc *softc)
2210 {
2211         int rc;
2212
2213         rc = bnxt_map_bar(softc, &softc->hwrm_bar, 0, true);
2214         if (rc)
2215                 return rc;
2216
2217         rc = bnxt_map_bar(softc, &softc->doorbell_bar, 2, false);
2218
2219         return rc;
2220 }
2221
2222 static void
2223 bnxt_pci_mapping_free(struct bnxt_softc *softc)
2224 {
2225         if (softc->hwrm_bar.res != NULL)
2226                 bus_release_resource(softc->dev, SYS_RES_MEMORY,
2227                     softc->hwrm_bar.rid, softc->hwrm_bar.res);
2228         softc->hwrm_bar.res = NULL;
2229
2230         if (softc->doorbell_bar.res != NULL)
2231                 bus_release_resource(softc->dev, SYS_RES_MEMORY,
2232                     softc->doorbell_bar.rid, softc->doorbell_bar.res);
2233         softc->doorbell_bar.res = NULL;
2234 }
2235
2236 static int
2237 bnxt_update_link(struct bnxt_softc *softc, bool chng_link_state)
2238 {
2239         struct bnxt_link_info *link_info = &softc->link_info;
2240         uint8_t link_up = link_info->link_up;
2241         int rc = 0;
2242
2243         rc = bnxt_hwrm_port_phy_qcfg(softc);
2244         if (rc)
2245                 goto exit;
2246
2247         /* TODO: need to add more logic to report VF link */
2248         if (chng_link_state) {
2249                 if (link_info->phy_link_status ==
2250                     HWRM_PORT_PHY_QCFG_OUTPUT_LINK_LINK)
2251                         link_info->link_up = 1;
2252                 else
2253                         link_info->link_up = 0;
2254                 if (link_up != link_info->link_up)
2255                         bnxt_report_link(softc);
2256         } else {
2257                 /* always link down if not require to update link state */
2258                 link_info->link_up = 0;
2259         }
2260
2261 exit:
2262         return rc;
2263 }
2264
2265 void
2266 bnxt_report_link(struct bnxt_softc *softc)
2267 {
2268         const char *duplex = NULL, *flow_ctrl = NULL;
2269
2270         if (softc->link_info.link_up == softc->link_info.last_link_up) {
2271                 if (!softc->link_info.link_up)
2272                         return;
2273                 if (softc->link_info.pause == softc->link_info.last_pause &&
2274                     softc->link_info.duplex == softc->link_info.last_duplex)
2275                         return;
2276         }
2277
2278         if (softc->link_info.link_up) {
2279                 if (softc->link_info.duplex ==
2280                     HWRM_PORT_PHY_QCFG_OUTPUT_DUPLEX_FULL)
2281                         duplex = "full duplex";
2282                 else
2283                         duplex = "half duplex";
2284                 if (softc->link_info.pause == (
2285                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2286                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX))
2287                         flow_ctrl = "FC - receive & transmit";
2288                 else if (softc->link_info.pause ==
2289                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX)
2290                         flow_ctrl = "FC - transmit";
2291                 else if (softc->link_info.pause ==
2292                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX)
2293                         flow_ctrl = "FC - receive";
2294                 else
2295                         flow_ctrl = "FC - none";
2296                 iflib_link_state_change(softc->ctx, LINK_STATE_UP,
2297                     IF_Gbps(100));
2298                 device_printf(softc->dev, "Link is UP %s, %s - %d Mbps \n", duplex,
2299                     flow_ctrl, (softc->link_info.link_speed * 100));
2300         } else {
2301                 iflib_link_state_change(softc->ctx, LINK_STATE_DOWN,
2302                     bnxt_get_baudrate(&softc->link_info));
2303                 device_printf(softc->dev, "Link is Down\n");
2304         }
2305
2306         softc->link_info.last_link_up = softc->link_info.link_up;
2307         softc->link_info.last_pause = softc->link_info.pause;
2308         softc->link_info.last_duplex = softc->link_info.duplex;
2309 }
2310
2311 static int
2312 bnxt_handle_rx_cp(void *arg)
2313 {
2314         struct bnxt_cp_ring *cpr = arg;
2315
2316         /* Disable further interrupts for this queue */
2317         BNXT_CP_DISABLE_DB(&cpr->ring);
2318         return FILTER_SCHEDULE_THREAD;
2319 }
2320
2321 static int
2322 bnxt_handle_def_cp(void *arg)
2323 {
2324         struct bnxt_softc *softc = arg;
2325
2326         BNXT_CP_DISABLE_DB(&softc->def_cp_ring.ring);
2327         GROUPTASK_ENQUEUE(&softc->def_cp_task);
2328         return FILTER_HANDLED;
2329 }
2330
2331 static void
2332 bnxt_clear_ids(struct bnxt_softc *softc)
2333 {
2334         int i;
2335
2336         softc->def_cp_ring.stats_ctx_id = HWRM_NA_SIGNATURE;
2337         softc->def_cp_ring.ring.phys_id = (uint16_t)HWRM_NA_SIGNATURE;
2338         for (i = 0; i < softc->ntxqsets; i++) {
2339                 softc->tx_cp_rings[i].stats_ctx_id = HWRM_NA_SIGNATURE;
2340                 softc->tx_cp_rings[i].ring.phys_id =
2341                     (uint16_t)HWRM_NA_SIGNATURE;
2342                 softc->tx_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
2343         }
2344         for (i = 0; i < softc->nrxqsets; i++) {
2345                 softc->rx_cp_rings[i].stats_ctx_id = HWRM_NA_SIGNATURE;
2346                 softc->rx_cp_rings[i].ring.phys_id =
2347                     (uint16_t)HWRM_NA_SIGNATURE;
2348                 softc->rx_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
2349                 softc->ag_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
2350                 softc->grp_info[i].grp_id = (uint16_t)HWRM_NA_SIGNATURE;
2351         }
2352         softc->vnic_info.filter_id = -1;
2353         softc->vnic_info.id = (uint16_t)HWRM_NA_SIGNATURE;
2354         softc->vnic_info.rss_id = (uint16_t)HWRM_NA_SIGNATURE;
2355         memset(softc->vnic_info.rss_grp_tbl.idi_vaddr, 0xff,
2356             softc->vnic_info.rss_grp_tbl.idi_size);
2357 }
2358
2359 static void
2360 bnxt_mark_cpr_invalid(struct bnxt_cp_ring *cpr)
2361 {
2362         struct cmpl_base *cmp = (void *)cpr->ring.vaddr;
2363         int i;
2364
2365         for (i = 0; i < cpr->ring.ring_size; i++)
2366                 cmp[i].info3_v = !cpr->v_bit;
2367 }
2368
2369 static void
2370 bnxt_handle_async_event(struct bnxt_softc *softc, struct cmpl_base *cmpl)
2371 {
2372         struct hwrm_async_event_cmpl *ae = (void *)cmpl;
2373         uint16_t async_id = le16toh(ae->event_id);
2374         struct ifmediareq ifmr;
2375
2376         switch (async_id) {
2377         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2378         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2379         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE:
2380                 bnxt_media_status(softc->ctx, &ifmr);
2381                 break;
2382         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_MTU_CHANGE:
2383         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DCB_CONFIG_CHANGE:
2384         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED:
2385         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_NOT_ALLOWED:
2386         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_FUNC_DRVR_UNLOAD:
2387         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_FUNC_DRVR_LOAD:
2388         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2389         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_LOAD:
2390         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_FLR:
2391         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_MAC_ADDR_CHANGE:
2392         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_VF_COMM_STATUS_CHANGE:
2393         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2394         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_HWRM_ERROR:
2395                 device_printf(softc->dev,
2396                     "Unhandled async completion type %u\n", async_id);
2397                 break;
2398         default:
2399                 device_printf(softc->dev,
2400                     "Unknown async completion type %u\n", async_id);
2401                 break;
2402         }
2403 }
2404
2405 static void
2406 bnxt_def_cp_task(void *context)
2407 {
2408         if_ctx_t ctx = context;
2409         struct bnxt_softc *softc = iflib_get_softc(ctx);
2410         struct bnxt_cp_ring *cpr = &softc->def_cp_ring;
2411
2412         /* Handle completions on the default completion ring */
2413         struct cmpl_base *cmpl;
2414         uint32_t cons = cpr->cons;
2415         bool v_bit = cpr->v_bit;
2416         bool last_v_bit;
2417         uint32_t last_cons;
2418         uint16_t type;
2419
2420         for (;;) {
2421                 last_cons = cons;
2422                 last_v_bit = v_bit;
2423                 NEXT_CP_CONS_V(&cpr->ring, cons, v_bit);
2424                 cmpl = &((struct cmpl_base *)cpr->ring.vaddr)[cons];
2425
2426                 if (!CMP_VALID(cmpl, v_bit))
2427                         break;
2428
2429                 type = le16toh(cmpl->type) & CMPL_BASE_TYPE_MASK;
2430                 switch (type) {
2431                 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2432                         bnxt_handle_async_event(softc, cmpl);
2433                         break;
2434                 case CMPL_BASE_TYPE_TX_L2:
2435                 case CMPL_BASE_TYPE_RX_L2:
2436                 case CMPL_BASE_TYPE_RX_AGG:
2437                 case CMPL_BASE_TYPE_RX_TPA_START:
2438                 case CMPL_BASE_TYPE_RX_TPA_END:
2439                 case CMPL_BASE_TYPE_STAT_EJECT:
2440                 case CMPL_BASE_TYPE_HWRM_DONE:
2441                 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2442                 case CMPL_BASE_TYPE_HWRM_FWD_RESP:
2443                 case CMPL_BASE_TYPE_CQ_NOTIFICATION:
2444                 case CMPL_BASE_TYPE_SRQ_EVENT:
2445                 case CMPL_BASE_TYPE_DBQ_EVENT:
2446                 case CMPL_BASE_TYPE_QP_EVENT:
2447                 case CMPL_BASE_TYPE_FUNC_EVENT:
2448                         device_printf(softc->dev,
2449                             "Unhandled completion type %u\n", type);
2450                         break;
2451                 default:
2452                         device_printf(softc->dev,
2453                             "Unknown completion type %u\n", type);
2454                         break;
2455                 }
2456         }
2457
2458         cpr->cons = last_cons;
2459         cpr->v_bit = last_v_bit;
2460         BNXT_CP_IDX_ENABLE_DB(&cpr->ring, cpr->cons);
2461 }
2462
2463 static uint8_t
2464 get_phy_type(struct bnxt_softc *softc)
2465 {
2466         struct bnxt_link_info *link_info = &softc->link_info;
2467         uint8_t phy_type = link_info->phy_type;
2468         uint16_t supported;
2469
2470         if (phy_type != HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_UNKNOWN)
2471                 return phy_type;
2472
2473         /* Deduce the phy type from the media type and supported speeds */
2474         supported = link_info->support_speeds;
2475
2476         if (link_info->media_type ==
2477             HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_TP)
2478                 return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET;
2479         if (link_info->media_type ==
2480             HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_DAC) {
2481                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_2_5GB)
2482                         return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX;
2483                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_20GB)
2484                         return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR;
2485                 return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR;
2486         }
2487         if (link_info->media_type ==
2488             HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_FIBRE)
2489                 return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR;
2490
2491         return phy_type;
2492 }
2493
2494 bool
2495 bnxt_check_hwrm_version(struct bnxt_softc *softc)
2496 {
2497         char buf[16];
2498
2499         sprintf(buf, "%hhu.%hhu.%hhu", softc->ver_info->hwrm_min_major,
2500             softc->ver_info->hwrm_min_minor, softc->ver_info->hwrm_min_update);
2501         if (softc->ver_info->hwrm_min_major > softc->ver_info->hwrm_if_major) {
2502                 device_printf(softc->dev,
2503                     "WARNING: HWRM version %s is too old (older than %s)\n",
2504                     softc->ver_info->hwrm_if_ver, buf);
2505                 return false;
2506         }
2507         else if(softc->ver_info->hwrm_min_major ==
2508             softc->ver_info->hwrm_if_major) {
2509                 if (softc->ver_info->hwrm_min_minor >
2510                     softc->ver_info->hwrm_if_minor) {
2511                         device_printf(softc->dev,
2512                             "WARNING: HWRM version %s is too old (older than %s)\n",
2513                             softc->ver_info->hwrm_if_ver, buf);
2514                         return false;
2515                 }
2516                 else if (softc->ver_info->hwrm_min_minor ==
2517                     softc->ver_info->hwrm_if_minor) {
2518                         if (softc->ver_info->hwrm_min_update >
2519                             softc->ver_info->hwrm_if_update) {
2520                                 device_printf(softc->dev,
2521                                     "WARNING: HWRM version %s is too old (older than %s)\n",
2522                                     softc->ver_info->hwrm_if_ver, buf);
2523                                 return false;
2524                         }
2525                 }
2526         }
2527         return true;
2528 }
2529
2530 static uint64_t
2531 bnxt_get_baudrate(struct bnxt_link_info *link)
2532 {
2533         switch (link->link_speed) {
2534         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB:
2535                 return IF_Mbps(100);
2536         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_1GB:
2537                 return IF_Gbps(1);
2538         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2GB:
2539                 return IF_Gbps(2);
2540         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2_5GB:
2541                 return IF_Mbps(2500);
2542         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10GB:
2543                 return IF_Gbps(10);
2544         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_20GB:
2545                 return IF_Gbps(20);
2546         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_25GB:
2547                 return IF_Gbps(25);
2548         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_40GB:
2549                 return IF_Gbps(40);
2550         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_50GB:
2551                 return IF_Gbps(50);
2552         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100GB:
2553                 return IF_Gbps(100);
2554         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10MB:
2555                 return IF_Mbps(10);
2556         }
2557         return IF_Gbps(100);
2558 }
2559
2560 static void
2561 bnxt_get_wol_settings(struct bnxt_softc *softc)
2562 {
2563         uint16_t wol_handle = 0;
2564
2565         if (!bnxt_wol_supported(softc))
2566                 return;
2567
2568         do {
2569                 wol_handle = bnxt_hwrm_get_wol_fltrs(softc, wol_handle);
2570         } while (wol_handle && wol_handle != BNXT_NO_MORE_WOL_FILTERS);
2571 }