xref: /freebsd/sys/dev/re/if_re.c (revision 44f7cbf5860cc711d89dee67b3f704c032c865e0)
1098ca2bdSWarner Losh /*-
2a94100faSBill Paul  * Copyright (c) 1997, 1998-2003
3a94100faSBill Paul  *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4a94100faSBill Paul  *
5a94100faSBill Paul  * Redistribution and use in source and binary forms, with or without
6a94100faSBill Paul  * modification, are permitted provided that the following conditions
7a94100faSBill Paul  * are met:
8a94100faSBill Paul  * 1. Redistributions of source code must retain the above copyright
9a94100faSBill Paul  *    notice, this list of conditions and the following disclaimer.
10a94100faSBill Paul  * 2. Redistributions in binary form must reproduce the above copyright
11a94100faSBill Paul  *    notice, this list of conditions and the following disclaimer in the
12a94100faSBill Paul  *    documentation and/or other materials provided with the distribution.
13a94100faSBill Paul  * 3. All advertising materials mentioning features or use of this software
14a94100faSBill Paul  *    must display the following acknowledgement:
15a94100faSBill Paul  *	This product includes software developed by Bill Paul.
16a94100faSBill Paul  * 4. Neither the name of the author nor the names of any co-contributors
17a94100faSBill Paul  *    may be used to endorse or promote products derived from this software
18a94100faSBill Paul  *    without specific prior written permission.
19a94100faSBill Paul  *
20a94100faSBill Paul  * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21a94100faSBill Paul  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22a94100faSBill Paul  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23a94100faSBill Paul  * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24a94100faSBill Paul  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25a94100faSBill Paul  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26a94100faSBill Paul  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27a94100faSBill Paul  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28a94100faSBill Paul  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29a94100faSBill Paul  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30a94100faSBill Paul  * THE POSSIBILITY OF SUCH DAMAGE.
31a94100faSBill Paul  */
32a94100faSBill Paul 
334dc52c32SDavid E. O'Brien #include <sys/cdefs.h>
344dc52c32SDavid E. O'Brien __FBSDID("$FreeBSD$");
354dc52c32SDavid E. O'Brien 
36a94100faSBill Paul /*
37ed510fb0SBill Paul  * RealTek 8139C+/8169/8169S/8110S/8168/8111/8101E PCI NIC driver
38a94100faSBill Paul  *
39a94100faSBill Paul  * Written by Bill Paul <wpaul@windriver.com>
40a94100faSBill Paul  * Senior Networking Software Engineer
41a94100faSBill Paul  * Wind River Systems
42a94100faSBill Paul  */
43a94100faSBill Paul 
44a94100faSBill Paul /*
45a94100faSBill Paul  * This driver is designed to support RealTek's next generation of
46a94100faSBill Paul  * 10/100 and 10/100/1000 PCI ethernet controllers. There are currently
47ed510fb0SBill Paul  * seven devices in this family: the RTL8139C+, the RTL8169, the RTL8169S,
48ed510fb0SBill Paul  * RTL8110S, the RTL8168, the RTL8111 and the RTL8101E.
49a94100faSBill Paul  *
50a94100faSBill Paul  * The 8139C+ is a 10/100 ethernet chip. It is backwards compatible
51a94100faSBill Paul  * with the older 8139 family, however it also supports a special
52a94100faSBill Paul  * C+ mode of operation that provides several new performance enhancing
53a94100faSBill Paul  * features. These include:
54a94100faSBill Paul  *
55a94100faSBill Paul  *	o Descriptor based DMA mechanism. Each descriptor represents
56a94100faSBill Paul  *	  a single packet fragment. Data buffers may be aligned on
57a94100faSBill Paul  *	  any byte boundary.
58a94100faSBill Paul  *
59a94100faSBill Paul  *	o 64-bit DMA
60a94100faSBill Paul  *
61a94100faSBill Paul  *	o TCP/IP checksum offload for both RX and TX
62a94100faSBill Paul  *
63a94100faSBill Paul  *	o High and normal priority transmit DMA rings
64a94100faSBill Paul  *
65a94100faSBill Paul  *	o VLAN tag insertion and extraction
66a94100faSBill Paul  *
67a94100faSBill Paul  *	o TCP large send (segmentation offload)
68a94100faSBill Paul  *
69a94100faSBill Paul  * Like the 8139, the 8139C+ also has a built-in 10/100 PHY. The C+
70a94100faSBill Paul  * programming API is fairly straightforward. The RX filtering, EEPROM
71a94100faSBill Paul  * access and PHY access is the same as it is on the older 8139 series
72a94100faSBill Paul  * chips.
73a94100faSBill Paul  *
74a94100faSBill Paul  * The 8169 is a 64-bit 10/100/1000 gigabit ethernet MAC. It has almost the
75a94100faSBill Paul  * same programming API and feature set as the 8139C+ with the following
76a94100faSBill Paul  * differences and additions:
77a94100faSBill Paul  *
78a94100faSBill Paul  *	o 1000Mbps mode
79a94100faSBill Paul  *
80a94100faSBill Paul  *	o Jumbo frames
81a94100faSBill Paul  *
82a94100faSBill Paul  *	o GMII and TBI ports/registers for interfacing with copper
83a94100faSBill Paul  *	  or fiber PHYs
84a94100faSBill Paul  *
85a94100faSBill Paul  *	o RX and TX DMA rings can have up to 1024 descriptors
86a94100faSBill Paul  *	  (the 8139C+ allows a maximum of 64)
87a94100faSBill Paul  *
88a94100faSBill Paul  *	o Slight differences in register layout from the 8139C+
89a94100faSBill Paul  *
90a94100faSBill Paul  * The TX start and timer interrupt registers are at different locations
91a94100faSBill Paul  * on the 8169 than they are on the 8139C+. Also, the status word in the
92a94100faSBill Paul  * RX descriptor has a slightly different bit layout. The 8169 does not
93a94100faSBill Paul  * have a built-in PHY. Most reference boards use a Marvell 88E1000 'Alaska'
94a94100faSBill Paul  * copper gigE PHY.
95a94100faSBill Paul  *
96a94100faSBill Paul  * The 8169S/8110S 10/100/1000 devices have built-in copper gigE PHYs
97a94100faSBill Paul  * (the 'S' stands for 'single-chip'). These devices have the same
98a94100faSBill Paul  * programming API as the older 8169, but also have some vendor-specific
99a94100faSBill Paul  * registers for the on-board PHY. The 8110S is a LAN-on-motherboard
100a94100faSBill Paul  * part designed to be pin-compatible with the RealTek 8100 10/100 chip.
101a94100faSBill Paul  *
102a94100faSBill Paul  * This driver takes advantage of the RX and TX checksum offload and
103a94100faSBill Paul  * VLAN tag insertion/extraction features. It also implements TX
104a94100faSBill Paul  * interrupt moderation using the timer interrupt registers, which
105a94100faSBill Paul  * significantly reduces TX interrupt load. There is also support
106a94100faSBill Paul  * for jumbo frames, however the 8169/8169S/8110S can not transmit
10722a11c96SJohn-Mark Gurney  * jumbo frames larger than 7440, so the max MTU possible with this
10822a11c96SJohn-Mark Gurney  * driver is 7422 bytes.
109a94100faSBill Paul  */
110a94100faSBill Paul 
111f0796cd2SGleb Smirnoff #ifdef HAVE_KERNEL_OPTION_HEADERS
112f0796cd2SGleb Smirnoff #include "opt_device_polling.h"
113f0796cd2SGleb Smirnoff #endif
114f0796cd2SGleb Smirnoff 
115a94100faSBill Paul #include <sys/param.h>
116a94100faSBill Paul #include <sys/endian.h>
117a94100faSBill Paul #include <sys/systm.h>
118a94100faSBill Paul #include <sys/sockio.h>
119a94100faSBill Paul #include <sys/mbuf.h>
120a94100faSBill Paul #include <sys/malloc.h>
121fe12f24bSPoul-Henning Kamp #include <sys/module.h>
122a94100faSBill Paul #include <sys/kernel.h>
123a94100faSBill Paul #include <sys/socket.h>
124ed510fb0SBill Paul #include <sys/lock.h>
125ed510fb0SBill Paul #include <sys/mutex.h>
1260534aae0SPyun YongHyeon #include <sys/sysctl.h>
127ed510fb0SBill Paul #include <sys/taskqueue.h>
128a94100faSBill Paul 
129a94100faSBill Paul #include <net/if.h>
130a94100faSBill Paul #include <net/if_arp.h>
131a94100faSBill Paul #include <net/ethernet.h>
132a94100faSBill Paul #include <net/if_dl.h>
133a94100faSBill Paul #include <net/if_media.h>
134fc74a9f9SBrooks Davis #include <net/if_types.h>
135a94100faSBill Paul #include <net/if_vlan_var.h>
136a94100faSBill Paul 
137a94100faSBill Paul #include <net/bpf.h>
138a94100faSBill Paul 
139a94100faSBill Paul #include <machine/bus.h>
140a94100faSBill Paul #include <machine/resource.h>
141a94100faSBill Paul #include <sys/bus.h>
142a94100faSBill Paul #include <sys/rman.h>
143a94100faSBill Paul 
144a94100faSBill Paul #include <dev/mii/mii.h>
145a94100faSBill Paul #include <dev/mii/miivar.h>
146a94100faSBill Paul 
147a94100faSBill Paul #include <dev/pci/pcireg.h>
148a94100faSBill Paul #include <dev/pci/pcivar.h>
149a94100faSBill Paul 
150d65abd66SPyun YongHyeon #include <pci/if_rlreg.h>
151d65abd66SPyun YongHyeon 
152a94100faSBill Paul MODULE_DEPEND(re, pci, 1, 1, 1);
153a94100faSBill Paul MODULE_DEPEND(re, ether, 1, 1, 1);
154a94100faSBill Paul MODULE_DEPEND(re, miibus, 1, 1, 1);
155a94100faSBill Paul 
156298bfdf3SWarner Losh /* "device miibus" required.  See GENERIC if you get errors here. */
157a94100faSBill Paul #include "miibus_if.h"
158a94100faSBill Paul 
1595774c5ffSPyun YongHyeon /* Tunables. */
160502be0f7SPyun YongHyeon static int intr_filter = 0;
161502be0f7SPyun YongHyeon TUNABLE_INT("hw.re.intr_filter", &intr_filter);
162c2d2e19cSPyun YongHyeon static int msi_disable = 0;
1635774c5ffSPyun YongHyeon TUNABLE_INT("hw.re.msi_disable", &msi_disable);
1644a58fd45SPyun YongHyeon static int msix_disable = 0;
1654a58fd45SPyun YongHyeon TUNABLE_INT("hw.re.msix_disable", &msix_disable);
1662c21710bSPyun YongHyeon static int prefer_iomap = 0;
1672c21710bSPyun YongHyeon TUNABLE_INT("hw.re.prefer_iomap", &prefer_iomap);
1685774c5ffSPyun YongHyeon 
169a94100faSBill Paul #define RE_CSUM_FEATURES    (CSUM_IP | CSUM_TCP | CSUM_UDP)
170a94100faSBill Paul 
171a94100faSBill Paul /*
172a94100faSBill Paul  * Various supported device vendors/types and their names.
173a94100faSBill Paul  */
174b3030306SMarius Strobl static const struct rl_type const re_devs[] = {
1759dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
17632aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
177caa19d50SPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_530T_REVC, 0,
178caa19d50SPyun YongHyeon 	    "D-Link DGE-530(T) Gigabit Ethernet Adapter" },
1799dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
180a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1819dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
18254899a96SPyun YongHyeon 	    "RealTek 810xE PCIe 10/100baseTX" },
1839dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
184d467ffaaSPyun YongHyeon 	    "RealTek 8168/8111 B/C/CP/D/DP/E/F PCIe Gigabit Ethernet" },
1859dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
186715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1879dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1882ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1899dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
190ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1919dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
19226390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1939dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
194dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
195a94100faSBill Paul };
196a94100faSBill Paul 
197b3030306SMarius Strobl static const struct rl_hwrev const re_hwrevs[] = {
19881eee0ebSPyun YongHyeon 	{ RL_HWREV_8139, RL_8139, "", RL_MTU },
19981eee0ebSPyun YongHyeon 	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
20081eee0ebSPyun YongHyeon 	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
20181eee0ebSPyun YongHyeon 	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
20281eee0ebSPyun YongHyeon 	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
20381eee0ebSPyun YongHyeon 	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
20481eee0ebSPyun YongHyeon 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
20581eee0ebSPyun YongHyeon 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
206ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
20781eee0ebSPyun YongHyeon 	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
20881eee0ebSPyun YongHyeon 	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
20981eee0ebSPyun YongHyeon 	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
21081eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
21181eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21281eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
21381eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21481eee0ebSPyun YongHyeon 	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
21581eee0ebSPyun YongHyeon 	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
21681eee0ebSPyun YongHyeon 	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
21781eee0ebSPyun YongHyeon 	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
21881eee0ebSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
21981eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
22081eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
22181eee0ebSPyun YongHyeon 	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
22239e69201SPyun YongHyeon 	{ RL_HWREV_8401E, RL_8169, "8401E", RL_MTU },
223a9e3362aSPyun YongHyeon 	{ RL_HWREV_8402, RL_8169, "8402", RL_MTU },
22454899a96SPyun YongHyeon 	{ RL_HWREV_8105E, RL_8169, "8105E", RL_MTU },
2256b0a8e04SPyun YongHyeon 	{ RL_HWREV_8105E_SPIN1, RL_8169, "8105E", RL_MTU },
226ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
227ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
22881eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
22981eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
23081eee0ebSPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
23181eee0ebSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
23281eee0ebSPyun YongHyeon 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
23381eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
23481eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
235d467ffaaSPyun YongHyeon 	{ RL_HWREV_8168F, RL_8169, "8168F/8111F", RL_JUMBO_MTU_9K},
236d56f7f52SPyun YongHyeon 	{ RL_HWREV_8411, RL_8169, "8411", RL_JUMBO_MTU_9K},
23781eee0ebSPyun YongHyeon 	{ 0, 0, NULL, 0 }
238a94100faSBill Paul };
239a94100faSBill Paul 
240a94100faSBill Paul static int re_probe		(device_t);
241a94100faSBill Paul static int re_attach		(device_t);
242a94100faSBill Paul static int re_detach		(device_t);
243a94100faSBill Paul 
244d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
245a94100faSBill Paul 
246a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
247a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
248d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
249d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
250d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
25181eee0ebSPyun YongHyeon static int re_jumbo_newbuf	(struct rl_softc *, int);
252a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
25381eee0ebSPyun YongHyeon static int re_jrx_list_init	(struct rl_softc *);
254a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
25522a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
25622a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
25722a11c96SJohn-Mark Gurney 				(struct mbuf *);
25822a11c96SJohn-Mark Gurney #endif
2591abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
260a94100faSBill Paul static void re_txeof		(struct rl_softc *);
26197b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2621abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2631abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
26497b9d4baSJohn-Mark Gurney #endif
265ef544f63SPaolo Pisati static int re_intr		(void *);
266502be0f7SPyun YongHyeon static void re_intr_msi		(void *);
267a94100faSBill Paul static void re_tick		(void *);
268ed510fb0SBill Paul static void re_int_task		(void *, int);
269a94100faSBill Paul static void re_start		(struct ifnet *);
270d180a66fSPyun YongHyeon static void re_start_locked	(struct ifnet *);
271a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
272a94100faSBill Paul static void re_init		(void *);
27397b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
274a94100faSBill Paul static void re_stop		(struct rl_softc *);
2751d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
276a94100faSBill Paul static int re_suspend		(device_t);
277a94100faSBill Paul static int re_resume		(device_t);
2786a087a87SPyun YongHyeon static int re_shutdown		(device_t);
279a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
280a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
281a94100faSBill Paul 
282a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
283a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
284ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
285a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
286a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
287a94100faSBill Paul 
288a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
289a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
290a94100faSBill Paul static void re_miibus_statchg	(device_t);
291a94100faSBill Paul 
29281eee0ebSPyun YongHyeon static void re_set_jumbo	(struct rl_softc *, int);
293ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
294a94100faSBill Paul static void re_reset		(struct rl_softc *);
2957467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2967467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
297a94100faSBill Paul 
298ed510fb0SBill Paul #ifdef RE_DIAG
299a94100faSBill Paul static int re_diag		(struct rl_softc *);
300ed510fb0SBill Paul #endif
301a94100faSBill Paul 
3020534aae0SPyun YongHyeon static void re_add_sysctls	(struct rl_softc *);
3030534aae0SPyun YongHyeon static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
304502be0f7SPyun YongHyeon static int sysctl_int_range	(SYSCTL_HANDLER_ARGS, int, int);
305502be0f7SPyun YongHyeon static int sysctl_hw_re_int_mod	(SYSCTL_HANDLER_ARGS);
3060534aae0SPyun YongHyeon 
307a94100faSBill Paul static device_method_t re_methods[] = {
308a94100faSBill Paul 	/* Device interface */
309a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
310a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
311a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
312a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
313a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
314a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
315a94100faSBill Paul 
316a94100faSBill Paul 	/* MII interface */
317a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
318a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
319a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
320a94100faSBill Paul 
3214b7ec270SMarius Strobl 	DEVMETHOD_END
322a94100faSBill Paul };
323a94100faSBill Paul 
324a94100faSBill Paul static driver_t re_driver = {
325a94100faSBill Paul 	"re",
326a94100faSBill Paul 	re_methods,
327a94100faSBill Paul 	sizeof(struct rl_softc)
328a94100faSBill Paul };
329a94100faSBill Paul 
330a94100faSBill Paul static devclass_t re_devclass;
331a94100faSBill Paul 
332a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
333a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
334a94100faSBill Paul 
335a94100faSBill Paul #define EE_SET(x)					\
336a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
337a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
338a94100faSBill Paul 
339a94100faSBill Paul #define EE_CLR(x)					\
340a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
341a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
342a94100faSBill Paul 
343a94100faSBill Paul /*
344a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
345a94100faSBill Paul  */
346a94100faSBill Paul static void
3477b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
348a94100faSBill Paul {
3490ce0868aSPyun YongHyeon 	int			d, i;
350a94100faSBill Paul 
351ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
352a94100faSBill Paul 
353a94100faSBill Paul 	/*
354a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
355a94100faSBill Paul 	 */
356ed510fb0SBill Paul 
357ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
358a94100faSBill Paul 		if (d & i) {
359a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
360a94100faSBill Paul 		} else {
361a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
362a94100faSBill Paul 		}
363a94100faSBill Paul 		DELAY(100);
364a94100faSBill Paul 		EE_SET(RL_EE_CLK);
365a94100faSBill Paul 		DELAY(150);
366a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
367a94100faSBill Paul 		DELAY(100);
368a94100faSBill Paul 	}
369a94100faSBill Paul }
370a94100faSBill Paul 
371a94100faSBill Paul /*
372a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
373a94100faSBill Paul  */
374a94100faSBill Paul static void
3757b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
376a94100faSBill Paul {
3770ce0868aSPyun YongHyeon 	int			i;
378a94100faSBill Paul 	u_int16_t		word = 0;
379a94100faSBill Paul 
380a94100faSBill Paul 	/*
381a94100faSBill Paul 	 * Send address of word we want to read.
382a94100faSBill Paul 	 */
383a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
384a94100faSBill Paul 
385a94100faSBill Paul 	/*
386a94100faSBill Paul 	 * Start reading bits from EEPROM.
387a94100faSBill Paul 	 */
388a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
389a94100faSBill Paul 		EE_SET(RL_EE_CLK);
390a94100faSBill Paul 		DELAY(100);
391a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
392a94100faSBill Paul 			word |= i;
393a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
394a94100faSBill Paul 		DELAY(100);
395a94100faSBill Paul 	}
396a94100faSBill Paul 
397a94100faSBill Paul 	*dest = word;
398a94100faSBill Paul }
399a94100faSBill Paul 
400a94100faSBill Paul /*
401a94100faSBill Paul  * Read a sequence of words from the EEPROM.
402a94100faSBill Paul  */
403a94100faSBill Paul static void
4047b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
405a94100faSBill Paul {
406a94100faSBill Paul 	int			i;
407a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
408a94100faSBill Paul 
409ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
410ed510fb0SBill Paul 
411ed510fb0SBill Paul         DELAY(100);
412ed510fb0SBill Paul 
413a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
414ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
415a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
416ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
417a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
418be099007SPyun YongHyeon                 *ptr = word;
419a94100faSBill Paul 	}
420ed510fb0SBill Paul 
421ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
422a94100faSBill Paul }
423a94100faSBill Paul 
424a94100faSBill Paul static int
4257b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
426a94100faSBill Paul {
427a94100faSBill Paul 	struct rl_softc		*sc;
428a94100faSBill Paul 	u_int32_t		rval;
429a94100faSBill Paul 	int			i;
430a94100faSBill Paul 
431a94100faSBill Paul 	sc = device_get_softc(dev);
432a94100faSBill Paul 
4339bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4349bac70b8SBill Paul 
4359bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4369bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4379bac70b8SBill Paul 		return (rval);
4389bac70b8SBill Paul 	}
4399bac70b8SBill Paul 
440a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
441a94100faSBill Paul 
44296b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
443a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
444a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
445a94100faSBill Paul 			break;
4462bc085c6SPyun YongHyeon 		DELAY(25);
447a94100faSBill Paul 	}
448a94100faSBill Paul 
44996b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4506b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
451a94100faSBill Paul 		return (0);
452a94100faSBill Paul 	}
453a94100faSBill Paul 
4542bc085c6SPyun YongHyeon 	/*
4552bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4562bc085c6SPyun YongHyeon 	 */
4572bc085c6SPyun YongHyeon 	DELAY(20);
4582bc085c6SPyun YongHyeon 
459a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
460a94100faSBill Paul }
461a94100faSBill Paul 
462a94100faSBill Paul static int
4637b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
464a94100faSBill Paul {
465a94100faSBill Paul 	struct rl_softc		*sc;
466a94100faSBill Paul 	u_int32_t		rval;
467a94100faSBill Paul 	int			i;
468a94100faSBill Paul 
469a94100faSBill Paul 	sc = device_get_softc(dev);
470a94100faSBill Paul 
471a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4729bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
473a94100faSBill Paul 
47496b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
475a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
476a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
477a94100faSBill Paul 			break;
4782bc085c6SPyun YongHyeon 		DELAY(25);
479a94100faSBill Paul 	}
480a94100faSBill Paul 
48196b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4826b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
483a94100faSBill Paul 		return (0);
484a94100faSBill Paul 	}
485a94100faSBill Paul 
4862bc085c6SPyun YongHyeon 	/*
4872bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4882bc085c6SPyun YongHyeon 	 */
4892bc085c6SPyun YongHyeon 	DELAY(20);
4902bc085c6SPyun YongHyeon 
491a94100faSBill Paul 	return (0);
492a94100faSBill Paul }
493a94100faSBill Paul 
494a94100faSBill Paul static int
4957b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
496a94100faSBill Paul {
497a94100faSBill Paul 	struct rl_softc		*sc;
498a94100faSBill Paul 	u_int16_t		rval = 0;
499a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
500a94100faSBill Paul 
501a94100faSBill Paul 	sc = device_get_softc(dev);
502a94100faSBill Paul 
503a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
504a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
505a94100faSBill Paul 		return (rval);
506a94100faSBill Paul 	}
507a94100faSBill Paul 
508a94100faSBill Paul 	switch (reg) {
509a94100faSBill Paul 	case MII_BMCR:
510a94100faSBill Paul 		re8139_reg = RL_BMCR;
511a94100faSBill Paul 		break;
512a94100faSBill Paul 	case MII_BMSR:
513a94100faSBill Paul 		re8139_reg = RL_BMSR;
514a94100faSBill Paul 		break;
515a94100faSBill Paul 	case MII_ANAR:
516a94100faSBill Paul 		re8139_reg = RL_ANAR;
517a94100faSBill Paul 		break;
518a94100faSBill Paul 	case MII_ANER:
519a94100faSBill Paul 		re8139_reg = RL_ANER;
520a94100faSBill Paul 		break;
521a94100faSBill Paul 	case MII_ANLPAR:
522a94100faSBill Paul 		re8139_reg = RL_LPAR;
523a94100faSBill Paul 		break;
524a94100faSBill Paul 	case MII_PHYIDR1:
525a94100faSBill Paul 	case MII_PHYIDR2:
526a94100faSBill Paul 		return (0);
527a94100faSBill Paul 	/*
528a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
529a94100faSBill Paul 	 * register. If we have a link partner which does not
530a94100faSBill Paul 	 * support NWAY, this is the register which will tell
531a94100faSBill Paul 	 * us the results of parallel detection.
532a94100faSBill Paul 	 */
533a94100faSBill Paul 	case RL_MEDIASTAT:
534a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
535a94100faSBill Paul 		return (rval);
536a94100faSBill Paul 	default:
5376b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
538a94100faSBill Paul 		return (0);
539a94100faSBill Paul 	}
540a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
541baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
542baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
543baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
544baa12772SPyun YongHyeon 	}
545a94100faSBill Paul 	return (rval);
546a94100faSBill Paul }
547a94100faSBill Paul 
548a94100faSBill Paul static int
5497b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
550a94100faSBill Paul {
551a94100faSBill Paul 	struct rl_softc		*sc;
552a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
553a94100faSBill Paul 	int			rval = 0;
554a94100faSBill Paul 
555a94100faSBill Paul 	sc = device_get_softc(dev);
556a94100faSBill Paul 
557a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
558a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
559a94100faSBill Paul 		return (rval);
560a94100faSBill Paul 	}
561a94100faSBill Paul 
562a94100faSBill Paul 	switch (reg) {
563a94100faSBill Paul 	case MII_BMCR:
564a94100faSBill Paul 		re8139_reg = RL_BMCR;
565baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
566baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
567baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
568baa12772SPyun YongHyeon 		}
569a94100faSBill Paul 		break;
570a94100faSBill Paul 	case MII_BMSR:
571a94100faSBill Paul 		re8139_reg = RL_BMSR;
572a94100faSBill Paul 		break;
573a94100faSBill Paul 	case MII_ANAR:
574a94100faSBill Paul 		re8139_reg = RL_ANAR;
575a94100faSBill Paul 		break;
576a94100faSBill Paul 	case MII_ANER:
577a94100faSBill Paul 		re8139_reg = RL_ANER;
578a94100faSBill Paul 		break;
579a94100faSBill Paul 	case MII_ANLPAR:
580a94100faSBill Paul 		re8139_reg = RL_LPAR;
581a94100faSBill Paul 		break;
582a94100faSBill Paul 	case MII_PHYIDR1:
583a94100faSBill Paul 	case MII_PHYIDR2:
584a94100faSBill Paul 		return (0);
585a94100faSBill Paul 		break;
586a94100faSBill Paul 	default:
5876b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
588a94100faSBill Paul 		return (0);
589a94100faSBill Paul 	}
590a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
591a94100faSBill Paul 	return (0);
592a94100faSBill Paul }
593a94100faSBill Paul 
594a94100faSBill Paul static void
5957b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
596a94100faSBill Paul {
597130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
598130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
599130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
600a11e2f18SBruce M Simpson 
601130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
602130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
603130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
604130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
605130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
606130b6dfbSPyun YongHyeon 		return;
607130b6dfbSPyun YongHyeon 
608130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
609130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
610130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
611130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
612130b6dfbSPyun YongHyeon 		case IFM_10_T:
613130b6dfbSPyun YongHyeon 		case IFM_100_TX:
614130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
615130b6dfbSPyun YongHyeon 			break;
616130b6dfbSPyun YongHyeon 		case IFM_1000_T:
617130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
618130b6dfbSPyun YongHyeon 				break;
619130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
620130b6dfbSPyun YongHyeon 			break;
621130b6dfbSPyun YongHyeon 		default:
622130b6dfbSPyun YongHyeon 			break;
623130b6dfbSPyun YongHyeon 		}
624130b6dfbSPyun YongHyeon 	}
625130b6dfbSPyun YongHyeon 	/*
626130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
627130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
628130b6dfbSPyun YongHyeon 	 * parameters.
629130b6dfbSPyun YongHyeon 	 */
630a94100faSBill Paul }
631a94100faSBill Paul 
632a94100faSBill Paul /*
633ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
634a94100faSBill Paul  */
635a94100faSBill Paul static void
636ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
637a94100faSBill Paul {
638a94100faSBill Paul 	struct ifnet		*ifp;
639a94100faSBill Paul 	struct ifmultiaddr	*ifma;
640ff191365SJung-uk Kim 	uint32_t		hashes[2] = { 0, 0 };
641ff191365SJung-uk Kim 	uint32_t		h, rxfilt;
642a94100faSBill Paul 
64397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
64497b9d4baSJohn-Mark Gurney 
645fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
646a94100faSBill Paul 
647ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
648a94100faSBill Paul 
649ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6507c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6517c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
652a0637caaSPyun YongHyeon 		/*
653a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
654a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
655a0637caaSPyun YongHyeon 		 * promiscuous mode.
656a0637caaSPyun YongHyeon 		 */
657a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
658ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
659ff191365SJung-uk Kim 		goto done;
660a94100faSBill Paul 	}
661a94100faSBill Paul 
662eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
663a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
664a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
665a94100faSBill Paul 			continue;
6660e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6670e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
668a94100faSBill Paul 		if (h < 32)
669a94100faSBill Paul 			hashes[0] |= (1 << h);
670a94100faSBill Paul 		else
671a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
672a94100faSBill Paul 	}
673eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
674a94100faSBill Paul 
675ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
676bb7dfefbSBill Paul 		/*
677ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
678ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
679ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
680ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
681ff191365SJung-uk Kim 		 * devices.
682bb7dfefbSBill Paul 		 */
683aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
684ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
685ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
686ff191365SJung-uk Kim 			hashes[1] = h;
687ff191365SJung-uk Kim 		}
688ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
689ff191365SJung-uk Kim 	}
690ff191365SJung-uk Kim 
691ff191365SJung-uk Kim done:
692a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
693a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
694ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
695bb7dfefbSBill Paul }
696a94100faSBill Paul 
697a94100faSBill Paul static void
6987b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
699a94100faSBill Paul {
7000ce0868aSPyun YongHyeon 	int			i;
701a94100faSBill Paul 
70297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
70397b9d4baSJohn-Mark Gurney 
704a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
705a94100faSBill Paul 
706a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
707a94100faSBill Paul 		DELAY(10);
708a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
709a94100faSBill Paul 			break;
710a94100faSBill Paul 	}
711a94100faSBill Paul 	if (i == RL_TIMEOUT)
7126b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
713a94100faSBill Paul 
714566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
715a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
71681eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
717566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
718a94100faSBill Paul }
719a94100faSBill Paul 
720ed510fb0SBill Paul #ifdef RE_DIAG
721ed510fb0SBill Paul 
722a94100faSBill Paul /*
723a94100faSBill Paul  * The following routine is designed to test for a defect on some
724a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
725a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
726a94100faSBill Paul  * should be pulled high. The result of this defect is that the
727a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
728a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
729a94100faSBill Paul  * because the 64-bit data lines aren't connected.
730a94100faSBill Paul  *
731a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
732a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
733a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
734a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
735a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
736a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
737a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
738a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
739a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
740a94100faSBill Paul  */
741a94100faSBill Paul 
742a94100faSBill Paul static int
7437b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
744a94100faSBill Paul {
745fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
746a94100faSBill Paul 	struct mbuf		*m0;
747a94100faSBill Paul 	struct ether_header	*eh;
748a94100faSBill Paul 	struct rl_desc		*cur_rx;
749a94100faSBill Paul 	u_int16_t		status;
750a94100faSBill Paul 	u_int32_t		rxstat;
751ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
752a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
753a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
754a94100faSBill Paul 
755a94100faSBill Paul 	/* Allocate a single mbuf */
756a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
757a94100faSBill Paul 	if (m0 == NULL)
758a94100faSBill Paul 		return (ENOBUFS);
759a94100faSBill Paul 
76097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
76197b9d4baSJohn-Mark Gurney 
762a94100faSBill Paul 	/*
763a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
764a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
765a94100faSBill Paul 	 * following special functions:
766a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
767a94100faSBill Paul 	 * - Enables digital loopback mode
768a94100faSBill Paul 	 * - Leaves interrupts turned off
769a94100faSBill Paul 	 */
770a94100faSBill Paul 
771a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
772a94100faSBill Paul 	sc->rl_testmode = 1;
7738476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
77497b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
775351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
776ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
777ed510fb0SBill Paul 		phyaddr = 1;
778ed510fb0SBill Paul 	else
779ed510fb0SBill Paul 		phyaddr = 0;
780ed510fb0SBill Paul 
781ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
782ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
783ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
784ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
785ed510fb0SBill Paul 			break;
786ed510fb0SBill Paul 	}
787ed510fb0SBill Paul 
788ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
789ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
790ed510fb0SBill Paul 
791804af9a1SBill Paul 	DELAY(100000);
792a94100faSBill Paul 
793a94100faSBill Paul 	/* Put some data in the mbuf */
794a94100faSBill Paul 
795a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
796a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
797a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
798a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
799a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
800a94100faSBill Paul 
8017cae6651SBill Paul 	/*
8027cae6651SBill Paul 	 * Queue the packet, start transmission.
8037cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
8047cae6651SBill Paul 	 */
805a94100faSBill Paul 
806abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
80797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
80852732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8097cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
81097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
811a94100faSBill Paul 	m0 = NULL;
812a94100faSBill Paul 
813a94100faSBill Paul 	/* Wait for it to propagate through the chip */
814a94100faSBill Paul 
815abc8ff44SBill Paul 	DELAY(100000);
816a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
817a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
818ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
819abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
820abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
821a94100faSBill Paul 			break;
822a94100faSBill Paul 		DELAY(10);
823a94100faSBill Paul 	}
824a94100faSBill Paul 
825a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8266b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8276b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8286b9f5c94SGleb Smirnoff 		    " loopback mode\n");
829a94100faSBill Paul 		error = EIO;
830a94100faSBill Paul 		goto done;
831a94100faSBill Paul 	}
832a94100faSBill Paul 
833a94100faSBill Paul 	/*
834a94100faSBill Paul 	 * The packet should have been dumped into the first
835a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
836a94100faSBill Paul 	 */
837a94100faSBill Paul 
838a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
839a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
840a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
841d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
842d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
843d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
844d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
845d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
846a94100faSBill Paul 
847d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
848d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
849a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
850a94100faSBill Paul 
851a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
852a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
853a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
854a94100faSBill Paul 
855a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8566b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8576b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
858a94100faSBill Paul 		error = EIO;
859a94100faSBill Paul 		goto done;
860a94100faSBill Paul 	}
861a94100faSBill Paul 
862a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
863a94100faSBill Paul 
864a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
865a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
866a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8676b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8686b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
869a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8706b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
871a94100faSBill Paul 		    eh->ether_dhost, ":", eh->ether_shost, ":",
872a94100faSBill Paul 		    ntohs(eh->ether_type));
8736b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8746b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8756b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8766b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8776b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8786b9f5c94SGleb Smirnoff 		    "details.\n");
879a94100faSBill Paul 		error = EIO;
880a94100faSBill Paul 	}
881a94100faSBill Paul 
882a94100faSBill Paul done:
883a94100faSBill Paul 	/* Turn interface off, release resources */
884a94100faSBill Paul 
885a94100faSBill Paul 	sc->rl_testmode = 0;
886351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
887a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
888a94100faSBill Paul 	re_stop(sc);
889a94100faSBill Paul 	if (m0 != NULL)
890a94100faSBill Paul 		m_freem(m0);
891a94100faSBill Paul 
89297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
89397b9d4baSJohn-Mark Gurney 
894a94100faSBill Paul 	return (error);
895a94100faSBill Paul }
896a94100faSBill Paul 
897ed510fb0SBill Paul #endif
898ed510fb0SBill Paul 
899a94100faSBill Paul /*
900a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
901a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
902a94100faSBill Paul  */
903a94100faSBill Paul static int
9047b5ffebfSPyun YongHyeon re_probe(device_t dev)
905a94100faSBill Paul {
906b3030306SMarius Strobl 	const struct rl_type	*t;
907dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
908dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
909dfdb409eSPyun YongHyeon 	int			i;
910a94100faSBill Paul 
911dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
912dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
913dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
914dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
915a94100faSBill Paul 
916dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
917dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
91826390635SJohn Baldwin 			/*
91926390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
920dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
92126390635SJohn Baldwin 			 */
922a94100faSBill Paul 			return (ENXIO);
923a94100faSBill Paul 		}
924dfdb409eSPyun YongHyeon 	}
925dfdb409eSPyun YongHyeon 
926dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
927dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
928dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
929dfdb409eSPyun YongHyeon 			return (ENXIO);
930dfdb409eSPyun YongHyeon 		}
931dfdb409eSPyun YongHyeon 	}
932dfdb409eSPyun YongHyeon 
933dfdb409eSPyun YongHyeon 	t = re_devs;
934dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
935dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
936a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
937d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
938a94100faSBill Paul 		}
939a94100faSBill Paul 	}
940a94100faSBill Paul 
941a94100faSBill Paul 	return (ENXIO);
942a94100faSBill Paul }
943a94100faSBill Paul 
944a94100faSBill Paul /*
945a94100faSBill Paul  * Map a single buffer address.
946a94100faSBill Paul  */
947a94100faSBill Paul 
948a94100faSBill Paul static void
9497b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
950a94100faSBill Paul {
9518fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
952a94100faSBill Paul 
953a94100faSBill Paul 	if (error)
954a94100faSBill Paul 		return;
955a94100faSBill Paul 
956a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
957a94100faSBill Paul 	addr = arg;
958a94100faSBill Paul 	*addr = segs->ds_addr;
959a94100faSBill Paul }
960a94100faSBill Paul 
961a94100faSBill Paul static int
9627b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
963a94100faSBill Paul {
96466366ca4SPyun YongHyeon 	bus_addr_t		lowaddr;
965d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
966a94100faSBill Paul 	int			error;
967a94100faSBill Paul 	int			i;
968a94100faSBill Paul 
969d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
970d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
971d65abd66SPyun YongHyeon 
972d65abd66SPyun YongHyeon 	/*
973d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
974ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
975ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
976ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
977ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
97866366ca4SPyun YongHyeon 	 * may not have the limitation.
979d65abd66SPyun YongHyeon 	 */
98066366ca4SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
98166366ca4SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
98266366ca4SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
983d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
98466366ca4SPyun YongHyeon 	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
985d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
986d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
987d65abd66SPyun YongHyeon 	if (error) {
988d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
989d65abd66SPyun YongHyeon 		return (error);
990d65abd66SPyun YongHyeon 	}
991d65abd66SPyun YongHyeon 
992d65abd66SPyun YongHyeon 	/*
993d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
994d65abd66SPyun YongHyeon 	 */
995d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
996d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
997d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
998d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
999d65abd66SPyun YongHyeon 	if (error) {
1000d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
1001d65abd66SPyun YongHyeon 		return (error);
1002d65abd66SPyun YongHyeon 	}
1003d65abd66SPyun YongHyeon 
1004a94100faSBill Paul 	/*
1005a94100faSBill Paul 	 * Allocate map for RX mbufs.
1006a94100faSBill Paul 	 */
1007d65abd66SPyun YongHyeon 
100881eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
100981eee0ebSPyun YongHyeon 		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
101081eee0ebSPyun YongHyeon 		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
101181eee0ebSPyun YongHyeon 		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
101281eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_mtag);
101381eee0ebSPyun YongHyeon 		if (error) {
101481eee0ebSPyun YongHyeon 			device_printf(dev,
101581eee0ebSPyun YongHyeon 			    "could not allocate jumbo RX DMA tag\n");
101681eee0ebSPyun YongHyeon 			return (error);
101781eee0ebSPyun YongHyeon 		}
101881eee0ebSPyun YongHyeon 	}
1019d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1020d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1021d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1022a94100faSBill Paul 	if (error) {
1023d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1024d65abd66SPyun YongHyeon 		return (error);
1025a94100faSBill Paul 	}
1026a94100faSBill Paul 
1027a94100faSBill Paul 	/*
1028a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1029a94100faSBill Paul 	 */
1030a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1031a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1032d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1033a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1034a94100faSBill Paul 	if (error) {
1035d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1036d65abd66SPyun YongHyeon 		return (error);
1037a94100faSBill Paul 	}
1038a94100faSBill Paul 
1039a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1040a94100faSBill Paul 
1041a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1042d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1043d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1044a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1045d65abd66SPyun YongHyeon 	if (error) {
1046d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1047d65abd66SPyun YongHyeon 		return (error);
1048d65abd66SPyun YongHyeon 	}
1049a94100faSBill Paul 
1050a94100faSBill Paul 	/* Load the map for the TX ring. */
1051a94100faSBill Paul 
1052d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1053a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1054a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1055d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1056a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1057d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1058d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1059d65abd66SPyun YongHyeon 		return (ENOMEM);
1060d65abd66SPyun YongHyeon 	}
1061a94100faSBill Paul 
1062a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1063a94100faSBill Paul 
1064d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1065d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1066d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1067a94100faSBill Paul 		if (error) {
1068d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1069d65abd66SPyun YongHyeon 			return (error);
1070a94100faSBill Paul 		}
1071a94100faSBill Paul 	}
1072a94100faSBill Paul 
1073a94100faSBill Paul 	/*
1074a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1075a94100faSBill Paul 	 */
1076a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1077a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1078d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1079a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1080a94100faSBill Paul 	if (error) {
1081d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1082d65abd66SPyun YongHyeon 		return (error);
1083a94100faSBill Paul 	}
1084a94100faSBill Paul 
1085a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1086a94100faSBill Paul 
1087a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1088d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1089d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1090a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1091d65abd66SPyun YongHyeon 	if (error) {
1092d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1093d65abd66SPyun YongHyeon 		return (error);
1094d65abd66SPyun YongHyeon 	}
1095a94100faSBill Paul 
1096a94100faSBill Paul 	/* Load the map for the RX ring. */
1097a94100faSBill Paul 
1098d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1099a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1100a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1101d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1102a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1103d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1104d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1105d65abd66SPyun YongHyeon 		return (ENOMEM);
1106d65abd66SPyun YongHyeon 	}
1107a94100faSBill Paul 
1108a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1109a94100faSBill Paul 
111081eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
111181eee0ebSPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
111281eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_sparemap);
111381eee0ebSPyun YongHyeon 		if (error) {
111481eee0ebSPyun YongHyeon 			device_printf(dev,
111581eee0ebSPyun YongHyeon 			    "could not create spare DMA map for jumbo RX\n");
111681eee0ebSPyun YongHyeon 			return (error);
111781eee0ebSPyun YongHyeon 		}
111881eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
111981eee0ebSPyun YongHyeon 			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
112081eee0ebSPyun YongHyeon 			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
112181eee0ebSPyun YongHyeon 			if (error) {
112281eee0ebSPyun YongHyeon 				device_printf(dev,
112381eee0ebSPyun YongHyeon 				    "could not create DMA map for jumbo RX\n");
112481eee0ebSPyun YongHyeon 				return (error);
112581eee0ebSPyun YongHyeon 			}
112681eee0ebSPyun YongHyeon 		}
112781eee0ebSPyun YongHyeon 	}
1128d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1129d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1130a94100faSBill Paul 	if (error) {
1131d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1132d65abd66SPyun YongHyeon 		return (error);
1133d65abd66SPyun YongHyeon 	}
1134d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1135d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1136d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1137d65abd66SPyun YongHyeon 		if (error) {
1138d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1139d65abd66SPyun YongHyeon 			return (error);
1140a94100faSBill Paul 		}
1141a94100faSBill Paul 	}
1142a94100faSBill Paul 
11430534aae0SPyun YongHyeon 	/* Create DMA map for statistics. */
11440534aae0SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
11450534aae0SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
11460534aae0SPyun YongHyeon 	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
11470534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_stag);
11480534aae0SPyun YongHyeon 	if (error) {
11490534aae0SPyun YongHyeon 		device_printf(dev, "could not create statistics DMA tag\n");
11500534aae0SPyun YongHyeon 		return (error);
11510534aae0SPyun YongHyeon 	}
11520534aae0SPyun YongHyeon 	/* Allocate DMA'able memory for statistics. */
11530534aae0SPyun YongHyeon 	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
11540534aae0SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_stats,
11550534aae0SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
11560534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_smap);
11570534aae0SPyun YongHyeon 	if (error) {
11580534aae0SPyun YongHyeon 		device_printf(dev,
11590534aae0SPyun YongHyeon 		    "could not allocate statistics DMA memory\n");
11600534aae0SPyun YongHyeon 		return (error);
11610534aae0SPyun YongHyeon 	}
11620534aae0SPyun YongHyeon 	/* Load the map for statistics. */
11630534aae0SPyun YongHyeon 	sc->rl_ldata.rl_stats_addr = 0;
11640534aae0SPyun YongHyeon 	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
11650534aae0SPyun YongHyeon 	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
11660534aae0SPyun YongHyeon 	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
11670534aae0SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
11680534aae0SPyun YongHyeon 		device_printf(dev, "could not load statistics DMA memory\n");
11690534aae0SPyun YongHyeon 		return (ENOMEM);
11700534aae0SPyun YongHyeon 	}
11710534aae0SPyun YongHyeon 
1172a94100faSBill Paul 	return (0);
1173a94100faSBill Paul }
1174a94100faSBill Paul 
1175a94100faSBill Paul /*
1176a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1177a94100faSBill Paul  * setup and ethernet/BPF attach.
1178a94100faSBill Paul  */
1179a94100faSBill Paul static int
11807b5ffebfSPyun YongHyeon re_attach(device_t dev)
1181a94100faSBill Paul {
1182a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1183be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1184a94100faSBill Paul 	struct rl_softc		*sc;
1185a94100faSBill Paul 	struct ifnet		*ifp;
1186b3030306SMarius Strobl 	const struct rl_hwrev	*hw_rev;
1187017f1c8dSPyun YongHyeon 	u_int32_t		cap, ctl;
1188a94100faSBill Paul 	int			hwrev;
1189ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
11908e5d93dbSMarius Strobl 	int			error = 0, i, phy, rid;
11914a58fd45SPyun YongHyeon 	int			msic, msixc, reg;
119203ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1193a94100faSBill Paul 
1194a94100faSBill Paul 	sc = device_get_softc(dev);
1195ed510fb0SBill Paul 	sc->rl_dev = dev;
1196a94100faSBill Paul 
1197a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
119897b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1199d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1200d1754a9bSJohn Baldwin 
1201a94100faSBill Paul 	/*
1202a94100faSBill Paul 	 * Map control/status registers.
1203a94100faSBill Paul 	 */
1204a94100faSBill Paul 	pci_enable_busmaster(dev);
1205a94100faSBill Paul 
1206ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
12072c21710bSPyun YongHyeon 	/*
12082c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
12092c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
12102c21710bSPyun YongHyeon 	 * is used always activate io mapping.
12112c21710bSPyun YongHyeon 	 */
12122c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
12132c21710bSPyun YongHyeon 		prefer_iomap = 1;
12142c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1215ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1216ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1217ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1218ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1219ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
12202c21710bSPyun YongHyeon 	} else {
12212c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
12222c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
12232c21710bSPyun YongHyeon 	}
1224ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1225ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
12262c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1227ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1228ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1229ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1230ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
12312c21710bSPyun YongHyeon 	}
1232ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1233d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1234a94100faSBill Paul 		error = ENXIO;
1235a94100faSBill Paul 		goto fail;
1236a94100faSBill Paul 	}
1237a94100faSBill Paul 
1238a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1239a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1240a94100faSBill Paul 
12415774c5ffSPyun YongHyeon 	msic = pci_msi_count(dev);
12424a58fd45SPyun YongHyeon 	msixc = pci_msix_count(dev);
1243017f1c8dSPyun YongHyeon 	if (pci_find_cap(dev, PCIY_EXPRESS, &reg) == 0) {
12444a58fd45SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
1245017f1c8dSPyun YongHyeon 		sc->rl_expcap = reg;
1246017f1c8dSPyun YongHyeon 	}
12474a58fd45SPyun YongHyeon 	if (bootverbose) {
12485774c5ffSPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
12494a58fd45SPyun YongHyeon 		device_printf(dev, "MSI-X count : %d\n", msixc);
12505774c5ffSPyun YongHyeon 	}
12514a58fd45SPyun YongHyeon 	if (msix_disable > 0)
12524a58fd45SPyun YongHyeon 		msixc = 0;
12534a58fd45SPyun YongHyeon 	if (msi_disable > 0)
12544a58fd45SPyun YongHyeon 		msic = 0;
12554a58fd45SPyun YongHyeon 	/* Prefer MSI-X to MSI. */
12564a58fd45SPyun YongHyeon 	if (msixc > 0) {
12574a58fd45SPyun YongHyeon 		msixc = 1;
12584a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
12594a58fd45SPyun YongHyeon 		sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
12604a58fd45SPyun YongHyeon 		    &rid, RF_ACTIVE);
12614a58fd45SPyun YongHyeon 		if (sc->rl_res_pba == NULL) {
12624a58fd45SPyun YongHyeon 			device_printf(sc->rl_dev,
12634a58fd45SPyun YongHyeon 			    "could not allocate MSI-X PBA resource\n");
12644a58fd45SPyun YongHyeon 		}
12654a58fd45SPyun YongHyeon 		if (sc->rl_res_pba != NULL &&
12664a58fd45SPyun YongHyeon 		    pci_alloc_msix(dev, &msixc) == 0) {
12674a58fd45SPyun YongHyeon 			if (msixc == 1) {
12684a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI-X message\n",
12694a58fd45SPyun YongHyeon 				    msixc);
12704a58fd45SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSIX;
12714a58fd45SPyun YongHyeon 			} else
12724a58fd45SPyun YongHyeon 				pci_release_msi(dev);
12734a58fd45SPyun YongHyeon 		}
12744a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
12754a58fd45SPyun YongHyeon 			if (sc->rl_res_pba != NULL)
12764a58fd45SPyun YongHyeon 				bus_release_resource(dev, SYS_RES_MEMORY, rid,
12774a58fd45SPyun YongHyeon 				    sc->rl_res_pba);
12784a58fd45SPyun YongHyeon 			sc->rl_res_pba = NULL;
12794a58fd45SPyun YongHyeon 			msixc = 0;
12804a58fd45SPyun YongHyeon 		}
12814a58fd45SPyun YongHyeon 	}
12824a58fd45SPyun YongHyeon 	/* Prefer MSI to INTx. */
12834a58fd45SPyun YongHyeon 	if (msixc == 0 && msic > 0) {
1284f1bb696aSPyun YongHyeon 		msic = 1;
12855774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
12865774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
12874a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI message\n",
12885774c5ffSPyun YongHyeon 				    msic);
1289351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1290339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1291339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1292339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1293339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1294339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1295f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
12965774c5ffSPyun YongHyeon 			} else
12975774c5ffSPyun YongHyeon 				pci_release_msi(dev);
12985774c5ffSPyun YongHyeon 		}
12994a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSI) == 0)
13004a58fd45SPyun YongHyeon 			msic = 0;
13015774c5ffSPyun YongHyeon 	}
1302a94100faSBill Paul 
13035774c5ffSPyun YongHyeon 	/* Allocate interrupt */
13044a58fd45SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
13055774c5ffSPyun YongHyeon 		rid = 0;
13065774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
13075774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
13085774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
13095774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1310a94100faSBill Paul 			error = ENXIO;
1311a94100faSBill Paul 			goto fail;
1312a94100faSBill Paul 		}
13135774c5ffSPyun YongHyeon 	} else {
13145774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
13155774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
13165774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
13175774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
13185774c5ffSPyun YongHyeon 				device_printf(dev,
13195774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
13205774c5ffSPyun YongHyeon 				    "message %d\n", rid);
13215774c5ffSPyun YongHyeon 				error = ENXIO;
13225774c5ffSPyun YongHyeon 				goto fail;
13235774c5ffSPyun YongHyeon 			}
13245774c5ffSPyun YongHyeon 		}
13255774c5ffSPyun YongHyeon 	}
1326a94100faSBill Paul 
13274d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
13284d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
13294d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
13304d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
13314d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
13324d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
13334d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
13344d2bf239SPyun YongHyeon 		}
13354d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13364d2bf239SPyun YongHyeon 	}
13374d2bf239SPyun YongHyeon 
1338017f1c8dSPyun YongHyeon 	/* Disable ASPM L0S/L1. */
1339017f1c8dSPyun YongHyeon 	if (sc->rl_expcap != 0) {
1340017f1c8dSPyun YongHyeon 		cap = pci_read_config(dev, sc->rl_expcap +
1341017f1c8dSPyun YongHyeon 		    PCIR_EXPRESS_LINK_CAP, 2);
1342017f1c8dSPyun YongHyeon 		if ((cap & PCIM_LINK_CAP_ASPM) != 0) {
1343017f1c8dSPyun YongHyeon 			ctl = pci_read_config(dev, sc->rl_expcap +
1344017f1c8dSPyun YongHyeon 			    PCIR_EXPRESS_LINK_CTL, 2);
1345017f1c8dSPyun YongHyeon 			if ((ctl & 0x0003) != 0) {
1346017f1c8dSPyun YongHyeon 				ctl &= ~0x0003;
1347017f1c8dSPyun YongHyeon 				pci_write_config(dev, sc->rl_expcap +
1348017f1c8dSPyun YongHyeon 				    PCIR_EXPRESS_LINK_CTL, ctl, 2);
1349017f1c8dSPyun YongHyeon 				device_printf(dev, "ASPM disabled\n");
1350017f1c8dSPyun YongHyeon 			}
1351017f1c8dSPyun YongHyeon 		} else
1352017f1c8dSPyun YongHyeon 			device_printf(dev, "no ASPM capability\n");
1353017f1c8dSPyun YongHyeon 	}
1354017f1c8dSPyun YongHyeon 
1355abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1356a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1357566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1358566ca8caSJung-uk Kim 	case 0x00000000:
1359566ca8caSJung-uk Kim 	case 0x10000000:
1360566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1361566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1362566ca8caSJung-uk Kim 		break;
1363566ca8caSJung-uk Kim 	default:
1364a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1365a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1366566ca8caSJung-uk Kim 		break;
1367566ca8caSJung-uk Kim 	}
1368566ca8caSJung-uk Kim 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1369abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1370abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1371abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
137281eee0ebSPyun YongHyeon 			sc->rl_hwrev = hw_rev;
1373abc8ff44SBill Paul 			break;
1374abc8ff44SBill Paul 		}
1375abc8ff44SBill Paul 		hw_rev++;
1376abc8ff44SBill Paul 	}
1377d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1378a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1379d65abd66SPyun YongHyeon 		error = ENXIO;
1380d65abd66SPyun YongHyeon 		goto fail;
1381d65abd66SPyun YongHyeon 	}
1382abc8ff44SBill Paul 
1383351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1384351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
138581eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1386351a76f9SPyun YongHyeon 		break;
1387351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1388351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
138981eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1390351a76f9SPyun YongHyeon 		break;
1391b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1392b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
13933d22427cSTai-hwa Liang 	case RL_HWREV_8102EL_SPIN1:
139481eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
139581eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
139681eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1397b1d62f0fSPyun YongHyeon 		break;
13988281a098SPyun YongHyeon 	case RL_HWREV_8103E:
139981eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
140081eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
140181eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
14028281a098SPyun YongHyeon 		break;
140339e69201SPyun YongHyeon 	case RL_HWREV_8401E:
1404a9e3362aSPyun YongHyeon 	case RL_HWREV_8402:
140554899a96SPyun YongHyeon 	case RL_HWREV_8105E:
14066b0a8e04SPyun YongHyeon 	case RL_HWREV_8105E_SPIN1:
140754899a96SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
140854899a96SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
140954899a96SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
141054899a96SPyun YongHyeon 		break;
1411ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN1:
1412ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN2:
1413886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1414886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1415ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN3:
1416aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1417deb5c680SPyun YongHyeon 		break;
1418deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
141961f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
142061f45a72SPyun YongHyeon 		/* FALLTHROUGH */
142161f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
142261f45a72SPyun YongHyeon 		if ((hwrev & 0x00700000) == 0x00200000)
142361f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
142461f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1425deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
142659ef640dSPyun YongHyeon 	case RL_HWREV_8168D:
14275fa06abeSPyun YongHyeon 	case RL_HWREV_8168DP:
1428aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1429f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
143081eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1431351a76f9SPyun YongHyeon 		break;
1432d0c45156SPyun YongHyeon 	case RL_HWREV_8168E:
1433d0c45156SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1434d0c45156SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
143581eee0ebSPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1436d0c45156SPyun YongHyeon 		break;
1437f0431c5bSPyun YongHyeon 	case RL_HWREV_8168E_VL:
1438d467ffaaSPyun YongHyeon 	case RL_HWREV_8168F:
1439d56f7f52SPyun YongHyeon 	case RL_HWREV_8411:
1440f0431c5bSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1441f0431c5bSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
144281eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1443f0431c5bSPyun YongHyeon 		break;
1444566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1445566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1446566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1447566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1448566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1449566ca8caSJung-uk Kim 		/* FALLTHROUGH */
14500596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
14510596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1452566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1453566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1454351a76f9SPyun YongHyeon 		break;
1455351a76f9SPyun YongHyeon 	default:
1456351a76f9SPyun YongHyeon 		break;
1457351a76f9SPyun YongHyeon 	}
1458351a76f9SPyun YongHyeon 
145993252626SPyun YongHyeon 	/* Reset the adapter. */
146093252626SPyun YongHyeon 	RL_LOCK(sc);
146193252626SPyun YongHyeon 	re_reset(sc);
146293252626SPyun YongHyeon 	RL_UNLOCK(sc);
146393252626SPyun YongHyeon 
1464deb5c680SPyun YongHyeon 	/* Enable PME. */
1465deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1466deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1467deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1468deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1469deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1470deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1471deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1472deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1473deb5c680SPyun YongHyeon 
1474deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1475deb5c680SPyun YongHyeon 		/*
1476deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1477deb5c680SPyun YongHyeon 		 * address from EEPROM.
1478deb5c680SPyun YongHyeon 		 */
1479deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1480deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1481deb5c680SPyun YongHyeon 	} else {
1482141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1483ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1484a94100faSBill Paul 		if (re_did != 0x8129)
1485141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1486a94100faSBill Paul 
1487a94100faSBill Paul 		/*
1488a94100faSBill Paul 		 * Get station address from the EEPROM.
1489a94100faSBill Paul 		 */
1490ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1491be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1492be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1493be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1494deb5c680SPyun YongHyeon 	}
1495ed510fb0SBill Paul 
1496ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1497d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1498ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1499ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1500d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1501d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1502ed510fb0SBill Paul 	} else {
1503d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1504ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1505ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1506d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1507d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1508abc8ff44SBill Paul 	}
15099bac70b8SBill Paul 
1510a94100faSBill Paul 	error = re_allocmem(dev, sc);
1511a94100faSBill Paul 	if (error)
1512a94100faSBill Paul 		goto fail;
15130534aae0SPyun YongHyeon 	re_add_sysctls(sc);
1514a94100faSBill Paul 
1515cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1516cd036ec1SBrooks Davis 	if (ifp == NULL) {
1517d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1518cd036ec1SBrooks Davis 		error = ENOSPC;
1519cd036ec1SBrooks Davis 		goto fail;
1520cd036ec1SBrooks Davis 	}
1521cd036ec1SBrooks Davis 
152261f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
152361f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
152461f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
152561f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
152661f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
152761f45a72SPyun YongHyeon 		else
152861f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
152961f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
153061f45a72SPyun YongHyeon 	}
153161f45a72SPyun YongHyeon 
1532351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
153339e69201SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0) {
1534d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
153539e69201SPyun YongHyeon 		if (hw_rev->rl_rev == RL_HWREV_8401E)
153639e69201SPyun YongHyeon 			CSR_WRITE_1(sc, 0xD1, CSR_READ_1(sc, 0xD1) & ~0x08);
153739e69201SPyun YongHyeon 	}
1538351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1539351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1540351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1541351a76f9SPyun YongHyeon 	}
1542351a76f9SPyun YongHyeon 
15438e5d93dbSMarius Strobl #define	RE_PHYAD_INTERNAL	 0
15448e5d93dbSMarius Strobl 
15458e5d93dbSMarius Strobl 	/* Do MII setup. */
15468e5d93dbSMarius Strobl 	phy = RE_PHYAD_INTERNAL;
15478e5d93dbSMarius Strobl 	if (sc->rl_type == RL_8169)
15488e5d93dbSMarius Strobl 		phy = 1;
15498e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
155064436f6eSPyun YongHyeon 	    re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
15518e5d93dbSMarius Strobl 	if (error != 0) {
15528e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
1553a94100faSBill Paul 		goto fail;
1554a94100faSBill Paul 	}
1555a94100faSBill Paul 
1556a94100faSBill Paul 	ifp->if_softc = sc;
15579bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1558a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1559a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1560a94100faSBill Paul 	ifp->if_start = re_start;
1561bc2a1002SPyun YongHyeon 	/*
1562bc2a1002SPyun YongHyeon 	 * RTL8168/8111C generates wrong IP checksummed frame if the
1563bc2a1002SPyun YongHyeon 	 * packet has IP options so disable TX IP checksum offloading.
1564bc2a1002SPyun YongHyeon 	 */
1565bc2a1002SPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168C ||
1566bc2a1002SPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8168C_SPIN2)
1567bc2a1002SPyun YongHyeon 		ifp->if_hwassist = CSUM_TCP | CSUM_UDP;
1568bc2a1002SPyun YongHyeon 	else
1569bc2a1002SPyun YongHyeon 		ifp->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
1570bc2a1002SPyun YongHyeon 	ifp->if_hwassist |= CSUM_TSO;
1571d6d7d923SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1572498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1573a94100faSBill Paul 	ifp->if_init = re_init;
157452732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
157552732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
157652732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1577a94100faSBill Paul 
1578ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1579ed510fb0SBill Paul 
1580a94100faSBill Paul 	/*
1581a94100faSBill Paul 	 * Call MI attach routine.
1582a94100faSBill Paul 	 */
1583a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1584a94100faSBill Paul 
1585960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1586960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1587960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1588960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
15897467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
15903b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &reg) == 0)
15917467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1592960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1593*44f7cbf5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_WOL_UCAST | IFCAP_WOL_MCAST);
1594a2a8420cSPyun YongHyeon 	/*
1595f9ad4da7SPyun YongHyeon 	 * Don't enable TSO by default.  It is known to generate
1596f9ad4da7SPyun YongHyeon 	 * corrupted TCP segments(bad TCP options) under certain
1597f9ad4da7SPyun YongHyeon 	 * circumtances.
1598a2a8420cSPyun YongHyeon 	 */
1599a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1600ecafbbb5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1601960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1602960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1603960fd5b3SPyun YongHyeon #endif
1604960fd5b3SPyun YongHyeon 	/*
1605960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1606960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1607960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1608960fd5b3SPyun YongHyeon 	 */
1609960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1610960fd5b3SPyun YongHyeon 
1611ed510fb0SBill Paul #ifdef RE_DIAG
1612ed510fb0SBill Paul 	/*
1613ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1614ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1615ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1616ed510fb0SBill Paul 	 */
1617a94100faSBill Paul 
1618ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1619ed510fb0SBill Paul 		error = re_diag(sc);
1620a94100faSBill Paul 		if (error) {
1621ed510fb0SBill Paul 			device_printf(dev,
1622ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1623a94100faSBill Paul 			ether_ifdetach(ifp);
1624a94100faSBill Paul 			goto fail;
1625a94100faSBill Paul 		}
1626ed510fb0SBill Paul 	}
1627ed510fb0SBill Paul #endif
1628a94100faSBill Paul 
1629502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
1630502be0f7SPyun YongHyeon 	intr_filter = 1;
1631502be0f7SPyun YongHyeon #endif
1632a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1633502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
1634502be0f7SPyun YongHyeon 	    intr_filter == 0) {
1635502be0f7SPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
1636502be0f7SPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc,
1637502be0f7SPyun YongHyeon 		    &sc->rl_intrhand[0]);
1638502be0f7SPyun YongHyeon 	} else {
16395774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
16405774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
16415774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
16425774c5ffSPyun YongHyeon 	}
1643a94100faSBill Paul 	if (error) {
1644d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1645a94100faSBill Paul 		ether_ifdetach(ifp);
1646a94100faSBill Paul 	}
1647a94100faSBill Paul 
1648a94100faSBill Paul fail:
1649ed510fb0SBill Paul 
1650a94100faSBill Paul 	if (error)
1651a94100faSBill Paul 		re_detach(dev);
1652a94100faSBill Paul 
1653a94100faSBill Paul 	return (error);
1654a94100faSBill Paul }
1655a94100faSBill Paul 
1656a94100faSBill Paul /*
1657a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1658a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1659a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1660a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1661a94100faSBill Paul  * allocated.
1662a94100faSBill Paul  */
1663a94100faSBill Paul static int
16647b5ffebfSPyun YongHyeon re_detach(device_t dev)
1665a94100faSBill Paul {
1666a94100faSBill Paul 	struct rl_softc		*sc;
1667a94100faSBill Paul 	struct ifnet		*ifp;
16685774c5ffSPyun YongHyeon 	int			i, rid;
1669a94100faSBill Paul 
1670a94100faSBill Paul 	sc = device_get_softc(dev);
1671fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1672aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
167397b9d4baSJohn-Mark Gurney 
167481cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
167581cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
167640929967SGleb Smirnoff #ifdef DEVICE_POLLING
167740929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
167840929967SGleb Smirnoff 			ether_poll_deregister(ifp);
167940929967SGleb Smirnoff #endif
168097b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
168197b9d4baSJohn-Mark Gurney #if 0
168297b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
168397b9d4baSJohn-Mark Gurney #endif
1684a94100faSBill Paul 		re_stop(sc);
1685525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1686d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
16873d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
1688a94100faSBill Paul 		/*
1689a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1690a94100faSBill Paul 		 * still had a BPF descriptor attached to this
169197b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1692a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1693a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1694a94100faSBill Paul 		 * which will try to call re_init() again. This will
1695a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1696a94100faSBill Paul 		 * which will panic the system when the kernel tries
1697a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1698a94100faSBill Paul 		 * anymore.
1699a94100faSBill Paul 		 */
1700a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1701525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1702a94100faSBill Paul 	}
1703a94100faSBill Paul 	if (sc->rl_miibus)
1704a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1705a94100faSBill Paul 	bus_generic_detach(dev);
1706a94100faSBill Paul 
170797b9d4baSJohn-Mark Gurney 	/*
170897b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
170997b9d4baSJohn-Mark Gurney 	 * stopped here.
171097b9d4baSJohn-Mark Gurney 	 */
171197b9d4baSJohn-Mark Gurney 
1712502be0f7SPyun YongHyeon 	if (sc->rl_intrhand[0] != NULL) {
1713502be0f7SPyun YongHyeon 		bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
1714502be0f7SPyun YongHyeon 		sc->rl_intrhand[0] = NULL;
17155774c5ffSPyun YongHyeon 	}
1716ad4f426eSWarner Losh 	if (ifp != NULL)
1717ad4f426eSWarner Losh 		if_free(ifp);
1718502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
1719502be0f7SPyun YongHyeon 		rid = 0;
1720502be0f7SPyun YongHyeon 	else
1721502be0f7SPyun YongHyeon 		rid = 1;
17225774c5ffSPyun YongHyeon 	if (sc->rl_irq[0] != NULL) {
1723502be0f7SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]);
17245774c5ffSPyun YongHyeon 		sc->rl_irq[0] = NULL;
17255774c5ffSPyun YongHyeon 	}
1726502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0)
17275774c5ffSPyun YongHyeon 		pci_release_msi(dev);
17284a58fd45SPyun YongHyeon 	if (sc->rl_res_pba) {
17294a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
17304a58fd45SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
17314a58fd45SPyun YongHyeon 	}
1732a94100faSBill Paul 	if (sc->rl_res)
1733ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1734ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1735a94100faSBill Paul 
1736a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1737a94100faSBill Paul 
1738a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
17390534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map)
1740a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1741a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
17420534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list)
1743a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1744a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list,
1745a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
1746a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1747a94100faSBill Paul 	}
1748a94100faSBill Paul 
1749a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1750a94100faSBill Paul 
1751a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
17520534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map)
1753a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1754a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
17550534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list)
1756a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1757a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list,
1758a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
1759a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1760a94100faSBill Paul 	}
1761a94100faSBill Paul 
1762a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1763a94100faSBill Paul 
1764d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
17659e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
17669e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1767d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1768d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
17699e18005dSPyun YongHyeon 		}
1770d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1771d65abd66SPyun YongHyeon 	}
1772d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
17739e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
17749e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1775d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1776d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
17779e18005dSPyun YongHyeon 		}
1778d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1779d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1780d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1781d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1782a94100faSBill Paul 	}
178381eee0ebSPyun YongHyeon 	if (sc->rl_ldata.rl_jrx_mtag) {
178481eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
178581eee0ebSPyun YongHyeon 			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
178681eee0ebSPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
178781eee0ebSPyun YongHyeon 				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
178881eee0ebSPyun YongHyeon 		}
178981eee0ebSPyun YongHyeon 		if (sc->rl_ldata.rl_jrx_sparemap)
179081eee0ebSPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
179181eee0ebSPyun YongHyeon 			    sc->rl_ldata.rl_jrx_sparemap);
179281eee0ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
179381eee0ebSPyun YongHyeon 	}
1794a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1795a94100faSBill Paul 
1796a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
17970534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap)
1798a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1799a94100faSBill Paul 			    sc->rl_ldata.rl_smap);
18000534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats)
18010534aae0SPyun YongHyeon 			bus_dmamem_free(sc->rl_ldata.rl_stag,
18020534aae0SPyun YongHyeon 			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1803a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1804a94100faSBill Paul 	}
1805a94100faSBill Paul 
1806a94100faSBill Paul 	if (sc->rl_parent_tag)
1807a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1808a94100faSBill Paul 
1809a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1810a94100faSBill Paul 
1811a94100faSBill Paul 	return (0);
1812a94100faSBill Paul }
1813a94100faSBill Paul 
1814d65abd66SPyun YongHyeon static __inline void
18157b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1816a94100faSBill Paul {
1817d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1818d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1819d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1820a94100faSBill Paul 
182181eee0ebSPyun YongHyeon 	if (sc->rl_ifp->if_mtu > RL_MTU &&
182281eee0ebSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
182381eee0ebSPyun YongHyeon 		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
182481eee0ebSPyun YongHyeon 	else
1825d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1826d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1827d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1828d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1829d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1830d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1831d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1832d65abd66SPyun YongHyeon }
1833d65abd66SPyun YongHyeon 
1834d65abd66SPyun YongHyeon static int
18357b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1836d65abd66SPyun YongHyeon {
1837d65abd66SPyun YongHyeon 	struct mbuf		*m;
1838d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1839d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1840d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1841d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1842d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1843d65abd66SPyun YongHyeon 	int			error, nsegs;
1844d65abd66SPyun YongHyeon 
1845d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1846d65abd66SPyun YongHyeon 	if (m == NULL)
1847a94100faSBill Paul 		return (ENOBUFS);
1848a94100faSBill Paul 
1849a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
185022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
185122a11c96SJohn-Mark Gurney 	/*
185222a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
185322a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
185422a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
185522a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
185622a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
185722a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
185822a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
185922a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
186022a11c96SJohn-Mark Gurney 	 */
186122a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
186222a11c96SJohn-Mark Gurney #endif
1863d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1864d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1865d65abd66SPyun YongHyeon 	if (error != 0) {
1866d65abd66SPyun YongHyeon 		m_freem(m);
1867d65abd66SPyun YongHyeon 		return (ENOBUFS);
1868d65abd66SPyun YongHyeon 	}
1869d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1870a94100faSBill Paul 
1871d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1872d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1873d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1874d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1875d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1876a94100faSBill Paul 	}
1877a94100faSBill Paul 
1878d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1879d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1880d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1881d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1882d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1883d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1884a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1885a94100faSBill Paul 
1886d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1887d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1888d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1889d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1890d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1891d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1892d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1893d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1894d65abd66SPyun YongHyeon 
1895a94100faSBill Paul 	return (0);
1896a94100faSBill Paul }
1897a94100faSBill Paul 
189881eee0ebSPyun YongHyeon static int
189981eee0ebSPyun YongHyeon re_jumbo_newbuf(struct rl_softc *sc, int idx)
190081eee0ebSPyun YongHyeon {
190181eee0ebSPyun YongHyeon 	struct mbuf		*m;
190281eee0ebSPyun YongHyeon 	struct rl_rxdesc	*rxd;
190381eee0ebSPyun YongHyeon 	bus_dma_segment_t	segs[1];
190481eee0ebSPyun YongHyeon 	bus_dmamap_t		map;
190581eee0ebSPyun YongHyeon 	struct rl_desc		*desc;
190681eee0ebSPyun YongHyeon 	uint32_t		cmdstat;
190781eee0ebSPyun YongHyeon 	int			error, nsegs;
190881eee0ebSPyun YongHyeon 
190981eee0ebSPyun YongHyeon 	m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
191081eee0ebSPyun YongHyeon 	if (m == NULL)
191181eee0ebSPyun YongHyeon 		return (ENOBUFS);
191281eee0ebSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
191381eee0ebSPyun YongHyeon #ifdef RE_FIXUP_RX
191481eee0ebSPyun YongHyeon 	m_adj(m, RE_ETHER_ALIGN);
191581eee0ebSPyun YongHyeon #endif
191681eee0ebSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
191781eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
191881eee0ebSPyun YongHyeon 	if (error != 0) {
191981eee0ebSPyun YongHyeon 		m_freem(m);
192081eee0ebSPyun YongHyeon 		return (ENOBUFS);
192181eee0ebSPyun YongHyeon 	}
192281eee0ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
192381eee0ebSPyun YongHyeon 
192481eee0ebSPyun YongHyeon 	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
192581eee0ebSPyun YongHyeon 	if (rxd->rx_m != NULL) {
192681eee0ebSPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
192781eee0ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
192881eee0ebSPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
192981eee0ebSPyun YongHyeon 	}
193081eee0ebSPyun YongHyeon 
193181eee0ebSPyun YongHyeon 	rxd->rx_m = m;
193281eee0ebSPyun YongHyeon 	map = rxd->rx_dmamap;
193381eee0ebSPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
193481eee0ebSPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
193581eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_jrx_sparemap = map;
193681eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
193781eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
193881eee0ebSPyun YongHyeon 
193981eee0ebSPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
194081eee0ebSPyun YongHyeon 	desc->rl_vlanctl = 0;
194181eee0ebSPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
194281eee0ebSPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
194381eee0ebSPyun YongHyeon 	cmdstat = segs[0].ds_len;
194481eee0ebSPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
194581eee0ebSPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
194681eee0ebSPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
194781eee0ebSPyun YongHyeon 
194881eee0ebSPyun YongHyeon 	return (0);
194981eee0ebSPyun YongHyeon }
195081eee0ebSPyun YongHyeon 
195122a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
195222a11c96SJohn-Mark Gurney static __inline void
19537b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
195422a11c96SJohn-Mark Gurney {
195522a11c96SJohn-Mark Gurney 	int                     i;
195622a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
195722a11c96SJohn-Mark Gurney 
195822a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
195922a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
196022a11c96SJohn-Mark Gurney 
196122a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
196222a11c96SJohn-Mark Gurney 		*dst++ = *src++;
196322a11c96SJohn-Mark Gurney 
196422a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
196522a11c96SJohn-Mark Gurney }
196622a11c96SJohn-Mark Gurney #endif
196722a11c96SJohn-Mark Gurney 
1968a94100faSBill Paul static int
19697b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
1970a94100faSBill Paul {
1971d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1972d65abd66SPyun YongHyeon 	int			i;
197397b9d4baSJohn-Mark Gurney 
197497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
197597b9d4baSJohn-Mark Gurney 
1976d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1977d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1978d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1979d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1980d65abd66SPyun YongHyeon 	/* Set EOR. */
1981d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1982d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1983a94100faSBill Paul 
1984a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1985d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1986d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1987d65abd66SPyun YongHyeon 
1988a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1989a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1990d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1991a94100faSBill Paul 
1992a94100faSBill Paul 	return (0);
1993a94100faSBill Paul }
1994a94100faSBill Paul 
1995a94100faSBill Paul static int
19967b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
1997a94100faSBill Paul {
1998d65abd66SPyun YongHyeon 	int			error, i;
1999a94100faSBill Paul 
2000d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
2001d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
2002d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
2003d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
2004d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
2005d65abd66SPyun YongHyeon 			return (error);
2006a94100faSBill Paul 	}
2007a94100faSBill Paul 
2008a94100faSBill Paul 	/* Flush the RX descriptors */
2009a94100faSBill Paul 
2010a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2011a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2012a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2013a94100faSBill Paul 
2014a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
2015a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
2016502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
2017a94100faSBill Paul 
2018a94100faSBill Paul 	return (0);
2019a94100faSBill Paul }
2020a94100faSBill Paul 
202181eee0ebSPyun YongHyeon static int
202281eee0ebSPyun YongHyeon re_jrx_list_init(struct rl_softc *sc)
202381eee0ebSPyun YongHyeon {
202481eee0ebSPyun YongHyeon 	int			error, i;
202581eee0ebSPyun YongHyeon 
202681eee0ebSPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
202781eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
202881eee0ebSPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
202981eee0ebSPyun YongHyeon 		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
203081eee0ebSPyun YongHyeon 		if ((error = re_jumbo_newbuf(sc, i)) != 0)
203181eee0ebSPyun YongHyeon 			return (error);
203281eee0ebSPyun YongHyeon 	}
203381eee0ebSPyun YongHyeon 
203481eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
203581eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_list_map,
203681eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
203781eee0ebSPyun YongHyeon 
203881eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_rx_prodidx = 0;
203981eee0ebSPyun YongHyeon 	sc->rl_head = sc->rl_tail = NULL;
2040502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
204181eee0ebSPyun YongHyeon 
204281eee0ebSPyun YongHyeon 	return (0);
204381eee0ebSPyun YongHyeon }
204481eee0ebSPyun YongHyeon 
2045a94100faSBill Paul /*
2046a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
2047a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
2048a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
2049a94100faSBill Paul  */
2050ed510fb0SBill Paul static int
20511abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2052a94100faSBill Paul {
2053a94100faSBill Paul 	struct mbuf		*m;
2054a94100faSBill Paul 	struct ifnet		*ifp;
205581eee0ebSPyun YongHyeon 	int			i, rxerr, total_len;
2056a94100faSBill Paul 	struct rl_desc		*cur_rx;
2057a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
205881eee0ebSPyun YongHyeon 	int			jumbo, maxpkt = 16, rx_npkts = 0;
2059a94100faSBill Paul 
20605120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
20615120abbfSSam Leffler 
2062fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
206381eee0ebSPyun YongHyeon 	if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
206481eee0ebSPyun YongHyeon 		jumbo = 1;
206581eee0ebSPyun YongHyeon 	else
206681eee0ebSPyun YongHyeon 		jumbo = 0;
2067a94100faSBill Paul 
2068a94100faSBill Paul 	/* Invalidate the descriptor memory */
2069a94100faSBill Paul 
2070a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2071a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2072d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2073a94100faSBill Paul 
2074d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2075d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
20765b6d1d9dSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
20775b6d1d9dSPyun YongHyeon 			break;
2078a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
2079a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
2080d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2081d65abd66SPyun YongHyeon 			break;
2082d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
2083a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
208481eee0ebSPyun YongHyeon 		if (jumbo != 0)
208581eee0ebSPyun YongHyeon 			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
208681eee0ebSPyun YongHyeon 		else
2087d65abd66SPyun YongHyeon 			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2088a94100faSBill Paul 
208981eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
209081eee0ebSPyun YongHyeon 		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
209181eee0ebSPyun YongHyeon 		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
209281eee0ebSPyun YongHyeon 			/*
209381eee0ebSPyun YongHyeon 			 * RTL8168C or later controllers do not
209481eee0ebSPyun YongHyeon 			 * support multi-fragment packet.
209581eee0ebSPyun YongHyeon 			 */
209681eee0ebSPyun YongHyeon 			re_discard_rxbuf(sc, i);
209781eee0ebSPyun YongHyeon 			continue;
209881eee0ebSPyun YongHyeon 		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2099d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
2100d65abd66SPyun YongHyeon 				/*
2101d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
2102d65abd66SPyun YongHyeon 				 * discard all the pieces.
2103d65abd66SPyun YongHyeon 				 */
2104d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
2105d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
2106d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
2107d65abd66SPyun YongHyeon 				}
2108d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2109d65abd66SPyun YongHyeon 				continue;
2110d65abd66SPyun YongHyeon 			}
211122a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
2112a94100faSBill Paul 			if (sc->rl_head == NULL)
2113a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
2114a94100faSBill Paul 			else {
2115a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2116a94100faSBill Paul 				sc->rl_tail->m_next = m;
2117a94100faSBill Paul 				sc->rl_tail = m;
2118a94100faSBill Paul 			}
2119a94100faSBill Paul 			continue;
2120a94100faSBill Paul 		}
2121a94100faSBill Paul 
2122a94100faSBill Paul 		/*
2123a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
2124a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
2125a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
2126a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
2127a94100faSBill Paul 		 * were already used, so to make room for the extra
2128a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
2129a94100faSBill Paul 		 * error' bit and shifted the other status bits
2130a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
2131a94100faSBill Paul 		 * still in the same places. We have already extracted
2132a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
2133a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
2134a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
2135a94100faSBill Paul 		 * them using the 8169 status as though it was in the
2136a94100faSBill Paul 		 * same format as that of the 8139C+.
2137a94100faSBill Paul 		 */
2138a94100faSBill Paul 		if (sc->rl_type == RL_8169)
2139a94100faSBill Paul 			rxstat >>= 1;
2140a94100faSBill Paul 
214122a11c96SJohn-Mark Gurney 		/*
214222a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
214322a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
214422a11c96SJohn-Mark Gurney 		 */
214581eee0ebSPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
214681eee0ebSPyun YongHyeon 			rxerr = 1;
214781eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
214881eee0ebSPyun YongHyeon 			    total_len > 8191 &&
214981eee0ebSPyun YongHyeon 			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
215081eee0ebSPyun YongHyeon 				rxerr = 0;
215181eee0ebSPyun YongHyeon 			if (rxerr != 0) {
2152a94100faSBill Paul 				ifp->if_ierrors++;
2153a94100faSBill Paul 				/*
2154a94100faSBill Paul 				 * If this is part of a multi-fragment packet,
2155a94100faSBill Paul 				 * discard all the pieces.
2156a94100faSBill Paul 				 */
2157a94100faSBill Paul 				if (sc->rl_head != NULL) {
2158a94100faSBill Paul 					m_freem(sc->rl_head);
2159a94100faSBill Paul 					sc->rl_head = sc->rl_tail = NULL;
2160a94100faSBill Paul 				}
2161d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2162a94100faSBill Paul 				continue;
2163a94100faSBill Paul 			}
216481eee0ebSPyun YongHyeon 		}
2165a94100faSBill Paul 
2166a94100faSBill Paul 		/*
2167a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
2168a94100faSBill Paul 		 * reload the current one.
2169a94100faSBill Paul 		 */
217081eee0ebSPyun YongHyeon 		if (jumbo != 0)
217181eee0ebSPyun YongHyeon 			rxerr = re_jumbo_newbuf(sc, i);
217281eee0ebSPyun YongHyeon 		else
217381eee0ebSPyun YongHyeon 			rxerr = re_newbuf(sc, i);
217481eee0ebSPyun YongHyeon 		if (rxerr != 0) {
2175d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
2176a94100faSBill Paul 			if (sc->rl_head != NULL) {
2177a94100faSBill Paul 				m_freem(sc->rl_head);
2178a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
2179a94100faSBill Paul 			}
2180d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
2181a94100faSBill Paul 			continue;
2182a94100faSBill Paul 		}
2183a94100faSBill Paul 
2184a94100faSBill Paul 		if (sc->rl_head != NULL) {
218581eee0ebSPyun YongHyeon 			if (jumbo != 0)
218681eee0ebSPyun YongHyeon 				m->m_len = total_len;
218781eee0ebSPyun YongHyeon 			else {
218822a11c96SJohn-Mark Gurney 				m->m_len = total_len % RE_RX_DESC_BUFLEN;
218922a11c96SJohn-Mark Gurney 				if (m->m_len == 0)
219022a11c96SJohn-Mark Gurney 					m->m_len = RE_RX_DESC_BUFLEN;
219181eee0ebSPyun YongHyeon 			}
2192a94100faSBill Paul 			/*
2193a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
2194a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
2195a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
2196a94100faSBill Paul 			 * care about anyway.
2197a94100faSBill Paul 			 */
2198a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
2199a94100faSBill Paul 				sc->rl_tail->m_len -=
2200a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
2201a94100faSBill Paul 				m_freem(m);
2202a94100faSBill Paul 			} else {
2203a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
2204a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2205a94100faSBill Paul 				sc->rl_tail->m_next = m;
2206a94100faSBill Paul 			}
2207a94100faSBill Paul 			m = sc->rl_head;
2208a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
2209a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2210a94100faSBill Paul 		} else
2211a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
2212a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
2213a94100faSBill Paul 
221422a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
221522a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
221622a11c96SJohn-Mark Gurney #endif
2217a94100faSBill Paul 		ifp->if_ipackets++;
2218a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
2219a94100faSBill Paul 
2220a94100faSBill Paul 		/* Do RX checksumming if enabled */
2221a94100faSBill Paul 
2222a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2223deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2224a94100faSBill Paul 				/* Check IP header checksum */
2225a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
2226deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2227deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2228a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2229deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2230deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2231a94100faSBill Paul 
2232a94100faSBill Paul 				/* Check TCP/UDP checksum */
2233a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
2234a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2235a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
2236a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2237a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
2238a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2239a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
2240a94100faSBill Paul 				}
2241deb5c680SPyun YongHyeon 			} else {
2242deb5c680SPyun YongHyeon 				/*
2243deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2244deb5c680SPyun YongHyeon 				 */
2245deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2246deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2247deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2248deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2249deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2250deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2251deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2252deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2253deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
2254deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2255deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
2256deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2257deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2258deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2259deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
2260deb5c680SPyun YongHyeon 				}
2261deb5c680SPyun YongHyeon 			}
2262a94100faSBill Paul 		}
2263ed510fb0SBill Paul 		maxpkt--;
2264d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
226578ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
2266bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
226778ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2268d147662cSGleb Smirnoff 		}
22695120abbfSSam Leffler 		RL_UNLOCK(sc);
2270a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
22715120abbfSSam Leffler 		RL_LOCK(sc);
22721abcdbd1SAttilio Rao 		rx_npkts++;
2273a94100faSBill Paul 	}
2274a94100faSBill Paul 
2275a94100faSBill Paul 	/* Flush the RX DMA ring */
2276a94100faSBill Paul 
2277a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2278a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2279a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2280a94100faSBill Paul 
2281a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
2282ed510fb0SBill Paul 
22831abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
22841abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2285ed510fb0SBill Paul 	if (maxpkt)
2286ed510fb0SBill Paul 		return (EAGAIN);
2287ed510fb0SBill Paul 
2288ed510fb0SBill Paul 	return (0);
2289a94100faSBill Paul }
2290a94100faSBill Paul 
2291a94100faSBill Paul static void
22927b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2293a94100faSBill Paul {
2294a94100faSBill Paul 	struct ifnet		*ifp;
2295d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2296a94100faSBill Paul 	u_int32_t		txstat;
2297d65abd66SPyun YongHyeon 	int			cons;
2298d65abd66SPyun YongHyeon 
2299d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2300d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2301d65abd66SPyun YongHyeon 		return;
2302a94100faSBill Paul 
2303fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2304a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2305a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2306a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2307d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2308a94100faSBill Paul 
2309d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2310d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2311d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2312d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2313a94100faSBill Paul 			break;
2314a94100faSBill Paul 		/*
2315a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2316a94100faSBill Paul 		 * in a fragment chain, which also happens to
2317a94100faSBill Paul 		 * be the only place where the TX status bits
2318a94100faSBill Paul 		 * are valid.
2319a94100faSBill Paul 		 */
2320a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2321d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2322d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2323d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2324d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2325d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2326d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2327d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2328d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2329d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2330a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2331a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2332a94100faSBill Paul 				ifp->if_collisions++;
2333a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2334a94100faSBill Paul 				ifp->if_oerrors++;
2335a94100faSBill Paul 			else
2336a94100faSBill Paul 				ifp->if_opackets++;
2337a94100faSBill Paul 		}
2338a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2339d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2340a94100faSBill Paul 	}
2341d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2342a94100faSBill Paul 
2343a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2344a94100faSBill Paul 
2345d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2346ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2347a94100faSBill Paul 		/*
2348b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2349b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2350a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2351a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2352a94100faSBill Paul 		 */
2353a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2354ed510fb0SBill Paul #endif
2355b4b95879SMarius Strobl 	} else
2356b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2357a94100faSBill Paul }
2358a94100faSBill Paul 
2359a94100faSBill Paul static void
23607b5ffebfSPyun YongHyeon re_tick(void *xsc)
2361a94100faSBill Paul {
2362a94100faSBill Paul 	struct rl_softc		*sc;
2363d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2364a94100faSBill Paul 
2365a94100faSBill Paul 	sc = xsc;
236697b9d4baSJohn-Mark Gurney 
236797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
236897b9d4baSJohn-Mark Gurney 
23691d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2370a94100faSBill Paul 	mii_tick(mii);
23710fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
23720fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2373c2d2e19cSPyun YongHyeon 	/*
2374c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2375c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2376c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2377c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2378c2d2e19cSPyun YongHyeon 	 */
2379c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2380130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2381d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2382a94100faSBill Paul }
2383a94100faSBill Paul 
2384a94100faSBill Paul #ifdef DEVICE_POLLING
23851abcdbd1SAttilio Rao static int
2386a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2387a94100faSBill Paul {
2388a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
23891abcdbd1SAttilio Rao 	int rx_npkts = 0;
2390a94100faSBill Paul 
2391a94100faSBill Paul 	RL_LOCK(sc);
239240929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
23931abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
239497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
23951abcdbd1SAttilio Rao 	return (rx_npkts);
239697b9d4baSJohn-Mark Gurney }
239797b9d4baSJohn-Mark Gurney 
23981abcdbd1SAttilio Rao static int
239997b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
240097b9d4baSJohn-Mark Gurney {
240197b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
24021abcdbd1SAttilio Rao 	int rx_npkts;
240397b9d4baSJohn-Mark Gurney 
240497b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
240597b9d4baSJohn-Mark Gurney 
2406a94100faSBill Paul 	sc->rxcycles = count;
24071abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2408a94100faSBill Paul 	re_txeof(sc);
2409a94100faSBill Paul 
241037652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2411d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2412a94100faSBill Paul 
2413a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2414a94100faSBill Paul 		u_int16_t       status;
2415a94100faSBill Paul 
2416a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2417a94100faSBill Paul 		if (status == 0xffff)
24181abcdbd1SAttilio Rao 			return (rx_npkts);
2419a94100faSBill Paul 		if (status)
2420a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2421818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2422818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2423818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2424a94100faSBill Paul 
2425a94100faSBill Paul 		/*
2426a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2427a94100faSBill Paul 		 */
2428a94100faSBill Paul 
24298476c243SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR) {
24308476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
243197b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2432a94100faSBill Paul 		}
24338476c243SPyun YongHyeon 	}
24341abcdbd1SAttilio Rao 	return (rx_npkts);
2435a94100faSBill Paul }
2436a94100faSBill Paul #endif /* DEVICE_POLLING */
2437a94100faSBill Paul 
2438ef544f63SPaolo Pisati static int
24397b5ffebfSPyun YongHyeon re_intr(void *arg)
2440a94100faSBill Paul {
2441a94100faSBill Paul 	struct rl_softc		*sc;
2442ed510fb0SBill Paul 	uint16_t		status;
2443a94100faSBill Paul 
2444a94100faSBill Paul 	sc = arg;
2445ed510fb0SBill Paul 
2446ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2447498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2448ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2449ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2450ed510fb0SBill Paul 
2451ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2452ed510fb0SBill Paul 
2453ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2454ed510fb0SBill Paul }
2455ed510fb0SBill Paul 
2456ed510fb0SBill Paul static void
24577b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2458ed510fb0SBill Paul {
2459ed510fb0SBill Paul 	struct rl_softc		*sc;
2460ed510fb0SBill Paul 	struct ifnet		*ifp;
2461ed510fb0SBill Paul 	u_int16_t		status;
2462ed510fb0SBill Paul 	int			rval = 0;
2463ed510fb0SBill Paul 
2464ed510fb0SBill Paul 	sc = arg;
2465ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2466a94100faSBill Paul 
2467a94100faSBill Paul 	RL_LOCK(sc);
246897b9d4baSJohn-Mark Gurney 
2469a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2470a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2471a94100faSBill Paul 
2472d65abd66SPyun YongHyeon 	if (sc->suspended ||
2473d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2474ed510fb0SBill Paul 		RL_UNLOCK(sc);
2475ed510fb0SBill Paul 		return;
2476ed510fb0SBill Paul 	}
2477a94100faSBill Paul 
2478ed510fb0SBill Paul #ifdef DEVICE_POLLING
2479ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2480ed510fb0SBill Paul 		RL_UNLOCK(sc);
2481ed510fb0SBill Paul 		return;
2482ed510fb0SBill Paul 	}
2483ed510fb0SBill Paul #endif
2484a94100faSBill Paul 
2485ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
24861abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2487ed510fb0SBill Paul 
2488818951afSPyun YongHyeon 	/*
2489818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2490818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2491818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2492818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2493818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2494818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2495818951afSPyun YongHyeon 	 */
2496818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2497818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2498818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
24993d85c23dSPyun YongHyeon 	if (status & (
2500ed510fb0SBill Paul #ifdef RE_TX_MODERATION
25013d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2502ed510fb0SBill Paul #else
25033d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2504ed510fb0SBill Paul #endif
2505ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2506a94100faSBill Paul 		re_txeof(sc);
2507a94100faSBill Paul 
25088476c243SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
25098476c243SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
251097b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
25118476c243SPyun YongHyeon 	}
2512a94100faSBill Paul 
251352732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2514d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2515a94100faSBill Paul 
2516a94100faSBill Paul 	RL_UNLOCK(sc);
2517ed510fb0SBill Paul 
2518ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2519ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2520ed510fb0SBill Paul 		return;
2521ed510fb0SBill Paul 	}
2522ed510fb0SBill Paul 
2523ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2524a94100faSBill Paul }
2525a94100faSBill Paul 
2526502be0f7SPyun YongHyeon static void
2527502be0f7SPyun YongHyeon re_intr_msi(void *xsc)
2528502be0f7SPyun YongHyeon {
2529502be0f7SPyun YongHyeon 	struct rl_softc		*sc;
2530502be0f7SPyun YongHyeon 	struct ifnet		*ifp;
2531502be0f7SPyun YongHyeon 	uint16_t		intrs, status;
2532502be0f7SPyun YongHyeon 
2533502be0f7SPyun YongHyeon 	sc = xsc;
2534502be0f7SPyun YongHyeon 	RL_LOCK(sc);
2535502be0f7SPyun YongHyeon 
2536502be0f7SPyun YongHyeon 	ifp = sc->rl_ifp;
2537502be0f7SPyun YongHyeon #ifdef DEVICE_POLLING
2538502be0f7SPyun YongHyeon 	if (ifp->if_capenable & IFCAP_POLLING) {
2539502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2540502be0f7SPyun YongHyeon 		return;
2541502be0f7SPyun YongHyeon 	}
2542502be0f7SPyun YongHyeon #endif
2543502be0f7SPyun YongHyeon 	/* Disable interrupts. */
2544502be0f7SPyun YongHyeon 	CSR_WRITE_2(sc, RL_IMR, 0);
2545502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2546502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2547502be0f7SPyun YongHyeon 		return;
2548502be0f7SPyun YongHyeon 	}
2549502be0f7SPyun YongHyeon 
2550502be0f7SPyun YongHyeon 	intrs = RL_INTRS_CPLUS;
2551502be0f7SPyun YongHyeon 	status = CSR_READ_2(sc, RL_ISR);
2552502be0f7SPyun YongHyeon         CSR_WRITE_2(sc, RL_ISR, status);
2553502be0f7SPyun YongHyeon 	if (sc->rl_int_rx_act > 0) {
2554502be0f7SPyun YongHyeon 		intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2555502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2556502be0f7SPyun YongHyeon 		status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2557502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2558502be0f7SPyun YongHyeon 	}
2559502be0f7SPyun YongHyeon 
2560502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR |
2561502be0f7SPyun YongHyeon 	    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) {
2562502be0f7SPyun YongHyeon 		re_rxeof(sc, NULL);
2563502be0f7SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2564502be0f7SPyun YongHyeon 			if (sc->rl_int_rx_mod != 0 &&
2565502be0f7SPyun YongHyeon 			    (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR |
2566502be0f7SPyun YongHyeon 			    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) {
2567502be0f7SPyun YongHyeon 				/* Rearm one-shot timer. */
2568502be0f7SPyun YongHyeon 				CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2569502be0f7SPyun YongHyeon 				intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR |
2570502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN);
2571502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 1;
2572502be0f7SPyun YongHyeon 			} else {
2573502be0f7SPyun YongHyeon 				intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR |
2574502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN;
2575502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 0;
2576502be0f7SPyun YongHyeon 			}
2577502be0f7SPyun YongHyeon 		}
2578502be0f7SPyun YongHyeon 	}
2579502be0f7SPyun YongHyeon 
2580502be0f7SPyun YongHyeon 	/*
2581502be0f7SPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2582502be0f7SPyun YongHyeon 	 * while an existing transmission is in progress. If
2583502be0f7SPyun YongHyeon 	 * the transmitter goes idle but there are still
2584502be0f7SPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2585502be0f7SPyun YongHyeon 	 * channel here to flush them out. This only seems to
2586502be0f7SPyun YongHyeon 	 * be required with the PCIe devices.
2587502be0f7SPyun YongHyeon 	 */
2588502be0f7SPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2589502be0f7SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2590502be0f7SPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2591502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL))
2592502be0f7SPyun YongHyeon 		re_txeof(sc);
2593502be0f7SPyun YongHyeon 
2594502be0f7SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
2595502be0f7SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2596502be0f7SPyun YongHyeon 		re_init_locked(sc);
2597502be0f7SPyun YongHyeon 	}
2598502be0f7SPyun YongHyeon 
2599502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2600502be0f7SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2601502be0f7SPyun YongHyeon 			re_start_locked(ifp);
2602502be0f7SPyun YongHyeon 		CSR_WRITE_2(sc, RL_IMR, intrs);
2603502be0f7SPyun YongHyeon 	}
2604502be0f7SPyun YongHyeon 	RL_UNLOCK(sc);
2605502be0f7SPyun YongHyeon }
2606502be0f7SPyun YongHyeon 
2607d65abd66SPyun YongHyeon static int
26087b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2609d65abd66SPyun YongHyeon {
2610d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2611d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2612d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2613d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2614d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2615d65abd66SPyun YongHyeon 	int			nsegs, prod;
2616d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2617d65abd66SPyun YongHyeon 	int			padlen;
2618ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2619a94100faSBill Paul 
2620d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2621738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
26220fc4974fSBill Paul 
26230fc4974fSBill Paul 	/*
26240fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
26250fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
26260fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
26270fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
26280fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
26290fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
263099c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
26310fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2632d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
26330fc4974fSBill Paul 	 */
2634f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2635deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
263699c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2637d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2638d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2639d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2640d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2641d65abd66SPyun YongHyeon 			m_freem(*m_head);
2642d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2643d65abd66SPyun YongHyeon 				*m_head = NULL;
2644a94100faSBill Paul 				return (ENOBUFS);
2645a94100faSBill Paul 			}
2646d65abd66SPyun YongHyeon 			*m_head = m_new;
2647d65abd66SPyun YongHyeon 		}
2648d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2649d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
265080a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2651b4b95879SMarius Strobl 			if (m_new == NULL) {
2652b4b95879SMarius Strobl 				m_freem(*m_head);
2653b4b95879SMarius Strobl 				*m_head = NULL;
265480a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2655b4b95879SMarius Strobl 			}
2656d65abd66SPyun YongHyeon 		} else
2657d65abd66SPyun YongHyeon 			m_new = *m_head;
2658a94100faSBill Paul 
26590fc4974fSBill Paul 		/*
26600fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
26610fc4974fSBill Paul 		 * to avoid leaking data.
26620fc4974fSBill Paul 		 */
2663d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2664d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
26650fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2666d65abd66SPyun YongHyeon 		*m_head = m_new;
26670fc4974fSBill Paul 	}
26680fc4974fSBill Paul 
2669d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2670d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2671d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2672d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2673d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2674304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2675d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2676d65abd66SPyun YongHyeon 			m_freem(*m_head);
2677b4b95879SMarius Strobl 			*m_head = NULL;
2678d65abd66SPyun YongHyeon 			return (ENOBUFS);
2679a94100faSBill Paul 		}
2680d65abd66SPyun YongHyeon 		*m_head = m_new;
2681d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2682d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2683d65abd66SPyun YongHyeon 		if (error != 0) {
2684d65abd66SPyun YongHyeon 			m_freem(*m_head);
2685d65abd66SPyun YongHyeon 			*m_head = NULL;
2686d65abd66SPyun YongHyeon 			return (error);
2687a94100faSBill Paul 		}
2688d65abd66SPyun YongHyeon 	} else if (error != 0)
2689d65abd66SPyun YongHyeon 		return (error);
2690d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2691d65abd66SPyun YongHyeon 		m_freem(*m_head);
2692d65abd66SPyun YongHyeon 		*m_head = NULL;
2693d65abd66SPyun YongHyeon 		return (EIO);
2694d65abd66SPyun YongHyeon 	}
2695d65abd66SPyun YongHyeon 
2696d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2697d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2698d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2699d65abd66SPyun YongHyeon 		return (ENOBUFS);
2700d65abd66SPyun YongHyeon 	}
2701d65abd66SPyun YongHyeon 
2702d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2703d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2704a94100faSBill Paul 
2705a94100faSBill Paul 	/*
2706d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2707d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2708d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2709d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2710a94100faSBill Paul 	 */
2711deb5c680SPyun YongHyeon 	vlanctl = 0;
2712d65abd66SPyun YongHyeon 	csum_flags = 0;
2713d6d7d923SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2714d6d7d923SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2715d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND;
2716d6d7d923SPyun YongHyeon 			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2717d6d7d923SPyun YongHyeon 			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2718d6d7d923SPyun YongHyeon 		} else {
2719d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND |
2720d65abd66SPyun YongHyeon 			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2721d65abd66SPyun YongHyeon 			    RL_TDESC_CMD_MSSVAL_SHIFT);
2722d6d7d923SPyun YongHyeon 		}
2723d6d7d923SPyun YongHyeon 	} else {
272499c8ae87SPyun YongHyeon 		/*
272599c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
272699c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
272799c8ae87SPyun YongHyeon 		 * does't make effects.
272899c8ae87SPyun YongHyeon 		 */
272999c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2730deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2731d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2732deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2733deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2734d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2735deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2736deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2737d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2738deb5c680SPyun YongHyeon 			} else {
2739deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2740deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2741deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2742deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2743deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2744deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2745deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2746deb5c680SPyun YongHyeon 			}
2747d65abd66SPyun YongHyeon 		}
274899c8ae87SPyun YongHyeon 	}
2749a94100faSBill Paul 
2750ccf34c81SPyun YongHyeon 	/*
2751ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2752ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2753ccf34c81SPyun YongHyeon 	 * transmission attempt.
2754ccf34c81SPyun YongHyeon 	 */
2755ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2756bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2757deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2758ccf34c81SPyun YongHyeon 
2759d65abd66SPyun YongHyeon 	si = prod;
2760d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2761d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2762deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2763d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2764d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2765d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2766d65abd66SPyun YongHyeon 		if (i != 0)
2767d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2768d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2769d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2770d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2771d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2772d65abd66SPyun YongHyeon 	}
2773d65abd66SPyun YongHyeon 	/* Update producer index. */
2774d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2775a94100faSBill Paul 
2776d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2777d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2778d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2779d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2780d65abd66SPyun YongHyeon 
2781d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2782d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2783d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2784a94100faSBill Paul 
2785d65abd66SPyun YongHyeon 	/*
2786d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2787d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2788d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2789d65abd66SPyun YongHyeon 	 */
2790d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2791d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2792d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2793d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2794d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2795a94100faSBill Paul 
2796a94100faSBill Paul 	return (0);
2797a94100faSBill Paul }
2798a94100faSBill Paul 
279997b9d4baSJohn-Mark Gurney static void
2800d180a66fSPyun YongHyeon re_start(struct ifnet *ifp)
280197b9d4baSJohn-Mark Gurney {
2802d180a66fSPyun YongHyeon 	struct rl_softc		*sc;
280397b9d4baSJohn-Mark Gurney 
2804d180a66fSPyun YongHyeon 	sc = ifp->if_softc;
2805d180a66fSPyun YongHyeon 	RL_LOCK(sc);
2806d180a66fSPyun YongHyeon 	re_start_locked(ifp);
2807d180a66fSPyun YongHyeon 	RL_UNLOCK(sc);
280897b9d4baSJohn-Mark Gurney }
280997b9d4baSJohn-Mark Gurney 
2810a94100faSBill Paul /*
2811a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2812a94100faSBill Paul  */
2813a94100faSBill Paul static void
2814d180a66fSPyun YongHyeon re_start_locked(struct ifnet *ifp)
2815a94100faSBill Paul {
2816a94100faSBill Paul 	struct rl_softc		*sc;
2817d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2818d65abd66SPyun YongHyeon 	int			queued;
2819a94100faSBill Paul 
2820a94100faSBill Paul 	sc = ifp->if_softc;
282197b9d4baSJohn-Mark Gurney 
2822d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2823d180a66fSPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
2824ed510fb0SBill Paul 		return;
2825a94100faSBill Paul 
2826d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2827d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
282852732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2829a94100faSBill Paul 		if (m_head == NULL)
2830a94100faSBill Paul 			break;
2831a94100faSBill Paul 
2832d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2833b4b95879SMarius Strobl 			if (m_head == NULL)
2834b4b95879SMarius Strobl 				break;
283552732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
283613f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2837a94100faSBill Paul 			break;
2838a94100faSBill Paul 		}
2839a94100faSBill Paul 
2840a94100faSBill Paul 		/*
2841a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2842a94100faSBill Paul 		 * to him.
2843a94100faSBill Paul 		 */
284459a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
284552732175SMax Laier 
284652732175SMax Laier 		queued++;
2847a94100faSBill Paul 	}
2848a94100faSBill Paul 
2849ed510fb0SBill Paul 	if (queued == 0) {
2850ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2851d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2852ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2853ed510fb0SBill Paul #endif
285452732175SMax Laier 		return;
2855ed510fb0SBill Paul 	}
285652732175SMax Laier 
2857a94100faSBill Paul 	/* Flush the TX descriptors */
2858a94100faSBill Paul 
2859a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2860a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2861a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2862a94100faSBill Paul 
28630fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2864a94100faSBill Paul 
2865ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2866a94100faSBill Paul 	/*
2867a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2868a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2869a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2870a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2871a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2872a94100faSBill Paul 	 * the timer count is reset to 0.
2873a94100faSBill Paul 	 */
2874a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2875ed510fb0SBill Paul #endif
2876a94100faSBill Paul 
2877a94100faSBill Paul 	/*
2878a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2879a94100faSBill Paul 	 */
28801d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2881a94100faSBill Paul }
2882a94100faSBill Paul 
2883a94100faSBill Paul static void
288481eee0ebSPyun YongHyeon re_set_jumbo(struct rl_softc *sc, int jumbo)
288581eee0ebSPyun YongHyeon {
288681eee0ebSPyun YongHyeon 
288781eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
288881eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
288981eee0ebSPyun YongHyeon 		return;
289081eee0ebSPyun YongHyeon 	}
289181eee0ebSPyun YongHyeon 
289281eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
289381eee0ebSPyun YongHyeon 	if (jumbo != 0) {
289481eee0ebSPyun YongHyeon 		CSR_WRITE_1(sc, RL_CFG3, CSR_READ_1(sc, RL_CFG3) |
289581eee0ebSPyun YongHyeon 		    RL_CFG3_JUMBO_EN0);
289681eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
289781eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
289881eee0ebSPyun YongHyeon 			break;
289981eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
290081eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) |
290181eee0ebSPyun YongHyeon 			    0x01);
290281eee0ebSPyun YongHyeon 			break;
290381eee0ebSPyun YongHyeon 		default:
290481eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) |
290581eee0ebSPyun YongHyeon 			    RL_CFG4_JUMBO_EN1);
290681eee0ebSPyun YongHyeon 		}
290781eee0ebSPyun YongHyeon 	} else {
290881eee0ebSPyun YongHyeon 		CSR_WRITE_1(sc, RL_CFG3, CSR_READ_1(sc, RL_CFG3) &
290981eee0ebSPyun YongHyeon 		    ~RL_CFG3_JUMBO_EN0);
291081eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
291181eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
291281eee0ebSPyun YongHyeon 			break;
291381eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
291481eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) &
291581eee0ebSPyun YongHyeon 			    ~0x01);
291681eee0ebSPyun YongHyeon 			break;
291781eee0ebSPyun YongHyeon 		default:
291881eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) &
291981eee0ebSPyun YongHyeon 			    ~RL_CFG4_JUMBO_EN1);
292081eee0ebSPyun YongHyeon 		}
292181eee0ebSPyun YongHyeon 	}
292281eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
292381eee0ebSPyun YongHyeon 
292481eee0ebSPyun YongHyeon 	switch (sc->rl_hwrev->rl_rev) {
292581eee0ebSPyun YongHyeon 	case RL_HWREV_8168DP:
292681eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
292781eee0ebSPyun YongHyeon 		break;
292881eee0ebSPyun YongHyeon 	default:
292981eee0ebSPyun YongHyeon 		if (jumbo != 0)
293081eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 512);
293181eee0ebSPyun YongHyeon 		else
293281eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 4096);
293381eee0ebSPyun YongHyeon 	}
293481eee0ebSPyun YongHyeon }
293581eee0ebSPyun YongHyeon 
293681eee0ebSPyun YongHyeon static void
29377b5ffebfSPyun YongHyeon re_init(void *xsc)
2938a94100faSBill Paul {
2939a94100faSBill Paul 	struct rl_softc		*sc = xsc;
294097b9d4baSJohn-Mark Gurney 
294197b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
294297b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
294397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
294497b9d4baSJohn-Mark Gurney }
294597b9d4baSJohn-Mark Gurney 
294697b9d4baSJohn-Mark Gurney static void
29477b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
294897b9d4baSJohn-Mark Gurney {
2949fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2950a94100faSBill Paul 	struct mii_data		*mii;
2951566ca8caSJung-uk Kim 	uint32_t		reg;
295270acaecfSPyun YongHyeon 	uint16_t		cfg;
29534d3d7085SBernd Walter 	union {
29544d3d7085SBernd Walter 		uint32_t align_dummy;
29554d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
29564d3d7085SBernd Walter         } eaddr;
2957a94100faSBill Paul 
295897b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
295997b9d4baSJohn-Mark Gurney 
2960a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2961a94100faSBill Paul 
29628476c243SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
29638476c243SPyun YongHyeon 		return;
29648476c243SPyun YongHyeon 
2965a94100faSBill Paul 	/*
2966a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2967a94100faSBill Paul 	 */
2968a94100faSBill Paul 	re_stop(sc);
2969a94100faSBill Paul 
2970b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
2971b659f1f0SPyun YongHyeon 	re_reset(sc);
2972b659f1f0SPyun YongHyeon 
2973a94100faSBill Paul 	/*
29744a814a5eSPyun YongHyeon 	 * For C+ mode, initialize the RX descriptors and mbufs.
29754a814a5eSPyun YongHyeon 	 */
297681eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
297781eee0ebSPyun YongHyeon 		if (ifp->if_mtu > RL_MTU) {
297881eee0ebSPyun YongHyeon 			if (re_jrx_list_init(sc) != 0) {
297981eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
298081eee0ebSPyun YongHyeon 				    "no memory for jumbo RX buffers\n");
298181eee0ebSPyun YongHyeon 				re_stop(sc);
298281eee0ebSPyun YongHyeon 				return;
298381eee0ebSPyun YongHyeon 			}
298481eee0ebSPyun YongHyeon 			/* Disable checksum offloading for jumbo frames. */
298581eee0ebSPyun YongHyeon 			ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
298681eee0ebSPyun YongHyeon 			ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
298781eee0ebSPyun YongHyeon 		} else {
298881eee0ebSPyun YongHyeon 			if (re_rx_list_init(sc) != 0) {
298981eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
299081eee0ebSPyun YongHyeon 				    "no memory for RX buffers\n");
299181eee0ebSPyun YongHyeon 				re_stop(sc);
299281eee0ebSPyun YongHyeon 				return;
299381eee0ebSPyun YongHyeon 			}
299481eee0ebSPyun YongHyeon 		}
299581eee0ebSPyun YongHyeon 		re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
299681eee0ebSPyun YongHyeon 	} else {
29974a814a5eSPyun YongHyeon 		if (re_rx_list_init(sc) != 0) {
29984a814a5eSPyun YongHyeon 			device_printf(sc->rl_dev, "no memory for RX buffers\n");
29994a814a5eSPyun YongHyeon 			re_stop(sc);
30004a814a5eSPyun YongHyeon 			return;
30014a814a5eSPyun YongHyeon 		}
300281eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
300381eee0ebSPyun YongHyeon 		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
300481eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
300581eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 512);
300681eee0ebSPyun YongHyeon 			else
300781eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 4096);
300881eee0ebSPyun YongHyeon 		}
300981eee0ebSPyun YongHyeon 	}
30104a814a5eSPyun YongHyeon 	re_tx_list_init(sc);
30114a814a5eSPyun YongHyeon 
30124a814a5eSPyun YongHyeon 	/*
3013c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
3014edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
3015c2c6548bSBill Paul 	 * before all others.
3016c2c6548bSBill Paul 	 */
301770acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
301870acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
301970acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
302070acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
302170acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
3022deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
3023deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
3024deb5c680SPyun YongHyeon 		/* XXX magic. */
3025deb5c680SPyun YongHyeon 		cfg |= 0x0001;
3026deb5c680SPyun YongHyeon 	} else
3027deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
3028deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
302981eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
303081eee0ebSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
3031566ca8caSJung-uk Kim 		reg = 0x000fff00;
3032566ca8caSJung-uk Kim 		if ((CSR_READ_1(sc, RL_CFG2) & RL_CFG2_PCI66MHZ) != 0)
3033566ca8caSJung-uk Kim 			reg |= 0x000000ff;
303481eee0ebSPyun YongHyeon 		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
3035566ca8caSJung-uk Kim 			reg |= 0x00f00000;
3036566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
3037566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
3038566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
3039566ca8caSJung-uk Kim 	}
3040ae644087SPyun YongHyeon 	/*
3041ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
3042ae644087SPyun YongHyeon 	 * allowed in controller.
3043ae644087SPyun YongHyeon 	 */
3044ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
3045ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
3046ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
3047ae644087SPyun YongHyeon 	}
3048c2c6548bSBill Paul 
3049c2c6548bSBill Paul 	/*
3050a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
3051a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
3052a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
3053a94100faSBill Paul 	 */
30544d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
30554d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
3056a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3057ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
3058ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
3059ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
3060ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
3061a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3062a94100faSBill Paul 
3063a94100faSBill Paul 	/*
3064d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
3065d01fac16SPyun YongHyeon 	 */
3066d01fac16SPyun YongHyeon 
3067d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
3068d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
3069d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
3070d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
3071d01fac16SPyun YongHyeon 
3072d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
3073d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
3074d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
3075d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
3076d01fac16SPyun YongHyeon 
3077d01fac16SPyun YongHyeon 	/*
3078a94100faSBill Paul 	 * Enable transmit and receive.
3079a94100faSBill Paul 	 */
3080a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3081a94100faSBill Paul 
3082a94100faSBill Paul 	/*
3083ff191365SJung-uk Kim 	 * Set the initial TX configuration.
3084a94100faSBill Paul 	 */
3085abc8ff44SBill Paul 	if (sc->rl_testmode) {
3086abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
3087abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3088abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
3089a94100faSBill Paul 		else
3090abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3091abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
3092abc8ff44SBill Paul 	} else
3093a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
3094d01fac16SPyun YongHyeon 
3095d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
3096d01fac16SPyun YongHyeon 
3097a94100faSBill Paul 	/*
3098ff191365SJung-uk Kim 	 * Set the initial RX configuration.
3099a94100faSBill Paul 	 */
3100ff191365SJung-uk Kim 	re_set_rxmode(sc);
3101a94100faSBill Paul 
3102483cc440SPyun YongHyeon 	/* Configure interrupt moderation. */
3103483cc440SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
3104483cc440SPyun YongHyeon 		/* Magic from vendor. */
31055e6906eeSPyun YongHyeon 		CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
3106483cc440SPyun YongHyeon 	}
3107483cc440SPyun YongHyeon 
3108a94100faSBill Paul #ifdef DEVICE_POLLING
3109a94100faSBill Paul 	/*
3110a94100faSBill Paul 	 * Disable interrupts if we are polling.
3111a94100faSBill Paul 	 */
311240929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3113a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3114a94100faSBill Paul 	else	/* otherwise ... */
311540929967SGleb Smirnoff #endif
3116ed510fb0SBill Paul 
3117a94100faSBill Paul 	/*
3118a94100faSBill Paul 	 * Enable interrupts.
3119a94100faSBill Paul 	 */
3120a94100faSBill Paul 	if (sc->rl_testmode)
3121a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3122a94100faSBill Paul 	else
3123a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3124ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3125a94100faSBill Paul 
3126a94100faSBill Paul 	/* Set initial TX threshold */
3127a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
3128a94100faSBill Paul 
3129a94100faSBill Paul 	/* Start RX/TX process. */
3130a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3131a94100faSBill Paul #ifdef notdef
3132a94100faSBill Paul 	/* Enable receiver and transmitter. */
3133a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3134a94100faSBill Paul #endif
3135a94100faSBill Paul 
3136a94100faSBill Paul 	/*
3137a94100faSBill Paul 	 * Initialize the timer interrupt register so that
3138a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
3139a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
3140502be0f7SPyun YongHyeon 	 * reloaded on each transmit.
3141502be0f7SPyun YongHyeon 	 */
3142502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
3143502be0f7SPyun YongHyeon 	/*
3144502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate TX interrupt
3145a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
3146a94100faSBill Paul 	 */
3147a94100faSBill Paul 	if (sc->rl_type == RL_8169)
3148a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3149a94100faSBill Paul 	else
3150a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3151502be0f7SPyun YongHyeon #else
3152502be0f7SPyun YongHyeon 	/*
3153502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate RX interrupt
3154502be0f7SPyun YongHyeon 	 * moderation.
3155502be0f7SPyun YongHyeon 	 */
3156502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
3157502be0f7SPyun YongHyeon 	    intr_filter == 0) {
3158502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3159502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169,
3160502be0f7SPyun YongHyeon 			    RL_USECS(sc->rl_int_rx_mod));
3161502be0f7SPyun YongHyeon 	} else {
3162502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3163502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0));
3164502be0f7SPyun YongHyeon 	}
3165ed510fb0SBill Paul #endif
3166a94100faSBill Paul 
3167a94100faSBill Paul 	/*
3168a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
3169a94100faSBill Paul 	 * size so we can receive jumbo frames.
3170a94100faSBill Paul 	 */
317189feeee4SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
317281eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
317381eee0ebSPyun YongHyeon 			/*
317481eee0ebSPyun YongHyeon 			 * For controllers that use new jumbo frame scheme,
317581eee0ebSPyun YongHyeon 			 * set maximum size of jumbo frame depedning on
317681eee0ebSPyun YongHyeon 			 * controller revisions.
317781eee0ebSPyun YongHyeon 			 */
317881eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
317981eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
318081eee0ebSPyun YongHyeon 				    sc->rl_hwrev->rl_max_mtu +
318181eee0ebSPyun YongHyeon 				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
318281eee0ebSPyun YongHyeon 				    ETHER_CRC_LEN);
318389feeee4SPyun YongHyeon 			else
318481eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
318581eee0ebSPyun YongHyeon 				    RE_RX_DESC_BUFLEN);
318681eee0ebSPyun YongHyeon 		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
318781eee0ebSPyun YongHyeon 		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
318881eee0ebSPyun YongHyeon 			/* RTL810x has no jumbo frame support. */
318981eee0ebSPyun YongHyeon 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
319081eee0ebSPyun YongHyeon 		} else
3191a94100faSBill Paul 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
319289feeee4SPyun YongHyeon 	}
3193a94100faSBill Paul 
319497b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
3195a94100faSBill Paul 		return;
3196a94100faSBill Paul 
3197a94100faSBill Paul 	mii_mediachg(mii);
3198a94100faSBill Paul 
319919ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
3200a94100faSBill Paul 
320113f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
320213f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3203a94100faSBill Paul 
3204351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
32051d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3206d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3207a94100faSBill Paul }
3208a94100faSBill Paul 
3209a94100faSBill Paul /*
3210a94100faSBill Paul  * Set media options.
3211a94100faSBill Paul  */
3212a94100faSBill Paul static int
32137b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
3214a94100faSBill Paul {
3215a94100faSBill Paul 	struct rl_softc		*sc;
3216a94100faSBill Paul 	struct mii_data		*mii;
32176f0f9b12SPyun YongHyeon 	int			error;
3218a94100faSBill Paul 
3219a94100faSBill Paul 	sc = ifp->if_softc;
3220a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3221d1754a9bSJohn Baldwin 	RL_LOCK(sc);
32226f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
3223d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3224a94100faSBill Paul 
32256f0f9b12SPyun YongHyeon 	return (error);
3226a94100faSBill Paul }
3227a94100faSBill Paul 
3228a94100faSBill Paul /*
3229a94100faSBill Paul  * Report current media status.
3230a94100faSBill Paul  */
3231a94100faSBill Paul static void
32327b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3233a94100faSBill Paul {
3234a94100faSBill Paul 	struct rl_softc		*sc;
3235a94100faSBill Paul 	struct mii_data		*mii;
3236a94100faSBill Paul 
3237a94100faSBill Paul 	sc = ifp->if_softc;
3238a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3239a94100faSBill Paul 
3240d1754a9bSJohn Baldwin 	RL_LOCK(sc);
3241a94100faSBill Paul 	mii_pollstat(mii);
3242a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
3243a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
324457c81d92SPyun YongHyeon 	RL_UNLOCK(sc);
3245a94100faSBill Paul }
3246a94100faSBill Paul 
3247a94100faSBill Paul static int
32487b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3249a94100faSBill Paul {
3250a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
3251a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
3252a94100faSBill Paul 	struct mii_data		*mii;
3253bc2a1002SPyun YongHyeon 	uint32_t		rev;
325440929967SGleb Smirnoff 	int			error = 0;
3255a94100faSBill Paul 
3256a94100faSBill Paul 	switch (command) {
3257a94100faSBill Paul 	case SIOCSIFMTU:
325881eee0ebSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN ||
325981eee0ebSPyun YongHyeon 		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu) {
3260c1d0b573SPyun YongHyeon 			error = EINVAL;
3261c1d0b573SPyun YongHyeon 			break;
3262c1d0b573SPyun YongHyeon 		}
3263c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
326481eee0ebSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
3265a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
326681eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
326781eee0ebSPyun YongHyeon 			    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
326881eee0ebSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
326981eee0ebSPyun YongHyeon 				re_init_locked(sc);
327081eee0ebSPyun YongHyeon 			}
3271ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3272ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
327381eee0ebSPyun YongHyeon 				ifp->if_capenable &= ~(IFCAP_TSO4 |
327481eee0ebSPyun YongHyeon 				    IFCAP_VLAN_HWTSO);
3275ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
327681eee0ebSPyun YongHyeon 			}
3277ecafbbb5SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
3278ae644087SPyun YongHyeon 		}
3279d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
3280a94100faSBill Paul 		break;
3281a94100faSBill Paul 	case SIOCSIFFLAGS:
328297b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
3283eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
3284eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3285eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
32863021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3287ff191365SJung-uk Kim 					re_set_rxmode(sc);
3288eed497bbSPyun YongHyeon 			} else
328997b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
3290eed497bbSPyun YongHyeon 		} else {
3291eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3292a94100faSBill Paul 				re_stop(sc);
3293eed497bbSPyun YongHyeon 		}
3294eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
329597b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3296a94100faSBill Paul 		break;
3297a94100faSBill Paul 	case SIOCADDMULTI:
3298a94100faSBill Paul 	case SIOCDELMULTI:
329997b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
33008476c243SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3301ff191365SJung-uk Kim 			re_set_rxmode(sc);
330297b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3303a94100faSBill Paul 		break;
3304a94100faSBill Paul 	case SIOCGIFMEDIA:
3305a94100faSBill Paul 	case SIOCSIFMEDIA:
3306a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
3307a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3308a94100faSBill Paul 		break;
3309a94100faSBill Paul 	case SIOCSIFCAP:
331040929967SGleb Smirnoff 	    {
3311f051cb85SGleb Smirnoff 		int mask, reinit;
3312f051cb85SGleb Smirnoff 
3313f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3314f051cb85SGleb Smirnoff 		reinit = 0;
331540929967SGleb Smirnoff #ifdef DEVICE_POLLING
331640929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
331740929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
331840929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
331940929967SGleb Smirnoff 				if (error)
332040929967SGleb Smirnoff 					return (error);
3321d1754a9bSJohn Baldwin 				RL_LOCK(sc);
332240929967SGleb Smirnoff 				/* Disable interrupts */
332340929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
332440929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
332540929967SGleb Smirnoff 				RL_UNLOCK(sc);
332640929967SGleb Smirnoff 			} else {
332740929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
332840929967SGleb Smirnoff 				/* Enable interrupts. */
332940929967SGleb Smirnoff 				RL_LOCK(sc);
333040929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
333140929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
333240929967SGleb Smirnoff 				RL_UNLOCK(sc);
333340929967SGleb Smirnoff 			}
333440929967SGleb Smirnoff 		}
333540929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3336600af6c2SPyun YongHyeon 		RL_LOCK(sc);
3337d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
3338d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3339d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
3340bc2a1002SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0) {
3341bc2a1002SPyun YongHyeon 				rev = sc->rl_hwrev->rl_rev;
3342bc2a1002SPyun YongHyeon 				if (rev == RL_HWREV_8168C ||
3343bc2a1002SPyun YongHyeon 				    rev == RL_HWREV_8168C_SPIN2)
3344bc2a1002SPyun YongHyeon 					ifp->if_hwassist |= CSUM_TCP | CSUM_UDP;
3345a94100faSBill Paul 				else
3346bc2a1002SPyun YongHyeon 					ifp->if_hwassist |= RE_CSUM_FEATURES;
3347bc2a1002SPyun YongHyeon 			} else
3348b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3349f051cb85SGleb Smirnoff 			reinit = 1;
335040929967SGleb Smirnoff 		}
3351d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
3352d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3353d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
3354d3b181aeSPyun YongHyeon 			reinit = 1;
3355d3b181aeSPyun YongHyeon 		}
3356ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
3357ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO) != 0) {
3358dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
3359ecafbbb5SPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3360dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
3361dc74159dSPyun YongHyeon 			else
3362dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3363ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3364ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3365ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
3366ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3367ae644087SPyun YongHyeon 			}
3368dc74159dSPyun YongHyeon 		}
3369ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3370ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3371ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3372ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3373ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3374ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3375ecafbbb5SPyun YongHyeon 			/* TSO over VLAN requires VLAN hardware tagging. */
3376ecafbbb5SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3377ecafbbb5SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3378ecafbbb5SPyun YongHyeon 			reinit = 1;
3379ecafbbb5SPyun YongHyeon 		}
338081eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
338181eee0ebSPyun YongHyeon 		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
338281eee0ebSPyun YongHyeon 		    IFCAP_VLAN_HWTSO)) != 0)
338381eee0ebSPyun YongHyeon 				reinit = 1;
33847467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
33857467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
33867467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
33877467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
33887467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
33897467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
33907467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
33917467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
33927467bd53SPyun YongHyeon 		}
33938476c243SPyun YongHyeon 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
33948476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3395600af6c2SPyun YongHyeon 			re_init_locked(sc);
33968476c243SPyun YongHyeon 		}
3397600af6c2SPyun YongHyeon 		RL_UNLOCK(sc);
3398960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
339940929967SGleb Smirnoff 	    }
3400a94100faSBill Paul 		break;
3401a94100faSBill Paul 	default:
3402a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
3403a94100faSBill Paul 		break;
3404a94100faSBill Paul 	}
3405a94100faSBill Paul 
3406a94100faSBill Paul 	return (error);
3407a94100faSBill Paul }
3408a94100faSBill Paul 
3409a94100faSBill Paul static void
34107b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
34111d545c7aSMarius Strobl {
3412130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
3413a94100faSBill Paul 
34141d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
34151d545c7aSMarius Strobl 
34161d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
34171d545c7aSMarius Strobl 		return;
34181d545c7aSMarius Strobl 
3419130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
3420a94100faSBill Paul 	re_txeof(sc);
3421130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3422130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3423130b6dfbSPyun YongHyeon 		    "-- recovering\n");
3424130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3425d180a66fSPyun YongHyeon 			re_start_locked(ifp);
3426130b6dfbSPyun YongHyeon 		return;
3427130b6dfbSPyun YongHyeon 	}
3428130b6dfbSPyun YongHyeon 
3429130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
3430130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
3431130b6dfbSPyun YongHyeon 
34321abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
34338476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
343497b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
3435130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3436d180a66fSPyun YongHyeon 		re_start_locked(ifp);
3437a94100faSBill Paul }
3438a94100faSBill Paul 
3439a94100faSBill Paul /*
3440a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
3441a94100faSBill Paul  * RX and TX lists.
3442a94100faSBill Paul  */
3443a94100faSBill Paul static void
34447b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
3445a94100faSBill Paul {
34460ce0868aSPyun YongHyeon 	int			i;
3447a94100faSBill Paul 	struct ifnet		*ifp;
3448d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
3449d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
3450a94100faSBill Paul 
345197b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
345297b9d4baSJohn-Mark Gurney 
3453fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
3454a94100faSBill Paul 
34551d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3456d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
345713f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3458a94100faSBill Paul 
3459ead8fc66SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0)
3460ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3461ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
3462ead8fc66SPyun YongHyeon 	else
3463a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3464ead8fc66SPyun YongHyeon 	DELAY(1000);
3465a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3466ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3467a94100faSBill Paul 
3468a94100faSBill Paul 	if (sc->rl_head != NULL) {
3469a94100faSBill Paul 		m_freem(sc->rl_head);
3470a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
3471a94100faSBill Paul 	}
3472a94100faSBill Paul 
3473a94100faSBill Paul 	/* Free the TX list buffers. */
3474a94100faSBill Paul 
3475d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3476d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
3477d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
3478d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3479d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3480d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3481d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
3482d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
3483d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
3484a94100faSBill Paul 		}
3485a94100faSBill Paul 	}
3486a94100faSBill Paul 
3487a94100faSBill Paul 	/* Free the RX list buffers. */
3488a94100faSBill Paul 
3489d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3490d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
3491d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
3492d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3493d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3494d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3495d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
3496d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
3497d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
3498a94100faSBill Paul 		}
3499a94100faSBill Paul 	}
3500a94100faSBill Paul }
3501a94100faSBill Paul 
3502a94100faSBill Paul /*
3503a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
3504a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
3505a94100faSBill Paul  * resume.
3506a94100faSBill Paul  */
3507a94100faSBill Paul static int
35087b5ffebfSPyun YongHyeon re_suspend(device_t dev)
3509a94100faSBill Paul {
3510a94100faSBill Paul 	struct rl_softc		*sc;
3511a94100faSBill Paul 
3512a94100faSBill Paul 	sc = device_get_softc(dev);
3513a94100faSBill Paul 
351497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3515a94100faSBill Paul 	re_stop(sc);
35167467bd53SPyun YongHyeon 	re_setwol(sc);
3517a94100faSBill Paul 	sc->suspended = 1;
351897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3519a94100faSBill Paul 
3520a94100faSBill Paul 	return (0);
3521a94100faSBill Paul }
3522a94100faSBill Paul 
3523a94100faSBill Paul /*
3524a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
3525a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
3526a94100faSBill Paul  * appropriate.
3527a94100faSBill Paul  */
3528a94100faSBill Paul static int
35297b5ffebfSPyun YongHyeon re_resume(device_t dev)
3530a94100faSBill Paul {
3531a94100faSBill Paul 	struct rl_softc		*sc;
3532a94100faSBill Paul 	struct ifnet		*ifp;
3533a94100faSBill Paul 
3534a94100faSBill Paul 	sc = device_get_softc(dev);
353597b9d4baSJohn-Mark Gurney 
353697b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
353797b9d4baSJohn-Mark Gurney 
3538fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
353961f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
354061f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
354161f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
354261f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
354361f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
354461f45a72SPyun YongHyeon 	}
3545a94100faSBill Paul 
35467467bd53SPyun YongHyeon 	/*
35477467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
35487467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
35497467bd53SPyun YongHyeon 	 */
35507467bd53SPyun YongHyeon 	re_clrwol(sc);
355101d1a6c3SPyun YongHyeon 
355201d1a6c3SPyun YongHyeon 	/* reinitialize interface if necessary */
355301d1a6c3SPyun YongHyeon 	if (ifp->if_flags & IFF_UP)
355401d1a6c3SPyun YongHyeon 		re_init_locked(sc);
355501d1a6c3SPyun YongHyeon 
3556a94100faSBill Paul 	sc->suspended = 0;
355797b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3558a94100faSBill Paul 
3559a94100faSBill Paul 	return (0);
3560a94100faSBill Paul }
3561a94100faSBill Paul 
3562a94100faSBill Paul /*
3563a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3564a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3565a94100faSBill Paul  */
35666a087a87SPyun YongHyeon static int
35677b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3568a94100faSBill Paul {
3569a94100faSBill Paul 	struct rl_softc		*sc;
3570a94100faSBill Paul 
3571a94100faSBill Paul 	sc = device_get_softc(dev);
3572a94100faSBill Paul 
357397b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3574a94100faSBill Paul 	re_stop(sc);
3575536fde34SMaxim Sobolev 	/*
3576536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3577536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
357872293673SRuslan Ermilov 	 * cases.
3579536fde34SMaxim Sobolev 	 */
3580536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
35817467bd53SPyun YongHyeon 	re_setwol(sc);
358297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
35836a087a87SPyun YongHyeon 
35846a087a87SPyun YongHyeon 	return (0);
3585a94100faSBill Paul }
35867467bd53SPyun YongHyeon 
35877467bd53SPyun YongHyeon static void
35887b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
35897467bd53SPyun YongHyeon {
35907467bd53SPyun YongHyeon 	struct ifnet		*ifp;
35917467bd53SPyun YongHyeon 	int			pmc;
35927467bd53SPyun YongHyeon 	uint16_t		pmstat;
35937467bd53SPyun YongHyeon 	uint8_t			v;
35947467bd53SPyun YongHyeon 
35957467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
35967467bd53SPyun YongHyeon 
35973b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
35987467bd53SPyun YongHyeon 		return;
35997467bd53SPyun YongHyeon 
36007467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
360161f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
360261f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
360361f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
360461f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
360561f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
360661f45a72SPyun YongHyeon 	}
3607886ff602SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3608886ff602SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3609886ff602SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
36107467bd53SPyun YongHyeon 	/* Enable config register write. */
36117467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
36127467bd53SPyun YongHyeon 
36137467bd53SPyun YongHyeon 	/* Enable PME. */
36147467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
36157467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
36167467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
36177467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
36187467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
36197467bd53SPyun YongHyeon 
36207467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
36217467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
36227467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
36237467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
36247467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
36257467bd53SPyun YongHyeon 
36267467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
3627*44f7cbf5SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST |
3628*44f7cbf5SPyun YongHyeon 	    RL_CFG5_WOL_LANWAKE);
36297467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
36307467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
36317467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
36327467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
36337467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
36347467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
36357467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
36367467bd53SPyun YongHyeon 
3637*44f7cbf5SPyun YongHyeon 	/* Config register write done. */
3638*44f7cbf5SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3639*44f7cbf5SPyun YongHyeon 
3640d0c45156SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3641d0c45156SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3642d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
36437467bd53SPyun YongHyeon 	/*
36447467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
36457467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
36467467bd53SPyun YongHyeon 	 * needed.
36477467bd53SPyun YongHyeon 	 */
36487467bd53SPyun YongHyeon 
36497467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
36507467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
36517467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
36527467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
36537467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
36547467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
36557467bd53SPyun YongHyeon }
36567467bd53SPyun YongHyeon 
36577467bd53SPyun YongHyeon static void
36587b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
36597467bd53SPyun YongHyeon {
36607467bd53SPyun YongHyeon 	int			pmc;
36617467bd53SPyun YongHyeon 	uint8_t			v;
36627467bd53SPyun YongHyeon 
36637467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
36647467bd53SPyun YongHyeon 
36653b0a4aefSJohn Baldwin 	if (pci_find_cap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
36667467bd53SPyun YongHyeon 		return;
36677467bd53SPyun YongHyeon 
36687467bd53SPyun YongHyeon 	/* Enable config register write. */
36697467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
36707467bd53SPyun YongHyeon 
36717467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
36727467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
36737467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
36747467bd53SPyun YongHyeon 
36757467bd53SPyun YongHyeon 	/* Config register write done. */
3676f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
36777467bd53SPyun YongHyeon 
36787467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
36797467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
36807467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
36817467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
36827467bd53SPyun YongHyeon }
36830534aae0SPyun YongHyeon 
36840534aae0SPyun YongHyeon static void
36850534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc)
36860534aae0SPyun YongHyeon {
36870534aae0SPyun YongHyeon 	struct sysctl_ctx_list	*ctx;
36880534aae0SPyun YongHyeon 	struct sysctl_oid_list	*children;
3689502be0f7SPyun YongHyeon 	int			error;
36900534aae0SPyun YongHyeon 
36910534aae0SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->rl_dev);
36920534aae0SPyun YongHyeon 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
36930534aae0SPyun YongHyeon 
36940534aae0SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
36950534aae0SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I",
36960534aae0SPyun YongHyeon 	    "Statistics Information");
3697502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
3698502be0f7SPyun YongHyeon 		return;
3699502be0f7SPyun YongHyeon 
3700502be0f7SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod",
3701502be0f7SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, &sc->rl_int_rx_mod, 0,
3702502be0f7SPyun YongHyeon 	    sysctl_hw_re_int_mod, "I", "re RX interrupt moderation");
3703502be0f7SPyun YongHyeon 	/* Pull in device tunables. */
3704502be0f7SPyun YongHyeon 	sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3705502be0f7SPyun YongHyeon 	error = resource_int_value(device_get_name(sc->rl_dev),
3706502be0f7SPyun YongHyeon 	    device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod);
3707502be0f7SPyun YongHyeon 	if (error == 0) {
3708502be0f7SPyun YongHyeon 		if (sc->rl_int_rx_mod < RL_TIMER_MIN ||
3709502be0f7SPyun YongHyeon 		    sc->rl_int_rx_mod > RL_TIMER_MAX) {
3710502be0f7SPyun YongHyeon 			device_printf(sc->rl_dev, "int_rx_mod value out of "
3711502be0f7SPyun YongHyeon 			    "range; using default: %d\n",
3712502be0f7SPyun YongHyeon 			    RL_TIMER_DEFAULT);
3713502be0f7SPyun YongHyeon 			sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3714502be0f7SPyun YongHyeon 		}
3715502be0f7SPyun YongHyeon 	}
3716502be0f7SPyun YongHyeon 
37170534aae0SPyun YongHyeon }
37180534aae0SPyun YongHyeon 
37190534aae0SPyun YongHyeon static int
37200534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS)
37210534aae0SPyun YongHyeon {
37220534aae0SPyun YongHyeon 	struct rl_softc		*sc;
37230534aae0SPyun YongHyeon 	struct rl_stats		*stats;
37240534aae0SPyun YongHyeon 	int			error, i, result;
37250534aae0SPyun YongHyeon 
37260534aae0SPyun YongHyeon 	result = -1;
37270534aae0SPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
37280534aae0SPyun YongHyeon 	if (error || req->newptr == NULL)
37290534aae0SPyun YongHyeon 		return (error);
37300534aae0SPyun YongHyeon 
37310534aae0SPyun YongHyeon 	if (result == 1) {
37320534aae0SPyun YongHyeon 		sc = (struct rl_softc *)arg1;
37330534aae0SPyun YongHyeon 		RL_LOCK(sc);
373416a4824bSPyun YongHyeon 		if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
373516a4824bSPyun YongHyeon 			RL_UNLOCK(sc);
373616a4824bSPyun YongHyeon 			goto done;
373716a4824bSPyun YongHyeon 		}
37380534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
37390534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
37400534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
37410534aae0SPyun YongHyeon 		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
37420534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
37430534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
37440534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
37450534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
37460534aae0SPyun YongHyeon 		    RL_DUMPSTATS_START));
37470534aae0SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
37480534aae0SPyun YongHyeon 			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
37490534aae0SPyun YongHyeon 			    RL_DUMPSTATS_START) == 0)
37500534aae0SPyun YongHyeon 				break;
37510534aae0SPyun YongHyeon 			DELAY(1000);
37520534aae0SPyun YongHyeon 		}
37530534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
37540534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
37550534aae0SPyun YongHyeon 		RL_UNLOCK(sc);
37560534aae0SPyun YongHyeon 		if (i == 0) {
37570534aae0SPyun YongHyeon 			device_printf(sc->rl_dev,
37580534aae0SPyun YongHyeon 			    "DUMP statistics request timedout\n");
37590534aae0SPyun YongHyeon 			return (ETIMEDOUT);
37600534aae0SPyun YongHyeon 		}
376116a4824bSPyun YongHyeon done:
37620534aae0SPyun YongHyeon 		stats = sc->rl_ldata.rl_stats;
37630534aae0SPyun YongHyeon 		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
37640534aae0SPyun YongHyeon 		printf("Tx frames : %ju\n",
37650534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_pkts));
37660534aae0SPyun YongHyeon 		printf("Rx frames : %ju\n",
37670534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_pkts));
37680534aae0SPyun YongHyeon 		printf("Tx errors : %ju\n",
37690534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_errs));
37700534aae0SPyun YongHyeon 		printf("Rx errors : %u\n",
37710534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_errs));
37720534aae0SPyun YongHyeon 		printf("Rx missed frames : %u\n",
37730534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_missed_pkts));
37740534aae0SPyun YongHyeon 		printf("Rx frame alignment errs : %u\n",
37750534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
37760534aae0SPyun YongHyeon 		printf("Tx single collisions : %u\n",
37770534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_onecoll));
37780534aae0SPyun YongHyeon 		printf("Tx multiple collisions : %u\n",
37790534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_multicolls));
37800534aae0SPyun YongHyeon 		printf("Rx unicast frames : %ju\n",
37810534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
37820534aae0SPyun YongHyeon 		printf("Rx broadcast frames : %ju\n",
37830534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
37840534aae0SPyun YongHyeon 		printf("Rx multicast frames : %u\n",
37850534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_mcasts));
37860534aae0SPyun YongHyeon 		printf("Tx aborts : %u\n",
37870534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_tx_aborts));
37880534aae0SPyun YongHyeon 		printf("Tx underruns : %u\n",
37890534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_underruns));
37900534aae0SPyun YongHyeon 	}
37910534aae0SPyun YongHyeon 
37920534aae0SPyun YongHyeon 	return (error);
37930534aae0SPyun YongHyeon }
3794502be0f7SPyun YongHyeon 
3795502be0f7SPyun YongHyeon static int
3796502be0f7SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
3797502be0f7SPyun YongHyeon {
3798502be0f7SPyun YongHyeon 	int error, value;
3799502be0f7SPyun YongHyeon 
3800502be0f7SPyun YongHyeon 	if (arg1 == NULL)
3801502be0f7SPyun YongHyeon 		return (EINVAL);
3802502be0f7SPyun YongHyeon 	value = *(int *)arg1;
3803502be0f7SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
3804502be0f7SPyun YongHyeon 	if (error || req->newptr == NULL)
3805502be0f7SPyun YongHyeon 		return (error);
3806502be0f7SPyun YongHyeon 	if (value < low || value > high)
3807502be0f7SPyun YongHyeon 		return (EINVAL);
3808502be0f7SPyun YongHyeon 	*(int *)arg1 = value;
3809502be0f7SPyun YongHyeon 
3810502be0f7SPyun YongHyeon 	return (0);
3811502be0f7SPyun YongHyeon }
3812502be0f7SPyun YongHyeon 
3813502be0f7SPyun YongHyeon static int
3814502be0f7SPyun YongHyeon sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)
3815502be0f7SPyun YongHyeon {
3816502be0f7SPyun YongHyeon 
3817502be0f7SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN,
3818502be0f7SPyun YongHyeon 	    RL_TIMER_MAX));
3819502be0f7SPyun YongHyeon }
3820