xref: /freebsd/sys/dev/nge/if_nge.c (revision e7451a6b82b3ce289161f6d60e7c650d159eba7b)
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 
243*e7451a6bSJohn Baldwin DRIVER_MODULE(nge, pci, nge_driver, 0, 0);
2443e38757dSJohn Baldwin DRIVER_MODULE(miibus, nge, miibus_driver, 0, 0);
245ce4946daSBill Paul 
246ce4946daSBill Paul #define NGE_SETBIT(sc, reg, x)				\
247ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
248ce4946daSBill Paul 		CSR_READ_4(sc, reg) | (x))
249ce4946daSBill Paul 
250ce4946daSBill Paul #define NGE_CLRBIT(sc, reg, x)				\
251ce4946daSBill Paul 	CSR_WRITE_4(sc, reg,				\
252ce4946daSBill Paul 		CSR_READ_4(sc, reg) & ~(x))
253ce4946daSBill Paul 
254ce4946daSBill Paul #define SIO_SET(x)					\
25529f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) | (x))
256ce4946daSBill Paul 
257ce4946daSBill Paul #define SIO_CLR(x)					\
25829f19445SAlfred Perlstein 	CSR_WRITE_4(sc, NGE_MEAR, CSR_READ_4(sc, NGE_MEAR) & ~(x))
259ce4946daSBill Paul 
260eaabec55SAlfred Perlstein static void
261284c81cbSPyun YongHyeon nge_delay(struct nge_softc *sc)
262ce4946daSBill Paul {
263ce4946daSBill Paul 	int idx;
264ce4946daSBill Paul 
265ce4946daSBill Paul 	for (idx = (300 / 33) + 1; idx > 0; idx--)
266ce4946daSBill Paul 		CSR_READ_4(sc, NGE_CSR);
267ce4946daSBill Paul }
268ce4946daSBill Paul 
269eaabec55SAlfred Perlstein static void
270284c81cbSPyun YongHyeon nge_eeprom_idle(struct nge_softc *sc)
271ce4946daSBill Paul {
2722cf2d799SPyun YongHyeon 	int i;
273ce4946daSBill Paul 
274ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
275ce4946daSBill Paul 	nge_delay(sc);
276ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CLK);
277ce4946daSBill Paul 	nge_delay(sc);
278ce4946daSBill Paul 
279ce4946daSBill Paul 	for (i = 0; i < 25; i++) {
280ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
281ce4946daSBill Paul 		nge_delay(sc);
282ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
283ce4946daSBill Paul 		nge_delay(sc);
284ce4946daSBill Paul 	}
285ce4946daSBill Paul 
286ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
287ce4946daSBill Paul 	nge_delay(sc);
288ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CSEL);
289ce4946daSBill Paul 	nge_delay(sc);
290ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_MEAR, 0x00000000);
291ce4946daSBill Paul }
292ce4946daSBill Paul 
293ce4946daSBill Paul /*
294ce4946daSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
295ce4946daSBill Paul  */
296eaabec55SAlfred Perlstein static void
297284c81cbSPyun YongHyeon nge_eeprom_putbyte(struct nge_softc *sc, int addr)
298ce4946daSBill Paul {
2992cf2d799SPyun YongHyeon 	int d, i;
300ce4946daSBill Paul 
301ce4946daSBill Paul 	d = addr | NGE_EECMD_READ;
302ce4946daSBill Paul 
303ce4946daSBill Paul 	/*
304ce4946daSBill Paul 	 * Feed in each bit and stobe the clock.
305ce4946daSBill Paul 	 */
306ce4946daSBill Paul 	for (i = 0x400; i; i >>= 1) {
307ce4946daSBill Paul 		if (d & i) {
308ce4946daSBill Paul 			SIO_SET(NGE_MEAR_EE_DIN);
309ce4946daSBill Paul 		} else {
310ce4946daSBill Paul 			SIO_CLR(NGE_MEAR_EE_DIN);
311ce4946daSBill Paul 		}
312ce4946daSBill Paul 		nge_delay(sc);
313ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
314ce4946daSBill Paul 		nge_delay(sc);
315ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
316ce4946daSBill Paul 		nge_delay(sc);
317ce4946daSBill Paul 	}
318ce4946daSBill Paul }
319ce4946daSBill Paul 
320ce4946daSBill Paul /*
321ce4946daSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
322ce4946daSBill Paul  */
323eaabec55SAlfred Perlstein static void
3243929ff51SPyun YongHyeon nge_eeprom_getword(struct nge_softc *sc, int addr, uint16_t *dest)
325ce4946daSBill Paul {
3262cf2d799SPyun YongHyeon 	int i;
3273929ff51SPyun YongHyeon 	uint16_t word = 0;
328ce4946daSBill Paul 
329ce4946daSBill Paul 	/* Force EEPROM to idle state. */
330ce4946daSBill Paul 	nge_eeprom_idle(sc);
331ce4946daSBill Paul 
332ce4946daSBill Paul 	/* Enter EEPROM access mode. */
333ce4946daSBill Paul 	nge_delay(sc);
334ce4946daSBill Paul 	SIO_CLR(NGE_MEAR_EE_CLK);
335ce4946daSBill Paul 	nge_delay(sc);
336ce4946daSBill Paul 	SIO_SET(NGE_MEAR_EE_CSEL);
337ce4946daSBill Paul 	nge_delay(sc);
338ce4946daSBill Paul 
339ce4946daSBill Paul 	/*
340ce4946daSBill Paul 	 * Send address of word we want to read.
341ce4946daSBill Paul 	 */
342ce4946daSBill Paul 	nge_eeprom_putbyte(sc, addr);
343ce4946daSBill Paul 
344ce4946daSBill Paul 	/*
345ce4946daSBill Paul 	 * Start reading bits from EEPROM.
346ce4946daSBill Paul 	 */
347ce4946daSBill Paul 	for (i = 0x8000; i; i >>= 1) {
348ce4946daSBill Paul 		SIO_SET(NGE_MEAR_EE_CLK);
349ce4946daSBill Paul 		nge_delay(sc);
350ce4946daSBill Paul 		if (CSR_READ_4(sc, NGE_MEAR) & NGE_MEAR_EE_DOUT)
351ce4946daSBill Paul 			word |= i;
352ce4946daSBill Paul 		nge_delay(sc);
353ce4946daSBill Paul 		SIO_CLR(NGE_MEAR_EE_CLK);
354ce4946daSBill Paul 		nge_delay(sc);
355ce4946daSBill Paul 	}
356ce4946daSBill Paul 
357ce4946daSBill Paul 	/* Turn off EEPROM access mode. */
358ce4946daSBill Paul 	nge_eeprom_idle(sc);
359ce4946daSBill Paul 
360ce4946daSBill Paul 	*dest = word;
361ce4946daSBill Paul }
362ce4946daSBill Paul 
363ce4946daSBill Paul /*
364ce4946daSBill Paul  * Read a sequence of words from the EEPROM.
365ce4946daSBill Paul  */
366eaabec55SAlfred Perlstein static void
367f6bc9430SPyun YongHyeon nge_read_eeprom(struct nge_softc *sc, caddr_t dest, int off, int cnt)
368ce4946daSBill Paul {
369ce4946daSBill Paul 	int i;
3703929ff51SPyun YongHyeon 	uint16_t word = 0, *ptr;
371ce4946daSBill Paul 
372ce4946daSBill Paul 	for (i = 0; i < cnt; i++) {
373ce4946daSBill Paul 		nge_eeprom_getword(sc, off + i, &word);
3743929ff51SPyun YongHyeon 		ptr = (uint16_t *)(dest + (i * 2));
375ce4946daSBill Paul 		*ptr = word;
376ce4946daSBill Paul 	}
377ce4946daSBill Paul }
378ce4946daSBill Paul 
379ce4946daSBill Paul /*
3808c1093fcSMarius Strobl  * Read the MII serial port for the MII bit-bang module.
3818c1093fcSMarius Strobl  */
3828c1093fcSMarius Strobl static uint32_t
3838c1093fcSMarius Strobl nge_mii_bitbang_read(device_t dev)
3848c1093fcSMarius Strobl {
3858c1093fcSMarius Strobl 	struct nge_softc *sc;
3868c1093fcSMarius Strobl 	uint32_t val;
3878c1093fcSMarius Strobl 
3888c1093fcSMarius Strobl 	sc = device_get_softc(dev);
3898c1093fcSMarius Strobl 
3908c1093fcSMarius Strobl 	val = CSR_READ_4(sc, NGE_MEAR);
3918c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_MEAR,
3928c1093fcSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
3938c1093fcSMarius Strobl 
3948c1093fcSMarius Strobl 	return (val);
3958c1093fcSMarius Strobl }
3968c1093fcSMarius Strobl 
3978c1093fcSMarius Strobl /*
3988c1093fcSMarius Strobl  * Write the MII serial port for the MII bit-bang module.
399ce4946daSBill Paul  */
400eaabec55SAlfred Perlstein static void
4018c1093fcSMarius Strobl nge_mii_bitbang_write(device_t dev, uint32_t val)
402ce4946daSBill Paul {
4038c1093fcSMarius Strobl 	struct nge_softc *sc;
404ce4946daSBill Paul 
4058c1093fcSMarius Strobl 	sc = device_get_softc(dev);
406ce4946daSBill Paul 
4078c1093fcSMarius Strobl 	CSR_WRITE_4(sc, NGE_MEAR, val);
4088c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_MEAR,
4098c1093fcSMarius Strobl 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
410ce4946daSBill Paul }
411ce4946daSBill Paul 
412eaabec55SAlfred Perlstein static int
413284c81cbSPyun YongHyeon nge_miibus_readreg(device_t dev, int phy, int reg)
414ce4946daSBill Paul {
415ce4946daSBill Paul 	struct nge_softc *sc;
416f6bc9430SPyun YongHyeon 	int rv;
417ce4946daSBill Paul 
418ce4946daSBill Paul 	sc = device_get_softc(dev);
419f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0) {
420f6bc9430SPyun YongHyeon 		/* Pretend PHY is at address 0. */
421f6bc9430SPyun YongHyeon 		if (phy != 0)
422f6bc9430SPyun YongHyeon 			return (0);
423f6bc9430SPyun YongHyeon 		switch (reg) {
424f6bc9430SPyun YongHyeon 		case MII_BMCR:
425f6bc9430SPyun YongHyeon 			reg = NGE_TBI_BMCR;
426f6bc9430SPyun YongHyeon 			break;
427f6bc9430SPyun YongHyeon 		case MII_BMSR:
428f6bc9430SPyun YongHyeon 			/* 83820/83821 has different bit layout for BMSR. */
429f6bc9430SPyun YongHyeon 			rv = BMSR_ANEG | BMSR_EXTCAP | BMSR_EXTSTAT;
430f6bc9430SPyun YongHyeon 			reg = CSR_READ_4(sc, NGE_TBI_BMSR);
431f6bc9430SPyun YongHyeon 			if ((reg & NGE_TBIBMSR_ANEG_DONE) != 0)
432f6bc9430SPyun YongHyeon 				rv |= BMSR_ACOMP;
433f6bc9430SPyun YongHyeon 			if ((reg & NGE_TBIBMSR_LINKSTAT) != 0)
434f6bc9430SPyun YongHyeon 				rv |= BMSR_LINK;
435f6bc9430SPyun YongHyeon 			return (rv);
436f6bc9430SPyun YongHyeon 		case MII_ANAR:
437f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANAR;
438f6bc9430SPyun YongHyeon 			break;
439f6bc9430SPyun YongHyeon 		case MII_ANLPAR:
440f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANLPAR;
441f6bc9430SPyun YongHyeon 			break;
442f6bc9430SPyun YongHyeon 		case MII_ANER:
443f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANER;
444f6bc9430SPyun YongHyeon 			break;
445f6bc9430SPyun YongHyeon 		case MII_EXTSR:
446f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ESR;
447f6bc9430SPyun YongHyeon 			break;
448f6bc9430SPyun YongHyeon 		case MII_PHYIDR1:
449f6bc9430SPyun YongHyeon 		case MII_PHYIDR2:
450f6bc9430SPyun YongHyeon 			return (0);
451f6bc9430SPyun YongHyeon 		default:
452f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
453f6bc9430SPyun YongHyeon 			    "bad phy register read : %d\n", reg);
454f6bc9430SPyun YongHyeon 			return (0);
455f6bc9430SPyun YongHyeon 		}
456f6bc9430SPyun YongHyeon 		return (CSR_READ_4(sc, reg));
457f6bc9430SPyun YongHyeon 	}
458ce4946daSBill Paul 
4598c1093fcSMarius Strobl 	return (mii_bitbang_readreg(dev, &nge_mii_bitbang_ops, phy, reg));
460ce4946daSBill Paul }
461ce4946daSBill Paul 
462eaabec55SAlfred Perlstein static int
463284c81cbSPyun YongHyeon nge_miibus_writereg(device_t dev, int phy, int reg, int data)
464ce4946daSBill Paul {
465ce4946daSBill Paul 	struct nge_softc *sc;
466ce4946daSBill Paul 
467ce4946daSBill Paul 	sc = device_get_softc(dev);
468f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0) {
469f6bc9430SPyun YongHyeon 		/* Pretend PHY is at address 0. */
470f6bc9430SPyun YongHyeon 		if (phy != 0)
471f6bc9430SPyun YongHyeon 			return (0);
472f6bc9430SPyun YongHyeon 		switch (reg) {
473f6bc9430SPyun YongHyeon 		case MII_BMCR:
474f6bc9430SPyun YongHyeon 			reg = NGE_TBI_BMCR;
475f6bc9430SPyun YongHyeon 			break;
476f6bc9430SPyun YongHyeon 		case MII_BMSR:
477f6bc9430SPyun YongHyeon 			return (0);
478f6bc9430SPyun YongHyeon 		case MII_ANAR:
479f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANAR;
480f6bc9430SPyun YongHyeon 			break;
481f6bc9430SPyun YongHyeon 		case MII_ANLPAR:
482f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANLPAR;
483f6bc9430SPyun YongHyeon 			break;
484f6bc9430SPyun YongHyeon 		case MII_ANER:
485f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ANER;
486f6bc9430SPyun YongHyeon 			break;
487f6bc9430SPyun YongHyeon 		case MII_EXTSR:
488f6bc9430SPyun YongHyeon 			reg = NGE_TBI_ESR;
489f6bc9430SPyun YongHyeon 			break;
490f6bc9430SPyun YongHyeon 		case MII_PHYIDR1:
491f6bc9430SPyun YongHyeon 		case MII_PHYIDR2:
492f6bc9430SPyun YongHyeon 			return (0);
493f6bc9430SPyun YongHyeon 		default:
494f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
495f6bc9430SPyun YongHyeon 			    "bad phy register write : %d\n", reg);
496f6bc9430SPyun YongHyeon 			return (0);
497f6bc9430SPyun YongHyeon 		}
498f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, reg, data);
499f6bc9430SPyun YongHyeon 		return (0);
500f6bc9430SPyun YongHyeon 	}
501ce4946daSBill Paul 
5028c1093fcSMarius Strobl 	mii_bitbang_writereg(dev, &nge_mii_bitbang_ops, phy, reg, data);
503ce4946daSBill Paul 
504ce4946daSBill Paul 	return (0);
505ce4946daSBill Paul }
506ce4946daSBill Paul 
507f6bc9430SPyun YongHyeon /*
508f6bc9430SPyun YongHyeon  * media status/link state change handler.
509f6bc9430SPyun YongHyeon  */
510eaabec55SAlfred Perlstein static void
511284c81cbSPyun YongHyeon nge_miibus_statchg(device_t dev)
512ce4946daSBill Paul {
513ce4946daSBill Paul 	struct nge_softc *sc;
514ce4946daSBill Paul 	struct mii_data *mii;
515f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
516f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
517f6bc9430SPyun YongHyeon 	uint32_t done, reg, status;
518f6bc9430SPyun YongHyeon 	int i;
519ce4946daSBill Paul 
520ce4946daSBill Paul 	sc = device_get_softc(dev);
521f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
5221f548804SDoug Ambrisko 
523ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
524f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
525f6bc9430SPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
526f6bc9430SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
527f6bc9430SPyun YongHyeon 		return;
528ce4946daSBill Paul 
529f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
530f6bc9430SPyun YongHyeon 	if ((mii->mii_media_status & (IFM_AVALID | IFM_ACTIVE)) ==
531f6bc9430SPyun YongHyeon 	    (IFM_AVALID | IFM_ACTIVE)) {
532f6bc9430SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
533f6bc9430SPyun YongHyeon 		case IFM_10_T:
534f6bc9430SPyun YongHyeon 		case IFM_100_TX:
535f6bc9430SPyun YongHyeon 		case IFM_1000_T:
536f6bc9430SPyun YongHyeon 		case IFM_1000_SX:
537f6bc9430SPyun YongHyeon 		case IFM_1000_LX:
538f6bc9430SPyun YongHyeon 		case IFM_1000_CX:
539f6bc9430SPyun YongHyeon 			sc->nge_flags |= NGE_FLAG_LINK;
540f6bc9430SPyun YongHyeon 			break;
541f6bc9430SPyun YongHyeon 		default:
542f6bc9430SPyun YongHyeon 			break;
543f6bc9430SPyun YongHyeon 		}
544f6bc9430SPyun YongHyeon 	}
545f6bc9430SPyun YongHyeon 
546f6bc9430SPyun YongHyeon 	/* Stop Tx/Rx MACs. */
547f6bc9430SPyun YongHyeon 	if (nge_stop_mac(sc) == ETIMEDOUT)
548f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
549f6bc9430SPyun YongHyeon 		    "%s: unable to stop Tx/Rx MAC\n", __func__);
550f6bc9430SPyun YongHyeon 	nge_txeof(sc);
551f6bc9430SPyun YongHyeon 	nge_rxeof(sc);
552f6bc9430SPyun YongHyeon 	if (sc->nge_head != NULL) {
553f6bc9430SPyun YongHyeon 		m_freem(sc->nge_head);
554f6bc9430SPyun YongHyeon 		sc->nge_head = sc->nge_tail = NULL;
555f6bc9430SPyun YongHyeon 	}
556f6bc9430SPyun YongHyeon 
557f6bc9430SPyun YongHyeon 	/* Release queued frames. */
558f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
559f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
560f6bc9430SPyun YongHyeon 		if (txd->tx_m != NULL) {
561f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_tx_tag,
562f6bc9430SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
563f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_tag,
564f6bc9430SPyun YongHyeon 			    txd->tx_dmamap);
565f6bc9430SPyun YongHyeon 			m_freem(txd->tx_m);
566f6bc9430SPyun YongHyeon 			txd->tx_m = NULL;
567f6bc9430SPyun YongHyeon 		}
568f6bc9430SPyun YongHyeon 	}
569f6bc9430SPyun YongHyeon 
570f6bc9430SPyun YongHyeon 	/* Program MAC with resolved speed/duplex. */
571f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_LINK) != 0) {
572f6bc9430SPyun YongHyeon 		if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
573ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_TX_CFG,
574ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT | NGE_TXCFG_IGN_CARR));
575ce4946daSBill Paul 			NGE_SETBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
576f6bc9430SPyun YongHyeon #ifdef notyet
577f6bc9430SPyun YongHyeon 			/* Enable flow-control. */
578f6bc9430SPyun YongHyeon 			if ((IFM_OPTIONS(mii->mii_media_active) &
579f6bc9430SPyun YongHyeon 			    (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) != 0)
580f6bc9430SPyun YongHyeon 				NGE_SETBIT(sc, NGE_PAUSECSR,
581f6bc9430SPyun YongHyeon 				    NGE_PAUSECSR_PAUSE_ENB);
582f6bc9430SPyun YongHyeon #endif
583ce4946daSBill Paul 		} else {
584ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_TX_CFG,
585ce4946daSBill Paul 			    (NGE_TXCFG_IGN_HBEAT | NGE_TXCFG_IGN_CARR));
586ce4946daSBill Paul 			NGE_CLRBIT(sc, NGE_RX_CFG, NGE_RXCFG_RX_FDX);
587f6bc9430SPyun YongHyeon 			NGE_CLRBIT(sc, NGE_PAUSECSR, NGE_PAUSECSR_PAUSE_ENB);
588f6bc9430SPyun YongHyeon 		}
589f6bc9430SPyun YongHyeon 		/* If we have a 1000Mbps link, set the mode_1000 bit. */
590f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CFG);
591f6bc9430SPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
592f6bc9430SPyun YongHyeon 		case IFM_1000_SX:
593f6bc9430SPyun YongHyeon 		case IFM_1000_LX:
594f6bc9430SPyun YongHyeon 		case IFM_1000_CX:
595f6bc9430SPyun YongHyeon 		case IFM_1000_T:
596f6bc9430SPyun YongHyeon 			reg |= NGE_CFG_MODE_1000;
597f6bc9430SPyun YongHyeon 			break;
598f6bc9430SPyun YongHyeon 		default:
599f6bc9430SPyun YongHyeon 			reg &= ~NGE_CFG_MODE_1000;
600f6bc9430SPyun YongHyeon 			break;
601f6bc9430SPyun YongHyeon 		}
602f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CFG, reg);
603f6bc9430SPyun YongHyeon 
604f6bc9430SPyun YongHyeon 		/* Reset Tx/Rx MAC. */
605f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CSR);
606f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_TX_RESET | NGE_CSR_RX_RESET;
607f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
608f6bc9430SPyun YongHyeon 		/* Check the completion of reset. */
609f6bc9430SPyun YongHyeon 		done = 0;
610f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
611f6bc9430SPyun YongHyeon 			DELAY(1);
612f6bc9430SPyun YongHyeon 			status = CSR_READ_4(sc, NGE_ISR);
613f6bc9430SPyun YongHyeon 			if ((status & NGE_ISR_RX_RESET_DONE) != 0)
614f6bc9430SPyun YongHyeon 				done |= NGE_ISR_RX_RESET_DONE;
615f6bc9430SPyun YongHyeon 			if ((status & NGE_ISR_TX_RESET_DONE) != 0)
616f6bc9430SPyun YongHyeon 				done |= NGE_ISR_TX_RESET_DONE;
617f6bc9430SPyun YongHyeon 			if (done ==
618f6bc9430SPyun YongHyeon 			    (NGE_ISR_TX_RESET_DONE | NGE_ISR_RX_RESET_DONE))
619f6bc9430SPyun YongHyeon 				break;
620f6bc9430SPyun YongHyeon 		}
621f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
622f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
623f6bc9430SPyun YongHyeon 			    "%s: unable to reset Tx/Rx MAC\n", __func__);
624f6bc9430SPyun YongHyeon 		/* Reuse Rx buffer and reset consumer pointer. */
625f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_cons = 0;
626f6bc9430SPyun YongHyeon 		/*
627f6bc9430SPyun YongHyeon 		 * It seems that resetting Rx/Tx MAC results in
628f6bc9430SPyun YongHyeon 		 * resetting Tx/Rx descriptor pointer registers such
629f6bc9430SPyun YongHyeon 		 * that reloading Tx/Rx lists address are needed.
630f6bc9430SPyun YongHyeon 		 */
631f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI,
632f6bc9430SPyun YongHyeon 		    NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr));
633f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO,
634f6bc9430SPyun YongHyeon 		    NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr));
635f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI,
636f6bc9430SPyun YongHyeon 		    NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr));
637f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO,
638f6bc9430SPyun YongHyeon 		    NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr));
639f6bc9430SPyun YongHyeon 		/* Reinitialize Tx buffers. */
640f6bc9430SPyun YongHyeon 		nge_list_tx_init(sc);
641f6bc9430SPyun YongHyeon 
642f6bc9430SPyun YongHyeon 		/* Restart Rx MAC. */
643f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CSR);
644f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_RX_ENABLE;
645f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
646f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
647f6bc9430SPyun YongHyeon 			if ((CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RX_ENABLE) != 0)
648f6bc9430SPyun YongHyeon 				break;
649f6bc9430SPyun YongHyeon 			DELAY(1);
650f6bc9430SPyun YongHyeon 		}
651f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
652f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
653f6bc9430SPyun YongHyeon 			    "%s: unable to restart Rx MAC\n", __func__);
654ce4946daSBill Paul 	}
655ce4946daSBill Paul 
656f6bc9430SPyun YongHyeon 	/* Data LED off for TBI mode */
657f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
658f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
659f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT);
660ce4946daSBill Paul }
661ce4946daSBill Paul 
6629bf5c8b4SGleb Smirnoff static u_int
6639bf5c8b4SGleb Smirnoff nge_write_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
6649bf5c8b4SGleb Smirnoff {
6659bf5c8b4SGleb Smirnoff 	struct nge_softc *sc = arg;
6669bf5c8b4SGleb Smirnoff 	uint32_t h;
6679bf5c8b4SGleb Smirnoff 	int bit, index;
6689bf5c8b4SGleb Smirnoff 
6699bf5c8b4SGleb Smirnoff 	/*
6709bf5c8b4SGleb Smirnoff 	 * From the 11 bits returned by the crc routine, the top 7
6719bf5c8b4SGleb Smirnoff 	 * bits represent the 16-bit word in the mcast hash table
6729bf5c8b4SGleb Smirnoff 	 * that needs to be updated, and the lower 4 bits represent
6739bf5c8b4SGleb Smirnoff 	 * which bit within that byte needs to be set.
6749bf5c8b4SGleb Smirnoff 	 */
6759bf5c8b4SGleb Smirnoff 	h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 21;
6769bf5c8b4SGleb Smirnoff 	index = (h >> 4) & 0x7F;
6779bf5c8b4SGleb Smirnoff 	bit = h & 0xF;
6789bf5c8b4SGleb Smirnoff 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + (index * 2));
6799bf5c8b4SGleb Smirnoff 	NGE_SETBIT(sc, NGE_RXFILT_DATA, (1 << bit));
6809bf5c8b4SGleb Smirnoff 
6819bf5c8b4SGleb Smirnoff 	return (1);
6829bf5c8b4SGleb Smirnoff }
6839bf5c8b4SGleb Smirnoff 
684eaabec55SAlfred Perlstein static void
685f6bc9430SPyun YongHyeon nge_rxfilter(struct nge_softc *sc)
686ce4946daSBill Paul {
687ce4946daSBill Paul 	struct ifnet *ifp;
6889bf5c8b4SGleb Smirnoff 	uint32_t i, rxfilt;
689ce4946daSBill Paul 
690ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
691fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
692ce4946daSBill Paul 
693f6bc9430SPyun YongHyeon 	/* Make sure to stop Rx filtering. */
694f6bc9430SPyun YongHyeon 	rxfilt = CSR_READ_4(sc, NGE_RXFILT_CTL);
695f6bc9430SPyun YongHyeon 	rxfilt &= ~NGE_RXFILTCTL_ENABLE;
696f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
6978c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
698f6bc9430SPyun YongHyeon 
699f6bc9430SPyun YongHyeon 	rxfilt &= ~(NGE_RXFILTCTL_ALLMULTI | NGE_RXFILTCTL_ALLPHYS);
700f6bc9430SPyun YongHyeon 	rxfilt &= ~NGE_RXFILTCTL_BROAD;
701f6bc9430SPyun YongHyeon 	/*
702f6bc9430SPyun YongHyeon 	 * We don't want to use the hash table for matching unicast
703f6bc9430SPyun YongHyeon 	 * addresses.
704f6bc9430SPyun YongHyeon 	 */
705f6bc9430SPyun YongHyeon 	rxfilt &= ~(NGE_RXFILTCTL_MCHASH | NGE_RXFILTCTL_UCHASH);
706f6bc9430SPyun YongHyeon 
707f6bc9430SPyun YongHyeon 	/*
708f6bc9430SPyun YongHyeon 	 * For the NatSemi chip, we have to explicitly enable the
709f6bc9430SPyun YongHyeon 	 * reception of ARP frames, as well as turn on the 'perfect
710f6bc9430SPyun YongHyeon 	 * match' filter where we store the station address, otherwise
711f6bc9430SPyun YongHyeon 	 * we won't receive unicasts meant for this host.
712f6bc9430SPyun YongHyeon 	 */
713f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_ARP | NGE_RXFILTCTL_PERFECT;
714f6bc9430SPyun YongHyeon 
715f6bc9430SPyun YongHyeon 	/*
716f6bc9430SPyun YongHyeon 	 * Set the capture broadcast bit to capture broadcast frames.
717f6bc9430SPyun YongHyeon 	 */
718f6bc9430SPyun YongHyeon 	if ((ifp->if_flags & IFF_BROADCAST) != 0)
719f6bc9430SPyun YongHyeon 		rxfilt |= NGE_RXFILTCTL_BROAD;
720f6bc9430SPyun YongHyeon 
721f6bc9430SPyun YongHyeon 	if ((ifp->if_flags & IFF_PROMISC) != 0 ||
722f6bc9430SPyun YongHyeon 	    (ifp->if_flags & IFF_ALLMULTI) != 0) {
723f6bc9430SPyun YongHyeon 		rxfilt |= NGE_RXFILTCTL_ALLMULTI;
724f6bc9430SPyun YongHyeon 		if ((ifp->if_flags & IFF_PROMISC) != 0)
725f6bc9430SPyun YongHyeon 			rxfilt |= NGE_RXFILTCTL_ALLPHYS;
726f6bc9430SPyun YongHyeon 		goto done;
727ce4946daSBill Paul 	}
728ce4946daSBill Paul 
729ce4946daSBill Paul 	/*
730ce4946daSBill Paul 	 * We have to explicitly enable the multicast hash table
731ce4946daSBill Paul 	 * on the NatSemi chip if we want to use it, which we do.
732ce4946daSBill Paul 	 */
733f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_MCHASH;
734ce4946daSBill Paul 
735ce4946daSBill Paul 	/* first, zot all the existing hash bits */
736ce4946daSBill Paul 	for (i = 0; i < NGE_MCAST_FILTER_LEN; i += 2) {
737ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_MCAST_LO + i);
738ce4946daSBill Paul 		CSR_WRITE_4(sc, NGE_RXFILT_DATA, 0);
739ce4946daSBill Paul 	}
740ce4946daSBill Paul 
7419bf5c8b4SGleb Smirnoff 	if_foreach_llmaddr(ifp, nge_write_maddr, sc);
742f6bc9430SPyun YongHyeon done:
743f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
744f6bc9430SPyun YongHyeon 	/* Turn the receive filter on. */
745f6bc9430SPyun YongHyeon 	rxfilt |= NGE_RXFILTCTL_ENABLE;
746f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, rxfilt);
7478c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
748ce4946daSBill Paul }
749ce4946daSBill Paul 
750eaabec55SAlfred Perlstein static void
751284c81cbSPyun YongHyeon nge_reset(struct nge_softc *sc)
752ce4946daSBill Paul {
753f6bc9430SPyun YongHyeon 	uint32_t v;
7542cf2d799SPyun YongHyeon 	int i;
755ce4946daSBill Paul 
756ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RESET);
757ce4946daSBill Paul 
758ce4946daSBill Paul 	for (i = 0; i < NGE_TIMEOUT; i++) {
759ce4946daSBill Paul 		if (!(CSR_READ_4(sc, NGE_CSR) & NGE_CSR_RESET))
760ce4946daSBill Paul 			break;
761f6bc9430SPyun YongHyeon 		DELAY(1);
762ce4946daSBill Paul 	}
763ce4946daSBill Paul 
764ce4946daSBill Paul 	if (i == NGE_TIMEOUT)
7656b9f5c94SGleb Smirnoff 		device_printf(sc->nge_dev, "reset never completed\n");
766ce4946daSBill Paul 
767ce4946daSBill Paul 	/* Wait a little while for the chip to get its brains in order. */
768ce4946daSBill Paul 	DELAY(1000);
769ce4946daSBill Paul 
770ce4946daSBill Paul 	/*
771ce4946daSBill Paul 	 * If this is a NetSemi chip, make sure to clear
772ce4946daSBill Paul 	 * PME mode.
773ce4946daSBill Paul 	 */
774ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, NGE_CLKRUN_PMESTS);
775ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_CLKRUN, 0);
776f6bc9430SPyun YongHyeon 
777f6bc9430SPyun YongHyeon 	/* Clear WOL events which may interfere normal Rx filter opertaion. */
778f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_WOLCSR, 0);
779f6bc9430SPyun YongHyeon 
780f6bc9430SPyun YongHyeon 	/*
781f6bc9430SPyun YongHyeon 	 * Only DP83820 supports 64bits addressing/data transfers and
782f6bc9430SPyun YongHyeon 	 * 64bit addressing requires different descriptor structures.
783f6bc9430SPyun YongHyeon 	 * To make it simple, disable 64bit addressing/data transfers.
784f6bc9430SPyun YongHyeon 	 */
785f6bc9430SPyun YongHyeon 	v = CSR_READ_4(sc, NGE_CFG);
786f6bc9430SPyun YongHyeon 	v &= ~(NGE_CFG_64BIT_ADDR_ENB | NGE_CFG_64BIT_DATA_ENB);
787f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_CFG, v);
788ce4946daSBill Paul }
789ce4946daSBill Paul 
790ce4946daSBill Paul /*
791d64ada50SJens Schweikhardt  * Probe for a NatSemi chip. Check the PCI vendor and device
792ce4946daSBill Paul  * IDs against our list and return a device name if we find a match.
793ce4946daSBill Paul  */
794eaabec55SAlfred Perlstein static int
795284c81cbSPyun YongHyeon nge_probe(device_t dev)
796ce4946daSBill Paul {
7978c1093fcSMarius Strobl 	const struct nge_type *t;
798ce4946daSBill Paul 
799ce4946daSBill Paul 	t = nge_devs;
800ce4946daSBill Paul 
801ce4946daSBill Paul 	while (t->nge_name != NULL) {
802ce4946daSBill Paul 		if ((pci_get_vendor(dev) == t->nge_vid) &&
803ce4946daSBill Paul 		    (pci_get_device(dev) == t->nge_did)) {
804ce4946daSBill Paul 			device_set_desc(dev, t->nge_name);
8056b9907e7SWarner Losh 			return (BUS_PROBE_DEFAULT);
806ce4946daSBill Paul 		}
807ce4946daSBill Paul 		t++;
808ce4946daSBill Paul 	}
809ce4946daSBill Paul 
810ce4946daSBill Paul 	return (ENXIO);
811ce4946daSBill Paul }
812ce4946daSBill Paul 
813ce4946daSBill Paul /*
814ce4946daSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
815ce4946daSBill Paul  * setup and ethernet/BPF attach.
816ce4946daSBill Paul  */
817eaabec55SAlfred Perlstein static int
818284c81cbSPyun YongHyeon nge_attach(device_t dev)
819ce4946daSBill Paul {
820f6bc9430SPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
821f6bc9430SPyun YongHyeon 	uint16_t ea[ETHER_ADDR_LEN/2], ea_temp, reg;
822ce4946daSBill Paul 	struct nge_softc *sc;
823f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
824f6bc9430SPyun YongHyeon 	int error, i, rid;
825ce4946daSBill Paul 
826f6bc9430SPyun YongHyeon 	error = 0;
827ce4946daSBill Paul 	sc = device_get_softc(dev);
8286b9f5c94SGleb Smirnoff 	sc->nge_dev = dev;
829ce4946daSBill Paul 
830ad6c618bSBill Paul 	NGE_LOCK_INIT(sc, device_get_nameunit(dev));
831646abee6SJohn Baldwin 	callout_init_mtx(&sc->nge_stat_ch, &sc->nge_mtx, 0);
832646abee6SJohn Baldwin 
833ce4946daSBill Paul 	/*
834ce4946daSBill Paul 	 * Map control/status registers.
835ce4946daSBill Paul 	 */
836ce4946daSBill Paul 	pci_enable_busmaster(dev);
837ce4946daSBill Paul 
838f6bc9430SPyun YongHyeon #ifdef NGE_USEIOSPACE
839f6bc9430SPyun YongHyeon 	sc->nge_res_type = SYS_RES_IOPORT;
840f6bc9430SPyun YongHyeon 	sc->nge_res_id = PCIR_BAR(0);
841f6bc9430SPyun YongHyeon #else
842f6bc9430SPyun YongHyeon 	sc->nge_res_type = SYS_RES_MEMORY;
843f6bc9430SPyun YongHyeon 	sc->nge_res_id = PCIR_BAR(1);
844f6bc9430SPyun YongHyeon #endif
845f6bc9430SPyun YongHyeon 	sc->nge_res = bus_alloc_resource_any(dev, sc->nge_res_type,
846f6bc9430SPyun YongHyeon 	    &sc->nge_res_id, RF_ACTIVE);
847ce4946daSBill Paul 
848ce4946daSBill Paul 	if (sc->nge_res == NULL) {
849f6bc9430SPyun YongHyeon 		if (sc->nge_res_type == SYS_RES_MEMORY) {
850f6bc9430SPyun YongHyeon 			sc->nge_res_type = SYS_RES_IOPORT;
851f6bc9430SPyun YongHyeon 			sc->nge_res_id = PCIR_BAR(0);
852f6bc9430SPyun YongHyeon 		} else {
853f6bc9430SPyun YongHyeon 			sc->nge_res_type = SYS_RES_MEMORY;
854f6bc9430SPyun YongHyeon 			sc->nge_res_id = PCIR_BAR(1);
855ce4946daSBill Paul 		}
856f6bc9430SPyun YongHyeon 		sc->nge_res = bus_alloc_resource_any(dev, sc->nge_res_type,
857f6bc9430SPyun YongHyeon 		    &sc->nge_res_id, RF_ACTIVE);
858f6bc9430SPyun YongHyeon 		if (sc->nge_res == NULL) {
859f6bc9430SPyun YongHyeon 			device_printf(dev, "couldn't allocate %s resources\n",
860f6bc9430SPyun YongHyeon 			    sc->nge_res_type == SYS_RES_MEMORY ? "memory" :
861f6bc9430SPyun YongHyeon 			    "I/O");
862f6bc9430SPyun YongHyeon 			NGE_LOCK_DESTROY(sc);
863f6bc9430SPyun YongHyeon 			return (ENXIO);
864f6bc9430SPyun YongHyeon 		}
865f6bc9430SPyun YongHyeon 	}
866ce4946daSBill Paul 
867ce4946daSBill Paul 	/* Allocate interrupt */
868ce4946daSBill Paul 	rid = 0;
8695f96beb9SNate Lawson 	sc->nge_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
870ce4946daSBill Paul 	    RF_SHAREABLE | RF_ACTIVE);
871ce4946daSBill Paul 
872ce4946daSBill Paul 	if (sc->nge_irq == NULL) {
873646abee6SJohn Baldwin 		device_printf(dev, "couldn't map interrupt\n");
874ce4946daSBill Paul 		error = ENXIO;
875ce4946daSBill Paul 		goto fail;
876ce4946daSBill Paul 	}
877ce4946daSBill Paul 
878f6bc9430SPyun YongHyeon 	/* Enable MWI. */
879f6bc9430SPyun YongHyeon 	reg = pci_read_config(dev, PCIR_COMMAND, 2);
880f6bc9430SPyun YongHyeon 	reg |= PCIM_CMD_MWRICEN;
881f6bc9430SPyun YongHyeon 	pci_write_config(dev, PCIR_COMMAND, reg, 2);
882f6bc9430SPyun YongHyeon 
883ce4946daSBill Paul 	/* Reset the adapter. */
884ce4946daSBill Paul 	nge_reset(sc);
885ce4946daSBill Paul 
886ce4946daSBill Paul 	/*
887ce4946daSBill Paul 	 * Get station address from the EEPROM.
888ce4946daSBill Paul 	 */
889f6bc9430SPyun YongHyeon 	nge_read_eeprom(sc, (caddr_t)ea, NGE_EE_NODEADDR, 3);
890f6bc9430SPyun YongHyeon 	for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
891f6bc9430SPyun YongHyeon 		ea[i] = le16toh(ea[i]);
892f6bc9430SPyun YongHyeon 	ea_temp = ea[0];
893f6bc9430SPyun YongHyeon 	ea[0] = ea[2];
894f6bc9430SPyun YongHyeon 	ea[2] = ea_temp;
895f6bc9430SPyun YongHyeon 	bcopy(ea, eaddr, sizeof(eaddr));
896ce4946daSBill Paul 
897f6bc9430SPyun YongHyeon 	if (nge_dma_alloc(sc) != 0) {
898ce4946daSBill Paul 		error = ENXIO;
899ce4946daSBill Paul 		goto fail;
900ce4946daSBill Paul 	}
901ce4946daSBill Paul 
902f6bc9430SPyun YongHyeon 	nge_sysctl_node(sc);
903f6bc9430SPyun YongHyeon 
904fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp = if_alloc(IFT_ETHER);
905fc74a9f9SBrooks Davis 	if (ifp == NULL) {
906f6bc9430SPyun YongHyeon 		device_printf(dev, "can not allocate ifnet structure\n");
907fc74a9f9SBrooks Davis 		error = ENOSPC;
908fc74a9f9SBrooks Davis 		goto fail;
909fc74a9f9SBrooks Davis 	}
910ce4946daSBill Paul 	ifp->if_softc = sc;
9119bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
912ad6c618bSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
913ce4946daSBill Paul 	ifp->if_ioctl = nge_ioctl;
914ce4946daSBill Paul 	ifp->if_start = nge_start;
915ce4946daSBill Paul 	ifp->if_init = nge_init;
916f6bc9430SPyun YongHyeon 	ifp->if_snd.ifq_drv_maxlen = NGE_TX_RING_CNT - 1;
917f6bc9430SPyun YongHyeon 	IFQ_SET_MAXLEN(&ifp->if_snd, ifp->if_snd.ifq_drv_maxlen);
918f6bc9430SPyun YongHyeon 	IFQ_SET_READY(&ifp->if_snd);
919ce4946daSBill Paul 	ifp->if_hwassist = NGE_CSUM_FEATURES;
920f6bc9430SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM;
921ce4946daSBill Paul 	/*
922f6bc9430SPyun YongHyeon 	 * It seems that some hardwares doesn't provide 3.3V auxiliary
923f6bc9430SPyun YongHyeon 	 * supply(3VAUX) to drive PME such that checking PCI power
924f6bc9430SPyun YongHyeon 	 * management capability is necessary.
925ce4946daSBill Paul 	 */
9263b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &i) == 0)
927f6bc9430SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
928f6bc9430SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
929f6bc9430SPyun YongHyeon 
930f6bc9430SPyun YongHyeon 	if ((CSR_READ_4(sc, NGE_CFG) & NGE_CFG_TBI_EN) != 0) {
931f6bc9430SPyun YongHyeon 		sc->nge_flags |= NGE_FLAG_TBI;
9321f548804SDoug Ambrisko 		device_printf(dev, "Using TBI\n");
933f6bc9430SPyun YongHyeon 		/* Configure GPIO. */
9341f548804SDoug Ambrisko 		CSR_WRITE_4(sc, NGE_GPIO, CSR_READ_4(sc, NGE_GPIO)
9351f548804SDoug Ambrisko 		    | NGE_GPIO_GP4_OUT
9361f548804SDoug Ambrisko 		    | NGE_GPIO_GP1_OUTENB | NGE_GPIO_GP2_OUTENB
9371f548804SDoug Ambrisko 		    | NGE_GPIO_GP3_OUTENB
9381f548804SDoug Ambrisko 		    | NGE_GPIO_GP3_IN | NGE_GPIO_GP4_IN);
939ce4946daSBill Paul 	}
940f6bc9430SPyun YongHyeon 
941f6bc9430SPyun YongHyeon 	/*
942f6bc9430SPyun YongHyeon 	 * Do MII setup.
943f6bc9430SPyun YongHyeon 	 */
944d6c65d27SMarius Strobl 	error = mii_attach(dev, &sc->nge_miibus, ifp, nge_mediachange,
945d6c65d27SMarius Strobl 	    nge_mediastatus, BMSR_DEFCAPMASK, MII_PHY_ANY, MII_OFFSET_ANY, 0);
946f6bc9430SPyun YongHyeon 	if (error != 0) {
947d6c65d27SMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
948f6bc9430SPyun YongHyeon 		goto fail;
9491f548804SDoug Ambrisko 	}
950ce4946daSBill Paul 
951ce4946daSBill Paul 	/*
952ce4946daSBill Paul 	 * Call MI attach routine.
953ce4946daSBill Paul 	 */
954673d9191SSam Leffler 	ether_ifattach(ifp, eaddr);
955ad6c618bSBill Paul 
956f6bc9430SPyun YongHyeon 	/* VLAN capability setup. */
957f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
958f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
959f6bc9430SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
960f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
961f6bc9430SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
962f6bc9430SPyun YongHyeon #endif
963f6bc9430SPyun YongHyeon 	/*
964f6bc9430SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
965f6bc9430SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
966f6bc9430SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
967f6bc9430SPyun YongHyeon 	 */
9681bffa951SGleb Smirnoff 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
969f6bc9430SPyun YongHyeon 
970ad6c618bSBill Paul 	/*
971ad6c618bSBill Paul 	 * Hookup IRQ last.
972ad6c618bSBill Paul 	 */
973ad6c618bSBill Paul 	error = bus_setup_intr(dev, sc->nge_irq, INTR_TYPE_NET | INTR_MPSAFE,
974ef544f63SPaolo Pisati 	    NULL, nge_intr, sc, &sc->nge_intrhand);
975ad6c618bSBill Paul 	if (error) {
976646abee6SJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
977646abee6SJohn Baldwin 		goto fail;
978ad6c618bSBill Paul 	}
979ce4946daSBill Paul 
980646abee6SJohn Baldwin fail:
981f6bc9430SPyun YongHyeon 	if (error != 0)
982f6bc9430SPyun YongHyeon 		nge_detach(dev);
983ce4946daSBill Paul 	return (error);
984ce4946daSBill Paul }
985ce4946daSBill Paul 
986eaabec55SAlfred Perlstein static int
987284c81cbSPyun YongHyeon nge_detach(device_t dev)
988ce4946daSBill Paul {
989ce4946daSBill Paul 	struct nge_softc *sc;
990ce4946daSBill Paul 	struct ifnet *ifp;
991ce4946daSBill Paul 
992ce4946daSBill Paul 	sc = device_get_softc(dev);
993fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
994ce4946daSBill Paul 
99540929967SGleb Smirnoff #ifdef DEVICE_POLLING
996f6bc9430SPyun YongHyeon 	if (ifp != NULL && ifp->if_capenable & IFCAP_POLLING)
99740929967SGleb Smirnoff 		ether_poll_deregister(ifp);
99840929967SGleb Smirnoff #endif
999f6bc9430SPyun YongHyeon 
1000f6bc9430SPyun YongHyeon 	if (device_is_attached(dev)) {
1001ad6c618bSBill Paul 		NGE_LOCK(sc);
1002f6bc9430SPyun YongHyeon 		sc->nge_flags |= NGE_FLAG_DETACH;
1003ce4946daSBill Paul 		nge_stop(sc);
1004ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1005646abee6SJohn Baldwin 		callout_drain(&sc->nge_stat_ch);
1006f6bc9430SPyun YongHyeon 		if (ifp != NULL)
1007673d9191SSam Leffler 			ether_ifdetach(ifp);
10081f548804SDoug Ambrisko 	}
1009ce4946daSBill Paul 
1010f6bc9430SPyun YongHyeon 	if (sc->nge_miibus != NULL) {
1011f6bc9430SPyun YongHyeon 		device_delete_child(dev, sc->nge_miibus);
1012f6bc9430SPyun YongHyeon 		sc->nge_miibus = NULL;
1013f6bc9430SPyun YongHyeon 	}
1014f6bc9430SPyun YongHyeon 	bus_generic_detach(dev);
1015f6bc9430SPyun YongHyeon 	if (sc->nge_intrhand != NULL)
1016f6bc9430SPyun YongHyeon 		bus_teardown_intr(dev, sc->nge_irq, sc->nge_intrhand);
1017f6bc9430SPyun YongHyeon 	if (sc->nge_irq != NULL)
1018f6bc9430SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ, 0, sc->nge_irq);
1019f6bc9430SPyun YongHyeon 	if (sc->nge_res != NULL)
1020f6bc9430SPyun YongHyeon 		bus_release_resource(dev, sc->nge_res_type, sc->nge_res_id,
1021f6bc9430SPyun YongHyeon 		    sc->nge_res);
1022f6bc9430SPyun YongHyeon 
1023f6bc9430SPyun YongHyeon 	nge_dma_free(sc);
1024f6bc9430SPyun YongHyeon 	if (ifp != NULL)
1025ad4f426eSWarner Losh 		if_free(ifp);
1026ce4946daSBill Paul 
10276ba160b6SBill Paul 	NGE_LOCK_DESTROY(sc);
10286ba160b6SBill Paul 
1029ce4946daSBill Paul 	return (0);
1030ce4946daSBill Paul }
1031ce4946daSBill Paul 
1032f6bc9430SPyun YongHyeon struct nge_dmamap_arg {
1033f6bc9430SPyun YongHyeon 	bus_addr_t	nge_busaddr;
1034f6bc9430SPyun YongHyeon };
1035f6bc9430SPyun YongHyeon 
1036f6bc9430SPyun YongHyeon static void
1037f6bc9430SPyun YongHyeon nge_dmamap_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1038f6bc9430SPyun YongHyeon {
1039f6bc9430SPyun YongHyeon 	struct nge_dmamap_arg *ctx;
1040f6bc9430SPyun YongHyeon 
1041f6bc9430SPyun YongHyeon 	if (error != 0)
1042f6bc9430SPyun YongHyeon 		return;
1043f6bc9430SPyun YongHyeon 	ctx = arg;
1044f6bc9430SPyun YongHyeon 	ctx->nge_busaddr = segs[0].ds_addr;
1045f6bc9430SPyun YongHyeon }
1046f6bc9430SPyun YongHyeon 
1047f6bc9430SPyun YongHyeon static int
1048f6bc9430SPyun YongHyeon nge_dma_alloc(struct nge_softc *sc)
1049f6bc9430SPyun YongHyeon {
1050f6bc9430SPyun YongHyeon 	struct nge_dmamap_arg ctx;
1051f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1052f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1053f6bc9430SPyun YongHyeon 	int error, i;
1054f6bc9430SPyun YongHyeon 
1055f6bc9430SPyun YongHyeon 	/* Create parent DMA tag. */
1056f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(
1057f6bc9430SPyun YongHyeon 	    bus_get_dma_tag(sc->nge_dev),	/* parent */
1058f6bc9430SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1059f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
1060f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1061f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1062f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
1063f6bc9430SPyun YongHyeon 	    0,				/* nsegments */
1064f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
1065f6bc9430SPyun YongHyeon 	    0,				/* flags */
1066f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1067f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_parent_tag);
1068f6bc9430SPyun YongHyeon 	if (error != 0) {
1069f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create parent DMA tag\n");
1070f6bc9430SPyun YongHyeon 		goto fail;
1071f6bc9430SPyun YongHyeon 	}
1072f6bc9430SPyun YongHyeon 	/* Create tag for Tx ring. */
1073f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1074f6bc9430SPyun YongHyeon 	    NGE_RING_ALIGN, 0,		/* alignment, boundary */
1075f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1076f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1077f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1078f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE,		/* maxsize */
1079f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1080f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE,		/* maxsegsize */
1081f6bc9430SPyun YongHyeon 	    0,				/* flags */
1082f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1083f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_tx_ring_tag);
1084f6bc9430SPyun YongHyeon 	if (error != 0) {
1085f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Tx ring DMA tag\n");
1086f6bc9430SPyun YongHyeon 		goto fail;
1087f6bc9430SPyun YongHyeon 	}
1088f6bc9430SPyun YongHyeon 
1089f6bc9430SPyun YongHyeon 	/* Create tag for Rx ring. */
1090f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1091f6bc9430SPyun YongHyeon 	    NGE_RING_ALIGN, 0,		/* alignment, boundary */
1092f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1093f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1094f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1095f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE,		/* maxsize */
1096f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1097f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE,		/* maxsegsize */
1098f6bc9430SPyun YongHyeon 	    0,				/* flags */
1099f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1100f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_ring_tag);
1101f6bc9430SPyun YongHyeon 	if (error != 0) {
1102f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1103f6bc9430SPyun YongHyeon 		    "failed to create Rx ring DMA tag\n");
1104f6bc9430SPyun YongHyeon 		goto fail;
1105f6bc9430SPyun YongHyeon 	}
1106f6bc9430SPyun YongHyeon 
1107f6bc9430SPyun YongHyeon 	/* Create tag for Tx buffers. */
1108f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1109f6bc9430SPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1110f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1111f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1112f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1113f6bc9430SPyun YongHyeon 	    MCLBYTES * NGE_MAXTXSEGS,	/* maxsize */
1114f6bc9430SPyun YongHyeon 	    NGE_MAXTXSEGS,		/* nsegments */
1115f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1116f6bc9430SPyun YongHyeon 	    0,				/* flags */
1117f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1118f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_tx_tag);
1119f6bc9430SPyun YongHyeon 	if (error != 0) {
1120f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Tx DMA tag\n");
1121f6bc9430SPyun YongHyeon 		goto fail;
1122f6bc9430SPyun YongHyeon 	}
1123f6bc9430SPyun YongHyeon 
1124f6bc9430SPyun YongHyeon 	/* Create tag for Rx buffers. */
1125f6bc9430SPyun YongHyeon 	error = bus_dma_tag_create(sc->nge_cdata.nge_parent_tag,/* parent */
1126f6bc9430SPyun YongHyeon 	    NGE_RX_ALIGN, 0,		/* alignment, boundary */
1127f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1128f6bc9430SPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1129f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1130f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsize */
1131f6bc9430SPyun YongHyeon 	    1,				/* nsegments */
1132f6bc9430SPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1133f6bc9430SPyun YongHyeon 	    0,				/* flags */
1134f6bc9430SPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1135f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_tag);
1136f6bc9430SPyun YongHyeon 	if (error != 0) {
1137f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev, "failed to create Rx DMA tag\n");
1138f6bc9430SPyun YongHyeon 		goto fail;
1139f6bc9430SPyun YongHyeon 	}
1140f6bc9430SPyun YongHyeon 
1141f6bc9430SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Tx ring. */
1142f6bc9430SPyun YongHyeon 	error = bus_dmamem_alloc(sc->nge_cdata.nge_tx_ring_tag,
1143f6bc9430SPyun YongHyeon 	    (void **)&sc->nge_rdata.nge_tx_ring, BUS_DMA_WAITOK |
1144f6bc9430SPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->nge_cdata.nge_tx_ring_map);
1145f6bc9430SPyun YongHyeon 	if (error != 0) {
1146f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1147f6bc9430SPyun YongHyeon 		    "failed to allocate DMA'able memory for Tx ring\n");
1148f6bc9430SPyun YongHyeon 		goto fail;
1149f6bc9430SPyun YongHyeon 	}
1150f6bc9430SPyun YongHyeon 
1151f6bc9430SPyun YongHyeon 	ctx.nge_busaddr = 0;
1152f6bc9430SPyun YongHyeon 	error = bus_dmamap_load(sc->nge_cdata.nge_tx_ring_tag,
1153f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map, sc->nge_rdata.nge_tx_ring,
1154f6bc9430SPyun YongHyeon 	    NGE_TX_RING_SIZE, nge_dmamap_cb, &ctx, 0);
1155f6bc9430SPyun YongHyeon 	if (error != 0 || ctx.nge_busaddr == 0) {
1156f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1157f6bc9430SPyun YongHyeon 		    "failed to load DMA'able memory for Tx ring\n");
1158f6bc9430SPyun YongHyeon 		goto fail;
1159f6bc9430SPyun YongHyeon 	}
1160f6bc9430SPyun YongHyeon 	sc->nge_rdata.nge_tx_ring_paddr = ctx.nge_busaddr;
1161f6bc9430SPyun YongHyeon 
1162f6bc9430SPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for Rx ring. */
1163f6bc9430SPyun YongHyeon 	error = bus_dmamem_alloc(sc->nge_cdata.nge_rx_ring_tag,
1164f6bc9430SPyun YongHyeon 	    (void **)&sc->nge_rdata.nge_rx_ring, BUS_DMA_WAITOK |
1165f6bc9430SPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &sc->nge_cdata.nge_rx_ring_map);
1166f6bc9430SPyun YongHyeon 	if (error != 0) {
1167f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1168f6bc9430SPyun YongHyeon 		    "failed to allocate DMA'able memory for Rx ring\n");
1169f6bc9430SPyun YongHyeon 		goto fail;
1170f6bc9430SPyun YongHyeon 	}
1171f6bc9430SPyun YongHyeon 
1172f6bc9430SPyun YongHyeon 	ctx.nge_busaddr = 0;
1173f6bc9430SPyun YongHyeon 	error = bus_dmamap_load(sc->nge_cdata.nge_rx_ring_tag,
1174f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map, sc->nge_rdata.nge_rx_ring,
1175f6bc9430SPyun YongHyeon 	    NGE_RX_RING_SIZE, nge_dmamap_cb, &ctx, 0);
1176f6bc9430SPyun YongHyeon 	if (error != 0 || ctx.nge_busaddr == 0) {
1177f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1178f6bc9430SPyun YongHyeon 		    "failed to load DMA'able memory for Rx ring\n");
1179f6bc9430SPyun YongHyeon 		goto fail;
1180f6bc9430SPyun YongHyeon 	}
1181f6bc9430SPyun YongHyeon 	sc->nge_rdata.nge_rx_ring_paddr = ctx.nge_busaddr;
1182f6bc9430SPyun YongHyeon 
1183f6bc9430SPyun YongHyeon 	/* Create DMA maps for Tx buffers. */
1184f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
1185f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
1186f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1187f6bc9430SPyun YongHyeon 		txd->tx_dmamap = NULL;
1188f6bc9430SPyun YongHyeon 		error = bus_dmamap_create(sc->nge_cdata.nge_tx_tag, 0,
1189f6bc9430SPyun YongHyeon 		    &txd->tx_dmamap);
1190f6bc9430SPyun YongHyeon 		if (error != 0) {
1191f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
1192f6bc9430SPyun YongHyeon 			    "failed to create Tx dmamap\n");
1193f6bc9430SPyun YongHyeon 			goto fail;
1194f6bc9430SPyun YongHyeon 		}
1195f6bc9430SPyun YongHyeon 	}
1196f6bc9430SPyun YongHyeon 	/* Create DMA maps for Rx buffers. */
1197f6bc9430SPyun YongHyeon 	if ((error = bus_dmamap_create(sc->nge_cdata.nge_rx_tag, 0,
1198f6bc9430SPyun YongHyeon 	    &sc->nge_cdata.nge_rx_sparemap)) != 0) {
1199f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
1200f6bc9430SPyun YongHyeon 		    "failed to create spare Rx dmamap\n");
1201f6bc9430SPyun YongHyeon 		goto fail;
1202f6bc9430SPyun YongHyeon 	}
1203f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
1204f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[i];
1205f6bc9430SPyun YongHyeon 		rxd->rx_m = NULL;
1206f6bc9430SPyun YongHyeon 		rxd->rx_dmamap = NULL;
1207f6bc9430SPyun YongHyeon 		error = bus_dmamap_create(sc->nge_cdata.nge_rx_tag, 0,
1208f6bc9430SPyun YongHyeon 		    &rxd->rx_dmamap);
1209f6bc9430SPyun YongHyeon 		if (error != 0) {
1210f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
1211f6bc9430SPyun YongHyeon 			    "failed to create Rx dmamap\n");
1212f6bc9430SPyun YongHyeon 			goto fail;
1213f6bc9430SPyun YongHyeon 		}
1214f6bc9430SPyun YongHyeon 	}
1215f6bc9430SPyun YongHyeon 
1216f6bc9430SPyun YongHyeon fail:
1217f6bc9430SPyun YongHyeon 	return (error);
1218f6bc9430SPyun YongHyeon }
1219f6bc9430SPyun YongHyeon 
1220f6bc9430SPyun YongHyeon static void
1221f6bc9430SPyun YongHyeon nge_dma_free(struct nge_softc *sc)
1222f6bc9430SPyun YongHyeon {
1223f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1224f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1225f6bc9430SPyun YongHyeon 	int i;
1226f6bc9430SPyun YongHyeon 
1227f6bc9430SPyun YongHyeon 	/* Tx ring. */
1228f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_ring_tag) {
1229068d8643SJohn Baldwin 		if (sc->nge_rdata.nge_tx_ring_paddr)
1230f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_ring_tag,
1231f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_tx_ring_map);
1232068d8643SJohn Baldwin 		if (sc->nge_rdata.nge_tx_ring)
1233f6bc9430SPyun YongHyeon 			bus_dmamem_free(sc->nge_cdata.nge_tx_ring_tag,
1234f6bc9430SPyun YongHyeon 			    sc->nge_rdata.nge_tx_ring,
1235f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_tx_ring_map);
1236f6bc9430SPyun YongHyeon 		sc->nge_rdata.nge_tx_ring = NULL;
1237068d8643SJohn Baldwin 		sc->nge_rdata.nge_tx_ring_paddr = 0;
1238f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_tx_ring_tag);
1239f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_ring_tag = NULL;
1240f6bc9430SPyun YongHyeon 	}
1241f6bc9430SPyun YongHyeon 	/* Rx ring. */
1242f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_rx_ring_tag) {
1243068d8643SJohn Baldwin 		if (sc->nge_rdata.nge_rx_ring_paddr)
1244f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_rx_ring_tag,
1245f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_ring_map);
1246068d8643SJohn Baldwin 		if (sc->nge_rdata.nge_rx_ring)
1247f6bc9430SPyun YongHyeon 			bus_dmamem_free(sc->nge_cdata.nge_rx_ring_tag,
1248f6bc9430SPyun YongHyeon 			    sc->nge_rdata.nge_rx_ring,
1249f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_ring_map);
1250f6bc9430SPyun YongHyeon 		sc->nge_rdata.nge_rx_ring = NULL;
1251068d8643SJohn Baldwin 		sc->nge_rdata.nge_rx_ring_paddr = 0;
1252f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_rx_ring_tag);
1253f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_ring_tag = NULL;
1254f6bc9430SPyun YongHyeon 	}
1255f6bc9430SPyun YongHyeon 	/* Tx buffers. */
1256f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_tag) {
1257f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TX_RING_CNT; i++) {
1258f6bc9430SPyun YongHyeon 			txd = &sc->nge_cdata.nge_txdesc[i];
1259f6bc9430SPyun YongHyeon 			if (txd->tx_dmamap) {
1260f6bc9430SPyun YongHyeon 				bus_dmamap_destroy(sc->nge_cdata.nge_tx_tag,
1261f6bc9430SPyun YongHyeon 				    txd->tx_dmamap);
1262f6bc9430SPyun YongHyeon 				txd->tx_dmamap = NULL;
1263f6bc9430SPyun YongHyeon 			}
1264f6bc9430SPyun YongHyeon 		}
1265f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_tx_tag);
1266f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_tag = NULL;
1267f6bc9430SPyun YongHyeon 	}
1268f6bc9430SPyun YongHyeon 	/* Rx buffers. */
1269f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_rx_tag) {
1270f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_RX_RING_CNT; i++) {
1271f6bc9430SPyun YongHyeon 			rxd = &sc->nge_cdata.nge_rxdesc[i];
1272f6bc9430SPyun YongHyeon 			if (rxd->rx_dmamap) {
1273f6bc9430SPyun YongHyeon 				bus_dmamap_destroy(sc->nge_cdata.nge_rx_tag,
1274f6bc9430SPyun YongHyeon 				    rxd->rx_dmamap);
1275f6bc9430SPyun YongHyeon 				rxd->rx_dmamap = NULL;
1276f6bc9430SPyun YongHyeon 			}
1277f6bc9430SPyun YongHyeon 		}
1278f6bc9430SPyun YongHyeon 		if (sc->nge_cdata.nge_rx_sparemap) {
1279f6bc9430SPyun YongHyeon 			bus_dmamap_destroy(sc->nge_cdata.nge_rx_tag,
1280f6bc9430SPyun YongHyeon 			    sc->nge_cdata.nge_rx_sparemap);
1281f6bc9430SPyun YongHyeon 			sc->nge_cdata.nge_rx_sparemap = 0;
1282f6bc9430SPyun YongHyeon 		}
1283f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_rx_tag);
1284f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_tag = NULL;
1285f6bc9430SPyun YongHyeon 	}
1286f6bc9430SPyun YongHyeon 
1287f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_parent_tag) {
1288f6bc9430SPyun YongHyeon 		bus_dma_tag_destroy(sc->nge_cdata.nge_parent_tag);
1289f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_parent_tag = NULL;
1290f6bc9430SPyun YongHyeon 	}
1291f6bc9430SPyun YongHyeon }
1292f6bc9430SPyun YongHyeon 
1293ce4946daSBill Paul /*
1294ce4946daSBill Paul  * Initialize the transmit descriptors.
1295ce4946daSBill Paul  */
1296eaabec55SAlfred Perlstein static int
1297284c81cbSPyun YongHyeon nge_list_tx_init(struct nge_softc *sc)
1298ce4946daSBill Paul {
1299f6bc9430SPyun YongHyeon 	struct nge_ring_data *rd;
1300f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1301f6bc9430SPyun YongHyeon 	bus_addr_t addr;
1302ce4946daSBill Paul 	int i;
1303ce4946daSBill Paul 
1304f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_prod = 0;
1305f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cons = 0;
1306f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cnt = 0;
1307ce4946daSBill Paul 
1308f6bc9430SPyun YongHyeon 	rd = &sc->nge_rdata;
1309f6bc9430SPyun YongHyeon 	bzero(rd->nge_tx_ring, sizeof(struct nge_desc) * NGE_TX_RING_CNT);
1310f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
1311f6bc9430SPyun YongHyeon 		if (i == NGE_TX_RING_CNT - 1)
1312f6bc9430SPyun YongHyeon 			addr = NGE_TX_RING_ADDR(sc, 0);
1313f6bc9430SPyun YongHyeon 		else
1314f6bc9430SPyun YongHyeon 			addr = NGE_TX_RING_ADDR(sc, i + 1);
1315f6bc9430SPyun YongHyeon 		rd->nge_tx_ring[i].nge_next = htole32(NGE_ADDR_LO(addr));
1316f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
1317f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1318ce4946daSBill Paul 	}
1319ce4946daSBill Paul 
1320f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
1321f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map,
1322f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1323ce4946daSBill Paul 
1324ce4946daSBill Paul 	return (0);
1325ce4946daSBill Paul }
1326ce4946daSBill Paul 
1327ce4946daSBill Paul /*
1328ce4946daSBill Paul  * Initialize the RX descriptors and allocate mbufs for them. Note that
1329ce4946daSBill Paul  * we arrange the descriptors in a closed ring, so that the last descriptor
1330ce4946daSBill Paul  * points back to the first.
1331ce4946daSBill Paul  */
1332eaabec55SAlfred Perlstein static int
1333284c81cbSPyun YongHyeon nge_list_rx_init(struct nge_softc *sc)
1334ce4946daSBill Paul {
1335f6bc9430SPyun YongHyeon 	struct nge_ring_data *rd;
1336f6bc9430SPyun YongHyeon 	bus_addr_t addr;
1337ce4946daSBill Paul 	int i;
1338ce4946daSBill Paul 
1339f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_rx_cons = 0;
1340ad6c618bSBill Paul 	sc->nge_head = sc->nge_tail = NULL;
1341ce4946daSBill Paul 
1342f6bc9430SPyun YongHyeon 	rd = &sc->nge_rdata;
1343f6bc9430SPyun YongHyeon 	bzero(rd->nge_rx_ring, sizeof(struct nge_desc) * NGE_RX_RING_CNT);
1344f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
1345f6bc9430SPyun YongHyeon 		if (nge_newbuf(sc, i) != 0)
1346f6bc9430SPyun YongHyeon 			return (ENOBUFS);
1347f6bc9430SPyun YongHyeon 		if (i == NGE_RX_RING_CNT - 1)
1348f6bc9430SPyun YongHyeon 			addr = NGE_RX_RING_ADDR(sc, 0);
1349f6bc9430SPyun YongHyeon 		else
1350f6bc9430SPyun YongHyeon 			addr = NGE_RX_RING_ADDR(sc, i + 1);
1351f6bc9430SPyun YongHyeon 		rd->nge_rx_ring[i].nge_next = htole32(NGE_ADDR_LO(addr));
1352f6bc9430SPyun YongHyeon 	}
1353f6bc9430SPyun YongHyeon 
1354f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1355f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map,
1356f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1357f6bc9430SPyun YongHyeon 
1358ce4946daSBill Paul 	return (0);
1359ce4946daSBill Paul }
1360ce4946daSBill Paul 
1361f6bc9430SPyun YongHyeon static __inline void
1362f6bc9430SPyun YongHyeon nge_discard_rxbuf(struct nge_softc *sc, int idx)
1363f6bc9430SPyun YongHyeon {
1364f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1365f6bc9430SPyun YongHyeon 
1366f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_rx_ring[idx];
1367f6bc9430SPyun YongHyeon 	desc->nge_cmdsts = htole32(MCLBYTES - sizeof(uint64_t));
1368f6bc9430SPyun YongHyeon 	desc->nge_extsts = 0;
1369f6bc9430SPyun YongHyeon }
1370f6bc9430SPyun YongHyeon 
1371ce4946daSBill Paul /*
1372ce4946daSBill Paul  * Initialize an RX descriptor and attach an MBUF cluster.
1373ce4946daSBill Paul  */
1374eaabec55SAlfred Perlstein static int
1375f6bc9430SPyun YongHyeon nge_newbuf(struct nge_softc *sc, int idx)
1376ce4946daSBill Paul {
1377f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1378f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
1379f6bc9430SPyun YongHyeon 	struct mbuf *m;
1380f6bc9430SPyun YongHyeon 	bus_dma_segment_t segs[1];
1381f6bc9430SPyun YongHyeon 	bus_dmamap_t map;
1382f6bc9430SPyun YongHyeon 	int nsegs;
1383ce4946daSBill Paul 
1384c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
13856c772336SSam Leffler 	if (m == NULL)
1386ce4946daSBill Paul 		return (ENOBUFS);
1387ad6c618bSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
13883929ff51SPyun YongHyeon 	m_adj(m, sizeof(uint64_t));
1389ce4946daSBill Paul 
1390f6bc9430SPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_rx_tag,
1391f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_sparemap, m, segs, &nsegs, 0) != 0) {
1392f6bc9430SPyun YongHyeon 		m_freem(m);
1393f6bc9430SPyun YongHyeon 		return (ENOBUFS);
1394f6bc9430SPyun YongHyeon 	}
1395f6bc9430SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1396f6bc9430SPyun YongHyeon 
1397f6bc9430SPyun YongHyeon 	rxd = &sc->nge_cdata.nge_rxdesc[idx];
1398f6bc9430SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1399f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap,
1400f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1401f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap);
1402f6bc9430SPyun YongHyeon 	}
1403f6bc9430SPyun YongHyeon 	map = rxd->rx_dmamap;
1404f6bc9430SPyun YongHyeon 	rxd->rx_dmamap = sc->nge_cdata.nge_rx_sparemap;
1405f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_rx_sparemap = map;
1406f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_tag, rxd->rx_dmamap,
1407f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
1408f6bc9430SPyun YongHyeon 	rxd->rx_m = m;
1409f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_rx_ring[idx];
1410f6bc9430SPyun YongHyeon 	desc->nge_ptr = htole32(NGE_ADDR_LO(segs[0].ds_addr));
1411f6bc9430SPyun YongHyeon 	desc->nge_cmdsts = htole32(segs[0].ds_len);
1412f6bc9430SPyun YongHyeon 	desc->nge_extsts = 0;
1413ce4946daSBill Paul 
1414ce4946daSBill Paul 	return (0);
1415ce4946daSBill Paul }
1416ce4946daSBill Paul 
1417f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
1418ad6c618bSBill Paul static __inline void
1419284c81cbSPyun YongHyeon nge_fixup_rx(struct mbuf *m)
1420ce4946daSBill Paul {
1421ce4946daSBill Paul 	int			i;
1422ad6c618bSBill Paul 	uint16_t		*src, *dst;
1423ce4946daSBill Paul 
1424ad6c618bSBill Paul 	src = mtod(m, uint16_t *);
1425ad6c618bSBill Paul 	dst = src - 1;
1426ce4946daSBill Paul 
1427ad6c618bSBill Paul 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
1428ad6c618bSBill Paul 		*dst++ = *src++;
1429ce4946daSBill Paul 
1430ad6c618bSBill Paul 	m->m_data -= ETHER_ALIGN;
1431ce4946daSBill Paul }
1432ad6c618bSBill Paul #endif
1433ad6c618bSBill Paul 
1434ce4946daSBill Paul /*
1435ce4946daSBill Paul  * A frame has been uploaded: pass the resulting mbuf chain up to
1436ce4946daSBill Paul  * the higher level protocols.
1437ce4946daSBill Paul  */
143856e13f2aSAttilio Rao static int
1439284c81cbSPyun YongHyeon nge_rxeof(struct nge_softc *sc)
1440ce4946daSBill Paul {
1441ce4946daSBill Paul 	struct mbuf *m;
1442ce4946daSBill Paul 	struct ifnet *ifp;
1443ce4946daSBill Paul 	struct nge_desc *cur_rx;
1444f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
144556e13f2aSAttilio Rao 	int cons, prog, rx_npkts, total_len;
1446f6bc9430SPyun YongHyeon 	uint32_t cmdsts, extsts;
1447ce4946daSBill Paul 
1448ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1449f6bc9430SPyun YongHyeon 
1450fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1451f6bc9430SPyun YongHyeon 	cons = sc->nge_cdata.nge_rx_cons;
145256e13f2aSAttilio Rao 	rx_npkts = 0;
1453ce4946daSBill Paul 
1454f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1455f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_rx_ring_map,
1456f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1457ce4946daSBill Paul 
1458f6bc9430SPyun YongHyeon 	for (prog = 0; prog < NGE_RX_RING_CNT &&
1459f6bc9430SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0;
1460f6bc9430SPyun YongHyeon 	    NGE_INC(cons, NGE_RX_RING_CNT)) {
1461196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
146240929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING) {
1463196c0df6SHidetoshi Shimokawa 			if (sc->rxcycles <= 0)
1464196c0df6SHidetoshi Shimokawa 				break;
1465196c0df6SHidetoshi Shimokawa 			sc->rxcycles--;
1466196c0df6SHidetoshi Shimokawa 		}
146740929967SGleb Smirnoff #endif
1468f6bc9430SPyun YongHyeon 		cur_rx = &sc->nge_rdata.nge_rx_ring[cons];
1469f6bc9430SPyun YongHyeon 		cmdsts = le32toh(cur_rx->nge_cmdsts);
1470f6bc9430SPyun YongHyeon 		extsts = le32toh(cur_rx->nge_extsts);
1471f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_OWN) == 0)
1472f6bc9430SPyun YongHyeon 			break;
1473f6bc9430SPyun YongHyeon 		prog++;
1474f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[cons];
1475f6bc9430SPyun YongHyeon 		m = rxd->rx_m;
1476f6bc9430SPyun YongHyeon 		total_len = cmdsts & NGE_CMDSTS_BUFLEN;
1477196c0df6SHidetoshi Shimokawa 
1478f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_MORE) != 0) {
1479f6bc9430SPyun YongHyeon 			if (nge_newbuf(sc, cons) != 0) {
1480e09fdb02SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1481f6bc9430SPyun YongHyeon 				if (sc->nge_head != NULL) {
1482f6bc9430SPyun YongHyeon 					m_freem(sc->nge_head);
1483f6bc9430SPyun YongHyeon 					sc->nge_head = sc->nge_tail = NULL;
1484f6bc9430SPyun YongHyeon 				}
1485f6bc9430SPyun YongHyeon 				nge_discard_rxbuf(sc, cons);
1486f6bc9430SPyun YongHyeon 				continue;
1487f6bc9430SPyun YongHyeon 			}
1488ad6c618bSBill Paul 			m->m_len = total_len;
1489ad6c618bSBill Paul 			if (sc->nge_head == NULL) {
1490ad6c618bSBill Paul 				m->m_pkthdr.len = total_len;
1491ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = m;
1492ad6c618bSBill Paul 			} else {
1493ad6c618bSBill Paul 				m->m_flags &= ~M_PKTHDR;
1494ad6c618bSBill Paul 				sc->nge_head->m_pkthdr.len += total_len;
1495ad6c618bSBill Paul 				sc->nge_tail->m_next = m;
1496ad6c618bSBill Paul 				sc->nge_tail = m;
1497ad6c618bSBill Paul 			}
1498ad6c618bSBill Paul 			continue;
1499ad6c618bSBill Paul 		}
1500ad6c618bSBill Paul 
1501ce4946daSBill Paul 		/*
1502ce4946daSBill Paul 		 * If an error occurs, update stats, clear the
1503ce4946daSBill Paul 		 * status word and leave the mbuf cluster in place:
1504ce4946daSBill Paul 		 * it should simply get re-used next time this descriptor
1505ce4946daSBill Paul 	 	 * comes up in the ring.
1506ce4946daSBill Paul 		 */
1507f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) {
1508f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_RXSTAT_RUNT) &&
1509f6bc9430SPyun YongHyeon 			    total_len >= (ETHER_MIN_LEN - ETHER_CRC_LEN - 4)) {
1510f6bc9430SPyun YongHyeon 				/*
1511f6bc9430SPyun YongHyeon 				 * Work-around hardware bug, accept runt frames
1512f6bc9430SPyun YongHyeon 				 * if its length is larger than or equal to 56.
1513f6bc9430SPyun YongHyeon 				 */
1514f6bc9430SPyun YongHyeon 			} else {
1515f6bc9430SPyun YongHyeon 				/*
1516f6bc9430SPyun YongHyeon 				 * Input error counters are updated by hardware.
1517f6bc9430SPyun YongHyeon 				 */
1518ad6c618bSBill Paul 				if (sc->nge_head != NULL) {
1519ad6c618bSBill Paul 					m_freem(sc->nge_head);
1520ad6c618bSBill Paul 					sc->nge_head = sc->nge_tail = NULL;
1521ad6c618bSBill Paul 				}
1522f6bc9430SPyun YongHyeon 				nge_discard_rxbuf(sc, cons);
1523ce4946daSBill Paul 				continue;
1524ce4946daSBill Paul 			}
1525f6bc9430SPyun YongHyeon 		}
1526ce4946daSBill Paul 
1527ad6c618bSBill Paul 		/* Try conjure up a replacement mbuf. */
1528ad6c618bSBill Paul 
1529f6bc9430SPyun YongHyeon 		if (nge_newbuf(sc, cons) != 0) {
1530e09fdb02SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
1531ad6c618bSBill Paul 			if (sc->nge_head != NULL) {
1532ad6c618bSBill Paul 				m_freem(sc->nge_head);
1533ad6c618bSBill Paul 				sc->nge_head = sc->nge_tail = NULL;
1534ad6c618bSBill Paul 			}
1535f6bc9430SPyun YongHyeon 			nge_discard_rxbuf(sc, cons);
1536ad6c618bSBill Paul 			continue;
1537ad6c618bSBill Paul 		}
1538ad6c618bSBill Paul 
1539f6bc9430SPyun YongHyeon 		/* Chain received mbufs. */
1540ad6c618bSBill Paul 		if (sc->nge_head != NULL) {
1541ad6c618bSBill Paul 			m->m_len = total_len;
1542ad6c618bSBill Paul 			m->m_flags &= ~M_PKTHDR;
1543ad6c618bSBill Paul 			sc->nge_tail->m_next = m;
1544ad6c618bSBill Paul 			m = sc->nge_head;
1545ad6c618bSBill Paul 			m->m_pkthdr.len += total_len;
1546ad6c618bSBill Paul 			sc->nge_head = sc->nge_tail = NULL;
1547ad6c618bSBill Paul 		} else
1548ad6c618bSBill Paul 			m->m_pkthdr.len = m->m_len = total_len;
1549ad6c618bSBill Paul 
1550ce4946daSBill Paul 		/*
1551ce4946daSBill Paul 		 * Ok. NatSemi really screwed up here. This is the
1552ce4946daSBill Paul 		 * only gigE chip I know of with alignment constraints
1553ce4946daSBill Paul 		 * on receive buffers. RX buffers must be 64-bit aligned.
1554ce4946daSBill Paul 		 */
1555962315f6SBill Paul 		/*
1556962315f6SBill Paul 		 * By popular demand, ignore the alignment problems
1557f6bc9430SPyun YongHyeon 		 * on the non-strict alignment platform. The performance hit
1558962315f6SBill Paul 		 * incurred due to unaligned accesses is much smaller
1559962315f6SBill Paul 		 * than the hit produced by forcing buffer copies all
1560962315f6SBill Paul 		 * the time, especially with jumbo frames. We still
1561962315f6SBill Paul 		 * need to fix up the alignment everywhere else though.
1562962315f6SBill Paul 		 */
1563f6bc9430SPyun YongHyeon #ifndef __NO_STRICT_ALIGNMENT
1564ad6c618bSBill Paul 		nge_fixup_rx(m);
1565962315f6SBill Paul #endif
1566ad6c618bSBill Paul 		m->m_pkthdr.rcvif = ifp;
1567e09fdb02SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
1568ce4946daSBill Paul 
1569f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_RXCSUM) != 0) {
1570ce4946daSBill Paul 			/* Do IP checksum checking. */
1571f6bc9430SPyun YongHyeon 			if ((extsts & NGE_RXEXTSTS_IPPKT) != 0)
1572ce4946daSBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
1573f6bc9430SPyun YongHyeon 			if ((extsts & NGE_RXEXTSTS_IPCSUMERR) == 0)
157401702579SBill Paul 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
15752195de46SBill Paul 			if ((extsts & NGE_RXEXTSTS_TCPPKT &&
15762195de46SBill Paul 			    !(extsts & NGE_RXEXTSTS_TCPCSUMERR)) ||
15772195de46SBill Paul 			    (extsts & NGE_RXEXTSTS_UDPPKT &&
15782195de46SBill Paul 			    !(extsts & NGE_RXEXTSTS_UDPCSUMERR))) {
15792195de46SBill Paul 				m->m_pkthdr.csum_flags |=
15802195de46SBill Paul 				    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
1581c9215605SBill Paul 				m->m_pkthdr.csum_data = 0xffff;
15822195de46SBill Paul 			}
1583f6bc9430SPyun YongHyeon 		}
1584ce4946daSBill Paul 
1585ce4946daSBill Paul 		/*
1586ce4946daSBill Paul 		 * If we received a packet with a vlan tag, pass it
1587ce4946daSBill Paul 		 * to vlan_input() instead of ether_input().
1588ce4946daSBill Paul 		 */
1589f6bc9430SPyun YongHyeon 		if ((extsts & NGE_RXEXTSTS_VLANPKT) != 0 &&
1590f6bc9430SPyun YongHyeon 		    (ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0) {
159178ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
1592f6bc9430SPyun YongHyeon 			    bswap16(extsts & NGE_RXEXTSTS_VTCI);
159378ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
1594ce4946daSBill Paul 		}
1595ad6c618bSBill Paul 		NGE_UNLOCK(sc);
1596673d9191SSam Leffler 		(*ifp->if_input)(ifp, m);
1597ad6c618bSBill Paul 		NGE_LOCK(sc);
159856e13f2aSAttilio Rao 		rx_npkts++;
1599ce4946daSBill Paul 	}
1600ce4946daSBill Paul 
1601f6bc9430SPyun YongHyeon 	if (prog > 0) {
1602f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_rx_cons = cons;
1603f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_rx_ring_tag,
1604f6bc9430SPyun YongHyeon 		    sc->nge_cdata.nge_rx_ring_map,
1605f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1606f6bc9430SPyun YongHyeon 	}
160756e13f2aSAttilio Rao 	return (rx_npkts);
1608ce4946daSBill Paul }
1609ce4946daSBill Paul 
1610ce4946daSBill Paul /*
1611ce4946daSBill Paul  * A frame was downloaded to the chip. It's safe for us to clean up
1612ce4946daSBill Paul  * the list buffers.
1613ce4946daSBill Paul  */
1614eaabec55SAlfred Perlstein static void
1615284c81cbSPyun YongHyeon nge_txeof(struct nge_softc *sc)
1616ce4946daSBill Paul {
16171e73ec7dSRuslan Ermilov 	struct nge_desc	*cur_tx;
1618f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
1619ce4946daSBill Paul 	struct ifnet *ifp;
1620f6bc9430SPyun YongHyeon 	uint32_t cmdsts;
1621f6bc9430SPyun YongHyeon 	int cons, prod;
1622ce4946daSBill Paul 
1623ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1624fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1625ce4946daSBill Paul 
1626f6bc9430SPyun YongHyeon 	cons = sc->nge_cdata.nge_tx_cons;
1627f6bc9430SPyun YongHyeon 	prod = sc->nge_cdata.nge_tx_prod;
1628f6bc9430SPyun YongHyeon 	if (cons == prod)
1629f6bc9430SPyun YongHyeon 		return;
1630f6bc9430SPyun YongHyeon 
1631f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
1632f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_ring_map,
1633f6bc9430SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1634f6bc9430SPyun YongHyeon 
1635ce4946daSBill Paul 	/*
1636ce4946daSBill Paul 	 * Go through our tx list and free mbufs for those
1637ce4946daSBill Paul 	 * frames that have been transmitted.
1638ce4946daSBill Paul 	 */
1639f6bc9430SPyun YongHyeon 	for (; cons != prod; NGE_INC(cons, NGE_TX_RING_CNT)) {
1640f6bc9430SPyun YongHyeon 		cur_tx = &sc->nge_rdata.nge_tx_ring[cons];
1641f6bc9430SPyun YongHyeon 		cmdsts = le32toh(cur_tx->nge_cmdsts);
1642f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_OWN) != 0)
1643ce4946daSBill Paul 			break;
1644ce4946daSBill Paul 		sc->nge_cdata.nge_tx_cnt--;
164513f4c340SRobert Watson 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
1646f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_MORE) != 0)
1647f6bc9430SPyun YongHyeon 			continue;
1648f6bc9430SPyun YongHyeon 
1649f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[cons];
1650f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap,
1651f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_POSTWRITE);
1652f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, txd->tx_dmamap);
1653f6bc9430SPyun YongHyeon 		if ((cmdsts & NGE_CMDSTS_PKT_OK) == 0) {
1654e09fdb02SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1655f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_TXSTAT_EXCESSCOLLS) != 0)
1656e09fdb02SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1657f6bc9430SPyun YongHyeon 			if ((cmdsts & NGE_TXSTAT_OUTOFWINCOLL) != 0)
1658e09fdb02SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
1659f6bc9430SPyun YongHyeon 		} else
1660e09fdb02SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
1661f6bc9430SPyun YongHyeon 
1662e09fdb02SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_COLLISIONS, (cmdsts & NGE_TXSTAT_COLLCNT) >> 16);
1663f6bc9430SPyun YongHyeon 		KASSERT(txd->tx_m != NULL, ("%s: freeing NULL mbuf!\n",
1664f6bc9430SPyun YongHyeon 		    __func__));
1665f6bc9430SPyun YongHyeon 		m_freem(txd->tx_m);
1666f6bc9430SPyun YongHyeon 		txd->tx_m = NULL;
1667ce4946daSBill Paul 	}
1668ce4946daSBill Paul 
1669f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_cons = cons;
1670f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_cnt == 0)
1671f6bc9430SPyun YongHyeon 		sc->nge_watchdog_timer = 0;
1672ce4946daSBill Paul }
1673ce4946daSBill Paul 
1674eaabec55SAlfred Perlstein static void
1675284c81cbSPyun YongHyeon nge_tick(void *xsc)
1676ce4946daSBill Paul {
1677ce4946daSBill Paul 	struct nge_softc *sc;
1678ad6c618bSBill Paul 	struct mii_data *mii;
1679ad6c618bSBill Paul 
1680646abee6SJohn Baldwin 	sc = xsc;
1681ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
1682ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
1683ce4946daSBill Paul 	mii_tick(mii);
1684f6bc9430SPyun YongHyeon 	/*
1685f6bc9430SPyun YongHyeon 	 * For PHYs that does not reset established link, it is
1686f6bc9430SPyun YongHyeon 	 * necessary to check whether driver still have a valid
1687f6bc9430SPyun YongHyeon 	 * link(e.g link state change callback is not called).
1688f6bc9430SPyun YongHyeon 	 * Otherwise, driver think it lost link because driver
1689f6bc9430SPyun YongHyeon 	 * initialization routine clears link state flag.
1690f6bc9430SPyun YongHyeon 	 */
1691f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_LINK) == 0)
1692f6bc9430SPyun YongHyeon 		nge_miibus_statchg(sc->nge_dev);
1693f6bc9430SPyun YongHyeon 	nge_stats_update(sc);
1694f6bc9430SPyun YongHyeon 	nge_watchdog(sc);
1695ad6c618bSBill Paul 	callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc);
1696ce4946daSBill Paul }
1697ce4946daSBill Paul 
1698f6bc9430SPyun YongHyeon static void
1699f6bc9430SPyun YongHyeon nge_stats_update(struct nge_softc *sc)
1700f6bc9430SPyun YongHyeon {
1701f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
1702f6bc9430SPyun YongHyeon 	struct nge_stats now, *stats, *nstats;
1703f6bc9430SPyun YongHyeon 
1704f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
1705f6bc9430SPyun YongHyeon 
1706f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
1707f6bc9430SPyun YongHyeon 	stats = &now;
1708f6bc9430SPyun YongHyeon 	stats->rx_pkts_errs =
1709f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRPKT) & 0xFFFF;
1710f6bc9430SPyun YongHyeon 	stats->rx_crc_errs =
1711f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRFCS) & 0xFFFF;
1712f6bc9430SPyun YongHyeon 	stats->rx_fifo_oflows =
1713f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRMISSEDPKT) & 0xFFFF;
1714f6bc9430SPyun YongHyeon 	stats->rx_align_errs =
1715f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRALIGN) & 0xFFFF;
1716f6bc9430SPyun YongHyeon 	stats->rx_sym_errs =
1717f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRSYM) & 0xFFFF;
1718f6bc9430SPyun YongHyeon 	stats->rx_pkts_jumbos =
1719f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRGIANT) & 0xFFFF;
1720f6bc9430SPyun YongHyeon 	stats->rx_len_errs =
1721f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXERRRANGLEN) & 0xFFFF;
1722f6bc9430SPyun YongHyeon 	stats->rx_unctl_frames =
1723f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXBADOPCODE) & 0xFFFF;
1724f6bc9430SPyun YongHyeon 	stats->rx_pause =
1725f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_RXPAUSEPKTS) & 0xFFFF;
1726f6bc9430SPyun YongHyeon 	stats->tx_pause =
1727f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_TXPAUSEPKTS) & 0xFFFF;
1728f6bc9430SPyun YongHyeon 	stats->tx_seq_errs =
1729f6bc9430SPyun YongHyeon 	    CSR_READ_4(sc, NGE_MIB_TXERRSQE) & 0xFF;
1730f6bc9430SPyun YongHyeon 
1731f6bc9430SPyun YongHyeon 	/*
1732f6bc9430SPyun YongHyeon 	 * Since we've accept errored frames exclude Rx length errors.
1733f6bc9430SPyun YongHyeon 	 */
1734e09fdb02SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_IERRORS,
1735e09fdb02SGleb Smirnoff 	    stats->rx_pkts_errs + stats->rx_crc_errs +
1736e09fdb02SGleb Smirnoff 	    stats->rx_fifo_oflows + stats->rx_sym_errs);
1737f6bc9430SPyun YongHyeon 
1738f6bc9430SPyun YongHyeon 	nstats = &sc->nge_stats;
1739f6bc9430SPyun YongHyeon 	nstats->rx_pkts_errs += stats->rx_pkts_errs;
1740f6bc9430SPyun YongHyeon 	nstats->rx_crc_errs += stats->rx_crc_errs;
1741f6bc9430SPyun YongHyeon 	nstats->rx_fifo_oflows += stats->rx_fifo_oflows;
1742f6bc9430SPyun YongHyeon 	nstats->rx_align_errs += stats->rx_align_errs;
1743f6bc9430SPyun YongHyeon 	nstats->rx_sym_errs += stats->rx_sym_errs;
1744f6bc9430SPyun YongHyeon 	nstats->rx_pkts_jumbos += stats->rx_pkts_jumbos;
1745f6bc9430SPyun YongHyeon 	nstats->rx_len_errs += stats->rx_len_errs;
1746f6bc9430SPyun YongHyeon 	nstats->rx_unctl_frames += stats->rx_unctl_frames;
1747f6bc9430SPyun YongHyeon 	nstats->rx_pause += stats->rx_pause;
1748f6bc9430SPyun YongHyeon 	nstats->tx_pause += stats->tx_pause;
1749f6bc9430SPyun YongHyeon 	nstats->tx_seq_errs += stats->tx_seq_errs;
1750f6bc9430SPyun YongHyeon }
1751f6bc9430SPyun YongHyeon 
1752196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
1753196c0df6SHidetoshi Shimokawa static poll_handler_t nge_poll;
1754196c0df6SHidetoshi Shimokawa 
175556e13f2aSAttilio Rao static int
1756196c0df6SHidetoshi Shimokawa nge_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
1757196c0df6SHidetoshi Shimokawa {
1758f6bc9430SPyun YongHyeon 	struct nge_softc *sc;
175956e13f2aSAttilio Rao 	int rx_npkts = 0;
1760f6bc9430SPyun YongHyeon 
1761f6bc9430SPyun YongHyeon 	sc = ifp->if_softc;
1762196c0df6SHidetoshi Shimokawa 
1763ad6c618bSBill Paul 	NGE_LOCK(sc);
1764f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
1765ad6c618bSBill Paul 		NGE_UNLOCK(sc);
176656e13f2aSAttilio Rao 		return (rx_npkts);
1767196c0df6SHidetoshi Shimokawa 	}
1768196c0df6SHidetoshi Shimokawa 
1769196c0df6SHidetoshi Shimokawa 	/*
1770196c0df6SHidetoshi Shimokawa 	 * On the nge, reading the status register also clears it.
1771196c0df6SHidetoshi Shimokawa 	 * So before returning to intr mode we must make sure that all
1772196c0df6SHidetoshi Shimokawa 	 * possible pending sources of interrupts have been served.
1773196c0df6SHidetoshi Shimokawa 	 * In practice this means run to completion the *eof routines,
1774f6bc9430SPyun YongHyeon 	 * and then call the interrupt routine.
1775196c0df6SHidetoshi Shimokawa 	 */
1776196c0df6SHidetoshi Shimokawa 	sc->rxcycles = count;
177756e13f2aSAttilio Rao 	rx_npkts = nge_rxeof(sc);
1778196c0df6SHidetoshi Shimokawa 	nge_txeof(sc);
1779f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1780ad6c618bSBill Paul 		nge_start_locked(ifp);
1781196c0df6SHidetoshi Shimokawa 
1782196c0df6SHidetoshi Shimokawa 	if (sc->rxcycles > 0 || cmd == POLL_AND_CHECK_STATUS) {
17833929ff51SPyun YongHyeon 		uint32_t	status;
1784196c0df6SHidetoshi Shimokawa 
1785196c0df6SHidetoshi Shimokawa 		/* Reading the ISR register clears all interrupts. */
1786196c0df6SHidetoshi Shimokawa 		status = CSR_READ_4(sc, NGE_ISR);
1787196c0df6SHidetoshi Shimokawa 
1788f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_RX_ERR|NGE_ISR_RX_OFLOW)) != 0)
178956e13f2aSAttilio Rao 			rx_npkts += nge_rxeof(sc);
1790196c0df6SHidetoshi Shimokawa 
1791f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_RX_IDLE) != 0)
1792196c0df6SHidetoshi Shimokawa 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1793196c0df6SHidetoshi Shimokawa 
1794f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_SYSERR) != 0) {
1795f6bc9430SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1796ad6c618bSBill Paul 			nge_init_locked(sc);
1797196c0df6SHidetoshi Shimokawa 		}
1798196c0df6SHidetoshi Shimokawa 	}
1799ad6c618bSBill Paul 	NGE_UNLOCK(sc);
180056e13f2aSAttilio Rao 	return (rx_npkts);
1801196c0df6SHidetoshi Shimokawa }
1802196c0df6SHidetoshi Shimokawa #endif /* DEVICE_POLLING */
1803196c0df6SHidetoshi Shimokawa 
1804eaabec55SAlfred Perlstein static void
1805284c81cbSPyun YongHyeon nge_intr(void *arg)
1806ce4946daSBill Paul {
1807ce4946daSBill Paul 	struct nge_softc *sc;
1808ce4946daSBill Paul 	struct ifnet *ifp;
18093929ff51SPyun YongHyeon 	uint32_t status;
1810ce4946daSBill Paul 
1811f6bc9430SPyun YongHyeon 	sc = (struct nge_softc *)arg;
1812fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
1813ce4946daSBill Paul 
1814ad6c618bSBill Paul 	NGE_LOCK(sc);
1815196c0df6SHidetoshi Shimokawa 
1816f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_SUSPENDED) != 0)
1817f6bc9430SPyun YongHyeon 		goto done_locked;
1818f6bc9430SPyun YongHyeon 
1819f6bc9430SPyun YongHyeon 	/* Reading the ISR register clears all interrupts. */
1820f6bc9430SPyun YongHyeon 	status = CSR_READ_4(sc, NGE_ISR);
1821f6bc9430SPyun YongHyeon 	if (status == 0xffffffff || (status & NGE_INTRS) == 0)
1822f6bc9430SPyun YongHyeon 		goto done_locked;
1823f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
1824f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
1825f6bc9430SPyun YongHyeon 		goto done_locked;
1826f6bc9430SPyun YongHyeon #endif
1827f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
1828f6bc9430SPyun YongHyeon 		goto done_locked;
1829ce4946daSBill Paul 
1830ce4946daSBill Paul 	/* Disable interrupts. */
1831ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 0);
1832ce4946daSBill Paul 
18331f548804SDoug Ambrisko 	/* Data LED on for TBI mode */
1834f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
1835f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
1836f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) | NGE_GPIO_GP3_OUT);
18371f548804SDoug Ambrisko 
1838f6bc9430SPyun YongHyeon 	for (; (status & NGE_INTRS) != 0;) {
1839f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_TX_DESC_OK | NGE_ISR_TX_ERR |
1840f6bc9430SPyun YongHyeon 		    NGE_ISR_TX_OK | NGE_ISR_TX_IDLE)) != 0)
1841ce4946daSBill Paul 			nge_txeof(sc);
1842ce4946daSBill Paul 
1843f6bc9430SPyun YongHyeon 		if ((status & (NGE_ISR_RX_DESC_OK | NGE_ISR_RX_ERR |
1844f6bc9430SPyun YongHyeon 		    NGE_ISR_RX_OFLOW | NGE_ISR_RX_FIFO_OFLOW |
1845f6bc9430SPyun YongHyeon 		    NGE_ISR_RX_IDLE | NGE_ISR_RX_OK)) != 0)
1846ce4946daSBill Paul 			nge_rxeof(sc);
1847ff7ed9f7SPoul-Henning Kamp 
1848f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_RX_IDLE) != 0)
1849ff7ed9f7SPoul-Henning Kamp 			NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
1850ff7ed9f7SPoul-Henning Kamp 
1851f6bc9430SPyun YongHyeon 		if ((status & NGE_ISR_SYSERR) != 0) {
185213f4c340SRobert Watson 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1853ad6c618bSBill Paul 			nge_init_locked(sc);
1854ce4946daSBill Paul 		}
1855f6bc9430SPyun YongHyeon 		/* Reading the ISR register clears all interrupts. */
1856f6bc9430SPyun YongHyeon 		status = CSR_READ_4(sc, NGE_ISR);
1857ce4946daSBill Paul 	}
1858ce4946daSBill Paul 
1859ce4946daSBill Paul 	/* Re-enable interrupts. */
1860ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
1861ce4946daSBill Paul 
1862f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
1863ad6c618bSBill Paul 		nge_start_locked(ifp);
1864ce4946daSBill Paul 
18651f548804SDoug Ambrisko 	/* Data LED off for TBI mode */
1866f6bc9430SPyun YongHyeon 	if ((sc->nge_flags & NGE_FLAG_TBI) != 0)
1867f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_GPIO,
1868f6bc9430SPyun YongHyeon 		    CSR_READ_4(sc, NGE_GPIO) & ~NGE_GPIO_GP3_OUT);
18691f548804SDoug Ambrisko 
1870f6bc9430SPyun YongHyeon done_locked:
1871ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1872ce4946daSBill Paul }
1873ce4946daSBill Paul 
1874ce4946daSBill Paul /*
1875ce4946daSBill Paul  * Encapsulate an mbuf chain in a descriptor by coupling the mbuf data
1876ce4946daSBill Paul  * pointers to the fragment pointers.
1877ce4946daSBill Paul  */
1878eaabec55SAlfred Perlstein static int
1879f6bc9430SPyun YongHyeon nge_encap(struct nge_softc *sc, struct mbuf **m_head)
1880ce4946daSBill Paul {
1881f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd, *txd_last;
1882f6bc9430SPyun YongHyeon 	struct nge_desc *desc;
1883ce4946daSBill Paul 	struct mbuf *m;
1884f6bc9430SPyun YongHyeon 	bus_dmamap_t map;
1885f6bc9430SPyun YongHyeon 	bus_dma_segment_t txsegs[NGE_MAXTXSEGS];
1886f6bc9430SPyun YongHyeon 	int error, i, nsegs, prod, si;
1887ce4946daSBill Paul 
1888f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
1889ce4946daSBill Paul 
1890f6bc9430SPyun YongHyeon 	m = *m_head;
1891f6bc9430SPyun YongHyeon 	prod = sc->nge_cdata.nge_tx_prod;
1892f6bc9430SPyun YongHyeon 	txd = &sc->nge_cdata.nge_txdesc[prod];
1893f6bc9430SPyun YongHyeon 	txd_last = txd;
1894f6bc9430SPyun YongHyeon 	map = txd->tx_dmamap;
1895f6bc9430SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag, map,
1896f6bc9430SPyun YongHyeon 	    *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1897f6bc9430SPyun YongHyeon 	if (error == EFBIG) {
1898c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, NGE_MAXTXSEGS);
1899f6bc9430SPyun YongHyeon 		if (m == NULL) {
1900f6bc9430SPyun YongHyeon 			m_freem(*m_head);
1901f6bc9430SPyun YongHyeon 			*m_head = NULL;
1902ce4946daSBill Paul 			return (ENOBUFS);
1903ce4946daSBill Paul 		}
1904f6bc9430SPyun YongHyeon 		*m_head = m;
1905f6bc9430SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->nge_cdata.nge_tx_tag,
1906f6bc9430SPyun YongHyeon 		    map, *m_head, txsegs, &nsegs, BUS_DMA_NOWAIT);
1907f6bc9430SPyun YongHyeon 		if (error != 0) {
1908f6bc9430SPyun YongHyeon 			m_freem(*m_head);
1909f6bc9430SPyun YongHyeon 			*m_head = NULL;
1910f6bc9430SPyun YongHyeon 			return (error);
1911f6bc9430SPyun YongHyeon 		}
1912f6bc9430SPyun YongHyeon 	} else if (error != 0)
1913f6bc9430SPyun YongHyeon 		return (error);
1914f6bc9430SPyun YongHyeon 	if (nsegs == 0) {
1915f6bc9430SPyun YongHyeon 		m_freem(*m_head);
1916f6bc9430SPyun YongHyeon 		*m_head = NULL;
1917f6bc9430SPyun YongHyeon 		return (EIO);
1918ce4946daSBill Paul 	}
1919ce4946daSBill Paul 
1920f6bc9430SPyun YongHyeon 	/* Check number of available descriptors. */
1921f6bc9430SPyun YongHyeon 	if (sc->nge_cdata.nge_tx_cnt + nsegs >= (NGE_TX_RING_CNT - 1)) {
1922f6bc9430SPyun YongHyeon 		bus_dmamap_unload(sc->nge_cdata.nge_tx_tag, map);
1923ce4946daSBill Paul 		return (ENOBUFS);
1924ce4946daSBill Paul 	}
1925ce4946daSBill Paul 
1926f6bc9430SPyun YongHyeon 	bus_dmamap_sync(sc->nge_cdata.nge_tx_tag, map, BUS_DMASYNC_PREWRITE);
1927ce4946daSBill Paul 
1928f6bc9430SPyun YongHyeon 	si = prod;
1929f6bc9430SPyun YongHyeon 	for (i = 0; i < nsegs; i++) {
1930f6bc9430SPyun YongHyeon 		desc = &sc->nge_rdata.nge_tx_ring[prod];
1931f6bc9430SPyun YongHyeon 		desc->nge_ptr = htole32(NGE_ADDR_LO(txsegs[i].ds_addr));
1932f6bc9430SPyun YongHyeon 		if (i == 0)
1933f6bc9430SPyun YongHyeon 			desc->nge_cmdsts = htole32(txsegs[i].ds_len |
1934f6bc9430SPyun YongHyeon 			    NGE_CMDSTS_MORE);
1935f6bc9430SPyun YongHyeon 		else
1936f6bc9430SPyun YongHyeon 			desc->nge_cmdsts = htole32(txsegs[i].ds_len |
1937f6bc9430SPyun YongHyeon 			    NGE_CMDSTS_MORE | NGE_CMDSTS_OWN);
1938f6bc9430SPyun YongHyeon 		desc->nge_extsts = 0;
1939f6bc9430SPyun YongHyeon 		sc->nge_cdata.nge_tx_cnt++;
1940f6bc9430SPyun YongHyeon 		NGE_INC(prod, NGE_TX_RING_CNT);
1941f6bc9430SPyun YongHyeon 	}
1942f6bc9430SPyun YongHyeon 	/* Update producer index. */
1943f6bc9430SPyun YongHyeon 	sc->nge_cdata.nge_tx_prod = prod;
1944f6bc9430SPyun YongHyeon 
1945f6bc9430SPyun YongHyeon 	prod = (prod + NGE_TX_RING_CNT - 1) % NGE_TX_RING_CNT;
1946f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_tx_ring[prod];
1947f6bc9430SPyun YongHyeon 	/* Check if we have a VLAN tag to insert. */
1948f6bc9430SPyun YongHyeon 	if ((m->m_flags & M_VLANTAG) != 0)
1949f6bc9430SPyun YongHyeon 		desc->nge_extsts |= htole32(NGE_TXEXTSTS_VLANPKT |
1950f6bc9430SPyun YongHyeon 		    bswap16(m->m_pkthdr.ether_vtag));
1951b1603638SGordon Bergling 	/* Set EOP on the last descriptor. */
1952f6bc9430SPyun YongHyeon 	desc->nge_cmdsts &= htole32(~NGE_CMDSTS_MORE);
1953f6bc9430SPyun YongHyeon 
1954f6bc9430SPyun YongHyeon 	/* Set checksum offload in the first descriptor. */
1955f6bc9430SPyun YongHyeon 	desc = &sc->nge_rdata.nge_tx_ring[si];
1956f6bc9430SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & NGE_CSUM_FEATURES) != 0) {
1957f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
1958f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_IPCSUM);
1959f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
1960f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_TCPCSUM);
1961f6bc9430SPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
1962f6bc9430SPyun YongHyeon 			desc->nge_extsts |= htole32(NGE_TXEXTSTS_UDPCSUM);
1963f6bc9430SPyun YongHyeon 	}
1964f6bc9430SPyun YongHyeon 	/* Lastly, turn the first descriptor ownership to hardware. */
1965f6bc9430SPyun YongHyeon 	desc->nge_cmdsts |= htole32(NGE_CMDSTS_OWN);
1966f6bc9430SPyun YongHyeon 
1967f6bc9430SPyun YongHyeon 	txd = &sc->nge_cdata.nge_txdesc[prod];
1968f6bc9430SPyun YongHyeon 	map = txd_last->tx_dmamap;
1969f6bc9430SPyun YongHyeon 	txd_last->tx_dmamap = txd->tx_dmamap;
1970f6bc9430SPyun YongHyeon 	txd->tx_dmamap = map;
1971f6bc9430SPyun YongHyeon 	txd->tx_m = m;
1972ce4946daSBill Paul 
1973ce4946daSBill Paul 	return (0);
1974ce4946daSBill Paul }
1975ce4946daSBill Paul 
1976ce4946daSBill Paul /*
1977ce4946daSBill Paul  * Main transmit routine. To avoid having to do mbuf copies, we put pointers
1978ce4946daSBill Paul  * to the mbuf data regions directly in the transmit lists. We also save a
1979ce4946daSBill Paul  * copy of the pointers since the transmit list fragment pointers are
1980ce4946daSBill Paul  * physical addresses.
1981ce4946daSBill Paul  */
1982ce4946daSBill Paul 
1983eaabec55SAlfred Perlstein static void
1984284c81cbSPyun YongHyeon nge_start(struct ifnet *ifp)
1985ce4946daSBill Paul {
1986ce4946daSBill Paul 	struct nge_softc *sc;
1987ad6c618bSBill Paul 
1988ad6c618bSBill Paul 	sc = ifp->if_softc;
1989ad6c618bSBill Paul 	NGE_LOCK(sc);
1990ad6c618bSBill Paul 	nge_start_locked(ifp);
1991ad6c618bSBill Paul 	NGE_UNLOCK(sc);
1992ad6c618bSBill Paul }
1993ad6c618bSBill Paul 
1994ad6c618bSBill Paul static void
1995284c81cbSPyun YongHyeon nge_start_locked(struct ifnet *ifp)
1996ad6c618bSBill Paul {
1997ad6c618bSBill Paul 	struct nge_softc *sc;
1998f6bc9430SPyun YongHyeon 	struct mbuf *m_head;
1999f6bc9430SPyun YongHyeon 	int enq;
2000ce4946daSBill Paul 
2001ce4946daSBill Paul 	sc = ifp->if_softc;
2002ce4946daSBill Paul 
2003f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2004f6bc9430SPyun YongHyeon 
2005f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2006f6bc9430SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->nge_flags & NGE_FLAG_LINK) == 0)
2007ce4946daSBill Paul 		return;
2008ce4946daSBill Paul 
2009f6bc9430SPyun YongHyeon 	for (enq = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2010f6bc9430SPyun YongHyeon 	    sc->nge_cdata.nge_tx_cnt < NGE_TX_RING_CNT - 2; ) {
2011f6bc9430SPyun YongHyeon 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2012ce4946daSBill Paul 		if (m_head == NULL)
2013ce4946daSBill Paul 			break;
2014f6bc9430SPyun YongHyeon 		/*
2015f6bc9430SPyun YongHyeon 		 * Pack the data into the transmit ring. If we
2016f6bc9430SPyun YongHyeon 		 * don't have room, set the OACTIVE flag and wait
2017f6bc9430SPyun YongHyeon 		 * for the NIC to drain the ring.
2018f6bc9430SPyun YongHyeon 		 */
2019f6bc9430SPyun YongHyeon 		if (nge_encap(sc, &m_head)) {
2020f6bc9430SPyun YongHyeon 			if (m_head == NULL)
2021f6bc9430SPyun YongHyeon 				break;
2022f6bc9430SPyun YongHyeon 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
202313f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2024ce4946daSBill Paul 			break;
2025ce4946daSBill Paul 		}
2026ce4946daSBill Paul 
2027f6bc9430SPyun YongHyeon 		enq++;
2028ce4946daSBill Paul 		/*
2029ce4946daSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2030ce4946daSBill Paul 		 * to him.
2031ce4946daSBill Paul 		 */
203259a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
2033ce4946daSBill Paul 	}
2034ce4946daSBill Paul 
2035f6bc9430SPyun YongHyeon 	if (enq > 0) {
2036f6bc9430SPyun YongHyeon 		bus_dmamap_sync(sc->nge_cdata.nge_tx_ring_tag,
2037f6bc9430SPyun YongHyeon 		    sc->nge_cdata.nge_tx_ring_map,
2038f6bc9430SPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2039ce4946daSBill Paul 		/* Transmit */
2040ce4946daSBill Paul 		NGE_SETBIT(sc, NGE_CSR, NGE_CSR_TX_ENABLE);
2041ce4946daSBill Paul 
2042f6bc9430SPyun YongHyeon 		/* Set a timeout in case the chip goes out to lunch. */
2043f6bc9430SPyun YongHyeon 		sc->nge_watchdog_timer = 5;
2044f6bc9430SPyun YongHyeon 	}
2045ce4946daSBill Paul }
2046ce4946daSBill Paul 
2047eaabec55SAlfred Perlstein static void
2048284c81cbSPyun YongHyeon nge_init(void *xsc)
2049ce4946daSBill Paul {
2050ce4946daSBill Paul 	struct nge_softc *sc = xsc;
2051ad6c618bSBill Paul 
2052ad6c618bSBill Paul 	NGE_LOCK(sc);
2053ad6c618bSBill Paul 	nge_init_locked(sc);
2054ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2055ad6c618bSBill Paul }
2056ad6c618bSBill Paul 
2057ad6c618bSBill Paul static void
2058284c81cbSPyun YongHyeon nge_init_locked(struct nge_softc *sc)
2059ad6c618bSBill Paul {
2060fc74a9f9SBrooks Davis 	struct ifnet *ifp = sc->nge_ifp;
2061ce4946daSBill Paul 	struct mii_data *mii;
2062f6bc9430SPyun YongHyeon 	uint8_t *eaddr;
2063f6bc9430SPyun YongHyeon 	uint32_t reg;
2064ad6c618bSBill Paul 
2065ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
2066ce4946daSBill Paul 
2067f6bc9430SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2068ce4946daSBill Paul 		return;
2069ce4946daSBill Paul 
2070ce4946daSBill Paul 	/*
2071ce4946daSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2072ce4946daSBill Paul 	 */
2073ce4946daSBill Paul 	nge_stop(sc);
2074ce4946daSBill Paul 
2075f6bc9430SPyun YongHyeon 	/* Reset the adapter. */
2076f6bc9430SPyun YongHyeon 	nge_reset(sc);
2077ce4946daSBill Paul 
2078f6bc9430SPyun YongHyeon 	/* Disable Rx filter prior to programming Rx filter. */
2079f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, 0);
20808c1093fcSMarius Strobl 	CSR_BARRIER_4(sc, NGE_RXFILT_CTL, BUS_SPACE_BARRIER_WRITE);
2081f6bc9430SPyun YongHyeon 
2082f6bc9430SPyun YongHyeon 	mii = device_get_softc(sc->nge_miibus);
2083f6bc9430SPyun YongHyeon 
2084f6bc9430SPyun YongHyeon 	/* Set MAC address. */
2085f6bc9430SPyun YongHyeon 	eaddr = IF_LLADDR(sc->nge_ifp);
2086ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR0);
2087f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[1] << 8) | eaddr[0]);
2088ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR1);
2089f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[3] << 8) | eaddr[2]);
2090ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RXFILT_CTL, NGE_FILTADDR_PAR2);
2091f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RXFILT_DATA, (eaddr[5] << 8) | eaddr[4]);
2092ce4946daSBill Paul 
2093ce4946daSBill Paul 	/* Init circular RX list. */
2094ce4946daSBill Paul 	if (nge_list_rx_init(sc) == ENOBUFS) {
20956b9f5c94SGleb Smirnoff 		device_printf(sc->nge_dev, "initialization failed: no "
2096646abee6SJohn Baldwin 			"memory for rx buffers\n");
2097ce4946daSBill Paul 		nge_stop(sc);
2098ce4946daSBill Paul 		return;
2099ce4946daSBill Paul 	}
2100ce4946daSBill Paul 
2101ce4946daSBill Paul 	/*
2102ce4946daSBill Paul 	 * Init tx descriptors.
2103ce4946daSBill Paul 	 */
2104ce4946daSBill Paul 	nge_list_tx_init(sc);
2105ce4946daSBill Paul 
2106f6bc9430SPyun YongHyeon 	/* Set Rx filter. */
2107f6bc9430SPyun YongHyeon 	nge_rxfilter(sc);
2108f6bc9430SPyun YongHyeon 
2109f6bc9430SPyun YongHyeon 	/* Disable PRIQ ctl. */
2110f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_PRIOQCTL, 0);
2111f6bc9430SPyun YongHyeon 
2112f6bc9430SPyun YongHyeon 	/*
2113453130d9SPedro F. Giffuni 	 * Set pause frames parameters.
2114f6bc9430SPyun YongHyeon 	 *  Rx stat FIFO hi-threshold : 2 or more packets
2115f6bc9430SPyun YongHyeon 	 *  Rx stat FIFO lo-threshold : less than 2 packets
2116f6bc9430SPyun YongHyeon 	 *  Rx data FIFO hi-threshold : 2K or more bytes
2117f6bc9430SPyun YongHyeon 	 *  Rx data FIFO lo-threshold : less than 2K bytes
2118f6bc9430SPyun YongHyeon 	 *  pause time : (512ns * 0xffff) -> 33.55ms
2119f6bc9430SPyun YongHyeon 	 */
2120f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_PAUSECSR,
2121f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_PAUSE_ON_MCAST |
2122f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_PAUSE_ON_DA |
2123f6bc9430SPyun YongHyeon 	    ((1 << 24) & NGE_PAUSECSR_RX_STATFIFO_THR_HI) |
2124f6bc9430SPyun YongHyeon 	    ((1 << 22) & NGE_PAUSECSR_RX_STATFIFO_THR_LO) |
2125f6bc9430SPyun YongHyeon 	    ((1 << 20) & NGE_PAUSECSR_RX_DATAFIFO_THR_HI) |
2126f6bc9430SPyun YongHyeon 	    ((1 << 18) & NGE_PAUSECSR_RX_DATAFIFO_THR_LO) |
2127f6bc9430SPyun YongHyeon 	    NGE_PAUSECSR_CNT);
2128f6bc9430SPyun YongHyeon 
2129ce4946daSBill Paul 	/*
2130ce4946daSBill Paul 	 * Load the address of the RX and TX lists.
2131ce4946daSBill Paul 	 */
2132f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI,
2133f6bc9430SPyun YongHyeon 	    NGE_ADDR_HI(sc->nge_rdata.nge_rx_ring_paddr));
2134f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO,
2135f6bc9430SPyun YongHyeon 	    NGE_ADDR_LO(sc->nge_rdata.nge_rx_ring_paddr));
2136f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI,
2137f6bc9430SPyun YongHyeon 	    NGE_ADDR_HI(sc->nge_rdata.nge_tx_ring_paddr));
2138f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO,
2139f6bc9430SPyun YongHyeon 	    NGE_ADDR_LO(sc->nge_rdata.nge_tx_ring_paddr));
2140ce4946daSBill Paul 
2141f6bc9430SPyun YongHyeon 	/* Set RX configuration. */
2142ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_RX_CFG, NGE_RXCFG);
2143f6bc9430SPyun YongHyeon 
2144f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_VLAN_IP_RXCTL, 0);
2145ce4946daSBill Paul 	/*
2146ce4946daSBill Paul 	 * Enable hardware checksum validation for all IPv4
2147ce4946daSBill Paul 	 * packets, do not reject packets with bad checksums.
2148ce4946daSBill Paul 	 */
2149f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
2150f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_IPCSUM_ENB);
2151ce4946daSBill Paul 
2152ce4946daSBill Paul 	/*
21539d4fe4b2SBrooks Davis 	 * Tell the chip to detect and strip VLAN tag info from
21549d4fe4b2SBrooks Davis 	 * received frames. The tag will be provided in the extsts
21559d4fe4b2SBrooks Davis 	 * field in the RX descriptors.
2156ce4946daSBill Paul 	 */
2157f6bc9430SPyun YongHyeon 	NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_DETECT_ENB);
2158f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
2159f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_VLAN_IP_RXCTL, NGE_VIPRXCTL_TAG_STRIP_ENB);
2160ce4946daSBill Paul 
2161f6bc9430SPyun YongHyeon 	/* Set TX configuration. */
2162ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_TX_CFG, NGE_TXCFG);
2163ce4946daSBill Paul 
2164ce4946daSBill Paul 	/*
2165ce4946daSBill Paul 	 * Enable TX IPv4 checksumming on a per-packet basis.
2166ce4946daSBill Paul 	 */
2167ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_CSUM_PER_PKT);
2168ce4946daSBill Paul 
2169ce4946daSBill Paul 	/*
21709d4fe4b2SBrooks Davis 	 * Tell the chip to insert VLAN tags on a per-packet basis as
21719d4fe4b2SBrooks Davis 	 * dictated by the code in the frame encapsulation routine.
2172ce4946daSBill Paul 	 */
2173ce4946daSBill Paul 	NGE_SETBIT(sc, NGE_VLAN_IP_TXCTL, NGE_VIPTXCTL_TAG_PER_PKT);
2174ce4946daSBill Paul 
2175ce4946daSBill Paul 	/*
2176ce4946daSBill Paul 	 * Enable the delivery of PHY interrupts based on
217723d3a203SBill Paul 	 * link/speed/duplex status changes. Also enable the
217823d3a203SBill Paul 	 * extsts field in the DMA descriptors (needed for
217923d3a203SBill Paul 	 * TCP/IP checksum offload on transmit).
2180ce4946daSBill Paul 	 */
21812ce0498bSBill Paul 	NGE_SETBIT(sc, NGE_CFG, NGE_CFG_PHYINTR_SPD |
218223d3a203SBill Paul 	    NGE_CFG_PHYINTR_LNK | NGE_CFG_PHYINTR_DUP | NGE_CFG_EXTSTS_ENB);
2183ce4946daSBill Paul 
2184ce4946daSBill Paul 	/*
2185962315f6SBill Paul 	 * Configure interrupt holdoff (moderation). We can
2186962315f6SBill Paul 	 * have the chip delay interrupt delivery for a certain
2187962315f6SBill Paul 	 * period. Units are in 100us, and the max setting
2188962315f6SBill Paul 	 * is 25500us (0xFF x 100us). Default is a 100us holdoff.
2189962315f6SBill Paul 	 */
2190f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IHR, sc->nge_int_holdoff);
2191f6bc9430SPyun YongHyeon 
2192f6bc9430SPyun YongHyeon 	/*
2193f6bc9430SPyun YongHyeon 	 * Enable MAC statistics counters and clear.
2194f6bc9430SPyun YongHyeon 	 */
2195f6bc9430SPyun YongHyeon 	reg = CSR_READ_4(sc, NGE_MIBCTL);
2196f6bc9430SPyun YongHyeon 	reg &= ~NGE_MIBCTL_FREEZE_CNT;
2197f6bc9430SPyun YongHyeon 	reg |= NGE_MIBCTL_CLEAR_CNT;
2198f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_MIBCTL, reg);
2199962315f6SBill Paul 
2200962315f6SBill Paul 	/*
2201ce4946daSBill Paul 	 * Enable interrupts.
2202ce4946daSBill Paul 	 */
2203ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IMR, NGE_INTRS);
2204196c0df6SHidetoshi Shimokawa #ifdef DEVICE_POLLING
2205196c0df6SHidetoshi Shimokawa 	/*
2206196c0df6SHidetoshi Shimokawa 	 * ... only enable interrupts if we are not polling, make sure
2207196c0df6SHidetoshi Shimokawa 	 * they are off otherwise.
2208196c0df6SHidetoshi Shimokawa 	 */
2209f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_POLLING) != 0)
2210196c0df6SHidetoshi Shimokawa 		CSR_WRITE_4(sc, NGE_IER, 0);
2211196c0df6SHidetoshi Shimokawa 	else
221240929967SGleb Smirnoff #endif
2213ce4946daSBill Paul 	CSR_WRITE_4(sc, NGE_IER, 1);
2214ce4946daSBill Paul 
2215f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
2216f6bc9430SPyun YongHyeon 	mii_mediachg(mii);
2217ce4946daSBill Paul 
2218f6bc9430SPyun YongHyeon 	sc->nge_watchdog_timer = 0;
2219f6bc9430SPyun YongHyeon 	callout_reset(&sc->nge_stat_ch, hz, nge_tick, sc);
2220ce4946daSBill Paul 
222113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
222213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2223ce4946daSBill Paul }
2224ce4946daSBill Paul 
2225ce4946daSBill Paul /*
2226ce4946daSBill Paul  * Set media options.
2227ce4946daSBill Paul  */
2228eaabec55SAlfred Perlstein static int
2229f6bc9430SPyun YongHyeon nge_mediachange(struct ifnet *ifp)
2230646abee6SJohn Baldwin {
2231646abee6SJohn Baldwin 	struct nge_softc *sc;
2232ce4946daSBill Paul 	struct mii_data	*mii;
2233f6bc9430SPyun YongHyeon 	struct mii_softc *miisc;
2234f6bc9430SPyun YongHyeon 	int error;
2235ce4946daSBill Paul 
2236ce4946daSBill Paul 	sc = ifp->if_softc;
2237f6bc9430SPyun YongHyeon 	NGE_LOCK(sc);
2238ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
2239646abee6SJohn Baldwin 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
22403fcb7a53SMarius Strobl 		PHY_RESET(miisc);
2241f6bc9430SPyun YongHyeon 	error = mii_mediachg(mii);
2242f6bc9430SPyun YongHyeon 	NGE_UNLOCK(sc);
2243f6bc9430SPyun YongHyeon 
2244f6bc9430SPyun YongHyeon 	return (error);
2245ce4946daSBill Paul }
2246ce4946daSBill Paul 
2247ce4946daSBill Paul /*
2248ce4946daSBill Paul  * Report current media status.
2249ce4946daSBill Paul  */
2250eaabec55SAlfred Perlstein static void
2251f6bc9430SPyun YongHyeon nge_mediastatus(struct ifnet *ifp, struct ifmediareq *ifmr)
2252ce4946daSBill Paul {
2253ce4946daSBill Paul 	struct nge_softc *sc;
2254ce4946daSBill Paul 	struct mii_data *mii;
2255ce4946daSBill Paul 
2256ce4946daSBill Paul 	sc = ifp->if_softc;
2257646abee6SJohn Baldwin 	NGE_LOCK(sc);
2258ce4946daSBill Paul 	mii = device_get_softc(sc->nge_miibus);
2259ce4946daSBill Paul 	mii_pollstat(mii);
2260ce4946daSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
2261ce4946daSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
226257c81d92SPyun YongHyeon 	NGE_UNLOCK(sc);
22631f548804SDoug Ambrisko }
2264ce4946daSBill Paul 
2265eaabec55SAlfred Perlstein static int
2266284c81cbSPyun YongHyeon nge_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2267ce4946daSBill Paul {
2268ce4946daSBill Paul 	struct nge_softc *sc = ifp->if_softc;
2269ce4946daSBill Paul 	struct ifreq *ifr = (struct ifreq *) data;
2270ce4946daSBill Paul 	struct mii_data *mii;
2271f6bc9430SPyun YongHyeon 	int error = 0, mask;
2272ce4946daSBill Paul 
2273ce4946daSBill Paul 	switch (command) {
2274ce4946daSBill Paul 	case SIOCSIFMTU:
2275f6bc9430SPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > NGE_JUMBO_MTU)
2276ce4946daSBill Paul 			error = EINVAL;
2277cb2f755cSBill Paul 		else {
2278646abee6SJohn Baldwin 			NGE_LOCK(sc);
2279ce4946daSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
2280cb2f755cSBill Paul 			/*
2281cb2f755cSBill Paul 			 * Workaround: if the MTU is larger than
2282cb2f755cSBill Paul 			 * 8152 (TX FIFO size minus 64 minus 18), turn off
2283cb2f755cSBill Paul 			 * TX checksum offloading.
2284cb2f755cSBill Paul 			 */
2285a5820ecbSYaroslav Tykhiy 			if (ifr->ifr_mtu >= 8152) {
2286a5820ecbSYaroslav Tykhiy 				ifp->if_capenable &= ~IFCAP_TXCSUM;
2287f6bc9430SPyun YongHyeon 				ifp->if_hwassist &= ~NGE_CSUM_FEATURES;
2288a5820ecbSYaroslav Tykhiy 			} else {
2289a5820ecbSYaroslav Tykhiy 				ifp->if_capenable |= IFCAP_TXCSUM;
2290f6bc9430SPyun YongHyeon 				ifp->if_hwassist |= NGE_CSUM_FEATURES;
2291cb2f755cSBill Paul 			}
2292646abee6SJohn Baldwin 			NGE_UNLOCK(sc);
2293f6bc9430SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
2294a5820ecbSYaroslav Tykhiy 		}
2295ce4946daSBill Paul 		break;
2296ce4946daSBill Paul 	case SIOCSIFFLAGS:
2297ad6c618bSBill Paul 		NGE_LOCK(sc);
2298f6bc9430SPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
2299f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2300f6bc9430SPyun YongHyeon 				if ((ifp->if_flags ^ sc->nge_if_flags) &
2301f6bc9430SPyun YongHyeon 				    (IFF_PROMISC | IFF_ALLMULTI))
2302f6bc9430SPyun YongHyeon 					nge_rxfilter(sc);
2303ce4946daSBill Paul 			} else {
2304f6bc9430SPyun YongHyeon 				if ((sc->nge_flags & NGE_FLAG_DETACH) == 0)
2305ad6c618bSBill Paul 					nge_init_locked(sc);
2306ce4946daSBill Paul 			}
2307ce4946daSBill Paul 		} else {
2308f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2309ce4946daSBill Paul 				nge_stop(sc);
2310ce4946daSBill Paul 		}
2311ce4946daSBill Paul 		sc->nge_if_flags = ifp->if_flags;
2312ad6c618bSBill Paul 		NGE_UNLOCK(sc);
2313ce4946daSBill Paul 		error = 0;
2314ce4946daSBill Paul 		break;
2315ce4946daSBill Paul 	case SIOCADDMULTI:
2316ce4946daSBill Paul 	case SIOCDELMULTI:
2317ad6c618bSBill Paul 		NGE_LOCK(sc);
2318a2621dd2SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
2319f6bc9430SPyun YongHyeon 			nge_rxfilter(sc);
2320ad6c618bSBill Paul 		NGE_UNLOCK(sc);
2321ce4946daSBill Paul 		break;
2322ce4946daSBill Paul 	case SIOCGIFMEDIA:
2323ce4946daSBill Paul 	case SIOCSIFMEDIA:
2324ce4946daSBill Paul 		mii = device_get_softc(sc->nge_miibus);
2325f6bc9430SPyun YongHyeon 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
2326ce4946daSBill Paul 		break;
232737f5f239SRuslan Ermilov 	case SIOCSIFCAP:
232840929967SGleb Smirnoff 		NGE_LOCK(sc);
2329f6bc9430SPyun YongHyeon 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2330f6bc9430SPyun YongHyeon #ifdef DEVICE_POLLING
2331f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_POLLING) != 0 &&
2332f6bc9430SPyun YongHyeon 		    (IFCAP_POLLING & ifp->if_capabilities) != 0) {
2333f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_POLLING;
2334f6bc9430SPyun YongHyeon 			if ((IFCAP_POLLING & ifp->if_capenable) != 0) {
2335f6bc9430SPyun YongHyeon 				error = ether_poll_register(nge_poll, ifp);
2336f6bc9430SPyun YongHyeon 				if (error != 0) {
233740929967SGleb Smirnoff 					NGE_UNLOCK(sc);
2338f6bc9430SPyun YongHyeon 					break;
233940929967SGleb Smirnoff 				}
2340f6bc9430SPyun YongHyeon 				/* Disable interrupts. */
2341f6bc9430SPyun YongHyeon 				CSR_WRITE_4(sc, NGE_IER, 0);
2342f6bc9430SPyun YongHyeon 			} else {
234340929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
234440929967SGleb Smirnoff 				/* Enable interrupts. */
234540929967SGleb Smirnoff 				CSR_WRITE_4(sc, NGE_IER, 1);
2346f6bc9430SPyun YongHyeon 			}
234740929967SGleb Smirnoff 		}
234840929967SGleb Smirnoff #endif /* DEVICE_POLLING */
2349f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
2350f6bc9430SPyun YongHyeon 		    (IFCAP_TXCSUM & ifp->if_capabilities) != 0) {
2351f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
2352f6bc9430SPyun YongHyeon 			if ((IFCAP_TXCSUM & ifp->if_capenable) != 0)
2353f6bc9430SPyun YongHyeon 				ifp->if_hwassist |= NGE_CSUM_FEATURES;
2354f6bc9430SPyun YongHyeon 			else
2355f6bc9430SPyun YongHyeon 				ifp->if_hwassist &= ~NGE_CSUM_FEATURES;
2356f6bc9430SPyun YongHyeon 		}
2357f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
2358f6bc9430SPyun YongHyeon 		    (IFCAP_RXCSUM & ifp->if_capabilities) != 0)
2359f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
2360f6bc9430SPyun YongHyeon 
2361f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
2362f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
2363f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
2364f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
2365f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
2366f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
2367f6bc9430SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
2368f6bc9430SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2369f6bc9430SPyun YongHyeon 		}
2370f6bc9430SPyun YongHyeon 
2371f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWCSUM) != 0 &&
2372f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWCSUM) != 0)
2373f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWCSUM;
2374f6bc9430SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
2375f6bc9430SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
2376f6bc9430SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2377f6bc9430SPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2378f6bc9430SPyun YongHyeon 				if ((ifp->if_capenable &
2379f6bc9430SPyun YongHyeon 				    IFCAP_VLAN_HWTAGGING) != 0)
2380f6bc9430SPyun YongHyeon 					NGE_SETBIT(sc,
2381f6bc9430SPyun YongHyeon 					    NGE_VLAN_IP_RXCTL,
2382f6bc9430SPyun YongHyeon 					    NGE_VIPRXCTL_TAG_STRIP_ENB);
2383f6bc9430SPyun YongHyeon 				else
2384f6bc9430SPyun YongHyeon 					NGE_CLRBIT(sc,
2385f6bc9430SPyun YongHyeon 					    NGE_VLAN_IP_RXCTL,
2386f6bc9430SPyun YongHyeon 					    NGE_VIPRXCTL_TAG_STRIP_ENB);
2387f6bc9430SPyun YongHyeon 			}
2388f6bc9430SPyun YongHyeon 		}
2389f6bc9430SPyun YongHyeon 		/*
2390f6bc9430SPyun YongHyeon 		 * Both VLAN hardware tagging and checksum offload is
2391f6bc9430SPyun YongHyeon 		 * required to do checksum offload on VLAN interface.
2392f6bc9430SPyun YongHyeon 		 */
2393f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_TXCSUM) == 0)
2394f6bc9430SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM;
2395f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
2396f6bc9430SPyun YongHyeon 			ifp->if_capenable &= ~IFCAP_VLAN_HWCSUM;
2397f6bc9430SPyun YongHyeon 		NGE_UNLOCK(sc);
2398f6bc9430SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
239937f5f239SRuslan Ermilov 		break;
2400ce4946daSBill Paul 	default:
2401673d9191SSam Leffler 		error = ether_ioctl(ifp, command, data);
2402ce4946daSBill Paul 		break;
2403ce4946daSBill Paul 	}
2404ce4946daSBill Paul 
2405ce4946daSBill Paul 	return (error);
2406ce4946daSBill Paul }
2407ce4946daSBill Paul 
2408eaabec55SAlfred Perlstein static void
2409f6bc9430SPyun YongHyeon nge_watchdog(struct nge_softc *sc)
2410ce4946daSBill Paul {
2411f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2412ce4946daSBill Paul 
2413f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2414ce4946daSBill Paul 
2415f6bc9430SPyun YongHyeon 	if (sc->nge_watchdog_timer == 0 || --sc->nge_watchdog_timer)
2416f6bc9430SPyun YongHyeon 		return;
2417f6bc9430SPyun YongHyeon 
2418f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
2419e09fdb02SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
24206b9f5c94SGleb Smirnoff 	if_printf(ifp, "watchdog timeout\n");
2421ce4946daSBill Paul 
242213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2423ad6c618bSBill Paul 	nge_init_locked(sc);
2424ce4946daSBill Paul 
2425f6bc9430SPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2426ad6c618bSBill Paul 		nge_start_locked(ifp);
2427f6bc9430SPyun YongHyeon }
2428ad6c618bSBill Paul 
2429f6bc9430SPyun YongHyeon static int
2430f6bc9430SPyun YongHyeon nge_stop_mac(struct nge_softc *sc)
2431f6bc9430SPyun YongHyeon {
2432f6bc9430SPyun YongHyeon 	uint32_t reg;
2433f6bc9430SPyun YongHyeon 	int i;
2434f6bc9430SPyun YongHyeon 
2435f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2436f6bc9430SPyun YongHyeon 
2437f6bc9430SPyun YongHyeon 	reg = CSR_READ_4(sc, NGE_CSR);
2438f6bc9430SPyun YongHyeon 	if ((reg & (NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE)) != 0) {
2439f6bc9430SPyun YongHyeon 		reg &= ~(NGE_CSR_TX_ENABLE | NGE_CSR_RX_ENABLE);
2440f6bc9430SPyun YongHyeon 		reg |= NGE_CSR_TX_DISABLE | NGE_CSR_RX_DISABLE;
2441f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CSR, reg);
2442f6bc9430SPyun YongHyeon 		for (i = 0; i < NGE_TIMEOUT; i++) {
2443f6bc9430SPyun YongHyeon 			DELAY(1);
2444f6bc9430SPyun YongHyeon 			if ((CSR_READ_4(sc, NGE_CSR) &
2445f6bc9430SPyun YongHyeon 			    (NGE_CSR_RX_ENABLE | NGE_CSR_TX_ENABLE)) == 0)
2446f6bc9430SPyun YongHyeon 				break;
2447f6bc9430SPyun YongHyeon 		}
2448f6bc9430SPyun YongHyeon 		if (i == NGE_TIMEOUT)
2449f6bc9430SPyun YongHyeon 			return (ETIMEDOUT);
2450f6bc9430SPyun YongHyeon 	}
2451f6bc9430SPyun YongHyeon 
2452f6bc9430SPyun YongHyeon 	return (0);
2453ce4946daSBill Paul }
2454ce4946daSBill Paul 
2455ce4946daSBill Paul /*
2456ce4946daSBill Paul  * Stop the adapter and free any mbufs allocated to the
2457ce4946daSBill Paul  * RX and TX lists.
2458ce4946daSBill Paul  */
2459eaabec55SAlfred Perlstein static void
2460284c81cbSPyun YongHyeon nge_stop(struct nge_softc *sc)
2461ce4946daSBill Paul {
2462f6bc9430SPyun YongHyeon 	struct nge_txdesc *txd;
2463f6bc9430SPyun YongHyeon 	struct nge_rxdesc *rxd;
24642cf2d799SPyun YongHyeon 	int i;
2465ce4946daSBill Paul 	struct ifnet *ifp;
2466ce4946daSBill Paul 
2467ad6c618bSBill Paul 	NGE_LOCK_ASSERT(sc);
2468fc74a9f9SBrooks Davis 	ifp = sc->nge_ifp;
2469ce4946daSBill Paul 
247013f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
2471f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_LINK;
2472f6bc9430SPyun YongHyeon 	callout_stop(&sc->nge_stat_ch);
2473f6bc9430SPyun YongHyeon 	sc->nge_watchdog_timer = 0;
2474f6bc9430SPyun YongHyeon 
2475f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IER, 0);
2476f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_IMR, 0);
2477f6bc9430SPyun YongHyeon 	if (nge_stop_mac(sc) == ETIMEDOUT)
2478f6bc9430SPyun YongHyeon 		device_printf(sc->nge_dev,
2479f6bc9430SPyun YongHyeon 		   "%s: unable to stop Tx/Rx MAC\n", __func__);
2480f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_HI, 0);
2481f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_TX_LISTPTR_LO, 0);
2482f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0);
2483f6bc9430SPyun YongHyeon 	CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0);
2484f6bc9430SPyun YongHyeon 	nge_stats_update(sc);
2485f6bc9430SPyun YongHyeon 	if (sc->nge_head != NULL) {
2486f6bc9430SPyun YongHyeon 		m_freem(sc->nge_head);
2487f6bc9430SPyun YongHyeon 		sc->nge_head = sc->nge_tail = NULL;
2488f6bc9430SPyun YongHyeon 	}
2489f6bc9430SPyun YongHyeon 
2490f6bc9430SPyun YongHyeon 	/*
2491f6bc9430SPyun YongHyeon 	 * Free RX and TX mbufs still in the queues.
2492f6bc9430SPyun YongHyeon 	 */
2493f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_RX_RING_CNT; i++) {
2494f6bc9430SPyun YongHyeon 		rxd = &sc->nge_cdata.nge_rxdesc[i];
2495f6bc9430SPyun YongHyeon 		if (rxd->rx_m != NULL) {
2496f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_rx_tag,
2497f6bc9430SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
2498f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_rx_tag,
2499f6bc9430SPyun YongHyeon 			    rxd->rx_dmamap);
2500f6bc9430SPyun YongHyeon 			m_freem(rxd->rx_m);
2501f6bc9430SPyun YongHyeon 			rxd->rx_m = NULL;
2502f6bc9430SPyun YongHyeon 		}
2503f6bc9430SPyun YongHyeon 	}
2504f6bc9430SPyun YongHyeon 	for (i = 0; i < NGE_TX_RING_CNT; i++) {
2505f6bc9430SPyun YongHyeon 		txd = &sc->nge_cdata.nge_txdesc[i];
2506f6bc9430SPyun YongHyeon 		if (txd->tx_m != NULL) {
2507f6bc9430SPyun YongHyeon 			bus_dmamap_sync(sc->nge_cdata.nge_tx_tag,
2508f6bc9430SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2509f6bc9430SPyun YongHyeon 			bus_dmamap_unload(sc->nge_cdata.nge_tx_tag,
2510f6bc9430SPyun YongHyeon 			    txd->tx_dmamap);
2511f6bc9430SPyun YongHyeon 			m_freem(txd->tx_m);
2512f6bc9430SPyun YongHyeon 			txd->tx_m = NULL;
2513f6bc9430SPyun YongHyeon 		}
2514f6bc9430SPyun YongHyeon 	}
2515f6bc9430SPyun YongHyeon }
2516f6bc9430SPyun YongHyeon 
2517f6bc9430SPyun YongHyeon /*
2518f6bc9430SPyun YongHyeon  * Before setting WOL bits, caller should have stopped Receiver.
2519f6bc9430SPyun YongHyeon  */
2520f6bc9430SPyun YongHyeon static void
2521f6bc9430SPyun YongHyeon nge_wol(struct nge_softc *sc)
2522f6bc9430SPyun YongHyeon {
2523f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2524f6bc9430SPyun YongHyeon 	uint32_t reg;
2525f6bc9430SPyun YongHyeon 	uint16_t pmstat;
2526f6bc9430SPyun YongHyeon 	int pmc;
2527f6bc9430SPyun YongHyeon 
2528f6bc9430SPyun YongHyeon 	NGE_LOCK_ASSERT(sc);
2529f6bc9430SPyun YongHyeon 
25303b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &pmc) != 0)
2531f6bc9430SPyun YongHyeon 		return;
2532f6bc9430SPyun YongHyeon 
2533f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
2534f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) == 0) {
2535f6bc9430SPyun YongHyeon 		/* Disable WOL & disconnect CLKRUN to save power. */
2536f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_WOLCSR, 0);
2537f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CLKRUN, 0);
2538f6bc9430SPyun YongHyeon 	} else {
2539f6bc9430SPyun YongHyeon 		if (nge_stop_mac(sc) == ETIMEDOUT)
2540f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
2541f6bc9430SPyun YongHyeon 			    "%s: unable to stop Tx/Rx MAC\n", __func__);
2542f6bc9430SPyun YongHyeon 		/*
2543f6bc9430SPyun YongHyeon 		 * Make sure wake frames will be buffered in the Rx FIFO.
2544f6bc9430SPyun YongHyeon 		 * (i.e. Silent Rx mode.)
2545f6bc9430SPyun YongHyeon 		 */
2546f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_HI, 0);
25478c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_RX_LISTPTR_HI, BUS_SPACE_BARRIER_WRITE);
2548f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_RX_LISTPTR_LO, 0);
25498c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_RX_LISTPTR_LO, BUS_SPACE_BARRIER_WRITE);
2550f6bc9430SPyun YongHyeon 		/* Enable Rx again. */
2551f6bc9430SPyun YongHyeon 		NGE_SETBIT(sc, NGE_CSR, NGE_CSR_RX_ENABLE);
25528c1093fcSMarius Strobl 		CSR_BARRIER_4(sc, NGE_CSR, BUS_SPACE_BARRIER_WRITE);
2553f6bc9430SPyun YongHyeon 
2554f6bc9430SPyun YongHyeon 		/* Configure WOL events. */
2555f6bc9430SPyun YongHyeon 		reg = 0;
2556f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
2557f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_UNICAST;
2558f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
2559f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_MULTICAST;
2560f6bc9430SPyun YongHyeon 		if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
2561f6bc9430SPyun YongHyeon 			reg |= NGE_WOLCSR_WAKE_ON_MAGICPKT;
2562f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_WOLCSR, reg);
2563f6bc9430SPyun YongHyeon 
2564f6bc9430SPyun YongHyeon 		/* Activate CLKRUN. */
2565f6bc9430SPyun YongHyeon 		reg = CSR_READ_4(sc, NGE_CLKRUN);
2566f6bc9430SPyun YongHyeon 		reg |= NGE_CLKRUN_PMEENB | NGE_CLNRUN_CLKRUN_ENB;
2567f6bc9430SPyun YongHyeon 		CSR_WRITE_4(sc, NGE_CLKRUN, reg);
2568f6bc9430SPyun YongHyeon 	}
2569f6bc9430SPyun YongHyeon 
2570f6bc9430SPyun YongHyeon 	/* Request PME. */
2571f6bc9430SPyun YongHyeon 	pmstat = pci_read_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, 2);
2572f6bc9430SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
2573f6bc9430SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
2574f6bc9430SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
2575f6bc9430SPyun YongHyeon 	pci_write_config(sc->nge_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
2576ce4946daSBill Paul }
2577ce4946daSBill Paul 
2578ce4946daSBill Paul /*
2579ce4946daSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
2580ce4946daSBill Paul  * get confused by errant DMAs when rebooting.
2581ce4946daSBill Paul  */
25826a087a87SPyun YongHyeon static int
2583284c81cbSPyun YongHyeon nge_shutdown(device_t dev)
2584ce4946daSBill Paul {
2585f6bc9430SPyun YongHyeon 
2586f6bc9430SPyun YongHyeon 	return (nge_suspend(dev));
2587f6bc9430SPyun YongHyeon }
2588f6bc9430SPyun YongHyeon 
2589f6bc9430SPyun YongHyeon static int
2590f6bc9430SPyun YongHyeon nge_suspend(device_t dev)
2591f6bc9430SPyun YongHyeon {
2592ce4946daSBill Paul 	struct nge_softc *sc;
2593ce4946daSBill Paul 
2594ce4946daSBill Paul 	sc = device_get_softc(dev);
2595ce4946daSBill Paul 
2596ad6c618bSBill Paul 	NGE_LOCK(sc);
2597ce4946daSBill Paul 	nge_stop(sc);
2598f6bc9430SPyun YongHyeon 	nge_wol(sc);
2599f6bc9430SPyun YongHyeon 	sc->nge_flags |= NGE_FLAG_SUSPENDED;
2600ad6c618bSBill Paul 	NGE_UNLOCK(sc);
2601ce4946daSBill Paul 
26026a087a87SPyun YongHyeon 	return (0);
2603ce4946daSBill Paul }
2604f6bc9430SPyun YongHyeon 
2605f6bc9430SPyun YongHyeon static int
2606f6bc9430SPyun YongHyeon nge_resume(device_t dev)
2607f6bc9430SPyun YongHyeon {
2608f6bc9430SPyun YongHyeon 	struct nge_softc *sc;
2609f6bc9430SPyun YongHyeon 	struct ifnet *ifp;
2610f6bc9430SPyun YongHyeon 	uint16_t pmstat;
2611f6bc9430SPyun YongHyeon 	int pmc;
2612f6bc9430SPyun YongHyeon 
2613f6bc9430SPyun YongHyeon 	sc = device_get_softc(dev);
2614f6bc9430SPyun YongHyeon 
2615f6bc9430SPyun YongHyeon 	NGE_LOCK(sc);
2616f6bc9430SPyun YongHyeon 	ifp = sc->nge_ifp;
26173b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->nge_dev, PCIY_PMG, &pmc) == 0) {
2618f6bc9430SPyun YongHyeon 		/* Disable PME and clear PME status. */
2619f6bc9430SPyun YongHyeon 		pmstat = pci_read_config(sc->nge_dev,
2620f6bc9430SPyun YongHyeon 		    pmc + PCIR_POWER_STATUS, 2);
2621f6bc9430SPyun YongHyeon 		if ((pmstat & PCIM_PSTAT_PMEENABLE) != 0) {
2622f6bc9430SPyun YongHyeon 			pmstat &= ~PCIM_PSTAT_PMEENABLE;
2623f6bc9430SPyun YongHyeon 			pci_write_config(sc->nge_dev,
2624f6bc9430SPyun YongHyeon 			    pmc + PCIR_POWER_STATUS, pmstat, 2);
2625f6bc9430SPyun YongHyeon 		}
2626f6bc9430SPyun YongHyeon 	}
2627f6bc9430SPyun YongHyeon 	if (ifp->if_flags & IFF_UP) {
2628f6bc9430SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2629f6bc9430SPyun YongHyeon 		nge_init_locked(sc);
2630f6bc9430SPyun YongHyeon 	}
2631f6bc9430SPyun YongHyeon 
2632f6bc9430SPyun YongHyeon 	sc->nge_flags &= ~NGE_FLAG_SUSPENDED;
2633f6bc9430SPyun YongHyeon 	NGE_UNLOCK(sc);
2634f6bc9430SPyun YongHyeon 
2635f6bc9430SPyun YongHyeon 	return (0);
2636f6bc9430SPyun YongHyeon }
2637f6bc9430SPyun YongHyeon 
2638f6bc9430SPyun YongHyeon #define	NGE_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
2639f6bc9430SPyun YongHyeon 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
2640f6bc9430SPyun YongHyeon 
2641f6bc9430SPyun YongHyeon static void
2642f6bc9430SPyun YongHyeon nge_sysctl_node(struct nge_softc *sc)
2643f6bc9430SPyun YongHyeon {
2644f6bc9430SPyun YongHyeon 	struct sysctl_ctx_list *ctx;
2645f6bc9430SPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
2646f6bc9430SPyun YongHyeon 	struct sysctl_oid *tree;
2647f6bc9430SPyun YongHyeon 	struct nge_stats *stats;
2648f6bc9430SPyun YongHyeon 	int error;
2649f6bc9430SPyun YongHyeon 
2650f6bc9430SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->nge_dev);
2651f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->nge_dev));
2652f6bc9430SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "int_holdoff",
26537029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, &sc->nge_int_holdoff,
26547029da5cSPawel Biernacki 	    0, sysctl_hw_nge_int_holdoff, "I", "NGE interrupt moderation");
2655f6bc9430SPyun YongHyeon 	/* Pull in device tunables. */
2656f6bc9430SPyun YongHyeon 	sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT;
2657f6bc9430SPyun YongHyeon 	error = resource_int_value(device_get_name(sc->nge_dev),
2658f6bc9430SPyun YongHyeon 	    device_get_unit(sc->nge_dev), "int_holdoff", &sc->nge_int_holdoff);
2659f6bc9430SPyun YongHyeon 	if (error == 0) {
2660f6bc9430SPyun YongHyeon 		if (sc->nge_int_holdoff < NGE_INT_HOLDOFF_MIN ||
2661f6bc9430SPyun YongHyeon 		    sc->nge_int_holdoff > NGE_INT_HOLDOFF_MAX ) {
2662f6bc9430SPyun YongHyeon 			device_printf(sc->nge_dev,
2663f6bc9430SPyun YongHyeon 			    "int_holdoff value out of range; "
2664f6bc9430SPyun YongHyeon 			    "using default: %d(%d us)\n",
2665f6bc9430SPyun YongHyeon 			    NGE_INT_HOLDOFF_DEFAULT,
2666f6bc9430SPyun YongHyeon 			    NGE_INT_HOLDOFF_DEFAULT * 100);
2667f6bc9430SPyun YongHyeon 			sc->nge_int_holdoff = NGE_INT_HOLDOFF_DEFAULT;
2668f6bc9430SPyun YongHyeon 		}
2669f6bc9430SPyun YongHyeon 	}
2670f6bc9430SPyun YongHyeon 
2671f6bc9430SPyun YongHyeon 	stats = &sc->nge_stats;
26727029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats",
26737029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NGE statistics");
2674f6bc9430SPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
2675f6bc9430SPyun YongHyeon 
2676f6bc9430SPyun YongHyeon 	/* Rx statistics. */
26777029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx",
26787029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Rx MAC statistics");
2679f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
2680f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_errs",
2681f6bc9430SPyun YongHyeon 	    &stats->rx_pkts_errs,
2682f6bc9430SPyun YongHyeon 	    "Packet errors including both wire errors and FIFO overruns");
2683f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "crc_errs",
2684f6bc9430SPyun YongHyeon 	    &stats->rx_crc_errs, "CRC errors");
2685f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "fifo_oflows",
2686f6bc9430SPyun YongHyeon 	    &stats->rx_fifo_oflows, "FIFO overflows");
2687f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "align_errs",
2688f6bc9430SPyun YongHyeon 	    &stats->rx_align_errs, "Frame alignment errors");
2689f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "sym_errs",
2690f6bc9430SPyun YongHyeon 	    &stats->rx_sym_errs, "One or more symbol errors");
2691f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pkts_jumbos",
2692f6bc9430SPyun YongHyeon 	    &stats->rx_pkts_jumbos,
2693f6bc9430SPyun YongHyeon 	    "Packets received with length greater than 1518 bytes");
2694f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "len_errs",
2695f6bc9430SPyun YongHyeon 	    &stats->rx_len_errs, "In Range Length errors");
2696f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "unctl_frames",
2697f6bc9430SPyun YongHyeon 	    &stats->rx_unctl_frames, "Control frames with unsupported opcode");
2698f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pause",
2699f6bc9430SPyun YongHyeon 	    &stats->rx_pause, "Pause frames");
2700f6bc9430SPyun YongHyeon 
2701f6bc9430SPyun YongHyeon 	/* Tx statistics. */
27027029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx",
27037029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Tx MAC statistics");
2704f6bc9430SPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
2705f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "pause",
2706f6bc9430SPyun YongHyeon 	    &stats->tx_pause, "Pause frames");
2707f6bc9430SPyun YongHyeon 	NGE_SYSCTL_STAT_ADD32(ctx, child, "seq_errs",
2708f6bc9430SPyun YongHyeon 	    &stats->tx_seq_errs,
2709f6bc9430SPyun YongHyeon 	    "Loss of collision heartbeat during transmission");
2710f6bc9430SPyun YongHyeon }
2711f6bc9430SPyun YongHyeon 
2712f6bc9430SPyun YongHyeon #undef NGE_SYSCTL_STAT_ADD32
2713f6bc9430SPyun YongHyeon 
2714f6bc9430SPyun YongHyeon static int
2715f6bc9430SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
2716f6bc9430SPyun YongHyeon {
2717f6bc9430SPyun YongHyeon 	int error, value;
2718f6bc9430SPyun YongHyeon 
2719f6bc9430SPyun YongHyeon 	if (arg1 == NULL)
2720f6bc9430SPyun YongHyeon 		return (EINVAL);
2721f6bc9430SPyun YongHyeon 	value = *(int *)arg1;
2722f6bc9430SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
2723f6bc9430SPyun YongHyeon 	if (error != 0 || req->newptr == NULL)
2724f6bc9430SPyun YongHyeon 		return (error);
2725f6bc9430SPyun YongHyeon 	if (value < low || value > high)
2726f6bc9430SPyun YongHyeon 		return (EINVAL);
2727f6bc9430SPyun YongHyeon 	*(int *)arg1 = value;
2728f6bc9430SPyun YongHyeon 
2729f6bc9430SPyun YongHyeon 	return (0);
2730f6bc9430SPyun YongHyeon }
2731f6bc9430SPyun YongHyeon 
2732f6bc9430SPyun YongHyeon static int
2733f6bc9430SPyun YongHyeon sysctl_hw_nge_int_holdoff(SYSCTL_HANDLER_ARGS)
2734f6bc9430SPyun YongHyeon {
2735f6bc9430SPyun YongHyeon 
2736f6bc9430SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, NGE_INT_HOLDOFF_MIN,
2737f6bc9430SPyun YongHyeon 	    NGE_INT_HOLDOFF_MAX));
2738f6bc9430SPyun YongHyeon }
2739