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