xref: /freebsd/sys/dev/age/if_age.c (revision 298cf604ccf133b101c6fad42d1a078a1fac58ca)
1 /*-
2  * Copyright (c) 2008, Pyun YongHyeon <yongari@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    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 
28 /* Driver for Attansic Technology Corp. L1 Gigabit Ethernet. */
29 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bus.h>
36 #include <sys/endian.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/mbuf.h>
40 #include <sys/rman.h>
41 #include <sys/module.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
47 
48 #include <net/bpf.h>
49 #include <net/if.h>
50 #include <net/if_arp.h>
51 #include <net/ethernet.h>
52 #include <net/if_dl.h>
53 #include <net/if_media.h>
54 #include <net/if_types.h>
55 #include <net/if_vlan_var.h>
56 
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
61 
62 #include <dev/mii/mii.h>
63 #include <dev/mii/miivar.h>
64 
65 #include <dev/pci/pcireg.h>
66 #include <dev/pci/pcivar.h>
67 
68 #include <machine/bus.h>
69 #include <machine/in_cksum.h>
70 
71 #include <dev/age/if_agereg.h>
72 #include <dev/age/if_agevar.h>
73 
74 /* "device miibus" required.  See GENERIC if you get errors here. */
75 #include "miibus_if.h"
76 
77 #define	AGE_CSUM_FEATURES	(CSUM_TCP | CSUM_UDP)
78 
79 MODULE_DEPEND(age, pci, 1, 1, 1);
80 MODULE_DEPEND(age, ether, 1, 1, 1);
81 MODULE_DEPEND(age, miibus, 1, 1, 1);
82 
83 /* Tunables. */
84 static int msi_disable = 0;
85 static int msix_disable = 0;
86 TUNABLE_INT("hw.age.msi_disable", &msi_disable);
87 TUNABLE_INT("hw.age.msix_disable", &msix_disable);
88 
89 /*
90  * Devices supported by this driver.
91  */
92 static struct age_dev {
93 	uint16_t	age_vendorid;
94 	uint16_t	age_deviceid;
95 	const char	*age_name;
96 } age_devs[] = {
97 	{ VENDORID_ATTANSIC, DEVICEID_ATTANSIC_L1,
98 	    "Attansic Technology Corp, L1 Gigabit Ethernet" },
99 };
100 
101 static int age_miibus_readreg(device_t, int, int);
102 static int age_miibus_writereg(device_t, int, int, int);
103 static void age_miibus_statchg(device_t);
104 static void age_mediastatus(struct ifnet *, struct ifmediareq *);
105 static int age_mediachange(struct ifnet *);
106 static int age_probe(device_t);
107 static void age_get_macaddr(struct age_softc *);
108 static void age_phy_reset(struct age_softc *);
109 static int age_attach(device_t);
110 static int age_detach(device_t);
111 static void age_sysctl_node(struct age_softc *);
112 static void age_dmamap_cb(void *, bus_dma_segment_t *, int, int);
113 static int age_check_boundary(struct age_softc *);
114 static int age_dma_alloc(struct age_softc *);
115 static void age_dma_free(struct age_softc *);
116 static int age_shutdown(device_t);
117 static void age_setwol(struct age_softc *);
118 static int age_suspend(device_t);
119 static int age_resume(device_t);
120 static int age_encap(struct age_softc *, struct mbuf **);
121 static void age_start(struct ifnet *);
122 static void age_start_locked(struct ifnet *);
123 static void age_watchdog(struct age_softc *);
124 static int age_ioctl(struct ifnet *, u_long, caddr_t);
125 static void age_mac_config(struct age_softc *);
126 static void age_link_task(void *, int);
127 static void age_stats_update(struct age_softc *);
128 static int age_intr(void *);
129 static void age_int_task(void *, int);
130 static void age_txintr(struct age_softc *, int);
131 static void age_rxeof(struct age_softc *sc, struct rx_rdesc *);
132 static int age_rxintr(struct age_softc *, int, int);
133 static void age_tick(void *);
134 static void age_reset(struct age_softc *);
135 static void age_init(void *);
136 static void age_init_locked(struct age_softc *);
137 static void age_stop(struct age_softc *);
138 static void age_stop_txmac(struct age_softc *);
139 static void age_stop_rxmac(struct age_softc *);
140 static void age_init_tx_ring(struct age_softc *);
141 static int age_init_rx_ring(struct age_softc *);
142 static void age_init_rr_ring(struct age_softc *);
143 static void age_init_cmb_block(struct age_softc *);
144 static void age_init_smb_block(struct age_softc *);
145 static int age_newbuf(struct age_softc *, struct age_rxdesc *);
146 static void age_rxvlan(struct age_softc *);
147 static void age_rxfilter(struct age_softc *);
148 static int sysctl_age_stats(SYSCTL_HANDLER_ARGS);
149 static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
150 static int sysctl_hw_age_proc_limit(SYSCTL_HANDLER_ARGS);
151 static int sysctl_hw_age_int_mod(SYSCTL_HANDLER_ARGS);
152 
153 
154 static device_method_t age_methods[] = {
155 	/* Device interface. */
156 	DEVMETHOD(device_probe,		age_probe),
157 	DEVMETHOD(device_attach,	age_attach),
158 	DEVMETHOD(device_detach,	age_detach),
159 	DEVMETHOD(device_shutdown,	age_shutdown),
160 	DEVMETHOD(device_suspend,	age_suspend),
161 	DEVMETHOD(device_resume,	age_resume),
162 
163 	/* MII interface. */
164 	DEVMETHOD(miibus_readreg,	age_miibus_readreg),
165 	DEVMETHOD(miibus_writereg,	age_miibus_writereg),
166 	DEVMETHOD(miibus_statchg,	age_miibus_statchg),
167 
168 	{ NULL, NULL }
169 };
170 
171 static driver_t age_driver = {
172 	"age",
173 	age_methods,
174 	sizeof(struct age_softc)
175 };
176 
177 static devclass_t age_devclass;
178 
179 DRIVER_MODULE(age, pci, age_driver, age_devclass, 0, 0);
180 DRIVER_MODULE(miibus, age, miibus_driver, miibus_devclass, 0, 0);
181 
182 static struct resource_spec age_res_spec_mem[] = {
183 	{ SYS_RES_MEMORY,	PCIR_BAR(0),	RF_ACTIVE },
184 	{ -1,			0,		0 }
185 };
186 
187 static struct resource_spec age_irq_spec_legacy[] = {
188 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
189 	{ -1,			0,		0 }
190 };
191 
192 static struct resource_spec age_irq_spec_msi[] = {
193 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
194 	{ -1,			0,		0 }
195 };
196 
197 static struct resource_spec age_irq_spec_msix[] = {
198 	{ SYS_RES_IRQ,		1,		RF_ACTIVE },
199 	{ -1,			0,		0 }
200 };
201 
202 /*
203  *	Read a PHY register on the MII of the L1.
204  */
205 static int
206 age_miibus_readreg(device_t dev, int phy, int reg)
207 {
208 	struct age_softc *sc;
209 	uint32_t v;
210 	int i;
211 
212 	sc = device_get_softc(dev);
213 
214 	CSR_WRITE_4(sc, AGE_MDIO, MDIO_OP_EXECUTE | MDIO_OP_READ |
215 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
216 	for (i = AGE_PHY_TIMEOUT; i > 0; i--) {
217 		DELAY(1);
218 		v = CSR_READ_4(sc, AGE_MDIO);
219 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
220 			break;
221 	}
222 
223 	if (i == 0) {
224 		device_printf(sc->age_dev, "phy read timeout : %d\n", reg);
225 		return (0);
226 	}
227 
228 	return ((v & MDIO_DATA_MASK) >> MDIO_DATA_SHIFT);
229 }
230 
231 /*
232  *	Write a PHY register on the MII of the L1.
233  */
234 static int
235 age_miibus_writereg(device_t dev, int phy, int reg, int val)
236 {
237 	struct age_softc *sc;
238 	uint32_t v;
239 	int i;
240 
241 	sc = device_get_softc(dev);
242 
243 	CSR_WRITE_4(sc, AGE_MDIO, MDIO_OP_EXECUTE | MDIO_OP_WRITE |
244 	    (val & MDIO_DATA_MASK) << MDIO_DATA_SHIFT |
245 	    MDIO_SUP_PREAMBLE | MDIO_CLK_25_4 | MDIO_REG_ADDR(reg));
246 	for (i = AGE_PHY_TIMEOUT; i > 0; i--) {
247 		DELAY(1);
248 		v = CSR_READ_4(sc, AGE_MDIO);
249 		if ((v & (MDIO_OP_EXECUTE | MDIO_OP_BUSY)) == 0)
250 			break;
251 	}
252 
253 	if (i == 0)
254 		device_printf(sc->age_dev, "phy write timeout : %d\n", reg);
255 
256 	return (0);
257 }
258 
259 /*
260  *	Callback from MII layer when media changes.
261  */
262 static void
263 age_miibus_statchg(device_t dev)
264 {
265 	struct age_softc *sc;
266 
267 	sc = device_get_softc(dev);
268 	taskqueue_enqueue(taskqueue_swi, &sc->age_link_task);
269 }
270 
271 /*
272  *	Get the current interface media status.
273  */
274 static void
275 age_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
276 {
277 	struct age_softc *sc;
278 	struct mii_data *mii;
279 
280 	sc = ifp->if_softc;
281 	AGE_LOCK(sc);
282 	mii = device_get_softc(sc->age_miibus);
283 
284 	mii_pollstat(mii);
285 	ifmr->ifm_status = mii->mii_media_status;
286 	ifmr->ifm_active = mii->mii_media_active;
287 	AGE_UNLOCK(sc);
288 }
289 
290 /*
291  *	Set hardware to newly-selected media.
292  */
293 static int
294 age_mediachange(struct ifnet *ifp)
295 {
296 	struct age_softc *sc;
297 	struct mii_data *mii;
298 	struct mii_softc *miisc;
299 	int error;
300 
301 	sc = ifp->if_softc;
302 	AGE_LOCK(sc);
303 	mii = device_get_softc(sc->age_miibus);
304 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
305 		PHY_RESET(miisc);
306 	error = mii_mediachg(mii);
307 	AGE_UNLOCK(sc);
308 
309 	return (error);
310 }
311 
312 static int
313 age_probe(device_t dev)
314 {
315 	struct age_dev *sp;
316 	int i;
317 	uint16_t vendor, devid;
318 
319 	vendor = pci_get_vendor(dev);
320 	devid = pci_get_device(dev);
321 	sp = age_devs;
322 	for (i = 0; i < sizeof(age_devs) / sizeof(age_devs[0]);
323 	    i++, sp++) {
324 		if (vendor == sp->age_vendorid &&
325 		    devid == sp->age_deviceid) {
326 			device_set_desc(dev, sp->age_name);
327 			return (BUS_PROBE_DEFAULT);
328 		}
329 	}
330 
331 	return (ENXIO);
332 }
333 
334 static void
335 age_get_macaddr(struct age_softc *sc)
336 {
337 	uint32_t ea[2], reg;
338 	int i, vpdc;
339 
340 	reg = CSR_READ_4(sc, AGE_SPI_CTRL);
341 	if ((reg & SPI_VPD_ENB) != 0) {
342 		/* Get VPD stored in TWSI EEPROM. */
343 		reg &= ~SPI_VPD_ENB;
344 		CSR_WRITE_4(sc, AGE_SPI_CTRL, reg);
345 	}
346 
347 	if (pci_find_cap(sc->age_dev, PCIY_VPD, &vpdc) == 0) {
348 		/*
349 		 * PCI VPD capability found, let TWSI reload EEPROM.
350 		 * This will set ethernet address of controller.
351 		 */
352 		CSR_WRITE_4(sc, AGE_TWSI_CTRL, CSR_READ_4(sc, AGE_TWSI_CTRL) |
353 		    TWSI_CTRL_SW_LD_START);
354 		for (i = 100; i > 0; i--) {
355 			DELAY(1000);
356 			reg = CSR_READ_4(sc, AGE_TWSI_CTRL);
357 			if ((reg & TWSI_CTRL_SW_LD_START) == 0)
358 				break;
359 		}
360 		if (i == 0)
361 			device_printf(sc->age_dev,
362 			    "reloading EEPROM timeout!\n");
363 	} else {
364 		if (bootverbose)
365 			device_printf(sc->age_dev,
366 			    "PCI VPD capability not found!\n");
367 	}
368 
369 	ea[0] = CSR_READ_4(sc, AGE_PAR0);
370 	ea[1] = CSR_READ_4(sc, AGE_PAR1);
371 	sc->age_eaddr[0] = (ea[1] >> 8) & 0xFF;
372 	sc->age_eaddr[1] = (ea[1] >> 0) & 0xFF;
373 	sc->age_eaddr[2] = (ea[0] >> 24) & 0xFF;
374 	sc->age_eaddr[3] = (ea[0] >> 16) & 0xFF;
375 	sc->age_eaddr[4] = (ea[0] >> 8) & 0xFF;
376 	sc->age_eaddr[5] = (ea[0] >> 0) & 0xFF;
377 }
378 
379 static void
380 age_phy_reset(struct age_softc *sc)
381 {
382 	uint16_t reg, pn;
383 	int i, linkup;
384 
385 	/* Reset PHY. */
386 	CSR_WRITE_4(sc, AGE_GPHY_CTRL, GPHY_CTRL_RST);
387 	DELAY(2000);
388 	CSR_WRITE_4(sc, AGE_GPHY_CTRL, GPHY_CTRL_CLR);
389 	DELAY(2000);
390 
391 #define	ATPHY_DBG_ADDR		0x1D
392 #define	ATPHY_DBG_DATA		0x1E
393 #define	ATPHY_CDTC		0x16
394 #define	PHY_CDTC_ENB		0x0001
395 #define	PHY_CDTC_POFF		8
396 #define	ATPHY_CDTS		0x1C
397 #define	PHY_CDTS_STAT_OK	0x0000
398 #define	PHY_CDTS_STAT_SHORT	0x0100
399 #define	PHY_CDTS_STAT_OPEN	0x0200
400 #define	PHY_CDTS_STAT_INVAL	0x0300
401 #define	PHY_CDTS_STAT_MASK	0x0300
402 
403 	/* Check power saving mode. Magic from Linux. */
404 	age_miibus_writereg(sc->age_dev, sc->age_phyaddr, MII_BMCR, BMCR_RESET);
405 	for (linkup = 0, pn = 0; pn < 4; pn++) {
406 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr, ATPHY_CDTC,
407 		    (pn << PHY_CDTC_POFF) | PHY_CDTC_ENB);
408 		for (i = 200; i > 0; i--) {
409 			DELAY(1000);
410 			reg = age_miibus_readreg(sc->age_dev, sc->age_phyaddr,
411 			    ATPHY_CDTC);
412 			if ((reg & PHY_CDTC_ENB) == 0)
413 				break;
414 		}
415 		DELAY(1000);
416 		reg = age_miibus_readreg(sc->age_dev, sc->age_phyaddr,
417 		    ATPHY_CDTS);
418 		if ((reg & PHY_CDTS_STAT_MASK) != PHY_CDTS_STAT_OPEN) {
419 			linkup++;
420 			break;
421 		}
422 	}
423 	age_miibus_writereg(sc->age_dev, sc->age_phyaddr, MII_BMCR,
424 	    BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
425 	if (linkup == 0) {
426 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
427 		    ATPHY_DBG_ADDR, 0);
428 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
429 		    ATPHY_DBG_DATA, 0x124E);
430 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
431 		    ATPHY_DBG_ADDR, 1);
432 		reg = age_miibus_readreg(sc->age_dev, sc->age_phyaddr,
433 		    ATPHY_DBG_DATA);
434 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
435 		    ATPHY_DBG_DATA, reg | 0x03);
436 		/* XXX */
437 		DELAY(1500 * 1000);
438 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
439 		    ATPHY_DBG_ADDR, 0);
440 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
441 		    ATPHY_DBG_DATA, 0x024E);
442     }
443 
444 #undef	ATPHY_DBG_ADDR
445 #undef	ATPHY_DBG_DATA
446 #undef	ATPHY_CDTC
447 #undef	PHY_CDTC_ENB
448 #undef	PHY_CDTC_POFF
449 #undef	ATPHY_CDTS
450 #undef	PHY_CDTS_STAT_OK
451 #undef	PHY_CDTS_STAT_SHORT
452 #undef	PHY_CDTS_STAT_OPEN
453 #undef	PHY_CDTS_STAT_INVAL
454 #undef	PHY_CDTS_STAT_MASK
455 }
456 
457 static int
458 age_attach(device_t dev)
459 {
460 	struct age_softc *sc;
461 	struct ifnet *ifp;
462 	uint16_t burst;
463 	int error, i, msic, msixc, pmc;
464 
465 	error = 0;
466 	sc = device_get_softc(dev);
467 	sc->age_dev = dev;
468 
469 	mtx_init(&sc->age_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
470 	    MTX_DEF);
471 	callout_init_mtx(&sc->age_tick_ch, &sc->age_mtx, 0);
472 	TASK_INIT(&sc->age_int_task, 0, age_int_task, sc);
473 	TASK_INIT(&sc->age_link_task, 0, age_link_task, sc);
474 
475 	/* Map the device. */
476 	pci_enable_busmaster(dev);
477 	sc->age_res_spec = age_res_spec_mem;
478 	sc->age_irq_spec = age_irq_spec_legacy;
479 	error = bus_alloc_resources(dev, sc->age_res_spec, sc->age_res);
480 	if (error != 0) {
481 		device_printf(dev, "cannot allocate memory resources.\n");
482 		goto fail;
483 	}
484 
485 	/* Set PHY address. */
486 	sc->age_phyaddr = AGE_PHY_ADDR;
487 
488 	/* Reset PHY. */
489 	age_phy_reset(sc);
490 
491 	/* Reset the ethernet controller. */
492 	age_reset(sc);
493 
494 	/* Get PCI and chip id/revision. */
495 	sc->age_rev = pci_get_revid(dev);
496 	sc->age_chip_rev = CSR_READ_4(sc, AGE_MASTER_CFG) >>
497 	    MASTER_CHIP_REV_SHIFT;
498 	if (bootverbose) {
499 		device_printf(dev, "PCI device revision : 0x%04x\n",
500 		    sc->age_rev);
501 		device_printf(dev, "Chip id/revision : 0x%04x\n",
502 		    sc->age_chip_rev);
503 	}
504 
505 	/*
506 	 * XXX
507 	 * Unintialized hardware returns an invalid chip id/revision
508 	 * as well as 0xFFFFFFFF for Tx/Rx fifo length. It seems that
509 	 * unplugged cable results in putting hardware into automatic
510 	 * power down mode which in turn returns invalld chip revision.
511 	 */
512 	if (sc->age_chip_rev == 0xFFFF) {
513 		device_printf(dev,"invalid chip revision : 0x%04x -- "
514 		    "not initialized?\n", sc->age_chip_rev);
515 		error = ENXIO;
516 		goto fail;
517 	}
518 
519 	device_printf(dev, "%d Tx FIFO, %d Rx FIFO\n",
520 	    CSR_READ_4(sc, AGE_SRAM_TX_FIFO_LEN),
521 	    CSR_READ_4(sc, AGE_SRAM_RX_FIFO_LEN));
522 
523 	/* Allocate IRQ resources. */
524 	msixc = pci_msix_count(dev);
525 	msic = pci_msi_count(dev);
526 	if (bootverbose) {
527 		device_printf(dev, "MSIX count : %d\n", msixc);
528 		device_printf(dev, "MSI count : %d\n", msic);
529 	}
530 
531 	/* Prefer MSIX over MSI. */
532 	if (msix_disable == 0 || msi_disable == 0) {
533 		if (msix_disable == 0 && msixc == AGE_MSIX_MESSAGES &&
534 		    pci_alloc_msix(dev, &msixc) == 0) {
535 			if (msic == AGE_MSIX_MESSAGES) {
536 				device_printf(dev, "Using %d MSIX messages.\n",
537 				    msixc);
538 				sc->age_flags |= AGE_FLAG_MSIX;
539 				sc->age_irq_spec = age_irq_spec_msix;
540 			} else
541 				pci_release_msi(dev);
542 		}
543 		if (msi_disable == 0 && (sc->age_flags & AGE_FLAG_MSIX) == 0 &&
544 		    msic == AGE_MSI_MESSAGES &&
545 		    pci_alloc_msi(dev, &msic) == 0) {
546 			if (msic == AGE_MSI_MESSAGES) {
547 				device_printf(dev, "Using %d MSI messages.\n",
548 				    msic);
549 				sc->age_flags |= AGE_FLAG_MSI;
550 				sc->age_irq_spec = age_irq_spec_msi;
551 			} else
552 				pci_release_msi(dev);
553 		}
554 	}
555 
556 	error = bus_alloc_resources(dev, sc->age_irq_spec, sc->age_irq);
557 	if (error != 0) {
558 		device_printf(dev, "cannot allocate IRQ resources.\n");
559 		goto fail;
560 	}
561 
562 
563 	/* Get DMA parameters from PCIe device control register. */
564 	if (pci_find_cap(dev, PCIY_EXPRESS, &i) == 0) {
565 		sc->age_flags |= AGE_FLAG_PCIE;
566 		burst = pci_read_config(dev, i + 0x08, 2);
567 		/* Max read request size. */
568 		sc->age_dma_rd_burst = ((burst >> 12) & 0x07) <<
569 		    DMA_CFG_RD_BURST_SHIFT;
570 		/* Max payload size. */
571 		sc->age_dma_wr_burst = ((burst >> 5) & 0x07) <<
572 		    DMA_CFG_WR_BURST_SHIFT;
573 		if (bootverbose) {
574 			device_printf(dev, "Read request size : %d bytes.\n",
575 			    128 << ((burst >> 12) & 0x07));
576 			device_printf(dev, "TLP payload size : %d bytes.\n",
577 			    128 << ((burst >> 5) & 0x07));
578 		}
579 	} else {
580 		sc->age_dma_rd_burst = DMA_CFG_RD_BURST_128;
581 		sc->age_dma_wr_burst = DMA_CFG_WR_BURST_128;
582 	}
583 
584 	/* Create device sysctl node. */
585 	age_sysctl_node(sc);
586 
587 	if ((error = age_dma_alloc(sc) != 0))
588 		goto fail;
589 
590 	/* Load station address. */
591 	age_get_macaddr(sc);
592 
593 	ifp = sc->age_ifp = if_alloc(IFT_ETHER);
594 	if (ifp == NULL) {
595 		device_printf(dev, "cannot allocate ifnet structure.\n");
596 		error = ENXIO;
597 		goto fail;
598 	}
599 
600 	ifp->if_softc = sc;
601 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
602 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
603 	ifp->if_ioctl = age_ioctl;
604 	ifp->if_start = age_start;
605 	ifp->if_init = age_init;
606 	ifp->if_snd.ifq_drv_maxlen = AGE_TX_RING_CNT - 1;
607 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
608 	IFQ_SET_READY(&ifp->if_snd);
609 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
610 	ifp->if_hwassist = AGE_CSUM_FEATURES | CSUM_TSO;
611 	if (pci_find_cap(dev, PCIY_PMG, &pmc) == 0) {
612 		sc->age_flags |= AGE_FLAG_PMCAP;
613 		ifp->if_capabilities |= IFCAP_WOL_MAGIC | IFCAP_WOL_MCAST;
614 	}
615 	ifp->if_capenable = ifp->if_capabilities;
616 
617 	/* Set up MII bus. */
618 	error = mii_attach(dev, &sc->age_miibus, ifp, age_mediachange,
619 	    age_mediastatus, BMSR_DEFCAPMASK, sc->age_phyaddr, MII_OFFSET_ANY,
620 	    0);
621 	if (error != 0) {
622 		device_printf(dev, "attaching PHYs failed\n");
623 		goto fail;
624 	}
625 
626 	ether_ifattach(ifp, sc->age_eaddr);
627 
628 	/* VLAN capability setup. */
629 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
630 	    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO;
631 	ifp->if_capenable = ifp->if_capabilities;
632 
633 	/* Tell the upper layer(s) we support long frames. */
634 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
635 
636 	/* Create local taskq. */
637 	sc->age_tq = taskqueue_create_fast("age_taskq", M_WAITOK,
638 	    taskqueue_thread_enqueue, &sc->age_tq);
639 	if (sc->age_tq == NULL) {
640 		device_printf(dev, "could not create taskqueue.\n");
641 		ether_ifdetach(ifp);
642 		error = ENXIO;
643 		goto fail;
644 	}
645 	taskqueue_start_threads(&sc->age_tq, 1, PI_NET, "%s taskq",
646 	    device_get_nameunit(sc->age_dev));
647 
648 	if ((sc->age_flags & AGE_FLAG_MSIX) != 0)
649 		msic = AGE_MSIX_MESSAGES;
650 	else if ((sc->age_flags & AGE_FLAG_MSI) != 0)
651 		msic = AGE_MSI_MESSAGES;
652 	else
653 		msic = 1;
654 	for (i = 0; i < msic; i++) {
655 		error = bus_setup_intr(dev, sc->age_irq[i],
656 		    INTR_TYPE_NET | INTR_MPSAFE, age_intr, NULL, sc,
657 		    &sc->age_intrhand[i]);
658 		if (error != 0)
659 			break;
660 	}
661 	if (error != 0) {
662 		device_printf(dev, "could not set up interrupt handler.\n");
663 		taskqueue_free(sc->age_tq);
664 		sc->age_tq = NULL;
665 		ether_ifdetach(ifp);
666 		goto fail;
667 	}
668 
669 fail:
670 	if (error != 0)
671 		age_detach(dev);
672 
673 	return (error);
674 }
675 
676 static int
677 age_detach(device_t dev)
678 {
679 	struct age_softc *sc;
680 	struct ifnet *ifp;
681 	int i, msic;
682 
683 	sc = device_get_softc(dev);
684 
685 	ifp = sc->age_ifp;
686 	if (device_is_attached(dev)) {
687 		AGE_LOCK(sc);
688 		sc->age_flags |= AGE_FLAG_DETACH;
689 		age_stop(sc);
690 		AGE_UNLOCK(sc);
691 		callout_drain(&sc->age_tick_ch);
692 		taskqueue_drain(sc->age_tq, &sc->age_int_task);
693 		taskqueue_drain(taskqueue_swi, &sc->age_link_task);
694 		ether_ifdetach(ifp);
695 	}
696 
697 	if (sc->age_tq != NULL) {
698 		taskqueue_drain(sc->age_tq, &sc->age_int_task);
699 		taskqueue_free(sc->age_tq);
700 		sc->age_tq = NULL;
701 	}
702 
703 	if (sc->age_miibus != NULL) {
704 		device_delete_child(dev, sc->age_miibus);
705 		sc->age_miibus = NULL;
706 	}
707 	bus_generic_detach(dev);
708 	age_dma_free(sc);
709 
710 	if (ifp != NULL) {
711 		if_free(ifp);
712 		sc->age_ifp = NULL;
713 	}
714 
715 	if ((sc->age_flags & AGE_FLAG_MSIX) != 0)
716 		msic = AGE_MSIX_MESSAGES;
717 	else if ((sc->age_flags & AGE_FLAG_MSI) != 0)
718 		msic = AGE_MSI_MESSAGES;
719 	else
720 		msic = 1;
721 	for (i = 0; i < msic; i++) {
722 		if (sc->age_intrhand[i] != NULL) {
723 			bus_teardown_intr(dev, sc->age_irq[i],
724 			    sc->age_intrhand[i]);
725 			sc->age_intrhand[i] = NULL;
726 		}
727 	}
728 
729 	bus_release_resources(dev, sc->age_irq_spec, sc->age_irq);
730 	if ((sc->age_flags & (AGE_FLAG_MSI | AGE_FLAG_MSIX)) != 0)
731 		pci_release_msi(dev);
732 	bus_release_resources(dev, sc->age_res_spec, sc->age_res);
733 	mtx_destroy(&sc->age_mtx);
734 
735 	return (0);
736 }
737 
738 static void
739 age_sysctl_node(struct age_softc *sc)
740 {
741 	int error;
742 
743 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->age_dev),
744 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->age_dev)), OID_AUTO,
745 	    "stats", CTLTYPE_INT | CTLFLAG_RW, sc, 0, sysctl_age_stats,
746 	    "I", "Statistics");
747 
748 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->age_dev),
749 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->age_dev)), OID_AUTO,
750 	    "int_mod", CTLTYPE_INT | CTLFLAG_RW, &sc->age_int_mod, 0,
751 	    sysctl_hw_age_int_mod, "I", "age interrupt moderation");
752 
753 	/* Pull in device tunables. */
754 	sc->age_int_mod = AGE_IM_TIMER_DEFAULT;
755 	error = resource_int_value(device_get_name(sc->age_dev),
756 	    device_get_unit(sc->age_dev), "int_mod", &sc->age_int_mod);
757 	if (error == 0) {
758 		if (sc->age_int_mod < AGE_IM_TIMER_MIN ||
759 		    sc->age_int_mod > AGE_IM_TIMER_MAX) {
760 			device_printf(sc->age_dev,
761 			    "int_mod value out of range; using default: %d\n",
762 			    AGE_IM_TIMER_DEFAULT);
763 			sc->age_int_mod = AGE_IM_TIMER_DEFAULT;
764 		}
765 	}
766 
767 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(sc->age_dev),
768 	    SYSCTL_CHILDREN(device_get_sysctl_tree(sc->age_dev)), OID_AUTO,
769 	    "process_limit", CTLTYPE_INT | CTLFLAG_RW, &sc->age_process_limit,
770 	    0, sysctl_hw_age_proc_limit, "I",
771 	    "max number of Rx events to process");
772 
773 	/* Pull in device tunables. */
774 	sc->age_process_limit = AGE_PROC_DEFAULT;
775 	error = resource_int_value(device_get_name(sc->age_dev),
776 	    device_get_unit(sc->age_dev), "process_limit",
777 	    &sc->age_process_limit);
778 	if (error == 0) {
779 		if (sc->age_process_limit < AGE_PROC_MIN ||
780 		    sc->age_process_limit > AGE_PROC_MAX) {
781 			device_printf(sc->age_dev,
782 			    "process_limit value out of range; "
783 			    "using default: %d\n", AGE_PROC_DEFAULT);
784 			sc->age_process_limit = AGE_PROC_DEFAULT;
785 		}
786 	}
787 }
788 
789 struct age_dmamap_arg {
790 	bus_addr_t	age_busaddr;
791 };
792 
793 static void
794 age_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nsegs, int error)
795 {
796 	struct age_dmamap_arg *ctx;
797 
798 	if (error != 0)
799 		return;
800 
801 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
802 
803 	ctx = (struct age_dmamap_arg *)arg;
804 	ctx->age_busaddr = segs[0].ds_addr;
805 }
806 
807 /*
808  * Attansic L1 controller have single register to specify high
809  * address part of DMA blocks. So all descriptor structures and
810  * DMA memory blocks should have the same high address of given
811  * 4GB address space(i.e. crossing 4GB boundary is not allowed).
812  */
813 static int
814 age_check_boundary(struct age_softc *sc)
815 {
816 	bus_addr_t rx_ring_end, rr_ring_end, tx_ring_end;
817 	bus_addr_t cmb_block_end, smb_block_end;
818 
819 	/* Tx/Rx descriptor queue should reside within 4GB boundary. */
820 	tx_ring_end = sc->age_rdata.age_tx_ring_paddr + AGE_TX_RING_SZ;
821 	rx_ring_end = sc->age_rdata.age_rx_ring_paddr + AGE_RX_RING_SZ;
822 	rr_ring_end = sc->age_rdata.age_rr_ring_paddr + AGE_RR_RING_SZ;
823 	cmb_block_end = sc->age_rdata.age_cmb_block_paddr + AGE_CMB_BLOCK_SZ;
824 	smb_block_end = sc->age_rdata.age_smb_block_paddr + AGE_SMB_BLOCK_SZ;
825 
826 	if ((AGE_ADDR_HI(tx_ring_end) !=
827 	    AGE_ADDR_HI(sc->age_rdata.age_tx_ring_paddr)) ||
828 	    (AGE_ADDR_HI(rx_ring_end) !=
829 	    AGE_ADDR_HI(sc->age_rdata.age_rx_ring_paddr)) ||
830 	    (AGE_ADDR_HI(rr_ring_end) !=
831 	    AGE_ADDR_HI(sc->age_rdata.age_rr_ring_paddr)) ||
832 	    (AGE_ADDR_HI(cmb_block_end) !=
833 	    AGE_ADDR_HI(sc->age_rdata.age_cmb_block_paddr)) ||
834 	    (AGE_ADDR_HI(smb_block_end) !=
835 	    AGE_ADDR_HI(sc->age_rdata.age_smb_block_paddr)))
836 		return (EFBIG);
837 
838 	if ((AGE_ADDR_HI(tx_ring_end) != AGE_ADDR_HI(rx_ring_end)) ||
839 	    (AGE_ADDR_HI(tx_ring_end) != AGE_ADDR_HI(rr_ring_end)) ||
840 	    (AGE_ADDR_HI(tx_ring_end) != AGE_ADDR_HI(cmb_block_end)) ||
841 	    (AGE_ADDR_HI(tx_ring_end) != AGE_ADDR_HI(smb_block_end)))
842 		return (EFBIG);
843 
844 	return (0);
845 }
846 
847 static int
848 age_dma_alloc(struct age_softc *sc)
849 {
850 	struct age_txdesc *txd;
851 	struct age_rxdesc *rxd;
852 	bus_addr_t lowaddr;
853 	struct age_dmamap_arg ctx;
854 	int error, i;
855 
856 	lowaddr = BUS_SPACE_MAXADDR;
857 
858 again:
859 	/* Create parent ring/DMA block tag. */
860 	error = bus_dma_tag_create(
861 	    bus_get_dma_tag(sc->age_dev), /* parent */
862 	    1, 0,			/* alignment, boundary */
863 	    lowaddr,			/* lowaddr */
864 	    BUS_SPACE_MAXADDR,		/* highaddr */
865 	    NULL, NULL,			/* filter, filterarg */
866 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
867 	    0,				/* nsegments */
868 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
869 	    0,				/* flags */
870 	    NULL, NULL,			/* lockfunc, lockarg */
871 	    &sc->age_cdata.age_parent_tag);
872 	if (error != 0) {
873 		device_printf(sc->age_dev,
874 		    "could not create parent DMA tag.\n");
875 		goto fail;
876 	}
877 
878 	/* Create tag for Tx ring. */
879 	error = bus_dma_tag_create(
880 	    sc->age_cdata.age_parent_tag, /* parent */
881 	    AGE_TX_RING_ALIGN, 0,	/* alignment, boundary */
882 	    BUS_SPACE_MAXADDR,		/* lowaddr */
883 	    BUS_SPACE_MAXADDR,		/* highaddr */
884 	    NULL, NULL,			/* filter, filterarg */
885 	    AGE_TX_RING_SZ,		/* maxsize */
886 	    1,				/* nsegments */
887 	    AGE_TX_RING_SZ,		/* maxsegsize */
888 	    0,				/* flags */
889 	    NULL, NULL,			/* lockfunc, lockarg */
890 	    &sc->age_cdata.age_tx_ring_tag);
891 	if (error != 0) {
892 		device_printf(sc->age_dev,
893 		    "could not create Tx ring DMA tag.\n");
894 		goto fail;
895 	}
896 
897 	/* Create tag for Rx ring. */
898 	error = bus_dma_tag_create(
899 	    sc->age_cdata.age_parent_tag, /* parent */
900 	    AGE_RX_RING_ALIGN, 0,	/* alignment, boundary */
901 	    BUS_SPACE_MAXADDR,		/* lowaddr */
902 	    BUS_SPACE_MAXADDR,		/* highaddr */
903 	    NULL, NULL,			/* filter, filterarg */
904 	    AGE_RX_RING_SZ,		/* maxsize */
905 	    1,				/* nsegments */
906 	    AGE_RX_RING_SZ,		/* maxsegsize */
907 	    0,				/* flags */
908 	    NULL, NULL,			/* lockfunc, lockarg */
909 	    &sc->age_cdata.age_rx_ring_tag);
910 	if (error != 0) {
911 		device_printf(sc->age_dev,
912 		    "could not create Rx ring DMA tag.\n");
913 		goto fail;
914 	}
915 
916 	/* Create tag for Rx return ring. */
917 	error = bus_dma_tag_create(
918 	    sc->age_cdata.age_parent_tag, /* parent */
919 	    AGE_RR_RING_ALIGN, 0,	/* alignment, boundary */
920 	    BUS_SPACE_MAXADDR,		/* lowaddr */
921 	    BUS_SPACE_MAXADDR,		/* highaddr */
922 	    NULL, NULL,			/* filter, filterarg */
923 	    AGE_RR_RING_SZ,		/* maxsize */
924 	    1,				/* nsegments */
925 	    AGE_RR_RING_SZ,		/* maxsegsize */
926 	    0,				/* flags */
927 	    NULL, NULL,			/* lockfunc, lockarg */
928 	    &sc->age_cdata.age_rr_ring_tag);
929 	if (error != 0) {
930 		device_printf(sc->age_dev,
931 		    "could not create Rx return ring DMA tag.\n");
932 		goto fail;
933 	}
934 
935 	/* Create tag for coalesing message block. */
936 	error = bus_dma_tag_create(
937 	    sc->age_cdata.age_parent_tag, /* parent */
938 	    AGE_CMB_ALIGN, 0,		/* alignment, boundary */
939 	    BUS_SPACE_MAXADDR,		/* lowaddr */
940 	    BUS_SPACE_MAXADDR,		/* highaddr */
941 	    NULL, NULL,			/* filter, filterarg */
942 	    AGE_CMB_BLOCK_SZ,		/* maxsize */
943 	    1,				/* nsegments */
944 	    AGE_CMB_BLOCK_SZ,		/* maxsegsize */
945 	    0,				/* flags */
946 	    NULL, NULL,			/* lockfunc, lockarg */
947 	    &sc->age_cdata.age_cmb_block_tag);
948 	if (error != 0) {
949 		device_printf(sc->age_dev,
950 		    "could not create CMB DMA tag.\n");
951 		goto fail;
952 	}
953 
954 	/* Create tag for statistics message block. */
955 	error = bus_dma_tag_create(
956 	    sc->age_cdata.age_parent_tag, /* parent */
957 	    AGE_SMB_ALIGN, 0,		/* alignment, boundary */
958 	    BUS_SPACE_MAXADDR,		/* lowaddr */
959 	    BUS_SPACE_MAXADDR,		/* highaddr */
960 	    NULL, NULL,			/* filter, filterarg */
961 	    AGE_SMB_BLOCK_SZ,		/* maxsize */
962 	    1,				/* nsegments */
963 	    AGE_SMB_BLOCK_SZ,		/* maxsegsize */
964 	    0,				/* flags */
965 	    NULL, NULL,			/* lockfunc, lockarg */
966 	    &sc->age_cdata.age_smb_block_tag);
967 	if (error != 0) {
968 		device_printf(sc->age_dev,
969 		    "could not create SMB DMA tag.\n");
970 		goto fail;
971 	}
972 
973 	/* Allocate DMA'able memory and load the DMA map. */
974 	error = bus_dmamem_alloc(sc->age_cdata.age_tx_ring_tag,
975 	    (void **)&sc->age_rdata.age_tx_ring,
976 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
977 	    &sc->age_cdata.age_tx_ring_map);
978 	if (error != 0) {
979 		device_printf(sc->age_dev,
980 		    "could not allocate DMA'able memory for Tx ring.\n");
981 		goto fail;
982 	}
983 	ctx.age_busaddr = 0;
984 	error = bus_dmamap_load(sc->age_cdata.age_tx_ring_tag,
985 	    sc->age_cdata.age_tx_ring_map, sc->age_rdata.age_tx_ring,
986 	    AGE_TX_RING_SZ, age_dmamap_cb, &ctx, 0);
987 	if (error != 0 || ctx.age_busaddr == 0) {
988 		device_printf(sc->age_dev,
989 		    "could not load DMA'able memory for Tx ring.\n");
990 		goto fail;
991 	}
992 	sc->age_rdata.age_tx_ring_paddr = ctx.age_busaddr;
993 	/* Rx ring */
994 	error = bus_dmamem_alloc(sc->age_cdata.age_rx_ring_tag,
995 	    (void **)&sc->age_rdata.age_rx_ring,
996 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
997 	    &sc->age_cdata.age_rx_ring_map);
998 	if (error != 0) {
999 		device_printf(sc->age_dev,
1000 		    "could not allocate DMA'able memory for Rx ring.\n");
1001 		goto fail;
1002 	}
1003 	ctx.age_busaddr = 0;
1004 	error = bus_dmamap_load(sc->age_cdata.age_rx_ring_tag,
1005 	    sc->age_cdata.age_rx_ring_map, sc->age_rdata.age_rx_ring,
1006 	    AGE_RX_RING_SZ, age_dmamap_cb, &ctx, 0);
1007 	if (error != 0 || ctx.age_busaddr == 0) {
1008 		device_printf(sc->age_dev,
1009 		    "could not load DMA'able memory for Rx ring.\n");
1010 		goto fail;
1011 	}
1012 	sc->age_rdata.age_rx_ring_paddr = ctx.age_busaddr;
1013 	/* Rx return ring */
1014 	error = bus_dmamem_alloc(sc->age_cdata.age_rr_ring_tag,
1015 	    (void **)&sc->age_rdata.age_rr_ring,
1016 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1017 	    &sc->age_cdata.age_rr_ring_map);
1018 	if (error != 0) {
1019 		device_printf(sc->age_dev,
1020 		    "could not allocate DMA'able memory for Rx return ring.\n");
1021 		goto fail;
1022 	}
1023 	ctx.age_busaddr = 0;
1024 	error = bus_dmamap_load(sc->age_cdata.age_rr_ring_tag,
1025 	    sc->age_cdata.age_rr_ring_map, sc->age_rdata.age_rr_ring,
1026 	    AGE_RR_RING_SZ, age_dmamap_cb,
1027 	    &ctx, 0);
1028 	if (error != 0 || ctx.age_busaddr == 0) {
1029 		device_printf(sc->age_dev,
1030 		    "could not load DMA'able memory for Rx return ring.\n");
1031 		goto fail;
1032 	}
1033 	sc->age_rdata.age_rr_ring_paddr = ctx.age_busaddr;
1034 	/* CMB block */
1035 	error = bus_dmamem_alloc(sc->age_cdata.age_cmb_block_tag,
1036 	    (void **)&sc->age_rdata.age_cmb_block,
1037 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1038 	    &sc->age_cdata.age_cmb_block_map);
1039 	if (error != 0) {
1040 		device_printf(sc->age_dev,
1041 		    "could not allocate DMA'able memory for CMB block.\n");
1042 		goto fail;
1043 	}
1044 	ctx.age_busaddr = 0;
1045 	error = bus_dmamap_load(sc->age_cdata.age_cmb_block_tag,
1046 	    sc->age_cdata.age_cmb_block_map, sc->age_rdata.age_cmb_block,
1047 	    AGE_CMB_BLOCK_SZ, age_dmamap_cb, &ctx, 0);
1048 	if (error != 0 || ctx.age_busaddr == 0) {
1049 		device_printf(sc->age_dev,
1050 		    "could not load DMA'able memory for CMB block.\n");
1051 		goto fail;
1052 	}
1053 	sc->age_rdata.age_cmb_block_paddr = ctx.age_busaddr;
1054 	/* SMB block */
1055 	error = bus_dmamem_alloc(sc->age_cdata.age_smb_block_tag,
1056 	    (void **)&sc->age_rdata.age_smb_block,
1057 	    BUS_DMA_WAITOK | BUS_DMA_ZERO | BUS_DMA_COHERENT,
1058 	    &sc->age_cdata.age_smb_block_map);
1059 	if (error != 0) {
1060 		device_printf(sc->age_dev,
1061 		    "could not allocate DMA'able memory for SMB block.\n");
1062 		goto fail;
1063 	}
1064 	ctx.age_busaddr = 0;
1065 	error = bus_dmamap_load(sc->age_cdata.age_smb_block_tag,
1066 	    sc->age_cdata.age_smb_block_map, sc->age_rdata.age_smb_block,
1067 	    AGE_SMB_BLOCK_SZ, age_dmamap_cb, &ctx, 0);
1068 	if (error != 0 || ctx.age_busaddr == 0) {
1069 		device_printf(sc->age_dev,
1070 		    "could not load DMA'able memory for SMB block.\n");
1071 		goto fail;
1072 	}
1073 	sc->age_rdata.age_smb_block_paddr = ctx.age_busaddr;
1074 
1075 	/*
1076 	 * All ring buffer and DMA blocks should have the same
1077 	 * high address part of 64bit DMA address space.
1078 	 */
1079 	if (lowaddr != BUS_SPACE_MAXADDR_32BIT &&
1080 	    (error = age_check_boundary(sc)) != 0) {
1081 		device_printf(sc->age_dev, "4GB boundary crossed, "
1082 		    "switching to 32bit DMA addressing mode.\n");
1083 		age_dma_free(sc);
1084 		/* Limit DMA address space to 32bit and try again. */
1085 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
1086 		goto again;
1087 	}
1088 
1089 	/*
1090 	 * Create Tx/Rx buffer parent tag.
1091 	 * L1 supports full 64bit DMA addressing in Tx/Rx buffers
1092 	 * so it needs separate parent DMA tag.
1093 	 * XXX
1094 	 * It seems enabling 64bit DMA causes data corruption. Limit
1095 	 * DMA address space to 32bit.
1096 	 */
1097 	error = bus_dma_tag_create(
1098 	    bus_get_dma_tag(sc->age_dev), /* parent */
1099 	    1, 0,			/* alignment, boundary */
1100 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1101 	    BUS_SPACE_MAXADDR,		/* highaddr */
1102 	    NULL, NULL,			/* filter, filterarg */
1103 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1104 	    0,				/* nsegments */
1105 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1106 	    0,				/* flags */
1107 	    NULL, NULL,			/* lockfunc, lockarg */
1108 	    &sc->age_cdata.age_buffer_tag);
1109 	if (error != 0) {
1110 		device_printf(sc->age_dev,
1111 		    "could not create parent buffer DMA tag.\n");
1112 		goto fail;
1113 	}
1114 
1115 	/* Create tag for Tx buffers. */
1116 	error = bus_dma_tag_create(
1117 	    sc->age_cdata.age_buffer_tag, /* parent */
1118 	    1, 0,			/* alignment, boundary */
1119 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1120 	    BUS_SPACE_MAXADDR,		/* highaddr */
1121 	    NULL, NULL,			/* filter, filterarg */
1122 	    AGE_TSO_MAXSIZE,		/* maxsize */
1123 	    AGE_MAXTXSEGS,		/* nsegments */
1124 	    AGE_TSO_MAXSEGSIZE,		/* maxsegsize */
1125 	    0,				/* flags */
1126 	    NULL, NULL,			/* lockfunc, lockarg */
1127 	    &sc->age_cdata.age_tx_tag);
1128 	if (error != 0) {
1129 		device_printf(sc->age_dev, "could not create Tx DMA tag.\n");
1130 		goto fail;
1131 	}
1132 
1133 	/* Create tag for Rx buffers. */
1134 	error = bus_dma_tag_create(
1135 	    sc->age_cdata.age_buffer_tag, /* parent */
1136 	    1, 0,			/* alignment, boundary */
1137 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1138 	    BUS_SPACE_MAXADDR,		/* highaddr */
1139 	    NULL, NULL,			/* filter, filterarg */
1140 	    MCLBYTES,			/* maxsize */
1141 	    1,				/* nsegments */
1142 	    MCLBYTES,			/* maxsegsize */
1143 	    0,				/* flags */
1144 	    NULL, NULL,			/* lockfunc, lockarg */
1145 	    &sc->age_cdata.age_rx_tag);
1146 	if (error != 0) {
1147 		device_printf(sc->age_dev, "could not create Rx DMA tag.\n");
1148 		goto fail;
1149 	}
1150 
1151 	/* Create DMA maps for Tx buffers. */
1152 	for (i = 0; i < AGE_TX_RING_CNT; i++) {
1153 		txd = &sc->age_cdata.age_txdesc[i];
1154 		txd->tx_m = NULL;
1155 		txd->tx_dmamap = NULL;
1156 		error = bus_dmamap_create(sc->age_cdata.age_tx_tag, 0,
1157 		    &txd->tx_dmamap);
1158 		if (error != 0) {
1159 			device_printf(sc->age_dev,
1160 			    "could not create Tx dmamap.\n");
1161 			goto fail;
1162 		}
1163 	}
1164 	/* Create DMA maps for Rx buffers. */
1165 	if ((error = bus_dmamap_create(sc->age_cdata.age_rx_tag, 0,
1166 	    &sc->age_cdata.age_rx_sparemap)) != 0) {
1167 		device_printf(sc->age_dev,
1168 		    "could not create spare Rx dmamap.\n");
1169 		goto fail;
1170 	}
1171 	for (i = 0; i < AGE_RX_RING_CNT; i++) {
1172 		rxd = &sc->age_cdata.age_rxdesc[i];
1173 		rxd->rx_m = NULL;
1174 		rxd->rx_dmamap = NULL;
1175 		error = bus_dmamap_create(sc->age_cdata.age_rx_tag, 0,
1176 		    &rxd->rx_dmamap);
1177 		if (error != 0) {
1178 			device_printf(sc->age_dev,
1179 			    "could not create Rx dmamap.\n");
1180 			goto fail;
1181 		}
1182 	}
1183 
1184 fail:
1185 	return (error);
1186 }
1187 
1188 static void
1189 age_dma_free(struct age_softc *sc)
1190 {
1191 	struct age_txdesc *txd;
1192 	struct age_rxdesc *rxd;
1193 	int i;
1194 
1195 	/* Tx buffers */
1196 	if (sc->age_cdata.age_tx_tag != NULL) {
1197 		for (i = 0; i < AGE_TX_RING_CNT; i++) {
1198 			txd = &sc->age_cdata.age_txdesc[i];
1199 			if (txd->tx_dmamap != NULL) {
1200 				bus_dmamap_destroy(sc->age_cdata.age_tx_tag,
1201 				    txd->tx_dmamap);
1202 				txd->tx_dmamap = NULL;
1203 			}
1204 		}
1205 		bus_dma_tag_destroy(sc->age_cdata.age_tx_tag);
1206 		sc->age_cdata.age_tx_tag = NULL;
1207 	}
1208 	/* Rx buffers */
1209 	if (sc->age_cdata.age_rx_tag != NULL) {
1210 		for (i = 0; i < AGE_RX_RING_CNT; i++) {
1211 			rxd = &sc->age_cdata.age_rxdesc[i];
1212 			if (rxd->rx_dmamap != NULL) {
1213 				bus_dmamap_destroy(sc->age_cdata.age_rx_tag,
1214 				    rxd->rx_dmamap);
1215 				rxd->rx_dmamap = NULL;
1216 			}
1217 		}
1218 		if (sc->age_cdata.age_rx_sparemap != NULL) {
1219 			bus_dmamap_destroy(sc->age_cdata.age_rx_tag,
1220 			    sc->age_cdata.age_rx_sparemap);
1221 			sc->age_cdata.age_rx_sparemap = NULL;
1222 		}
1223 		bus_dma_tag_destroy(sc->age_cdata.age_rx_tag);
1224 		sc->age_cdata.age_rx_tag = NULL;
1225 	}
1226 	/* Tx ring. */
1227 	if (sc->age_cdata.age_tx_ring_tag != NULL) {
1228 		if (sc->age_cdata.age_tx_ring_map != NULL)
1229 			bus_dmamap_unload(sc->age_cdata.age_tx_ring_tag,
1230 			    sc->age_cdata.age_tx_ring_map);
1231 		if (sc->age_cdata.age_tx_ring_map != NULL &&
1232 		    sc->age_rdata.age_tx_ring != NULL)
1233 			bus_dmamem_free(sc->age_cdata.age_tx_ring_tag,
1234 			    sc->age_rdata.age_tx_ring,
1235 			    sc->age_cdata.age_tx_ring_map);
1236 		sc->age_rdata.age_tx_ring = NULL;
1237 		sc->age_cdata.age_tx_ring_map = NULL;
1238 		bus_dma_tag_destroy(sc->age_cdata.age_tx_ring_tag);
1239 		sc->age_cdata.age_tx_ring_tag = NULL;
1240 	}
1241 	/* Rx ring. */
1242 	if (sc->age_cdata.age_rx_ring_tag != NULL) {
1243 		if (sc->age_cdata.age_rx_ring_map != NULL)
1244 			bus_dmamap_unload(sc->age_cdata.age_rx_ring_tag,
1245 			    sc->age_cdata.age_rx_ring_map);
1246 		if (sc->age_cdata.age_rx_ring_map != NULL &&
1247 		    sc->age_rdata.age_rx_ring != NULL)
1248 			bus_dmamem_free(sc->age_cdata.age_rx_ring_tag,
1249 			    sc->age_rdata.age_rx_ring,
1250 			    sc->age_cdata.age_rx_ring_map);
1251 		sc->age_rdata.age_rx_ring = NULL;
1252 		sc->age_cdata.age_rx_ring_map = NULL;
1253 		bus_dma_tag_destroy(sc->age_cdata.age_rx_ring_tag);
1254 		sc->age_cdata.age_rx_ring_tag = NULL;
1255 	}
1256 	/* Rx return ring. */
1257 	if (sc->age_cdata.age_rr_ring_tag != NULL) {
1258 		if (sc->age_cdata.age_rr_ring_map != NULL)
1259 			bus_dmamap_unload(sc->age_cdata.age_rr_ring_tag,
1260 			    sc->age_cdata.age_rr_ring_map);
1261 		if (sc->age_cdata.age_rr_ring_map != NULL &&
1262 		    sc->age_rdata.age_rr_ring != NULL)
1263 			bus_dmamem_free(sc->age_cdata.age_rr_ring_tag,
1264 			    sc->age_rdata.age_rr_ring,
1265 			    sc->age_cdata.age_rr_ring_map);
1266 		sc->age_rdata.age_rr_ring = NULL;
1267 		sc->age_cdata.age_rr_ring_map = NULL;
1268 		bus_dma_tag_destroy(sc->age_cdata.age_rr_ring_tag);
1269 		sc->age_cdata.age_rr_ring_tag = NULL;
1270 	}
1271 	/* CMB block */
1272 	if (sc->age_cdata.age_cmb_block_tag != NULL) {
1273 		if (sc->age_cdata.age_cmb_block_map != NULL)
1274 			bus_dmamap_unload(sc->age_cdata.age_cmb_block_tag,
1275 			    sc->age_cdata.age_cmb_block_map);
1276 		if (sc->age_cdata.age_cmb_block_map != NULL &&
1277 		    sc->age_rdata.age_cmb_block != NULL)
1278 			bus_dmamem_free(sc->age_cdata.age_cmb_block_tag,
1279 			    sc->age_rdata.age_cmb_block,
1280 			    sc->age_cdata.age_cmb_block_map);
1281 		sc->age_rdata.age_cmb_block = NULL;
1282 		sc->age_cdata.age_cmb_block_map = NULL;
1283 		bus_dma_tag_destroy(sc->age_cdata.age_cmb_block_tag);
1284 		sc->age_cdata.age_cmb_block_tag = NULL;
1285 	}
1286 	/* SMB block */
1287 	if (sc->age_cdata.age_smb_block_tag != NULL) {
1288 		if (sc->age_cdata.age_smb_block_map != NULL)
1289 			bus_dmamap_unload(sc->age_cdata.age_smb_block_tag,
1290 			    sc->age_cdata.age_smb_block_map);
1291 		if (sc->age_cdata.age_smb_block_map != NULL &&
1292 		    sc->age_rdata.age_smb_block != NULL)
1293 			bus_dmamem_free(sc->age_cdata.age_smb_block_tag,
1294 			    sc->age_rdata.age_smb_block,
1295 			    sc->age_cdata.age_smb_block_map);
1296 		sc->age_rdata.age_smb_block = NULL;
1297 		sc->age_cdata.age_smb_block_map = NULL;
1298 		bus_dma_tag_destroy(sc->age_cdata.age_smb_block_tag);
1299 		sc->age_cdata.age_smb_block_tag = NULL;
1300 	}
1301 
1302 	if (sc->age_cdata.age_buffer_tag != NULL) {
1303 		bus_dma_tag_destroy(sc->age_cdata.age_buffer_tag);
1304 		sc->age_cdata.age_buffer_tag = NULL;
1305 	}
1306 	if (sc->age_cdata.age_parent_tag != NULL) {
1307 		bus_dma_tag_destroy(sc->age_cdata.age_parent_tag);
1308 		sc->age_cdata.age_parent_tag = NULL;
1309 	}
1310 }
1311 
1312 /*
1313  *	Make sure the interface is stopped at reboot time.
1314  */
1315 static int
1316 age_shutdown(device_t dev)
1317 {
1318 
1319 	return (age_suspend(dev));
1320 }
1321 
1322 static void
1323 age_setwol(struct age_softc *sc)
1324 {
1325 	struct ifnet *ifp;
1326 	struct mii_data *mii;
1327 	uint32_t reg, pmcs;
1328 	uint16_t pmstat;
1329 	int aneg, i, pmc;
1330 
1331 	AGE_LOCK_ASSERT(sc);
1332 
1333 	if (pci_find_cap(sc->age_dev, PCIY_PMG, &pmc) != 0) {
1334 		CSR_WRITE_4(sc, AGE_WOL_CFG, 0);
1335 		/*
1336 		 * No PME capability, PHY power down.
1337 		 * XXX
1338 		 * Due to an unknown reason powering down PHY resulted
1339 		 * in unexpected results such as inaccessbility of
1340 		 * hardware of freshly rebooted system. Disable
1341 		 * powering down PHY until I got more information for
1342 		 * Attansic/Atheros PHY hardwares.
1343 		 */
1344 #ifdef notyet
1345 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
1346 		    MII_BMCR, BMCR_PDOWN);
1347 #endif
1348 		return;
1349 	}
1350 
1351 	ifp = sc->age_ifp;
1352 	if ((ifp->if_capenable & IFCAP_WOL) != 0) {
1353 		/*
1354 		 * Note, this driver resets the link speed to 10/100Mbps with
1355 		 * auto-negotiation but we don't know whether that operation
1356 		 * would succeed or not as it have no control after powering
1357 		 * off. If the renegotiation fail WOL may not work. Running
1358 		 * at 1Gbps will draw more power than 375mA at 3.3V which is
1359 		 * specified in PCI specification and that would result in
1360 		 * complete shutdowning power to ethernet controller.
1361 		 *
1362 		 * TODO
1363 		 *  Save current negotiated media speed/duplex/flow-control
1364 		 *  to softc and restore the same link again after resuming.
1365 		 *  PHY handling such as power down/resetting to 100Mbps
1366 		 *  may be better handled in suspend method in phy driver.
1367 		 */
1368 		mii = device_get_softc(sc->age_miibus);
1369 		mii_pollstat(mii);
1370 		aneg = 0;
1371 		if ((mii->mii_media_status & IFM_AVALID) != 0) {
1372 			switch IFM_SUBTYPE(mii->mii_media_active) {
1373 			case IFM_10_T:
1374 			case IFM_100_TX:
1375 				goto got_link;
1376 			case IFM_1000_T:
1377 				aneg++;
1378 			default:
1379 				break;
1380 			}
1381 		}
1382 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
1383 		    MII_100T2CR, 0);
1384 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
1385 		    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD |
1386 		    ANAR_10 | ANAR_CSMA);
1387 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
1388 		    MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
1389 		DELAY(1000);
1390 		if (aneg != 0) {
1391 			/* Poll link state until age(4) get a 10/100 link. */
1392 			for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
1393 				mii_pollstat(mii);
1394 				if ((mii->mii_media_status & IFM_AVALID) != 0) {
1395 					switch (IFM_SUBTYPE(
1396 					    mii->mii_media_active)) {
1397 					case IFM_10_T:
1398 					case IFM_100_TX:
1399 						age_mac_config(sc);
1400 						goto got_link;
1401 					default:
1402 						break;
1403 					}
1404 				}
1405 				AGE_UNLOCK(sc);
1406 				pause("agelnk", hz);
1407 				AGE_LOCK(sc);
1408 			}
1409 			if (i == MII_ANEGTICKS_GIGE)
1410 				device_printf(sc->age_dev,
1411 				    "establishing link failed, "
1412 				    "WOL may not work!");
1413 		}
1414 		/*
1415 		 * No link, force MAC to have 100Mbps, full-duplex link.
1416 		 * This is the last resort and may/may not work.
1417 		 */
1418 		mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
1419 		mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
1420 		age_mac_config(sc);
1421 	}
1422 
1423 got_link:
1424 	pmcs = 0;
1425 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
1426 		pmcs |= WOL_CFG_MAGIC | WOL_CFG_MAGIC_ENB;
1427 	CSR_WRITE_4(sc, AGE_WOL_CFG, pmcs);
1428 	reg = CSR_READ_4(sc, AGE_MAC_CFG);
1429 	reg &= ~(MAC_CFG_DBG | MAC_CFG_PROMISC);
1430 	reg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST);
1431 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
1432 		reg |= MAC_CFG_ALLMULTI | MAC_CFG_BCAST;
1433 	if ((ifp->if_capenable & IFCAP_WOL) != 0) {
1434 		reg |= MAC_CFG_RX_ENB;
1435 		CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
1436 	}
1437 
1438 	/* Request PME. */
1439 	pmstat = pci_read_config(sc->age_dev, pmc + PCIR_POWER_STATUS, 2);
1440 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
1441 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
1442 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
1443 	pci_write_config(sc->age_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
1444 #ifdef notyet
1445 	/* See above for powering down PHY issues. */
1446 	if ((ifp->if_capenable & IFCAP_WOL) == 0) {
1447 		/* No WOL, PHY power down. */
1448 		age_miibus_writereg(sc->age_dev, sc->age_phyaddr,
1449 		    MII_BMCR, BMCR_PDOWN);
1450 	}
1451 #endif
1452 }
1453 
1454 static int
1455 age_suspend(device_t dev)
1456 {
1457 	struct age_softc *sc;
1458 
1459 	sc = device_get_softc(dev);
1460 
1461 	AGE_LOCK(sc);
1462 	age_stop(sc);
1463 	age_setwol(sc);
1464 	AGE_UNLOCK(sc);
1465 
1466 	return (0);
1467 }
1468 
1469 static int
1470 age_resume(device_t dev)
1471 {
1472 	struct age_softc *sc;
1473 	struct ifnet *ifp;
1474 
1475 	sc = device_get_softc(dev);
1476 
1477 	AGE_LOCK(sc);
1478 	age_phy_reset(sc);
1479 	ifp = sc->age_ifp;
1480 	if ((ifp->if_flags & IFF_UP) != 0)
1481 		age_init_locked(sc);
1482 
1483 	AGE_UNLOCK(sc);
1484 
1485 	return (0);
1486 }
1487 
1488 static int
1489 age_encap(struct age_softc *sc, struct mbuf **m_head)
1490 {
1491 	struct age_txdesc *txd, *txd_last;
1492 	struct tx_desc *desc;
1493 	struct mbuf *m;
1494 	struct ip *ip;
1495 	struct tcphdr *tcp;
1496 	bus_dma_segment_t txsegs[AGE_MAXTXSEGS];
1497 	bus_dmamap_t map;
1498 	uint32_t cflags, hdrlen, ip_off, poff, vtag;
1499 	int error, i, nsegs, prod, si;
1500 
1501 	AGE_LOCK_ASSERT(sc);
1502 
1503 	M_ASSERTPKTHDR((*m_head));
1504 
1505 	m = *m_head;
1506 	ip = NULL;
1507 	tcp = NULL;
1508 	cflags = vtag = 0;
1509 	ip_off = poff = 0;
1510 	if ((m->m_pkthdr.csum_flags & (AGE_CSUM_FEATURES | CSUM_TSO)) != 0) {
1511 		/*
1512 		 * L1 requires offset of TCP/UDP payload in its Tx
1513 		 * descriptor to perform hardware Tx checksum offload.
1514 		 * Additionally, TSO requires IP/TCP header size and
1515 		 * modification of IP/TCP header in order to make TSO
1516 		 * engine work. This kind of operation takes many CPU
1517 		 * cycles on FreeBSD so fast host CPU is needed to get
1518 		 * smooth TSO performance.
1519 		 */
1520 		struct ether_header *eh;
1521 
1522 		if (M_WRITABLE(m) == 0) {
1523 			/* Get a writable copy. */
1524 			m = m_dup(*m_head, M_NOWAIT);
1525 			/* Release original mbufs. */
1526 			m_freem(*m_head);
1527 			if (m == NULL) {
1528 				*m_head = NULL;
1529 				return (ENOBUFS);
1530 			}
1531 			*m_head = m;
1532 		}
1533 		ip_off = sizeof(struct ether_header);
1534 		m = m_pullup(m, ip_off);
1535 		if (m == NULL) {
1536 			*m_head = NULL;
1537 			return (ENOBUFS);
1538 		}
1539 		eh = mtod(m, struct ether_header *);
1540 		/*
1541 		 * Check if hardware VLAN insertion is off.
1542 		 * Additional check for LLC/SNAP frame?
1543 		 */
1544 		if (eh->ether_type == htons(ETHERTYPE_VLAN)) {
1545 			ip_off = sizeof(struct ether_vlan_header);
1546 			m = m_pullup(m, ip_off);
1547 			if (m == NULL) {
1548 				*m_head = NULL;
1549 				return (ENOBUFS);
1550 			}
1551 		}
1552 		m = m_pullup(m, ip_off + sizeof(struct ip));
1553 		if (m == NULL) {
1554 			*m_head = NULL;
1555 			return (ENOBUFS);
1556 		}
1557 		ip = (struct ip *)(mtod(m, char *) + ip_off);
1558 		poff = ip_off + (ip->ip_hl << 2);
1559 		if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
1560 			m = m_pullup(m, poff + sizeof(struct tcphdr));
1561 			if (m == NULL) {
1562 				*m_head = NULL;
1563 				return (ENOBUFS);
1564 			}
1565 			tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1566 			m = m_pullup(m, poff + (tcp->th_off << 2));
1567 			if (m == NULL) {
1568 				*m_head = NULL;
1569 				return (ENOBUFS);
1570 			}
1571 			/*
1572 			 * L1 requires IP/TCP header size and offset as
1573 			 * well as TCP pseudo checksum which complicates
1574 			 * TSO configuration. I guess this comes from the
1575 			 * adherence to Microsoft NDIS Large Send
1576 			 * specification which requires insertion of
1577 			 * pseudo checksum by upper stack. The pseudo
1578 			 * checksum that NDIS refers to doesn't include
1579 			 * TCP payload length so age(4) should recompute
1580 			 * the pseudo checksum here. Hopefully this wouldn't
1581 			 * be much burden on modern CPUs.
1582 			 * Reset IP checksum and recompute TCP pseudo
1583 			 * checksum as NDIS specification said.
1584 			 */
1585 			ip = (struct ip *)(mtod(m, char *) + ip_off);
1586 			tcp = (struct tcphdr *)(mtod(m, char *) + poff);
1587 			ip->ip_sum = 0;
1588 			tcp->th_sum = in_pseudo(ip->ip_src.s_addr,
1589 			    ip->ip_dst.s_addr, htons(IPPROTO_TCP));
1590 		}
1591 		*m_head = m;
1592 	}
1593 
1594 	si = prod = sc->age_cdata.age_tx_prod;
1595 	txd = &sc->age_cdata.age_txdesc[prod];
1596 	txd_last = txd;
1597 	map = txd->tx_dmamap;
1598 
1599 	error =  bus_dmamap_load_mbuf_sg(sc->age_cdata.age_tx_tag, map,
1600 	    *m_head, txsegs, &nsegs, 0);
1601 	if (error == EFBIG) {
1602 		m = m_collapse(*m_head, M_NOWAIT, AGE_MAXTXSEGS);
1603 		if (m == NULL) {
1604 			m_freem(*m_head);
1605 			*m_head = NULL;
1606 			return (ENOMEM);
1607 		}
1608 		*m_head = m;
1609 		error = bus_dmamap_load_mbuf_sg(sc->age_cdata.age_tx_tag, map,
1610 		    *m_head, txsegs, &nsegs, 0);
1611 		if (error != 0) {
1612 			m_freem(*m_head);
1613 			*m_head = NULL;
1614 			return (error);
1615 		}
1616 	} else if (error != 0)
1617 		return (error);
1618 	if (nsegs == 0) {
1619 		m_freem(*m_head);
1620 		*m_head = NULL;
1621 		return (EIO);
1622 	}
1623 
1624 	/* Check descriptor overrun. */
1625 	if (sc->age_cdata.age_tx_cnt + nsegs >= AGE_TX_RING_CNT - 2) {
1626 		bus_dmamap_unload(sc->age_cdata.age_tx_tag, map);
1627 		return (ENOBUFS);
1628 	}
1629 
1630 	m = *m_head;
1631 	/* Configure VLAN hardware tag insertion. */
1632 	if ((m->m_flags & M_VLANTAG) != 0) {
1633 		vtag = AGE_TX_VLAN_TAG(m->m_pkthdr.ether_vtag);
1634 		vtag = ((vtag << AGE_TD_VLAN_SHIFT) & AGE_TD_VLAN_MASK);
1635 		cflags |= AGE_TD_INSERT_VLAN_TAG;
1636 	}
1637 
1638 	desc = NULL;
1639 	i = 0;
1640 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
1641 		/* Request TSO and set MSS. */
1642 		cflags |= AGE_TD_TSO_IPV4;
1643 		cflags |= AGE_TD_IPCSUM | AGE_TD_TCPCSUM;
1644 		cflags |= ((uint32_t)m->m_pkthdr.tso_segsz <<
1645 		    AGE_TD_TSO_MSS_SHIFT);
1646 		/* Set IP/TCP header size. */
1647 		cflags |= ip->ip_hl << AGE_TD_IPHDR_LEN_SHIFT;
1648 		cflags |= tcp->th_off << AGE_TD_TSO_TCPHDR_LEN_SHIFT;
1649 		/*
1650 		 * L1 requires the first buffer should only hold IP/TCP
1651 		 * header data. TCP payload should be handled in other
1652 		 * descriptors.
1653 		 */
1654 		hdrlen = poff + (tcp->th_off << 2);
1655 		desc = &sc->age_rdata.age_tx_ring[prod];
1656 		desc->addr = htole64(txsegs[0].ds_addr);
1657 		desc->len = htole32(AGE_TX_BYTES(hdrlen) | vtag);
1658 		desc->flags = htole32(cflags);
1659 		sc->age_cdata.age_tx_cnt++;
1660 		AGE_DESC_INC(prod, AGE_TX_RING_CNT);
1661 		if (m->m_len - hdrlen > 0) {
1662 			/* Handle remaining payload of the 1st fragment. */
1663 			desc = &sc->age_rdata.age_tx_ring[prod];
1664 			desc->addr = htole64(txsegs[0].ds_addr + hdrlen);
1665 			desc->len = htole32(AGE_TX_BYTES(m->m_len - hdrlen) |
1666 			    vtag);
1667 			desc->flags = htole32(cflags);
1668 			sc->age_cdata.age_tx_cnt++;
1669 			AGE_DESC_INC(prod, AGE_TX_RING_CNT);
1670 		}
1671 		/* Handle remaining fragments. */
1672 		i = 1;
1673 	} else if ((m->m_pkthdr.csum_flags & AGE_CSUM_FEATURES) != 0) {
1674 		/* Configure Tx IP/TCP/UDP checksum offload. */
1675 		cflags |= AGE_TD_CSUM;
1676 		if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
1677 			cflags |= AGE_TD_TCPCSUM;
1678 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
1679 			cflags |= AGE_TD_UDPCSUM;
1680 		/* Set checksum start offset. */
1681 		cflags |= (poff << AGE_TD_CSUM_PLOADOFFSET_SHIFT);
1682 		/* Set checksum insertion position of TCP/UDP. */
1683 		cflags |= ((poff + m->m_pkthdr.csum_data) <<
1684 		    AGE_TD_CSUM_XSUMOFFSET_SHIFT);
1685 	}
1686 	for (; i < nsegs; i++) {
1687 		desc = &sc->age_rdata.age_tx_ring[prod];
1688 		desc->addr = htole64(txsegs[i].ds_addr);
1689 		desc->len = htole32(AGE_TX_BYTES(txsegs[i].ds_len) | vtag);
1690 		desc->flags = htole32(cflags);
1691 		sc->age_cdata.age_tx_cnt++;
1692 		AGE_DESC_INC(prod, AGE_TX_RING_CNT);
1693 	}
1694 	/* Update producer index. */
1695 	sc->age_cdata.age_tx_prod = prod;
1696 
1697 	/* Set EOP on the last descriptor. */
1698 	prod = (prod + AGE_TX_RING_CNT - 1) % AGE_TX_RING_CNT;
1699 	desc = &sc->age_rdata.age_tx_ring[prod];
1700 	desc->flags |= htole32(AGE_TD_EOP);
1701 
1702 	/* Lastly set TSO header and modify IP/TCP header for TSO operation. */
1703 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
1704 		desc = &sc->age_rdata.age_tx_ring[si];
1705 		desc->flags |= htole32(AGE_TD_TSO_HDR);
1706 	}
1707 
1708 	/* Swap dmamap of the first and the last. */
1709 	txd = &sc->age_cdata.age_txdesc[prod];
1710 	map = txd_last->tx_dmamap;
1711 	txd_last->tx_dmamap = txd->tx_dmamap;
1712 	txd->tx_dmamap = map;
1713 	txd->tx_m = m;
1714 
1715 	/* Sync descriptors. */
1716 	bus_dmamap_sync(sc->age_cdata.age_tx_tag, map, BUS_DMASYNC_PREWRITE);
1717 	bus_dmamap_sync(sc->age_cdata.age_tx_ring_tag,
1718 	    sc->age_cdata.age_tx_ring_map,
1719 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1720 
1721 	return (0);
1722 }
1723 
1724 static void
1725 age_start(struct ifnet *ifp)
1726 {
1727         struct age_softc *sc;
1728 
1729 	sc = ifp->if_softc;
1730 	AGE_LOCK(sc);
1731 	age_start_locked(ifp);
1732 	AGE_UNLOCK(sc);
1733 }
1734 
1735 static void
1736 age_start_locked(struct ifnet *ifp)
1737 {
1738         struct age_softc *sc;
1739         struct mbuf *m_head;
1740 	int enq;
1741 
1742 	sc = ifp->if_softc;
1743 
1744 	AGE_LOCK_ASSERT(sc);
1745 
1746 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
1747 	    IFF_DRV_RUNNING || (sc->age_flags & AGE_FLAG_LINK) == 0)
1748 		return;
1749 
1750 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
1751 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1752 		if (m_head == NULL)
1753 			break;
1754 		/*
1755 		 * Pack the data into the transmit ring. If we
1756 		 * don't have room, set the OACTIVE flag and wait
1757 		 * for the NIC to drain the ring.
1758 		 */
1759 		if (age_encap(sc, &m_head)) {
1760 			if (m_head == NULL)
1761 				break;
1762 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1763 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1764 			break;
1765 		}
1766 
1767 		enq++;
1768 		/*
1769 		 * If there's a BPF listener, bounce a copy of this frame
1770 		 * to him.
1771 		 */
1772 		ETHER_BPF_MTAP(ifp, m_head);
1773 	}
1774 
1775 	if (enq > 0) {
1776 		/* Update mbox. */
1777 		AGE_COMMIT_MBOX(sc);
1778 		/* Set a timeout in case the chip goes out to lunch. */
1779 		sc->age_watchdog_timer = AGE_TX_TIMEOUT;
1780 	}
1781 }
1782 
1783 static void
1784 age_watchdog(struct age_softc *sc)
1785 {
1786 	struct ifnet *ifp;
1787 
1788 	AGE_LOCK_ASSERT(sc);
1789 
1790 	if (sc->age_watchdog_timer == 0 || --sc->age_watchdog_timer)
1791 		return;
1792 
1793 	ifp = sc->age_ifp;
1794 	if ((sc->age_flags & AGE_FLAG_LINK) == 0) {
1795 		if_printf(sc->age_ifp, "watchdog timeout (missed link)\n");
1796 		ifp->if_oerrors++;
1797 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1798 		age_init_locked(sc);
1799 		return;
1800 	}
1801 	if (sc->age_cdata.age_tx_cnt == 0) {
1802 		if_printf(sc->age_ifp,
1803 		    "watchdog timeout (missed Tx interrupts) -- recovering\n");
1804 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1805 			age_start_locked(ifp);
1806 		return;
1807 	}
1808 	if_printf(sc->age_ifp, "watchdog timeout\n");
1809 	ifp->if_oerrors++;
1810 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1811 	age_init_locked(sc);
1812 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1813 		age_start_locked(ifp);
1814 }
1815 
1816 static int
1817 age_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1818 {
1819 	struct age_softc *sc;
1820 	struct ifreq *ifr;
1821 	struct mii_data *mii;
1822 	uint32_t reg;
1823 	int error, mask;
1824 
1825 	sc = ifp->if_softc;
1826 	ifr = (struct ifreq *)data;
1827 	error = 0;
1828 	switch (cmd) {
1829 	case SIOCSIFMTU:
1830 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > AGE_JUMBO_MTU)
1831 			error = EINVAL;
1832 		else if (ifp->if_mtu != ifr->ifr_mtu) {
1833 			AGE_LOCK(sc);
1834 			ifp->if_mtu = ifr->ifr_mtu;
1835 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1836 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1837 				age_init_locked(sc);
1838 			}
1839 			AGE_UNLOCK(sc);
1840 		}
1841 		break;
1842 	case SIOCSIFFLAGS:
1843 		AGE_LOCK(sc);
1844 		if ((ifp->if_flags & IFF_UP) != 0) {
1845 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1846 				if (((ifp->if_flags ^ sc->age_if_flags)
1847 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
1848 					age_rxfilter(sc);
1849 			} else {
1850 				if ((sc->age_flags & AGE_FLAG_DETACH) == 0)
1851 					age_init_locked(sc);
1852 			}
1853 		} else {
1854 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1855 				age_stop(sc);
1856 		}
1857 		sc->age_if_flags = ifp->if_flags;
1858 		AGE_UNLOCK(sc);
1859 		break;
1860 	case SIOCADDMULTI:
1861 	case SIOCDELMULTI:
1862 		AGE_LOCK(sc);
1863 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1864 			age_rxfilter(sc);
1865 		AGE_UNLOCK(sc);
1866 		break;
1867 	case SIOCSIFMEDIA:
1868 	case SIOCGIFMEDIA:
1869 		mii = device_get_softc(sc->age_miibus);
1870 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1871 		break;
1872 	case SIOCSIFCAP:
1873 		AGE_LOCK(sc);
1874 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1875 		if ((mask & IFCAP_TXCSUM) != 0 &&
1876 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
1877 			ifp->if_capenable ^= IFCAP_TXCSUM;
1878 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
1879 				ifp->if_hwassist |= AGE_CSUM_FEATURES;
1880 			else
1881 				ifp->if_hwassist &= ~AGE_CSUM_FEATURES;
1882 		}
1883 		if ((mask & IFCAP_RXCSUM) != 0 &&
1884 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
1885 			ifp->if_capenable ^= IFCAP_RXCSUM;
1886 			reg = CSR_READ_4(sc, AGE_MAC_CFG);
1887 			reg &= ~MAC_CFG_RXCSUM_ENB;
1888 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
1889 				reg |= MAC_CFG_RXCSUM_ENB;
1890 			CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
1891 		}
1892 		if ((mask & IFCAP_TSO4) != 0 &&
1893 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
1894 			ifp->if_capenable ^= IFCAP_TSO4;
1895 			if ((ifp->if_capenable & IFCAP_TSO4) != 0)
1896 				ifp->if_hwassist |= CSUM_TSO;
1897 			else
1898 				ifp->if_hwassist &= ~CSUM_TSO;
1899 		}
1900 
1901 		if ((mask & IFCAP_WOL_MCAST) != 0 &&
1902 		    (ifp->if_capabilities & IFCAP_WOL_MCAST) != 0)
1903 			ifp->if_capenable ^= IFCAP_WOL_MCAST;
1904 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
1905 		    (ifp->if_capabilities & IFCAP_WOL_MAGIC) != 0)
1906 			ifp->if_capenable ^= IFCAP_WOL_MAGIC;
1907 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
1908 		    (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
1909 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
1910 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
1911 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
1912 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
1913 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
1914 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
1915 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1916 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
1917 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
1918 			age_rxvlan(sc);
1919 		}
1920 		AGE_UNLOCK(sc);
1921 		VLAN_CAPABILITIES(ifp);
1922 		break;
1923 	default:
1924 		error = ether_ioctl(ifp, cmd, data);
1925 		break;
1926 	}
1927 
1928 	return (error);
1929 }
1930 
1931 static void
1932 age_mac_config(struct age_softc *sc)
1933 {
1934 	struct mii_data *mii;
1935 	uint32_t reg;
1936 
1937 	AGE_LOCK_ASSERT(sc);
1938 
1939 	mii = device_get_softc(sc->age_miibus);
1940 	reg = CSR_READ_4(sc, AGE_MAC_CFG);
1941 	reg &= ~MAC_CFG_FULL_DUPLEX;
1942 	reg &= ~(MAC_CFG_TX_FC | MAC_CFG_RX_FC);
1943 	reg &= ~MAC_CFG_SPEED_MASK;
1944 	/* Reprogram MAC with resolved speed/duplex. */
1945 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
1946 	case IFM_10_T:
1947 	case IFM_100_TX:
1948 		reg |= MAC_CFG_SPEED_10_100;
1949 		break;
1950 	case IFM_1000_T:
1951 		reg |= MAC_CFG_SPEED_1000;
1952 		break;
1953 	}
1954 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
1955 		reg |= MAC_CFG_FULL_DUPLEX;
1956 #ifdef notyet
1957 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_TXPAUSE) != 0)
1958 			reg |= MAC_CFG_TX_FC;
1959 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_ETH_RXPAUSE) != 0)
1960 			reg |= MAC_CFG_RX_FC;
1961 #endif
1962 	}
1963 
1964 	CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
1965 }
1966 
1967 static void
1968 age_link_task(void *arg, int pending)
1969 {
1970 	struct age_softc *sc;
1971 	struct mii_data *mii;
1972 	struct ifnet *ifp;
1973 	uint32_t reg;
1974 
1975 	sc = (struct age_softc *)arg;
1976 
1977 	AGE_LOCK(sc);
1978 	mii = device_get_softc(sc->age_miibus);
1979 	ifp = sc->age_ifp;
1980 	if (mii == NULL || ifp == NULL ||
1981 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1982 		AGE_UNLOCK(sc);
1983 		return;
1984 	}
1985 
1986 	sc->age_flags &= ~AGE_FLAG_LINK;
1987 	if ((mii->mii_media_status & IFM_AVALID) != 0) {
1988 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
1989 		case IFM_10_T:
1990 		case IFM_100_TX:
1991 		case IFM_1000_T:
1992 			sc->age_flags |= AGE_FLAG_LINK;
1993 			break;
1994 		default:
1995 			break;
1996 		}
1997 	}
1998 
1999 	/* Stop Rx/Tx MACs. */
2000 	age_stop_rxmac(sc);
2001 	age_stop_txmac(sc);
2002 
2003 	/* Program MACs with resolved speed/duplex/flow-control. */
2004 	if ((sc->age_flags & AGE_FLAG_LINK) != 0) {
2005 		age_mac_config(sc);
2006 		reg = CSR_READ_4(sc, AGE_MAC_CFG);
2007 		/* Restart DMA engine and Tx/Rx MAC. */
2008 		CSR_WRITE_4(sc, AGE_DMA_CFG, CSR_READ_4(sc, AGE_DMA_CFG) |
2009 		    DMA_CFG_RD_ENB | DMA_CFG_WR_ENB);
2010 		reg |= MAC_CFG_TX_ENB | MAC_CFG_RX_ENB;
2011 		CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
2012 	}
2013 
2014 	AGE_UNLOCK(sc);
2015 }
2016 
2017 static void
2018 age_stats_update(struct age_softc *sc)
2019 {
2020 	struct age_stats *stat;
2021 	struct smb *smb;
2022 	struct ifnet *ifp;
2023 
2024 	AGE_LOCK_ASSERT(sc);
2025 
2026 	stat = &sc->age_stat;
2027 
2028 	bus_dmamap_sync(sc->age_cdata.age_smb_block_tag,
2029 	    sc->age_cdata.age_smb_block_map,
2030 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2031 
2032 	smb = sc->age_rdata.age_smb_block;
2033 	if (smb->updated == 0)
2034 		return;
2035 
2036 	ifp = sc->age_ifp;
2037 	/* Rx stats. */
2038 	stat->rx_frames += smb->rx_frames;
2039 	stat->rx_bcast_frames += smb->rx_bcast_frames;
2040 	stat->rx_mcast_frames += smb->rx_mcast_frames;
2041 	stat->rx_pause_frames += smb->rx_pause_frames;
2042 	stat->rx_control_frames += smb->rx_control_frames;
2043 	stat->rx_crcerrs += smb->rx_crcerrs;
2044 	stat->rx_lenerrs += smb->rx_lenerrs;
2045 	stat->rx_bytes += smb->rx_bytes;
2046 	stat->rx_runts += smb->rx_runts;
2047 	stat->rx_fragments += smb->rx_fragments;
2048 	stat->rx_pkts_64 += smb->rx_pkts_64;
2049 	stat->rx_pkts_65_127 += smb->rx_pkts_65_127;
2050 	stat->rx_pkts_128_255 += smb->rx_pkts_128_255;
2051 	stat->rx_pkts_256_511 += smb->rx_pkts_256_511;
2052 	stat->rx_pkts_512_1023 += smb->rx_pkts_512_1023;
2053 	stat->rx_pkts_1024_1518 += smb->rx_pkts_1024_1518;
2054 	stat->rx_pkts_1519_max += smb->rx_pkts_1519_max;
2055 	stat->rx_pkts_truncated += smb->rx_pkts_truncated;
2056 	stat->rx_fifo_oflows += smb->rx_fifo_oflows;
2057 	stat->rx_desc_oflows += smb->rx_desc_oflows;
2058 	stat->rx_alignerrs += smb->rx_alignerrs;
2059 	stat->rx_bcast_bytes += smb->rx_bcast_bytes;
2060 	stat->rx_mcast_bytes += smb->rx_mcast_bytes;
2061 	stat->rx_pkts_filtered += smb->rx_pkts_filtered;
2062 
2063 	/* Tx stats. */
2064 	stat->tx_frames += smb->tx_frames;
2065 	stat->tx_bcast_frames += smb->tx_bcast_frames;
2066 	stat->tx_mcast_frames += smb->tx_mcast_frames;
2067 	stat->tx_pause_frames += smb->tx_pause_frames;
2068 	stat->tx_excess_defer += smb->tx_excess_defer;
2069 	stat->tx_control_frames += smb->tx_control_frames;
2070 	stat->tx_deferred += smb->tx_deferred;
2071 	stat->tx_bytes += smb->tx_bytes;
2072 	stat->tx_pkts_64 += smb->tx_pkts_64;
2073 	stat->tx_pkts_65_127 += smb->tx_pkts_65_127;
2074 	stat->tx_pkts_128_255 += smb->tx_pkts_128_255;
2075 	stat->tx_pkts_256_511 += smb->tx_pkts_256_511;
2076 	stat->tx_pkts_512_1023 += smb->tx_pkts_512_1023;
2077 	stat->tx_pkts_1024_1518 += smb->tx_pkts_1024_1518;
2078 	stat->tx_pkts_1519_max += smb->tx_pkts_1519_max;
2079 	stat->tx_single_colls += smb->tx_single_colls;
2080 	stat->tx_multi_colls += smb->tx_multi_colls;
2081 	stat->tx_late_colls += smb->tx_late_colls;
2082 	stat->tx_excess_colls += smb->tx_excess_colls;
2083 	stat->tx_underrun += smb->tx_underrun;
2084 	stat->tx_desc_underrun += smb->tx_desc_underrun;
2085 	stat->tx_lenerrs += smb->tx_lenerrs;
2086 	stat->tx_pkts_truncated += smb->tx_pkts_truncated;
2087 	stat->tx_bcast_bytes += smb->tx_bcast_bytes;
2088 	stat->tx_mcast_bytes += smb->tx_mcast_bytes;
2089 
2090 	/* Update counters in ifnet. */
2091 	ifp->if_opackets += smb->tx_frames;
2092 
2093 	ifp->if_collisions += smb->tx_single_colls +
2094 	    smb->tx_multi_colls + smb->tx_late_colls +
2095 	    smb->tx_excess_colls * HDPX_CFG_RETRY_DEFAULT;
2096 
2097 	ifp->if_oerrors += smb->tx_excess_colls +
2098 	    smb->tx_late_colls + smb->tx_underrun +
2099 	    smb->tx_pkts_truncated;
2100 
2101 	ifp->if_ipackets += smb->rx_frames;
2102 
2103 	ifp->if_ierrors += smb->rx_crcerrs + smb->rx_lenerrs +
2104 	    smb->rx_runts + smb->rx_pkts_truncated +
2105 	    smb->rx_fifo_oflows + smb->rx_desc_oflows +
2106 	    smb->rx_alignerrs;
2107 
2108 	/* Update done, clear. */
2109 	smb->updated = 0;
2110 
2111 	bus_dmamap_sync(sc->age_cdata.age_smb_block_tag,
2112 	    sc->age_cdata.age_smb_block_map,
2113 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2114 }
2115 
2116 static int
2117 age_intr(void *arg)
2118 {
2119 	struct age_softc *sc;
2120 	uint32_t status;
2121 
2122 	sc = (struct age_softc *)arg;
2123 
2124 	status = CSR_READ_4(sc, AGE_INTR_STATUS);
2125 	if (status == 0 || (status & AGE_INTRS) == 0)
2126 		return (FILTER_STRAY);
2127 	/* Disable interrupts. */
2128 	CSR_WRITE_4(sc, AGE_INTR_STATUS, status | INTR_DIS_INT);
2129 	taskqueue_enqueue(sc->age_tq, &sc->age_int_task);
2130 
2131 	return (FILTER_HANDLED);
2132 }
2133 
2134 static void
2135 age_int_task(void *arg, int pending)
2136 {
2137 	struct age_softc *sc;
2138 	struct ifnet *ifp;
2139 	struct cmb *cmb;
2140 	uint32_t status;
2141 
2142 	sc = (struct age_softc *)arg;
2143 
2144 	AGE_LOCK(sc);
2145 
2146 	bus_dmamap_sync(sc->age_cdata.age_cmb_block_tag,
2147 	    sc->age_cdata.age_cmb_block_map,
2148 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2149 	cmb = sc->age_rdata.age_cmb_block;
2150 	status = le32toh(cmb->intr_status);
2151 	if (sc->age_morework != 0)
2152 		status |= INTR_CMB_RX;
2153 	if ((status & AGE_INTRS) == 0)
2154 		goto done;
2155 
2156 	sc->age_tpd_cons = (le32toh(cmb->tpd_cons) & TPD_CONS_MASK) >>
2157 	    TPD_CONS_SHIFT;
2158 	sc->age_rr_prod = (le32toh(cmb->rprod_cons) & RRD_PROD_MASK) >>
2159 	    RRD_PROD_SHIFT;
2160 	/* Let hardware know CMB was served. */
2161 	cmb->intr_status = 0;
2162 	bus_dmamap_sync(sc->age_cdata.age_cmb_block_tag,
2163 	    sc->age_cdata.age_cmb_block_map,
2164 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2165 
2166 #if 0
2167 	printf("INTR: 0x%08x\n", status);
2168 	status &= ~INTR_DIS_DMA;
2169 	CSR_WRITE_4(sc, AGE_INTR_STATUS, status | INTR_DIS_INT);
2170 #endif
2171 	ifp = sc->age_ifp;
2172 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2173 		if ((status & INTR_CMB_RX) != 0)
2174 			sc->age_morework = age_rxintr(sc, sc->age_rr_prod,
2175 			    sc->age_process_limit);
2176 		if ((status & INTR_CMB_TX) != 0)
2177 			age_txintr(sc, sc->age_tpd_cons);
2178 		if ((status & (INTR_DMA_RD_TO_RST | INTR_DMA_WR_TO_RST)) != 0) {
2179 			if ((status & INTR_DMA_RD_TO_RST) != 0)
2180 				device_printf(sc->age_dev,
2181 				    "DMA read error! -- resetting\n");
2182 			if ((status & INTR_DMA_WR_TO_RST) != 0)
2183 				device_printf(sc->age_dev,
2184 				    "DMA write error! -- resetting\n");
2185 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2186 			age_init_locked(sc);
2187 		}
2188 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2189 			age_start_locked(ifp);
2190 		if ((status & INTR_SMB) != 0)
2191 			age_stats_update(sc);
2192 	}
2193 
2194 	/* Check whether CMB was updated while serving Tx/Rx/SMB handler. */
2195 	bus_dmamap_sync(sc->age_cdata.age_cmb_block_tag,
2196 	    sc->age_cdata.age_cmb_block_map,
2197 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2198 	status = le32toh(cmb->intr_status);
2199 	if (sc->age_morework != 0 || (status & AGE_INTRS) != 0) {
2200 		taskqueue_enqueue(sc->age_tq, &sc->age_int_task);
2201 		AGE_UNLOCK(sc);
2202 		return;
2203 	}
2204 
2205 done:
2206 	/* Re-enable interrupts. */
2207 	CSR_WRITE_4(sc, AGE_INTR_STATUS, 0);
2208 	AGE_UNLOCK(sc);
2209 }
2210 
2211 static void
2212 age_txintr(struct age_softc *sc, int tpd_cons)
2213 {
2214 	struct ifnet *ifp;
2215 	struct age_txdesc *txd;
2216 	int cons, prog;
2217 
2218 	AGE_LOCK_ASSERT(sc);
2219 
2220 	ifp = sc->age_ifp;
2221 
2222 	bus_dmamap_sync(sc->age_cdata.age_tx_ring_tag,
2223 	    sc->age_cdata.age_tx_ring_map,
2224 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2225 
2226 	/*
2227 	 * Go through our Tx list and free mbufs for those
2228 	 * frames which have been transmitted.
2229 	 */
2230 	cons = sc->age_cdata.age_tx_cons;
2231 	for (prog = 0; cons != tpd_cons; AGE_DESC_INC(cons, AGE_TX_RING_CNT)) {
2232 		if (sc->age_cdata.age_tx_cnt <= 0)
2233 			break;
2234 		prog++;
2235 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2236 		sc->age_cdata.age_tx_cnt--;
2237 		txd = &sc->age_cdata.age_txdesc[cons];
2238 		/*
2239 		 * Clear Tx descriptors, it's not required but would
2240 		 * help debugging in case of Tx issues.
2241 		 */
2242 		txd->tx_desc->addr = 0;
2243 		txd->tx_desc->len = 0;
2244 		txd->tx_desc->flags = 0;
2245 
2246 		if (txd->tx_m == NULL)
2247 			continue;
2248 		/* Reclaim transmitted mbufs. */
2249 		bus_dmamap_sync(sc->age_cdata.age_tx_tag, txd->tx_dmamap,
2250 		    BUS_DMASYNC_POSTWRITE);
2251 		bus_dmamap_unload(sc->age_cdata.age_tx_tag, txd->tx_dmamap);
2252 		m_freem(txd->tx_m);
2253 		txd->tx_m = NULL;
2254 	}
2255 
2256 	if (prog > 0) {
2257 		sc->age_cdata.age_tx_cons = cons;
2258 
2259 		/*
2260 		 * Unarm watchdog timer only when there are no pending
2261 		 * Tx descriptors in queue.
2262 		 */
2263 		if (sc->age_cdata.age_tx_cnt == 0)
2264 			sc->age_watchdog_timer = 0;
2265 		bus_dmamap_sync(sc->age_cdata.age_tx_ring_tag,
2266 		    sc->age_cdata.age_tx_ring_map,
2267 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2268 	}
2269 }
2270 
2271 /* Receive a frame. */
2272 static void
2273 age_rxeof(struct age_softc *sc, struct rx_rdesc *rxrd)
2274 {
2275 	struct age_rxdesc *rxd;
2276 	struct rx_desc *desc;
2277 	struct ifnet *ifp;
2278 	struct mbuf *mp, *m;
2279 	uint32_t status, index, vtag;
2280 	int count, nsegs, pktlen;
2281 	int rx_cons;
2282 
2283 	AGE_LOCK_ASSERT(sc);
2284 
2285 	ifp = sc->age_ifp;
2286 	status = le32toh(rxrd->flags);
2287 	index = le32toh(rxrd->index);
2288 	rx_cons = AGE_RX_CONS(index);
2289 	nsegs = AGE_RX_NSEGS(index);
2290 
2291 	sc->age_cdata.age_rxlen = AGE_RX_BYTES(le32toh(rxrd->len));
2292 	if ((status & AGE_RRD_ERROR) != 0 &&
2293 	    (status & (AGE_RRD_CRC | AGE_RRD_CODE | AGE_RRD_DRIBBLE |
2294 	    AGE_RRD_RUNT | AGE_RRD_OFLOW | AGE_RRD_TRUNC)) != 0) {
2295 		/*
2296 		 * We want to pass the following frames to upper
2297 		 * layer regardless of error status of Rx return
2298 		 * ring.
2299 		 *
2300 		 *  o IP/TCP/UDP checksum is bad.
2301 		 *  o frame length and protocol specific length
2302 		 *     does not match.
2303 		 */
2304 		sc->age_cdata.age_rx_cons += nsegs;
2305 		sc->age_cdata.age_rx_cons %= AGE_RX_RING_CNT;
2306 		return;
2307 	}
2308 
2309 	pktlen = 0;
2310 	for (count = 0; count < nsegs; count++,
2311 	    AGE_DESC_INC(rx_cons, AGE_RX_RING_CNT)) {
2312 		rxd = &sc->age_cdata.age_rxdesc[rx_cons];
2313 		mp = rxd->rx_m;
2314 		desc = rxd->rx_desc;
2315 		/* Add a new receive buffer to the ring. */
2316 		if (age_newbuf(sc, rxd) != 0) {
2317 			ifp->if_iqdrops++;
2318 			/* Reuse Rx buffers. */
2319 			if (sc->age_cdata.age_rxhead != NULL) {
2320 				m_freem(sc->age_cdata.age_rxhead);
2321 				AGE_RXCHAIN_RESET(sc);
2322 			}
2323 			break;
2324 		}
2325 
2326 		/* The length of the first mbuf is computed last. */
2327 		if (count != 0) {
2328 			mp->m_len = AGE_RX_BYTES(le32toh(desc->len));
2329 			pktlen += mp->m_len;
2330 		}
2331 
2332 		/* Chain received mbufs. */
2333 		if (sc->age_cdata.age_rxhead == NULL) {
2334 			sc->age_cdata.age_rxhead = mp;
2335 			sc->age_cdata.age_rxtail = mp;
2336 		} else {
2337 			mp->m_flags &= ~M_PKTHDR;
2338 			sc->age_cdata.age_rxprev_tail =
2339 			    sc->age_cdata.age_rxtail;
2340 			sc->age_cdata.age_rxtail->m_next = mp;
2341 			sc->age_cdata.age_rxtail = mp;
2342 		}
2343 
2344 		if (count == nsegs - 1) {
2345 			/*
2346 			 * It seems that L1 controller has no way
2347 			 * to tell hardware to strip CRC bytes.
2348 			 */
2349 			sc->age_cdata.age_rxlen -= ETHER_CRC_LEN;
2350 			if (nsegs > 1) {
2351 				/* Remove the CRC bytes in chained mbufs. */
2352 				pktlen -= ETHER_CRC_LEN;
2353 				if (mp->m_len <= ETHER_CRC_LEN) {
2354 					sc->age_cdata.age_rxtail =
2355 					    sc->age_cdata.age_rxprev_tail;
2356 					sc->age_cdata.age_rxtail->m_len -=
2357 					    (ETHER_CRC_LEN - mp->m_len);
2358 					sc->age_cdata.age_rxtail->m_next = NULL;
2359 					m_freem(mp);
2360 				} else {
2361 					mp->m_len -= ETHER_CRC_LEN;
2362 				}
2363 			}
2364 
2365 			m = sc->age_cdata.age_rxhead;
2366 			m->m_flags |= M_PKTHDR;
2367 			m->m_pkthdr.rcvif = ifp;
2368 			m->m_pkthdr.len = sc->age_cdata.age_rxlen;
2369 			/* Set the first mbuf length. */
2370 			m->m_len = sc->age_cdata.age_rxlen - pktlen;
2371 
2372 			/*
2373 			 * Set checksum information.
2374 			 * It seems that L1 controller can compute partial
2375 			 * checksum. The partial checksum value can be used
2376 			 * to accelerate checksum computation for fragmented
2377 			 * TCP/UDP packets. Upper network stack already
2378 			 * takes advantage of the partial checksum value in
2379 			 * IP reassembly stage. But I'm not sure the
2380 			 * correctness of the partial hardware checksum
2381 			 * assistance due to lack of data sheet. If it is
2382 			 * proven to work on L1 I'll enable it.
2383 			 */
2384 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0 &&
2385 			    (status & AGE_RRD_IPV4) != 0) {
2386 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2387 				if ((status & AGE_RRD_IPCSUM_NOK) == 0)
2388 					m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2389 				if ((status & (AGE_RRD_TCP | AGE_RRD_UDP)) &&
2390 				    (status & AGE_RRD_TCP_UDPCSUM_NOK) == 0) {
2391 					m->m_pkthdr.csum_flags |=
2392 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2393 					m->m_pkthdr.csum_data = 0xffff;
2394 				}
2395 				/*
2396 				 * Don't mark bad checksum for TCP/UDP frames
2397 				 * as fragmented frames may always have set
2398 				 * bad checksummed bit of descriptor status.
2399 				 */
2400 			}
2401 
2402 			/* Check for VLAN tagged frames. */
2403 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0 &&
2404 			    (status & AGE_RRD_VLAN) != 0) {
2405 				vtag = AGE_RX_VLAN(le32toh(rxrd->vtags));
2406 				m->m_pkthdr.ether_vtag = AGE_RX_VLAN_TAG(vtag);
2407 				m->m_flags |= M_VLANTAG;
2408 			}
2409 
2410 			/* Pass it on. */
2411 			AGE_UNLOCK(sc);
2412 			(*ifp->if_input)(ifp, m);
2413 			AGE_LOCK(sc);
2414 
2415 			/* Reset mbuf chains. */
2416 			AGE_RXCHAIN_RESET(sc);
2417 		}
2418 	}
2419 
2420 	if (count != nsegs) {
2421 		sc->age_cdata.age_rx_cons += nsegs;
2422 		sc->age_cdata.age_rx_cons %= AGE_RX_RING_CNT;
2423 	} else
2424 		sc->age_cdata.age_rx_cons = rx_cons;
2425 }
2426 
2427 static int
2428 age_rxintr(struct age_softc *sc, int rr_prod, int count)
2429 {
2430 	struct rx_rdesc *rxrd;
2431 	int rr_cons, nsegs, pktlen, prog;
2432 
2433 	AGE_LOCK_ASSERT(sc);
2434 
2435 	rr_cons = sc->age_cdata.age_rr_cons;
2436 	if (rr_cons == rr_prod)
2437 		return (0);
2438 
2439 	bus_dmamap_sync(sc->age_cdata.age_rr_ring_tag,
2440 	    sc->age_cdata.age_rr_ring_map,
2441 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2442 	bus_dmamap_sync(sc->age_cdata.age_rx_ring_tag,
2443 	    sc->age_cdata.age_rx_ring_map, BUS_DMASYNC_POSTWRITE);
2444 
2445 	for (prog = 0; rr_cons != rr_prod; prog++) {
2446 		if (count <= 0)
2447 			break;
2448 		rxrd = &sc->age_rdata.age_rr_ring[rr_cons];
2449 		nsegs = AGE_RX_NSEGS(le32toh(rxrd->index));
2450 		if (nsegs == 0)
2451 			break;
2452 		/*
2453 		 * Check number of segments against received bytes.
2454 		 * Non-matching value would indicate that hardware
2455 		 * is still trying to update Rx return descriptors.
2456 		 * I'm not sure whether this check is really needed.
2457 		 */
2458 		pktlen = AGE_RX_BYTES(le32toh(rxrd->len));
2459 		if (nsegs != ((pktlen + (MCLBYTES - ETHER_ALIGN - 1)) /
2460 		    (MCLBYTES - ETHER_ALIGN)))
2461 			break;
2462 
2463 		prog++;
2464 		/* Received a frame. */
2465 		age_rxeof(sc, rxrd);
2466 		/* Clear return ring. */
2467 		rxrd->index = 0;
2468 		AGE_DESC_INC(rr_cons, AGE_RR_RING_CNT);
2469 	}
2470 
2471 	if (prog > 0) {
2472 		/* Update the consumer index. */
2473 		sc->age_cdata.age_rr_cons = rr_cons;
2474 
2475 		bus_dmamap_sync(sc->age_cdata.age_rx_ring_tag,
2476 		    sc->age_cdata.age_rx_ring_map, BUS_DMASYNC_PREWRITE);
2477 		/* Sync descriptors. */
2478 		bus_dmamap_sync(sc->age_cdata.age_rr_ring_tag,
2479 		    sc->age_cdata.age_rr_ring_map,
2480 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2481 
2482 		/* Notify hardware availability of new Rx buffers. */
2483 		AGE_COMMIT_MBOX(sc);
2484 	}
2485 
2486 	return (count > 0 ? 0 : EAGAIN);
2487 }
2488 
2489 static void
2490 age_tick(void *arg)
2491 {
2492 	struct age_softc *sc;
2493 	struct mii_data *mii;
2494 
2495 	sc = (struct age_softc *)arg;
2496 
2497 	AGE_LOCK_ASSERT(sc);
2498 
2499 	mii = device_get_softc(sc->age_miibus);
2500 	mii_tick(mii);
2501 	age_watchdog(sc);
2502 	callout_reset(&sc->age_tick_ch, hz, age_tick, sc);
2503 }
2504 
2505 static void
2506 age_reset(struct age_softc *sc)
2507 {
2508 	uint32_t reg;
2509 	int i;
2510 
2511 	CSR_WRITE_4(sc, AGE_MASTER_CFG, MASTER_RESET);
2512 	CSR_READ_4(sc, AGE_MASTER_CFG);
2513 	DELAY(1000);
2514 	for (i = AGE_RESET_TIMEOUT; i > 0; i--) {
2515 		if ((reg = CSR_READ_4(sc, AGE_IDLE_STATUS)) == 0)
2516 			break;
2517 		DELAY(10);
2518 	}
2519 
2520 	if (i == 0)
2521 		device_printf(sc->age_dev, "reset timeout(0x%08x)!\n", reg);
2522 	/* Initialize PCIe module. From Linux. */
2523 	CSR_WRITE_4(sc, 0x12FC, 0x6500);
2524 	CSR_WRITE_4(sc, 0x1008, CSR_READ_4(sc, 0x1008) | 0x8000);
2525 }
2526 
2527 static void
2528 age_init(void *xsc)
2529 {
2530 	struct age_softc *sc;
2531 
2532 	sc = (struct age_softc *)xsc;
2533 	AGE_LOCK(sc);
2534 	age_init_locked(sc);
2535 	AGE_UNLOCK(sc);
2536 }
2537 
2538 static void
2539 age_init_locked(struct age_softc *sc)
2540 {
2541 	struct ifnet *ifp;
2542 	struct mii_data *mii;
2543 	uint8_t eaddr[ETHER_ADDR_LEN];
2544 	bus_addr_t paddr;
2545 	uint32_t reg, fsize;
2546 	uint32_t rxf_hi, rxf_lo, rrd_hi, rrd_lo;
2547 	int error;
2548 
2549 	AGE_LOCK_ASSERT(sc);
2550 
2551 	ifp = sc->age_ifp;
2552 	mii = device_get_softc(sc->age_miibus);
2553 
2554 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2555 		return;
2556 
2557 	/*
2558 	 * Cancel any pending I/O.
2559 	 */
2560 	age_stop(sc);
2561 
2562 	/*
2563 	 * Reset the chip to a known state.
2564 	 */
2565 	age_reset(sc);
2566 
2567 	/* Initialize descriptors. */
2568 	error = age_init_rx_ring(sc);
2569         if (error != 0) {
2570                 device_printf(sc->age_dev, "no memory for Rx buffers.\n");
2571                 age_stop(sc);
2572 		return;
2573         }
2574 	age_init_rr_ring(sc);
2575 	age_init_tx_ring(sc);
2576 	age_init_cmb_block(sc);
2577 	age_init_smb_block(sc);
2578 
2579 	/* Reprogram the station address. */
2580 	bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2581 	CSR_WRITE_4(sc, AGE_PAR0,
2582 	    eaddr[2] << 24 | eaddr[3] << 16 | eaddr[4] << 8 | eaddr[5]);
2583 	CSR_WRITE_4(sc, AGE_PAR1, eaddr[0] << 8 | eaddr[1]);
2584 
2585 	/* Set descriptor base addresses. */
2586 	paddr = sc->age_rdata.age_tx_ring_paddr;
2587 	CSR_WRITE_4(sc, AGE_DESC_ADDR_HI, AGE_ADDR_HI(paddr));
2588 	paddr = sc->age_rdata.age_rx_ring_paddr;
2589 	CSR_WRITE_4(sc, AGE_DESC_RD_ADDR_LO, AGE_ADDR_LO(paddr));
2590 	paddr = sc->age_rdata.age_rr_ring_paddr;
2591 	CSR_WRITE_4(sc, AGE_DESC_RRD_ADDR_LO, AGE_ADDR_LO(paddr));
2592 	paddr = sc->age_rdata.age_tx_ring_paddr;
2593 	CSR_WRITE_4(sc, AGE_DESC_TPD_ADDR_LO, AGE_ADDR_LO(paddr));
2594 	paddr = sc->age_rdata.age_cmb_block_paddr;
2595 	CSR_WRITE_4(sc, AGE_DESC_CMB_ADDR_LO, AGE_ADDR_LO(paddr));
2596 	paddr = sc->age_rdata.age_smb_block_paddr;
2597 	CSR_WRITE_4(sc, AGE_DESC_SMB_ADDR_LO, AGE_ADDR_LO(paddr));
2598 	/* Set Rx/Rx return descriptor counter. */
2599 	CSR_WRITE_4(sc, AGE_DESC_RRD_RD_CNT,
2600 	    ((AGE_RR_RING_CNT << DESC_RRD_CNT_SHIFT) &
2601 	    DESC_RRD_CNT_MASK) |
2602 	    ((AGE_RX_RING_CNT << DESC_RD_CNT_SHIFT) & DESC_RD_CNT_MASK));
2603 	/* Set Tx descriptor counter. */
2604 	CSR_WRITE_4(sc, AGE_DESC_TPD_CNT,
2605 	    (AGE_TX_RING_CNT << DESC_TPD_CNT_SHIFT) & DESC_TPD_CNT_MASK);
2606 
2607 	/* Tell hardware that we're ready to load descriptors. */
2608 	CSR_WRITE_4(sc, AGE_DMA_BLOCK, DMA_BLOCK_LOAD);
2609 
2610 	/*
2611 	 * Initialize mailbox register.
2612 	 * Updated producer/consumer index information is exchanged
2613 	 * through this mailbox register. However Tx producer and
2614 	 * Rx return consumer/Rx producer are all shared such that
2615 	 * it's hard to separate code path between Tx and Rx without
2616 	 * locking. If L1 hardware have a separate mail box register
2617 	 * for Tx and Rx consumer/producer management we could have
2618 	 * indepent Tx/Rx handler which in turn Rx handler could have
2619 	 * been run without any locking.
2620 	 */
2621 	AGE_COMMIT_MBOX(sc);
2622 
2623 	/* Configure IPG/IFG parameters. */
2624 	CSR_WRITE_4(sc, AGE_IPG_IFG_CFG,
2625 	    ((IPG_IFG_IPG2_DEFAULT << IPG_IFG_IPG2_SHIFT) & IPG_IFG_IPG2_MASK) |
2626 	    ((IPG_IFG_IPG1_DEFAULT << IPG_IFG_IPG1_SHIFT) & IPG_IFG_IPG1_MASK) |
2627 	    ((IPG_IFG_MIFG_DEFAULT << IPG_IFG_MIFG_SHIFT) & IPG_IFG_MIFG_MASK) |
2628 	    ((IPG_IFG_IPGT_DEFAULT << IPG_IFG_IPGT_SHIFT) & IPG_IFG_IPGT_MASK));
2629 
2630 	/* Set parameters for half-duplex media. */
2631 	CSR_WRITE_4(sc, AGE_HDPX_CFG,
2632 	    ((HDPX_CFG_LCOL_DEFAULT << HDPX_CFG_LCOL_SHIFT) &
2633 	    HDPX_CFG_LCOL_MASK) |
2634 	    ((HDPX_CFG_RETRY_DEFAULT << HDPX_CFG_RETRY_SHIFT) &
2635 	    HDPX_CFG_RETRY_MASK) | HDPX_CFG_EXC_DEF_EN |
2636 	    ((HDPX_CFG_ABEBT_DEFAULT << HDPX_CFG_ABEBT_SHIFT) &
2637 	    HDPX_CFG_ABEBT_MASK) |
2638 	    ((HDPX_CFG_JAMIPG_DEFAULT << HDPX_CFG_JAMIPG_SHIFT) &
2639 	    HDPX_CFG_JAMIPG_MASK));
2640 
2641 	/* Configure interrupt moderation timer. */
2642 	CSR_WRITE_2(sc, AGE_IM_TIMER, AGE_USECS(sc->age_int_mod));
2643 	reg = CSR_READ_4(sc, AGE_MASTER_CFG);
2644 	reg &= ~MASTER_MTIMER_ENB;
2645 	if (AGE_USECS(sc->age_int_mod) == 0)
2646 		reg &= ~MASTER_ITIMER_ENB;
2647 	else
2648 		reg |= MASTER_ITIMER_ENB;
2649 	CSR_WRITE_4(sc, AGE_MASTER_CFG, reg);
2650 	if (bootverbose)
2651 		device_printf(sc->age_dev, "interrupt moderation is %d us.\n",
2652 		    sc->age_int_mod);
2653 	CSR_WRITE_2(sc, AGE_INTR_CLR_TIMER, AGE_USECS(1000));
2654 
2655 	/* Set Maximum frame size but don't let MTU be lass than ETHER_MTU. */
2656 	if (ifp->if_mtu < ETHERMTU)
2657 		sc->age_max_frame_size = ETHERMTU;
2658 	else
2659 		sc->age_max_frame_size = ifp->if_mtu;
2660 	sc->age_max_frame_size += ETHER_HDR_LEN +
2661 	    sizeof(struct ether_vlan_header) + ETHER_CRC_LEN;
2662 	CSR_WRITE_4(sc, AGE_FRAME_SIZE, sc->age_max_frame_size);
2663 	/* Configure jumbo frame. */
2664 	fsize = roundup(sc->age_max_frame_size, sizeof(uint64_t));
2665 	CSR_WRITE_4(sc, AGE_RXQ_JUMBO_CFG,
2666 	    (((fsize / sizeof(uint64_t)) <<
2667 	    RXQ_JUMBO_CFG_SZ_THRESH_SHIFT) & RXQ_JUMBO_CFG_SZ_THRESH_MASK) |
2668 	    ((RXQ_JUMBO_CFG_LKAH_DEFAULT <<
2669 	    RXQ_JUMBO_CFG_LKAH_SHIFT) & RXQ_JUMBO_CFG_LKAH_MASK) |
2670 	    ((AGE_USECS(8) << RXQ_JUMBO_CFG_RRD_TIMER_SHIFT) &
2671 	    RXQ_JUMBO_CFG_RRD_TIMER_MASK));
2672 
2673 	/* Configure flow-control parameters. From Linux. */
2674 	if ((sc->age_flags & AGE_FLAG_PCIE) != 0) {
2675 		/*
2676 		 * Magic workaround for old-L1.
2677 		 * Don't know which hw revision requires this magic.
2678 		 */
2679 		CSR_WRITE_4(sc, 0x12FC, 0x6500);
2680 		/*
2681 		 * Another magic workaround for flow-control mode
2682 		 * change. From Linux.
2683 		 */
2684 		CSR_WRITE_4(sc, 0x1008, CSR_READ_4(sc, 0x1008) | 0x8000);
2685 	}
2686 	/*
2687 	 * TODO
2688 	 *  Should understand pause parameter relationships between FIFO
2689 	 *  size and number of Rx descriptors and Rx return descriptors.
2690 	 *
2691 	 *  Magic parameters came from Linux.
2692 	 */
2693 	switch (sc->age_chip_rev) {
2694 	case 0x8001:
2695 	case 0x9001:
2696 	case 0x9002:
2697 	case 0x9003:
2698 		rxf_hi = AGE_RX_RING_CNT / 16;
2699 		rxf_lo = (AGE_RX_RING_CNT * 7) / 8;
2700 		rrd_hi = (AGE_RR_RING_CNT * 7) / 8;
2701 		rrd_lo = AGE_RR_RING_CNT / 16;
2702 		break;
2703 	default:
2704 		reg = CSR_READ_4(sc, AGE_SRAM_RX_FIFO_LEN);
2705 		rxf_lo = reg / 16;
2706 		if (rxf_lo < 192)
2707 			rxf_lo = 192;
2708 		rxf_hi = (reg * 7) / 8;
2709 		if (rxf_hi < rxf_lo)
2710 			rxf_hi = rxf_lo + 16;
2711 		reg = CSR_READ_4(sc, AGE_SRAM_RRD_LEN);
2712 		rrd_lo = reg / 8;
2713 		rrd_hi = (reg * 7) / 8;
2714 		if (rrd_lo < 2)
2715 			rrd_lo = 2;
2716 		if (rrd_hi < rrd_lo)
2717 			rrd_hi = rrd_lo + 3;
2718 		break;
2719 	}
2720 	CSR_WRITE_4(sc, AGE_RXQ_FIFO_PAUSE_THRESH,
2721 	    ((rxf_lo << RXQ_FIFO_PAUSE_THRESH_LO_SHIFT) &
2722 	    RXQ_FIFO_PAUSE_THRESH_LO_MASK) |
2723 	    ((rxf_hi << RXQ_FIFO_PAUSE_THRESH_HI_SHIFT) &
2724 	    RXQ_FIFO_PAUSE_THRESH_HI_MASK));
2725 	CSR_WRITE_4(sc, AGE_RXQ_RRD_PAUSE_THRESH,
2726 	    ((rrd_lo << RXQ_RRD_PAUSE_THRESH_LO_SHIFT) &
2727 	    RXQ_RRD_PAUSE_THRESH_LO_MASK) |
2728 	    ((rrd_hi << RXQ_RRD_PAUSE_THRESH_HI_SHIFT) &
2729 	    RXQ_RRD_PAUSE_THRESH_HI_MASK));
2730 
2731 	/* Configure RxQ. */
2732 	CSR_WRITE_4(sc, AGE_RXQ_CFG,
2733 	    ((RXQ_CFG_RD_BURST_DEFAULT << RXQ_CFG_RD_BURST_SHIFT) &
2734 	    RXQ_CFG_RD_BURST_MASK) |
2735 	    ((RXQ_CFG_RRD_BURST_THRESH_DEFAULT <<
2736 	    RXQ_CFG_RRD_BURST_THRESH_SHIFT) & RXQ_CFG_RRD_BURST_THRESH_MASK) |
2737 	    ((RXQ_CFG_RD_PREF_MIN_IPG_DEFAULT <<
2738 	    RXQ_CFG_RD_PREF_MIN_IPG_SHIFT) & RXQ_CFG_RD_PREF_MIN_IPG_MASK) |
2739 	    RXQ_CFG_CUT_THROUGH_ENB | RXQ_CFG_ENB);
2740 
2741 	/* Configure TxQ. */
2742 	CSR_WRITE_4(sc, AGE_TXQ_CFG,
2743 	    ((TXQ_CFG_TPD_BURST_DEFAULT << TXQ_CFG_TPD_BURST_SHIFT) &
2744 	    TXQ_CFG_TPD_BURST_MASK) |
2745 	    ((TXQ_CFG_TX_FIFO_BURST_DEFAULT << TXQ_CFG_TX_FIFO_BURST_SHIFT) &
2746 	    TXQ_CFG_TX_FIFO_BURST_MASK) |
2747 	    ((TXQ_CFG_TPD_FETCH_DEFAULT <<
2748 	    TXQ_CFG_TPD_FETCH_THRESH_SHIFT) & TXQ_CFG_TPD_FETCH_THRESH_MASK) |
2749 	    TXQ_CFG_ENB);
2750 
2751 	CSR_WRITE_4(sc, AGE_TX_JUMBO_TPD_TH_IPG,
2752 	    (((fsize / sizeof(uint64_t) << TX_JUMBO_TPD_TH_SHIFT)) &
2753 	    TX_JUMBO_TPD_TH_MASK) |
2754 	    ((TX_JUMBO_TPD_IPG_DEFAULT << TX_JUMBO_TPD_IPG_SHIFT) &
2755 	    TX_JUMBO_TPD_IPG_MASK));
2756 	/* Configure DMA parameters. */
2757 	CSR_WRITE_4(sc, AGE_DMA_CFG,
2758 	    DMA_CFG_ENH_ORDER | DMA_CFG_RCB_64 |
2759 	    sc->age_dma_rd_burst | DMA_CFG_RD_ENB |
2760 	    sc->age_dma_wr_burst | DMA_CFG_WR_ENB);
2761 
2762 	/* Configure CMB DMA write threshold. */
2763 	CSR_WRITE_4(sc, AGE_CMB_WR_THRESH,
2764 	    ((CMB_WR_THRESH_RRD_DEFAULT << CMB_WR_THRESH_RRD_SHIFT) &
2765 	    CMB_WR_THRESH_RRD_MASK) |
2766 	    ((CMB_WR_THRESH_TPD_DEFAULT << CMB_WR_THRESH_TPD_SHIFT) &
2767 	    CMB_WR_THRESH_TPD_MASK));
2768 
2769 	/* Set CMB/SMB timer and enable them. */
2770 	CSR_WRITE_4(sc, AGE_CMB_WR_TIMER,
2771 	    ((AGE_USECS(2) << CMB_WR_TIMER_TX_SHIFT) & CMB_WR_TIMER_TX_MASK) |
2772 	    ((AGE_USECS(2) << CMB_WR_TIMER_RX_SHIFT) & CMB_WR_TIMER_RX_MASK));
2773 	/* Request SMB updates for every seconds. */
2774 	CSR_WRITE_4(sc, AGE_SMB_TIMER, AGE_USECS(1000 * 1000));
2775 	CSR_WRITE_4(sc, AGE_CSMB_CTRL, CSMB_CTRL_SMB_ENB | CSMB_CTRL_CMB_ENB);
2776 
2777 	/*
2778 	 * Disable all WOL bits as WOL can interfere normal Rx
2779 	 * operation.
2780 	 */
2781 	CSR_WRITE_4(sc, AGE_WOL_CFG, 0);
2782 
2783 	/*
2784 	 * Configure Tx/Rx MACs.
2785 	 *  - Auto-padding for short frames.
2786 	 *  - Enable CRC generation.
2787 	 *  Start with full-duplex/1000Mbps media. Actual reconfiguration
2788 	 *  of MAC is followed after link establishment.
2789 	 */
2790 	CSR_WRITE_4(sc, AGE_MAC_CFG,
2791 	    MAC_CFG_TX_CRC_ENB | MAC_CFG_TX_AUTO_PAD |
2792 	    MAC_CFG_FULL_DUPLEX | MAC_CFG_SPEED_1000 |
2793 	    ((MAC_CFG_PREAMBLE_DEFAULT << MAC_CFG_PREAMBLE_SHIFT) &
2794 	    MAC_CFG_PREAMBLE_MASK));
2795 	/* Set up the receive filter. */
2796 	age_rxfilter(sc);
2797 	age_rxvlan(sc);
2798 
2799 	reg = CSR_READ_4(sc, AGE_MAC_CFG);
2800 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
2801 		reg |= MAC_CFG_RXCSUM_ENB;
2802 
2803 	/* Ack all pending interrupts and clear it. */
2804 	CSR_WRITE_4(sc, AGE_INTR_STATUS, 0);
2805 	CSR_WRITE_4(sc, AGE_INTR_MASK, AGE_INTRS);
2806 
2807 	/* Finally enable Tx/Rx MAC. */
2808 	CSR_WRITE_4(sc, AGE_MAC_CFG, reg | MAC_CFG_TX_ENB | MAC_CFG_RX_ENB);
2809 
2810 	sc->age_flags &= ~AGE_FLAG_LINK;
2811 	/* Switch to the current media. */
2812 	mii_mediachg(mii);
2813 
2814 	callout_reset(&sc->age_tick_ch, hz, age_tick, sc);
2815 
2816 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2817 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2818 }
2819 
2820 static void
2821 age_stop(struct age_softc *sc)
2822 {
2823 	struct ifnet *ifp;
2824 	struct age_txdesc *txd;
2825 	struct age_rxdesc *rxd;
2826 	uint32_t reg;
2827 	int i;
2828 
2829 	AGE_LOCK_ASSERT(sc);
2830 	/*
2831 	 * Mark the interface down and cancel the watchdog timer.
2832 	 */
2833 	ifp = sc->age_ifp;
2834 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2835 	sc->age_flags &= ~AGE_FLAG_LINK;
2836 	callout_stop(&sc->age_tick_ch);
2837 	sc->age_watchdog_timer = 0;
2838 
2839 	/*
2840 	 * Disable interrupts.
2841 	 */
2842 	CSR_WRITE_4(sc, AGE_INTR_MASK, 0);
2843 	CSR_WRITE_4(sc, AGE_INTR_STATUS, 0xFFFFFFFF);
2844 	/* Stop CMB/SMB updates. */
2845 	CSR_WRITE_4(sc, AGE_CSMB_CTRL, 0);
2846 	/* Stop Rx/Tx MAC. */
2847 	age_stop_rxmac(sc);
2848 	age_stop_txmac(sc);
2849 	/* Stop DMA. */
2850 	CSR_WRITE_4(sc, AGE_DMA_CFG,
2851 	    CSR_READ_4(sc, AGE_DMA_CFG) & ~(DMA_CFG_RD_ENB | DMA_CFG_WR_ENB));
2852 	/* Stop TxQ/RxQ. */
2853 	CSR_WRITE_4(sc, AGE_TXQ_CFG,
2854 	    CSR_READ_4(sc, AGE_TXQ_CFG) & ~TXQ_CFG_ENB);
2855 	CSR_WRITE_4(sc, AGE_RXQ_CFG,
2856 	    CSR_READ_4(sc, AGE_RXQ_CFG) & ~RXQ_CFG_ENB);
2857 	for (i = AGE_RESET_TIMEOUT; i > 0; i--) {
2858 		if ((reg = CSR_READ_4(sc, AGE_IDLE_STATUS)) == 0)
2859 			break;
2860 		DELAY(10);
2861 	}
2862 	if (i == 0)
2863 		device_printf(sc->age_dev,
2864 		    "stopping Rx/Tx MACs timed out(0x%08x)!\n", reg);
2865 
2866 	 /* Reclaim Rx buffers that have been processed. */
2867 	if (sc->age_cdata.age_rxhead != NULL)
2868 		m_freem(sc->age_cdata.age_rxhead);
2869 	AGE_RXCHAIN_RESET(sc);
2870 	/*
2871 	 * Free RX and TX mbufs still in the queues.
2872 	 */
2873 	for (i = 0; i < AGE_RX_RING_CNT; i++) {
2874 		rxd = &sc->age_cdata.age_rxdesc[i];
2875 		if (rxd->rx_m != NULL) {
2876 			bus_dmamap_sync(sc->age_cdata.age_rx_tag,
2877 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2878 			bus_dmamap_unload(sc->age_cdata.age_rx_tag,
2879 			    rxd->rx_dmamap);
2880 			m_freem(rxd->rx_m);
2881 			rxd->rx_m = NULL;
2882 		}
2883         }
2884 	for (i = 0; i < AGE_TX_RING_CNT; i++) {
2885 		txd = &sc->age_cdata.age_txdesc[i];
2886 		if (txd->tx_m != NULL) {
2887 			bus_dmamap_sync(sc->age_cdata.age_tx_tag,
2888 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2889 			bus_dmamap_unload(sc->age_cdata.age_tx_tag,
2890 			    txd->tx_dmamap);
2891 			m_freem(txd->tx_m);
2892 			txd->tx_m = NULL;
2893 		}
2894         }
2895 }
2896 
2897 static void
2898 age_stop_txmac(struct age_softc *sc)
2899 {
2900 	uint32_t reg;
2901 	int i;
2902 
2903 	AGE_LOCK_ASSERT(sc);
2904 
2905 	reg = CSR_READ_4(sc, AGE_MAC_CFG);
2906 	if ((reg & MAC_CFG_TX_ENB) != 0) {
2907 		reg &= ~MAC_CFG_TX_ENB;
2908 		CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
2909 	}
2910 	/* Stop Tx DMA engine. */
2911 	reg = CSR_READ_4(sc, AGE_DMA_CFG);
2912 	if ((reg & DMA_CFG_RD_ENB) != 0) {
2913 		reg &= ~DMA_CFG_RD_ENB;
2914 		CSR_WRITE_4(sc, AGE_DMA_CFG, reg);
2915 	}
2916 	for (i = AGE_RESET_TIMEOUT; i > 0; i--) {
2917 		if ((CSR_READ_4(sc, AGE_IDLE_STATUS) &
2918 		    (IDLE_STATUS_TXMAC | IDLE_STATUS_DMARD)) == 0)
2919 			break;
2920 		DELAY(10);
2921 	}
2922 	if (i == 0)
2923 		device_printf(sc->age_dev, "stopping TxMAC timeout!\n");
2924 }
2925 
2926 static void
2927 age_stop_rxmac(struct age_softc *sc)
2928 {
2929 	uint32_t reg;
2930 	int i;
2931 
2932 	AGE_LOCK_ASSERT(sc);
2933 
2934 	reg = CSR_READ_4(sc, AGE_MAC_CFG);
2935 	if ((reg & MAC_CFG_RX_ENB) != 0) {
2936 		reg &= ~MAC_CFG_RX_ENB;
2937 		CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
2938 	}
2939 	/* Stop Rx DMA engine. */
2940 	reg = CSR_READ_4(sc, AGE_DMA_CFG);
2941 	if ((reg & DMA_CFG_WR_ENB) != 0) {
2942 		reg &= ~DMA_CFG_WR_ENB;
2943 		CSR_WRITE_4(sc, AGE_DMA_CFG, reg);
2944 	}
2945 	for (i = AGE_RESET_TIMEOUT; i > 0; i--) {
2946 		if ((CSR_READ_4(sc, AGE_IDLE_STATUS) &
2947 		    (IDLE_STATUS_RXMAC | IDLE_STATUS_DMAWR)) == 0)
2948 			break;
2949 		DELAY(10);
2950 	}
2951 	if (i == 0)
2952 		device_printf(sc->age_dev, "stopping RxMAC timeout!\n");
2953 }
2954 
2955 static void
2956 age_init_tx_ring(struct age_softc *sc)
2957 {
2958 	struct age_ring_data *rd;
2959 	struct age_txdesc *txd;
2960 	int i;
2961 
2962 	AGE_LOCK_ASSERT(sc);
2963 
2964 	sc->age_cdata.age_tx_prod = 0;
2965 	sc->age_cdata.age_tx_cons = 0;
2966 	sc->age_cdata.age_tx_cnt = 0;
2967 
2968 	rd = &sc->age_rdata;
2969 	bzero(rd->age_tx_ring, AGE_TX_RING_SZ);
2970 	for (i = 0; i < AGE_TX_RING_CNT; i++) {
2971 		txd = &sc->age_cdata.age_txdesc[i];
2972 		txd->tx_desc = &rd->age_tx_ring[i];
2973 		txd->tx_m = NULL;
2974 	}
2975 
2976 	bus_dmamap_sync(sc->age_cdata.age_tx_ring_tag,
2977 	    sc->age_cdata.age_tx_ring_map,
2978 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2979 }
2980 
2981 static int
2982 age_init_rx_ring(struct age_softc *sc)
2983 {
2984 	struct age_ring_data *rd;
2985 	struct age_rxdesc *rxd;
2986 	int i;
2987 
2988 	AGE_LOCK_ASSERT(sc);
2989 
2990 	sc->age_cdata.age_rx_cons = AGE_RX_RING_CNT - 1;
2991 	sc->age_morework = 0;
2992 	rd = &sc->age_rdata;
2993 	bzero(rd->age_rx_ring, AGE_RX_RING_SZ);
2994 	for (i = 0; i < AGE_RX_RING_CNT; i++) {
2995 		rxd = &sc->age_cdata.age_rxdesc[i];
2996 		rxd->rx_m = NULL;
2997 		rxd->rx_desc = &rd->age_rx_ring[i];
2998 		if (age_newbuf(sc, rxd) != 0)
2999 			return (ENOBUFS);
3000 	}
3001 
3002 	bus_dmamap_sync(sc->age_cdata.age_rx_ring_tag,
3003 	    sc->age_cdata.age_rx_ring_map, BUS_DMASYNC_PREWRITE);
3004 
3005 	return (0);
3006 }
3007 
3008 static void
3009 age_init_rr_ring(struct age_softc *sc)
3010 {
3011 	struct age_ring_data *rd;
3012 
3013 	AGE_LOCK_ASSERT(sc);
3014 
3015 	sc->age_cdata.age_rr_cons = 0;
3016 	AGE_RXCHAIN_RESET(sc);
3017 
3018 	rd = &sc->age_rdata;
3019 	bzero(rd->age_rr_ring, AGE_RR_RING_SZ);
3020 	bus_dmamap_sync(sc->age_cdata.age_rr_ring_tag,
3021 	    sc->age_cdata.age_rr_ring_map,
3022 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3023 }
3024 
3025 static void
3026 age_init_cmb_block(struct age_softc *sc)
3027 {
3028 	struct age_ring_data *rd;
3029 
3030 	AGE_LOCK_ASSERT(sc);
3031 
3032 	rd = &sc->age_rdata;
3033 	bzero(rd->age_cmb_block, AGE_CMB_BLOCK_SZ);
3034 	bus_dmamap_sync(sc->age_cdata.age_cmb_block_tag,
3035 	    sc->age_cdata.age_cmb_block_map,
3036 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3037 }
3038 
3039 static void
3040 age_init_smb_block(struct age_softc *sc)
3041 {
3042 	struct age_ring_data *rd;
3043 
3044 	AGE_LOCK_ASSERT(sc);
3045 
3046 	rd = &sc->age_rdata;
3047 	bzero(rd->age_smb_block, AGE_SMB_BLOCK_SZ);
3048 	bus_dmamap_sync(sc->age_cdata.age_smb_block_tag,
3049 	    sc->age_cdata.age_smb_block_map,
3050 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
3051 }
3052 
3053 static int
3054 age_newbuf(struct age_softc *sc, struct age_rxdesc *rxd)
3055 {
3056 	struct rx_desc *desc;
3057 	struct mbuf *m;
3058 	bus_dma_segment_t segs[1];
3059 	bus_dmamap_t map;
3060 	int nsegs;
3061 
3062 	AGE_LOCK_ASSERT(sc);
3063 
3064 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
3065 	if (m == NULL)
3066 		return (ENOBUFS);
3067 	m->m_len = m->m_pkthdr.len = MCLBYTES;
3068 	m_adj(m, ETHER_ALIGN);
3069 
3070 	if (bus_dmamap_load_mbuf_sg(sc->age_cdata.age_rx_tag,
3071 	    sc->age_cdata.age_rx_sparemap, m, segs, &nsegs, 0) != 0) {
3072 		m_freem(m);
3073 		return (ENOBUFS);
3074 	}
3075 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
3076 
3077 	if (rxd->rx_m != NULL) {
3078 		bus_dmamap_sync(sc->age_cdata.age_rx_tag, rxd->rx_dmamap,
3079 		    BUS_DMASYNC_POSTREAD);
3080 		bus_dmamap_unload(sc->age_cdata.age_rx_tag, rxd->rx_dmamap);
3081 	}
3082 	map = rxd->rx_dmamap;
3083 	rxd->rx_dmamap = sc->age_cdata.age_rx_sparemap;
3084 	sc->age_cdata.age_rx_sparemap = map;
3085 	bus_dmamap_sync(sc->age_cdata.age_rx_tag, rxd->rx_dmamap,
3086 	    BUS_DMASYNC_PREREAD);
3087 	rxd->rx_m = m;
3088 
3089 	desc = rxd->rx_desc;
3090 	desc->addr = htole64(segs[0].ds_addr);
3091 	desc->len = htole32((segs[0].ds_len & AGE_RD_LEN_MASK) <<
3092 	    AGE_RD_LEN_SHIFT);
3093 	return (0);
3094 }
3095 
3096 static void
3097 age_rxvlan(struct age_softc *sc)
3098 {
3099 	struct ifnet *ifp;
3100 	uint32_t reg;
3101 
3102 	AGE_LOCK_ASSERT(sc);
3103 
3104 	ifp = sc->age_ifp;
3105 	reg = CSR_READ_4(sc, AGE_MAC_CFG);
3106 	reg &= ~MAC_CFG_VLAN_TAG_STRIP;
3107 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
3108 		reg |= MAC_CFG_VLAN_TAG_STRIP;
3109 	CSR_WRITE_4(sc, AGE_MAC_CFG, reg);
3110 }
3111 
3112 static void
3113 age_rxfilter(struct age_softc *sc)
3114 {
3115 	struct ifnet *ifp;
3116 	struct ifmultiaddr *ifma;
3117 	uint32_t crc;
3118 	uint32_t mchash[2];
3119 	uint32_t rxcfg;
3120 
3121 	AGE_LOCK_ASSERT(sc);
3122 
3123 	ifp = sc->age_ifp;
3124 
3125 	rxcfg = CSR_READ_4(sc, AGE_MAC_CFG);
3126 	rxcfg &= ~(MAC_CFG_ALLMULTI | MAC_CFG_BCAST | MAC_CFG_PROMISC);
3127 	if ((ifp->if_flags & IFF_BROADCAST) != 0)
3128 		rxcfg |= MAC_CFG_BCAST;
3129 	if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
3130 		if ((ifp->if_flags & IFF_PROMISC) != 0)
3131 			rxcfg |= MAC_CFG_PROMISC;
3132 		if ((ifp->if_flags & IFF_ALLMULTI) != 0)
3133 			rxcfg |= MAC_CFG_ALLMULTI;
3134 		CSR_WRITE_4(sc, AGE_MAR0, 0xFFFFFFFF);
3135 		CSR_WRITE_4(sc, AGE_MAR1, 0xFFFFFFFF);
3136 		CSR_WRITE_4(sc, AGE_MAC_CFG, rxcfg);
3137 		return;
3138 	}
3139 
3140 	/* Program new filter. */
3141 	bzero(mchash, sizeof(mchash));
3142 
3143 	if_maddr_rlock(ifp);
3144 	TAILQ_FOREACH(ifma, &sc->age_ifp->if_multiaddrs, ifma_link) {
3145 		if (ifma->ifma_addr->sa_family != AF_LINK)
3146 			continue;
3147 		crc = ether_crc32_be(LLADDR((struct sockaddr_dl *)
3148 		    ifma->ifma_addr), ETHER_ADDR_LEN);
3149 		mchash[crc >> 31] |= 1 << ((crc >> 26) & 0x1f);
3150 	}
3151 	if_maddr_runlock(ifp);
3152 
3153 	CSR_WRITE_4(sc, AGE_MAR0, mchash[0]);
3154 	CSR_WRITE_4(sc, AGE_MAR1, mchash[1]);
3155 	CSR_WRITE_4(sc, AGE_MAC_CFG, rxcfg);
3156 }
3157 
3158 static int
3159 sysctl_age_stats(SYSCTL_HANDLER_ARGS)
3160 {
3161 	struct age_softc *sc;
3162 	struct age_stats *stats;
3163 	int error, result;
3164 
3165 	result = -1;
3166 	error = sysctl_handle_int(oidp, &result, 0, req);
3167 
3168 	if (error != 0 || req->newptr == NULL)
3169 		return (error);
3170 
3171 	if (result != 1)
3172 		return (error);
3173 
3174 	sc = (struct age_softc *)arg1;
3175 	stats = &sc->age_stat;
3176 	printf("%s statistics:\n", device_get_nameunit(sc->age_dev));
3177 	printf("Transmit good frames : %ju\n",
3178 	    (uintmax_t)stats->tx_frames);
3179 	printf("Transmit good broadcast frames : %ju\n",
3180 	    (uintmax_t)stats->tx_bcast_frames);
3181 	printf("Transmit good multicast frames : %ju\n",
3182 	    (uintmax_t)stats->tx_mcast_frames);
3183 	printf("Transmit pause control frames : %u\n",
3184 	    stats->tx_pause_frames);
3185 	printf("Transmit control frames : %u\n",
3186 	    stats->tx_control_frames);
3187 	printf("Transmit frames with excessive deferrals : %u\n",
3188 	    stats->tx_excess_defer);
3189 	printf("Transmit deferrals : %u\n",
3190 	    stats->tx_deferred);
3191 	printf("Transmit good octets : %ju\n",
3192 	    (uintmax_t)stats->tx_bytes);
3193 	printf("Transmit good broadcast octets : %ju\n",
3194 	    (uintmax_t)stats->tx_bcast_bytes);
3195 	printf("Transmit good multicast octets : %ju\n",
3196 	    (uintmax_t)stats->tx_mcast_bytes);
3197 	printf("Transmit frames 64 bytes : %ju\n",
3198 	    (uintmax_t)stats->tx_pkts_64);
3199 	printf("Transmit frames 65 to 127 bytes : %ju\n",
3200 	    (uintmax_t)stats->tx_pkts_65_127);
3201 	printf("Transmit frames 128 to 255 bytes : %ju\n",
3202 	    (uintmax_t)stats->tx_pkts_128_255);
3203 	printf("Transmit frames 256 to 511 bytes : %ju\n",
3204 	    (uintmax_t)stats->tx_pkts_256_511);
3205 	printf("Transmit frames 512 to 1024 bytes : %ju\n",
3206 	    (uintmax_t)stats->tx_pkts_512_1023);
3207 	printf("Transmit frames 1024 to 1518 bytes : %ju\n",
3208 	    (uintmax_t)stats->tx_pkts_1024_1518);
3209 	printf("Transmit frames 1519 to MTU bytes : %ju\n",
3210 	    (uintmax_t)stats->tx_pkts_1519_max);
3211 	printf("Transmit single collisions : %u\n",
3212 	    stats->tx_single_colls);
3213 	printf("Transmit multiple collisions : %u\n",
3214 	    stats->tx_multi_colls);
3215 	printf("Transmit late collisions : %u\n",
3216 	    stats->tx_late_colls);
3217 	printf("Transmit abort due to excessive collisions : %u\n",
3218 	    stats->tx_excess_colls);
3219 	printf("Transmit underruns due to FIFO underruns : %u\n",
3220 	    stats->tx_underrun);
3221 	printf("Transmit descriptor write-back errors : %u\n",
3222 	    stats->tx_desc_underrun);
3223 	printf("Transmit frames with length mismatched frame size : %u\n",
3224 	    stats->tx_lenerrs);
3225 	printf("Transmit frames with truncated due to MTU size : %u\n",
3226 	    stats->tx_lenerrs);
3227 
3228 	printf("Receive good frames : %ju\n",
3229 	    (uintmax_t)stats->rx_frames);
3230 	printf("Receive good broadcast frames : %ju\n",
3231 	    (uintmax_t)stats->rx_bcast_frames);
3232 	printf("Receive good multicast frames : %ju\n",
3233 	    (uintmax_t)stats->rx_mcast_frames);
3234 	printf("Receive pause control frames : %u\n",
3235 	    stats->rx_pause_frames);
3236 	printf("Receive control frames : %u\n",
3237 	    stats->rx_control_frames);
3238 	printf("Receive CRC errors : %u\n",
3239 	    stats->rx_crcerrs);
3240 	printf("Receive frames with length errors : %u\n",
3241 	    stats->rx_lenerrs);
3242 	printf("Receive good octets : %ju\n",
3243 	    (uintmax_t)stats->rx_bytes);
3244 	printf("Receive good broadcast octets : %ju\n",
3245 	    (uintmax_t)stats->rx_bcast_bytes);
3246 	printf("Receive good multicast octets : %ju\n",
3247 	    (uintmax_t)stats->rx_mcast_bytes);
3248 	printf("Receive frames too short : %u\n",
3249 	    stats->rx_runts);
3250 	printf("Receive fragmented frames : %ju\n",
3251 	    (uintmax_t)stats->rx_fragments);
3252 	printf("Receive frames 64 bytes : %ju\n",
3253 	    (uintmax_t)stats->rx_pkts_64);
3254 	printf("Receive frames 65 to 127 bytes : %ju\n",
3255 	    (uintmax_t)stats->rx_pkts_65_127);
3256 	printf("Receive frames 128 to 255 bytes : %ju\n",
3257 	    (uintmax_t)stats->rx_pkts_128_255);
3258 	printf("Receive frames 256 to 511 bytes : %ju\n",
3259 	    (uintmax_t)stats->rx_pkts_256_511);
3260 	printf("Receive frames 512 to 1024 bytes : %ju\n",
3261 	    (uintmax_t)stats->rx_pkts_512_1023);
3262 	printf("Receive frames 1024 to 1518 bytes : %ju\n",
3263 	    (uintmax_t)stats->rx_pkts_1024_1518);
3264 	printf("Receive frames 1519 to MTU bytes : %ju\n",
3265 	    (uintmax_t)stats->rx_pkts_1519_max);
3266 	printf("Receive frames too long : %ju\n",
3267 	    (uint64_t)stats->rx_pkts_truncated);
3268 	printf("Receive frames with FIFO overflow : %u\n",
3269 	    stats->rx_fifo_oflows);
3270 	printf("Receive frames with return descriptor overflow : %u\n",
3271 	    stats->rx_desc_oflows);
3272 	printf("Receive frames with alignment errors : %u\n",
3273 	    stats->rx_alignerrs);
3274 	printf("Receive frames dropped due to address filtering : %ju\n",
3275 	    (uint64_t)stats->rx_pkts_filtered);
3276 
3277 	return (error);
3278 }
3279 
3280 static int
3281 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
3282 {
3283 	int error, value;
3284 
3285 	if (arg1 == NULL)
3286 		return (EINVAL);
3287 	value = *(int *)arg1;
3288 	error = sysctl_handle_int(oidp, &value, 0, req);
3289 	if (error || req->newptr == NULL)
3290 		return (error);
3291 	if (value < low || value > high)
3292 		return (EINVAL);
3293         *(int *)arg1 = value;
3294 
3295         return (0);
3296 }
3297 
3298 static int
3299 sysctl_hw_age_proc_limit(SYSCTL_HANDLER_ARGS)
3300 {
3301 	return (sysctl_int_range(oidp, arg1, arg2, req,
3302 	    AGE_PROC_MIN, AGE_PROC_MAX));
3303 }
3304 
3305 static int
3306 sysctl_hw_age_int_mod(SYSCTL_HANDLER_ARGS)
3307 {
3308 
3309 	return (sysctl_int_range(oidp, arg1, arg2, req, AGE_IM_TIMER_MIN,
3310 	    AGE_IM_TIMER_MAX));
3311 }
3312