]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/bnxt/if_bnxt.c
Import bmake-20170720
[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         /* Get the HW capabilities */
695         rc = bnxt_hwrm_func_qcaps(softc);
696         if (rc)
697                 goto failed;
698
699         iflib_set_mac(ctx, softc->func.mac_addr);
700
701         scctx->isc_txrx = &bnxt_txrx;
702         scctx->isc_tx_csum_flags = (CSUM_IP | CSUM_TCP | CSUM_UDP |
703             CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_TSO);
704         scctx->isc_capenable =
705             /* These are translated to hwassit bits */
706             IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 | IFCAP_TSO4 | IFCAP_TSO6 |
707             /* These are checked by iflib */
708             IFCAP_LRO | IFCAP_VLAN_HWFILTER |
709             /* These are part of the iflib mask */
710             IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | IFCAP_VLAN_MTU |
711             IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWTSO |
712             /* These likely get lost... */
713             IFCAP_VLAN_HWCSUM | IFCAP_JUMBO_MTU;
714
715         if (bnxt_wol_supported(softc))
716                 scctx->isc_capenable |= IFCAP_WOL_MAGIC;
717
718         /* Get the queue config */
719         rc = bnxt_hwrm_queue_qportcfg(softc);
720         if (rc) {
721                 device_printf(softc->dev, "attach: hwrm qportcfg failed\n");
722                 goto failed;
723         }
724
725         bnxt_get_wol_settings(softc);
726
727         /* Now perform a function reset */
728         rc = bnxt_hwrm_func_reset(softc);
729         bnxt_clear_ids(softc);
730         if (rc)
731                 goto failed;
732
733         /* Now set up iflib sc */
734         scctx->isc_tx_nsegments = 31,
735         scctx->isc_tx_tso_segments_max = 31;
736         scctx->isc_tx_tso_size_max = BNXT_TSO_SIZE;
737         scctx->isc_tx_tso_segsize_max = BNXT_TSO_SIZE;
738         scctx->isc_vectors = softc->func.max_cp_rings;
739         scctx->isc_txrx = &bnxt_txrx;
740
741         if (scctx->isc_nrxd[0] <
742             ((scctx->isc_nrxd[1] * 4) + scctx->isc_nrxd[2]))
743                 device_printf(softc->dev,
744                     "WARNING: nrxd0 (%d) should be at least 4 * nrxd1 (%d) + nrxd2 (%d).  Driver may be unstable\n",
745                     scctx->isc_nrxd[0], scctx->isc_nrxd[1], scctx->isc_nrxd[2]);
746         if (scctx->isc_ntxd[0] < scctx->isc_ntxd[1] * 2)
747                 device_printf(softc->dev,
748                     "WARNING: ntxd0 (%d) should be at least 2 * ntxd1 (%d).  Driver may be unstable\n",
749                     scctx->isc_ntxd[0], scctx->isc_ntxd[1]);
750         scctx->isc_txqsizes[0] = sizeof(struct cmpl_base) * scctx->isc_ntxd[0];
751         scctx->isc_txqsizes[1] = sizeof(struct tx_bd_short) *
752             scctx->isc_ntxd[1];
753         scctx->isc_rxqsizes[0] = sizeof(struct cmpl_base) * scctx->isc_nrxd[0];
754         scctx->isc_rxqsizes[1] = sizeof(struct rx_prod_pkt_bd) *
755             scctx->isc_nrxd[1];
756         scctx->isc_rxqsizes[2] = sizeof(struct rx_prod_pkt_bd) *
757             scctx->isc_nrxd[2];
758         scctx->isc_max_rxqsets = min(pci_msix_count(softc->dev)-1,
759             softc->func.max_cp_rings - 1);
760         scctx->isc_max_rxqsets = min(scctx->isc_max_rxqsets,
761             softc->func.max_rx_rings);
762         scctx->isc_max_txqsets = min(softc->func.max_rx_rings,
763             softc->func.max_cp_rings - scctx->isc_max_rxqsets - 1);
764         scctx->isc_rss_table_size = HW_HASH_INDEX_SIZE;
765         scctx->isc_rss_table_mask = scctx->isc_rss_table_size - 1;
766
767         /* iflib will map and release this bar */
768         scctx->isc_msix_bar = pci_msix_table_bar(softc->dev);
769
770         /* Allocate the default completion ring */
771         softc->def_cp_ring.stats_ctx_id = HWRM_NA_SIGNATURE;
772         softc->def_cp_ring.ring.phys_id = (uint16_t)HWRM_NA_SIGNATURE;
773         softc->def_cp_ring.ring.softc = softc;
774         softc->def_cp_ring.ring.id = 0;
775         softc->def_cp_ring.ring.doorbell = softc->def_cp_ring.ring.id * 0x80;
776         softc->def_cp_ring.ring.ring_size = PAGE_SIZE /
777             sizeof(struct cmpl_base);
778         rc = iflib_dma_alloc(ctx,
779             sizeof(struct cmpl_base) * softc->def_cp_ring.ring.ring_size,
780             &softc->def_cp_ring_mem, 0);
781         softc->def_cp_ring.ring.vaddr = softc->def_cp_ring_mem.idi_vaddr;
782         softc->def_cp_ring.ring.paddr = softc->def_cp_ring_mem.idi_paddr;
783         iflib_config_gtask_init(ctx, &softc->def_cp_task, bnxt_def_cp_task,
784             "dflt_cp");
785
786         rc = bnxt_init_sysctl_ctx(softc);
787         if (rc)
788                 goto init_sysctl_failed;
789         rc = bnxt_create_nvram_sysctls(softc->nvm_info);
790         if (rc)
791                 goto failed;
792
793         arc4rand(softc->vnic_info.rss_hash_key, HW_HASH_KEY_SIZE, 0);
794         softc->vnic_info.rss_hash_type =
795             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV4 |
796             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV4 |
797             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV4 |
798             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_IPV6 |
799             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_TCP_IPV6 |
800             HWRM_VNIC_RSS_CFG_INPUT_HASH_TYPE_UDP_IPV6;
801         rc = bnxt_create_config_sysctls_pre(softc);
802         if (rc)
803                 goto failed;
804
805         /* Initialize the vlan list */
806         SLIST_INIT(&softc->vnic_info.vlan_tags);
807         softc->vnic_info.vlan_tag_list.idi_vaddr = NULL;
808
809         return (rc);
810
811 failed:
812         bnxt_free_sysctl_ctx(softc);
813 init_sysctl_failed:
814         bnxt_hwrm_func_drv_unrgtr(softc, false);
815 drv_rgtr_fail:
816         free(softc->nvm_info, M_DEVBUF);
817 nvm_alloc_fail:
818 ver_fail:
819         free(softc->ver_info, M_DEVBUF);
820 ver_alloc_fail:
821         bnxt_free_hwrm_dma_mem(softc);
822 dma_fail:
823         BNXT_HWRM_LOCK_DESTROY(softc);
824         bnxt_pci_mapping_free(softc);
825         pci_disable_busmaster(softc->dev);
826         return (rc);
827 }
828
829 static int
830 bnxt_attach_post(if_ctx_t ctx)
831 {
832         struct bnxt_softc *softc = iflib_get_softc(ctx);
833         if_t ifp = iflib_get_ifp(ctx);
834         int rc;
835
836         bnxt_create_config_sysctls_post(softc);
837
838         /* Update link state etc... */
839         rc = bnxt_probe_phy(softc);
840         if (rc)
841                 goto failed;
842
843         /* Needs to be done after probing the phy */
844         bnxt_create_ver_sysctls(softc);
845         bnxt_add_media_types(softc);
846         ifmedia_set(softc->media, IFM_ETHER | IFM_AUTO);
847
848         softc->scctx->isc_max_frame_size = ifp->if_mtu + ETHER_HDR_LEN +
849             ETHER_CRC_LEN;
850
851 failed:
852         return rc;
853 }
854
855 static int
856 bnxt_detach(if_ctx_t ctx)
857 {
858         struct bnxt_softc *softc = iflib_get_softc(ctx);
859         struct bnxt_vlan_tag *tag;
860         struct bnxt_vlan_tag *tmp;
861         int i;
862
863         bnxt_wol_config(ctx);
864         bnxt_do_disable_intr(&softc->def_cp_ring);
865         bnxt_free_sysctl_ctx(softc);
866         bnxt_hwrm_func_reset(softc);
867         bnxt_clear_ids(softc);
868         iflib_irq_free(ctx, &softc->def_cp_ring.irq);
869         iflib_config_gtask_deinit(&softc->def_cp_task);
870         /* We need to free() these here... */
871         for (i = softc->nrxqsets-1; i>=0; i--) {
872                 iflib_irq_free(ctx, &softc->rx_cp_rings[i].irq);
873         }
874         iflib_dma_free(&softc->vnic_info.mc_list);
875         iflib_dma_free(&softc->vnic_info.rss_hash_key_tbl);
876         iflib_dma_free(&softc->vnic_info.rss_grp_tbl);
877         if (softc->vnic_info.vlan_tag_list.idi_vaddr)
878                 iflib_dma_free(&softc->vnic_info.vlan_tag_list);
879         SLIST_FOREACH_SAFE(tag, &softc->vnic_info.vlan_tags, next, tmp)
880                 free(tag, M_DEVBUF);
881         iflib_dma_free(&softc->def_cp_ring_mem);
882         for (i = 0; i < softc->nrxqsets; i++)
883                 free(softc->rx_rings[i].tpa_start, M_DEVBUF);
884         free(softc->ver_info, M_DEVBUF);
885         free(softc->nvm_info, M_DEVBUF);
886
887         bnxt_hwrm_func_drv_unrgtr(softc, false);
888         bnxt_free_hwrm_dma_mem(softc);
889         BNXT_HWRM_LOCK_DESTROY(softc);
890
891         pci_disable_busmaster(softc->dev);
892         bnxt_pci_mapping_free(softc);
893
894         return 0;
895 }
896
897 /* Device configuration */
898 static void
899 bnxt_init(if_ctx_t ctx)
900 {
901         struct bnxt_softc *softc = iflib_get_softc(ctx);
902         struct ifmediareq ifmr;
903         int i, j;
904         int rc;
905
906         rc = bnxt_hwrm_func_reset(softc);
907         if (rc)
908                 return;
909         bnxt_clear_ids(softc);
910
911         /* Allocate the default completion ring */
912         softc->def_cp_ring.cons = UINT32_MAX;
913         softc->def_cp_ring.v_bit = 1;
914         bnxt_mark_cpr_invalid(&softc->def_cp_ring);
915         rc = bnxt_hwrm_ring_alloc(softc,
916             HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
917             &softc->def_cp_ring.ring,
918             (uint16_t)HWRM_NA_SIGNATURE,
919             HWRM_NA_SIGNATURE, true);
920         if (rc)
921                 goto fail;
922
923         /* And now set the default CP ring as the async CP ring */
924         rc = bnxt_hwrm_func_cfg(softc);
925         if (rc)
926                 goto fail;
927
928         for (i = 0; i < softc->nrxqsets; i++) {
929                 /* Allocate the statistics context */
930                 rc = bnxt_hwrm_stat_ctx_alloc(softc, &softc->rx_cp_rings[i],
931                     softc->rx_stats.idi_paddr +
932                     (sizeof(struct ctx_hw_stats) * i));
933                 if (rc)
934                         goto fail;
935
936                 /* Allocate the completion ring */
937                 softc->rx_cp_rings[i].cons = UINT32_MAX;
938                 softc->rx_cp_rings[i].v_bit = 1;
939                 softc->rx_cp_rings[i].last_idx = UINT32_MAX;
940                 bnxt_mark_cpr_invalid(&softc->rx_cp_rings[i]);
941                 rc = bnxt_hwrm_ring_alloc(softc,
942                     HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
943                     &softc->rx_cp_rings[i].ring, (uint16_t)HWRM_NA_SIGNATURE,
944                     HWRM_NA_SIGNATURE, true);
945                 if (rc)
946                         goto fail;
947
948                 /* Allocate the RX ring */
949                 rc = bnxt_hwrm_ring_alloc(softc,
950                     HWRM_RING_ALLOC_INPUT_RING_TYPE_RX,
951                     &softc->rx_rings[i], (uint16_t)HWRM_NA_SIGNATURE,
952                     HWRM_NA_SIGNATURE, false);
953                 if (rc)
954                         goto fail;
955                 BNXT_RX_DB(&softc->rx_rings[i], 0);
956                 /* TODO: Cumulus+ doesn't need the double doorbell */
957                 BNXT_RX_DB(&softc->rx_rings[i], 0);
958
959                 /* Allocate the AG ring */
960                 rc = bnxt_hwrm_ring_alloc(softc,
961                     HWRM_RING_ALLOC_INPUT_RING_TYPE_RX,
962                     &softc->ag_rings[i], (uint16_t)HWRM_NA_SIGNATURE,
963                     HWRM_NA_SIGNATURE, false);
964                 if (rc)
965                         goto fail;
966                 BNXT_RX_DB(&softc->rx_rings[i], 0);
967                 /* TODO: Cumulus+ doesn't need the double doorbell */
968                 BNXT_RX_DB(&softc->ag_rings[i], 0);
969
970                 /* Allocate the ring group */
971                 softc->grp_info[i].stats_ctx =
972                     softc->rx_cp_rings[i].stats_ctx_id;
973                 softc->grp_info[i].rx_ring_id = softc->rx_rings[i].phys_id;
974                 softc->grp_info[i].ag_ring_id = softc->ag_rings[i].phys_id;
975                 softc->grp_info[i].cp_ring_id =
976                     softc->rx_cp_rings[i].ring.phys_id;
977                 rc = bnxt_hwrm_ring_grp_alloc(softc, &softc->grp_info[i]);
978                 if (rc)
979                         goto fail;
980
981         }
982
983         /* Allocate the VNIC RSS context */
984         rc = bnxt_hwrm_vnic_ctx_alloc(softc, &softc->vnic_info.rss_id);
985         if (rc)
986                 goto fail;
987
988         /* Allocate the vnic */
989         softc->vnic_info.def_ring_grp = softc->grp_info[0].grp_id;
990         softc->vnic_info.mru = softc->scctx->isc_max_frame_size;
991         rc = bnxt_hwrm_vnic_alloc(softc, &softc->vnic_info);
992         if (rc)
993                 goto fail;
994         rc = bnxt_hwrm_vnic_cfg(softc, &softc->vnic_info);
995         if (rc)
996                 goto fail;
997         rc = bnxt_hwrm_set_filter(softc, &softc->vnic_info);
998         if (rc)
999                 goto fail;
1000
1001         /* Enable RSS on the VNICs */
1002         for (i = 0, j = 0; i < HW_HASH_INDEX_SIZE; i++) {
1003                 ((uint16_t *)
1004                     softc->vnic_info.rss_grp_tbl.idi_vaddr)[i] =
1005                     htole16(softc->grp_info[j].grp_id);
1006                 if (++j == softc->nrxqsets)
1007                         j = 0;
1008         }
1009
1010         rc = bnxt_hwrm_rss_cfg(softc, &softc->vnic_info,
1011             softc->vnic_info.rss_hash_type);
1012         if (rc)
1013                 goto fail;
1014
1015         /* 
1016          * Enable LRO/TPA/GRO 
1017          * TBD: 
1018          *      Enable / Disable HW_LRO based on
1019          *      ifconfig lro / ifconfig -lro setting
1020          */
1021         rc = bnxt_hwrm_vnic_tpa_cfg(softc, &softc->vnic_info,
1022             (if_getcapenable(iflib_get_ifp(ctx)) & IFCAP_LRO) ?
1023             HWRM_VNIC_TPA_CFG_INPUT_FLAGS_TPA : 0);
1024         if (rc)
1025                 goto fail;
1026
1027         for (i = 0; i < softc->ntxqsets; i++) {
1028                 /* Allocate the statistics context */
1029                 rc = bnxt_hwrm_stat_ctx_alloc(softc, &softc->tx_cp_rings[i],
1030                     softc->tx_stats.idi_paddr +
1031                     (sizeof(struct ctx_hw_stats) * i));
1032                 if (rc)
1033                         goto fail;
1034
1035                 /* Allocate the completion ring */
1036                 softc->tx_cp_rings[i].cons = UINT32_MAX;
1037                 softc->tx_cp_rings[i].v_bit = 1;
1038                 bnxt_mark_cpr_invalid(&softc->tx_cp_rings[i]);
1039                 rc = bnxt_hwrm_ring_alloc(softc,
1040                     HWRM_RING_ALLOC_INPUT_RING_TYPE_CMPL,
1041                     &softc->tx_cp_rings[i].ring, (uint16_t)HWRM_NA_SIGNATURE,
1042                     HWRM_NA_SIGNATURE, false);
1043                 if (rc)
1044                         goto fail;
1045
1046                 /* Allocate the TX ring */
1047                 rc = bnxt_hwrm_ring_alloc(softc,
1048                     HWRM_RING_ALLOC_INPUT_RING_TYPE_TX,
1049                     &softc->tx_rings[i], softc->tx_cp_rings[i].ring.phys_id,
1050                     softc->tx_cp_rings[i].stats_ctx_id, false);
1051                 if (rc)
1052                         goto fail;
1053                 BNXT_TX_DB(&softc->tx_rings[i], 0);
1054                 /* TODO: Cumulus+ doesn't need the double doorbell */
1055                 BNXT_TX_DB(&softc->tx_rings[i], 0);
1056         }
1057
1058         bnxt_do_enable_intr(&softc->def_cp_ring);
1059         bnxt_media_status(softc->ctx, &ifmr);
1060         return;
1061
1062 fail:
1063         bnxt_hwrm_func_reset(softc);
1064         bnxt_clear_ids(softc);
1065         return;
1066 }
1067
1068 static void
1069 bnxt_stop(if_ctx_t ctx)
1070 {
1071         struct bnxt_softc *softc = iflib_get_softc(ctx);
1072
1073         bnxt_do_disable_intr(&softc->def_cp_ring);
1074         bnxt_hwrm_func_reset(softc);
1075         bnxt_clear_ids(softc);
1076         return;
1077 }
1078
1079 static void
1080 bnxt_multi_set(if_ctx_t ctx)
1081 {
1082         struct bnxt_softc *softc = iflib_get_softc(ctx);
1083         if_t ifp = iflib_get_ifp(ctx);
1084         uint8_t *mta;
1085         int cnt, mcnt;
1086
1087         mcnt = if_multiaddr_count(ifp, -1);
1088
1089         if (mcnt > BNXT_MAX_MC_ADDRS) {
1090                 softc->vnic_info.rx_mask |=
1091                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
1092                 bnxt_hwrm_cfa_l2_set_rx_mask(softc, &softc->vnic_info);
1093         }
1094         else {
1095                 softc->vnic_info.rx_mask &=
1096                     ~HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
1097                 mta = softc->vnic_info.mc_list.idi_vaddr;
1098                 bzero(mta, softc->vnic_info.mc_list.idi_size);
1099                 if_multiaddr_array(ifp, mta, &cnt, mcnt);
1100                 bus_dmamap_sync(softc->vnic_info.mc_list.idi_tag,
1101                     softc->vnic_info.mc_list.idi_map, BUS_DMASYNC_PREWRITE);
1102                 softc->vnic_info.mc_list_count = cnt;
1103                 softc->vnic_info.rx_mask |=
1104                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_MCAST;
1105                 if (bnxt_hwrm_cfa_l2_set_rx_mask(softc, &softc->vnic_info))
1106                         device_printf(softc->dev,
1107                             "set_multi: rx_mask set failed\n");
1108         }
1109 }
1110
1111 static int
1112 bnxt_mtu_set(if_ctx_t ctx, uint32_t mtu)
1113 {
1114         struct bnxt_softc *softc = iflib_get_softc(ctx);
1115
1116         if (mtu > BNXT_MAX_MTU)
1117                 return EINVAL;
1118
1119         softc->scctx->isc_max_frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
1120         return 0;
1121 }
1122
1123 static void
1124 bnxt_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
1125 {
1126         struct bnxt_softc *softc = iflib_get_softc(ctx);
1127         struct bnxt_link_info *link_info = &softc->link_info;
1128         uint8_t phy_type = get_phy_type(softc);
1129
1130         bnxt_update_link(softc, true);
1131
1132         ifmr->ifm_status = IFM_AVALID;
1133         ifmr->ifm_active = IFM_ETHER;
1134
1135         if (link_info->link_up)
1136                 ifmr->ifm_status |= IFM_ACTIVE;
1137         else
1138                 ifmr->ifm_status &= ~IFM_ACTIVE;
1139
1140         if (link_info->duplex == HWRM_PORT_PHY_QCFG_OUTPUT_DUPLEX_FULL)
1141                 ifmr->ifm_active |= IFM_FDX;
1142         else
1143                 ifmr->ifm_active |= IFM_HDX;
1144
1145         switch (link_info->link_speed) {
1146         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB:
1147                 ifmr->ifm_active |= IFM_100_T;
1148                 break;
1149         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_1GB:
1150                 switch (phy_type) {
1151                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX:
1152                         ifmr->ifm_active |= IFM_1000_KX;
1153                         break;
1154                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET:
1155                         ifmr->ifm_active |= IFM_1000_T;
1156                         break;
1157                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_SGMIIEXTPHY:
1158                         ifmr->ifm_active |= IFM_1000_SGMII;
1159                         break;
1160                 default:
1161                         /*
1162                          * Workaround: 
1163                          *    Don't return IFM_UNKNOWN until 
1164                          *    Stratus return proper media_type 
1165                          */  
1166                         ifmr->ifm_active |= IFM_1000_KX;
1167                         break;
1168                 }
1169         break;
1170         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2_5GB:
1171                 switch (phy_type) {
1172                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX:
1173                         ifmr->ifm_active |= IFM_2500_KX;
1174                         break;
1175                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET:
1176                         ifmr->ifm_active |= IFM_2500_T;
1177                         break;
1178                 default:
1179                         ifmr->ifm_active |= IFM_UNKNOWN;
1180                         break;
1181                 }
1182                 break;
1183         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10GB:
1184                 switch (phy_type) {
1185                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1186                         ifmr->ifm_active |= IFM_10G_CR1;
1187                         break;
1188                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1189                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1190                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1191                         ifmr->ifm_active |= IFM_10G_KR;
1192                         break;
1193                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASELR:
1194                         ifmr->ifm_active |= IFM_10G_LR;
1195                         break;
1196                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
1197                         ifmr->ifm_active |= IFM_10G_SR;
1198                         break;
1199                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX:
1200                         ifmr->ifm_active |= IFM_10G_KX4;
1201                         break;
1202                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET:
1203                         ifmr->ifm_active |= IFM_10G_T;
1204                         break;
1205                 default:
1206                         /*
1207                          * Workaround: 
1208                          *    Don't return IFM_UNKNOWN until 
1209                          *    Stratus return proper media_type 
1210                          */  
1211                         ifmr->ifm_active |= IFM_10G_CR1;
1212                         break;
1213                 }
1214                 break;
1215         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_20GB:
1216                 ifmr->ifm_active |= IFM_20G_KR2;
1217                 break;
1218         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_25GB:
1219                 switch (phy_type) {
1220                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1221                         ifmr->ifm_active |= IFM_25G_CR;
1222                         break;
1223                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1224                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1225                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1226                         ifmr->ifm_active |= IFM_25G_KR;
1227                         break;
1228                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
1229                         ifmr->ifm_active |= IFM_25G_SR;
1230                         break;
1231                 default:
1232                         /*
1233                          * Workaround: 
1234                          *    Don't return IFM_UNKNOWN until 
1235                          *    Stratus return proper media_type 
1236                          */  
1237                         ifmr->ifm_active |= IFM_25G_CR;
1238                         break;
1239                 }
1240                 break;
1241         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_40GB:
1242                 switch (phy_type) {
1243                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1244                         ifmr->ifm_active |= IFM_40G_CR4;
1245                         break;
1246                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1247                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1248                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1249                         ifmr->ifm_active |= IFM_40G_KR4;
1250                         break;
1251                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASELR:
1252                         ifmr->ifm_active |= IFM_40G_LR4;
1253                         break;
1254                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
1255                         ifmr->ifm_active |= IFM_40G_SR4;
1256                         break;
1257                 default:
1258                         ifmr->ifm_active |= IFM_UNKNOWN;
1259                         break;
1260                 }
1261                 break;
1262         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_50GB:
1263                 switch (phy_type) {
1264                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1265                         ifmr->ifm_active |= IFM_50G_CR2;
1266                         break;
1267                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1268                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1269                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1270                         ifmr->ifm_active |= IFM_50G_KR2;
1271                         break;
1272                 default:
1273                         /*
1274                          * Workaround: 
1275                          *    Don't return IFM_UNKNOWN until 
1276                          *    Stratus return proper media_type 
1277                          */  
1278                         ifmr->ifm_active |= IFM_50G_CR2;
1279                         break;
1280                 }
1281                 break;
1282         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100GB:
1283                 switch (phy_type) {
1284                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
1285                         ifmr->ifm_active |= IFM_100G_CR4;
1286                         break;
1287                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
1288                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
1289                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
1290                         ifmr->ifm_active |= IFM_100G_KR4;
1291                         break;
1292                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASELR:
1293                         ifmr->ifm_active |= IFM_100G_LR4;
1294                         break;
1295                 case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
1296                         ifmr->ifm_active |= IFM_100G_SR4;
1297                         break;
1298                 default:
1299                         /*
1300                          * Workaround: 
1301                          *    Don't return IFM_UNKNOWN until 
1302                          *    Stratus return proper media_type 
1303                          */  
1304                         ifmr->ifm_active |= IFM_100G_CR4;
1305                         break;
1306                 }
1307         default:
1308                 return;
1309         }
1310
1311         if (link_info->pause == (HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
1312             HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX))
1313                 ifmr->ifm_active |= (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
1314         else if (link_info->pause == HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX)
1315                 ifmr->ifm_active |= IFM_ETH_TXPAUSE;
1316         else if (link_info->pause == HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX)
1317                 ifmr->ifm_active |= IFM_ETH_RXPAUSE;
1318
1319         bnxt_report_link(softc);
1320         return;
1321 }
1322
1323 static int
1324 bnxt_media_change(if_ctx_t ctx)
1325 {
1326         struct bnxt_softc *softc = iflib_get_softc(ctx);
1327         struct ifmedia *ifm = iflib_get_media(ctx);
1328         struct ifmediareq ifmr;
1329         int rc;
1330
1331         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
1332                 return EINVAL;
1333
1334         switch (IFM_SUBTYPE(ifm->ifm_media)) {
1335         case IFM_100_T:
1336                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1337                 softc->link_info.req_link_speed =
1338                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_100MB;
1339                 break;
1340         case IFM_1000_KX:
1341         case IFM_1000_T:
1342         case IFM_1000_SGMII:
1343                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1344                 softc->link_info.req_link_speed =
1345                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_1GB;
1346                 break;
1347         case IFM_2500_KX:
1348         case IFM_2500_T:
1349                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1350                 softc->link_info.req_link_speed =
1351                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_2_5GB;
1352                 break;
1353         case IFM_10G_CR1:
1354         case IFM_10G_KR:
1355         case IFM_10G_LR:
1356         case IFM_10G_SR:
1357         case IFM_10G_T:
1358                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1359                 softc->link_info.req_link_speed =
1360                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_10GB;
1361                 break;
1362         case IFM_20G_KR2:
1363                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1364                 softc->link_info.req_link_speed =
1365                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_20GB;
1366                 break;
1367         case IFM_25G_CR:
1368         case IFM_25G_KR:
1369         case IFM_25G_SR:
1370                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1371                 softc->link_info.req_link_speed =
1372                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_25GB;
1373                 break;
1374         case IFM_40G_CR4:
1375         case IFM_40G_KR4:
1376         case IFM_40G_LR4:
1377         case IFM_40G_SR4:
1378                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1379                 softc->link_info.req_link_speed =
1380                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_40GB;
1381                 break;
1382         case IFM_50G_CR2:
1383         case IFM_50G_KR2:
1384                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1385                 softc->link_info.req_link_speed =
1386                     HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_50GB;
1387                 break;
1388         case IFM_100G_CR4:
1389         case IFM_100G_KR4:
1390         case IFM_100G_LR4:
1391         case IFM_100G_SR4:
1392                 softc->link_info.autoneg &= ~BNXT_AUTONEG_SPEED;
1393                 softc->link_info.req_link_speed =
1394                         HWRM_PORT_PHY_CFG_INPUT_FORCE_LINK_SPEED_100GB;
1395                 break;
1396         default:
1397                 device_printf(softc->dev,
1398                     "Unsupported media type!  Using auto\n");
1399                 /* Fall-through */
1400         case IFM_AUTO:
1401                 // Auto
1402                 softc->link_info.autoneg |= BNXT_AUTONEG_SPEED;
1403                 break;
1404         }
1405         rc = bnxt_hwrm_set_link_setting(softc, true, true);
1406         bnxt_media_status(softc->ctx, &ifmr);
1407         return rc;
1408 }
1409
1410 static int
1411 bnxt_promisc_set(if_ctx_t ctx, int flags)
1412 {
1413         struct bnxt_softc *softc = iflib_get_softc(ctx);
1414         if_t ifp = iflib_get_ifp(ctx);
1415         int rc;
1416
1417         if (ifp->if_flags & IFF_ALLMULTI ||
1418             if_multiaddr_count(ifp, -1) > BNXT_MAX_MC_ADDRS)
1419                 softc->vnic_info.rx_mask |=
1420                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
1421         else
1422                 softc->vnic_info.rx_mask &=
1423                     ~HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ALL_MCAST;
1424
1425         if (ifp->if_flags & IFF_PROMISC)
1426                 softc->vnic_info.rx_mask |=
1427                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_PROMISCUOUS |
1428                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ANYVLAN_NONVLAN;
1429         else
1430                 softc->vnic_info.rx_mask &=
1431                     ~(HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_PROMISCUOUS |
1432                     HWRM_CFA_L2_SET_RX_MASK_INPUT_MASK_ANYVLAN_NONVLAN);
1433
1434         rc = bnxt_hwrm_cfa_l2_set_rx_mask(softc, &softc->vnic_info);
1435
1436         return rc;
1437 }
1438
1439 static uint64_t
1440 bnxt_get_counter(if_ctx_t ctx, ift_counter cnt)
1441 {
1442         if_t ifp = iflib_get_ifp(ctx);
1443
1444         if (cnt < IFCOUNTERS)
1445                 return if_get_counter_default(ifp, cnt);
1446
1447         return 0;
1448 }
1449
1450 static void
1451 bnxt_update_admin_status(if_ctx_t ctx)
1452 {
1453         /* TODO: do we need to do anything here? */
1454         return;
1455 }
1456
1457 static void inline
1458 bnxt_do_enable_intr(struct bnxt_cp_ring *cpr)
1459 {
1460         if (cpr->ring.phys_id != (uint16_t)HWRM_NA_SIGNATURE) {
1461                 /* First time enabling, do not set index */
1462                 if (cpr->cons == UINT32_MAX)
1463                         BNXT_CP_ENABLE_DB(&cpr->ring);
1464                 else
1465                         BNXT_CP_IDX_ENABLE_DB(&cpr->ring, cpr->cons);
1466         }
1467 }
1468
1469 static void inline
1470 bnxt_do_disable_intr(struct bnxt_cp_ring *cpr)
1471 {
1472         if (cpr->ring.phys_id != (uint16_t)HWRM_NA_SIGNATURE)
1473                 BNXT_CP_DISABLE_DB(&cpr->ring);
1474 }
1475
1476 /* Enable all interrupts */
1477 static void
1478 bnxt_intr_enable(if_ctx_t ctx)
1479 {
1480         struct bnxt_softc *softc = iflib_get_softc(ctx);
1481         int i;
1482
1483         bnxt_do_enable_intr(&softc->def_cp_ring);
1484         for (i = 0; i < softc->nrxqsets; i++)
1485                 bnxt_do_enable_intr(&softc->rx_cp_rings[i]);
1486
1487         return;
1488 }
1489
1490 /* Enable interrupt for a single queue */
1491 static int
1492 bnxt_tx_queue_intr_enable(if_ctx_t ctx, uint16_t qid)
1493 {
1494         struct bnxt_softc *softc = iflib_get_softc(ctx);
1495
1496         bnxt_do_enable_intr(&softc->tx_cp_rings[qid]);
1497         return 0;
1498 }
1499
1500 static int
1501 bnxt_rx_queue_intr_enable(if_ctx_t ctx, uint16_t qid)
1502 {
1503         struct bnxt_softc *softc = iflib_get_softc(ctx);
1504
1505         bnxt_do_enable_intr(&softc->rx_cp_rings[qid]);
1506         return 0;
1507 }
1508
1509 /* Disable all interrupts */
1510 static void
1511 bnxt_disable_intr(if_ctx_t ctx)
1512 {
1513         struct bnxt_softc *softc = iflib_get_softc(ctx);
1514         int i;
1515
1516         /*
1517          * NOTE: These TX interrupts should never get enabled, so don't
1518          * update the index
1519          */
1520         for (i = 0; i < softc->ntxqsets; i++)
1521                 bnxt_do_disable_intr(&softc->tx_cp_rings[i]);
1522         for (i = 0; i < softc->nrxqsets; i++)
1523                 bnxt_do_disable_intr(&softc->rx_cp_rings[i]);
1524
1525         return;
1526 }
1527
1528 static int
1529 bnxt_msix_intr_assign(if_ctx_t ctx, int msix)
1530 {
1531         struct bnxt_softc *softc = iflib_get_softc(ctx);
1532         int rc;
1533         int i;
1534         char irq_name[16];
1535
1536         rc = iflib_irq_alloc_generic(ctx, &softc->def_cp_ring.irq,
1537             softc->def_cp_ring.ring.id + 1, IFLIB_INTR_ADMIN,
1538             bnxt_handle_def_cp, softc, 0, "def_cp");
1539         if (rc) {
1540                 device_printf(iflib_get_dev(ctx),
1541                     "Failed to register default completion ring handler\n");
1542                 return rc;
1543         }
1544
1545         for (i=0; i<softc->scctx->isc_nrxqsets; i++) {
1546                 snprintf(irq_name, sizeof(irq_name), "rxq%d", i);
1547                 rc = iflib_irq_alloc_generic(ctx, &softc->rx_cp_rings[i].irq,
1548                     softc->rx_cp_rings[i].ring.id + 1, IFLIB_INTR_RX,
1549                     bnxt_handle_rx_cp, &softc->rx_cp_rings[i], i, irq_name);
1550                 if (rc) {
1551                         device_printf(iflib_get_dev(ctx),
1552                             "Failed to register RX completion ring handler\n");
1553                         i--;
1554                         goto fail;
1555                 }
1556         }
1557
1558         for (i=0; i<softc->scctx->isc_ntxqsets; i++)
1559                 iflib_softirq_alloc_generic(ctx, i + 1, IFLIB_INTR_TX, NULL, i,
1560                     "tx_cp");
1561
1562         return rc;
1563
1564 fail:
1565         for (; i>=0; i--)
1566                 iflib_irq_free(ctx, &softc->rx_cp_rings[i].irq);
1567         iflib_irq_free(ctx, &softc->def_cp_ring.irq);
1568         return rc;
1569 }
1570
1571 /*
1572  * We're explicitly allowing duplicates here.  They will need to be
1573  * removed as many times as they are added.
1574  */
1575 static void
1576 bnxt_vlan_register(if_ctx_t ctx, uint16_t vtag)
1577 {
1578         struct bnxt_softc *softc = iflib_get_softc(ctx);
1579         struct bnxt_vlan_tag *new_tag;
1580
1581         new_tag = malloc(sizeof(struct bnxt_vlan_tag), M_DEVBUF, M_NOWAIT);
1582         if (new_tag == NULL)
1583                 return;
1584         new_tag->tag = vtag;
1585         new_tag->tpid = 8100;
1586         SLIST_INSERT_HEAD(&softc->vnic_info.vlan_tags, new_tag, next);
1587 };
1588
1589 static void
1590 bnxt_vlan_unregister(if_ctx_t ctx, uint16_t vtag)
1591 {
1592         struct bnxt_softc *softc = iflib_get_softc(ctx);
1593         struct bnxt_vlan_tag *vlan_tag;
1594
1595         SLIST_FOREACH(vlan_tag, &softc->vnic_info.vlan_tags, next) {
1596                 if (vlan_tag->tag == vtag) {
1597                         SLIST_REMOVE(&softc->vnic_info.vlan_tags, vlan_tag,
1598                             bnxt_vlan_tag, next);
1599                         free(vlan_tag, M_DEVBUF);
1600                         break;
1601                 }
1602         }
1603 }
1604
1605 static int
1606 bnxt_wol_config(if_ctx_t ctx)
1607 {
1608         struct bnxt_softc *softc = iflib_get_softc(ctx);
1609         if_t ifp = iflib_get_ifp(ctx);
1610
1611         if (!softc)
1612                 return -EBUSY;
1613
1614         if (!bnxt_wol_supported(softc))
1615                 return -ENOTSUP;
1616
1617         if (if_getcapabilities(ifp) & IFCAP_WOL_MAGIC) {
1618                 if (!softc->wol) {
1619                         if (bnxt_hwrm_alloc_wol_fltr(softc))
1620                                 return -EBUSY;
1621                         softc->wol = 1;
1622                 }
1623         } else {
1624                 if (softc->wol) {
1625                         if (bnxt_hwrm_free_wol_fltr(softc))
1626                                 return -EBUSY;
1627                         softc->wol = 0;
1628                 }
1629         }
1630
1631         return 0;
1632 }
1633
1634 static int
1635 bnxt_shutdown(if_ctx_t ctx)
1636 {
1637         bnxt_wol_config(ctx);
1638         return 0;
1639 }
1640
1641 static int
1642 bnxt_suspend(if_ctx_t ctx)
1643 {
1644         bnxt_wol_config(ctx);
1645         return 0;
1646 }
1647
1648 static int
1649 bnxt_resume(if_ctx_t ctx)
1650 {
1651         struct bnxt_softc *softc = iflib_get_softc(ctx);
1652
1653         bnxt_get_wol_settings(softc);
1654         return 0;
1655 }
1656
1657 static int
1658 bnxt_priv_ioctl(if_ctx_t ctx, u_long command, caddr_t data)
1659 {
1660         struct bnxt_softc *softc = iflib_get_softc(ctx);
1661         struct ifreq *ifr = (struct ifreq *)data;
1662         struct ifreq_buffer *ifbuf = &ifr->ifr_ifru.ifru_buffer;
1663         struct bnxt_ioctl_header *ioh =
1664             (struct bnxt_ioctl_header *)(ifbuf->buffer);
1665         int rc = ENOTSUP;
1666         struct bnxt_ioctl_data *iod = NULL;
1667
1668         switch (command) {
1669         case SIOCGPRIVATE_0:
1670                 if ((rc = priv_check(curthread, PRIV_DRIVER)) != 0)
1671                         goto exit;
1672
1673                 iod = malloc(ifbuf->length, M_DEVBUF, M_NOWAIT | M_ZERO);
1674                 if (!iod) {
1675                         rc = ENOMEM;
1676                         goto exit;
1677                 }
1678                 copyin(ioh, iod, ifbuf->length);
1679
1680                 switch (ioh->type) {
1681                 case BNXT_HWRM_NVM_FIND_DIR_ENTRY:
1682                 {
1683                         struct bnxt_ioctl_hwrm_nvm_find_dir_entry *find =
1684                             &iod->find;
1685
1686                         rc = bnxt_hwrm_nvm_find_dir_entry(softc, find->type,
1687                             &find->ordinal, find->ext, &find->index,
1688                             find->use_index, find->search_opt,
1689                             &find->data_length, &find->item_length,
1690                             &find->fw_ver);
1691                         if (rc) {
1692                                 iod->hdr.rc = rc;
1693                                 copyout(&iod->hdr.rc, &ioh->rc,
1694                                     sizeof(ioh->rc));
1695                         }
1696                         else {
1697                                 iod->hdr.rc = 0;
1698                                 copyout(iod, ioh, ifbuf->length);
1699                         }
1700
1701                         rc = 0;
1702                         goto exit;
1703                 }
1704                 case BNXT_HWRM_NVM_READ:
1705                 {
1706                         struct bnxt_ioctl_hwrm_nvm_read *rd = &iod->read;
1707                         struct iflib_dma_info dma_data;
1708                         size_t offset;
1709                         size_t remain;
1710                         size_t csize;
1711
1712                         /*
1713                          * Some HWRM versions can't read more than 0x8000 bytes
1714                          */
1715                         rc = iflib_dma_alloc(softc->ctx,
1716                             min(rd->length, 0x8000), &dma_data, BUS_DMA_NOWAIT);
1717                         if (rc)
1718                                 break;
1719                         for (remain = rd->length, offset = 0;
1720                             remain && offset < rd->length; offset += 0x8000) {
1721                                 csize = min(remain, 0x8000);
1722                                 rc = bnxt_hwrm_nvm_read(softc, rd->index,
1723                                     rd->offset + offset, csize, &dma_data);
1724                                 if (rc) {
1725                                         iod->hdr.rc = rc;
1726                                         copyout(&iod->hdr.rc, &ioh->rc,
1727                                             sizeof(ioh->rc));
1728                                         break;
1729                                 }
1730                                 else {
1731                                         copyout(dma_data.idi_vaddr,
1732                                             rd->data + offset, csize);
1733                                         iod->hdr.rc = 0;
1734                                 }
1735                                 remain -= csize;
1736                         }
1737                         if (iod->hdr.rc == 0)
1738                                 copyout(iod, ioh, ifbuf->length);
1739
1740                         iflib_dma_free(&dma_data);
1741                         rc = 0;
1742                         goto exit;
1743                 }
1744                 case BNXT_HWRM_FW_RESET:
1745                 {
1746                         struct bnxt_ioctl_hwrm_fw_reset *rst =
1747                             &iod->reset;
1748
1749                         rc = bnxt_hwrm_fw_reset(softc, rst->processor,
1750                             &rst->selfreset);
1751                         if (rc) {
1752                                 iod->hdr.rc = rc;
1753                                 copyout(&iod->hdr.rc, &ioh->rc,
1754                                     sizeof(ioh->rc));
1755                         }
1756                         else {
1757                                 iod->hdr.rc = 0;
1758                                 copyout(iod, ioh, ifbuf->length);
1759                         }
1760
1761                         rc = 0;
1762                         goto exit;
1763                 }
1764                 case BNXT_HWRM_FW_QSTATUS:
1765                 {
1766                         struct bnxt_ioctl_hwrm_fw_qstatus *qstat =
1767                             &iod->status;
1768
1769                         rc = bnxt_hwrm_fw_qstatus(softc, qstat->processor,
1770                             &qstat->selfreset);
1771                         if (rc) {
1772                                 iod->hdr.rc = rc;
1773                                 copyout(&iod->hdr.rc, &ioh->rc,
1774                                     sizeof(ioh->rc));
1775                         }
1776                         else {
1777                                 iod->hdr.rc = 0;
1778                                 copyout(iod, ioh, ifbuf->length);
1779                         }
1780
1781                         rc = 0;
1782                         goto exit;
1783                 }
1784                 case BNXT_HWRM_NVM_WRITE:
1785                 {
1786                         struct bnxt_ioctl_hwrm_nvm_write *wr =
1787                             &iod->write;
1788
1789                         rc = bnxt_hwrm_nvm_write(softc, wr->data, true,
1790                             wr->type, wr->ordinal, wr->ext, wr->attr,
1791                             wr->option, wr->data_length, wr->keep,
1792                             &wr->item_length, &wr->index);
1793                         if (rc) {
1794                                 iod->hdr.rc = rc;
1795                                 copyout(&iod->hdr.rc, &ioh->rc,
1796                                     sizeof(ioh->rc));
1797                         }
1798                         else {
1799                                 iod->hdr.rc = 0;
1800                                 copyout(iod, ioh, ifbuf->length);
1801                         }
1802
1803                         rc = 0;
1804                         goto exit;
1805                 }
1806                 case BNXT_HWRM_NVM_ERASE_DIR_ENTRY:
1807                 {
1808                         struct bnxt_ioctl_hwrm_nvm_erase_dir_entry *erase =
1809                             &iod->erase;
1810
1811                         rc = bnxt_hwrm_nvm_erase_dir_entry(softc, erase->index);
1812                         if (rc) {
1813                                 iod->hdr.rc = rc;
1814                                 copyout(&iod->hdr.rc, &ioh->rc,
1815                                     sizeof(ioh->rc));
1816                         }
1817                         else {
1818                                 iod->hdr.rc = 0;
1819                                 copyout(iod, ioh, ifbuf->length);
1820                         }
1821
1822                         rc = 0;
1823                         goto exit;
1824                 }
1825                 case BNXT_HWRM_NVM_GET_DIR_INFO:
1826                 {
1827                         struct bnxt_ioctl_hwrm_nvm_get_dir_info *info =
1828                             &iod->dir_info;
1829
1830                         rc = bnxt_hwrm_nvm_get_dir_info(softc, &info->entries,
1831                             &info->entry_length);
1832                         if (rc) {
1833                                 iod->hdr.rc = rc;
1834                                 copyout(&iod->hdr.rc, &ioh->rc,
1835                                     sizeof(ioh->rc));
1836                         }
1837                         else {
1838                                 iod->hdr.rc = 0;
1839                                 copyout(iod, ioh, ifbuf->length);
1840                         }
1841
1842                         rc = 0;
1843                         goto exit;
1844                 }
1845                 case BNXT_HWRM_NVM_GET_DIR_ENTRIES:
1846                 {
1847                         struct bnxt_ioctl_hwrm_nvm_get_dir_entries *get =
1848                             &iod->dir_entries;
1849                         struct iflib_dma_info dma_data;
1850
1851                         rc = iflib_dma_alloc(softc->ctx, get->max_size,
1852                             &dma_data, BUS_DMA_NOWAIT);
1853                         if (rc)
1854                                 break;
1855                         rc = bnxt_hwrm_nvm_get_dir_entries(softc, &get->entries,
1856                             &get->entry_length, &dma_data);
1857                         if (rc) {
1858                                 iod->hdr.rc = rc;
1859                                 copyout(&iod->hdr.rc, &ioh->rc,
1860                                     sizeof(ioh->rc));
1861                         }
1862                         else {
1863                                 copyout(dma_data.idi_vaddr, get->data,
1864                                     get->entry_length * get->entries);
1865                                 iod->hdr.rc = 0;
1866                                 copyout(iod, ioh, ifbuf->length);
1867                         }
1868                         iflib_dma_free(&dma_data);
1869
1870                         rc = 0;
1871                         goto exit;
1872                 }
1873                 case BNXT_HWRM_NVM_VERIFY_UPDATE:
1874                 {
1875                         struct bnxt_ioctl_hwrm_nvm_verify_update *vrfy =
1876                             &iod->verify;
1877
1878                         rc = bnxt_hwrm_nvm_verify_update(softc, vrfy->type,
1879                             vrfy->ordinal, vrfy->ext);
1880                         if (rc) {
1881                                 iod->hdr.rc = rc;
1882                                 copyout(&iod->hdr.rc, &ioh->rc,
1883                                     sizeof(ioh->rc));
1884                         }
1885                         else {
1886                                 iod->hdr.rc = 0;
1887                                 copyout(iod, ioh, ifbuf->length);
1888                         }
1889
1890                         rc = 0;
1891                         goto exit;
1892                 }
1893                 case BNXT_HWRM_NVM_INSTALL_UPDATE:
1894                 {
1895                         struct bnxt_ioctl_hwrm_nvm_install_update *inst =
1896                             &iod->install;
1897
1898                         rc = bnxt_hwrm_nvm_install_update(softc,
1899                             inst->install_type, &inst->installed_items,
1900                             &inst->result, &inst->problem_item,
1901                             &inst->reset_required);
1902                         if (rc) {
1903                                 iod->hdr.rc = rc;
1904                                 copyout(&iod->hdr.rc, &ioh->rc,
1905                                     sizeof(ioh->rc));
1906                         }
1907                         else {
1908                                 iod->hdr.rc = 0;
1909                                 copyout(iod, ioh, ifbuf->length);
1910                         }
1911
1912                         rc = 0;
1913                         goto exit;
1914                 }
1915                 case BNXT_HWRM_NVM_MODIFY:
1916                 {
1917                         struct bnxt_ioctl_hwrm_nvm_modify *mod = &iod->modify;
1918
1919                         rc = bnxt_hwrm_nvm_modify(softc, mod->index,
1920                             mod->offset, mod->data, true, mod->length);
1921                         if (rc) {
1922                                 iod->hdr.rc = rc;
1923                                 copyout(&iod->hdr.rc, &ioh->rc,
1924                                     sizeof(ioh->rc));
1925                         }
1926                         else {
1927                                 iod->hdr.rc = 0;
1928                                 copyout(iod, ioh, ifbuf->length);
1929                         }
1930
1931                         rc = 0;
1932                         goto exit;
1933                 }
1934                 case BNXT_HWRM_FW_GET_TIME:
1935                 {
1936                         struct bnxt_ioctl_hwrm_fw_get_time *gtm =
1937                             &iod->get_time;
1938
1939                         rc = bnxt_hwrm_fw_get_time(softc, &gtm->year,
1940                             &gtm->month, &gtm->day, &gtm->hour, &gtm->minute,
1941                             &gtm->second, &gtm->millisecond, &gtm->zone);
1942                         if (rc) {
1943                                 iod->hdr.rc = rc;
1944                                 copyout(&iod->hdr.rc, &ioh->rc,
1945                                     sizeof(ioh->rc));
1946                         }
1947                         else {
1948                                 iod->hdr.rc = 0;
1949                                 copyout(iod, ioh, ifbuf->length);
1950                         }
1951
1952                         rc = 0;
1953                         goto exit;
1954                 }
1955                 case BNXT_HWRM_FW_SET_TIME:
1956                 {
1957                         struct bnxt_ioctl_hwrm_fw_set_time *stm =
1958                             &iod->set_time;
1959
1960                         rc = bnxt_hwrm_fw_set_time(softc, stm->year,
1961                             stm->month, stm->day, stm->hour, stm->minute,
1962                             stm->second, stm->millisecond, stm->zone);
1963                         if (rc) {
1964                                 iod->hdr.rc = rc;
1965                                 copyout(&iod->hdr.rc, &ioh->rc,
1966                                     sizeof(ioh->rc));
1967                         }
1968                         else {
1969                                 iod->hdr.rc = 0;
1970                                 copyout(iod, ioh, ifbuf->length);
1971                         }
1972
1973                         rc = 0;
1974                         goto exit;
1975                 }
1976                 }
1977                 break;
1978         }
1979
1980 exit:
1981         if (iod)
1982                 free(iod, M_DEVBUF);
1983         return rc;
1984 }
1985
1986 /*
1987  * Support functions
1988  */
1989 static int
1990 bnxt_probe_phy(struct bnxt_softc *softc)
1991 {
1992         struct bnxt_link_info *link_info = &softc->link_info;
1993         int rc = 0;
1994
1995         rc = bnxt_update_link(softc, false);
1996         if (rc) {
1997                 device_printf(softc->dev,
1998                     "Probe phy can't update link (rc: %x)\n", rc);
1999                 return (rc);
2000         }
2001
2002         /*initialize the ethool setting copy with NVM settings */
2003         if (link_info->auto_mode != HWRM_PORT_PHY_QCFG_OUTPUT_AUTO_MODE_NONE)
2004                 link_info->autoneg |= BNXT_AUTONEG_SPEED;
2005
2006         if (link_info->auto_pause & (HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2007             HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX)) {
2008                 if (link_info->auto_pause == (
2009                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2010                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX))
2011                         link_info->autoneg |= BNXT_AUTONEG_FLOW_CTRL;
2012                 link_info->req_flow_ctrl = link_info->auto_pause;
2013         } else if (link_info->force_pause & (
2014             HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2015             HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX)) {
2016                 link_info->req_flow_ctrl = link_info->force_pause;
2017         }
2018         link_info->req_duplex = link_info->duplex_setting;
2019         if (link_info->autoneg & BNXT_AUTONEG_SPEED)
2020                 link_info->req_link_speed = link_info->auto_link_speed;
2021         else
2022                 link_info->req_link_speed = link_info->force_link_speed;
2023         return (rc);
2024 }
2025
2026 static void
2027 bnxt_add_media_types(struct bnxt_softc *softc)
2028 {
2029         struct bnxt_link_info *link_info = &softc->link_info;
2030         uint16_t supported;
2031         uint8_t phy_type = get_phy_type(softc);
2032
2033         supported = link_info->support_speeds;
2034
2035         /* Auto is always supported */
2036         ifmedia_add(softc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2037
2038         if (softc->flags & BNXT_FLAG_NPAR)
2039                 return;
2040
2041         switch (phy_type) {
2042         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR:
2043                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
2044                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_CR4, 0,
2045                             NULL);
2046                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_50GB)
2047                         ifmedia_add(softc->media, IFM_ETHER | IFM_50G_CR2, 0,
2048                             NULL);
2049                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
2050                         ifmedia_add(softc->media, IFM_ETHER | IFM_40G_CR4, 0,
2051                             NULL);
2052                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_25GB)
2053                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_CR, 0,
2054                             NULL);
2055                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2056                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_CR1, 0,
2057                             NULL);
2058                 break;
2059         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR4:
2060         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR2:
2061         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR:
2062                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
2063                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_KR4, 0,
2064                             NULL);
2065                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_50GB)
2066                         ifmedia_add(softc->media, IFM_ETHER | IFM_50G_KR2, 0,
2067                             NULL);
2068                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
2069                         ifmedia_add(softc->media, IFM_ETHER | IFM_40G_KR4, 0,
2070                             NULL);
2071                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_25GB)
2072                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_KR, 0,
2073                             NULL);
2074                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_20GB)
2075                         ifmedia_add(softc->media, IFM_ETHER | IFM_20G_KR2, 0,
2076                             NULL);
2077                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2078                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_KR, 0,
2079                             NULL);
2080                 break;
2081         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASELR:
2082                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
2083                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_LR4, 0,
2084                             NULL);
2085                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
2086                         ifmedia_add(softc->media, IFM_ETHER | IFM_40G_LR4, 0,
2087                             NULL);
2088                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2089                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_LR, 0,
2090                             NULL);
2091                 break;
2092         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR:
2093                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100GB)
2094                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_SR4, 0,
2095                             NULL);
2096                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_40GB)
2097                         ifmedia_add(softc->media, IFM_ETHER | IFM_40G_SR4, 0,
2098                             NULL);
2099                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_25GB)
2100                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_SR, 0,
2101                             NULL);
2102                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2103                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_SR, 0,
2104                             NULL);
2105                 break;
2106         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX:
2107                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2108                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_KX4, 0,
2109                             NULL);
2110                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_2_5GB)
2111                         ifmedia_add(softc->media, IFM_ETHER | IFM_2500_KX, 0,
2112                             NULL);
2113                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_1GB)
2114                         ifmedia_add(softc->media, IFM_ETHER | IFM_1000_KX, 0,
2115                             NULL);
2116                 break;
2117         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET:
2118         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASETE:
2119                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10MB)
2120                         ifmedia_add(softc->media, IFM_ETHER | IFM_10_T, 0,
2121                             NULL);
2122                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_100MB)
2123                         ifmedia_add(softc->media, IFM_ETHER | IFM_100_T, 0,
2124                             NULL);
2125                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_1GB)
2126                         ifmedia_add(softc->media, IFM_ETHER | IFM_1000_T, 0,
2127                             NULL);
2128                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_2_5GB)
2129                         ifmedia_add(softc->media, IFM_ETHER | IFM_2500_T, 0,
2130                             NULL);
2131                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_10GB)
2132                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_T, 0,
2133                             NULL);
2134                 break;
2135         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_SGMIIEXTPHY:
2136                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_1GB)
2137                         ifmedia_add(softc->media, IFM_ETHER | IFM_1000_SGMII, 0,
2138                             NULL);
2139                 break;
2140         case HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_UNKNOWN:
2141         default:
2142                 /*
2143                  * Workaround for Cumulus & Stratus 
2144                  *  For Stratus: 
2145                  *      media_type is being returned as 0x0
2146                  *      Return support speeds as 10G, 25G, 50G & 100G
2147                  *
2148                  *  For Cumulus: 
2149                  *      phy_type is being returned as 0x14 (PHY_TYPE_40G_BASECR4)
2150                  *      Return support speeds as 1G, 10G, 25G & 50G
2151                  */
2152                 if (pci_get_device(softc->dev) == BCM57454) {
2153                         /* For Stratus: 10G, 25G, 50G & 100G */
2154                         ifmedia_add(softc->media, IFM_ETHER | IFM_100G_CR4, 0, NULL);
2155                         ifmedia_add(softc->media, IFM_ETHER | IFM_50G_CR2, 0, NULL);
2156                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_CR, 0, NULL);
2157                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_CR1, 0, NULL);
2158                 } else if (pci_get_device(softc->dev) == BCM57414) {
2159                         /* For Cumulus: 1G, 10G, 25G & 50G */
2160                         ifmedia_add(softc->media, IFM_ETHER | IFM_50G_CR2, 0, NULL);
2161                         ifmedia_add(softc->media, IFM_ETHER | IFM_25G_CR, 0, NULL);
2162                         ifmedia_add(softc->media, IFM_ETHER | IFM_10G_CR1, 0, NULL);
2163                         ifmedia_add(softc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
2164                 } 
2165                 break;
2166         }
2167
2168         return;
2169 }
2170
2171 static int
2172 bnxt_map_bar(struct bnxt_softc *softc, struct bnxt_bar_info *bar, int bar_num, bool shareable)
2173 {
2174         uint32_t        flag;
2175
2176         if (bar->res != NULL) {
2177                 device_printf(softc->dev, "Bar %d already mapped\n", bar_num);
2178                 return EDOOFUS;
2179         }
2180
2181         bar->rid = PCIR_BAR(bar_num);
2182         flag = RF_ACTIVE;
2183         if (shareable)
2184                 flag |= RF_SHAREABLE;
2185
2186         if ((bar->res =
2187                 bus_alloc_resource_any(softc->dev,
2188                            SYS_RES_MEMORY,
2189                            &bar->rid,
2190                            flag)) == NULL) {
2191                 device_printf(softc->dev,
2192                     "PCI BAR%d mapping failure\n", bar_num);
2193                 return (ENXIO);
2194         }
2195         bar->tag = rman_get_bustag(bar->res);
2196         bar->handle = rman_get_bushandle(bar->res);
2197         bar->size = rman_get_size(bar->res);
2198
2199         return 0;
2200 }
2201
2202 static int
2203 bnxt_pci_mapping(struct bnxt_softc *softc)
2204 {
2205         int rc;
2206
2207         rc = bnxt_map_bar(softc, &softc->hwrm_bar, 0, true);
2208         if (rc)
2209                 return rc;
2210
2211         rc = bnxt_map_bar(softc, &softc->doorbell_bar, 2, false);
2212
2213         return rc;
2214 }
2215
2216 static void
2217 bnxt_pci_mapping_free(struct bnxt_softc *softc)
2218 {
2219         if (softc->hwrm_bar.res != NULL)
2220                 bus_release_resource(softc->dev, SYS_RES_MEMORY,
2221                     softc->hwrm_bar.rid, softc->hwrm_bar.res);
2222         softc->hwrm_bar.res = NULL;
2223
2224         if (softc->doorbell_bar.res != NULL)
2225                 bus_release_resource(softc->dev, SYS_RES_MEMORY,
2226                     softc->doorbell_bar.rid, softc->doorbell_bar.res);
2227         softc->doorbell_bar.res = NULL;
2228 }
2229
2230 static int
2231 bnxt_update_link(struct bnxt_softc *softc, bool chng_link_state)
2232 {
2233         struct bnxt_link_info *link_info = &softc->link_info;
2234         uint8_t link_up = link_info->link_up;
2235         int rc = 0;
2236
2237         rc = bnxt_hwrm_port_phy_qcfg(softc);
2238         if (rc)
2239                 goto exit;
2240
2241         /* TODO: need to add more logic to report VF link */
2242         if (chng_link_state) {
2243                 if (link_info->phy_link_status ==
2244                     HWRM_PORT_PHY_QCFG_OUTPUT_LINK_LINK)
2245                         link_info->link_up = 1;
2246                 else
2247                         link_info->link_up = 0;
2248                 if (link_up != link_info->link_up)
2249                         bnxt_report_link(softc);
2250         } else {
2251                 /* always link down if not require to update link state */
2252                 link_info->link_up = 0;
2253         }
2254
2255 exit:
2256         return rc;
2257 }
2258
2259 void
2260 bnxt_report_link(struct bnxt_softc *softc)
2261 {
2262         const char *duplex = NULL, *flow_ctrl = NULL;
2263
2264         if (softc->link_info.link_up == softc->link_info.last_link_up) {
2265                 if (!softc->link_info.link_up)
2266                         return;
2267                 if (softc->link_info.pause == softc->link_info.last_pause &&
2268                     softc->link_info.duplex == softc->link_info.last_duplex)
2269                         return;
2270         }
2271
2272         if (softc->link_info.link_up) {
2273                 if (softc->link_info.duplex ==
2274                     HWRM_PORT_PHY_QCFG_OUTPUT_DUPLEX_FULL)
2275                         duplex = "full duplex";
2276                 else
2277                         duplex = "half duplex";
2278                 if (softc->link_info.pause == (
2279                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX |
2280                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX))
2281                         flow_ctrl = "FC - receive & transmit";
2282                 else if (softc->link_info.pause ==
2283                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_TX)
2284                         flow_ctrl = "FC - transmit";
2285                 else if (softc->link_info.pause ==
2286                     HWRM_PORT_PHY_QCFG_OUTPUT_PAUSE_RX)
2287                         flow_ctrl = "FC - receive";
2288                 else
2289                         flow_ctrl = "none";
2290                 iflib_link_state_change(softc->ctx, LINK_STATE_UP,
2291                     IF_Gbps(100));
2292                 device_printf(softc->dev, "Link is UP %s, %s\n", duplex,
2293                     flow_ctrl);
2294         } else {
2295                 iflib_link_state_change(softc->ctx, LINK_STATE_DOWN,
2296                     bnxt_get_baudrate(&softc->link_info));
2297                 device_printf(softc->dev, "Link is Down\n");
2298         }
2299
2300         softc->link_info.last_link_up = softc->link_info.link_up;
2301         softc->link_info.last_pause = softc->link_info.pause;
2302         softc->link_info.last_duplex = softc->link_info.duplex;
2303 }
2304
2305 static int
2306 bnxt_handle_rx_cp(void *arg)
2307 {
2308         struct bnxt_cp_ring *cpr = arg;
2309
2310         /* Disable further interrupts for this queue */
2311         BNXT_CP_DISABLE_DB(&cpr->ring);
2312         return FILTER_SCHEDULE_THREAD;
2313 }
2314
2315 static int
2316 bnxt_handle_def_cp(void *arg)
2317 {
2318         struct bnxt_softc *softc = arg;
2319
2320         BNXT_CP_DISABLE_DB(&softc->def_cp_ring.ring);
2321         GROUPTASK_ENQUEUE(&softc->def_cp_task);
2322         return FILTER_HANDLED;
2323 }
2324
2325 static void
2326 bnxt_clear_ids(struct bnxt_softc *softc)
2327 {
2328         int i;
2329
2330         softc->def_cp_ring.stats_ctx_id = HWRM_NA_SIGNATURE;
2331         softc->def_cp_ring.ring.phys_id = (uint16_t)HWRM_NA_SIGNATURE;
2332         for (i = 0; i < softc->ntxqsets; i++) {
2333                 softc->tx_cp_rings[i].stats_ctx_id = HWRM_NA_SIGNATURE;
2334                 softc->tx_cp_rings[i].ring.phys_id =
2335                     (uint16_t)HWRM_NA_SIGNATURE;
2336                 softc->tx_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
2337         }
2338         for (i = 0; i < softc->nrxqsets; i++) {
2339                 softc->rx_cp_rings[i].stats_ctx_id = HWRM_NA_SIGNATURE;
2340                 softc->rx_cp_rings[i].ring.phys_id =
2341                     (uint16_t)HWRM_NA_SIGNATURE;
2342                 softc->rx_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
2343                 softc->ag_rings[i].phys_id = (uint16_t)HWRM_NA_SIGNATURE;
2344                 softc->grp_info[i].grp_id = (uint16_t)HWRM_NA_SIGNATURE;
2345         }
2346         softc->vnic_info.filter_id = -1;
2347         softc->vnic_info.id = (uint16_t)HWRM_NA_SIGNATURE;
2348         softc->vnic_info.rss_id = (uint16_t)HWRM_NA_SIGNATURE;
2349         memset(softc->vnic_info.rss_grp_tbl.idi_vaddr, 0xff,
2350             softc->vnic_info.rss_grp_tbl.idi_size);
2351 }
2352
2353 static void
2354 bnxt_mark_cpr_invalid(struct bnxt_cp_ring *cpr)
2355 {
2356         struct cmpl_base *cmp = (void *)cpr->ring.vaddr;
2357         int i;
2358
2359         for (i = 0; i < cpr->ring.ring_size; i++)
2360                 cmp[i].info3_v = !cpr->v_bit;
2361 }
2362
2363 static void
2364 bnxt_handle_async_event(struct bnxt_softc *softc, struct cmpl_base *cmpl)
2365 {
2366         struct hwrm_async_event_cmpl *ae = (void *)cmpl;
2367         uint16_t async_id = le16toh(ae->event_id);
2368         struct ifmediareq ifmr;
2369
2370         switch (async_id) {
2371         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_STATUS_CHANGE:
2372         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CHANGE:
2373         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_CHANGE:
2374                 bnxt_media_status(softc->ctx, &ifmr);
2375                 break;
2376         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_MTU_CHANGE:
2377         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_DCB_CONFIG_CHANGE:
2378         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PORT_CONN_NOT_ALLOWED:
2379         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_LINK_SPEED_CFG_NOT_ALLOWED:
2380         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_FUNC_DRVR_UNLOAD:
2381         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_FUNC_DRVR_LOAD:
2382         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_UNLOAD:
2383         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_DRVR_LOAD:
2384         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_FLR:
2385         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_MAC_ADDR_CHANGE:
2386         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_PF_VF_COMM_STATUS_CHANGE:
2387         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_VF_CFG_CHANGE:
2388         case HWRM_ASYNC_EVENT_CMPL_EVENT_ID_HWRM_ERROR:
2389                 device_printf(softc->dev,
2390                     "Unhandled async completion type %u\n", async_id);
2391                 break;
2392         default:
2393                 device_printf(softc->dev,
2394                     "Unknown async completion type %u\n", async_id);
2395                 break;
2396         }
2397 }
2398
2399 static void
2400 bnxt_def_cp_task(void *context)
2401 {
2402         if_ctx_t ctx = context;
2403         struct bnxt_softc *softc = iflib_get_softc(ctx);
2404         struct bnxt_cp_ring *cpr = &softc->def_cp_ring;
2405
2406         /* Handle completions on the default completion ring */
2407         struct cmpl_base *cmpl;
2408         uint32_t cons = cpr->cons;
2409         bool v_bit = cpr->v_bit;
2410         bool last_v_bit;
2411         uint32_t last_cons;
2412         uint16_t type;
2413
2414         for (;;) {
2415                 last_cons = cons;
2416                 last_v_bit = v_bit;
2417                 NEXT_CP_CONS_V(&cpr->ring, cons, v_bit);
2418                 cmpl = &((struct cmpl_base *)cpr->ring.vaddr)[cons];
2419
2420                 if (!CMP_VALID(cmpl, v_bit))
2421                         break;
2422
2423                 type = le16toh(cmpl->type) & CMPL_BASE_TYPE_MASK;
2424                 switch (type) {
2425                 case CMPL_BASE_TYPE_HWRM_ASYNC_EVENT:
2426                         bnxt_handle_async_event(softc, cmpl);
2427                         break;
2428                 case CMPL_BASE_TYPE_TX_L2:
2429                 case CMPL_BASE_TYPE_RX_L2:
2430                 case CMPL_BASE_TYPE_RX_AGG:
2431                 case CMPL_BASE_TYPE_RX_TPA_START:
2432                 case CMPL_BASE_TYPE_RX_TPA_END:
2433                 case CMPL_BASE_TYPE_STAT_EJECT:
2434                 case CMPL_BASE_TYPE_HWRM_DONE:
2435                 case CMPL_BASE_TYPE_HWRM_FWD_REQ:
2436                 case CMPL_BASE_TYPE_HWRM_FWD_RESP:
2437                 case CMPL_BASE_TYPE_CQ_NOTIFICATION:
2438                 case CMPL_BASE_TYPE_SRQ_EVENT:
2439                 case CMPL_BASE_TYPE_DBQ_EVENT:
2440                 case CMPL_BASE_TYPE_QP_EVENT:
2441                 case CMPL_BASE_TYPE_FUNC_EVENT:
2442                         device_printf(softc->dev,
2443                             "Unhandled completion type %u\n", type);
2444                         break;
2445                 default:
2446                         device_printf(softc->dev,
2447                             "Unknown completion type %u\n", type);
2448                         break;
2449                 }
2450         }
2451
2452         cpr->cons = last_cons;
2453         cpr->v_bit = last_v_bit;
2454         BNXT_CP_IDX_ENABLE_DB(&cpr->ring, cpr->cons);
2455 }
2456
2457 static uint8_t
2458 get_phy_type(struct bnxt_softc *softc)
2459 {
2460         struct bnxt_link_info *link_info = &softc->link_info;
2461         uint8_t phy_type = link_info->phy_type;
2462         uint16_t supported;
2463
2464         if (phy_type != HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_UNKNOWN)
2465                 return phy_type;
2466
2467         /* Deduce the phy type from the media type and supported speeds */
2468         supported = link_info->support_speeds;
2469
2470         if (link_info->media_type ==
2471             HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_TP)
2472                 return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASET;
2473         if (link_info->media_type ==
2474             HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_DAC) {
2475                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_2_5GB)
2476                         return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKX;
2477                 if (supported & HWRM_PORT_PHY_QCFG_OUTPUT_SUPPORT_SPEEDS_20GB)
2478                         return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASEKR;
2479                 return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASECR;
2480         }
2481         if (link_info->media_type ==
2482             HWRM_PORT_PHY_QCFG_OUTPUT_MEDIA_TYPE_FIBRE)
2483                 return HWRM_PORT_PHY_QCFG_OUTPUT_PHY_TYPE_BASESR;
2484
2485         return phy_type;
2486 }
2487
2488 bool
2489 bnxt_check_hwrm_version(struct bnxt_softc *softc)
2490 {
2491         char buf[16];
2492
2493         sprintf(buf, "%hhu.%hhu.%hhu", softc->ver_info->hwrm_min_major,
2494             softc->ver_info->hwrm_min_minor, softc->ver_info->hwrm_min_update);
2495         if (softc->ver_info->hwrm_min_major > softc->ver_info->hwrm_if_major) {
2496                 device_printf(softc->dev,
2497                     "WARNING: HWRM version %s is too old (older than %s)\n",
2498                     softc->ver_info->hwrm_if_ver, buf);
2499                 return false;
2500         }
2501         else if(softc->ver_info->hwrm_min_major ==
2502             softc->ver_info->hwrm_if_major) {
2503                 if (softc->ver_info->hwrm_min_minor >
2504                     softc->ver_info->hwrm_if_minor) {
2505                         device_printf(softc->dev,
2506                             "WARNING: HWRM version %s is too old (older than %s)\n",
2507                             softc->ver_info->hwrm_if_ver, buf);
2508                         return false;
2509                 }
2510                 else if (softc->ver_info->hwrm_min_minor ==
2511                     softc->ver_info->hwrm_if_minor) {
2512                         if (softc->ver_info->hwrm_min_update >
2513                             softc->ver_info->hwrm_if_update) {
2514                                 device_printf(softc->dev,
2515                                     "WARNING: HWRM version %s is too old (older than %s)\n",
2516                                     softc->ver_info->hwrm_if_ver, buf);
2517                                 return false;
2518                         }
2519                 }
2520         }
2521         return true;
2522 }
2523
2524 static uint64_t
2525 bnxt_get_baudrate(struct bnxt_link_info *link)
2526 {
2527         switch (link->link_speed) {
2528         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100MB:
2529                 return IF_Mbps(100);
2530         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_1GB:
2531                 return IF_Gbps(1);
2532         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2GB:
2533                 return IF_Gbps(2);
2534         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_2_5GB:
2535                 return IF_Mbps(2500);
2536         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10GB:
2537                 return IF_Gbps(10);
2538         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_20GB:
2539                 return IF_Gbps(20);
2540         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_25GB:
2541                 return IF_Gbps(25);
2542         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_40GB:
2543                 return IF_Gbps(40);
2544         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_50GB:
2545                 return IF_Gbps(50);
2546         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_100GB:
2547                 return IF_Gbps(100);
2548         case HWRM_PORT_PHY_QCFG_OUTPUT_LINK_SPEED_10MB:
2549                 return IF_Mbps(10);
2550         }
2551         return IF_Gbps(100);
2552 }
2553
2554 static void
2555 bnxt_get_wol_settings(struct bnxt_softc *softc)
2556 {
2557         uint16_t wol_handle = 0;
2558
2559         if (!bnxt_wol_supported(softc))
2560                 return;
2561
2562         do {
2563                 wol_handle = bnxt_hwrm_get_wol_fltrs(softc, wol_handle);
2564         } while (wol_handle && wol_handle != BNXT_NO_MORE_WOL_FILTERS);
2565 }