xref: /freebsd/sys/dev/nfe/if_nfe.c (revision ddaf6524682b3ab9e50f7575db319814dbbd053a)
1bfc788c2SDavid E. O'Brien /*	$OpenBSD: if_nfe.c,v 1.54 2006/04/07 12:38:12 jsg Exp $	*/
2257c5577SDavid E. O'Brien 
3257c5577SDavid E. O'Brien /*-
4bfc788c2SDavid E. O'Brien  * Copyright (c) 2006 Shigeaki Tagashira <shigeaki@se.hiroshima-u.ac.jp>
5257c5577SDavid E. O'Brien  * Copyright (c) 2006 Damien Bergamini <damien.bergamini@free.fr>
6257c5577SDavid E. O'Brien  * Copyright (c) 2005, 2006 Jonathan Gray <jsg@openbsd.org>
7257c5577SDavid E. O'Brien  *
8257c5577SDavid E. O'Brien  * Permission to use, copy, modify, and distribute this software for any
9257c5577SDavid E. O'Brien  * purpose with or without fee is hereby granted, provided that the above
10257c5577SDavid E. O'Brien  * copyright notice and this permission notice appear in all copies.
11257c5577SDavid E. O'Brien  *
12257c5577SDavid E. O'Brien  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13257c5577SDavid E. O'Brien  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14257c5577SDavid E. O'Brien  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15257c5577SDavid E. O'Brien  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16257c5577SDavid E. O'Brien  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17257c5577SDavid E. O'Brien  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18257c5577SDavid E. O'Brien  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19257c5577SDavid E. O'Brien  */
20257c5577SDavid E. O'Brien 
21257c5577SDavid E. O'Brien /* Driver for NVIDIA nForce MCP Fast Ethernet and Gigabit Ethernet */
22257c5577SDavid E. O'Brien 
23bfc788c2SDavid E. O'Brien #include <sys/cdefs.h>
24bfc788c2SDavid E. O'Brien #ifdef HAVE_KERNEL_OPTION_HEADERS
25bfc788c2SDavid E. O'Brien #include "opt_device_polling.h"
26bfc788c2SDavid E. O'Brien #endif
27257c5577SDavid E. O'Brien 
28257c5577SDavid E. O'Brien #include <sys/param.h>
29257c5577SDavid E. O'Brien #include <sys/endian.h>
30257c5577SDavid E. O'Brien #include <sys/systm.h>
31257c5577SDavid E. O'Brien #include <sys/sockio.h>
32257c5577SDavid E. O'Brien #include <sys/mbuf.h>
33257c5577SDavid E. O'Brien #include <sys/malloc.h>
34bfc788c2SDavid E. O'Brien #include <sys/module.h>
35257c5577SDavid E. O'Brien #include <sys/kernel.h>
36aab5582fSPyun YongHyeon #include <sys/queue.h>
37257c5577SDavid E. O'Brien #include <sys/socket.h>
38aab5582fSPyun YongHyeon #include <sys/sysctl.h>
39bfc788c2SDavid E. O'Brien #include <sys/taskqueue.h>
40257c5577SDavid E. O'Brien 
41257c5577SDavid E. O'Brien #include <net/if.h>
4276039bc8SGleb Smirnoff #include <net/if_var.h>
43bfc788c2SDavid E. O'Brien #include <net/if_arp.h>
44bfc788c2SDavid E. O'Brien #include <net/ethernet.h>
45257c5577SDavid E. O'Brien #include <net/if_dl.h>
46257c5577SDavid E. O'Brien #include <net/if_media.h>
47257c5577SDavid E. O'Brien #include <net/if_types.h>
48257c5577SDavid E. O'Brien #include <net/if_vlan_var.h>
49257c5577SDavid E. O'Brien 
50257c5577SDavid E. O'Brien #include <net/bpf.h>
51bfc788c2SDavid E. O'Brien 
52bfc788c2SDavid E. O'Brien #include <machine/bus.h>
53bfc788c2SDavid E. O'Brien #include <machine/resource.h>
54bfc788c2SDavid E. O'Brien #include <sys/bus.h>
55bfc788c2SDavid E. O'Brien #include <sys/rman.h>
56257c5577SDavid E. O'Brien 
57257c5577SDavid E. O'Brien #include <dev/mii/mii.h>
58257c5577SDavid E. O'Brien #include <dev/mii/miivar.h>
59257c5577SDavid E. O'Brien 
60257c5577SDavid E. O'Brien #include <dev/pci/pcireg.h>
61257c5577SDavid E. O'Brien #include <dev/pci/pcivar.h>
62257c5577SDavid E. O'Brien 
63bfc788c2SDavid E. O'Brien #include <dev/nfe/if_nfereg.h>
64bfc788c2SDavid E. O'Brien #include <dev/nfe/if_nfevar.h>
65257c5577SDavid E. O'Brien 
66bfc788c2SDavid E. O'Brien MODULE_DEPEND(nfe, pci, 1, 1, 1);
67bfc788c2SDavid E. O'Brien MODULE_DEPEND(nfe, ether, 1, 1, 1);
68bfc788c2SDavid E. O'Brien MODULE_DEPEND(nfe, miibus, 1, 1, 1);
69aab5582fSPyun YongHyeon 
70aab5582fSPyun YongHyeon /* "device miibus" required.  See GENERIC if you get errors here. */
71bfc788c2SDavid E. O'Brien #include "miibus_if.h"
72257c5577SDavid E. O'Brien 
73bfc788c2SDavid E. O'Brien static int  nfe_probe(device_t);
74bfc788c2SDavid E. O'Brien static int  nfe_attach(device_t);
75bfc788c2SDavid E. O'Brien static int  nfe_detach(device_t);
76aab5582fSPyun YongHyeon static int  nfe_suspend(device_t);
77aab5582fSPyun YongHyeon static int  nfe_resume(device_t);
786a087a87SPyun YongHyeon static int nfe_shutdown(device_t);
797e7a45ceSPyun YongHyeon static int  nfe_can_use_msix(struct nfe_softc *);
80a173db2dSPyun YongHyeon static int  nfe_detect_msik9(struct nfe_softc *);
81aab5582fSPyun YongHyeon static void nfe_power(struct nfe_softc *);
82bfc788c2SDavid E. O'Brien static int  nfe_miibus_readreg(device_t, int, int);
83bfc788c2SDavid E. O'Brien static int  nfe_miibus_writereg(device_t, int, int, int);
84bfc788c2SDavid E. O'Brien static void nfe_miibus_statchg(device_t);
8552a1393eSPyun YongHyeon static void nfe_mac_config(struct nfe_softc *, struct mii_data *);
86aab5582fSPyun YongHyeon static void nfe_set_intr(struct nfe_softc *);
87aab5582fSPyun YongHyeon static __inline void nfe_enable_intr(struct nfe_softc *);
88aab5582fSPyun YongHyeon static __inline void nfe_disable_intr(struct nfe_softc *);
89c2df7334SMarcel Moolenaar static int  nfe_ioctl(if_t, u_long, caddr_t);
90aab5582fSPyun YongHyeon static void nfe_alloc_msix(struct nfe_softc *, int);
91aab5582fSPyun YongHyeon static int nfe_intr(void *);
92aab5582fSPyun YongHyeon static void nfe_int_task(void *, int);
93aab5582fSPyun YongHyeon static __inline void nfe_discard_rxbuf(struct nfe_softc *, int);
94aab5582fSPyun YongHyeon static __inline void nfe_discard_jrxbuf(struct nfe_softc *, int);
95aab5582fSPyun YongHyeon static int nfe_newbuf(struct nfe_softc *, int);
96aab5582fSPyun YongHyeon static int nfe_jnewbuf(struct nfe_softc *, int);
971abcdbd1SAttilio Rao static int  nfe_rxeof(struct nfe_softc *, int, int *);
981abcdbd1SAttilio Rao static int  nfe_jrxeof(struct nfe_softc *, int, int *);
99bfc788c2SDavid E. O'Brien static void nfe_txeof(struct nfe_softc *);
100aab5582fSPyun YongHyeon static int  nfe_encap(struct nfe_softc *, struct mbuf **);
101bfc788c2SDavid E. O'Brien static void nfe_setmulti(struct nfe_softc *);
102c2df7334SMarcel Moolenaar static void nfe_start(if_t);
103c2df7334SMarcel Moolenaar static void nfe_start_locked(if_t);
104c2df7334SMarcel Moolenaar static void nfe_watchdog(if_t);
105bfc788c2SDavid E. O'Brien static void nfe_init(void *);
106bfc788c2SDavid E. O'Brien static void nfe_init_locked(void *);
107c2df7334SMarcel Moolenaar static void nfe_stop(if_t);
108bfc788c2SDavid E. O'Brien static int  nfe_alloc_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
1098b590ad2SPyun YongHyeon static void nfe_alloc_jrx_ring(struct nfe_softc *, struct nfe_jrx_ring *);
110aab5582fSPyun YongHyeon static int  nfe_init_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
111aab5582fSPyun YongHyeon static int  nfe_init_jrx_ring(struct nfe_softc *, struct nfe_jrx_ring *);
112bfc788c2SDavid E. O'Brien static void nfe_free_rx_ring(struct nfe_softc *, struct nfe_rx_ring *);
113aab5582fSPyun YongHyeon static void nfe_free_jrx_ring(struct nfe_softc *, struct nfe_jrx_ring *);
114bfc788c2SDavid E. O'Brien static int  nfe_alloc_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
115aab5582fSPyun YongHyeon static void nfe_init_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
116bfc788c2SDavid E. O'Brien static void nfe_free_tx_ring(struct nfe_softc *, struct nfe_tx_ring *);
117c2df7334SMarcel Moolenaar static int  nfe_ifmedia_upd(if_t);
118c2df7334SMarcel Moolenaar static void nfe_ifmedia_sts(if_t, struct ifmediareq *);
119bfc788c2SDavid E. O'Brien static void nfe_tick(void *);
120aab5582fSPyun YongHyeon static void nfe_get_macaddr(struct nfe_softc *, uint8_t *);
121aab5582fSPyun YongHyeon static void nfe_set_macaddr(struct nfe_softc *, uint8_t *);
122bfc788c2SDavid E. O'Brien static void nfe_dma_map_segs(void *, bus_dma_segment_t *, int, int);
123aab5582fSPyun YongHyeon 
124aab5582fSPyun YongHyeon static int sysctl_int_range(SYSCTL_HANDLER_ARGS, int, int);
125aab5582fSPyun YongHyeon static int sysctl_hw_nfe_proc_limit(SYSCTL_HANDLER_ARGS);
12617d022beSPyun YongHyeon static void nfe_sysctl_node(struct nfe_softc *);
12717d022beSPyun YongHyeon static void nfe_stats_clear(struct nfe_softc *);
12817d022beSPyun YongHyeon static void nfe_stats_update(struct nfe_softc *);
12952a1393eSPyun YongHyeon static void nfe_set_linkspeed(struct nfe_softc *);
13052a1393eSPyun YongHyeon static void nfe_set_wol(struct nfe_softc *);
131257c5577SDavid E. O'Brien 
132257c5577SDavid E. O'Brien #ifdef NFE_DEBUG
133aab5582fSPyun YongHyeon static int nfedebug = 0;
134aab5582fSPyun YongHyeon #define	DPRINTF(sc, ...)	do {				\
135aab5582fSPyun YongHyeon 	if (nfedebug)						\
136aab5582fSPyun YongHyeon 		device_printf((sc)->nfe_dev, __VA_ARGS__);	\
137aab5582fSPyun YongHyeon } while (0)
138aab5582fSPyun YongHyeon #define	DPRINTFN(sc, n, ...)	do {				\
139aab5582fSPyun YongHyeon 	if (nfedebug >= (n))					\
140aab5582fSPyun YongHyeon 		device_printf((sc)->nfe_dev, __VA_ARGS__);	\
141aab5582fSPyun YongHyeon } while (0)
142257c5577SDavid E. O'Brien #else
143aab5582fSPyun YongHyeon #define	DPRINTF(sc, ...)
144aab5582fSPyun YongHyeon #define	DPRINTFN(sc, n, ...)
145257c5577SDavid E. O'Brien #endif
146257c5577SDavid E. O'Brien 
147bfc788c2SDavid E. O'Brien #define	NFE_LOCK(_sc)		mtx_lock(&(_sc)->nfe_mtx)
148bfc788c2SDavid E. O'Brien #define	NFE_UNLOCK(_sc)		mtx_unlock(&(_sc)->nfe_mtx)
149bfc788c2SDavid E. O'Brien #define	NFE_LOCK_ASSERT(_sc)	mtx_assert(&(_sc)->nfe_mtx, MA_OWNED)
150bfc788c2SDavid E. O'Brien 
151aab5582fSPyun YongHyeon /* Tunables. */
152aab5582fSPyun YongHyeon static int msi_disable = 0;
153aab5582fSPyun YongHyeon static int msix_disable = 0;
1548b590ad2SPyun YongHyeon static int jumbo_disable = 0;
155aab5582fSPyun YongHyeon TUNABLE_INT("hw.nfe.msi_disable", &msi_disable);
156aab5582fSPyun YongHyeon TUNABLE_INT("hw.nfe.msix_disable", &msix_disable);
1578b590ad2SPyun YongHyeon TUNABLE_INT("hw.nfe.jumbo_disable", &jumbo_disable);
158bfc788c2SDavid E. O'Brien 
159bfc788c2SDavid E. O'Brien static device_method_t nfe_methods[] = {
160bfc788c2SDavid E. O'Brien 	/* Device interface */
161bfc788c2SDavid E. O'Brien 	DEVMETHOD(device_probe,		nfe_probe),
162bfc788c2SDavid E. O'Brien 	DEVMETHOD(device_attach,	nfe_attach),
163bfc788c2SDavid E. O'Brien 	DEVMETHOD(device_detach,	nfe_detach),
164aab5582fSPyun YongHyeon 	DEVMETHOD(device_suspend,	nfe_suspend),
165aab5582fSPyun YongHyeon 	DEVMETHOD(device_resume,	nfe_resume),
166bfc788c2SDavid E. O'Brien 	DEVMETHOD(device_shutdown,	nfe_shutdown),
167bfc788c2SDavid E. O'Brien 
168bfc788c2SDavid E. O'Brien 	/* MII interface */
169bfc788c2SDavid E. O'Brien 	DEVMETHOD(miibus_readreg,	nfe_miibus_readreg),
170bfc788c2SDavid E. O'Brien 	DEVMETHOD(miibus_writereg,	nfe_miibus_writereg),
171bfc788c2SDavid E. O'Brien 	DEVMETHOD(miibus_statchg,	nfe_miibus_statchg),
172bfc788c2SDavid E. O'Brien 
1734b7ec270SMarius Strobl 	DEVMETHOD_END
174257c5577SDavid E. O'Brien };
175257c5577SDavid E. O'Brien 
176bfc788c2SDavid E. O'Brien static driver_t nfe_driver = {
177bfc788c2SDavid E. O'Brien 	"nfe",
178bfc788c2SDavid E. O'Brien 	nfe_methods,
179bfc788c2SDavid E. O'Brien 	sizeof(struct nfe_softc)
180bfc788c2SDavid E. O'Brien };
181bfc788c2SDavid E. O'Brien 
182108252d8SJohn Baldwin DRIVER_MODULE(nfe, pci, nfe_driver, 0, 0);
1833e38757dSJohn Baldwin DRIVER_MODULE(miibus, nfe, miibus_driver, 0, 0);
184bfc788c2SDavid E. O'Brien 
185bfc788c2SDavid E. O'Brien static struct nfe_type nfe_devs[] = {
186bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE_LAN,
1876bec3967SDavid E. O'Brien 	    "NVIDIA nForce MCP Networking Adapter"},
188bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_LAN,
1896bec3967SDavid E. O'Brien 	    "NVIDIA nForce2 MCP2 Networking Adapter"},
190bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_400_LAN1,
1916bec3967SDavid E. O'Brien 	    "NVIDIA nForce2 400 MCP4 Networking Adapter"},
192bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE2_400_LAN2,
1936bec3967SDavid E. O'Brien 	    "NVIDIA nForce2 400 MCP5 Networking Adapter"},
1946bec3967SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN1,
1956bec3967SDavid E. O'Brien 	    "NVIDIA nForce3 MCP3 Networking Adapter"},
196bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_250_LAN,
1976bec3967SDavid E. O'Brien 	    "NVIDIA nForce3 250 MCP6 Networking Adapter"},
198bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE3_LAN4,
1996bec3967SDavid E. O'Brien 	    "NVIDIA nForce3 MCP7 Networking Adapter"},
200bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE4_LAN1,
2016bec3967SDavid E. O'Brien 	    "NVIDIA nForce4 CK804 MCP8 Networking Adapter"},
202bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE4_LAN2,
2036bec3967SDavid E. O'Brien 	    "NVIDIA nForce4 CK804 MCP9 Networking Adapter"},
204bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP04_LAN1,
205aab5582fSPyun YongHyeon 	    "NVIDIA nForce MCP04 Networking Adapter"},		/* MCP10 */
206bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP04_LAN2,
207aab5582fSPyun YongHyeon 	    "NVIDIA nForce MCP04 Networking Adapter"},		/* MCP11 */
208bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE430_LAN1,
2096bec3967SDavid E. O'Brien 	    "NVIDIA nForce 430 MCP12 Networking Adapter"},
210bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_NFORCE430_LAN2,
2116bec3967SDavid E. O'Brien 	    "NVIDIA nForce 430 MCP13 Networking Adapter"},
212bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP55_LAN1,
213bfc788c2SDavid E. O'Brien 	    "NVIDIA nForce MCP55 Networking Adapter"},
214bfc788c2SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP55_LAN2,
215bfc788c2SDavid E. O'Brien 	    "NVIDIA nForce MCP55 Networking Adapter"},
2163e232000SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN1,
2173e232000SDavid E. O'Brien 	    "NVIDIA nForce MCP61 Networking Adapter"},
2183e232000SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN2,
2193e232000SDavid E. O'Brien 	    "NVIDIA nForce MCP61 Networking Adapter"},
2203e232000SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN3,
2213e232000SDavid E. O'Brien 	    "NVIDIA nForce MCP61 Networking Adapter"},
222aab5582fSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP61_LAN4,
2233e232000SDavid E. O'Brien 	    "NVIDIA nForce MCP61 Networking Adapter"},
2243e232000SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN1,
2253e232000SDavid E. O'Brien 	    "NVIDIA nForce MCP65 Networking Adapter"},
2263e232000SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN2,
2273e232000SDavid E. O'Brien 	    "NVIDIA nForce MCP65 Networking Adapter"},
2283e232000SDavid E. O'Brien 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN3,
2293e232000SDavid E. O'Brien 	    "NVIDIA nForce MCP65 Networking Adapter"},
230aab5582fSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP65_LAN4,
2313e232000SDavid E. O'Brien 	    "NVIDIA nForce MCP65 Networking Adapter"},
232aab5582fSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN1,
233aab5582fSPyun YongHyeon 	    "NVIDIA nForce MCP67 Networking Adapter"},
234aab5582fSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN2,
235aab5582fSPyun YongHyeon 	    "NVIDIA nForce MCP67 Networking Adapter"},
236aab5582fSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN3,
237aab5582fSPyun YongHyeon 	    "NVIDIA nForce MCP67 Networking Adapter"},
238aab5582fSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP67_LAN4,
239aab5582fSPyun YongHyeon 	    "NVIDIA nForce MCP67 Networking Adapter"},
240b7e548dcSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_LAN1,
241b7e548dcSPyun YongHyeon 	    "NVIDIA nForce MCP73 Networking Adapter"},
242b7e548dcSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_LAN2,
243b7e548dcSPyun YongHyeon 	    "NVIDIA nForce MCP73 Networking Adapter"},
244b7e548dcSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_LAN3,
245b7e548dcSPyun YongHyeon 	    "NVIDIA nForce MCP73 Networking Adapter"},
246b7e548dcSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP73_LAN4,
247b7e548dcSPyun YongHyeon 	    "NVIDIA nForce MCP73 Networking Adapter"},
248be38e61aSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_LAN1,
249be38e61aSPyun YongHyeon 	    "NVIDIA nForce MCP77 Networking Adapter"},
250be38e61aSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_LAN2,
251be38e61aSPyun YongHyeon 	    "NVIDIA nForce MCP77 Networking Adapter"},
252be38e61aSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_LAN3,
253be38e61aSPyun YongHyeon 	    "NVIDIA nForce MCP77 Networking Adapter"},
254be38e61aSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP77_LAN4,
255be38e61aSPyun YongHyeon 	    "NVIDIA nForce MCP77 Networking Adapter"},
256be38e61aSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP79_LAN1,
257be38e61aSPyun YongHyeon 	    "NVIDIA nForce MCP79 Networking Adapter"},
258be38e61aSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP79_LAN2,
259be38e61aSPyun YongHyeon 	    "NVIDIA nForce MCP79 Networking Adapter"},
260be38e61aSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP79_LAN3,
261be38e61aSPyun YongHyeon 	    "NVIDIA nForce MCP79 Networking Adapter"},
262be38e61aSPyun YongHyeon 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP79_LAN4,
263be38e61aSPyun YongHyeon 	    "NVIDIA nForce MCP79 Networking Adapter"},
2648b2de3f0SMark Johnston 	{PCI_VENDOR_NVIDIA, PCI_PRODUCT_NVIDIA_MCP89_LAN,
2658b2de3f0SMark Johnston 	    "NVIDIA nForce MCP89 Networking Adapter"},
266bfc788c2SDavid E. O'Brien 	{0, 0, NULL}
267bfc788c2SDavid E. O'Brien };
268bfc788c2SDavid E. O'Brien 
269bfc788c2SDavid E. O'Brien /* Probe for supported hardware ID's */
270bfc788c2SDavid E. O'Brien static int
nfe_probe(device_t dev)271bfc788c2SDavid E. O'Brien nfe_probe(device_t dev)
272257c5577SDavid E. O'Brien {
273bfc788c2SDavid E. O'Brien 	struct nfe_type *t;
274bfc788c2SDavid E. O'Brien 
275bfc788c2SDavid E. O'Brien 	t = nfe_devs;
276bfc788c2SDavid E. O'Brien 	/* Check for matching PCI DEVICE ID's */
277bfc788c2SDavid E. O'Brien 	while (t->name != NULL) {
278bfc788c2SDavid E. O'Brien 		if ((pci_get_vendor(dev) == t->vid_id) &&
279bfc788c2SDavid E. O'Brien 		    (pci_get_device(dev) == t->dev_id)) {
280bfc788c2SDavid E. O'Brien 			device_set_desc(dev, t->name);
281aab5582fSPyun YongHyeon 			return (BUS_PROBE_DEFAULT);
282bfc788c2SDavid E. O'Brien 		}
283bfc788c2SDavid E. O'Brien 		t++;
284257c5577SDavid E. O'Brien 	}
285257c5577SDavid E. O'Brien 
286bfc788c2SDavid E. O'Brien 	return (ENXIO);
287bfc788c2SDavid E. O'Brien }
288bfc788c2SDavid E. O'Brien 
289aab5582fSPyun YongHyeon static void
nfe_alloc_msix(struct nfe_softc * sc,int count)290aab5582fSPyun YongHyeon nfe_alloc_msix(struct nfe_softc *sc, int count)
291aab5582fSPyun YongHyeon {
292aab5582fSPyun YongHyeon 	int rid;
293aab5582fSPyun YongHyeon 
294aab5582fSPyun YongHyeon 	rid = PCIR_BAR(2);
295aab5582fSPyun YongHyeon 	sc->nfe_msix_res = bus_alloc_resource_any(sc->nfe_dev, SYS_RES_MEMORY,
296aab5582fSPyun YongHyeon 	    &rid, RF_ACTIVE);
297aab5582fSPyun YongHyeon 	if (sc->nfe_msix_res == NULL) {
298aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
299aab5582fSPyun YongHyeon 		    "couldn't allocate MSIX table resource\n");
300aab5582fSPyun YongHyeon 		return;
301aab5582fSPyun YongHyeon 	}
302aab5582fSPyun YongHyeon 	rid = PCIR_BAR(3);
303aab5582fSPyun YongHyeon 	sc->nfe_msix_pba_res = bus_alloc_resource_any(sc->nfe_dev,
304aab5582fSPyun YongHyeon 	    SYS_RES_MEMORY, &rid, RF_ACTIVE);
305aab5582fSPyun YongHyeon 	if (sc->nfe_msix_pba_res == NULL) {
306aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
307aab5582fSPyun YongHyeon 		    "couldn't allocate MSIX PBA resource\n");
308aab5582fSPyun YongHyeon 		bus_release_resource(sc->nfe_dev, SYS_RES_MEMORY, PCIR_BAR(2),
309aab5582fSPyun YongHyeon 		    sc->nfe_msix_res);
310aab5582fSPyun YongHyeon 		sc->nfe_msix_res = NULL;
311aab5582fSPyun YongHyeon 		return;
312aab5582fSPyun YongHyeon 	}
313aab5582fSPyun YongHyeon 
314aab5582fSPyun YongHyeon 	if (pci_alloc_msix(sc->nfe_dev, &count) == 0) {
315aab5582fSPyun YongHyeon 		if (count == NFE_MSI_MESSAGES) {
316aab5582fSPyun YongHyeon 			if (bootverbose)
317aab5582fSPyun YongHyeon 				device_printf(sc->nfe_dev,
318aab5582fSPyun YongHyeon 				    "Using %d MSIX messages\n", count);
319aab5582fSPyun YongHyeon 			sc->nfe_msix = 1;
320aab5582fSPyun YongHyeon 		} else {
321aab5582fSPyun YongHyeon 			if (bootverbose)
322aab5582fSPyun YongHyeon 				device_printf(sc->nfe_dev,
323aab5582fSPyun YongHyeon 				    "couldn't allocate MSIX\n");
324aab5582fSPyun YongHyeon 			pci_release_msi(sc->nfe_dev);
325aab5582fSPyun YongHyeon 			bus_release_resource(sc->nfe_dev, SYS_RES_MEMORY,
326aab5582fSPyun YongHyeon 			    PCIR_BAR(3), sc->nfe_msix_pba_res);
327aab5582fSPyun YongHyeon 			bus_release_resource(sc->nfe_dev, SYS_RES_MEMORY,
328aab5582fSPyun YongHyeon 			    PCIR_BAR(2), sc->nfe_msix_res);
329aab5582fSPyun YongHyeon 			sc->nfe_msix_pba_res = NULL;
330aab5582fSPyun YongHyeon 			sc->nfe_msix_res = NULL;
331aab5582fSPyun YongHyeon 		}
332aab5582fSPyun YongHyeon 	}
333aab5582fSPyun YongHyeon }
3342c3adf61SDavid E. O'Brien 
335a173db2dSPyun YongHyeon static int
nfe_detect_msik9(struct nfe_softc * sc)336a173db2dSPyun YongHyeon nfe_detect_msik9(struct nfe_softc *sc)
337a173db2dSPyun YongHyeon {
338a173db2dSPyun YongHyeon 	static const char *maker = "MSI";
339a173db2dSPyun YongHyeon 	static const char *product = "K9N6PGM2-V2 (MS-7309)";
340a173db2dSPyun YongHyeon 	char *m, *p;
341a173db2dSPyun YongHyeon 	int found;
342a173db2dSPyun YongHyeon 
343a173db2dSPyun YongHyeon 	found = 0;
3442be111bfSDavide Italiano 	m = kern_getenv("smbios.planar.maker");
3452be111bfSDavide Italiano 	p = kern_getenv("smbios.planar.product");
346a173db2dSPyun YongHyeon 	if (m != NULL && p != NULL) {
347a173db2dSPyun YongHyeon 		if (strcmp(m, maker) == 0 && strcmp(p, product) == 0)
348a173db2dSPyun YongHyeon 			found = 1;
349a173db2dSPyun YongHyeon 	}
350a173db2dSPyun YongHyeon 	if (m != NULL)
351a173db2dSPyun YongHyeon 		freeenv(m);
352a173db2dSPyun YongHyeon 	if (p != NULL)
353a173db2dSPyun YongHyeon 		freeenv(p);
354a173db2dSPyun YongHyeon 
355a173db2dSPyun YongHyeon 	return (found);
356a173db2dSPyun YongHyeon }
357a173db2dSPyun YongHyeon 
358bfc788c2SDavid E. O'Brien static int
nfe_attach(device_t dev)359bfc788c2SDavid E. O'Brien nfe_attach(device_t dev)
360257c5577SDavid E. O'Brien {
361bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc;
362c2df7334SMarcel Moolenaar 	if_t ifp;
363aab5582fSPyun YongHyeon 	bus_addr_t dma_addr_max;
364a173db2dSPyun YongHyeon 	int error = 0, i, msic, phyloc, reg, rid;
365257c5577SDavid E. O'Brien 
366bfc788c2SDavid E. O'Brien 	sc = device_get_softc(dev);
367bfc788c2SDavid E. O'Brien 	sc->nfe_dev = dev;
368bfc788c2SDavid E. O'Brien 
369bfc788c2SDavid E. O'Brien 	mtx_init(&sc->nfe_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
370aab5582fSPyun YongHyeon 	    MTX_DEF);
371bfc788c2SDavid E. O'Brien 	callout_init_mtx(&sc->nfe_stat_ch, &sc->nfe_mtx, 0);
372bfc788c2SDavid E. O'Brien 
373bfc788c2SDavid E. O'Brien 	pci_enable_busmaster(dev);
374bfc788c2SDavid E. O'Brien 
375aab5582fSPyun YongHyeon 	rid = PCIR_BAR(0);
376aab5582fSPyun YongHyeon 	sc->nfe_res[0] = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
377aab5582fSPyun YongHyeon 	    RF_ACTIVE);
378aab5582fSPyun YongHyeon 	if (sc->nfe_res[0] == NULL) {
379aab5582fSPyun YongHyeon 		device_printf(dev, "couldn't map memory resources\n");
380aab5582fSPyun YongHyeon 		mtx_destroy(&sc->nfe_mtx);
381aab5582fSPyun YongHyeon 		return (ENXIO);
382257c5577SDavid E. O'Brien 	}
383257c5577SDavid E. O'Brien 
3843b0a4aefSJohn Baldwin 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
385aab5582fSPyun YongHyeon 		uint16_t v, width;
386aab5582fSPyun YongHyeon 
387aab5582fSPyun YongHyeon 		v = pci_read_config(dev, reg + 0x08, 2);
388aab5582fSPyun YongHyeon 		/* Change max. read request size to 4096. */
389aab5582fSPyun YongHyeon 		v &= ~(7 << 12);
390aab5582fSPyun YongHyeon 		v |= (5 << 12);
391aab5582fSPyun YongHyeon 		pci_write_config(dev, reg + 0x08, v, 2);
392aab5582fSPyun YongHyeon 
393aab5582fSPyun YongHyeon 		v = pci_read_config(dev, reg + 0x0c, 2);
394aab5582fSPyun YongHyeon 		/* link capability */
395aab5582fSPyun YongHyeon 		v = (v >> 4) & 0x0f;
396aab5582fSPyun YongHyeon 		width = pci_read_config(dev, reg + 0x12, 2);
397aab5582fSPyun YongHyeon 		/* negotiated link width */
398aab5582fSPyun YongHyeon 		width = (width >> 4) & 0x3f;
399aab5582fSPyun YongHyeon 		if (v != width)
400aab5582fSPyun YongHyeon 			device_printf(sc->nfe_dev,
401aab5582fSPyun YongHyeon 			    "warning, negotiated width of link(x%d) != "
402aab5582fSPyun YongHyeon 			    "max. width of link(x%d)\n", width, v);
403aab5582fSPyun YongHyeon 	}
404bfc788c2SDavid E. O'Brien 
4057e7a45ceSPyun YongHyeon 	if (nfe_can_use_msix(sc) == 0) {
4067e7a45ceSPyun YongHyeon 		device_printf(sc->nfe_dev,
4077e7a45ceSPyun YongHyeon 		    "MSI/MSI-X capability black-listed, will use INTx\n");
4087e7a45ceSPyun YongHyeon 		msix_disable = 1;
4097e7a45ceSPyun YongHyeon 		msi_disable = 1;
4107e7a45ceSPyun YongHyeon 	}
4117e7a45ceSPyun YongHyeon 
412bfc788c2SDavid E. O'Brien 	/* Allocate interrupt */
413aab5582fSPyun YongHyeon 	if (msix_disable == 0 || msi_disable == 0) {
414aab5582fSPyun YongHyeon 		if (msix_disable == 0 &&
415aab5582fSPyun YongHyeon 		    (msic = pci_msix_count(dev)) == NFE_MSI_MESSAGES)
416aab5582fSPyun YongHyeon 			nfe_alloc_msix(sc, msic);
417aab5582fSPyun YongHyeon 		if (msi_disable == 0 && sc->nfe_msix == 0 &&
418aab5582fSPyun YongHyeon 		    (msic = pci_msi_count(dev)) == NFE_MSI_MESSAGES &&
419aab5582fSPyun YongHyeon 		    pci_alloc_msi(dev, &msic) == 0) {
420aab5582fSPyun YongHyeon 			if (msic == NFE_MSI_MESSAGES) {
421aab5582fSPyun YongHyeon 				if (bootverbose)
422aab5582fSPyun YongHyeon 					device_printf(dev,
423aab5582fSPyun YongHyeon 					    "Using %d MSI messages\n", msic);
424aab5582fSPyun YongHyeon 				sc->nfe_msi = 1;
425aab5582fSPyun YongHyeon 			} else
426aab5582fSPyun YongHyeon 				pci_release_msi(dev);
427aab5582fSPyun YongHyeon 		}
428aab5582fSPyun YongHyeon 	}
429bfc788c2SDavid E. O'Brien 
430aab5582fSPyun YongHyeon 	if (sc->nfe_msix == 0 && sc->nfe_msi == 0) {
431aab5582fSPyun YongHyeon 		rid = 0;
432aab5582fSPyun YongHyeon 		sc->nfe_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
433aab5582fSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
434aab5582fSPyun YongHyeon 		if (sc->nfe_irq[0] == NULL) {
435aab5582fSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
436bfc788c2SDavid E. O'Brien 			error = ENXIO;
437bfc788c2SDavid E. O'Brien 			goto fail;
438257c5577SDavid E. O'Brien 		}
439aab5582fSPyun YongHyeon 	} else {
440aab5582fSPyun YongHyeon 		for (i = 0, rid = 1; i < NFE_MSI_MESSAGES; i++, rid++) {
441aab5582fSPyun YongHyeon 			sc->nfe_irq[i] = bus_alloc_resource_any(dev,
442aab5582fSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
443aab5582fSPyun YongHyeon 			if (sc->nfe_irq[i] == NULL) {
444aab5582fSPyun YongHyeon 				device_printf(dev,
445aab5582fSPyun YongHyeon 				    "couldn't allocate IRQ resources for "
446aab5582fSPyun YongHyeon 				    "message %d\n", rid);
447aab5582fSPyun YongHyeon 				error = ENXIO;
448aab5582fSPyun YongHyeon 				goto fail;
449aab5582fSPyun YongHyeon 			}
450aab5582fSPyun YongHyeon 		}
451aab5582fSPyun YongHyeon 		/* Map interrupts to vector 0. */
452aab5582fSPyun YongHyeon 		if (sc->nfe_msix != 0) {
453aab5582fSPyun YongHyeon 			NFE_WRITE(sc, NFE_MSIX_MAP0, 0);
454aab5582fSPyun YongHyeon 			NFE_WRITE(sc, NFE_MSIX_MAP1, 0);
455aab5582fSPyun YongHyeon 		} else if (sc->nfe_msi != 0) {
456aab5582fSPyun YongHyeon 			NFE_WRITE(sc, NFE_MSI_MAP0, 0);
457aab5582fSPyun YongHyeon 			NFE_WRITE(sc, NFE_MSI_MAP1, 0);
458aab5582fSPyun YongHyeon 		}
459aab5582fSPyun YongHyeon 	}
460257c5577SDavid E. O'Brien 
461aab5582fSPyun YongHyeon 	/* Set IRQ status/mask register. */
462aab5582fSPyun YongHyeon 	sc->nfe_irq_status = NFE_IRQ_STATUS;
463aab5582fSPyun YongHyeon 	sc->nfe_irq_mask = NFE_IRQ_MASK;
464aab5582fSPyun YongHyeon 	sc->nfe_intrs = NFE_IRQ_WANTED;
465aab5582fSPyun YongHyeon 	sc->nfe_nointrs = 0;
466aab5582fSPyun YongHyeon 	if (sc->nfe_msix != 0) {
467aab5582fSPyun YongHyeon 		sc->nfe_irq_status = NFE_MSIX_IRQ_STATUS;
468aab5582fSPyun YongHyeon 		sc->nfe_nointrs = NFE_IRQ_WANTED;
469aab5582fSPyun YongHyeon 	} else if (sc->nfe_msi != 0) {
470aab5582fSPyun YongHyeon 		sc->nfe_irq_mask = NFE_MSI_IRQ_MASK;
471aab5582fSPyun YongHyeon 		sc->nfe_intrs = NFE_MSI_VECTOR_0_ENABLED;
472aab5582fSPyun YongHyeon 	}
473257c5577SDavid E. O'Brien 
474aab5582fSPyun YongHyeon 	sc->nfe_devid = pci_get_device(dev);
475aab5582fSPyun YongHyeon 	sc->nfe_revid = pci_get_revid(dev);
476bfc788c2SDavid E. O'Brien 	sc->nfe_flags = 0;
477257c5577SDavid E. O'Brien 
478aab5582fSPyun YongHyeon 	switch (sc->nfe_devid) {
479257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_NFORCE3_LAN2:
480257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_NFORCE3_LAN3:
481257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_NFORCE3_LAN4:
482257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_NFORCE3_LAN5:
483bfc788c2SDavid E. O'Brien 		sc->nfe_flags |= NFE_JUMBO_SUP | NFE_HW_CSUM;
484257c5577SDavid E. O'Brien 		break;
485257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP51_LAN1:
486257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP51_LAN2:
48717d022beSPyun YongHyeon 		sc->nfe_flags |= NFE_40BIT_ADDR | NFE_PWR_MGMT | NFE_MIB_V1;
488257c5577SDavid E. O'Brien 		break;
489257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_CK804_LAN1:
490257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_CK804_LAN2:
491257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP04_LAN1:
492257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP04_LAN2:
49317d022beSPyun YongHyeon 		sc->nfe_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | NFE_HW_CSUM |
49417d022beSPyun YongHyeon 		    NFE_MIB_V1;
495257c5577SDavid E. O'Brien 		break;
496257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP55_LAN1:
497257c5577SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP55_LAN2:
4982c3adf61SDavid E. O'Brien 		sc->nfe_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | NFE_HW_CSUM |
49917d022beSPyun YongHyeon 		    NFE_HW_VLAN | NFE_PWR_MGMT | NFE_TX_FLOW_CTRL | NFE_MIB_V2;
500257c5577SDavid E. O'Brien 		break;
501aab5582fSPyun YongHyeon 
5023e232000SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP61_LAN1:
5033e232000SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP61_LAN2:
5043e232000SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP61_LAN3:
5053e232000SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP61_LAN4:
506aab5582fSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP67_LAN1:
507aab5582fSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP67_LAN2:
508aab5582fSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP67_LAN3:
509aab5582fSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP67_LAN4:
510b7e548dcSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP73_LAN1:
511b7e548dcSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP73_LAN2:
512b7e548dcSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP73_LAN3:
513b7e548dcSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP73_LAN4:
514aab5582fSPyun YongHyeon 		sc->nfe_flags |= NFE_40BIT_ADDR | NFE_PWR_MGMT |
51517d022beSPyun YongHyeon 		    NFE_CORRECT_MACADDR | NFE_TX_FLOW_CTRL | NFE_MIB_V2;
5163e232000SDavid E. O'Brien 		break;
517be38e61aSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP77_LAN1:
518be38e61aSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP77_LAN2:
519be38e61aSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP77_LAN3:
520be38e61aSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP77_LAN4:
521be38e61aSPyun YongHyeon 		/* XXX flow control */
522be38e61aSPyun YongHyeon 		sc->nfe_flags |= NFE_40BIT_ADDR | NFE_HW_CSUM | NFE_PWR_MGMT |
52317d022beSPyun YongHyeon 		    NFE_CORRECT_MACADDR | NFE_MIB_V3;
524be38e61aSPyun YongHyeon 		break;
525be38e61aSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP79_LAN1:
526be38e61aSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP79_LAN2:
527be38e61aSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP79_LAN3:
528be38e61aSPyun YongHyeon 	case PCI_PRODUCT_NVIDIA_MCP79_LAN4:
5298b2de3f0SMark Johnston 	case PCI_PRODUCT_NVIDIA_MCP89_LAN:
530be38e61aSPyun YongHyeon 		/* XXX flow control */
531be38e61aSPyun YongHyeon 		sc->nfe_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR | NFE_HW_CSUM |
53217d022beSPyun YongHyeon 		    NFE_PWR_MGMT | NFE_CORRECT_MACADDR | NFE_MIB_V3;
533be38e61aSPyun YongHyeon 		break;
5343e232000SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP65_LAN1:
5353e232000SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP65_LAN2:
5363e232000SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP65_LAN3:
5373e232000SDavid E. O'Brien 	case PCI_PRODUCT_NVIDIA_MCP65_LAN4:
538aab5582fSPyun YongHyeon 		sc->nfe_flags |= NFE_JUMBO_SUP | NFE_40BIT_ADDR |
53917d022beSPyun YongHyeon 		    NFE_PWR_MGMT | NFE_CORRECT_MACADDR | NFE_TX_FLOW_CTRL |
54017d022beSPyun YongHyeon 		    NFE_MIB_V2;
5413e232000SDavid E. O'Brien 		break;
542257c5577SDavid E. O'Brien 	}
543257c5577SDavid E. O'Brien 
544aab5582fSPyun YongHyeon 	nfe_power(sc);
545aab5582fSPyun YongHyeon 	/* Check for reversed ethernet address */
546aab5582fSPyun YongHyeon 	if ((NFE_READ(sc, NFE_TX_UNK) & NFE_MAC_ADDR_INORDER) != 0)
547aab5582fSPyun YongHyeon 		sc->nfe_flags |= NFE_CORRECT_MACADDR;
548aab5582fSPyun YongHyeon 	nfe_get_macaddr(sc, sc->eaddr);
549257c5577SDavid E. O'Brien 	/*
550bfc788c2SDavid E. O'Brien 	 * Allocate the parent bus DMA tag appropriate for PCI.
551bfc788c2SDavid E. O'Brien 	 */
552aab5582fSPyun YongHyeon 	dma_addr_max = BUS_SPACE_MAXADDR_32BIT;
553aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_40BIT_ADDR) != 0)
554aab5582fSPyun YongHyeon 		dma_addr_max = NFE_DMA_MAXADDR;
555aab5582fSPyun YongHyeon 	error = bus_dma_tag_create(
556aab5582fSPyun YongHyeon 	    bus_get_dma_tag(sc->nfe_dev),	/* parent */
557bfc788c2SDavid E. O'Brien 	    1, 0,				/* alignment, boundary */
558aab5582fSPyun YongHyeon 	    dma_addr_max,			/* lowaddr */
559bfc788c2SDavid E. O'Brien 	    BUS_SPACE_MAXADDR,			/* highaddr */
560bfc788c2SDavid E. O'Brien 	    NULL, NULL,				/* filter, filterarg */
561aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0,		/* maxsize, nsegments */
562bfc788c2SDavid E. O'Brien 	    BUS_SPACE_MAXSIZE_32BIT,		/* maxsegsize */
563aab5582fSPyun YongHyeon 	    0,					/* flags */
564bfc788c2SDavid E. O'Brien 	    NULL, NULL,				/* lockfunc, lockarg */
565bfc788c2SDavid E. O'Brien 	    &sc->nfe_parent_tag);
566bfc788c2SDavid E. O'Brien 	if (error)
567bfc788c2SDavid E. O'Brien 		goto fail;
568bfc788c2SDavid E. O'Brien 
569c2df7334SMarcel Moolenaar 	ifp = sc->nfe_ifp = if_gethandle(IFT_ETHER);
5706124fe21SDavid E. O'Brien 
571bfc788c2SDavid E. O'Brien 	/*
572257c5577SDavid E. O'Brien 	 * Allocate Tx and Rx rings.
573257c5577SDavid E. O'Brien 	 */
574aab5582fSPyun YongHyeon 	if ((error = nfe_alloc_tx_ring(sc, &sc->txq)) != 0)
575bfc788c2SDavid E. O'Brien 		goto fail;
576257c5577SDavid E. O'Brien 
577aab5582fSPyun YongHyeon 	if ((error = nfe_alloc_rx_ring(sc, &sc->rxq)) != 0)
578bfc788c2SDavid E. O'Brien 		goto fail;
579aab5582fSPyun YongHyeon 
5808b590ad2SPyun YongHyeon 	nfe_alloc_jrx_ring(sc, &sc->jrxq);
58117d022beSPyun YongHyeon 	/* Create sysctl node. */
58217d022beSPyun YongHyeon 	nfe_sysctl_node(sc);
583257c5577SDavid E. O'Brien 
584c2df7334SMarcel Moolenaar 	if_setsoftc(ifp, sc);
58509a8241fSGleb Smirnoff 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
586c2df7334SMarcel Moolenaar 	if_setflags(ifp, IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST);
587c2df7334SMarcel Moolenaar 	if_setioctlfn(ifp, nfe_ioctl);
588c2df7334SMarcel Moolenaar 	if_setstartfn(ifp, nfe_start);
589c2df7334SMarcel Moolenaar 	if_sethwassist(ifp, 0);
590c2df7334SMarcel Moolenaar 	if_setcapabilities(ifp, 0);
591c2df7334SMarcel Moolenaar 	if_setinitfn(ifp, nfe_init);
592c2df7334SMarcel Moolenaar 	if_setsendqlen(ifp, NFE_TX_RING_COUNT - 1);
593c2df7334SMarcel Moolenaar 	if_setsendqready(ifp);
594c2df7334SMarcel Moolenaar 
595bfc788c2SDavid E. O'Brien 	if (sc->nfe_flags & NFE_HW_CSUM) {
596c2df7334SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_HWCSUM | IFCAP_TSO4, 0);
597c2df7334SMarcel Moolenaar 		if_sethwassistbits(ifp, NFE_CSUM_FEATURES | CSUM_TSO, 0);
598257c5577SDavid E. O'Brien 	}
599c2df7334SMarcel Moolenaar 	if_setcapenable(ifp, if_getcapabilities(ifp));
600257c5577SDavid E. O'Brien 
601c2df7334SMarcel Moolenaar 	sc->nfe_framesize = if_getmtu(ifp) + NFE_RX_HEADERS;
602aab5582fSPyun YongHyeon 	/* VLAN capability setup. */
603c2df7334SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_VLAN_MTU, 0);
604aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_HW_VLAN) != 0) {
605c2df7334SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_VLAN_HWTAGGING, 0);
606c2df7334SMarcel Moolenaar 		if ((if_getcapabilities(ifp) & IFCAP_HWCSUM) != 0)
607c2df7334SMarcel Moolenaar 			if_setcapabilitiesbit(ifp,
608c2df7334SMarcel Moolenaar 			    (IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO), 0);
609aab5582fSPyun YongHyeon 	}
61052a1393eSPyun YongHyeon 
611*ddaf6524SJohn Baldwin 	if (pci_has_pm(dev))
612c2df7334SMarcel Moolenaar 		if_setcapabilitiesbit(ifp, IFCAP_WOL_MAGIC, 0);
613c2df7334SMarcel Moolenaar 	if_setcapenable(ifp, if_getcapabilities(ifp));
614aab5582fSPyun YongHyeon 
615aab5582fSPyun YongHyeon 	/*
616aab5582fSPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
61709a8241fSGleb Smirnoff 	 * Must appear after the call to ether_ifattach() because
61809a8241fSGleb Smirnoff 	 * ether_ifattach() sets ifi_hdrlen to the default value.
619aab5582fSPyun YongHyeon 	 */
620c2df7334SMarcel Moolenaar 	if_setifheaderlen(ifp, sizeof(struct ether_vlan_header));
621aab5582fSPyun YongHyeon 
622bfc788c2SDavid E. O'Brien #ifdef DEVICE_POLLING
623c2df7334SMarcel Moolenaar 	if_setcapabilitiesbit(ifp, IFCAP_POLLING, 0);
624bfc788c2SDavid E. O'Brien #endif
625257c5577SDavid E. O'Brien 
626bfc788c2SDavid E. O'Brien 	/* Do MII setup */
627a173db2dSPyun YongHyeon 	phyloc = MII_PHY_ANY;
628a173db2dSPyun YongHyeon 	if (sc->nfe_devid == PCI_PRODUCT_NVIDIA_MCP61_LAN1 ||
629a173db2dSPyun YongHyeon 	    sc->nfe_devid == PCI_PRODUCT_NVIDIA_MCP61_LAN2 ||
630a173db2dSPyun YongHyeon 	    sc->nfe_devid == PCI_PRODUCT_NVIDIA_MCP61_LAN3 ||
631a173db2dSPyun YongHyeon 	    sc->nfe_devid == PCI_PRODUCT_NVIDIA_MCP61_LAN4) {
632a173db2dSPyun YongHyeon 		if (nfe_detect_msik9(sc) != 0)
633a173db2dSPyun YongHyeon 			phyloc = 0;
634a173db2dSPyun YongHyeon 	}
635c2df7334SMarcel Moolenaar 	error = mii_attach(dev, &sc->nfe_miibus, ifp,
636c2df7334SMarcel Moolenaar 	    (ifm_change_cb_t)nfe_ifmedia_upd, (ifm_stat_cb_t)nfe_ifmedia_sts,
637c2df7334SMarcel Moolenaar 	    BMSR_DEFCAPMASK, phyloc, MII_OFFSET_ANY, MIIF_DOPAUSE);
638d6c65d27SMarius Strobl 	if (error != 0) {
639d6c65d27SMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
640bfc788c2SDavid E. O'Brien 		goto fail;
641257c5577SDavid E. O'Brien 	}
64209a8241fSGleb Smirnoff 	ether_ifattach(ifp, sc->eaddr);
643bfc788c2SDavid E. O'Brien 
6446c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&sc->nfe_int_task, 0, nfe_int_task, sc);
645aab5582fSPyun YongHyeon 	sc->nfe_tq = taskqueue_create_fast("nfe_taskq", M_WAITOK,
646aab5582fSPyun YongHyeon 	    taskqueue_thread_enqueue, &sc->nfe_tq);
647aab5582fSPyun YongHyeon 	taskqueue_start_threads(&sc->nfe_tq, 1, PI_NET, "%s taskq",
648aab5582fSPyun YongHyeon 	    device_get_nameunit(sc->nfe_dev));
649aab5582fSPyun YongHyeon 	error = 0;
650aab5582fSPyun YongHyeon 	if (sc->nfe_msi == 0 && sc->nfe_msix == 0) {
651aab5582fSPyun YongHyeon 		error = bus_setup_intr(dev, sc->nfe_irq[0],
652aab5582fSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, nfe_intr, NULL, sc,
653aab5582fSPyun YongHyeon 		    &sc->nfe_intrhand[0]);
654aab5582fSPyun YongHyeon 	} else {
655aab5582fSPyun YongHyeon 		for (i = 0; i < NFE_MSI_MESSAGES; i++) {
656aab5582fSPyun YongHyeon 			error = bus_setup_intr(dev, sc->nfe_irq[i],
657aab5582fSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, nfe_intr, NULL, sc,
658aab5582fSPyun YongHyeon 			    &sc->nfe_intrhand[i]);
659aab5582fSPyun YongHyeon 			if (error != 0)
660aab5582fSPyun YongHyeon 				break;
661aab5582fSPyun YongHyeon 		}
662aab5582fSPyun YongHyeon 	}
663bfc788c2SDavid E. O'Brien 	if (error) {
664aab5582fSPyun YongHyeon 		device_printf(dev, "couldn't set up irq\n");
665aab5582fSPyun YongHyeon 		taskqueue_free(sc->nfe_tq);
666aab5582fSPyun YongHyeon 		sc->nfe_tq = NULL;
66709a8241fSGleb Smirnoff 		ether_ifdetach(ifp);
668bfc788c2SDavid E. O'Brien 		goto fail;
669bfc788c2SDavid E. O'Brien 	}
670bfc788c2SDavid E. O'Brien 
671bfc788c2SDavid E. O'Brien fail:
672bfc788c2SDavid E. O'Brien 	if (error)
673bfc788c2SDavid E. O'Brien 		nfe_detach(dev);
674bfc788c2SDavid E. O'Brien 
675bfc788c2SDavid E. O'Brien 	return (error);
676bfc788c2SDavid E. O'Brien }
677bfc788c2SDavid E. O'Brien 
678bfc788c2SDavid E. O'Brien static int
nfe_detach(device_t dev)679bfc788c2SDavid E. O'Brien nfe_detach(device_t dev)
680257c5577SDavid E. O'Brien {
681bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc;
682c2df7334SMarcel Moolenaar 	if_t ifp;
683aab5582fSPyun YongHyeon 	uint8_t eaddr[ETHER_ADDR_LEN];
684aab5582fSPyun YongHyeon 	int i, rid;
685257c5577SDavid E. O'Brien 
686bfc788c2SDavid E. O'Brien 	sc = device_get_softc(dev);
687bfc788c2SDavid E. O'Brien 	KASSERT(mtx_initialized(&sc->nfe_mtx), ("nfe mutex not initialized"));
688bfc788c2SDavid E. O'Brien 	ifp = sc->nfe_ifp;
689bfc788c2SDavid E. O'Brien 
690bfc788c2SDavid E. O'Brien #ifdef DEVICE_POLLING
691c2df7334SMarcel Moolenaar 	if (ifp != NULL && if_getcapenable(ifp) & IFCAP_POLLING)
692bfc788c2SDavid E. O'Brien 		ether_poll_deregister(ifp);
693bfc788c2SDavid E. O'Brien #endif
694bfc788c2SDavid E. O'Brien 	if (device_is_attached(dev)) {
69596058696SDavid E. O'Brien 		NFE_LOCK(sc);
696aab5582fSPyun YongHyeon 		nfe_stop(ifp);
697c2df7334SMarcel Moolenaar 		if_setflagbits(ifp, 0, IFF_UP);
69896058696SDavid E. O'Brien 		NFE_UNLOCK(sc);
699bfc788c2SDavid E. O'Brien 		callout_drain(&sc->nfe_stat_ch);
70009a8241fSGleb Smirnoff 		ether_ifdetach(ifp);
701257c5577SDavid E. O'Brien 	}
702257c5577SDavid E. O'Brien 
703aab5582fSPyun YongHyeon 	if (ifp) {
704aab5582fSPyun YongHyeon 		/* restore ethernet address */
705aab5582fSPyun YongHyeon 		if ((sc->nfe_flags & NFE_CORRECT_MACADDR) == 0) {
706aab5582fSPyun YongHyeon 			for (i = 0; i < ETHER_ADDR_LEN; i++) {
707aab5582fSPyun YongHyeon 				eaddr[i] = sc->eaddr[5 - i];
708aab5582fSPyun YongHyeon 			}
709aab5582fSPyun YongHyeon 		} else
710aab5582fSPyun YongHyeon 			bcopy(sc->eaddr, eaddr, ETHER_ADDR_LEN);
711aab5582fSPyun YongHyeon 		nfe_set_macaddr(sc, eaddr);
71209a8241fSGleb Smirnoff 		if_free(ifp);
713aab5582fSPyun YongHyeon 	}
714bfc788c2SDavid E. O'Brien 	bus_generic_detach(dev);
715aab5582fSPyun YongHyeon 	if (sc->nfe_tq != NULL) {
716aab5582fSPyun YongHyeon 		taskqueue_drain(sc->nfe_tq, &sc->nfe_int_task);
717aab5582fSPyun YongHyeon 		taskqueue_free(sc->nfe_tq);
718aab5582fSPyun YongHyeon 		sc->nfe_tq = NULL;
719aab5582fSPyun YongHyeon 	}
720bfc788c2SDavid E. O'Brien 
721aab5582fSPyun YongHyeon 	for (i = 0; i < NFE_MSI_MESSAGES; i++) {
722aab5582fSPyun YongHyeon 		if (sc->nfe_intrhand[i] != NULL) {
723aab5582fSPyun YongHyeon 			bus_teardown_intr(dev, sc->nfe_irq[i],
724aab5582fSPyun YongHyeon 			    sc->nfe_intrhand[i]);
725aab5582fSPyun YongHyeon 			sc->nfe_intrhand[i] = NULL;
726aab5582fSPyun YongHyeon 		}
727aab5582fSPyun YongHyeon 	}
728aab5582fSPyun YongHyeon 
729aab5582fSPyun YongHyeon 	if (sc->nfe_msi == 0 && sc->nfe_msix == 0) {
730aab5582fSPyun YongHyeon 		if (sc->nfe_irq[0] != NULL)
731aab5582fSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
732aab5582fSPyun YongHyeon 			    sc->nfe_irq[0]);
733aab5582fSPyun YongHyeon 	} else {
734aab5582fSPyun YongHyeon 		for (i = 0, rid = 1; i < NFE_MSI_MESSAGES; i++, rid++) {
735aab5582fSPyun YongHyeon 			if (sc->nfe_irq[i] != NULL) {
736aab5582fSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
737aab5582fSPyun YongHyeon 				    sc->nfe_irq[i]);
738aab5582fSPyun YongHyeon 				sc->nfe_irq[i] = NULL;
739aab5582fSPyun YongHyeon 			}
740aab5582fSPyun YongHyeon 		}
741aab5582fSPyun YongHyeon 		pci_release_msi(dev);
742aab5582fSPyun YongHyeon 	}
743aab5582fSPyun YongHyeon 	if (sc->nfe_msix_pba_res != NULL) {
744aab5582fSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(3),
745aab5582fSPyun YongHyeon 		    sc->nfe_msix_pba_res);
746aab5582fSPyun YongHyeon 		sc->nfe_msix_pba_res = NULL;
747aab5582fSPyun YongHyeon 	}
748aab5582fSPyun YongHyeon 	if (sc->nfe_msix_res != NULL) {
749aab5582fSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(2),
750aab5582fSPyun YongHyeon 		    sc->nfe_msix_res);
751aab5582fSPyun YongHyeon 		sc->nfe_msix_res = NULL;
752aab5582fSPyun YongHyeon 	}
753aab5582fSPyun YongHyeon 	if (sc->nfe_res[0] != NULL) {
754aab5582fSPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, PCIR_BAR(0),
755aab5582fSPyun YongHyeon 		    sc->nfe_res[0]);
756aab5582fSPyun YongHyeon 		sc->nfe_res[0] = NULL;
757aab5582fSPyun YongHyeon 	}
758bfc788c2SDavid E. O'Brien 
759bfc788c2SDavid E. O'Brien 	nfe_free_tx_ring(sc, &sc->txq);
760bfc788c2SDavid E. O'Brien 	nfe_free_rx_ring(sc, &sc->rxq);
761aab5582fSPyun YongHyeon 	nfe_free_jrx_ring(sc, &sc->jrxq);
762bfc788c2SDavid E. O'Brien 
763aab5582fSPyun YongHyeon 	if (sc->nfe_parent_tag) {
764bfc788c2SDavid E. O'Brien 		bus_dma_tag_destroy(sc->nfe_parent_tag);
765aab5582fSPyun YongHyeon 		sc->nfe_parent_tag = NULL;
766aab5582fSPyun YongHyeon 	}
767bfc788c2SDavid E. O'Brien 
768bfc788c2SDavid E. O'Brien 	mtx_destroy(&sc->nfe_mtx);
769bfc788c2SDavid E. O'Brien 
770bfc788c2SDavid E. O'Brien 	return (0);
771bfc788c2SDavid E. O'Brien }
772bfc788c2SDavid E. O'Brien 
773aab5582fSPyun YongHyeon static int
nfe_suspend(device_t dev)774aab5582fSPyun YongHyeon nfe_suspend(device_t dev)
775aab5582fSPyun YongHyeon {
776aab5582fSPyun YongHyeon 	struct nfe_softc *sc;
777aab5582fSPyun YongHyeon 
778aab5582fSPyun YongHyeon 	sc = device_get_softc(dev);
779aab5582fSPyun YongHyeon 
780aab5582fSPyun YongHyeon 	NFE_LOCK(sc);
781aab5582fSPyun YongHyeon 	nfe_stop(sc->nfe_ifp);
78252a1393eSPyun YongHyeon 	nfe_set_wol(sc);
783aab5582fSPyun YongHyeon 	sc->nfe_suspended = 1;
784aab5582fSPyun YongHyeon 	NFE_UNLOCK(sc);
785aab5582fSPyun YongHyeon 
786aab5582fSPyun YongHyeon 	return (0);
787aab5582fSPyun YongHyeon }
788aab5582fSPyun YongHyeon 
789aab5582fSPyun YongHyeon static int
nfe_resume(device_t dev)790aab5582fSPyun YongHyeon nfe_resume(device_t dev)
791aab5582fSPyun YongHyeon {
792aab5582fSPyun YongHyeon 	struct nfe_softc *sc;
793c2df7334SMarcel Moolenaar 	if_t ifp;
794aab5582fSPyun YongHyeon 
795aab5582fSPyun YongHyeon 	sc = device_get_softc(dev);
796aab5582fSPyun YongHyeon 
797aab5582fSPyun YongHyeon 	NFE_LOCK(sc);
79852a1393eSPyun YongHyeon 	nfe_power(sc);
799aab5582fSPyun YongHyeon 	ifp = sc->nfe_ifp;
800c2df7334SMarcel Moolenaar 	if (if_getflags(ifp) & IFF_UP)
801aab5582fSPyun YongHyeon 		nfe_init_locked(sc);
802aab5582fSPyun YongHyeon 	sc->nfe_suspended = 0;
803aab5582fSPyun YongHyeon 	NFE_UNLOCK(sc);
804aab5582fSPyun YongHyeon 
805aab5582fSPyun YongHyeon 	return (0);
806aab5582fSPyun YongHyeon }
807aab5582fSPyun YongHyeon 
8087e7a45ceSPyun YongHyeon static int
nfe_can_use_msix(struct nfe_softc * sc)8097e7a45ceSPyun YongHyeon nfe_can_use_msix(struct nfe_softc *sc)
8107e7a45ceSPyun YongHyeon {
8117e7a45ceSPyun YongHyeon 	static struct msix_blacklist {
8127e7a45ceSPyun YongHyeon 		char	*maker;
8137e7a45ceSPyun YongHyeon 		char	*product;
8147e7a45ceSPyun YongHyeon 	} msix_blacklists[] = {
8157e7a45ceSPyun YongHyeon 		{ "ASUSTeK Computer INC.", "P5N32-SLI PREMIUM" }
8167e7a45ceSPyun YongHyeon 	};
8177e7a45ceSPyun YongHyeon 
8187e7a45ceSPyun YongHyeon 	struct msix_blacklist *mblp;
8197e7a45ceSPyun YongHyeon 	char *maker, *product;
820b0630da9SPyun YongHyeon 	int count, n, use_msix;
8217e7a45ceSPyun YongHyeon 
8227e7a45ceSPyun YongHyeon 	/*
8237e7a45ceSPyun YongHyeon 	 * Search base board manufacturer and product name table
8247e7a45ceSPyun YongHyeon 	 * to see this system has a known MSI/MSI-X issue.
8257e7a45ceSPyun YongHyeon 	 */
8262be111bfSDavide Italiano 	maker = kern_getenv("smbios.planar.maker");
8272be111bfSDavide Italiano 	product = kern_getenv("smbios.planar.product");
828b0630da9SPyun YongHyeon 	use_msix = 1;
8297e7a45ceSPyun YongHyeon 	if (maker != NULL && product != NULL) {
83073a1170aSPedro F. Giffuni 		count = nitems(msix_blacklists);
8317e7a45ceSPyun YongHyeon 		mblp = msix_blacklists;
8327e7a45ceSPyun YongHyeon 		for (n = 0; n < count; n++) {
8337e7a45ceSPyun YongHyeon 			if (strcmp(maker, mblp->maker) == 0 &&
834b0630da9SPyun YongHyeon 			    strcmp(product, mblp->product) == 0) {
835b0630da9SPyun YongHyeon 				use_msix = 0;
836b0630da9SPyun YongHyeon 				break;
837b0630da9SPyun YongHyeon 			}
8387e7a45ceSPyun YongHyeon 			mblp++;
8397e7a45ceSPyun YongHyeon 		}
8407e7a45ceSPyun YongHyeon 	}
841b0630da9SPyun YongHyeon 	if (maker != NULL)
842b0630da9SPyun YongHyeon 		freeenv(maker);
843b0630da9SPyun YongHyeon 	if (product != NULL)
844b0630da9SPyun YongHyeon 		freeenv(product);
8457e7a45ceSPyun YongHyeon 
846b0630da9SPyun YongHyeon 	return (use_msix);
8477e7a45ceSPyun YongHyeon }
8487e7a45ceSPyun YongHyeon 
849aab5582fSPyun YongHyeon /* Take PHY/NIC out of powerdown, from Linux */
850aab5582fSPyun YongHyeon static void
nfe_power(struct nfe_softc * sc)851aab5582fSPyun YongHyeon nfe_power(struct nfe_softc *sc)
852aab5582fSPyun YongHyeon {
853aab5582fSPyun YongHyeon 	uint32_t pwr;
854aab5582fSPyun YongHyeon 
855aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_PWR_MGMT) == 0)
856aab5582fSPyun YongHyeon 		return;
857aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_RESET | NFE_RXTX_BIT2);
858aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_MAC_RESET, NFE_MAC_RESET_MAGIC);
859aab5582fSPyun YongHyeon 	DELAY(100);
860aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_MAC_RESET, 0);
861aab5582fSPyun YongHyeon 	DELAY(100);
862aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_BIT2);
863aab5582fSPyun YongHyeon 	pwr = NFE_READ(sc, NFE_PWR2_CTL);
864aab5582fSPyun YongHyeon 	pwr &= ~NFE_PWR2_WAKEUP_MASK;
865aab5582fSPyun YongHyeon 	if (sc->nfe_revid >= 0xa3 &&
866aab5582fSPyun YongHyeon 	    (sc->nfe_devid == PCI_PRODUCT_NVIDIA_NFORCE430_LAN1 ||
867aab5582fSPyun YongHyeon 	    sc->nfe_devid == PCI_PRODUCT_NVIDIA_NFORCE430_LAN2))
868aab5582fSPyun YongHyeon 		pwr |= NFE_PWR2_REVA3;
869aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_PWR2_CTL, pwr);
870aab5582fSPyun YongHyeon }
871aab5582fSPyun YongHyeon 
872bfc788c2SDavid E. O'Brien static void
nfe_miibus_statchg(device_t dev)873bfc788c2SDavid E. O'Brien nfe_miibus_statchg(device_t dev)
874257c5577SDavid E. O'Brien {
875bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc;
876aab5582fSPyun YongHyeon 	struct mii_data *mii;
877c2df7334SMarcel Moolenaar 	if_t ifp;
87852a1393eSPyun YongHyeon 	uint32_t rxctl, txctl;
879aab5582fSPyun YongHyeon 
88052a1393eSPyun YongHyeon 	sc = device_get_softc(dev);
881aab5582fSPyun YongHyeon 
882bfc788c2SDavid E. O'Brien 	mii = device_get_softc(sc->nfe_miibus);
883aab5582fSPyun YongHyeon 	ifp = sc->nfe_ifp;
88452a1393eSPyun YongHyeon 
88552a1393eSPyun YongHyeon 	sc->nfe_link = 0;
88652a1393eSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
88752a1393eSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
88852a1393eSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
88952a1393eSPyun YongHyeon 		case IFM_10_T:
89052a1393eSPyun YongHyeon 		case IFM_100_TX:
89152a1393eSPyun YongHyeon 		case IFM_1000_T:
89252a1393eSPyun YongHyeon 			sc->nfe_link = 1;
89352a1393eSPyun YongHyeon 			break;
89452a1393eSPyun YongHyeon 		default:
89552a1393eSPyun YongHyeon 			break;
89652a1393eSPyun YongHyeon 		}
897aab5582fSPyun YongHyeon 	}
898aab5582fSPyun YongHyeon 
89952a1393eSPyun YongHyeon 	nfe_mac_config(sc, mii);
90052a1393eSPyun YongHyeon 	txctl = NFE_READ(sc, NFE_TX_CTL);
90152a1393eSPyun YongHyeon 	rxctl = NFE_READ(sc, NFE_RX_CTL);
902c2df7334SMarcel Moolenaar 	if (sc->nfe_link != 0 && (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
90352a1393eSPyun YongHyeon 		txctl |= NFE_TX_START;
90452a1393eSPyun YongHyeon 		rxctl |= NFE_RX_START;
90552a1393eSPyun YongHyeon 	} else {
90652a1393eSPyun YongHyeon 		txctl &= ~NFE_TX_START;
90752a1393eSPyun YongHyeon 		rxctl &= ~NFE_RX_START;
90852a1393eSPyun YongHyeon 	}
90952a1393eSPyun YongHyeon 	NFE_WRITE(sc, NFE_TX_CTL, txctl);
91052a1393eSPyun YongHyeon 	NFE_WRITE(sc, NFE_RX_CTL, rxctl);
91152a1393eSPyun YongHyeon }
91252a1393eSPyun YongHyeon 
91352a1393eSPyun YongHyeon static void
nfe_mac_config(struct nfe_softc * sc,struct mii_data * mii)91452a1393eSPyun YongHyeon nfe_mac_config(struct nfe_softc *sc, struct mii_data *mii)
91552a1393eSPyun YongHyeon {
91652a1393eSPyun YongHyeon 	uint32_t link, misc, phy, seed;
91752a1393eSPyun YongHyeon 	uint32_t val;
91852a1393eSPyun YongHyeon 
91952a1393eSPyun YongHyeon 	NFE_LOCK_ASSERT(sc);
920257c5577SDavid E. O'Brien 
921257c5577SDavid E. O'Brien 	phy = NFE_READ(sc, NFE_PHY_IFACE);
922257c5577SDavid E. O'Brien 	phy &= ~(NFE_PHY_HDX | NFE_PHY_100TX | NFE_PHY_1000T);
923257c5577SDavid E. O'Brien 
924257c5577SDavid E. O'Brien 	seed = NFE_READ(sc, NFE_RNDSEED);
925257c5577SDavid E. O'Brien 	seed &= ~NFE_SEED_MASK;
926257c5577SDavid E. O'Brien 
92752a1393eSPyun YongHyeon 	misc = NFE_MISC1_MAGIC;
92852a1393eSPyun YongHyeon 	link = NFE_MEDIA_SET;
92952a1393eSPyun YongHyeon 
93052a1393eSPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) == 0) {
931257c5577SDavid E. O'Brien 		phy  |= NFE_PHY_HDX;	/* half-duplex */
932257c5577SDavid E. O'Brien 		misc |= NFE_MISC1_HDX;
933257c5577SDavid E. O'Brien 	}
934257c5577SDavid E. O'Brien 
935257c5577SDavid E. O'Brien 	switch (IFM_SUBTYPE(mii->mii_media_active)) {
936257c5577SDavid E. O'Brien 	case IFM_1000_T:	/* full-duplex only */
937257c5577SDavid E. O'Brien 		link |= NFE_MEDIA_1000T;
938257c5577SDavid E. O'Brien 		seed |= NFE_SEED_1000T;
939257c5577SDavid E. O'Brien 		phy  |= NFE_PHY_1000T;
940257c5577SDavid E. O'Brien 		break;
941257c5577SDavid E. O'Brien 	case IFM_100_TX:
942257c5577SDavid E. O'Brien 		link |= NFE_MEDIA_100TX;
943257c5577SDavid E. O'Brien 		seed |= NFE_SEED_100TX;
944257c5577SDavid E. O'Brien 		phy  |= NFE_PHY_100TX;
945257c5577SDavid E. O'Brien 		break;
946257c5577SDavid E. O'Brien 	case IFM_10_T:
947257c5577SDavid E. O'Brien 		link |= NFE_MEDIA_10T;
948257c5577SDavid E. O'Brien 		seed |= NFE_SEED_10T;
949257c5577SDavid E. O'Brien 		break;
950257c5577SDavid E. O'Brien 	}
951257c5577SDavid E. O'Brien 
952aab5582fSPyun YongHyeon 	if ((phy & 0x10000000) != 0) {
953aab5582fSPyun YongHyeon 		if (IFM_SUBTYPE(mii->mii_media_active) == IFM_1000_T)
954aab5582fSPyun YongHyeon 			val = NFE_R1_MAGIC_1000;
955aab5582fSPyun YongHyeon 		else
956aab5582fSPyun YongHyeon 			val = NFE_R1_MAGIC_10_100;
957aab5582fSPyun YongHyeon 	} else
958aab5582fSPyun YongHyeon 		val = NFE_R1_MAGIC_DEFAULT;
959aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_SETUP_R1, val);
960aab5582fSPyun YongHyeon 
961257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RNDSEED, seed);	/* XXX: gigabit NICs only? */
962257c5577SDavid E. O'Brien 
963257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_PHY_IFACE, phy);
964257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_MISC1, misc);
965257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_LINKSPEED, link);
966aab5582fSPyun YongHyeon 
96752a1393eSPyun YongHyeon 	if ((IFM_OPTIONS(mii->mii_media_active) & IFM_FDX) != 0) {
968aab5582fSPyun YongHyeon 		/* It seems all hardwares supports Rx pause frames. */
969aab5582fSPyun YongHyeon 		val = NFE_READ(sc, NFE_RXFILTER);
970efd4fc3fSMarius Strobl 		if ((IFM_OPTIONS(mii->mii_media_active) &
971efd4fc3fSMarius Strobl 		    IFM_ETH_RXPAUSE) != 0)
972aab5582fSPyun YongHyeon 			val |= NFE_PFF_RX_PAUSE;
973aab5582fSPyun YongHyeon 		else
974aab5582fSPyun YongHyeon 			val &= ~NFE_PFF_RX_PAUSE;
975aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_RXFILTER, val);
976aab5582fSPyun YongHyeon 		if ((sc->nfe_flags & NFE_TX_FLOW_CTRL) != 0) {
977aab5582fSPyun YongHyeon 			val = NFE_READ(sc, NFE_MISC1);
97852a1393eSPyun YongHyeon 			if ((IFM_OPTIONS(mii->mii_media_active) &
979efd4fc3fSMarius Strobl 			    IFM_ETH_TXPAUSE) != 0) {
980aab5582fSPyun YongHyeon 				NFE_WRITE(sc, NFE_TX_PAUSE_FRAME,
981aab5582fSPyun YongHyeon 				    NFE_TX_PAUSE_FRAME_ENABLE);
982aab5582fSPyun YongHyeon 				val |= NFE_MISC1_TX_PAUSE;
983aab5582fSPyun YongHyeon 			} else {
984aab5582fSPyun YongHyeon 				val &= ~NFE_MISC1_TX_PAUSE;
985aab5582fSPyun YongHyeon 				NFE_WRITE(sc, NFE_TX_PAUSE_FRAME,
986aab5582fSPyun YongHyeon 				    NFE_TX_PAUSE_FRAME_DISABLE);
987aab5582fSPyun YongHyeon 			}
988aab5582fSPyun YongHyeon 			NFE_WRITE(sc, NFE_MISC1, val);
989aab5582fSPyun YongHyeon 		}
990aab5582fSPyun YongHyeon 	} else {
991aab5582fSPyun YongHyeon 		/* disable rx/tx pause frames */
992aab5582fSPyun YongHyeon 		val = NFE_READ(sc, NFE_RXFILTER);
993aab5582fSPyun YongHyeon 		val &= ~NFE_PFF_RX_PAUSE;
994aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_RXFILTER, val);
995aab5582fSPyun YongHyeon 		if ((sc->nfe_flags & NFE_TX_FLOW_CTRL) != 0) {
996aab5582fSPyun YongHyeon 			NFE_WRITE(sc, NFE_TX_PAUSE_FRAME,
997aab5582fSPyun YongHyeon 			    NFE_TX_PAUSE_FRAME_DISABLE);
998aab5582fSPyun YongHyeon 			val = NFE_READ(sc, NFE_MISC1);
999aab5582fSPyun YongHyeon 			val &= ~NFE_MISC1_TX_PAUSE;
1000aab5582fSPyun YongHyeon 			NFE_WRITE(sc, NFE_MISC1, val);
1001aab5582fSPyun YongHyeon 		}
1002aab5582fSPyun YongHyeon 	}
1003257c5577SDavid E. O'Brien }
1004257c5577SDavid E. O'Brien 
1005bfc788c2SDavid E. O'Brien static int
nfe_miibus_readreg(device_t dev,int phy,int reg)1006bfc788c2SDavid E. O'Brien nfe_miibus_readreg(device_t dev, int phy, int reg)
1007257c5577SDavid E. O'Brien {
1008bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc = device_get_softc(dev);
1009aab5582fSPyun YongHyeon 	uint32_t val;
1010257c5577SDavid E. O'Brien 	int ntries;
1011257c5577SDavid E. O'Brien 
1012257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
1013257c5577SDavid E. O'Brien 
1014257c5577SDavid E. O'Brien 	if (NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY) {
1015257c5577SDavid E. O'Brien 		NFE_WRITE(sc, NFE_PHY_CTL, NFE_PHY_BUSY);
1016257c5577SDavid E. O'Brien 		DELAY(100);
1017257c5577SDavid E. O'Brien 	}
1018257c5577SDavid E. O'Brien 
1019257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_PHY_CTL, (phy << NFE_PHYADD_SHIFT) | reg);
1020257c5577SDavid E. O'Brien 
1021aab5582fSPyun YongHyeon 	for (ntries = 0; ntries < NFE_TIMEOUT; ntries++) {
1022257c5577SDavid E. O'Brien 		DELAY(100);
1023257c5577SDavid E. O'Brien 		if (!(NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY))
1024257c5577SDavid E. O'Brien 			break;
1025257c5577SDavid E. O'Brien 	}
1026aab5582fSPyun YongHyeon 	if (ntries == NFE_TIMEOUT) {
1027aab5582fSPyun YongHyeon 		DPRINTFN(sc, 2, "timeout waiting for PHY\n");
1028257c5577SDavid E. O'Brien 		return 0;
1029257c5577SDavid E. O'Brien 	}
1030257c5577SDavid E. O'Brien 
1031257c5577SDavid E. O'Brien 	if (NFE_READ(sc, NFE_PHY_STATUS) & NFE_PHY_ERROR) {
1032aab5582fSPyun YongHyeon 		DPRINTFN(sc, 2, "could not read PHY\n");
1033257c5577SDavid E. O'Brien 		return 0;
1034257c5577SDavid E. O'Brien 	}
1035257c5577SDavid E. O'Brien 
1036257c5577SDavid E. O'Brien 	val = NFE_READ(sc, NFE_PHY_DATA);
1037257c5577SDavid E. O'Brien 	if (val != 0xffffffff && val != 0)
1038257c5577SDavid E. O'Brien 		sc->mii_phyaddr = phy;
1039257c5577SDavid E. O'Brien 
1040aab5582fSPyun YongHyeon 	DPRINTFN(sc, 2, "mii read phy %d reg 0x%x ret 0x%x\n", phy, reg, val);
1041257c5577SDavid E. O'Brien 
1042aab5582fSPyun YongHyeon 	return (val);
1043257c5577SDavid E. O'Brien }
1044257c5577SDavid E. O'Brien 
1045bfc788c2SDavid E. O'Brien static int
nfe_miibus_writereg(device_t dev,int phy,int reg,int val)1046bfc788c2SDavid E. O'Brien nfe_miibus_writereg(device_t dev, int phy, int reg, int val)
1047257c5577SDavid E. O'Brien {
1048bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc = device_get_softc(dev);
1049aab5582fSPyun YongHyeon 	uint32_t ctl;
1050257c5577SDavid E. O'Brien 	int ntries;
1051257c5577SDavid E. O'Brien 
1052257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
1053257c5577SDavid E. O'Brien 
1054257c5577SDavid E. O'Brien 	if (NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY) {
1055257c5577SDavid E. O'Brien 		NFE_WRITE(sc, NFE_PHY_CTL, NFE_PHY_BUSY);
1056257c5577SDavid E. O'Brien 		DELAY(100);
1057257c5577SDavid E. O'Brien 	}
1058257c5577SDavid E. O'Brien 
1059257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_PHY_DATA, val);
1060257c5577SDavid E. O'Brien 	ctl = NFE_PHY_WRITE | (phy << NFE_PHYADD_SHIFT) | reg;
1061257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_PHY_CTL, ctl);
1062257c5577SDavid E. O'Brien 
1063aab5582fSPyun YongHyeon 	for (ntries = 0; ntries < NFE_TIMEOUT; ntries++) {
1064257c5577SDavid E. O'Brien 		DELAY(100);
1065257c5577SDavid E. O'Brien 		if (!(NFE_READ(sc, NFE_PHY_CTL) & NFE_PHY_BUSY))
1066257c5577SDavid E. O'Brien 			break;
1067257c5577SDavid E. O'Brien 	}
1068257c5577SDavid E. O'Brien #ifdef NFE_DEBUG
1069aab5582fSPyun YongHyeon 	if (nfedebug >= 2 && ntries == NFE_TIMEOUT)
1070aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not write to PHY\n");
1071257c5577SDavid E. O'Brien #endif
1072aab5582fSPyun YongHyeon 	return (0);
1073257c5577SDavid E. O'Brien }
1074257c5577SDavid E. O'Brien 
1075aab5582fSPyun YongHyeon struct nfe_dmamap_arg {
1076aab5582fSPyun YongHyeon 	bus_addr_t nfe_busaddr;
1077aab5582fSPyun YongHyeon };
10782c3adf61SDavid E. O'Brien 
1079bfc788c2SDavid E. O'Brien static int
nfe_alloc_rx_ring(struct nfe_softc * sc,struct nfe_rx_ring * ring)1080bfc788c2SDavid E. O'Brien nfe_alloc_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1081bfc788c2SDavid E. O'Brien {
1082aab5582fSPyun YongHyeon 	struct nfe_dmamap_arg ctx;
1083bfc788c2SDavid E. O'Brien 	struct nfe_rx_data *data;
1084aab5582fSPyun YongHyeon 	void *desc;
1085bfc788c2SDavid E. O'Brien 	int i, error, descsize;
1086bfc788c2SDavid E. O'Brien 
1087bfc788c2SDavid E. O'Brien 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1088aab5582fSPyun YongHyeon 		desc = ring->desc64;
1089bfc788c2SDavid E. O'Brien 		descsize = sizeof (struct nfe_desc64);
1090bfc788c2SDavid E. O'Brien 	} else {
1091aab5582fSPyun YongHyeon 		desc = ring->desc32;
1092bfc788c2SDavid E. O'Brien 		descsize = sizeof (struct nfe_desc32);
1093bfc788c2SDavid E. O'Brien 	}
1094bfc788c2SDavid E. O'Brien 
1095bfc788c2SDavid E. O'Brien 	ring->cur = ring->next = 0;
1096bfc788c2SDavid E. O'Brien 
1097bfc788c2SDavid E. O'Brien 	error = bus_dma_tag_create(sc->nfe_parent_tag,
1098aab5582fSPyun YongHyeon 	    NFE_RING_ALIGN, 0,			/* alignment, boundary */
1099aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1100bfc788c2SDavid E. O'Brien 	    BUS_SPACE_MAXADDR,			/* highaddr */
1101bfc788c2SDavid E. O'Brien 	    NULL, NULL,				/* filter, filterarg */
1102bfc788c2SDavid E. O'Brien 	    NFE_RX_RING_COUNT * descsize, 1,	/* maxsize, nsegments */
1103bfc788c2SDavid E. O'Brien 	    NFE_RX_RING_COUNT * descsize,	/* maxsegsize */
1104aab5582fSPyun YongHyeon 	    0,					/* flags */
1105bfc788c2SDavid E. O'Brien 	    NULL, NULL,				/* lockfunc, lockarg */
1106bfc788c2SDavid E. O'Brien 	    &ring->rx_desc_tag);
1107bfc788c2SDavid E. O'Brien 	if (error != 0) {
1108aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not create desc DMA tag\n");
1109bfc788c2SDavid E. O'Brien 		goto fail;
1110bfc788c2SDavid E. O'Brien 	}
1111bfc788c2SDavid E. O'Brien 
1112bfc788c2SDavid E. O'Brien 	/* allocate memory to desc */
1113aab5582fSPyun YongHyeon 	error = bus_dmamem_alloc(ring->rx_desc_tag, &desc, BUS_DMA_WAITOK |
1114aab5582fSPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &ring->rx_desc_map);
1115bfc788c2SDavid E. O'Brien 	if (error != 0) {
1116aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not create desc DMA map\n");
1117bfc788c2SDavid E. O'Brien 		goto fail;
1118bfc788c2SDavid E. O'Brien 	}
1119aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR)
1120aab5582fSPyun YongHyeon 		ring->desc64 = desc;
1121aab5582fSPyun YongHyeon 	else
1122aab5582fSPyun YongHyeon 		ring->desc32 = desc;
1123bfc788c2SDavid E. O'Brien 
1124bfc788c2SDavid E. O'Brien 	/* map desc to device visible address space */
1125aab5582fSPyun YongHyeon 	ctx.nfe_busaddr = 0;
1126aab5582fSPyun YongHyeon 	error = bus_dmamap_load(ring->rx_desc_tag, ring->rx_desc_map, desc,
1127aab5582fSPyun YongHyeon 	    NFE_RX_RING_COUNT * descsize, nfe_dma_map_segs, &ctx, 0);
1128bfc788c2SDavid E. O'Brien 	if (error != 0) {
1129aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not load desc DMA map\n");
1130aab5582fSPyun YongHyeon 		goto fail;
1131aab5582fSPyun YongHyeon 	}
1132aab5582fSPyun YongHyeon 	ring->physaddr = ctx.nfe_busaddr;
1133aab5582fSPyun YongHyeon 
1134aab5582fSPyun YongHyeon 	error = bus_dma_tag_create(sc->nfe_parent_tag,
1135aab5582fSPyun YongHyeon 	    1, 0,			/* alignment, boundary */
1136aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* lowaddr */
1137aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,		/* highaddr */
1138aab5582fSPyun YongHyeon 	    NULL, NULL,			/* filter, filterarg */
1139aab5582fSPyun YongHyeon 	    MCLBYTES, 1,		/* maxsize, nsegments */
1140aab5582fSPyun YongHyeon 	    MCLBYTES,			/* maxsegsize */
1141aab5582fSPyun YongHyeon 	    0,				/* flags */
1142aab5582fSPyun YongHyeon 	    NULL, NULL,			/* lockfunc, lockarg */
1143aab5582fSPyun YongHyeon 	    &ring->rx_data_tag);
1144aab5582fSPyun YongHyeon 	if (error != 0) {
1145aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not create Rx DMA tag\n");
1146bfc788c2SDavid E. O'Brien 		goto fail;
1147bfc788c2SDavid E. O'Brien 	}
1148bfc788c2SDavid E. O'Brien 
1149aab5582fSPyun YongHyeon 	error = bus_dmamap_create(ring->rx_data_tag, 0, &ring->rx_spare_map);
1150aab5582fSPyun YongHyeon 	if (error != 0) {
1151aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
1152aab5582fSPyun YongHyeon 		    "could not create Rx DMA spare map\n");
1153aab5582fSPyun YongHyeon 		goto fail;
1154aab5582fSPyun YongHyeon 	}
1155bfc788c2SDavid E. O'Brien 
1156bfc788c2SDavid E. O'Brien 	/*
1157bfc788c2SDavid E. O'Brien 	 * Pre-allocate Rx buffers and populate Rx ring.
1158bfc788c2SDavid E. O'Brien 	 */
1159bfc788c2SDavid E. O'Brien 	for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1160bfc788c2SDavid E. O'Brien 		data = &sc->rxq.data[i];
1161aab5582fSPyun YongHyeon 		data->rx_data_map = NULL;
1162aab5582fSPyun YongHyeon 		data->m = NULL;
1163aab5582fSPyun YongHyeon 		error = bus_dmamap_create(ring->rx_data_tag, 0,
11642c3adf61SDavid E. O'Brien 		    &data->rx_data_map);
1165bfc788c2SDavid E. O'Brien 		if (error != 0) {
1166aab5582fSPyun YongHyeon 			device_printf(sc->nfe_dev,
1167aab5582fSPyun YongHyeon 			    "could not create Rx DMA map\n");
1168aab5582fSPyun YongHyeon 			goto fail;
1169aab5582fSPyun YongHyeon 		}
1170aab5582fSPyun YongHyeon 	}
1171aab5582fSPyun YongHyeon 
1172aab5582fSPyun YongHyeon fail:
1173aab5582fSPyun YongHyeon 	return (error);
1174aab5582fSPyun YongHyeon }
1175aab5582fSPyun YongHyeon 
11768b590ad2SPyun YongHyeon static void
nfe_alloc_jrx_ring(struct nfe_softc * sc,struct nfe_jrx_ring * ring)1177aab5582fSPyun YongHyeon nfe_alloc_jrx_ring(struct nfe_softc *sc, struct nfe_jrx_ring *ring)
1178aab5582fSPyun YongHyeon {
1179aab5582fSPyun YongHyeon 	struct nfe_dmamap_arg ctx;
1180aab5582fSPyun YongHyeon 	struct nfe_rx_data *data;
1181aab5582fSPyun YongHyeon 	void *desc;
1182aab5582fSPyun YongHyeon 	int i, error, descsize;
1183aab5582fSPyun YongHyeon 
1184aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_JUMBO_SUP) == 0)
11858b590ad2SPyun YongHyeon 		return;
11868b590ad2SPyun YongHyeon 	if (jumbo_disable != 0) {
11878b590ad2SPyun YongHyeon 		device_printf(sc->nfe_dev, "disabling jumbo frame support\n");
11888b590ad2SPyun YongHyeon 		sc->nfe_jumbo_disable = 1;
11898b590ad2SPyun YongHyeon 		return;
11908b590ad2SPyun YongHyeon 	}
1191aab5582fSPyun YongHyeon 
1192aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1193aab5582fSPyun YongHyeon 		desc = ring->jdesc64;
1194aab5582fSPyun YongHyeon 		descsize = sizeof (struct nfe_desc64);
1195aab5582fSPyun YongHyeon 	} else {
1196aab5582fSPyun YongHyeon 		desc = ring->jdesc32;
1197aab5582fSPyun YongHyeon 		descsize = sizeof (struct nfe_desc32);
1198aab5582fSPyun YongHyeon 	}
1199aab5582fSPyun YongHyeon 
1200aab5582fSPyun YongHyeon 	ring->jcur = ring->jnext = 0;
1201aab5582fSPyun YongHyeon 
1202aab5582fSPyun YongHyeon 	/* Create DMA tag for jumbo Rx ring. */
1203aab5582fSPyun YongHyeon 	error = bus_dma_tag_create(sc->nfe_parent_tag,
1204aab5582fSPyun YongHyeon 	    NFE_RING_ALIGN, 0,			/* alignment, boundary */
1205aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1206aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,			/* highaddr */
1207aab5582fSPyun YongHyeon 	    NULL, NULL,				/* filter, filterarg */
1208aab5582fSPyun YongHyeon 	    NFE_JUMBO_RX_RING_COUNT * descsize,	/* maxsize */
1209aab5582fSPyun YongHyeon 	    1, 					/* nsegments */
1210aab5582fSPyun YongHyeon 	    NFE_JUMBO_RX_RING_COUNT * descsize,	/* maxsegsize */
1211aab5582fSPyun YongHyeon 	    0,					/* flags */
1212aab5582fSPyun YongHyeon 	    NULL, NULL,				/* lockfunc, lockarg */
1213aab5582fSPyun YongHyeon 	    &ring->jrx_desc_tag);
1214aab5582fSPyun YongHyeon 	if (error != 0) {
1215aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
1216aab5582fSPyun YongHyeon 		    "could not create jumbo ring DMA tag\n");
1217bfc788c2SDavid E. O'Brien 		goto fail;
1218bfc788c2SDavid E. O'Brien 	}
1219bfc788c2SDavid E. O'Brien 
1220aab5582fSPyun YongHyeon 	/* Create DMA tag for jumbo Rx buffers. */
1221aab5582fSPyun YongHyeon 	error = bus_dma_tag_create(sc->nfe_parent_tag,
1222767fb36cSPyun YongHyeon 	    1, 0,				/* alignment, boundary */
1223aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1224aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,			/* highaddr */
1225aab5582fSPyun YongHyeon 	    NULL, NULL,				/* filter, filterarg */
12263a5d5a69SPyun YongHyeon 	    MJUM9BYTES,				/* maxsize */
1227aab5582fSPyun YongHyeon 	    1,					/* nsegments */
12283a5d5a69SPyun YongHyeon 	    MJUM9BYTES,				/* maxsegsize */
1229aab5582fSPyun YongHyeon 	    0,					/* flags */
1230aab5582fSPyun YongHyeon 	    NULL, NULL,				/* lockfunc, lockarg */
1231aab5582fSPyun YongHyeon 	    &ring->jrx_data_tag);
1232aab5582fSPyun YongHyeon 	if (error != 0) {
1233aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
1234aab5582fSPyun YongHyeon 		    "could not create jumbo Rx buffer DMA tag\n");
1235aab5582fSPyun YongHyeon 		goto fail;
1236aab5582fSPyun YongHyeon 	}
1237aab5582fSPyun YongHyeon 
1238aab5582fSPyun YongHyeon 	/* Allocate DMA'able memory and load the DMA map for jumbo Rx ring. */
1239aab5582fSPyun YongHyeon 	error = bus_dmamem_alloc(ring->jrx_desc_tag, &desc, BUS_DMA_WAITOK |
1240aab5582fSPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &ring->jrx_desc_map);
1241aab5582fSPyun YongHyeon 	if (error != 0) {
1242aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
1243aab5582fSPyun YongHyeon 		    "could not allocate DMA'able memory for jumbo Rx ring\n");
1244aab5582fSPyun YongHyeon 		goto fail;
1245aab5582fSPyun YongHyeon 	}
1246aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR)
1247aab5582fSPyun YongHyeon 		ring->jdesc64 = desc;
1248aab5582fSPyun YongHyeon 	else
1249aab5582fSPyun YongHyeon 		ring->jdesc32 = desc;
1250aab5582fSPyun YongHyeon 
1251aab5582fSPyun YongHyeon 	ctx.nfe_busaddr = 0;
1252aab5582fSPyun YongHyeon 	error = bus_dmamap_load(ring->jrx_desc_tag, ring->jrx_desc_map, desc,
1253aab5582fSPyun YongHyeon 	    NFE_JUMBO_RX_RING_COUNT * descsize, nfe_dma_map_segs, &ctx, 0);
1254aab5582fSPyun YongHyeon 	if (error != 0) {
1255aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
1256aab5582fSPyun YongHyeon 		    "could not load DMA'able memory for jumbo Rx ring\n");
1257aab5582fSPyun YongHyeon 		goto fail;
1258aab5582fSPyun YongHyeon 	}
1259aab5582fSPyun YongHyeon 	ring->jphysaddr = ctx.nfe_busaddr;
1260aab5582fSPyun YongHyeon 
1261aab5582fSPyun YongHyeon 	/* Create DMA maps for jumbo Rx buffers. */
1262aab5582fSPyun YongHyeon 	error = bus_dmamap_create(ring->jrx_data_tag, 0, &ring->jrx_spare_map);
1263aab5582fSPyun YongHyeon 	if (error != 0) {
1264aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
1265aab5582fSPyun YongHyeon 		    "could not create jumbo Rx DMA spare map\n");
1266aab5582fSPyun YongHyeon 		goto fail;
1267aab5582fSPyun YongHyeon 	}
1268aab5582fSPyun YongHyeon 
1269aab5582fSPyun YongHyeon 	for (i = 0; i < NFE_JUMBO_RX_RING_COUNT; i++) {
1270aab5582fSPyun YongHyeon 		data = &sc->jrxq.jdata[i];
1271aab5582fSPyun YongHyeon 		data->rx_data_map = NULL;
1272aab5582fSPyun YongHyeon 		data->m = NULL;
1273aab5582fSPyun YongHyeon 		error = bus_dmamap_create(ring->jrx_data_tag, 0,
1274aab5582fSPyun YongHyeon 		    &data->rx_data_map);
1275aab5582fSPyun YongHyeon 		if (error != 0) {
1276aab5582fSPyun YongHyeon 			device_printf(sc->nfe_dev,
1277aab5582fSPyun YongHyeon 			    "could not create jumbo Rx DMA map\n");
1278aab5582fSPyun YongHyeon 			goto fail;
1279aab5582fSPyun YongHyeon 		}
1280aab5582fSPyun YongHyeon 	}
1281aab5582fSPyun YongHyeon 
12828b590ad2SPyun YongHyeon 	return;
1283bfc788c2SDavid E. O'Brien 
1284aab5582fSPyun YongHyeon fail:
12858b590ad2SPyun YongHyeon 	/*
12868b590ad2SPyun YongHyeon 	 * Running without jumbo frame support is ok for most cases
12878b590ad2SPyun YongHyeon 	 * so don't fail on creating dma tag/map for jumbo frame.
12888b590ad2SPyun YongHyeon 	 */
1289aab5582fSPyun YongHyeon 	nfe_free_jrx_ring(sc, ring);
12908b590ad2SPyun YongHyeon 	device_printf(sc->nfe_dev, "disabling jumbo frame support due to "
12918b590ad2SPyun YongHyeon 	    "resource shortage\n");
12928b590ad2SPyun YongHyeon 	sc->nfe_jumbo_disable = 1;
1293bfc788c2SDavid E. O'Brien }
1294bfc788c2SDavid E. O'Brien 
1295aab5582fSPyun YongHyeon static int
nfe_init_rx_ring(struct nfe_softc * sc,struct nfe_rx_ring * ring)1296aab5582fSPyun YongHyeon nfe_init_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1297bfc788c2SDavid E. O'Brien {
1298aab5582fSPyun YongHyeon 	void *desc;
1299aab5582fSPyun YongHyeon 	size_t descsize;
1300bfc788c2SDavid E. O'Brien 	int i;
1301bfc788c2SDavid E. O'Brien 
1302aab5582fSPyun YongHyeon 	ring->cur = ring->next = 0;
1303bfc788c2SDavid E. O'Brien 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1304aab5582fSPyun YongHyeon 		desc = ring->desc64;
1305aab5582fSPyun YongHyeon 		descsize = sizeof (struct nfe_desc64);
1306bfc788c2SDavid E. O'Brien 	} else {
1307aab5582fSPyun YongHyeon 		desc = ring->desc32;
1308aab5582fSPyun YongHyeon 		descsize = sizeof (struct nfe_desc32);
1309bfc788c2SDavid E. O'Brien 	}
1310aab5582fSPyun YongHyeon 	bzero(desc, descsize * NFE_RX_RING_COUNT);
1311aab5582fSPyun YongHyeon 	for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1312aab5582fSPyun YongHyeon 		if (nfe_newbuf(sc, i) != 0)
1313aab5582fSPyun YongHyeon 			return (ENOBUFS);
1314bfc788c2SDavid E. O'Brien 	}
1315bfc788c2SDavid E. O'Brien 
13162c3adf61SDavid E. O'Brien 	bus_dmamap_sync(ring->rx_desc_tag, ring->rx_desc_map,
1317aab5582fSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1318bfc788c2SDavid E. O'Brien 
1319aab5582fSPyun YongHyeon 	return (0);
1320aab5582fSPyun YongHyeon }
1321aab5582fSPyun YongHyeon 
1322aab5582fSPyun YongHyeon static int
nfe_init_jrx_ring(struct nfe_softc * sc,struct nfe_jrx_ring * ring)1323aab5582fSPyun YongHyeon nfe_init_jrx_ring(struct nfe_softc *sc, struct nfe_jrx_ring *ring)
1324aab5582fSPyun YongHyeon {
1325aab5582fSPyun YongHyeon 	void *desc;
1326aab5582fSPyun YongHyeon 	size_t descsize;
1327aab5582fSPyun YongHyeon 	int i;
1328aab5582fSPyun YongHyeon 
1329aab5582fSPyun YongHyeon 	ring->jcur = ring->jnext = 0;
1330aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1331aab5582fSPyun YongHyeon 		desc = ring->jdesc64;
1332aab5582fSPyun YongHyeon 		descsize = sizeof (struct nfe_desc64);
1333aab5582fSPyun YongHyeon 	} else {
1334aab5582fSPyun YongHyeon 		desc = ring->jdesc32;
1335aab5582fSPyun YongHyeon 		descsize = sizeof (struct nfe_desc32);
1336aab5582fSPyun YongHyeon 	}
13373a5d5a69SPyun YongHyeon 	bzero(desc, descsize * NFE_JUMBO_RX_RING_COUNT);
1338aab5582fSPyun YongHyeon 	for (i = 0; i < NFE_JUMBO_RX_RING_COUNT; i++) {
1339aab5582fSPyun YongHyeon 		if (nfe_jnewbuf(sc, i) != 0)
1340aab5582fSPyun YongHyeon 			return (ENOBUFS);
1341aab5582fSPyun YongHyeon 	}
1342aab5582fSPyun YongHyeon 
1343aab5582fSPyun YongHyeon 	bus_dmamap_sync(ring->jrx_desc_tag, ring->jrx_desc_map,
1344aab5582fSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1345aab5582fSPyun YongHyeon 
1346aab5582fSPyun YongHyeon 	return (0);
1347bfc788c2SDavid E. O'Brien }
1348bfc788c2SDavid E. O'Brien 
1349bfc788c2SDavid E. O'Brien static void
nfe_free_rx_ring(struct nfe_softc * sc,struct nfe_rx_ring * ring)1350bfc788c2SDavid E. O'Brien nfe_free_rx_ring(struct nfe_softc *sc, struct nfe_rx_ring *ring)
1351bfc788c2SDavid E. O'Brien {
1352bfc788c2SDavid E. O'Brien 	struct nfe_rx_data *data;
1353bfc788c2SDavid E. O'Brien 	void *desc;
135473aa8b9aSChristian Brueffer 	int i;
1355bfc788c2SDavid E. O'Brien 
135673aa8b9aSChristian Brueffer 	if (sc->nfe_flags & NFE_40BIT_ADDR)
1357bfc788c2SDavid E. O'Brien 		desc = ring->desc64;
135873aa8b9aSChristian Brueffer 	else
1359bfc788c2SDavid E. O'Brien 		desc = ring->desc32;
1360bfc788c2SDavid E. O'Brien 
1361bfc788c2SDavid E. O'Brien 	for (i = 0; i < NFE_RX_RING_COUNT; i++) {
1362bfc788c2SDavid E. O'Brien 		data = &ring->data[i];
1363bfc788c2SDavid E. O'Brien 		if (data->rx_data_map != NULL) {
1364aab5582fSPyun YongHyeon 			bus_dmamap_destroy(ring->rx_data_tag,
13652c3adf61SDavid E. O'Brien 			    data->rx_data_map);
1366aab5582fSPyun YongHyeon 			data->rx_data_map = NULL;
1367aab5582fSPyun YongHyeon 		}
1368aab5582fSPyun YongHyeon 		if (data->m != NULL) {
1369aab5582fSPyun YongHyeon 			m_freem(data->m);
1370aab5582fSPyun YongHyeon 			data->m = NULL;
1371aab5582fSPyun YongHyeon 		}
1372aab5582fSPyun YongHyeon 	}
1373aab5582fSPyun YongHyeon 	if (ring->rx_data_tag != NULL) {
1374aab5582fSPyun YongHyeon 		if (ring->rx_spare_map != NULL) {
1375aab5582fSPyun YongHyeon 			bus_dmamap_destroy(ring->rx_data_tag,
1376aab5582fSPyun YongHyeon 			    ring->rx_spare_map);
1377aab5582fSPyun YongHyeon 			ring->rx_spare_map = NULL;
1378aab5582fSPyun YongHyeon 		}
1379aab5582fSPyun YongHyeon 		bus_dma_tag_destroy(ring->rx_data_tag);
1380aab5582fSPyun YongHyeon 		ring->rx_data_tag = NULL;
1381bfc788c2SDavid E. O'Brien 	}
13822c3adf61SDavid E. O'Brien 
1383aab5582fSPyun YongHyeon 	if (desc != NULL) {
1384aab5582fSPyun YongHyeon 		bus_dmamap_unload(ring->rx_desc_tag, ring->rx_desc_map);
1385aab5582fSPyun YongHyeon 		bus_dmamem_free(ring->rx_desc_tag, desc, ring->rx_desc_map);
1386aab5582fSPyun YongHyeon 		ring->desc64 = NULL;
1387aab5582fSPyun YongHyeon 		ring->desc32 = NULL;
1388aab5582fSPyun YongHyeon 	}
1389aab5582fSPyun YongHyeon 	if (ring->rx_desc_tag != NULL) {
1390aab5582fSPyun YongHyeon 		bus_dma_tag_destroy(ring->rx_desc_tag);
1391aab5582fSPyun YongHyeon 		ring->rx_desc_tag = NULL;
1392aab5582fSPyun YongHyeon 	}
1393aab5582fSPyun YongHyeon }
1394aab5582fSPyun YongHyeon 
1395aab5582fSPyun YongHyeon static void
nfe_free_jrx_ring(struct nfe_softc * sc,struct nfe_jrx_ring * ring)1396aab5582fSPyun YongHyeon nfe_free_jrx_ring(struct nfe_softc *sc, struct nfe_jrx_ring *ring)
1397aab5582fSPyun YongHyeon {
1398aab5582fSPyun YongHyeon 	struct nfe_rx_data *data;
1399aab5582fSPyun YongHyeon 	void *desc;
1400bc02034cSMateusz Guzik 	int i;
1401aab5582fSPyun YongHyeon 
1402aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_JUMBO_SUP) == 0)
1403aab5582fSPyun YongHyeon 		return;
1404aab5582fSPyun YongHyeon 
1405aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1406aab5582fSPyun YongHyeon 		desc = ring->jdesc64;
1407aab5582fSPyun YongHyeon 	} else {
1408aab5582fSPyun YongHyeon 		desc = ring->jdesc32;
1409aab5582fSPyun YongHyeon 	}
1410aab5582fSPyun YongHyeon 
1411aab5582fSPyun YongHyeon 	for (i = 0; i < NFE_JUMBO_RX_RING_COUNT; i++) {
1412aab5582fSPyun YongHyeon 		data = &ring->jdata[i];
1413aab5582fSPyun YongHyeon 		if (data->rx_data_map != NULL) {
1414aab5582fSPyun YongHyeon 			bus_dmamap_destroy(ring->jrx_data_tag,
1415aab5582fSPyun YongHyeon 			    data->rx_data_map);
1416aab5582fSPyun YongHyeon 			data->rx_data_map = NULL;
1417aab5582fSPyun YongHyeon 		}
1418aab5582fSPyun YongHyeon 		if (data->m != NULL) {
1419bfc788c2SDavid E. O'Brien 			m_freem(data->m);
1420aab5582fSPyun YongHyeon 			data->m = NULL;
1421aab5582fSPyun YongHyeon 		}
1422aab5582fSPyun YongHyeon 	}
1423aab5582fSPyun YongHyeon 	if (ring->jrx_data_tag != NULL) {
1424aab5582fSPyun YongHyeon 		if (ring->jrx_spare_map != NULL) {
1425aab5582fSPyun YongHyeon 			bus_dmamap_destroy(ring->jrx_data_tag,
1426aab5582fSPyun YongHyeon 			    ring->jrx_spare_map);
1427aab5582fSPyun YongHyeon 			ring->jrx_spare_map = NULL;
1428aab5582fSPyun YongHyeon 		}
1429aab5582fSPyun YongHyeon 		bus_dma_tag_destroy(ring->jrx_data_tag);
1430aab5582fSPyun YongHyeon 		ring->jrx_data_tag = NULL;
1431aab5582fSPyun YongHyeon 	}
1432aab5582fSPyun YongHyeon 
1433aab5582fSPyun YongHyeon 	if (desc != NULL) {
1434aab5582fSPyun YongHyeon 		bus_dmamap_unload(ring->jrx_desc_tag, ring->jrx_desc_map);
1435aab5582fSPyun YongHyeon 		bus_dmamem_free(ring->jrx_desc_tag, desc, ring->jrx_desc_map);
1436aab5582fSPyun YongHyeon 		ring->jdesc64 = NULL;
1437aab5582fSPyun YongHyeon 		ring->jdesc32 = NULL;
1438aab5582fSPyun YongHyeon 	}
14393a5d5a69SPyun YongHyeon 
1440aab5582fSPyun YongHyeon 	if (ring->jrx_desc_tag != NULL) {
1441aab5582fSPyun YongHyeon 		bus_dma_tag_destroy(ring->jrx_desc_tag);
1442aab5582fSPyun YongHyeon 		ring->jrx_desc_tag = NULL;
1443bfc788c2SDavid E. O'Brien 	}
1444bfc788c2SDavid E. O'Brien }
1445bfc788c2SDavid E. O'Brien 
1446bfc788c2SDavid E. O'Brien static int
nfe_alloc_tx_ring(struct nfe_softc * sc,struct nfe_tx_ring * ring)1447bfc788c2SDavid E. O'Brien nfe_alloc_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1448bfc788c2SDavid E. O'Brien {
1449aab5582fSPyun YongHyeon 	struct nfe_dmamap_arg ctx;
1450bfc788c2SDavid E. O'Brien 	int i, error;
1451aab5582fSPyun YongHyeon 	void *desc;
1452bfc788c2SDavid E. O'Brien 	int descsize;
1453bfc788c2SDavid E. O'Brien 
1454bfc788c2SDavid E. O'Brien 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1455aab5582fSPyun YongHyeon 		desc = ring->desc64;
1456bfc788c2SDavid E. O'Brien 		descsize = sizeof (struct nfe_desc64);
1457bfc788c2SDavid E. O'Brien 	} else {
1458aab5582fSPyun YongHyeon 		desc = ring->desc32;
1459bfc788c2SDavid E. O'Brien 		descsize = sizeof (struct nfe_desc32);
1460bfc788c2SDavid E. O'Brien 	}
1461bfc788c2SDavid E. O'Brien 
1462bfc788c2SDavid E. O'Brien 	ring->queued = 0;
1463bfc788c2SDavid E. O'Brien 	ring->cur = ring->next = 0;
1464bfc788c2SDavid E. O'Brien 
1465bfc788c2SDavid E. O'Brien 	error = bus_dma_tag_create(sc->nfe_parent_tag,
1466aab5582fSPyun YongHyeon 	    NFE_RING_ALIGN, 0,			/* alignment, boundary */
1467aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,			/* lowaddr */
1468bfc788c2SDavid E. O'Brien 	    BUS_SPACE_MAXADDR,			/* highaddr */
1469bfc788c2SDavid E. O'Brien 	    NULL, NULL,				/* filter, filterarg */
1470bfc788c2SDavid E. O'Brien 	    NFE_TX_RING_COUNT * descsize, 1,	/* maxsize, nsegments */
1471bfc788c2SDavid E. O'Brien 	    NFE_TX_RING_COUNT * descsize,	/* maxsegsize */
1472aab5582fSPyun YongHyeon 	    0,					/* flags */
1473bfc788c2SDavid E. O'Brien 	    NULL, NULL,				/* lockfunc, lockarg */
1474bfc788c2SDavid E. O'Brien 	    &ring->tx_desc_tag);
1475bfc788c2SDavid E. O'Brien 	if (error != 0) {
1476aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not create desc DMA tag\n");
1477bfc788c2SDavid E. O'Brien 		goto fail;
1478bfc788c2SDavid E. O'Brien 	}
1479bfc788c2SDavid E. O'Brien 
1480aab5582fSPyun YongHyeon 	error = bus_dmamem_alloc(ring->tx_desc_tag, &desc, BUS_DMA_WAITOK |
1481aab5582fSPyun YongHyeon 	    BUS_DMA_COHERENT | BUS_DMA_ZERO, &ring->tx_desc_map);
1482bfc788c2SDavid E. O'Brien 	if (error != 0) {
1483aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not create desc DMA map\n");
1484bfc788c2SDavid E. O'Brien 		goto fail;
1485bfc788c2SDavid E. O'Brien 	}
1486aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR)
1487aab5582fSPyun YongHyeon 		ring->desc64 = desc;
1488aab5582fSPyun YongHyeon 	else
1489aab5582fSPyun YongHyeon 		ring->desc32 = desc;
1490bfc788c2SDavid E. O'Brien 
1491aab5582fSPyun YongHyeon 	ctx.nfe_busaddr = 0;
1492aab5582fSPyun YongHyeon 	error = bus_dmamap_load(ring->tx_desc_tag, ring->tx_desc_map, desc,
1493aab5582fSPyun YongHyeon 	    NFE_TX_RING_COUNT * descsize, nfe_dma_map_segs, &ctx, 0);
1494bfc788c2SDavid E. O'Brien 	if (error != 0) {
1495aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not load desc DMA map\n");
1496bfc788c2SDavid E. O'Brien 		goto fail;
1497bfc788c2SDavid E. O'Brien 	}
1498aab5582fSPyun YongHyeon 	ring->physaddr = ctx.nfe_busaddr;
1499bfc788c2SDavid E. O'Brien 
1500bfc788c2SDavid E. O'Brien 	error = bus_dma_tag_create(sc->nfe_parent_tag,
1501aab5582fSPyun YongHyeon 	    1, 0,
1502aab5582fSPyun YongHyeon 	    BUS_SPACE_MAXADDR,
1503bfc788c2SDavid E. O'Brien 	    BUS_SPACE_MAXADDR,
1504bfc788c2SDavid E. O'Brien 	    NULL, NULL,
15051c889016SPyun YongHyeon 	    NFE_TSO_MAXSIZE,
1506aab5582fSPyun YongHyeon 	    NFE_MAX_SCATTER,
15071c889016SPyun YongHyeon 	    NFE_TSO_MAXSGSIZE,
1508aab5582fSPyun YongHyeon 	    0,
1509bfc788c2SDavid E. O'Brien 	    NULL, NULL,
1510bfc788c2SDavid E. O'Brien 	    &ring->tx_data_tag);
1511bfc788c2SDavid E. O'Brien 	if (error != 0) {
1512aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev, "could not create Tx DMA tag\n");
1513bfc788c2SDavid E. O'Brien 		goto fail;
1514bfc788c2SDavid E. O'Brien 	}
1515bfc788c2SDavid E. O'Brien 
1516bfc788c2SDavid E. O'Brien 	for (i = 0; i < NFE_TX_RING_COUNT; i++) {
15172c3adf61SDavid E. O'Brien 		error = bus_dmamap_create(ring->tx_data_tag, 0,
15182c3adf61SDavid E. O'Brien 		    &ring->data[i].tx_data_map);
1519bfc788c2SDavid E. O'Brien 		if (error != 0) {
1520aab5582fSPyun YongHyeon 			device_printf(sc->nfe_dev,
1521aab5582fSPyun YongHyeon 			    "could not create Tx DMA map\n");
1522bfc788c2SDavid E. O'Brien 			goto fail;
1523bfc788c2SDavid E. O'Brien 		}
1524bfc788c2SDavid E. O'Brien 	}
1525bfc788c2SDavid E. O'Brien 
1526aab5582fSPyun YongHyeon fail:
1527aab5582fSPyun YongHyeon 	return (error);
1528bfc788c2SDavid E. O'Brien }
1529bfc788c2SDavid E. O'Brien 
1530bfc788c2SDavid E. O'Brien static void
nfe_init_tx_ring(struct nfe_softc * sc,struct nfe_tx_ring * ring)1531aab5582fSPyun YongHyeon nfe_init_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1532bfc788c2SDavid E. O'Brien {
1533aab5582fSPyun YongHyeon 	void *desc;
1534aab5582fSPyun YongHyeon 	size_t descsize;
1535bfc788c2SDavid E. O'Brien 
1536aab5582fSPyun YongHyeon 	sc->nfe_force_tx = 0;
1537bfc788c2SDavid E. O'Brien 	ring->queued = 0;
1538bfc788c2SDavid E. O'Brien 	ring->cur = ring->next = 0;
1539aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1540aab5582fSPyun YongHyeon 		desc = ring->desc64;
1541aab5582fSPyun YongHyeon 		descsize = sizeof (struct nfe_desc64);
1542aab5582fSPyun YongHyeon 	} else {
1543aab5582fSPyun YongHyeon 		desc = ring->desc32;
1544aab5582fSPyun YongHyeon 		descsize = sizeof (struct nfe_desc32);
1545aab5582fSPyun YongHyeon 	}
1546aab5582fSPyun YongHyeon 	bzero(desc, descsize * NFE_TX_RING_COUNT);
1547aab5582fSPyun YongHyeon 
1548aab5582fSPyun YongHyeon 	bus_dmamap_sync(ring->tx_desc_tag, ring->tx_desc_map,
1549aab5582fSPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1550bfc788c2SDavid E. O'Brien }
1551bfc788c2SDavid E. O'Brien 
1552bfc788c2SDavid E. O'Brien static void
nfe_free_tx_ring(struct nfe_softc * sc,struct nfe_tx_ring * ring)1553bfc788c2SDavid E. O'Brien nfe_free_tx_ring(struct nfe_softc *sc, struct nfe_tx_ring *ring)
1554bfc788c2SDavid E. O'Brien {
1555bfc788c2SDavid E. O'Brien 	struct nfe_tx_data *data;
1556bfc788c2SDavid E. O'Brien 	void *desc;
1557bc02034cSMateusz Guzik 	int i;
1558bfc788c2SDavid E. O'Brien 
1559bfc788c2SDavid E. O'Brien 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1560bfc788c2SDavid E. O'Brien 		desc = ring->desc64;
1561bfc788c2SDavid E. O'Brien 	} else {
1562bfc788c2SDavid E. O'Brien 		desc = ring->desc32;
1563bfc788c2SDavid E. O'Brien 	}
1564bfc788c2SDavid E. O'Brien 
1565aab5582fSPyun YongHyeon 	for (i = 0; i < NFE_TX_RING_COUNT; i++) {
1566aab5582fSPyun YongHyeon 		data = &ring->data[i];
1567aab5582fSPyun YongHyeon 
1568aab5582fSPyun YongHyeon 		if (data->m != NULL) {
1569aab5582fSPyun YongHyeon 			bus_dmamap_sync(ring->tx_data_tag, data->tx_data_map,
1570aab5582fSPyun YongHyeon 			    BUS_DMASYNC_POSTWRITE);
1571aab5582fSPyun YongHyeon 			bus_dmamap_unload(ring->tx_data_tag, data->tx_data_map);
1572aab5582fSPyun YongHyeon 			m_freem(data->m);
1573aab5582fSPyun YongHyeon 			data->m = NULL;
1574aab5582fSPyun YongHyeon 		}
1575aab5582fSPyun YongHyeon 		if (data->tx_data_map != NULL) {
1576aab5582fSPyun YongHyeon 			bus_dmamap_destroy(ring->tx_data_tag,
1577aab5582fSPyun YongHyeon 			    data->tx_data_map);
1578aab5582fSPyun YongHyeon 			data->tx_data_map = NULL;
1579aab5582fSPyun YongHyeon 		}
1580aab5582fSPyun YongHyeon 	}
1581aab5582fSPyun YongHyeon 
1582aab5582fSPyun YongHyeon 	if (ring->tx_data_tag != NULL) {
1583aab5582fSPyun YongHyeon 		bus_dma_tag_destroy(ring->tx_data_tag);
1584aab5582fSPyun YongHyeon 		ring->tx_data_tag = NULL;
1585aab5582fSPyun YongHyeon 	}
1586aab5582fSPyun YongHyeon 
1587bfc788c2SDavid E. O'Brien 	if (desc != NULL) {
15882c3adf61SDavid E. O'Brien 		bus_dmamap_sync(ring->tx_desc_tag, ring->tx_desc_map,
15892c3adf61SDavid E. O'Brien 		    BUS_DMASYNC_POSTWRITE);
1590bfc788c2SDavid E. O'Brien 		bus_dmamap_unload(ring->tx_desc_tag, ring->tx_desc_map);
1591bfc788c2SDavid E. O'Brien 		bus_dmamem_free(ring->tx_desc_tag, desc, ring->tx_desc_map);
1592aab5582fSPyun YongHyeon 		ring->desc64 = NULL;
1593aab5582fSPyun YongHyeon 		ring->desc32 = NULL;
1594bfc788c2SDavid E. O'Brien 		bus_dma_tag_destroy(ring->tx_desc_tag);
1595aab5582fSPyun YongHyeon 		ring->tx_desc_tag = NULL;
1596bfc788c2SDavid E. O'Brien 	}
1597bfc788c2SDavid E. O'Brien }
1598bfc788c2SDavid E. O'Brien 
1599bfc788c2SDavid E. O'Brien #ifdef DEVICE_POLLING
1600bd071d4dSGleb Smirnoff static poll_handler_t nfe_poll;
1601bfc788c2SDavid E. O'Brien 
16021abcdbd1SAttilio Rao static int
nfe_poll(if_t ifp,enum poll_cmd cmd,int count)1603c2df7334SMarcel Moolenaar nfe_poll(if_t ifp, enum poll_cmd cmd, int count)
1604bfc788c2SDavid E. O'Brien {
1605c2df7334SMarcel Moolenaar 	struct nfe_softc *sc = if_getsoftc(ifp);
1606aab5582fSPyun YongHyeon 	uint32_t r;
16071abcdbd1SAttilio Rao 	int rx_npkts = 0;
1608bfc788c2SDavid E. O'Brien 
1609bfc788c2SDavid E. O'Brien 	NFE_LOCK(sc);
1610bfc788c2SDavid E. O'Brien 
1611c2df7334SMarcel Moolenaar 	if (!(if_getdrvflags(ifp) & IFF_DRV_RUNNING)) {
1612aab5582fSPyun YongHyeon 		NFE_UNLOCK(sc);
16131abcdbd1SAttilio Rao 		return (rx_npkts);
1614bfc788c2SDavid E. O'Brien 	}
1615bfc788c2SDavid E. O'Brien 
16168b590ad2SPyun YongHyeon 	if (sc->nfe_framesize > MCLBYTES - ETHER_HDR_LEN)
16171abcdbd1SAttilio Rao 		rx_npkts = nfe_jrxeof(sc, count, &rx_npkts);
16188b590ad2SPyun YongHyeon 	else
16191abcdbd1SAttilio Rao 		rx_npkts = nfe_rxeof(sc, count, &rx_npkts);
1620bfc788c2SDavid E. O'Brien 	nfe_txeof(sc);
1621c2df7334SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
162232341ad6SJohn Baldwin 		nfe_start_locked(ifp);
1623bfc788c2SDavid E. O'Brien 
1624bfc788c2SDavid E. O'Brien 	if (cmd == POLL_AND_CHECK_STATUS) {
1625aab5582fSPyun YongHyeon 		if ((r = NFE_READ(sc, sc->nfe_irq_status)) == 0) {
1626aab5582fSPyun YongHyeon 			NFE_UNLOCK(sc);
16271abcdbd1SAttilio Rao 			return (rx_npkts);
1628bfc788c2SDavid E. O'Brien 		}
1629aab5582fSPyun YongHyeon 		NFE_WRITE(sc, sc->nfe_irq_status, r);
1630257c5577SDavid E. O'Brien 
1631257c5577SDavid E. O'Brien 		if (r & NFE_IRQ_LINK) {
1632257c5577SDavid E. O'Brien 			NFE_READ(sc, NFE_PHY_STATUS);
1633257c5577SDavid E. O'Brien 			NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
1634aab5582fSPyun YongHyeon 			DPRINTF(sc, "link state changed\n");
1635257c5577SDavid E. O'Brien 		}
1636257c5577SDavid E. O'Brien 	}
1637aab5582fSPyun YongHyeon 	NFE_UNLOCK(sc);
16381abcdbd1SAttilio Rao 	return (rx_npkts);
1639257c5577SDavid E. O'Brien }
1640bfc788c2SDavid E. O'Brien #endif /* DEVICE_POLLING */
1641257c5577SDavid E. O'Brien 
1642aab5582fSPyun YongHyeon static void
nfe_set_intr(struct nfe_softc * sc)1643aab5582fSPyun YongHyeon nfe_set_intr(struct nfe_softc *sc)
1644aab5582fSPyun YongHyeon {
1645aab5582fSPyun YongHyeon 
1646aab5582fSPyun YongHyeon 	if (sc->nfe_msi != 0)
1647aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_IRQ_MASK, NFE_IRQ_WANTED);
1648aab5582fSPyun YongHyeon }
1649aab5582fSPyun YongHyeon 
1650aab5582fSPyun YongHyeon /* In MSIX, a write to mask reegisters behaves as XOR. */
1651aab5582fSPyun YongHyeon static __inline void
nfe_enable_intr(struct nfe_softc * sc)1652aab5582fSPyun YongHyeon nfe_enable_intr(struct nfe_softc *sc)
1653aab5582fSPyun YongHyeon {
1654aab5582fSPyun YongHyeon 
1655aab5582fSPyun YongHyeon 	if (sc->nfe_msix != 0) {
1656aab5582fSPyun YongHyeon 		/* XXX Should have a better way to enable interrupts! */
1657aab5582fSPyun YongHyeon 		if (NFE_READ(sc, sc->nfe_irq_mask) == 0)
1658aab5582fSPyun YongHyeon 			NFE_WRITE(sc, sc->nfe_irq_mask, sc->nfe_intrs);
1659aab5582fSPyun YongHyeon 	} else
1660aab5582fSPyun YongHyeon 		NFE_WRITE(sc, sc->nfe_irq_mask, sc->nfe_intrs);
1661aab5582fSPyun YongHyeon }
1662aab5582fSPyun YongHyeon 
1663aab5582fSPyun YongHyeon static __inline void
nfe_disable_intr(struct nfe_softc * sc)1664aab5582fSPyun YongHyeon nfe_disable_intr(struct nfe_softc *sc)
1665aab5582fSPyun YongHyeon {
1666aab5582fSPyun YongHyeon 
1667aab5582fSPyun YongHyeon 	if (sc->nfe_msix != 0) {
1668aab5582fSPyun YongHyeon 		/* XXX Should have a better way to disable interrupts! */
1669aab5582fSPyun YongHyeon 		if (NFE_READ(sc, sc->nfe_irq_mask) != 0)
1670aab5582fSPyun YongHyeon 			NFE_WRITE(sc, sc->nfe_irq_mask, sc->nfe_nointrs);
1671aab5582fSPyun YongHyeon 	} else
1672aab5582fSPyun YongHyeon 		NFE_WRITE(sc, sc->nfe_irq_mask, sc->nfe_nointrs);
1673aab5582fSPyun YongHyeon }
1674aab5582fSPyun YongHyeon 
1675bfc788c2SDavid E. O'Brien static int
nfe_ioctl(if_t ifp,u_long cmd,caddr_t data)1676c2df7334SMarcel Moolenaar nfe_ioctl(if_t ifp, u_long cmd, caddr_t data)
1677257c5577SDavid E. O'Brien {
1678aab5582fSPyun YongHyeon 	struct nfe_softc *sc;
1679aab5582fSPyun YongHyeon 	struct ifreq *ifr;
1680bfc788c2SDavid E. O'Brien 	struct mii_data *mii;
1681aab5582fSPyun YongHyeon 	int error, init, mask;
1682257c5577SDavid E. O'Brien 
1683c2df7334SMarcel Moolenaar 	sc = if_getsoftc(ifp);
1684aab5582fSPyun YongHyeon 	ifr = (struct ifreq *) data;
1685aab5582fSPyun YongHyeon 	error = 0;
1686aab5582fSPyun YongHyeon 	init = 0;
1687257c5577SDavid E. O'Brien 	switch (cmd) {
1688257c5577SDavid E. O'Brien 	case SIOCSIFMTU:
1689aab5582fSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN || ifr->ifr_mtu > NFE_JUMBO_MTU)
1690257c5577SDavid E. O'Brien 			error = EINVAL;
1691c2df7334SMarcel Moolenaar 		else if (if_getmtu(ifp) != ifr->ifr_mtu) {
16928b590ad2SPyun YongHyeon 			if ((((sc->nfe_flags & NFE_JUMBO_SUP) == 0) ||
16938b590ad2SPyun YongHyeon 			    (sc->nfe_jumbo_disable != 0)) &&
1694aab5582fSPyun YongHyeon 			    ifr->ifr_mtu > ETHERMTU)
1695aab5582fSPyun YongHyeon 				error = EINVAL;
1696aab5582fSPyun YongHyeon 			else {
16976124fe21SDavid E. O'Brien 				NFE_LOCK(sc);
1698c2df7334SMarcel Moolenaar 				if_setmtu(ifp, ifr->ifr_mtu);
1699c2df7334SMarcel Moolenaar 				if (if_getdrvflags(ifp) & IFF_DRV_RUNNING) {
1700c2df7334SMarcel Moolenaar 					if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
17016124fe21SDavid E. O'Brien 					nfe_init_locked(sc);
17024de89d86SPyun YongHyeon 				}
17036124fe21SDavid E. O'Brien 				NFE_UNLOCK(sc);
1704aab5582fSPyun YongHyeon 			}
1705bfc788c2SDavid E. O'Brien 		}
1706257c5577SDavid E. O'Brien 		break;
1707257c5577SDavid E. O'Brien 	case SIOCSIFFLAGS:
1708bfc788c2SDavid E. O'Brien 		NFE_LOCK(sc);
1709c2df7334SMarcel Moolenaar 		if (if_getflags(ifp) & IFF_UP) {
1710257c5577SDavid E. O'Brien 			/*
1711257c5577SDavid E. O'Brien 			 * If only the PROMISC or ALLMULTI flag changes, then
1712257c5577SDavid E. O'Brien 			 * don't do a full re-init of the chip, just update
1713257c5577SDavid E. O'Brien 			 * the Rx filter.
1714257c5577SDavid E. O'Brien 			 */
1715c2df7334SMarcel Moolenaar 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) &&
1716c2df7334SMarcel Moolenaar 			    ((if_getflags(ifp) ^ sc->nfe_if_flags) &
1717bfc788c2SDavid E. O'Brien 			     (IFF_ALLMULTI | IFF_PROMISC)) != 0)
1718257c5577SDavid E. O'Brien 				nfe_setmulti(sc);
1719bfc788c2SDavid E. O'Brien 			else
1720bfc788c2SDavid E. O'Brien 				nfe_init_locked(sc);
1721257c5577SDavid E. O'Brien 		} else {
1722c2df7334SMarcel Moolenaar 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1723aab5582fSPyun YongHyeon 				nfe_stop(ifp);
1724257c5577SDavid E. O'Brien 		}
1725c2df7334SMarcel Moolenaar 		sc->nfe_if_flags = if_getflags(ifp);
1726bfc788c2SDavid E. O'Brien 		NFE_UNLOCK(sc);
1727bfc788c2SDavid E. O'Brien 		error = 0;
1728257c5577SDavid E. O'Brien 		break;
1729257c5577SDavid E. O'Brien 	case SIOCADDMULTI:
1730257c5577SDavid E. O'Brien 	case SIOCDELMULTI:
1731c2df7334SMarcel Moolenaar 		if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
1732bfc788c2SDavid E. O'Brien 			NFE_LOCK(sc);
1733257c5577SDavid E. O'Brien 			nfe_setmulti(sc);
1734bfc788c2SDavid E. O'Brien 			NFE_UNLOCK(sc);
1735257c5577SDavid E. O'Brien 			error = 0;
1736257c5577SDavid E. O'Brien 		}
1737257c5577SDavid E. O'Brien 		break;
1738257c5577SDavid E. O'Brien 	case SIOCSIFMEDIA:
1739257c5577SDavid E. O'Brien 	case SIOCGIFMEDIA:
1740bfc788c2SDavid E. O'Brien 		mii = device_get_softc(sc->nfe_miibus);
174109a8241fSGleb Smirnoff 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, cmd);
1742257c5577SDavid E. O'Brien 		break;
1743bfc788c2SDavid E. O'Brien 	case SIOCSIFCAP:
1744c2df7334SMarcel Moolenaar 		mask = ifr->ifr_reqcap ^ if_getcapenable(ifp);
1745bfc788c2SDavid E. O'Brien #ifdef DEVICE_POLLING
1746aab5582fSPyun YongHyeon 		if ((mask & IFCAP_POLLING) != 0) {
1747aab5582fSPyun YongHyeon 			if ((ifr->ifr_reqcap & IFCAP_POLLING) != 0) {
1748bd071d4dSGleb Smirnoff 				error = ether_poll_register(nfe_poll, ifp);
1749bfc788c2SDavid E. O'Brien 				if (error)
1750aab5582fSPyun YongHyeon 					break;
1751bfc788c2SDavid E. O'Brien 				NFE_LOCK(sc);
1752aab5582fSPyun YongHyeon 				nfe_disable_intr(sc);
1753c2df7334SMarcel Moolenaar 				if_setcapenablebit(ifp, IFCAP_POLLING, 0);
1754bfc788c2SDavid E. O'Brien 				NFE_UNLOCK(sc);
1755bfc788c2SDavid E. O'Brien 			} else {
1756bfc788c2SDavid E. O'Brien 				error = ether_poll_deregister(ifp);
1757bfc788c2SDavid E. O'Brien 				/* Enable interrupt even in error case */
1758bfc788c2SDavid E. O'Brien 				NFE_LOCK(sc);
1759aab5582fSPyun YongHyeon 				nfe_enable_intr(sc);
1760c2df7334SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_POLLING);
1761bfc788c2SDavid E. O'Brien 				NFE_UNLOCK(sc);
1762257c5577SDavid E. O'Brien 			}
1763bfc788c2SDavid E. O'Brien 		}
17642c3adf61SDavid E. O'Brien #endif /* DEVICE_POLLING */
176552a1393eSPyun YongHyeon 		if ((mask & IFCAP_WOL_MAGIC) != 0 &&
1766c2df7334SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_WOL_MAGIC) != 0)
1767c2df7334SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_WOL_MAGIC);
17682f4fcd48SPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
1769c2df7334SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TXCSUM) != 0) {
1770c2df7334SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TXCSUM);
1771c2df7334SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_TXCSUM) != 0)
1772c2df7334SMarcel Moolenaar 				if_sethwassistbits(ifp, NFE_CSUM_FEATURES, 0);
1773bfc788c2SDavid E. O'Brien 			else
1774c2df7334SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, NFE_CSUM_FEATURES);
17752f4fcd48SPyun YongHyeon 		}
17762f4fcd48SPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
1777c2df7334SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_RXCSUM) != 0) {
1778c2df7334SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_RXCSUM);
1779aab5582fSPyun YongHyeon 			init++;
1780bfc788c2SDavid E. O'Brien 		}
17812f4fcd48SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
1782c2df7334SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_TSO4) != 0) {
1783c2df7334SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_TSO4);
1784c2df7334SMarcel Moolenaar 			if ((IFCAP_TSO4 & if_getcapenable(ifp)) != 0)
1785c2df7334SMarcel Moolenaar 				if_sethwassistbits(ifp, CSUM_TSO, 0);
17862f4fcd48SPyun YongHyeon 			else
1787c2df7334SMarcel Moolenaar 				if_sethwassistbits(ifp, 0, CSUM_TSO);
17882f4fcd48SPyun YongHyeon 		}
17892f4fcd48SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
1790c2df7334SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTSO) != 0)
1791c2df7334SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTSO);
17922f4fcd48SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
1793c2df7334SMarcel Moolenaar 		    (if_getcapabilities(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
1794c2df7334SMarcel Moolenaar 			if_togglecapenable(ifp, IFCAP_VLAN_HWTAGGING);
1795c2df7334SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) == 0)
1796c2df7334SMarcel Moolenaar 				if_setcapenablebit(ifp, 0, IFCAP_VLAN_HWTSO);
1797aab5582fSPyun YongHyeon 			init++;
1798aab5582fSPyun YongHyeon 		}
1799aab5582fSPyun YongHyeon 		/*
1800aab5582fSPyun YongHyeon 		 * XXX
1801aab5582fSPyun YongHyeon 		 * It seems that VLAN stripping requires Rx checksum offload.
1802aab5582fSPyun YongHyeon 		 * Unfortunately FreeBSD has no way to disable only Rx side
1803aab5582fSPyun YongHyeon 		 * VLAN stripping. So when we know Rx checksum offload is
1804aab5582fSPyun YongHyeon 		 * disabled turn entire hardware VLAN assist off.
1805aab5582fSPyun YongHyeon 		 */
1806c2df7334SMarcel Moolenaar 		if ((if_getcapenable(ifp) & IFCAP_RXCSUM) == 0) {
1807c2df7334SMarcel Moolenaar 			if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
18082f4fcd48SPyun YongHyeon 				init++;
1809c2df7334SMarcel Moolenaar 			if_setcapenablebit(ifp, 0,
1810c2df7334SMarcel Moolenaar 			    (IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWTSO));
1811aab5582fSPyun YongHyeon 		}
1812c2df7334SMarcel Moolenaar 		if (init > 0 && (if_getdrvflags(ifp) & IFF_DRV_RUNNING) != 0) {
1813c2df7334SMarcel Moolenaar 			if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
18147597761aSDavid E. O'Brien 			nfe_init(sc);
1815bfc788c2SDavid E. O'Brien 		}
1816c2df7334SMarcel Moolenaar 		if_vlancap(ifp);
1817bfc788c2SDavid E. O'Brien 		break;
1818bfc788c2SDavid E. O'Brien 	default:
181909a8241fSGleb Smirnoff 		error = ether_ioctl(ifp, cmd, data);
1820bfc788c2SDavid E. O'Brien 		break;
1821bfc788c2SDavid E. O'Brien 	}
1822257c5577SDavid E. O'Brien 
1823aab5582fSPyun YongHyeon 	return (error);
1824aab5582fSPyun YongHyeon }
1825aab5582fSPyun YongHyeon 
1826aab5582fSPyun YongHyeon static int
nfe_intr(void * arg)1827aab5582fSPyun YongHyeon nfe_intr(void *arg)
1828aab5582fSPyun YongHyeon {
1829aab5582fSPyun YongHyeon 	struct nfe_softc *sc;
1830aab5582fSPyun YongHyeon 	uint32_t status;
1831aab5582fSPyun YongHyeon 
1832aab5582fSPyun YongHyeon 	sc = (struct nfe_softc *)arg;
1833aab5582fSPyun YongHyeon 
1834aab5582fSPyun YongHyeon 	status = NFE_READ(sc, sc->nfe_irq_status);
1835aab5582fSPyun YongHyeon 	if (status == 0 || status == 0xffffffff)
1836aab5582fSPyun YongHyeon 		return (FILTER_STRAY);
1837aab5582fSPyun YongHyeon 	nfe_disable_intr(sc);
1838cbc4d2dbSJohn Baldwin 	taskqueue_enqueue(sc->nfe_tq, &sc->nfe_int_task);
1839aab5582fSPyun YongHyeon 
1840aab5582fSPyun YongHyeon 	return (FILTER_HANDLED);
1841257c5577SDavid E. O'Brien }
1842257c5577SDavid E. O'Brien 
18432c3adf61SDavid E. O'Brien static void
nfe_int_task(void * arg,int pending)1844aab5582fSPyun YongHyeon nfe_int_task(void *arg, int pending)
1845bfc788c2SDavid E. O'Brien {
1846bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc = arg;
1847c2df7334SMarcel Moolenaar 	if_t ifp = sc->nfe_ifp;
1848aab5582fSPyun YongHyeon 	uint32_t r;
1849aab5582fSPyun YongHyeon 	int domore;
1850bfc788c2SDavid E. O'Brien 
1851bfc788c2SDavid E. O'Brien 	NFE_LOCK(sc);
1852bfc788c2SDavid E. O'Brien 
1853aab5582fSPyun YongHyeon 	if ((r = NFE_READ(sc, sc->nfe_irq_status)) == 0) {
1854aab5582fSPyun YongHyeon 		nfe_enable_intr(sc);
1855aab5582fSPyun YongHyeon 		NFE_UNLOCK(sc);
1856aab5582fSPyun YongHyeon 		return;	/* not for us */
1857aab5582fSPyun YongHyeon 	}
1858aab5582fSPyun YongHyeon 	NFE_WRITE(sc, sc->nfe_irq_status, r);
1859aab5582fSPyun YongHyeon 
1860aab5582fSPyun YongHyeon 	DPRINTFN(sc, 5, "nfe_intr: interrupt register %x\n", r);
1861aab5582fSPyun YongHyeon 
1862bfc788c2SDavid E. O'Brien #ifdef DEVICE_POLLING
1863c2df7334SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING) {
1864bfc788c2SDavid E. O'Brien 		NFE_UNLOCK(sc);
1865bfc788c2SDavid E. O'Brien 		return;
1866bfc788c2SDavid E. O'Brien 	}
1867bfc788c2SDavid E. O'Brien #endif
1868bfc788c2SDavid E. O'Brien 
1869bfc788c2SDavid E. O'Brien 	if (r & NFE_IRQ_LINK) {
1870bfc788c2SDavid E. O'Brien 		NFE_READ(sc, NFE_PHY_STATUS);
1871bfc788c2SDavid E. O'Brien 		NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
1872aab5582fSPyun YongHyeon 		DPRINTF(sc, "link state changed\n");
1873bfc788c2SDavid E. O'Brien 	}
1874bfc788c2SDavid E. O'Brien 
1875c2df7334SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0) {
18760142a9b1SPyun YongHyeon 		NFE_UNLOCK(sc);
187720e43c4aSPyun YongHyeon 		nfe_disable_intr(sc);
18780142a9b1SPyun YongHyeon 		return;
18790142a9b1SPyun YongHyeon 	}
18800142a9b1SPyun YongHyeon 
1881aab5582fSPyun YongHyeon 	domore = 0;
1882bfc788c2SDavid E. O'Brien 	/* check Rx ring */
1883aab5582fSPyun YongHyeon 	if (sc->nfe_framesize > MCLBYTES - ETHER_HDR_LEN)
18841abcdbd1SAttilio Rao 		domore = nfe_jrxeof(sc, sc->nfe_process_limit, NULL);
1885aab5582fSPyun YongHyeon 	else
18861abcdbd1SAttilio Rao 		domore = nfe_rxeof(sc, sc->nfe_process_limit, NULL);
1887bfc788c2SDavid E. O'Brien 	/* check Tx ring */
1888bfc788c2SDavid E. O'Brien 	nfe_txeof(sc);
1889bfc788c2SDavid E. O'Brien 
1890c2df7334SMarcel Moolenaar 	if (!if_sendq_empty(ifp))
189132341ad6SJohn Baldwin 		nfe_start_locked(ifp);
1892bfc788c2SDavid E. O'Brien 
1893bfc788c2SDavid E. O'Brien 	NFE_UNLOCK(sc);
1894bfc788c2SDavid E. O'Brien 
1895aab5582fSPyun YongHyeon 	if (domore || (NFE_READ(sc, sc->nfe_irq_status) != 0)) {
1896cbc4d2dbSJohn Baldwin 		taskqueue_enqueue(sc->nfe_tq, &sc->nfe_int_task);
1897bfc788c2SDavid E. O'Brien 		return;
1898bfc788c2SDavid E. O'Brien 	}
1899bfc788c2SDavid E. O'Brien 
1900aab5582fSPyun YongHyeon 	/* Reenable interrupts. */
1901aab5582fSPyun YongHyeon 	nfe_enable_intr(sc);
1902257c5577SDavid E. O'Brien }
1903257c5577SDavid E. O'Brien 
1904aab5582fSPyun YongHyeon static __inline void
nfe_discard_rxbuf(struct nfe_softc * sc,int idx)1905aab5582fSPyun YongHyeon nfe_discard_rxbuf(struct nfe_softc *sc, int idx)
1906257c5577SDavid E. O'Brien {
1907aab5582fSPyun YongHyeon 	struct nfe_desc32 *desc32;
1908aab5582fSPyun YongHyeon 	struct nfe_desc64 *desc64;
1909aab5582fSPyun YongHyeon 	struct nfe_rx_data *data;
1910aab5582fSPyun YongHyeon 	struct mbuf *m;
19112c3adf61SDavid E. O'Brien 
1912aab5582fSPyun YongHyeon 	data = &sc->rxq.data[idx];
1913aab5582fSPyun YongHyeon 	m = data->m;
1914aab5582fSPyun YongHyeon 
1915aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1916aab5582fSPyun YongHyeon 		desc64 = &sc->rxq.desc64[idx];
1917aab5582fSPyun YongHyeon 		/* VLAN packet may have overwritten it. */
1918aab5582fSPyun YongHyeon 		desc64->physaddr[0] = htole32(NFE_ADDR_HI(data->paddr));
1919aab5582fSPyun YongHyeon 		desc64->physaddr[1] = htole32(NFE_ADDR_LO(data->paddr));
1920aab5582fSPyun YongHyeon 		desc64->length = htole16(m->m_len);
1921aab5582fSPyun YongHyeon 		desc64->flags = htole16(NFE_RX_READY);
1922aab5582fSPyun YongHyeon 	} else {
1923aab5582fSPyun YongHyeon 		desc32 = &sc->rxq.desc32[idx];
1924aab5582fSPyun YongHyeon 		desc32->length = htole16(m->m_len);
1925aab5582fSPyun YongHyeon 		desc32->flags = htole16(NFE_RX_READY);
1926aab5582fSPyun YongHyeon 	}
1927257c5577SDavid E. O'Brien }
1928257c5577SDavid E. O'Brien 
1929aab5582fSPyun YongHyeon static __inline void
nfe_discard_jrxbuf(struct nfe_softc * sc,int idx)1930aab5582fSPyun YongHyeon nfe_discard_jrxbuf(struct nfe_softc *sc, int idx)
1931257c5577SDavid E. O'Brien {
1932aab5582fSPyun YongHyeon 	struct nfe_desc32 *desc32;
1933aab5582fSPyun YongHyeon 	struct nfe_desc64 *desc64;
1934aab5582fSPyun YongHyeon 	struct nfe_rx_data *data;
1935aab5582fSPyun YongHyeon 	struct mbuf *m;
19362c3adf61SDavid E. O'Brien 
1937aab5582fSPyun YongHyeon 	data = &sc->jrxq.jdata[idx];
1938aab5582fSPyun YongHyeon 	m = data->m;
1939aab5582fSPyun YongHyeon 
1940aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1941aab5582fSPyun YongHyeon 		desc64 = &sc->jrxq.jdesc64[idx];
1942aab5582fSPyun YongHyeon 		/* VLAN packet may have overwritten it. */
1943aab5582fSPyun YongHyeon 		desc64->physaddr[0] = htole32(NFE_ADDR_HI(data->paddr));
1944aab5582fSPyun YongHyeon 		desc64->physaddr[1] = htole32(NFE_ADDR_LO(data->paddr));
1945aab5582fSPyun YongHyeon 		desc64->length = htole16(m->m_len);
1946aab5582fSPyun YongHyeon 		desc64->flags = htole16(NFE_RX_READY);
1947aab5582fSPyun YongHyeon 	} else {
1948aab5582fSPyun YongHyeon 		desc32 = &sc->jrxq.jdesc32[idx];
1949aab5582fSPyun YongHyeon 		desc32->length = htole16(m->m_len);
1950aab5582fSPyun YongHyeon 		desc32->flags = htole16(NFE_RX_READY);
1951aab5582fSPyun YongHyeon 	}
1952257c5577SDavid E. O'Brien }
1953257c5577SDavid E. O'Brien 
1954aab5582fSPyun YongHyeon static int
nfe_newbuf(struct nfe_softc * sc,int idx)1955aab5582fSPyun YongHyeon nfe_newbuf(struct nfe_softc *sc, int idx)
1956257c5577SDavid E. O'Brien {
1957aab5582fSPyun YongHyeon 	struct nfe_rx_data *data;
1958aab5582fSPyun YongHyeon 	struct nfe_desc32 *desc32;
1959aab5582fSPyun YongHyeon 	struct nfe_desc64 *desc64;
1960aab5582fSPyun YongHyeon 	struct mbuf *m;
1961aab5582fSPyun YongHyeon 	bus_dma_segment_t segs[1];
1962aab5582fSPyun YongHyeon 	bus_dmamap_t map;
1963aab5582fSPyun YongHyeon 	int nsegs;
19642c3adf61SDavid E. O'Brien 
1965c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1966aab5582fSPyun YongHyeon 	if (m == NULL)
1967aab5582fSPyun YongHyeon 		return (ENOBUFS);
1968aab5582fSPyun YongHyeon 
1969aab5582fSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MCLBYTES;
1970aab5582fSPyun YongHyeon 	m_adj(m, ETHER_ALIGN);
1971aab5582fSPyun YongHyeon 
1972aab5582fSPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->rxq.rx_data_tag, sc->rxq.rx_spare_map,
1973aab5582fSPyun YongHyeon 	    m, segs, &nsegs, BUS_DMA_NOWAIT) != 0) {
1974aab5582fSPyun YongHyeon 		m_freem(m);
1975aab5582fSPyun YongHyeon 		return (ENOBUFS);
1976aab5582fSPyun YongHyeon 	}
1977aab5582fSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
1978aab5582fSPyun YongHyeon 
1979aab5582fSPyun YongHyeon 	data = &sc->rxq.data[idx];
1980aab5582fSPyun YongHyeon 	if (data->m != NULL) {
1981aab5582fSPyun YongHyeon 		bus_dmamap_sync(sc->rxq.rx_data_tag, data->rx_data_map,
1982aab5582fSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1983aab5582fSPyun YongHyeon 		bus_dmamap_unload(sc->rxq.rx_data_tag, data->rx_data_map);
1984aab5582fSPyun YongHyeon 	}
1985aab5582fSPyun YongHyeon 	map = data->rx_data_map;
1986aab5582fSPyun YongHyeon 	data->rx_data_map = sc->rxq.rx_spare_map;
1987aab5582fSPyun YongHyeon 	sc->rxq.rx_spare_map = map;
1988aab5582fSPyun YongHyeon 	bus_dmamap_sync(sc->rxq.rx_data_tag, data->rx_data_map,
1989aab5582fSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
1990aab5582fSPyun YongHyeon 	data->paddr = segs[0].ds_addr;
1991aab5582fSPyun YongHyeon 	data->m = m;
1992aab5582fSPyun YongHyeon 	/* update mapping address in h/w descriptor */
1993aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
1994aab5582fSPyun YongHyeon 		desc64 = &sc->rxq.desc64[idx];
1995aab5582fSPyun YongHyeon 		desc64->physaddr[0] = htole32(NFE_ADDR_HI(segs[0].ds_addr));
1996aab5582fSPyun YongHyeon 		desc64->physaddr[1] = htole32(NFE_ADDR_LO(segs[0].ds_addr));
1997aab5582fSPyun YongHyeon 		desc64->length = htole16(segs[0].ds_len);
1998aab5582fSPyun YongHyeon 		desc64->flags = htole16(NFE_RX_READY);
1999aab5582fSPyun YongHyeon 	} else {
2000aab5582fSPyun YongHyeon 		desc32 = &sc->rxq.desc32[idx];
2001aab5582fSPyun YongHyeon 		desc32->physaddr = htole32(NFE_ADDR_LO(segs[0].ds_addr));
2002aab5582fSPyun YongHyeon 		desc32->length = htole16(segs[0].ds_len);
2003aab5582fSPyun YongHyeon 		desc32->flags = htole16(NFE_RX_READY);
2004aab5582fSPyun YongHyeon 	}
2005aab5582fSPyun YongHyeon 
2006aab5582fSPyun YongHyeon 	return (0);
2007257c5577SDavid E. O'Brien }
2008257c5577SDavid E. O'Brien 
2009aab5582fSPyun YongHyeon static int
nfe_jnewbuf(struct nfe_softc * sc,int idx)2010aab5582fSPyun YongHyeon nfe_jnewbuf(struct nfe_softc *sc, int idx)
2011257c5577SDavid E. O'Brien {
2012aab5582fSPyun YongHyeon 	struct nfe_rx_data *data;
2013aab5582fSPyun YongHyeon 	struct nfe_desc32 *desc32;
2014aab5582fSPyun YongHyeon 	struct nfe_desc64 *desc64;
2015aab5582fSPyun YongHyeon 	struct mbuf *m;
2016aab5582fSPyun YongHyeon 	bus_dma_segment_t segs[1];
2017aab5582fSPyun YongHyeon 	bus_dmamap_t map;
2018aab5582fSPyun YongHyeon 	int nsegs;
20192c3adf61SDavid E. O'Brien 
2020c6499eccSGleb Smirnoff 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
2021aab5582fSPyun YongHyeon 	if (m == NULL)
2022aab5582fSPyun YongHyeon 		return (ENOBUFS);
20233a5d5a69SPyun YongHyeon 	m->m_pkthdr.len = m->m_len = MJUM9BYTES;
2024aab5582fSPyun YongHyeon 	m_adj(m, ETHER_ALIGN);
2025aab5582fSPyun YongHyeon 
2026aab5582fSPyun YongHyeon 	if (bus_dmamap_load_mbuf_sg(sc->jrxq.jrx_data_tag,
2027aab5582fSPyun YongHyeon 	    sc->jrxq.jrx_spare_map, m, segs, &nsegs, BUS_DMA_NOWAIT) != 0) {
2028aab5582fSPyun YongHyeon 		m_freem(m);
2029aab5582fSPyun YongHyeon 		return (ENOBUFS);
2030aab5582fSPyun YongHyeon 	}
2031aab5582fSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segments returned!", __func__, nsegs));
2032aab5582fSPyun YongHyeon 
2033aab5582fSPyun YongHyeon 	data = &sc->jrxq.jdata[idx];
2034aab5582fSPyun YongHyeon 	if (data->m != NULL) {
2035aab5582fSPyun YongHyeon 		bus_dmamap_sync(sc->jrxq.jrx_data_tag, data->rx_data_map,
2036aab5582fSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
2037aab5582fSPyun YongHyeon 		bus_dmamap_unload(sc->jrxq.jrx_data_tag, data->rx_data_map);
2038aab5582fSPyun YongHyeon 	}
2039aab5582fSPyun YongHyeon 	map = data->rx_data_map;
2040aab5582fSPyun YongHyeon 	data->rx_data_map = sc->jrxq.jrx_spare_map;
2041aab5582fSPyun YongHyeon 	sc->jrxq.jrx_spare_map = map;
2042aab5582fSPyun YongHyeon 	bus_dmamap_sync(sc->jrxq.jrx_data_tag, data->rx_data_map,
2043aab5582fSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
2044aab5582fSPyun YongHyeon 	data->paddr = segs[0].ds_addr;
2045aab5582fSPyun YongHyeon 	data->m = m;
2046aab5582fSPyun YongHyeon 	/* update mapping address in h/w descriptor */
2047aab5582fSPyun YongHyeon 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
2048aab5582fSPyun YongHyeon 		desc64 = &sc->jrxq.jdesc64[idx];
2049aab5582fSPyun YongHyeon 		desc64->physaddr[0] = htole32(NFE_ADDR_HI(segs[0].ds_addr));
2050aab5582fSPyun YongHyeon 		desc64->physaddr[1] = htole32(NFE_ADDR_LO(segs[0].ds_addr));
2051aab5582fSPyun YongHyeon 		desc64->length = htole16(segs[0].ds_len);
2052aab5582fSPyun YongHyeon 		desc64->flags = htole16(NFE_RX_READY);
2053aab5582fSPyun YongHyeon 	} else {
2054aab5582fSPyun YongHyeon 		desc32 = &sc->jrxq.jdesc32[idx];
2055aab5582fSPyun YongHyeon 		desc32->physaddr = htole32(NFE_ADDR_LO(segs[0].ds_addr));
2056aab5582fSPyun YongHyeon 		desc32->length = htole16(segs[0].ds_len);
2057aab5582fSPyun YongHyeon 		desc32->flags = htole16(NFE_RX_READY);
2058aab5582fSPyun YongHyeon 	}
2059aab5582fSPyun YongHyeon 
2060aab5582fSPyun YongHyeon 	return (0);
2061257c5577SDavid E. O'Brien }
2062257c5577SDavid E. O'Brien 
2063aab5582fSPyun YongHyeon static int
nfe_rxeof(struct nfe_softc * sc,int count,int * rx_npktsp)20641abcdbd1SAttilio Rao nfe_rxeof(struct nfe_softc *sc, int count, int *rx_npktsp)
2065257c5577SDavid E. O'Brien {
2066c2df7334SMarcel Moolenaar 	if_t ifp = sc->nfe_ifp;
2067aab5582fSPyun YongHyeon 	struct nfe_desc32 *desc32;
2068aab5582fSPyun YongHyeon 	struct nfe_desc64 *desc64;
2069257c5577SDavid E. O'Brien 	struct nfe_rx_data *data;
2070aab5582fSPyun YongHyeon 	struct mbuf *m;
2071aab5582fSPyun YongHyeon 	uint16_t flags;
20721abcdbd1SAttilio Rao 	int len, prog, rx_npkts;
2073aab5582fSPyun YongHyeon 	uint32_t vtag = 0;
2074bfc788c2SDavid E. O'Brien 
20751abcdbd1SAttilio Rao 	rx_npkts = 0;
2076bfc788c2SDavid E. O'Brien 	NFE_LOCK_ASSERT(sc);
2077257c5577SDavid E. O'Brien 
2078aab5582fSPyun YongHyeon 	bus_dmamap_sync(sc->rxq.rx_desc_tag, sc->rxq.rx_desc_map,
2079aab5582fSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
2080bfc788c2SDavid E. O'Brien 
2081aab5582fSPyun YongHyeon 	for (prog = 0;;NFE_INC(sc->rxq.cur, NFE_RX_RING_COUNT), vtag = 0) {
2082aab5582fSPyun YongHyeon 		if (count <= 0)
2083bfc788c2SDavid E. O'Brien 			break;
2084aab5582fSPyun YongHyeon 		count--;
2085bfc788c2SDavid E. O'Brien 
2086257c5577SDavid E. O'Brien 		data = &sc->rxq.data[sc->rxq.cur];
2087257c5577SDavid E. O'Brien 
2088bfc788c2SDavid E. O'Brien 		if (sc->nfe_flags & NFE_40BIT_ADDR) {
2089257c5577SDavid E. O'Brien 			desc64 = &sc->rxq.desc64[sc->rxq.cur];
2090aab5582fSPyun YongHyeon 			vtag = le32toh(desc64->physaddr[1]);
2091aab5582fSPyun YongHyeon 			flags = le16toh(desc64->flags);
2092aab5582fSPyun YongHyeon 			len = le16toh(desc64->length) & NFE_RX_LEN_MASK;
2093257c5577SDavid E. O'Brien 		} else {
2094257c5577SDavid E. O'Brien 			desc32 = &sc->rxq.desc32[sc->rxq.cur];
2095aab5582fSPyun YongHyeon 			flags = le16toh(desc32->flags);
2096aab5582fSPyun YongHyeon 			len = le16toh(desc32->length) & NFE_RX_LEN_MASK;
2097257c5577SDavid E. O'Brien 		}
2098257c5577SDavid E. O'Brien 
2099257c5577SDavid E. O'Brien 		if (flags & NFE_RX_READY)
2100257c5577SDavid E. O'Brien 			break;
2101aab5582fSPyun YongHyeon 		prog++;
2102bfc788c2SDavid E. O'Brien 		if ((sc->nfe_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) {
2103aab5582fSPyun YongHyeon 			if (!(flags & NFE_RX_VALID_V1)) {
2104df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2105aab5582fSPyun YongHyeon 				nfe_discard_rxbuf(sc, sc->rxq.cur);
2106aab5582fSPyun YongHyeon 				continue;
2107aab5582fSPyun YongHyeon 			}
2108257c5577SDavid E. O'Brien 			if ((flags & NFE_RX_FIXME_V1) == NFE_RX_FIXME_V1) {
2109257c5577SDavid E. O'Brien 				flags &= ~NFE_RX_ERROR;
2110257c5577SDavid E. O'Brien 				len--;	/* fix buffer length */
2111257c5577SDavid E. O'Brien 			}
2112257c5577SDavid E. O'Brien 		} else {
2113aab5582fSPyun YongHyeon 			if (!(flags & NFE_RX_VALID_V2)) {
2114df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2115aab5582fSPyun YongHyeon 				nfe_discard_rxbuf(sc, sc->rxq.cur);
2116aab5582fSPyun YongHyeon 				continue;
2117aab5582fSPyun YongHyeon 			}
2118257c5577SDavid E. O'Brien 
2119257c5577SDavid E. O'Brien 			if ((flags & NFE_RX_FIXME_V2) == NFE_RX_FIXME_V2) {
2120257c5577SDavid E. O'Brien 				flags &= ~NFE_RX_ERROR;
2121257c5577SDavid E. O'Brien 				len--;	/* fix buffer length */
2122257c5577SDavid E. O'Brien 			}
2123257c5577SDavid E. O'Brien 		}
2124257c5577SDavid E. O'Brien 
2125257c5577SDavid E. O'Brien 		if (flags & NFE_RX_ERROR) {
2126df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2127aab5582fSPyun YongHyeon 			nfe_discard_rxbuf(sc, sc->rxq.cur);
2128aab5582fSPyun YongHyeon 			continue;
2129257c5577SDavid E. O'Brien 		}
2130257c5577SDavid E. O'Brien 
2131257c5577SDavid E. O'Brien 		m = data->m;
2132aab5582fSPyun YongHyeon 		if (nfe_newbuf(sc, sc->rxq.cur) != 0) {
2133df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2134aab5582fSPyun YongHyeon 			nfe_discard_rxbuf(sc, sc->rxq.cur);
2135aab5582fSPyun YongHyeon 			continue;
2136aab5582fSPyun YongHyeon 		}
2137257c5577SDavid E. O'Brien 
2138aab5582fSPyun YongHyeon 		if ((vtag & NFE_RX_VTAG) != 0 &&
2139c2df7334SMarcel Moolenaar 		    (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
2140aab5582fSPyun YongHyeon 			m->m_pkthdr.ether_vtag = vtag & 0xffff;
2141aab5582fSPyun YongHyeon 			m->m_flags |= M_VLANTAG;
2142aab5582fSPyun YongHyeon 		}
2143aab5582fSPyun YongHyeon 
2144257c5577SDavid E. O'Brien 		m->m_pkthdr.len = m->m_len = len;
2145257c5577SDavid E. O'Brien 		m->m_pkthdr.rcvif = ifp;
2146257c5577SDavid E. O'Brien 
2147c2df7334SMarcel Moolenaar 		if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0) {
2148aab5582fSPyun YongHyeon 			if ((flags & NFE_RX_IP_CSUMOK) != 0) {
2149bfc788c2SDavid E. O'Brien 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2150bfc788c2SDavid E. O'Brien 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2151aab5582fSPyun YongHyeon 				if ((flags & NFE_RX_TCP_CSUMOK) != 0 ||
2152aab5582fSPyun YongHyeon 				    (flags & NFE_RX_UDP_CSUMOK) != 0) {
2153bfc788c2SDavid E. O'Brien 					m->m_pkthdr.csum_flags |=
2154bfc788c2SDavid E. O'Brien 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2155bfc788c2SDavid E. O'Brien 					m->m_pkthdr.csum_data = 0xffff;
2156bfc788c2SDavid E. O'Brien 				}
2157bfc788c2SDavid E. O'Brien 			}
2158bfc788c2SDavid E. O'Brien 		}
2159aab5582fSPyun YongHyeon 
2160df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2161bfc788c2SDavid E. O'Brien 
2162bfc788c2SDavid E. O'Brien 		NFE_UNLOCK(sc);
2163c2df7334SMarcel Moolenaar 		if_input(ifp, m);
2164bfc788c2SDavid E. O'Brien 		NFE_LOCK(sc);
21651abcdbd1SAttilio Rao 		rx_npkts++;
2166aab5582fSPyun YongHyeon 	}
2167257c5577SDavid E. O'Brien 
2168aab5582fSPyun YongHyeon 	if (prog > 0)
2169aab5582fSPyun YongHyeon 		bus_dmamap_sync(sc->rxq.rx_desc_tag, sc->rxq.rx_desc_map,
2170aab5582fSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2171aab5582fSPyun YongHyeon 
21721abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
21731abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2174aab5582fSPyun YongHyeon 	return (count > 0 ? 0 : EAGAIN);
2175aab5582fSPyun YongHyeon }
2176aab5582fSPyun YongHyeon 
2177aab5582fSPyun YongHyeon static int
nfe_jrxeof(struct nfe_softc * sc,int count,int * rx_npktsp)21781abcdbd1SAttilio Rao nfe_jrxeof(struct nfe_softc *sc, int count, int *rx_npktsp)
2179aab5582fSPyun YongHyeon {
2180c2df7334SMarcel Moolenaar 	if_t ifp = sc->nfe_ifp;
2181aab5582fSPyun YongHyeon 	struct nfe_desc32 *desc32;
2182aab5582fSPyun YongHyeon 	struct nfe_desc64 *desc64;
2183aab5582fSPyun YongHyeon 	struct nfe_rx_data *data;
2184aab5582fSPyun YongHyeon 	struct mbuf *m;
2185aab5582fSPyun YongHyeon 	uint16_t flags;
21861abcdbd1SAttilio Rao 	int len, prog, rx_npkts;
2187aab5582fSPyun YongHyeon 	uint32_t vtag = 0;
2188aab5582fSPyun YongHyeon 
21891abcdbd1SAttilio Rao 	rx_npkts = 0;
2190aab5582fSPyun YongHyeon 	NFE_LOCK_ASSERT(sc);
2191aab5582fSPyun YongHyeon 
2192aab5582fSPyun YongHyeon 	bus_dmamap_sync(sc->jrxq.jrx_desc_tag, sc->jrxq.jrx_desc_map,
2193aab5582fSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
2194aab5582fSPyun YongHyeon 
2195aab5582fSPyun YongHyeon 	for (prog = 0;;NFE_INC(sc->jrxq.jcur, NFE_JUMBO_RX_RING_COUNT),
2196aab5582fSPyun YongHyeon 	    vtag = 0) {
2197aab5582fSPyun YongHyeon 		if (count <= 0)
2198aab5582fSPyun YongHyeon 			break;
2199aab5582fSPyun YongHyeon 		count--;
2200aab5582fSPyun YongHyeon 
2201aab5582fSPyun YongHyeon 		data = &sc->jrxq.jdata[sc->jrxq.jcur];
2202aab5582fSPyun YongHyeon 
2203bfc788c2SDavid E. O'Brien 		if (sc->nfe_flags & NFE_40BIT_ADDR) {
2204aab5582fSPyun YongHyeon 			desc64 = &sc->jrxq.jdesc64[sc->jrxq.jcur];
2205aab5582fSPyun YongHyeon 			vtag = le32toh(desc64->physaddr[1]);
2206aab5582fSPyun YongHyeon 			flags = le16toh(desc64->flags);
2207aab5582fSPyun YongHyeon 			len = le16toh(desc64->length) & NFE_RX_LEN_MASK;
2208257c5577SDavid E. O'Brien 		} else {
2209aab5582fSPyun YongHyeon 			desc32 = &sc->jrxq.jdesc32[sc->jrxq.jcur];
2210aab5582fSPyun YongHyeon 			flags = le16toh(desc32->flags);
2211aab5582fSPyun YongHyeon 			len = le16toh(desc32->length) & NFE_RX_LEN_MASK;
2212257c5577SDavid E. O'Brien 		}
2213257c5577SDavid E. O'Brien 
2214aab5582fSPyun YongHyeon 		if (flags & NFE_RX_READY)
2215aab5582fSPyun YongHyeon 			break;
2216aab5582fSPyun YongHyeon 		prog++;
2217aab5582fSPyun YongHyeon 		if ((sc->nfe_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) {
2218aab5582fSPyun YongHyeon 			if (!(flags & NFE_RX_VALID_V1)) {
2219df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2220aab5582fSPyun YongHyeon 				nfe_discard_jrxbuf(sc, sc->jrxq.jcur);
2221aab5582fSPyun YongHyeon 				continue;
2222aab5582fSPyun YongHyeon 			}
2223aab5582fSPyun YongHyeon 			if ((flags & NFE_RX_FIXME_V1) == NFE_RX_FIXME_V1) {
2224aab5582fSPyun YongHyeon 				flags &= ~NFE_RX_ERROR;
2225aab5582fSPyun YongHyeon 				len--;	/* fix buffer length */
2226aab5582fSPyun YongHyeon 			}
2227257c5577SDavid E. O'Brien 		} else {
2228aab5582fSPyun YongHyeon 			if (!(flags & NFE_RX_VALID_V2)) {
2229df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2230aab5582fSPyun YongHyeon 				nfe_discard_jrxbuf(sc, sc->jrxq.jcur);
2231aab5582fSPyun YongHyeon 				continue;
2232257c5577SDavid E. O'Brien 			}
2233257c5577SDavid E. O'Brien 
2234aab5582fSPyun YongHyeon 			if ((flags & NFE_RX_FIXME_V2) == NFE_RX_FIXME_V2) {
2235aab5582fSPyun YongHyeon 				flags &= ~NFE_RX_ERROR;
2236aab5582fSPyun YongHyeon 				len--;	/* fix buffer length */
2237aab5582fSPyun YongHyeon 			}
2238aab5582fSPyun YongHyeon 		}
2239aab5582fSPyun YongHyeon 
2240aab5582fSPyun YongHyeon 		if (flags & NFE_RX_ERROR) {
2241df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2242aab5582fSPyun YongHyeon 			nfe_discard_jrxbuf(sc, sc->jrxq.jcur);
2243aab5582fSPyun YongHyeon 			continue;
2244aab5582fSPyun YongHyeon 		}
2245aab5582fSPyun YongHyeon 
2246aab5582fSPyun YongHyeon 		m = data->m;
2247aab5582fSPyun YongHyeon 		if (nfe_jnewbuf(sc, sc->jrxq.jcur) != 0) {
2248df360178SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2249aab5582fSPyun YongHyeon 			nfe_discard_jrxbuf(sc, sc->jrxq.jcur);
2250aab5582fSPyun YongHyeon 			continue;
2251aab5582fSPyun YongHyeon 		}
2252aab5582fSPyun YongHyeon 
2253aab5582fSPyun YongHyeon 		if ((vtag & NFE_RX_VTAG) != 0 &&
2254c2df7334SMarcel Moolenaar 		    (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0) {
2255aab5582fSPyun YongHyeon 			m->m_pkthdr.ether_vtag = vtag & 0xffff;
2256aab5582fSPyun YongHyeon 			m->m_flags |= M_VLANTAG;
2257aab5582fSPyun YongHyeon 		}
2258aab5582fSPyun YongHyeon 
2259aab5582fSPyun YongHyeon 		m->m_pkthdr.len = m->m_len = len;
2260aab5582fSPyun YongHyeon 		m->m_pkthdr.rcvif = ifp;
2261aab5582fSPyun YongHyeon 
2262c2df7334SMarcel Moolenaar 		if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0) {
2263aab5582fSPyun YongHyeon 			if ((flags & NFE_RX_IP_CSUMOK) != 0) {
2264aab5582fSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_CHECKED;
2265aab5582fSPyun YongHyeon 				m->m_pkthdr.csum_flags |= CSUM_IP_VALID;
2266aab5582fSPyun YongHyeon 				if ((flags & NFE_RX_TCP_CSUMOK) != 0 ||
2267aab5582fSPyun YongHyeon 				    (flags & NFE_RX_UDP_CSUMOK) != 0) {
2268aab5582fSPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2269aab5582fSPyun YongHyeon 					    CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
2270aab5582fSPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
2271aab5582fSPyun YongHyeon 				}
2272aab5582fSPyun YongHyeon 			}
2273aab5582fSPyun YongHyeon 		}
2274aab5582fSPyun YongHyeon 
2275df360178SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2276aab5582fSPyun YongHyeon 
2277aab5582fSPyun YongHyeon 		NFE_UNLOCK(sc);
2278c2df7334SMarcel Moolenaar 		if_input(ifp, m);
2279aab5582fSPyun YongHyeon 		NFE_LOCK(sc);
22801abcdbd1SAttilio Rao 		rx_npkts++;
2281aab5582fSPyun YongHyeon 	}
2282aab5582fSPyun YongHyeon 
2283aab5582fSPyun YongHyeon 	if (prog > 0)
2284aab5582fSPyun YongHyeon 		bus_dmamap_sync(sc->jrxq.jrx_desc_tag, sc->jrxq.jrx_desc_map,
2285aab5582fSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2286aab5582fSPyun YongHyeon 
22871abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
22881abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2289aab5582fSPyun YongHyeon 	return (count > 0 ? 0 : EAGAIN);
2290257c5577SDavid E. O'Brien }
2291257c5577SDavid E. O'Brien 
22922c3adf61SDavid E. O'Brien static void
nfe_txeof(struct nfe_softc * sc)22932c3adf61SDavid E. O'Brien nfe_txeof(struct nfe_softc *sc)
2294257c5577SDavid E. O'Brien {
2295c2df7334SMarcel Moolenaar 	if_t ifp = sc->nfe_ifp;
2296257c5577SDavid E. O'Brien 	struct nfe_desc32 *desc32;
2297257c5577SDavid E. O'Brien 	struct nfe_desc64 *desc64;
2298257c5577SDavid E. O'Brien 	struct nfe_tx_data *data = NULL;
2299aab5582fSPyun YongHyeon 	uint16_t flags;
2300aab5582fSPyun YongHyeon 	int cons, prog;
2301bfc788c2SDavid E. O'Brien 
2302bfc788c2SDavid E. O'Brien 	NFE_LOCK_ASSERT(sc);
2303257c5577SDavid E. O'Brien 
2304aab5582fSPyun YongHyeon 	bus_dmamap_sync(sc->txq.tx_desc_tag, sc->txq.tx_desc_map,
2305aab5582fSPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
2306aab5582fSPyun YongHyeon 
2307aab5582fSPyun YongHyeon 	prog = 0;
2308aab5582fSPyun YongHyeon 	for (cons = sc->txq.next; cons != sc->txq.cur;
2309aab5582fSPyun YongHyeon 	    NFE_INC(cons, NFE_TX_RING_COUNT)) {
2310bfc788c2SDavid E. O'Brien 		if (sc->nfe_flags & NFE_40BIT_ADDR) {
2311aab5582fSPyun YongHyeon 			desc64 = &sc->txq.desc64[cons];
2312aab5582fSPyun YongHyeon 			flags = le16toh(desc64->flags);
2313257c5577SDavid E. O'Brien 		} else {
2314aab5582fSPyun YongHyeon 			desc32 = &sc->txq.desc32[cons];
2315aab5582fSPyun YongHyeon 			flags = le16toh(desc32->flags);
2316257c5577SDavid E. O'Brien 		}
2317257c5577SDavid E. O'Brien 
2318257c5577SDavid E. O'Brien 		if (flags & NFE_TX_VALID)
2319257c5577SDavid E. O'Brien 			break;
2320257c5577SDavid E. O'Brien 
2321aab5582fSPyun YongHyeon 		prog++;
2322aab5582fSPyun YongHyeon 		sc->txq.queued--;
2323aab5582fSPyun YongHyeon 		data = &sc->txq.data[cons];
2324257c5577SDavid E. O'Brien 
2325bfc788c2SDavid E. O'Brien 		if ((sc->nfe_flags & (NFE_JUMBO_SUP | NFE_40BIT_ADDR)) == 0) {
2326aab5582fSPyun YongHyeon 			if ((flags & NFE_TX_LASTFRAG_V1) == 0)
2327aab5582fSPyun YongHyeon 				continue;
2328257c5577SDavid E. O'Brien 			if ((flags & NFE_TX_ERROR_V1) != 0) {
2329aab5582fSPyun YongHyeon 				device_printf(sc->nfe_dev,
2330aab5582fSPyun YongHyeon 				    "tx v1 error 0x%4b\n", flags, NFE_V1_TXERR);
2331bfc788c2SDavid E. O'Brien 
2332df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2333257c5577SDavid E. O'Brien 			} else
2334df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2335257c5577SDavid E. O'Brien 		} else {
2336aab5582fSPyun YongHyeon 			if ((flags & NFE_TX_LASTFRAG_V2) == 0)
2337aab5582fSPyun YongHyeon 				continue;
2338257c5577SDavid E. O'Brien 			if ((flags & NFE_TX_ERROR_V2) != 0) {
2339aab5582fSPyun YongHyeon 				device_printf(sc->nfe_dev,
2340aab5582fSPyun YongHyeon 				    "tx v2 error 0x%4b\n", flags, NFE_V2_TXERR);
2341df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2342257c5577SDavid E. O'Brien 			} else
2343df360178SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2344257c5577SDavid E. O'Brien 		}
2345257c5577SDavid E. O'Brien 
2346257c5577SDavid E. O'Brien 		/* last fragment of the mbuf chain transmitted */
2347aab5582fSPyun YongHyeon 		KASSERT(data->m != NULL, ("%s: freeing NULL mbuf!", __func__));
2348aab5582fSPyun YongHyeon 		bus_dmamap_sync(sc->txq.tx_data_tag, data->tx_data_map,
2349bfc788c2SDavid E. O'Brien 		    BUS_DMASYNC_POSTWRITE);
2350aab5582fSPyun YongHyeon 		bus_dmamap_unload(sc->txq.tx_data_tag, data->tx_data_map);
2351257c5577SDavid E. O'Brien 		m_freem(data->m);
2352257c5577SDavid E. O'Brien 		data->m = NULL;
2353257c5577SDavid E. O'Brien 	}
2354257c5577SDavid E. O'Brien 
2355aab5582fSPyun YongHyeon 	if (prog > 0) {
2356aab5582fSPyun YongHyeon 		sc->nfe_force_tx = 0;
2357aab5582fSPyun YongHyeon 		sc->txq.next = cons;
2358c2df7334SMarcel Moolenaar 		if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
2359aab5582fSPyun YongHyeon 		if (sc->txq.queued == 0)
2360aab5582fSPyun YongHyeon 			sc->nfe_watchdog_timer = 0;
2361257c5577SDavid E. O'Brien 	}
2362257c5577SDavid E. O'Brien }
2363257c5577SDavid E. O'Brien 
23642c3adf61SDavid E. O'Brien static int
nfe_encap(struct nfe_softc * sc,struct mbuf ** m_head)2365aab5582fSPyun YongHyeon nfe_encap(struct nfe_softc *sc, struct mbuf **m_head)
2366257c5577SDavid E. O'Brien {
2367bfc788c2SDavid E. O'Brien 	struct nfe_desc32 *desc32 = NULL;
2368bfc788c2SDavid E. O'Brien 	struct nfe_desc64 *desc64 = NULL;
2369257c5577SDavid E. O'Brien 	bus_dmamap_t map;
2370bfc788c2SDavid E. O'Brien 	bus_dma_segment_t segs[NFE_MAX_SCATTER];
2371aab5582fSPyun YongHyeon 	int error, i, nsegs, prod, si;
2372edd26b66SAndre Oppermann 	uint32_t tsosegsz;
2373aab5582fSPyun YongHyeon 	uint16_t cflags, flags;
2374aab5582fSPyun YongHyeon 	struct mbuf *m;
2375257c5577SDavid E. O'Brien 
2376aab5582fSPyun YongHyeon 	prod = si = sc->txq.cur;
2377aab5582fSPyun YongHyeon 	map = sc->txq.data[prod].tx_data_map;
2378257c5577SDavid E. O'Brien 
2379aab5582fSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->txq.tx_data_tag, map, *m_head, segs,
2380bfc788c2SDavid E. O'Brien 	    &nsegs, BUS_DMA_NOWAIT);
2381aab5582fSPyun YongHyeon 	if (error == EFBIG) {
2382c6499eccSGleb Smirnoff 		m = m_collapse(*m_head, M_NOWAIT, NFE_MAX_SCATTER);
2383aab5582fSPyun YongHyeon 		if (m == NULL) {
2384aab5582fSPyun YongHyeon 			m_freem(*m_head);
2385aab5582fSPyun YongHyeon 			*m_head = NULL;
2386aab5582fSPyun YongHyeon 			return (ENOBUFS);
2387aab5582fSPyun YongHyeon 		}
2388aab5582fSPyun YongHyeon 		*m_head = m;
2389aab5582fSPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->txq.tx_data_tag, map,
2390aab5582fSPyun YongHyeon 		    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2391257c5577SDavid E. O'Brien 		if (error != 0) {
2392aab5582fSPyun YongHyeon 			m_freem(*m_head);
2393aab5582fSPyun YongHyeon 			*m_head = NULL;
2394aab5582fSPyun YongHyeon 			return (ENOBUFS);
2395aab5582fSPyun YongHyeon 		}
2396aab5582fSPyun YongHyeon 	} else if (error != 0)
2397aab5582fSPyun YongHyeon 		return (error);
2398aab5582fSPyun YongHyeon 	if (nsegs == 0) {
2399aab5582fSPyun YongHyeon 		m_freem(*m_head);
2400aab5582fSPyun YongHyeon 		*m_head = NULL;
2401aab5582fSPyun YongHyeon 		return (EIO);
2402257c5577SDavid E. O'Brien 	}
2403257c5577SDavid E. O'Brien 
2404aab5582fSPyun YongHyeon 	if (sc->txq.queued + nsegs >= NFE_TX_RING_COUNT - 2) {
2405bfc788c2SDavid E. O'Brien 		bus_dmamap_unload(sc->txq.tx_data_tag, map);
2406aab5582fSPyun YongHyeon 		return (ENOBUFS);
2407257c5577SDavid E. O'Brien 	}
2408257c5577SDavid E. O'Brien 
2409aab5582fSPyun YongHyeon 	m = *m_head;
2410aab5582fSPyun YongHyeon 	cflags = flags = 0;
2411edd26b66SAndre Oppermann 	tsosegsz = 0;
24126da6d0a9SPyun YongHyeon 	if ((m->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2413edd26b66SAndre Oppermann 		tsosegsz = (uint32_t)m->m_pkthdr.tso_segsz <<
24146da6d0a9SPyun YongHyeon 		    NFE_TX_TSO_SHIFT;
24156da6d0a9SPyun YongHyeon 		cflags &= ~(NFE_TX_IP_CSUM | NFE_TX_TCP_UDP_CSUM);
24166da6d0a9SPyun YongHyeon 		cflags |= NFE_TX_TSO;
24176da6d0a9SPyun YongHyeon 	} else if ((m->m_pkthdr.csum_flags & NFE_CSUM_FEATURES) != 0) {
2418aab5582fSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_IP) != 0)
2419aab5582fSPyun YongHyeon 			cflags |= NFE_TX_IP_CSUM;
2420aab5582fSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_TCP) != 0)
2421aab5582fSPyun YongHyeon 			cflags |= NFE_TX_TCP_UDP_CSUM;
2422aab5582fSPyun YongHyeon 		if ((m->m_pkthdr.csum_flags & CSUM_UDP) != 0)
2423aab5582fSPyun YongHyeon 			cflags |= NFE_TX_TCP_UDP_CSUM;
2424aab5582fSPyun YongHyeon 	}
2425257c5577SDavid E. O'Brien 
2426bfc788c2SDavid E. O'Brien 	for (i = 0; i < nsegs; i++) {
2427bfc788c2SDavid E. O'Brien 		if (sc->nfe_flags & NFE_40BIT_ADDR) {
2428aab5582fSPyun YongHyeon 			desc64 = &sc->txq.desc64[prod];
2429aab5582fSPyun YongHyeon 			desc64->physaddr[0] =
2430aab5582fSPyun YongHyeon 			    htole32(NFE_ADDR_HI(segs[i].ds_addr));
2431aab5582fSPyun YongHyeon 			desc64->physaddr[1] =
2432aab5582fSPyun YongHyeon 			    htole32(NFE_ADDR_LO(segs[i].ds_addr));
2433aab5582fSPyun YongHyeon 			desc64->vtag = 0;
2434bfc788c2SDavid E. O'Brien 			desc64->length = htole16(segs[i].ds_len - 1);
2435257c5577SDavid E. O'Brien 			desc64->flags = htole16(flags);
2436257c5577SDavid E. O'Brien 		} else {
2437aab5582fSPyun YongHyeon 			desc32 = &sc->txq.desc32[prod];
2438aab5582fSPyun YongHyeon 			desc32->physaddr =
2439aab5582fSPyun YongHyeon 			    htole32(NFE_ADDR_LO(segs[i].ds_addr));
2440bfc788c2SDavid E. O'Brien 			desc32->length = htole16(segs[i].ds_len - 1);
2441257c5577SDavid E. O'Brien 			desc32->flags = htole16(flags);
2442257c5577SDavid E. O'Brien 		}
2443257c5577SDavid E. O'Brien 
2444aab5582fSPyun YongHyeon 		/*
2445aab5582fSPyun YongHyeon 		 * Setting of the valid bit in the first descriptor is
2446aab5582fSPyun YongHyeon 		 * deferred until the whole chain is fully setup.
2447aab5582fSPyun YongHyeon 		 */
2448aab5582fSPyun YongHyeon 		flags |= NFE_TX_VALID;
2449257c5577SDavid E. O'Brien 
2450257c5577SDavid E. O'Brien 		sc->txq.queued++;
2451aab5582fSPyun YongHyeon 		NFE_INC(prod, NFE_TX_RING_COUNT);
2452257c5577SDavid E. O'Brien 	}
2453257c5577SDavid E. O'Brien 
2454aab5582fSPyun YongHyeon 	/*
2455aab5582fSPyun YongHyeon 	 * the whole mbuf chain has been DMA mapped, fix last/first descriptor.
2456aab5582fSPyun YongHyeon 	 * csum flags, vtag and TSO belong to the first fragment only.
2457aab5582fSPyun YongHyeon 	 */
2458bfc788c2SDavid E. O'Brien 	if (sc->nfe_flags & NFE_40BIT_ADDR) {
2459aab5582fSPyun YongHyeon 		desc64->flags |= htole16(NFE_TX_LASTFRAG_V2);
2460aab5582fSPyun YongHyeon 		desc64 = &sc->txq.desc64[si];
2461aab5582fSPyun YongHyeon 		if ((m->m_flags & M_VLANTAG) != 0)
2462aab5582fSPyun YongHyeon 			desc64->vtag = htole32(NFE_TX_VTAG |
2463aab5582fSPyun YongHyeon 			    m->m_pkthdr.ether_vtag);
2464edd26b66SAndre Oppermann 		if (tsosegsz != 0) {
2465aab5582fSPyun YongHyeon 			/*
2466aab5582fSPyun YongHyeon 			 * XXX
2467aab5582fSPyun YongHyeon 			 * The following indicates the descriptor element
2468aab5582fSPyun YongHyeon 			 * is a 32bit quantity.
2469aab5582fSPyun YongHyeon 			 */
2470edd26b66SAndre Oppermann 			desc64->length |= htole16((uint16_t)tsosegsz);
2471edd26b66SAndre Oppermann 			desc64->flags |= htole16(tsosegsz >> 16);
2472aab5582fSPyun YongHyeon 		}
2473aab5582fSPyun YongHyeon 		/*
2474aab5582fSPyun YongHyeon 		 * finally, set the valid/checksum/TSO bit in the first
2475aab5582fSPyun YongHyeon 		 * descriptor.
2476aab5582fSPyun YongHyeon 		 */
2477aab5582fSPyun YongHyeon 		desc64->flags |= htole16(NFE_TX_VALID | cflags);
2478257c5577SDavid E. O'Brien 	} else {
2479bfc788c2SDavid E. O'Brien 		if (sc->nfe_flags & NFE_JUMBO_SUP)
2480aab5582fSPyun YongHyeon 			desc32->flags |= htole16(NFE_TX_LASTFRAG_V2);
2481257c5577SDavid E. O'Brien 		else
2482aab5582fSPyun YongHyeon 			desc32->flags |= htole16(NFE_TX_LASTFRAG_V1);
2483aab5582fSPyun YongHyeon 		desc32 = &sc->txq.desc32[si];
2484edd26b66SAndre Oppermann 		if (tsosegsz != 0) {
2485aab5582fSPyun YongHyeon 			/*
2486aab5582fSPyun YongHyeon 			 * XXX
2487aab5582fSPyun YongHyeon 			 * The following indicates the descriptor element
2488aab5582fSPyun YongHyeon 			 * is a 32bit quantity.
2489aab5582fSPyun YongHyeon 			 */
2490edd26b66SAndre Oppermann 			desc32->length |= htole16((uint16_t)tsosegsz);
2491edd26b66SAndre Oppermann 			desc32->flags |= htole16(tsosegsz >> 16);
2492aab5582fSPyun YongHyeon 		}
2493aab5582fSPyun YongHyeon 		/*
2494aab5582fSPyun YongHyeon 		 * finally, set the valid/checksum/TSO bit in the first
2495aab5582fSPyun YongHyeon 		 * descriptor.
2496aab5582fSPyun YongHyeon 		 */
2497aab5582fSPyun YongHyeon 		desc32->flags |= htole16(NFE_TX_VALID | cflags);
2498257c5577SDavid E. O'Brien 	}
2499257c5577SDavid E. O'Brien 
2500aab5582fSPyun YongHyeon 	sc->txq.cur = prod;
2501aab5582fSPyun YongHyeon 	prod = (prod + NFE_TX_RING_COUNT - 1) % NFE_TX_RING_COUNT;
2502aab5582fSPyun YongHyeon 	sc->txq.data[si].tx_data_map = sc->txq.data[prod].tx_data_map;
2503aab5582fSPyun YongHyeon 	sc->txq.data[prod].tx_data_map = map;
2504aab5582fSPyun YongHyeon 	sc->txq.data[prod].m = m;
2505257c5577SDavid E. O'Brien 
2506bfc788c2SDavid E. O'Brien 	bus_dmamap_sync(sc->txq.tx_data_tag, map, BUS_DMASYNC_PREWRITE);
2507257c5577SDavid E. O'Brien 
2508aab5582fSPyun YongHyeon 	return (0);
2509257c5577SDavid E. O'Brien }
2510257c5577SDavid E. O'Brien 
25112db0e8ffSGleb Smirnoff struct nfe_hash_maddr_ctx {
25122db0e8ffSGleb Smirnoff 	uint8_t addr[ETHER_ADDR_LEN];
25132db0e8ffSGleb Smirnoff 	uint8_t mask[ETHER_ADDR_LEN];
25142db0e8ffSGleb Smirnoff };
25152db0e8ffSGleb Smirnoff 
25162db0e8ffSGleb Smirnoff static u_int
nfe_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int cnt)25172db0e8ffSGleb Smirnoff nfe_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
25182db0e8ffSGleb Smirnoff {
25192db0e8ffSGleb Smirnoff 	struct nfe_hash_maddr_ctx *ctx = arg;
25202db0e8ffSGleb Smirnoff 	uint8_t *addrp, mcaddr;
25212db0e8ffSGleb Smirnoff 	int j;
25222db0e8ffSGleb Smirnoff 
25232db0e8ffSGleb Smirnoff 	addrp = LLADDR(sdl);
25242db0e8ffSGleb Smirnoff 	for (j = 0; j < ETHER_ADDR_LEN; j++) {
25252db0e8ffSGleb Smirnoff 		mcaddr = addrp[j];
25262db0e8ffSGleb Smirnoff 		ctx->addr[j] &= mcaddr;
25272db0e8ffSGleb Smirnoff 		ctx->mask[j] &= ~mcaddr;
25282db0e8ffSGleb Smirnoff 	}
25292db0e8ffSGleb Smirnoff 
25302db0e8ffSGleb Smirnoff 	return (1);
25312db0e8ffSGleb Smirnoff }
2532bfc788c2SDavid E. O'Brien 
25332c3adf61SDavid E. O'Brien static void
nfe_setmulti(struct nfe_softc * sc)25342c3adf61SDavid E. O'Brien nfe_setmulti(struct nfe_softc *sc)
2535bfc788c2SDavid E. O'Brien {
2536c2df7334SMarcel Moolenaar 	if_t ifp = sc->nfe_ifp;
25372db0e8ffSGleb Smirnoff 	struct nfe_hash_maddr_ctx ctx;
2538aab5582fSPyun YongHyeon 	uint32_t filter;
2539aab5582fSPyun YongHyeon 	uint8_t etherbroadcastaddr[ETHER_ADDR_LEN] = {
25402c3adf61SDavid E. O'Brien 		0xff, 0xff, 0xff, 0xff, 0xff, 0xff
25412c3adf61SDavid E. O'Brien 	};
25422db0e8ffSGleb Smirnoff 	int i;
2543bfc788c2SDavid E. O'Brien 
2544bfc788c2SDavid E. O'Brien 	NFE_LOCK_ASSERT(sc);
2545bfc788c2SDavid E. O'Brien 
2546c2df7334SMarcel Moolenaar 	if ((if_getflags(ifp) & (IFF_ALLMULTI | IFF_PROMISC)) != 0) {
25472db0e8ffSGleb Smirnoff 		bzero(ctx.addr, ETHER_ADDR_LEN);
25482db0e8ffSGleb Smirnoff 		bzero(ctx.mask, ETHER_ADDR_LEN);
2549bfc788c2SDavid E. O'Brien 		goto done;
2550bfc788c2SDavid E. O'Brien 	}
2551bfc788c2SDavid E. O'Brien 
25522db0e8ffSGleb Smirnoff 	bcopy(etherbroadcastaddr, ctx.addr, ETHER_ADDR_LEN);
25532db0e8ffSGleb Smirnoff 	bcopy(etherbroadcastaddr, ctx.mask, ETHER_ADDR_LEN);
2554bfc788c2SDavid E. O'Brien 
25552db0e8ffSGleb Smirnoff 	if_foreach_llmaddr(ifp, nfe_hash_maddr, &ctx);
2556bfc788c2SDavid E. O'Brien 
2557bfc788c2SDavid E. O'Brien 	for (i = 0; i < ETHER_ADDR_LEN; i++) {
25582db0e8ffSGleb Smirnoff 		ctx.mask[i] |= ctx.addr[i];
2559bfc788c2SDavid E. O'Brien 	}
2560bfc788c2SDavid E. O'Brien 
2561bfc788c2SDavid E. O'Brien done:
25622db0e8ffSGleb Smirnoff 	ctx.addr[0] |= 0x01;	/* make sure multicast bit is set */
2563bfc788c2SDavid E. O'Brien 
25642db0e8ffSGleb Smirnoff 	NFE_WRITE(sc, NFE_MULTIADDR_HI, ctx.addr[3] << 24 | ctx.addr[2] << 16 |
25652db0e8ffSGleb Smirnoff 	    ctx.addr[1] << 8 | ctx.addr[0]);
2566bfc788c2SDavid E. O'Brien 	NFE_WRITE(sc, NFE_MULTIADDR_LO,
25672db0e8ffSGleb Smirnoff 	    ctx.addr[5] <<  8 | ctx.addr[4]);
25682db0e8ffSGleb Smirnoff 	NFE_WRITE(sc, NFE_MULTIMASK_HI, ctx.mask[3] << 24 | ctx.mask[2] << 16 |
25692db0e8ffSGleb Smirnoff 	    ctx.mask[1] << 8 | ctx.mask[0]);
2570bfc788c2SDavid E. O'Brien 	NFE_WRITE(sc, NFE_MULTIMASK_LO,
25712db0e8ffSGleb Smirnoff 	    ctx.mask[5] <<  8 | ctx.mask[4]);
2572bfc788c2SDavid E. O'Brien 
2573aab5582fSPyun YongHyeon 	filter = NFE_READ(sc, NFE_RXFILTER);
2574aab5582fSPyun YongHyeon 	filter &= NFE_PFF_RX_PAUSE;
2575aab5582fSPyun YongHyeon 	filter |= NFE_RXFILTER_MAGIC;
2576c2df7334SMarcel Moolenaar 	filter |= (if_getflags(ifp) & IFF_PROMISC) ? NFE_PFF_PROMISC : NFE_PFF_U2M;
2577bfc788c2SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RXFILTER, filter);
2578bfc788c2SDavid E. O'Brien }
2579bfc788c2SDavid E. O'Brien 
25802c3adf61SDavid E. O'Brien static void
nfe_start(if_t ifp)2581c2df7334SMarcel Moolenaar nfe_start(if_t ifp)
2582aab5582fSPyun YongHyeon {
2583c2df7334SMarcel Moolenaar 	struct nfe_softc *sc = if_getsoftc(ifp);
2584aab5582fSPyun YongHyeon 
258532341ad6SJohn Baldwin 	NFE_LOCK(sc);
258632341ad6SJohn Baldwin 	nfe_start_locked(ifp);
258732341ad6SJohn Baldwin 	NFE_UNLOCK(sc);
2588aab5582fSPyun YongHyeon }
2589aab5582fSPyun YongHyeon 
2590aab5582fSPyun YongHyeon static void
nfe_start_locked(if_t ifp)2591c2df7334SMarcel Moolenaar nfe_start_locked(if_t ifp)
2592bfc788c2SDavid E. O'Brien {
2593c2df7334SMarcel Moolenaar 	struct nfe_softc *sc = if_getsoftc(ifp);
2594257c5577SDavid E. O'Brien 	struct mbuf *m0;
2595c2df7334SMarcel Moolenaar 	int enq = 0;
2596257c5577SDavid E. O'Brien 
259732341ad6SJohn Baldwin 	NFE_LOCK_ASSERT(sc);
2598aab5582fSPyun YongHyeon 
2599c2df7334SMarcel Moolenaar 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
260032341ad6SJohn Baldwin 	    IFF_DRV_RUNNING || sc->nfe_link == 0)
2601bfc788c2SDavid E. O'Brien 		return;
2602bfc788c2SDavid E. O'Brien 
2603c2df7334SMarcel Moolenaar 	while (!if_sendq_empty(ifp)) {
2604c2df7334SMarcel Moolenaar 		m0 = if_dequeue(ifp);
2605c2df7334SMarcel Moolenaar 
2606257c5577SDavid E. O'Brien 		if (m0 == NULL)
2607257c5577SDavid E. O'Brien 			break;
2608257c5577SDavid E. O'Brien 
2609aab5582fSPyun YongHyeon 		if (nfe_encap(sc, &m0) != 0) {
2610aab5582fSPyun YongHyeon 			if (m0 == NULL)
2611aab5582fSPyun YongHyeon 				break;
2612c2df7334SMarcel Moolenaar 			if_sendq_prepend(ifp, m0);
2613c2df7334SMarcel Moolenaar 			if_setdrvflagbits(ifp, IFF_DRV_OACTIVE, 0);
2614257c5577SDavid E. O'Brien 			break;
2615257c5577SDavid E. O'Brien 		}
2616aab5582fSPyun YongHyeon 		enq++;
26172a371643SJustin Hibbits 		ether_bpf_mtap_if(ifp, m0);
2618257c5577SDavid E. O'Brien 	}
2619257c5577SDavid E. O'Brien 
2620aab5582fSPyun YongHyeon 	if (enq > 0) {
2621aab5582fSPyun YongHyeon 		bus_dmamap_sync(sc->txq.tx_desc_tag, sc->txq.tx_desc_map,
2622aab5582fSPyun YongHyeon 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2623257c5577SDavid E. O'Brien 
2624257c5577SDavid E. O'Brien 		/* kick Tx */
2625257c5577SDavid E. O'Brien 		NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_KICKTX | sc->rxtxctl);
2626257c5577SDavid E. O'Brien 
2627257c5577SDavid E. O'Brien 		/*
2628257c5577SDavid E. O'Brien 		 * Set a timeout in case the chip goes out to lunch.
2629257c5577SDavid E. O'Brien 		 */
2630aab5582fSPyun YongHyeon 		sc->nfe_watchdog_timer = 5;
2631aab5582fSPyun YongHyeon 	}
2632257c5577SDavid E. O'Brien }
2633257c5577SDavid E. O'Brien 
26342c3adf61SDavid E. O'Brien static void
nfe_watchdog(if_t ifp)2635c2df7334SMarcel Moolenaar nfe_watchdog(if_t ifp)
2636257c5577SDavid E. O'Brien {
2637c2df7334SMarcel Moolenaar 	struct nfe_softc *sc = if_getsoftc(ifp);
2638257c5577SDavid E. O'Brien 
2639aab5582fSPyun YongHyeon 	if (sc->nfe_watchdog_timer == 0 || --sc->nfe_watchdog_timer)
2640aab5582fSPyun YongHyeon 		return;
2641aab5582fSPyun YongHyeon 
2642aab5582fSPyun YongHyeon 	/* Check if we've lost Tx completion interrupt. */
2643aab5582fSPyun YongHyeon 	nfe_txeof(sc);
2644aab5582fSPyun YongHyeon 	if (sc->txq.queued == 0) {
2645aab5582fSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
2646aab5582fSPyun YongHyeon 		    "-- recovering\n");
2647c2df7334SMarcel Moolenaar 		if (!if_sendq_empty(ifp))
264832341ad6SJohn Baldwin 			nfe_start_locked(ifp);
2649aab5582fSPyun YongHyeon 		return;
2650aab5582fSPyun YongHyeon 	}
2651aab5582fSPyun YongHyeon 	/* Check if we've lost start Tx command. */
2652aab5582fSPyun YongHyeon 	sc->nfe_force_tx++;
2653aab5582fSPyun YongHyeon 	if (sc->nfe_force_tx <= 3) {
2654aab5582fSPyun YongHyeon 		/*
2655aab5582fSPyun YongHyeon 		 * If this is the case for watchdog timeout, the following
2656aab5582fSPyun YongHyeon 		 * code should go to nfe_txeof().
2657aab5582fSPyun YongHyeon 		 */
2658aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_KICKTX | sc->rxtxctl);
2659aab5582fSPyun YongHyeon 		return;
2660aab5582fSPyun YongHyeon 	}
2661aab5582fSPyun YongHyeon 	sc->nfe_force_tx = 0;
2662aab5582fSPyun YongHyeon 
2663aab5582fSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
2664257c5577SDavid E. O'Brien 
2665c2df7334SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_RUNNING);
2666df360178SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2667aab5582fSPyun YongHyeon 	nfe_init_locked(sc);
2668257c5577SDavid E. O'Brien }
2669257c5577SDavid E. O'Brien 
26702c3adf61SDavid E. O'Brien static void
nfe_init(void * xsc)26712c3adf61SDavid E. O'Brien nfe_init(void *xsc)
2672257c5577SDavid E. O'Brien {
2673bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc = xsc;
2674bfc788c2SDavid E. O'Brien 
2675bfc788c2SDavid E. O'Brien 	NFE_LOCK(sc);
2676bfc788c2SDavid E. O'Brien 	nfe_init_locked(sc);
2677bfc788c2SDavid E. O'Brien 	NFE_UNLOCK(sc);
2678bfc788c2SDavid E. O'Brien }
2679bfc788c2SDavid E. O'Brien 
26802c3adf61SDavid E. O'Brien static void
nfe_init_locked(void * xsc)26812c3adf61SDavid E. O'Brien nfe_init_locked(void *xsc)
2682bfc788c2SDavid E. O'Brien {
2683bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc = xsc;
2684c2df7334SMarcel Moolenaar 	if_t ifp = sc->nfe_ifp;
2685bfc788c2SDavid E. O'Brien 	struct mii_data *mii;
2686aab5582fSPyun YongHyeon 	uint32_t val;
2687aab5582fSPyun YongHyeon 	int error;
2688bfc788c2SDavid E. O'Brien 
2689bfc788c2SDavid E. O'Brien 	NFE_LOCK_ASSERT(sc);
2690bfc788c2SDavid E. O'Brien 
2691bfc788c2SDavid E. O'Brien 	mii = device_get_softc(sc->nfe_miibus);
2692bfc788c2SDavid E. O'Brien 
2693c2df7334SMarcel Moolenaar 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
2694aab5582fSPyun YongHyeon 		return;
2695aab5582fSPyun YongHyeon 
2696aab5582fSPyun YongHyeon 	nfe_stop(ifp);
2697aab5582fSPyun YongHyeon 
2698c2df7334SMarcel Moolenaar 	sc->nfe_framesize = if_getmtu(ifp) + NFE_RX_HEADERS;
2699aab5582fSPyun YongHyeon 
2700aab5582fSPyun YongHyeon 	nfe_init_tx_ring(sc, &sc->txq);
2701aab5582fSPyun YongHyeon 	if (sc->nfe_framesize > (MCLBYTES - ETHER_HDR_LEN))
2702aab5582fSPyun YongHyeon 		error = nfe_init_jrx_ring(sc, &sc->jrxq);
2703aab5582fSPyun YongHyeon 	else
2704aab5582fSPyun YongHyeon 		error = nfe_init_rx_ring(sc, &sc->rxq);
2705aab5582fSPyun YongHyeon 	if (error != 0) {
2706aab5582fSPyun YongHyeon 		device_printf(sc->nfe_dev,
2707aab5582fSPyun YongHyeon 		    "initialization failed: no memory for rx buffers\n");
2708aab5582fSPyun YongHyeon 		nfe_stop(ifp);
2709bfc788c2SDavid E. O'Brien 		return;
2710bfc788c2SDavid E. O'Brien 	}
2711257c5577SDavid E. O'Brien 
2712aab5582fSPyun YongHyeon 	val = 0;
2713aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_CORRECT_MACADDR) != 0)
2714aab5582fSPyun YongHyeon 		val |= NFE_MAC_ADDR_INORDER;
2715aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_TX_UNK, val);
2716257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_STATUS, 0);
2717257c5577SDavid E. O'Brien 
2718aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_TX_FLOW_CTRL) != 0)
2719aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_TX_PAUSE_FRAME, NFE_TX_PAUSE_FRAME_DISABLE);
2720aab5582fSPyun YongHyeon 
2721257c5577SDavid E. O'Brien 	sc->rxtxctl = NFE_RXTX_BIT2;
2722bfc788c2SDavid E. O'Brien 	if (sc->nfe_flags & NFE_40BIT_ADDR)
2723257c5577SDavid E. O'Brien 		sc->rxtxctl |= NFE_RXTX_V3MAGIC;
2724bfc788c2SDavid E. O'Brien 	else if (sc->nfe_flags & NFE_JUMBO_SUP)
2725257c5577SDavid E. O'Brien 		sc->rxtxctl |= NFE_RXTX_V2MAGIC;
27267597761aSDavid E. O'Brien 
2727c2df7334SMarcel Moolenaar 	if ((if_getcapenable(ifp) & IFCAP_RXCSUM) != 0)
2728257c5577SDavid E. O'Brien 		sc->rxtxctl |= NFE_RXTX_RXCSUM;
2729c2df7334SMarcel Moolenaar 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
2730aab5582fSPyun YongHyeon 		sc->rxtxctl |= NFE_RXTX_VTAG_INSERT | NFE_RXTX_VTAG_STRIP;
2731bfc788c2SDavid E. O'Brien 
2732257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_RESET | sc->rxtxctl);
2733257c5577SDavid E. O'Brien 	DELAY(10);
2734257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl);
2735257c5577SDavid E. O'Brien 
2736c2df7334SMarcel Moolenaar 	if ((if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING) != 0)
2737257c5577SDavid E. O'Brien 		NFE_WRITE(sc, NFE_VTAG_CTL, NFE_VTAG_ENABLE);
2738aab5582fSPyun YongHyeon 	else
2739aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_VTAG_CTL, 0);
2740257c5577SDavid E. O'Brien 
2741257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_SETUP_R6, 0);
2742257c5577SDavid E. O'Brien 
2743257c5577SDavid E. O'Brien 	/* set MAC address */
2744c2df7334SMarcel Moolenaar 	nfe_set_macaddr(sc, if_getlladdr(ifp));
2745257c5577SDavid E. O'Brien 
2746257c5577SDavid E. O'Brien 	/* tell MAC where rings are in memory */
2747aab5582fSPyun YongHyeon 	if (sc->nfe_framesize > MCLBYTES - ETHER_HDR_LEN) {
2748aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_RX_RING_ADDR_HI,
2749aab5582fSPyun YongHyeon 		    NFE_ADDR_HI(sc->jrxq.jphysaddr));
2750aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_RX_RING_ADDR_LO,
2751aab5582fSPyun YongHyeon 		    NFE_ADDR_LO(sc->jrxq.jphysaddr));
2752aab5582fSPyun YongHyeon 	} else {
2753aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_RX_RING_ADDR_HI,
2754aab5582fSPyun YongHyeon 		    NFE_ADDR_HI(sc->rxq.physaddr));
2755aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_RX_RING_ADDR_LO,
2756aab5582fSPyun YongHyeon 		    NFE_ADDR_LO(sc->rxq.physaddr));
2757aab5582fSPyun YongHyeon 	}
2758aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_TX_RING_ADDR_HI, NFE_ADDR_HI(sc->txq.physaddr));
2759aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_TX_RING_ADDR_LO, NFE_ADDR_LO(sc->txq.physaddr));
2760257c5577SDavid E. O'Brien 
2761257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RING_SIZE,
2762257c5577SDavid E. O'Brien 	    (NFE_RX_RING_COUNT - 1) << 16 |
2763257c5577SDavid E. O'Brien 	    (NFE_TX_RING_COUNT - 1));
2764257c5577SDavid E. O'Brien 
2765aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_RXBUFSZ, sc->nfe_framesize);
2766257c5577SDavid E. O'Brien 
2767257c5577SDavid E. O'Brien 	/* force MAC to wakeup */
2768aab5582fSPyun YongHyeon 	val = NFE_READ(sc, NFE_PWR_STATE);
2769aab5582fSPyun YongHyeon 	if ((val & NFE_PWR_WAKEUP) == 0)
2770aab5582fSPyun YongHyeon 		NFE_WRITE(sc, NFE_PWR_STATE, val | NFE_PWR_WAKEUP);
2771257c5577SDavid E. O'Brien 	DELAY(10);
2772aab5582fSPyun YongHyeon 	val = NFE_READ(sc, NFE_PWR_STATE);
2773aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_PWR_STATE, val | NFE_PWR_VALID);
2774257c5577SDavid E. O'Brien 
2775257c5577SDavid E. O'Brien #if 1
2776257c5577SDavid E. O'Brien 	/* configure interrupts coalescing/mitigation */
2777257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_IMTIMER, NFE_IM_DEFAULT);
2778257c5577SDavid E. O'Brien #else
2779257c5577SDavid E. O'Brien 	/* no interrupt mitigation: one interrupt per packet */
2780257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_IMTIMER, 970);
2781257c5577SDavid E. O'Brien #endif
2782257c5577SDavid E. O'Brien 
2783aab5582fSPyun YongHyeon 	NFE_WRITE(sc, NFE_SETUP_R1, NFE_R1_MAGIC_10_100);
2784257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_SETUP_R2, NFE_R2_MAGIC);
2785257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_SETUP_R6, NFE_R6_MAGIC);
2786257c5577SDavid E. O'Brien 
2787257c5577SDavid E. O'Brien 	/* update MAC knowledge of PHY; generates a NFE_IRQ_LINK interrupt */
2788257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_STATUS, sc->mii_phyaddr << 24 | NFE_STATUS_MAGIC);
2789257c5577SDavid E. O'Brien 
2790257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_SETUP_R4, NFE_R4_MAGIC);
279152a1393eSPyun YongHyeon 	/* Disable WOL. */
279252a1393eSPyun YongHyeon 	NFE_WRITE(sc, NFE_WOL_CTL, 0);
2793257c5577SDavid E. O'Brien 
2794257c5577SDavid E. O'Brien 	sc->rxtxctl &= ~NFE_RXTX_BIT2;
2795257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RXTX_CTL, sc->rxtxctl);
2796257c5577SDavid E. O'Brien 	DELAY(10);
2797257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RXTX_CTL, NFE_RXTX_BIT1 | sc->rxtxctl);
2798257c5577SDavid E. O'Brien 
2799257c5577SDavid E. O'Brien 	/* set Rx filter */
2800257c5577SDavid E. O'Brien 	nfe_setmulti(sc);
2801257c5577SDavid E. O'Brien 
2802257c5577SDavid E. O'Brien 	/* enable Rx */
2803257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RX_CTL, NFE_RX_START);
2804257c5577SDavid E. O'Brien 
2805257c5577SDavid E. O'Brien 	/* enable Tx */
2806257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_TX_CTL, NFE_TX_START);
2807257c5577SDavid E. O'Brien 
2808257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_PHY_STATUS, 0xf);
2809257c5577SDavid E. O'Brien 
281017d022beSPyun YongHyeon 	/* Clear hardware stats. */
281117d022beSPyun YongHyeon 	nfe_stats_clear(sc);
281217d022beSPyun YongHyeon 
2813bfc788c2SDavid E. O'Brien #ifdef DEVICE_POLLING
2814c2df7334SMarcel Moolenaar 	if (if_getcapenable(ifp) & IFCAP_POLLING)
2815aab5582fSPyun YongHyeon 		nfe_disable_intr(sc);
2816bfc788c2SDavid E. O'Brien 	else
2817bfc788c2SDavid E. O'Brien #endif
2818aab5582fSPyun YongHyeon 	nfe_set_intr(sc);
2819aab5582fSPyun YongHyeon 	nfe_enable_intr(sc); /* enable interrupts */
2820257c5577SDavid E. O'Brien 
2821c2df7334SMarcel Moolenaar 	if_setdrvflagbits(ifp, IFF_DRV_RUNNING, 0);
2822c2df7334SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, IFF_DRV_OACTIVE);
2823257c5577SDavid E. O'Brien 
2824bfc788c2SDavid E. O'Brien 	sc->nfe_link = 0;
2825aab5582fSPyun YongHyeon 	mii_mediachg(mii);
2826257c5577SDavid E. O'Brien 
2827aab5582fSPyun YongHyeon 	callout_reset(&sc->nfe_stat_ch, hz, nfe_tick, sc);
2828257c5577SDavid E. O'Brien }
2829257c5577SDavid E. O'Brien 
28302c3adf61SDavid E. O'Brien static void
nfe_stop(if_t ifp)2831c2df7334SMarcel Moolenaar nfe_stop(if_t ifp)
2832257c5577SDavid E. O'Brien {
2833c2df7334SMarcel Moolenaar 	struct nfe_softc *sc = if_getsoftc(ifp);
2834aab5582fSPyun YongHyeon 	struct nfe_rx_ring *rx_ring;
2835aab5582fSPyun YongHyeon 	struct nfe_jrx_ring *jrx_ring;
2836aab5582fSPyun YongHyeon 	struct nfe_tx_ring *tx_ring;
2837aab5582fSPyun YongHyeon 	struct nfe_rx_data *rdata;
2838aab5582fSPyun YongHyeon 	struct nfe_tx_data *tdata;
2839aab5582fSPyun YongHyeon 	int i;
2840257c5577SDavid E. O'Brien 
2841bfc788c2SDavid E. O'Brien 	NFE_LOCK_ASSERT(sc);
2842257c5577SDavid E. O'Brien 
2843aab5582fSPyun YongHyeon 	sc->nfe_watchdog_timer = 0;
2844c2df7334SMarcel Moolenaar 	if_setdrvflagbits(ifp, 0, (IFF_DRV_RUNNING | IFF_DRV_OACTIVE));
2845257c5577SDavid E. O'Brien 
2846bfc788c2SDavid E. O'Brien 	callout_stop(&sc->nfe_stat_ch);
2847257c5577SDavid E. O'Brien 
2848257c5577SDavid E. O'Brien 	/* abort Tx */
2849257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_TX_CTL, 0);
2850257c5577SDavid E. O'Brien 
2851257c5577SDavid E. O'Brien 	/* disable Rx */
2852257c5577SDavid E. O'Brien 	NFE_WRITE(sc, NFE_RX_CTL, 0);
2853257c5577SDavid E. O'Brien 
2854257c5577SDavid E. O'Brien 	/* disable interrupts */
2855aab5582fSPyun YongHyeon 	nfe_disable_intr(sc);
2856257c5577SDavid E. O'Brien 
2857bfc788c2SDavid E. O'Brien 	sc->nfe_link = 0;
2858bfc788c2SDavid E. O'Brien 
2859aab5582fSPyun YongHyeon 	/* free Rx and Tx mbufs still in the queues. */
2860aab5582fSPyun YongHyeon 	rx_ring = &sc->rxq;
2861aab5582fSPyun YongHyeon 	for (i = 0; i < NFE_RX_RING_COUNT; i++) {
2862aab5582fSPyun YongHyeon 		rdata = &rx_ring->data[i];
2863aab5582fSPyun YongHyeon 		if (rdata->m != NULL) {
2864aab5582fSPyun YongHyeon 			bus_dmamap_sync(rx_ring->rx_data_tag,
2865aab5582fSPyun YongHyeon 			    rdata->rx_data_map, BUS_DMASYNC_POSTREAD);
2866aab5582fSPyun YongHyeon 			bus_dmamap_unload(rx_ring->rx_data_tag,
2867aab5582fSPyun YongHyeon 			    rdata->rx_data_map);
2868aab5582fSPyun YongHyeon 			m_freem(rdata->m);
2869aab5582fSPyun YongHyeon 			rdata->m = NULL;
2870aab5582fSPyun YongHyeon 		}
2871aab5582fSPyun YongHyeon 	}
2872257c5577SDavid E. O'Brien 
2873aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_JUMBO_SUP) != 0) {
2874aab5582fSPyun YongHyeon 		jrx_ring = &sc->jrxq;
2875aab5582fSPyun YongHyeon 		for (i = 0; i < NFE_JUMBO_RX_RING_COUNT; i++) {
2876aab5582fSPyun YongHyeon 			rdata = &jrx_ring->jdata[i];
2877aab5582fSPyun YongHyeon 			if (rdata->m != NULL) {
2878aab5582fSPyun YongHyeon 				bus_dmamap_sync(jrx_ring->jrx_data_tag,
2879aab5582fSPyun YongHyeon 				    rdata->rx_data_map, BUS_DMASYNC_POSTREAD);
2880aab5582fSPyun YongHyeon 				bus_dmamap_unload(jrx_ring->jrx_data_tag,
2881aab5582fSPyun YongHyeon 				    rdata->rx_data_map);
2882aab5582fSPyun YongHyeon 				m_freem(rdata->m);
2883aab5582fSPyun YongHyeon 				rdata->m = NULL;
2884aab5582fSPyun YongHyeon 			}
2885aab5582fSPyun YongHyeon 		}
2886aab5582fSPyun YongHyeon 	}
2887aab5582fSPyun YongHyeon 
2888aab5582fSPyun YongHyeon 	tx_ring = &sc->txq;
2889aab5582fSPyun YongHyeon 	for (i = 0; i < NFE_RX_RING_COUNT; i++) {
2890aab5582fSPyun YongHyeon 		tdata = &tx_ring->data[i];
2891aab5582fSPyun YongHyeon 		if (tdata->m != NULL) {
2892aab5582fSPyun YongHyeon 			bus_dmamap_sync(tx_ring->tx_data_tag,
2893aab5582fSPyun YongHyeon 			    tdata->tx_data_map, BUS_DMASYNC_POSTWRITE);
2894aab5582fSPyun YongHyeon 			bus_dmamap_unload(tx_ring->tx_data_tag,
2895aab5582fSPyun YongHyeon 			    tdata->tx_data_map);
2896aab5582fSPyun YongHyeon 			m_freem(tdata->m);
2897aab5582fSPyun YongHyeon 			tdata->m = NULL;
2898aab5582fSPyun YongHyeon 		}
2899aab5582fSPyun YongHyeon 	}
290017d022beSPyun YongHyeon 	/* Update hardware stats. */
290117d022beSPyun YongHyeon 	nfe_stats_update(sc);
2902257c5577SDavid E. O'Brien }
2903257c5577SDavid E. O'Brien 
29042c3adf61SDavid E. O'Brien static int
nfe_ifmedia_upd(if_t ifp)2905c2df7334SMarcel Moolenaar nfe_ifmedia_upd(if_t ifp)
2906257c5577SDavid E. O'Brien {
2907c2df7334SMarcel Moolenaar 	struct nfe_softc *sc = if_getsoftc(ifp);
2908bfc788c2SDavid E. O'Brien 	struct mii_data *mii;
2909bfc788c2SDavid E. O'Brien 
2910aab5582fSPyun YongHyeon 	NFE_LOCK(sc);
2911bfc788c2SDavid E. O'Brien 	mii = device_get_softc(sc->nfe_miibus);
2912bfc788c2SDavid E. O'Brien 	mii_mediachg(mii);
2913aab5582fSPyun YongHyeon 	NFE_UNLOCK(sc);
2914bfc788c2SDavid E. O'Brien 
2915bfc788c2SDavid E. O'Brien 	return (0);
2916257c5577SDavid E. O'Brien }
2917257c5577SDavid E. O'Brien 
29182c3adf61SDavid E. O'Brien static void
nfe_ifmedia_sts(if_t ifp,struct ifmediareq * ifmr)2919c2df7334SMarcel Moolenaar nfe_ifmedia_sts(if_t ifp, struct ifmediareq *ifmr)
2920257c5577SDavid E. O'Brien {
2921bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc;
2922bfc788c2SDavid E. O'Brien 	struct mii_data *mii;
2923257c5577SDavid E. O'Brien 
2924c2df7334SMarcel Moolenaar 	sc = if_getsoftc(ifp);
2925bfc788c2SDavid E. O'Brien 
2926bfc788c2SDavid E. O'Brien 	NFE_LOCK(sc);
2927bfc788c2SDavid E. O'Brien 	mii = device_get_softc(sc->nfe_miibus);
2928257c5577SDavid E. O'Brien 	mii_pollstat(mii);
2929bfc788c2SDavid E. O'Brien 
2930257c5577SDavid E. O'Brien 	ifmr->ifm_active = mii->mii_media_active;
2931bfc788c2SDavid E. O'Brien 	ifmr->ifm_status = mii->mii_media_status;
293257c81d92SPyun YongHyeon 	NFE_UNLOCK(sc);
2933257c5577SDavid E. O'Brien }
2934257c5577SDavid E. O'Brien 
29352c3adf61SDavid E. O'Brien void
nfe_tick(void * xsc)2936aab5582fSPyun YongHyeon nfe_tick(void *xsc)
2937bfc788c2SDavid E. O'Brien {
2938bfc788c2SDavid E. O'Brien 	struct nfe_softc *sc;
2939bfc788c2SDavid E. O'Brien 	struct mii_data *mii;
2940c2df7334SMarcel Moolenaar 	if_t ifp;
2941bfc788c2SDavid E. O'Brien 
2942aab5582fSPyun YongHyeon 	sc = (struct nfe_softc *)xsc;
2943bfc788c2SDavid E. O'Brien 
2944bfc788c2SDavid E. O'Brien 	NFE_LOCK_ASSERT(sc);
2945bfc788c2SDavid E. O'Brien 
2946bfc788c2SDavid E. O'Brien 	ifp = sc->nfe_ifp;
2947bfc788c2SDavid E. O'Brien 
2948bfc788c2SDavid E. O'Brien 	mii = device_get_softc(sc->nfe_miibus);
2949bfc788c2SDavid E. O'Brien 	mii_tick(mii);
295017d022beSPyun YongHyeon 	nfe_stats_update(sc);
2951aab5582fSPyun YongHyeon 	nfe_watchdog(ifp);
2952bfc788c2SDavid E. O'Brien 	callout_reset(&sc->nfe_stat_ch, hz, nfe_tick, sc);
2953257c5577SDavid E. O'Brien }
2954257c5577SDavid E. O'Brien 
29556a087a87SPyun YongHyeon static int
nfe_shutdown(device_t dev)29562c3adf61SDavid E. O'Brien nfe_shutdown(device_t dev)
2957bfc788c2SDavid E. O'Brien {
2958bfc788c2SDavid E. O'Brien 
295952a1393eSPyun YongHyeon 	return (nfe_suspend(dev));
2960bfc788c2SDavid E. O'Brien }
2961bfc788c2SDavid E. O'Brien 
29622c3adf61SDavid E. O'Brien static void
nfe_get_macaddr(struct nfe_softc * sc,uint8_t * addr)2963aab5582fSPyun YongHyeon nfe_get_macaddr(struct nfe_softc *sc, uint8_t *addr)
2964257c5577SDavid E. O'Brien {
2965aab5582fSPyun YongHyeon 	uint32_t val;
2966257c5577SDavid E. O'Brien 
2967aab5582fSPyun YongHyeon 	if ((sc->nfe_flags & NFE_CORRECT_MACADDR) == 0) {
2968aab5582fSPyun YongHyeon 		val = NFE_READ(sc, NFE_MACADDR_LO);
2969aab5582fSPyun YongHyeon 		addr[0] = (val >> 8) & 0xff;
2970aab5582fSPyun YongHyeon 		addr[1] = (val & 0xff);
2971257c5577SDavid E. O'Brien 
2972aab5582fSPyun YongHyeon 		val = NFE_READ(sc, NFE_MACADDR_HI);
2973aab5582fSPyun YongHyeon 		addr[2] = (val >> 24) & 0xff;
2974aab5582fSPyun YongHyeon 		addr[3] = (val >> 16) & 0xff;
2975aab5582fSPyun YongHyeon 		addr[4] = (val >>  8) & 0xff;
2976aab5582fSPyun YongHyeon 		addr[5] = (val & 0xff);
2977aab5582fSPyun YongHyeon 	} else {
2978aab5582fSPyun YongHyeon 		val = NFE_READ(sc, NFE_MACADDR_LO);
2979aab5582fSPyun YongHyeon 		addr[5] = (val >> 8) & 0xff;
2980aab5582fSPyun YongHyeon 		addr[4] = (val & 0xff);
2981aab5582fSPyun YongHyeon 
2982aab5582fSPyun YongHyeon 		val = NFE_READ(sc, NFE_MACADDR_HI);
2983aab5582fSPyun YongHyeon 		addr[3] = (val >> 24) & 0xff;
2984aab5582fSPyun YongHyeon 		addr[2] = (val >> 16) & 0xff;
2985aab5582fSPyun YongHyeon 		addr[1] = (val >>  8) & 0xff;
2986aab5582fSPyun YongHyeon 		addr[0] = (val & 0xff);
2987aab5582fSPyun YongHyeon 	}
2988257c5577SDavid E. O'Brien }
2989257c5577SDavid E. O'Brien 
29902c3adf61SDavid E. O'Brien static void
nfe_set_macaddr(struct nfe_softc * sc,uint8_t * addr)2991aab5582fSPyun YongHyeon nfe_set_macaddr(struct nfe_softc *sc, uint8_t *addr)
2992257c5577SDavid E. O'Brien {
2993bfc788c2SDavid E. O'Brien 
2994bfc788c2SDavid E. O'Brien 	NFE_WRITE(sc, NFE_MACADDR_LO, addr[5] <<  8 | addr[4]);
2995bfc788c2SDavid E. O'Brien 	NFE_WRITE(sc, NFE_MACADDR_HI, addr[3] << 24 | addr[2] << 16 |
2996bfc788c2SDavid E. O'Brien 	    addr[1] << 8 | addr[0]);
2997257c5577SDavid E. O'Brien }
2998257c5577SDavid E. O'Brien 
2999bfc788c2SDavid E. O'Brien /*
3000bfc788c2SDavid E. O'Brien  * Map a single buffer address.
3001bfc788c2SDavid E. O'Brien  */
3002bfc788c2SDavid E. O'Brien 
3003bfc788c2SDavid E. O'Brien static void
nfe_dma_map_segs(void * arg,bus_dma_segment_t * segs,int nseg,int error)3004aab5582fSPyun YongHyeon nfe_dma_map_segs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
3005257c5577SDavid E. O'Brien {
3006aab5582fSPyun YongHyeon 	struct nfe_dmamap_arg *ctx;
3007257c5577SDavid E. O'Brien 
3008aab5582fSPyun YongHyeon 	if (error != 0)
3009bfc788c2SDavid E. O'Brien 		return;
3010257c5577SDavid E. O'Brien 
3011bfc788c2SDavid E. O'Brien 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
3012bfc788c2SDavid E. O'Brien 
3013aab5582fSPyun YongHyeon 	ctx = (struct nfe_dmamap_arg *)arg;
3014aab5582fSPyun YongHyeon 	ctx->nfe_busaddr = segs[0].ds_addr;
3015aab5582fSPyun YongHyeon }
3016bfc788c2SDavid E. O'Brien 
3017aab5582fSPyun YongHyeon static int
sysctl_int_range(SYSCTL_HANDLER_ARGS,int low,int high)3018aab5582fSPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
3019aab5582fSPyun YongHyeon {
3020aab5582fSPyun YongHyeon 	int error, value;
3021aab5582fSPyun YongHyeon 
3022aab5582fSPyun YongHyeon 	if (!arg1)
3023aab5582fSPyun YongHyeon 		return (EINVAL);
3024aab5582fSPyun YongHyeon 	value = *(int *)arg1;
3025aab5582fSPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
3026aab5582fSPyun YongHyeon 	if (error || !req->newptr)
3027aab5582fSPyun YongHyeon 		return (error);
3028aab5582fSPyun YongHyeon 	if (value < low || value > high)
3029aab5582fSPyun YongHyeon 		return (EINVAL);
3030aab5582fSPyun YongHyeon 	*(int *)arg1 = value;
3031aab5582fSPyun YongHyeon 
3032aab5582fSPyun YongHyeon 	return (0);
3033aab5582fSPyun YongHyeon }
3034aab5582fSPyun YongHyeon 
3035aab5582fSPyun YongHyeon static int
sysctl_hw_nfe_proc_limit(SYSCTL_HANDLER_ARGS)3036aab5582fSPyun YongHyeon sysctl_hw_nfe_proc_limit(SYSCTL_HANDLER_ARGS)
3037aab5582fSPyun YongHyeon {
3038aab5582fSPyun YongHyeon 
3039aab5582fSPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, NFE_PROC_MIN,
3040aab5582fSPyun YongHyeon 	    NFE_PROC_MAX));
3041257c5577SDavid E. O'Brien }
304217d022beSPyun YongHyeon 
304317d022beSPyun YongHyeon #define	NFE_SYSCTL_STAT_ADD32(c, h, n, p, d)	\
304417d022beSPyun YongHyeon 	    SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d)
304517d022beSPyun YongHyeon #define	NFE_SYSCTL_STAT_ADD64(c, h, n, p, d)	\
30466dc7dc9aSMatthew D Fleming 	    SYSCTL_ADD_UQUAD(c, h, OID_AUTO, n, CTLFLAG_RD, p, d)
304717d022beSPyun YongHyeon 
304817d022beSPyun YongHyeon static void
nfe_sysctl_node(struct nfe_softc * sc)304917d022beSPyun YongHyeon nfe_sysctl_node(struct nfe_softc *sc)
305017d022beSPyun YongHyeon {
305117d022beSPyun YongHyeon 	struct sysctl_ctx_list *ctx;
305217d022beSPyun YongHyeon 	struct sysctl_oid_list *child, *parent;
305317d022beSPyun YongHyeon 	struct sysctl_oid *tree;
305417d022beSPyun YongHyeon 	struct nfe_hw_stats *stats;
305517d022beSPyun YongHyeon 	int error;
305617d022beSPyun YongHyeon 
305717d022beSPyun YongHyeon 	stats = &sc->nfe_stats;
305817d022beSPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->nfe_dev);
305917d022beSPyun YongHyeon 	child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->nfe_dev));
30607029da5cSPawel Biernacki 	SYSCTL_ADD_PROC(ctx, child, OID_AUTO, "process_limit",
30617029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
306217d022beSPyun YongHyeon 	    &sc->nfe_process_limit, 0, sysctl_hw_nfe_proc_limit, "I",
306317d022beSPyun YongHyeon 	    "max number of Rx events to process");
306417d022beSPyun YongHyeon 
306517d022beSPyun YongHyeon 	sc->nfe_process_limit = NFE_PROC_DEFAULT;
306617d022beSPyun YongHyeon 	error = resource_int_value(device_get_name(sc->nfe_dev),
306717d022beSPyun YongHyeon 	    device_get_unit(sc->nfe_dev), "process_limit",
306817d022beSPyun YongHyeon 	    &sc->nfe_process_limit);
306917d022beSPyun YongHyeon 	if (error == 0) {
307017d022beSPyun YongHyeon 		if (sc->nfe_process_limit < NFE_PROC_MIN ||
307117d022beSPyun YongHyeon 		    sc->nfe_process_limit > NFE_PROC_MAX) {
307217d022beSPyun YongHyeon 			device_printf(sc->nfe_dev,
307317d022beSPyun YongHyeon 			    "process_limit value out of range; "
307417d022beSPyun YongHyeon 			    "using default: %d\n", NFE_PROC_DEFAULT);
307517d022beSPyun YongHyeon 			sc->nfe_process_limit = NFE_PROC_DEFAULT;
307617d022beSPyun YongHyeon 		}
307717d022beSPyun YongHyeon 	}
307817d022beSPyun YongHyeon 
307917d022beSPyun YongHyeon 	if ((sc->nfe_flags & (NFE_MIB_V1 | NFE_MIB_V2 | NFE_MIB_V3)) == 0)
308017d022beSPyun YongHyeon 		return;
308117d022beSPyun YongHyeon 
30827029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats",
30837029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "NFE statistics");
308417d022beSPyun YongHyeon 	parent = SYSCTL_CHILDREN(tree);
308517d022beSPyun YongHyeon 
308617d022beSPyun YongHyeon 	/* Rx statistics. */
30877029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "rx",
30887029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Rx MAC statistics");
308917d022beSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
309017d022beSPyun YongHyeon 
309117d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "frame_errors",
309217d022beSPyun YongHyeon 	    &stats->rx_frame_errors, "Framing Errors");
309317d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "extra_bytes",
309417d022beSPyun YongHyeon 	    &stats->rx_extra_bytes, "Extra Bytes");
309517d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "late_cols",
309617d022beSPyun YongHyeon 	    &stats->rx_late_cols, "Late Collisions");
309717d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "runts",
309817d022beSPyun YongHyeon 	    &stats->rx_runts, "Runts");
309917d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "jumbos",
310017d022beSPyun YongHyeon 	    &stats->rx_jumbos, "Jumbos");
310117d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "fifo_overuns",
310217d022beSPyun YongHyeon 	    &stats->rx_fifo_overuns, "FIFO Overruns");
310317d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "crc_errors",
310417d022beSPyun YongHyeon 	    &stats->rx_crc_errors, "CRC Errors");
310517d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "fae",
310617d022beSPyun YongHyeon 	    &stats->rx_fae, "Frame Alignment Errors");
310717d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "len_errors",
310817d022beSPyun YongHyeon 	    &stats->rx_len_errors, "Length Errors");
310917d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "unicast",
311017d022beSPyun YongHyeon 	    &stats->rx_unicast, "Unicast Frames");
311117d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "multicast",
311217d022beSPyun YongHyeon 	    &stats->rx_multicast, "Multicast Frames");
311369d6a6aaSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "broadcast",
311417d022beSPyun YongHyeon 	    &stats->rx_broadcast, "Broadcast Frames");
311517d022beSPyun YongHyeon 	if ((sc->nfe_flags & NFE_MIB_V2) != 0) {
311617d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD64(ctx, child, "octets",
311717d022beSPyun YongHyeon 		    &stats->rx_octets, "Octets");
311817d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD32(ctx, child, "pause",
311917d022beSPyun YongHyeon 		    &stats->rx_pause, "Pause frames");
312017d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD32(ctx, child, "drops",
312117d022beSPyun YongHyeon 		    &stats->rx_drops, "Drop frames");
312217d022beSPyun YongHyeon 	}
312317d022beSPyun YongHyeon 
312417d022beSPyun YongHyeon 	/* Tx statistics. */
31257029da5cSPawel Biernacki 	tree = SYSCTL_ADD_NODE(ctx, parent, OID_AUTO, "tx",
31267029da5cSPawel Biernacki 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Tx MAC statistics");
312717d022beSPyun YongHyeon 	child = SYSCTL_CHILDREN(tree);
312817d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD64(ctx, child, "octets",
312917d022beSPyun YongHyeon 	    &stats->tx_octets, "Octets");
313017d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "zero_rexmits",
313117d022beSPyun YongHyeon 	    &stats->tx_zero_rexmits, "Zero Retransmits");
313217d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "one_rexmits",
313317d022beSPyun YongHyeon 	    &stats->tx_one_rexmits, "One Retransmits");
313417d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "multi_rexmits",
313517d022beSPyun YongHyeon 	    &stats->tx_multi_rexmits, "Multiple Retransmits");
313617d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "late_cols",
313717d022beSPyun YongHyeon 	    &stats->tx_late_cols, "Late Collisions");
313817d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "fifo_underuns",
313917d022beSPyun YongHyeon 	    &stats->tx_fifo_underuns, "FIFO Underruns");
314017d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "carrier_losts",
314117d022beSPyun YongHyeon 	    &stats->tx_carrier_losts, "Carrier Losts");
314217d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "excess_deferrals",
314317d022beSPyun YongHyeon 	    &stats->tx_excess_deferals, "Excess Deferrals");
314417d022beSPyun YongHyeon 	NFE_SYSCTL_STAT_ADD32(ctx, child, "retry_errors",
314517d022beSPyun YongHyeon 	    &stats->tx_retry_errors, "Retry Errors");
314617d022beSPyun YongHyeon 	if ((sc->nfe_flags & NFE_MIB_V2) != 0) {
314717d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD32(ctx, child, "deferrals",
314817d022beSPyun YongHyeon 		    &stats->tx_deferals, "Deferrals");
314917d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD32(ctx, child, "frames",
315017d022beSPyun YongHyeon 		    &stats->tx_frames, "Frames");
315117d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD32(ctx, child, "pause",
315217d022beSPyun YongHyeon 		    &stats->tx_pause, "Pause Frames");
315317d022beSPyun YongHyeon 	}
315417d022beSPyun YongHyeon 	if ((sc->nfe_flags & NFE_MIB_V3) != 0) {
315517d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD32(ctx, child, "unicast",
315617d022beSPyun YongHyeon 		    &stats->tx_deferals, "Unicast Frames");
315717d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD32(ctx, child, "multicast",
315817d022beSPyun YongHyeon 		    &stats->tx_frames, "Multicast Frames");
315917d022beSPyun YongHyeon 		NFE_SYSCTL_STAT_ADD32(ctx, child, "broadcast",
316017d022beSPyun YongHyeon 		    &stats->tx_pause, "Broadcast Frames");
316117d022beSPyun YongHyeon 	}
316217d022beSPyun YongHyeon }
316317d022beSPyun YongHyeon 
316417d022beSPyun YongHyeon #undef NFE_SYSCTL_STAT_ADD32
316517d022beSPyun YongHyeon #undef NFE_SYSCTL_STAT_ADD64
316617d022beSPyun YongHyeon 
316717d022beSPyun YongHyeon static void
nfe_stats_clear(struct nfe_softc * sc)316817d022beSPyun YongHyeon nfe_stats_clear(struct nfe_softc *sc)
316917d022beSPyun YongHyeon {
317017d022beSPyun YongHyeon 	int i, mib_cnt;
317117d022beSPyun YongHyeon 
317217d022beSPyun YongHyeon 	if ((sc->nfe_flags & NFE_MIB_V1) != 0)
317317d022beSPyun YongHyeon 		mib_cnt = NFE_NUM_MIB_STATV1;
317417d022beSPyun YongHyeon 	else if ((sc->nfe_flags & (NFE_MIB_V2 | NFE_MIB_V3)) != 0)
317517d022beSPyun YongHyeon 		mib_cnt = NFE_NUM_MIB_STATV2;
317617d022beSPyun YongHyeon 	else
317717d022beSPyun YongHyeon 		return;
317817d022beSPyun YongHyeon 
31792067877eSPyun YongHyeon 	for (i = 0; i < mib_cnt; i++)
31802067877eSPyun YongHyeon 		NFE_READ(sc, NFE_TX_OCTET + i * sizeof(uint32_t));
318117d022beSPyun YongHyeon 
318217d022beSPyun YongHyeon 	if ((sc->nfe_flags & NFE_MIB_V3) != 0) {
318317d022beSPyun YongHyeon 		NFE_READ(sc, NFE_TX_UNICAST);
318417d022beSPyun YongHyeon 		NFE_READ(sc, NFE_TX_MULTICAST);
318517d022beSPyun YongHyeon 		NFE_READ(sc, NFE_TX_BROADCAST);
318617d022beSPyun YongHyeon 	}
318717d022beSPyun YongHyeon }
318817d022beSPyun YongHyeon 
318917d022beSPyun YongHyeon static void
nfe_stats_update(struct nfe_softc * sc)319017d022beSPyun YongHyeon nfe_stats_update(struct nfe_softc *sc)
319117d022beSPyun YongHyeon {
319217d022beSPyun YongHyeon 	struct nfe_hw_stats *stats;
319317d022beSPyun YongHyeon 
319417d022beSPyun YongHyeon 	NFE_LOCK_ASSERT(sc);
319517d022beSPyun YongHyeon 
319617d022beSPyun YongHyeon 	if ((sc->nfe_flags & (NFE_MIB_V1 | NFE_MIB_V2 | NFE_MIB_V3)) == 0)
319717d022beSPyun YongHyeon 		return;
319817d022beSPyun YongHyeon 
319917d022beSPyun YongHyeon 	stats = &sc->nfe_stats;
320017d022beSPyun YongHyeon 	stats->tx_octets += NFE_READ(sc, NFE_TX_OCTET);
320117d022beSPyun YongHyeon 	stats->tx_zero_rexmits += NFE_READ(sc, NFE_TX_ZERO_REXMIT);
320217d022beSPyun YongHyeon 	stats->tx_one_rexmits += NFE_READ(sc, NFE_TX_ONE_REXMIT);
320317d022beSPyun YongHyeon 	stats->tx_multi_rexmits += NFE_READ(sc, NFE_TX_MULTI_REXMIT);
320417d022beSPyun YongHyeon 	stats->tx_late_cols += NFE_READ(sc, NFE_TX_LATE_COL);
320517d022beSPyun YongHyeon 	stats->tx_fifo_underuns += NFE_READ(sc, NFE_TX_FIFO_UNDERUN);
320617d022beSPyun YongHyeon 	stats->tx_carrier_losts += NFE_READ(sc, NFE_TX_CARRIER_LOST);
320717d022beSPyun YongHyeon 	stats->tx_excess_deferals += NFE_READ(sc, NFE_TX_EXCESS_DEFERRAL);
320817d022beSPyun YongHyeon 	stats->tx_retry_errors += NFE_READ(sc, NFE_TX_RETRY_ERROR);
320917d022beSPyun YongHyeon 	stats->rx_frame_errors += NFE_READ(sc, NFE_RX_FRAME_ERROR);
321017d022beSPyun YongHyeon 	stats->rx_extra_bytes += NFE_READ(sc, NFE_RX_EXTRA_BYTES);
321117d022beSPyun YongHyeon 	stats->rx_late_cols += NFE_READ(sc, NFE_RX_LATE_COL);
321217d022beSPyun YongHyeon 	stats->rx_runts += NFE_READ(sc, NFE_RX_RUNT);
321317d022beSPyun YongHyeon 	stats->rx_jumbos += NFE_READ(sc, NFE_RX_JUMBO);
321417d022beSPyun YongHyeon 	stats->rx_fifo_overuns += NFE_READ(sc, NFE_RX_FIFO_OVERUN);
321517d022beSPyun YongHyeon 	stats->rx_crc_errors += NFE_READ(sc, NFE_RX_CRC_ERROR);
321617d022beSPyun YongHyeon 	stats->rx_fae += NFE_READ(sc, NFE_RX_FAE);
321717d022beSPyun YongHyeon 	stats->rx_len_errors += NFE_READ(sc, NFE_RX_LEN_ERROR);
321817d022beSPyun YongHyeon 	stats->rx_unicast += NFE_READ(sc, NFE_RX_UNICAST);
321917d022beSPyun YongHyeon 	stats->rx_multicast += NFE_READ(sc, NFE_RX_MULTICAST);
322017d022beSPyun YongHyeon 	stats->rx_broadcast += NFE_READ(sc, NFE_RX_BROADCAST);
322117d022beSPyun YongHyeon 
322217d022beSPyun YongHyeon 	if ((sc->nfe_flags & NFE_MIB_V2) != 0) {
322317d022beSPyun YongHyeon 		stats->tx_deferals += NFE_READ(sc, NFE_TX_DEFERAL);
322417d022beSPyun YongHyeon 		stats->tx_frames += NFE_READ(sc, NFE_TX_FRAME);
322517d022beSPyun YongHyeon 		stats->rx_octets += NFE_READ(sc, NFE_RX_OCTET);
322617d022beSPyun YongHyeon 		stats->tx_pause += NFE_READ(sc, NFE_TX_PAUSE);
322717d022beSPyun YongHyeon 		stats->rx_pause += NFE_READ(sc, NFE_RX_PAUSE);
322817d022beSPyun YongHyeon 		stats->rx_drops += NFE_READ(sc, NFE_RX_DROP);
322917d022beSPyun YongHyeon 	}
323017d022beSPyun YongHyeon 
323117d022beSPyun YongHyeon 	if ((sc->nfe_flags & NFE_MIB_V3) != 0) {
323217d022beSPyun YongHyeon 		stats->tx_unicast += NFE_READ(sc, NFE_TX_UNICAST);
323317d022beSPyun YongHyeon 		stats->tx_multicast += NFE_READ(sc, NFE_TX_MULTICAST);
3234040f9b1eSXin LI 		stats->tx_broadcast += NFE_READ(sc, NFE_TX_BROADCAST);
323517d022beSPyun YongHyeon 	}
323617d022beSPyun YongHyeon }
323752a1393eSPyun YongHyeon 
323852a1393eSPyun YongHyeon static void
nfe_set_linkspeed(struct nfe_softc * sc)323952a1393eSPyun YongHyeon nfe_set_linkspeed(struct nfe_softc *sc)
324052a1393eSPyun YongHyeon {
324152a1393eSPyun YongHyeon 	struct mii_softc *miisc;
324252a1393eSPyun YongHyeon 	struct mii_data *mii;
324352a1393eSPyun YongHyeon 	int aneg, i, phyno;
324452a1393eSPyun YongHyeon 
324552a1393eSPyun YongHyeon 	NFE_LOCK_ASSERT(sc);
324652a1393eSPyun YongHyeon 
324752a1393eSPyun YongHyeon 	mii = device_get_softc(sc->nfe_miibus);
324852a1393eSPyun YongHyeon 	mii_pollstat(mii);
324952a1393eSPyun YongHyeon 	aneg = 0;
325052a1393eSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
325152a1393eSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
325252a1393eSPyun YongHyeon 		switch IFM_SUBTYPE(mii->mii_media_active) {
325352a1393eSPyun YongHyeon 		case IFM_10_T:
325452a1393eSPyun YongHyeon 		case IFM_100_TX:
325552a1393eSPyun YongHyeon 			return;
325652a1393eSPyun YongHyeon 		case IFM_1000_T:
325752a1393eSPyun YongHyeon 			aneg++;
325852a1393eSPyun YongHyeon 			break;
325952a1393eSPyun YongHyeon 		default:
326052a1393eSPyun YongHyeon 			break;
326152a1393eSPyun YongHyeon 		}
326252a1393eSPyun YongHyeon 	}
326352a1393eSPyun YongHyeon 	miisc = LIST_FIRST(&mii->mii_phys);
326452a1393eSPyun YongHyeon 	phyno = miisc->mii_phy;
326552a1393eSPyun YongHyeon 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
32663fcb7a53SMarius Strobl 		PHY_RESET(miisc);
326752a1393eSPyun YongHyeon 	nfe_miibus_writereg(sc->nfe_dev, phyno, MII_100T2CR, 0);
326852a1393eSPyun YongHyeon 	nfe_miibus_writereg(sc->nfe_dev, phyno,
326952a1393eSPyun YongHyeon 	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
327052a1393eSPyun YongHyeon 	nfe_miibus_writereg(sc->nfe_dev, phyno,
327152a1393eSPyun YongHyeon 	    MII_BMCR, BMCR_RESET | BMCR_AUTOEN | BMCR_STARTNEG);
327252a1393eSPyun YongHyeon 	DELAY(1000);
327352a1393eSPyun YongHyeon 	if (aneg != 0) {
327452a1393eSPyun YongHyeon 		/*
327552a1393eSPyun YongHyeon 		 * Poll link state until nfe(4) get a 10/100Mbps link.
327652a1393eSPyun YongHyeon 		 */
327752a1393eSPyun YongHyeon 		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
327852a1393eSPyun YongHyeon 			mii_pollstat(mii);
327952a1393eSPyun YongHyeon 			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
328052a1393eSPyun YongHyeon 			    == (IFM_ACTIVE | IFM_AVALID)) {
328152a1393eSPyun YongHyeon 				switch (IFM_SUBTYPE(mii->mii_media_active)) {
328252a1393eSPyun YongHyeon 				case IFM_10_T:
328352a1393eSPyun YongHyeon 				case IFM_100_TX:
328452a1393eSPyun YongHyeon 					nfe_mac_config(sc, mii);
328552a1393eSPyun YongHyeon 					return;
328652a1393eSPyun YongHyeon 				default:
328752a1393eSPyun YongHyeon 					break;
328852a1393eSPyun YongHyeon 				}
328952a1393eSPyun YongHyeon 			}
329052a1393eSPyun YongHyeon 			NFE_UNLOCK(sc);
329152a1393eSPyun YongHyeon 			pause("nfelnk", hz);
329252a1393eSPyun YongHyeon 			NFE_LOCK(sc);
329352a1393eSPyun YongHyeon 		}
329452a1393eSPyun YongHyeon 		if (i == MII_ANEGTICKS_GIGE)
329552a1393eSPyun YongHyeon 			device_printf(sc->nfe_dev,
329652a1393eSPyun YongHyeon 			    "establishing a link failed, WOL may not work!");
329752a1393eSPyun YongHyeon 	}
329852a1393eSPyun YongHyeon 	/*
329952a1393eSPyun YongHyeon 	 * No link, force MAC to have 100Mbps, full-duplex link.
330052a1393eSPyun YongHyeon 	 * This is the last resort and may/may not work.
330152a1393eSPyun YongHyeon 	 */
330252a1393eSPyun YongHyeon 	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
330352a1393eSPyun YongHyeon 	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
330452a1393eSPyun YongHyeon 	nfe_mac_config(sc, mii);
330552a1393eSPyun YongHyeon }
330652a1393eSPyun YongHyeon 
330752a1393eSPyun YongHyeon static void
nfe_set_wol(struct nfe_softc * sc)330852a1393eSPyun YongHyeon nfe_set_wol(struct nfe_softc *sc)
330952a1393eSPyun YongHyeon {
3310c2df7334SMarcel Moolenaar 	if_t ifp;
331152a1393eSPyun YongHyeon 	uint32_t wolctl;
331252a1393eSPyun YongHyeon 
331352a1393eSPyun YongHyeon 	NFE_LOCK_ASSERT(sc);
331452a1393eSPyun YongHyeon 
3315*ddaf6524SJohn Baldwin 	if (!pci_has_pm(sc->nfe_dev))
331652a1393eSPyun YongHyeon 		return;
331752a1393eSPyun YongHyeon 	ifp = sc->nfe_ifp;
3318c2df7334SMarcel Moolenaar 	if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0)
331952a1393eSPyun YongHyeon 		wolctl = NFE_WOL_MAGIC;
332052a1393eSPyun YongHyeon 	else
332152a1393eSPyun YongHyeon 		wolctl = 0;
332252a1393eSPyun YongHyeon 	NFE_WRITE(sc, NFE_WOL_CTL, wolctl);
3323c2df7334SMarcel Moolenaar 	if ((if_getcapenable(ifp) & IFCAP_WOL_MAGIC) != 0) {
332452a1393eSPyun YongHyeon 		nfe_set_linkspeed(sc);
332552a1393eSPyun YongHyeon 		if ((sc->nfe_flags & NFE_PWR_MGMT) != 0)
332652a1393eSPyun YongHyeon 			NFE_WRITE(sc, NFE_PWR2_CTL,
332752a1393eSPyun YongHyeon 			    NFE_READ(sc, NFE_PWR2_CTL) & ~NFE_PWR2_GATE_CLOCKS);
332852a1393eSPyun YongHyeon 		/* Enable RX. */
332952a1393eSPyun YongHyeon 		NFE_WRITE(sc, NFE_RX_RING_ADDR_HI, 0);
333052a1393eSPyun YongHyeon 		NFE_WRITE(sc, NFE_RX_RING_ADDR_LO, 0);
333152a1393eSPyun YongHyeon 		NFE_WRITE(sc, NFE_RX_CTL, NFE_READ(sc, NFE_RX_CTL) |
333252a1393eSPyun YongHyeon 		    NFE_RX_START);
333352a1393eSPyun YongHyeon 	}
333452a1393eSPyun YongHyeon 	/* Request PME if WOL is requested. */
3335c2df7334SMarcel Moolenaar 	if ((if_getcapenable(ifp) & IFCAP_WOL) != 0)
3336*ddaf6524SJohn Baldwin 		pci_enable_pme(sc->nfe_dev);
333752a1393eSPyun YongHyeon }
3338