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