xref: /freebsd/sys/dev/axgbe/if_axgbe_pci.c (revision 1edb7116)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2020 Advanced Micro Devices, Inc.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * Contact Information :
28  * Rajesh Kumar <rajesh1.kumar@amd.com>
29  * Shreyank Amartya <Shreyank.Amartya@amd.com>
30  */
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/mutex.h>
38 #include <sys/rman.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
41 #include <sys/systm.h>
42 
43 #include <net/if.h>
44 #include <net/if_media.h>
45 
46 #include <dev/mii/mii.h>
47 #include <dev/mii/miivar.h>
48 
49 #include <dev/pci/pcireg.h>
50 #include <dev/pci/pcivar.h>
51 
52 #include "xgbe.h"
53 #include "xgbe-common.h"
54 
55 #include "miibus_if.h"
56 #include "ifdi_if.h"
57 #include "opt_inet.h"
58 #include "opt_inet6.h"
59 
60 MALLOC_DEFINE(M_AXGBE, "axgbe", "axgbe data");
61 
62 extern struct if_txrx axgbe_txrx;
63 static int axgbe_sph_enable;
64 
65 /* Function prototypes */
66 static void *axgbe_register(device_t);
67 static int axgbe_if_attach_pre(if_ctx_t);
68 static int axgbe_if_attach_post(if_ctx_t);
69 static int axgbe_if_detach(if_ctx_t);
70 static void axgbe_if_stop(if_ctx_t);
71 static void axgbe_if_init(if_ctx_t);
72 
73 /* Queue related routines */
74 static int axgbe_if_tx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
75 static int axgbe_if_rx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
76 static int axgbe_alloc_channels(if_ctx_t);
77 static void axgbe_free_channels(struct axgbe_if_softc *);
78 static void axgbe_if_queues_free(if_ctx_t);
79 static int axgbe_if_tx_queue_intr_enable(if_ctx_t, uint16_t);
80 static int axgbe_if_rx_queue_intr_enable(if_ctx_t, uint16_t);
81 
82 /* Interrupt related routines */
83 static void axgbe_if_disable_intr(if_ctx_t);
84 static void axgbe_if_enable_intr(if_ctx_t);
85 static int axgbe_if_msix_intr_assign(if_ctx_t, int);
86 static void xgbe_free_intr(struct xgbe_prv_data *, struct resource *, void *, int);
87 
88 /* Init and Iflib routines */
89 static void axgbe_pci_init(struct xgbe_prv_data *);
90 static void axgbe_pci_stop(if_ctx_t);
91 static void xgbe_disable_rx_tx_int(struct xgbe_prv_data *, struct xgbe_channel *);
92 static void xgbe_disable_rx_tx_ints(struct xgbe_prv_data *);
93 static int axgbe_if_mtu_set(if_ctx_t, uint32_t);
94 static void axgbe_if_update_admin_status(if_ctx_t);
95 static void axgbe_if_media_status(if_ctx_t, struct ifmediareq *);
96 static int axgbe_if_media_change(if_ctx_t);
97 static int axgbe_if_promisc_set(if_ctx_t, int);
98 static uint64_t axgbe_if_get_counter(if_ctx_t, ift_counter);
99 static void axgbe_if_vlan_register(if_ctx_t, uint16_t);
100 static void axgbe_if_vlan_unregister(if_ctx_t, uint16_t);
101 #if __FreeBSD_version >= 1300000
102 static bool axgbe_if_needs_restart(if_ctx_t, enum iflib_restart_event);
103 #endif
104 static void axgbe_set_counts(if_ctx_t);
105 static void axgbe_init_iflib_softc_ctx(struct axgbe_if_softc *);
106 
107 /* MII interface registered functions */
108 static int axgbe_miibus_readreg(device_t, int, int);
109 static int axgbe_miibus_writereg(device_t, int, int, int);
110 static void axgbe_miibus_statchg(device_t);
111 
112 /* ISR routines */
113 static int axgbe_dev_isr(void *);
114 static void axgbe_ecc_isr(void *);
115 static void axgbe_i2c_isr(void *);
116 static void axgbe_an_isr(void *);
117 static int axgbe_msix_que(void *);
118 
119 /* Timer routines */
120 static void xgbe_service(void *, int);
121 static void xgbe_service_timer(void *);
122 static void xgbe_init_timers(struct xgbe_prv_data *);
123 static void xgbe_stop_timers(struct xgbe_prv_data *);
124 
125 /* Dump routines */
126 static void xgbe_dump_prop_registers(struct xgbe_prv_data *);
127 
128 /*
129  * Allocate only for MAC (BAR0) and PCS (BAR1) registers, and just point the
130  * MSI-X table bar  (BAR5) to iflib. iflib will do the allocation for MSI-X
131  * table.
132  */
133 static struct resource_spec axgbe_pci_mac_spec[] = {
134 	{ SYS_RES_MEMORY, PCIR_BAR(0), RF_ACTIVE }, /* MAC regs */
135 	{ SYS_RES_MEMORY, PCIR_BAR(1), RF_ACTIVE }, /* PCS regs */
136 	{ -1, 0 }
137 };
138 
139 static const pci_vendor_info_t axgbe_vendor_info_array[] =
140 {
141 	PVID(0x1022, 0x1458,  "AMD 10 Gigabit Ethernet Driver"),
142 	PVID(0x1022, 0x1459,  "AMD 10 Gigabit Ethernet Driver"),
143 	PVID_END
144 };
145 
146 static struct xgbe_version_data xgbe_v2a = {
147 	.init_function_ptrs_phy_impl    = xgbe_init_function_ptrs_phy_v2,
148 	.xpcs_access                    = XGBE_XPCS_ACCESS_V2,
149 	.mmc_64bit                      = 1,
150 	.tx_max_fifo_size               = 229376,
151 	.rx_max_fifo_size               = 229376,
152 	.tx_tstamp_workaround           = 1,
153 	.ecc_support                    = 1,
154 	.i2c_support                    = 1,
155 	.irq_reissue_support            = 1,
156 	.tx_desc_prefetch               = 5,
157 	.rx_desc_prefetch               = 5,
158 	.an_cdr_workaround              = 1,
159 };
160 
161 static struct xgbe_version_data xgbe_v2b = {
162 	.init_function_ptrs_phy_impl    = xgbe_init_function_ptrs_phy_v2,
163 	.xpcs_access                    = XGBE_XPCS_ACCESS_V2,
164 	.mmc_64bit                      = 1,
165 	.tx_max_fifo_size               = 65536,
166 	.rx_max_fifo_size               = 65536,
167 	.tx_tstamp_workaround           = 1,
168 	.ecc_support                    = 1,
169 	.i2c_support                    = 1,
170 	.irq_reissue_support            = 1,
171 	.tx_desc_prefetch               = 5,
172 	.rx_desc_prefetch               = 5,
173 	.an_cdr_workaround              = 1,
174 };
175 
176 /* Device Interface */
177 static device_method_t ax_methods[] = {
178 	DEVMETHOD(device_register, axgbe_register),
179 	DEVMETHOD(device_probe, iflib_device_probe),
180 	DEVMETHOD(device_attach, iflib_device_attach),
181 	DEVMETHOD(device_detach, iflib_device_detach),
182 
183 	/* MII interface */
184 	DEVMETHOD(miibus_readreg, axgbe_miibus_readreg),
185 	DEVMETHOD(miibus_writereg, axgbe_miibus_writereg),
186 	DEVMETHOD(miibus_statchg, axgbe_miibus_statchg),
187 
188 	DEVMETHOD_END
189 };
190 
191 static driver_t ax_driver = {
192 	"ax", ax_methods, sizeof(struct axgbe_if_softc),
193 };
194 
195 DRIVER_MODULE(axp, pci, ax_driver, 0, 0);
196 DRIVER_MODULE(miibus, ax, miibus_driver, 0, 0);
197 IFLIB_PNP_INFO(pci, ax_driver, axgbe_vendor_info_array);
198 
199 MODULE_DEPEND(ax, pci, 1, 1, 1);
200 MODULE_DEPEND(ax, ether, 1, 1, 1);
201 MODULE_DEPEND(ax, iflib, 1, 1, 1);
202 MODULE_DEPEND(ax, miibus, 1, 1, 1);
203 
204 /* Iflib Interface */
205 static device_method_t axgbe_if_methods[] = {
206 	DEVMETHOD(ifdi_attach_pre, axgbe_if_attach_pre),
207 	DEVMETHOD(ifdi_attach_post, axgbe_if_attach_post),
208 	DEVMETHOD(ifdi_detach, axgbe_if_detach),
209 	DEVMETHOD(ifdi_init, axgbe_if_init),
210 	DEVMETHOD(ifdi_stop, axgbe_if_stop),
211 	DEVMETHOD(ifdi_msix_intr_assign, axgbe_if_msix_intr_assign),
212 	DEVMETHOD(ifdi_intr_enable, axgbe_if_enable_intr),
213 	DEVMETHOD(ifdi_intr_disable, axgbe_if_disable_intr),
214 	DEVMETHOD(ifdi_tx_queue_intr_enable, axgbe_if_tx_queue_intr_enable),
215 	DEVMETHOD(ifdi_rx_queue_intr_enable, axgbe_if_rx_queue_intr_enable),
216 	DEVMETHOD(ifdi_tx_queues_alloc, axgbe_if_tx_queues_alloc),
217 	DEVMETHOD(ifdi_rx_queues_alloc, axgbe_if_rx_queues_alloc),
218 	DEVMETHOD(ifdi_queues_free, axgbe_if_queues_free),
219 	DEVMETHOD(ifdi_update_admin_status, axgbe_if_update_admin_status),
220 	DEVMETHOD(ifdi_mtu_set, axgbe_if_mtu_set),
221 	DEVMETHOD(ifdi_media_status, axgbe_if_media_status),
222 	DEVMETHOD(ifdi_media_change, axgbe_if_media_change),
223 	DEVMETHOD(ifdi_promisc_set, axgbe_if_promisc_set),
224 	DEVMETHOD(ifdi_get_counter, axgbe_if_get_counter),
225 	DEVMETHOD(ifdi_vlan_register, axgbe_if_vlan_register),
226 	DEVMETHOD(ifdi_vlan_unregister, axgbe_if_vlan_unregister),
227 #if __FreeBSD_version >= 1300000
228 	DEVMETHOD(ifdi_needs_restart, axgbe_if_needs_restart),
229 #endif
230 	DEVMETHOD_END
231 };
232 
233 static driver_t axgbe_if_driver = {
234 	"axgbe_if", axgbe_if_methods, sizeof(struct axgbe_if_softc)
235 };
236 
237 /* Iflib Shared Context */
238 static struct if_shared_ctx axgbe_sctx_init = {
239 	.isc_magic = IFLIB_MAGIC,
240 	.isc_driver = &axgbe_if_driver,
241 	.isc_q_align = PAGE_SIZE,
242 	.isc_tx_maxsize = XGBE_TSO_MAX_SIZE + sizeof(struct ether_vlan_header),
243 	.isc_tx_maxsegsize = PAGE_SIZE,
244 	.isc_tso_maxsize = XGBE_TSO_MAX_SIZE + sizeof(struct ether_vlan_header),
245 	.isc_tso_maxsegsize = PAGE_SIZE,
246 	.isc_rx_maxsize = MJUM9BYTES,
247 	.isc_rx_maxsegsize = MJUM9BYTES,
248 	.isc_rx_nsegments = 1,
249 	.isc_admin_intrcnt = 4,
250 
251 	.isc_vendor_info = axgbe_vendor_info_array,
252 	.isc_driver_version = XGBE_DRV_VERSION,
253 
254 	.isc_ntxd_min = {XGBE_TX_DESC_CNT_MIN},
255 	.isc_ntxd_default = {XGBE_TX_DESC_CNT_DEFAULT},
256 	.isc_ntxd_max = {XGBE_TX_DESC_CNT_MAX},
257 
258 	.isc_ntxqs = 1,
259 	.isc_flags = IFLIB_TSO_INIT_IP | IFLIB_NEED_SCRATCH |
260 	    IFLIB_NEED_ZERO_CSUM | IFLIB_NEED_ETHER_PAD,
261 };
262 
263 static void *
264 axgbe_register(device_t dev)
265 {
266 	int axgbe_nfl;
267 	int axgbe_nrxqs;
268 	int error, i;
269 	char *value = NULL;
270 
271 	value = kern_getenv("dev.ax.sph_enable");
272 	if (value) {
273 		axgbe_sph_enable = strtol(value, NULL, 10);
274 		freeenv(value);
275 	} else {
276 		/*
277 		 * No tunable found, generate one with default values
278 		 * Note: only a reboot will reveal the new kenv
279 		 */
280 		error = kern_setenv("dev.ax.sph_enable", "1");
281 		if (error) {
282 			printf("Error setting tunable, using default driver values\n");
283 		}
284 		axgbe_sph_enable = 1;
285 	}
286 
287 	if (!axgbe_sph_enable) {
288 		axgbe_nfl = 1;
289 		axgbe_nrxqs = 1;
290 	} else {
291 		axgbe_nfl = 2;
292 		axgbe_nrxqs = 2;
293 	}
294 
295 	axgbe_sctx_init.isc_nfl = axgbe_nfl;
296 	axgbe_sctx_init.isc_nrxqs = axgbe_nrxqs;
297 
298 	for (i = 0 ; i < axgbe_nrxqs ; i++) {
299 		axgbe_sctx_init.isc_nrxd_min[i] = XGBE_RX_DESC_CNT_MIN;
300 		axgbe_sctx_init.isc_nrxd_default[i] = XGBE_RX_DESC_CNT_DEFAULT;
301 		axgbe_sctx_init.isc_nrxd_max[i] = XGBE_RX_DESC_CNT_MAX;
302 	}
303 
304 	return (&axgbe_sctx_init);
305 }
306 
307 /* MII Interface Functions */
308 static int
309 axgbe_miibus_readreg(device_t dev, int phy, int reg)
310 {
311 	struct axgbe_if_softc   *sc = iflib_get_softc(device_get_softc(dev));
312 	struct xgbe_prv_data    *pdata = &sc->pdata;
313 	int val;
314 
315 	axgbe_printf(3, "%s: phy %d reg %d\n", __func__, phy, reg);
316 
317 	val = xgbe_phy_mii_read(pdata, phy, reg);
318 
319 	axgbe_printf(2, "%s: val 0x%x\n", __func__, val);
320 	return (val & 0xFFFF);
321 }
322 
323 static int
324 axgbe_miibus_writereg(device_t dev, int phy, int reg, int val)
325 {
326 	struct axgbe_if_softc   *sc = iflib_get_softc(device_get_softc(dev));
327 	struct xgbe_prv_data    *pdata = &sc->pdata;
328 
329 	axgbe_printf(3, "%s: phy %d reg %d val 0x%x\n", __func__, phy, reg, val);
330 
331 	xgbe_phy_mii_write(pdata, phy, reg, val);
332 
333 	return(0);
334 }
335 
336 static void
337 axgbe_miibus_statchg(device_t dev)
338 {
339         struct axgbe_if_softc   *sc = iflib_get_softc(device_get_softc(dev));
340         struct xgbe_prv_data    *pdata = &sc->pdata;
341 	struct mii_data		*mii = device_get_softc(pdata->axgbe_miibus);
342 	if_t			 ifp = pdata->netdev;
343 	int bmsr;
344 
345 	axgbe_printf(2, "%s: Link %d/%d\n", __func__, pdata->phy.link,
346 	    pdata->phy_link);
347 
348 	if (mii == NULL || ifp == NULL ||
349 	    (if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
350 		return;
351 
352 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
353 	    (IFM_ACTIVE | IFM_AVALID)) {
354 
355 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
356 		case IFM_10_T:
357 		case IFM_100_TX:
358 			pdata->phy.link = 1;
359 			break;
360 		case IFM_1000_T:
361 		case IFM_1000_SX:
362 		case IFM_2500_SX:
363 			pdata->phy.link = 1;
364 			break;
365 		default:
366 			pdata->phy.link = 0;
367 			break;
368 		}
369 	} else
370 		pdata->phy_link = 0;
371 
372 	bmsr = axgbe_miibus_readreg(pdata->dev, pdata->mdio_addr, MII_BMSR);
373 	if (bmsr & BMSR_ANEG) {
374 
375 		axgbe_printf(2, "%s: Autoneg Done\n", __func__);
376 
377 		/* Raise AN Interrupt */
378 		XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK,
379 		    XGBE_AN_CL73_INT_MASK);
380 	}
381 }
382 
383 static int
384 axgbe_if_attach_pre(if_ctx_t ctx)
385 {
386 	struct axgbe_if_softc	*sc;
387 	struct xgbe_prv_data	*pdata;
388 	struct resource		*mac_res[2];
389 	if_softc_ctx_t		scctx;
390 	if_shared_ctx_t		sctx;
391 	device_t		dev;
392 	unsigned int		ma_lo, ma_hi;
393 	unsigned int		reg;
394 	int			ret;
395 
396 	sc = iflib_get_softc(ctx);
397 	sc->pdata.dev = dev = iflib_get_dev(ctx);
398 	sc->sctx = sctx = iflib_get_sctx(ctx);
399 	sc->scctx = scctx = iflib_get_softc_ctx(ctx);
400 	sc->media = iflib_get_media(ctx);
401 	sc->ctx = ctx;
402 	sc->link_status = LINK_STATE_DOWN;
403 	pdata = &sc->pdata;
404 	pdata->netdev = iflib_get_ifp(ctx);
405 
406 	spin_lock_init(&pdata->xpcs_lock);
407 
408 	/* Initialize locks */
409         mtx_init(&pdata->rss_mutex, "xgbe rss mutex lock", NULL, MTX_DEF);
410 	mtx_init(&pdata->mdio_mutex, "xgbe MDIO mutex lock", NULL, MTX_SPIN);
411 
412 	/* Allocate VLAN bitmap */
413 	pdata->active_vlans = bit_alloc(VLAN_NVID, M_AXGBE, M_WAITOK|M_ZERO);
414 	pdata->num_active_vlans = 0;
415 
416 	/* Get the version data */
417 	DBGPR("%s: Device ID: 0x%x\n", __func__, pci_get_device(dev));
418 	if (pci_get_device(dev) == 0x1458)
419 		sc->pdata.vdata = &xgbe_v2a;
420 	else if (pci_get_device(dev) == 0x1459)
421 		sc->pdata.vdata = &xgbe_v2b;
422 
423 	/* PCI setup */
424         if (bus_alloc_resources(dev, axgbe_pci_mac_spec, mac_res)) {
425 		axgbe_error("Unable to allocate bus resources\n");
426 		ret = ENXIO;
427 		goto free_vlans;
428 	}
429 
430         sc->pdata.xgmac_res = mac_res[0];
431         sc->pdata.xpcs_res = mac_res[1];
432 
433         /* Set the PCS indirect addressing definition registers*/
434 	pdata->xpcs_window_def_reg = PCS_V2_WINDOW_DEF;
435 	pdata->xpcs_window_sel_reg = PCS_V2_WINDOW_SELECT;
436 
437         /* Configure the PCS indirect addressing support */
438 	reg = XPCS32_IOREAD(pdata, pdata->xpcs_window_def_reg);
439 	pdata->xpcs_window = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, OFFSET);
440 	pdata->xpcs_window <<= 6;
441 	pdata->xpcs_window_size = XPCS_GET_BITS(reg, PCS_V2_WINDOW_DEF, SIZE);
442 	pdata->xpcs_window_size = 1 << (pdata->xpcs_window_size + 7);
443 	pdata->xpcs_window_mask = pdata->xpcs_window_size - 1;
444 	DBGPR("xpcs window def : %#010x\n",
445 	    pdata->xpcs_window_def_reg);
446 	DBGPR("xpcs window sel : %#010x\n",
447 	    pdata->xpcs_window_sel_reg);
448         DBGPR("xpcs window : %#010x\n",
449 	    pdata->xpcs_window);
450 	DBGPR("xpcs window size : %#010x\n",
451 	    pdata->xpcs_window_size);
452 	DBGPR("xpcs window mask : %#010x\n",
453 	    pdata->xpcs_window_mask);
454 
455 	/* Enable all interrupts in the hardware */
456         XP_IOWRITE(pdata, XP_INT_EN, 0x1fffff);
457 
458 	/* Retrieve the MAC address */
459 	ma_lo = XP_IOREAD(pdata, XP_MAC_ADDR_LO);
460 	ma_hi = XP_IOREAD(pdata, XP_MAC_ADDR_HI);
461 	pdata->mac_addr[0] = ma_lo & 0xff;
462 	pdata->mac_addr[1] = (ma_lo >> 8) & 0xff;
463 	pdata->mac_addr[2] = (ma_lo >>16) & 0xff;
464 	pdata->mac_addr[3] = (ma_lo >> 24) & 0xff;
465 	pdata->mac_addr[4] = ma_hi & 0xff;
466 	pdata->mac_addr[5] = (ma_hi >> 8) & 0xff;
467 	if (!XP_GET_BITS(ma_hi, XP_MAC_ADDR_HI, VALID)) {
468 		axgbe_error("Invalid mac address\n");
469 		ret = EINVAL;
470 		goto release_bus_resource;
471 	}
472 	iflib_set_mac(ctx, pdata->mac_addr);
473 
474 	/* Clock settings */
475 	pdata->sysclk_rate = XGBE_V2_DMA_CLOCK_FREQ;
476 	pdata->ptpclk_rate = XGBE_V2_PTP_CLOCK_FREQ;
477 
478 	/* Set the DMA coherency values */
479 	pdata->coherent = 1;
480 	pdata->arcr = XGBE_DMA_PCI_ARCR;
481 	pdata->awcr = XGBE_DMA_PCI_AWCR;
482 	pdata->awarcr = XGBE_DMA_PCI_AWARCR;
483 
484 	/* Read the port property registers */
485 	pdata->pp0 = XP_IOREAD(pdata, XP_PROP_0);
486 	pdata->pp1 = XP_IOREAD(pdata, XP_PROP_1);
487 	pdata->pp2 = XP_IOREAD(pdata, XP_PROP_2);
488 	pdata->pp3 = XP_IOREAD(pdata, XP_PROP_3);
489 	pdata->pp4 = XP_IOREAD(pdata, XP_PROP_4);
490 	DBGPR("port property 0 = %#010x\n", pdata->pp0);
491 	DBGPR("port property 1 = %#010x\n", pdata->pp1);
492 	DBGPR("port property 2 = %#010x\n", pdata->pp2);
493 	DBGPR("port property 3 = %#010x\n", pdata->pp3);
494 	DBGPR("port property 4 = %#010x\n", pdata->pp4);
495 
496 	/* Set the maximum channels and queues */
497 	pdata->tx_max_channel_count = XP_GET_BITS(pdata->pp1, XP_PROP_1,
498 	    MAX_TX_DMA);
499 	pdata->rx_max_channel_count = XP_GET_BITS(pdata->pp1, XP_PROP_1,
500 	    MAX_RX_DMA);
501 	pdata->tx_max_q_count = XP_GET_BITS(pdata->pp1, XP_PROP_1,
502 	    MAX_TX_QUEUES);
503 	pdata->rx_max_q_count = XP_GET_BITS(pdata->pp1, XP_PROP_1,
504 	    MAX_RX_QUEUES);
505 	DBGPR("max tx/rx channel count = %u/%u\n",
506 	    pdata->tx_max_channel_count, pdata->rx_max_channel_count);
507 	DBGPR("max tx/rx hw queue count = %u/%u\n",
508 	    pdata->tx_max_q_count, pdata->rx_max_q_count);
509 
510 	axgbe_set_counts(ctx);
511 
512 	/* Set the maximum fifo amounts */
513         pdata->tx_max_fifo_size = XP_GET_BITS(pdata->pp2, XP_PROP_2,
514                                               TX_FIFO_SIZE);
515         pdata->tx_max_fifo_size *= 16384;
516         pdata->tx_max_fifo_size = min(pdata->tx_max_fifo_size,
517                                       pdata->vdata->tx_max_fifo_size);
518         pdata->rx_max_fifo_size = XP_GET_BITS(pdata->pp2, XP_PROP_2,
519                                               RX_FIFO_SIZE);
520         pdata->rx_max_fifo_size *= 16384;
521         pdata->rx_max_fifo_size = min(pdata->rx_max_fifo_size,
522                                       pdata->vdata->rx_max_fifo_size);
523 	DBGPR("max tx/rx max fifo size = %u/%u\n",
524 	    pdata->tx_max_fifo_size, pdata->rx_max_fifo_size);
525 
526 	/* Initialize IFLIB if_softc_ctx_t */
527 	axgbe_init_iflib_softc_ctx(sc);
528 
529 	/* Alloc channels */
530 	if (axgbe_alloc_channels(ctx)) {
531 		axgbe_error("Unable to allocate channel memory\n");
532 		ret = ENOMEM;
533 		goto release_bus_resource;
534         }
535 
536 	TASK_INIT(&pdata->service_work, 0, xgbe_service, pdata);
537 
538 	/* create the workqueue */
539 	pdata->dev_workqueue = taskqueue_create("axgbe", M_WAITOK,
540 	    taskqueue_thread_enqueue, &pdata->dev_workqueue);
541 	if (pdata->dev_workqueue == NULL) {
542 		axgbe_error("Unable to allocate workqueue\n");
543 		ret = ENOMEM;
544 		goto free_channels;
545 	}
546 	ret = taskqueue_start_threads(&pdata->dev_workqueue, 1, PI_NET,
547 	    "axgbe dev taskq");
548 	if (ret) {
549 		axgbe_error("Unable to start taskqueue\n");
550 		ret = ENOMEM;
551 		goto free_task_queue;
552 	}
553 
554 	/* Init timers */
555 	xgbe_init_timers(pdata);
556 
557         return (0);
558 
559 free_task_queue:
560 	taskqueue_free(pdata->dev_workqueue);
561 
562 free_channels:
563 	axgbe_free_channels(sc);
564 
565 release_bus_resource:
566         bus_release_resources(dev, axgbe_pci_mac_spec, mac_res);
567 
568 free_vlans:
569 	free(pdata->active_vlans, M_AXGBE);
570 
571 	return (ret);
572 } /* axgbe_if_attach_pre */
573 
574 static void
575 xgbe_init_all_fptrs(struct xgbe_prv_data *pdata)
576 {
577 	xgbe_init_function_ptrs_dev(&pdata->hw_if);
578 	xgbe_init_function_ptrs_phy(&pdata->phy_if);
579         xgbe_init_function_ptrs_i2c(&pdata->i2c_if);
580 	xgbe_init_function_ptrs_desc(&pdata->desc_if);
581 
582         pdata->vdata->init_function_ptrs_phy_impl(&pdata->phy_if);
583 }
584 
585 static void
586 axgbe_set_counts(if_ctx_t ctx)
587 {
588 	struct axgbe_if_softc *sc = iflib_get_softc(ctx);
589 	struct xgbe_prv_data *pdata = &sc->pdata;
590 	cpuset_t lcpus;
591 	int cpu_count, err;
592 	size_t len;
593 
594 	/* Set all function pointers */
595 	xgbe_init_all_fptrs(pdata);
596 
597 	/* Populate the hardware features */
598 	xgbe_get_all_hw_features(pdata);
599 
600 	if (!pdata->tx_max_channel_count)
601 		pdata->tx_max_channel_count = pdata->hw_feat.tx_ch_cnt;
602 	if (!pdata->rx_max_channel_count)
603 		pdata->rx_max_channel_count = pdata->hw_feat.rx_ch_cnt;
604 
605 	if (!pdata->tx_max_q_count)
606 		pdata->tx_max_q_count = pdata->hw_feat.tx_q_cnt;
607 	if (!pdata->rx_max_q_count)
608 		pdata->rx_max_q_count = pdata->hw_feat.rx_q_cnt;
609 
610 	/*
611 	 * Calculate the number of Tx and Rx rings to be created
612 	 *  -Tx (DMA) Channels map 1-to-1 to Tx Queues so set
613 	 *   the number of Tx queues to the number of Tx channels
614 	 *   enabled
615 	 *  -Rx (DMA) Channels do not map 1-to-1 so use the actual
616 	 *   number of Rx queues or maximum allowed
617 	 */
618 
619 	/* Get cpu count from sysctl */
620 	len = sizeof(cpu_count);
621 	err = kernel_sysctlbyname(curthread, "hw.ncpu", &cpu_count, &len, NULL,
622 	    0, NULL, 0);
623 	if (err) {
624 		axgbe_error("Unable to fetch number of cpus\n");
625 		cpu_count = 1;
626 	}
627 
628 	if (bus_get_cpus(pdata->dev, INTR_CPUS, sizeof(lcpus), &lcpus) != 0) {
629                 axgbe_error("Unable to fetch CPU list\n");
630                 /* TODO - handle CPU_COPY(&all_cpus, &lcpus); */
631         }
632 
633 	DBGPR("ncpu %d intrcpu %d\n", cpu_count, CPU_COUNT(&lcpus));
634 
635 	pdata->tx_ring_count = min(CPU_COUNT(&lcpus), pdata->hw_feat.tx_ch_cnt);
636 	pdata->tx_ring_count = min(pdata->tx_ring_count,
637 	    pdata->tx_max_channel_count);
638 	pdata->tx_ring_count = min(pdata->tx_ring_count, pdata->tx_max_q_count);
639 
640 	pdata->tx_q_count = pdata->tx_ring_count;
641 
642 	pdata->rx_ring_count = min(CPU_COUNT(&lcpus), pdata->hw_feat.rx_ch_cnt);
643 	pdata->rx_ring_count = min(pdata->rx_ring_count,
644 	    pdata->rx_max_channel_count);
645 
646 	pdata->rx_q_count = min(pdata->hw_feat.rx_q_cnt, pdata->rx_max_q_count);
647 
648 	DBGPR("TX/RX max channel count = %u/%u\n",
649 	    pdata->tx_max_channel_count, pdata->rx_max_channel_count);
650 	DBGPR("TX/RX max queue count = %u/%u\n",
651 	    pdata->tx_max_q_count, pdata->rx_max_q_count);
652 	DBGPR("TX/RX DMA ring count = %u/%u\n",
653 	    pdata->tx_ring_count, pdata->rx_ring_count);
654 	DBGPR("TX/RX hardware queue count = %u/%u\n",
655 	    pdata->tx_q_count, pdata->rx_q_count);
656 } /* axgbe_set_counts */
657 
658 static void
659 axgbe_init_iflib_softc_ctx(struct axgbe_if_softc *sc)
660 {
661 	struct xgbe_prv_data *pdata = &sc->pdata;
662 	if_softc_ctx_t scctx = sc->scctx;
663 	if_shared_ctx_t sctx = sc->sctx;
664 	int i;
665 
666 	scctx->isc_nrxqsets = pdata->rx_q_count;
667 	scctx->isc_ntxqsets = pdata->tx_q_count;
668 	scctx->isc_msix_bar = pci_msix_table_bar(pdata->dev);
669 	scctx->isc_tx_nsegments = 32;
670 
671 	for (i = 0; i < sctx->isc_ntxqs; i++) {
672 		scctx->isc_txqsizes[i] =
673 		    roundup2(scctx->isc_ntxd[i] * sizeof(struct xgbe_ring_desc),
674 		    128);
675 		scctx->isc_txd_size[i] = sizeof(struct xgbe_ring_desc);
676 	}
677 
678 	for (i = 0; i < sctx->isc_nrxqs; i++) {
679 		scctx->isc_rxqsizes[i] =
680 		    roundup2(scctx->isc_nrxd[i] * sizeof(struct xgbe_ring_desc),
681 		    128);
682 		scctx->isc_rxd_size[i] = sizeof(struct xgbe_ring_desc);
683 	}
684 
685 	scctx->isc_tx_tso_segments_max = 32;
686 	scctx->isc_tx_tso_size_max = XGBE_TSO_MAX_SIZE;
687 	scctx->isc_tx_tso_segsize_max = PAGE_SIZE;
688 
689 	/*
690 	 * Set capabilities
691 	 * 1) IFLIB automatically adds IFCAP_HWSTATS, so need to set explicitly
692 	 * 2) isc_tx_csum_flags is mandatory if IFCAP_TXCSUM (included in
693 	 *    IFCAP_HWCSUM) is set
694 	 */
695 	scctx->isc_tx_csum_flags = (CSUM_IP | CSUM_TCP | CSUM_UDP | CSUM_SCTP |
696 	    CSUM_TCP_IPV6 | CSUM_UDP_IPV6 | CSUM_SCTP_IPV6 |
697 	    CSUM_TSO);
698 	scctx->isc_capenable = (IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6 |
699 	    IFCAP_JUMBO_MTU |
700 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWFILTER |
701 	    IFCAP_VLAN_HWCSUM |
702 	    IFCAP_TSO | IFCAP_VLAN_HWTSO);
703 	scctx->isc_capabilities = scctx->isc_capenable;
704 
705 	/*
706 	 * Set rss_table_size alone when adding RSS support. rss_table_mask
707 	 * will be set by IFLIB based on rss_table_size
708 	 */
709 	scctx->isc_rss_table_size = XGBE_RSS_MAX_TABLE_SIZE;
710 
711 	scctx->isc_ntxqsets_max = XGBE_MAX_QUEUES;
712 	scctx->isc_nrxqsets_max = XGBE_MAX_QUEUES;
713 
714 	scctx->isc_txrx = &axgbe_txrx;
715 }
716 
717 static int
718 axgbe_alloc_channels(if_ctx_t ctx)
719 {
720 	struct axgbe_if_softc 	*sc = iflib_get_softc(ctx);
721 	struct xgbe_prv_data	*pdata = &sc->pdata;
722 	struct xgbe_channel	*channel;
723 	int i, j, count;
724 
725 	DBGPR("%s: txqs %d rxqs %d\n", __func__, pdata->tx_ring_count,
726 	    pdata->rx_ring_count);
727 
728 	/* Iflibe sets based on isc_ntxqsets/nrxqsets */
729 	count = max_t(unsigned int, pdata->tx_ring_count, pdata->rx_ring_count);
730 
731 	/* Allocate channel memory */
732 	for (i = 0; i < count ; i++) {
733 		channel = (struct xgbe_channel*)malloc(sizeof(struct xgbe_channel),
734 		    M_AXGBE, M_NOWAIT | M_ZERO);
735 
736 		if (channel == NULL) {
737 			for (j = 0; j < i; j++) {
738 				free(pdata->channel[j], M_AXGBE);
739 				pdata->channel[j] = NULL;
740 			}
741 			return (ENOMEM);
742 		}
743 
744 		pdata->channel[i] = channel;
745 	}
746 
747 	pdata->total_channel_count = count;
748 	DBGPR("Channel count set to: %u\n", pdata->total_channel_count);
749 
750 	for (i = 0; i < count; i++) {
751 
752 		channel = pdata->channel[i];
753 		snprintf(channel->name, sizeof(channel->name), "channel-%d",i);
754 
755 		channel->pdata = pdata;
756 		channel->queue_index = i;
757 		channel->dma_tag = rman_get_bustag(pdata->xgmac_res);
758 		bus_space_subregion(channel->dma_tag,
759 		    rman_get_bushandle(pdata->xgmac_res),
760 		    DMA_CH_BASE + (DMA_CH_INC * i), DMA_CH_INC,
761 		    &channel->dma_handle);
762 		channel->tx_ring = NULL;
763 		channel->rx_ring = NULL;
764 	}
765 
766 	return (0);
767 } /* axgbe_alloc_channels */
768 
769 static void
770 axgbe_free_channels(struct axgbe_if_softc *sc)
771 {
772 	struct xgbe_prv_data	*pdata = &sc->pdata;
773 	int i;
774 
775 	for (i = 0; i < pdata->total_channel_count ; i++) {
776 		free(pdata->channel[i], M_AXGBE);
777 		pdata->channel[i] = NULL;
778 	}
779 
780 	pdata->total_channel_count = 0;
781 	pdata->channel_count = 0;
782 }
783 
784 static void
785 xgbe_service(void *ctx, int pending)
786 {
787         struct xgbe_prv_data *pdata = ctx;
788 	struct axgbe_if_softc *sc = (struct axgbe_if_softc *)pdata;
789 	bool prev_state = false;
790 
791 	/* Get previous link status */
792 	prev_state = pdata->phy.link;
793 
794         pdata->phy_if.phy_status(pdata);
795 
796 	if (prev_state != pdata->phy.link) {
797 		pdata->phy_link = pdata->phy.link;
798 		axgbe_if_update_admin_status(sc->ctx);
799 	}
800 
801         callout_reset(&pdata->service_timer, 1*hz, xgbe_service_timer, pdata);
802 }
803 
804 static void
805 xgbe_service_timer(void *data)
806 {
807         struct xgbe_prv_data *pdata = data;
808 
809         taskqueue_enqueue(pdata->dev_workqueue, &pdata->service_work);
810 }
811 
812 static void
813 xgbe_init_timers(struct xgbe_prv_data *pdata)
814 {
815         callout_init(&pdata->service_timer, 1);
816 }
817 
818 static void
819 xgbe_start_timers(struct xgbe_prv_data *pdata)
820 {
821 	callout_reset(&pdata->service_timer, 1*hz, xgbe_service_timer, pdata);
822 }
823 
824 static void
825 xgbe_stop_timers(struct xgbe_prv_data *pdata)
826 {
827         callout_drain(&pdata->service_timer);
828         callout_stop(&pdata->service_timer);
829 }
830 
831 static void
832 xgbe_dump_phy_registers(struct xgbe_prv_data *pdata)
833 {
834         axgbe_printf(1, "\n************* PHY Reg dump *********************\n");
835 
836         axgbe_printf(1, "PCS Control Reg (%#06x) = %#06x\n", MDIO_CTRL1,
837             XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_CTRL1));
838         axgbe_printf(1, "PCS Status Reg (%#06x) = %#06x\n", MDIO_STAT1,
839             XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_STAT1));
840         axgbe_printf(1, "Phy Id (PHYS ID 1 %#06x)= %#06x\n", MDIO_DEVID1,
841             XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVID1));
842         axgbe_printf(1, "Phy Id (PHYS ID 2 %#06x)= %#06x\n", MDIO_DEVID2,
843             XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVID2));
844         axgbe_printf(1, "Devices in Package (%#06x)= %#06x\n", MDIO_DEVS1,
845             XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVS1));
846         axgbe_printf(1, "Devices in Package (%#06x)= %#06x\n", MDIO_DEVS2,
847             XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_DEVS2));
848         axgbe_printf(1, "Auto-Neg Control Reg (%#06x) = %#06x\n", MDIO_CTRL1,
849             XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_CTRL1));
850         axgbe_printf(1, "Auto-Neg Status Reg (%#06x) = %#06x\n", MDIO_STAT1,
851             XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_STAT1));
852         axgbe_printf(1, "Auto-Neg Ad Reg 1 (%#06x) = %#06x\n",
853             MDIO_AN_ADVERTISE,
854             XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE));
855         axgbe_printf(1, "Auto-Neg Ad Reg 2 (%#06x) = %#06x\n",
856             MDIO_AN_ADVERTISE + 1,
857             XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1));
858         axgbe_printf(1, "Auto-Neg Ad Reg 3 (%#06x) = %#06x\n",
859             MDIO_AN_ADVERTISE + 2,
860             XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2));
861         axgbe_printf(1, "Auto-Neg Completion Reg (%#06x) = %#06x\n",
862             MDIO_AN_COMP_STAT,
863             XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_COMP_STAT));
864 
865         axgbe_printf(1, "\n************************************************\n");
866 }
867 
868 static void
869 xgbe_dump_prop_registers(struct xgbe_prv_data *pdata)
870 {
871 	int i;
872 
873         axgbe_printf(1, "\n************* PROP Reg dump ********************\n");
874 
875 	for (i = 0 ; i < 38 ; i++) {
876 		axgbe_printf(1, "PROP Offset 0x%08x = %08x\n",
877 		    (XP_PROP_0 + (i * 4)), XP_IOREAD(pdata,
878 		    (XP_PROP_0 + (i * 4))));
879 	}
880 }
881 
882 static void
883 xgbe_dump_dma_registers(struct xgbe_prv_data *pdata, int ch)
884 {
885 	struct xgbe_channel     *channel;
886 	int i;
887 
888         axgbe_printf(1, "\n************* DMA Reg dump *********************\n");
889 
890         axgbe_printf(1, "DMA MR Reg (%08x) = %08x\n", DMA_MR,
891            XGMAC_IOREAD(pdata, DMA_MR));
892         axgbe_printf(1, "DMA SBMR Reg (%08x) = %08x\n", DMA_SBMR,
893            XGMAC_IOREAD(pdata, DMA_SBMR));
894         axgbe_printf(1, "DMA ISR Reg (%08x) = %08x\n", DMA_ISR,
895            XGMAC_IOREAD(pdata, DMA_ISR));
896         axgbe_printf(1, "DMA AXIARCR Reg (%08x) = %08x\n", DMA_AXIARCR,
897            XGMAC_IOREAD(pdata, DMA_AXIARCR));
898         axgbe_printf(1, "DMA AXIAWCR Reg (%08x) = %08x\n", DMA_AXIAWCR,
899            XGMAC_IOREAD(pdata, DMA_AXIAWCR));
900         axgbe_printf(1, "DMA AXIAWARCR Reg (%08x) = %08x\n", DMA_AXIAWARCR,
901            XGMAC_IOREAD(pdata, DMA_AXIAWARCR));
902         axgbe_printf(1, "DMA DSR0 Reg (%08x) = %08x\n", DMA_DSR0,
903            XGMAC_IOREAD(pdata, DMA_DSR0));
904         axgbe_printf(1, "DMA DSR1 Reg (%08x) = %08x\n", DMA_DSR1,
905            XGMAC_IOREAD(pdata, DMA_DSR1));
906         axgbe_printf(1, "DMA DSR2 Reg (%08x) = %08x\n", DMA_DSR2,
907            XGMAC_IOREAD(pdata, DMA_DSR2));
908         axgbe_printf(1, "DMA DSR3 Reg (%08x) = %08x\n", DMA_DSR3,
909            XGMAC_IOREAD(pdata, DMA_DSR3));
910         axgbe_printf(1, "DMA DSR4 Reg (%08x) = %08x\n", DMA_DSR4,
911            XGMAC_IOREAD(pdata, DMA_DSR4));
912         axgbe_printf(1, "DMA TXEDMACR Reg (%08x) = %08x\n", DMA_TXEDMACR,
913            XGMAC_IOREAD(pdata, DMA_TXEDMACR));
914         axgbe_printf(1, "DMA RXEDMACR Reg (%08x) = %08x\n", DMA_RXEDMACR,
915            XGMAC_IOREAD(pdata, DMA_RXEDMACR));
916 
917 	for (i = 0 ; i < 8 ; i++ ) {
918 
919 		if (ch >= 0) {
920 			if (i != ch)
921 				continue;
922 		}
923 
924 		channel = pdata->channel[i];
925 
926         	axgbe_printf(1, "\n************* DMA CH %d dump ****************\n", i);
927 
928         	axgbe_printf(1, "DMA_CH_CR Reg (%08x) = %08x\n",
929 		    DMA_CH_CR, XGMAC_DMA_IOREAD(channel, DMA_CH_CR));
930         	axgbe_printf(1, "DMA_CH_TCR Reg (%08x) = %08x\n",
931 		    DMA_CH_TCR, XGMAC_DMA_IOREAD(channel, DMA_CH_TCR));
932         	axgbe_printf(1, "DMA_CH_RCR Reg (%08x) = %08x\n",
933 		    DMA_CH_RCR, XGMAC_DMA_IOREAD(channel, DMA_CH_RCR));
934         	axgbe_printf(1, "DMA_CH_TDLR_HI Reg (%08x) = %08x\n",
935 		    DMA_CH_TDLR_HI, XGMAC_DMA_IOREAD(channel, DMA_CH_TDLR_HI));
936         	axgbe_printf(1, "DMA_CH_TDLR_LO Reg (%08x) = %08x\n",
937 		    DMA_CH_TDLR_LO, XGMAC_DMA_IOREAD(channel, DMA_CH_TDLR_LO));
938         	axgbe_printf(1, "DMA_CH_RDLR_HI Reg (%08x) = %08x\n",
939 		    DMA_CH_RDLR_HI, XGMAC_DMA_IOREAD(channel, DMA_CH_RDLR_HI));
940         	axgbe_printf(1, "DMA_CH_RDLR_LO Reg (%08x) = %08x\n",
941 		    DMA_CH_RDLR_LO, XGMAC_DMA_IOREAD(channel, DMA_CH_RDLR_LO));
942         	axgbe_printf(1, "DMA_CH_TDTR_LO Reg (%08x) = %08x\n",
943 		    DMA_CH_TDTR_LO, XGMAC_DMA_IOREAD(channel, DMA_CH_TDTR_LO));
944         	axgbe_printf(1, "DMA_CH_RDTR_LO Reg (%08x) = %08x\n",
945 		    DMA_CH_RDTR_LO, XGMAC_DMA_IOREAD(channel, DMA_CH_RDTR_LO));
946         	axgbe_printf(1, "DMA_CH_TDRLR Reg (%08x) = %08x\n",
947 		    DMA_CH_TDRLR, XGMAC_DMA_IOREAD(channel, DMA_CH_TDRLR));
948         	axgbe_printf(1, "DMA_CH_RDRLR Reg (%08x) = %08x\n",
949 		    DMA_CH_RDRLR, XGMAC_DMA_IOREAD(channel, DMA_CH_RDRLR));
950         	axgbe_printf(1, "DMA_CH_IER Reg (%08x) = %08x\n",
951 		    DMA_CH_IER, XGMAC_DMA_IOREAD(channel, DMA_CH_IER));
952         	axgbe_printf(1, "DMA_CH_RIWT Reg (%08x) = %08x\n",
953 		    DMA_CH_RIWT, XGMAC_DMA_IOREAD(channel, DMA_CH_RIWT));
954         	axgbe_printf(1, "DMA_CH_CATDR_LO Reg (%08x) = %08x\n",
955 		    DMA_CH_CATDR_LO, XGMAC_DMA_IOREAD(channel, DMA_CH_CATDR_LO));
956         	axgbe_printf(1, "DMA_CH_CARDR_LO Reg (%08x) = %08x\n",
957 		    DMA_CH_CARDR_LO, XGMAC_DMA_IOREAD(channel, DMA_CH_CARDR_LO));
958         	axgbe_printf(1, "DMA_CH_CATBR_HI Reg (%08x) = %08x\n",
959 		    DMA_CH_CATBR_HI, XGMAC_DMA_IOREAD(channel, DMA_CH_CATBR_HI));
960         	axgbe_printf(1, "DMA_CH_CATBR_LO Reg (%08x) = %08x\n",
961 		    DMA_CH_CATBR_LO, XGMAC_DMA_IOREAD(channel, DMA_CH_CATBR_LO));
962         	axgbe_printf(1, "DMA_CH_CARBR_HI Reg (%08x) = %08x\n",
963 		    DMA_CH_CARBR_HI, XGMAC_DMA_IOREAD(channel, DMA_CH_CARBR_HI));
964         	axgbe_printf(1, "DMA_CH_CARBR_LO Reg (%08x) = %08x\n",
965 		    DMA_CH_CARBR_LO, XGMAC_DMA_IOREAD(channel, DMA_CH_CARBR_LO));
966         	axgbe_printf(1, "DMA_CH_SR Reg (%08x) = %08x\n",
967 		    DMA_CH_SR, XGMAC_DMA_IOREAD(channel, DMA_CH_SR));
968         	axgbe_printf(1, "DMA_CH_DSR Reg (%08x) = %08x\n",
969 		    DMA_CH_DSR,	XGMAC_DMA_IOREAD(channel, DMA_CH_DSR));
970         	axgbe_printf(1, "DMA_CH_DCFL Reg (%08x) = %08x\n",
971 		    DMA_CH_DCFL, XGMAC_DMA_IOREAD(channel, DMA_CH_DCFL));
972         	axgbe_printf(1, "DMA_CH_MFC Reg (%08x) = %08x\n",
973 		    DMA_CH_MFC, XGMAC_DMA_IOREAD(channel, DMA_CH_MFC));
974         	axgbe_printf(1, "DMA_CH_TDTRO Reg (%08x) = %08x\n",
975 		    DMA_CH_TDTRO, XGMAC_DMA_IOREAD(channel, DMA_CH_TDTRO));
976         	axgbe_printf(1, "DMA_CH_RDTRO Reg (%08x) = %08x\n",
977 		    DMA_CH_RDTRO, XGMAC_DMA_IOREAD(channel, DMA_CH_RDTRO));
978         	axgbe_printf(1, "DMA_CH_TDWRO Reg (%08x) = %08x\n",
979 		    DMA_CH_TDWRO, XGMAC_DMA_IOREAD(channel, DMA_CH_TDWRO));
980         	axgbe_printf(1, "DMA_CH_RDWRO Reg (%08x) = %08x\n",
981 		    DMA_CH_RDWRO, XGMAC_DMA_IOREAD(channel, DMA_CH_RDWRO));
982 	}
983 }
984 
985 static void
986 xgbe_dump_mtl_registers(struct xgbe_prv_data *pdata)
987 {
988 	int i;
989 
990         axgbe_printf(1, "\n************* MTL Reg dump *********************\n");
991 
992         axgbe_printf(1, "MTL OMR Reg (%08x) = %08x\n", MTL_OMR,
993            XGMAC_IOREAD(pdata, MTL_OMR));
994         axgbe_printf(1, "MTL FDCR Reg (%08x) = %08x\n", MTL_FDCR,
995            XGMAC_IOREAD(pdata, MTL_FDCR));
996         axgbe_printf(1, "MTL FDSR Reg (%08x) = %08x\n", MTL_FDSR,
997            XGMAC_IOREAD(pdata, MTL_FDSR));
998         axgbe_printf(1, "MTL FDDR Reg (%08x) = %08x\n", MTL_FDDR,
999            XGMAC_IOREAD(pdata, MTL_FDDR));
1000         axgbe_printf(1, "MTL ISR Reg (%08x) = %08x\n", MTL_ISR,
1001            XGMAC_IOREAD(pdata, MTL_ISR));
1002         axgbe_printf(1, "MTL RQDCM0R Reg (%08x) = %08x\n", MTL_RQDCM0R,
1003            XGMAC_IOREAD(pdata, MTL_RQDCM0R));
1004         axgbe_printf(1, "MTL RQDCM1R Reg (%08x) = %08x\n", MTL_RQDCM1R,
1005            XGMAC_IOREAD(pdata, MTL_RQDCM1R));
1006         axgbe_printf(1, "MTL RQDCM2R Reg (%08x) = %08x\n", MTL_RQDCM2R,
1007            XGMAC_IOREAD(pdata, MTL_RQDCM2R));
1008         axgbe_printf(1, "MTL TCPM0R Reg (%08x) = %08x\n", MTL_TCPM0R,
1009            XGMAC_IOREAD(pdata, MTL_TCPM0R));
1010         axgbe_printf(1, "MTL TCPM1R Reg (%08x) = %08x\n", MTL_TCPM1R,
1011            XGMAC_IOREAD(pdata, MTL_TCPM1R));
1012 
1013 	for (i = 0 ; i < 8 ; i++ ) {
1014 
1015         	axgbe_printf(1, "\n************* MTL CH %d dump ****************\n", i);
1016 
1017         	axgbe_printf(1, "MTL_Q_TQOMR Reg (%08x) = %08x\n",
1018 		    MTL_Q_TQOMR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_TQOMR));
1019         	axgbe_printf(1, "MTL_Q_TQUR Reg (%08x) = %08x\n",
1020 		    MTL_Q_TQUR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_TQUR));
1021         	axgbe_printf(1, "MTL_Q_TQDR Reg (%08x) = %08x\n",
1022 		    MTL_Q_TQDR,	XGMAC_MTL_IOREAD(pdata, i, MTL_Q_TQDR));
1023         	axgbe_printf(1, "MTL_Q_TC0ETSCR Reg (%08x) = %08x\n",
1024 		    MTL_Q_TC0ETSCR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_TC0ETSCR));
1025         	axgbe_printf(1, "MTL_Q_TC0ETSSR Reg (%08x) = %08x\n",
1026 		    MTL_Q_TC0ETSSR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_TC0ETSSR));
1027         	axgbe_printf(1, "MTL_Q_TC0QWR Reg (%08x) = %08x\n",
1028 		    MTL_Q_TC0QWR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_TC0QWR));
1029 
1030         	axgbe_printf(1, "MTL_Q_RQOMR Reg (%08x) = %08x\n",
1031 		    MTL_Q_RQOMR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_RQOMR));
1032         	axgbe_printf(1, "MTL_Q_RQMPOCR Reg (%08x) = %08x\n",
1033 		    MTL_Q_RQMPOCR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_RQMPOCR));
1034         	axgbe_printf(1, "MTL_Q_RQDR Reg (%08x) = %08x\n",
1035 		    MTL_Q_RQDR,	XGMAC_MTL_IOREAD(pdata, i, MTL_Q_RQDR));
1036         	axgbe_printf(1, "MTL_Q_RQCR Reg (%08x) = %08x\n",
1037 		    MTL_Q_RQCR,	XGMAC_MTL_IOREAD(pdata, i, MTL_Q_RQCR));
1038         	axgbe_printf(1, "MTL_Q_RQFCR Reg (%08x) = %08x\n",
1039 		    MTL_Q_RQFCR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_RQFCR));
1040         	axgbe_printf(1, "MTL_Q_IER Reg (%08x) = %08x\n",
1041 		    MTL_Q_IER, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_IER));
1042         	axgbe_printf(1, "MTL_Q_ISR Reg (%08x) = %08x\n",
1043 		    MTL_Q_ISR, XGMAC_MTL_IOREAD(pdata, i, MTL_Q_ISR));
1044 	}
1045 }
1046 
1047 static void
1048 xgbe_dump_mac_registers(struct xgbe_prv_data *pdata)
1049 {
1050         axgbe_printf(1, "\n************* MAC Reg dump **********************\n");
1051 
1052         axgbe_printf(1, "MAC TCR Reg (%08x) = %08x\n", MAC_TCR,
1053            XGMAC_IOREAD(pdata, MAC_TCR));
1054         axgbe_printf(1, "MAC RCR Reg (%08x) = %08x\n", MAC_RCR,
1055            XGMAC_IOREAD(pdata, MAC_RCR));
1056         axgbe_printf(1, "MAC PFR Reg (%08x) = %08x\n", MAC_PFR,
1057            XGMAC_IOREAD(pdata, MAC_PFR));
1058         axgbe_printf(1, "MAC WTR Reg (%08x) = %08x\n", MAC_WTR,
1059            XGMAC_IOREAD(pdata, MAC_WTR));
1060         axgbe_printf(1, "MAC HTR0 Reg (%08x) = %08x\n", MAC_HTR0,
1061            XGMAC_IOREAD(pdata, MAC_HTR0));
1062         axgbe_printf(1, "MAC HTR1 Reg (%08x) = %08x\n", MAC_HTR1,
1063            XGMAC_IOREAD(pdata, MAC_HTR1));
1064         axgbe_printf(1, "MAC HTR2 Reg (%08x) = %08x\n", MAC_HTR2,
1065            XGMAC_IOREAD(pdata, MAC_HTR2));
1066         axgbe_printf(1, "MAC HTR3 Reg (%08x) = %08x\n", MAC_HTR3,
1067            XGMAC_IOREAD(pdata, MAC_HTR3));
1068         axgbe_printf(1, "MAC HTR4 Reg (%08x) = %08x\n", MAC_HTR4,
1069            XGMAC_IOREAD(pdata, MAC_HTR4));
1070         axgbe_printf(1, "MAC HTR5 Reg (%08x) = %08x\n", MAC_HTR5,
1071            XGMAC_IOREAD(pdata, MAC_HTR5));
1072         axgbe_printf(1, "MAC HTR6 Reg (%08x) = %08x\n", MAC_HTR6,
1073            XGMAC_IOREAD(pdata, MAC_HTR6));
1074         axgbe_printf(1, "MAC HTR7 Reg (%08x) = %08x\n", MAC_HTR7,
1075            XGMAC_IOREAD(pdata, MAC_HTR7));
1076         axgbe_printf(1, "MAC VLANTR Reg (%08x) = %08x\n", MAC_VLANTR,
1077            XGMAC_IOREAD(pdata, MAC_VLANTR));
1078         axgbe_printf(1, "MAC VLANHTR Reg (%08x) = %08x\n", MAC_VLANHTR,
1079            XGMAC_IOREAD(pdata, MAC_VLANHTR));
1080         axgbe_printf(1, "MAC VLANIR Reg (%08x) = %08x\n", MAC_VLANIR,
1081            XGMAC_IOREAD(pdata, MAC_VLANIR));
1082         axgbe_printf(1, "MAC IVLANIR Reg (%08x) = %08x\n", MAC_IVLANIR,
1083            XGMAC_IOREAD(pdata, MAC_IVLANIR));
1084         axgbe_printf(1, "MAC RETMR Reg (%08x) = %08x\n", MAC_RETMR,
1085            XGMAC_IOREAD(pdata, MAC_RETMR));
1086         axgbe_printf(1, "MAC Q0TFCR Reg (%08x) = %08x\n", MAC_Q0TFCR,
1087            XGMAC_IOREAD(pdata, MAC_Q0TFCR));
1088         axgbe_printf(1, "MAC Q1TFCR Reg (%08x) = %08x\n", MAC_Q1TFCR,
1089            XGMAC_IOREAD(pdata, MAC_Q1TFCR));
1090         axgbe_printf(1, "MAC Q2TFCR Reg (%08x) = %08x\n", MAC_Q2TFCR,
1091            XGMAC_IOREAD(pdata, MAC_Q2TFCR));
1092         axgbe_printf(1, "MAC Q3TFCR Reg (%08x) = %08x\n", MAC_Q3TFCR,
1093            XGMAC_IOREAD(pdata, MAC_Q3TFCR));
1094         axgbe_printf(1, "MAC Q4TFCR Reg (%08x) = %08x\n", MAC_Q4TFCR,
1095            XGMAC_IOREAD(pdata, MAC_Q4TFCR));
1096         axgbe_printf(1, "MAC Q5TFCR Reg (%08x) = %08x\n", MAC_Q5TFCR,
1097            XGMAC_IOREAD(pdata, MAC_Q5TFCR));
1098         axgbe_printf(1, "MAC Q6TFCR Reg (%08x) = %08x\n", MAC_Q6TFCR,
1099            XGMAC_IOREAD(pdata, MAC_Q6TFCR));
1100         axgbe_printf(1, "MAC Q7TFCR Reg (%08x) = %08x\n", MAC_Q7TFCR,
1101            XGMAC_IOREAD(pdata, MAC_Q7TFCR));
1102         axgbe_printf(1, "MAC RFCR Reg (%08x) = %08x\n", MAC_RFCR,
1103            XGMAC_IOREAD(pdata, MAC_RFCR));
1104         axgbe_printf(1, "MAC RQC0R Reg (%08x) = %08x\n", MAC_RQC0R,
1105            XGMAC_IOREAD(pdata, MAC_RQC0R));
1106         axgbe_printf(1, "MAC RQC1R Reg (%08x) = %08x\n", MAC_RQC1R,
1107            XGMAC_IOREAD(pdata, MAC_RQC1R));
1108         axgbe_printf(1, "MAC RQC2R Reg (%08x) = %08x\n", MAC_RQC2R,
1109            XGMAC_IOREAD(pdata, MAC_RQC2R));
1110         axgbe_printf(1, "MAC RQC3R Reg (%08x) = %08x\n", MAC_RQC3R,
1111            XGMAC_IOREAD(pdata, MAC_RQC3R));
1112         axgbe_printf(1, "MAC ISR Reg (%08x) = %08x\n", MAC_ISR,
1113            XGMAC_IOREAD(pdata, MAC_ISR));
1114         axgbe_printf(1, "MAC IER Reg (%08x) = %08x\n", MAC_IER,
1115            XGMAC_IOREAD(pdata, MAC_IER));
1116         axgbe_printf(1, "MAC RTSR Reg (%08x) = %08x\n", MAC_RTSR,
1117            XGMAC_IOREAD(pdata, MAC_RTSR));
1118         axgbe_printf(1, "MAC PMTCSR Reg (%08x) = %08x\n", MAC_PMTCSR,
1119            XGMAC_IOREAD(pdata, MAC_PMTCSR));
1120         axgbe_printf(1, "MAC RWKPFR Reg (%08x) = %08x\n", MAC_RWKPFR,
1121            XGMAC_IOREAD(pdata, MAC_RWKPFR));
1122         axgbe_printf(1, "MAC LPICSR Reg (%08x) = %08x\n", MAC_LPICSR,
1123            XGMAC_IOREAD(pdata, MAC_LPICSR));
1124         axgbe_printf(1, "MAC LPITCR Reg (%08x) = %08x\n", MAC_LPITCR,
1125            XGMAC_IOREAD(pdata, MAC_LPITCR));
1126         axgbe_printf(1, "MAC TIR Reg (%08x) = %08x\n", MAC_TIR,
1127            XGMAC_IOREAD(pdata, MAC_TIR));
1128         axgbe_printf(1, "MAC VR Reg (%08x) = %08x\n", MAC_VR,
1129            XGMAC_IOREAD(pdata, MAC_VR));
1130 	axgbe_printf(1, "MAC DR Reg (%08x) = %08x\n", MAC_DR,
1131            XGMAC_IOREAD(pdata, MAC_DR));
1132         axgbe_printf(1, "MAC HWF0R Reg (%08x) = %08x\n", MAC_HWF0R,
1133            XGMAC_IOREAD(pdata, MAC_HWF0R));
1134         axgbe_printf(1, "MAC HWF1R Reg (%08x) = %08x\n", MAC_HWF1R,
1135            XGMAC_IOREAD(pdata, MAC_HWF1R));
1136         axgbe_printf(1, "MAC HWF2R Reg (%08x) = %08x\n", MAC_HWF2R,
1137            XGMAC_IOREAD(pdata, MAC_HWF2R));
1138         axgbe_printf(1, "MAC MDIOSCAR Reg (%08x) = %08x\n", MAC_MDIOSCAR,
1139            XGMAC_IOREAD(pdata, MAC_MDIOSCAR));
1140         axgbe_printf(1, "MAC MDIOSCCDR Reg (%08x) = %08x\n", MAC_MDIOSCCDR,
1141            XGMAC_IOREAD(pdata, MAC_MDIOSCCDR));
1142         axgbe_printf(1, "MAC MDIOISR Reg (%08x) = %08x\n", MAC_MDIOISR,
1143            XGMAC_IOREAD(pdata, MAC_MDIOISR));
1144         axgbe_printf(1, "MAC MDIOIER Reg (%08x) = %08x\n", MAC_MDIOIER,
1145            XGMAC_IOREAD(pdata, MAC_MDIOIER));
1146         axgbe_printf(1, "MAC MDIOCL22R Reg (%08x) = %08x\n", MAC_MDIOCL22R,
1147            XGMAC_IOREAD(pdata, MAC_MDIOCL22R));
1148         axgbe_printf(1, "MAC GPIOCR Reg (%08x) = %08x\n", MAC_GPIOCR,
1149            XGMAC_IOREAD(pdata, MAC_GPIOCR));
1150         axgbe_printf(1, "MAC GPIOSR Reg (%08x) = %08x\n", MAC_GPIOSR,
1151            XGMAC_IOREAD(pdata, MAC_GPIOSR));
1152         axgbe_printf(1, "MAC MACA0HR Reg (%08x) = %08x\n", MAC_MACA0HR,
1153            XGMAC_IOREAD(pdata, MAC_MACA0HR));
1154         axgbe_printf(1, "MAC MACA0LR Reg (%08x) = %08x\n", MAC_TCR,
1155            XGMAC_IOREAD(pdata, MAC_MACA0LR));
1156         axgbe_printf(1, "MAC MACA1HR Reg (%08x) = %08x\n", MAC_MACA1HR,
1157            XGMAC_IOREAD(pdata, MAC_MACA1HR));
1158         axgbe_printf(1, "MAC MACA1LR Reg (%08x) = %08x\n", MAC_MACA1LR,
1159            XGMAC_IOREAD(pdata, MAC_MACA1LR));
1160         axgbe_printf(1, "MAC RSSCR Reg (%08x) = %08x\n", MAC_RSSCR,
1161            XGMAC_IOREAD(pdata, MAC_RSSCR));
1162         axgbe_printf(1, "MAC RSSDR Reg (%08x) = %08x\n", MAC_RSSDR,
1163            XGMAC_IOREAD(pdata, MAC_RSSDR));
1164         axgbe_printf(1, "MAC RSSAR Reg (%08x) = %08x\n", MAC_RSSAR,
1165            XGMAC_IOREAD(pdata, MAC_RSSAR));
1166         axgbe_printf(1, "MAC TSCR Reg (%08x) = %08x\n", MAC_TSCR,
1167            XGMAC_IOREAD(pdata, MAC_TSCR));
1168         axgbe_printf(1, "MAC SSIR Reg (%08x) = %08x\n", MAC_SSIR,
1169            XGMAC_IOREAD(pdata, MAC_SSIR));
1170         axgbe_printf(1, "MAC STSR Reg (%08x) = %08x\n", MAC_STSR,
1171            XGMAC_IOREAD(pdata, MAC_STSR));
1172         axgbe_printf(1, "MAC STNR Reg (%08x) = %08x\n", MAC_STNR,
1173            XGMAC_IOREAD(pdata, MAC_STNR));
1174         axgbe_printf(1, "MAC STSUR Reg (%08x) = %08x\n", MAC_STSUR,
1175            XGMAC_IOREAD(pdata, MAC_STSUR));
1176         axgbe_printf(1, "MAC STNUR Reg (%08x) = %08x\n", MAC_STNUR,
1177            XGMAC_IOREAD(pdata, MAC_STNUR));
1178         axgbe_printf(1, "MAC TSAR Reg (%08x) = %08x\n", MAC_TSAR,
1179            XGMAC_IOREAD(pdata, MAC_TSAR));
1180         axgbe_printf(1, "MAC TSSR Reg (%08x) = %08x\n", MAC_TSSR,
1181            XGMAC_IOREAD(pdata, MAC_TSSR));
1182         axgbe_printf(1, "MAC TXSNR Reg (%08x) = %08x\n", MAC_TXSNR,
1183            XGMAC_IOREAD(pdata, MAC_TXSNR));
1184 	 axgbe_printf(1, "MAC TXSSR Reg (%08x) = %08x\n", MAC_TXSSR,
1185            XGMAC_IOREAD(pdata, MAC_TXSSR));
1186 }
1187 
1188 static void
1189 xgbe_dump_rmon_counters(struct xgbe_prv_data *pdata)
1190 {
1191         struct xgbe_mmc_stats *stats = &pdata->mmc_stats;
1192 
1193         axgbe_printf(1, "\n************* RMON counters dump ***************\n");
1194 
1195         pdata->hw_if.read_mmc_stats(pdata);
1196 
1197         axgbe_printf(1, "rmon txoctetcount_gb (%08x) = %08lx\n",
1198 	    MMC_TXOCTETCOUNT_GB_LO, stats->txoctetcount_gb);
1199         axgbe_printf(1, "rmon txframecount_gb (%08x) = %08lx\n",
1200 	    MMC_TXFRAMECOUNT_GB_LO, stats->txframecount_gb);
1201         axgbe_printf(1, "rmon txbroadcastframes_g (%08x) = %08lx\n",
1202 	    MMC_TXBROADCASTFRAMES_G_LO, stats->txbroadcastframes_g);
1203         axgbe_printf(1, "rmon txmulticastframes_g (%08x) = %08lx\n",
1204 	    MMC_TXMULTICASTFRAMES_G_LO, stats->txmulticastframes_g);
1205         axgbe_printf(1, "rmon tx64octets_gb (%08x) = %08lx\n",
1206 	    MMC_TX64OCTETS_GB_LO, stats->tx64octets_gb);
1207         axgbe_printf(1, "rmon tx65to127octets_gb (%08x) = %08lx\n",
1208 	    MMC_TX65TO127OCTETS_GB_LO, stats->tx65to127octets_gb);
1209         axgbe_printf(1, "rmon tx128to255octets_gb (%08x) = %08lx\n",
1210 	    MMC_TX128TO255OCTETS_GB_LO, stats->tx128to255octets_gb);
1211         axgbe_printf(1, "rmon tx256to511octets_gb (%08x) = %08lx\n",
1212 	    MMC_TX256TO511OCTETS_GB_LO, stats->tx256to511octets_gb);
1213         axgbe_printf(1, "rmon tx512to1023octets_gb (%08x) = %08lx\n",
1214 	    MMC_TX512TO1023OCTETS_GB_LO, stats->tx512to1023octets_gb);
1215 	axgbe_printf(1, "rmon tx1024tomaxoctets_gb (%08x) = %08lx\n",
1216 	    MMC_TX1024TOMAXOCTETS_GB_LO, stats->tx1024tomaxoctets_gb);
1217         axgbe_printf(1, "rmon txunicastframes_gb (%08x) = %08lx\n",
1218 	    MMC_TXUNICASTFRAMES_GB_LO, stats->txunicastframes_gb);
1219         axgbe_printf(1, "rmon txmulticastframes_gb (%08x) = %08lx\n",
1220 	    MMC_TXMULTICASTFRAMES_GB_LO, stats->txmulticastframes_gb);
1221         axgbe_printf(1, "rmon txbroadcastframes_gb (%08x) = %08lx\n",
1222 	    MMC_TXBROADCASTFRAMES_GB_LO, stats->txbroadcastframes_gb);
1223         axgbe_printf(1, "rmon txunderflowerror (%08x) = %08lx\n",
1224 	    MMC_TXUNDERFLOWERROR_LO, stats->txunderflowerror);
1225         axgbe_printf(1, "rmon txoctetcount_g (%08x) = %08lx\n",
1226 	    MMC_TXOCTETCOUNT_G_LO, stats->txoctetcount_g);
1227         axgbe_printf(1, "rmon txframecount_g (%08x) = %08lx\n",
1228 	    MMC_TXFRAMECOUNT_G_LO, stats->txframecount_g);
1229         axgbe_printf(1, "rmon txpauseframes (%08x) = %08lx\n",
1230 	    MMC_TXPAUSEFRAMES_LO, stats->txpauseframes);
1231         axgbe_printf(1, "rmon txvlanframes_g (%08x) = %08lx\n",
1232 	    MMC_TXVLANFRAMES_G_LO, stats->txvlanframes_g);
1233         axgbe_printf(1, "rmon rxframecount_gb (%08x) = %08lx\n",
1234 	    MMC_RXFRAMECOUNT_GB_LO, stats->rxframecount_gb);
1235         axgbe_printf(1, "rmon rxoctetcount_gb (%08x) = %08lx\n",
1236 	    MMC_RXOCTETCOUNT_GB_LO, stats->rxoctetcount_gb);
1237         axgbe_printf(1, "rmon rxoctetcount_g (%08x) = %08lx\n",
1238 	    MMC_RXOCTETCOUNT_G_LO, stats->rxoctetcount_g);
1239         axgbe_printf(1, "rmon rxbroadcastframes_g (%08x) = %08lx\n",
1240 	    MMC_RXBROADCASTFRAMES_G_LO, stats->rxbroadcastframes_g);
1241         axgbe_printf(1, "rmon rxmulticastframes_g (%08x) = %08lx\n",
1242 	    MMC_RXMULTICASTFRAMES_G_LO, stats->rxmulticastframes_g);
1243         axgbe_printf(1, "rmon rxcrcerror (%08x) = %08lx\n",
1244 	    MMC_RXCRCERROR_LO, stats->rxcrcerror);
1245 	axgbe_printf(1, "rmon rxrunterror (%08x) = %08lx\n",
1246 	    MMC_RXRUNTERROR, stats->rxrunterror);
1247         axgbe_printf(1, "rmon rxjabbererror (%08x) = %08lx\n",
1248 	    MMC_RXJABBERERROR, stats->rxjabbererror);
1249         axgbe_printf(1, "rmon rxundersize_g (%08x) = %08lx\n",
1250 	    MMC_RXUNDERSIZE_G, stats->rxundersize_g);
1251         axgbe_printf(1, "rmon rxoversize_g (%08x) = %08lx\n",
1252 	    MMC_RXOVERSIZE_G, stats->rxoversize_g);
1253         axgbe_printf(1, "rmon rx64octets_gb (%08x) = %08lx\n",
1254 	    MMC_RX64OCTETS_GB_LO, stats->rx64octets_gb);
1255         axgbe_printf(1, "rmon rx65to127octets_gb (%08x) = %08lx\n",
1256 	    MMC_RX65TO127OCTETS_GB_LO, stats->rx65to127octets_gb);
1257         axgbe_printf(1, "rmon rx128to255octets_gb (%08x) = %08lx\n",
1258 	    MMC_RX128TO255OCTETS_GB_LO, stats->rx128to255octets_gb);
1259         axgbe_printf(1, "rmon rx256to511octets_gb (%08x) = %08lx\n",
1260 	    MMC_RX256TO511OCTETS_GB_LO, stats->rx256to511octets_gb);
1261         axgbe_printf(1, "rmon rx512to1023octets_gb (%08x) = %08lx\n",
1262 	    MMC_RX512TO1023OCTETS_GB_LO, stats->rx512to1023octets_gb);
1263         axgbe_printf(1, "rmon rx1024tomaxoctets_gb (%08x) = %08lx\n",
1264 	    MMC_RX1024TOMAXOCTETS_GB_LO, stats->rx1024tomaxoctets_gb);
1265         axgbe_printf(1, "rmon rxunicastframes_g (%08x) = %08lx\n",
1266 	    MMC_RXUNICASTFRAMES_G_LO, stats->rxunicastframes_g);
1267         axgbe_printf(1, "rmon rxlengtherror (%08x) = %08lx\n",
1268 	    MMC_RXLENGTHERROR_LO, stats->rxlengtherror);
1269         axgbe_printf(1, "rmon rxoutofrangetype (%08x) = %08lx\n",
1270 	    MMC_RXOUTOFRANGETYPE_LO, stats->rxoutofrangetype);
1271         axgbe_printf(1, "rmon rxpauseframes (%08x) = %08lx\n",
1272 	    MMC_RXPAUSEFRAMES_LO, stats->rxpauseframes);
1273         axgbe_printf(1, "rmon rxfifooverflow (%08x) = %08lx\n",
1274 	    MMC_RXFIFOOVERFLOW_LO, stats->rxfifooverflow);
1275 	axgbe_printf(1, "rmon rxvlanframes_gb (%08x) = %08lx\n",
1276 	    MMC_RXVLANFRAMES_GB_LO, stats->rxvlanframes_gb);
1277         axgbe_printf(1, "rmon rxwatchdogerror (%08x) = %08lx\n",
1278 	    MMC_RXWATCHDOGERROR, stats->rxwatchdogerror);
1279 }
1280 
1281 void
1282 xgbe_dump_i2c_registers(struct xgbe_prv_data *pdata)
1283 {
1284           axgbe_printf(1, "*************** I2C Registers **************\n");
1285           axgbe_printf(1, "  IC_CON             : %010x\n",
1286 	      XI2C_IOREAD(pdata, 0x00));
1287           axgbe_printf(1, "  IC_TAR             : %010x\n",
1288 	      XI2C_IOREAD(pdata, 0x04));
1289           axgbe_printf(1, "  IC_HS_MADDR        : %010x\n",
1290 	      XI2C_IOREAD(pdata, 0x0c));
1291           axgbe_printf(1, "  IC_INTR_STAT       : %010x\n",
1292 	      XI2C_IOREAD(pdata, 0x2c));
1293           axgbe_printf(1, "  IC_INTR_MASK       : %010x\n",
1294 	      XI2C_IOREAD(pdata, 0x30));
1295           axgbe_printf(1, "  IC_RAW_INTR_STAT   : %010x\n",
1296 	      XI2C_IOREAD(pdata, 0x34));
1297           axgbe_printf(1, "  IC_RX_TL           : %010x\n",
1298 	      XI2C_IOREAD(pdata, 0x38));
1299           axgbe_printf(1, "  IC_TX_TL           : %010x\n",
1300 	      XI2C_IOREAD(pdata, 0x3c));
1301           axgbe_printf(1, "  IC_ENABLE          : %010x\n",
1302 	      XI2C_IOREAD(pdata, 0x6c));
1303           axgbe_printf(1, "  IC_STATUS          : %010x\n",
1304 	      XI2C_IOREAD(pdata, 0x70));
1305           axgbe_printf(1, "  IC_TXFLR           : %010x\n",
1306 	      XI2C_IOREAD(pdata, 0x74));
1307           axgbe_printf(1, "  IC_RXFLR           : %010x\n",
1308 	      XI2C_IOREAD(pdata, 0x78));
1309           axgbe_printf(1, "  IC_ENABLE_STATUS   : %010x\n",
1310 	      XI2C_IOREAD(pdata, 0x9c));
1311           axgbe_printf(1, "  IC_COMP_PARAM1     : %010x\n",
1312 	      XI2C_IOREAD(pdata, 0xf4));
1313 }
1314 
1315 static void
1316 xgbe_dump_active_vlans(struct xgbe_prv_data *pdata)
1317 {
1318 	int i;
1319 
1320 	for(i=0 ; i<BITS_TO_LONGS(VLAN_NVID); i++) {
1321 		if (i && (i%8 == 0))
1322 			axgbe_printf(1, "\n");
1323                 axgbe_printf(1, "vlans[%d]: 0x%08lx ", i, pdata->active_vlans[i]);
1324 	}
1325 	axgbe_printf(1, "\n");
1326 }
1327 
1328 static void
1329 xgbe_default_config(struct xgbe_prv_data *pdata)
1330 {
1331         pdata->blen = DMA_SBMR_BLEN_64;
1332         pdata->pbl = DMA_PBL_128;
1333         pdata->aal = 1;
1334         pdata->rd_osr_limit = 8;
1335         pdata->wr_osr_limit = 8;
1336         pdata->tx_sf_mode = MTL_TSF_ENABLE;
1337         pdata->tx_threshold = MTL_TX_THRESHOLD_64;
1338         pdata->tx_osp_mode = DMA_OSP_ENABLE;
1339         pdata->rx_sf_mode = MTL_RSF_DISABLE;
1340         pdata->rx_threshold = MTL_RX_THRESHOLD_64;
1341         pdata->pause_autoneg = 1;
1342         pdata->tx_pause = 1;
1343         pdata->rx_pause = 1;
1344         pdata->phy_speed = SPEED_UNKNOWN;
1345         pdata->power_down = 0;
1346         pdata->enable_rss = 1;
1347 }
1348 
1349 static int
1350 axgbe_if_attach_post(if_ctx_t ctx)
1351 {
1352 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
1353 	struct xgbe_prv_data	*pdata = &sc->pdata;
1354 	if_t			 ifp = pdata->netdev;
1355         struct xgbe_phy_if	*phy_if = &pdata->phy_if;
1356 	struct xgbe_hw_if 	*hw_if = &pdata->hw_if;
1357 	if_softc_ctx_t		scctx = sc->scctx;
1358 	int i, ret;
1359 
1360 	/* set split header support based on tunable */
1361 	pdata->sph_enable = axgbe_sph_enable;
1362 
1363 	/* Initialize ECC timestamps */
1364         pdata->tx_sec_period = ticks;
1365         pdata->tx_ded_period = ticks;
1366         pdata->rx_sec_period = ticks;
1367         pdata->rx_ded_period = ticks;
1368         pdata->desc_sec_period = ticks;
1369         pdata->desc_ded_period = ticks;
1370 
1371 	/* Reset the hardware */
1372 	ret = hw_if->exit(&sc->pdata);
1373 	if (ret)
1374 		axgbe_error("%s: exit error %d\n", __func__, ret);
1375 
1376 	/* Configure the defaults */
1377 	xgbe_default_config(pdata);
1378 
1379 	/* Set default max values if not provided */
1380         if (!pdata->tx_max_fifo_size)
1381                 pdata->tx_max_fifo_size = pdata->hw_feat.tx_fifo_size;
1382         if (!pdata->rx_max_fifo_size)
1383                 pdata->rx_max_fifo_size = pdata->hw_feat.rx_fifo_size;
1384 
1385 	DBGPR("%s: tx fifo 0x%x rx fifo 0x%x\n", __func__,
1386 	    pdata->tx_max_fifo_size, pdata->rx_max_fifo_size);
1387 
1388         /* Set and validate the number of descriptors for a ring */
1389         MPASS(powerof2(XGBE_TX_DESC_CNT));
1390         pdata->tx_desc_count = XGBE_TX_DESC_CNT;
1391         MPASS(powerof2(XGBE_RX_DESC_CNT));
1392         pdata->rx_desc_count = XGBE_RX_DESC_CNT;
1393 
1394         /* Adjust the number of queues based on interrupts assigned */
1395         if (pdata->channel_irq_count) {
1396                 pdata->tx_ring_count = min_t(unsigned int, pdata->tx_ring_count,
1397 		    pdata->channel_irq_count);
1398                 pdata->rx_ring_count = min_t(unsigned int, pdata->rx_ring_count,
1399 		    pdata->channel_irq_count);
1400 
1401 		DBGPR("adjusted TX %u/%u RX %u/%u\n",
1402 		    pdata->tx_ring_count, pdata->tx_q_count,
1403 		    pdata->rx_ring_count, pdata->rx_q_count);
1404         }
1405 
1406 	/* Set channel count based on interrupts assigned */
1407 	pdata->channel_count = max_t(unsigned int, scctx->isc_ntxqsets,
1408 	    scctx->isc_nrxqsets);
1409 	DBGPR("Channel count set to: %u\n", pdata->channel_count);
1410 
1411 	/* Get RSS key */
1412 #ifdef	RSS
1413 	rss_getkey((uint8_t *)pdata->rss_key);
1414 #else
1415 	arc4rand(&pdata->rss_key, ARRAY_SIZE(pdata->rss_key), 0);
1416 #endif
1417 	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, IP2TE, 1);
1418 	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, TCP4TE, 1);
1419 	XGMAC_SET_BITS(pdata->rss_options, MAC_RSSCR, UDP4TE, 1);
1420 
1421 	/* Initialize the PHY device */
1422 	pdata->sysctl_an_cdr_workaround = pdata->vdata->an_cdr_workaround;
1423 	phy_if->phy_init(pdata);
1424 
1425 	/* Set the coalescing */
1426         xgbe_init_rx_coalesce(&sc->pdata);
1427         xgbe_init_tx_coalesce(&sc->pdata);
1428 
1429 	ifmedia_add(sc->media, IFM_ETHER | IFM_10G_KR, 0, NULL);
1430 	ifmedia_add(sc->media, IFM_ETHER | IFM_10G_T, 0, NULL);
1431 	ifmedia_add(sc->media, IFM_ETHER | IFM_10G_SFI, 0, NULL);
1432 	ifmedia_add(sc->media, IFM_ETHER | IFM_1000_KX, 0, NULL);
1433 	ifmedia_add(sc->media, IFM_ETHER | IFM_1000_CX, 0, NULL);
1434 	ifmedia_add(sc->media, IFM_ETHER | IFM_1000_LX, 0, NULL);
1435 	ifmedia_add(sc->media, IFM_ETHER | IFM_1000_SX, 0, NULL);
1436 	ifmedia_add(sc->media, IFM_ETHER | IFM_1000_T, 0, NULL);
1437 	ifmedia_add(sc->media, IFM_ETHER | IFM_1000_SGMII, 0, NULL);
1438 	ifmedia_add(sc->media, IFM_ETHER | IFM_100_TX, 0, NULL);
1439 	ifmedia_add(sc->media, IFM_ETHER | IFM_100_SGMII, 0, NULL);
1440 	ifmedia_add(sc->media, IFM_ETHER | IFM_AUTO, 0, NULL);
1441 	ifmedia_set(sc->media, IFM_ETHER | IFM_AUTO);
1442 
1443 	/* Initialize the phy */
1444 	pdata->phy_link = -1;
1445 	pdata->phy_speed = SPEED_UNKNOWN;
1446 	ret = phy_if->phy_reset(pdata);
1447 	if (ret)
1448 		return (ret);
1449 
1450 	/* Calculate the Rx buffer size before allocating rings */
1451 	ret = xgbe_calc_rx_buf_size(pdata->netdev, if_getmtu(pdata->netdev));
1452 	pdata->rx_buf_size = ret;
1453 	DBGPR("%s: rx_buf_size %d\n", __func__, ret);
1454 
1455 	/* Setup RSS lookup table */
1456 	for (i = 0; i < XGBE_RSS_MAX_TABLE_SIZE; i++)
1457 		XGMAC_SET_BITS(pdata->rss_table[i], MAC_RSSDR, DMCH,
1458 				i % pdata->rx_ring_count);
1459 
1460 	/*
1461 	 * Mark the device down until it is initialized, which happens
1462 	 * when the device is accessed first (for configuring the iface,
1463 	 * eg: setting IP)
1464 	 */
1465 	set_bit(XGBE_DOWN, &pdata->dev_state);
1466 
1467 	DBGPR("mtu %d\n", if_getmtu(ifp));
1468 	scctx->isc_max_frame_size = if_getmtu(ifp) + 18;
1469 	scctx->isc_min_frame_size = XGMAC_MIN_PACKET;
1470 
1471 	axgbe_sysctl_init(pdata);
1472 
1473 	axgbe_pci_init(pdata);
1474 
1475 	return (0);
1476 } /* axgbe_if_attach_post */
1477 
1478 static void
1479 xgbe_free_intr(struct xgbe_prv_data *pdata, struct resource *res, void *tag,
1480 		int rid)
1481 {
1482 	if (tag)
1483 		bus_teardown_intr(pdata->dev, res, tag);
1484 
1485 	if (res)
1486 		bus_release_resource(pdata->dev, SYS_RES_IRQ, rid, res);
1487 }
1488 
1489 static void
1490 axgbe_interrupts_free(if_ctx_t ctx)
1491 {
1492 	struct axgbe_if_softc   *sc = iflib_get_softc(ctx);
1493         struct xgbe_prv_data	*pdata = &sc->pdata;
1494         if_softc_ctx_t          scctx = sc->scctx;
1495         struct xgbe_channel     *channel;
1496         struct if_irq   irq;
1497         int i;
1498 
1499 	axgbe_printf(2, "%s: mode %d\n", __func__, scctx->isc_intr);
1500 
1501 	/* Free dev_irq */
1502 	iflib_irq_free(ctx, &pdata->dev_irq);
1503 
1504 	/* Free ecc_irq */
1505 	xgbe_free_intr(pdata, pdata->ecc_irq_res, pdata->ecc_irq_tag,
1506 	    pdata->ecc_rid);
1507 
1508 	/* Free i2c_irq */
1509 	xgbe_free_intr(pdata, pdata->i2c_irq_res, pdata->i2c_irq_tag,
1510 	    pdata->i2c_rid);
1511 
1512 	/* Free an_irq */
1513 	xgbe_free_intr(pdata, pdata->an_irq_res, pdata->an_irq_tag,
1514 	    pdata->an_rid);
1515 
1516 	for (i = 0; i < scctx->isc_nrxqsets; i++) {
1517 
1518 		channel = pdata->channel[i];
1519 		axgbe_printf(2, "%s: rid %d\n", __func__, channel->dma_irq_rid);
1520 		irq.ii_res = channel->dma_irq_res;
1521 		irq.ii_tag = channel->dma_irq_tag;
1522 		iflib_irq_free(ctx, &irq);
1523 	}
1524 }
1525 
1526 static int
1527 axgbe_if_detach(if_ctx_t ctx)
1528 {
1529 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
1530 	struct xgbe_prv_data	*pdata = &sc->pdata;
1531         struct xgbe_phy_if	*phy_if = &pdata->phy_if;
1532         struct resource *mac_res[2];
1533 
1534 	mac_res[0] = pdata->xgmac_res;
1535 	mac_res[1] = pdata->xpcs_res;
1536 
1537 	phy_if->phy_stop(pdata);
1538 	phy_if->phy_exit(pdata);
1539 
1540 	/* Free Interrupts */
1541 	axgbe_interrupts_free(ctx);
1542 
1543 	/* Free workqueues */
1544 	taskqueue_free(pdata->dev_workqueue);
1545 
1546 	/* Release bus resources */
1547 	bus_release_resources(iflib_get_dev(ctx), axgbe_pci_mac_spec, mac_res);
1548 
1549 	/* Free VLAN bitmap */
1550 	free(pdata->active_vlans, M_AXGBE);
1551 
1552 	axgbe_sysctl_exit(pdata);
1553 
1554 	return (0);
1555 } /* axgbe_if_detach */
1556 
1557 static void
1558 axgbe_pci_init(struct xgbe_prv_data *pdata)
1559 {
1560 	struct xgbe_phy_if	*phy_if = &pdata->phy_if;
1561 	struct xgbe_hw_if       *hw_if = &pdata->hw_if;
1562 	int ret = 0;
1563 
1564 	if (!__predict_false((test_bit(XGBE_DOWN, &pdata->dev_state)))) {
1565 		axgbe_printf(1, "%s: Starting when XGBE_UP\n", __func__);
1566 		return;
1567 	}
1568 
1569 	hw_if->init(pdata);
1570 
1571         ret = phy_if->phy_start(pdata);
1572         if (ret) {
1573 		axgbe_error("%s:  phy start %d\n", __func__, ret);
1574 		ret = hw_if->exit(pdata);
1575 		if (ret)
1576 			axgbe_error("%s: exit error %d\n", __func__, ret);
1577 		return;
1578 	}
1579 
1580 	hw_if->enable_tx(pdata);
1581 	hw_if->enable_rx(pdata);
1582 
1583 	xgbe_start_timers(pdata);
1584 
1585 	clear_bit(XGBE_DOWN, &pdata->dev_state);
1586 
1587 	xgbe_dump_phy_registers(pdata);
1588 	xgbe_dump_prop_registers(pdata);
1589 	xgbe_dump_dma_registers(pdata, -1);
1590 	xgbe_dump_mtl_registers(pdata);
1591 	xgbe_dump_mac_registers(pdata);
1592 	xgbe_dump_rmon_counters(pdata);
1593 }
1594 
1595 static void
1596 axgbe_if_init(if_ctx_t ctx)
1597 {
1598 	struct axgbe_if_softc   *sc = iflib_get_softc(ctx);
1599 	struct xgbe_prv_data    *pdata = &sc->pdata;
1600 
1601 	axgbe_pci_init(pdata);
1602 }
1603 
1604 static void
1605 axgbe_pci_stop(if_ctx_t ctx)
1606 {
1607 	struct axgbe_if_softc   *sc = iflib_get_softc(ctx);
1608         struct xgbe_prv_data    *pdata = &sc->pdata;
1609 	struct xgbe_hw_if       *hw_if = &pdata->hw_if;
1610 	int ret;
1611 
1612 	if (__predict_false(test_bit(XGBE_DOWN, &pdata->dev_state))) {
1613 		axgbe_printf(1, "%s: Stopping when XGBE_DOWN\n", __func__);
1614 		return;
1615 	}
1616 
1617 	xgbe_stop_timers(pdata);
1618 	taskqueue_drain_all(pdata->dev_workqueue);
1619 
1620 	hw_if->disable_tx(pdata);
1621 	hw_if->disable_rx(pdata);
1622 
1623 	ret = hw_if->exit(pdata);
1624 	if (ret)
1625 		axgbe_error("%s: exit error %d\n", __func__, ret);
1626 
1627 	set_bit(XGBE_DOWN, &pdata->dev_state);
1628 }
1629 
1630 static void
1631 axgbe_if_stop(if_ctx_t ctx)
1632 {
1633 	axgbe_pci_stop(ctx);
1634 }
1635 
1636 static void
1637 axgbe_if_disable_intr(if_ctx_t ctx)
1638 {
1639 	/* TODO - implement */
1640 }
1641 
1642 static void
1643 axgbe_if_enable_intr(if_ctx_t ctx)
1644 {
1645 	/* TODO - implement */
1646 }
1647 
1648 static int
1649 axgbe_if_tx_queues_alloc(if_ctx_t ctx, caddr_t *va, uint64_t *pa, int ntxqs,
1650     int ntxqsets)
1651 {
1652 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
1653 	struct xgbe_prv_data 	*pdata = &sc->pdata;
1654 	if_softc_ctx_t		scctx = sc->scctx;
1655 	struct xgbe_channel	*channel;
1656 	struct xgbe_ring	*tx_ring;
1657 	int			i, j, k;
1658 
1659 	MPASS(scctx->isc_ntxqsets > 0);
1660 	MPASS(scctx->isc_ntxqsets == ntxqsets);
1661 	MPASS(ntxqs == 1);
1662 
1663 	axgbe_printf(1, "%s: txqsets %d/%d txqs %d\n", __func__,
1664 	    scctx->isc_ntxqsets, ntxqsets, ntxqs);
1665 
1666 	for (i = 0 ; i < ntxqsets; i++) {
1667 
1668 		channel = pdata->channel[i];
1669 
1670 		tx_ring = (struct xgbe_ring*)malloc(ntxqs *
1671 		    sizeof(struct xgbe_ring), M_AXGBE, M_NOWAIT | M_ZERO);
1672 
1673 		if (tx_ring == NULL) {
1674 			axgbe_error("Unable to allocate TX ring memory\n");
1675 			goto tx_ring_fail;
1676 		}
1677 
1678 		channel->tx_ring = tx_ring;
1679 
1680 		for (j = 0; j < ntxqs; j++, tx_ring++) {
1681 			tx_ring->rdata =
1682 			    (struct xgbe_ring_data*)malloc(scctx->isc_ntxd[j] *
1683 			    sizeof(struct xgbe_ring_data), M_AXGBE, M_NOWAIT);
1684 
1685 			/* Get the virtual & physical address of hw queues */
1686 			tx_ring->rdesc = (struct xgbe_ring_desc *)va[i*ntxqs + j];
1687 			tx_ring->rdesc_paddr = pa[i*ntxqs + j];
1688 			tx_ring->rdesc_count = scctx->isc_ntxd[j];
1689 			spin_lock_init(&tx_ring->lock);
1690 		}
1691 	}
1692 
1693 	axgbe_printf(1, "allocated for %d tx queues\n", scctx->isc_ntxqsets);
1694 
1695 	return (0);
1696 
1697 tx_ring_fail:
1698 
1699 	for (j = 0; j < i ; j++) {
1700 
1701 		channel = pdata->channel[j];
1702 
1703 		tx_ring = channel->tx_ring;
1704 		for (k = 0; k < ntxqs ; k++, tx_ring++) {
1705 			if (tx_ring && tx_ring->rdata)
1706 				free(tx_ring->rdata, M_AXGBE);
1707 		}
1708 		free(channel->tx_ring, M_AXGBE);
1709 
1710 		channel->tx_ring = NULL;
1711 	}
1712 
1713 	return (ENOMEM);
1714 
1715 } /* axgbe_if_tx_queues_alloc */
1716 
1717 static int
1718 axgbe_if_rx_queues_alloc(if_ctx_t ctx, caddr_t *va, uint64_t *pa, int nrxqs,
1719     int nrxqsets)
1720 {
1721 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
1722 	struct xgbe_prv_data 	*pdata = &sc->pdata;
1723 	if_softc_ctx_t		scctx = sc->scctx;
1724 	struct xgbe_channel	*channel;
1725 	struct xgbe_ring	*rx_ring;
1726 	int			i, j, k;
1727 
1728 	MPASS(scctx->isc_nrxqsets > 0);
1729 	MPASS(scctx->isc_nrxqsets == nrxqsets);
1730 	if (!pdata->sph_enable) {
1731 		MPASS(nrxqs == 1);
1732 	} else {
1733 		MPASS(nrxqs == 2);
1734 	}
1735 
1736 	axgbe_printf(1, "%s: rxqsets %d/%d rxqs %d\n", __func__,
1737 	    scctx->isc_nrxqsets, nrxqsets, nrxqs);
1738 
1739 	for (i = 0 ; i < nrxqsets; i++) {
1740 
1741 		channel = pdata->channel[i];
1742 
1743 		rx_ring = (struct xgbe_ring*)malloc(nrxqs *
1744 		    sizeof(struct xgbe_ring), M_AXGBE, M_NOWAIT | M_ZERO);
1745 
1746 		if (rx_ring == NULL) {
1747 			axgbe_error("Unable to allocate RX ring memory\n");
1748 			goto rx_ring_fail;
1749 		}
1750 
1751 		channel->rx_ring = rx_ring;
1752 
1753 		for (j = 0; j < nrxqs; j++, rx_ring++) {
1754 			rx_ring->rdata =
1755 			    (struct xgbe_ring_data*)malloc(scctx->isc_nrxd[j] *
1756 			    sizeof(struct xgbe_ring_data), M_AXGBE, M_NOWAIT);
1757 
1758 			/* Get the virtual and physical address of the hw queues */
1759 			rx_ring->rdesc = (struct xgbe_ring_desc *)va[i*nrxqs + j];
1760 			rx_ring->rdesc_paddr = pa[i*nrxqs + j];
1761 			rx_ring->rdesc_count = scctx->isc_nrxd[j];
1762 			spin_lock_init(&rx_ring->lock);
1763 		}
1764 	}
1765 
1766 	axgbe_printf(2, "allocated for %d rx queues\n", scctx->isc_nrxqsets);
1767 
1768 	return (0);
1769 
1770 rx_ring_fail:
1771 
1772 	for (j = 0 ; j < i ; j++) {
1773 
1774 		channel = pdata->channel[j];
1775 
1776 		rx_ring = channel->rx_ring;
1777 		for (k = 0; k < nrxqs ; k++, rx_ring++) {
1778 			if (rx_ring && rx_ring->rdata)
1779 				free(rx_ring->rdata, M_AXGBE);
1780 		}
1781 		free(channel->rx_ring, M_AXGBE);
1782 
1783 		channel->rx_ring = NULL;
1784 	}
1785 
1786 	return (ENOMEM);
1787 
1788 } /* axgbe_if_rx_queues_alloc */
1789 
1790 static void
1791 axgbe_if_queues_free(if_ctx_t ctx)
1792 {
1793 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
1794 	struct xgbe_prv_data 	*pdata = &sc->pdata;
1795 	if_softc_ctx_t		scctx = sc->scctx;
1796 	if_shared_ctx_t		sctx = sc->sctx;
1797 	struct xgbe_channel	*channel;
1798 	struct xgbe_ring        *tx_ring;
1799 	struct xgbe_ring        *rx_ring;
1800 	int i, j;
1801 
1802 	for (i = 0 ; i < scctx->isc_ntxqsets; i++) {
1803 
1804 		channel = pdata->channel[i];
1805 
1806 		tx_ring = channel->tx_ring;
1807 		for (j = 0; j < sctx->isc_ntxqs ; j++, tx_ring++) {
1808 			if (tx_ring && tx_ring->rdata)
1809 				free(tx_ring->rdata, M_AXGBE);
1810 		}
1811 		free(channel->tx_ring, M_AXGBE);
1812 		channel->tx_ring = NULL;
1813 	}
1814 
1815 	for (i = 0 ; i < scctx->isc_nrxqsets; i++) {
1816 
1817 		channel = pdata->channel[i];
1818 
1819 		rx_ring = channel->rx_ring;
1820 		for (j = 0; j < sctx->isc_nrxqs ; j++, rx_ring++) {
1821 			if (rx_ring && rx_ring->rdata)
1822 				free(rx_ring->rdata, M_AXGBE);
1823 		}
1824 		free(channel->rx_ring, M_AXGBE);
1825 		channel->rx_ring = NULL;
1826 	}
1827 
1828 	axgbe_free_channels(sc);
1829 } /* axgbe_if_queues_free */
1830 
1831 static void
1832 axgbe_if_vlan_register(if_ctx_t ctx, uint16_t vtag)
1833 {
1834 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
1835 	struct xgbe_prv_data 	*pdata = &sc->pdata;
1836 	struct xgbe_hw_if 	*hw_if = &pdata->hw_if;
1837 
1838 	if (!bit_test(pdata->active_vlans, vtag)) {
1839 		axgbe_printf(0, "Registering VLAN %d\n", vtag);
1840 
1841 		bit_set(pdata->active_vlans, vtag);
1842 		hw_if->update_vlan_hash_table(pdata);
1843 		pdata->num_active_vlans++;
1844 
1845 		axgbe_printf(1, "Total active vlans: %d\n",
1846 		    pdata->num_active_vlans);
1847 	} else
1848 		axgbe_printf(0, "VLAN %d already registered\n", vtag);
1849 
1850 	xgbe_dump_active_vlans(pdata);
1851 }
1852 
1853 static void
1854 axgbe_if_vlan_unregister(if_ctx_t ctx, uint16_t vtag)
1855 {
1856 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
1857 	struct xgbe_prv_data 	*pdata = &sc->pdata;
1858 	struct xgbe_hw_if 	*hw_if = &pdata->hw_if;
1859 
1860 	if (pdata->num_active_vlans == 0) {
1861 		axgbe_printf(1, "No active VLANs to unregister\n");
1862 		return;
1863 	}
1864 
1865 	if (bit_test(pdata->active_vlans, vtag)){
1866 		axgbe_printf(0, "Un-Registering VLAN %d\n", vtag);
1867 
1868 		bit_clear(pdata->active_vlans, vtag);
1869 		hw_if->update_vlan_hash_table(pdata);
1870 		pdata->num_active_vlans--;
1871 
1872 		axgbe_printf(1, "Total active vlans: %d\n",
1873 		    pdata->num_active_vlans);
1874 	} else
1875 		axgbe_printf(0, "VLAN %d already unregistered\n", vtag);
1876 
1877 	xgbe_dump_active_vlans(pdata);
1878 }
1879 
1880 #if __FreeBSD_version >= 1300000
1881 static bool
1882 axgbe_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
1883 {
1884         switch (event) {
1885         case IFLIB_RESTART_VLAN_CONFIG:
1886         default:
1887                 return (true);
1888         }
1889 }
1890 #endif
1891 
1892 static int
1893 axgbe_if_msix_intr_assign(if_ctx_t ctx, int msix)
1894 {
1895 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
1896 	struct xgbe_prv_data 	*pdata = &sc->pdata;
1897 	if_softc_ctx_t		scctx = sc->scctx;
1898 	struct xgbe_channel	*channel;
1899 	struct if_irq		irq;
1900 	int			i, error, rid = 0, flags;
1901 	char			buf[16];
1902 
1903 	MPASS(scctx->isc_intr != IFLIB_INTR_LEGACY);
1904 
1905 	pdata->isr_as_tasklet = 1;
1906 
1907 	if (scctx->isc_intr == IFLIB_INTR_MSI) {
1908 		pdata->irq_count = 1;
1909 		pdata->channel_irq_count = 1;
1910 		return (0);
1911 	}
1912 
1913 	axgbe_printf(1, "%s: msix %d txqsets %d rxqsets %d\n", __func__, msix,
1914 	    scctx->isc_ntxqsets, scctx->isc_nrxqsets);
1915 
1916 	flags = RF_ACTIVE;
1917 
1918 	/* DEV INTR SETUP */
1919 	rid++;
1920 	error = iflib_irq_alloc_generic(ctx, &pdata->dev_irq, rid,
1921 	    IFLIB_INTR_ADMIN, axgbe_dev_isr, sc, 0, "dev_irq");
1922 	if (error) {
1923 		axgbe_error("Failed to register device interrupt rid %d name %s\n",
1924 		    rid, "dev_irq");
1925 		return (error);
1926 	}
1927 
1928 	/* ECC INTR SETUP */
1929 	rid++;
1930 	pdata->ecc_rid = rid;
1931 	pdata->ecc_irq_res = bus_alloc_resource_any(pdata->dev, SYS_RES_IRQ,
1932 	    &rid, flags);
1933 	if (!pdata->ecc_irq_res) {
1934 		axgbe_error("failed to allocate IRQ for rid %d, name %s.\n",
1935 		    rid, "ecc_irq");
1936 		return (ENOMEM);
1937 	}
1938 
1939 	error = bus_setup_intr(pdata->dev, pdata->ecc_irq_res, INTR_MPSAFE |
1940 	    INTR_TYPE_NET, NULL, axgbe_ecc_isr, sc, &pdata->ecc_irq_tag);
1941         if (error) {
1942                 axgbe_error("failed to setup interrupt for rid %d, name %s: %d\n",
1943 		    rid, "ecc_irq", error);
1944                 return (error);
1945 	}
1946 
1947 	/* I2C INTR SETUP */
1948 	rid++;
1949 	pdata->i2c_rid = rid;
1950         pdata->i2c_irq_res = bus_alloc_resource_any(pdata->dev, SYS_RES_IRQ,
1951 	    &rid, flags);
1952         if (!pdata->i2c_irq_res) {
1953                 axgbe_error("failed to allocate IRQ for rid %d, name %s.\n",
1954 		    rid, "i2c_irq");
1955                 return (ENOMEM);
1956         }
1957 
1958         error = bus_setup_intr(pdata->dev, pdata->i2c_irq_res, INTR_MPSAFE |
1959 	    INTR_TYPE_NET, NULL, axgbe_i2c_isr, sc, &pdata->i2c_irq_tag);
1960         if (error) {
1961                 axgbe_error("failed to setup interrupt for rid %d, name %s: %d\n",
1962 		    rid, "i2c_irq", error);
1963                 return (error);
1964 	}
1965 
1966 	/* AN INTR SETUP */
1967 	rid++;
1968 	pdata->an_rid = rid;
1969         pdata->an_irq_res = bus_alloc_resource_any(pdata->dev, SYS_RES_IRQ,
1970 	    &rid, flags);
1971         if (!pdata->an_irq_res) {
1972                 axgbe_error("failed to allocate IRQ for rid %d, name %s.\n",
1973 		    rid, "an_irq");
1974                 return (ENOMEM);
1975         }
1976 
1977         error = bus_setup_intr(pdata->dev, pdata->an_irq_res, INTR_MPSAFE |
1978 	    INTR_TYPE_NET, NULL, axgbe_an_isr, sc, &pdata->an_irq_tag);
1979         if (error) {
1980                 axgbe_error("failed to setup interrupt for rid %d, name %s: %d\n",
1981 		    rid, "an_irq", error);
1982                 return (error);
1983 	}
1984 
1985 	pdata->per_channel_irq = 1;
1986 	pdata->channel_irq_mode = XGBE_IRQ_MODE_LEVEL;
1987 	rid++;
1988 	for (i = 0; i < scctx->isc_nrxqsets; i++, rid++) {
1989 
1990 		channel = pdata->channel[i];
1991 
1992 		snprintf(buf, sizeof(buf), "rxq%d", i);
1993 		error = iflib_irq_alloc_generic(ctx, &irq, rid, IFLIB_INTR_RXTX,
1994 		    axgbe_msix_que, channel, channel->queue_index, buf);
1995 
1996 		if (error) {
1997 			axgbe_error("Failed to allocated que int %d err: %d\n",
1998 			    i, error);
1999 			return (error);
2000 		}
2001 
2002 		channel->dma_irq_rid = rid;
2003 		channel->dma_irq_res = irq.ii_res;
2004 		channel->dma_irq_tag = irq.ii_tag;
2005 		axgbe_printf(1, "%s: channel count %d idx %d irq %d\n",
2006 		    __func__, scctx->isc_nrxqsets, i, rid);
2007 	}
2008 	pdata->irq_count = msix;
2009 	pdata->channel_irq_count = scctx->isc_nrxqsets;
2010 
2011 	for (i = 0; i < scctx->isc_ntxqsets; i++) {
2012 
2013 		channel = pdata->channel[i];
2014 
2015 		snprintf(buf, sizeof(buf), "txq%d", i);
2016 		irq.ii_res = channel->dma_irq_res;
2017 		iflib_softirq_alloc_generic(ctx, &irq, IFLIB_INTR_TX, channel,
2018 		    channel->queue_index, buf);
2019 	}
2020 
2021 	return (0);
2022 } /* axgbe_if_msix_intr_assign */
2023 
2024 static int
2025 xgbe_enable_rx_tx_int(struct xgbe_prv_data *pdata, struct xgbe_channel *channel)
2026 {
2027         struct xgbe_hw_if *hw_if = &pdata->hw_if;
2028         enum xgbe_int int_id;
2029 
2030 	if (channel->tx_ring && channel->rx_ring)
2031 		int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
2032 	else if (channel->tx_ring)
2033 		int_id = XGMAC_INT_DMA_CH_SR_TI;
2034 	else if (channel->rx_ring)
2035 		int_id = XGMAC_INT_DMA_CH_SR_RI;
2036 	else
2037 		return (-1);
2038 
2039 	axgbe_printf(1, "%s channel: %d rx_tx interrupt enabled %d\n",
2040 	    __func__, channel->queue_index, int_id);
2041         return (hw_if->enable_int(channel, int_id));
2042 }
2043 
2044 static void
2045 xgbe_disable_rx_tx_int(struct xgbe_prv_data *pdata, struct xgbe_channel *channel)
2046 {
2047         struct xgbe_hw_if *hw_if = &pdata->hw_if;
2048         enum xgbe_int int_id;
2049 
2050         if (channel->tx_ring && channel->rx_ring)
2051                 int_id = XGMAC_INT_DMA_CH_SR_TI_RI;
2052         else if (channel->tx_ring)
2053                 int_id = XGMAC_INT_DMA_CH_SR_TI;
2054         else if (channel->rx_ring)
2055                 int_id = XGMAC_INT_DMA_CH_SR_RI;
2056         else
2057                 return;
2058 
2059 	axgbe_printf(1, "%s channel: %d rx_tx interrupt disabled %d\n",
2060 	    __func__, channel->queue_index, int_id);
2061         hw_if->disable_int(channel, int_id);
2062 }
2063 
2064 static void
2065 xgbe_disable_rx_tx_ints(struct xgbe_prv_data *pdata)
2066 {
2067         unsigned int i;
2068 
2069         for (i = 0; i < pdata->channel_count; i++)
2070                 xgbe_disable_rx_tx_int(pdata, pdata->channel[i]);
2071 }
2072 
2073 static int
2074 axgbe_msix_que(void *arg)
2075 {
2076 	struct xgbe_channel	*channel = (struct xgbe_channel *)arg;
2077 	struct xgbe_prv_data	*pdata = channel->pdata;
2078 	unsigned int 		dma_status;
2079 
2080 	axgbe_printf(1, "%s: Channel: %d SR 0x%04x DSR 0x%04x IER:0x%04x D_ISR:0x%04x M_ISR:0x%04x\n",
2081 	    __func__, channel->queue_index,
2082 	    XGMAC_DMA_IOREAD(channel, DMA_CH_SR),
2083 	    XGMAC_DMA_IOREAD(channel, DMA_CH_DSR),
2084 	    XGMAC_DMA_IOREAD(channel, DMA_CH_IER),
2085 	    XGMAC_IOREAD(pdata, DMA_ISR),
2086 	    XGMAC_IOREAD(pdata, MAC_ISR));
2087 
2088 	(void)XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
2089 
2090 	/* Disable Tx and Rx channel interrupts */
2091 	xgbe_disable_rx_tx_int(pdata, channel);
2092 
2093 	/* Clear the interrupts */
2094 	dma_status = 0;
2095 	XGMAC_SET_BITS(dma_status, DMA_CH_SR, TI, 1);
2096 	XGMAC_SET_BITS(dma_status, DMA_CH_SR, RI, 1);
2097 	XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_status);
2098 
2099 	return (FILTER_SCHEDULE_THREAD);
2100 }
2101 
2102 static int
2103 axgbe_dev_isr(void *arg)
2104 {
2105 	struct axgbe_if_softc *sc = (struct axgbe_if_softc *)arg;
2106 	struct xgbe_prv_data	*pdata = &sc->pdata;
2107 	struct xgbe_channel	*channel;
2108 	struct xgbe_hw_if	*hw_if = &pdata->hw_if;
2109 	unsigned int		i, dma_isr, dma_ch_isr;
2110 	unsigned int		mac_isr, mac_mdioisr;
2111 	int ret = FILTER_HANDLED;
2112 
2113 	dma_isr = XGMAC_IOREAD(pdata, DMA_ISR);
2114 	axgbe_printf(2, "%s DMA ISR: 0x%x\n", __func__, dma_isr);
2115 
2116         if (!dma_isr)
2117                 return (FILTER_HANDLED);
2118 
2119         for (i = 0; i < pdata->channel_count; i++) {
2120 
2121                 if (!(dma_isr & (1 << i)))
2122                         continue;
2123 
2124                 channel = pdata->channel[i];
2125 
2126                 dma_ch_isr = XGMAC_DMA_IOREAD(channel, DMA_CH_SR);
2127 		axgbe_printf(2, "%s: channel %d SR 0x%x DSR 0x%x\n", __func__,
2128 		    channel->queue_index, dma_ch_isr, XGMAC_DMA_IOREAD(channel,
2129 		    DMA_CH_DSR));
2130 
2131                 /*
2132 		 * The TI or RI interrupt bits may still be set even if using
2133                  * per channel DMA interrupts. Check to be sure those are not
2134                  * enabled before using the private data napi structure.
2135                  */
2136 		if (!pdata->per_channel_irq &&
2137 		    (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, TI) ||
2138 		    XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RI))) {
2139 
2140 			/* Disable Tx and Rx interrupts */
2141 			xgbe_disable_rx_tx_ints(pdata);
2142                 } else {
2143 
2144 			/*
2145 			 * Don't clear Rx/Tx status if doing per channel DMA
2146 			 * interrupts, these will be cleared by the ISR for
2147 		 	 * per channel DMA interrupts
2148 		 	 */
2149                 	XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, TI, 0);
2150                 	XGMAC_SET_BITS(dma_ch_isr, DMA_CH_SR, RI, 0);
2151 		}
2152 
2153                 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, RBU))
2154                         pdata->ext_stats.rx_buffer_unavailable++;
2155 
2156                 /* Restart the device on a Fatal Bus Error */
2157                 if (XGMAC_GET_BITS(dma_ch_isr, DMA_CH_SR, FBE))
2158 			axgbe_error("%s: Fatal bus error reported 0x%x\n",
2159 			    __func__, dma_ch_isr);
2160 
2161                 /* Clear all interrupt signals */
2162                 XGMAC_DMA_IOWRITE(channel, DMA_CH_SR, dma_ch_isr);
2163 
2164 		ret = FILTER_SCHEDULE_THREAD;
2165         }
2166 
2167         if (XGMAC_GET_BITS(dma_isr, DMA_ISR, MACIS)) {
2168 
2169                 mac_isr = XGMAC_IOREAD(pdata, MAC_ISR);
2170 		axgbe_printf(2, "%s MAC ISR: 0x%x\n", __func__, mac_isr);
2171 
2172                 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCTXIS))
2173                         hw_if->tx_mmc_int(pdata);
2174 
2175                 if (XGMAC_GET_BITS(mac_isr, MAC_ISR, MMCRXIS))
2176                         hw_if->rx_mmc_int(pdata);
2177 
2178 		if (XGMAC_GET_BITS(mac_isr, MAC_ISR, SMI)) {
2179 			mac_mdioisr = XGMAC_IOREAD(pdata, MAC_MDIOISR);
2180 
2181 			if (XGMAC_GET_BITS(mac_mdioisr, MAC_MDIOISR,
2182 			    SNGLCOMPINT))
2183 				wakeup_one(pdata);
2184 		}
2185 
2186 	}
2187 
2188 	return (ret);
2189 } /* axgbe_dev_isr */
2190 
2191 static void
2192 axgbe_i2c_isr(void *arg)
2193 {
2194 	struct axgbe_if_softc *sc = (struct axgbe_if_softc *)arg;
2195 
2196 	sc->pdata.i2c_if.i2c_isr(&sc->pdata);
2197 }
2198 
2199 static void
2200 axgbe_ecc_isr(void *arg)
2201 {
2202 	/* TODO - implement */
2203 }
2204 
2205 static void
2206 axgbe_an_isr(void *arg)
2207 {
2208 	struct axgbe_if_softc *sc = (struct axgbe_if_softc *)arg;
2209 
2210 	sc->pdata.phy_if.an_isr(&sc->pdata);
2211 }
2212 
2213 static int
2214 axgbe_if_tx_queue_intr_enable(if_ctx_t ctx, uint16_t qid)
2215 {
2216 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
2217 	struct xgbe_prv_data 	*pdata = &sc->pdata;
2218 	int ret;
2219 
2220 	if (qid < pdata->tx_q_count) {
2221 		ret = xgbe_enable_rx_tx_int(pdata, pdata->channel[qid]);
2222 		if (ret) {
2223 			axgbe_error("Enable TX INT failed\n");
2224 			return (ret);
2225 		}
2226 	} else
2227 		axgbe_error("Queue ID exceed channel count\n");
2228 
2229 	return (0);
2230 }
2231 
2232 static int
2233 axgbe_if_rx_queue_intr_enable(if_ctx_t ctx, uint16_t qid)
2234 {
2235 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
2236 	struct xgbe_prv_data 	*pdata = &sc->pdata;
2237 	int ret;
2238 
2239 	if (qid < pdata->rx_q_count) {
2240 		ret = xgbe_enable_rx_tx_int(pdata, pdata->channel[qid]);
2241 		if (ret) {
2242 			axgbe_error("Enable RX INT failed\n");
2243 			return (ret);
2244 		}
2245 	} else
2246 		axgbe_error("Queue ID exceed channel count\n");
2247 
2248 	return (0);
2249 }
2250 
2251 static void
2252 axgbe_if_update_admin_status(if_ctx_t ctx)
2253 {
2254 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
2255 	struct xgbe_prv_data 	*pdata = &sc->pdata;
2256 
2257 	axgbe_printf(1, "%s: phy_link %d status %d speed %d\n", __func__,
2258 	    pdata->phy_link, sc->link_status, pdata->phy.speed);
2259 
2260 	if (pdata->phy_link < 0)
2261 		return;
2262 
2263 	if (pdata->phy_link) {
2264 		if (sc->link_status == LINK_STATE_DOWN) {
2265 			sc->link_status = LINK_STATE_UP;
2266 			if (pdata->phy.speed & SPEED_10000)
2267 				iflib_link_state_change(ctx, LINK_STATE_UP,
2268 				    IF_Gbps(10));
2269 			else if (pdata->phy.speed & SPEED_2500)
2270 				iflib_link_state_change(ctx, LINK_STATE_UP,
2271 				    IF_Gbps(2.5));
2272 			else if (pdata->phy.speed & SPEED_1000)
2273 				iflib_link_state_change(ctx, LINK_STATE_UP,
2274 				    IF_Gbps(1));
2275 			else if (pdata->phy.speed & SPEED_100)
2276 				iflib_link_state_change(ctx, LINK_STATE_UP,
2277 				    IF_Mbps(100));
2278 			else if (pdata->phy.speed & SPEED_10)
2279 				iflib_link_state_change(ctx, LINK_STATE_UP,
2280 				    IF_Mbps(10));
2281 		}
2282 	} else {
2283 		if (sc->link_status == LINK_STATE_UP) {
2284 			sc->link_status = LINK_STATE_DOWN;
2285 			iflib_link_state_change(ctx, LINK_STATE_DOWN, 0);
2286 		}
2287 	}
2288 }
2289 
2290 static int
2291 axgbe_if_media_change(if_ctx_t ctx)
2292 {
2293         struct axgbe_if_softc   *sc = iflib_get_softc(ctx);
2294         struct ifmedia          *ifm = iflib_get_media(ctx);
2295 
2296         sx_xlock(&sc->pdata.an_mutex);
2297         if (IFM_TYPE(ifm->ifm_media) != IFM_ETHER)
2298                 return (EINVAL);
2299 
2300         switch (IFM_SUBTYPE(ifm->ifm_media)) {
2301         case IFM_10G_KR:
2302                 sc->pdata.phy.speed = SPEED_10000;
2303                 sc->pdata.phy.autoneg = AUTONEG_DISABLE;
2304                 break;
2305         case IFM_2500_KX:
2306                 sc->pdata.phy.speed = SPEED_2500;
2307                 sc->pdata.phy.autoneg = AUTONEG_DISABLE;
2308                 break;
2309         case IFM_1000_KX:
2310                 sc->pdata.phy.speed = SPEED_1000;
2311                 sc->pdata.phy.autoneg = AUTONEG_DISABLE;
2312                 break;
2313         case IFM_100_TX:
2314                 sc->pdata.phy.speed = SPEED_100;
2315                 sc->pdata.phy.autoneg = AUTONEG_DISABLE;
2316                 break;
2317         case IFM_AUTO:
2318                 sc->pdata.phy.autoneg = AUTONEG_ENABLE;
2319                 break;
2320         }
2321         sx_xunlock(&sc->pdata.an_mutex);
2322 
2323         return (-sc->pdata.phy_if.phy_config_aneg(&sc->pdata));
2324 }
2325 
2326 static int
2327 axgbe_if_promisc_set(if_ctx_t ctx, int flags)
2328 {
2329 	struct axgbe_if_softc *sc = iflib_get_softc(ctx);
2330 	struct xgbe_prv_data *pdata = &sc->pdata;
2331 	if_t ifp = pdata->netdev;
2332 
2333 	axgbe_printf(1, "%s: MAC_PFR 0x%x drv_flags 0x%x if_flags 0x%x\n",
2334 	    __func__, XGMAC_IOREAD(pdata, MAC_PFR), if_getdrvflags(ifp),
2335 	    if_getflags(ifp));
2336 
2337 	if (if_getflags(ifp) & IFF_PPROMISC) {
2338 
2339 		axgbe_printf(1, "User requested to enter promisc mode\n");
2340 
2341 		if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == 1) {
2342 			axgbe_printf(1, "Already in promisc mode\n");
2343 			return (0);
2344 		}
2345 
2346 		axgbe_printf(1, "Entering promisc mode\n");
2347 		XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, 1);
2348 		XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 0);
2349 	} else {
2350 
2351 		axgbe_printf(1, "User requested to leave promisc mode\n");
2352 
2353 		if (XGMAC_IOREAD_BITS(pdata, MAC_PFR, PR) == 0) {
2354 			axgbe_printf(1, "Already not in promisc mode\n");
2355 			return (0);
2356 		}
2357 
2358 		axgbe_printf(1, "Leaving promisc mode\n");
2359 		XGMAC_IOWRITE_BITS(pdata, MAC_PFR, PR, 0);
2360 		XGMAC_IOWRITE_BITS(pdata, MAC_PFR, VTFE, 1);
2361 	}
2362 
2363 	return (0);
2364 }
2365 
2366 static uint64_t
2367 axgbe_if_get_counter(if_ctx_t ctx, ift_counter cnt)
2368 {
2369 	struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
2370         if_t			 ifp = iflib_get_ifp(ctx);
2371         struct xgbe_prv_data    *pdata = &sc->pdata;
2372         struct xgbe_mmc_stats	*pstats = &pdata->mmc_stats;
2373 
2374         pdata->hw_if.read_mmc_stats(pdata);
2375 
2376         switch(cnt) {
2377         case IFCOUNTER_IPACKETS:
2378                 return (pstats->rxframecount_gb);
2379         case IFCOUNTER_IERRORS:
2380                 return (pstats->rxframecount_gb - pstats->rxbroadcastframes_g -
2381                     pstats->rxmulticastframes_g - pstats->rxunicastframes_g);
2382         case IFCOUNTER_OPACKETS:
2383                 return (pstats->txframecount_gb);
2384         case IFCOUNTER_OERRORS:
2385                 return (pstats->txframecount_gb - pstats->txframecount_g);
2386         case IFCOUNTER_IBYTES:
2387                 return (pstats->rxoctetcount_gb);
2388         case IFCOUNTER_OBYTES:
2389                 return (pstats->txoctetcount_gb);
2390         default:
2391                 return (if_get_counter_default(ifp, cnt));
2392         }
2393 }
2394 
2395 static int
2396 axgbe_if_mtu_set(if_ctx_t ctx, uint32_t mtu)
2397 {
2398         struct axgbe_if_softc	*sc = iflib_get_softc(ctx);
2399 	struct xgbe_prv_data	*pdata = &sc->pdata;
2400 	int ret;
2401 
2402         if (mtu > XGMAC_JUMBO_PACKET_MTU)
2403                 return (EINVAL);
2404 
2405 	ret = xgbe_calc_rx_buf_size(pdata->netdev, mtu);
2406         pdata->rx_buf_size = ret;
2407         axgbe_printf(1, "%s: rx_buf_size %d\n", __func__, ret);
2408 
2409         sc->scctx->isc_max_frame_size = mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
2410         return (0);
2411 }
2412 
2413 static void
2414 axgbe_if_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
2415 {
2416         struct axgbe_if_softc *sc = iflib_get_softc(ctx);
2417         struct xgbe_prv_data *pdata = &sc->pdata;
2418 
2419         ifmr->ifm_status = IFM_AVALID;
2420 	if (!sc->pdata.phy.link)
2421 		return;
2422 
2423 	ifmr->ifm_active = IFM_ETHER;
2424 	ifmr->ifm_status |= IFM_ACTIVE;
2425 
2426 	axgbe_printf(1, "Speed 0x%x Mode %d\n", sc->pdata.phy.speed,
2427 	    pdata->phy_if.phy_impl.cur_mode(pdata));
2428 	pdata->phy_if.phy_impl.get_type(pdata, ifmr);
2429 
2430 	ifmr->ifm_active |= IFM_FDX;
2431 	ifmr->ifm_active |= IFM_ETH_TXPAUSE;
2432 	ifmr->ifm_active |= IFM_ETH_RXPAUSE;
2433 }
2434