xref: /freebsd/sys/dev/stge/if_stge.c (revision e87ec409fa9b21abf79895837fe375ab3d7e408a)
1 /*	$NetBSD: if_stge.c,v 1.32 2005/12/11 12:22:49 christos Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 2001 The NetBSD Foundation, Inc.
7  * All rights reserved.
8  *
9  * This code is derived from software contributed to The NetBSD Foundation
10  * by Jason R. Thorpe.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * Device driver for the Sundance Tech. TC9021 10/100/1000
36  * Ethernet controller.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #ifdef HAVE_KERNEL_OPTION_HEADERS
43 #include "opt_device_polling.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/endian.h>
49 #include <sys/mbuf.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/socket.h>
54 #include <sys/sockio.h>
55 #include <sys/sysctl.h>
56 #include <sys/taskqueue.h>
57 
58 #include <net/bpf.h>
59 #include <net/ethernet.h>
60 #include <net/if.h>
61 #include <net/if_var.h>
62 #include <net/if_dl.h>
63 #include <net/if_media.h>
64 #include <net/if_types.h>
65 #include <net/if_vlan_var.h>
66 
67 #include <machine/bus.h>
68 #include <machine/resource.h>
69 #include <sys/bus.h>
70 #include <sys/rman.h>
71 
72 #include <dev/mii/mii.h>
73 #include <dev/mii/mii_bitbang.h>
74 #include <dev/mii/miivar.h>
75 
76 #include <dev/pci/pcireg.h>
77 #include <dev/pci/pcivar.h>
78 
79 #include <dev/stge/if_stgereg.h>
80 
81 #define	STGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
82 
83 MODULE_DEPEND(stge, pci, 1, 1, 1);
84 MODULE_DEPEND(stge, ether, 1, 1, 1);
85 MODULE_DEPEND(stge, miibus, 1, 1, 1);
86 
87 /* "device miibus" required.  See GENERIC if you get errors here. */
88 #include "miibus_if.h"
89 
90 /*
91  * Devices supported by this driver.
92  */
93 static const struct stge_product {
94 	uint16_t	stge_vendorid;
95 	uint16_t	stge_deviceid;
96 	const char	*stge_name;
97 } stge_products[] = {
98 	{ VENDOR_SUNDANCETI,	DEVICEID_SUNDANCETI_ST1023,
99 	  "Sundance ST-1023 Gigabit Ethernet" },
100 
101 	{ VENDOR_SUNDANCETI,	DEVICEID_SUNDANCETI_ST2021,
102 	  "Sundance ST-2021 Gigabit Ethernet" },
103 
104 	{ VENDOR_TAMARACK,	DEVICEID_TAMARACK_TC9021,
105 	  "Tamarack TC9021 Gigabit Ethernet" },
106 
107 	{ VENDOR_TAMARACK,	DEVICEID_TAMARACK_TC9021_ALT,
108 	  "Tamarack TC9021 Gigabit Ethernet" },
109 
110 	/*
111 	 * The Sundance sample boards use the Sundance vendor ID,
112 	 * but the Tamarack product ID.
113 	 */
114 	{ VENDOR_SUNDANCETI,	DEVICEID_TAMARACK_TC9021,
115 	  "Sundance TC9021 Gigabit Ethernet" },
116 
117 	{ VENDOR_SUNDANCETI,	DEVICEID_TAMARACK_TC9021_ALT,
118 	  "Sundance TC9021 Gigabit Ethernet" },
119 
120 	{ VENDOR_DLINK,		DEVICEID_DLINK_DL4000,
121 	  "D-Link DL-4000 Gigabit Ethernet" },
122 
123 	{ VENDOR_ANTARES,	DEVICEID_ANTARES_TC9021,
124 	  "Antares Gigabit Ethernet" }
125 };
126 
127 static int	stge_probe(device_t);
128 static int	stge_attach(device_t);
129 static int	stge_detach(device_t);
130 static int	stge_shutdown(device_t);
131 static int	stge_suspend(device_t);
132 static int	stge_resume(device_t);
133 
134 static int	stge_encap(struct stge_softc *, struct mbuf **);
135 static void	stge_start(struct ifnet *);
136 static void	stge_start_locked(struct ifnet *);
137 static void	stge_watchdog(struct stge_softc *);
138 static int	stge_ioctl(struct ifnet *, u_long, caddr_t);
139 static void	stge_init(void *);
140 static void	stge_init_locked(struct stge_softc *);
141 static void	stge_vlan_setup(struct stge_softc *);
142 static void	stge_stop(struct stge_softc *);
143 static void	stge_start_tx(struct stge_softc *);
144 static void	stge_start_rx(struct stge_softc *);
145 static void	stge_stop_tx(struct stge_softc *);
146 static void	stge_stop_rx(struct stge_softc *);
147 
148 static void	stge_reset(struct stge_softc *, uint32_t);
149 static int	stge_eeprom_wait(struct stge_softc *);
150 static void	stge_read_eeprom(struct stge_softc *, int, uint16_t *);
151 static void	stge_tick(void *);
152 static void	stge_stats_update(struct stge_softc *);
153 static void	stge_set_filter(struct stge_softc *);
154 static void	stge_set_multi(struct stge_softc *);
155 
156 static void	stge_link_task(void *, int);
157 static void	stge_intr(void *);
158 static __inline int stge_tx_error(struct stge_softc *);
159 static void	stge_txeof(struct stge_softc *);
160 static int	stge_rxeof(struct stge_softc *);
161 static __inline void stge_discard_rxbuf(struct stge_softc *, int);
162 static int	stge_newbuf(struct stge_softc *, int);
163 #ifndef __NO_STRICT_ALIGNMENT
164 static __inline struct mbuf *stge_fixup_rx(struct stge_softc *, struct mbuf *);
165 #endif
166 
167 static int	stge_miibus_readreg(device_t, int, int);
168 static int	stge_miibus_writereg(device_t, int, int, int);
169 static void	stge_miibus_statchg(device_t);
170 static int	stge_mediachange(struct ifnet *);
171 static void	stge_mediastatus(struct ifnet *, struct ifmediareq *);
172 
173 static void	stge_dmamap_cb(void *, bus_dma_segment_t *, int, int);
174 static int	stge_dma_alloc(struct stge_softc *);
175 static void	stge_dma_free(struct stge_softc *);
176 static void	stge_dma_wait(struct stge_softc *);
177 static void	stge_init_tx_ring(struct stge_softc *);
178 static int	stge_init_rx_ring(struct stge_softc *);
179 #ifdef DEVICE_POLLING
180 static int	stge_poll(struct ifnet *, enum poll_cmd, int);
181 #endif
182 
183 static void	stge_setwol(struct stge_softc *);
184 static int	sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
185 static int	sysctl_hw_stge_rxint_nframe(SYSCTL_HANDLER_ARGS);
186 static int	sysctl_hw_stge_rxint_dmawait(SYSCTL_HANDLER_ARGS);
187 
188 /*
189  * MII bit-bang glue
190  */
191 static uint32_t stge_mii_bitbang_read(device_t);
192 static void	stge_mii_bitbang_write(device_t, uint32_t);
193 
194 static const struct mii_bitbang_ops stge_mii_bitbang_ops = {
195 	stge_mii_bitbang_read,
196 	stge_mii_bitbang_write,
197 	{
198 		PC_MgmtData,		/* MII_BIT_MDO */
199 		PC_MgmtData,		/* MII_BIT_MDI */
200 		PC_MgmtClk,		/* MII_BIT_MDC */
201 		PC_MgmtDir,		/* MII_BIT_DIR_HOST_PHY */
202 		0,			/* MII_BIT_DIR_PHY_HOST */
203 	}
204 };
205 
206 static device_method_t stge_methods[] = {
207 	/* Device interface */
208 	DEVMETHOD(device_probe,		stge_probe),
209 	DEVMETHOD(device_attach,	stge_attach),
210 	DEVMETHOD(device_detach,	stge_detach),
211 	DEVMETHOD(device_shutdown,	stge_shutdown),
212 	DEVMETHOD(device_suspend,	stge_suspend),
213 	DEVMETHOD(device_resume,	stge_resume),
214 
215 	/* MII interface */
216 	DEVMETHOD(miibus_readreg,	stge_miibus_readreg),
217 	DEVMETHOD(miibus_writereg,	stge_miibus_writereg),
218 	DEVMETHOD(miibus_statchg,	stge_miibus_statchg),
219 
220 	DEVMETHOD_END
221 };
222 
223 static driver_t stge_driver = {
224 	"stge",
225 	stge_methods,
226 	sizeof(struct stge_softc)
227 };
228 
229 static devclass_t stge_devclass;
230 
231 DRIVER_MODULE(stge, pci, stge_driver, stge_devclass, 0, 0);
232 DRIVER_MODULE(miibus, stge, miibus_driver, miibus_devclass, 0, 0);
233 
234 static struct resource_spec stge_res_spec_io[] = {
235 	{ SYS_RES_IOPORT,	PCIR_BAR(0),	RF_ACTIVE },
236 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
237 	{ -1,			0,		0 }
238 };
239 
240 static struct resource_spec stge_res_spec_mem[] = {
241 	{ SYS_RES_MEMORY,	PCIR_BAR(1),	RF_ACTIVE },
242 	{ SYS_RES_IRQ,		0,		RF_ACTIVE | RF_SHAREABLE },
243 	{ -1,			0,		0 }
244 };
245 
246 /*
247  * stge_mii_bitbang_read: [mii bit-bang interface function]
248  *
249  *	Read the MII serial port for the MII bit-bang module.
250  */
251 static uint32_t
252 stge_mii_bitbang_read(device_t dev)
253 {
254 	struct stge_softc *sc;
255 	uint32_t val;
256 
257 	sc = device_get_softc(dev);
258 
259 	val = CSR_READ_1(sc, STGE_PhyCtrl);
260 	CSR_BARRIER(sc, STGE_PhyCtrl, 1,
261 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
262 	return (val);
263 }
264 
265 /*
266  * stge_mii_bitbang_write: [mii big-bang interface function]
267  *
268  *	Write the MII serial port for the MII bit-bang module.
269  */
270 static void
271 stge_mii_bitbang_write(device_t dev, uint32_t val)
272 {
273 	struct stge_softc *sc;
274 
275 	sc = device_get_softc(dev);
276 
277 	CSR_WRITE_1(sc, STGE_PhyCtrl, val);
278 	CSR_BARRIER(sc, STGE_PhyCtrl, 1,
279 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
280 }
281 
282 /*
283  * sc_miibus_readreg:	[mii interface function]
284  *
285  *	Read a PHY register on the MII of the TC9021.
286  */
287 static int
288 stge_miibus_readreg(device_t dev, int phy, int reg)
289 {
290 	struct stge_softc *sc;
291 	int error, val;
292 
293 	sc = device_get_softc(dev);
294 
295 	if (reg == STGE_PhyCtrl) {
296 		/* XXX allow ip1000phy read STGE_PhyCtrl register. */
297 		STGE_MII_LOCK(sc);
298 		error = CSR_READ_1(sc, STGE_PhyCtrl);
299 		STGE_MII_UNLOCK(sc);
300 		return (error);
301 	}
302 
303 	STGE_MII_LOCK(sc);
304 	val = mii_bitbang_readreg(dev, &stge_mii_bitbang_ops, phy, reg);
305 	STGE_MII_UNLOCK(sc);
306 	return (val);
307 }
308 
309 /*
310  * stge_miibus_writereg:	[mii interface function]
311  *
312  *	Write a PHY register on the MII of the TC9021.
313  */
314 static int
315 stge_miibus_writereg(device_t dev, int phy, int reg, int val)
316 {
317 	struct stge_softc *sc;
318 
319 	sc = device_get_softc(dev);
320 
321 	STGE_MII_LOCK(sc);
322 	mii_bitbang_writereg(dev, &stge_mii_bitbang_ops, phy, reg, val);
323 	STGE_MII_UNLOCK(sc);
324 	return (0);
325 }
326 
327 /*
328  * stge_miibus_statchg:	[mii interface function]
329  *
330  *	Callback from MII layer when media changes.
331  */
332 static void
333 stge_miibus_statchg(device_t dev)
334 {
335 	struct stge_softc *sc;
336 
337 	sc = device_get_softc(dev);
338 	taskqueue_enqueue(taskqueue_swi, &sc->sc_link_task);
339 }
340 
341 /*
342  * stge_mediastatus:	[ifmedia interface function]
343  *
344  *	Get the current interface media status.
345  */
346 static void
347 stge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
348 {
349 	struct stge_softc *sc;
350 	struct mii_data *mii;
351 
352 	sc = ifp->if_softc;
353 	mii = device_get_softc(sc->sc_miibus);
354 
355 	mii_pollstat(mii);
356 	ifmr->ifm_status = mii->mii_media_status;
357 	ifmr->ifm_active = mii->mii_media_active;
358 }
359 
360 /*
361  * stge_mediachange:	[ifmedia interface function]
362  *
363  *	Set hardware to newly-selected media.
364  */
365 static int
366 stge_mediachange(struct ifnet *ifp)
367 {
368 	struct stge_softc *sc;
369 	struct mii_data *mii;
370 
371 	sc = ifp->if_softc;
372 	mii = device_get_softc(sc->sc_miibus);
373 	mii_mediachg(mii);
374 
375 	return (0);
376 }
377 
378 static int
379 stge_eeprom_wait(struct stge_softc *sc)
380 {
381 	int i;
382 
383 	for (i = 0; i < STGE_TIMEOUT; i++) {
384 		DELAY(1000);
385 		if ((CSR_READ_2(sc, STGE_EepromCtrl) & EC_EepromBusy) == 0)
386 			return (0);
387 	}
388 	return (1);
389 }
390 
391 /*
392  * stge_read_eeprom:
393  *
394  *	Read data from the serial EEPROM.
395  */
396 static void
397 stge_read_eeprom(struct stge_softc *sc, int offset, uint16_t *data)
398 {
399 
400 	if (stge_eeprom_wait(sc))
401 		device_printf(sc->sc_dev, "EEPROM failed to come ready\n");
402 
403 	CSR_WRITE_2(sc, STGE_EepromCtrl,
404 	    EC_EepromAddress(offset) | EC_EepromOpcode(EC_OP_RR));
405 	if (stge_eeprom_wait(sc))
406 		device_printf(sc->sc_dev, "EEPROM read timed out\n");
407 	*data = CSR_READ_2(sc, STGE_EepromData);
408 }
409 
410 static int
411 stge_probe(device_t dev)
412 {
413 	const struct stge_product *sp;
414 	int i;
415 	uint16_t vendor, devid;
416 
417 	vendor = pci_get_vendor(dev);
418 	devid = pci_get_device(dev);
419 	sp = stge_products;
420 	for (i = 0; i < nitems(stge_products); i++, sp++) {
421 		if (vendor == sp->stge_vendorid &&
422 		    devid == sp->stge_deviceid) {
423 			device_set_desc(dev, sp->stge_name);
424 			return (BUS_PROBE_DEFAULT);
425 		}
426 	}
427 
428 	return (ENXIO);
429 }
430 
431 static int
432 stge_attach(device_t dev)
433 {
434 	struct stge_softc *sc;
435 	struct ifnet *ifp;
436 	uint8_t enaddr[ETHER_ADDR_LEN];
437 	int error, flags, i;
438 	uint16_t cmd;
439 	uint32_t val;
440 
441 	error = 0;
442 	sc = device_get_softc(dev);
443 	sc->sc_dev = dev;
444 
445 	mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
446 	    MTX_DEF);
447 	mtx_init(&sc->sc_mii_mtx, "stge_mii_mutex", NULL, MTX_DEF);
448 	callout_init_mtx(&sc->sc_tick_ch, &sc->sc_mtx, 0);
449 	TASK_INIT(&sc->sc_link_task, 0, stge_link_task, sc);
450 
451 	/*
452 	 * Map the device.
453 	 */
454 	pci_enable_busmaster(dev);
455 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
456 	val = pci_read_config(dev, PCIR_BAR(1), 4);
457 	if (PCI_BAR_IO(val))
458 		sc->sc_spec = stge_res_spec_mem;
459 	else {
460 		val = pci_read_config(dev, PCIR_BAR(0), 4);
461 		if (!PCI_BAR_IO(val)) {
462 			device_printf(sc->sc_dev, "couldn't locate IO BAR\n");
463 			error = ENXIO;
464 			goto fail;
465 		}
466 		sc->sc_spec = stge_res_spec_io;
467 	}
468 	error = bus_alloc_resources(dev, sc->sc_spec, sc->sc_res);
469 	if (error != 0) {
470 		device_printf(dev, "couldn't allocate %s resources\n",
471 		    sc->sc_spec == stge_res_spec_mem ? "memory" : "I/O");
472 		goto fail;
473 	}
474 	sc->sc_rev = pci_get_revid(dev);
475 
476 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
477 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
478 	    "rxint_nframe", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
479 	    &sc->sc_rxint_nframe, 0, sysctl_hw_stge_rxint_nframe, "I",
480 	    "stge rx interrupt nframe");
481 
482 	SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
483 	    SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO,
484 	    "rxint_dmawait", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
485 	    &sc->sc_rxint_dmawait, 0, sysctl_hw_stge_rxint_dmawait, "I",
486 	    "stge rx interrupt dmawait");
487 
488 	/* Pull in device tunables. */
489 	sc->sc_rxint_nframe = STGE_RXINT_NFRAME_DEFAULT;
490 	error = resource_int_value(device_get_name(dev), device_get_unit(dev),
491 	    "rxint_nframe", &sc->sc_rxint_nframe);
492 	if (error == 0) {
493 		if (sc->sc_rxint_nframe < STGE_RXINT_NFRAME_MIN ||
494 		    sc->sc_rxint_nframe > STGE_RXINT_NFRAME_MAX) {
495 			device_printf(dev, "rxint_nframe value out of range; "
496 			    "using default: %d\n", STGE_RXINT_NFRAME_DEFAULT);
497 			sc->sc_rxint_nframe = STGE_RXINT_NFRAME_DEFAULT;
498 		}
499 	}
500 
501 	sc->sc_rxint_dmawait = STGE_RXINT_DMAWAIT_DEFAULT;
502 	error = resource_int_value(device_get_name(dev), device_get_unit(dev),
503 	    "rxint_dmawait", &sc->sc_rxint_dmawait);
504 	if (error == 0) {
505 		if (sc->sc_rxint_dmawait < STGE_RXINT_DMAWAIT_MIN ||
506 		    sc->sc_rxint_dmawait > STGE_RXINT_DMAWAIT_MAX) {
507 			device_printf(dev, "rxint_dmawait value out of range; "
508 			    "using default: %d\n", STGE_RXINT_DMAWAIT_DEFAULT);
509 			sc->sc_rxint_dmawait = STGE_RXINT_DMAWAIT_DEFAULT;
510 		}
511 	}
512 
513 	if ((error = stge_dma_alloc(sc)) != 0)
514 		goto fail;
515 
516 	/*
517 	 * Determine if we're copper or fiber.  It affects how we
518 	 * reset the card.
519 	 */
520 	if (CSR_READ_4(sc, STGE_AsicCtrl) & AC_PhyMedia)
521 		sc->sc_usefiber = 1;
522 	else
523 		sc->sc_usefiber = 0;
524 
525 	/* Load LED configuration from EEPROM. */
526 	stge_read_eeprom(sc, STGE_EEPROM_LEDMode, &sc->sc_led);
527 
528 	/*
529 	 * Reset the chip to a known state.
530 	 */
531 	STGE_LOCK(sc);
532 	stge_reset(sc, STGE_RESET_FULL);
533 	STGE_UNLOCK(sc);
534 
535 	/*
536 	 * Reading the station address from the EEPROM doesn't seem
537 	 * to work, at least on my sample boards.  Instead, since
538 	 * the reset sequence does AutoInit, read it from the station
539 	 * address registers. For Sundance 1023 you can only read it
540 	 * from EEPROM.
541 	 */
542 	if (pci_get_device(dev) != DEVICEID_SUNDANCETI_ST1023) {
543 		uint16_t v;
544 
545 		v = CSR_READ_2(sc, STGE_StationAddress0);
546 		enaddr[0] = v & 0xff;
547 		enaddr[1] = v >> 8;
548 		v = CSR_READ_2(sc, STGE_StationAddress1);
549 		enaddr[2] = v & 0xff;
550 		enaddr[3] = v >> 8;
551 		v = CSR_READ_2(sc, STGE_StationAddress2);
552 		enaddr[4] = v & 0xff;
553 		enaddr[5] = v >> 8;
554 		sc->sc_stge1023 = 0;
555 	} else {
556 		uint16_t myaddr[ETHER_ADDR_LEN / 2];
557 		for (i = 0; i <ETHER_ADDR_LEN / 2; i++) {
558 			stge_read_eeprom(sc, STGE_EEPROM_StationAddress0 + i,
559 			    &myaddr[i]);
560 			myaddr[i] = le16toh(myaddr[i]);
561 		}
562 		bcopy(myaddr, enaddr, sizeof(enaddr));
563 		sc->sc_stge1023 = 1;
564 	}
565 
566 	ifp = sc->sc_ifp = if_alloc(IFT_ETHER);
567 	if (ifp == NULL) {
568 		device_printf(sc->sc_dev, "failed to if_alloc()\n");
569 		error = ENXIO;
570 		goto fail;
571 	}
572 
573 	ifp->if_softc = sc;
574 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
575 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
576 	ifp->if_ioctl = stge_ioctl;
577 	ifp->if_start = stge_start;
578 	ifp->if_init = stge_init;
579 	ifp->if_snd.ifq_drv_maxlen = STGE_TX_RING_CNT - 1;
580 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
581 	IFQ_SET_READY(&ifp->if_snd);
582 	/* Revision B3 and earlier chips have checksum bug. */
583 	if (sc->sc_rev >= 0x0c) {
584 		ifp->if_hwassist = STGE_CSUM_FEATURES;
585 		ifp->if_capabilities = IFCAP_HWCSUM;
586 	} else {
587 		ifp->if_hwassist = 0;
588 		ifp->if_capabilities = 0;
589 	}
590 	ifp->if_capabilities |= IFCAP_WOL_MAGIC;
591 	ifp->if_capenable = ifp->if_capabilities;
592 
593 	/*
594 	 * Read some important bits from the PhyCtrl register.
595 	 */
596 	sc->sc_PhyCtrl = CSR_READ_1(sc, STGE_PhyCtrl) &
597 	    (PC_PhyDuplexPolarity | PC_PhyLnkPolarity);
598 
599 	/* Set up MII bus. */
600 	flags = MIIF_DOPAUSE;
601 	if (sc->sc_rev >= 0x40 && sc->sc_rev <= 0x4e)
602 		flags |= MIIF_MACPRIV0;
603 	error = mii_attach(sc->sc_dev, &sc->sc_miibus, ifp, stge_mediachange,
604 	    stge_mediastatus, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY,
605 	    flags);
606 	if (error != 0) {
607 		device_printf(sc->sc_dev, "attaching PHYs failed\n");
608 		goto fail;
609 	}
610 
611 	ether_ifattach(ifp, enaddr);
612 
613 	/* VLAN capability setup */
614 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
615 	if (sc->sc_rev >= 0x0c)
616 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
617 	ifp->if_capenable = ifp->if_capabilities;
618 #ifdef DEVICE_POLLING
619 	ifp->if_capabilities |= IFCAP_POLLING;
620 #endif
621 	/*
622 	 * Tell the upper layer(s) we support long frames.
623 	 * Must appear after the call to ether_ifattach() because
624 	 * ether_ifattach() sets ifi_hdrlen to the default value.
625 	 */
626 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
627 
628 	/*
629 	 * The manual recommends disabling early transmit, so we
630 	 * do.  It's disabled anyway, if using IP checksumming,
631 	 * since the entire packet must be in the FIFO in order
632 	 * for the chip to perform the checksum.
633 	 */
634 	sc->sc_txthresh = 0x0fff;
635 
636 	/*
637 	 * Disable MWI if the PCI layer tells us to.
638 	 */
639 	sc->sc_DMACtrl = 0;
640 	if ((cmd & PCIM_CMD_MWRICEN) == 0)
641 		sc->sc_DMACtrl |= DMAC_MWIDisable;
642 
643 	/*
644 	 * Hookup IRQ
645 	 */
646 	error = bus_setup_intr(dev, sc->sc_res[1], INTR_TYPE_NET | INTR_MPSAFE,
647 	    NULL, stge_intr, sc, &sc->sc_ih);
648 	if (error != 0) {
649 		ether_ifdetach(ifp);
650 		device_printf(sc->sc_dev, "couldn't set up IRQ\n");
651 		sc->sc_ifp = NULL;
652 		goto fail;
653 	}
654 
655 fail:
656 	if (error != 0)
657 		stge_detach(dev);
658 
659 	return (error);
660 }
661 
662 static int
663 stge_detach(device_t dev)
664 {
665 	struct stge_softc *sc;
666 	struct ifnet *ifp;
667 
668 	sc = device_get_softc(dev);
669 
670 	ifp = sc->sc_ifp;
671 #ifdef DEVICE_POLLING
672 	if (ifp && ifp->if_capenable & IFCAP_POLLING)
673 		ether_poll_deregister(ifp);
674 #endif
675 	if (device_is_attached(dev)) {
676 		STGE_LOCK(sc);
677 		/* XXX */
678 		sc->sc_detach = 1;
679 		stge_stop(sc);
680 		STGE_UNLOCK(sc);
681 		callout_drain(&sc->sc_tick_ch);
682 		taskqueue_drain(taskqueue_swi, &sc->sc_link_task);
683 		ether_ifdetach(ifp);
684 	}
685 
686 	if (sc->sc_miibus != NULL) {
687 		device_delete_child(dev, sc->sc_miibus);
688 		sc->sc_miibus = NULL;
689 	}
690 	bus_generic_detach(dev);
691 	stge_dma_free(sc);
692 
693 	if (ifp != NULL) {
694 		if_free(ifp);
695 		sc->sc_ifp = NULL;
696 	}
697 
698 	if (sc->sc_ih) {
699 		bus_teardown_intr(dev, sc->sc_res[1], sc->sc_ih);
700 		sc->sc_ih = NULL;
701 	}
702 
703 	if (sc->sc_spec)
704 		bus_release_resources(dev, sc->sc_spec, sc->sc_res);
705 
706 	mtx_destroy(&sc->sc_mii_mtx);
707 	mtx_destroy(&sc->sc_mtx);
708 
709 	return (0);
710 }
711 
712 struct stge_dmamap_arg {
713 	bus_addr_t	stge_busaddr;
714 };
715 
716 static void
717 stge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
718 {
719 	struct stge_dmamap_arg *ctx;
720 
721 	if (error != 0)
722 		return;
723 
724 	ctx = (struct stge_dmamap_arg *)arg;
725 	ctx->stge_busaddr = segs[0].ds_addr;
726 }
727 
728 static int
729 stge_dma_alloc(struct stge_softc *sc)
730 {
731 	struct stge_dmamap_arg ctx;
732 	struct stge_txdesc *txd;
733 	struct stge_rxdesc *rxd;
734 	int error, i;
735 
736 	/* create parent tag. */
737 	error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev),/* parent */
738 		    1, 0,			/* algnmnt, boundary */
739 		    STGE_DMA_MAXADDR,		/* lowaddr */
740 		    BUS_SPACE_MAXADDR,		/* highaddr */
741 		    NULL, NULL,			/* filter, filterarg */
742 		    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
743 		    0,				/* nsegments */
744 		    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
745 		    0,				/* flags */
746 		    NULL, NULL,			/* lockfunc, lockarg */
747 		    &sc->sc_cdata.stge_parent_tag);
748 	if (error != 0) {
749 		device_printf(sc->sc_dev, "failed to create parent DMA tag\n");
750 		goto fail;
751 	}
752 	/* create tag for Tx ring. */
753 	error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
754 		    STGE_RING_ALIGN, 0,		/* algnmnt, boundary */
755 		    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
756 		    BUS_SPACE_MAXADDR,		/* highaddr */
757 		    NULL, NULL,			/* filter, filterarg */
758 		    STGE_TX_RING_SZ,		/* maxsize */
759 		    1,				/* nsegments */
760 		    STGE_TX_RING_SZ,		/* maxsegsize */
761 		    0,				/* flags */
762 		    NULL, NULL,			/* lockfunc, lockarg */
763 		    &sc->sc_cdata.stge_tx_ring_tag);
764 	if (error != 0) {
765 		device_printf(sc->sc_dev,
766 		    "failed to allocate Tx ring DMA tag\n");
767 		goto fail;
768 	}
769 
770 	/* create tag for Rx ring. */
771 	error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
772 		    STGE_RING_ALIGN, 0,		/* algnmnt, boundary */
773 		    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
774 		    BUS_SPACE_MAXADDR,		/* highaddr */
775 		    NULL, NULL,			/* filter, filterarg */
776 		    STGE_RX_RING_SZ,		/* maxsize */
777 		    1,				/* nsegments */
778 		    STGE_RX_RING_SZ,		/* maxsegsize */
779 		    0,				/* flags */
780 		    NULL, NULL,			/* lockfunc, lockarg */
781 		    &sc->sc_cdata.stge_rx_ring_tag);
782 	if (error != 0) {
783 		device_printf(sc->sc_dev,
784 		    "failed to allocate Rx ring DMA tag\n");
785 		goto fail;
786 	}
787 
788 	/* create tag for Tx buffers. */
789 	error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
790 		    1, 0,			/* algnmnt, boundary */
791 		    BUS_SPACE_MAXADDR,		/* lowaddr */
792 		    BUS_SPACE_MAXADDR,		/* highaddr */
793 		    NULL, NULL,			/* filter, filterarg */
794 		    MCLBYTES * STGE_MAXTXSEGS,	/* maxsize */
795 		    STGE_MAXTXSEGS,		/* nsegments */
796 		    MCLBYTES,			/* maxsegsize */
797 		    0,				/* flags */
798 		    NULL, NULL,			/* lockfunc, lockarg */
799 		    &sc->sc_cdata.stge_tx_tag);
800 	if (error != 0) {
801 		device_printf(sc->sc_dev, "failed to allocate Tx DMA tag\n");
802 		goto fail;
803 	}
804 
805 	/* create tag for Rx buffers. */
806 	error = bus_dma_tag_create(sc->sc_cdata.stge_parent_tag,/* parent */
807 		    1, 0,			/* algnmnt, boundary */
808 		    BUS_SPACE_MAXADDR,		/* lowaddr */
809 		    BUS_SPACE_MAXADDR,		/* highaddr */
810 		    NULL, NULL,			/* filter, filterarg */
811 		    MCLBYTES,			/* maxsize */
812 		    1,				/* nsegments */
813 		    MCLBYTES,			/* maxsegsize */
814 		    0,				/* flags */
815 		    NULL, NULL,			/* lockfunc, lockarg */
816 		    &sc->sc_cdata.stge_rx_tag);
817 	if (error != 0) {
818 		device_printf(sc->sc_dev, "failed to allocate Rx DMA tag\n");
819 		goto fail;
820 	}
821 
822 	/* allocate DMA'able memory and load the DMA map for Tx ring. */
823 	error = bus_dmamem_alloc(sc->sc_cdata.stge_tx_ring_tag,
824 	    (void **)&sc->sc_rdata.stge_tx_ring, BUS_DMA_NOWAIT |
825 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sc_cdata.stge_tx_ring_map);
826 	if (error != 0) {
827 		device_printf(sc->sc_dev,
828 		    "failed to allocate DMA'able memory for Tx ring\n");
829 		goto fail;
830 	}
831 
832 	ctx.stge_busaddr = 0;
833 	error = bus_dmamap_load(sc->sc_cdata.stge_tx_ring_tag,
834 	    sc->sc_cdata.stge_tx_ring_map, sc->sc_rdata.stge_tx_ring,
835 	    STGE_TX_RING_SZ, stge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
836 	if (error != 0 || ctx.stge_busaddr == 0) {
837 		device_printf(sc->sc_dev,
838 		    "failed to load DMA'able memory for Tx ring\n");
839 		goto fail;
840 	}
841 	sc->sc_rdata.stge_tx_ring_paddr = ctx.stge_busaddr;
842 
843 	/* allocate DMA'able memory and load the DMA map for Rx ring. */
844 	error = bus_dmamem_alloc(sc->sc_cdata.stge_rx_ring_tag,
845 	    (void **)&sc->sc_rdata.stge_rx_ring, BUS_DMA_NOWAIT |
846 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->sc_cdata.stge_rx_ring_map);
847 	if (error != 0) {
848 		device_printf(sc->sc_dev,
849 		    "failed to allocate DMA'able memory for Rx ring\n");
850 		goto fail;
851 	}
852 
853 	ctx.stge_busaddr = 0;
854 	error = bus_dmamap_load(sc->sc_cdata.stge_rx_ring_tag,
855 	    sc->sc_cdata.stge_rx_ring_map, sc->sc_rdata.stge_rx_ring,
856 	    STGE_RX_RING_SZ, stge_dmamap_cb, &ctx, BUS_DMA_NOWAIT);
857 	if (error != 0 || ctx.stge_busaddr == 0) {
858 		device_printf(sc->sc_dev,
859 		    "failed to load DMA'able memory for Rx ring\n");
860 		goto fail;
861 	}
862 	sc->sc_rdata.stge_rx_ring_paddr = ctx.stge_busaddr;
863 
864 	/* create DMA maps for Tx buffers. */
865 	for (i = 0; i < STGE_TX_RING_CNT; i++) {
866 		txd = &sc->sc_cdata.stge_txdesc[i];
867 		txd->tx_m = NULL;
868 		txd->tx_dmamap = 0;
869 		error = bus_dmamap_create(sc->sc_cdata.stge_tx_tag, 0,
870 		    &txd->tx_dmamap);
871 		if (error != 0) {
872 			device_printf(sc->sc_dev,
873 			    "failed to create Tx dmamap\n");
874 			goto fail;
875 		}
876 	}
877 	/* create DMA maps for Rx buffers. */
878 	if ((error = bus_dmamap_create(sc->sc_cdata.stge_rx_tag, 0,
879 	    &sc->sc_cdata.stge_rx_sparemap)) != 0) {
880 		device_printf(sc->sc_dev, "failed to create spare Rx dmamap\n");
881 		goto fail;
882 	}
883 	for (i = 0; i < STGE_RX_RING_CNT; i++) {
884 		rxd = &sc->sc_cdata.stge_rxdesc[i];
885 		rxd->rx_m = NULL;
886 		rxd->rx_dmamap = 0;
887 		error = bus_dmamap_create(sc->sc_cdata.stge_rx_tag, 0,
888 		    &rxd->rx_dmamap);
889 		if (error != 0) {
890 			device_printf(sc->sc_dev,
891 			    "failed to create Rx dmamap\n");
892 			goto fail;
893 		}
894 	}
895 
896 fail:
897 	return (error);
898 }
899 
900 static void
901 stge_dma_free(struct stge_softc *sc)
902 {
903 	struct stge_txdesc *txd;
904 	struct stge_rxdesc *rxd;
905 	int i;
906 
907 	/* Tx ring */
908 	if (sc->sc_cdata.stge_tx_ring_tag) {
909 		if (sc->sc_rdata.stge_tx_ring_paddr)
910 			bus_dmamap_unload(sc->sc_cdata.stge_tx_ring_tag,
911 			    sc->sc_cdata.stge_tx_ring_map);
912 		if (sc->sc_rdata.stge_tx_ring)
913 			bus_dmamem_free(sc->sc_cdata.stge_tx_ring_tag,
914 			    sc->sc_rdata.stge_tx_ring,
915 			    sc->sc_cdata.stge_tx_ring_map);
916 		sc->sc_rdata.stge_tx_ring = NULL;
917 		sc->sc_rdata.stge_tx_ring_paddr = 0;
918 		bus_dma_tag_destroy(sc->sc_cdata.stge_tx_ring_tag);
919 		sc->sc_cdata.stge_tx_ring_tag = NULL;
920 	}
921 	/* Rx ring */
922 	if (sc->sc_cdata.stge_rx_ring_tag) {
923 		if (sc->sc_rdata.stge_rx_ring_paddr)
924 			bus_dmamap_unload(sc->sc_cdata.stge_rx_ring_tag,
925 			    sc->sc_cdata.stge_rx_ring_map);
926 		if (sc->sc_rdata.stge_rx_ring)
927 			bus_dmamem_free(sc->sc_cdata.stge_rx_ring_tag,
928 			    sc->sc_rdata.stge_rx_ring,
929 			    sc->sc_cdata.stge_rx_ring_map);
930 		sc->sc_rdata.stge_rx_ring = NULL;
931 		sc->sc_rdata.stge_rx_ring_paddr = 0;
932 		bus_dma_tag_destroy(sc->sc_cdata.stge_rx_ring_tag);
933 		sc->sc_cdata.stge_rx_ring_tag = NULL;
934 	}
935 	/* Tx buffers */
936 	if (sc->sc_cdata.stge_tx_tag) {
937 		for (i = 0; i < STGE_TX_RING_CNT; i++) {
938 			txd = &sc->sc_cdata.stge_txdesc[i];
939 			if (txd->tx_dmamap) {
940 				bus_dmamap_destroy(sc->sc_cdata.stge_tx_tag,
941 				    txd->tx_dmamap);
942 				txd->tx_dmamap = 0;
943 			}
944 		}
945 		bus_dma_tag_destroy(sc->sc_cdata.stge_tx_tag);
946 		sc->sc_cdata.stge_tx_tag = NULL;
947 	}
948 	/* Rx buffers */
949 	if (sc->sc_cdata.stge_rx_tag) {
950 		for (i = 0; i < STGE_RX_RING_CNT; i++) {
951 			rxd = &sc->sc_cdata.stge_rxdesc[i];
952 			if (rxd->rx_dmamap) {
953 				bus_dmamap_destroy(sc->sc_cdata.stge_rx_tag,
954 				    rxd->rx_dmamap);
955 				rxd->rx_dmamap = 0;
956 			}
957 		}
958 		if (sc->sc_cdata.stge_rx_sparemap) {
959 			bus_dmamap_destroy(sc->sc_cdata.stge_rx_tag,
960 			    sc->sc_cdata.stge_rx_sparemap);
961 			sc->sc_cdata.stge_rx_sparemap = 0;
962 		}
963 		bus_dma_tag_destroy(sc->sc_cdata.stge_rx_tag);
964 		sc->sc_cdata.stge_rx_tag = NULL;
965 	}
966 
967 	if (sc->sc_cdata.stge_parent_tag) {
968 		bus_dma_tag_destroy(sc->sc_cdata.stge_parent_tag);
969 		sc->sc_cdata.stge_parent_tag = NULL;
970 	}
971 }
972 
973 /*
974  * stge_shutdown:
975  *
976  *	Make sure the interface is stopped at reboot time.
977  */
978 static int
979 stge_shutdown(device_t dev)
980 {
981 
982 	return (stge_suspend(dev));
983 }
984 
985 static void
986 stge_setwol(struct stge_softc *sc)
987 {
988 	struct ifnet *ifp;
989 	uint8_t v;
990 
991 	STGE_LOCK_ASSERT(sc);
992 
993 	ifp = sc->sc_ifp;
994 	v = CSR_READ_1(sc, STGE_WakeEvent);
995 	/* Disable all WOL bits. */
996 	v &= ~(WE_WakePktEnable | WE_MagicPktEnable | WE_LinkEventEnable |
997 	    WE_WakeOnLanEnable);
998 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
999 		v |= WE_MagicPktEnable | WE_WakeOnLanEnable;
1000 	CSR_WRITE_1(sc, STGE_WakeEvent, v);
1001 	/* Reset Tx and prevent transmission. */
1002 	CSR_WRITE_4(sc, STGE_AsicCtrl,
1003 	    CSR_READ_4(sc, STGE_AsicCtrl) | AC_TxReset);
1004 	/*
1005 	 * TC9021 automatically reset link speed to 100Mbps when it's put
1006 	 * into sleep so there is no need to try to resetting link speed.
1007 	 */
1008 }
1009 
1010 static int
1011 stge_suspend(device_t dev)
1012 {
1013 	struct stge_softc *sc;
1014 
1015 	sc = device_get_softc(dev);
1016 
1017 	STGE_LOCK(sc);
1018 	stge_stop(sc);
1019 	sc->sc_suspended = 1;
1020 	stge_setwol(sc);
1021 	STGE_UNLOCK(sc);
1022 
1023 	return (0);
1024 }
1025 
1026 static int
1027 stge_resume(device_t dev)
1028 {
1029 	struct stge_softc *sc;
1030 	struct ifnet *ifp;
1031 	uint8_t v;
1032 
1033 	sc = device_get_softc(dev);
1034 
1035 	STGE_LOCK(sc);
1036 	/*
1037 	 * Clear WOL bits, so special frames wouldn't interfere
1038 	 * normal Rx operation anymore.
1039 	 */
1040 	v = CSR_READ_1(sc, STGE_WakeEvent);
1041 	v &= ~(WE_WakePktEnable | WE_MagicPktEnable | WE_LinkEventEnable |
1042 	    WE_WakeOnLanEnable);
1043 	CSR_WRITE_1(sc, STGE_WakeEvent, v);
1044 	ifp = sc->sc_ifp;
1045 	if (ifp->if_flags & IFF_UP)
1046 		stge_init_locked(sc);
1047 
1048 	sc->sc_suspended = 0;
1049 	STGE_UNLOCK(sc);
1050 
1051 	return (0);
1052 }
1053 
1054 static void
1055 stge_dma_wait(struct stge_softc *sc)
1056 {
1057 	int i;
1058 
1059 	for (i = 0; i < STGE_TIMEOUT; i++) {
1060 		DELAY(2);
1061 		if ((CSR_READ_4(sc, STGE_DMACtrl) & DMAC_TxDMAInProg) == 0)
1062 			break;
1063 	}
1064 
1065 	if (i == STGE_TIMEOUT)
1066 		device_printf(sc->sc_dev, "DMA wait timed out\n");
1067 }
1068 
1069 static int
1070 stge_encap(struct stge_softc *sc, struct mbuf **m_head)
1071 {
1072 	struct stge_txdesc *txd;
1073 	struct stge_tfd *tfd;
1074 	struct mbuf *m;
1075 	bus_dma_segment_t txsegs[STGE_MAXTXSEGS];
1076 	int error, i, nsegs, si;
1077 	uint64_t csum_flags, tfc;
1078 
1079 	STGE_LOCK_ASSERT(sc);
1080 
1081 	if ((txd = STAILQ_FIRST(&sc->sc_cdata.stge_txfreeq)) == NULL)
1082 		return (ENOBUFS);
1083 
1084 	error =  bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_tx_tag,
1085 	    txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1086 	if (error == EFBIG) {
1087 		m = m_collapse(*m_head, M_NOWAIT, STGE_MAXTXSEGS);
1088 		if (m == NULL) {
1089 			m_freem(*m_head);
1090 			*m_head = NULL;
1091 			return (ENOMEM);
1092 		}
1093 		*m_head = m;
1094 		error = bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_tx_tag,
1095 		    txd->tx_dmamap, *m_head, txsegs, &nsegs, 0);
1096 		if (error != 0) {
1097 			m_freem(*m_head);
1098 			*m_head = NULL;
1099 			return (error);
1100 		}
1101 	} else if (error != 0)
1102 		return (error);
1103 	if (nsegs == 0) {
1104 		m_freem(*m_head);
1105 		*m_head = NULL;
1106 		return (EIO);
1107 	}
1108 
1109 	m = *m_head;
1110 	csum_flags = 0;
1111 	if ((m->m_pkthdr.csum_flags & STGE_CSUM_FEATURES) != 0) {
1112 		if (m->m_pkthdr.csum_flags & CSUM_IP)
1113 			csum_flags |= TFD_IPChecksumEnable;
1114 		if (m->m_pkthdr.csum_flags & CSUM_TCP)
1115 			csum_flags |= TFD_TCPChecksumEnable;
1116 		else if (m->m_pkthdr.csum_flags & CSUM_UDP)
1117 			csum_flags |= TFD_UDPChecksumEnable;
1118 	}
1119 
1120 	si = sc->sc_cdata.stge_tx_prod;
1121 	tfd = &sc->sc_rdata.stge_tx_ring[si];
1122 	for (i = 0; i < nsegs; i++)
1123 		tfd->tfd_frags[i].frag_word0 =
1124 		    htole64(FRAG_ADDR(txsegs[i].ds_addr) |
1125 		    FRAG_LEN(txsegs[i].ds_len));
1126 	sc->sc_cdata.stge_tx_cnt++;
1127 
1128 	tfc = TFD_FrameId(si) | TFD_WordAlign(TFD_WordAlign_disable) |
1129 	    TFD_FragCount(nsegs) | csum_flags;
1130 	if (sc->sc_cdata.stge_tx_cnt >= STGE_TX_HIWAT)
1131 		tfc |= TFD_TxDMAIndicate;
1132 
1133 	/* Update producer index. */
1134 	sc->sc_cdata.stge_tx_prod = (si + 1) % STGE_TX_RING_CNT;
1135 
1136 	/* Check if we have a VLAN tag to insert. */
1137 	if (m->m_flags & M_VLANTAG)
1138 		tfc |= (TFD_VLANTagInsert | TFD_VID(m->m_pkthdr.ether_vtag));
1139 	tfd->tfd_control = htole64(tfc);
1140 
1141 	/* Update Tx Queue. */
1142 	STAILQ_REMOVE_HEAD(&sc->sc_cdata.stge_txfreeq, tx_q);
1143 	STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txbusyq, txd, tx_q);
1144 	txd->tx_m = m;
1145 
1146 	/* Sync descriptors. */
1147 	bus_dmamap_sync(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap,
1148 	    BUS_DMASYNC_PREWRITE);
1149 	bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1150 	    sc->sc_cdata.stge_tx_ring_map,
1151 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1152 
1153 	return (0);
1154 }
1155 
1156 /*
1157  * stge_start:		[ifnet interface function]
1158  *
1159  *	Start packet transmission on the interface.
1160  */
1161 static void
1162 stge_start(struct ifnet *ifp)
1163 {
1164 	struct stge_softc *sc;
1165 
1166 	sc = ifp->if_softc;
1167 	STGE_LOCK(sc);
1168 	stge_start_locked(ifp);
1169 	STGE_UNLOCK(sc);
1170 }
1171 
1172 static void
1173 stge_start_locked(struct ifnet *ifp)
1174 {
1175         struct stge_softc *sc;
1176         struct mbuf *m_head;
1177 	int enq;
1178 
1179 	sc = ifp->if_softc;
1180 
1181 	STGE_LOCK_ASSERT(sc);
1182 
1183 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
1184 	    IFF_DRV_RUNNING || sc->sc_link == 0)
1185 		return;
1186 
1187 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd); ) {
1188 		if (sc->sc_cdata.stge_tx_cnt >= STGE_TX_HIWAT) {
1189 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1190 			break;
1191 		}
1192 
1193 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
1194 		if (m_head == NULL)
1195 			break;
1196 		/*
1197 		 * Pack the data into the transmit ring. If we
1198 		 * don't have room, set the OACTIVE flag and wait
1199 		 * for the NIC to drain the ring.
1200 		 */
1201 		if (stge_encap(sc, &m_head)) {
1202 			if (m_head == NULL)
1203 				break;
1204 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
1205 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1206 			break;
1207 		}
1208 
1209 		enq++;
1210 		/*
1211 		 * If there's a BPF listener, bounce a copy of this frame
1212 		 * to him.
1213 		 */
1214 		ETHER_BPF_MTAP(ifp, m_head);
1215 	}
1216 
1217 	if (enq > 0) {
1218 		/* Transmit */
1219 		CSR_WRITE_4(sc, STGE_DMACtrl, DMAC_TxDMAPollNow);
1220 
1221 		/* Set a timeout in case the chip goes out to lunch. */
1222 		sc->sc_watchdog_timer = 5;
1223 	}
1224 }
1225 
1226 /*
1227  * stge_watchdog:
1228  *
1229  *	Watchdog timer handler.
1230  */
1231 static void
1232 stge_watchdog(struct stge_softc *sc)
1233 {
1234 	struct ifnet *ifp;
1235 
1236 	STGE_LOCK_ASSERT(sc);
1237 
1238 	if (sc->sc_watchdog_timer == 0 || --sc->sc_watchdog_timer)
1239 		return;
1240 
1241 	ifp = sc->sc_ifp;
1242 	if_printf(sc->sc_ifp, "device timeout\n");
1243 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1244 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1245 	stge_init_locked(sc);
1246 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1247 		stge_start_locked(ifp);
1248 }
1249 
1250 /*
1251  * stge_ioctl:		[ifnet interface function]
1252  *
1253  *	Handle control requests from the operator.
1254  */
1255 static int
1256 stge_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data)
1257 {
1258 	struct stge_softc *sc;
1259 	struct ifreq *ifr;
1260 	struct mii_data *mii;
1261 	int error, mask;
1262 
1263 	sc = ifp->if_softc;
1264 	ifr = (struct ifreq *)data;
1265 	error = 0;
1266 	switch (cmd) {
1267 	case SIOCSIFMTU:
1268 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > STGE_JUMBO_MTU)
1269 			error = EINVAL;
1270 		else if (ifp->if_mtu != ifr->ifr_mtu) {
1271 			ifp->if_mtu = ifr->ifr_mtu;
1272 			STGE_LOCK(sc);
1273 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1274 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1275 				stge_init_locked(sc);
1276 			}
1277 			STGE_UNLOCK(sc);
1278 		}
1279 		break;
1280 	case SIOCSIFFLAGS:
1281 		STGE_LOCK(sc);
1282 		if ((ifp->if_flags & IFF_UP) != 0) {
1283 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1284 				if (((ifp->if_flags ^ sc->sc_if_flags)
1285 				    & IFF_PROMISC) != 0)
1286 					stge_set_filter(sc);
1287 			} else {
1288 				if (sc->sc_detach == 0)
1289 					stge_init_locked(sc);
1290 			}
1291 		} else {
1292 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1293 				stge_stop(sc);
1294 		}
1295 		sc->sc_if_flags = ifp->if_flags;
1296 		STGE_UNLOCK(sc);
1297 		break;
1298 	case SIOCADDMULTI:
1299 	case SIOCDELMULTI:
1300 		STGE_LOCK(sc);
1301 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1302 			stge_set_multi(sc);
1303 		STGE_UNLOCK(sc);
1304 		break;
1305 	case SIOCSIFMEDIA:
1306 	case SIOCGIFMEDIA:
1307 		mii = device_get_softc(sc->sc_miibus);
1308 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1309 		break;
1310 	case SIOCSIFCAP:
1311 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
1312 #ifdef DEVICE_POLLING
1313 		if ((mask & IFCAP_POLLING) != 0) {
1314 			if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
1315 				error = ether_poll_register(stge_poll, ifp);
1316 				if (error != 0)
1317 					break;
1318 				STGE_LOCK(sc);
1319 				CSR_WRITE_2(sc, STGE_IntEnable, 0);
1320 				ifp->if_capenable |= IFCAP_POLLING;
1321 				STGE_UNLOCK(sc);
1322 			} else {
1323 				error = ether_poll_deregister(ifp);
1324 				if (error != 0)
1325 					break;
1326 				STGE_LOCK(sc);
1327 				CSR_WRITE_2(sc, STGE_IntEnable,
1328 				    sc->sc_IntEnable);
1329 				ifp->if_capenable &= ~IFCAP_POLLING;
1330 				STGE_UNLOCK(sc);
1331 			}
1332 		}
1333 #endif
1334 		if ((mask & IFCAP_HWCSUM) != 0) {
1335 			ifp->if_capenable ^= IFCAP_HWCSUM;
1336 			if ((IFCAP_HWCSUM & ifp->if_capenable) != 0 &&
1337 			    (IFCAP_HWCSUM & ifp->if_capabilities) != 0)
1338 				ifp->if_hwassist = STGE_CSUM_FEATURES;
1339 			else
1340 				ifp->if_hwassist = 0;
1341 		}
1342 		if ((mask & IFCAP_WOL) != 0 &&
1343 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
1344 			if ((mask & IFCAP_WOL_MAGIC) != 0)
1345 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
1346 		}
1347 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0) {
1348 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
1349 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
1350 				STGE_LOCK(sc);
1351 				stge_vlan_setup(sc);
1352 				STGE_UNLOCK(sc);
1353 			}
1354 		}
1355 		VLAN_CAPABILITIES(ifp);
1356 		break;
1357 	default:
1358 		error = ether_ioctl(ifp, cmd, data);
1359 		break;
1360 	}
1361 
1362 	return (error);
1363 }
1364 
1365 static void
1366 stge_link_task(void *arg, int pending)
1367 {
1368 	struct stge_softc *sc;
1369 	struct mii_data *mii;
1370 	uint32_t v, ac;
1371 	int i;
1372 
1373 	sc = (struct stge_softc *)arg;
1374 	STGE_LOCK(sc);
1375 
1376 	mii = device_get_softc(sc->sc_miibus);
1377 	if (mii->mii_media_status & IFM_ACTIVE) {
1378 		if (IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE)
1379 			sc->sc_link = 1;
1380 	} else
1381 		sc->sc_link = 0;
1382 
1383 	sc->sc_MACCtrl = 0;
1384 	if (((mii->mii_media_active & IFM_GMASK) & IFM_FDX) != 0)
1385 		sc->sc_MACCtrl |= MC_DuplexSelect;
1386 	if (((mii->mii_media_active & IFM_GMASK) & IFM_ETH_RXPAUSE) != 0)
1387 		sc->sc_MACCtrl |= MC_RxFlowControlEnable;
1388 	if (((mii->mii_media_active & IFM_GMASK) & IFM_ETH_TXPAUSE) != 0)
1389 		sc->sc_MACCtrl |= MC_TxFlowControlEnable;
1390 	/*
1391 	 * Update STGE_MACCtrl register depending on link status.
1392 	 * (duplex, flow control etc)
1393 	 */
1394 	v = ac = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
1395 	v &= ~(MC_DuplexSelect|MC_RxFlowControlEnable|MC_TxFlowControlEnable);
1396 	v |= sc->sc_MACCtrl;
1397 	CSR_WRITE_4(sc, STGE_MACCtrl, v);
1398 	if (((ac ^ sc->sc_MACCtrl) & MC_DuplexSelect) != 0) {
1399 		/* Duplex setting changed, reset Tx/Rx functions. */
1400 		ac = CSR_READ_4(sc, STGE_AsicCtrl);
1401 		ac |= AC_TxReset | AC_RxReset;
1402 		CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1403 		for (i = 0; i < STGE_TIMEOUT; i++) {
1404 			DELAY(100);
1405 			if ((CSR_READ_4(sc, STGE_AsicCtrl) & AC_ResetBusy) == 0)
1406 				break;
1407 		}
1408 		if (i == STGE_TIMEOUT)
1409 			device_printf(sc->sc_dev, "reset failed to complete\n");
1410 	}
1411 	STGE_UNLOCK(sc);
1412 }
1413 
1414 static __inline int
1415 stge_tx_error(struct stge_softc *sc)
1416 {
1417 	uint32_t txstat;
1418 	int error;
1419 
1420 	for (error = 0;;) {
1421 		txstat = CSR_READ_4(sc, STGE_TxStatus);
1422 		if ((txstat & TS_TxComplete) == 0)
1423 			break;
1424 		/* Tx underrun */
1425 		if ((txstat & TS_TxUnderrun) != 0) {
1426 			/*
1427 			 * XXX
1428 			 * There should be a more better way to recover
1429 			 * from Tx underrun instead of a full reset.
1430 			 */
1431 			if (sc->sc_nerr++ < STGE_MAXERR)
1432 				device_printf(sc->sc_dev, "Tx underrun, "
1433 				    "resetting...\n");
1434 			if (sc->sc_nerr == STGE_MAXERR)
1435 				device_printf(sc->sc_dev, "too many errors; "
1436 				    "not reporting any more\n");
1437 			error = -1;
1438 			break;
1439 		}
1440 		/* Maximum/Late collisions, Re-enable Tx MAC. */
1441 		if ((txstat & (TS_MaxCollisions|TS_LateCollision)) != 0)
1442 			CSR_WRITE_4(sc, STGE_MACCtrl,
1443 			    (CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK) |
1444 			    MC_TxEnable);
1445 	}
1446 
1447 	return (error);
1448 }
1449 
1450 /*
1451  * stge_intr:
1452  *
1453  *	Interrupt service routine.
1454  */
1455 static void
1456 stge_intr(void *arg)
1457 {
1458 	struct stge_softc *sc;
1459 	struct ifnet *ifp;
1460 	int reinit;
1461 	uint16_t status;
1462 
1463 	sc = (struct stge_softc *)arg;
1464 	ifp = sc->sc_ifp;
1465 
1466 	STGE_LOCK(sc);
1467 
1468 #ifdef DEVICE_POLLING
1469 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1470 		goto done_locked;
1471 #endif
1472 	status = CSR_READ_2(sc, STGE_IntStatus);
1473 	if (sc->sc_suspended || (status & IS_InterruptStatus) == 0)
1474 		goto done_locked;
1475 
1476 	/* Disable interrupts. */
1477 	for (reinit = 0;;) {
1478 		status = CSR_READ_2(sc, STGE_IntStatusAck);
1479 		status &= sc->sc_IntEnable;
1480 		if (status == 0)
1481 			break;
1482 		/* Host interface errors. */
1483 		if ((status & IS_HostError) != 0) {
1484 			device_printf(sc->sc_dev,
1485 			    "Host interface error, resetting...\n");
1486 			reinit = 1;
1487 			goto force_init;
1488 		}
1489 
1490 		/* Receive interrupts. */
1491 		if ((status & IS_RxDMAComplete) != 0) {
1492 			stge_rxeof(sc);
1493 			if ((status & IS_RFDListEnd) != 0)
1494 				CSR_WRITE_4(sc, STGE_DMACtrl,
1495 				    DMAC_RxDMAPollNow);
1496 		}
1497 
1498 		/* Transmit interrupts. */
1499 		if ((status & (IS_TxDMAComplete | IS_TxComplete)) != 0)
1500 			stge_txeof(sc);
1501 
1502 		/* Transmission errors.*/
1503 		if ((status & IS_TxComplete) != 0) {
1504 			if ((reinit = stge_tx_error(sc)) != 0)
1505 				break;
1506 		}
1507 	}
1508 
1509 force_init:
1510 	if (reinit != 0) {
1511 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1512 		stge_init_locked(sc);
1513 	}
1514 
1515 	/* Re-enable interrupts. */
1516 	CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
1517 
1518 	/* Try to get more packets going. */
1519 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1520 		stge_start_locked(ifp);
1521 
1522 done_locked:
1523 	STGE_UNLOCK(sc);
1524 }
1525 
1526 /*
1527  * stge_txeof:
1528  *
1529  *	Helper; handle transmit interrupts.
1530  */
1531 static void
1532 stge_txeof(struct stge_softc *sc)
1533 {
1534 	struct ifnet *ifp;
1535 	struct stge_txdesc *txd;
1536 	uint64_t control;
1537 	int cons;
1538 
1539 	STGE_LOCK_ASSERT(sc);
1540 
1541 	ifp = sc->sc_ifp;
1542 
1543 	txd = STAILQ_FIRST(&sc->sc_cdata.stge_txbusyq);
1544 	if (txd == NULL)
1545 		return;
1546 	bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1547 	    sc->sc_cdata.stge_tx_ring_map, BUS_DMASYNC_POSTREAD);
1548 
1549 	/*
1550 	 * Go through our Tx list and free mbufs for those
1551 	 * frames which have been transmitted.
1552 	 */
1553 	for (cons = sc->sc_cdata.stge_tx_cons;;
1554 	    cons = (cons + 1) % STGE_TX_RING_CNT) {
1555 		if (sc->sc_cdata.stge_tx_cnt <= 0)
1556 			break;
1557 		control = le64toh(sc->sc_rdata.stge_tx_ring[cons].tfd_control);
1558 		if ((control & TFD_TFDDone) == 0)
1559 			break;
1560 		sc->sc_cdata.stge_tx_cnt--;
1561 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1562 
1563 		bus_dmamap_sync(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap,
1564 		    BUS_DMASYNC_POSTWRITE);
1565 		bus_dmamap_unload(sc->sc_cdata.stge_tx_tag, txd->tx_dmamap);
1566 
1567 		/* Output counter is updated with statistics register */
1568 		m_freem(txd->tx_m);
1569 		txd->tx_m = NULL;
1570 		STAILQ_REMOVE_HEAD(&sc->sc_cdata.stge_txbusyq, tx_q);
1571 		STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txfreeq, txd, tx_q);
1572 		txd = STAILQ_FIRST(&sc->sc_cdata.stge_txbusyq);
1573 	}
1574 	sc->sc_cdata.stge_tx_cons = cons;
1575 	if (sc->sc_cdata.stge_tx_cnt == 0)
1576 		sc->sc_watchdog_timer = 0;
1577 
1578         bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
1579 	    sc->sc_cdata.stge_tx_ring_map,
1580 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1581 }
1582 
1583 static __inline void
1584 stge_discard_rxbuf(struct stge_softc *sc, int idx)
1585 {
1586 	struct stge_rfd *rfd;
1587 
1588 	rfd = &sc->sc_rdata.stge_rx_ring[idx];
1589 	rfd->rfd_status = 0;
1590 }
1591 
1592 #ifndef __NO_STRICT_ALIGNMENT
1593 /*
1594  * It seems that TC9021's DMA engine has alignment restrictions in
1595  * DMA scatter operations. The first DMA segment has no address
1596  * alignment restrictins but the rest should be aligned on 4(?) bytes
1597  * boundary. Otherwise it would corrupt random memory. Since we don't
1598  * know which one is used for the first segment in advance we simply
1599  * don't align at all.
1600  * To avoid copying over an entire frame to align, we allocate a new
1601  * mbuf and copy ethernet header to the new mbuf. The new mbuf is
1602  * prepended into the existing mbuf chain.
1603  */
1604 static __inline struct mbuf *
1605 stge_fixup_rx(struct stge_softc *sc, struct mbuf *m)
1606 {
1607 	struct mbuf *n;
1608 
1609 	n = NULL;
1610 	if (m->m_len <= (MCLBYTES - ETHER_HDR_LEN)) {
1611 		bcopy(m->m_data, m->m_data + ETHER_HDR_LEN, m->m_len);
1612 		m->m_data += ETHER_HDR_LEN;
1613 		n = m;
1614 	} else {
1615 		MGETHDR(n, M_NOWAIT, MT_DATA);
1616 		if (n != NULL) {
1617 			bcopy(m->m_data, n->m_data, ETHER_HDR_LEN);
1618 			m->m_data += ETHER_HDR_LEN;
1619 			m->m_len -= ETHER_HDR_LEN;
1620 			n->m_len = ETHER_HDR_LEN;
1621 			M_MOVE_PKTHDR(n, m);
1622 			n->m_next = m;
1623 		} else
1624 			m_freem(m);
1625 	}
1626 
1627 	return (n);
1628 }
1629 #endif
1630 
1631 /*
1632  * stge_rxeof:
1633  *
1634  *	Helper; handle receive interrupts.
1635  */
1636 static int
1637 stge_rxeof(struct stge_softc *sc)
1638 {
1639 	struct ifnet *ifp;
1640 	struct stge_rxdesc *rxd;
1641 	struct mbuf *mp, *m;
1642 	uint64_t status64;
1643 	uint32_t status;
1644 	int cons, prog, rx_npkts;
1645 
1646 	STGE_LOCK_ASSERT(sc);
1647 
1648 	rx_npkts = 0;
1649 	ifp = sc->sc_ifp;
1650 
1651 	bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
1652 	    sc->sc_cdata.stge_rx_ring_map, BUS_DMASYNC_POSTREAD);
1653 
1654 	prog = 0;
1655 	for (cons = sc->sc_cdata.stge_rx_cons; prog < STGE_RX_RING_CNT;
1656 	    prog++, cons = (cons + 1) % STGE_RX_RING_CNT) {
1657 		status64 = le64toh(sc->sc_rdata.stge_rx_ring[cons].rfd_status);
1658 		status = RFD_RxStatus(status64);
1659 		if ((status & RFD_RFDDone) == 0)
1660 			break;
1661 #ifdef DEVICE_POLLING
1662 		if (ifp->if_capenable & IFCAP_POLLING) {
1663 			if (sc->sc_cdata.stge_rxcycles <= 0)
1664 				break;
1665 			sc->sc_cdata.stge_rxcycles--;
1666 		}
1667 #endif
1668 		prog++;
1669 		rxd = &sc->sc_cdata.stge_rxdesc[cons];
1670 		mp = rxd->rx_m;
1671 
1672 		/*
1673 		 * If the packet had an error, drop it.  Note we count
1674 		 * the error later in the periodic stats update.
1675 		 */
1676 		if ((status & RFD_FrameEnd) != 0 && (status &
1677 		    (RFD_RxFIFOOverrun | RFD_RxRuntFrame |
1678 		    RFD_RxAlignmentError | RFD_RxFCSError |
1679 		    RFD_RxLengthError)) != 0) {
1680 			stge_discard_rxbuf(sc, cons);
1681 			if (sc->sc_cdata.stge_rxhead != NULL) {
1682 				m_freem(sc->sc_cdata.stge_rxhead);
1683 				STGE_RXCHAIN_RESET(sc);
1684 			}
1685 			continue;
1686 		}
1687 		/*
1688 		 * Add a new receive buffer to the ring.
1689 		 */
1690 		if (stge_newbuf(sc, cons) != 0) {
1691 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1692 			stge_discard_rxbuf(sc, cons);
1693 			if (sc->sc_cdata.stge_rxhead != NULL) {
1694 				m_freem(sc->sc_cdata.stge_rxhead);
1695 				STGE_RXCHAIN_RESET(sc);
1696 			}
1697 			continue;
1698 		}
1699 
1700 		if ((status & RFD_FrameEnd) != 0)
1701 			mp->m_len = RFD_RxDMAFrameLen(status) -
1702 			    sc->sc_cdata.stge_rxlen;
1703 		sc->sc_cdata.stge_rxlen += mp->m_len;
1704 
1705 		/* Chain mbufs. */
1706 		if (sc->sc_cdata.stge_rxhead == NULL) {
1707 			sc->sc_cdata.stge_rxhead = mp;
1708 			sc->sc_cdata.stge_rxtail = mp;
1709 		} else {
1710 			mp->m_flags &= ~M_PKTHDR;
1711 			sc->sc_cdata.stge_rxtail->m_next = mp;
1712 			sc->sc_cdata.stge_rxtail = mp;
1713 		}
1714 
1715 		if ((status & RFD_FrameEnd) != 0) {
1716 			m = sc->sc_cdata.stge_rxhead;
1717 			m->m_pkthdr.rcvif = ifp;
1718 			m->m_pkthdr.len = sc->sc_cdata.stge_rxlen;
1719 
1720 			if (m->m_pkthdr.len > sc->sc_if_framesize) {
1721 				m_freem(m);
1722 				STGE_RXCHAIN_RESET(sc);
1723 				continue;
1724 			}
1725 			/*
1726 			 * Set the incoming checksum information for
1727 			 * the packet.
1728 			 */
1729 			if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1730 				if ((status & RFD_IPDetected) != 0) {
1731 					m->m_pkthdr.csum_flags |=
1732 						CSUM_IP_CHECKED;
1733 					if ((status & RFD_IPError) == 0)
1734 						m->m_pkthdr.csum_flags |=
1735 						    CSUM_IP_VALID;
1736 				}
1737 				if (((status & RFD_TCPDetected) != 0 &&
1738 				    (status & RFD_TCPError) == 0) ||
1739 				    ((status & RFD_UDPDetected) != 0 &&
1740 				    (status & RFD_UDPError) == 0)) {
1741 					m->m_pkthdr.csum_flags |=
1742 					    (CSUM_DATA_VALID | CSUM_PSEUDO_HDR);
1743 					m->m_pkthdr.csum_data = 0xffff;
1744 				}
1745 			}
1746 
1747 #ifndef __NO_STRICT_ALIGNMENT
1748 			if (sc->sc_if_framesize > (MCLBYTES - ETHER_ALIGN)) {
1749 				if ((m = stge_fixup_rx(sc, m)) == NULL) {
1750 					STGE_RXCHAIN_RESET(sc);
1751 					continue;
1752 				}
1753 			}
1754 #endif
1755 			/* Check for VLAN tagged packets. */
1756 			if ((status & RFD_VLANDetected) != 0 &&
1757 			    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
1758 				m->m_pkthdr.ether_vtag = RFD_TCI(status64);
1759 				m->m_flags |= M_VLANTAG;
1760 			}
1761 
1762 			STGE_UNLOCK(sc);
1763 			/* Pass it on. */
1764 			(*ifp->if_input)(ifp, m);
1765 			STGE_LOCK(sc);
1766 			rx_npkts++;
1767 
1768 			STGE_RXCHAIN_RESET(sc);
1769 		}
1770 	}
1771 
1772 	if (prog > 0) {
1773 		/* Update the consumer index. */
1774 		sc->sc_cdata.stge_rx_cons = cons;
1775 		bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
1776 		    sc->sc_cdata.stge_rx_ring_map,
1777 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1778 	}
1779 	return (rx_npkts);
1780 }
1781 
1782 #ifdef DEVICE_POLLING
1783 static int
1784 stge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1785 {
1786 	struct stge_softc *sc;
1787 	uint16_t status;
1788 	int rx_npkts;
1789 
1790 	rx_npkts = 0;
1791 	sc = ifp->if_softc;
1792 	STGE_LOCK(sc);
1793 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1794 		STGE_UNLOCK(sc);
1795 		return (rx_npkts);
1796 	}
1797 
1798 	sc->sc_cdata.stge_rxcycles = count;
1799 	rx_npkts = stge_rxeof(sc);
1800 	stge_txeof(sc);
1801 
1802 	if (cmd == POLL_AND_CHECK_STATUS) {
1803 		status = CSR_READ_2(sc, STGE_IntStatus);
1804 		status &= sc->sc_IntEnable;
1805 		if (status != 0) {
1806 			if ((status & IS_HostError) != 0) {
1807 				device_printf(sc->sc_dev,
1808 				    "Host interface error, resetting...\n");
1809 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1810 				stge_init_locked(sc);
1811 			}
1812 			if ((status & IS_TxComplete) != 0) {
1813 				if (stge_tx_error(sc) != 0) {
1814 					ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1815 					stge_init_locked(sc);
1816 				}
1817 			}
1818 		}
1819 	}
1820 
1821 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1822 		stge_start_locked(ifp);
1823 
1824 	STGE_UNLOCK(sc);
1825 	return (rx_npkts);
1826 }
1827 #endif	/* DEVICE_POLLING */
1828 
1829 /*
1830  * stge_tick:
1831  *
1832  *	One second timer, used to tick the MII.
1833  */
1834 static void
1835 stge_tick(void *arg)
1836 {
1837 	struct stge_softc *sc;
1838 	struct mii_data *mii;
1839 
1840 	sc = (struct stge_softc *)arg;
1841 
1842 	STGE_LOCK_ASSERT(sc);
1843 
1844 	mii = device_get_softc(sc->sc_miibus);
1845 	mii_tick(mii);
1846 
1847 	/* Update statistics counters. */
1848 	stge_stats_update(sc);
1849 
1850 	/*
1851 	 * Relcaim any pending Tx descriptors to release mbufs in a
1852 	 * timely manner as we don't generate Tx completion interrupts
1853 	 * for every frame. This limits the delay to a maximum of one
1854 	 * second.
1855 	 */
1856 	if (sc->sc_cdata.stge_tx_cnt != 0)
1857 		stge_txeof(sc);
1858 
1859 	stge_watchdog(sc);
1860 
1861 	callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
1862 }
1863 
1864 /*
1865  * stge_stats_update:
1866  *
1867  *	Read the TC9021 statistics counters.
1868  */
1869 static void
1870 stge_stats_update(struct stge_softc *sc)
1871 {
1872 	struct ifnet *ifp;
1873 
1874 	STGE_LOCK_ASSERT(sc);
1875 
1876 	ifp = sc->sc_ifp;
1877 
1878 	CSR_READ_4(sc,STGE_OctetRcvOk);
1879 
1880 	if_inc_counter(ifp, IFCOUNTER_IPACKETS, CSR_READ_4(sc, STGE_FramesRcvdOk));
1881 
1882 	if_inc_counter(ifp, IFCOUNTER_IERRORS, CSR_READ_2(sc, STGE_FramesLostRxErrors));
1883 
1884 	CSR_READ_4(sc, STGE_OctetXmtdOk);
1885 
1886 	if_inc_counter(ifp, IFCOUNTER_OPACKETS, CSR_READ_4(sc, STGE_FramesXmtdOk));
1887 
1888 	if_inc_counter(ifp, IFCOUNTER_COLLISIONS,
1889 	    CSR_READ_4(sc, STGE_LateCollisions) +
1890 	    CSR_READ_4(sc, STGE_MultiColFrames) +
1891 	    CSR_READ_4(sc, STGE_SingleColFrames));
1892 
1893 	if_inc_counter(ifp, IFCOUNTER_OERRORS,
1894 	    CSR_READ_2(sc, STGE_FramesAbortXSColls) +
1895 	    CSR_READ_2(sc, STGE_FramesWEXDeferal));
1896 }
1897 
1898 /*
1899  * stge_reset:
1900  *
1901  *	Perform a soft reset on the TC9021.
1902  */
1903 static void
1904 stge_reset(struct stge_softc *sc, uint32_t how)
1905 {
1906 	uint32_t ac;
1907 	uint8_t v;
1908 	int i, dv;
1909 
1910 	STGE_LOCK_ASSERT(sc);
1911 
1912 	dv = 5000;
1913 	ac = CSR_READ_4(sc, STGE_AsicCtrl);
1914 	switch (how) {
1915 	case STGE_RESET_TX:
1916 		ac |= AC_TxReset | AC_FIFO;
1917 		dv = 100;
1918 		break;
1919 	case STGE_RESET_RX:
1920 		ac |= AC_RxReset | AC_FIFO;
1921 		dv = 100;
1922 		break;
1923 	case STGE_RESET_FULL:
1924 	default:
1925 		/*
1926 		 * Only assert RstOut if we're fiber.  We need GMII clocks
1927 		 * to be present in order for the reset to complete on fiber
1928 		 * cards.
1929 		 */
1930 		ac |= AC_GlobalReset | AC_RxReset | AC_TxReset |
1931 		    AC_DMA | AC_FIFO | AC_Network | AC_Host | AC_AutoInit |
1932 		    (sc->sc_usefiber ? AC_RstOut : 0);
1933 		break;
1934 	}
1935 
1936 	CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1937 
1938 	/* Account for reset problem at 10Mbps. */
1939 	DELAY(dv);
1940 
1941 	for (i = 0; i < STGE_TIMEOUT; i++) {
1942 		if ((CSR_READ_4(sc, STGE_AsicCtrl) & AC_ResetBusy) == 0)
1943 			break;
1944 		DELAY(dv);
1945 	}
1946 
1947 	if (i == STGE_TIMEOUT)
1948 		device_printf(sc->sc_dev, "reset failed to complete\n");
1949 
1950 	/* Set LED, from Linux IPG driver. */
1951 	ac = CSR_READ_4(sc, STGE_AsicCtrl);
1952 	ac &= ~(AC_LEDMode | AC_LEDSpeed | AC_LEDModeBit1);
1953 	if ((sc->sc_led & 0x01) != 0)
1954 		ac |= AC_LEDMode;
1955 	if ((sc->sc_led & 0x03) != 0)
1956 		ac |= AC_LEDModeBit1;
1957 	if ((sc->sc_led & 0x08) != 0)
1958 		ac |= AC_LEDSpeed;
1959 	CSR_WRITE_4(sc, STGE_AsicCtrl, ac);
1960 
1961 	/* Set PHY, from Linux IPG driver */
1962 	v = CSR_READ_1(sc, STGE_PhySet);
1963 	v &= ~(PS_MemLenb9b | PS_MemLen | PS_NonCompdet);
1964 	v |= ((sc->sc_led & 0x70) >> 4);
1965 	CSR_WRITE_1(sc, STGE_PhySet, v);
1966 }
1967 
1968 /*
1969  * stge_init:		[ ifnet interface function ]
1970  *
1971  *	Initialize the interface.
1972  */
1973 static void
1974 stge_init(void *xsc)
1975 {
1976 	struct stge_softc *sc;
1977 
1978 	sc = (struct stge_softc *)xsc;
1979 	STGE_LOCK(sc);
1980 	stge_init_locked(sc);
1981 	STGE_UNLOCK(sc);
1982 }
1983 
1984 static void
1985 stge_init_locked(struct stge_softc *sc)
1986 {
1987 	struct ifnet *ifp;
1988 	struct mii_data *mii;
1989 	uint16_t eaddr[3];
1990 	uint32_t v;
1991 	int error;
1992 
1993 	STGE_LOCK_ASSERT(sc);
1994 
1995 	ifp = sc->sc_ifp;
1996 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
1997 		return;
1998 	mii = device_get_softc(sc->sc_miibus);
1999 
2000 	/*
2001 	 * Cancel any pending I/O.
2002 	 */
2003 	stge_stop(sc);
2004 
2005 	/*
2006 	 * Reset the chip to a known state.
2007 	 */
2008 	stge_reset(sc, STGE_RESET_FULL);
2009 
2010 	/* Init descriptors. */
2011 	error = stge_init_rx_ring(sc);
2012         if (error != 0) {
2013                 device_printf(sc->sc_dev,
2014                     "initialization failed: no memory for rx buffers\n");
2015                 stge_stop(sc);
2016 		goto out;
2017         }
2018 	stge_init_tx_ring(sc);
2019 
2020 	/* Set the station address. */
2021 	bcopy(IF_LLADDR(ifp), eaddr, ETHER_ADDR_LEN);
2022 	CSR_WRITE_2(sc, STGE_StationAddress0, htole16(eaddr[0]));
2023 	CSR_WRITE_2(sc, STGE_StationAddress1, htole16(eaddr[1]));
2024 	CSR_WRITE_2(sc, STGE_StationAddress2, htole16(eaddr[2]));
2025 
2026 	/*
2027 	 * Set the statistics masks.  Disable all the RMON stats,
2028 	 * and disable selected stats in the non-RMON stats registers.
2029 	 */
2030 	CSR_WRITE_4(sc, STGE_RMONStatisticsMask, 0xffffffff);
2031 	CSR_WRITE_4(sc, STGE_StatisticsMask,
2032 	    (1U << 1) | (1U << 2) | (1U << 3) | (1U << 4) | (1U << 5) |
2033 	    (1U << 6) | (1U << 7) | (1U << 8) | (1U << 9) | (1U << 10) |
2034 	    (1U << 13) | (1U << 14) | (1U << 15) | (1U << 19) | (1U << 20) |
2035 	    (1U << 21));
2036 
2037 	/* Set up the receive filter. */
2038 	stge_set_filter(sc);
2039 	/* Program multicast filter. */
2040 	stge_set_multi(sc);
2041 
2042 	/*
2043 	 * Give the transmit and receive ring to the chip.
2044 	 */
2045 	CSR_WRITE_4(sc, STGE_TFDListPtrHi,
2046 	    STGE_ADDR_HI(STGE_TX_RING_ADDR(sc, 0)));
2047 	CSR_WRITE_4(sc, STGE_TFDListPtrLo,
2048 	    STGE_ADDR_LO(STGE_TX_RING_ADDR(sc, 0)));
2049 
2050 	CSR_WRITE_4(sc, STGE_RFDListPtrHi,
2051 	    STGE_ADDR_HI(STGE_RX_RING_ADDR(sc, 0)));
2052 	CSR_WRITE_4(sc, STGE_RFDListPtrLo,
2053 	    STGE_ADDR_LO(STGE_RX_RING_ADDR(sc, 0)));
2054 
2055 	/*
2056 	 * Initialize the Tx auto-poll period.  It's OK to make this number
2057 	 * large (255 is the max, but we use 127) -- we explicitly kick the
2058 	 * transmit engine when there's actually a packet.
2059 	 */
2060 	CSR_WRITE_1(sc, STGE_TxDMAPollPeriod, 127);
2061 
2062 	/* ..and the Rx auto-poll period. */
2063 	CSR_WRITE_1(sc, STGE_RxDMAPollPeriod, 1);
2064 
2065 	/* Initialize the Tx start threshold. */
2066 	CSR_WRITE_2(sc, STGE_TxStartThresh, sc->sc_txthresh);
2067 
2068 	/* Rx DMA thresholds, from Linux */
2069 	CSR_WRITE_1(sc, STGE_RxDMABurstThresh, 0x30);
2070 	CSR_WRITE_1(sc, STGE_RxDMAUrgentThresh, 0x30);
2071 
2072 	/* Rx early threhold, from Linux */
2073 	CSR_WRITE_2(sc, STGE_RxEarlyThresh, 0x7ff);
2074 
2075 	/* Tx DMA thresholds, from Linux */
2076 	CSR_WRITE_1(sc, STGE_TxDMABurstThresh, 0x30);
2077 	CSR_WRITE_1(sc, STGE_TxDMAUrgentThresh, 0x04);
2078 
2079 	/*
2080 	 * Initialize the Rx DMA interrupt control register.  We
2081 	 * request an interrupt after every incoming packet, but
2082 	 * defer it for sc_rxint_dmawait us. When the number of
2083 	 * interrupts pending reaches STGE_RXINT_NFRAME, we stop
2084 	 * deferring the interrupt, and signal it immediately.
2085 	 */
2086 	CSR_WRITE_4(sc, STGE_RxDMAIntCtrl,
2087 	    RDIC_RxFrameCount(sc->sc_rxint_nframe) |
2088 	    RDIC_RxDMAWaitTime(STGE_RXINT_USECS2TICK(sc->sc_rxint_dmawait)));
2089 
2090 	/*
2091 	 * Initialize the interrupt mask.
2092 	 */
2093 	sc->sc_IntEnable = IS_HostError | IS_TxComplete |
2094 	    IS_TxDMAComplete | IS_RxDMAComplete | IS_RFDListEnd;
2095 #ifdef DEVICE_POLLING
2096 	/* Disable interrupts if we are polling. */
2097 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
2098 		CSR_WRITE_2(sc, STGE_IntEnable, 0);
2099 	else
2100 #endif
2101 	CSR_WRITE_2(sc, STGE_IntEnable, sc->sc_IntEnable);
2102 
2103 	/*
2104 	 * Configure the DMA engine.
2105 	 * XXX Should auto-tune TxBurstLimit.
2106 	 */
2107 	CSR_WRITE_4(sc, STGE_DMACtrl, sc->sc_DMACtrl | DMAC_TxBurstLimit(3));
2108 
2109 	/*
2110 	 * Send a PAUSE frame when we reach 29,696 bytes in the Rx
2111 	 * FIFO, and send an un-PAUSE frame when we reach 3056 bytes
2112 	 * in the Rx FIFO.
2113 	 */
2114 	CSR_WRITE_2(sc, STGE_FlowOnTresh, 29696 / 16);
2115 	CSR_WRITE_2(sc, STGE_FlowOffThresh, 3056 / 16);
2116 
2117 	/*
2118 	 * Set the maximum frame size.
2119 	 */
2120 	sc->sc_if_framesize = ifp->if_mtu + ETHER_HDR_LEN + ETHER_CRC_LEN;
2121 	CSR_WRITE_2(sc, STGE_MaxFrameSize, sc->sc_if_framesize);
2122 
2123 	/*
2124 	 * Initialize MacCtrl -- do it before setting the media,
2125 	 * as setting the media will actually program the register.
2126 	 *
2127 	 * Note: We have to poke the IFS value before poking
2128 	 * anything else.
2129 	 */
2130 	/* Tx/Rx MAC should be disabled before programming IFS.*/
2131 	CSR_WRITE_4(sc, STGE_MACCtrl, MC_IFSSelect(MC_IFS96bit));
2132 
2133 	stge_vlan_setup(sc);
2134 
2135 	if (sc->sc_rev >= 6) {		/* >= B.2 */
2136 		/* Multi-frag frame bug work-around. */
2137 		CSR_WRITE_2(sc, STGE_DebugCtrl,
2138 		    CSR_READ_2(sc, STGE_DebugCtrl) | 0x0200);
2139 
2140 		/* Tx Poll Now bug work-around. */
2141 		CSR_WRITE_2(sc, STGE_DebugCtrl,
2142 		    CSR_READ_2(sc, STGE_DebugCtrl) | 0x0010);
2143 		/* Tx Poll Now bug work-around. */
2144 		CSR_WRITE_2(sc, STGE_DebugCtrl,
2145 		    CSR_READ_2(sc, STGE_DebugCtrl) | 0x0020);
2146 	}
2147 
2148 	v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2149 	v |= MC_StatisticsEnable | MC_TxEnable | MC_RxEnable;
2150 	CSR_WRITE_4(sc, STGE_MACCtrl, v);
2151 	/*
2152 	 * It seems that transmitting frames without checking the state of
2153 	 * Rx/Tx MAC wedge the hardware.
2154 	 */
2155 	stge_start_tx(sc);
2156 	stge_start_rx(sc);
2157 
2158 	sc->sc_link = 0;
2159 	/*
2160 	 * Set the current media.
2161 	 */
2162 	mii_mediachg(mii);
2163 
2164 	/*
2165 	 * Start the one second MII clock.
2166 	 */
2167 	callout_reset(&sc->sc_tick_ch, hz, stge_tick, sc);
2168 
2169 	/*
2170 	 * ...all done!
2171 	 */
2172 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
2173 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2174 
2175  out:
2176 	if (error != 0)
2177 		device_printf(sc->sc_dev, "interface not running\n");
2178 }
2179 
2180 static void
2181 stge_vlan_setup(struct stge_softc *sc)
2182 {
2183 	struct ifnet *ifp;
2184 	uint32_t v;
2185 
2186 	ifp = sc->sc_ifp;
2187 	/*
2188 	 * The NIC always copy a VLAN tag regardless of STGE_MACCtrl
2189 	 * MC_AutoVLANuntagging bit.
2190 	 * MC_AutoVLANtagging bit selects which VLAN source to use
2191 	 * between STGE_VLANTag and TFC. However TFC TFD_VLANTagInsert
2192 	 * bit has priority over MC_AutoVLANtagging bit. So we always
2193 	 * use TFC instead of STGE_VLANTag register.
2194 	 */
2195 	v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2196 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
2197 		v |= MC_AutoVLANuntagging;
2198 	else
2199 		v &= ~MC_AutoVLANuntagging;
2200 	CSR_WRITE_4(sc, STGE_MACCtrl, v);
2201 }
2202 
2203 /*
2204  *	Stop transmission on the interface.
2205  */
2206 static void
2207 stge_stop(struct stge_softc *sc)
2208 {
2209 	struct ifnet *ifp;
2210 	struct stge_txdesc *txd;
2211 	struct stge_rxdesc *rxd;
2212 	uint32_t v;
2213 	int i;
2214 
2215 	STGE_LOCK_ASSERT(sc);
2216 	/*
2217 	 * Stop the one second clock.
2218 	 */
2219 	callout_stop(&sc->sc_tick_ch);
2220 	sc->sc_watchdog_timer = 0;
2221 
2222 	/*
2223 	 * Disable interrupts.
2224 	 */
2225 	CSR_WRITE_2(sc, STGE_IntEnable, 0);
2226 
2227 	/*
2228 	 * Stop receiver, transmitter, and stats update.
2229 	 */
2230 	stge_stop_rx(sc);
2231 	stge_stop_tx(sc);
2232 	v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2233 	v |= MC_StatisticsDisable;
2234 	CSR_WRITE_4(sc, STGE_MACCtrl, v);
2235 
2236 	/*
2237 	 * Stop the transmit and receive DMA.
2238 	 */
2239 	stge_dma_wait(sc);
2240 	CSR_WRITE_4(sc, STGE_TFDListPtrHi, 0);
2241 	CSR_WRITE_4(sc, STGE_TFDListPtrLo, 0);
2242 	CSR_WRITE_4(sc, STGE_RFDListPtrHi, 0);
2243 	CSR_WRITE_4(sc, STGE_RFDListPtrLo, 0);
2244 
2245 	/*
2246 	 * Free RX and TX mbufs still in the queues.
2247 	 */
2248 	for (i = 0; i < STGE_RX_RING_CNT; i++) {
2249 		rxd = &sc->sc_cdata.stge_rxdesc[i];
2250 		if (rxd->rx_m != NULL) {
2251 			bus_dmamap_sync(sc->sc_cdata.stge_rx_tag,
2252 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2253 			bus_dmamap_unload(sc->sc_cdata.stge_rx_tag,
2254 			    rxd->rx_dmamap);
2255 			m_freem(rxd->rx_m);
2256 			rxd->rx_m = NULL;
2257 		}
2258         }
2259 	for (i = 0; i < STGE_TX_RING_CNT; i++) {
2260 		txd = &sc->sc_cdata.stge_txdesc[i];
2261 		if (txd->tx_m != NULL) {
2262 			bus_dmamap_sync(sc->sc_cdata.stge_tx_tag,
2263 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2264 			bus_dmamap_unload(sc->sc_cdata.stge_tx_tag,
2265 			    txd->tx_dmamap);
2266 			m_freem(txd->tx_m);
2267 			txd->tx_m = NULL;
2268 		}
2269         }
2270 
2271 	/*
2272 	 * Mark the interface down and cancel the watchdog timer.
2273 	 */
2274 	ifp = sc->sc_ifp;
2275 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2276 	sc->sc_link = 0;
2277 }
2278 
2279 static void
2280 stge_start_tx(struct stge_softc *sc)
2281 {
2282 	uint32_t v;
2283 	int i;
2284 
2285 	v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2286 	if ((v & MC_TxEnabled) != 0)
2287 		return;
2288 	v |= MC_TxEnable;
2289 	CSR_WRITE_4(sc, STGE_MACCtrl, v);
2290 	CSR_WRITE_1(sc, STGE_TxDMAPollPeriod, 127);
2291 	for (i = STGE_TIMEOUT; i > 0; i--) {
2292 		DELAY(10);
2293 		v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2294 		if ((v & MC_TxEnabled) != 0)
2295 			break;
2296 	}
2297 	if (i == 0)
2298 		device_printf(sc->sc_dev, "Starting Tx MAC timed out\n");
2299 }
2300 
2301 static void
2302 stge_start_rx(struct stge_softc *sc)
2303 {
2304 	uint32_t v;
2305 	int i;
2306 
2307 	v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2308 	if ((v & MC_RxEnabled) != 0)
2309 		return;
2310 	v |= MC_RxEnable;
2311 	CSR_WRITE_4(sc, STGE_MACCtrl, v);
2312 	CSR_WRITE_1(sc, STGE_RxDMAPollPeriod, 1);
2313 	for (i = STGE_TIMEOUT; i > 0; i--) {
2314 		DELAY(10);
2315 		v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2316 		if ((v & MC_RxEnabled) != 0)
2317 			break;
2318 	}
2319 	if (i == 0)
2320 		device_printf(sc->sc_dev, "Starting Rx MAC timed out\n");
2321 }
2322 
2323 static void
2324 stge_stop_tx(struct stge_softc *sc)
2325 {
2326 	uint32_t v;
2327 	int i;
2328 
2329 	v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2330 	if ((v & MC_TxEnabled) == 0)
2331 		return;
2332 	v |= MC_TxDisable;
2333 	CSR_WRITE_4(sc, STGE_MACCtrl, v);
2334 	for (i = STGE_TIMEOUT; i > 0; i--) {
2335 		DELAY(10);
2336 		v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2337 		if ((v & MC_TxEnabled) == 0)
2338 			break;
2339 	}
2340 	if (i == 0)
2341 		device_printf(sc->sc_dev, "Stopping Tx MAC timed out\n");
2342 }
2343 
2344 static void
2345 stge_stop_rx(struct stge_softc *sc)
2346 {
2347 	uint32_t v;
2348 	int i;
2349 
2350 	v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2351 	if ((v & MC_RxEnabled) == 0)
2352 		return;
2353 	v |= MC_RxDisable;
2354 	CSR_WRITE_4(sc, STGE_MACCtrl, v);
2355 	for (i = STGE_TIMEOUT; i > 0; i--) {
2356 		DELAY(10);
2357 		v = CSR_READ_4(sc, STGE_MACCtrl) & MC_MASK;
2358 		if ((v & MC_RxEnabled) == 0)
2359 			break;
2360 	}
2361 	if (i == 0)
2362 		device_printf(sc->sc_dev, "Stopping Rx MAC timed out\n");
2363 }
2364 
2365 static void
2366 stge_init_tx_ring(struct stge_softc *sc)
2367 {
2368 	struct stge_ring_data *rd;
2369 	struct stge_txdesc *txd;
2370 	bus_addr_t addr;
2371 	int i;
2372 
2373 	STAILQ_INIT(&sc->sc_cdata.stge_txfreeq);
2374 	STAILQ_INIT(&sc->sc_cdata.stge_txbusyq);
2375 
2376 	sc->sc_cdata.stge_tx_prod = 0;
2377 	sc->sc_cdata.stge_tx_cons = 0;
2378 	sc->sc_cdata.stge_tx_cnt = 0;
2379 
2380 	rd = &sc->sc_rdata;
2381 	bzero(rd->stge_tx_ring, STGE_TX_RING_SZ);
2382 	for (i = 0; i < STGE_TX_RING_CNT; i++) {
2383 		if (i == (STGE_TX_RING_CNT - 1))
2384 			addr = STGE_TX_RING_ADDR(sc, 0);
2385 		else
2386 			addr = STGE_TX_RING_ADDR(sc, i + 1);
2387 		rd->stge_tx_ring[i].tfd_next = htole64(addr);
2388 		rd->stge_tx_ring[i].tfd_control = htole64(TFD_TFDDone);
2389 		txd = &sc->sc_cdata.stge_txdesc[i];
2390 		STAILQ_INSERT_TAIL(&sc->sc_cdata.stge_txfreeq, txd, tx_q);
2391 	}
2392 
2393 	bus_dmamap_sync(sc->sc_cdata.stge_tx_ring_tag,
2394 	    sc->sc_cdata.stge_tx_ring_map,
2395 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2396 
2397 }
2398 
2399 static int
2400 stge_init_rx_ring(struct stge_softc *sc)
2401 {
2402 	struct stge_ring_data *rd;
2403 	bus_addr_t addr;
2404 	int i;
2405 
2406 	sc->sc_cdata.stge_rx_cons = 0;
2407 	STGE_RXCHAIN_RESET(sc);
2408 
2409 	rd = &sc->sc_rdata;
2410 	bzero(rd->stge_rx_ring, STGE_RX_RING_SZ);
2411 	for (i = 0; i < STGE_RX_RING_CNT; i++) {
2412 		if (stge_newbuf(sc, i) != 0)
2413 			return (ENOBUFS);
2414 		if (i == (STGE_RX_RING_CNT - 1))
2415 			addr = STGE_RX_RING_ADDR(sc, 0);
2416 		else
2417 			addr = STGE_RX_RING_ADDR(sc, i + 1);
2418 		rd->stge_rx_ring[i].rfd_next = htole64(addr);
2419 		rd->stge_rx_ring[i].rfd_status = 0;
2420 	}
2421 
2422 	bus_dmamap_sync(sc->sc_cdata.stge_rx_ring_tag,
2423 	    sc->sc_cdata.stge_rx_ring_map,
2424 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2425 
2426 	return (0);
2427 }
2428 
2429 /*
2430  * stge_newbuf:
2431  *
2432  *	Add a receive buffer to the indicated descriptor.
2433  */
2434 static int
2435 stge_newbuf(struct stge_softc *sc, int idx)
2436 {
2437 	struct stge_rxdesc *rxd;
2438 	struct stge_rfd *rfd;
2439 	struct mbuf *m;
2440 	bus_dma_segment_t segs[1];
2441 	bus_dmamap_t map;
2442 	int nsegs;
2443 
2444 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
2445 	if (m == NULL)
2446 		return (ENOBUFS);
2447 	m->m_len = m->m_pkthdr.len = MCLBYTES;
2448 	/*
2449 	 * The hardware requires 4bytes aligned DMA address when JUMBO
2450 	 * frame is used.
2451 	 */
2452 	if (sc->sc_if_framesize <= (MCLBYTES - ETHER_ALIGN))
2453 		m_adj(m, ETHER_ALIGN);
2454 
2455 	if (bus_dmamap_load_mbuf_sg(sc->sc_cdata.stge_rx_tag,
2456 	    sc->sc_cdata.stge_rx_sparemap, m, segs, &nsegs, 0) != 0) {
2457 		m_freem(m);
2458 		return (ENOBUFS);
2459 	}
2460 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
2461 
2462 	rxd = &sc->sc_cdata.stge_rxdesc[idx];
2463 	if (rxd->rx_m != NULL) {
2464 		bus_dmamap_sync(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap,
2465 		    BUS_DMASYNC_POSTREAD);
2466 		bus_dmamap_unload(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap);
2467 	}
2468 	map = rxd->rx_dmamap;
2469 	rxd->rx_dmamap = sc->sc_cdata.stge_rx_sparemap;
2470 	sc->sc_cdata.stge_rx_sparemap = map;
2471 	bus_dmamap_sync(sc->sc_cdata.stge_rx_tag, rxd->rx_dmamap,
2472 	    BUS_DMASYNC_PREREAD);
2473 	rxd->rx_m = m;
2474 
2475 	rfd = &sc->sc_rdata.stge_rx_ring[idx];
2476 	rfd->rfd_frag.frag_word0 =
2477 	    htole64(FRAG_ADDR(segs[0].ds_addr) | FRAG_LEN(segs[0].ds_len));
2478 	rfd->rfd_status = 0;
2479 
2480 	return (0);
2481 }
2482 
2483 /*
2484  * stge_set_filter:
2485  *
2486  *	Set up the receive filter.
2487  */
2488 static void
2489 stge_set_filter(struct stge_softc *sc)
2490 {
2491 	struct ifnet *ifp;
2492 	uint16_t mode;
2493 
2494 	STGE_LOCK_ASSERT(sc);
2495 
2496 	ifp = sc->sc_ifp;
2497 
2498 	mode = CSR_READ_2(sc, STGE_ReceiveMode);
2499 	mode |= RM_ReceiveUnicast;
2500 	if ((ifp->if_flags & IFF_BROADCAST) != 0)
2501 		mode |= RM_ReceiveBroadcast;
2502 	else
2503 		mode &= ~RM_ReceiveBroadcast;
2504 	if ((ifp->if_flags & IFF_PROMISC) != 0)
2505 		mode |= RM_ReceiveAllFrames;
2506 	else
2507 		mode &= ~RM_ReceiveAllFrames;
2508 
2509 	CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2510 }
2511 
2512 static u_int
2513 stge_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
2514 {
2515 	uint32_t crc, *mchash = arg;
2516 
2517 	crc = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN);
2518 	/* Just want the 6 least significant bits. */
2519 	crc &= 0x3f;
2520 	/* Set the corresponding bit in the hash table. */
2521 	mchash[crc >> 5] |= 1 << (crc & 0x1f);
2522 
2523 	return (1);
2524 }
2525 
2526 static void
2527 stge_set_multi(struct stge_softc *sc)
2528 {
2529 	struct ifnet *ifp;
2530 	uint32_t mchash[2];
2531 	uint16_t mode;
2532 	int count;
2533 
2534 	STGE_LOCK_ASSERT(sc);
2535 
2536 	ifp = sc->sc_ifp;
2537 
2538 	mode = CSR_READ_2(sc, STGE_ReceiveMode);
2539 	if ((ifp->if_flags & (IFF_PROMISC | IFF_ALLMULTI)) != 0) {
2540 		if ((ifp->if_flags & IFF_PROMISC) != 0)
2541 			mode |= RM_ReceiveAllFrames;
2542 		else if ((ifp->if_flags & IFF_ALLMULTI) != 0)
2543 			mode |= RM_ReceiveMulticast;
2544 		CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2545 		return;
2546 	}
2547 
2548 	/* clear existing filters. */
2549 	CSR_WRITE_4(sc, STGE_HashTable0, 0);
2550 	CSR_WRITE_4(sc, STGE_HashTable1, 0);
2551 
2552 	/*
2553 	 * Set up the multicast address filter by passing all multicast
2554 	 * addresses through a CRC generator, and then using the low-order
2555 	 * 6 bits as an index into the 64 bit multicast hash table.  The
2556 	 * high order bits select the register, while the rest of the bits
2557 	 * select the bit within the register.
2558 	 */
2559 	bzero(mchash, sizeof(mchash));
2560 	count = if_foreach_llmaddr(ifp, stge_hash_maddr, mchash);
2561 
2562 	mode &= ~(RM_ReceiveMulticast | RM_ReceiveAllFrames);
2563 	if (count > 0)
2564 		mode |= RM_ReceiveMulticastHash;
2565 	else
2566 		mode &= ~RM_ReceiveMulticastHash;
2567 
2568 	CSR_WRITE_4(sc, STGE_HashTable0, mchash[0]);
2569 	CSR_WRITE_4(sc, STGE_HashTable1, mchash[1]);
2570 	CSR_WRITE_2(sc, STGE_ReceiveMode, mode);
2571 }
2572 
2573 static int
2574 sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2575 {
2576 	int error, value;
2577 
2578 	if (!arg1)
2579 		return (EINVAL);
2580 	value = *(int *)arg1;
2581 	error = sysctl_handle_int(oidp, &value, 0, req);
2582 	if (error || !req->newptr)
2583 		return (error);
2584 	if (value < low || value > high)
2585 		return (EINVAL);
2586         *(int *)arg1 = value;
2587 
2588         return (0);
2589 }
2590 
2591 static int
2592 sysctl_hw_stge_rxint_nframe(SYSCTL_HANDLER_ARGS)
2593 {
2594 	return (sysctl_int_range(oidp, arg1, arg2, req,
2595 	    STGE_RXINT_NFRAME_MIN, STGE_RXINT_NFRAME_MAX));
2596 }
2597 
2598 static int
2599 sysctl_hw_stge_rxint_dmawait(SYSCTL_HANDLER_ARGS)
2600 {
2601 	return (sysctl_int_range(oidp, arg1, arg2, req,
2602 	    STGE_RXINT_DMAWAIT_MIN, STGE_RXINT_DMAWAIT_MAX));
2603 }
2604