xref: /freebsd/sys/dev/nge/if_nge.c (revision 7029da5c36f2d3cf6bb6c81bf551229f416399e8)
1098ca2bdSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
4ce4946daSBill Paul  * Copyright (c) 2001 Wind River Systems
5ce4946daSBill Paul  * Copyright (c) 1997, 1998, 1999, 2000, 2001
6ce4946daSBill Paul  *	Bill Paul <wpaul@bsdi.com>.  All rights reserved.
7ce4946daSBill Paul  *
8ce4946daSBill Paul  * Redistribution and use in source and binary forms, with or without
9ce4946daSBill Paul  * modification, are permitted provided that the following conditions
10ce4946daSBill Paul  * are met:
11ce4946daSBill Paul  * 1. Redistributions of source code must retain the above copyright
12ce4946daSBill Paul  *    notice, this list of conditions and the following disclaimer.
13ce4946daSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
14ce4946daSBill Paul  *    notice, this list of conditions and the following disclaimer in the
15ce4946daSBill Paul  *    documentation and/or other materials provided with the distribution.
16ce4946daSBill Paul  * 3. All advertising materials mentioning features or use of this software
17ce4946daSBill Paul  *    must display the following acknowledgement:
18ce4946daSBill Paul  *	This product includes software developed by Bill Paul.
19ce4946daSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
20ce4946daSBill Paul  *    may be used to endorse or promote products derived from this software
21ce4946daSBill Paul  *    without specific prior written permission.
22ce4946daSBill Paul  *
23ce4946daSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
24ce4946daSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ce4946daSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ce4946daSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
27ce4946daSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ce4946daSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ce4946daSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30ce4946daSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31ce4946daSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32ce4946daSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
33ce4946daSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
34ce4946daSBill Paul  */
35ce4946daSBill Paul 
36aad970f1SDavid E. O'Brien #include <sys/cdefs.h>
37aad970f1SDavid E. O'Brien __FBSDID("$FreeBSD$");
38aad970f1SDavid E. O'Brien 
39ce4946daSBill Paul /*
40ce4946daSBill Paul  * National Semiconductor DP83820/DP83821 gigabit ethernet driver
41ce4946daSBill Paul  * for FreeBSD. Datasheets are available from:
42ce4946daSBill Paul  *
43ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83820.pdf
44ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83821.pdf
45ce4946daSBill Paul  *
46ce4946daSBill Paul  * These chips are used on several low cost gigabit ethernet NICs
47ce4946daSBill Paul  * sold by D-Link, Addtron, SMC and Asante. Both parts are
48ce4946daSBill Paul  * virtually the same, except the 83820 is a 64-bit/32-bit part,
49ce4946daSBill Paul  * while the 83821 is 32-bit only.
50ce4946daSBill Paul  *
51ce4946daSBill Paul  * Many cards also use National gigE transceivers, such as the
52ce4946daSBill Paul  * DP83891, DP83861 and DP83862 gigPHYTER parts. The DP83861 datasheet
53ce4946daSBill Paul  * contains a full register description that applies to all of these
54ce4946daSBill Paul  * components:
55ce4946daSBill Paul  *
56ce4946daSBill Paul  * http://www.national.com/ds/DP/DP83861.pdf
57ce4946daSBill Paul  *
58ce4946daSBill Paul  * Written by Bill Paul <wpaul@bsdi.com>
59ce4946daSBill Paul  * BSDi Open Source Solutions
60ce4946daSBill Paul  */
61ce4946daSBill Paul 
62ce4946daSBill Paul /*
63ce4946daSBill Paul  * The NatSemi DP83820 and 83821 controllers are enhanced versions
64ce4946daSBill Paul  * of the NatSemi MacPHYTER 10/100 devices. They support 10, 100
65ce4946daSBill Paul  * and 1000Mbps speeds with 1000baseX (ten bit interface), MII and GMII
66ce4946daSBill Paul  * ports. Other features include 8K TX FIFO and 32K RX FIFO, TCP/IP
67ce4946daSBill Paul  * hardware checksum offload (IPv4 only), VLAN tagging and filtering,
68ce4946daSBill Paul  * priority TX and RX queues, a 2048 bit multicast hash filter, 4 RX pattern
69ce4946daSBill Paul  * matching buffers, one perfect address filter buffer and interrupt
70ce4946daSBill Paul  * moderation. The 83820 supports both 64-bit and 32-bit addressing
71ce4946daSBill Paul  * and data transfers: the 64-bit support can be toggled on or off
72ce4946daSBill Paul  * via software. This affects the size of certain fields in the DMA
73ce4946daSBill Paul  * descriptors.
74ce4946daSBill Paul  *
75cb2f755cSBill Paul  * There are two bugs/misfeatures in the 83820/83821 that I have
76cb2f755cSBill Paul  * discovered so far:
77cb2f755cSBill Paul  *
78cb2f755cSBill Paul  * - Receive buffers must be aligned on 64-bit boundaries, which means
79cb2f755cSBill Paul  *   you must resort to copying data in order to fix up the payload
80cb2f755cSBill Paul  *   alignment.
81cb2f755cSBill Paul  *
82cb2f755cSBill Paul  * - In order to transmit jumbo frames larger than 8170 bytes, you have
83cb2f755cSBill Paul  *   to turn off transmit checksum offloading, because the chip can't
84cb2f755cSBill Paul  *   compute the checksum on an outgoing frame unless it fits entirely
85cb2f755cSBill Paul  *   within the TX FIFO, which is only 8192 bytes in size. If you have
86cb2f755cSBill Paul  *   TX checksum offload enabled and you transmit attempt to transmit a
87cb2f755cSBill Paul  *   frame larger than 8170 bytes, the transmitter will wedge.
88cb2f755cSBill Paul  *
89cb2f755cSBill Paul  * To work around the latter problem, TX checksum offload is disabled
90cb2f755cSBill Paul  * if the user selects an MTU larger than 8152 (8170 - 18).
91ce4946daSBill Paul  */
92ce4946daSBill Paul 
93f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
94f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
95f0796cd2SGleb Smirnoff #endif
96f0796cd2SGleb Smirnoff 
97ce4946daSBill Paul #include <sys/param.h>
98ce4946daSBill Paul #include <sys/systm.h>
99f6bc9430SPyun YongHyeon #include <sys/bus.h>
100f6bc9430SPyun YongHyeon #include <sys/endian.h>
101ce4946daSBill Paul #include <sys/kernel.h>
102f6bc9430SPyun YongHyeon #include <sys/lock.h>
103f6bc9430SPyun YongHyeon #include <sys/malloc.h>
104f6bc9430SPyun YongHyeon #include <sys/mbuf.h>
105f6bc9430SPyun YongHyeon #include <sys/module.h>
106f6bc9430SPyun YongHyeon #include <sys/mutex.h>
107f6bc9430SPyun YongHyeon #include <sys/rman.h>
108ce4946daSBill Paul #include <sys/socket.h>
109f6bc9430SPyun YongHyeon #include <sys/sockio.h>
110f6bc9430SPyun YongHyeon #include <sys/sysctl.h>
111ce4946daSBill Paul 
112f6bc9430SPyun YongHyeon #include <net/bpf.h>
113ce4946daSBill Paul #include <net/if.h>
11476039bc8SGleb Smirnoff #include <net/if_var.h>
115ce4946daSBill Paul #include <net/if_arp.h>
116ce4946daSBill Paul #include <net/ethernet.h>
117ce4946daSBill Paul #include <net/if_dl.h>
118ce4946daSBill Paul #include <net/if_media.h>
119ce4946daSBill Paul #include <net/if_types.h>
120ce4946daSBill Paul #include <net/if_vlan_var.h>
121ce4946daSBill Paul 
122ce4946daSBill Paul #include <dev/mii/mii.h>
1238c1093fcSMarius Strobl #include <dev/mii/mii_bitbang.h>
124ce4946daSBill Paul #include <dev/mii/miivar.h>
125ce4946daSBill Paul 
12638d8c994SWarner Losh #include <dev/pci/pcireg.h>
12738d8c994SWarner Losh #include <dev/pci/pcivar.h>
128ce4946daSBill Paul 
129f6bc9430SPyun YongHyeon #include <machine/bus.h>
130ce4946daSBill Paul 
1315da751e4SBill Paul #include <dev/nge/if_ngereg.h>
132ce4946daSBill Paul 
133f6bc9430SPyun YongHyeon /* "device miibus" required.  See GENERIC if you get errors here. */
134f6bc9430SPyun YongHyeon #include "miibus_if.h"
135f6bc9430SPyun YongHyeon 
136f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, pci, 1, 1, 1);
137f246e4a1SMatthew N. Dodd MODULE_DEPEND(nge, ether, 1, 1, 1);
138ce4946daSBill Paul MODULE_DEPEND(nge, miibus, 1, 1, 1);
139ce4946daSBill Paul 
140ce4946daSBill Paul #define NGE_CSUM_FEATURES	(CSUM_IP | CSUM_TCP | CSUM_UDP)
141ce4946daSBill Paul 
142ce4946daSBill Paul /*
143ce4946daSBill Paul  * Various supported device vendors/types and their names.
144ce4946daSBill Paul  */
14529658c96SDimitry Andric static const struct nge_type nge_devs[] = {
146ce4946daSBill Paul 	{ NGE_VENDORID, NGE_DEVICEID,
147ce4946daSBill Paul 	    "National Semiconductor Gigabit Ethernet" },
148ce4946daSBill Paul 	{ 0, 0, NULL }
149ce4946daSBill Paul };
150ce4946daSBill Paul 
151e51a25f8SAlfred Perlstein static int nge_probe(device_t);
152e51a25f8SAlfred Perlstein static int nge_attach(device_t);
153e51a25f8SAlfred Perlstein static int nge_detach(device_t);
154f6bc9430SPyun YongHyeon static int nge_shutdown(device_t);
155f6bc9430SPyun YongHyeon static int nge_suspend(device_t);
156f6bc9430SPyun YongHyeon static int nge_resume(device_t);
157ce4946daSBill Paul 
158f6bc9430SPyun YongHyeon static __inline void nge_discard_rxbuf(struct nge_softc *, int);
159f6bc9430SPyun YongHyeon static int nge_newbuf(struct nge_softc *, int);
160f6bc9430SPyun YongHyeon static int nge_encap(struct nge_softc *, struct mbuf **);
161f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
162ad6c618bSBill Paul static __inline void nge_fixup_rx(struct mbuf *);
163ad6c618bSBill Paul #endif
16456e13f2aSAttilio Rao static int nge_rxeof(struct nge_softc *);
165e51a25f8SAlfred Perlstein static void nge_txeof(struct nge_softc *);
166e51a25f8SAlfred Perlstein static void nge_intr(void *);
167e51a25f8SAlfred Perlstein static void nge_tick(void *);
168f6bc9430SPyun YongHyeon static void nge_stats_update(struct nge_softc *);
169e51a25f8SAlfred Perlstein static void nge_start(struct ifnet *);
170ad6c618bSBill Paul static void nge_start_locked(struct ifnet *);
171e51a25f8SAlfred Perlstein static int nge_ioctl(struct ifnet *, u_long, caddr_t);
172e51a25f8SAlfred Perlstein static void nge_init(void *);
173ad6c618bSBill Paul static void nge_init_locked(struct nge_softc *);
174f6bc9430SPyun YongHyeon static int nge_stop_mac(struct nge_softc *);
175e51a25f8SAlfred Perlstein static void nge_stop(struct nge_softc *);
176f6bc9430SPyun YongHyeon static void nge_wol(struct nge_softc *);
177f6bc9430SPyun YongHyeon static void nge_watchdog(struct nge_softc *);
178f6bc9430SPyun YongHyeon static int nge_mediachange(struct ifnet *);
179f6bc9430SPyun YongHyeon static void nge_mediastatus(struct ifnet *, struct ifmediareq *);
180ce4946daSBill Paul 
181e51a25f8SAlfred Perlstein static void nge_delay(struct nge_softc *);
182e51a25f8SAlfred Perlstein static void nge_eeprom_idle(struct nge_softc *);
183e51a25f8SAlfred Perlstein static void nge_eeprom_putbyte(struct nge_softc *, int);
1843929ff51SPyun YongHyeon static void nge_eeprom_getword(struct nge_softc *, int, uint16_t *);
185f6bc9430SPyun YongHyeon static void nge_read_eeprom(struct nge_softc *, caddr_t, int, int);
186ce4946daSBill Paul 
187e51a25f8SAlfred Perlstein static int nge_miibus_readreg(device_t, int, int);
188e51a25f8SAlfred Perlstein static int nge_miibus_writereg(device_t, int, int, int);
189e51a25f8SAlfred Perlstein static void nge_miibus_statchg(device_t);
190ce4946daSBill Paul 
191f6bc9430SPyun YongHyeon static void nge_rxfilter(struct nge_softc *);
192e51a25f8SAlfred Perlstein static void nge_reset(struct nge_softc *);
193f6bc9430SPyun YongHyeon static void nge_dmamap_cb(void *, bus_dma_segment_t *, int, int);
194f6bc9430SPyun YongHyeon static int nge_dma_alloc(struct nge_softc *);
195f6bc9430SPyun YongHyeon static void nge_dma_free(struct nge_softc *);
196e51a25f8SAlfred Perlstein static int nge_list_rx_init(struct nge_softc *);
197e51a25f8SAlfred Perlstein static int nge_list_tx_init(struct nge_softc *);
198f6bc9430SPyun YongHyeon static void nge_sysctl_node(struct nge_softc *);
199f6bc9430SPyun YongHyeon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
200f6bc9430SPyun YongHyeon static int sysctl_hw_nge_int_holdoff(SYSCTL_HANDLER_ARGS);
201ce4946daSBill Paul 
2028c1093fcSMarius Strobl /*
2038c1093fcSMarius Strobl  * MII bit-bang glue
2048c1093fcSMarius Strobl  */
2058c1093fcSMarius Strobl static uint32_t nge_mii_bitbang_read(device_t);
2068c1093fcSMarius Strobl static void nge_mii_bitbang_write(device_t, uint32_t);
2078c1093fcSMarius Strobl 
2088c1093fcSMarius Strobl static const struct mii_bitbang_ops nge_mii_bitbang_ops = {
2098c1093fcSMarius Strobl 	nge_mii_bitbang_read,
2108c1093fcSMarius Strobl 	nge_mii_bitbang_write,
2118c1093fcSMarius Strobl 	{
2128c1093fcSMarius Strobl 		NGE_MEAR_MII_DATA,	/* MII_BIT_MDO */
2138c1093fcSMarius Strobl 		NGE_MEAR_MII_DATA,	/* MII_BIT_MDI */
2148c1093fcSMarius Strobl 		NGE_MEAR_MII_CLK,	/* MII_BIT_MDC */
2158c1093fcSMarius Strobl 		NGE_MEAR_MII_DIR,	/* MII_BIT_DIR_HOST_PHY */
2168c1093fcSMarius Strobl 		0,			/* MII_BIT_DIR_PHY_HOST */
2178c1093fcSMarius Strobl 	}
2188c1093fcSMarius Strobl };
2198c1093fcSMarius Strobl 
220ce4946daSBill Paul static device_method_t nge_methods[] = {
221ce4946daSBill Paul 	/* Device interface */
222ce4946daSBill Paul 	DEVMETHOD(device_probe,		nge_probe),
223ce4946daSBill Paul 	DEVMETHOD(device_attach,	nge_attach),
224ce4946daSBill Paul 	DEVMETHOD(device_detach,	nge_detach),
225ce4946daSBill Paul 	DEVMETHOD(device_shutdown,	nge_shutdown),
226f6bc9430SPyun YongHyeon 	DEVMETHOD(device_suspend,	nge_suspend),
227f6bc9430SPyun YongHyeon 	DEVMETHOD(device_resume,	nge_resume),
228ce4946daSBill Paul 
229ce4946daSBill Paul 	/* MII interface */
230ce4946daSBill Paul 	DEVMETHOD(miibus_readreg,	nge_miibus_readreg),
231ce4946daSBill Paul 	DEVMETHOD(miibus_writereg,	nge_miibus_writereg),
232ce4946daSBill Paul 	DEVMETHOD(miibus_statchg,	nge_miibus_statchg),
233ce4946daSBill Paul 
2344b7ec270SMarius Strobl 	DEVMETHOD_END
235ce4946daSBill Paul };
236ce4946daSBill Paul 
237ce4946daSBill Paul static driver_t nge_driver = {
238ce4946daSBill Paul 	"nge",
239ce4946daSBill Paul 	nge_methods,
240ce4946daSBill Paul 	sizeof(struct nge_softc)
241ce4946daSBill Paul };
242ce4946daSBill Paul 
243ce4946daSBill Paul static devclass_t nge_devclass;
244ce4946daSBill Paul 
245f246e4a1SMatthew N. Dodd DRIVER_MODULE(nge, pci, nge_driver, nge_devclass, 0, 0);
246ce4946daSBill Paul DRIVER_MODULE(miibus, nge, miibus_driver, miibus_devclass, 0, 0);
247ce4946daSBill Paul 
248ce4946daSBill Paul #define NGE_SETBIT(sc, reg, x)				\
249ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
250ce4946daSBill Paul 		CSR_READ_4(sc, reg) | (x))
251ce4946daSBill Paul 
252ce4946daSBill Paul #define NGE_CLRBIT(sc, reg, x)				\
253ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
254ce4946daSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
255ce4946daSBill Paul 
256ce4946daSBill Paul #define SIO_SET(x)					\
25729f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x))
258ce4946daSBill Paul 
259ce4946daSBill Paul #define SIO_CLR(x)					\
26029f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x))
261ce4946daSBill Paul 
262eaabec55SAlfred Perlstein static void
263284c81cbSPyun YongHyeon nge_delay(struct nge_softc *sc)
264ce4946daSBill Paul {
265ce4946daSBill Paul 	int idx;
266ce4946daSBill Paul 
267ce4946daSBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
268ce4946daSBill Paul 		CSR_READ_4(sc, NGE_CSR);
269ce4946daSBill Paul }
270ce4946daSBill Paul 
271eaabec55SAlfred Perlstein static void
272284c81cbSPyun YongHyeon nge_eeprom_idle(struct nge_softc *sc)
273ce4946daSBill Paul {
2742cf2d799SPyun YongHyeon 	int i;
275ce4946daSBill Paul 
276ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
277ce4946daSBill Paul 	nge_delay(sc);
278ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CLK);
279ce4946daSBill Paul 	nge_delay(sc);
280ce4946daSBill Paul 
281ce4946daSBill Paul 	for (i = 0; i < 25; i++) {
282ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
283ce4946daSBill Paul 		nge_delay(sc);
284ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
285ce4946daSBill Paul 		nge_delay(sc);
286ce4946daSBill Paul 	}
287ce4946daSBill Paul 
288ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
289ce4946daSBill Paul 	nge_delay(sc);
290ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CSEL);
291ce4946daSBill Paul 	nge_delay(sc);
292ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0x00000000);
293ce4946daSBill Paul }
294ce4946daSBill Paul 
295ce4946daSBill Paul /*
296ce4946daSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
297ce4946daSBill Paul  */
298eaabec55SAlfred Perlstein static void
299284c81cbSPyun YongHyeon nge_eeprom_putbyte(struct nge_softc *sc, int addr)
300ce4946daSBill Paul {
3012cf2d799SPyun YongHyeon 	int d, i;
302ce4946daSBill Paul 
303ce4946daSBill Paul 	d = addr | NGE_EECMD_READ;
304ce4946daSBill Paul 
305ce4946daSBill Paul 	/*
306ce4946daSBill Paul 	 * Feed in each bit and stobe the clock.
307ce4946daSBill Paul 	 */
308ce4946daSBill Paul 	for (i = 0x400; i; i >>= 1) {
309ce4946daSBill Paul 		if (d & i) {
310ce4946daSBill Paul 			SIO_SET(NGE_MEAR_EE_DIN);
311ce4946daSBill Paul 		} else {
312ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_EE_DIN);
313ce4946daSBill Paul 		}
314ce4946daSBill Paul 		nge_delay(sc);
315ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
316ce4946daSBill Paul 		nge_delay(sc);
317ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
318ce4946daSBill Paul 		nge_delay(sc);
319ce4946daSBill Paul 	}
320ce4946daSBill Paul }
321ce4946daSBill Paul 
322ce4946daSBill Paul /*
323ce4946daSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
324ce4946daSBill Paul  */
325eaabec55SAlfred Perlstein static void
3263929ff51SPyun YongHyeon nge_eeprom_getword(struct nge_softc *sc, int addr, uint16_t *dest)
327ce4946daSBill Paul {
3282cf2d799SPyun YongHyeon 	int i;
3293929ff51SPyun YongHyeon 	uint16_t word = 0;
330ce4946daSBill Paul 
331ce4946daSBill Paul 	/* Force EEPROM to idle state. */
332ce4946daSBill Paul 	nge_eeprom_idle(sc);
333ce4946daSBill Paul 
334ce4946daSBill Paul 	/* Enter EEPROM access mode. */
335ce4946daSBill Paul 	nge_delay(sc);
336ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
337ce4946daSBill Paul 	nge_delay(sc);
338ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
339ce4946daSBill Paul 	nge_delay(sc);
340ce4946daSBill Paul 
341ce4946daSBill Paul 	/*
342ce4946daSBill Paul 	 * Send address of word we want to read.
343ce4946daSBill Paul 	 */
344ce4946daSBill Paul 	nge_eeprom_putbyte(sc, addr);
345ce4946daSBill Paul 
346ce4946daSBill Paul 	/*
347ce4946daSBill Paul 	 * Start reading bits from EEPROM.
348ce4946daSBill Paul 	 */
349ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
350ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
351ce4946daSBill Paul 		nge_delay(sc);
352ce4946daSBill Paul 		if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT)
353ce4946daSBill Paul 			word |= i;
354ce4946daSBill Paul 		nge_delay(sc);
355ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
356ce4946daSBill Paul 		nge_delay(sc);
357ce4946daSBill Paul 	}
358ce4946daSBill Paul 
359ce4946daSBill Paul 	/* Turn off EEPROM access mode. */
360ce4946daSBill Paul 	nge_eeprom_idle(sc);
361ce4946daSBill Paul 
362ce4946daSBill Paul 	*dest = word;
363ce4946daSBill Paul }
364ce4946daSBill Paul 
365ce4946daSBill Paul /*
366ce4946daSBill Paul  * Read a sequence of words from the EEPROM.
367ce4946daSBill Paul  */
368eaabec55SAlfred Perlstein static void
369f6bc9430SPyun YongHyeon nge_read_eeprom(struct nge_softc *sc, caddr_t dest, int off, int cnt)
370ce4946daSBill Paul {
371ce4946daSBill Paul 	int i;
3723929ff51SPyun YongHyeon 	uint16_t word = 0, *ptr;
373ce4946daSBill Paul 
374ce4946daSBill Paul 	for (i = 0; i < cnt; i++) {
375ce4946daSBill Paul 		nge_eeprom_getword(sc, off + i, &word);
3763929ff51SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
377ce4946daSBill Paul 		*ptr = word;
378ce4946daSBill Paul 	}
379ce4946daSBill Paul }
380ce4946daSBill Paul 
381ce4946daSBill Paul /*
3828c1093fcSMarius Strobl  * Read the MII serial port for the MII bit-bang module.
3838c1093fcSMarius Strobl  */
3848c1093fcSMarius Strobl static uint32_t
3858c1093fcSMarius Strobl nge_mii_bitbang_read(device_t dev)
3868c1093fcSMarius Strobl {
3878c1093fcSMarius Strobl 	struct nge_softc *sc;
3888c1093fcSMarius Strobl 	uint32_t val;
3898c1093fcSMarius Strobl 
3908c1093fcSMarius Strobl 	sc = device_get_softc(dev);
3918c1093fcSMarius Strobl 
3928c1093fcSMarius Strobl 	val = CSR_READ_4(sc, NGE_MEAR);
3938c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_MEAR,
3948c1093fcSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
3958c1093fcSMarius Strobl 
3968c1093fcSMarius Strobl 	return (val);
3978c1093fcSMarius Strobl }
3988c1093fcSMarius Strobl 
3998c1093fcSMarius Strobl /*
4008c1093fcSMarius Strobl  * Write the MII serial port for the MII bit-bang module.
401ce4946daSBill Paul  */
402eaabec55SAlfred Perlstein static void
4038c1093fcSMarius Strobl nge_mii_bitbang_write(device_t dev, uint32_t val)
404ce4946daSBill Paul {
4058c1093fcSMarius Strobl 	struct nge_softc *sc;
406ce4946daSBill Paul 
4078c1093fcSMarius Strobl 	sc = device_get_softc(dev);
408ce4946daSBill Paul 
4098c1093fcSMarius Strobl 	CSR_WRITE_4(sc, NGE_MEAR, val);
4108c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_MEAR,
4118c1093fcSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
412ce4946daSBill Paul }
413ce4946daSBill Paul 
414eaabec55SAlfred Perlstein static int
415284c81cbSPyun YongHyeon nge_miibus_readreg(device_t dev, int phy, int reg)
416ce4946daSBill Paul {
417ce4946daSBill Paul 	struct nge_softc *sc;
418f6bc9430SPyun YongHyeon 	int rv;
419ce4946daSBill Paul 
420ce4946daSBill Paul 	sc = device_get_softc(dev);
421f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0) {
422f6bc9430SPyun YongHyeon 		/* Pretend PHY is at address 0. */
423f6bc9430SPyun YongHyeon 		if (phy != 0)
424f6bc9430SPyun YongHyeon 			return (0);
425f6bc9430SPyun YongHyeon 		switch (reg) {
426f6bc9430SPyun YongHyeon 		case MII_BMCR:
427f6bc9430SPyun YongHyeon 			reg = NGE_TBI_BMCR;
428f6bc9430SPyun YongHyeon 			break;
429f6bc9430SPyun YongHyeon 		case MII_BMSR:
430f6bc9430SPyun YongHyeon 			/* 83820/83821 has different bit layout for BMSR. */
431f6bc9430SPyun YongHyeon 			rv = BMSR_ANEG | BMSR_EXTCAP | BMSR_EXTSTAT;
432f6bc9430SPyun YongHyeon 			reg = CSR_READ_4(sc, NGE_TBI_BMSR);
433f6bc9430SPyun YongHyeon 			if ((reg & NGE_TBIBMSR_ANEG_DONE) != 0)
434f6bc9430SPyun YongHyeon 				rv |= BMSR_ACOMP;
435f6bc9430SPyun YongHyeon 			if ((reg & NGE_TBIBMSR_LINKSTAT) != 0)
436f6bc9430SPyun YongHyeon 				rv |= BMSR_LINK;
437f6bc9430SPyun YongHyeon 			return (rv);
438f6bc9430SPyun YongHyeon 		case MII_ANAR:
439f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANAR;
440f6bc9430SPyun YongHyeon 			break;
441f6bc9430SPyun YongHyeon 		case MII_ANLPAR:
442f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANLPAR;
443f6bc9430SPyun YongHyeon 			break;
444f6bc9430SPyun YongHyeon 		case MII_ANER:
445f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANER;
446f6bc9430SPyun YongHyeon 			break;
447f6bc9430SPyun YongHyeon 		case MII_EXTSR:
448f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ESR;
449f6bc9430SPyun YongHyeon 			break;
450f6bc9430SPyun YongHyeon 		case MII_PHYIDR1:
451f6bc9430SPyun YongHyeon 		case MII_PHYIDR2:
452f6bc9430SPyun YongHyeon 			return (0);
453f6bc9430SPyun YongHyeon 		default:
454f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
455f6bc9430SPyun YongHyeon 			    "bad phy register read : %d\n", reg);
456f6bc9430SPyun YongHyeon 			return (0);
457f6bc9430SPyun YongHyeon 		}
458f6bc9430SPyun YongHyeon 		return (CSR_READ_4(sc, reg));
459f6bc9430SPyun YongHyeon 	}
460ce4946daSBill Paul 
4618c1093fcSMarius Strobl 	return (mii_bitbang_readreg(dev, &nge_mii_bitbang_ops, phy, reg));
462ce4946daSBill Paul }
463ce4946daSBill Paul 
464eaabec55SAlfred Perlstein static int
465284c81cbSPyun YongHyeon nge_miibus_writereg(device_t dev, int phy, int reg, int data)
466ce4946daSBill Paul {
467ce4946daSBill Paul 	struct nge_softc *sc;
468ce4946daSBill Paul 
469ce4946daSBill Paul 	sc = device_get_softc(dev);
470f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0) {
471f6bc9430SPyun YongHyeon 		/* Pretend PHY is at address 0. */
472f6bc9430SPyun YongHyeon 		if (phy != 0)
473f6bc9430SPyun YongHyeon 			return (0);
474f6bc9430SPyun YongHyeon 		switch (reg) {
475f6bc9430SPyun YongHyeon 		case MII_BMCR:
476f6bc9430SPyun YongHyeon 			reg = NGE_TBI_BMCR;
477f6bc9430SPyun YongHyeon 			break;
478f6bc9430SPyun YongHyeon 		case MII_BMSR:
479f6bc9430SPyun YongHyeon 			return (0);
480f6bc9430SPyun YongHyeon 		case MII_ANAR:
481f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANAR;
482f6bc9430SPyun YongHyeon 			break;
483f6bc9430SPyun YongHyeon 		case MII_ANLPAR:
484f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANLPAR;
485f6bc9430SPyun YongHyeon 			break;
486f6bc9430SPyun YongHyeon 		case MII_ANER:
487f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANER;
488f6bc9430SPyun YongHyeon 			break;
489f6bc9430SPyun YongHyeon 		case MII_EXTSR:
490f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ESR;
491f6bc9430SPyun YongHyeon 			break;
492f6bc9430SPyun YongHyeon 		case MII_PHYIDR1:
493f6bc9430SPyun YongHyeon 		case MII_PHYIDR2:
494f6bc9430SPyun YongHyeon 			return (0);
495f6bc9430SPyun YongHyeon 		default:
496f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
497f6bc9430SPyun YongHyeon 			    "bad phy register write : %d\n", reg);
498f6bc9430SPyun YongHyeon 			return (0);
499f6bc9430SPyun YongHyeon 		}
500f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, reg, data);
501f6bc9430SPyun YongHyeon 		return (0);
502f6bc9430SPyun YongHyeon 	}
503ce4946daSBill Paul 
5048c1093fcSMarius Strobl 	mii_bitbang_writereg(dev, &nge_mii_bitbang_ops, phy, reg, data);
505ce4946daSBill Paul 
506ce4946daSBill Paul 	return (0);
507ce4946daSBill Paul }
508ce4946daSBill Paul 
509f6bc9430SPyun YongHyeon /*
510f6bc9430SPyun YongHyeon  * media status/link state change handler.
511f6bc9430SPyun YongHyeon  */
512eaabec55SAlfred Perlstein static void
513284c81cbSPyun YongHyeon nge_miibus_statchg(device_t dev)
514ce4946daSBill Paul {
515ce4946daSBill Paul 	struct nge_softc *sc;
516ce4946daSBill Paul 	struct mii_data *mii;
517f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
518f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
519f6bc9430SPyun YongHyeon 	uint32_t done, reg, status;
520f6bc9430SPyun YongHyeon 	int i;
521ce4946daSBill Paul 
522ce4946daSBill Paul 	sc = device_get_softc(dev);
523f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
5241f548804SDoug Ambrisko 
525ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
526f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
527f6bc9430SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
528f6bc9430SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
529f6bc9430SPyun YongHyeon 		return;
530ce4946daSBill Paul 
531f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
532f6bc9430SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) ==
533f6bc9430SPyun YongHyeon 	    (IFM_AVALID | IFM_ACTIVE)) {
534f6bc9430SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
535f6bc9430SPyun YongHyeon 		case IFM_10_T:
536f6bc9430SPyun YongHyeon 		case IFM_100_TX:
537f6bc9430SPyun YongHyeon 		case IFM_1000_T:
538f6bc9430SPyun YongHyeon 		case IFM_1000_SX:
539f6bc9430SPyun YongHyeon 		case IFM_1000_LX:
540f6bc9430SPyun YongHyeon 		case IFM_1000_CX:
541f6bc9430SPyun YongHyeon 			sc->nge_flags |= NGE_FLAG_LINK;
542f6bc9430SPyun YongHyeon 			break;
543f6bc9430SPyun YongHyeon 		default:
544f6bc9430SPyun YongHyeon 			break;
545f6bc9430SPyun YongHyeon 		}
546f6bc9430SPyun YongHyeon 	}
547f6bc9430SPyun YongHyeon 
548f6bc9430SPyun YongHyeon 	/* Stop Tx/Rx MACs. */
549f6bc9430SPyun YongHyeon 	if (nge_stop_mac(sc) == ETIMEDOUT)
550f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
551f6bc9430SPyun YongHyeon 		    "%s: unable to stop Tx/Rx MAC\n", __func__);
552f6bc9430SPyun YongHyeon 	nge_txeof(sc);
553f6bc9430SPyun YongHyeon 	nge_rxeof(sc);
554f6bc9430SPyun YongHyeon 	if (sc->nge_head != NULL) {
555f6bc9430SPyun YongHyeon 		m_freem(sc->nge_head);
556f6bc9430SPyun YongHyeon 		sc->nge_head = sc->nge_tail = NULL;
557f6bc9430SPyun YongHyeon 	}
558f6bc9430SPyun YongHyeon 
559f6bc9430SPyun YongHyeon 	/* Release queued frames. */
560f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
561f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
562f6bc9430SPyun YongHyeon 		if (txd->tx_m != NULL) {
563f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_tx_tag,
564f6bc9430SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
565f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_tag,
566f6bc9430SPyun YongHyeon 			    txd->tx_dmamap);
567f6bc9430SPyun YongHyeon 			m_freem(txd->tx_m);
568f6bc9430SPyun YongHyeon 			txd->tx_m = NULL;
569f6bc9430SPyun YongHyeon 		}
570f6bc9430SPyun YongHyeon 	}
571f6bc9430SPyun YongHyeon 
572f6bc9430SPyun YongHyeon 	/* Program MAC with resolved speed/duplex. */
573f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_LINK) != 0) {
574f6bc9430SPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
575ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_TX_CFG,
576ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT | NGE_TXCFG_IGN_CARR));
577ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
578f6bc9430SPyun YongHyeon #ifdef notyet
579f6bc9430SPyun YongHyeon 			/* Enable flow-control. */
580f6bc9430SPyun YongHyeon 			if ((IFM_OPTIONS(mii->mii_media_active) &
581f6bc9430SPyun YongHyeon 			    (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) != 0)
582f6bc9430SPyun YongHyeon 				NGE_SETBIT(sc, NGE_PAUSECSR,
583f6bc9430SPyun YongHyeon 				    NGE_PAUSECSR_PAUSE_ENB);
584f6bc9430SPyun YongHyeon #endif
585ce4946daSBill Paul 		} else {
586ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
587ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT | NGE_TXCFG_IGN_CARR));
588ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
589f6bc9430SPyun YongHyeon 			NGE_CLRBIT(sc, NGE_PAUSECSR, NGE_PAUSECSR_PAUSE_ENB);
590f6bc9430SPyun YongHyeon 		}
591f6bc9430SPyun YongHyeon 		/* If we have a 1000Mbps link, set the mode_1000 bit. */
592f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CFG);
593f6bc9430SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
594f6bc9430SPyun YongHyeon 		case IFM_1000_SX:
595f6bc9430SPyun YongHyeon 		case IFM_1000_LX:
596f6bc9430SPyun YongHyeon 		case IFM_1000_CX:
597f6bc9430SPyun YongHyeon 		case IFM_1000_T:
598f6bc9430SPyun YongHyeon 			reg |= NGE_CFG_MODE_1000;
599f6bc9430SPyun YongHyeon 			break;
600f6bc9430SPyun YongHyeon 		default:
601f6bc9430SPyun YongHyeon 			reg &= ~NGE_CFG_MODE_1000;
602f6bc9430SPyun YongHyeon 			break;
603f6bc9430SPyun YongHyeon 		}
604f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CFG, reg);
605f6bc9430SPyun YongHyeon 
606f6bc9430SPyun YongHyeon 		/* Reset Tx/Rx MAC. */
607f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CSR);
608f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_TX_RESET | NGE_CSR_RX_RESET;
609f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
610f6bc9430SPyun YongHyeon 		/* Check the completion of reset. */
611f6bc9430SPyun YongHyeon 		done = 0;
612f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
613f6bc9430SPyun YongHyeon 			DELAY(1);
614f6bc9430SPyun YongHyeon 			status = CSR_READ_4(sc, NGE_ISR);
615f6bc9430SPyun YongHyeon 			if ((status & NGE_ISR_RX_RESET_DONE) != 0)
616f6bc9430SPyun YongHyeon 				done |= NGE_ISR_RX_RESET_DONE;
617f6bc9430SPyun YongHyeon 			if ((status & NGE_ISR_TX_RESET_DONE) != 0)
618f6bc9430SPyun YongHyeon 				done |= NGE_ISR_TX_RESET_DONE;
619f6bc9430SPyun YongHyeon 			if (done ==
620f6bc9430SPyun YongHyeon 			    (NGE_ISR_TX_RESET_DONE | NGE_ISR_RX_RESET_DONE))
621f6bc9430SPyun YongHyeon 				break;
622f6bc9430SPyun YongHyeon 		}
623f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
624f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
625f6bc9430SPyun YongHyeon 			    "%s: unable to reset Tx/Rx MAC\n", __func__);
626f6bc9430SPyun YongHyeon 		/* Reuse Rx buffer and reset consumer pointer. */
627f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_cons = 0;
628f6bc9430SPyun YongHyeon 		/*
629f6bc9430SPyun YongHyeon 		 * It seems that resetting Rx/Tx MAC results in
630f6bc9430SPyun YongHyeon 		 * resetting Tx/Rx descriptor pointer registers such
631f6bc9430SPyun YongHyeon 		 * that reloading Tx/Rx lists address are needed.
632f6bc9430SPyun YongHyeon 		 */
633f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI,
634f6bc9430SPyun YongHyeon 		    NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr));
635f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO,
636f6bc9430SPyun YongHyeon 		    NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr));
637f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI,
638f6bc9430SPyun YongHyeon 		    NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr));
639f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO,
640f6bc9430SPyun YongHyeon 		    NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr));
641f6bc9430SPyun YongHyeon 		/* Reinitialize Tx buffers. */
642f6bc9430SPyun YongHyeon 		nge_list_tx_init(sc);
643f6bc9430SPyun YongHyeon 
644f6bc9430SPyun YongHyeon 		/* Restart Rx MAC. */
645f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CSR);
646f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_RX_ENABLE;
647f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
648f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
649f6bc9430SPyun YongHyeon 			if ((CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RX_ENABLE) != 0)
650f6bc9430SPyun YongHyeon 				break;
651f6bc9430SPyun YongHyeon 			DELAY(1);
652f6bc9430SPyun YongHyeon 		}
653f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
654f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
655f6bc9430SPyun YongHyeon 			    "%s: unable to restart Rx MAC\n", __func__);
656ce4946daSBill Paul 	}
657ce4946daSBill Paul 
658f6bc9430SPyun YongHyeon 	/* Data LED off for TBI mode */
659f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
660f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
661f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT);
662ce4946daSBill Paul }
663ce4946daSBill Paul 
6649bf5c8b4SGleb Smirnoff static u_int
6659bf5c8b4SGleb Smirnoff nge_write_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6669bf5c8b4SGleb Smirnoff {
6679bf5c8b4SGleb Smirnoff 	struct nge_softc *sc = arg;
6689bf5c8b4SGleb Smirnoff 	uint32_t h;
6699bf5c8b4SGleb Smirnoff 	int bit, index;
6709bf5c8b4SGleb Smirnoff 
6719bf5c8b4SGleb Smirnoff 	/*
6729bf5c8b4SGleb Smirnoff 	 * From the 11 bits returned by the crc routine, the top 7
6739bf5c8b4SGleb Smirnoff 	 * bits represent the 16-bit word in the mcast hash table
6749bf5c8b4SGleb Smirnoff 	 * that needs to be updated, and the lower 4 bits represent
6759bf5c8b4SGleb Smirnoff 	 * which bit within that byte needs to be set.
6769bf5c8b4SGleb Smirnoff 	 */
6779bf5c8b4SGleb Smirnoff 	h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 21;
6789bf5c8b4SGleb Smirnoff 	index = (h >> 4) & 0x7F;
6799bf5c8b4SGleb Smirnoff 	bit = h & 0xF;
6809bf5c8b4SGleb Smirnoff 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + (index * 2));
6819bf5c8b4SGleb Smirnoff 	NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit));
6829bf5c8b4SGleb Smirnoff 
6839bf5c8b4SGleb Smirnoff 	return (1);
6849bf5c8b4SGleb Smirnoff }
6859bf5c8b4SGleb Smirnoff 
686eaabec55SAlfred Perlstein static void
687f6bc9430SPyun YongHyeon nge_rxfilter(struct nge_softc *sc)
688ce4946daSBill Paul {
689ce4946daSBill Paul 	struct ifnet *ifp;
6909bf5c8b4SGleb Smirnoff 	uint32_t i, rxfilt;
691ce4946daSBill Paul 
692ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
693fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
694ce4946daSBill Paul 
695f6bc9430SPyun YongHyeon 	/* Make sure to stop Rx filtering. */
696f6bc9430SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, NGE_RXFILT_CTL);
697f6bc9430SPyun YongHyeon 	rxfilt &= ~NGE_RXFILTCTL_ENABLE;
698f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
6998c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
700f6bc9430SPyun YongHyeon 
701f6bc9430SPyun YongHyeon 	rxfilt &= ~(NGE_RXFILTCTL_ALLMULTI | NGE_RXFILTCTL_ALLPHYS);
702f6bc9430SPyun YongHyeon 	rxfilt &= ~NGE_RXFILTCTL_BROAD;
703f6bc9430SPyun YongHyeon 	/*
704f6bc9430SPyun YongHyeon 	 * We don't want to use the hash table for matching unicast
705f6bc9430SPyun YongHyeon 	 * addresses.
706f6bc9430SPyun YongHyeon 	 */
707f6bc9430SPyun YongHyeon 	rxfilt &= ~(NGE_RXFILTCTL_MCHASH | NGE_RXFILTCTL_UCHASH);
708f6bc9430SPyun YongHyeon 
709f6bc9430SPyun YongHyeon 	/*
710f6bc9430SPyun YongHyeon 	 * For the NatSemi chip, we have to explicitly enable the
711f6bc9430SPyun YongHyeon 	 * reception of ARP frames, as well as turn on the 'perfect
712f6bc9430SPyun YongHyeon 	 * match' filter where we store the station address, otherwise
713f6bc9430SPyun YongHyeon 	 * we won't receive unicasts meant for this host.
714f6bc9430SPyun YongHyeon 	 */
715f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_ARP | NGE_RXFILTCTL_PERFECT;
716f6bc9430SPyun YongHyeon 
717f6bc9430SPyun YongHyeon 	/*
718f6bc9430SPyun YongHyeon 	 * Set the capture broadcast bit to capture broadcast frames.
719f6bc9430SPyun YongHyeon 	 */
720f6bc9430SPyun YongHyeon 	if ((ifp->if_flags & IFF_BROADCAST) != 0)
721f6bc9430SPyun YongHyeon 		rxfilt |= NGE_RXFILTCTL_BROAD;
722f6bc9430SPyun YongHyeon 
723f6bc9430SPyun YongHyeon 	if ((ifp->if_flags & IFF_PROMISC) != 0 ||
724f6bc9430SPyun YongHyeon 	    (ifp->if_flags & IFF_ALLMULTI) != 0) {
725f6bc9430SPyun YongHyeon 		rxfilt |= NGE_RXFILTCTL_ALLMULTI;
726f6bc9430SPyun YongHyeon 		if ((ifp->if_flags & IFF_PROMISC) != 0)
727f6bc9430SPyun YongHyeon 			rxfilt |= NGE_RXFILTCTL_ALLPHYS;
728f6bc9430SPyun YongHyeon 		goto done;
729ce4946daSBill Paul 	}
730ce4946daSBill Paul 
731ce4946daSBill Paul 	/*
732ce4946daSBill Paul 	 * We have to explicitly enable the multicast hash table
733ce4946daSBill Paul 	 * on the NatSemi chip if we want to use it, which we do.
734ce4946daSBill Paul 	 */
735f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_MCHASH;
736ce4946daSBill Paul 
737ce4946daSBill Paul 	/* first, zot all the existing hash bits */
738ce4946daSBill Paul 	for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) {
739ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i);
740ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0);
741ce4946daSBill Paul 	}
742ce4946daSBill Paul 
7439bf5c8b4SGleb Smirnoff 	if_foreach_llmaddr(ifp, nge_write_maddr, sc);
744f6bc9430SPyun YongHyeon done:
745f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
746f6bc9430SPyun YongHyeon 	/* Turn the receive filter on. */
747f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_ENABLE;
748f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
7498c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
750ce4946daSBill Paul }
751ce4946daSBill Paul 
752eaabec55SAlfred Perlstein static void
753284c81cbSPyun YongHyeon nge_reset(struct nge_softc *sc)
754ce4946daSBill Paul {
755f6bc9430SPyun YongHyeon 	uint32_t v;
7562cf2d799SPyun YongHyeon 	int i;
757ce4946daSBill Paul 
758ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET);
759ce4946daSBill Paul 
760ce4946daSBill Paul 	for (i = 0; i < NGE_TIMEOUT; i++) {
761ce4946daSBill Paul 		if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET))
762ce4946daSBill Paul 			break;
763f6bc9430SPyun YongHyeon 		DELAY(1);
764ce4946daSBill Paul 	}
765ce4946daSBill Paul 
766ce4946daSBill Paul 	if (i == NGE_TIMEOUT)
7676b9f5c94SGleb Smirnoff 		device_printf(sc->nge_dev, "reset never completed\n");
768ce4946daSBill Paul 
769ce4946daSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
770ce4946daSBill Paul 	DELAY(1000);
771ce4946daSBill Paul 
772ce4946daSBill Paul 	/*
773ce4946daSBill Paul 	 * If this is a NetSemi chip, make sure to clear
774ce4946daSBill Paul 	 * PME mode.
775ce4946daSBill Paul 	 */
776ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS);
777ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, 0);
778f6bc9430SPyun YongHyeon 
779f6bc9430SPyun YongHyeon 	/* Clear WOL events which may interfere normal Rx filter opertaion. */
780f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_WOLCSR, 0);
781f6bc9430SPyun YongHyeon 
782f6bc9430SPyun YongHyeon 	/*
783f6bc9430SPyun YongHyeon 	 * Only DP83820 supports 64bits addressing/data transfers and
784f6bc9430SPyun YongHyeon 	 * 64bit addressing requires different descriptor structures.
785f6bc9430SPyun YongHyeon 	 * To make it simple, disable 64bit addressing/data transfers.
786f6bc9430SPyun YongHyeon 	 */
787f6bc9430SPyun YongHyeon 	v = CSR_READ_4(sc, NGE_CFG);
788f6bc9430SPyun YongHyeon 	v &= ~(NGE_CFG_64BIT_ADDR_ENB | NGE_CFG_64BIT_DATA_ENB);
789f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_CFG, v);
790ce4946daSBill Paul }
791ce4946daSBill Paul 
792ce4946daSBill Paul /*
793d64ada50SJens Schweikhardt  * Probe for a NatSemi chip. Check the PCI vendor and device
794ce4946daSBill Paul  * IDs against our list and return a device name if we find a match.
795ce4946daSBill Paul  */
796eaabec55SAlfred Perlstein static int
797284c81cbSPyun YongHyeon nge_probe(device_t dev)
798ce4946daSBill Paul {
7998c1093fcSMarius Strobl 	const struct nge_type *t;
800ce4946daSBill Paul 
801ce4946daSBill Paul 	t = nge_devs;
802ce4946daSBill Paul 
803ce4946daSBill Paul 	while (t->nge_name != NULL) {
804ce4946daSBill Paul 		if ((pci_get_vendor(dev) == t->nge_vid) &&
805ce4946daSBill Paul 		    (pci_get_device(dev) == t->nge_did)) {
806ce4946daSBill Paul 			device_set_desc(dev, t->nge_name);
8076b9907e7SWarner Losh 			return (BUS_PROBE_DEFAULT);
808ce4946daSBill Paul 		}
809ce4946daSBill Paul 		t++;
810ce4946daSBill Paul 	}
811ce4946daSBill Paul 
812ce4946daSBill Paul 	return (ENXIO);
813ce4946daSBill Paul }
814ce4946daSBill Paul 
815ce4946daSBill Paul /*
816ce4946daSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
817ce4946daSBill Paul  * setup and ethernet/BPF attach.
818ce4946daSBill Paul  */
819eaabec55SAlfred Perlstein static int
820284c81cbSPyun YongHyeon nge_attach(device_t dev)
821ce4946daSBill Paul {
822f6bc9430SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
823f6bc9430SPyun YongHyeon 	uint16_t ea[ETHER_ADDR_LEN/2], ea_temp, reg;
824ce4946daSBill Paul 	struct nge_softc *sc;
825f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
826f6bc9430SPyun YongHyeon 	int error, i, rid;
827ce4946daSBill Paul 
828f6bc9430SPyun YongHyeon 	error = 0;
829ce4946daSBill Paul 	sc = device_get_softc(dev);
8306b9f5c94SGleb Smirnoff 	sc->nge_dev = dev;
831ce4946daSBill Paul 
832ad6c618bSBill Paul 	NGE_LOCK_INIT(sc, device_get_nameunit(dev));
833646abee6SJohn Baldwin 	callout_init_mtx(&sc->nge_stat_ch, &sc->nge_mtx, 0);
834646abee6SJohn Baldwin 
835ce4946daSBill Paul 	/*
836ce4946daSBill Paul 	 * Map control/status registers.
837ce4946daSBill Paul 	 */
838ce4946daSBill Paul 	pci_enable_busmaster(dev);
839ce4946daSBill Paul 
840f6bc9430SPyun YongHyeon #ifdef NGE_USEIOSPACE
841f6bc9430SPyun YongHyeon 	sc->nge_res_type = SYS_RES_IOPORT;
842f6bc9430SPyun YongHyeon 	sc->nge_res_id = PCIR_BAR(0);
843f6bc9430SPyun YongHyeon #else
844f6bc9430SPyun YongHyeon 	sc->nge_res_type = SYS_RES_MEMORY;
845f6bc9430SPyun YongHyeon 	sc->nge_res_id = PCIR_BAR(1);
846f6bc9430SPyun YongHyeon #endif
847f6bc9430SPyun YongHyeon 	sc->nge_res = bus_alloc_resource_any(dev, sc->nge_res_type,
848f6bc9430SPyun YongHyeon 	    &sc->nge_res_id, RF_ACTIVE);
849ce4946daSBill Paul 
850ce4946daSBill Paul 	if (sc->nge_res == NULL) {
851f6bc9430SPyun YongHyeon 		if (sc->nge_res_type == SYS_RES_MEMORY) {
852f6bc9430SPyun YongHyeon 			sc->nge_res_type = SYS_RES_IOPORT;
853f6bc9430SPyun YongHyeon 			sc->nge_res_id = PCIR_BAR(0);
854f6bc9430SPyun YongHyeon 		} else {
855f6bc9430SPyun YongHyeon 			sc->nge_res_type = SYS_RES_MEMORY;
856f6bc9430SPyun YongHyeon 			sc->nge_res_id = PCIR_BAR(1);
857ce4946daSBill Paul 		}
858f6bc9430SPyun YongHyeon 		sc->nge_res = bus_alloc_resource_any(dev, sc->nge_res_type,
859f6bc9430SPyun YongHyeon 		    &sc->nge_res_id, RF_ACTIVE);
860f6bc9430SPyun YongHyeon 		if (sc->nge_res == NULL) {
861f6bc9430SPyun YongHyeon 			device_printf(dev, "couldn't allocate %s resources\n",
862f6bc9430SPyun YongHyeon 			    sc->nge_res_type == SYS_RES_MEMORY ? "memory" :
863f6bc9430SPyun YongHyeon 			    "I/O");
864f6bc9430SPyun YongHyeon 			NGE_LOCK_DESTROY(sc);
865f6bc9430SPyun YongHyeon 			return (ENXIO);
866f6bc9430SPyun YongHyeon 		}
867f6bc9430SPyun YongHyeon 	}
868ce4946daSBill Paul 
869ce4946daSBill Paul 	/* Allocate interrupt */
870ce4946daSBill Paul 	rid = 0;
8715f96beb9SNate Lawson 	sc->nge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
872ce4946daSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
873ce4946daSBill Paul 
874ce4946daSBill Paul 	if (sc->nge_irq == NULL) {
875646abee6SJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
876ce4946daSBill Paul 		error = ENXIO;
877ce4946daSBill Paul 		goto fail;
878ce4946daSBill Paul 	}
879ce4946daSBill Paul 
880f6bc9430SPyun YongHyeon 	/* Enable MWI. */
881f6bc9430SPyun YongHyeon 	reg = pci_read_config(dev, PCIR_COMMAND, 2);
882f6bc9430SPyun YongHyeon 	reg |= PCIM_CMD_MWRICEN;
883f6bc9430SPyun YongHyeon 	pci_write_config(dev, PCIR_COMMAND, reg, 2);
884f6bc9430SPyun YongHyeon 
885ce4946daSBill Paul 	/* Reset the adapter. */
886ce4946daSBill Paul 	nge_reset(sc);
887ce4946daSBill Paul 
888ce4946daSBill Paul 	/*
889ce4946daSBill Paul 	 * Get station address from the EEPROM.
890ce4946daSBill Paul 	 */
891f6bc9430SPyun YongHyeon 	nge_read_eeprom(sc, (caddr_t)ea, NGE_EE_NODEADDR, 3);
892f6bc9430SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
893f6bc9430SPyun YongHyeon 		ea[i] = le16toh(ea[i]);
894f6bc9430SPyun YongHyeon 	ea_temp = ea[0];
895f6bc9430SPyun YongHyeon 	ea[0] = ea[2];
896f6bc9430SPyun YongHyeon 	ea[2] = ea_temp;
897f6bc9430SPyun YongHyeon 	bcopy(ea, eaddr, sizeof(eaddr));
898ce4946daSBill Paul 
899f6bc9430SPyun YongHyeon 	if (nge_dma_alloc(sc) != 0) {
900ce4946daSBill Paul 		error = ENXIO;
901ce4946daSBill Paul 		goto fail;
902ce4946daSBill Paul 	}
903ce4946daSBill Paul 
904f6bc9430SPyun YongHyeon 	nge_sysctl_node(sc);
905f6bc9430SPyun YongHyeon 
906fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp = if_alloc(IFT_ETHER);
907fc74a9f9SBrooks Davis 	if (ifp == NULL) {
908f6bc9430SPyun YongHyeon 		device_printf(dev, "can not allocate ifnet structure\n");
909fc74a9f9SBrooks Davis 		error = ENOSPC;
910fc74a9f9SBrooks Davis 		goto fail;
911fc74a9f9SBrooks Davis 	}
912ce4946daSBill Paul 	ifp->if_softc = sc;
9139bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
914ad6c618bSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
915ce4946daSBill Paul 	ifp->if_ioctl = nge_ioctl;
916ce4946daSBill Paul 	ifp->if_start = nge_start;
917ce4946daSBill Paul 	ifp->if_init = nge_init;
918f6bc9430SPyun YongHyeon 	ifp->if_snd.ifq_drv_maxlen = NGE_TX_RING_CNT - 1;
919f6bc9430SPyun YongHyeon 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
920f6bc9430SPyun YongHyeon 	IFQ_SET_READY(&ifp->if_snd);
921ce4946daSBill Paul 	ifp->if_hwassist = NGE_CSUM_FEATURES;
922f6bc9430SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
923ce4946daSBill Paul 	/*
924f6bc9430SPyun YongHyeon 	 * It seems that some hardwares doesn't provide 3.3V auxiliary
925f6bc9430SPyun YongHyeon 	 * supply(3VAUX) to drive PME such that checking PCI power
926f6bc9430SPyun YongHyeon 	 * management capability is necessary.
927ce4946daSBill Paul 	 */
9283b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &i) == 0)
929f6bc9430SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
930f6bc9430SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
931f6bc9430SPyun YongHyeon 
932f6bc9430SPyun YongHyeon 	if ((CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) != 0) {
933f6bc9430SPyun YongHyeon 		sc->nge_flags |= NGE_FLAG_TBI;
9341f548804SDoug Ambrisko 		device_printf(dev, "Using TBI\n");
935f6bc9430SPyun YongHyeon 		/* Configure GPIO. */
9361f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
9371f548804SDoug Ambrisko 		    | NGE_GPIO_GP4_OUT
9381f548804SDoug Ambrisko 		    | NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB
9391f548804SDoug Ambrisko 		    | NGE_GPIO_GP3_OUTENB
9401f548804SDoug Ambrisko 		    | NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN);
941ce4946daSBill Paul 	}
942f6bc9430SPyun YongHyeon 
943f6bc9430SPyun YongHyeon 	/*
944f6bc9430SPyun YongHyeon 	 * Do MII setup.
945f6bc9430SPyun YongHyeon 	 */
946d6c65d27SMarius Strobl 	error = mii_attach(dev, &sc->nge_miibus, ifp, nge_mediachange,
947d6c65d27SMarius Strobl 	    nge_mediastatus, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
948f6bc9430SPyun YongHyeon 	if (error != 0) {
949d6c65d27SMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
950f6bc9430SPyun YongHyeon 		goto fail;
9511f548804SDoug Ambrisko 	}
952ce4946daSBill Paul 
953ce4946daSBill Paul 	/*
954ce4946daSBill Paul 	 * Call MI attach routine.
955ce4946daSBill Paul 	 */
956673d9191SSam Leffler 	ether_ifattach(ifp, eaddr);
957ad6c618bSBill Paul 
958f6bc9430SPyun YongHyeon 	/* VLAN capability setup. */
959f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
960f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
961f6bc9430SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
962f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
963f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
964f6bc9430SPyun YongHyeon #endif
965f6bc9430SPyun YongHyeon 	/*
966f6bc9430SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
967f6bc9430SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
968f6bc9430SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
969f6bc9430SPyun YongHyeon 	 */
9701bffa951SGleb Smirnoff 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
971f6bc9430SPyun YongHyeon 
972ad6c618bSBill Paul 	/*
973ad6c618bSBill Paul 	 * Hookup IRQ last.
974ad6c618bSBill Paul 	 */
975ad6c618bSBill Paul 	error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET | INTR_MPSAFE,
976ef544f63SPaolo Pisati 	    NULL, nge_intr, sc, &sc->nge_intrhand);
977ad6c618bSBill Paul 	if (error) {
978646abee6SJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
979646abee6SJohn Baldwin 		goto fail;
980ad6c618bSBill Paul 	}
981ce4946daSBill Paul 
982646abee6SJohn Baldwin fail:
983f6bc9430SPyun YongHyeon 	if (error != 0)
984f6bc9430SPyun YongHyeon 		nge_detach(dev);
985ce4946daSBill Paul 	return (error);
986ce4946daSBill Paul }
987ce4946daSBill Paul 
988eaabec55SAlfred Perlstein static int
989284c81cbSPyun YongHyeon nge_detach(device_t dev)
990ce4946daSBill Paul {
991ce4946daSBill Paul 	struct nge_softc *sc;
992ce4946daSBill Paul 	struct ifnet *ifp;
993ce4946daSBill Paul 
994ce4946daSBill Paul 	sc = device_get_softc(dev);
995fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
996ce4946daSBill Paul 
99740929967SGleb Smirnoff #ifdef DEVICE_POLLING
998f6bc9430SPyun YongHyeon 	if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
99940929967SGleb Smirnoff 		ether_poll_deregister(ifp);
100040929967SGleb Smirnoff #endif
1001f6bc9430SPyun YongHyeon 
1002f6bc9430SPyun YongHyeon 	if (device_is_attached(dev)) {
1003ad6c618bSBill Paul 		NGE_LOCK(sc);
1004f6bc9430SPyun YongHyeon 		sc->nge_flags |= NGE_FLAG_DETACH;
1005ce4946daSBill Paul 		nge_stop(sc);
1006ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1007646abee6SJohn Baldwin 		callout_drain(&sc->nge_stat_ch);
1008f6bc9430SPyun YongHyeon 		if (ifp != NULL)
1009673d9191SSam Leffler 			ether_ifdetach(ifp);
10101f548804SDoug Ambrisko 	}
1011ce4946daSBill Paul 
1012f6bc9430SPyun YongHyeon 	if (sc->nge_miibus != NULL) {
1013f6bc9430SPyun YongHyeon 		device_delete_child(dev, sc->nge_miibus);
1014f6bc9430SPyun YongHyeon 		sc->nge_miibus = NULL;
1015f6bc9430SPyun YongHyeon 	}
1016f6bc9430SPyun YongHyeon 	bus_generic_detach(dev);
1017f6bc9430SPyun YongHyeon 	if (sc->nge_intrhand != NULL)
1018f6bc9430SPyun YongHyeon 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
1019f6bc9430SPyun YongHyeon 	if (sc->nge_irq != NULL)
1020f6bc9430SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
1021f6bc9430SPyun YongHyeon 	if (sc->nge_res != NULL)
1022f6bc9430SPyun YongHyeon 		bus_release_resource(dev, sc->nge_res_type, sc->nge_res_id,
1023f6bc9430SPyun YongHyeon 		    sc->nge_res);
1024f6bc9430SPyun YongHyeon 
1025f6bc9430SPyun YongHyeon 	nge_dma_free(sc);
1026f6bc9430SPyun YongHyeon 	if (ifp != NULL)
1027ad4f426eSWarner Losh 		if_free(ifp);
1028ce4946daSBill Paul 
10296ba160b6SBill Paul 	NGE_LOCK_DESTROY(sc);
10306ba160b6SBill Paul 
1031ce4946daSBill Paul 	return (0);
1032ce4946daSBill Paul }
1033ce4946daSBill Paul 
1034f6bc9430SPyun YongHyeon struct nge_dmamap_arg {
1035f6bc9430SPyun YongHyeon 	bus_addr_t	nge_busaddr;
1036f6bc9430SPyun YongHyeon };
1037f6bc9430SPyun YongHyeon 
1038f6bc9430SPyun YongHyeon static void
1039f6bc9430SPyun YongHyeon nge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1040f6bc9430SPyun YongHyeon {
1041f6bc9430SPyun YongHyeon 	struct nge_dmamap_arg *ctx;
1042f6bc9430SPyun YongHyeon 
1043f6bc9430SPyun YongHyeon 	if (error != 0)
1044f6bc9430SPyun YongHyeon 		return;
1045f6bc9430SPyun YongHyeon 	ctx = arg;
1046f6bc9430SPyun YongHyeon 	ctx->nge_busaddr = segs[0].ds_addr;
1047f6bc9430SPyun YongHyeon }
1048f6bc9430SPyun YongHyeon 
1049f6bc9430SPyun YongHyeon static int
1050f6bc9430SPyun YongHyeon nge_dma_alloc(struct nge_softc *sc)
1051f6bc9430SPyun YongHyeon {
1052f6bc9430SPyun YongHyeon 	struct nge_dmamap_arg ctx;
1053f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1054f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1055f6bc9430SPyun YongHyeon 	int error, i;
1056f6bc9430SPyun YongHyeon 
1057f6bc9430SPyun YongHyeon 	/* Create parent DMA tag. */
1058f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(
1059f6bc9430SPyun YongHyeon 	    bus_get_dma_tag(sc->nge_dev),	/* parent */
1060f6bc9430SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1061f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1062f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1063f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1064f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1065f6bc9430SPyun YongHyeon 	    0,				/* nsegments */
1066f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1067f6bc9430SPyun YongHyeon 	    0,				/* flags */
1068f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1069f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_parent_tag);
1070f6bc9430SPyun YongHyeon 	if (error != 0) {
1071f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create parent DMA tag\n");
1072f6bc9430SPyun YongHyeon 		goto fail;
1073f6bc9430SPyun YongHyeon 	}
1074f6bc9430SPyun YongHyeon 	/* Create tag for Tx ring. */
1075f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1076f6bc9430SPyun YongHyeon 	    NGE_RING_ALIGN, 0,		/* alignment, boundary */
1077f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1078f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1079f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1080f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE,		/* maxsize */
1081f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1082f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE,		/* maxsegsize */
1083f6bc9430SPyun YongHyeon 	    0,				/* flags */
1084f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1085f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_tx_ring_tag);
1086f6bc9430SPyun YongHyeon 	if (error != 0) {
1087f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Tx ring DMA tag\n");
1088f6bc9430SPyun YongHyeon 		goto fail;
1089f6bc9430SPyun YongHyeon 	}
1090f6bc9430SPyun YongHyeon 
1091f6bc9430SPyun YongHyeon 	/* Create tag for Rx ring. */
1092f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1093f6bc9430SPyun YongHyeon 	    NGE_RING_ALIGN, 0,		/* alignment, boundary */
1094f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1095f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1096f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1097f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE,		/* maxsize */
1098f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1099f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE,		/* maxsegsize */
1100f6bc9430SPyun YongHyeon 	    0,				/* flags */
1101f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1102f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_ring_tag);
1103f6bc9430SPyun YongHyeon 	if (error != 0) {
1104f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1105f6bc9430SPyun YongHyeon 		    "failed to create Rx ring DMA tag\n");
1106f6bc9430SPyun YongHyeon 		goto fail;
1107f6bc9430SPyun YongHyeon 	}
1108f6bc9430SPyun YongHyeon 
1109f6bc9430SPyun YongHyeon 	/* Create tag for Tx buffers. */
1110f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1111f6bc9430SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1112f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1113f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1114f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1115f6bc9430SPyun YongHyeon 	    MCLBYTES * NGE_MAXTXSEGS,	/* maxsize */
1116f6bc9430SPyun YongHyeon 	    NGE_MAXTXSEGS,		/* nsegments */
1117f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1118f6bc9430SPyun YongHyeon 	    0,				/* flags */
1119f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1120f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_tx_tag);
1121f6bc9430SPyun YongHyeon 	if (error != 0) {
1122f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Tx DMA tag\n");
1123f6bc9430SPyun YongHyeon 		goto fail;
1124f6bc9430SPyun YongHyeon 	}
1125f6bc9430SPyun YongHyeon 
1126f6bc9430SPyun YongHyeon 	/* Create tag for Rx buffers. */
1127f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1128f6bc9430SPyun YongHyeon 	    NGE_RX_ALIGN, 0,		/* alignment, boundary */
1129f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1130f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1131f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1132f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsize */
1133f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1134f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1135f6bc9430SPyun YongHyeon 	    0,				/* flags */
1136f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1137f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_tag);
1138f6bc9430SPyun YongHyeon 	if (error != 0) {
1139f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Rx DMA tag\n");
1140f6bc9430SPyun YongHyeon 		goto fail;
1141f6bc9430SPyun YongHyeon 	}
1142f6bc9430SPyun YongHyeon 
1143f6bc9430SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Tx ring. */
1144f6bc9430SPyun YongHyeon 	error = bus_dmamem_alloc(sc->nge_cdata.nge_tx_ring_tag,
1145f6bc9430SPyun YongHyeon 	    (void **)&sc->nge_rdata.nge_tx_ring, BUS_DMA_WAITOK |
1146f6bc9430SPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->nge_cdata.nge_tx_ring_map);
1147f6bc9430SPyun YongHyeon 	if (error != 0) {
1148f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1149f6bc9430SPyun YongHyeon 		    "failed to allocate DMA'able memory for Tx ring\n");
1150f6bc9430SPyun YongHyeon 		goto fail;
1151f6bc9430SPyun YongHyeon 	}
1152f6bc9430SPyun YongHyeon 
1153f6bc9430SPyun YongHyeon 	ctx.nge_busaddr = 0;
1154f6bc9430SPyun YongHyeon 	error = bus_dmamap_load(sc->nge_cdata.nge_tx_ring_tag,
1155f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map, sc->nge_rdata.nge_tx_ring,
1156f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE, nge_dmamap_cb, &ctx, 0);
1157f6bc9430SPyun YongHyeon 	if (error != 0 || ctx.nge_busaddr == 0) {
1158f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1159f6bc9430SPyun YongHyeon 		    "failed to load DMA'able memory for Tx ring\n");
1160f6bc9430SPyun YongHyeon 		goto fail;
1161f6bc9430SPyun YongHyeon 	}
1162f6bc9430SPyun YongHyeon 	sc->nge_rdata.nge_tx_ring_paddr = ctx.nge_busaddr;
1163f6bc9430SPyun YongHyeon 
1164f6bc9430SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Rx ring. */
1165f6bc9430SPyun YongHyeon 	error = bus_dmamem_alloc(sc->nge_cdata.nge_rx_ring_tag,
1166f6bc9430SPyun YongHyeon 	    (void **)&sc->nge_rdata.nge_rx_ring, BUS_DMA_WAITOK |
1167f6bc9430SPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->nge_cdata.nge_rx_ring_map);
1168f6bc9430SPyun YongHyeon 	if (error != 0) {
1169f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1170f6bc9430SPyun YongHyeon 		    "failed to allocate DMA'able memory for Rx ring\n");
1171f6bc9430SPyun YongHyeon 		goto fail;
1172f6bc9430SPyun YongHyeon 	}
1173f6bc9430SPyun YongHyeon 
1174f6bc9430SPyun YongHyeon 	ctx.nge_busaddr = 0;
1175f6bc9430SPyun YongHyeon 	error = bus_dmamap_load(sc->nge_cdata.nge_rx_ring_tag,
1176f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map, sc->nge_rdata.nge_rx_ring,
1177f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE, nge_dmamap_cb, &ctx, 0);
1178f6bc9430SPyun YongHyeon 	if (error != 0 || ctx.nge_busaddr == 0) {
1179f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1180f6bc9430SPyun YongHyeon 		    "failed to load DMA'able memory for Rx ring\n");
1181f6bc9430SPyun YongHyeon 		goto fail;
1182f6bc9430SPyun YongHyeon 	}
1183f6bc9430SPyun YongHyeon 	sc->nge_rdata.nge_rx_ring_paddr = ctx.nge_busaddr;
1184f6bc9430SPyun YongHyeon 
1185f6bc9430SPyun YongHyeon 	/* Create DMA maps for Tx buffers. */
1186f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
1187f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
1188f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1189f6bc9430SPyun YongHyeon 		txd->tx_dmamap = NULL;
1190f6bc9430SPyun YongHyeon 		error = bus_dmamap_create(sc->nge_cdata.nge_tx_tag, 0,
1191f6bc9430SPyun YongHyeon 		    &txd->tx_dmamap);
1192f6bc9430SPyun YongHyeon 		if (error != 0) {
1193f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
1194f6bc9430SPyun YongHyeon 			    "failed to create Tx dmamap\n");
1195f6bc9430SPyun YongHyeon 			goto fail;
1196f6bc9430SPyun YongHyeon 		}
1197f6bc9430SPyun YongHyeon 	}
1198f6bc9430SPyun YongHyeon 	/* Create DMA maps for Rx buffers. */
1199f6bc9430SPyun YongHyeon 	if ((error = bus_dmamap_create(sc->nge_cdata.nge_rx_tag, 0,
1200f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_sparemap)) != 0) {
1201f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1202f6bc9430SPyun YongHyeon 		    "failed to create spare Rx dmamap\n");
1203f6bc9430SPyun YongHyeon 		goto fail;
1204f6bc9430SPyun YongHyeon 	}
1205f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
1206f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[i];
1207f6bc9430SPyun YongHyeon 		rxd->rx_m = NULL;
1208f6bc9430SPyun YongHyeon 		rxd->rx_dmamap = NULL;
1209f6bc9430SPyun YongHyeon 		error = bus_dmamap_create(sc->nge_cdata.nge_rx_tag, 0,
1210f6bc9430SPyun YongHyeon 		    &rxd->rx_dmamap);
1211f6bc9430SPyun YongHyeon 		if (error != 0) {
1212f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
1213f6bc9430SPyun YongHyeon 			    "failed to create Rx dmamap\n");
1214f6bc9430SPyun YongHyeon 			goto fail;
1215f6bc9430SPyun YongHyeon 		}
1216f6bc9430SPyun YongHyeon 	}
1217f6bc9430SPyun YongHyeon 
1218f6bc9430SPyun YongHyeon fail:
1219f6bc9430SPyun YongHyeon 	return (error);
1220f6bc9430SPyun YongHyeon }
1221f6bc9430SPyun YongHyeon 
1222f6bc9430SPyun YongHyeon static void
1223f6bc9430SPyun YongHyeon nge_dma_free(struct nge_softc *sc)
1224f6bc9430SPyun YongHyeon {
1225f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1226f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1227f6bc9430SPyun YongHyeon 	int i;
1228f6bc9430SPyun YongHyeon 
1229f6bc9430SPyun YongHyeon 	/* Tx ring. */
1230f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_ring_tag) {
1231068d8643SJohn Baldwin 		if (sc->nge_rdata.nge_tx_ring_paddr)
1232f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_ring_tag,
1233f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_tx_ring_map);
1234068d8643SJohn Baldwin 		if (sc->nge_rdata.nge_tx_ring)
1235f6bc9430SPyun YongHyeon 			bus_dmamem_free(sc->nge_cdata.nge_tx_ring_tag,
1236f6bc9430SPyun YongHyeon 			    sc->nge_rdata.nge_tx_ring,
1237f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_tx_ring_map);
1238f6bc9430SPyun YongHyeon 		sc->nge_rdata.nge_tx_ring = NULL;
1239068d8643SJohn Baldwin 		sc->nge_rdata.nge_tx_ring_paddr = 0;
1240f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_tx_ring_tag);
1241f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_ring_tag = NULL;
1242f6bc9430SPyun YongHyeon 	}
1243f6bc9430SPyun YongHyeon 	/* Rx ring. */
1244f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_rx_ring_tag) {
1245068d8643SJohn Baldwin 		if (sc->nge_rdata.nge_rx_ring_paddr)
1246f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_rx_ring_tag,
1247f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_ring_map);
1248068d8643SJohn Baldwin 		if (sc->nge_rdata.nge_rx_ring)
1249f6bc9430SPyun YongHyeon 			bus_dmamem_free(sc->nge_cdata.nge_rx_ring_tag,
1250f6bc9430SPyun YongHyeon 			    sc->nge_rdata.nge_rx_ring,
1251f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_ring_map);
1252f6bc9430SPyun YongHyeon 		sc->nge_rdata.nge_rx_ring = NULL;
1253068d8643SJohn Baldwin 		sc->nge_rdata.nge_rx_ring_paddr = 0;
1254f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_rx_ring_tag);
1255f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_ring_tag = NULL;
1256f6bc9430SPyun YongHyeon 	}
1257f6bc9430SPyun YongHyeon 	/* Tx buffers. */
1258f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_tag) {
1259f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TX_RING_CNT; i++) {
1260f6bc9430SPyun YongHyeon 			txd = &sc->nge_cdata.nge_txdesc[i];
1261f6bc9430SPyun YongHyeon 			if (txd->tx_dmamap) {
1262f6bc9430SPyun YongHyeon 				bus_dmamap_destroy(sc->nge_cdata.nge_tx_tag,
1263f6bc9430SPyun YongHyeon 				    txd->tx_dmamap);
1264f6bc9430SPyun YongHyeon 				txd->tx_dmamap = NULL;
1265f6bc9430SPyun YongHyeon 			}
1266f6bc9430SPyun YongHyeon 		}
1267f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_tx_tag);
1268f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_tag = NULL;
1269f6bc9430SPyun YongHyeon 	}
1270f6bc9430SPyun YongHyeon 	/* Rx buffers. */
1271f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_rx_tag) {
1272f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_RX_RING_CNT; i++) {
1273f6bc9430SPyun YongHyeon 			rxd = &sc->nge_cdata.nge_rxdesc[i];
1274f6bc9430SPyun YongHyeon 			if (rxd->rx_dmamap) {
1275f6bc9430SPyun YongHyeon 				bus_dmamap_destroy(sc->nge_cdata.nge_rx_tag,
1276f6bc9430SPyun YongHyeon 				    rxd->rx_dmamap);
1277f6bc9430SPyun YongHyeon 				rxd->rx_dmamap = NULL;
1278f6bc9430SPyun YongHyeon 			}
1279f6bc9430SPyun YongHyeon 		}
1280f6bc9430SPyun YongHyeon 		if (sc->nge_cdata.nge_rx_sparemap) {
1281f6bc9430SPyun YongHyeon 			bus_dmamap_destroy(sc->nge_cdata.nge_rx_tag,
1282f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_sparemap);
1283f6bc9430SPyun YongHyeon 			sc->nge_cdata.nge_rx_sparemap = 0;
1284f6bc9430SPyun YongHyeon 		}
1285f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_rx_tag);
1286f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_tag = NULL;
1287f6bc9430SPyun YongHyeon 	}
1288f6bc9430SPyun YongHyeon 
1289f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_parent_tag) {
1290f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_parent_tag);
1291f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_parent_tag = NULL;
1292f6bc9430SPyun YongHyeon 	}
1293f6bc9430SPyun YongHyeon }
1294f6bc9430SPyun YongHyeon 
1295ce4946daSBill Paul /*
1296ce4946daSBill Paul  * Initialize the transmit descriptors.
1297ce4946daSBill Paul  */
1298eaabec55SAlfred Perlstein static int
1299284c81cbSPyun YongHyeon nge_list_tx_init(struct nge_softc *sc)
1300ce4946daSBill Paul {
1301f6bc9430SPyun YongHyeon 	struct nge_ring_data *rd;
1302f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1303f6bc9430SPyun YongHyeon 	bus_addr_t addr;
1304ce4946daSBill Paul 	int i;
1305ce4946daSBill Paul 
1306f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_prod = 0;
1307f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cons = 0;
1308f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cnt = 0;
1309ce4946daSBill Paul 
1310f6bc9430SPyun YongHyeon 	rd = &sc->nge_rdata;
1311f6bc9430SPyun YongHyeon 	bzero(rd->nge_tx_ring, sizeof(struct nge_desc) * NGE_TX_RING_CNT);
1312f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
1313f6bc9430SPyun YongHyeon 		if (i == NGE_TX_RING_CNT - 1)
1314f6bc9430SPyun YongHyeon 			addr = NGE_TX_RING_ADDR(sc, 0);
1315f6bc9430SPyun YongHyeon 		else
1316f6bc9430SPyun YongHyeon 			addr = NGE_TX_RING_ADDR(sc, i + 1);
1317f6bc9430SPyun YongHyeon 		rd->nge_tx_ring[i].nge_next = htole32(NGE_ADDR_LO(addr));
1318f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
1319f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1320ce4946daSBill Paul 	}
1321ce4946daSBill Paul 
1322f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
1323f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map,
1324f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1325ce4946daSBill Paul 
1326ce4946daSBill Paul 	return (0);
1327ce4946daSBill Paul }
1328ce4946daSBill Paul 
1329ce4946daSBill Paul /*
1330ce4946daSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
1331ce4946daSBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
1332ce4946daSBill Paul  * points back to the first.
1333ce4946daSBill Paul  */
1334eaabec55SAlfred Perlstein static int
1335284c81cbSPyun YongHyeon nge_list_rx_init(struct nge_softc *sc)
1336ce4946daSBill Paul {
1337f6bc9430SPyun YongHyeon 	struct nge_ring_data *rd;
1338f6bc9430SPyun YongHyeon 	bus_addr_t addr;
1339ce4946daSBill Paul 	int i;
1340ce4946daSBill Paul 
1341f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_rx_cons = 0;
1342ad6c618bSBill Paul 	sc->nge_head = sc->nge_tail = NULL;
1343ce4946daSBill Paul 
1344f6bc9430SPyun YongHyeon 	rd = &sc->nge_rdata;
1345f6bc9430SPyun YongHyeon 	bzero(rd->nge_rx_ring, sizeof(struct nge_desc) * NGE_RX_RING_CNT);
1346f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
1347f6bc9430SPyun YongHyeon 		if (nge_newbuf(sc, i) != 0)
1348f6bc9430SPyun YongHyeon 			return (ENOBUFS);
1349f6bc9430SPyun YongHyeon 		if (i == NGE_RX_RING_CNT - 1)
1350f6bc9430SPyun YongHyeon 			addr = NGE_RX_RING_ADDR(sc, 0);
1351f6bc9430SPyun YongHyeon 		else
1352f6bc9430SPyun YongHyeon 			addr = NGE_RX_RING_ADDR(sc, i + 1);
1353f6bc9430SPyun YongHyeon 		rd->nge_rx_ring[i].nge_next = htole32(NGE_ADDR_LO(addr));
1354f6bc9430SPyun YongHyeon 	}
1355f6bc9430SPyun YongHyeon 
1356f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1357f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map,
1358f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1359f6bc9430SPyun YongHyeon 
1360ce4946daSBill Paul 	return (0);
1361ce4946daSBill Paul }
1362ce4946daSBill Paul 
1363f6bc9430SPyun YongHyeon static __inline void
1364f6bc9430SPyun YongHyeon nge_discard_rxbuf(struct nge_softc *sc, int idx)
1365f6bc9430SPyun YongHyeon {
1366f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1367f6bc9430SPyun YongHyeon 
1368f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_rx_ring[idx];
1369f6bc9430SPyun YongHyeon 	desc->nge_cmdsts = htole32(MCLBYTES - sizeof(uint64_t));
1370f6bc9430SPyun YongHyeon 	desc->nge_extsts = 0;
1371f6bc9430SPyun YongHyeon }
1372f6bc9430SPyun YongHyeon 
1373ce4946daSBill Paul /*
1374ce4946daSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
1375ce4946daSBill Paul  */
1376eaabec55SAlfred Perlstein static int
1377f6bc9430SPyun YongHyeon nge_newbuf(struct nge_softc *sc, int idx)
1378ce4946daSBill Paul {
1379f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1380f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1381f6bc9430SPyun YongHyeon 	struct mbuf *m;
1382f6bc9430SPyun YongHyeon 	bus_dma_segment_t segs[1];
1383f6bc9430SPyun YongHyeon 	bus_dmamap_t map;
1384f6bc9430SPyun YongHyeon 	int nsegs;
1385ce4946daSBill Paul 
1386c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13876c772336SSam Leffler 	if (m == NULL)
1388ce4946daSBill Paul 		return (ENOBUFS);
1389ad6c618bSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
13903929ff51SPyun YongHyeon 	m_adj(m, sizeof(uint64_t));
1391ce4946daSBill Paul 
1392f6bc9430SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_rx_tag,
1393f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_sparemap, m, segs, &nsegs, 0) != 0) {
1394f6bc9430SPyun YongHyeon 		m_freem(m);
1395f6bc9430SPyun YongHyeon 		return (ENOBUFS);
1396f6bc9430SPyun YongHyeon 	}
1397f6bc9430SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1398f6bc9430SPyun YongHyeon 
1399f6bc9430SPyun YongHyeon 	rxd = &sc->nge_cdata.nge_rxdesc[idx];
1400f6bc9430SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1401f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap,
1402f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1403f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap);
1404f6bc9430SPyun YongHyeon 	}
1405f6bc9430SPyun YongHyeon 	map = rxd->rx_dmamap;
1406f6bc9430SPyun YongHyeon 	rxd->rx_dmamap = sc->nge_cdata.nge_rx_sparemap;
1407f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_rx_sparemap = map;
1408f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap,
1409f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
1410f6bc9430SPyun YongHyeon 	rxd->rx_m = m;
1411f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_rx_ring[idx];
1412f6bc9430SPyun YongHyeon 	desc->nge_ptr = htole32(NGE_ADDR_LO(segs[0].ds_addr));
1413f6bc9430SPyun YongHyeon 	desc->nge_cmdsts = htole32(segs[0].ds_len);
1414f6bc9430SPyun YongHyeon 	desc->nge_extsts = 0;
1415ce4946daSBill Paul 
1416ce4946daSBill Paul 	return (0);
1417ce4946daSBill Paul }
1418ce4946daSBill Paul 
1419f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
1420ad6c618bSBill Paul static __inline void
1421284c81cbSPyun YongHyeon nge_fixup_rx(struct mbuf *m)
1422ce4946daSBill Paul {
1423ce4946daSBill Paul 	int			i;
1424ad6c618bSBill Paul 	uint16_t		*src, *dst;
1425ce4946daSBill Paul 
1426ad6c618bSBill Paul 	src = mtod(m, uint16_t *);
1427ad6c618bSBill Paul 	dst = src - 1;
1428ce4946daSBill Paul 
1429ad6c618bSBill Paul 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1430ad6c618bSBill Paul 		*dst++ = *src++;
1431ce4946daSBill Paul 
1432ad6c618bSBill Paul 	m->m_data -= ETHER_ALIGN;
1433ce4946daSBill Paul }
1434ad6c618bSBill Paul #endif
1435ad6c618bSBill Paul 
1436ce4946daSBill Paul /*
1437ce4946daSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
1438ce4946daSBill Paul  * the higher level protocols.
1439ce4946daSBill Paul  */
144056e13f2aSAttilio Rao static int
1441284c81cbSPyun YongHyeon nge_rxeof(struct nge_softc *sc)
1442ce4946daSBill Paul {
1443ce4946daSBill Paul 	struct mbuf *m;
1444ce4946daSBill Paul 	struct ifnet *ifp;
1445ce4946daSBill Paul 	struct nge_desc *cur_rx;
1446f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
144756e13f2aSAttilio Rao 	int cons, prog, rx_npkts, total_len;
1448f6bc9430SPyun YongHyeon 	uint32_t cmdsts, extsts;
1449ce4946daSBill Paul 
1450ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1451f6bc9430SPyun YongHyeon 
1452fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1453f6bc9430SPyun YongHyeon 	cons = sc->nge_cdata.nge_rx_cons;
145456e13f2aSAttilio Rao 	rx_npkts = 0;
1455ce4946daSBill Paul 
1456f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1457f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map,
1458f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1459ce4946daSBill Paul 
1460f6bc9430SPyun YongHyeon 	for (prog = 0; prog < NGE_RX_RING_CNT &&
1461f6bc9430SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1462f6bc9430SPyun YongHyeon 	    NGE_INC(cons, NGE_RX_RING_CNT)) {
1463196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
146440929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
1465196c0df6SHidetoshi Shimokawa 			if (sc->rxcycles <= 0)
1466196c0df6SHidetoshi Shimokawa 				break;
1467196c0df6SHidetoshi Shimokawa 			sc->rxcycles--;
1468196c0df6SHidetoshi Shimokawa 		}
146940929967SGleb Smirnoff #endif
1470f6bc9430SPyun YongHyeon 		cur_rx = &sc->nge_rdata.nge_rx_ring[cons];
1471f6bc9430SPyun YongHyeon 		cmdsts = le32toh(cur_rx->nge_cmdsts);
1472f6bc9430SPyun YongHyeon 		extsts = le32toh(cur_rx->nge_extsts);
1473f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_OWN) == 0)
1474f6bc9430SPyun YongHyeon 			break;
1475f6bc9430SPyun YongHyeon 		prog++;
1476f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[cons];
1477f6bc9430SPyun YongHyeon 		m = rxd->rx_m;
1478f6bc9430SPyun YongHyeon 		total_len = cmdsts & NGE_CMDSTS_BUFLEN;
1479196c0df6SHidetoshi Shimokawa 
1480f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_MORE) != 0) {
1481f6bc9430SPyun YongHyeon 			if (nge_newbuf(sc, cons) != 0) {
1482e09fdb02SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1483f6bc9430SPyun YongHyeon 				if (sc->nge_head != NULL) {
1484f6bc9430SPyun YongHyeon 					m_freem(sc->nge_head);
1485f6bc9430SPyun YongHyeon 					sc->nge_head = sc->nge_tail = NULL;
1486f6bc9430SPyun YongHyeon 				}
1487f6bc9430SPyun YongHyeon 				nge_discard_rxbuf(sc, cons);
1488f6bc9430SPyun YongHyeon 				continue;
1489f6bc9430SPyun YongHyeon 			}
1490ad6c618bSBill Paul 			m->m_len = total_len;
1491ad6c618bSBill Paul 			if (sc->nge_head == NULL) {
1492ad6c618bSBill Paul 				m->m_pkthdr.len = total_len;
1493ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = m;
1494ad6c618bSBill Paul 			} else {
1495ad6c618bSBill Paul 				m->m_flags &= ~M_PKTHDR;
1496ad6c618bSBill Paul 				sc->nge_head->m_pkthdr.len += total_len;
1497ad6c618bSBill Paul 				sc->nge_tail->m_next = m;
1498ad6c618bSBill Paul 				sc->nge_tail = m;
1499ad6c618bSBill Paul 			}
1500ad6c618bSBill Paul 			continue;
1501ad6c618bSBill Paul 		}
1502ad6c618bSBill Paul 
1503ce4946daSBill Paul 		/*
1504ce4946daSBill Paul 		 * If an error occurs, update stats, clear the
1505ce4946daSBill Paul 		 * status word and leave the mbuf cluster in place:
1506ce4946daSBill Paul 		 * it should simply get re-used next time this descriptor
1507ce4946daSBill Paul 	 	 * comes up in the ring.
1508ce4946daSBill Paul 		 */
1509f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) {
1510f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_RXSTAT_RUNT) &&
1511f6bc9430SPyun YongHyeon 			    total_len >= (ETHER_MIN_LEN - ETHER_CRC_LEN - 4)) {
1512f6bc9430SPyun YongHyeon 				/*
1513f6bc9430SPyun YongHyeon 				 * Work-around hardware bug, accept runt frames
1514f6bc9430SPyun YongHyeon 				 * if its length is larger than or equal to 56.
1515f6bc9430SPyun YongHyeon 				 */
1516f6bc9430SPyun YongHyeon 			} else {
1517f6bc9430SPyun YongHyeon 				/*
1518f6bc9430SPyun YongHyeon 				 * Input error counters are updated by hardware.
1519f6bc9430SPyun YongHyeon 				 */
1520ad6c618bSBill Paul 				if (sc->nge_head != NULL) {
1521ad6c618bSBill Paul 					m_freem(sc->nge_head);
1522ad6c618bSBill Paul 					sc->nge_head = sc->nge_tail = NULL;
1523ad6c618bSBill Paul 				}
1524f6bc9430SPyun YongHyeon 				nge_discard_rxbuf(sc, cons);
1525ce4946daSBill Paul 				continue;
1526ce4946daSBill Paul 			}
1527f6bc9430SPyun YongHyeon 		}
1528ce4946daSBill Paul 
1529ad6c618bSBill Paul 		/* Try conjure up a replacement mbuf. */
1530ad6c618bSBill Paul 
1531f6bc9430SPyun YongHyeon 		if (nge_newbuf(sc, cons) != 0) {
1532e09fdb02SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1533ad6c618bSBill Paul 			if (sc->nge_head != NULL) {
1534ad6c618bSBill Paul 				m_freem(sc->nge_head);
1535ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = NULL;
1536ad6c618bSBill Paul 			}
1537f6bc9430SPyun YongHyeon 			nge_discard_rxbuf(sc, cons);
1538ad6c618bSBill Paul 			continue;
1539ad6c618bSBill Paul 		}
1540ad6c618bSBill Paul 
1541f6bc9430SPyun YongHyeon 		/* Chain received mbufs. */
1542ad6c618bSBill Paul 		if (sc->nge_head != NULL) {
1543ad6c618bSBill Paul 			m->m_len = total_len;
1544ad6c618bSBill Paul 			m->m_flags &= ~M_PKTHDR;
1545ad6c618bSBill Paul 			sc->nge_tail->m_next = m;
1546ad6c618bSBill Paul 			m = sc->nge_head;
1547ad6c618bSBill Paul 			m->m_pkthdr.len += total_len;
1548ad6c618bSBill Paul 			sc->nge_head = sc->nge_tail = NULL;
1549ad6c618bSBill Paul 		} else
1550ad6c618bSBill Paul 			m->m_pkthdr.len = m->m_len = total_len;
1551ad6c618bSBill Paul 
1552ce4946daSBill Paul 		/*
1553ce4946daSBill Paul 		 * Ok. NatSemi really screwed up here. This is the
1554ce4946daSBill Paul 		 * only gigE chip I know of with alignment constraints
1555ce4946daSBill Paul 		 * on receive buffers. RX buffers must be 64-bit aligned.
1556ce4946daSBill Paul 		 */
1557962315f6SBill Paul 		/*
1558962315f6SBill Paul 		 * By popular demand, ignore the alignment problems
1559f6bc9430SPyun YongHyeon 		 * on the non-strict alignment platform. The performance hit
1560962315f6SBill Paul 		 * incurred due to unaligned accesses is much smaller
1561962315f6SBill Paul 		 * than the hit produced by forcing buffer copies all
1562962315f6SBill Paul 		 * the time, especially with jumbo frames. We still
1563962315f6SBill Paul 		 * need to fix up the alignment everywhere else though.
1564962315f6SBill Paul 		 */
1565f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
1566ad6c618bSBill Paul 		nge_fixup_rx(m);
1567962315f6SBill Paul #endif
1568ad6c618bSBill Paul 		m->m_pkthdr.rcvif = ifp;
1569e09fdb02SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1570ce4946daSBill Paul 
1571f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1572ce4946daSBill Paul 			/* Do IP checksum checking. */
1573f6bc9430SPyun YongHyeon 			if ((extsts & NGE_RXEXTSTS_IPPKT) != 0)
1574ce4946daSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1575f6bc9430SPyun YongHyeon 			if ((extsts & NGE_RXEXTSTS_IPCSUMERR) == 0)
157601702579SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
15772195de46SBill Paul 			if ((extsts & NGE_RXEXTSTS_TCPPKT &&
15782195de46SBill Paul 			    !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) ||
15792195de46SBill Paul 			    (extsts & NGE_RXEXTSTS_UDPPKT &&
15802195de46SBill Paul 			    !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) {
15812195de46SBill Paul 				m->m_pkthdr.csum_flags |=
15822195de46SBill Paul 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1583c9215605SBill Paul 				m->m_pkthdr.csum_data = 0xffff;
15842195de46SBill Paul 			}
1585f6bc9430SPyun YongHyeon 		}
1586ce4946daSBill Paul 
1587ce4946daSBill Paul 		/*
1588ce4946daSBill Paul 		 * If we received a packet with a vlan tag, pass it
1589ce4946daSBill Paul 		 * to vlan_input() instead of ether_input().
1590ce4946daSBill Paul 		 */
1591f6bc9430SPyun YongHyeon 		if ((extsts & NGE_RXEXTSTS_VLANPKT) != 0 &&
1592f6bc9430SPyun YongHyeon 		    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
159378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
1594f6bc9430SPyun YongHyeon 			    bswap16(extsts & NGE_RXEXTSTS_VTCI);
159578ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1596ce4946daSBill Paul 		}
1597ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1598673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
1599ad6c618bSBill Paul 		NGE_LOCK(sc);
160056e13f2aSAttilio Rao 		rx_npkts++;
1601ce4946daSBill Paul 	}
1602ce4946daSBill Paul 
1603f6bc9430SPyun YongHyeon 	if (prog > 0) {
1604f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_cons = cons;
1605f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1606f6bc9430SPyun YongHyeon 		    sc->nge_cdata.nge_rx_ring_map,
1607f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1608f6bc9430SPyun YongHyeon 	}
160956e13f2aSAttilio Rao 	return (rx_npkts);
1610ce4946daSBill Paul }
1611ce4946daSBill Paul 
1612ce4946daSBill Paul /*
1613ce4946daSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
1614ce4946daSBill Paul  * the list buffers.
1615ce4946daSBill Paul  */
1616eaabec55SAlfred Perlstein static void
1617284c81cbSPyun YongHyeon nge_txeof(struct nge_softc *sc)
1618ce4946daSBill Paul {
16191e73ec7dSRuslan Ermilov 	struct nge_desc	*cur_tx;
1620f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1621ce4946daSBill Paul 	struct ifnet *ifp;
1622f6bc9430SPyun YongHyeon 	uint32_t cmdsts;
1623f6bc9430SPyun YongHyeon 	int cons, prod;
1624ce4946daSBill Paul 
1625ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1626fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1627ce4946daSBill Paul 
1628f6bc9430SPyun YongHyeon 	cons = sc->nge_cdata.nge_tx_cons;
1629f6bc9430SPyun YongHyeon 	prod = sc->nge_cdata.nge_tx_prod;
1630f6bc9430SPyun YongHyeon 	if (cons == prod)
1631f6bc9430SPyun YongHyeon 		return;
1632f6bc9430SPyun YongHyeon 
1633f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
1634f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map,
1635f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1636f6bc9430SPyun YongHyeon 
1637ce4946daSBill Paul 	/*
1638ce4946daSBill Paul 	 * Go through our tx list and free mbufs for those
1639ce4946daSBill Paul 	 * frames that have been transmitted.
1640ce4946daSBill Paul 	 */
1641f6bc9430SPyun YongHyeon 	for (; cons != prod; NGE_INC(cons, NGE_TX_RING_CNT)) {
1642f6bc9430SPyun YongHyeon 		cur_tx = &sc->nge_rdata.nge_tx_ring[cons];
1643f6bc9430SPyun YongHyeon 		cmdsts = le32toh(cur_tx->nge_cmdsts);
1644f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_OWN) != 0)
1645ce4946daSBill Paul 			break;
1646ce4946daSBill Paul 		sc->nge_cdata.nge_tx_cnt--;
164713f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1648f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_MORE) != 0)
1649f6bc9430SPyun YongHyeon 			continue;
1650f6bc9430SPyun YongHyeon 
1651f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[cons];
1652f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap,
1653f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_POSTWRITE);
1654f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap);
1655f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) {
1656e09fdb02SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1657f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_TXSTAT_EXCESSCOLLS) != 0)
1658e09fdb02SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1659f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_TXSTAT_OUTOFWINCOLL) != 0)
1660e09fdb02SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1661f6bc9430SPyun YongHyeon 		} else
1662e09fdb02SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1663f6bc9430SPyun YongHyeon 
1664e09fdb02SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_COLLISIONS, (cmdsts & NGE_TXSTAT_COLLCNT) >> 16);
1665f6bc9430SPyun YongHyeon 		KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!\n",
1666f6bc9430SPyun YongHyeon 		    __func__));
1667f6bc9430SPyun YongHyeon 		m_freem(txd->tx_m);
1668f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1669ce4946daSBill Paul 	}
1670ce4946daSBill Paul 
1671f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cons = cons;
1672f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_cnt == 0)
1673f6bc9430SPyun YongHyeon 		sc->nge_watchdog_timer = 0;
1674ce4946daSBill Paul }
1675ce4946daSBill Paul 
1676eaabec55SAlfred Perlstein static void
1677284c81cbSPyun YongHyeon nge_tick(void *xsc)
1678ce4946daSBill Paul {
1679ce4946daSBill Paul 	struct nge_softc *sc;
1680ad6c618bSBill Paul 	struct mii_data *mii;
1681ad6c618bSBill Paul 
1682646abee6SJohn Baldwin 	sc = xsc;
1683ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1684ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
1685ce4946daSBill Paul 	mii_tick(mii);
1686f6bc9430SPyun YongHyeon 	/*
1687f6bc9430SPyun YongHyeon 	 * For PHYs that does not reset established link, it is
1688f6bc9430SPyun YongHyeon 	 * necessary to check whether driver still have a valid
1689f6bc9430SPyun YongHyeon 	 * link(e.g link state change callback is not called).
1690f6bc9430SPyun YongHyeon 	 * Otherwise, driver think it lost link because driver
1691f6bc9430SPyun YongHyeon 	 * initialization routine clears link state flag.
1692f6bc9430SPyun YongHyeon 	 */
1693f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_LINK) == 0)
1694f6bc9430SPyun YongHyeon 		nge_miibus_statchg(sc->nge_dev);
1695f6bc9430SPyun YongHyeon 	nge_stats_update(sc);
1696f6bc9430SPyun YongHyeon 	nge_watchdog(sc);
1697ad6c618bSBill Paul 	callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc);
1698ce4946daSBill Paul }
1699ce4946daSBill Paul 
1700f6bc9430SPyun YongHyeon static void
1701f6bc9430SPyun YongHyeon nge_stats_update(struct nge_softc *sc)
1702f6bc9430SPyun YongHyeon {
1703f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
1704f6bc9430SPyun YongHyeon 	struct nge_stats now, *stats, *nstats;
1705f6bc9430SPyun YongHyeon 
1706f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
1707f6bc9430SPyun YongHyeon 
1708f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
1709f6bc9430SPyun YongHyeon 	stats = &now;
1710f6bc9430SPyun YongHyeon 	stats->rx_pkts_errs =
1711f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRPKT) & 0xFFFF;
1712f6bc9430SPyun YongHyeon 	stats->rx_crc_errs =
1713f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRFCS) & 0xFFFF;
1714f6bc9430SPyun YongHyeon 	stats->rx_fifo_oflows =
1715f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRMISSEDPKT) & 0xFFFF;
1716f6bc9430SPyun YongHyeon 	stats->rx_align_errs =
1717f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRALIGN) & 0xFFFF;
1718f6bc9430SPyun YongHyeon 	stats->rx_sym_errs =
1719f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRSYM) & 0xFFFF;
1720f6bc9430SPyun YongHyeon 	stats->rx_pkts_jumbos =
1721f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRGIANT) & 0xFFFF;
1722f6bc9430SPyun YongHyeon 	stats->rx_len_errs =
1723f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRRANGLEN) & 0xFFFF;
1724f6bc9430SPyun YongHyeon 	stats->rx_unctl_frames =
1725f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXBADOPCODE) & 0xFFFF;
1726f6bc9430SPyun YongHyeon 	stats->rx_pause =
1727f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXPAUSEPKTS) & 0xFFFF;
1728f6bc9430SPyun YongHyeon 	stats->tx_pause =
1729f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_TXPAUSEPKTS) & 0xFFFF;
1730f6bc9430SPyun YongHyeon 	stats->tx_seq_errs =
1731f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_TXERRSQE) & 0xFF;
1732f6bc9430SPyun YongHyeon 
1733f6bc9430SPyun YongHyeon 	/*
1734f6bc9430SPyun YongHyeon 	 * Since we've accept errored frames exclude Rx length errors.
1735f6bc9430SPyun YongHyeon 	 */
1736e09fdb02SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS,
1737e09fdb02SGleb Smirnoff 	    stats->rx_pkts_errs + stats->rx_crc_errs +
1738e09fdb02SGleb Smirnoff 	    stats->rx_fifo_oflows + stats->rx_sym_errs);
1739f6bc9430SPyun YongHyeon 
1740f6bc9430SPyun YongHyeon 	nstats = &sc->nge_stats;
1741f6bc9430SPyun YongHyeon 	nstats->rx_pkts_errs += stats->rx_pkts_errs;
1742f6bc9430SPyun YongHyeon 	nstats->rx_crc_errs += stats->rx_crc_errs;
1743f6bc9430SPyun YongHyeon 	nstats->rx_fifo_oflows += stats->rx_fifo_oflows;
1744f6bc9430SPyun YongHyeon 	nstats->rx_align_errs += stats->rx_align_errs;
1745f6bc9430SPyun YongHyeon 	nstats->rx_sym_errs += stats->rx_sym_errs;
1746f6bc9430SPyun YongHyeon 	nstats->rx_pkts_jumbos += stats->rx_pkts_jumbos;
1747f6bc9430SPyun YongHyeon 	nstats->rx_len_errs += stats->rx_len_errs;
1748f6bc9430SPyun YongHyeon 	nstats->rx_unctl_frames += stats->rx_unctl_frames;
1749f6bc9430SPyun YongHyeon 	nstats->rx_pause += stats->rx_pause;
1750f6bc9430SPyun YongHyeon 	nstats->tx_pause += stats->tx_pause;
1751f6bc9430SPyun YongHyeon 	nstats->tx_seq_errs += stats->tx_seq_errs;
1752f6bc9430SPyun YongHyeon }
1753f6bc9430SPyun YongHyeon 
1754196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1755196c0df6SHidetoshi Shimokawa static poll_handler_t nge_poll;
1756196c0df6SHidetoshi Shimokawa 
175756e13f2aSAttilio Rao static int
1758196c0df6SHidetoshi Shimokawa nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1759196c0df6SHidetoshi Shimokawa {
1760f6bc9430SPyun YongHyeon 	struct nge_softc *sc;
176156e13f2aSAttilio Rao 	int rx_npkts = 0;
1762f6bc9430SPyun YongHyeon 
1763f6bc9430SPyun YongHyeon 	sc = ifp->if_softc;
1764196c0df6SHidetoshi Shimokawa 
1765ad6c618bSBill Paul 	NGE_LOCK(sc);
1766f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1767ad6c618bSBill Paul 		NGE_UNLOCK(sc);
176856e13f2aSAttilio Rao 		return (rx_npkts);
1769196c0df6SHidetoshi Shimokawa 	}
1770196c0df6SHidetoshi Shimokawa 
1771196c0df6SHidetoshi Shimokawa 	/*
1772196c0df6SHidetoshi Shimokawa 	 * On the nge, reading the status register also clears it.
1773196c0df6SHidetoshi Shimokawa 	 * So before returning to intr mode we must make sure that all
1774196c0df6SHidetoshi Shimokawa 	 * possible pending sources of interrupts have been served.
1775196c0df6SHidetoshi Shimokawa 	 * In practice this means run to completion the *eof routines,
1776f6bc9430SPyun YongHyeon 	 * and then call the interrupt routine.
1777196c0df6SHidetoshi Shimokawa 	 */
1778196c0df6SHidetoshi Shimokawa 	sc->rxcycles = count;
177956e13f2aSAttilio Rao 	rx_npkts = nge_rxeof(sc);
1780196c0df6SHidetoshi Shimokawa 	nge_txeof(sc);
1781f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1782ad6c618bSBill Paul 		nge_start_locked(ifp);
1783196c0df6SHidetoshi Shimokawa 
1784196c0df6SHidetoshi Shimokawa 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
17853929ff51SPyun YongHyeon 		uint32_t	status;
1786196c0df6SHidetoshi Shimokawa 
1787196c0df6SHidetoshi Shimokawa 		/* Reading the ISR register clears all interrupts. */
1788196c0df6SHidetoshi Shimokawa 		status = CSR_READ_4(sc, NGE_ISR);
1789196c0df6SHidetoshi Shimokawa 
1790f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW)) != 0)
179156e13f2aSAttilio Rao 			rx_npkts += nge_rxeof(sc);
1792196c0df6SHidetoshi Shimokawa 
1793f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_RX_IDLE) != 0)
1794196c0df6SHidetoshi Shimokawa 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1795196c0df6SHidetoshi Shimokawa 
1796f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_SYSERR) != 0) {
1797f6bc9430SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1798ad6c618bSBill Paul 			nge_init_locked(sc);
1799196c0df6SHidetoshi Shimokawa 		}
1800196c0df6SHidetoshi Shimokawa 	}
1801ad6c618bSBill Paul 	NGE_UNLOCK(sc);
180256e13f2aSAttilio Rao 	return (rx_npkts);
1803196c0df6SHidetoshi Shimokawa }
1804196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1805196c0df6SHidetoshi Shimokawa 
1806eaabec55SAlfred Perlstein static void
1807284c81cbSPyun YongHyeon nge_intr(void *arg)
1808ce4946daSBill Paul {
1809ce4946daSBill Paul 	struct nge_softc *sc;
1810ce4946daSBill Paul 	struct ifnet *ifp;
18113929ff51SPyun YongHyeon 	uint32_t status;
1812ce4946daSBill Paul 
1813f6bc9430SPyun YongHyeon 	sc = (struct nge_softc *)arg;
1814fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1815ce4946daSBill Paul 
1816ad6c618bSBill Paul 	NGE_LOCK(sc);
1817196c0df6SHidetoshi Shimokawa 
1818f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_SUSPENDED) != 0)
1819f6bc9430SPyun YongHyeon 		goto done_locked;
1820f6bc9430SPyun YongHyeon 
1821f6bc9430SPyun YongHyeon 	/* Reading the ISR register clears all interrupts. */
1822f6bc9430SPyun YongHyeon 	status = CSR_READ_4(sc, NGE_ISR);
1823f6bc9430SPyun YongHyeon 	if (status == 0xffffffff || (status & NGE_INTRS) == 0)
1824f6bc9430SPyun YongHyeon 		goto done_locked;
1825f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
1826f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1827f6bc9430SPyun YongHyeon 		goto done_locked;
1828f6bc9430SPyun YongHyeon #endif
1829f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1830f6bc9430SPyun YongHyeon 		goto done_locked;
1831ce4946daSBill Paul 
1832ce4946daSBill Paul 	/* Disable interrupts. */
1833ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
1834ce4946daSBill Paul 
18351f548804SDoug Ambrisko 	/* Data LED on for TBI mode */
1836f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
1837f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
1838f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) | NGE_GPIO_GP3_OUT);
18391f548804SDoug Ambrisko 
1840f6bc9430SPyun YongHyeon 	for (; (status & NGE_INTRS) != 0;) {
1841f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_TX_DESC_OK | NGE_ISR_TX_ERR |
1842f6bc9430SPyun YongHyeon 		    NGE_ISR_TX_OK | NGE_ISR_TX_IDLE)) != 0)
1843ce4946daSBill Paul 			nge_txeof(sc);
1844ce4946daSBill Paul 
1845f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_RX_DESC_OK | NGE_ISR_RX_ERR |
1846f6bc9430SPyun YongHyeon 		    NGE_ISR_RX_OFLOW | NGE_ISR_RX_FIFO_OFLOW |
1847f6bc9430SPyun YongHyeon 		    NGE_ISR_RX_IDLE | NGE_ISR_RX_OK)) != 0)
1848ce4946daSBill Paul 			nge_rxeof(sc);
1849ff7ed9f7SPoul-Henning Kamp 
1850f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_RX_IDLE) != 0)
1851ff7ed9f7SPoul-Henning Kamp 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1852ff7ed9f7SPoul-Henning Kamp 
1853f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_SYSERR) != 0) {
185413f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1855ad6c618bSBill Paul 			nge_init_locked(sc);
1856ce4946daSBill Paul 		}
1857f6bc9430SPyun YongHyeon 		/* Reading the ISR register clears all interrupts. */
1858f6bc9430SPyun YongHyeon 		status = CSR_READ_4(sc, NGE_ISR);
1859ce4946daSBill Paul 	}
1860ce4946daSBill Paul 
1861ce4946daSBill Paul 	/* Re-enable interrupts. */
1862ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1863ce4946daSBill Paul 
1864f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1865ad6c618bSBill Paul 		nge_start_locked(ifp);
1866ce4946daSBill Paul 
18671f548804SDoug Ambrisko 	/* Data LED off for TBI mode */
1868f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
1869f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
1870f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT);
18711f548804SDoug Ambrisko 
1872f6bc9430SPyun YongHyeon done_locked:
1873ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1874ce4946daSBill Paul }
1875ce4946daSBill Paul 
1876ce4946daSBill Paul /*
1877ce4946daSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1878ce4946daSBill Paul  * pointers to the fragment pointers.
1879ce4946daSBill Paul  */
1880eaabec55SAlfred Perlstein static int
1881f6bc9430SPyun YongHyeon nge_encap(struct nge_softc *sc, struct mbuf **m_head)
1882ce4946daSBill Paul {
1883f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd, *txd_last;
1884f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1885ce4946daSBill Paul 	struct mbuf *m;
1886f6bc9430SPyun YongHyeon 	bus_dmamap_t map;
1887f6bc9430SPyun YongHyeon 	bus_dma_segment_t txsegs[NGE_MAXTXSEGS];
1888f6bc9430SPyun YongHyeon 	int error, i, nsegs, prod, si;
1889ce4946daSBill Paul 
1890f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
1891ce4946daSBill Paul 
1892f6bc9430SPyun YongHyeon 	m = *m_head;
1893f6bc9430SPyun YongHyeon 	prod = sc->nge_cdata.nge_tx_prod;
1894f6bc9430SPyun YongHyeon 	txd = &sc->nge_cdata.nge_txdesc[prod];
1895f6bc9430SPyun YongHyeon 	txd_last = txd;
1896f6bc9430SPyun YongHyeon 	map = txd->tx_dmamap;
1897f6bc9430SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag, map,
1898f6bc9430SPyun YongHyeon 	    *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1899f6bc9430SPyun YongHyeon 	if (error == EFBIG) {
1900c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, NGE_MAXTXSEGS);
1901f6bc9430SPyun YongHyeon 		if (m == NULL) {
1902f6bc9430SPyun YongHyeon 			m_freem(*m_head);
1903f6bc9430SPyun YongHyeon 			*m_head = NULL;
1904ce4946daSBill Paul 			return (ENOBUFS);
1905ce4946daSBill Paul 		}
1906f6bc9430SPyun YongHyeon 		*m_head = m;
1907f6bc9430SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag,
1908f6bc9430SPyun YongHyeon 		    map, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1909f6bc9430SPyun YongHyeon 		if (error != 0) {
1910f6bc9430SPyun YongHyeon 			m_freem(*m_head);
1911f6bc9430SPyun YongHyeon 			*m_head = NULL;
1912f6bc9430SPyun YongHyeon 			return (error);
1913f6bc9430SPyun YongHyeon 		}
1914f6bc9430SPyun YongHyeon 	} else if (error != 0)
1915f6bc9430SPyun YongHyeon 		return (error);
1916f6bc9430SPyun YongHyeon 	if (nsegs == 0) {
1917f6bc9430SPyun YongHyeon 		m_freem(*m_head);
1918f6bc9430SPyun YongHyeon 		*m_head = NULL;
1919f6bc9430SPyun YongHyeon 		return (EIO);
1920ce4946daSBill Paul 	}
1921ce4946daSBill Paul 
1922f6bc9430SPyun YongHyeon 	/* Check number of available descriptors. */
1923f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_cnt + nsegs >= (NGE_TX_RING_CNT - 1)) {
1924f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, map);
1925ce4946daSBill Paul 		return (ENOBUFS);
1926ce4946daSBill Paul 	}
1927ce4946daSBill Paul 
1928f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, map, BUS_DMASYNC_PREWRITE);
1929ce4946daSBill Paul 
1930f6bc9430SPyun YongHyeon 	si = prod;
1931f6bc9430SPyun YongHyeon 	for (i = 0; i < nsegs; i++) {
1932f6bc9430SPyun YongHyeon 		desc = &sc->nge_rdata.nge_tx_ring[prod];
1933f6bc9430SPyun YongHyeon 		desc->nge_ptr = htole32(NGE_ADDR_LO(txsegs[i].ds_addr));
1934f6bc9430SPyun YongHyeon 		if (i == 0)
1935f6bc9430SPyun YongHyeon 			desc->nge_cmdsts = htole32(txsegs[i].ds_len |
1936f6bc9430SPyun YongHyeon 			    NGE_CMDSTS_MORE);
1937f6bc9430SPyun YongHyeon 		else
1938f6bc9430SPyun YongHyeon 			desc->nge_cmdsts = htole32(txsegs[i].ds_len |
1939f6bc9430SPyun YongHyeon 			    NGE_CMDSTS_MORE | NGE_CMDSTS_OWN);
1940f6bc9430SPyun YongHyeon 		desc->nge_extsts = 0;
1941f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_cnt++;
1942f6bc9430SPyun YongHyeon 		NGE_INC(prod, NGE_TX_RING_CNT);
1943f6bc9430SPyun YongHyeon 	}
1944f6bc9430SPyun YongHyeon 	/* Update producer index. */
1945f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_prod = prod;
1946f6bc9430SPyun YongHyeon 
1947f6bc9430SPyun YongHyeon 	prod = (prod + NGE_TX_RING_CNT - 1) % NGE_TX_RING_CNT;
1948f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_tx_ring[prod];
1949f6bc9430SPyun YongHyeon 	/* Check if we have a VLAN tag to insert. */
1950f6bc9430SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0)
1951f6bc9430SPyun YongHyeon 		desc->nge_extsts |= htole32(NGE_TXEXTSTS_VLANPKT |
1952f6bc9430SPyun YongHyeon 		    bswap16(m->m_pkthdr.ether_vtag));
1953f6bc9430SPyun YongHyeon 	/* Set EOP on the last desciptor. */
1954f6bc9430SPyun YongHyeon 	desc->nge_cmdsts &= htole32(~NGE_CMDSTS_MORE);
1955f6bc9430SPyun YongHyeon 
1956f6bc9430SPyun YongHyeon 	/* Set checksum offload in the first descriptor. */
1957f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_tx_ring[si];
1958f6bc9430SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & NGE_CSUM_FEATURES) != 0) {
1959f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
1960f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_IPCSUM);
1961f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
1962f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_TCPCSUM);
1963f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
1964f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_UDPCSUM);
1965f6bc9430SPyun YongHyeon 	}
1966f6bc9430SPyun YongHyeon 	/* Lastly, turn the first descriptor ownership to hardware. */
1967f6bc9430SPyun YongHyeon 	desc->nge_cmdsts |= htole32(NGE_CMDSTS_OWN);
1968f6bc9430SPyun YongHyeon 
1969f6bc9430SPyun YongHyeon 	txd = &sc->nge_cdata.nge_txdesc[prod];
1970f6bc9430SPyun YongHyeon 	map = txd_last->tx_dmamap;
1971f6bc9430SPyun YongHyeon 	txd_last->tx_dmamap = txd->tx_dmamap;
1972f6bc9430SPyun YongHyeon 	txd->tx_dmamap = map;
1973f6bc9430SPyun YongHyeon 	txd->tx_m = m;
1974ce4946daSBill Paul 
1975ce4946daSBill Paul 	return (0);
1976ce4946daSBill Paul }
1977ce4946daSBill Paul 
1978ce4946daSBill Paul /*
1979ce4946daSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1980ce4946daSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1981ce4946daSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1982ce4946daSBill Paul  * physical addresses.
1983ce4946daSBill Paul  */
1984ce4946daSBill Paul 
1985eaabec55SAlfred Perlstein static void
1986284c81cbSPyun YongHyeon nge_start(struct ifnet *ifp)
1987ce4946daSBill Paul {
1988ce4946daSBill Paul 	struct nge_softc *sc;
1989ad6c618bSBill Paul 
1990ad6c618bSBill Paul 	sc = ifp->if_softc;
1991ad6c618bSBill Paul 	NGE_LOCK(sc);
1992ad6c618bSBill Paul 	nge_start_locked(ifp);
1993ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1994ad6c618bSBill Paul }
1995ad6c618bSBill Paul 
1996ad6c618bSBill Paul static void
1997284c81cbSPyun YongHyeon nge_start_locked(struct ifnet *ifp)
1998ad6c618bSBill Paul {
1999ad6c618bSBill Paul 	struct nge_softc *sc;
2000f6bc9430SPyun YongHyeon 	struct mbuf *m_head;
2001f6bc9430SPyun YongHyeon 	int enq;
2002ce4946daSBill Paul 
2003ce4946daSBill Paul 	sc = ifp->if_softc;
2004ce4946daSBill Paul 
2005f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2006f6bc9430SPyun YongHyeon 
2007f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2008f6bc9430SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->nge_flags & NGE_FLAG_LINK) == 0)
2009ce4946daSBill Paul 		return;
2010ce4946daSBill Paul 
2011f6bc9430SPyun YongHyeon 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2012f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_cnt < NGE_TX_RING_CNT - 2; ) {
2013f6bc9430SPyun YongHyeon 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2014ce4946daSBill Paul 		if (m_head == NULL)
2015ce4946daSBill Paul 			break;
2016f6bc9430SPyun YongHyeon 		/*
2017f6bc9430SPyun YongHyeon 		 * Pack the data into the transmit ring. If we
2018f6bc9430SPyun YongHyeon 		 * don't have room, set the OACTIVE flag and wait
2019f6bc9430SPyun YongHyeon 		 * for the NIC to drain the ring.
2020f6bc9430SPyun YongHyeon 		 */
2021f6bc9430SPyun YongHyeon 		if (nge_encap(sc, &m_head)) {
2022f6bc9430SPyun YongHyeon 			if (m_head == NULL)
2023f6bc9430SPyun YongHyeon 				break;
2024f6bc9430SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
202513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2026ce4946daSBill Paul 			break;
2027ce4946daSBill Paul 		}
2028ce4946daSBill Paul 
2029f6bc9430SPyun YongHyeon 		enq++;
2030ce4946daSBill Paul 		/*
2031ce4946daSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2032ce4946daSBill Paul 		 * to him.
2033ce4946daSBill Paul 		 */
203459a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
2035ce4946daSBill Paul 	}
2036ce4946daSBill Paul 
2037f6bc9430SPyun YongHyeon 	if (enq > 0) {
2038f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
2039f6bc9430SPyun YongHyeon 		    sc->nge_cdata.nge_tx_ring_map,
2040f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2041ce4946daSBill Paul 		/* Transmit */
2042ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE);
2043ce4946daSBill Paul 
2044f6bc9430SPyun YongHyeon 		/* Set a timeout in case the chip goes out to lunch. */
2045f6bc9430SPyun YongHyeon 		sc->nge_watchdog_timer = 5;
2046f6bc9430SPyun YongHyeon 	}
2047ce4946daSBill Paul }
2048ce4946daSBill Paul 
2049eaabec55SAlfred Perlstein static void
2050284c81cbSPyun YongHyeon nge_init(void *xsc)
2051ce4946daSBill Paul {
2052ce4946daSBill Paul 	struct nge_softc *sc = xsc;
2053ad6c618bSBill Paul 
2054ad6c618bSBill Paul 	NGE_LOCK(sc);
2055ad6c618bSBill Paul 	nge_init_locked(sc);
2056ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2057ad6c618bSBill Paul }
2058ad6c618bSBill Paul 
2059ad6c618bSBill Paul static void
2060284c81cbSPyun YongHyeon nge_init_locked(struct nge_softc *sc)
2061ad6c618bSBill Paul {
2062fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->nge_ifp;
2063ce4946daSBill Paul 	struct mii_data *mii;
2064f6bc9430SPyun YongHyeon 	uint8_t *eaddr;
2065f6bc9430SPyun YongHyeon 	uint32_t reg;
2066ad6c618bSBill Paul 
2067ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
2068ce4946daSBill Paul 
2069f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2070ce4946daSBill Paul 		return;
2071ce4946daSBill Paul 
2072ce4946daSBill Paul 	/*
2073ce4946daSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2074ce4946daSBill Paul 	 */
2075ce4946daSBill Paul 	nge_stop(sc);
2076ce4946daSBill Paul 
2077f6bc9430SPyun YongHyeon 	/* Reset the adapter. */
2078f6bc9430SPyun YongHyeon 	nge_reset(sc);
2079ce4946daSBill Paul 
2080f6bc9430SPyun YongHyeon 	/* Disable Rx filter prior to programming Rx filter. */
2081f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, 0);
20828c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
2083f6bc9430SPyun YongHyeon 
2084f6bc9430SPyun YongHyeon 	mii = device_get_softc(sc->nge_miibus);
2085f6bc9430SPyun YongHyeon 
2086f6bc9430SPyun YongHyeon 	/* Set MAC address. */
2087f6bc9430SPyun YongHyeon 	eaddr = IF_LLADDR(sc->nge_ifp);
2088ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0);
2089f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[1] << 8) | eaddr[0]);
2090ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1);
2091f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[3] << 8) | eaddr[2]);
2092ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2);
2093f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[5] << 8) | eaddr[4]);
2094ce4946daSBill Paul 
2095ce4946daSBill Paul 	/* Init circular RX list. */
2096ce4946daSBill Paul 	if (nge_list_rx_init(sc) == ENOBUFS) {
20976b9f5c94SGleb Smirnoff 		device_printf(sc->nge_dev, "initialization failed: no "
2098646abee6SJohn Baldwin 			"memory for rx buffers\n");
2099ce4946daSBill Paul 		nge_stop(sc);
2100ce4946daSBill Paul 		return;
2101ce4946daSBill Paul 	}
2102ce4946daSBill Paul 
2103ce4946daSBill Paul 	/*
2104ce4946daSBill Paul 	 * Init tx descriptors.
2105ce4946daSBill Paul 	 */
2106ce4946daSBill Paul 	nge_list_tx_init(sc);
2107ce4946daSBill Paul 
2108f6bc9430SPyun YongHyeon 	/* Set Rx filter. */
2109f6bc9430SPyun YongHyeon 	nge_rxfilter(sc);
2110f6bc9430SPyun YongHyeon 
2111f6bc9430SPyun YongHyeon 	/* Disable PRIQ ctl. */
2112f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_PRIOQCTL, 0);
2113f6bc9430SPyun YongHyeon 
2114f6bc9430SPyun YongHyeon 	/*
2115453130d9SPedro F. Giffuni 	 * Set pause frames parameters.
2116f6bc9430SPyun YongHyeon 	 *  Rx stat FIFO hi-threshold : 2 or more packets
2117f6bc9430SPyun YongHyeon 	 *  Rx stat FIFO lo-threshold : less than 2 packets
2118f6bc9430SPyun YongHyeon 	 *  Rx data FIFO hi-threshold : 2K or more bytes
2119f6bc9430SPyun YongHyeon 	 *  Rx data FIFO lo-threshold : less than 2K bytes
2120f6bc9430SPyun YongHyeon 	 *  pause time : (512ns * 0xffff) -> 33.55ms
2121f6bc9430SPyun YongHyeon 	 */
2122f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_PAUSECSR,
2123f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_PAUSE_ON_MCAST |
2124f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_PAUSE_ON_DA |
2125f6bc9430SPyun YongHyeon 	    ((1 << 24) & NGE_PAUSECSR_RX_STATFIFO_THR_HI) |
2126f6bc9430SPyun YongHyeon 	    ((1 << 22) & NGE_PAUSECSR_RX_STATFIFO_THR_LO) |
2127f6bc9430SPyun YongHyeon 	    ((1 << 20) & NGE_PAUSECSR_RX_DATAFIFO_THR_HI) |
2128f6bc9430SPyun YongHyeon 	    ((1 << 18) & NGE_PAUSECSR_RX_DATAFIFO_THR_LO) |
2129f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_CNT);
2130f6bc9430SPyun YongHyeon 
2131ce4946daSBill Paul 	/*
2132ce4946daSBill Paul 	 * Load the address of the RX and TX lists.
2133ce4946daSBill Paul 	 */
2134f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI,
2135f6bc9430SPyun YongHyeon 	    NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr));
2136f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO,
2137f6bc9430SPyun YongHyeon 	    NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr));
2138f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI,
2139f6bc9430SPyun YongHyeon 	    NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr));
2140f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO,
2141f6bc9430SPyun YongHyeon 	    NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr));
2142ce4946daSBill Paul 
2143f6bc9430SPyun YongHyeon 	/* Set RX configuration. */
2144ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG);
2145f6bc9430SPyun YongHyeon 
2146f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, 0);
2147ce4946daSBill Paul 	/*
2148ce4946daSBill Paul 	 * Enable hardware checksum validation for all IPv4
2149ce4946daSBill Paul 	 * packets, do not reject packets with bad checksums.
2150ce4946daSBill Paul 	 */
2151f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
2152f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB);
2153ce4946daSBill Paul 
2154ce4946daSBill Paul 	/*
21559d4fe4b2SBrooks Davis 	 * Tell the chip to detect and strip VLAN tag info from
21569d4fe4b2SBrooks Davis 	 * received frames. The tag will be provided in the extsts
21579d4fe4b2SBrooks Davis 	 * field in the RX descriptors.
2158ce4946daSBill Paul 	 */
2159f6bc9430SPyun YongHyeon 	NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_DETECT_ENB);
2160f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
2161f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_STRIP_ENB);
2162ce4946daSBill Paul 
2163f6bc9430SPyun YongHyeon 	/* Set TX configuration. */
2164ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG);
2165ce4946daSBill Paul 
2166ce4946daSBill Paul 	/*
2167ce4946daSBill Paul 	 * Enable TX IPv4 checksumming on a per-packet basis.
2168ce4946daSBill Paul 	 */
2169ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT);
2170ce4946daSBill Paul 
2171ce4946daSBill Paul 	/*
21729d4fe4b2SBrooks Davis 	 * Tell the chip to insert VLAN tags on a per-packet basis as
21739d4fe4b2SBrooks Davis 	 * dictated by the code in the frame encapsulation routine.
2174ce4946daSBill Paul 	 */
2175ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT);
2176ce4946daSBill Paul 
2177ce4946daSBill Paul 	/*
2178ce4946daSBill Paul 	 * Enable the delivery of PHY interrupts based on
217923d3a203SBill Paul 	 * link/speed/duplex status changes. Also enable the
218023d3a203SBill Paul 	 * extsts field in the DMA descriptors (needed for
218123d3a203SBill Paul 	 * TCP/IP checksum offload on transmit).
2182ce4946daSBill Paul 	 */
21832ce0498bSBill Paul 	NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD |
218423d3a203SBill Paul 	    NGE_CFG_PHYINTR_LNK | NGE_CFG_PHYINTR_DUP | NGE_CFG_EXTSTS_ENB);
2185ce4946daSBill Paul 
2186ce4946daSBill Paul 	/*
2187962315f6SBill Paul 	 * Configure interrupt holdoff (moderation). We can
2188962315f6SBill Paul 	 * have the chip delay interrupt delivery for a certain
2189962315f6SBill Paul 	 * period. Units are in 100us, and the max setting
2190962315f6SBill Paul 	 * is 25500us (0xFF x 100us). Default is a 100us holdoff.
2191962315f6SBill Paul 	 */
2192f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IHR, sc->nge_int_holdoff);
2193f6bc9430SPyun YongHyeon 
2194f6bc9430SPyun YongHyeon 	/*
2195f6bc9430SPyun YongHyeon 	 * Enable MAC statistics counters and clear.
2196f6bc9430SPyun YongHyeon 	 */
2197f6bc9430SPyun YongHyeon 	reg = CSR_READ_4(sc, NGE_MIBCTL);
2198f6bc9430SPyun YongHyeon 	reg &= ~NGE_MIBCTL_FREEZE_CNT;
2199f6bc9430SPyun YongHyeon 	reg |= NGE_MIBCTL_CLEAR_CNT;
2200f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_MIBCTL, reg);
2201962315f6SBill Paul 
2202962315f6SBill Paul 	/*
2203ce4946daSBill Paul 	 * Enable interrupts.
2204ce4946daSBill Paul 	 */
2205ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS);
2206196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
2207196c0df6SHidetoshi Shimokawa 	/*
2208196c0df6SHidetoshi Shimokawa 	 * ... only enable interrupts if we are not polling, make sure
2209196c0df6SHidetoshi Shimokawa 	 * they are off otherwise.
2210196c0df6SHidetoshi Shimokawa 	 */
2211f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
2212196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 0);
2213196c0df6SHidetoshi Shimokawa 	else
221440929967SGleb Smirnoff #endif
2215ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
2216ce4946daSBill Paul 
2217f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
2218f6bc9430SPyun YongHyeon 	mii_mediachg(mii);
2219ce4946daSBill Paul 
2220f6bc9430SPyun YongHyeon 	sc->nge_watchdog_timer = 0;
2221f6bc9430SPyun YongHyeon 	callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc);
2222ce4946daSBill Paul 
222313f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
222413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2225ce4946daSBill Paul }
2226ce4946daSBill Paul 
2227ce4946daSBill Paul /*
2228ce4946daSBill Paul  * Set media options.
2229ce4946daSBill Paul  */
2230eaabec55SAlfred Perlstein static int
2231f6bc9430SPyun YongHyeon nge_mediachange(struct ifnet *ifp)
2232646abee6SJohn Baldwin {
2233646abee6SJohn Baldwin 	struct nge_softc *sc;
2234ce4946daSBill Paul 	struct mii_data	*mii;
2235f6bc9430SPyun YongHyeon 	struct mii_softc *miisc;
2236f6bc9430SPyun YongHyeon 	int error;
2237ce4946daSBill Paul 
2238ce4946daSBill Paul 	sc = ifp->if_softc;
2239f6bc9430SPyun YongHyeon 	NGE_LOCK(sc);
2240ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
2241646abee6SJohn Baldwin 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
22423fcb7a53SMarius Strobl 		PHY_RESET(miisc);
2243f6bc9430SPyun YongHyeon 	error = mii_mediachg(mii);
2244f6bc9430SPyun YongHyeon 	NGE_UNLOCK(sc);
2245f6bc9430SPyun YongHyeon 
2246f6bc9430SPyun YongHyeon 	return (error);
2247ce4946daSBill Paul }
2248ce4946daSBill Paul 
2249ce4946daSBill Paul /*
2250ce4946daSBill Paul  * Report current media status.
2251ce4946daSBill Paul  */
2252eaabec55SAlfred Perlstein static void
2253f6bc9430SPyun YongHyeon nge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2254ce4946daSBill Paul {
2255ce4946daSBill Paul 	struct nge_softc *sc;
2256ce4946daSBill Paul 	struct mii_data *mii;
2257ce4946daSBill Paul 
2258ce4946daSBill Paul 	sc = ifp->if_softc;
2259646abee6SJohn Baldwin 	NGE_LOCK(sc);
2260ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
2261ce4946daSBill Paul 	mii_pollstat(mii);
2262ce4946daSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2263ce4946daSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
226457c81d92SPyun YongHyeon 	NGE_UNLOCK(sc);
22651f548804SDoug Ambrisko }
2266ce4946daSBill Paul 
2267eaabec55SAlfred Perlstein static int
2268284c81cbSPyun YongHyeon nge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2269ce4946daSBill Paul {
2270ce4946daSBill Paul 	struct nge_softc *sc = ifp->if_softc;
2271ce4946daSBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
2272ce4946daSBill Paul 	struct mii_data *mii;
2273f6bc9430SPyun YongHyeon 	int error = 0, mask;
2274ce4946daSBill Paul 
2275ce4946daSBill Paul 	switch (command) {
2276ce4946daSBill Paul 	case SIOCSIFMTU:
2277f6bc9430SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > NGE_JUMBO_MTU)
2278ce4946daSBill Paul 			error = EINVAL;
2279cb2f755cSBill Paul 		else {
2280646abee6SJohn Baldwin 			NGE_LOCK(sc);
2281ce4946daSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2282cb2f755cSBill Paul 			/*
2283cb2f755cSBill Paul 			 * Workaround: if the MTU is larger than
2284cb2f755cSBill Paul 			 * 8152 (TX FIFO size minus 64 minus 18), turn off
2285cb2f755cSBill Paul 			 * TX checksum offloading.
2286cb2f755cSBill Paul 			 */
2287a5820ecbSYaroslav Tykhiy 			if (ifr->ifr_mtu >= 8152) {
2288a5820ecbSYaroslav Tykhiy 				ifp->if_capenable &= ~IFCAP_TXCSUM;
2289f6bc9430SPyun YongHyeon 				ifp->if_hwassist &= ~NGE_CSUM_FEATURES;
2290a5820ecbSYaroslav Tykhiy 			} else {
2291a5820ecbSYaroslav Tykhiy 				ifp->if_capenable |= IFCAP_TXCSUM;
2292f6bc9430SPyun YongHyeon 				ifp->if_hwassist |= NGE_CSUM_FEATURES;
2293cb2f755cSBill Paul 			}
2294646abee6SJohn Baldwin 			NGE_UNLOCK(sc);
2295f6bc9430SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
2296a5820ecbSYaroslav Tykhiy 		}
2297ce4946daSBill Paul 		break;
2298ce4946daSBill Paul 	case SIOCSIFFLAGS:
2299ad6c618bSBill Paul 		NGE_LOCK(sc);
2300f6bc9430SPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2301f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2302f6bc9430SPyun YongHyeon 				if ((ifp->if_flags ^ sc->nge_if_flags) &
2303f6bc9430SPyun YongHyeon 				    (IFF_PROMISC | IFF_ALLMULTI))
2304f6bc9430SPyun YongHyeon 					nge_rxfilter(sc);
2305ce4946daSBill Paul 			} else {
2306f6bc9430SPyun YongHyeon 				if ((sc->nge_flags & NGE_FLAG_DETACH) == 0)
2307ad6c618bSBill Paul 					nge_init_locked(sc);
2308ce4946daSBill Paul 			}
2309ce4946daSBill Paul 		} else {
2310f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2311ce4946daSBill Paul 				nge_stop(sc);
2312ce4946daSBill Paul 		}
2313ce4946daSBill Paul 		sc->nge_if_flags = ifp->if_flags;
2314ad6c618bSBill Paul 		NGE_UNLOCK(sc);
2315ce4946daSBill Paul 		error = 0;
2316ce4946daSBill Paul 		break;
2317ce4946daSBill Paul 	case SIOCADDMULTI:
2318ce4946daSBill Paul 	case SIOCDELMULTI:
2319ad6c618bSBill Paul 		NGE_LOCK(sc);
2320a2621dd2SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2321f6bc9430SPyun YongHyeon 			nge_rxfilter(sc);
2322ad6c618bSBill Paul 		NGE_UNLOCK(sc);
2323ce4946daSBill Paul 		break;
2324ce4946daSBill Paul 	case SIOCGIFMEDIA:
2325ce4946daSBill Paul 	case SIOCSIFMEDIA:
2326ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
2327f6bc9430SPyun YongHyeon 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2328ce4946daSBill Paul 		break;
232937f5f239SRuslan Ermilov 	case SIOCSIFCAP:
233040929967SGleb Smirnoff 		NGE_LOCK(sc);
2331f6bc9430SPyun YongHyeon 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2332f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
2333f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_POLLING) != 0 &&
2334f6bc9430SPyun YongHyeon 		    (IFCAP_POLLING & ifp->if_capabilities) != 0) {
2335f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_POLLING;
2336f6bc9430SPyun YongHyeon 			if ((IFCAP_POLLING & ifp->if_capenable) != 0) {
2337f6bc9430SPyun YongHyeon 				error = ether_poll_register(nge_poll, ifp);
2338f6bc9430SPyun YongHyeon 				if (error != 0) {
233940929967SGleb Smirnoff 					NGE_UNLOCK(sc);
2340f6bc9430SPyun YongHyeon 					break;
234140929967SGleb Smirnoff 				}
2342f6bc9430SPyun YongHyeon 				/* Disable interrupts. */
2343f6bc9430SPyun YongHyeon 				CSR_WRITE_4(sc, NGE_IER, 0);
2344f6bc9430SPyun YongHyeon 			} else {
234540929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
234640929967SGleb Smirnoff 				/* Enable interrupts. */
234740929967SGleb Smirnoff 				CSR_WRITE_4(sc, NGE_IER, 1);
2348f6bc9430SPyun YongHyeon 			}
234940929967SGleb Smirnoff 		}
235040929967SGleb Smirnoff #endif /* DEVICE_POLLING */
2351f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
2352f6bc9430SPyun YongHyeon 		    (IFCAP_TXCSUM & ifp->if_capabilities) != 0) {
2353f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
2354f6bc9430SPyun YongHyeon 			if ((IFCAP_TXCSUM & ifp->if_capenable) != 0)
2355f6bc9430SPyun YongHyeon 				ifp->if_hwassist |= NGE_CSUM_FEATURES;
2356f6bc9430SPyun YongHyeon 			else
2357f6bc9430SPyun YongHyeon 				ifp->if_hwassist &= ~NGE_CSUM_FEATURES;
2358f6bc9430SPyun YongHyeon 		}
2359f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2360f6bc9430SPyun YongHyeon 		    (IFCAP_RXCSUM & ifp->if_capabilities) != 0)
2361f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2362f6bc9430SPyun YongHyeon 
2363f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
2364f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
2365f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
2366f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
2367f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
2368f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
2369f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
2370f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2371f6bc9430SPyun YongHyeon 		}
2372f6bc9430SPyun YongHyeon 
2373f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
2374f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
2375f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2376f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2377f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2378f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2379f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2380f6bc9430SPyun YongHyeon 				if ((ifp->if_capenable &
2381f6bc9430SPyun YongHyeon 				    IFCAP_VLAN_HWTAGGING) != 0)
2382f6bc9430SPyun YongHyeon 					NGE_SETBIT(sc,
2383f6bc9430SPyun YongHyeon 					    NGE_VLAN_IP_RXCTL,
2384f6bc9430SPyun YongHyeon 					    NGE_VIPRXCTL_TAG_STRIP_ENB);
2385f6bc9430SPyun YongHyeon 				else
2386f6bc9430SPyun YongHyeon 					NGE_CLRBIT(sc,
2387f6bc9430SPyun YongHyeon 					    NGE_VLAN_IP_RXCTL,
2388f6bc9430SPyun YongHyeon 					    NGE_VIPRXCTL_TAG_STRIP_ENB);
2389f6bc9430SPyun YongHyeon 			}
2390f6bc9430SPyun YongHyeon 		}
2391f6bc9430SPyun YongHyeon 		/*
2392f6bc9430SPyun YongHyeon 		 * Both VLAN hardware tagging and checksum offload is
2393f6bc9430SPyun YongHyeon 		 * required to do checksum offload on VLAN interface.
2394f6bc9430SPyun YongHyeon 		 */
2395f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_TXCSUM) == 0)
2396f6bc9430SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM;
2397f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
2398f6bc9430SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM;
2399f6bc9430SPyun YongHyeon 		NGE_UNLOCK(sc);
2400f6bc9430SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
240137f5f239SRuslan Ermilov 		break;
2402ce4946daSBill Paul 	default:
2403673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2404ce4946daSBill Paul 		break;
2405ce4946daSBill Paul 	}
2406ce4946daSBill Paul 
2407ce4946daSBill Paul 	return (error);
2408ce4946daSBill Paul }
2409ce4946daSBill Paul 
2410eaabec55SAlfred Perlstein static void
2411f6bc9430SPyun YongHyeon nge_watchdog(struct nge_softc *sc)
2412ce4946daSBill Paul {
2413f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2414ce4946daSBill Paul 
2415f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2416ce4946daSBill Paul 
2417f6bc9430SPyun YongHyeon 	if (sc->nge_watchdog_timer == 0 || --sc->nge_watchdog_timer)
2418f6bc9430SPyun YongHyeon 		return;
2419f6bc9430SPyun YongHyeon 
2420f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
2421e09fdb02SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
24226b9f5c94SGleb Smirnoff 	if_printf(ifp, "watchdog timeout\n");
2423ce4946daSBill Paul 
242413f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2425ad6c618bSBill Paul 	nge_init_locked(sc);
2426ce4946daSBill Paul 
2427f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2428ad6c618bSBill Paul 		nge_start_locked(ifp);
2429f6bc9430SPyun YongHyeon }
2430ad6c618bSBill Paul 
2431f6bc9430SPyun YongHyeon static int
2432f6bc9430SPyun YongHyeon nge_stop_mac(struct nge_softc *sc)
2433f6bc9430SPyun YongHyeon {
2434f6bc9430SPyun YongHyeon 	uint32_t reg;
2435f6bc9430SPyun YongHyeon 	int i;
2436f6bc9430SPyun YongHyeon 
2437f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2438f6bc9430SPyun YongHyeon 
2439f6bc9430SPyun YongHyeon 	reg = CSR_READ_4(sc, NGE_CSR);
2440f6bc9430SPyun YongHyeon 	if ((reg & (NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE)) != 0) {
2441f6bc9430SPyun YongHyeon 		reg &= ~(NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE);
2442f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_TX_DISABLE | NGE_CSR_RX_DISABLE;
2443f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
2444f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
2445f6bc9430SPyun YongHyeon 			DELAY(1);
2446f6bc9430SPyun YongHyeon 			if ((CSR_READ_4(sc, NGE_CSR) &
2447f6bc9430SPyun YongHyeon 			    (NGE_CSR_RX_ENABLE | NGE_CSR_TX_ENABLE)) == 0)
2448f6bc9430SPyun YongHyeon 				break;
2449f6bc9430SPyun YongHyeon 		}
2450f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
2451f6bc9430SPyun YongHyeon 			return (ETIMEDOUT);
2452f6bc9430SPyun YongHyeon 	}
2453f6bc9430SPyun YongHyeon 
2454f6bc9430SPyun YongHyeon 	return (0);
2455ce4946daSBill Paul }
2456ce4946daSBill Paul 
2457ce4946daSBill Paul /*
2458ce4946daSBill Paul  * Stop the adapter and free any mbufs allocated to the
2459ce4946daSBill Paul  * RX and TX lists.
2460ce4946daSBill Paul  */
2461eaabec55SAlfred Perlstein static void
2462284c81cbSPyun YongHyeon nge_stop(struct nge_softc *sc)
2463ce4946daSBill Paul {
2464f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
2465f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
24662cf2d799SPyun YongHyeon 	int i;
2467ce4946daSBill Paul 	struct ifnet *ifp;
2468ce4946daSBill Paul 
2469ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
2470fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
2471ce4946daSBill Paul 
247213f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2473f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
2474f6bc9430SPyun YongHyeon 	callout_stop(&sc->nge_stat_ch);
2475f6bc9430SPyun YongHyeon 	sc->nge_watchdog_timer = 0;
2476f6bc9430SPyun YongHyeon 
2477f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IER, 0);
2478f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IMR, 0);
2479f6bc9430SPyun YongHyeon 	if (nge_stop_mac(sc) == ETIMEDOUT)
2480f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
2481f6bc9430SPyun YongHyeon 		   "%s: unable to stop Tx/Rx MAC\n", __func__);
2482f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI, 0);
2483f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO, 0);
2484f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0);
2485f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0);
2486f6bc9430SPyun YongHyeon 	nge_stats_update(sc);
2487f6bc9430SPyun YongHyeon 	if (sc->nge_head != NULL) {
2488f6bc9430SPyun YongHyeon 		m_freem(sc->nge_head);
2489f6bc9430SPyun YongHyeon 		sc->nge_head = sc->nge_tail = NULL;
2490f6bc9430SPyun YongHyeon 	}
2491f6bc9430SPyun YongHyeon 
2492f6bc9430SPyun YongHyeon 	/*
2493f6bc9430SPyun YongHyeon 	 * Free RX and TX mbufs still in the queues.
2494f6bc9430SPyun YongHyeon 	 */
2495f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
2496f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[i];
2497f6bc9430SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2498f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_rx_tag,
2499f6bc9430SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2500f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_rx_tag,
2501f6bc9430SPyun YongHyeon 			    rxd->rx_dmamap);
2502f6bc9430SPyun YongHyeon 			m_freem(rxd->rx_m);
2503f6bc9430SPyun YongHyeon 			rxd->rx_m = NULL;
2504f6bc9430SPyun YongHyeon 		}
2505f6bc9430SPyun YongHyeon 	}
2506f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
2507f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
2508f6bc9430SPyun YongHyeon 		if (txd->tx_m != NULL) {
2509f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_tx_tag,
2510f6bc9430SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2511f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_tag,
2512f6bc9430SPyun YongHyeon 			    txd->tx_dmamap);
2513f6bc9430SPyun YongHyeon 			m_freem(txd->tx_m);
2514f6bc9430SPyun YongHyeon 			txd->tx_m = NULL;
2515f6bc9430SPyun YongHyeon 		}
2516f6bc9430SPyun YongHyeon 	}
2517f6bc9430SPyun YongHyeon }
2518f6bc9430SPyun YongHyeon 
2519f6bc9430SPyun YongHyeon /*
2520f6bc9430SPyun YongHyeon  * Before setting WOL bits, caller should have stopped Receiver.
2521f6bc9430SPyun YongHyeon  */
2522f6bc9430SPyun YongHyeon static void
2523f6bc9430SPyun YongHyeon nge_wol(struct nge_softc *sc)
2524f6bc9430SPyun YongHyeon {
2525f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2526f6bc9430SPyun YongHyeon 	uint32_t reg;
2527f6bc9430SPyun YongHyeon 	uint16_t pmstat;
2528f6bc9430SPyun YongHyeon 	int pmc;
2529f6bc9430SPyun YongHyeon 
2530f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2531f6bc9430SPyun YongHyeon 
25323b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &pmc) != 0)
2533f6bc9430SPyun YongHyeon 		return;
2534f6bc9430SPyun YongHyeon 
2535f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
2536f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) == 0) {
2537f6bc9430SPyun YongHyeon 		/* Disable WOL & disconnect CLKRUN to save power. */
2538f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_WOLCSR, 0);
2539f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CLKRUN, 0);
2540f6bc9430SPyun YongHyeon 	} else {
2541f6bc9430SPyun YongHyeon 		if (nge_stop_mac(sc) == ETIMEDOUT)
2542f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
2543f6bc9430SPyun YongHyeon 			    "%s: unable to stop Tx/Rx MAC\n", __func__);
2544f6bc9430SPyun YongHyeon 		/*
2545f6bc9430SPyun YongHyeon 		 * Make sure wake frames will be buffered in the Rx FIFO.
2546f6bc9430SPyun YongHyeon 		 * (i.e. Silent Rx mode.)
2547f6bc9430SPyun YongHyeon 		 */
2548f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0);
25498c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_RX_LISTPTR_HI, BUS_SPACE_BARRIER_WRITE);
2550f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0);
25518c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_RX_LISTPTR_LO, BUS_SPACE_BARRIER_WRITE);
2552f6bc9430SPyun YongHyeon 		/* Enable Rx again. */
2553f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
25548c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_CSR, BUS_SPACE_BARRIER_WRITE);
2555f6bc9430SPyun YongHyeon 
2556f6bc9430SPyun YongHyeon 		/* Configure WOL events. */
2557f6bc9430SPyun YongHyeon 		reg = 0;
2558f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
2559f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_UNICAST;
2560f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2561f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_MULTICAST;
2562f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2563f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_MAGICPKT;
2564f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_WOLCSR, reg);
2565f6bc9430SPyun YongHyeon 
2566f6bc9430SPyun YongHyeon 		/* Activate CLKRUN. */
2567f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CLKRUN);
2568f6bc9430SPyun YongHyeon 		reg |= NGE_CLKRUN_PMEENB | NGE_CLNRUN_CLKRUN_ENB;
2569f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CLKRUN, reg);
2570f6bc9430SPyun YongHyeon 	}
2571f6bc9430SPyun YongHyeon 
2572f6bc9430SPyun YongHyeon 	/* Request PME. */
2573f6bc9430SPyun YongHyeon 	pmstat = pci_read_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, 2);
2574f6bc9430SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2575f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
2576f6bc9430SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2577f6bc9430SPyun YongHyeon 	pci_write_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
2578ce4946daSBill Paul }
2579ce4946daSBill Paul 
2580ce4946daSBill Paul /*
2581ce4946daSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2582ce4946daSBill Paul  * get confused by errant DMAs when rebooting.
2583ce4946daSBill Paul  */
25846a087a87SPyun YongHyeon static int
2585284c81cbSPyun YongHyeon nge_shutdown(device_t dev)
2586ce4946daSBill Paul {
2587f6bc9430SPyun YongHyeon 
2588f6bc9430SPyun YongHyeon 	return (nge_suspend(dev));
2589f6bc9430SPyun YongHyeon }
2590f6bc9430SPyun YongHyeon 
2591f6bc9430SPyun YongHyeon static int
2592f6bc9430SPyun YongHyeon nge_suspend(device_t dev)
2593f6bc9430SPyun YongHyeon {
2594ce4946daSBill Paul 	struct nge_softc *sc;
2595ce4946daSBill Paul 
2596ce4946daSBill Paul 	sc = device_get_softc(dev);
2597ce4946daSBill Paul 
2598ad6c618bSBill Paul 	NGE_LOCK(sc);
2599ce4946daSBill Paul 	nge_stop(sc);
2600f6bc9430SPyun YongHyeon 	nge_wol(sc);
2601f6bc9430SPyun YongHyeon 	sc->nge_flags |= NGE_FLAG_SUSPENDED;
2602ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2603ce4946daSBill Paul 
26046a087a87SPyun YongHyeon 	return (0);
2605ce4946daSBill Paul }
2606f6bc9430SPyun YongHyeon 
2607f6bc9430SPyun YongHyeon static int
2608f6bc9430SPyun YongHyeon nge_resume(device_t dev)
2609f6bc9430SPyun YongHyeon {
2610f6bc9430SPyun YongHyeon 	struct nge_softc *sc;
2611f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2612f6bc9430SPyun YongHyeon 	uint16_t pmstat;
2613f6bc9430SPyun YongHyeon 	int pmc;
2614f6bc9430SPyun YongHyeon 
2615f6bc9430SPyun YongHyeon 	sc = device_get_softc(dev);
2616f6bc9430SPyun YongHyeon 
2617f6bc9430SPyun YongHyeon 	NGE_LOCK(sc);
2618f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
26193b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &pmc) == 0) {
2620f6bc9430SPyun YongHyeon 		/* Disable PME and clear PME status. */
2621f6bc9430SPyun YongHyeon 		pmstat = pci_read_config(sc->nge_dev,
2622f6bc9430SPyun YongHyeon 		    pmc + PCIR_POWER_STATUS, 2);
2623f6bc9430SPyun YongHyeon 		if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
2624f6bc9430SPyun YongHyeon 			pmstat &= ~PCIM_PSTAT_PMEENABLE;
2625f6bc9430SPyun YongHyeon 			pci_write_config(sc->nge_dev,
2626f6bc9430SPyun YongHyeon 			    pmc + PCIR_POWER_STATUS, pmstat, 2);
2627f6bc9430SPyun YongHyeon 		}
2628f6bc9430SPyun YongHyeon 	}
2629f6bc9430SPyun YongHyeon 	if (ifp->if_flags & IFF_UP) {
2630f6bc9430SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2631f6bc9430SPyun YongHyeon 		nge_init_locked(sc);
2632f6bc9430SPyun YongHyeon 	}
2633f6bc9430SPyun YongHyeon 
2634f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_SUSPENDED;
2635f6bc9430SPyun YongHyeon 	NGE_UNLOCK(sc);
2636f6bc9430SPyun YongHyeon 
2637f6bc9430SPyun YongHyeon 	return (0);
2638f6bc9430SPyun YongHyeon }
2639f6bc9430SPyun YongHyeon 
2640f6bc9430SPyun YongHyeon #define	NGE_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2641f6bc9430SPyun YongHyeon 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2642f6bc9430SPyun YongHyeon 
2643f6bc9430SPyun YongHyeon static void
2644f6bc9430SPyun YongHyeon nge_sysctl_node(struct nge_softc *sc)
2645f6bc9430SPyun YongHyeon {
2646f6bc9430SPyun YongHyeon 	struct sysctl_ctx_list *ctx;
2647f6bc9430SPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
2648f6bc9430SPyun YongHyeon 	struct sysctl_oid *tree;
2649f6bc9430SPyun YongHyeon 	struct nge_stats *stats;
2650f6bc9430SPyun YongHyeon 	int error;
2651f6bc9430SPyun YongHyeon 
2652f6bc9430SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->nge_dev);
2653f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->nge_dev));
2654f6bc9430SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_holdoff",
2655*7029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &sc->nge_int_holdoff,
2656*7029da5cSPawel Biernacki 	    0, sysctl_hw_nge_int_holdoff, "I", "NGE interrupt moderation");
2657f6bc9430SPyun YongHyeon 	/* Pull in device tunables. */
2658f6bc9430SPyun YongHyeon 	sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT;
2659f6bc9430SPyun YongHyeon 	error = resource_int_value(device_get_name(sc->nge_dev),
2660f6bc9430SPyun YongHyeon 	    device_get_unit(sc->nge_dev), "int_holdoff", &sc->nge_int_holdoff);
2661f6bc9430SPyun YongHyeon 	if (error == 0) {
2662f6bc9430SPyun YongHyeon 		if (sc->nge_int_holdoff < NGE_INT_HOLDOFF_MIN ||
2663f6bc9430SPyun YongHyeon 		    sc->nge_int_holdoff > NGE_INT_HOLDOFF_MAX ) {
2664f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
2665f6bc9430SPyun YongHyeon 			    "int_holdoff value out of range; "
2666f6bc9430SPyun YongHyeon 			    "using default: %d(%d us)\n",
2667f6bc9430SPyun YongHyeon 			    NGE_INT_HOLDOFF_DEFAULT,
2668f6bc9430SPyun YongHyeon 			    NGE_INT_HOLDOFF_DEFAULT * 100);
2669f6bc9430SPyun YongHyeon 			sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT;
2670f6bc9430SPyun YongHyeon 		}
2671f6bc9430SPyun YongHyeon 	}
2672f6bc9430SPyun YongHyeon 
2673f6bc9430SPyun YongHyeon 	stats = &sc->nge_stats;
2674*7029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats",
2675*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NGE statistics");
2676f6bc9430SPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
2677f6bc9430SPyun YongHyeon 
2678f6bc9430SPyun YongHyeon 	/* Rx statistics. */
2679*7029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx",
2680*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Rx MAC statistics");
2681f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
2682f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_errs",
2683f6bc9430SPyun YongHyeon 	    &stats->rx_pkts_errs,
2684f6bc9430SPyun YongHyeon 	    "Packet errors including both wire errors and FIFO overruns");
2685f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "crc_errs",
2686f6bc9430SPyun YongHyeon 	    &stats->rx_crc_errs, "CRC errors");
2687f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
2688f6bc9430SPyun YongHyeon 	    &stats->rx_fifo_oflows, "FIFO overflows");
2689f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
2690f6bc9430SPyun YongHyeon 	    &stats->rx_align_errs, "Frame alignment errors");
2691f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "sym_errs",
2692f6bc9430SPyun YongHyeon 	    &stats->rx_sym_errs, "One or more symbol errors");
2693f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_jumbos",
2694f6bc9430SPyun YongHyeon 	    &stats->rx_pkts_jumbos,
2695f6bc9430SPyun YongHyeon 	    "Packets received with length greater than 1518 bytes");
2696f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
2697f6bc9430SPyun YongHyeon 	    &stats->rx_len_errs, "In Range Length errors");
2698f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "unctl_frames",
2699f6bc9430SPyun YongHyeon 	    &stats->rx_unctl_frames, "Control frames with unsupported opcode");
2700f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pause",
2701f6bc9430SPyun YongHyeon 	    &stats->rx_pause, "Pause frames");
2702f6bc9430SPyun YongHyeon 
2703f6bc9430SPyun YongHyeon 	/* Tx statistics. */
2704*7029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx",
2705*7029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Tx MAC statistics");
2706f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
2707f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pause",
2708f6bc9430SPyun YongHyeon 	    &stats->tx_pause, "Pause frames");
2709f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "seq_errs",
2710f6bc9430SPyun YongHyeon 	    &stats->tx_seq_errs,
2711f6bc9430SPyun YongHyeon 	    "Loss of collision heartbeat during transmission");
2712f6bc9430SPyun YongHyeon }
2713f6bc9430SPyun YongHyeon 
2714f6bc9430SPyun YongHyeon #undef NGE_SYSCTL_STAT_ADD32
2715f6bc9430SPyun YongHyeon 
2716f6bc9430SPyun YongHyeon static int
2717f6bc9430SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2718f6bc9430SPyun YongHyeon {
2719f6bc9430SPyun YongHyeon 	int error, value;
2720f6bc9430SPyun YongHyeon 
2721f6bc9430SPyun YongHyeon 	if (arg1 == NULL)
2722f6bc9430SPyun YongHyeon 		return (EINVAL);
2723f6bc9430SPyun YongHyeon 	value = *(int *)arg1;
2724f6bc9430SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
2725f6bc9430SPyun YongHyeon 	if (error != 0 || req->newptr == NULL)
2726f6bc9430SPyun YongHyeon 		return (error);
2727f6bc9430SPyun YongHyeon 	if (value < low || value > high)
2728f6bc9430SPyun YongHyeon 		return (EINVAL);
2729f6bc9430SPyun YongHyeon 	*(int *)arg1 = value;
2730f6bc9430SPyun YongHyeon 
2731f6bc9430SPyun YongHyeon 	return (0);
2732f6bc9430SPyun YongHyeon }
2733f6bc9430SPyun YongHyeon 
2734f6bc9430SPyun YongHyeon static int
2735f6bc9430SPyun YongHyeon sysctl_hw_nge_int_holdoff(SYSCTL_HANDLER_ARGS)
2736f6bc9430SPyun YongHyeon {
2737f6bc9430SPyun YongHyeon 
2738f6bc9430SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, NGE_INT_HOLDOFF_MIN,
2739f6bc9430SPyun YongHyeon 	    NGE_INT_HOLDOFF_MAX));
2740f6bc9430SPyun YongHyeon }
2741