xref: /freebsd/sys/dev/nge/if_nge.c (revision 284c81cbcb4237c4e0c621b18c1500d88a1eaee4)
1098ca2bdSWarner Losh /*-
2ce4946daSBill Paul  * Copyright (c) 2001 Wind River Systems
3ce4946daSBill Paul  * Copyright (c) 1997, 1998, 1999, 2000, 2001
4ce4946daSBill Paul  *	Bill Paul <wpaul@bsdi.com>.  All rights reserved.
5ce4946daSBill Paul  *
6ce4946daSBill Paul  * Redistribution and use in source and binary forms, with or without
7ce4946daSBill Paul  * modification, are permitted provided that the following conditions
8ce4946daSBill Paul  * are met:
9ce4946daSBill Paul  * 1. Redistributions of source code must retain the above copyright
10ce4946daSBill Paul  *    notice, this list of conditions and the following disclaimer.
11ce4946daSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
12ce4946daSBill Paul  *    notice, this list of conditions and the following disclaimer in the
13ce4946daSBill Paul  *    documentation and/or other materials provided with the distribution.
14ce4946daSBill Paul  * 3. All advertising materials mentioning features or use of this software
15ce4946daSBill Paul  *    must display the following acknowledgement:
16ce4946daSBill Paul  *	This product includes software developed by Bill Paul.
17ce4946daSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
18ce4946daSBill Paul  *    may be used to endorse or promote products derived from this software
19ce4946daSBill Paul  *    without specific prior written permission.
20ce4946daSBill Paul  *
21ce4946daSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
22ce4946daSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23ce4946daSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24ce4946daSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
25ce4946daSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26ce4946daSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27ce4946daSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28ce4946daSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29ce4946daSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30ce4946daSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31ce4946daSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
32ce4946daSBill Paul  */
33ce4946daSBill Paul 
34aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
35aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
36aad970f1SDavid E. O'Brien 
37ce4946daSBill Paul /*
38ce4946daSBill Paul  * National Semiconductor DP83820/DP83821 gigabit ethernet driver
39ce4946daSBill Paul  * for FreeBSD. Datasheets are available from:
40ce4946daSBill Paul  *
41ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83820.pdf
42ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83821.pdf
43ce4946daSBill Paul  *
44ce4946daSBill Paul  * These chips are used on several low cost gigabit ethernet NICs
45ce4946daSBill Paul  * sold by D-Link, Addtron, SMC and Asante. Both parts are
46ce4946daSBill Paul  * virtually the same, except the 83820 is a 64-bit/32-bit part,
47ce4946daSBill Paul  * while the 83821 is 32-bit only.
48ce4946daSBill Paul  *
49ce4946daSBill Paul  * Many cards also use National gigE transceivers, such as the
50ce4946daSBill Paul  * DP83891, DP83861 and DP83862 gigPHYTER parts. The DP83861 datasheet
51ce4946daSBill Paul  * contains a full register description that applies to all of these
52ce4946daSBill Paul  * components:
53ce4946daSBill Paul  *
54ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83861.pdf
55ce4946daSBill Paul  *
56ce4946daSBill Paul  * Written by Bill Paul <wpaul@bsdi.com>
57ce4946daSBill Paul  * BSDi Open Source Solutions
58ce4946daSBill Paul  */
59ce4946daSBill Paul 
60ce4946daSBill Paul /*
61ce4946daSBill Paul  * The NatSemi DP83820 and 83821 controllers are enhanced versions
62ce4946daSBill Paul  * of the NatSemi MacPHYTER 10/100 devices. They support 10, 100
63ce4946daSBill Paul  * and 1000Mbps speeds with 1000baseX (ten bit interface), MII and GMII
64ce4946daSBill Paul  * ports. Other features include 8K TX FIFO and 32K RX FIFO, TCP/IP
65ce4946daSBill Paul  * hardware checksum offload (IPv4 only), VLAN tagging and filtering,
66ce4946daSBill Paul  * priority TX and RX queues, a 2048 bit multicast hash filter, 4 RX pattern
67ce4946daSBill Paul  * matching buffers, one perfect address filter buffer and interrupt
68ce4946daSBill Paul  * moderation. The 83820 supports both 64-bit and 32-bit addressing
69ce4946daSBill Paul  * and data transfers: the 64-bit support can be toggled on or off
70ce4946daSBill Paul  * via software. This affects the size of certain fields in the DMA
71ce4946daSBill Paul  * descriptors.
72ce4946daSBill Paul  *
73cb2f755cSBill Paul  * There are two bugs/misfeatures in the 83820/83821 that I have
74cb2f755cSBill Paul  * discovered so far:
75cb2f755cSBill Paul  *
76cb2f755cSBill Paul  * - Receive buffers must be aligned on 64-bit boundaries, which means
77cb2f755cSBill Paul  *   you must resort to copying data in order to fix up the payload
78cb2f755cSBill Paul  *   alignment.
79cb2f755cSBill Paul  *
80cb2f755cSBill Paul  * - In order to transmit jumbo frames larger than 8170 bytes, you have
81cb2f755cSBill Paul  *   to turn off transmit checksum offloading, because the chip can't
82cb2f755cSBill Paul  *   compute the checksum on an outgoing frame unless it fits entirely
83cb2f755cSBill Paul  *   within the TX FIFO, which is only 8192 bytes in size. If you have
84cb2f755cSBill Paul  *   TX checksum offload enabled and you transmit attempt to transmit a
85cb2f755cSBill Paul  *   frame larger than 8170 bytes, the transmitter will wedge.
86cb2f755cSBill Paul  *
87cb2f755cSBill Paul  * To work around the latter problem, TX checksum offload is disabled
88cb2f755cSBill Paul  * if the user selects an MTU larger than 8152 (8170 - 18).
89ce4946daSBill Paul  */
90ce4946daSBill Paul 
91f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
92f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
93f0796cd2SGleb Smirnoff #endif
94f0796cd2SGleb Smirnoff 
95ce4946daSBill Paul #include <sys/param.h>
96ce4946daSBill Paul #include <sys/systm.h>
97ce4946daSBill Paul #include <sys/sockio.h>
98ce4946daSBill Paul #include <sys/mbuf.h>
99ce4946daSBill Paul #include <sys/malloc.h>
100fe12f24bSPoul-Henning Kamp #include <sys/module.h>
101ce4946daSBill Paul #include <sys/kernel.h>
102ce4946daSBill Paul #include <sys/socket.h>
103ce4946daSBill Paul 
104ce4946daSBill Paul #include <net/if.h>
105ce4946daSBill Paul #include <net/if_arp.h>
106ce4946daSBill Paul #include <net/ethernet.h>
107ce4946daSBill Paul #include <net/if_dl.h>
108ce4946daSBill Paul #include <net/if_media.h>
109ce4946daSBill Paul #include <net/if_types.h>
110ce4946daSBill Paul #include <net/if_vlan_var.h>
111ce4946daSBill Paul 
112ce4946daSBill Paul #include <net/bpf.h>
113ce4946daSBill Paul 
114ce4946daSBill Paul #include <vm/vm.h>              /* for vtophys */
115ce4946daSBill Paul #include <vm/pmap.h>            /* for vtophys */
116ce4946daSBill Paul #include <machine/bus.h>
117ce4946daSBill Paul #include <machine/resource.h>
118ce4946daSBill Paul #include <sys/bus.h>
119ce4946daSBill Paul #include <sys/rman.h>
120ce4946daSBill Paul 
121ce4946daSBill Paul #include <dev/mii/mii.h>
122ce4946daSBill Paul #include <dev/mii/miivar.h>
123ce4946daSBill Paul 
12438d8c994SWarner Losh #include <dev/pci/pcireg.h>
12538d8c994SWarner Losh #include <dev/pci/pcivar.h>
126ce4946daSBill Paul 
127ce4946daSBill Paul #define NGE_USEIOSPACE
128ce4946daSBill Paul 
1295da751e4SBill Paul #include <dev/nge/if_ngereg.h>
130ce4946daSBill Paul 
131f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, pci, 1, 1, 1);
132f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, ether, 1, 1, 1);
133ce4946daSBill Paul MODULE_DEPEND(nge, miibus, 1, 1, 1);
134ce4946daSBill Paul 
1357b279558SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
136ce4946daSBill Paul #include "miibus_if.h"
137ce4946daSBill Paul 
138ce4946daSBill Paul #define NGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
139ce4946daSBill Paul 
140ce4946daSBill Paul /*
141ce4946daSBill Paul  * Various supported device vendors/types and their names.
142ce4946daSBill Paul  */
143ce4946daSBill Paul static struct nge_type nge_devs[] = {
144ce4946daSBill Paul 	{ NGE_VENDORID, NGE_DEVICEID,
145ce4946daSBill Paul 	    "National Semiconductor Gigabit Ethernet" },
146ce4946daSBill Paul 	{ 0, 0, NULL }
147ce4946daSBill Paul };
148ce4946daSBill Paul 
149e51a25f8SAlfred Perlstein static int nge_probe(device_t);
150e51a25f8SAlfred Perlstein static int nge_attach(device_t);
151e51a25f8SAlfred Perlstein static int nge_detach(device_t);
152ce4946daSBill Paul 
153eaabec55SAlfred Perlstein static int nge_newbuf(struct nge_softc *, struct nge_desc *, struct mbuf *);
154eaabec55SAlfred Perlstein static int nge_encap(struct nge_softc *, struct mbuf *, u_int32_t *);
1555a71a6feSAlan Cox #ifdef NGE_FIXUP_RX
156ad6c618bSBill Paul static __inline void nge_fixup_rx (struct mbuf *);
157ad6c618bSBill Paul #endif
158e51a25f8SAlfred Perlstein static void nge_rxeof(struct nge_softc *);
159e51a25f8SAlfred Perlstein static void nge_txeof(struct nge_softc *);
160e51a25f8SAlfred Perlstein static void nge_intr(void *);
161e51a25f8SAlfred Perlstein static void nge_tick(void *);
162e51a25f8SAlfred Perlstein static void nge_start(struct ifnet *);
163ad6c618bSBill Paul static void nge_start_locked(struct ifnet *);
164e51a25f8SAlfred Perlstein static int nge_ioctl(struct ifnet *, u_long, caddr_t);
165e51a25f8SAlfred Perlstein static void nge_init(void *);
166ad6c618bSBill Paul static void nge_init_locked(struct nge_softc *);
167e51a25f8SAlfred Perlstein static void nge_stop(struct nge_softc *);
168e51a25f8SAlfred Perlstein static void nge_watchdog(struct ifnet *);
1696a087a87SPyun YongHyeon static int nge_shutdown(device_t);
170e51a25f8SAlfred Perlstein static int nge_ifmedia_upd(struct ifnet *);
171646abee6SJohn Baldwin static void nge_ifmedia_upd_locked(struct ifnet *);
172e51a25f8SAlfred Perlstein static void nge_ifmedia_sts(struct ifnet *, struct ifmediareq *);
173ce4946daSBill Paul 
174e51a25f8SAlfred Perlstein static void nge_delay(struct nge_softc *);
175e51a25f8SAlfred Perlstein static void nge_eeprom_idle(struct nge_softc *);
176e51a25f8SAlfred Perlstein static void nge_eeprom_putbyte(struct nge_softc *, int);
177e51a25f8SAlfred Perlstein static void nge_eeprom_getword(struct nge_softc *, int, u_int16_t *);
178e51a25f8SAlfred Perlstein static void nge_read_eeprom(struct nge_softc *, caddr_t, int, int, int);
179ce4946daSBill Paul 
180e51a25f8SAlfred Perlstein static void nge_mii_sync(struct nge_softc *);
181e51a25f8SAlfred Perlstein static void nge_mii_send(struct nge_softc *, u_int32_t, int);
182e51a25f8SAlfred Perlstein static int nge_mii_readreg(struct nge_softc *, struct nge_mii_frame *);
183e51a25f8SAlfred Perlstein static int nge_mii_writereg(struct nge_softc *, struct nge_mii_frame *);
184ce4946daSBill Paul 
185e51a25f8SAlfred Perlstein static int nge_miibus_readreg(device_t, int, int);
186e51a25f8SAlfred Perlstein static int nge_miibus_writereg(device_t, int, int, int);
187e51a25f8SAlfred Perlstein static void nge_miibus_statchg(device_t);
188ce4946daSBill Paul 
189e51a25f8SAlfred Perlstein static void nge_setmulti(struct nge_softc *);
190e51a25f8SAlfred Perlstein static void nge_reset(struct nge_softc *);
191e51a25f8SAlfred Perlstein static int nge_list_rx_init(struct nge_softc *);
192e51a25f8SAlfred Perlstein static int nge_list_tx_init(struct nge_softc *);
193ce4946daSBill Paul 
194ce4946daSBill Paul #ifdef NGE_USEIOSPACE
195ce4946daSBill Paul #define NGE_RES			SYS_RES_IOPORT
196ce4946daSBill Paul #define NGE_RID			NGE_PCI_LOIO
197ce4946daSBill Paul #else
198ce4946daSBill Paul #define NGE_RES			SYS_RES_MEMORY
199ce4946daSBill Paul #define NGE_RID			NGE_PCI_LOMEM
200ce4946daSBill Paul #endif
201ce4946daSBill Paul 
202ce4946daSBill Paul static device_method_t nge_methods[] = {
203ce4946daSBill Paul 	/* Device interface */
204ce4946daSBill Paul 	DEVMETHOD(device_probe,		nge_probe),
205ce4946daSBill Paul 	DEVMETHOD(device_attach,	nge_attach),
206ce4946daSBill Paul 	DEVMETHOD(device_detach,	nge_detach),
207ce4946daSBill Paul 	DEVMETHOD(device_shutdown,	nge_shutdown),
208ce4946daSBill Paul 
209ce4946daSBill Paul 	/* bus interface */
210ce4946daSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
211ce4946daSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
212ce4946daSBill Paul 
213ce4946daSBill Paul 	/* MII interface */
214ce4946daSBill Paul 	DEVMETHOD(miibus_readreg,	nge_miibus_readreg),
215ce4946daSBill Paul 	DEVMETHOD(miibus_writereg,	nge_miibus_writereg),
216ce4946daSBill Paul 	DEVMETHOD(miibus_statchg,	nge_miibus_statchg),
217ce4946daSBill Paul 
218ce4946daSBill Paul 	{ 0, 0 }
219ce4946daSBill Paul };
220ce4946daSBill Paul 
221ce4946daSBill Paul static driver_t nge_driver = {
222ce4946daSBill Paul 	"nge",
223ce4946daSBill Paul 	nge_methods,
224ce4946daSBill Paul 	sizeof(struct nge_softc)
225ce4946daSBill Paul };
226ce4946daSBill Paul 
227ce4946daSBill Paul static devclass_t nge_devclass;
228ce4946daSBill Paul 
229f246e4a1SMatthew N. Dodd DRIVER_MODULE(nge, pci, nge_driver, nge_devclass, 0, 0);
230ce4946daSBill Paul DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0);
231ce4946daSBill Paul 
232ce4946daSBill Paul #define NGE_SETBIT(sc, reg, x)				\
233ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
234ce4946daSBill Paul 		CSR_READ_4(sc, reg) | (x))
235ce4946daSBill Paul 
236ce4946daSBill Paul #define NGE_CLRBIT(sc, reg, x)				\
237ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
238ce4946daSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
239ce4946daSBill Paul 
240ce4946daSBill Paul #define SIO_SET(x)					\
24129f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x))
242ce4946daSBill Paul 
243ce4946daSBill Paul #define SIO_CLR(x)					\
24429f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x))
245ce4946daSBill Paul 
246eaabec55SAlfred Perlstein static void
247284c81cbSPyun YongHyeon nge_delay(struct nge_softc *sc)
248ce4946daSBill Paul {
249ce4946daSBill Paul 	int			idx;
250ce4946daSBill Paul 
251ce4946daSBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
252ce4946daSBill Paul 		CSR_READ_4(sc, NGE_CSR);
253ce4946daSBill Paul 
254ce4946daSBill Paul 	return;
255ce4946daSBill Paul }
256ce4946daSBill Paul 
257eaabec55SAlfred Perlstein static void
258284c81cbSPyun YongHyeon nge_eeprom_idle(struct nge_softc *sc)
259ce4946daSBill Paul {
260ce4946daSBill Paul 	register int		i;
261ce4946daSBill Paul 
262ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
263ce4946daSBill Paul 	nge_delay(sc);
264ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CLK);
265ce4946daSBill Paul 	nge_delay(sc);
266ce4946daSBill Paul 
267ce4946daSBill Paul 	for (i = 0; i < 25; i++) {
268ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
269ce4946daSBill Paul 		nge_delay(sc);
270ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
271ce4946daSBill Paul 		nge_delay(sc);
272ce4946daSBill Paul 	}
273ce4946daSBill Paul 
274ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
275ce4946daSBill Paul 	nge_delay(sc);
276ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CSEL);
277ce4946daSBill Paul 	nge_delay(sc);
278ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0x00000000);
279ce4946daSBill Paul 
280ce4946daSBill Paul 	return;
281ce4946daSBill Paul }
282ce4946daSBill Paul 
283ce4946daSBill Paul /*
284ce4946daSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
285ce4946daSBill Paul  */
286eaabec55SAlfred Perlstein static void
287284c81cbSPyun YongHyeon nge_eeprom_putbyte(struct nge_softc *sc, int addr)
288ce4946daSBill Paul {
289ce4946daSBill Paul 	register int		d, i;
290ce4946daSBill Paul 
291ce4946daSBill Paul 	d = addr | NGE_EECMD_READ;
292ce4946daSBill Paul 
293ce4946daSBill Paul 	/*
294ce4946daSBill Paul 	 * Feed in each bit and stobe the clock.
295ce4946daSBill Paul 	 */
296ce4946daSBill Paul 	for (i = 0x400; i; i >>= 1) {
297ce4946daSBill Paul 		if (d & i) {
298ce4946daSBill Paul 			SIO_SET(NGE_MEAR_EE_DIN);
299ce4946daSBill Paul 		} else {
300ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_EE_DIN);
301ce4946daSBill Paul 		}
302ce4946daSBill Paul 		nge_delay(sc);
303ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
304ce4946daSBill Paul 		nge_delay(sc);
305ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
306ce4946daSBill Paul 		nge_delay(sc);
307ce4946daSBill Paul 	}
308ce4946daSBill Paul 
309ce4946daSBill Paul 	return;
310ce4946daSBill Paul }
311ce4946daSBill Paul 
312ce4946daSBill Paul /*
313ce4946daSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
314ce4946daSBill Paul  */
315eaabec55SAlfred Perlstein static void
316284c81cbSPyun YongHyeon nge_eeprom_getword(struct nge_softc *sc, int addr, u_int16_t *dest)
317ce4946daSBill Paul {
318ce4946daSBill Paul 	register int		i;
319ce4946daSBill Paul 	u_int16_t		word = 0;
320ce4946daSBill Paul 
321ce4946daSBill Paul 	/* Force EEPROM to idle state. */
322ce4946daSBill Paul 	nge_eeprom_idle(sc);
323ce4946daSBill Paul 
324ce4946daSBill Paul 	/* Enter EEPROM access mode. */
325ce4946daSBill Paul 	nge_delay(sc);
326ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
327ce4946daSBill Paul 	nge_delay(sc);
328ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
329ce4946daSBill Paul 	nge_delay(sc);
330ce4946daSBill Paul 
331ce4946daSBill Paul 	/*
332ce4946daSBill Paul 	 * Send address of word we want to read.
333ce4946daSBill Paul 	 */
334ce4946daSBill Paul 	nge_eeprom_putbyte(sc, addr);
335ce4946daSBill Paul 
336ce4946daSBill Paul 	/*
337ce4946daSBill Paul 	 * Start reading bits from EEPROM.
338ce4946daSBill Paul 	 */
339ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
340ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
341ce4946daSBill Paul 		nge_delay(sc);
342ce4946daSBill Paul 		if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT)
343ce4946daSBill Paul 			word |= i;
344ce4946daSBill Paul 		nge_delay(sc);
345ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
346ce4946daSBill Paul 		nge_delay(sc);
347ce4946daSBill Paul 	}
348ce4946daSBill Paul 
349ce4946daSBill Paul 	/* Turn off EEPROM access mode. */
350ce4946daSBill Paul 	nge_eeprom_idle(sc);
351ce4946daSBill Paul 
352ce4946daSBill Paul 	*dest = word;
353ce4946daSBill Paul 
354ce4946daSBill Paul 	return;
355ce4946daSBill Paul }
356ce4946daSBill Paul 
357ce4946daSBill Paul /*
358ce4946daSBill Paul  * Read a sequence of words from the EEPROM.
359ce4946daSBill Paul  */
360eaabec55SAlfred Perlstein static void
361284c81cbSPyun YongHyeon nge_read_eeprom(struct nge_softc *sc, caddr_t dest, int off, int cnt, int swap)
362ce4946daSBill Paul {
363ce4946daSBill Paul 	int			i;
364ce4946daSBill Paul 	u_int16_t		word = 0, *ptr;
365ce4946daSBill Paul 
366ce4946daSBill Paul 	for (i = 0; i < cnt; i++) {
367ce4946daSBill Paul 		nge_eeprom_getword(sc, off + i, &word);
368ce4946daSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
369ce4946daSBill Paul 		if (swap)
370ce4946daSBill Paul 			*ptr = ntohs(word);
371ce4946daSBill Paul 		else
372ce4946daSBill Paul 			*ptr = word;
373ce4946daSBill Paul 	}
374ce4946daSBill Paul 
375ce4946daSBill Paul 	return;
376ce4946daSBill Paul }
377ce4946daSBill Paul 
378ce4946daSBill Paul /*
379ce4946daSBill Paul  * Sync the PHYs by setting data bit and strobing the clock 32 times.
380ce4946daSBill Paul  */
381eaabec55SAlfred Perlstein static void
382284c81cbSPyun YongHyeon nge_mii_sync(struct nge_softc *sc)
383ce4946daSBill Paul {
384ce4946daSBill Paul 	register int		i;
385ce4946daSBill Paul 
386ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR|NGE_MEAR_MII_DATA);
387ce4946daSBill Paul 
388ce4946daSBill Paul 	for (i = 0; i < 32; i++) {
389ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
390ce4946daSBill Paul 		DELAY(1);
391ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
392ce4946daSBill Paul 		DELAY(1);
393ce4946daSBill Paul 	}
394ce4946daSBill Paul 
395ce4946daSBill Paul 	return;
396ce4946daSBill Paul }
397ce4946daSBill Paul 
398ce4946daSBill Paul /*
399ce4946daSBill Paul  * Clock a series of bits through the MII.
400ce4946daSBill Paul  */
401eaabec55SAlfred Perlstein static void
402284c81cbSPyun YongHyeon nge_mii_send(struct nge_softc *sc, u_int32_t bits, int cnt)
403ce4946daSBill Paul {
404ce4946daSBill Paul 	int			i;
405ce4946daSBill Paul 
406ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
407ce4946daSBill Paul 
408ce4946daSBill Paul 	for (i = (0x1 << (cnt - 1)); i; i >>= 1) {
409ce4946daSBill Paul                 if (bits & i) {
410ce4946daSBill Paul 			SIO_SET(NGE_MEAR_MII_DATA);
411ce4946daSBill Paul                 } else {
412ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_MII_DATA);
413ce4946daSBill Paul                 }
414ce4946daSBill Paul 		DELAY(1);
415ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
416ce4946daSBill Paul 		DELAY(1);
417ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
418ce4946daSBill Paul 	}
419ce4946daSBill Paul }
420ce4946daSBill Paul 
421ce4946daSBill Paul /*
422ce4946daSBill Paul  * Read an PHY register through the MII.
423ce4946daSBill Paul  */
424eaabec55SAlfred Perlstein static int
425284c81cbSPyun YongHyeon nge_mii_readreg(struct nge_softc *sc, struct nge_mii_frame *frame)
426ce4946daSBill Paul {
427ad6c618bSBill Paul 	int			i, ack;
428ce4946daSBill Paul 
429ce4946daSBill Paul 	/*
430ce4946daSBill Paul 	 * Set up frame for RX.
431ce4946daSBill Paul 	 */
432ce4946daSBill Paul 	frame->mii_stdelim = NGE_MII_STARTDELIM;
433ce4946daSBill Paul 	frame->mii_opcode = NGE_MII_READOP;
434ce4946daSBill Paul 	frame->mii_turnaround = 0;
435ce4946daSBill Paul 	frame->mii_data = 0;
436ce4946daSBill Paul 
437ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0);
438ce4946daSBill Paul 
439ce4946daSBill Paul 	/*
440ce4946daSBill Paul  	 * Turn on data xmit.
441ce4946daSBill Paul 	 */
442ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR);
443ce4946daSBill Paul 
444ce4946daSBill Paul 	nge_mii_sync(sc);
445ce4946daSBill Paul 
446ce4946daSBill Paul 	/*
447ce4946daSBill Paul 	 * Send command/address info.
448ce4946daSBill Paul 	 */
449ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_stdelim, 2);
450ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_opcode, 2);
451ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_phyaddr, 5);
452ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_regaddr, 5);
453ce4946daSBill Paul 
454ce4946daSBill Paul 	/* Idle bit */
455ce4946daSBill Paul 	SIO_CLR((NGE_MEAR_MII_CLK|NGE_MEAR_MII_DATA));
456ce4946daSBill Paul 	DELAY(1);
457ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
458ce4946daSBill Paul 	DELAY(1);
459ce4946daSBill Paul 
460ce4946daSBill Paul 	/* Turn off xmit. */
461ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_DIR);
462ce4946daSBill Paul 	/* Check for ack */
463ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
464ce4946daSBill Paul 	DELAY(1);
465e808cf62SMartin Blapp 	ack = CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA;
466ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
467ce4946daSBill Paul 	DELAY(1);
468ce4946daSBill Paul 
469ce4946daSBill Paul 	/*
470ce4946daSBill Paul 	 * Now try reading data bits. If the ack failed, we still
471ce4946daSBill Paul 	 * need to clock through 16 cycles to keep the PHY(s) in sync.
472ce4946daSBill Paul 	 */
473ce4946daSBill Paul 	if (ack) {
474ce4946daSBill Paul 		for(i = 0; i < 16; i++) {
475ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_MII_CLK);
476ce4946daSBill Paul 			DELAY(1);
477ce4946daSBill Paul 			SIO_SET(NGE_MEAR_MII_CLK);
478ce4946daSBill Paul 			DELAY(1);
479ce4946daSBill Paul 		}
480ce4946daSBill Paul 		goto fail;
481ce4946daSBill Paul 	}
482ce4946daSBill Paul 
483ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
484ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_MII_CLK);
485ce4946daSBill Paul 		DELAY(1);
486ce4946daSBill Paul 		if (!ack) {
487ce4946daSBill Paul 			if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_MII_DATA)
488ce4946daSBill Paul 				frame->mii_data |= i;
489ce4946daSBill Paul 			DELAY(1);
490ce4946daSBill Paul 		}
491ce4946daSBill Paul 		SIO_SET(NGE_MEAR_MII_CLK);
492ce4946daSBill Paul 		DELAY(1);
493ce4946daSBill Paul 	}
494ce4946daSBill Paul 
495ce4946daSBill Paul fail:
496ce4946daSBill Paul 
497ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
498ce4946daSBill Paul 	DELAY(1);
499ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
500ce4946daSBill Paul 	DELAY(1);
501ce4946daSBill Paul 
502ce4946daSBill Paul 	if (ack)
503ce4946daSBill Paul 		return(1);
504ce4946daSBill Paul 	return(0);
505ce4946daSBill Paul }
506ce4946daSBill Paul 
507ce4946daSBill Paul /*
508ce4946daSBill Paul  * Write to a PHY register through the MII.
509ce4946daSBill Paul  */
510eaabec55SAlfred Perlstein static int
511284c81cbSPyun YongHyeon nge_mii_writereg(struct nge_softc *sc, struct nge_mii_frame *frame)
512ce4946daSBill Paul {
513ce4946daSBill Paul 
514ce4946daSBill Paul 	/*
515ce4946daSBill Paul 	 * Set up frame for TX.
516ce4946daSBill Paul 	 */
517ce4946daSBill Paul 
518ce4946daSBill Paul 	frame->mii_stdelim = NGE_MII_STARTDELIM;
519ce4946daSBill Paul 	frame->mii_opcode = NGE_MII_WRITEOP;
520ce4946daSBill Paul 	frame->mii_turnaround = NGE_MII_TURNAROUND;
521ce4946daSBill Paul 
522ce4946daSBill Paul 	/*
523ce4946daSBill Paul  	 * Turn on data output.
524ce4946daSBill Paul 	 */
525ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_DIR);
526ce4946daSBill Paul 
527ce4946daSBill Paul 	nge_mii_sync(sc);
528ce4946daSBill Paul 
529ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_stdelim, 2);
530ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_opcode, 2);
531ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_phyaddr, 5);
532ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_regaddr, 5);
533ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_turnaround, 2);
534ce4946daSBill Paul 	nge_mii_send(sc, frame->mii_data, 16);
535ce4946daSBill Paul 
536ce4946daSBill Paul 	/* Idle bit. */
537ce4946daSBill Paul 	SIO_SET(NGE_MEAR_MII_CLK);
538ce4946daSBill Paul 	DELAY(1);
539ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_CLK);
540ce4946daSBill Paul 	DELAY(1);
541ce4946daSBill Paul 
542ce4946daSBill Paul 	/*
543ce4946daSBill Paul 	 * Turn off xmit.
544ce4946daSBill Paul 	 */
545ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_MII_DIR);
546ce4946daSBill Paul 
547ce4946daSBill Paul 	return(0);
548ce4946daSBill Paul }
549ce4946daSBill Paul 
550eaabec55SAlfred Perlstein static int
551284c81cbSPyun YongHyeon nge_miibus_readreg(device_t dev, int phy, int reg)
552ce4946daSBill Paul {
553ce4946daSBill Paul 	struct nge_softc	*sc;
554ce4946daSBill Paul 	struct nge_mii_frame	frame;
555ce4946daSBill Paul 
556ce4946daSBill Paul 	sc = device_get_softc(dev);
557ce4946daSBill Paul 
558ce4946daSBill Paul 	bzero((char *)&frame, sizeof(frame));
559ce4946daSBill Paul 
560ce4946daSBill Paul 	frame.mii_phyaddr = phy;
561ce4946daSBill Paul 	frame.mii_regaddr = reg;
562ce4946daSBill Paul 	nge_mii_readreg(sc, &frame);
563ce4946daSBill Paul 
564ce4946daSBill Paul 	return(frame.mii_data);
565ce4946daSBill Paul }
566ce4946daSBill Paul 
567eaabec55SAlfred Perlstein static int
568284c81cbSPyun YongHyeon nge_miibus_writereg(device_t dev, int phy, int reg, int data)
569ce4946daSBill Paul {
570ce4946daSBill Paul 	struct nge_softc	*sc;
571ce4946daSBill Paul 	struct nge_mii_frame	frame;
572ce4946daSBill Paul 
573ce4946daSBill Paul 	sc = device_get_softc(dev);
574ce4946daSBill Paul 
575ce4946daSBill Paul 	bzero((char *)&frame, sizeof(frame));
576ce4946daSBill Paul 
577ce4946daSBill Paul 	frame.mii_phyaddr = phy;
578ce4946daSBill Paul 	frame.mii_regaddr = reg;
579ce4946daSBill Paul 	frame.mii_data = data;
580ce4946daSBill Paul 	nge_mii_writereg(sc, &frame);
581ce4946daSBill Paul 
582ce4946daSBill Paul 	return(0);
583ce4946daSBill Paul }
584ce4946daSBill Paul 
585eaabec55SAlfred Perlstein static void
586284c81cbSPyun YongHyeon nge_miibus_statchg(device_t dev)
587ce4946daSBill Paul {
5881f548804SDoug Ambrisko 	int			status;
589ce4946daSBill Paul 	struct nge_softc	*sc;
590ce4946daSBill Paul 	struct mii_data		*mii;
591ce4946daSBill Paul 
592ce4946daSBill Paul 	sc = device_get_softc(dev);
5931f548804SDoug Ambrisko 	if (sc->nge_tbi) {
5941f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
5951f548804SDoug Ambrisko 		    == IFM_AUTO) {
5961f548804SDoug Ambrisko 			status = CSR_READ_4(sc, NGE_TBI_ANLPAR);
5971f548804SDoug Ambrisko 			if (status == 0 || status & NGE_TBIANAR_FDX) {
5981f548804SDoug Ambrisko 				NGE_SETBIT(sc, NGE_TX_CFG,
5991f548804SDoug Ambrisko 				    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6001f548804SDoug Ambrisko 				NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6011f548804SDoug Ambrisko 			} else {
6021f548804SDoug Ambrisko 				NGE_CLRBIT(sc, NGE_TX_CFG,
6031f548804SDoug Ambrisko 				    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6041f548804SDoug Ambrisko 				NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6051f548804SDoug Ambrisko 			}
6061f548804SDoug Ambrisko 
6071f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
6081f548804SDoug Ambrisko 			!= IFM_FDX) {
6091f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
6101f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6111f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6121f548804SDoug Ambrisko 		} else {
6131f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
6141f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
6151f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
6161f548804SDoug Ambrisko 		}
6171f548804SDoug Ambrisko 	} else {
618ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
619ce4946daSBill Paul 
620ce4946daSBill Paul 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
621ce4946daSBill Paul 		        NGE_SETBIT(sc, NGE_TX_CFG,
622ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
623ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
624ce4946daSBill Paul 		} else {
625ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
626ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
627ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
628ce4946daSBill Paul 		}
629ce4946daSBill Paul 
6302ce0498bSBill Paul 		/* If we have a 1000Mbps link, set the mode_1000 bit. */
631b418ad5cSPoul-Henning Kamp 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T ||
6322ce0498bSBill Paul 		    IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_SX) {
6332ce0498bSBill Paul 			NGE_SETBIT(sc, NGE_CFG, NGE_CFG_MODE_1000);
6342ce0498bSBill Paul 		} else {
6352ce0498bSBill Paul 			NGE_CLRBIT(sc, NGE_CFG, NGE_CFG_MODE_1000);
6362ce0498bSBill Paul 		}
6371f548804SDoug Ambrisko 	}
638ce4946daSBill Paul 	return;
639ce4946daSBill Paul }
640ce4946daSBill Paul 
641eaabec55SAlfred Perlstein static void
642284c81cbSPyun YongHyeon nge_setmulti(struct nge_softc *sc)
643ce4946daSBill Paul {
644ce4946daSBill Paul 	struct ifnet		*ifp;
645ce4946daSBill Paul 	struct ifmultiaddr	*ifma;
646ce4946daSBill Paul 	u_int32_t		h = 0, i, filtsave;
647ce4946daSBill Paul 	int			bit, index;
648ce4946daSBill Paul 
649ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
650fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
651ce4946daSBill Paul 
652ce4946daSBill Paul 	if (ifp->if_flags & IFF_ALLMULTI || ifp->if_flags & IFF_PROMISC) {
653ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL,
654ce4946daSBill Paul 		    NGE_RXFILTCTL_MCHASH|NGE_RXFILTCTL_UCHASH);
655ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLMULTI);
656ce4946daSBill Paul 		return;
657ce4946daSBill Paul 	}
658ce4946daSBill Paul 
659ce4946daSBill Paul 	/*
660ce4946daSBill Paul 	 * We have to explicitly enable the multicast hash table
661ce4946daSBill Paul 	 * on the NatSemi chip if we want to use it, which we do.
662ce4946daSBill Paul 	 * We also have to tell it that we don't want to use the
663ce4946daSBill Paul 	 * hash table for matching unicast addresses.
664ce4946daSBill Paul 	 */
665ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_MCHASH);
666ce4946daSBill Paul 	NGE_CLRBIT(sc, NGE_RXFILT_CTL,
667ce4946daSBill Paul 	    NGE_RXFILTCTL_ALLMULTI|NGE_RXFILTCTL_UCHASH);
668ce4946daSBill Paul 
669ce4946daSBill Paul 	filtsave = CSR_READ_4(sc, NGE_RXFILT_CTL);
670ce4946daSBill Paul 
671ce4946daSBill Paul 	/* first, zot all the existing hash bits */
672ce4946daSBill Paul 	for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) {
673ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i);
674ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0);
675ce4946daSBill Paul 	}
676ce4946daSBill Paul 
677ce4946daSBill Paul 	/*
678ce4946daSBill Paul 	 * From the 11 bits returned by the crc routine, the top 7
679ce4946daSBill Paul 	 * bits represent the 16-bit word in the mcast hash table
680ce4946daSBill Paul 	 * that needs to be updated, and the lower 4 bits represent
681ce4946daSBill Paul 	 * which bit within that byte needs to be set.
682ce4946daSBill Paul 	 */
68313b203d0SRobert Watson 	IF_ADDR_LOCK(ifp);
684ce4946daSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
685ce4946daSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
686ce4946daSBill Paul 			continue;
6870e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6880e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 21;
689ce4946daSBill Paul 		index = (h >> 4) & 0x7F;
690ce4946daSBill Paul 		bit = h & 0xF;
691ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL,
692ce4946daSBill Paul 		    NGE_FILTADDR_MCAST_LO + (index * 2));
693ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit));
694ce4946daSBill Paul 	}
69513b203d0SRobert Watson 	IF_ADDR_UNLOCK(ifp);
696ce4946daSBill Paul 
697ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, filtsave);
698ce4946daSBill Paul 
699ce4946daSBill Paul 	return;
700ce4946daSBill Paul }
701ce4946daSBill Paul 
702eaabec55SAlfred Perlstein static void
703284c81cbSPyun YongHyeon nge_reset(struct nge_softc *sc)
704ce4946daSBill Paul {
705ce4946daSBill Paul 	register int		i;
706ce4946daSBill Paul 
707ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET);
708ce4946daSBill Paul 
709ce4946daSBill Paul 	for (i = 0; i < NGE_TIMEOUT; i++) {
710ce4946daSBill Paul 		if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET))
711ce4946daSBill Paul 			break;
712ce4946daSBill Paul 	}
713ce4946daSBill Paul 
714ce4946daSBill Paul 	if (i == NGE_TIMEOUT)
7156b9f5c94SGleb Smirnoff 		device_printf(sc->nge_dev, "reset never completed\n");
716ce4946daSBill Paul 
717ce4946daSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
718ce4946daSBill Paul 	DELAY(1000);
719ce4946daSBill Paul 
720ce4946daSBill Paul 	/*
721ce4946daSBill Paul 	 * If this is a NetSemi chip, make sure to clear
722ce4946daSBill Paul 	 * PME mode.
723ce4946daSBill Paul 	 */
724ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS);
725ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, 0);
726ce4946daSBill Paul 
727ce4946daSBill Paul         return;
728ce4946daSBill Paul }
729ce4946daSBill Paul 
730ce4946daSBill Paul /*
731d64ada50SJens Schweikhardt  * Probe for a NatSemi chip. Check the PCI vendor and device
732ce4946daSBill Paul  * IDs against our list and return a device name if we find a match.
733ce4946daSBill Paul  */
734eaabec55SAlfred Perlstein static int
735284c81cbSPyun YongHyeon nge_probe(device_t dev)
736ce4946daSBill Paul {
737ce4946daSBill Paul 	struct nge_type		*t;
738ce4946daSBill Paul 
739ce4946daSBill Paul 	t = nge_devs;
740ce4946daSBill Paul 
741ce4946daSBill Paul 	while(t->nge_name != NULL) {
742ce4946daSBill Paul 		if ((pci_get_vendor(dev) == t->nge_vid) &&
743ce4946daSBill Paul 		    (pci_get_device(dev) == t->nge_did)) {
744ce4946daSBill Paul 			device_set_desc(dev, t->nge_name);
7456b9907e7SWarner Losh 			return(BUS_PROBE_DEFAULT);
746ce4946daSBill Paul 		}
747ce4946daSBill Paul 		t++;
748ce4946daSBill Paul 	}
749ce4946daSBill Paul 
750ce4946daSBill Paul 	return(ENXIO);
751ce4946daSBill Paul }
752ce4946daSBill Paul 
753ce4946daSBill Paul /*
754ce4946daSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
755ce4946daSBill Paul  * setup and ethernet/BPF attach.
756ce4946daSBill Paul  */
757eaabec55SAlfred Perlstein static int
758284c81cbSPyun YongHyeon nge_attach(device_t dev)
759ce4946daSBill Paul {
760ce4946daSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
761ce4946daSBill Paul 	struct nge_softc	*sc;
762646abee6SJohn Baldwin 	struct ifnet		*ifp = NULL;
763646abee6SJohn Baldwin 	int			error = 0, rid;
764ce4946daSBill Paul 
765ce4946daSBill Paul 	sc = device_get_softc(dev);
7666b9f5c94SGleb Smirnoff 	sc->nge_dev = dev;
767ce4946daSBill Paul 
768ad6c618bSBill Paul 	NGE_LOCK_INIT(sc, device_get_nameunit(dev));
769646abee6SJohn Baldwin 	callout_init_mtx(&sc->nge_stat_ch, &sc->nge_mtx, 0);
770646abee6SJohn Baldwin 
771ce4946daSBill Paul 	/*
772ce4946daSBill Paul 	 * Map control/status registers.
773ce4946daSBill Paul 	 */
774ce4946daSBill Paul 	pci_enable_busmaster(dev);
775ce4946daSBill Paul 
776ce4946daSBill Paul 	rid = NGE_RID;
7775f96beb9SNate Lawson 	sc->nge_res = bus_alloc_resource_any(dev, NGE_RES, &rid, RF_ACTIVE);
778ce4946daSBill Paul 
779ce4946daSBill Paul 	if (sc->nge_res == NULL) {
780646abee6SJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
781ce4946daSBill Paul 		error = ENXIO;
782ce4946daSBill Paul 		goto fail;
783ce4946daSBill Paul 	}
784ce4946daSBill Paul 
785ce4946daSBill Paul 	sc->nge_btag = rman_get_bustag(sc->nge_res);
786ce4946daSBill Paul 	sc->nge_bhandle = rman_get_bushandle(sc->nge_res);
787ce4946daSBill Paul 
788ce4946daSBill Paul 	/* Allocate interrupt */
789ce4946daSBill Paul 	rid = 0;
7905f96beb9SNate Lawson 	sc->nge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
791ce4946daSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
792ce4946daSBill Paul 
793ce4946daSBill Paul 	if (sc->nge_irq == NULL) {
794646abee6SJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
795ce4946daSBill Paul 		error = ENXIO;
796ce4946daSBill Paul 		goto fail;
797ce4946daSBill Paul 	}
798ce4946daSBill Paul 
799ce4946daSBill Paul 	/* Reset the adapter. */
800ce4946daSBill Paul 	nge_reset(sc);
801ce4946daSBill Paul 
802ce4946daSBill Paul 	/*
803ce4946daSBill Paul 	 * Get station address from the EEPROM.
804ce4946daSBill Paul 	 */
805ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[4], NGE_EE_NODEADDR, 1, 0);
806ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[2], NGE_EE_NODEADDR + 1, 1, 0);
807ce4946daSBill Paul 	nge_read_eeprom(sc, (caddr_t)&eaddr[0], NGE_EE_NODEADDR + 2, 1, 0);
808ce4946daSBill Paul 
809ce4946daSBill Paul 	sc->nge_ldata = contigmalloc(sizeof(struct nge_list_data), M_DEVBUF,
810646abee6SJohn Baldwin 	    M_NOWAIT|M_ZERO, 0, 0xffffffff, PAGE_SIZE, 0);
811ce4946daSBill Paul 
812ce4946daSBill Paul 	if (sc->nge_ldata == NULL) {
813646abee6SJohn Baldwin 		device_printf(dev, "no memory for list buffers!\n");
814ce4946daSBill Paul 		error = ENXIO;
815ce4946daSBill Paul 		goto fail;
816ce4946daSBill Paul 	}
817ce4946daSBill Paul 
818fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp = if_alloc(IFT_ETHER);
819fc74a9f9SBrooks Davis 	if (ifp == NULL) {
820646abee6SJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
821fc74a9f9SBrooks Davis 		error = ENOSPC;
822fc74a9f9SBrooks Davis 		goto fail;
823fc74a9f9SBrooks Davis 	}
824ce4946daSBill Paul 	ifp->if_softc = sc;
8259bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
826ce4946daSBill Paul 	ifp->if_mtu = ETHERMTU;
827ad6c618bSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
828ce4946daSBill Paul 	ifp->if_ioctl = nge_ioctl;
829ce4946daSBill Paul 	ifp->if_start = nge_start;
830ce4946daSBill Paul 	ifp->if_watchdog = nge_watchdog;
831ce4946daSBill Paul 	ifp->if_init = nge_init;
832ce4946daSBill Paul 	ifp->if_snd.ifq_maxlen = NGE_TX_LIST_CNT - 1;
833ce4946daSBill Paul 	ifp->if_hwassist = NGE_CSUM_FEATURES;
834673d9191SSam Leffler 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWTAGGING;
83540929967SGleb Smirnoff 	ifp->if_capenable = ifp->if_capabilities;
83637f5f239SRuslan Ermilov #ifdef DEVICE_POLLING
83737f5f239SRuslan Ermilov 	ifp->if_capabilities |= IFCAP_POLLING;
83837f5f239SRuslan Ermilov #endif
839ce4946daSBill Paul 
840ce4946daSBill Paul 	/*
841ce4946daSBill Paul 	 * Do MII setup.
842ce4946daSBill Paul 	 */
8435ae7c95cSRuslan Ermilov 	/* XXX: leaked on error */
844ce4946daSBill Paul 	if (mii_phy_probe(dev, &sc->nge_miibus,
845ce4946daSBill Paul 			  nge_ifmedia_upd, nge_ifmedia_sts)) {
8461f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) {
8471f548804SDoug Ambrisko 			sc->nge_tbi = 1;
8481f548804SDoug Ambrisko 			device_printf(dev, "Using TBI\n");
8491f548804SDoug Ambrisko 
8501f548804SDoug Ambrisko 			sc->nge_miibus = dev;
8511f548804SDoug Ambrisko 
8521f548804SDoug Ambrisko 			ifmedia_init(&sc->nge_ifmedia, 0, nge_ifmedia_upd,
8531f548804SDoug Ambrisko 				nge_ifmedia_sts);
8541f548804SDoug Ambrisko #define	ADD(m, c)	ifmedia_add(&sc->nge_ifmedia, (m), (c), NULL)
8551f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_NONE, 0, 0), 0);
8561f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, 0, 0), 0);
8571f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_1000_SX, IFM_FDX, 0),0);
8581f548804SDoug Ambrisko 			ADD(IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0), 0);
8591f548804SDoug Ambrisko #undef ADD
860646abee6SJohn Baldwin 			device_printf(dev, " 1000baseSX, 1000baseSX-FDX, auto\n");
861646abee6SJohn Baldwin 
8621f548804SDoug Ambrisko 			ifmedia_set(&sc->nge_ifmedia,
8631f548804SDoug Ambrisko 				IFM_MAKEWORD(IFM_ETHER, IFM_AUTO, 0, 0));
8641f548804SDoug Ambrisko 
8651f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
8661f548804SDoug Ambrisko 				| NGE_GPIO_GP4_OUT
8671f548804SDoug Ambrisko 				| NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB
8681f548804SDoug Ambrisko 				| NGE_GPIO_GP3_OUTENB
8691f548804SDoug Ambrisko 				| NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN);
8701f548804SDoug Ambrisko 
8711f548804SDoug Ambrisko 		} else {
872646abee6SJohn Baldwin 			device_printf(dev, "MII without any PHY!\n");
873ce4946daSBill Paul 			error = ENXIO;
874ce4946daSBill Paul 			goto fail;
875ce4946daSBill Paul 		}
8761f548804SDoug Ambrisko 	}
877ce4946daSBill Paul 
878ce4946daSBill Paul 	/*
879ce4946daSBill Paul 	 * Call MI attach routine.
880ce4946daSBill Paul 	 */
881673d9191SSam Leffler 	ether_ifattach(ifp, eaddr);
882ad6c618bSBill Paul 
883ad6c618bSBill Paul 	/*
884ad6c618bSBill Paul 	 * Hookup IRQ last.
885ad6c618bSBill Paul 	 */
886ad6c618bSBill Paul 	error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET | INTR_MPSAFE,
887ef544f63SPaolo Pisati 	    NULL, nge_intr, sc, &sc->nge_intrhand);
888ad6c618bSBill Paul 	if (error) {
889646abee6SJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
890646abee6SJohn Baldwin 		goto fail;
891ad6c618bSBill Paul 	}
892ce4946daSBill Paul 
893646abee6SJohn Baldwin 	return (0);
8941f548804SDoug Ambrisko 
895646abee6SJohn Baldwin fail:
896646abee6SJohn Baldwin 	if (sc->nge_ldata)
897646abee6SJohn Baldwin 		contigfree(sc->nge_ldata,
898646abee6SJohn Baldwin 		  sizeof(struct nge_list_data), M_DEVBUF);
899646abee6SJohn Baldwin 	if (ifp)
900646abee6SJohn Baldwin 		if_free(ifp);
901646abee6SJohn Baldwin 	if (sc->nge_irq)
902646abee6SJohn Baldwin 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
903646abee6SJohn Baldwin 	if (sc->nge_res)
904646abee6SJohn Baldwin 		bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
905ad6c618bSBill Paul 	NGE_LOCK_DESTROY(sc);
906ce4946daSBill Paul 	return(error);
907ce4946daSBill Paul }
908ce4946daSBill Paul 
909eaabec55SAlfred Perlstein static int
910284c81cbSPyun YongHyeon nge_detach(device_t dev)
911ce4946daSBill Paul {
912ce4946daSBill Paul 	struct nge_softc	*sc;
913ce4946daSBill Paul 	struct ifnet		*ifp;
914ce4946daSBill Paul 
915ce4946daSBill Paul 	sc = device_get_softc(dev);
916fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
917ce4946daSBill Paul 
91840929967SGleb Smirnoff #ifdef DEVICE_POLLING
91940929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
92040929967SGleb Smirnoff 		ether_poll_deregister(ifp);
92140929967SGleb Smirnoff #endif
922ad6c618bSBill Paul 	NGE_LOCK(sc);
923ce4946daSBill Paul 	nge_reset(sc);
924ce4946daSBill Paul 	nge_stop(sc);
925ad6c618bSBill Paul 	NGE_UNLOCK(sc);
926646abee6SJohn Baldwin 	callout_drain(&sc->nge_stat_ch);
927673d9191SSam Leffler 	ether_ifdetach(ifp);
928ce4946daSBill Paul 
929ce4946daSBill Paul 	bus_generic_detach(dev);
9301f548804SDoug Ambrisko 	if (!sc->nge_tbi) {
931ce4946daSBill Paul 		device_delete_child(dev, sc->nge_miibus);
9321f548804SDoug Ambrisko 	}
933ce4946daSBill Paul 	bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
934ce4946daSBill Paul 	bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
935ce4946daSBill Paul 	bus_release_resource(dev, NGE_RES, NGE_RID, sc->nge_res);
936ce4946daSBill Paul 
937ce4946daSBill Paul 	contigfree(sc->nge_ldata, sizeof(struct nge_list_data), M_DEVBUF);
938ad4f426eSWarner Losh 	if_free(ifp);
939ce4946daSBill Paul 
9406ba160b6SBill Paul 	NGE_LOCK_DESTROY(sc);
9416ba160b6SBill Paul 
942ce4946daSBill Paul 	return(0);
943ce4946daSBill Paul }
944ce4946daSBill Paul 
945ce4946daSBill Paul /*
946ce4946daSBill Paul  * Initialize the transmit descriptors.
947ce4946daSBill Paul  */
948eaabec55SAlfred Perlstein static int
949284c81cbSPyun YongHyeon nge_list_tx_init(struct nge_softc *sc)
950ce4946daSBill Paul {
951ce4946daSBill Paul 	struct nge_list_data	*ld;
952ce4946daSBill Paul 	struct nge_ring_data	*cd;
953ce4946daSBill Paul 	int			i;
954ce4946daSBill Paul 
955ce4946daSBill Paul 	cd = &sc->nge_cdata;
956ce4946daSBill Paul 	ld = sc->nge_ldata;
957ce4946daSBill Paul 
958ce4946daSBill Paul 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
959ce4946daSBill Paul 		if (i == (NGE_TX_LIST_CNT - 1)) {
960ce4946daSBill Paul 			ld->nge_tx_list[i].nge_nextdesc =
961ce4946daSBill Paul 			    &ld->nge_tx_list[0];
962ce4946daSBill Paul 			ld->nge_tx_list[i].nge_next =
963ce4946daSBill Paul 			    vtophys(&ld->nge_tx_list[0]);
964ce4946daSBill Paul 		} else {
965ce4946daSBill Paul 			ld->nge_tx_list[i].nge_nextdesc =
966ce4946daSBill Paul 			    &ld->nge_tx_list[i + 1];
967ce4946daSBill Paul 			ld->nge_tx_list[i].nge_next =
968ce4946daSBill Paul 			    vtophys(&ld->nge_tx_list[i + 1]);
969ce4946daSBill Paul 		}
970ce4946daSBill Paul 		ld->nge_tx_list[i].nge_mbuf = NULL;
971ce4946daSBill Paul 		ld->nge_tx_list[i].nge_ptr = 0;
972ce4946daSBill Paul 		ld->nge_tx_list[i].nge_ctl = 0;
973ce4946daSBill Paul 	}
974ce4946daSBill Paul 
975ce4946daSBill Paul 	cd->nge_tx_prod = cd->nge_tx_cons = cd->nge_tx_cnt = 0;
976ce4946daSBill Paul 
977ce4946daSBill Paul 	return(0);
978ce4946daSBill Paul }
979ce4946daSBill Paul 
980ce4946daSBill Paul 
981ce4946daSBill Paul /*
982ce4946daSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
983ce4946daSBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
984ce4946daSBill Paul  * points back to the first.
985ce4946daSBill Paul  */
986eaabec55SAlfred Perlstein static int
987284c81cbSPyun YongHyeon nge_list_rx_init(struct nge_softc *sc)
988ce4946daSBill Paul {
989ce4946daSBill Paul 	struct nge_list_data	*ld;
990ce4946daSBill Paul 	struct nge_ring_data	*cd;
991ce4946daSBill Paul 	int			i;
992ce4946daSBill Paul 
993ce4946daSBill Paul 	ld = sc->nge_ldata;
994ce4946daSBill Paul 	cd = &sc->nge_cdata;
995ce4946daSBill Paul 
996ce4946daSBill Paul 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
997ce4946daSBill Paul 		if (nge_newbuf(sc, &ld->nge_rx_list[i], NULL) == ENOBUFS)
998ce4946daSBill Paul 			return(ENOBUFS);
999ce4946daSBill Paul 		if (i == (NGE_RX_LIST_CNT - 1)) {
1000ce4946daSBill Paul 			ld->nge_rx_list[i].nge_nextdesc =
1001ce4946daSBill Paul 			    &ld->nge_rx_list[0];
1002ce4946daSBill Paul 			ld->nge_rx_list[i].nge_next =
1003ce4946daSBill Paul 			    vtophys(&ld->nge_rx_list[0]);
1004ce4946daSBill Paul 		} else {
1005ce4946daSBill Paul 			ld->nge_rx_list[i].nge_nextdesc =
1006ce4946daSBill Paul 			    &ld->nge_rx_list[i + 1];
1007ce4946daSBill Paul 			ld->nge_rx_list[i].nge_next =
1008ce4946daSBill Paul 			    vtophys(&ld->nge_rx_list[i + 1]);
1009ce4946daSBill Paul 		}
1010ce4946daSBill Paul 	}
1011ce4946daSBill Paul 
1012ce4946daSBill Paul 	cd->nge_rx_prod = 0;
1013ad6c618bSBill Paul 	sc->nge_head = sc->nge_tail = NULL;
1014ce4946daSBill Paul 
1015ce4946daSBill Paul 	return(0);
1016ce4946daSBill Paul }
1017ce4946daSBill Paul 
1018ce4946daSBill Paul /*
1019ce4946daSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
1020ce4946daSBill Paul  */
1021eaabec55SAlfred Perlstein static int
1022284c81cbSPyun YongHyeon nge_newbuf(struct nge_softc *sc, struct nge_desc *c, struct mbuf *m)
1023ce4946daSBill Paul {
1024ce4946daSBill Paul 
1025ce4946daSBill Paul 	if (m == NULL) {
10266c772336SSam Leffler 		m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
10276c772336SSam Leffler 		if (m == NULL)
1028ce4946daSBill Paul 			return (ENOBUFS);
1029ad6c618bSBill Paul 	} else
1030ad6c618bSBill Paul 		m->m_data = m->m_ext.ext_buf;
1031ce4946daSBill Paul 
1032ad6c618bSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
1033ce4946daSBill Paul 
10346c772336SSam Leffler 	m_adj(m, sizeof(u_int64_t));
1035ce4946daSBill Paul 
10366c772336SSam Leffler 	c->nge_mbuf = m;
10376c772336SSam Leffler 	c->nge_ptr = vtophys(mtod(m, caddr_t));
10386c772336SSam Leffler 	c->nge_ctl = m->m_len;
1039ce4946daSBill Paul 	c->nge_extsts = 0;
1040ce4946daSBill Paul 
1041ce4946daSBill Paul 	return(0);
1042ce4946daSBill Paul }
1043ce4946daSBill Paul 
1044ad6c618bSBill Paul #ifdef NGE_FIXUP_RX
1045ad6c618bSBill Paul static __inline void
1046284c81cbSPyun YongHyeon nge_fixup_rx(struct mbuf *m)
1047ce4946daSBill Paul {
1048ce4946daSBill Paul         int			i;
1049ad6c618bSBill Paul         uint16_t		*src, *dst;
1050ce4946daSBill Paul 
1051ad6c618bSBill Paul 	src = mtod(m, uint16_t *);
1052ad6c618bSBill Paul 	dst = src - 1;
1053ce4946daSBill Paul 
1054ad6c618bSBill Paul 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1055ad6c618bSBill Paul 		*dst++ = *src++;
1056ce4946daSBill Paul 
1057ad6c618bSBill Paul 	m->m_data -= ETHER_ALIGN;
1058ce4946daSBill Paul 
1059ce4946daSBill Paul 	return;
1060ce4946daSBill Paul }
1061ad6c618bSBill Paul #endif
1062ad6c618bSBill Paul 
1063ce4946daSBill Paul /*
1064ce4946daSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
1065ce4946daSBill Paul  * the higher level protocols.
1066ce4946daSBill Paul  */
1067eaabec55SAlfred Perlstein static void
1068284c81cbSPyun YongHyeon nge_rxeof(struct nge_softc *sc)
1069ce4946daSBill Paul {
1070ce4946daSBill Paul         struct mbuf		*m;
1071ce4946daSBill Paul         struct ifnet		*ifp;
1072ce4946daSBill Paul 	struct nge_desc		*cur_rx;
1073ce4946daSBill Paul 	int			i, total_len = 0;
1074ce4946daSBill Paul 	u_int32_t		rxstat;
1075ce4946daSBill Paul 
1076ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1077fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1078ce4946daSBill Paul 	i = sc->nge_cdata.nge_rx_prod;
1079ce4946daSBill Paul 
1080ce4946daSBill Paul 	while(NGE_OWNDESC(&sc->nge_ldata->nge_rx_list[i])) {
1081ce4946daSBill Paul 		u_int32_t		extsts;
1082ce4946daSBill Paul 
1083196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
108440929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
1085196c0df6SHidetoshi Shimokawa 			if (sc->rxcycles <= 0)
1086196c0df6SHidetoshi Shimokawa 				break;
1087196c0df6SHidetoshi Shimokawa 			sc->rxcycles--;
1088196c0df6SHidetoshi Shimokawa 		}
108940929967SGleb Smirnoff #endif
1090196c0df6SHidetoshi Shimokawa 
1091ce4946daSBill Paul 		cur_rx = &sc->nge_ldata->nge_rx_list[i];
1092ce4946daSBill Paul 		rxstat = cur_rx->nge_rxstat;
1093ce4946daSBill Paul 		extsts = cur_rx->nge_extsts;
1094ce4946daSBill Paul 		m = cur_rx->nge_mbuf;
1095ce4946daSBill Paul 		cur_rx->nge_mbuf = NULL;
1096ce4946daSBill Paul 		total_len = NGE_RXBYTES(cur_rx);
1097ce4946daSBill Paul 		NGE_INC(i, NGE_RX_LIST_CNT);
1098ad6c618bSBill Paul 
1099ad6c618bSBill Paul 		if (rxstat & NGE_CMDSTS_MORE) {
1100ad6c618bSBill Paul 			m->m_len = total_len;
1101ad6c618bSBill Paul 			if (sc->nge_head == NULL) {
1102ad6c618bSBill Paul 				m->m_pkthdr.len = total_len;
1103ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = m;
1104ad6c618bSBill Paul 			} else {
1105ad6c618bSBill Paul 				m->m_flags &= ~M_PKTHDR;
1106ad6c618bSBill Paul 				sc->nge_head->m_pkthdr.len += total_len;
1107ad6c618bSBill Paul 				sc->nge_tail->m_next = m;
1108ad6c618bSBill Paul 				sc->nge_tail = m;
1109ad6c618bSBill Paul 			}
1110ad6c618bSBill Paul 			nge_newbuf(sc, cur_rx, NULL);
1111ad6c618bSBill Paul 			continue;
1112ad6c618bSBill Paul 		}
1113ad6c618bSBill Paul 
1114ce4946daSBill Paul 		/*
1115ce4946daSBill Paul 		 * If an error occurs, update stats, clear the
1116ce4946daSBill Paul 		 * status word and leave the mbuf cluster in place:
1117ce4946daSBill Paul 		 * it should simply get re-used next time this descriptor
1118ce4946daSBill Paul 	 	 * comes up in the ring.
1119ce4946daSBill Paul 		 */
1120ce4946daSBill Paul 		if (!(rxstat & NGE_CMDSTS_PKT_OK)) {
1121ce4946daSBill Paul 			ifp->if_ierrors++;
1122ad6c618bSBill Paul 			if (sc->nge_head != NULL) {
1123ad6c618bSBill Paul 				m_freem(sc->nge_head);
1124ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = NULL;
1125ad6c618bSBill Paul 			}
1126ce4946daSBill Paul 			nge_newbuf(sc, cur_rx, m);
1127ce4946daSBill Paul 			continue;
1128ce4946daSBill Paul 		}
1129ce4946daSBill Paul 
1130ad6c618bSBill Paul 		/* Try conjure up a replacement mbuf. */
1131ad6c618bSBill Paul 
1132ad6c618bSBill Paul 		if (nge_newbuf(sc, cur_rx, NULL)) {
1133ad6c618bSBill Paul 			ifp->if_ierrors++;
1134ad6c618bSBill Paul 			if (sc->nge_head != NULL) {
1135ad6c618bSBill Paul 				m_freem(sc->nge_head);
1136ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = NULL;
1137ad6c618bSBill Paul 			}
1138ad6c618bSBill Paul 			nge_newbuf(sc, cur_rx, m);
1139ad6c618bSBill Paul 			continue;
1140ad6c618bSBill Paul 		}
1141ad6c618bSBill Paul 
1142ad6c618bSBill Paul 		if (sc->nge_head != NULL) {
1143ad6c618bSBill Paul 			m->m_len = total_len;
1144ad6c618bSBill Paul 			m->m_flags &= ~M_PKTHDR;
1145ad6c618bSBill Paul 			sc->nge_tail->m_next = m;
1146ad6c618bSBill Paul 			m = sc->nge_head;
1147ad6c618bSBill Paul 			m->m_pkthdr.len += total_len;
1148ad6c618bSBill Paul 			sc->nge_head = sc->nge_tail = NULL;
1149ad6c618bSBill Paul 		} else
1150ad6c618bSBill Paul 			m->m_pkthdr.len = m->m_len = total_len;
1151ad6c618bSBill Paul 
1152ce4946daSBill Paul 		/*
1153ce4946daSBill Paul 		 * Ok. NatSemi really screwed up here. This is the
1154ce4946daSBill Paul 		 * only gigE chip I know of with alignment constraints
1155ce4946daSBill Paul 		 * on receive buffers. RX buffers must be 64-bit aligned.
1156ce4946daSBill Paul 		 */
1157962315f6SBill Paul 		/*
1158962315f6SBill Paul 		 * By popular demand, ignore the alignment problems
1159962315f6SBill Paul 		 * on the Intel x86 platform. The performance hit
1160962315f6SBill Paul 		 * incurred due to unaligned accesses is much smaller
1161962315f6SBill Paul 		 * than the hit produced by forcing buffer copies all
1162962315f6SBill Paul 		 * the time, especially with jumbo frames. We still
1163962315f6SBill Paul 		 * need to fix up the alignment everywhere else though.
1164962315f6SBill Paul 		 */
1165ad6c618bSBill Paul #ifdef NGE_FIXUP_RX
1166ad6c618bSBill Paul 		nge_fixup_rx(m);
1167962315f6SBill Paul #endif
1168ce4946daSBill Paul 
1169ce4946daSBill Paul 		ifp->if_ipackets++;
1170ad6c618bSBill Paul 		m->m_pkthdr.rcvif = ifp;
1171ce4946daSBill Paul 
1172ce4946daSBill Paul 		/* Do IP checksum checking. */
1173ce4946daSBill Paul 		if (extsts & NGE_RXEXTSTS_IPPKT)
1174ce4946daSBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1175ce4946daSBill Paul 		if (!(extsts & NGE_RXEXTSTS_IPCSUMERR))
117601702579SBill Paul 			m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
11772195de46SBill Paul 		if ((extsts & NGE_RXEXTSTS_TCPPKT &&
11782195de46SBill Paul 		    !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) ||
11792195de46SBill Paul 		    (extsts & NGE_RXEXTSTS_UDPPKT &&
11802195de46SBill Paul 		    !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) {
11812195de46SBill Paul 			m->m_pkthdr.csum_flags |=
11822195de46SBill Paul 			    CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
1183c9215605SBill Paul 			m->m_pkthdr.csum_data = 0xffff;
11842195de46SBill Paul 		}
1185ce4946daSBill Paul 
1186ce4946daSBill Paul 		/*
1187ce4946daSBill Paul 		 * If we received a packet with a vlan tag, pass it
1188ce4946daSBill Paul 		 * to vlan_input() instead of ether_input().
1189ce4946daSBill Paul 		 */
1190ce4946daSBill Paul 		if (extsts & NGE_RXEXTSTS_VLANPKT) {
119178ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
119278ba57b9SAndre Oppermann 			    ntohs(extsts & NGE_RXEXTSTS_VTCI);
119378ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1194ce4946daSBill Paul 		}
1195ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1196673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
1197ad6c618bSBill Paul 		NGE_LOCK(sc);
1198ce4946daSBill Paul 	}
1199ce4946daSBill Paul 
1200ce4946daSBill Paul 	sc->nge_cdata.nge_rx_prod = i;
1201ce4946daSBill Paul 
1202ce4946daSBill Paul 	return;
1203ce4946daSBill Paul }
1204ce4946daSBill Paul 
1205ce4946daSBill Paul /*
1206ce4946daSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
1207ce4946daSBill Paul  * the list buffers.
1208ce4946daSBill Paul  */
1209ce4946daSBill Paul 
1210eaabec55SAlfred Perlstein static void
1211284c81cbSPyun YongHyeon nge_txeof(struct nge_softc *sc)
1212ce4946daSBill Paul {
12131e73ec7dSRuslan Ermilov 	struct nge_desc		*cur_tx;
1214ce4946daSBill Paul 	struct ifnet		*ifp;
1215ce4946daSBill Paul 	u_int32_t		idx;
1216ce4946daSBill Paul 
1217ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1218fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1219ce4946daSBill Paul 
1220ce4946daSBill Paul 	/*
1221ce4946daSBill Paul 	 * Go through our tx list and free mbufs for those
1222ce4946daSBill Paul 	 * frames that have been transmitted.
1223ce4946daSBill Paul 	 */
1224ce4946daSBill Paul 	idx = sc->nge_cdata.nge_tx_cons;
1225ce4946daSBill Paul 	while (idx != sc->nge_cdata.nge_tx_prod) {
1226ce4946daSBill Paul 		cur_tx = &sc->nge_ldata->nge_tx_list[idx];
1227ce4946daSBill Paul 
1228ce4946daSBill Paul 		if (NGE_OWNDESC(cur_tx))
1229ce4946daSBill Paul 			break;
1230ce4946daSBill Paul 
1231ce4946daSBill Paul 		if (cur_tx->nge_ctl & NGE_CMDSTS_MORE) {
1232ce4946daSBill Paul 			sc->nge_cdata.nge_tx_cnt--;
1233ce4946daSBill Paul 			NGE_INC(idx, NGE_TX_LIST_CNT);
1234ce4946daSBill Paul 			continue;
1235ce4946daSBill Paul 		}
1236ce4946daSBill Paul 
1237ce4946daSBill Paul 		if (!(cur_tx->nge_ctl & NGE_CMDSTS_PKT_OK)) {
1238ce4946daSBill Paul 			ifp->if_oerrors++;
1239ce4946daSBill Paul 			if (cur_tx->nge_txstat & NGE_TXSTAT_EXCESSCOLLS)
1240ce4946daSBill Paul 				ifp->if_collisions++;
1241ce4946daSBill Paul 			if (cur_tx->nge_txstat & NGE_TXSTAT_OUTOFWINCOLL)
1242ce4946daSBill Paul 				ifp->if_collisions++;
1243ce4946daSBill Paul 		}
1244ce4946daSBill Paul 
1245ce4946daSBill Paul 		ifp->if_collisions +=
1246ce4946daSBill Paul 		    (cur_tx->nge_txstat & NGE_TXSTAT_COLLCNT) >> 16;
1247ce4946daSBill Paul 
1248ce4946daSBill Paul 		ifp->if_opackets++;
1249ce4946daSBill Paul 		if (cur_tx->nge_mbuf != NULL) {
1250ce4946daSBill Paul 			m_freem(cur_tx->nge_mbuf);
1251ce4946daSBill Paul 			cur_tx->nge_mbuf = NULL;
125213f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1253ce4946daSBill Paul 		}
1254ce4946daSBill Paul 
1255ce4946daSBill Paul 		sc->nge_cdata.nge_tx_cnt--;
1256ce4946daSBill Paul 		NGE_INC(idx, NGE_TX_LIST_CNT);
1257ce4946daSBill Paul 	}
1258ce4946daSBill Paul 
1259ce4946daSBill Paul 	sc->nge_cdata.nge_tx_cons = idx;
1260ce4946daSBill Paul 
12611e73ec7dSRuslan Ermilov 	if (idx == sc->nge_cdata.nge_tx_prod)
12621e73ec7dSRuslan Ermilov 		ifp->if_timer = 0;
1263ce4946daSBill Paul 
1264ce4946daSBill Paul 	return;
1265ce4946daSBill Paul }
1266ce4946daSBill Paul 
1267eaabec55SAlfred Perlstein static void
1268284c81cbSPyun YongHyeon nge_tick(void *xsc)
1269ce4946daSBill Paul {
1270ce4946daSBill Paul 	struct nge_softc	*sc;
1271ad6c618bSBill Paul 	struct mii_data		*mii;
1272ad6c618bSBill Paul 	struct ifnet		*ifp;
1273ad6c618bSBill Paul 
1274646abee6SJohn Baldwin 	sc = xsc;
1275ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1276fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1277ce4946daSBill Paul 
12781f548804SDoug Ambrisko 	if (sc->nge_tbi) {
12791f548804SDoug Ambrisko 		if (!sc->nge_link) {
12801f548804SDoug Ambrisko 			if (CSR_READ_4(sc, NGE_TBI_BMSR)
12811f548804SDoug Ambrisko 			    & NGE_TBIBMSR_ANEG_DONE) {
12826d6b7a18SPoul-Henning Kamp 				if (bootverbose)
12836b9f5c94SGleb Smirnoff 					device_printf(sc->nge_dev,
1284646abee6SJohn Baldwin 					    "gigabit link up\n");
12851f548804SDoug Ambrisko 				nge_miibus_statchg(sc->nge_miibus);
12861f548804SDoug Ambrisko 				sc->nge_link++;
12871f548804SDoug Ambrisko 				if (ifp->if_snd.ifq_head != NULL)
1288ad6c618bSBill Paul 					nge_start_locked(ifp);
12891f548804SDoug Ambrisko 			}
12901f548804SDoug Ambrisko 		}
12911f548804SDoug Ambrisko 	} else {
1292ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
1293ce4946daSBill Paul 		mii_tick(mii);
1294ce4946daSBill Paul 
1295ce4946daSBill Paul 		if (!sc->nge_link) {
1296ce4946daSBill Paul 			if (mii->mii_media_status & IFM_ACTIVE &&
1297ce4946daSBill Paul 			    IFM_SUBTYPE(mii->mii_media_active) != IFM_NONE) {
1298ce4946daSBill Paul 				sc->nge_link++;
12991f548804SDoug Ambrisko 				if (IFM_SUBTYPE(mii->mii_media_active)
13006d6b7a18SPoul-Henning Kamp 				    == IFM_1000_T && bootverbose)
13016b9f5c94SGleb Smirnoff 					device_printf(sc->nge_dev,
1302646abee6SJohn Baldwin 					    "gigabit link up\n");
1303ce4946daSBill Paul 				if (ifp->if_snd.ifq_head != NULL)
1304ad6c618bSBill Paul 					nge_start_locked(ifp);
1305ce4946daSBill Paul 			}
1306d9fc2b81SPoul-Henning Kamp 		}
13071f548804SDoug Ambrisko 	}
1308ad6c618bSBill Paul 	callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc);
1309ce4946daSBill Paul 
1310ce4946daSBill Paul 	return;
1311ce4946daSBill Paul }
1312ce4946daSBill Paul 
1313196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1314196c0df6SHidetoshi Shimokawa static poll_handler_t nge_poll;
1315196c0df6SHidetoshi Shimokawa 
1316196c0df6SHidetoshi Shimokawa static void
1317196c0df6SHidetoshi Shimokawa nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1318196c0df6SHidetoshi Shimokawa {
1319196c0df6SHidetoshi Shimokawa 	struct  nge_softc *sc = ifp->if_softc;
1320196c0df6SHidetoshi Shimokawa 
1321ad6c618bSBill Paul 	NGE_LOCK(sc);
132240929967SGleb Smirnoff 	if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1323ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1324196c0df6SHidetoshi Shimokawa 		return;
1325196c0df6SHidetoshi Shimokawa 	}
1326196c0df6SHidetoshi Shimokawa 
1327196c0df6SHidetoshi Shimokawa 	/*
1328196c0df6SHidetoshi Shimokawa 	 * On the nge, reading the status register also clears it.
1329196c0df6SHidetoshi Shimokawa 	 * So before returning to intr mode we must make sure that all
1330196c0df6SHidetoshi Shimokawa 	 * possible pending sources of interrupts have been served.
1331196c0df6SHidetoshi Shimokawa 	 * In practice this means run to completion the *eof routines,
1332196c0df6SHidetoshi Shimokawa 	 * and then call the interrupt routine
1333196c0df6SHidetoshi Shimokawa 	 */
1334196c0df6SHidetoshi Shimokawa 	sc->rxcycles = count;
1335196c0df6SHidetoshi Shimokawa 	nge_rxeof(sc);
1336196c0df6SHidetoshi Shimokawa 	nge_txeof(sc);
1337196c0df6SHidetoshi Shimokawa 	if (ifp->if_snd.ifq_head != NULL)
1338ad6c618bSBill Paul 		nge_start_locked(ifp);
1339196c0df6SHidetoshi Shimokawa 
1340196c0df6SHidetoshi Shimokawa 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
1341196c0df6SHidetoshi Shimokawa 		u_int32_t	status;
1342196c0df6SHidetoshi Shimokawa 
1343196c0df6SHidetoshi Shimokawa 		/* Reading the ISR register clears all interrupts. */
1344196c0df6SHidetoshi Shimokawa 		status = CSR_READ_4(sc, NGE_ISR);
1345196c0df6SHidetoshi Shimokawa 
1346196c0df6SHidetoshi Shimokawa 		if (status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW))
1347196c0df6SHidetoshi Shimokawa 			nge_rxeof(sc);
1348196c0df6SHidetoshi Shimokawa 
1349196c0df6SHidetoshi Shimokawa 		if (status & (NGE_ISR_RX_IDLE))
1350196c0df6SHidetoshi Shimokawa 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1351196c0df6SHidetoshi Shimokawa 
1352196c0df6SHidetoshi Shimokawa 		if (status & NGE_ISR_SYSERR) {
1353196c0df6SHidetoshi Shimokawa 			nge_reset(sc);
1354ad6c618bSBill Paul 			nge_init_locked(sc);
1355196c0df6SHidetoshi Shimokawa 		}
1356196c0df6SHidetoshi Shimokawa 	}
1357ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1358196c0df6SHidetoshi Shimokawa }
1359196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1360196c0df6SHidetoshi Shimokawa 
1361eaabec55SAlfred Perlstein static void
1362284c81cbSPyun YongHyeon nge_intr(void *arg)
1363ce4946daSBill Paul {
1364ce4946daSBill Paul 	struct nge_softc	*sc;
1365ce4946daSBill Paul 	struct ifnet		*ifp;
1366ce4946daSBill Paul 	u_int32_t		status;
1367ce4946daSBill Paul 
1368ce4946daSBill Paul 	sc = arg;
1369fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1370ce4946daSBill Paul 
1371ad6c618bSBill Paul 	NGE_LOCK(sc);
1372196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
137340929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING) {
1374ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1375196c0df6SHidetoshi Shimokawa 		return;
1376ad6c618bSBill Paul 	}
137740929967SGleb Smirnoff #endif
1378196c0df6SHidetoshi Shimokawa 
1379ce4946daSBill Paul 	/* Supress unwanted interrupts */
1380ce4946daSBill Paul 	if (!(ifp->if_flags & IFF_UP)) {
1381ce4946daSBill Paul 		nge_stop(sc);
1382ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1383ce4946daSBill Paul 		return;
1384ce4946daSBill Paul 	}
1385ce4946daSBill Paul 
1386ce4946daSBill Paul 	/* Disable interrupts. */
1387ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
1388ce4946daSBill Paul 
13891f548804SDoug Ambrisko 	/* Data LED on for TBI mode */
13901f548804SDoug Ambrisko 	if(sc->nge_tbi)
13911f548804SDoug Ambrisko 		 CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
13921f548804SDoug Ambrisko 			     | NGE_GPIO_GP3_OUT);
13931f548804SDoug Ambrisko 
1394ce4946daSBill Paul 	for (;;) {
1395ce4946daSBill Paul 		/* Reading the ISR register clears all interrupts. */
1396ce4946daSBill Paul 		status = CSR_READ_4(sc, NGE_ISR);
1397ce4946daSBill Paul 
1398ce4946daSBill Paul 		if ((status & NGE_INTRS) == 0)
1399ce4946daSBill Paul 			break;
1400ce4946daSBill Paul 
1401ce4946daSBill Paul 		if ((status & NGE_ISR_TX_DESC_OK) ||
1402ce4946daSBill Paul 		    (status & NGE_ISR_TX_ERR) ||
1403ce4946daSBill Paul 		    (status & NGE_ISR_TX_OK) ||
1404ce4946daSBill Paul 		    (status & NGE_ISR_TX_IDLE))
1405ce4946daSBill Paul 			nge_txeof(sc);
1406ce4946daSBill Paul 
1407ce4946daSBill Paul 		if ((status & NGE_ISR_RX_DESC_OK) ||
1408248fa967SBill Paul 		    (status & NGE_ISR_RX_ERR) ||
14098ce3678aSBill Paul 		    (status & NGE_ISR_RX_OFLOW) ||
1410ff7ed9f7SPoul-Henning Kamp 		    (status & NGE_ISR_RX_FIFO_OFLOW) ||
1411ff7ed9f7SPoul-Henning Kamp 		    (status & NGE_ISR_RX_IDLE) ||
1412ce4946daSBill Paul 		    (status & NGE_ISR_RX_OK))
1413ce4946daSBill Paul 			nge_rxeof(sc);
1414ff7ed9f7SPoul-Henning Kamp 
1415ff7ed9f7SPoul-Henning Kamp 		if ((status & NGE_ISR_RX_IDLE))
1416ff7ed9f7SPoul-Henning Kamp 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1417ff7ed9f7SPoul-Henning Kamp 
1418ce4946daSBill Paul 		if (status & NGE_ISR_SYSERR) {
1419ce4946daSBill Paul 			nge_reset(sc);
142013f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1421ad6c618bSBill Paul 			nge_init_locked(sc);
1422ce4946daSBill Paul 		}
1423ce4946daSBill Paul 
1424d9fc2b81SPoul-Henning Kamp #if 0
1425d9fc2b81SPoul-Henning Kamp 		/*
1426d9fc2b81SPoul-Henning Kamp 		 * XXX: nge_tick() is not ready to be called this way
1427d9fc2b81SPoul-Henning Kamp 		 * it screws up the aneg timeout because mii_tick() is
1428d9fc2b81SPoul-Henning Kamp 		 * only to be called once per second.
1429d9fc2b81SPoul-Henning Kamp 		 */
1430ce4946daSBill Paul 		if (status & NGE_IMR_PHY_INTR) {
1431ce4946daSBill Paul 			sc->nge_link = 0;
1432646abee6SJohn Baldwin 			nge_tick(sc);
1433ce4946daSBill Paul 		}
1434d9fc2b81SPoul-Henning Kamp #endif
1435ce4946daSBill Paul 	}
1436ce4946daSBill Paul 
1437ce4946daSBill Paul 	/* Re-enable interrupts. */
1438ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1439ce4946daSBill Paul 
1440ce4946daSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
1441ad6c618bSBill Paul 		nge_start_locked(ifp);
1442ce4946daSBill Paul 
14431f548804SDoug Ambrisko 	/* Data LED off for TBI mode */
14441f548804SDoug Ambrisko 
14451f548804SDoug Ambrisko 	if(sc->nge_tbi)
14461f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
14471f548804SDoug Ambrisko 			    & ~NGE_GPIO_GP3_OUT);
14481f548804SDoug Ambrisko 
1449ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1450ad6c618bSBill Paul 
1451ce4946daSBill Paul 	return;
1452ce4946daSBill Paul }
1453ce4946daSBill Paul 
1454ce4946daSBill Paul /*
1455ce4946daSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1456ce4946daSBill Paul  * pointers to the fragment pointers.
1457ce4946daSBill Paul  */
1458eaabec55SAlfred Perlstein static int
1459284c81cbSPyun YongHyeon nge_encap(struct nge_softc *sc, struct mbuf *m_head, u_int32_t *txidx)
1460ce4946daSBill Paul {
1461ce4946daSBill Paul 	struct nge_desc		*f = NULL;
1462ce4946daSBill Paul 	struct mbuf		*m;
1463ce4946daSBill Paul 	int			frag, cur, cnt = 0;
1464ce4946daSBill Paul 
1465ce4946daSBill Paul 	/*
1466ce4946daSBill Paul  	 * Start packing the mbufs in this chain into
1467ce4946daSBill Paul 	 * the fragment pointers. Stop when we run out
1468ce4946daSBill Paul  	 * of fragments or hit the end of the mbuf chain.
1469ce4946daSBill Paul 	 */
1470ce4946daSBill Paul 	m = m_head;
1471ce4946daSBill Paul 	cur = frag = *txidx;
1472ce4946daSBill Paul 
1473ce4946daSBill Paul 	for (m = m_head; m != NULL; m = m->m_next) {
1474ce4946daSBill Paul 		if (m->m_len != 0) {
1475ce4946daSBill Paul 			if ((NGE_TX_LIST_CNT -
1476ce4946daSBill Paul 			    (sc->nge_cdata.nge_tx_cnt + cnt)) < 2)
1477ce4946daSBill Paul 				return(ENOBUFS);
1478ce4946daSBill Paul 			f = &sc->nge_ldata->nge_tx_list[frag];
1479ce4946daSBill Paul 			f->nge_ctl = NGE_CMDSTS_MORE | m->m_len;
1480ce4946daSBill Paul 			f->nge_ptr = vtophys(mtod(m, vm_offset_t));
1481ce4946daSBill Paul 			if (cnt != 0)
1482ce4946daSBill Paul 				f->nge_ctl |= NGE_CMDSTS_OWN;
1483ce4946daSBill Paul 			cur = frag;
1484ce4946daSBill Paul 			NGE_INC(frag, NGE_TX_LIST_CNT);
1485ce4946daSBill Paul 			cnt++;
1486ce4946daSBill Paul 		}
1487ce4946daSBill Paul 	}
1488ce4946daSBill Paul 
1489ce4946daSBill Paul 	if (m != NULL)
1490ce4946daSBill Paul 		return(ENOBUFS);
1491ce4946daSBill Paul 
14921bacd83aSBill Paul 	sc->nge_ldata->nge_tx_list[*txidx].nge_extsts = 0;
1493ce4946daSBill Paul 	if (m_head->m_pkthdr.csum_flags) {
1494ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_IP)
14951bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1496ce4946daSBill Paul 			    NGE_TXEXTSTS_IPCSUM;
1497ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_TCP)
14981bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1499ce4946daSBill Paul 			    NGE_TXEXTSTS_TCPCSUM;
1500ce4946daSBill Paul 		if (m_head->m_pkthdr.csum_flags & CSUM_UDP)
15011bacd83aSBill Paul 			sc->nge_ldata->nge_tx_list[*txidx].nge_extsts |=
1502ce4946daSBill Paul 			    NGE_TXEXTSTS_UDPCSUM;
1503ce4946daSBill Paul 	}
1504ce4946daSBill Paul 
150578ba57b9SAndre Oppermann 	if (m_head->m_flags & M_VLANTAG) {
1506ce4946daSBill Paul 		sc->nge_ldata->nge_tx_list[cur].nge_extsts |=
150778ba57b9SAndre Oppermann 		    (NGE_TXEXTSTS_VLANPKT|htons(m_head->m_pkthdr.ether_vtag));
1508ce4946daSBill Paul 	}
1509ce4946daSBill Paul 
1510ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[cur].nge_mbuf = m_head;
1511ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[cur].nge_ctl &= ~NGE_CMDSTS_MORE;
1512ce4946daSBill Paul 	sc->nge_ldata->nge_tx_list[*txidx].nge_ctl |= NGE_CMDSTS_OWN;
1513ce4946daSBill Paul 	sc->nge_cdata.nge_tx_cnt += cnt;
1514ce4946daSBill Paul 	*txidx = frag;
1515ce4946daSBill Paul 
1516ce4946daSBill Paul 	return(0);
1517ce4946daSBill Paul }
1518ce4946daSBill Paul 
1519ce4946daSBill Paul /*
1520ce4946daSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1521ce4946daSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1522ce4946daSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1523ce4946daSBill Paul  * physical addresses.
1524ce4946daSBill Paul  */
1525ce4946daSBill Paul 
1526eaabec55SAlfred Perlstein static void
1527284c81cbSPyun YongHyeon nge_start(struct ifnet *ifp)
1528ce4946daSBill Paul {
1529ce4946daSBill Paul 	struct nge_softc	*sc;
1530ad6c618bSBill Paul 
1531ad6c618bSBill Paul 	sc = ifp->if_softc;
1532ad6c618bSBill Paul 	NGE_LOCK(sc);
1533ad6c618bSBill Paul 	nge_start_locked(ifp);
1534ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1535ad6c618bSBill Paul }
1536ad6c618bSBill Paul 
1537ad6c618bSBill Paul static void
1538284c81cbSPyun YongHyeon nge_start_locked(struct ifnet *ifp)
1539ad6c618bSBill Paul {
1540ad6c618bSBill Paul 	struct nge_softc	*sc;
1541ce4946daSBill Paul 	struct mbuf		*m_head = NULL;
1542ce4946daSBill Paul 	u_int32_t		idx;
1543ce4946daSBill Paul 
1544ce4946daSBill Paul 	sc = ifp->if_softc;
1545ce4946daSBill Paul 
1546ce4946daSBill Paul 	if (!sc->nge_link)
1547ce4946daSBill Paul 		return;
1548ce4946daSBill Paul 
1549ce4946daSBill Paul 	idx = sc->nge_cdata.nge_tx_prod;
1550ce4946daSBill Paul 
155113f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_OACTIVE)
1552ce4946daSBill Paul 		return;
1553ce4946daSBill Paul 
1554ce4946daSBill Paul 	while(sc->nge_ldata->nge_tx_list[idx].nge_mbuf == NULL) {
1555ce4946daSBill Paul 		IF_DEQUEUE(&ifp->if_snd, m_head);
1556ce4946daSBill Paul 		if (m_head == NULL)
1557ce4946daSBill Paul 			break;
1558ce4946daSBill Paul 
1559ce4946daSBill Paul 		if (nge_encap(sc, m_head, &idx)) {
1560ce4946daSBill Paul 			IF_PREPEND(&ifp->if_snd, m_head);
156113f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
1562ce4946daSBill Paul 			break;
1563ce4946daSBill Paul 		}
1564ce4946daSBill Paul 
1565ce4946daSBill Paul 		/*
1566ce4946daSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
1567ce4946daSBill Paul 		 * to him.
1568ce4946daSBill Paul 		 */
156959a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
1570ce4946daSBill Paul 
1571ce4946daSBill Paul 	}
1572ce4946daSBill Paul 
1573ce4946daSBill Paul 	/* Transmit */
1574ce4946daSBill Paul 	sc->nge_cdata.nge_tx_prod = idx;
1575ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE);
1576ce4946daSBill Paul 
1577ce4946daSBill Paul 	/*
1578ce4946daSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
1579ce4946daSBill Paul 	 */
1580ce4946daSBill Paul 	ifp->if_timer = 5;
1581ce4946daSBill Paul 
1582ce4946daSBill Paul 	return;
1583ce4946daSBill Paul }
1584ce4946daSBill Paul 
1585eaabec55SAlfred Perlstein static void
1586284c81cbSPyun YongHyeon nge_init(void *xsc)
1587ce4946daSBill Paul {
1588ce4946daSBill Paul 	struct nge_softc	*sc = xsc;
1589ad6c618bSBill Paul 
1590ad6c618bSBill Paul 	NGE_LOCK(sc);
1591ad6c618bSBill Paul 	nge_init_locked(sc);
1592ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1593ad6c618bSBill Paul }
1594ad6c618bSBill Paul 
1595ad6c618bSBill Paul static void
1596284c81cbSPyun YongHyeon nge_init_locked(struct nge_softc *sc)
1597ad6c618bSBill Paul {
1598fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->nge_ifp;
1599ce4946daSBill Paul 	struct mii_data		*mii;
1600ad6c618bSBill Paul 
1601ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1602ce4946daSBill Paul 
160313f4c340SRobert Watson 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1604ce4946daSBill Paul 		return;
1605ce4946daSBill Paul 
1606ce4946daSBill Paul 	/*
1607ce4946daSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
1608ce4946daSBill Paul 	 */
1609ce4946daSBill Paul 	nge_stop(sc);
1610ce4946daSBill Paul 
16111f548804SDoug Ambrisko 	if (sc->nge_tbi) {
16121f548804SDoug Ambrisko 		mii = NULL;
16131f548804SDoug Ambrisko 	} else {
1614ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
16151f548804SDoug Ambrisko 	}
1616ce4946daSBill Paul 
1617ce4946daSBill Paul 	/* Set MAC address */
1618ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0);
1619ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
16204a0d6638SRuslan Ermilov 	    ((u_int16_t *)IF_LLADDR(sc->nge_ifp))[0]);
1621ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1);
1622ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
16234a0d6638SRuslan Ermilov 	    ((u_int16_t *)IF_LLADDR(sc->nge_ifp))[1]);
1624ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2);
1625ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_DATA,
16264a0d6638SRuslan Ermilov 	    ((u_int16_t *)IF_LLADDR(sc->nge_ifp))[2]);
1627ce4946daSBill Paul 
1628ce4946daSBill Paul 	/* Init circular RX list. */
1629ce4946daSBill Paul 	if (nge_list_rx_init(sc) == ENOBUFS) {
16306b9f5c94SGleb Smirnoff 		device_printf(sc->nge_dev, "initialization failed: no "
1631646abee6SJohn Baldwin 			"memory for rx buffers\n");
1632ce4946daSBill Paul 		nge_stop(sc);
1633ce4946daSBill Paul 		return;
1634ce4946daSBill Paul 	}
1635ce4946daSBill Paul 
1636ce4946daSBill Paul 	/*
1637ce4946daSBill Paul 	 * Init tx descriptors.
1638ce4946daSBill Paul 	 */
1639ce4946daSBill Paul 	nge_list_tx_init(sc);
1640ce4946daSBill Paul 
1641ce4946daSBill Paul 	/*
1642ce4946daSBill Paul 	 * For the NatSemi chip, we have to explicitly enable the
1643ce4946daSBill Paul 	 * reception of ARP frames, as well as turn on the 'perfect
1644ce4946daSBill Paul 	 * match' filter where we store the station address, otherwise
1645ce4946daSBill Paul 	 * we won't receive unicasts meant for this host.
1646ce4946daSBill Paul 	 */
1647ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP);
1648ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT);
1649ce4946daSBill Paul 
1650ce4946daSBill Paul 	 /* If we want promiscuous mode, set the allframes bit. */
1651ce4946daSBill Paul 	if (ifp->if_flags & IFF_PROMISC) {
1652ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1653ce4946daSBill Paul 	} else {
1654ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ALLPHYS);
1655ce4946daSBill Paul 	}
1656ce4946daSBill Paul 
1657ce4946daSBill Paul 	/*
1658ce4946daSBill Paul 	 * Set the capture broadcast bit to capture broadcast frames.
1659ce4946daSBill Paul 	 */
1660ce4946daSBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
1661ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1662ce4946daSBill Paul 	} else {
1663ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
1664ce4946daSBill Paul 	}
1665ce4946daSBill Paul 
1666ce4946daSBill Paul 	/*
1667ce4946daSBill Paul 	 * Load the multicast filter.
1668ce4946daSBill Paul 	 */
1669ce4946daSBill Paul 	nge_setmulti(sc);
1670ce4946daSBill Paul 
1671ce4946daSBill Paul 	/* Turn the receive filter on */
1672ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE);
1673ce4946daSBill Paul 
1674ce4946daSBill Paul 	/*
1675ce4946daSBill Paul 	 * Load the address of the RX and TX lists.
1676ce4946daSBill Paul 	 */
1677ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_LISTPTR,
1678ce4946daSBill Paul 	    vtophys(&sc->nge_ldata->nge_rx_list[0]));
1679ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_LISTPTR,
1680ce4946daSBill Paul 	    vtophys(&sc->nge_ldata->nge_tx_list[0]));
1681ce4946daSBill Paul 
1682ce4946daSBill Paul 	/* Set RX configuration */
1683ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG);
1684ce4946daSBill Paul 	/*
1685ce4946daSBill Paul 	 * Enable hardware checksum validation for all IPv4
1686ce4946daSBill Paul 	 * packets, do not reject packets with bad checksums.
1687ce4946daSBill Paul 	 */
1688ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB);
1689ce4946daSBill Paul 
1690ce4946daSBill Paul 	/*
16919d4fe4b2SBrooks Davis 	 * Tell the chip to detect and strip VLAN tag info from
16929d4fe4b2SBrooks Davis 	 * received frames. The tag will be provided in the extsts
16939d4fe4b2SBrooks Davis 	 * field in the RX descriptors.
1694ce4946daSBill Paul 	 */
1695ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL,
1696ce4946daSBill Paul 	    NGE_VIPRXCTL_TAG_DETECT_ENB|NGE_VIPRXCTL_TAG_STRIP_ENB);
1697ce4946daSBill Paul 
1698ce4946daSBill Paul 	/* Set TX configuration */
1699ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG);
1700ce4946daSBill Paul 
1701ce4946daSBill Paul 	/*
1702ce4946daSBill Paul 	 * Enable TX IPv4 checksumming on a per-packet basis.
1703ce4946daSBill Paul 	 */
1704ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT);
1705ce4946daSBill Paul 
1706ce4946daSBill Paul 	/*
17079d4fe4b2SBrooks Davis 	 * Tell the chip to insert VLAN tags on a per-packet basis as
17089d4fe4b2SBrooks Davis 	 * dictated by the code in the frame encapsulation routine.
1709ce4946daSBill Paul 	 */
1710ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT);
1711ce4946daSBill Paul 
1712ce4946daSBill Paul 	/* Set full/half duplex mode. */
17131f548804SDoug Ambrisko 	if (sc->nge_tbi) {
17141f548804SDoug Ambrisko 		if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
17151f548804SDoug Ambrisko 		    == IFM_FDX) {
17161f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
17171f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
17181f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
17191f548804SDoug Ambrisko 		} else {
17201f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
17211f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
17221f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
17231f548804SDoug Ambrisko 		}
17241f548804SDoug Ambrisko 	} else {
1725ce4946daSBill Paul 		if ((mii->mii_media_active & IFM_GMASK) == IFM_FDX) {
1726ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_TX_CFG,
1727ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1728ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1729ce4946daSBill Paul 		} else {
1730ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
1731ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
1732ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
1733ce4946daSBill Paul 		}
17341f548804SDoug Ambrisko 	}
1735ce4946daSBill Paul 
1736646abee6SJohn Baldwin 	nge_tick(sc);
1737d9fc2b81SPoul-Henning Kamp 
1738ce4946daSBill Paul 	/*
1739ce4946daSBill Paul 	 * Enable the delivery of PHY interrupts based on
174023d3a203SBill Paul 	 * link/speed/duplex status changes. Also enable the
174123d3a203SBill Paul 	 * extsts field in the DMA descriptors (needed for
174223d3a203SBill Paul 	 * TCP/IP checksum offload on transmit).
1743ce4946daSBill Paul 	 */
17442ce0498bSBill Paul 	NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD|
174523d3a203SBill Paul 	    NGE_CFG_PHYINTR_LNK|NGE_CFG_PHYINTR_DUP|NGE_CFG_EXTSTS_ENB);
1746ce4946daSBill Paul 
1747ce4946daSBill Paul 	/*
1748962315f6SBill Paul 	 * Configure interrupt holdoff (moderation). We can
1749962315f6SBill Paul 	 * have the chip delay interrupt delivery for a certain
1750962315f6SBill Paul 	 * period. Units are in 100us, and the max setting
1751962315f6SBill Paul 	 * is 25500us (0xFF x 100us). Default is a 100us holdoff.
1752962315f6SBill Paul 	 */
1753962315f6SBill Paul 	CSR_WRITE_4(sc, NGE_IHR, 0x01);
1754962315f6SBill Paul 
1755962315f6SBill Paul 	/*
1756ce4946daSBill Paul 	 * Enable interrupts.
1757ce4946daSBill Paul 	 */
1758ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS);
1759196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1760196c0df6SHidetoshi Shimokawa 	/*
1761196c0df6SHidetoshi Shimokawa 	 * ... only enable interrupts if we are not polling, make sure
1762196c0df6SHidetoshi Shimokawa 	 * they are off otherwise.
1763196c0df6SHidetoshi Shimokawa 	 */
176440929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
1765196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 0);
1766196c0df6SHidetoshi Shimokawa 	else
176740929967SGleb Smirnoff #endif
1768ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1769ce4946daSBill Paul 
1770ce4946daSBill Paul 	/* Enable receiver and transmitter. */
1771ce4946daSBill Paul 	NGE_CLRBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
1772ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1773ce4946daSBill Paul 
1774646abee6SJohn Baldwin 	nge_ifmedia_upd_locked(ifp);
1775ce4946daSBill Paul 
177613f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
177713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1778ce4946daSBill Paul 
1779ce4946daSBill Paul 	return;
1780ce4946daSBill Paul }
1781ce4946daSBill Paul 
1782ce4946daSBill Paul /*
1783ce4946daSBill Paul  * Set media options.
1784ce4946daSBill Paul  */
1785eaabec55SAlfred Perlstein static int
1786284c81cbSPyun YongHyeon nge_ifmedia_upd(struct ifnet *ifp)
1787ce4946daSBill Paul {
1788ce4946daSBill Paul 	struct nge_softc	*sc;
1789646abee6SJohn Baldwin 
1790646abee6SJohn Baldwin 	sc = ifp->if_softc;
1791646abee6SJohn Baldwin 	NGE_LOCK(sc);
1792646abee6SJohn Baldwin 	nge_ifmedia_upd_locked(ifp);
1793646abee6SJohn Baldwin 	NGE_UNLOCK(sc);
1794646abee6SJohn Baldwin 	return (0);
1795646abee6SJohn Baldwin }
1796646abee6SJohn Baldwin 
1797646abee6SJohn Baldwin static void
1798284c81cbSPyun YongHyeon nge_ifmedia_upd_locked(struct ifnet *ifp)
1799646abee6SJohn Baldwin {
1800646abee6SJohn Baldwin 	struct nge_softc	*sc;
1801ce4946daSBill Paul 	struct mii_data		*mii;
1802ce4946daSBill Paul 
1803ce4946daSBill Paul 	sc = ifp->if_softc;
1804646abee6SJohn Baldwin 	NGE_LOCK_ASSERT(sc);
1805ce4946daSBill Paul 
18061f548804SDoug Ambrisko 	if (sc->nge_tbi) {
18071f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
18081f548804SDoug Ambrisko 		     == IFM_AUTO) {
18091f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR,
18101f548804SDoug Ambrisko 				CSR_READ_4(sc, NGE_TBI_ANAR)
18111f548804SDoug Ambrisko 					| NGE_TBIANAR_HDX | NGE_TBIANAR_FDX
18121f548804SDoug Ambrisko 					| NGE_TBIANAR_PS1 | NGE_TBIANAR_PS2);
18131f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG
18141f548804SDoug Ambrisko 				| NGE_TBIBMCR_RESTART_ANEG);
18151f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, NGE_TBIBMCR_ENABLE_ANEG);
18161f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media
18171f548804SDoug Ambrisko 			    & IFM_GMASK) == IFM_FDX) {
18181f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_TX_CFG,
18191f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
18201f548804SDoug Ambrisko 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
18211f548804SDoug Ambrisko 
18221f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR, 0);
18231f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, 0);
18241f548804SDoug Ambrisko 		} else {
18251f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_TX_CFG,
18261f548804SDoug Ambrisko 			    (NGE_TXCFG_IGN_HBEAT|NGE_TXCFG_IGN_CARR));
18271f548804SDoug Ambrisko 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
18281f548804SDoug Ambrisko 
18291f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_ANAR, 0);
18301f548804SDoug Ambrisko 			CSR_WRITE_4(sc, NGE_TBI_BMCR, 0);
18311f548804SDoug Ambrisko 		}
18321f548804SDoug Ambrisko 
18331f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
18341f548804SDoug Ambrisko 			    & ~NGE_GPIO_GP3_OUT);
18351f548804SDoug Ambrisko 	} else {
1836ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
1837ce4946daSBill Paul 		sc->nge_link = 0;
1838ce4946daSBill Paul 		if (mii->mii_instance) {
1839ce4946daSBill Paul 			struct mii_softc	*miisc;
1840646abee6SJohn Baldwin 
1841646abee6SJohn Baldwin 			LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
1842ce4946daSBill Paul 				mii_phy_reset(miisc);
1843ce4946daSBill Paul 		}
1844ce4946daSBill Paul 		mii_mediachg(mii);
18451f548804SDoug Ambrisko 	}
1846ce4946daSBill Paul }
1847ce4946daSBill Paul 
1848ce4946daSBill Paul /*
1849ce4946daSBill Paul  * Report current media status.
1850ce4946daSBill Paul  */
1851eaabec55SAlfred Perlstein static void
1852284c81cbSPyun YongHyeon nge_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
1853ce4946daSBill Paul {
1854ce4946daSBill Paul 	struct nge_softc	*sc;
1855ce4946daSBill Paul 	struct mii_data		*mii;
1856ce4946daSBill Paul 
1857ce4946daSBill Paul 	sc = ifp->if_softc;
1858ce4946daSBill Paul 
1859646abee6SJohn Baldwin 	NGE_LOCK(sc);
18601f548804SDoug Ambrisko 	if (sc->nge_tbi) {
18611f548804SDoug Ambrisko 		ifmr->ifm_status = IFM_AVALID;
18621f548804SDoug Ambrisko 		ifmr->ifm_active = IFM_ETHER;
18631f548804SDoug Ambrisko 
18641f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) {
18651f548804SDoug Ambrisko 			ifmr->ifm_status |= IFM_ACTIVE;
18661f548804SDoug Ambrisko 		}
18671f548804SDoug Ambrisko 		if (CSR_READ_4(sc, NGE_TBI_BMCR) & NGE_TBIBMCR_LOOPBACK)
18681f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_LOOP;
18691f548804SDoug Ambrisko 		if (!CSR_READ_4(sc, NGE_TBI_BMSR) & NGE_TBIBMSR_ANEG_DONE) {
18701f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_NONE;
18711f548804SDoug Ambrisko 			ifmr->ifm_status = 0;
1872646abee6SJohn Baldwin 			NGE_UNLOCK(sc);
18731f548804SDoug Ambrisko 			return;
18741f548804SDoug Ambrisko 		}
18751f548804SDoug Ambrisko 		ifmr->ifm_active |= IFM_1000_SX;
18761f548804SDoug Ambrisko 		if (IFM_SUBTYPE(sc->nge_ifmedia.ifm_cur->ifm_media)
18771f548804SDoug Ambrisko 		    == IFM_AUTO) {
18781f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_AUTO;
18791f548804SDoug Ambrisko 			if (CSR_READ_4(sc, NGE_TBI_ANLPAR)
18801f548804SDoug Ambrisko 			    & NGE_TBIANAR_FDX) {
18811f548804SDoug Ambrisko 				ifmr->ifm_active |= IFM_FDX;
18821f548804SDoug Ambrisko 			}else if (CSR_READ_4(sc, NGE_TBI_ANLPAR)
18831f548804SDoug Ambrisko 				  & NGE_TBIANAR_HDX) {
18841f548804SDoug Ambrisko 				ifmr->ifm_active |= IFM_HDX;
18851f548804SDoug Ambrisko 			}
18861f548804SDoug Ambrisko 		} else if ((sc->nge_ifmedia.ifm_cur->ifm_media & IFM_GMASK)
18871f548804SDoug Ambrisko 			== IFM_FDX)
18881f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_FDX;
18891f548804SDoug Ambrisko 		else
18901f548804SDoug Ambrisko 			ifmr->ifm_active |= IFM_HDX;
18911f548804SDoug Ambrisko 
18921f548804SDoug Ambrisko 	} else {
1893ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
1894ce4946daSBill Paul 		mii_pollstat(mii);
1895ce4946daSBill Paul 		ifmr->ifm_active = mii->mii_media_active;
1896ce4946daSBill Paul 		ifmr->ifm_status = mii->mii_media_status;
18971f548804SDoug Ambrisko 	}
1898646abee6SJohn Baldwin 	NGE_UNLOCK(sc);
1899ce4946daSBill Paul 
1900ce4946daSBill Paul 	return;
1901ce4946daSBill Paul }
1902ce4946daSBill Paul 
1903eaabec55SAlfred Perlstein static int
1904284c81cbSPyun YongHyeon nge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
1905ce4946daSBill Paul {
1906ce4946daSBill Paul 	struct nge_softc	*sc = ifp->if_softc;
1907ce4946daSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
1908ce4946daSBill Paul 	struct mii_data		*mii;
1909ad6c618bSBill Paul 	int			error = 0;
1910ce4946daSBill Paul 
1911ce4946daSBill Paul 	switch(command) {
1912ce4946daSBill Paul 	case SIOCSIFMTU:
1913ce4946daSBill Paul 		if (ifr->ifr_mtu > NGE_JUMBO_MTU)
1914ce4946daSBill Paul 			error = EINVAL;
1915cb2f755cSBill Paul 		else {
1916646abee6SJohn Baldwin 			NGE_LOCK(sc);
1917ce4946daSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
1918cb2f755cSBill Paul 			/*
1919cb2f755cSBill Paul 			 * Workaround: if the MTU is larger than
1920cb2f755cSBill Paul 			 * 8152 (TX FIFO size minus 64 minus 18), turn off
1921cb2f755cSBill Paul 			 * TX checksum offloading.
1922cb2f755cSBill Paul 			 */
1923a5820ecbSYaroslav Tykhiy 			if (ifr->ifr_mtu >= 8152) {
1924a5820ecbSYaroslav Tykhiy 				ifp->if_capenable &= ~IFCAP_TXCSUM;
1925cb2f755cSBill Paul 				ifp->if_hwassist = 0;
1926a5820ecbSYaroslav Tykhiy 			} else {
1927a5820ecbSYaroslav Tykhiy 				ifp->if_capenable |= IFCAP_TXCSUM;
1928cb2f755cSBill Paul 				ifp->if_hwassist = NGE_CSUM_FEATURES;
1929cb2f755cSBill Paul 			}
1930646abee6SJohn Baldwin 			NGE_UNLOCK(sc);
1931a5820ecbSYaroslav Tykhiy 		}
1932ce4946daSBill Paul 		break;
1933ce4946daSBill Paul 	case SIOCSIFFLAGS:
1934ad6c618bSBill Paul 		NGE_LOCK(sc);
1935ce4946daSBill Paul 		if (ifp->if_flags & IFF_UP) {
193613f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1937ce4946daSBill Paul 			    ifp->if_flags & IFF_PROMISC &&
1938ce4946daSBill Paul 			    !(sc->nge_if_flags & IFF_PROMISC)) {
1939ce4946daSBill Paul 				NGE_SETBIT(sc, NGE_RXFILT_CTL,
1940ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLPHYS|
1941ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLMULTI);
194213f4c340SRobert Watson 			} else if (ifp->if_drv_flags & IFF_DRV_RUNNING &&
1943ce4946daSBill Paul 			    !(ifp->if_flags & IFF_PROMISC) &&
1944ce4946daSBill Paul 			    sc->nge_if_flags & IFF_PROMISC) {
1945ce4946daSBill Paul 				NGE_CLRBIT(sc, NGE_RXFILT_CTL,
1946ce4946daSBill Paul 				    NGE_RXFILTCTL_ALLPHYS);
1947ce4946daSBill Paul 				if (!(ifp->if_flags & IFF_ALLMULTI))
1948ce4946daSBill Paul 					NGE_CLRBIT(sc, NGE_RXFILT_CTL,
1949ce4946daSBill Paul 					    NGE_RXFILTCTL_ALLMULTI);
1950ce4946daSBill Paul 			} else {
195113f4c340SRobert Watson 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1952ad6c618bSBill Paul 				nge_init_locked(sc);
1953ce4946daSBill Paul 			}
1954ce4946daSBill Paul 		} else {
195513f4c340SRobert Watson 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1956ce4946daSBill Paul 				nge_stop(sc);
1957ce4946daSBill Paul 		}
1958ce4946daSBill Paul 		sc->nge_if_flags = ifp->if_flags;
1959ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1960ce4946daSBill Paul 		error = 0;
1961ce4946daSBill Paul 		break;
1962ce4946daSBill Paul 	case SIOCADDMULTI:
1963ce4946daSBill Paul 	case SIOCDELMULTI:
1964ad6c618bSBill Paul 		NGE_LOCK(sc);
1965ce4946daSBill Paul 		nge_setmulti(sc);
1966ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1967ce4946daSBill Paul 		error = 0;
1968ce4946daSBill Paul 		break;
1969ce4946daSBill Paul 	case SIOCGIFMEDIA:
1970ce4946daSBill Paul 	case SIOCSIFMEDIA:
19711f548804SDoug Ambrisko 		if (sc->nge_tbi) {
19721f548804SDoug Ambrisko 			error = ifmedia_ioctl(ifp, ifr, &sc->nge_ifmedia,
19731f548804SDoug Ambrisko 					      command);
19741f548804SDoug Ambrisko 		} else {
1975ce4946daSBill Paul 			mii = device_get_softc(sc->nge_miibus);
19761f548804SDoug Ambrisko 			error = ifmedia_ioctl(ifp, ifr, &mii->mii_media,
19771f548804SDoug Ambrisko 					      command);
19781f548804SDoug Ambrisko 		}
1979ce4946daSBill Paul 		break;
198037f5f239SRuslan Ermilov 	case SIOCSIFCAP:
198140929967SGleb Smirnoff #ifdef DEVICE_POLLING
198240929967SGleb Smirnoff 		if (ifr->ifr_reqcap & IFCAP_POLLING &&
198340929967SGleb Smirnoff 		    !(ifp->if_capenable & IFCAP_POLLING)) {
198440929967SGleb Smirnoff 			error = ether_poll_register(nge_poll, ifp);
198540929967SGleb Smirnoff 			if (error)
198640929967SGleb Smirnoff 				return(error);
198740929967SGleb Smirnoff 			NGE_LOCK(sc);
198840929967SGleb Smirnoff 			/* Disable interrupts */
198940929967SGleb Smirnoff 			CSR_WRITE_4(sc, NGE_IER, 0);
199040929967SGleb Smirnoff 			ifp->if_capenable |= IFCAP_POLLING;
199140929967SGleb Smirnoff 			NGE_UNLOCK(sc);
199240929967SGleb Smirnoff 			return (error);
199340929967SGleb Smirnoff 
199440929967SGleb Smirnoff 		}
199540929967SGleb Smirnoff 		if (!(ifr->ifr_reqcap & IFCAP_POLLING) &&
199640929967SGleb Smirnoff 		    ifp->if_capenable & IFCAP_POLLING) {
199740929967SGleb Smirnoff 			error = ether_poll_deregister(ifp);
199840929967SGleb Smirnoff 			/* Enable interrupts. */
199940929967SGleb Smirnoff 			NGE_LOCK(sc);
200040929967SGleb Smirnoff 			CSR_WRITE_4(sc, NGE_IER, 1);
200125fbb2c3SYaroslav Tykhiy 			ifp->if_capenable &= ~IFCAP_POLLING;
200240929967SGleb Smirnoff 			NGE_UNLOCK(sc);
200340929967SGleb Smirnoff 			return (error);
200440929967SGleb Smirnoff 		}
200540929967SGleb Smirnoff #endif /* DEVICE_POLLING */
200637f5f239SRuslan Ermilov 		break;
2007ce4946daSBill Paul 	default:
2008673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2009ce4946daSBill Paul 		break;
2010ce4946daSBill Paul 	}
2011ce4946daSBill Paul 
2012ce4946daSBill Paul 	return(error);
2013ce4946daSBill Paul }
2014ce4946daSBill Paul 
2015eaabec55SAlfred Perlstein static void
2016284c81cbSPyun YongHyeon nge_watchdog(struct ifnet *ifp)
2017ce4946daSBill Paul {
2018ce4946daSBill Paul 	struct nge_softc	*sc;
2019ce4946daSBill Paul 
2020ce4946daSBill Paul 	sc = ifp->if_softc;
2021ce4946daSBill Paul 
2022ce4946daSBill Paul 	ifp->if_oerrors++;
20236b9f5c94SGleb Smirnoff 	if_printf(ifp, "watchdog timeout\n");
2024ce4946daSBill Paul 
2025ad6c618bSBill Paul 	NGE_LOCK(sc);
2026ce4946daSBill Paul 	nge_stop(sc);
2027ce4946daSBill Paul 	nge_reset(sc);
202813f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2029ad6c618bSBill Paul 	nge_init_locked(sc);
2030ce4946daSBill Paul 
2031ce4946daSBill Paul 	if (ifp->if_snd.ifq_head != NULL)
2032ad6c618bSBill Paul 		nge_start_locked(ifp);
2033ad6c618bSBill Paul 
2034ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2035ce4946daSBill Paul 
2036ce4946daSBill Paul 	return;
2037ce4946daSBill Paul }
2038ce4946daSBill Paul 
2039ce4946daSBill Paul /*
2040ce4946daSBill Paul  * Stop the adapter and free any mbufs allocated to the
2041ce4946daSBill Paul  * RX and TX lists.
2042ce4946daSBill Paul  */
2043eaabec55SAlfred Perlstein static void
2044284c81cbSPyun YongHyeon nge_stop(struct nge_softc *sc)
2045ce4946daSBill Paul {
2046ce4946daSBill Paul 	register int		i;
2047ce4946daSBill Paul 	struct ifnet		*ifp;
2048ce4946daSBill Paul 	struct mii_data		*mii;
2049ce4946daSBill Paul 
2050ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
2051fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
2052ce4946daSBill Paul 	ifp->if_timer = 0;
20531f548804SDoug Ambrisko 	if (sc->nge_tbi) {
20541f548804SDoug Ambrisko 		mii = NULL;
20551f548804SDoug Ambrisko 	} else {
2056ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
20571f548804SDoug Ambrisko 	}
2058ce4946daSBill Paul 
2059ad6c618bSBill Paul 	callout_stop(&sc->nge_stat_ch);
2060ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
2061ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, 0);
2062ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_DISABLE|NGE_CSR_RX_DISABLE);
2063ce4946daSBill Paul 	DELAY(1000);
2064ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_LISTPTR, 0);
2065ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_LISTPTR, 0);
2066ce4946daSBill Paul 
20671f548804SDoug Ambrisko 	if (!sc->nge_tbi)
2068279fe8d1SPoul-Henning Kamp 		mii_down(mii);
2069ce4946daSBill Paul 
2070ce4946daSBill Paul 	sc->nge_link = 0;
2071ce4946daSBill Paul 
2072ce4946daSBill Paul 	/*
2073ce4946daSBill Paul 	 * Free data in the RX lists.
2074ce4946daSBill Paul 	 */
2075ce4946daSBill Paul 	for (i = 0; i < NGE_RX_LIST_CNT; i++) {
2076ce4946daSBill Paul 		if (sc->nge_ldata->nge_rx_list[i].nge_mbuf != NULL) {
2077ce4946daSBill Paul 			m_freem(sc->nge_ldata->nge_rx_list[i].nge_mbuf);
2078ce4946daSBill Paul 			sc->nge_ldata->nge_rx_list[i].nge_mbuf = NULL;
2079ce4946daSBill Paul 		}
2080ce4946daSBill Paul 	}
2081ce4946daSBill Paul 	bzero((char *)&sc->nge_ldata->nge_rx_list,
2082ce4946daSBill Paul 		sizeof(sc->nge_ldata->nge_rx_list));
2083ce4946daSBill Paul 
2084ce4946daSBill Paul 	/*
2085ce4946daSBill Paul 	 * Free the TX list buffers.
2086ce4946daSBill Paul 	 */
2087ce4946daSBill Paul 	for (i = 0; i < NGE_TX_LIST_CNT; i++) {
2088ce4946daSBill Paul 		if (sc->nge_ldata->nge_tx_list[i].nge_mbuf != NULL) {
2089ce4946daSBill Paul 			m_freem(sc->nge_ldata->nge_tx_list[i].nge_mbuf);
2090ce4946daSBill Paul 			sc->nge_ldata->nge_tx_list[i].nge_mbuf = NULL;
2091ce4946daSBill Paul 		}
2092ce4946daSBill Paul 	}
2093ce4946daSBill Paul 
2094ce4946daSBill Paul 	bzero((char *)&sc->nge_ldata->nge_tx_list,
2095ce4946daSBill Paul 		sizeof(sc->nge_ldata->nge_tx_list));
2096ce4946daSBill Paul 
209713f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2098ce4946daSBill Paul 
2099ce4946daSBill Paul 	return;
2100ce4946daSBill Paul }
2101ce4946daSBill Paul 
2102ce4946daSBill Paul /*
2103ce4946daSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2104ce4946daSBill Paul  * get confused by errant DMAs when rebooting.
2105ce4946daSBill Paul  */
21066a087a87SPyun YongHyeon static int
2107284c81cbSPyun YongHyeon nge_shutdown(device_t dev)
2108ce4946daSBill Paul {
2109ce4946daSBill Paul 	struct nge_softc	*sc;
2110ce4946daSBill Paul 
2111ce4946daSBill Paul 	sc = device_get_softc(dev);
2112ce4946daSBill Paul 
2113ad6c618bSBill Paul 	NGE_LOCK(sc);
2114ce4946daSBill Paul 	nge_reset(sc);
2115ce4946daSBill Paul 	nge_stop(sc);
2116ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2117ce4946daSBill Paul 
21186a087a87SPyun YongHyeon 	return (0);
2119ce4946daSBill Paul }
2120