xref: /freebsd/sys/dev/re/if_re.c (revision 4a58fd452a4b07bf02f7b94010f6637dbed9804b)
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);
162*4a58fd45SPyun YongHyeon static int msix_disable = 0;
163*4a58fd45SPyun YongHyeon TUNABLE_INT("hw.re.msix_disable", &msix_disable);
1642c21710bSPyun YongHyeon static int prefer_iomap = 0;
1652c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
1665774c5ffSPyun YongHyeon 
167a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
168a94100faSBill Paul 
169a94100faSBill Paul /*
170a94100faSBill Paul  * Various supported device vendors/types and their names.
171a94100faSBill Paul  */
172a94100faSBill Paul static struct rl_type re_devs[] = {
1739dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
17432aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
1759dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
176a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1779dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
1788281a098SPyun YongHyeon 	    "RealTek 8101E/8102E/8102EL/8103E PCIe 10/100baseTX" },
1799dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
180d0c45156SPyun YongHyeon 	    "RealTek 8168/8111 B/C/CP/D/DP/E PCIe Gigabit Ethernet" },
1819dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
182715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1839dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1842ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1859dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
186ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1879dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
18826390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1899dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
190dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
191a94100faSBill Paul };
192a94100faSBill Paul 
193a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
19481eee0ebSPyun YongHyeon 	{ RL_HWREV_8139, RL_8139,  "", RL_MTU },
19581eee0ebSPyun YongHyeon 	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
19681eee0ebSPyun YongHyeon 	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
19781eee0ebSPyun YongHyeon 	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
19881eee0ebSPyun YongHyeon 	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
19981eee0ebSPyun YongHyeon 	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
20081eee0ebSPyun YongHyeon 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
20181eee0ebSPyun YongHyeon 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
202ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
20381eee0ebSPyun YongHyeon 	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
20481eee0ebSPyun YongHyeon 	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
20581eee0ebSPyun YongHyeon 	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
20681eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
20781eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
20881eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
20981eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21081eee0ebSPyun YongHyeon 	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
21181eee0ebSPyun YongHyeon 	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
21281eee0ebSPyun YongHyeon 	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
21381eee0ebSPyun YongHyeon 	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
21481eee0ebSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
21581eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
21681eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
21781eee0ebSPyun YongHyeon 	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
218ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
219ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
22081eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
22181eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
22281eee0ebSPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
22381eee0ebSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
22481eee0ebSPyun YongHyeon 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
22581eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
22681eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
22781eee0ebSPyun YongHyeon 	{ 0, 0, NULL, 0 }
228a94100faSBill Paul };
229a94100faSBill Paul 
230a94100faSBill Paul static int re_probe		(device_t);
231a94100faSBill Paul static int re_attach		(device_t);
232a94100faSBill Paul static int re_detach		(device_t);
233a94100faSBill Paul 
234d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
235a94100faSBill Paul 
236a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
237a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
238d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
239d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
240d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
24181eee0ebSPyun YongHyeon static int re_jumbo_newbuf	(struct rl_softc *, int);
242a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
24381eee0ebSPyun YongHyeon static int re_jrx_list_init	(struct rl_softc *);
244a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
24522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
24622a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
24722a11c96SJohn-Mark Gurney 				(struct mbuf *);
24822a11c96SJohn-Mark Gurney #endif
2491abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
250a94100faSBill Paul static void re_txeof		(struct rl_softc *);
25197b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2521abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2531abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
25497b9d4baSJohn-Mark Gurney #endif
255ef544f63SPaolo Pisati static int re_intr		(void *);
256a94100faSBill Paul static void re_tick		(void *);
257ed510fb0SBill Paul static void re_tx_task		(void *, int);
258ed510fb0SBill Paul static void re_int_task		(void *, int);
259a94100faSBill Paul static void re_start		(struct ifnet *);
260a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
261a94100faSBill Paul static void re_init		(void *);
26297b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
263a94100faSBill Paul static void re_stop		(struct rl_softc *);
2641d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
265a94100faSBill Paul static int re_suspend		(device_t);
266a94100faSBill Paul static int re_resume		(device_t);
2676a087a87SPyun YongHyeon static int re_shutdown		(device_t);
268a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
269a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
270a94100faSBill Paul 
271a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
272a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
273ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
274a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
275a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
276a94100faSBill Paul 
277a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
278a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
279a94100faSBill Paul static void re_miibus_statchg	(device_t);
280a94100faSBill Paul 
28181eee0ebSPyun YongHyeon static void re_set_jumbo	(struct rl_softc *, int);
282ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
283a94100faSBill Paul static void re_reset		(struct rl_softc *);
2847467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2857467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
286a94100faSBill Paul 
287ed510fb0SBill Paul #ifdef RE_DIAG
288a94100faSBill Paul static int re_diag		(struct rl_softc *);
289ed510fb0SBill Paul #endif
290a94100faSBill Paul 
2910534aae0SPyun YongHyeon static void re_add_sysctls	(struct rl_softc *);
2920534aae0SPyun YongHyeon static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
2930534aae0SPyun YongHyeon 
294a94100faSBill Paul static device_method_t re_methods[] = {
295a94100faSBill Paul 	/* Device interface */
296a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
297a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
298a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
299a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
300a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
301a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
302a94100faSBill Paul 
303a94100faSBill Paul 	/* bus interface */
304a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
305a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
306a94100faSBill Paul 
307a94100faSBill Paul 	/* MII interface */
308a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
309a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
310a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
311a94100faSBill Paul 
312a94100faSBill Paul 	{ 0, 0 }
313a94100faSBill Paul };
314a94100faSBill Paul 
315a94100faSBill Paul static driver_t re_driver = {
316a94100faSBill Paul 	"re",
317a94100faSBill Paul 	re_methods,
318a94100faSBill Paul 	sizeof(struct rl_softc)
319a94100faSBill Paul };
320a94100faSBill Paul 
321a94100faSBill Paul static devclass_t re_devclass;
322a94100faSBill Paul 
323a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
324a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
325a94100faSBill Paul 
326a94100faSBill Paul #define EE_SET(x)					\
327a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
328a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
329a94100faSBill Paul 
330a94100faSBill Paul #define EE_CLR(x)					\
331a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
332a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
333a94100faSBill Paul 
334a94100faSBill Paul /*
335a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
336a94100faSBill Paul  */
337a94100faSBill Paul static void
3387b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
339a94100faSBill Paul {
3400ce0868aSPyun YongHyeon 	int			d, i;
341a94100faSBill Paul 
342ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
343a94100faSBill Paul 
344a94100faSBill Paul 	/*
345a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
346a94100faSBill Paul 	 */
347ed510fb0SBill Paul 
348ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
349a94100faSBill Paul 		if (d & i) {
350a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
351a94100faSBill Paul 		} else {
352a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
353a94100faSBill Paul 		}
354a94100faSBill Paul 		DELAY(100);
355a94100faSBill Paul 		EE_SET(RL_EE_CLK);
356a94100faSBill Paul 		DELAY(150);
357a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
358a94100faSBill Paul 		DELAY(100);
359a94100faSBill Paul 	}
360a94100faSBill Paul }
361a94100faSBill Paul 
362a94100faSBill Paul /*
363a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
364a94100faSBill Paul  */
365a94100faSBill Paul static void
3667b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
367a94100faSBill Paul {
3680ce0868aSPyun YongHyeon 	int			i;
369a94100faSBill Paul 	u_int16_t		word = 0;
370a94100faSBill Paul 
371a94100faSBill Paul 	/*
372a94100faSBill Paul 	 * Send address of word we want to read.
373a94100faSBill Paul 	 */
374a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
375a94100faSBill Paul 
376a94100faSBill Paul 	/*
377a94100faSBill Paul 	 * Start reading bits from EEPROM.
378a94100faSBill Paul 	 */
379a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
380a94100faSBill Paul 		EE_SET(RL_EE_CLK);
381a94100faSBill Paul 		DELAY(100);
382a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
383a94100faSBill Paul 			word |= i;
384a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
385a94100faSBill Paul 		DELAY(100);
386a94100faSBill Paul 	}
387a94100faSBill Paul 
388a94100faSBill Paul 	*dest = word;
389a94100faSBill Paul }
390a94100faSBill Paul 
391a94100faSBill Paul /*
392a94100faSBill Paul  * Read a sequence of words from the EEPROM.
393a94100faSBill Paul  */
394a94100faSBill Paul static void
3957b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
396a94100faSBill Paul {
397a94100faSBill Paul 	int			i;
398a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
399a94100faSBill Paul 
400ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
401ed510fb0SBill Paul 
402ed510fb0SBill Paul         DELAY(100);
403ed510fb0SBill Paul 
404a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
405ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
406a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
407ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
408a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
409be099007SPyun YongHyeon                 *ptr = word;
410a94100faSBill Paul 	}
411ed510fb0SBill Paul 
412ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
413a94100faSBill Paul }
414a94100faSBill Paul 
415a94100faSBill Paul static int
4167b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
417a94100faSBill Paul {
418a94100faSBill Paul 	struct rl_softc		*sc;
419a94100faSBill Paul 	u_int32_t		rval;
420a94100faSBill Paul 	int			i;
421a94100faSBill Paul 
422a94100faSBill Paul 	sc = device_get_softc(dev);
423a94100faSBill Paul 
4249bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4259bac70b8SBill Paul 
4269bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4279bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4289bac70b8SBill Paul 		return (rval);
4299bac70b8SBill Paul 	}
4309bac70b8SBill Paul 
431a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
432a94100faSBill Paul 
43396b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
434a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
435a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
436a94100faSBill Paul 			break;
4372bc085c6SPyun YongHyeon 		DELAY(25);
438a94100faSBill Paul 	}
439a94100faSBill Paul 
44096b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4416b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
442a94100faSBill Paul 		return (0);
443a94100faSBill Paul 	}
444a94100faSBill Paul 
4452bc085c6SPyun YongHyeon 	/*
4462bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4472bc085c6SPyun YongHyeon 	 */
4482bc085c6SPyun YongHyeon 	DELAY(20);
4492bc085c6SPyun YongHyeon 
450a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
451a94100faSBill Paul }
452a94100faSBill Paul 
453a94100faSBill Paul static int
4547b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
455a94100faSBill Paul {
456a94100faSBill Paul 	struct rl_softc		*sc;
457a94100faSBill Paul 	u_int32_t		rval;
458a94100faSBill Paul 	int			i;
459a94100faSBill Paul 
460a94100faSBill Paul 	sc = device_get_softc(dev);
461a94100faSBill Paul 
462a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4639bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
464a94100faSBill Paul 
46596b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
466a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
467a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
468a94100faSBill Paul 			break;
4692bc085c6SPyun YongHyeon 		DELAY(25);
470a94100faSBill Paul 	}
471a94100faSBill Paul 
47296b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4736b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
474a94100faSBill Paul 		return (0);
475a94100faSBill Paul 	}
476a94100faSBill Paul 
4772bc085c6SPyun YongHyeon 	/*
4782bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4792bc085c6SPyun YongHyeon 	 */
4802bc085c6SPyun YongHyeon 	DELAY(20);
4812bc085c6SPyun YongHyeon 
482a94100faSBill Paul 	return (0);
483a94100faSBill Paul }
484a94100faSBill Paul 
485a94100faSBill Paul static int
4867b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
487a94100faSBill Paul {
488a94100faSBill Paul 	struct rl_softc		*sc;
489a94100faSBill Paul 	u_int16_t		rval = 0;
490a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
491a94100faSBill Paul 
492a94100faSBill Paul 	sc = device_get_softc(dev);
493a94100faSBill Paul 
494a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
495a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
496a94100faSBill Paul 		return (rval);
497a94100faSBill Paul 	}
498a94100faSBill Paul 
499a94100faSBill Paul 	switch (reg) {
500a94100faSBill Paul 	case MII_BMCR:
501a94100faSBill Paul 		re8139_reg = RL_BMCR;
502a94100faSBill Paul 		break;
503a94100faSBill Paul 	case MII_BMSR:
504a94100faSBill Paul 		re8139_reg = RL_BMSR;
505a94100faSBill Paul 		break;
506a94100faSBill Paul 	case MII_ANAR:
507a94100faSBill Paul 		re8139_reg = RL_ANAR;
508a94100faSBill Paul 		break;
509a94100faSBill Paul 	case MII_ANER:
510a94100faSBill Paul 		re8139_reg = RL_ANER;
511a94100faSBill Paul 		break;
512a94100faSBill Paul 	case MII_ANLPAR:
513a94100faSBill Paul 		re8139_reg = RL_LPAR;
514a94100faSBill Paul 		break;
515a94100faSBill Paul 	case MII_PHYIDR1:
516a94100faSBill Paul 	case MII_PHYIDR2:
517a94100faSBill Paul 		return (0);
518a94100faSBill Paul 	/*
519a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
520a94100faSBill Paul 	 * register. If we have a link partner which does not
521a94100faSBill Paul 	 * support NWAY, this is the register which will tell
522a94100faSBill Paul 	 * us the results of parallel detection.
523a94100faSBill Paul 	 */
524a94100faSBill Paul 	case RL_MEDIASTAT:
525a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
526a94100faSBill Paul 		return (rval);
527a94100faSBill Paul 	default:
5286b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
529a94100faSBill Paul 		return (0);
530a94100faSBill Paul 	}
531a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
532baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
533baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
534baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
535baa12772SPyun YongHyeon 	}
536a94100faSBill Paul 	return (rval);
537a94100faSBill Paul }
538a94100faSBill Paul 
539a94100faSBill Paul static int
5407b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
541a94100faSBill Paul {
542a94100faSBill Paul 	struct rl_softc		*sc;
543a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
544a94100faSBill Paul 	int			rval = 0;
545a94100faSBill Paul 
546a94100faSBill Paul 	sc = device_get_softc(dev);
547a94100faSBill Paul 
548a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
549a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
550a94100faSBill Paul 		return (rval);
551a94100faSBill Paul 	}
552a94100faSBill Paul 
553a94100faSBill Paul 	switch (reg) {
554a94100faSBill Paul 	case MII_BMCR:
555a94100faSBill Paul 		re8139_reg = RL_BMCR;
556baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
557baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
558baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
559baa12772SPyun YongHyeon 		}
560a94100faSBill Paul 		break;
561a94100faSBill Paul 	case MII_BMSR:
562a94100faSBill Paul 		re8139_reg = RL_BMSR;
563a94100faSBill Paul 		break;
564a94100faSBill Paul 	case MII_ANAR:
565a94100faSBill Paul 		re8139_reg = RL_ANAR;
566a94100faSBill Paul 		break;
567a94100faSBill Paul 	case MII_ANER:
568a94100faSBill Paul 		re8139_reg = RL_ANER;
569a94100faSBill Paul 		break;
570a94100faSBill Paul 	case MII_ANLPAR:
571a94100faSBill Paul 		re8139_reg = RL_LPAR;
572a94100faSBill Paul 		break;
573a94100faSBill Paul 	case MII_PHYIDR1:
574a94100faSBill Paul 	case MII_PHYIDR2:
575a94100faSBill Paul 		return (0);
576a94100faSBill Paul 		break;
577a94100faSBill Paul 	default:
5786b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
579a94100faSBill Paul 		return (0);
580a94100faSBill Paul 	}
581a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
582a94100faSBill Paul 	return (0);
583a94100faSBill Paul }
584a94100faSBill Paul 
585a94100faSBill Paul static void
5867b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
587a94100faSBill Paul {
588130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
589130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
590130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
591a11e2f18SBruce M Simpson 
592130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
593130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
594130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
595130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
596130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
597130b6dfbSPyun YongHyeon 		return;
598130b6dfbSPyun YongHyeon 
599130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
600130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
601130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
602130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
603130b6dfbSPyun YongHyeon 		case IFM_10_T:
604130b6dfbSPyun YongHyeon 		case IFM_100_TX:
605130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
606130b6dfbSPyun YongHyeon 			break;
607130b6dfbSPyun YongHyeon 		case IFM_1000_T:
608130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
609130b6dfbSPyun YongHyeon 				break;
610130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
611130b6dfbSPyun YongHyeon 			break;
612130b6dfbSPyun YongHyeon 		default:
613130b6dfbSPyun YongHyeon 			break;
614130b6dfbSPyun YongHyeon 		}
615130b6dfbSPyun YongHyeon 	}
616130b6dfbSPyun YongHyeon 	/*
617130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
618130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
619130b6dfbSPyun YongHyeon 	 * parameters.
620130b6dfbSPyun YongHyeon 	 */
621a94100faSBill Paul }
622a94100faSBill Paul 
623a94100faSBill Paul /*
624ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
625a94100faSBill Paul  */
626a94100faSBill Paul static void
627ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
628a94100faSBill Paul {
629a94100faSBill Paul 	struct ifnet		*ifp;
630a94100faSBill Paul 	struct ifmultiaddr	*ifma;
631ff191365SJung-uk Kim 	uint32_t		hashes[2] = { 0, 0 };
632ff191365SJung-uk Kim 	uint32_t		h, rxfilt;
633a94100faSBill Paul 
63497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
63597b9d4baSJohn-Mark Gurney 
636fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
637a94100faSBill Paul 
638ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
639a94100faSBill Paul 
640ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6417c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6427c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
643a0637caaSPyun YongHyeon 		/*
644a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
645a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
646a0637caaSPyun YongHyeon 		 * promiscuous mode.
647a0637caaSPyun YongHyeon 		 */
648a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
649ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
650ff191365SJung-uk Kim 		goto done;
651a94100faSBill Paul 	}
652a94100faSBill Paul 
653eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
654a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
655a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
656a94100faSBill Paul 			continue;
6570e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6580e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
659a94100faSBill Paul 		if (h < 32)
660a94100faSBill Paul 			hashes[0] |= (1 << h);
661a94100faSBill Paul 		else
662a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
663a94100faSBill Paul 	}
664eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
665a94100faSBill Paul 
666ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
667bb7dfefbSBill Paul 		/*
668ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
669ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
670ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
671ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
672ff191365SJung-uk Kim 		 * devices.
673bb7dfefbSBill Paul 		 */
674aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
675ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
676ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
677ff191365SJung-uk Kim 			hashes[1] = h;
678ff191365SJung-uk Kim 		}
679ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
680ff191365SJung-uk Kim 	}
681ff191365SJung-uk Kim 
682ff191365SJung-uk Kim done:
683a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
684a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
685ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
686bb7dfefbSBill Paul }
687a94100faSBill Paul 
688a94100faSBill Paul static void
6897b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
690a94100faSBill Paul {
6910ce0868aSPyun YongHyeon 	int			i;
692a94100faSBill Paul 
69397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
69497b9d4baSJohn-Mark Gurney 
695a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
696a94100faSBill Paul 
697a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
698a94100faSBill Paul 		DELAY(10);
699a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
700a94100faSBill Paul 			break;
701a94100faSBill Paul 	}
702a94100faSBill Paul 	if (i == RL_TIMEOUT)
7036b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
704a94100faSBill Paul 
705566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
706a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
70781eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
708566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
709a94100faSBill Paul }
710a94100faSBill Paul 
711ed510fb0SBill Paul #ifdef RE_DIAG
712ed510fb0SBill Paul 
713a94100faSBill Paul /*
714a94100faSBill Paul  * The following routine is designed to test for a defect on some
715a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
716a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
717a94100faSBill Paul  * should be pulled high. The result of this defect is that the
718a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
719a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
720a94100faSBill Paul  * because the 64-bit data lines aren't connected.
721a94100faSBill Paul  *
722a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
723a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
724a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
725a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
726a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
727a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
728a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
729a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
730a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
731a94100faSBill Paul  */
732a94100faSBill Paul 
733a94100faSBill Paul static int
7347b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
735a94100faSBill Paul {
736fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
737a94100faSBill Paul 	struct mbuf		*m0;
738a94100faSBill Paul 	struct ether_header	*eh;
739a94100faSBill Paul 	struct rl_desc		*cur_rx;
740a94100faSBill Paul 	u_int16_t		status;
741a94100faSBill Paul 	u_int32_t		rxstat;
742ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
743a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
744a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
745a94100faSBill Paul 
746a94100faSBill Paul 	/* Allocate a single mbuf */
747a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
748a94100faSBill Paul 	if (m0 == NULL)
749a94100faSBill Paul 		return (ENOBUFS);
750a94100faSBill Paul 
75197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
75297b9d4baSJohn-Mark Gurney 
753a94100faSBill Paul 	/*
754a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
755a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
756a94100faSBill Paul 	 * following special functions:
757a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
758a94100faSBill Paul 	 * - Enables digital loopback mode
759a94100faSBill Paul 	 * - Leaves interrupts turned off
760a94100faSBill Paul 	 */
761a94100faSBill Paul 
762a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
763a94100faSBill Paul 	sc->rl_testmode = 1;
7648476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
76597b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
766351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
767ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
768ed510fb0SBill Paul 		phyaddr = 1;
769ed510fb0SBill Paul 	else
770ed510fb0SBill Paul 		phyaddr = 0;
771ed510fb0SBill Paul 
772ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
773ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
774ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
775ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
776ed510fb0SBill Paul 			break;
777ed510fb0SBill Paul 	}
778ed510fb0SBill Paul 
779ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
780ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
781ed510fb0SBill Paul 
782804af9a1SBill Paul 	DELAY(100000);
783a94100faSBill Paul 
784a94100faSBill Paul 	/* Put some data in the mbuf */
785a94100faSBill Paul 
786a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
787a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
788a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
789a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
790a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
791a94100faSBill Paul 
7927cae6651SBill Paul 	/*
7937cae6651SBill Paul 	 * Queue the packet, start transmission.
7947cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
7957cae6651SBill Paul 	 */
796a94100faSBill Paul 
797abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
79897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
79952732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8007cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
80197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
802a94100faSBill Paul 	m0 = NULL;
803a94100faSBill Paul 
804a94100faSBill Paul 	/* Wait for it to propagate through the chip */
805a94100faSBill Paul 
806abc8ff44SBill Paul 	DELAY(100000);
807a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
808a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
809ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
810abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
811abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
812a94100faSBill Paul 			break;
813a94100faSBill Paul 		DELAY(10);
814a94100faSBill Paul 	}
815a94100faSBill Paul 
816a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8176b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8186b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8196b9f5c94SGleb Smirnoff 		    " loopback mode\n");
820a94100faSBill Paul 		error = EIO;
821a94100faSBill Paul 		goto done;
822a94100faSBill Paul 	}
823a94100faSBill Paul 
824a94100faSBill Paul 	/*
825a94100faSBill Paul 	 * The packet should have been dumped into the first
826a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
827a94100faSBill Paul 	 */
828a94100faSBill Paul 
829a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
830a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
831a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
832d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
833d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
834d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
835d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
836d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
837a94100faSBill Paul 
838d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
839d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
840a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
841a94100faSBill Paul 
842a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
843a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
844a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
845a94100faSBill Paul 
846a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8476b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8486b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
849a94100faSBill Paul 		error = EIO;
850a94100faSBill Paul 		goto done;
851a94100faSBill Paul 	}
852a94100faSBill Paul 
853a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
854a94100faSBill Paul 
855a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
856a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
857a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8586b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8596b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
860a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8616b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
862a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
863a94100faSBill Paul 		    ntohs(eh->ether_type));
8646b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8656b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8666b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8676b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8686b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8696b9f5c94SGleb Smirnoff 		    "details.\n");
870a94100faSBill Paul 		error = EIO;
871a94100faSBill Paul 	}
872a94100faSBill Paul 
873a94100faSBill Paul done:
874a94100faSBill Paul 	/* Turn interface off, release resources */
875a94100faSBill Paul 
876a94100faSBill Paul 	sc->rl_testmode = 0;
877351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
878a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
879a94100faSBill Paul 	re_stop(sc);
880a94100faSBill Paul 	if (m0 != NULL)
881a94100faSBill Paul 		m_freem(m0);
882a94100faSBill Paul 
88397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
88497b9d4baSJohn-Mark Gurney 
885a94100faSBill Paul 	return (error);
886a94100faSBill Paul }
887a94100faSBill Paul 
888ed510fb0SBill Paul #endif
889ed510fb0SBill Paul 
890a94100faSBill Paul /*
891a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
892a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
893a94100faSBill Paul  */
894a94100faSBill Paul static int
8957b5ffebfSPyun YongHyeon re_probe(device_t dev)
896a94100faSBill Paul {
897a94100faSBill Paul 	struct rl_type		*t;
898dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
899dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
900dfdb409eSPyun YongHyeon 	int			i;
901a94100faSBill Paul 
902dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
903dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
904dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
905dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
906a94100faSBill Paul 
907dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
908dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
90926390635SJohn Baldwin 			/*
91026390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
911dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
91226390635SJohn Baldwin 			 */
913a94100faSBill Paul 			return (ENXIO);
914a94100faSBill Paul 		}
915dfdb409eSPyun YongHyeon 	}
916dfdb409eSPyun YongHyeon 
917dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
918dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
919dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
920dfdb409eSPyun YongHyeon 			return (ENXIO);
921dfdb409eSPyun YongHyeon 		}
922dfdb409eSPyun YongHyeon 	}
923dfdb409eSPyun YongHyeon 
924dfdb409eSPyun YongHyeon 	t = re_devs;
925dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
926dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
927a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
928d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
929a94100faSBill Paul 		}
930a94100faSBill Paul 	}
931a94100faSBill Paul 
932a94100faSBill Paul 	return (ENXIO);
933a94100faSBill Paul }
934a94100faSBill Paul 
935a94100faSBill Paul /*
936a94100faSBill Paul  * Map a single buffer address.
937a94100faSBill Paul  */
938a94100faSBill Paul 
939a94100faSBill Paul static void
9407b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
941a94100faSBill Paul {
9428fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
943a94100faSBill Paul 
944a94100faSBill Paul 	if (error)
945a94100faSBill Paul 		return;
946a94100faSBill Paul 
947a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
948a94100faSBill Paul 	addr = arg;
949a94100faSBill Paul 	*addr = segs->ds_addr;
950a94100faSBill Paul }
951a94100faSBill Paul 
952a94100faSBill Paul static int
9537b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
954a94100faSBill Paul {
95566366ca4SPyun YongHyeon 	bus_addr_t		lowaddr;
956d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
957a94100faSBill Paul 	int			error;
958a94100faSBill Paul 	int			i;
959a94100faSBill Paul 
960d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
961d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
962d65abd66SPyun YongHyeon 
963d65abd66SPyun YongHyeon 	/*
964d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
965ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
966ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
967ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
968ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
96966366ca4SPyun YongHyeon 	 * may not have the limitation.
970d65abd66SPyun YongHyeon 	 */
97166366ca4SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
97266366ca4SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
97366366ca4SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
974d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
97566366ca4SPyun YongHyeon 	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
976d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
977d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
978d65abd66SPyun YongHyeon 	if (error) {
979d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
980d65abd66SPyun YongHyeon 		return (error);
981d65abd66SPyun YongHyeon 	}
982d65abd66SPyun YongHyeon 
983d65abd66SPyun YongHyeon 	/*
984d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
985d65abd66SPyun YongHyeon 	 */
986d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
987d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
988d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
989d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
990d65abd66SPyun YongHyeon 	if (error) {
991d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
992d65abd66SPyun YongHyeon 		return (error);
993d65abd66SPyun YongHyeon 	}
994d65abd66SPyun YongHyeon 
995a94100faSBill Paul 	/*
996a94100faSBill Paul 	 * Allocate map for RX mbufs.
997a94100faSBill Paul 	 */
998d65abd66SPyun YongHyeon 
99981eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
100081eee0ebSPyun YongHyeon 		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
100181eee0ebSPyun YongHyeon 		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
100281eee0ebSPyun YongHyeon 		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
100381eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_mtag);
100481eee0ebSPyun YongHyeon 		if (error) {
100581eee0ebSPyun YongHyeon 			device_printf(dev,
100681eee0ebSPyun YongHyeon 			    "could not allocate jumbo RX DMA tag\n");
100781eee0ebSPyun YongHyeon 			return (error);
100881eee0ebSPyun YongHyeon 		}
100981eee0ebSPyun YongHyeon 	}
1010d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1011d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1012d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1013a94100faSBill Paul 	if (error) {
1014d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1015d65abd66SPyun YongHyeon 		return (error);
1016a94100faSBill Paul 	}
1017a94100faSBill Paul 
1018a94100faSBill Paul 	/*
1019a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1020a94100faSBill Paul 	 */
1021a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1022a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1023d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1024a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1025a94100faSBill Paul 	if (error) {
1026d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1027d65abd66SPyun YongHyeon 		return (error);
1028a94100faSBill Paul 	}
1029a94100faSBill Paul 
1030a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1031a94100faSBill Paul 
1032a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1033d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1034d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1035a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1036d65abd66SPyun YongHyeon 	if (error) {
1037d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1038d65abd66SPyun YongHyeon 		return (error);
1039d65abd66SPyun YongHyeon 	}
1040a94100faSBill Paul 
1041a94100faSBill Paul 	/* Load the map for the TX ring. */
1042a94100faSBill Paul 
1043d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1044a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1045a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1046d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1047a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1048d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1049d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1050d65abd66SPyun YongHyeon 		return (ENOMEM);
1051d65abd66SPyun YongHyeon 	}
1052a94100faSBill Paul 
1053a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1054a94100faSBill Paul 
1055d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1056d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1057d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1058a94100faSBill Paul 		if (error) {
1059d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1060d65abd66SPyun YongHyeon 			return (error);
1061a94100faSBill Paul 		}
1062a94100faSBill Paul 	}
1063a94100faSBill Paul 
1064a94100faSBill Paul 	/*
1065a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1066a94100faSBill Paul 	 */
1067a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1068a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1069d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1070a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1071a94100faSBill Paul 	if (error) {
1072d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1073d65abd66SPyun YongHyeon 		return (error);
1074a94100faSBill Paul 	}
1075a94100faSBill Paul 
1076a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1077a94100faSBill Paul 
1078a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1079d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1080d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1081a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1082d65abd66SPyun YongHyeon 	if (error) {
1083d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1084d65abd66SPyun YongHyeon 		return (error);
1085d65abd66SPyun YongHyeon 	}
1086a94100faSBill Paul 
1087a94100faSBill Paul 	/* Load the map for the RX ring. */
1088a94100faSBill Paul 
1089d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1090a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1091a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1092d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1093a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1094d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1095d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1096d65abd66SPyun YongHyeon 		return (ENOMEM);
1097d65abd66SPyun YongHyeon 	}
1098a94100faSBill Paul 
1099a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1100a94100faSBill Paul 
110181eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
110281eee0ebSPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
110381eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_sparemap);
110481eee0ebSPyun YongHyeon 		if (error) {
110581eee0ebSPyun YongHyeon 			device_printf(dev,
110681eee0ebSPyun YongHyeon 			    "could not create spare DMA map for jumbo RX\n");
110781eee0ebSPyun YongHyeon 			return (error);
110881eee0ebSPyun YongHyeon 		}
110981eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
111081eee0ebSPyun YongHyeon 			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
111181eee0ebSPyun YongHyeon 			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
111281eee0ebSPyun YongHyeon 			if (error) {
111381eee0ebSPyun YongHyeon 				device_printf(dev,
111481eee0ebSPyun YongHyeon 				    "could not create DMA map for jumbo RX\n");
111581eee0ebSPyun YongHyeon 				return (error);
111681eee0ebSPyun YongHyeon 			}
111781eee0ebSPyun YongHyeon 		}
111881eee0ebSPyun YongHyeon 	}
1119d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1120d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1121a94100faSBill Paul 	if (error) {
1122d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1123d65abd66SPyun YongHyeon 		return (error);
1124d65abd66SPyun YongHyeon 	}
1125d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1126d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1127d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1128d65abd66SPyun YongHyeon 		if (error) {
1129d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1130d65abd66SPyun YongHyeon 			return (error);
1131a94100faSBill Paul 		}
1132a94100faSBill Paul 	}
1133a94100faSBill Paul 
11340534aae0SPyun YongHyeon 	/* Create DMA map for statistics. */
11350534aae0SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
11360534aae0SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
11370534aae0SPyun YongHyeon 	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
11380534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_stag);
11390534aae0SPyun YongHyeon 	if (error) {
11400534aae0SPyun YongHyeon 		device_printf(dev, "could not create statistics DMA tag\n");
11410534aae0SPyun YongHyeon 		return (error);
11420534aae0SPyun YongHyeon 	}
11430534aae0SPyun YongHyeon 	/* Allocate DMA'able memory for statistics. */
11440534aae0SPyun YongHyeon 	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
11450534aae0SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_stats,
11460534aae0SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
11470534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_smap);
11480534aae0SPyun YongHyeon 	if (error) {
11490534aae0SPyun YongHyeon 		device_printf(dev,
11500534aae0SPyun YongHyeon 		    "could not allocate statistics DMA memory\n");
11510534aae0SPyun YongHyeon 		return (error);
11520534aae0SPyun YongHyeon 	}
11530534aae0SPyun YongHyeon 	/* Load the map for statistics. */
11540534aae0SPyun YongHyeon 	sc->rl_ldata.rl_stats_addr = 0;
11550534aae0SPyun YongHyeon 	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
11560534aae0SPyun YongHyeon 	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
11570534aae0SPyun YongHyeon 	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
11580534aae0SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
11590534aae0SPyun YongHyeon 		device_printf(dev, "could not load statistics DMA memory\n");
11600534aae0SPyun YongHyeon 		return (ENOMEM);
11610534aae0SPyun YongHyeon 	}
11620534aae0SPyun YongHyeon 
1163a94100faSBill Paul 	return (0);
1164a94100faSBill Paul }
1165a94100faSBill Paul 
1166a94100faSBill Paul /*
1167a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1168a94100faSBill Paul  * setup and ethernet/BPF attach.
1169a94100faSBill Paul  */
1170a94100faSBill Paul static int
11717b5ffebfSPyun YongHyeon re_attach(device_t dev)
1172a94100faSBill Paul {
1173a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1174be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1175a94100faSBill Paul 	struct rl_softc		*sc;
1176a94100faSBill Paul 	struct ifnet		*ifp;
1177a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1178a94100faSBill Paul 	int			hwrev;
1179ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
11808e5d93dbSMarius Strobl 	int			error = 0, i, phy, rid;
1181*4a58fd45SPyun YongHyeon 	int			msic, msixc, reg;
118203ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1183a94100faSBill Paul 
1184a94100faSBill Paul 	sc = device_get_softc(dev);
1185ed510fb0SBill Paul 	sc->rl_dev = dev;
1186a94100faSBill Paul 
1187a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
118897b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1189d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1190d1754a9bSJohn Baldwin 
1191a94100faSBill Paul 	/*
1192a94100faSBill Paul 	 * Map control/status registers.
1193a94100faSBill Paul 	 */
1194a94100faSBill Paul 	pci_enable_busmaster(dev);
1195a94100faSBill Paul 
1196ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
11972c21710bSPyun YongHyeon 	/*
11982c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
11992c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
12002c21710bSPyun YongHyeon 	 * is used always activate io mapping.
12012c21710bSPyun YongHyeon 	 */
12022c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
12032c21710bSPyun YongHyeon 		prefer_iomap = 1;
12042c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1205ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1206ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1207ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1208ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1209ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
12102c21710bSPyun YongHyeon 	} else {
12112c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
12122c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
12132c21710bSPyun YongHyeon 	}
1214ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1215ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
12162c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1217ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1218ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1219ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1220ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
12212c21710bSPyun YongHyeon 	}
1222ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1223d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1224a94100faSBill Paul 		error = ENXIO;
1225a94100faSBill Paul 		goto fail;
1226a94100faSBill Paul 	}
1227a94100faSBill Paul 
1228a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1229a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1230a94100faSBill Paul 
12315774c5ffSPyun YongHyeon 	msic = pci_msi_count(dev);
1232*4a58fd45SPyun YongHyeon 	msixc = pci_msix_count(dev);
1233*4a58fd45SPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0)
1234*4a58fd45SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
1235*4a58fd45SPyun YongHyeon 	if (bootverbose) {
12365774c5ffSPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
1237*4a58fd45SPyun YongHyeon 		device_printf(dev, "MSI-X count : %d\n", msixc);
12385774c5ffSPyun YongHyeon 	}
1239*4a58fd45SPyun YongHyeon 	if (msix_disable > 0)
1240*4a58fd45SPyun YongHyeon 		msixc = 0;
1241*4a58fd45SPyun YongHyeon 	if (msi_disable > 0)
1242*4a58fd45SPyun YongHyeon 		msic = 0;
1243*4a58fd45SPyun YongHyeon 	/* Prefer MSI-X to MSI. */
1244*4a58fd45SPyun YongHyeon 	if (msixc > 0) {
1245*4a58fd45SPyun YongHyeon 		msixc = 1;
1246*4a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
1247*4a58fd45SPyun YongHyeon 		sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
1248*4a58fd45SPyun YongHyeon 		    &rid, RF_ACTIVE);
1249*4a58fd45SPyun YongHyeon 		if (sc->rl_res_pba == NULL) {
1250*4a58fd45SPyun YongHyeon 			device_printf(sc->rl_dev,
1251*4a58fd45SPyun YongHyeon 			    "could not allocate MSI-X PBA resource\n");
1252*4a58fd45SPyun YongHyeon 		}
1253*4a58fd45SPyun YongHyeon 		if (sc->rl_res_pba != NULL &&
1254*4a58fd45SPyun YongHyeon 		    pci_alloc_msix(dev, &msixc) == 0) {
1255*4a58fd45SPyun YongHyeon 			if (msixc == 1) {
1256*4a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI-X message\n",
1257*4a58fd45SPyun YongHyeon 				    msixc);
1258*4a58fd45SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSIX;
1259*4a58fd45SPyun YongHyeon 			} else
1260*4a58fd45SPyun YongHyeon 				pci_release_msi(dev);
1261*4a58fd45SPyun YongHyeon 		}
1262*4a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
1263*4a58fd45SPyun YongHyeon 			if (sc->rl_res_pba != NULL)
1264*4a58fd45SPyun YongHyeon 				bus_release_resource(dev, SYS_RES_MEMORY, rid,
1265*4a58fd45SPyun YongHyeon 				    sc->rl_res_pba);
1266*4a58fd45SPyun YongHyeon 			sc->rl_res_pba = NULL;
1267*4a58fd45SPyun YongHyeon 			msixc = 0;
1268*4a58fd45SPyun YongHyeon 		}
1269*4a58fd45SPyun YongHyeon 	}
1270*4a58fd45SPyun YongHyeon 	/* Prefer MSI to INTx. */
1271*4a58fd45SPyun YongHyeon 	if (msixc == 0 && msic > 0) {
1272f1bb696aSPyun YongHyeon 		msic = 1;
12735774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
12745774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
1275*4a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI message\n",
12765774c5ffSPyun YongHyeon 				    msic);
1277351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1278339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1279339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1280339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1281339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1282339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1283f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
12845774c5ffSPyun YongHyeon 			} else
12855774c5ffSPyun YongHyeon 				pci_release_msi(dev);
12865774c5ffSPyun YongHyeon 		}
1287*4a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSI) == 0)
1288*4a58fd45SPyun YongHyeon 			msic = 0;
12895774c5ffSPyun YongHyeon 	}
1290a94100faSBill Paul 
12915774c5ffSPyun YongHyeon 	/* Allocate interrupt */
1292*4a58fd45SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
12935774c5ffSPyun YongHyeon 		rid = 0;
12945774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
12955774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
12965774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
12975774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1298a94100faSBill Paul 			error = ENXIO;
1299a94100faSBill Paul 			goto fail;
1300a94100faSBill Paul 		}
13015774c5ffSPyun YongHyeon 	} else {
13025774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
13035774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
13045774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
13055774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
13065774c5ffSPyun YongHyeon 				device_printf(dev,
13075774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
13085774c5ffSPyun YongHyeon 				    "message %d\n", rid);
13095774c5ffSPyun YongHyeon 				error = ENXIO;
13105774c5ffSPyun YongHyeon 				goto fail;
13115774c5ffSPyun YongHyeon 			}
13125774c5ffSPyun YongHyeon 		}
13135774c5ffSPyun YongHyeon 	}
1314a94100faSBill Paul 
13154d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
13164d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
13174d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
13184d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
13194d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
13204d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
13214d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
13224d2bf239SPyun YongHyeon 		}
13234d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13244d2bf239SPyun YongHyeon 	}
13254d2bf239SPyun YongHyeon 
1326abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1327a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1328566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1329566ca8caSJung-uk Kim 	case 0x00000000:
1330566ca8caSJung-uk Kim 	case 0x10000000:
1331566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1332566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1333566ca8caSJung-uk Kim 		break;
1334566ca8caSJung-uk Kim 	default:
1335a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1336a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1337566ca8caSJung-uk Kim 		break;
1338566ca8caSJung-uk Kim 	}
1339566ca8caSJung-uk Kim 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1340abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1341abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1342abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
134381eee0ebSPyun YongHyeon 			sc->rl_hwrev = hw_rev;
1344abc8ff44SBill Paul 			break;
1345abc8ff44SBill Paul 		}
1346abc8ff44SBill Paul 		hw_rev++;
1347abc8ff44SBill Paul 	}
1348d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1349a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1350d65abd66SPyun YongHyeon 		error = ENXIO;
1351d65abd66SPyun YongHyeon 		goto fail;
1352d65abd66SPyun YongHyeon 	}
1353abc8ff44SBill Paul 
1354351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1355351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
135681eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1357351a76f9SPyun YongHyeon 		break;
1358351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1359351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
136081eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1361351a76f9SPyun YongHyeon 		break;
1362b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1363b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
13643d22427cSTai-hwa Liang 	case RL_HWREV_8102EL_SPIN1:
136581eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
136681eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
136781eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1368b1d62f0fSPyun YongHyeon 		break;
13698281a098SPyun YongHyeon 	case RL_HWREV_8103E:
137081eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
137181eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
137281eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
13738281a098SPyun YongHyeon 		break;
1374ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN1:
1375ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN2:
1376886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1377886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1378ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN3:
1379aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1380deb5c680SPyun YongHyeon 		break;
1381deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
138261f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
138361f45a72SPyun YongHyeon 		/* FALLTHROUGH */
138461f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
138561f45a72SPyun YongHyeon 		if ((hwrev & 0x00700000) == 0x00200000)
138661f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
138761f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1388deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
138959ef640dSPyun YongHyeon 	case RL_HWREV_8168D:
13905fa06abeSPyun YongHyeon 	case RL_HWREV_8168DP:
1391aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1392f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
139381eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1394351a76f9SPyun YongHyeon 		break;
1395d0c45156SPyun YongHyeon 	case RL_HWREV_8168E:
1396d0c45156SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1397d0c45156SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
139881eee0ebSPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1399d0c45156SPyun YongHyeon 		break;
1400f0431c5bSPyun YongHyeon 	case RL_HWREV_8168E_VL:
1401f0431c5bSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1402f0431c5bSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
140381eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1404f0431c5bSPyun YongHyeon 		break;
1405566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1406566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1407566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1408566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1409566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1410566ca8caSJung-uk Kim 		/* FALLTHROUGH */
14110596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
14120596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1413566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1414566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1415351a76f9SPyun YongHyeon 		break;
1416351a76f9SPyun YongHyeon 	default:
1417351a76f9SPyun YongHyeon 		break;
1418351a76f9SPyun YongHyeon 	}
1419351a76f9SPyun YongHyeon 
142093252626SPyun YongHyeon 	/* Reset the adapter. */
142193252626SPyun YongHyeon 	RL_LOCK(sc);
142293252626SPyun YongHyeon 	re_reset(sc);
142393252626SPyun YongHyeon 	RL_UNLOCK(sc);
142493252626SPyun YongHyeon 
1425deb5c680SPyun YongHyeon 	/* Enable PME. */
1426deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1427deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1428deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1429deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1430deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1431deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1432deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1433deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1434deb5c680SPyun YongHyeon 
1435deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1436deb5c680SPyun YongHyeon 		/*
1437deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1438deb5c680SPyun YongHyeon 		 * address from EEPROM.
1439deb5c680SPyun YongHyeon 		 */
1440deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1441deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1442deb5c680SPyun YongHyeon 	} else {
1443141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1444ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1445a94100faSBill Paul 		if (re_did != 0x8129)
1446141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1447a94100faSBill Paul 
1448a94100faSBill Paul 		/*
1449a94100faSBill Paul 		 * Get station address from the EEPROM.
1450a94100faSBill Paul 		 */
1451ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1452be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1453be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1454be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1455deb5c680SPyun YongHyeon 	}
1456ed510fb0SBill Paul 
1457ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1458d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1459ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1460ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1461d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1462d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1463ed510fb0SBill Paul 	} else {
1464d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1465ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1466ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1467d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1468d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1469abc8ff44SBill Paul 	}
14709bac70b8SBill Paul 
1471a94100faSBill Paul 	error = re_allocmem(dev, sc);
1472a94100faSBill Paul 	if (error)
1473a94100faSBill Paul 		goto fail;
14740534aae0SPyun YongHyeon 	re_add_sysctls(sc);
1475a94100faSBill Paul 
1476cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1477cd036ec1SBrooks Davis 	if (ifp == NULL) {
1478d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1479cd036ec1SBrooks Davis 		error = ENOSPC;
1480cd036ec1SBrooks Davis 		goto fail;
1481cd036ec1SBrooks Davis 	}
1482cd036ec1SBrooks Davis 
148361f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
148461f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
148561f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
148661f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
148761f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
148861f45a72SPyun YongHyeon 		else
148961f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
149061f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
149161f45a72SPyun YongHyeon 	}
149261f45a72SPyun YongHyeon 
1493351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
1494d0c45156SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
1495d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
1496351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1497351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1498351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1499351a76f9SPyun YongHyeon 	}
1500351a76f9SPyun YongHyeon 
15018e5d93dbSMarius Strobl #define	RE_PHYAD_INTERNAL	 0
15028e5d93dbSMarius Strobl 
15038e5d93dbSMarius Strobl 	/* Do MII setup. */
15048e5d93dbSMarius Strobl 	phy = RE_PHYAD_INTERNAL;
15058e5d93dbSMarius Strobl 	if (sc->rl_type == RL_8169)
15068e5d93dbSMarius Strobl 		phy = 1;
15078e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
150864436f6eSPyun YongHyeon 	    re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
15098e5d93dbSMarius Strobl 	if (error != 0) {
15108e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
1511a94100faSBill Paul 		goto fail;
1512a94100faSBill Paul 	}
1513a94100faSBill Paul 
1514a94100faSBill Paul 	ifp->if_softc = sc;
15159bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1516a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1517a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1518a94100faSBill Paul 	ifp->if_start = re_start;
1519d6d7d923SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES | CSUM_TSO;
1520d6d7d923SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1521498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1522a94100faSBill Paul 	ifp->if_init = re_init;
152352732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
152452732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
152552732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1526a94100faSBill Paul 
1527ed510fb0SBill Paul 	TASK_INIT(&sc->rl_txtask, 1, re_tx_task, ifp);
1528ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1529ed510fb0SBill Paul 
1530a94100faSBill Paul 	/*
1531a94100faSBill Paul 	 * Call MI attach routine.
1532a94100faSBill Paul 	 */
1533a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1534a94100faSBill Paul 
1535960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1536960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1537960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1538960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
15397467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
15407467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
15417467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1542960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1543a2a8420cSPyun YongHyeon 	/*
1544f9ad4da7SPyun YongHyeon 	 * Don't enable TSO by default.  It is known to generate
1545f9ad4da7SPyun YongHyeon 	 * corrupted TCP segments(bad TCP options) under certain
1546f9ad4da7SPyun YongHyeon 	 * circumtances.
1547a2a8420cSPyun YongHyeon 	 */
1548a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1549ecafbbb5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1550960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1551960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1552960fd5b3SPyun YongHyeon #endif
1553960fd5b3SPyun YongHyeon 	/*
1554960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1555960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1556960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1557960fd5b3SPyun YongHyeon 	 */
1558960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1559960fd5b3SPyun YongHyeon 
1560ed510fb0SBill Paul #ifdef RE_DIAG
1561ed510fb0SBill Paul 	/*
1562ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1563ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1564ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1565ed510fb0SBill Paul 	 */
1566a94100faSBill Paul 
1567ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1568ed510fb0SBill Paul 		error = re_diag(sc);
1569a94100faSBill Paul 		if (error) {
1570ed510fb0SBill Paul 			device_printf(dev,
1571ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1572a94100faSBill Paul 			ether_ifdetach(ifp);
1573a94100faSBill Paul 			goto fail;
1574a94100faSBill Paul 		}
1575ed510fb0SBill Paul 	}
1576ed510fb0SBill Paul #endif
1577a94100faSBill Paul 
1578a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1579*4a58fd45SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
15805774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
15815774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
15825774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
15835774c5ffSPyun YongHyeon 	else {
15845774c5ffSPyun YongHyeon 		for (i = 0; i < RL_MSI_MESSAGES; i++) {
15855774c5ffSPyun YongHyeon 			error = bus_setup_intr(dev, sc->rl_irq[i],
15865774c5ffSPyun YongHyeon 			    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
15875774c5ffSPyun YongHyeon 		    	    &sc->rl_intrhand[i]);
15885774c5ffSPyun YongHyeon 			if (error != 0)
15895774c5ffSPyun YongHyeon 				break;
15905774c5ffSPyun YongHyeon 		}
15915774c5ffSPyun YongHyeon 	}
1592a94100faSBill Paul 	if (error) {
1593d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1594a94100faSBill Paul 		ether_ifdetach(ifp);
1595a94100faSBill Paul 	}
1596a94100faSBill Paul 
1597a94100faSBill Paul fail:
1598ed510fb0SBill Paul 
1599a94100faSBill Paul 	if (error)
1600a94100faSBill Paul 		re_detach(dev);
1601a94100faSBill Paul 
1602a94100faSBill Paul 	return (error);
1603a94100faSBill Paul }
1604a94100faSBill Paul 
1605a94100faSBill Paul /*
1606a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1607a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1608a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1609a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1610a94100faSBill Paul  * allocated.
1611a94100faSBill Paul  */
1612a94100faSBill Paul static int
16137b5ffebfSPyun YongHyeon re_detach(device_t dev)
1614a94100faSBill Paul {
1615a94100faSBill Paul 	struct rl_softc		*sc;
1616a94100faSBill Paul 	struct ifnet		*ifp;
16175774c5ffSPyun YongHyeon 	int			i, rid;
1618a94100faSBill Paul 
1619a94100faSBill Paul 	sc = device_get_softc(dev);
1620fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1621aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
162297b9d4baSJohn-Mark Gurney 
162381cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
162481cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
162540929967SGleb Smirnoff #ifdef DEVICE_POLLING
162640929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
162740929967SGleb Smirnoff 			ether_poll_deregister(ifp);
162840929967SGleb Smirnoff #endif
162997b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
163097b9d4baSJohn-Mark Gurney #if 0
163197b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
163297b9d4baSJohn-Mark Gurney #endif
1633a94100faSBill Paul 		re_stop(sc);
1634525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1635d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
16363d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
16373d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_txtask);
1638a94100faSBill Paul 		/*
1639a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1640a94100faSBill Paul 		 * still had a BPF descriptor attached to this
164197b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1642a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1643a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1644a94100faSBill Paul 		 * which will try to call re_init() again. This will
1645a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1646a94100faSBill Paul 		 * which will panic the system when the kernel tries
1647a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1648a94100faSBill Paul 		 * anymore.
1649a94100faSBill Paul 		 */
1650a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1651525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1652a94100faSBill Paul 	}
1653a94100faSBill Paul 	if (sc->rl_miibus)
1654a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1655a94100faSBill Paul 	bus_generic_detach(dev);
1656a94100faSBill Paul 
165797b9d4baSJohn-Mark Gurney 	/*
165897b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
165997b9d4baSJohn-Mark Gurney 	 * stopped here.
166097b9d4baSJohn-Mark Gurney 	 */
166197b9d4baSJohn-Mark Gurney 
16625774c5ffSPyun YongHyeon 	for (i = 0; i < RL_MSI_MESSAGES; i++) {
16635774c5ffSPyun YongHyeon 		if (sc->rl_intrhand[i] != NULL) {
16645774c5ffSPyun YongHyeon 			bus_teardown_intr(dev, sc->rl_irq[i],
16655774c5ffSPyun YongHyeon 			    sc->rl_intrhand[i]);
16665774c5ffSPyun YongHyeon 			sc->rl_intrhand[i] = NULL;
16675774c5ffSPyun YongHyeon 		}
16685774c5ffSPyun YongHyeon 	}
1669ad4f426eSWarner Losh 	if (ifp != NULL)
1670ad4f426eSWarner Losh 		if_free(ifp);
1671*4a58fd45SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
16725774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] != NULL) {
16735774c5ffSPyun YongHyeon 			bus_release_resource(dev, SYS_RES_IRQ, 0,
16745774c5ffSPyun YongHyeon 			    sc->rl_irq[0]);
16755774c5ffSPyun YongHyeon 			sc->rl_irq[0] = NULL;
16765774c5ffSPyun YongHyeon 		}
16775774c5ffSPyun YongHyeon 	} else {
16785774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
16795774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] != NULL) {
16805774c5ffSPyun YongHyeon 				bus_release_resource(dev, SYS_RES_IRQ, rid,
16815774c5ffSPyun YongHyeon 				    sc->rl_irq[i]);
16825774c5ffSPyun YongHyeon 				sc->rl_irq[i] = NULL;
16835774c5ffSPyun YongHyeon 			}
16845774c5ffSPyun YongHyeon 		}
16855774c5ffSPyun YongHyeon 		pci_release_msi(dev);
16865774c5ffSPyun YongHyeon 	}
1687*4a58fd45SPyun YongHyeon 	if (sc->rl_res_pba) {
1688*4a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
1689*4a58fd45SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
1690*4a58fd45SPyun YongHyeon 	}
1691a94100faSBill Paul 	if (sc->rl_res)
1692ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1693ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1694a94100faSBill Paul 
1695a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1696a94100faSBill Paul 
1697a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
16980534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map)
1699a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1700a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
17010534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list)
1702a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1703a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list,
1704a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
1705a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1706a94100faSBill Paul 	}
1707a94100faSBill Paul 
1708a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1709a94100faSBill Paul 
1710a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
17110534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map)
1712a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1713a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
17140534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list)
1715a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1716a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list,
1717a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
1718a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1719a94100faSBill Paul 	}
1720a94100faSBill Paul 
1721a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1722a94100faSBill Paul 
1723d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
17249e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
17259e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1726d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1727d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
17289e18005dSPyun YongHyeon 		}
1729d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1730d65abd66SPyun YongHyeon 	}
1731d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
17329e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
17339e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1734d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1735d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
17369e18005dSPyun YongHyeon 		}
1737d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1738d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1739d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1740d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1741a94100faSBill Paul 	}
174281eee0ebSPyun YongHyeon 	if (sc->rl_ldata.rl_jrx_mtag) {
174381eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
174481eee0ebSPyun YongHyeon 			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
174581eee0ebSPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
174681eee0ebSPyun YongHyeon 				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
174781eee0ebSPyun YongHyeon 		}
174881eee0ebSPyun YongHyeon 		if (sc->rl_ldata.rl_jrx_sparemap)
174981eee0ebSPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
175081eee0ebSPyun YongHyeon 			    sc->rl_ldata.rl_jrx_sparemap);
175181eee0ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
175281eee0ebSPyun YongHyeon 	}
1753a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1754a94100faSBill Paul 
1755a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
17560534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap)
1757a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1758a94100faSBill Paul 			    sc->rl_ldata.rl_smap);
17590534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats)
17600534aae0SPyun YongHyeon 			bus_dmamem_free(sc->rl_ldata.rl_stag,
17610534aae0SPyun YongHyeon 			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1762a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1763a94100faSBill Paul 	}
1764a94100faSBill Paul 
1765a94100faSBill Paul 	if (sc->rl_parent_tag)
1766a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1767a94100faSBill Paul 
1768a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1769a94100faSBill Paul 
1770a94100faSBill Paul 	return (0);
1771a94100faSBill Paul }
1772a94100faSBill Paul 
1773d65abd66SPyun YongHyeon static __inline void
17747b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1775a94100faSBill Paul {
1776d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1777d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1778d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1779a94100faSBill Paul 
178081eee0ebSPyun YongHyeon 	if (sc->rl_ifp->if_mtu > RL_MTU &&
178181eee0ebSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
178281eee0ebSPyun YongHyeon 		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
178381eee0ebSPyun YongHyeon 	else
1784d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1785d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1786d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1787d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1788d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1789d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1790d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1791d65abd66SPyun YongHyeon }
1792d65abd66SPyun YongHyeon 
1793d65abd66SPyun YongHyeon static int
17947b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1795d65abd66SPyun YongHyeon {
1796d65abd66SPyun YongHyeon 	struct mbuf		*m;
1797d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1798d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1799d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1800d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1801d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1802d65abd66SPyun YongHyeon 	int			error, nsegs;
1803d65abd66SPyun YongHyeon 
1804d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1805d65abd66SPyun YongHyeon 	if (m == NULL)
1806a94100faSBill Paul 		return (ENOBUFS);
1807a94100faSBill Paul 
1808a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
180922a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
181022a11c96SJohn-Mark Gurney 	/*
181122a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
181222a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
181322a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
181422a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
181522a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
181622a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
181722a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
181822a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
181922a11c96SJohn-Mark Gurney 	 */
182022a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
182122a11c96SJohn-Mark Gurney #endif
1822d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1823d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1824d65abd66SPyun YongHyeon 	if (error != 0) {
1825d65abd66SPyun YongHyeon 		m_freem(m);
1826d65abd66SPyun YongHyeon 		return (ENOBUFS);
1827d65abd66SPyun YongHyeon 	}
1828d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1829a94100faSBill Paul 
1830d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1831d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1832d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1833d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1834d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1835a94100faSBill Paul 	}
1836a94100faSBill Paul 
1837d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1838d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1839d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1840d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1841d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1842d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1843a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1844a94100faSBill Paul 
1845d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1846d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1847d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1848d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1849d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1850d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1851d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1852d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1853d65abd66SPyun YongHyeon 
1854a94100faSBill Paul 	return (0);
1855a94100faSBill Paul }
1856a94100faSBill Paul 
185781eee0ebSPyun YongHyeon static int
185881eee0ebSPyun YongHyeon re_jumbo_newbuf(struct rl_softc *sc, int idx)
185981eee0ebSPyun YongHyeon {
186081eee0ebSPyun YongHyeon 	struct mbuf		*m;
186181eee0ebSPyun YongHyeon 	struct rl_rxdesc	*rxd;
186281eee0ebSPyun YongHyeon 	bus_dma_segment_t	segs[1];
186381eee0ebSPyun YongHyeon 	bus_dmamap_t		map;
186481eee0ebSPyun YongHyeon 	struct rl_desc		*desc;
186581eee0ebSPyun YongHyeon 	uint32_t		cmdstat;
186681eee0ebSPyun YongHyeon 	int			error, nsegs;
186781eee0ebSPyun YongHyeon 
186881eee0ebSPyun YongHyeon 	m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
186981eee0ebSPyun YongHyeon 	if (m == NULL)
187081eee0ebSPyun YongHyeon 		return (ENOBUFS);
187181eee0ebSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
187281eee0ebSPyun YongHyeon #ifdef RE_FIXUP_RX
187381eee0ebSPyun YongHyeon 	m_adj(m, RE_ETHER_ALIGN);
187481eee0ebSPyun YongHyeon #endif
187581eee0ebSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
187681eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
187781eee0ebSPyun YongHyeon 	if (error != 0) {
187881eee0ebSPyun YongHyeon 		m_freem(m);
187981eee0ebSPyun YongHyeon 		return (ENOBUFS);
188081eee0ebSPyun YongHyeon 	}
188181eee0ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
188281eee0ebSPyun YongHyeon 
188381eee0ebSPyun YongHyeon 	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
188481eee0ebSPyun YongHyeon 	if (rxd->rx_m != NULL) {
188581eee0ebSPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
188681eee0ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
188781eee0ebSPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
188881eee0ebSPyun YongHyeon 	}
188981eee0ebSPyun YongHyeon 
189081eee0ebSPyun YongHyeon 	rxd->rx_m = m;
189181eee0ebSPyun YongHyeon 	map = rxd->rx_dmamap;
189281eee0ebSPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
189381eee0ebSPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
189481eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_jrx_sparemap = map;
189581eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
189681eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
189781eee0ebSPyun YongHyeon 
189881eee0ebSPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
189981eee0ebSPyun YongHyeon 	desc->rl_vlanctl = 0;
190081eee0ebSPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
190181eee0ebSPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
190281eee0ebSPyun YongHyeon 	cmdstat = segs[0].ds_len;
190381eee0ebSPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
190481eee0ebSPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
190581eee0ebSPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
190681eee0ebSPyun YongHyeon 
190781eee0ebSPyun YongHyeon 	return (0);
190881eee0ebSPyun YongHyeon }
190981eee0ebSPyun YongHyeon 
191022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
191122a11c96SJohn-Mark Gurney static __inline void
19127b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
191322a11c96SJohn-Mark Gurney {
191422a11c96SJohn-Mark Gurney 	int                     i;
191522a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
191622a11c96SJohn-Mark Gurney 
191722a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
191822a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
191922a11c96SJohn-Mark Gurney 
192022a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
192122a11c96SJohn-Mark Gurney 		*dst++ = *src++;
192222a11c96SJohn-Mark Gurney 
192322a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
192422a11c96SJohn-Mark Gurney }
192522a11c96SJohn-Mark Gurney #endif
192622a11c96SJohn-Mark Gurney 
1927a94100faSBill Paul static int
19287b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
1929a94100faSBill Paul {
1930d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1931d65abd66SPyun YongHyeon 	int			i;
193297b9d4baSJohn-Mark Gurney 
193397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
193497b9d4baSJohn-Mark Gurney 
1935d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1936d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1937d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1938d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1939d65abd66SPyun YongHyeon 	/* Set EOR. */
1940d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1941d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1942a94100faSBill Paul 
1943a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1944d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1945d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1946d65abd66SPyun YongHyeon 
1947a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1948a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1949d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1950a94100faSBill Paul 
1951a94100faSBill Paul 	return (0);
1952a94100faSBill Paul }
1953a94100faSBill Paul 
1954a94100faSBill Paul static int
19557b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
1956a94100faSBill Paul {
1957d65abd66SPyun YongHyeon 	int			error, i;
1958a94100faSBill Paul 
1959d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1960d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1961d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1962d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1963d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1964d65abd66SPyun YongHyeon 			return (error);
1965a94100faSBill Paul 	}
1966a94100faSBill Paul 
1967a94100faSBill Paul 	/* Flush the RX descriptors */
1968a94100faSBill Paul 
1969a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1970a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1971a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1972a94100faSBill Paul 
1973a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1974a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1975a94100faSBill Paul 
1976a94100faSBill Paul 	return (0);
1977a94100faSBill Paul }
1978a94100faSBill Paul 
197981eee0ebSPyun YongHyeon static int
198081eee0ebSPyun YongHyeon re_jrx_list_init(struct rl_softc *sc)
198181eee0ebSPyun YongHyeon {
198281eee0ebSPyun YongHyeon 	int			error, i;
198381eee0ebSPyun YongHyeon 
198481eee0ebSPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
198581eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
198681eee0ebSPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
198781eee0ebSPyun YongHyeon 		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
198881eee0ebSPyun YongHyeon 		if ((error = re_jumbo_newbuf(sc, i)) != 0)
198981eee0ebSPyun YongHyeon 			return (error);
199081eee0ebSPyun YongHyeon 	}
199181eee0ebSPyun YongHyeon 
199281eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
199381eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_list_map,
199481eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
199581eee0ebSPyun YongHyeon 
199681eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_rx_prodidx = 0;
199781eee0ebSPyun YongHyeon 	sc->rl_head = sc->rl_tail = NULL;
199881eee0ebSPyun YongHyeon 
199981eee0ebSPyun YongHyeon 	return (0);
200081eee0ebSPyun YongHyeon }
200181eee0ebSPyun YongHyeon 
2002a94100faSBill Paul /*
2003a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
2004a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
2005a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
2006a94100faSBill Paul  */
2007ed510fb0SBill Paul static int
20081abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2009a94100faSBill Paul {
2010a94100faSBill Paul 	struct mbuf		*m;
2011a94100faSBill Paul 	struct ifnet		*ifp;
201281eee0ebSPyun YongHyeon 	int			i, rxerr, total_len;
2013a94100faSBill Paul 	struct rl_desc		*cur_rx;
2014a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
201581eee0ebSPyun YongHyeon 	int			jumbo, maxpkt = 16, rx_npkts = 0;
2016a94100faSBill Paul 
20175120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
20185120abbfSSam Leffler 
2019fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
202081eee0ebSPyun YongHyeon 	if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
202181eee0ebSPyun YongHyeon 		jumbo = 1;
202281eee0ebSPyun YongHyeon 	else
202381eee0ebSPyun YongHyeon 		jumbo = 0;
2024a94100faSBill Paul 
2025a94100faSBill Paul 	/* Invalidate the descriptor memory */
2026a94100faSBill Paul 
2027a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2028a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2029d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2030a94100faSBill Paul 
2031d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2032d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
20335b6d1d9dSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
20345b6d1d9dSPyun YongHyeon 			break;
2035a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
2036a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
2037d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2038d65abd66SPyun YongHyeon 			break;
2039d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
2040a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
204181eee0ebSPyun YongHyeon 		if (jumbo != 0)
204281eee0ebSPyun YongHyeon 			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
204381eee0ebSPyun YongHyeon 		else
2044d65abd66SPyun YongHyeon 			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2045a94100faSBill Paul 
204681eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
204781eee0ebSPyun YongHyeon 		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
204881eee0ebSPyun YongHyeon 		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
204981eee0ebSPyun YongHyeon 			/*
205081eee0ebSPyun YongHyeon 			 * RTL8168C or later controllers do not
205181eee0ebSPyun YongHyeon 			 * support multi-fragment packet.
205281eee0ebSPyun YongHyeon 			 */
205381eee0ebSPyun YongHyeon 			re_discard_rxbuf(sc, i);
205481eee0ebSPyun YongHyeon 			continue;
205581eee0ebSPyun YongHyeon 		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2056d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
2057d65abd66SPyun YongHyeon 				/*
2058d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
2059d65abd66SPyun YongHyeon 				 * discard all the pieces.
2060d65abd66SPyun YongHyeon 				 */
2061d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
2062d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
2063d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
2064d65abd66SPyun YongHyeon 				}
2065d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2066d65abd66SPyun YongHyeon 				continue;
2067d65abd66SPyun YongHyeon 			}
206822a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
2069a94100faSBill Paul 			if (sc->rl_head == NULL)
2070a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
2071a94100faSBill Paul 			else {
2072a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2073a94100faSBill Paul 				sc->rl_tail->m_next = m;
2074a94100faSBill Paul 				sc->rl_tail = m;
2075a94100faSBill Paul 			}
2076a94100faSBill Paul 			continue;
2077a94100faSBill Paul 		}
2078a94100faSBill Paul 
2079a94100faSBill Paul 		/*
2080a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
2081a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
2082a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
2083a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
2084a94100faSBill Paul 		 * were already used, so to make room for the extra
2085a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
2086a94100faSBill Paul 		 * error' bit and shifted the other status bits
2087a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
2088a94100faSBill Paul 		 * still in the same places. We have already extracted
2089a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
2090a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
2091a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
2092a94100faSBill Paul 		 * them using the 8169 status as though it was in the
2093a94100faSBill Paul 		 * same format as that of the 8139C+.
2094a94100faSBill Paul 		 */
2095a94100faSBill Paul 		if (sc->rl_type == RL_8169)
2096a94100faSBill Paul 			rxstat >>= 1;
2097a94100faSBill Paul 
209822a11c96SJohn-Mark Gurney 		/*
209922a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
210022a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
210122a11c96SJohn-Mark Gurney 		 */
210281eee0ebSPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
210381eee0ebSPyun YongHyeon 			rxerr = 1;
210481eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
210581eee0ebSPyun YongHyeon 			    total_len > 8191 &&
210681eee0ebSPyun YongHyeon 			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
210781eee0ebSPyun YongHyeon 				rxerr = 0;
210881eee0ebSPyun YongHyeon 			if (rxerr != 0) {
2109a94100faSBill Paul 				ifp->if_ierrors++;
2110a94100faSBill Paul 				/*
2111a94100faSBill Paul 				 * If this is part of a multi-fragment packet,
2112a94100faSBill Paul 				 * discard all the pieces.
2113a94100faSBill Paul 				 */
2114a94100faSBill Paul 				if (sc->rl_head != NULL) {
2115a94100faSBill Paul 					m_freem(sc->rl_head);
2116a94100faSBill Paul 					sc->rl_head = sc->rl_tail = NULL;
2117a94100faSBill Paul 				}
2118d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2119a94100faSBill Paul 				continue;
2120a94100faSBill Paul 			}
212181eee0ebSPyun YongHyeon 		}
2122a94100faSBill Paul 
2123a94100faSBill Paul 		/*
2124a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
2125a94100faSBill Paul 		 * reload the current one.
2126a94100faSBill Paul 		 */
212781eee0ebSPyun YongHyeon 		if (jumbo != 0)
212881eee0ebSPyun YongHyeon 			rxerr = re_jumbo_newbuf(sc, i);
212981eee0ebSPyun YongHyeon 		else
213081eee0ebSPyun YongHyeon 			rxerr = re_newbuf(sc, i);
213181eee0ebSPyun YongHyeon 		if (rxerr != 0) {
2132d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
2133a94100faSBill Paul 			if (sc->rl_head != NULL) {
2134a94100faSBill Paul 				m_freem(sc->rl_head);
2135a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
2136a94100faSBill Paul 			}
2137d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
2138a94100faSBill Paul 			continue;
2139a94100faSBill Paul 		}
2140a94100faSBill Paul 
2141a94100faSBill Paul 		if (sc->rl_head != NULL) {
214281eee0ebSPyun YongHyeon 			if (jumbo != 0)
214381eee0ebSPyun YongHyeon 				m->m_len = total_len;
214481eee0ebSPyun YongHyeon 			else {
214522a11c96SJohn-Mark Gurney 				m->m_len = total_len % RE_RX_DESC_BUFLEN;
214622a11c96SJohn-Mark Gurney 				if (m->m_len == 0)
214722a11c96SJohn-Mark Gurney 					m->m_len = RE_RX_DESC_BUFLEN;
214881eee0ebSPyun YongHyeon 			}
2149a94100faSBill Paul 			/*
2150a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
2151a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
2152a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
2153a94100faSBill Paul 			 * care about anyway.
2154a94100faSBill Paul 			 */
2155a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
2156a94100faSBill Paul 				sc->rl_tail->m_len -=
2157a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
2158a94100faSBill Paul 				m_freem(m);
2159a94100faSBill Paul 			} else {
2160a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
2161a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2162a94100faSBill Paul 				sc->rl_tail->m_next = m;
2163a94100faSBill Paul 			}
2164a94100faSBill Paul 			m = sc->rl_head;
2165a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
2166a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2167a94100faSBill Paul 		} else
2168a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
2169a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
2170a94100faSBill Paul 
217122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
217222a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
217322a11c96SJohn-Mark Gurney #endif
2174a94100faSBill Paul 		ifp->if_ipackets++;
2175a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
2176a94100faSBill Paul 
2177a94100faSBill Paul 		/* Do RX checksumming if enabled */
2178a94100faSBill Paul 
2179a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2180deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2181a94100faSBill Paul 				/* Check IP header checksum */
2182a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
2183deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2184deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2185a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2186deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2187deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2188a94100faSBill Paul 
2189a94100faSBill Paul 				/* Check TCP/UDP checksum */
2190a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
2191a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2192a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
2193a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2194a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
2195a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2196a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
2197a94100faSBill Paul 				}
2198deb5c680SPyun YongHyeon 			} else {
2199deb5c680SPyun YongHyeon 				/*
2200deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2201deb5c680SPyun YongHyeon 				 */
2202deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2203deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2204deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2205deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2206deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2207deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2208deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2209deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2210deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
2211deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2212deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
2213deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2214deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2215deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2216deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
2217deb5c680SPyun YongHyeon 				}
2218deb5c680SPyun YongHyeon 			}
2219a94100faSBill Paul 		}
2220ed510fb0SBill Paul 		maxpkt--;
2221d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
222278ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
2223bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
222478ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2225d147662cSGleb Smirnoff 		}
22265120abbfSSam Leffler 		RL_UNLOCK(sc);
2227a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
22285120abbfSSam Leffler 		RL_LOCK(sc);
22291abcdbd1SAttilio Rao 		rx_npkts++;
2230a94100faSBill Paul 	}
2231a94100faSBill Paul 
2232a94100faSBill Paul 	/* Flush the RX DMA ring */
2233a94100faSBill Paul 
2234a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2235a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2236a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2237a94100faSBill Paul 
2238a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
2239ed510fb0SBill Paul 
22401abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
22411abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2242ed510fb0SBill Paul 	if (maxpkt)
2243ed510fb0SBill Paul 		return (EAGAIN);
2244ed510fb0SBill Paul 
2245ed510fb0SBill Paul 	return (0);
2246a94100faSBill Paul }
2247a94100faSBill Paul 
2248a94100faSBill Paul static void
22497b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2250a94100faSBill Paul {
2251a94100faSBill Paul 	struct ifnet		*ifp;
2252d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2253a94100faSBill Paul 	u_int32_t		txstat;
2254d65abd66SPyun YongHyeon 	int			cons;
2255d65abd66SPyun YongHyeon 
2256d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2257d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2258d65abd66SPyun YongHyeon 		return;
2259a94100faSBill Paul 
2260fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2261a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2262a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2263a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2264d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2265a94100faSBill Paul 
2266d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2267d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2268d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2269d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2270a94100faSBill Paul 			break;
2271a94100faSBill Paul 		/*
2272a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2273a94100faSBill Paul 		 * in a fragment chain, which also happens to
2274a94100faSBill Paul 		 * be the only place where the TX status bits
2275a94100faSBill Paul 		 * are valid.
2276a94100faSBill Paul 		 */
2277a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2278d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2279d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2280d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2281d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2282d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2283d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2284d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2285d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2286d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2287a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2288a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2289a94100faSBill Paul 				ifp->if_collisions++;
2290a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2291a94100faSBill Paul 				ifp->if_oerrors++;
2292a94100faSBill Paul 			else
2293a94100faSBill Paul 				ifp->if_opackets++;
2294a94100faSBill Paul 		}
2295a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2296d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2297a94100faSBill Paul 	}
2298d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2299a94100faSBill Paul 
2300a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2301a94100faSBill Paul 
2302d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2303ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2304a94100faSBill Paul 		/*
2305b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2306b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2307a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2308a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2309a94100faSBill Paul 		 */
2310a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2311ed510fb0SBill Paul #endif
2312b4b95879SMarius Strobl 	} else
2313b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2314a94100faSBill Paul }
2315a94100faSBill Paul 
2316a94100faSBill Paul static void
23177b5ffebfSPyun YongHyeon re_tick(void *xsc)
2318a94100faSBill Paul {
2319a94100faSBill Paul 	struct rl_softc		*sc;
2320d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2321a94100faSBill Paul 
2322a94100faSBill Paul 	sc = xsc;
232397b9d4baSJohn-Mark Gurney 
232497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
232597b9d4baSJohn-Mark Gurney 
23261d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2327a94100faSBill Paul 	mii_tick(mii);
23280fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
23290fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2330c2d2e19cSPyun YongHyeon 	/*
2331c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2332c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2333c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2334c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2335c2d2e19cSPyun YongHyeon 	 */
2336c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2337130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2338d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2339a94100faSBill Paul }
2340a94100faSBill Paul 
2341a94100faSBill Paul #ifdef DEVICE_POLLING
23421abcdbd1SAttilio Rao static int
2343a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2344a94100faSBill Paul {
2345a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
23461abcdbd1SAttilio Rao 	int rx_npkts = 0;
2347a94100faSBill Paul 
2348a94100faSBill Paul 	RL_LOCK(sc);
234940929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
23501abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
235197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
23521abcdbd1SAttilio Rao 	return (rx_npkts);
235397b9d4baSJohn-Mark Gurney }
235497b9d4baSJohn-Mark Gurney 
23551abcdbd1SAttilio Rao static int
235697b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
235797b9d4baSJohn-Mark Gurney {
235897b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
23591abcdbd1SAttilio Rao 	int rx_npkts;
236097b9d4baSJohn-Mark Gurney 
236197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
236297b9d4baSJohn-Mark Gurney 
2363a94100faSBill Paul 	sc->rxcycles = count;
23641abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2365a94100faSBill Paul 	re_txeof(sc);
2366a94100faSBill Paul 
236737652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2368ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2369a94100faSBill Paul 
2370a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2371a94100faSBill Paul 		u_int16_t       status;
2372a94100faSBill Paul 
2373a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2374a94100faSBill Paul 		if (status == 0xffff)
23751abcdbd1SAttilio Rao 			return (rx_npkts);
2376a94100faSBill Paul 		if (status)
2377a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2378818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2379818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2380818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2381a94100faSBill Paul 
2382a94100faSBill Paul 		/*
2383a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2384a94100faSBill Paul 		 */
2385a94100faSBill Paul 
23868476c243SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR) {
23878476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
238897b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2389a94100faSBill Paul 		}
23908476c243SPyun YongHyeon 	}
23911abcdbd1SAttilio Rao 	return (rx_npkts);
2392a94100faSBill Paul }
2393a94100faSBill Paul #endif /* DEVICE_POLLING */
2394a94100faSBill Paul 
2395ef544f63SPaolo Pisati static int
23967b5ffebfSPyun YongHyeon re_intr(void *arg)
2397a94100faSBill Paul {
2398a94100faSBill Paul 	struct rl_softc		*sc;
2399ed510fb0SBill Paul 	uint16_t		status;
2400a94100faSBill Paul 
2401a94100faSBill Paul 	sc = arg;
2402ed510fb0SBill Paul 
2403ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2404498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2405ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2406ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2407ed510fb0SBill Paul 
2408ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2409ed510fb0SBill Paul 
2410ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2411ed510fb0SBill Paul }
2412ed510fb0SBill Paul 
2413ed510fb0SBill Paul static void
24147b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2415ed510fb0SBill Paul {
2416ed510fb0SBill Paul 	struct rl_softc		*sc;
2417ed510fb0SBill Paul 	struct ifnet		*ifp;
2418ed510fb0SBill Paul 	u_int16_t		status;
2419ed510fb0SBill Paul 	int			rval = 0;
2420ed510fb0SBill Paul 
2421ed510fb0SBill Paul 	sc = arg;
2422ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2423a94100faSBill Paul 
2424a94100faSBill Paul 	RL_LOCK(sc);
242597b9d4baSJohn-Mark Gurney 
2426a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2427a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2428a94100faSBill Paul 
2429d65abd66SPyun YongHyeon 	if (sc->suspended ||
2430d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2431ed510fb0SBill Paul 		RL_UNLOCK(sc);
2432ed510fb0SBill Paul 		return;
2433ed510fb0SBill Paul 	}
2434a94100faSBill Paul 
2435ed510fb0SBill Paul #ifdef DEVICE_POLLING
2436ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2437ed510fb0SBill Paul 		RL_UNLOCK(sc);
2438ed510fb0SBill Paul 		return;
2439ed510fb0SBill Paul 	}
2440ed510fb0SBill Paul #endif
2441a94100faSBill Paul 
2442ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
24431abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2444ed510fb0SBill Paul 
2445818951afSPyun YongHyeon 	/*
2446818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2447818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2448818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2449818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2450818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2451818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2452818951afSPyun YongHyeon 	 */
2453818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2454818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2455818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
24563d85c23dSPyun YongHyeon 	if (status & (
2457ed510fb0SBill Paul #ifdef RE_TX_MODERATION
24583d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2459ed510fb0SBill Paul #else
24603d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2461ed510fb0SBill Paul #endif
2462ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2463a94100faSBill Paul 		re_txeof(sc);
2464a94100faSBill Paul 
24658476c243SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
24668476c243SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
246797b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
24688476c243SPyun YongHyeon 	}
2469a94100faSBill Paul 
247052732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2471ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
2472a94100faSBill Paul 
2473a94100faSBill Paul 	RL_UNLOCK(sc);
2474ed510fb0SBill Paul 
2475ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2476ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2477ed510fb0SBill Paul 		return;
2478ed510fb0SBill Paul 	}
2479ed510fb0SBill Paul 
2480ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2481a94100faSBill Paul }
2482a94100faSBill Paul 
2483d65abd66SPyun YongHyeon static int
24847b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2485d65abd66SPyun YongHyeon {
2486d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2487d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2488d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2489d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2490d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2491d65abd66SPyun YongHyeon 	int			nsegs, prod;
2492d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2493d65abd66SPyun YongHyeon 	int			padlen;
2494ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2495a94100faSBill Paul 
2496d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2497738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
24980fc4974fSBill Paul 
24990fc4974fSBill Paul 	/*
25000fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
25010fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
25020fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
25030fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
25040fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
25050fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
250699c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
25070fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2508d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
25090fc4974fSBill Paul 	 */
2510f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2511deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
251299c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2513d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2514d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2515d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2516d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2517d65abd66SPyun YongHyeon 			m_freem(*m_head);
2518d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2519d65abd66SPyun YongHyeon 				*m_head = NULL;
2520a94100faSBill Paul 				return (ENOBUFS);
2521a94100faSBill Paul 			}
2522d65abd66SPyun YongHyeon 			*m_head = m_new;
2523d65abd66SPyun YongHyeon 		}
2524d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2525d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
252680a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2527b4b95879SMarius Strobl 			if (m_new == NULL) {
2528b4b95879SMarius Strobl 				m_freem(*m_head);
2529b4b95879SMarius Strobl 				*m_head = NULL;
253080a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2531b4b95879SMarius Strobl 			}
2532d65abd66SPyun YongHyeon 		} else
2533d65abd66SPyun YongHyeon 			m_new = *m_head;
2534a94100faSBill Paul 
25350fc4974fSBill Paul 		/*
25360fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
25370fc4974fSBill Paul 		 * to avoid leaking data.
25380fc4974fSBill Paul 		 */
2539d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2540d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
25410fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2542d65abd66SPyun YongHyeon 		*m_head = m_new;
25430fc4974fSBill Paul 	}
25440fc4974fSBill Paul 
2545d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2546d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2547d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2548d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2549d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2550304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2551d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2552d65abd66SPyun YongHyeon 			m_freem(*m_head);
2553b4b95879SMarius Strobl 			*m_head = NULL;
2554d65abd66SPyun YongHyeon 			return (ENOBUFS);
2555a94100faSBill Paul 		}
2556d65abd66SPyun YongHyeon 		*m_head = m_new;
2557d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2558d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2559d65abd66SPyun YongHyeon 		if (error != 0) {
2560d65abd66SPyun YongHyeon 			m_freem(*m_head);
2561d65abd66SPyun YongHyeon 			*m_head = NULL;
2562d65abd66SPyun YongHyeon 			return (error);
2563a94100faSBill Paul 		}
2564d65abd66SPyun YongHyeon 	} else if (error != 0)
2565d65abd66SPyun YongHyeon 		return (error);
2566d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2567d65abd66SPyun YongHyeon 		m_freem(*m_head);
2568d65abd66SPyun YongHyeon 		*m_head = NULL;
2569d65abd66SPyun YongHyeon 		return (EIO);
2570d65abd66SPyun YongHyeon 	}
2571d65abd66SPyun YongHyeon 
2572d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2573d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2574d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2575d65abd66SPyun YongHyeon 		return (ENOBUFS);
2576d65abd66SPyun YongHyeon 	}
2577d65abd66SPyun YongHyeon 
2578d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2579d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2580a94100faSBill Paul 
2581a94100faSBill Paul 	/*
2582d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2583d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2584d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2585d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2586a94100faSBill Paul 	 */
2587deb5c680SPyun YongHyeon 	vlanctl = 0;
2588d65abd66SPyun YongHyeon 	csum_flags = 0;
2589d6d7d923SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2590d6d7d923SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2591d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND;
2592d6d7d923SPyun YongHyeon 			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2593d6d7d923SPyun YongHyeon 			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2594d6d7d923SPyun YongHyeon 		} else {
2595d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND |
2596d65abd66SPyun YongHyeon 			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2597d65abd66SPyun YongHyeon 			    RL_TDESC_CMD_MSSVAL_SHIFT);
2598d6d7d923SPyun YongHyeon 		}
2599d6d7d923SPyun YongHyeon 	} else {
260099c8ae87SPyun YongHyeon 		/*
260199c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
260299c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
260399c8ae87SPyun YongHyeon 		 * does't make effects.
260499c8ae87SPyun YongHyeon 		 */
260599c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2606deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2607d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2608deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2609deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2610d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2611deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2612deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2613d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2614deb5c680SPyun YongHyeon 			} else {
2615deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2616deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2617deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2618deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2619deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2620deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2621deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2622deb5c680SPyun YongHyeon 			}
2623d65abd66SPyun YongHyeon 		}
262499c8ae87SPyun YongHyeon 	}
2625a94100faSBill Paul 
2626ccf34c81SPyun YongHyeon 	/*
2627ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2628ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2629ccf34c81SPyun YongHyeon 	 * transmission attempt.
2630ccf34c81SPyun YongHyeon 	 */
2631ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2632bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2633deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2634ccf34c81SPyun YongHyeon 
2635d65abd66SPyun YongHyeon 	si = prod;
2636d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2637d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2638deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2639d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2640d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2641d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2642d65abd66SPyun YongHyeon 		if (i != 0)
2643d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2644d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2645d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2646d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2647d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2648d65abd66SPyun YongHyeon 	}
2649d65abd66SPyun YongHyeon 	/* Update producer index. */
2650d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2651a94100faSBill Paul 
2652d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2653d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2654d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2655d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2656d65abd66SPyun YongHyeon 
2657d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2658d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2659d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2660a94100faSBill Paul 
2661d65abd66SPyun YongHyeon 	/*
2662d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2663d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2664d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2665d65abd66SPyun YongHyeon 	 */
2666d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2667d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2668d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2669d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2670d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2671a94100faSBill Paul 
2672a94100faSBill Paul 	return (0);
2673a94100faSBill Paul }
2674a94100faSBill Paul 
267597b9d4baSJohn-Mark Gurney static void
26767b5ffebfSPyun YongHyeon re_tx_task(void *arg, int npending)
267797b9d4baSJohn-Mark Gurney {
2678ed510fb0SBill Paul 	struct ifnet		*ifp;
267997b9d4baSJohn-Mark Gurney 
2680ed510fb0SBill Paul 	ifp = arg;
2681ed510fb0SBill Paul 	re_start(ifp);
268297b9d4baSJohn-Mark Gurney }
268397b9d4baSJohn-Mark Gurney 
2684a94100faSBill Paul /*
2685a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2686a94100faSBill Paul  */
2687a94100faSBill Paul static void
26887b5ffebfSPyun YongHyeon re_start(struct ifnet *ifp)
2689a94100faSBill Paul {
2690a94100faSBill Paul 	struct rl_softc		*sc;
2691d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2692d65abd66SPyun YongHyeon 	int			queued;
2693a94100faSBill Paul 
2694a94100faSBill Paul 	sc = ifp->if_softc;
269597b9d4baSJohn-Mark Gurney 
2696ed510fb0SBill Paul 	RL_LOCK(sc);
2697ed510fb0SBill Paul 
2698d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2699351a76f9SPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0) {
2700ed510fb0SBill Paul 		RL_UNLOCK(sc);
2701ed510fb0SBill Paul 		return;
2702ed510fb0SBill Paul 	}
2703a94100faSBill Paul 
2704d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2705d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
270652732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2707a94100faSBill Paul 		if (m_head == NULL)
2708a94100faSBill Paul 			break;
2709a94100faSBill Paul 
2710d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2711b4b95879SMarius Strobl 			if (m_head == NULL)
2712b4b95879SMarius Strobl 				break;
271352732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
271413f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2715a94100faSBill Paul 			break;
2716a94100faSBill Paul 		}
2717a94100faSBill Paul 
2718a94100faSBill Paul 		/*
2719a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2720a94100faSBill Paul 		 * to him.
2721a94100faSBill Paul 		 */
272259a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
272352732175SMax Laier 
272452732175SMax Laier 		queued++;
2725a94100faSBill Paul 	}
2726a94100faSBill Paul 
2727ed510fb0SBill Paul 	if (queued == 0) {
2728ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2729d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2730ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2731ed510fb0SBill Paul #endif
2732ed510fb0SBill Paul 		RL_UNLOCK(sc);
273352732175SMax Laier 		return;
2734ed510fb0SBill Paul 	}
273552732175SMax Laier 
2736a94100faSBill Paul 	/* Flush the TX descriptors */
2737a94100faSBill Paul 
2738a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2739a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2740a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2741a94100faSBill Paul 
27420fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2743a94100faSBill Paul 
2744ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2745a94100faSBill Paul 	/*
2746a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2747a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2748a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2749a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2750a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2751a94100faSBill Paul 	 * the timer count is reset to 0.
2752a94100faSBill Paul 	 */
2753a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2754ed510fb0SBill Paul #endif
2755a94100faSBill Paul 
2756a94100faSBill Paul 	/*
2757a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2758a94100faSBill Paul 	 */
27591d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2760ed510fb0SBill Paul 
2761ed510fb0SBill Paul 	RL_UNLOCK(sc);
2762a94100faSBill Paul }
2763a94100faSBill Paul 
2764a94100faSBill Paul static void
276581eee0ebSPyun YongHyeon re_set_jumbo(struct rl_softc *sc, int jumbo)
276681eee0ebSPyun YongHyeon {
276781eee0ebSPyun YongHyeon 
276881eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
276981eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
277081eee0ebSPyun YongHyeon 		return;
277181eee0ebSPyun YongHyeon 	}
277281eee0ebSPyun YongHyeon 
277381eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
277481eee0ebSPyun YongHyeon 	if (jumbo != 0) {
277581eee0ebSPyun YongHyeon 		CSR_WRITE_1(sc, RL_CFG3, CSR_READ_1(sc, RL_CFG3) |
277681eee0ebSPyun YongHyeon 		    RL_CFG3_JUMBO_EN0);
277781eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
277881eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
277981eee0ebSPyun YongHyeon 			break;
278081eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
278181eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) |
278281eee0ebSPyun YongHyeon 			    0x01);
278381eee0ebSPyun YongHyeon 			break;
278481eee0ebSPyun YongHyeon 		default:
278581eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) |
278681eee0ebSPyun YongHyeon 			    RL_CFG4_JUMBO_EN1);
278781eee0ebSPyun YongHyeon 		}
278881eee0ebSPyun YongHyeon 	} else {
278981eee0ebSPyun YongHyeon 		CSR_WRITE_1(sc, RL_CFG3, CSR_READ_1(sc, RL_CFG3) &
279081eee0ebSPyun YongHyeon 		    ~RL_CFG3_JUMBO_EN0);
279181eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
279281eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
279381eee0ebSPyun YongHyeon 			break;
279481eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
279581eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) &
279681eee0ebSPyun YongHyeon 			    ~0x01);
279781eee0ebSPyun YongHyeon 			break;
279881eee0ebSPyun YongHyeon 		default:
279981eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) &
280081eee0ebSPyun YongHyeon 			    ~RL_CFG4_JUMBO_EN1);
280181eee0ebSPyun YongHyeon 		}
280281eee0ebSPyun YongHyeon 	}
280381eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
280481eee0ebSPyun YongHyeon 
280581eee0ebSPyun YongHyeon 	switch (sc->rl_hwrev->rl_rev) {
280681eee0ebSPyun YongHyeon 	case RL_HWREV_8168DP:
280781eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
280881eee0ebSPyun YongHyeon 		break;
280981eee0ebSPyun YongHyeon 	default:
281081eee0ebSPyun YongHyeon 		if (jumbo != 0)
281181eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 512);
281281eee0ebSPyun YongHyeon 		else
281381eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 4096);
281481eee0ebSPyun YongHyeon 	}
281581eee0ebSPyun YongHyeon }
281681eee0ebSPyun YongHyeon 
281781eee0ebSPyun YongHyeon static void
28187b5ffebfSPyun YongHyeon re_init(void *xsc)
2819a94100faSBill Paul {
2820a94100faSBill Paul 	struct rl_softc		*sc = xsc;
282197b9d4baSJohn-Mark Gurney 
282297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
282397b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
282497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
282597b9d4baSJohn-Mark Gurney }
282697b9d4baSJohn-Mark Gurney 
282797b9d4baSJohn-Mark Gurney static void
28287b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
282997b9d4baSJohn-Mark Gurney {
2830fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2831a94100faSBill Paul 	struct mii_data		*mii;
2832566ca8caSJung-uk Kim 	uint32_t		reg;
283370acaecfSPyun YongHyeon 	uint16_t		cfg;
28344d3d7085SBernd Walter 	union {
28354d3d7085SBernd Walter 		uint32_t align_dummy;
28364d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
28374d3d7085SBernd Walter         } eaddr;
2838a94100faSBill Paul 
283997b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
284097b9d4baSJohn-Mark Gurney 
2841a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2842a94100faSBill Paul 
28438476c243SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
28448476c243SPyun YongHyeon 		return;
28458476c243SPyun YongHyeon 
2846a94100faSBill Paul 	/*
2847a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2848a94100faSBill Paul 	 */
2849a94100faSBill Paul 	re_stop(sc);
2850a94100faSBill Paul 
2851b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
2852b659f1f0SPyun YongHyeon 	re_reset(sc);
2853b659f1f0SPyun YongHyeon 
2854a94100faSBill Paul 	/*
28554a814a5eSPyun YongHyeon 	 * For C+ mode, initialize the RX descriptors and mbufs.
28564a814a5eSPyun YongHyeon 	 */
285781eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
285881eee0ebSPyun YongHyeon 		if (ifp->if_mtu > RL_MTU) {
285981eee0ebSPyun YongHyeon 			if (re_jrx_list_init(sc) != 0) {
286081eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
286181eee0ebSPyun YongHyeon 				    "no memory for jumbo RX buffers\n");
286281eee0ebSPyun YongHyeon 				re_stop(sc);
286381eee0ebSPyun YongHyeon 				return;
286481eee0ebSPyun YongHyeon 			}
286581eee0ebSPyun YongHyeon 			/* Disable checksum offloading for jumbo frames. */
286681eee0ebSPyun YongHyeon 			ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
286781eee0ebSPyun YongHyeon 			ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
286881eee0ebSPyun YongHyeon 		} else {
286981eee0ebSPyun YongHyeon 			if (re_rx_list_init(sc) != 0) {
287081eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
287181eee0ebSPyun YongHyeon 				    "no memory for RX buffers\n");
287281eee0ebSPyun YongHyeon 				re_stop(sc);
287381eee0ebSPyun YongHyeon 				return;
287481eee0ebSPyun YongHyeon 			}
287581eee0ebSPyun YongHyeon 		}
287681eee0ebSPyun YongHyeon 		re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
287781eee0ebSPyun YongHyeon 	} else {
28784a814a5eSPyun YongHyeon 		if (re_rx_list_init(sc) != 0) {
28794a814a5eSPyun YongHyeon 			device_printf(sc->rl_dev, "no memory for RX buffers\n");
28804a814a5eSPyun YongHyeon 			re_stop(sc);
28814a814a5eSPyun YongHyeon 			return;
28824a814a5eSPyun YongHyeon 		}
288381eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
288481eee0ebSPyun YongHyeon 		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
288581eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
288681eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 512);
288781eee0ebSPyun YongHyeon 			else
288881eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 4096);
288981eee0ebSPyun YongHyeon 		}
289081eee0ebSPyun YongHyeon 	}
28914a814a5eSPyun YongHyeon 	re_tx_list_init(sc);
28924a814a5eSPyun YongHyeon 
28934a814a5eSPyun YongHyeon 	/*
2894c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2895edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2896c2c6548bSBill Paul 	 * before all others.
2897c2c6548bSBill Paul 	 */
289870acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
289970acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
290070acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
290170acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
290270acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
2903deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
2904deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
2905deb5c680SPyun YongHyeon 		/* XXX magic. */
2906deb5c680SPyun YongHyeon 		cfg |= 0x0001;
2907deb5c680SPyun YongHyeon 	} else
2908deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
2909deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
291081eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
291181eee0ebSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
2912566ca8caSJung-uk Kim 		reg = 0x000fff00;
2913566ca8caSJung-uk Kim 		if ((CSR_READ_1(sc, RL_CFG2) & RL_CFG2_PCI66MHZ) != 0)
2914566ca8caSJung-uk Kim 			reg |= 0x000000ff;
291581eee0ebSPyun YongHyeon 		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
2916566ca8caSJung-uk Kim 			reg |= 0x00f00000;
2917566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
2918566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
2919566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
2920566ca8caSJung-uk Kim 	}
2921ae644087SPyun YongHyeon 	/*
2922ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
2923ae644087SPyun YongHyeon 	 * allowed in controller.
2924ae644087SPyun YongHyeon 	 */
2925ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
2926ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
2927ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
2928ae644087SPyun YongHyeon 	}
2929c2c6548bSBill Paul 
2930c2c6548bSBill Paul 	/*
2931a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
2932a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
2933a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
2934a94100faSBill Paul 	 */
29354d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
29364d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
2937a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
2938ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
2939ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
2940ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
2941ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
2942a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
2943a94100faSBill Paul 
2944a94100faSBill Paul 	/*
2945d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
2946d01fac16SPyun YongHyeon 	 */
2947d01fac16SPyun YongHyeon 
2948d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
2949d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
2950d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
2951d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
2952d01fac16SPyun YongHyeon 
2953d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
2954d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
2955d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
2956d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
2957d01fac16SPyun YongHyeon 
2958d01fac16SPyun YongHyeon 	/*
2959a94100faSBill Paul 	 * Enable transmit and receive.
2960a94100faSBill Paul 	 */
2961a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
2962a94100faSBill Paul 
2963a94100faSBill Paul 	/*
2964ff191365SJung-uk Kim 	 * Set the initial TX configuration.
2965a94100faSBill Paul 	 */
2966abc8ff44SBill Paul 	if (sc->rl_testmode) {
2967abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
2968abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2969abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
2970a94100faSBill Paul 		else
2971abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
2972abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
2973abc8ff44SBill Paul 	} else
2974a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
2975d01fac16SPyun YongHyeon 
2976d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
2977d01fac16SPyun YongHyeon 
2978a94100faSBill Paul 	/*
2979ff191365SJung-uk Kim 	 * Set the initial RX configuration.
2980a94100faSBill Paul 	 */
2981ff191365SJung-uk Kim 	re_set_rxmode(sc);
2982a94100faSBill Paul 
2983483cc440SPyun YongHyeon 	/* Configure interrupt moderation. */
2984483cc440SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
2985483cc440SPyun YongHyeon 		/* Magic from vendor. */
29865e6906eeSPyun YongHyeon 		CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
2987483cc440SPyun YongHyeon 	}
2988483cc440SPyun YongHyeon 
2989a94100faSBill Paul #ifdef DEVICE_POLLING
2990a94100faSBill Paul 	/*
2991a94100faSBill Paul 	 * Disable interrupts if we are polling.
2992a94100faSBill Paul 	 */
299340929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
2994a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
2995a94100faSBill Paul 	else	/* otherwise ... */
299640929967SGleb Smirnoff #endif
2997ed510fb0SBill Paul 
2998a94100faSBill Paul 	/*
2999a94100faSBill Paul 	 * Enable interrupts.
3000a94100faSBill Paul 	 */
3001a94100faSBill Paul 	if (sc->rl_testmode)
3002a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3003a94100faSBill Paul 	else
3004a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3005ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3006a94100faSBill Paul 
3007a94100faSBill Paul 	/* Set initial TX threshold */
3008a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
3009a94100faSBill Paul 
3010a94100faSBill Paul 	/* Start RX/TX process. */
3011a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3012a94100faSBill Paul #ifdef notdef
3013a94100faSBill Paul 	/* Enable receiver and transmitter. */
3014a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3015a94100faSBill Paul #endif
3016a94100faSBill Paul 
3017ed510fb0SBill Paul #ifdef RE_TX_MODERATION
3018a94100faSBill Paul 	/*
3019a94100faSBill Paul 	 * Initialize the timer interrupt register so that
3020a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
3021a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
3022a94100faSBill Paul 	 * reloaded on each transmit. This gives us TX interrupt
3023a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
3024a94100faSBill Paul 	 */
3025a94100faSBill Paul 	if (sc->rl_type == RL_8169)
3026a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3027a94100faSBill Paul 	else
3028a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3029ed510fb0SBill Paul #endif
3030a94100faSBill Paul 
3031a94100faSBill Paul 	/*
3032a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
3033a94100faSBill Paul 	 * size so we can receive jumbo frames.
3034a94100faSBill Paul 	 */
303589feeee4SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
303681eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
303781eee0ebSPyun YongHyeon 			/*
303881eee0ebSPyun YongHyeon 			 * For controllers that use new jumbo frame scheme,
303981eee0ebSPyun YongHyeon 			 * set maximum size of jumbo frame depedning on
304081eee0ebSPyun YongHyeon 			 * controller revisions.
304181eee0ebSPyun YongHyeon 			 */
304281eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
304381eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
304481eee0ebSPyun YongHyeon 				    sc->rl_hwrev->rl_max_mtu +
304581eee0ebSPyun YongHyeon 				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
304681eee0ebSPyun YongHyeon 				    ETHER_CRC_LEN);
304789feeee4SPyun YongHyeon 			else
304881eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
304981eee0ebSPyun YongHyeon 				    RE_RX_DESC_BUFLEN);
305081eee0ebSPyun YongHyeon 		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
305181eee0ebSPyun YongHyeon 		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
305281eee0ebSPyun YongHyeon 			/* RTL810x has no jumbo frame support. */
305381eee0ebSPyun YongHyeon 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
305481eee0ebSPyun YongHyeon 		} else
3055a94100faSBill Paul 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
305689feeee4SPyun YongHyeon 	}
3057a94100faSBill Paul 
305897b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
3059a94100faSBill Paul 		return;
3060a94100faSBill Paul 
3061a94100faSBill Paul 	mii_mediachg(mii);
3062a94100faSBill Paul 
306319ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
3064a94100faSBill Paul 
306513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
306613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3067a94100faSBill Paul 
3068351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
30691d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3070d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3071a94100faSBill Paul }
3072a94100faSBill Paul 
3073a94100faSBill Paul /*
3074a94100faSBill Paul  * Set media options.
3075a94100faSBill Paul  */
3076a94100faSBill Paul static int
30777b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
3078a94100faSBill Paul {
3079a94100faSBill Paul 	struct rl_softc		*sc;
3080a94100faSBill Paul 	struct mii_data		*mii;
30816f0f9b12SPyun YongHyeon 	int			error;
3082a94100faSBill Paul 
3083a94100faSBill Paul 	sc = ifp->if_softc;
3084a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3085d1754a9bSJohn Baldwin 	RL_LOCK(sc);
30866f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
3087d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3088a94100faSBill Paul 
30896f0f9b12SPyun YongHyeon 	return (error);
3090a94100faSBill Paul }
3091a94100faSBill Paul 
3092a94100faSBill Paul /*
3093a94100faSBill Paul  * Report current media status.
3094a94100faSBill Paul  */
3095a94100faSBill Paul static void
30967b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3097a94100faSBill Paul {
3098a94100faSBill Paul 	struct rl_softc		*sc;
3099a94100faSBill Paul 	struct mii_data		*mii;
3100a94100faSBill Paul 
3101a94100faSBill Paul 	sc = ifp->if_softc;
3102a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3103a94100faSBill Paul 
3104d1754a9bSJohn Baldwin 	RL_LOCK(sc);
3105a94100faSBill Paul 	mii_pollstat(mii);
3106d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3107a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
3108a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3109a94100faSBill Paul }
3110a94100faSBill Paul 
3111a94100faSBill Paul static int
31127b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3113a94100faSBill Paul {
3114a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
3115a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
3116a94100faSBill Paul 	struct mii_data		*mii;
311740929967SGleb Smirnoff 	int			error = 0;
3118a94100faSBill Paul 
3119a94100faSBill Paul 	switch (command) {
3120a94100faSBill Paul 	case SIOCSIFMTU:
312181eee0ebSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN ||
312281eee0ebSPyun YongHyeon 		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu) {
3123c1d0b573SPyun YongHyeon 			error = EINVAL;
3124c1d0b573SPyun YongHyeon 			break;
3125c1d0b573SPyun YongHyeon 		}
3126c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
312781eee0ebSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
3128a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
312981eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
313081eee0ebSPyun YongHyeon 			    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
313181eee0ebSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
313281eee0ebSPyun YongHyeon 				re_init_locked(sc);
313381eee0ebSPyun YongHyeon 			}
3134ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3135ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
313681eee0ebSPyun YongHyeon 				ifp->if_capenable &= ~(IFCAP_TSO4 |
313781eee0ebSPyun YongHyeon 				    IFCAP_VLAN_HWTSO);
3138ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
313981eee0ebSPyun YongHyeon 			}
3140ecafbbb5SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
3141ae644087SPyun YongHyeon 		}
3142d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
3143a94100faSBill Paul 		break;
3144a94100faSBill Paul 	case SIOCSIFFLAGS:
314597b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
3146eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
3147eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3148eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
31493021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3150ff191365SJung-uk Kim 					re_set_rxmode(sc);
3151eed497bbSPyun YongHyeon 			} else
315297b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
3153eed497bbSPyun YongHyeon 		} else {
3154eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3155a94100faSBill Paul 				re_stop(sc);
3156eed497bbSPyun YongHyeon 		}
3157eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
315897b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3159a94100faSBill Paul 		break;
3160a94100faSBill Paul 	case SIOCADDMULTI:
3161a94100faSBill Paul 	case SIOCDELMULTI:
316297b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
31638476c243SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3164ff191365SJung-uk Kim 			re_set_rxmode(sc);
316597b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3166a94100faSBill Paul 		break;
3167a94100faSBill Paul 	case SIOCGIFMEDIA:
3168a94100faSBill Paul 	case SIOCSIFMEDIA:
3169a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
3170a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3171a94100faSBill Paul 		break;
3172a94100faSBill Paul 	case SIOCSIFCAP:
317340929967SGleb Smirnoff 	    {
3174f051cb85SGleb Smirnoff 		int mask, reinit;
3175f051cb85SGleb Smirnoff 
3176f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3177f051cb85SGleb Smirnoff 		reinit = 0;
317840929967SGleb Smirnoff #ifdef DEVICE_POLLING
317940929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
318040929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
318140929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
318240929967SGleb Smirnoff 				if (error)
318340929967SGleb Smirnoff 					return (error);
3184d1754a9bSJohn Baldwin 				RL_LOCK(sc);
318540929967SGleb Smirnoff 				/* Disable interrupts */
318640929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
318740929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
318840929967SGleb Smirnoff 				RL_UNLOCK(sc);
318940929967SGleb Smirnoff 			} else {
319040929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
319140929967SGleb Smirnoff 				/* Enable interrupts. */
319240929967SGleb Smirnoff 				RL_LOCK(sc);
319340929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
319440929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
319540929967SGleb Smirnoff 				RL_UNLOCK(sc);
319640929967SGleb Smirnoff 			}
319740929967SGleb Smirnoff 		}
319840929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3199d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
3200d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3201d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
3202d3b181aeSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
3203dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
3204a94100faSBill Paul 			else
3205b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3206f051cb85SGleb Smirnoff 			reinit = 1;
320740929967SGleb Smirnoff 		}
3208d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
3209d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3210d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
3211d3b181aeSPyun YongHyeon 			reinit = 1;
3212d3b181aeSPyun YongHyeon 		}
3213ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
3214ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO) != 0) {
3215dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
3216ecafbbb5SPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3217dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
3218dc74159dSPyun YongHyeon 			else
3219dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3220ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3221ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3222ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
3223ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3224ae644087SPyun YongHyeon 			}
3225dc74159dSPyun YongHyeon 		}
3226ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3227ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3228ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3229ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3230ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3231ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3232ecafbbb5SPyun YongHyeon 			/* TSO over VLAN requires VLAN hardware tagging. */
3233ecafbbb5SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3234ecafbbb5SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3235ecafbbb5SPyun YongHyeon 			reinit = 1;
3236ecafbbb5SPyun YongHyeon 		}
323781eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
323881eee0ebSPyun YongHyeon 		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
323981eee0ebSPyun YongHyeon 		    IFCAP_VLAN_HWTSO)) != 0)
324081eee0ebSPyun YongHyeon 				reinit = 1;
32417467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
32427467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
32437467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
32447467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
32457467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
32467467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
32477467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
32487467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
32497467bd53SPyun YongHyeon 		}
32508476c243SPyun YongHyeon 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
32518476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3252f051cb85SGleb Smirnoff 			re_init(sc);
32538476c243SPyun YongHyeon 		}
3254960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
325540929967SGleb Smirnoff 	    }
3256a94100faSBill Paul 		break;
3257a94100faSBill Paul 	default:
3258a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
3259a94100faSBill Paul 		break;
3260a94100faSBill Paul 	}
3261a94100faSBill Paul 
3262a94100faSBill Paul 	return (error);
3263a94100faSBill Paul }
3264a94100faSBill Paul 
3265a94100faSBill Paul static void
32667b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
32671d545c7aSMarius Strobl {
3268130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
3269a94100faSBill Paul 
32701d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
32711d545c7aSMarius Strobl 
32721d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
32731d545c7aSMarius Strobl 		return;
32741d545c7aSMarius Strobl 
3275130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
3276a94100faSBill Paul 	re_txeof(sc);
3277130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3278130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3279130b6dfbSPyun YongHyeon 		    "-- recovering\n");
3280130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3281130b6dfbSPyun YongHyeon 			taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
3282130b6dfbSPyun YongHyeon 		return;
3283130b6dfbSPyun YongHyeon 	}
3284130b6dfbSPyun YongHyeon 
3285130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
3286130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
3287130b6dfbSPyun YongHyeon 
32881abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
32898476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
329097b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
3291130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3292130b6dfbSPyun YongHyeon 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_txtask);
3293a94100faSBill Paul }
3294a94100faSBill Paul 
3295a94100faSBill Paul /*
3296a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
3297a94100faSBill Paul  * RX and TX lists.
3298a94100faSBill Paul  */
3299a94100faSBill Paul static void
33007b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
3301a94100faSBill Paul {
33020ce0868aSPyun YongHyeon 	int			i;
3303a94100faSBill Paul 	struct ifnet		*ifp;
3304d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
3305d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
3306a94100faSBill Paul 
330797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
330897b9d4baSJohn-Mark Gurney 
3309fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
3310a94100faSBill Paul 
33111d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3312d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
331313f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3314a94100faSBill Paul 
3315ead8fc66SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0)
3316ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3317ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
3318ead8fc66SPyun YongHyeon 	else
3319a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3320ead8fc66SPyun YongHyeon 	DELAY(1000);
3321a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3322ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3323a94100faSBill Paul 
3324a94100faSBill Paul 	if (sc->rl_head != NULL) {
3325a94100faSBill Paul 		m_freem(sc->rl_head);
3326a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
3327a94100faSBill Paul 	}
3328a94100faSBill Paul 
3329a94100faSBill Paul 	/* Free the TX list buffers. */
3330a94100faSBill Paul 
3331d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3332d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
3333d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
3334d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3335d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3336d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3337d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
3338d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
3339d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
3340a94100faSBill Paul 		}
3341a94100faSBill Paul 	}
3342a94100faSBill Paul 
3343a94100faSBill Paul 	/* Free the RX list buffers. */
3344a94100faSBill Paul 
3345d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3346d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
3347d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
3348d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3349d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3350d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3351d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
3352d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
3353d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
3354a94100faSBill Paul 		}
3355a94100faSBill Paul 	}
3356a94100faSBill Paul }
3357a94100faSBill Paul 
3358a94100faSBill Paul /*
3359a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
3360a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
3361a94100faSBill Paul  * resume.
3362a94100faSBill Paul  */
3363a94100faSBill Paul static int
33647b5ffebfSPyun YongHyeon re_suspend(device_t dev)
3365a94100faSBill Paul {
3366a94100faSBill Paul 	struct rl_softc		*sc;
3367a94100faSBill Paul 
3368a94100faSBill Paul 	sc = device_get_softc(dev);
3369a94100faSBill Paul 
337097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3371a94100faSBill Paul 	re_stop(sc);
33727467bd53SPyun YongHyeon 	re_setwol(sc);
3373a94100faSBill Paul 	sc->suspended = 1;
337497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3375a94100faSBill Paul 
3376a94100faSBill Paul 	return (0);
3377a94100faSBill Paul }
3378a94100faSBill Paul 
3379a94100faSBill Paul /*
3380a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
3381a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
3382a94100faSBill Paul  * appropriate.
3383a94100faSBill Paul  */
3384a94100faSBill Paul static int
33857b5ffebfSPyun YongHyeon re_resume(device_t dev)
3386a94100faSBill Paul {
3387a94100faSBill Paul 	struct rl_softc		*sc;
3388a94100faSBill Paul 	struct ifnet		*ifp;
3389a94100faSBill Paul 
3390a94100faSBill Paul 	sc = device_get_softc(dev);
339197b9d4baSJohn-Mark Gurney 
339297b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
339397b9d4baSJohn-Mark Gurney 
3394fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
339561f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
339661f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
339761f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
339861f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
339961f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
340061f45a72SPyun YongHyeon 	}
3401a94100faSBill Paul 
34027467bd53SPyun YongHyeon 	/*
34037467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
34047467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
34057467bd53SPyun YongHyeon 	 */
34067467bd53SPyun YongHyeon 	re_clrwol(sc);
340701d1a6c3SPyun YongHyeon 
340801d1a6c3SPyun YongHyeon 	/* reinitialize interface if necessary */
340901d1a6c3SPyun YongHyeon 	if (ifp->if_flags & IFF_UP)
341001d1a6c3SPyun YongHyeon 		re_init_locked(sc);
341101d1a6c3SPyun YongHyeon 
3412a94100faSBill Paul 	sc->suspended = 0;
341397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3414a94100faSBill Paul 
3415a94100faSBill Paul 	return (0);
3416a94100faSBill Paul }
3417a94100faSBill Paul 
3418a94100faSBill Paul /*
3419a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3420a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3421a94100faSBill Paul  */
34226a087a87SPyun YongHyeon static int
34237b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3424a94100faSBill Paul {
3425a94100faSBill Paul 	struct rl_softc		*sc;
3426a94100faSBill Paul 
3427a94100faSBill Paul 	sc = device_get_softc(dev);
3428a94100faSBill Paul 
342997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3430a94100faSBill Paul 	re_stop(sc);
3431536fde34SMaxim Sobolev 	/*
3432536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3433536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
343472293673SRuslan Ermilov 	 * cases.
3435536fde34SMaxim Sobolev 	 */
3436536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
34377467bd53SPyun YongHyeon 	re_setwol(sc);
343897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
34396a087a87SPyun YongHyeon 
34406a087a87SPyun YongHyeon 	return (0);
3441a94100faSBill Paul }
34427467bd53SPyun YongHyeon 
34437467bd53SPyun YongHyeon static void
34447b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
34457467bd53SPyun YongHyeon {
34467467bd53SPyun YongHyeon 	struct ifnet		*ifp;
34477467bd53SPyun YongHyeon 	int			pmc;
34487467bd53SPyun YongHyeon 	uint16_t		pmstat;
34497467bd53SPyun YongHyeon 	uint8_t			v;
34507467bd53SPyun YongHyeon 
34517467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
34527467bd53SPyun YongHyeon 
34537467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
34547467bd53SPyun YongHyeon 		return;
34557467bd53SPyun YongHyeon 
34567467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
345761f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
345861f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
345961f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
346061f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
346161f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
346261f45a72SPyun YongHyeon 	}
3463886ff602SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3464886ff602SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3465886ff602SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
34667467bd53SPyun YongHyeon 	/* Enable config register write. */
34677467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
34687467bd53SPyun YongHyeon 
34697467bd53SPyun YongHyeon 	/* Enable PME. */
34707467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
34717467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
34727467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
34737467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
34747467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
34757467bd53SPyun YongHyeon 
34767467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
34777467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
34787467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
34797467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
34807467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
34817467bd53SPyun YongHyeon 
34827467bd53SPyun YongHyeon 	/* Config register write done. */
3483f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
34847467bd53SPyun YongHyeon 
34857467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
34867467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
34877467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
34887467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
34897467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
34907467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
34917467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
34927467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
34937467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
34947467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
34957467bd53SPyun YongHyeon 
3496d0c45156SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3497d0c45156SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3498d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
34997467bd53SPyun YongHyeon 	/*
35007467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
35017467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
35027467bd53SPyun YongHyeon 	 * needed.
35037467bd53SPyun YongHyeon 	 */
35047467bd53SPyun YongHyeon 
35057467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
35067467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
35077467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
35087467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
35097467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
35107467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
35117467bd53SPyun YongHyeon }
35127467bd53SPyun YongHyeon 
35137467bd53SPyun YongHyeon static void
35147b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
35157467bd53SPyun YongHyeon {
35167467bd53SPyun YongHyeon 	int			pmc;
35177467bd53SPyun YongHyeon 	uint8_t			v;
35187467bd53SPyun YongHyeon 
35197467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
35207467bd53SPyun YongHyeon 
35217467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
35227467bd53SPyun YongHyeon 		return;
35237467bd53SPyun YongHyeon 
35247467bd53SPyun YongHyeon 	/* Enable config register write. */
35257467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
35267467bd53SPyun YongHyeon 
35277467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
35287467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
35297467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
35307467bd53SPyun YongHyeon 
35317467bd53SPyun YongHyeon 	/* Config register write done. */
3532f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
35337467bd53SPyun YongHyeon 
35347467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
35357467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
35367467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
35377467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
35387467bd53SPyun YongHyeon }
35390534aae0SPyun YongHyeon 
35400534aae0SPyun YongHyeon static void
35410534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc)
35420534aae0SPyun YongHyeon {
35430534aae0SPyun YongHyeon 	struct sysctl_ctx_list	*ctx;
35440534aae0SPyun YongHyeon 	struct sysctl_oid_list	*children;
35450534aae0SPyun YongHyeon 
35460534aae0SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->rl_dev);
35470534aae0SPyun YongHyeon 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
35480534aae0SPyun YongHyeon 
35490534aae0SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
35500534aae0SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I",
35510534aae0SPyun YongHyeon 	    "Statistics Information");
35520534aae0SPyun YongHyeon }
35530534aae0SPyun YongHyeon 
35540534aae0SPyun YongHyeon static int
35550534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS)
35560534aae0SPyun YongHyeon {
35570534aae0SPyun YongHyeon 	struct rl_softc		*sc;
35580534aae0SPyun YongHyeon 	struct rl_stats		*stats;
35590534aae0SPyun YongHyeon 	int			error, i, result;
35600534aae0SPyun YongHyeon 
35610534aae0SPyun YongHyeon 	result = -1;
35620534aae0SPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
35630534aae0SPyun YongHyeon 	if (error || req->newptr == NULL)
35640534aae0SPyun YongHyeon 		return (error);
35650534aae0SPyun YongHyeon 
35660534aae0SPyun YongHyeon 	if (result == 1) {
35670534aae0SPyun YongHyeon 		sc = (struct rl_softc *)arg1;
35680534aae0SPyun YongHyeon 		RL_LOCK(sc);
356916a4824bSPyun YongHyeon 		if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
357016a4824bSPyun YongHyeon 			RL_UNLOCK(sc);
357116a4824bSPyun YongHyeon 			goto done;
357216a4824bSPyun YongHyeon 		}
35730534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
35740534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
35750534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
35760534aae0SPyun YongHyeon 		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
35770534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
35780534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
35790534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
35800534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
35810534aae0SPyun YongHyeon 		    RL_DUMPSTATS_START));
35820534aae0SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
35830534aae0SPyun YongHyeon 			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
35840534aae0SPyun YongHyeon 			    RL_DUMPSTATS_START) == 0)
35850534aae0SPyun YongHyeon 				break;
35860534aae0SPyun YongHyeon 			DELAY(1000);
35870534aae0SPyun YongHyeon 		}
35880534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
35890534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
35900534aae0SPyun YongHyeon 		RL_UNLOCK(sc);
35910534aae0SPyun YongHyeon 		if (i == 0) {
35920534aae0SPyun YongHyeon 			device_printf(sc->rl_dev,
35930534aae0SPyun YongHyeon 			    "DUMP statistics request timedout\n");
35940534aae0SPyun YongHyeon 			return (ETIMEDOUT);
35950534aae0SPyun YongHyeon 		}
359616a4824bSPyun YongHyeon done:
35970534aae0SPyun YongHyeon 		stats = sc->rl_ldata.rl_stats;
35980534aae0SPyun YongHyeon 		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
35990534aae0SPyun YongHyeon 		printf("Tx frames : %ju\n",
36000534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_pkts));
36010534aae0SPyun YongHyeon 		printf("Rx frames : %ju\n",
36020534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_pkts));
36030534aae0SPyun YongHyeon 		printf("Tx errors : %ju\n",
36040534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_errs));
36050534aae0SPyun YongHyeon 		printf("Rx errors : %u\n",
36060534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_errs));
36070534aae0SPyun YongHyeon 		printf("Rx missed frames : %u\n",
36080534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_missed_pkts));
36090534aae0SPyun YongHyeon 		printf("Rx frame alignment errs : %u\n",
36100534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
36110534aae0SPyun YongHyeon 		printf("Tx single collisions : %u\n",
36120534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_onecoll));
36130534aae0SPyun YongHyeon 		printf("Tx multiple collisions : %u\n",
36140534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_multicolls));
36150534aae0SPyun YongHyeon 		printf("Rx unicast frames : %ju\n",
36160534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
36170534aae0SPyun YongHyeon 		printf("Rx broadcast frames : %ju\n",
36180534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
36190534aae0SPyun YongHyeon 		printf("Rx multicast frames : %u\n",
36200534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_mcasts));
36210534aae0SPyun YongHyeon 		printf("Tx aborts : %u\n",
36220534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_tx_aborts));
36230534aae0SPyun YongHyeon 		printf("Tx underruns : %u\n",
36240534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_underruns));
36250534aae0SPyun YongHyeon 	}
36260534aae0SPyun YongHyeon 
36270534aae0SPyun YongHyeon 	return (error);
36280534aae0SPyun YongHyeon }
3629