xref: /freebsd/sys/dev/nge/if_nge.c (revision cb2f755c7f8f45672812ecfc06fdc71084abb185)
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  * $FreeBSD$
34ce4946daSBill Paul  */
35ce4946daSBill Paul 
36ce4946daSBill Paul /*
37ce4946daSBill Paul  * National Semiconductor DP83820/DP83821 gigabit ethernet driver
38ce4946daSBill Paul  * for FreeBSD. Datasheets are available from:
39ce4946daSBill Paul  *
40ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83820.pdf
41ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83821.pdf
42ce4946daSBill Paul  *
43ce4946daSBill Paul  * These chips are used on several low cost gigabit ethernet NICs
44ce4946daSBill Paul  * sold by D-Link, Addtron, SMC and Asante. Both parts are
45ce4946daSBill Paul  * virtually the same, except the 83820 is a 64-bit/32-bit part,
46ce4946daSBill Paul  * while the 83821 is 32-bit only.
47ce4946daSBill Paul  *
48ce4946daSBill Paul  * Many cards also use National gigE transceivers, such as the
49ce4946daSBill Paul  * DP83891, DP83861 and DP83862 gigPHYTER parts. The DP83861 datasheet
50ce4946daSBill Paul  * contains a full register description that applies to all of these
51ce4946daSBill Paul  * components:
52ce4946daSBill Paul  *
53ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83861.pdf
54ce4946daSBill Paul  *
55ce4946daSBill Paul  * Written by Bill Paul <wpaul@bsdi.com>
56ce4946daSBill Paul  * BSDi Open Source Solutions
57ce4946daSBill Paul  */
58ce4946daSBill Paul 
59ce4946daSBill Paul /*
60ce4946daSBill Paul  * The NatSemi DP83820 and 83821 controllers are enhanced versions
61ce4946daSBill Paul  * of the NatSemi MacPHYTER 10/100 devices. They support 10, 100
62ce4946daSBill Paul  * and 1000Mbps speeds with 1000baseX (ten bit interface), MII and GMII
63ce4946daSBill Paul  * ports. Other features include 8K TX FIFO and 32K RX FIFO, TCP/IP
64ce4946daSBill Paul  * hardware checksum offload (IPv4 only), VLAN tagging and filtering,
65ce4946daSBill Paul  * priority TX and RX queues, a 2048 bit multicast hash filter, 4 RX pattern
66ce4946daSBill Paul  * matching buffers, one perfect address filter buffer and interrupt
67ce4946daSBill Paul  * moderation. The 83820 supports both 64-bit and 32-bit addressing
68ce4946daSBill Paul  * and data transfers: the 64-bit support can be toggled on or off
69ce4946daSBill Paul  * via software. This affects the size of certain fields in the DMA
70ce4946daSBill Paul  * descriptors.
71ce4946daSBill Paul  *
72cb2f755cSBill Paul  * There are two bugs/misfeatures in the 83820/83821 that I have
73cb2f755cSBill Paul  * discovered so far:
74cb2f755cSBill Paul  *
75cb2f755cSBill Paul  * - Receive buffers must be aligned on 64-bit boundaries, which means
76cb2f755cSBill Paul  *   you must resort to copying data in order to fix up the payload
77cb2f755cSBill Paul  *   alignment.
78cb2f755cSBill Paul  *
79cb2f755cSBill Paul  * - In order to transmit jumbo frames larger than 8170 bytes, you have
80cb2f755cSBill Paul  *   to turn off transmit checksum offloading, because the chip can't
81cb2f755cSBill Paul  *   compute the checksum on an outgoing frame unless it fits entirely
82cb2f755cSBill Paul  *   within the TX FIFO, which is only 8192 bytes in size. If you have
83cb2f755cSBill Paul  *   TX checksum offload enabled and you transmit attempt to transmit a
84cb2f755cSBill Paul  *   frame larger than 8170 bytes, the transmitter will wedge.
85cb2f755cSBill Paul  *
86cb2f755cSBill Paul  * To work around the latter problem, TX checksum offload is disabled
87cb2f755cSBill Paul  * if the user selects an MTU larger than 8152 (8170 - 18).
88ce4946daSBill Paul  */
89ce4946daSBill Paul 
90e39cd3b2SBill Paul #include "vlan.h"
91ce4946daSBill Paul 
92ce4946daSBill Paul #include <sys/param.h>
93ce4946daSBill Paul #include <sys/systm.h>
94ce4946daSBill Paul #include <sys/sockio.h>
95ce4946daSBill Paul #include <sys/mbuf.h>
96ce4946daSBill Paul #include <sys/malloc.h>
97ce4946daSBill Paul #include <sys/kernel.h>
98ce4946daSBill Paul #include <sys/socket.h>
99ce4946daSBill Paul 
100ce4946daSBill Paul #include <net/if.h>
101ce4946daSBill Paul #include <net/if_arp.h>
102ce4946daSBill Paul #include <net/ethernet.h>
103ce4946daSBill Paul #include <net/if_dl.h>
104ce4946daSBill Paul #include <net/if_media.h>
105ce4946daSBill Paul 
106ce4946daSBill Paul #if NVLAN > 0
107ce4946daSBill Paul #include <net/if_types.h>
108ce4946daSBill Paul #include <net/if_vlan_var.h>
109ce4946daSBill Paul #endif
110ce4946daSBill Paul 
111ce4946daSBill Paul #include <net/bpf.h>
112ce4946daSBill Paul 
113ce4946daSBill Paul #include <vm/vm.h>              /* for vtophys */
114ce4946daSBill Paul #include <vm/pmap.h>            /* for vtophys */
115ce4946daSBill Paul #include <machine/clock.h>      /* for DELAY */
116ce4946daSBill Paul #include <machine/bus_pio.h>
117ce4946daSBill Paul #include <machine/bus_memio.h>
118ce4946daSBill Paul #include <machine/bus.h>
119ce4946daSBill Paul #include <machine/resource.h>
120ce4946daSBill Paul #include <sys/bus.h>
121ce4946daSBill Paul #include <sys/rman.h>
122ce4946daSBill Paul 
123ce4946daSBill Paul #include <dev/mii/mii.h>
124ce4946daSBill Paul #include <dev/mii/miivar.h>
125ce4946daSBill Paul 
126ce4946daSBill Paul #include <pci/pcireg.h>
127ce4946daSBill Paul #include <pci/pcivar.h>
128ce4946daSBill Paul 
129ce4946daSBill Paul #define NGE_USEIOSPACE
130ce4946daSBill Paul 
1315da751e4SBill Paul #include <dev/nge/if_ngereg.h>
132ce4946daSBill Paul 
133ce4946daSBill Paul MODULE_DEPEND(nge, miibus, 1, 1, 1);
134ce4946daSBill Paul 
135ce4946daSBill Paul /* "controller miibus0" required.  See GENERIC if you get errors here. */
136ce4946daSBill Paul #include "miibus_if.h"
137ce4946daSBill Paul 
138ce4946daSBill Paul #ifndef lint
139ce4946daSBill Paul static const char rcsid[] =
140ce4946daSBill Paul   "$FreeBSD$";
141ce4946daSBill Paul #endif
142ce4946daSBill Paul 
143ce4946daSBill Paul #define NGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
144ce4946daSBill Paul 
145ce4946daSBill Paul /*
146ce4946daSBill Paul  * Various supported device vendors/types and their names.
147ce4946daSBill Paul  */
148ce4946daSBill Paul static struct nge_type nge_devs[] = {
149ce4946daSBill Paul 	{ NGE_VENDORID, NGE_DEVICEID,
150ce4946daSBill Paul 	    "National Semiconductor Gigabit Ethernet" },
151ce4946daSBill Paul 	{ 0, 0, NULL }
152ce4946daSBill Paul };
153ce4946daSBill Paul 
154ce4946daSBill Paul static int nge_probe		__P((device_t));
155ce4946daSBill Paul static int nge_attach		__P((device_t));
156ce4946daSBill Paul static int nge_detach		__P((device_t));
157ce4946daSBill Paul 
158ce4946daSBill Paul static int nge_alloc_jumbo_mem	__P((struct nge_softc *));
159ce4946daSBill Paul static void nge_free_jumbo_mem	__P((struct nge_softc *));
160ce4946daSBill Paul static void *nge_jalloc		__P((struct nge_softc *));
161ce4946daSBill Paul static void nge_jfree		__P((caddr_t, void *));
162ce4946daSBill Paul 
163ce4946daSBill Paul static int nge_newbuf		__P((struct nge_softc *,
164ce4946daSBill Paul 					struct nge_desc *,
165ce4946daSBill Paul 					struct mbuf *));
166ce4946daSBill Paul static int nge_encap		__P((struct nge_softc *,
167ce4946daSBill Paul 					struct mbuf *, u_int32_t *));
168ce4946daSBill Paul static void nge_rxeof		__P((struct nge_softc *));
169ce4946daSBill Paul static void nge_rxeoc		__P((struct nge_softc *));
170ce4946daSBill Paul static void nge_txeof		__P((struct nge_softc *));
171ce4946daSBill Paul static void nge_intr		__P((void *));
172ce4946daSBill Paul static void nge_tick		__P((void *));
173ce4946daSBill Paul static void nge_start		__P((struct ifnet *));
174ce4946daSBill Paul static int nge_ioctl		__P((struct ifnet *, u_long, caddr_t));
175ce4946daSBill Paul static void nge_init		__P((void *));
176ce4946daSBill Paul static void nge_stop		__P((struct nge_softc *));
177ce4946daSBill Paul static void nge_watchdog		__P((struct ifnet *));
178ce4946daSBill Paul static void nge_shutdown		__P((device_t));
179ce4946daSBill Paul static int nge_ifmedia_upd	__P((struct ifnet *));
180ce4946daSBill Paul static void nge_ifmedia_sts	__P((struct ifnet *, struct ifmediareq *));
181ce4946daSBill Paul 
182ce4946daSBill Paul static void nge_delay		__P((struct nge_softc *));
183ce4946daSBill Paul static void nge_eeprom_idle	__P((struct nge_softc *));
184ce4946daSBill Paul static void nge_eeprom_putbyte	__P((struct nge_softc *, int));
185ce4946daSBill Paul static void nge_eeprom_getword	__P((struct nge_softc *, int, u_int16_t *));
186ce4946daSBill Paul static void nge_read_eeprom	__P((struct nge_softc *, caddr_t, int,
187ce4946daSBill Paul 							int, int));
188ce4946daSBill Paul 
189ce4946daSBill Paul static void nge_mii_sync	__P((struct nge_softc *));
190ce4946daSBill Paul static void nge_mii_send	__P((struct nge_softc *, u_int32_t, int));
191ce4946daSBill Paul static int nge_mii_readreg	__P((struct nge_softc *,
192ce4946daSBill Paul 					struct nge_mii_frame *));
193ce4946daSBill Paul static int nge_mii_writereg	__P((struct nge_softc *,
194ce4946daSBill Paul 					struct nge_mii_frame *));
195ce4946daSBill Paul 
196ce4946daSBill Paul static int nge_miibus_readreg	__P((device_t, int, int));
197ce4946daSBill Paul static int nge_miibus_writereg	__P((device_t, int, int, int));
198ce4946daSBill Paul static void nge_miibus_statchg	__P((device_t));
199ce4946daSBill Paul 
200ce4946daSBill Paul static void nge_setmulti	__P((struct nge_softc *));
201ce4946daSBill Paul static u_int32_t nge_crc	__P((struct nge_softc *, caddr_t));
202ce4946daSBill Paul static void nge_reset		__P((struct nge_softc *));
203ce4946daSBill Paul static int nge_list_rx_init	__P((struct nge_softc *));
204ce4946daSBill Paul static int nge_list_tx_init	__P((struct nge_softc *));
205ce4946daSBill Paul 
206ce4946daSBill Paul #ifdef NGE_USEIOSPACE
207ce4946daSBill Paul #define NGE_RES			SYS_RES_IOPORT
208ce4946daSBill Paul #define NGE_RID			NGE_PCI_LOIO
209ce4946daSBill Paul #else
210ce4946daSBill Paul #define NGE_RES			SYS_RES_MEMORY
211ce4946daSBill Paul #define NGE_RID			NGE_PCI_LOMEM
212ce4946daSBill Paul #endif
213ce4946daSBill Paul 
214ce4946daSBill Paul static device_method_t nge_methods[] = {
215ce4946daSBill Paul 	/* Device interface */
216ce4946daSBill Paul 	DEVMETHOD(device_probe,		nge_probe),
217ce4946daSBill Paul 	DEVMETHOD(device_attach,	nge_attach),
218ce4946daSBill Paul 	DEVMETHOD(device_detach,	nge_detach),
219ce4946daSBill Paul 	DEVMETHOD(device_shutdown,	nge_shutdown),
220ce4946daSBill Paul 
221ce4946daSBill Paul 	/* bus interface */
222ce4946daSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
223ce4946daSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
224ce4946daSBill Paul 
225ce4946daSBill Paul 	/* MII interface */
226ce4946daSBill Paul 	DEVMETHOD(miibus_readreg,	nge_miibus_readreg),
227ce4946daSBill Paul 	DEVMETHOD(miibus_writereg,	nge_miibus_writereg),
228ce4946daSBill Paul 	DEVMETHOD(miibus_statchg,	nge_miibus_statchg),
229ce4946daSBill Paul 
230ce4946daSBill Paul 	{ 0, 0 }
231ce4946daSBill Paul };
232ce4946daSBill Paul 
233ce4946daSBill Paul static driver_t nge_driver = {
234ce4946daSBill Paul 	"nge",
235ce4946daSBill Paul 	nge_methods,
236ce4946daSBill Paul 	sizeof(struct nge_softc)
237ce4946daSBill Paul };
238ce4946daSBill Paul 
239ce4946daSBill Paul static devclass_t nge_devclass;
240ce4946daSBill Paul 
241ce4946daSBill Paul DRIVER_MODULE(if_nge, pci, nge_driver, nge_devclass, 0, 0);
242ce4946daSBill Paul DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0);
243ce4946daSBill Paul 
244ce4946daSBill Paul #define NGE_SETBIT(sc, reg, x)				\
245ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
246ce4946daSBill Paul 		CSR_READ_4(sc, reg) | (x))
247ce4946daSBill Paul 
248ce4946daSBill Paul #define NGE_CLRBIT(sc, reg, x)				\
249ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
250ce4946daSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
251ce4946daSBill Paul 
252ce4946daSBill Paul #define SIO_SET(x)					\
253ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | x)
254ce4946daSBill Paul 
255ce4946daSBill Paul #define SIO_CLR(x)					\
256ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~x)
257ce4946daSBill Paul 
258ce4946daSBill Paul static void nge_delay(sc)
259ce4946daSBill Paul 	struct nge_softc	*sc;
260ce4946daSBill Paul {
261ce4946daSBill Paul 	int			idx;
262ce4946daSBill Paul 
263ce4946daSBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
264ce4946daSBill Paul 		CSR_READ_4(sc, NGE_CSR);
265ce4946daSBill Paul 
266ce4946daSBill Paul 	return;
267ce4946daSBill Paul }
268ce4946daSBill Paul 
269ce4946daSBill Paul static void nge_eeprom_idle(sc)
270ce4946daSBill Paul 	struct nge_softc	*sc;
271ce4946daSBill Paul {
272ce4946daSBill Paul 	register int		i;
273ce4946daSBill Paul 
274ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
275ce4946daSBill Paul 	nge_delay(sc);
276ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CLK);
277ce4946daSBill Paul 	nge_delay(sc);
278ce4946daSBill Paul 
279ce4946daSBill Paul 	for (i = 0; i < 25; i++) {
280ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
281ce4946daSBill Paul 		nge_delay(sc);
282ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
283ce4946daSBill Paul 		nge_delay(sc);
284ce4946daSBill Paul 	}
285ce4946daSBill Paul 
286ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
287ce4946daSBill Paul 	nge_delay(sc);
288ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CSEL);
289ce4946daSBill Paul 	nge_delay(sc);
290ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0x00000000);
291ce4946daSBill Paul 
292ce4946daSBill Paul 	return;
293ce4946daSBill Paul }
294ce4946daSBill Paul 
295ce4946daSBill Paul /*
296ce4946daSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
297ce4946daSBill Paul  */
298ce4946daSBill Paul static void nge_eeprom_putbyte(sc, addr)
299ce4946daSBill Paul 	struct nge_softc	*sc;
300ce4946daSBill Paul 	int			addr;
301ce4946daSBill Paul {
302ce4946daSBill Paul 	register int		d, i;
303ce4946daSBill Paul 
304ce4946daSBill Paul 	d = addr | NGE_EECMD_READ;
305ce4946daSBill Paul 
306ce4946daSBill Paul 	/*
307ce4946daSBill Paul 	 * Feed in each bit and stobe the clock.
308ce4946daSBill Paul 	 */
309ce4946daSBill Paul 	for (i = 0x400; i; i >>= 1) {
310ce4946daSBill Paul 		if (d & i) {
311ce4946daSBill Paul 			SIO_SET(NGE_MEAR_EE_DIN);
312ce4946daSBill Paul 		} else {
313ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_EE_DIN);
314ce4946daSBill Paul 		}
315ce4946daSBill Paul 		nge_delay(sc);
316ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
317ce4946daSBill Paul 		nge_delay(sc);
318ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
319ce4946daSBill Paul 		nge_delay(sc);
320ce4946daSBill Paul 	}
321ce4946daSBill Paul 
322ce4946daSBill Paul 	return;
323ce4946daSBill Paul }
324ce4946daSBill Paul 
325ce4946daSBill Paul /*
326ce4946daSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
327ce4946daSBill Paul  */
328ce4946daSBill Paul static void nge_eeprom_getword(sc, addr, dest)
329ce4946daSBill Paul 	struct nge_softc	*sc;
330ce4946daSBill Paul 	int			addr;
331ce4946daSBill Paul 	u_int16_t		*dest;
332ce4946daSBill Paul {
333ce4946daSBill Paul 	register int		i;
334ce4946daSBill Paul 	u_int16_t		word = 0;
335ce4946daSBill Paul 
336ce4946daSBill Paul 	/* Force EEPROM to idle state. */
337ce4946daSBill Paul 	nge_eeprom_idle(sc);
338ce4946daSBill Paul 
339ce4946daSBill Paul 	/* Enter EEPROM access mode. */
340ce4946daSBill Paul 	nge_delay(sc);
341ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
342ce4946daSBill Paul 	nge_delay(sc);
343ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
344ce4946daSBill Paul 	nge_delay(sc);
345ce4946daSBill Paul 
346ce4946daSBill Paul 	/*
347ce4946daSBill Paul 	 * Send address of word we want to read.
348ce4946daSBill Paul 	 */
349ce4946daSBill Paul 	nge_eeprom_putbyte(sc, addr);
350ce4946daSBill Paul 
351ce4946daSBill Paul 	/*
352ce4946daSBill Paul 	 * Start reading bits from EEPROM.
353ce4946daSBill Paul 	 */
354ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
355ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
356ce4946daSBill Paul 		nge_delay(sc);
357ce4946daSBill Paul 		if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT)
358ce4946daSBill Paul 			word |= i;
359ce4946daSBill Paul 		nge_delay(sc);
360ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
361ce4946daSBill Paul 		nge_delay(sc);
362ce4946daSBill Paul 	}
363ce4946daSBill Paul 
364ce4946daSBill Paul 	/* Turn off EEPROM access mode. */
365ce4946daSBill Paul 	nge_eeprom_idle(sc);
366ce4946daSBill Paul 
367ce4946daSBill Paul 	*dest = word;
368ce4946daSBill Paul 
369ce4946daSBill Paul 	return;
370ce4946daSBill Paul }
371ce4946daSBill Paul 
372ce4946daSBill Paul /*
373ce4946daSBill Paul  * Read a sequence of words from the EEPROM.
374ce4946daSBill Paul  */
375ce4946daSBill Paul static void nge_read_eeprom(sc, dest, off, cnt, swap)
376ce4946daSBill Paul 	struct nge_softc	*sc;
377ce4946daSBill Paul 	caddr_t			dest;
378ce4946daSBill Paul 	int			off;
379ce4946daSBill Paul 	int			cnt;
380ce4946daSBill Paul 	int			swap;
381ce4946daSBill Paul {
382ce4946daSBill Paul 	int			i;
383ce4946daSBill Paul 	u_int16_t		word = 0, *ptr;
384ce4946daSBill Paul 
385ce4946daSBill Paul 	for (i = 0; i < cnt; i++) {
386ce4946daSBill Paul 		nge_eeprom_getword(sc, off + i, &word);
387ce4946daSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
388ce4946daSBill Paul 		if (swap)
389ce4946daSBill Paul 			*ptr = ntohs(word);
390ce4946daSBill Paul 		else
391ce4946daSBill Paul 			*ptr = word;
392ce4946daSBill Paul 	}
393ce4946daSBill Paul 
394ce4946daSBill Paul 	return;
395ce4946daSBill Paul }
396ce4946daSBill Paul 
397ce4946daSBill Paul /*
398ce4946daSBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
399ce4946daSBill Paul  */
400ce4946daSBill Paul static void nge_mii_sync(sc)
401ce4946daSBill Paul 	struct nge_softc		*sc;
402ce4946daSBill Paul {
403ce4946daSBill Paul 	register int		i;
404ce4946daSBill Paul 
405ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR|NGE_MEAR_MII_DATA);
406ce4946daSBill Paul 
407ce4946daSBill Paul 	for (i = 0; i < 32; i++) {
408ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
409ce4946daSBill Paul 		DELAY(1);
410ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
411ce4946daSBill Paul 		DELAY(1);
412ce4946daSBill Paul 	}
413ce4946daSBill Paul 
414ce4946daSBill Paul 	return;
415ce4946daSBill Paul }
416ce4946daSBill Paul 
417ce4946daSBill Paul /*
418ce4946daSBill Paul  * Clock a series of bits through the MII.
419ce4946daSBill Paul  */
420ce4946daSBill Paul static void nge_mii_send(sc, bits, cnt)
421ce4946daSBill Paul 	struct nge_softc		*sc;
422ce4946daSBill Paul 	u_int32_t		bits;
423ce4946daSBill Paul 	int			cnt;
424ce4946daSBill Paul {
425ce4946daSBill Paul 	int			i;
426ce4946daSBill Paul 
427ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
428ce4946daSBill Paul 
429ce4946daSBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
430ce4946daSBill Paul                 if (bits & i) {
431ce4946daSBill Paul 			SIO_SET(NGE_MEAR_MII_DATA);
432ce4946daSBill Paul                 } else {
433ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_MII_DATA);
434ce4946daSBill Paul                 }
435ce4946daSBill Paul 		DELAY(1);
436ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
437ce4946daSBill Paul 		DELAY(1);
438ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
439ce4946daSBill Paul 	}
440ce4946daSBill Paul }
441ce4946daSBill Paul 
442ce4946daSBill Paul /*
443ce4946daSBill Paul  * Read an PHY register through the MII.
444ce4946daSBill Paul  */
445ce4946daSBill Paul static int nge_mii_readreg(sc, frame)
446ce4946daSBill Paul 	struct nge_softc		*sc;
447ce4946daSBill Paul 	struct nge_mii_frame	*frame;
448ce4946daSBill Paul 
449ce4946daSBill Paul {
450ce4946daSBill Paul 	int			i, ack, s;
451ce4946daSBill Paul 
452ce4946daSBill Paul 	s = splimp();
453ce4946daSBill Paul 
454ce4946daSBill Paul 	/*
455ce4946daSBill Paul 	 * Set up frame for RX.
456ce4946daSBill Paul 	 */
457ce4946daSBill Paul 	frame->mii_stdelim = NGE_MII_STARTDELIM;
458ce4946daSBill Paul 	frame->mii_opcode = NGE_MII_READOP;
459ce4946daSBill Paul 	frame->mii_turnaround = 0;
460ce4946daSBill Paul 	frame->mii_data = 0;
461ce4946daSBill Paul 
462ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0);
463ce4946daSBill Paul 
464ce4946daSBill Paul 	/*
465ce4946daSBill Paul  	 * Turn on data xmit.
466ce4946daSBill Paul 	 */
467ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR);
468ce4946daSBill Paul 
469ce4946daSBill Paul 	nge_mii_sync(sc);
470ce4946daSBill Paul 
471ce4946daSBill Paul 	/*
472ce4946daSBill Paul 	 * Send command/address info.
473ce4946daSBill Paul 	 */
474ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_stdelim, 2);
475ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_opcode, 2);
476ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_phyaddr, 5);
477ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_regaddr, 5);
478ce4946daSBill Paul 
479ce4946daSBill Paul 	/* Idle bit */
480ce4946daSBill Paul 	SIO_CLR((NGE_MEAR_MII_CLK|NGE_MEAR_MII_DATA));
481ce4946daSBill Paul 	DELAY(1);
482ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
483ce4946daSBill Paul 	DELAY(1);
484ce4946daSBill Paul 
485ce4946daSBill Paul 	/* Turn off xmit. */
486ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_DIR);
487ce4946daSBill Paul 	/* Check for ack */
488ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
489ce4946daSBill Paul 	DELAY(1);
490ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
491ce4946daSBill Paul 	DELAY(1);
492ce4946daSBill Paul 	ack = CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA;
493ce4946daSBill Paul 
494ce4946daSBill Paul 	/*
495ce4946daSBill Paul 	 * Now try reading data bits. If the ack failed, we still
496ce4946daSBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
497ce4946daSBill Paul 	 */
498ce4946daSBill Paul 	if (ack) {
499ce4946daSBill Paul 		for(i = 0; i < 16; i++) {
500ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_MII_CLK);
501ce4946daSBill Paul 			DELAY(1);
502ce4946daSBill Paul 			SIO_SET(NGE_MEAR_MII_CLK);
503ce4946daSBill Paul 			DELAY(1);
504ce4946daSBill Paul 		}
505ce4946daSBill Paul 		goto fail;
506ce4946daSBill Paul 	}
507ce4946daSBill Paul 
508ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
509ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
510ce4946daSBill Paul 		DELAY(1);
511ce4946daSBill Paul 		if (!ack) {
512ce4946daSBill Paul 			if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA)
513ce4946daSBill Paul 				frame->mii_data |= i;
514ce4946daSBill Paul 			DELAY(1);
515ce4946daSBill Paul 		}
516ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
517ce4946daSBill Paul 		DELAY(1);
518ce4946daSBill Paul 	}
519ce4946daSBill Paul 
520ce4946daSBill Paul fail:
521ce4946daSBill Paul 
522ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
523ce4946daSBill Paul 	DELAY(1);
524ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
525ce4946daSBill Paul 	DELAY(1);
526ce4946daSBill Paul 
527ce4946daSBill Paul 	splx(s);
528ce4946daSBill Paul 
529ce4946daSBill Paul 	if (ack)
530ce4946daSBill Paul 		return(1);
531ce4946daSBill Paul 	return(0);
532ce4946daSBill Paul }
533ce4946daSBill Paul 
534ce4946daSBill Paul /*
535ce4946daSBill Paul  * Write to a PHY register through the MII.
536ce4946daSBill Paul  */
537ce4946daSBill Paul static int nge_mii_writereg(sc, frame)
538ce4946daSBill Paul 	struct nge_softc		*sc;
539ce4946daSBill Paul 	struct nge_mii_frame	*frame;
540ce4946daSBill Paul 
541ce4946daSBill Paul {
542ce4946daSBill Paul 	int			s;
543ce4946daSBill Paul 
544ce4946daSBill Paul 	s = splimp();
545ce4946daSBill Paul 	/*
546ce4946daSBill Paul 	 * Set up frame for TX.
547ce4946daSBill Paul 	 */
548ce4946daSBill Paul 
549ce4946daSBill Paul 	frame->mii_stdelim = NGE_MII_STARTDELIM;
550ce4946daSBill Paul 	frame->mii_opcode = NGE_MII_WRITEOP;
551ce4946daSBill Paul 	frame->mii_turnaround = NGE_MII_TURNAROUND;
552ce4946daSBill Paul 
553ce4946daSBill Paul 	/*
554ce4946daSBill Paul  	 * Turn on data output.
555ce4946daSBill Paul 	 */
556ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR);
557ce4946daSBill Paul 
558ce4946daSBill Paul 	nge_mii_sync(sc);
559ce4946daSBill Paul 
560ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_stdelim, 2);
561ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_opcode, 2);
562ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_phyaddr, 5);
563ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_regaddr, 5);
564ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_turnaround, 2);
565ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_data, 16);
566ce4946daSBill Paul 
567ce4946daSBill Paul 	/* Idle bit. */
568ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
569ce4946daSBill Paul 	DELAY(1);
570ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
571ce4946daSBill Paul 	DELAY(1);
572ce4946daSBill Paul 
573ce4946daSBill Paul 	/*
574ce4946daSBill Paul 	 * Turn off xmit.
575ce4946daSBill Paul 	 */
576ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_DIR);
577ce4946daSBill Paul 
578ce4946daSBill Paul 	splx(s);
579ce4946daSBill Paul 
580ce4946daSBill Paul 	return(0);
581ce4946daSBill Paul }
582ce4946daSBill Paul 
583ce4946daSBill Paul static int nge_miibus_readreg(dev, phy, reg)
584ce4946daSBill Paul 	device_t		dev;
585ce4946daSBill Paul 	int			phy, reg;
586ce4946daSBill Paul {
587ce4946daSBill Paul 	struct nge_softc	*sc;
588ce4946daSBill Paul 	struct nge_mii_frame	frame;
589ce4946daSBill Paul 
590ce4946daSBill Paul 	sc = device_get_softc(dev);
591ce4946daSBill Paul 
592ce4946daSBill Paul 	bzero((char *)&frame, sizeof(frame));
593ce4946daSBill Paul 
594ce4946daSBill Paul 	frame.mii_phyaddr = phy;
595ce4946daSBill Paul 	frame.mii_regaddr = reg;
596ce4946daSBill Paul 	nge_mii_readreg(sc, &frame);
597ce4946daSBill Paul 
598ce4946daSBill Paul 	return(frame.mii_data);
599ce4946daSBill Paul }
600ce4946daSBill Paul 
601ce4946daSBill Paul static int 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 
620ce4946daSBill Paul static void nge_miibus_statchg(dev)
621ce4946daSBill Paul 	device_t		dev;
622ce4946daSBill Paul {
623ce4946daSBill Paul 	struct nge_softc	*sc;
624ce4946daSBill Paul 	struct mii_data		*mii;
625ce4946daSBill Paul 
626ce4946daSBill Paul 	sc = device_get_softc(dev);
627ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
628ce4946daSBill Paul 
629ce4946daSBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
630ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_TX_CFG,
631ce4946daSBill Paul 		    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
632ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
633ce4946daSBill Paul 	} else {
634ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_TX_CFG,
635ce4946daSBill Paul 		    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
636ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
637ce4946daSBill Paul 	}
638ce4946daSBill Paul 
639ce4946daSBill Paul 	return;
640ce4946daSBill Paul }
641ce4946daSBill Paul 
642ce4946daSBill Paul static u_int32_t nge_crc(sc, addr)
643ce4946daSBill Paul 	struct nge_softc	*sc;
644ce4946daSBill Paul 	caddr_t			addr;
645ce4946daSBill Paul {
646ce4946daSBill Paul 	u_int32_t		crc, carry;
647ce4946daSBill Paul 	int			i, j;
648ce4946daSBill Paul 	u_int8_t		c;
649ce4946daSBill Paul 
650ce4946daSBill Paul 	/* Compute CRC for the address value. */
651ce4946daSBill Paul 	crc = 0xFFFFFFFF; /* initial value */
652ce4946daSBill Paul 
653ce4946daSBill Paul 	for (i = 0; i < 6; i++) {
654ce4946daSBill Paul 		c = *(addr + i);
655ce4946daSBill Paul 		for (j = 0; j < 8; j++) {
656ce4946daSBill Paul 			carry = ((crc & 0x80000000) ? 1 : 0) ^ (c & 0x01);
657ce4946daSBill Paul 			crc <<= 1;
658ce4946daSBill Paul 			c >>= 1;
659ce4946daSBill Paul 			if (carry)
660ce4946daSBill Paul 				crc = (crc ^ 0x04c11db6) | carry;
661ce4946daSBill Paul 		}
662ce4946daSBill Paul 	}
663ce4946daSBill Paul 
664ce4946daSBill Paul 	/*
665ce4946daSBill Paul 	 * return the filter bit position
666ce4946daSBill Paul 	 */
667ce4946daSBill Paul 
668ce4946daSBill Paul 	return((crc >> 21) & 0x00000FFF);
669ce4946daSBill Paul }
670ce4946daSBill Paul 
671ce4946daSBill Paul static void nge_setmulti(sc)
672ce4946daSBill Paul 	struct nge_softc	*sc;
673ce4946daSBill Paul {
674ce4946daSBill Paul 	struct ifnet		*ifp;
675ce4946daSBill Paul 	struct ifmultiaddr	*ifma;
676ce4946daSBill Paul 	u_int32_t		h = 0, i, filtsave;
677ce4946daSBill Paul 	int			bit, index;
678ce4946daSBill Paul 
679ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
680ce4946daSBill Paul 
681ce4946daSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
682ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL,
683ce4946daSBill Paul 		    NGE_RXFILTCTL_MCHASH|NGE_RXFILTCTL_UCHASH);
684ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLMULTI);
685ce4946daSBill Paul 		return;
686ce4946daSBill Paul 	}
687ce4946daSBill Paul 
688ce4946daSBill Paul 	/*
689ce4946daSBill Paul 	 * We have to explicitly enable the multicast hash table
690ce4946daSBill Paul 	 * on the NatSemi chip if we want to use it, which we do.
691ce4946daSBill Paul 	 * We also have to tell it that we don't want to use the
692ce4946daSBill Paul 	 * hash table for matching unicast addresses.
693ce4946daSBill Paul 	 */
694ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_MCHASH);
695ce4946daSBill Paul 	NGE_CLRBIT(sc, NGE_RXFILT_CTL,
696ce4946daSBill Paul 	    NGE_RXFILTCTL_ALLMULTI|NGE_RXFILTCTL_UCHASH);
697ce4946daSBill Paul 
698ce4946daSBill Paul 	filtsave = CSR_READ_4(sc, NGE_RXFILT_CTL);
699ce4946daSBill Paul 
700ce4946daSBill Paul 	/* first, zot all the existing hash bits */
701ce4946daSBill Paul 	for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) {
702ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i);
703ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0);
704ce4946daSBill Paul 	}
705ce4946daSBill Paul 
706ce4946daSBill Paul 	/*
707ce4946daSBill Paul 	 * From the 11 bits returned by the crc routine, the top 7
708ce4946daSBill Paul 	 * bits represent the 16-bit word in the mcast hash table
709ce4946daSBill Paul 	 * that needs to be updated, and the lower 4 bits represent
710ce4946daSBill Paul 	 * which bit within that byte needs to be set.
711ce4946daSBill Paul 	 */
712ce4946daSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
713ce4946daSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
714ce4946daSBill Paul 			continue;
715ce4946daSBill Paul 		h = nge_crc(sc, LLADDR((struct sockaddr_dl *)ifma->ifma_addr));
716ce4946daSBill Paul 		index = (h >> 4) & 0x7F;
717ce4946daSBill Paul 		bit = h & 0xF;
718ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL,
719ce4946daSBill Paul 		    NGE_FILTADDR_MCAST_LO + (index * 2));
720ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit));
721ce4946daSBill Paul 	}
722ce4946daSBill Paul 
723ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, filtsave);
724ce4946daSBill Paul 
725ce4946daSBill Paul 	return;
726ce4946daSBill Paul }
727ce4946daSBill Paul 
728ce4946daSBill Paul static void nge_reset(sc)
729ce4946daSBill Paul 	struct nge_softc	*sc;
730ce4946daSBill Paul {
731ce4946daSBill Paul 	register int		i;
732ce4946daSBill Paul 
733ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET);
734ce4946daSBill Paul 
735ce4946daSBill Paul 	for (i = 0; i < NGE_TIMEOUT; i++) {
736ce4946daSBill Paul 		if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET))
737ce4946daSBill Paul 			break;
738ce4946daSBill Paul 	}
739ce4946daSBill Paul 
740ce4946daSBill Paul 	if (i == NGE_TIMEOUT)
741ce4946daSBill Paul 		printf("nge%d: reset never completed\n", sc->nge_unit);
742ce4946daSBill Paul 
743ce4946daSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
744ce4946daSBill Paul 	DELAY(1000);
745ce4946daSBill Paul 
746ce4946daSBill Paul 	/*
747ce4946daSBill Paul 	 * If this is a NetSemi chip, make sure to clear
748ce4946daSBill Paul 	 * PME mode.
749ce4946daSBill Paul 	 */
750ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS);
751ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, 0);
752ce4946daSBill Paul 
753ce4946daSBill Paul         return;
754ce4946daSBill Paul }
755ce4946daSBill Paul 
756ce4946daSBill Paul /*
757ce4946daSBill Paul  * Probe for an NatSemi chip. Check the PCI vendor and device
758ce4946daSBill Paul  * IDs against our list and return a device name if we find a match.
759ce4946daSBill Paul  */
760ce4946daSBill Paul static int nge_probe(dev)
761ce4946daSBill Paul 	device_t		dev;
762ce4946daSBill Paul {
763ce4946daSBill Paul 	struct nge_type		*t;
764ce4946daSBill Paul 
765ce4946daSBill Paul 	t = nge_devs;
766ce4946daSBill Paul 
767ce4946daSBill Paul 	while(t->nge_name != NULL) {
768ce4946daSBill Paul 		if ((pci_get_vendor(dev) == t->nge_vid) &&
769ce4946daSBill Paul 		    (pci_get_device(dev) == t->nge_did)) {
770ce4946daSBill Paul 			device_set_desc(dev, t->nge_name);
771ce4946daSBill Paul 			return(0);
772ce4946daSBill Paul 		}
773ce4946daSBill Paul 		t++;
774ce4946daSBill Paul 	}
775ce4946daSBill Paul 
776ce4946daSBill Paul 	return(ENXIO);
777ce4946daSBill Paul }
778ce4946daSBill Paul 
779ce4946daSBill Paul /*
780ce4946daSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
781ce4946daSBill Paul  * setup and ethernet/BPF attach.
782ce4946daSBill Paul  */
783ce4946daSBill Paul static int nge_attach(dev)
784ce4946daSBill Paul 	device_t		dev;
785ce4946daSBill Paul {
786ce4946daSBill Paul 	int			s;
787ce4946daSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
788ce4946daSBill Paul 	u_int32_t		command;
789ce4946daSBill Paul 	struct nge_softc	*sc;
790ce4946daSBill Paul 	struct ifnet		*ifp;
791ce4946daSBill Paul 	int			unit, error = 0, rid;
792ce4946daSBill Paul 
793ce4946daSBill Paul 	s = splimp();
794ce4946daSBill Paul 
795ce4946daSBill Paul 	sc = device_get_softc(dev);
796ce4946daSBill Paul 	unit = device_get_unit(dev);
797ce4946daSBill Paul 	bzero(sc, sizeof(struct nge_softc));
798ce4946daSBill Paul 
799ce4946daSBill Paul 	mtx_init(&sc->nge_mtx, device_get_nameunit(dev), MTX_DEF|MTX_RECURSE);
800ce4946daSBill Paul 
801ce4946daSBill Paul 	/*
802ce4946daSBill Paul 	 * Handle power management nonsense.
803ce4946daSBill Paul 	 */
804ce4946daSBill Paul 	if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) {
805ce4946daSBill Paul 		u_int32_t		iobase, membase, irq;
806ce4946daSBill Paul 
807ce4946daSBill Paul 		/* Save important PCI config data. */
808ce4946daSBill Paul 		iobase = pci_read_config(dev, NGE_PCI_LOIO, 4);
809ce4946daSBill Paul 		membase = pci_read_config(dev, NGE_PCI_LOMEM, 4);
810ce4946daSBill Paul 		irq = pci_read_config(dev, NGE_PCI_INTLINE, 4);
811ce4946daSBill Paul 
812ce4946daSBill Paul 		/* Reset the power state. */
813ce4946daSBill Paul 		printf("nge%d: chip is in D%d power mode "
814ce4946daSBill Paul 		    "-- setting to D0\n", unit,
815ce4946daSBill Paul 		    pci_get_powerstate(dev));
816ce4946daSBill Paul 		pci_set_powerstate(dev, PCI_POWERSTATE_D0);
817ce4946daSBill Paul 
818ce4946daSBill Paul 		/* Restore PCI config data. */
819ce4946daSBill Paul 		pci_write_config(dev, NGE_PCI_LOIO, iobase, 4);
820ce4946daSBill Paul 		pci_write_config(dev, NGE_PCI_LOMEM, membase, 4);
821ce4946daSBill Paul 		pci_write_config(dev, NGE_PCI_INTLINE, irq, 4);
822ce4946daSBill Paul 	}
823ce4946daSBill Paul 
824ce4946daSBill Paul 	/*
825ce4946daSBill Paul 	 * Map control/status registers.
826ce4946daSBill Paul 	 */
827ce4946daSBill Paul 	pci_enable_busmaster(dev);
828ce4946daSBill Paul 	pci_enable_io(dev, PCIM_CMD_PORTEN);
829ce4946daSBill Paul 	pci_enable_io(dev, PCIM_CMD_MEMEN);
830ce4946daSBill Paul 	command = pci_read_config(dev, PCIR_COMMAND, 4);
831ce4946daSBill Paul 
832ce4946daSBill Paul #ifdef NGE_USEIOSPACE
833ce4946daSBill Paul 	if (!(command & PCIM_CMD_PORTEN)) {
834ce4946daSBill Paul 		printf("nge%d: failed to enable I/O ports!\n", unit);
835ce4946daSBill Paul 		error = ENXIO;;
836ce4946daSBill Paul 		goto fail;
837ce4946daSBill Paul 	}
838ce4946daSBill Paul #else
839ce4946daSBill Paul 	if (!(command & PCIM_CMD_MEMEN)) {
840ce4946daSBill Paul 		printf("nge%d: failed to enable memory mapping!\n", unit);
841ce4946daSBill Paul 		error = ENXIO;;
842ce4946daSBill Paul 		goto fail;
843ce4946daSBill Paul 	}
844ce4946daSBill Paul #endif
845ce4946daSBill Paul 
846ce4946daSBill Paul 	rid = NGE_RID;
847ce4946daSBill Paul 	sc->nge_res = bus_alloc_resource(dev, NGE_RES, &rid,
848ce4946daSBill Paul 	    0, ~0, 1, RF_ACTIVE);
849ce4946daSBill Paul 
850ce4946daSBill Paul 	if (sc->nge_res == NULL) {
851ce4946daSBill Paul 		printf("nge%d: couldn't map ports/memory\n", unit);
852ce4946daSBill Paul 		error = ENXIO;
853ce4946daSBill Paul 		goto fail;
854ce4946daSBill Paul 	}
855ce4946daSBill Paul 
856ce4946daSBill Paul 	sc->nge_btag = rman_get_bustag(sc->nge_res);
857ce4946daSBill Paul 	sc->nge_bhandle = rman_get_bushandle(sc->nge_res);
858ce4946daSBill Paul 
859ce4946daSBill Paul 	/* Allocate interrupt */
860ce4946daSBill Paul 	rid = 0;
861ce4946daSBill Paul 	sc->nge_irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
862ce4946daSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
863ce4946daSBill Paul 
864ce4946daSBill Paul 	if (sc->nge_irq == NULL) {
865ce4946daSBill Paul 		printf("nge%d: couldn't map interrupt\n", unit);
866ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
867ce4946daSBill Paul 		error = ENXIO;
868ce4946daSBill Paul 		goto fail;
869ce4946daSBill Paul 	}
870ce4946daSBill Paul 
871ce4946daSBill Paul 	error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET,
872ce4946daSBill Paul 	    nge_intr, sc, &sc->nge_intrhand);
873ce4946daSBill Paul 
874ce4946daSBill Paul 	if (error) {
875ce4946daSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
876ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
877ce4946daSBill Paul 		printf("nge%d: couldn't set up irq\n", unit);
878ce4946daSBill Paul 		goto fail;
879ce4946daSBill Paul 	}
880ce4946daSBill Paul 
881ce4946daSBill Paul 	/* Reset the adapter. */
882ce4946daSBill Paul 	nge_reset(sc);
883ce4946daSBill Paul 
884ce4946daSBill Paul 	/*
885ce4946daSBill Paul 	 * Get station address from the EEPROM.
886ce4946daSBill Paul 	 */
887ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[4], NGE_EE_NODEADDR, 1, 0);
888ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[2], NGE_EE_NODEADDR + 1, 1, 0);
889ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[0], NGE_EE_NODEADDR + 2, 1, 0);
890ce4946daSBill Paul 
891ce4946daSBill Paul 	/*
892ce4946daSBill Paul 	 * A NatSemi chip was detected. Inform the world.
893ce4946daSBill Paul 	 */
894ce4946daSBill Paul 	printf("nge%d: Ethernet address: %6D\n", unit, eaddr, ":");
895ce4946daSBill Paul 
896ce4946daSBill Paul 	sc->nge_unit = unit;
897ce4946daSBill Paul 	bcopy(eaddr, (char *)&sc->arpcom.ac_enaddr, ETHER_ADDR_LEN);
898ce4946daSBill Paul 
899ce4946daSBill Paul 	sc->nge_ldata = contigmalloc(sizeof(struct nge_list_data), M_DEVBUF,
900ce4946daSBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
901ce4946daSBill Paul 
902ce4946daSBill Paul 	if (sc->nge_ldata == NULL) {
903ce4946daSBill Paul 		printf("nge%d: no memory for list buffers!\n", unit);
904ce4946daSBill Paul 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
905ce4946daSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
906ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
907ce4946daSBill Paul 		error = ENXIO;
908ce4946daSBill Paul 		goto fail;
909ce4946daSBill Paul 	}
910ce4946daSBill Paul 	bzero(sc->nge_ldata, sizeof(struct nge_list_data));
911ce4946daSBill Paul 
912ce4946daSBill Paul 	/* Try to allocate memory for jumbo buffers. */
913ce4946daSBill Paul 	if (nge_alloc_jumbo_mem(sc)) {
914ce4946daSBill Paul 		printf("nge%d: jumbo buffer allocation failed\n",
915ce4946daSBill Paul                     sc->nge_unit);
916ce4946daSBill Paul 		contigfree(sc->nge_ldata,
917ce4946daSBill Paul 		    sizeof(struct nge_list_data), M_DEVBUF);
918ce4946daSBill Paul 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
919ce4946daSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
920ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
921ce4946daSBill Paul 		error = ENXIO;
922ce4946daSBill Paul 		goto fail;
923ce4946daSBill Paul 	}
924ce4946daSBill Paul 
925ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
926ce4946daSBill Paul 	ifp->if_softc = sc;
927ce4946daSBill Paul 	ifp->if_unit = unit;
928ce4946daSBill Paul 	ifp->if_name = "nge";
929ce4946daSBill Paul 	ifp->if_mtu = ETHERMTU;
930ce4946daSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
931ce4946daSBill Paul 	ifp->if_ioctl = nge_ioctl;
932ce4946daSBill Paul 	ifp->if_output = ether_output;
933ce4946daSBill Paul 	ifp->if_start = nge_start;
934ce4946daSBill Paul 	ifp->if_watchdog = nge_watchdog;
935ce4946daSBill Paul 	ifp->if_init = nge_init;
936ce4946daSBill Paul 	ifp->if_baudrate = 1000000000;
937ce4946daSBill Paul 	ifp->if_snd.ifq_maxlen = NGE_TX_LIST_CNT - 1;
938ce4946daSBill Paul 	ifp->if_hwassist = NGE_CSUM_FEATURES;
939ce4946daSBill Paul 
940ce4946daSBill Paul 	/*
941ce4946daSBill Paul 	 * Do MII setup.
942ce4946daSBill Paul 	 */
943ce4946daSBill Paul 	if (mii_phy_probe(dev, &sc->nge_miibus,
944ce4946daSBill Paul 	    nge_ifmedia_upd, nge_ifmedia_sts)) {
945ce4946daSBill Paul 		printf("nge%d: MII without any PHY!\n", sc->nge_unit);
946ce4946daSBill Paul 		nge_free_jumbo_mem(sc);
947ce4946daSBill Paul 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
948ce4946daSBill Paul 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
949ce4946daSBill Paul 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
950ce4946daSBill Paul 		error = ENXIO;
951ce4946daSBill Paul 		goto fail;
952ce4946daSBill Paul 	}
953ce4946daSBill Paul 
954ce4946daSBill Paul 	/*
955ce4946daSBill Paul 	 * Call MI attach routine.
956ce4946daSBill Paul 	 */
957ce4946daSBill Paul 	ether_ifattach(ifp, ETHER_BPF_SUPPORTED);
958ce4946daSBill Paul 	callout_handle_init(&sc->nge_stat_ch);
959ce4946daSBill Paul 
960ce4946daSBill Paul fail:
961ce4946daSBill Paul 	splx(s);
962ce4946daSBill Paul 	mtx_destroy(&sc->nge_mtx);
963ce4946daSBill Paul 	return(error);
964ce4946daSBill Paul }
965ce4946daSBill Paul 
966ce4946daSBill Paul static int nge_detach(dev)
967ce4946daSBill Paul 	device_t		dev;
968ce4946daSBill Paul {
969ce4946daSBill Paul 	struct nge_softc	*sc;
970ce4946daSBill Paul 	struct ifnet		*ifp;
971ce4946daSBill Paul 	int			s;
972ce4946daSBill Paul 
973ce4946daSBill Paul 	s = splimp();
974ce4946daSBill Paul 
975ce4946daSBill Paul 	sc = device_get_softc(dev);
976ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
977ce4946daSBill Paul 
978ce4946daSBill Paul 	nge_reset(sc);
979ce4946daSBill Paul 	nge_stop(sc);
980ce4946daSBill Paul 	ether_ifdetach(ifp, ETHER_BPF_SUPPORTED);
981ce4946daSBill Paul 
982ce4946daSBill Paul 	bus_generic_detach(dev);
983ce4946daSBill Paul 	device_delete_child(dev, sc->nge_miibus);
984ce4946daSBill Paul 
985ce4946daSBill Paul 	bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
986ce4946daSBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
987ce4946daSBill Paul 	bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
988ce4946daSBill Paul 
989ce4946daSBill Paul 	contigfree(sc->nge_ldata, sizeof(struct nge_list_data), M_DEVBUF);
990ce4946daSBill Paul 	nge_free_jumbo_mem(sc);
991ce4946daSBill Paul 
992ce4946daSBill Paul 	splx(s);
993ce4946daSBill Paul 	mtx_destroy(&sc->nge_mtx);
994ce4946daSBill Paul 
995ce4946daSBill Paul 	return(0);
996ce4946daSBill Paul }
997ce4946daSBill Paul 
998ce4946daSBill Paul /*
999ce4946daSBill Paul  * Initialize the transmit descriptors.
1000ce4946daSBill Paul  */
1001ce4946daSBill Paul static int nge_list_tx_init(sc)
1002ce4946daSBill Paul 	struct nge_softc	*sc;
1003ce4946daSBill Paul {
1004ce4946daSBill Paul 	struct nge_list_data	*ld;
1005ce4946daSBill Paul 	struct nge_ring_data	*cd;
1006ce4946daSBill Paul 	int			i;
1007ce4946daSBill Paul 
1008ce4946daSBill Paul 	cd = &sc->nge_cdata;
1009ce4946daSBill Paul 	ld = sc->nge_ldata;
1010ce4946daSBill Paul 
1011ce4946daSBill Paul 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
1012ce4946daSBill Paul 		if (i == (NGE_TX_LIST_CNT - 1)) {
1013ce4946daSBill Paul 			ld->nge_tx_list[i].nge_nextdesc =
1014ce4946daSBill Paul 			    &ld->nge_tx_list[0];
1015ce4946daSBill Paul 			ld->nge_tx_list[i].nge_next =
1016ce4946daSBill Paul 			    vtophys(&ld->nge_tx_list[0]);
1017ce4946daSBill Paul 		} else {
1018ce4946daSBill Paul 			ld->nge_tx_list[i].nge_nextdesc =
1019ce4946daSBill Paul 			    &ld->nge_tx_list[i + 1];
1020ce4946daSBill Paul 			ld->nge_tx_list[i].nge_next =
1021ce4946daSBill Paul 			    vtophys(&ld->nge_tx_list[i + 1]);
1022ce4946daSBill Paul 		}
1023ce4946daSBill Paul 		ld->nge_tx_list[i].nge_mbuf = NULL;
1024ce4946daSBill Paul 		ld->nge_tx_list[i].nge_ptr = 0;
1025ce4946daSBill Paul 		ld->nge_tx_list[i].nge_ctl = 0;
1026ce4946daSBill Paul 	}
1027ce4946daSBill Paul 
1028ce4946daSBill Paul 	cd->nge_tx_prod = cd->nge_tx_cons = cd->nge_tx_cnt = 0;
1029ce4946daSBill Paul 
1030ce4946daSBill Paul 	return(0);
1031ce4946daSBill Paul }
1032ce4946daSBill Paul 
1033ce4946daSBill Paul 
1034ce4946daSBill Paul /*
1035ce4946daSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
1036ce4946daSBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
1037ce4946daSBill Paul  * points back to the first.
1038ce4946daSBill Paul  */
1039ce4946daSBill Paul static int nge_list_rx_init(sc)
1040ce4946daSBill Paul 	struct nge_softc	*sc;
1041ce4946daSBill Paul {
1042ce4946daSBill Paul 	struct nge_list_data	*ld;
1043ce4946daSBill Paul 	struct nge_ring_data	*cd;
1044ce4946daSBill Paul 	int			i;
1045ce4946daSBill Paul 
1046ce4946daSBill Paul 	ld = sc->nge_ldata;
1047ce4946daSBill Paul 	cd = &sc->nge_cdata;
1048ce4946daSBill Paul 
1049ce4946daSBill Paul 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
1050ce4946daSBill Paul 		if (nge_newbuf(sc, &ld->nge_rx_list[i], NULL) == ENOBUFS)
1051ce4946daSBill Paul 			return(ENOBUFS);
1052ce4946daSBill Paul 		if (i == (NGE_RX_LIST_CNT - 1)) {
1053ce4946daSBill Paul 			ld->nge_rx_list[i].nge_nextdesc =
1054ce4946daSBill Paul 			    &ld->nge_rx_list[0];
1055ce4946daSBill Paul 			ld->nge_rx_list[i].nge_next =
1056ce4946daSBill Paul 			    vtophys(&ld->nge_rx_list[0]);
1057ce4946daSBill Paul 		} else {
1058ce4946daSBill Paul 			ld->nge_rx_list[i].nge_nextdesc =
1059ce4946daSBill Paul 			    &ld->nge_rx_list[i + 1];
1060ce4946daSBill Paul 			ld->nge_rx_list[i].nge_next =
1061ce4946daSBill Paul 			    vtophys(&ld->nge_rx_list[i + 1]);
1062ce4946daSBill Paul 		}
1063ce4946daSBill Paul 	}
1064ce4946daSBill Paul 
1065ce4946daSBill Paul 	cd->nge_rx_prod = 0;
1066ce4946daSBill Paul 
1067ce4946daSBill Paul 	return(0);
1068ce4946daSBill Paul }
1069ce4946daSBill Paul 
1070ce4946daSBill Paul /*
1071ce4946daSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
1072ce4946daSBill Paul  */
1073ce4946daSBill Paul static int nge_newbuf(sc, c, m)
1074ce4946daSBill Paul 	struct nge_softc	*sc;
1075ce4946daSBill Paul 	struct nge_desc		*c;
1076ce4946daSBill Paul 	struct mbuf		*m;
1077ce4946daSBill Paul {
1078ce4946daSBill Paul 	struct mbuf		*m_new = NULL;
1079ce4946daSBill Paul 	caddr_t			*buf = NULL;
1080ce4946daSBill Paul 
1081ce4946daSBill Paul 	if (m == NULL) {
1082ce4946daSBill Paul 		MGETHDR(m_new, M_DONTWAIT, MT_DATA);
1083ce4946daSBill Paul 		if (m_new == NULL) {
1084ce4946daSBill Paul 			printf("nge%d: no memory for rx list "
1085ce4946daSBill Paul 			    "-- packet dropped!\n", sc->nge_unit);
1086ce4946daSBill Paul 			return(ENOBUFS);
1087ce4946daSBill Paul 		}
1088ce4946daSBill Paul 
1089ce4946daSBill Paul 		/* Allocate the jumbo buffer */
1090ce4946daSBill Paul 		buf = nge_jalloc(sc);
1091ce4946daSBill Paul 		if (buf == NULL) {
1092ce4946daSBill Paul #ifdef NGE_VERBOSE
1093ce4946daSBill Paul 			printf("nge%d: jumbo allocation failed "
1094ce4946daSBill Paul 			    "-- packet dropped!\n", sc->nge_unit);
1095ce4946daSBill Paul #endif
1096ce4946daSBill Paul 			m_freem(m_new);
1097ce4946daSBill Paul 			return(ENOBUFS);
1098ce4946daSBill Paul 		}
1099ce4946daSBill Paul 		/* Attach the buffer to the mbuf */
1100ce4946daSBill Paul 		m_new->m_data = (void *)buf;
1101ce4946daSBill Paul 		m_new->m_len = m_new->m_pkthdr.len = NGE_MCLBYTES;
1102ce4946daSBill Paul 		MEXTADD(m_new, buf, NGE_MCLBYTES, nge_jfree,
1103065a7922SBill Paul 		    (struct nge_softc *)sc, 0, EXT_NET_DRV);
1104ce4946daSBill Paul 	} else {
1105ce4946daSBill Paul 		m_new = m;
1106ce4946daSBill Paul 		m_new->m_len = m_new->m_pkthdr.len = NGE_MCLBYTES;
1107ce4946daSBill Paul 		m_new->m_data = m_new->m_ext.ext_buf;
1108ce4946daSBill Paul 	}
1109ce4946daSBill Paul 
1110ce4946daSBill Paul 	m_adj(m_new, sizeof(u_int64_t));
1111ce4946daSBill Paul 
1112ce4946daSBill Paul 	c->nge_mbuf = m_new;
1113ce4946daSBill Paul 	c->nge_ptr = vtophys(mtod(m_new, caddr_t));
1114ce4946daSBill Paul 	c->nge_ctl = m_new->m_len;
1115ce4946daSBill Paul 	c->nge_extsts = 0;
1116ce4946daSBill Paul 
1117ce4946daSBill Paul 	return(0);
1118ce4946daSBill Paul }
1119ce4946daSBill Paul 
1120ce4946daSBill Paul static int nge_alloc_jumbo_mem(sc)
1121ce4946daSBill Paul 	struct nge_softc	*sc;
1122ce4946daSBill Paul {
1123ce4946daSBill Paul 	caddr_t			ptr;
1124ce4946daSBill Paul 	register int		i;
1125ce4946daSBill Paul 	struct nge_jpool_entry   *entry;
1126ce4946daSBill Paul 
1127ce4946daSBill Paul 	/* Grab a big chunk o' storage. */
1128ce4946daSBill Paul 	sc->nge_cdata.nge_jumbo_buf = contigmalloc(NGE_JMEM, M_DEVBUF,
1129ce4946daSBill Paul 	    M_NOWAIT, 0, 0xffffffff, PAGE_SIZE, 0);
1130ce4946daSBill Paul 
1131ce4946daSBill Paul 	if (sc->nge_cdata.nge_jumbo_buf == NULL) {
1132ce4946daSBill Paul 		printf("nge%d: no memory for jumbo buffers!\n", sc->nge_unit);
1133ce4946daSBill Paul 		return(ENOBUFS);
1134ce4946daSBill Paul 	}
1135ce4946daSBill Paul 
1136ce4946daSBill Paul 	SLIST_INIT(&sc->nge_jfree_listhead);
1137ce4946daSBill Paul 	SLIST_INIT(&sc->nge_jinuse_listhead);
1138ce4946daSBill Paul 
1139ce4946daSBill Paul 	/*
1140ce4946daSBill Paul 	 * Now divide it up into 9K pieces and save the addresses
1141ce4946daSBill Paul 	 * in an array.
1142ce4946daSBill Paul 	 */
1143ce4946daSBill Paul 	ptr = sc->nge_cdata.nge_jumbo_buf;
1144ce4946daSBill Paul 	for (i = 0; i < NGE_JSLOTS; i++) {
1145ce4946daSBill Paul 		sc->nge_cdata.nge_jslots[i] = ptr;
1146ce4946daSBill Paul 		ptr += NGE_MCLBYTES;
1147ce4946daSBill Paul 		entry = malloc(sizeof(struct nge_jpool_entry),
1148ce4946daSBill Paul 		    M_DEVBUF, M_NOWAIT);
1149ce4946daSBill Paul 		if (entry == NULL) {
1150ce4946daSBill Paul 			free(sc->nge_cdata.nge_jumbo_buf, M_DEVBUF);
1151ce4946daSBill Paul 			sc->nge_cdata.nge_jumbo_buf = NULL;
1152ce4946daSBill Paul 			printf("nge%d: no memory for jumbo "
1153ce4946daSBill Paul 			    "buffer queue!\n", sc->nge_unit);
1154ce4946daSBill Paul 			return(ENOBUFS);
1155ce4946daSBill Paul 		}
1156ce4946daSBill Paul 		entry->slot = i;
1157ce4946daSBill Paul 		SLIST_INSERT_HEAD(&sc->nge_jfree_listhead,
1158ce4946daSBill Paul 		    entry, jpool_entries);
1159ce4946daSBill Paul 	}
1160ce4946daSBill Paul 
1161ce4946daSBill Paul 	return(0);
1162ce4946daSBill Paul }
1163ce4946daSBill Paul 
1164ce4946daSBill Paul static void nge_free_jumbo_mem(sc)
1165ce4946daSBill Paul 	struct nge_softc	*sc;
1166ce4946daSBill Paul {
1167ce4946daSBill Paul 	register int		i;
1168ce4946daSBill Paul 	struct nge_jpool_entry   *entry;
1169ce4946daSBill Paul 
1170ce4946daSBill Paul 	for (i = 0; i < NGE_JSLOTS; i++) {
1171ce4946daSBill Paul 		entry = SLIST_FIRST(&sc->nge_jfree_listhead);
1172ce4946daSBill Paul 		free(entry, M_DEVBUF);
1173ce4946daSBill Paul 		SLIST_REMOVE_HEAD(&sc->nge_jfree_listhead, jpool_entries);
1174ce4946daSBill Paul 	}
1175ce4946daSBill Paul 
1176ce4946daSBill Paul 	contigfree(sc->nge_cdata.nge_jumbo_buf, NGE_JMEM, M_DEVBUF);
1177ce4946daSBill Paul 
1178ce4946daSBill Paul 	return;
1179ce4946daSBill Paul }
1180ce4946daSBill Paul 
1181ce4946daSBill Paul /*
1182ce4946daSBill Paul  * Allocate a jumbo buffer.
1183ce4946daSBill Paul  */
1184ce4946daSBill Paul static void *nge_jalloc(sc)
1185ce4946daSBill Paul 	struct nge_softc	*sc;
1186ce4946daSBill Paul {
1187ce4946daSBill Paul 	struct nge_jpool_entry   *entry;
1188ce4946daSBill Paul 
1189ce4946daSBill Paul 	entry = SLIST_FIRST(&sc->nge_jfree_listhead);
1190ce4946daSBill Paul 
1191ce4946daSBill Paul 	if (entry == NULL) {
1192ce4946daSBill Paul #ifdef NGE_VERBOSE
1193ce4946daSBill Paul 		printf("nge%d: no free jumbo buffers\n", sc->nge_unit);
1194ce4946daSBill Paul #endif
1195ce4946daSBill Paul 		return(NULL);
1196ce4946daSBill Paul 	}
1197ce4946daSBill Paul 
1198ce4946daSBill Paul 	SLIST_REMOVE_HEAD(&sc->nge_jfree_listhead, jpool_entries);
1199ce4946daSBill Paul 	SLIST_INSERT_HEAD(&sc->nge_jinuse_listhead, entry, jpool_entries);
1200ce4946daSBill Paul 	return(sc->nge_cdata.nge_jslots[entry->slot]);
1201ce4946daSBill Paul }
1202ce4946daSBill Paul 
1203ce4946daSBill Paul /*
1204ce4946daSBill Paul  * Release a jumbo buffer.
1205ce4946daSBill Paul  */
1206ce4946daSBill Paul static void nge_jfree(buf, args)
1207ce4946daSBill Paul 	caddr_t			buf;
1208ce4946daSBill Paul 	void			*args;
1209ce4946daSBill Paul {
1210ce4946daSBill Paul 	struct nge_softc	*sc;
1211ce4946daSBill Paul 	int		        i;
1212ce4946daSBill Paul 	struct nge_jpool_entry   *entry;
1213ce4946daSBill Paul 
1214ce4946daSBill Paul 	/* Extract the softc struct pointer. */
1215ce4946daSBill Paul 	sc = args;
1216ce4946daSBill Paul 
1217ce4946daSBill Paul 	if (sc == NULL)
1218ce4946daSBill Paul 		panic("nge_jfree: can't find softc pointer!");
1219ce4946daSBill Paul 
1220ce4946daSBill Paul 	/* calculate the slot this buffer belongs to */
1221ce4946daSBill Paul 	i = ((vm_offset_t)buf
1222ce4946daSBill Paul 	     - (vm_offset_t)sc->nge_cdata.nge_jumbo_buf) / NGE_JLEN;
1223ce4946daSBill Paul 
1224ce4946daSBill Paul 	if ((i < 0) || (i >= NGE_JSLOTS))
1225ce4946daSBill Paul 		panic("nge_jfree: asked to free buffer that we don't manage!");
1226ce4946daSBill Paul 
1227ce4946daSBill Paul 	entry = SLIST_FIRST(&sc->nge_jinuse_listhead);
1228ce4946daSBill Paul 	if (entry == NULL)
1229ce4946daSBill Paul 		panic("nge_jfree: buffer not in use!");
1230ce4946daSBill Paul 	entry->slot = i;
1231ce4946daSBill Paul 	SLIST_REMOVE_HEAD(&sc->nge_jinuse_listhead, jpool_entries);
1232ce4946daSBill Paul 	SLIST_INSERT_HEAD(&sc->nge_jfree_listhead, entry, jpool_entries);
1233ce4946daSBill Paul 
1234ce4946daSBill Paul 	return;
1235ce4946daSBill Paul }
1236ce4946daSBill Paul /*
1237ce4946daSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
1238ce4946daSBill Paul  * the higher level protocols.
1239ce4946daSBill Paul  */
1240ce4946daSBill Paul static void nge_rxeof(sc)
1241ce4946daSBill Paul 	struct nge_softc	*sc;
1242ce4946daSBill Paul {
1243ce4946daSBill Paul         struct ether_header	*eh;
1244ce4946daSBill Paul         struct mbuf		*m;
1245ce4946daSBill Paul         struct ifnet		*ifp;
1246ce4946daSBill Paul 	struct nge_desc		*cur_rx;
1247ce4946daSBill Paul 	int			i, total_len = 0;
1248ce4946daSBill Paul 	u_int32_t		rxstat;
1249ce4946daSBill Paul 
1250ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1251ce4946daSBill Paul 	i = sc->nge_cdata.nge_rx_prod;
1252ce4946daSBill Paul 
1253ce4946daSBill Paul 	while(NGE_OWNDESC(&sc->nge_ldata->nge_rx_list[i])) {
1254ce4946daSBill Paul 		struct mbuf		*m0 = NULL;
1255ce4946daSBill Paul 		u_int32_t		extsts;
1256ce4946daSBill Paul 
1257ce4946daSBill Paul 		cur_rx = &sc->nge_ldata->nge_rx_list[i];
1258ce4946daSBill Paul 		rxstat = cur_rx->nge_rxstat;
1259ce4946daSBill Paul 		extsts = cur_rx->nge_extsts;
1260ce4946daSBill Paul 		m = cur_rx->nge_mbuf;
1261ce4946daSBill Paul 		cur_rx->nge_mbuf = NULL;
1262ce4946daSBill Paul 		total_len = NGE_RXBYTES(cur_rx);
1263ce4946daSBill Paul 		NGE_INC(i, NGE_RX_LIST_CNT);
1264ce4946daSBill Paul 
1265ce4946daSBill Paul 		/*
1266ce4946daSBill Paul 		 * If an error occurs, update stats, clear the
1267ce4946daSBill Paul 		 * status word and leave the mbuf cluster in place:
1268ce4946daSBill Paul 		 * it should simply get re-used next time this descriptor
1269ce4946daSBill Paul 	 	 * comes up in the ring.
1270ce4946daSBill Paul 		 */
1271ce4946daSBill Paul 		if (!(rxstat & NGE_CMDSTS_PKT_OK)) {
1272ce4946daSBill Paul 			ifp->if_ierrors++;
1273ce4946daSBill Paul 			nge_newbuf(sc, cur_rx, m);
1274ce4946daSBill Paul 			continue;
1275ce4946daSBill Paul 		}
1276ce4946daSBill Paul 
1277ce4946daSBill Paul 
1278ce4946daSBill Paul 		/*
1279ce4946daSBill Paul 		 * Ok. NatSemi really screwed up here. This is the
1280ce4946daSBill Paul 		 * only gigE chip I know of with alignment constraints
1281ce4946daSBill Paul 		 * on receive buffers. RX buffers must be 64-bit aligned.
1282ce4946daSBill Paul 		 */
1283ce4946daSBill Paul 		m0 = m_devget(mtod(m, char *) - ETHER_ALIGN,
1284ce4946daSBill Paul 		    total_len + ETHER_ALIGN, 0, ifp, NULL);
1285ce4946daSBill Paul 		nge_newbuf(sc, cur_rx, m);
1286ce4946daSBill Paul 		if (m0 == NULL) {
1287ce4946daSBill Paul 			printf("nge%d: no receive buffers "
1288ce4946daSBill Paul 			    "available -- packet dropped!\n",
1289ce4946daSBill Paul 			    sc->nge_unit);
1290ce4946daSBill Paul 			ifp->if_ierrors++;
1291ce4946daSBill Paul 			continue;
1292ce4946daSBill Paul 		}
1293ce4946daSBill Paul 		m_adj(m0, ETHER_ALIGN);
1294ce4946daSBill Paul 		m = m0;
1295ce4946daSBill Paul 
1296ce4946daSBill Paul 		ifp->if_ipackets++;
1297ce4946daSBill Paul 		eh = mtod(m, struct ether_header *);
1298ce4946daSBill Paul 
1299ce4946daSBill Paul 		/* Remove header from mbuf and pass it on. */
1300ce4946daSBill Paul 		m_adj(m, sizeof(struct ether_header));
1301ce4946daSBill Paul 
1302ce4946daSBill Paul 		/* Do IP checksum checking. */
1303ce4946daSBill Paul 		if (extsts & NGE_RXEXTSTS_IPPKT)
1304ce4946daSBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1305ce4946daSBill Paul 		if (!(extsts & NGE_RXEXTSTS_IPCSUMERR))
130601702579SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
13072195de46SBill Paul 		if ((extsts & NGE_RXEXTSTS_TCPPKT &&
13082195de46SBill Paul 		    !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) ||
13092195de46SBill Paul 		    (extsts & NGE_RXEXTSTS_UDPPKT &&
13102195de46SBill Paul 		    !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) {
13112195de46SBill Paul 			m->m_pkthdr.csum_flags |=
13122195de46SBill Paul 			    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1313c9215605SBill Paul 			m->m_pkthdr.csum_data = 0xffff;
13142195de46SBill Paul 		}
1315ce4946daSBill Paul 
1316ce4946daSBill Paul #if NVLAN > 0
1317ce4946daSBill Paul 		/*
1318ce4946daSBill Paul 		 * If we received a packet with a vlan tag, pass it
1319ce4946daSBill Paul 		 * to vlan_input() instead of ether_input().
1320ce4946daSBill Paul 		 */
1321ce4946daSBill Paul 		if (extsts & NGE_RXEXTSTS_VLANPKT) {
1322ce4946daSBill Paul 			vlan_input_tag(eh, m, extsts & NGE_RXEXTSTS_VTCI);
1323ce4946daSBill Paul                         continue;
1324ce4946daSBill Paul                 }
1325ce4946daSBill Paul #endif
1326ce4946daSBill Paul 
1327ce4946daSBill Paul 		ether_input(ifp, eh, m);
1328ce4946daSBill Paul 	}
1329ce4946daSBill Paul 
1330ce4946daSBill Paul 	sc->nge_cdata.nge_rx_prod = i;
1331ce4946daSBill Paul 
1332ce4946daSBill Paul 	return;
1333ce4946daSBill Paul }
1334ce4946daSBill Paul 
1335ce4946daSBill Paul void nge_rxeoc(sc)
1336ce4946daSBill Paul 	struct nge_softc	*sc;
1337ce4946daSBill Paul {
1338ce4946daSBill Paul 	struct ifnet		*ifp;
1339ce4946daSBill Paul 
1340ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1341ce4946daSBill Paul 	nge_rxeof(sc);
1342ce4946daSBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
1343ce4946daSBill Paul 	nge_init(sc);
1344ce4946daSBill Paul 	return;
1345ce4946daSBill Paul }
1346ce4946daSBill Paul 
1347ce4946daSBill Paul /*
1348ce4946daSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
1349ce4946daSBill Paul  * the list buffers.
1350ce4946daSBill Paul  */
1351ce4946daSBill Paul 
1352ce4946daSBill Paul static void nge_txeof(sc)
1353ce4946daSBill Paul 	struct nge_softc	*sc;
1354ce4946daSBill Paul {
1355ce4946daSBill Paul 	struct nge_desc		*cur_tx = NULL;
1356ce4946daSBill Paul 	struct ifnet		*ifp;
1357ce4946daSBill Paul 	u_int32_t		idx;
1358ce4946daSBill Paul 
1359ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1360ce4946daSBill Paul 
1361ce4946daSBill Paul 	/* Clear the timeout timer. */
1362ce4946daSBill Paul 	ifp->if_timer = 0;
1363ce4946daSBill Paul 
1364ce4946daSBill Paul 	/*
1365ce4946daSBill Paul 	 * Go through our tx list and free mbufs for those
1366ce4946daSBill Paul 	 * frames that have been transmitted.
1367ce4946daSBill Paul 	 */
1368ce4946daSBill Paul 	idx = sc->nge_cdata.nge_tx_cons;
1369ce4946daSBill Paul 	while (idx != sc->nge_cdata.nge_tx_prod) {
1370ce4946daSBill Paul 		cur_tx = &sc->nge_ldata->nge_tx_list[idx];
1371ce4946daSBill Paul 
1372ce4946daSBill Paul 		if (NGE_OWNDESC(cur_tx))
1373ce4946daSBill Paul 			break;
1374ce4946daSBill Paul 
1375ce4946daSBill Paul 		if (cur_tx->nge_ctl & NGE_CMDSTS_MORE) {
1376ce4946daSBill Paul 			sc->nge_cdata.nge_tx_cnt--;
1377ce4946daSBill Paul 			NGE_INC(idx, NGE_TX_LIST_CNT);
1378ce4946daSBill Paul 			continue;
1379ce4946daSBill Paul 		}
1380ce4946daSBill Paul 
1381ce4946daSBill Paul 		if (!(cur_tx->nge_ctl & NGE_CMDSTS_PKT_OK)) {
1382ce4946daSBill Paul 			ifp->if_oerrors++;
1383ce4946daSBill Paul 			if (cur_tx->nge_txstat & NGE_TXSTAT_EXCESSCOLLS)
1384ce4946daSBill Paul 				ifp->if_collisions++;
1385ce4946daSBill Paul 			if (cur_tx->nge_txstat & NGE_TXSTAT_OUTOFWINCOLL)
1386ce4946daSBill Paul 				ifp->if_collisions++;
1387ce4946daSBill Paul 		}
1388ce4946daSBill Paul 
1389ce4946daSBill Paul 		ifp->if_collisions +=
1390ce4946daSBill Paul 		    (cur_tx->nge_txstat & NGE_TXSTAT_COLLCNT) >> 16;
1391ce4946daSBill Paul 
1392ce4946daSBill Paul 		ifp->if_opackets++;
1393ce4946daSBill Paul 		if (cur_tx->nge_mbuf != NULL) {
1394ce4946daSBill Paul 			m_freem(cur_tx->nge_mbuf);
1395ce4946daSBill Paul 			cur_tx->nge_mbuf = NULL;
1396ce4946daSBill Paul 		}
1397ce4946daSBill Paul 
1398ce4946daSBill Paul 		sc->nge_cdata.nge_tx_cnt--;
1399ce4946daSBill Paul 		NGE_INC(idx, NGE_TX_LIST_CNT);
1400ce4946daSBill Paul 		ifp->if_timer = 0;
1401ce4946daSBill Paul 	}
1402ce4946daSBill Paul 
1403ce4946daSBill Paul 	sc->nge_cdata.nge_tx_cons = idx;
1404ce4946daSBill Paul 
1405ce4946daSBill Paul 	if (cur_tx != NULL)
1406ce4946daSBill Paul 		ifp->if_flags &= ~IFF_OACTIVE;
1407ce4946daSBill Paul 
1408ce4946daSBill Paul 	return;
1409ce4946daSBill Paul }
1410ce4946daSBill Paul 
1411ce4946daSBill Paul static void nge_tick(xsc)
1412ce4946daSBill Paul 	void			*xsc;
1413ce4946daSBill Paul {
1414ce4946daSBill Paul 	struct nge_softc	*sc;
1415ce4946daSBill Paul 	struct mii_data		*mii;
1416ce4946daSBill Paul 	struct ifnet		*ifp;
1417ce4946daSBill Paul 	int			s;
1418ce4946daSBill Paul 
1419ce4946daSBill Paul 	s = splimp();
1420ce4946daSBill Paul 
1421ce4946daSBill Paul 	sc = xsc;
1422ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1423ce4946daSBill Paul 
1424ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
1425ce4946daSBill Paul 	mii_tick(mii);
1426ce4946daSBill Paul 
1427ce4946daSBill Paul 	if (!sc->nge_link) {
1428ce4946daSBill Paul 		mii_pollstat(mii);
1429ce4946daSBill Paul 		if (mii->mii_media_status & IFM_ACTIVE &&
1430ce4946daSBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1431ce4946daSBill Paul 			sc->nge_link++;
1432ce4946daSBill Paul 			if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_TX)
1433ce4946daSBill Paul 				printf("nge%d: gigabit link up\n",
1434ce4946daSBill Paul 				    sc->nge_unit);
1435ce4946daSBill Paul 			if (ifp->if_snd.ifq_head != NULL)
1436ce4946daSBill Paul 				nge_start(ifp);
1437ce4946daSBill Paul 		} else
1438ce4946daSBill Paul 			sc->nge_stat_ch = timeout(nge_tick, sc, hz);
1439ce4946daSBill Paul 	}
1440ce4946daSBill Paul 
1441ce4946daSBill Paul 
1442ce4946daSBill Paul 	splx(s);
1443ce4946daSBill Paul 
1444ce4946daSBill Paul 	return;
1445ce4946daSBill Paul }
1446ce4946daSBill Paul 
1447ce4946daSBill Paul static void nge_intr(arg)
1448ce4946daSBill Paul 	void			*arg;
1449ce4946daSBill Paul {
1450ce4946daSBill Paul 	struct nge_softc	*sc;
1451ce4946daSBill Paul 	struct ifnet		*ifp;
1452ce4946daSBill Paul 	u_int32_t		status;
1453ce4946daSBill Paul 
1454ce4946daSBill Paul 	sc = arg;
1455ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1456ce4946daSBill Paul 
1457ce4946daSBill Paul 	/* Supress unwanted interrupts */
1458ce4946daSBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
1459ce4946daSBill Paul 		nge_stop(sc);
1460ce4946daSBill Paul 		return;
1461ce4946daSBill Paul 	}
1462ce4946daSBill Paul 
1463ce4946daSBill Paul 	/* Disable interrupts. */
1464ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
1465ce4946daSBill Paul 
1466ce4946daSBill Paul 	for (;;) {
1467ce4946daSBill Paul 		/* Reading the ISR register clears all interrupts. */
1468ce4946daSBill Paul 		status = CSR_READ_4(sc, NGE_ISR);
1469ce4946daSBill Paul 
1470ce4946daSBill Paul 		if ((status & NGE_INTRS) == 0)
1471ce4946daSBill Paul 			break;
1472ce4946daSBill Paul 
1473ce4946daSBill Paul 		if ((status & NGE_ISR_TX_DESC_OK) ||
1474ce4946daSBill Paul 		    (status & NGE_ISR_TX_ERR) ||
1475ce4946daSBill Paul 		    (status & NGE_ISR_TX_OK) ||
1476ce4946daSBill Paul 		    (status & NGE_ISR_TX_IDLE))
1477ce4946daSBill Paul 			nge_txeof(sc);
1478ce4946daSBill Paul 
1479ce4946daSBill Paul 		if ((status & NGE_ISR_RX_DESC_OK) ||
1480ce4946daSBill Paul 		    (status & NGE_ISR_RX_OK))
1481ce4946daSBill Paul 			nge_rxeof(sc);
1482ce4946daSBill Paul 
1483ce4946daSBill Paul 		if ((status & NGE_ISR_RX_ERR) ||
1484ce4946daSBill Paul 		    (status & NGE_ISR_RX_OFLOW)) {
1485ce4946daSBill Paul 			nge_rxeoc(sc);
1486ce4946daSBill Paul 		}
1487ce4946daSBill Paul 
1488ce4946daSBill Paul 		if (status & NGE_ISR_SYSERR) {
1489ce4946daSBill Paul 			nge_reset(sc);
1490ce4946daSBill Paul 			ifp->if_flags &= ~IFF_RUNNING;
1491ce4946daSBill Paul 			nge_init(sc);
1492ce4946daSBill Paul 		}
1493ce4946daSBill Paul 
1494ce4946daSBill Paul 		if (status & NGE_IMR_PHY_INTR) {
1495ce4946daSBill Paul 			sc->nge_link = 0;
1496ce4946daSBill Paul 			nge_tick(sc);
1497ce4946daSBill Paul 		}
1498ce4946daSBill Paul 	}
1499ce4946daSBill Paul 
1500ce4946daSBill Paul 	/* Re-enable interrupts. */
1501ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1502ce4946daSBill Paul 
1503ce4946daSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1504ce4946daSBill Paul 		nge_start(ifp);
1505ce4946daSBill Paul 
1506ce4946daSBill Paul 	return;
1507ce4946daSBill Paul }
1508ce4946daSBill Paul 
1509ce4946daSBill Paul /*
1510ce4946daSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1511ce4946daSBill Paul  * pointers to the fragment pointers.
1512ce4946daSBill Paul  */
1513ce4946daSBill Paul static int nge_encap(sc, m_head, txidx)
1514ce4946daSBill Paul 	struct nge_softc	*sc;
1515ce4946daSBill Paul 	struct mbuf		*m_head;
1516ce4946daSBill Paul 	u_int32_t		*txidx;
1517ce4946daSBill Paul {
1518ce4946daSBill Paul 	struct nge_desc		*f = NULL;
1519ce4946daSBill Paul 	struct mbuf		*m;
1520ce4946daSBill Paul 	int			frag, cur, cnt = 0;
1521ce4946daSBill Paul #if NVLAN > 0
1522ce4946daSBill Paul 	struct ifvlan		*ifv = NULL;
1523ce4946daSBill Paul 
1524ce4946daSBill Paul 	if ((m_head->m_flags & (M_PROTO1|M_PKTHDR)) == (M_PROTO1|M_PKTHDR) &&
1525ce4946daSBill Paul 	    m_head->m_pkthdr.rcvif != NULL &&
1526ce4946daSBill Paul 	    m_head->m_pkthdr.rcvif->if_type == IFT_8021_VLAN)
1527ce4946daSBill Paul 		ifv = m_head->m_pkthdr.rcvif->if_softc;
1528ce4946daSBill Paul #endif
1529ce4946daSBill Paul 
1530ce4946daSBill Paul 	/*
1531ce4946daSBill Paul  	 * Start packing the mbufs in this chain into
1532ce4946daSBill Paul 	 * the fragment pointers. Stop when we run out
1533ce4946daSBill Paul  	 * of fragments or hit the end of the mbuf chain.
1534ce4946daSBill Paul 	 */
1535ce4946daSBill Paul 	m = m_head;
1536ce4946daSBill Paul 	cur = frag = *txidx;
1537ce4946daSBill Paul 
1538ce4946daSBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
1539ce4946daSBill Paul 		if (m->m_len != 0) {
1540ce4946daSBill Paul 			if ((NGE_TX_LIST_CNT -
1541ce4946daSBill Paul 			    (sc->nge_cdata.nge_tx_cnt + cnt)) < 2)
1542ce4946daSBill Paul 				return(ENOBUFS);
1543ce4946daSBill Paul 			f = &sc->nge_ldata->nge_tx_list[frag];
1544ce4946daSBill Paul 			f->nge_ctl = NGE_CMDSTS_MORE | m->m_len;
1545ce4946daSBill Paul 			f->nge_ptr = vtophys(mtod(m, vm_offset_t));
1546ce4946daSBill Paul 			if (cnt != 0)
1547ce4946daSBill Paul 				f->nge_ctl |= NGE_CMDSTS_OWN;
1548ce4946daSBill Paul 			cur = frag;
1549ce4946daSBill Paul 			NGE_INC(frag, NGE_TX_LIST_CNT);
1550ce4946daSBill Paul 			cnt++;
1551ce4946daSBill Paul 		}
1552ce4946daSBill Paul 	}
1553ce4946daSBill Paul 
1554ce4946daSBill Paul 	if (m != NULL)
1555ce4946daSBill Paul 		return(ENOBUFS);
1556ce4946daSBill Paul 
15571bacd83aSBill Paul 	sc->nge_ldata->nge_tx_list[*txidx].nge_extsts = 0;
1558ce4946daSBill Paul 	if (m_head->m_pkthdr.csum_flags) {
1559ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
15601bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1561ce4946daSBill Paul 			    NGE_TXEXTSTS_IPCSUM;
1562ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
15631bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1564ce4946daSBill Paul 			    NGE_TXEXTSTS_TCPCSUM;
1565ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
15661bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1567ce4946daSBill Paul 			    NGE_TXEXTSTS_UDPCSUM;
1568ce4946daSBill Paul 	}
1569ce4946daSBill Paul 
1570ce4946daSBill Paul #if NVLAN > 0
1571ce4946daSBill Paul 	if (ifv != NULL) {
1572ce4946daSBill Paul 		sc->nge_ldata->nge_tx_list[cur].nge_extsts |=
1573ce4946daSBill Paul 			(NGE_TXEXTSTS_VLANPKT|ifv->ifv_tag);
1574ce4946daSBill Paul 	}
1575ce4946daSBill Paul #endif
1576ce4946daSBill Paul 
1577ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[cur].nge_mbuf = m_head;
1578ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[cur].nge_ctl &= ~NGE_CMDSTS_MORE;
1579ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[*txidx].nge_ctl |= NGE_CMDSTS_OWN;
1580ce4946daSBill Paul 	sc->nge_cdata.nge_tx_cnt += cnt;
1581ce4946daSBill Paul 	*txidx = frag;
1582ce4946daSBill Paul 
1583ce4946daSBill Paul 	return(0);
1584ce4946daSBill Paul }
1585ce4946daSBill Paul 
1586ce4946daSBill Paul /*
1587ce4946daSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1588ce4946daSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1589ce4946daSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1590ce4946daSBill Paul  * physical addresses.
1591ce4946daSBill Paul  */
1592ce4946daSBill Paul 
1593ce4946daSBill Paul static void nge_start(ifp)
1594ce4946daSBill Paul 	struct ifnet		*ifp;
1595ce4946daSBill Paul {
1596ce4946daSBill Paul 	struct nge_softc	*sc;
1597ce4946daSBill Paul 	struct mbuf		*m_head = NULL;
1598ce4946daSBill Paul 	u_int32_t		idx;
1599ce4946daSBill Paul 
1600ce4946daSBill Paul 	sc = ifp->if_softc;
1601ce4946daSBill Paul 
1602ce4946daSBill Paul 	if (!sc->nge_link)
1603ce4946daSBill Paul 		return;
1604ce4946daSBill Paul 
1605ce4946daSBill Paul 	idx = sc->nge_cdata.nge_tx_prod;
1606ce4946daSBill Paul 
1607ce4946daSBill Paul 	if (ifp->if_flags & IFF_OACTIVE)
1608ce4946daSBill Paul 		return;
1609ce4946daSBill Paul 
1610ce4946daSBill Paul 	while(sc->nge_ldata->nge_tx_list[idx].nge_mbuf == NULL) {
1611ce4946daSBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
1612ce4946daSBill Paul 		if (m_head == NULL)
1613ce4946daSBill Paul 			break;
1614ce4946daSBill Paul 
1615ce4946daSBill Paul 		if (nge_encap(sc, m_head, &idx)) {
1616ce4946daSBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
1617ce4946daSBill Paul 			ifp->if_flags |= IFF_OACTIVE;
1618ce4946daSBill Paul 			break;
1619ce4946daSBill Paul 		}
1620ce4946daSBill Paul 
1621ce4946daSBill Paul 		/*
1622ce4946daSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
1623ce4946daSBill Paul 		 * to him.
1624ce4946daSBill Paul 		 */
1625ce4946daSBill Paul 		if (ifp->if_bpf)
1626ce4946daSBill Paul 			bpf_mtap(ifp, m_head);
1627ce4946daSBill Paul 
1628ce4946daSBill Paul 	}
1629ce4946daSBill Paul 
1630ce4946daSBill Paul 	/* Transmit */
1631ce4946daSBill Paul 	sc->nge_cdata.nge_tx_prod = idx;
1632ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE);
1633ce4946daSBill Paul 
1634ce4946daSBill Paul 	/*
1635ce4946daSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
1636ce4946daSBill Paul 	 */
1637ce4946daSBill Paul 	ifp->if_timer = 5;
1638ce4946daSBill Paul 
1639ce4946daSBill Paul 	return;
1640ce4946daSBill Paul }
1641ce4946daSBill Paul 
1642ce4946daSBill Paul static void nge_init(xsc)
1643ce4946daSBill Paul 	void			*xsc;
1644ce4946daSBill Paul {
1645ce4946daSBill Paul 	struct nge_softc	*sc = xsc;
1646ce4946daSBill Paul 	struct ifnet		*ifp = &sc->arpcom.ac_if;
1647ce4946daSBill Paul 	struct mii_data		*mii;
1648ce4946daSBill Paul 	int			s;
1649ce4946daSBill Paul 
1650ce4946daSBill Paul 	if (ifp->if_flags & IFF_RUNNING)
1651ce4946daSBill Paul 		return;
1652ce4946daSBill Paul 
1653ce4946daSBill Paul 	s = splimp();
1654ce4946daSBill Paul 
1655ce4946daSBill Paul 	/*
1656ce4946daSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
1657ce4946daSBill Paul 	 */
1658ce4946daSBill Paul 	nge_stop(sc);
1659ce4946daSBill Paul 
1660ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
1661ce4946daSBill Paul 
1662ce4946daSBill Paul 	/* Set MAC address */
1663ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0);
1664ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1665ce4946daSBill Paul 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[0]);
1666ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1);
1667ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1668ce4946daSBill Paul 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[1]);
1669ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2);
1670ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
1671ce4946daSBill Paul 	    ((u_int16_t *)sc->arpcom.ac_enaddr)[2]);
1672ce4946daSBill Paul 
1673ce4946daSBill Paul 	/* Init circular RX list. */
1674ce4946daSBill Paul 	if (nge_list_rx_init(sc) == ENOBUFS) {
1675ce4946daSBill Paul 		printf("nge%d: initialization failed: no "
1676ce4946daSBill Paul 			"memory for rx buffers\n", sc->nge_unit);
1677ce4946daSBill Paul 		nge_stop(sc);
1678ce4946daSBill Paul 		(void)splx(s);
1679ce4946daSBill Paul 		return;
1680ce4946daSBill Paul 	}
1681ce4946daSBill Paul 
1682ce4946daSBill Paul 	/*
1683ce4946daSBill Paul 	 * Init tx descriptors.
1684ce4946daSBill Paul 	 */
1685ce4946daSBill Paul 	nge_list_tx_init(sc);
1686ce4946daSBill Paul 
1687ce4946daSBill Paul 	/*
1688ce4946daSBill Paul 	 * For the NatSemi chip, we have to explicitly enable the
1689ce4946daSBill Paul 	 * reception of ARP frames, as well as turn on the 'perfect
1690ce4946daSBill Paul 	 * match' filter where we store the station address, otherwise
1691ce4946daSBill Paul 	 * we won't receive unicasts meant for this host.
1692ce4946daSBill Paul 	 */
1693ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP);
1694ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT);
1695ce4946daSBill Paul 
1696ce4946daSBill Paul 	 /* If we want promiscuous mode, set the allframes bit. */
1697ce4946daSBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
1698ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1699ce4946daSBill Paul 	} else {
1700ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1701ce4946daSBill Paul 	}
1702ce4946daSBill Paul 
1703ce4946daSBill Paul 	/*
1704ce4946daSBill Paul 	 * Set the capture broadcast bit to capture broadcast frames.
1705ce4946daSBill Paul 	 */
1706ce4946daSBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1707ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1708ce4946daSBill Paul 	} else {
1709ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1710ce4946daSBill Paul 	}
1711ce4946daSBill Paul 
1712ce4946daSBill Paul 	/*
1713ce4946daSBill Paul 	 * Load the multicast filter.
1714ce4946daSBill Paul 	 */
1715ce4946daSBill Paul 	nge_setmulti(sc);
1716ce4946daSBill Paul 
1717ce4946daSBill Paul 	/* Turn the receive filter on */
1718ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE);
1719ce4946daSBill Paul 
1720ce4946daSBill Paul 	/*
1721ce4946daSBill Paul 	 * Load the address of the RX and TX lists.
1722ce4946daSBill Paul 	 */
1723ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_LISTPTR,
1724ce4946daSBill Paul 	    vtophys(&sc->nge_ldata->nge_rx_list[0]));
1725ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_LISTPTR,
1726ce4946daSBill Paul 	    vtophys(&sc->nge_ldata->nge_tx_list[0]));
1727ce4946daSBill Paul 
1728ce4946daSBill Paul 	/* Set RX configuration */
1729ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG);
1730ce4946daSBill Paul 	/*
1731ce4946daSBill Paul 	 * Enable hardware checksum validation for all IPv4
1732ce4946daSBill Paul 	 * packets, do not reject packets with bad checksums.
1733ce4946daSBill Paul 	 */
1734ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB);
1735ce4946daSBill Paul 
1736ce4946daSBill Paul #if NVLAN > 0
1737ce4946daSBill Paul 	/*
1738ce4946daSBill Paul 	 * If VLAN support is enabled, tell the chip to detect
1739ce4946daSBill Paul 	 * and strip VLAN tag info from received frames. The tag
1740ce4946daSBill Paul 	 * will be provided in the extsts field in the RX descriptors.
1741ce4946daSBill Paul 	 */
1742ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL,
1743ce4946daSBill Paul 	    NGE_VIPRXCTL_TAG_DETECT_ENB|NGE_VIPRXCTL_TAG_STRIP_ENB);
1744ce4946daSBill Paul #endif
1745ce4946daSBill Paul 
1746ce4946daSBill Paul 	/* Set TX configuration */
1747ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG);
1748ce4946daSBill Paul 
1749ce4946daSBill Paul 	/*
1750ce4946daSBill Paul 	 * Enable TX IPv4 checksumming on a per-packet basis.
1751ce4946daSBill Paul 	 */
1752ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT);
1753ce4946daSBill Paul 
1754ce4946daSBill Paul #if NVLAN > 0
1755ce4946daSBill Paul 	/*
1756ce4946daSBill Paul 	 * If VLAN support is enabled, tell the chip to insert
1757ce4946daSBill Paul 	 * VLAN tags on a per-packet basis as dictated by the
1758ce4946daSBill Paul 	 * code in the frame encapsulation routine.
1759ce4946daSBill Paul 	 */
1760ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT);
1761ce4946daSBill Paul #endif
1762ce4946daSBill Paul 
1763ce4946daSBill Paul 	/* Set full/half duplex mode. */
1764ce4946daSBill Paul 	if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1765ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_TX_CFG,
1766ce4946daSBill Paul 		    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1767ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1768ce4946daSBill Paul 	} else {
1769ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_TX_CFG,
1770ce4946daSBill Paul 		    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1771ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1772ce4946daSBill Paul 	}
1773ce4946daSBill Paul 
1774ce4946daSBill Paul 	/*
1775ce4946daSBill Paul 	 * Enable the delivery of PHY interrupts based on
177623d3a203SBill Paul 	 * link/speed/duplex status changes. Also enable the
177723d3a203SBill Paul 	 * extsts field in the DMA descriptors (needed for
177823d3a203SBill Paul 	 * TCP/IP checksum offload on transmit).
1779ce4946daSBill Paul 	 */
1780ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD|NGE_CFG_MODE_1000|
178123d3a203SBill Paul 	    NGE_CFG_PHYINTR_LNK|NGE_CFG_PHYINTR_DUP|NGE_CFG_EXTSTS_ENB);
1782ce4946daSBill Paul 
1783ce4946daSBill Paul 	/*
1784ce4946daSBill Paul 	 * Enable interrupts.
1785ce4946daSBill Paul 	 */
1786ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS);
1787ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1788ce4946daSBill Paul 
1789ce4946daSBill Paul 	/* Enable receiver and transmitter. */
1790ce4946daSBill Paul 	NGE_CLRBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
1791ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1792ce4946daSBill Paul 
1793ce4946daSBill Paul 	nge_ifmedia_upd(ifp);
1794ce4946daSBill Paul 
1795ce4946daSBill Paul 	ifp->if_flags |= IFF_RUNNING;
1796ce4946daSBill Paul 	ifp->if_flags &= ~IFF_OACTIVE;
1797ce4946daSBill Paul 
1798ce4946daSBill Paul 	(void)splx(s);
1799ce4946daSBill Paul 
1800ce4946daSBill Paul 	return;
1801ce4946daSBill Paul }
1802ce4946daSBill Paul 
1803ce4946daSBill Paul /*
1804ce4946daSBill Paul  * Set media options.
1805ce4946daSBill Paul  */
1806ce4946daSBill Paul static int nge_ifmedia_upd(ifp)
1807ce4946daSBill Paul 	struct ifnet		*ifp;
1808ce4946daSBill Paul {
1809ce4946daSBill Paul 	struct nge_softc	*sc;
1810ce4946daSBill Paul 	struct mii_data		*mii;
1811ce4946daSBill Paul 
1812ce4946daSBill Paul 	sc = ifp->if_softc;
1813ce4946daSBill Paul 
1814ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
1815ce4946daSBill Paul 	sc->nge_link = 0;
1816ce4946daSBill Paul 	if (mii->mii_instance) {
1817ce4946daSBill Paul 		struct mii_softc	*miisc;
1818ce4946daSBill Paul 		for (miisc = LIST_FIRST(&mii->mii_phys); miisc != NULL;
1819ce4946daSBill Paul 		    miisc = LIST_NEXT(miisc, mii_list))
1820ce4946daSBill Paul 			mii_phy_reset(miisc);
1821ce4946daSBill Paul 	}
1822ce4946daSBill Paul 	mii_mediachg(mii);
1823ce4946daSBill Paul 
1824ce4946daSBill Paul 	return(0);
1825ce4946daSBill Paul }
1826ce4946daSBill Paul 
1827ce4946daSBill Paul /*
1828ce4946daSBill Paul  * Report current media status.
1829ce4946daSBill Paul  */
1830ce4946daSBill Paul static void nge_ifmedia_sts(ifp, ifmr)
1831ce4946daSBill Paul 	struct ifnet		*ifp;
1832ce4946daSBill Paul 	struct ifmediareq	*ifmr;
1833ce4946daSBill Paul {
1834ce4946daSBill Paul 	struct nge_softc	*sc;
1835ce4946daSBill Paul 	struct mii_data		*mii;
1836ce4946daSBill Paul 
1837ce4946daSBill Paul 	sc = ifp->if_softc;
1838ce4946daSBill Paul 
1839ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
1840ce4946daSBill Paul 	mii_pollstat(mii);
1841ce4946daSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
1842ce4946daSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
1843ce4946daSBill Paul 
1844ce4946daSBill Paul 	return;
1845ce4946daSBill Paul }
1846ce4946daSBill Paul 
1847ce4946daSBill Paul static int nge_ioctl(ifp, command, data)
1848ce4946daSBill Paul 	struct ifnet		*ifp;
1849ce4946daSBill Paul 	u_long			command;
1850ce4946daSBill Paul 	caddr_t			data;
1851ce4946daSBill Paul {
1852ce4946daSBill Paul 	struct nge_softc	*sc = ifp->if_softc;
1853ce4946daSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
1854ce4946daSBill Paul 	struct mii_data		*mii;
1855ce4946daSBill Paul 	int			s, error = 0;
1856ce4946daSBill Paul 
1857ce4946daSBill Paul 	s = splimp();
1858ce4946daSBill Paul 
1859ce4946daSBill Paul 	switch(command) {
1860ce4946daSBill Paul 	case SIOCSIFADDR:
1861ce4946daSBill Paul 	case SIOCGIFADDR:
1862ce4946daSBill Paul 		error = ether_ioctl(ifp, command, data);
1863ce4946daSBill Paul 		break;
1864ce4946daSBill Paul 	case SIOCSIFMTU:
1865ce4946daSBill Paul 		if (ifr->ifr_mtu > NGE_JUMBO_MTU)
1866ce4946daSBill Paul 			error = EINVAL;
1867cb2f755cSBill Paul 		else {
1868ce4946daSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
1869cb2f755cSBill Paul 			/*
1870cb2f755cSBill Paul 			 * Workaround: if the MTU is larger than
1871cb2f755cSBill Paul 			 * 8152 (TX FIFO size minus 64 minus 18), turn off
1872cb2f755cSBill Paul 			 * TX checksum offloading.
1873cb2f755cSBill Paul 			 */
1874cb2f755cSBill Paul 			if (ifr->ifr_mtu == 8152)
1875cb2f755cSBill Paul 				ifp->if_hwassist = 0;
1876cb2f755cSBill Paul 			else
1877cb2f755cSBill Paul 				ifp->if_hwassist = NGE_CSUM_FEATURES;
1878cb2f755cSBill Paul 		}
1879ce4946daSBill Paul 		break;
1880ce4946daSBill Paul 	case SIOCSIFFLAGS:
1881ce4946daSBill Paul 		if (ifp->if_flags & IFF_UP) {
1882ce4946daSBill Paul 			if (ifp->if_flags & IFF_RUNNING &&
1883ce4946daSBill Paul 			    ifp->if_flags & IFF_PROMISC &&
1884ce4946daSBill Paul 			    !(sc->nge_if_flags & IFF_PROMISC)) {
1885ce4946daSBill Paul 				NGE_SETBIT(sc, NGE_RXFILT_CTL,
1886ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLPHYS|
1887ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLMULTI);
1888ce4946daSBill Paul 			} else if (ifp->if_flags & IFF_RUNNING &&
1889ce4946daSBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
1890ce4946daSBill Paul 			    sc->nge_if_flags & IFF_PROMISC) {
1891ce4946daSBill Paul 				NGE_CLRBIT(sc, NGE_RXFILT_CTL,
1892ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLPHYS);
1893ce4946daSBill Paul 				if (!(ifp->if_flags & IFF_ALLMULTI))
1894ce4946daSBill Paul 					NGE_CLRBIT(sc, NGE_RXFILT_CTL,
1895ce4946daSBill Paul 					    NGE_RXFILTCTL_ALLMULTI);
1896ce4946daSBill Paul 			} else {
1897ce4946daSBill Paul 				ifp->if_flags &= ~IFF_RUNNING;
1898ce4946daSBill Paul 				nge_init(sc);
1899ce4946daSBill Paul 			}
1900ce4946daSBill Paul 		} else {
1901ce4946daSBill Paul 			if (ifp->if_flags & IFF_RUNNING)
1902ce4946daSBill Paul 				nge_stop(sc);
1903ce4946daSBill Paul 		}
1904ce4946daSBill Paul 		sc->nge_if_flags = ifp->if_flags;
1905ce4946daSBill Paul 		error = 0;
1906ce4946daSBill Paul 		break;
1907ce4946daSBill Paul 	case SIOCADDMULTI:
1908ce4946daSBill Paul 	case SIOCDELMULTI:
1909ce4946daSBill Paul 		nge_setmulti(sc);
1910ce4946daSBill Paul 		error = 0;
1911ce4946daSBill Paul 		break;
1912ce4946daSBill Paul 	case SIOCGIFMEDIA:
1913ce4946daSBill Paul 	case SIOCSIFMEDIA:
1914ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
1915ce4946daSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
1916ce4946daSBill Paul 		break;
1917ce4946daSBill Paul 	default:
1918ce4946daSBill Paul 		error = EINVAL;
1919ce4946daSBill Paul 		break;
1920ce4946daSBill Paul 	}
1921ce4946daSBill Paul 
1922ce4946daSBill Paul 	(void)splx(s);
1923ce4946daSBill Paul 
1924ce4946daSBill Paul 	return(error);
1925ce4946daSBill Paul }
1926ce4946daSBill Paul 
1927ce4946daSBill Paul static void nge_watchdog(ifp)
1928ce4946daSBill Paul 	struct ifnet		*ifp;
1929ce4946daSBill Paul {
1930ce4946daSBill Paul 	struct nge_softc	*sc;
1931ce4946daSBill Paul 
1932ce4946daSBill Paul 	sc = ifp->if_softc;
1933ce4946daSBill Paul 
1934ce4946daSBill Paul 	ifp->if_oerrors++;
1935ce4946daSBill Paul 	printf("nge%d: watchdog timeout\n", sc->nge_unit);
1936ce4946daSBill Paul 
1937ce4946daSBill Paul 	nge_stop(sc);
1938ce4946daSBill Paul 	nge_reset(sc);
1939ce4946daSBill Paul 	ifp->if_flags &= ~IFF_RUNNING;
1940ce4946daSBill Paul 	nge_init(sc);
1941ce4946daSBill Paul 
1942ce4946daSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1943ce4946daSBill Paul 		nge_start(ifp);
1944ce4946daSBill Paul 
1945ce4946daSBill Paul 	return;
1946ce4946daSBill Paul }
1947ce4946daSBill Paul 
1948ce4946daSBill Paul /*
1949ce4946daSBill Paul  * Stop the adapter and free any mbufs allocated to the
1950ce4946daSBill Paul  * RX and TX lists.
1951ce4946daSBill Paul  */
1952ce4946daSBill Paul static void nge_stop(sc)
1953ce4946daSBill Paul 	struct nge_softc	*sc;
1954ce4946daSBill Paul {
1955ce4946daSBill Paul 	register int		i;
1956ce4946daSBill Paul 	struct ifnet		*ifp;
1957ce4946daSBill Paul 	struct ifmedia_entry	*ifm;
1958ce4946daSBill Paul 	struct mii_data		*mii;
1959ce4946daSBill Paul 	int			mtmp, itmp;
1960ce4946daSBill Paul 
1961ce4946daSBill Paul 	ifp = &sc->arpcom.ac_if;
1962ce4946daSBill Paul 	ifp->if_timer = 0;
1963ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
1964ce4946daSBill Paul 
1965ce4946daSBill Paul 	untimeout(nge_tick, sc, sc->nge_stat_ch);
1966ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
1967ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, 0);
1968ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
1969ce4946daSBill Paul 	DELAY(1000);
1970ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_LISTPTR, 0);
1971ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_LISTPTR, 0);
1972ce4946daSBill Paul 
1973ce4946daSBill Paul 	/*
1974ce4946daSBill Paul 	 * Isolate/power down the PHY, but leave the media selection
1975ce4946daSBill Paul 	 * unchanged so that things will be put back to normal when
1976ce4946daSBill Paul 	 * we bring the interface back up.
1977ce4946daSBill Paul 	 */
1978ce4946daSBill Paul 	itmp = ifp->if_flags;
1979ce4946daSBill Paul 	ifp->if_flags |= IFF_UP;
1980ce4946daSBill Paul 	ifm = mii->mii_media.ifm_cur;
1981ce4946daSBill Paul 	mtmp = ifm->ifm_media;
1982ce4946daSBill Paul 	ifm->ifm_media = IFM_ETHER|IFM_NONE;
1983ce4946daSBill Paul 	mii_mediachg(mii);
1984ce4946daSBill Paul 	ifm->ifm_media = mtmp;
1985ce4946daSBill Paul 	ifp->if_flags = itmp;
1986ce4946daSBill Paul 
1987ce4946daSBill Paul 	sc->nge_link = 0;
1988ce4946daSBill Paul 
1989ce4946daSBill Paul 	/*
1990ce4946daSBill Paul 	 * Free data in the RX lists.
1991ce4946daSBill Paul 	 */
1992ce4946daSBill Paul 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
1993ce4946daSBill Paul 		if (sc->nge_ldata->nge_rx_list[i].nge_mbuf != NULL) {
1994ce4946daSBill Paul 			m_freem(sc->nge_ldata->nge_rx_list[i].nge_mbuf);
1995ce4946daSBill Paul 			sc->nge_ldata->nge_rx_list[i].nge_mbuf = NULL;
1996ce4946daSBill Paul 		}
1997ce4946daSBill Paul 	}
1998ce4946daSBill Paul 	bzero((char *)&sc->nge_ldata->nge_rx_list,
1999ce4946daSBill Paul 		sizeof(sc->nge_ldata->nge_rx_list));
2000ce4946daSBill Paul 
2001ce4946daSBill Paul 	/*
2002ce4946daSBill Paul 	 * Free the TX list buffers.
2003ce4946daSBill Paul 	 */
2004ce4946daSBill Paul 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
2005ce4946daSBill Paul 		if (sc->nge_ldata->nge_tx_list[i].nge_mbuf != NULL) {
2006ce4946daSBill Paul 			m_freem(sc->nge_ldata->nge_tx_list[i].nge_mbuf);
2007ce4946daSBill Paul 			sc->nge_ldata->nge_tx_list[i].nge_mbuf = NULL;
2008ce4946daSBill Paul 		}
2009ce4946daSBill Paul 	}
2010ce4946daSBill Paul 
2011ce4946daSBill Paul 	bzero((char *)&sc->nge_ldata->nge_tx_list,
2012ce4946daSBill Paul 		sizeof(sc->nge_ldata->nge_tx_list));
2013ce4946daSBill Paul 
2014ce4946daSBill Paul 	ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2015ce4946daSBill Paul 
2016ce4946daSBill Paul 	return;
2017ce4946daSBill Paul }
2018ce4946daSBill Paul 
2019ce4946daSBill Paul /*
2020ce4946daSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2021ce4946daSBill Paul  * get confused by errant DMAs when rebooting.
2022ce4946daSBill Paul  */
2023ce4946daSBill Paul static void nge_shutdown(dev)
2024ce4946daSBill Paul 	device_t		dev;
2025ce4946daSBill Paul {
2026ce4946daSBill Paul 	struct nge_softc	*sc;
2027ce4946daSBill Paul 
2028ce4946daSBill Paul 	sc = device_get_softc(dev);
2029ce4946daSBill Paul 
2030ce4946daSBill Paul 	nge_reset(sc);
2031ce4946daSBill Paul 	nge_stop(sc);
2032ce4946daSBill Paul 
2033ce4946daSBill Paul 	return;
2034ce4946daSBill Paul }
2035