xref: /freebsd/sys/dev/nge/if_nge.c (revision fc74a9f93a5fbc83262aa12084404ac953c854b5)
1098ca2bdSWarner Losh /*-
2ce4946daSBill Paul  * Copyright (c) 2001 Wind River Systems
3ce4946daSBill Paul  * Copyright (c) 1997, 1998, 1999, 2000, 2001
4ce4946daSBill Paul  *	Bill Paul <wpaul@bsdi.com>.  All rights reserved.
5ce4946daSBill Paul  *
6ce4946daSBill Paul  * Redistribution and use in source and binary forms, with or without
7ce4946daSBill Paul  * modification, are permitted provided that the following conditions
8ce4946daSBill Paul  * are met:
9ce4946daSBill Paul  * 1. Redistributions of source code must retain the above copyright
10ce4946daSBill Paul  *    notice, this list of conditions and the following disclaimer.
11ce4946daSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
12ce4946daSBill Paul  *    notice, this list of conditions and the following disclaimer in the
13ce4946daSBill Paul  *    documentation and/or other materials provided with the distribution.
14ce4946daSBill Paul  * 3. All advertising materials mentioning features or use of this software
15ce4946daSBill Paul  *    must display the following acknowledgement:
16ce4946daSBill Paul  *	This product includes software developed by Bill Paul.
17ce4946daSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
18ce4946daSBill Paul  *    may be used to endorse or promote products derived from this software
19ce4946daSBill Paul  *    without specific prior written permission.
20ce4946daSBill Paul  *
21ce4946daSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22ce4946daSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ce4946daSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ce4946daSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25ce4946daSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26ce4946daSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27ce4946daSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28ce4946daSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29ce4946daSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30ce4946daSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31ce4946daSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
32ce4946daSBill Paul  */
33ce4946daSBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
37ce4946daSBill Paul /*
38ce4946daSBill Paul  * National Semiconductor DP83820/DP83821 gigabit ethernet driver
39ce4946daSBill Paul  * for FreeBSD. Datasheets are available from:
40ce4946daSBill Paul  *
41ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83820.pdf
42ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83821.pdf
43ce4946daSBill Paul  *
44ce4946daSBill Paul  * These chips are used on several low cost gigabit ethernet NICs
45ce4946daSBill Paul  * sold by D-Link, Addtron, SMC and Asante. Both parts are
46ce4946daSBill Paul  * virtually the same, except the 83820 is a 64-bit/32-bit part,
47ce4946daSBill Paul  * while the 83821 is 32-bit only.
48ce4946daSBill Paul  *
49ce4946daSBill Paul  * Many cards also use National gigE transceivers, such as the
50ce4946daSBill Paul  * DP83891, DP83861 and DP83862 gigPHYTER parts. The DP83861 datasheet
51ce4946daSBill Paul  * contains a full register description that applies to all of these
52ce4946daSBill Paul  * components:
53ce4946daSBill Paul  *
54ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83861.pdf
55ce4946daSBill Paul  *
56ce4946daSBill Paul  * Written by Bill Paul <wpaul@bsdi.com>
57ce4946daSBill Paul  * BSDi Open Source Solutions
58ce4946daSBill Paul  */
59ce4946daSBill Paul 
60ce4946daSBill Paul /*
61ce4946daSBill Paul  * The NatSemi DP83820 and 83821 controllers are enhanced versions
62ce4946daSBill Paul  * of the NatSemi MacPHYTER 10/100 devices. They support 10, 100
63ce4946daSBill Paul  * and 1000Mbps speeds with 1000baseX (ten bit interface), MII and GMII
64ce4946daSBill Paul  * ports. Other features include 8K TX FIFO and 32K RX FIFO, TCP/IP
65ce4946daSBill Paul  * hardware checksum offload (IPv4 only), VLAN tagging and filtering,
66ce4946daSBill Paul  * priority TX and RX queues, a 2048 bit multicast hash filter, 4 RX pattern
67ce4946daSBill Paul  * matching buffers, one perfect address filter buffer and interrupt
68ce4946daSBill Paul  * moderation. The 83820 supports both 64-bit and 32-bit addressing
69ce4946daSBill Paul  * and data transfers: the 64-bit support can be toggled on or off
70ce4946daSBill Paul  * via software. This affects the size of certain fields in the DMA
71ce4946daSBill Paul  * descriptors.
72ce4946daSBill Paul  *
73cb2f755cSBill Paul  * There are two bugs/misfeatures in the 83820/83821 that I have
74cb2f755cSBill Paul  * discovered so far:
75cb2f755cSBill Paul  *
76cb2f755cSBill Paul  * - Receive buffers must be aligned on 64-bit boundaries, which means
77cb2f755cSBill Paul  *   you must resort to copying data in order to fix up the payload
78cb2f755cSBill Paul  *   alignment.
79cb2f755cSBill Paul  *
80cb2f755cSBill Paul  * - In order to transmit jumbo frames larger than 8170 bytes, you have
81cb2f755cSBill Paul  *   to turn off transmit checksum offloading, because the chip can't
82cb2f755cSBill Paul  *   compute the checksum on an outgoing frame unless it fits entirely
83cb2f755cSBill Paul  *   within the TX FIFO, which is only 8192 bytes in size. If you have
84cb2f755cSBill Paul  *   TX checksum offload enabled and you transmit attempt to transmit a
85cb2f755cSBill Paul  *   frame larger than 8170 bytes, the transmitter will wedge.
86cb2f755cSBill Paul  *
87cb2f755cSBill Paul  * To work around the latter problem, TX checksum offload is disabled
88cb2f755cSBill Paul  * if the user selects an MTU larger than 8152 (8170 - 18).
89ce4946daSBill Paul  */
90ce4946daSBill Paul 
91ce4946daSBill Paul #include <sys/param.h>
92ce4946daSBill Paul #include <sys/systm.h>
93ce4946daSBill Paul #include <sys/sockio.h>
94ce4946daSBill Paul #include <sys/mbuf.h>
95ce4946daSBill Paul #include <sys/malloc.h>
96fe12f24bSPoul-Henning Kamp #include <sys/module.h>
97ce4946daSBill Paul #include <sys/kernel.h>
98ce4946daSBill Paul #include <sys/socket.h>
99ce4946daSBill Paul 
100ce4946daSBill Paul #include <net/if.h>
101ce4946daSBill Paul #include <net/if_arp.h>
102ce4946daSBill Paul #include <net/ethernet.h>
103ce4946daSBill Paul #include <net/if_dl.h>
104ce4946daSBill Paul #include <net/if_media.h>
105ce4946daSBill Paul #include <net/if_types.h>
106ce4946daSBill Paul #include <net/if_vlan_var.h>
107ce4946daSBill Paul 
108ce4946daSBill Paul #include <net/bpf.h>
109ce4946daSBill Paul 
110ce4946daSBill Paul #include <vm/vm.h>              /* for vtophys */
111ce4946daSBill Paul #include <vm/pmap.h>            /* for vtophys */
112ce4946daSBill Paul #include <machine/clock.h>      /* for DELAY */
113ce4946daSBill Paul #include <machine/bus.h>
114ce4946daSBill Paul #include <machine/resource.h>
115ce4946daSBill Paul #include <sys/bus.h>
116ce4946daSBill Paul #include <sys/rman.h>
117ce4946daSBill Paul 
118ce4946daSBill Paul #include <dev/mii/mii.h>
119ce4946daSBill Paul #include <dev/mii/miivar.h>
120ce4946daSBill Paul 
12138d8c994SWarner Losh #include <dev/pci/pcireg.h>
12238d8c994SWarner Losh #include <dev/pci/pcivar.h>
123ce4946daSBill Paul 
124ce4946daSBill Paul #define NGE_USEIOSPACE
125ce4946daSBill Paul 
1265da751e4SBill Paul #include <dev/nge/if_ngereg.h>
127ce4946daSBill Paul 
128f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, pci, 1, 1, 1);
129f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, ether, 1, 1, 1);
130ce4946daSBill Paul MODULE_DEPEND(nge, miibus, 1, 1, 1);
131ce4946daSBill Paul 
132ce4946daSBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
133ce4946daSBill Paul #include "miibus_if.h"
134ce4946daSBill Paul 
135ce4946daSBill Paul #define NGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
136ce4946daSBill Paul 
137ce4946daSBill Paul /*
138ce4946daSBill Paul  * Various supported device vendors/types and their names.
139ce4946daSBill Paul  */
140ce4946daSBill Paul static struct nge_type nge_devs[] = {
141ce4946daSBill Paul 	{ NGE_VENDORID, NGE_DEVICEID,
142ce4946daSBill Paul 	    "National Semiconductor Gigabit Ethernet" },
143ce4946daSBill Paul 	{ 0, 0, NULL }
144ce4946daSBill Paul };
145ce4946daSBill Paul 
146e51a25f8SAlfred Perlstein static int nge_probe(device_t);
147e51a25f8SAlfred Perlstein static int nge_attach(device_t);
148e51a25f8SAlfred Perlstein static int nge_detach(device_t);
149ce4946daSBill Paul 
150eaabec55SAlfred Perlstein static int nge_newbuf(struct nge_softc *, struct nge_desc *, struct mbuf *);
151eaabec55SAlfred Perlstein static int nge_encap(struct nge_softc *, struct mbuf *, u_int32_t *);
1525a71a6feSAlan Cox #ifdef NGE_FIXUP_RX
153ad6c618bSBill Paul static __inline void nge_fixup_rx (struct mbuf *);
154ad6c618bSBill Paul #endif
155e51a25f8SAlfred Perlstein static void nge_rxeof(struct nge_softc *);
156e51a25f8SAlfred Perlstein static void nge_txeof(struct nge_softc *);
157e51a25f8SAlfred Perlstein static void nge_intr(void *);
158e51a25f8SAlfred Perlstein static void nge_tick(void *);
159ad6c618bSBill Paul static void nge_tick_locked(struct nge_softc *);
160e51a25f8SAlfred Perlstein static void nge_start(struct ifnet *);
161ad6c618bSBill Paul static void nge_start_locked(struct ifnet *);
162e51a25f8SAlfred Perlstein static int nge_ioctl(struct ifnet *, u_long, caddr_t);
163e51a25f8SAlfred Perlstein static void nge_init(void *);
164ad6c618bSBill Paul static void nge_init_locked(struct nge_softc *);
165e51a25f8SAlfred Perlstein static void nge_stop(struct nge_softc *);
166e51a25f8SAlfred Perlstein static void nge_watchdog(struct ifnet *);
167e51a25f8SAlfred Perlstein static void nge_shutdown(device_t);
168e51a25f8SAlfred Perlstein static int nge_ifmedia_upd(struct ifnet *);
169e51a25f8SAlfred Perlstein static void nge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
170ce4946daSBill Paul 
171e51a25f8SAlfred Perlstein static void nge_delay(struct nge_softc *);
172e51a25f8SAlfred Perlstein static void nge_eeprom_idle(struct nge_softc *);
173e51a25f8SAlfred Perlstein static void nge_eeprom_putbyte(struct nge_softc *, int);
174e51a25f8SAlfred Perlstein static void nge_eeprom_getword(struct nge_softc *, int, u_int16_t *);
175e51a25f8SAlfred Perlstein static void nge_read_eeprom(struct nge_softc *, caddr_t, int, int, int);
176ce4946daSBill Paul 
177e51a25f8SAlfred Perlstein static void nge_mii_sync(struct nge_softc *);
178e51a25f8SAlfred Perlstein static void nge_mii_send(struct nge_softc *, u_int32_t, int);
179e51a25f8SAlfred Perlstein static int nge_mii_readreg(struct nge_softc *, struct nge_mii_frame *);
180e51a25f8SAlfred Perlstein static int nge_mii_writereg(struct nge_softc *, struct nge_mii_frame *);
181ce4946daSBill Paul 
182e51a25f8SAlfred Perlstein static int nge_miibus_readreg(device_t, int, int);
183e51a25f8SAlfred Perlstein static int nge_miibus_writereg(device_t, int, int, int);
184e51a25f8SAlfred Perlstein static void nge_miibus_statchg(device_t);
185ce4946daSBill Paul 
186e51a25f8SAlfred Perlstein static void nge_setmulti(struct nge_softc *);
187e51a25f8SAlfred Perlstein static void nge_reset(struct nge_softc *);
188e51a25f8SAlfred Perlstein static int nge_list_rx_init(struct nge_softc *);
189e51a25f8SAlfred Perlstein static int nge_list_tx_init(struct nge_softc *);
190ce4946daSBill Paul 
191ce4946daSBill Paul #ifdef NGE_USEIOSPACE
192ce4946daSBill Paul #define NGE_RES			SYS_RES_IOPORT
193ce4946daSBill Paul #define NGE_RID			NGE_PCI_LOIO
194ce4946daSBill Paul #else
195ce4946daSBill Paul #define NGE_RES			SYS_RES_MEMORY
196ce4946daSBill Paul #define NGE_RID			NGE_PCI_LOMEM
197ce4946daSBill Paul #endif
198ce4946daSBill Paul 
199ce4946daSBill Paul static device_method_t nge_methods[] = {
200ce4946daSBill Paul 	/* Device interface */
201ce4946daSBill Paul 	DEVMETHOD(device_probe,		nge_probe),
202ce4946daSBill Paul 	DEVMETHOD(device_attach,	nge_attach),
203ce4946daSBill Paul 	DEVMETHOD(device_detach,	nge_detach),
204ce4946daSBill Paul 	DEVMETHOD(device_shutdown,	nge_shutdown),
205ce4946daSBill Paul 
206ce4946daSBill Paul 	/* bus interface */
207ce4946daSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
208ce4946daSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
209ce4946daSBill Paul 
210ce4946daSBill Paul 	/* MII interface */
211ce4946daSBill Paul 	DEVMETHOD(miibus_readreg,	nge_miibus_readreg),
212ce4946daSBill Paul 	DEVMETHOD(miibus_writereg,	nge_miibus_writereg),
213ce4946daSBill Paul 	DEVMETHOD(miibus_statchg,	nge_miibus_statchg),
214ce4946daSBill Paul 
215ce4946daSBill Paul 	{ 0, 0 }
216ce4946daSBill Paul };
217ce4946daSBill Paul 
218ce4946daSBill Paul static driver_t nge_driver = {
219ce4946daSBill Paul 	"nge",
220ce4946daSBill Paul 	nge_methods,
221ce4946daSBill Paul 	sizeof(struct nge_softc)
222ce4946daSBill Paul };
223ce4946daSBill Paul 
224ce4946daSBill Paul static devclass_t nge_devclass;
225ce4946daSBill Paul 
226f246e4a1SMatthew N. Dodd DRIVER_MODULE(nge, pci, nge_driver, nge_devclass, 0, 0);
227ce4946daSBill Paul DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0);
228ce4946daSBill Paul 
229ce4946daSBill Paul #define NGE_SETBIT(sc, reg, x)				\
230ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
231ce4946daSBill Paul 		CSR_READ_4(sc, reg) | (x))
232ce4946daSBill Paul 
233ce4946daSBill Paul #define NGE_CLRBIT(sc, reg, x)				\
234ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
235ce4946daSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
236ce4946daSBill Paul 
237ce4946daSBill Paul #define SIO_SET(x)					\
23829f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x))
239ce4946daSBill Paul 
240ce4946daSBill Paul #define SIO_CLR(x)					\
24129f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x))
242ce4946daSBill Paul 
243eaabec55SAlfred Perlstein static void
244eaabec55SAlfred Perlstein nge_delay(sc)
245ce4946daSBill Paul 	struct nge_softc	*sc;
246ce4946daSBill Paul {
247ce4946daSBill Paul 	int			idx;
248ce4946daSBill Paul 
249ce4946daSBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
250ce4946daSBill Paul 		CSR_READ_4(sc, NGE_CSR);
251ce4946daSBill Paul 
252ce4946daSBill Paul 	return;
253ce4946daSBill Paul }
254ce4946daSBill Paul 
255eaabec55SAlfred Perlstein static void
256eaabec55SAlfred Perlstein nge_eeprom_idle(sc)
257ce4946daSBill Paul 	struct nge_softc	*sc;
258ce4946daSBill Paul {
259ce4946daSBill Paul 	register int		i;
260ce4946daSBill Paul 
261ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
262ce4946daSBill Paul 	nge_delay(sc);
263ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CLK);
264ce4946daSBill Paul 	nge_delay(sc);
265ce4946daSBill Paul 
266ce4946daSBill Paul 	for (i = 0; i < 25; i++) {
267ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
268ce4946daSBill Paul 		nge_delay(sc);
269ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
270ce4946daSBill Paul 		nge_delay(sc);
271ce4946daSBill Paul 	}
272ce4946daSBill Paul 
273ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
274ce4946daSBill Paul 	nge_delay(sc);
275ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CSEL);
276ce4946daSBill Paul 	nge_delay(sc);
277ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0x00000000);
278ce4946daSBill Paul 
279ce4946daSBill Paul 	return;
280ce4946daSBill Paul }
281ce4946daSBill Paul 
282ce4946daSBill Paul /*
283ce4946daSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
284ce4946daSBill Paul  */
285eaabec55SAlfred Perlstein static void
286eaabec55SAlfred Perlstein nge_eeprom_putbyte(sc, addr)
287ce4946daSBill Paul 	struct nge_softc	*sc;
288ce4946daSBill Paul 	int			addr;
289ce4946daSBill Paul {
290ce4946daSBill Paul 	register int		d, i;
291ce4946daSBill Paul 
292ce4946daSBill Paul 	d = addr | NGE_EECMD_READ;
293ce4946daSBill Paul 
294ce4946daSBill Paul 	/*
295ce4946daSBill Paul 	 * Feed in each bit and stobe the clock.
296ce4946daSBill Paul 	 */
297ce4946daSBill Paul 	for (i = 0x400; i; i >>= 1) {
298ce4946daSBill Paul 		if (d & i) {
299ce4946daSBill Paul 			SIO_SET(NGE_MEAR_EE_DIN);
300ce4946daSBill Paul 		} else {
301ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_EE_DIN);
302ce4946daSBill Paul 		}
303ce4946daSBill Paul 		nge_delay(sc);
304ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
305ce4946daSBill Paul 		nge_delay(sc);
306ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
307ce4946daSBill Paul 		nge_delay(sc);
308ce4946daSBill Paul 	}
309ce4946daSBill Paul 
310ce4946daSBill Paul 	return;
311ce4946daSBill Paul }
312ce4946daSBill Paul 
313ce4946daSBill Paul /*
314ce4946daSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
315ce4946daSBill Paul  */
316eaabec55SAlfred Perlstein static void
317eaabec55SAlfred Perlstein nge_eeprom_getword(sc, addr, dest)
318ce4946daSBill Paul 	struct nge_softc	*sc;
319ce4946daSBill Paul 	int			addr;
320ce4946daSBill Paul 	u_int16_t		*dest;
321ce4946daSBill Paul {
322ce4946daSBill Paul 	register int		i;
323ce4946daSBill Paul 	u_int16_t		word = 0;
324ce4946daSBill Paul 
325ce4946daSBill Paul 	/* Force EEPROM to idle state. */
326ce4946daSBill Paul 	nge_eeprom_idle(sc);
327ce4946daSBill Paul 
328ce4946daSBill Paul 	/* Enter EEPROM access mode. */
329ce4946daSBill Paul 	nge_delay(sc);
330ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
331ce4946daSBill Paul 	nge_delay(sc);
332ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
333ce4946daSBill Paul 	nge_delay(sc);
334ce4946daSBill Paul 
335ce4946daSBill Paul 	/*
336ce4946daSBill Paul 	 * Send address of word we want to read.
337ce4946daSBill Paul 	 */
338ce4946daSBill Paul 	nge_eeprom_putbyte(sc, addr);
339ce4946daSBill Paul 
340ce4946daSBill Paul 	/*
341ce4946daSBill Paul 	 * Start reading bits from EEPROM.
342ce4946daSBill Paul 	 */
343ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
344ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
345ce4946daSBill Paul 		nge_delay(sc);
346ce4946daSBill Paul 		if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT)
347ce4946daSBill Paul 			word |= i;
348ce4946daSBill Paul 		nge_delay(sc);
349ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
350ce4946daSBill Paul 		nge_delay(sc);
351ce4946daSBill Paul 	}
352ce4946daSBill Paul 
353ce4946daSBill Paul 	/* Turn off EEPROM access mode. */
354ce4946daSBill Paul 	nge_eeprom_idle(sc);
355ce4946daSBill Paul 
356ce4946daSBill Paul 	*dest = word;
357ce4946daSBill Paul 
358ce4946daSBill Paul 	return;
359ce4946daSBill Paul }
360ce4946daSBill Paul 
361ce4946daSBill Paul /*
362ce4946daSBill Paul  * Read a sequence of words from the EEPROM.
363ce4946daSBill Paul  */
364eaabec55SAlfred Perlstein static void
365eaabec55SAlfred Perlstein nge_read_eeprom(sc, dest, off, cnt, swap)
366ce4946daSBill Paul 	struct nge_softc	*sc;
367ce4946daSBill Paul 	caddr_t			dest;
368ce4946daSBill Paul 	int			off;
369ce4946daSBill Paul 	int			cnt;
370ce4946daSBill Paul 	int			swap;
371ce4946daSBill Paul {
372ce4946daSBill Paul 	int			i;
373ce4946daSBill Paul 	u_int16_t		word = 0, *ptr;
374ce4946daSBill Paul 
375ce4946daSBill Paul 	for (i = 0; i < cnt; i++) {
376ce4946daSBill Paul 		nge_eeprom_getword(sc, off + i, &word);
377ce4946daSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
378ce4946daSBill Paul 		if (swap)
379ce4946daSBill Paul 			*ptr = ntohs(word);
380ce4946daSBill Paul 		else
381ce4946daSBill Paul 			*ptr = word;
382ce4946daSBill Paul 	}
383ce4946daSBill Paul 
384ce4946daSBill Paul 	return;
385ce4946daSBill Paul }
386ce4946daSBill Paul 
387ce4946daSBill Paul /*
388ce4946daSBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
389ce4946daSBill Paul  */
390eaabec55SAlfred Perlstein static void
391eaabec55SAlfred Perlstein nge_mii_sync(sc)
392ce4946daSBill Paul 	struct nge_softc		*sc;
393ce4946daSBill Paul {
394ce4946daSBill Paul 	register int		i;
395ce4946daSBill Paul 
396ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR|NGE_MEAR_MII_DATA);
397ce4946daSBill Paul 
398ce4946daSBill Paul 	for (i = 0; i < 32; i++) {
399ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
400ce4946daSBill Paul 		DELAY(1);
401ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
402ce4946daSBill Paul 		DELAY(1);
403ce4946daSBill Paul 	}
404ce4946daSBill Paul 
405ce4946daSBill Paul 	return;
406ce4946daSBill Paul }
407ce4946daSBill Paul 
408ce4946daSBill Paul /*
409ce4946daSBill Paul  * Clock a series of bits through the MII.
410ce4946daSBill Paul  */
411eaabec55SAlfred Perlstein static void
412eaabec55SAlfred Perlstein nge_mii_send(sc, bits, cnt)
413ce4946daSBill Paul 	struct nge_softc		*sc;
414ce4946daSBill Paul 	u_int32_t		bits;
415ce4946daSBill Paul 	int			cnt;
416ce4946daSBill Paul {
417ce4946daSBill Paul 	int			i;
418ce4946daSBill Paul 
419ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
420ce4946daSBill Paul 
421ce4946daSBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
422ce4946daSBill Paul                 if (bits & i) {
423ce4946daSBill Paul 			SIO_SET(NGE_MEAR_MII_DATA);
424ce4946daSBill Paul                 } else {
425ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_MII_DATA);
426ce4946daSBill Paul                 }
427ce4946daSBill Paul 		DELAY(1);
428ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
429ce4946daSBill Paul 		DELAY(1);
430ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
431ce4946daSBill Paul 	}
432ce4946daSBill Paul }
433ce4946daSBill Paul 
434ce4946daSBill Paul /*
435ce4946daSBill Paul  * Read an PHY register through the MII.
436ce4946daSBill Paul  */
437eaabec55SAlfred Perlstein static int
438eaabec55SAlfred Perlstein nge_mii_readreg(sc, frame)
439ce4946daSBill Paul 	struct nge_softc		*sc;
440ce4946daSBill Paul 	struct nge_mii_frame	*frame;
441ce4946daSBill Paul 
442ce4946daSBill Paul {
443ad6c618bSBill Paul 	int			i, ack;
444ce4946daSBill Paul 
445ce4946daSBill Paul 	/*
446ce4946daSBill Paul 	 * Set up frame for RX.
447ce4946daSBill Paul 	 */
448ce4946daSBill Paul 	frame->mii_stdelim = NGE_MII_STARTDELIM;
449ce4946daSBill Paul 	frame->mii_opcode = NGE_MII_READOP;
450ce4946daSBill Paul 	frame->mii_turnaround = 0;
451ce4946daSBill Paul 	frame->mii_data = 0;
452ce4946daSBill Paul 
453ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0);
454ce4946daSBill Paul 
455ce4946daSBill Paul 	/*
456ce4946daSBill Paul  	 * Turn on data xmit.
457ce4946daSBill Paul 	 */
458ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR);
459ce4946daSBill Paul 
460ce4946daSBill Paul 	nge_mii_sync(sc);
461ce4946daSBill Paul 
462ce4946daSBill Paul 	/*
463ce4946daSBill Paul 	 * Send command/address info.
464ce4946daSBill Paul 	 */
465ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_stdelim, 2);
466ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_opcode, 2);
467ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_phyaddr, 5);
468ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_regaddr, 5);
469ce4946daSBill Paul 
470ce4946daSBill Paul 	/* Idle bit */
471ce4946daSBill Paul 	SIO_CLR((NGE_MEAR_MII_CLK|NGE_MEAR_MII_DATA));
472ce4946daSBill Paul 	DELAY(1);
473ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
474ce4946daSBill Paul 	DELAY(1);
475ce4946daSBill Paul 
476ce4946daSBill Paul 	/* Turn off xmit. */
477ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_DIR);
478ce4946daSBill Paul 	/* Check for ack */
479ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
480ce4946daSBill Paul 	DELAY(1);
481e808cf62SMartin Blapp 	ack = CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA;
482ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
483ce4946daSBill Paul 	DELAY(1);
484ce4946daSBill Paul 
485ce4946daSBill Paul 	/*
486ce4946daSBill Paul 	 * Now try reading data bits. If the ack failed, we still
487ce4946daSBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
488ce4946daSBill Paul 	 */
489ce4946daSBill Paul 	if (ack) {
490ce4946daSBill Paul 		for(i = 0; i < 16; i++) {
491ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_MII_CLK);
492ce4946daSBill Paul 			DELAY(1);
493ce4946daSBill Paul 			SIO_SET(NGE_MEAR_MII_CLK);
494ce4946daSBill Paul 			DELAY(1);
495ce4946daSBill Paul 		}
496ce4946daSBill Paul 		goto fail;
497ce4946daSBill Paul 	}
498ce4946daSBill Paul 
499ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
500ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
501ce4946daSBill Paul 		DELAY(1);
502ce4946daSBill Paul 		if (!ack) {
503ce4946daSBill Paul 			if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA)
504ce4946daSBill Paul 				frame->mii_data |= i;
505ce4946daSBill Paul 			DELAY(1);
506ce4946daSBill Paul 		}
507ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
508ce4946daSBill Paul 		DELAY(1);
509ce4946daSBill Paul 	}
510ce4946daSBill Paul 
511ce4946daSBill Paul fail:
512ce4946daSBill Paul 
513ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
514ce4946daSBill Paul 	DELAY(1);
515ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
516ce4946daSBill Paul 	DELAY(1);
517ce4946daSBill Paul 
518ce4946daSBill Paul 	if (ack)
519ce4946daSBill Paul 		return(1);
520ce4946daSBill Paul 	return(0);
521ce4946daSBill Paul }
522ce4946daSBill Paul 
523ce4946daSBill Paul /*
524ce4946daSBill Paul  * Write to a PHY register through the MII.
525ce4946daSBill Paul  */
526eaabec55SAlfred Perlstein static int
527eaabec55SAlfred Perlstein nge_mii_writereg(sc, frame)
528ce4946daSBill Paul 	struct nge_softc		*sc;
529ce4946daSBill Paul 	struct nge_mii_frame	*frame;
530ce4946daSBill Paul 
531ce4946daSBill Paul {
532ce4946daSBill Paul 
533ce4946daSBill Paul 	/*
534ce4946daSBill Paul 	 * Set up frame for TX.
535ce4946daSBill Paul 	 */
536ce4946daSBill Paul 
537ce4946daSBill Paul 	frame->mii_stdelim = NGE_MII_STARTDELIM;
538ce4946daSBill Paul 	frame->mii_opcode = NGE_MII_WRITEOP;
539ce4946daSBill Paul 	frame->mii_turnaround = NGE_MII_TURNAROUND;
540ce4946daSBill Paul 
541ce4946daSBill Paul 	/*
542ce4946daSBill Paul  	 * Turn on data output.
543ce4946daSBill Paul 	 */
544ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR);
545ce4946daSBill Paul 
546ce4946daSBill Paul 	nge_mii_sync(sc);
547ce4946daSBill Paul 
548ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_stdelim, 2);
549ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_opcode, 2);
550ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_phyaddr, 5);
551ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_regaddr, 5);
552ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_turnaround, 2);
553ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_data, 16);
554ce4946daSBill Paul 
555ce4946daSBill Paul 	/* Idle bit. */
556ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
557ce4946daSBill Paul 	DELAY(1);
558ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
559ce4946daSBill Paul 	DELAY(1);
560ce4946daSBill Paul 
561ce4946daSBill Paul 	/*
562ce4946daSBill Paul 	 * Turn off xmit.
563ce4946daSBill Paul 	 */
564ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_DIR);
565ce4946daSBill Paul 
566ce4946daSBill Paul 	return(0);
567ce4946daSBill Paul }
568ce4946daSBill Paul 
569eaabec55SAlfred Perlstein static int
570eaabec55SAlfred Perlstein nge_miibus_readreg(dev, phy, reg)
571ce4946daSBill Paul 	device_t		dev;
572ce4946daSBill Paul 	int			phy, reg;
573ce4946daSBill Paul {
574ce4946daSBill Paul 	struct nge_softc	*sc;
575ce4946daSBill Paul 	struct nge_mii_frame	frame;
576ce4946daSBill Paul 
577ce4946daSBill Paul 	sc = device_get_softc(dev);
578ce4946daSBill Paul 
579ce4946daSBill Paul 	bzero((char *)&frame, sizeof(frame));
580ce4946daSBill Paul 
581ce4946daSBill Paul 	frame.mii_phyaddr = phy;
582ce4946daSBill Paul 	frame.mii_regaddr = reg;
583ce4946daSBill Paul 	nge_mii_readreg(sc, &frame);
584ce4946daSBill Paul 
585ce4946daSBill Paul 	return(frame.mii_data);
586ce4946daSBill Paul }
587ce4946daSBill Paul 
588eaabec55SAlfred Perlstein static int
589eaabec55SAlfred Perlstein nge_miibus_writereg(dev, phy, reg, data)
590ce4946daSBill Paul 	device_t		dev;
591ce4946daSBill Paul 	int			phy, reg, data;
592ce4946daSBill Paul {
593ce4946daSBill Paul 	struct nge_softc	*sc;
594ce4946daSBill Paul 	struct nge_mii_frame	frame;
595ce4946daSBill Paul 
596ce4946daSBill Paul 	sc = device_get_softc(dev);
597ce4946daSBill Paul 
598ce4946daSBill Paul 	bzero((char *)&frame, sizeof(frame));
599ce4946daSBill Paul 
600ce4946daSBill Paul 	frame.mii_phyaddr = phy;
601ce4946daSBill Paul 	frame.mii_regaddr = reg;
602ce4946daSBill Paul 	frame.mii_data = data;
603ce4946daSBill Paul 	nge_mii_writereg(sc, &frame);
604ce4946daSBill Paul 
605ce4946daSBill Paul 	return(0);
606ce4946daSBill Paul }
607ce4946daSBill Paul 
608eaabec55SAlfred Perlstein static void
609eaabec55SAlfred Perlstein nge_miibus_statchg(dev)
610ce4946daSBill Paul 	device_t		dev;
611ce4946daSBill Paul {
6121f548804SDoug Ambrisko 	int			status;
613ce4946daSBill Paul 	struct nge_softc	*sc;
614ce4946daSBill Paul 	struct mii_data		*mii;
615ce4946daSBill Paul 
616ce4946daSBill Paul 	sc = device_get_softc(dev);
6171f548804SDoug Ambrisko 	if (sc->nge_tbi) {
6181f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
6191f548804SDoug Ambrisko 		    == IFM_AUTO) {
6201f548804SDoug Ambrisko 			status = CSR_READ_4(sc, NGE_TBI_ANLPAR);
6211f548804SDoug Ambrisko 			if (status == 0 || status & NGE_TBIANAR_FDX) {
6221f548804SDoug Ambrisko 				NGE_SETBIT(sc, NGE_TX_CFG,
6231f548804SDoug Ambrisko 				    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6241f548804SDoug Ambrisko 				NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6251f548804SDoug Ambrisko 			} else {
6261f548804SDoug Ambrisko 				NGE_CLRBIT(sc, NGE_TX_CFG,
6271f548804SDoug Ambrisko 				    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6281f548804SDoug Ambrisko 				NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6291f548804SDoug Ambrisko 			}
6301f548804SDoug Ambrisko 
6311f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
6321f548804SDoug Ambrisko 			!= IFM_FDX) {
6331f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
6341f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6351f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6361f548804SDoug Ambrisko 		} else {
6371f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
6381f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6391f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6401f548804SDoug Ambrisko 		}
6411f548804SDoug Ambrisko 	} else {
642ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
643ce4946daSBill Paul 
644ce4946daSBill Paul 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
645ce4946daSBill Paul 		        NGE_SETBIT(sc, NGE_TX_CFG,
646ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
647ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
648ce4946daSBill Paul 		} else {
649ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
650ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
651ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
652ce4946daSBill Paul 		}
653ce4946daSBill Paul 
6542ce0498bSBill Paul 		/* If we have a 1000Mbps link, set the mode_1000 bit. */
655b418ad5cSPoul-Henning Kamp 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
6562ce0498bSBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
6572ce0498bSBill Paul 			NGE_SETBIT(sc, NGE_CFG, NGE_CFG_MODE_1000);
6582ce0498bSBill Paul 		} else {
6592ce0498bSBill Paul 			NGE_CLRBIT(sc, NGE_CFG, NGE_CFG_MODE_1000);
6602ce0498bSBill Paul 		}
6611f548804SDoug Ambrisko 	}
662ce4946daSBill Paul 	return;
663ce4946daSBill Paul }
664ce4946daSBill Paul 
665eaabec55SAlfred Perlstein static void
666eaabec55SAlfred Perlstein nge_setmulti(sc)
667ce4946daSBill Paul 	struct nge_softc	*sc;
668ce4946daSBill Paul {
669ce4946daSBill Paul 	struct ifnet		*ifp;
670ce4946daSBill Paul 	struct ifmultiaddr	*ifma;
671ce4946daSBill Paul 	u_int32_t		h = 0, i, filtsave;
672ce4946daSBill Paul 	int			bit, index;
673ce4946daSBill Paul 
674ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
675fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
676ce4946daSBill Paul 
677ce4946daSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
678ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL,
679ce4946daSBill Paul 		    NGE_RXFILTCTL_MCHASH|NGE_RXFILTCTL_UCHASH);
680ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLMULTI);
681ce4946daSBill Paul 		return;
682ce4946daSBill Paul 	}
683ce4946daSBill Paul 
684ce4946daSBill Paul 	/*
685ce4946daSBill Paul 	 * We have to explicitly enable the multicast hash table
686ce4946daSBill Paul 	 * on the NatSemi chip if we want to use it, which we do.
687ce4946daSBill Paul 	 * We also have to tell it that we don't want to use the
688ce4946daSBill Paul 	 * hash table for matching unicast addresses.
689ce4946daSBill Paul 	 */
690ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_MCHASH);
691ce4946daSBill Paul 	NGE_CLRBIT(sc, NGE_RXFILT_CTL,
692ce4946daSBill Paul 	    NGE_RXFILTCTL_ALLMULTI|NGE_RXFILTCTL_UCHASH);
693ce4946daSBill Paul 
694ce4946daSBill Paul 	filtsave = CSR_READ_4(sc, NGE_RXFILT_CTL);
695ce4946daSBill Paul 
696ce4946daSBill Paul 	/* first, zot all the existing hash bits */
697ce4946daSBill Paul 	for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) {
698ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i);
699ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0);
700ce4946daSBill Paul 	}
701ce4946daSBill Paul 
702ce4946daSBill Paul 	/*
703ce4946daSBill Paul 	 * From the 11 bits returned by the crc routine, the top 7
704ce4946daSBill Paul 	 * bits represent the 16-bit word in the mcast hash table
705ce4946daSBill Paul 	 * that needs to be updated, and the lower 4 bits represent
706ce4946daSBill Paul 	 * which bit within that byte needs to be set.
707ce4946daSBill Paul 	 */
708ce4946daSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
709ce4946daSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
710ce4946daSBill Paul 			continue;
7110e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
7120e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 21;
713ce4946daSBill Paul 		index = (h >> 4) & 0x7F;
714ce4946daSBill Paul 		bit = h & 0xF;
715ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL,
716ce4946daSBill Paul 		    NGE_FILTADDR_MCAST_LO + (index * 2));
717ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit));
718ce4946daSBill Paul 	}
719ce4946daSBill Paul 
720ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, filtsave);
721ce4946daSBill Paul 
722ce4946daSBill Paul 	return;
723ce4946daSBill Paul }
724ce4946daSBill Paul 
725eaabec55SAlfred Perlstein static void
726eaabec55SAlfred Perlstein nge_reset(sc)
727ce4946daSBill Paul 	struct nge_softc	*sc;
728ce4946daSBill Paul {
729ce4946daSBill Paul 	register int		i;
730ce4946daSBill Paul 
731ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET);
732ce4946daSBill Paul 
733ce4946daSBill Paul 	for (i = 0; i < NGE_TIMEOUT; i++) {
734ce4946daSBill Paul 		if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET))
735ce4946daSBill Paul 			break;
736ce4946daSBill Paul 	}
737ce4946daSBill Paul 
738ce4946daSBill Paul 	if (i == NGE_TIMEOUT)
739ce4946daSBill Paul 		printf("nge%d: reset never completed\n", sc->nge_unit);
740ce4946daSBill Paul 
741ce4946daSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
742ce4946daSBill Paul 	DELAY(1000);
743ce4946daSBill Paul 
744ce4946daSBill Paul 	/*
745ce4946daSBill Paul 	 * If this is a NetSemi chip, make sure to clear
746ce4946daSBill Paul 	 * PME mode.
747ce4946daSBill Paul 	 */
748ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS);
749ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, 0);
750ce4946daSBill Paul 
751ce4946daSBill Paul         return;
752ce4946daSBill Paul }
753ce4946daSBill Paul 
754ce4946daSBill Paul /*
755d64ada50SJens Schweikhardt  * Probe for a NatSemi chip. Check the PCI vendor and device
756ce4946daSBill Paul  * IDs against our list and return a device name if we find a match.
757ce4946daSBill Paul  */
758eaabec55SAlfred Perlstein static int
759eaabec55SAlfred Perlstein nge_probe(dev)
760ce4946daSBill Paul 	device_t		dev;
761ce4946daSBill Paul {
762ce4946daSBill Paul 	struct nge_type		*t;
763ce4946daSBill Paul 
764ce4946daSBill Paul 	t = nge_devs;
765ce4946daSBill Paul 
766ce4946daSBill Paul 	while(t->nge_name != NULL) {
767ce4946daSBill Paul 		if ((pci_get_vendor(dev) == t->nge_vid) &&
768ce4946daSBill Paul 		    (pci_get_device(dev) == t->nge_did)) {
769ce4946daSBill Paul 			device_set_desc(dev, t->nge_name);
7706b9907e7SWarner Losh 			return(BUS_PROBE_DEFAULT);
771ce4946daSBill Paul 		}
772ce4946daSBill Paul 		t++;
773ce4946daSBill Paul 	}
774ce4946daSBill Paul 
775ce4946daSBill Paul 	return(ENXIO);
776ce4946daSBill Paul }
777ce4946daSBill Paul 
778ce4946daSBill Paul /*
779ce4946daSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
780ce4946daSBill Paul  * setup and ethernet/BPF attach.
781ce4946daSBill Paul  */
782eaabec55SAlfred Perlstein static int
783eaabec55SAlfred Perlstein nge_attach(dev)
784ce4946daSBill Paul 	device_t		dev;
785ce4946daSBill Paul {
786ce4946daSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
787ce4946daSBill Paul 	struct nge_softc	*sc;
788ce4946daSBill Paul 	struct ifnet		*ifp;
789ce4946daSBill Paul 	int			unit, error = 0, rid;
7901f548804SDoug Ambrisko 	const char		*sep = "";
791ce4946daSBill Paul 
792ce4946daSBill Paul 	sc = device_get_softc(dev);
793ce4946daSBill Paul 	unit = device_get_unit(dev);
794ce4946daSBill Paul 	bzero(sc, sizeof(struct nge_softc));
795ce4946daSBill Paul 
796ad6c618bSBill Paul 	NGE_LOCK_INIT(sc, device_get_nameunit(dev));
797ce4946daSBill Paul 	/*
798ce4946daSBill Paul 	 * Map control/status registers.
799ce4946daSBill Paul 	 */
800ce4946daSBill Paul 	pci_enable_busmaster(dev);
801ce4946daSBill Paul 
802ce4946daSBill Paul 	rid = NGE_RID;
8035f96beb9SNate Lawson 	sc->nge_res = bus_alloc_resource_any(dev, NGE_RES, &rid, RF_ACTIVE);
804ce4946daSBill Paul 
805ce4946daSBill Paul 	if (sc->nge_res == NULL) {
806ce4946daSBill Paul 		printf("nge%d: couldn't map ports/memory\n", unit);
807ce4946daSBill Paul 		error = ENXIO;
808ce4946daSBill Paul 		goto fail;
809ce4946daSBill Paul 	}
810ce4946daSBill Paul 
811ce4946daSBill Paul 	sc->nge_btag = rman_get_bustag(sc->nge_res);
812ce4946daSBill Paul 	sc->nge_bhandle = rman_get_bushandle(sc->nge_res);
813ce4946daSBill Paul 
814ce4946daSBill Paul 	/* Allocate interrupt */
815ce4946daSBill Paul 	rid = 0;
8165f96beb9SNate Lawson 	sc->nge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
817ce4946daSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
818ce4946daSBill Paul 
819ce4946daSBill Paul 	if (sc->nge_irq == NULL) {
820ce4946daSBill Paul 		printf("nge%d: couldn't map interrupt\n", unit);
821ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
822ce4946daSBill Paul 		error = ENXIO;
823ce4946daSBill Paul 		goto fail;
824ce4946daSBill Paul 	}
825ce4946daSBill Paul 
826ce4946daSBill Paul 	/* Reset the adapter. */
827ce4946daSBill Paul 	nge_reset(sc);
828ce4946daSBill Paul 
829ce4946daSBill Paul 	/*
830ce4946daSBill Paul 	 * Get station address from the EEPROM.
831ce4946daSBill Paul 	 */
832ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[4], NGE_EE_NODEADDR, 1, 0);
833ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[2], NGE_EE_NODEADDR + 1, 1, 0);
834ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[0], NGE_EE_NODEADDR + 2, 1, 0);
835ce4946daSBill Paul 
836ce4946daSBill Paul 	sc->nge_unit = unit;
837ce4946daSBill Paul 
838fc74a9f9SBrooks Davis 	/* XXX: leaked on error */
839ce4946daSBill Paul 	sc->nge_ldata = contigmalloc(sizeof(struct nge_list_data), M_DEVBUF,
840ce4946daSBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
841ce4946daSBill Paul 
842ce4946daSBill Paul 	if (sc->nge_ldata == NULL) {
843ce4946daSBill Paul 		printf("nge%d: no memory for list buffers!\n", unit);
844ce4946daSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
845ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
846ce4946daSBill Paul 		error = ENXIO;
847ce4946daSBill Paul 		goto fail;
848ce4946daSBill Paul 	}
849ce4946daSBill Paul 
850fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp = if_alloc(IFT_ETHER);
851fc74a9f9SBrooks Davis 	if (ifp == NULL) {
852fc74a9f9SBrooks Davis 		printf("nge%d: can not if_alloc()\n", unit);
853fc74a9f9SBrooks Davis 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
854fc74a9f9SBrooks Davis 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
855fc74a9f9SBrooks Davis 		error = ENOSPC;
856fc74a9f9SBrooks Davis 		goto fail;
857fc74a9f9SBrooks Davis 	}
858ce4946daSBill Paul 	ifp->if_softc = sc;
8599bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
860ce4946daSBill Paul 	ifp->if_mtu = ETHERMTU;
861ad6c618bSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
862ce4946daSBill Paul 	ifp->if_ioctl = nge_ioctl;
863ce4946daSBill Paul 	ifp->if_start = nge_start;
864ce4946daSBill Paul 	ifp->if_watchdog = nge_watchdog;
865ce4946daSBill Paul 	ifp->if_init = nge_init;
866ce4946daSBill Paul 	ifp->if_baudrate = 1000000000;
867ce4946daSBill Paul 	ifp->if_snd.ifq_maxlen = NGE_TX_LIST_CNT - 1;
868ce4946daSBill Paul 	ifp->if_hwassist = NGE_CSUM_FEATURES;
869673d9191SSam Leffler 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING;
87037f5f239SRuslan Ermilov #ifdef DEVICE_POLLING
87137f5f239SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
87237f5f239SRuslan Ermilov #endif
87384e4de28SJonathan Lemon 	ifp->if_capenable = ifp->if_capabilities;
874ce4946daSBill Paul 
875ce4946daSBill Paul 	/*
876ce4946daSBill Paul 	 * Do MII setup.
877ce4946daSBill Paul 	 */
878ce4946daSBill Paul 	if (mii_phy_probe(dev, &sc->nge_miibus,
879ce4946daSBill Paul 			  nge_ifmedia_upd, nge_ifmedia_sts)) {
8801f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) {
8811f548804SDoug Ambrisko 			sc->nge_tbi = 1;
8821f548804SDoug Ambrisko 			device_printf(dev, "Using TBI\n");
8831f548804SDoug Ambrisko 
8841f548804SDoug Ambrisko 			sc->nge_miibus = dev;
8851f548804SDoug Ambrisko 
8861f548804SDoug Ambrisko 			ifmedia_init(&sc->nge_ifmedia, 0, nge_ifmedia_upd,
8871f548804SDoug Ambrisko 				nge_ifmedia_sts);
8881f548804SDoug Ambrisko #define	ADD(m, c)	ifmedia_add(&sc->nge_ifmedia, (m), (c), NULL)
8891f548804SDoug Ambrisko #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
8901f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, 0), 0);
8911f548804SDoug Ambrisko 			device_printf(dev, " ");
8921f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, 0), 0);
8931f548804SDoug Ambrisko 			PRINT("1000baseSX");
8941f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, 0),0);
8951f548804SDoug Ambrisko 			PRINT("1000baseSX-FDX");
8961f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0);
8971f548804SDoug Ambrisko 			PRINT("auto");
8981f548804SDoug Ambrisko 
8991f548804SDoug Ambrisko 			printf("\n");
9001f548804SDoug Ambrisko #undef ADD
9011f548804SDoug Ambrisko #undef PRINT
9021f548804SDoug Ambrisko 			ifmedia_set(&sc->nge_ifmedia,
9031f548804SDoug Ambrisko 				IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0));
9041f548804SDoug Ambrisko 
9051f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
9061f548804SDoug Ambrisko 				| NGE_GPIO_GP4_OUT
9071f548804SDoug Ambrisko 				| NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB
9081f548804SDoug Ambrisko 				| NGE_GPIO_GP3_OUTENB
9091f548804SDoug Ambrisko 				| NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN);
9101f548804SDoug Ambrisko 
9111f548804SDoug Ambrisko 		} else {
912ce4946daSBill Paul 			printf("nge%d: MII without any PHY!\n", sc->nge_unit);
913ce4946daSBill Paul 			bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
9141f548804SDoug Ambrisko 			bus_release_resource(dev, NGE_RES, NGE_RID,
9151f548804SDoug Ambrisko 					 sc->nge_res);
916ce4946daSBill Paul 			error = ENXIO;
917ce4946daSBill Paul 			goto fail;
918ce4946daSBill Paul 		}
9191f548804SDoug Ambrisko 	}
920ce4946daSBill Paul 
921ce4946daSBill Paul 	/*
922ce4946daSBill Paul 	 * Call MI attach routine.
923ce4946daSBill Paul 	 */
924673d9191SSam Leffler 	ether_ifattach(ifp, eaddr);
925ad6c618bSBill Paul 	callout_init(&sc->nge_stat_ch, CALLOUT_MPSAFE);
926ad6c618bSBill Paul 
927ad6c618bSBill Paul 	/*
928ad6c618bSBill Paul 	 * Hookup IRQ last.
929ad6c618bSBill Paul 	 */
930ad6c618bSBill Paul 	error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET | INTR_MPSAFE,
931ad6c618bSBill Paul 	    nge_intr, sc, &sc->nge_intrhand);
932ad6c618bSBill Paul 	if (error) {
933fc74a9f9SBrooks Davis 		/* XXX: resource leaks */
934ad6c618bSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
935ad6c618bSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
936ad6c618bSBill Paul 		printf("nge%d: couldn't set up irq\n", unit);
937ad6c618bSBill Paul 	}
938ce4946daSBill Paul 
939ce4946daSBill Paul fail:
9401f548804SDoug Ambrisko 
941ad6c618bSBill Paul 	if (error)
942ad6c618bSBill Paul 		NGE_LOCK_DESTROY(sc);
943ce4946daSBill Paul 	return(error);
944ce4946daSBill Paul }
945ce4946daSBill Paul 
946eaabec55SAlfred Perlstein static int
947eaabec55SAlfred Perlstein nge_detach(dev)
948ce4946daSBill Paul 	device_t		dev;
949ce4946daSBill Paul {
950ce4946daSBill Paul 	struct nge_softc	*sc;
951ce4946daSBill Paul 	struct ifnet		*ifp;
952ce4946daSBill Paul 
953ce4946daSBill Paul 	sc = device_get_softc(dev);
954fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
955ce4946daSBill Paul 
956ad6c618bSBill Paul 	NGE_LOCK(sc);
957ce4946daSBill Paul 	nge_reset(sc);
958ce4946daSBill Paul 	nge_stop(sc);
959ad6c618bSBill Paul 	NGE_UNLOCK(sc);
960673d9191SSam Leffler 	ether_ifdetach(ifp);
961fc74a9f9SBrooks Davis 	if_free(ifp);
962ce4946daSBill Paul 
963ce4946daSBill Paul 	bus_generic_detach(dev);
9641f548804SDoug Ambrisko 	if (!sc->nge_tbi) {
965ce4946daSBill Paul 		device_delete_child(dev, sc->nge_miibus);
9661f548804SDoug Ambrisko 	}
967ce4946daSBill Paul 	bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
968ce4946daSBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
969ce4946daSBill Paul 	bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
970ce4946daSBill Paul 
971ce4946daSBill Paul 	contigfree(sc->nge_ldata, sizeof(struct nge_list_data), M_DEVBUF);
972ce4946daSBill Paul 
9736ba160b6SBill Paul 	NGE_LOCK_DESTROY(sc);
9746ba160b6SBill Paul 
975ce4946daSBill Paul 	return(0);
976ce4946daSBill Paul }
977ce4946daSBill Paul 
978ce4946daSBill Paul /*
979ce4946daSBill Paul  * Initialize the transmit descriptors.
980ce4946daSBill Paul  */
981eaabec55SAlfred Perlstein static int
982eaabec55SAlfred Perlstein nge_list_tx_init(sc)
983ce4946daSBill Paul 	struct nge_softc	*sc;
984ce4946daSBill Paul {
985ce4946daSBill Paul 	struct nge_list_data	*ld;
986ce4946daSBill Paul 	struct nge_ring_data	*cd;
987ce4946daSBill Paul 	int			i;
988ce4946daSBill Paul 
989ce4946daSBill Paul 	cd = &sc->nge_cdata;
990ce4946daSBill Paul 	ld = sc->nge_ldata;
991ce4946daSBill Paul 
992ce4946daSBill Paul 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
993ce4946daSBill Paul 		if (i == (NGE_TX_LIST_CNT - 1)) {
994ce4946daSBill Paul 			ld->nge_tx_list[i].nge_nextdesc =
995ce4946daSBill Paul 			    &ld->nge_tx_list[0];
996ce4946daSBill Paul 			ld->nge_tx_list[i].nge_next =
997ce4946daSBill Paul 			    vtophys(&ld->nge_tx_list[0]);
998ce4946daSBill Paul 		} else {
999ce4946daSBill Paul 			ld->nge_tx_list[i].nge_nextdesc =
1000ce4946daSBill Paul 			    &ld->nge_tx_list[i + 1];
1001ce4946daSBill Paul 			ld->nge_tx_list[i].nge_next =
1002ce4946daSBill Paul 			    vtophys(&ld->nge_tx_list[i + 1]);
1003ce4946daSBill Paul 		}
1004ce4946daSBill Paul 		ld->nge_tx_list[i].nge_mbuf = NULL;
1005ce4946daSBill Paul 		ld->nge_tx_list[i].nge_ptr = 0;
1006ce4946daSBill Paul 		ld->nge_tx_list[i].nge_ctl = 0;
1007ce4946daSBill Paul 	}
1008ce4946daSBill Paul 
1009ce4946daSBill Paul 	cd->nge_tx_prod = cd->nge_tx_cons = cd->nge_tx_cnt = 0;
1010ce4946daSBill Paul 
1011ce4946daSBill Paul 	return(0);
1012ce4946daSBill Paul }
1013ce4946daSBill Paul 
1014ce4946daSBill Paul 
1015ce4946daSBill Paul /*
1016ce4946daSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
1017ce4946daSBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
1018ce4946daSBill Paul  * points back to the first.
1019ce4946daSBill Paul  */
1020eaabec55SAlfred Perlstein static int
1021eaabec55SAlfred Perlstein nge_list_rx_init(sc)
1022ce4946daSBill Paul 	struct nge_softc	*sc;
1023ce4946daSBill Paul {
1024ce4946daSBill Paul 	struct nge_list_data	*ld;
1025ce4946daSBill Paul 	struct nge_ring_data	*cd;
1026ce4946daSBill Paul 	int			i;
1027ce4946daSBill Paul 
1028ce4946daSBill Paul 	ld = sc->nge_ldata;
1029ce4946daSBill Paul 	cd = &sc->nge_cdata;
1030ce4946daSBill Paul 
1031ce4946daSBill Paul 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
1032ce4946daSBill Paul 		if (nge_newbuf(sc, &ld->nge_rx_list[i], NULL) == ENOBUFS)
1033ce4946daSBill Paul 			return(ENOBUFS);
1034ce4946daSBill Paul 		if (i == (NGE_RX_LIST_CNT - 1)) {
1035ce4946daSBill Paul 			ld->nge_rx_list[i].nge_nextdesc =
1036ce4946daSBill Paul 			    &ld->nge_rx_list[0];
1037ce4946daSBill Paul 			ld->nge_rx_list[i].nge_next =
1038ce4946daSBill Paul 			    vtophys(&ld->nge_rx_list[0]);
1039ce4946daSBill Paul 		} else {
1040ce4946daSBill Paul 			ld->nge_rx_list[i].nge_nextdesc =
1041ce4946daSBill Paul 			    &ld->nge_rx_list[i + 1];
1042ce4946daSBill Paul 			ld->nge_rx_list[i].nge_next =
1043ce4946daSBill Paul 			    vtophys(&ld->nge_rx_list[i + 1]);
1044ce4946daSBill Paul 		}
1045ce4946daSBill Paul 	}
1046ce4946daSBill Paul 
1047ce4946daSBill Paul 	cd->nge_rx_prod = 0;
1048ad6c618bSBill Paul 	sc->nge_head = sc->nge_tail = NULL;
1049ce4946daSBill Paul 
1050ce4946daSBill Paul 	return(0);
1051ce4946daSBill Paul }
1052ce4946daSBill Paul 
1053ce4946daSBill Paul /*
1054ce4946daSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
1055ce4946daSBill Paul  */
1056eaabec55SAlfred Perlstein static int
1057eaabec55SAlfred Perlstein nge_newbuf(sc, c, m)
1058ce4946daSBill Paul 	struct nge_softc	*sc;
1059ce4946daSBill Paul 	struct nge_desc		*c;
1060ce4946daSBill Paul 	struct mbuf		*m;
1061ce4946daSBill Paul {
1062ce4946daSBill Paul 
1063ce4946daSBill Paul 	if (m == NULL) {
10646c772336SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
10656c772336SSam Leffler 		if (m == NULL)
1066ce4946daSBill Paul 			return (ENOBUFS);
1067ad6c618bSBill Paul 	} else
1068ad6c618bSBill Paul 		m->m_data = m->m_ext.ext_buf;
1069ce4946daSBill Paul 
1070ad6c618bSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
1071ce4946daSBill Paul 
10726c772336SSam Leffler 	m_adj(m, sizeof(u_int64_t));
1073ce4946daSBill Paul 
10746c772336SSam Leffler 	c->nge_mbuf = m;
10756c772336SSam Leffler 	c->nge_ptr = vtophys(mtod(m, caddr_t));
10766c772336SSam Leffler 	c->nge_ctl = m->m_len;
1077ce4946daSBill Paul 	c->nge_extsts = 0;
1078ce4946daSBill Paul 
1079ce4946daSBill Paul 	return(0);
1080ce4946daSBill Paul }
1081ce4946daSBill Paul 
1082ad6c618bSBill Paul #ifdef NGE_FIXUP_RX
1083ad6c618bSBill Paul static __inline void
1084ad6c618bSBill Paul nge_fixup_rx(m)
1085ad6c618bSBill Paul 	struct mbuf		*m;
1086ce4946daSBill Paul {
1087ce4946daSBill Paul         int			i;
1088ad6c618bSBill Paul         uint16_t		*src, *dst;
1089ce4946daSBill Paul 
1090ad6c618bSBill Paul 	src = mtod(m, uint16_t *);
1091ad6c618bSBill Paul 	dst = src - 1;
1092ce4946daSBill Paul 
1093ad6c618bSBill Paul 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1094ad6c618bSBill Paul 		*dst++ = *src++;
1095ce4946daSBill Paul 
1096ad6c618bSBill Paul 	m->m_data -= ETHER_ALIGN;
1097ce4946daSBill Paul 
1098ce4946daSBill Paul 	return;
1099ce4946daSBill Paul }
1100ad6c618bSBill Paul #endif
1101ad6c618bSBill Paul 
1102ce4946daSBill Paul /*
1103ce4946daSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
1104ce4946daSBill Paul  * the higher level protocols.
1105ce4946daSBill Paul  */
1106eaabec55SAlfred Perlstein static void
1107eaabec55SAlfred Perlstein nge_rxeof(sc)
1108ce4946daSBill Paul 	struct nge_softc	*sc;
1109ce4946daSBill Paul {
1110ce4946daSBill Paul         struct mbuf		*m;
1111ce4946daSBill Paul         struct ifnet		*ifp;
1112ce4946daSBill Paul 	struct nge_desc		*cur_rx;
1113ce4946daSBill Paul 	int			i, total_len = 0;
1114ce4946daSBill Paul 	u_int32_t		rxstat;
1115ce4946daSBill Paul 
1116ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1117fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1118ce4946daSBill Paul 	i = sc->nge_cdata.nge_rx_prod;
1119ce4946daSBill Paul 
1120ce4946daSBill Paul 	while(NGE_OWNDESC(&sc->nge_ldata->nge_rx_list[i])) {
1121ce4946daSBill Paul 		u_int32_t		extsts;
1122ce4946daSBill Paul 
1123196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1124fa9336b8SRobert Watson 		if (ifp->if_flags & IFF_POLLING) {
1125196c0df6SHidetoshi Shimokawa 			if (sc->rxcycles <= 0)
1126196c0df6SHidetoshi Shimokawa 				break;
1127196c0df6SHidetoshi Shimokawa 			sc->rxcycles--;
1128196c0df6SHidetoshi Shimokawa 		}
1129196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1130196c0df6SHidetoshi Shimokawa 
1131ce4946daSBill Paul 		cur_rx = &sc->nge_ldata->nge_rx_list[i];
1132ce4946daSBill Paul 		rxstat = cur_rx->nge_rxstat;
1133ce4946daSBill Paul 		extsts = cur_rx->nge_extsts;
1134ce4946daSBill Paul 		m = cur_rx->nge_mbuf;
1135ce4946daSBill Paul 		cur_rx->nge_mbuf = NULL;
1136ce4946daSBill Paul 		total_len = NGE_RXBYTES(cur_rx);
1137ce4946daSBill Paul 		NGE_INC(i, NGE_RX_LIST_CNT);
1138ad6c618bSBill Paul 
1139ad6c618bSBill Paul 		if (rxstat & NGE_CMDSTS_MORE) {
1140ad6c618bSBill Paul 			m->m_len = total_len;
1141ad6c618bSBill Paul 			if (sc->nge_head == NULL) {
1142ad6c618bSBill Paul 				m->m_pkthdr.len = total_len;
1143ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = m;
1144ad6c618bSBill Paul 			} else {
1145ad6c618bSBill Paul 				m->m_flags &= ~M_PKTHDR;
1146ad6c618bSBill Paul 				sc->nge_head->m_pkthdr.len += total_len;
1147ad6c618bSBill Paul 				sc->nge_tail->m_next = m;
1148ad6c618bSBill Paul 				sc->nge_tail = m;
1149ad6c618bSBill Paul 			}
1150ad6c618bSBill Paul 			nge_newbuf(sc, cur_rx, NULL);
1151ad6c618bSBill Paul 			continue;
1152ad6c618bSBill Paul 		}
1153ad6c618bSBill Paul 
1154ce4946daSBill Paul 		/*
1155ce4946daSBill Paul 		 * If an error occurs, update stats, clear the
1156ce4946daSBill Paul 		 * status word and leave the mbuf cluster in place:
1157ce4946daSBill Paul 		 * it should simply get re-used next time this descriptor
1158ce4946daSBill Paul 	 	 * comes up in the ring.
1159ce4946daSBill Paul 		 */
1160ce4946daSBill Paul 		if (!(rxstat & NGE_CMDSTS_PKT_OK)) {
1161ce4946daSBill Paul 			ifp->if_ierrors++;
1162ad6c618bSBill Paul 			if (sc->nge_head != NULL) {
1163ad6c618bSBill Paul 				m_freem(sc->nge_head);
1164ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = NULL;
1165ad6c618bSBill Paul 			}
1166ce4946daSBill Paul 			nge_newbuf(sc, cur_rx, m);
1167ce4946daSBill Paul 			continue;
1168ce4946daSBill Paul 		}
1169ce4946daSBill Paul 
1170ad6c618bSBill Paul 		/* Try conjure up a replacement mbuf. */
1171ad6c618bSBill Paul 
1172ad6c618bSBill Paul 		if (nge_newbuf(sc, cur_rx, NULL)) {
1173ad6c618bSBill Paul 			ifp->if_ierrors++;
1174ad6c618bSBill Paul 			if (sc->nge_head != NULL) {
1175ad6c618bSBill Paul 				m_freem(sc->nge_head);
1176ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = NULL;
1177ad6c618bSBill Paul 			}
1178ad6c618bSBill Paul 			nge_newbuf(sc, cur_rx, m);
1179ad6c618bSBill Paul 			continue;
1180ad6c618bSBill Paul 		}
1181ad6c618bSBill Paul 
1182ad6c618bSBill Paul 		if (sc->nge_head != NULL) {
1183ad6c618bSBill Paul 			m->m_len = total_len;
1184ad6c618bSBill Paul 			m->m_flags &= ~M_PKTHDR;
1185ad6c618bSBill Paul 			sc->nge_tail->m_next = m;
1186ad6c618bSBill Paul 			m = sc->nge_head;
1187ad6c618bSBill Paul 			m->m_pkthdr.len += total_len;
1188ad6c618bSBill Paul 			sc->nge_head = sc->nge_tail = NULL;
1189ad6c618bSBill Paul 		} else
1190ad6c618bSBill Paul 			m->m_pkthdr.len = m->m_len = total_len;
1191ad6c618bSBill Paul 
1192ce4946daSBill Paul 		/*
1193ce4946daSBill Paul 		 * Ok. NatSemi really screwed up here. This is the
1194ce4946daSBill Paul 		 * only gigE chip I know of with alignment constraints
1195ce4946daSBill Paul 		 * on receive buffers. RX buffers must be 64-bit aligned.
1196ce4946daSBill Paul 		 */
1197962315f6SBill Paul 		/*
1198962315f6SBill Paul 		 * By popular demand, ignore the alignment problems
1199962315f6SBill Paul 		 * on the Intel x86 platform. The performance hit
1200962315f6SBill Paul 		 * incurred due to unaligned accesses is much smaller
1201962315f6SBill Paul 		 * than the hit produced by forcing buffer copies all
1202962315f6SBill Paul 		 * the time, especially with jumbo frames. We still
1203962315f6SBill Paul 		 * need to fix up the alignment everywhere else though.
1204962315f6SBill Paul 		 */
1205ad6c618bSBill Paul #ifdef NGE_FIXUP_RX
1206ad6c618bSBill Paul 		nge_fixup_rx(m);
1207962315f6SBill Paul #endif
1208ce4946daSBill Paul 
1209ce4946daSBill Paul 		ifp->if_ipackets++;
1210ad6c618bSBill Paul 		m->m_pkthdr.rcvif = ifp;
1211ce4946daSBill Paul 
1212ce4946daSBill Paul 		/* Do IP checksum checking. */
1213ce4946daSBill Paul 		if (extsts & NGE_RXEXTSTS_IPPKT)
1214ce4946daSBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1215ce4946daSBill Paul 		if (!(extsts & NGE_RXEXTSTS_IPCSUMERR))
121601702579SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
12172195de46SBill Paul 		if ((extsts & NGE_RXEXTSTS_TCPPKT &&
12182195de46SBill Paul 		    !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) ||
12192195de46SBill Paul 		    (extsts & NGE_RXEXTSTS_UDPPKT &&
12202195de46SBill Paul 		    !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) {
12212195de46SBill Paul 			m->m_pkthdr.csum_flags |=
12222195de46SBill Paul 			    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1223c9215605SBill Paul 			m->m_pkthdr.csum_data = 0xffff;
12242195de46SBill Paul 		}
1225ce4946daSBill Paul 
1226ce4946daSBill Paul 		/*
1227ce4946daSBill Paul 		 * If we received a packet with a vlan tag, pass it
1228ce4946daSBill Paul 		 * to vlan_input() instead of ether_input().
1229ce4946daSBill Paul 		 */
1230ce4946daSBill Paul 		if (extsts & NGE_RXEXTSTS_VLANPKT) {
1231673d9191SSam Leffler 			VLAN_INPUT_TAG(ifp, m,
123253090724SRuslan Ermilov 			    ntohs(extsts & NGE_RXEXTSTS_VTCI), continue);
1233ce4946daSBill Paul 		}
1234ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1235673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
1236ad6c618bSBill Paul 		NGE_LOCK(sc);
1237ce4946daSBill Paul 	}
1238ce4946daSBill Paul 
1239ce4946daSBill Paul 	sc->nge_cdata.nge_rx_prod = i;
1240ce4946daSBill Paul 
1241ce4946daSBill Paul 	return;
1242ce4946daSBill Paul }
1243ce4946daSBill Paul 
1244ce4946daSBill Paul /*
1245ce4946daSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
1246ce4946daSBill Paul  * the list buffers.
1247ce4946daSBill Paul  */
1248ce4946daSBill Paul 
1249eaabec55SAlfred Perlstein static void
1250eaabec55SAlfred Perlstein nge_txeof(sc)
1251ce4946daSBill Paul 	struct nge_softc	*sc;
1252ce4946daSBill Paul {
12531e73ec7dSRuslan Ermilov 	struct nge_desc		*cur_tx;
1254ce4946daSBill Paul 	struct ifnet		*ifp;
1255ce4946daSBill Paul 	u_int32_t		idx;
1256ce4946daSBill Paul 
1257ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1258fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1259ce4946daSBill Paul 
1260ce4946daSBill Paul 	/*
1261ce4946daSBill Paul 	 * Go through our tx list and free mbufs for those
1262ce4946daSBill Paul 	 * frames that have been transmitted.
1263ce4946daSBill Paul 	 */
1264ce4946daSBill Paul 	idx = sc->nge_cdata.nge_tx_cons;
1265ce4946daSBill Paul 	while (idx != sc->nge_cdata.nge_tx_prod) {
1266ce4946daSBill Paul 		cur_tx = &sc->nge_ldata->nge_tx_list[idx];
1267ce4946daSBill Paul 
1268ce4946daSBill Paul 		if (NGE_OWNDESC(cur_tx))
1269ce4946daSBill Paul 			break;
1270ce4946daSBill Paul 
1271ce4946daSBill Paul 		if (cur_tx->nge_ctl & NGE_CMDSTS_MORE) {
1272ce4946daSBill Paul 			sc->nge_cdata.nge_tx_cnt--;
1273ce4946daSBill Paul 			NGE_INC(idx, NGE_TX_LIST_CNT);
1274ce4946daSBill Paul 			continue;
1275ce4946daSBill Paul 		}
1276ce4946daSBill Paul 
1277ce4946daSBill Paul 		if (!(cur_tx->nge_ctl & NGE_CMDSTS_PKT_OK)) {
1278ce4946daSBill Paul 			ifp->if_oerrors++;
1279ce4946daSBill Paul 			if (cur_tx->nge_txstat & NGE_TXSTAT_EXCESSCOLLS)
1280ce4946daSBill Paul 				ifp->if_collisions++;
1281ce4946daSBill Paul 			if (cur_tx->nge_txstat & NGE_TXSTAT_OUTOFWINCOLL)
1282ce4946daSBill Paul 				ifp->if_collisions++;
1283ce4946daSBill Paul 		}
1284ce4946daSBill Paul 
1285ce4946daSBill Paul 		ifp->if_collisions +=
1286ce4946daSBill Paul 		    (cur_tx->nge_txstat & NGE_TXSTAT_COLLCNT) >> 16;
1287ce4946daSBill Paul 
1288ce4946daSBill Paul 		ifp->if_opackets++;
1289ce4946daSBill Paul 		if (cur_tx->nge_mbuf != NULL) {
1290ce4946daSBill Paul 			m_freem(cur_tx->nge_mbuf);
1291ce4946daSBill Paul 			cur_tx->nge_mbuf = NULL;
12921e73ec7dSRuslan Ermilov 			ifp->if_flags &= ~IFF_OACTIVE;
1293ce4946daSBill Paul 		}
1294ce4946daSBill Paul 
1295ce4946daSBill Paul 		sc->nge_cdata.nge_tx_cnt--;
1296ce4946daSBill Paul 		NGE_INC(idx, NGE_TX_LIST_CNT);
1297ce4946daSBill Paul 	}
1298ce4946daSBill Paul 
1299ce4946daSBill Paul 	sc->nge_cdata.nge_tx_cons = idx;
1300ce4946daSBill Paul 
13011e73ec7dSRuslan Ermilov 	if (idx == sc->nge_cdata.nge_tx_prod)
13021e73ec7dSRuslan Ermilov 		ifp->if_timer = 0;
1303ce4946daSBill Paul 
1304ce4946daSBill Paul 	return;
1305ce4946daSBill Paul }
1306ce4946daSBill Paul 
1307eaabec55SAlfred Perlstein static void
1308eaabec55SAlfred Perlstein nge_tick(xsc)
1309ce4946daSBill Paul 	void			*xsc;
1310ce4946daSBill Paul {
1311ce4946daSBill Paul 	struct nge_softc	*sc;
1312ce4946daSBill Paul 
1313ce4946daSBill Paul 	sc = xsc;
1314ad6c618bSBill Paul 
1315ad6c618bSBill Paul 	NGE_LOCK(sc);
1316ad6c618bSBill Paul 	nge_tick_locked(sc);
1317ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1318ad6c618bSBill Paul }
1319ad6c618bSBill Paul 
1320ad6c618bSBill Paul static void
1321ad6c618bSBill Paul nge_tick_locked(sc)
1322ad6c618bSBill Paul 	struct nge_softc	*sc;
1323ad6c618bSBill Paul {
1324ad6c618bSBill Paul 	struct mii_data		*mii;
1325ad6c618bSBill Paul 	struct ifnet		*ifp;
1326ad6c618bSBill Paul 
1327ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1328fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1329ce4946daSBill Paul 
13301f548804SDoug Ambrisko 	if (sc->nge_tbi) {
13311f548804SDoug Ambrisko 		if (!sc->nge_link) {
13321f548804SDoug Ambrisko 			if (CSR_READ_4(sc, NGE_TBI_BMSR)
13331f548804SDoug Ambrisko 			    & NGE_TBIBMSR_ANEG_DONE) {
13346d6b7a18SPoul-Henning Kamp 				if (bootverbose)
13351f548804SDoug Ambrisko 					printf("nge%d: gigabit link up\n",
13361f548804SDoug Ambrisko 					    sc->nge_unit);
13371f548804SDoug Ambrisko 				nge_miibus_statchg(sc->nge_miibus);
13381f548804SDoug Ambrisko 				sc->nge_link++;
13391f548804SDoug Ambrisko 				if (ifp->if_snd.ifq_head != NULL)
1340ad6c618bSBill Paul 					nge_start_locked(ifp);
13411f548804SDoug Ambrisko 			}
13421f548804SDoug Ambrisko 		}
13431f548804SDoug Ambrisko 	} else {
1344ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
1345ce4946daSBill Paul 		mii_tick(mii);
1346ce4946daSBill Paul 
1347ce4946daSBill Paul 		if (!sc->nge_link) {
1348ce4946daSBill Paul 			if (mii->mii_media_status & IFM_ACTIVE &&
1349ce4946daSBill Paul 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1350ce4946daSBill Paul 				sc->nge_link++;
13511f548804SDoug Ambrisko 				if (IFM_SUBTYPE(mii->mii_media_active)
13526d6b7a18SPoul-Henning Kamp 				    == IFM_1000_T && bootverbose)
1353ce4946daSBill Paul 					printf("nge%d: gigabit link up\n",
1354ce4946daSBill Paul 					    sc->nge_unit);
1355ce4946daSBill Paul 				if (ifp->if_snd.ifq_head != NULL)
1356ad6c618bSBill Paul 					nge_start_locked(ifp);
1357ce4946daSBill Paul 			}
1358d9fc2b81SPoul-Henning Kamp 		}
13591f548804SDoug Ambrisko 	}
1360ad6c618bSBill Paul 	callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc);
1361ce4946daSBill Paul 
1362ce4946daSBill Paul 	return;
1363ce4946daSBill Paul }
1364ce4946daSBill Paul 
1365196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1366196c0df6SHidetoshi Shimokawa static poll_handler_t nge_poll;
1367196c0df6SHidetoshi Shimokawa 
1368196c0df6SHidetoshi Shimokawa static void
1369196c0df6SHidetoshi Shimokawa nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1370196c0df6SHidetoshi Shimokawa {
1371196c0df6SHidetoshi Shimokawa 	struct  nge_softc *sc = ifp->if_softc;
1372196c0df6SHidetoshi Shimokawa 
1373ad6c618bSBill Paul 	NGE_LOCK(sc);
137437f5f239SRuslan Ermilov 	if (!(ifp->if_capenable & IFCAP_POLLING)) {
137537f5f239SRuslan Ermilov 		ether_poll_deregister(ifp);
137637f5f239SRuslan Ermilov 		cmd = POLL_DEREGISTER;
137737f5f239SRuslan Ermilov 	}
1378196c0df6SHidetoshi Shimokawa 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1379196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 1);
1380ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1381196c0df6SHidetoshi Shimokawa 		return;
1382196c0df6SHidetoshi Shimokawa 	}
1383196c0df6SHidetoshi Shimokawa 
1384196c0df6SHidetoshi Shimokawa 	/*
1385196c0df6SHidetoshi Shimokawa 	 * On the nge, reading the status register also clears it.
1386196c0df6SHidetoshi Shimokawa 	 * So before returning to intr mode we must make sure that all
1387196c0df6SHidetoshi Shimokawa 	 * possible pending sources of interrupts have been served.
1388196c0df6SHidetoshi Shimokawa 	 * In practice this means run to completion the *eof routines,
1389196c0df6SHidetoshi Shimokawa 	 * and then call the interrupt routine
1390196c0df6SHidetoshi Shimokawa 	 */
1391196c0df6SHidetoshi Shimokawa 	sc->rxcycles = count;
1392196c0df6SHidetoshi Shimokawa 	nge_rxeof(sc);
1393196c0df6SHidetoshi Shimokawa 	nge_txeof(sc);
1394196c0df6SHidetoshi Shimokawa 	if (ifp->if_snd.ifq_head != NULL)
1395ad6c618bSBill Paul 		nge_start_locked(ifp);
1396196c0df6SHidetoshi Shimokawa 
1397196c0df6SHidetoshi Shimokawa 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1398196c0df6SHidetoshi Shimokawa 		u_int32_t	status;
1399196c0df6SHidetoshi Shimokawa 
1400196c0df6SHidetoshi Shimokawa 		/* Reading the ISR register clears all interrupts. */
1401196c0df6SHidetoshi Shimokawa 		status = CSR_READ_4(sc, NGE_ISR);
1402196c0df6SHidetoshi Shimokawa 
1403196c0df6SHidetoshi Shimokawa 		if (status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW))
1404196c0df6SHidetoshi Shimokawa 			nge_rxeof(sc);
1405196c0df6SHidetoshi Shimokawa 
1406196c0df6SHidetoshi Shimokawa 		if (status & (NGE_ISR_RX_IDLE))
1407196c0df6SHidetoshi Shimokawa 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1408196c0df6SHidetoshi Shimokawa 
1409196c0df6SHidetoshi Shimokawa 		if (status & NGE_ISR_SYSERR) {
1410196c0df6SHidetoshi Shimokawa 			nge_reset(sc);
1411ad6c618bSBill Paul 			nge_init_locked(sc);
1412196c0df6SHidetoshi Shimokawa 		}
1413196c0df6SHidetoshi Shimokawa 	}
1414ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1415196c0df6SHidetoshi Shimokawa }
1416196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1417196c0df6SHidetoshi Shimokawa 
1418eaabec55SAlfred Perlstein static void
1419eaabec55SAlfred Perlstein nge_intr(arg)
1420ce4946daSBill Paul 	void			*arg;
1421ce4946daSBill Paul {
1422ce4946daSBill Paul 	struct nge_softc	*sc;
1423ce4946daSBill Paul 	struct ifnet		*ifp;
1424ce4946daSBill Paul 	u_int32_t		status;
1425ce4946daSBill Paul 
1426ce4946daSBill Paul 	sc = arg;
1427fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1428ce4946daSBill Paul 
1429ad6c618bSBill Paul 	NGE_LOCK(sc);
1430196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1431ad6c618bSBill Paul 	if (ifp->if_flags & IFF_POLLING) {
1432ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1433196c0df6SHidetoshi Shimokawa 		return;
1434ad6c618bSBill Paul 	}
143537f5f239SRuslan Ermilov 	if ((ifp->if_capenable & IFCAP_POLLING) &&
143637f5f239SRuslan Ermilov 	    ether_poll_register(nge_poll, ifp)) { /* ok, disable interrupts */
1437196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 0);
1438ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1439196c0df6SHidetoshi Shimokawa 		nge_poll(ifp, 0, 1);
1440196c0df6SHidetoshi Shimokawa 		return;
1441196c0df6SHidetoshi Shimokawa 	}
1442196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1443196c0df6SHidetoshi Shimokawa 
1444ce4946daSBill Paul 	/* Supress unwanted interrupts */
1445ce4946daSBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
1446ce4946daSBill Paul 		nge_stop(sc);
1447ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1448ce4946daSBill Paul 		return;
1449ce4946daSBill Paul 	}
1450ce4946daSBill Paul 
1451ce4946daSBill Paul 	/* Disable interrupts. */
1452ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
1453ce4946daSBill Paul 
14541f548804SDoug Ambrisko 	/* Data LED on for TBI mode */
14551f548804SDoug Ambrisko 	if(sc->nge_tbi)
14561f548804SDoug Ambrisko 		 CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
14571f548804SDoug Ambrisko 			     | NGE_GPIO_GP3_OUT);
14581f548804SDoug Ambrisko 
1459ce4946daSBill Paul 	for (;;) {
1460ce4946daSBill Paul 		/* Reading the ISR register clears all interrupts. */
1461ce4946daSBill Paul 		status = CSR_READ_4(sc, NGE_ISR);
1462ce4946daSBill Paul 
1463ce4946daSBill Paul 		if ((status & NGE_INTRS) == 0)
1464ce4946daSBill Paul 			break;
1465ce4946daSBill Paul 
1466ce4946daSBill Paul 		if ((status & NGE_ISR_TX_DESC_OK) ||
1467ce4946daSBill Paul 		    (status & NGE_ISR_TX_ERR) ||
1468ce4946daSBill Paul 		    (status & NGE_ISR_TX_OK) ||
1469ce4946daSBill Paul 		    (status & NGE_ISR_TX_IDLE))
1470ce4946daSBill Paul 			nge_txeof(sc);
1471ce4946daSBill Paul 
1472ce4946daSBill Paul 		if ((status & NGE_ISR_RX_DESC_OK) ||
1473248fa967SBill Paul 		    (status & NGE_ISR_RX_ERR) ||
14748ce3678aSBill Paul 		    (status & NGE_ISR_RX_OFLOW) ||
1475ff7ed9f7SPoul-Henning Kamp 		    (status & NGE_ISR_RX_FIFO_OFLOW) ||
1476ff7ed9f7SPoul-Henning Kamp 		    (status & NGE_ISR_RX_IDLE) ||
1477ce4946daSBill Paul 		    (status & NGE_ISR_RX_OK))
1478ce4946daSBill Paul 			nge_rxeof(sc);
1479ff7ed9f7SPoul-Henning Kamp 
1480ff7ed9f7SPoul-Henning Kamp 		if ((status & NGE_ISR_RX_IDLE))
1481ff7ed9f7SPoul-Henning Kamp 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1482ff7ed9f7SPoul-Henning Kamp 
1483ce4946daSBill Paul 		if (status & NGE_ISR_SYSERR) {
1484ce4946daSBill Paul 			nge_reset(sc);
1485ce4946daSBill Paul 			ifp->if_flags &= ~IFF_RUNNING;
1486ad6c618bSBill Paul 			nge_init_locked(sc);
1487ce4946daSBill Paul 		}
1488ce4946daSBill Paul 
1489d9fc2b81SPoul-Henning Kamp #if 0
1490d9fc2b81SPoul-Henning Kamp 		/*
1491d9fc2b81SPoul-Henning Kamp 		 * XXX: nge_tick() is not ready to be called this way
1492d9fc2b81SPoul-Henning Kamp 		 * it screws up the aneg timeout because mii_tick() is
1493d9fc2b81SPoul-Henning Kamp 		 * only to be called once per second.
1494d9fc2b81SPoul-Henning Kamp 		 */
1495ce4946daSBill Paul 		if (status & NGE_IMR_PHY_INTR) {
1496ce4946daSBill Paul 			sc->nge_link = 0;
1497ad6c618bSBill Paul 			nge_tick_locked(sc);
1498ce4946daSBill Paul 		}
1499d9fc2b81SPoul-Henning Kamp #endif
1500ce4946daSBill Paul 	}
1501ce4946daSBill Paul 
1502ce4946daSBill Paul 	/* Re-enable interrupts. */
1503ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1504ce4946daSBill Paul 
1505ce4946daSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1506ad6c618bSBill Paul 		nge_start_locked(ifp);
1507ce4946daSBill Paul 
15081f548804SDoug Ambrisko 	/* Data LED off for TBI mode */
15091f548804SDoug Ambrisko 
15101f548804SDoug Ambrisko 	if(sc->nge_tbi)
15111f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
15121f548804SDoug Ambrisko 			    & ~NGE_GPIO_GP3_OUT);
15131f548804SDoug Ambrisko 
1514ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1515ad6c618bSBill Paul 
1516ce4946daSBill Paul 	return;
1517ce4946daSBill Paul }
1518ce4946daSBill Paul 
1519ce4946daSBill Paul /*
1520ce4946daSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1521ce4946daSBill Paul  * pointers to the fragment pointers.
1522ce4946daSBill Paul  */
1523eaabec55SAlfred Perlstein static int
1524eaabec55SAlfred Perlstein nge_encap(sc, m_head, txidx)
1525ce4946daSBill Paul 	struct nge_softc	*sc;
1526ce4946daSBill Paul 	struct mbuf		*m_head;
1527ce4946daSBill Paul 	u_int32_t		*txidx;
1528ce4946daSBill Paul {
1529ce4946daSBill Paul 	struct nge_desc		*f = NULL;
1530ce4946daSBill Paul 	struct mbuf		*m;
1531ce4946daSBill Paul 	int			frag, cur, cnt = 0;
1532673d9191SSam Leffler 	struct m_tag		*mtag;
1533ce4946daSBill Paul 
1534ce4946daSBill Paul 	/*
1535ce4946daSBill Paul  	 * Start packing the mbufs in this chain into
1536ce4946daSBill Paul 	 * the fragment pointers. Stop when we run out
1537ce4946daSBill Paul  	 * of fragments or hit the end of the mbuf chain.
1538ce4946daSBill Paul 	 */
1539ce4946daSBill Paul 	m = m_head;
1540ce4946daSBill Paul 	cur = frag = *txidx;
1541ce4946daSBill Paul 
1542ce4946daSBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
1543ce4946daSBill Paul 		if (m->m_len != 0) {
1544ce4946daSBill Paul 			if ((NGE_TX_LIST_CNT -
1545ce4946daSBill Paul 			    (sc->nge_cdata.nge_tx_cnt + cnt)) < 2)
1546ce4946daSBill Paul 				return(ENOBUFS);
1547ce4946daSBill Paul 			f = &sc->nge_ldata->nge_tx_list[frag];
1548ce4946daSBill Paul 			f->nge_ctl = NGE_CMDSTS_MORE | m->m_len;
1549ce4946daSBill Paul 			f->nge_ptr = vtophys(mtod(m, vm_offset_t));
1550ce4946daSBill Paul 			if (cnt != 0)
1551ce4946daSBill Paul 				f->nge_ctl |= NGE_CMDSTS_OWN;
1552ce4946daSBill Paul 			cur = frag;
1553ce4946daSBill Paul 			NGE_INC(frag, NGE_TX_LIST_CNT);
1554ce4946daSBill Paul 			cnt++;
1555ce4946daSBill Paul 		}
1556ce4946daSBill Paul 	}
1557ce4946daSBill Paul 
1558ce4946daSBill Paul 	if (m != NULL)
1559ce4946daSBill Paul 		return(ENOBUFS);
1560ce4946daSBill Paul 
15611bacd83aSBill Paul 	sc->nge_ldata->nge_tx_list[*txidx].nge_extsts = 0;
1562ce4946daSBill Paul 	if (m_head->m_pkthdr.csum_flags) {
1563ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
15641bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1565ce4946daSBill Paul 			    NGE_TXEXTSTS_IPCSUM;
1566ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
15671bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1568ce4946daSBill Paul 			    NGE_TXEXTSTS_TCPCSUM;
1569ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
15701bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1571ce4946daSBill Paul 			    NGE_TXEXTSTS_UDPCSUM;
1572ce4946daSBill Paul 	}
1573ce4946daSBill Paul 
1574fc74a9f9SBrooks Davis 	mtag = VLAN_OUTPUT_TAG(sc->nge_ifp, m_head);
1575673d9191SSam Leffler 	if (mtag != NULL) {
1576ce4946daSBill Paul 		sc->nge_ldata->nge_tx_list[cur].nge_extsts |=
157753090724SRuslan Ermilov 		    (NGE_TXEXTSTS_VLANPKT|htons(VLAN_TAG_VALUE(mtag)));
1578ce4946daSBill Paul 	}
1579ce4946daSBill Paul 
1580ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[cur].nge_mbuf = m_head;
1581ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[cur].nge_ctl &= ~NGE_CMDSTS_MORE;
1582ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[*txidx].nge_ctl |= NGE_CMDSTS_OWN;
1583ce4946daSBill Paul 	sc->nge_cdata.nge_tx_cnt += cnt;
1584ce4946daSBill Paul 	*txidx = frag;
1585ce4946daSBill Paul 
1586ce4946daSBill Paul 	return(0);
1587ce4946daSBill Paul }
1588ce4946daSBill Paul 
1589ce4946daSBill Paul /*
1590ce4946daSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1591ce4946daSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1592ce4946daSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1593ce4946daSBill Paul  * physical addresses.
1594ce4946daSBill Paul  */
1595ce4946daSBill Paul 
1596eaabec55SAlfred Perlstein static void
1597eaabec55SAlfred Perlstein nge_start(ifp)
1598ce4946daSBill Paul 	struct ifnet		*ifp;
1599ce4946daSBill Paul {
1600ce4946daSBill Paul 	struct nge_softc	*sc;
1601ad6c618bSBill Paul 
1602ad6c618bSBill Paul 	sc = ifp->if_softc;
1603ad6c618bSBill Paul 	NGE_LOCK(sc);
1604ad6c618bSBill Paul 	nge_start_locked(ifp);
1605ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1606ad6c618bSBill Paul }
1607ad6c618bSBill Paul 
1608ad6c618bSBill Paul static void
1609ad6c618bSBill Paul nge_start_locked(ifp)
1610ad6c618bSBill Paul 	struct ifnet		*ifp;
1611ad6c618bSBill Paul {
1612ad6c618bSBill Paul 	struct nge_softc	*sc;
1613ce4946daSBill Paul 	struct mbuf		*m_head = NULL;
1614ce4946daSBill Paul 	u_int32_t		idx;
1615ce4946daSBill Paul 
1616ce4946daSBill Paul 	sc = ifp->if_softc;
1617ce4946daSBill Paul 
1618ce4946daSBill Paul 	if (!sc->nge_link)
1619ce4946daSBill Paul 		return;
1620ce4946daSBill Paul 
1621ce4946daSBill Paul 	idx = sc->nge_cdata.nge_tx_prod;
1622ce4946daSBill Paul 
1623ce4946daSBill Paul 	if (ifp->if_flags & IFF_OACTIVE)
1624ce4946daSBill Paul 		return;
1625ce4946daSBill Paul 
1626ce4946daSBill Paul 	while(sc->nge_ldata->nge_tx_list[idx].nge_mbuf == NULL) {
1627ce4946daSBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
1628ce4946daSBill Paul 		if (m_head == NULL)
1629ce4946daSBill Paul 			break;
1630ce4946daSBill Paul 
1631ce4946daSBill Paul 		if (nge_encap(sc, m_head, &idx)) {
1632ce4946daSBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
1633ce4946daSBill Paul 			ifp->if_flags |= IFF_OACTIVE;
1634ce4946daSBill Paul 			break;
1635ce4946daSBill Paul 		}
1636ce4946daSBill Paul 
1637ce4946daSBill Paul 		/*
1638ce4946daSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
1639ce4946daSBill Paul 		 * to him.
1640ce4946daSBill Paul 		 */
1641673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
1642ce4946daSBill Paul 
1643ce4946daSBill Paul 	}
1644ce4946daSBill Paul 
1645ce4946daSBill Paul 	/* Transmit */
1646ce4946daSBill Paul 	sc->nge_cdata.nge_tx_prod = idx;
1647ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE);
1648ce4946daSBill Paul 
1649ce4946daSBill Paul 	/*
1650ce4946daSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
1651ce4946daSBill Paul 	 */
1652ce4946daSBill Paul 	ifp->if_timer = 5;
1653ce4946daSBill Paul 
1654ce4946daSBill Paul 	return;
1655ce4946daSBill Paul }
1656ce4946daSBill Paul 
1657eaabec55SAlfred Perlstein static void
1658eaabec55SAlfred Perlstein nge_init(xsc)
1659ce4946daSBill Paul 	void			*xsc;
1660ce4946daSBill Paul {
1661ce4946daSBill Paul 	struct nge_softc	*sc = xsc;
1662ad6c618bSBill Paul 
1663ad6c618bSBill Paul 	NGE_LOCK(sc);
1664ad6c618bSBill Paul 	nge_init_locked(sc);
1665ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1666ad6c618bSBill Paul }
1667ad6c618bSBill Paul 
1668ad6c618bSBill Paul static void
1669ad6c618bSBill Paul nge_init_locked(sc)
1670ad6c618bSBill Paul 	struct nge_softc	*sc;
1671ad6c618bSBill Paul {
1672fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->nge_ifp;
1673ce4946daSBill Paul 	struct mii_data		*mii;
1674ad6c618bSBill Paul 
1675ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1676ce4946daSBill Paul 
1677ce4946daSBill Paul 	if (ifp->if_flags & IFF_RUNNING)
1678ce4946daSBill Paul 		return;
1679ce4946daSBill Paul 
1680ce4946daSBill Paul 	/*
1681ce4946daSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
1682ce4946daSBill Paul 	 */
1683ce4946daSBill Paul 	nge_stop(sc);
1684ce4946daSBill Paul 
16851f548804SDoug Ambrisko 	if (sc->nge_tbi) {
16861f548804SDoug Ambrisko 		mii = NULL;
16871f548804SDoug Ambrisko 	} else {
1688ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
16891f548804SDoug Ambrisko 	}
1690ce4946daSBill Paul 
1691ce4946daSBill Paul 	/* Set MAC address */
1692ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0);
1693ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1694fc74a9f9SBrooks Davis 	    ((u_int16_t *)IFP2ENADDR(sc->nge_ifp))[0]);
1695ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1);
1696ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1697fc74a9f9SBrooks Davis 	    ((u_int16_t *)IFP2ENADDR(sc->nge_ifp))[1]);
1698ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2);
1699ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1700fc74a9f9SBrooks Davis 	    ((u_int16_t *)IFP2ENADDR(sc->nge_ifp))[2]);
1701ce4946daSBill Paul 
1702ce4946daSBill Paul 	/* Init circular RX list. */
1703ce4946daSBill Paul 	if (nge_list_rx_init(sc) == ENOBUFS) {
1704ce4946daSBill Paul 		printf("nge%d: initialization failed: no "
1705ce4946daSBill Paul 			"memory for rx buffers\n", sc->nge_unit);
1706ce4946daSBill Paul 		nge_stop(sc);
1707ce4946daSBill Paul 		return;
1708ce4946daSBill Paul 	}
1709ce4946daSBill Paul 
1710ce4946daSBill Paul 	/*
1711ce4946daSBill Paul 	 * Init tx descriptors.
1712ce4946daSBill Paul 	 */
1713ce4946daSBill Paul 	nge_list_tx_init(sc);
1714ce4946daSBill Paul 
1715ce4946daSBill Paul 	/*
1716ce4946daSBill Paul 	 * For the NatSemi chip, we have to explicitly enable the
1717ce4946daSBill Paul 	 * reception of ARP frames, as well as turn on the 'perfect
1718ce4946daSBill Paul 	 * match' filter where we store the station address, otherwise
1719ce4946daSBill Paul 	 * we won't receive unicasts meant for this host.
1720ce4946daSBill Paul 	 */
1721ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP);
1722ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT);
1723ce4946daSBill Paul 
1724ce4946daSBill Paul 	 /* If we want promiscuous mode, set the allframes bit. */
1725ce4946daSBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
1726ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1727ce4946daSBill Paul 	} else {
1728ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1729ce4946daSBill Paul 	}
1730ce4946daSBill Paul 
1731ce4946daSBill Paul 	/*
1732ce4946daSBill Paul 	 * Set the capture broadcast bit to capture broadcast frames.
1733ce4946daSBill Paul 	 */
1734ce4946daSBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1735ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1736ce4946daSBill Paul 	} else {
1737ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1738ce4946daSBill Paul 	}
1739ce4946daSBill Paul 
1740ce4946daSBill Paul 	/*
1741ce4946daSBill Paul 	 * Load the multicast filter.
1742ce4946daSBill Paul 	 */
1743ce4946daSBill Paul 	nge_setmulti(sc);
1744ce4946daSBill Paul 
1745ce4946daSBill Paul 	/* Turn the receive filter on */
1746ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE);
1747ce4946daSBill Paul 
1748ce4946daSBill Paul 	/*
1749ce4946daSBill Paul 	 * Load the address of the RX and TX lists.
1750ce4946daSBill Paul 	 */
1751ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_LISTPTR,
1752ce4946daSBill Paul 	    vtophys(&sc->nge_ldata->nge_rx_list[0]));
1753ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_LISTPTR,
1754ce4946daSBill Paul 	    vtophys(&sc->nge_ldata->nge_tx_list[0]));
1755ce4946daSBill Paul 
1756ce4946daSBill Paul 	/* Set RX configuration */
1757ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG);
1758ce4946daSBill Paul 	/*
1759ce4946daSBill Paul 	 * Enable hardware checksum validation for all IPv4
1760ce4946daSBill Paul 	 * packets, do not reject packets with bad checksums.
1761ce4946daSBill Paul 	 */
1762ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB);
1763ce4946daSBill Paul 
1764ce4946daSBill Paul 	/*
17659d4fe4b2SBrooks Davis 	 * Tell the chip to detect and strip VLAN tag info from
17669d4fe4b2SBrooks Davis 	 * received frames. The tag will be provided in the extsts
17679d4fe4b2SBrooks Davis 	 * field in the RX descriptors.
1768ce4946daSBill Paul 	 */
1769ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL,
1770ce4946daSBill Paul 	    NGE_VIPRXCTL_TAG_DETECT_ENB|NGE_VIPRXCTL_TAG_STRIP_ENB);
1771ce4946daSBill Paul 
1772ce4946daSBill Paul 	/* Set TX configuration */
1773ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG);
1774ce4946daSBill Paul 
1775ce4946daSBill Paul 	/*
1776ce4946daSBill Paul 	 * Enable TX IPv4 checksumming on a per-packet basis.
1777ce4946daSBill Paul 	 */
1778ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT);
1779ce4946daSBill Paul 
1780ce4946daSBill Paul 	/*
17819d4fe4b2SBrooks Davis 	 * Tell the chip to insert VLAN tags on a per-packet basis as
17829d4fe4b2SBrooks Davis 	 * dictated by the code in the frame encapsulation routine.
1783ce4946daSBill Paul 	 */
1784ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT);
1785ce4946daSBill Paul 
1786ce4946daSBill Paul 	/* Set full/half duplex mode. */
17871f548804SDoug Ambrisko 	if (sc->nge_tbi) {
17881f548804SDoug Ambrisko 		if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
17891f548804SDoug Ambrisko 		    == IFM_FDX) {
17901f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
17911f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
17921f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
17931f548804SDoug Ambrisko 		} else {
17941f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
17951f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
17961f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
17971f548804SDoug Ambrisko 		}
17981f548804SDoug Ambrisko 	} else {
1799ce4946daSBill Paul 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1800ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_TX_CFG,
1801ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1802ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1803ce4946daSBill Paul 		} else {
1804ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
1805ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1806ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1807ce4946daSBill Paul 		}
18081f548804SDoug Ambrisko 	}
1809ce4946daSBill Paul 
1810ad6c618bSBill Paul 	nge_tick_locked(sc);
1811d9fc2b81SPoul-Henning Kamp 
1812ce4946daSBill Paul 	/*
1813ce4946daSBill Paul 	 * Enable the delivery of PHY interrupts based on
181423d3a203SBill Paul 	 * link/speed/duplex status changes. Also enable the
181523d3a203SBill Paul 	 * extsts field in the DMA descriptors (needed for
181623d3a203SBill Paul 	 * TCP/IP checksum offload on transmit).
1817ce4946daSBill Paul 	 */
18182ce0498bSBill Paul 	NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD|
181923d3a203SBill Paul 	    NGE_CFG_PHYINTR_LNK|NGE_CFG_PHYINTR_DUP|NGE_CFG_EXTSTS_ENB);
1820ce4946daSBill Paul 
1821ce4946daSBill Paul 	/*
1822962315f6SBill Paul 	 * Configure interrupt holdoff (moderation). We can
1823962315f6SBill Paul 	 * have the chip delay interrupt delivery for a certain
1824962315f6SBill Paul 	 * period. Units are in 100us, and the max setting
1825962315f6SBill Paul 	 * is 25500us (0xFF x 100us). Default is a 100us holdoff.
1826962315f6SBill Paul 	 */
1827962315f6SBill Paul 	CSR_WRITE_4(sc, NGE_IHR, 0x01);
1828962315f6SBill Paul 
1829962315f6SBill Paul 	/*
1830ce4946daSBill Paul 	 * Enable interrupts.
1831ce4946daSBill Paul 	 */
1832ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS);
1833196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1834196c0df6SHidetoshi Shimokawa 	/*
1835196c0df6SHidetoshi Shimokawa 	 * ... only enable interrupts if we are not polling, make sure
1836196c0df6SHidetoshi Shimokawa 	 * they are off otherwise.
1837196c0df6SHidetoshi Shimokawa 	 */
1838fa9336b8SRobert Watson 	if (ifp->if_flags & IFF_POLLING)
1839196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 0);
1840196c0df6SHidetoshi Shimokawa 	else
1841196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1842ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1843ce4946daSBill Paul 
1844ce4946daSBill Paul 	/* Enable receiver and transmitter. */
1845ce4946daSBill Paul 	NGE_CLRBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
1846ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1847ce4946daSBill Paul 
1848ce4946daSBill Paul 	nge_ifmedia_upd(ifp);
1849ce4946daSBill Paul 
1850ce4946daSBill Paul 	ifp->if_flags |= IFF_RUNNING;
1851ce4946daSBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
1852ce4946daSBill Paul 
1853ce4946daSBill Paul 	return;
1854ce4946daSBill Paul }
1855ce4946daSBill Paul 
1856ce4946daSBill Paul /*
1857ce4946daSBill Paul  * Set media options.
1858ce4946daSBill Paul  */
1859eaabec55SAlfred Perlstein static int
1860eaabec55SAlfred Perlstein nge_ifmedia_upd(ifp)
1861ce4946daSBill Paul 	struct ifnet		*ifp;
1862ce4946daSBill Paul {
1863ce4946daSBill Paul 	struct nge_softc	*sc;
1864ce4946daSBill Paul 	struct mii_data		*mii;
1865ce4946daSBill Paul 
1866ce4946daSBill Paul 	sc = ifp->if_softc;
1867ce4946daSBill Paul 
18681f548804SDoug Ambrisko 	if (sc->nge_tbi) {
18691f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
18701f548804SDoug Ambrisko 		     == IFM_AUTO) {
18711f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR,
18721f548804SDoug Ambrisko 				CSR_READ_4(sc, NGE_TBI_ANAR)
18731f548804SDoug Ambrisko 					| NGE_TBIANAR_HDX | NGE_TBIANAR_FDX
18741f548804SDoug Ambrisko 					| NGE_TBIANAR_PS1 | NGE_TBIANAR_PS2);
18751f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG
18761f548804SDoug Ambrisko 				| NGE_TBIBMCR_RESTART_ANEG);
18771f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG);
18781f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media
18791f548804SDoug Ambrisko 			    & IFM_GMASK) == IFM_FDX) {
18801f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
18811f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
18821f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
18831f548804SDoug Ambrisko 
18841f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR, 0);
18851f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, 0);
18861f548804SDoug Ambrisko 		} else {
18871f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
18881f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
18891f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
18901f548804SDoug Ambrisko 
18911f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR, 0);
18921f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, 0);
18931f548804SDoug Ambrisko 		}
18941f548804SDoug Ambrisko 
18951f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
18961f548804SDoug Ambrisko 			    & ~NGE_GPIO_GP3_OUT);
18971f548804SDoug Ambrisko 	} else {
1898ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
1899ce4946daSBill Paul 		sc->nge_link = 0;
1900ce4946daSBill Paul 		if (mii->mii_instance) {
1901ce4946daSBill Paul 			struct mii_softc	*miisc;
1902ce4946daSBill Paul 			for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1903ce4946daSBill Paul 			    miisc = LIST_NEXT(miisc, mii_list))
1904ce4946daSBill Paul 				mii_phy_reset(miisc);
1905ce4946daSBill Paul 		}
1906ce4946daSBill Paul 		mii_mediachg(mii);
19071f548804SDoug Ambrisko 	}
1908ce4946daSBill Paul 
1909ce4946daSBill Paul 	return(0);
1910ce4946daSBill Paul }
1911ce4946daSBill Paul 
1912ce4946daSBill Paul /*
1913ce4946daSBill Paul  * Report current media status.
1914ce4946daSBill Paul  */
1915eaabec55SAlfred Perlstein static void
1916eaabec55SAlfred Perlstein nge_ifmedia_sts(ifp, ifmr)
1917ce4946daSBill Paul 	struct ifnet		*ifp;
1918ce4946daSBill Paul 	struct ifmediareq	*ifmr;
1919ce4946daSBill Paul {
1920ce4946daSBill Paul 	struct nge_softc	*sc;
1921ce4946daSBill Paul 	struct mii_data		*mii;
1922ce4946daSBill Paul 
1923ce4946daSBill Paul 	sc = ifp->if_softc;
1924ce4946daSBill Paul 
19251f548804SDoug Ambrisko 	if (sc->nge_tbi) {
19261f548804SDoug Ambrisko 		ifmr->ifm_status = IFM_AVALID;
19271f548804SDoug Ambrisko 		ifmr->ifm_active = IFM_ETHER;
19281f548804SDoug Ambrisko 
19291f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) {
19301f548804SDoug Ambrisko 			ifmr->ifm_status |= IFM_ACTIVE;
19311f548804SDoug Ambrisko 		}
19321f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_TBI_BMCR) & NGE_TBIBMCR_LOOPBACK)
19331f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_LOOP;
19341f548804SDoug Ambrisko 		if (!CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) {
19351f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_NONE;
19361f548804SDoug Ambrisko 			ifmr->ifm_status = 0;
19371f548804SDoug Ambrisko 			return;
19381f548804SDoug Ambrisko 		}
19391f548804SDoug Ambrisko 		ifmr->ifm_active |= IFM_1000_SX;
19401f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
19411f548804SDoug Ambrisko 		    == IFM_AUTO) {
19421f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_AUTO;
19431f548804SDoug Ambrisko 			if (CSR_READ_4(sc, NGE_TBI_ANLPAR)
19441f548804SDoug Ambrisko 			    & NGE_TBIANAR_FDX) {
19451f548804SDoug Ambrisko 				ifmr->ifm_active |= IFM_FDX;
19461f548804SDoug Ambrisko 			}else if (CSR_READ_4(sc, NGE_TBI_ANLPAR)
19471f548804SDoug Ambrisko 				  & NGE_TBIANAR_HDX) {
19481f548804SDoug Ambrisko 				ifmr->ifm_active |= IFM_HDX;
19491f548804SDoug Ambrisko 			}
19501f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
19511f548804SDoug Ambrisko 			== IFM_FDX)
19521f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_FDX;
19531f548804SDoug Ambrisko 		else
19541f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_HDX;
19551f548804SDoug Ambrisko 
19561f548804SDoug Ambrisko 	} else {
1957ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
1958ce4946daSBill Paul 		mii_pollstat(mii);
1959ce4946daSBill Paul 		ifmr->ifm_active = mii->mii_media_active;
1960ce4946daSBill Paul 		ifmr->ifm_status = mii->mii_media_status;
19611f548804SDoug Ambrisko 	}
1962ce4946daSBill Paul 
1963ce4946daSBill Paul 	return;
1964ce4946daSBill Paul }
1965ce4946daSBill Paul 
1966eaabec55SAlfred Perlstein static int
1967eaabec55SAlfred Perlstein nge_ioctl(ifp, command, data)
1968ce4946daSBill Paul 	struct ifnet		*ifp;
1969ce4946daSBill Paul 	u_long			command;
1970ce4946daSBill Paul 	caddr_t			data;
1971ce4946daSBill Paul {
1972ce4946daSBill Paul 	struct nge_softc	*sc = ifp->if_softc;
1973ce4946daSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
1974ce4946daSBill Paul 	struct mii_data		*mii;
1975ad6c618bSBill Paul 	int			error = 0;
1976ce4946daSBill Paul 
1977ce4946daSBill Paul 	switch(command) {
1978ce4946daSBill Paul 	case SIOCSIFMTU:
1979ce4946daSBill Paul 		if (ifr->ifr_mtu > NGE_JUMBO_MTU)
1980ce4946daSBill Paul 			error = EINVAL;
1981cb2f755cSBill Paul 		else {
1982ce4946daSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
1983cb2f755cSBill Paul 			/*
1984cb2f755cSBill Paul 			 * Workaround: if the MTU is larger than
1985cb2f755cSBill Paul 			 * 8152 (TX FIFO size minus 64 minus 18), turn off
1986cb2f755cSBill Paul 			 * TX checksum offloading.
1987cb2f755cSBill Paul 			 */
1988a5820ecbSYaroslav Tykhiy 			if (ifr->ifr_mtu >= 8152) {
1989a5820ecbSYaroslav Tykhiy 				ifp->if_capenable &= ~IFCAP_TXCSUM;
1990cb2f755cSBill Paul 				ifp->if_hwassist = 0;
1991a5820ecbSYaroslav Tykhiy 			} else {
1992a5820ecbSYaroslav Tykhiy 				ifp->if_capenable |= IFCAP_TXCSUM;
1993cb2f755cSBill Paul 				ifp->if_hwassist = NGE_CSUM_FEATURES;
1994cb2f755cSBill Paul 			}
1995a5820ecbSYaroslav Tykhiy 		}
1996ce4946daSBill Paul 		break;
1997ce4946daSBill Paul 	case SIOCSIFFLAGS:
1998ad6c618bSBill Paul 		NGE_LOCK(sc);
1999ce4946daSBill Paul 		if (ifp->if_flags & IFF_UP) {
2000ce4946daSBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
2001ce4946daSBill Paul 			    ifp->if_flags & IFF_PROMISC &&
2002ce4946daSBill Paul 			    !(sc->nge_if_flags & IFF_PROMISC)) {
2003ce4946daSBill Paul 				NGE_SETBIT(sc, NGE_RXFILT_CTL,
2004ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLPHYS|
2005ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLMULTI);
2006ce4946daSBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
2007ce4946daSBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
2008ce4946daSBill Paul 			    sc->nge_if_flags & IFF_PROMISC) {
2009ce4946daSBill Paul 				NGE_CLRBIT(sc, NGE_RXFILT_CTL,
2010ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLPHYS);
2011ce4946daSBill Paul 				if (!(ifp->if_flags & IFF_ALLMULTI))
2012ce4946daSBill Paul 					NGE_CLRBIT(sc, NGE_RXFILT_CTL,
2013ce4946daSBill Paul 					    NGE_RXFILTCTL_ALLMULTI);
2014ce4946daSBill Paul 			} else {
2015ce4946daSBill Paul 				ifp->if_flags &= ~IFF_RUNNING;
2016ad6c618bSBill Paul 				nge_init_locked(sc);
2017ce4946daSBill Paul 			}
2018ce4946daSBill Paul 		} else {
2019ce4946daSBill Paul 			if (ifp->if_flags & IFF_RUNNING)
2020ce4946daSBill Paul 				nge_stop(sc);
2021ce4946daSBill Paul 		}
2022ce4946daSBill Paul 		sc->nge_if_flags = ifp->if_flags;
2023ad6c618bSBill Paul 		NGE_UNLOCK(sc);
2024ce4946daSBill Paul 		error = 0;
2025ce4946daSBill Paul 		break;
2026ce4946daSBill Paul 	case SIOCADDMULTI:
2027ce4946daSBill Paul 	case SIOCDELMULTI:
2028ad6c618bSBill Paul 		NGE_LOCK(sc);
2029ce4946daSBill Paul 		nge_setmulti(sc);
2030ad6c618bSBill Paul 		NGE_UNLOCK(sc);
2031ce4946daSBill Paul 		error = 0;
2032ce4946daSBill Paul 		break;
2033ce4946daSBill Paul 	case SIOCGIFMEDIA:
2034ce4946daSBill Paul 	case SIOCSIFMEDIA:
20351f548804SDoug Ambrisko 		if (sc->nge_tbi) {
20361f548804SDoug Ambrisko 			error = ifmedia_ioctl(ifp, ifr, &sc->nge_ifmedia,
20371f548804SDoug Ambrisko 					      command);
20381f548804SDoug Ambrisko 		} else {
2039ce4946daSBill Paul 			mii = device_get_softc(sc->nge_miibus);
20401f548804SDoug Ambrisko 			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
20411f548804SDoug Ambrisko 					      command);
20421f548804SDoug Ambrisko 		}
2043ce4946daSBill Paul 		break;
204437f5f239SRuslan Ermilov 	case SIOCSIFCAP:
204525fbb2c3SYaroslav Tykhiy 		ifp->if_capenable &= ~IFCAP_POLLING;
204625fbb2c3SYaroslav Tykhiy 		ifp->if_capenable |= ifr->ifr_reqcap & IFCAP_POLLING;
204737f5f239SRuslan Ermilov 		break;
2048ce4946daSBill Paul 	default:
2049673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2050ce4946daSBill Paul 		break;
2051ce4946daSBill Paul 	}
2052ce4946daSBill Paul 
2053ce4946daSBill Paul 	return(error);
2054ce4946daSBill Paul }
2055ce4946daSBill Paul 
2056eaabec55SAlfred Perlstein static void
2057eaabec55SAlfred Perlstein nge_watchdog(ifp)
2058ce4946daSBill Paul 	struct ifnet		*ifp;
2059ce4946daSBill Paul {
2060ce4946daSBill Paul 	struct nge_softc	*sc;
2061ce4946daSBill Paul 
2062ce4946daSBill Paul 	sc = ifp->if_softc;
2063ce4946daSBill Paul 
2064ce4946daSBill Paul 	ifp->if_oerrors++;
2065ce4946daSBill Paul 	printf("nge%d: watchdog timeout\n", sc->nge_unit);
2066ce4946daSBill Paul 
2067ad6c618bSBill Paul 	NGE_LOCK(sc);
2068ce4946daSBill Paul 	nge_stop(sc);
2069ce4946daSBill Paul 	nge_reset(sc);
2070ce4946daSBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
2071ad6c618bSBill Paul 	nge_init_locked(sc);
2072ce4946daSBill Paul 
2073ce4946daSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
2074ad6c618bSBill Paul 		nge_start_locked(ifp);
2075ad6c618bSBill Paul 
2076ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2077ce4946daSBill Paul 
2078ce4946daSBill Paul 	return;
2079ce4946daSBill Paul }
2080ce4946daSBill Paul 
2081ce4946daSBill Paul /*
2082ce4946daSBill Paul  * Stop the adapter and free any mbufs allocated to the
2083ce4946daSBill Paul  * RX and TX lists.
2084ce4946daSBill Paul  */
2085eaabec55SAlfred Perlstein static void
2086eaabec55SAlfred Perlstein nge_stop(sc)
2087ce4946daSBill Paul 	struct nge_softc	*sc;
2088ce4946daSBill Paul {
2089ce4946daSBill Paul 	register int		i;
2090ce4946daSBill Paul 	struct ifnet		*ifp;
2091ce4946daSBill Paul 	struct mii_data		*mii;
2092ce4946daSBill Paul 
2093ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
2094fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
2095ce4946daSBill Paul 	ifp->if_timer = 0;
20961f548804SDoug Ambrisko 	if (sc->nge_tbi) {
20971f548804SDoug Ambrisko 		mii = NULL;
20981f548804SDoug Ambrisko 	} else {
2099ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
21001f548804SDoug Ambrisko 	}
2101ce4946daSBill Paul 
2102ad6c618bSBill Paul 	callout_stop(&sc->nge_stat_ch);
2103196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
2104196c0df6SHidetoshi Shimokawa 	ether_poll_deregister(ifp);
2105196c0df6SHidetoshi Shimokawa #endif
2106ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
2107ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, 0);
2108ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
2109ce4946daSBill Paul 	DELAY(1000);
2110ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_LISTPTR, 0);
2111ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_LISTPTR, 0);
2112ce4946daSBill Paul 
21131f548804SDoug Ambrisko 	if (!sc->nge_tbi)
2114279fe8d1SPoul-Henning Kamp 		mii_down(mii);
2115ce4946daSBill Paul 
2116ce4946daSBill Paul 	sc->nge_link = 0;
2117ce4946daSBill Paul 
2118ce4946daSBill Paul 	/*
2119ce4946daSBill Paul 	 * Free data in the RX lists.
2120ce4946daSBill Paul 	 */
2121ce4946daSBill Paul 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
2122ce4946daSBill Paul 		if (sc->nge_ldata->nge_rx_list[i].nge_mbuf != NULL) {
2123ce4946daSBill Paul 			m_freem(sc->nge_ldata->nge_rx_list[i].nge_mbuf);
2124ce4946daSBill Paul 			sc->nge_ldata->nge_rx_list[i].nge_mbuf = NULL;
2125ce4946daSBill Paul 		}
2126ce4946daSBill Paul 	}
2127ce4946daSBill Paul 	bzero((char *)&sc->nge_ldata->nge_rx_list,
2128ce4946daSBill Paul 		sizeof(sc->nge_ldata->nge_rx_list));
2129ce4946daSBill Paul 
2130ce4946daSBill Paul 	/*
2131ce4946daSBill Paul 	 * Free the TX list buffers.
2132ce4946daSBill Paul 	 */
2133ce4946daSBill Paul 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
2134ce4946daSBill Paul 		if (sc->nge_ldata->nge_tx_list[i].nge_mbuf != NULL) {
2135ce4946daSBill Paul 			m_freem(sc->nge_ldata->nge_tx_list[i].nge_mbuf);
2136ce4946daSBill Paul 			sc->nge_ldata->nge_tx_list[i].nge_mbuf = NULL;
2137ce4946daSBill Paul 		}
2138ce4946daSBill Paul 	}
2139ce4946daSBill Paul 
2140ce4946daSBill Paul 	bzero((char *)&sc->nge_ldata->nge_tx_list,
2141ce4946daSBill Paul 		sizeof(sc->nge_ldata->nge_tx_list));
2142ce4946daSBill Paul 
2143ce4946daSBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2144ce4946daSBill Paul 
2145ce4946daSBill Paul 	return;
2146ce4946daSBill Paul }
2147ce4946daSBill Paul 
2148ce4946daSBill Paul /*
2149ce4946daSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2150ce4946daSBill Paul  * get confused by errant DMAs when rebooting.
2151ce4946daSBill Paul  */
2152eaabec55SAlfred Perlstein static void
2153eaabec55SAlfred Perlstein nge_shutdown(dev)
2154ce4946daSBill Paul 	device_t		dev;
2155ce4946daSBill Paul {
2156ce4946daSBill Paul 	struct nge_softc	*sc;
2157ce4946daSBill Paul 
2158ce4946daSBill Paul 	sc = device_get_softc(dev);
2159ce4946daSBill Paul 
2160ad6c618bSBill Paul 	NGE_LOCK(sc);
2161ce4946daSBill Paul 	nge_reset(sc);
2162ce4946daSBill Paul 	nge_stop(sc);
2163ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2164ce4946daSBill Paul 
2165ce4946daSBill Paul 	return;
2166ce4946daSBill Paul }
2167