xref: /freebsd/sys/dev/re/if_re.c (revision ef278cb4ae57de9821b259b0734f8c4a67fb28d5)
1098ca2bdSWarner Losh /*-
2a94100faSBill Paul  * Copyright (c) 1997, 1998-2003
3a94100faSBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4a94100faSBill Paul  *
5a94100faSBill Paul  * Redistribution and use in source and binary forms, with or without
6a94100faSBill Paul  * modification, are permitted provided that the following conditions
7a94100faSBill Paul  * are met:
8a94100faSBill Paul  * 1. Redistributions of source code must retain the above copyright
9a94100faSBill Paul  *    notice, this list of conditions and the following disclaimer.
10a94100faSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
11a94100faSBill Paul  *    notice, this list of conditions and the following disclaimer in the
12a94100faSBill Paul  *    documentation and/or other materials provided with the distribution.
13a94100faSBill Paul  * 3. All advertising materials mentioning features or use of this software
14a94100faSBill Paul  *    must display the following acknowledgement:
15a94100faSBill Paul  *	This product includes software developed by Bill Paul.
16a94100faSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
17a94100faSBill Paul  *    may be used to endorse or promote products derived from this software
18a94100faSBill Paul  *    without specific prior written permission.
19a94100faSBill Paul  *
20a94100faSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21a94100faSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a94100faSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a94100faSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24a94100faSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25a94100faSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26a94100faSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27a94100faSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28a94100faSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29a94100faSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30a94100faSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
31a94100faSBill Paul  */
32a94100faSBill Paul 
334dc52c32SDavid E. O'Brien #include <sys/cdefs.h>
344dc52c32SDavid E. O'Brien __FBSDID("$FreeBSD$");
354dc52c32SDavid E. O'Brien 
36a94100faSBill Paul /*
37ed510fb0SBill Paul  * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
38a94100faSBill Paul  *
39a94100faSBill Paul  * Written by Bill Paul <wpaul@windriver.com>
40a94100faSBill Paul  * Senior Networking Software Engineer
41a94100faSBill Paul  * Wind River Systems
42a94100faSBill Paul  */
43a94100faSBill Paul 
44a94100faSBill Paul /*
45a94100faSBill Paul  * This driver is designed to support RealTek's next generation of
46a94100faSBill Paul  * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
47ed510fb0SBill Paul  * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
48ed510fb0SBill Paul  * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E.
49a94100faSBill Paul  *
50a94100faSBill Paul  * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
51a94100faSBill Paul  * with the older 8139 family, however it also supports a special
52a94100faSBill Paul  * C+ mode of operation that provides several new performance enhancing
53a94100faSBill Paul  * features. These include:
54a94100faSBill Paul  *
55a94100faSBill Paul  *	o Descriptor based DMA mechanism. Each descriptor represents
56a94100faSBill Paul  *	  a single packet fragment. Data buffers may be aligned on
57a94100faSBill Paul  *	  any byte boundary.
58a94100faSBill Paul  *
59a94100faSBill Paul  *	o 64-bit DMA
60a94100faSBill Paul  *
61a94100faSBill Paul  *	o TCP/IP checksum offload for both RX and TX
62a94100faSBill Paul  *
63a94100faSBill Paul  *	o High and normal priority transmit DMA rings
64a94100faSBill Paul  *
65a94100faSBill Paul  *	o VLAN tag insertion and extraction
66a94100faSBill Paul  *
67a94100faSBill Paul  *	o TCP large send (segmentation offload)
68a94100faSBill Paul  *
69a94100faSBill Paul  * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
70a94100faSBill Paul  * programming API is fairly straightforward. The RX filtering, EEPROM
71a94100faSBill Paul  * access and PHY access is the same as it is on the older 8139 series
72a94100faSBill Paul  * chips.
73a94100faSBill Paul  *
74a94100faSBill Paul  * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
75a94100faSBill Paul  * same programming API and feature set as the 8139C+ with the following
76a94100faSBill Paul  * differences and additions:
77a94100faSBill Paul  *
78a94100faSBill Paul  *	o 1000Mbps mode
79a94100faSBill Paul  *
80a94100faSBill Paul  *	o Jumbo frames
81a94100faSBill Paul  *
82a94100faSBill Paul  *	o GMII and TBI ports/registers for interfacing with copper
83a94100faSBill Paul  *	  or fiber PHYs
84a94100faSBill Paul  *
85a94100faSBill Paul  *	o RX and TX DMA rings can have up to 1024 descriptors
86a94100faSBill Paul  *	  (the 8139C+ allows a maximum of 64)
87a94100faSBill Paul  *
88a94100faSBill Paul  *	o Slight differences in register layout from the 8139C+
89a94100faSBill Paul  *
90a94100faSBill Paul  * The TX start and timer interrupt registers are at different locations
91a94100faSBill Paul  * on the 8169 than they are on the 8139C+. Also, the status word in the
92a94100faSBill Paul  * RX descriptor has a slightly different bit layout. The 8169 does not
93a94100faSBill Paul  * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
94a94100faSBill Paul  * copper gigE PHY.
95a94100faSBill Paul  *
96a94100faSBill Paul  * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
97a94100faSBill Paul  * (the 'S' stands for 'single-chip'). These devices have the same
98a94100faSBill Paul  * programming API as the older 8169, but also have some vendor-specific
99a94100faSBill Paul  * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
100a94100faSBill Paul  * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
101a94100faSBill Paul  *
102a94100faSBill Paul  * This driver takes advantage of the RX and TX checksum offload and
103a94100faSBill Paul  * VLAN tag insertion/extraction features. It also implements TX
104a94100faSBill Paul  * interrupt moderation using the timer interrupt registers, which
105a94100faSBill Paul  * significantly reduces TX interrupt load. There is also support
106a94100faSBill Paul  * for jumbo frames, however the 8169/8169S/8110S can not transmit
10722a11c96SJohn-Mark Gurney  * jumbo frames larger than 7440, so the max MTU possible with this
10822a11c96SJohn-Mark Gurney  * driver is 7422 bytes.
109a94100faSBill Paul  */
110a94100faSBill Paul 
111f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
112f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
113f0796cd2SGleb Smirnoff #endif
114f0796cd2SGleb Smirnoff 
115a94100faSBill Paul #include <sys/param.h>
116a94100faSBill Paul #include <sys/endian.h>
117a94100faSBill Paul #include <sys/systm.h>
118a94100faSBill Paul #include <sys/sockio.h>
119a94100faSBill Paul #include <sys/mbuf.h>
120a94100faSBill Paul #include <sys/malloc.h>
121fe12f24bSPoul-Henning Kamp #include <sys/module.h>
122a94100faSBill Paul #include <sys/kernel.h>
123a94100faSBill Paul #include <sys/socket.h>
124ed510fb0SBill Paul #include <sys/lock.h>
125ed510fb0SBill Paul #include <sys/mutex.h>
1260534aae0SPyun YongHyeon #include <sys/sysctl.h>
127ed510fb0SBill Paul #include <sys/taskqueue.h>
128a94100faSBill Paul 
129a94100faSBill Paul #include <net/if.h>
130a94100faSBill Paul #include <net/if_arp.h>
131a94100faSBill Paul #include <net/ethernet.h>
132a94100faSBill Paul #include <net/if_dl.h>
133a94100faSBill Paul #include <net/if_media.h>
134fc74a9f9SBrooks Davis #include <net/if_types.h>
135a94100faSBill Paul #include <net/if_vlan_var.h>
136a94100faSBill Paul 
137a94100faSBill Paul #include <net/bpf.h>
138a94100faSBill Paul 
139a94100faSBill Paul #include <machine/bus.h>
140a94100faSBill Paul #include <machine/resource.h>
141a94100faSBill Paul #include <sys/bus.h>
142a94100faSBill Paul #include <sys/rman.h>
143a94100faSBill Paul 
144a94100faSBill Paul #include <dev/mii/mii.h>
145a94100faSBill Paul #include <dev/mii/miivar.h>
146a94100faSBill Paul 
147a94100faSBill Paul #include <dev/pci/pcireg.h>
148a94100faSBill Paul #include <dev/pci/pcivar.h>
149a94100faSBill Paul 
150d65abd66SPyun YongHyeon #include <pci/if_rlreg.h>
151d65abd66SPyun YongHyeon 
152a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
153a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
154a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
155a94100faSBill Paul 
156298bfdf3SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
157a94100faSBill Paul #include "miibus_if.h"
158a94100faSBill Paul 
1595774c5ffSPyun YongHyeon /* Tunables. */
160c2d2e19cSPyun YongHyeon static int msi_disable = 0;
1615774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1622c21710bSPyun YongHyeon static int prefer_iomap = 0;
1632c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
1645774c5ffSPyun YongHyeon 
165a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
166a94100faSBill Paul 
167a94100faSBill Paul /*
168a94100faSBill Paul  * Various supported device vendors/types and their names.
169a94100faSBill Paul  */
170a94100faSBill Paul static struct rl_type re_devs[] = {
1719dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
17232aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
1739dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
174a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1759dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
1768281a098SPyun YongHyeon 	    "RealTek 8101E/8102E/8102EL/8103E PCIe 10/100baseTX" },
1779dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
178d0c45156SPyun YongHyeon 	    "RealTek 8168/8111 B/C/CP/D/DP/E PCIe Gigabit Ethernet" },
1799dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
180715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1819dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1822ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1839dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
184ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1859dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
18626390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1879dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
188dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
189a94100faSBill Paul };
190a94100faSBill Paul 
191a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
19281eee0ebSPyun YongHyeon 	{ RL_HWREV_8139, RL_8139,  "", RL_MTU },
19381eee0ebSPyun YongHyeon 	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
19481eee0ebSPyun YongHyeon 	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
19581eee0ebSPyun YongHyeon 	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
19681eee0ebSPyun YongHyeon 	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
19781eee0ebSPyun YongHyeon 	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
19881eee0ebSPyun YongHyeon 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
19981eee0ebSPyun YongHyeon 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
200*ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
20181eee0ebSPyun YongHyeon 	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
20281eee0ebSPyun YongHyeon 	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
20381eee0ebSPyun YongHyeon 	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
20481eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
20581eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
20681eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
20781eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
20881eee0ebSPyun YongHyeon 	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
20981eee0ebSPyun YongHyeon 	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
21081eee0ebSPyun YongHyeon 	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
21181eee0ebSPyun YongHyeon 	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
21281eee0ebSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
21381eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
21481eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
21581eee0ebSPyun YongHyeon 	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
216*ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
217*ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
21881eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
21981eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
22081eee0ebSPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
22181eee0ebSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
22281eee0ebSPyun YongHyeon 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
22381eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
22481eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
22581eee0ebSPyun YongHyeon 	{ 0, 0, NULL, 0 }
226a94100faSBill Paul };
227a94100faSBill Paul 
228a94100faSBill Paul static int re_probe		(device_t);
229a94100faSBill Paul static int re_attach		(device_t);
230a94100faSBill Paul static int re_detach		(device_t);
231a94100faSBill Paul 
232d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
233a94100faSBill Paul 
234a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
235a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
236d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
237d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
238d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
23981eee0ebSPyun YongHyeon static int re_jumbo_newbuf	(struct rl_softc *, int);
240a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
24181eee0ebSPyun YongHyeon static int re_jrx_list_init	(struct rl_softc *);
242a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
24322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
24422a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
24522a11c96SJohn-Mark Gurney 				(struct mbuf *);
24622a11c96SJohn-Mark Gurney #endif
2471abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
248a94100faSBill Paul static void re_txeof		(struct rl_softc *);
24997b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2501abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2511abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
25297b9d4baSJohn-Mark Gurney #endif
253ef544f63SPaolo Pisati static int re_intr		(void *);
254a94100faSBill Paul static void re_tick		(void *);
255ed510fb0SBill Paul static void re_tx_task		(void *, int);
256ed510fb0SBill Paul static void re_int_task		(void *, int);
257a94100faSBill Paul static void re_start		(struct ifnet *);
258a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
259a94100faSBill Paul static void re_init		(void *);
26097b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
261a94100faSBill Paul static void re_stop		(struct rl_softc *);
2621d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
263a94100faSBill Paul static int re_suspend		(device_t);
264a94100faSBill Paul static int re_resume		(device_t);
2656a087a87SPyun YongHyeon static int re_shutdown		(device_t);
266a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
267a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
268a94100faSBill Paul 
269a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
270a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
271ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
272a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
273a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
274a94100faSBill Paul 
275a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
276a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
277a94100faSBill Paul static void re_miibus_statchg	(device_t);
278a94100faSBill Paul 
27981eee0ebSPyun YongHyeon static void re_set_jumbo	(struct rl_softc *, int);
280ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
281a94100faSBill Paul static void re_reset		(struct rl_softc *);
2827467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2837467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
284a94100faSBill Paul 
285ed510fb0SBill Paul #ifdef RE_DIAG
286a94100faSBill Paul static int re_diag		(struct rl_softc *);
287ed510fb0SBill Paul #endif
288a94100faSBill Paul 
2890534aae0SPyun YongHyeon static void re_add_sysctls	(struct rl_softc *);
2900534aae0SPyun YongHyeon static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
2910534aae0SPyun YongHyeon 
292a94100faSBill Paul static device_method_t re_methods[] = {
293a94100faSBill Paul 	/* Device interface */
294a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
295a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
296a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
297a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
298a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
299a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
300a94100faSBill Paul 
301a94100faSBill Paul 	/* bus interface */
302a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
303a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
304a94100faSBill Paul 
305a94100faSBill Paul 	/* MII interface */
306a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
307a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
308a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
309a94100faSBill Paul 
310a94100faSBill Paul 	{ 0, 0 }
311a94100faSBill Paul };
312a94100faSBill Paul 
313a94100faSBill Paul static driver_t re_driver = {
314a94100faSBill Paul 	"re",
315a94100faSBill Paul 	re_methods,
316a94100faSBill Paul 	sizeof(struct rl_softc)
317a94100faSBill Paul };
318a94100faSBill Paul 
319a94100faSBill Paul static devclass_t re_devclass;
320a94100faSBill Paul 
321a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
322a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
323a94100faSBill Paul 
324a94100faSBill Paul #define EE_SET(x)					\
325a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
326a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
327a94100faSBill Paul 
328a94100faSBill Paul #define EE_CLR(x)					\
329a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
330a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
331a94100faSBill Paul 
332a94100faSBill Paul /*
333a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
334a94100faSBill Paul  */
335a94100faSBill Paul static void
3367b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
337a94100faSBill Paul {
3380ce0868aSPyun YongHyeon 	int			d, i;
339a94100faSBill Paul 
340ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
341a94100faSBill Paul 
342a94100faSBill Paul 	/*
343a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
344a94100faSBill Paul 	 */
345ed510fb0SBill Paul 
346ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
347a94100faSBill Paul 		if (d & i) {
348a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
349a94100faSBill Paul 		} else {
350a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
351a94100faSBill Paul 		}
352a94100faSBill Paul 		DELAY(100);
353a94100faSBill Paul 		EE_SET(RL_EE_CLK);
354a94100faSBill Paul 		DELAY(150);
355a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
356a94100faSBill Paul 		DELAY(100);
357a94100faSBill Paul 	}
358a94100faSBill Paul }
359a94100faSBill Paul 
360a94100faSBill Paul /*
361a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
362a94100faSBill Paul  */
363a94100faSBill Paul static void
3647b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
365a94100faSBill Paul {
3660ce0868aSPyun YongHyeon 	int			i;
367a94100faSBill Paul 	u_int16_t		word = 0;
368a94100faSBill Paul 
369a94100faSBill Paul 	/*
370a94100faSBill Paul 	 * Send address of word we want to read.
371a94100faSBill Paul 	 */
372a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
373a94100faSBill Paul 
374a94100faSBill Paul 	/*
375a94100faSBill Paul 	 * Start reading bits from EEPROM.
376a94100faSBill Paul 	 */
377a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
378a94100faSBill Paul 		EE_SET(RL_EE_CLK);
379a94100faSBill Paul 		DELAY(100);
380a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
381a94100faSBill Paul 			word |= i;
382a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
383a94100faSBill Paul 		DELAY(100);
384a94100faSBill Paul 	}
385a94100faSBill Paul 
386a94100faSBill Paul 	*dest = word;
387a94100faSBill Paul }
388a94100faSBill Paul 
389a94100faSBill Paul /*
390a94100faSBill Paul  * Read a sequence of words from the EEPROM.
391a94100faSBill Paul  */
392a94100faSBill Paul static void
3937b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
394a94100faSBill Paul {
395a94100faSBill Paul 	int			i;
396a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
397a94100faSBill Paul 
398ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
399ed510fb0SBill Paul 
400ed510fb0SBill Paul         DELAY(100);
401ed510fb0SBill Paul 
402a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
403ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
404a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
405ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
406a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
407be099007SPyun YongHyeon                 *ptr = word;
408a94100faSBill Paul 	}
409ed510fb0SBill Paul 
410ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
411a94100faSBill Paul }
412a94100faSBill Paul 
413a94100faSBill Paul static int
4147b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
415a94100faSBill Paul {
416a94100faSBill Paul 	struct rl_softc		*sc;
417a94100faSBill Paul 	u_int32_t		rval;
418a94100faSBill Paul 	int			i;
419a94100faSBill Paul 
420a94100faSBill Paul 	sc = device_get_softc(dev);
421a94100faSBill Paul 
4229bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4239bac70b8SBill Paul 
4249bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4259bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4269bac70b8SBill Paul 		return (rval);
4279bac70b8SBill Paul 	}
4289bac70b8SBill Paul 
429a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
430a94100faSBill Paul 
43196b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
432a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
433a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
434a94100faSBill Paul 			break;
4352bc085c6SPyun YongHyeon 		DELAY(25);
436a94100faSBill Paul 	}
437a94100faSBill Paul 
43896b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4396b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
440a94100faSBill Paul 		return (0);
441a94100faSBill Paul 	}
442a94100faSBill Paul 
4432bc085c6SPyun YongHyeon 	/*
4442bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4452bc085c6SPyun YongHyeon 	 */
4462bc085c6SPyun YongHyeon 	DELAY(20);
4472bc085c6SPyun YongHyeon 
448a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
449a94100faSBill Paul }
450a94100faSBill Paul 
451a94100faSBill Paul static int
4527b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
453a94100faSBill Paul {
454a94100faSBill Paul 	struct rl_softc		*sc;
455a94100faSBill Paul 	u_int32_t		rval;
456a94100faSBill Paul 	int			i;
457a94100faSBill Paul 
458a94100faSBill Paul 	sc = device_get_softc(dev);
459a94100faSBill Paul 
460a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4619bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
462a94100faSBill Paul 
46396b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
464a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
465a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
466a94100faSBill Paul 			break;
4672bc085c6SPyun YongHyeon 		DELAY(25);
468a94100faSBill Paul 	}
469a94100faSBill Paul 
47096b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4716b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
472a94100faSBill Paul 		return (0);
473a94100faSBill Paul 	}
474a94100faSBill Paul 
4752bc085c6SPyun YongHyeon 	/*
4762bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4772bc085c6SPyun YongHyeon 	 */
4782bc085c6SPyun YongHyeon 	DELAY(20);
4792bc085c6SPyun YongHyeon 
480a94100faSBill Paul 	return (0);
481a94100faSBill Paul }
482a94100faSBill Paul 
483a94100faSBill Paul static int
4847b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
485a94100faSBill Paul {
486a94100faSBill Paul 	struct rl_softc		*sc;
487a94100faSBill Paul 	u_int16_t		rval = 0;
488a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
489a94100faSBill Paul 
490a94100faSBill Paul 	sc = device_get_softc(dev);
491a94100faSBill Paul 
492a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
493a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
494a94100faSBill Paul 		return (rval);
495a94100faSBill Paul 	}
496a94100faSBill Paul 
497a94100faSBill Paul 	switch (reg) {
498a94100faSBill Paul 	case MII_BMCR:
499a94100faSBill Paul 		re8139_reg = RL_BMCR;
500a94100faSBill Paul 		break;
501a94100faSBill Paul 	case MII_BMSR:
502a94100faSBill Paul 		re8139_reg = RL_BMSR;
503a94100faSBill Paul 		break;
504a94100faSBill Paul 	case MII_ANAR:
505a94100faSBill Paul 		re8139_reg = RL_ANAR;
506a94100faSBill Paul 		break;
507a94100faSBill Paul 	case MII_ANER:
508a94100faSBill Paul 		re8139_reg = RL_ANER;
509a94100faSBill Paul 		break;
510a94100faSBill Paul 	case MII_ANLPAR:
511a94100faSBill Paul 		re8139_reg = RL_LPAR;
512a94100faSBill Paul 		break;
513a94100faSBill Paul 	case MII_PHYIDR1:
514a94100faSBill Paul 	case MII_PHYIDR2:
515a94100faSBill Paul 		return (0);
516a94100faSBill Paul 	/*
517a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
518a94100faSBill Paul 	 * register. If we have a link partner which does not
519a94100faSBill Paul 	 * support NWAY, this is the register which will tell
520a94100faSBill Paul 	 * us the results of parallel detection.
521a94100faSBill Paul 	 */
522a94100faSBill Paul 	case RL_MEDIASTAT:
523a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
524a94100faSBill Paul 		return (rval);
525a94100faSBill Paul 	default:
5266b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
527a94100faSBill Paul 		return (0);
528a94100faSBill Paul 	}
529a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
530baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
531baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
532baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
533baa12772SPyun YongHyeon 	}
534a94100faSBill Paul 	return (rval);
535a94100faSBill Paul }
536a94100faSBill Paul 
537a94100faSBill Paul static int
5387b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
539a94100faSBill Paul {
540a94100faSBill Paul 	struct rl_softc		*sc;
541a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
542a94100faSBill Paul 	int			rval = 0;
543a94100faSBill Paul 
544a94100faSBill Paul 	sc = device_get_softc(dev);
545a94100faSBill Paul 
546a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
547a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
548a94100faSBill Paul 		return (rval);
549a94100faSBill Paul 	}
550a94100faSBill Paul 
551a94100faSBill Paul 	switch (reg) {
552a94100faSBill Paul 	case MII_BMCR:
553a94100faSBill Paul 		re8139_reg = RL_BMCR;
554baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
555baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
556baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
557baa12772SPyun YongHyeon 		}
558a94100faSBill Paul 		break;
559a94100faSBill Paul 	case MII_BMSR:
560a94100faSBill Paul 		re8139_reg = RL_BMSR;
561a94100faSBill Paul 		break;
562a94100faSBill Paul 	case MII_ANAR:
563a94100faSBill Paul 		re8139_reg = RL_ANAR;
564a94100faSBill Paul 		break;
565a94100faSBill Paul 	case MII_ANER:
566a94100faSBill Paul 		re8139_reg = RL_ANER;
567a94100faSBill Paul 		break;
568a94100faSBill Paul 	case MII_ANLPAR:
569a94100faSBill Paul 		re8139_reg = RL_LPAR;
570a94100faSBill Paul 		break;
571a94100faSBill Paul 	case MII_PHYIDR1:
572a94100faSBill Paul 	case MII_PHYIDR2:
573a94100faSBill Paul 		return (0);
574a94100faSBill Paul 		break;
575a94100faSBill Paul 	default:
5766b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
577a94100faSBill Paul 		return (0);
578a94100faSBill Paul 	}
579a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
580a94100faSBill Paul 	return (0);
581a94100faSBill Paul }
582a94100faSBill Paul 
583a94100faSBill Paul static void
5847b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
585a94100faSBill Paul {
586130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
587130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
588130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
589a11e2f18SBruce M Simpson 
590130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
591130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
592130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
593130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
594130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
595130b6dfbSPyun YongHyeon 		return;
596130b6dfbSPyun YongHyeon 
597130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
598130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
599130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
600130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
601130b6dfbSPyun YongHyeon 		case IFM_10_T:
602130b6dfbSPyun YongHyeon 		case IFM_100_TX:
603130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
604130b6dfbSPyun YongHyeon 			break;
605130b6dfbSPyun YongHyeon 		case IFM_1000_T:
606130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
607130b6dfbSPyun YongHyeon 				break;
608130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
609130b6dfbSPyun YongHyeon 			break;
610130b6dfbSPyun YongHyeon 		default:
611130b6dfbSPyun YongHyeon 			break;
612130b6dfbSPyun YongHyeon 		}
613130b6dfbSPyun YongHyeon 	}
614130b6dfbSPyun YongHyeon 	/*
615130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
616130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
617130b6dfbSPyun YongHyeon 	 * parameters.
618130b6dfbSPyun YongHyeon 	 */
619a94100faSBill Paul }
620a94100faSBill Paul 
621a94100faSBill Paul /*
622ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
623a94100faSBill Paul  */
624a94100faSBill Paul static void
625ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
626a94100faSBill Paul {
627a94100faSBill Paul 	struct ifnet		*ifp;
628a94100faSBill Paul 	struct ifmultiaddr	*ifma;
629ff191365SJung-uk Kim 	uint32_t		hashes[2] = { 0, 0 };
630ff191365SJung-uk Kim 	uint32_t		h, rxfilt;
631a94100faSBill Paul 
63297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
63397b9d4baSJohn-Mark Gurney 
634fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
635a94100faSBill Paul 
636ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
637a94100faSBill Paul 
638ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6397c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6407c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
641a0637caaSPyun YongHyeon 		/*
642a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
643a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
644a0637caaSPyun YongHyeon 		 * promiscuous mode.
645a0637caaSPyun YongHyeon 		 */
646a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
647ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
648ff191365SJung-uk Kim 		goto done;
649a94100faSBill Paul 	}
650a94100faSBill Paul 
651eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
652a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
653a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
654a94100faSBill Paul 			continue;
6550e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6560e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
657a94100faSBill Paul 		if (h < 32)
658a94100faSBill Paul 			hashes[0] |= (1 << h);
659a94100faSBill Paul 		else
660a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
661a94100faSBill Paul 	}
662eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
663a94100faSBill Paul 
664ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
665bb7dfefbSBill Paul 		/*
666ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
667ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
668ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
669ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
670ff191365SJung-uk Kim 		 * devices.
671bb7dfefbSBill Paul 		 */
672aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
673ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
674ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
675ff191365SJung-uk Kim 			hashes[1] = h;
676ff191365SJung-uk Kim 		}
677ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
678ff191365SJung-uk Kim 	}
679ff191365SJung-uk Kim 
680ff191365SJung-uk Kim done:
681a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
682a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
683ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
684bb7dfefbSBill Paul }
685a94100faSBill Paul 
686a94100faSBill Paul static void
6877b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
688a94100faSBill Paul {
6890ce0868aSPyun YongHyeon 	int			i;
690a94100faSBill Paul 
69197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
69297b9d4baSJohn-Mark Gurney 
693a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
694a94100faSBill Paul 
695a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
696a94100faSBill Paul 		DELAY(10);
697a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
698a94100faSBill Paul 			break;
699a94100faSBill Paul 	}
700a94100faSBill Paul 	if (i == RL_TIMEOUT)
7016b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
702a94100faSBill Paul 
703566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
704a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
70581eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
706566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
707a94100faSBill Paul }
708a94100faSBill Paul 
709ed510fb0SBill Paul #ifdef RE_DIAG
710ed510fb0SBill Paul 
711a94100faSBill Paul /*
712a94100faSBill Paul  * The following routine is designed to test for a defect on some
713a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
714a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
715a94100faSBill Paul  * should be pulled high. The result of this defect is that the
716a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
717a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
718a94100faSBill Paul  * because the 64-bit data lines aren't connected.
719a94100faSBill Paul  *
720a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
721a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
722a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
723a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
724a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
725a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
726a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
727a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
728a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
729a94100faSBill Paul  */
730a94100faSBill Paul 
731a94100faSBill Paul static int
7327b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
733a94100faSBill Paul {
734fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
735a94100faSBill Paul 	struct mbuf		*m0;
736a94100faSBill Paul 	struct ether_header	*eh;
737a94100faSBill Paul 	struct rl_desc		*cur_rx;
738a94100faSBill Paul 	u_int16_t		status;
739a94100faSBill Paul 	u_int32_t		rxstat;
740ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
741a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
742a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
743a94100faSBill Paul 
744a94100faSBill Paul 	/* Allocate a single mbuf */
745a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
746a94100faSBill Paul 	if (m0 == NULL)
747a94100faSBill Paul 		return (ENOBUFS);
748a94100faSBill Paul 
74997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
75097b9d4baSJohn-Mark Gurney 
751a94100faSBill Paul 	/*
752a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
753a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
754a94100faSBill Paul 	 * following special functions:
755a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
756a94100faSBill Paul 	 * - Enables digital loopback mode
757a94100faSBill Paul 	 * - Leaves interrupts turned off
758a94100faSBill Paul 	 */
759a94100faSBill Paul 
760a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
761a94100faSBill Paul 	sc->rl_testmode = 1;
7628476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
76397b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
764351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
765ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
766ed510fb0SBill Paul 		phyaddr = 1;
767ed510fb0SBill Paul 	else
768ed510fb0SBill Paul 		phyaddr = 0;
769ed510fb0SBill Paul 
770ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
771ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
772ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
773ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
774ed510fb0SBill Paul 			break;
775ed510fb0SBill Paul 	}
776ed510fb0SBill Paul 
777ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
778ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
779ed510fb0SBill Paul 
780804af9a1SBill Paul 	DELAY(100000);
781a94100faSBill Paul 
782a94100faSBill Paul 	/* Put some data in the mbuf */
783a94100faSBill Paul 
784a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
785a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
786a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
787a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
788a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
789a94100faSBill Paul 
7907cae6651SBill Paul 	/*
7917cae6651SBill Paul 	 * Queue the packet, start transmission.
7927cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7937cae6651SBill Paul 	 */
794a94100faSBill Paul 
795abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
79697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
79752732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
7987cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
79997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
800a94100faSBill Paul 	m0 = NULL;
801a94100faSBill Paul 
802a94100faSBill Paul 	/* Wait for it to propagate through the chip */
803a94100faSBill Paul 
804abc8ff44SBill Paul 	DELAY(100000);
805a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
806a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
807ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
808abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
809abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
810a94100faSBill Paul 			break;
811a94100faSBill Paul 		DELAY(10);
812a94100faSBill Paul 	}
813a94100faSBill Paul 
814a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8156b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8166b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8176b9f5c94SGleb Smirnoff 		    " loopback mode\n");
818a94100faSBill Paul 		error = EIO;
819a94100faSBill Paul 		goto done;
820a94100faSBill Paul 	}
821a94100faSBill Paul 
822a94100faSBill Paul 	/*
823a94100faSBill Paul 	 * The packet should have been dumped into the first
824a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
825a94100faSBill Paul 	 */
826a94100faSBill Paul 
827a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
828a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
829a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
830d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
831d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
832d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
833d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
834d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
835a94100faSBill Paul 
836d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
837d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
838a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
839a94100faSBill Paul 
840a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
841a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
842a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
843a94100faSBill Paul 
844a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8456b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8466b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
847a94100faSBill Paul 		error = EIO;
848a94100faSBill Paul 		goto done;
849a94100faSBill Paul 	}
850a94100faSBill Paul 
851a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
852a94100faSBill Paul 
853a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
854a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
855a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8566b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8576b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
858a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8596b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
860a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
861a94100faSBill Paul 		    ntohs(eh->ether_type));
8626b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8636b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8646b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8656b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8666b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8676b9f5c94SGleb Smirnoff 		    "details.\n");
868a94100faSBill Paul 		error = EIO;
869a94100faSBill Paul 	}
870a94100faSBill Paul 
871a94100faSBill Paul done:
872a94100faSBill Paul 	/* Turn interface off, release resources */
873a94100faSBill Paul 
874a94100faSBill Paul 	sc->rl_testmode = 0;
875351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
876a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
877a94100faSBill Paul 	re_stop(sc);
878a94100faSBill Paul 	if (m0 != NULL)
879a94100faSBill Paul 		m_freem(m0);
880a94100faSBill Paul 
88197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
88297b9d4baSJohn-Mark Gurney 
883a94100faSBill Paul 	return (error);
884a94100faSBill Paul }
885a94100faSBill Paul 
886ed510fb0SBill Paul #endif
887ed510fb0SBill Paul 
888a94100faSBill Paul /*
889a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
890a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
891a94100faSBill Paul  */
892a94100faSBill Paul static int
8937b5ffebfSPyun YongHyeon re_probe(device_t dev)
894a94100faSBill Paul {
895a94100faSBill Paul 	struct rl_type		*t;
896dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
897dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
898dfdb409eSPyun YongHyeon 	int			i;
899a94100faSBill Paul 
900dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
901dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
902dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
903dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
904a94100faSBill Paul 
905dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
906dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
90726390635SJohn Baldwin 			/*
90826390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
909dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
91026390635SJohn Baldwin 			 */
911a94100faSBill Paul 			return (ENXIO);
912a94100faSBill Paul 		}
913dfdb409eSPyun YongHyeon 	}
914dfdb409eSPyun YongHyeon 
915dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
916dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
917dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
918dfdb409eSPyun YongHyeon 			return (ENXIO);
919dfdb409eSPyun YongHyeon 		}
920dfdb409eSPyun YongHyeon 	}
921dfdb409eSPyun YongHyeon 
922dfdb409eSPyun YongHyeon 	t = re_devs;
923dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
924dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
925a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
926d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
927a94100faSBill Paul 		}
928a94100faSBill Paul 	}
929a94100faSBill Paul 
930a94100faSBill Paul 	return (ENXIO);
931a94100faSBill Paul }
932a94100faSBill Paul 
933a94100faSBill Paul /*
934a94100faSBill Paul  * Map a single buffer address.
935a94100faSBill Paul  */
936a94100faSBill Paul 
937a94100faSBill Paul static void
9387b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
939a94100faSBill Paul {
9408fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
941a94100faSBill Paul 
942a94100faSBill Paul 	if (error)
943a94100faSBill Paul 		return;
944a94100faSBill Paul 
945a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
946a94100faSBill Paul 	addr = arg;
947a94100faSBill Paul 	*addr = segs->ds_addr;
948a94100faSBill Paul }
949a94100faSBill Paul 
950a94100faSBill Paul static int
9517b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
952a94100faSBill Paul {
95366366ca4SPyun YongHyeon 	bus_addr_t		lowaddr;
954d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
955a94100faSBill Paul 	int			error;
956a94100faSBill Paul 	int			i;
957a94100faSBill Paul 
958d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
959d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
960d65abd66SPyun YongHyeon 
961d65abd66SPyun YongHyeon 	/*
962d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
963ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
964ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
965ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
966ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
96766366ca4SPyun YongHyeon 	 * may not have the limitation.
968d65abd66SPyun YongHyeon 	 */
96966366ca4SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
97066366ca4SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
97166366ca4SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
972d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
97366366ca4SPyun YongHyeon 	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
974d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
975d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
976d65abd66SPyun YongHyeon 	if (error) {
977d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
978d65abd66SPyun YongHyeon 		return (error);
979d65abd66SPyun YongHyeon 	}
980d65abd66SPyun YongHyeon 
981d65abd66SPyun YongHyeon 	/*
982d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
983d65abd66SPyun YongHyeon 	 */
984d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
985d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
986d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
987d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
988d65abd66SPyun YongHyeon 	if (error) {
989d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
990d65abd66SPyun YongHyeon 		return (error);
991d65abd66SPyun YongHyeon 	}
992d65abd66SPyun YongHyeon 
993a94100faSBill Paul 	/*
994a94100faSBill Paul 	 * Allocate map for RX mbufs.
995a94100faSBill Paul 	 */
996d65abd66SPyun YongHyeon 
99781eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
99881eee0ebSPyun YongHyeon 		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
99981eee0ebSPyun YongHyeon 		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
100081eee0ebSPyun YongHyeon 		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
100181eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_mtag);
100281eee0ebSPyun YongHyeon 		if (error) {
100381eee0ebSPyun YongHyeon 			device_printf(dev,
100481eee0ebSPyun YongHyeon 			    "could not allocate jumbo RX DMA tag\n");
100581eee0ebSPyun YongHyeon 			return (error);
100681eee0ebSPyun YongHyeon 		}
100781eee0ebSPyun YongHyeon 	}
1008d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1009d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1010d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1011a94100faSBill Paul 	if (error) {
1012d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1013d65abd66SPyun YongHyeon 		return (error);
1014a94100faSBill Paul 	}
1015a94100faSBill Paul 
1016a94100faSBill Paul 	/*
1017a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1018a94100faSBill Paul 	 */
1019a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1020a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1021d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1022a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1023a94100faSBill Paul 	if (error) {
1024d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1025d65abd66SPyun YongHyeon 		return (error);
1026a94100faSBill Paul 	}
1027a94100faSBill Paul 
1028a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1029a94100faSBill Paul 
1030a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1031d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1032d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1033a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1034d65abd66SPyun YongHyeon 	if (error) {
1035d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1036d65abd66SPyun YongHyeon 		return (error);
1037d65abd66SPyun YongHyeon 	}
1038a94100faSBill Paul 
1039a94100faSBill Paul 	/* Load the map for the TX ring. */
1040a94100faSBill Paul 
1041d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1042a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1043a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1044d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1045a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1046d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1047d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1048d65abd66SPyun YongHyeon 		return (ENOMEM);
1049d65abd66SPyun YongHyeon 	}
1050a94100faSBill Paul 
1051a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1052a94100faSBill Paul 
1053d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1054d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1055d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1056a94100faSBill Paul 		if (error) {
1057d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1058d65abd66SPyun YongHyeon 			return (error);
1059a94100faSBill Paul 		}
1060a94100faSBill Paul 	}
1061a94100faSBill Paul 
1062a94100faSBill Paul 	/*
1063a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1064a94100faSBill Paul 	 */
1065a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1066a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1067d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1068a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1069a94100faSBill Paul 	if (error) {
1070d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1071d65abd66SPyun YongHyeon 		return (error);
1072a94100faSBill Paul 	}
1073a94100faSBill Paul 
1074a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1075a94100faSBill Paul 
1076a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1077d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1078d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1079a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1080d65abd66SPyun YongHyeon 	if (error) {
1081d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1082d65abd66SPyun YongHyeon 		return (error);
1083d65abd66SPyun YongHyeon 	}
1084a94100faSBill Paul 
1085a94100faSBill Paul 	/* Load the map for the RX ring. */
1086a94100faSBill Paul 
1087d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1088a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1089a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1090d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1091a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1092d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1093d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1094d65abd66SPyun YongHyeon 		return (ENOMEM);
1095d65abd66SPyun YongHyeon 	}
1096a94100faSBill Paul 
1097a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1098a94100faSBill Paul 
109981eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
110081eee0ebSPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
110181eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_sparemap);
110281eee0ebSPyun YongHyeon 		if (error) {
110381eee0ebSPyun YongHyeon 			device_printf(dev,
110481eee0ebSPyun YongHyeon 			    "could not create spare DMA map for jumbo RX\n");
110581eee0ebSPyun YongHyeon 			return (error);
110681eee0ebSPyun YongHyeon 		}
110781eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
110881eee0ebSPyun YongHyeon 			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
110981eee0ebSPyun YongHyeon 			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
111081eee0ebSPyun YongHyeon 			if (error) {
111181eee0ebSPyun YongHyeon 				device_printf(dev,
111281eee0ebSPyun YongHyeon 				    "could not create DMA map for jumbo RX\n");
111381eee0ebSPyun YongHyeon 				return (error);
111481eee0ebSPyun YongHyeon 			}
111581eee0ebSPyun YongHyeon 		}
111681eee0ebSPyun YongHyeon 	}
1117d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1118d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1119a94100faSBill Paul 	if (error) {
1120d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1121d65abd66SPyun YongHyeon 		return (error);
1122d65abd66SPyun YongHyeon 	}
1123d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1124d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1125d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1126d65abd66SPyun YongHyeon 		if (error) {
1127d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1128d65abd66SPyun YongHyeon 			return (error);
1129a94100faSBill Paul 		}
1130a94100faSBill Paul 	}
1131a94100faSBill Paul 
11320534aae0SPyun YongHyeon 	/* Create DMA map for statistics. */
11330534aae0SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
11340534aae0SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
11350534aae0SPyun YongHyeon 	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
11360534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_stag);
11370534aae0SPyun YongHyeon 	if (error) {
11380534aae0SPyun YongHyeon 		device_printf(dev, "could not create statistics DMA tag\n");
11390534aae0SPyun YongHyeon 		return (error);
11400534aae0SPyun YongHyeon 	}
11410534aae0SPyun YongHyeon 	/* Allocate DMA'able memory for statistics. */
11420534aae0SPyun YongHyeon 	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
11430534aae0SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_stats,
11440534aae0SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
11450534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_smap);
11460534aae0SPyun YongHyeon 	if (error) {
11470534aae0SPyun YongHyeon 		device_printf(dev,
11480534aae0SPyun YongHyeon 		    "could not allocate statistics DMA memory\n");
11490534aae0SPyun YongHyeon 		return (error);
11500534aae0SPyun YongHyeon 	}
11510534aae0SPyun YongHyeon 	/* Load the map for statistics. */
11520534aae0SPyun YongHyeon 	sc->rl_ldata.rl_stats_addr = 0;
11530534aae0SPyun YongHyeon 	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
11540534aae0SPyun YongHyeon 	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
11550534aae0SPyun YongHyeon 	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
11560534aae0SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
11570534aae0SPyun YongHyeon 		device_printf(dev, "could not load statistics DMA memory\n");
11580534aae0SPyun YongHyeon 		return (ENOMEM);
11590534aae0SPyun YongHyeon 	}
11600534aae0SPyun YongHyeon 
1161a94100faSBill Paul 	return (0);
1162a94100faSBill Paul }
1163a94100faSBill Paul 
1164a94100faSBill Paul /*
1165a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1166a94100faSBill Paul  * setup and ethernet/BPF attach.
1167a94100faSBill Paul  */
1168a94100faSBill Paul static int
11697b5ffebfSPyun YongHyeon re_attach(device_t dev)
1170a94100faSBill Paul {
1171a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1172be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1173a94100faSBill Paul 	struct rl_softc		*sc;
1174a94100faSBill Paul 	struct ifnet		*ifp;
1175a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1176a94100faSBill Paul 	int			hwrev;
1177ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
11788e5d93dbSMarius Strobl 	int			error = 0, i, phy, rid;
11795774c5ffSPyun YongHyeon 	int			msic, reg;
118003ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1181a94100faSBill Paul 
1182a94100faSBill Paul 	sc = device_get_softc(dev);
1183ed510fb0SBill Paul 	sc->rl_dev = dev;
1184a94100faSBill Paul 
1185a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
118697b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1187d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1188d1754a9bSJohn Baldwin 
1189a94100faSBill Paul 	/*
1190a94100faSBill Paul 	 * Map control/status registers.
1191a94100faSBill Paul 	 */
1192a94100faSBill Paul 	pci_enable_busmaster(dev);
1193a94100faSBill Paul 
1194ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
11952c21710bSPyun YongHyeon 	/*
11962c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
11972c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
11982c21710bSPyun YongHyeon 	 * is used always activate io mapping.
11992c21710bSPyun YongHyeon 	 */
12002c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
12012c21710bSPyun YongHyeon 		prefer_iomap = 1;
12022c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1203ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1204ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1205ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1206ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1207ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
12082c21710bSPyun YongHyeon 	} else {
12092c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
12102c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
12112c21710bSPyun YongHyeon 	}
1212ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1213ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
12142c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1215ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1216ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1217ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1218ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
12192c21710bSPyun YongHyeon 	}
1220ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1221d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1222a94100faSBill Paul 		error = ENXIO;
1223a94100faSBill Paul 		goto fail;
1224a94100faSBill Paul 	}
1225a94100faSBill Paul 
1226a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1227a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1228a94100faSBill Paul 
12295774c5ffSPyun YongHyeon 	msic = 0;
12305774c5ffSPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0) {
1231818951afSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
12325774c5ffSPyun YongHyeon 		msic = pci_msi_count(dev);
12335774c5ffSPyun YongHyeon 		if (bootverbose)
12345774c5ffSPyun YongHyeon 			device_printf(dev, "MSI count : %d\n", msic);
12355774c5ffSPyun YongHyeon 	}
1236f1bb696aSPyun YongHyeon 	if (msic > 0 && msi_disable == 0) {
1237f1bb696aSPyun YongHyeon 		msic = 1;
12385774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
12395774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
12405774c5ffSPyun YongHyeon 				device_printf(dev, "Using %d MSI messages\n",
12415774c5ffSPyun YongHyeon 				    msic);
1242351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1243339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1244339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1245339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1246339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1247339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1248f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
12495774c5ffSPyun YongHyeon 			} else
12505774c5ffSPyun YongHyeon 				pci_release_msi(dev);
12515774c5ffSPyun YongHyeon 		}
12525774c5ffSPyun YongHyeon 	}
1253a94100faSBill Paul 
12545774c5ffSPyun YongHyeon 	/* Allocate interrupt */
1255351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
12565774c5ffSPyun YongHyeon 		rid = 0;
12575774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
12585774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
12595774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
12605774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1261a94100faSBill Paul 			error = ENXIO;
1262a94100faSBill Paul 			goto fail;
1263a94100faSBill Paul 		}
12645774c5ffSPyun YongHyeon 	} else {
12655774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
12665774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
12675774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
12685774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
12695774c5ffSPyun YongHyeon 				device_printf(dev,
12705774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
12715774c5ffSPyun YongHyeon 				    "message %d\n", rid);
12725774c5ffSPyun YongHyeon 				error = ENXIO;
12735774c5ffSPyun YongHyeon 				goto fail;
12745774c5ffSPyun YongHyeon 			}
12755774c5ffSPyun YongHyeon 		}
12765774c5ffSPyun YongHyeon 	}
1277a94100faSBill Paul 
12784d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
12794d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
12804d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
12814d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
12824d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
12834d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
12844d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
12854d2bf239SPyun YongHyeon 		}
12864d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
12874d2bf239SPyun YongHyeon 	}
12884d2bf239SPyun YongHyeon 
1289abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1290a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1291566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1292566ca8caSJung-uk Kim 	case 0x00000000:
1293566ca8caSJung-uk Kim 	case 0x10000000:
1294566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1295566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1296566ca8caSJung-uk Kim 		break;
1297566ca8caSJung-uk Kim 	default:
1298a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1299a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1300566ca8caSJung-uk Kim 		break;
1301566ca8caSJung-uk Kim 	}
1302566ca8caSJung-uk Kim 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1303abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1304abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1305abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
130681eee0ebSPyun YongHyeon 			sc->rl_hwrev = hw_rev;
1307abc8ff44SBill Paul 			break;
1308abc8ff44SBill Paul 		}
1309abc8ff44SBill Paul 		hw_rev++;
1310abc8ff44SBill Paul 	}
1311d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1312a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1313d65abd66SPyun YongHyeon 		error = ENXIO;
1314d65abd66SPyun YongHyeon 		goto fail;
1315d65abd66SPyun YongHyeon 	}
1316abc8ff44SBill Paul 
1317351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1318351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
131981eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1320351a76f9SPyun YongHyeon 		break;
1321351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1322351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
132381eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1324351a76f9SPyun YongHyeon 		break;
1325b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1326b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
13273d22427cSTai-hwa Liang 	case RL_HWREV_8102EL_SPIN1:
132881eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
132981eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
133081eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1331b1d62f0fSPyun YongHyeon 		break;
13328281a098SPyun YongHyeon 	case RL_HWREV_8103E:
133381eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
133481eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
133581eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
13368281a098SPyun YongHyeon 		break;
1337*ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN1:
1338*ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN2:
1339886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1340886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1341*ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN3:
1342aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1343deb5c680SPyun YongHyeon 		break;
1344deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
134561f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
134661f45a72SPyun YongHyeon 		/* FALLTHROUGH */
134761f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
134861f45a72SPyun YongHyeon 		if ((hwrev & 0x00700000) == 0x00200000)
134961f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
135061f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1351deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
135259ef640dSPyun YongHyeon 	case RL_HWREV_8168D:
13535fa06abeSPyun YongHyeon 	case RL_HWREV_8168DP:
1354aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1355f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
135681eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1357351a76f9SPyun YongHyeon 		break;
1358d0c45156SPyun YongHyeon 	case RL_HWREV_8168E:
1359d0c45156SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1360d0c45156SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
136181eee0ebSPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1362d0c45156SPyun YongHyeon 		break;
1363f0431c5bSPyun YongHyeon 	case RL_HWREV_8168E_VL:
1364f0431c5bSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1365f0431c5bSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
136681eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1367f0431c5bSPyun YongHyeon 		break;
1368566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1369566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1370566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1371566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1372566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1373566ca8caSJung-uk Kim 		/* FALLTHROUGH */
13740596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
13750596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1376566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1377566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1378351a76f9SPyun YongHyeon 		break;
1379351a76f9SPyun YongHyeon 	default:
1380351a76f9SPyun YongHyeon 		break;
1381351a76f9SPyun YongHyeon 	}
1382351a76f9SPyun YongHyeon 
138393252626SPyun YongHyeon 	/* Reset the adapter. */
138493252626SPyun YongHyeon 	RL_LOCK(sc);
138593252626SPyun YongHyeon 	re_reset(sc);
138693252626SPyun YongHyeon 	RL_UNLOCK(sc);
138793252626SPyun YongHyeon 
1388deb5c680SPyun YongHyeon 	/* Enable PME. */
1389deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1390deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1391deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1392deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1393deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1394deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1395deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1396deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1397deb5c680SPyun YongHyeon 
1398deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1399deb5c680SPyun YongHyeon 		/*
1400deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1401deb5c680SPyun YongHyeon 		 * address from EEPROM.
1402deb5c680SPyun YongHyeon 		 */
1403deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1404deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1405deb5c680SPyun YongHyeon 	} else {
1406141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1407ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1408a94100faSBill Paul 		if (re_did != 0x8129)
1409141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1410a94100faSBill Paul 
1411a94100faSBill Paul 		/*
1412a94100faSBill Paul 		 * Get station address from the EEPROM.
1413a94100faSBill Paul 		 */
1414ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1415be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1416be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1417be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1418deb5c680SPyun YongHyeon 	}
1419ed510fb0SBill Paul 
1420ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1421d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1422ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1423ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1424d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1425d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1426ed510fb0SBill Paul 	} else {
1427d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1428ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1429ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1430d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1431d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1432abc8ff44SBill Paul 	}
14339bac70b8SBill Paul 
1434a94100faSBill Paul 	error = re_allocmem(dev, sc);
1435a94100faSBill Paul 	if (error)
1436a94100faSBill Paul 		goto fail;
14370534aae0SPyun YongHyeon 	re_add_sysctls(sc);
1438a94100faSBill Paul 
1439cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1440cd036ec1SBrooks Davis 	if (ifp == NULL) {
1441d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1442cd036ec1SBrooks Davis 		error = ENOSPC;
1443cd036ec1SBrooks Davis 		goto fail;
1444cd036ec1SBrooks Davis 	}
1445cd036ec1SBrooks Davis 
144661f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
144761f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
144861f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
144961f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
145061f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
145161f45a72SPyun YongHyeon 		else
145261f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
145361f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
145461f45a72SPyun YongHyeon 	}
145561f45a72SPyun YongHyeon 
1456351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
1457d0c45156SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
1458d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
1459351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1460351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1461351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1462351a76f9SPyun YongHyeon 	}
1463351a76f9SPyun YongHyeon 
14648e5d93dbSMarius Strobl #define	RE_PHYAD_INTERNAL	 0
14658e5d93dbSMarius Strobl 
14668e5d93dbSMarius Strobl 	/* Do MII setup. */
14678e5d93dbSMarius Strobl 	phy = RE_PHYAD_INTERNAL;
14688e5d93dbSMarius Strobl 	if (sc->rl_type == RL_8169)
14698e5d93dbSMarius Strobl 		phy = 1;
14708e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
147164436f6eSPyun YongHyeon 	    re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
14728e5d93dbSMarius Strobl 	if (error != 0) {
14738e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
1474a94100faSBill Paul 		goto fail;
1475a94100faSBill Paul 	}
1476a94100faSBill Paul 
1477a94100faSBill Paul 	ifp->if_softc = sc;
14789bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1479a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1480a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1481a94100faSBill Paul 	ifp->if_start = re_start;
1482d6d7d923SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES | CSUM_TSO;
1483d6d7d923SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1484498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1485a94100faSBill Paul 	ifp->if_init = re_init;
148652732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
148752732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
148852732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1489a94100faSBill Paul 
1490ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1491ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1492ed510fb0SBill Paul 
1493a94100faSBill Paul 	/*
1494a94100faSBill Paul 	 * Call MI attach routine.
1495a94100faSBill Paul 	 */
1496a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1497a94100faSBill Paul 
1498960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1499960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1500960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1501960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
15027467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
15037467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
15047467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1505960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1506a2a8420cSPyun YongHyeon 	/*
1507d6d7d923SPyun YongHyeon 	 * Don't enable TSO by default for old controllers. Under
1508d6d7d923SPyun YongHyeon 	 * certain circumtances the controller generated corrupted
1509a2a8420cSPyun YongHyeon 	 * packets in TSO size.
1510a2a8420cSPyun YongHyeon 	 */
1511d6d7d923SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
1512a2a8420cSPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
1513ecafbbb5SPyun YongHyeon 		ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1514d6d7d923SPyun YongHyeon 	}
1515960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1516960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1517960fd5b3SPyun YongHyeon #endif
1518960fd5b3SPyun YongHyeon 	/*
1519960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1520960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1521960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1522960fd5b3SPyun YongHyeon 	 */
1523960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1524960fd5b3SPyun YongHyeon 
1525ed510fb0SBill Paul #ifdef RE_DIAG
1526ed510fb0SBill Paul 	/*
1527ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1528ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1529ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1530ed510fb0SBill Paul 	 */
1531a94100faSBill Paul 
1532ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1533ed510fb0SBill Paul 		error = re_diag(sc);
1534a94100faSBill Paul 		if (error) {
1535ed510fb0SBill Paul 			device_printf(dev,
1536ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1537a94100faSBill Paul 			ether_ifdetach(ifp);
1538a94100faSBill Paul 			goto fail;
1539a94100faSBill Paul 		}
1540ed510fb0SBill Paul 	}
1541ed510fb0SBill Paul #endif
1542a94100faSBill Paul 
1543a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1544351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0)
15455774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
15465774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
15475774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
15485774c5ffSPyun YongHyeon 	else {
15495774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
15505774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
15515774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
15525774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
15535774c5ffSPyun YongHyeon 			if (error != 0)
15545774c5ffSPyun YongHyeon 				break;
15555774c5ffSPyun YongHyeon 		}
15565774c5ffSPyun YongHyeon 	}
1557a94100faSBill Paul 	if (error) {
1558d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1559a94100faSBill Paul 		ether_ifdetach(ifp);
1560a94100faSBill Paul 	}
1561a94100faSBill Paul 
1562a94100faSBill Paul fail:
1563ed510fb0SBill Paul 
1564a94100faSBill Paul 	if (error)
1565a94100faSBill Paul 		re_detach(dev);
1566a94100faSBill Paul 
1567a94100faSBill Paul 	return (error);
1568a94100faSBill Paul }
1569a94100faSBill Paul 
1570a94100faSBill Paul /*
1571a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1572a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1573a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1574a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1575a94100faSBill Paul  * allocated.
1576a94100faSBill Paul  */
1577a94100faSBill Paul static int
15787b5ffebfSPyun YongHyeon re_detach(device_t dev)
1579a94100faSBill Paul {
1580a94100faSBill Paul 	struct rl_softc		*sc;
1581a94100faSBill Paul 	struct ifnet		*ifp;
15825774c5ffSPyun YongHyeon 	int			i, rid;
1583a94100faSBill Paul 
1584a94100faSBill Paul 	sc = device_get_softc(dev);
1585fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1586aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
158797b9d4baSJohn-Mark Gurney 
158881cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
158981cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
159040929967SGleb Smirnoff #ifdef DEVICE_POLLING
159140929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
159240929967SGleb Smirnoff 			ether_poll_deregister(ifp);
159340929967SGleb Smirnoff #endif
159497b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
159597b9d4baSJohn-Mark Gurney #if 0
159697b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
159797b9d4baSJohn-Mark Gurney #endif
1598a94100faSBill Paul 		re_stop(sc);
1599525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1600d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
16013d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
16023d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1603a94100faSBill Paul 		/*
1604a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1605a94100faSBill Paul 		 * still had a BPF descriptor attached to this
160697b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1607a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1608a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1609a94100faSBill Paul 		 * which will try to call re_init() again. This will
1610a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1611a94100faSBill Paul 		 * which will panic the system when the kernel tries
1612a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1613a94100faSBill Paul 		 * anymore.
1614a94100faSBill Paul 		 */
1615a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1616525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1617a94100faSBill Paul 	}
1618a94100faSBill Paul 	if (sc->rl_miibus)
1619a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1620a94100faSBill Paul 	bus_generic_detach(dev);
1621a94100faSBill Paul 
162297b9d4baSJohn-Mark Gurney 	/*
162397b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
162497b9d4baSJohn-Mark Gurney 	 * stopped here.
162597b9d4baSJohn-Mark Gurney 	 */
162697b9d4baSJohn-Mark Gurney 
16275774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
16285774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
16295774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
16305774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
16315774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
16325774c5ffSPyun YongHyeon 		}
16335774c5ffSPyun YongHyeon 	}
1634ad4f426eSWarner Losh 	if (ifp != NULL)
1635ad4f426eSWarner Losh 		if_free(ifp);
1636351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
16375774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
16385774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
16395774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
16405774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
16415774c5ffSPyun YongHyeon 		}
16425774c5ffSPyun YongHyeon 	} else {
16435774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
16445774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
16455774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
16465774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
16475774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
16485774c5ffSPyun YongHyeon 			}
16495774c5ffSPyun YongHyeon 		}
16505774c5ffSPyun YongHyeon 		pci_release_msi(dev);
16515774c5ffSPyun YongHyeon 	}
1652a94100faSBill Paul 	if (sc->rl_res)
1653ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1654ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1655a94100faSBill Paul 
1656a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1657a94100faSBill Paul 
1658a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
16590534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map)
1660a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1661a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
16620534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list)
1663a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1664a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list,
1665a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
1666a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1667a94100faSBill Paul 	}
1668a94100faSBill Paul 
1669a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1670a94100faSBill Paul 
1671a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
16720534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map)
1673a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1674a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
16750534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list)
1676a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1677a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list,
1678a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
1679a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1680a94100faSBill Paul 	}
1681a94100faSBill Paul 
1682a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1683a94100faSBill Paul 
1684d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
16859e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
16869e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1687d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1688d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
16899e18005dSPyun YongHyeon 		}
1690d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1691d65abd66SPyun YongHyeon 	}
1692d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
16939e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
16949e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1695d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1696d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
16979e18005dSPyun YongHyeon 		}
1698d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1699d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1700d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1701d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1702a94100faSBill Paul 	}
170381eee0ebSPyun YongHyeon 	if (sc->rl_ldata.rl_jrx_mtag) {
170481eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
170581eee0ebSPyun YongHyeon 			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
170681eee0ebSPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
170781eee0ebSPyun YongHyeon 				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
170881eee0ebSPyun YongHyeon 		}
170981eee0ebSPyun YongHyeon 		if (sc->rl_ldata.rl_jrx_sparemap)
171081eee0ebSPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
171181eee0ebSPyun YongHyeon 			    sc->rl_ldata.rl_jrx_sparemap);
171281eee0ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
171381eee0ebSPyun YongHyeon 	}
1714a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1715a94100faSBill Paul 
1716a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
17170534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap)
1718a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1719a94100faSBill Paul 			    sc->rl_ldata.rl_smap);
17200534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats)
17210534aae0SPyun YongHyeon 			bus_dmamem_free(sc->rl_ldata.rl_stag,
17220534aae0SPyun YongHyeon 			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1723a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1724a94100faSBill Paul 	}
1725a94100faSBill Paul 
1726a94100faSBill Paul 	if (sc->rl_parent_tag)
1727a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1728a94100faSBill Paul 
1729a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1730a94100faSBill Paul 
1731a94100faSBill Paul 	return (0);
1732a94100faSBill Paul }
1733a94100faSBill Paul 
1734d65abd66SPyun YongHyeon static __inline void
17357b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1736a94100faSBill Paul {
1737d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1738d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1739d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1740a94100faSBill Paul 
174181eee0ebSPyun YongHyeon 	if (sc->rl_ifp->if_mtu > RL_MTU &&
174281eee0ebSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
174381eee0ebSPyun YongHyeon 		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
174481eee0ebSPyun YongHyeon 	else
1745d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1746d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1747d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1748d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1749d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1750d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1751d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1752d65abd66SPyun YongHyeon }
1753d65abd66SPyun YongHyeon 
1754d65abd66SPyun YongHyeon static int
17557b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1756d65abd66SPyun YongHyeon {
1757d65abd66SPyun YongHyeon 	struct mbuf		*m;
1758d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1759d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1760d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1761d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1762d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1763d65abd66SPyun YongHyeon 	int			error, nsegs;
1764d65abd66SPyun YongHyeon 
1765d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1766d65abd66SPyun YongHyeon 	if (m == NULL)
1767a94100faSBill Paul 		return (ENOBUFS);
1768a94100faSBill Paul 
1769a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
177022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
177122a11c96SJohn-Mark Gurney 	/*
177222a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
177322a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
177422a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
177522a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
177622a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
177722a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
177822a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
177922a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
178022a11c96SJohn-Mark Gurney 	 */
178122a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
178222a11c96SJohn-Mark Gurney #endif
1783d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1784d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1785d65abd66SPyun YongHyeon 	if (error != 0) {
1786d65abd66SPyun YongHyeon 		m_freem(m);
1787d65abd66SPyun YongHyeon 		return (ENOBUFS);
1788d65abd66SPyun YongHyeon 	}
1789d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1790a94100faSBill Paul 
1791d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1792d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1793d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1794d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1795d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1796a94100faSBill Paul 	}
1797a94100faSBill Paul 
1798d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1799d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1800d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1801d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1802d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1803d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1804a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1805a94100faSBill Paul 
1806d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1807d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1808d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1809d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1810d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1811d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1812d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1813d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1814d65abd66SPyun YongHyeon 
1815a94100faSBill Paul 	return (0);
1816a94100faSBill Paul }
1817a94100faSBill Paul 
181881eee0ebSPyun YongHyeon static int
181981eee0ebSPyun YongHyeon re_jumbo_newbuf(struct rl_softc *sc, int idx)
182081eee0ebSPyun YongHyeon {
182181eee0ebSPyun YongHyeon 	struct mbuf		*m;
182281eee0ebSPyun YongHyeon 	struct rl_rxdesc	*rxd;
182381eee0ebSPyun YongHyeon 	bus_dma_segment_t	segs[1];
182481eee0ebSPyun YongHyeon 	bus_dmamap_t		map;
182581eee0ebSPyun YongHyeon 	struct rl_desc		*desc;
182681eee0ebSPyun YongHyeon 	uint32_t		cmdstat;
182781eee0ebSPyun YongHyeon 	int			error, nsegs;
182881eee0ebSPyun YongHyeon 
182981eee0ebSPyun YongHyeon 	m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
183081eee0ebSPyun YongHyeon 	if (m == NULL)
183181eee0ebSPyun YongHyeon 		return (ENOBUFS);
183281eee0ebSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
183381eee0ebSPyun YongHyeon #ifdef RE_FIXUP_RX
183481eee0ebSPyun YongHyeon 	m_adj(m, RE_ETHER_ALIGN);
183581eee0ebSPyun YongHyeon #endif
183681eee0ebSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
183781eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
183881eee0ebSPyun YongHyeon 	if (error != 0) {
183981eee0ebSPyun YongHyeon 		m_freem(m);
184081eee0ebSPyun YongHyeon 		return (ENOBUFS);
184181eee0ebSPyun YongHyeon 	}
184281eee0ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
184381eee0ebSPyun YongHyeon 
184481eee0ebSPyun YongHyeon 	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
184581eee0ebSPyun YongHyeon 	if (rxd->rx_m != NULL) {
184681eee0ebSPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
184781eee0ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
184881eee0ebSPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
184981eee0ebSPyun YongHyeon 	}
185081eee0ebSPyun YongHyeon 
185181eee0ebSPyun YongHyeon 	rxd->rx_m = m;
185281eee0ebSPyun YongHyeon 	map = rxd->rx_dmamap;
185381eee0ebSPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
185481eee0ebSPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
185581eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_jrx_sparemap = map;
185681eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
185781eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
185881eee0ebSPyun YongHyeon 
185981eee0ebSPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
186081eee0ebSPyun YongHyeon 	desc->rl_vlanctl = 0;
186181eee0ebSPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
186281eee0ebSPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
186381eee0ebSPyun YongHyeon 	cmdstat = segs[0].ds_len;
186481eee0ebSPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
186581eee0ebSPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
186681eee0ebSPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
186781eee0ebSPyun YongHyeon 
186881eee0ebSPyun YongHyeon 	return (0);
186981eee0ebSPyun YongHyeon }
187081eee0ebSPyun YongHyeon 
187122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
187222a11c96SJohn-Mark Gurney static __inline void
18737b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
187422a11c96SJohn-Mark Gurney {
187522a11c96SJohn-Mark Gurney 	int                     i;
187622a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
187722a11c96SJohn-Mark Gurney 
187822a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
187922a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
188022a11c96SJohn-Mark Gurney 
188122a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
188222a11c96SJohn-Mark Gurney 		*dst++ = *src++;
188322a11c96SJohn-Mark Gurney 
188422a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
188522a11c96SJohn-Mark Gurney }
188622a11c96SJohn-Mark Gurney #endif
188722a11c96SJohn-Mark Gurney 
1888a94100faSBill Paul static int
18897b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
1890a94100faSBill Paul {
1891d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1892d65abd66SPyun YongHyeon 	int			i;
189397b9d4baSJohn-Mark Gurney 
189497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
189597b9d4baSJohn-Mark Gurney 
1896d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1897d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1898d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1899d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1900d65abd66SPyun YongHyeon 	/* Set EOR. */
1901d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1902d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1903a94100faSBill Paul 
1904a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1905d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1906d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1907d65abd66SPyun YongHyeon 
1908a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1909a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1910d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1911a94100faSBill Paul 
1912a94100faSBill Paul 	return (0);
1913a94100faSBill Paul }
1914a94100faSBill Paul 
1915a94100faSBill Paul static int
19167b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
1917a94100faSBill Paul {
1918d65abd66SPyun YongHyeon 	int			error, i;
1919a94100faSBill Paul 
1920d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1921d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1922d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1923d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1924d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1925d65abd66SPyun YongHyeon 			return (error);
1926a94100faSBill Paul 	}
1927a94100faSBill Paul 
1928a94100faSBill Paul 	/* Flush the RX descriptors */
1929a94100faSBill Paul 
1930a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1931a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1932a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1933a94100faSBill Paul 
1934a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1935a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1936a94100faSBill Paul 
1937a94100faSBill Paul 	return (0);
1938a94100faSBill Paul }
1939a94100faSBill Paul 
194081eee0ebSPyun YongHyeon static int
194181eee0ebSPyun YongHyeon re_jrx_list_init(struct rl_softc *sc)
194281eee0ebSPyun YongHyeon {
194381eee0ebSPyun YongHyeon 	int			error, i;
194481eee0ebSPyun YongHyeon 
194581eee0ebSPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
194681eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
194781eee0ebSPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
194881eee0ebSPyun YongHyeon 		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
194981eee0ebSPyun YongHyeon 		if ((error = re_jumbo_newbuf(sc, i)) != 0)
195081eee0ebSPyun YongHyeon 			return (error);
195181eee0ebSPyun YongHyeon 	}
195281eee0ebSPyun YongHyeon 
195381eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
195481eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_list_map,
195581eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
195681eee0ebSPyun YongHyeon 
195781eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_rx_prodidx = 0;
195881eee0ebSPyun YongHyeon 	sc->rl_head = sc->rl_tail = NULL;
195981eee0ebSPyun YongHyeon 
196081eee0ebSPyun YongHyeon 	return (0);
196181eee0ebSPyun YongHyeon }
196281eee0ebSPyun YongHyeon 
1963a94100faSBill Paul /*
1964a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
1965a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
1966a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
1967a94100faSBill Paul  */
1968ed510fb0SBill Paul static int
19691abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
1970a94100faSBill Paul {
1971a94100faSBill Paul 	struct mbuf		*m;
1972a94100faSBill Paul 	struct ifnet		*ifp;
197381eee0ebSPyun YongHyeon 	int			i, rxerr, total_len;
1974a94100faSBill Paul 	struct rl_desc		*cur_rx;
1975a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
197681eee0ebSPyun YongHyeon 	int			jumbo, maxpkt = 16, rx_npkts = 0;
1977a94100faSBill Paul 
19785120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
19795120abbfSSam Leffler 
1980fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
198181eee0ebSPyun YongHyeon 	if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
198281eee0ebSPyun YongHyeon 		jumbo = 1;
198381eee0ebSPyun YongHyeon 	else
198481eee0ebSPyun YongHyeon 		jumbo = 0;
1985a94100faSBill Paul 
1986a94100faSBill Paul 	/* Invalidate the descriptor memory */
1987a94100faSBill Paul 
1988a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1989a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1990d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1991a94100faSBill Paul 
1992d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
1993d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
19945b6d1d9dSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
19955b6d1d9dSPyun YongHyeon 			break;
1996a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
1997a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
1998d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
1999d65abd66SPyun YongHyeon 			break;
2000d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
2001a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
200281eee0ebSPyun YongHyeon 		if (jumbo != 0)
200381eee0ebSPyun YongHyeon 			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
200481eee0ebSPyun YongHyeon 		else
2005d65abd66SPyun YongHyeon 			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2006a94100faSBill Paul 
200781eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
200881eee0ebSPyun YongHyeon 		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
200981eee0ebSPyun YongHyeon 		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
201081eee0ebSPyun YongHyeon 			/*
201181eee0ebSPyun YongHyeon 			 * RTL8168C or later controllers do not
201281eee0ebSPyun YongHyeon 			 * support multi-fragment packet.
201381eee0ebSPyun YongHyeon 			 */
201481eee0ebSPyun YongHyeon 			re_discard_rxbuf(sc, i);
201581eee0ebSPyun YongHyeon 			continue;
201681eee0ebSPyun YongHyeon 		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2017d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
2018d65abd66SPyun YongHyeon 				/*
2019d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
2020d65abd66SPyun YongHyeon 				 * discard all the pieces.
2021d65abd66SPyun YongHyeon 				 */
2022d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
2023d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
2024d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
2025d65abd66SPyun YongHyeon 				}
2026d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2027d65abd66SPyun YongHyeon 				continue;
2028d65abd66SPyun YongHyeon 			}
202922a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
2030a94100faSBill Paul 			if (sc->rl_head == NULL)
2031a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
2032a94100faSBill Paul 			else {
2033a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2034a94100faSBill Paul 				sc->rl_tail->m_next = m;
2035a94100faSBill Paul 				sc->rl_tail = m;
2036a94100faSBill Paul 			}
2037a94100faSBill Paul 			continue;
2038a94100faSBill Paul 		}
2039a94100faSBill Paul 
2040a94100faSBill Paul 		/*
2041a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
2042a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
2043a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
2044a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
2045a94100faSBill Paul 		 * were already used, so to make room for the extra
2046a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
2047a94100faSBill Paul 		 * error' bit and shifted the other status bits
2048a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
2049a94100faSBill Paul 		 * still in the same places. We have already extracted
2050a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
2051a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
2052a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
2053a94100faSBill Paul 		 * them using the 8169 status as though it was in the
2054a94100faSBill Paul 		 * same format as that of the 8139C+.
2055a94100faSBill Paul 		 */
2056a94100faSBill Paul 		if (sc->rl_type == RL_8169)
2057a94100faSBill Paul 			rxstat >>= 1;
2058a94100faSBill Paul 
205922a11c96SJohn-Mark Gurney 		/*
206022a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
206122a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
206222a11c96SJohn-Mark Gurney 		 */
206381eee0ebSPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
206481eee0ebSPyun YongHyeon 			rxerr = 1;
206581eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
206681eee0ebSPyun YongHyeon 			    total_len > 8191 &&
206781eee0ebSPyun YongHyeon 			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
206881eee0ebSPyun YongHyeon 				rxerr = 0;
206981eee0ebSPyun YongHyeon 			if (rxerr != 0) {
2070a94100faSBill Paul 				ifp->if_ierrors++;
2071a94100faSBill Paul 				/*
2072a94100faSBill Paul 				 * If this is part of a multi-fragment packet,
2073a94100faSBill Paul 				 * discard all the pieces.
2074a94100faSBill Paul 				 */
2075a94100faSBill Paul 				if (sc->rl_head != NULL) {
2076a94100faSBill Paul 					m_freem(sc->rl_head);
2077a94100faSBill Paul 					sc->rl_head = sc->rl_tail = NULL;
2078a94100faSBill Paul 				}
2079d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2080a94100faSBill Paul 				continue;
2081a94100faSBill Paul 			}
208281eee0ebSPyun YongHyeon 		}
2083a94100faSBill Paul 
2084a94100faSBill Paul 		/*
2085a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
2086a94100faSBill Paul 		 * reload the current one.
2087a94100faSBill Paul 		 */
208881eee0ebSPyun YongHyeon 		if (jumbo != 0)
208981eee0ebSPyun YongHyeon 			rxerr = re_jumbo_newbuf(sc, i);
209081eee0ebSPyun YongHyeon 		else
209181eee0ebSPyun YongHyeon 			rxerr = re_newbuf(sc, i);
209281eee0ebSPyun YongHyeon 		if (rxerr != 0) {
2093d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
2094a94100faSBill Paul 			if (sc->rl_head != NULL) {
2095a94100faSBill Paul 				m_freem(sc->rl_head);
2096a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
2097a94100faSBill Paul 			}
2098d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
2099a94100faSBill Paul 			continue;
2100a94100faSBill Paul 		}
2101a94100faSBill Paul 
2102a94100faSBill Paul 		if (sc->rl_head != NULL) {
210381eee0ebSPyun YongHyeon 			if (jumbo != 0)
210481eee0ebSPyun YongHyeon 				m->m_len = total_len;
210581eee0ebSPyun YongHyeon 			else {
210622a11c96SJohn-Mark Gurney 				m->m_len = total_len % RE_RX_DESC_BUFLEN;
210722a11c96SJohn-Mark Gurney 				if (m->m_len == 0)
210822a11c96SJohn-Mark Gurney 					m->m_len = RE_RX_DESC_BUFLEN;
210981eee0ebSPyun YongHyeon 			}
2110a94100faSBill Paul 			/*
2111a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
2112a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
2113a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
2114a94100faSBill Paul 			 * care about anyway.
2115a94100faSBill Paul 			 */
2116a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
2117a94100faSBill Paul 				sc->rl_tail->m_len -=
2118a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
2119a94100faSBill Paul 				m_freem(m);
2120a94100faSBill Paul 			} else {
2121a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
2122a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2123a94100faSBill Paul 				sc->rl_tail->m_next = m;
2124a94100faSBill Paul 			}
2125a94100faSBill Paul 			m = sc->rl_head;
2126a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
2127a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2128a94100faSBill Paul 		} else
2129a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
2130a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
2131a94100faSBill Paul 
213222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
213322a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
213422a11c96SJohn-Mark Gurney #endif
2135a94100faSBill Paul 		ifp->if_ipackets++;
2136a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
2137a94100faSBill Paul 
2138a94100faSBill Paul 		/* Do RX checksumming if enabled */
2139a94100faSBill Paul 
2140a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2141deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2142a94100faSBill Paul 				/* Check IP header checksum */
2143a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
2144deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2145deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2146a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2147deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2148deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2149a94100faSBill Paul 
2150a94100faSBill Paul 				/* Check TCP/UDP checksum */
2151a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
2152a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2153a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
2154a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2155a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
2156a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2157a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
2158a94100faSBill Paul 				}
2159deb5c680SPyun YongHyeon 			} else {
2160deb5c680SPyun YongHyeon 				/*
2161deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2162deb5c680SPyun YongHyeon 				 */
2163deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2164deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2165deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2166deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2167deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2168deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2169deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2170deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2171deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
2172deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2173deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
2174deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2175deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2176deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2177deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
2178deb5c680SPyun YongHyeon 				}
2179deb5c680SPyun YongHyeon 			}
2180a94100faSBill Paul 		}
2181ed510fb0SBill Paul 		maxpkt--;
2182d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
218378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
2184bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
218578ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2186d147662cSGleb Smirnoff 		}
21875120abbfSSam Leffler 		RL_UNLOCK(sc);
2188a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
21895120abbfSSam Leffler 		RL_LOCK(sc);
21901abcdbd1SAttilio Rao 		rx_npkts++;
2191a94100faSBill Paul 	}
2192a94100faSBill Paul 
2193a94100faSBill Paul 	/* Flush the RX DMA ring */
2194a94100faSBill Paul 
2195a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2196a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2197a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2198a94100faSBill Paul 
2199a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
2200ed510fb0SBill Paul 
22011abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
22021abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2203ed510fb0SBill Paul 	if (maxpkt)
2204ed510fb0SBill Paul 		return (EAGAIN);
2205ed510fb0SBill Paul 
2206ed510fb0SBill Paul 	return (0);
2207a94100faSBill Paul }
2208a94100faSBill Paul 
2209a94100faSBill Paul static void
22107b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2211a94100faSBill Paul {
2212a94100faSBill Paul 	struct ifnet		*ifp;
2213d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2214a94100faSBill Paul 	u_int32_t		txstat;
2215d65abd66SPyun YongHyeon 	int			cons;
2216d65abd66SPyun YongHyeon 
2217d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2218d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2219d65abd66SPyun YongHyeon 		return;
2220a94100faSBill Paul 
2221fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2222a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2223a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2224a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2225d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2226a94100faSBill Paul 
2227d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2228d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2229d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2230d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2231a94100faSBill Paul 			break;
2232a94100faSBill Paul 		/*
2233a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2234a94100faSBill Paul 		 * in a fragment chain, which also happens to
2235a94100faSBill Paul 		 * be the only place where the TX status bits
2236a94100faSBill Paul 		 * are valid.
2237a94100faSBill Paul 		 */
2238a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2239d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2240d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2241d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2242d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2243d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2244d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2245d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2246d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2247d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2248a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2249a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2250a94100faSBill Paul 				ifp->if_collisions++;
2251a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2252a94100faSBill Paul 				ifp->if_oerrors++;
2253a94100faSBill Paul 			else
2254a94100faSBill Paul 				ifp->if_opackets++;
2255a94100faSBill Paul 		}
2256a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2257d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2258a94100faSBill Paul 	}
2259d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2260a94100faSBill Paul 
2261a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2262a94100faSBill Paul 
2263d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2264ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2265a94100faSBill Paul 		/*
2266b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2267b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2268a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2269a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2270a94100faSBill Paul 		 */
2271a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2272ed510fb0SBill Paul #endif
2273b4b95879SMarius Strobl 	} else
2274b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2275a94100faSBill Paul }
2276a94100faSBill Paul 
2277a94100faSBill Paul static void
22787b5ffebfSPyun YongHyeon re_tick(void *xsc)
2279a94100faSBill Paul {
2280a94100faSBill Paul 	struct rl_softc		*sc;
2281d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2282a94100faSBill Paul 
2283a94100faSBill Paul 	sc = xsc;
228497b9d4baSJohn-Mark Gurney 
228597b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
228697b9d4baSJohn-Mark Gurney 
22871d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2288a94100faSBill Paul 	mii_tick(mii);
22890fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
22900fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2291c2d2e19cSPyun YongHyeon 	/*
2292c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2293c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2294c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2295c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2296c2d2e19cSPyun YongHyeon 	 */
2297c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2298130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2299d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2300a94100faSBill Paul }
2301a94100faSBill Paul 
2302a94100faSBill Paul #ifdef DEVICE_POLLING
23031abcdbd1SAttilio Rao static int
2304a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2305a94100faSBill Paul {
2306a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
23071abcdbd1SAttilio Rao 	int rx_npkts = 0;
2308a94100faSBill Paul 
2309a94100faSBill Paul 	RL_LOCK(sc);
231040929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
23111abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
231297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
23131abcdbd1SAttilio Rao 	return (rx_npkts);
231497b9d4baSJohn-Mark Gurney }
231597b9d4baSJohn-Mark Gurney 
23161abcdbd1SAttilio Rao static int
231797b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
231897b9d4baSJohn-Mark Gurney {
231997b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
23201abcdbd1SAttilio Rao 	int rx_npkts;
232197b9d4baSJohn-Mark Gurney 
232297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
232397b9d4baSJohn-Mark Gurney 
2324a94100faSBill Paul 	sc->rxcycles = count;
23251abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2326a94100faSBill Paul 	re_txeof(sc);
2327a94100faSBill Paul 
232837652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2329ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2330a94100faSBill Paul 
2331a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2332a94100faSBill Paul 		u_int16_t       status;
2333a94100faSBill Paul 
2334a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2335a94100faSBill Paul 		if (status == 0xffff)
23361abcdbd1SAttilio Rao 			return (rx_npkts);
2337a94100faSBill Paul 		if (status)
2338a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2339818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2340818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2341818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2342a94100faSBill Paul 
2343a94100faSBill Paul 		/*
2344a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2345a94100faSBill Paul 		 */
2346a94100faSBill Paul 
23478476c243SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR) {
23488476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
234997b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2350a94100faSBill Paul 		}
23518476c243SPyun YongHyeon 	}
23521abcdbd1SAttilio Rao 	return (rx_npkts);
2353a94100faSBill Paul }
2354a94100faSBill Paul #endif /* DEVICE_POLLING */
2355a94100faSBill Paul 
2356ef544f63SPaolo Pisati static int
23577b5ffebfSPyun YongHyeon re_intr(void *arg)
2358a94100faSBill Paul {
2359a94100faSBill Paul 	struct rl_softc		*sc;
2360ed510fb0SBill Paul 	uint16_t		status;
2361a94100faSBill Paul 
2362a94100faSBill Paul 	sc = arg;
2363ed510fb0SBill Paul 
2364ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2365498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2366ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2367ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2368ed510fb0SBill Paul 
2369ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2370ed510fb0SBill Paul 
2371ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2372ed510fb0SBill Paul }
2373ed510fb0SBill Paul 
2374ed510fb0SBill Paul static void
23757b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2376ed510fb0SBill Paul {
2377ed510fb0SBill Paul 	struct rl_softc		*sc;
2378ed510fb0SBill Paul 	struct ifnet		*ifp;
2379ed510fb0SBill Paul 	u_int16_t		status;
2380ed510fb0SBill Paul 	int			rval = 0;
2381ed510fb0SBill Paul 
2382ed510fb0SBill Paul 	sc = arg;
2383ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2384a94100faSBill Paul 
2385a94100faSBill Paul 	RL_LOCK(sc);
238697b9d4baSJohn-Mark Gurney 
2387a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2388a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2389a94100faSBill Paul 
2390d65abd66SPyun YongHyeon 	if (sc->suspended ||
2391d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2392ed510fb0SBill Paul 		RL_UNLOCK(sc);
2393ed510fb0SBill Paul 		return;
2394ed510fb0SBill Paul 	}
2395a94100faSBill Paul 
2396ed510fb0SBill Paul #ifdef DEVICE_POLLING
2397ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2398ed510fb0SBill Paul 		RL_UNLOCK(sc);
2399ed510fb0SBill Paul 		return;
2400ed510fb0SBill Paul 	}
2401ed510fb0SBill Paul #endif
2402a94100faSBill Paul 
2403ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
24041abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2405ed510fb0SBill Paul 
2406818951afSPyun YongHyeon 	/*
2407818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2408818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2409818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2410818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2411818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2412818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2413818951afSPyun YongHyeon 	 */
2414818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2415818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2416818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
24173d85c23dSPyun YongHyeon 	if (status & (
2418ed510fb0SBill Paul #ifdef RE_TX_MODERATION
24193d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2420ed510fb0SBill Paul #else
24213d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2422ed510fb0SBill Paul #endif
2423ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2424a94100faSBill Paul 		re_txeof(sc);
2425a94100faSBill Paul 
24268476c243SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
24278476c243SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
242897b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
24298476c243SPyun YongHyeon 	}
2430a94100faSBill Paul 
243152732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2432ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2433a94100faSBill Paul 
2434a94100faSBill Paul 	RL_UNLOCK(sc);
2435ed510fb0SBill Paul 
2436ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2437ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2438ed510fb0SBill Paul 		return;
2439ed510fb0SBill Paul 	}
2440ed510fb0SBill Paul 
2441ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2442a94100faSBill Paul }
2443a94100faSBill Paul 
2444d65abd66SPyun YongHyeon static int
24457b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2446d65abd66SPyun YongHyeon {
2447d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2448d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2449d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2450d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2451d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2452d65abd66SPyun YongHyeon 	int			nsegs, prod;
2453d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2454d65abd66SPyun YongHyeon 	int			padlen;
2455ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2456a94100faSBill Paul 
2457d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2458738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
24590fc4974fSBill Paul 
24600fc4974fSBill Paul 	/*
24610fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
24620fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
24630fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
24640fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
24650fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
24660fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
246799c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
24680fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2469d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
24700fc4974fSBill Paul 	 */
2471f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2472deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
247399c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2474d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2475d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2476d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2477d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2478d65abd66SPyun YongHyeon 			m_freem(*m_head);
2479d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2480d65abd66SPyun YongHyeon 				*m_head = NULL;
2481a94100faSBill Paul 				return (ENOBUFS);
2482a94100faSBill Paul 			}
2483d65abd66SPyun YongHyeon 			*m_head = m_new;
2484d65abd66SPyun YongHyeon 		}
2485d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2486d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
248780a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2488b4b95879SMarius Strobl 			if (m_new == NULL) {
2489b4b95879SMarius Strobl 				m_freem(*m_head);
2490b4b95879SMarius Strobl 				*m_head = NULL;
249180a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2492b4b95879SMarius Strobl 			}
2493d65abd66SPyun YongHyeon 		} else
2494d65abd66SPyun YongHyeon 			m_new = *m_head;
2495a94100faSBill Paul 
24960fc4974fSBill Paul 		/*
24970fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
24980fc4974fSBill Paul 		 * to avoid leaking data.
24990fc4974fSBill Paul 		 */
2500d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2501d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
25020fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2503d65abd66SPyun YongHyeon 		*m_head = m_new;
25040fc4974fSBill Paul 	}
25050fc4974fSBill Paul 
2506d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2507d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2508d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2509d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2510d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2511304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2512d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2513d65abd66SPyun YongHyeon 			m_freem(*m_head);
2514b4b95879SMarius Strobl 			*m_head = NULL;
2515d65abd66SPyun YongHyeon 			return (ENOBUFS);
2516a94100faSBill Paul 		}
2517d65abd66SPyun YongHyeon 		*m_head = m_new;
2518d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2519d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2520d65abd66SPyun YongHyeon 		if (error != 0) {
2521d65abd66SPyun YongHyeon 			m_freem(*m_head);
2522d65abd66SPyun YongHyeon 			*m_head = NULL;
2523d65abd66SPyun YongHyeon 			return (error);
2524a94100faSBill Paul 		}
2525d65abd66SPyun YongHyeon 	} else if (error != 0)
2526d65abd66SPyun YongHyeon 		return (error);
2527d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2528d65abd66SPyun YongHyeon 		m_freem(*m_head);
2529d65abd66SPyun YongHyeon 		*m_head = NULL;
2530d65abd66SPyun YongHyeon 		return (EIO);
2531d65abd66SPyun YongHyeon 	}
2532d65abd66SPyun YongHyeon 
2533d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2534d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2535d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2536d65abd66SPyun YongHyeon 		return (ENOBUFS);
2537d65abd66SPyun YongHyeon 	}
2538d65abd66SPyun YongHyeon 
2539d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2540d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2541a94100faSBill Paul 
2542a94100faSBill Paul 	/*
2543d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2544d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2545d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2546d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2547a94100faSBill Paul 	 */
2548deb5c680SPyun YongHyeon 	vlanctl = 0;
2549d65abd66SPyun YongHyeon 	csum_flags = 0;
2550d6d7d923SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2551d6d7d923SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2552d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND;
2553d6d7d923SPyun YongHyeon 			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2554d6d7d923SPyun YongHyeon 			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2555d6d7d923SPyun YongHyeon 		} else {
2556d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND |
2557d65abd66SPyun YongHyeon 			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2558d65abd66SPyun YongHyeon 			    RL_TDESC_CMD_MSSVAL_SHIFT);
2559d6d7d923SPyun YongHyeon 		}
2560d6d7d923SPyun YongHyeon 	} else {
256199c8ae87SPyun YongHyeon 		/*
256299c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
256399c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
256499c8ae87SPyun YongHyeon 		 * does't make effects.
256599c8ae87SPyun YongHyeon 		 */
256699c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2567deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2568d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2569deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2570deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2571d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2572deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2573deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2574d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2575deb5c680SPyun YongHyeon 			} else {
2576deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2577deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2578deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2579deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2580deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2581deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2582deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2583deb5c680SPyun YongHyeon 			}
2584d65abd66SPyun YongHyeon 		}
258599c8ae87SPyun YongHyeon 	}
2586a94100faSBill Paul 
2587ccf34c81SPyun YongHyeon 	/*
2588ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2589ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2590ccf34c81SPyun YongHyeon 	 * transmission attempt.
2591ccf34c81SPyun YongHyeon 	 */
2592ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2593bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2594deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2595ccf34c81SPyun YongHyeon 
2596d65abd66SPyun YongHyeon 	si = prod;
2597d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2598d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2599deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2600d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2601d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2602d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2603d65abd66SPyun YongHyeon 		if (i != 0)
2604d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2605d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2606d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2607d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2608d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2609d65abd66SPyun YongHyeon 	}
2610d65abd66SPyun YongHyeon 	/* Update producer index. */
2611d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2612a94100faSBill Paul 
2613d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2614d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2615d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2616d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2617d65abd66SPyun YongHyeon 
2618d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2619d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2620d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2621a94100faSBill Paul 
2622d65abd66SPyun YongHyeon 	/*
2623d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2624d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2625d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2626d65abd66SPyun YongHyeon 	 */
2627d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2628d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2629d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2630d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2631d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2632a94100faSBill Paul 
2633a94100faSBill Paul 	return (0);
2634a94100faSBill Paul }
2635a94100faSBill Paul 
263697b9d4baSJohn-Mark Gurney static void
26377b5ffebfSPyun YongHyeon re_tx_task(void *arg, int npending)
263897b9d4baSJohn-Mark Gurney {
2639ed510fb0SBill Paul 	struct ifnet		*ifp;
264097b9d4baSJohn-Mark Gurney 
2641ed510fb0SBill Paul 	ifp = arg;
2642ed510fb0SBill Paul 	re_start(ifp);
264397b9d4baSJohn-Mark Gurney }
264497b9d4baSJohn-Mark Gurney 
2645a94100faSBill Paul /*
2646a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2647a94100faSBill Paul  */
2648a94100faSBill Paul static void
26497b5ffebfSPyun YongHyeon re_start(struct ifnet *ifp)
2650a94100faSBill Paul {
2651a94100faSBill Paul 	struct rl_softc		*sc;
2652d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2653d65abd66SPyun YongHyeon 	int			queued;
2654a94100faSBill Paul 
2655a94100faSBill Paul 	sc = ifp->if_softc;
265697b9d4baSJohn-Mark Gurney 
2657ed510fb0SBill Paul 	RL_LOCK(sc);
2658ed510fb0SBill Paul 
2659d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2660351a76f9SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) {
2661ed510fb0SBill Paul 		RL_UNLOCK(sc);
2662ed510fb0SBill Paul 		return;
2663ed510fb0SBill Paul 	}
2664a94100faSBill Paul 
2665d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2666d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
266752732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2668a94100faSBill Paul 		if (m_head == NULL)
2669a94100faSBill Paul 			break;
2670a94100faSBill Paul 
2671d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2672b4b95879SMarius Strobl 			if (m_head == NULL)
2673b4b95879SMarius Strobl 				break;
267452732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
267513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2676a94100faSBill Paul 			break;
2677a94100faSBill Paul 		}
2678a94100faSBill Paul 
2679a94100faSBill Paul 		/*
2680a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2681a94100faSBill Paul 		 * to him.
2682a94100faSBill Paul 		 */
268359a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
268452732175SMax Laier 
268552732175SMax Laier 		queued++;
2686a94100faSBill Paul 	}
2687a94100faSBill Paul 
2688ed510fb0SBill Paul 	if (queued == 0) {
2689ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2690d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2691ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2692ed510fb0SBill Paul #endif
2693ed510fb0SBill Paul 		RL_UNLOCK(sc);
269452732175SMax Laier 		return;
2695ed510fb0SBill Paul 	}
269652732175SMax Laier 
2697a94100faSBill Paul 	/* Flush the TX descriptors */
2698a94100faSBill Paul 
2699a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2700a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2701a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2702a94100faSBill Paul 
27030fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2704a94100faSBill Paul 
2705ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2706a94100faSBill Paul 	/*
2707a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2708a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2709a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2710a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2711a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2712a94100faSBill Paul 	 * the timer count is reset to 0.
2713a94100faSBill Paul 	 */
2714a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2715ed510fb0SBill Paul #endif
2716a94100faSBill Paul 
2717a94100faSBill Paul 	/*
2718a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2719a94100faSBill Paul 	 */
27201d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2721ed510fb0SBill Paul 
2722ed510fb0SBill Paul 	RL_UNLOCK(sc);
2723a94100faSBill Paul }
2724a94100faSBill Paul 
2725a94100faSBill Paul static void
272681eee0ebSPyun YongHyeon re_set_jumbo(struct rl_softc *sc, int jumbo)
272781eee0ebSPyun YongHyeon {
272881eee0ebSPyun YongHyeon 
272981eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
273081eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
273181eee0ebSPyun YongHyeon 		return;
273281eee0ebSPyun YongHyeon 	}
273381eee0ebSPyun YongHyeon 
273481eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
273581eee0ebSPyun YongHyeon 	if (jumbo != 0) {
273681eee0ebSPyun YongHyeon 		CSR_WRITE_1(sc, RL_CFG3, CSR_READ_1(sc, RL_CFG3) |
273781eee0ebSPyun YongHyeon 		    RL_CFG3_JUMBO_EN0);
273881eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
273981eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
274081eee0ebSPyun YongHyeon 			break;
274181eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
274281eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) |
274381eee0ebSPyun YongHyeon 			    0x01);
274481eee0ebSPyun YongHyeon 			break;
274581eee0ebSPyun YongHyeon 		default:
274681eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) |
274781eee0ebSPyun YongHyeon 			    RL_CFG4_JUMBO_EN1);
274881eee0ebSPyun YongHyeon 		}
274981eee0ebSPyun YongHyeon 	} else {
275081eee0ebSPyun YongHyeon 		CSR_WRITE_1(sc, RL_CFG3, CSR_READ_1(sc, RL_CFG3) &
275181eee0ebSPyun YongHyeon 		    ~RL_CFG3_JUMBO_EN0);
275281eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
275381eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
275481eee0ebSPyun YongHyeon 			break;
275581eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
275681eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) &
275781eee0ebSPyun YongHyeon 			    ~0x01);
275881eee0ebSPyun YongHyeon 			break;
275981eee0ebSPyun YongHyeon 		default:
276081eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) &
276181eee0ebSPyun YongHyeon 			    ~RL_CFG4_JUMBO_EN1);
276281eee0ebSPyun YongHyeon 		}
276381eee0ebSPyun YongHyeon 	}
276481eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
276581eee0ebSPyun YongHyeon 
276681eee0ebSPyun YongHyeon 	switch (sc->rl_hwrev->rl_rev) {
276781eee0ebSPyun YongHyeon 	case RL_HWREV_8168DP:
276881eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
276981eee0ebSPyun YongHyeon 		break;
277081eee0ebSPyun YongHyeon 	default:
277181eee0ebSPyun YongHyeon 		if (jumbo != 0)
277281eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 512);
277381eee0ebSPyun YongHyeon 		else
277481eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 4096);
277581eee0ebSPyun YongHyeon 	}
277681eee0ebSPyun YongHyeon }
277781eee0ebSPyun YongHyeon 
277881eee0ebSPyun YongHyeon static void
27797b5ffebfSPyun YongHyeon re_init(void *xsc)
2780a94100faSBill Paul {
2781a94100faSBill Paul 	struct rl_softc		*sc = xsc;
278297b9d4baSJohn-Mark Gurney 
278397b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
278497b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
278597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
278697b9d4baSJohn-Mark Gurney }
278797b9d4baSJohn-Mark Gurney 
278897b9d4baSJohn-Mark Gurney static void
27897b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
279097b9d4baSJohn-Mark Gurney {
2791fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2792a94100faSBill Paul 	struct mii_data		*mii;
2793566ca8caSJung-uk Kim 	uint32_t		reg;
279470acaecfSPyun YongHyeon 	uint16_t		cfg;
27954d3d7085SBernd Walter 	union {
27964d3d7085SBernd Walter 		uint32_t align_dummy;
27974d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
27984d3d7085SBernd Walter         } eaddr;
2799a94100faSBill Paul 
280097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
280197b9d4baSJohn-Mark Gurney 
2802a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2803a94100faSBill Paul 
28048476c243SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
28058476c243SPyun YongHyeon 		return;
28068476c243SPyun YongHyeon 
2807a94100faSBill Paul 	/*
2808a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2809a94100faSBill Paul 	 */
2810a94100faSBill Paul 	re_stop(sc);
2811a94100faSBill Paul 
2812b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
2813b659f1f0SPyun YongHyeon 	re_reset(sc);
2814b659f1f0SPyun YongHyeon 
2815a94100faSBill Paul 	/*
28164a814a5eSPyun YongHyeon 	 * For C+ mode, initialize the RX descriptors and mbufs.
28174a814a5eSPyun YongHyeon 	 */
281881eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
281981eee0ebSPyun YongHyeon 		if (ifp->if_mtu > RL_MTU) {
282081eee0ebSPyun YongHyeon 			if (re_jrx_list_init(sc) != 0) {
282181eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
282281eee0ebSPyun YongHyeon 				    "no memory for jumbo RX buffers\n");
282381eee0ebSPyun YongHyeon 				re_stop(sc);
282481eee0ebSPyun YongHyeon 				return;
282581eee0ebSPyun YongHyeon 			}
282681eee0ebSPyun YongHyeon 			/* Disable checksum offloading for jumbo frames. */
282781eee0ebSPyun YongHyeon 			ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
282881eee0ebSPyun YongHyeon 			ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
282981eee0ebSPyun YongHyeon 		} else {
283081eee0ebSPyun YongHyeon 			if (re_rx_list_init(sc) != 0) {
283181eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
283281eee0ebSPyun YongHyeon 				    "no memory for RX buffers\n");
283381eee0ebSPyun YongHyeon 				re_stop(sc);
283481eee0ebSPyun YongHyeon 				return;
283581eee0ebSPyun YongHyeon 			}
283681eee0ebSPyun YongHyeon 		}
283781eee0ebSPyun YongHyeon 		re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
283881eee0ebSPyun YongHyeon 	} else {
28394a814a5eSPyun YongHyeon 		if (re_rx_list_init(sc) != 0) {
28404a814a5eSPyun YongHyeon 			device_printf(sc->rl_dev, "no memory for RX buffers\n");
28414a814a5eSPyun YongHyeon 			re_stop(sc);
28424a814a5eSPyun YongHyeon 			return;
28434a814a5eSPyun YongHyeon 		}
284481eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
284581eee0ebSPyun YongHyeon 		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
284681eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
284781eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 512);
284881eee0ebSPyun YongHyeon 			else
284981eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 4096);
285081eee0ebSPyun YongHyeon 		}
285181eee0ebSPyun YongHyeon 	}
28524a814a5eSPyun YongHyeon 	re_tx_list_init(sc);
28534a814a5eSPyun YongHyeon 
28544a814a5eSPyun YongHyeon 	/*
2855c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2856edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2857c2c6548bSBill Paul 	 * before all others.
2858c2c6548bSBill Paul 	 */
285970acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
286070acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
286170acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
286270acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
286370acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
2864deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
2865deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
2866deb5c680SPyun YongHyeon 		/* XXX magic. */
2867deb5c680SPyun YongHyeon 		cfg |= 0x0001;
2868deb5c680SPyun YongHyeon 	} else
2869deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
2870deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
287181eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
287281eee0ebSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
2873566ca8caSJung-uk Kim 		reg = 0x000fff00;
2874566ca8caSJung-uk Kim 		if ((CSR_READ_1(sc, RL_CFG2) & RL_CFG2_PCI66MHZ) != 0)
2875566ca8caSJung-uk Kim 			reg |= 0x000000ff;
287681eee0ebSPyun YongHyeon 		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
2877566ca8caSJung-uk Kim 			reg |= 0x00f00000;
2878566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
2879566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
2880566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
2881566ca8caSJung-uk Kim 	}
2882ae644087SPyun YongHyeon 	/*
2883ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
2884ae644087SPyun YongHyeon 	 * allowed in controller.
2885ae644087SPyun YongHyeon 	 */
2886ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
2887ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
2888ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
2889ae644087SPyun YongHyeon 	}
2890c2c6548bSBill Paul 
2891c2c6548bSBill Paul 	/*
2892a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2893a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2894a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2895a94100faSBill Paul 	 */
28964d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
28974d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2898a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2899ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2900ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2901ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2902ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2903a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2904a94100faSBill Paul 
2905a94100faSBill Paul 	/*
2906d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2907d01fac16SPyun YongHyeon 	 */
2908d01fac16SPyun YongHyeon 
2909d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2910d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2911d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2912d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2913d01fac16SPyun YongHyeon 
2914d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2915d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2916d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2917d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2918d01fac16SPyun YongHyeon 
2919d01fac16SPyun YongHyeon 	/*
2920a94100faSBill Paul 	 * Enable transmit and receive.
2921a94100faSBill Paul 	 */
2922a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2923a94100faSBill Paul 
2924a94100faSBill Paul 	/*
2925ff191365SJung-uk Kim 	 * Set the initial TX configuration.
2926a94100faSBill Paul 	 */
2927abc8ff44SBill Paul 	if (sc->rl_testmode) {
2928abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2929abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2930abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2931a94100faSBill Paul 		else
2932abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2933abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2934abc8ff44SBill Paul 	} else
2935a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2936d01fac16SPyun YongHyeon 
2937d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2938d01fac16SPyun YongHyeon 
2939a94100faSBill Paul 	/*
2940ff191365SJung-uk Kim 	 * Set the initial RX configuration.
2941a94100faSBill Paul 	 */
2942ff191365SJung-uk Kim 	re_set_rxmode(sc);
2943a94100faSBill Paul 
2944483cc440SPyun YongHyeon 	/* Configure interrupt moderation. */
2945483cc440SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
294681eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
2947483cc440SPyun YongHyeon 		case RL_HWREV_8100E:
2948483cc440SPyun YongHyeon 		case RL_HWREV_8101E:
2949483cc440SPyun YongHyeon 		case RL_HWREV_8102E:
2950483cc440SPyun YongHyeon 		case RL_HWREV_8102EL:
2951483cc440SPyun YongHyeon 		case RL_HWREV_8102EL_SPIN1:
2952483cc440SPyun YongHyeon 		case RL_HWREV_8103E:
2953483cc440SPyun YongHyeon 			CSR_WRITE_2(sc, RL_INTRMOD, 0);
2954483cc440SPyun YongHyeon 			break;
2955483cc440SPyun YongHyeon 		default:
2956483cc440SPyun YongHyeon 			/* Magic from vendor. */
29575e6906eeSPyun YongHyeon 			CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
2958483cc440SPyun YongHyeon 			break;
2959483cc440SPyun YongHyeon 		}
2960483cc440SPyun YongHyeon 	}
2961483cc440SPyun YongHyeon 
2962a94100faSBill Paul #ifdef DEVICE_POLLING
2963a94100faSBill Paul 	/*
2964a94100faSBill Paul 	 * Disable interrupts if we are polling.
2965a94100faSBill Paul 	 */
296640929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2967a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2968a94100faSBill Paul 	else	/* otherwise ... */
296940929967SGleb Smirnoff #endif
2970ed510fb0SBill Paul 
2971a94100faSBill Paul 	/*
2972a94100faSBill Paul 	 * Enable interrupts.
2973a94100faSBill Paul 	 */
2974a94100faSBill Paul 	if (sc->rl_testmode)
2975a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2976a94100faSBill Paul 	else
2977a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2978ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
2979a94100faSBill Paul 
2980a94100faSBill Paul 	/* Set initial TX threshold */
2981a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
2982a94100faSBill Paul 
2983a94100faSBill Paul 	/* Start RX/TX process. */
2984a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
2985a94100faSBill Paul #ifdef notdef
2986a94100faSBill Paul 	/* Enable receiver and transmitter. */
2987a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2988a94100faSBill Paul #endif
2989a94100faSBill Paul 
2990ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2991a94100faSBill Paul 	/*
2992a94100faSBill Paul 	 * Initialize the timer interrupt register so that
2993a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
2994a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
2995a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
2996a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
2997a94100faSBill Paul 	 */
2998a94100faSBill Paul 	if (sc->rl_type == RL_8169)
2999a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3000a94100faSBill Paul 	else
3001a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3002ed510fb0SBill Paul #endif
3003a94100faSBill Paul 
3004a94100faSBill Paul 	/*
3005a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
3006a94100faSBill Paul 	 * size so we can receive jumbo frames.
3007a94100faSBill Paul 	 */
300889feeee4SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
300981eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
301081eee0ebSPyun YongHyeon 			/*
301181eee0ebSPyun YongHyeon 			 * For controllers that use new jumbo frame scheme,
301281eee0ebSPyun YongHyeon 			 * set maximum size of jumbo frame depedning on
301381eee0ebSPyun YongHyeon 			 * controller revisions.
301481eee0ebSPyun YongHyeon 			 */
301581eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
301681eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
301781eee0ebSPyun YongHyeon 				    sc->rl_hwrev->rl_max_mtu +
301881eee0ebSPyun YongHyeon 				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
301981eee0ebSPyun YongHyeon 				    ETHER_CRC_LEN);
302089feeee4SPyun YongHyeon 			else
302181eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
302281eee0ebSPyun YongHyeon 				    RE_RX_DESC_BUFLEN);
302381eee0ebSPyun YongHyeon 		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
302481eee0ebSPyun YongHyeon 		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
302581eee0ebSPyun YongHyeon 			/* RTL810x has no jumbo frame support. */
302681eee0ebSPyun YongHyeon 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
302781eee0ebSPyun YongHyeon 		} else
3028a94100faSBill Paul 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
302989feeee4SPyun YongHyeon 	}
3030a94100faSBill Paul 
303197b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
3032a94100faSBill Paul 		return;
3033a94100faSBill Paul 
3034a94100faSBill Paul 	mii_mediachg(mii);
3035a94100faSBill Paul 
303619ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
3037a94100faSBill Paul 
303813f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
303913f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3040a94100faSBill Paul 
3041351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
30421d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3043d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3044a94100faSBill Paul }
3045a94100faSBill Paul 
3046a94100faSBill Paul /*
3047a94100faSBill Paul  * Set media options.
3048a94100faSBill Paul  */
3049a94100faSBill Paul static int
30507b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
3051a94100faSBill Paul {
3052a94100faSBill Paul 	struct rl_softc		*sc;
3053a94100faSBill Paul 	struct mii_data		*mii;
30546f0f9b12SPyun YongHyeon 	int			error;
3055a94100faSBill Paul 
3056a94100faSBill Paul 	sc = ifp->if_softc;
3057a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3058d1754a9bSJohn Baldwin 	RL_LOCK(sc);
30596f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
3060d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3061a94100faSBill Paul 
30626f0f9b12SPyun YongHyeon 	return (error);
3063a94100faSBill Paul }
3064a94100faSBill Paul 
3065a94100faSBill Paul /*
3066a94100faSBill Paul  * Report current media status.
3067a94100faSBill Paul  */
3068a94100faSBill Paul static void
30697b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3070a94100faSBill Paul {
3071a94100faSBill Paul 	struct rl_softc		*sc;
3072a94100faSBill Paul 	struct mii_data		*mii;
3073a94100faSBill Paul 
3074a94100faSBill Paul 	sc = ifp->if_softc;
3075a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3076a94100faSBill Paul 
3077d1754a9bSJohn Baldwin 	RL_LOCK(sc);
3078a94100faSBill Paul 	mii_pollstat(mii);
3079d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3080a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
3081a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3082a94100faSBill Paul }
3083a94100faSBill Paul 
3084a94100faSBill Paul static int
30857b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3086a94100faSBill Paul {
3087a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
3088a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
3089a94100faSBill Paul 	struct mii_data		*mii;
309040929967SGleb Smirnoff 	int			error = 0;
3091a94100faSBill Paul 
3092a94100faSBill Paul 	switch (command) {
3093a94100faSBill Paul 	case SIOCSIFMTU:
309481eee0ebSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN ||
309581eee0ebSPyun YongHyeon 		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu) {
3096c1d0b573SPyun YongHyeon 			error = EINVAL;
3097c1d0b573SPyun YongHyeon 			break;
3098c1d0b573SPyun YongHyeon 		}
3099c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
310081eee0ebSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
3101a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
310281eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
310381eee0ebSPyun YongHyeon 			    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
310481eee0ebSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
310581eee0ebSPyun YongHyeon 				re_init_locked(sc);
310681eee0ebSPyun YongHyeon 			}
3107ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3108ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
310981eee0ebSPyun YongHyeon 				ifp->if_capenable &= ~(IFCAP_TSO4 |
311081eee0ebSPyun YongHyeon 				    IFCAP_VLAN_HWTSO);
3111ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
311281eee0ebSPyun YongHyeon 			}
3113ecafbbb5SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
3114ae644087SPyun YongHyeon 		}
3115d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
3116a94100faSBill Paul 		break;
3117a94100faSBill Paul 	case SIOCSIFFLAGS:
311897b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
3119eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
3120eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3121eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
31223021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3123ff191365SJung-uk Kim 					re_set_rxmode(sc);
3124eed497bbSPyun YongHyeon 			} else
312597b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
3126eed497bbSPyun YongHyeon 		} else {
3127eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3128a94100faSBill Paul 				re_stop(sc);
3129eed497bbSPyun YongHyeon 		}
3130eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
313197b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3132a94100faSBill Paul 		break;
3133a94100faSBill Paul 	case SIOCADDMULTI:
3134a94100faSBill Paul 	case SIOCDELMULTI:
313597b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
31368476c243SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3137ff191365SJung-uk Kim 			re_set_rxmode(sc);
313897b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3139a94100faSBill Paul 		break;
3140a94100faSBill Paul 	case SIOCGIFMEDIA:
3141a94100faSBill Paul 	case SIOCSIFMEDIA:
3142a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
3143a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3144a94100faSBill Paul 		break;
3145a94100faSBill Paul 	case SIOCSIFCAP:
314640929967SGleb Smirnoff 	    {
3147f051cb85SGleb Smirnoff 		int mask, reinit;
3148f051cb85SGleb Smirnoff 
3149f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3150f051cb85SGleb Smirnoff 		reinit = 0;
315140929967SGleb Smirnoff #ifdef DEVICE_POLLING
315240929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
315340929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
315440929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
315540929967SGleb Smirnoff 				if (error)
315640929967SGleb Smirnoff 					return (error);
3157d1754a9bSJohn Baldwin 				RL_LOCK(sc);
315840929967SGleb Smirnoff 				/* Disable interrupts */
315940929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
316040929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
316140929967SGleb Smirnoff 				RL_UNLOCK(sc);
316240929967SGleb Smirnoff 			} else {
316340929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
316440929967SGleb Smirnoff 				/* Enable interrupts. */
316540929967SGleb Smirnoff 				RL_LOCK(sc);
316640929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
316740929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
316840929967SGleb Smirnoff 				RL_UNLOCK(sc);
316940929967SGleb Smirnoff 			}
317040929967SGleb Smirnoff 		}
317140929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3172d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
3173d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3174d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
3175d3b181aeSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
3176dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
3177a94100faSBill Paul 			else
3178b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3179f051cb85SGleb Smirnoff 			reinit = 1;
318040929967SGleb Smirnoff 		}
3181d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
3182d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3183d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
3184d3b181aeSPyun YongHyeon 			reinit = 1;
3185d3b181aeSPyun YongHyeon 		}
3186ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
3187ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO) != 0) {
3188dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
3189ecafbbb5SPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3190dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
3191dc74159dSPyun YongHyeon 			else
3192dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3193ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3194ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3195ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
3196ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3197ae644087SPyun YongHyeon 			}
3198dc74159dSPyun YongHyeon 		}
3199ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3200ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3201ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3202ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3203ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3204ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3205ecafbbb5SPyun YongHyeon 			/* TSO over VLAN requires VLAN hardware tagging. */
3206ecafbbb5SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3207ecafbbb5SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3208ecafbbb5SPyun YongHyeon 			reinit = 1;
3209ecafbbb5SPyun YongHyeon 		}
321081eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
321181eee0ebSPyun YongHyeon 		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
321281eee0ebSPyun YongHyeon 		    IFCAP_VLAN_HWTSO)) != 0)
321381eee0ebSPyun YongHyeon 				reinit = 1;
32147467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
32157467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
32167467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
32177467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
32187467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
32197467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
32207467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
32217467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
32227467bd53SPyun YongHyeon 		}
32238476c243SPyun YongHyeon 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
32248476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3225f051cb85SGleb Smirnoff 			re_init(sc);
32268476c243SPyun YongHyeon 		}
3227960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
322840929967SGleb Smirnoff 	    }
3229a94100faSBill Paul 		break;
3230a94100faSBill Paul 	default:
3231a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
3232a94100faSBill Paul 		break;
3233a94100faSBill Paul 	}
3234a94100faSBill Paul 
3235a94100faSBill Paul 	return (error);
3236a94100faSBill Paul }
3237a94100faSBill Paul 
3238a94100faSBill Paul static void
32397b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
32401d545c7aSMarius Strobl {
3241130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
3242a94100faSBill Paul 
32431d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
32441d545c7aSMarius Strobl 
32451d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
32461d545c7aSMarius Strobl 		return;
32471d545c7aSMarius Strobl 
3248130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
3249a94100faSBill Paul 	re_txeof(sc);
3250130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3251130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3252130b6dfbSPyun YongHyeon 		    "-- recovering\n");
3253130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3254130b6dfbSPyun YongHyeon 			taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
3255130b6dfbSPyun YongHyeon 		return;
3256130b6dfbSPyun YongHyeon 	}
3257130b6dfbSPyun YongHyeon 
3258130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
3259130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
3260130b6dfbSPyun YongHyeon 
32611abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
32628476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
326397b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
3264130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3265130b6dfbSPyun YongHyeon 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
3266a94100faSBill Paul }
3267a94100faSBill Paul 
3268a94100faSBill Paul /*
3269a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
3270a94100faSBill Paul  * RX and TX lists.
3271a94100faSBill Paul  */
3272a94100faSBill Paul static void
32737b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
3274a94100faSBill Paul {
32750ce0868aSPyun YongHyeon 	int			i;
3276a94100faSBill Paul 	struct ifnet		*ifp;
3277d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
3278d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
3279a94100faSBill Paul 
328097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
328197b9d4baSJohn-Mark Gurney 
3282fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
3283a94100faSBill Paul 
32841d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3285d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
328613f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3287a94100faSBill Paul 
3288ead8fc66SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0)
3289ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3290ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
3291ead8fc66SPyun YongHyeon 	else
3292a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3293ead8fc66SPyun YongHyeon 	DELAY(1000);
3294a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3295ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3296a94100faSBill Paul 
3297a94100faSBill Paul 	if (sc->rl_head != NULL) {
3298a94100faSBill Paul 		m_freem(sc->rl_head);
3299a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
3300a94100faSBill Paul 	}
3301a94100faSBill Paul 
3302a94100faSBill Paul 	/* Free the TX list buffers. */
3303a94100faSBill Paul 
3304d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3305d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
3306d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
3307d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3308d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3309d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3310d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
3311d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
3312d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
3313a94100faSBill Paul 		}
3314a94100faSBill Paul 	}
3315a94100faSBill Paul 
3316a94100faSBill Paul 	/* Free the RX list buffers. */
3317a94100faSBill Paul 
3318d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3319d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
3320d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
3321d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3322d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3323d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3324d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
3325d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
3326d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
3327a94100faSBill Paul 		}
3328a94100faSBill Paul 	}
3329a94100faSBill Paul }
3330a94100faSBill Paul 
3331a94100faSBill Paul /*
3332a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
3333a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
3334a94100faSBill Paul  * resume.
3335a94100faSBill Paul  */
3336a94100faSBill Paul static int
33377b5ffebfSPyun YongHyeon re_suspend(device_t dev)
3338a94100faSBill Paul {
3339a94100faSBill Paul 	struct rl_softc		*sc;
3340a94100faSBill Paul 
3341a94100faSBill Paul 	sc = device_get_softc(dev);
3342a94100faSBill Paul 
334397b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3344a94100faSBill Paul 	re_stop(sc);
33457467bd53SPyun YongHyeon 	re_setwol(sc);
3346a94100faSBill Paul 	sc->suspended = 1;
334797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3348a94100faSBill Paul 
3349a94100faSBill Paul 	return (0);
3350a94100faSBill Paul }
3351a94100faSBill Paul 
3352a94100faSBill Paul /*
3353a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
3354a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
3355a94100faSBill Paul  * appropriate.
3356a94100faSBill Paul  */
3357a94100faSBill Paul static int
33587b5ffebfSPyun YongHyeon re_resume(device_t dev)
3359a94100faSBill Paul {
3360a94100faSBill Paul 	struct rl_softc		*sc;
3361a94100faSBill Paul 	struct ifnet		*ifp;
3362a94100faSBill Paul 
3363a94100faSBill Paul 	sc = device_get_softc(dev);
336497b9d4baSJohn-Mark Gurney 
336597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
336697b9d4baSJohn-Mark Gurney 
3367fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
336861f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
336961f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
337061f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
337161f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
337261f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
337361f45a72SPyun YongHyeon 	}
3374a94100faSBill Paul 
33757467bd53SPyun YongHyeon 	/*
33767467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
33777467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
33787467bd53SPyun YongHyeon 	 */
33797467bd53SPyun YongHyeon 	re_clrwol(sc);
338001d1a6c3SPyun YongHyeon 
338101d1a6c3SPyun YongHyeon 	/* reinitialize interface if necessary */
338201d1a6c3SPyun YongHyeon 	if (ifp->if_flags & IFF_UP)
338301d1a6c3SPyun YongHyeon 		re_init_locked(sc);
338401d1a6c3SPyun YongHyeon 
3385a94100faSBill Paul 	sc->suspended = 0;
338697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3387a94100faSBill Paul 
3388a94100faSBill Paul 	return (0);
3389a94100faSBill Paul }
3390a94100faSBill Paul 
3391a94100faSBill Paul /*
3392a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3393a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3394a94100faSBill Paul  */
33956a087a87SPyun YongHyeon static int
33967b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3397a94100faSBill Paul {
3398a94100faSBill Paul 	struct rl_softc		*sc;
3399a94100faSBill Paul 
3400a94100faSBill Paul 	sc = device_get_softc(dev);
3401a94100faSBill Paul 
340297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3403a94100faSBill Paul 	re_stop(sc);
3404536fde34SMaxim Sobolev 	/*
3405536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3406536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
340772293673SRuslan Ermilov 	 * cases.
3408536fde34SMaxim Sobolev 	 */
3409536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
34107467bd53SPyun YongHyeon 	re_setwol(sc);
341197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
34126a087a87SPyun YongHyeon 
34136a087a87SPyun YongHyeon 	return (0);
3414a94100faSBill Paul }
34157467bd53SPyun YongHyeon 
34167467bd53SPyun YongHyeon static void
34177b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
34187467bd53SPyun YongHyeon {
34197467bd53SPyun YongHyeon 	struct ifnet		*ifp;
34207467bd53SPyun YongHyeon 	int			pmc;
34217467bd53SPyun YongHyeon 	uint16_t		pmstat;
34227467bd53SPyun YongHyeon 	uint8_t			v;
34237467bd53SPyun YongHyeon 
34247467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
34257467bd53SPyun YongHyeon 
34267467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
34277467bd53SPyun YongHyeon 		return;
34287467bd53SPyun YongHyeon 
34297467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
343061f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
343161f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
343261f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
343361f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
343461f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
343561f45a72SPyun YongHyeon 	}
3436886ff602SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3437886ff602SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3438886ff602SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
34397467bd53SPyun YongHyeon 	/* Enable config register write. */
34407467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
34417467bd53SPyun YongHyeon 
34427467bd53SPyun YongHyeon 	/* Enable PME. */
34437467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
34447467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
34457467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
34467467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
34477467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
34487467bd53SPyun YongHyeon 
34497467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
34507467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
34517467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
34527467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
34537467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
34547467bd53SPyun YongHyeon 
34557467bd53SPyun YongHyeon 	/* Config register write done. */
3456f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
34577467bd53SPyun YongHyeon 
34587467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
34597467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
34607467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
34617467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
34627467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
34637467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
34647467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
34657467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
34667467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
34677467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
34687467bd53SPyun YongHyeon 
3469d0c45156SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3470d0c45156SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3471d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
34727467bd53SPyun YongHyeon 	/*
34737467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
34747467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
34757467bd53SPyun YongHyeon 	 * needed.
34767467bd53SPyun YongHyeon 	 */
34777467bd53SPyun YongHyeon 
34787467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
34797467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
34807467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
34817467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
34827467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
34837467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
34847467bd53SPyun YongHyeon }
34857467bd53SPyun YongHyeon 
34867467bd53SPyun YongHyeon static void
34877b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
34887467bd53SPyun YongHyeon {
34897467bd53SPyun YongHyeon 	int			pmc;
34907467bd53SPyun YongHyeon 	uint8_t			v;
34917467bd53SPyun YongHyeon 
34927467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
34937467bd53SPyun YongHyeon 
34947467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
34957467bd53SPyun YongHyeon 		return;
34967467bd53SPyun YongHyeon 
34977467bd53SPyun YongHyeon 	/* Enable config register write. */
34987467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
34997467bd53SPyun YongHyeon 
35007467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
35017467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
35027467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
35037467bd53SPyun YongHyeon 
35047467bd53SPyun YongHyeon 	/* Config register write done. */
3505f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
35067467bd53SPyun YongHyeon 
35077467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
35087467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
35097467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
35107467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
35117467bd53SPyun YongHyeon }
35120534aae0SPyun YongHyeon 
35130534aae0SPyun YongHyeon static void
35140534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc)
35150534aae0SPyun YongHyeon {
35160534aae0SPyun YongHyeon 	struct sysctl_ctx_list	*ctx;
35170534aae0SPyun YongHyeon 	struct sysctl_oid_list	*children;
35180534aae0SPyun YongHyeon 
35190534aae0SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->rl_dev);
35200534aae0SPyun YongHyeon 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
35210534aae0SPyun YongHyeon 
35220534aae0SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
35230534aae0SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I",
35240534aae0SPyun YongHyeon 	    "Statistics Information");
35250534aae0SPyun YongHyeon }
35260534aae0SPyun YongHyeon 
35270534aae0SPyun YongHyeon static int
35280534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS)
35290534aae0SPyun YongHyeon {
35300534aae0SPyun YongHyeon 	struct rl_softc		*sc;
35310534aae0SPyun YongHyeon 	struct rl_stats		*stats;
35320534aae0SPyun YongHyeon 	int			error, i, result;
35330534aae0SPyun YongHyeon 
35340534aae0SPyun YongHyeon 	result = -1;
35350534aae0SPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
35360534aae0SPyun YongHyeon 	if (error || req->newptr == NULL)
35370534aae0SPyun YongHyeon 		return (error);
35380534aae0SPyun YongHyeon 
35390534aae0SPyun YongHyeon 	if (result == 1) {
35400534aae0SPyun YongHyeon 		sc = (struct rl_softc *)arg1;
35410534aae0SPyun YongHyeon 		RL_LOCK(sc);
354216a4824bSPyun YongHyeon 		if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
354316a4824bSPyun YongHyeon 			RL_UNLOCK(sc);
354416a4824bSPyun YongHyeon 			goto done;
354516a4824bSPyun YongHyeon 		}
35460534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
35470534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
35480534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
35490534aae0SPyun YongHyeon 		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
35500534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
35510534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
35520534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
35530534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
35540534aae0SPyun YongHyeon 		    RL_DUMPSTATS_START));
35550534aae0SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
35560534aae0SPyun YongHyeon 			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
35570534aae0SPyun YongHyeon 			    RL_DUMPSTATS_START) == 0)
35580534aae0SPyun YongHyeon 				break;
35590534aae0SPyun YongHyeon 			DELAY(1000);
35600534aae0SPyun YongHyeon 		}
35610534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
35620534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
35630534aae0SPyun YongHyeon 		RL_UNLOCK(sc);
35640534aae0SPyun YongHyeon 		if (i == 0) {
35650534aae0SPyun YongHyeon 			device_printf(sc->rl_dev,
35660534aae0SPyun YongHyeon 			    "DUMP statistics request timedout\n");
35670534aae0SPyun YongHyeon 			return (ETIMEDOUT);
35680534aae0SPyun YongHyeon 		}
356916a4824bSPyun YongHyeon done:
35700534aae0SPyun YongHyeon 		stats = sc->rl_ldata.rl_stats;
35710534aae0SPyun YongHyeon 		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
35720534aae0SPyun YongHyeon 		printf("Tx frames : %ju\n",
35730534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_pkts));
35740534aae0SPyun YongHyeon 		printf("Rx frames : %ju\n",
35750534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_pkts));
35760534aae0SPyun YongHyeon 		printf("Tx errors : %ju\n",
35770534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_errs));
35780534aae0SPyun YongHyeon 		printf("Rx errors : %u\n",
35790534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_errs));
35800534aae0SPyun YongHyeon 		printf("Rx missed frames : %u\n",
35810534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_missed_pkts));
35820534aae0SPyun YongHyeon 		printf("Rx frame alignment errs : %u\n",
35830534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
35840534aae0SPyun YongHyeon 		printf("Tx single collisions : %u\n",
35850534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_onecoll));
35860534aae0SPyun YongHyeon 		printf("Tx multiple collisions : %u\n",
35870534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_multicolls));
35880534aae0SPyun YongHyeon 		printf("Rx unicast frames : %ju\n",
35890534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
35900534aae0SPyun YongHyeon 		printf("Rx broadcast frames : %ju\n",
35910534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
35920534aae0SPyun YongHyeon 		printf("Rx multicast frames : %u\n",
35930534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_mcasts));
35940534aae0SPyun YongHyeon 		printf("Tx aborts : %u\n",
35950534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_tx_aborts));
35960534aae0SPyun YongHyeon 		printf("Tx underruns : %u\n",
35970534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_underruns));
35980534aae0SPyun YongHyeon 	}
35990534aae0SPyun YongHyeon 
36000534aae0SPyun YongHyeon 	return (error);
36010534aae0SPyun YongHyeon }
3602