xref: /freebsd/sys/dev/re/if_re.c (revision 3c2a957d2d38dcc9d72fed5d50bdaaeaa40f27cd)
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. */
160502be0f7SPyun YongHyeon static int intr_filter = 0;
161502be0f7SPyun YongHyeon TUNABLE_INT("hw.re.intr_filter", &intr_filter);
162c2d2e19cSPyun YongHyeon static int msi_disable = 0;
1635774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1644a58fd45SPyun YongHyeon static int msix_disable = 0;
1654a58fd45SPyun YongHyeon TUNABLE_INT("hw.re.msix_disable", &msix_disable);
1662c21710bSPyun YongHyeon static int prefer_iomap = 0;
1672c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
1685774c5ffSPyun YongHyeon 
169a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
170a94100faSBill Paul 
171a94100faSBill Paul /*
172a94100faSBill Paul  * Various supported device vendors/types and their names.
173a94100faSBill Paul  */
17429658c96SDimitry Andric static const struct rl_type re_devs[] = {
1759dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
17632aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
177caa19d50SPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_530T_REVC, 0,
178caa19d50SPyun YongHyeon 	    "D-Link DGE-530(T) Gigabit Ethernet Adapter" },
1799dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
180a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1819dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
18254899a96SPyun YongHyeon 	    "RealTek 810xE PCIe 10/100baseTX" },
1839dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
184d467ffaaSPyun YongHyeon 	    "RealTek 8168/8111 B/C/CP/D/DP/E/F PCIe Gigabit Ethernet" },
1859dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
186715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1879dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1882ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1899dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
190ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1919dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
19226390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1939dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
194dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
195a94100faSBill Paul };
196a94100faSBill Paul 
19729658c96SDimitry Andric static const struct rl_hwrev re_hwrevs[] = {
19881eee0ebSPyun YongHyeon 	{ RL_HWREV_8139, RL_8139, "", RL_MTU },
19981eee0ebSPyun YongHyeon 	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
20081eee0ebSPyun YongHyeon 	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
20181eee0ebSPyun YongHyeon 	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
20281eee0ebSPyun YongHyeon 	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
20381eee0ebSPyun YongHyeon 	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
20481eee0ebSPyun YongHyeon 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
20581eee0ebSPyun YongHyeon 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
206ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
20781eee0ebSPyun YongHyeon 	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
20881eee0ebSPyun YongHyeon 	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
20981eee0ebSPyun YongHyeon 	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
21081eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
21181eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21281eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
21381eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21481eee0ebSPyun YongHyeon 	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
21581eee0ebSPyun YongHyeon 	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
21681eee0ebSPyun YongHyeon 	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
21781eee0ebSPyun YongHyeon 	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
21881eee0ebSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
21981eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
22081eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
22181eee0ebSPyun YongHyeon 	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
22239e69201SPyun YongHyeon 	{ RL_HWREV_8401E, RL_8169, "8401E", RL_MTU },
223a9e3362aSPyun YongHyeon 	{ RL_HWREV_8402, RL_8169, "8402", RL_MTU },
22454899a96SPyun YongHyeon 	{ RL_HWREV_8105E, RL_8169, "8105E", RL_MTU },
2256b0a8e04SPyun YongHyeon 	{ RL_HWREV_8105E_SPIN1, RL_8169, "8105E", RL_MTU },
226ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
227ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
22881eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
22981eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
23081eee0ebSPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
23181eee0ebSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
23281eee0ebSPyun YongHyeon 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
23381eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
23481eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
235d467ffaaSPyun YongHyeon 	{ RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K},
236d56f7f52SPyun YongHyeon 	{ RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K},
23781eee0ebSPyun YongHyeon 	{ 0, 0, NULL, 0 }
238a94100faSBill Paul };
239a94100faSBill Paul 
240a94100faSBill Paul static int re_probe		(device_t);
241a94100faSBill Paul static int re_attach		(device_t);
242a94100faSBill Paul static int re_detach		(device_t);
243a94100faSBill Paul 
244d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
245a94100faSBill Paul 
246a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
247a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
248d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
249d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
250d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
25181eee0ebSPyun YongHyeon static int re_jumbo_newbuf	(struct rl_softc *, int);
252a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
25381eee0ebSPyun YongHyeon static int re_jrx_list_init	(struct rl_softc *);
254a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
25522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
25622a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
25722a11c96SJohn-Mark Gurney 				(struct mbuf *);
25822a11c96SJohn-Mark Gurney #endif
2591abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
260a94100faSBill Paul static void re_txeof		(struct rl_softc *);
26197b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2621abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2631abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
26497b9d4baSJohn-Mark Gurney #endif
265ef544f63SPaolo Pisati static int re_intr		(void *);
266502be0f7SPyun YongHyeon static void re_intr_msi		(void *);
267a94100faSBill Paul static void re_tick		(void *);
268ed510fb0SBill Paul static void re_int_task		(void *, int);
269a94100faSBill Paul static void re_start		(struct ifnet *);
270d180a66fSPyun YongHyeon static void re_start_locked	(struct ifnet *);
271a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
272a94100faSBill Paul static void re_init		(void *);
27397b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
274a94100faSBill Paul static void re_stop		(struct rl_softc *);
2751d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
276a94100faSBill Paul static int re_suspend		(device_t);
277a94100faSBill Paul static int re_resume		(device_t);
2786a087a87SPyun YongHyeon static int re_shutdown		(device_t);
279a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
280a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
281a94100faSBill Paul 
282a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
283a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
284ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
285a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
286a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
287a94100faSBill Paul 
288a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
289a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
290a94100faSBill Paul static void re_miibus_statchg	(device_t);
291a94100faSBill Paul 
29281eee0ebSPyun YongHyeon static void re_set_jumbo	(struct rl_softc *, int);
293ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
294a94100faSBill Paul static void re_reset		(struct rl_softc *);
2957467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2967467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
2976830588dSPyun YongHyeon static void re_set_linkspeed	(struct rl_softc *);
298a94100faSBill Paul 
299579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP	/* see ixgbe.c for details */
300579a6e3cSLuigi Rizzo #include <dev/netmap/if_re_netmap.h>
301579a6e3cSLuigi Rizzo #endif /* !DEV_NETMAP */
302579a6e3cSLuigi Rizzo 
303ed510fb0SBill Paul #ifdef RE_DIAG
304a94100faSBill Paul static int re_diag		(struct rl_softc *);
305ed510fb0SBill Paul #endif
306a94100faSBill Paul 
3070534aae0SPyun YongHyeon static void re_add_sysctls	(struct rl_softc *);
3080534aae0SPyun YongHyeon static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
309502be0f7SPyun YongHyeon static int sysctl_int_range	(SYSCTL_HANDLER_ARGS, int, int);
310502be0f7SPyun YongHyeon static int sysctl_hw_re_int_mod	(SYSCTL_HANDLER_ARGS);
3110534aae0SPyun YongHyeon 
312a94100faSBill Paul static device_method_t re_methods[] = {
313a94100faSBill Paul 	/* Device interface */
314a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
315a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
316a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
317a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
318a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
319a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
320a94100faSBill Paul 
321a94100faSBill Paul 	/* MII interface */
322a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
323a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
324a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
325a94100faSBill Paul 
3264b7ec270SMarius Strobl 	DEVMETHOD_END
327a94100faSBill Paul };
328a94100faSBill Paul 
329a94100faSBill Paul static driver_t re_driver = {
330a94100faSBill Paul 	"re",
331a94100faSBill Paul 	re_methods,
332a94100faSBill Paul 	sizeof(struct rl_softc)
333a94100faSBill Paul };
334a94100faSBill Paul 
335a94100faSBill Paul static devclass_t re_devclass;
336a94100faSBill Paul 
337a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
338a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
339a94100faSBill Paul 
340a94100faSBill Paul #define EE_SET(x)					\
341a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
342a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
343a94100faSBill Paul 
344a94100faSBill Paul #define EE_CLR(x)					\
345a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
346a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
347a94100faSBill Paul 
348a94100faSBill Paul /*
349a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
350a94100faSBill Paul  */
351a94100faSBill Paul static void
3527b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
353a94100faSBill Paul {
3540ce0868aSPyun YongHyeon 	int			d, i;
355a94100faSBill Paul 
356ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
357a94100faSBill Paul 
358a94100faSBill Paul 	/*
359a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
360a94100faSBill Paul 	 */
361ed510fb0SBill Paul 
362ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
363a94100faSBill Paul 		if (d & i) {
364a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
365a94100faSBill Paul 		} else {
366a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
367a94100faSBill Paul 		}
368a94100faSBill Paul 		DELAY(100);
369a94100faSBill Paul 		EE_SET(RL_EE_CLK);
370a94100faSBill Paul 		DELAY(150);
371a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
372a94100faSBill Paul 		DELAY(100);
373a94100faSBill Paul 	}
374a94100faSBill Paul }
375a94100faSBill Paul 
376a94100faSBill Paul /*
377a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
378a94100faSBill Paul  */
379a94100faSBill Paul static void
3807b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
381a94100faSBill Paul {
3820ce0868aSPyun YongHyeon 	int			i;
383a94100faSBill Paul 	u_int16_t		word = 0;
384a94100faSBill Paul 
385a94100faSBill Paul 	/*
386a94100faSBill Paul 	 * Send address of word we want to read.
387a94100faSBill Paul 	 */
388a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
389a94100faSBill Paul 
390a94100faSBill Paul 	/*
391a94100faSBill Paul 	 * Start reading bits from EEPROM.
392a94100faSBill Paul 	 */
393a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
394a94100faSBill Paul 		EE_SET(RL_EE_CLK);
395a94100faSBill Paul 		DELAY(100);
396a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
397a94100faSBill Paul 			word |= i;
398a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
399a94100faSBill Paul 		DELAY(100);
400a94100faSBill Paul 	}
401a94100faSBill Paul 
402a94100faSBill Paul 	*dest = word;
403a94100faSBill Paul }
404a94100faSBill Paul 
405a94100faSBill Paul /*
406a94100faSBill Paul  * Read a sequence of words from the EEPROM.
407a94100faSBill Paul  */
408a94100faSBill Paul static void
4097b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
410a94100faSBill Paul {
411a94100faSBill Paul 	int			i;
412a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
413a94100faSBill Paul 
414ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
415ed510fb0SBill Paul 
416ed510fb0SBill Paul         DELAY(100);
417ed510fb0SBill Paul 
418a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
419ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
420a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
421ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
422a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
423be099007SPyun YongHyeon                 *ptr = word;
424a94100faSBill Paul 	}
425ed510fb0SBill Paul 
426ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
427a94100faSBill Paul }
428a94100faSBill Paul 
429a94100faSBill Paul static int
4307b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
431a94100faSBill Paul {
432a94100faSBill Paul 	struct rl_softc		*sc;
433a94100faSBill Paul 	u_int32_t		rval;
434a94100faSBill Paul 	int			i;
435a94100faSBill Paul 
436a94100faSBill Paul 	sc = device_get_softc(dev);
437a94100faSBill Paul 
4389bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4399bac70b8SBill Paul 
4409bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4419bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4429bac70b8SBill Paul 		return (rval);
4439bac70b8SBill Paul 	}
4449bac70b8SBill Paul 
445a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
446a94100faSBill Paul 
44796b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
448a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
449a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
450a94100faSBill Paul 			break;
4512bc085c6SPyun YongHyeon 		DELAY(25);
452a94100faSBill Paul 	}
453a94100faSBill Paul 
45496b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4556b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
456a94100faSBill Paul 		return (0);
457a94100faSBill Paul 	}
458a94100faSBill Paul 
4592bc085c6SPyun YongHyeon 	/*
4602bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4612bc085c6SPyun YongHyeon 	 */
4622bc085c6SPyun YongHyeon 	DELAY(20);
4632bc085c6SPyun YongHyeon 
464a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
465a94100faSBill Paul }
466a94100faSBill Paul 
467a94100faSBill Paul static int
4687b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
469a94100faSBill Paul {
470a94100faSBill Paul 	struct rl_softc		*sc;
471a94100faSBill Paul 	u_int32_t		rval;
472a94100faSBill Paul 	int			i;
473a94100faSBill Paul 
474a94100faSBill Paul 	sc = device_get_softc(dev);
475a94100faSBill Paul 
476a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4779bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
478a94100faSBill Paul 
47996b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
480a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
481a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
482a94100faSBill Paul 			break;
4832bc085c6SPyun YongHyeon 		DELAY(25);
484a94100faSBill Paul 	}
485a94100faSBill Paul 
48696b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4876b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
488a94100faSBill Paul 		return (0);
489a94100faSBill Paul 	}
490a94100faSBill Paul 
4912bc085c6SPyun YongHyeon 	/*
4922bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4932bc085c6SPyun YongHyeon 	 */
4942bc085c6SPyun YongHyeon 	DELAY(20);
4952bc085c6SPyun YongHyeon 
496a94100faSBill Paul 	return (0);
497a94100faSBill Paul }
498a94100faSBill Paul 
499a94100faSBill Paul static int
5007b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
501a94100faSBill Paul {
502a94100faSBill Paul 	struct rl_softc		*sc;
503a94100faSBill Paul 	u_int16_t		rval = 0;
504a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
505a94100faSBill Paul 
506a94100faSBill Paul 	sc = device_get_softc(dev);
507a94100faSBill Paul 
508a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
509a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
510a94100faSBill Paul 		return (rval);
511a94100faSBill Paul 	}
512a94100faSBill Paul 
513a94100faSBill Paul 	switch (reg) {
514a94100faSBill Paul 	case MII_BMCR:
515a94100faSBill Paul 		re8139_reg = RL_BMCR;
516a94100faSBill Paul 		break;
517a94100faSBill Paul 	case MII_BMSR:
518a94100faSBill Paul 		re8139_reg = RL_BMSR;
519a94100faSBill Paul 		break;
520a94100faSBill Paul 	case MII_ANAR:
521a94100faSBill Paul 		re8139_reg = RL_ANAR;
522a94100faSBill Paul 		break;
523a94100faSBill Paul 	case MII_ANER:
524a94100faSBill Paul 		re8139_reg = RL_ANER;
525a94100faSBill Paul 		break;
526a94100faSBill Paul 	case MII_ANLPAR:
527a94100faSBill Paul 		re8139_reg = RL_LPAR;
528a94100faSBill Paul 		break;
529a94100faSBill Paul 	case MII_PHYIDR1:
530a94100faSBill Paul 	case MII_PHYIDR2:
531a94100faSBill Paul 		return (0);
532a94100faSBill Paul 	/*
533a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
534a94100faSBill Paul 	 * register. If we have a link partner which does not
535a94100faSBill Paul 	 * support NWAY, this is the register which will tell
536a94100faSBill Paul 	 * us the results of parallel detection.
537a94100faSBill Paul 	 */
538a94100faSBill Paul 	case RL_MEDIASTAT:
539a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
540a94100faSBill Paul 		return (rval);
541a94100faSBill Paul 	default:
5426b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
543a94100faSBill Paul 		return (0);
544a94100faSBill Paul 	}
545a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
546baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
547baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
548baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
549baa12772SPyun YongHyeon 	}
550a94100faSBill Paul 	return (rval);
551a94100faSBill Paul }
552a94100faSBill Paul 
553a94100faSBill Paul static int
5547b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
555a94100faSBill Paul {
556a94100faSBill Paul 	struct rl_softc		*sc;
557a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
558a94100faSBill Paul 	int			rval = 0;
559a94100faSBill Paul 
560a94100faSBill Paul 	sc = device_get_softc(dev);
561a94100faSBill Paul 
562a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
563a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
564a94100faSBill Paul 		return (rval);
565a94100faSBill Paul 	}
566a94100faSBill Paul 
567a94100faSBill Paul 	switch (reg) {
568a94100faSBill Paul 	case MII_BMCR:
569a94100faSBill Paul 		re8139_reg = RL_BMCR;
570baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
571baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
572baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
573baa12772SPyun YongHyeon 		}
574a94100faSBill Paul 		break;
575a94100faSBill Paul 	case MII_BMSR:
576a94100faSBill Paul 		re8139_reg = RL_BMSR;
577a94100faSBill Paul 		break;
578a94100faSBill Paul 	case MII_ANAR:
579a94100faSBill Paul 		re8139_reg = RL_ANAR;
580a94100faSBill Paul 		break;
581a94100faSBill Paul 	case MII_ANER:
582a94100faSBill Paul 		re8139_reg = RL_ANER;
583a94100faSBill Paul 		break;
584a94100faSBill Paul 	case MII_ANLPAR:
585a94100faSBill Paul 		re8139_reg = RL_LPAR;
586a94100faSBill Paul 		break;
587a94100faSBill Paul 	case MII_PHYIDR1:
588a94100faSBill Paul 	case MII_PHYIDR2:
589a94100faSBill Paul 		return (0);
590a94100faSBill Paul 		break;
591a94100faSBill Paul 	default:
5926b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
593a94100faSBill Paul 		return (0);
594a94100faSBill Paul 	}
595a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
596a94100faSBill Paul 	return (0);
597a94100faSBill Paul }
598a94100faSBill Paul 
599a94100faSBill Paul static void
6007b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
601a94100faSBill Paul {
602130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
603130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
604130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
605a11e2f18SBruce M Simpson 
606130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
607130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
608130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
609130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
610130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
611130b6dfbSPyun YongHyeon 		return;
612130b6dfbSPyun YongHyeon 
613130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
614130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
615130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
616130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
617130b6dfbSPyun YongHyeon 		case IFM_10_T:
618130b6dfbSPyun YongHyeon 		case IFM_100_TX:
619130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
620130b6dfbSPyun YongHyeon 			break;
621130b6dfbSPyun YongHyeon 		case IFM_1000_T:
622130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
623130b6dfbSPyun YongHyeon 				break;
624130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
625130b6dfbSPyun YongHyeon 			break;
626130b6dfbSPyun YongHyeon 		default:
627130b6dfbSPyun YongHyeon 			break;
628130b6dfbSPyun YongHyeon 		}
629130b6dfbSPyun YongHyeon 	}
630130b6dfbSPyun YongHyeon 	/*
631130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
632130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
633130b6dfbSPyun YongHyeon 	 * parameters.
634130b6dfbSPyun YongHyeon 	 */
635a94100faSBill Paul }
636a94100faSBill Paul 
637a94100faSBill Paul /*
638ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
639a94100faSBill Paul  */
640a94100faSBill Paul static void
641ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
642a94100faSBill Paul {
643a94100faSBill Paul 	struct ifnet		*ifp;
644a94100faSBill Paul 	struct ifmultiaddr	*ifma;
645ff191365SJung-uk Kim 	uint32_t		hashes[2] = { 0, 0 };
646ff191365SJung-uk Kim 	uint32_t		h, rxfilt;
647a94100faSBill Paul 
64897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
64997b9d4baSJohn-Mark Gurney 
650fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
651a94100faSBill Paul 
652ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
653a94100faSBill Paul 
654ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6557c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6567c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
657a0637caaSPyun YongHyeon 		/*
658a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
659a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
660a0637caaSPyun YongHyeon 		 * promiscuous mode.
661a0637caaSPyun YongHyeon 		 */
662a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
663ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
664ff191365SJung-uk Kim 		goto done;
665a94100faSBill Paul 	}
666a94100faSBill Paul 
667eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
668a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
669a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
670a94100faSBill Paul 			continue;
6710e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6720e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
673a94100faSBill Paul 		if (h < 32)
674a94100faSBill Paul 			hashes[0] |= (1 << h);
675a94100faSBill Paul 		else
676a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
677a94100faSBill Paul 	}
678eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
679a94100faSBill Paul 
680ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
681bb7dfefbSBill Paul 		/*
682ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
683ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
684ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
685ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
686ff191365SJung-uk Kim 		 * devices.
687bb7dfefbSBill Paul 		 */
688aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
689ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
690ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
691ff191365SJung-uk Kim 			hashes[1] = h;
692ff191365SJung-uk Kim 		}
693ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
694ff191365SJung-uk Kim 	}
695ff191365SJung-uk Kim 
696ff191365SJung-uk Kim done:
697a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
698a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
699ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
700bb7dfefbSBill Paul }
701a94100faSBill Paul 
702a94100faSBill Paul static void
7037b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
704a94100faSBill Paul {
7050ce0868aSPyun YongHyeon 	int			i;
706a94100faSBill Paul 
70797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
70897b9d4baSJohn-Mark Gurney 
709a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
710a94100faSBill Paul 
711a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
712a94100faSBill Paul 		DELAY(10);
713a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
714a94100faSBill Paul 			break;
715a94100faSBill Paul 	}
716a94100faSBill Paul 	if (i == RL_TIMEOUT)
7176b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
718a94100faSBill Paul 
719566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
720a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
72181eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
722566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
723a94100faSBill Paul }
724a94100faSBill Paul 
725ed510fb0SBill Paul #ifdef RE_DIAG
726ed510fb0SBill Paul 
727a94100faSBill Paul /*
728a94100faSBill Paul  * The following routine is designed to test for a defect on some
729a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
730a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
731a94100faSBill Paul  * should be pulled high. The result of this defect is that the
732a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
733a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
734a94100faSBill Paul  * because the 64-bit data lines aren't connected.
735a94100faSBill Paul  *
736a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
737a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
738a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
739a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
740a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
741a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
742a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
743a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
744a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
745a94100faSBill Paul  */
746a94100faSBill Paul 
747a94100faSBill Paul static int
7487b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
749a94100faSBill Paul {
750fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
751a94100faSBill Paul 	struct mbuf		*m0;
752a94100faSBill Paul 	struct ether_header	*eh;
753a94100faSBill Paul 	struct rl_desc		*cur_rx;
754a94100faSBill Paul 	u_int16_t		status;
755a94100faSBill Paul 	u_int32_t		rxstat;
756ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
757a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
758a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
759a94100faSBill Paul 
760a94100faSBill Paul 	/* Allocate a single mbuf */
761c6499eccSGleb Smirnoff 	MGETHDR(m0, M_NOWAIT, MT_DATA);
762a94100faSBill Paul 	if (m0 == NULL)
763a94100faSBill Paul 		return (ENOBUFS);
764a94100faSBill Paul 
76597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
76697b9d4baSJohn-Mark Gurney 
767a94100faSBill Paul 	/*
768a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
769a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
770a94100faSBill Paul 	 * following special functions:
771a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
772a94100faSBill Paul 	 * - Enables digital loopback mode
773a94100faSBill Paul 	 * - Leaves interrupts turned off
774a94100faSBill Paul 	 */
775a94100faSBill Paul 
776a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
777a94100faSBill Paul 	sc->rl_testmode = 1;
7788476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
77997b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
780351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
781ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
782ed510fb0SBill Paul 		phyaddr = 1;
783ed510fb0SBill Paul 	else
784ed510fb0SBill Paul 		phyaddr = 0;
785ed510fb0SBill Paul 
786ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
787ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
788ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
789ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
790ed510fb0SBill Paul 			break;
791ed510fb0SBill Paul 	}
792ed510fb0SBill Paul 
793ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
794ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
795ed510fb0SBill Paul 
796804af9a1SBill Paul 	DELAY(100000);
797a94100faSBill Paul 
798a94100faSBill Paul 	/* Put some data in the mbuf */
799a94100faSBill Paul 
800a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
801a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
802a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
803a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
804a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
805a94100faSBill Paul 
8067cae6651SBill Paul 	/*
8077cae6651SBill Paul 	 * Queue the packet, start transmission.
8087cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
8097cae6651SBill Paul 	 */
810a94100faSBill Paul 
811abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
81297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
81352732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8147cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
81597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
816a94100faSBill Paul 	m0 = NULL;
817a94100faSBill Paul 
818a94100faSBill Paul 	/* Wait for it to propagate through the chip */
819a94100faSBill Paul 
820abc8ff44SBill Paul 	DELAY(100000);
821a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
822a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
823ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
824abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
825abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
826a94100faSBill Paul 			break;
827a94100faSBill Paul 		DELAY(10);
828a94100faSBill Paul 	}
829a94100faSBill Paul 
830a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8316b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8326b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8336b9f5c94SGleb Smirnoff 		    " loopback mode\n");
834a94100faSBill Paul 		error = EIO;
835a94100faSBill Paul 		goto done;
836a94100faSBill Paul 	}
837a94100faSBill Paul 
838a94100faSBill Paul 	/*
839a94100faSBill Paul 	 * The packet should have been dumped into the first
840a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
841a94100faSBill Paul 	 */
842a94100faSBill Paul 
843a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
844a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
845a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
846d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
847d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
848d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
849d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
850d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
851a94100faSBill Paul 
852d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
853d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
854a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
855a94100faSBill Paul 
856a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
857a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
858a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
859a94100faSBill Paul 
860a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8616b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8626b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
863a94100faSBill Paul 		error = EIO;
864a94100faSBill Paul 		goto done;
865a94100faSBill Paul 	}
866a94100faSBill Paul 
867a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
868a94100faSBill Paul 
869a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
870a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
871a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8726b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8736b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
874a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8756b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
876a94100faSBill Paul 		    eh->ether_dhost, ":", eh->ether_shost, ":",
877a94100faSBill Paul 		    ntohs(eh->ether_type));
8786b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8796b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8806b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8816b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8826b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8836b9f5c94SGleb Smirnoff 		    "details.\n");
884a94100faSBill Paul 		error = EIO;
885a94100faSBill Paul 	}
886a94100faSBill Paul 
887a94100faSBill Paul done:
888a94100faSBill Paul 	/* Turn interface off, release resources */
889a94100faSBill Paul 
890a94100faSBill Paul 	sc->rl_testmode = 0;
891351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
892a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
893a94100faSBill Paul 	re_stop(sc);
894a94100faSBill Paul 	if (m0 != NULL)
895a94100faSBill Paul 		m_freem(m0);
896a94100faSBill Paul 
89797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
89897b9d4baSJohn-Mark Gurney 
899a94100faSBill Paul 	return (error);
900a94100faSBill Paul }
901a94100faSBill Paul 
902ed510fb0SBill Paul #endif
903ed510fb0SBill Paul 
904a94100faSBill Paul /*
905a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
906a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
907a94100faSBill Paul  */
908a94100faSBill Paul static int
9097b5ffebfSPyun YongHyeon re_probe(device_t dev)
910a94100faSBill Paul {
911b3030306SMarius Strobl 	const struct rl_type	*t;
912dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
913dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
914dfdb409eSPyun YongHyeon 	int			i;
915a94100faSBill Paul 
916dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
917dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
918dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
919dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
920a94100faSBill Paul 
921dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
922dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
92326390635SJohn Baldwin 			/*
92426390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
925dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
92626390635SJohn Baldwin 			 */
927a94100faSBill Paul 			return (ENXIO);
928a94100faSBill Paul 		}
929dfdb409eSPyun YongHyeon 	}
930dfdb409eSPyun YongHyeon 
931dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
932dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
933dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
934dfdb409eSPyun YongHyeon 			return (ENXIO);
935dfdb409eSPyun YongHyeon 		}
936dfdb409eSPyun YongHyeon 	}
937dfdb409eSPyun YongHyeon 
938dfdb409eSPyun YongHyeon 	t = re_devs;
939dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
940dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
941a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
942d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
943a94100faSBill Paul 		}
944a94100faSBill Paul 	}
945a94100faSBill Paul 
946a94100faSBill Paul 	return (ENXIO);
947a94100faSBill Paul }
948a94100faSBill Paul 
949a94100faSBill Paul /*
950a94100faSBill Paul  * Map a single buffer address.
951a94100faSBill Paul  */
952a94100faSBill Paul 
953a94100faSBill Paul static void
9547b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
955a94100faSBill Paul {
9568fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
957a94100faSBill Paul 
958a94100faSBill Paul 	if (error)
959a94100faSBill Paul 		return;
960a94100faSBill Paul 
961a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
962a94100faSBill Paul 	addr = arg;
963a94100faSBill Paul 	*addr = segs->ds_addr;
964a94100faSBill Paul }
965a94100faSBill Paul 
966a94100faSBill Paul static int
9677b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
968a94100faSBill Paul {
96966366ca4SPyun YongHyeon 	bus_addr_t		lowaddr;
970d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
971a94100faSBill Paul 	int			error;
972a94100faSBill Paul 	int			i;
973a94100faSBill Paul 
974d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
975d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
976d65abd66SPyun YongHyeon 
977d65abd66SPyun YongHyeon 	/*
978d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
979ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
980ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
981ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
982ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
98366366ca4SPyun YongHyeon 	 * may not have the limitation.
984d65abd66SPyun YongHyeon 	 */
98566366ca4SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
98666366ca4SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
98766366ca4SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
988d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
98966366ca4SPyun YongHyeon 	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
990d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
991d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
992d65abd66SPyun YongHyeon 	if (error) {
993d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
994d65abd66SPyun YongHyeon 		return (error);
995d65abd66SPyun YongHyeon 	}
996d65abd66SPyun YongHyeon 
997d65abd66SPyun YongHyeon 	/*
998d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
999d65abd66SPyun YongHyeon 	 */
1000d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
1001d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1002d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
1003d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
1004d65abd66SPyun YongHyeon 	if (error) {
1005d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
1006d65abd66SPyun YongHyeon 		return (error);
1007d65abd66SPyun YongHyeon 	}
1008d65abd66SPyun YongHyeon 
1009a94100faSBill Paul 	/*
1010a94100faSBill Paul 	 * Allocate map for RX mbufs.
1011a94100faSBill Paul 	 */
1012d65abd66SPyun YongHyeon 
101381eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
101481eee0ebSPyun YongHyeon 		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
101581eee0ebSPyun YongHyeon 		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
101681eee0ebSPyun YongHyeon 		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
101781eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_mtag);
101881eee0ebSPyun YongHyeon 		if (error) {
101981eee0ebSPyun YongHyeon 			device_printf(dev,
102081eee0ebSPyun YongHyeon 			    "could not allocate jumbo RX DMA tag\n");
102181eee0ebSPyun YongHyeon 			return (error);
102281eee0ebSPyun YongHyeon 		}
102381eee0ebSPyun YongHyeon 	}
1024d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1025d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1026d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1027a94100faSBill Paul 	if (error) {
1028d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1029d65abd66SPyun YongHyeon 		return (error);
1030a94100faSBill Paul 	}
1031a94100faSBill Paul 
1032a94100faSBill Paul 	/*
1033a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1034a94100faSBill Paul 	 */
1035a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1036a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1037d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1038a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1039a94100faSBill Paul 	if (error) {
1040d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1041d65abd66SPyun YongHyeon 		return (error);
1042a94100faSBill Paul 	}
1043a94100faSBill Paul 
1044a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1045a94100faSBill Paul 
1046a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1047d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1048d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1049a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1050d65abd66SPyun YongHyeon 	if (error) {
1051d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1052d65abd66SPyun YongHyeon 		return (error);
1053d65abd66SPyun YongHyeon 	}
1054a94100faSBill Paul 
1055a94100faSBill Paul 	/* Load the map for the TX ring. */
1056a94100faSBill Paul 
1057d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1058a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1059a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1060d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1061a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1062d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1063d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1064d65abd66SPyun YongHyeon 		return (ENOMEM);
1065d65abd66SPyun YongHyeon 	}
1066a94100faSBill Paul 
1067a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1068a94100faSBill Paul 
1069d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1070d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1071d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1072a94100faSBill Paul 		if (error) {
1073d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1074d65abd66SPyun YongHyeon 			return (error);
1075a94100faSBill Paul 		}
1076a94100faSBill Paul 	}
1077a94100faSBill Paul 
1078a94100faSBill Paul 	/*
1079a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1080a94100faSBill Paul 	 */
1081a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1082a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1083d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1084a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1085a94100faSBill Paul 	if (error) {
1086d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1087d65abd66SPyun YongHyeon 		return (error);
1088a94100faSBill Paul 	}
1089a94100faSBill Paul 
1090a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1091a94100faSBill Paul 
1092a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1093d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1094d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1095a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1096d65abd66SPyun YongHyeon 	if (error) {
1097d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1098d65abd66SPyun YongHyeon 		return (error);
1099d65abd66SPyun YongHyeon 	}
1100a94100faSBill Paul 
1101a94100faSBill Paul 	/* Load the map for the RX ring. */
1102a94100faSBill Paul 
1103d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1104a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1105a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1106d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1107a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1108d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1109d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1110d65abd66SPyun YongHyeon 		return (ENOMEM);
1111d65abd66SPyun YongHyeon 	}
1112a94100faSBill Paul 
1113a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1114a94100faSBill Paul 
111581eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
111681eee0ebSPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
111781eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_sparemap);
111881eee0ebSPyun YongHyeon 		if (error) {
111981eee0ebSPyun YongHyeon 			device_printf(dev,
112081eee0ebSPyun YongHyeon 			    "could not create spare DMA map for jumbo RX\n");
112181eee0ebSPyun YongHyeon 			return (error);
112281eee0ebSPyun YongHyeon 		}
112381eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
112481eee0ebSPyun YongHyeon 			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
112581eee0ebSPyun YongHyeon 			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
112681eee0ebSPyun YongHyeon 			if (error) {
112781eee0ebSPyun YongHyeon 				device_printf(dev,
112881eee0ebSPyun YongHyeon 				    "could not create DMA map for jumbo RX\n");
112981eee0ebSPyun YongHyeon 				return (error);
113081eee0ebSPyun YongHyeon 			}
113181eee0ebSPyun YongHyeon 		}
113281eee0ebSPyun YongHyeon 	}
1133d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1134d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1135a94100faSBill Paul 	if (error) {
1136d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1137d65abd66SPyun YongHyeon 		return (error);
1138d65abd66SPyun YongHyeon 	}
1139d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1140d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1141d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1142d65abd66SPyun YongHyeon 		if (error) {
1143d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1144d65abd66SPyun YongHyeon 			return (error);
1145a94100faSBill Paul 		}
1146a94100faSBill Paul 	}
1147a94100faSBill Paul 
11480534aae0SPyun YongHyeon 	/* Create DMA map for statistics. */
11490534aae0SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
11500534aae0SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
11510534aae0SPyun YongHyeon 	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
11520534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_stag);
11530534aae0SPyun YongHyeon 	if (error) {
11540534aae0SPyun YongHyeon 		device_printf(dev, "could not create statistics DMA tag\n");
11550534aae0SPyun YongHyeon 		return (error);
11560534aae0SPyun YongHyeon 	}
11570534aae0SPyun YongHyeon 	/* Allocate DMA'able memory for statistics. */
11580534aae0SPyun YongHyeon 	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
11590534aae0SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_stats,
11600534aae0SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
11610534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_smap);
11620534aae0SPyun YongHyeon 	if (error) {
11630534aae0SPyun YongHyeon 		device_printf(dev,
11640534aae0SPyun YongHyeon 		    "could not allocate statistics DMA memory\n");
11650534aae0SPyun YongHyeon 		return (error);
11660534aae0SPyun YongHyeon 	}
11670534aae0SPyun YongHyeon 	/* Load the map for statistics. */
11680534aae0SPyun YongHyeon 	sc->rl_ldata.rl_stats_addr = 0;
11690534aae0SPyun YongHyeon 	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
11700534aae0SPyun YongHyeon 	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
11710534aae0SPyun YongHyeon 	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
11720534aae0SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
11730534aae0SPyun YongHyeon 		device_printf(dev, "could not load statistics DMA memory\n");
11740534aae0SPyun YongHyeon 		return (ENOMEM);
11750534aae0SPyun YongHyeon 	}
11760534aae0SPyun YongHyeon 
1177a94100faSBill Paul 	return (0);
1178a94100faSBill Paul }
1179a94100faSBill Paul 
1180a94100faSBill Paul /*
1181a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1182a94100faSBill Paul  * setup and ethernet/BPF attach.
1183a94100faSBill Paul  */
1184a94100faSBill Paul static int
11857b5ffebfSPyun YongHyeon re_attach(device_t dev)
1186a94100faSBill Paul {
1187a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1188be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1189a94100faSBill Paul 	struct rl_softc		*sc;
1190a94100faSBill Paul 	struct ifnet		*ifp;
1191b3030306SMarius Strobl 	const struct rl_hwrev	*hw_rev;
1192017f1c8dSPyun YongHyeon 	u_int32_t		cap, ctl;
1193a94100faSBill Paul 	int			hwrev;
1194ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
11958e5d93dbSMarius Strobl 	int			error = 0, i, phy, rid;
11964a58fd45SPyun YongHyeon 	int			msic, msixc, reg;
119703ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1198a94100faSBill Paul 
1199a94100faSBill Paul 	sc = device_get_softc(dev);
1200ed510fb0SBill Paul 	sc->rl_dev = dev;
1201a94100faSBill Paul 
1202a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
120397b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1204d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1205d1754a9bSJohn Baldwin 
1206a94100faSBill Paul 	/*
1207a94100faSBill Paul 	 * Map control/status registers.
1208a94100faSBill Paul 	 */
1209a94100faSBill Paul 	pci_enable_busmaster(dev);
1210a94100faSBill Paul 
1211ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
12122c21710bSPyun YongHyeon 	/*
12132c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
12142c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
12152c21710bSPyun YongHyeon 	 * is used always activate io mapping.
12162c21710bSPyun YongHyeon 	 */
12172c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
12182c21710bSPyun YongHyeon 		prefer_iomap = 1;
12192c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1220ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1221ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1222ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1223ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1224ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
12252c21710bSPyun YongHyeon 	} else {
12262c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
12272c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
12282c21710bSPyun YongHyeon 	}
1229ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1230ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
12312c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1232ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1233ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1234ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1235ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
12362c21710bSPyun YongHyeon 	}
1237ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1238d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1239a94100faSBill Paul 		error = ENXIO;
1240a94100faSBill Paul 		goto fail;
1241a94100faSBill Paul 	}
1242a94100faSBill Paul 
1243a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1244a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1245a94100faSBill Paul 
12465774c5ffSPyun YongHyeon 	msic = pci_msi_count(dev);
12474a58fd45SPyun YongHyeon 	msixc = pci_msix_count(dev);
1248017f1c8dSPyun YongHyeon 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
12494a58fd45SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
1250017f1c8dSPyun YongHyeon 		sc->rl_expcap = reg;
1251017f1c8dSPyun YongHyeon 	}
12524a58fd45SPyun YongHyeon 	if (bootverbose) {
12535774c5ffSPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
12544a58fd45SPyun YongHyeon 		device_printf(dev, "MSI-X count : %d\n", msixc);
12555774c5ffSPyun YongHyeon 	}
12564a58fd45SPyun YongHyeon 	if (msix_disable > 0)
12574a58fd45SPyun YongHyeon 		msixc = 0;
12584a58fd45SPyun YongHyeon 	if (msi_disable > 0)
12594a58fd45SPyun YongHyeon 		msic = 0;
12604a58fd45SPyun YongHyeon 	/* Prefer MSI-X to MSI. */
12614a58fd45SPyun YongHyeon 	if (msixc > 0) {
12624a58fd45SPyun YongHyeon 		msixc = 1;
12634a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
12644a58fd45SPyun YongHyeon 		sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
12654a58fd45SPyun YongHyeon 		    &rid, RF_ACTIVE);
12664a58fd45SPyun YongHyeon 		if (sc->rl_res_pba == NULL) {
12674a58fd45SPyun YongHyeon 			device_printf(sc->rl_dev,
12684a58fd45SPyun YongHyeon 			    "could not allocate MSI-X PBA resource\n");
12694a58fd45SPyun YongHyeon 		}
12704a58fd45SPyun YongHyeon 		if (sc->rl_res_pba != NULL &&
12714a58fd45SPyun YongHyeon 		    pci_alloc_msix(dev, &msixc) == 0) {
12724a58fd45SPyun YongHyeon 			if (msixc == 1) {
12734a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI-X message\n",
12744a58fd45SPyun YongHyeon 				    msixc);
12754a58fd45SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSIX;
12764a58fd45SPyun YongHyeon 			} else
12774a58fd45SPyun YongHyeon 				pci_release_msi(dev);
12784a58fd45SPyun YongHyeon 		}
12794a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
12804a58fd45SPyun YongHyeon 			if (sc->rl_res_pba != NULL)
12814a58fd45SPyun YongHyeon 				bus_release_resource(dev, SYS_RES_MEMORY, rid,
12824a58fd45SPyun YongHyeon 				    sc->rl_res_pba);
12834a58fd45SPyun YongHyeon 			sc->rl_res_pba = NULL;
12844a58fd45SPyun YongHyeon 			msixc = 0;
12854a58fd45SPyun YongHyeon 		}
12864a58fd45SPyun YongHyeon 	}
12874a58fd45SPyun YongHyeon 	/* Prefer MSI to INTx. */
12884a58fd45SPyun YongHyeon 	if (msixc == 0 && msic > 0) {
1289f1bb696aSPyun YongHyeon 		msic = 1;
12905774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
12915774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
12924a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI message\n",
12935774c5ffSPyun YongHyeon 				    msic);
1294351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1295339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1296339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1297339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1298339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1299339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1300f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13015774c5ffSPyun YongHyeon 			} else
13025774c5ffSPyun YongHyeon 				pci_release_msi(dev);
13035774c5ffSPyun YongHyeon 		}
13044a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSI) == 0)
13054a58fd45SPyun YongHyeon 			msic = 0;
13065774c5ffSPyun YongHyeon 	}
1307a94100faSBill Paul 
13085774c5ffSPyun YongHyeon 	/* Allocate interrupt */
13094a58fd45SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
13105774c5ffSPyun YongHyeon 		rid = 0;
13115774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
13125774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
13135774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
13145774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1315a94100faSBill Paul 			error = ENXIO;
1316a94100faSBill Paul 			goto fail;
1317a94100faSBill Paul 		}
13185774c5ffSPyun YongHyeon 	} else {
13195774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
13205774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
13215774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
13225774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
13235774c5ffSPyun YongHyeon 				device_printf(dev,
13245774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
13255774c5ffSPyun YongHyeon 				    "message %d\n", rid);
13265774c5ffSPyun YongHyeon 				error = ENXIO;
13275774c5ffSPyun YongHyeon 				goto fail;
13285774c5ffSPyun YongHyeon 			}
13295774c5ffSPyun YongHyeon 		}
13305774c5ffSPyun YongHyeon 	}
1331a94100faSBill Paul 
13324d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
13334d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
13344d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
13354d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
13364d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
13374d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
13384d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
13394d2bf239SPyun YongHyeon 		}
13404d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13414d2bf239SPyun YongHyeon 	}
13424d2bf239SPyun YongHyeon 
1343017f1c8dSPyun YongHyeon 	/* Disable ASPM L0S/L1. */
1344017f1c8dSPyun YongHyeon 	if (sc->rl_expcap != 0) {
1345017f1c8dSPyun YongHyeon 		cap = pci_read_config(dev, sc->rl_expcap +
1346389c8bd5SGavin Atkinson 		    PCIER_LINK_CAP, 2);
1347389c8bd5SGavin Atkinson 		if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
1348017f1c8dSPyun YongHyeon 			ctl = pci_read_config(dev, sc->rl_expcap +
1349389c8bd5SGavin Atkinson 			    PCIER_LINK_CTL, 2);
1350e935190aSGavin Atkinson 			if ((ctl & PCIEM_LINK_CTL_ASPMC) != 0) {
1351e935190aSGavin Atkinson 				ctl &= ~PCIEM_LINK_CTL_ASPMC;
1352017f1c8dSPyun YongHyeon 				pci_write_config(dev, sc->rl_expcap +
1353389c8bd5SGavin Atkinson 				    PCIER_LINK_CTL, ctl, 2);
1354017f1c8dSPyun YongHyeon 				device_printf(dev, "ASPM disabled\n");
1355017f1c8dSPyun YongHyeon 			}
1356017f1c8dSPyun YongHyeon 		} else
1357017f1c8dSPyun YongHyeon 			device_printf(dev, "no ASPM capability\n");
1358017f1c8dSPyun YongHyeon 	}
1359017f1c8dSPyun YongHyeon 
1360abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1361a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1362566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1363566ca8caSJung-uk Kim 	case 0x00000000:
1364566ca8caSJung-uk Kim 	case 0x10000000:
1365566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1366566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1367566ca8caSJung-uk Kim 		break;
1368566ca8caSJung-uk Kim 	default:
1369a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1370a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1371566ca8caSJung-uk Kim 		break;
1372566ca8caSJung-uk Kim 	}
1373566ca8caSJung-uk Kim 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1374abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1375abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1376abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
137781eee0ebSPyun YongHyeon 			sc->rl_hwrev = hw_rev;
1378abc8ff44SBill Paul 			break;
1379abc8ff44SBill Paul 		}
1380abc8ff44SBill Paul 		hw_rev++;
1381abc8ff44SBill Paul 	}
1382d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1383a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1384d65abd66SPyun YongHyeon 		error = ENXIO;
1385d65abd66SPyun YongHyeon 		goto fail;
1386d65abd66SPyun YongHyeon 	}
1387abc8ff44SBill Paul 
1388351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1389351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
139081eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1391351a76f9SPyun YongHyeon 		break;
1392351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1393351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
139481eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1395351a76f9SPyun YongHyeon 		break;
1396b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1397b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
13983d22427cSTai-hwa Liang 	case RL_HWREV_8102EL_SPIN1:
139981eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
140081eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
140181eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1402b1d62f0fSPyun YongHyeon 		break;
14038281a098SPyun YongHyeon 	case RL_HWREV_8103E:
140481eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
140581eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
140681eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
14078281a098SPyun YongHyeon 		break;
140839e69201SPyun YongHyeon 	case RL_HWREV_8401E:
140954899a96SPyun YongHyeon 	case RL_HWREV_8105E:
14106b0a8e04SPyun YongHyeon 	case RL_HWREV_8105E_SPIN1:
141154899a96SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
141254899a96SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
141354899a96SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
141454899a96SPyun YongHyeon 		break;
1415eef0e496SPyun YongHyeon 	case RL_HWREV_8402:
1416eef0e496SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1417eef0e496SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1418eef0e496SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD |
1419eef0e496SPyun YongHyeon 		    RL_FLAG_CMDSTOP_WAIT_TXQ;
1420eef0e496SPyun YongHyeon 		break;
1421ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN1:
1422ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN2:
1423886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1424886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1425ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN3:
1426aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1427deb5c680SPyun YongHyeon 		break;
1428deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
142961f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
143061f45a72SPyun YongHyeon 		/* FALLTHROUGH */
143161f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
143261f45a72SPyun YongHyeon 		if ((hwrev & 0x00700000) == 0x00200000)
143361f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
143461f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1435deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
1436aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1437f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
14386830588dSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1439351a76f9SPyun YongHyeon 		break;
1440df2dc2b3SPyun YongHyeon 	case RL_HWREV_8168D:
1441df2dc2b3SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1442df2dc2b3SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1443df2dc2b3SPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1444df2dc2b3SPyun YongHyeon 		    RL_FLAG_WOL_MANLINK;
1445df2dc2b3SPyun YongHyeon 		break;
1446eef0e496SPyun YongHyeon 	case RL_HWREV_8168DP:
1447eef0e496SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1448eef0e496SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_AUTOPAD |
14496830588dSPyun YongHyeon 		    RL_FLAG_JUMBOV2 | RL_FLAG_WAIT_TXPOLL | RL_FLAG_WOL_MANLINK;
1450eef0e496SPyun YongHyeon 		break;
1451d0c45156SPyun YongHyeon 	case RL_HWREV_8168E:
1452d0c45156SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1453d0c45156SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
14546830588dSPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
14556830588dSPyun YongHyeon 		    RL_FLAG_WOL_MANLINK;
1456d0c45156SPyun YongHyeon 		break;
1457f0431c5bSPyun YongHyeon 	case RL_HWREV_8168E_VL:
1458d467ffaaSPyun YongHyeon 	case RL_HWREV_8168F:
1459d56f7f52SPyun YongHyeon 	case RL_HWREV_8411:
1460f0431c5bSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1461f0431c5bSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1462eef0e496SPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
14636830588dSPyun YongHyeon 		    RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK;
1464f0431c5bSPyun YongHyeon 		break;
1465566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1466566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1467566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1468566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1469566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1470566ca8caSJung-uk Kim 		/* FALLTHROUGH */
14710596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
14720596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1473566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1474566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1475351a76f9SPyun YongHyeon 		break;
1476351a76f9SPyun YongHyeon 	default:
1477351a76f9SPyun YongHyeon 		break;
1478351a76f9SPyun YongHyeon 	}
1479351a76f9SPyun YongHyeon 
1480e7e7593cSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8139CPLUS) {
1481e7e7593cSPyun YongHyeon 		sc->rl_cfg0 = RL_8139_CFG0;
1482e7e7593cSPyun YongHyeon 		sc->rl_cfg1 = RL_8139_CFG1;
1483e7e7593cSPyun YongHyeon 		sc->rl_cfg2 = 0;
1484e7e7593cSPyun YongHyeon 		sc->rl_cfg3 = RL_8139_CFG3;
1485e7e7593cSPyun YongHyeon 		sc->rl_cfg4 = RL_8139_CFG4;
1486e7e7593cSPyun YongHyeon 		sc->rl_cfg5 = RL_8139_CFG5;
1487e7e7593cSPyun YongHyeon 	} else {
1488e7e7593cSPyun YongHyeon 		sc->rl_cfg0 = RL_CFG0;
1489e7e7593cSPyun YongHyeon 		sc->rl_cfg1 = RL_CFG1;
1490e7e7593cSPyun YongHyeon 		sc->rl_cfg2 = RL_CFG2;
1491e7e7593cSPyun YongHyeon 		sc->rl_cfg3 = RL_CFG3;
1492e7e7593cSPyun YongHyeon 		sc->rl_cfg4 = RL_CFG4;
1493e7e7593cSPyun YongHyeon 		sc->rl_cfg5 = RL_CFG5;
1494e7e7593cSPyun YongHyeon 	}
1495e7e7593cSPyun YongHyeon 
149693252626SPyun YongHyeon 	/* Reset the adapter. */
149793252626SPyun YongHyeon 	RL_LOCK(sc);
149893252626SPyun YongHyeon 	re_reset(sc);
149993252626SPyun YongHyeon 	RL_UNLOCK(sc);
150093252626SPyun YongHyeon 
1501deb5c680SPyun YongHyeon 	/* Enable PME. */
1502deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1503e7e7593cSPyun YongHyeon 	cfg = CSR_READ_1(sc, sc->rl_cfg1);
1504deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1505e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, cfg);
1506e7e7593cSPyun YongHyeon 	cfg = CSR_READ_1(sc, sc->rl_cfg5);
1507deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1508e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, cfg);
1509deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1510deb5c680SPyun YongHyeon 
1511deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1512deb5c680SPyun YongHyeon 		/*
1513deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1514deb5c680SPyun YongHyeon 		 * address from EEPROM.
1515deb5c680SPyun YongHyeon 		 */
1516deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1517deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1518deb5c680SPyun YongHyeon 	} else {
1519141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1520ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1521a94100faSBill Paul 		if (re_did != 0x8129)
1522141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1523a94100faSBill Paul 
1524a94100faSBill Paul 		/*
1525a94100faSBill Paul 		 * Get station address from the EEPROM.
1526a94100faSBill Paul 		 */
1527ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1528be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1529be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1530de8925a2SKevin Lo 		bcopy(as, eaddr, ETHER_ADDR_LEN);
1531deb5c680SPyun YongHyeon 	}
1532ed510fb0SBill Paul 
1533ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1534d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1535ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1536ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1537d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1538d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1539ed510fb0SBill Paul 	} else {
1540d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1541ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1542ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1543d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1544d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1545abc8ff44SBill Paul 	}
15469bac70b8SBill Paul 
1547a94100faSBill Paul 	error = re_allocmem(dev, sc);
1548a94100faSBill Paul 	if (error)
1549a94100faSBill Paul 		goto fail;
15500534aae0SPyun YongHyeon 	re_add_sysctls(sc);
1551a94100faSBill Paul 
1552cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1553cd036ec1SBrooks Davis 	if (ifp == NULL) {
1554d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1555cd036ec1SBrooks Davis 		error = ENOSPC;
1556cd036ec1SBrooks Davis 		goto fail;
1557cd036ec1SBrooks Davis 	}
1558cd036ec1SBrooks Davis 
155961f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
156061f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
156161f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
156261f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
156361f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
156461f45a72SPyun YongHyeon 		else
156561f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
156661f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
156761f45a72SPyun YongHyeon 	}
156861f45a72SPyun YongHyeon 
1569351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
157039e69201SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) {
1571d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
157239e69201SPyun YongHyeon 		if (hw_rev->rl_rev == RL_HWREV_8401E)
157339e69201SPyun YongHyeon 			CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08);
157439e69201SPyun YongHyeon 	}
1575351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1576351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1577351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1578351a76f9SPyun YongHyeon 	}
1579351a76f9SPyun YongHyeon 
1580a94100faSBill Paul 	ifp->if_softc = sc;
15819bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1582a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1583a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1584a94100faSBill Paul 	ifp->if_start = re_start;
1585bc2a1002SPyun YongHyeon 	/*
1586bc2a1002SPyun YongHyeon 	 * RTL8168/8111C generates wrong IP checksummed frame if the
1587bc2a1002SPyun YongHyeon 	 * packet has IP options so disable TX IP checksum offloading.
1588bc2a1002SPyun YongHyeon 	 */
1589bc2a1002SPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C ||
1590*3c2a957dSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2 ||
1591*3c2a957dSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168CP)
1592bc2a1002SPyun YongHyeon 		ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1593bc2a1002SPyun YongHyeon 	else
1594bc2a1002SPyun YongHyeon 		ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
1595bc2a1002SPyun YongHyeon 	ifp->if_hwassist |= CSUM_TSO;
1596d6d7d923SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1597498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1598a94100faSBill Paul 	ifp->if_init = re_init;
159952732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
160052732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
160152732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1602a94100faSBill Paul 
1603ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1604ed510fb0SBill Paul 
1605fed3ed71SPyun YongHyeon #define	RE_PHYAD_INTERNAL	 0
1606fed3ed71SPyun YongHyeon 
1607fed3ed71SPyun YongHyeon 	/* Do MII setup. */
1608fed3ed71SPyun YongHyeon 	phy = RE_PHYAD_INTERNAL;
1609fed3ed71SPyun YongHyeon 	if (sc->rl_type == RL_8169)
1610fed3ed71SPyun YongHyeon 		phy = 1;
1611fed3ed71SPyun YongHyeon 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
1612fed3ed71SPyun YongHyeon 	    re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
1613fed3ed71SPyun YongHyeon 	if (error != 0) {
1614fed3ed71SPyun YongHyeon 		device_printf(dev, "attaching PHYs failed\n");
1615fed3ed71SPyun YongHyeon 		goto fail;
1616fed3ed71SPyun YongHyeon 	}
1617fed3ed71SPyun YongHyeon 
1618a94100faSBill Paul 	/*
1619a94100faSBill Paul 	 * Call MI attach routine.
1620a94100faSBill Paul 	 */
1621a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1622a94100faSBill Paul 
1623960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1624960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1625960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1626960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
16277467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
16283b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &reg) == 0)
16297467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1630960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
163144f7cbf5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST);
1632a2a8420cSPyun YongHyeon 	/*
1633f9ad4da7SPyun YongHyeon 	 * Don't enable TSO by default.  It is known to generate
1634f9ad4da7SPyun YongHyeon 	 * corrupted TCP segments(bad TCP options) under certain
1635f9ad4da7SPyun YongHyeon 	 * circumtances.
1636a2a8420cSPyun YongHyeon 	 */
1637a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1638ecafbbb5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1639960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1640960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1641960fd5b3SPyun YongHyeon #endif
1642960fd5b3SPyun YongHyeon 	/*
1643960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1644960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1645960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1646960fd5b3SPyun YongHyeon 	 */
1647960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1648960fd5b3SPyun YongHyeon 
1649579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
1650579a6e3cSLuigi Rizzo 	re_netmap_attach(sc);
1651579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
1652ed510fb0SBill Paul #ifdef RE_DIAG
1653ed510fb0SBill Paul 	/*
1654ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1655ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1656ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1657ed510fb0SBill Paul 	 */
1658a94100faSBill Paul 
1659ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1660ed510fb0SBill Paul 		error = re_diag(sc);
1661a94100faSBill Paul 		if (error) {
1662ed510fb0SBill Paul 			device_printf(dev,
1663ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1664a94100faSBill Paul 			ether_ifdetach(ifp);
1665a94100faSBill Paul 			goto fail;
1666a94100faSBill Paul 		}
1667ed510fb0SBill Paul 	}
1668ed510fb0SBill Paul #endif
1669a94100faSBill Paul 
1670502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
1671502be0f7SPyun YongHyeon 	intr_filter = 1;
1672502be0f7SPyun YongHyeon #endif
1673a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1674502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
1675502be0f7SPyun YongHyeon 	    intr_filter == 0) {
1676502be0f7SPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
1677502be0f7SPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc,
1678502be0f7SPyun YongHyeon 		    &sc->rl_intrhand[0]);
1679502be0f7SPyun YongHyeon 	} else {
16805774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
16815774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
16825774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
16835774c5ffSPyun YongHyeon 	}
1684a94100faSBill Paul 	if (error) {
1685d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1686a94100faSBill Paul 		ether_ifdetach(ifp);
1687a94100faSBill Paul 	}
1688a94100faSBill Paul 
1689a94100faSBill Paul fail:
1690ed510fb0SBill Paul 
1691a94100faSBill Paul 	if (error)
1692a94100faSBill Paul 		re_detach(dev);
1693a94100faSBill Paul 
1694a94100faSBill Paul 	return (error);
1695a94100faSBill Paul }
1696a94100faSBill Paul 
1697a94100faSBill Paul /*
1698a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1699a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1700a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1701a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1702a94100faSBill Paul  * allocated.
1703a94100faSBill Paul  */
1704a94100faSBill Paul static int
17057b5ffebfSPyun YongHyeon re_detach(device_t dev)
1706a94100faSBill Paul {
1707a94100faSBill Paul 	struct rl_softc		*sc;
1708a94100faSBill Paul 	struct ifnet		*ifp;
17095774c5ffSPyun YongHyeon 	int			i, rid;
1710a94100faSBill Paul 
1711a94100faSBill Paul 	sc = device_get_softc(dev);
1712fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1713aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
171497b9d4baSJohn-Mark Gurney 
171581cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
171681cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
171740929967SGleb Smirnoff #ifdef DEVICE_POLLING
171840929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
171940929967SGleb Smirnoff 			ether_poll_deregister(ifp);
172040929967SGleb Smirnoff #endif
172197b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
172297b9d4baSJohn-Mark Gurney #if 0
172397b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
172497b9d4baSJohn-Mark Gurney #endif
1725a94100faSBill Paul 		re_stop(sc);
1726525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1727d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
17283d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
1729a94100faSBill Paul 		/*
1730a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1731a94100faSBill Paul 		 * still had a BPF descriptor attached to this
173297b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1733a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1734a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1735a94100faSBill Paul 		 * which will try to call re_init() again. This will
1736a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1737a94100faSBill Paul 		 * which will panic the system when the kernel tries
1738a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1739a94100faSBill Paul 		 * anymore.
1740a94100faSBill Paul 		 */
1741a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1742525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1743a94100faSBill Paul 	}
1744a94100faSBill Paul 	if (sc->rl_miibus)
1745a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1746a94100faSBill Paul 	bus_generic_detach(dev);
1747a94100faSBill Paul 
174897b9d4baSJohn-Mark Gurney 	/*
174997b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
175097b9d4baSJohn-Mark Gurney 	 * stopped here.
175197b9d4baSJohn-Mark Gurney 	 */
175297b9d4baSJohn-Mark Gurney 
1753502be0f7SPyun YongHyeon 	if (sc->rl_intrhand[0] != NULL) {
1754502be0f7SPyun YongHyeon 		bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
1755502be0f7SPyun YongHyeon 		sc->rl_intrhand[0] = NULL;
17565774c5ffSPyun YongHyeon 	}
175782242c11SKevin Lo 	if (ifp != NULL) {
175882242c11SKevin Lo #ifdef DEV_NETMAP
175982242c11SKevin Lo 		netmap_detach(ifp);
176082242c11SKevin Lo #endif /* DEV_NETMAP */
1761ad4f426eSWarner Losh 		if_free(ifp);
176282242c11SKevin Lo 	}
1763502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
1764502be0f7SPyun YongHyeon 		rid = 0;
1765502be0f7SPyun YongHyeon 	else
1766502be0f7SPyun YongHyeon 		rid = 1;
17675774c5ffSPyun YongHyeon 	if (sc->rl_irq[0] != NULL) {
1768502be0f7SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]);
17695774c5ffSPyun YongHyeon 		sc->rl_irq[0] = NULL;
17705774c5ffSPyun YongHyeon 	}
1771502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0)
17725774c5ffSPyun YongHyeon 		pci_release_msi(dev);
17734a58fd45SPyun YongHyeon 	if (sc->rl_res_pba) {
17744a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
17754a58fd45SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
17764a58fd45SPyun YongHyeon 	}
1777a94100faSBill Paul 	if (sc->rl_res)
1778ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1779ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1780a94100faSBill Paul 
1781a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1782a94100faSBill Paul 
1783a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
17840534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map)
1785a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1786a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
17870534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list)
1788a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1789a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list,
1790a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
1791a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1792a94100faSBill Paul 	}
1793a94100faSBill Paul 
1794a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1795a94100faSBill Paul 
1796a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
17970534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map)
1798a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1799a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
18000534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list)
1801a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1802a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list,
1803a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
1804a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1805a94100faSBill Paul 	}
1806a94100faSBill Paul 
1807a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1808a94100faSBill Paul 
1809d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
18109e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
18119e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1812d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1813d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
18149e18005dSPyun YongHyeon 		}
1815d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1816d65abd66SPyun YongHyeon 	}
1817d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
18189e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
18199e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1820d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1821d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
18229e18005dSPyun YongHyeon 		}
1823d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1824d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1825d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1826d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1827a94100faSBill Paul 	}
182881eee0ebSPyun YongHyeon 	if (sc->rl_ldata.rl_jrx_mtag) {
182981eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
183081eee0ebSPyun YongHyeon 			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
183181eee0ebSPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
183281eee0ebSPyun YongHyeon 				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
183381eee0ebSPyun YongHyeon 		}
183481eee0ebSPyun YongHyeon 		if (sc->rl_ldata.rl_jrx_sparemap)
183581eee0ebSPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
183681eee0ebSPyun YongHyeon 			    sc->rl_ldata.rl_jrx_sparemap);
183781eee0ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
183881eee0ebSPyun YongHyeon 	}
1839a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1840a94100faSBill Paul 
1841a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
18420534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap)
1843a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1844a94100faSBill Paul 			    sc->rl_ldata.rl_smap);
18450534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats)
18460534aae0SPyun YongHyeon 			bus_dmamem_free(sc->rl_ldata.rl_stag,
18470534aae0SPyun YongHyeon 			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1848a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1849a94100faSBill Paul 	}
1850a94100faSBill Paul 
1851a94100faSBill Paul 	if (sc->rl_parent_tag)
1852a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1853a94100faSBill Paul 
1854a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1855a94100faSBill Paul 
1856a94100faSBill Paul 	return (0);
1857a94100faSBill Paul }
1858a94100faSBill Paul 
1859d65abd66SPyun YongHyeon static __inline void
18607b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1861a94100faSBill Paul {
1862d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1863d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1864d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1865a94100faSBill Paul 
186681eee0ebSPyun YongHyeon 	if (sc->rl_ifp->if_mtu > RL_MTU &&
186781eee0ebSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
186881eee0ebSPyun YongHyeon 		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
186981eee0ebSPyun YongHyeon 	else
1870d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1871d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1872d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1873d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1874d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1875d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1876d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1877d65abd66SPyun YongHyeon }
1878d65abd66SPyun YongHyeon 
1879d65abd66SPyun YongHyeon static int
18807b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1881d65abd66SPyun YongHyeon {
1882d65abd66SPyun YongHyeon 	struct mbuf		*m;
1883d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1884d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1885d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1886d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1887d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1888d65abd66SPyun YongHyeon 	int			error, nsegs;
1889d65abd66SPyun YongHyeon 
1890c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1891d65abd66SPyun YongHyeon 	if (m == NULL)
1892a94100faSBill Paul 		return (ENOBUFS);
1893a94100faSBill Paul 
1894a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
189522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
189622a11c96SJohn-Mark Gurney 	/*
189722a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
189822a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
189922a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
190022a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
190122a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
190222a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
190322a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
190422a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
190522a11c96SJohn-Mark Gurney 	 */
190622a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
190722a11c96SJohn-Mark Gurney #endif
1908d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1909d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1910d65abd66SPyun YongHyeon 	if (error != 0) {
1911d65abd66SPyun YongHyeon 		m_freem(m);
1912d65abd66SPyun YongHyeon 		return (ENOBUFS);
1913d65abd66SPyun YongHyeon 	}
1914d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1915a94100faSBill Paul 
1916d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1917d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1918d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1919d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1920d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1921a94100faSBill Paul 	}
1922a94100faSBill Paul 
1923d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1924d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1925d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1926d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1927d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1928d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1929a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1930a94100faSBill Paul 
1931d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1932d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1933d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1934d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1935d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1936d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1937d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1938d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1939d65abd66SPyun YongHyeon 
1940a94100faSBill Paul 	return (0);
1941a94100faSBill Paul }
1942a94100faSBill Paul 
194381eee0ebSPyun YongHyeon static int
194481eee0ebSPyun YongHyeon re_jumbo_newbuf(struct rl_softc *sc, int idx)
194581eee0ebSPyun YongHyeon {
194681eee0ebSPyun YongHyeon 	struct mbuf		*m;
194781eee0ebSPyun YongHyeon 	struct rl_rxdesc	*rxd;
194881eee0ebSPyun YongHyeon 	bus_dma_segment_t	segs[1];
194981eee0ebSPyun YongHyeon 	bus_dmamap_t		map;
195081eee0ebSPyun YongHyeon 	struct rl_desc		*desc;
195181eee0ebSPyun YongHyeon 	uint32_t		cmdstat;
195281eee0ebSPyun YongHyeon 	int			error, nsegs;
195381eee0ebSPyun YongHyeon 
1954c6499eccSGleb Smirnoff 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
195581eee0ebSPyun YongHyeon 	if (m == NULL)
195681eee0ebSPyun YongHyeon 		return (ENOBUFS);
195781eee0ebSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
195881eee0ebSPyun YongHyeon #ifdef RE_FIXUP_RX
195981eee0ebSPyun YongHyeon 	m_adj(m, RE_ETHER_ALIGN);
196081eee0ebSPyun YongHyeon #endif
196181eee0ebSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
196281eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
196381eee0ebSPyun YongHyeon 	if (error != 0) {
196481eee0ebSPyun YongHyeon 		m_freem(m);
196581eee0ebSPyun YongHyeon 		return (ENOBUFS);
196681eee0ebSPyun YongHyeon 	}
196781eee0ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
196881eee0ebSPyun YongHyeon 
196981eee0ebSPyun YongHyeon 	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
197081eee0ebSPyun YongHyeon 	if (rxd->rx_m != NULL) {
197181eee0ebSPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
197281eee0ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
197381eee0ebSPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
197481eee0ebSPyun YongHyeon 	}
197581eee0ebSPyun YongHyeon 
197681eee0ebSPyun YongHyeon 	rxd->rx_m = m;
197781eee0ebSPyun YongHyeon 	map = rxd->rx_dmamap;
197881eee0ebSPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
197981eee0ebSPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
198081eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_jrx_sparemap = map;
198181eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
198281eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
198381eee0ebSPyun YongHyeon 
198481eee0ebSPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
198581eee0ebSPyun YongHyeon 	desc->rl_vlanctl = 0;
198681eee0ebSPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
198781eee0ebSPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
198881eee0ebSPyun YongHyeon 	cmdstat = segs[0].ds_len;
198981eee0ebSPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
199081eee0ebSPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
199181eee0ebSPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
199281eee0ebSPyun YongHyeon 
199381eee0ebSPyun YongHyeon 	return (0);
199481eee0ebSPyun YongHyeon }
199581eee0ebSPyun YongHyeon 
199622a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
199722a11c96SJohn-Mark Gurney static __inline void
19987b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
199922a11c96SJohn-Mark Gurney {
200022a11c96SJohn-Mark Gurney 	int                     i;
200122a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
200222a11c96SJohn-Mark Gurney 
200322a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
200422a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
200522a11c96SJohn-Mark Gurney 
200622a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
200722a11c96SJohn-Mark Gurney 		*dst++ = *src++;
200822a11c96SJohn-Mark Gurney 
200922a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
201022a11c96SJohn-Mark Gurney }
201122a11c96SJohn-Mark Gurney #endif
201222a11c96SJohn-Mark Gurney 
2013a94100faSBill Paul static int
20147b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
2015a94100faSBill Paul {
2016d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2017d65abd66SPyun YongHyeon 	int			i;
201897b9d4baSJohn-Mark Gurney 
201997b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
202097b9d4baSJohn-Mark Gurney 
2021d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
2022d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
2023d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
2024d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
2025579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2026579a6e3cSLuigi Rizzo 	re_netmap_tx_init(sc);
2027579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2028d65abd66SPyun YongHyeon 	/* Set EOR. */
2029d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
2030d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
2031a94100faSBill Paul 
2032a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2033d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
2034d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2035d65abd66SPyun YongHyeon 
2036a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
2037a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
2038d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
2039a94100faSBill Paul 
2040a94100faSBill Paul 	return (0);
2041a94100faSBill Paul }
2042a94100faSBill Paul 
2043a94100faSBill Paul static int
20447b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
2045a94100faSBill Paul {
2046d65abd66SPyun YongHyeon 	int			error, i;
2047a94100faSBill Paul 
2048d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
2049d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2050d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2051d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
2052d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
2053d65abd66SPyun YongHyeon 			return (error);
2054a94100faSBill Paul 	}
2055579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2056579a6e3cSLuigi Rizzo 	re_netmap_rx_init(sc);
2057579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2058a94100faSBill Paul 
2059a94100faSBill Paul 	/* Flush the RX descriptors */
2060a94100faSBill Paul 
2061a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2062a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2063a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2064a94100faSBill Paul 
2065a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
2066a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
2067502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
2068a94100faSBill Paul 
2069a94100faSBill Paul 	return (0);
2070a94100faSBill Paul }
2071a94100faSBill Paul 
207281eee0ebSPyun YongHyeon static int
207381eee0ebSPyun YongHyeon re_jrx_list_init(struct rl_softc *sc)
207481eee0ebSPyun YongHyeon {
207581eee0ebSPyun YongHyeon 	int			error, i;
207681eee0ebSPyun YongHyeon 
207781eee0ebSPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
207881eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
207981eee0ebSPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
208081eee0ebSPyun YongHyeon 		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
208181eee0ebSPyun YongHyeon 		if ((error = re_jumbo_newbuf(sc, i)) != 0)
208281eee0ebSPyun YongHyeon 			return (error);
208381eee0ebSPyun YongHyeon 	}
208481eee0ebSPyun YongHyeon 
208581eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
208681eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_list_map,
208781eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
208881eee0ebSPyun YongHyeon 
208981eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_rx_prodidx = 0;
209081eee0ebSPyun YongHyeon 	sc->rl_head = sc->rl_tail = NULL;
2091502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
209281eee0ebSPyun YongHyeon 
209381eee0ebSPyun YongHyeon 	return (0);
209481eee0ebSPyun YongHyeon }
209581eee0ebSPyun YongHyeon 
2096a94100faSBill Paul /*
2097a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
2098a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
2099a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
2100a94100faSBill Paul  */
2101ed510fb0SBill Paul static int
21021abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2103a94100faSBill Paul {
2104a94100faSBill Paul 	struct mbuf		*m;
2105a94100faSBill Paul 	struct ifnet		*ifp;
210681eee0ebSPyun YongHyeon 	int			i, rxerr, total_len;
2107a94100faSBill Paul 	struct rl_desc		*cur_rx;
2108a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
210981eee0ebSPyun YongHyeon 	int			jumbo, maxpkt = 16, rx_npkts = 0;
2110a94100faSBill Paul 
21115120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
21125120abbfSSam Leffler 
2113fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2114579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2115579a6e3cSLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
21161221cc67SEd Maste 		NA(ifp)->rx_rings[0].nr_kflags |= NKR_PENDINTR;
21171221cc67SEd Maste 		selwakeuppri(&NA(ifp)->rx_rings[0].si, PI_NET);
2118579a6e3cSLuigi Rizzo 		return 0;
2119579a6e3cSLuigi Rizzo 	}
2120579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
212181eee0ebSPyun YongHyeon 	if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
212281eee0ebSPyun YongHyeon 		jumbo = 1;
212381eee0ebSPyun YongHyeon 	else
212481eee0ebSPyun YongHyeon 		jumbo = 0;
2125a94100faSBill Paul 
2126a94100faSBill Paul 	/* Invalidate the descriptor memory */
2127a94100faSBill Paul 
2128a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2129a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2130d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2131a94100faSBill Paul 
2132d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2133d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
21345b6d1d9dSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
21355b6d1d9dSPyun YongHyeon 			break;
2136a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
2137a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
2138d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2139d65abd66SPyun YongHyeon 			break;
2140d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
2141a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
214281eee0ebSPyun YongHyeon 		if (jumbo != 0)
214381eee0ebSPyun YongHyeon 			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
214481eee0ebSPyun YongHyeon 		else
2145d65abd66SPyun YongHyeon 			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2146a94100faSBill Paul 
214781eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
214881eee0ebSPyun YongHyeon 		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
214981eee0ebSPyun YongHyeon 		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
215081eee0ebSPyun YongHyeon 			/*
215181eee0ebSPyun YongHyeon 			 * RTL8168C or later controllers do not
215281eee0ebSPyun YongHyeon 			 * support multi-fragment packet.
215381eee0ebSPyun YongHyeon 			 */
215481eee0ebSPyun YongHyeon 			re_discard_rxbuf(sc, i);
215581eee0ebSPyun YongHyeon 			continue;
215681eee0ebSPyun YongHyeon 		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2157d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
2158d65abd66SPyun YongHyeon 				/*
2159d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
2160d65abd66SPyun YongHyeon 				 * discard all the pieces.
2161d65abd66SPyun YongHyeon 				 */
2162d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
2163d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
2164d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
2165d65abd66SPyun YongHyeon 				}
2166d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2167d65abd66SPyun YongHyeon 				continue;
2168d65abd66SPyun YongHyeon 			}
216922a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
2170a94100faSBill Paul 			if (sc->rl_head == NULL)
2171a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
2172a94100faSBill Paul 			else {
2173a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2174a94100faSBill Paul 				sc->rl_tail->m_next = m;
2175a94100faSBill Paul 				sc->rl_tail = m;
2176a94100faSBill Paul 			}
2177a94100faSBill Paul 			continue;
2178a94100faSBill Paul 		}
2179a94100faSBill Paul 
2180a94100faSBill Paul 		/*
2181a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
2182a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
2183a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
2184a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
2185a94100faSBill Paul 		 * were already used, so to make room for the extra
2186a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
2187a94100faSBill Paul 		 * error' bit and shifted the other status bits
2188a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
2189a94100faSBill Paul 		 * still in the same places. We have already extracted
2190a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
2191a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
2192a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
2193a94100faSBill Paul 		 * them using the 8169 status as though it was in the
2194a94100faSBill Paul 		 * same format as that of the 8139C+.
2195a94100faSBill Paul 		 */
2196a94100faSBill Paul 		if (sc->rl_type == RL_8169)
2197a94100faSBill Paul 			rxstat >>= 1;
2198a94100faSBill Paul 
219922a11c96SJohn-Mark Gurney 		/*
220022a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
220122a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
220222a11c96SJohn-Mark Gurney 		 */
220381eee0ebSPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
220481eee0ebSPyun YongHyeon 			rxerr = 1;
220581eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
220681eee0ebSPyun YongHyeon 			    total_len > 8191 &&
220781eee0ebSPyun YongHyeon 			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
220881eee0ebSPyun YongHyeon 				rxerr = 0;
220981eee0ebSPyun YongHyeon 			if (rxerr != 0) {
2210a94100faSBill Paul 				ifp->if_ierrors++;
2211a94100faSBill Paul 				/*
2212a94100faSBill Paul 				 * If this is part of a multi-fragment packet,
2213a94100faSBill Paul 				 * discard all the pieces.
2214a94100faSBill Paul 				 */
2215a94100faSBill Paul 				if (sc->rl_head != NULL) {
2216a94100faSBill Paul 					m_freem(sc->rl_head);
2217a94100faSBill Paul 					sc->rl_head = sc->rl_tail = NULL;
2218a94100faSBill Paul 				}
2219d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2220a94100faSBill Paul 				continue;
2221a94100faSBill Paul 			}
222281eee0ebSPyun YongHyeon 		}
2223a94100faSBill Paul 
2224a94100faSBill Paul 		/*
2225a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
2226a94100faSBill Paul 		 * reload the current one.
2227a94100faSBill Paul 		 */
222881eee0ebSPyun YongHyeon 		if (jumbo != 0)
222981eee0ebSPyun YongHyeon 			rxerr = re_jumbo_newbuf(sc, i);
223081eee0ebSPyun YongHyeon 		else
223181eee0ebSPyun YongHyeon 			rxerr = re_newbuf(sc, i);
223281eee0ebSPyun YongHyeon 		if (rxerr != 0) {
2233d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
2234a94100faSBill Paul 			if (sc->rl_head != NULL) {
2235a94100faSBill Paul 				m_freem(sc->rl_head);
2236a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
2237a94100faSBill Paul 			}
2238d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
2239a94100faSBill Paul 			continue;
2240a94100faSBill Paul 		}
2241a94100faSBill Paul 
2242a94100faSBill Paul 		if (sc->rl_head != NULL) {
224381eee0ebSPyun YongHyeon 			if (jumbo != 0)
224481eee0ebSPyun YongHyeon 				m->m_len = total_len;
224581eee0ebSPyun YongHyeon 			else {
224622a11c96SJohn-Mark Gurney 				m->m_len = total_len % RE_RX_DESC_BUFLEN;
224722a11c96SJohn-Mark Gurney 				if (m->m_len == 0)
224822a11c96SJohn-Mark Gurney 					m->m_len = RE_RX_DESC_BUFLEN;
224981eee0ebSPyun YongHyeon 			}
2250a94100faSBill Paul 			/*
2251a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
2252a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
2253a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
2254a94100faSBill Paul 			 * care about anyway.
2255a94100faSBill Paul 			 */
2256a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
2257a94100faSBill Paul 				sc->rl_tail->m_len -=
2258a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
2259a94100faSBill Paul 				m_freem(m);
2260a94100faSBill Paul 			} else {
2261a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
2262a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2263a94100faSBill Paul 				sc->rl_tail->m_next = m;
2264a94100faSBill Paul 			}
2265a94100faSBill Paul 			m = sc->rl_head;
2266a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
2267a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2268a94100faSBill Paul 		} else
2269a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
2270a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
2271a94100faSBill Paul 
227222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
227322a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
227422a11c96SJohn-Mark Gurney #endif
2275a94100faSBill Paul 		ifp->if_ipackets++;
2276a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
2277a94100faSBill Paul 
2278a94100faSBill Paul 		/* Do RX checksumming if enabled */
2279a94100faSBill Paul 
2280a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2281deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2282a94100faSBill Paul 				/* Check IP header checksum */
2283a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
2284deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2285deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2286a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2287deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2288deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2289a94100faSBill Paul 
2290a94100faSBill Paul 				/* Check TCP/UDP checksum */
2291a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
2292a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2293a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
2294a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2295a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
2296a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2297a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
2298a94100faSBill Paul 				}
2299deb5c680SPyun YongHyeon 			} else {
2300deb5c680SPyun YongHyeon 				/*
2301deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2302deb5c680SPyun YongHyeon 				 */
2303deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2304deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2305deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2306deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2307deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2308deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2309deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2310deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2311deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
2312deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2313deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
2314deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2315deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2316deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2317deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
2318deb5c680SPyun YongHyeon 				}
2319deb5c680SPyun YongHyeon 			}
2320a94100faSBill Paul 		}
2321ed510fb0SBill Paul 		maxpkt--;
2322d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
232378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
2324bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
232578ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2326d147662cSGleb Smirnoff 		}
23275120abbfSSam Leffler 		RL_UNLOCK(sc);
2328a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
23295120abbfSSam Leffler 		RL_LOCK(sc);
23301abcdbd1SAttilio Rao 		rx_npkts++;
2331a94100faSBill Paul 	}
2332a94100faSBill Paul 
2333a94100faSBill Paul 	/* Flush the RX DMA ring */
2334a94100faSBill Paul 
2335a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2336a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2337a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2338a94100faSBill Paul 
2339a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
2340ed510fb0SBill Paul 
23411abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
23421abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2343ed510fb0SBill Paul 	if (maxpkt)
2344ed510fb0SBill Paul 		return (EAGAIN);
2345ed510fb0SBill Paul 
2346ed510fb0SBill Paul 	return (0);
2347a94100faSBill Paul }
2348a94100faSBill Paul 
2349a94100faSBill Paul static void
23507b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2351a94100faSBill Paul {
2352a94100faSBill Paul 	struct ifnet		*ifp;
2353d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2354a94100faSBill Paul 	u_int32_t		txstat;
2355d65abd66SPyun YongHyeon 	int			cons;
2356d65abd66SPyun YongHyeon 
2357d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2358d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2359d65abd66SPyun YongHyeon 		return;
2360a94100faSBill Paul 
2361fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2362579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2363579a6e3cSLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
2364579a6e3cSLuigi Rizzo 		selwakeuppri(&NA(ifp)->tx_rings[0].si, PI_NET);
2365579a6e3cSLuigi Rizzo 		return;
2366579a6e3cSLuigi Rizzo 	}
2367579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2368a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2369a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2370a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2371d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2372a94100faSBill Paul 
2373d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2374d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2375d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2376d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2377a94100faSBill Paul 			break;
2378a94100faSBill Paul 		/*
2379a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2380a94100faSBill Paul 		 * in a fragment chain, which also happens to
2381a94100faSBill Paul 		 * be the only place where the TX status bits
2382a94100faSBill Paul 		 * are valid.
2383a94100faSBill Paul 		 */
2384a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2385d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2386d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2387d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2388d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2389d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2390d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2391d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2392d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2393d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2394a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2395a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2396a94100faSBill Paul 				ifp->if_collisions++;
2397a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2398a94100faSBill Paul 				ifp->if_oerrors++;
2399a94100faSBill Paul 			else
2400a94100faSBill Paul 				ifp->if_opackets++;
2401a94100faSBill Paul 		}
2402a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2403d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2404a94100faSBill Paul 	}
2405d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2406a94100faSBill Paul 
2407a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2408a94100faSBill Paul 
2409d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2410ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2411a94100faSBill Paul 		/*
2412b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2413b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2414a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2415a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2416a94100faSBill Paul 		 */
2417a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2418ed510fb0SBill Paul #endif
2419b4b95879SMarius Strobl 	} else
2420b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2421a94100faSBill Paul }
2422a94100faSBill Paul 
2423a94100faSBill Paul static void
24247b5ffebfSPyun YongHyeon re_tick(void *xsc)
2425a94100faSBill Paul {
2426a94100faSBill Paul 	struct rl_softc		*sc;
2427d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2428a94100faSBill Paul 
2429a94100faSBill Paul 	sc = xsc;
243097b9d4baSJohn-Mark Gurney 
243197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
243297b9d4baSJohn-Mark Gurney 
24331d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2434a94100faSBill Paul 	mii_tick(mii);
24350fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
24360fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2437c2d2e19cSPyun YongHyeon 	/*
2438c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2439c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2440c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2441c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2442c2d2e19cSPyun YongHyeon 	 */
2443c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2444130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2445d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2446a94100faSBill Paul }
2447a94100faSBill Paul 
2448a94100faSBill Paul #ifdef DEVICE_POLLING
24491abcdbd1SAttilio Rao static int
2450a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2451a94100faSBill Paul {
2452a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
24531abcdbd1SAttilio Rao 	int rx_npkts = 0;
2454a94100faSBill Paul 
2455a94100faSBill Paul 	RL_LOCK(sc);
245640929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
24571abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
245897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
24591abcdbd1SAttilio Rao 	return (rx_npkts);
246097b9d4baSJohn-Mark Gurney }
246197b9d4baSJohn-Mark Gurney 
24621abcdbd1SAttilio Rao static int
246397b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
246497b9d4baSJohn-Mark Gurney {
246597b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
24661abcdbd1SAttilio Rao 	int rx_npkts;
246797b9d4baSJohn-Mark Gurney 
246897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
246997b9d4baSJohn-Mark Gurney 
2470a94100faSBill Paul 	sc->rxcycles = count;
24711abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2472a94100faSBill Paul 	re_txeof(sc);
2473a94100faSBill Paul 
247437652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2475d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2476a94100faSBill Paul 
2477a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2478a94100faSBill Paul 		u_int16_t       status;
2479a94100faSBill Paul 
2480a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2481a94100faSBill Paul 		if (status == 0xffff)
24821abcdbd1SAttilio Rao 			return (rx_npkts);
2483a94100faSBill Paul 		if (status)
2484a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2485818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2486818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2487818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2488a94100faSBill Paul 
2489a94100faSBill Paul 		/*
2490a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2491a94100faSBill Paul 		 */
2492a94100faSBill Paul 
24938476c243SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR) {
24948476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
249597b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2496a94100faSBill Paul 		}
24978476c243SPyun YongHyeon 	}
24981abcdbd1SAttilio Rao 	return (rx_npkts);
2499a94100faSBill Paul }
2500a94100faSBill Paul #endif /* DEVICE_POLLING */
2501a94100faSBill Paul 
2502ef544f63SPaolo Pisati static int
25037b5ffebfSPyun YongHyeon re_intr(void *arg)
2504a94100faSBill Paul {
2505a94100faSBill Paul 	struct rl_softc		*sc;
2506ed510fb0SBill Paul 	uint16_t		status;
2507a94100faSBill Paul 
2508a94100faSBill Paul 	sc = arg;
2509ed510fb0SBill Paul 
2510ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2511498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2512ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2513ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2514ed510fb0SBill Paul 
2515ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2516ed510fb0SBill Paul 
2517ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2518ed510fb0SBill Paul }
2519ed510fb0SBill Paul 
2520ed510fb0SBill Paul static void
25217b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2522ed510fb0SBill Paul {
2523ed510fb0SBill Paul 	struct rl_softc		*sc;
2524ed510fb0SBill Paul 	struct ifnet		*ifp;
2525ed510fb0SBill Paul 	u_int16_t		status;
2526ed510fb0SBill Paul 	int			rval = 0;
2527ed510fb0SBill Paul 
2528ed510fb0SBill Paul 	sc = arg;
2529ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2530a94100faSBill Paul 
2531a94100faSBill Paul 	RL_LOCK(sc);
253297b9d4baSJohn-Mark Gurney 
2533a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2534a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2535a94100faSBill Paul 
2536d65abd66SPyun YongHyeon 	if (sc->suspended ||
2537d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2538ed510fb0SBill Paul 		RL_UNLOCK(sc);
2539ed510fb0SBill Paul 		return;
2540ed510fb0SBill Paul 	}
2541a94100faSBill Paul 
2542ed510fb0SBill Paul #ifdef DEVICE_POLLING
2543ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2544ed510fb0SBill Paul 		RL_UNLOCK(sc);
2545ed510fb0SBill Paul 		return;
2546ed510fb0SBill Paul 	}
2547ed510fb0SBill Paul #endif
2548a94100faSBill Paul 
2549ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
25501abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2551ed510fb0SBill Paul 
2552818951afSPyun YongHyeon 	/*
2553818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2554818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2555818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2556818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2557818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2558818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2559818951afSPyun YongHyeon 	 */
2560818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2561818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2562818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
25633d85c23dSPyun YongHyeon 	if (status & (
2564ed510fb0SBill Paul #ifdef RE_TX_MODERATION
25653d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2566ed510fb0SBill Paul #else
25673d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2568ed510fb0SBill Paul #endif
2569ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2570a94100faSBill Paul 		re_txeof(sc);
2571a94100faSBill Paul 
25728476c243SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
25738476c243SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
257497b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
25758476c243SPyun YongHyeon 	}
2576a94100faSBill Paul 
257752732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2578d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2579a94100faSBill Paul 
2580a94100faSBill Paul 	RL_UNLOCK(sc);
2581ed510fb0SBill Paul 
2582ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2583ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2584ed510fb0SBill Paul 		return;
2585ed510fb0SBill Paul 	}
2586ed510fb0SBill Paul 
2587ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2588a94100faSBill Paul }
2589a94100faSBill Paul 
2590502be0f7SPyun YongHyeon static void
2591502be0f7SPyun YongHyeon re_intr_msi(void *xsc)
2592502be0f7SPyun YongHyeon {
2593502be0f7SPyun YongHyeon 	struct rl_softc		*sc;
2594502be0f7SPyun YongHyeon 	struct ifnet		*ifp;
2595502be0f7SPyun YongHyeon 	uint16_t		intrs, status;
2596502be0f7SPyun YongHyeon 
2597502be0f7SPyun YongHyeon 	sc = xsc;
2598502be0f7SPyun YongHyeon 	RL_LOCK(sc);
2599502be0f7SPyun YongHyeon 
2600502be0f7SPyun YongHyeon 	ifp = sc->rl_ifp;
2601502be0f7SPyun YongHyeon #ifdef DEVICE_POLLING
2602502be0f7SPyun YongHyeon 	if (ifp->if_capenable & IFCAP_POLLING) {
2603502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2604502be0f7SPyun YongHyeon 		return;
2605502be0f7SPyun YongHyeon 	}
2606502be0f7SPyun YongHyeon #endif
2607502be0f7SPyun YongHyeon 	/* Disable interrupts. */
2608502be0f7SPyun YongHyeon 	CSR_WRITE_2(sc, RL_IMR, 0);
2609502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2610502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2611502be0f7SPyun YongHyeon 		return;
2612502be0f7SPyun YongHyeon 	}
2613502be0f7SPyun YongHyeon 
2614502be0f7SPyun YongHyeon 	intrs = RL_INTRS_CPLUS;
2615502be0f7SPyun YongHyeon 	status = CSR_READ_2(sc, RL_ISR);
2616502be0f7SPyun YongHyeon         CSR_WRITE_2(sc, RL_ISR, status);
2617502be0f7SPyun YongHyeon 	if (sc->rl_int_rx_act > 0) {
2618502be0f7SPyun YongHyeon 		intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2619502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2620502be0f7SPyun YongHyeon 		status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2621502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2622502be0f7SPyun YongHyeon 	}
2623502be0f7SPyun YongHyeon 
2624502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR |
2625502be0f7SPyun YongHyeon 	    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) {
2626502be0f7SPyun YongHyeon 		re_rxeof(sc, NULL);
2627502be0f7SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2628502be0f7SPyun YongHyeon 			if (sc->rl_int_rx_mod != 0 &&
2629502be0f7SPyun YongHyeon 			    (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR |
2630502be0f7SPyun YongHyeon 			    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) {
2631502be0f7SPyun YongHyeon 				/* Rearm one-shot timer. */
2632502be0f7SPyun YongHyeon 				CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2633502be0f7SPyun YongHyeon 				intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR |
2634502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN);
2635502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 1;
2636502be0f7SPyun YongHyeon 			} else {
2637502be0f7SPyun YongHyeon 				intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR |
2638502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN;
2639502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 0;
2640502be0f7SPyun YongHyeon 			}
2641502be0f7SPyun YongHyeon 		}
2642502be0f7SPyun YongHyeon 	}
2643502be0f7SPyun YongHyeon 
2644502be0f7SPyun YongHyeon 	/*
2645502be0f7SPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2646502be0f7SPyun YongHyeon 	 * while an existing transmission is in progress. If
2647502be0f7SPyun YongHyeon 	 * the transmitter goes idle but there are still
2648502be0f7SPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2649502be0f7SPyun YongHyeon 	 * channel here to flush them out. This only seems to
2650502be0f7SPyun YongHyeon 	 * be required with the PCIe devices.
2651502be0f7SPyun YongHyeon 	 */
2652502be0f7SPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2653502be0f7SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2654502be0f7SPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2655502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL))
2656502be0f7SPyun YongHyeon 		re_txeof(sc);
2657502be0f7SPyun YongHyeon 
2658502be0f7SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
2659502be0f7SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2660502be0f7SPyun YongHyeon 		re_init_locked(sc);
2661502be0f7SPyun YongHyeon 	}
2662502be0f7SPyun YongHyeon 
2663502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2664502be0f7SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2665502be0f7SPyun YongHyeon 			re_start_locked(ifp);
2666502be0f7SPyun YongHyeon 		CSR_WRITE_2(sc, RL_IMR, intrs);
2667502be0f7SPyun YongHyeon 	}
2668502be0f7SPyun YongHyeon 	RL_UNLOCK(sc);
2669502be0f7SPyun YongHyeon }
2670502be0f7SPyun YongHyeon 
2671d65abd66SPyun YongHyeon static int
26727b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2673d65abd66SPyun YongHyeon {
2674d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2675d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2676d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2677d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2678d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2679d65abd66SPyun YongHyeon 	int			nsegs, prod;
2680d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2681d65abd66SPyun YongHyeon 	int			padlen;
2682ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2683a94100faSBill Paul 
2684d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2685738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
26860fc4974fSBill Paul 
26870fc4974fSBill Paul 	/*
26880fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
26890fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
26900fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
26910fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
26920fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
26930fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
269499c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
26950fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2696d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
26970fc4974fSBill Paul 	 */
2698f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2699deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
270099c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2701d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2702d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2703d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2704c6499eccSGleb Smirnoff 			m_new = m_dup(*m_head, M_NOWAIT);
2705d65abd66SPyun YongHyeon 			m_freem(*m_head);
2706d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2707d65abd66SPyun YongHyeon 				*m_head = NULL;
2708a94100faSBill Paul 				return (ENOBUFS);
2709a94100faSBill Paul 			}
2710d65abd66SPyun YongHyeon 			*m_head = m_new;
2711d65abd66SPyun YongHyeon 		}
2712d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2713d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
2714c6499eccSGleb Smirnoff 			m_new = m_defrag(*m_head, M_NOWAIT);
2715b4b95879SMarius Strobl 			if (m_new == NULL) {
2716b4b95879SMarius Strobl 				m_freem(*m_head);
2717b4b95879SMarius Strobl 				*m_head = NULL;
271880a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2719b4b95879SMarius Strobl 			}
2720d65abd66SPyun YongHyeon 		} else
2721d65abd66SPyun YongHyeon 			m_new = *m_head;
2722a94100faSBill Paul 
27230fc4974fSBill Paul 		/*
27240fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
27250fc4974fSBill Paul 		 * to avoid leaking data.
27260fc4974fSBill Paul 		 */
2727d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2728d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
27290fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2730d65abd66SPyun YongHyeon 		*m_head = m_new;
27310fc4974fSBill Paul 	}
27320fc4974fSBill Paul 
2733d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2734d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2735d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2736d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2737d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2738c6499eccSGleb Smirnoff 		m_new = m_collapse(*m_head, M_NOWAIT, RL_NTXSEGS);
2739d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2740d65abd66SPyun YongHyeon 			m_freem(*m_head);
2741b4b95879SMarius Strobl 			*m_head = NULL;
2742d65abd66SPyun YongHyeon 			return (ENOBUFS);
2743a94100faSBill Paul 		}
2744d65abd66SPyun YongHyeon 		*m_head = m_new;
2745d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2746d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2747d65abd66SPyun YongHyeon 		if (error != 0) {
2748d65abd66SPyun YongHyeon 			m_freem(*m_head);
2749d65abd66SPyun YongHyeon 			*m_head = NULL;
2750d65abd66SPyun YongHyeon 			return (error);
2751a94100faSBill Paul 		}
2752d65abd66SPyun YongHyeon 	} else if (error != 0)
2753d65abd66SPyun YongHyeon 		return (error);
2754d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2755d65abd66SPyun YongHyeon 		m_freem(*m_head);
2756d65abd66SPyun YongHyeon 		*m_head = NULL;
2757d65abd66SPyun YongHyeon 		return (EIO);
2758d65abd66SPyun YongHyeon 	}
2759d65abd66SPyun YongHyeon 
2760d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2761d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2762d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2763d65abd66SPyun YongHyeon 		return (ENOBUFS);
2764d65abd66SPyun YongHyeon 	}
2765d65abd66SPyun YongHyeon 
2766d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2767d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2768a94100faSBill Paul 
2769a94100faSBill Paul 	/*
2770d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2771d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2772d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2773d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2774a94100faSBill Paul 	 */
2775deb5c680SPyun YongHyeon 	vlanctl = 0;
2776d65abd66SPyun YongHyeon 	csum_flags = 0;
2777d6d7d923SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2778d6d7d923SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2779d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND;
2780d6d7d923SPyun YongHyeon 			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2781d6d7d923SPyun YongHyeon 			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2782d6d7d923SPyun YongHyeon 		} else {
2783d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND |
2784d65abd66SPyun YongHyeon 			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2785d65abd66SPyun YongHyeon 			    RL_TDESC_CMD_MSSVAL_SHIFT);
2786d6d7d923SPyun YongHyeon 		}
2787d6d7d923SPyun YongHyeon 	} else {
278899c8ae87SPyun YongHyeon 		/*
278999c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
279099c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
279199c8ae87SPyun YongHyeon 		 * does't make effects.
279299c8ae87SPyun YongHyeon 		 */
279399c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2794deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2795d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2796deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2797deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2798d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2799deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2800deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2801d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2802deb5c680SPyun YongHyeon 			} else {
2803deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2804deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2805deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2806deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2807deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2808deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2809deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2810deb5c680SPyun YongHyeon 			}
2811d65abd66SPyun YongHyeon 		}
281299c8ae87SPyun YongHyeon 	}
2813a94100faSBill Paul 
2814ccf34c81SPyun YongHyeon 	/*
2815ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2816ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2817ccf34c81SPyun YongHyeon 	 * transmission attempt.
2818ccf34c81SPyun YongHyeon 	 */
2819ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2820bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2821deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2822ccf34c81SPyun YongHyeon 
2823d65abd66SPyun YongHyeon 	si = prod;
2824d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2825d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2826deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2827d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2828d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2829d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2830d65abd66SPyun YongHyeon 		if (i != 0)
2831d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2832d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2833d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2834d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2835d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2836d65abd66SPyun YongHyeon 	}
2837d65abd66SPyun YongHyeon 	/* Update producer index. */
2838d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2839a94100faSBill Paul 
2840d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2841d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2842d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2843d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2844d65abd66SPyun YongHyeon 
2845d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2846d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2847d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2848a94100faSBill Paul 
2849d65abd66SPyun YongHyeon 	/*
2850d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2851d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2852d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2853d65abd66SPyun YongHyeon 	 */
2854d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2855d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2856d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2857d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2858d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2859a94100faSBill Paul 
2860a94100faSBill Paul 	return (0);
2861a94100faSBill Paul }
2862a94100faSBill Paul 
286397b9d4baSJohn-Mark Gurney static void
2864d180a66fSPyun YongHyeon re_start(struct ifnet *ifp)
286597b9d4baSJohn-Mark Gurney {
2866d180a66fSPyun YongHyeon 	struct rl_softc		*sc;
286797b9d4baSJohn-Mark Gurney 
2868d180a66fSPyun YongHyeon 	sc = ifp->if_softc;
2869d180a66fSPyun YongHyeon 	RL_LOCK(sc);
2870d180a66fSPyun YongHyeon 	re_start_locked(ifp);
2871d180a66fSPyun YongHyeon 	RL_UNLOCK(sc);
287297b9d4baSJohn-Mark Gurney }
287397b9d4baSJohn-Mark Gurney 
2874a94100faSBill Paul /*
2875a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2876a94100faSBill Paul  */
2877a94100faSBill Paul static void
2878d180a66fSPyun YongHyeon re_start_locked(struct ifnet *ifp)
2879a94100faSBill Paul {
2880a94100faSBill Paul 	struct rl_softc		*sc;
2881d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2882d65abd66SPyun YongHyeon 	int			queued;
2883a94100faSBill Paul 
2884a94100faSBill Paul 	sc = ifp->if_softc;
288597b9d4baSJohn-Mark Gurney 
2886579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2887579a6e3cSLuigi Rizzo 	/* XXX is this necessary ? */
2888579a6e3cSLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
2889579a6e3cSLuigi Rizzo 		struct netmap_kring *kring = &NA(ifp)->tx_rings[0];
2890579a6e3cSLuigi Rizzo 		if (sc->rl_ldata.rl_tx_prodidx != kring->nr_hwcur) {
2891579a6e3cSLuigi Rizzo 			/* kick the tx unit */
2892579a6e3cSLuigi Rizzo 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2893579a6e3cSLuigi Rizzo #ifdef RE_TX_MODERATION
2894579a6e3cSLuigi Rizzo 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2895579a6e3cSLuigi Rizzo #endif
2896579a6e3cSLuigi Rizzo 			sc->rl_watchdog_timer = 5;
2897579a6e3cSLuigi Rizzo 		}
2898579a6e3cSLuigi Rizzo 		return;
2899579a6e3cSLuigi Rizzo 	}
2900579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2901d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2902d180a66fSPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
2903ed510fb0SBill Paul 		return;
2904a94100faSBill Paul 
2905d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2906d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
290752732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2908a94100faSBill Paul 		if (m_head == NULL)
2909a94100faSBill Paul 			break;
2910a94100faSBill Paul 
2911d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2912b4b95879SMarius Strobl 			if (m_head == NULL)
2913b4b95879SMarius Strobl 				break;
291452732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
291513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2916a94100faSBill Paul 			break;
2917a94100faSBill Paul 		}
2918a94100faSBill Paul 
2919a94100faSBill Paul 		/*
2920a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2921a94100faSBill Paul 		 * to him.
2922a94100faSBill Paul 		 */
292359a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
292452732175SMax Laier 
292552732175SMax Laier 		queued++;
2926a94100faSBill Paul 	}
2927a94100faSBill Paul 
2928ed510fb0SBill Paul 	if (queued == 0) {
2929ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2930d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2931ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2932ed510fb0SBill Paul #endif
293352732175SMax Laier 		return;
2934ed510fb0SBill Paul 	}
293552732175SMax Laier 
2936a94100faSBill Paul 	/* Flush the TX descriptors */
2937a94100faSBill Paul 
2938a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2939a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2940a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2941a94100faSBill Paul 
29420fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2943a94100faSBill Paul 
2944ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2945a94100faSBill Paul 	/*
2946a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2947a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2948a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2949a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2950a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2951a94100faSBill Paul 	 * the timer count is reset to 0.
2952a94100faSBill Paul 	 */
2953a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2954ed510fb0SBill Paul #endif
2955a94100faSBill Paul 
2956a94100faSBill Paul 	/*
2957a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2958a94100faSBill Paul 	 */
29591d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2960a94100faSBill Paul }
2961a94100faSBill Paul 
2962a94100faSBill Paul static void
296381eee0ebSPyun YongHyeon re_set_jumbo(struct rl_softc *sc, int jumbo)
296481eee0ebSPyun YongHyeon {
296581eee0ebSPyun YongHyeon 
296681eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
296781eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
296881eee0ebSPyun YongHyeon 		return;
296981eee0ebSPyun YongHyeon 	}
297081eee0ebSPyun YongHyeon 
297181eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
297281eee0ebSPyun YongHyeon 	if (jumbo != 0) {
2973e7e7593cSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) |
297481eee0ebSPyun YongHyeon 		    RL_CFG3_JUMBO_EN0);
297581eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
297681eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
297781eee0ebSPyun YongHyeon 			break;
297881eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
2979e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
2980e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) | 0x01);
298181eee0ebSPyun YongHyeon 			break;
298281eee0ebSPyun YongHyeon 		default:
2983e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
2984e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) | RL_CFG4_JUMBO_EN1);
298581eee0ebSPyun YongHyeon 		}
298681eee0ebSPyun YongHyeon 	} else {
2987e7e7593cSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) &
298881eee0ebSPyun YongHyeon 		    ~RL_CFG3_JUMBO_EN0);
298981eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
299081eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
299181eee0ebSPyun YongHyeon 			break;
299281eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
2993e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
2994e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) & ~0x01);
299581eee0ebSPyun YongHyeon 			break;
299681eee0ebSPyun YongHyeon 		default:
2997e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
2998e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) & ~RL_CFG4_JUMBO_EN1);
299981eee0ebSPyun YongHyeon 		}
300081eee0ebSPyun YongHyeon 	}
300181eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
300281eee0ebSPyun YongHyeon 
300381eee0ebSPyun YongHyeon 	switch (sc->rl_hwrev->rl_rev) {
300481eee0ebSPyun YongHyeon 	case RL_HWREV_8168DP:
300581eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
300681eee0ebSPyun YongHyeon 		break;
300781eee0ebSPyun YongHyeon 	default:
300881eee0ebSPyun YongHyeon 		if (jumbo != 0)
300981eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 512);
301081eee0ebSPyun YongHyeon 		else
301181eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 4096);
301281eee0ebSPyun YongHyeon 	}
301381eee0ebSPyun YongHyeon }
301481eee0ebSPyun YongHyeon 
301581eee0ebSPyun YongHyeon static void
30167b5ffebfSPyun YongHyeon re_init(void *xsc)
3017a94100faSBill Paul {
3018a94100faSBill Paul 	struct rl_softc		*sc = xsc;
301997b9d4baSJohn-Mark Gurney 
302097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
302197b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
302297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
302397b9d4baSJohn-Mark Gurney }
302497b9d4baSJohn-Mark Gurney 
302597b9d4baSJohn-Mark Gurney static void
30267b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
302797b9d4baSJohn-Mark Gurney {
3028fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
3029a94100faSBill Paul 	struct mii_data		*mii;
3030566ca8caSJung-uk Kim 	uint32_t		reg;
303170acaecfSPyun YongHyeon 	uint16_t		cfg;
30324d3d7085SBernd Walter 	union {
30334d3d7085SBernd Walter 		uint32_t align_dummy;
30344d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
30354d3d7085SBernd Walter         } eaddr;
3036a94100faSBill Paul 
303797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
303897b9d4baSJohn-Mark Gurney 
3039a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3040a94100faSBill Paul 
30418476c243SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
30428476c243SPyun YongHyeon 		return;
30438476c243SPyun YongHyeon 
3044a94100faSBill Paul 	/*
3045a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
3046a94100faSBill Paul 	 */
3047a94100faSBill Paul 	re_stop(sc);
3048a94100faSBill Paul 
3049b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
3050b659f1f0SPyun YongHyeon 	re_reset(sc);
3051b659f1f0SPyun YongHyeon 
3052a94100faSBill Paul 	/*
30534a814a5eSPyun YongHyeon 	 * For C+ mode, initialize the RX descriptors and mbufs.
30544a814a5eSPyun YongHyeon 	 */
305581eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
305681eee0ebSPyun YongHyeon 		if (ifp->if_mtu > RL_MTU) {
305781eee0ebSPyun YongHyeon 			if (re_jrx_list_init(sc) != 0) {
305881eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
305981eee0ebSPyun YongHyeon 				    "no memory for jumbo RX buffers\n");
306081eee0ebSPyun YongHyeon 				re_stop(sc);
306181eee0ebSPyun YongHyeon 				return;
306281eee0ebSPyun YongHyeon 			}
306381eee0ebSPyun YongHyeon 			/* Disable checksum offloading for jumbo frames. */
306481eee0ebSPyun YongHyeon 			ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
306581eee0ebSPyun YongHyeon 			ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
306681eee0ebSPyun YongHyeon 		} else {
306781eee0ebSPyun YongHyeon 			if (re_rx_list_init(sc) != 0) {
306881eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
306981eee0ebSPyun YongHyeon 				    "no memory for RX buffers\n");
307081eee0ebSPyun YongHyeon 				re_stop(sc);
307181eee0ebSPyun YongHyeon 				return;
307281eee0ebSPyun YongHyeon 			}
307381eee0ebSPyun YongHyeon 		}
307481eee0ebSPyun YongHyeon 		re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
307581eee0ebSPyun YongHyeon 	} else {
30764a814a5eSPyun YongHyeon 		if (re_rx_list_init(sc) != 0) {
30774a814a5eSPyun YongHyeon 			device_printf(sc->rl_dev, "no memory for RX buffers\n");
30784a814a5eSPyun YongHyeon 			re_stop(sc);
30794a814a5eSPyun YongHyeon 			return;
30804a814a5eSPyun YongHyeon 		}
308181eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
308281eee0ebSPyun YongHyeon 		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
308381eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
308481eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 512);
308581eee0ebSPyun YongHyeon 			else
308681eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 4096);
308781eee0ebSPyun YongHyeon 		}
308881eee0ebSPyun YongHyeon 	}
30894a814a5eSPyun YongHyeon 	re_tx_list_init(sc);
30904a814a5eSPyun YongHyeon 
30914a814a5eSPyun YongHyeon 	/*
3092c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
3093edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
3094c2c6548bSBill Paul 	 * before all others.
3095c2c6548bSBill Paul 	 */
309670acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
309770acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
309870acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
309970acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
310070acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
3101deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
3102deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
3103deb5c680SPyun YongHyeon 		/* XXX magic. */
3104deb5c680SPyun YongHyeon 		cfg |= 0x0001;
3105deb5c680SPyun YongHyeon 	} else
3106deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
3107deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
310881eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
310981eee0ebSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
3110566ca8caSJung-uk Kim 		reg = 0x000fff00;
3111e7e7593cSPyun YongHyeon 		if ((CSR_READ_1(sc, sc->rl_cfg2) & RL_CFG2_PCI66MHZ) != 0)
3112566ca8caSJung-uk Kim 			reg |= 0x000000ff;
311381eee0ebSPyun YongHyeon 		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
3114566ca8caSJung-uk Kim 			reg |= 0x00f00000;
3115566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
3116566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
3117566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
3118566ca8caSJung-uk Kim 	}
3119ae644087SPyun YongHyeon 	/*
3120ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
3121ae644087SPyun YongHyeon 	 * allowed in controller.
3122ae644087SPyun YongHyeon 	 */
3123ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
3124ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
3125ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
3126ae644087SPyun YongHyeon 	}
3127c2c6548bSBill Paul 
3128c2c6548bSBill Paul 	/*
3129a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
3130a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
3131a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
3132a94100faSBill Paul 	 */
31334d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
31344d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
3135a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3136ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
3137ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
3138ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
3139ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
3140a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3141a94100faSBill Paul 
3142a94100faSBill Paul 	/*
3143d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
3144d01fac16SPyun YongHyeon 	 */
3145d01fac16SPyun YongHyeon 
3146d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
3147d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
3148d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
3149d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
3150d01fac16SPyun YongHyeon 
3151d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
3152d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
3153d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
3154d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
3155d01fac16SPyun YongHyeon 
3156d01fac16SPyun YongHyeon 	/*
3157a94100faSBill Paul 	 * Enable transmit and receive.
3158a94100faSBill Paul 	 */
3159a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3160a94100faSBill Paul 
3161a94100faSBill Paul 	/*
3162ff191365SJung-uk Kim 	 * Set the initial TX configuration.
3163a94100faSBill Paul 	 */
3164abc8ff44SBill Paul 	if (sc->rl_testmode) {
3165abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
3166abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3167abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
3168a94100faSBill Paul 		else
3169abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3170abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
3171abc8ff44SBill Paul 	} else
3172a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
3173d01fac16SPyun YongHyeon 
3174d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
3175d01fac16SPyun YongHyeon 
3176a94100faSBill Paul 	/*
3177ff191365SJung-uk Kim 	 * Set the initial RX configuration.
3178a94100faSBill Paul 	 */
3179ff191365SJung-uk Kim 	re_set_rxmode(sc);
3180a94100faSBill Paul 
3181483cc440SPyun YongHyeon 	/* Configure interrupt moderation. */
3182483cc440SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
3183483cc440SPyun YongHyeon 		/* Magic from vendor. */
31845e6906eeSPyun YongHyeon 		CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
3185483cc440SPyun YongHyeon 	}
3186483cc440SPyun YongHyeon 
3187a94100faSBill Paul #ifdef DEVICE_POLLING
3188a94100faSBill Paul 	/*
3189a94100faSBill Paul 	 * Disable interrupts if we are polling.
3190a94100faSBill Paul 	 */
319140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3192a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3193a94100faSBill Paul 	else	/* otherwise ... */
319440929967SGleb Smirnoff #endif
3195ed510fb0SBill Paul 
3196a94100faSBill Paul 	/*
3197a94100faSBill Paul 	 * Enable interrupts.
3198a94100faSBill Paul 	 */
3199a94100faSBill Paul 	if (sc->rl_testmode)
3200a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3201a94100faSBill Paul 	else
3202a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3203ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3204a94100faSBill Paul 
3205a94100faSBill Paul 	/* Set initial TX threshold */
3206a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
3207a94100faSBill Paul 
3208a94100faSBill Paul 	/* Start RX/TX process. */
3209a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3210a94100faSBill Paul #ifdef notdef
3211a94100faSBill Paul 	/* Enable receiver and transmitter. */
3212a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3213a94100faSBill Paul #endif
3214a94100faSBill Paul 
3215a94100faSBill Paul 	/*
3216a94100faSBill Paul 	 * Initialize the timer interrupt register so that
3217a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
3218a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
3219502be0f7SPyun YongHyeon 	 * reloaded on each transmit.
3220502be0f7SPyun YongHyeon 	 */
3221502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
3222502be0f7SPyun YongHyeon 	/*
3223502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate TX interrupt
3224a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
3225a94100faSBill Paul 	 */
3226a94100faSBill Paul 	if (sc->rl_type == RL_8169)
3227a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3228a94100faSBill Paul 	else
3229a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3230502be0f7SPyun YongHyeon #else
3231502be0f7SPyun YongHyeon 	/*
3232502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate RX interrupt
3233502be0f7SPyun YongHyeon 	 * moderation.
3234502be0f7SPyun YongHyeon 	 */
3235502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
3236502be0f7SPyun YongHyeon 	    intr_filter == 0) {
3237502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3238502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169,
3239502be0f7SPyun YongHyeon 			    RL_USECS(sc->rl_int_rx_mod));
3240502be0f7SPyun YongHyeon 	} else {
3241502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3242502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0));
3243502be0f7SPyun YongHyeon 	}
3244ed510fb0SBill Paul #endif
3245a94100faSBill Paul 
3246a94100faSBill Paul 	/*
3247a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
3248a94100faSBill Paul 	 * size so we can receive jumbo frames.
3249a94100faSBill Paul 	 */
325089feeee4SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
325181eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
325281eee0ebSPyun YongHyeon 			/*
325381eee0ebSPyun YongHyeon 			 * For controllers that use new jumbo frame scheme,
325481eee0ebSPyun YongHyeon 			 * set maximum size of jumbo frame depedning on
325581eee0ebSPyun YongHyeon 			 * controller revisions.
325681eee0ebSPyun YongHyeon 			 */
325781eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
325881eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
325981eee0ebSPyun YongHyeon 				    sc->rl_hwrev->rl_max_mtu +
326081eee0ebSPyun YongHyeon 				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
326181eee0ebSPyun YongHyeon 				    ETHER_CRC_LEN);
326289feeee4SPyun YongHyeon 			else
326381eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
326481eee0ebSPyun YongHyeon 				    RE_RX_DESC_BUFLEN);
326581eee0ebSPyun YongHyeon 		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
326681eee0ebSPyun YongHyeon 		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
326781eee0ebSPyun YongHyeon 			/* RTL810x has no jumbo frame support. */
326881eee0ebSPyun YongHyeon 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
326981eee0ebSPyun YongHyeon 		} else
3270a94100faSBill Paul 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
327189feeee4SPyun YongHyeon 	}
3272a94100faSBill Paul 
327397b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
3274a94100faSBill Paul 		return;
3275a94100faSBill Paul 
3276e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, CSR_READ_1(sc, sc->rl_cfg1) |
3277e7e7593cSPyun YongHyeon 	    RL_CFG1_DRVLOAD);
3278a94100faSBill Paul 
327913f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
328013f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3281a94100faSBill Paul 
3282351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
32831662c49eSPyun YongHyeon 	mii_mediachg(mii);
32841662c49eSPyun YongHyeon 
32851d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3286d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3287a94100faSBill Paul }
3288a94100faSBill Paul 
3289a94100faSBill Paul /*
3290a94100faSBill Paul  * Set media options.
3291a94100faSBill Paul  */
3292a94100faSBill Paul static int
32937b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
3294a94100faSBill Paul {
3295a94100faSBill Paul 	struct rl_softc		*sc;
3296a94100faSBill Paul 	struct mii_data		*mii;
32976f0f9b12SPyun YongHyeon 	int			error;
3298a94100faSBill Paul 
3299a94100faSBill Paul 	sc = ifp->if_softc;
3300a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3301d1754a9bSJohn Baldwin 	RL_LOCK(sc);
33026f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
3303d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3304a94100faSBill Paul 
33056f0f9b12SPyun YongHyeon 	return (error);
3306a94100faSBill Paul }
3307a94100faSBill Paul 
3308a94100faSBill Paul /*
3309a94100faSBill Paul  * Report current media status.
3310a94100faSBill Paul  */
3311a94100faSBill Paul static void
33127b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3313a94100faSBill Paul {
3314a94100faSBill Paul 	struct rl_softc		*sc;
3315a94100faSBill Paul 	struct mii_data		*mii;
3316a94100faSBill Paul 
3317a94100faSBill Paul 	sc = ifp->if_softc;
3318a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3319a94100faSBill Paul 
3320d1754a9bSJohn Baldwin 	RL_LOCK(sc);
3321a94100faSBill Paul 	mii_pollstat(mii);
3322a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
3323a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
332457c81d92SPyun YongHyeon 	RL_UNLOCK(sc);
3325a94100faSBill Paul }
3326a94100faSBill Paul 
3327a94100faSBill Paul static int
33287b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3329a94100faSBill Paul {
3330a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
3331a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
3332a94100faSBill Paul 	struct mii_data		*mii;
3333bc2a1002SPyun YongHyeon 	uint32_t		rev;
333440929967SGleb Smirnoff 	int			error = 0;
3335a94100faSBill Paul 
3336a94100faSBill Paul 	switch (command) {
3337a94100faSBill Paul 	case SIOCSIFMTU:
333881eee0ebSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN ||
333981eee0ebSPyun YongHyeon 		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu) {
3340c1d0b573SPyun YongHyeon 			error = EINVAL;
3341c1d0b573SPyun YongHyeon 			break;
3342c1d0b573SPyun YongHyeon 		}
3343c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
334481eee0ebSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
3345a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
334681eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
334781eee0ebSPyun YongHyeon 			    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
334881eee0ebSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
334981eee0ebSPyun YongHyeon 				re_init_locked(sc);
335081eee0ebSPyun YongHyeon 			}
3351ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3352ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
335381eee0ebSPyun YongHyeon 				ifp->if_capenable &= ~(IFCAP_TSO4 |
335481eee0ebSPyun YongHyeon 				    IFCAP_VLAN_HWTSO);
3355ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
335681eee0ebSPyun YongHyeon 			}
3357ecafbbb5SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
3358ae644087SPyun YongHyeon 		}
3359d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
3360a94100faSBill Paul 		break;
3361a94100faSBill Paul 	case SIOCSIFFLAGS:
336297b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
3363eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
3364eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3365eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
33663021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3367ff191365SJung-uk Kim 					re_set_rxmode(sc);
3368eed497bbSPyun YongHyeon 			} else
336997b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
3370eed497bbSPyun YongHyeon 		} else {
3371eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3372a94100faSBill Paul 				re_stop(sc);
3373eed497bbSPyun YongHyeon 		}
3374eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
337597b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3376a94100faSBill Paul 		break;
3377a94100faSBill Paul 	case SIOCADDMULTI:
3378a94100faSBill Paul 	case SIOCDELMULTI:
337997b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
33808476c243SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3381ff191365SJung-uk Kim 			re_set_rxmode(sc);
338297b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3383a94100faSBill Paul 		break;
3384a94100faSBill Paul 	case SIOCGIFMEDIA:
3385a94100faSBill Paul 	case SIOCSIFMEDIA:
3386a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
3387a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3388a94100faSBill Paul 		break;
3389a94100faSBill Paul 	case SIOCSIFCAP:
339040929967SGleb Smirnoff 	    {
3391f051cb85SGleb Smirnoff 		int mask, reinit;
3392f051cb85SGleb Smirnoff 
3393f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3394f051cb85SGleb Smirnoff 		reinit = 0;
339540929967SGleb Smirnoff #ifdef DEVICE_POLLING
339640929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
339740929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
339840929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
339940929967SGleb Smirnoff 				if (error)
340040929967SGleb Smirnoff 					return (error);
3401d1754a9bSJohn Baldwin 				RL_LOCK(sc);
340240929967SGleb Smirnoff 				/* Disable interrupts */
340340929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
340440929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
340540929967SGleb Smirnoff 				RL_UNLOCK(sc);
340640929967SGleb Smirnoff 			} else {
340740929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
340840929967SGleb Smirnoff 				/* Enable interrupts. */
340940929967SGleb Smirnoff 				RL_LOCK(sc);
341040929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
341140929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
341240929967SGleb Smirnoff 				RL_UNLOCK(sc);
341340929967SGleb Smirnoff 			}
341440929967SGleb Smirnoff 		}
341540929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3416600af6c2SPyun YongHyeon 		RL_LOCK(sc);
3417d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
3418d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3419d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
3420bc2a1002SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) {
3421bc2a1002SPyun YongHyeon 				rev = sc->rl_hwrev->rl_rev;
3422bc2a1002SPyun YongHyeon 				if (rev == RL_HWREV_8168C ||
3423*3c2a957dSPyun YongHyeon 				    rev == RL_HWREV_8168C_SPIN2 ||
3424*3c2a957dSPyun YongHyeon 				    rev == RL_HWREV_8168CP)
3425bc2a1002SPyun YongHyeon 					ifp->if_hwassist |= CSUM_TCP | CSUM_UDP;
3426a94100faSBill Paul 				else
3427bc2a1002SPyun YongHyeon 					ifp->if_hwassist |= RE_CSUM_FEATURES;
3428bc2a1002SPyun YongHyeon 			} else
3429b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3430f051cb85SGleb Smirnoff 			reinit = 1;
343140929967SGleb Smirnoff 		}
3432d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
3433d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3434d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
3435d3b181aeSPyun YongHyeon 			reinit = 1;
3436d3b181aeSPyun YongHyeon 		}
3437ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
3438fca1e0abSBjoern A. Zeeb 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
3439dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
3440ecafbbb5SPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3441dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
3442dc74159dSPyun YongHyeon 			else
3443dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3444ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3445ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3446ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
3447ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3448ae644087SPyun YongHyeon 			}
3449dc74159dSPyun YongHyeon 		}
3450ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3451ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3452ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3453ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3454ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3455ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3456ecafbbb5SPyun YongHyeon 			/* TSO over VLAN requires VLAN hardware tagging. */
3457ecafbbb5SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3458ecafbbb5SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3459ecafbbb5SPyun YongHyeon 			reinit = 1;
3460ecafbbb5SPyun YongHyeon 		}
346181eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
346281eee0ebSPyun YongHyeon 		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
346381eee0ebSPyun YongHyeon 		    IFCAP_VLAN_HWTSO)) != 0)
346481eee0ebSPyun YongHyeon 				reinit = 1;
34657467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
34667467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
34677467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
34687467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
34697467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
34707467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
34717467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
34727467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
34737467bd53SPyun YongHyeon 		}
34748476c243SPyun YongHyeon 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
34758476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3476600af6c2SPyun YongHyeon 			re_init_locked(sc);
34778476c243SPyun YongHyeon 		}
3478600af6c2SPyun YongHyeon 		RL_UNLOCK(sc);
3479960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
348040929967SGleb Smirnoff 	    }
3481a94100faSBill Paul 		break;
3482a94100faSBill Paul 	default:
3483a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
3484a94100faSBill Paul 		break;
3485a94100faSBill Paul 	}
3486a94100faSBill Paul 
3487a94100faSBill Paul 	return (error);
3488a94100faSBill Paul }
3489a94100faSBill Paul 
3490a94100faSBill Paul static void
34917b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
34921d545c7aSMarius Strobl {
3493130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
3494a94100faSBill Paul 
34951d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
34961d545c7aSMarius Strobl 
34971d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
34981d545c7aSMarius Strobl 		return;
34991d545c7aSMarius Strobl 
3500130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
3501a94100faSBill Paul 	re_txeof(sc);
3502130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3503130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3504130b6dfbSPyun YongHyeon 		    "-- recovering\n");
3505130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3506d180a66fSPyun YongHyeon 			re_start_locked(ifp);
3507130b6dfbSPyun YongHyeon 		return;
3508130b6dfbSPyun YongHyeon 	}
3509130b6dfbSPyun YongHyeon 
3510130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
3511130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
3512130b6dfbSPyun YongHyeon 
35131abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
35148476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
351597b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
3516130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3517d180a66fSPyun YongHyeon 		re_start_locked(ifp);
3518a94100faSBill Paul }
3519a94100faSBill Paul 
3520a94100faSBill Paul /*
3521a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
3522a94100faSBill Paul  * RX and TX lists.
3523a94100faSBill Paul  */
3524a94100faSBill Paul static void
35257b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
3526a94100faSBill Paul {
35270ce0868aSPyun YongHyeon 	int			i;
3528a94100faSBill Paul 	struct ifnet		*ifp;
3529d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
3530d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
3531a94100faSBill Paul 
353297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
353397b9d4baSJohn-Mark Gurney 
3534fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
3535a94100faSBill Paul 
35361d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3537d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
353813f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3539a94100faSBill Paul 
3540fcb220acSPyun YongHyeon 	/*
3541fcb220acSPyun YongHyeon 	 * Disable accepting frames to put RX MAC into idle state.
3542fcb220acSPyun YongHyeon 	 * Otherwise it's possible to get frames while stop command
3543fcb220acSPyun YongHyeon 	 * execution is in progress and controller can DMA the frame
3544fcb220acSPyun YongHyeon 	 * to already freed RX buffer during that period.
3545fcb220acSPyun YongHyeon 	 */
3546fcb220acSPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXCFG, CSR_READ_4(sc, RL_RXCFG) &
3547fcb220acSPyun YongHyeon 	    ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI |
3548fcb220acSPyun YongHyeon 	    RL_RXCFG_RX_BROAD));
3549fcb220acSPyun YongHyeon 
3550eef0e496SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) {
3551eef0e496SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
3552eef0e496SPyun YongHyeon 			if ((CSR_READ_1(sc, sc->rl_txstart) &
3553eef0e496SPyun YongHyeon 			    RL_TXSTART_START) == 0)
3554eef0e496SPyun YongHyeon 				break;
3555eef0e496SPyun YongHyeon 			DELAY(20);
3556eef0e496SPyun YongHyeon 		}
3557eef0e496SPyun YongHyeon 		if (i == 0)
3558eef0e496SPyun YongHyeon 			device_printf(sc->rl_dev,
3559eef0e496SPyun YongHyeon 			    "stopping TX poll timed out!\n");
3560eef0e496SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3561eef0e496SPyun YongHyeon 	} else if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) {
3562ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3563ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
3564eef0e496SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_CMDSTOP_WAIT_TXQ) != 0) {
3565eef0e496SPyun YongHyeon 			for (i = RL_TIMEOUT; i > 0; i--) {
3566eef0e496SPyun YongHyeon 				if ((CSR_READ_4(sc, RL_TXCFG) &
3567eef0e496SPyun YongHyeon 				    RL_TXCFG_QUEUE_EMPTY) != 0)
3568eef0e496SPyun YongHyeon 					break;
3569eef0e496SPyun YongHyeon 				DELAY(100);
3570eef0e496SPyun YongHyeon 			}
3571eef0e496SPyun YongHyeon 			if (i == 0)
3572eef0e496SPyun YongHyeon 				device_printf(sc->rl_dev,
3573eef0e496SPyun YongHyeon 				   "stopping TXQ timed out!\n");
3574eef0e496SPyun YongHyeon 		}
3575eef0e496SPyun YongHyeon 	} else
3576a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3577ead8fc66SPyun YongHyeon 	DELAY(1000);
3578a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3579ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3580a94100faSBill Paul 
3581a94100faSBill Paul 	if (sc->rl_head != NULL) {
3582a94100faSBill Paul 		m_freem(sc->rl_head);
3583a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
3584a94100faSBill Paul 	}
3585a94100faSBill Paul 
3586a94100faSBill Paul 	/* Free the TX list buffers. */
3587d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3588d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
3589d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
3590d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3591d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3592d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3593d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
3594d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
3595d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
3596a94100faSBill Paul 		}
3597a94100faSBill Paul 	}
3598a94100faSBill Paul 
3599a94100faSBill Paul 	/* Free the RX list buffers. */
3600d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3601d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
3602d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
3603cba16362SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
3604d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3605d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3606d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
3607d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
3608d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
3609a94100faSBill Paul 		}
3610a94100faSBill Paul 	}
36111f32d3b7SPyun YongHyeon 
36121f32d3b7SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
36131f32d3b7SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
36141f32d3b7SPyun YongHyeon 			rxd = &sc->rl_ldata.rl_jrx_desc[i];
36151f32d3b7SPyun YongHyeon 			if (rxd->rx_m != NULL) {
36161f32d3b7SPyun YongHyeon 				bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag,
36171f32d3b7SPyun YongHyeon 				    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
36181f32d3b7SPyun YongHyeon 				bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag,
36191f32d3b7SPyun YongHyeon 				    rxd->rx_dmamap);
36201f32d3b7SPyun YongHyeon 				m_freem(rxd->rx_m);
36211f32d3b7SPyun YongHyeon 				rxd->rx_m = NULL;
36221f32d3b7SPyun YongHyeon 			}
36231f32d3b7SPyun YongHyeon 		}
36241f32d3b7SPyun YongHyeon 	}
3625a94100faSBill Paul }
3626a94100faSBill Paul 
3627a94100faSBill Paul /*
3628a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
3629a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
3630a94100faSBill Paul  * resume.
3631a94100faSBill Paul  */
3632a94100faSBill Paul static int
36337b5ffebfSPyun YongHyeon re_suspend(device_t dev)
3634a94100faSBill Paul {
3635a94100faSBill Paul 	struct rl_softc		*sc;
3636a94100faSBill Paul 
3637a94100faSBill Paul 	sc = device_get_softc(dev);
3638a94100faSBill Paul 
363997b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3640a94100faSBill Paul 	re_stop(sc);
36417467bd53SPyun YongHyeon 	re_setwol(sc);
3642a94100faSBill Paul 	sc->suspended = 1;
364397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3644a94100faSBill Paul 
3645a94100faSBill Paul 	return (0);
3646a94100faSBill Paul }
3647a94100faSBill Paul 
3648a94100faSBill Paul /*
3649a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
3650a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
3651a94100faSBill Paul  * appropriate.
3652a94100faSBill Paul  */
3653a94100faSBill Paul static int
36547b5ffebfSPyun YongHyeon re_resume(device_t dev)
3655a94100faSBill Paul {
3656a94100faSBill Paul 	struct rl_softc		*sc;
3657a94100faSBill Paul 	struct ifnet		*ifp;
3658a94100faSBill Paul 
3659a94100faSBill Paul 	sc = device_get_softc(dev);
366097b9d4baSJohn-Mark Gurney 
366197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
366297b9d4baSJohn-Mark Gurney 
3663fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
366461f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
366561f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
366661f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
366761f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
366861f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
366961f45a72SPyun YongHyeon 	}
3670a94100faSBill Paul 
36717467bd53SPyun YongHyeon 	/*
36727467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
36737467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
36747467bd53SPyun YongHyeon 	 */
36757467bd53SPyun YongHyeon 	re_clrwol(sc);
367601d1a6c3SPyun YongHyeon 
367701d1a6c3SPyun YongHyeon 	/* reinitialize interface if necessary */
367801d1a6c3SPyun YongHyeon 	if (ifp->if_flags & IFF_UP)
367901d1a6c3SPyun YongHyeon 		re_init_locked(sc);
368001d1a6c3SPyun YongHyeon 
3681a94100faSBill Paul 	sc->suspended = 0;
368297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3683a94100faSBill Paul 
3684a94100faSBill Paul 	return (0);
3685a94100faSBill Paul }
3686a94100faSBill Paul 
3687a94100faSBill Paul /*
3688a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3689a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3690a94100faSBill Paul  */
36916a087a87SPyun YongHyeon static int
36927b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3693a94100faSBill Paul {
3694a94100faSBill Paul 	struct rl_softc		*sc;
3695a94100faSBill Paul 
3696a94100faSBill Paul 	sc = device_get_softc(dev);
3697a94100faSBill Paul 
369897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3699a94100faSBill Paul 	re_stop(sc);
3700536fde34SMaxim Sobolev 	/*
3701536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3702536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
370372293673SRuslan Ermilov 	 * cases.
3704536fde34SMaxim Sobolev 	 */
3705536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
37067467bd53SPyun YongHyeon 	re_setwol(sc);
370797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
37086a087a87SPyun YongHyeon 
37096a087a87SPyun YongHyeon 	return (0);
3710a94100faSBill Paul }
37117467bd53SPyun YongHyeon 
37127467bd53SPyun YongHyeon static void
37136830588dSPyun YongHyeon re_set_linkspeed(struct rl_softc *sc)
37146830588dSPyun YongHyeon {
37156830588dSPyun YongHyeon 	struct mii_softc *miisc;
37166830588dSPyun YongHyeon 	struct mii_data *mii;
37176830588dSPyun YongHyeon 	int aneg, i, phyno;
37186830588dSPyun YongHyeon 
37196830588dSPyun YongHyeon 	RL_LOCK_ASSERT(sc);
37206830588dSPyun YongHyeon 
37216830588dSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
37226830588dSPyun YongHyeon 	mii_pollstat(mii);
37236830588dSPyun YongHyeon 	aneg = 0;
37246830588dSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
37256830588dSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
37266830588dSPyun YongHyeon 		switch IFM_SUBTYPE(mii->mii_media_active) {
37276830588dSPyun YongHyeon 		case IFM_10_T:
37286830588dSPyun YongHyeon 		case IFM_100_TX:
37296830588dSPyun YongHyeon 			return;
37306830588dSPyun YongHyeon 		case IFM_1000_T:
37316830588dSPyun YongHyeon 			aneg++;
37326830588dSPyun YongHyeon 			break;
37336830588dSPyun YongHyeon 		default:
37346830588dSPyun YongHyeon 			break;
37356830588dSPyun YongHyeon 		}
37366830588dSPyun YongHyeon 	}
37376830588dSPyun YongHyeon 	miisc = LIST_FIRST(&mii->mii_phys);
37386830588dSPyun YongHyeon 	phyno = miisc->mii_phy;
37396830588dSPyun YongHyeon 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
37406830588dSPyun YongHyeon 		PHY_RESET(miisc);
37416830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno, MII_100T2CR, 0);
37426830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno,
37436830588dSPyun YongHyeon 	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
37446830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno,
37456830588dSPyun YongHyeon 	    MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
37466830588dSPyun YongHyeon 	DELAY(1000);
37476830588dSPyun YongHyeon 	if (aneg != 0) {
37486830588dSPyun YongHyeon 		/*
37496830588dSPyun YongHyeon 		 * Poll link state until re(4) get a 10/100Mbps link.
37506830588dSPyun YongHyeon 		 */
37516830588dSPyun YongHyeon 		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
37526830588dSPyun YongHyeon 			mii_pollstat(mii);
37536830588dSPyun YongHyeon 			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
37546830588dSPyun YongHyeon 			    == (IFM_ACTIVE | IFM_AVALID)) {
37556830588dSPyun YongHyeon 				switch (IFM_SUBTYPE(mii->mii_media_active)) {
37566830588dSPyun YongHyeon 				case IFM_10_T:
37576830588dSPyun YongHyeon 				case IFM_100_TX:
37586830588dSPyun YongHyeon 					return;
37596830588dSPyun YongHyeon 				default:
37606830588dSPyun YongHyeon 					break;
37616830588dSPyun YongHyeon 				}
37626830588dSPyun YongHyeon 			}
37636830588dSPyun YongHyeon 			RL_UNLOCK(sc);
37646830588dSPyun YongHyeon 			pause("relnk", hz);
37656830588dSPyun YongHyeon 			RL_LOCK(sc);
37666830588dSPyun YongHyeon 		}
37676830588dSPyun YongHyeon 		if (i == MII_ANEGTICKS_GIGE)
37686830588dSPyun YongHyeon 			device_printf(sc->rl_dev,
37696830588dSPyun YongHyeon 			    "establishing a link failed, WOL may not work!");
37706830588dSPyun YongHyeon 	}
37716830588dSPyun YongHyeon 	/*
37726830588dSPyun YongHyeon 	 * No link, force MAC to have 100Mbps, full-duplex link.
37736830588dSPyun YongHyeon 	 * MAC does not require reprogramming on resolved speed/duplex,
37746830588dSPyun YongHyeon 	 * so this is just for completeness.
37756830588dSPyun YongHyeon 	 */
37766830588dSPyun YongHyeon 	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
37776830588dSPyun YongHyeon 	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
37786830588dSPyun YongHyeon }
37796830588dSPyun YongHyeon 
37806830588dSPyun YongHyeon static void
37817b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
37827467bd53SPyun YongHyeon {
37837467bd53SPyun YongHyeon 	struct ifnet		*ifp;
37847467bd53SPyun YongHyeon 	int			pmc;
37857467bd53SPyun YongHyeon 	uint16_t		pmstat;
37867467bd53SPyun YongHyeon 	uint8_t			v;
37877467bd53SPyun YongHyeon 
37887467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
37897467bd53SPyun YongHyeon 
37903b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
37917467bd53SPyun YongHyeon 		return;
37927467bd53SPyun YongHyeon 
37937467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
379461f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
379561f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
379661f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
379761f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
379861f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
379961f45a72SPyun YongHyeon 	}
3800fcb220acSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0) {
3801fcb220acSPyun YongHyeon 		re_set_rxmode(sc);
38026830588dSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0)
38036830588dSPyun YongHyeon 			re_set_linkspeed(sc);
3804fcb220acSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3805886ff602SPyun YongHyeon 			CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
3806fcb220acSPyun YongHyeon 	}
38077467bd53SPyun YongHyeon 	/* Enable config register write. */
38087467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
38097467bd53SPyun YongHyeon 
38107467bd53SPyun YongHyeon 	/* Enable PME. */
3811e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg1);
38127467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
38137467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
38147467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
3815e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, v);
38167467bd53SPyun YongHyeon 
3817e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg3);
38187467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
38197467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
38207467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
3821e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
38227467bd53SPyun YongHyeon 
3823e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg5);
382444f7cbf5SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST |
382544f7cbf5SPyun YongHyeon 	    RL_CFG5_WOL_LANWAKE);
38267467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
38277467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
38287467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
38297467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
38307467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
38317467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
3832e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
38337467bd53SPyun YongHyeon 
383444f7cbf5SPyun YongHyeon 	/* Config register write done. */
383544f7cbf5SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
383644f7cbf5SPyun YongHyeon 
3837bc6b129bSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) == 0 &&
3838d0c45156SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3839d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
38407467bd53SPyun YongHyeon 	/*
38417467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
38427467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
38437467bd53SPyun YongHyeon 	 * needed.
38447467bd53SPyun YongHyeon 	 */
38457467bd53SPyun YongHyeon 
38467467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
38477467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
38487467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
38497467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
38507467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
38517467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
38527467bd53SPyun YongHyeon }
38537467bd53SPyun YongHyeon 
38547467bd53SPyun YongHyeon static void
38557b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
38567467bd53SPyun YongHyeon {
38577467bd53SPyun YongHyeon 	int			pmc;
38587467bd53SPyun YongHyeon 	uint8_t			v;
38597467bd53SPyun YongHyeon 
38607467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
38617467bd53SPyun YongHyeon 
38623b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
38637467bd53SPyun YongHyeon 		return;
38647467bd53SPyun YongHyeon 
38657467bd53SPyun YongHyeon 	/* Enable config register write. */
38667467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
38677467bd53SPyun YongHyeon 
3868e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg3);
38697467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
3870e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
38717467bd53SPyun YongHyeon 
38727467bd53SPyun YongHyeon 	/* Config register write done. */
3873f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
38747467bd53SPyun YongHyeon 
3875e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg5);
38767467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
38777467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
3878e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
38797467bd53SPyun YongHyeon }
38800534aae0SPyun YongHyeon 
38810534aae0SPyun YongHyeon static void
38820534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc)
38830534aae0SPyun YongHyeon {
38840534aae0SPyun YongHyeon 	struct sysctl_ctx_list	*ctx;
38850534aae0SPyun YongHyeon 	struct sysctl_oid_list	*children;
3886502be0f7SPyun YongHyeon 	int			error;
38870534aae0SPyun YongHyeon 
38880534aae0SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->rl_dev);
38890534aae0SPyun YongHyeon 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
38900534aae0SPyun YongHyeon 
38910534aae0SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
38920534aae0SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I",
38930534aae0SPyun YongHyeon 	    "Statistics Information");
3894502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
3895502be0f7SPyun YongHyeon 		return;
3896502be0f7SPyun YongHyeon 
3897502be0f7SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod",
3898502be0f7SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, &sc->rl_int_rx_mod, 0,
3899502be0f7SPyun YongHyeon 	    sysctl_hw_re_int_mod, "I", "re RX interrupt moderation");
3900502be0f7SPyun YongHyeon 	/* Pull in device tunables. */
3901502be0f7SPyun YongHyeon 	sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3902502be0f7SPyun YongHyeon 	error = resource_int_value(device_get_name(sc->rl_dev),
3903502be0f7SPyun YongHyeon 	    device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod);
3904502be0f7SPyun YongHyeon 	if (error == 0) {
3905502be0f7SPyun YongHyeon 		if (sc->rl_int_rx_mod < RL_TIMER_MIN ||
3906502be0f7SPyun YongHyeon 		    sc->rl_int_rx_mod > RL_TIMER_MAX) {
3907502be0f7SPyun YongHyeon 			device_printf(sc->rl_dev, "int_rx_mod value out of "
3908502be0f7SPyun YongHyeon 			    "range; using default: %d\n",
3909502be0f7SPyun YongHyeon 			    RL_TIMER_DEFAULT);
3910502be0f7SPyun YongHyeon 			sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3911502be0f7SPyun YongHyeon 		}
3912502be0f7SPyun YongHyeon 	}
3913502be0f7SPyun YongHyeon 
39140534aae0SPyun YongHyeon }
39150534aae0SPyun YongHyeon 
39160534aae0SPyun YongHyeon static int
39170534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS)
39180534aae0SPyun YongHyeon {
39190534aae0SPyun YongHyeon 	struct rl_softc		*sc;
39200534aae0SPyun YongHyeon 	struct rl_stats		*stats;
39210534aae0SPyun YongHyeon 	int			error, i, result;
39220534aae0SPyun YongHyeon 
39230534aae0SPyun YongHyeon 	result = -1;
39240534aae0SPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
39250534aae0SPyun YongHyeon 	if (error || req->newptr == NULL)
39260534aae0SPyun YongHyeon 		return (error);
39270534aae0SPyun YongHyeon 
39280534aae0SPyun YongHyeon 	if (result == 1) {
39290534aae0SPyun YongHyeon 		sc = (struct rl_softc *)arg1;
39300534aae0SPyun YongHyeon 		RL_LOCK(sc);
393116a4824bSPyun YongHyeon 		if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
393216a4824bSPyun YongHyeon 			RL_UNLOCK(sc);
393316a4824bSPyun YongHyeon 			goto done;
393416a4824bSPyun YongHyeon 		}
39350534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
39360534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
39370534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
39380534aae0SPyun YongHyeon 		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
39390534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
39400534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
39410534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
39420534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
39430534aae0SPyun YongHyeon 		    RL_DUMPSTATS_START));
39440534aae0SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
39450534aae0SPyun YongHyeon 			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
39460534aae0SPyun YongHyeon 			    RL_DUMPSTATS_START) == 0)
39470534aae0SPyun YongHyeon 				break;
39480534aae0SPyun YongHyeon 			DELAY(1000);
39490534aae0SPyun YongHyeon 		}
39500534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
39510534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
39520534aae0SPyun YongHyeon 		RL_UNLOCK(sc);
39530534aae0SPyun YongHyeon 		if (i == 0) {
39540534aae0SPyun YongHyeon 			device_printf(sc->rl_dev,
39550534aae0SPyun YongHyeon 			    "DUMP statistics request timedout\n");
39560534aae0SPyun YongHyeon 			return (ETIMEDOUT);
39570534aae0SPyun YongHyeon 		}
395816a4824bSPyun YongHyeon done:
39590534aae0SPyun YongHyeon 		stats = sc->rl_ldata.rl_stats;
39600534aae0SPyun YongHyeon 		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
39610534aae0SPyun YongHyeon 		printf("Tx frames : %ju\n",
39620534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_pkts));
39630534aae0SPyun YongHyeon 		printf("Rx frames : %ju\n",
39640534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_pkts));
39650534aae0SPyun YongHyeon 		printf("Tx errors : %ju\n",
39660534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_errs));
39670534aae0SPyun YongHyeon 		printf("Rx errors : %u\n",
39680534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_errs));
39690534aae0SPyun YongHyeon 		printf("Rx missed frames : %u\n",
39700534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_missed_pkts));
39710534aae0SPyun YongHyeon 		printf("Rx frame alignment errs : %u\n",
39720534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
39730534aae0SPyun YongHyeon 		printf("Tx single collisions : %u\n",
39740534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_onecoll));
39750534aae0SPyun YongHyeon 		printf("Tx multiple collisions : %u\n",
39760534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_multicolls));
39770534aae0SPyun YongHyeon 		printf("Rx unicast frames : %ju\n",
39780534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
39790534aae0SPyun YongHyeon 		printf("Rx broadcast frames : %ju\n",
39800534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
39810534aae0SPyun YongHyeon 		printf("Rx multicast frames : %u\n",
39820534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_mcasts));
39830534aae0SPyun YongHyeon 		printf("Tx aborts : %u\n",
39840534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_tx_aborts));
39850534aae0SPyun YongHyeon 		printf("Tx underruns : %u\n",
39860534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_underruns));
39870534aae0SPyun YongHyeon 	}
39880534aae0SPyun YongHyeon 
39890534aae0SPyun YongHyeon 	return (error);
39900534aae0SPyun YongHyeon }
3991502be0f7SPyun YongHyeon 
3992502be0f7SPyun YongHyeon static int
3993502be0f7SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
3994502be0f7SPyun YongHyeon {
3995502be0f7SPyun YongHyeon 	int error, value;
3996502be0f7SPyun YongHyeon 
3997502be0f7SPyun YongHyeon 	if (arg1 == NULL)
3998502be0f7SPyun YongHyeon 		return (EINVAL);
3999502be0f7SPyun YongHyeon 	value = *(int *)arg1;
4000502be0f7SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
4001502be0f7SPyun YongHyeon 	if (error || req->newptr == NULL)
4002502be0f7SPyun YongHyeon 		return (error);
4003502be0f7SPyun YongHyeon 	if (value < low || value > high)
4004502be0f7SPyun YongHyeon 		return (EINVAL);
4005502be0f7SPyun YongHyeon 	*(int *)arg1 = value;
4006502be0f7SPyun YongHyeon 
4007502be0f7SPyun YongHyeon 	return (0);
4008502be0f7SPyun YongHyeon }
4009502be0f7SPyun YongHyeon 
4010502be0f7SPyun YongHyeon static int
4011502be0f7SPyun YongHyeon sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)
4012502be0f7SPyun YongHyeon {
4013502be0f7SPyun YongHyeon 
4014502be0f7SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN,
4015502be0f7SPyun YongHyeon 	    RL_TIMER_MAX));
4016502be0f7SPyun YongHyeon }
4017