xref: /freebsd/sys/dev/re/if_re.c (revision ce3ee1e7c4cac5b86bbc15daac68f2129aa42187)
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>
13076039bc8SGleb Smirnoff #include <net/if_var.h>
131a94100faSBill Paul #include <net/if_arp.h>
132a94100faSBill Paul #include <net/ethernet.h>
133a94100faSBill Paul #include <net/if_dl.h>
134a94100faSBill Paul #include <net/if_media.h>
135fc74a9f9SBrooks Davis #include <net/if_types.h>
136a94100faSBill Paul #include <net/if_vlan_var.h>
137a94100faSBill Paul 
138a94100faSBill Paul #include <net/bpf.h>
139a94100faSBill Paul 
140a94100faSBill Paul #include <machine/bus.h>
141a94100faSBill Paul #include <machine/resource.h>
142a94100faSBill Paul #include <sys/bus.h>
143a94100faSBill Paul #include <sys/rman.h>
144a94100faSBill Paul 
145a94100faSBill Paul #include <dev/mii/mii.h>
146a94100faSBill Paul #include <dev/mii/miivar.h>
147a94100faSBill Paul 
148a94100faSBill Paul #include <dev/pci/pcireg.h>
149a94100faSBill Paul #include <dev/pci/pcivar.h>
150a94100faSBill Paul 
151d65abd66SPyun YongHyeon #include <pci/if_rlreg.h>
152d65abd66SPyun YongHyeon 
153a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
154a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
155a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
156a94100faSBill Paul 
157298bfdf3SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
158a94100faSBill Paul #include "miibus_if.h"
159a94100faSBill Paul 
1605774c5ffSPyun YongHyeon /* Tunables. */
161502be0f7SPyun YongHyeon static int intr_filter = 0;
162502be0f7SPyun YongHyeon TUNABLE_INT("hw.re.intr_filter", &intr_filter);
163c2d2e19cSPyun YongHyeon static int msi_disable = 0;
1645774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1654a58fd45SPyun YongHyeon static int msix_disable = 0;
1664a58fd45SPyun YongHyeon TUNABLE_INT("hw.re.msix_disable", &msix_disable);
1672c21710bSPyun YongHyeon static int prefer_iomap = 0;
1682c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
1695774c5ffSPyun YongHyeon 
170a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
171a94100faSBill Paul 
172a94100faSBill Paul /*
173a94100faSBill Paul  * Various supported device vendors/types and their names.
174a94100faSBill Paul  */
17529658c96SDimitry Andric static const struct rl_type re_devs[] = {
1769dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
17732aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
178caa19d50SPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_530T_REVC, 0,
179caa19d50SPyun YongHyeon 	    "D-Link DGE-530(T) Gigabit Ethernet Adapter" },
1809dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
181a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1829dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
18354899a96SPyun YongHyeon 	    "RealTek 810xE PCIe 10/100baseTX" },
1849dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
185ab9f923eSPyun YongHyeon 	    "RealTek 8168/8111 B/C/CP/D/DP/E/F/G PCIe Gigabit Ethernet" },
1869dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
187715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1889dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1892ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1909dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
191ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1929dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
19326390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1949dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
195dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
196a94100faSBill Paul };
197a94100faSBill Paul 
19829658c96SDimitry Andric static const struct rl_hwrev re_hwrevs[] = {
19981eee0ebSPyun YongHyeon 	{ RL_HWREV_8139, RL_8139, "", RL_MTU },
20081eee0ebSPyun YongHyeon 	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
20181eee0ebSPyun YongHyeon 	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
20281eee0ebSPyun YongHyeon 	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
20381eee0ebSPyun YongHyeon 	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
20481eee0ebSPyun YongHyeon 	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
20581eee0ebSPyun YongHyeon 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
20681eee0ebSPyun YongHyeon 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
207ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
20881eee0ebSPyun YongHyeon 	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
20981eee0ebSPyun YongHyeon 	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
21081eee0ebSPyun YongHyeon 	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
21181eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
21281eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21381eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
21481eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21581eee0ebSPyun YongHyeon 	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
21681eee0ebSPyun YongHyeon 	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
21781eee0ebSPyun YongHyeon 	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
21881eee0ebSPyun YongHyeon 	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
21981eee0ebSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
22081eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
22181eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
22281eee0ebSPyun YongHyeon 	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
22339e69201SPyun YongHyeon 	{ RL_HWREV_8401E, RL_8169, "8401E", RL_MTU },
224a9e3362aSPyun YongHyeon 	{ RL_HWREV_8402, RL_8169, "8402", RL_MTU },
22554899a96SPyun YongHyeon 	{ RL_HWREV_8105E, RL_8169, "8105E", RL_MTU },
2266b0a8e04SPyun YongHyeon 	{ RL_HWREV_8105E_SPIN1, RL_8169, "8105E", RL_MTU },
227214c71f6SPyun YongHyeon 	{ RL_HWREV_8106E, RL_8169, "8106E", RL_MTU },
228ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
229ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
23081eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
23181eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
23281eee0ebSPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
23381eee0ebSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
23481eee0ebSPyun YongHyeon 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
23581eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
23681eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
237c3767eabSPyun YongHyeon 	{ RL_HWREV_8168EP, RL_8169, "8168EP/8111EP", RL_JUMBO_MTU_9K},
238d467ffaaSPyun YongHyeon 	{ RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K},
239ab9f923eSPyun YongHyeon 	{ RL_HWREV_8168G, RL_8169, "8168G/8111G", RL_JUMBO_MTU_9K},
240ab9f923eSPyun YongHyeon 	{ RL_HWREV_8168GU, RL_8169, "8168GU/8111GU", RL_JUMBO_MTU_9K},
241d56f7f52SPyun YongHyeon 	{ RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K},
242ab9f923eSPyun YongHyeon 	{ RL_HWREV_8411B, RL_8169, "8411B", RL_JUMBO_MTU_9K},
24381eee0ebSPyun YongHyeon 	{ 0, 0, NULL, 0 }
244a94100faSBill Paul };
245a94100faSBill Paul 
246a94100faSBill Paul static int re_probe		(device_t);
247a94100faSBill Paul static int re_attach		(device_t);
248a94100faSBill Paul static int re_detach		(device_t);
249a94100faSBill Paul 
250d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
251a94100faSBill Paul 
252a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
253a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
254d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
255d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
256d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
25781eee0ebSPyun YongHyeon static int re_jumbo_newbuf	(struct rl_softc *, int);
258a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
25981eee0ebSPyun YongHyeon static int re_jrx_list_init	(struct rl_softc *);
260a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
26122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
26222a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
26322a11c96SJohn-Mark Gurney 				(struct mbuf *);
26422a11c96SJohn-Mark Gurney #endif
2651abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
266a94100faSBill Paul static void re_txeof		(struct rl_softc *);
26797b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2681abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2691abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
27097b9d4baSJohn-Mark Gurney #endif
271ef544f63SPaolo Pisati static int re_intr		(void *);
272502be0f7SPyun YongHyeon static void re_intr_msi		(void *);
273a94100faSBill Paul static void re_tick		(void *);
274ed510fb0SBill Paul static void re_int_task		(void *, int);
275a94100faSBill Paul static void re_start		(struct ifnet *);
276d180a66fSPyun YongHyeon static void re_start_locked	(struct ifnet *);
277a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
278a94100faSBill Paul static void re_init		(void *);
27997b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
280a94100faSBill Paul static void re_stop		(struct rl_softc *);
2811d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
282a94100faSBill Paul static int re_suspend		(device_t);
283a94100faSBill Paul static int re_resume		(device_t);
2846a087a87SPyun YongHyeon static int re_shutdown		(device_t);
285a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
286a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
287a94100faSBill Paul 
288a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
289a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
290ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
291a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
292a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
293a94100faSBill Paul 
294a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
295a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
296a94100faSBill Paul static void re_miibus_statchg	(device_t);
297a94100faSBill Paul 
29881eee0ebSPyun YongHyeon static void re_set_jumbo	(struct rl_softc *, int);
299ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
300a94100faSBill Paul static void re_reset		(struct rl_softc *);
3017467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
3027467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
3036830588dSPyun YongHyeon static void re_set_linkspeed	(struct rl_softc *);
304a94100faSBill Paul 
305579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP	/* see ixgbe.c for details */
306579a6e3cSLuigi Rizzo #include <dev/netmap/if_re_netmap.h>
307579a6e3cSLuigi Rizzo #endif /* !DEV_NETMAP */
308579a6e3cSLuigi Rizzo 
309ed510fb0SBill Paul #ifdef RE_DIAG
310a94100faSBill Paul static int re_diag		(struct rl_softc *);
311ed510fb0SBill Paul #endif
312a94100faSBill Paul 
3130534aae0SPyun YongHyeon static void re_add_sysctls	(struct rl_softc *);
3140534aae0SPyun YongHyeon static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
315502be0f7SPyun YongHyeon static int sysctl_int_range	(SYSCTL_HANDLER_ARGS, int, int);
316502be0f7SPyun YongHyeon static int sysctl_hw_re_int_mod	(SYSCTL_HANDLER_ARGS);
3170534aae0SPyun YongHyeon 
318a94100faSBill Paul static device_method_t re_methods[] = {
319a94100faSBill Paul 	/* Device interface */
320a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
321a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
322a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
323a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
324a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
325a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
326a94100faSBill Paul 
327a94100faSBill Paul 	/* MII interface */
328a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
329a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
330a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
331a94100faSBill Paul 
3324b7ec270SMarius Strobl 	DEVMETHOD_END
333a94100faSBill Paul };
334a94100faSBill Paul 
335a94100faSBill Paul static driver_t re_driver = {
336a94100faSBill Paul 	"re",
337a94100faSBill Paul 	re_methods,
338a94100faSBill Paul 	sizeof(struct rl_softc)
339a94100faSBill Paul };
340a94100faSBill Paul 
341a94100faSBill Paul static devclass_t re_devclass;
342a94100faSBill Paul 
343a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
344a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
345a94100faSBill Paul 
346a94100faSBill Paul #define EE_SET(x)					\
347a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
348a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
349a94100faSBill Paul 
350a94100faSBill Paul #define EE_CLR(x)					\
351a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
352a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
353a94100faSBill Paul 
354a94100faSBill Paul /*
355a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
356a94100faSBill Paul  */
357a94100faSBill Paul static void
3587b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
359a94100faSBill Paul {
3600ce0868aSPyun YongHyeon 	int			d, i;
361a94100faSBill Paul 
362ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
363a94100faSBill Paul 
364a94100faSBill Paul 	/*
365a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
366a94100faSBill Paul 	 */
367ed510fb0SBill Paul 
368ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
369a94100faSBill Paul 		if (d & i) {
370a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
371a94100faSBill Paul 		} else {
372a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
373a94100faSBill Paul 		}
374a94100faSBill Paul 		DELAY(100);
375a94100faSBill Paul 		EE_SET(RL_EE_CLK);
376a94100faSBill Paul 		DELAY(150);
377a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
378a94100faSBill Paul 		DELAY(100);
379a94100faSBill Paul 	}
380a94100faSBill Paul }
381a94100faSBill Paul 
382a94100faSBill Paul /*
383a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
384a94100faSBill Paul  */
385a94100faSBill Paul static void
3867b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
387a94100faSBill Paul {
3880ce0868aSPyun YongHyeon 	int			i;
389a94100faSBill Paul 	u_int16_t		word = 0;
390a94100faSBill Paul 
391a94100faSBill Paul 	/*
392a94100faSBill Paul 	 * Send address of word we want to read.
393a94100faSBill Paul 	 */
394a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
395a94100faSBill Paul 
396a94100faSBill Paul 	/*
397a94100faSBill Paul 	 * Start reading bits from EEPROM.
398a94100faSBill Paul 	 */
399a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
400a94100faSBill Paul 		EE_SET(RL_EE_CLK);
401a94100faSBill Paul 		DELAY(100);
402a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
403a94100faSBill Paul 			word |= i;
404a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
405a94100faSBill Paul 		DELAY(100);
406a94100faSBill Paul 	}
407a94100faSBill Paul 
408a94100faSBill Paul 	*dest = word;
409a94100faSBill Paul }
410a94100faSBill Paul 
411a94100faSBill Paul /*
412a94100faSBill Paul  * Read a sequence of words from the EEPROM.
413a94100faSBill Paul  */
414a94100faSBill Paul static void
4157b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
416a94100faSBill Paul {
417a94100faSBill Paul 	int			i;
418a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
419a94100faSBill Paul 
420ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
421ed510fb0SBill Paul 
422ed510fb0SBill Paul         DELAY(100);
423ed510fb0SBill Paul 
424a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
425ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
426a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
427ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
428a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
429be099007SPyun YongHyeon                 *ptr = word;
430a94100faSBill Paul 	}
431ed510fb0SBill Paul 
432ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
433a94100faSBill Paul }
434a94100faSBill Paul 
435a94100faSBill Paul static int
4367b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
437a94100faSBill Paul {
438a94100faSBill Paul 	struct rl_softc		*sc;
439a94100faSBill Paul 	u_int32_t		rval;
440a94100faSBill Paul 	int			i;
441a94100faSBill Paul 
442a94100faSBill Paul 	sc = device_get_softc(dev);
443a94100faSBill Paul 
4449bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4459bac70b8SBill Paul 
4469bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4479bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4489bac70b8SBill Paul 		return (rval);
4499bac70b8SBill Paul 	}
4509bac70b8SBill Paul 
451a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
452a94100faSBill Paul 
45396b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
454a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
455a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
456a94100faSBill Paul 			break;
4572bc085c6SPyun YongHyeon 		DELAY(25);
458a94100faSBill Paul 	}
459a94100faSBill Paul 
46096b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4616b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
462a94100faSBill Paul 		return (0);
463a94100faSBill Paul 	}
464a94100faSBill Paul 
4652bc085c6SPyun YongHyeon 	/*
4662bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4672bc085c6SPyun YongHyeon 	 */
4682bc085c6SPyun YongHyeon 	DELAY(20);
4692bc085c6SPyun YongHyeon 
470a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
471a94100faSBill Paul }
472a94100faSBill Paul 
473a94100faSBill Paul static int
4747b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
475a94100faSBill Paul {
476a94100faSBill Paul 	struct rl_softc		*sc;
477a94100faSBill Paul 	u_int32_t		rval;
478a94100faSBill Paul 	int			i;
479a94100faSBill Paul 
480a94100faSBill Paul 	sc = device_get_softc(dev);
481a94100faSBill Paul 
482a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4839bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
484a94100faSBill Paul 
48596b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
486a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
487a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
488a94100faSBill Paul 			break;
4892bc085c6SPyun YongHyeon 		DELAY(25);
490a94100faSBill Paul 	}
491a94100faSBill Paul 
49296b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4936b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
494a94100faSBill Paul 		return (0);
495a94100faSBill Paul 	}
496a94100faSBill Paul 
4972bc085c6SPyun YongHyeon 	/*
4982bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4992bc085c6SPyun YongHyeon 	 */
5002bc085c6SPyun YongHyeon 	DELAY(20);
5012bc085c6SPyun YongHyeon 
502a94100faSBill Paul 	return (0);
503a94100faSBill Paul }
504a94100faSBill Paul 
505a94100faSBill Paul static int
5067b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
507a94100faSBill Paul {
508a94100faSBill Paul 	struct rl_softc		*sc;
509a94100faSBill Paul 	u_int16_t		rval = 0;
510a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
511a94100faSBill Paul 
512a94100faSBill Paul 	sc = device_get_softc(dev);
513a94100faSBill Paul 
514a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
515a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
516a94100faSBill Paul 		return (rval);
517a94100faSBill Paul 	}
518a94100faSBill Paul 
519a94100faSBill Paul 	switch (reg) {
520a94100faSBill Paul 	case MII_BMCR:
521a94100faSBill Paul 		re8139_reg = RL_BMCR;
522a94100faSBill Paul 		break;
523a94100faSBill Paul 	case MII_BMSR:
524a94100faSBill Paul 		re8139_reg = RL_BMSR;
525a94100faSBill Paul 		break;
526a94100faSBill Paul 	case MII_ANAR:
527a94100faSBill Paul 		re8139_reg = RL_ANAR;
528a94100faSBill Paul 		break;
529a94100faSBill Paul 	case MII_ANER:
530a94100faSBill Paul 		re8139_reg = RL_ANER;
531a94100faSBill Paul 		break;
532a94100faSBill Paul 	case MII_ANLPAR:
533a94100faSBill Paul 		re8139_reg = RL_LPAR;
534a94100faSBill Paul 		break;
535a94100faSBill Paul 	case MII_PHYIDR1:
536a94100faSBill Paul 	case MII_PHYIDR2:
537a94100faSBill Paul 		return (0);
538a94100faSBill Paul 	/*
539a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
540a94100faSBill Paul 	 * register. If we have a link partner which does not
541a94100faSBill Paul 	 * support NWAY, this is the register which will tell
542a94100faSBill Paul 	 * us the results of parallel detection.
543a94100faSBill Paul 	 */
544a94100faSBill Paul 	case RL_MEDIASTAT:
545a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
546a94100faSBill Paul 		return (rval);
547a94100faSBill Paul 	default:
5486b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
549a94100faSBill Paul 		return (0);
550a94100faSBill Paul 	}
551a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
552baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
553baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
554baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
555baa12772SPyun YongHyeon 	}
556a94100faSBill Paul 	return (rval);
557a94100faSBill Paul }
558a94100faSBill Paul 
559a94100faSBill Paul static int
5607b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
561a94100faSBill Paul {
562a94100faSBill Paul 	struct rl_softc		*sc;
563a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
564a94100faSBill Paul 	int			rval = 0;
565a94100faSBill Paul 
566a94100faSBill Paul 	sc = device_get_softc(dev);
567a94100faSBill Paul 
568a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
569a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
570a94100faSBill Paul 		return (rval);
571a94100faSBill Paul 	}
572a94100faSBill Paul 
573a94100faSBill Paul 	switch (reg) {
574a94100faSBill Paul 	case MII_BMCR:
575a94100faSBill Paul 		re8139_reg = RL_BMCR;
576baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
577baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
578baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
579baa12772SPyun YongHyeon 		}
580a94100faSBill Paul 		break;
581a94100faSBill Paul 	case MII_BMSR:
582a94100faSBill Paul 		re8139_reg = RL_BMSR;
583a94100faSBill Paul 		break;
584a94100faSBill Paul 	case MII_ANAR:
585a94100faSBill Paul 		re8139_reg = RL_ANAR;
586a94100faSBill Paul 		break;
587a94100faSBill Paul 	case MII_ANER:
588a94100faSBill Paul 		re8139_reg = RL_ANER;
589a94100faSBill Paul 		break;
590a94100faSBill Paul 	case MII_ANLPAR:
591a94100faSBill Paul 		re8139_reg = RL_LPAR;
592a94100faSBill Paul 		break;
593a94100faSBill Paul 	case MII_PHYIDR1:
594a94100faSBill Paul 	case MII_PHYIDR2:
595a94100faSBill Paul 		return (0);
596a94100faSBill Paul 		break;
597a94100faSBill Paul 	default:
5986b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
599a94100faSBill Paul 		return (0);
600a94100faSBill Paul 	}
601a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
602a94100faSBill Paul 	return (0);
603a94100faSBill Paul }
604a94100faSBill Paul 
605a94100faSBill Paul static void
6067b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
607a94100faSBill Paul {
608130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
609130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
610130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
611a11e2f18SBruce M Simpson 
612130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
613130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
614130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
615130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
616130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
617130b6dfbSPyun YongHyeon 		return;
618130b6dfbSPyun YongHyeon 
619130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
620130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
621130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
622130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
623130b6dfbSPyun YongHyeon 		case IFM_10_T:
624130b6dfbSPyun YongHyeon 		case IFM_100_TX:
625130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
626130b6dfbSPyun YongHyeon 			break;
627130b6dfbSPyun YongHyeon 		case IFM_1000_T:
628130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
629130b6dfbSPyun YongHyeon 				break;
630130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
631130b6dfbSPyun YongHyeon 			break;
632130b6dfbSPyun YongHyeon 		default:
633130b6dfbSPyun YongHyeon 			break;
634130b6dfbSPyun YongHyeon 		}
635130b6dfbSPyun YongHyeon 	}
636130b6dfbSPyun YongHyeon 	/*
637130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
638130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
639130b6dfbSPyun YongHyeon 	 * parameters.
640130b6dfbSPyun YongHyeon 	 */
641a94100faSBill Paul }
642a94100faSBill Paul 
643a94100faSBill Paul /*
644ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
645a94100faSBill Paul  */
646a94100faSBill Paul static void
647ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
648a94100faSBill Paul {
649a94100faSBill Paul 	struct ifnet		*ifp;
650a94100faSBill Paul 	struct ifmultiaddr	*ifma;
651ff191365SJung-uk Kim 	uint32_t		hashes[2] = { 0, 0 };
652ff191365SJung-uk Kim 	uint32_t		h, rxfilt;
653a94100faSBill Paul 
65497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
65597b9d4baSJohn-Mark Gurney 
656fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
657a94100faSBill Paul 
658ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
659a94100faSBill Paul 
660ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6617c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6627c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
663a0637caaSPyun YongHyeon 		/*
664a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
665a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
666a0637caaSPyun YongHyeon 		 * promiscuous mode.
667a0637caaSPyun YongHyeon 		 */
668a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
669ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
670ff191365SJung-uk Kim 		goto done;
671a94100faSBill Paul 	}
672a94100faSBill Paul 
673eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
674a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
675a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
676a94100faSBill Paul 			continue;
6770e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6780e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
679a94100faSBill Paul 		if (h < 32)
680a94100faSBill Paul 			hashes[0] |= (1 << h);
681a94100faSBill Paul 		else
682a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
683a94100faSBill Paul 	}
684eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
685a94100faSBill Paul 
686ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
687bb7dfefbSBill Paul 		/*
688ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
689ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
690ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
691ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
692ff191365SJung-uk Kim 		 * devices.
693bb7dfefbSBill Paul 		 */
694aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
695ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
696ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
697ff191365SJung-uk Kim 			hashes[1] = h;
698ff191365SJung-uk Kim 		}
699ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
700ff191365SJung-uk Kim 	}
701ff191365SJung-uk Kim 
702ff191365SJung-uk Kim done:
703a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
704a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
705ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
706bb7dfefbSBill Paul }
707a94100faSBill Paul 
708a94100faSBill Paul static void
7097b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
710a94100faSBill Paul {
7110ce0868aSPyun YongHyeon 	int			i;
712a94100faSBill Paul 
71397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
71497b9d4baSJohn-Mark Gurney 
715a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
716a94100faSBill Paul 
717a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
718a94100faSBill Paul 		DELAY(10);
719a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
720a94100faSBill Paul 			break;
721a94100faSBill Paul 	}
722a94100faSBill Paul 	if (i == RL_TIMEOUT)
7236b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
724a94100faSBill Paul 
725566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
726a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
72781eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
728566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
729a94100faSBill Paul }
730a94100faSBill Paul 
731ed510fb0SBill Paul #ifdef RE_DIAG
732ed510fb0SBill Paul 
733a94100faSBill Paul /*
734a94100faSBill Paul  * The following routine is designed to test for a defect on some
735a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
736a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
737a94100faSBill Paul  * should be pulled high. The result of this defect is that the
738a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
739a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
740a94100faSBill Paul  * because the 64-bit data lines aren't connected.
741a94100faSBill Paul  *
742a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
743a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
744a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
745a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
746a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
747a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
748a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
749a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
750a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
751a94100faSBill Paul  */
752a94100faSBill Paul 
753a94100faSBill Paul static int
7547b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
755a94100faSBill Paul {
756fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
757a94100faSBill Paul 	struct mbuf		*m0;
758a94100faSBill Paul 	struct ether_header	*eh;
759a94100faSBill Paul 	struct rl_desc		*cur_rx;
760a94100faSBill Paul 	u_int16_t		status;
761a94100faSBill Paul 	u_int32_t		rxstat;
762ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
763a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
764a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
765a94100faSBill Paul 
766a94100faSBill Paul 	/* Allocate a single mbuf */
767c6499eccSGleb Smirnoff 	MGETHDR(m0, M_NOWAIT, MT_DATA);
768a94100faSBill Paul 	if (m0 == NULL)
769a94100faSBill Paul 		return (ENOBUFS);
770a94100faSBill Paul 
77197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
77297b9d4baSJohn-Mark Gurney 
773a94100faSBill Paul 	/*
774a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
775a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
776a94100faSBill Paul 	 * following special functions:
777a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
778a94100faSBill Paul 	 * - Enables digital loopback mode
779a94100faSBill Paul 	 * - Leaves interrupts turned off
780a94100faSBill Paul 	 */
781a94100faSBill Paul 
782a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
783a94100faSBill Paul 	sc->rl_testmode = 1;
7848476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
78597b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
786351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
787ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
788ed510fb0SBill Paul 		phyaddr = 1;
789ed510fb0SBill Paul 	else
790ed510fb0SBill Paul 		phyaddr = 0;
791ed510fb0SBill Paul 
792ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
793ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
794ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
795ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
796ed510fb0SBill Paul 			break;
797ed510fb0SBill Paul 	}
798ed510fb0SBill Paul 
799ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
800ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
801ed510fb0SBill Paul 
802804af9a1SBill Paul 	DELAY(100000);
803a94100faSBill Paul 
804a94100faSBill Paul 	/* Put some data in the mbuf */
805a94100faSBill Paul 
806a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
807a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
808a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
809a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
810a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
811a94100faSBill Paul 
8127cae6651SBill Paul 	/*
8137cae6651SBill Paul 	 * Queue the packet, start transmission.
8147cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
8157cae6651SBill Paul 	 */
816a94100faSBill Paul 
817abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
81897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
81952732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8207cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
82197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
822a94100faSBill Paul 	m0 = NULL;
823a94100faSBill Paul 
824a94100faSBill Paul 	/* Wait for it to propagate through the chip */
825a94100faSBill Paul 
826abc8ff44SBill Paul 	DELAY(100000);
827a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
828a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
829ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
830abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
831abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
832a94100faSBill Paul 			break;
833a94100faSBill Paul 		DELAY(10);
834a94100faSBill Paul 	}
835a94100faSBill Paul 
836a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8376b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8386b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8396b9f5c94SGleb Smirnoff 		    " loopback mode\n");
840a94100faSBill Paul 		error = EIO;
841a94100faSBill Paul 		goto done;
842a94100faSBill Paul 	}
843a94100faSBill Paul 
844a94100faSBill Paul 	/*
845a94100faSBill Paul 	 * The packet should have been dumped into the first
846a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
847a94100faSBill Paul 	 */
848a94100faSBill Paul 
849a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
850a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
851a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
852d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
853d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
854d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
855d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
856d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
857a94100faSBill Paul 
858d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
859d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
860a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
861a94100faSBill Paul 
862a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
863a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
864a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
865a94100faSBill Paul 
866a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8676b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8686b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
869a94100faSBill Paul 		error = EIO;
870a94100faSBill Paul 		goto done;
871a94100faSBill Paul 	}
872a94100faSBill Paul 
873a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
874a94100faSBill Paul 
875a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
876a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
877a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8786b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8796b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
880a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8816b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
882a94100faSBill Paul 		    eh->ether_dhost, ":", eh->ether_shost, ":",
883a94100faSBill Paul 		    ntohs(eh->ether_type));
8846b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8856b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8866b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8876b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8886b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8896b9f5c94SGleb Smirnoff 		    "details.\n");
890a94100faSBill Paul 		error = EIO;
891a94100faSBill Paul 	}
892a94100faSBill Paul 
893a94100faSBill Paul done:
894a94100faSBill Paul 	/* Turn interface off, release resources */
895a94100faSBill Paul 
896a94100faSBill Paul 	sc->rl_testmode = 0;
897351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
898a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
899a94100faSBill Paul 	re_stop(sc);
900a94100faSBill Paul 	if (m0 != NULL)
901a94100faSBill Paul 		m_freem(m0);
902a94100faSBill Paul 
90397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
90497b9d4baSJohn-Mark Gurney 
905a94100faSBill Paul 	return (error);
906a94100faSBill Paul }
907a94100faSBill Paul 
908ed510fb0SBill Paul #endif
909ed510fb0SBill Paul 
910a94100faSBill Paul /*
911a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
912a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
913a94100faSBill Paul  */
914a94100faSBill Paul static int
9157b5ffebfSPyun YongHyeon re_probe(device_t dev)
916a94100faSBill Paul {
917b3030306SMarius Strobl 	const struct rl_type	*t;
918dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
919dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
920dfdb409eSPyun YongHyeon 	int			i;
921a94100faSBill Paul 
922dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
923dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
924dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
925dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
926a94100faSBill Paul 
927dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
928dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
92926390635SJohn Baldwin 			/*
93026390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
931dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
93226390635SJohn Baldwin 			 */
933a94100faSBill Paul 			return (ENXIO);
934a94100faSBill Paul 		}
935dfdb409eSPyun YongHyeon 	}
936dfdb409eSPyun YongHyeon 
937dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
938dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
939dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
940dfdb409eSPyun YongHyeon 			return (ENXIO);
941dfdb409eSPyun YongHyeon 		}
942dfdb409eSPyun YongHyeon 	}
943dfdb409eSPyun YongHyeon 
944dfdb409eSPyun YongHyeon 	t = re_devs;
945dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
946dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
947a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
948d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
949a94100faSBill Paul 		}
950a94100faSBill Paul 	}
951a94100faSBill Paul 
952a94100faSBill Paul 	return (ENXIO);
953a94100faSBill Paul }
954a94100faSBill Paul 
955a94100faSBill Paul /*
956a94100faSBill Paul  * Map a single buffer address.
957a94100faSBill Paul  */
958a94100faSBill Paul 
959a94100faSBill Paul static void
9607b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
961a94100faSBill Paul {
9628fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
963a94100faSBill Paul 
964a94100faSBill Paul 	if (error)
965a94100faSBill Paul 		return;
966a94100faSBill Paul 
967a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
968a94100faSBill Paul 	addr = arg;
969a94100faSBill Paul 	*addr = segs->ds_addr;
970a94100faSBill Paul }
971a94100faSBill Paul 
972a94100faSBill Paul static int
9737b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
974a94100faSBill Paul {
97566366ca4SPyun YongHyeon 	bus_addr_t		lowaddr;
976d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
977a94100faSBill Paul 	int			error;
978a94100faSBill Paul 	int			i;
979a94100faSBill Paul 
980d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
981d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
982d65abd66SPyun YongHyeon 
983d65abd66SPyun YongHyeon 	/*
984d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
985ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
986ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
987ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
988ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
98966366ca4SPyun YongHyeon 	 * may not have the limitation.
990d65abd66SPyun YongHyeon 	 */
99166366ca4SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
99266366ca4SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
99366366ca4SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
994d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
99566366ca4SPyun YongHyeon 	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
996d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
997d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
998d65abd66SPyun YongHyeon 	if (error) {
999d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
1000d65abd66SPyun YongHyeon 		return (error);
1001d65abd66SPyun YongHyeon 	}
1002d65abd66SPyun YongHyeon 
1003d65abd66SPyun YongHyeon 	/*
1004d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
1005d65abd66SPyun YongHyeon 	 */
1006d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
1007d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1008d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
1009d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
1010d65abd66SPyun YongHyeon 	if (error) {
1011d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
1012d65abd66SPyun YongHyeon 		return (error);
1013d65abd66SPyun YongHyeon 	}
1014d65abd66SPyun YongHyeon 
1015a94100faSBill Paul 	/*
1016a94100faSBill Paul 	 * Allocate map for RX mbufs.
1017a94100faSBill Paul 	 */
1018d65abd66SPyun YongHyeon 
101981eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
102081eee0ebSPyun YongHyeon 		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
102181eee0ebSPyun YongHyeon 		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
102281eee0ebSPyun YongHyeon 		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
102381eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_mtag);
102481eee0ebSPyun YongHyeon 		if (error) {
102581eee0ebSPyun YongHyeon 			device_printf(dev,
102681eee0ebSPyun YongHyeon 			    "could not allocate jumbo RX DMA tag\n");
102781eee0ebSPyun YongHyeon 			return (error);
102881eee0ebSPyun YongHyeon 		}
102981eee0ebSPyun YongHyeon 	}
1030d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1031d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1032d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1033a94100faSBill Paul 	if (error) {
1034d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1035d65abd66SPyun YongHyeon 		return (error);
1036a94100faSBill Paul 	}
1037a94100faSBill Paul 
1038a94100faSBill Paul 	/*
1039a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1040a94100faSBill Paul 	 */
1041a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1042a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1043d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1044a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1045a94100faSBill Paul 	if (error) {
1046d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1047d65abd66SPyun YongHyeon 		return (error);
1048a94100faSBill Paul 	}
1049a94100faSBill Paul 
1050a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1051a94100faSBill Paul 
1052a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1053d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1054d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1055a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1056d65abd66SPyun YongHyeon 	if (error) {
1057d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1058d65abd66SPyun YongHyeon 		return (error);
1059d65abd66SPyun YongHyeon 	}
1060a94100faSBill Paul 
1061a94100faSBill Paul 	/* Load the map for the TX ring. */
1062a94100faSBill Paul 
1063d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1064a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1065a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1066d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1067a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1068d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1069d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1070d65abd66SPyun YongHyeon 		return (ENOMEM);
1071d65abd66SPyun YongHyeon 	}
1072a94100faSBill Paul 
1073a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1074a94100faSBill Paul 
1075d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1076d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1077d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1078a94100faSBill Paul 		if (error) {
1079d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1080d65abd66SPyun YongHyeon 			return (error);
1081a94100faSBill Paul 		}
1082a94100faSBill Paul 	}
1083a94100faSBill Paul 
1084a94100faSBill Paul 	/*
1085a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1086a94100faSBill Paul 	 */
1087a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1088a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1089d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1090a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1091a94100faSBill Paul 	if (error) {
1092d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1093d65abd66SPyun YongHyeon 		return (error);
1094a94100faSBill Paul 	}
1095a94100faSBill Paul 
1096a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1097a94100faSBill Paul 
1098a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1099d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1100d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1101a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1102d65abd66SPyun YongHyeon 	if (error) {
1103d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1104d65abd66SPyun YongHyeon 		return (error);
1105d65abd66SPyun YongHyeon 	}
1106a94100faSBill Paul 
1107a94100faSBill Paul 	/* Load the map for the RX ring. */
1108a94100faSBill Paul 
1109d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1110a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1111a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1112d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1113a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1114d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1115d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1116d65abd66SPyun YongHyeon 		return (ENOMEM);
1117d65abd66SPyun YongHyeon 	}
1118a94100faSBill Paul 
1119a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1120a94100faSBill Paul 
112181eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
112281eee0ebSPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
112381eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_sparemap);
112481eee0ebSPyun YongHyeon 		if (error) {
112581eee0ebSPyun YongHyeon 			device_printf(dev,
112681eee0ebSPyun YongHyeon 			    "could not create spare DMA map for jumbo RX\n");
112781eee0ebSPyun YongHyeon 			return (error);
112881eee0ebSPyun YongHyeon 		}
112981eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
113081eee0ebSPyun YongHyeon 			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
113181eee0ebSPyun YongHyeon 			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
113281eee0ebSPyun YongHyeon 			if (error) {
113381eee0ebSPyun YongHyeon 				device_printf(dev,
113481eee0ebSPyun YongHyeon 				    "could not create DMA map for jumbo RX\n");
113581eee0ebSPyun YongHyeon 				return (error);
113681eee0ebSPyun YongHyeon 			}
113781eee0ebSPyun YongHyeon 		}
113881eee0ebSPyun YongHyeon 	}
1139d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1140d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1141a94100faSBill Paul 	if (error) {
1142d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1143d65abd66SPyun YongHyeon 		return (error);
1144d65abd66SPyun YongHyeon 	}
1145d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1146d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1147d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1148d65abd66SPyun YongHyeon 		if (error) {
1149d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1150d65abd66SPyun YongHyeon 			return (error);
1151a94100faSBill Paul 		}
1152a94100faSBill Paul 	}
1153a94100faSBill Paul 
11540534aae0SPyun YongHyeon 	/* Create DMA map for statistics. */
11550534aae0SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
11560534aae0SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
11570534aae0SPyun YongHyeon 	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
11580534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_stag);
11590534aae0SPyun YongHyeon 	if (error) {
11600534aae0SPyun YongHyeon 		device_printf(dev, "could not create statistics DMA tag\n");
11610534aae0SPyun YongHyeon 		return (error);
11620534aae0SPyun YongHyeon 	}
11630534aae0SPyun YongHyeon 	/* Allocate DMA'able memory for statistics. */
11640534aae0SPyun YongHyeon 	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
11650534aae0SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_stats,
11660534aae0SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
11670534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_smap);
11680534aae0SPyun YongHyeon 	if (error) {
11690534aae0SPyun YongHyeon 		device_printf(dev,
11700534aae0SPyun YongHyeon 		    "could not allocate statistics DMA memory\n");
11710534aae0SPyun YongHyeon 		return (error);
11720534aae0SPyun YongHyeon 	}
11730534aae0SPyun YongHyeon 	/* Load the map for statistics. */
11740534aae0SPyun YongHyeon 	sc->rl_ldata.rl_stats_addr = 0;
11750534aae0SPyun YongHyeon 	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
11760534aae0SPyun YongHyeon 	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
11770534aae0SPyun YongHyeon 	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
11780534aae0SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
11790534aae0SPyun YongHyeon 		device_printf(dev, "could not load statistics DMA memory\n");
11800534aae0SPyun YongHyeon 		return (ENOMEM);
11810534aae0SPyun YongHyeon 	}
11820534aae0SPyun YongHyeon 
1183a94100faSBill Paul 	return (0);
1184a94100faSBill Paul }
1185a94100faSBill Paul 
1186a94100faSBill Paul /*
1187a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1188a94100faSBill Paul  * setup and ethernet/BPF attach.
1189a94100faSBill Paul  */
1190a94100faSBill Paul static int
11917b5ffebfSPyun YongHyeon re_attach(device_t dev)
1192a94100faSBill Paul {
1193a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1194be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1195a94100faSBill Paul 	struct rl_softc		*sc;
1196a94100faSBill Paul 	struct ifnet		*ifp;
1197b3030306SMarius Strobl 	const struct rl_hwrev	*hw_rev;
1198017f1c8dSPyun YongHyeon 	u_int32_t		cap, ctl;
1199a94100faSBill Paul 	int			hwrev;
1200ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
12018e5d93dbSMarius Strobl 	int			error = 0, i, phy, rid;
12024a58fd45SPyun YongHyeon 	int			msic, msixc, reg;
120303ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1204a94100faSBill Paul 
1205a94100faSBill Paul 	sc = device_get_softc(dev);
1206ed510fb0SBill Paul 	sc->rl_dev = dev;
1207a94100faSBill Paul 
1208a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
120997b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1210d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1211d1754a9bSJohn Baldwin 
1212a94100faSBill Paul 	/*
1213a94100faSBill Paul 	 * Map control/status registers.
1214a94100faSBill Paul 	 */
1215a94100faSBill Paul 	pci_enable_busmaster(dev);
1216a94100faSBill Paul 
1217ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
12182c21710bSPyun YongHyeon 	/*
12192c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
12202c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
12212c21710bSPyun YongHyeon 	 * is used always activate io mapping.
12222c21710bSPyun YongHyeon 	 */
12232c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
12242c21710bSPyun YongHyeon 		prefer_iomap = 1;
12252c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1226ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1227ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1228ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1229ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1230ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
12312c21710bSPyun YongHyeon 	} else {
12322c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
12332c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
12342c21710bSPyun YongHyeon 	}
1235ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1236ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
12372c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1238ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1239ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1240ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1241ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
12422c21710bSPyun YongHyeon 	}
1243ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1244d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1245a94100faSBill Paul 		error = ENXIO;
1246a94100faSBill Paul 		goto fail;
1247a94100faSBill Paul 	}
1248a94100faSBill Paul 
1249a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1250a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1251a94100faSBill Paul 
12525774c5ffSPyun YongHyeon 	msic = pci_msi_count(dev);
12534a58fd45SPyun YongHyeon 	msixc = pci_msix_count(dev);
1254017f1c8dSPyun YongHyeon 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
12554a58fd45SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
1256017f1c8dSPyun YongHyeon 		sc->rl_expcap = reg;
1257017f1c8dSPyun YongHyeon 	}
12584a58fd45SPyun YongHyeon 	if (bootverbose) {
12595774c5ffSPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
12604a58fd45SPyun YongHyeon 		device_printf(dev, "MSI-X count : %d\n", msixc);
12615774c5ffSPyun YongHyeon 	}
12624a58fd45SPyun YongHyeon 	if (msix_disable > 0)
12634a58fd45SPyun YongHyeon 		msixc = 0;
12644a58fd45SPyun YongHyeon 	if (msi_disable > 0)
12654a58fd45SPyun YongHyeon 		msic = 0;
12664a58fd45SPyun YongHyeon 	/* Prefer MSI-X to MSI. */
12674a58fd45SPyun YongHyeon 	if (msixc > 0) {
12684a58fd45SPyun YongHyeon 		msixc = 1;
12694a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
12704a58fd45SPyun YongHyeon 		sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
12714a58fd45SPyun YongHyeon 		    &rid, RF_ACTIVE);
12724a58fd45SPyun YongHyeon 		if (sc->rl_res_pba == NULL) {
12734a58fd45SPyun YongHyeon 			device_printf(sc->rl_dev,
12744a58fd45SPyun YongHyeon 			    "could not allocate MSI-X PBA resource\n");
12754a58fd45SPyun YongHyeon 		}
12764a58fd45SPyun YongHyeon 		if (sc->rl_res_pba != NULL &&
12774a58fd45SPyun YongHyeon 		    pci_alloc_msix(dev, &msixc) == 0) {
12784a58fd45SPyun YongHyeon 			if (msixc == 1) {
12794a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI-X message\n",
12804a58fd45SPyun YongHyeon 				    msixc);
12814a58fd45SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSIX;
12824a58fd45SPyun YongHyeon 			} else
12834a58fd45SPyun YongHyeon 				pci_release_msi(dev);
12844a58fd45SPyun YongHyeon 		}
12854a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
12864a58fd45SPyun YongHyeon 			if (sc->rl_res_pba != NULL)
12874a58fd45SPyun YongHyeon 				bus_release_resource(dev, SYS_RES_MEMORY, rid,
12884a58fd45SPyun YongHyeon 				    sc->rl_res_pba);
12894a58fd45SPyun YongHyeon 			sc->rl_res_pba = NULL;
12904a58fd45SPyun YongHyeon 			msixc = 0;
12914a58fd45SPyun YongHyeon 		}
12924a58fd45SPyun YongHyeon 	}
12934a58fd45SPyun YongHyeon 	/* Prefer MSI to INTx. */
12944a58fd45SPyun YongHyeon 	if (msixc == 0 && msic > 0) {
1295f1bb696aSPyun YongHyeon 		msic = 1;
12965774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
12975774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
12984a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI message\n",
12995774c5ffSPyun YongHyeon 				    msic);
1300351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1301339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1302339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1303339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1304339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1305339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1306f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13075774c5ffSPyun YongHyeon 			} else
13085774c5ffSPyun YongHyeon 				pci_release_msi(dev);
13095774c5ffSPyun YongHyeon 		}
13104a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSI) == 0)
13114a58fd45SPyun YongHyeon 			msic = 0;
13125774c5ffSPyun YongHyeon 	}
1313a94100faSBill Paul 
13145774c5ffSPyun YongHyeon 	/* Allocate interrupt */
13154a58fd45SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
13165774c5ffSPyun YongHyeon 		rid = 0;
13175774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
13185774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
13195774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
13205774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1321a94100faSBill Paul 			error = ENXIO;
1322a94100faSBill Paul 			goto fail;
1323a94100faSBill Paul 		}
13245774c5ffSPyun YongHyeon 	} else {
13255774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
13265774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
13275774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
13285774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
13295774c5ffSPyun YongHyeon 				device_printf(dev,
13302df05392SSergey Kandaurov 				    "couldn't allocate IRQ resources for "
13315774c5ffSPyun YongHyeon 				    "message %d\n", rid);
13325774c5ffSPyun YongHyeon 				error = ENXIO;
13335774c5ffSPyun YongHyeon 				goto fail;
13345774c5ffSPyun YongHyeon 			}
13355774c5ffSPyun YongHyeon 		}
13365774c5ffSPyun YongHyeon 	}
1337a94100faSBill Paul 
13384d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
13394d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
13404d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
13414d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
13424d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
13434d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
13444d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
13454d2bf239SPyun YongHyeon 		}
13464d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13474d2bf239SPyun YongHyeon 	}
13484d2bf239SPyun YongHyeon 
1349017f1c8dSPyun YongHyeon 	/* Disable ASPM L0S/L1. */
1350017f1c8dSPyun YongHyeon 	if (sc->rl_expcap != 0) {
1351017f1c8dSPyun YongHyeon 		cap = pci_read_config(dev, sc->rl_expcap +
1352389c8bd5SGavin Atkinson 		    PCIER_LINK_CAP, 2);
1353389c8bd5SGavin Atkinson 		if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
1354017f1c8dSPyun YongHyeon 			ctl = pci_read_config(dev, sc->rl_expcap +
1355389c8bd5SGavin Atkinson 			    PCIER_LINK_CTL, 2);
1356e935190aSGavin Atkinson 			if ((ctl & PCIEM_LINK_CTL_ASPMC) != 0) {
1357e935190aSGavin Atkinson 				ctl &= ~PCIEM_LINK_CTL_ASPMC;
1358017f1c8dSPyun YongHyeon 				pci_write_config(dev, sc->rl_expcap +
1359389c8bd5SGavin Atkinson 				    PCIER_LINK_CTL, ctl, 2);
1360017f1c8dSPyun YongHyeon 				device_printf(dev, "ASPM disabled\n");
1361017f1c8dSPyun YongHyeon 			}
1362017f1c8dSPyun YongHyeon 		} else
1363017f1c8dSPyun YongHyeon 			device_printf(dev, "no ASPM capability\n");
1364017f1c8dSPyun YongHyeon 	}
1365017f1c8dSPyun YongHyeon 
1366abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1367a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1368566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1369566ca8caSJung-uk Kim 	case 0x00000000:
1370566ca8caSJung-uk Kim 	case 0x10000000:
1371566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1372566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1373566ca8caSJung-uk Kim 		break;
1374566ca8caSJung-uk Kim 	default:
1375a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1376fd3ae0f5SPyun YongHyeon 		sc->rl_macrev = hwrev & 0x00700000;
1377a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1378566ca8caSJung-uk Kim 		break;
1379566ca8caSJung-uk Kim 	}
1380fd3ae0f5SPyun YongHyeon 	device_printf(dev, "MAC rev. 0x%08x\n", sc->rl_macrev);
1381abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1382abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1383abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
138481eee0ebSPyun YongHyeon 			sc->rl_hwrev = hw_rev;
1385abc8ff44SBill Paul 			break;
1386abc8ff44SBill Paul 		}
1387abc8ff44SBill Paul 		hw_rev++;
1388abc8ff44SBill Paul 	}
1389d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1390a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1391d65abd66SPyun YongHyeon 		error = ENXIO;
1392d65abd66SPyun YongHyeon 		goto fail;
1393d65abd66SPyun YongHyeon 	}
1394abc8ff44SBill Paul 
1395351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1396351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
139781eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1398351a76f9SPyun YongHyeon 		break;
1399351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1400351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
140181eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1402351a76f9SPyun YongHyeon 		break;
1403b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1404b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
14053d22427cSTai-hwa Liang 	case RL_HWREV_8102EL_SPIN1:
140681eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
140781eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
140881eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1409b1d62f0fSPyun YongHyeon 		break;
14108281a098SPyun YongHyeon 	case RL_HWREV_8103E:
141181eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
141281eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
141381eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
14148281a098SPyun YongHyeon 		break;
141539e69201SPyun YongHyeon 	case RL_HWREV_8401E:
141654899a96SPyun YongHyeon 	case RL_HWREV_8105E:
14176b0a8e04SPyun YongHyeon 	case RL_HWREV_8105E_SPIN1:
1418214c71f6SPyun YongHyeon 	case RL_HWREV_8106E:
141954899a96SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
142054899a96SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
142154899a96SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
142254899a96SPyun YongHyeon 		break;
1423eef0e496SPyun YongHyeon 	case RL_HWREV_8402:
1424eef0e496SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1425eef0e496SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1426eef0e496SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD |
1427eef0e496SPyun YongHyeon 		    RL_FLAG_CMDSTOP_WAIT_TXQ;
1428eef0e496SPyun YongHyeon 		break;
1429ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN1:
1430ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN2:
1431886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1432886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1433ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN3:
1434aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1435deb5c680SPyun YongHyeon 		break;
1436deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
143761f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
143861f45a72SPyun YongHyeon 		/* FALLTHROUGH */
143961f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
1440fd3ae0f5SPyun YongHyeon 		if (sc->rl_macrev == 0x00200000)
144161f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
144261f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1443deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
1444aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1445f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
14466830588dSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1447351a76f9SPyun YongHyeon 		break;
1448df2dc2b3SPyun YongHyeon 	case RL_HWREV_8168D:
1449df2dc2b3SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1450df2dc2b3SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1451df2dc2b3SPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1452df2dc2b3SPyun YongHyeon 		    RL_FLAG_WOL_MANLINK;
1453df2dc2b3SPyun YongHyeon 		break;
1454eef0e496SPyun YongHyeon 	case RL_HWREV_8168DP:
1455eef0e496SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1456eef0e496SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_AUTOPAD |
14576830588dSPyun YongHyeon 		    RL_FLAG_JUMBOV2 | RL_FLAG_WAIT_TXPOLL | RL_FLAG_WOL_MANLINK;
1458eef0e496SPyun YongHyeon 		break;
1459d0c45156SPyun YongHyeon 	case RL_HWREV_8168E:
1460d0c45156SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1461d0c45156SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
14626830588dSPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
14636830588dSPyun YongHyeon 		    RL_FLAG_WOL_MANLINK;
1464d0c45156SPyun YongHyeon 		break;
1465f0431c5bSPyun YongHyeon 	case RL_HWREV_8168E_VL:
1466c3767eabSPyun YongHyeon 	case RL_HWREV_8168EP:
1467d467ffaaSPyun YongHyeon 	case RL_HWREV_8168F:
1468ab9f923eSPyun YongHyeon 	case RL_HWREV_8168G:
1469d56f7f52SPyun YongHyeon 	case RL_HWREV_8411:
1470ab9f923eSPyun YongHyeon 	case RL_HWREV_8411B:
1471f0431c5bSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1472f0431c5bSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1473eef0e496SPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
14746830588dSPyun YongHyeon 		    RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK;
1475f0431c5bSPyun YongHyeon 		break;
1476ab9f923eSPyun YongHyeon 	case RL_HWREV_8168GU:
1477ab9f923eSPyun YongHyeon 		if (pci_get_device(dev) == RT_DEVICEID_8101E) {
1478ab9f923eSPyun YongHyeon 			/* RTL8106EUS */
1479ab9f923eSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_FASTETHER;
1480ab9f923eSPyun YongHyeon 		} else
1481ab9f923eSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1482ab9f923eSPyun YongHyeon 
1483ab9f923eSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1484ab9f923eSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1485ab9f923eSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_CMDSTOP_WAIT_TXQ;
1486ab9f923eSPyun YongHyeon 		break;
1487566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1488566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1489566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1490566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1491566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1492566ca8caSJung-uk Kim 		/* FALLTHROUGH */
14930596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
14940596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1495566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1496566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1497351a76f9SPyun YongHyeon 		break;
1498351a76f9SPyun YongHyeon 	default:
1499351a76f9SPyun YongHyeon 		break;
1500351a76f9SPyun YongHyeon 	}
1501351a76f9SPyun YongHyeon 
1502e7e7593cSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8139CPLUS) {
1503e7e7593cSPyun YongHyeon 		sc->rl_cfg0 = RL_8139_CFG0;
1504e7e7593cSPyun YongHyeon 		sc->rl_cfg1 = RL_8139_CFG1;
1505e7e7593cSPyun YongHyeon 		sc->rl_cfg2 = 0;
1506e7e7593cSPyun YongHyeon 		sc->rl_cfg3 = RL_8139_CFG3;
1507e7e7593cSPyun YongHyeon 		sc->rl_cfg4 = RL_8139_CFG4;
1508e7e7593cSPyun YongHyeon 		sc->rl_cfg5 = RL_8139_CFG5;
1509e7e7593cSPyun YongHyeon 	} else {
1510e7e7593cSPyun YongHyeon 		sc->rl_cfg0 = RL_CFG0;
1511e7e7593cSPyun YongHyeon 		sc->rl_cfg1 = RL_CFG1;
1512e7e7593cSPyun YongHyeon 		sc->rl_cfg2 = RL_CFG2;
1513e7e7593cSPyun YongHyeon 		sc->rl_cfg3 = RL_CFG3;
1514e7e7593cSPyun YongHyeon 		sc->rl_cfg4 = RL_CFG4;
1515e7e7593cSPyun YongHyeon 		sc->rl_cfg5 = RL_CFG5;
1516e7e7593cSPyun YongHyeon 	}
1517e7e7593cSPyun YongHyeon 
151893252626SPyun YongHyeon 	/* Reset the adapter. */
151993252626SPyun YongHyeon 	RL_LOCK(sc);
152093252626SPyun YongHyeon 	re_reset(sc);
152193252626SPyun YongHyeon 	RL_UNLOCK(sc);
152293252626SPyun YongHyeon 
1523deb5c680SPyun YongHyeon 	/* Enable PME. */
1524deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1525e7e7593cSPyun YongHyeon 	cfg = CSR_READ_1(sc, sc->rl_cfg1);
1526deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1527e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, cfg);
1528e7e7593cSPyun YongHyeon 	cfg = CSR_READ_1(sc, sc->rl_cfg5);
1529deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1530e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, cfg);
1531deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1532deb5c680SPyun YongHyeon 
1533deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1534deb5c680SPyun YongHyeon 		/*
1535deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1536deb5c680SPyun YongHyeon 		 * address from EEPROM.
1537deb5c680SPyun YongHyeon 		 */
1538deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1539deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1540deb5c680SPyun YongHyeon 	} else {
1541141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1542ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1543a94100faSBill Paul 		if (re_did != 0x8129)
1544141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1545a94100faSBill Paul 
1546a94100faSBill Paul 		/*
1547a94100faSBill Paul 		 * Get station address from the EEPROM.
1548a94100faSBill Paul 		 */
1549ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1550be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1551be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1552de8925a2SKevin Lo 		bcopy(as, eaddr, ETHER_ADDR_LEN);
1553deb5c680SPyun YongHyeon 	}
1554ed510fb0SBill Paul 
1555ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1556d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1557ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1558ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1559d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1560d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1561ed510fb0SBill Paul 	} else {
1562d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1563ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1564ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1565d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1566d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1567abc8ff44SBill Paul 	}
15689bac70b8SBill Paul 
1569a94100faSBill Paul 	error = re_allocmem(dev, sc);
1570a94100faSBill Paul 	if (error)
1571a94100faSBill Paul 		goto fail;
15720534aae0SPyun YongHyeon 	re_add_sysctls(sc);
1573a94100faSBill Paul 
1574cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1575cd036ec1SBrooks Davis 	if (ifp == NULL) {
1576d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1577cd036ec1SBrooks Davis 		error = ENOSPC;
1578cd036ec1SBrooks Davis 		goto fail;
1579cd036ec1SBrooks Davis 	}
1580cd036ec1SBrooks Davis 
158161f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
158261f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
158361f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
158461f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
158561f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
158661f45a72SPyun YongHyeon 		else
158761f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
158861f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
158961f45a72SPyun YongHyeon 	}
159061f45a72SPyun YongHyeon 
1591351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
159239e69201SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) {
1593d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
159439e69201SPyun YongHyeon 		if (hw_rev->rl_rev == RL_HWREV_8401E)
159539e69201SPyun YongHyeon 			CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08);
159639e69201SPyun YongHyeon 	}
1597351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1598351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1599351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1600351a76f9SPyun YongHyeon 	}
1601351a76f9SPyun YongHyeon 
1602a94100faSBill Paul 	ifp->if_softc = sc;
16039bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1604a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1605a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1606a94100faSBill Paul 	ifp->if_start = re_start;
1607bc2a1002SPyun YongHyeon 	/*
1608bc2a1002SPyun YongHyeon 	 * RTL8168/8111C generates wrong IP checksummed frame if the
1609bc2a1002SPyun YongHyeon 	 * packet has IP options so disable TX IP checksum offloading.
1610bc2a1002SPyun YongHyeon 	 */
1611bc2a1002SPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C ||
16123c2a957dSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2 ||
16133c2a957dSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168CP)
1614bc2a1002SPyun YongHyeon 		ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1615bc2a1002SPyun YongHyeon 	else
1616bc2a1002SPyun YongHyeon 		ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
1617bc2a1002SPyun YongHyeon 	ifp->if_hwassist |= CSUM_TSO;
1618d6d7d923SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1619498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1620a94100faSBill Paul 	ifp->if_init = re_init;
162152732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
162252732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
162352732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1624a94100faSBill Paul 
1625ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1626ed510fb0SBill Paul 
1627fed3ed71SPyun YongHyeon #define	RE_PHYAD_INTERNAL	 0
1628fed3ed71SPyun YongHyeon 
1629fed3ed71SPyun YongHyeon 	/* Do MII setup. */
1630fed3ed71SPyun YongHyeon 	phy = RE_PHYAD_INTERNAL;
1631fed3ed71SPyun YongHyeon 	if (sc->rl_type == RL_8169)
1632fed3ed71SPyun YongHyeon 		phy = 1;
1633fed3ed71SPyun YongHyeon 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
1634fed3ed71SPyun YongHyeon 	    re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
1635fed3ed71SPyun YongHyeon 	if (error != 0) {
1636fed3ed71SPyun YongHyeon 		device_printf(dev, "attaching PHYs failed\n");
1637fed3ed71SPyun YongHyeon 		goto fail;
1638fed3ed71SPyun YongHyeon 	}
1639fed3ed71SPyun YongHyeon 
1640a94100faSBill Paul 	/*
1641a94100faSBill Paul 	 * Call MI attach routine.
1642a94100faSBill Paul 	 */
1643a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1644a94100faSBill Paul 
1645960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1646960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1647960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1648960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
16497467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
16503b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &reg) == 0)
16517467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1652960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
165344f7cbf5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST);
1654a2a8420cSPyun YongHyeon 	/*
1655f9ad4da7SPyun YongHyeon 	 * Don't enable TSO by default.  It is known to generate
1656f9ad4da7SPyun YongHyeon 	 * corrupted TCP segments(bad TCP options) under certain
16572df05392SSergey Kandaurov 	 * circumstances.
1658a2a8420cSPyun YongHyeon 	 */
1659a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1660ecafbbb5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1661960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1662960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1663960fd5b3SPyun YongHyeon #endif
1664960fd5b3SPyun YongHyeon 	/*
1665960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1666960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1667960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1668960fd5b3SPyun YongHyeon 	 */
1669960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1670960fd5b3SPyun YongHyeon 
1671579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
1672579a6e3cSLuigi Rizzo 	re_netmap_attach(sc);
1673579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
1674ed510fb0SBill Paul #ifdef RE_DIAG
1675ed510fb0SBill Paul 	/*
1676ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1677ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1678ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1679ed510fb0SBill Paul 	 */
1680a94100faSBill Paul 
1681ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1682ed510fb0SBill Paul 		error = re_diag(sc);
1683a94100faSBill Paul 		if (error) {
1684ed510fb0SBill Paul 			device_printf(dev,
1685ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1686a94100faSBill Paul 			ether_ifdetach(ifp);
1687a94100faSBill Paul 			goto fail;
1688a94100faSBill Paul 		}
1689ed510fb0SBill Paul 	}
1690ed510fb0SBill Paul #endif
1691a94100faSBill Paul 
1692502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
1693502be0f7SPyun YongHyeon 	intr_filter = 1;
1694502be0f7SPyun YongHyeon #endif
1695a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1696502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
1697502be0f7SPyun YongHyeon 	    intr_filter == 0) {
1698502be0f7SPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
1699502be0f7SPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc,
1700502be0f7SPyun YongHyeon 		    &sc->rl_intrhand[0]);
1701502be0f7SPyun YongHyeon 	} else {
17025774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
17035774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
17045774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
17055774c5ffSPyun YongHyeon 	}
1706a94100faSBill Paul 	if (error) {
1707d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1708a94100faSBill Paul 		ether_ifdetach(ifp);
1709a94100faSBill Paul 	}
1710a94100faSBill Paul 
1711a94100faSBill Paul fail:
1712ed510fb0SBill Paul 
1713a94100faSBill Paul 	if (error)
1714a94100faSBill Paul 		re_detach(dev);
1715a94100faSBill Paul 
1716a94100faSBill Paul 	return (error);
1717a94100faSBill Paul }
1718a94100faSBill Paul 
1719a94100faSBill Paul /*
1720a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1721a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1722a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1723a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1724a94100faSBill Paul  * allocated.
1725a94100faSBill Paul  */
1726a94100faSBill Paul static int
17277b5ffebfSPyun YongHyeon re_detach(device_t dev)
1728a94100faSBill Paul {
1729a94100faSBill Paul 	struct rl_softc		*sc;
1730a94100faSBill Paul 	struct ifnet		*ifp;
17315774c5ffSPyun YongHyeon 	int			i, rid;
1732a94100faSBill Paul 
1733a94100faSBill Paul 	sc = device_get_softc(dev);
1734fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1735aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
173697b9d4baSJohn-Mark Gurney 
173781cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
173881cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
173940929967SGleb Smirnoff #ifdef DEVICE_POLLING
174040929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
174140929967SGleb Smirnoff 			ether_poll_deregister(ifp);
174240929967SGleb Smirnoff #endif
174397b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
174497b9d4baSJohn-Mark Gurney #if 0
174597b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
174697b9d4baSJohn-Mark Gurney #endif
1747a94100faSBill Paul 		re_stop(sc);
1748525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1749d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
17503d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
1751a94100faSBill Paul 		/*
1752a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1753a94100faSBill Paul 		 * still had a BPF descriptor attached to this
175497b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1755a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1756a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1757a94100faSBill Paul 		 * which will try to call re_init() again. This will
1758a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1759a94100faSBill Paul 		 * which will panic the system when the kernel tries
1760a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1761a94100faSBill Paul 		 * anymore.
1762a94100faSBill Paul 		 */
1763a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1764525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1765a94100faSBill Paul 	}
1766a94100faSBill Paul 	if (sc->rl_miibus)
1767a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1768a94100faSBill Paul 	bus_generic_detach(dev);
1769a94100faSBill Paul 
177097b9d4baSJohn-Mark Gurney 	/*
177197b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
177297b9d4baSJohn-Mark Gurney 	 * stopped here.
177397b9d4baSJohn-Mark Gurney 	 */
177497b9d4baSJohn-Mark Gurney 
1775502be0f7SPyun YongHyeon 	if (sc->rl_intrhand[0] != NULL) {
1776502be0f7SPyun YongHyeon 		bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
1777502be0f7SPyun YongHyeon 		sc->rl_intrhand[0] = NULL;
17785774c5ffSPyun YongHyeon 	}
177982242c11SKevin Lo 	if (ifp != NULL) {
178082242c11SKevin Lo #ifdef DEV_NETMAP
178182242c11SKevin Lo 		netmap_detach(ifp);
178282242c11SKevin Lo #endif /* DEV_NETMAP */
1783ad4f426eSWarner Losh 		if_free(ifp);
178482242c11SKevin Lo 	}
1785502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
1786502be0f7SPyun YongHyeon 		rid = 0;
1787502be0f7SPyun YongHyeon 	else
1788502be0f7SPyun YongHyeon 		rid = 1;
17895774c5ffSPyun YongHyeon 	if (sc->rl_irq[0] != NULL) {
1790502be0f7SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]);
17915774c5ffSPyun YongHyeon 		sc->rl_irq[0] = NULL;
17925774c5ffSPyun YongHyeon 	}
1793502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0)
17945774c5ffSPyun YongHyeon 		pci_release_msi(dev);
17954a58fd45SPyun YongHyeon 	if (sc->rl_res_pba) {
17964a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
17974a58fd45SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
17984a58fd45SPyun YongHyeon 	}
1799a94100faSBill Paul 	if (sc->rl_res)
1800ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1801ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1802a94100faSBill Paul 
1803a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1804a94100faSBill Paul 
1805a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
18060534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map)
1807a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1808a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
18090534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list)
1810a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1811a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list,
1812a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
1813a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1814a94100faSBill Paul 	}
1815a94100faSBill Paul 
1816a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1817a94100faSBill Paul 
1818a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
18190534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map)
1820a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1821a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
18220534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list)
1823a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1824a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list,
1825a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
1826a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1827a94100faSBill Paul 	}
1828a94100faSBill Paul 
1829a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1830a94100faSBill Paul 
1831d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
18329e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
18339e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1834d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1835d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
18369e18005dSPyun YongHyeon 		}
1837d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1838d65abd66SPyun YongHyeon 	}
1839d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
18409e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
18419e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1842d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1843d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
18449e18005dSPyun YongHyeon 		}
1845d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1846d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1847d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1848d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1849a94100faSBill Paul 	}
185081eee0ebSPyun YongHyeon 	if (sc->rl_ldata.rl_jrx_mtag) {
185181eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
185281eee0ebSPyun YongHyeon 			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
185381eee0ebSPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
185481eee0ebSPyun YongHyeon 				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
185581eee0ebSPyun YongHyeon 		}
185681eee0ebSPyun YongHyeon 		if (sc->rl_ldata.rl_jrx_sparemap)
185781eee0ebSPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
185881eee0ebSPyun YongHyeon 			    sc->rl_ldata.rl_jrx_sparemap);
185981eee0ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
186081eee0ebSPyun YongHyeon 	}
1861a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1862a94100faSBill Paul 
1863a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
18640534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap)
1865a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1866a94100faSBill Paul 			    sc->rl_ldata.rl_smap);
18670534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats)
18680534aae0SPyun YongHyeon 			bus_dmamem_free(sc->rl_ldata.rl_stag,
18690534aae0SPyun YongHyeon 			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1870a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1871a94100faSBill Paul 	}
1872a94100faSBill Paul 
1873a94100faSBill Paul 	if (sc->rl_parent_tag)
1874a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1875a94100faSBill Paul 
1876a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1877a94100faSBill Paul 
1878a94100faSBill Paul 	return (0);
1879a94100faSBill Paul }
1880a94100faSBill Paul 
1881d65abd66SPyun YongHyeon static __inline void
18827b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1883a94100faSBill Paul {
1884d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1885d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1886d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1887a94100faSBill Paul 
188881eee0ebSPyun YongHyeon 	if (sc->rl_ifp->if_mtu > RL_MTU &&
188981eee0ebSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
189081eee0ebSPyun YongHyeon 		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
189181eee0ebSPyun YongHyeon 	else
1892d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1893d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1894d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1895d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1896d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1897d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1898d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1899d65abd66SPyun YongHyeon }
1900d65abd66SPyun YongHyeon 
1901d65abd66SPyun YongHyeon static int
19027b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1903d65abd66SPyun YongHyeon {
1904d65abd66SPyun YongHyeon 	struct mbuf		*m;
1905d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1906d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1907d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1908d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1909d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1910d65abd66SPyun YongHyeon 	int			error, nsegs;
1911d65abd66SPyun YongHyeon 
1912c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1913d65abd66SPyun YongHyeon 	if (m == NULL)
1914a94100faSBill Paul 		return (ENOBUFS);
1915a94100faSBill Paul 
1916a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
191722a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
191822a11c96SJohn-Mark Gurney 	/*
191922a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
192022a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
192122a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
192222a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
192322a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
192422a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
192522a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
192622a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
192722a11c96SJohn-Mark Gurney 	 */
192822a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
192922a11c96SJohn-Mark Gurney #endif
1930d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1931d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1932d65abd66SPyun YongHyeon 	if (error != 0) {
1933d65abd66SPyun YongHyeon 		m_freem(m);
1934d65abd66SPyun YongHyeon 		return (ENOBUFS);
1935d65abd66SPyun YongHyeon 	}
1936d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1937a94100faSBill Paul 
1938d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1939d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1940d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1941d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1942d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1943a94100faSBill Paul 	}
1944a94100faSBill Paul 
1945d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1946d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1947d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1948d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1949d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1950d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1951a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1952a94100faSBill Paul 
1953d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1954d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1955d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1956d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1957d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1958d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1959d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1960d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1961d65abd66SPyun YongHyeon 
1962a94100faSBill Paul 	return (0);
1963a94100faSBill Paul }
1964a94100faSBill Paul 
196581eee0ebSPyun YongHyeon static int
196681eee0ebSPyun YongHyeon re_jumbo_newbuf(struct rl_softc *sc, int idx)
196781eee0ebSPyun YongHyeon {
196881eee0ebSPyun YongHyeon 	struct mbuf		*m;
196981eee0ebSPyun YongHyeon 	struct rl_rxdesc	*rxd;
197081eee0ebSPyun YongHyeon 	bus_dma_segment_t	segs[1];
197181eee0ebSPyun YongHyeon 	bus_dmamap_t		map;
197281eee0ebSPyun YongHyeon 	struct rl_desc		*desc;
197381eee0ebSPyun YongHyeon 	uint32_t		cmdstat;
197481eee0ebSPyun YongHyeon 	int			error, nsegs;
197581eee0ebSPyun YongHyeon 
1976c6499eccSGleb Smirnoff 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
197781eee0ebSPyun YongHyeon 	if (m == NULL)
197881eee0ebSPyun YongHyeon 		return (ENOBUFS);
197981eee0ebSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
198081eee0ebSPyun YongHyeon #ifdef RE_FIXUP_RX
198181eee0ebSPyun YongHyeon 	m_adj(m, RE_ETHER_ALIGN);
198281eee0ebSPyun YongHyeon #endif
198381eee0ebSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
198481eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
198581eee0ebSPyun YongHyeon 	if (error != 0) {
198681eee0ebSPyun YongHyeon 		m_freem(m);
198781eee0ebSPyun YongHyeon 		return (ENOBUFS);
198881eee0ebSPyun YongHyeon 	}
198981eee0ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
199081eee0ebSPyun YongHyeon 
199181eee0ebSPyun YongHyeon 	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
199281eee0ebSPyun YongHyeon 	if (rxd->rx_m != NULL) {
199381eee0ebSPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
199481eee0ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
199581eee0ebSPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
199681eee0ebSPyun YongHyeon 	}
199781eee0ebSPyun YongHyeon 
199881eee0ebSPyun YongHyeon 	rxd->rx_m = m;
199981eee0ebSPyun YongHyeon 	map = rxd->rx_dmamap;
200081eee0ebSPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
200181eee0ebSPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
200281eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_jrx_sparemap = map;
200381eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
200481eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
200581eee0ebSPyun YongHyeon 
200681eee0ebSPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
200781eee0ebSPyun YongHyeon 	desc->rl_vlanctl = 0;
200881eee0ebSPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
200981eee0ebSPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
201081eee0ebSPyun YongHyeon 	cmdstat = segs[0].ds_len;
201181eee0ebSPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
201281eee0ebSPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
201381eee0ebSPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
201481eee0ebSPyun YongHyeon 
201581eee0ebSPyun YongHyeon 	return (0);
201681eee0ebSPyun YongHyeon }
201781eee0ebSPyun YongHyeon 
201822a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
201922a11c96SJohn-Mark Gurney static __inline void
20207b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
202122a11c96SJohn-Mark Gurney {
202222a11c96SJohn-Mark Gurney 	int                     i;
202322a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
202422a11c96SJohn-Mark Gurney 
202522a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
202622a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
202722a11c96SJohn-Mark Gurney 
202822a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
202922a11c96SJohn-Mark Gurney 		*dst++ = *src++;
203022a11c96SJohn-Mark Gurney 
203122a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
203222a11c96SJohn-Mark Gurney }
203322a11c96SJohn-Mark Gurney #endif
203422a11c96SJohn-Mark Gurney 
2035a94100faSBill Paul static int
20367b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
2037a94100faSBill Paul {
2038d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2039d65abd66SPyun YongHyeon 	int			i;
204097b9d4baSJohn-Mark Gurney 
204197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
204297b9d4baSJohn-Mark Gurney 
2043d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
2044d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
2045d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
2046d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
2047579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2048579a6e3cSLuigi Rizzo 	re_netmap_tx_init(sc);
2049579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2050d65abd66SPyun YongHyeon 	/* Set EOR. */
2051d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
2052d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
2053a94100faSBill Paul 
2054a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2055d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
2056d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2057d65abd66SPyun YongHyeon 
2058a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
2059a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
2060d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
2061a94100faSBill Paul 
2062a94100faSBill Paul 	return (0);
2063a94100faSBill Paul }
2064a94100faSBill Paul 
2065a94100faSBill Paul static int
20667b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
2067a94100faSBill Paul {
2068d65abd66SPyun YongHyeon 	int			error, i;
2069a94100faSBill Paul 
2070d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
2071d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2072d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2073d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
2074d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
2075d65abd66SPyun YongHyeon 			return (error);
2076a94100faSBill Paul 	}
2077579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2078579a6e3cSLuigi Rizzo 	re_netmap_rx_init(sc);
2079579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2080a94100faSBill Paul 
2081a94100faSBill Paul 	/* Flush the RX descriptors */
2082a94100faSBill Paul 
2083a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2084a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2085a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2086a94100faSBill Paul 
2087a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
2088a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
2089502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
2090a94100faSBill Paul 
2091a94100faSBill Paul 	return (0);
2092a94100faSBill Paul }
2093a94100faSBill Paul 
209481eee0ebSPyun YongHyeon static int
209581eee0ebSPyun YongHyeon re_jrx_list_init(struct rl_softc *sc)
209681eee0ebSPyun YongHyeon {
209781eee0ebSPyun YongHyeon 	int			error, i;
209881eee0ebSPyun YongHyeon 
209981eee0ebSPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
210081eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
210181eee0ebSPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
210281eee0ebSPyun YongHyeon 		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
210381eee0ebSPyun YongHyeon 		if ((error = re_jumbo_newbuf(sc, i)) != 0)
210481eee0ebSPyun YongHyeon 			return (error);
210581eee0ebSPyun YongHyeon 	}
210681eee0ebSPyun YongHyeon 
210781eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
210881eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_list_map,
210981eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
211081eee0ebSPyun YongHyeon 
211181eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_rx_prodidx = 0;
211281eee0ebSPyun YongHyeon 	sc->rl_head = sc->rl_tail = NULL;
2113502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
211481eee0ebSPyun YongHyeon 
211581eee0ebSPyun YongHyeon 	return (0);
211681eee0ebSPyun YongHyeon }
211781eee0ebSPyun YongHyeon 
2118a94100faSBill Paul /*
2119a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
2120a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
2121a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
2122a94100faSBill Paul  */
2123ed510fb0SBill Paul static int
21241abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2125a94100faSBill Paul {
2126a94100faSBill Paul 	struct mbuf		*m;
2127a94100faSBill Paul 	struct ifnet		*ifp;
212881eee0ebSPyun YongHyeon 	int			i, rxerr, total_len;
2129a94100faSBill Paul 	struct rl_desc		*cur_rx;
2130a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
213181eee0ebSPyun YongHyeon 	int			jumbo, maxpkt = 16, rx_npkts = 0;
2132a94100faSBill Paul 
21335120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
21345120abbfSSam Leffler 
2135fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2136579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2137*ce3ee1e7SLuigi Rizzo 	if (netmap_rx_irq(ifp, 0, &rx_npkts))
2138579a6e3cSLuigi Rizzo 		return 0;
2139579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
214081eee0ebSPyun YongHyeon 	if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
214181eee0ebSPyun YongHyeon 		jumbo = 1;
214281eee0ebSPyun YongHyeon 	else
214381eee0ebSPyun YongHyeon 		jumbo = 0;
2144a94100faSBill Paul 
2145a94100faSBill Paul 	/* Invalidate the descriptor memory */
2146a94100faSBill Paul 
2147a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2148a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2149d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2150a94100faSBill Paul 
2151d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2152d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
21535b6d1d9dSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
21545b6d1d9dSPyun YongHyeon 			break;
2155a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
2156a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
2157d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2158d65abd66SPyun YongHyeon 			break;
2159d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
2160a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
216181eee0ebSPyun YongHyeon 		if (jumbo != 0)
216281eee0ebSPyun YongHyeon 			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
216381eee0ebSPyun YongHyeon 		else
2164d65abd66SPyun YongHyeon 			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2165a94100faSBill Paul 
216681eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
216781eee0ebSPyun YongHyeon 		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
216881eee0ebSPyun YongHyeon 		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
216981eee0ebSPyun YongHyeon 			/*
217081eee0ebSPyun YongHyeon 			 * RTL8168C or later controllers do not
217181eee0ebSPyun YongHyeon 			 * support multi-fragment packet.
217281eee0ebSPyun YongHyeon 			 */
217381eee0ebSPyun YongHyeon 			re_discard_rxbuf(sc, i);
217481eee0ebSPyun YongHyeon 			continue;
217581eee0ebSPyun YongHyeon 		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2176d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
2177d65abd66SPyun YongHyeon 				/*
2178d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
2179d65abd66SPyun YongHyeon 				 * discard all the pieces.
2180d65abd66SPyun YongHyeon 				 */
2181d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
2182d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
2183d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
2184d65abd66SPyun YongHyeon 				}
2185d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2186d65abd66SPyun YongHyeon 				continue;
2187d65abd66SPyun YongHyeon 			}
218822a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
2189a94100faSBill Paul 			if (sc->rl_head == NULL)
2190a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
2191a94100faSBill Paul 			else {
2192a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2193a94100faSBill Paul 				sc->rl_tail->m_next = m;
2194a94100faSBill Paul 				sc->rl_tail = m;
2195a94100faSBill Paul 			}
2196a94100faSBill Paul 			continue;
2197a94100faSBill Paul 		}
2198a94100faSBill Paul 
2199a94100faSBill Paul 		/*
2200a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
2201a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
2202a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
2203a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
2204a94100faSBill Paul 		 * were already used, so to make room for the extra
2205a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
2206a94100faSBill Paul 		 * error' bit and shifted the other status bits
2207a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
2208a94100faSBill Paul 		 * still in the same places. We have already extracted
2209a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
2210a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
2211a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
2212a94100faSBill Paul 		 * them using the 8169 status as though it was in the
2213a94100faSBill Paul 		 * same format as that of the 8139C+.
2214a94100faSBill Paul 		 */
2215a94100faSBill Paul 		if (sc->rl_type == RL_8169)
2216a94100faSBill Paul 			rxstat >>= 1;
2217a94100faSBill Paul 
221822a11c96SJohn-Mark Gurney 		/*
221922a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
222022a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
222122a11c96SJohn-Mark Gurney 		 */
222281eee0ebSPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
222381eee0ebSPyun YongHyeon 			rxerr = 1;
222481eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
222581eee0ebSPyun YongHyeon 			    total_len > 8191 &&
222681eee0ebSPyun YongHyeon 			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
222781eee0ebSPyun YongHyeon 				rxerr = 0;
222881eee0ebSPyun YongHyeon 			if (rxerr != 0) {
2229a94100faSBill Paul 				ifp->if_ierrors++;
2230a94100faSBill Paul 				/*
2231a94100faSBill Paul 				 * If this is part of a multi-fragment packet,
2232a94100faSBill Paul 				 * discard all the pieces.
2233a94100faSBill Paul 				 */
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 			}
224181eee0ebSPyun YongHyeon 		}
2242a94100faSBill Paul 
2243a94100faSBill Paul 		/*
2244a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
2245a94100faSBill Paul 		 * reload the current one.
2246a94100faSBill Paul 		 */
224781eee0ebSPyun YongHyeon 		if (jumbo != 0)
224881eee0ebSPyun YongHyeon 			rxerr = re_jumbo_newbuf(sc, i);
224981eee0ebSPyun YongHyeon 		else
225081eee0ebSPyun YongHyeon 			rxerr = re_newbuf(sc, i);
225181eee0ebSPyun YongHyeon 		if (rxerr != 0) {
2252d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
2253a94100faSBill Paul 			if (sc->rl_head != NULL) {
2254a94100faSBill Paul 				m_freem(sc->rl_head);
2255a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
2256a94100faSBill Paul 			}
2257d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
2258a94100faSBill Paul 			continue;
2259a94100faSBill Paul 		}
2260a94100faSBill Paul 
2261a94100faSBill Paul 		if (sc->rl_head != NULL) {
226281eee0ebSPyun YongHyeon 			if (jumbo != 0)
226381eee0ebSPyun YongHyeon 				m->m_len = total_len;
226481eee0ebSPyun YongHyeon 			else {
226522a11c96SJohn-Mark Gurney 				m->m_len = total_len % RE_RX_DESC_BUFLEN;
226622a11c96SJohn-Mark Gurney 				if (m->m_len == 0)
226722a11c96SJohn-Mark Gurney 					m->m_len = RE_RX_DESC_BUFLEN;
226881eee0ebSPyun YongHyeon 			}
2269a94100faSBill Paul 			/*
2270a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
2271a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
2272a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
2273a94100faSBill Paul 			 * care about anyway.
2274a94100faSBill Paul 			 */
2275a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
2276a94100faSBill Paul 				sc->rl_tail->m_len -=
2277a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
2278a94100faSBill Paul 				m_freem(m);
2279a94100faSBill Paul 			} else {
2280a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
2281a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2282a94100faSBill Paul 				sc->rl_tail->m_next = m;
2283a94100faSBill Paul 			}
2284a94100faSBill Paul 			m = sc->rl_head;
2285a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
2286a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2287a94100faSBill Paul 		} else
2288a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
2289a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
2290a94100faSBill Paul 
229122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
229222a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
229322a11c96SJohn-Mark Gurney #endif
2294a94100faSBill Paul 		ifp->if_ipackets++;
2295a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
2296a94100faSBill Paul 
2297a94100faSBill Paul 		/* Do RX checksumming if enabled */
2298a94100faSBill Paul 
2299a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2300deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2301a94100faSBill Paul 				/* Check IP header checksum */
2302a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
2303deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2304deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2305a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2306deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2307deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2308a94100faSBill Paul 
2309a94100faSBill Paul 				/* Check TCP/UDP checksum */
2310a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
2311a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2312a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
2313a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2314a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
2315a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2316a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
2317a94100faSBill Paul 				}
2318deb5c680SPyun YongHyeon 			} else {
2319deb5c680SPyun YongHyeon 				/*
2320deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2321deb5c680SPyun YongHyeon 				 */
2322deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2323deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2324deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2325deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2326deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2327deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2328deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2329deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2330deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
2331deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2332deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
2333deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2334deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2335deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2336deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
2337deb5c680SPyun YongHyeon 				}
2338deb5c680SPyun YongHyeon 			}
2339a94100faSBill Paul 		}
2340ed510fb0SBill Paul 		maxpkt--;
2341d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
234278ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
2343bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
234478ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2345d147662cSGleb Smirnoff 		}
23465120abbfSSam Leffler 		RL_UNLOCK(sc);
2347a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
23485120abbfSSam Leffler 		RL_LOCK(sc);
23491abcdbd1SAttilio Rao 		rx_npkts++;
2350a94100faSBill Paul 	}
2351a94100faSBill Paul 
2352a94100faSBill Paul 	/* Flush the RX DMA ring */
2353a94100faSBill Paul 
2354a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2355a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2356a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2357a94100faSBill Paul 
2358a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
2359ed510fb0SBill Paul 
23601abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
23611abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2362ed510fb0SBill Paul 	if (maxpkt)
2363ed510fb0SBill Paul 		return (EAGAIN);
2364ed510fb0SBill Paul 
2365ed510fb0SBill Paul 	return (0);
2366a94100faSBill Paul }
2367a94100faSBill Paul 
2368a94100faSBill Paul static void
23697b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2370a94100faSBill Paul {
2371a94100faSBill Paul 	struct ifnet		*ifp;
2372d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2373a94100faSBill Paul 	u_int32_t		txstat;
2374d65abd66SPyun YongHyeon 	int			cons;
2375d65abd66SPyun YongHyeon 
2376d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2377d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2378d65abd66SPyun YongHyeon 		return;
2379a94100faSBill Paul 
2380fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2381579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2382*ce3ee1e7SLuigi Rizzo 	if (netmap_tx_irq(ifp, 0))
2383579a6e3cSLuigi Rizzo 		return;
2384579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2385a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2386a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2387a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2388d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2389a94100faSBill Paul 
2390d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2391d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2392d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2393d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2394a94100faSBill Paul 			break;
2395a94100faSBill Paul 		/*
2396a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2397a94100faSBill Paul 		 * in a fragment chain, which also happens to
2398a94100faSBill Paul 		 * be the only place where the TX status bits
2399a94100faSBill Paul 		 * are valid.
2400a94100faSBill Paul 		 */
2401a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2402d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2403d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2404d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2405d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2406d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2407d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2408d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2409d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2410d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2411a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2412a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2413a94100faSBill Paul 				ifp->if_collisions++;
2414a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2415a94100faSBill Paul 				ifp->if_oerrors++;
2416a94100faSBill Paul 			else
2417a94100faSBill Paul 				ifp->if_opackets++;
2418a94100faSBill Paul 		}
2419a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2420d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2421a94100faSBill Paul 	}
2422d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2423a94100faSBill Paul 
2424a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2425a94100faSBill Paul 
2426d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2427ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2428a94100faSBill Paul 		/*
2429b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2430b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2431a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2432a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2433a94100faSBill Paul 		 */
2434a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2435ed510fb0SBill Paul #endif
2436b4b95879SMarius Strobl 	} else
2437b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2438a94100faSBill Paul }
2439a94100faSBill Paul 
2440a94100faSBill Paul static void
24417b5ffebfSPyun YongHyeon re_tick(void *xsc)
2442a94100faSBill Paul {
2443a94100faSBill Paul 	struct rl_softc		*sc;
2444d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2445a94100faSBill Paul 
2446a94100faSBill Paul 	sc = xsc;
244797b9d4baSJohn-Mark Gurney 
244897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
244997b9d4baSJohn-Mark Gurney 
24501d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2451a94100faSBill Paul 	mii_tick(mii);
24520fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
24530fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2454c2d2e19cSPyun YongHyeon 	/*
2455c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2456c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2457c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2458c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2459c2d2e19cSPyun YongHyeon 	 */
2460c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2461130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2462d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2463a94100faSBill Paul }
2464a94100faSBill Paul 
2465a94100faSBill Paul #ifdef DEVICE_POLLING
24661abcdbd1SAttilio Rao static int
2467a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2468a94100faSBill Paul {
2469a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
24701abcdbd1SAttilio Rao 	int rx_npkts = 0;
2471a94100faSBill Paul 
2472a94100faSBill Paul 	RL_LOCK(sc);
247340929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
24741abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
247597b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
24761abcdbd1SAttilio Rao 	return (rx_npkts);
247797b9d4baSJohn-Mark Gurney }
247897b9d4baSJohn-Mark Gurney 
24791abcdbd1SAttilio Rao static int
248097b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
248197b9d4baSJohn-Mark Gurney {
248297b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
24831abcdbd1SAttilio Rao 	int rx_npkts;
248497b9d4baSJohn-Mark Gurney 
248597b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
248697b9d4baSJohn-Mark Gurney 
2487a94100faSBill Paul 	sc->rxcycles = count;
24881abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2489a94100faSBill Paul 	re_txeof(sc);
2490a94100faSBill Paul 
249137652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2492d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2493a94100faSBill Paul 
2494a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2495a94100faSBill Paul 		u_int16_t       status;
2496a94100faSBill Paul 
2497a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2498a94100faSBill Paul 		if (status == 0xffff)
24991abcdbd1SAttilio Rao 			return (rx_npkts);
2500a94100faSBill Paul 		if (status)
2501a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2502818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2503818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2504818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2505a94100faSBill Paul 
2506a94100faSBill Paul 		/*
2507a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2508a94100faSBill Paul 		 */
2509a94100faSBill Paul 
25108476c243SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR) {
25118476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
251297b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2513a94100faSBill Paul 		}
25148476c243SPyun YongHyeon 	}
25151abcdbd1SAttilio Rao 	return (rx_npkts);
2516a94100faSBill Paul }
2517a94100faSBill Paul #endif /* DEVICE_POLLING */
2518a94100faSBill Paul 
2519ef544f63SPaolo Pisati static int
25207b5ffebfSPyun YongHyeon re_intr(void *arg)
2521a94100faSBill Paul {
2522a94100faSBill Paul 	struct rl_softc		*sc;
2523ed510fb0SBill Paul 	uint16_t		status;
2524a94100faSBill Paul 
2525a94100faSBill Paul 	sc = arg;
2526ed510fb0SBill Paul 
2527ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2528498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2529ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2530ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2531ed510fb0SBill Paul 
2532ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2533ed510fb0SBill Paul 
2534ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2535ed510fb0SBill Paul }
2536ed510fb0SBill Paul 
2537ed510fb0SBill Paul static void
25387b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2539ed510fb0SBill Paul {
2540ed510fb0SBill Paul 	struct rl_softc		*sc;
2541ed510fb0SBill Paul 	struct ifnet		*ifp;
2542ed510fb0SBill Paul 	u_int16_t		status;
2543ed510fb0SBill Paul 	int			rval = 0;
2544ed510fb0SBill Paul 
2545ed510fb0SBill Paul 	sc = arg;
2546ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2547a94100faSBill Paul 
2548a94100faSBill Paul 	RL_LOCK(sc);
254997b9d4baSJohn-Mark Gurney 
2550a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2551a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2552a94100faSBill Paul 
2553d65abd66SPyun YongHyeon 	if (sc->suspended ||
2554d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2555ed510fb0SBill Paul 		RL_UNLOCK(sc);
2556ed510fb0SBill Paul 		return;
2557ed510fb0SBill Paul 	}
2558a94100faSBill Paul 
2559ed510fb0SBill Paul #ifdef DEVICE_POLLING
2560ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2561ed510fb0SBill Paul 		RL_UNLOCK(sc);
2562ed510fb0SBill Paul 		return;
2563ed510fb0SBill Paul 	}
2564ed510fb0SBill Paul #endif
2565a94100faSBill Paul 
2566ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
25671abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2568ed510fb0SBill Paul 
2569818951afSPyun YongHyeon 	/*
2570818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2571818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2572818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2573818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2574818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2575818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2576818951afSPyun YongHyeon 	 */
2577818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2578818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2579818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
25803d85c23dSPyun YongHyeon 	if (status & (
2581ed510fb0SBill Paul #ifdef RE_TX_MODERATION
25823d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2583ed510fb0SBill Paul #else
25843d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2585ed510fb0SBill Paul #endif
2586ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2587a94100faSBill Paul 		re_txeof(sc);
2588a94100faSBill Paul 
25898476c243SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
25908476c243SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
259197b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
25928476c243SPyun YongHyeon 	}
2593a94100faSBill Paul 
259452732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2595d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2596a94100faSBill Paul 
2597a94100faSBill Paul 	RL_UNLOCK(sc);
2598ed510fb0SBill Paul 
2599ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2600ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2601ed510fb0SBill Paul 		return;
2602ed510fb0SBill Paul 	}
2603ed510fb0SBill Paul 
2604ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2605a94100faSBill Paul }
2606a94100faSBill Paul 
2607502be0f7SPyun YongHyeon static void
2608502be0f7SPyun YongHyeon re_intr_msi(void *xsc)
2609502be0f7SPyun YongHyeon {
2610502be0f7SPyun YongHyeon 	struct rl_softc		*sc;
2611502be0f7SPyun YongHyeon 	struct ifnet		*ifp;
2612502be0f7SPyun YongHyeon 	uint16_t		intrs, status;
2613502be0f7SPyun YongHyeon 
2614502be0f7SPyun YongHyeon 	sc = xsc;
2615502be0f7SPyun YongHyeon 	RL_LOCK(sc);
2616502be0f7SPyun YongHyeon 
2617502be0f7SPyun YongHyeon 	ifp = sc->rl_ifp;
2618502be0f7SPyun YongHyeon #ifdef DEVICE_POLLING
2619502be0f7SPyun YongHyeon 	if (ifp->if_capenable & IFCAP_POLLING) {
2620502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2621502be0f7SPyun YongHyeon 		return;
2622502be0f7SPyun YongHyeon 	}
2623502be0f7SPyun YongHyeon #endif
2624502be0f7SPyun YongHyeon 	/* Disable interrupts. */
2625502be0f7SPyun YongHyeon 	CSR_WRITE_2(sc, RL_IMR, 0);
2626502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2627502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2628502be0f7SPyun YongHyeon 		return;
2629502be0f7SPyun YongHyeon 	}
2630502be0f7SPyun YongHyeon 
2631502be0f7SPyun YongHyeon 	intrs = RL_INTRS_CPLUS;
2632502be0f7SPyun YongHyeon 	status = CSR_READ_2(sc, RL_ISR);
2633502be0f7SPyun YongHyeon         CSR_WRITE_2(sc, RL_ISR, status);
2634502be0f7SPyun YongHyeon 	if (sc->rl_int_rx_act > 0) {
2635502be0f7SPyun YongHyeon 		intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2636502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2637502be0f7SPyun YongHyeon 		status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2638502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2639502be0f7SPyun YongHyeon 	}
2640502be0f7SPyun YongHyeon 
2641502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR |
2642502be0f7SPyun YongHyeon 	    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) {
2643502be0f7SPyun YongHyeon 		re_rxeof(sc, NULL);
2644502be0f7SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2645502be0f7SPyun YongHyeon 			if (sc->rl_int_rx_mod != 0 &&
2646502be0f7SPyun YongHyeon 			    (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR |
2647502be0f7SPyun YongHyeon 			    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) {
2648502be0f7SPyun YongHyeon 				/* Rearm one-shot timer. */
2649502be0f7SPyun YongHyeon 				CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2650502be0f7SPyun YongHyeon 				intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR |
2651502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN);
2652502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 1;
2653502be0f7SPyun YongHyeon 			} else {
2654502be0f7SPyun YongHyeon 				intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR |
2655502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN;
2656502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 0;
2657502be0f7SPyun YongHyeon 			}
2658502be0f7SPyun YongHyeon 		}
2659502be0f7SPyun YongHyeon 	}
2660502be0f7SPyun YongHyeon 
2661502be0f7SPyun YongHyeon 	/*
2662502be0f7SPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2663502be0f7SPyun YongHyeon 	 * while an existing transmission is in progress. If
2664502be0f7SPyun YongHyeon 	 * the transmitter goes idle but there are still
2665502be0f7SPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2666502be0f7SPyun YongHyeon 	 * channel here to flush them out. This only seems to
2667502be0f7SPyun YongHyeon 	 * be required with the PCIe devices.
2668502be0f7SPyun YongHyeon 	 */
2669502be0f7SPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2670502be0f7SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2671502be0f7SPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2672502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL))
2673502be0f7SPyun YongHyeon 		re_txeof(sc);
2674502be0f7SPyun YongHyeon 
2675502be0f7SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
2676502be0f7SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2677502be0f7SPyun YongHyeon 		re_init_locked(sc);
2678502be0f7SPyun YongHyeon 	}
2679502be0f7SPyun YongHyeon 
2680502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2681502be0f7SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2682502be0f7SPyun YongHyeon 			re_start_locked(ifp);
2683502be0f7SPyun YongHyeon 		CSR_WRITE_2(sc, RL_IMR, intrs);
2684502be0f7SPyun YongHyeon 	}
2685502be0f7SPyun YongHyeon 	RL_UNLOCK(sc);
2686502be0f7SPyun YongHyeon }
2687502be0f7SPyun YongHyeon 
2688d65abd66SPyun YongHyeon static int
26897b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2690d65abd66SPyun YongHyeon {
2691d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2692d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2693d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2694d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2695d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2696d65abd66SPyun YongHyeon 	int			nsegs, prod;
2697d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2698d65abd66SPyun YongHyeon 	int			padlen;
2699ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2700a94100faSBill Paul 
2701d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2702738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
27030fc4974fSBill Paul 
27040fc4974fSBill Paul 	/*
27050fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
27060fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
27070fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
27080fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
27090fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
27100fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
271199c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
27120fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2713d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
27140fc4974fSBill Paul 	 */
2715f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2716deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
271799c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2718d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2719d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2720d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2721c6499eccSGleb Smirnoff 			m_new = m_dup(*m_head, M_NOWAIT);
2722d65abd66SPyun YongHyeon 			m_freem(*m_head);
2723d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2724d65abd66SPyun YongHyeon 				*m_head = NULL;
2725a94100faSBill Paul 				return (ENOBUFS);
2726a94100faSBill Paul 			}
2727d65abd66SPyun YongHyeon 			*m_head = m_new;
2728d65abd66SPyun YongHyeon 		}
2729d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2730d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
2731c6499eccSGleb Smirnoff 			m_new = m_defrag(*m_head, M_NOWAIT);
2732b4b95879SMarius Strobl 			if (m_new == NULL) {
2733b4b95879SMarius Strobl 				m_freem(*m_head);
2734b4b95879SMarius Strobl 				*m_head = NULL;
273580a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2736b4b95879SMarius Strobl 			}
2737d65abd66SPyun YongHyeon 		} else
2738d65abd66SPyun YongHyeon 			m_new = *m_head;
2739a94100faSBill Paul 
27400fc4974fSBill Paul 		/*
27410fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
27420fc4974fSBill Paul 		 * to avoid leaking data.
27430fc4974fSBill Paul 		 */
2744d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2745d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
27460fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2747d65abd66SPyun YongHyeon 		*m_head = m_new;
27480fc4974fSBill Paul 	}
27490fc4974fSBill Paul 
2750d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2751d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2752d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2753d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2754d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2755c6499eccSGleb Smirnoff 		m_new = m_collapse(*m_head, M_NOWAIT, RL_NTXSEGS);
2756d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2757d65abd66SPyun YongHyeon 			m_freem(*m_head);
2758b4b95879SMarius Strobl 			*m_head = NULL;
2759d65abd66SPyun YongHyeon 			return (ENOBUFS);
2760a94100faSBill Paul 		}
2761d65abd66SPyun YongHyeon 		*m_head = m_new;
2762d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2763d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2764d65abd66SPyun YongHyeon 		if (error != 0) {
2765d65abd66SPyun YongHyeon 			m_freem(*m_head);
2766d65abd66SPyun YongHyeon 			*m_head = NULL;
2767d65abd66SPyun YongHyeon 			return (error);
2768a94100faSBill Paul 		}
2769d65abd66SPyun YongHyeon 	} else if (error != 0)
2770d65abd66SPyun YongHyeon 		return (error);
2771d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2772d65abd66SPyun YongHyeon 		m_freem(*m_head);
2773d65abd66SPyun YongHyeon 		*m_head = NULL;
2774d65abd66SPyun YongHyeon 		return (EIO);
2775d65abd66SPyun YongHyeon 	}
2776d65abd66SPyun YongHyeon 
2777d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2778d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2779d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2780d65abd66SPyun YongHyeon 		return (ENOBUFS);
2781d65abd66SPyun YongHyeon 	}
2782d65abd66SPyun YongHyeon 
2783d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2784d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2785a94100faSBill Paul 
2786a94100faSBill Paul 	/*
2787d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2788d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2789d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2790d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2791a94100faSBill Paul 	 */
2792deb5c680SPyun YongHyeon 	vlanctl = 0;
2793d65abd66SPyun YongHyeon 	csum_flags = 0;
2794d6d7d923SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2795d6d7d923SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2796d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND;
2797d6d7d923SPyun YongHyeon 			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2798d6d7d923SPyun YongHyeon 			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2799d6d7d923SPyun YongHyeon 		} else {
2800d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND |
2801d65abd66SPyun YongHyeon 			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2802d65abd66SPyun YongHyeon 			    RL_TDESC_CMD_MSSVAL_SHIFT);
2803d6d7d923SPyun YongHyeon 		}
2804d6d7d923SPyun YongHyeon 	} else {
280599c8ae87SPyun YongHyeon 		/*
280699c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
280799c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
28082df05392SSergey Kandaurov 		 * doesn't make effects.
280999c8ae87SPyun YongHyeon 		 */
281099c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2811deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2812d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2813deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2814deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2815d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2816deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2817deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2818d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2819deb5c680SPyun YongHyeon 			} else {
2820deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2821deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2822deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2823deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2824deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2825deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2826deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2827deb5c680SPyun YongHyeon 			}
2828d65abd66SPyun YongHyeon 		}
282999c8ae87SPyun YongHyeon 	}
2830a94100faSBill Paul 
2831ccf34c81SPyun YongHyeon 	/*
2832ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2833ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2834ccf34c81SPyun YongHyeon 	 * transmission attempt.
2835ccf34c81SPyun YongHyeon 	 */
2836ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2837bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2838deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2839ccf34c81SPyun YongHyeon 
2840d65abd66SPyun YongHyeon 	si = prod;
2841d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2842d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2843deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2844d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2845d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2846d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2847d65abd66SPyun YongHyeon 		if (i != 0)
2848d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2849d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2850d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2851d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2852d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2853d65abd66SPyun YongHyeon 	}
2854d65abd66SPyun YongHyeon 	/* Update producer index. */
2855d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2856a94100faSBill Paul 
2857d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2858d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2859d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2860d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2861d65abd66SPyun YongHyeon 
2862d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2863d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2864d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2865a94100faSBill Paul 
2866d65abd66SPyun YongHyeon 	/*
2867d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2868d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2869d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2870d65abd66SPyun YongHyeon 	 */
2871d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2872d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2873d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2874d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2875d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2876a94100faSBill Paul 
2877a94100faSBill Paul 	return (0);
2878a94100faSBill Paul }
2879a94100faSBill Paul 
288097b9d4baSJohn-Mark Gurney static void
2881d180a66fSPyun YongHyeon re_start(struct ifnet *ifp)
288297b9d4baSJohn-Mark Gurney {
2883d180a66fSPyun YongHyeon 	struct rl_softc		*sc;
288497b9d4baSJohn-Mark Gurney 
2885d180a66fSPyun YongHyeon 	sc = ifp->if_softc;
2886d180a66fSPyun YongHyeon 	RL_LOCK(sc);
2887d180a66fSPyun YongHyeon 	re_start_locked(ifp);
2888d180a66fSPyun YongHyeon 	RL_UNLOCK(sc);
288997b9d4baSJohn-Mark Gurney }
289097b9d4baSJohn-Mark Gurney 
2891a94100faSBill Paul /*
2892a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2893a94100faSBill Paul  */
2894a94100faSBill Paul static void
2895d180a66fSPyun YongHyeon re_start_locked(struct ifnet *ifp)
2896a94100faSBill Paul {
2897a94100faSBill Paul 	struct rl_softc		*sc;
2898d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2899d65abd66SPyun YongHyeon 	int			queued;
2900a94100faSBill Paul 
2901a94100faSBill Paul 	sc = ifp->if_softc;
290297b9d4baSJohn-Mark Gurney 
2903579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2904579a6e3cSLuigi Rizzo 	/* XXX is this necessary ? */
2905579a6e3cSLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
2906579a6e3cSLuigi Rizzo 		struct netmap_kring *kring = &NA(ifp)->tx_rings[0];
2907579a6e3cSLuigi Rizzo 		if (sc->rl_ldata.rl_tx_prodidx != kring->nr_hwcur) {
2908579a6e3cSLuigi Rizzo 			/* kick the tx unit */
2909579a6e3cSLuigi Rizzo 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2910579a6e3cSLuigi Rizzo #ifdef RE_TX_MODERATION
2911579a6e3cSLuigi Rizzo 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2912579a6e3cSLuigi Rizzo #endif
2913579a6e3cSLuigi Rizzo 			sc->rl_watchdog_timer = 5;
2914579a6e3cSLuigi Rizzo 		}
2915579a6e3cSLuigi Rizzo 		return;
2916579a6e3cSLuigi Rizzo 	}
2917579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2918d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2919d180a66fSPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
2920ed510fb0SBill Paul 		return;
2921a94100faSBill Paul 
2922d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2923d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
292452732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2925a94100faSBill Paul 		if (m_head == NULL)
2926a94100faSBill Paul 			break;
2927a94100faSBill Paul 
2928d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2929b4b95879SMarius Strobl 			if (m_head == NULL)
2930b4b95879SMarius Strobl 				break;
293152732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
293213f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2933a94100faSBill Paul 			break;
2934a94100faSBill Paul 		}
2935a94100faSBill Paul 
2936a94100faSBill Paul 		/*
2937a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2938a94100faSBill Paul 		 * to him.
2939a94100faSBill Paul 		 */
294059a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
294152732175SMax Laier 
294252732175SMax Laier 		queued++;
2943a94100faSBill Paul 	}
2944a94100faSBill Paul 
2945ed510fb0SBill Paul 	if (queued == 0) {
2946ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2947d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2948ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2949ed510fb0SBill Paul #endif
295052732175SMax Laier 		return;
2951ed510fb0SBill Paul 	}
295252732175SMax Laier 
2953a94100faSBill Paul 	/* Flush the TX descriptors */
2954a94100faSBill Paul 
2955a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2956a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2957a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2958a94100faSBill Paul 
29590fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2960a94100faSBill Paul 
2961ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2962a94100faSBill Paul 	/*
2963a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2964a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2965a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2966a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2967a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2968a94100faSBill Paul 	 * the timer count is reset to 0.
2969a94100faSBill Paul 	 */
2970a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2971ed510fb0SBill Paul #endif
2972a94100faSBill Paul 
2973a94100faSBill Paul 	/*
2974a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2975a94100faSBill Paul 	 */
29761d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2977a94100faSBill Paul }
2978a94100faSBill Paul 
2979a94100faSBill Paul static void
298081eee0ebSPyun YongHyeon re_set_jumbo(struct rl_softc *sc, int jumbo)
298181eee0ebSPyun YongHyeon {
298281eee0ebSPyun YongHyeon 
298381eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
298481eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
298581eee0ebSPyun YongHyeon 		return;
298681eee0ebSPyun YongHyeon 	}
298781eee0ebSPyun YongHyeon 
298881eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
298981eee0ebSPyun YongHyeon 	if (jumbo != 0) {
2990e7e7593cSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) |
299181eee0ebSPyun YongHyeon 		    RL_CFG3_JUMBO_EN0);
299281eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
299381eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
299481eee0ebSPyun YongHyeon 			break;
299581eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
2996e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
2997e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) | 0x01);
299881eee0ebSPyun YongHyeon 			break;
299981eee0ebSPyun YongHyeon 		default:
3000e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
3001e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) | RL_CFG4_JUMBO_EN1);
300281eee0ebSPyun YongHyeon 		}
300381eee0ebSPyun YongHyeon 	} else {
3004e7e7593cSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) &
300581eee0ebSPyun YongHyeon 		    ~RL_CFG3_JUMBO_EN0);
300681eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
300781eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
300881eee0ebSPyun YongHyeon 			break;
300981eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
3010e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
3011e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) & ~0x01);
301281eee0ebSPyun YongHyeon 			break;
301381eee0ebSPyun YongHyeon 		default:
3014e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
3015e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) & ~RL_CFG4_JUMBO_EN1);
301681eee0ebSPyun YongHyeon 		}
301781eee0ebSPyun YongHyeon 	}
301881eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
301981eee0ebSPyun YongHyeon 
302081eee0ebSPyun YongHyeon 	switch (sc->rl_hwrev->rl_rev) {
302181eee0ebSPyun YongHyeon 	case RL_HWREV_8168DP:
302281eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
302381eee0ebSPyun YongHyeon 		break;
302481eee0ebSPyun YongHyeon 	default:
302581eee0ebSPyun YongHyeon 		if (jumbo != 0)
302681eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 512);
302781eee0ebSPyun YongHyeon 		else
302881eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 4096);
302981eee0ebSPyun YongHyeon 	}
303081eee0ebSPyun YongHyeon }
303181eee0ebSPyun YongHyeon 
303281eee0ebSPyun YongHyeon static void
30337b5ffebfSPyun YongHyeon re_init(void *xsc)
3034a94100faSBill Paul {
3035a94100faSBill Paul 	struct rl_softc		*sc = xsc;
303697b9d4baSJohn-Mark Gurney 
303797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
303897b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
303997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
304097b9d4baSJohn-Mark Gurney }
304197b9d4baSJohn-Mark Gurney 
304297b9d4baSJohn-Mark Gurney static void
30437b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
304497b9d4baSJohn-Mark Gurney {
3045fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
3046a94100faSBill Paul 	struct mii_data		*mii;
3047566ca8caSJung-uk Kim 	uint32_t		reg;
304870acaecfSPyun YongHyeon 	uint16_t		cfg;
30494d3d7085SBernd Walter 	union {
30504d3d7085SBernd Walter 		uint32_t align_dummy;
30514d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
30524d3d7085SBernd Walter         } eaddr;
3053a94100faSBill Paul 
305497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
305597b9d4baSJohn-Mark Gurney 
3056a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3057a94100faSBill Paul 
30588476c243SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
30598476c243SPyun YongHyeon 		return;
30608476c243SPyun YongHyeon 
3061a94100faSBill Paul 	/*
3062a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
3063a94100faSBill Paul 	 */
3064a94100faSBill Paul 	re_stop(sc);
3065a94100faSBill Paul 
3066b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
3067b659f1f0SPyun YongHyeon 	re_reset(sc);
3068b659f1f0SPyun YongHyeon 
3069a94100faSBill Paul 	/*
30704a814a5eSPyun YongHyeon 	 * For C+ mode, initialize the RX descriptors and mbufs.
30714a814a5eSPyun YongHyeon 	 */
307281eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
307381eee0ebSPyun YongHyeon 		if (ifp->if_mtu > RL_MTU) {
307481eee0ebSPyun YongHyeon 			if (re_jrx_list_init(sc) != 0) {
307581eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
307681eee0ebSPyun YongHyeon 				    "no memory for jumbo RX buffers\n");
307781eee0ebSPyun YongHyeon 				re_stop(sc);
307881eee0ebSPyun YongHyeon 				return;
307981eee0ebSPyun YongHyeon 			}
308081eee0ebSPyun YongHyeon 			/* Disable checksum offloading for jumbo frames. */
308181eee0ebSPyun YongHyeon 			ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
308281eee0ebSPyun YongHyeon 			ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
308381eee0ebSPyun YongHyeon 		} else {
308481eee0ebSPyun YongHyeon 			if (re_rx_list_init(sc) != 0) {
308581eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
308681eee0ebSPyun YongHyeon 				    "no memory for RX buffers\n");
308781eee0ebSPyun YongHyeon 				re_stop(sc);
308881eee0ebSPyun YongHyeon 				return;
308981eee0ebSPyun YongHyeon 			}
309081eee0ebSPyun YongHyeon 		}
309181eee0ebSPyun YongHyeon 		re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
309281eee0ebSPyun YongHyeon 	} else {
30934a814a5eSPyun YongHyeon 		if (re_rx_list_init(sc) != 0) {
30944a814a5eSPyun YongHyeon 			device_printf(sc->rl_dev, "no memory for RX buffers\n");
30954a814a5eSPyun YongHyeon 			re_stop(sc);
30964a814a5eSPyun YongHyeon 			return;
30974a814a5eSPyun YongHyeon 		}
309881eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
309981eee0ebSPyun YongHyeon 		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
310081eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
310181eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 512);
310281eee0ebSPyun YongHyeon 			else
310381eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 4096);
310481eee0ebSPyun YongHyeon 		}
310581eee0ebSPyun YongHyeon 	}
31064a814a5eSPyun YongHyeon 	re_tx_list_init(sc);
31074a814a5eSPyun YongHyeon 
31084a814a5eSPyun YongHyeon 	/*
3109c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
3110edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
3111c2c6548bSBill Paul 	 * before all others.
3112c2c6548bSBill Paul 	 */
311370acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
311470acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
311570acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
311670acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
311770acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
3118deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
3119deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
3120deb5c680SPyun YongHyeon 		/* XXX magic. */
3121deb5c680SPyun YongHyeon 		cfg |= 0x0001;
3122deb5c680SPyun YongHyeon 	} else
3123deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
3124deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
312581eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
312681eee0ebSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
3127566ca8caSJung-uk Kim 		reg = 0x000fff00;
3128e7e7593cSPyun YongHyeon 		if ((CSR_READ_1(sc, sc->rl_cfg2) & RL_CFG2_PCI66MHZ) != 0)
3129566ca8caSJung-uk Kim 			reg |= 0x000000ff;
313081eee0ebSPyun YongHyeon 		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
3131566ca8caSJung-uk Kim 			reg |= 0x00f00000;
3132566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
3133566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
3134566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
3135566ca8caSJung-uk Kim 	}
3136ae644087SPyun YongHyeon 	/*
3137ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
3138ae644087SPyun YongHyeon 	 * allowed in controller.
3139ae644087SPyun YongHyeon 	 */
3140ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
3141ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
3142ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
3143ae644087SPyun YongHyeon 	}
3144c2c6548bSBill Paul 
3145c2c6548bSBill Paul 	/*
3146a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
3147a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
3148a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
3149a94100faSBill Paul 	 */
31504d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
31514d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
3152a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3153ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
3154ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
3155ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
3156ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
3157a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3158a94100faSBill Paul 
3159a94100faSBill Paul 	/*
3160d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
3161d01fac16SPyun YongHyeon 	 */
3162d01fac16SPyun YongHyeon 
3163d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
3164d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
3165d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
3166d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
3167d01fac16SPyun YongHyeon 
3168d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
3169d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
3170d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
3171d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
3172d01fac16SPyun YongHyeon 
3173d01fac16SPyun YongHyeon 	/*
3174a94100faSBill Paul 	 * Enable transmit and receive.
3175a94100faSBill Paul 	 */
3176a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3177a94100faSBill Paul 
3178a94100faSBill Paul 	/*
3179ff191365SJung-uk Kim 	 * Set the initial TX configuration.
3180a94100faSBill Paul 	 */
3181abc8ff44SBill Paul 	if (sc->rl_testmode) {
3182abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
3183abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3184abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
3185a94100faSBill Paul 		else
3186abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3187abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
3188abc8ff44SBill Paul 	} else
3189a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
3190d01fac16SPyun YongHyeon 
3191d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
3192d01fac16SPyun YongHyeon 
3193a94100faSBill Paul 	/*
3194ff191365SJung-uk Kim 	 * Set the initial RX configuration.
3195a94100faSBill Paul 	 */
3196ff191365SJung-uk Kim 	re_set_rxmode(sc);
3197a94100faSBill Paul 
3198483cc440SPyun YongHyeon 	/* Configure interrupt moderation. */
3199483cc440SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
3200483cc440SPyun YongHyeon 		/* Magic from vendor. */
32015e6906eeSPyun YongHyeon 		CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
3202483cc440SPyun YongHyeon 	}
3203483cc440SPyun YongHyeon 
3204a94100faSBill Paul #ifdef DEVICE_POLLING
3205a94100faSBill Paul 	/*
3206a94100faSBill Paul 	 * Disable interrupts if we are polling.
3207a94100faSBill Paul 	 */
320840929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3209a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3210a94100faSBill Paul 	else	/* otherwise ... */
321140929967SGleb Smirnoff #endif
3212ed510fb0SBill Paul 
3213a94100faSBill Paul 	/*
3214a94100faSBill Paul 	 * Enable interrupts.
3215a94100faSBill Paul 	 */
3216a94100faSBill Paul 	if (sc->rl_testmode)
3217a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3218a94100faSBill Paul 	else
3219a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3220ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3221a94100faSBill Paul 
3222a94100faSBill Paul 	/* Set initial TX threshold */
3223a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
3224a94100faSBill Paul 
3225a94100faSBill Paul 	/* Start RX/TX process. */
3226a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3227a94100faSBill Paul #ifdef notdef
3228a94100faSBill Paul 	/* Enable receiver and transmitter. */
3229a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3230a94100faSBill Paul #endif
3231a94100faSBill Paul 
3232a94100faSBill Paul 	/*
3233a94100faSBill Paul 	 * Initialize the timer interrupt register so that
3234a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
3235a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
3236502be0f7SPyun YongHyeon 	 * reloaded on each transmit.
3237502be0f7SPyun YongHyeon 	 */
3238502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
3239502be0f7SPyun YongHyeon 	/*
3240502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate TX interrupt
3241a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
3242a94100faSBill Paul 	 */
3243a94100faSBill Paul 	if (sc->rl_type == RL_8169)
3244a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3245a94100faSBill Paul 	else
3246a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3247502be0f7SPyun YongHyeon #else
3248502be0f7SPyun YongHyeon 	/*
3249502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate RX interrupt
3250502be0f7SPyun YongHyeon 	 * moderation.
3251502be0f7SPyun YongHyeon 	 */
3252502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
3253502be0f7SPyun YongHyeon 	    intr_filter == 0) {
3254502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3255502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169,
3256502be0f7SPyun YongHyeon 			    RL_USECS(sc->rl_int_rx_mod));
3257502be0f7SPyun YongHyeon 	} else {
3258502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3259502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0));
3260502be0f7SPyun YongHyeon 	}
3261ed510fb0SBill Paul #endif
3262a94100faSBill Paul 
3263a94100faSBill Paul 	/*
3264a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
3265a94100faSBill Paul 	 * size so we can receive jumbo frames.
3266a94100faSBill Paul 	 */
326789feeee4SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
326881eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
326981eee0ebSPyun YongHyeon 			/*
327081eee0ebSPyun YongHyeon 			 * For controllers that use new jumbo frame scheme,
32712df05392SSergey Kandaurov 			 * set maximum size of jumbo frame depending on
327281eee0ebSPyun YongHyeon 			 * controller revisions.
327381eee0ebSPyun YongHyeon 			 */
327481eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
327581eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
327681eee0ebSPyun YongHyeon 				    sc->rl_hwrev->rl_max_mtu +
327781eee0ebSPyun YongHyeon 				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
327881eee0ebSPyun YongHyeon 				    ETHER_CRC_LEN);
327989feeee4SPyun YongHyeon 			else
328081eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
328181eee0ebSPyun YongHyeon 				    RE_RX_DESC_BUFLEN);
328281eee0ebSPyun YongHyeon 		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
328381eee0ebSPyun YongHyeon 		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
328481eee0ebSPyun YongHyeon 			/* RTL810x has no jumbo frame support. */
328581eee0ebSPyun YongHyeon 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
328681eee0ebSPyun YongHyeon 		} else
3287a94100faSBill Paul 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
328889feeee4SPyun YongHyeon 	}
3289a94100faSBill Paul 
329097b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
3291a94100faSBill Paul 		return;
3292a94100faSBill Paul 
3293e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, CSR_READ_1(sc, sc->rl_cfg1) |
3294e7e7593cSPyun YongHyeon 	    RL_CFG1_DRVLOAD);
3295a94100faSBill Paul 
329613f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
329713f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3298a94100faSBill Paul 
3299351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
33001662c49eSPyun YongHyeon 	mii_mediachg(mii);
33011662c49eSPyun YongHyeon 
33021d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3303d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3304a94100faSBill Paul }
3305a94100faSBill Paul 
3306a94100faSBill Paul /*
3307a94100faSBill Paul  * Set media options.
3308a94100faSBill Paul  */
3309a94100faSBill Paul static int
33107b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
3311a94100faSBill Paul {
3312a94100faSBill Paul 	struct rl_softc		*sc;
3313a94100faSBill Paul 	struct mii_data		*mii;
33146f0f9b12SPyun YongHyeon 	int			error;
3315a94100faSBill Paul 
3316a94100faSBill Paul 	sc = ifp->if_softc;
3317a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3318d1754a9bSJohn Baldwin 	RL_LOCK(sc);
33196f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
3320d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3321a94100faSBill Paul 
33226f0f9b12SPyun YongHyeon 	return (error);
3323a94100faSBill Paul }
3324a94100faSBill Paul 
3325a94100faSBill Paul /*
3326a94100faSBill Paul  * Report current media status.
3327a94100faSBill Paul  */
3328a94100faSBill Paul static void
33297b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3330a94100faSBill Paul {
3331a94100faSBill Paul 	struct rl_softc		*sc;
3332a94100faSBill Paul 	struct mii_data		*mii;
3333a94100faSBill Paul 
3334a94100faSBill Paul 	sc = ifp->if_softc;
3335a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3336a94100faSBill Paul 
3337d1754a9bSJohn Baldwin 	RL_LOCK(sc);
3338a94100faSBill Paul 	mii_pollstat(mii);
3339a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
3340a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
334157c81d92SPyun YongHyeon 	RL_UNLOCK(sc);
3342a94100faSBill Paul }
3343a94100faSBill Paul 
3344a94100faSBill Paul static int
33457b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3346a94100faSBill Paul {
3347a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
3348a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
3349a94100faSBill Paul 	struct mii_data		*mii;
3350bc2a1002SPyun YongHyeon 	uint32_t		rev;
335140929967SGleb Smirnoff 	int			error = 0;
3352a94100faSBill Paul 
3353a94100faSBill Paul 	switch (command) {
3354a94100faSBill Paul 	case SIOCSIFMTU:
335581eee0ebSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN ||
3356ab9f923eSPyun YongHyeon 		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu ||
3357ab9f923eSPyun YongHyeon 		    ((sc->rl_flags & RL_FLAG_FASTETHER) != 0 &&
3358ab9f923eSPyun YongHyeon 		    ifr->ifr_mtu > RL_MTU)) {
3359c1d0b573SPyun YongHyeon 			error = EINVAL;
3360c1d0b573SPyun YongHyeon 			break;
3361c1d0b573SPyun YongHyeon 		}
3362c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
336381eee0ebSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
3364a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
336581eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
336681eee0ebSPyun YongHyeon 			    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
336781eee0ebSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
336881eee0ebSPyun YongHyeon 				re_init_locked(sc);
336981eee0ebSPyun YongHyeon 			}
3370ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3371ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
337281eee0ebSPyun YongHyeon 				ifp->if_capenable &= ~(IFCAP_TSO4 |
337381eee0ebSPyun YongHyeon 				    IFCAP_VLAN_HWTSO);
3374ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
337581eee0ebSPyun YongHyeon 			}
3376ecafbbb5SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
3377ae644087SPyun YongHyeon 		}
3378d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
3379a94100faSBill Paul 		break;
3380a94100faSBill Paul 	case SIOCSIFFLAGS:
338197b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
3382eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
3383eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3384eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
33853021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3386ff191365SJung-uk Kim 					re_set_rxmode(sc);
3387eed497bbSPyun YongHyeon 			} else
338897b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
3389eed497bbSPyun YongHyeon 		} else {
3390eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3391a94100faSBill Paul 				re_stop(sc);
3392eed497bbSPyun YongHyeon 		}
3393eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
339497b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3395a94100faSBill Paul 		break;
3396a94100faSBill Paul 	case SIOCADDMULTI:
3397a94100faSBill Paul 	case SIOCDELMULTI:
339897b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
33998476c243SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3400ff191365SJung-uk Kim 			re_set_rxmode(sc);
340197b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3402a94100faSBill Paul 		break;
3403a94100faSBill Paul 	case SIOCGIFMEDIA:
3404a94100faSBill Paul 	case SIOCSIFMEDIA:
3405a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
3406a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3407a94100faSBill Paul 		break;
3408a94100faSBill Paul 	case SIOCSIFCAP:
340940929967SGleb Smirnoff 	    {
3410f051cb85SGleb Smirnoff 		int mask, reinit;
3411f051cb85SGleb Smirnoff 
3412f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3413f051cb85SGleb Smirnoff 		reinit = 0;
341440929967SGleb Smirnoff #ifdef DEVICE_POLLING
341540929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
341640929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
341740929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
341840929967SGleb Smirnoff 				if (error)
341940929967SGleb Smirnoff 					return (error);
3420d1754a9bSJohn Baldwin 				RL_LOCK(sc);
342140929967SGleb Smirnoff 				/* Disable interrupts */
342240929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
342340929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
342440929967SGleb Smirnoff 				RL_UNLOCK(sc);
342540929967SGleb Smirnoff 			} else {
342640929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
342740929967SGleb Smirnoff 				/* Enable interrupts. */
342840929967SGleb Smirnoff 				RL_LOCK(sc);
342940929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
343040929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
343140929967SGleb Smirnoff 				RL_UNLOCK(sc);
343240929967SGleb Smirnoff 			}
343340929967SGleb Smirnoff 		}
343440929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3435600af6c2SPyun YongHyeon 		RL_LOCK(sc);
3436d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
3437d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3438d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
3439bc2a1002SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) {
3440bc2a1002SPyun YongHyeon 				rev = sc->rl_hwrev->rl_rev;
3441bc2a1002SPyun YongHyeon 				if (rev == RL_HWREV_8168C ||
34423c2a957dSPyun YongHyeon 				    rev == RL_HWREV_8168C_SPIN2 ||
34433c2a957dSPyun YongHyeon 				    rev == RL_HWREV_8168CP)
3444bc2a1002SPyun YongHyeon 					ifp->if_hwassist |= CSUM_TCP | CSUM_UDP;
3445a94100faSBill Paul 				else
3446bc2a1002SPyun YongHyeon 					ifp->if_hwassist |= RE_CSUM_FEATURES;
3447bc2a1002SPyun YongHyeon 			} else
3448b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3449f051cb85SGleb Smirnoff 			reinit = 1;
345040929967SGleb Smirnoff 		}
3451d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
3452d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3453d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
3454d3b181aeSPyun YongHyeon 			reinit = 1;
3455d3b181aeSPyun YongHyeon 		}
3456ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
3457fca1e0abSBjoern A. Zeeb 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
3458dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
3459ecafbbb5SPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3460dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
3461dc74159dSPyun YongHyeon 			else
3462dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3463ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3464ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3465ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
3466ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3467ae644087SPyun YongHyeon 			}
3468dc74159dSPyun YongHyeon 		}
3469ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3470ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3471ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3472ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3473ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3474ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3475ecafbbb5SPyun YongHyeon 			/* TSO over VLAN requires VLAN hardware tagging. */
3476ecafbbb5SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3477ecafbbb5SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3478ecafbbb5SPyun YongHyeon 			reinit = 1;
3479ecafbbb5SPyun YongHyeon 		}
348081eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
348181eee0ebSPyun YongHyeon 		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
348281eee0ebSPyun YongHyeon 		    IFCAP_VLAN_HWTSO)) != 0)
348381eee0ebSPyun YongHyeon 				reinit = 1;
34847467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
34857467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
34867467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
34877467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
34887467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
34897467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
34907467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
34917467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
34927467bd53SPyun YongHyeon 		}
34938476c243SPyun YongHyeon 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
34948476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3495600af6c2SPyun YongHyeon 			re_init_locked(sc);
34968476c243SPyun YongHyeon 		}
3497600af6c2SPyun YongHyeon 		RL_UNLOCK(sc);
3498960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
349940929967SGleb Smirnoff 	    }
3500a94100faSBill Paul 		break;
3501a94100faSBill Paul 	default:
3502a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
3503a94100faSBill Paul 		break;
3504a94100faSBill Paul 	}
3505a94100faSBill Paul 
3506a94100faSBill Paul 	return (error);
3507a94100faSBill Paul }
3508a94100faSBill Paul 
3509a94100faSBill Paul static void
35107b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
35111d545c7aSMarius Strobl {
3512130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
3513a94100faSBill Paul 
35141d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
35151d545c7aSMarius Strobl 
35161d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
35171d545c7aSMarius Strobl 		return;
35181d545c7aSMarius Strobl 
3519130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
3520a94100faSBill Paul 	re_txeof(sc);
3521130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3522130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3523130b6dfbSPyun YongHyeon 		    "-- recovering\n");
3524130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3525d180a66fSPyun YongHyeon 			re_start_locked(ifp);
3526130b6dfbSPyun YongHyeon 		return;
3527130b6dfbSPyun YongHyeon 	}
3528130b6dfbSPyun YongHyeon 
3529130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
3530130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
3531130b6dfbSPyun YongHyeon 
35321abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
35338476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
353497b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
3535130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3536d180a66fSPyun YongHyeon 		re_start_locked(ifp);
3537a94100faSBill Paul }
3538a94100faSBill Paul 
3539a94100faSBill Paul /*
3540a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
3541a94100faSBill Paul  * RX and TX lists.
3542a94100faSBill Paul  */
3543a94100faSBill Paul static void
35447b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
3545a94100faSBill Paul {
35460ce0868aSPyun YongHyeon 	int			i;
3547a94100faSBill Paul 	struct ifnet		*ifp;
3548d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
3549d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
3550a94100faSBill Paul 
355197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
355297b9d4baSJohn-Mark Gurney 
3553fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
3554a94100faSBill Paul 
35551d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3556d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
355713f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3558a94100faSBill Paul 
3559fcb220acSPyun YongHyeon 	/*
3560fcb220acSPyun YongHyeon 	 * Disable accepting frames to put RX MAC into idle state.
3561fcb220acSPyun YongHyeon 	 * Otherwise it's possible to get frames while stop command
3562fcb220acSPyun YongHyeon 	 * execution is in progress and controller can DMA the frame
3563fcb220acSPyun YongHyeon 	 * to already freed RX buffer during that period.
3564fcb220acSPyun YongHyeon 	 */
3565fcb220acSPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXCFG, CSR_READ_4(sc, RL_RXCFG) &
3566fcb220acSPyun YongHyeon 	    ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI |
3567fcb220acSPyun YongHyeon 	    RL_RXCFG_RX_BROAD));
3568fcb220acSPyun YongHyeon 
3569eef0e496SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) {
3570eef0e496SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
3571eef0e496SPyun YongHyeon 			if ((CSR_READ_1(sc, sc->rl_txstart) &
3572eef0e496SPyun YongHyeon 			    RL_TXSTART_START) == 0)
3573eef0e496SPyun YongHyeon 				break;
3574eef0e496SPyun YongHyeon 			DELAY(20);
3575eef0e496SPyun YongHyeon 		}
3576eef0e496SPyun YongHyeon 		if (i == 0)
3577eef0e496SPyun YongHyeon 			device_printf(sc->rl_dev,
3578eef0e496SPyun YongHyeon 			    "stopping TX poll timed out!\n");
3579eef0e496SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3580eef0e496SPyun YongHyeon 	} else if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) {
3581ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3582ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
3583eef0e496SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_CMDSTOP_WAIT_TXQ) != 0) {
3584eef0e496SPyun YongHyeon 			for (i = RL_TIMEOUT; i > 0; i--) {
3585eef0e496SPyun YongHyeon 				if ((CSR_READ_4(sc, RL_TXCFG) &
3586eef0e496SPyun YongHyeon 				    RL_TXCFG_QUEUE_EMPTY) != 0)
3587eef0e496SPyun YongHyeon 					break;
3588eef0e496SPyun YongHyeon 				DELAY(100);
3589eef0e496SPyun YongHyeon 			}
3590eef0e496SPyun YongHyeon 			if (i == 0)
3591eef0e496SPyun YongHyeon 				device_printf(sc->rl_dev,
3592eef0e496SPyun YongHyeon 				   "stopping TXQ timed out!\n");
3593eef0e496SPyun YongHyeon 		}
3594eef0e496SPyun YongHyeon 	} else
3595a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3596ead8fc66SPyun YongHyeon 	DELAY(1000);
3597a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3598ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3599a94100faSBill Paul 
3600a94100faSBill Paul 	if (sc->rl_head != NULL) {
3601a94100faSBill Paul 		m_freem(sc->rl_head);
3602a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
3603a94100faSBill Paul 	}
3604a94100faSBill Paul 
3605a94100faSBill Paul 	/* Free the TX list buffers. */
3606d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3607d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
3608d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
3609d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3610d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3611d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3612d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
3613d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
3614d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
3615a94100faSBill Paul 		}
3616a94100faSBill Paul 	}
3617a94100faSBill Paul 
3618a94100faSBill Paul 	/* Free the RX list buffers. */
3619d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3620d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
3621d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
3622cba16362SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
3623d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3624d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3625d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
3626d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
3627d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
3628a94100faSBill Paul 		}
3629a94100faSBill Paul 	}
36301f32d3b7SPyun YongHyeon 
36311f32d3b7SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
36321f32d3b7SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
36331f32d3b7SPyun YongHyeon 			rxd = &sc->rl_ldata.rl_jrx_desc[i];
36341f32d3b7SPyun YongHyeon 			if (rxd->rx_m != NULL) {
36351f32d3b7SPyun YongHyeon 				bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag,
36361f32d3b7SPyun YongHyeon 				    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
36371f32d3b7SPyun YongHyeon 				bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag,
36381f32d3b7SPyun YongHyeon 				    rxd->rx_dmamap);
36391f32d3b7SPyun YongHyeon 				m_freem(rxd->rx_m);
36401f32d3b7SPyun YongHyeon 				rxd->rx_m = NULL;
36411f32d3b7SPyun YongHyeon 			}
36421f32d3b7SPyun YongHyeon 		}
36431f32d3b7SPyun YongHyeon 	}
3644a94100faSBill Paul }
3645a94100faSBill Paul 
3646a94100faSBill Paul /*
3647a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
3648a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
3649a94100faSBill Paul  * resume.
3650a94100faSBill Paul  */
3651a94100faSBill Paul static int
36527b5ffebfSPyun YongHyeon re_suspend(device_t dev)
3653a94100faSBill Paul {
3654a94100faSBill Paul 	struct rl_softc		*sc;
3655a94100faSBill Paul 
3656a94100faSBill Paul 	sc = device_get_softc(dev);
3657a94100faSBill Paul 
365897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3659a94100faSBill Paul 	re_stop(sc);
36607467bd53SPyun YongHyeon 	re_setwol(sc);
3661a94100faSBill Paul 	sc->suspended = 1;
366297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3663a94100faSBill Paul 
3664a94100faSBill Paul 	return (0);
3665a94100faSBill Paul }
3666a94100faSBill Paul 
3667a94100faSBill Paul /*
3668a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
3669a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
3670a94100faSBill Paul  * appropriate.
3671a94100faSBill Paul  */
3672a94100faSBill Paul static int
36737b5ffebfSPyun YongHyeon re_resume(device_t dev)
3674a94100faSBill Paul {
3675a94100faSBill Paul 	struct rl_softc		*sc;
3676a94100faSBill Paul 	struct ifnet		*ifp;
3677a94100faSBill Paul 
3678a94100faSBill Paul 	sc = device_get_softc(dev);
367997b9d4baSJohn-Mark Gurney 
368097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
368197b9d4baSJohn-Mark Gurney 
3682fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
368361f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
368461f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
368561f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
368661f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
368761f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
368861f45a72SPyun YongHyeon 	}
3689a94100faSBill Paul 
36907467bd53SPyun YongHyeon 	/*
36917467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
36927467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
36937467bd53SPyun YongHyeon 	 */
36947467bd53SPyun YongHyeon 	re_clrwol(sc);
369501d1a6c3SPyun YongHyeon 
369601d1a6c3SPyun YongHyeon 	/* reinitialize interface if necessary */
369701d1a6c3SPyun YongHyeon 	if (ifp->if_flags & IFF_UP)
369801d1a6c3SPyun YongHyeon 		re_init_locked(sc);
369901d1a6c3SPyun YongHyeon 
3700a94100faSBill Paul 	sc->suspended = 0;
370197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3702a94100faSBill Paul 
3703a94100faSBill Paul 	return (0);
3704a94100faSBill Paul }
3705a94100faSBill Paul 
3706a94100faSBill Paul /*
3707a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3708a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3709a94100faSBill Paul  */
37106a087a87SPyun YongHyeon static int
37117b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3712a94100faSBill Paul {
3713a94100faSBill Paul 	struct rl_softc		*sc;
3714a94100faSBill Paul 
3715a94100faSBill Paul 	sc = device_get_softc(dev);
3716a94100faSBill Paul 
371797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3718a94100faSBill Paul 	re_stop(sc);
3719536fde34SMaxim Sobolev 	/*
3720536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3721536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
372272293673SRuslan Ermilov 	 * cases.
3723536fde34SMaxim Sobolev 	 */
3724536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
37257467bd53SPyun YongHyeon 	re_setwol(sc);
372697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
37276a087a87SPyun YongHyeon 
37286a087a87SPyun YongHyeon 	return (0);
3729a94100faSBill Paul }
37307467bd53SPyun YongHyeon 
37317467bd53SPyun YongHyeon static void
37326830588dSPyun YongHyeon re_set_linkspeed(struct rl_softc *sc)
37336830588dSPyun YongHyeon {
37346830588dSPyun YongHyeon 	struct mii_softc *miisc;
37356830588dSPyun YongHyeon 	struct mii_data *mii;
37366830588dSPyun YongHyeon 	int aneg, i, phyno;
37376830588dSPyun YongHyeon 
37386830588dSPyun YongHyeon 	RL_LOCK_ASSERT(sc);
37396830588dSPyun YongHyeon 
37406830588dSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
37416830588dSPyun YongHyeon 	mii_pollstat(mii);
37426830588dSPyun YongHyeon 	aneg = 0;
37436830588dSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
37446830588dSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
37456830588dSPyun YongHyeon 		switch IFM_SUBTYPE(mii->mii_media_active) {
37466830588dSPyun YongHyeon 		case IFM_10_T:
37476830588dSPyun YongHyeon 		case IFM_100_TX:
37486830588dSPyun YongHyeon 			return;
37496830588dSPyun YongHyeon 		case IFM_1000_T:
37506830588dSPyun YongHyeon 			aneg++;
37516830588dSPyun YongHyeon 			break;
37526830588dSPyun YongHyeon 		default:
37536830588dSPyun YongHyeon 			break;
37546830588dSPyun YongHyeon 		}
37556830588dSPyun YongHyeon 	}
37566830588dSPyun YongHyeon 	miisc = LIST_FIRST(&mii->mii_phys);
37576830588dSPyun YongHyeon 	phyno = miisc->mii_phy;
37586830588dSPyun YongHyeon 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
37596830588dSPyun YongHyeon 		PHY_RESET(miisc);
37606830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno, MII_100T2CR, 0);
37616830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno,
37626830588dSPyun YongHyeon 	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
37636830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno,
37646830588dSPyun YongHyeon 	    MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
37656830588dSPyun YongHyeon 	DELAY(1000);
37666830588dSPyun YongHyeon 	if (aneg != 0) {
37676830588dSPyun YongHyeon 		/*
37686830588dSPyun YongHyeon 		 * Poll link state until re(4) get a 10/100Mbps link.
37696830588dSPyun YongHyeon 		 */
37706830588dSPyun YongHyeon 		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
37716830588dSPyun YongHyeon 			mii_pollstat(mii);
37726830588dSPyun YongHyeon 			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
37736830588dSPyun YongHyeon 			    == (IFM_ACTIVE | IFM_AVALID)) {
37746830588dSPyun YongHyeon 				switch (IFM_SUBTYPE(mii->mii_media_active)) {
37756830588dSPyun YongHyeon 				case IFM_10_T:
37766830588dSPyun YongHyeon 				case IFM_100_TX:
37776830588dSPyun YongHyeon 					return;
37786830588dSPyun YongHyeon 				default:
37796830588dSPyun YongHyeon 					break;
37806830588dSPyun YongHyeon 				}
37816830588dSPyun YongHyeon 			}
37826830588dSPyun YongHyeon 			RL_UNLOCK(sc);
37836830588dSPyun YongHyeon 			pause("relnk", hz);
37846830588dSPyun YongHyeon 			RL_LOCK(sc);
37856830588dSPyun YongHyeon 		}
37866830588dSPyun YongHyeon 		if (i == MII_ANEGTICKS_GIGE)
37876830588dSPyun YongHyeon 			device_printf(sc->rl_dev,
37886830588dSPyun YongHyeon 			    "establishing a link failed, WOL may not work!");
37896830588dSPyun YongHyeon 	}
37906830588dSPyun YongHyeon 	/*
37916830588dSPyun YongHyeon 	 * No link, force MAC to have 100Mbps, full-duplex link.
37926830588dSPyun YongHyeon 	 * MAC does not require reprogramming on resolved speed/duplex,
37936830588dSPyun YongHyeon 	 * so this is just for completeness.
37946830588dSPyun YongHyeon 	 */
37956830588dSPyun YongHyeon 	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
37966830588dSPyun YongHyeon 	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
37976830588dSPyun YongHyeon }
37986830588dSPyun YongHyeon 
37996830588dSPyun YongHyeon static void
38007b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
38017467bd53SPyun YongHyeon {
38027467bd53SPyun YongHyeon 	struct ifnet		*ifp;
38037467bd53SPyun YongHyeon 	int			pmc;
38047467bd53SPyun YongHyeon 	uint16_t		pmstat;
38057467bd53SPyun YongHyeon 	uint8_t			v;
38067467bd53SPyun YongHyeon 
38077467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
38087467bd53SPyun YongHyeon 
38093b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
38107467bd53SPyun YongHyeon 		return;
38117467bd53SPyun YongHyeon 
38127467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
381361f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
381461f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
381561f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
381661f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
381761f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
381861f45a72SPyun YongHyeon 	}
3819fcb220acSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0) {
3820fcb220acSPyun YongHyeon 		re_set_rxmode(sc);
38216830588dSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0)
38226830588dSPyun YongHyeon 			re_set_linkspeed(sc);
3823fcb220acSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3824886ff602SPyun YongHyeon 			CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
3825fcb220acSPyun YongHyeon 	}
38267467bd53SPyun YongHyeon 	/* Enable config register write. */
38277467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
38287467bd53SPyun YongHyeon 
38297467bd53SPyun YongHyeon 	/* Enable PME. */
3830e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg1);
38317467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
38327467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
38337467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
3834e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, v);
38357467bd53SPyun YongHyeon 
3836e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg3);
38377467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
38387467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
38397467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
3840e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
38417467bd53SPyun YongHyeon 
3842e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg5);
384344f7cbf5SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST |
384444f7cbf5SPyun YongHyeon 	    RL_CFG5_WOL_LANWAKE);
38457467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
38467467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
38477467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
38487467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
38497467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
38507467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
3851e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
38527467bd53SPyun YongHyeon 
385344f7cbf5SPyun YongHyeon 	/* Config register write done. */
385444f7cbf5SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
385544f7cbf5SPyun YongHyeon 
3856bc6b129bSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) == 0 &&
3857d0c45156SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3858d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
38597467bd53SPyun YongHyeon 	/*
38607467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
38617467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
38627467bd53SPyun YongHyeon 	 * needed.
38637467bd53SPyun YongHyeon 	 */
38647467bd53SPyun YongHyeon 
38657467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
38667467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
38677467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
38687467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
38697467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
38707467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
38717467bd53SPyun YongHyeon }
38727467bd53SPyun YongHyeon 
38737467bd53SPyun YongHyeon static void
38747b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
38757467bd53SPyun YongHyeon {
38767467bd53SPyun YongHyeon 	int			pmc;
38777467bd53SPyun YongHyeon 	uint8_t			v;
38787467bd53SPyun YongHyeon 
38797467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
38807467bd53SPyun YongHyeon 
38813b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
38827467bd53SPyun YongHyeon 		return;
38837467bd53SPyun YongHyeon 
38847467bd53SPyun YongHyeon 	/* Enable config register write. */
38857467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
38867467bd53SPyun YongHyeon 
3887e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg3);
38887467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
3889e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
38907467bd53SPyun YongHyeon 
38917467bd53SPyun YongHyeon 	/* Config register write done. */
3892f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
38937467bd53SPyun YongHyeon 
3894e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg5);
38957467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
38967467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
3897e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
38987467bd53SPyun YongHyeon }
38990534aae0SPyun YongHyeon 
39000534aae0SPyun YongHyeon static void
39010534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc)
39020534aae0SPyun YongHyeon {
39030534aae0SPyun YongHyeon 	struct sysctl_ctx_list	*ctx;
39040534aae0SPyun YongHyeon 	struct sysctl_oid_list	*children;
3905502be0f7SPyun YongHyeon 	int			error;
39060534aae0SPyun YongHyeon 
39070534aae0SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->rl_dev);
39080534aae0SPyun YongHyeon 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
39090534aae0SPyun YongHyeon 
39100534aae0SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
39110534aae0SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I",
39120534aae0SPyun YongHyeon 	    "Statistics Information");
3913502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
3914502be0f7SPyun YongHyeon 		return;
3915502be0f7SPyun YongHyeon 
3916502be0f7SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod",
3917502be0f7SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, &sc->rl_int_rx_mod, 0,
3918502be0f7SPyun YongHyeon 	    sysctl_hw_re_int_mod, "I", "re RX interrupt moderation");
3919502be0f7SPyun YongHyeon 	/* Pull in device tunables. */
3920502be0f7SPyun YongHyeon 	sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3921502be0f7SPyun YongHyeon 	error = resource_int_value(device_get_name(sc->rl_dev),
3922502be0f7SPyun YongHyeon 	    device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod);
3923502be0f7SPyun YongHyeon 	if (error == 0) {
3924502be0f7SPyun YongHyeon 		if (sc->rl_int_rx_mod < RL_TIMER_MIN ||
3925502be0f7SPyun YongHyeon 		    sc->rl_int_rx_mod > RL_TIMER_MAX) {
3926502be0f7SPyun YongHyeon 			device_printf(sc->rl_dev, "int_rx_mod value out of "
3927502be0f7SPyun YongHyeon 			    "range; using default: %d\n",
3928502be0f7SPyun YongHyeon 			    RL_TIMER_DEFAULT);
3929502be0f7SPyun YongHyeon 			sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3930502be0f7SPyun YongHyeon 		}
3931502be0f7SPyun YongHyeon 	}
3932502be0f7SPyun YongHyeon 
39330534aae0SPyun YongHyeon }
39340534aae0SPyun YongHyeon 
39350534aae0SPyun YongHyeon static int
39360534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS)
39370534aae0SPyun YongHyeon {
39380534aae0SPyun YongHyeon 	struct rl_softc		*sc;
39390534aae0SPyun YongHyeon 	struct rl_stats		*stats;
39400534aae0SPyun YongHyeon 	int			error, i, result;
39410534aae0SPyun YongHyeon 
39420534aae0SPyun YongHyeon 	result = -1;
39430534aae0SPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
39440534aae0SPyun YongHyeon 	if (error || req->newptr == NULL)
39450534aae0SPyun YongHyeon 		return (error);
39460534aae0SPyun YongHyeon 
39470534aae0SPyun YongHyeon 	if (result == 1) {
39480534aae0SPyun YongHyeon 		sc = (struct rl_softc *)arg1;
39490534aae0SPyun YongHyeon 		RL_LOCK(sc);
395016a4824bSPyun YongHyeon 		if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
395116a4824bSPyun YongHyeon 			RL_UNLOCK(sc);
395216a4824bSPyun YongHyeon 			goto done;
395316a4824bSPyun YongHyeon 		}
39540534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
39550534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
39560534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
39570534aae0SPyun YongHyeon 		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
39580534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
39590534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
39600534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
39610534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
39620534aae0SPyun YongHyeon 		    RL_DUMPSTATS_START));
39630534aae0SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
39640534aae0SPyun YongHyeon 			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
39650534aae0SPyun YongHyeon 			    RL_DUMPSTATS_START) == 0)
39660534aae0SPyun YongHyeon 				break;
39670534aae0SPyun YongHyeon 			DELAY(1000);
39680534aae0SPyun YongHyeon 		}
39690534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
39700534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
39710534aae0SPyun YongHyeon 		RL_UNLOCK(sc);
39720534aae0SPyun YongHyeon 		if (i == 0) {
39730534aae0SPyun YongHyeon 			device_printf(sc->rl_dev,
39740534aae0SPyun YongHyeon 			    "DUMP statistics request timed out\n");
39750534aae0SPyun YongHyeon 			return (ETIMEDOUT);
39760534aae0SPyun YongHyeon 		}
397716a4824bSPyun YongHyeon done:
39780534aae0SPyun YongHyeon 		stats = sc->rl_ldata.rl_stats;
39790534aae0SPyun YongHyeon 		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
39800534aae0SPyun YongHyeon 		printf("Tx frames : %ju\n",
39810534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_pkts));
39820534aae0SPyun YongHyeon 		printf("Rx frames : %ju\n",
39830534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_pkts));
39840534aae0SPyun YongHyeon 		printf("Tx errors : %ju\n",
39850534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_errs));
39860534aae0SPyun YongHyeon 		printf("Rx errors : %u\n",
39870534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_errs));
39880534aae0SPyun YongHyeon 		printf("Rx missed frames : %u\n",
39890534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_missed_pkts));
39900534aae0SPyun YongHyeon 		printf("Rx frame alignment errs : %u\n",
39910534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
39920534aae0SPyun YongHyeon 		printf("Tx single collisions : %u\n",
39930534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_onecoll));
39940534aae0SPyun YongHyeon 		printf("Tx multiple collisions : %u\n",
39950534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_multicolls));
39960534aae0SPyun YongHyeon 		printf("Rx unicast frames : %ju\n",
39970534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
39980534aae0SPyun YongHyeon 		printf("Rx broadcast frames : %ju\n",
39990534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
40000534aae0SPyun YongHyeon 		printf("Rx multicast frames : %u\n",
40010534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_mcasts));
40020534aae0SPyun YongHyeon 		printf("Tx aborts : %u\n",
40030534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_tx_aborts));
40040534aae0SPyun YongHyeon 		printf("Tx underruns : %u\n",
40050534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_underruns));
40060534aae0SPyun YongHyeon 	}
40070534aae0SPyun YongHyeon 
40080534aae0SPyun YongHyeon 	return (error);
40090534aae0SPyun YongHyeon }
4010502be0f7SPyun YongHyeon 
4011502be0f7SPyun YongHyeon static int
4012502be0f7SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
4013502be0f7SPyun YongHyeon {
4014502be0f7SPyun YongHyeon 	int error, value;
4015502be0f7SPyun YongHyeon 
4016502be0f7SPyun YongHyeon 	if (arg1 == NULL)
4017502be0f7SPyun YongHyeon 		return (EINVAL);
4018502be0f7SPyun YongHyeon 	value = *(int *)arg1;
4019502be0f7SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
4020502be0f7SPyun YongHyeon 	if (error || req->newptr == NULL)
4021502be0f7SPyun YongHyeon 		return (error);
4022502be0f7SPyun YongHyeon 	if (value < low || value > high)
4023502be0f7SPyun YongHyeon 		return (EINVAL);
4024502be0f7SPyun YongHyeon 	*(int *)arg1 = value;
4025502be0f7SPyun YongHyeon 
4026502be0f7SPyun YongHyeon 	return (0);
4027502be0f7SPyun YongHyeon }
4028502be0f7SPyun YongHyeon 
4029502be0f7SPyun YongHyeon static int
4030502be0f7SPyun YongHyeon sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)
4031502be0f7SPyun YongHyeon {
4032502be0f7SPyun YongHyeon 
4033502be0f7SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN,
4034502be0f7SPyun YongHyeon 	    RL_TIMER_MAX));
4035502be0f7SPyun YongHyeon }
4036