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