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