xref: /freebsd/sys/dev/nge/if_nge.c (revision aad970f1fee9a2a3e5a0f880be9b87c6193b3bd1)
1ce4946daSBill Paul /*
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 
918368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
928368cf8fSDavid E. O'Brien __FBSDID("$FreeBSD$");
938368cf8fSDavid E. O'Brien 
94ce4946daSBill Paul #include <sys/param.h>
95ce4946daSBill Paul #include <sys/systm.h>
96ce4946daSBill Paul #include <sys/sockio.h>
97ce4946daSBill Paul #include <sys/mbuf.h>
98ce4946daSBill Paul #include <sys/malloc.h>
99ce4946daSBill Paul #include <sys/kernel.h>
100ce4946daSBill Paul #include <sys/socket.h>
101ce4946daSBill Paul 
102ce4946daSBill Paul #include <net/if.h>
103ce4946daSBill Paul #include <net/if_arp.h>
104ce4946daSBill Paul #include <net/ethernet.h>
105ce4946daSBill Paul #include <net/if_dl.h>
106ce4946daSBill Paul #include <net/if_media.h>
107ce4946daSBill Paul #include <net/if_types.h>
108ce4946daSBill Paul #include <net/if_vlan_var.h>
109ce4946daSBill Paul 
110ce4946daSBill Paul #include <net/bpf.h>
111ce4946daSBill Paul 
112ce4946daSBill Paul #include <vm/vm.h>              /* for vtophys */
113ce4946daSBill Paul #include <vm/pmap.h>            /* for vtophys */
114ce4946daSBill Paul #include <machine/clock.h>      /* for DELAY */
115ce4946daSBill Paul #include <machine/bus_pio.h>
116ce4946daSBill Paul #include <machine/bus_memio.h>
117ce4946daSBill Paul #include <machine/bus.h>
118ce4946daSBill Paul #include <machine/resource.h>
119ce4946daSBill Paul #include <sys/bus.h>
120ce4946daSBill Paul #include <sys/rman.h>
121ce4946daSBill Paul 
122ce4946daSBill Paul #include <dev/mii/mii.h>
123ce4946daSBill Paul #include <dev/mii/miivar.h>
124ce4946daSBill Paul 
12538d8c994SWarner Losh #include <dev/pci/pcireg.h>
12638d8c994SWarner Losh #include <dev/pci/pcivar.h>
127ce4946daSBill Paul 
128ce4946daSBill Paul #define NGE_USEIOSPACE
129ce4946daSBill Paul 
1305da751e4SBill Paul #include <dev/nge/if_ngereg.h>
131ce4946daSBill Paul 
132f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, pci, 1, 1, 1);
133f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, ether, 1, 1, 1);
134ce4946daSBill Paul MODULE_DEPEND(nge, miibus, 1, 1, 1);
135ce4946daSBill Paul 
136ce4946daSBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
137ce4946daSBill Paul #include "miibus_if.h"
138ce4946daSBill Paul 
139ce4946daSBill Paul #define NGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
140ce4946daSBill Paul 
141ce4946daSBill Paul /*
142ce4946daSBill Paul  * Various supported device vendors/types and their names.
143ce4946daSBill Paul  */
144ce4946daSBill Paul static struct nge_type nge_devs[] = {
145ce4946daSBill Paul 	{ NGE_VENDORID, NGE_DEVICEID,
146ce4946daSBill Paul 	    "National Semiconductor Gigabit Ethernet" },
147ce4946daSBill Paul 	{ 0, 0, NULL }
148ce4946daSBill Paul };
149ce4946daSBill Paul 
150e51a25f8SAlfred Perlstein static int nge_probe(device_t);
151e51a25f8SAlfred Perlstein static int nge_attach(device_t);
152e51a25f8SAlfred Perlstein static int nge_detach(device_t);
153ce4946daSBill Paul 
154e51a25f8SAlfred Perlstein static int nge_alloc_jumbo_mem(struct nge_softc *);
155e51a25f8SAlfred Perlstein static void nge_free_jumbo_mem(struct nge_softc *);
156e51a25f8SAlfred Perlstein static void *nge_jalloc(struct nge_softc *);
157914596abSAlfred Perlstein static void nge_jfree(void *, void *);
158ce4946daSBill Paul 
159eaabec55SAlfred Perlstein static int nge_newbuf(struct nge_softc *, struct nge_desc *, struct mbuf *);
160eaabec55SAlfred Perlstein static int nge_encap(struct nge_softc *, struct mbuf *, u_int32_t *);
161e51a25f8SAlfred Perlstein static void nge_rxeof(struct nge_softc *);
162e51a25f8SAlfred Perlstein static void nge_txeof(struct nge_softc *);
163e51a25f8SAlfred Perlstein static void nge_intr(void *);
164e51a25f8SAlfred Perlstein static void nge_tick(void *);
165e51a25f8SAlfred Perlstein static void nge_start(struct ifnet *);
166e51a25f8SAlfred Perlstein static int nge_ioctl(struct ifnet *, u_long, caddr_t);
167e51a25f8SAlfred Perlstein static void nge_init(void *);
168e51a25f8SAlfred Perlstein static void nge_stop(struct nge_softc *);
169e51a25f8SAlfred Perlstein static void nge_watchdog(struct ifnet *);
170e51a25f8SAlfred Perlstein static void nge_shutdown(device_t);
171e51a25f8SAlfred Perlstein static int nge_ifmedia_upd(struct ifnet *);
172e51a25f8SAlfred Perlstein static void nge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
173ce4946daSBill Paul 
174e51a25f8SAlfred Perlstein static void nge_delay(struct nge_softc *);
175e51a25f8SAlfred Perlstein static void nge_eeprom_idle(struct nge_softc *);
176e51a25f8SAlfred Perlstein static void nge_eeprom_putbyte(struct nge_softc *, int);
177e51a25f8SAlfred Perlstein static void nge_eeprom_getword(struct nge_softc *, int, u_int16_t *);
178e51a25f8SAlfred Perlstein static void nge_read_eeprom(struct nge_softc *, caddr_t, int, int, int);
179ce4946daSBill Paul 
180e51a25f8SAlfred Perlstein static void nge_mii_sync(struct nge_softc *);
181e51a25f8SAlfred Perlstein static void nge_mii_send(struct nge_softc *, u_int32_t, int);
182e51a25f8SAlfred Perlstein static int nge_mii_readreg(struct nge_softc *, struct nge_mii_frame *);
183e51a25f8SAlfred Perlstein static int nge_mii_writereg(struct nge_softc *, struct nge_mii_frame *);
184ce4946daSBill Paul 
185e51a25f8SAlfred Perlstein static int nge_miibus_readreg(device_t, int, int);
186e51a25f8SAlfred Perlstein static int nge_miibus_writereg(device_t, int, int, int);
187e51a25f8SAlfred Perlstein static void nge_miibus_statchg(device_t);
188ce4946daSBill Paul 
189e51a25f8SAlfred Perlstein static void nge_setmulti(struct nge_softc *);
190e51a25f8SAlfred Perlstein static u_int32_t nge_crc(struct nge_softc *, caddr_t);
191e51a25f8SAlfred Perlstein static void nge_reset(struct nge_softc *);
192e51a25f8SAlfred Perlstein static int nge_list_rx_init(struct nge_softc *);
193e51a25f8SAlfred Perlstein static int nge_list_tx_init(struct nge_softc *);
194ce4946daSBill Paul 
195ce4946daSBill Paul #ifdef NGE_USEIOSPACE
196ce4946daSBill Paul #define NGE_RES			SYS_RES_IOPORT
197ce4946daSBill Paul #define NGE_RID			NGE_PCI_LOIO
198ce4946daSBill Paul #else
199ce4946daSBill Paul #define NGE_RES			SYS_RES_MEMORY
200ce4946daSBill Paul #define NGE_RID			NGE_PCI_LOMEM
201ce4946daSBill Paul #endif
202ce4946daSBill Paul 
203ce4946daSBill Paul static device_method_t nge_methods[] = {
204ce4946daSBill Paul 	/* Device interface */
205ce4946daSBill Paul 	DEVMETHOD(device_probe,		nge_probe),
206ce4946daSBill Paul 	DEVMETHOD(device_attach,	nge_attach),
207ce4946daSBill Paul 	DEVMETHOD(device_detach,	nge_detach),
208ce4946daSBill Paul 	DEVMETHOD(device_shutdown,	nge_shutdown),
209ce4946daSBill Paul 
210ce4946daSBill Paul 	/* bus interface */
211ce4946daSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
212ce4946daSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
213ce4946daSBill Paul 
214ce4946daSBill Paul 	/* MII interface */
215ce4946daSBill Paul 	DEVMETHOD(miibus_readreg,	nge_miibus_readreg),
216ce4946daSBill Paul 	DEVMETHOD(miibus_writereg,	nge_miibus_writereg),
217ce4946daSBill Paul 	DEVMETHOD(miibus_statchg,	nge_miibus_statchg),
218ce4946daSBill Paul 
219ce4946daSBill Paul 	{ 0, 0 }
220ce4946daSBill Paul };
221ce4946daSBill Paul 
222ce4946daSBill Paul static driver_t nge_driver = {
223ce4946daSBill Paul 	"nge",
224ce4946daSBill Paul 	nge_methods,
225ce4946daSBill Paul 	sizeof(struct nge_softc)
226ce4946daSBill Paul };
227ce4946daSBill Paul 
228ce4946daSBill Paul static devclass_t nge_devclass;
229ce4946daSBill Paul 
230f246e4a1SMatthew N. Dodd DRIVER_MODULE(nge, pci, nge_driver, nge_devclass, 0, 0);
231ce4946daSBill Paul DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0);
232ce4946daSBill Paul 
233ce4946daSBill Paul #define NGE_SETBIT(sc, reg, x)				\
234ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
235ce4946daSBill Paul 		CSR_READ_4(sc, reg) | (x))
236ce4946daSBill Paul 
237ce4946daSBill Paul #define NGE_CLRBIT(sc, reg, x)				\
238ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
239ce4946daSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
240ce4946daSBill Paul 
241ce4946daSBill Paul #define SIO_SET(x)					\
24229f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x))
243ce4946daSBill Paul 
244ce4946daSBill Paul #define SIO_CLR(x)					\
24529f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x))
246ce4946daSBill Paul 
247eaabec55SAlfred Perlstein static void
248eaabec55SAlfred Perlstein nge_delay(sc)
249ce4946daSBill Paul 	struct nge_softc	*sc;
250ce4946daSBill Paul {
251ce4946daSBill Paul 	int			idx;
252ce4946daSBill Paul 
253ce4946daSBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
254ce4946daSBill Paul 		CSR_READ_4(sc, NGE_CSR);
255ce4946daSBill Paul 
256ce4946daSBill Paul 	return;
257ce4946daSBill Paul }
258ce4946daSBill Paul 
259eaabec55SAlfred Perlstein static void
260eaabec55SAlfred Perlstein nge_eeprom_idle(sc)
261ce4946daSBill Paul 	struct nge_softc	*sc;
262ce4946daSBill Paul {
263ce4946daSBill Paul 	register int		i;
264ce4946daSBill Paul 
265ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
266ce4946daSBill Paul 	nge_delay(sc);
267ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CLK);
268ce4946daSBill Paul 	nge_delay(sc);
269ce4946daSBill Paul 
270ce4946daSBill Paul 	for (i = 0; i < 25; i++) {
271ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
272ce4946daSBill Paul 		nge_delay(sc);
273ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
274ce4946daSBill Paul 		nge_delay(sc);
275ce4946daSBill Paul 	}
276ce4946daSBill Paul 
277ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
278ce4946daSBill Paul 	nge_delay(sc);
279ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CSEL);
280ce4946daSBill Paul 	nge_delay(sc);
281ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0x00000000);
282ce4946daSBill Paul 
283ce4946daSBill Paul 	return;
284ce4946daSBill Paul }
285ce4946daSBill Paul 
286ce4946daSBill Paul /*
287ce4946daSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
288ce4946daSBill Paul  */
289eaabec55SAlfred Perlstein static void
290eaabec55SAlfred Perlstein nge_eeprom_putbyte(sc, addr)
291ce4946daSBill Paul 	struct nge_softc	*sc;
292ce4946daSBill Paul 	int			addr;
293ce4946daSBill Paul {
294ce4946daSBill Paul 	register int		d, i;
295ce4946daSBill Paul 
296ce4946daSBill Paul 	d = addr | NGE_EECMD_READ;
297ce4946daSBill Paul 
298ce4946daSBill Paul 	/*
299ce4946daSBill Paul 	 * Feed in each bit and stobe the clock.
300ce4946daSBill Paul 	 */
301ce4946daSBill Paul 	for (i = 0x400; i; i >>= 1) {
302ce4946daSBill Paul 		if (d & i) {
303ce4946daSBill Paul 			SIO_SET(NGE_MEAR_EE_DIN);
304ce4946daSBill Paul 		} else {
305ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_EE_DIN);
306ce4946daSBill Paul 		}
307ce4946daSBill Paul 		nge_delay(sc);
308ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
309ce4946daSBill Paul 		nge_delay(sc);
310ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
311ce4946daSBill Paul 		nge_delay(sc);
312ce4946daSBill Paul 	}
313ce4946daSBill Paul 
314ce4946daSBill Paul 	return;
315ce4946daSBill Paul }
316ce4946daSBill Paul 
317ce4946daSBill Paul /*
318ce4946daSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
319ce4946daSBill Paul  */
320eaabec55SAlfred Perlstein static void
321eaabec55SAlfred Perlstein nge_eeprom_getword(sc, addr, dest)
322ce4946daSBill Paul 	struct nge_softc	*sc;
323ce4946daSBill Paul 	int			addr;
324ce4946daSBill Paul 	u_int16_t		*dest;
325ce4946daSBill Paul {
326ce4946daSBill Paul 	register int		i;
327ce4946daSBill Paul 	u_int16_t		word = 0;
328ce4946daSBill Paul 
329ce4946daSBill Paul 	/* Force EEPROM to idle state. */
330ce4946daSBill Paul 	nge_eeprom_idle(sc);
331ce4946daSBill Paul 
332ce4946daSBill Paul 	/* Enter EEPROM access mode. */
333ce4946daSBill Paul 	nge_delay(sc);
334ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
335ce4946daSBill Paul 	nge_delay(sc);
336ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
337ce4946daSBill Paul 	nge_delay(sc);
338ce4946daSBill Paul 
339ce4946daSBill Paul 	/*
340ce4946daSBill Paul 	 * Send address of word we want to read.
341ce4946daSBill Paul 	 */
342ce4946daSBill Paul 	nge_eeprom_putbyte(sc, addr);
343ce4946daSBill Paul 
344ce4946daSBill Paul 	/*
345ce4946daSBill Paul 	 * Start reading bits from EEPROM.
346ce4946daSBill Paul 	 */
347ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
348ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
349ce4946daSBill Paul 		nge_delay(sc);
350ce4946daSBill Paul 		if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT)
351ce4946daSBill Paul 			word |= i;
352ce4946daSBill Paul 		nge_delay(sc);
353ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
354ce4946daSBill Paul 		nge_delay(sc);
355ce4946daSBill Paul 	}
356ce4946daSBill Paul 
357ce4946daSBill Paul 	/* Turn off EEPROM access mode. */
358ce4946daSBill Paul 	nge_eeprom_idle(sc);
359ce4946daSBill Paul 
360ce4946daSBill Paul 	*dest = word;
361ce4946daSBill Paul 
362ce4946daSBill Paul 	return;
363ce4946daSBill Paul }
364ce4946daSBill Paul 
365ce4946daSBill Paul /*
366ce4946daSBill Paul  * Read a sequence of words from the EEPROM.
367ce4946daSBill Paul  */
368eaabec55SAlfred Perlstein static void
369eaabec55SAlfred Perlstein nge_read_eeprom(sc, dest, off, cnt, swap)
370ce4946daSBill Paul 	struct nge_softc	*sc;
371ce4946daSBill Paul 	caddr_t			dest;
372ce4946daSBill Paul 	int			off;
373ce4946daSBill Paul 	int			cnt;
374ce4946daSBill Paul 	int			swap;
375ce4946daSBill Paul {
376ce4946daSBill Paul 	int			i;
377ce4946daSBill Paul 	u_int16_t		word = 0, *ptr;
378ce4946daSBill Paul 
379ce4946daSBill Paul 	for (i = 0; i < cnt; i++) {
380ce4946daSBill Paul 		nge_eeprom_getword(sc, off + i, &word);
381ce4946daSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
382ce4946daSBill Paul 		if (swap)
383ce4946daSBill Paul 			*ptr = ntohs(word);
384ce4946daSBill Paul 		else
385ce4946daSBill Paul 			*ptr = word;
386ce4946daSBill Paul 	}
387ce4946daSBill Paul 
388ce4946daSBill Paul 	return;
389ce4946daSBill Paul }
390ce4946daSBill Paul 
391ce4946daSBill Paul /*
392ce4946daSBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
393ce4946daSBill Paul  */
394eaabec55SAlfred Perlstein static void
395eaabec55SAlfred Perlstein nge_mii_sync(sc)
396ce4946daSBill Paul 	struct nge_softc		*sc;
397ce4946daSBill Paul {
398ce4946daSBill Paul 	register int		i;
399ce4946daSBill Paul 
400ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR|NGE_MEAR_MII_DATA);
401ce4946daSBill Paul 
402ce4946daSBill Paul 	for (i = 0; i < 32; i++) {
403ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
404ce4946daSBill Paul 		DELAY(1);
405ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
406ce4946daSBill Paul 		DELAY(1);
407ce4946daSBill Paul 	}
408ce4946daSBill Paul 
409ce4946daSBill Paul 	return;
410ce4946daSBill Paul }
411ce4946daSBill Paul 
412ce4946daSBill Paul /*
413ce4946daSBill Paul  * Clock a series of bits through the MII.
414ce4946daSBill Paul  */
415eaabec55SAlfred Perlstein static void
416eaabec55SAlfred Perlstein nge_mii_send(sc, bits, cnt)
417ce4946daSBill Paul 	struct nge_softc		*sc;
418ce4946daSBill Paul 	u_int32_t		bits;
419ce4946daSBill Paul 	int			cnt;
420ce4946daSBill Paul {
421ce4946daSBill Paul 	int			i;
422ce4946daSBill Paul 
423ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
424ce4946daSBill Paul 
425ce4946daSBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
426ce4946daSBill Paul                 if (bits & i) {
427ce4946daSBill Paul 			SIO_SET(NGE_MEAR_MII_DATA);
428ce4946daSBill Paul                 } else {
429ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_MII_DATA);
430ce4946daSBill Paul                 }
431ce4946daSBill Paul 		DELAY(1);
432ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
433ce4946daSBill Paul 		DELAY(1);
434ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
435ce4946daSBill Paul 	}
436ce4946daSBill Paul }
437ce4946daSBill Paul 
438ce4946daSBill Paul /*
439ce4946daSBill Paul  * Read an PHY register through the MII.
440ce4946daSBill Paul  */
441eaabec55SAlfred Perlstein static int
442eaabec55SAlfred Perlstein nge_mii_readreg(sc, frame)
443ce4946daSBill Paul 	struct nge_softc		*sc;
444ce4946daSBill Paul 	struct nge_mii_frame	*frame;
445ce4946daSBill Paul 
446ce4946daSBill Paul {
447ce4946daSBill Paul 	int			i, ack, s;
448ce4946daSBill Paul 
449ce4946daSBill Paul 	s = splimp();
450ce4946daSBill Paul 
451ce4946daSBill Paul 	/*
452ce4946daSBill Paul 	 * Set up frame for RX.
453ce4946daSBill Paul 	 */
454ce4946daSBill Paul 	frame->mii_stdelim = NGE_MII_STARTDELIM;
455ce4946daSBill Paul 	frame->mii_opcode = NGE_MII_READOP;
456ce4946daSBill Paul 	frame->mii_turnaround = 0;
457ce4946daSBill Paul 	frame->mii_data = 0;
458ce4946daSBill Paul 
459ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0);
460ce4946daSBill Paul 
461ce4946daSBill Paul 	/*
462ce4946daSBill Paul  	 * Turn on data xmit.
463ce4946daSBill Paul 	 */
464ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR);
465ce4946daSBill Paul 
466ce4946daSBill Paul 	nge_mii_sync(sc);
467ce4946daSBill Paul 
468ce4946daSBill Paul 	/*
469ce4946daSBill Paul 	 * Send command/address info.
470ce4946daSBill Paul 	 */
471ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_stdelim, 2);
472ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_opcode, 2);
473ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_phyaddr, 5);
474ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_regaddr, 5);
475ce4946daSBill Paul 
476ce4946daSBill Paul 	/* Idle bit */
477ce4946daSBill Paul 	SIO_CLR((NGE_MEAR_MII_CLK|NGE_MEAR_MII_DATA));
478ce4946daSBill Paul 	DELAY(1);
479ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
480ce4946daSBill Paul 	DELAY(1);
481ce4946daSBill Paul 
482ce4946daSBill Paul 	/* Turn off xmit. */
483ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_DIR);
484ce4946daSBill Paul 	/* Check for ack */
485ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
486ce4946daSBill Paul 	DELAY(1);
487e808cf62SMartin Blapp 	ack = CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA;
488ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
489ce4946daSBill Paul 	DELAY(1);
490ce4946daSBill Paul 
491ce4946daSBill Paul 	/*
492ce4946daSBill Paul 	 * Now try reading data bits. If the ack failed, we still
493ce4946daSBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
494ce4946daSBill Paul 	 */
495ce4946daSBill Paul 	if (ack) {
496ce4946daSBill Paul 		for(i = 0; i < 16; i++) {
497ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_MII_CLK);
498ce4946daSBill Paul 			DELAY(1);
499ce4946daSBill Paul 			SIO_SET(NGE_MEAR_MII_CLK);
500ce4946daSBill Paul 			DELAY(1);
501ce4946daSBill Paul 		}
502ce4946daSBill Paul 		goto fail;
503ce4946daSBill Paul 	}
504ce4946daSBill Paul 
505ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
506ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
507ce4946daSBill Paul 		DELAY(1);
508ce4946daSBill Paul 		if (!ack) {
509ce4946daSBill Paul 			if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA)
510ce4946daSBill Paul 				frame->mii_data |= i;
511ce4946daSBill Paul 			DELAY(1);
512ce4946daSBill Paul 		}
513ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
514ce4946daSBill Paul 		DELAY(1);
515ce4946daSBill Paul 	}
516ce4946daSBill Paul 
517ce4946daSBill Paul fail:
518ce4946daSBill Paul 
519ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
520ce4946daSBill Paul 	DELAY(1);
521ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
522ce4946daSBill Paul 	DELAY(1);
523ce4946daSBill Paul 
524ce4946daSBill Paul 	splx(s);
525ce4946daSBill Paul 
526ce4946daSBill Paul 	if (ack)
527ce4946daSBill Paul 		return(1);
528ce4946daSBill Paul 	return(0);
529ce4946daSBill Paul }
530ce4946daSBill Paul 
531ce4946daSBill Paul /*
532ce4946daSBill Paul  * Write to a PHY register through the MII.
533ce4946daSBill Paul  */
534eaabec55SAlfred Perlstein static int
535eaabec55SAlfred Perlstein nge_mii_writereg(sc, frame)
536ce4946daSBill Paul 	struct nge_softc		*sc;
537ce4946daSBill Paul 	struct nge_mii_frame	*frame;
538ce4946daSBill Paul 
539ce4946daSBill Paul {
540ce4946daSBill Paul 	int			s;
541ce4946daSBill Paul 
542ce4946daSBill Paul 	s = splimp();
543ce4946daSBill Paul 	/*
544ce4946daSBill Paul 	 * Set up frame for TX.
545ce4946daSBill Paul 	 */
546ce4946daSBill Paul 
547ce4946daSBill Paul 	frame->mii_stdelim = NGE_MII_STARTDELIM;
548ce4946daSBill Paul 	frame->mii_opcode = NGE_MII_WRITEOP;
549ce4946daSBill Paul 	frame->mii_turnaround = NGE_MII_TURNAROUND;
550ce4946daSBill Paul 
551ce4946daSBill Paul 	/*
552ce4946daSBill Paul  	 * Turn on data output.
553ce4946daSBill Paul 	 */
554ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR);
555ce4946daSBill Paul 
556ce4946daSBill Paul 	nge_mii_sync(sc);
557ce4946daSBill Paul 
558ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_stdelim, 2);
559ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_opcode, 2);
560ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_phyaddr, 5);
561ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_regaddr, 5);
562ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_turnaround, 2);
563ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_data, 16);
564ce4946daSBill Paul 
565ce4946daSBill Paul 	/* Idle bit. */
566ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
567ce4946daSBill Paul 	DELAY(1);
568ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
569ce4946daSBill Paul 	DELAY(1);
570ce4946daSBill Paul 
571ce4946daSBill Paul 	/*
572ce4946daSBill Paul 	 * Turn off xmit.
573ce4946daSBill Paul 	 */
574ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_DIR);
575ce4946daSBill Paul 
576ce4946daSBill Paul 	splx(s);
577ce4946daSBill Paul 
578ce4946daSBill Paul 	return(0);
579ce4946daSBill Paul }
580ce4946daSBill Paul 
581eaabec55SAlfred Perlstein static int
582eaabec55SAlfred Perlstein nge_miibus_readreg(dev, phy, reg)
583ce4946daSBill Paul 	device_t		dev;
584ce4946daSBill Paul 	int			phy, reg;
585ce4946daSBill Paul {
586ce4946daSBill Paul 	struct nge_softc	*sc;
587ce4946daSBill Paul 	struct nge_mii_frame	frame;
588ce4946daSBill Paul 
589ce4946daSBill Paul 	sc = device_get_softc(dev);
590ce4946daSBill Paul 
591ce4946daSBill Paul 	bzero((char *)&frame, sizeof(frame));
592ce4946daSBill Paul 
593ce4946daSBill Paul 	frame.mii_phyaddr = phy;
594ce4946daSBill Paul 	frame.mii_regaddr = reg;
595ce4946daSBill Paul 	nge_mii_readreg(sc, &frame);
596ce4946daSBill Paul 
597ce4946daSBill Paul 	return(frame.mii_data);
598ce4946daSBill Paul }
599ce4946daSBill Paul 
600eaabec55SAlfred Perlstein static int
601eaabec55SAlfred Perlstein nge_miibus_writereg(dev, phy, reg, data)
602ce4946daSBill Paul 	device_t		dev;
603ce4946daSBill Paul 	int			phy, reg, data;
604ce4946daSBill Paul {
605ce4946daSBill Paul 	struct nge_softc	*sc;
606ce4946daSBill Paul 	struct nge_mii_frame	frame;
607ce4946daSBill Paul 
608ce4946daSBill Paul 	sc = device_get_softc(dev);
609ce4946daSBill Paul 
610ce4946daSBill Paul 	bzero((char *)&frame, sizeof(frame));
611ce4946daSBill Paul 
612ce4946daSBill Paul 	frame.mii_phyaddr = phy;
613ce4946daSBill Paul 	frame.mii_regaddr = reg;
614ce4946daSBill Paul 	frame.mii_data = data;
615ce4946daSBill Paul 	nge_mii_writereg(sc, &frame);
616ce4946daSBill Paul 
617ce4946daSBill Paul 	return(0);
618ce4946daSBill Paul }
619ce4946daSBill Paul 
620eaabec55SAlfred Perlstein static void
621eaabec55SAlfred Perlstein nge_miibus_statchg(dev)
622ce4946daSBill Paul 	device_t		dev;
623ce4946daSBill Paul {
6241f548804SDoug Ambrisko 	int			status;
625ce4946daSBill Paul 	struct nge_softc	*sc;
626ce4946daSBill Paul 	struct mii_data		*mii;
627ce4946daSBill Paul 
628ce4946daSBill Paul 	sc = device_get_softc(dev);
6291f548804SDoug Ambrisko 	if (sc->nge_tbi) {
6301f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
6311f548804SDoug Ambrisko 		    == IFM_AUTO) {
6321f548804SDoug Ambrisko 			status = CSR_READ_4(sc, NGE_TBI_ANLPAR);
6331f548804SDoug Ambrisko 			if (status == 0 || status & NGE_TBIANAR_FDX) {
6341f548804SDoug Ambrisko 				NGE_SETBIT(sc, NGE_TX_CFG,
6351f548804SDoug Ambrisko 				    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6361f548804SDoug Ambrisko 				NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6371f548804SDoug Ambrisko 			} else {
6381f548804SDoug Ambrisko 				NGE_CLRBIT(sc, NGE_TX_CFG,
6391f548804SDoug Ambrisko 				    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6401f548804SDoug Ambrisko 				NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6411f548804SDoug Ambrisko 			}
6421f548804SDoug Ambrisko 
6431f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
6441f548804SDoug Ambrisko 			!= IFM_FDX) {
6451f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
6461f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6471f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6481f548804SDoug Ambrisko 		} else {
6491f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
6501f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6511f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6521f548804SDoug Ambrisko 		}
6531f548804SDoug Ambrisko 	} else {
654ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
655ce4946daSBill Paul 
656ce4946daSBill Paul 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
657ce4946daSBill Paul 		        NGE_SETBIT(sc, NGE_TX_CFG,
658ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
659ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
660ce4946daSBill Paul 		} else {
661ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
662ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
663ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
664ce4946daSBill Paul 		}
665ce4946daSBill Paul 
6662ce0498bSBill Paul 		/* If we have a 1000Mbps link, set the mode_1000 bit. */
667b418ad5cSPoul-Henning Kamp 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
6682ce0498bSBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
6692ce0498bSBill Paul 			NGE_SETBIT(sc, NGE_CFG, NGE_CFG_MODE_1000);
6702ce0498bSBill Paul 		} else {
6712ce0498bSBill Paul 			NGE_CLRBIT(sc, NGE_CFG, NGE_CFG_MODE_1000);
6722ce0498bSBill Paul 		}
6731f548804SDoug Ambrisko 	}
674ce4946daSBill Paul 	return;
675ce4946daSBill Paul }
676ce4946daSBill Paul 
677eaabec55SAlfred Perlstein static u_int32_t
678eaabec55SAlfred Perlstein nge_crc(sc, addr)
679ce4946daSBill Paul 	struct nge_softc	*sc;
680ce4946daSBill Paul 	caddr_t			addr;
681ce4946daSBill Paul {
682ce4946daSBill Paul 	u_int32_t		crc, carry;
683ce4946daSBill Paul 	int			i, j;
684ce4946daSBill Paul 	u_int8_t		c;
685ce4946daSBill Paul 
686ce4946daSBill Paul 	/* Compute CRC for the address value. */
687ce4946daSBill Paul 	crc = 0xFFFFFFFF; /* initial value */
688ce4946daSBill Paul 
689ce4946daSBill Paul 	for (i = 0; i < 6; i++) {
690ce4946daSBill Paul 		c = *(addr + i);
691ce4946daSBill Paul 		for (j = 0; j < 8; j++) {
692ce4946daSBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
693ce4946daSBill Paul 			crc <<= 1;
694ce4946daSBill Paul 			c >>= 1;
695ce4946daSBill Paul 			if (carry)
696ce4946daSBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
697ce4946daSBill Paul 		}
698ce4946daSBill Paul 	}
699ce4946daSBill Paul 
700ce4946daSBill Paul 	/*
701ce4946daSBill Paul 	 * return the filter bit position
702ce4946daSBill Paul 	 */
703ce4946daSBill Paul 
704ce4946daSBill Paul 	return((crc >> 21) & 0x00000FFF);
705ce4946daSBill Paul }
706ce4946daSBill Paul 
707eaabec55SAlfred Perlstein static void
708eaabec55SAlfred Perlstein nge_setmulti(sc)
709ce4946daSBill Paul 	struct nge_softc	*sc;
710ce4946daSBill Paul {
711ce4946daSBill Paul 	struct ifnet		*ifp;
712ce4946daSBill Paul 	struct ifmultiaddr	*ifma;
713ce4946daSBill Paul 	u_int32_t		h = 0, i, filtsave;
714ce4946daSBill Paul 	int			bit, index;
715ce4946daSBill Paul 
716ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
717ce4946daSBill Paul 
718ce4946daSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
719ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL,
720ce4946daSBill Paul 		    NGE_RXFILTCTL_MCHASH|NGE_RXFILTCTL_UCHASH);
721ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLMULTI);
722ce4946daSBill Paul 		return;
723ce4946daSBill Paul 	}
724ce4946daSBill Paul 
725ce4946daSBill Paul 	/*
726ce4946daSBill Paul 	 * We have to explicitly enable the multicast hash table
727ce4946daSBill Paul 	 * on the NatSemi chip if we want to use it, which we do.
728ce4946daSBill Paul 	 * We also have to tell it that we don't want to use the
729ce4946daSBill Paul 	 * hash table for matching unicast addresses.
730ce4946daSBill Paul 	 */
731ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_MCHASH);
732ce4946daSBill Paul 	NGE_CLRBIT(sc, NGE_RXFILT_CTL,
733ce4946daSBill Paul 	    NGE_RXFILTCTL_ALLMULTI|NGE_RXFILTCTL_UCHASH);
734ce4946daSBill Paul 
735ce4946daSBill Paul 	filtsave = CSR_READ_4(sc, NGE_RXFILT_CTL);
736ce4946daSBill Paul 
737ce4946daSBill Paul 	/* first, zot all the existing hash bits */
738ce4946daSBill Paul 	for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) {
739ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i);
740ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0);
741ce4946daSBill Paul 	}
742ce4946daSBill Paul 
743ce4946daSBill Paul 	/*
744ce4946daSBill Paul 	 * From the 11 bits returned by the crc routine, the top 7
745ce4946daSBill Paul 	 * bits represent the 16-bit word in the mcast hash table
746ce4946daSBill Paul 	 * that needs to be updated, and the lower 4 bits represent
747ce4946daSBill Paul 	 * which bit within that byte needs to be set.
748ce4946daSBill Paul 	 */
749ce4946daSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
750ce4946daSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
751ce4946daSBill Paul 			continue;
752ce4946daSBill Paul 		h = nge_crc(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
753ce4946daSBill Paul 		index = (h >> 4) & 0x7F;
754ce4946daSBill Paul 		bit = h & 0xF;
755ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL,
756ce4946daSBill Paul 		    NGE_FILTADDR_MCAST_LO + (index * 2));
757ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit));
758ce4946daSBill Paul 	}
759ce4946daSBill Paul 
760ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, filtsave);
761ce4946daSBill Paul 
762ce4946daSBill Paul 	return;
763ce4946daSBill Paul }
764ce4946daSBill Paul 
765eaabec55SAlfred Perlstein static void
766eaabec55SAlfred Perlstein nge_reset(sc)
767ce4946daSBill Paul 	struct nge_softc	*sc;
768ce4946daSBill Paul {
769ce4946daSBill Paul 	register int		i;
770ce4946daSBill Paul 
771ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET);
772ce4946daSBill Paul 
773ce4946daSBill Paul 	for (i = 0; i < NGE_TIMEOUT; i++) {
774ce4946daSBill Paul 		if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET))
775ce4946daSBill Paul 			break;
776ce4946daSBill Paul 	}
777ce4946daSBill Paul 
778ce4946daSBill Paul 	if (i == NGE_TIMEOUT)
779ce4946daSBill Paul 		printf("nge%d: reset never completed\n", sc->nge_unit);
780ce4946daSBill Paul 
781ce4946daSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
782ce4946daSBill Paul 	DELAY(1000);
783ce4946daSBill Paul 
784ce4946daSBill Paul 	/*
785ce4946daSBill Paul 	 * If this is a NetSemi chip, make sure to clear
786ce4946daSBill Paul 	 * PME mode.
787ce4946daSBill Paul 	 */
788ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS);
789ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, 0);
790ce4946daSBill Paul 
791ce4946daSBill Paul         return;
792ce4946daSBill Paul }
793ce4946daSBill Paul 
794ce4946daSBill Paul /*
795d64ada50SJens Schweikhardt  * Probe for a NatSemi chip. Check the PCI vendor and device
796ce4946daSBill Paul  * IDs against our list and return a device name if we find a match.
797ce4946daSBill Paul  */
798eaabec55SAlfred Perlstein static int
799eaabec55SAlfred Perlstein nge_probe(dev)
800ce4946daSBill Paul 	device_t		dev;
801ce4946daSBill Paul {
802ce4946daSBill Paul 	struct nge_type		*t;
803ce4946daSBill Paul 
804ce4946daSBill Paul 	t = nge_devs;
805ce4946daSBill Paul 
806ce4946daSBill Paul 	while(t->nge_name != NULL) {
807ce4946daSBill Paul 		if ((pci_get_vendor(dev) == t->nge_vid) &&
808ce4946daSBill Paul 		    (pci_get_device(dev) == t->nge_did)) {
809ce4946daSBill Paul 			device_set_desc(dev, t->nge_name);
810ce4946daSBill Paul 			return(0);
811ce4946daSBill Paul 		}
812ce4946daSBill Paul 		t++;
813ce4946daSBill Paul 	}
814ce4946daSBill Paul 
815ce4946daSBill Paul 	return(ENXIO);
816ce4946daSBill Paul }
817ce4946daSBill Paul 
818ce4946daSBill Paul /*
819ce4946daSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
820ce4946daSBill Paul  * setup and ethernet/BPF attach.
821ce4946daSBill Paul  */
822eaabec55SAlfred Perlstein static int
823eaabec55SAlfred Perlstein nge_attach(dev)
824ce4946daSBill Paul 	device_t		dev;
825ce4946daSBill Paul {
826ce4946daSBill Paul 	int			s;
827ce4946daSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
828ce4946daSBill Paul 	struct nge_softc	*sc;
829ce4946daSBill Paul 	struct ifnet		*ifp;
830ce4946daSBill Paul 	int			unit, error = 0, rid;
8311f548804SDoug Ambrisko 	const char		*sep = "";
832ce4946daSBill Paul 
833ce4946daSBill Paul 	s = splimp();
834ce4946daSBill Paul 
835ce4946daSBill Paul 	sc = device_get_softc(dev);
836ce4946daSBill Paul 	unit = device_get_unit(dev);
837ce4946daSBill Paul 	bzero(sc, sizeof(struct nge_softc));
838ce4946daSBill Paul 
8396008862bSJohn Baldwin 	mtx_init(&sc->nge_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
8406008862bSJohn Baldwin 	    MTX_DEF | MTX_RECURSE);
841fa4b32faSWarner Losh #ifndef BURN_BRIDGES
842ce4946daSBill Paul 	/*
843ce4946daSBill Paul 	 * Handle power management nonsense.
844ce4946daSBill Paul 	 */
845ce4946daSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
846ce4946daSBill Paul 		u_int32_t		iobase, membase, irq;
847ce4946daSBill Paul 
848ce4946daSBill Paul 		/* Save important PCI config data. */
849ce4946daSBill Paul 		iobase = pci_read_config(dev, NGE_PCI_LOIO, 4);
850ce4946daSBill Paul 		membase = pci_read_config(dev, NGE_PCI_LOMEM, 4);
851ce4946daSBill Paul 		irq = pci_read_config(dev, NGE_PCI_INTLINE, 4);
852ce4946daSBill Paul 
853ce4946daSBill Paul 		/* Reset the power state. */
854ce4946daSBill Paul 		printf("nge%d: chip is in D%d power mode "
855ce4946daSBill Paul 		    "-- setting to D0\n", unit,
856ce4946daSBill Paul 		    pci_get_powerstate(dev));
857ce4946daSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
858ce4946daSBill Paul 
859ce4946daSBill Paul 		/* Restore PCI config data. */
860ce4946daSBill Paul 		pci_write_config(dev, NGE_PCI_LOIO, iobase, 4);
861ce4946daSBill Paul 		pci_write_config(dev, NGE_PCI_LOMEM, membase, 4);
862ce4946daSBill Paul 		pci_write_config(dev, NGE_PCI_INTLINE, irq, 4);
863ce4946daSBill Paul 	}
864fa4b32faSWarner Losh #endif
865ce4946daSBill Paul 	/*
866ce4946daSBill Paul 	 * Map control/status registers.
867ce4946daSBill Paul 	 */
868ce4946daSBill Paul 	pci_enable_busmaster(dev);
869ce4946daSBill Paul 
870ce4946daSBill Paul 	rid = NGE_RID;
871ce4946daSBill Paul 	sc->nge_res = bus_alloc_resource(dev, NGE_RES, &rid,
872ce4946daSBill Paul 	    0, ~0, 1, RF_ACTIVE);
873ce4946daSBill Paul 
874ce4946daSBill Paul 	if (sc->nge_res == NULL) {
875ce4946daSBill Paul 		printf("nge%d: couldn't map ports/memory\n", unit);
876ce4946daSBill Paul 		error = ENXIO;
877ce4946daSBill Paul 		goto fail;
878ce4946daSBill Paul 	}
879ce4946daSBill Paul 
880ce4946daSBill Paul 	sc->nge_btag = rman_get_bustag(sc->nge_res);
881ce4946daSBill Paul 	sc->nge_bhandle = rman_get_bushandle(sc->nge_res);
882ce4946daSBill Paul 
883ce4946daSBill Paul 	/* Allocate interrupt */
884ce4946daSBill Paul 	rid = 0;
885ce4946daSBill Paul 	sc->nge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
886ce4946daSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
887ce4946daSBill Paul 
888ce4946daSBill Paul 	if (sc->nge_irq == NULL) {
889ce4946daSBill Paul 		printf("nge%d: couldn't map interrupt\n", unit);
890ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
891ce4946daSBill Paul 		error = ENXIO;
892ce4946daSBill Paul 		goto fail;
893ce4946daSBill Paul 	}
894ce4946daSBill Paul 
895ce4946daSBill Paul 	error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET,
896ce4946daSBill Paul 	    nge_intr, sc, &sc->nge_intrhand);
897ce4946daSBill Paul 
898ce4946daSBill Paul 	if (error) {
899ce4946daSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
900ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
901ce4946daSBill Paul 		printf("nge%d: couldn't set up irq\n", unit);
902ce4946daSBill Paul 		goto fail;
903ce4946daSBill Paul 	}
904ce4946daSBill Paul 
905ce4946daSBill Paul 	/* Reset the adapter. */
906ce4946daSBill Paul 	nge_reset(sc);
907ce4946daSBill Paul 
908ce4946daSBill Paul 	/*
909ce4946daSBill Paul 	 * Get station address from the EEPROM.
910ce4946daSBill Paul 	 */
911ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[4], NGE_EE_NODEADDR, 1, 0);
912ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[2], NGE_EE_NODEADDR + 1, 1, 0);
913ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[0], NGE_EE_NODEADDR + 2, 1, 0);
914ce4946daSBill Paul 
915ce4946daSBill Paul 	/*
916ce4946daSBill Paul 	 * A NatSemi chip was detected. Inform the world.
917ce4946daSBill Paul 	 */
918ce4946daSBill Paul 	printf("nge%d: Ethernet address: %6D\n", unit, eaddr, ":");
919ce4946daSBill Paul 
920ce4946daSBill Paul 	sc->nge_unit = unit;
921ce4946daSBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
922ce4946daSBill Paul 
923ce4946daSBill Paul 	sc->nge_ldata = contigmalloc(sizeof(struct nge_list_data), M_DEVBUF,
924ce4946daSBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
925ce4946daSBill Paul 
926ce4946daSBill Paul 	if (sc->nge_ldata == NULL) {
927ce4946daSBill Paul 		printf("nge%d: no memory for list buffers!\n", unit);
928ce4946daSBill Paul 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
929ce4946daSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
930ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
931ce4946daSBill Paul 		error = ENXIO;
932ce4946daSBill Paul 		goto fail;
933ce4946daSBill Paul 	}
934ce4946daSBill Paul 	bzero(sc->nge_ldata, sizeof(struct nge_list_data));
935ce4946daSBill Paul 
936ce4946daSBill Paul 	/* Try to allocate memory for jumbo buffers. */
937ce4946daSBill Paul 	if (nge_alloc_jumbo_mem(sc)) {
938ce4946daSBill Paul 		printf("nge%d: jumbo buffer allocation failed\n",
939ce4946daSBill Paul                     sc->nge_unit);
940ce4946daSBill Paul 		contigfree(sc->nge_ldata,
941ce4946daSBill Paul 		    sizeof(struct nge_list_data), M_DEVBUF);
942ce4946daSBill Paul 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
943ce4946daSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
944ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
945ce4946daSBill Paul 		error = ENXIO;
946ce4946daSBill Paul 		goto fail;
947ce4946daSBill Paul 	}
948ce4946daSBill Paul 
949ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
950ce4946daSBill Paul 	ifp->if_softc = sc;
951ce4946daSBill Paul 	ifp->if_unit = unit;
952ce4946daSBill Paul 	ifp->if_name = "nge";
953ce4946daSBill Paul 	ifp->if_mtu = ETHERMTU;
954ce4946daSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
955ce4946daSBill Paul 	ifp->if_ioctl = nge_ioctl;
956ce4946daSBill Paul 	ifp->if_output = ether_output;
957ce4946daSBill Paul 	ifp->if_start = nge_start;
958ce4946daSBill Paul 	ifp->if_watchdog = nge_watchdog;
959ce4946daSBill Paul 	ifp->if_init = nge_init;
960ce4946daSBill Paul 	ifp->if_baudrate = 1000000000;
961ce4946daSBill Paul 	ifp->if_snd.ifq_maxlen = NGE_TX_LIST_CNT - 1;
962ce4946daSBill Paul 	ifp->if_hwassist = NGE_CSUM_FEATURES;
963673d9191SSam Leffler 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING;
96484e4de28SJonathan Lemon 	ifp->if_capenable = ifp->if_capabilities;
965ce4946daSBill Paul 
966ce4946daSBill Paul 	/*
967ce4946daSBill Paul 	 * Do MII setup.
968ce4946daSBill Paul 	 */
969ce4946daSBill Paul 	if (mii_phy_probe(dev, &sc->nge_miibus,
970ce4946daSBill Paul 			  nge_ifmedia_upd, nge_ifmedia_sts)) {
9711f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) {
9721f548804SDoug Ambrisko 			sc->nge_tbi = 1;
9731f548804SDoug Ambrisko 			device_printf(dev, "Using TBI\n");
9741f548804SDoug Ambrisko 
9751f548804SDoug Ambrisko 			sc->nge_miibus = dev;
9761f548804SDoug Ambrisko 
9771f548804SDoug Ambrisko 			ifmedia_init(&sc->nge_ifmedia, 0, nge_ifmedia_upd,
9781f548804SDoug Ambrisko 				nge_ifmedia_sts);
9791f548804SDoug Ambrisko #define	ADD(m, c)	ifmedia_add(&sc->nge_ifmedia, (m), (c), NULL)
9801f548804SDoug Ambrisko #define PRINT(s)	printf("%s%s", sep, s); sep = ", "
9811f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, 0), 0);
9821f548804SDoug Ambrisko 			device_printf(dev, " ");
9831f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, 0), 0);
9841f548804SDoug Ambrisko 			PRINT("1000baseSX");
9851f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, 0),0);
9861f548804SDoug Ambrisko 			PRINT("1000baseSX-FDX");
9871f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0);
9881f548804SDoug Ambrisko 			PRINT("auto");
9891f548804SDoug Ambrisko 
9901f548804SDoug Ambrisko 			printf("\n");
9911f548804SDoug Ambrisko #undef ADD
9921f548804SDoug Ambrisko #undef PRINT
9931f548804SDoug Ambrisko 			ifmedia_set(&sc->nge_ifmedia,
9941f548804SDoug Ambrisko 				IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0));
9951f548804SDoug Ambrisko 
9961f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
9971f548804SDoug Ambrisko 				| NGE_GPIO_GP4_OUT
9981f548804SDoug Ambrisko 				| NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB
9991f548804SDoug Ambrisko 				| NGE_GPIO_GP3_OUTENB
10001f548804SDoug Ambrisko 				| NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN);
10011f548804SDoug Ambrisko 
10021f548804SDoug Ambrisko 		} else {
1003ce4946daSBill Paul 			printf("nge%d: MII without any PHY!\n", sc->nge_unit);
1004ce4946daSBill Paul 			nge_free_jumbo_mem(sc);
1005ce4946daSBill Paul 			bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
1006ce4946daSBill Paul 			bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
10071f548804SDoug Ambrisko 			bus_release_resource(dev, NGE_RES, NGE_RID,
10081f548804SDoug Ambrisko 					 sc->nge_res);
1009ce4946daSBill Paul 			error = ENXIO;
1010ce4946daSBill Paul 			goto fail;
1011ce4946daSBill Paul 		}
10121f548804SDoug Ambrisko 	}
1013ce4946daSBill Paul 
1014ce4946daSBill Paul 	/*
1015ce4946daSBill Paul 	 * Call MI attach routine.
1016ce4946daSBill Paul 	 */
1017673d9191SSam Leffler 	ether_ifattach(ifp, eaddr);
1018ce4946daSBill Paul 	callout_handle_init(&sc->nge_stat_ch);
1019ce4946daSBill Paul 
1020ce4946daSBill Paul fail:
10211f548804SDoug Ambrisko 
1022ce4946daSBill Paul 	splx(s);
1023ce4946daSBill Paul 	mtx_destroy(&sc->nge_mtx);
1024ce4946daSBill Paul 	return(error);
1025ce4946daSBill Paul }
1026ce4946daSBill Paul 
1027eaabec55SAlfred Perlstein static int
1028eaabec55SAlfred Perlstein nge_detach(dev)
1029ce4946daSBill Paul 	device_t		dev;
1030ce4946daSBill Paul {
1031ce4946daSBill Paul 	struct nge_softc	*sc;
1032ce4946daSBill Paul 	struct ifnet		*ifp;
1033ce4946daSBill Paul 	int			s;
1034ce4946daSBill Paul 
1035ce4946daSBill Paul 	s = splimp();
1036ce4946daSBill Paul 
1037ce4946daSBill Paul 	sc = device_get_softc(dev);
1038ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1039ce4946daSBill Paul 
1040ce4946daSBill Paul 	nge_reset(sc);
1041ce4946daSBill Paul 	nge_stop(sc);
1042673d9191SSam Leffler 	ether_ifdetach(ifp);
1043ce4946daSBill Paul 
1044ce4946daSBill Paul 	bus_generic_detach(dev);
10451f548804SDoug Ambrisko 	if (!sc->nge_tbi) {
1046ce4946daSBill Paul 		device_delete_child(dev, sc->nge_miibus);
10471f548804SDoug Ambrisko 	}
1048ce4946daSBill Paul 	bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
1049ce4946daSBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
1050ce4946daSBill Paul 	bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
1051ce4946daSBill Paul 
1052ce4946daSBill Paul 	contigfree(sc->nge_ldata, sizeof(struct nge_list_data), M_DEVBUF);
1053ce4946daSBill Paul 	nge_free_jumbo_mem(sc);
1054ce4946daSBill Paul 
1055ce4946daSBill Paul 	splx(s);
1056ce4946daSBill Paul 	mtx_destroy(&sc->nge_mtx);
1057ce4946daSBill Paul 
1058ce4946daSBill Paul 	return(0);
1059ce4946daSBill Paul }
1060ce4946daSBill Paul 
1061ce4946daSBill Paul /*
1062ce4946daSBill Paul  * Initialize the transmit descriptors.
1063ce4946daSBill Paul  */
1064eaabec55SAlfred Perlstein static int
1065eaabec55SAlfred Perlstein nge_list_tx_init(sc)
1066ce4946daSBill Paul 	struct nge_softc	*sc;
1067ce4946daSBill Paul {
1068ce4946daSBill Paul 	struct nge_list_data	*ld;
1069ce4946daSBill Paul 	struct nge_ring_data	*cd;
1070ce4946daSBill Paul 	int			i;
1071ce4946daSBill Paul 
1072ce4946daSBill Paul 	cd = &sc->nge_cdata;
1073ce4946daSBill Paul 	ld = sc->nge_ldata;
1074ce4946daSBill Paul 
1075ce4946daSBill Paul 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
1076ce4946daSBill Paul 		if (i == (NGE_TX_LIST_CNT - 1)) {
1077ce4946daSBill Paul 			ld->nge_tx_list[i].nge_nextdesc =
1078ce4946daSBill Paul 			    &ld->nge_tx_list[0];
1079ce4946daSBill Paul 			ld->nge_tx_list[i].nge_next =
1080ce4946daSBill Paul 			    vtophys(&ld->nge_tx_list[0]);
1081ce4946daSBill Paul 		} else {
1082ce4946daSBill Paul 			ld->nge_tx_list[i].nge_nextdesc =
1083ce4946daSBill Paul 			    &ld->nge_tx_list[i + 1];
1084ce4946daSBill Paul 			ld->nge_tx_list[i].nge_next =
1085ce4946daSBill Paul 			    vtophys(&ld->nge_tx_list[i + 1]);
1086ce4946daSBill Paul 		}
1087ce4946daSBill Paul 		ld->nge_tx_list[i].nge_mbuf = NULL;
1088ce4946daSBill Paul 		ld->nge_tx_list[i].nge_ptr = 0;
1089ce4946daSBill Paul 		ld->nge_tx_list[i].nge_ctl = 0;
1090ce4946daSBill Paul 	}
1091ce4946daSBill Paul 
1092ce4946daSBill Paul 	cd->nge_tx_prod = cd->nge_tx_cons = cd->nge_tx_cnt = 0;
1093ce4946daSBill Paul 
1094ce4946daSBill Paul 	return(0);
1095ce4946daSBill Paul }
1096ce4946daSBill Paul 
1097ce4946daSBill Paul 
1098ce4946daSBill Paul /*
1099ce4946daSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
1100ce4946daSBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
1101ce4946daSBill Paul  * points back to the first.
1102ce4946daSBill Paul  */
1103eaabec55SAlfred Perlstein static int
1104eaabec55SAlfred Perlstein nge_list_rx_init(sc)
1105ce4946daSBill Paul 	struct nge_softc	*sc;
1106ce4946daSBill Paul {
1107ce4946daSBill Paul 	struct nge_list_data	*ld;
1108ce4946daSBill Paul 	struct nge_ring_data	*cd;
1109ce4946daSBill Paul 	int			i;
1110ce4946daSBill Paul 
1111ce4946daSBill Paul 	ld = sc->nge_ldata;
1112ce4946daSBill Paul 	cd = &sc->nge_cdata;
1113ce4946daSBill Paul 
1114ce4946daSBill Paul 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
1115ce4946daSBill Paul 		if (nge_newbuf(sc, &ld->nge_rx_list[i], NULL) == ENOBUFS)
1116ce4946daSBill Paul 			return(ENOBUFS);
1117ce4946daSBill Paul 		if (i == (NGE_RX_LIST_CNT - 1)) {
1118ce4946daSBill Paul 			ld->nge_rx_list[i].nge_nextdesc =
1119ce4946daSBill Paul 			    &ld->nge_rx_list[0];
1120ce4946daSBill Paul 			ld->nge_rx_list[i].nge_next =
1121ce4946daSBill Paul 			    vtophys(&ld->nge_rx_list[0]);
1122ce4946daSBill Paul 		} else {
1123ce4946daSBill Paul 			ld->nge_rx_list[i].nge_nextdesc =
1124ce4946daSBill Paul 			    &ld->nge_rx_list[i + 1];
1125ce4946daSBill Paul 			ld->nge_rx_list[i].nge_next =
1126ce4946daSBill Paul 			    vtophys(&ld->nge_rx_list[i + 1]);
1127ce4946daSBill Paul 		}
1128ce4946daSBill Paul 	}
1129ce4946daSBill Paul 
1130ce4946daSBill Paul 	cd->nge_rx_prod = 0;
1131ce4946daSBill Paul 
1132ce4946daSBill Paul 	return(0);
1133ce4946daSBill Paul }
1134ce4946daSBill Paul 
1135ce4946daSBill Paul /*
1136ce4946daSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
1137ce4946daSBill Paul  */
1138eaabec55SAlfred Perlstein static int
1139eaabec55SAlfred Perlstein nge_newbuf(sc, c, m)
1140ce4946daSBill Paul 	struct nge_softc	*sc;
1141ce4946daSBill Paul 	struct nge_desc		*c;
1142ce4946daSBill Paul 	struct mbuf		*m;
1143ce4946daSBill Paul {
1144ce4946daSBill Paul 	struct mbuf		*m_new = NULL;
1145ce4946daSBill Paul 	caddr_t			*buf = NULL;
1146ce4946daSBill Paul 
1147ce4946daSBill Paul 	if (m == NULL) {
1148a163d034SWarner Losh 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1149ce4946daSBill Paul 		if (m_new == NULL) {
1150ce4946daSBill Paul 			printf("nge%d: no memory for rx list "
1151ce4946daSBill Paul 			    "-- packet dropped!\n", sc->nge_unit);
1152ce4946daSBill Paul 			return(ENOBUFS);
1153ce4946daSBill Paul 		}
1154ce4946daSBill Paul 
1155ce4946daSBill Paul 		/* Allocate the jumbo buffer */
1156ce4946daSBill Paul 		buf = nge_jalloc(sc);
1157ce4946daSBill Paul 		if (buf == NULL) {
1158ce4946daSBill Paul #ifdef NGE_VERBOSE
1159ce4946daSBill Paul 			printf("nge%d: jumbo allocation failed "
1160ce4946daSBill Paul 			    "-- packet dropped!\n", sc->nge_unit);
1161ce4946daSBill Paul #endif
1162ce4946daSBill Paul 			m_freem(m_new);
1163ce4946daSBill Paul 			return(ENOBUFS);
1164ce4946daSBill Paul 		}
1165ce4946daSBill Paul 		/* Attach the buffer to the mbuf */
1166ce4946daSBill Paul 		m_new->m_data = (void *)buf;
11677437599fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len = NGE_JUMBO_FRAMELEN;
11687437599fSBill Paul 		MEXTADD(m_new, buf, NGE_JUMBO_FRAMELEN, nge_jfree,
1169065a7922SBill Paul 		    (struct nge_softc *)sc, 0, EXT_NET_DRV);
1170ce4946daSBill Paul 	} else {
1171ce4946daSBill Paul 		m_new = m;
11727437599fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len = NGE_JUMBO_FRAMELEN;
1173ce4946daSBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
1174ce4946daSBill Paul 	}
1175ce4946daSBill Paul 
1176ce4946daSBill Paul 	m_adj(m_new, sizeof(u_int64_t));
1177ce4946daSBill Paul 
1178ce4946daSBill Paul 	c->nge_mbuf = m_new;
1179ce4946daSBill Paul 	c->nge_ptr = vtophys(mtod(m_new, caddr_t));
1180ce4946daSBill Paul 	c->nge_ctl = m_new->m_len;
1181ce4946daSBill Paul 	c->nge_extsts = 0;
1182ce4946daSBill Paul 
1183ce4946daSBill Paul 	return(0);
1184ce4946daSBill Paul }
1185ce4946daSBill Paul 
1186eaabec55SAlfred Perlstein static int
1187eaabec55SAlfred Perlstein nge_alloc_jumbo_mem(sc)
1188ce4946daSBill Paul 	struct nge_softc	*sc;
1189ce4946daSBill Paul {
1190ce4946daSBill Paul 	caddr_t			ptr;
1191ce4946daSBill Paul 	register int		i;
1192ce4946daSBill Paul 	struct nge_jpool_entry   *entry;
1193ce4946daSBill Paul 
1194ce4946daSBill Paul 	/* Grab a big chunk o' storage. */
1195ce4946daSBill Paul 	sc->nge_cdata.nge_jumbo_buf = contigmalloc(NGE_JMEM, M_DEVBUF,
1196ce4946daSBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1197ce4946daSBill Paul 
1198ce4946daSBill Paul 	if (sc->nge_cdata.nge_jumbo_buf == NULL) {
1199ce4946daSBill Paul 		printf("nge%d: no memory for jumbo buffers!\n", sc->nge_unit);
1200ce4946daSBill Paul 		return(ENOBUFS);
1201ce4946daSBill Paul 	}
1202ce4946daSBill Paul 
1203ce4946daSBill Paul 	SLIST_INIT(&sc->nge_jfree_listhead);
1204ce4946daSBill Paul 	SLIST_INIT(&sc->nge_jinuse_listhead);
1205ce4946daSBill Paul 
1206ce4946daSBill Paul 	/*
1207ce4946daSBill Paul 	 * Now divide it up into 9K pieces and save the addresses
1208ce4946daSBill Paul 	 * in an array.
1209ce4946daSBill Paul 	 */
1210ce4946daSBill Paul 	ptr = sc->nge_cdata.nge_jumbo_buf;
1211ce4946daSBill Paul 	for (i = 0; i < NGE_JSLOTS; i++) {
1212ce4946daSBill Paul 		sc->nge_cdata.nge_jslots[i] = ptr;
12137437599fSBill Paul 		ptr += NGE_JLEN;
1214ce4946daSBill Paul 		entry = malloc(sizeof(struct nge_jpool_entry),
1215ce4946daSBill Paul 		    M_DEVBUF, M_NOWAIT);
1216ce4946daSBill Paul 		if (entry == NULL) {
1217ce4946daSBill Paul 			printf("nge%d: no memory for jumbo "
1218ce4946daSBill Paul 			    "buffer queue!\n", sc->nge_unit);
1219ce4946daSBill Paul 			return(ENOBUFS);
1220ce4946daSBill Paul 		}
1221ce4946daSBill Paul 		entry->slot = i;
1222ce4946daSBill Paul 		SLIST_INSERT_HEAD(&sc->nge_jfree_listhead,
1223ce4946daSBill Paul 		    entry, jpool_entries);
1224ce4946daSBill Paul 	}
1225ce4946daSBill Paul 
1226ce4946daSBill Paul 	return(0);
1227ce4946daSBill Paul }
1228ce4946daSBill Paul 
1229eaabec55SAlfred Perlstein static void
1230eaabec55SAlfred Perlstein nge_free_jumbo_mem(sc)
1231ce4946daSBill Paul 	struct nge_softc	*sc;
1232ce4946daSBill Paul {
1233ce4946daSBill Paul 	register int		i;
1234ce4946daSBill Paul 	struct nge_jpool_entry   *entry;
1235ce4946daSBill Paul 
1236ce4946daSBill Paul 	for (i = 0; i < NGE_JSLOTS; i++) {
1237ce4946daSBill Paul 		entry = SLIST_FIRST(&sc->nge_jfree_listhead);
1238ce4946daSBill Paul 		SLIST_REMOVE_HEAD(&sc->nge_jfree_listhead, jpool_entries);
12397437599fSBill Paul 		free(entry, M_DEVBUF);
1240ce4946daSBill Paul 	}
1241ce4946daSBill Paul 
1242ce4946daSBill Paul 	contigfree(sc->nge_cdata.nge_jumbo_buf, NGE_JMEM, M_DEVBUF);
1243ce4946daSBill Paul 
1244ce4946daSBill Paul 	return;
1245ce4946daSBill Paul }
1246ce4946daSBill Paul 
1247ce4946daSBill Paul /*
1248ce4946daSBill Paul  * Allocate a jumbo buffer.
1249ce4946daSBill Paul  */
1250eaabec55SAlfred Perlstein static void *
1251eaabec55SAlfred Perlstein nge_jalloc(sc)
1252ce4946daSBill Paul 	struct nge_softc	*sc;
1253ce4946daSBill Paul {
1254ce4946daSBill Paul 	struct nge_jpool_entry   *entry;
1255ce4946daSBill Paul 
1256ce4946daSBill Paul 	entry = SLIST_FIRST(&sc->nge_jfree_listhead);
1257ce4946daSBill Paul 
1258ce4946daSBill Paul 	if (entry == NULL) {
1259ce4946daSBill Paul #ifdef NGE_VERBOSE
1260ce4946daSBill Paul 		printf("nge%d: no free jumbo buffers\n", sc->nge_unit);
1261ce4946daSBill Paul #endif
1262ce4946daSBill Paul 		return(NULL);
1263ce4946daSBill Paul 	}
1264ce4946daSBill Paul 
1265ce4946daSBill Paul 	SLIST_REMOVE_HEAD(&sc->nge_jfree_listhead, jpool_entries);
1266ce4946daSBill Paul 	SLIST_INSERT_HEAD(&sc->nge_jinuse_listhead, entry, jpool_entries);
1267ce4946daSBill Paul 	return(sc->nge_cdata.nge_jslots[entry->slot]);
1268ce4946daSBill Paul }
1269ce4946daSBill Paul 
1270ce4946daSBill Paul /*
1271ce4946daSBill Paul  * Release a jumbo buffer.
1272ce4946daSBill Paul  */
1273eaabec55SAlfred Perlstein static void
1274eaabec55SAlfred Perlstein nge_jfree(buf, args)
1275914596abSAlfred Perlstein 	void			*buf;
1276ce4946daSBill Paul 	void			*args;
1277ce4946daSBill Paul {
1278ce4946daSBill Paul 	struct nge_softc	*sc;
1279ce4946daSBill Paul 	int		        i;
1280ce4946daSBill Paul 	struct nge_jpool_entry   *entry;
1281ce4946daSBill Paul 
1282ce4946daSBill Paul 	/* Extract the softc struct pointer. */
1283ce4946daSBill Paul 	sc = args;
1284ce4946daSBill Paul 
1285ce4946daSBill Paul 	if (sc == NULL)
1286ce4946daSBill Paul 		panic("nge_jfree: can't find softc pointer!");
1287ce4946daSBill Paul 
1288ce4946daSBill Paul 	/* calculate the slot this buffer belongs to */
1289ce4946daSBill Paul 	i = ((vm_offset_t)buf
1290ce4946daSBill Paul 	     - (vm_offset_t)sc->nge_cdata.nge_jumbo_buf) / NGE_JLEN;
1291ce4946daSBill Paul 
1292ce4946daSBill Paul 	if ((i < 0) || (i >= NGE_JSLOTS))
1293ce4946daSBill Paul 		panic("nge_jfree: asked to free buffer that we don't manage!");
1294ce4946daSBill Paul 
1295ce4946daSBill Paul 	entry = SLIST_FIRST(&sc->nge_jinuse_listhead);
1296ce4946daSBill Paul 	if (entry == NULL)
1297ce4946daSBill Paul 		panic("nge_jfree: buffer not in use!");
1298ce4946daSBill Paul 	entry->slot = i;
1299ce4946daSBill Paul 	SLIST_REMOVE_HEAD(&sc->nge_jinuse_listhead, jpool_entries);
1300ce4946daSBill Paul 	SLIST_INSERT_HEAD(&sc->nge_jfree_listhead, entry, jpool_entries);
1301ce4946daSBill Paul 
1302ce4946daSBill Paul 	return;
1303ce4946daSBill Paul }
1304ce4946daSBill Paul /*
1305ce4946daSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
1306ce4946daSBill Paul  * the higher level protocols.
1307ce4946daSBill Paul  */
1308eaabec55SAlfred Perlstein static void
1309eaabec55SAlfred Perlstein nge_rxeof(sc)
1310ce4946daSBill Paul 	struct nge_softc	*sc;
1311ce4946daSBill Paul {
1312ce4946daSBill Paul         struct mbuf		*m;
1313ce4946daSBill Paul         struct ifnet		*ifp;
1314ce4946daSBill Paul 	struct nge_desc		*cur_rx;
1315ce4946daSBill Paul 	int			i, total_len = 0;
1316ce4946daSBill Paul 	u_int32_t		rxstat;
1317ce4946daSBill Paul 
1318ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1319ce4946daSBill Paul 	i = sc->nge_cdata.nge_rx_prod;
1320ce4946daSBill Paul 
1321ce4946daSBill Paul 	while(NGE_OWNDESC(&sc->nge_ldata->nge_rx_list[i])) {
1322ce4946daSBill Paul 		struct mbuf		*m0 = NULL;
1323ce4946daSBill Paul 		u_int32_t		extsts;
1324ce4946daSBill Paul 
1325196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1326196c0df6SHidetoshi Shimokawa 		if (ifp->if_ipending & IFF_POLLING) {
1327196c0df6SHidetoshi Shimokawa 			if (sc->rxcycles <= 0)
1328196c0df6SHidetoshi Shimokawa 				break;
1329196c0df6SHidetoshi Shimokawa 			sc->rxcycles--;
1330196c0df6SHidetoshi Shimokawa 		}
1331196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1332196c0df6SHidetoshi Shimokawa 
1333ce4946daSBill Paul 		cur_rx = &sc->nge_ldata->nge_rx_list[i];
1334ce4946daSBill Paul 		rxstat = cur_rx->nge_rxstat;
1335ce4946daSBill Paul 		extsts = cur_rx->nge_extsts;
1336ce4946daSBill Paul 		m = cur_rx->nge_mbuf;
1337ce4946daSBill Paul 		cur_rx->nge_mbuf = NULL;
1338ce4946daSBill Paul 		total_len = NGE_RXBYTES(cur_rx);
1339ce4946daSBill Paul 		NGE_INC(i, NGE_RX_LIST_CNT);
1340ce4946daSBill Paul 		/*
1341ce4946daSBill Paul 		 * If an error occurs, update stats, clear the
1342ce4946daSBill Paul 		 * status word and leave the mbuf cluster in place:
1343ce4946daSBill Paul 		 * it should simply get re-used next time this descriptor
1344ce4946daSBill Paul 	 	 * comes up in the ring.
1345ce4946daSBill Paul 		 */
1346ce4946daSBill Paul 		if (!(rxstat & NGE_CMDSTS_PKT_OK)) {
1347ce4946daSBill Paul 			ifp->if_ierrors++;
1348ce4946daSBill Paul 			nge_newbuf(sc, cur_rx, m);
1349ce4946daSBill Paul 			continue;
1350ce4946daSBill Paul 		}
1351ce4946daSBill Paul 
1352ce4946daSBill Paul 		/*
1353ce4946daSBill Paul 		 * Ok. NatSemi really screwed up here. This is the
1354ce4946daSBill Paul 		 * only gigE chip I know of with alignment constraints
1355ce4946daSBill Paul 		 * on receive buffers. RX buffers must be 64-bit aligned.
1356ce4946daSBill Paul 		 */
1357962315f6SBill Paul #ifdef __i386__
1358962315f6SBill Paul 		/*
1359962315f6SBill Paul 		 * By popular demand, ignore the alignment problems
1360962315f6SBill Paul 		 * on the Intel x86 platform. The performance hit
1361962315f6SBill Paul 		 * incurred due to unaligned accesses is much smaller
1362962315f6SBill Paul 		 * than the hit produced by forcing buffer copies all
1363962315f6SBill Paul 		 * the time, especially with jumbo frames. We still
1364962315f6SBill Paul 		 * need to fix up the alignment everywhere else though.
1365962315f6SBill Paul 		 */
1366962315f6SBill Paul 		if (nge_newbuf(sc, cur_rx, NULL) == ENOBUFS) {
1367962315f6SBill Paul #endif
1368962315f6SBill Paul 			m0 = m_devget(mtod(m, char *), total_len,
1369962315f6SBill Paul 			    ETHER_ALIGN, ifp, NULL);
1370ce4946daSBill Paul 			nge_newbuf(sc, cur_rx, m);
1371ce4946daSBill Paul 			if (m0 == NULL) {
1372ce4946daSBill Paul 				printf("nge%d: no receive buffers "
1373ce4946daSBill Paul 				    "available -- packet dropped!\n",
1374ce4946daSBill Paul 				    sc->nge_unit);
1375ce4946daSBill Paul 				ifp->if_ierrors++;
1376ce4946daSBill Paul 				continue;
1377ce4946daSBill Paul 			}
1378ce4946daSBill Paul 			m = m0;
1379962315f6SBill Paul #ifdef __i386__
1380962315f6SBill Paul 		} else {
1381962315f6SBill Paul 			m->m_pkthdr.rcvif = ifp;
1382962315f6SBill Paul 			m->m_pkthdr.len = m->m_len = total_len;
1383962315f6SBill Paul 		}
1384962315f6SBill Paul #endif
1385ce4946daSBill Paul 
1386ce4946daSBill Paul 		ifp->if_ipackets++;
1387ce4946daSBill Paul 
1388ce4946daSBill Paul 		/* Do IP checksum checking. */
1389ce4946daSBill Paul 		if (extsts & NGE_RXEXTSTS_IPPKT)
1390ce4946daSBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1391ce4946daSBill Paul 		if (!(extsts & NGE_RXEXTSTS_IPCSUMERR))
139201702579SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
13932195de46SBill Paul 		if ((extsts & NGE_RXEXTSTS_TCPPKT &&
13942195de46SBill Paul 		    !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) ||
13952195de46SBill Paul 		    (extsts & NGE_RXEXTSTS_UDPPKT &&
13962195de46SBill Paul 		    !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) {
13972195de46SBill Paul 			m->m_pkthdr.csum_flags |=
13982195de46SBill Paul 			    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1399c9215605SBill Paul 			m->m_pkthdr.csum_data = 0xffff;
14002195de46SBill Paul 		}
1401ce4946daSBill Paul 
1402ce4946daSBill Paul 		/*
1403ce4946daSBill Paul 		 * If we received a packet with a vlan tag, pass it
1404ce4946daSBill Paul 		 * to vlan_input() instead of ether_input().
1405ce4946daSBill Paul 		 */
1406ce4946daSBill Paul 		if (extsts & NGE_RXEXTSTS_VLANPKT) {
1407673d9191SSam Leffler 			VLAN_INPUT_TAG(ifp, m,
1408673d9191SSam Leffler 				extsts & NGE_RXEXTSTS_VTCI, continue);
1409ce4946daSBill Paul 		}
1410ce4946daSBill Paul 
1411673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
1412ce4946daSBill Paul 	}
1413ce4946daSBill Paul 
1414ce4946daSBill Paul 	sc->nge_cdata.nge_rx_prod = i;
1415ce4946daSBill Paul 
1416ce4946daSBill Paul 	return;
1417ce4946daSBill Paul }
1418ce4946daSBill Paul 
1419ce4946daSBill Paul /*
1420ce4946daSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
1421ce4946daSBill Paul  * the list buffers.
1422ce4946daSBill Paul  */
1423ce4946daSBill Paul 
1424eaabec55SAlfred Perlstein static void
1425eaabec55SAlfred Perlstein nge_txeof(sc)
1426ce4946daSBill Paul 	struct nge_softc	*sc;
1427ce4946daSBill Paul {
1428ce4946daSBill Paul 	struct nge_desc		*cur_tx = NULL;
1429ce4946daSBill Paul 	struct ifnet		*ifp;
1430ce4946daSBill Paul 	u_int32_t		idx;
1431ce4946daSBill Paul 
1432ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1433ce4946daSBill Paul 
1434ce4946daSBill Paul 	/* Clear the timeout timer. */
1435ce4946daSBill Paul 	ifp->if_timer = 0;
1436ce4946daSBill Paul 
1437ce4946daSBill Paul 	/*
1438ce4946daSBill Paul 	 * Go through our tx list and free mbufs for those
1439ce4946daSBill Paul 	 * frames that have been transmitted.
1440ce4946daSBill Paul 	 */
1441ce4946daSBill Paul 	idx = sc->nge_cdata.nge_tx_cons;
1442ce4946daSBill Paul 	while (idx != sc->nge_cdata.nge_tx_prod) {
1443ce4946daSBill Paul 		cur_tx = &sc->nge_ldata->nge_tx_list[idx];
1444ce4946daSBill Paul 
1445ce4946daSBill Paul 		if (NGE_OWNDESC(cur_tx))
1446ce4946daSBill Paul 			break;
1447ce4946daSBill Paul 
1448ce4946daSBill Paul 		if (cur_tx->nge_ctl & NGE_CMDSTS_MORE) {
1449ce4946daSBill Paul 			sc->nge_cdata.nge_tx_cnt--;
1450ce4946daSBill Paul 			NGE_INC(idx, NGE_TX_LIST_CNT);
1451ce4946daSBill Paul 			continue;
1452ce4946daSBill Paul 		}
1453ce4946daSBill Paul 
1454ce4946daSBill Paul 		if (!(cur_tx->nge_ctl & NGE_CMDSTS_PKT_OK)) {
1455ce4946daSBill Paul 			ifp->if_oerrors++;
1456ce4946daSBill Paul 			if (cur_tx->nge_txstat & NGE_TXSTAT_EXCESSCOLLS)
1457ce4946daSBill Paul 				ifp->if_collisions++;
1458ce4946daSBill Paul 			if (cur_tx->nge_txstat & NGE_TXSTAT_OUTOFWINCOLL)
1459ce4946daSBill Paul 				ifp->if_collisions++;
1460ce4946daSBill Paul 		}
1461ce4946daSBill Paul 
1462ce4946daSBill Paul 		ifp->if_collisions +=
1463ce4946daSBill Paul 		    (cur_tx->nge_txstat & NGE_TXSTAT_COLLCNT) >> 16;
1464ce4946daSBill Paul 
1465ce4946daSBill Paul 		ifp->if_opackets++;
1466ce4946daSBill Paul 		if (cur_tx->nge_mbuf != NULL) {
1467ce4946daSBill Paul 			m_freem(cur_tx->nge_mbuf);
1468ce4946daSBill Paul 			cur_tx->nge_mbuf = NULL;
1469ce4946daSBill Paul 		}
1470ce4946daSBill Paul 
1471ce4946daSBill Paul 		sc->nge_cdata.nge_tx_cnt--;
1472ce4946daSBill Paul 		NGE_INC(idx, NGE_TX_LIST_CNT);
1473ce4946daSBill Paul 		ifp->if_timer = 0;
1474ce4946daSBill Paul 	}
1475ce4946daSBill Paul 
1476ce4946daSBill Paul 	sc->nge_cdata.nge_tx_cons = idx;
1477ce4946daSBill Paul 
1478ce4946daSBill Paul 	if (cur_tx != NULL)
1479ce4946daSBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
1480ce4946daSBill Paul 
1481ce4946daSBill Paul 	return;
1482ce4946daSBill Paul }
1483ce4946daSBill Paul 
1484eaabec55SAlfred Perlstein static void
1485eaabec55SAlfred Perlstein nge_tick(xsc)
1486ce4946daSBill Paul 	void			*xsc;
1487ce4946daSBill Paul {
1488ce4946daSBill Paul 	struct nge_softc	*sc;
1489ce4946daSBill Paul 	struct mii_data		*mii;
1490ce4946daSBill Paul 	struct ifnet		*ifp;
1491ce4946daSBill Paul 	int			s;
1492ce4946daSBill Paul 
1493ce4946daSBill Paul 	s = splimp();
1494ce4946daSBill Paul 
1495ce4946daSBill Paul 	sc = xsc;
1496ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1497ce4946daSBill Paul 
14981f548804SDoug Ambrisko 	if (sc->nge_tbi) {
14991f548804SDoug Ambrisko 		if (!sc->nge_link) {
15001f548804SDoug Ambrisko 			if (CSR_READ_4(sc, NGE_TBI_BMSR)
15011f548804SDoug Ambrisko 			    & NGE_TBIBMSR_ANEG_DONE) {
15021f548804SDoug Ambrisko 				printf("nge%d: gigabit link up\n",
15031f548804SDoug Ambrisko 				    sc->nge_unit);
15041f548804SDoug Ambrisko 				nge_miibus_statchg(sc->nge_miibus);
15051f548804SDoug Ambrisko 				sc->nge_link++;
15061f548804SDoug Ambrisko 				if (ifp->if_snd.ifq_head != NULL)
15071f548804SDoug Ambrisko 					nge_start(ifp);
15081f548804SDoug Ambrisko 			}
15091f548804SDoug Ambrisko 		}
15101f548804SDoug Ambrisko 	} else {
1511ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
1512ce4946daSBill Paul 		mii_tick(mii);
1513ce4946daSBill Paul 
1514ce4946daSBill Paul 		if (!sc->nge_link) {
1515ce4946daSBill Paul 			if (mii->mii_media_status & IFM_ACTIVE &&
1516ce4946daSBill Paul 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1517ce4946daSBill Paul 				sc->nge_link++;
15181f548804SDoug Ambrisko 				if (IFM_SUBTYPE(mii->mii_media_active)
15191f548804SDoug Ambrisko 				    == IFM_1000_T)
1520ce4946daSBill Paul 					printf("nge%d: gigabit link up\n",
1521ce4946daSBill Paul 					    sc->nge_unit);
1522ce4946daSBill Paul 				if (ifp->if_snd.ifq_head != NULL)
1523ce4946daSBill Paul 					nge_start(ifp);
1524ce4946daSBill Paul 			}
1525d9fc2b81SPoul-Henning Kamp 		}
15261f548804SDoug Ambrisko 	}
1527d9fc2b81SPoul-Henning Kamp 	sc->nge_stat_ch = timeout(nge_tick, sc, hz);
1528ce4946daSBill Paul 
1529ce4946daSBill Paul 	splx(s);
1530ce4946daSBill Paul 
1531ce4946daSBill Paul 	return;
1532ce4946daSBill Paul }
1533ce4946daSBill Paul 
1534196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1535196c0df6SHidetoshi Shimokawa static poll_handler_t nge_poll;
1536196c0df6SHidetoshi Shimokawa 
1537196c0df6SHidetoshi Shimokawa static void
1538196c0df6SHidetoshi Shimokawa nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1539196c0df6SHidetoshi Shimokawa {
1540196c0df6SHidetoshi Shimokawa 	struct  nge_softc *sc = ifp->if_softc;
1541196c0df6SHidetoshi Shimokawa 
1542196c0df6SHidetoshi Shimokawa 	if (cmd == POLL_DEREGISTER) {	/* final call, enable interrupts */
1543196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 1);
1544196c0df6SHidetoshi Shimokawa 		return;
1545196c0df6SHidetoshi Shimokawa 	}
1546196c0df6SHidetoshi Shimokawa 
1547196c0df6SHidetoshi Shimokawa 	/*
1548196c0df6SHidetoshi Shimokawa 	 * On the nge, reading the status register also clears it.
1549196c0df6SHidetoshi Shimokawa 	 * So before returning to intr mode we must make sure that all
1550196c0df6SHidetoshi Shimokawa 	 * possible pending sources of interrupts have been served.
1551196c0df6SHidetoshi Shimokawa 	 * In practice this means run to completion the *eof routines,
1552196c0df6SHidetoshi Shimokawa 	 * and then call the interrupt routine
1553196c0df6SHidetoshi Shimokawa 	 */
1554196c0df6SHidetoshi Shimokawa 	sc->rxcycles = count;
1555196c0df6SHidetoshi Shimokawa 	nge_rxeof(sc);
1556196c0df6SHidetoshi Shimokawa 	nge_txeof(sc);
1557196c0df6SHidetoshi Shimokawa 	if (ifp->if_snd.ifq_head != NULL)
1558196c0df6SHidetoshi Shimokawa 		nge_start(ifp);
1559196c0df6SHidetoshi Shimokawa 
1560196c0df6SHidetoshi Shimokawa 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1561196c0df6SHidetoshi Shimokawa 		u_int32_t	status;
1562196c0df6SHidetoshi Shimokawa 
1563196c0df6SHidetoshi Shimokawa 		/* Reading the ISR register clears all interrupts. */
1564196c0df6SHidetoshi Shimokawa 		status = CSR_READ_4(sc, NGE_ISR);
1565196c0df6SHidetoshi Shimokawa 
1566196c0df6SHidetoshi Shimokawa 		if (status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW))
1567196c0df6SHidetoshi Shimokawa 			nge_rxeof(sc);
1568196c0df6SHidetoshi Shimokawa 
1569196c0df6SHidetoshi Shimokawa 		if (status & (NGE_ISR_RX_IDLE))
1570196c0df6SHidetoshi Shimokawa 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1571196c0df6SHidetoshi Shimokawa 
1572196c0df6SHidetoshi Shimokawa 		if (status & NGE_ISR_SYSERR) {
1573196c0df6SHidetoshi Shimokawa 			nge_reset(sc);
1574196c0df6SHidetoshi Shimokawa 			nge_init(sc);
1575196c0df6SHidetoshi Shimokawa 		}
1576196c0df6SHidetoshi Shimokawa 	}
1577196c0df6SHidetoshi Shimokawa }
1578196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1579196c0df6SHidetoshi Shimokawa 
1580eaabec55SAlfred Perlstein static void
1581eaabec55SAlfred Perlstein nge_intr(arg)
1582ce4946daSBill Paul 	void			*arg;
1583ce4946daSBill Paul {
1584ce4946daSBill Paul 	struct nge_softc	*sc;
1585ce4946daSBill Paul 	struct ifnet		*ifp;
1586ce4946daSBill Paul 	u_int32_t		status;
1587ce4946daSBill Paul 
1588ce4946daSBill Paul 	sc = arg;
1589ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1590ce4946daSBill Paul 
1591196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1592196c0df6SHidetoshi Shimokawa 	if (ifp->if_ipending & IFF_POLLING)
1593196c0df6SHidetoshi Shimokawa 		return;
1594196c0df6SHidetoshi Shimokawa 	if (ether_poll_register(nge_poll, ifp)) { /* ok, disable interrupts */
1595196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 0);
1596196c0df6SHidetoshi Shimokawa 		nge_poll(ifp, 0, 1);
1597196c0df6SHidetoshi Shimokawa 		return;
1598196c0df6SHidetoshi Shimokawa 	}
1599196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1600196c0df6SHidetoshi Shimokawa 
1601ce4946daSBill Paul 	/* Supress unwanted interrupts */
1602ce4946daSBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
1603ce4946daSBill Paul 		nge_stop(sc);
1604ce4946daSBill Paul 		return;
1605ce4946daSBill Paul 	}
1606ce4946daSBill Paul 
1607ce4946daSBill Paul 	/* Disable interrupts. */
1608ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
1609ce4946daSBill Paul 
16101f548804SDoug Ambrisko 	/* Data LED on for TBI mode */
16111f548804SDoug Ambrisko 	if(sc->nge_tbi)
16121f548804SDoug Ambrisko 		 CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
16131f548804SDoug Ambrisko 			     | NGE_GPIO_GP3_OUT);
16141f548804SDoug Ambrisko 
1615ce4946daSBill Paul 	for (;;) {
1616ce4946daSBill Paul 		/* Reading the ISR register clears all interrupts. */
1617ce4946daSBill Paul 		status = CSR_READ_4(sc, NGE_ISR);
1618ce4946daSBill Paul 
1619ce4946daSBill Paul 		if ((status & NGE_INTRS) == 0)
1620ce4946daSBill Paul 			break;
1621ce4946daSBill Paul 
1622ce4946daSBill Paul 		if ((status & NGE_ISR_TX_DESC_OK) ||
1623ce4946daSBill Paul 		    (status & NGE_ISR_TX_ERR) ||
1624ce4946daSBill Paul 		    (status & NGE_ISR_TX_OK) ||
1625ce4946daSBill Paul 		    (status & NGE_ISR_TX_IDLE))
1626ce4946daSBill Paul 			nge_txeof(sc);
1627ce4946daSBill Paul 
1628ce4946daSBill Paul 		if ((status & NGE_ISR_RX_DESC_OK) ||
1629248fa967SBill Paul 		    (status & NGE_ISR_RX_ERR) ||
16308ce3678aSBill Paul 		    (status & NGE_ISR_RX_OFLOW) ||
1631ff7ed9f7SPoul-Henning Kamp 		    (status & NGE_ISR_RX_FIFO_OFLOW) ||
1632ff7ed9f7SPoul-Henning Kamp 		    (status & NGE_ISR_RX_IDLE) ||
1633ce4946daSBill Paul 		    (status & NGE_ISR_RX_OK))
1634ce4946daSBill Paul 			nge_rxeof(sc);
1635ff7ed9f7SPoul-Henning Kamp 
1636ff7ed9f7SPoul-Henning Kamp 		if ((status & NGE_ISR_RX_IDLE))
1637ff7ed9f7SPoul-Henning Kamp 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1638ff7ed9f7SPoul-Henning Kamp 
1639ce4946daSBill Paul 		if (status & NGE_ISR_SYSERR) {
1640ce4946daSBill Paul 			nge_reset(sc);
1641ce4946daSBill Paul 			ifp->if_flags &= ~IFF_RUNNING;
1642ce4946daSBill Paul 			nge_init(sc);
1643ce4946daSBill Paul 		}
1644ce4946daSBill Paul 
1645d9fc2b81SPoul-Henning Kamp #if 0
1646d9fc2b81SPoul-Henning Kamp 		/*
1647d9fc2b81SPoul-Henning Kamp 		 * XXX: nge_tick() is not ready to be called this way
1648d9fc2b81SPoul-Henning Kamp 		 * it screws up the aneg timeout because mii_tick() is
1649d9fc2b81SPoul-Henning Kamp 		 * only to be called once per second.
1650d9fc2b81SPoul-Henning Kamp 		 */
1651ce4946daSBill Paul 		if (status & NGE_IMR_PHY_INTR) {
1652ce4946daSBill Paul 			sc->nge_link = 0;
1653ce4946daSBill Paul 			nge_tick(sc);
1654ce4946daSBill Paul 		}
1655d9fc2b81SPoul-Henning Kamp #endif
1656ce4946daSBill Paul 	}
1657ce4946daSBill Paul 
1658ce4946daSBill Paul 	/* Re-enable interrupts. */
1659ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1660ce4946daSBill Paul 
1661ce4946daSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1662ce4946daSBill Paul 		nge_start(ifp);
1663ce4946daSBill Paul 
16641f548804SDoug Ambrisko 	/* Data LED off for TBI mode */
16651f548804SDoug Ambrisko 
16661f548804SDoug Ambrisko 	if(sc->nge_tbi)
16671f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
16681f548804SDoug Ambrisko 			    & ~NGE_GPIO_GP3_OUT);
16691f548804SDoug Ambrisko 
1670ce4946daSBill Paul 	return;
1671ce4946daSBill Paul }
1672ce4946daSBill Paul 
1673ce4946daSBill Paul /*
1674ce4946daSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1675ce4946daSBill Paul  * pointers to the fragment pointers.
1676ce4946daSBill Paul  */
1677eaabec55SAlfred Perlstein static int
1678eaabec55SAlfred Perlstein nge_encap(sc, m_head, txidx)
1679ce4946daSBill Paul 	struct nge_softc	*sc;
1680ce4946daSBill Paul 	struct mbuf		*m_head;
1681ce4946daSBill Paul 	u_int32_t		*txidx;
1682ce4946daSBill Paul {
1683ce4946daSBill Paul 	struct nge_desc		*f = NULL;
1684ce4946daSBill Paul 	struct mbuf		*m;
1685ce4946daSBill Paul 	int			frag, cur, cnt = 0;
1686673d9191SSam Leffler 	struct m_tag		*mtag;
1687ce4946daSBill Paul 
1688ce4946daSBill Paul 	/*
1689ce4946daSBill Paul  	 * Start packing the mbufs in this chain into
1690ce4946daSBill Paul 	 * the fragment pointers. Stop when we run out
1691ce4946daSBill Paul  	 * of fragments or hit the end of the mbuf chain.
1692ce4946daSBill Paul 	 */
1693ce4946daSBill Paul 	m = m_head;
1694ce4946daSBill Paul 	cur = frag = *txidx;
1695ce4946daSBill Paul 
1696ce4946daSBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
1697ce4946daSBill Paul 		if (m->m_len != 0) {
1698ce4946daSBill Paul 			if ((NGE_TX_LIST_CNT -
1699ce4946daSBill Paul 			    (sc->nge_cdata.nge_tx_cnt + cnt)) < 2)
1700ce4946daSBill Paul 				return(ENOBUFS);
1701ce4946daSBill Paul 			f = &sc->nge_ldata->nge_tx_list[frag];
1702ce4946daSBill Paul 			f->nge_ctl = NGE_CMDSTS_MORE | m->m_len;
1703ce4946daSBill Paul 			f->nge_ptr = vtophys(mtod(m, vm_offset_t));
1704ce4946daSBill Paul 			if (cnt != 0)
1705ce4946daSBill Paul 				f->nge_ctl |= NGE_CMDSTS_OWN;
1706ce4946daSBill Paul 			cur = frag;
1707ce4946daSBill Paul 			NGE_INC(frag, NGE_TX_LIST_CNT);
1708ce4946daSBill Paul 			cnt++;
1709ce4946daSBill Paul 		}
1710ce4946daSBill Paul 	}
1711ce4946daSBill Paul 
1712ce4946daSBill Paul 	if (m != NULL)
1713ce4946daSBill Paul 		return(ENOBUFS);
1714ce4946daSBill Paul 
17151bacd83aSBill Paul 	sc->nge_ldata->nge_tx_list[*txidx].nge_extsts = 0;
1716ce4946daSBill Paul 	if (m_head->m_pkthdr.csum_flags) {
1717ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
17181bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1719ce4946daSBill Paul 			    NGE_TXEXTSTS_IPCSUM;
1720ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
17211bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1722ce4946daSBill Paul 			    NGE_TXEXTSTS_TCPCSUM;
1723ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
17241bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1725ce4946daSBill Paul 			    NGE_TXEXTSTS_UDPCSUM;
1726ce4946daSBill Paul 	}
1727ce4946daSBill Paul 
1728673d9191SSam Leffler 	mtag = VLAN_OUTPUT_TAG(&sc->arpcom.ac_if, m);
1729673d9191SSam Leffler 	if (mtag != NULL) {
1730ce4946daSBill Paul 		sc->nge_ldata->nge_tx_list[cur].nge_extsts |=
1731673d9191SSam Leffler 			(NGE_TXEXTSTS_VLANPKT|VLAN_TAG_VALUE(mtag));
1732ce4946daSBill Paul 	}
1733ce4946daSBill Paul 
1734ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[cur].nge_mbuf = m_head;
1735ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[cur].nge_ctl &= ~NGE_CMDSTS_MORE;
1736ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[*txidx].nge_ctl |= NGE_CMDSTS_OWN;
1737ce4946daSBill Paul 	sc->nge_cdata.nge_tx_cnt += cnt;
1738ce4946daSBill Paul 	*txidx = frag;
1739ce4946daSBill Paul 
1740ce4946daSBill Paul 	return(0);
1741ce4946daSBill Paul }
1742ce4946daSBill Paul 
1743ce4946daSBill Paul /*
1744ce4946daSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1745ce4946daSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1746ce4946daSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1747ce4946daSBill Paul  * physical addresses.
1748ce4946daSBill Paul  */
1749ce4946daSBill Paul 
1750eaabec55SAlfred Perlstein static void
1751eaabec55SAlfred Perlstein nge_start(ifp)
1752ce4946daSBill Paul 	struct ifnet		*ifp;
1753ce4946daSBill Paul {
1754ce4946daSBill Paul 	struct nge_softc	*sc;
1755ce4946daSBill Paul 	struct mbuf		*m_head = NULL;
1756ce4946daSBill Paul 	u_int32_t		idx;
1757ce4946daSBill Paul 
1758ce4946daSBill Paul 	sc = ifp->if_softc;
1759ce4946daSBill Paul 
1760ce4946daSBill Paul 	if (!sc->nge_link)
1761ce4946daSBill Paul 		return;
1762ce4946daSBill Paul 
1763ce4946daSBill Paul 	idx = sc->nge_cdata.nge_tx_prod;
1764ce4946daSBill Paul 
1765ce4946daSBill Paul 	if (ifp->if_flags & IFF_OACTIVE)
1766ce4946daSBill Paul 		return;
1767ce4946daSBill Paul 
1768ce4946daSBill Paul 	while(sc->nge_ldata->nge_tx_list[idx].nge_mbuf == NULL) {
1769ce4946daSBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
1770ce4946daSBill Paul 		if (m_head == NULL)
1771ce4946daSBill Paul 			break;
1772ce4946daSBill Paul 
1773ce4946daSBill Paul 		if (nge_encap(sc, m_head, &idx)) {
1774ce4946daSBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
1775ce4946daSBill Paul 			ifp->if_flags |= IFF_OACTIVE;
1776ce4946daSBill Paul 			break;
1777ce4946daSBill Paul 		}
1778ce4946daSBill Paul 
1779ce4946daSBill Paul 		/*
1780ce4946daSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
1781ce4946daSBill Paul 		 * to him.
1782ce4946daSBill Paul 		 */
1783673d9191SSam Leffler 		BPF_MTAP(ifp, m_head);
1784ce4946daSBill Paul 
1785ce4946daSBill Paul 	}
1786ce4946daSBill Paul 
1787ce4946daSBill Paul 	/* Transmit */
1788ce4946daSBill Paul 	sc->nge_cdata.nge_tx_prod = idx;
1789ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE);
1790ce4946daSBill Paul 
1791ce4946daSBill Paul 	/*
1792ce4946daSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
1793ce4946daSBill Paul 	 */
1794ce4946daSBill Paul 	ifp->if_timer = 5;
1795ce4946daSBill Paul 
1796ce4946daSBill Paul 	return;
1797ce4946daSBill Paul }
1798ce4946daSBill Paul 
1799eaabec55SAlfred Perlstein static void
1800eaabec55SAlfred Perlstein nge_init(xsc)
1801ce4946daSBill Paul 	void			*xsc;
1802ce4946daSBill Paul {
1803ce4946daSBill Paul 	struct nge_softc	*sc = xsc;
1804ce4946daSBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1805ce4946daSBill Paul 	struct mii_data		*mii;
1806ce4946daSBill Paul 	int			s;
1807ce4946daSBill Paul 
1808ce4946daSBill Paul 	if (ifp->if_flags & IFF_RUNNING)
1809ce4946daSBill Paul 		return;
1810ce4946daSBill Paul 
1811ce4946daSBill Paul 	s = splimp();
1812ce4946daSBill Paul 
1813ce4946daSBill Paul 	/*
1814ce4946daSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
1815ce4946daSBill Paul 	 */
1816ce4946daSBill Paul 	nge_stop(sc);
1817ce4946daSBill Paul 
18181f548804SDoug Ambrisko 	if (sc->nge_tbi) {
18191f548804SDoug Ambrisko 		mii = NULL;
18201f548804SDoug Ambrisko 	} else {
1821ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
18221f548804SDoug Ambrisko 	}
1823ce4946daSBill Paul 
1824ce4946daSBill Paul 	/* Set MAC address */
1825ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0);
1826ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1827ce4946daSBill Paul 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1828ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1);
1829ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1830ce4946daSBill Paul 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1831ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2);
1832ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1833ce4946daSBill Paul 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1834ce4946daSBill Paul 
1835ce4946daSBill Paul 	/* Init circular RX list. */
1836ce4946daSBill Paul 	if (nge_list_rx_init(sc) == ENOBUFS) {
1837ce4946daSBill Paul 		printf("nge%d: initialization failed: no "
1838ce4946daSBill Paul 			"memory for rx buffers\n", sc->nge_unit);
1839ce4946daSBill Paul 		nge_stop(sc);
1840ce4946daSBill Paul 		(void)splx(s);
1841ce4946daSBill Paul 		return;
1842ce4946daSBill Paul 	}
1843ce4946daSBill Paul 
1844ce4946daSBill Paul 	/*
1845ce4946daSBill Paul 	 * Init tx descriptors.
1846ce4946daSBill Paul 	 */
1847ce4946daSBill Paul 	nge_list_tx_init(sc);
1848ce4946daSBill Paul 
1849ce4946daSBill Paul 	/*
1850ce4946daSBill Paul 	 * For the NatSemi chip, we have to explicitly enable the
1851ce4946daSBill Paul 	 * reception of ARP frames, as well as turn on the 'perfect
1852ce4946daSBill Paul 	 * match' filter where we store the station address, otherwise
1853ce4946daSBill Paul 	 * we won't receive unicasts meant for this host.
1854ce4946daSBill Paul 	 */
1855ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP);
1856ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT);
1857ce4946daSBill Paul 
1858ce4946daSBill Paul 	 /* If we want promiscuous mode, set the allframes bit. */
1859ce4946daSBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
1860ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1861ce4946daSBill Paul 	} else {
1862ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1863ce4946daSBill Paul 	}
1864ce4946daSBill Paul 
1865ce4946daSBill Paul 	/*
1866ce4946daSBill Paul 	 * Set the capture broadcast bit to capture broadcast frames.
1867ce4946daSBill Paul 	 */
1868ce4946daSBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1869ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1870ce4946daSBill Paul 	} else {
1871ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1872ce4946daSBill Paul 	}
1873ce4946daSBill Paul 
1874ce4946daSBill Paul 	/*
1875ce4946daSBill Paul 	 * Load the multicast filter.
1876ce4946daSBill Paul 	 */
1877ce4946daSBill Paul 	nge_setmulti(sc);
1878ce4946daSBill Paul 
1879ce4946daSBill Paul 	/* Turn the receive filter on */
1880ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE);
1881ce4946daSBill Paul 
1882ce4946daSBill Paul 	/*
1883ce4946daSBill Paul 	 * Load the address of the RX and TX lists.
1884ce4946daSBill Paul 	 */
1885ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_LISTPTR,
1886ce4946daSBill Paul 	    vtophys(&sc->nge_ldata->nge_rx_list[0]));
1887ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_LISTPTR,
1888ce4946daSBill Paul 	    vtophys(&sc->nge_ldata->nge_tx_list[0]));
1889ce4946daSBill Paul 
1890ce4946daSBill Paul 	/* Set RX configuration */
1891ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG);
1892ce4946daSBill Paul 	/*
1893ce4946daSBill Paul 	 * Enable hardware checksum validation for all IPv4
1894ce4946daSBill Paul 	 * packets, do not reject packets with bad checksums.
1895ce4946daSBill Paul 	 */
1896ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB);
1897ce4946daSBill Paul 
1898ce4946daSBill Paul 	/*
18999d4fe4b2SBrooks Davis 	 * Tell the chip to detect and strip VLAN tag info from
19009d4fe4b2SBrooks Davis 	 * received frames. The tag will be provided in the extsts
19019d4fe4b2SBrooks Davis 	 * field in the RX descriptors.
1902ce4946daSBill Paul 	 */
1903ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL,
1904ce4946daSBill Paul 	    NGE_VIPRXCTL_TAG_DETECT_ENB|NGE_VIPRXCTL_TAG_STRIP_ENB);
1905ce4946daSBill Paul 
1906ce4946daSBill Paul 	/* Set TX configuration */
1907ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG);
1908ce4946daSBill Paul 
1909ce4946daSBill Paul 	/*
1910ce4946daSBill Paul 	 * Enable TX IPv4 checksumming on a per-packet basis.
1911ce4946daSBill Paul 	 */
1912ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT);
1913ce4946daSBill Paul 
1914ce4946daSBill Paul 	/*
19159d4fe4b2SBrooks Davis 	 * Tell the chip to insert VLAN tags on a per-packet basis as
19169d4fe4b2SBrooks Davis 	 * dictated by the code in the frame encapsulation routine.
1917ce4946daSBill Paul 	 */
1918ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT);
1919ce4946daSBill Paul 
1920ce4946daSBill Paul 	/* Set full/half duplex mode. */
19211f548804SDoug Ambrisko 	if (sc->nge_tbi) {
19221f548804SDoug Ambrisko 		if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
19231f548804SDoug Ambrisko 		    == IFM_FDX) {
19241f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
19251f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
19261f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
19271f548804SDoug Ambrisko 		} else {
19281f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
19291f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
19301f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
19311f548804SDoug Ambrisko 		}
19321f548804SDoug Ambrisko 	} else {
1933ce4946daSBill Paul 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1934ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_TX_CFG,
1935ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1936ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1937ce4946daSBill Paul 		} else {
1938ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
1939ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1940ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1941ce4946daSBill Paul 		}
19421f548804SDoug Ambrisko 	}
1943ce4946daSBill Paul 
1944d9fc2b81SPoul-Henning Kamp 	nge_tick(sc);
1945d9fc2b81SPoul-Henning Kamp 
1946ce4946daSBill Paul 	/*
1947ce4946daSBill Paul 	 * Enable the delivery of PHY interrupts based on
194823d3a203SBill Paul 	 * link/speed/duplex status changes. Also enable the
194923d3a203SBill Paul 	 * extsts field in the DMA descriptors (needed for
195023d3a203SBill Paul 	 * TCP/IP checksum offload on transmit).
1951ce4946daSBill Paul 	 */
19522ce0498bSBill Paul 	NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD|
195323d3a203SBill Paul 	    NGE_CFG_PHYINTR_LNK|NGE_CFG_PHYINTR_DUP|NGE_CFG_EXTSTS_ENB);
1954ce4946daSBill Paul 
1955ce4946daSBill Paul 	/*
1956962315f6SBill Paul 	 * Configure interrupt holdoff (moderation). We can
1957962315f6SBill Paul 	 * have the chip delay interrupt delivery for a certain
1958962315f6SBill Paul 	 * period. Units are in 100us, and the max setting
1959962315f6SBill Paul 	 * is 25500us (0xFF x 100us). Default is a 100us holdoff.
1960962315f6SBill Paul 	 */
1961962315f6SBill Paul 	CSR_WRITE_4(sc, NGE_IHR, 0x01);
1962962315f6SBill Paul 
1963962315f6SBill Paul 	/*
1964ce4946daSBill Paul 	 * Enable interrupts.
1965ce4946daSBill Paul 	 */
1966ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS);
1967196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1968196c0df6SHidetoshi Shimokawa 	/*
1969196c0df6SHidetoshi Shimokawa 	 * ... only enable interrupts if we are not polling, make sure
1970196c0df6SHidetoshi Shimokawa 	 * they are off otherwise.
1971196c0df6SHidetoshi Shimokawa 	 */
1972196c0df6SHidetoshi Shimokawa 	if (ifp->if_ipending & IFF_POLLING)
1973196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 0);
1974196c0df6SHidetoshi Shimokawa 	else
1975196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1976ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1977ce4946daSBill Paul 
1978ce4946daSBill Paul 	/* Enable receiver and transmitter. */
1979ce4946daSBill Paul 	NGE_CLRBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
1980ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1981ce4946daSBill Paul 
1982ce4946daSBill Paul 	nge_ifmedia_upd(ifp);
1983ce4946daSBill Paul 
1984ce4946daSBill Paul 	ifp->if_flags |= IFF_RUNNING;
1985ce4946daSBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
1986ce4946daSBill Paul 
1987ce4946daSBill Paul 	(void)splx(s);
1988ce4946daSBill Paul 
1989ce4946daSBill Paul 	return;
1990ce4946daSBill Paul }
1991ce4946daSBill Paul 
1992ce4946daSBill Paul /*
1993ce4946daSBill Paul  * Set media options.
1994ce4946daSBill Paul  */
1995eaabec55SAlfred Perlstein static int
1996eaabec55SAlfred Perlstein nge_ifmedia_upd(ifp)
1997ce4946daSBill Paul 	struct ifnet		*ifp;
1998ce4946daSBill Paul {
1999ce4946daSBill Paul 	struct nge_softc	*sc;
2000ce4946daSBill Paul 	struct mii_data		*mii;
2001ce4946daSBill Paul 
2002ce4946daSBill Paul 	sc = ifp->if_softc;
2003ce4946daSBill Paul 
20041f548804SDoug Ambrisko 	if (sc->nge_tbi) {
20051f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
20061f548804SDoug Ambrisko 		     == IFM_AUTO) {
20071f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR,
20081f548804SDoug Ambrisko 				CSR_READ_4(sc, NGE_TBI_ANAR)
20091f548804SDoug Ambrisko 					| NGE_TBIANAR_HDX | NGE_TBIANAR_FDX
20101f548804SDoug Ambrisko 					| NGE_TBIANAR_PS1 | NGE_TBIANAR_PS2);
20111f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG
20121f548804SDoug Ambrisko 				| NGE_TBIBMCR_RESTART_ANEG);
20131f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG);
20141f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media
20151f548804SDoug Ambrisko 			    & IFM_GMASK) == IFM_FDX) {
20161f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
20171f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
20181f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
20191f548804SDoug Ambrisko 
20201f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR, 0);
20211f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, 0);
20221f548804SDoug Ambrisko 		} else {
20231f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
20241f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
20251f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
20261f548804SDoug Ambrisko 
20271f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR, 0);
20281f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, 0);
20291f548804SDoug Ambrisko 		}
20301f548804SDoug Ambrisko 
20311f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
20321f548804SDoug Ambrisko 			    & ~NGE_GPIO_GP3_OUT);
20331f548804SDoug Ambrisko 	} else {
2034ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
2035ce4946daSBill Paul 		sc->nge_link = 0;
2036ce4946daSBill Paul 		if (mii->mii_instance) {
2037ce4946daSBill Paul 			struct mii_softc	*miisc;
2038ce4946daSBill Paul 			for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
2039ce4946daSBill Paul 			    miisc = LIST_NEXT(miisc, mii_list))
2040ce4946daSBill Paul 				mii_phy_reset(miisc);
2041ce4946daSBill Paul 		}
2042ce4946daSBill Paul 		mii_mediachg(mii);
20431f548804SDoug Ambrisko 	}
2044ce4946daSBill Paul 
2045ce4946daSBill Paul 	return(0);
2046ce4946daSBill Paul }
2047ce4946daSBill Paul 
2048ce4946daSBill Paul /*
2049ce4946daSBill Paul  * Report current media status.
2050ce4946daSBill Paul  */
2051eaabec55SAlfred Perlstein static void
2052eaabec55SAlfred Perlstein nge_ifmedia_sts(ifp, ifmr)
2053ce4946daSBill Paul 	struct ifnet		*ifp;
2054ce4946daSBill Paul 	struct ifmediareq	*ifmr;
2055ce4946daSBill Paul {
2056ce4946daSBill Paul 	struct nge_softc	*sc;
2057ce4946daSBill Paul 	struct mii_data		*mii;
2058ce4946daSBill Paul 
2059ce4946daSBill Paul 	sc = ifp->if_softc;
2060ce4946daSBill Paul 
20611f548804SDoug Ambrisko 	if (sc->nge_tbi) {
20621f548804SDoug Ambrisko 		ifmr->ifm_status = IFM_AVALID;
20631f548804SDoug Ambrisko 		ifmr->ifm_active = IFM_ETHER;
20641f548804SDoug Ambrisko 
20651f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) {
20661f548804SDoug Ambrisko 			ifmr->ifm_status |= IFM_ACTIVE;
20671f548804SDoug Ambrisko 		}
20681f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_TBI_BMCR) & NGE_TBIBMCR_LOOPBACK)
20691f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_LOOP;
20701f548804SDoug Ambrisko 		if (!CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) {
20711f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_NONE;
20721f548804SDoug Ambrisko 			ifmr->ifm_status = 0;
20731f548804SDoug Ambrisko 			return;
20741f548804SDoug Ambrisko 		}
20751f548804SDoug Ambrisko 		ifmr->ifm_active |= IFM_1000_SX;
20761f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
20771f548804SDoug Ambrisko 		    == IFM_AUTO) {
20781f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_AUTO;
20791f548804SDoug Ambrisko 			if (CSR_READ_4(sc, NGE_TBI_ANLPAR)
20801f548804SDoug Ambrisko 			    & NGE_TBIANAR_FDX) {
20811f548804SDoug Ambrisko 				ifmr->ifm_active |= IFM_FDX;
20821f548804SDoug Ambrisko 			}else if (CSR_READ_4(sc, NGE_TBI_ANLPAR)
20831f548804SDoug Ambrisko 				  & NGE_TBIANAR_HDX) {
20841f548804SDoug Ambrisko 				ifmr->ifm_active |= IFM_HDX;
20851f548804SDoug Ambrisko 			}
20861f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
20871f548804SDoug Ambrisko 			== IFM_FDX)
20881f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_FDX;
20891f548804SDoug Ambrisko 		else
20901f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_HDX;
20911f548804SDoug Ambrisko 
20921f548804SDoug Ambrisko 	} else {
2093ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
2094ce4946daSBill Paul 		mii_pollstat(mii);
2095ce4946daSBill Paul 		ifmr->ifm_active = mii->mii_media_active;
2096ce4946daSBill Paul 		ifmr->ifm_status = mii->mii_media_status;
20971f548804SDoug Ambrisko 	}
2098ce4946daSBill Paul 
2099ce4946daSBill Paul 	return;
2100ce4946daSBill Paul }
2101ce4946daSBill Paul 
2102eaabec55SAlfred Perlstein static int
2103eaabec55SAlfred Perlstein nge_ioctl(ifp, command, data)
2104ce4946daSBill Paul 	struct ifnet		*ifp;
2105ce4946daSBill Paul 	u_long			command;
2106ce4946daSBill Paul 	caddr_t			data;
2107ce4946daSBill Paul {
2108ce4946daSBill Paul 	struct nge_softc	*sc = ifp->if_softc;
2109ce4946daSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
2110ce4946daSBill Paul 	struct mii_data		*mii;
2111ce4946daSBill Paul 	int			s, error = 0;
2112ce4946daSBill Paul 
2113ce4946daSBill Paul 	s = splimp();
2114ce4946daSBill Paul 
2115ce4946daSBill Paul 	switch(command) {
2116ce4946daSBill Paul 	case SIOCSIFMTU:
2117ce4946daSBill Paul 		if (ifr->ifr_mtu > NGE_JUMBO_MTU)
2118ce4946daSBill Paul 			error = EINVAL;
2119cb2f755cSBill Paul 		else {
2120ce4946daSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2121cb2f755cSBill Paul 			/*
2122cb2f755cSBill Paul 			 * Workaround: if the MTU is larger than
2123cb2f755cSBill Paul 			 * 8152 (TX FIFO size minus 64 minus 18), turn off
2124cb2f755cSBill Paul 			 * TX checksum offloading.
2125cb2f755cSBill Paul 			 */
212680782fbfSBill Paul 			if (ifr->ifr_mtu >= 8152)
2127cb2f755cSBill Paul 				ifp->if_hwassist = 0;
2128cb2f755cSBill Paul 			else
2129cb2f755cSBill Paul 				ifp->if_hwassist = NGE_CSUM_FEATURES;
2130cb2f755cSBill Paul 		}
2131ce4946daSBill Paul 		break;
2132ce4946daSBill Paul 	case SIOCSIFFLAGS:
2133ce4946daSBill Paul 		if (ifp->if_flags & IFF_UP) {
2134ce4946daSBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
2135ce4946daSBill Paul 			    ifp->if_flags & IFF_PROMISC &&
2136ce4946daSBill Paul 			    !(sc->nge_if_flags & IFF_PROMISC)) {
2137ce4946daSBill Paul 				NGE_SETBIT(sc, NGE_RXFILT_CTL,
2138ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLPHYS|
2139ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLMULTI);
2140ce4946daSBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
2141ce4946daSBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
2142ce4946daSBill Paul 			    sc->nge_if_flags & IFF_PROMISC) {
2143ce4946daSBill Paul 				NGE_CLRBIT(sc, NGE_RXFILT_CTL,
2144ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLPHYS);
2145ce4946daSBill Paul 				if (!(ifp->if_flags & IFF_ALLMULTI))
2146ce4946daSBill Paul 					NGE_CLRBIT(sc, NGE_RXFILT_CTL,
2147ce4946daSBill Paul 					    NGE_RXFILTCTL_ALLMULTI);
2148ce4946daSBill Paul 			} else {
2149ce4946daSBill Paul 				ifp->if_flags &= ~IFF_RUNNING;
2150ce4946daSBill Paul 				nge_init(sc);
2151ce4946daSBill Paul 			}
2152ce4946daSBill Paul 		} else {
2153ce4946daSBill Paul 			if (ifp->if_flags & IFF_RUNNING)
2154ce4946daSBill Paul 				nge_stop(sc);
2155ce4946daSBill Paul 		}
2156ce4946daSBill Paul 		sc->nge_if_flags = ifp->if_flags;
2157ce4946daSBill Paul 		error = 0;
2158ce4946daSBill Paul 		break;
2159ce4946daSBill Paul 	case SIOCADDMULTI:
2160ce4946daSBill Paul 	case SIOCDELMULTI:
2161ce4946daSBill Paul 		nge_setmulti(sc);
2162ce4946daSBill Paul 		error = 0;
2163ce4946daSBill Paul 		break;
2164ce4946daSBill Paul 	case SIOCGIFMEDIA:
2165ce4946daSBill Paul 	case SIOCSIFMEDIA:
21661f548804SDoug Ambrisko 		if (sc->nge_tbi) {
21671f548804SDoug Ambrisko 			error = ifmedia_ioctl(ifp, ifr, &sc->nge_ifmedia,
21681f548804SDoug Ambrisko 					      command);
21691f548804SDoug Ambrisko 		} else {
2170ce4946daSBill Paul 			mii = device_get_softc(sc->nge_miibus);
21711f548804SDoug Ambrisko 			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
21721f548804SDoug Ambrisko 					      command);
21731f548804SDoug Ambrisko 		}
2174ce4946daSBill Paul 		break;
2175ce4946daSBill Paul 	default:
2176673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2177ce4946daSBill Paul 		break;
2178ce4946daSBill Paul 	}
2179ce4946daSBill Paul 
2180ce4946daSBill Paul 	(void)splx(s);
2181ce4946daSBill Paul 
2182ce4946daSBill Paul 	return(error);
2183ce4946daSBill Paul }
2184ce4946daSBill Paul 
2185eaabec55SAlfred Perlstein static void
2186eaabec55SAlfred Perlstein nge_watchdog(ifp)
2187ce4946daSBill Paul 	struct ifnet		*ifp;
2188ce4946daSBill Paul {
2189ce4946daSBill Paul 	struct nge_softc	*sc;
2190ce4946daSBill Paul 
2191ce4946daSBill Paul 	sc = ifp->if_softc;
2192ce4946daSBill Paul 
2193ce4946daSBill Paul 	ifp->if_oerrors++;
2194ce4946daSBill Paul 	printf("nge%d: watchdog timeout\n", sc->nge_unit);
2195ce4946daSBill Paul 
2196ce4946daSBill Paul 	nge_stop(sc);
2197ce4946daSBill Paul 	nge_reset(sc);
2198ce4946daSBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
2199ce4946daSBill Paul 	nge_init(sc);
2200ce4946daSBill Paul 
2201ce4946daSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
2202ce4946daSBill Paul 		nge_start(ifp);
2203ce4946daSBill Paul 
2204ce4946daSBill Paul 	return;
2205ce4946daSBill Paul }
2206ce4946daSBill Paul 
2207ce4946daSBill Paul /*
2208ce4946daSBill Paul  * Stop the adapter and free any mbufs allocated to the
2209ce4946daSBill Paul  * RX and TX lists.
2210ce4946daSBill Paul  */
2211eaabec55SAlfred Perlstein static void
2212eaabec55SAlfred Perlstein nge_stop(sc)
2213ce4946daSBill Paul 	struct nge_softc	*sc;
2214ce4946daSBill Paul {
2215ce4946daSBill Paul 	register int		i;
2216ce4946daSBill Paul 	struct ifnet		*ifp;
2217ce4946daSBill Paul 	struct mii_data		*mii;
2218ce4946daSBill Paul 
2219ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
2220ce4946daSBill Paul 	ifp->if_timer = 0;
22211f548804SDoug Ambrisko 	if (sc->nge_tbi) {
22221f548804SDoug Ambrisko 		mii = NULL;
22231f548804SDoug Ambrisko 	} else {
2224ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
22251f548804SDoug Ambrisko 	}
2226ce4946daSBill Paul 
2227ce4946daSBill Paul 	untimeout(nge_tick, sc, sc->nge_stat_ch);
2228196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
2229196c0df6SHidetoshi Shimokawa 	ether_poll_deregister(ifp);
2230196c0df6SHidetoshi Shimokawa #endif
2231ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
2232ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, 0);
2233ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
2234ce4946daSBill Paul 	DELAY(1000);
2235ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_LISTPTR, 0);
2236ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_LISTPTR, 0);
2237ce4946daSBill Paul 
22381f548804SDoug Ambrisko 	if (!sc->nge_tbi)
2239279fe8d1SPoul-Henning Kamp 		mii_down(mii);
2240ce4946daSBill Paul 
2241ce4946daSBill Paul 	sc->nge_link = 0;
2242ce4946daSBill Paul 
2243ce4946daSBill Paul 	/*
2244ce4946daSBill Paul 	 * Free data in the RX lists.
2245ce4946daSBill Paul 	 */
2246ce4946daSBill Paul 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
2247ce4946daSBill Paul 		if (sc->nge_ldata->nge_rx_list[i].nge_mbuf != NULL) {
2248ce4946daSBill Paul 			m_freem(sc->nge_ldata->nge_rx_list[i].nge_mbuf);
2249ce4946daSBill Paul 			sc->nge_ldata->nge_rx_list[i].nge_mbuf = NULL;
2250ce4946daSBill Paul 		}
2251ce4946daSBill Paul 	}
2252ce4946daSBill Paul 	bzero((char *)&sc->nge_ldata->nge_rx_list,
2253ce4946daSBill Paul 		sizeof(sc->nge_ldata->nge_rx_list));
2254ce4946daSBill Paul 
2255ce4946daSBill Paul 	/*
2256ce4946daSBill Paul 	 * Free the TX list buffers.
2257ce4946daSBill Paul 	 */
2258ce4946daSBill Paul 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
2259ce4946daSBill Paul 		if (sc->nge_ldata->nge_tx_list[i].nge_mbuf != NULL) {
2260ce4946daSBill Paul 			m_freem(sc->nge_ldata->nge_tx_list[i].nge_mbuf);
2261ce4946daSBill Paul 			sc->nge_ldata->nge_tx_list[i].nge_mbuf = NULL;
2262ce4946daSBill Paul 		}
2263ce4946daSBill Paul 	}
2264ce4946daSBill Paul 
2265ce4946daSBill Paul 	bzero((char *)&sc->nge_ldata->nge_tx_list,
2266ce4946daSBill Paul 		sizeof(sc->nge_ldata->nge_tx_list));
2267ce4946daSBill Paul 
2268ce4946daSBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2269ce4946daSBill Paul 
2270ce4946daSBill Paul 	return;
2271ce4946daSBill Paul }
2272ce4946daSBill Paul 
2273ce4946daSBill Paul /*
2274ce4946daSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2275ce4946daSBill Paul  * get confused by errant DMAs when rebooting.
2276ce4946daSBill Paul  */
2277eaabec55SAlfred Perlstein static void
2278eaabec55SAlfred Perlstein nge_shutdown(dev)
2279ce4946daSBill Paul 	device_t		dev;
2280ce4946daSBill Paul {
2281ce4946daSBill Paul 	struct nge_softc	*sc;
2282ce4946daSBill Paul 
2283ce4946daSBill Paul 	sc = device_get_softc(dev);
2284ce4946daSBill Paul 
2285ce4946daSBill Paul 	nge_reset(sc);
2286ce4946daSBill Paul 	nge_stop(sc);
2287ce4946daSBill Paul 
2288ce4946daSBill Paul 	return;
2289ce4946daSBill Paul }
2290