xref: /freebsd/sys/dev/re/if_re.c (revision ce3e137ca1bf6638f9200d5313a0afb25099abf9)
1098ca2bdSWarner Losh /*-
2df57947fSPedro F. Giffuni  * SPDX-License-Identifier: BSD-4-Clause
3df57947fSPedro F. Giffuni  *
4a94100faSBill Paul  * Copyright (c) 1997, 1998-2003
5a94100faSBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
6a94100faSBill Paul  *
7a94100faSBill Paul  * Redistribution and use in source and binary forms, with or without
8a94100faSBill Paul  * modification, are permitted provided that the following conditions
9a94100faSBill Paul  * are met:
10a94100faSBill Paul  * 1. Redistributions of source code must retain the above copyright
11a94100faSBill Paul  *    notice, this list of conditions and the following disclaimer.
12a94100faSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
13a94100faSBill Paul  *    notice, this list of conditions and the following disclaimer in the
14a94100faSBill Paul  *    documentation and/or other materials provided with the distribution.
15a94100faSBill Paul  * 3. All advertising materials mentioning features or use of this software
16a94100faSBill Paul  *    must display the following acknowledgement:
17a94100faSBill Paul  *	This product includes software developed by Bill Paul.
18a94100faSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
19a94100faSBill Paul  *    may be used to endorse or promote products derived from this software
20a94100faSBill Paul  *    without specific prior written permission.
21a94100faSBill Paul  *
22a94100faSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
23a94100faSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24a94100faSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25a94100faSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
26a94100faSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27a94100faSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28a94100faSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29a94100faSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30a94100faSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31a94100faSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32a94100faSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
33a94100faSBill Paul  */
34a94100faSBill Paul 
354dc52c32SDavid E. O'Brien #include <sys/cdefs.h>
364dc52c32SDavid E. O'Brien __FBSDID("$FreeBSD$");
374dc52c32SDavid E. O'Brien 
38a94100faSBill Paul /*
39ed510fb0SBill Paul  * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
40a94100faSBill Paul  *
41a94100faSBill Paul  * Written by Bill Paul <wpaul@windriver.com>
42a94100faSBill Paul  * Senior Networking Software Engineer
43a94100faSBill Paul  * Wind River Systems
44a94100faSBill Paul  */
45a94100faSBill Paul 
46a94100faSBill Paul /*
47a94100faSBill Paul  * This driver is designed to support RealTek's next generation of
48a94100faSBill Paul  * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
49ed510fb0SBill Paul  * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
50ed510fb0SBill Paul  * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E.
51a94100faSBill Paul  *
52a94100faSBill Paul  * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
53a94100faSBill Paul  * with the older 8139 family, however it also supports a special
54a94100faSBill Paul  * C+ mode of operation that provides several new performance enhancing
55a94100faSBill Paul  * features. These include:
56a94100faSBill Paul  *
57a94100faSBill Paul  *	o Descriptor based DMA mechanism. Each descriptor represents
58a94100faSBill Paul  *	  a single packet fragment. Data buffers may be aligned on
59a94100faSBill Paul  *	  any byte boundary.
60a94100faSBill Paul  *
61a94100faSBill Paul  *	o 64-bit DMA
62a94100faSBill Paul  *
63a94100faSBill Paul  *	o TCP/IP checksum offload for both RX and TX
64a94100faSBill Paul  *
65a94100faSBill Paul  *	o High and normal priority transmit DMA rings
66a94100faSBill Paul  *
67a94100faSBill Paul  *	o VLAN tag insertion and extraction
68a94100faSBill Paul  *
69a94100faSBill Paul  *	o TCP large send (segmentation offload)
70a94100faSBill Paul  *
71a94100faSBill Paul  * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
72a94100faSBill Paul  * programming API is fairly straightforward. The RX filtering, EEPROM
73a94100faSBill Paul  * access and PHY access is the same as it is on the older 8139 series
74a94100faSBill Paul  * chips.
75a94100faSBill Paul  *
76a94100faSBill Paul  * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
77a94100faSBill Paul  * same programming API and feature set as the 8139C+ with the following
78a94100faSBill Paul  * differences and additions:
79a94100faSBill Paul  *
80a94100faSBill Paul  *	o 1000Mbps mode
81a94100faSBill Paul  *
82a94100faSBill Paul  *	o Jumbo frames
83a94100faSBill Paul  *
84a94100faSBill Paul  *	o GMII and TBI ports/registers for interfacing with copper
85a94100faSBill Paul  *	  or fiber PHYs
86a94100faSBill Paul  *
87a94100faSBill Paul  *	o RX and TX DMA rings can have up to 1024 descriptors
88a94100faSBill Paul  *	  (the 8139C+ allows a maximum of 64)
89a94100faSBill Paul  *
90a94100faSBill Paul  *	o Slight differences in register layout from the 8139C+
91a94100faSBill Paul  *
92a94100faSBill Paul  * The TX start and timer interrupt registers are at different locations
93a94100faSBill Paul  * on the 8169 than they are on the 8139C+. Also, the status word in the
94a94100faSBill Paul  * RX descriptor has a slightly different bit layout. The 8169 does not
95a94100faSBill Paul  * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
96a94100faSBill Paul  * copper gigE PHY.
97a94100faSBill Paul  *
98a94100faSBill Paul  * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
99a94100faSBill Paul  * (the 'S' stands for 'single-chip'). These devices have the same
100a94100faSBill Paul  * programming API as the older 8169, but also have some vendor-specific
101a94100faSBill Paul  * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
102a94100faSBill Paul  * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
103a94100faSBill Paul  *
104a94100faSBill Paul  * This driver takes advantage of the RX and TX checksum offload and
105a94100faSBill Paul  * VLAN tag insertion/extraction features. It also implements TX
106a94100faSBill Paul  * interrupt moderation using the timer interrupt registers, which
107a94100faSBill Paul  * significantly reduces TX interrupt load. There is also support
108a94100faSBill Paul  * for jumbo frames, however the 8169/8169S/8110S can not transmit
10922a11c96SJohn-Mark Gurney  * jumbo frames larger than 7440, so the max MTU possible with this
11022a11c96SJohn-Mark Gurney  * driver is 7422 bytes.
111a94100faSBill Paul  */
112a94100faSBill Paul 
113f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
114f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
115f0796cd2SGleb Smirnoff #endif
116f0796cd2SGleb Smirnoff 
117a94100faSBill Paul #include <sys/param.h>
118a94100faSBill Paul #include <sys/endian.h>
119a94100faSBill Paul #include <sys/systm.h>
120a94100faSBill Paul #include <sys/sockio.h>
121a94100faSBill Paul #include <sys/mbuf.h>
122a94100faSBill Paul #include <sys/malloc.h>
123fe12f24bSPoul-Henning Kamp #include <sys/module.h>
124a94100faSBill Paul #include <sys/kernel.h>
125a94100faSBill Paul #include <sys/socket.h>
126ed510fb0SBill Paul #include <sys/lock.h>
127ed510fb0SBill Paul #include <sys/mutex.h>
1280534aae0SPyun YongHyeon #include <sys/sysctl.h>
129ed510fb0SBill Paul #include <sys/taskqueue.h>
130a94100faSBill Paul 
1317790c8c1SConrad Meyer #include <net/debugnet.h>
132a94100faSBill Paul #include <net/if.h>
13376039bc8SGleb Smirnoff #include <net/if_var.h>
134a94100faSBill Paul #include <net/if_arp.h>
135a94100faSBill Paul #include <net/ethernet.h>
136a94100faSBill Paul #include <net/if_dl.h>
137a94100faSBill Paul #include <net/if_media.h>
138fc74a9f9SBrooks Davis #include <net/if_types.h>
139a94100faSBill Paul #include <net/if_vlan_var.h>
140a94100faSBill Paul 
141a94100faSBill Paul #include <net/bpf.h>
142a94100faSBill Paul 
143a94100faSBill Paul #include <machine/bus.h>
144a94100faSBill Paul #include <machine/resource.h>
145a94100faSBill Paul #include <sys/bus.h>
146a94100faSBill Paul #include <sys/rman.h>
147a94100faSBill Paul 
148a94100faSBill Paul #include <dev/mii/mii.h>
149a94100faSBill Paul #include <dev/mii/miivar.h>
150a94100faSBill Paul 
151a94100faSBill Paul #include <dev/pci/pcireg.h>
152a94100faSBill Paul #include <dev/pci/pcivar.h>
153a94100faSBill Paul 
154b2d3d26fSGleb Smirnoff #include <dev/rl/if_rlreg.h>
155d65abd66SPyun YongHyeon 
156a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
157a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
158a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
159a94100faSBill Paul 
160298bfdf3SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
161a94100faSBill Paul #include "miibus_if.h"
162a94100faSBill Paul 
1635774c5ffSPyun YongHyeon /* Tunables. */
164502be0f7SPyun YongHyeon static int intr_filter = 0;
165502be0f7SPyun YongHyeon TUNABLE_INT("hw.re.intr_filter", &intr_filter);
166c2d2e19cSPyun YongHyeon static int msi_disable = 0;
1675774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1684a58fd45SPyun YongHyeon static int msix_disable = 0;
1694a58fd45SPyun YongHyeon TUNABLE_INT("hw.re.msix_disable", &msix_disable);
1702c21710bSPyun YongHyeon static int prefer_iomap = 0;
1712c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
1725774c5ffSPyun YongHyeon 
173a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
174a94100faSBill Paul 
175a94100faSBill Paul /*
176a94100faSBill Paul  * Various supported device vendors/types and their names.
177a94100faSBill Paul  */
17829658c96SDimitry Andric static const struct rl_type re_devs[] = {
1799dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
18032aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
181caa19d50SPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_530T_REVC, 0,
182caa19d50SPyun YongHyeon 	    "D-Link DGE-530(T) Gigabit Ethernet Adapter" },
1839dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
184a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1859dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
18654899a96SPyun YongHyeon 	    "RealTek 810xE PCIe 10/100baseTX" },
1879dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
188ab9f923eSPyun YongHyeon 	    "RealTek 8168/8111 B/C/CP/D/DP/E/F/G PCIe Gigabit Ethernet" },
189*ce3e137cSMark Johnston 	{ RT_VENDORID, RT_DEVICEID_8161, 0,
190*ce3e137cSMark Johnston 	    "RealTek 8168 Gigabit Ethernet" },
191938e9a89SKevin Lo 	{ NCUBE_VENDORID, RT_DEVICEID_8168, 0,
192938e9a89SKevin Lo 	    "TP-Link TG-3468 v2 (RTL8168) Gigabit Ethernet" },
1939dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
194715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1959dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1962ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1979dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
198ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1999dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
20026390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
2019dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
202dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
203a94100faSBill Paul };
204a94100faSBill Paul 
20529658c96SDimitry Andric static const struct rl_hwrev re_hwrevs[] = {
20681eee0ebSPyun YongHyeon 	{ RL_HWREV_8139, RL_8139, "", RL_MTU },
20781eee0ebSPyun YongHyeon 	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
20881eee0ebSPyun YongHyeon 	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
20981eee0ebSPyun YongHyeon 	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
21081eee0ebSPyun YongHyeon 	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
21181eee0ebSPyun YongHyeon 	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
21281eee0ebSPyun YongHyeon 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
21381eee0ebSPyun YongHyeon 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
214ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
21581eee0ebSPyun YongHyeon 	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
21681eee0ebSPyun YongHyeon 	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
21781eee0ebSPyun YongHyeon 	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
21881eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
21981eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
22081eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
22181eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
22281eee0ebSPyun YongHyeon 	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
22381eee0ebSPyun YongHyeon 	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
22481eee0ebSPyun YongHyeon 	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
22581eee0ebSPyun YongHyeon 	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
22681eee0ebSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
22781eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
22881eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
22981eee0ebSPyun YongHyeon 	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
23039e69201SPyun YongHyeon 	{ RL_HWREV_8401E, RL_8169, "8401E", RL_MTU },
231a9e3362aSPyun YongHyeon 	{ RL_HWREV_8402, RL_8169, "8402", RL_MTU },
23254899a96SPyun YongHyeon 	{ RL_HWREV_8105E, RL_8169, "8105E", RL_MTU },
2336b0a8e04SPyun YongHyeon 	{ RL_HWREV_8105E_SPIN1, RL_8169, "8105E", RL_MTU },
234214c71f6SPyun YongHyeon 	{ RL_HWREV_8106E, RL_8169, "8106E", RL_MTU },
235ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
236ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
23781eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
23881eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
23981eee0ebSPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
24081eee0ebSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
24181eee0ebSPyun YongHyeon 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
24281eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
24381eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
244c3767eabSPyun YongHyeon 	{ RL_HWREV_8168EP, RL_8169, "8168EP/8111EP", RL_JUMBO_MTU_9K},
245d467ffaaSPyun YongHyeon 	{ RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K},
246ab9f923eSPyun YongHyeon 	{ RL_HWREV_8168G, RL_8169, "8168G/8111G", RL_JUMBO_MTU_9K},
247ab9f923eSPyun YongHyeon 	{ RL_HWREV_8168GU, RL_8169, "8168GU/8111GU", RL_JUMBO_MTU_9K},
24896b2c26aSMarius Strobl 	{ RL_HWREV_8168H, RL_8169, "8168H/8111H", RL_JUMBO_MTU_9K},
249d56f7f52SPyun YongHyeon 	{ RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K},
250ab9f923eSPyun YongHyeon 	{ RL_HWREV_8411B, RL_8169, "8411B", RL_JUMBO_MTU_9K},
25181eee0ebSPyun YongHyeon 	{ 0, 0, NULL, 0 }
252a94100faSBill Paul };
253a94100faSBill Paul 
254a94100faSBill Paul static int re_probe		(device_t);
255a94100faSBill Paul static int re_attach		(device_t);
256a94100faSBill Paul static int re_detach		(device_t);
257a94100faSBill Paul 
258d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
259a94100faSBill Paul 
260a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
261a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
262d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
263d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
264d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
26581eee0ebSPyun YongHyeon static int re_jumbo_newbuf	(struct rl_softc *, int);
266a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
26781eee0ebSPyun YongHyeon static int re_jrx_list_init	(struct rl_softc *);
268a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
26922a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
27022a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
27122a11c96SJohn-Mark Gurney 				(struct mbuf *);
27222a11c96SJohn-Mark Gurney #endif
2731abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
274a94100faSBill Paul static void re_txeof		(struct rl_softc *);
27597b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2761abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2771abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
27897b9d4baSJohn-Mark Gurney #endif
279ef544f63SPaolo Pisati static int re_intr		(void *);
280502be0f7SPyun YongHyeon static void re_intr_msi		(void *);
281a94100faSBill Paul static void re_tick		(void *);
282ed510fb0SBill Paul static void re_int_task		(void *, int);
283a94100faSBill Paul static void re_start		(struct ifnet *);
284d180a66fSPyun YongHyeon static void re_start_locked	(struct ifnet *);
285306c97e2SMark Johnston static void re_start_tx		(struct rl_softc *);
286a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
287a94100faSBill Paul static void re_init		(void *);
28897b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
289a94100faSBill Paul static void re_stop		(struct rl_softc *);
2901d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
291a94100faSBill Paul static int re_suspend		(device_t);
292a94100faSBill Paul static int re_resume		(device_t);
2936a087a87SPyun YongHyeon static int re_shutdown		(device_t);
294a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
295a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
296a94100faSBill Paul 
297a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
298a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
299ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
300a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
301a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
302a94100faSBill Paul 
303a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
304a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
305a94100faSBill Paul static void re_miibus_statchg	(device_t);
306a94100faSBill Paul 
30781eee0ebSPyun YongHyeon static void re_set_jumbo	(struct rl_softc *, int);
308ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
309a94100faSBill Paul static void re_reset		(struct rl_softc *);
3107467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
3117467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
3126830588dSPyun YongHyeon static void re_set_linkspeed	(struct rl_softc *);
313a94100faSBill Paul 
3147790c8c1SConrad Meyer DEBUGNET_DEFINE(re);
315306c97e2SMark Johnston 
316579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP	/* see ixgbe.c for details */
317579a6e3cSLuigi Rizzo #include <dev/netmap/if_re_netmap.h>
318847bf383SLuigi Rizzo MODULE_DEPEND(re, netmap, 1, 1, 1);
319579a6e3cSLuigi Rizzo #endif /* !DEV_NETMAP */
320579a6e3cSLuigi Rizzo 
321ed510fb0SBill Paul #ifdef RE_DIAG
322a94100faSBill Paul static int re_diag		(struct rl_softc *);
323ed510fb0SBill Paul #endif
324a94100faSBill Paul 
3250534aae0SPyun YongHyeon static void re_add_sysctls	(struct rl_softc *);
3260534aae0SPyun YongHyeon static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
327502be0f7SPyun YongHyeon static int sysctl_int_range	(SYSCTL_HANDLER_ARGS, int, int);
328502be0f7SPyun YongHyeon static int sysctl_hw_re_int_mod	(SYSCTL_HANDLER_ARGS);
3290534aae0SPyun YongHyeon 
330a94100faSBill Paul static device_method_t re_methods[] = {
331a94100faSBill Paul 	/* Device interface */
332a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
333a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
334a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
335a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
336a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
337a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
338a94100faSBill Paul 
339a94100faSBill Paul 	/* MII interface */
340a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
341a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
342a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
343a94100faSBill Paul 
3444b7ec270SMarius Strobl 	DEVMETHOD_END
345a94100faSBill Paul };
346a94100faSBill Paul 
347a94100faSBill Paul static driver_t re_driver = {
348a94100faSBill Paul 	"re",
349a94100faSBill Paul 	re_methods,
350a94100faSBill Paul 	sizeof(struct rl_softc)
351a94100faSBill Paul };
352a94100faSBill Paul 
353a94100faSBill Paul static devclass_t re_devclass;
354a94100faSBill Paul 
355a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
356a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
357a94100faSBill Paul 
358a94100faSBill Paul #define EE_SET(x)					\
359a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
360a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
361a94100faSBill Paul 
362a94100faSBill Paul #define EE_CLR(x)					\
363a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
364a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
365a94100faSBill Paul 
366a94100faSBill Paul /*
367a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
368a94100faSBill Paul  */
369a94100faSBill Paul static void
3707b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
371a94100faSBill Paul {
3720ce0868aSPyun YongHyeon 	int			d, i;
373a94100faSBill Paul 
374ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
375a94100faSBill Paul 
376a94100faSBill Paul 	/*
377a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
378a94100faSBill Paul 	 */
379ed510fb0SBill Paul 
380ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
381a94100faSBill Paul 		if (d & i) {
382a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
383a94100faSBill Paul 		} else {
384a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
385a94100faSBill Paul 		}
386a94100faSBill Paul 		DELAY(100);
387a94100faSBill Paul 		EE_SET(RL_EE_CLK);
388a94100faSBill Paul 		DELAY(150);
389a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
390a94100faSBill Paul 		DELAY(100);
391a94100faSBill Paul 	}
392a94100faSBill Paul }
393a94100faSBill Paul 
394a94100faSBill Paul /*
395a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
396a94100faSBill Paul  */
397a94100faSBill Paul static void
3987b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
399a94100faSBill Paul {
4000ce0868aSPyun YongHyeon 	int			i;
401a94100faSBill Paul 	u_int16_t		word = 0;
402a94100faSBill Paul 
403a94100faSBill Paul 	/*
404a94100faSBill Paul 	 * Send address of word we want to read.
405a94100faSBill Paul 	 */
406a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
407a94100faSBill Paul 
408a94100faSBill Paul 	/*
409a94100faSBill Paul 	 * Start reading bits from EEPROM.
410a94100faSBill Paul 	 */
411a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
412a94100faSBill Paul 		EE_SET(RL_EE_CLK);
413a94100faSBill Paul 		DELAY(100);
414a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
415a94100faSBill Paul 			word |= i;
416a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
417a94100faSBill Paul 		DELAY(100);
418a94100faSBill Paul 	}
419a94100faSBill Paul 
420a94100faSBill Paul 	*dest = word;
421a94100faSBill Paul }
422a94100faSBill Paul 
423a94100faSBill Paul /*
424a94100faSBill Paul  * Read a sequence of words from the EEPROM.
425a94100faSBill Paul  */
426a94100faSBill Paul static void
4277b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
428a94100faSBill Paul {
429a94100faSBill Paul 	int			i;
430a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
431a94100faSBill Paul 
432ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
433ed510fb0SBill Paul 
434ed510fb0SBill Paul         DELAY(100);
435ed510fb0SBill Paul 
436a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
437ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
438a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
439ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
440a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
441be099007SPyun YongHyeon                 *ptr = word;
442a94100faSBill Paul 	}
443ed510fb0SBill Paul 
444ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
445a94100faSBill Paul }
446a94100faSBill Paul 
447a94100faSBill Paul static int
4487b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
449a94100faSBill Paul {
450a94100faSBill Paul 	struct rl_softc		*sc;
451a94100faSBill Paul 	u_int32_t		rval;
452a94100faSBill Paul 	int			i;
453a94100faSBill Paul 
454a94100faSBill Paul 	sc = device_get_softc(dev);
455a94100faSBill Paul 
4569bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4579bac70b8SBill Paul 
4589bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4599bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4609bac70b8SBill Paul 		return (rval);
4619bac70b8SBill Paul 	}
4629bac70b8SBill Paul 
463a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
464a94100faSBill Paul 
46596b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
466a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
467a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
468a94100faSBill Paul 			break;
4692bc085c6SPyun YongHyeon 		DELAY(25);
470a94100faSBill Paul 	}
471a94100faSBill Paul 
47296b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4736b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
474a94100faSBill Paul 		return (0);
475a94100faSBill Paul 	}
476a94100faSBill Paul 
4772bc085c6SPyun YongHyeon 	/*
4782bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4792bc085c6SPyun YongHyeon 	 */
4802bc085c6SPyun YongHyeon 	DELAY(20);
4812bc085c6SPyun YongHyeon 
482a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
483a94100faSBill Paul }
484a94100faSBill Paul 
485a94100faSBill Paul static int
4867b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
487a94100faSBill Paul {
488a94100faSBill Paul 	struct rl_softc		*sc;
489a94100faSBill Paul 	u_int32_t		rval;
490a94100faSBill Paul 	int			i;
491a94100faSBill Paul 
492a94100faSBill Paul 	sc = device_get_softc(dev);
493a94100faSBill Paul 
494a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4959bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
496a94100faSBill Paul 
49796b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
498a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
499a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
500a94100faSBill Paul 			break;
5012bc085c6SPyun YongHyeon 		DELAY(25);
502a94100faSBill Paul 	}
503a94100faSBill Paul 
50496b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
5056b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
506a94100faSBill Paul 		return (0);
507a94100faSBill Paul 	}
508a94100faSBill Paul 
5092bc085c6SPyun YongHyeon 	/*
5102bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
5112bc085c6SPyun YongHyeon 	 */
5122bc085c6SPyun YongHyeon 	DELAY(20);
5132bc085c6SPyun YongHyeon 
514a94100faSBill Paul 	return (0);
515a94100faSBill Paul }
516a94100faSBill Paul 
517a94100faSBill Paul static int
5187b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
519a94100faSBill Paul {
520a94100faSBill Paul 	struct rl_softc		*sc;
521a94100faSBill Paul 	u_int16_t		rval = 0;
522a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
523a94100faSBill Paul 
524a94100faSBill Paul 	sc = device_get_softc(dev);
525a94100faSBill Paul 
526a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
527a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
528a94100faSBill Paul 		return (rval);
529a94100faSBill Paul 	}
530a94100faSBill Paul 
531a94100faSBill Paul 	switch (reg) {
532a94100faSBill Paul 	case MII_BMCR:
533a94100faSBill Paul 		re8139_reg = RL_BMCR;
534a94100faSBill Paul 		break;
535a94100faSBill Paul 	case MII_BMSR:
536a94100faSBill Paul 		re8139_reg = RL_BMSR;
537a94100faSBill Paul 		break;
538a94100faSBill Paul 	case MII_ANAR:
539a94100faSBill Paul 		re8139_reg = RL_ANAR;
540a94100faSBill Paul 		break;
541a94100faSBill Paul 	case MII_ANER:
542a94100faSBill Paul 		re8139_reg = RL_ANER;
543a94100faSBill Paul 		break;
544a94100faSBill Paul 	case MII_ANLPAR:
545a94100faSBill Paul 		re8139_reg = RL_LPAR;
546a94100faSBill Paul 		break;
547a94100faSBill Paul 	case MII_PHYIDR1:
548a94100faSBill Paul 	case MII_PHYIDR2:
549a94100faSBill Paul 		return (0);
550a94100faSBill Paul 	/*
551a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
552a94100faSBill Paul 	 * register. If we have a link partner which does not
553a94100faSBill Paul 	 * support NWAY, this is the register which will tell
554a94100faSBill Paul 	 * us the results of parallel detection.
555a94100faSBill Paul 	 */
556a94100faSBill Paul 	case RL_MEDIASTAT:
557a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
558a94100faSBill Paul 		return (rval);
559a94100faSBill Paul 	default:
5606b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
561a94100faSBill Paul 		return (0);
562a94100faSBill Paul 	}
563a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
564baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
565baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
566baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
567baa12772SPyun YongHyeon 	}
568a94100faSBill Paul 	return (rval);
569a94100faSBill Paul }
570a94100faSBill Paul 
571a94100faSBill Paul static int
5727b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
573a94100faSBill Paul {
574a94100faSBill Paul 	struct rl_softc		*sc;
575a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
576a94100faSBill Paul 	int			rval = 0;
577a94100faSBill Paul 
578a94100faSBill Paul 	sc = device_get_softc(dev);
579a94100faSBill Paul 
580a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
581a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
582a94100faSBill Paul 		return (rval);
583a94100faSBill Paul 	}
584a94100faSBill Paul 
585a94100faSBill Paul 	switch (reg) {
586a94100faSBill Paul 	case MII_BMCR:
587a94100faSBill Paul 		re8139_reg = RL_BMCR;
588baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
589baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
590baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
591baa12772SPyun YongHyeon 		}
592a94100faSBill Paul 		break;
593a94100faSBill Paul 	case MII_BMSR:
594a94100faSBill Paul 		re8139_reg = RL_BMSR;
595a94100faSBill Paul 		break;
596a94100faSBill Paul 	case MII_ANAR:
597a94100faSBill Paul 		re8139_reg = RL_ANAR;
598a94100faSBill Paul 		break;
599a94100faSBill Paul 	case MII_ANER:
600a94100faSBill Paul 		re8139_reg = RL_ANER;
601a94100faSBill Paul 		break;
602a94100faSBill Paul 	case MII_ANLPAR:
603a94100faSBill Paul 		re8139_reg = RL_LPAR;
604a94100faSBill Paul 		break;
605a94100faSBill Paul 	case MII_PHYIDR1:
606a94100faSBill Paul 	case MII_PHYIDR2:
607a94100faSBill Paul 		return (0);
608a94100faSBill Paul 		break;
609a94100faSBill Paul 	default:
6106b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
611a94100faSBill Paul 		return (0);
612a94100faSBill Paul 	}
613a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
614a94100faSBill Paul 	return (0);
615a94100faSBill Paul }
616a94100faSBill Paul 
617a94100faSBill Paul static void
6187b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
619a94100faSBill Paul {
620130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
621130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
622130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
623a11e2f18SBruce M Simpson 
624130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
625130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
626130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
627130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
628130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
629130b6dfbSPyun YongHyeon 		return;
630130b6dfbSPyun YongHyeon 
631130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
632130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
633130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
634130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
635130b6dfbSPyun YongHyeon 		case IFM_10_T:
636130b6dfbSPyun YongHyeon 		case IFM_100_TX:
637130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
638130b6dfbSPyun YongHyeon 			break;
639130b6dfbSPyun YongHyeon 		case IFM_1000_T:
640130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
641130b6dfbSPyun YongHyeon 				break;
642130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
643130b6dfbSPyun YongHyeon 			break;
644130b6dfbSPyun YongHyeon 		default:
645130b6dfbSPyun YongHyeon 			break;
646130b6dfbSPyun YongHyeon 		}
647130b6dfbSPyun YongHyeon 	}
648130b6dfbSPyun YongHyeon 	/*
64914013280SMarius Strobl 	 * RealTek controllers do not provide any interface to the RX/TX
65014013280SMarius Strobl 	 * MACs for resolved speed, duplex and flow-control parameters.
651130b6dfbSPyun YongHyeon 	 */
652a94100faSBill Paul }
653a94100faSBill Paul 
654307b050dSGleb Smirnoff static u_int
655307b050dSGleb Smirnoff re_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int cnt)
656307b050dSGleb Smirnoff {
657307b050dSGleb Smirnoff 	uint32_t h, *hashes = arg;
658307b050dSGleb Smirnoff 
659307b050dSGleb Smirnoff 	h = ether_crc32_be(LLADDR(sdl), ETHER_ADDR_LEN) >> 26;
660307b050dSGleb Smirnoff 	if (h < 32)
661307b050dSGleb Smirnoff 		hashes[0] |= (1 << h);
662307b050dSGleb Smirnoff 	else
663307b050dSGleb Smirnoff 		hashes[1] |= (1 << (h - 32));
664307b050dSGleb Smirnoff 
665307b050dSGleb Smirnoff 	return (1);
666307b050dSGleb Smirnoff }
667307b050dSGleb Smirnoff 
668a94100faSBill Paul /*
669ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
670a94100faSBill Paul  */
671a94100faSBill Paul static void
672ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
673a94100faSBill Paul {
674a94100faSBill Paul 	struct ifnet		*ifp;
675307b050dSGleb Smirnoff 	uint32_t		h, hashes[2] = { 0, 0 };
676307b050dSGleb Smirnoff 	uint32_t		rxfilt;
677a94100faSBill Paul 
67897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
67997b9d4baSJohn-Mark Gurney 
680fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
681a94100faSBill Paul 
682ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
683f1a5f291SMarius Strobl 	if ((sc->rl_flags & RL_FLAG_EARLYOFF) != 0)
684f1a5f291SMarius Strobl 		rxfilt |= RL_RXCFG_EARLYOFF;
68514013280SMarius Strobl 	else if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0)
686f1a5f291SMarius Strobl 		rxfilt |= RL_RXCFG_EARLYOFFV2;
687a94100faSBill Paul 
688ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6897c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6907c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
691a0637caaSPyun YongHyeon 		/*
692a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
693a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
694a0637caaSPyun YongHyeon 		 * promiscuous mode.
695a0637caaSPyun YongHyeon 		 */
696a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
697ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
698ff191365SJung-uk Kim 		goto done;
699a94100faSBill Paul 	}
700a94100faSBill Paul 
701307b050dSGleb Smirnoff 	if_foreach_llmaddr(ifp, re_hash_maddr, hashes);
702a94100faSBill Paul 
703ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
704bb7dfefbSBill Paul 		/*
705ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
706ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
707ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
708ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
709ff191365SJung-uk Kim 		 * devices.
710bb7dfefbSBill Paul 		 */
711aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
712ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
713ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
714ff191365SJung-uk Kim 			hashes[1] = h;
715ff191365SJung-uk Kim 		}
716ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
717ff191365SJung-uk Kim 	}
718ff191365SJung-uk Kim 
719b8333e45SPyun YongHyeon 	if  (sc->rl_hwrev->rl_rev == RL_HWREV_8168F) {
720b8333e45SPyun YongHyeon 		/* Disable multicast filtering due to silicon bug. */
721b8333e45SPyun YongHyeon 		hashes[0] = 0xffffffff;
722b8333e45SPyun YongHyeon 		hashes[1] = 0xffffffff;
723b8333e45SPyun YongHyeon 	}
724b8333e45SPyun YongHyeon 
725ff191365SJung-uk Kim done:
726a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
727a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
728ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
729bb7dfefbSBill Paul }
730a94100faSBill Paul 
731a94100faSBill Paul static void
7327b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
733a94100faSBill Paul {
7340ce0868aSPyun YongHyeon 	int			i;
735a94100faSBill Paul 
73697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
73797b9d4baSJohn-Mark Gurney 
738a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
739a94100faSBill Paul 
740a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
741a94100faSBill Paul 		DELAY(10);
742a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
743a94100faSBill Paul 			break;
744a94100faSBill Paul 	}
745a94100faSBill Paul 	if (i == RL_TIMEOUT)
7466b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
747a94100faSBill Paul 
748566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
749a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
75081eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
751566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
752a94100faSBill Paul }
753a94100faSBill Paul 
754ed510fb0SBill Paul #ifdef RE_DIAG
755ed510fb0SBill Paul 
756a94100faSBill Paul /*
757a94100faSBill Paul  * The following routine is designed to test for a defect on some
758a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
759a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
760a94100faSBill Paul  * should be pulled high. The result of this defect is that the
761a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
762a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
763a94100faSBill Paul  * because the 64-bit data lines aren't connected.
764a94100faSBill Paul  *
765a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
766a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
767a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
768a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
769a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
770a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
771a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
772a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
773a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
774a94100faSBill Paul  */
775a94100faSBill Paul 
776a94100faSBill Paul static int
7777b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
778a94100faSBill Paul {
779fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
780a94100faSBill Paul 	struct mbuf		*m0;
781a94100faSBill Paul 	struct ether_header	*eh;
782a94100faSBill Paul 	struct rl_desc		*cur_rx;
783a94100faSBill Paul 	u_int16_t		status;
784a94100faSBill Paul 	u_int32_t		rxstat;
785ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
786a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
787a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
788a94100faSBill Paul 
789a94100faSBill Paul 	/* Allocate a single mbuf */
790c6499eccSGleb Smirnoff 	MGETHDR(m0, M_NOWAIT, MT_DATA);
791a94100faSBill Paul 	if (m0 == NULL)
792a94100faSBill Paul 		return (ENOBUFS);
793a94100faSBill Paul 
79497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
79597b9d4baSJohn-Mark Gurney 
796a94100faSBill Paul 	/*
797a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
798a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
799a94100faSBill Paul 	 * following special functions:
800a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
801a94100faSBill Paul 	 * - Enables digital loopback mode
802a94100faSBill Paul 	 * - Leaves interrupts turned off
803a94100faSBill Paul 	 */
804a94100faSBill Paul 
805a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
806a94100faSBill Paul 	sc->rl_testmode = 1;
8078476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
80897b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
809351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
810ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
811ed510fb0SBill Paul 		phyaddr = 1;
812ed510fb0SBill Paul 	else
813ed510fb0SBill Paul 		phyaddr = 0;
814ed510fb0SBill Paul 
815ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
816ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
817ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
818ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
819ed510fb0SBill Paul 			break;
820ed510fb0SBill Paul 	}
821ed510fb0SBill Paul 
822ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
823ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
824ed510fb0SBill Paul 
825804af9a1SBill Paul 	DELAY(100000);
826a94100faSBill Paul 
827a94100faSBill Paul 	/* Put some data in the mbuf */
828a94100faSBill Paul 
829a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
830a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
831a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
832a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
833a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
834a94100faSBill Paul 
8357cae6651SBill Paul 	/*
8367cae6651SBill Paul 	 * Queue the packet, start transmission.
8377cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
8387cae6651SBill Paul 	 */
839a94100faSBill Paul 
840abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
84197b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
84252732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8437cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
84497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
845a94100faSBill Paul 	m0 = NULL;
846a94100faSBill Paul 
847a94100faSBill Paul 	/* Wait for it to propagate through the chip */
848a94100faSBill Paul 
849abc8ff44SBill Paul 	DELAY(100000);
850a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
851a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
852ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
853abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
854abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
855a94100faSBill Paul 			break;
856a94100faSBill Paul 		DELAY(10);
857a94100faSBill Paul 	}
858a94100faSBill Paul 
859a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8606b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8616b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8626b9f5c94SGleb Smirnoff 		    " loopback mode\n");
863a94100faSBill Paul 		error = EIO;
864a94100faSBill Paul 		goto done;
865a94100faSBill Paul 	}
866a94100faSBill Paul 
867a94100faSBill Paul 	/*
868a94100faSBill Paul 	 * The packet should have been dumped into the first
869a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
870a94100faSBill Paul 	 */
871a94100faSBill Paul 
872a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
873a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
874a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
875d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
876d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
877d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
878d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
879d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
880a94100faSBill Paul 
881d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
882d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
883a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
884a94100faSBill Paul 
885a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
886a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
887a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
888a94100faSBill Paul 
889a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8906b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8916b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
892a94100faSBill Paul 		error = EIO;
893a94100faSBill Paul 		goto done;
894a94100faSBill Paul 	}
895a94100faSBill Paul 
896a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
897a94100faSBill Paul 
898a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
899a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
900a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
9016b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
9026b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
903a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
9046b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
905a94100faSBill Paul 		    eh->ether_dhost, ":", eh->ether_shost, ":",
906a94100faSBill Paul 		    ntohs(eh->ether_type));
9076b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
9086b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
9096b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
9106b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
9116b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
9126b9f5c94SGleb Smirnoff 		    "details.\n");
913a94100faSBill Paul 		error = EIO;
914a94100faSBill Paul 	}
915a94100faSBill Paul 
916a94100faSBill Paul done:
917a94100faSBill Paul 	/* Turn interface off, release resources */
918a94100faSBill Paul 
919a94100faSBill Paul 	sc->rl_testmode = 0;
920351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
921a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
922a94100faSBill Paul 	re_stop(sc);
923a94100faSBill Paul 	if (m0 != NULL)
924a94100faSBill Paul 		m_freem(m0);
925a94100faSBill Paul 
92697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
92797b9d4baSJohn-Mark Gurney 
928a94100faSBill Paul 	return (error);
929a94100faSBill Paul }
930a94100faSBill Paul 
931ed510fb0SBill Paul #endif
932ed510fb0SBill Paul 
933a94100faSBill Paul /*
934a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
935a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
936a94100faSBill Paul  */
937a94100faSBill Paul static int
9387b5ffebfSPyun YongHyeon re_probe(device_t dev)
939a94100faSBill Paul {
940b3030306SMarius Strobl 	const struct rl_type	*t;
941dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
942dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
943dfdb409eSPyun YongHyeon 	int			i;
944a94100faSBill Paul 
945dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
946dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
947dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
948dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
949a94100faSBill Paul 
950dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
951dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
95226390635SJohn Baldwin 			/*
95326390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
954dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
95526390635SJohn Baldwin 			 */
956a94100faSBill Paul 			return (ENXIO);
957a94100faSBill Paul 		}
958dfdb409eSPyun YongHyeon 	}
959dfdb409eSPyun YongHyeon 
960dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
961dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
962dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
963dfdb409eSPyun YongHyeon 			return (ENXIO);
964dfdb409eSPyun YongHyeon 		}
965dfdb409eSPyun YongHyeon 	}
966dfdb409eSPyun YongHyeon 
967dfdb409eSPyun YongHyeon 	t = re_devs;
96873a1170aSPedro F. Giffuni 	for (i = 0; i < nitems(re_devs); i++, t++) {
969dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
970a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
971d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
972a94100faSBill Paul 		}
973a94100faSBill Paul 	}
974a94100faSBill Paul 
975a94100faSBill Paul 	return (ENXIO);
976a94100faSBill Paul }
977a94100faSBill Paul 
978a94100faSBill Paul /*
979a94100faSBill Paul  * Map a single buffer address.
980a94100faSBill Paul  */
981a94100faSBill Paul 
982a94100faSBill Paul static void
9837b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
984a94100faSBill Paul {
9858fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
986a94100faSBill Paul 
987a94100faSBill Paul 	if (error)
988a94100faSBill Paul 		return;
989a94100faSBill Paul 
990a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
991a94100faSBill Paul 	addr = arg;
992a94100faSBill Paul 	*addr = segs->ds_addr;
993a94100faSBill Paul }
994a94100faSBill Paul 
995a94100faSBill Paul static int
9967b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
997a94100faSBill Paul {
99866366ca4SPyun YongHyeon 	bus_addr_t		lowaddr;
999d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
1000a94100faSBill Paul 	int			error;
1001a94100faSBill Paul 	int			i;
1002a94100faSBill Paul 
1003d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
1004d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
1005d65abd66SPyun YongHyeon 
1006d65abd66SPyun YongHyeon 	/*
1007d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
1008ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
1009ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
1010ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
1011ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
101266366ca4SPyun YongHyeon 	 * may not have the limitation.
1013d65abd66SPyun YongHyeon 	 */
101466366ca4SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
101566366ca4SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
101666366ca4SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
1017d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
101866366ca4SPyun YongHyeon 	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
1019d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
1020d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
1021d65abd66SPyun YongHyeon 	if (error) {
1022d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
1023d65abd66SPyun YongHyeon 		return (error);
1024d65abd66SPyun YongHyeon 	}
1025d65abd66SPyun YongHyeon 
1026d65abd66SPyun YongHyeon 	/*
1027d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
1028d65abd66SPyun YongHyeon 	 */
1029d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
1030d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
1031d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
1032d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
1033d65abd66SPyun YongHyeon 	if (error) {
1034d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
1035d65abd66SPyun YongHyeon 		return (error);
1036d65abd66SPyun YongHyeon 	}
1037d65abd66SPyun YongHyeon 
1038a94100faSBill Paul 	/*
1039a94100faSBill Paul 	 * Allocate map for RX mbufs.
1040a94100faSBill Paul 	 */
1041d65abd66SPyun YongHyeon 
104281eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
104381eee0ebSPyun YongHyeon 		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
104481eee0ebSPyun YongHyeon 		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
104581eee0ebSPyun YongHyeon 		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
104681eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_mtag);
104781eee0ebSPyun YongHyeon 		if (error) {
104881eee0ebSPyun YongHyeon 			device_printf(dev,
104981eee0ebSPyun YongHyeon 			    "could not allocate jumbo RX DMA tag\n");
105081eee0ebSPyun YongHyeon 			return (error);
105181eee0ebSPyun YongHyeon 		}
105281eee0ebSPyun YongHyeon 	}
1053d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1054d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1055d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1056a94100faSBill Paul 	if (error) {
1057d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1058d65abd66SPyun YongHyeon 		return (error);
1059a94100faSBill Paul 	}
1060a94100faSBill Paul 
1061a94100faSBill Paul 	/*
1062a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1063a94100faSBill Paul 	 */
1064a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1065a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1066d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1067a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1068a94100faSBill Paul 	if (error) {
1069d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1070d65abd66SPyun YongHyeon 		return (error);
1071a94100faSBill Paul 	}
1072a94100faSBill Paul 
1073a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1074a94100faSBill Paul 
1075a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1076d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1077d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1078a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1079d65abd66SPyun YongHyeon 	if (error) {
1080d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1081d65abd66SPyun YongHyeon 		return (error);
1082d65abd66SPyun YongHyeon 	}
1083a94100faSBill Paul 
1084a94100faSBill Paul 	/* Load the map for the TX ring. */
1085a94100faSBill Paul 
1086d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1087a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1088a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1089d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1090a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1091d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1092d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1093d65abd66SPyun YongHyeon 		return (ENOMEM);
1094d65abd66SPyun YongHyeon 	}
1095a94100faSBill Paul 
1096a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1097a94100faSBill Paul 
1098d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1099d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1100d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1101a94100faSBill Paul 		if (error) {
1102d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1103d65abd66SPyun YongHyeon 			return (error);
1104a94100faSBill Paul 		}
1105a94100faSBill Paul 	}
1106a94100faSBill Paul 
1107a94100faSBill Paul 	/*
1108a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1109a94100faSBill Paul 	 */
1110a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1111a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1112d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1113a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1114a94100faSBill Paul 	if (error) {
1115d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1116d65abd66SPyun YongHyeon 		return (error);
1117a94100faSBill Paul 	}
1118a94100faSBill Paul 
1119a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1120a94100faSBill Paul 
1121a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1122d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1123d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1124a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1125d65abd66SPyun YongHyeon 	if (error) {
1126d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1127d65abd66SPyun YongHyeon 		return (error);
1128d65abd66SPyun YongHyeon 	}
1129a94100faSBill Paul 
1130a94100faSBill Paul 	/* Load the map for the RX ring. */
1131a94100faSBill Paul 
1132d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1133a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1134a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1135d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1136a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1137d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1138d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1139d65abd66SPyun YongHyeon 		return (ENOMEM);
1140d65abd66SPyun YongHyeon 	}
1141a94100faSBill Paul 
1142a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1143a94100faSBill Paul 
114481eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
114581eee0ebSPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
114681eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_sparemap);
114781eee0ebSPyun YongHyeon 		if (error) {
114881eee0ebSPyun YongHyeon 			device_printf(dev,
114981eee0ebSPyun YongHyeon 			    "could not create spare DMA map for jumbo RX\n");
115081eee0ebSPyun YongHyeon 			return (error);
115181eee0ebSPyun YongHyeon 		}
115281eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
115381eee0ebSPyun YongHyeon 			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
115481eee0ebSPyun YongHyeon 			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
115581eee0ebSPyun YongHyeon 			if (error) {
115681eee0ebSPyun YongHyeon 				device_printf(dev,
115781eee0ebSPyun YongHyeon 				    "could not create DMA map for jumbo RX\n");
115881eee0ebSPyun YongHyeon 				return (error);
115981eee0ebSPyun YongHyeon 			}
116081eee0ebSPyun YongHyeon 		}
116181eee0ebSPyun YongHyeon 	}
1162d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1163d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1164a94100faSBill Paul 	if (error) {
1165d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1166d65abd66SPyun YongHyeon 		return (error);
1167d65abd66SPyun YongHyeon 	}
1168d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1169d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1170d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1171d65abd66SPyun YongHyeon 		if (error) {
1172d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1173d65abd66SPyun YongHyeon 			return (error);
1174a94100faSBill Paul 		}
1175a94100faSBill Paul 	}
1176a94100faSBill Paul 
11770534aae0SPyun YongHyeon 	/* Create DMA map for statistics. */
11780534aae0SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
11790534aae0SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
11800534aae0SPyun YongHyeon 	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
11810534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_stag);
11820534aae0SPyun YongHyeon 	if (error) {
11830534aae0SPyun YongHyeon 		device_printf(dev, "could not create statistics DMA tag\n");
11840534aae0SPyun YongHyeon 		return (error);
11850534aae0SPyun YongHyeon 	}
11860534aae0SPyun YongHyeon 	/* Allocate DMA'able memory for statistics. */
11870534aae0SPyun YongHyeon 	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
11880534aae0SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_stats,
11890534aae0SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
11900534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_smap);
11910534aae0SPyun YongHyeon 	if (error) {
11920534aae0SPyun YongHyeon 		device_printf(dev,
11930534aae0SPyun YongHyeon 		    "could not allocate statistics DMA memory\n");
11940534aae0SPyun YongHyeon 		return (error);
11950534aae0SPyun YongHyeon 	}
11960534aae0SPyun YongHyeon 	/* Load the map for statistics. */
11970534aae0SPyun YongHyeon 	sc->rl_ldata.rl_stats_addr = 0;
11980534aae0SPyun YongHyeon 	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
11990534aae0SPyun YongHyeon 	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
12000534aae0SPyun YongHyeon 	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
12010534aae0SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
12020534aae0SPyun YongHyeon 		device_printf(dev, "could not load statistics DMA memory\n");
12030534aae0SPyun YongHyeon 		return (ENOMEM);
12040534aae0SPyun YongHyeon 	}
12050534aae0SPyun YongHyeon 
1206a94100faSBill Paul 	return (0);
1207a94100faSBill Paul }
1208a94100faSBill Paul 
1209a94100faSBill Paul /*
1210a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1211a94100faSBill Paul  * setup and ethernet/BPF attach.
1212a94100faSBill Paul  */
1213a94100faSBill Paul static int
12147b5ffebfSPyun YongHyeon re_attach(device_t dev)
1215a94100faSBill Paul {
1216a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1217be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1218a94100faSBill Paul 	struct rl_softc		*sc;
1219a94100faSBill Paul 	struct ifnet		*ifp;
1220b3030306SMarius Strobl 	const struct rl_hwrev	*hw_rev;
122114013280SMarius Strobl 	int			capmask, error = 0, hwrev, i, msic, msixc,
122214013280SMarius Strobl 				phy, reg, rid;
1223017f1c8dSPyun YongHyeon 	u_int32_t		cap, ctl;
1224ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
122503ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1226a94100faSBill Paul 
1227a94100faSBill Paul 	sc = device_get_softc(dev);
1228ed510fb0SBill Paul 	sc->rl_dev = dev;
1229a94100faSBill Paul 
1230a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
123197b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1232d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1233d1754a9bSJohn Baldwin 
1234a94100faSBill Paul 	/*
1235a94100faSBill Paul 	 * Map control/status registers.
1236a94100faSBill Paul 	 */
1237a94100faSBill Paul 	pci_enable_busmaster(dev);
1238a94100faSBill Paul 
1239ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
12402c21710bSPyun YongHyeon 	/*
12412c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
12422c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
12432c21710bSPyun YongHyeon 	 * is used always activate io mapping.
12442c21710bSPyun YongHyeon 	 */
12452c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
12462c21710bSPyun YongHyeon 		prefer_iomap = 1;
12472c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1248ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1249ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1250ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1251ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1252ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
12532c21710bSPyun YongHyeon 	} else {
12542c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
12552c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
12562c21710bSPyun YongHyeon 	}
1257ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1258ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
12592c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1260ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1261ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1262ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1263ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
12642c21710bSPyun YongHyeon 	}
1265ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1266d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1267a94100faSBill Paul 		error = ENXIO;
1268a94100faSBill Paul 		goto fail;
1269a94100faSBill Paul 	}
1270a94100faSBill Paul 
1271a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1272a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1273a94100faSBill Paul 
12745774c5ffSPyun YongHyeon 	msic = pci_msi_count(dev);
12754a58fd45SPyun YongHyeon 	msixc = pci_msix_count(dev);
1276017f1c8dSPyun YongHyeon 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
12774a58fd45SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
1278017f1c8dSPyun YongHyeon 		sc->rl_expcap = reg;
1279017f1c8dSPyun YongHyeon 	}
12804a58fd45SPyun YongHyeon 	if (bootverbose) {
12815774c5ffSPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
12824a58fd45SPyun YongHyeon 		device_printf(dev, "MSI-X count : %d\n", msixc);
12835774c5ffSPyun YongHyeon 	}
12844a58fd45SPyun YongHyeon 	if (msix_disable > 0)
12854a58fd45SPyun YongHyeon 		msixc = 0;
12864a58fd45SPyun YongHyeon 	if (msi_disable > 0)
12874a58fd45SPyun YongHyeon 		msic = 0;
12884a58fd45SPyun YongHyeon 	/* Prefer MSI-X to MSI. */
12894a58fd45SPyun YongHyeon 	if (msixc > 0) {
1290f1a5f291SMarius Strobl 		msixc = RL_MSI_MESSAGES;
12914a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
12924a58fd45SPyun YongHyeon 		sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
12934a58fd45SPyun YongHyeon 		    &rid, RF_ACTIVE);
12944a58fd45SPyun YongHyeon 		if (sc->rl_res_pba == NULL) {
12954a58fd45SPyun YongHyeon 			device_printf(sc->rl_dev,
12964a58fd45SPyun YongHyeon 			    "could not allocate MSI-X PBA resource\n");
12974a58fd45SPyun YongHyeon 		}
12984a58fd45SPyun YongHyeon 		if (sc->rl_res_pba != NULL &&
12994a58fd45SPyun YongHyeon 		    pci_alloc_msix(dev, &msixc) == 0) {
1300f1a5f291SMarius Strobl 			if (msixc == RL_MSI_MESSAGES) {
13014a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI-X message\n",
13024a58fd45SPyun YongHyeon 				    msixc);
13034a58fd45SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSIX;
13044a58fd45SPyun YongHyeon 			} else
13054a58fd45SPyun YongHyeon 				pci_release_msi(dev);
13064a58fd45SPyun YongHyeon 		}
13074a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
13084a58fd45SPyun YongHyeon 			if (sc->rl_res_pba != NULL)
13094a58fd45SPyun YongHyeon 				bus_release_resource(dev, SYS_RES_MEMORY, rid,
13104a58fd45SPyun YongHyeon 				    sc->rl_res_pba);
13114a58fd45SPyun YongHyeon 			sc->rl_res_pba = NULL;
13124a58fd45SPyun YongHyeon 			msixc = 0;
13134a58fd45SPyun YongHyeon 		}
13144a58fd45SPyun YongHyeon 	}
13154a58fd45SPyun YongHyeon 	/* Prefer MSI to INTx. */
13164a58fd45SPyun YongHyeon 	if (msixc == 0 && msic > 0) {
1317f1a5f291SMarius Strobl 		msic = RL_MSI_MESSAGES;
13185774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
13195774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
13204a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI message\n",
13215774c5ffSPyun YongHyeon 				    msic);
1322351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1323339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1324339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1325339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1326339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1327339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1328f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13295774c5ffSPyun YongHyeon 			} else
13305774c5ffSPyun YongHyeon 				pci_release_msi(dev);
13315774c5ffSPyun YongHyeon 		}
13324a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSI) == 0)
13334a58fd45SPyun YongHyeon 			msic = 0;
13345774c5ffSPyun YongHyeon 	}
1335a94100faSBill Paul 
13365774c5ffSPyun YongHyeon 	/* Allocate interrupt */
13374a58fd45SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
13385774c5ffSPyun YongHyeon 		rid = 0;
13395774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
13405774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
13415774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
13425774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1343a94100faSBill Paul 			error = ENXIO;
1344a94100faSBill Paul 			goto fail;
1345a94100faSBill Paul 		}
13465774c5ffSPyun YongHyeon 	} else {
13475774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
13485774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
13495774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
13505774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
13515774c5ffSPyun YongHyeon 				device_printf(dev,
13522df05392SSergey Kandaurov 				    "couldn't allocate IRQ resources for "
13535774c5ffSPyun YongHyeon 				    "message %d\n", rid);
13545774c5ffSPyun YongHyeon 				error = ENXIO;
13555774c5ffSPyun YongHyeon 				goto fail;
13565774c5ffSPyun YongHyeon 			}
13575774c5ffSPyun YongHyeon 		}
13585774c5ffSPyun YongHyeon 	}
1359a94100faSBill Paul 
13604d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
13614d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
13624d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
13634d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
13644d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
13654d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
13664d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
13674d2bf239SPyun YongHyeon 		}
13684d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13694d2bf239SPyun YongHyeon 	}
13704d2bf239SPyun YongHyeon 
13713d810282SKevin Lo 	/* Disable ASPM L0S/L1 and CLKREQ. */
1372017f1c8dSPyun YongHyeon 	if (sc->rl_expcap != 0) {
1373017f1c8dSPyun YongHyeon 		cap = pci_read_config(dev, sc->rl_expcap +
1374389c8bd5SGavin Atkinson 		    PCIER_LINK_CAP, 2);
1375389c8bd5SGavin Atkinson 		if ((cap & PCIEM_LINK_CAP_ASPM) != 0) {
1376017f1c8dSPyun YongHyeon 			ctl = pci_read_config(dev, sc->rl_expcap +
1377389c8bd5SGavin Atkinson 			    PCIER_LINK_CTL, 2);
13783d810282SKevin Lo 			if ((ctl & (PCIEM_LINK_CTL_ECPM |
13793d810282SKevin Lo 			    PCIEM_LINK_CTL_ASPMC))!= 0) {
13803d810282SKevin Lo 				ctl &= ~(PCIEM_LINK_CTL_ECPM |
13813d810282SKevin Lo 				    PCIEM_LINK_CTL_ASPMC);
1382017f1c8dSPyun YongHyeon 				pci_write_config(dev, sc->rl_expcap +
1383389c8bd5SGavin Atkinson 				    PCIER_LINK_CTL, ctl, 2);
1384017f1c8dSPyun YongHyeon 				device_printf(dev, "ASPM disabled\n");
1385017f1c8dSPyun YongHyeon 			}
1386017f1c8dSPyun YongHyeon 		} else
1387017f1c8dSPyun YongHyeon 			device_printf(dev, "no ASPM capability\n");
1388017f1c8dSPyun YongHyeon 	}
1389017f1c8dSPyun YongHyeon 
1390abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1391a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1392566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1393566ca8caSJung-uk Kim 	case 0x00000000:
1394566ca8caSJung-uk Kim 	case 0x10000000:
1395566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1396566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1397566ca8caSJung-uk Kim 		break;
1398566ca8caSJung-uk Kim 	default:
1399a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1400fd3ae0f5SPyun YongHyeon 		sc->rl_macrev = hwrev & 0x00700000;
1401a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1402566ca8caSJung-uk Kim 		break;
1403566ca8caSJung-uk Kim 	}
1404fd3ae0f5SPyun YongHyeon 	device_printf(dev, "MAC rev. 0x%08x\n", sc->rl_macrev);
1405abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1406abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1407abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
140881eee0ebSPyun YongHyeon 			sc->rl_hwrev = hw_rev;
1409abc8ff44SBill Paul 			break;
1410abc8ff44SBill Paul 		}
1411abc8ff44SBill Paul 		hw_rev++;
1412abc8ff44SBill Paul 	}
1413d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1414a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1415d65abd66SPyun YongHyeon 		error = ENXIO;
1416d65abd66SPyun YongHyeon 		goto fail;
1417d65abd66SPyun YongHyeon 	}
1418abc8ff44SBill Paul 
1419351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1420351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
142181eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1422351a76f9SPyun YongHyeon 		break;
1423351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1424351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
142581eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1426351a76f9SPyun YongHyeon 		break;
1427b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1428b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
14293d22427cSTai-hwa Liang 	case RL_HWREV_8102EL_SPIN1:
143081eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
143181eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
143281eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1433b1d62f0fSPyun YongHyeon 		break;
14348281a098SPyun YongHyeon 	case RL_HWREV_8103E:
143581eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
143681eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
143781eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
14388281a098SPyun YongHyeon 		break;
143939e69201SPyun YongHyeon 	case RL_HWREV_8401E:
144054899a96SPyun YongHyeon 	case RL_HWREV_8105E:
14416b0a8e04SPyun YongHyeon 	case RL_HWREV_8105E_SPIN1:
1442214c71f6SPyun YongHyeon 	case RL_HWREV_8106E:
144354899a96SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
144454899a96SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
144554899a96SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
144654899a96SPyun YongHyeon 		break;
1447eef0e496SPyun YongHyeon 	case RL_HWREV_8402:
1448eef0e496SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1449eef0e496SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1450eef0e496SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD |
1451eef0e496SPyun YongHyeon 		    RL_FLAG_CMDSTOP_WAIT_TXQ;
1452eef0e496SPyun YongHyeon 		break;
1453ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN1:
1454ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN2:
1455886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1456886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1457ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN3:
1458aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1459deb5c680SPyun YongHyeon 		break;
1460deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
146161f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
146261f45a72SPyun YongHyeon 		/* FALLTHROUGH */
146361f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
1464fd3ae0f5SPyun YongHyeon 		if (sc->rl_macrev == 0x00200000)
146561f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
146661f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1467deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
1468aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1469f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
14706830588dSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1471351a76f9SPyun YongHyeon 		break;
1472df2dc2b3SPyun YongHyeon 	case RL_HWREV_8168D:
1473df2dc2b3SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1474df2dc2b3SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1475df2dc2b3SPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1476df2dc2b3SPyun YongHyeon 		    RL_FLAG_WOL_MANLINK;
1477df2dc2b3SPyun YongHyeon 		break;
1478eef0e496SPyun YongHyeon 	case RL_HWREV_8168DP:
1479eef0e496SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1480eef0e496SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_AUTOPAD |
14816830588dSPyun YongHyeon 		    RL_FLAG_JUMBOV2 | RL_FLAG_WAIT_TXPOLL | RL_FLAG_WOL_MANLINK;
1482eef0e496SPyun YongHyeon 		break;
1483d0c45156SPyun YongHyeon 	case RL_HWREV_8168E:
1484d0c45156SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1485d0c45156SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
14866830588dSPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
14876830588dSPyun YongHyeon 		    RL_FLAG_WOL_MANLINK;
1488d0c45156SPyun YongHyeon 		break;
1489f0431c5bSPyun YongHyeon 	case RL_HWREV_8168E_VL:
1490d467ffaaSPyun YongHyeon 	case RL_HWREV_8168F:
1491f1a5f291SMarius Strobl 		sc->rl_flags |= RL_FLAG_EARLYOFF;
1492f1a5f291SMarius Strobl 		/* FALLTHROUGH */
1493d56f7f52SPyun YongHyeon 	case RL_HWREV_8411:
1494f0431c5bSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1495f0431c5bSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1496eef0e496SPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
14976830588dSPyun YongHyeon 		    RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK;
1498f0431c5bSPyun YongHyeon 		break;
1499f1a5f291SMarius Strobl 	case RL_HWREV_8168EP:
1500f1a5f291SMarius Strobl 	case RL_HWREV_8168G:
1501f1a5f291SMarius Strobl 	case RL_HWREV_8411B:
1502f1a5f291SMarius Strobl 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1503f1a5f291SMarius Strobl 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1504f1a5f291SMarius Strobl 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2 |
1505f1a5f291SMarius Strobl 		    RL_FLAG_CMDSTOP_WAIT_TXQ | RL_FLAG_WOL_MANLINK |
150614013280SMarius Strobl 		    RL_FLAG_8168G_PLUS;
1507f1a5f291SMarius Strobl 		break;
1508ab9f923eSPyun YongHyeon 	case RL_HWREV_8168GU:
150914013280SMarius Strobl 	case RL_HWREV_8168H:
1510ab9f923eSPyun YongHyeon 		if (pci_get_device(dev) == RT_DEVICEID_8101E) {
151114013280SMarius Strobl 			/* RTL8106E(US), RTL8107E */
1512ab9f923eSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_FASTETHER;
1513ab9f923eSPyun YongHyeon 		} else
1514ab9f923eSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_JUMBOV2 | RL_FLAG_WOL_MANLINK;
1515ab9f923eSPyun YongHyeon 
1516ab9f923eSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1517ab9f923eSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
1518f1a5f291SMarius Strobl 		    RL_FLAG_AUTOPAD | RL_FLAG_CMDSTOP_WAIT_TXQ |
151914013280SMarius Strobl 		    RL_FLAG_8168G_PLUS;
1520ab9f923eSPyun YongHyeon 		break;
1521566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1522566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1523566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1524566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1525566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1526566ca8caSJung-uk Kim 		/* FALLTHROUGH */
15270596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
15280596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1529566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1530566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1531351a76f9SPyun YongHyeon 		break;
1532351a76f9SPyun YongHyeon 	default:
1533351a76f9SPyun YongHyeon 		break;
1534351a76f9SPyun YongHyeon 	}
1535351a76f9SPyun YongHyeon 
1536e7e7593cSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8139CPLUS) {
1537e7e7593cSPyun YongHyeon 		sc->rl_cfg0 = RL_8139_CFG0;
1538e7e7593cSPyun YongHyeon 		sc->rl_cfg1 = RL_8139_CFG1;
1539e7e7593cSPyun YongHyeon 		sc->rl_cfg2 = 0;
1540e7e7593cSPyun YongHyeon 		sc->rl_cfg3 = RL_8139_CFG3;
1541e7e7593cSPyun YongHyeon 		sc->rl_cfg4 = RL_8139_CFG4;
1542e7e7593cSPyun YongHyeon 		sc->rl_cfg5 = RL_8139_CFG5;
1543e7e7593cSPyun YongHyeon 	} else {
1544e7e7593cSPyun YongHyeon 		sc->rl_cfg0 = RL_CFG0;
1545e7e7593cSPyun YongHyeon 		sc->rl_cfg1 = RL_CFG1;
1546e7e7593cSPyun YongHyeon 		sc->rl_cfg2 = RL_CFG2;
1547e7e7593cSPyun YongHyeon 		sc->rl_cfg3 = RL_CFG3;
1548e7e7593cSPyun YongHyeon 		sc->rl_cfg4 = RL_CFG4;
1549e7e7593cSPyun YongHyeon 		sc->rl_cfg5 = RL_CFG5;
1550e7e7593cSPyun YongHyeon 	}
1551e7e7593cSPyun YongHyeon 
155293252626SPyun YongHyeon 	/* Reset the adapter. */
155393252626SPyun YongHyeon 	RL_LOCK(sc);
155493252626SPyun YongHyeon 	re_reset(sc);
155593252626SPyun YongHyeon 	RL_UNLOCK(sc);
155693252626SPyun YongHyeon 
1557deb5c680SPyun YongHyeon 	/* Enable PME. */
1558deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1559e7e7593cSPyun YongHyeon 	cfg = CSR_READ_1(sc, sc->rl_cfg1);
1560deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1561e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, cfg);
1562e7e7593cSPyun YongHyeon 	cfg = CSR_READ_1(sc, sc->rl_cfg5);
1563deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1564e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, cfg);
1565deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1566deb5c680SPyun YongHyeon 
1567deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1568deb5c680SPyun YongHyeon 		/*
1569deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1570deb5c680SPyun YongHyeon 		 * address from EEPROM.
1571deb5c680SPyun YongHyeon 		 */
1572deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1573deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1574deb5c680SPyun YongHyeon 	} else {
1575141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1576ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1577a94100faSBill Paul 		if (re_did != 0x8129)
1578141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1579a94100faSBill Paul 
1580a94100faSBill Paul 		/*
1581a94100faSBill Paul 		 * Get station address from the EEPROM.
1582a94100faSBill Paul 		 */
1583ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1584be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1585be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1586de8925a2SKevin Lo 		bcopy(as, eaddr, ETHER_ADDR_LEN);
1587deb5c680SPyun YongHyeon 	}
1588ed510fb0SBill Paul 
1589ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1590d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1591ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1592ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1593d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1594d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1595ed510fb0SBill Paul 	} else {
1596d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1597ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1598ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1599d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1600d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1601abc8ff44SBill Paul 	}
16029bac70b8SBill Paul 
1603a94100faSBill Paul 	error = re_allocmem(dev, sc);
1604a94100faSBill Paul 	if (error)
1605a94100faSBill Paul 		goto fail;
16060534aae0SPyun YongHyeon 	re_add_sysctls(sc);
1607a94100faSBill Paul 
1608cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1609cd036ec1SBrooks Davis 	if (ifp == NULL) {
1610d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1611cd036ec1SBrooks Davis 		error = ENOSPC;
1612cd036ec1SBrooks Davis 		goto fail;
1613cd036ec1SBrooks Davis 	}
1614cd036ec1SBrooks Davis 
161561f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
161661f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
161761f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
161861f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
161961f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
162061f45a72SPyun YongHyeon 		else
162161f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
162261f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
162361f45a72SPyun YongHyeon 	}
162461f45a72SPyun YongHyeon 
1625351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
162639e69201SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) {
1627d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
162839e69201SPyun YongHyeon 		if (hw_rev->rl_rev == RL_HWREV_8401E)
162939e69201SPyun YongHyeon 			CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08);
163039e69201SPyun YongHyeon 	}
1631351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1632351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1633351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1634351a76f9SPyun YongHyeon 	}
1635351a76f9SPyun YongHyeon 
1636a94100faSBill Paul 	ifp->if_softc = sc;
16379bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1638a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1639a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1640a94100faSBill Paul 	ifp->if_start = re_start;
1641bc2a1002SPyun YongHyeon 	/*
1642bc2a1002SPyun YongHyeon 	 * RTL8168/8111C generates wrong IP checksummed frame if the
164374a03446SPyun YongHyeon 	 * packet has IP options so disable TX checksum offloading.
1644bc2a1002SPyun YongHyeon 	 */
1645bc2a1002SPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C ||
16463c2a957dSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2 ||
164774a03446SPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168CP) {
164874a03446SPyun YongHyeon 		ifp->if_hwassist = 0;
164974a03446SPyun YongHyeon 		ifp->if_capabilities = IFCAP_RXCSUM | IFCAP_TSO4;
165074a03446SPyun YongHyeon 	} else {
1651bc2a1002SPyun YongHyeon 		ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
1652d6d7d923SPyun YongHyeon 		ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
165374a03446SPyun YongHyeon 	}
165474a03446SPyun YongHyeon 	ifp->if_hwassist |= CSUM_TSO;
1655498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1656a94100faSBill Paul 	ifp->if_init = re_init;
165752732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
165852732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
165952732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1660a94100faSBill Paul 
16616c3e93cbSGleb Smirnoff 	NET_TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1662ed510fb0SBill Paul 
1663fed3ed71SPyun YongHyeon #define	RE_PHYAD_INTERNAL	 0
1664fed3ed71SPyun YongHyeon 
1665fed3ed71SPyun YongHyeon 	/* Do MII setup. */
1666fed3ed71SPyun YongHyeon 	phy = RE_PHYAD_INTERNAL;
1667fed3ed71SPyun YongHyeon 	if (sc->rl_type == RL_8169)
1668fed3ed71SPyun YongHyeon 		phy = 1;
166914013280SMarius Strobl 	capmask = BMSR_DEFCAPMASK;
167014013280SMarius Strobl 	if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
167114013280SMarius Strobl 		 capmask &= ~BMSR_EXTSTAT;
1672fed3ed71SPyun YongHyeon 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
167314013280SMarius Strobl 	    re_ifmedia_sts, capmask, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
1674fed3ed71SPyun YongHyeon 	if (error != 0) {
1675fed3ed71SPyun YongHyeon 		device_printf(dev, "attaching PHYs failed\n");
1676fed3ed71SPyun YongHyeon 		goto fail;
1677fed3ed71SPyun YongHyeon 	}
1678fed3ed71SPyun YongHyeon 
1679a94100faSBill Paul 	/*
1680a94100faSBill Paul 	 * Call MI attach routine.
1681a94100faSBill Paul 	 */
1682a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1683a94100faSBill Paul 
1684960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1685960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1686960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1687960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
16887467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
16893b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &reg) == 0)
16907467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1691960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
169244f7cbf5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST);
1693a2a8420cSPyun YongHyeon 	/*
1694f9ad4da7SPyun YongHyeon 	 * Don't enable TSO by default.  It is known to generate
1695f9ad4da7SPyun YongHyeon 	 * corrupted TCP segments(bad TCP options) under certain
16962df05392SSergey Kandaurov 	 * circumstances.
1697a2a8420cSPyun YongHyeon 	 */
1698a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1699ecafbbb5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1700960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1701960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1702960fd5b3SPyun YongHyeon #endif
1703960fd5b3SPyun YongHyeon 	/*
1704960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1705960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1706960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1707960fd5b3SPyun YongHyeon 	 */
17081bffa951SGleb Smirnoff 	ifp->if_hdrlen = sizeof(struct ether_vlan_header);
1709960fd5b3SPyun YongHyeon 
1710579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
1711579a6e3cSLuigi Rizzo 	re_netmap_attach(sc);
1712579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
1713e9f8886eSMarius Strobl 
1714ed510fb0SBill Paul #ifdef RE_DIAG
1715ed510fb0SBill Paul 	/*
1716ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1717ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1718ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1719ed510fb0SBill Paul 	 */
1720ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1721ed510fb0SBill Paul 		error = re_diag(sc);
1722a94100faSBill Paul 		if (error) {
1723ed510fb0SBill Paul 			device_printf(dev,
1724ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1725a94100faSBill Paul 			ether_ifdetach(ifp);
1726a94100faSBill Paul 			goto fail;
1727a94100faSBill Paul 		}
1728ed510fb0SBill Paul 	}
1729ed510fb0SBill Paul #endif
1730a94100faSBill Paul 
1731502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
1732502be0f7SPyun YongHyeon 	intr_filter = 1;
1733502be0f7SPyun YongHyeon #endif
1734a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1735502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
1736502be0f7SPyun YongHyeon 	    intr_filter == 0) {
1737502be0f7SPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
1738502be0f7SPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc,
1739502be0f7SPyun YongHyeon 		    &sc->rl_intrhand[0]);
1740502be0f7SPyun YongHyeon 	} else {
17415774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
17425774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
17435774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
17445774c5ffSPyun YongHyeon 	}
1745a94100faSBill Paul 	if (error) {
1746d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1747a94100faSBill Paul 		ether_ifdetach(ifp);
1748306c97e2SMark Johnston 		goto fail;
1749a94100faSBill Paul 	}
1750a94100faSBill Paul 
17517790c8c1SConrad Meyer 	DEBUGNET_SET(ifp, re);
1752306c97e2SMark Johnston 
1753a94100faSBill Paul fail:
1754a94100faSBill Paul 	if (error)
1755a94100faSBill Paul 		re_detach(dev);
1756a94100faSBill Paul 
1757a94100faSBill Paul 	return (error);
1758a94100faSBill Paul }
1759a94100faSBill Paul 
1760a94100faSBill Paul /*
1761a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1762a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1763a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1764a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1765a94100faSBill Paul  * allocated.
1766a94100faSBill Paul  */
1767a94100faSBill Paul static int
17687b5ffebfSPyun YongHyeon re_detach(device_t dev)
1769a94100faSBill Paul {
1770a94100faSBill Paul 	struct rl_softc		*sc;
1771a94100faSBill Paul 	struct ifnet		*ifp;
17725774c5ffSPyun YongHyeon 	int			i, rid;
1773a94100faSBill Paul 
1774a94100faSBill Paul 	sc = device_get_softc(dev);
1775fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1776aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
177797b9d4baSJohn-Mark Gurney 
177881cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
177981cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
178040929967SGleb Smirnoff #ifdef DEVICE_POLLING
178140929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
178240929967SGleb Smirnoff 			ether_poll_deregister(ifp);
178340929967SGleb Smirnoff #endif
178497b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
178597b9d4baSJohn-Mark Gurney #if 0
178697b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
178797b9d4baSJohn-Mark Gurney #endif
1788a94100faSBill Paul 		re_stop(sc);
1789525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1790d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
17913d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
1792a94100faSBill Paul 		/*
1793a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1794a94100faSBill Paul 		 * still had a BPF descriptor attached to this
179597b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1796a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1797a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1798a94100faSBill Paul 		 * which will try to call re_init() again. This will
1799a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1800a94100faSBill Paul 		 * which will panic the system when the kernel tries
1801a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1802a94100faSBill Paul 		 * anymore.
1803a94100faSBill Paul 		 */
1804a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1805525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1806a94100faSBill Paul 	}
1807a94100faSBill Paul 	if (sc->rl_miibus)
1808a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1809a94100faSBill Paul 	bus_generic_detach(dev);
1810a94100faSBill Paul 
181197b9d4baSJohn-Mark Gurney 	/*
181297b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
181397b9d4baSJohn-Mark Gurney 	 * stopped here.
181497b9d4baSJohn-Mark Gurney 	 */
181597b9d4baSJohn-Mark Gurney 
1816502be0f7SPyun YongHyeon 	if (sc->rl_intrhand[0] != NULL) {
1817502be0f7SPyun YongHyeon 		bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
1818502be0f7SPyun YongHyeon 		sc->rl_intrhand[0] = NULL;
18195774c5ffSPyun YongHyeon 	}
182082242c11SKevin Lo 	if (ifp != NULL) {
182182242c11SKevin Lo #ifdef DEV_NETMAP
182282242c11SKevin Lo 		netmap_detach(ifp);
182382242c11SKevin Lo #endif /* DEV_NETMAP */
1824ad4f426eSWarner Losh 		if_free(ifp);
182582242c11SKevin Lo 	}
1826502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
1827502be0f7SPyun YongHyeon 		rid = 0;
1828502be0f7SPyun YongHyeon 	else
1829502be0f7SPyun YongHyeon 		rid = 1;
18305774c5ffSPyun YongHyeon 	if (sc->rl_irq[0] != NULL) {
1831502be0f7SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]);
18325774c5ffSPyun YongHyeon 		sc->rl_irq[0] = NULL;
18335774c5ffSPyun YongHyeon 	}
1834502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0)
18355774c5ffSPyun YongHyeon 		pci_release_msi(dev);
18364a58fd45SPyun YongHyeon 	if (sc->rl_res_pba) {
18374a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
18384a58fd45SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
18394a58fd45SPyun YongHyeon 	}
1840a94100faSBill Paul 	if (sc->rl_res)
1841ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1842ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1843a94100faSBill Paul 
1844a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1845a94100faSBill Paul 
1846a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
1847068d8643SJohn Baldwin 		if (sc->rl_ldata.rl_rx_list_addr)
1848a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1849a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
1850068d8643SJohn Baldwin 		if (sc->rl_ldata.rl_rx_list)
1851a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1852a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list,
1853a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
1854a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1855a94100faSBill Paul 	}
1856a94100faSBill Paul 
1857a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1858a94100faSBill Paul 
1859a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
1860068d8643SJohn Baldwin 		if (sc->rl_ldata.rl_tx_list_addr)
1861a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1862a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
1863068d8643SJohn Baldwin 		if (sc->rl_ldata.rl_tx_list)
1864a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1865a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list,
1866a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
1867a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1868a94100faSBill Paul 	}
1869a94100faSBill Paul 
1870a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1871a94100faSBill Paul 
1872d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
18739e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
18749e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1875d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1876d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
18779e18005dSPyun YongHyeon 		}
1878d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1879d65abd66SPyun YongHyeon 	}
1880d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
18819e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
18829e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1883d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1884d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
18859e18005dSPyun YongHyeon 		}
1886d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1887d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1888d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1889d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1890a94100faSBill Paul 	}
189181eee0ebSPyun YongHyeon 	if (sc->rl_ldata.rl_jrx_mtag) {
189281eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
189381eee0ebSPyun YongHyeon 			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
189481eee0ebSPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
189581eee0ebSPyun YongHyeon 				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
189681eee0ebSPyun YongHyeon 		}
189781eee0ebSPyun YongHyeon 		if (sc->rl_ldata.rl_jrx_sparemap)
189881eee0ebSPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
189981eee0ebSPyun YongHyeon 			    sc->rl_ldata.rl_jrx_sparemap);
190081eee0ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
190181eee0ebSPyun YongHyeon 	}
1902a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1903a94100faSBill Paul 
1904a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
1905068d8643SJohn Baldwin 		if (sc->rl_ldata.rl_stats_addr)
1906a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1907a94100faSBill Paul 			    sc->rl_ldata.rl_smap);
1908068d8643SJohn Baldwin 		if (sc->rl_ldata.rl_stats)
19090534aae0SPyun YongHyeon 			bus_dmamem_free(sc->rl_ldata.rl_stag,
19100534aae0SPyun YongHyeon 			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1911a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1912a94100faSBill Paul 	}
1913a94100faSBill Paul 
1914a94100faSBill Paul 	if (sc->rl_parent_tag)
1915a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1916a94100faSBill Paul 
1917a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1918a94100faSBill Paul 
1919a94100faSBill Paul 	return (0);
1920a94100faSBill Paul }
1921a94100faSBill Paul 
1922d65abd66SPyun YongHyeon static __inline void
19237b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1924a94100faSBill Paul {
1925d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1926d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1927d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1928a94100faSBill Paul 
192981eee0ebSPyun YongHyeon 	if (sc->rl_ifp->if_mtu > RL_MTU &&
193081eee0ebSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
193181eee0ebSPyun YongHyeon 		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
193281eee0ebSPyun YongHyeon 	else
1933d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1934d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1935d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1936d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1937d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1938d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1939d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1940d65abd66SPyun YongHyeon }
1941d65abd66SPyun YongHyeon 
1942d65abd66SPyun YongHyeon static int
19437b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1944d65abd66SPyun YongHyeon {
1945d65abd66SPyun YongHyeon 	struct mbuf		*m;
1946d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1947d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1948d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1949d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1950d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1951d65abd66SPyun YongHyeon 	int			error, nsegs;
1952d65abd66SPyun YongHyeon 
1953c6499eccSGleb Smirnoff 	m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR);
1954d65abd66SPyun YongHyeon 	if (m == NULL)
1955a94100faSBill Paul 		return (ENOBUFS);
1956a94100faSBill Paul 
1957a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
195822a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
195922a11c96SJohn-Mark Gurney 	/*
196022a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
196122a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
196222a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
196322a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
196422a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
196522a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
196622a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
196722a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
196822a11c96SJohn-Mark Gurney 	 */
196922a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
197022a11c96SJohn-Mark Gurney #endif
1971d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1972d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1973d65abd66SPyun YongHyeon 	if (error != 0) {
1974d65abd66SPyun YongHyeon 		m_freem(m);
1975d65abd66SPyun YongHyeon 		return (ENOBUFS);
1976d65abd66SPyun YongHyeon 	}
1977d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1978a94100faSBill Paul 
1979d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1980d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1981d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1982d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1983d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1984a94100faSBill Paul 	}
1985a94100faSBill Paul 
1986d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1987d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1988d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1989d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1990d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1991d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1992a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1993a94100faSBill Paul 
1994d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1995d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1996d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1997d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1998d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1999d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
2000d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
2001d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
2002d65abd66SPyun YongHyeon 
2003a94100faSBill Paul 	return (0);
2004a94100faSBill Paul }
2005a94100faSBill Paul 
200681eee0ebSPyun YongHyeon static int
200781eee0ebSPyun YongHyeon re_jumbo_newbuf(struct rl_softc *sc, int idx)
200881eee0ebSPyun YongHyeon {
200981eee0ebSPyun YongHyeon 	struct mbuf		*m;
201081eee0ebSPyun YongHyeon 	struct rl_rxdesc	*rxd;
201181eee0ebSPyun YongHyeon 	bus_dma_segment_t	segs[1];
201281eee0ebSPyun YongHyeon 	bus_dmamap_t		map;
201381eee0ebSPyun YongHyeon 	struct rl_desc		*desc;
201481eee0ebSPyun YongHyeon 	uint32_t		cmdstat;
201581eee0ebSPyun YongHyeon 	int			error, nsegs;
201681eee0ebSPyun YongHyeon 
2017c6499eccSGleb Smirnoff 	m = m_getjcl(M_NOWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
201881eee0ebSPyun YongHyeon 	if (m == NULL)
201981eee0ebSPyun YongHyeon 		return (ENOBUFS);
202081eee0ebSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
202181eee0ebSPyun YongHyeon #ifdef RE_FIXUP_RX
202281eee0ebSPyun YongHyeon 	m_adj(m, RE_ETHER_ALIGN);
202381eee0ebSPyun YongHyeon #endif
202481eee0ebSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
202581eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
202681eee0ebSPyun YongHyeon 	if (error != 0) {
202781eee0ebSPyun YongHyeon 		m_freem(m);
202881eee0ebSPyun YongHyeon 		return (ENOBUFS);
202981eee0ebSPyun YongHyeon 	}
203081eee0ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
203181eee0ebSPyun YongHyeon 
203281eee0ebSPyun YongHyeon 	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
203381eee0ebSPyun YongHyeon 	if (rxd->rx_m != NULL) {
203481eee0ebSPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
203581eee0ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
203681eee0ebSPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
203781eee0ebSPyun YongHyeon 	}
203881eee0ebSPyun YongHyeon 
203981eee0ebSPyun YongHyeon 	rxd->rx_m = m;
204081eee0ebSPyun YongHyeon 	map = rxd->rx_dmamap;
204181eee0ebSPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
204281eee0ebSPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
204381eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_jrx_sparemap = map;
204481eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
204581eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
204681eee0ebSPyun YongHyeon 
204781eee0ebSPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
204881eee0ebSPyun YongHyeon 	desc->rl_vlanctl = 0;
204981eee0ebSPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
205081eee0ebSPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
205181eee0ebSPyun YongHyeon 	cmdstat = segs[0].ds_len;
205281eee0ebSPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
205381eee0ebSPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
205481eee0ebSPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
205581eee0ebSPyun YongHyeon 
205681eee0ebSPyun YongHyeon 	return (0);
205781eee0ebSPyun YongHyeon }
205881eee0ebSPyun YongHyeon 
205922a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
206022a11c96SJohn-Mark Gurney static __inline void
20617b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
206222a11c96SJohn-Mark Gurney {
206322a11c96SJohn-Mark Gurney 	int                     i;
206422a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
206522a11c96SJohn-Mark Gurney 
206622a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
206722a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
206822a11c96SJohn-Mark Gurney 
206922a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
207022a11c96SJohn-Mark Gurney 		*dst++ = *src++;
207122a11c96SJohn-Mark Gurney 
207222a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
207322a11c96SJohn-Mark Gurney }
207422a11c96SJohn-Mark Gurney #endif
207522a11c96SJohn-Mark Gurney 
2076a94100faSBill Paul static int
20777b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
2078a94100faSBill Paul {
2079d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2080d65abd66SPyun YongHyeon 	int			i;
208197b9d4baSJohn-Mark Gurney 
208297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
208397b9d4baSJohn-Mark Gurney 
2084d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
2085d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
2086d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
2087d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
2088579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2089579a6e3cSLuigi Rizzo 	re_netmap_tx_init(sc);
2090579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2091d65abd66SPyun YongHyeon 	/* Set EOR. */
2092d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
2093d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
2094a94100faSBill Paul 
2095a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2096d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
2097d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
2098d65abd66SPyun YongHyeon 
2099a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
2100a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
2101d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
2102a94100faSBill Paul 
2103a94100faSBill Paul 	return (0);
2104a94100faSBill Paul }
2105a94100faSBill Paul 
2106a94100faSBill Paul static int
21077b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
2108a94100faSBill Paul {
2109d65abd66SPyun YongHyeon 	int			error, i;
2110a94100faSBill Paul 
2111d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
2112d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2113d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2114d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
2115d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
2116d65abd66SPyun YongHyeon 			return (error);
2117a94100faSBill Paul 	}
2118579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2119579a6e3cSLuigi Rizzo 	re_netmap_rx_init(sc);
2120579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2121a94100faSBill Paul 
2122a94100faSBill Paul 	/* Flush the RX descriptors */
2123a94100faSBill Paul 
2124a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2125a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2126a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2127a94100faSBill Paul 
2128a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
2129a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
2130502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
2131a94100faSBill Paul 
2132a94100faSBill Paul 	return (0);
2133a94100faSBill Paul }
2134a94100faSBill Paul 
213581eee0ebSPyun YongHyeon static int
213681eee0ebSPyun YongHyeon re_jrx_list_init(struct rl_softc *sc)
213781eee0ebSPyun YongHyeon {
213881eee0ebSPyun YongHyeon 	int			error, i;
213981eee0ebSPyun YongHyeon 
214081eee0ebSPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
214181eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
214281eee0ebSPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
214381eee0ebSPyun YongHyeon 		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
214481eee0ebSPyun YongHyeon 		if ((error = re_jumbo_newbuf(sc, i)) != 0)
214581eee0ebSPyun YongHyeon 			return (error);
214681eee0ebSPyun YongHyeon 	}
214781eee0ebSPyun YongHyeon 
214881eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
214981eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_list_map,
215081eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
215181eee0ebSPyun YongHyeon 
215281eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_rx_prodidx = 0;
215381eee0ebSPyun YongHyeon 	sc->rl_head = sc->rl_tail = NULL;
2154502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
215581eee0ebSPyun YongHyeon 
215681eee0ebSPyun YongHyeon 	return (0);
215781eee0ebSPyun YongHyeon }
215881eee0ebSPyun YongHyeon 
2159a94100faSBill Paul /*
2160a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
2161a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
2162a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
2163a94100faSBill Paul  */
2164ed510fb0SBill Paul static int
21651abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2166a94100faSBill Paul {
2167a94100faSBill Paul 	struct mbuf		*m;
2168a94100faSBill Paul 	struct ifnet		*ifp;
216981eee0ebSPyun YongHyeon 	int			i, rxerr, total_len;
2170a94100faSBill Paul 	struct rl_desc		*cur_rx;
2171a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
217281eee0ebSPyun YongHyeon 	int			jumbo, maxpkt = 16, rx_npkts = 0;
2173a94100faSBill Paul 
21745120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
21755120abbfSSam Leffler 
2176fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2177579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2178ce3ee1e7SLuigi Rizzo 	if (netmap_rx_irq(ifp, 0, &rx_npkts))
2179579a6e3cSLuigi Rizzo 		return 0;
2180579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
218181eee0ebSPyun YongHyeon 	if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
218281eee0ebSPyun YongHyeon 		jumbo = 1;
218381eee0ebSPyun YongHyeon 	else
218481eee0ebSPyun YongHyeon 		jumbo = 0;
2185a94100faSBill Paul 
2186a94100faSBill Paul 	/* Invalidate the descriptor memory */
2187a94100faSBill Paul 
2188a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2189a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2190d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2191a94100faSBill Paul 
2192d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2193d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
21945b6d1d9dSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
21955b6d1d9dSPyun YongHyeon 			break;
2196a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
2197a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
2198d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2199d65abd66SPyun YongHyeon 			break;
2200d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
2201a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
220281eee0ebSPyun YongHyeon 		if (jumbo != 0)
220381eee0ebSPyun YongHyeon 			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
220481eee0ebSPyun YongHyeon 		else
2205d65abd66SPyun YongHyeon 			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2206a94100faSBill Paul 
220781eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
220881eee0ebSPyun YongHyeon 		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
220981eee0ebSPyun YongHyeon 		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
221081eee0ebSPyun YongHyeon 			/*
221181eee0ebSPyun YongHyeon 			 * RTL8168C or later controllers do not
221281eee0ebSPyun YongHyeon 			 * support multi-fragment packet.
221381eee0ebSPyun YongHyeon 			 */
221481eee0ebSPyun YongHyeon 			re_discard_rxbuf(sc, i);
221581eee0ebSPyun YongHyeon 			continue;
221681eee0ebSPyun YongHyeon 		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2217d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
2218d65abd66SPyun YongHyeon 				/*
2219d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
2220d65abd66SPyun YongHyeon 				 * discard all the pieces.
2221d65abd66SPyun YongHyeon 				 */
2222d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
2223d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
2224d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
2225d65abd66SPyun YongHyeon 				}
2226d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2227d65abd66SPyun YongHyeon 				continue;
2228d65abd66SPyun YongHyeon 			}
222922a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
2230a94100faSBill Paul 			if (sc->rl_head == NULL)
2231a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
2232a94100faSBill Paul 			else {
2233a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2234a94100faSBill Paul 				sc->rl_tail->m_next = m;
2235a94100faSBill Paul 				sc->rl_tail = m;
2236a94100faSBill Paul 			}
2237a94100faSBill Paul 			continue;
2238a94100faSBill Paul 		}
2239a94100faSBill Paul 
2240a94100faSBill Paul 		/*
2241a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
2242a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
2243a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
2244a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
2245a94100faSBill Paul 		 * were already used, so to make room for the extra
2246a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
2247a94100faSBill Paul 		 * error' bit and shifted the other status bits
2248a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
2249a94100faSBill Paul 		 * still in the same places. We have already extracted
2250a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
2251a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
2252a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
2253a94100faSBill Paul 		 * them using the 8169 status as though it was in the
2254a94100faSBill Paul 		 * same format as that of the 8139C+.
2255a94100faSBill Paul 		 */
2256a94100faSBill Paul 		if (sc->rl_type == RL_8169)
2257a94100faSBill Paul 			rxstat >>= 1;
2258a94100faSBill Paul 
225922a11c96SJohn-Mark Gurney 		/*
226022a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
226122a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
226222a11c96SJohn-Mark Gurney 		 */
226381eee0ebSPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
226481eee0ebSPyun YongHyeon 			rxerr = 1;
226581eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
226681eee0ebSPyun YongHyeon 			    total_len > 8191 &&
226781eee0ebSPyun YongHyeon 			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
226881eee0ebSPyun YongHyeon 				rxerr = 0;
226981eee0ebSPyun YongHyeon 			if (rxerr != 0) {
2270c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
2271a94100faSBill Paul 				/*
2272a94100faSBill Paul 				 * If this is part of a multi-fragment packet,
2273a94100faSBill Paul 				 * discard all the pieces.
2274a94100faSBill Paul 				 */
2275a94100faSBill Paul 				if (sc->rl_head != NULL) {
2276a94100faSBill Paul 					m_freem(sc->rl_head);
2277a94100faSBill Paul 					sc->rl_head = sc->rl_tail = NULL;
2278a94100faSBill Paul 				}
2279d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2280a94100faSBill Paul 				continue;
2281a94100faSBill Paul 			}
228281eee0ebSPyun YongHyeon 		}
2283a94100faSBill Paul 
2284a94100faSBill Paul 		/*
2285a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
2286a94100faSBill Paul 		 * reload the current one.
2287a94100faSBill Paul 		 */
228881eee0ebSPyun YongHyeon 		if (jumbo != 0)
228981eee0ebSPyun YongHyeon 			rxerr = re_jumbo_newbuf(sc, i);
229081eee0ebSPyun YongHyeon 		else
229181eee0ebSPyun YongHyeon 			rxerr = re_newbuf(sc, i);
229281eee0ebSPyun YongHyeon 		if (rxerr != 0) {
2293c8dfaf38SGleb Smirnoff 			if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
2294a94100faSBill Paul 			if (sc->rl_head != NULL) {
2295a94100faSBill Paul 				m_freem(sc->rl_head);
2296a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
2297a94100faSBill Paul 			}
2298d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
2299a94100faSBill Paul 			continue;
2300a94100faSBill Paul 		}
2301a94100faSBill Paul 
2302a94100faSBill Paul 		if (sc->rl_head != NULL) {
230381eee0ebSPyun YongHyeon 			if (jumbo != 0)
230481eee0ebSPyun YongHyeon 				m->m_len = total_len;
230581eee0ebSPyun YongHyeon 			else {
230622a11c96SJohn-Mark Gurney 				m->m_len = total_len % RE_RX_DESC_BUFLEN;
230722a11c96SJohn-Mark Gurney 				if (m->m_len == 0)
230822a11c96SJohn-Mark Gurney 					m->m_len = RE_RX_DESC_BUFLEN;
230981eee0ebSPyun YongHyeon 			}
2310a94100faSBill Paul 			/*
2311a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
2312a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
2313a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
2314a94100faSBill Paul 			 * care about anyway.
2315a94100faSBill Paul 			 */
2316a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
2317a94100faSBill Paul 				sc->rl_tail->m_len -=
2318a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
2319a94100faSBill Paul 				m_freem(m);
2320a94100faSBill Paul 			} else {
2321a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
2322a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2323a94100faSBill Paul 				sc->rl_tail->m_next = m;
2324a94100faSBill Paul 			}
2325a94100faSBill Paul 			m = sc->rl_head;
2326a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
2327a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2328a94100faSBill Paul 		} else
2329a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
2330a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
2331a94100faSBill Paul 
233222a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
233322a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
233422a11c96SJohn-Mark Gurney #endif
2335c8dfaf38SGleb Smirnoff 		if_inc_counter(ifp, IFCOUNTER_IPACKETS, 1);
2336a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
2337a94100faSBill Paul 
2338a94100faSBill Paul 		/* Do RX checksumming if enabled */
2339a94100faSBill Paul 
2340a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2341deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2342a94100faSBill Paul 				/* Check IP header checksum */
2343a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
2344deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2345deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2346a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2347deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2348deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2349a94100faSBill Paul 
2350a94100faSBill Paul 				/* Check TCP/UDP checksum */
2351a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
2352a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2353a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
2354a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2355a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
2356a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2357a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
2358a94100faSBill Paul 				}
2359deb5c680SPyun YongHyeon 			} else {
2360deb5c680SPyun YongHyeon 				/*
2361deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2362deb5c680SPyun YongHyeon 				 */
2363deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2364deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2365deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2366deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2367deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2368deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2369deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2370deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2371deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
2372deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2373deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
2374deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2375deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2376deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2377deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
2378deb5c680SPyun YongHyeon 				}
2379deb5c680SPyun YongHyeon 			}
2380a94100faSBill Paul 		}
2381ed510fb0SBill Paul 		maxpkt--;
2382d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
238378ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
2384bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
238578ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2386d147662cSGleb Smirnoff 		}
23875120abbfSSam Leffler 		RL_UNLOCK(sc);
2388a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
23895120abbfSSam Leffler 		RL_LOCK(sc);
23901abcdbd1SAttilio Rao 		rx_npkts++;
2391a94100faSBill Paul 	}
2392a94100faSBill Paul 
2393a94100faSBill Paul 	/* Flush the RX DMA ring */
2394a94100faSBill Paul 
2395a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2396a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2397a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2398a94100faSBill Paul 
2399a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
2400ed510fb0SBill Paul 
24011abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
24021abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2403ed510fb0SBill Paul 	if (maxpkt)
2404ed510fb0SBill Paul 		return (EAGAIN);
2405ed510fb0SBill Paul 
2406ed510fb0SBill Paul 	return (0);
2407a94100faSBill Paul }
2408a94100faSBill Paul 
2409a94100faSBill Paul static void
24107b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2411a94100faSBill Paul {
2412a94100faSBill Paul 	struct ifnet		*ifp;
2413d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2414a94100faSBill Paul 	u_int32_t		txstat;
2415d65abd66SPyun YongHyeon 	int			cons;
2416d65abd66SPyun YongHyeon 
2417d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2418d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2419d65abd66SPyun YongHyeon 		return;
2420a94100faSBill Paul 
2421fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2422579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2423ce3ee1e7SLuigi Rizzo 	if (netmap_tx_irq(ifp, 0))
2424579a6e3cSLuigi Rizzo 		return;
2425579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2426a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2427a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2428a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2429d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2430a94100faSBill Paul 
2431d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2432d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2433d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2434d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2435a94100faSBill Paul 			break;
2436a94100faSBill Paul 		/*
2437a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2438a94100faSBill Paul 		 * in a fragment chain, which also happens to
2439a94100faSBill Paul 		 * be the only place where the TX status bits
2440a94100faSBill Paul 		 * are valid.
2441a94100faSBill Paul 		 */
2442a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2443d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2444d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2445d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2446d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2447d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2448d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2449d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2450d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2451d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2452a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2453a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2454c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_COLLISIONS, 1);
2455a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2456c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
2457a94100faSBill Paul 			else
2458c8dfaf38SGleb Smirnoff 				if_inc_counter(ifp, IFCOUNTER_OPACKETS, 1);
2459a94100faSBill Paul 		}
2460a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2461d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2462a94100faSBill Paul 	}
2463d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2464a94100faSBill Paul 
2465a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2466a94100faSBill Paul 
2467d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2468ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2469a94100faSBill Paul 		/*
2470b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2471b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2472a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2473a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2474a94100faSBill Paul 		 */
2475a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2476ed510fb0SBill Paul #endif
2477b4b95879SMarius Strobl 	} else
2478b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2479a94100faSBill Paul }
2480a94100faSBill Paul 
2481a94100faSBill Paul static void
24827b5ffebfSPyun YongHyeon re_tick(void *xsc)
2483a94100faSBill Paul {
2484a94100faSBill Paul 	struct rl_softc		*sc;
2485d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2486a94100faSBill Paul 
2487a94100faSBill Paul 	sc = xsc;
248897b9d4baSJohn-Mark Gurney 
248997b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
249097b9d4baSJohn-Mark Gurney 
24911d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2492a94100faSBill Paul 	mii_tick(mii);
24930fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
24940fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2495c2d2e19cSPyun YongHyeon 	/*
2496c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2497c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2498c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2499c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2500c2d2e19cSPyun YongHyeon 	 */
2501c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2502130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2503d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2504a94100faSBill Paul }
2505a94100faSBill Paul 
2506a94100faSBill Paul #ifdef DEVICE_POLLING
25071abcdbd1SAttilio Rao static int
2508a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2509a94100faSBill Paul {
2510a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
25111abcdbd1SAttilio Rao 	int rx_npkts = 0;
2512a94100faSBill Paul 
2513a94100faSBill Paul 	RL_LOCK(sc);
251440929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
25151abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
251697b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
25171abcdbd1SAttilio Rao 	return (rx_npkts);
251897b9d4baSJohn-Mark Gurney }
251997b9d4baSJohn-Mark Gurney 
25201abcdbd1SAttilio Rao static int
252197b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
252297b9d4baSJohn-Mark Gurney {
252397b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
25241abcdbd1SAttilio Rao 	int rx_npkts;
252597b9d4baSJohn-Mark Gurney 
252697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
252797b9d4baSJohn-Mark Gurney 
2528a94100faSBill Paul 	sc->rxcycles = count;
25291abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2530a94100faSBill Paul 	re_txeof(sc);
2531a94100faSBill Paul 
253237652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2533d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2534a94100faSBill Paul 
2535a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2536a94100faSBill Paul 		u_int16_t       status;
2537a94100faSBill Paul 
2538a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2539a94100faSBill Paul 		if (status == 0xffff)
25401abcdbd1SAttilio Rao 			return (rx_npkts);
2541a94100faSBill Paul 		if (status)
2542a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2543818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2544818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2545818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2546a94100faSBill Paul 
2547a94100faSBill Paul 		/*
2548a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2549a94100faSBill Paul 		 */
2550a94100faSBill Paul 
25518476c243SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR) {
25528476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
255397b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2554a94100faSBill Paul 		}
25558476c243SPyun YongHyeon 	}
25561abcdbd1SAttilio Rao 	return (rx_npkts);
2557a94100faSBill Paul }
2558a94100faSBill Paul #endif /* DEVICE_POLLING */
2559a94100faSBill Paul 
2560ef544f63SPaolo Pisati static int
25617b5ffebfSPyun YongHyeon re_intr(void *arg)
2562a94100faSBill Paul {
2563a94100faSBill Paul 	struct rl_softc		*sc;
2564ed510fb0SBill Paul 	uint16_t		status;
2565a94100faSBill Paul 
2566a94100faSBill Paul 	sc = arg;
2567ed510fb0SBill Paul 
2568ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2569498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2570ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2571ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2572ed510fb0SBill Paul 
2573cbc4d2dbSJohn Baldwin 	taskqueue_enqueue(taskqueue_fast, &sc->rl_inttask);
2574ed510fb0SBill Paul 
2575ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2576ed510fb0SBill Paul }
2577ed510fb0SBill Paul 
2578ed510fb0SBill Paul static void
25797b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2580ed510fb0SBill Paul {
2581ed510fb0SBill Paul 	struct rl_softc		*sc;
2582ed510fb0SBill Paul 	struct ifnet		*ifp;
2583ed510fb0SBill Paul 	u_int16_t		status;
2584ed510fb0SBill Paul 	int			rval = 0;
2585ed510fb0SBill Paul 
2586ed510fb0SBill Paul 	sc = arg;
2587ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2588a94100faSBill Paul 
2589a94100faSBill Paul 	RL_LOCK(sc);
259097b9d4baSJohn-Mark Gurney 
2591a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2592a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2593a94100faSBill Paul 
2594d65abd66SPyun YongHyeon 	if (sc->suspended ||
2595d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2596ed510fb0SBill Paul 		RL_UNLOCK(sc);
2597ed510fb0SBill Paul 		return;
2598ed510fb0SBill Paul 	}
2599a94100faSBill Paul 
2600ed510fb0SBill Paul #ifdef DEVICE_POLLING
2601ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2602ed510fb0SBill Paul 		RL_UNLOCK(sc);
2603ed510fb0SBill Paul 		return;
2604ed510fb0SBill Paul 	}
2605ed510fb0SBill Paul #endif
2606a94100faSBill Paul 
26076c3e93cbSGleb Smirnoff 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
26081abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2609ed510fb0SBill Paul 
2610818951afSPyun YongHyeon 	/*
2611818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2612818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2613818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2614818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2615818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2616818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2617818951afSPyun YongHyeon 	 */
2618818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2619818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2620818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
26213d85c23dSPyun YongHyeon 	if (status & (
2622ed510fb0SBill Paul #ifdef RE_TX_MODERATION
26233d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2624ed510fb0SBill Paul #else
26253d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2626ed510fb0SBill Paul #endif
2627ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2628a94100faSBill Paul 		re_txeof(sc);
2629a94100faSBill Paul 
26308476c243SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
26318476c243SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
263297b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
26338476c243SPyun YongHyeon 	}
2634a94100faSBill Paul 
263552732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2636d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2637a94100faSBill Paul 
2638a94100faSBill Paul 	RL_UNLOCK(sc);
2639ed510fb0SBill Paul 
2640ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2641cbc4d2dbSJohn Baldwin 		taskqueue_enqueue(taskqueue_fast, &sc->rl_inttask);
2642ed510fb0SBill Paul 		return;
2643ed510fb0SBill Paul 	}
2644ed510fb0SBill Paul 
2645ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2646a94100faSBill Paul }
2647a94100faSBill Paul 
2648502be0f7SPyun YongHyeon static void
2649502be0f7SPyun YongHyeon re_intr_msi(void *xsc)
2650502be0f7SPyun YongHyeon {
2651502be0f7SPyun YongHyeon 	struct rl_softc		*sc;
2652502be0f7SPyun YongHyeon 	struct ifnet		*ifp;
2653502be0f7SPyun YongHyeon 	uint16_t		intrs, status;
2654502be0f7SPyun YongHyeon 
2655502be0f7SPyun YongHyeon 	sc = xsc;
2656502be0f7SPyun YongHyeon 	RL_LOCK(sc);
2657502be0f7SPyun YongHyeon 
2658502be0f7SPyun YongHyeon 	ifp = sc->rl_ifp;
2659502be0f7SPyun YongHyeon #ifdef DEVICE_POLLING
2660502be0f7SPyun YongHyeon 	if (ifp->if_capenable & IFCAP_POLLING) {
2661502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2662502be0f7SPyun YongHyeon 		return;
2663502be0f7SPyun YongHyeon 	}
2664502be0f7SPyun YongHyeon #endif
2665502be0f7SPyun YongHyeon 	/* Disable interrupts. */
2666502be0f7SPyun YongHyeon 	CSR_WRITE_2(sc, RL_IMR, 0);
2667502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2668502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2669502be0f7SPyun YongHyeon 		return;
2670502be0f7SPyun YongHyeon 	}
2671502be0f7SPyun YongHyeon 
2672502be0f7SPyun YongHyeon 	intrs = RL_INTRS_CPLUS;
2673502be0f7SPyun YongHyeon 	status = CSR_READ_2(sc, RL_ISR);
2674502be0f7SPyun YongHyeon         CSR_WRITE_2(sc, RL_ISR, status);
2675502be0f7SPyun YongHyeon 	if (sc->rl_int_rx_act > 0) {
2676502be0f7SPyun YongHyeon 		intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2677502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2678502be0f7SPyun YongHyeon 		status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2679502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2680502be0f7SPyun YongHyeon 	}
2681502be0f7SPyun YongHyeon 
2682502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR |
2683502be0f7SPyun YongHyeon 	    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) {
2684502be0f7SPyun YongHyeon 		re_rxeof(sc, NULL);
2685502be0f7SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2686502be0f7SPyun YongHyeon 			if (sc->rl_int_rx_mod != 0 &&
2687502be0f7SPyun YongHyeon 			    (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR |
2688502be0f7SPyun YongHyeon 			    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) {
2689502be0f7SPyun YongHyeon 				/* Rearm one-shot timer. */
2690502be0f7SPyun YongHyeon 				CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2691502be0f7SPyun YongHyeon 				intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR |
2692502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN);
2693502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 1;
2694502be0f7SPyun YongHyeon 			} else {
2695502be0f7SPyun YongHyeon 				intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR |
2696502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN;
2697502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 0;
2698502be0f7SPyun YongHyeon 			}
2699502be0f7SPyun YongHyeon 		}
2700502be0f7SPyun YongHyeon 	}
2701502be0f7SPyun YongHyeon 
2702502be0f7SPyun YongHyeon 	/*
2703502be0f7SPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2704502be0f7SPyun YongHyeon 	 * while an existing transmission is in progress. If
2705502be0f7SPyun YongHyeon 	 * the transmitter goes idle but there are still
2706502be0f7SPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2707502be0f7SPyun YongHyeon 	 * channel here to flush them out. This only seems to
2708502be0f7SPyun YongHyeon 	 * be required with the PCIe devices.
2709502be0f7SPyun YongHyeon 	 */
2710502be0f7SPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2711502be0f7SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2712502be0f7SPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2713502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL))
2714502be0f7SPyun YongHyeon 		re_txeof(sc);
2715502be0f7SPyun YongHyeon 
2716502be0f7SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
2717502be0f7SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2718502be0f7SPyun YongHyeon 		re_init_locked(sc);
2719502be0f7SPyun YongHyeon 	}
2720502be0f7SPyun YongHyeon 
2721502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2722502be0f7SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2723502be0f7SPyun YongHyeon 			re_start_locked(ifp);
2724502be0f7SPyun YongHyeon 		CSR_WRITE_2(sc, RL_IMR, intrs);
2725502be0f7SPyun YongHyeon 	}
2726502be0f7SPyun YongHyeon 	RL_UNLOCK(sc);
2727502be0f7SPyun YongHyeon }
2728502be0f7SPyun YongHyeon 
2729d65abd66SPyun YongHyeon static int
27307b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2731d65abd66SPyun YongHyeon {
2732d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2733d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2734d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2735d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2736d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2737d65abd66SPyun YongHyeon 	int			nsegs, prod;
2738d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2739d65abd66SPyun YongHyeon 	int			padlen;
2740ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2741a94100faSBill Paul 
2742d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2743738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
27440fc4974fSBill Paul 
27450fc4974fSBill Paul 	/*
27460fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
27470fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
27480fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
27490fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
27500fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
27510fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
275299c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
27530fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2754d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
27550fc4974fSBill Paul 	 */
2756f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2757deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
275899c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2759d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2760d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2761d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2762c6499eccSGleb Smirnoff 			m_new = m_dup(*m_head, M_NOWAIT);
2763d65abd66SPyun YongHyeon 			m_freem(*m_head);
2764d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2765d65abd66SPyun YongHyeon 				*m_head = NULL;
2766a94100faSBill Paul 				return (ENOBUFS);
2767a94100faSBill Paul 			}
2768d65abd66SPyun YongHyeon 			*m_head = m_new;
2769d65abd66SPyun YongHyeon 		}
2770d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2771d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
2772c6499eccSGleb Smirnoff 			m_new = m_defrag(*m_head, M_NOWAIT);
2773b4b95879SMarius Strobl 			if (m_new == NULL) {
2774b4b95879SMarius Strobl 				m_freem(*m_head);
2775b4b95879SMarius Strobl 				*m_head = NULL;
277680a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2777b4b95879SMarius Strobl 			}
2778d65abd66SPyun YongHyeon 		} else
2779d65abd66SPyun YongHyeon 			m_new = *m_head;
2780a94100faSBill Paul 
27810fc4974fSBill Paul 		/*
27820fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
27830fc4974fSBill Paul 		 * to avoid leaking data.
27840fc4974fSBill Paul 		 */
2785d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2786d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
27870fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2788d65abd66SPyun YongHyeon 		*m_head = m_new;
27890fc4974fSBill Paul 	}
27900fc4974fSBill Paul 
2791d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2792d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2793d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2794d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2795d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2796c6499eccSGleb Smirnoff 		m_new = m_collapse(*m_head, M_NOWAIT, RL_NTXSEGS);
2797d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2798d65abd66SPyun YongHyeon 			m_freem(*m_head);
2799b4b95879SMarius Strobl 			*m_head = NULL;
2800d65abd66SPyun YongHyeon 			return (ENOBUFS);
2801a94100faSBill Paul 		}
2802d65abd66SPyun YongHyeon 		*m_head = m_new;
2803d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2804d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2805d65abd66SPyun YongHyeon 		if (error != 0) {
2806d65abd66SPyun YongHyeon 			m_freem(*m_head);
2807d65abd66SPyun YongHyeon 			*m_head = NULL;
2808d65abd66SPyun YongHyeon 			return (error);
2809a94100faSBill Paul 		}
2810d65abd66SPyun YongHyeon 	} else if (error != 0)
2811d65abd66SPyun YongHyeon 		return (error);
2812d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2813d65abd66SPyun YongHyeon 		m_freem(*m_head);
2814d65abd66SPyun YongHyeon 		*m_head = NULL;
2815d65abd66SPyun YongHyeon 		return (EIO);
2816d65abd66SPyun YongHyeon 	}
2817d65abd66SPyun YongHyeon 
2818d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2819d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2820d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2821d65abd66SPyun YongHyeon 		return (ENOBUFS);
2822d65abd66SPyun YongHyeon 	}
2823d65abd66SPyun YongHyeon 
2824d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2825d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2826a94100faSBill Paul 
2827a94100faSBill Paul 	/*
2828d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2829d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2830d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2831d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2832a94100faSBill Paul 	 */
2833deb5c680SPyun YongHyeon 	vlanctl = 0;
2834d65abd66SPyun YongHyeon 	csum_flags = 0;
2835d6d7d923SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2836d6d7d923SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2837d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND;
2838d6d7d923SPyun YongHyeon 			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2839d6d7d923SPyun YongHyeon 			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2840d6d7d923SPyun YongHyeon 		} else {
2841d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND |
2842d65abd66SPyun YongHyeon 			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2843d65abd66SPyun YongHyeon 			    RL_TDESC_CMD_MSSVAL_SHIFT);
2844d6d7d923SPyun YongHyeon 		}
2845d6d7d923SPyun YongHyeon 	} else {
284699c8ae87SPyun YongHyeon 		/*
284799c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
284899c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
28492df05392SSergey Kandaurov 		 * doesn't make effects.
285099c8ae87SPyun YongHyeon 		 */
285199c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2852deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2853d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2854deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2855deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2856d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2857deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2858deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2859d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2860deb5c680SPyun YongHyeon 			} else {
2861deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2862deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2863deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2864deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2865deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2866deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2867deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2868deb5c680SPyun YongHyeon 			}
2869d65abd66SPyun YongHyeon 		}
287099c8ae87SPyun YongHyeon 	}
2871a94100faSBill Paul 
2872ccf34c81SPyun YongHyeon 	/*
2873ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2874ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2875ccf34c81SPyun YongHyeon 	 * transmission attempt.
2876ccf34c81SPyun YongHyeon 	 */
2877ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2878bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2879deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2880ccf34c81SPyun YongHyeon 
2881d65abd66SPyun YongHyeon 	si = prod;
2882d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2883d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2884deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2885d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2886d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2887d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2888d65abd66SPyun YongHyeon 		if (i != 0)
2889d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2890d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2891d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2892d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2893d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2894d65abd66SPyun YongHyeon 	}
2895d65abd66SPyun YongHyeon 	/* Update producer index. */
2896d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2897a94100faSBill Paul 
2898d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2899d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2900d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2901d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2902d65abd66SPyun YongHyeon 
2903d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2904d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2905d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2906a94100faSBill Paul 
2907d65abd66SPyun YongHyeon 	/*
2908d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2909d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2910d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2911d65abd66SPyun YongHyeon 	 */
2912d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2913d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2914d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2915d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2916d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2917a94100faSBill Paul 
2918a94100faSBill Paul 	return (0);
2919a94100faSBill Paul }
2920a94100faSBill Paul 
292197b9d4baSJohn-Mark Gurney static void
2922d180a66fSPyun YongHyeon re_start(struct ifnet *ifp)
292397b9d4baSJohn-Mark Gurney {
2924d180a66fSPyun YongHyeon 	struct rl_softc		*sc;
292597b9d4baSJohn-Mark Gurney 
2926d180a66fSPyun YongHyeon 	sc = ifp->if_softc;
2927d180a66fSPyun YongHyeon 	RL_LOCK(sc);
2928d180a66fSPyun YongHyeon 	re_start_locked(ifp);
2929d180a66fSPyun YongHyeon 	RL_UNLOCK(sc);
293097b9d4baSJohn-Mark Gurney }
293197b9d4baSJohn-Mark Gurney 
2932a94100faSBill Paul /*
2933a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2934a94100faSBill Paul  */
2935a94100faSBill Paul static void
2936d180a66fSPyun YongHyeon re_start_locked(struct ifnet *ifp)
2937a94100faSBill Paul {
2938a94100faSBill Paul 	struct rl_softc		*sc;
2939d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2940d65abd66SPyun YongHyeon 	int			queued;
2941a94100faSBill Paul 
2942a94100faSBill Paul 	sc = ifp->if_softc;
294397b9d4baSJohn-Mark Gurney 
2944579a6e3cSLuigi Rizzo #ifdef DEV_NETMAP
2945579a6e3cSLuigi Rizzo 	/* XXX is this necessary ? */
2946579a6e3cSLuigi Rizzo 	if (ifp->if_capenable & IFCAP_NETMAP) {
29472ff91c17SVincenzo Maffione 		struct netmap_kring *kring = NA(ifp)->tx_rings[0];
2948579a6e3cSLuigi Rizzo 		if (sc->rl_ldata.rl_tx_prodidx != kring->nr_hwcur) {
2949579a6e3cSLuigi Rizzo 			/* kick the tx unit */
2950579a6e3cSLuigi Rizzo 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2951579a6e3cSLuigi Rizzo #ifdef RE_TX_MODERATION
2952579a6e3cSLuigi Rizzo 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2953579a6e3cSLuigi Rizzo #endif
2954579a6e3cSLuigi Rizzo 			sc->rl_watchdog_timer = 5;
2955579a6e3cSLuigi Rizzo 		}
2956579a6e3cSLuigi Rizzo 		return;
2957579a6e3cSLuigi Rizzo 	}
2958579a6e3cSLuigi Rizzo #endif /* DEV_NETMAP */
2959e9f8886eSMarius Strobl 
2960d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2961d180a66fSPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
2962ed510fb0SBill Paul 		return;
2963a94100faSBill Paul 
2964d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2965d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
296652732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2967a94100faSBill Paul 		if (m_head == NULL)
2968a94100faSBill Paul 			break;
2969a94100faSBill Paul 
2970d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2971b4b95879SMarius Strobl 			if (m_head == NULL)
2972b4b95879SMarius Strobl 				break;
297352732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
297413f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2975a94100faSBill Paul 			break;
2976a94100faSBill Paul 		}
2977a94100faSBill Paul 
2978a94100faSBill Paul 		/*
2979a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2980a94100faSBill Paul 		 * to him.
2981a94100faSBill Paul 		 */
298259a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
298352732175SMax Laier 
298452732175SMax Laier 		queued++;
2985a94100faSBill Paul 	}
2986a94100faSBill Paul 
2987ed510fb0SBill Paul 	if (queued == 0) {
2988ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2989d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2990ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2991ed510fb0SBill Paul #endif
299252732175SMax Laier 		return;
2993ed510fb0SBill Paul 	}
299452732175SMax Laier 
2995306c97e2SMark Johnston 	re_start_tx(sc);
2996306c97e2SMark Johnston }
2997a94100faSBill Paul 
2998306c97e2SMark Johnston static void
2999306c97e2SMark Johnston re_start_tx(struct rl_softc *sc)
3000306c97e2SMark Johnston {
3001306c97e2SMark Johnston 
3002306c97e2SMark Johnston 	/* Flush the TX descriptors */
3003a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
3004a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
3005a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
3006a94100faSBill Paul 
30070fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
3008a94100faSBill Paul 
3009ed510fb0SBill Paul #ifdef RE_TX_MODERATION
3010a94100faSBill Paul 	/*
3011a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
3012a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
3013a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
3014a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
3015a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
3016a94100faSBill Paul 	 * the timer count is reset to 0.
3017a94100faSBill Paul 	 */
3018a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
3019ed510fb0SBill Paul #endif
3020a94100faSBill Paul 
3021a94100faSBill Paul 	/*
3022a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
3023a94100faSBill Paul 	 */
30241d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
3025a94100faSBill Paul }
3026a94100faSBill Paul 
3027a94100faSBill Paul static void
302881eee0ebSPyun YongHyeon re_set_jumbo(struct rl_softc *sc, int jumbo)
302981eee0ebSPyun YongHyeon {
303081eee0ebSPyun YongHyeon 
303181eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
303281eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
303381eee0ebSPyun YongHyeon 		return;
303481eee0ebSPyun YongHyeon 	}
303581eee0ebSPyun YongHyeon 
303681eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
303781eee0ebSPyun YongHyeon 	if (jumbo != 0) {
3038e7e7593cSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) |
303981eee0ebSPyun YongHyeon 		    RL_CFG3_JUMBO_EN0);
304081eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
304181eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
304281eee0ebSPyun YongHyeon 			break;
304381eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
3044e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
3045e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) | 0x01);
304681eee0ebSPyun YongHyeon 			break;
304781eee0ebSPyun YongHyeon 		default:
3048e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
3049e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) | RL_CFG4_JUMBO_EN1);
305081eee0ebSPyun YongHyeon 		}
305181eee0ebSPyun YongHyeon 	} else {
3052e7e7593cSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_cfg3, CSR_READ_1(sc, sc->rl_cfg3) &
305381eee0ebSPyun YongHyeon 		    ~RL_CFG3_JUMBO_EN0);
305481eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
305581eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
305681eee0ebSPyun YongHyeon 			break;
305781eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
3058e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
3059e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) & ~0x01);
306081eee0ebSPyun YongHyeon 			break;
306181eee0ebSPyun YongHyeon 		default:
3062e7e7593cSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_cfg4,
3063e7e7593cSPyun YongHyeon 			    CSR_READ_1(sc, sc->rl_cfg4) & ~RL_CFG4_JUMBO_EN1);
306481eee0ebSPyun YongHyeon 		}
306581eee0ebSPyun YongHyeon 	}
306681eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
306781eee0ebSPyun YongHyeon 
306881eee0ebSPyun YongHyeon 	switch (sc->rl_hwrev->rl_rev) {
306981eee0ebSPyun YongHyeon 	case RL_HWREV_8168DP:
307081eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
307181eee0ebSPyun YongHyeon 		break;
307281eee0ebSPyun YongHyeon 	default:
307381eee0ebSPyun YongHyeon 		if (jumbo != 0)
307481eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 512);
307581eee0ebSPyun YongHyeon 		else
307681eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 4096);
307781eee0ebSPyun YongHyeon 	}
307881eee0ebSPyun YongHyeon }
307981eee0ebSPyun YongHyeon 
308081eee0ebSPyun YongHyeon static void
30817b5ffebfSPyun YongHyeon re_init(void *xsc)
3082a94100faSBill Paul {
3083a94100faSBill Paul 	struct rl_softc		*sc = xsc;
308497b9d4baSJohn-Mark Gurney 
308597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
308697b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
308797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
308897b9d4baSJohn-Mark Gurney }
308997b9d4baSJohn-Mark Gurney 
309097b9d4baSJohn-Mark Gurney static void
30917b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
309297b9d4baSJohn-Mark Gurney {
3093fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
3094a94100faSBill Paul 	struct mii_data		*mii;
3095566ca8caSJung-uk Kim 	uint32_t		reg;
309670acaecfSPyun YongHyeon 	uint16_t		cfg;
30974d3d7085SBernd Walter 	union {
30984d3d7085SBernd Walter 		uint32_t align_dummy;
30994d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
31004d3d7085SBernd Walter         } eaddr;
3101a94100faSBill Paul 
310297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
310397b9d4baSJohn-Mark Gurney 
3104a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3105a94100faSBill Paul 
31068476c243SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
31078476c243SPyun YongHyeon 		return;
31088476c243SPyun YongHyeon 
3109a94100faSBill Paul 	/*
3110a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
3111a94100faSBill Paul 	 */
3112a94100faSBill Paul 	re_stop(sc);
3113a94100faSBill Paul 
3114b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
3115b659f1f0SPyun YongHyeon 	re_reset(sc);
3116b659f1f0SPyun YongHyeon 
3117a94100faSBill Paul 	/*
31184a814a5eSPyun YongHyeon 	 * For C+ mode, initialize the RX descriptors and mbufs.
31194a814a5eSPyun YongHyeon 	 */
312081eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
312181eee0ebSPyun YongHyeon 		if (ifp->if_mtu > RL_MTU) {
312281eee0ebSPyun YongHyeon 			if (re_jrx_list_init(sc) != 0) {
312381eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
312481eee0ebSPyun YongHyeon 				    "no memory for jumbo RX buffers\n");
312581eee0ebSPyun YongHyeon 				re_stop(sc);
312681eee0ebSPyun YongHyeon 				return;
312781eee0ebSPyun YongHyeon 			}
312881eee0ebSPyun YongHyeon 			/* Disable checksum offloading for jumbo frames. */
312981eee0ebSPyun YongHyeon 			ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
313081eee0ebSPyun YongHyeon 			ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
313181eee0ebSPyun YongHyeon 		} else {
313281eee0ebSPyun YongHyeon 			if (re_rx_list_init(sc) != 0) {
313381eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
313481eee0ebSPyun YongHyeon 				    "no memory for RX buffers\n");
313581eee0ebSPyun YongHyeon 				re_stop(sc);
313681eee0ebSPyun YongHyeon 				return;
313781eee0ebSPyun YongHyeon 			}
313881eee0ebSPyun YongHyeon 		}
313981eee0ebSPyun YongHyeon 		re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
314081eee0ebSPyun YongHyeon 	} else {
31414a814a5eSPyun YongHyeon 		if (re_rx_list_init(sc) != 0) {
31424a814a5eSPyun YongHyeon 			device_printf(sc->rl_dev, "no memory for RX buffers\n");
31434a814a5eSPyun YongHyeon 			re_stop(sc);
31444a814a5eSPyun YongHyeon 			return;
31454a814a5eSPyun YongHyeon 		}
314681eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
314781eee0ebSPyun YongHyeon 		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
314881eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
314981eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 512);
315081eee0ebSPyun YongHyeon 			else
315181eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 4096);
315281eee0ebSPyun YongHyeon 		}
315381eee0ebSPyun YongHyeon 	}
31544a814a5eSPyun YongHyeon 	re_tx_list_init(sc);
31554a814a5eSPyun YongHyeon 
31564a814a5eSPyun YongHyeon 	/*
3157c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
3158edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
3159c2c6548bSBill Paul 	 * before all others.
3160c2c6548bSBill Paul 	 */
316170acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
316270acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
316370acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
316470acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
316570acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
3166deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
3167deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
3168deb5c680SPyun YongHyeon 		/* XXX magic. */
3169deb5c680SPyun YongHyeon 		cfg |= 0x0001;
3170deb5c680SPyun YongHyeon 	} else
3171deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
3172deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
317381eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
317481eee0ebSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
3175566ca8caSJung-uk Kim 		reg = 0x000fff00;
3176e7e7593cSPyun YongHyeon 		if ((CSR_READ_1(sc, sc->rl_cfg2) & RL_CFG2_PCI66MHZ) != 0)
3177566ca8caSJung-uk Kim 			reg |= 0x000000ff;
317881eee0ebSPyun YongHyeon 		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
3179566ca8caSJung-uk Kim 			reg |= 0x00f00000;
3180566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
3181566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
3182566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
3183566ca8caSJung-uk Kim 	}
3184ae644087SPyun YongHyeon 	/*
3185ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
3186ae644087SPyun YongHyeon 	 * allowed in controller.
3187ae644087SPyun YongHyeon 	 */
3188ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
3189ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
3190ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
3191ae644087SPyun YongHyeon 	}
3192c2c6548bSBill Paul 
3193c2c6548bSBill Paul 	/*
3194a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
3195a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
3196a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
3197a94100faSBill Paul 	 */
31984d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
31994d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
3200a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3201ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
3202ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
3203ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
3204ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
3205a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3206a94100faSBill Paul 
3207a94100faSBill Paul 	/*
3208d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
3209d01fac16SPyun YongHyeon 	 */
3210d01fac16SPyun YongHyeon 
3211d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
3212d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
3213d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
3214d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
3215d01fac16SPyun YongHyeon 
3216d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
3217d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
3218d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
3219d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
3220d01fac16SPyun YongHyeon 
322114013280SMarius Strobl 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
322214013280SMarius Strobl 		/* Disable RXDV gate. */
3223f1a5f291SMarius Strobl 		CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) &
3224f1a5f291SMarius Strobl 		    ~0x00080000);
322514013280SMarius Strobl 	}
322614013280SMarius Strobl 
322714013280SMarius Strobl 	/*
322814013280SMarius Strobl 	 * Enable transmit and receive for pre-RTL8168G controllers.
322914013280SMarius Strobl 	 * RX/TX MACs should be enabled before RX/TX configuration.
323014013280SMarius Strobl 	 */
323114013280SMarius Strobl 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) == 0)
323214013280SMarius Strobl 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
3233f1a5f291SMarius Strobl 
3234d01fac16SPyun YongHyeon 	/*
3235ff191365SJung-uk Kim 	 * Set the initial TX configuration.
3236a94100faSBill Paul 	 */
3237abc8ff44SBill Paul 	if (sc->rl_testmode) {
3238abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
3239abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3240abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
3241a94100faSBill Paul 		else
3242abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3243abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
3244abc8ff44SBill Paul 	} else
3245a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
3246d01fac16SPyun YongHyeon 
3247d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
3248d01fac16SPyun YongHyeon 
3249a94100faSBill Paul 	/*
3250ff191365SJung-uk Kim 	 * Set the initial RX configuration.
3251a94100faSBill Paul 	 */
3252ff191365SJung-uk Kim 	re_set_rxmode(sc);
3253a94100faSBill Paul 
3254483cc440SPyun YongHyeon 	/* Configure interrupt moderation. */
3255483cc440SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
3256483cc440SPyun YongHyeon 		/* Magic from vendor. */
32575e6906eeSPyun YongHyeon 		CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
3258483cc440SPyun YongHyeon 	}
3259483cc440SPyun YongHyeon 
32600f55f9d6SMarius Strobl 	/*
326114013280SMarius Strobl 	 * Enable transmit and receive for RTL8168G and later controllers.
326214013280SMarius Strobl 	 * RX/TX MACs should be enabled after RX/TX configuration.
32630f55f9d6SMarius Strobl 	 */
326414013280SMarius Strobl 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0)
32650f55f9d6SMarius Strobl 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB | RL_CMD_RX_ENB);
32660f55f9d6SMarius Strobl 
3267a94100faSBill Paul #ifdef DEVICE_POLLING
3268a94100faSBill Paul 	/*
3269a94100faSBill Paul 	 * Disable interrupts if we are polling.
3270a94100faSBill Paul 	 */
327140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3272a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3273a94100faSBill Paul 	else	/* otherwise ... */
327440929967SGleb Smirnoff #endif
3275ed510fb0SBill Paul 
3276a94100faSBill Paul 	/*
3277a94100faSBill Paul 	 * Enable interrupts.
3278a94100faSBill Paul 	 */
3279a94100faSBill Paul 	if (sc->rl_testmode)
3280a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3281a94100faSBill Paul 	else
3282a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3283ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3284a94100faSBill Paul 
3285a94100faSBill Paul 	/* Set initial TX threshold */
3286a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
3287a94100faSBill Paul 
3288a94100faSBill Paul 	/* Start RX/TX process. */
3289a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3290a94100faSBill Paul 
3291a94100faSBill Paul 	/*
3292a94100faSBill Paul 	 * Initialize the timer interrupt register so that
3293a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
3294a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
3295502be0f7SPyun YongHyeon 	 * reloaded on each transmit.
3296502be0f7SPyun YongHyeon 	 */
3297502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
3298502be0f7SPyun YongHyeon 	/*
3299502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate TX interrupt
3300a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
3301a94100faSBill Paul 	 */
3302a94100faSBill Paul 	if (sc->rl_type == RL_8169)
3303a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3304a94100faSBill Paul 	else
3305a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3306502be0f7SPyun YongHyeon #else
3307502be0f7SPyun YongHyeon 	/*
3308502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate RX interrupt
3309502be0f7SPyun YongHyeon 	 * moderation.
3310502be0f7SPyun YongHyeon 	 */
3311502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
3312502be0f7SPyun YongHyeon 	    intr_filter == 0) {
3313502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3314502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169,
3315502be0f7SPyun YongHyeon 			    RL_USECS(sc->rl_int_rx_mod));
3316502be0f7SPyun YongHyeon 	} else {
3317502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3318502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0));
3319502be0f7SPyun YongHyeon 	}
3320ed510fb0SBill Paul #endif
3321a94100faSBill Paul 
3322a94100faSBill Paul 	/*
3323a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
3324a94100faSBill Paul 	 * size so we can receive jumbo frames.
3325a94100faSBill Paul 	 */
332689feeee4SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
332781eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
332881eee0ebSPyun YongHyeon 			/*
332981eee0ebSPyun YongHyeon 			 * For controllers that use new jumbo frame scheme,
33302df05392SSergey Kandaurov 			 * set maximum size of jumbo frame depending on
333181eee0ebSPyun YongHyeon 			 * controller revisions.
333281eee0ebSPyun YongHyeon 			 */
333381eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
333481eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
333581eee0ebSPyun YongHyeon 				    sc->rl_hwrev->rl_max_mtu +
333681eee0ebSPyun YongHyeon 				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
333781eee0ebSPyun YongHyeon 				    ETHER_CRC_LEN);
333889feeee4SPyun YongHyeon 			else
333981eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
334081eee0ebSPyun YongHyeon 				    RE_RX_DESC_BUFLEN);
334181eee0ebSPyun YongHyeon 		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
334281eee0ebSPyun YongHyeon 		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
334381eee0ebSPyun YongHyeon 			/* RTL810x has no jumbo frame support. */
334481eee0ebSPyun YongHyeon 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
334581eee0ebSPyun YongHyeon 		} else
3346a94100faSBill Paul 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
334789feeee4SPyun YongHyeon 	}
3348a94100faSBill Paul 
334997b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
3350a94100faSBill Paul 		return;
3351a94100faSBill Paul 
3352e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, CSR_READ_1(sc, sc->rl_cfg1) |
3353e7e7593cSPyun YongHyeon 	    RL_CFG1_DRVLOAD);
3354a94100faSBill Paul 
335513f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
335613f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3357a94100faSBill Paul 
3358351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
33591662c49eSPyun YongHyeon 	mii_mediachg(mii);
33601662c49eSPyun YongHyeon 
33611d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3362d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3363a94100faSBill Paul }
3364a94100faSBill Paul 
3365a94100faSBill Paul /*
3366a94100faSBill Paul  * Set media options.
3367a94100faSBill Paul  */
3368a94100faSBill Paul static int
33697b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
3370a94100faSBill Paul {
3371a94100faSBill Paul 	struct rl_softc		*sc;
3372a94100faSBill Paul 	struct mii_data		*mii;
33736f0f9b12SPyun YongHyeon 	int			error;
3374a94100faSBill Paul 
3375a94100faSBill Paul 	sc = ifp->if_softc;
3376a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3377d1754a9bSJohn Baldwin 	RL_LOCK(sc);
33786f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
3379d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3380a94100faSBill Paul 
33816f0f9b12SPyun YongHyeon 	return (error);
3382a94100faSBill Paul }
3383a94100faSBill Paul 
3384a94100faSBill Paul /*
3385a94100faSBill Paul  * Report current media status.
3386a94100faSBill Paul  */
3387a94100faSBill Paul static void
33887b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3389a94100faSBill Paul {
3390a94100faSBill Paul 	struct rl_softc		*sc;
3391a94100faSBill Paul 	struct mii_data		*mii;
3392a94100faSBill Paul 
3393a94100faSBill Paul 	sc = ifp->if_softc;
3394a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3395a94100faSBill Paul 
3396d1754a9bSJohn Baldwin 	RL_LOCK(sc);
3397a94100faSBill Paul 	mii_pollstat(mii);
3398a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
3399a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
340057c81d92SPyun YongHyeon 	RL_UNLOCK(sc);
3401a94100faSBill Paul }
3402a94100faSBill Paul 
3403a94100faSBill Paul static int
34047b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3405a94100faSBill Paul {
3406a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
3407a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
3408a94100faSBill Paul 	struct mii_data		*mii;
340940929967SGleb Smirnoff 	int			error = 0;
3410a94100faSBill Paul 
3411a94100faSBill Paul 	switch (command) {
3412a94100faSBill Paul 	case SIOCSIFMTU:
341381eee0ebSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN ||
3414ab9f923eSPyun YongHyeon 		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu ||
3415ab9f923eSPyun YongHyeon 		    ((sc->rl_flags & RL_FLAG_FASTETHER) != 0 &&
3416ab9f923eSPyun YongHyeon 		    ifr->ifr_mtu > RL_MTU)) {
3417c1d0b573SPyun YongHyeon 			error = EINVAL;
3418c1d0b573SPyun YongHyeon 			break;
3419c1d0b573SPyun YongHyeon 		}
3420c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
342181eee0ebSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
3422a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
342381eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
342481eee0ebSPyun YongHyeon 			    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
342581eee0ebSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
342681eee0ebSPyun YongHyeon 				re_init_locked(sc);
342781eee0ebSPyun YongHyeon 			}
3428ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3429ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
343081eee0ebSPyun YongHyeon 				ifp->if_capenable &= ~(IFCAP_TSO4 |
343181eee0ebSPyun YongHyeon 				    IFCAP_VLAN_HWTSO);
3432ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
343381eee0ebSPyun YongHyeon 			}
3434ecafbbb5SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
3435ae644087SPyun YongHyeon 		}
3436d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
3437a94100faSBill Paul 		break;
3438a94100faSBill Paul 	case SIOCSIFFLAGS:
343997b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
3440eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
3441eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3442eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
34433021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3444ff191365SJung-uk Kim 					re_set_rxmode(sc);
3445eed497bbSPyun YongHyeon 			} else
344697b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
3447eed497bbSPyun YongHyeon 		} else {
3448eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3449a94100faSBill Paul 				re_stop(sc);
3450eed497bbSPyun YongHyeon 		}
3451eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
345297b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3453a94100faSBill Paul 		break;
3454a94100faSBill Paul 	case SIOCADDMULTI:
3455a94100faSBill Paul 	case SIOCDELMULTI:
345697b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
34578476c243SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3458ff191365SJung-uk Kim 			re_set_rxmode(sc);
345997b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3460a94100faSBill Paul 		break;
3461a94100faSBill Paul 	case SIOCGIFMEDIA:
3462a94100faSBill Paul 	case SIOCSIFMEDIA:
3463a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
3464a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3465a94100faSBill Paul 		break;
3466a94100faSBill Paul 	case SIOCSIFCAP:
346740929967SGleb Smirnoff 	    {
3468f051cb85SGleb Smirnoff 		int mask, reinit;
3469f051cb85SGleb Smirnoff 
3470f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3471f051cb85SGleb Smirnoff 		reinit = 0;
347240929967SGleb Smirnoff #ifdef DEVICE_POLLING
347340929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
347440929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
347540929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
347640929967SGleb Smirnoff 				if (error)
347740929967SGleb Smirnoff 					return (error);
3478d1754a9bSJohn Baldwin 				RL_LOCK(sc);
347940929967SGleb Smirnoff 				/* Disable interrupts */
348040929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
348140929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
348240929967SGleb Smirnoff 				RL_UNLOCK(sc);
348340929967SGleb Smirnoff 			} else {
348440929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
348540929967SGleb Smirnoff 				/* Enable interrupts. */
348640929967SGleb Smirnoff 				RL_LOCK(sc);
348740929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
348840929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
348940929967SGleb Smirnoff 				RL_UNLOCK(sc);
349040929967SGleb Smirnoff 			}
349140929967SGleb Smirnoff 		}
349240929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3493600af6c2SPyun YongHyeon 		RL_LOCK(sc);
3494d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
3495d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3496d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
349774a03446SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
3498bc2a1002SPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
349974a03446SPyun YongHyeon 			else
3500b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3501f051cb85SGleb Smirnoff 			reinit = 1;
350240929967SGleb Smirnoff 		}
3503d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
3504d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3505d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
3506d3b181aeSPyun YongHyeon 			reinit = 1;
3507d3b181aeSPyun YongHyeon 		}
3508ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
3509fca1e0abSBjoern A. Zeeb 		    (ifp->if_capabilities & IFCAP_TSO4) != 0) {
3510dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
3511ecafbbb5SPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3512dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
3513dc74159dSPyun YongHyeon 			else
3514dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3515ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3516ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3517ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
3518ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3519ae644087SPyun YongHyeon 			}
3520dc74159dSPyun YongHyeon 		}
3521ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3522ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3523ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3524ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3525ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3526ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3527ecafbbb5SPyun YongHyeon 			/* TSO over VLAN requires VLAN hardware tagging. */
3528ecafbbb5SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3529ecafbbb5SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3530ecafbbb5SPyun YongHyeon 			reinit = 1;
3531ecafbbb5SPyun YongHyeon 		}
353281eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
353381eee0ebSPyun YongHyeon 		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
353481eee0ebSPyun YongHyeon 		    IFCAP_VLAN_HWTSO)) != 0)
353581eee0ebSPyun YongHyeon 				reinit = 1;
35367467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
35377467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
35387467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
35397467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
35407467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
35417467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
35427467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
35437467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
35447467bd53SPyun YongHyeon 		}
35458476c243SPyun YongHyeon 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
35468476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3547600af6c2SPyun YongHyeon 			re_init_locked(sc);
35488476c243SPyun YongHyeon 		}
3549600af6c2SPyun YongHyeon 		RL_UNLOCK(sc);
3550960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
355140929967SGleb Smirnoff 	    }
3552a94100faSBill Paul 		break;
3553a94100faSBill Paul 	default:
3554a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
3555a94100faSBill Paul 		break;
3556a94100faSBill Paul 	}
3557a94100faSBill Paul 
3558a94100faSBill Paul 	return (error);
3559a94100faSBill Paul }
3560a94100faSBill Paul 
3561a94100faSBill Paul static void
35627b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
35631d545c7aSMarius Strobl {
3564130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
3565a94100faSBill Paul 
35661d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
35671d545c7aSMarius Strobl 
35681d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
35691d545c7aSMarius Strobl 		return;
35701d545c7aSMarius Strobl 
3571130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
3572a94100faSBill Paul 	re_txeof(sc);
3573130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3574130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3575130b6dfbSPyun YongHyeon 		    "-- recovering\n");
3576130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3577d180a66fSPyun YongHyeon 			re_start_locked(ifp);
3578130b6dfbSPyun YongHyeon 		return;
3579130b6dfbSPyun YongHyeon 	}
3580130b6dfbSPyun YongHyeon 
3581130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
3582c8dfaf38SGleb Smirnoff 	if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
3583130b6dfbSPyun YongHyeon 
35841abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
35858476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
358697b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
3587130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3588d180a66fSPyun YongHyeon 		re_start_locked(ifp);
3589a94100faSBill Paul }
3590a94100faSBill Paul 
3591a94100faSBill Paul /*
3592a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
3593a94100faSBill Paul  * RX and TX lists.
3594a94100faSBill Paul  */
3595a94100faSBill Paul static void
35967b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
3597a94100faSBill Paul {
35980ce0868aSPyun YongHyeon 	int			i;
3599a94100faSBill Paul 	struct ifnet		*ifp;
3600d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
3601d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
3602a94100faSBill Paul 
360397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
360497b9d4baSJohn-Mark Gurney 
3605fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
3606a94100faSBill Paul 
36071d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3608d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
360913f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3610a94100faSBill Paul 
3611fcb220acSPyun YongHyeon 	/*
3612fcb220acSPyun YongHyeon 	 * Disable accepting frames to put RX MAC into idle state.
3613fcb220acSPyun YongHyeon 	 * Otherwise it's possible to get frames while stop command
3614fcb220acSPyun YongHyeon 	 * execution is in progress and controller can DMA the frame
3615fcb220acSPyun YongHyeon 	 * to already freed RX buffer during that period.
3616fcb220acSPyun YongHyeon 	 */
3617fcb220acSPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXCFG, CSR_READ_4(sc, RL_RXCFG) &
3618fcb220acSPyun YongHyeon 	    ~(RL_RXCFG_RX_ALLPHYS | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_MULTI |
3619fcb220acSPyun YongHyeon 	    RL_RXCFG_RX_BROAD));
3620fcb220acSPyun YongHyeon 
362114013280SMarius Strobl 	if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
362214013280SMarius Strobl 		/* Enable RXDV gate. */
362314013280SMarius Strobl 		CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) |
362414013280SMarius Strobl 		    0x00080000);
362514013280SMarius Strobl 	}
362614013280SMarius Strobl 
3627eef0e496SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_WAIT_TXPOLL) != 0) {
3628eef0e496SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
3629eef0e496SPyun YongHyeon 			if ((CSR_READ_1(sc, sc->rl_txstart) &
3630eef0e496SPyun YongHyeon 			    RL_TXSTART_START) == 0)
3631eef0e496SPyun YongHyeon 				break;
3632eef0e496SPyun YongHyeon 			DELAY(20);
3633eef0e496SPyun YongHyeon 		}
3634eef0e496SPyun YongHyeon 		if (i == 0)
3635eef0e496SPyun YongHyeon 			device_printf(sc->rl_dev,
3636eef0e496SPyun YongHyeon 			    "stopping TX poll timed out!\n");
3637eef0e496SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3638eef0e496SPyun YongHyeon 	} else if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0) {
3639ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3640ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
3641eef0e496SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_CMDSTOP_WAIT_TXQ) != 0) {
3642eef0e496SPyun YongHyeon 			for (i = RL_TIMEOUT; i > 0; i--) {
3643eef0e496SPyun YongHyeon 				if ((CSR_READ_4(sc, RL_TXCFG) &
3644eef0e496SPyun YongHyeon 				    RL_TXCFG_QUEUE_EMPTY) != 0)
3645eef0e496SPyun YongHyeon 					break;
3646eef0e496SPyun YongHyeon 				DELAY(100);
3647eef0e496SPyun YongHyeon 			}
3648eef0e496SPyun YongHyeon 			if (i == 0)
3649eef0e496SPyun YongHyeon 				device_printf(sc->rl_dev,
3650eef0e496SPyun YongHyeon 				   "stopping TXQ timed out!\n");
3651eef0e496SPyun YongHyeon 		}
3652eef0e496SPyun YongHyeon 	} else
3653a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3654ead8fc66SPyun YongHyeon 	DELAY(1000);
3655a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3656ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3657a94100faSBill Paul 
3658a94100faSBill Paul 	if (sc->rl_head != NULL) {
3659a94100faSBill Paul 		m_freem(sc->rl_head);
3660a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
3661a94100faSBill Paul 	}
3662a94100faSBill Paul 
3663a94100faSBill Paul 	/* Free the TX list buffers. */
3664d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3665d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
3666d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
3667d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3668d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3669d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3670d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
3671d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
3672d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
3673a94100faSBill Paul 		}
3674a94100faSBill Paul 	}
3675a94100faSBill Paul 
3676a94100faSBill Paul 	/* Free the RX list buffers. */
3677d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3678d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
3679d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
3680cba16362SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
3681d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3682d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3683d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
3684d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
3685d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
3686a94100faSBill Paul 		}
3687a94100faSBill Paul 	}
36881f32d3b7SPyun YongHyeon 
36891f32d3b7SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
36901f32d3b7SPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
36911f32d3b7SPyun YongHyeon 			rxd = &sc->rl_ldata.rl_jrx_desc[i];
36921f32d3b7SPyun YongHyeon 			if (rxd->rx_m != NULL) {
36931f32d3b7SPyun YongHyeon 				bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag,
36941f32d3b7SPyun YongHyeon 				    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
36951f32d3b7SPyun YongHyeon 				bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag,
36961f32d3b7SPyun YongHyeon 				    rxd->rx_dmamap);
36971f32d3b7SPyun YongHyeon 				m_freem(rxd->rx_m);
36981f32d3b7SPyun YongHyeon 				rxd->rx_m = NULL;
36991f32d3b7SPyun YongHyeon 			}
37001f32d3b7SPyun YongHyeon 		}
37011f32d3b7SPyun YongHyeon 	}
3702a94100faSBill Paul }
3703a94100faSBill Paul 
3704a94100faSBill Paul /*
3705a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
3706a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
3707a94100faSBill Paul  * resume.
3708a94100faSBill Paul  */
3709a94100faSBill Paul static int
37107b5ffebfSPyun YongHyeon re_suspend(device_t dev)
3711a94100faSBill Paul {
3712a94100faSBill Paul 	struct rl_softc		*sc;
3713a94100faSBill Paul 
3714a94100faSBill Paul 	sc = device_get_softc(dev);
3715a94100faSBill Paul 
371697b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3717a94100faSBill Paul 	re_stop(sc);
37187467bd53SPyun YongHyeon 	re_setwol(sc);
3719a94100faSBill Paul 	sc->suspended = 1;
372097b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3721a94100faSBill Paul 
3722a94100faSBill Paul 	return (0);
3723a94100faSBill Paul }
3724a94100faSBill Paul 
3725a94100faSBill Paul /*
3726a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
3727a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
3728a94100faSBill Paul  * appropriate.
3729a94100faSBill Paul  */
3730a94100faSBill Paul static int
37317b5ffebfSPyun YongHyeon re_resume(device_t dev)
3732a94100faSBill Paul {
3733a94100faSBill Paul 	struct rl_softc		*sc;
3734a94100faSBill Paul 	struct ifnet		*ifp;
3735a94100faSBill Paul 
3736a94100faSBill Paul 	sc = device_get_softc(dev);
373797b9d4baSJohn-Mark Gurney 
373897b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
373997b9d4baSJohn-Mark Gurney 
3740fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
374161f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
374261f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
374361f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
374461f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
374561f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
374661f45a72SPyun YongHyeon 	}
3747a94100faSBill Paul 
37487467bd53SPyun YongHyeon 	/*
37497467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
37507467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
37517467bd53SPyun YongHyeon 	 */
37527467bd53SPyun YongHyeon 	re_clrwol(sc);
375301d1a6c3SPyun YongHyeon 
375401d1a6c3SPyun YongHyeon 	/* reinitialize interface if necessary */
375501d1a6c3SPyun YongHyeon 	if (ifp->if_flags & IFF_UP)
375601d1a6c3SPyun YongHyeon 		re_init_locked(sc);
375701d1a6c3SPyun YongHyeon 
3758a94100faSBill Paul 	sc->suspended = 0;
375997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3760a94100faSBill Paul 
3761a94100faSBill Paul 	return (0);
3762a94100faSBill Paul }
3763a94100faSBill Paul 
3764a94100faSBill Paul /*
3765a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3766a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3767a94100faSBill Paul  */
37686a087a87SPyun YongHyeon static int
37697b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3770a94100faSBill Paul {
3771a94100faSBill Paul 	struct rl_softc		*sc;
3772a94100faSBill Paul 
3773a94100faSBill Paul 	sc = device_get_softc(dev);
3774a94100faSBill Paul 
377597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3776a94100faSBill Paul 	re_stop(sc);
3777536fde34SMaxim Sobolev 	/*
3778536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3779536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
378072293673SRuslan Ermilov 	 * cases.
3781536fde34SMaxim Sobolev 	 */
3782536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
37837467bd53SPyun YongHyeon 	re_setwol(sc);
378497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
37856a087a87SPyun YongHyeon 
37866a087a87SPyun YongHyeon 	return (0);
3787a94100faSBill Paul }
37887467bd53SPyun YongHyeon 
37897467bd53SPyun YongHyeon static void
37906830588dSPyun YongHyeon re_set_linkspeed(struct rl_softc *sc)
37916830588dSPyun YongHyeon {
37926830588dSPyun YongHyeon 	struct mii_softc *miisc;
37936830588dSPyun YongHyeon 	struct mii_data *mii;
37946830588dSPyun YongHyeon 	int aneg, i, phyno;
37956830588dSPyun YongHyeon 
37966830588dSPyun YongHyeon 	RL_LOCK_ASSERT(sc);
37976830588dSPyun YongHyeon 
37986830588dSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
37996830588dSPyun YongHyeon 	mii_pollstat(mii);
38006830588dSPyun YongHyeon 	aneg = 0;
38016830588dSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
38026830588dSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
38036830588dSPyun YongHyeon 		switch IFM_SUBTYPE(mii->mii_media_active) {
38046830588dSPyun YongHyeon 		case IFM_10_T:
38056830588dSPyun YongHyeon 		case IFM_100_TX:
38066830588dSPyun YongHyeon 			return;
38076830588dSPyun YongHyeon 		case IFM_1000_T:
38086830588dSPyun YongHyeon 			aneg++;
38096830588dSPyun YongHyeon 			break;
38106830588dSPyun YongHyeon 		default:
38116830588dSPyun YongHyeon 			break;
38126830588dSPyun YongHyeon 		}
38136830588dSPyun YongHyeon 	}
38146830588dSPyun YongHyeon 	miisc = LIST_FIRST(&mii->mii_phys);
38156830588dSPyun YongHyeon 	phyno = miisc->mii_phy;
38166830588dSPyun YongHyeon 	LIST_FOREACH(miisc, &mii->mii_phys, mii_list)
38176830588dSPyun YongHyeon 		PHY_RESET(miisc);
38186830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno, MII_100T2CR, 0);
38196830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno,
38206830588dSPyun YongHyeon 	    MII_ANAR, ANAR_TX_FD | ANAR_TX | ANAR_10_FD | ANAR_10 | ANAR_CSMA);
38216830588dSPyun YongHyeon 	re_miibus_writereg(sc->rl_dev, phyno,
38226830588dSPyun YongHyeon 	    MII_BMCR, BMCR_AUTOEN | BMCR_STARTNEG);
38236830588dSPyun YongHyeon 	DELAY(1000);
38246830588dSPyun YongHyeon 	if (aneg != 0) {
38256830588dSPyun YongHyeon 		/*
38266830588dSPyun YongHyeon 		 * Poll link state until re(4) get a 10/100Mbps link.
38276830588dSPyun YongHyeon 		 */
38286830588dSPyun YongHyeon 		for (i = 0; i < MII_ANEGTICKS_GIGE; i++) {
38296830588dSPyun YongHyeon 			mii_pollstat(mii);
38306830588dSPyun YongHyeon 			if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID))
38316830588dSPyun YongHyeon 			    == (IFM_ACTIVE | IFM_AVALID)) {
38326830588dSPyun YongHyeon 				switch (IFM_SUBTYPE(mii->mii_media_active)) {
38336830588dSPyun YongHyeon 				case IFM_10_T:
38346830588dSPyun YongHyeon 				case IFM_100_TX:
38356830588dSPyun YongHyeon 					return;
38366830588dSPyun YongHyeon 				default:
38376830588dSPyun YongHyeon 					break;
38386830588dSPyun YongHyeon 				}
38396830588dSPyun YongHyeon 			}
38406830588dSPyun YongHyeon 			RL_UNLOCK(sc);
38416830588dSPyun YongHyeon 			pause("relnk", hz);
38426830588dSPyun YongHyeon 			RL_LOCK(sc);
38436830588dSPyun YongHyeon 		}
38446830588dSPyun YongHyeon 		if (i == MII_ANEGTICKS_GIGE)
38456830588dSPyun YongHyeon 			device_printf(sc->rl_dev,
38466830588dSPyun YongHyeon 			    "establishing a link failed, WOL may not work!");
38476830588dSPyun YongHyeon 	}
38486830588dSPyun YongHyeon 	/*
38496830588dSPyun YongHyeon 	 * No link, force MAC to have 100Mbps, full-duplex link.
38506830588dSPyun YongHyeon 	 * MAC does not require reprogramming on resolved speed/duplex,
38516830588dSPyun YongHyeon 	 * so this is just for completeness.
38526830588dSPyun YongHyeon 	 */
38536830588dSPyun YongHyeon 	mii->mii_media_status = IFM_AVALID | IFM_ACTIVE;
38546830588dSPyun YongHyeon 	mii->mii_media_active = IFM_ETHER | IFM_100_TX | IFM_FDX;
38556830588dSPyun YongHyeon }
38566830588dSPyun YongHyeon 
38576830588dSPyun YongHyeon static void
38587b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
38597467bd53SPyun YongHyeon {
38607467bd53SPyun YongHyeon 	struct ifnet		*ifp;
38617467bd53SPyun YongHyeon 	int			pmc;
38627467bd53SPyun YongHyeon 	uint16_t		pmstat;
38637467bd53SPyun YongHyeon 	uint8_t			v;
38647467bd53SPyun YongHyeon 
38657467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
38667467bd53SPyun YongHyeon 
38673b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
38687467bd53SPyun YongHyeon 		return;
38697467bd53SPyun YongHyeon 
38707467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
387161f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
387261f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
387361f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
387461f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
387561f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
387661f45a72SPyun YongHyeon 	}
3877fcb220acSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0) {
3878e9f8886eSMarius Strobl 		if ((sc->rl_flags & RL_FLAG_8168G_PLUS) != 0) {
3879e9f8886eSMarius Strobl 			/* Disable RXDV gate. */
3880e9f8886eSMarius Strobl 			CSR_WRITE_4(sc, RL_MISC, CSR_READ_4(sc, RL_MISC) &
3881e9f8886eSMarius Strobl 			    ~0x00080000);
3882e9f8886eSMarius Strobl 		}
3883fcb220acSPyun YongHyeon 		re_set_rxmode(sc);
38846830588dSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_WOL_MANLINK) != 0)
38856830588dSPyun YongHyeon 			re_set_linkspeed(sc);
3886fcb220acSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3887886ff602SPyun YongHyeon 			CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
3888fcb220acSPyun YongHyeon 	}
38897467bd53SPyun YongHyeon 	/* Enable config register write. */
38907467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
38917467bd53SPyun YongHyeon 
38927467bd53SPyun YongHyeon 	/* Enable PME. */
3893e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg1);
38947467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
38957467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
38967467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
3897e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg1, v);
38987467bd53SPyun YongHyeon 
3899e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg3);
39007467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
39017467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
39027467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
3903e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
39047467bd53SPyun YongHyeon 
3905e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg5);
390644f7cbf5SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST |
390744f7cbf5SPyun YongHyeon 	    RL_CFG5_WOL_LANWAKE);
39087467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
39097467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
39107467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
39117467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
39127467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
39137467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
3914e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
39157467bd53SPyun YongHyeon 
391644f7cbf5SPyun YongHyeon 	/* Config register write done. */
391744f7cbf5SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
391844f7cbf5SPyun YongHyeon 
3919bc6b129bSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) == 0 &&
3920d0c45156SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3921d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
39227467bd53SPyun YongHyeon 	/*
39237467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
39247467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
39257467bd53SPyun YongHyeon 	 * needed.
39267467bd53SPyun YongHyeon 	 */
39277467bd53SPyun YongHyeon 
39287467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
39297467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
39307467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
39317467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
39327467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
39337467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
39347467bd53SPyun YongHyeon }
39357467bd53SPyun YongHyeon 
39367467bd53SPyun YongHyeon static void
39377b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
39387467bd53SPyun YongHyeon {
39397467bd53SPyun YongHyeon 	int			pmc;
39407467bd53SPyun YongHyeon 	uint8_t			v;
39417467bd53SPyun YongHyeon 
39427467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
39437467bd53SPyun YongHyeon 
39443b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
39457467bd53SPyun YongHyeon 		return;
39467467bd53SPyun YongHyeon 
39477467bd53SPyun YongHyeon 	/* Enable config register write. */
39487467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
39497467bd53SPyun YongHyeon 
3950e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg3);
39517467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
3952e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg3, v);
39537467bd53SPyun YongHyeon 
39547467bd53SPyun YongHyeon 	/* Config register write done. */
3955f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
39567467bd53SPyun YongHyeon 
3957e7e7593cSPyun YongHyeon 	v = CSR_READ_1(sc, sc->rl_cfg5);
39587467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
39597467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
3960e7e7593cSPyun YongHyeon 	CSR_WRITE_1(sc, sc->rl_cfg5, v);
39617467bd53SPyun YongHyeon }
39620534aae0SPyun YongHyeon 
39630534aae0SPyun YongHyeon static void
39640534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc)
39650534aae0SPyun YongHyeon {
39660534aae0SPyun YongHyeon 	struct sysctl_ctx_list	*ctx;
39670534aae0SPyun YongHyeon 	struct sysctl_oid_list	*children;
3968502be0f7SPyun YongHyeon 	int			error;
39690534aae0SPyun YongHyeon 
39700534aae0SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->rl_dev);
39710534aae0SPyun YongHyeon 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
39720534aae0SPyun YongHyeon 
39730534aae0SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
39747029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
39757029da5cSPawel Biernacki 	    re_sysctl_stats, "I", "Statistics Information");
3976502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
3977502be0f7SPyun YongHyeon 		return;
3978502be0f7SPyun YongHyeon 
3979502be0f7SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod",
39807029da5cSPawel Biernacki 	    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT,
39817029da5cSPawel Biernacki 	    &sc->rl_int_rx_mod, 0, sysctl_hw_re_int_mod, "I",
39827029da5cSPawel Biernacki 	    "re RX interrupt moderation");
3983502be0f7SPyun YongHyeon 	/* Pull in device tunables. */
3984502be0f7SPyun YongHyeon 	sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3985502be0f7SPyun YongHyeon 	error = resource_int_value(device_get_name(sc->rl_dev),
3986502be0f7SPyun YongHyeon 	    device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod);
3987502be0f7SPyun YongHyeon 	if (error == 0) {
3988502be0f7SPyun YongHyeon 		if (sc->rl_int_rx_mod < RL_TIMER_MIN ||
3989502be0f7SPyun YongHyeon 		    sc->rl_int_rx_mod > RL_TIMER_MAX) {
3990502be0f7SPyun YongHyeon 			device_printf(sc->rl_dev, "int_rx_mod value out of "
3991502be0f7SPyun YongHyeon 			    "range; using default: %d\n",
3992502be0f7SPyun YongHyeon 			    RL_TIMER_DEFAULT);
3993502be0f7SPyun YongHyeon 			sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3994502be0f7SPyun YongHyeon 		}
3995502be0f7SPyun YongHyeon 	}
39960534aae0SPyun YongHyeon }
39970534aae0SPyun YongHyeon 
39980534aae0SPyun YongHyeon static int
39990534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS)
40000534aae0SPyun YongHyeon {
40010534aae0SPyun YongHyeon 	struct rl_softc		*sc;
40020534aae0SPyun YongHyeon 	struct rl_stats		*stats;
40030534aae0SPyun YongHyeon 	int			error, i, result;
40040534aae0SPyun YongHyeon 
40050534aae0SPyun YongHyeon 	result = -1;
40060534aae0SPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
40070534aae0SPyun YongHyeon 	if (error || req->newptr == NULL)
40080534aae0SPyun YongHyeon 		return (error);
40090534aae0SPyun YongHyeon 
40100534aae0SPyun YongHyeon 	if (result == 1) {
40110534aae0SPyun YongHyeon 		sc = (struct rl_softc *)arg1;
40120534aae0SPyun YongHyeon 		RL_LOCK(sc);
401316a4824bSPyun YongHyeon 		if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
401416a4824bSPyun YongHyeon 			RL_UNLOCK(sc);
401516a4824bSPyun YongHyeon 			goto done;
401616a4824bSPyun YongHyeon 		}
40170534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
40180534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
40190534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
40200534aae0SPyun YongHyeon 		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
40210534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
40220534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
40230534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
40240534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
40250534aae0SPyun YongHyeon 		    RL_DUMPSTATS_START));
40260534aae0SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
40270534aae0SPyun YongHyeon 			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
40280534aae0SPyun YongHyeon 			    RL_DUMPSTATS_START) == 0)
40290534aae0SPyun YongHyeon 				break;
40300534aae0SPyun YongHyeon 			DELAY(1000);
40310534aae0SPyun YongHyeon 		}
40320534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
40330534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
40340534aae0SPyun YongHyeon 		RL_UNLOCK(sc);
40350534aae0SPyun YongHyeon 		if (i == 0) {
40360534aae0SPyun YongHyeon 			device_printf(sc->rl_dev,
40370534aae0SPyun YongHyeon 			    "DUMP statistics request timed out\n");
40380534aae0SPyun YongHyeon 			return (ETIMEDOUT);
40390534aae0SPyun YongHyeon 		}
404016a4824bSPyun YongHyeon done:
40410534aae0SPyun YongHyeon 		stats = sc->rl_ldata.rl_stats;
40420534aae0SPyun YongHyeon 		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
40430534aae0SPyun YongHyeon 		printf("Tx frames : %ju\n",
40440534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_pkts));
40450534aae0SPyun YongHyeon 		printf("Rx frames : %ju\n",
40460534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_pkts));
40470534aae0SPyun YongHyeon 		printf("Tx errors : %ju\n",
40480534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_errs));
40490534aae0SPyun YongHyeon 		printf("Rx errors : %u\n",
40500534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_errs));
40510534aae0SPyun YongHyeon 		printf("Rx missed frames : %u\n",
40520534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_missed_pkts));
40530534aae0SPyun YongHyeon 		printf("Rx frame alignment errs : %u\n",
40540534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
40550534aae0SPyun YongHyeon 		printf("Tx single collisions : %u\n",
40560534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_onecoll));
40570534aae0SPyun YongHyeon 		printf("Tx multiple collisions : %u\n",
40580534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_multicolls));
40590534aae0SPyun YongHyeon 		printf("Rx unicast frames : %ju\n",
40600534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
40610534aae0SPyun YongHyeon 		printf("Rx broadcast frames : %ju\n",
40620534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
40630534aae0SPyun YongHyeon 		printf("Rx multicast frames : %u\n",
40640534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_mcasts));
40650534aae0SPyun YongHyeon 		printf("Tx aborts : %u\n",
40660534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_tx_aborts));
40670534aae0SPyun YongHyeon 		printf("Tx underruns : %u\n",
40680534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_underruns));
40690534aae0SPyun YongHyeon 	}
40700534aae0SPyun YongHyeon 
40710534aae0SPyun YongHyeon 	return (error);
40720534aae0SPyun YongHyeon }
4073502be0f7SPyun YongHyeon 
4074502be0f7SPyun YongHyeon static int
4075502be0f7SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
4076502be0f7SPyun YongHyeon {
4077502be0f7SPyun YongHyeon 	int error, value;
4078502be0f7SPyun YongHyeon 
4079502be0f7SPyun YongHyeon 	if (arg1 == NULL)
4080502be0f7SPyun YongHyeon 		return (EINVAL);
4081502be0f7SPyun YongHyeon 	value = *(int *)arg1;
4082502be0f7SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
4083502be0f7SPyun YongHyeon 	if (error || req->newptr == NULL)
4084502be0f7SPyun YongHyeon 		return (error);
4085502be0f7SPyun YongHyeon 	if (value < low || value > high)
4086502be0f7SPyun YongHyeon 		return (EINVAL);
4087502be0f7SPyun YongHyeon 	*(int *)arg1 = value;
4088502be0f7SPyun YongHyeon 
4089502be0f7SPyun YongHyeon 	return (0);
4090502be0f7SPyun YongHyeon }
4091502be0f7SPyun YongHyeon 
4092502be0f7SPyun YongHyeon static int
4093502be0f7SPyun YongHyeon sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)
4094502be0f7SPyun YongHyeon {
4095502be0f7SPyun YongHyeon 
4096502be0f7SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN,
4097502be0f7SPyun YongHyeon 	    RL_TIMER_MAX));
4098502be0f7SPyun YongHyeon }
4099306c97e2SMark Johnston 
41007790c8c1SConrad Meyer #ifdef DEBUGNET
4101306c97e2SMark Johnston static void
41027790c8c1SConrad Meyer re_debugnet_init(struct ifnet *ifp, int *nrxr, int *ncl, int *clsize)
4103306c97e2SMark Johnston {
4104306c97e2SMark Johnston 	struct rl_softc *sc;
4105306c97e2SMark Johnston 
4106306c97e2SMark Johnston 	sc = if_getsoftc(ifp);
4107306c97e2SMark Johnston 	RL_LOCK(sc);
4108306c97e2SMark Johnston 	*nrxr = sc->rl_ldata.rl_rx_desc_cnt;
41097790c8c1SConrad Meyer 	*ncl = DEBUGNET_MAX_IN_FLIGHT;
4110306c97e2SMark Johnston 	*clsize = (ifp->if_mtu > RL_MTU &&
4111306c97e2SMark Johnston 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0) ? MJUM9BYTES : MCLBYTES;
4112306c97e2SMark Johnston 	RL_UNLOCK(sc);
4113306c97e2SMark Johnston }
4114306c97e2SMark Johnston 
4115306c97e2SMark Johnston static void
41167790c8c1SConrad Meyer re_debugnet_event(struct ifnet *ifp __unused, enum debugnet_ev event __unused)
4117306c97e2SMark Johnston {
4118306c97e2SMark Johnston }
4119306c97e2SMark Johnston 
4120306c97e2SMark Johnston static int
41217790c8c1SConrad Meyer re_debugnet_transmit(struct ifnet *ifp, struct mbuf *m)
4122306c97e2SMark Johnston {
4123306c97e2SMark Johnston 	struct rl_softc *sc;
4124306c97e2SMark Johnston 	int error;
4125306c97e2SMark Johnston 
4126306c97e2SMark Johnston 	sc = if_getsoftc(ifp);
4127306c97e2SMark Johnston 	if ((if_getdrvflags(ifp) & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
4128306c97e2SMark Johnston 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
4129306c97e2SMark Johnston 		return (EBUSY);
4130306c97e2SMark Johnston 
4131306c97e2SMark Johnston 	error = re_encap(sc, &m);
4132306c97e2SMark Johnston 	if (error == 0)
4133306c97e2SMark Johnston 		re_start_tx(sc);
4134306c97e2SMark Johnston 	return (error);
4135306c97e2SMark Johnston }
4136306c97e2SMark Johnston 
4137306c97e2SMark Johnston static int
41387790c8c1SConrad Meyer re_debugnet_poll(struct ifnet *ifp, int count)
4139306c97e2SMark Johnston {
4140306c97e2SMark Johnston 	struct rl_softc *sc;
4141306c97e2SMark Johnston 	int error;
4142306c97e2SMark Johnston 
4143306c97e2SMark Johnston 	sc = if_getsoftc(ifp);
4144306c97e2SMark Johnston 	if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0 ||
4145306c97e2SMark Johnston 	    (sc->rl_flags & RL_FLAG_LINK) == 0)
4146306c97e2SMark Johnston 		return (EBUSY);
4147306c97e2SMark Johnston 
4148306c97e2SMark Johnston 	re_txeof(sc);
4149306c97e2SMark Johnston 	error = re_rxeof(sc, NULL);
4150306c97e2SMark Johnston 	if (error != 0 && error != EAGAIN)
4151306c97e2SMark Johnston 		return (error);
4152306c97e2SMark Johnston 	return (0);
4153306c97e2SMark Johnston }
41547790c8c1SConrad Meyer #endif /* DEBUGNET */
4155