xref: /freebsd/sys/dev/nge/if_nge.c (revision c6499eccad497913a5025fbde8ae76da70e08043)
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>
97f6bc9430SPyun YongHyeon #include <sys/bus.h>
98f6bc9430SPyun YongHyeon #include <sys/endian.h>
99ce4946daSBill Paul #include <sys/kernel.h>
100f6bc9430SPyun YongHyeon #include <sys/lock.h>
101f6bc9430SPyun YongHyeon #include <sys/malloc.h>
102f6bc9430SPyun YongHyeon #include <sys/mbuf.h>
103f6bc9430SPyun YongHyeon #include <sys/module.h>
104f6bc9430SPyun YongHyeon #include <sys/mutex.h>
105f6bc9430SPyun YongHyeon #include <sys/rman.h>
106ce4946daSBill Paul #include <sys/socket.h>
107f6bc9430SPyun YongHyeon #include <sys/sockio.h>
108f6bc9430SPyun YongHyeon #include <sys/sysctl.h>
109ce4946daSBill Paul 
110f6bc9430SPyun YongHyeon #include <net/bpf.h>
111ce4946daSBill Paul #include <net/if.h>
112ce4946daSBill Paul #include <net/if_arp.h>
113ce4946daSBill Paul #include <net/ethernet.h>
114ce4946daSBill Paul #include <net/if_dl.h>
115ce4946daSBill Paul #include <net/if_media.h>
116ce4946daSBill Paul #include <net/if_types.h>
117ce4946daSBill Paul #include <net/if_vlan_var.h>
118ce4946daSBill Paul 
119ce4946daSBill Paul #include <dev/mii/mii.h>
1208c1093fcSMarius Strobl #include <dev/mii/mii_bitbang.h>
121ce4946daSBill Paul #include <dev/mii/miivar.h>
122ce4946daSBill Paul 
12338d8c994SWarner Losh #include <dev/pci/pcireg.h>
12438d8c994SWarner Losh #include <dev/pci/pcivar.h>
125ce4946daSBill Paul 
126f6bc9430SPyun YongHyeon #include <machine/bus.h>
127ce4946daSBill Paul 
1285da751e4SBill Paul #include <dev/nge/if_ngereg.h>
129ce4946daSBill Paul 
130f6bc9430SPyun YongHyeon /* "device miibus" required.  See GENERIC if you get errors here. */
131f6bc9430SPyun YongHyeon #include "miibus_if.h"
132f6bc9430SPyun YongHyeon 
133f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, pci, 1, 1, 1);
134f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, ether, 1, 1, 1);
135ce4946daSBill Paul MODULE_DEPEND(nge, miibus, 1, 1, 1);
136ce4946daSBill Paul 
137ce4946daSBill Paul #define NGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
138ce4946daSBill Paul 
139ce4946daSBill Paul /*
140ce4946daSBill Paul  * Various supported device vendors/types and their names.
141ce4946daSBill Paul  */
14229658c96SDimitry Andric static const struct nge_type nge_devs[] = {
143ce4946daSBill Paul 	{ NGE_VENDORID, NGE_DEVICEID,
144ce4946daSBill Paul 	    "National Semiconductor Gigabit Ethernet" },
145ce4946daSBill Paul 	{ 0, 0, NULL }
146ce4946daSBill Paul };
147ce4946daSBill Paul 
148e51a25f8SAlfred Perlstein static int nge_probe(device_t);
149e51a25f8SAlfred Perlstein static int nge_attach(device_t);
150e51a25f8SAlfred Perlstein static int nge_detach(device_t);
151f6bc9430SPyun YongHyeon static int nge_shutdown(device_t);
152f6bc9430SPyun YongHyeon static int nge_suspend(device_t);
153f6bc9430SPyun YongHyeon static int nge_resume(device_t);
154ce4946daSBill Paul 
155f6bc9430SPyun YongHyeon static __inline void nge_discard_rxbuf(struct nge_softc *, int);
156f6bc9430SPyun YongHyeon static int nge_newbuf(struct nge_softc *, int);
157f6bc9430SPyun YongHyeon static int nge_encap(struct nge_softc *, struct mbuf **);
158f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
159ad6c618bSBill Paul static __inline void nge_fixup_rx(struct mbuf *);
160ad6c618bSBill Paul #endif
16156e13f2aSAttilio Rao static int nge_rxeof(struct nge_softc *);
162e51a25f8SAlfred Perlstein static void nge_txeof(struct nge_softc *);
163e51a25f8SAlfred Perlstein static void nge_intr(void *);
164e51a25f8SAlfred Perlstein static void nge_tick(void *);
165f6bc9430SPyun YongHyeon static void nge_stats_update(struct nge_softc *);
166e51a25f8SAlfred Perlstein static void nge_start(struct ifnet *);
167ad6c618bSBill Paul static void nge_start_locked(struct ifnet *);
168e51a25f8SAlfred Perlstein static int nge_ioctl(struct ifnet *, u_long, caddr_t);
169e51a25f8SAlfred Perlstein static void nge_init(void *);
170ad6c618bSBill Paul static void nge_init_locked(struct nge_softc *);
171f6bc9430SPyun YongHyeon static int nge_stop_mac(struct nge_softc *);
172e51a25f8SAlfred Perlstein static void nge_stop(struct nge_softc *);
173f6bc9430SPyun YongHyeon static void nge_wol(struct nge_softc *);
174f6bc9430SPyun YongHyeon static void nge_watchdog(struct nge_softc *);
175f6bc9430SPyun YongHyeon static int nge_mediachange(struct ifnet *);
176f6bc9430SPyun YongHyeon static void nge_mediastatus(struct ifnet *, struct ifmediareq *);
177ce4946daSBill Paul 
178e51a25f8SAlfred Perlstein static void nge_delay(struct nge_softc *);
179e51a25f8SAlfred Perlstein static void nge_eeprom_idle(struct nge_softc *);
180e51a25f8SAlfred Perlstein static void nge_eeprom_putbyte(struct nge_softc *, int);
1813929ff51SPyun YongHyeon static void nge_eeprom_getword(struct nge_softc *, int, uint16_t *);
182f6bc9430SPyun YongHyeon static void nge_read_eeprom(struct nge_softc *, caddr_t, int, int);
183ce4946daSBill Paul 
184e51a25f8SAlfred Perlstein static int nge_miibus_readreg(device_t, int, int);
185e51a25f8SAlfred Perlstein static int nge_miibus_writereg(device_t, int, int, int);
186e51a25f8SAlfred Perlstein static void nge_miibus_statchg(device_t);
187ce4946daSBill Paul 
188f6bc9430SPyun YongHyeon static void nge_rxfilter(struct nge_softc *);
189e51a25f8SAlfred Perlstein static void nge_reset(struct nge_softc *);
190f6bc9430SPyun YongHyeon static void nge_dmamap_cb(void *, bus_dma_segment_t *, int, int);
191f6bc9430SPyun YongHyeon static int nge_dma_alloc(struct nge_softc *);
192f6bc9430SPyun YongHyeon static void nge_dma_free(struct nge_softc *);
193e51a25f8SAlfred Perlstein static int nge_list_rx_init(struct nge_softc *);
194e51a25f8SAlfred Perlstein static int nge_list_tx_init(struct nge_softc *);
195f6bc9430SPyun YongHyeon static void nge_sysctl_node(struct nge_softc *);
196f6bc9430SPyun YongHyeon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
197f6bc9430SPyun YongHyeon static int sysctl_hw_nge_int_holdoff(SYSCTL_HANDLER_ARGS);
198ce4946daSBill Paul 
1998c1093fcSMarius Strobl /*
2008c1093fcSMarius Strobl  * MII bit-bang glue
2018c1093fcSMarius Strobl  */
2028c1093fcSMarius Strobl static uint32_t nge_mii_bitbang_read(device_t);
2038c1093fcSMarius Strobl static void nge_mii_bitbang_write(device_t, uint32_t);
2048c1093fcSMarius Strobl 
2058c1093fcSMarius Strobl static const struct mii_bitbang_ops nge_mii_bitbang_ops = {
2068c1093fcSMarius Strobl 	nge_mii_bitbang_read,
2078c1093fcSMarius Strobl 	nge_mii_bitbang_write,
2088c1093fcSMarius Strobl 	{
2098c1093fcSMarius Strobl 		NGE_MEAR_MII_DATA,	/* MII_BIT_MDO */
2108c1093fcSMarius Strobl 		NGE_MEAR_MII_DATA,	/* MII_BIT_MDI */
2118c1093fcSMarius Strobl 		NGE_MEAR_MII_CLK,	/* MII_BIT_MDC */
2128c1093fcSMarius Strobl 		NGE_MEAR_MII_DIR,	/* MII_BIT_DIR_HOST_PHY */
2138c1093fcSMarius Strobl 		0,			/* MII_BIT_DIR_PHY_HOST */
2148c1093fcSMarius Strobl 	}
2158c1093fcSMarius Strobl };
2168c1093fcSMarius Strobl 
217ce4946daSBill Paul static device_method_t nge_methods[] = {
218ce4946daSBill Paul 	/* Device interface */
219ce4946daSBill Paul 	DEVMETHOD(device_probe,		nge_probe),
220ce4946daSBill Paul 	DEVMETHOD(device_attach,	nge_attach),
221ce4946daSBill Paul 	DEVMETHOD(device_detach,	nge_detach),
222ce4946daSBill Paul 	DEVMETHOD(device_shutdown,	nge_shutdown),
223f6bc9430SPyun YongHyeon 	DEVMETHOD(device_suspend,	nge_suspend),
224f6bc9430SPyun YongHyeon 	DEVMETHOD(device_resume,	nge_resume),
225ce4946daSBill Paul 
226ce4946daSBill Paul 	/* MII interface */
227ce4946daSBill Paul 	DEVMETHOD(miibus_readreg,	nge_miibus_readreg),
228ce4946daSBill Paul 	DEVMETHOD(miibus_writereg,	nge_miibus_writereg),
229ce4946daSBill Paul 	DEVMETHOD(miibus_statchg,	nge_miibus_statchg),
230ce4946daSBill Paul 
2314b7ec270SMarius Strobl 	DEVMETHOD_END
232ce4946daSBill Paul };
233ce4946daSBill Paul 
234ce4946daSBill Paul static driver_t nge_driver = {
235ce4946daSBill Paul 	"nge",
236ce4946daSBill Paul 	nge_methods,
237ce4946daSBill Paul 	sizeof(struct nge_softc)
238ce4946daSBill Paul };
239ce4946daSBill Paul 
240ce4946daSBill Paul static devclass_t nge_devclass;
241ce4946daSBill Paul 
242f246e4a1SMatthew N. Dodd DRIVER_MODULE(nge, pci, nge_driver, nge_devclass, 0, 0);
243ce4946daSBill Paul DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0);
244ce4946daSBill Paul 
245ce4946daSBill Paul #define NGE_SETBIT(sc, reg, x)				\
246ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
247ce4946daSBill Paul 		CSR_READ_4(sc, reg) | (x))
248ce4946daSBill Paul 
249ce4946daSBill Paul #define NGE_CLRBIT(sc, reg, x)				\
250ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
251ce4946daSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
252ce4946daSBill Paul 
253ce4946daSBill Paul #define SIO_SET(x)					\
25429f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x))
255ce4946daSBill Paul 
256ce4946daSBill Paul #define SIO_CLR(x)					\
25729f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x))
258ce4946daSBill Paul 
259eaabec55SAlfred Perlstein static void
260284c81cbSPyun YongHyeon nge_delay(struct nge_softc *sc)
261ce4946daSBill Paul {
262ce4946daSBill Paul 	int idx;
263ce4946daSBill Paul 
264ce4946daSBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
265ce4946daSBill Paul 		CSR_READ_4(sc, NGE_CSR);
266ce4946daSBill Paul }
267ce4946daSBill Paul 
268eaabec55SAlfred Perlstein static void
269284c81cbSPyun YongHyeon nge_eeprom_idle(struct nge_softc *sc)
270ce4946daSBill Paul {
2712cf2d799SPyun YongHyeon 	int i;
272ce4946daSBill Paul 
273ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
274ce4946daSBill Paul 	nge_delay(sc);
275ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CLK);
276ce4946daSBill Paul 	nge_delay(sc);
277ce4946daSBill Paul 
278ce4946daSBill Paul 	for (i = 0; i < 25; i++) {
279ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
280ce4946daSBill Paul 		nge_delay(sc);
281ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
282ce4946daSBill Paul 		nge_delay(sc);
283ce4946daSBill Paul 	}
284ce4946daSBill Paul 
285ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
286ce4946daSBill Paul 	nge_delay(sc);
287ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CSEL);
288ce4946daSBill Paul 	nge_delay(sc);
289ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0x00000000);
290ce4946daSBill Paul }
291ce4946daSBill Paul 
292ce4946daSBill Paul /*
293ce4946daSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
294ce4946daSBill Paul  */
295eaabec55SAlfred Perlstein static void
296284c81cbSPyun YongHyeon nge_eeprom_putbyte(struct nge_softc *sc, int addr)
297ce4946daSBill Paul {
2982cf2d799SPyun YongHyeon 	int d, i;
299ce4946daSBill Paul 
300ce4946daSBill Paul 	d = addr | NGE_EECMD_READ;
301ce4946daSBill Paul 
302ce4946daSBill Paul 	/*
303ce4946daSBill Paul 	 * Feed in each bit and stobe the clock.
304ce4946daSBill Paul 	 */
305ce4946daSBill Paul 	for (i = 0x400; i; i >>= 1) {
306ce4946daSBill Paul 		if (d & i) {
307ce4946daSBill Paul 			SIO_SET(NGE_MEAR_EE_DIN);
308ce4946daSBill Paul 		} else {
309ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_EE_DIN);
310ce4946daSBill Paul 		}
311ce4946daSBill Paul 		nge_delay(sc);
312ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
313ce4946daSBill Paul 		nge_delay(sc);
314ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
315ce4946daSBill Paul 		nge_delay(sc);
316ce4946daSBill Paul 	}
317ce4946daSBill Paul }
318ce4946daSBill Paul 
319ce4946daSBill Paul /*
320ce4946daSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
321ce4946daSBill Paul  */
322eaabec55SAlfred Perlstein static void
3233929ff51SPyun YongHyeon nge_eeprom_getword(struct nge_softc *sc, int addr, uint16_t *dest)
324ce4946daSBill Paul {
3252cf2d799SPyun YongHyeon 	int i;
3263929ff51SPyun YongHyeon 	uint16_t word = 0;
327ce4946daSBill Paul 
328ce4946daSBill Paul 	/* Force EEPROM to idle state. */
329ce4946daSBill Paul 	nge_eeprom_idle(sc);
330ce4946daSBill Paul 
331ce4946daSBill Paul 	/* Enter EEPROM access mode. */
332ce4946daSBill Paul 	nge_delay(sc);
333ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
334ce4946daSBill Paul 	nge_delay(sc);
335ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
336ce4946daSBill Paul 	nge_delay(sc);
337ce4946daSBill Paul 
338ce4946daSBill Paul 	/*
339ce4946daSBill Paul 	 * Send address of word we want to read.
340ce4946daSBill Paul 	 */
341ce4946daSBill Paul 	nge_eeprom_putbyte(sc, addr);
342ce4946daSBill Paul 
343ce4946daSBill Paul 	/*
344ce4946daSBill Paul 	 * Start reading bits from EEPROM.
345ce4946daSBill Paul 	 */
346ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
347ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
348ce4946daSBill Paul 		nge_delay(sc);
349ce4946daSBill Paul 		if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT)
350ce4946daSBill Paul 			word |= i;
351ce4946daSBill Paul 		nge_delay(sc);
352ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
353ce4946daSBill Paul 		nge_delay(sc);
354ce4946daSBill Paul 	}
355ce4946daSBill Paul 
356ce4946daSBill Paul 	/* Turn off EEPROM access mode. */
357ce4946daSBill Paul 	nge_eeprom_idle(sc);
358ce4946daSBill Paul 
359ce4946daSBill Paul 	*dest = word;
360ce4946daSBill Paul }
361ce4946daSBill Paul 
362ce4946daSBill Paul /*
363ce4946daSBill Paul  * Read a sequence of words from the EEPROM.
364ce4946daSBill Paul  */
365eaabec55SAlfred Perlstein static void
366f6bc9430SPyun YongHyeon nge_read_eeprom(struct nge_softc *sc, caddr_t dest, int off, int cnt)
367ce4946daSBill Paul {
368ce4946daSBill Paul 	int i;
3693929ff51SPyun YongHyeon 	uint16_t word = 0, *ptr;
370ce4946daSBill Paul 
371ce4946daSBill Paul 	for (i = 0; i < cnt; i++) {
372ce4946daSBill Paul 		nge_eeprom_getword(sc, off + i, &word);
3733929ff51SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
374ce4946daSBill Paul 		*ptr = word;
375ce4946daSBill Paul 	}
376ce4946daSBill Paul }
377ce4946daSBill Paul 
378ce4946daSBill Paul /*
3798c1093fcSMarius Strobl  * Read the MII serial port for the MII bit-bang module.
3808c1093fcSMarius Strobl  */
3818c1093fcSMarius Strobl static uint32_t
3828c1093fcSMarius Strobl nge_mii_bitbang_read(device_t dev)
3838c1093fcSMarius Strobl {
3848c1093fcSMarius Strobl 	struct nge_softc *sc;
3858c1093fcSMarius Strobl 	uint32_t val;
3868c1093fcSMarius Strobl 
3878c1093fcSMarius Strobl 	sc = device_get_softc(dev);
3888c1093fcSMarius Strobl 
3898c1093fcSMarius Strobl 	val = CSR_READ_4(sc, NGE_MEAR);
3908c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_MEAR,
3918c1093fcSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
3928c1093fcSMarius Strobl 
3938c1093fcSMarius Strobl 	return (val);
3948c1093fcSMarius Strobl }
3958c1093fcSMarius Strobl 
3968c1093fcSMarius Strobl /*
3978c1093fcSMarius Strobl  * Write the MII serial port for the MII bit-bang module.
398ce4946daSBill Paul  */
399eaabec55SAlfred Perlstein static void
4008c1093fcSMarius Strobl nge_mii_bitbang_write(device_t dev, uint32_t val)
401ce4946daSBill Paul {
4028c1093fcSMarius Strobl 	struct nge_softc *sc;
403ce4946daSBill Paul 
4048c1093fcSMarius Strobl 	sc = device_get_softc(dev);
405ce4946daSBill Paul 
4068c1093fcSMarius Strobl 	CSR_WRITE_4(sc, NGE_MEAR, val);
4078c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_MEAR,
4088c1093fcSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
409ce4946daSBill Paul }
410ce4946daSBill Paul 
411eaabec55SAlfred Perlstein static int
412284c81cbSPyun YongHyeon nge_miibus_readreg(device_t dev, int phy, int reg)
413ce4946daSBill Paul {
414ce4946daSBill Paul 	struct nge_softc *sc;
415f6bc9430SPyun YongHyeon 	int rv;
416ce4946daSBill Paul 
417ce4946daSBill Paul 	sc = device_get_softc(dev);
418f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0) {
419f6bc9430SPyun YongHyeon 		/* Pretend PHY is at address 0. */
420f6bc9430SPyun YongHyeon 		if (phy != 0)
421f6bc9430SPyun YongHyeon 			return (0);
422f6bc9430SPyun YongHyeon 		switch (reg) {
423f6bc9430SPyun YongHyeon 		case MII_BMCR:
424f6bc9430SPyun YongHyeon 			reg = NGE_TBI_BMCR;
425f6bc9430SPyun YongHyeon 			break;
426f6bc9430SPyun YongHyeon 		case MII_BMSR:
427f6bc9430SPyun YongHyeon 			/* 83820/83821 has different bit layout for BMSR. */
428f6bc9430SPyun YongHyeon 			rv = BMSR_ANEG | BMSR_EXTCAP | BMSR_EXTSTAT;
429f6bc9430SPyun YongHyeon 			reg = CSR_READ_4(sc, NGE_TBI_BMSR);
430f6bc9430SPyun YongHyeon 			if ((reg & NGE_TBIBMSR_ANEG_DONE) != 0)
431f6bc9430SPyun YongHyeon 				rv |= BMSR_ACOMP;
432f6bc9430SPyun YongHyeon 			if ((reg & NGE_TBIBMSR_LINKSTAT) != 0)
433f6bc9430SPyun YongHyeon 				rv |= BMSR_LINK;
434f6bc9430SPyun YongHyeon 			return (rv);
435f6bc9430SPyun YongHyeon 		case MII_ANAR:
436f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANAR;
437f6bc9430SPyun YongHyeon 			break;
438f6bc9430SPyun YongHyeon 		case MII_ANLPAR:
439f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANLPAR;
440f6bc9430SPyun YongHyeon 			break;
441f6bc9430SPyun YongHyeon 		case MII_ANER:
442f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANER;
443f6bc9430SPyun YongHyeon 			break;
444f6bc9430SPyun YongHyeon 		case MII_EXTSR:
445f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ESR;
446f6bc9430SPyun YongHyeon 			break;
447f6bc9430SPyun YongHyeon 		case MII_PHYIDR1:
448f6bc9430SPyun YongHyeon 		case MII_PHYIDR2:
449f6bc9430SPyun YongHyeon 			return (0);
450f6bc9430SPyun YongHyeon 		default:
451f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
452f6bc9430SPyun YongHyeon 			    "bad phy register read : %d\n", reg);
453f6bc9430SPyun YongHyeon 			return (0);
454f6bc9430SPyun YongHyeon 		}
455f6bc9430SPyun YongHyeon 		return (CSR_READ_4(sc, reg));
456f6bc9430SPyun YongHyeon 	}
457ce4946daSBill Paul 
4588c1093fcSMarius Strobl 	return (mii_bitbang_readreg(dev, &nge_mii_bitbang_ops, phy, reg));
459ce4946daSBill Paul }
460ce4946daSBill Paul 
461eaabec55SAlfred Perlstein static int
462284c81cbSPyun YongHyeon nge_miibus_writereg(device_t dev, int phy, int reg, int data)
463ce4946daSBill Paul {
464ce4946daSBill Paul 	struct nge_softc *sc;
465ce4946daSBill Paul 
466ce4946daSBill Paul 	sc = device_get_softc(dev);
467f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0) {
468f6bc9430SPyun YongHyeon 		/* Pretend PHY is at address 0. */
469f6bc9430SPyun YongHyeon 		if (phy != 0)
470f6bc9430SPyun YongHyeon 			return (0);
471f6bc9430SPyun YongHyeon 		switch (reg) {
472f6bc9430SPyun YongHyeon 		case MII_BMCR:
473f6bc9430SPyun YongHyeon 			reg = NGE_TBI_BMCR;
474f6bc9430SPyun YongHyeon 			break;
475f6bc9430SPyun YongHyeon 		case MII_BMSR:
476f6bc9430SPyun YongHyeon 			return (0);
477f6bc9430SPyun YongHyeon 		case MII_ANAR:
478f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANAR;
479f6bc9430SPyun YongHyeon 			break;
480f6bc9430SPyun YongHyeon 		case MII_ANLPAR:
481f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANLPAR;
482f6bc9430SPyun YongHyeon 			break;
483f6bc9430SPyun YongHyeon 		case MII_ANER:
484f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANER;
485f6bc9430SPyun YongHyeon 			break;
486f6bc9430SPyun YongHyeon 		case MII_EXTSR:
487f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ESR;
488f6bc9430SPyun YongHyeon 			break;
489f6bc9430SPyun YongHyeon 		case MII_PHYIDR1:
490f6bc9430SPyun YongHyeon 		case MII_PHYIDR2:
491f6bc9430SPyun YongHyeon 			return (0);
492f6bc9430SPyun YongHyeon 		default:
493f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
494f6bc9430SPyun YongHyeon 			    "bad phy register write : %d\n", reg);
495f6bc9430SPyun YongHyeon 			return (0);
496f6bc9430SPyun YongHyeon 		}
497f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, reg, data);
498f6bc9430SPyun YongHyeon 		return (0);
499f6bc9430SPyun YongHyeon 	}
500ce4946daSBill Paul 
5018c1093fcSMarius Strobl 	mii_bitbang_writereg(dev, &nge_mii_bitbang_ops, phy, reg, data);
502ce4946daSBill Paul 
503ce4946daSBill Paul 	return (0);
504ce4946daSBill Paul }
505ce4946daSBill Paul 
506f6bc9430SPyun YongHyeon /*
507f6bc9430SPyun YongHyeon  * media status/link state change handler.
508f6bc9430SPyun YongHyeon  */
509eaabec55SAlfred Perlstein static void
510284c81cbSPyun YongHyeon nge_miibus_statchg(device_t dev)
511ce4946daSBill Paul {
512ce4946daSBill Paul 	struct nge_softc *sc;
513ce4946daSBill Paul 	struct mii_data *mii;
514f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
515f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
516f6bc9430SPyun YongHyeon 	uint32_t done, reg, status;
517f6bc9430SPyun YongHyeon 	int i;
518ce4946daSBill Paul 
519ce4946daSBill Paul 	sc = device_get_softc(dev);
520f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
5211f548804SDoug Ambrisko 
522ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
523f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
524f6bc9430SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
525f6bc9430SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
526f6bc9430SPyun YongHyeon 		return;
527ce4946daSBill Paul 
528f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
529f6bc9430SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) ==
530f6bc9430SPyun YongHyeon 	    (IFM_AVALID | IFM_ACTIVE)) {
531f6bc9430SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
532f6bc9430SPyun YongHyeon 		case IFM_10_T:
533f6bc9430SPyun YongHyeon 		case IFM_100_TX:
534f6bc9430SPyun YongHyeon 		case IFM_1000_T:
535f6bc9430SPyun YongHyeon 		case IFM_1000_SX:
536f6bc9430SPyun YongHyeon 		case IFM_1000_LX:
537f6bc9430SPyun YongHyeon 		case IFM_1000_CX:
538f6bc9430SPyun YongHyeon 			sc->nge_flags |= NGE_FLAG_LINK;
539f6bc9430SPyun YongHyeon 			break;
540f6bc9430SPyun YongHyeon 		default:
541f6bc9430SPyun YongHyeon 			break;
542f6bc9430SPyun YongHyeon 		}
543f6bc9430SPyun YongHyeon 	}
544f6bc9430SPyun YongHyeon 
545f6bc9430SPyun YongHyeon 	/* Stop Tx/Rx MACs. */
546f6bc9430SPyun YongHyeon 	if (nge_stop_mac(sc) == ETIMEDOUT)
547f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
548f6bc9430SPyun YongHyeon 		    "%s: unable to stop Tx/Rx MAC\n", __func__);
549f6bc9430SPyun YongHyeon 	nge_txeof(sc);
550f6bc9430SPyun YongHyeon 	nge_rxeof(sc);
551f6bc9430SPyun YongHyeon 	if (sc->nge_head != NULL) {
552f6bc9430SPyun YongHyeon 		m_freem(sc->nge_head);
553f6bc9430SPyun YongHyeon 		sc->nge_head = sc->nge_tail = NULL;
554f6bc9430SPyun YongHyeon 	}
555f6bc9430SPyun YongHyeon 
556f6bc9430SPyun YongHyeon 	/* Release queued frames. */
557f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
558f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
559f6bc9430SPyun YongHyeon 		if (txd->tx_m != NULL) {
560f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_tx_tag,
561f6bc9430SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
562f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_tag,
563f6bc9430SPyun YongHyeon 			    txd->tx_dmamap);
564f6bc9430SPyun YongHyeon 			m_freem(txd->tx_m);
565f6bc9430SPyun YongHyeon 			txd->tx_m = NULL;
566f6bc9430SPyun YongHyeon 		}
567f6bc9430SPyun YongHyeon 	}
568f6bc9430SPyun YongHyeon 
569f6bc9430SPyun YongHyeon 	/* Program MAC with resolved speed/duplex. */
570f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_LINK) != 0) {
571f6bc9430SPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
572ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_TX_CFG,
573ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT | NGE_TXCFG_IGN_CARR));
574ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
575f6bc9430SPyun YongHyeon #ifdef notyet
576f6bc9430SPyun YongHyeon 			/* Enable flow-control. */
577f6bc9430SPyun YongHyeon 			if ((IFM_OPTIONS(mii->mii_media_active) &
578f6bc9430SPyun YongHyeon 			    (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) != 0)
579f6bc9430SPyun YongHyeon 				NGE_SETBIT(sc, NGE_PAUSECSR,
580f6bc9430SPyun YongHyeon 				    NGE_PAUSECSR_PAUSE_ENB);
581f6bc9430SPyun YongHyeon #endif
582ce4946daSBill Paul 		} else {
583ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
584ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT | NGE_TXCFG_IGN_CARR));
585ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
586f6bc9430SPyun YongHyeon 			NGE_CLRBIT(sc, NGE_PAUSECSR, NGE_PAUSECSR_PAUSE_ENB);
587f6bc9430SPyun YongHyeon 		}
588f6bc9430SPyun YongHyeon 		/* If we have a 1000Mbps link, set the mode_1000 bit. */
589f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CFG);
590f6bc9430SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
591f6bc9430SPyun YongHyeon 		case IFM_1000_SX:
592f6bc9430SPyun YongHyeon 		case IFM_1000_LX:
593f6bc9430SPyun YongHyeon 		case IFM_1000_CX:
594f6bc9430SPyun YongHyeon 		case IFM_1000_T:
595f6bc9430SPyun YongHyeon 			reg |= NGE_CFG_MODE_1000;
596f6bc9430SPyun YongHyeon 			break;
597f6bc9430SPyun YongHyeon 		default:
598f6bc9430SPyun YongHyeon 			reg &= ~NGE_CFG_MODE_1000;
599f6bc9430SPyun YongHyeon 			break;
600f6bc9430SPyun YongHyeon 		}
601f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CFG, reg);
602f6bc9430SPyun YongHyeon 
603f6bc9430SPyun YongHyeon 		/* Reset Tx/Rx MAC. */
604f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CSR);
605f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_TX_RESET | NGE_CSR_RX_RESET;
606f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
607f6bc9430SPyun YongHyeon 		/* Check the completion of reset. */
608f6bc9430SPyun YongHyeon 		done = 0;
609f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
610f6bc9430SPyun YongHyeon 			DELAY(1);
611f6bc9430SPyun YongHyeon 			status = CSR_READ_4(sc, NGE_ISR);
612f6bc9430SPyun YongHyeon 			if ((status & NGE_ISR_RX_RESET_DONE) != 0)
613f6bc9430SPyun YongHyeon 				done |= NGE_ISR_RX_RESET_DONE;
614f6bc9430SPyun YongHyeon 			if ((status & NGE_ISR_TX_RESET_DONE) != 0)
615f6bc9430SPyun YongHyeon 				done |= NGE_ISR_TX_RESET_DONE;
616f6bc9430SPyun YongHyeon 			if (done ==
617f6bc9430SPyun YongHyeon 			    (NGE_ISR_TX_RESET_DONE | NGE_ISR_RX_RESET_DONE))
618f6bc9430SPyun YongHyeon 				break;
619f6bc9430SPyun YongHyeon 		}
620f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
621f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
622f6bc9430SPyun YongHyeon 			    "%s: unable to reset Tx/Rx MAC\n", __func__);
623f6bc9430SPyun YongHyeon 		/* Reuse Rx buffer and reset consumer pointer. */
624f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_cons = 0;
625f6bc9430SPyun YongHyeon 		/*
626f6bc9430SPyun YongHyeon 		 * It seems that resetting Rx/Tx MAC results in
627f6bc9430SPyun YongHyeon 		 * resetting Tx/Rx descriptor pointer registers such
628f6bc9430SPyun YongHyeon 		 * that reloading Tx/Rx lists address are needed.
629f6bc9430SPyun YongHyeon 		 */
630f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI,
631f6bc9430SPyun YongHyeon 		    NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr));
632f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO,
633f6bc9430SPyun YongHyeon 		    NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr));
634f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI,
635f6bc9430SPyun YongHyeon 		    NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr));
636f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO,
637f6bc9430SPyun YongHyeon 		    NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr));
638f6bc9430SPyun YongHyeon 		/* Reinitialize Tx buffers. */
639f6bc9430SPyun YongHyeon 		nge_list_tx_init(sc);
640f6bc9430SPyun YongHyeon 
641f6bc9430SPyun YongHyeon 		/* Restart Rx MAC. */
642f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CSR);
643f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_RX_ENABLE;
644f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
645f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
646f6bc9430SPyun YongHyeon 			if ((CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RX_ENABLE) != 0)
647f6bc9430SPyun YongHyeon 				break;
648f6bc9430SPyun YongHyeon 			DELAY(1);
649f6bc9430SPyun YongHyeon 		}
650f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
651f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
652f6bc9430SPyun YongHyeon 			    "%s: unable to restart Rx MAC\n", __func__);
653ce4946daSBill Paul 	}
654ce4946daSBill Paul 
655f6bc9430SPyun YongHyeon 	/* Data LED off for TBI mode */
656f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
657f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
658f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT);
659ce4946daSBill Paul }
660ce4946daSBill Paul 
661eaabec55SAlfred Perlstein static void
662f6bc9430SPyun YongHyeon nge_rxfilter(struct nge_softc *sc)
663ce4946daSBill Paul {
664ce4946daSBill Paul 	struct ifnet *ifp;
665ce4946daSBill Paul 	struct ifmultiaddr *ifma;
666f6bc9430SPyun YongHyeon 	uint32_t h, i, rxfilt;
667ce4946daSBill Paul 	int bit, index;
668ce4946daSBill Paul 
669ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
670fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
671ce4946daSBill Paul 
672f6bc9430SPyun YongHyeon 	/* Make sure to stop Rx filtering. */
673f6bc9430SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, NGE_RXFILT_CTL);
674f6bc9430SPyun YongHyeon 	rxfilt &= ~NGE_RXFILTCTL_ENABLE;
675f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
6768c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
677f6bc9430SPyun YongHyeon 
678f6bc9430SPyun YongHyeon 	rxfilt &= ~(NGE_RXFILTCTL_ALLMULTI | NGE_RXFILTCTL_ALLPHYS);
679f6bc9430SPyun YongHyeon 	rxfilt &= ~NGE_RXFILTCTL_BROAD;
680f6bc9430SPyun YongHyeon 	/*
681f6bc9430SPyun YongHyeon 	 * We don't want to use the hash table for matching unicast
682f6bc9430SPyun YongHyeon 	 * addresses.
683f6bc9430SPyun YongHyeon 	 */
684f6bc9430SPyun YongHyeon 	rxfilt &= ~(NGE_RXFILTCTL_MCHASH | NGE_RXFILTCTL_UCHASH);
685f6bc9430SPyun YongHyeon 
686f6bc9430SPyun YongHyeon 	/*
687f6bc9430SPyun YongHyeon 	 * For the NatSemi chip, we have to explicitly enable the
688f6bc9430SPyun YongHyeon 	 * reception of ARP frames, as well as turn on the 'perfect
689f6bc9430SPyun YongHyeon 	 * match' filter where we store the station address, otherwise
690f6bc9430SPyun YongHyeon 	 * we won't receive unicasts meant for this host.
691f6bc9430SPyun YongHyeon 	 */
692f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_ARP | NGE_RXFILTCTL_PERFECT;
693f6bc9430SPyun YongHyeon 
694f6bc9430SPyun YongHyeon 	/*
695f6bc9430SPyun YongHyeon 	 * Set the capture broadcast bit to capture broadcast frames.
696f6bc9430SPyun YongHyeon 	 */
697f6bc9430SPyun YongHyeon 	if ((ifp->if_flags & IFF_BROADCAST) != 0)
698f6bc9430SPyun YongHyeon 		rxfilt |= NGE_RXFILTCTL_BROAD;
699f6bc9430SPyun YongHyeon 
700f6bc9430SPyun YongHyeon 	if ((ifp->if_flags & IFF_PROMISC) != 0 ||
701f6bc9430SPyun YongHyeon 	    (ifp->if_flags & IFF_ALLMULTI) != 0) {
702f6bc9430SPyun YongHyeon 		rxfilt |= NGE_RXFILTCTL_ALLMULTI;
703f6bc9430SPyun YongHyeon 		if ((ifp->if_flags & IFF_PROMISC) != 0)
704f6bc9430SPyun YongHyeon 			rxfilt |= NGE_RXFILTCTL_ALLPHYS;
705f6bc9430SPyun YongHyeon 		goto done;
706ce4946daSBill Paul 	}
707ce4946daSBill Paul 
708ce4946daSBill Paul 	/*
709ce4946daSBill Paul 	 * We have to explicitly enable the multicast hash table
710ce4946daSBill Paul 	 * on the NatSemi chip if we want to use it, which we do.
711ce4946daSBill Paul 	 */
712f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_MCHASH;
713ce4946daSBill Paul 
714ce4946daSBill Paul 	/* first, zot all the existing hash bits */
715ce4946daSBill Paul 	for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) {
716ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i);
717ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0);
718ce4946daSBill Paul 	}
719ce4946daSBill Paul 
720ce4946daSBill Paul 	/*
721ce4946daSBill Paul 	 * From the 11 bits returned by the crc routine, the top 7
722ce4946daSBill Paul 	 * bits represent the 16-bit word in the mcast hash table
723ce4946daSBill Paul 	 * that needs to be updated, and the lower 4 bits represent
724ce4946daSBill Paul 	 * which bit within that byte needs to be set.
725ce4946daSBill Paul 	 */
726eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
727ce4946daSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
728ce4946daSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
729ce4946daSBill Paul 			continue;
7300e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
7310e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 21;
732ce4946daSBill Paul 		index = (h >> 4) & 0x7F;
733ce4946daSBill Paul 		bit = h & 0xF;
734ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL,
735ce4946daSBill Paul 		    NGE_FILTADDR_MCAST_LO + (index * 2));
736ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit));
737ce4946daSBill Paul 	}
738eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
739ce4946daSBill Paul 
740f6bc9430SPyun YongHyeon done:
741f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
742f6bc9430SPyun YongHyeon 	/* Turn the receive filter on. */
743f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_ENABLE;
744f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
7458c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
746ce4946daSBill Paul }
747ce4946daSBill Paul 
748eaabec55SAlfred Perlstein static void
749284c81cbSPyun YongHyeon nge_reset(struct nge_softc *sc)
750ce4946daSBill Paul {
751f6bc9430SPyun YongHyeon 	uint32_t v;
7522cf2d799SPyun YongHyeon 	int i;
753ce4946daSBill Paul 
754ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET);
755ce4946daSBill Paul 
756ce4946daSBill Paul 	for (i = 0; i < NGE_TIMEOUT; i++) {
757ce4946daSBill Paul 		if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET))
758ce4946daSBill Paul 			break;
759f6bc9430SPyun YongHyeon 		DELAY(1);
760ce4946daSBill Paul 	}
761ce4946daSBill Paul 
762ce4946daSBill Paul 	if (i == NGE_TIMEOUT)
7636b9f5c94SGleb Smirnoff 		device_printf(sc->nge_dev, "reset never completed\n");
764ce4946daSBill Paul 
765ce4946daSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
766ce4946daSBill Paul 	DELAY(1000);
767ce4946daSBill Paul 
768ce4946daSBill Paul 	/*
769ce4946daSBill Paul 	 * If this is a NetSemi chip, make sure to clear
770ce4946daSBill Paul 	 * PME mode.
771ce4946daSBill Paul 	 */
772ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS);
773ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, 0);
774f6bc9430SPyun YongHyeon 
775f6bc9430SPyun YongHyeon 	/* Clear WOL events which may interfere normal Rx filter opertaion. */
776f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_WOLCSR, 0);
777f6bc9430SPyun YongHyeon 
778f6bc9430SPyun YongHyeon 	/*
779f6bc9430SPyun YongHyeon 	 * Only DP83820 supports 64bits addressing/data transfers and
780f6bc9430SPyun YongHyeon 	 * 64bit addressing requires different descriptor structures.
781f6bc9430SPyun YongHyeon 	 * To make it simple, disable 64bit addressing/data transfers.
782f6bc9430SPyun YongHyeon 	 */
783f6bc9430SPyun YongHyeon 	v = CSR_READ_4(sc, NGE_CFG);
784f6bc9430SPyun YongHyeon 	v &= ~(NGE_CFG_64BIT_ADDR_ENB | NGE_CFG_64BIT_DATA_ENB);
785f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_CFG, v);
786ce4946daSBill Paul }
787ce4946daSBill Paul 
788ce4946daSBill Paul /*
789d64ada50SJens Schweikhardt  * Probe for a NatSemi chip. Check the PCI vendor and device
790ce4946daSBill Paul  * IDs against our list and return a device name if we find a match.
791ce4946daSBill Paul  */
792eaabec55SAlfred Perlstein static int
793284c81cbSPyun YongHyeon nge_probe(device_t dev)
794ce4946daSBill Paul {
7958c1093fcSMarius Strobl 	const struct nge_type *t;
796ce4946daSBill Paul 
797ce4946daSBill Paul 	t = nge_devs;
798ce4946daSBill Paul 
799ce4946daSBill Paul 	while (t->nge_name != NULL) {
800ce4946daSBill Paul 		if ((pci_get_vendor(dev) == t->nge_vid) &&
801ce4946daSBill Paul 		    (pci_get_device(dev) == t->nge_did)) {
802ce4946daSBill Paul 			device_set_desc(dev, t->nge_name);
8036b9907e7SWarner Losh 			return (BUS_PROBE_DEFAULT);
804ce4946daSBill Paul 		}
805ce4946daSBill Paul 		t++;
806ce4946daSBill Paul 	}
807ce4946daSBill Paul 
808ce4946daSBill Paul 	return (ENXIO);
809ce4946daSBill Paul }
810ce4946daSBill Paul 
811ce4946daSBill Paul /*
812ce4946daSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
813ce4946daSBill Paul  * setup and ethernet/BPF attach.
814ce4946daSBill Paul  */
815eaabec55SAlfred Perlstein static int
816284c81cbSPyun YongHyeon nge_attach(device_t dev)
817ce4946daSBill Paul {
818f6bc9430SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
819f6bc9430SPyun YongHyeon 	uint16_t ea[ETHER_ADDR_LEN/2], ea_temp, reg;
820ce4946daSBill Paul 	struct nge_softc *sc;
821f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
822f6bc9430SPyun YongHyeon 	int error, i, rid;
823ce4946daSBill Paul 
824f6bc9430SPyun YongHyeon 	error = 0;
825ce4946daSBill Paul 	sc = device_get_softc(dev);
8266b9f5c94SGleb Smirnoff 	sc->nge_dev = dev;
827ce4946daSBill Paul 
828ad6c618bSBill Paul 	NGE_LOCK_INIT(sc, device_get_nameunit(dev));
829646abee6SJohn Baldwin 	callout_init_mtx(&sc->nge_stat_ch, &sc->nge_mtx, 0);
830646abee6SJohn Baldwin 
831ce4946daSBill Paul 	/*
832ce4946daSBill Paul 	 * Map control/status registers.
833ce4946daSBill Paul 	 */
834ce4946daSBill Paul 	pci_enable_busmaster(dev);
835ce4946daSBill Paul 
836f6bc9430SPyun YongHyeon #ifdef NGE_USEIOSPACE
837f6bc9430SPyun YongHyeon 	sc->nge_res_type = SYS_RES_IOPORT;
838f6bc9430SPyun YongHyeon 	sc->nge_res_id = PCIR_BAR(0);
839f6bc9430SPyun YongHyeon #else
840f6bc9430SPyun YongHyeon 	sc->nge_res_type = SYS_RES_MEMORY;
841f6bc9430SPyun YongHyeon 	sc->nge_res_id = PCIR_BAR(1);
842f6bc9430SPyun YongHyeon #endif
843f6bc9430SPyun YongHyeon 	sc->nge_res = bus_alloc_resource_any(dev, sc->nge_res_type,
844f6bc9430SPyun YongHyeon 	    &sc->nge_res_id, RF_ACTIVE);
845ce4946daSBill Paul 
846ce4946daSBill Paul 	if (sc->nge_res == NULL) {
847f6bc9430SPyun YongHyeon 		if (sc->nge_res_type == SYS_RES_MEMORY) {
848f6bc9430SPyun YongHyeon 			sc->nge_res_type = SYS_RES_IOPORT;
849f6bc9430SPyun YongHyeon 			sc->nge_res_id = PCIR_BAR(0);
850f6bc9430SPyun YongHyeon 		} else {
851f6bc9430SPyun YongHyeon 			sc->nge_res_type = SYS_RES_MEMORY;
852f6bc9430SPyun YongHyeon 			sc->nge_res_id = PCIR_BAR(1);
853ce4946daSBill Paul 		}
854f6bc9430SPyun YongHyeon 		sc->nge_res = bus_alloc_resource_any(dev, sc->nge_res_type,
855f6bc9430SPyun YongHyeon 		    &sc->nge_res_id, RF_ACTIVE);
856f6bc9430SPyun YongHyeon 		if (sc->nge_res == NULL) {
857f6bc9430SPyun YongHyeon 			device_printf(dev, "couldn't allocate %s resources\n",
858f6bc9430SPyun YongHyeon 			    sc->nge_res_type == SYS_RES_MEMORY ? "memory" :
859f6bc9430SPyun YongHyeon 			    "I/O");
860f6bc9430SPyun YongHyeon 			NGE_LOCK_DESTROY(sc);
861f6bc9430SPyun YongHyeon 			return (ENXIO);
862f6bc9430SPyun YongHyeon 		}
863f6bc9430SPyun YongHyeon 	}
864ce4946daSBill Paul 
865ce4946daSBill Paul 	/* Allocate interrupt */
866ce4946daSBill Paul 	rid = 0;
8675f96beb9SNate Lawson 	sc->nge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
868ce4946daSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
869ce4946daSBill Paul 
870ce4946daSBill Paul 	if (sc->nge_irq == NULL) {
871646abee6SJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
872ce4946daSBill Paul 		error = ENXIO;
873ce4946daSBill Paul 		goto fail;
874ce4946daSBill Paul 	}
875ce4946daSBill Paul 
876f6bc9430SPyun YongHyeon 	/* Enable MWI. */
877f6bc9430SPyun YongHyeon 	reg = pci_read_config(dev, PCIR_COMMAND, 2);
878f6bc9430SPyun YongHyeon 	reg |= PCIM_CMD_MWRICEN;
879f6bc9430SPyun YongHyeon 	pci_write_config(dev, PCIR_COMMAND, reg, 2);
880f6bc9430SPyun YongHyeon 
881ce4946daSBill Paul 	/* Reset the adapter. */
882ce4946daSBill Paul 	nge_reset(sc);
883ce4946daSBill Paul 
884ce4946daSBill Paul 	/*
885ce4946daSBill Paul 	 * Get station address from the EEPROM.
886ce4946daSBill Paul 	 */
887f6bc9430SPyun YongHyeon 	nge_read_eeprom(sc, (caddr_t)ea, NGE_EE_NODEADDR, 3);
888f6bc9430SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
889f6bc9430SPyun YongHyeon 		ea[i] = le16toh(ea[i]);
890f6bc9430SPyun YongHyeon 	ea_temp = ea[0];
891f6bc9430SPyun YongHyeon 	ea[0] = ea[2];
892f6bc9430SPyun YongHyeon 	ea[2] = ea_temp;
893f6bc9430SPyun YongHyeon 	bcopy(ea, eaddr, sizeof(eaddr));
894ce4946daSBill Paul 
895f6bc9430SPyun YongHyeon 	if (nge_dma_alloc(sc) != 0) {
896ce4946daSBill Paul 		error = ENXIO;
897ce4946daSBill Paul 		goto fail;
898ce4946daSBill Paul 	}
899ce4946daSBill Paul 
900f6bc9430SPyun YongHyeon 	nge_sysctl_node(sc);
901f6bc9430SPyun YongHyeon 
902fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp = if_alloc(IFT_ETHER);
903fc74a9f9SBrooks Davis 	if (ifp == NULL) {
904f6bc9430SPyun YongHyeon 		device_printf(dev, "can not allocate ifnet structure\n");
905fc74a9f9SBrooks Davis 		error = ENOSPC;
906fc74a9f9SBrooks Davis 		goto fail;
907fc74a9f9SBrooks Davis 	}
908ce4946daSBill Paul 	ifp->if_softc = sc;
9099bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
910ad6c618bSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
911ce4946daSBill Paul 	ifp->if_ioctl = nge_ioctl;
912ce4946daSBill Paul 	ifp->if_start = nge_start;
913ce4946daSBill Paul 	ifp->if_init = nge_init;
914f6bc9430SPyun YongHyeon 	ifp->if_snd.ifq_drv_maxlen = NGE_TX_RING_CNT - 1;
915f6bc9430SPyun YongHyeon 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
916f6bc9430SPyun YongHyeon 	IFQ_SET_READY(&ifp->if_snd);
917ce4946daSBill Paul 	ifp->if_hwassist = NGE_CSUM_FEATURES;
918f6bc9430SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
919ce4946daSBill Paul 	/*
920f6bc9430SPyun YongHyeon 	 * It seems that some hardwares doesn't provide 3.3V auxiliary
921f6bc9430SPyun YongHyeon 	 * supply(3VAUX) to drive PME such that checking PCI power
922f6bc9430SPyun YongHyeon 	 * management capability is necessary.
923ce4946daSBill Paul 	 */
9243b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &i) == 0)
925f6bc9430SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
926f6bc9430SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
927f6bc9430SPyun YongHyeon 
928f6bc9430SPyun YongHyeon 	if ((CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) != 0) {
929f6bc9430SPyun YongHyeon 		sc->nge_flags |= NGE_FLAG_TBI;
9301f548804SDoug Ambrisko 		device_printf(dev, "Using TBI\n");
931f6bc9430SPyun YongHyeon 		/* Configure GPIO. */
9321f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
9331f548804SDoug Ambrisko 		    | NGE_GPIO_GP4_OUT
9341f548804SDoug Ambrisko 		    | NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB
9351f548804SDoug Ambrisko 		    | NGE_GPIO_GP3_OUTENB
9361f548804SDoug Ambrisko 		    | NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN);
937ce4946daSBill Paul 	}
938f6bc9430SPyun YongHyeon 
939f6bc9430SPyun YongHyeon 	/*
940f6bc9430SPyun YongHyeon 	 * Do MII setup.
941f6bc9430SPyun YongHyeon 	 */
942d6c65d27SMarius Strobl 	error = mii_attach(dev, &sc->nge_miibus, ifp, nge_mediachange,
943d6c65d27SMarius Strobl 	    nge_mediastatus, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
944f6bc9430SPyun YongHyeon 	if (error != 0) {
945d6c65d27SMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
946f6bc9430SPyun YongHyeon 		goto fail;
9471f548804SDoug Ambrisko 	}
948ce4946daSBill Paul 
949ce4946daSBill Paul 	/*
950ce4946daSBill Paul 	 * Call MI attach routine.
951ce4946daSBill Paul 	 */
952673d9191SSam Leffler 	ether_ifattach(ifp, eaddr);
953ad6c618bSBill Paul 
954f6bc9430SPyun YongHyeon 	/* VLAN capability setup. */
955f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
956f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
957f6bc9430SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
958f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
959f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
960f6bc9430SPyun YongHyeon #endif
961f6bc9430SPyun YongHyeon 	/*
962f6bc9430SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
963f6bc9430SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
964f6bc9430SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
965f6bc9430SPyun YongHyeon 	 */
966f6bc9430SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
967f6bc9430SPyun YongHyeon 
968ad6c618bSBill Paul 	/*
969ad6c618bSBill Paul 	 * Hookup IRQ last.
970ad6c618bSBill Paul 	 */
971ad6c618bSBill Paul 	error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET | INTR_MPSAFE,
972ef544f63SPaolo Pisati 	    NULL, nge_intr, sc, &sc->nge_intrhand);
973ad6c618bSBill Paul 	if (error) {
974646abee6SJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
975646abee6SJohn Baldwin 		goto fail;
976ad6c618bSBill Paul 	}
977ce4946daSBill Paul 
978646abee6SJohn Baldwin fail:
979f6bc9430SPyun YongHyeon 	if (error != 0)
980f6bc9430SPyun YongHyeon 		nge_detach(dev);
981ce4946daSBill Paul 	return (error);
982ce4946daSBill Paul }
983ce4946daSBill Paul 
984eaabec55SAlfred Perlstein static int
985284c81cbSPyun YongHyeon nge_detach(device_t dev)
986ce4946daSBill Paul {
987ce4946daSBill Paul 	struct nge_softc *sc;
988ce4946daSBill Paul 	struct ifnet *ifp;
989ce4946daSBill Paul 
990ce4946daSBill Paul 	sc = device_get_softc(dev);
991fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
992ce4946daSBill Paul 
99340929967SGleb Smirnoff #ifdef DEVICE_POLLING
994f6bc9430SPyun YongHyeon 	if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
99540929967SGleb Smirnoff 		ether_poll_deregister(ifp);
99640929967SGleb Smirnoff #endif
997f6bc9430SPyun YongHyeon 
998f6bc9430SPyun YongHyeon 	if (device_is_attached(dev)) {
999ad6c618bSBill Paul 		NGE_LOCK(sc);
1000f6bc9430SPyun YongHyeon 		sc->nge_flags |= NGE_FLAG_DETACH;
1001ce4946daSBill Paul 		nge_stop(sc);
1002ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1003646abee6SJohn Baldwin 		callout_drain(&sc->nge_stat_ch);
1004f6bc9430SPyun YongHyeon 		if (ifp != NULL)
1005673d9191SSam Leffler 			ether_ifdetach(ifp);
10061f548804SDoug Ambrisko 	}
1007ce4946daSBill Paul 
1008f6bc9430SPyun YongHyeon 	if (sc->nge_miibus != NULL) {
1009f6bc9430SPyun YongHyeon 		device_delete_child(dev, sc->nge_miibus);
1010f6bc9430SPyun YongHyeon 		sc->nge_miibus = NULL;
1011f6bc9430SPyun YongHyeon 	}
1012f6bc9430SPyun YongHyeon 	bus_generic_detach(dev);
1013f6bc9430SPyun YongHyeon 	if (sc->nge_intrhand != NULL)
1014f6bc9430SPyun YongHyeon 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
1015f6bc9430SPyun YongHyeon 	if (sc->nge_irq != NULL)
1016f6bc9430SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
1017f6bc9430SPyun YongHyeon 	if (sc->nge_res != NULL)
1018f6bc9430SPyun YongHyeon 		bus_release_resource(dev, sc->nge_res_type, sc->nge_res_id,
1019f6bc9430SPyun YongHyeon 		    sc->nge_res);
1020f6bc9430SPyun YongHyeon 
1021f6bc9430SPyun YongHyeon 	nge_dma_free(sc);
1022f6bc9430SPyun YongHyeon 	if (ifp != NULL)
1023ad4f426eSWarner Losh 		if_free(ifp);
1024ce4946daSBill Paul 
10256ba160b6SBill Paul 	NGE_LOCK_DESTROY(sc);
10266ba160b6SBill Paul 
1027ce4946daSBill Paul 	return (0);
1028ce4946daSBill Paul }
1029ce4946daSBill Paul 
1030f6bc9430SPyun YongHyeon struct nge_dmamap_arg {
1031f6bc9430SPyun YongHyeon 	bus_addr_t	nge_busaddr;
1032f6bc9430SPyun YongHyeon };
1033f6bc9430SPyun YongHyeon 
1034f6bc9430SPyun YongHyeon static void
1035f6bc9430SPyun YongHyeon nge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1036f6bc9430SPyun YongHyeon {
1037f6bc9430SPyun YongHyeon 	struct nge_dmamap_arg *ctx;
1038f6bc9430SPyun YongHyeon 
1039f6bc9430SPyun YongHyeon 	if (error != 0)
1040f6bc9430SPyun YongHyeon 		return;
1041f6bc9430SPyun YongHyeon 	ctx = arg;
1042f6bc9430SPyun YongHyeon 	ctx->nge_busaddr = segs[0].ds_addr;
1043f6bc9430SPyun YongHyeon }
1044f6bc9430SPyun YongHyeon 
1045f6bc9430SPyun YongHyeon static int
1046f6bc9430SPyun YongHyeon nge_dma_alloc(struct nge_softc *sc)
1047f6bc9430SPyun YongHyeon {
1048f6bc9430SPyun YongHyeon 	struct nge_dmamap_arg ctx;
1049f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1050f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1051f6bc9430SPyun YongHyeon 	int error, i;
1052f6bc9430SPyun YongHyeon 
1053f6bc9430SPyun YongHyeon 	/* Create parent DMA tag. */
1054f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(
1055f6bc9430SPyun YongHyeon 	    bus_get_dma_tag(sc->nge_dev),	/* parent */
1056f6bc9430SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1057f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1058f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1059f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1060f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1061f6bc9430SPyun YongHyeon 	    0,				/* nsegments */
1062f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1063f6bc9430SPyun YongHyeon 	    0,				/* flags */
1064f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1065f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_parent_tag);
1066f6bc9430SPyun YongHyeon 	if (error != 0) {
1067f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create parent DMA tag\n");
1068f6bc9430SPyun YongHyeon 		goto fail;
1069f6bc9430SPyun YongHyeon 	}
1070f6bc9430SPyun YongHyeon 	/* Create tag for Tx ring. */
1071f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1072f6bc9430SPyun YongHyeon 	    NGE_RING_ALIGN, 0,		/* alignment, boundary */
1073f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1074f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1075f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1076f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE,		/* maxsize */
1077f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1078f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE,		/* maxsegsize */
1079f6bc9430SPyun YongHyeon 	    0,				/* flags */
1080f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1081f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_tx_ring_tag);
1082f6bc9430SPyun YongHyeon 	if (error != 0) {
1083f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Tx ring DMA tag\n");
1084f6bc9430SPyun YongHyeon 		goto fail;
1085f6bc9430SPyun YongHyeon 	}
1086f6bc9430SPyun YongHyeon 
1087f6bc9430SPyun YongHyeon 	/* Create tag for Rx ring. */
1088f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1089f6bc9430SPyun YongHyeon 	    NGE_RING_ALIGN, 0,		/* alignment, boundary */
1090f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1091f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1092f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1093f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE,		/* maxsize */
1094f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1095f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE,		/* maxsegsize */
1096f6bc9430SPyun YongHyeon 	    0,				/* flags */
1097f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1098f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_ring_tag);
1099f6bc9430SPyun YongHyeon 	if (error != 0) {
1100f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1101f6bc9430SPyun YongHyeon 		    "failed to create Rx ring DMA tag\n");
1102f6bc9430SPyun YongHyeon 		goto fail;
1103f6bc9430SPyun YongHyeon 	}
1104f6bc9430SPyun YongHyeon 
1105f6bc9430SPyun YongHyeon 	/* Create tag for Tx buffers. */
1106f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1107f6bc9430SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1108f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1109f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1110f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1111f6bc9430SPyun YongHyeon 	    MCLBYTES * NGE_MAXTXSEGS,	/* maxsize */
1112f6bc9430SPyun YongHyeon 	    NGE_MAXTXSEGS,		/* nsegments */
1113f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1114f6bc9430SPyun YongHyeon 	    0,				/* flags */
1115f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1116f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_tx_tag);
1117f6bc9430SPyun YongHyeon 	if (error != 0) {
1118f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Tx DMA tag\n");
1119f6bc9430SPyun YongHyeon 		goto fail;
1120f6bc9430SPyun YongHyeon 	}
1121f6bc9430SPyun YongHyeon 
1122f6bc9430SPyun YongHyeon 	/* Create tag for Rx buffers. */
1123f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1124f6bc9430SPyun YongHyeon 	    NGE_RX_ALIGN, 0,		/* alignment, boundary */
1125f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1126f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1127f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1128f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsize */
1129f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1130f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1131f6bc9430SPyun YongHyeon 	    0,				/* flags */
1132f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1133f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_tag);
1134f6bc9430SPyun YongHyeon 	if (error != 0) {
1135f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Rx DMA tag\n");
1136f6bc9430SPyun YongHyeon 		goto fail;
1137f6bc9430SPyun YongHyeon 	}
1138f6bc9430SPyun YongHyeon 
1139f6bc9430SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Tx ring. */
1140f6bc9430SPyun YongHyeon 	error = bus_dmamem_alloc(sc->nge_cdata.nge_tx_ring_tag,
1141f6bc9430SPyun YongHyeon 	    (void **)&sc->nge_rdata.nge_tx_ring, BUS_DMA_WAITOK |
1142f6bc9430SPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->nge_cdata.nge_tx_ring_map);
1143f6bc9430SPyun YongHyeon 	if (error != 0) {
1144f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1145f6bc9430SPyun YongHyeon 		    "failed to allocate DMA'able memory for Tx ring\n");
1146f6bc9430SPyun YongHyeon 		goto fail;
1147f6bc9430SPyun YongHyeon 	}
1148f6bc9430SPyun YongHyeon 
1149f6bc9430SPyun YongHyeon 	ctx.nge_busaddr = 0;
1150f6bc9430SPyun YongHyeon 	error = bus_dmamap_load(sc->nge_cdata.nge_tx_ring_tag,
1151f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map, sc->nge_rdata.nge_tx_ring,
1152f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE, nge_dmamap_cb, &ctx, 0);
1153f6bc9430SPyun YongHyeon 	if (error != 0 || ctx.nge_busaddr == 0) {
1154f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1155f6bc9430SPyun YongHyeon 		    "failed to load DMA'able memory for Tx ring\n");
1156f6bc9430SPyun YongHyeon 		goto fail;
1157f6bc9430SPyun YongHyeon 	}
1158f6bc9430SPyun YongHyeon 	sc->nge_rdata.nge_tx_ring_paddr = ctx.nge_busaddr;
1159f6bc9430SPyun YongHyeon 
1160f6bc9430SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Rx ring. */
1161f6bc9430SPyun YongHyeon 	error = bus_dmamem_alloc(sc->nge_cdata.nge_rx_ring_tag,
1162f6bc9430SPyun YongHyeon 	    (void **)&sc->nge_rdata.nge_rx_ring, BUS_DMA_WAITOK |
1163f6bc9430SPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->nge_cdata.nge_rx_ring_map);
1164f6bc9430SPyun YongHyeon 	if (error != 0) {
1165f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1166f6bc9430SPyun YongHyeon 		    "failed to allocate DMA'able memory for Rx ring\n");
1167f6bc9430SPyun YongHyeon 		goto fail;
1168f6bc9430SPyun YongHyeon 	}
1169f6bc9430SPyun YongHyeon 
1170f6bc9430SPyun YongHyeon 	ctx.nge_busaddr = 0;
1171f6bc9430SPyun YongHyeon 	error = bus_dmamap_load(sc->nge_cdata.nge_rx_ring_tag,
1172f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map, sc->nge_rdata.nge_rx_ring,
1173f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE, nge_dmamap_cb, &ctx, 0);
1174f6bc9430SPyun YongHyeon 	if (error != 0 || ctx.nge_busaddr == 0) {
1175f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1176f6bc9430SPyun YongHyeon 		    "failed to load DMA'able memory for Rx ring\n");
1177f6bc9430SPyun YongHyeon 		goto fail;
1178f6bc9430SPyun YongHyeon 	}
1179f6bc9430SPyun YongHyeon 	sc->nge_rdata.nge_rx_ring_paddr = ctx.nge_busaddr;
1180f6bc9430SPyun YongHyeon 
1181f6bc9430SPyun YongHyeon 	/* Create DMA maps for Tx buffers. */
1182f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
1183f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
1184f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1185f6bc9430SPyun YongHyeon 		txd->tx_dmamap = NULL;
1186f6bc9430SPyun YongHyeon 		error = bus_dmamap_create(sc->nge_cdata.nge_tx_tag, 0,
1187f6bc9430SPyun YongHyeon 		    &txd->tx_dmamap);
1188f6bc9430SPyun YongHyeon 		if (error != 0) {
1189f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
1190f6bc9430SPyun YongHyeon 			    "failed to create Tx dmamap\n");
1191f6bc9430SPyun YongHyeon 			goto fail;
1192f6bc9430SPyun YongHyeon 		}
1193f6bc9430SPyun YongHyeon 	}
1194f6bc9430SPyun YongHyeon 	/* Create DMA maps for Rx buffers. */
1195f6bc9430SPyun YongHyeon 	if ((error = bus_dmamap_create(sc->nge_cdata.nge_rx_tag, 0,
1196f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_sparemap)) != 0) {
1197f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1198f6bc9430SPyun YongHyeon 		    "failed to create spare Rx dmamap\n");
1199f6bc9430SPyun YongHyeon 		goto fail;
1200f6bc9430SPyun YongHyeon 	}
1201f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
1202f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[i];
1203f6bc9430SPyun YongHyeon 		rxd->rx_m = NULL;
1204f6bc9430SPyun YongHyeon 		rxd->rx_dmamap = NULL;
1205f6bc9430SPyun YongHyeon 		error = bus_dmamap_create(sc->nge_cdata.nge_rx_tag, 0,
1206f6bc9430SPyun YongHyeon 		    &rxd->rx_dmamap);
1207f6bc9430SPyun YongHyeon 		if (error != 0) {
1208f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
1209f6bc9430SPyun YongHyeon 			    "failed to create Rx dmamap\n");
1210f6bc9430SPyun YongHyeon 			goto fail;
1211f6bc9430SPyun YongHyeon 		}
1212f6bc9430SPyun YongHyeon 	}
1213f6bc9430SPyun YongHyeon 
1214f6bc9430SPyun YongHyeon fail:
1215f6bc9430SPyun YongHyeon 	return (error);
1216f6bc9430SPyun YongHyeon }
1217f6bc9430SPyun YongHyeon 
1218f6bc9430SPyun YongHyeon static void
1219f6bc9430SPyun YongHyeon nge_dma_free(struct nge_softc *sc)
1220f6bc9430SPyun YongHyeon {
1221f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1222f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1223f6bc9430SPyun YongHyeon 	int i;
1224f6bc9430SPyun YongHyeon 
1225f6bc9430SPyun YongHyeon 	/* Tx ring. */
1226f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_ring_tag) {
1227f6bc9430SPyun YongHyeon 		if (sc->nge_cdata.nge_tx_ring_map)
1228f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_ring_tag,
1229f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_tx_ring_map);
1230f6bc9430SPyun YongHyeon 		if (sc->nge_cdata.nge_tx_ring_map &&
1231f6bc9430SPyun YongHyeon 		    sc->nge_rdata.nge_tx_ring)
1232f6bc9430SPyun YongHyeon 			bus_dmamem_free(sc->nge_cdata.nge_tx_ring_tag,
1233f6bc9430SPyun YongHyeon 			    sc->nge_rdata.nge_tx_ring,
1234f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_tx_ring_map);
1235f6bc9430SPyun YongHyeon 		sc->nge_rdata.nge_tx_ring = NULL;
1236f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_ring_map = NULL;
1237f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_tx_ring_tag);
1238f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_ring_tag = NULL;
1239f6bc9430SPyun YongHyeon 	}
1240f6bc9430SPyun YongHyeon 	/* Rx ring. */
1241f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_rx_ring_tag) {
1242f6bc9430SPyun YongHyeon 		if (sc->nge_cdata.nge_rx_ring_map)
1243f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_rx_ring_tag,
1244f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_ring_map);
1245f6bc9430SPyun YongHyeon 		if (sc->nge_cdata.nge_rx_ring_map &&
1246f6bc9430SPyun YongHyeon 		    sc->nge_rdata.nge_rx_ring)
1247f6bc9430SPyun YongHyeon 			bus_dmamem_free(sc->nge_cdata.nge_rx_ring_tag,
1248f6bc9430SPyun YongHyeon 			    sc->nge_rdata.nge_rx_ring,
1249f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_ring_map);
1250f6bc9430SPyun YongHyeon 		sc->nge_rdata.nge_rx_ring = NULL;
1251f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_ring_map = NULL;
1252f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_rx_ring_tag);
1253f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_ring_tag = NULL;
1254f6bc9430SPyun YongHyeon 	}
1255f6bc9430SPyun YongHyeon 	/* Tx buffers. */
1256f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_tag) {
1257f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TX_RING_CNT; i++) {
1258f6bc9430SPyun YongHyeon 			txd = &sc->nge_cdata.nge_txdesc[i];
1259f6bc9430SPyun YongHyeon 			if (txd->tx_dmamap) {
1260f6bc9430SPyun YongHyeon 				bus_dmamap_destroy(sc->nge_cdata.nge_tx_tag,
1261f6bc9430SPyun YongHyeon 				    txd->tx_dmamap);
1262f6bc9430SPyun YongHyeon 				txd->tx_dmamap = NULL;
1263f6bc9430SPyun YongHyeon 			}
1264f6bc9430SPyun YongHyeon 		}
1265f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_tx_tag);
1266f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_tag = NULL;
1267f6bc9430SPyun YongHyeon 	}
1268f6bc9430SPyun YongHyeon 	/* Rx buffers. */
1269f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_rx_tag) {
1270f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_RX_RING_CNT; i++) {
1271f6bc9430SPyun YongHyeon 			rxd = &sc->nge_cdata.nge_rxdesc[i];
1272f6bc9430SPyun YongHyeon 			if (rxd->rx_dmamap) {
1273f6bc9430SPyun YongHyeon 				bus_dmamap_destroy(sc->nge_cdata.nge_rx_tag,
1274f6bc9430SPyun YongHyeon 				    rxd->rx_dmamap);
1275f6bc9430SPyun YongHyeon 				rxd->rx_dmamap = NULL;
1276f6bc9430SPyun YongHyeon 			}
1277f6bc9430SPyun YongHyeon 		}
1278f6bc9430SPyun YongHyeon 		if (sc->nge_cdata.nge_rx_sparemap) {
1279f6bc9430SPyun YongHyeon 			bus_dmamap_destroy(sc->nge_cdata.nge_rx_tag,
1280f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_sparemap);
1281f6bc9430SPyun YongHyeon 			sc->nge_cdata.nge_rx_sparemap = 0;
1282f6bc9430SPyun YongHyeon 		}
1283f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_rx_tag);
1284f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_tag = NULL;
1285f6bc9430SPyun YongHyeon 	}
1286f6bc9430SPyun YongHyeon 
1287f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_parent_tag) {
1288f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_parent_tag);
1289f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_parent_tag = NULL;
1290f6bc9430SPyun YongHyeon 	}
1291f6bc9430SPyun YongHyeon }
1292f6bc9430SPyun YongHyeon 
1293ce4946daSBill Paul /*
1294ce4946daSBill Paul  * Initialize the transmit descriptors.
1295ce4946daSBill Paul  */
1296eaabec55SAlfred Perlstein static int
1297284c81cbSPyun YongHyeon nge_list_tx_init(struct nge_softc *sc)
1298ce4946daSBill Paul {
1299f6bc9430SPyun YongHyeon 	struct nge_ring_data *rd;
1300f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1301f6bc9430SPyun YongHyeon 	bus_addr_t addr;
1302ce4946daSBill Paul 	int i;
1303ce4946daSBill Paul 
1304f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_prod = 0;
1305f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cons = 0;
1306f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cnt = 0;
1307ce4946daSBill Paul 
1308f6bc9430SPyun YongHyeon 	rd = &sc->nge_rdata;
1309f6bc9430SPyun YongHyeon 	bzero(rd->nge_tx_ring, sizeof(struct nge_desc) * NGE_TX_RING_CNT);
1310f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
1311f6bc9430SPyun YongHyeon 		if (i == NGE_TX_RING_CNT - 1)
1312f6bc9430SPyun YongHyeon 			addr = NGE_TX_RING_ADDR(sc, 0);
1313f6bc9430SPyun YongHyeon 		else
1314f6bc9430SPyun YongHyeon 			addr = NGE_TX_RING_ADDR(sc, i + 1);
1315f6bc9430SPyun YongHyeon 		rd->nge_tx_ring[i].nge_next = htole32(NGE_ADDR_LO(addr));
1316f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
1317f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1318ce4946daSBill Paul 	}
1319ce4946daSBill Paul 
1320f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
1321f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map,
1322f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1323ce4946daSBill Paul 
1324ce4946daSBill Paul 	return (0);
1325ce4946daSBill Paul }
1326ce4946daSBill Paul 
1327ce4946daSBill Paul /*
1328ce4946daSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
1329ce4946daSBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
1330ce4946daSBill Paul  * points back to the first.
1331ce4946daSBill Paul  */
1332eaabec55SAlfred Perlstein static int
1333284c81cbSPyun YongHyeon nge_list_rx_init(struct nge_softc *sc)
1334ce4946daSBill Paul {
1335f6bc9430SPyun YongHyeon 	struct nge_ring_data *rd;
1336f6bc9430SPyun YongHyeon 	bus_addr_t addr;
1337ce4946daSBill Paul 	int i;
1338ce4946daSBill Paul 
1339f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_rx_cons = 0;
1340ad6c618bSBill Paul 	sc->nge_head = sc->nge_tail = NULL;
1341ce4946daSBill Paul 
1342f6bc9430SPyun YongHyeon 	rd = &sc->nge_rdata;
1343f6bc9430SPyun YongHyeon 	bzero(rd->nge_rx_ring, sizeof(struct nge_desc) * NGE_RX_RING_CNT);
1344f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
1345f6bc9430SPyun YongHyeon 		if (nge_newbuf(sc, i) != 0)
1346f6bc9430SPyun YongHyeon 			return (ENOBUFS);
1347f6bc9430SPyun YongHyeon 		if (i == NGE_RX_RING_CNT - 1)
1348f6bc9430SPyun YongHyeon 			addr = NGE_RX_RING_ADDR(sc, 0);
1349f6bc9430SPyun YongHyeon 		else
1350f6bc9430SPyun YongHyeon 			addr = NGE_RX_RING_ADDR(sc, i + 1);
1351f6bc9430SPyun YongHyeon 		rd->nge_rx_ring[i].nge_next = htole32(NGE_ADDR_LO(addr));
1352f6bc9430SPyun YongHyeon 	}
1353f6bc9430SPyun YongHyeon 
1354f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1355f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map,
1356f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1357f6bc9430SPyun YongHyeon 
1358ce4946daSBill Paul 	return (0);
1359ce4946daSBill Paul }
1360ce4946daSBill Paul 
1361f6bc9430SPyun YongHyeon static __inline void
1362f6bc9430SPyun YongHyeon nge_discard_rxbuf(struct nge_softc *sc, int idx)
1363f6bc9430SPyun YongHyeon {
1364f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1365f6bc9430SPyun YongHyeon 
1366f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_rx_ring[idx];
1367f6bc9430SPyun YongHyeon 	desc->nge_cmdsts = htole32(MCLBYTES - sizeof(uint64_t));
1368f6bc9430SPyun YongHyeon 	desc->nge_extsts = 0;
1369f6bc9430SPyun YongHyeon }
1370f6bc9430SPyun YongHyeon 
1371ce4946daSBill Paul /*
1372ce4946daSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
1373ce4946daSBill Paul  */
1374eaabec55SAlfred Perlstein static int
1375f6bc9430SPyun YongHyeon nge_newbuf(struct nge_softc *sc, int idx)
1376ce4946daSBill Paul {
1377f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1378f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1379f6bc9430SPyun YongHyeon 	struct mbuf *m;
1380f6bc9430SPyun YongHyeon 	bus_dma_segment_t segs[1];
1381f6bc9430SPyun YongHyeon 	bus_dmamap_t map;
1382f6bc9430SPyun YongHyeon 	int nsegs;
1383ce4946daSBill Paul 
1384*c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13856c772336SSam Leffler 	if (m == NULL)
1386ce4946daSBill Paul 		return (ENOBUFS);
1387ad6c618bSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
13883929ff51SPyun YongHyeon 	m_adj(m, sizeof(uint64_t));
1389ce4946daSBill Paul 
1390f6bc9430SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_rx_tag,
1391f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_sparemap, m, segs, &nsegs, 0) != 0) {
1392f6bc9430SPyun YongHyeon 		m_freem(m);
1393f6bc9430SPyun YongHyeon 		return (ENOBUFS);
1394f6bc9430SPyun YongHyeon 	}
1395f6bc9430SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1396f6bc9430SPyun YongHyeon 
1397f6bc9430SPyun YongHyeon 	rxd = &sc->nge_cdata.nge_rxdesc[idx];
1398f6bc9430SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1399f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap,
1400f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1401f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap);
1402f6bc9430SPyun YongHyeon 	}
1403f6bc9430SPyun YongHyeon 	map = rxd->rx_dmamap;
1404f6bc9430SPyun YongHyeon 	rxd->rx_dmamap = sc->nge_cdata.nge_rx_sparemap;
1405f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_rx_sparemap = map;
1406f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap,
1407f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
1408f6bc9430SPyun YongHyeon 	rxd->rx_m = m;
1409f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_rx_ring[idx];
1410f6bc9430SPyun YongHyeon 	desc->nge_ptr = htole32(NGE_ADDR_LO(segs[0].ds_addr));
1411f6bc9430SPyun YongHyeon 	desc->nge_cmdsts = htole32(segs[0].ds_len);
1412f6bc9430SPyun YongHyeon 	desc->nge_extsts = 0;
1413ce4946daSBill Paul 
1414ce4946daSBill Paul 	return (0);
1415ce4946daSBill Paul }
1416ce4946daSBill Paul 
1417f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
1418ad6c618bSBill Paul static __inline void
1419284c81cbSPyun YongHyeon nge_fixup_rx(struct mbuf *m)
1420ce4946daSBill Paul {
1421ce4946daSBill Paul 	int			i;
1422ad6c618bSBill Paul 	uint16_t		*src, *dst;
1423ce4946daSBill Paul 
1424ad6c618bSBill Paul 	src = mtod(m, uint16_t *);
1425ad6c618bSBill Paul 	dst = src - 1;
1426ce4946daSBill Paul 
1427ad6c618bSBill Paul 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1428ad6c618bSBill Paul 		*dst++ = *src++;
1429ce4946daSBill Paul 
1430ad6c618bSBill Paul 	m->m_data -= ETHER_ALIGN;
1431ce4946daSBill Paul }
1432ad6c618bSBill Paul #endif
1433ad6c618bSBill Paul 
1434ce4946daSBill Paul /*
1435ce4946daSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
1436ce4946daSBill Paul  * the higher level protocols.
1437ce4946daSBill Paul  */
143856e13f2aSAttilio Rao static int
1439284c81cbSPyun YongHyeon nge_rxeof(struct nge_softc *sc)
1440ce4946daSBill Paul {
1441ce4946daSBill Paul 	struct mbuf *m;
1442ce4946daSBill Paul 	struct ifnet *ifp;
1443ce4946daSBill Paul 	struct nge_desc *cur_rx;
1444f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
144556e13f2aSAttilio Rao 	int cons, prog, rx_npkts, total_len;
1446f6bc9430SPyun YongHyeon 	uint32_t cmdsts, extsts;
1447ce4946daSBill Paul 
1448ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1449f6bc9430SPyun YongHyeon 
1450fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1451f6bc9430SPyun YongHyeon 	cons = sc->nge_cdata.nge_rx_cons;
145256e13f2aSAttilio Rao 	rx_npkts = 0;
1453ce4946daSBill Paul 
1454f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1455f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map,
1456f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1457ce4946daSBill Paul 
1458f6bc9430SPyun YongHyeon 	for (prog = 0; prog < NGE_RX_RING_CNT &&
1459f6bc9430SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1460f6bc9430SPyun YongHyeon 	    NGE_INC(cons, NGE_RX_RING_CNT)) {
1461196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
146240929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
1463196c0df6SHidetoshi Shimokawa 			if (sc->rxcycles <= 0)
1464196c0df6SHidetoshi Shimokawa 				break;
1465196c0df6SHidetoshi Shimokawa 			sc->rxcycles--;
1466196c0df6SHidetoshi Shimokawa 		}
146740929967SGleb Smirnoff #endif
1468f6bc9430SPyun YongHyeon 		cur_rx = &sc->nge_rdata.nge_rx_ring[cons];
1469f6bc9430SPyun YongHyeon 		cmdsts = le32toh(cur_rx->nge_cmdsts);
1470f6bc9430SPyun YongHyeon 		extsts = le32toh(cur_rx->nge_extsts);
1471f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_OWN) == 0)
1472f6bc9430SPyun YongHyeon 			break;
1473f6bc9430SPyun YongHyeon 		prog++;
1474f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[cons];
1475f6bc9430SPyun YongHyeon 		m = rxd->rx_m;
1476f6bc9430SPyun YongHyeon 		total_len = cmdsts & NGE_CMDSTS_BUFLEN;
1477196c0df6SHidetoshi Shimokawa 
1478f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_MORE) != 0) {
1479f6bc9430SPyun YongHyeon 			if (nge_newbuf(sc, cons) != 0) {
1480f6bc9430SPyun YongHyeon 				ifp->if_iqdrops++;
1481f6bc9430SPyun YongHyeon 				if (sc->nge_head != NULL) {
1482f6bc9430SPyun YongHyeon 					m_freem(sc->nge_head);
1483f6bc9430SPyun YongHyeon 					sc->nge_head = sc->nge_tail = NULL;
1484f6bc9430SPyun YongHyeon 				}
1485f6bc9430SPyun YongHyeon 				nge_discard_rxbuf(sc, cons);
1486f6bc9430SPyun YongHyeon 				continue;
1487f6bc9430SPyun YongHyeon 			}
1488ad6c618bSBill Paul 			m->m_len = total_len;
1489ad6c618bSBill Paul 			if (sc->nge_head == NULL) {
1490ad6c618bSBill Paul 				m->m_pkthdr.len = total_len;
1491ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = m;
1492ad6c618bSBill Paul 			} else {
1493ad6c618bSBill Paul 				m->m_flags &= ~M_PKTHDR;
1494ad6c618bSBill Paul 				sc->nge_head->m_pkthdr.len += total_len;
1495ad6c618bSBill Paul 				sc->nge_tail->m_next = m;
1496ad6c618bSBill Paul 				sc->nge_tail = m;
1497ad6c618bSBill Paul 			}
1498ad6c618bSBill Paul 			continue;
1499ad6c618bSBill Paul 		}
1500ad6c618bSBill Paul 
1501ce4946daSBill Paul 		/*
1502ce4946daSBill Paul 		 * If an error occurs, update stats, clear the
1503ce4946daSBill Paul 		 * status word and leave the mbuf cluster in place:
1504ce4946daSBill Paul 		 * it should simply get re-used next time this descriptor
1505ce4946daSBill Paul 	 	 * comes up in the ring.
1506ce4946daSBill Paul 		 */
1507f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) {
1508f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_RXSTAT_RUNT) &&
1509f6bc9430SPyun YongHyeon 			    total_len >= (ETHER_MIN_LEN - ETHER_CRC_LEN - 4)) {
1510f6bc9430SPyun YongHyeon 				/*
1511f6bc9430SPyun YongHyeon 				 * Work-around hardware bug, accept runt frames
1512f6bc9430SPyun YongHyeon 				 * if its length is larger than or equal to 56.
1513f6bc9430SPyun YongHyeon 				 */
1514f6bc9430SPyun YongHyeon 			} else {
1515f6bc9430SPyun YongHyeon 				/*
1516f6bc9430SPyun YongHyeon 				 * Input error counters are updated by hardware.
1517f6bc9430SPyun YongHyeon 				 */
1518ad6c618bSBill Paul 				if (sc->nge_head != NULL) {
1519ad6c618bSBill Paul 					m_freem(sc->nge_head);
1520ad6c618bSBill Paul 					sc->nge_head = sc->nge_tail = NULL;
1521ad6c618bSBill Paul 				}
1522f6bc9430SPyun YongHyeon 				nge_discard_rxbuf(sc, cons);
1523ce4946daSBill Paul 				continue;
1524ce4946daSBill Paul 			}
1525f6bc9430SPyun YongHyeon 		}
1526ce4946daSBill Paul 
1527ad6c618bSBill Paul 		/* Try conjure up a replacement mbuf. */
1528ad6c618bSBill Paul 
1529f6bc9430SPyun YongHyeon 		if (nge_newbuf(sc, cons) != 0) {
1530f6bc9430SPyun YongHyeon 			ifp->if_iqdrops++;
1531ad6c618bSBill Paul 			if (sc->nge_head != NULL) {
1532ad6c618bSBill Paul 				m_freem(sc->nge_head);
1533ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = NULL;
1534ad6c618bSBill Paul 			}
1535f6bc9430SPyun YongHyeon 			nge_discard_rxbuf(sc, cons);
1536ad6c618bSBill Paul 			continue;
1537ad6c618bSBill Paul 		}
1538ad6c618bSBill Paul 
1539f6bc9430SPyun YongHyeon 		/* Chain received mbufs. */
1540ad6c618bSBill Paul 		if (sc->nge_head != NULL) {
1541ad6c618bSBill Paul 			m->m_len = total_len;
1542ad6c618bSBill Paul 			m->m_flags &= ~M_PKTHDR;
1543ad6c618bSBill Paul 			sc->nge_tail->m_next = m;
1544ad6c618bSBill Paul 			m = sc->nge_head;
1545ad6c618bSBill Paul 			m->m_pkthdr.len += total_len;
1546ad6c618bSBill Paul 			sc->nge_head = sc->nge_tail = NULL;
1547ad6c618bSBill Paul 		} else
1548ad6c618bSBill Paul 			m->m_pkthdr.len = m->m_len = total_len;
1549ad6c618bSBill Paul 
1550ce4946daSBill Paul 		/*
1551ce4946daSBill Paul 		 * Ok. NatSemi really screwed up here. This is the
1552ce4946daSBill Paul 		 * only gigE chip I know of with alignment constraints
1553ce4946daSBill Paul 		 * on receive buffers. RX buffers must be 64-bit aligned.
1554ce4946daSBill Paul 		 */
1555962315f6SBill Paul 		/*
1556962315f6SBill Paul 		 * By popular demand, ignore the alignment problems
1557f6bc9430SPyun YongHyeon 		 * on the non-strict alignment platform. The performance hit
1558962315f6SBill Paul 		 * incurred due to unaligned accesses is much smaller
1559962315f6SBill Paul 		 * than the hit produced by forcing buffer copies all
1560962315f6SBill Paul 		 * the time, especially with jumbo frames. We still
1561962315f6SBill Paul 		 * need to fix up the alignment everywhere else though.
1562962315f6SBill Paul 		 */
1563f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
1564ad6c618bSBill Paul 		nge_fixup_rx(m);
1565962315f6SBill Paul #endif
1566ad6c618bSBill Paul 		m->m_pkthdr.rcvif = ifp;
1567f6bc9430SPyun YongHyeon 		ifp->if_ipackets++;
1568ce4946daSBill Paul 
1569f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1570ce4946daSBill Paul 			/* Do IP checksum checking. */
1571f6bc9430SPyun YongHyeon 			if ((extsts & NGE_RXEXTSTS_IPPKT) != 0)
1572ce4946daSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1573f6bc9430SPyun YongHyeon 			if ((extsts & NGE_RXEXTSTS_IPCSUMERR) == 0)
157401702579SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
15752195de46SBill Paul 			if ((extsts & NGE_RXEXTSTS_TCPPKT &&
15762195de46SBill Paul 			    !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) ||
15772195de46SBill Paul 			    (extsts & NGE_RXEXTSTS_UDPPKT &&
15782195de46SBill Paul 			    !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) {
15792195de46SBill Paul 				m->m_pkthdr.csum_flags |=
15802195de46SBill Paul 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1581c9215605SBill Paul 				m->m_pkthdr.csum_data = 0xffff;
15822195de46SBill Paul 			}
1583f6bc9430SPyun YongHyeon 		}
1584ce4946daSBill Paul 
1585ce4946daSBill Paul 		/*
1586ce4946daSBill Paul 		 * If we received a packet with a vlan tag, pass it
1587ce4946daSBill Paul 		 * to vlan_input() instead of ether_input().
1588ce4946daSBill Paul 		 */
1589f6bc9430SPyun YongHyeon 		if ((extsts & NGE_RXEXTSTS_VLANPKT) != 0 &&
1590f6bc9430SPyun YongHyeon 		    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
159178ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
1592f6bc9430SPyun YongHyeon 			    bswap16(extsts & NGE_RXEXTSTS_VTCI);
159378ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1594ce4946daSBill Paul 		}
1595ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1596673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
1597ad6c618bSBill Paul 		NGE_LOCK(sc);
159856e13f2aSAttilio Rao 		rx_npkts++;
1599ce4946daSBill Paul 	}
1600ce4946daSBill Paul 
1601f6bc9430SPyun YongHyeon 	if (prog > 0) {
1602f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_cons = cons;
1603f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1604f6bc9430SPyun YongHyeon 		    sc->nge_cdata.nge_rx_ring_map,
1605f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1606f6bc9430SPyun YongHyeon 	}
160756e13f2aSAttilio Rao 	return (rx_npkts);
1608ce4946daSBill Paul }
1609ce4946daSBill Paul 
1610ce4946daSBill Paul /*
1611ce4946daSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
1612ce4946daSBill Paul  * the list buffers.
1613ce4946daSBill Paul  */
1614eaabec55SAlfred Perlstein static void
1615284c81cbSPyun YongHyeon nge_txeof(struct nge_softc *sc)
1616ce4946daSBill Paul {
16171e73ec7dSRuslan Ermilov 	struct nge_desc	*cur_tx;
1618f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1619ce4946daSBill Paul 	struct ifnet *ifp;
1620f6bc9430SPyun YongHyeon 	uint32_t cmdsts;
1621f6bc9430SPyun YongHyeon 	int cons, prod;
1622ce4946daSBill Paul 
1623ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1624fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1625ce4946daSBill Paul 
1626f6bc9430SPyun YongHyeon 	cons = sc->nge_cdata.nge_tx_cons;
1627f6bc9430SPyun YongHyeon 	prod = sc->nge_cdata.nge_tx_prod;
1628f6bc9430SPyun YongHyeon 	if (cons == prod)
1629f6bc9430SPyun YongHyeon 		return;
1630f6bc9430SPyun YongHyeon 
1631f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
1632f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map,
1633f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1634f6bc9430SPyun YongHyeon 
1635ce4946daSBill Paul 	/*
1636ce4946daSBill Paul 	 * Go through our tx list and free mbufs for those
1637ce4946daSBill Paul 	 * frames that have been transmitted.
1638ce4946daSBill Paul 	 */
1639f6bc9430SPyun YongHyeon 	for (; cons != prod; NGE_INC(cons, NGE_TX_RING_CNT)) {
1640f6bc9430SPyun YongHyeon 		cur_tx = &sc->nge_rdata.nge_tx_ring[cons];
1641f6bc9430SPyun YongHyeon 		cmdsts = le32toh(cur_tx->nge_cmdsts);
1642f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_OWN) != 0)
1643ce4946daSBill Paul 			break;
1644ce4946daSBill Paul 		sc->nge_cdata.nge_tx_cnt--;
164513f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1646f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_MORE) != 0)
1647f6bc9430SPyun YongHyeon 			continue;
1648f6bc9430SPyun YongHyeon 
1649f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[cons];
1650f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap,
1651f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_POSTWRITE);
1652f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap);
1653f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) {
1654f6bc9430SPyun YongHyeon 			ifp->if_oerrors++;
1655f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_TXSTAT_EXCESSCOLLS) != 0)
1656f6bc9430SPyun YongHyeon 				ifp->if_collisions++;
1657f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_TXSTAT_OUTOFWINCOLL) != 0)
1658f6bc9430SPyun YongHyeon 				ifp->if_collisions++;
1659f6bc9430SPyun YongHyeon 		} else
1660f6bc9430SPyun YongHyeon 			ifp->if_opackets++;
1661f6bc9430SPyun YongHyeon 
1662f6bc9430SPyun YongHyeon 		ifp->if_collisions += (cmdsts & NGE_TXSTAT_COLLCNT) >> 16;
1663f6bc9430SPyun YongHyeon 		KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!\n",
1664f6bc9430SPyun YongHyeon 		    __func__));
1665f6bc9430SPyun YongHyeon 		m_freem(txd->tx_m);
1666f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1667ce4946daSBill Paul 	}
1668ce4946daSBill Paul 
1669f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cons = cons;
1670f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_cnt == 0)
1671f6bc9430SPyun YongHyeon 		sc->nge_watchdog_timer = 0;
1672ce4946daSBill Paul }
1673ce4946daSBill Paul 
1674eaabec55SAlfred Perlstein static void
1675284c81cbSPyun YongHyeon nge_tick(void *xsc)
1676ce4946daSBill Paul {
1677ce4946daSBill Paul 	struct nge_softc *sc;
1678ad6c618bSBill Paul 	struct mii_data *mii;
1679ad6c618bSBill Paul 
1680646abee6SJohn Baldwin 	sc = xsc;
1681ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1682ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
1683ce4946daSBill Paul 	mii_tick(mii);
1684f6bc9430SPyun YongHyeon 	/*
1685f6bc9430SPyun YongHyeon 	 * For PHYs that does not reset established link, it is
1686f6bc9430SPyun YongHyeon 	 * necessary to check whether driver still have a valid
1687f6bc9430SPyun YongHyeon 	 * link(e.g link state change callback is not called).
1688f6bc9430SPyun YongHyeon 	 * Otherwise, driver think it lost link because driver
1689f6bc9430SPyun YongHyeon 	 * initialization routine clears link state flag.
1690f6bc9430SPyun YongHyeon 	 */
1691f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_LINK) == 0)
1692f6bc9430SPyun YongHyeon 		nge_miibus_statchg(sc->nge_dev);
1693f6bc9430SPyun YongHyeon 	nge_stats_update(sc);
1694f6bc9430SPyun YongHyeon 	nge_watchdog(sc);
1695ad6c618bSBill Paul 	callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc);
1696ce4946daSBill Paul }
1697ce4946daSBill Paul 
1698f6bc9430SPyun YongHyeon static void
1699f6bc9430SPyun YongHyeon nge_stats_update(struct nge_softc *sc)
1700f6bc9430SPyun YongHyeon {
1701f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
1702f6bc9430SPyun YongHyeon 	struct nge_stats now, *stats, *nstats;
1703f6bc9430SPyun YongHyeon 
1704f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
1705f6bc9430SPyun YongHyeon 
1706f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
1707f6bc9430SPyun YongHyeon 	stats = &now;
1708f6bc9430SPyun YongHyeon 	stats->rx_pkts_errs =
1709f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRPKT) & 0xFFFF;
1710f6bc9430SPyun YongHyeon 	stats->rx_crc_errs =
1711f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRFCS) & 0xFFFF;
1712f6bc9430SPyun YongHyeon 	stats->rx_fifo_oflows =
1713f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRMISSEDPKT) & 0xFFFF;
1714f6bc9430SPyun YongHyeon 	stats->rx_align_errs =
1715f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRALIGN) & 0xFFFF;
1716f6bc9430SPyun YongHyeon 	stats->rx_sym_errs =
1717f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRSYM) & 0xFFFF;
1718f6bc9430SPyun YongHyeon 	stats->rx_pkts_jumbos =
1719f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRGIANT) & 0xFFFF;
1720f6bc9430SPyun YongHyeon 	stats->rx_len_errs =
1721f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRRANGLEN) & 0xFFFF;
1722f6bc9430SPyun YongHyeon 	stats->rx_unctl_frames =
1723f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXBADOPCODE) & 0xFFFF;
1724f6bc9430SPyun YongHyeon 	stats->rx_pause =
1725f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXPAUSEPKTS) & 0xFFFF;
1726f6bc9430SPyun YongHyeon 	stats->tx_pause =
1727f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_TXPAUSEPKTS) & 0xFFFF;
1728f6bc9430SPyun YongHyeon 	stats->tx_seq_errs =
1729f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_TXERRSQE) & 0xFF;
1730f6bc9430SPyun YongHyeon 
1731f6bc9430SPyun YongHyeon 	/*
1732f6bc9430SPyun YongHyeon 	 * Since we've accept errored frames exclude Rx length errors.
1733f6bc9430SPyun YongHyeon 	 */
1734f6bc9430SPyun YongHyeon 	ifp->if_ierrors += stats->rx_pkts_errs + stats->rx_crc_errs +
1735f6bc9430SPyun YongHyeon 	    stats->rx_fifo_oflows + stats->rx_sym_errs;
1736f6bc9430SPyun YongHyeon 
1737f6bc9430SPyun YongHyeon 	nstats = &sc->nge_stats;
1738f6bc9430SPyun YongHyeon 	nstats->rx_pkts_errs += stats->rx_pkts_errs;
1739f6bc9430SPyun YongHyeon 	nstats->rx_crc_errs += stats->rx_crc_errs;
1740f6bc9430SPyun YongHyeon 	nstats->rx_fifo_oflows += stats->rx_fifo_oflows;
1741f6bc9430SPyun YongHyeon 	nstats->rx_align_errs += stats->rx_align_errs;
1742f6bc9430SPyun YongHyeon 	nstats->rx_sym_errs += stats->rx_sym_errs;
1743f6bc9430SPyun YongHyeon 	nstats->rx_pkts_jumbos += stats->rx_pkts_jumbos;
1744f6bc9430SPyun YongHyeon 	nstats->rx_len_errs += stats->rx_len_errs;
1745f6bc9430SPyun YongHyeon 	nstats->rx_unctl_frames += stats->rx_unctl_frames;
1746f6bc9430SPyun YongHyeon 	nstats->rx_pause += stats->rx_pause;
1747f6bc9430SPyun YongHyeon 	nstats->tx_pause += stats->tx_pause;
1748f6bc9430SPyun YongHyeon 	nstats->tx_seq_errs += stats->tx_seq_errs;
1749f6bc9430SPyun YongHyeon }
1750f6bc9430SPyun YongHyeon 
1751196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1752196c0df6SHidetoshi Shimokawa static poll_handler_t nge_poll;
1753196c0df6SHidetoshi Shimokawa 
175456e13f2aSAttilio Rao static int
1755196c0df6SHidetoshi Shimokawa nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1756196c0df6SHidetoshi Shimokawa {
1757f6bc9430SPyun YongHyeon 	struct nge_softc *sc;
175856e13f2aSAttilio Rao 	int rx_npkts = 0;
1759f6bc9430SPyun YongHyeon 
1760f6bc9430SPyun YongHyeon 	sc = ifp->if_softc;
1761196c0df6SHidetoshi Shimokawa 
1762ad6c618bSBill Paul 	NGE_LOCK(sc);
1763f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1764ad6c618bSBill Paul 		NGE_UNLOCK(sc);
176556e13f2aSAttilio Rao 		return (rx_npkts);
1766196c0df6SHidetoshi Shimokawa 	}
1767196c0df6SHidetoshi Shimokawa 
1768196c0df6SHidetoshi Shimokawa 	/*
1769196c0df6SHidetoshi Shimokawa 	 * On the nge, reading the status register also clears it.
1770196c0df6SHidetoshi Shimokawa 	 * So before returning to intr mode we must make sure that all
1771196c0df6SHidetoshi Shimokawa 	 * possible pending sources of interrupts have been served.
1772196c0df6SHidetoshi Shimokawa 	 * In practice this means run to completion the *eof routines,
1773f6bc9430SPyun YongHyeon 	 * and then call the interrupt routine.
1774196c0df6SHidetoshi Shimokawa 	 */
1775196c0df6SHidetoshi Shimokawa 	sc->rxcycles = count;
177656e13f2aSAttilio Rao 	rx_npkts = nge_rxeof(sc);
1777196c0df6SHidetoshi Shimokawa 	nge_txeof(sc);
1778f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1779ad6c618bSBill Paul 		nge_start_locked(ifp);
1780196c0df6SHidetoshi Shimokawa 
1781196c0df6SHidetoshi Shimokawa 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
17823929ff51SPyun YongHyeon 		uint32_t	status;
1783196c0df6SHidetoshi Shimokawa 
1784196c0df6SHidetoshi Shimokawa 		/* Reading the ISR register clears all interrupts. */
1785196c0df6SHidetoshi Shimokawa 		status = CSR_READ_4(sc, NGE_ISR);
1786196c0df6SHidetoshi Shimokawa 
1787f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW)) != 0)
178856e13f2aSAttilio Rao 			rx_npkts += nge_rxeof(sc);
1789196c0df6SHidetoshi Shimokawa 
1790f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_RX_IDLE) != 0)
1791196c0df6SHidetoshi Shimokawa 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1792196c0df6SHidetoshi Shimokawa 
1793f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_SYSERR) != 0) {
1794f6bc9430SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1795ad6c618bSBill Paul 			nge_init_locked(sc);
1796196c0df6SHidetoshi Shimokawa 		}
1797196c0df6SHidetoshi Shimokawa 	}
1798ad6c618bSBill Paul 	NGE_UNLOCK(sc);
179956e13f2aSAttilio Rao 	return (rx_npkts);
1800196c0df6SHidetoshi Shimokawa }
1801196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1802196c0df6SHidetoshi Shimokawa 
1803eaabec55SAlfred Perlstein static void
1804284c81cbSPyun YongHyeon nge_intr(void *arg)
1805ce4946daSBill Paul {
1806ce4946daSBill Paul 	struct nge_softc *sc;
1807ce4946daSBill Paul 	struct ifnet *ifp;
18083929ff51SPyun YongHyeon 	uint32_t status;
1809ce4946daSBill Paul 
1810f6bc9430SPyun YongHyeon 	sc = (struct nge_softc *)arg;
1811fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1812ce4946daSBill Paul 
1813ad6c618bSBill Paul 	NGE_LOCK(sc);
1814196c0df6SHidetoshi Shimokawa 
1815f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_SUSPENDED) != 0)
1816f6bc9430SPyun YongHyeon 		goto done_locked;
1817f6bc9430SPyun YongHyeon 
1818f6bc9430SPyun YongHyeon 	/* Reading the ISR register clears all interrupts. */
1819f6bc9430SPyun YongHyeon 	status = CSR_READ_4(sc, NGE_ISR);
1820f6bc9430SPyun YongHyeon 	if (status == 0xffffffff || (status & NGE_INTRS) == 0)
1821f6bc9430SPyun YongHyeon 		goto done_locked;
1822f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
1823f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1824f6bc9430SPyun YongHyeon 		goto done_locked;
1825f6bc9430SPyun YongHyeon #endif
1826f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1827f6bc9430SPyun YongHyeon 		goto done_locked;
1828ce4946daSBill Paul 
1829ce4946daSBill Paul 	/* Disable interrupts. */
1830ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
1831ce4946daSBill Paul 
18321f548804SDoug Ambrisko 	/* Data LED on for TBI mode */
1833f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
1834f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
1835f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) | NGE_GPIO_GP3_OUT);
18361f548804SDoug Ambrisko 
1837f6bc9430SPyun YongHyeon 	for (; (status & NGE_INTRS) != 0;) {
1838f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_TX_DESC_OK | NGE_ISR_TX_ERR |
1839f6bc9430SPyun YongHyeon 		    NGE_ISR_TX_OK | NGE_ISR_TX_IDLE)) != 0)
1840ce4946daSBill Paul 			nge_txeof(sc);
1841ce4946daSBill Paul 
1842f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_RX_DESC_OK | NGE_ISR_RX_ERR |
1843f6bc9430SPyun YongHyeon 		    NGE_ISR_RX_OFLOW | NGE_ISR_RX_FIFO_OFLOW |
1844f6bc9430SPyun YongHyeon 		    NGE_ISR_RX_IDLE | NGE_ISR_RX_OK)) != 0)
1845ce4946daSBill Paul 			nge_rxeof(sc);
1846ff7ed9f7SPoul-Henning Kamp 
1847f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_RX_IDLE) != 0)
1848ff7ed9f7SPoul-Henning Kamp 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1849ff7ed9f7SPoul-Henning Kamp 
1850f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_SYSERR) != 0) {
185113f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1852ad6c618bSBill Paul 			nge_init_locked(sc);
1853ce4946daSBill Paul 		}
1854f6bc9430SPyun YongHyeon 		/* Reading the ISR register clears all interrupts. */
1855f6bc9430SPyun YongHyeon 		status = CSR_READ_4(sc, NGE_ISR);
1856ce4946daSBill Paul 	}
1857ce4946daSBill Paul 
1858ce4946daSBill Paul 	/* Re-enable interrupts. */
1859ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1860ce4946daSBill Paul 
1861f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1862ad6c618bSBill Paul 		nge_start_locked(ifp);
1863ce4946daSBill Paul 
18641f548804SDoug Ambrisko 	/* Data LED off for TBI mode */
1865f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
1866f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
1867f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT);
18681f548804SDoug Ambrisko 
1869f6bc9430SPyun YongHyeon done_locked:
1870ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1871ce4946daSBill Paul }
1872ce4946daSBill Paul 
1873ce4946daSBill Paul /*
1874ce4946daSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1875ce4946daSBill Paul  * pointers to the fragment pointers.
1876ce4946daSBill Paul  */
1877eaabec55SAlfred Perlstein static int
1878f6bc9430SPyun YongHyeon nge_encap(struct nge_softc *sc, struct mbuf **m_head)
1879ce4946daSBill Paul {
1880f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd, *txd_last;
1881f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1882ce4946daSBill Paul 	struct mbuf *m;
1883f6bc9430SPyun YongHyeon 	bus_dmamap_t map;
1884f6bc9430SPyun YongHyeon 	bus_dma_segment_t txsegs[NGE_MAXTXSEGS];
1885f6bc9430SPyun YongHyeon 	int error, i, nsegs, prod, si;
1886ce4946daSBill Paul 
1887f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
1888ce4946daSBill Paul 
1889f6bc9430SPyun YongHyeon 	m = *m_head;
1890f6bc9430SPyun YongHyeon 	prod = sc->nge_cdata.nge_tx_prod;
1891f6bc9430SPyun YongHyeon 	txd = &sc->nge_cdata.nge_txdesc[prod];
1892f6bc9430SPyun YongHyeon 	txd_last = txd;
1893f6bc9430SPyun YongHyeon 	map = txd->tx_dmamap;
1894f6bc9430SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag, map,
1895f6bc9430SPyun YongHyeon 	    *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1896f6bc9430SPyun YongHyeon 	if (error == EFBIG) {
1897*c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, NGE_MAXTXSEGS);
1898f6bc9430SPyun YongHyeon 		if (m == NULL) {
1899f6bc9430SPyun YongHyeon 			m_freem(*m_head);
1900f6bc9430SPyun YongHyeon 			*m_head = NULL;
1901ce4946daSBill Paul 			return (ENOBUFS);
1902ce4946daSBill Paul 		}
1903f6bc9430SPyun YongHyeon 		*m_head = m;
1904f6bc9430SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag,
1905f6bc9430SPyun YongHyeon 		    map, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1906f6bc9430SPyun YongHyeon 		if (error != 0) {
1907f6bc9430SPyun YongHyeon 			m_freem(*m_head);
1908f6bc9430SPyun YongHyeon 			*m_head = NULL;
1909f6bc9430SPyun YongHyeon 			return (error);
1910f6bc9430SPyun YongHyeon 		}
1911f6bc9430SPyun YongHyeon 	} else if (error != 0)
1912f6bc9430SPyun YongHyeon 		return (error);
1913f6bc9430SPyun YongHyeon 	if (nsegs == 0) {
1914f6bc9430SPyun YongHyeon 		m_freem(*m_head);
1915f6bc9430SPyun YongHyeon 		*m_head = NULL;
1916f6bc9430SPyun YongHyeon 		return (EIO);
1917ce4946daSBill Paul 	}
1918ce4946daSBill Paul 
1919f6bc9430SPyun YongHyeon 	/* Check number of available descriptors. */
1920f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_cnt + nsegs >= (NGE_TX_RING_CNT - 1)) {
1921f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, map);
1922ce4946daSBill Paul 		return (ENOBUFS);
1923ce4946daSBill Paul 	}
1924ce4946daSBill Paul 
1925f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, map, BUS_DMASYNC_PREWRITE);
1926ce4946daSBill Paul 
1927f6bc9430SPyun YongHyeon 	si = prod;
1928f6bc9430SPyun YongHyeon 	for (i = 0; i < nsegs; i++) {
1929f6bc9430SPyun YongHyeon 		desc = &sc->nge_rdata.nge_tx_ring[prod];
1930f6bc9430SPyun YongHyeon 		desc->nge_ptr = htole32(NGE_ADDR_LO(txsegs[i].ds_addr));
1931f6bc9430SPyun YongHyeon 		if (i == 0)
1932f6bc9430SPyun YongHyeon 			desc->nge_cmdsts = htole32(txsegs[i].ds_len |
1933f6bc9430SPyun YongHyeon 			    NGE_CMDSTS_MORE);
1934f6bc9430SPyun YongHyeon 		else
1935f6bc9430SPyun YongHyeon 			desc->nge_cmdsts = htole32(txsegs[i].ds_len |
1936f6bc9430SPyun YongHyeon 			    NGE_CMDSTS_MORE | NGE_CMDSTS_OWN);
1937f6bc9430SPyun YongHyeon 		desc->nge_extsts = 0;
1938f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_cnt++;
1939f6bc9430SPyun YongHyeon 		NGE_INC(prod, NGE_TX_RING_CNT);
1940f6bc9430SPyun YongHyeon 	}
1941f6bc9430SPyun YongHyeon 	/* Update producer index. */
1942f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_prod = prod;
1943f6bc9430SPyun YongHyeon 
1944f6bc9430SPyun YongHyeon 	prod = (prod + NGE_TX_RING_CNT - 1) % NGE_TX_RING_CNT;
1945f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_tx_ring[prod];
1946f6bc9430SPyun YongHyeon 	/* Check if we have a VLAN tag to insert. */
1947f6bc9430SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0)
1948f6bc9430SPyun YongHyeon 		desc->nge_extsts |= htole32(NGE_TXEXTSTS_VLANPKT |
1949f6bc9430SPyun YongHyeon 		    bswap16(m->m_pkthdr.ether_vtag));
1950f6bc9430SPyun YongHyeon 	/* Set EOP on the last desciptor. */
1951f6bc9430SPyun YongHyeon 	desc->nge_cmdsts &= htole32(~NGE_CMDSTS_MORE);
1952f6bc9430SPyun YongHyeon 
1953f6bc9430SPyun YongHyeon 	/* Set checksum offload in the first descriptor. */
1954f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_tx_ring[si];
1955f6bc9430SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & NGE_CSUM_FEATURES) != 0) {
1956f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
1957f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_IPCSUM);
1958f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
1959f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_TCPCSUM);
1960f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
1961f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_UDPCSUM);
1962f6bc9430SPyun YongHyeon 	}
1963f6bc9430SPyun YongHyeon 	/* Lastly, turn the first descriptor ownership to hardware. */
1964f6bc9430SPyun YongHyeon 	desc->nge_cmdsts |= htole32(NGE_CMDSTS_OWN);
1965f6bc9430SPyun YongHyeon 
1966f6bc9430SPyun YongHyeon 	txd = &sc->nge_cdata.nge_txdesc[prod];
1967f6bc9430SPyun YongHyeon 	map = txd_last->tx_dmamap;
1968f6bc9430SPyun YongHyeon 	txd_last->tx_dmamap = txd->tx_dmamap;
1969f6bc9430SPyun YongHyeon 	txd->tx_dmamap = map;
1970f6bc9430SPyun YongHyeon 	txd->tx_m = m;
1971ce4946daSBill Paul 
1972ce4946daSBill Paul 	return (0);
1973ce4946daSBill Paul }
1974ce4946daSBill Paul 
1975ce4946daSBill Paul /*
1976ce4946daSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1977ce4946daSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1978ce4946daSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1979ce4946daSBill Paul  * physical addresses.
1980ce4946daSBill Paul  */
1981ce4946daSBill Paul 
1982eaabec55SAlfred Perlstein static void
1983284c81cbSPyun YongHyeon nge_start(struct ifnet *ifp)
1984ce4946daSBill Paul {
1985ce4946daSBill Paul 	struct nge_softc *sc;
1986ad6c618bSBill Paul 
1987ad6c618bSBill Paul 	sc = ifp->if_softc;
1988ad6c618bSBill Paul 	NGE_LOCK(sc);
1989ad6c618bSBill Paul 	nge_start_locked(ifp);
1990ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1991ad6c618bSBill Paul }
1992ad6c618bSBill Paul 
1993ad6c618bSBill Paul static void
1994284c81cbSPyun YongHyeon nge_start_locked(struct ifnet *ifp)
1995ad6c618bSBill Paul {
1996ad6c618bSBill Paul 	struct nge_softc *sc;
1997f6bc9430SPyun YongHyeon 	struct mbuf *m_head;
1998f6bc9430SPyun YongHyeon 	int enq;
1999ce4946daSBill Paul 
2000ce4946daSBill Paul 	sc = ifp->if_softc;
2001ce4946daSBill Paul 
2002f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2003f6bc9430SPyun YongHyeon 
2004f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2005f6bc9430SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->nge_flags & NGE_FLAG_LINK) == 0)
2006ce4946daSBill Paul 		return;
2007ce4946daSBill Paul 
2008f6bc9430SPyun YongHyeon 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2009f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_cnt < NGE_TX_RING_CNT - 2; ) {
2010f6bc9430SPyun YongHyeon 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2011ce4946daSBill Paul 		if (m_head == NULL)
2012ce4946daSBill Paul 			break;
2013f6bc9430SPyun YongHyeon 		/*
2014f6bc9430SPyun YongHyeon 		 * Pack the data into the transmit ring. If we
2015f6bc9430SPyun YongHyeon 		 * don't have room, set the OACTIVE flag and wait
2016f6bc9430SPyun YongHyeon 		 * for the NIC to drain the ring.
2017f6bc9430SPyun YongHyeon 		 */
2018f6bc9430SPyun YongHyeon 		if (nge_encap(sc, &m_head)) {
2019f6bc9430SPyun YongHyeon 			if (m_head == NULL)
2020f6bc9430SPyun YongHyeon 				break;
2021f6bc9430SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
202213f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2023ce4946daSBill Paul 			break;
2024ce4946daSBill Paul 		}
2025ce4946daSBill Paul 
2026f6bc9430SPyun YongHyeon 		enq++;
2027ce4946daSBill Paul 		/*
2028ce4946daSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2029ce4946daSBill Paul 		 * to him.
2030ce4946daSBill Paul 		 */
203159a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
2032ce4946daSBill Paul 	}
2033ce4946daSBill Paul 
2034f6bc9430SPyun YongHyeon 	if (enq > 0) {
2035f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
2036f6bc9430SPyun YongHyeon 		    sc->nge_cdata.nge_tx_ring_map,
2037f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2038ce4946daSBill Paul 		/* Transmit */
2039ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE);
2040ce4946daSBill Paul 
2041f6bc9430SPyun YongHyeon 		/* Set a timeout in case the chip goes out to lunch. */
2042f6bc9430SPyun YongHyeon 		sc->nge_watchdog_timer = 5;
2043f6bc9430SPyun YongHyeon 	}
2044ce4946daSBill Paul }
2045ce4946daSBill Paul 
2046eaabec55SAlfred Perlstein static void
2047284c81cbSPyun YongHyeon nge_init(void *xsc)
2048ce4946daSBill Paul {
2049ce4946daSBill Paul 	struct nge_softc *sc = xsc;
2050ad6c618bSBill Paul 
2051ad6c618bSBill Paul 	NGE_LOCK(sc);
2052ad6c618bSBill Paul 	nge_init_locked(sc);
2053ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2054ad6c618bSBill Paul }
2055ad6c618bSBill Paul 
2056ad6c618bSBill Paul static void
2057284c81cbSPyun YongHyeon nge_init_locked(struct nge_softc *sc)
2058ad6c618bSBill Paul {
2059fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->nge_ifp;
2060ce4946daSBill Paul 	struct mii_data *mii;
2061f6bc9430SPyun YongHyeon 	uint8_t *eaddr;
2062f6bc9430SPyun YongHyeon 	uint32_t reg;
2063ad6c618bSBill Paul 
2064ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
2065ce4946daSBill Paul 
2066f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2067ce4946daSBill Paul 		return;
2068ce4946daSBill Paul 
2069ce4946daSBill Paul 	/*
2070ce4946daSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2071ce4946daSBill Paul 	 */
2072ce4946daSBill Paul 	nge_stop(sc);
2073ce4946daSBill Paul 
2074f6bc9430SPyun YongHyeon 	/* Reset the adapter. */
2075f6bc9430SPyun YongHyeon 	nge_reset(sc);
2076ce4946daSBill Paul 
2077f6bc9430SPyun YongHyeon 	/* Disable Rx filter prior to programming Rx filter. */
2078f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, 0);
20798c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
2080f6bc9430SPyun YongHyeon 
2081f6bc9430SPyun YongHyeon 	mii = device_get_softc(sc->nge_miibus);
2082f6bc9430SPyun YongHyeon 
2083f6bc9430SPyun YongHyeon 	/* Set MAC address. */
2084f6bc9430SPyun YongHyeon 	eaddr = IF_LLADDR(sc->nge_ifp);
2085ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0);
2086f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[1] << 8) | eaddr[0]);
2087ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1);
2088f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[3] << 8) | eaddr[2]);
2089ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2);
2090f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[5] << 8) | eaddr[4]);
2091ce4946daSBill Paul 
2092ce4946daSBill Paul 	/* Init circular RX list. */
2093ce4946daSBill Paul 	if (nge_list_rx_init(sc) == ENOBUFS) {
20946b9f5c94SGleb Smirnoff 		device_printf(sc->nge_dev, "initialization failed: no "
2095646abee6SJohn Baldwin 			"memory for rx buffers\n");
2096ce4946daSBill Paul 		nge_stop(sc);
2097ce4946daSBill Paul 		return;
2098ce4946daSBill Paul 	}
2099ce4946daSBill Paul 
2100ce4946daSBill Paul 	/*
2101ce4946daSBill Paul 	 * Init tx descriptors.
2102ce4946daSBill Paul 	 */
2103ce4946daSBill Paul 	nge_list_tx_init(sc);
2104ce4946daSBill Paul 
2105ce4946daSBill Paul 	/*
2106ce4946daSBill Paul 	 * For the NatSemi chip, we have to explicitly enable the
2107ce4946daSBill Paul 	 * reception of ARP frames, as well as turn on the 'perfect
2108ce4946daSBill Paul 	 * match' filter where we store the station address, otherwise
2109ce4946daSBill Paul 	 * we won't receive unicasts meant for this host.
2110ce4946daSBill Paul 	 */
2111ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ARP);
2112ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_PERFECT);
2113ce4946daSBill Paul 
2114ce4946daSBill Paul 	/*
2115ce4946daSBill Paul 	 * Set the capture broadcast bit to capture broadcast frames.
2116ce4946daSBill Paul 	 */
2117ce4946daSBill Paul 	if (ifp->if_flags & IFF_BROADCAST) {
2118ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
2119ce4946daSBill Paul 	} else {
2120ce4946daSBill Paul 		NGE_CLRBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_BROAD);
2121ce4946daSBill Paul 	}
2122ce4946daSBill Paul 
2123f6bc9430SPyun YongHyeon 	/* Turn the receive filter on. */
2124ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_RXFILT_CTL, NGE_RXFILTCTL_ENABLE);
2125ce4946daSBill Paul 
2126f6bc9430SPyun YongHyeon 	/* Set Rx filter. */
2127f6bc9430SPyun YongHyeon 	nge_rxfilter(sc);
2128f6bc9430SPyun YongHyeon 
2129f6bc9430SPyun YongHyeon 	/* Disable PRIQ ctl. */
2130f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_PRIOQCTL, 0);
2131f6bc9430SPyun YongHyeon 
2132f6bc9430SPyun YongHyeon 	/*
2133f6bc9430SPyun YongHyeon 	 * Set pause frames paramters.
2134f6bc9430SPyun YongHyeon 	 *  Rx stat FIFO hi-threshold : 2 or more packets
2135f6bc9430SPyun YongHyeon 	 *  Rx stat FIFO lo-threshold : less than 2 packets
2136f6bc9430SPyun YongHyeon 	 *  Rx data FIFO hi-threshold : 2K or more bytes
2137f6bc9430SPyun YongHyeon 	 *  Rx data FIFO lo-threshold : less than 2K bytes
2138f6bc9430SPyun YongHyeon 	 *  pause time : (512ns * 0xffff) -> 33.55ms
2139f6bc9430SPyun YongHyeon 	 */
2140f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_PAUSECSR,
2141f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_PAUSE_ON_MCAST |
2142f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_PAUSE_ON_DA |
2143f6bc9430SPyun YongHyeon 	    ((1 << 24) & NGE_PAUSECSR_RX_STATFIFO_THR_HI) |
2144f6bc9430SPyun YongHyeon 	    ((1 << 22) & NGE_PAUSECSR_RX_STATFIFO_THR_LO) |
2145f6bc9430SPyun YongHyeon 	    ((1 << 20) & NGE_PAUSECSR_RX_DATAFIFO_THR_HI) |
2146f6bc9430SPyun YongHyeon 	    ((1 << 18) & NGE_PAUSECSR_RX_DATAFIFO_THR_LO) |
2147f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_CNT);
2148f6bc9430SPyun YongHyeon 
2149ce4946daSBill Paul 	/*
2150ce4946daSBill Paul 	 * Load the address of the RX and TX lists.
2151ce4946daSBill Paul 	 */
2152f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI,
2153f6bc9430SPyun YongHyeon 	    NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr));
2154f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO,
2155f6bc9430SPyun YongHyeon 	    NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr));
2156f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI,
2157f6bc9430SPyun YongHyeon 	    NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr));
2158f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO,
2159f6bc9430SPyun YongHyeon 	    NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr));
2160ce4946daSBill Paul 
2161f6bc9430SPyun YongHyeon 	/* Set RX configuration. */
2162ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG);
2163f6bc9430SPyun YongHyeon 
2164f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, 0);
2165ce4946daSBill Paul 	/*
2166ce4946daSBill Paul 	 * Enable hardware checksum validation for all IPv4
2167ce4946daSBill Paul 	 * packets, do not reject packets with bad checksums.
2168ce4946daSBill Paul 	 */
2169f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
2170f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB);
2171ce4946daSBill Paul 
2172ce4946daSBill Paul 	/*
21739d4fe4b2SBrooks Davis 	 * Tell the chip to detect and strip VLAN tag info from
21749d4fe4b2SBrooks Davis 	 * received frames. The tag will be provided in the extsts
21759d4fe4b2SBrooks Davis 	 * field in the RX descriptors.
2176ce4946daSBill Paul 	 */
2177f6bc9430SPyun YongHyeon 	NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_DETECT_ENB);
2178f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
2179f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_STRIP_ENB);
2180ce4946daSBill Paul 
2181f6bc9430SPyun YongHyeon 	/* Set TX configuration. */
2182ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG);
2183ce4946daSBill Paul 
2184ce4946daSBill Paul 	/*
2185ce4946daSBill Paul 	 * Enable TX IPv4 checksumming on a per-packet basis.
2186ce4946daSBill Paul 	 */
2187ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT);
2188ce4946daSBill Paul 
2189ce4946daSBill Paul 	/*
21909d4fe4b2SBrooks Davis 	 * Tell the chip to insert VLAN tags on a per-packet basis as
21919d4fe4b2SBrooks Davis 	 * dictated by the code in the frame encapsulation routine.
2192ce4946daSBill Paul 	 */
2193ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT);
2194ce4946daSBill Paul 
2195ce4946daSBill Paul 	/*
2196ce4946daSBill Paul 	 * Enable the delivery of PHY interrupts based on
219723d3a203SBill Paul 	 * link/speed/duplex status changes. Also enable the
219823d3a203SBill Paul 	 * extsts field in the DMA descriptors (needed for
219923d3a203SBill Paul 	 * TCP/IP checksum offload on transmit).
2200ce4946daSBill Paul 	 */
22012ce0498bSBill Paul 	NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD |
220223d3a203SBill Paul 	    NGE_CFG_PHYINTR_LNK | NGE_CFG_PHYINTR_DUP | NGE_CFG_EXTSTS_ENB);
2203ce4946daSBill Paul 
2204ce4946daSBill Paul 	/*
2205962315f6SBill Paul 	 * Configure interrupt holdoff (moderation). We can
2206962315f6SBill Paul 	 * have the chip delay interrupt delivery for a certain
2207962315f6SBill Paul 	 * period. Units are in 100us, and the max setting
2208962315f6SBill Paul 	 * is 25500us (0xFF x 100us). Default is a 100us holdoff.
2209962315f6SBill Paul 	 */
2210f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IHR, sc->nge_int_holdoff);
2211f6bc9430SPyun YongHyeon 
2212f6bc9430SPyun YongHyeon 	/*
2213f6bc9430SPyun YongHyeon 	 * Enable MAC statistics counters and clear.
2214f6bc9430SPyun YongHyeon 	 */
2215f6bc9430SPyun YongHyeon 	reg = CSR_READ_4(sc, NGE_MIBCTL);
2216f6bc9430SPyun YongHyeon 	reg &= ~NGE_MIBCTL_FREEZE_CNT;
2217f6bc9430SPyun YongHyeon 	reg |= NGE_MIBCTL_CLEAR_CNT;
2218f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_MIBCTL, reg);
2219962315f6SBill Paul 
2220962315f6SBill Paul 	/*
2221ce4946daSBill Paul 	 * Enable interrupts.
2222ce4946daSBill Paul 	 */
2223ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS);
2224196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
2225196c0df6SHidetoshi Shimokawa 	/*
2226196c0df6SHidetoshi Shimokawa 	 * ... only enable interrupts if we are not polling, make sure
2227196c0df6SHidetoshi Shimokawa 	 * they are off otherwise.
2228196c0df6SHidetoshi Shimokawa 	 */
2229f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
2230196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 0);
2231196c0df6SHidetoshi Shimokawa 	else
223240929967SGleb Smirnoff #endif
2233ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
2234ce4946daSBill Paul 
2235f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
2236f6bc9430SPyun YongHyeon 	mii_mediachg(mii);
2237ce4946daSBill Paul 
2238f6bc9430SPyun YongHyeon 	sc->nge_watchdog_timer = 0;
2239f6bc9430SPyun YongHyeon 	callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc);
2240ce4946daSBill Paul 
224113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
224213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2243ce4946daSBill Paul }
2244ce4946daSBill Paul 
2245ce4946daSBill Paul /*
2246ce4946daSBill Paul  * Set media options.
2247ce4946daSBill Paul  */
2248eaabec55SAlfred Perlstein static int
2249f6bc9430SPyun YongHyeon nge_mediachange(struct ifnet *ifp)
2250646abee6SJohn Baldwin {
2251646abee6SJohn Baldwin 	struct nge_softc *sc;
2252ce4946daSBill Paul 	struct mii_data	*mii;
2253f6bc9430SPyun YongHyeon 	struct mii_softc *miisc;
2254f6bc9430SPyun YongHyeon 	int error;
2255ce4946daSBill Paul 
2256ce4946daSBill Paul 	sc = ifp->if_softc;
2257f6bc9430SPyun YongHyeon 	NGE_LOCK(sc);
2258ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
2259646abee6SJohn Baldwin 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
22603fcb7a53SMarius Strobl 		PHY_RESET(miisc);
2261f6bc9430SPyun YongHyeon 	error = mii_mediachg(mii);
2262f6bc9430SPyun YongHyeon 	NGE_UNLOCK(sc);
2263f6bc9430SPyun YongHyeon 
2264f6bc9430SPyun YongHyeon 	return (error);
2265ce4946daSBill Paul }
2266ce4946daSBill Paul 
2267ce4946daSBill Paul /*
2268ce4946daSBill Paul  * Report current media status.
2269ce4946daSBill Paul  */
2270eaabec55SAlfred Perlstein static void
2271f6bc9430SPyun YongHyeon nge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2272ce4946daSBill Paul {
2273ce4946daSBill Paul 	struct nge_softc *sc;
2274ce4946daSBill Paul 	struct mii_data *mii;
2275ce4946daSBill Paul 
2276ce4946daSBill Paul 	sc = ifp->if_softc;
2277646abee6SJohn Baldwin 	NGE_LOCK(sc);
2278ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
2279ce4946daSBill Paul 	mii_pollstat(mii);
2280ce4946daSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2281ce4946daSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
228257c81d92SPyun YongHyeon 	NGE_UNLOCK(sc);
22831f548804SDoug Ambrisko }
2284ce4946daSBill Paul 
2285eaabec55SAlfred Perlstein static int
2286284c81cbSPyun YongHyeon nge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2287ce4946daSBill Paul {
2288ce4946daSBill Paul 	struct nge_softc *sc = ifp->if_softc;
2289ce4946daSBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
2290ce4946daSBill Paul 	struct mii_data *mii;
2291f6bc9430SPyun YongHyeon 	int error = 0, mask;
2292ce4946daSBill Paul 
2293ce4946daSBill Paul 	switch (command) {
2294ce4946daSBill Paul 	case SIOCSIFMTU:
2295f6bc9430SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > NGE_JUMBO_MTU)
2296ce4946daSBill Paul 			error = EINVAL;
2297cb2f755cSBill Paul 		else {
2298646abee6SJohn Baldwin 			NGE_LOCK(sc);
2299ce4946daSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2300cb2f755cSBill Paul 			/*
2301cb2f755cSBill Paul 			 * Workaround: if the MTU is larger than
2302cb2f755cSBill Paul 			 * 8152 (TX FIFO size minus 64 minus 18), turn off
2303cb2f755cSBill Paul 			 * TX checksum offloading.
2304cb2f755cSBill Paul 			 */
2305a5820ecbSYaroslav Tykhiy 			if (ifr->ifr_mtu >= 8152) {
2306a5820ecbSYaroslav Tykhiy 				ifp->if_capenable &= ~IFCAP_TXCSUM;
2307f6bc9430SPyun YongHyeon 				ifp->if_hwassist &= ~NGE_CSUM_FEATURES;
2308a5820ecbSYaroslav Tykhiy 			} else {
2309a5820ecbSYaroslav Tykhiy 				ifp->if_capenable |= IFCAP_TXCSUM;
2310f6bc9430SPyun YongHyeon 				ifp->if_hwassist |= NGE_CSUM_FEATURES;
2311cb2f755cSBill Paul 			}
2312646abee6SJohn Baldwin 			NGE_UNLOCK(sc);
2313f6bc9430SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
2314a5820ecbSYaroslav Tykhiy 		}
2315ce4946daSBill Paul 		break;
2316ce4946daSBill Paul 	case SIOCSIFFLAGS:
2317ad6c618bSBill Paul 		NGE_LOCK(sc);
2318f6bc9430SPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2319f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2320f6bc9430SPyun YongHyeon 				if ((ifp->if_flags ^ sc->nge_if_flags) &
2321f6bc9430SPyun YongHyeon 				    (IFF_PROMISC | IFF_ALLMULTI))
2322f6bc9430SPyun YongHyeon 					nge_rxfilter(sc);
2323ce4946daSBill Paul 			} else {
2324f6bc9430SPyun YongHyeon 				if ((sc->nge_flags & NGE_FLAG_DETACH) == 0)
2325ad6c618bSBill Paul 					nge_init_locked(sc);
2326ce4946daSBill Paul 			}
2327ce4946daSBill Paul 		} else {
2328f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2329ce4946daSBill Paul 				nge_stop(sc);
2330ce4946daSBill Paul 		}
2331ce4946daSBill Paul 		sc->nge_if_flags = ifp->if_flags;
2332ad6c618bSBill Paul 		NGE_UNLOCK(sc);
2333ce4946daSBill Paul 		error = 0;
2334ce4946daSBill Paul 		break;
2335ce4946daSBill Paul 	case SIOCADDMULTI:
2336ce4946daSBill Paul 	case SIOCDELMULTI:
2337ad6c618bSBill Paul 		NGE_LOCK(sc);
2338f6bc9430SPyun YongHyeon 		nge_rxfilter(sc);
2339ad6c618bSBill Paul 		NGE_UNLOCK(sc);
2340ce4946daSBill Paul 		error = 0;
2341ce4946daSBill Paul 		break;
2342ce4946daSBill Paul 	case SIOCGIFMEDIA:
2343ce4946daSBill Paul 	case SIOCSIFMEDIA:
2344ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
2345f6bc9430SPyun YongHyeon 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2346ce4946daSBill Paul 		break;
234737f5f239SRuslan Ermilov 	case SIOCSIFCAP:
234840929967SGleb Smirnoff 		NGE_LOCK(sc);
2349f6bc9430SPyun YongHyeon 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2350f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
2351f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_POLLING) != 0 &&
2352f6bc9430SPyun YongHyeon 		    (IFCAP_POLLING & ifp->if_capabilities) != 0) {
2353f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_POLLING;
2354f6bc9430SPyun YongHyeon 			if ((IFCAP_POLLING & ifp->if_capenable) != 0) {
2355f6bc9430SPyun YongHyeon 				error = ether_poll_register(nge_poll, ifp);
2356f6bc9430SPyun YongHyeon 				if (error != 0) {
235740929967SGleb Smirnoff 					NGE_UNLOCK(sc);
2358f6bc9430SPyun YongHyeon 					break;
235940929967SGleb Smirnoff 				}
2360f6bc9430SPyun YongHyeon 				/* Disable interrupts. */
2361f6bc9430SPyun YongHyeon 				CSR_WRITE_4(sc, NGE_IER, 0);
2362f6bc9430SPyun YongHyeon 			} else {
236340929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
236440929967SGleb Smirnoff 				/* Enable interrupts. */
236540929967SGleb Smirnoff 				CSR_WRITE_4(sc, NGE_IER, 1);
2366f6bc9430SPyun YongHyeon 			}
236740929967SGleb Smirnoff 		}
236840929967SGleb Smirnoff #endif /* DEVICE_POLLING */
2369f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
2370f6bc9430SPyun YongHyeon 		    (IFCAP_TXCSUM & ifp->if_capabilities) != 0) {
2371f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
2372f6bc9430SPyun YongHyeon 			if ((IFCAP_TXCSUM & ifp->if_capenable) != 0)
2373f6bc9430SPyun YongHyeon 				ifp->if_hwassist |= NGE_CSUM_FEATURES;
2374f6bc9430SPyun YongHyeon 			else
2375f6bc9430SPyun YongHyeon 				ifp->if_hwassist &= ~NGE_CSUM_FEATURES;
2376f6bc9430SPyun YongHyeon 		}
2377f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2378f6bc9430SPyun YongHyeon 		    (IFCAP_RXCSUM & ifp->if_capabilities) != 0)
2379f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2380f6bc9430SPyun YongHyeon 
2381f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
2382f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
2383f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
2384f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
2385f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
2386f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
2387f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
2388f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2389f6bc9430SPyun YongHyeon 		}
2390f6bc9430SPyun YongHyeon 
2391f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
2392f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
2393f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2394f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2395f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2396f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2397f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2398f6bc9430SPyun YongHyeon 				if ((ifp->if_capenable &
2399f6bc9430SPyun YongHyeon 				    IFCAP_VLAN_HWTAGGING) != 0)
2400f6bc9430SPyun YongHyeon 					NGE_SETBIT(sc,
2401f6bc9430SPyun YongHyeon 					    NGE_VLAN_IP_RXCTL,
2402f6bc9430SPyun YongHyeon 					    NGE_VIPRXCTL_TAG_STRIP_ENB);
2403f6bc9430SPyun YongHyeon 				else
2404f6bc9430SPyun YongHyeon 					NGE_CLRBIT(sc,
2405f6bc9430SPyun YongHyeon 					    NGE_VLAN_IP_RXCTL,
2406f6bc9430SPyun YongHyeon 					    NGE_VIPRXCTL_TAG_STRIP_ENB);
2407f6bc9430SPyun YongHyeon 			}
2408f6bc9430SPyun YongHyeon 		}
2409f6bc9430SPyun YongHyeon 		/*
2410f6bc9430SPyun YongHyeon 		 * Both VLAN hardware tagging and checksum offload is
2411f6bc9430SPyun YongHyeon 		 * required to do checksum offload on VLAN interface.
2412f6bc9430SPyun YongHyeon 		 */
2413f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_TXCSUM) == 0)
2414f6bc9430SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM;
2415f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
2416f6bc9430SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM;
2417f6bc9430SPyun YongHyeon 		NGE_UNLOCK(sc);
2418f6bc9430SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
241937f5f239SRuslan Ermilov 		break;
2420ce4946daSBill Paul 	default:
2421673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2422ce4946daSBill Paul 		break;
2423ce4946daSBill Paul 	}
2424ce4946daSBill Paul 
2425ce4946daSBill Paul 	return (error);
2426ce4946daSBill Paul }
2427ce4946daSBill Paul 
2428eaabec55SAlfred Perlstein static void
2429f6bc9430SPyun YongHyeon nge_watchdog(struct nge_softc *sc)
2430ce4946daSBill Paul {
2431f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2432ce4946daSBill Paul 
2433f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2434ce4946daSBill Paul 
2435f6bc9430SPyun YongHyeon 	if (sc->nge_watchdog_timer == 0 || --sc->nge_watchdog_timer)
2436f6bc9430SPyun YongHyeon 		return;
2437f6bc9430SPyun YongHyeon 
2438f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
2439ce4946daSBill Paul 	ifp->if_oerrors++;
24406b9f5c94SGleb Smirnoff 	if_printf(ifp, "watchdog timeout\n");
2441ce4946daSBill Paul 
244213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2443ad6c618bSBill Paul 	nge_init_locked(sc);
2444ce4946daSBill Paul 
2445f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2446ad6c618bSBill Paul 		nge_start_locked(ifp);
2447f6bc9430SPyun YongHyeon }
2448ad6c618bSBill Paul 
2449f6bc9430SPyun YongHyeon static int
2450f6bc9430SPyun YongHyeon nge_stop_mac(struct nge_softc *sc)
2451f6bc9430SPyun YongHyeon {
2452f6bc9430SPyun YongHyeon 	uint32_t reg;
2453f6bc9430SPyun YongHyeon 	int i;
2454f6bc9430SPyun YongHyeon 
2455f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2456f6bc9430SPyun YongHyeon 
2457f6bc9430SPyun YongHyeon 	reg = CSR_READ_4(sc, NGE_CSR);
2458f6bc9430SPyun YongHyeon 	if ((reg & (NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE)) != 0) {
2459f6bc9430SPyun YongHyeon 		reg &= ~(NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE);
2460f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_TX_DISABLE | NGE_CSR_RX_DISABLE;
2461f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
2462f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
2463f6bc9430SPyun YongHyeon 			DELAY(1);
2464f6bc9430SPyun YongHyeon 			if ((CSR_READ_4(sc, NGE_CSR) &
2465f6bc9430SPyun YongHyeon 			    (NGE_CSR_RX_ENABLE | NGE_CSR_TX_ENABLE)) == 0)
2466f6bc9430SPyun YongHyeon 				break;
2467f6bc9430SPyun YongHyeon 		}
2468f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
2469f6bc9430SPyun YongHyeon 			return (ETIMEDOUT);
2470f6bc9430SPyun YongHyeon 	}
2471f6bc9430SPyun YongHyeon 
2472f6bc9430SPyun YongHyeon 	return (0);
2473ce4946daSBill Paul }
2474ce4946daSBill Paul 
2475ce4946daSBill Paul /*
2476ce4946daSBill Paul  * Stop the adapter and free any mbufs allocated to the
2477ce4946daSBill Paul  * RX and TX lists.
2478ce4946daSBill Paul  */
2479eaabec55SAlfred Perlstein static void
2480284c81cbSPyun YongHyeon nge_stop(struct nge_softc *sc)
2481ce4946daSBill Paul {
2482f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
2483f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
24842cf2d799SPyun YongHyeon 	int i;
2485ce4946daSBill Paul 	struct ifnet *ifp;
2486ce4946daSBill Paul 
2487ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
2488fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
2489ce4946daSBill Paul 
249013f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2491f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
2492f6bc9430SPyun YongHyeon 	callout_stop(&sc->nge_stat_ch);
2493f6bc9430SPyun YongHyeon 	sc->nge_watchdog_timer = 0;
2494f6bc9430SPyun YongHyeon 
2495f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IER, 0);
2496f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IMR, 0);
2497f6bc9430SPyun YongHyeon 	if (nge_stop_mac(sc) == ETIMEDOUT)
2498f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
2499f6bc9430SPyun YongHyeon 		   "%s: unable to stop Tx/Rx MAC\n", __func__);
2500f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI, 0);
2501f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO, 0);
2502f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0);
2503f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0);
2504f6bc9430SPyun YongHyeon 	nge_stats_update(sc);
2505f6bc9430SPyun YongHyeon 	if (sc->nge_head != NULL) {
2506f6bc9430SPyun YongHyeon 		m_freem(sc->nge_head);
2507f6bc9430SPyun YongHyeon 		sc->nge_head = sc->nge_tail = NULL;
2508f6bc9430SPyun YongHyeon 	}
2509f6bc9430SPyun YongHyeon 
2510f6bc9430SPyun YongHyeon 	/*
2511f6bc9430SPyun YongHyeon 	 * Free RX and TX mbufs still in the queues.
2512f6bc9430SPyun YongHyeon 	 */
2513f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
2514f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[i];
2515f6bc9430SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2516f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_rx_tag,
2517f6bc9430SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2518f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_rx_tag,
2519f6bc9430SPyun YongHyeon 			    rxd->rx_dmamap);
2520f6bc9430SPyun YongHyeon 			m_freem(rxd->rx_m);
2521f6bc9430SPyun YongHyeon 			rxd->rx_m = NULL;
2522f6bc9430SPyun YongHyeon 		}
2523f6bc9430SPyun YongHyeon 	}
2524f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
2525f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
2526f6bc9430SPyun YongHyeon 		if (txd->tx_m != NULL) {
2527f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_tx_tag,
2528f6bc9430SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2529f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_tag,
2530f6bc9430SPyun YongHyeon 			    txd->tx_dmamap);
2531f6bc9430SPyun YongHyeon 			m_freem(txd->tx_m);
2532f6bc9430SPyun YongHyeon 			txd->tx_m = NULL;
2533f6bc9430SPyun YongHyeon 		}
2534f6bc9430SPyun YongHyeon 	}
2535f6bc9430SPyun YongHyeon }
2536f6bc9430SPyun YongHyeon 
2537f6bc9430SPyun YongHyeon /*
2538f6bc9430SPyun YongHyeon  * Before setting WOL bits, caller should have stopped Receiver.
2539f6bc9430SPyun YongHyeon  */
2540f6bc9430SPyun YongHyeon static void
2541f6bc9430SPyun YongHyeon nge_wol(struct nge_softc *sc)
2542f6bc9430SPyun YongHyeon {
2543f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2544f6bc9430SPyun YongHyeon 	uint32_t reg;
2545f6bc9430SPyun YongHyeon 	uint16_t pmstat;
2546f6bc9430SPyun YongHyeon 	int pmc;
2547f6bc9430SPyun YongHyeon 
2548f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2549f6bc9430SPyun YongHyeon 
25503b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &pmc) != 0)
2551f6bc9430SPyun YongHyeon 		return;
2552f6bc9430SPyun YongHyeon 
2553f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
2554f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) == 0) {
2555f6bc9430SPyun YongHyeon 		/* Disable WOL & disconnect CLKRUN to save power. */
2556f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_WOLCSR, 0);
2557f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CLKRUN, 0);
2558f6bc9430SPyun YongHyeon 	} else {
2559f6bc9430SPyun YongHyeon 		if (nge_stop_mac(sc) == ETIMEDOUT)
2560f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
2561f6bc9430SPyun YongHyeon 			    "%s: unable to stop Tx/Rx MAC\n", __func__);
2562f6bc9430SPyun YongHyeon 		/*
2563f6bc9430SPyun YongHyeon 		 * Make sure wake frames will be buffered in the Rx FIFO.
2564f6bc9430SPyun YongHyeon 		 * (i.e. Silent Rx mode.)
2565f6bc9430SPyun YongHyeon 		 */
2566f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0);
25678c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_RX_LISTPTR_HI, BUS_SPACE_BARRIER_WRITE);
2568f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0);
25698c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_RX_LISTPTR_LO, BUS_SPACE_BARRIER_WRITE);
2570f6bc9430SPyun YongHyeon 		/* Enable Rx again. */
2571f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
25728c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_CSR, BUS_SPACE_BARRIER_WRITE);
2573f6bc9430SPyun YongHyeon 
2574f6bc9430SPyun YongHyeon 		/* Configure WOL events. */
2575f6bc9430SPyun YongHyeon 		reg = 0;
2576f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
2577f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_UNICAST;
2578f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2579f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_MULTICAST;
2580f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2581f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_MAGICPKT;
2582f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_WOLCSR, reg);
2583f6bc9430SPyun YongHyeon 
2584f6bc9430SPyun YongHyeon 		/* Activate CLKRUN. */
2585f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CLKRUN);
2586f6bc9430SPyun YongHyeon 		reg |= NGE_CLKRUN_PMEENB | NGE_CLNRUN_CLKRUN_ENB;
2587f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CLKRUN, reg);
2588f6bc9430SPyun YongHyeon 	}
2589f6bc9430SPyun YongHyeon 
2590f6bc9430SPyun YongHyeon 	/* Request PME. */
2591f6bc9430SPyun YongHyeon 	pmstat = pci_read_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, 2);
2592f6bc9430SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2593f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
2594f6bc9430SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2595f6bc9430SPyun YongHyeon 	pci_write_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
2596ce4946daSBill Paul }
2597ce4946daSBill Paul 
2598ce4946daSBill Paul /*
2599ce4946daSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2600ce4946daSBill Paul  * get confused by errant DMAs when rebooting.
2601ce4946daSBill Paul  */
26026a087a87SPyun YongHyeon static int
2603284c81cbSPyun YongHyeon nge_shutdown(device_t dev)
2604ce4946daSBill Paul {
2605f6bc9430SPyun YongHyeon 
2606f6bc9430SPyun YongHyeon 	return (nge_suspend(dev));
2607f6bc9430SPyun YongHyeon }
2608f6bc9430SPyun YongHyeon 
2609f6bc9430SPyun YongHyeon static int
2610f6bc9430SPyun YongHyeon nge_suspend(device_t dev)
2611f6bc9430SPyun YongHyeon {
2612ce4946daSBill Paul 	struct nge_softc *sc;
2613ce4946daSBill Paul 
2614ce4946daSBill Paul 	sc = device_get_softc(dev);
2615ce4946daSBill Paul 
2616ad6c618bSBill Paul 	NGE_LOCK(sc);
2617ce4946daSBill Paul 	nge_stop(sc);
2618f6bc9430SPyun YongHyeon 	nge_wol(sc);
2619f6bc9430SPyun YongHyeon 	sc->nge_flags |= NGE_FLAG_SUSPENDED;
2620ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2621ce4946daSBill Paul 
26226a087a87SPyun YongHyeon 	return (0);
2623ce4946daSBill Paul }
2624f6bc9430SPyun YongHyeon 
2625f6bc9430SPyun YongHyeon static int
2626f6bc9430SPyun YongHyeon nge_resume(device_t dev)
2627f6bc9430SPyun YongHyeon {
2628f6bc9430SPyun YongHyeon 	struct nge_softc *sc;
2629f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2630f6bc9430SPyun YongHyeon 	uint16_t pmstat;
2631f6bc9430SPyun YongHyeon 	int pmc;
2632f6bc9430SPyun YongHyeon 
2633f6bc9430SPyun YongHyeon 	sc = device_get_softc(dev);
2634f6bc9430SPyun YongHyeon 
2635f6bc9430SPyun YongHyeon 	NGE_LOCK(sc);
2636f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
26373b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &pmc) == 0) {
2638f6bc9430SPyun YongHyeon 		/* Disable PME and clear PME status. */
2639f6bc9430SPyun YongHyeon 		pmstat = pci_read_config(sc->nge_dev,
2640f6bc9430SPyun YongHyeon 		    pmc + PCIR_POWER_STATUS, 2);
2641f6bc9430SPyun YongHyeon 		if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
2642f6bc9430SPyun YongHyeon 			pmstat &= ~PCIM_PSTAT_PMEENABLE;
2643f6bc9430SPyun YongHyeon 			pci_write_config(sc->nge_dev,
2644f6bc9430SPyun YongHyeon 			    pmc + PCIR_POWER_STATUS, pmstat, 2);
2645f6bc9430SPyun YongHyeon 		}
2646f6bc9430SPyun YongHyeon 	}
2647f6bc9430SPyun YongHyeon 	if (ifp->if_flags & IFF_UP) {
2648f6bc9430SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2649f6bc9430SPyun YongHyeon 		nge_init_locked(sc);
2650f6bc9430SPyun YongHyeon 	}
2651f6bc9430SPyun YongHyeon 
2652f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_SUSPENDED;
2653f6bc9430SPyun YongHyeon 	NGE_UNLOCK(sc);
2654f6bc9430SPyun YongHyeon 
2655f6bc9430SPyun YongHyeon 	return (0);
2656f6bc9430SPyun YongHyeon }
2657f6bc9430SPyun YongHyeon 
2658f6bc9430SPyun YongHyeon #define	NGE_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2659f6bc9430SPyun YongHyeon 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2660f6bc9430SPyun YongHyeon 
2661f6bc9430SPyun YongHyeon static void
2662f6bc9430SPyun YongHyeon nge_sysctl_node(struct nge_softc *sc)
2663f6bc9430SPyun YongHyeon {
2664f6bc9430SPyun YongHyeon 	struct sysctl_ctx_list *ctx;
2665f6bc9430SPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
2666f6bc9430SPyun YongHyeon 	struct sysctl_oid *tree;
2667f6bc9430SPyun YongHyeon 	struct nge_stats *stats;
2668f6bc9430SPyun YongHyeon 	int error;
2669f6bc9430SPyun YongHyeon 
2670f6bc9430SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->nge_dev);
2671f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->nge_dev));
2672f6bc9430SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_holdoff",
2673f6bc9430SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, &sc->nge_int_holdoff, 0,
2674f6bc9430SPyun YongHyeon 	    sysctl_hw_nge_int_holdoff, "I", "NGE interrupt moderation");
2675f6bc9430SPyun YongHyeon 	/* Pull in device tunables. */
2676f6bc9430SPyun YongHyeon 	sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT;
2677f6bc9430SPyun YongHyeon 	error = resource_int_value(device_get_name(sc->nge_dev),
2678f6bc9430SPyun YongHyeon 	    device_get_unit(sc->nge_dev), "int_holdoff", &sc->nge_int_holdoff);
2679f6bc9430SPyun YongHyeon 	if (error == 0) {
2680f6bc9430SPyun YongHyeon 		if (sc->nge_int_holdoff < NGE_INT_HOLDOFF_MIN ||
2681f6bc9430SPyun YongHyeon 		    sc->nge_int_holdoff > NGE_INT_HOLDOFF_MAX ) {
2682f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
2683f6bc9430SPyun YongHyeon 			    "int_holdoff value out of range; "
2684f6bc9430SPyun YongHyeon 			    "using default: %d(%d us)\n",
2685f6bc9430SPyun YongHyeon 			    NGE_INT_HOLDOFF_DEFAULT,
2686f6bc9430SPyun YongHyeon 			    NGE_INT_HOLDOFF_DEFAULT * 100);
2687f6bc9430SPyun YongHyeon 			sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT;
2688f6bc9430SPyun YongHyeon 		}
2689f6bc9430SPyun YongHyeon 	}
2690f6bc9430SPyun YongHyeon 
2691f6bc9430SPyun YongHyeon 	stats = &sc->nge_stats;
2692f6bc9430SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD,
2693f6bc9430SPyun YongHyeon 	    NULL, "NGE statistics");
2694f6bc9430SPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
2695f6bc9430SPyun YongHyeon 
2696f6bc9430SPyun YongHyeon 	/* Rx statistics. */
2697f6bc9430SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx", CTLFLAG_RD,
2698f6bc9430SPyun YongHyeon 	    NULL, "Rx MAC statistics");
2699f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
2700f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_errs",
2701f6bc9430SPyun YongHyeon 	    &stats->rx_pkts_errs,
2702f6bc9430SPyun YongHyeon 	    "Packet errors including both wire errors and FIFO overruns");
2703f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "crc_errs",
2704f6bc9430SPyun YongHyeon 	    &stats->rx_crc_errs, "CRC errors");
2705f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
2706f6bc9430SPyun YongHyeon 	    &stats->rx_fifo_oflows, "FIFO overflows");
2707f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
2708f6bc9430SPyun YongHyeon 	    &stats->rx_align_errs, "Frame alignment errors");
2709f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "sym_errs",
2710f6bc9430SPyun YongHyeon 	    &stats->rx_sym_errs, "One or more symbol errors");
2711f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_jumbos",
2712f6bc9430SPyun YongHyeon 	    &stats->rx_pkts_jumbos,
2713f6bc9430SPyun YongHyeon 	    "Packets received with length greater than 1518 bytes");
2714f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
2715f6bc9430SPyun YongHyeon 	    &stats->rx_len_errs, "In Range Length errors");
2716f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "unctl_frames",
2717f6bc9430SPyun YongHyeon 	    &stats->rx_unctl_frames, "Control frames with unsupported opcode");
2718f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pause",
2719f6bc9430SPyun YongHyeon 	    &stats->rx_pause, "Pause frames");
2720f6bc9430SPyun YongHyeon 
2721f6bc9430SPyun YongHyeon 	/* Tx statistics. */
2722f6bc9430SPyun YongHyeon 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx", CTLFLAG_RD,
2723f6bc9430SPyun YongHyeon 	    NULL, "Tx MAC statistics");
2724f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
2725f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pause",
2726f6bc9430SPyun YongHyeon 	    &stats->tx_pause, "Pause frames");
2727f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "seq_errs",
2728f6bc9430SPyun YongHyeon 	    &stats->tx_seq_errs,
2729f6bc9430SPyun YongHyeon 	    "Loss of collision heartbeat during transmission");
2730f6bc9430SPyun YongHyeon }
2731f6bc9430SPyun YongHyeon 
2732f6bc9430SPyun YongHyeon #undef NGE_SYSCTL_STAT_ADD32
2733f6bc9430SPyun YongHyeon 
2734f6bc9430SPyun YongHyeon static int
2735f6bc9430SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2736f6bc9430SPyun YongHyeon {
2737f6bc9430SPyun YongHyeon 	int error, value;
2738f6bc9430SPyun YongHyeon 
2739f6bc9430SPyun YongHyeon 	if (arg1 == NULL)
2740f6bc9430SPyun YongHyeon 		return (EINVAL);
2741f6bc9430SPyun YongHyeon 	value = *(int *)arg1;
2742f6bc9430SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
2743f6bc9430SPyun YongHyeon 	if (error != 0 || req->newptr == NULL)
2744f6bc9430SPyun YongHyeon 		return (error);
2745f6bc9430SPyun YongHyeon 	if (value < low || value > high)
2746f6bc9430SPyun YongHyeon 		return (EINVAL);
2747f6bc9430SPyun YongHyeon 	*(int *)arg1 = value;
2748f6bc9430SPyun YongHyeon 
2749f6bc9430SPyun YongHyeon 	return (0);
2750f6bc9430SPyun YongHyeon }
2751f6bc9430SPyun YongHyeon 
2752f6bc9430SPyun YongHyeon static int
2753f6bc9430SPyun YongHyeon sysctl_hw_nge_int_holdoff(SYSCTL_HANDLER_ARGS)
2754f6bc9430SPyun YongHyeon {
2755f6bc9430SPyun YongHyeon 
2756f6bc9430SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, NGE_INT_HOLDOFF_MIN,
2757f6bc9430SPyun YongHyeon 	    NGE_INT_HOLDOFF_MAX));
2758f6bc9430SPyun YongHyeon }
2759