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