xref: /freebsd/sys/dev/re/if_re.c (revision 54899a96e16b866c27604124251064535b8ca99d)
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  */
174a94100faSBill Paul static struct rl_type re_devs[] = {
1759dfcacbeSPyun YongHyeon 	{ DLINK_VENDORID, DLINK_DEVICEID_528T, 0,
17632aa5f0eSAnton Berezin 	    "D-Link DGE-528(T) Gigabit Ethernet Adapter" },
1779dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8139, 0,
178a94100faSBill Paul 	    "RealTek 8139C+ 10/100BaseTX" },
1799dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8101E, 0,
180*54899a96SPyun YongHyeon 	    "RealTek 810xE PCIe 10/100baseTX" },
1819dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8168, 0,
182d0c45156SPyun YongHyeon 	    "RealTek 8168/8111 B/C/CP/D/DP/E PCIe Gigabit Ethernet" },
1839dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169, 0,
184715922d7SPyun YongHyeon 	    "RealTek 8169/8169S/8169SB(L)/8110S/8110SB(L) Gigabit Ethernet" },
1859dfcacbeSPyun YongHyeon 	{ RT_VENDORID, RT_DEVICEID_8169SC, 0,
1862ee2c3b4SRemko Lodder 	    "RealTek 8169SC/8110SC Single-chip Gigabit Ethernet" },
1879dfcacbeSPyun YongHyeon 	{ COREGA_VENDORID, COREGA_DEVICEID_CGLAPCIGT, 0,
188ea263191SMIHIRA Sanpei Yoshiro 	    "Corega CG-LAPCIGT (RTL8169S) Gigabit Ethernet" },
1899dfcacbeSPyun YongHyeon 	{ LINKSYS_VENDORID, LINKSYS_DEVICEID_EG1032, 0,
19026390635SJohn Baldwin 	    "Linksys EG1032 (RTL8169S) Gigabit Ethernet" },
1919dfcacbeSPyun YongHyeon 	{ USR_VENDORID, USR_DEVICEID_997902, 0,
192dfdb409eSPyun YongHyeon 	    "US Robotics 997902 (RTL8169S) Gigabit Ethernet" }
193a94100faSBill Paul };
194a94100faSBill Paul 
195a94100faSBill Paul static struct rl_hwrev re_hwrevs[] = {
19681eee0ebSPyun YongHyeon 	{ RL_HWREV_8139, RL_8139,  "", RL_MTU },
19781eee0ebSPyun YongHyeon 	{ RL_HWREV_8139A, RL_8139, "A", RL_MTU },
19881eee0ebSPyun YongHyeon 	{ RL_HWREV_8139AG, RL_8139, "A-G", RL_MTU },
19981eee0ebSPyun YongHyeon 	{ RL_HWREV_8139B, RL_8139, "B", RL_MTU },
20081eee0ebSPyun YongHyeon 	{ RL_HWREV_8130, RL_8139, "8130", RL_MTU },
20181eee0ebSPyun YongHyeon 	{ RL_HWREV_8139C, RL_8139, "C", RL_MTU },
20281eee0ebSPyun YongHyeon 	{ RL_HWREV_8139D, RL_8139, "8139D/8100B/8100C", RL_MTU },
20381eee0ebSPyun YongHyeon 	{ RL_HWREV_8139CPLUS, RL_8139CPLUS, "C+", RL_MTU },
204ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN1, RL_8169, "8168", RL_JUMBO_MTU },
20581eee0ebSPyun YongHyeon 	{ RL_HWREV_8169, RL_8169, "8169", RL_JUMBO_MTU },
20681eee0ebSPyun YongHyeon 	{ RL_HWREV_8169S, RL_8169, "8169S", RL_JUMBO_MTU },
20781eee0ebSPyun YongHyeon 	{ RL_HWREV_8110S, RL_8169, "8110S", RL_JUMBO_MTU },
20881eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SB, RL_8169, "8169SB/8110SB", RL_JUMBO_MTU },
20981eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SC, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21081eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SBL, RL_8169, "8169SBL/8110SBL", RL_JUMBO_MTU },
21181eee0ebSPyun YongHyeon 	{ RL_HWREV_8169_8110SCE, RL_8169, "8169SC/8110SC", RL_JUMBO_MTU },
21281eee0ebSPyun YongHyeon 	{ RL_HWREV_8100, RL_8139, "8100", RL_MTU },
21381eee0ebSPyun YongHyeon 	{ RL_HWREV_8101, RL_8139, "8101", RL_MTU },
21481eee0ebSPyun YongHyeon 	{ RL_HWREV_8100E, RL_8169, "8100E", RL_MTU },
21581eee0ebSPyun YongHyeon 	{ RL_HWREV_8101E, RL_8169, "8101E", RL_MTU },
21681eee0ebSPyun YongHyeon 	{ RL_HWREV_8102E, RL_8169, "8102E", RL_MTU },
21781eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL, RL_8169, "8102EL", RL_MTU },
21881eee0ebSPyun YongHyeon 	{ RL_HWREV_8102EL_SPIN1, RL_8169, "8102EL", RL_MTU },
21981eee0ebSPyun YongHyeon 	{ RL_HWREV_8103E, RL_8169, "8103E", RL_MTU },
220*54899a96SPyun YongHyeon 	{ RL_HWREV_8105E, RL_8169, "8105E", RL_MTU },
221ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN2, RL_8169, "8168", RL_JUMBO_MTU },
222ef278cb4SPyun YongHyeon 	{ RL_HWREV_8168B_SPIN3, RL_8169, "8168", RL_JUMBO_MTU },
22381eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
22481eee0ebSPyun YongHyeon 	{ RL_HWREV_8168C_SPIN2, RL_8169, "8168C/8111C", RL_JUMBO_MTU_6K },
22581eee0ebSPyun YongHyeon 	{ RL_HWREV_8168CP, RL_8169, "8168CP/8111CP", RL_JUMBO_MTU_6K },
22681eee0ebSPyun YongHyeon 	{ RL_HWREV_8168D, RL_8169, "8168D/8111D", RL_JUMBO_MTU_9K },
22781eee0ebSPyun YongHyeon 	{ RL_HWREV_8168DP, RL_8169, "8168DP/8111DP", RL_JUMBO_MTU_9K },
22881eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E, RL_8169, "8168E/8111E", RL_JUMBO_MTU_9K},
22981eee0ebSPyun YongHyeon 	{ RL_HWREV_8168E_VL, RL_8169, "8168E/8111E-VL", RL_JUMBO_MTU_6K},
23081eee0ebSPyun YongHyeon 	{ 0, 0, NULL, 0 }
231a94100faSBill Paul };
232a94100faSBill Paul 
233a94100faSBill Paul static int re_probe		(device_t);
234a94100faSBill Paul static int re_attach		(device_t);
235a94100faSBill Paul static int re_detach		(device_t);
236a94100faSBill Paul 
237d65abd66SPyun YongHyeon static int re_encap		(struct rl_softc *, struct mbuf **);
238a94100faSBill Paul 
239a94100faSBill Paul static void re_dma_map_addr	(void *, bus_dma_segment_t *, int, int);
240a94100faSBill Paul static int re_allocmem		(device_t, struct rl_softc *);
241d65abd66SPyun YongHyeon static __inline void re_discard_rxbuf
242d65abd66SPyun YongHyeon 				(struct rl_softc *, int);
243d65abd66SPyun YongHyeon static int re_newbuf		(struct rl_softc *, int);
24481eee0ebSPyun YongHyeon static int re_jumbo_newbuf	(struct rl_softc *, int);
245a94100faSBill Paul static int re_rx_list_init	(struct rl_softc *);
24681eee0ebSPyun YongHyeon static int re_jrx_list_init	(struct rl_softc *);
247a94100faSBill Paul static int re_tx_list_init	(struct rl_softc *);
24822a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
24922a11c96SJohn-Mark Gurney static __inline void re_fixup_rx
25022a11c96SJohn-Mark Gurney 				(struct mbuf *);
25122a11c96SJohn-Mark Gurney #endif
2521abcdbd1SAttilio Rao static int re_rxeof		(struct rl_softc *, int *);
253a94100faSBill Paul static void re_txeof		(struct rl_softc *);
25497b9d4baSJohn-Mark Gurney #ifdef DEVICE_POLLING
2551abcdbd1SAttilio Rao static int re_poll		(struct ifnet *, enum poll_cmd, int);
2561abcdbd1SAttilio Rao static int re_poll_locked	(struct ifnet *, enum poll_cmd, int);
25797b9d4baSJohn-Mark Gurney #endif
258ef544f63SPaolo Pisati static int re_intr		(void *);
259502be0f7SPyun YongHyeon static void re_intr_msi		(void *);
260a94100faSBill Paul static void re_tick		(void *);
261ed510fb0SBill Paul static void re_int_task		(void *, int);
262a94100faSBill Paul static void re_start		(struct ifnet *);
263d180a66fSPyun YongHyeon static void re_start_locked	(struct ifnet *);
264a94100faSBill Paul static int re_ioctl		(struct ifnet *, u_long, caddr_t);
265a94100faSBill Paul static void re_init		(void *);
26697b9d4baSJohn-Mark Gurney static void re_init_locked	(struct rl_softc *);
267a94100faSBill Paul static void re_stop		(struct rl_softc *);
2681d545c7aSMarius Strobl static void re_watchdog		(struct rl_softc *);
269a94100faSBill Paul static int re_suspend		(device_t);
270a94100faSBill Paul static int re_resume		(device_t);
2716a087a87SPyun YongHyeon static int re_shutdown		(device_t);
272a94100faSBill Paul static int re_ifmedia_upd	(struct ifnet *);
273a94100faSBill Paul static void re_ifmedia_sts	(struct ifnet *, struct ifmediareq *);
274a94100faSBill Paul 
275a94100faSBill Paul static void re_eeprom_putbyte	(struct rl_softc *, int);
276a94100faSBill Paul static void re_eeprom_getword	(struct rl_softc *, int, u_int16_t *);
277ed510fb0SBill Paul static void re_read_eeprom	(struct rl_softc *, caddr_t, int, int);
278a94100faSBill Paul static int re_gmii_readreg	(device_t, int, int);
279a94100faSBill Paul static int re_gmii_writereg	(device_t, int, int, int);
280a94100faSBill Paul 
281a94100faSBill Paul static int re_miibus_readreg	(device_t, int, int);
282a94100faSBill Paul static int re_miibus_writereg	(device_t, int, int, int);
283a94100faSBill Paul static void re_miibus_statchg	(device_t);
284a94100faSBill Paul 
28581eee0ebSPyun YongHyeon static void re_set_jumbo	(struct rl_softc *, int);
286ff191365SJung-uk Kim static void re_set_rxmode		(struct rl_softc *);
287a94100faSBill Paul static void re_reset		(struct rl_softc *);
2887467bd53SPyun YongHyeon static void re_setwol		(struct rl_softc *);
2897467bd53SPyun YongHyeon static void re_clrwol		(struct rl_softc *);
290a94100faSBill Paul 
291ed510fb0SBill Paul #ifdef RE_DIAG
292a94100faSBill Paul static int re_diag		(struct rl_softc *);
293ed510fb0SBill Paul #endif
294a94100faSBill Paul 
2950534aae0SPyun YongHyeon static void re_add_sysctls	(struct rl_softc *);
2960534aae0SPyun YongHyeon static int re_sysctl_stats	(SYSCTL_HANDLER_ARGS);
297502be0f7SPyun YongHyeon static int sysctl_int_range	(SYSCTL_HANDLER_ARGS, int, int);
298502be0f7SPyun YongHyeon static int sysctl_hw_re_int_mod	(SYSCTL_HANDLER_ARGS);
2990534aae0SPyun YongHyeon 
300a94100faSBill Paul static device_method_t re_methods[] = {
301a94100faSBill Paul 	/* Device interface */
302a94100faSBill Paul 	DEVMETHOD(device_probe,		re_probe),
303a94100faSBill Paul 	DEVMETHOD(device_attach,	re_attach),
304a94100faSBill Paul 	DEVMETHOD(device_detach,	re_detach),
305a94100faSBill Paul 	DEVMETHOD(device_suspend,	re_suspend),
306a94100faSBill Paul 	DEVMETHOD(device_resume,	re_resume),
307a94100faSBill Paul 	DEVMETHOD(device_shutdown,	re_shutdown),
308a94100faSBill Paul 
309a94100faSBill Paul 	/* bus interface */
310a94100faSBill Paul 	DEVMETHOD(bus_print_child,	bus_generic_print_child),
311a94100faSBill Paul 	DEVMETHOD(bus_driver_added,	bus_generic_driver_added),
312a94100faSBill Paul 
313a94100faSBill Paul 	/* MII interface */
314a94100faSBill Paul 	DEVMETHOD(miibus_readreg,	re_miibus_readreg),
315a94100faSBill Paul 	DEVMETHOD(miibus_writereg,	re_miibus_writereg),
316a94100faSBill Paul 	DEVMETHOD(miibus_statchg,	re_miibus_statchg),
317a94100faSBill Paul 
318a94100faSBill Paul 	{ 0, 0 }
319a94100faSBill Paul };
320a94100faSBill Paul 
321a94100faSBill Paul static driver_t re_driver = {
322a94100faSBill Paul 	"re",
323a94100faSBill Paul 	re_methods,
324a94100faSBill Paul 	sizeof(struct rl_softc)
325a94100faSBill Paul };
326a94100faSBill Paul 
327a94100faSBill Paul static devclass_t re_devclass;
328a94100faSBill Paul 
329a94100faSBill Paul DRIVER_MODULE(re, pci, re_driver, re_devclass, 0, 0);
330a94100faSBill Paul DRIVER_MODULE(miibus, re, miibus_driver, miibus_devclass, 0, 0);
331a94100faSBill Paul 
332a94100faSBill Paul #define EE_SET(x)					\
333a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
334a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) | x)
335a94100faSBill Paul 
336a94100faSBill Paul #define EE_CLR(x)					\
337a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD,			\
338a94100faSBill Paul 		CSR_READ_1(sc, RL_EECMD) & ~x)
339a94100faSBill Paul 
340a94100faSBill Paul /*
341a94100faSBill Paul  * Send a read command and address to the EEPROM, check for ACK.
342a94100faSBill Paul  */
343a94100faSBill Paul static void
3447b5ffebfSPyun YongHyeon re_eeprom_putbyte(struct rl_softc *sc, int addr)
345a94100faSBill Paul {
3460ce0868aSPyun YongHyeon 	int			d, i;
347a94100faSBill Paul 
348ed510fb0SBill Paul 	d = addr | (RL_9346_READ << sc->rl_eewidth);
349a94100faSBill Paul 
350a94100faSBill Paul 	/*
351a94100faSBill Paul 	 * Feed in each bit and strobe the clock.
352a94100faSBill Paul 	 */
353ed510fb0SBill Paul 
354ed510fb0SBill Paul 	for (i = 1 << (sc->rl_eewidth + 3); i; i >>= 1) {
355a94100faSBill Paul 		if (d & i) {
356a94100faSBill Paul 			EE_SET(RL_EE_DATAIN);
357a94100faSBill Paul 		} else {
358a94100faSBill Paul 			EE_CLR(RL_EE_DATAIN);
359a94100faSBill Paul 		}
360a94100faSBill Paul 		DELAY(100);
361a94100faSBill Paul 		EE_SET(RL_EE_CLK);
362a94100faSBill Paul 		DELAY(150);
363a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
364a94100faSBill Paul 		DELAY(100);
365a94100faSBill Paul 	}
366a94100faSBill Paul }
367a94100faSBill Paul 
368a94100faSBill Paul /*
369a94100faSBill Paul  * Read a word of data stored in the EEPROM at address 'addr.'
370a94100faSBill Paul  */
371a94100faSBill Paul static void
3727b5ffebfSPyun YongHyeon re_eeprom_getword(struct rl_softc *sc, int addr, u_int16_t *dest)
373a94100faSBill Paul {
3740ce0868aSPyun YongHyeon 	int			i;
375a94100faSBill Paul 	u_int16_t		word = 0;
376a94100faSBill Paul 
377a94100faSBill Paul 	/*
378a94100faSBill Paul 	 * Send address of word we want to read.
379a94100faSBill Paul 	 */
380a94100faSBill Paul 	re_eeprom_putbyte(sc, addr);
381a94100faSBill Paul 
382a94100faSBill Paul 	/*
383a94100faSBill Paul 	 * Start reading bits from EEPROM.
384a94100faSBill Paul 	 */
385a94100faSBill Paul 	for (i = 0x8000; i; i >>= 1) {
386a94100faSBill Paul 		EE_SET(RL_EE_CLK);
387a94100faSBill Paul 		DELAY(100);
388a94100faSBill Paul 		if (CSR_READ_1(sc, RL_EECMD) & RL_EE_DATAOUT)
389a94100faSBill Paul 			word |= i;
390a94100faSBill Paul 		EE_CLR(RL_EE_CLK);
391a94100faSBill Paul 		DELAY(100);
392a94100faSBill Paul 	}
393a94100faSBill Paul 
394a94100faSBill Paul 	*dest = word;
395a94100faSBill Paul }
396a94100faSBill Paul 
397a94100faSBill Paul /*
398a94100faSBill Paul  * Read a sequence of words from the EEPROM.
399a94100faSBill Paul  */
400a94100faSBill Paul static void
4017b5ffebfSPyun YongHyeon re_read_eeprom(struct rl_softc *sc, caddr_t dest, int off, int cnt)
402a94100faSBill Paul {
403a94100faSBill Paul 	int			i;
404a94100faSBill Paul 	u_int16_t		word = 0, *ptr;
405a94100faSBill Paul 
406ed510fb0SBill Paul 	CSR_SETBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
407ed510fb0SBill Paul 
408ed510fb0SBill Paul         DELAY(100);
409ed510fb0SBill Paul 
410a94100faSBill Paul 	for (i = 0; i < cnt; i++) {
411ed510fb0SBill Paul 		CSR_SETBIT_1(sc, RL_EECMD, RL_EE_SEL);
412a94100faSBill Paul 		re_eeprom_getword(sc, off + i, &word);
413ed510fb0SBill Paul 		CSR_CLRBIT_1(sc, RL_EECMD, RL_EE_SEL);
414a94100faSBill Paul 		ptr = (u_int16_t *)(dest + (i * 2));
415be099007SPyun YongHyeon                 *ptr = word;
416a94100faSBill Paul 	}
417ed510fb0SBill Paul 
418ed510fb0SBill Paul 	CSR_CLRBIT_1(sc, RL_EECMD, RL_EEMODE_PROGRAM);
419a94100faSBill Paul }
420a94100faSBill Paul 
421a94100faSBill Paul static int
4227b5ffebfSPyun YongHyeon re_gmii_readreg(device_t dev, int phy, int reg)
423a94100faSBill Paul {
424a94100faSBill Paul 	struct rl_softc		*sc;
425a94100faSBill Paul 	u_int32_t		rval;
426a94100faSBill Paul 	int			i;
427a94100faSBill Paul 
428a94100faSBill Paul 	sc = device_get_softc(dev);
429a94100faSBill Paul 
4309bac70b8SBill Paul 	/* Let the rgephy driver read the GMEDIASTAT register */
4319bac70b8SBill Paul 
4329bac70b8SBill Paul 	if (reg == RL_GMEDIASTAT) {
4339bac70b8SBill Paul 		rval = CSR_READ_1(sc, RL_GMEDIASTAT);
4349bac70b8SBill Paul 		return (rval);
4359bac70b8SBill Paul 	}
4369bac70b8SBill Paul 
437a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, reg << 16);
438a94100faSBill Paul 
43996b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
440a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
441a94100faSBill Paul 		if (rval & RL_PHYAR_BUSY)
442a94100faSBill Paul 			break;
4432bc085c6SPyun YongHyeon 		DELAY(25);
444a94100faSBill Paul 	}
445a94100faSBill Paul 
44696b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4476b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY read failed\n");
448a94100faSBill Paul 		return (0);
449a94100faSBill Paul 	}
450a94100faSBill Paul 
4512bc085c6SPyun YongHyeon 	/*
4522bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4532bc085c6SPyun YongHyeon 	 */
4542bc085c6SPyun YongHyeon 	DELAY(20);
4552bc085c6SPyun YongHyeon 
456a94100faSBill Paul 	return (rval & RL_PHYAR_PHYDATA);
457a94100faSBill Paul }
458a94100faSBill Paul 
459a94100faSBill Paul static int
4607b5ffebfSPyun YongHyeon re_gmii_writereg(device_t dev, int phy, int reg, int data)
461a94100faSBill Paul {
462a94100faSBill Paul 	struct rl_softc		*sc;
463a94100faSBill Paul 	u_int32_t		rval;
464a94100faSBill Paul 	int			i;
465a94100faSBill Paul 
466a94100faSBill Paul 	sc = device_get_softc(dev);
467a94100faSBill Paul 
468a94100faSBill Paul 	CSR_WRITE_4(sc, RL_PHYAR, (reg << 16) |
4699bac70b8SBill Paul 	    (data & RL_PHYAR_PHYDATA) | RL_PHYAR_BUSY);
470a94100faSBill Paul 
47196b774f4SPyun YongHyeon 	for (i = 0; i < RL_PHY_TIMEOUT; i++) {
472a94100faSBill Paul 		rval = CSR_READ_4(sc, RL_PHYAR);
473a94100faSBill Paul 		if (!(rval & RL_PHYAR_BUSY))
474a94100faSBill Paul 			break;
4752bc085c6SPyun YongHyeon 		DELAY(25);
476a94100faSBill Paul 	}
477a94100faSBill Paul 
47896b774f4SPyun YongHyeon 	if (i == RL_PHY_TIMEOUT) {
4796b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "PHY write failed\n");
480a94100faSBill Paul 		return (0);
481a94100faSBill Paul 	}
482a94100faSBill Paul 
4832bc085c6SPyun YongHyeon 	/*
4842bc085c6SPyun YongHyeon 	 * Controller requires a 20us delay to process next MDIO request.
4852bc085c6SPyun YongHyeon 	 */
4862bc085c6SPyun YongHyeon 	DELAY(20);
4872bc085c6SPyun YongHyeon 
488a94100faSBill Paul 	return (0);
489a94100faSBill Paul }
490a94100faSBill Paul 
491a94100faSBill Paul static int
4927b5ffebfSPyun YongHyeon re_miibus_readreg(device_t dev, int phy, int reg)
493a94100faSBill Paul {
494a94100faSBill Paul 	struct rl_softc		*sc;
495a94100faSBill Paul 	u_int16_t		rval = 0;
496a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
497a94100faSBill Paul 
498a94100faSBill Paul 	sc = device_get_softc(dev);
499a94100faSBill Paul 
500a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
501a94100faSBill Paul 		rval = re_gmii_readreg(dev, phy, reg);
502a94100faSBill Paul 		return (rval);
503a94100faSBill Paul 	}
504a94100faSBill Paul 
505a94100faSBill Paul 	switch (reg) {
506a94100faSBill Paul 	case MII_BMCR:
507a94100faSBill Paul 		re8139_reg = RL_BMCR;
508a94100faSBill Paul 		break;
509a94100faSBill Paul 	case MII_BMSR:
510a94100faSBill Paul 		re8139_reg = RL_BMSR;
511a94100faSBill Paul 		break;
512a94100faSBill Paul 	case MII_ANAR:
513a94100faSBill Paul 		re8139_reg = RL_ANAR;
514a94100faSBill Paul 		break;
515a94100faSBill Paul 	case MII_ANER:
516a94100faSBill Paul 		re8139_reg = RL_ANER;
517a94100faSBill Paul 		break;
518a94100faSBill Paul 	case MII_ANLPAR:
519a94100faSBill Paul 		re8139_reg = RL_LPAR;
520a94100faSBill Paul 		break;
521a94100faSBill Paul 	case MII_PHYIDR1:
522a94100faSBill Paul 	case MII_PHYIDR2:
523a94100faSBill Paul 		return (0);
524a94100faSBill Paul 	/*
525a94100faSBill Paul 	 * Allow the rlphy driver to read the media status
526a94100faSBill Paul 	 * register. If we have a link partner which does not
527a94100faSBill Paul 	 * support NWAY, this is the register which will tell
528a94100faSBill Paul 	 * us the results of parallel detection.
529a94100faSBill Paul 	 */
530a94100faSBill Paul 	case RL_MEDIASTAT:
531a94100faSBill Paul 		rval = CSR_READ_1(sc, RL_MEDIASTAT);
532a94100faSBill Paul 		return (rval);
533a94100faSBill Paul 	default:
5346b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
535a94100faSBill Paul 		return (0);
536a94100faSBill Paul 	}
537a94100faSBill Paul 	rval = CSR_READ_2(sc, re8139_reg);
538baa12772SPyun YongHyeon 	if (sc->rl_type == RL_8139CPLUS && re8139_reg == RL_BMCR) {
539baa12772SPyun YongHyeon 		/* 8139C+ has different bit layout. */
540baa12772SPyun YongHyeon 		rval &= ~(BMCR_LOOP | BMCR_ISO);
541baa12772SPyun YongHyeon 	}
542a94100faSBill Paul 	return (rval);
543a94100faSBill Paul }
544a94100faSBill Paul 
545a94100faSBill Paul static int
5467b5ffebfSPyun YongHyeon re_miibus_writereg(device_t dev, int phy, int reg, int data)
547a94100faSBill Paul {
548a94100faSBill Paul 	struct rl_softc		*sc;
549a94100faSBill Paul 	u_int16_t		re8139_reg = 0;
550a94100faSBill Paul 	int			rval = 0;
551a94100faSBill Paul 
552a94100faSBill Paul 	sc = device_get_softc(dev);
553a94100faSBill Paul 
554a94100faSBill Paul 	if (sc->rl_type == RL_8169) {
555a94100faSBill Paul 		rval = re_gmii_writereg(dev, phy, reg, data);
556a94100faSBill Paul 		return (rval);
557a94100faSBill Paul 	}
558a94100faSBill Paul 
559a94100faSBill Paul 	switch (reg) {
560a94100faSBill Paul 	case MII_BMCR:
561a94100faSBill Paul 		re8139_reg = RL_BMCR;
562baa12772SPyun YongHyeon 		if (sc->rl_type == RL_8139CPLUS) {
563baa12772SPyun YongHyeon 			/* 8139C+ has different bit layout. */
564baa12772SPyun YongHyeon 			data &= ~(BMCR_LOOP | BMCR_ISO);
565baa12772SPyun YongHyeon 		}
566a94100faSBill Paul 		break;
567a94100faSBill Paul 	case MII_BMSR:
568a94100faSBill Paul 		re8139_reg = RL_BMSR;
569a94100faSBill Paul 		break;
570a94100faSBill Paul 	case MII_ANAR:
571a94100faSBill Paul 		re8139_reg = RL_ANAR;
572a94100faSBill Paul 		break;
573a94100faSBill Paul 	case MII_ANER:
574a94100faSBill Paul 		re8139_reg = RL_ANER;
575a94100faSBill Paul 		break;
576a94100faSBill Paul 	case MII_ANLPAR:
577a94100faSBill Paul 		re8139_reg = RL_LPAR;
578a94100faSBill Paul 		break;
579a94100faSBill Paul 	case MII_PHYIDR1:
580a94100faSBill Paul 	case MII_PHYIDR2:
581a94100faSBill Paul 		return (0);
582a94100faSBill Paul 		break;
583a94100faSBill Paul 	default:
5846b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "bad phy register\n");
585a94100faSBill Paul 		return (0);
586a94100faSBill Paul 	}
587a94100faSBill Paul 	CSR_WRITE_2(sc, re8139_reg, data);
588a94100faSBill Paul 	return (0);
589a94100faSBill Paul }
590a94100faSBill Paul 
591a94100faSBill Paul static void
5927b5ffebfSPyun YongHyeon re_miibus_statchg(device_t dev)
593a94100faSBill Paul {
594130b6dfbSPyun YongHyeon 	struct rl_softc		*sc;
595130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
596130b6dfbSPyun YongHyeon 	struct mii_data		*mii;
597a11e2f18SBruce M Simpson 
598130b6dfbSPyun YongHyeon 	sc = device_get_softc(dev);
599130b6dfbSPyun YongHyeon 	mii = device_get_softc(sc->rl_miibus);
600130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
601130b6dfbSPyun YongHyeon 	if (mii == NULL || ifp == NULL ||
602130b6dfbSPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
603130b6dfbSPyun YongHyeon 		return;
604130b6dfbSPyun YongHyeon 
605130b6dfbSPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
606130b6dfbSPyun YongHyeon 	if ((mii->mii_media_status & (IFM_ACTIVE | IFM_AVALID)) ==
607130b6dfbSPyun YongHyeon 	    (IFM_ACTIVE | IFM_AVALID)) {
608130b6dfbSPyun YongHyeon 		switch (IFM_SUBTYPE(mii->mii_media_active)) {
609130b6dfbSPyun YongHyeon 		case IFM_10_T:
610130b6dfbSPyun YongHyeon 		case IFM_100_TX:
611130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
612130b6dfbSPyun YongHyeon 			break;
613130b6dfbSPyun YongHyeon 		case IFM_1000_T:
614130b6dfbSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_FASTETHER) != 0)
615130b6dfbSPyun YongHyeon 				break;
616130b6dfbSPyun YongHyeon 			sc->rl_flags |= RL_FLAG_LINK;
617130b6dfbSPyun YongHyeon 			break;
618130b6dfbSPyun YongHyeon 		default:
619130b6dfbSPyun YongHyeon 			break;
620130b6dfbSPyun YongHyeon 		}
621130b6dfbSPyun YongHyeon 	}
622130b6dfbSPyun YongHyeon 	/*
623130b6dfbSPyun YongHyeon 	 * RealTek controllers does not provide any interface to
624130b6dfbSPyun YongHyeon 	 * Tx/Rx MACs for resolved speed, duplex and flow-control
625130b6dfbSPyun YongHyeon 	 * parameters.
626130b6dfbSPyun YongHyeon 	 */
627a94100faSBill Paul }
628a94100faSBill Paul 
629a94100faSBill Paul /*
630ff191365SJung-uk Kim  * Set the RX configuration and 64-bit multicast hash filter.
631a94100faSBill Paul  */
632a94100faSBill Paul static void
633ff191365SJung-uk Kim re_set_rxmode(struct rl_softc *sc)
634a94100faSBill Paul {
635a94100faSBill Paul 	struct ifnet		*ifp;
636a94100faSBill Paul 	struct ifmultiaddr	*ifma;
637ff191365SJung-uk Kim 	uint32_t		hashes[2] = { 0, 0 };
638ff191365SJung-uk Kim 	uint32_t		h, rxfilt;
639a94100faSBill Paul 
64097b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
64197b9d4baSJohn-Mark Gurney 
642fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
643a94100faSBill Paul 
644ff191365SJung-uk Kim 	rxfilt = RL_RXCFG_CONFIG | RL_RXCFG_RX_INDIV | RL_RXCFG_RX_BROAD;
645a94100faSBill Paul 
646ff191365SJung-uk Kim 	if (ifp->if_flags & (IFF_ALLMULTI | IFF_PROMISC)) {
6477c103000SPyun YongHyeon 		if (ifp->if_flags & IFF_PROMISC)
6487c103000SPyun YongHyeon 			rxfilt |= RL_RXCFG_RX_ALLPHYS;
649a0637caaSPyun YongHyeon 		/*
650a0637caaSPyun YongHyeon 		 * Unlike other hardwares, we have to explicitly set
651a0637caaSPyun YongHyeon 		 * RL_RXCFG_RX_MULTI to receive multicast frames in
652a0637caaSPyun YongHyeon 		 * promiscuous mode.
653a0637caaSPyun YongHyeon 		 */
654a94100faSBill Paul 		rxfilt |= RL_RXCFG_RX_MULTI;
655ff191365SJung-uk Kim 		hashes[0] = hashes[1] = 0xffffffff;
656ff191365SJung-uk Kim 		goto done;
657a94100faSBill Paul 	}
658a94100faSBill Paul 
659eb956cd0SRobert Watson 	if_maddr_rlock(ifp);
660a94100faSBill Paul 	TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
661a94100faSBill Paul 		if (ifma->ifma_addr->sa_family != AF_LINK)
662a94100faSBill Paul 			continue;
6630e939c0cSChristian Weisgerber 		h = ether_crc32_be(LLADDR((struct sockaddr_dl *)
6640e939c0cSChristian Weisgerber 		    ifma->ifma_addr), ETHER_ADDR_LEN) >> 26;
665a94100faSBill Paul 		if (h < 32)
666a94100faSBill Paul 			hashes[0] |= (1 << h);
667a94100faSBill Paul 		else
668a94100faSBill Paul 			hashes[1] |= (1 << (h - 32));
669a94100faSBill Paul 	}
670eb956cd0SRobert Watson 	if_maddr_runlock(ifp);
671a94100faSBill Paul 
672ff191365SJung-uk Kim 	if (hashes[0] != 0 || hashes[1] != 0) {
673bb7dfefbSBill Paul 		/*
674ff191365SJung-uk Kim 		 * For some unfathomable reason, RealTek decided to
675ff191365SJung-uk Kim 		 * reverse the order of the multicast hash registers
676ff191365SJung-uk Kim 		 * in the PCI Express parts.  This means we have to
677ff191365SJung-uk Kim 		 * write the hash pattern in reverse order for those
678ff191365SJung-uk Kim 		 * devices.
679bb7dfefbSBill Paul 		 */
680aaab4fbeSJung-uk Kim 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0) {
681ff191365SJung-uk Kim 			h = bswap32(hashes[0]);
682ff191365SJung-uk Kim 			hashes[0] = bswap32(hashes[1]);
683ff191365SJung-uk Kim 			hashes[1] = h;
684ff191365SJung-uk Kim 		}
685ff191365SJung-uk Kim 		rxfilt |= RL_RXCFG_RX_MULTI;
686ff191365SJung-uk Kim 	}
687ff191365SJung-uk Kim 
688ff191365SJung-uk Kim done:
689a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR0, hashes[0]);
690a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MAR4, hashes[1]);
691ff191365SJung-uk Kim 	CSR_WRITE_4(sc, RL_RXCFG, rxfilt);
692bb7dfefbSBill Paul }
693a94100faSBill Paul 
694a94100faSBill Paul static void
6957b5ffebfSPyun YongHyeon re_reset(struct rl_softc *sc)
696a94100faSBill Paul {
6970ce0868aSPyun YongHyeon 	int			i;
698a94100faSBill Paul 
69997b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
70097b9d4baSJohn-Mark Gurney 
701a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RESET);
702a94100faSBill Paul 
703a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
704a94100faSBill Paul 		DELAY(10);
705a94100faSBill Paul 		if (!(CSR_READ_1(sc, RL_COMMAND) & RL_CMD_RESET))
706a94100faSBill Paul 			break;
707a94100faSBill Paul 	}
708a94100faSBill Paul 	if (i == RL_TIMEOUT)
7096b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "reset never completed!\n");
710a94100faSBill Paul 
711566ca8caSJung-uk Kim 	if ((sc->rl_flags & RL_FLAG_MACRESET) != 0)
712a94100faSBill Paul 		CSR_WRITE_1(sc, 0x82, 1);
71381eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169S)
714566ca8caSJung-uk Kim 		re_gmii_writereg(sc->rl_dev, 1, 0x0b, 0);
715a94100faSBill Paul }
716a94100faSBill Paul 
717ed510fb0SBill Paul #ifdef RE_DIAG
718ed510fb0SBill Paul 
719a94100faSBill Paul /*
720a94100faSBill Paul  * The following routine is designed to test for a defect on some
721a94100faSBill Paul  * 32-bit 8169 cards. Some of these NICs have the REQ64# and ACK64#
722a94100faSBill Paul  * lines connected to the bus, however for a 32-bit only card, they
723a94100faSBill Paul  * should be pulled high. The result of this defect is that the
724a94100faSBill Paul  * NIC will not work right if you plug it into a 64-bit slot: DMA
725a94100faSBill Paul  * operations will be done with 64-bit transfers, which will fail
726a94100faSBill Paul  * because the 64-bit data lines aren't connected.
727a94100faSBill Paul  *
728a94100faSBill Paul  * There's no way to work around this (short of talking a soldering
729a94100faSBill Paul  * iron to the board), however we can detect it. The method we use
730a94100faSBill Paul  * here is to put the NIC into digital loopback mode, set the receiver
731a94100faSBill Paul  * to promiscuous mode, and then try to send a frame. We then compare
732a94100faSBill Paul  * the frame data we sent to what was received. If the data matches,
733a94100faSBill Paul  * then the NIC is working correctly, otherwise we know the user has
734a94100faSBill Paul  * a defective NIC which has been mistakenly plugged into a 64-bit PCI
735a94100faSBill Paul  * slot. In the latter case, there's no way the NIC can work correctly,
736a94100faSBill Paul  * so we print out a message on the console and abort the device attach.
737a94100faSBill Paul  */
738a94100faSBill Paul 
739a94100faSBill Paul static int
7407b5ffebfSPyun YongHyeon re_diag(struct rl_softc *sc)
741a94100faSBill Paul {
742fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
743a94100faSBill Paul 	struct mbuf		*m0;
744a94100faSBill Paul 	struct ether_header	*eh;
745a94100faSBill Paul 	struct rl_desc		*cur_rx;
746a94100faSBill Paul 	u_int16_t		status;
747a94100faSBill Paul 	u_int32_t		rxstat;
748ed510fb0SBill Paul 	int			total_len, i, error = 0, phyaddr;
749a94100faSBill Paul 	u_int8_t		dst[] = { 0x00, 'h', 'e', 'l', 'l', 'o' };
750a94100faSBill Paul 	u_int8_t		src[] = { 0x00, 'w', 'o', 'r', 'l', 'd' };
751a94100faSBill Paul 
752a94100faSBill Paul 	/* Allocate a single mbuf */
753a94100faSBill Paul 	MGETHDR(m0, M_DONTWAIT, MT_DATA);
754a94100faSBill Paul 	if (m0 == NULL)
755a94100faSBill Paul 		return (ENOBUFS);
756a94100faSBill Paul 
75797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
75897b9d4baSJohn-Mark Gurney 
759a94100faSBill Paul 	/*
760a94100faSBill Paul 	 * Initialize the NIC in test mode. This sets the chip up
761a94100faSBill Paul 	 * so that it can send and receive frames, but performs the
762a94100faSBill Paul 	 * following special functions:
763a94100faSBill Paul 	 * - Puts receiver in promiscuous mode
764a94100faSBill Paul 	 * - Enables digital loopback mode
765a94100faSBill Paul 	 * - Leaves interrupts turned off
766a94100faSBill Paul 	 */
767a94100faSBill Paul 
768a94100faSBill Paul 	ifp->if_flags |= IFF_PROMISC;
769a94100faSBill Paul 	sc->rl_testmode = 1;
7708476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
77197b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
772351a76f9SPyun YongHyeon 	sc->rl_flags |= RL_FLAG_LINK;
773ed510fb0SBill Paul 	if (sc->rl_type == RL_8169)
774ed510fb0SBill Paul 		phyaddr = 1;
775ed510fb0SBill Paul 	else
776ed510fb0SBill Paul 		phyaddr = 0;
777ed510fb0SBill Paul 
778ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_RESET);
779ed510fb0SBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
780ed510fb0SBill Paul 		status = re_miibus_readreg(sc->rl_dev, phyaddr, MII_BMCR);
781ed510fb0SBill Paul 		if (!(status & BMCR_RESET))
782ed510fb0SBill Paul 			break;
783ed510fb0SBill Paul 	}
784ed510fb0SBill Paul 
785ed510fb0SBill Paul 	re_miibus_writereg(sc->rl_dev, phyaddr, MII_BMCR, BMCR_LOOP);
786ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS);
787ed510fb0SBill Paul 
788804af9a1SBill Paul 	DELAY(100000);
789a94100faSBill Paul 
790a94100faSBill Paul 	/* Put some data in the mbuf */
791a94100faSBill Paul 
792a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
793a94100faSBill Paul 	bcopy ((char *)&dst, eh->ether_dhost, ETHER_ADDR_LEN);
794a94100faSBill Paul 	bcopy ((char *)&src, eh->ether_shost, ETHER_ADDR_LEN);
795a94100faSBill Paul 	eh->ether_type = htons(ETHERTYPE_IP);
796a94100faSBill Paul 	m0->m_pkthdr.len = m0->m_len = ETHER_MIN_LEN - ETHER_CRC_LEN;
797a94100faSBill Paul 
7987cae6651SBill Paul 	/*
7997cae6651SBill Paul 	 * Queue the packet, start transmission.
8007cae6651SBill Paul 	 * Note: IF_HANDOFF() ultimately calls re_start() for us.
8017cae6651SBill Paul 	 */
802a94100faSBill Paul 
803abc8ff44SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
80497b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
80552732175SMax Laier 	/* XXX: re_diag must not be called when in ALTQ mode */
8067cae6651SBill Paul 	IF_HANDOFF(&ifp->if_snd, m0, ifp);
80797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
808a94100faSBill Paul 	m0 = NULL;
809a94100faSBill Paul 
810a94100faSBill Paul 	/* Wait for it to propagate through the chip */
811a94100faSBill Paul 
812abc8ff44SBill Paul 	DELAY(100000);
813a94100faSBill Paul 	for (i = 0; i < RL_TIMEOUT; i++) {
814a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
815ed510fb0SBill Paul 		CSR_WRITE_2(sc, RL_ISR, status);
816abc8ff44SBill Paul 		if ((status & (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK)) ==
817abc8ff44SBill Paul 		    (RL_ISR_TIMEOUT_EXPIRED|RL_ISR_RX_OK))
818a94100faSBill Paul 			break;
819a94100faSBill Paul 		DELAY(10);
820a94100faSBill Paul 	}
821a94100faSBill Paul 
822a94100faSBill Paul 	if (i == RL_TIMEOUT) {
8236b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8246b9f5c94SGleb Smirnoff 		    "diagnostic failed, failed to receive packet in"
8256b9f5c94SGleb Smirnoff 		    " loopback mode\n");
826a94100faSBill Paul 		error = EIO;
827a94100faSBill Paul 		goto done;
828a94100faSBill Paul 	}
829a94100faSBill Paul 
830a94100faSBill Paul 	/*
831a94100faSBill Paul 	 * The packet should have been dumped into the first
832a94100faSBill Paul 	 * entry in the RX DMA ring. Grab it from there.
833a94100faSBill Paul 	 */
834a94100faSBill Paul 
835a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
836a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
837a94100faSBill Paul 	    BUS_DMASYNC_POSTREAD);
838d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag,
839d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap,
840d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD);
841d65abd66SPyun YongHyeon 	bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
842d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc[0].rx_dmamap);
843a94100faSBill Paul 
844d65abd66SPyun YongHyeon 	m0 = sc->rl_ldata.rl_rx_desc[0].rx_m;
845d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_desc[0].rx_m = NULL;
846a94100faSBill Paul 	eh = mtod(m0, struct ether_header *);
847a94100faSBill Paul 
848a94100faSBill Paul 	cur_rx = &sc->rl_ldata.rl_rx_list[0];
849a94100faSBill Paul 	total_len = RL_RXBYTES(cur_rx);
850a94100faSBill Paul 	rxstat = le32toh(cur_rx->rl_cmdstat);
851a94100faSBill Paul 
852a94100faSBill Paul 	if (total_len != ETHER_MIN_LEN) {
8536b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev,
8546b9f5c94SGleb Smirnoff 		    "diagnostic failed, received short packet\n");
855a94100faSBill Paul 		error = EIO;
856a94100faSBill Paul 		goto done;
857a94100faSBill Paul 	}
858a94100faSBill Paul 
859a94100faSBill Paul 	/* Test that the received packet data matches what we sent. */
860a94100faSBill Paul 
861a94100faSBill Paul 	if (bcmp((char *)&eh->ether_dhost, (char *)&dst, ETHER_ADDR_LEN) ||
862a94100faSBill Paul 	    bcmp((char *)&eh->ether_shost, (char *)&src, ETHER_ADDR_LEN) ||
863a94100faSBill Paul 	    ntohs(eh->ether_type) != ETHERTYPE_IP) {
8646b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "WARNING, DMA FAILURE!\n");
8656b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "expected TX data: %6D/%6D/0x%x\n",
866a94100faSBill Paul 		    dst, ":", src, ":", ETHERTYPE_IP);
8676b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "received RX data: %6D/%6D/0x%x\n",
868a94100faSBill Paul 		    eh->ether_dhost, ":",  eh->ether_shost, ":",
869a94100faSBill Paul 		    ntohs(eh->ether_type));
8706b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "You may have a defective 32-bit "
8716b9f5c94SGleb Smirnoff 		    "NIC plugged into a 64-bit PCI slot.\n");
8726b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Please re-install the NIC in a "
8736b9f5c94SGleb Smirnoff 		    "32-bit slot for proper operation.\n");
8746b9f5c94SGleb Smirnoff 		device_printf(sc->rl_dev, "Read the re(4) man page for more "
8756b9f5c94SGleb Smirnoff 		    "details.\n");
876a94100faSBill Paul 		error = EIO;
877a94100faSBill Paul 	}
878a94100faSBill Paul 
879a94100faSBill Paul done:
880a94100faSBill Paul 	/* Turn interface off, release resources */
881a94100faSBill Paul 
882a94100faSBill Paul 	sc->rl_testmode = 0;
883351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
884a94100faSBill Paul 	ifp->if_flags &= ~IFF_PROMISC;
885a94100faSBill Paul 	re_stop(sc);
886a94100faSBill Paul 	if (m0 != NULL)
887a94100faSBill Paul 		m_freem(m0);
888a94100faSBill Paul 
88997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
89097b9d4baSJohn-Mark Gurney 
891a94100faSBill Paul 	return (error);
892a94100faSBill Paul }
893a94100faSBill Paul 
894ed510fb0SBill Paul #endif
895ed510fb0SBill Paul 
896a94100faSBill Paul /*
897a94100faSBill Paul  * Probe for a RealTek 8139C+/8169/8110 chip. Check the PCI vendor and device
898a94100faSBill Paul  * IDs against our list and return a device name if we find a match.
899a94100faSBill Paul  */
900a94100faSBill Paul static int
9017b5ffebfSPyun YongHyeon re_probe(device_t dev)
902a94100faSBill Paul {
903a94100faSBill Paul 	struct rl_type		*t;
904dfdb409eSPyun YongHyeon 	uint16_t		devid, vendor;
905dfdb409eSPyun YongHyeon 	uint16_t		revid, sdevid;
906dfdb409eSPyun YongHyeon 	int			i;
907a94100faSBill Paul 
908dfdb409eSPyun YongHyeon 	vendor = pci_get_vendor(dev);
909dfdb409eSPyun YongHyeon 	devid = pci_get_device(dev);
910dfdb409eSPyun YongHyeon 	revid = pci_get_revid(dev);
911dfdb409eSPyun YongHyeon 	sdevid = pci_get_subdevice(dev);
912a94100faSBill Paul 
913dfdb409eSPyun YongHyeon 	if (vendor == LINKSYS_VENDORID && devid == LINKSYS_DEVICEID_EG1032) {
914dfdb409eSPyun YongHyeon 		if (sdevid != LINKSYS_SUBDEVICE_EG1032_REV3) {
91526390635SJohn Baldwin 			/*
91626390635SJohn Baldwin 			 * Only attach to rev. 3 of the Linksys EG1032 adapter.
917dfdb409eSPyun YongHyeon 			 * Rev. 2 is supported by sk(4).
91826390635SJohn Baldwin 			 */
919a94100faSBill Paul 			return (ENXIO);
920a94100faSBill Paul 		}
921dfdb409eSPyun YongHyeon 	}
922dfdb409eSPyun YongHyeon 
923dfdb409eSPyun YongHyeon 	if (vendor == RT_VENDORID && devid == RT_DEVICEID_8139) {
924dfdb409eSPyun YongHyeon 		if (revid != 0x20) {
925dfdb409eSPyun YongHyeon 			/* 8139, let rl(4) take care of this device. */
926dfdb409eSPyun YongHyeon 			return (ENXIO);
927dfdb409eSPyun YongHyeon 		}
928dfdb409eSPyun YongHyeon 	}
929dfdb409eSPyun YongHyeon 
930dfdb409eSPyun YongHyeon 	t = re_devs;
931dfdb409eSPyun YongHyeon 	for (i = 0; i < sizeof(re_devs) / sizeof(re_devs[0]); i++, t++) {
932dfdb409eSPyun YongHyeon 		if (vendor == t->rl_vid && devid == t->rl_did) {
933a94100faSBill Paul 			device_set_desc(dev, t->rl_name);
934d2b677bbSWarner Losh 			return (BUS_PROBE_DEFAULT);
935a94100faSBill Paul 		}
936a94100faSBill Paul 	}
937a94100faSBill Paul 
938a94100faSBill Paul 	return (ENXIO);
939a94100faSBill Paul }
940a94100faSBill Paul 
941a94100faSBill Paul /*
942a94100faSBill Paul  * Map a single buffer address.
943a94100faSBill Paul  */
944a94100faSBill Paul 
945a94100faSBill Paul static void
9467b5ffebfSPyun YongHyeon re_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
947a94100faSBill Paul {
9488fd99e38SPyun YongHyeon 	bus_addr_t		*addr;
949a94100faSBill Paul 
950a94100faSBill Paul 	if (error)
951a94100faSBill Paul 		return;
952a94100faSBill Paul 
953a94100faSBill Paul 	KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
954a94100faSBill Paul 	addr = arg;
955a94100faSBill Paul 	*addr = segs->ds_addr;
956a94100faSBill Paul }
957a94100faSBill Paul 
958a94100faSBill Paul static int
9597b5ffebfSPyun YongHyeon re_allocmem(device_t dev, struct rl_softc *sc)
960a94100faSBill Paul {
96166366ca4SPyun YongHyeon 	bus_addr_t		lowaddr;
962d65abd66SPyun YongHyeon 	bus_size_t		rx_list_size, tx_list_size;
963a94100faSBill Paul 	int			error;
964a94100faSBill Paul 	int			i;
965a94100faSBill Paul 
966d65abd66SPyun YongHyeon 	rx_list_size = sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc);
967d65abd66SPyun YongHyeon 	tx_list_size = sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc);
968d65abd66SPyun YongHyeon 
969d65abd66SPyun YongHyeon 	/*
970d65abd66SPyun YongHyeon 	 * Allocate the parent bus DMA tag appropriate for PCI.
971ce628393SPyun YongHyeon 	 * In order to use DAC, RL_CPLUSCMD_PCI_DAC bit of RL_CPLUS_CMD
972ce628393SPyun YongHyeon 	 * register should be set. However some RealTek chips are known
973ce628393SPyun YongHyeon 	 * to be buggy on DAC handling, therefore disable DAC by limiting
974ce628393SPyun YongHyeon 	 * DMA address space to 32bit. PCIe variants of RealTek chips
97566366ca4SPyun YongHyeon 	 * may not have the limitation.
976d65abd66SPyun YongHyeon 	 */
97766366ca4SPyun YongHyeon 	lowaddr = BUS_SPACE_MAXADDR;
97866366ca4SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PCIE) == 0)
97966366ca4SPyun YongHyeon 		lowaddr = BUS_SPACE_MAXADDR_32BIT;
980d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(bus_get_dma_tag(dev), 1, 0,
98166366ca4SPyun YongHyeon 	    lowaddr, BUS_SPACE_MAXADDR, NULL, NULL,
982d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXSIZE_32BIT, 0, BUS_SPACE_MAXSIZE_32BIT, 0,
983d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_parent_tag);
984d65abd66SPyun YongHyeon 	if (error) {
985d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate parent DMA tag\n");
986d65abd66SPyun YongHyeon 		return (error);
987d65abd66SPyun YongHyeon 	}
988d65abd66SPyun YongHyeon 
989d65abd66SPyun YongHyeon 	/*
990d65abd66SPyun YongHyeon 	 * Allocate map for TX mbufs.
991d65abd66SPyun YongHyeon 	 */
992d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, 1, 0,
993d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL,
994d65abd66SPyun YongHyeon 	    NULL, MCLBYTES * RL_NTXSEGS, RL_NTXSEGS, 4096, 0,
995d65abd66SPyun YongHyeon 	    NULL, NULL, &sc->rl_ldata.rl_tx_mtag);
996d65abd66SPyun YongHyeon 	if (error) {
997d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA tag\n");
998d65abd66SPyun YongHyeon 		return (error);
999d65abd66SPyun YongHyeon 	}
1000d65abd66SPyun YongHyeon 
1001a94100faSBill Paul 	/*
1002a94100faSBill Paul 	 * Allocate map for RX mbufs.
1003a94100faSBill Paul 	 */
1004d65abd66SPyun YongHyeon 
100581eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
100681eee0ebSPyun YongHyeon 		error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t),
100781eee0ebSPyun YongHyeon 		    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
100881eee0ebSPyun YongHyeon 		    MJUM9BYTES, 1, MJUM9BYTES, 0, NULL, NULL,
100981eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_mtag);
101081eee0ebSPyun YongHyeon 		if (error) {
101181eee0ebSPyun YongHyeon 			device_printf(dev,
101281eee0ebSPyun YongHyeon 			    "could not allocate jumbo RX DMA tag\n");
101381eee0ebSPyun YongHyeon 			return (error);
101481eee0ebSPyun YongHyeon 		}
101581eee0ebSPyun YongHyeon 	}
1016d65abd66SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, sizeof(uint64_t), 0,
1017d65abd66SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
1018d65abd66SPyun YongHyeon 	    MCLBYTES, 1, MCLBYTES, 0, NULL, NULL, &sc->rl_ldata.rl_rx_mtag);
1019a94100faSBill Paul 	if (error) {
1020d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA tag\n");
1021d65abd66SPyun YongHyeon 		return (error);
1022a94100faSBill Paul 	}
1023a94100faSBill Paul 
1024a94100faSBill Paul 	/*
1025a94100faSBill Paul 	 * Allocate map for TX descriptor list.
1026a94100faSBill Paul 	 */
1027a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1028a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1029d65abd66SPyun YongHyeon 	    NULL, tx_list_size, 1, tx_list_size, 0,
1030a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_tx_list_tag);
1031a94100faSBill Paul 	if (error) {
1032d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring tag\n");
1033d65abd66SPyun YongHyeon 		return (error);
1034a94100faSBill Paul 	}
1035a94100faSBill Paul 
1036a94100faSBill Paul 	/* Allocate DMA'able memory for the TX ring */
1037a94100faSBill Paul 
1038a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_tx_list_tag,
1039d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_tx_list,
1040d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1041a94100faSBill Paul 	    &sc->rl_ldata.rl_tx_list_map);
1042d65abd66SPyun YongHyeon 	if (error) {
1043d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate TX DMA ring\n");
1044d65abd66SPyun YongHyeon 		return (error);
1045d65abd66SPyun YongHyeon 	}
1046a94100faSBill Paul 
1047a94100faSBill Paul 	/* Load the map for the TX ring. */
1048a94100faSBill Paul 
1049d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_list_addr = 0;
1050a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_tx_list_tag,
1051a94100faSBill Paul 	     sc->rl_ldata.rl_tx_list_map, sc->rl_ldata.rl_tx_list,
1052d65abd66SPyun YongHyeon 	     tx_list_size, re_dma_map_addr,
1053a94100faSBill Paul 	     &sc->rl_ldata.rl_tx_list_addr, BUS_DMA_NOWAIT);
1054d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_tx_list_addr == 0) {
1055d65abd66SPyun YongHyeon 		device_printf(dev, "could not load TX DMA ring\n");
1056d65abd66SPyun YongHyeon 		return (ENOMEM);
1057d65abd66SPyun YongHyeon 	}
1058a94100faSBill Paul 
1059a94100faSBill Paul 	/* Create DMA maps for TX buffers */
1060a94100faSBill Paul 
1061d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
1062d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_tx_mtag, 0,
1063d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
1064a94100faSBill Paul 		if (error) {
1065d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for TX\n");
1066d65abd66SPyun YongHyeon 			return (error);
1067a94100faSBill Paul 		}
1068a94100faSBill Paul 	}
1069a94100faSBill Paul 
1070a94100faSBill Paul 	/*
1071a94100faSBill Paul 	 * Allocate map for RX descriptor list.
1072a94100faSBill Paul 	 */
1073a94100faSBill Paul 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_RING_ALIGN,
1074a94100faSBill Paul 	    0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL,
1075d65abd66SPyun YongHyeon 	    NULL, rx_list_size, 1, rx_list_size, 0,
1076a94100faSBill Paul 	    NULL, NULL, &sc->rl_ldata.rl_rx_list_tag);
1077a94100faSBill Paul 	if (error) {
1078d65abd66SPyun YongHyeon 		device_printf(dev, "could not create RX DMA ring tag\n");
1079d65abd66SPyun YongHyeon 		return (error);
1080a94100faSBill Paul 	}
1081a94100faSBill Paul 
1082a94100faSBill Paul 	/* Allocate DMA'able memory for the RX ring */
1083a94100faSBill Paul 
1084a94100faSBill Paul 	error = bus_dmamem_alloc(sc->rl_ldata.rl_rx_list_tag,
1085d65abd66SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_rx_list,
1086d65abd66SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
1087a94100faSBill Paul 	    &sc->rl_ldata.rl_rx_list_map);
1088d65abd66SPyun YongHyeon 	if (error) {
1089d65abd66SPyun YongHyeon 		device_printf(dev, "could not allocate RX DMA ring\n");
1090d65abd66SPyun YongHyeon 		return (error);
1091d65abd66SPyun YongHyeon 	}
1092a94100faSBill Paul 
1093a94100faSBill Paul 	/* Load the map for the RX ring. */
1094a94100faSBill Paul 
1095d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_list_addr = 0;
1096a94100faSBill Paul 	error = bus_dmamap_load(sc->rl_ldata.rl_rx_list_tag,
1097a94100faSBill Paul 	     sc->rl_ldata.rl_rx_list_map, sc->rl_ldata.rl_rx_list,
1098d65abd66SPyun YongHyeon 	     rx_list_size, re_dma_map_addr,
1099a94100faSBill Paul 	     &sc->rl_ldata.rl_rx_list_addr, BUS_DMA_NOWAIT);
1100d65abd66SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_rx_list_addr == 0) {
1101d65abd66SPyun YongHyeon 		device_printf(dev, "could not load RX DMA ring\n");
1102d65abd66SPyun YongHyeon 		return (ENOMEM);
1103d65abd66SPyun YongHyeon 	}
1104a94100faSBill Paul 
1105a94100faSBill Paul 	/* Create DMA maps for RX buffers */
1106a94100faSBill Paul 
110781eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
110881eee0ebSPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
110981eee0ebSPyun YongHyeon 		    &sc->rl_ldata.rl_jrx_sparemap);
111081eee0ebSPyun YongHyeon 		if (error) {
111181eee0ebSPyun YongHyeon 			device_printf(dev,
111281eee0ebSPyun YongHyeon 			    "could not create spare DMA map for jumbo RX\n");
111381eee0ebSPyun YongHyeon 			return (error);
111481eee0ebSPyun YongHyeon 		}
111581eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
111681eee0ebSPyun YongHyeon 			error = bus_dmamap_create(sc->rl_ldata.rl_jrx_mtag, 0,
111781eee0ebSPyun YongHyeon 			    &sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
111881eee0ebSPyun YongHyeon 			if (error) {
111981eee0ebSPyun YongHyeon 				device_printf(dev,
112081eee0ebSPyun YongHyeon 				    "could not create DMA map for jumbo RX\n");
112181eee0ebSPyun YongHyeon 				return (error);
112281eee0ebSPyun YongHyeon 			}
112381eee0ebSPyun YongHyeon 		}
112481eee0ebSPyun YongHyeon 	}
1125d65abd66SPyun YongHyeon 	error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1126d65abd66SPyun YongHyeon 	    &sc->rl_ldata.rl_rx_sparemap);
1127a94100faSBill Paul 	if (error) {
1128d65abd66SPyun YongHyeon 		device_printf(dev, "could not create spare DMA map for RX\n");
1129d65abd66SPyun YongHyeon 		return (error);
1130d65abd66SPyun YongHyeon 	}
1131d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1132d65abd66SPyun YongHyeon 		error = bus_dmamap_create(sc->rl_ldata.rl_rx_mtag, 0,
1133d65abd66SPyun YongHyeon 		    &sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
1134d65abd66SPyun YongHyeon 		if (error) {
1135d65abd66SPyun YongHyeon 			device_printf(dev, "could not create DMA map for RX\n");
1136d65abd66SPyun YongHyeon 			return (error);
1137a94100faSBill Paul 		}
1138a94100faSBill Paul 	}
1139a94100faSBill Paul 
11400534aae0SPyun YongHyeon 	/* Create DMA map for statistics. */
11410534aae0SPyun YongHyeon 	error = bus_dma_tag_create(sc->rl_parent_tag, RL_DUMP_ALIGN, 0,
11420534aae0SPyun YongHyeon 	    BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR, NULL, NULL,
11430534aae0SPyun YongHyeon 	    sizeof(struct rl_stats), 1, sizeof(struct rl_stats), 0, NULL, NULL,
11440534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_stag);
11450534aae0SPyun YongHyeon 	if (error) {
11460534aae0SPyun YongHyeon 		device_printf(dev, "could not create statistics DMA tag\n");
11470534aae0SPyun YongHyeon 		return (error);
11480534aae0SPyun YongHyeon 	}
11490534aae0SPyun YongHyeon 	/* Allocate DMA'able memory for statistics. */
11500534aae0SPyun YongHyeon 	error = bus_dmamem_alloc(sc->rl_ldata.rl_stag,
11510534aae0SPyun YongHyeon 	    (void **)&sc->rl_ldata.rl_stats,
11520534aae0SPyun YongHyeon 	    BUS_DMA_WAITOK | BUS_DMA_COHERENT | BUS_DMA_ZERO,
11530534aae0SPyun YongHyeon 	    &sc->rl_ldata.rl_smap);
11540534aae0SPyun YongHyeon 	if (error) {
11550534aae0SPyun YongHyeon 		device_printf(dev,
11560534aae0SPyun YongHyeon 		    "could not allocate statistics DMA memory\n");
11570534aae0SPyun YongHyeon 		return (error);
11580534aae0SPyun YongHyeon 	}
11590534aae0SPyun YongHyeon 	/* Load the map for statistics. */
11600534aae0SPyun YongHyeon 	sc->rl_ldata.rl_stats_addr = 0;
11610534aae0SPyun YongHyeon 	error = bus_dmamap_load(sc->rl_ldata.rl_stag, sc->rl_ldata.rl_smap,
11620534aae0SPyun YongHyeon 	    sc->rl_ldata.rl_stats, sizeof(struct rl_stats), re_dma_map_addr,
11630534aae0SPyun YongHyeon 	     &sc->rl_ldata.rl_stats_addr, BUS_DMA_NOWAIT);
11640534aae0SPyun YongHyeon 	if (error != 0 || sc->rl_ldata.rl_stats_addr == 0) {
11650534aae0SPyun YongHyeon 		device_printf(dev, "could not load statistics DMA memory\n");
11660534aae0SPyun YongHyeon 		return (ENOMEM);
11670534aae0SPyun YongHyeon 	}
11680534aae0SPyun YongHyeon 
1169a94100faSBill Paul 	return (0);
1170a94100faSBill Paul }
1171a94100faSBill Paul 
1172a94100faSBill Paul /*
1173a94100faSBill Paul  * Attach the interface. Allocate softc structures, do ifmedia
1174a94100faSBill Paul  * setup and ethernet/BPF attach.
1175a94100faSBill Paul  */
1176a94100faSBill Paul static int
11777b5ffebfSPyun YongHyeon re_attach(device_t dev)
1178a94100faSBill Paul {
1179a94100faSBill Paul 	u_char			eaddr[ETHER_ADDR_LEN];
1180be099007SPyun YongHyeon 	u_int16_t		as[ETHER_ADDR_LEN / 2];
1181a94100faSBill Paul 	struct rl_softc		*sc;
1182a94100faSBill Paul 	struct ifnet		*ifp;
1183a94100faSBill Paul 	struct rl_hwrev		*hw_rev;
1184a94100faSBill Paul 	int			hwrev;
1185ace7ed5dSPyun YongHyeon 	u_int16_t		devid, re_did = 0;
11868e5d93dbSMarius Strobl 	int			error = 0, i, phy, rid;
11874a58fd45SPyun YongHyeon 	int			msic, msixc, reg;
118803ca7ae8SPyun YongHyeon 	uint8_t			cfg;
1189a94100faSBill Paul 
1190a94100faSBill Paul 	sc = device_get_softc(dev);
1191ed510fb0SBill Paul 	sc->rl_dev = dev;
1192a94100faSBill Paul 
1193a94100faSBill Paul 	mtx_init(&sc->rl_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK,
119497b9d4baSJohn-Mark Gurney 	    MTX_DEF);
1195d1754a9bSJohn Baldwin 	callout_init_mtx(&sc->rl_stat_callout, &sc->rl_mtx, 0);
1196d1754a9bSJohn Baldwin 
1197a94100faSBill Paul 	/*
1198a94100faSBill Paul 	 * Map control/status registers.
1199a94100faSBill Paul 	 */
1200a94100faSBill Paul 	pci_enable_busmaster(dev);
1201a94100faSBill Paul 
1202ace7ed5dSPyun YongHyeon 	devid = pci_get_device(dev);
12032c21710bSPyun YongHyeon 	/*
12042c21710bSPyun YongHyeon 	 * Prefer memory space register mapping over IO space.
12052c21710bSPyun YongHyeon 	 * Because RTL8169SC does not seem to work when memory mapping
12062c21710bSPyun YongHyeon 	 * is used always activate io mapping.
12072c21710bSPyun YongHyeon 	 */
12082c21710bSPyun YongHyeon 	if (devid == RT_DEVICEID_8169SC)
12092c21710bSPyun YongHyeon 		prefer_iomap = 1;
12102c21710bSPyun YongHyeon 	if (prefer_iomap == 0) {
1211ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(1);
1212ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_MEMORY;
1213ace7ed5dSPyun YongHyeon 		/* RTL8168/8101E seems to use different BARs. */
1214ace7ed5dSPyun YongHyeon 		if (devid == RT_DEVICEID_8168 || devid == RT_DEVICEID_8101E)
1215ace7ed5dSPyun YongHyeon 			sc->rl_res_id = PCIR_BAR(2);
12162c21710bSPyun YongHyeon 	} else {
12172c21710bSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
12182c21710bSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
12192c21710bSPyun YongHyeon 	}
1220ace7ed5dSPyun YongHyeon 	sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1221ace7ed5dSPyun YongHyeon 	    &sc->rl_res_id, RF_ACTIVE);
12222c21710bSPyun YongHyeon 	if (sc->rl_res == NULL && prefer_iomap == 0) {
1223ace7ed5dSPyun YongHyeon 		sc->rl_res_id = PCIR_BAR(0);
1224ace7ed5dSPyun YongHyeon 		sc->rl_res_type = SYS_RES_IOPORT;
1225ace7ed5dSPyun YongHyeon 		sc->rl_res = bus_alloc_resource_any(dev, sc->rl_res_type,
1226ace7ed5dSPyun YongHyeon 		    &sc->rl_res_id, RF_ACTIVE);
12272c21710bSPyun YongHyeon 	}
1228ace7ed5dSPyun YongHyeon 	if (sc->rl_res == NULL) {
1229d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't map ports/memory\n");
1230a94100faSBill Paul 		error = ENXIO;
1231a94100faSBill Paul 		goto fail;
1232a94100faSBill Paul 	}
1233a94100faSBill Paul 
1234a94100faSBill Paul 	sc->rl_btag = rman_get_bustag(sc->rl_res);
1235a94100faSBill Paul 	sc->rl_bhandle = rman_get_bushandle(sc->rl_res);
1236a94100faSBill Paul 
12375774c5ffSPyun YongHyeon 	msic = pci_msi_count(dev);
12384a58fd45SPyun YongHyeon 	msixc = pci_msix_count(dev);
12394a58fd45SPyun YongHyeon 	if (pci_find_extcap(dev, PCIY_EXPRESS, &reg) == 0)
12404a58fd45SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PCIE;
12414a58fd45SPyun YongHyeon 	if (bootverbose) {
12425774c5ffSPyun YongHyeon 		device_printf(dev, "MSI count : %d\n", msic);
12434a58fd45SPyun YongHyeon 		device_printf(dev, "MSI-X count : %d\n", msixc);
12445774c5ffSPyun YongHyeon 	}
12454a58fd45SPyun YongHyeon 	if (msix_disable > 0)
12464a58fd45SPyun YongHyeon 		msixc = 0;
12474a58fd45SPyun YongHyeon 	if (msi_disable > 0)
12484a58fd45SPyun YongHyeon 		msic = 0;
12494a58fd45SPyun YongHyeon 	/* Prefer MSI-X to MSI. */
12504a58fd45SPyun YongHyeon 	if (msixc > 0) {
12514a58fd45SPyun YongHyeon 		msixc = 1;
12524a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
12534a58fd45SPyun YongHyeon 		sc->rl_res_pba = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
12544a58fd45SPyun YongHyeon 		    &rid, RF_ACTIVE);
12554a58fd45SPyun YongHyeon 		if (sc->rl_res_pba == NULL) {
12564a58fd45SPyun YongHyeon 			device_printf(sc->rl_dev,
12574a58fd45SPyun YongHyeon 			    "could not allocate MSI-X PBA resource\n");
12584a58fd45SPyun YongHyeon 		}
12594a58fd45SPyun YongHyeon 		if (sc->rl_res_pba != NULL &&
12604a58fd45SPyun YongHyeon 		    pci_alloc_msix(dev, &msixc) == 0) {
12614a58fd45SPyun YongHyeon 			if (msixc == 1) {
12624a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI-X message\n",
12634a58fd45SPyun YongHyeon 				    msixc);
12644a58fd45SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSIX;
12654a58fd45SPyun YongHyeon 			} else
12664a58fd45SPyun YongHyeon 				pci_release_msi(dev);
12674a58fd45SPyun YongHyeon 		}
12684a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSIX) == 0) {
12694a58fd45SPyun YongHyeon 			if (sc->rl_res_pba != NULL)
12704a58fd45SPyun YongHyeon 				bus_release_resource(dev, SYS_RES_MEMORY, rid,
12714a58fd45SPyun YongHyeon 				    sc->rl_res_pba);
12724a58fd45SPyun YongHyeon 			sc->rl_res_pba = NULL;
12734a58fd45SPyun YongHyeon 			msixc = 0;
12744a58fd45SPyun YongHyeon 		}
12754a58fd45SPyun YongHyeon 	}
12764a58fd45SPyun YongHyeon 	/* Prefer MSI to INTx. */
12774a58fd45SPyun YongHyeon 	if (msixc == 0 && msic > 0) {
1278f1bb696aSPyun YongHyeon 		msic = 1;
12795774c5ffSPyun YongHyeon 		if (pci_alloc_msi(dev, &msic) == 0) {
12805774c5ffSPyun YongHyeon 			if (msic == RL_MSI_MESSAGES) {
12814a58fd45SPyun YongHyeon 				device_printf(dev, "Using %d MSI message\n",
12825774c5ffSPyun YongHyeon 				    msic);
1283351a76f9SPyun YongHyeon 				sc->rl_flags |= RL_FLAG_MSI;
1284339a44fbSPyun YongHyeon 				/* Explicitly set MSI enable bit. */
1285339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1286339a44fbSPyun YongHyeon 				cfg = CSR_READ_1(sc, RL_CFG2);
1287339a44fbSPyun YongHyeon 				cfg |= RL_CFG2_MSI;
1288339a44fbSPyun YongHyeon 				CSR_WRITE_1(sc, RL_CFG2, cfg);
1289f98dd8cfSPyun YongHyeon 				CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
12905774c5ffSPyun YongHyeon 			} else
12915774c5ffSPyun YongHyeon 				pci_release_msi(dev);
12925774c5ffSPyun YongHyeon 		}
12934a58fd45SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_MSI) == 0)
12944a58fd45SPyun YongHyeon 			msic = 0;
12955774c5ffSPyun YongHyeon 	}
1296a94100faSBill Paul 
12975774c5ffSPyun YongHyeon 	/* Allocate interrupt */
12984a58fd45SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0) {
12995774c5ffSPyun YongHyeon 		rid = 0;
13005774c5ffSPyun YongHyeon 		sc->rl_irq[0] = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
13015774c5ffSPyun YongHyeon 		    RF_SHAREABLE | RF_ACTIVE);
13025774c5ffSPyun YongHyeon 		if (sc->rl_irq[0] == NULL) {
13035774c5ffSPyun YongHyeon 			device_printf(dev, "couldn't allocate IRQ resources\n");
1304a94100faSBill Paul 			error = ENXIO;
1305a94100faSBill Paul 			goto fail;
1306a94100faSBill Paul 		}
13075774c5ffSPyun YongHyeon 	} else {
13085774c5ffSPyun YongHyeon 		for (i = 0, rid = 1; i < RL_MSI_MESSAGES; i++, rid++) {
13095774c5ffSPyun YongHyeon 			sc->rl_irq[i] = bus_alloc_resource_any(dev,
13105774c5ffSPyun YongHyeon 			    SYS_RES_IRQ, &rid, RF_ACTIVE);
13115774c5ffSPyun YongHyeon 			if (sc->rl_irq[i] == NULL) {
13125774c5ffSPyun YongHyeon 				device_printf(dev,
13135774c5ffSPyun YongHyeon 				    "couldn't llocate IRQ resources for "
13145774c5ffSPyun YongHyeon 				    "message %d\n", rid);
13155774c5ffSPyun YongHyeon 				error = ENXIO;
13165774c5ffSPyun YongHyeon 				goto fail;
13175774c5ffSPyun YongHyeon 			}
13185774c5ffSPyun YongHyeon 		}
13195774c5ffSPyun YongHyeon 	}
1320a94100faSBill Paul 
13214d2bf239SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MSI) == 0) {
13224d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
13234d2bf239SPyun YongHyeon 		cfg = CSR_READ_1(sc, RL_CFG2);
13244d2bf239SPyun YongHyeon 		if ((cfg & RL_CFG2_MSI) != 0) {
13254d2bf239SPyun YongHyeon 			device_printf(dev, "turning off MSI enable bit.\n");
13264d2bf239SPyun YongHyeon 			cfg &= ~RL_CFG2_MSI;
13274d2bf239SPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG2, cfg);
13284d2bf239SPyun YongHyeon 		}
13294d2bf239SPyun YongHyeon 		CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
13304d2bf239SPyun YongHyeon 	}
13314d2bf239SPyun YongHyeon 
1332abc8ff44SBill Paul 	hw_rev = re_hwrevs;
1333a810fc83SPyun YongHyeon 	hwrev = CSR_READ_4(sc, RL_TXCFG);
1334566ca8caSJung-uk Kim 	switch (hwrev & 0x70000000) {
1335566ca8caSJung-uk Kim 	case 0x00000000:
1336566ca8caSJung-uk Kim 	case 0x10000000:
1337566ca8caSJung-uk Kim 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0xfc800000);
1338566ca8caSJung-uk Kim 		hwrev &= (RL_TXCFG_HWREV | 0x80000000);
1339566ca8caSJung-uk Kim 		break;
1340566ca8caSJung-uk Kim 	default:
1341a810fc83SPyun YongHyeon 		device_printf(dev, "Chip rev. 0x%08x\n", hwrev & 0x7c800000);
1342a810fc83SPyun YongHyeon 		hwrev &= RL_TXCFG_HWREV;
1343566ca8caSJung-uk Kim 		break;
1344566ca8caSJung-uk Kim 	}
1345566ca8caSJung-uk Kim 	device_printf(dev, "MAC rev. 0x%08x\n", hwrev & 0x00700000);
1346abc8ff44SBill Paul 	while (hw_rev->rl_desc != NULL) {
1347abc8ff44SBill Paul 		if (hw_rev->rl_rev == hwrev) {
1348abc8ff44SBill Paul 			sc->rl_type = hw_rev->rl_type;
134981eee0ebSPyun YongHyeon 			sc->rl_hwrev = hw_rev;
1350abc8ff44SBill Paul 			break;
1351abc8ff44SBill Paul 		}
1352abc8ff44SBill Paul 		hw_rev++;
1353abc8ff44SBill Paul 	}
1354d65abd66SPyun YongHyeon 	if (hw_rev->rl_desc == NULL) {
1355a810fc83SPyun YongHyeon 		device_printf(dev, "Unknown H/W revision: 0x%08x\n", hwrev);
1356d65abd66SPyun YongHyeon 		error = ENXIO;
1357d65abd66SPyun YongHyeon 		goto fail;
1358d65abd66SPyun YongHyeon 	}
1359abc8ff44SBill Paul 
1360351a76f9SPyun YongHyeon 	switch (hw_rev->rl_rev) {
1361351a76f9SPyun YongHyeon 	case RL_HWREV_8139CPLUS:
136281eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_FASTETHER | RL_FLAG_AUTOPAD;
1363351a76f9SPyun YongHyeon 		break;
1364351a76f9SPyun YongHyeon 	case RL_HWREV_8100E:
1365351a76f9SPyun YongHyeon 	case RL_HWREV_8101E:
136681eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_FASTETHER;
1367351a76f9SPyun YongHyeon 		break;
1368b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102E:
1369b1d62f0fSPyun YongHyeon 	case RL_HWREV_8102EL:
13703d22427cSTai-hwa Liang 	case RL_HWREV_8102EL_SPIN1:
137181eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
137281eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
137381eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD;
1374b1d62f0fSPyun YongHyeon 		break;
13758281a098SPyun YongHyeon 	case RL_HWREV_8103E:
137681eee0ebSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR | RL_FLAG_DESCV2 |
137781eee0ebSPyun YongHyeon 		    RL_FLAG_MACSTAT | RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP |
137881eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_MACSLEEP;
13798281a098SPyun YongHyeon 		break;
1380*54899a96SPyun YongHyeon 	case RL_HWREV_8105E:
1381*54899a96SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1382*54899a96SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
1383*54899a96SPyun YongHyeon 		    RL_FLAG_FASTETHER | RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD;
1384*54899a96SPyun YongHyeon 		break;
1385ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN1:
1386ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN2:
1387886ff602SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_WOLRXENB;
1388886ff602SPyun YongHyeon 		/* FALLTHROUGH */
1389ef278cb4SPyun YongHyeon 	case RL_HWREV_8168B_SPIN3:
1390aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_MACSTAT;
1391deb5c680SPyun YongHyeon 		break;
1392deb5c680SPyun YongHyeon 	case RL_HWREV_8168C_SPIN2:
139361f45a72SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_MACSLEEP;
139461f45a72SPyun YongHyeon 		/* FALLTHROUGH */
139561f45a72SPyun YongHyeon 	case RL_HWREV_8168C:
139661f45a72SPyun YongHyeon 		if ((hwrev & 0x00700000) == 0x00200000)
139761f45a72SPyun YongHyeon 			sc->rl_flags |= RL_FLAG_MACSLEEP;
139861f45a72SPyun YongHyeon 		/* FALLTHROUGH */
1399deb5c680SPyun YongHyeon 	case RL_HWREV_8168CP:
140059ef640dSPyun YongHyeon 	case RL_HWREV_8168D:
14015fa06abeSPyun YongHyeon 	case RL_HWREV_8168DP:
1402aaab4fbeSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1403f2e491c9SPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
140481eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1405351a76f9SPyun YongHyeon 		break;
1406d0c45156SPyun YongHyeon 	case RL_HWREV_8168E:
1407d0c45156SPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PHYWAKE_PM |
1408d0c45156SPyun YongHyeon 		    RL_FLAG_PAR | RL_FLAG_DESCV2 | RL_FLAG_MACSTAT |
140981eee0ebSPyun YongHyeon 		    RL_FLAG_CMDSTOP | RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1410d0c45156SPyun YongHyeon 		break;
1411f0431c5bSPyun YongHyeon 	case RL_HWREV_8168E_VL:
1412f0431c5bSPyun YongHyeon 		sc->rl_flags |= RL_FLAG_PHYWAKE | RL_FLAG_PAR |
1413f0431c5bSPyun YongHyeon 		    RL_FLAG_DESCV2 | RL_FLAG_MACSTAT | RL_FLAG_CMDSTOP |
141481eee0ebSPyun YongHyeon 		    RL_FLAG_AUTOPAD | RL_FLAG_JUMBOV2;
1415f0431c5bSPyun YongHyeon 		break;
1416566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SB:
1417566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SBL:
1418566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SC:
1419566ca8caSJung-uk Kim 	case RL_HWREV_8169_8110SCE:
1420566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_PHYWAKE;
1421566ca8caSJung-uk Kim 		/* FALLTHROUGH */
14220596d7e6SPyun YongHyeon 	case RL_HWREV_8169:
14230596d7e6SPyun YongHyeon 	case RL_HWREV_8169S:
1424566ca8caSJung-uk Kim 	case RL_HWREV_8110S:
1425566ca8caSJung-uk Kim 		sc->rl_flags |= RL_FLAG_MACRESET;
1426351a76f9SPyun YongHyeon 		break;
1427351a76f9SPyun YongHyeon 	default:
1428351a76f9SPyun YongHyeon 		break;
1429351a76f9SPyun YongHyeon 	}
1430351a76f9SPyun YongHyeon 
143193252626SPyun YongHyeon 	/* Reset the adapter. */
143293252626SPyun YongHyeon 	RL_LOCK(sc);
143393252626SPyun YongHyeon 	re_reset(sc);
143493252626SPyun YongHyeon 	RL_UNLOCK(sc);
143593252626SPyun YongHyeon 
1436deb5c680SPyun YongHyeon 	/* Enable PME. */
1437deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
1438deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG1);
1439deb5c680SPyun YongHyeon 	cfg |= RL_CFG1_PME;
1440deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, cfg);
1441deb5c680SPyun YongHyeon 	cfg = CSR_READ_1(sc, RL_CFG5);
1442deb5c680SPyun YongHyeon 	cfg &= RL_CFG5_PME_STS;
1443deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, cfg);
1444deb5c680SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
1445deb5c680SPyun YongHyeon 
1446deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PAR) != 0) {
1447deb5c680SPyun YongHyeon 		/*
1448deb5c680SPyun YongHyeon 		 * XXX Should have a better way to extract station
1449deb5c680SPyun YongHyeon 		 * address from EEPROM.
1450deb5c680SPyun YongHyeon 		 */
1451deb5c680SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN; i++)
1452deb5c680SPyun YongHyeon 			eaddr[i] = CSR_READ_1(sc, RL_IDR0 + i);
1453deb5c680SPyun YongHyeon 	} else {
1454141f92e7SPyun YongHyeon 		sc->rl_eewidth = RL_9356_ADDR_LEN;
1455ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)&re_did, 0, 1);
1456a94100faSBill Paul 		if (re_did != 0x8129)
1457141f92e7SPyun YongHyeon 			sc->rl_eewidth = RL_9346_ADDR_LEN;
1458a94100faSBill Paul 
1459a94100faSBill Paul 		/*
1460a94100faSBill Paul 		 * Get station address from the EEPROM.
1461a94100faSBill Paul 		 */
1462ed510fb0SBill Paul 		re_read_eeprom(sc, (caddr_t)as, RL_EE_EADDR, 3);
1463be099007SPyun YongHyeon 		for (i = 0; i < ETHER_ADDR_LEN / 2; i++)
1464be099007SPyun YongHyeon 			as[i] = le16toh(as[i]);
1465be099007SPyun YongHyeon 		bcopy(as, eaddr, sizeof(eaddr));
1466deb5c680SPyun YongHyeon 	}
1467ed510fb0SBill Paul 
1468ed510fb0SBill Paul 	if (sc->rl_type == RL_8169) {
1469d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1470ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_GFRAGLEN;
1471ed510fb0SBill Paul 		sc->rl_txstart = RL_GTXSTART;
1472d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8169_TX_DESC_CNT;
1473d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8169_RX_DESC_CNT;
1474ed510fb0SBill Paul 	} else {
1475d65abd66SPyun YongHyeon 		/* Set RX length mask and number of descriptors. */
1476ed510fb0SBill Paul 		sc->rl_rxlenmask = RL_RDESC_STAT_FRAGLEN;
1477ed510fb0SBill Paul 		sc->rl_txstart = RL_TXSTART;
1478d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc_cnt = RL_8139_TX_DESC_CNT;
1479d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc_cnt = RL_8139_RX_DESC_CNT;
1480abc8ff44SBill Paul 	}
14819bac70b8SBill Paul 
1482a94100faSBill Paul 	error = re_allocmem(dev, sc);
1483a94100faSBill Paul 	if (error)
1484a94100faSBill Paul 		goto fail;
14850534aae0SPyun YongHyeon 	re_add_sysctls(sc);
1486a94100faSBill Paul 
1487cd036ec1SBrooks Davis 	ifp = sc->rl_ifp = if_alloc(IFT_ETHER);
1488cd036ec1SBrooks Davis 	if (ifp == NULL) {
1489d1754a9bSJohn Baldwin 		device_printf(dev, "can not if_alloc()\n");
1490cd036ec1SBrooks Davis 		error = ENOSPC;
1491cd036ec1SBrooks Davis 		goto fail;
1492cd036ec1SBrooks Davis 	}
1493cd036ec1SBrooks Davis 
149461f45a72SPyun YongHyeon 	/* Take controller out of deep sleep mode. */
149561f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
149661f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
149761f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
149861f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
149961f45a72SPyun YongHyeon 		else
150061f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
150161f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
150261f45a72SPyun YongHyeon 	}
150361f45a72SPyun YongHyeon 
1504351a76f9SPyun YongHyeon 	/* Take PHY out of power down mode. */
1505d0c45156SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
1506d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) | 0x80);
1507351a76f9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_PHYWAKE) != 0) {
1508351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x1f, 0);
1509351a76f9SPyun YongHyeon 		re_gmii_writereg(dev, 1, 0x0e, 0);
1510351a76f9SPyun YongHyeon 	}
1511351a76f9SPyun YongHyeon 
15128e5d93dbSMarius Strobl #define	RE_PHYAD_INTERNAL	 0
15138e5d93dbSMarius Strobl 
15148e5d93dbSMarius Strobl 	/* Do MII setup. */
15158e5d93dbSMarius Strobl 	phy = RE_PHYAD_INTERNAL;
15168e5d93dbSMarius Strobl 	if (sc->rl_type == RL_8169)
15178e5d93dbSMarius Strobl 		phy = 1;
15188e5d93dbSMarius Strobl 	error = mii_attach(dev, &sc->rl_miibus, ifp, re_ifmedia_upd,
151964436f6eSPyun YongHyeon 	    re_ifmedia_sts, BMSR_DEFCAPMASK, phy, MII_OFFSET_ANY, MIIF_DOPAUSE);
15208e5d93dbSMarius Strobl 	if (error != 0) {
15218e5d93dbSMarius Strobl 		device_printf(dev, "attaching PHYs failed\n");
1522a94100faSBill Paul 		goto fail;
1523a94100faSBill Paul 	}
1524a94100faSBill Paul 
1525a94100faSBill Paul 	ifp->if_softc = sc;
15269bf40edeSBrooks Davis 	if_initname(ifp, device_get_name(dev), device_get_unit(dev));
1527a94100faSBill Paul 	ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1528a94100faSBill Paul 	ifp->if_ioctl = re_ioctl;
1529a94100faSBill Paul 	ifp->if_start = re_start;
1530d6d7d923SPyun YongHyeon 	ifp->if_hwassist = RE_CSUM_FEATURES | CSUM_TSO;
1531d6d7d923SPyun YongHyeon 	ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_TSO4;
1532498bd0d3SBill Paul 	ifp->if_capenable = ifp->if_capabilities;
1533a94100faSBill Paul 	ifp->if_init = re_init;
153452732175SMax Laier 	IFQ_SET_MAXLEN(&ifp->if_snd, RL_IFQ_MAXLEN);
153552732175SMax Laier 	ifp->if_snd.ifq_drv_maxlen = RL_IFQ_MAXLEN;
153652732175SMax Laier 	IFQ_SET_READY(&ifp->if_snd);
1537a94100faSBill Paul 
1538ed510fb0SBill Paul 	TASK_INIT(&sc->rl_inttask, 0, re_int_task, sc);
1539ed510fb0SBill Paul 
1540a94100faSBill Paul 	/*
1541a94100faSBill Paul 	 * Call MI attach routine.
1542a94100faSBill Paul 	 */
1543a94100faSBill Paul 	ether_ifattach(ifp, eaddr);
1544a94100faSBill Paul 
1545960fd5b3SPyun YongHyeon 	/* VLAN capability setup */
1546960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
1547960fd5b3SPyun YongHyeon 	if (ifp->if_capabilities & IFCAP_HWCSUM)
1548960fd5b3SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_VLAN_HWCSUM;
15497467bd53SPyun YongHyeon 	/* Enable WOL if PM is supported. */
15507467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &reg) == 0)
15517467bd53SPyun YongHyeon 		ifp->if_capabilities |= IFCAP_WOL;
1552960fd5b3SPyun YongHyeon 	ifp->if_capenable = ifp->if_capabilities;
1553a2a8420cSPyun YongHyeon 	/*
1554f9ad4da7SPyun YongHyeon 	 * Don't enable TSO by default.  It is known to generate
1555f9ad4da7SPyun YongHyeon 	 * corrupted TCP segments(bad TCP options) under certain
1556f9ad4da7SPyun YongHyeon 	 * circumtances.
1557a2a8420cSPyun YongHyeon 	 */
1558a2a8420cSPyun YongHyeon 	ifp->if_hwassist &= ~CSUM_TSO;
1559ecafbbb5SPyun YongHyeon 	ifp->if_capenable &= ~(IFCAP_TSO4 | IFCAP_VLAN_HWTSO);
1560960fd5b3SPyun YongHyeon #ifdef DEVICE_POLLING
1561960fd5b3SPyun YongHyeon 	ifp->if_capabilities |= IFCAP_POLLING;
1562960fd5b3SPyun YongHyeon #endif
1563960fd5b3SPyun YongHyeon 	/*
1564960fd5b3SPyun YongHyeon 	 * Tell the upper layer(s) we support long frames.
1565960fd5b3SPyun YongHyeon 	 * Must appear after the call to ether_ifattach() because
1566960fd5b3SPyun YongHyeon 	 * ether_ifattach() sets ifi_hdrlen to the default value.
1567960fd5b3SPyun YongHyeon 	 */
1568960fd5b3SPyun YongHyeon 	ifp->if_data.ifi_hdrlen = sizeof(struct ether_vlan_header);
1569960fd5b3SPyun YongHyeon 
1570ed510fb0SBill Paul #ifdef RE_DIAG
1571ed510fb0SBill Paul 	/*
1572ed510fb0SBill Paul 	 * Perform hardware diagnostic on the original RTL8169.
1573ed510fb0SBill Paul 	 * Some 32-bit cards were incorrectly wired and would
1574ed510fb0SBill Paul 	 * malfunction if plugged into a 64-bit slot.
1575ed510fb0SBill Paul 	 */
1576a94100faSBill Paul 
1577ed510fb0SBill Paul 	if (hwrev == RL_HWREV_8169) {
1578ed510fb0SBill Paul 		error = re_diag(sc);
1579a94100faSBill Paul 		if (error) {
1580ed510fb0SBill Paul 			device_printf(dev,
1581ed510fb0SBill Paul 		    	"attach aborted due to hardware diag failure\n");
1582a94100faSBill Paul 			ether_ifdetach(ifp);
1583a94100faSBill Paul 			goto fail;
1584a94100faSBill Paul 		}
1585ed510fb0SBill Paul 	}
1586ed510fb0SBill Paul #endif
1587a94100faSBill Paul 
1588502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
1589502be0f7SPyun YongHyeon 	intr_filter = 1;
1590502be0f7SPyun YongHyeon #endif
1591a94100faSBill Paul 	/* Hook interrupt last to avoid having to lock softc */
1592502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
1593502be0f7SPyun YongHyeon 	    intr_filter == 0) {
1594502be0f7SPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
1595502be0f7SPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, NULL, re_intr_msi, sc,
1596502be0f7SPyun YongHyeon 		    &sc->rl_intrhand[0]);
1597502be0f7SPyun YongHyeon 	} else {
15985774c5ffSPyun YongHyeon 		error = bus_setup_intr(dev, sc->rl_irq[0],
15995774c5ffSPyun YongHyeon 		    INTR_TYPE_NET | INTR_MPSAFE, re_intr, NULL, sc,
16005774c5ffSPyun YongHyeon 		    &sc->rl_intrhand[0]);
16015774c5ffSPyun YongHyeon 	}
1602a94100faSBill Paul 	if (error) {
1603d1754a9bSJohn Baldwin 		device_printf(dev, "couldn't set up irq\n");
1604a94100faSBill Paul 		ether_ifdetach(ifp);
1605a94100faSBill Paul 	}
1606a94100faSBill Paul 
1607a94100faSBill Paul fail:
1608ed510fb0SBill Paul 
1609a94100faSBill Paul 	if (error)
1610a94100faSBill Paul 		re_detach(dev);
1611a94100faSBill Paul 
1612a94100faSBill Paul 	return (error);
1613a94100faSBill Paul }
1614a94100faSBill Paul 
1615a94100faSBill Paul /*
1616a94100faSBill Paul  * Shutdown hardware and free up resources. This can be called any
1617a94100faSBill Paul  * time after the mutex has been initialized. It is called in both
1618a94100faSBill Paul  * the error case in attach and the normal detach case so it needs
1619a94100faSBill Paul  * to be careful about only freeing resources that have actually been
1620a94100faSBill Paul  * allocated.
1621a94100faSBill Paul  */
1622a94100faSBill Paul static int
16237b5ffebfSPyun YongHyeon re_detach(device_t dev)
1624a94100faSBill Paul {
1625a94100faSBill Paul 	struct rl_softc		*sc;
1626a94100faSBill Paul 	struct ifnet		*ifp;
16275774c5ffSPyun YongHyeon 	int			i, rid;
1628a94100faSBill Paul 
1629a94100faSBill Paul 	sc = device_get_softc(dev);
1630fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
1631aedd16d9SJohn-Mark Gurney 	KASSERT(mtx_initialized(&sc->rl_mtx), ("re mutex not initialized"));
163297b9d4baSJohn-Mark Gurney 
163381cf2eb6SPyun YongHyeon 	/* These should only be active if attach succeeded */
163481cf2eb6SPyun YongHyeon 	if (device_is_attached(dev)) {
163540929967SGleb Smirnoff #ifdef DEVICE_POLLING
163640929967SGleb Smirnoff 		if (ifp->if_capenable & IFCAP_POLLING)
163740929967SGleb Smirnoff 			ether_poll_deregister(ifp);
163840929967SGleb Smirnoff #endif
163997b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
164097b9d4baSJohn-Mark Gurney #if 0
164197b9d4baSJohn-Mark Gurney 		sc->suspended = 1;
164297b9d4baSJohn-Mark Gurney #endif
1643a94100faSBill Paul 		re_stop(sc);
1644525e6a87SRuslan Ermilov 		RL_UNLOCK(sc);
1645d1754a9bSJohn Baldwin 		callout_drain(&sc->rl_stat_callout);
16463d4c1b57SJohn Baldwin 		taskqueue_drain(taskqueue_fast, &sc->rl_inttask);
1647a94100faSBill Paul 		/*
1648a94100faSBill Paul 		 * Force off the IFF_UP flag here, in case someone
1649a94100faSBill Paul 		 * still had a BPF descriptor attached to this
165097b9d4baSJohn-Mark Gurney 		 * interface. If they do, ether_ifdetach() will cause
1651a94100faSBill Paul 		 * the BPF code to try and clear the promisc mode
1652a94100faSBill Paul 		 * flag, which will bubble down to re_ioctl(),
1653a94100faSBill Paul 		 * which will try to call re_init() again. This will
1654a94100faSBill Paul 		 * turn the NIC back on and restart the MII ticker,
1655a94100faSBill Paul 		 * which will panic the system when the kernel tries
1656a94100faSBill Paul 		 * to invoke the re_tick() function that isn't there
1657a94100faSBill Paul 		 * anymore.
1658a94100faSBill Paul 		 */
1659a94100faSBill Paul 		ifp->if_flags &= ~IFF_UP;
1660525e6a87SRuslan Ermilov 		ether_ifdetach(ifp);
1661a94100faSBill Paul 	}
1662a94100faSBill Paul 	if (sc->rl_miibus)
1663a94100faSBill Paul 		device_delete_child(dev, sc->rl_miibus);
1664a94100faSBill Paul 	bus_generic_detach(dev);
1665a94100faSBill Paul 
166697b9d4baSJohn-Mark Gurney 	/*
166797b9d4baSJohn-Mark Gurney 	 * The rest is resource deallocation, so we should already be
166897b9d4baSJohn-Mark Gurney 	 * stopped here.
166997b9d4baSJohn-Mark Gurney 	 */
167097b9d4baSJohn-Mark Gurney 
1671502be0f7SPyun YongHyeon 	if (sc->rl_intrhand[0] != NULL) {
1672502be0f7SPyun YongHyeon 		bus_teardown_intr(dev, sc->rl_irq[0], sc->rl_intrhand[0]);
1673502be0f7SPyun YongHyeon 		sc->rl_intrhand[0] = NULL;
16745774c5ffSPyun YongHyeon 	}
1675ad4f426eSWarner Losh 	if (ifp != NULL)
1676ad4f426eSWarner Losh 		if_free(ifp);
1677502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
1678502be0f7SPyun YongHyeon 		rid = 0;
1679502be0f7SPyun YongHyeon 	else
1680502be0f7SPyun YongHyeon 		rid = 1;
16815774c5ffSPyun YongHyeon 	if (sc->rl_irq[0] != NULL) {
1682502be0f7SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_IRQ, rid, sc->rl_irq[0]);
16835774c5ffSPyun YongHyeon 		sc->rl_irq[0] = NULL;
16845774c5ffSPyun YongHyeon 	}
1685502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0)
16865774c5ffSPyun YongHyeon 		pci_release_msi(dev);
16874a58fd45SPyun YongHyeon 	if (sc->rl_res_pba) {
16884a58fd45SPyun YongHyeon 		rid = PCIR_BAR(4);
16894a58fd45SPyun YongHyeon 		bus_release_resource(dev, SYS_RES_MEMORY, rid, sc->rl_res_pba);
16904a58fd45SPyun YongHyeon 	}
1691a94100faSBill Paul 	if (sc->rl_res)
1692ace7ed5dSPyun YongHyeon 		bus_release_resource(dev, sc->rl_res_type, sc->rl_res_id,
1693ace7ed5dSPyun YongHyeon 		    sc->rl_res);
1694a94100faSBill Paul 
1695a94100faSBill Paul 	/* Unload and free the RX DMA ring memory and map */
1696a94100faSBill Paul 
1697a94100faSBill Paul 	if (sc->rl_ldata.rl_rx_list_tag) {
16980534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map)
1699a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_rx_list_tag,
1700a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
17010534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_list_map && sc->rl_ldata.rl_rx_list)
1702a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_rx_list_tag,
1703a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list,
1704a94100faSBill Paul 			    sc->rl_ldata.rl_rx_list_map);
1705a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_list_tag);
1706a94100faSBill Paul 	}
1707a94100faSBill Paul 
1708a94100faSBill Paul 	/* Unload and free the TX DMA ring memory and map */
1709a94100faSBill Paul 
1710a94100faSBill Paul 	if (sc->rl_ldata.rl_tx_list_tag) {
17110534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map)
1712a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_tx_list_tag,
1713a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
17140534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_list_map && sc->rl_ldata.rl_tx_list)
1715a94100faSBill Paul 			bus_dmamem_free(sc->rl_ldata.rl_tx_list_tag,
1716a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list,
1717a94100faSBill Paul 			    sc->rl_ldata.rl_tx_list_map);
1718a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_list_tag);
1719a94100faSBill Paul 	}
1720a94100faSBill Paul 
1721a94100faSBill Paul 	/* Destroy all the RX and TX buffer maps */
1722a94100faSBill Paul 
1723d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_mtag) {
17249e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
17259e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_tx_desc[i].tx_dmamap)
1726d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_tx_mtag,
1727d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_tx_desc[i].tx_dmamap);
17289e18005dSPyun YongHyeon 		}
1729d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_tx_mtag);
1730d65abd66SPyun YongHyeon 	}
1731d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_rx_mtag) {
17329e18005dSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
17339e18005dSPyun YongHyeon 			if (sc->rl_ldata.rl_rx_desc[i].rx_dmamap)
1734d65abd66SPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1735d65abd66SPyun YongHyeon 				    sc->rl_ldata.rl_rx_desc[i].rx_dmamap);
17369e18005dSPyun YongHyeon 		}
1737d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_rx_sparemap)
1738d65abd66SPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_rx_mtag,
1739d65abd66SPyun YongHyeon 			    sc->rl_ldata.rl_rx_sparemap);
1740d65abd66SPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_rx_mtag);
1741a94100faSBill Paul 	}
174281eee0ebSPyun YongHyeon 	if (sc->rl_ldata.rl_jrx_mtag) {
174381eee0ebSPyun YongHyeon 		for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
174481eee0ebSPyun YongHyeon 			if (sc->rl_ldata.rl_jrx_desc[i].rx_dmamap)
174581eee0ebSPyun YongHyeon 				bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
174681eee0ebSPyun YongHyeon 				    sc->rl_ldata.rl_jrx_desc[i].rx_dmamap);
174781eee0ebSPyun YongHyeon 		}
174881eee0ebSPyun YongHyeon 		if (sc->rl_ldata.rl_jrx_sparemap)
174981eee0ebSPyun YongHyeon 			bus_dmamap_destroy(sc->rl_ldata.rl_jrx_mtag,
175081eee0ebSPyun YongHyeon 			    sc->rl_ldata.rl_jrx_sparemap);
175181eee0ebSPyun YongHyeon 		bus_dma_tag_destroy(sc->rl_ldata.rl_jrx_mtag);
175281eee0ebSPyun YongHyeon 	}
1753a94100faSBill Paul 	/* Unload and free the stats buffer and map */
1754a94100faSBill Paul 
1755a94100faSBill Paul 	if (sc->rl_ldata.rl_stag) {
17560534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap)
1757a94100faSBill Paul 			bus_dmamap_unload(sc->rl_ldata.rl_stag,
1758a94100faSBill Paul 			    sc->rl_ldata.rl_smap);
17590534aae0SPyun YongHyeon 		if (sc->rl_ldata.rl_smap && sc->rl_ldata.rl_stats)
17600534aae0SPyun YongHyeon 			bus_dmamem_free(sc->rl_ldata.rl_stag,
17610534aae0SPyun YongHyeon 			    sc->rl_ldata.rl_stats, sc->rl_ldata.rl_smap);
1762a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_ldata.rl_stag);
1763a94100faSBill Paul 	}
1764a94100faSBill Paul 
1765a94100faSBill Paul 	if (sc->rl_parent_tag)
1766a94100faSBill Paul 		bus_dma_tag_destroy(sc->rl_parent_tag);
1767a94100faSBill Paul 
1768a94100faSBill Paul 	mtx_destroy(&sc->rl_mtx);
1769a94100faSBill Paul 
1770a94100faSBill Paul 	return (0);
1771a94100faSBill Paul }
1772a94100faSBill Paul 
1773d65abd66SPyun YongHyeon static __inline void
17747b5ffebfSPyun YongHyeon re_discard_rxbuf(struct rl_softc *sc, int idx)
1775a94100faSBill Paul {
1776d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1777d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1778d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1779a94100faSBill Paul 
178081eee0ebSPyun YongHyeon 	if (sc->rl_ifp->if_mtu > RL_MTU &&
178181eee0ebSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
178281eee0ebSPyun YongHyeon 		rxd = &sc->rl_ldata.rl_jrx_desc[idx];
178381eee0ebSPyun YongHyeon 	else
1784d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[idx];
1785d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1786d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1787d65abd66SPyun YongHyeon 	cmdstat = rxd->rx_size;
1788d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1789d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1790d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1791d65abd66SPyun YongHyeon }
1792d65abd66SPyun YongHyeon 
1793d65abd66SPyun YongHyeon static int
17947b5ffebfSPyun YongHyeon re_newbuf(struct rl_softc *sc, int idx)
1795d65abd66SPyun YongHyeon {
1796d65abd66SPyun YongHyeon 	struct mbuf		*m;
1797d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
1798d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[1];
1799d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
1800d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1801d65abd66SPyun YongHyeon 	uint32_t		cmdstat;
1802d65abd66SPyun YongHyeon 	int			error, nsegs;
1803d65abd66SPyun YongHyeon 
1804d65abd66SPyun YongHyeon 	m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR);
1805d65abd66SPyun YongHyeon 	if (m == NULL)
1806a94100faSBill Paul 		return (ENOBUFS);
1807a94100faSBill Paul 
1808a94100faSBill Paul 	m->m_len = m->m_pkthdr.len = MCLBYTES;
180922a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
181022a11c96SJohn-Mark Gurney 	/*
181122a11c96SJohn-Mark Gurney 	 * This is part of an evil trick to deal with non-x86 platforms.
181222a11c96SJohn-Mark Gurney 	 * The RealTek chip requires RX buffers to be aligned on 64-bit
181322a11c96SJohn-Mark Gurney 	 * boundaries, but that will hose non-x86 machines. To get around
181422a11c96SJohn-Mark Gurney 	 * this, we leave some empty space at the start of each buffer
181522a11c96SJohn-Mark Gurney 	 * and for non-x86 hosts, we copy the buffer back six bytes
181622a11c96SJohn-Mark Gurney 	 * to achieve word alignment. This is slightly more efficient
181722a11c96SJohn-Mark Gurney 	 * than allocating a new buffer, copying the contents, and
181822a11c96SJohn-Mark Gurney 	 * discarding the old buffer.
181922a11c96SJohn-Mark Gurney 	 */
182022a11c96SJohn-Mark Gurney 	m_adj(m, RE_ETHER_ALIGN);
182122a11c96SJohn-Mark Gurney #endif
1822d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_rx_mtag,
1823d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
1824d65abd66SPyun YongHyeon 	if (error != 0) {
1825d65abd66SPyun YongHyeon 		m_freem(m);
1826d65abd66SPyun YongHyeon 		return (ENOBUFS);
1827d65abd66SPyun YongHyeon 	}
1828d65abd66SPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
1829a94100faSBill Paul 
1830d65abd66SPyun YongHyeon 	rxd = &sc->rl_ldata.rl_rx_desc[idx];
1831d65abd66SPyun YongHyeon 	if (rxd->rx_m != NULL) {
1832d65abd66SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1833d65abd66SPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
1834d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap);
1835a94100faSBill Paul 	}
1836a94100faSBill Paul 
1837d65abd66SPyun YongHyeon 	rxd->rx_m = m;
1838d65abd66SPyun YongHyeon 	map = rxd->rx_dmamap;
1839d65abd66SPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_rx_sparemap;
1840d65abd66SPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
1841d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_rx_sparemap = map;
1842d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_mtag, rxd->rx_dmamap,
1843a94100faSBill Paul 	    BUS_DMASYNC_PREREAD);
1844a94100faSBill Paul 
1845d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
1846d65abd66SPyun YongHyeon 	desc->rl_vlanctl = 0;
1847d65abd66SPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
1848d65abd66SPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
1849d65abd66SPyun YongHyeon 	cmdstat = segs[0].ds_len;
1850d65abd66SPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
1851d65abd66SPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
1852d65abd66SPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
1853d65abd66SPyun YongHyeon 
1854a94100faSBill Paul 	return (0);
1855a94100faSBill Paul }
1856a94100faSBill Paul 
185781eee0ebSPyun YongHyeon static int
185881eee0ebSPyun YongHyeon re_jumbo_newbuf(struct rl_softc *sc, int idx)
185981eee0ebSPyun YongHyeon {
186081eee0ebSPyun YongHyeon 	struct mbuf		*m;
186181eee0ebSPyun YongHyeon 	struct rl_rxdesc	*rxd;
186281eee0ebSPyun YongHyeon 	bus_dma_segment_t	segs[1];
186381eee0ebSPyun YongHyeon 	bus_dmamap_t		map;
186481eee0ebSPyun YongHyeon 	struct rl_desc		*desc;
186581eee0ebSPyun YongHyeon 	uint32_t		cmdstat;
186681eee0ebSPyun YongHyeon 	int			error, nsegs;
186781eee0ebSPyun YongHyeon 
186881eee0ebSPyun YongHyeon 	m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUM9BYTES);
186981eee0ebSPyun YongHyeon 	if (m == NULL)
187081eee0ebSPyun YongHyeon 		return (ENOBUFS);
187181eee0ebSPyun YongHyeon 	m->m_len = m->m_pkthdr.len = MJUM9BYTES;
187281eee0ebSPyun YongHyeon #ifdef RE_FIXUP_RX
187381eee0ebSPyun YongHyeon 	m_adj(m, RE_ETHER_ALIGN);
187481eee0ebSPyun YongHyeon #endif
187581eee0ebSPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_jrx_mtag,
187681eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_jrx_sparemap, m, segs, &nsegs, BUS_DMA_NOWAIT);
187781eee0ebSPyun YongHyeon 	if (error != 0) {
187881eee0ebSPyun YongHyeon 		m_freem(m);
187981eee0ebSPyun YongHyeon 		return (ENOBUFS);
188081eee0ebSPyun YongHyeon 	}
188181eee0ebSPyun YongHyeon 	KASSERT(nsegs == 1, ("%s: %d segment returned!", __func__, nsegs));
188281eee0ebSPyun YongHyeon 
188381eee0ebSPyun YongHyeon 	rxd = &sc->rl_ldata.rl_jrx_desc[idx];
188481eee0ebSPyun YongHyeon 	if (rxd->rx_m != NULL) {
188581eee0ebSPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
188681eee0ebSPyun YongHyeon 		    BUS_DMASYNC_POSTREAD);
188781eee0ebSPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap);
188881eee0ebSPyun YongHyeon 	}
188981eee0ebSPyun YongHyeon 
189081eee0ebSPyun YongHyeon 	rxd->rx_m = m;
189181eee0ebSPyun YongHyeon 	map = rxd->rx_dmamap;
189281eee0ebSPyun YongHyeon 	rxd->rx_dmamap = sc->rl_ldata.rl_jrx_sparemap;
189381eee0ebSPyun YongHyeon 	rxd->rx_size = segs[0].ds_len;
189481eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_jrx_sparemap = map;
189581eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_jrx_mtag, rxd->rx_dmamap,
189681eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREREAD);
189781eee0ebSPyun YongHyeon 
189881eee0ebSPyun YongHyeon 	desc = &sc->rl_ldata.rl_rx_list[idx];
189981eee0ebSPyun YongHyeon 	desc->rl_vlanctl = 0;
190081eee0ebSPyun YongHyeon 	desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[0].ds_addr));
190181eee0ebSPyun YongHyeon 	desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[0].ds_addr));
190281eee0ebSPyun YongHyeon 	cmdstat = segs[0].ds_len;
190381eee0ebSPyun YongHyeon 	if (idx == sc->rl_ldata.rl_rx_desc_cnt - 1)
190481eee0ebSPyun YongHyeon 		cmdstat |= RL_RDESC_CMD_EOR;
190581eee0ebSPyun YongHyeon 	desc->rl_cmdstat = htole32(cmdstat | RL_RDESC_CMD_OWN);
190681eee0ebSPyun YongHyeon 
190781eee0ebSPyun YongHyeon 	return (0);
190881eee0ebSPyun YongHyeon }
190981eee0ebSPyun YongHyeon 
191022a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
191122a11c96SJohn-Mark Gurney static __inline void
19127b5ffebfSPyun YongHyeon re_fixup_rx(struct mbuf *m)
191322a11c96SJohn-Mark Gurney {
191422a11c96SJohn-Mark Gurney 	int                     i;
191522a11c96SJohn-Mark Gurney 	uint16_t                *src, *dst;
191622a11c96SJohn-Mark Gurney 
191722a11c96SJohn-Mark Gurney 	src = mtod(m, uint16_t *);
191822a11c96SJohn-Mark Gurney 	dst = src - (RE_ETHER_ALIGN - ETHER_ALIGN) / sizeof *src;
191922a11c96SJohn-Mark Gurney 
192022a11c96SJohn-Mark Gurney 	for (i = 0; i < (m->m_len / sizeof(uint16_t) + 1); i++)
192122a11c96SJohn-Mark Gurney 		*dst++ = *src++;
192222a11c96SJohn-Mark Gurney 
192322a11c96SJohn-Mark Gurney 	m->m_data -= RE_ETHER_ALIGN - ETHER_ALIGN;
192422a11c96SJohn-Mark Gurney }
192522a11c96SJohn-Mark Gurney #endif
192622a11c96SJohn-Mark Gurney 
1927a94100faSBill Paul static int
19287b5ffebfSPyun YongHyeon re_tx_list_init(struct rl_softc *sc)
1929a94100faSBill Paul {
1930d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
1931d65abd66SPyun YongHyeon 	int			i;
193297b9d4baSJohn-Mark Gurney 
193397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
193497b9d4baSJohn-Mark Gurney 
1935d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_tx_list,
1936d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_desc_cnt * sizeof(struct rl_desc));
1937d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++)
1938d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_desc[i].tx_m = NULL;
1939d65abd66SPyun YongHyeon 	/* Set EOR. */
1940d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[sc->rl_ldata.rl_tx_desc_cnt - 1];
1941d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOR);
1942a94100faSBill Paul 
1943a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
1944d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_list_map,
1945d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1946d65abd66SPyun YongHyeon 
1947a94100faSBill Paul 	sc->rl_ldata.rl_tx_prodidx = 0;
1948a94100faSBill Paul 	sc->rl_ldata.rl_tx_considx = 0;
1949d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_free = sc->rl_ldata.rl_tx_desc_cnt;
1950a94100faSBill Paul 
1951a94100faSBill Paul 	return (0);
1952a94100faSBill Paul }
1953a94100faSBill Paul 
1954a94100faSBill Paul static int
19557b5ffebfSPyun YongHyeon re_rx_list_init(struct rl_softc *sc)
1956a94100faSBill Paul {
1957d65abd66SPyun YongHyeon 	int			error, i;
1958a94100faSBill Paul 
1959d65abd66SPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
1960d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
1961d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
1962d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_rx_desc[i].rx_m = NULL;
1963d65abd66SPyun YongHyeon 		if ((error = re_newbuf(sc, i)) != 0)
1964d65abd66SPyun YongHyeon 			return (error);
1965a94100faSBill Paul 	}
1966a94100faSBill Paul 
1967a94100faSBill Paul 	/* Flush the RX descriptors */
1968a94100faSBill Paul 
1969a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
1970a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
1971a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
1972a94100faSBill Paul 
1973a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = 0;
1974a94100faSBill Paul 	sc->rl_head = sc->rl_tail = NULL;
1975502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
1976a94100faSBill Paul 
1977a94100faSBill Paul 	return (0);
1978a94100faSBill Paul }
1979a94100faSBill Paul 
198081eee0ebSPyun YongHyeon static int
198181eee0ebSPyun YongHyeon re_jrx_list_init(struct rl_softc *sc)
198281eee0ebSPyun YongHyeon {
198381eee0ebSPyun YongHyeon 	int			error, i;
198481eee0ebSPyun YongHyeon 
198581eee0ebSPyun YongHyeon 	bzero(sc->rl_ldata.rl_rx_list,
198681eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_desc_cnt * sizeof(struct rl_desc));
198781eee0ebSPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
198881eee0ebSPyun YongHyeon 		sc->rl_ldata.rl_jrx_desc[i].rx_m = NULL;
198981eee0ebSPyun YongHyeon 		if ((error = re_jumbo_newbuf(sc, i)) != 0)
199081eee0ebSPyun YongHyeon 			return (error);
199181eee0ebSPyun YongHyeon 	}
199281eee0ebSPyun YongHyeon 
199381eee0ebSPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
199481eee0ebSPyun YongHyeon 	    sc->rl_ldata.rl_rx_list_map,
199581eee0ebSPyun YongHyeon 	    BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
199681eee0ebSPyun YongHyeon 
199781eee0ebSPyun YongHyeon 	sc->rl_ldata.rl_rx_prodidx = 0;
199881eee0ebSPyun YongHyeon 	sc->rl_head = sc->rl_tail = NULL;
1999502be0f7SPyun YongHyeon 	sc->rl_int_rx_act = 0;
200081eee0ebSPyun YongHyeon 
200181eee0ebSPyun YongHyeon 	return (0);
200281eee0ebSPyun YongHyeon }
200381eee0ebSPyun YongHyeon 
2004a94100faSBill Paul /*
2005a94100faSBill Paul  * RX handler for C+ and 8169. For the gigE chips, we support
2006a94100faSBill Paul  * the reception of jumbo frames that have been fragmented
2007a94100faSBill Paul  * across multiple 2K mbuf cluster buffers.
2008a94100faSBill Paul  */
2009ed510fb0SBill Paul static int
20101abcdbd1SAttilio Rao re_rxeof(struct rl_softc *sc, int *rx_npktsp)
2011a94100faSBill Paul {
2012a94100faSBill Paul 	struct mbuf		*m;
2013a94100faSBill Paul 	struct ifnet		*ifp;
201481eee0ebSPyun YongHyeon 	int			i, rxerr, total_len;
2015a94100faSBill Paul 	struct rl_desc		*cur_rx;
2016a94100faSBill Paul 	u_int32_t		rxstat, rxvlan;
201781eee0ebSPyun YongHyeon 	int			jumbo, maxpkt = 16, rx_npkts = 0;
2018a94100faSBill Paul 
20195120abbfSSam Leffler 	RL_LOCK_ASSERT(sc);
20205120abbfSSam Leffler 
2021fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
202281eee0ebSPyun YongHyeon 	if (ifp->if_mtu > RL_MTU && (sc->rl_flags & RL_FLAG_JUMBOV2) != 0)
202381eee0ebSPyun YongHyeon 		jumbo = 1;
202481eee0ebSPyun YongHyeon 	else
202581eee0ebSPyun YongHyeon 		jumbo = 0;
2026a94100faSBill Paul 
2027a94100faSBill Paul 	/* Invalidate the descriptor memory */
2028a94100faSBill Paul 
2029a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2030a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2031d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2032a94100faSBill Paul 
2033d65abd66SPyun YongHyeon 	for (i = sc->rl_ldata.rl_rx_prodidx; maxpkt > 0;
2034d65abd66SPyun YongHyeon 	    i = RL_RX_DESC_NXT(sc, i)) {
20355b6d1d9dSPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
20365b6d1d9dSPyun YongHyeon 			break;
2037a94100faSBill Paul 		cur_rx = &sc->rl_ldata.rl_rx_list[i];
2038a94100faSBill Paul 		rxstat = le32toh(cur_rx->rl_cmdstat);
2039d65abd66SPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_OWN) != 0)
2040d65abd66SPyun YongHyeon 			break;
2041d65abd66SPyun YongHyeon 		total_len = rxstat & sc->rl_rxlenmask;
2042a94100faSBill Paul 		rxvlan = le32toh(cur_rx->rl_vlanctl);
204381eee0ebSPyun YongHyeon 		if (jumbo != 0)
204481eee0ebSPyun YongHyeon 			m = sc->rl_ldata.rl_jrx_desc[i].rx_m;
204581eee0ebSPyun YongHyeon 		else
2046d65abd66SPyun YongHyeon 			m = sc->rl_ldata.rl_rx_desc[i].rx_m;
2047a94100faSBill Paul 
204881eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
204981eee0ebSPyun YongHyeon 		    (rxstat & (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) !=
205081eee0ebSPyun YongHyeon 		    (RL_RDESC_STAT_SOF | RL_RDESC_STAT_EOF)) {
205181eee0ebSPyun YongHyeon 			/*
205281eee0ebSPyun YongHyeon 			 * RTL8168C or later controllers do not
205381eee0ebSPyun YongHyeon 			 * support multi-fragment packet.
205481eee0ebSPyun YongHyeon 			 */
205581eee0ebSPyun YongHyeon 			re_discard_rxbuf(sc, i);
205681eee0ebSPyun YongHyeon 			continue;
205781eee0ebSPyun YongHyeon 		} else if ((rxstat & RL_RDESC_STAT_EOF) == 0) {
2058d65abd66SPyun YongHyeon 			if (re_newbuf(sc, i) != 0) {
2059d65abd66SPyun YongHyeon 				/*
2060d65abd66SPyun YongHyeon 				 * If this is part of a multi-fragment packet,
2061d65abd66SPyun YongHyeon 				 * discard all the pieces.
2062d65abd66SPyun YongHyeon 				 */
2063d65abd66SPyun YongHyeon 				if (sc->rl_head != NULL) {
2064d65abd66SPyun YongHyeon 					m_freem(sc->rl_head);
2065d65abd66SPyun YongHyeon 					sc->rl_head = sc->rl_tail = NULL;
2066d65abd66SPyun YongHyeon 				}
2067d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2068d65abd66SPyun YongHyeon 				continue;
2069d65abd66SPyun YongHyeon 			}
207022a11c96SJohn-Mark Gurney 			m->m_len = RE_RX_DESC_BUFLEN;
2071a94100faSBill Paul 			if (sc->rl_head == NULL)
2072a94100faSBill Paul 				sc->rl_head = sc->rl_tail = m;
2073a94100faSBill Paul 			else {
2074a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2075a94100faSBill Paul 				sc->rl_tail->m_next = m;
2076a94100faSBill Paul 				sc->rl_tail = m;
2077a94100faSBill Paul 			}
2078a94100faSBill Paul 			continue;
2079a94100faSBill Paul 		}
2080a94100faSBill Paul 
2081a94100faSBill Paul 		/*
2082a94100faSBill Paul 		 * NOTE: for the 8139C+, the frame length field
2083a94100faSBill Paul 		 * is always 12 bits in size, but for the gigE chips,
2084a94100faSBill Paul 		 * it is 13 bits (since the max RX frame length is 16K).
2085a94100faSBill Paul 		 * Unfortunately, all 32 bits in the status word
2086a94100faSBill Paul 		 * were already used, so to make room for the extra
2087a94100faSBill Paul 		 * length bit, RealTek took out the 'frame alignment
2088a94100faSBill Paul 		 * error' bit and shifted the other status bits
2089a94100faSBill Paul 		 * over one slot. The OWN, EOR, FS and LS bits are
2090a94100faSBill Paul 		 * still in the same places. We have already extracted
2091a94100faSBill Paul 		 * the frame length and checked the OWN bit, so rather
2092a94100faSBill Paul 		 * than using an alternate bit mapping, we shift the
2093a94100faSBill Paul 		 * status bits one space to the right so we can evaluate
2094a94100faSBill Paul 		 * them using the 8169 status as though it was in the
2095a94100faSBill Paul 		 * same format as that of the 8139C+.
2096a94100faSBill Paul 		 */
2097a94100faSBill Paul 		if (sc->rl_type == RL_8169)
2098a94100faSBill Paul 			rxstat >>= 1;
2099a94100faSBill Paul 
210022a11c96SJohn-Mark Gurney 		/*
210122a11c96SJohn-Mark Gurney 		 * if total_len > 2^13-1, both _RXERRSUM and _GIANT will be
210222a11c96SJohn-Mark Gurney 		 * set, but if CRC is clear, it will still be a valid frame.
210322a11c96SJohn-Mark Gurney 		 */
210481eee0ebSPyun YongHyeon 		if ((rxstat & RL_RDESC_STAT_RXERRSUM) != 0) {
210581eee0ebSPyun YongHyeon 			rxerr = 1;
210681eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) == 0 &&
210781eee0ebSPyun YongHyeon 			    total_len > 8191 &&
210881eee0ebSPyun YongHyeon 			    (rxstat & RL_RDESC_STAT_ERRS) == RL_RDESC_STAT_GIANT)
210981eee0ebSPyun YongHyeon 				rxerr = 0;
211081eee0ebSPyun YongHyeon 			if (rxerr != 0) {
2111a94100faSBill Paul 				ifp->if_ierrors++;
2112a94100faSBill Paul 				/*
2113a94100faSBill Paul 				 * If this is part of a multi-fragment packet,
2114a94100faSBill Paul 				 * discard all the pieces.
2115a94100faSBill Paul 				 */
2116a94100faSBill Paul 				if (sc->rl_head != NULL) {
2117a94100faSBill Paul 					m_freem(sc->rl_head);
2118a94100faSBill Paul 					sc->rl_head = sc->rl_tail = NULL;
2119a94100faSBill Paul 				}
2120d65abd66SPyun YongHyeon 				re_discard_rxbuf(sc, i);
2121a94100faSBill Paul 				continue;
2122a94100faSBill Paul 			}
212381eee0ebSPyun YongHyeon 		}
2124a94100faSBill Paul 
2125a94100faSBill Paul 		/*
2126a94100faSBill Paul 		 * If allocating a replacement mbuf fails,
2127a94100faSBill Paul 		 * reload the current one.
2128a94100faSBill Paul 		 */
212981eee0ebSPyun YongHyeon 		if (jumbo != 0)
213081eee0ebSPyun YongHyeon 			rxerr = re_jumbo_newbuf(sc, i);
213181eee0ebSPyun YongHyeon 		else
213281eee0ebSPyun YongHyeon 			rxerr = re_newbuf(sc, i);
213381eee0ebSPyun YongHyeon 		if (rxerr != 0) {
2134d65abd66SPyun YongHyeon 			ifp->if_iqdrops++;
2135a94100faSBill Paul 			if (sc->rl_head != NULL) {
2136a94100faSBill Paul 				m_freem(sc->rl_head);
2137a94100faSBill Paul 				sc->rl_head = sc->rl_tail = NULL;
2138a94100faSBill Paul 			}
2139d65abd66SPyun YongHyeon 			re_discard_rxbuf(sc, i);
2140a94100faSBill Paul 			continue;
2141a94100faSBill Paul 		}
2142a94100faSBill Paul 
2143a94100faSBill Paul 		if (sc->rl_head != NULL) {
214481eee0ebSPyun YongHyeon 			if (jumbo != 0)
214581eee0ebSPyun YongHyeon 				m->m_len = total_len;
214681eee0ebSPyun YongHyeon 			else {
214722a11c96SJohn-Mark Gurney 				m->m_len = total_len % RE_RX_DESC_BUFLEN;
214822a11c96SJohn-Mark Gurney 				if (m->m_len == 0)
214922a11c96SJohn-Mark Gurney 					m->m_len = RE_RX_DESC_BUFLEN;
215081eee0ebSPyun YongHyeon 			}
2151a94100faSBill Paul 			/*
2152a94100faSBill Paul 			 * Special case: if there's 4 bytes or less
2153a94100faSBill Paul 			 * in this buffer, the mbuf can be discarded:
2154a94100faSBill Paul 			 * the last 4 bytes is the CRC, which we don't
2155a94100faSBill Paul 			 * care about anyway.
2156a94100faSBill Paul 			 */
2157a94100faSBill Paul 			if (m->m_len <= ETHER_CRC_LEN) {
2158a94100faSBill Paul 				sc->rl_tail->m_len -=
2159a94100faSBill Paul 				    (ETHER_CRC_LEN - m->m_len);
2160a94100faSBill Paul 				m_freem(m);
2161a94100faSBill Paul 			} else {
2162a94100faSBill Paul 				m->m_len -= ETHER_CRC_LEN;
2163a94100faSBill Paul 				m->m_flags &= ~M_PKTHDR;
2164a94100faSBill Paul 				sc->rl_tail->m_next = m;
2165a94100faSBill Paul 			}
2166a94100faSBill Paul 			m = sc->rl_head;
2167a94100faSBill Paul 			sc->rl_head = sc->rl_tail = NULL;
2168a94100faSBill Paul 			m->m_pkthdr.len = total_len - ETHER_CRC_LEN;
2169a94100faSBill Paul 		} else
2170a94100faSBill Paul 			m->m_pkthdr.len = m->m_len =
2171a94100faSBill Paul 			    (total_len - ETHER_CRC_LEN);
2172a94100faSBill Paul 
217322a11c96SJohn-Mark Gurney #ifdef RE_FIXUP_RX
217422a11c96SJohn-Mark Gurney 		re_fixup_rx(m);
217522a11c96SJohn-Mark Gurney #endif
2176a94100faSBill Paul 		ifp->if_ipackets++;
2177a94100faSBill Paul 		m->m_pkthdr.rcvif = ifp;
2178a94100faSBill Paul 
2179a94100faSBill Paul 		/* Do RX checksumming if enabled */
2180a94100faSBill Paul 
2181a94100faSBill Paul 		if (ifp->if_capenable & IFCAP_RXCSUM) {
2182deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2183a94100faSBill Paul 				/* Check IP header checksum */
2184a94100faSBill Paul 				if (rxstat & RL_RDESC_STAT_PROTOID)
2185deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2186deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2187a94100faSBill Paul 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD))
2188deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2189deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2190a94100faSBill Paul 
2191a94100faSBill Paul 				/* Check TCP/UDP checksum */
2192a94100faSBill Paul 				if ((RL_TCPPKT(rxstat) &&
2193a94100faSBill Paul 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2194a94100faSBill Paul 				    (RL_UDPPKT(rxstat) &&
2195a94100faSBill Paul 				     !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2196a94100faSBill Paul 					m->m_pkthdr.csum_flags |=
2197a94100faSBill Paul 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2198a94100faSBill Paul 					m->m_pkthdr.csum_data = 0xffff;
2199a94100faSBill Paul 				}
2200deb5c680SPyun YongHyeon 			} else {
2201deb5c680SPyun YongHyeon 				/*
2202deb5c680SPyun YongHyeon 				 * RTL8168C/RTL816CP/RTL8111C/RTL8111CP
2203deb5c680SPyun YongHyeon 				 */
2204deb5c680SPyun YongHyeon 				if ((rxstat & RL_RDESC_STAT_PROTOID) &&
2205deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2206deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2207deb5c680SPyun YongHyeon 					    CSUM_IP_CHECKED;
2208deb5c680SPyun YongHyeon 				if (!(rxstat & RL_RDESC_STAT_IPSUMBAD) &&
2209deb5c680SPyun YongHyeon 				    (rxvlan & RL_RDESC_IPV4))
2210deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2211deb5c680SPyun YongHyeon 					    CSUM_IP_VALID;
2212deb5c680SPyun YongHyeon 				if (((rxstat & RL_RDESC_STAT_TCP) &&
2213deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_TCPSUMBAD)) ||
2214deb5c680SPyun YongHyeon 				    ((rxstat & RL_RDESC_STAT_UDP) &&
2215deb5c680SPyun YongHyeon 				    !(rxstat & RL_RDESC_STAT_UDPSUMBAD))) {
2216deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_flags |=
2217deb5c680SPyun YongHyeon 						CSUM_DATA_VALID|CSUM_PSEUDO_HDR;
2218deb5c680SPyun YongHyeon 					m->m_pkthdr.csum_data = 0xffff;
2219deb5c680SPyun YongHyeon 				}
2220deb5c680SPyun YongHyeon 			}
2221a94100faSBill Paul 		}
2222ed510fb0SBill Paul 		maxpkt--;
2223d147662cSGleb Smirnoff 		if (rxvlan & RL_RDESC_VLANCTL_TAG) {
222478ba57b9SAndre Oppermann 			m->m_pkthdr.ether_vtag =
2225bddff934SPyun YongHyeon 			    bswap16((rxvlan & RL_RDESC_VLANCTL_DATA));
222678ba57b9SAndre Oppermann 			m->m_flags |= M_VLANTAG;
2227d147662cSGleb Smirnoff 		}
22285120abbfSSam Leffler 		RL_UNLOCK(sc);
2229a94100faSBill Paul 		(*ifp->if_input)(ifp, m);
22305120abbfSSam Leffler 		RL_LOCK(sc);
22311abcdbd1SAttilio Rao 		rx_npkts++;
2232a94100faSBill Paul 	}
2233a94100faSBill Paul 
2234a94100faSBill Paul 	/* Flush the RX DMA ring */
2235a94100faSBill Paul 
2236a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_rx_list_tag,
2237a94100faSBill Paul 	    sc->rl_ldata.rl_rx_list_map,
2238a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2239a94100faSBill Paul 
2240a94100faSBill Paul 	sc->rl_ldata.rl_rx_prodidx = i;
2241ed510fb0SBill Paul 
22421abcdbd1SAttilio Rao 	if (rx_npktsp != NULL)
22431abcdbd1SAttilio Rao 		*rx_npktsp = rx_npkts;
2244ed510fb0SBill Paul 	if (maxpkt)
2245ed510fb0SBill Paul 		return (EAGAIN);
2246ed510fb0SBill Paul 
2247ed510fb0SBill Paul 	return (0);
2248a94100faSBill Paul }
2249a94100faSBill Paul 
2250a94100faSBill Paul static void
22517b5ffebfSPyun YongHyeon re_txeof(struct rl_softc *sc)
2252a94100faSBill Paul {
2253a94100faSBill Paul 	struct ifnet		*ifp;
2254d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
2255a94100faSBill Paul 	u_int32_t		txstat;
2256d65abd66SPyun YongHyeon 	int			cons;
2257d65abd66SPyun YongHyeon 
2258d65abd66SPyun YongHyeon 	cons = sc->rl_ldata.rl_tx_considx;
2259d65abd66SPyun YongHyeon 	if (cons == sc->rl_ldata.rl_tx_prodidx)
2260d65abd66SPyun YongHyeon 		return;
2261a94100faSBill Paul 
2262fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
2263a94100faSBill Paul 	/* Invalidate the TX descriptor list */
2264a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2265a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2266d65abd66SPyun YongHyeon 	    BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2267a94100faSBill Paul 
2268d65abd66SPyun YongHyeon 	for (; cons != sc->rl_ldata.rl_tx_prodidx;
2269d65abd66SPyun YongHyeon 	    cons = RL_TX_DESC_NXT(sc, cons)) {
2270d65abd66SPyun YongHyeon 		txstat = le32toh(sc->rl_ldata.rl_tx_list[cons].rl_cmdstat);
2271d65abd66SPyun YongHyeon 		if (txstat & RL_TDESC_STAT_OWN)
2272a94100faSBill Paul 			break;
2273a94100faSBill Paul 		/*
2274a94100faSBill Paul 		 * We only stash mbufs in the last descriptor
2275a94100faSBill Paul 		 * in a fragment chain, which also happens to
2276a94100faSBill Paul 		 * be the only place where the TX status bits
2277a94100faSBill Paul 		 * are valid.
2278a94100faSBill Paul 		 */
2279a94100faSBill Paul 		if (txstat & RL_TDESC_CMD_EOF) {
2280d65abd66SPyun YongHyeon 			txd = &sc->rl_ldata.rl_tx_desc[cons];
2281d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
2282d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
2283d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
2284d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
2285d65abd66SPyun YongHyeon 			KASSERT(txd->tx_m != NULL,
2286d65abd66SPyun YongHyeon 			    ("%s: freeing NULL mbufs!", __func__));
2287d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
2288d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
2289a94100faSBill Paul 			if (txstat & (RL_TDESC_STAT_EXCESSCOL|
2290a94100faSBill Paul 			    RL_TDESC_STAT_COLCNT))
2291a94100faSBill Paul 				ifp->if_collisions++;
2292a94100faSBill Paul 			if (txstat & RL_TDESC_STAT_TXERRSUM)
2293a94100faSBill Paul 				ifp->if_oerrors++;
2294a94100faSBill Paul 			else
2295a94100faSBill Paul 				ifp->if_opackets++;
2296a94100faSBill Paul 		}
2297a94100faSBill Paul 		sc->rl_ldata.rl_tx_free++;
2298d65abd66SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
2299a94100faSBill Paul 	}
2300d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_considx = cons;
2301a94100faSBill Paul 
2302a94100faSBill Paul 	/* No changes made to the TX ring, so no flush needed */
2303a94100faSBill Paul 
2304d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt) {
2305ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2306a94100faSBill Paul 		/*
2307b4b95879SMarius Strobl 		 * If not all descriptors have been reaped yet, reload
2308b4b95879SMarius Strobl 		 * the timer so that we will eventually get another
2309a94100faSBill Paul 		 * interrupt that will cause us to re-enter this routine.
2310a94100faSBill Paul 		 * This is done in case the transmitter has gone idle.
2311a94100faSBill Paul 		 */
2312a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2313ed510fb0SBill Paul #endif
2314b4b95879SMarius Strobl 	} else
2315b4b95879SMarius Strobl 		sc->rl_watchdog_timer = 0;
2316a94100faSBill Paul }
2317a94100faSBill Paul 
2318a94100faSBill Paul static void
23197b5ffebfSPyun YongHyeon re_tick(void *xsc)
2320a94100faSBill Paul {
2321a94100faSBill Paul 	struct rl_softc		*sc;
2322d1754a9bSJohn Baldwin 	struct mii_data		*mii;
2323a94100faSBill Paul 
2324a94100faSBill Paul 	sc = xsc;
232597b9d4baSJohn-Mark Gurney 
232697b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
232797b9d4baSJohn-Mark Gurney 
23281d545c7aSMarius Strobl 	mii = device_get_softc(sc->rl_miibus);
2329a94100faSBill Paul 	mii_tick(mii);
23300fe200d9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_LINK) == 0)
23310fe200d9SPyun YongHyeon 		re_miibus_statchg(sc->rl_dev);
2332c2d2e19cSPyun YongHyeon 	/*
2333c2d2e19cSPyun YongHyeon 	 * Reclaim transmitted frames here. Technically it is not
2334c2d2e19cSPyun YongHyeon 	 * necessary to do here but it ensures periodic reclamation
2335c2d2e19cSPyun YongHyeon 	 * regardless of Tx completion interrupt which seems to be
2336c2d2e19cSPyun YongHyeon 	 * lost on PCIe based controllers under certain situations.
2337c2d2e19cSPyun YongHyeon 	 */
2338c2d2e19cSPyun YongHyeon 	re_txeof(sc);
2339130b6dfbSPyun YongHyeon 	re_watchdog(sc);
2340d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
2341a94100faSBill Paul }
2342a94100faSBill Paul 
2343a94100faSBill Paul #ifdef DEVICE_POLLING
23441abcdbd1SAttilio Rao static int
2345a94100faSBill Paul re_poll(struct ifnet *ifp, enum poll_cmd cmd, int count)
2346a94100faSBill Paul {
2347a94100faSBill Paul 	struct rl_softc *sc = ifp->if_softc;
23481abcdbd1SAttilio Rao 	int rx_npkts = 0;
2349a94100faSBill Paul 
2350a94100faSBill Paul 	RL_LOCK(sc);
235140929967SGleb Smirnoff 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
23521abcdbd1SAttilio Rao 		rx_npkts = re_poll_locked(ifp, cmd, count);
235397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
23541abcdbd1SAttilio Rao 	return (rx_npkts);
235597b9d4baSJohn-Mark Gurney }
235697b9d4baSJohn-Mark Gurney 
23571abcdbd1SAttilio Rao static int
235897b9d4baSJohn-Mark Gurney re_poll_locked(struct ifnet *ifp, enum poll_cmd cmd, int count)
235997b9d4baSJohn-Mark Gurney {
236097b9d4baSJohn-Mark Gurney 	struct rl_softc *sc = ifp->if_softc;
23611abcdbd1SAttilio Rao 	int rx_npkts;
236297b9d4baSJohn-Mark Gurney 
236397b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
236497b9d4baSJohn-Mark Gurney 
2365a94100faSBill Paul 	sc->rxcycles = count;
23661abcdbd1SAttilio Rao 	re_rxeof(sc, &rx_npkts);
2367a94100faSBill Paul 	re_txeof(sc);
2368a94100faSBill Paul 
236937652939SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2370d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2371a94100faSBill Paul 
2372a94100faSBill Paul 	if (cmd == POLL_AND_CHECK_STATUS) { /* also check status register */
2373a94100faSBill Paul 		u_int16_t       status;
2374a94100faSBill Paul 
2375a94100faSBill Paul 		status = CSR_READ_2(sc, RL_ISR);
2376a94100faSBill Paul 		if (status == 0xffff)
23771abcdbd1SAttilio Rao 			return (rx_npkts);
2378a94100faSBill Paul 		if (status)
2379a94100faSBill Paul 			CSR_WRITE_2(sc, RL_ISR, status);
2380818951afSPyun YongHyeon 		if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2381818951afSPyun YongHyeon 		    (sc->rl_flags & RL_FLAG_PCIE))
2382818951afSPyun YongHyeon 			CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2383a94100faSBill Paul 
2384a94100faSBill Paul 		/*
2385a94100faSBill Paul 		 * XXX check behaviour on receiver stalls.
2386a94100faSBill Paul 		 */
2387a94100faSBill Paul 
23888476c243SPyun YongHyeon 		if (status & RL_ISR_SYSTEM_ERR) {
23898476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
239097b9d4baSJohn-Mark Gurney 			re_init_locked(sc);
2391a94100faSBill Paul 		}
23928476c243SPyun YongHyeon 	}
23931abcdbd1SAttilio Rao 	return (rx_npkts);
2394a94100faSBill Paul }
2395a94100faSBill Paul #endif /* DEVICE_POLLING */
2396a94100faSBill Paul 
2397ef544f63SPaolo Pisati static int
23987b5ffebfSPyun YongHyeon re_intr(void *arg)
2399a94100faSBill Paul {
2400a94100faSBill Paul 	struct rl_softc		*sc;
2401ed510fb0SBill Paul 	uint16_t		status;
2402a94100faSBill Paul 
2403a94100faSBill Paul 	sc = arg;
2404ed510fb0SBill Paul 
2405ed510fb0SBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2406498bd0d3SBill Paul 	if (status == 0xFFFF || (status & RL_INTRS_CPLUS) == 0)
2407ef544f63SPaolo Pisati                 return (FILTER_STRAY);
2408ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0);
2409ed510fb0SBill Paul 
2410ed510fb0SBill Paul 	taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2411ed510fb0SBill Paul 
2412ef544f63SPaolo Pisati 	return (FILTER_HANDLED);
2413ed510fb0SBill Paul }
2414ed510fb0SBill Paul 
2415ed510fb0SBill Paul static void
24167b5ffebfSPyun YongHyeon re_int_task(void *arg, int npending)
2417ed510fb0SBill Paul {
2418ed510fb0SBill Paul 	struct rl_softc		*sc;
2419ed510fb0SBill Paul 	struct ifnet		*ifp;
2420ed510fb0SBill Paul 	u_int16_t		status;
2421ed510fb0SBill Paul 	int			rval = 0;
2422ed510fb0SBill Paul 
2423ed510fb0SBill Paul 	sc = arg;
2424ed510fb0SBill Paul 	ifp = sc->rl_ifp;
2425a94100faSBill Paul 
2426a94100faSBill Paul 	RL_LOCK(sc);
242797b9d4baSJohn-Mark Gurney 
2428a94100faSBill Paul 	status = CSR_READ_2(sc, RL_ISR);
2429a94100faSBill Paul         CSR_WRITE_2(sc, RL_ISR, status);
2430a94100faSBill Paul 
2431d65abd66SPyun YongHyeon 	if (sc->suspended ||
2432d65abd66SPyun YongHyeon 	    (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2433ed510fb0SBill Paul 		RL_UNLOCK(sc);
2434ed510fb0SBill Paul 		return;
2435ed510fb0SBill Paul 	}
2436a94100faSBill Paul 
2437ed510fb0SBill Paul #ifdef DEVICE_POLLING
2438ed510fb0SBill Paul 	if  (ifp->if_capenable & IFCAP_POLLING) {
2439ed510fb0SBill Paul 		RL_UNLOCK(sc);
2440ed510fb0SBill Paul 		return;
2441ed510fb0SBill Paul 	}
2442ed510fb0SBill Paul #endif
2443a94100faSBill Paul 
2444ed510fb0SBill Paul 	if (status & (RL_ISR_RX_OK|RL_ISR_RX_ERR|RL_ISR_FIFO_OFLOW))
24451abcdbd1SAttilio Rao 		rval = re_rxeof(sc, NULL);
2446ed510fb0SBill Paul 
2447818951afSPyun YongHyeon 	/*
2448818951afSPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2449818951afSPyun YongHyeon 	 * while an existing transmission is in progress. If
2450818951afSPyun YongHyeon 	 * the transmitter goes idle but there are still
2451818951afSPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2452818951afSPyun YongHyeon 	 * channel here to flush them out. This only seems to
2453818951afSPyun YongHyeon 	 * be required with the PCIe devices.
2454818951afSPyun YongHyeon 	 */
2455818951afSPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2456818951afSPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2457818951afSPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
24583d85c23dSPyun YongHyeon 	if (status & (
2459ed510fb0SBill Paul #ifdef RE_TX_MODERATION
24603d85c23dSPyun YongHyeon 	    RL_ISR_TIMEOUT_EXPIRED|
2461ed510fb0SBill Paul #else
24623d85c23dSPyun YongHyeon 	    RL_ISR_TX_OK|
2463ed510fb0SBill Paul #endif
2464ed510fb0SBill Paul 	    RL_ISR_TX_ERR|RL_ISR_TX_DESC_UNAVAIL))
2465a94100faSBill Paul 		re_txeof(sc);
2466a94100faSBill Paul 
24678476c243SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
24688476c243SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
246997b9d4baSJohn-Mark Gurney 		re_init_locked(sc);
24708476c243SPyun YongHyeon 	}
2471a94100faSBill Paul 
247252732175SMax Laier 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2473d180a66fSPyun YongHyeon 		re_start_locked(ifp);
2474a94100faSBill Paul 
2475a94100faSBill Paul 	RL_UNLOCK(sc);
2476ed510fb0SBill Paul 
2477ed510fb0SBill Paul         if ((CSR_READ_2(sc, RL_ISR) & RL_INTRS_CPLUS) || rval) {
2478ed510fb0SBill Paul 		taskqueue_enqueue_fast(taskqueue_fast, &sc->rl_inttask);
2479ed510fb0SBill Paul 		return;
2480ed510fb0SBill Paul 	}
2481ed510fb0SBill Paul 
2482ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
2483a94100faSBill Paul }
2484a94100faSBill Paul 
2485502be0f7SPyun YongHyeon static void
2486502be0f7SPyun YongHyeon re_intr_msi(void *xsc)
2487502be0f7SPyun YongHyeon {
2488502be0f7SPyun YongHyeon 	struct rl_softc		*sc;
2489502be0f7SPyun YongHyeon 	struct ifnet		*ifp;
2490502be0f7SPyun YongHyeon 	uint16_t		intrs, status;
2491502be0f7SPyun YongHyeon 
2492502be0f7SPyun YongHyeon 	sc = xsc;
2493502be0f7SPyun YongHyeon 	RL_LOCK(sc);
2494502be0f7SPyun YongHyeon 
2495502be0f7SPyun YongHyeon 	ifp = sc->rl_ifp;
2496502be0f7SPyun YongHyeon #ifdef DEVICE_POLLING
2497502be0f7SPyun YongHyeon 	if (ifp->if_capenable & IFCAP_POLLING) {
2498502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2499502be0f7SPyun YongHyeon 		return;
2500502be0f7SPyun YongHyeon 	}
2501502be0f7SPyun YongHyeon #endif
2502502be0f7SPyun YongHyeon 	/* Disable interrupts. */
2503502be0f7SPyun YongHyeon 	CSR_WRITE_2(sc, RL_IMR, 0);
2504502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2505502be0f7SPyun YongHyeon 		RL_UNLOCK(sc);
2506502be0f7SPyun YongHyeon 		return;
2507502be0f7SPyun YongHyeon 	}
2508502be0f7SPyun YongHyeon 
2509502be0f7SPyun YongHyeon 	intrs = RL_INTRS_CPLUS;
2510502be0f7SPyun YongHyeon 	status = CSR_READ_2(sc, RL_ISR);
2511502be0f7SPyun YongHyeon         CSR_WRITE_2(sc, RL_ISR, status);
2512502be0f7SPyun YongHyeon 	if (sc->rl_int_rx_act > 0) {
2513502be0f7SPyun YongHyeon 		intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2514502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2515502be0f7SPyun YongHyeon 		status &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR | RL_ISR_FIFO_OFLOW |
2516502be0f7SPyun YongHyeon 		    RL_ISR_RX_OVERRUN);
2517502be0f7SPyun YongHyeon 	}
2518502be0f7SPyun YongHyeon 
2519502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TIMEOUT_EXPIRED | RL_ISR_RX_OK | RL_ISR_RX_ERR |
2520502be0f7SPyun YongHyeon 	    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) {
2521502be0f7SPyun YongHyeon 		re_rxeof(sc, NULL);
2522502be0f7SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2523502be0f7SPyun YongHyeon 			if (sc->rl_int_rx_mod != 0 &&
2524502be0f7SPyun YongHyeon 			    (status & (RL_ISR_RX_OK | RL_ISR_RX_ERR |
2525502be0f7SPyun YongHyeon 			    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN)) != 0) {
2526502be0f7SPyun YongHyeon 				/* Rearm one-shot timer. */
2527502be0f7SPyun YongHyeon 				CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2528502be0f7SPyun YongHyeon 				intrs &= ~(RL_ISR_RX_OK | RL_ISR_RX_ERR |
2529502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN);
2530502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 1;
2531502be0f7SPyun YongHyeon 			} else {
2532502be0f7SPyun YongHyeon 				intrs |= RL_ISR_RX_OK | RL_ISR_RX_ERR |
2533502be0f7SPyun YongHyeon 				    RL_ISR_FIFO_OFLOW | RL_ISR_RX_OVERRUN;
2534502be0f7SPyun YongHyeon 				sc->rl_int_rx_act = 0;
2535502be0f7SPyun YongHyeon 			}
2536502be0f7SPyun YongHyeon 		}
2537502be0f7SPyun YongHyeon 	}
2538502be0f7SPyun YongHyeon 
2539502be0f7SPyun YongHyeon 	/*
2540502be0f7SPyun YongHyeon 	 * Some chips will ignore a second TX request issued
2541502be0f7SPyun YongHyeon 	 * while an existing transmission is in progress. If
2542502be0f7SPyun YongHyeon 	 * the transmitter goes idle but there are still
2543502be0f7SPyun YongHyeon 	 * packets waiting to be sent, we need to restart the
2544502be0f7SPyun YongHyeon 	 * channel here to flush them out. This only seems to
2545502be0f7SPyun YongHyeon 	 * be required with the PCIe devices.
2546502be0f7SPyun YongHyeon 	 */
2547502be0f7SPyun YongHyeon 	if ((status & (RL_ISR_TX_OK | RL_ISR_TX_DESC_UNAVAIL)) &&
2548502be0f7SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PCIE))
2549502be0f7SPyun YongHyeon 		CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2550502be0f7SPyun YongHyeon 	if (status & (RL_ISR_TX_OK | RL_ISR_TX_ERR | RL_ISR_TX_DESC_UNAVAIL))
2551502be0f7SPyun YongHyeon 		re_txeof(sc);
2552502be0f7SPyun YongHyeon 
2553502be0f7SPyun YongHyeon 	if (status & RL_ISR_SYSTEM_ERR) {
2554502be0f7SPyun YongHyeon 		ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2555502be0f7SPyun YongHyeon 		re_init_locked(sc);
2556502be0f7SPyun YongHyeon 	}
2557502be0f7SPyun YongHyeon 
2558502be0f7SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
2559502be0f7SPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
2560502be0f7SPyun YongHyeon 			re_start_locked(ifp);
2561502be0f7SPyun YongHyeon 		CSR_WRITE_2(sc, RL_IMR, intrs);
2562502be0f7SPyun YongHyeon 	}
2563502be0f7SPyun YongHyeon 	RL_UNLOCK(sc);
2564502be0f7SPyun YongHyeon }
2565502be0f7SPyun YongHyeon 
2566d65abd66SPyun YongHyeon static int
25677b5ffebfSPyun YongHyeon re_encap(struct rl_softc *sc, struct mbuf **m_head)
2568d65abd66SPyun YongHyeon {
2569d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd, *txd_last;
2570d65abd66SPyun YongHyeon 	bus_dma_segment_t	segs[RL_NTXSEGS];
2571d65abd66SPyun YongHyeon 	bus_dmamap_t		map;
2572d65abd66SPyun YongHyeon 	struct mbuf		*m_new;
2573d65abd66SPyun YongHyeon 	struct rl_desc		*desc;
2574d65abd66SPyun YongHyeon 	int			nsegs, prod;
2575d65abd66SPyun YongHyeon 	int			i, error, ei, si;
2576d65abd66SPyun YongHyeon 	int			padlen;
2577ccf34c81SPyun YongHyeon 	uint32_t		cmdstat, csum_flags, vlanctl;
2578a94100faSBill Paul 
2579d65abd66SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
2580738489d1SPyun YongHyeon 	M_ASSERTPKTHDR((*m_head));
25810fc4974fSBill Paul 
25820fc4974fSBill Paul 	/*
25830fc4974fSBill Paul 	 * With some of the RealTek chips, using the checksum offload
25840fc4974fSBill Paul 	 * support in conjunction with the autopadding feature results
25850fc4974fSBill Paul 	 * in the transmission of corrupt frames. For example, if we
25860fc4974fSBill Paul 	 * need to send a really small IP fragment that's less than 60
25870fc4974fSBill Paul 	 * bytes in size, and IP header checksumming is enabled, the
25880fc4974fSBill Paul 	 * resulting ethernet frame that appears on the wire will
258999c8ae87SPyun YongHyeon 	 * have garbled payload. To work around this, if TX IP checksum
25900fc4974fSBill Paul 	 * offload is enabled, we always manually pad short frames out
2591d65abd66SPyun YongHyeon 	 * to the minimum ethernet frame size.
25920fc4974fSBill Paul 	 */
2593f2e491c9SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_AUTOPAD) == 0 &&
2594deb5c680SPyun YongHyeon 	    (*m_head)->m_pkthdr.len < RL_IP4CSUMTX_PADLEN &&
259599c8ae87SPyun YongHyeon 	    ((*m_head)->m_pkthdr.csum_flags & CSUM_IP) != 0) {
2596d65abd66SPyun YongHyeon 		padlen = RL_MIN_FRAMELEN - (*m_head)->m_pkthdr.len;
2597d65abd66SPyun YongHyeon 		if (M_WRITABLE(*m_head) == 0) {
2598d65abd66SPyun YongHyeon 			/* Get a writable copy. */
2599d65abd66SPyun YongHyeon 			m_new = m_dup(*m_head, M_DONTWAIT);
2600d65abd66SPyun YongHyeon 			m_freem(*m_head);
2601d65abd66SPyun YongHyeon 			if (m_new == NULL) {
2602d65abd66SPyun YongHyeon 				*m_head = NULL;
2603a94100faSBill Paul 				return (ENOBUFS);
2604a94100faSBill Paul 			}
2605d65abd66SPyun YongHyeon 			*m_head = m_new;
2606d65abd66SPyun YongHyeon 		}
2607d65abd66SPyun YongHyeon 		if ((*m_head)->m_next != NULL ||
2608d65abd66SPyun YongHyeon 		    M_TRAILINGSPACE(*m_head) < padlen) {
260980a2a305SJohn-Mark Gurney 			m_new = m_defrag(*m_head, M_DONTWAIT);
2610b4b95879SMarius Strobl 			if (m_new == NULL) {
2611b4b95879SMarius Strobl 				m_freem(*m_head);
2612b4b95879SMarius Strobl 				*m_head = NULL;
261380a2a305SJohn-Mark Gurney 				return (ENOBUFS);
2614b4b95879SMarius Strobl 			}
2615d65abd66SPyun YongHyeon 		} else
2616d65abd66SPyun YongHyeon 			m_new = *m_head;
2617a94100faSBill Paul 
26180fc4974fSBill Paul 		/*
26190fc4974fSBill Paul 		 * Manually pad short frames, and zero the pad space
26200fc4974fSBill Paul 		 * to avoid leaking data.
26210fc4974fSBill Paul 		 */
2622d65abd66SPyun YongHyeon 		bzero(mtod(m_new, char *) + m_new->m_pkthdr.len, padlen);
2623d65abd66SPyun YongHyeon 		m_new->m_pkthdr.len += padlen;
26240fc4974fSBill Paul 		m_new->m_len = m_new->m_pkthdr.len;
2625d65abd66SPyun YongHyeon 		*m_head = m_new;
26260fc4974fSBill Paul 	}
26270fc4974fSBill Paul 
2628d65abd66SPyun YongHyeon 	prod = sc->rl_ldata.rl_tx_prodidx;
2629d65abd66SPyun YongHyeon 	txd = &sc->rl_ldata.rl_tx_desc[prod];
2630d65abd66SPyun YongHyeon 	error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2631d65abd66SPyun YongHyeon 	    *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2632d65abd66SPyun YongHyeon 	if (error == EFBIG) {
2633304a4c6fSJohn Baldwin 		m_new = m_collapse(*m_head, M_DONTWAIT, RL_NTXSEGS);
2634d65abd66SPyun YongHyeon 		if (m_new == NULL) {
2635d65abd66SPyun YongHyeon 			m_freem(*m_head);
2636b4b95879SMarius Strobl 			*m_head = NULL;
2637d65abd66SPyun YongHyeon 			return (ENOBUFS);
2638a94100faSBill Paul 		}
2639d65abd66SPyun YongHyeon 		*m_head = m_new;
2640d65abd66SPyun YongHyeon 		error = bus_dmamap_load_mbuf_sg(sc->rl_ldata.rl_tx_mtag,
2641d65abd66SPyun YongHyeon 		    txd->tx_dmamap, *m_head, segs, &nsegs, BUS_DMA_NOWAIT);
2642d65abd66SPyun YongHyeon 		if (error != 0) {
2643d65abd66SPyun YongHyeon 			m_freem(*m_head);
2644d65abd66SPyun YongHyeon 			*m_head = NULL;
2645d65abd66SPyun YongHyeon 			return (error);
2646a94100faSBill Paul 		}
2647d65abd66SPyun YongHyeon 	} else if (error != 0)
2648d65abd66SPyun YongHyeon 		return (error);
2649d65abd66SPyun YongHyeon 	if (nsegs == 0) {
2650d65abd66SPyun YongHyeon 		m_freem(*m_head);
2651d65abd66SPyun YongHyeon 		*m_head = NULL;
2652d65abd66SPyun YongHyeon 		return (EIO);
2653d65abd66SPyun YongHyeon 	}
2654d65abd66SPyun YongHyeon 
2655d65abd66SPyun YongHyeon 	/* Check for number of available descriptors. */
2656d65abd66SPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free - nsegs <= 1) {
2657d65abd66SPyun YongHyeon 		bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap);
2658d65abd66SPyun YongHyeon 		return (ENOBUFS);
2659d65abd66SPyun YongHyeon 	}
2660d65abd66SPyun YongHyeon 
2661d65abd66SPyun YongHyeon 	bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag, txd->tx_dmamap,
2662d65abd66SPyun YongHyeon 	    BUS_DMASYNC_PREWRITE);
2663a94100faSBill Paul 
2664a94100faSBill Paul 	/*
2665d65abd66SPyun YongHyeon 	 * Set up checksum offload. Note: checksum offload bits must
2666d65abd66SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor transmit
2667d65abd66SPyun YongHyeon 	 * attempt. This is according to testing done with an 8169
2668d65abd66SPyun YongHyeon 	 * chip. This is a requirement.
2669a94100faSBill Paul 	 */
2670deb5c680SPyun YongHyeon 	vlanctl = 0;
2671d65abd66SPyun YongHyeon 	csum_flags = 0;
2672d6d7d923SPyun YongHyeon 	if (((*m_head)->m_pkthdr.csum_flags & CSUM_TSO) != 0) {
2673d6d7d923SPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_DESCV2) != 0) {
2674d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND;
2675d6d7d923SPyun YongHyeon 			vlanctl |= ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2676d6d7d923SPyun YongHyeon 			    RL_TDESC_CMD_MSSVALV2_SHIFT);
2677d6d7d923SPyun YongHyeon 		} else {
2678d6d7d923SPyun YongHyeon 			csum_flags |= RL_TDESC_CMD_LGSEND |
2679d65abd66SPyun YongHyeon 			    ((uint32_t)(*m_head)->m_pkthdr.tso_segsz <<
2680d65abd66SPyun YongHyeon 			    RL_TDESC_CMD_MSSVAL_SHIFT);
2681d6d7d923SPyun YongHyeon 		}
2682d6d7d923SPyun YongHyeon 	} else {
268399c8ae87SPyun YongHyeon 		/*
268499c8ae87SPyun YongHyeon 		 * Unconditionally enable IP checksum if TCP or UDP
268599c8ae87SPyun YongHyeon 		 * checksum is required. Otherwise, TCP/UDP checksum
268699c8ae87SPyun YongHyeon 		 * does't make effects.
268799c8ae87SPyun YongHyeon 		 */
268899c8ae87SPyun YongHyeon 		if (((*m_head)->m_pkthdr.csum_flags & RE_CSUM_FEATURES) != 0) {
2689deb5c680SPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_DESCV2) == 0) {
2690d65abd66SPyun YongHyeon 				csum_flags |= RL_TDESC_CMD_IPCSUM;
2691deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2692deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2693d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_TCPCSUM;
2694deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2695deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2696d65abd66SPyun YongHyeon 					csum_flags |= RL_TDESC_CMD_UDPCSUM;
2697deb5c680SPyun YongHyeon 			} else {
2698deb5c680SPyun YongHyeon 				vlanctl |= RL_TDESC_CMD_IPCSUMV2;
2699deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2700deb5c680SPyun YongHyeon 				    CSUM_TCP) != 0)
2701deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_TCPCSUMV2;
2702deb5c680SPyun YongHyeon 				if (((*m_head)->m_pkthdr.csum_flags &
2703deb5c680SPyun YongHyeon 				    CSUM_UDP) != 0)
2704deb5c680SPyun YongHyeon 					vlanctl |= RL_TDESC_CMD_UDPCSUMV2;
2705deb5c680SPyun YongHyeon 			}
2706d65abd66SPyun YongHyeon 		}
270799c8ae87SPyun YongHyeon 	}
2708a94100faSBill Paul 
2709ccf34c81SPyun YongHyeon 	/*
2710ccf34c81SPyun YongHyeon 	 * Set up hardware VLAN tagging. Note: vlan tag info must
2711ccf34c81SPyun YongHyeon 	 * appear in all descriptors of a multi-descriptor
2712ccf34c81SPyun YongHyeon 	 * transmission attempt.
2713ccf34c81SPyun YongHyeon 	 */
2714ccf34c81SPyun YongHyeon 	if ((*m_head)->m_flags & M_VLANTAG)
2715bddff934SPyun YongHyeon 		vlanctl |= bswap16((*m_head)->m_pkthdr.ether_vtag) |
2716deb5c680SPyun YongHyeon 		    RL_TDESC_VLANCTL_TAG;
2717ccf34c81SPyun YongHyeon 
2718d65abd66SPyun YongHyeon 	si = prod;
2719d65abd66SPyun YongHyeon 	for (i = 0; i < nsegs; i++, prod = RL_TX_DESC_NXT(sc, prod)) {
2720d65abd66SPyun YongHyeon 		desc = &sc->rl_ldata.rl_tx_list[prod];
2721deb5c680SPyun YongHyeon 		desc->rl_vlanctl = htole32(vlanctl);
2722d65abd66SPyun YongHyeon 		desc->rl_bufaddr_lo = htole32(RL_ADDR_LO(segs[i].ds_addr));
2723d65abd66SPyun YongHyeon 		desc->rl_bufaddr_hi = htole32(RL_ADDR_HI(segs[i].ds_addr));
2724d65abd66SPyun YongHyeon 		cmdstat = segs[i].ds_len;
2725d65abd66SPyun YongHyeon 		if (i != 0)
2726d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_OWN;
2727d65abd66SPyun YongHyeon 		if (prod == sc->rl_ldata.rl_tx_desc_cnt - 1)
2728d65abd66SPyun YongHyeon 			cmdstat |= RL_TDESC_CMD_EOR;
2729d65abd66SPyun YongHyeon 		desc->rl_cmdstat = htole32(cmdstat | csum_flags);
2730d65abd66SPyun YongHyeon 		sc->rl_ldata.rl_tx_free--;
2731d65abd66SPyun YongHyeon 	}
2732d65abd66SPyun YongHyeon 	/* Update producer index. */
2733d65abd66SPyun YongHyeon 	sc->rl_ldata.rl_tx_prodidx = prod;
2734a94100faSBill Paul 
2735d65abd66SPyun YongHyeon 	/* Set EOF on the last descriptor. */
2736d65abd66SPyun YongHyeon 	ei = RL_TX_DESC_PRV(sc, prod);
2737d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[ei];
2738d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_EOF);
2739d65abd66SPyun YongHyeon 
2740d65abd66SPyun YongHyeon 	desc = &sc->rl_ldata.rl_tx_list[si];
2741d65abd66SPyun YongHyeon 	/* Set SOF and transfer ownership of packet to the chip. */
2742d65abd66SPyun YongHyeon 	desc->rl_cmdstat |= htole32(RL_TDESC_CMD_OWN | RL_TDESC_CMD_SOF);
2743a94100faSBill Paul 
2744d65abd66SPyun YongHyeon 	/*
2745d65abd66SPyun YongHyeon 	 * Insure that the map for this transmission
2746d65abd66SPyun YongHyeon 	 * is placed at the array index of the last descriptor
2747d65abd66SPyun YongHyeon 	 * in this chain.  (Swap last and first dmamaps.)
2748d65abd66SPyun YongHyeon 	 */
2749d65abd66SPyun YongHyeon 	txd_last = &sc->rl_ldata.rl_tx_desc[ei];
2750d65abd66SPyun YongHyeon 	map = txd->tx_dmamap;
2751d65abd66SPyun YongHyeon 	txd->tx_dmamap = txd_last->tx_dmamap;
2752d65abd66SPyun YongHyeon 	txd_last->tx_dmamap = map;
2753d65abd66SPyun YongHyeon 	txd_last->tx_m = *m_head;
2754a94100faSBill Paul 
2755a94100faSBill Paul 	return (0);
2756a94100faSBill Paul }
2757a94100faSBill Paul 
275897b9d4baSJohn-Mark Gurney static void
2759d180a66fSPyun YongHyeon re_start(struct ifnet *ifp)
276097b9d4baSJohn-Mark Gurney {
2761d180a66fSPyun YongHyeon 	struct rl_softc		*sc;
276297b9d4baSJohn-Mark Gurney 
2763d180a66fSPyun YongHyeon 	sc = ifp->if_softc;
2764d180a66fSPyun YongHyeon 	RL_LOCK(sc);
2765d180a66fSPyun YongHyeon 	re_start_locked(ifp);
2766d180a66fSPyun YongHyeon 	RL_UNLOCK(sc);
276797b9d4baSJohn-Mark Gurney }
276897b9d4baSJohn-Mark Gurney 
2769a94100faSBill Paul /*
2770a94100faSBill Paul  * Main transmit routine for C+ and gigE NICs.
2771a94100faSBill Paul  */
2772a94100faSBill Paul static void
2773d180a66fSPyun YongHyeon re_start_locked(struct ifnet *ifp)
2774a94100faSBill Paul {
2775a94100faSBill Paul 	struct rl_softc		*sc;
2776d65abd66SPyun YongHyeon 	struct mbuf		*m_head;
2777d65abd66SPyun YongHyeon 	int			queued;
2778a94100faSBill Paul 
2779a94100faSBill Paul 	sc = ifp->if_softc;
278097b9d4baSJohn-Mark Gurney 
2781d65abd66SPyun YongHyeon 	if ((ifp->if_drv_flags & (IFF_DRV_RUNNING | IFF_DRV_OACTIVE)) !=
2782d180a66fSPyun YongHyeon 	    IFF_DRV_RUNNING || (sc->rl_flags & RL_FLAG_LINK) == 0)
2783ed510fb0SBill Paul 		return;
2784a94100faSBill Paul 
2785d65abd66SPyun YongHyeon 	for (queued = 0; !IFQ_DRV_IS_EMPTY(&ifp->if_snd) &&
2786d65abd66SPyun YongHyeon 	    sc->rl_ldata.rl_tx_free > 1;) {
278752732175SMax Laier 		IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
2788a94100faSBill Paul 		if (m_head == NULL)
2789a94100faSBill Paul 			break;
2790a94100faSBill Paul 
2791d65abd66SPyun YongHyeon 		if (re_encap(sc, &m_head) != 0) {
2792b4b95879SMarius Strobl 			if (m_head == NULL)
2793b4b95879SMarius Strobl 				break;
279452732175SMax Laier 			IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
279513f4c340SRobert Watson 			ifp->if_drv_flags |= IFF_DRV_OACTIVE;
2796a94100faSBill Paul 			break;
2797a94100faSBill Paul 		}
2798a94100faSBill Paul 
2799a94100faSBill Paul 		/*
2800a94100faSBill Paul 		 * If there's a BPF listener, bounce a copy of this frame
2801a94100faSBill Paul 		 * to him.
2802a94100faSBill Paul 		 */
280359a0d28bSChristian S.J. Peron 		ETHER_BPF_MTAP(ifp, m_head);
280452732175SMax Laier 
280552732175SMax Laier 		queued++;
2806a94100faSBill Paul 	}
2807a94100faSBill Paul 
2808ed510fb0SBill Paul 	if (queued == 0) {
2809ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2810d65abd66SPyun YongHyeon 		if (sc->rl_ldata.rl_tx_free != sc->rl_ldata.rl_tx_desc_cnt)
2811ed510fb0SBill Paul 			CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2812ed510fb0SBill Paul #endif
281352732175SMax Laier 		return;
2814ed510fb0SBill Paul 	}
281552732175SMax Laier 
2816a94100faSBill Paul 	/* Flush the TX descriptors */
2817a94100faSBill Paul 
2818a94100faSBill Paul 	bus_dmamap_sync(sc->rl_ldata.rl_tx_list_tag,
2819a94100faSBill Paul 	    sc->rl_ldata.rl_tx_list_map,
2820a94100faSBill Paul 	    BUS_DMASYNC_PREWRITE|BUS_DMASYNC_PREREAD);
2821a94100faSBill Paul 
28220fc4974fSBill Paul 	CSR_WRITE_1(sc, sc->rl_txstart, RL_TXSTART_START);
2823a94100faSBill Paul 
2824ed510fb0SBill Paul #ifdef RE_TX_MODERATION
2825a94100faSBill Paul 	/*
2826a94100faSBill Paul 	 * Use the countdown timer for interrupt moderation.
2827a94100faSBill Paul 	 * 'TX done' interrupts are disabled. Instead, we reset the
2828a94100faSBill Paul 	 * countdown timer, which will begin counting until it hits
2829a94100faSBill Paul 	 * the value in the TIMERINT register, and then trigger an
2830a94100faSBill Paul 	 * interrupt. Each time we write to the TIMERCNT register,
2831a94100faSBill Paul 	 * the timer count is reset to 0.
2832a94100faSBill Paul 	 */
2833a94100faSBill Paul 	CSR_WRITE_4(sc, RL_TIMERCNT, 1);
2834ed510fb0SBill Paul #endif
2835a94100faSBill Paul 
2836a94100faSBill Paul 	/*
2837a94100faSBill Paul 	 * Set a timeout in case the chip goes out to lunch.
2838a94100faSBill Paul 	 */
28391d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 5;
2840a94100faSBill Paul }
2841a94100faSBill Paul 
2842a94100faSBill Paul static void
284381eee0ebSPyun YongHyeon re_set_jumbo(struct rl_softc *sc, int jumbo)
284481eee0ebSPyun YongHyeon {
284581eee0ebSPyun YongHyeon 
284681eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8168E_VL) {
284781eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
284881eee0ebSPyun YongHyeon 		return;
284981eee0ebSPyun YongHyeon 	}
285081eee0ebSPyun YongHyeon 
285181eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
285281eee0ebSPyun YongHyeon 	if (jumbo != 0) {
285381eee0ebSPyun YongHyeon 		CSR_WRITE_1(sc, RL_CFG3, CSR_READ_1(sc, RL_CFG3) |
285481eee0ebSPyun YongHyeon 		    RL_CFG3_JUMBO_EN0);
285581eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
285681eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
285781eee0ebSPyun YongHyeon 			break;
285881eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
285981eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) |
286081eee0ebSPyun YongHyeon 			    0x01);
286181eee0ebSPyun YongHyeon 			break;
286281eee0ebSPyun YongHyeon 		default:
286381eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) |
286481eee0ebSPyun YongHyeon 			    RL_CFG4_JUMBO_EN1);
286581eee0ebSPyun YongHyeon 		}
286681eee0ebSPyun YongHyeon 	} else {
286781eee0ebSPyun YongHyeon 		CSR_WRITE_1(sc, RL_CFG3, CSR_READ_1(sc, RL_CFG3) &
286881eee0ebSPyun YongHyeon 		    ~RL_CFG3_JUMBO_EN0);
286981eee0ebSPyun YongHyeon 		switch (sc->rl_hwrev->rl_rev) {
287081eee0ebSPyun YongHyeon 		case RL_HWREV_8168DP:
287181eee0ebSPyun YongHyeon 			break;
287281eee0ebSPyun YongHyeon 		case RL_HWREV_8168E:
287381eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) &
287481eee0ebSPyun YongHyeon 			    ~0x01);
287581eee0ebSPyun YongHyeon 			break;
287681eee0ebSPyun YongHyeon 		default:
287781eee0ebSPyun YongHyeon 			CSR_WRITE_1(sc, RL_CFG4, CSR_READ_1(sc, RL_CFG4) &
287881eee0ebSPyun YongHyeon 			    ~RL_CFG4_JUMBO_EN1);
287981eee0ebSPyun YongHyeon 		}
288081eee0ebSPyun YongHyeon 	}
288181eee0ebSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
288281eee0ebSPyun YongHyeon 
288381eee0ebSPyun YongHyeon 	switch (sc->rl_hwrev->rl_rev) {
288481eee0ebSPyun YongHyeon 	case RL_HWREV_8168DP:
288581eee0ebSPyun YongHyeon 		pci_set_max_read_req(sc->rl_dev, 4096);
288681eee0ebSPyun YongHyeon 		break;
288781eee0ebSPyun YongHyeon 	default:
288881eee0ebSPyun YongHyeon 		if (jumbo != 0)
288981eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 512);
289081eee0ebSPyun YongHyeon 		else
289181eee0ebSPyun YongHyeon 			pci_set_max_read_req(sc->rl_dev, 4096);
289281eee0ebSPyun YongHyeon 	}
289381eee0ebSPyun YongHyeon }
289481eee0ebSPyun YongHyeon 
289581eee0ebSPyun YongHyeon static void
28967b5ffebfSPyun YongHyeon re_init(void *xsc)
2897a94100faSBill Paul {
2898a94100faSBill Paul 	struct rl_softc		*sc = xsc;
289997b9d4baSJohn-Mark Gurney 
290097b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
290197b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
290297b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
290397b9d4baSJohn-Mark Gurney }
290497b9d4baSJohn-Mark Gurney 
290597b9d4baSJohn-Mark Gurney static void
29067b5ffebfSPyun YongHyeon re_init_locked(struct rl_softc *sc)
290797b9d4baSJohn-Mark Gurney {
2908fc74a9f9SBrooks Davis 	struct ifnet		*ifp = sc->rl_ifp;
2909a94100faSBill Paul 	struct mii_data		*mii;
2910566ca8caSJung-uk Kim 	uint32_t		reg;
291170acaecfSPyun YongHyeon 	uint16_t		cfg;
29124d3d7085SBernd Walter 	union {
29134d3d7085SBernd Walter 		uint32_t align_dummy;
29144d3d7085SBernd Walter 		u_char eaddr[ETHER_ADDR_LEN];
29154d3d7085SBernd Walter         } eaddr;
2916a94100faSBill Paul 
291797b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
291897b9d4baSJohn-Mark Gurney 
2919a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
2920a94100faSBill Paul 
29218476c243SPyun YongHyeon 	if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
29228476c243SPyun YongHyeon 		return;
29238476c243SPyun YongHyeon 
2924a94100faSBill Paul 	/*
2925a94100faSBill Paul 	 * Cancel pending I/O and free all RX/TX buffers.
2926a94100faSBill Paul 	 */
2927a94100faSBill Paul 	re_stop(sc);
2928a94100faSBill Paul 
2929b659f1f0SPyun YongHyeon 	/* Put controller into known state. */
2930b659f1f0SPyun YongHyeon 	re_reset(sc);
2931b659f1f0SPyun YongHyeon 
2932a94100faSBill Paul 	/*
29334a814a5eSPyun YongHyeon 	 * For C+ mode, initialize the RX descriptors and mbufs.
29344a814a5eSPyun YongHyeon 	 */
293581eee0ebSPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
293681eee0ebSPyun YongHyeon 		if (ifp->if_mtu > RL_MTU) {
293781eee0ebSPyun YongHyeon 			if (re_jrx_list_init(sc) != 0) {
293881eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
293981eee0ebSPyun YongHyeon 				    "no memory for jumbo RX buffers\n");
294081eee0ebSPyun YongHyeon 				re_stop(sc);
294181eee0ebSPyun YongHyeon 				return;
294281eee0ebSPyun YongHyeon 			}
294381eee0ebSPyun YongHyeon 			/* Disable checksum offloading for jumbo frames. */
294481eee0ebSPyun YongHyeon 			ifp->if_capenable &= ~(IFCAP_HWCSUM | IFCAP_TSO4);
294581eee0ebSPyun YongHyeon 			ifp->if_hwassist &= ~(RE_CSUM_FEATURES | CSUM_TSO);
294681eee0ebSPyun YongHyeon 		} else {
294781eee0ebSPyun YongHyeon 			if (re_rx_list_init(sc) != 0) {
294881eee0ebSPyun YongHyeon 				device_printf(sc->rl_dev,
294981eee0ebSPyun YongHyeon 				    "no memory for RX buffers\n");
295081eee0ebSPyun YongHyeon 				re_stop(sc);
295181eee0ebSPyun YongHyeon 				return;
295281eee0ebSPyun YongHyeon 			}
295381eee0ebSPyun YongHyeon 		}
295481eee0ebSPyun YongHyeon 		re_set_jumbo(sc, ifp->if_mtu > RL_MTU);
295581eee0ebSPyun YongHyeon 	} else {
29564a814a5eSPyun YongHyeon 		if (re_rx_list_init(sc) != 0) {
29574a814a5eSPyun YongHyeon 			device_printf(sc->rl_dev, "no memory for RX buffers\n");
29584a814a5eSPyun YongHyeon 			re_stop(sc);
29594a814a5eSPyun YongHyeon 			return;
29604a814a5eSPyun YongHyeon 		}
296181eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
296281eee0ebSPyun YongHyeon 		    pci_get_device(sc->rl_dev) != RT_DEVICEID_8101E) {
296381eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
296481eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 512);
296581eee0ebSPyun YongHyeon 			else
296681eee0ebSPyun YongHyeon 				pci_set_max_read_req(sc->rl_dev, 4096);
296781eee0ebSPyun YongHyeon 		}
296881eee0ebSPyun YongHyeon 	}
29694a814a5eSPyun YongHyeon 	re_tx_list_init(sc);
29704a814a5eSPyun YongHyeon 
29714a814a5eSPyun YongHyeon 	/*
2972c2c6548bSBill Paul 	 * Enable C+ RX and TX mode, as well as VLAN stripping and
2973edd03374SBill Paul 	 * RX checksum offload. We must configure the C+ register
2974c2c6548bSBill Paul 	 * before all others.
2975c2c6548bSBill Paul 	 */
297670acaecfSPyun YongHyeon 	cfg = RL_CPLUSCMD_PCI_MRW;
297770acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_RXCSUM) != 0)
297870acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXCSUM_ENB;
297970acaecfSPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) != 0)
298070acaecfSPyun YongHyeon 		cfg |= RL_CPLUSCMD_VLANSTRIP;
2981deb5c680SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSTAT) != 0) {
2982deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_MACSTAT_DIS;
2983deb5c680SPyun YongHyeon 		/* XXX magic. */
2984deb5c680SPyun YongHyeon 		cfg |= 0x0001;
2985deb5c680SPyun YongHyeon 	} else
2986deb5c680SPyun YongHyeon 		cfg |= RL_CPLUSCMD_RXENB | RL_CPLUSCMD_TXENB;
2987deb5c680SPyun YongHyeon 	CSR_WRITE_2(sc, RL_CPLUS_CMD, cfg);
298881eee0ebSPyun YongHyeon 	if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SC ||
298981eee0ebSPyun YongHyeon 	    sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE) {
2990566ca8caSJung-uk Kim 		reg = 0x000fff00;
2991566ca8caSJung-uk Kim 		if ((CSR_READ_1(sc, RL_CFG2) & RL_CFG2_PCI66MHZ) != 0)
2992566ca8caSJung-uk Kim 			reg |= 0x000000ff;
299381eee0ebSPyun YongHyeon 		if (sc->rl_hwrev->rl_rev == RL_HWREV_8169_8110SCE)
2994566ca8caSJung-uk Kim 			reg |= 0x00f00000;
2995566ca8caSJung-uk Kim 		CSR_WRITE_4(sc, 0x7c, reg);
2996566ca8caSJung-uk Kim 		/* Disable interrupt mitigation. */
2997566ca8caSJung-uk Kim 		CSR_WRITE_2(sc, 0xe2, 0);
2998566ca8caSJung-uk Kim 	}
2999ae644087SPyun YongHyeon 	/*
3000ae644087SPyun YongHyeon 	 * Disable TSO if interface MTU size is greater than MSS
3001ae644087SPyun YongHyeon 	 * allowed in controller.
3002ae644087SPyun YongHyeon 	 */
3003ae644087SPyun YongHyeon 	if (ifp->if_mtu > RL_TSO_MTU && (ifp->if_capenable & IFCAP_TSO4) != 0) {
3004ae644087SPyun YongHyeon 		ifp->if_capenable &= ~IFCAP_TSO4;
3005ae644087SPyun YongHyeon 		ifp->if_hwassist &= ~CSUM_TSO;
3006ae644087SPyun YongHyeon 	}
3007c2c6548bSBill Paul 
3008c2c6548bSBill Paul 	/*
3009a94100faSBill Paul 	 * Init our MAC address.  Even though the chipset
3010a94100faSBill Paul 	 * documentation doesn't mention it, we need to enter "Config
3011a94100faSBill Paul 	 * register write enable" mode to modify the ID registers.
3012a94100faSBill Paul 	 */
30134d3d7085SBernd Walter 	/* Copy MAC address on stack to align. */
30144d3d7085SBernd Walter 	bcopy(IF_LLADDR(ifp), eaddr.eaddr, ETHER_ADDR_LEN);
3015a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_WRITECFG);
3016ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR0,
3017ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[0])));
3018ed510fb0SBill Paul 	CSR_WRITE_4(sc, RL_IDR4,
3019ed510fb0SBill Paul 	    htole32(*(u_int32_t *)(&eaddr.eaddr[4])));
3020a94100faSBill Paul 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
3021a94100faSBill Paul 
3022a94100faSBill Paul 	/*
3023d01fac16SPyun YongHyeon 	 * Load the addresses of the RX and TX lists into the chip.
3024d01fac16SPyun YongHyeon 	 */
3025d01fac16SPyun YongHyeon 
3026d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_HI,
3027d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_rx_list_addr));
3028d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_RXLIST_ADDR_LO,
3029d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_rx_list_addr));
3030d01fac16SPyun YongHyeon 
3031d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_HI,
3032d01fac16SPyun YongHyeon 	    RL_ADDR_HI(sc->rl_ldata.rl_tx_list_addr));
3033d01fac16SPyun YongHyeon 	CSR_WRITE_4(sc, RL_TXLIST_ADDR_LO,
3034d01fac16SPyun YongHyeon 	    RL_ADDR_LO(sc->rl_ldata.rl_tx_list_addr));
3035d01fac16SPyun YongHyeon 
3036d01fac16SPyun YongHyeon 	/*
3037a94100faSBill Paul 	 * Enable transmit and receive.
3038a94100faSBill Paul 	 */
3039a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3040a94100faSBill Paul 
3041a94100faSBill Paul 	/*
3042ff191365SJung-uk Kim 	 * Set the initial TX configuration.
3043a94100faSBill Paul 	 */
3044abc8ff44SBill Paul 	if (sc->rl_testmode) {
3045abc8ff44SBill Paul 		if (sc->rl_type == RL_8169)
3046abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3047abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON);
3048a94100faSBill Paul 		else
3049abc8ff44SBill Paul 			CSR_WRITE_4(sc, RL_TXCFG,
3050abc8ff44SBill Paul 			    RL_TXCFG_CONFIG|RL_LOOPTEST_ON_CPLUS);
3051abc8ff44SBill Paul 	} else
3052a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TXCFG, RL_TXCFG_CONFIG);
3053d01fac16SPyun YongHyeon 
3054d01fac16SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EARLY_TX_THRESH, 16);
3055d01fac16SPyun YongHyeon 
3056a94100faSBill Paul 	/*
3057ff191365SJung-uk Kim 	 * Set the initial RX configuration.
3058a94100faSBill Paul 	 */
3059ff191365SJung-uk Kim 	re_set_rxmode(sc);
3060a94100faSBill Paul 
3061483cc440SPyun YongHyeon 	/* Configure interrupt moderation. */
3062483cc440SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
3063483cc440SPyun YongHyeon 		/* Magic from vendor. */
30645e6906eeSPyun YongHyeon 		CSR_WRITE_2(sc, RL_INTRMOD, 0x5100);
3065483cc440SPyun YongHyeon 	}
3066483cc440SPyun YongHyeon 
3067a94100faSBill Paul #ifdef DEVICE_POLLING
3068a94100faSBill Paul 	/*
3069a94100faSBill Paul 	 * Disable interrupts if we are polling.
3070a94100faSBill Paul 	 */
307140929967SGleb Smirnoff 	if (ifp->if_capenable & IFCAP_POLLING)
3072a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3073a94100faSBill Paul 	else	/* otherwise ... */
307440929967SGleb Smirnoff #endif
3075ed510fb0SBill Paul 
3076a94100faSBill Paul 	/*
3077a94100faSBill Paul 	 * Enable interrupts.
3078a94100faSBill Paul 	 */
3079a94100faSBill Paul 	if (sc->rl_testmode)
3080a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, 0);
3081a94100faSBill Paul 	else
3082a94100faSBill Paul 		CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
3083ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, RL_INTRS_CPLUS);
3084a94100faSBill Paul 
3085a94100faSBill Paul 	/* Set initial TX threshold */
3086a94100faSBill Paul 	sc->rl_txthresh = RL_TX_THRESH_INIT;
3087a94100faSBill Paul 
3088a94100faSBill Paul 	/* Start RX/TX process. */
3089a94100faSBill Paul 	CSR_WRITE_4(sc, RL_MISSEDPKT, 0);
3090a94100faSBill Paul #ifdef notdef
3091a94100faSBill Paul 	/* Enable receiver and transmitter. */
3092a94100faSBill Paul 	CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_TX_ENB|RL_CMD_RX_ENB);
3093a94100faSBill Paul #endif
3094a94100faSBill Paul 
3095a94100faSBill Paul 	/*
3096a94100faSBill Paul 	 * Initialize the timer interrupt register so that
3097a94100faSBill Paul 	 * a timer interrupt will be generated once the timer
3098a94100faSBill Paul 	 * reaches a certain number of ticks. The timer is
3099502be0f7SPyun YongHyeon 	 * reloaded on each transmit.
3100502be0f7SPyun YongHyeon 	 */
3101502be0f7SPyun YongHyeon #ifdef RE_TX_MODERATION
3102502be0f7SPyun YongHyeon 	/*
3103502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate TX interrupt
3104a94100faSBill Paul 	 * moderation, which dramatically improves TX frame rate.
3105a94100faSBill Paul 	 */
3106a94100faSBill Paul 	if (sc->rl_type == RL_8169)
3107a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT_8169, 0x800);
3108a94100faSBill Paul 	else
3109a94100faSBill Paul 		CSR_WRITE_4(sc, RL_TIMERINT, 0x400);
3110502be0f7SPyun YongHyeon #else
3111502be0f7SPyun YongHyeon 	/*
3112502be0f7SPyun YongHyeon 	 * Use timer interrupt register to moderate RX interrupt
3113502be0f7SPyun YongHyeon 	 * moderation.
3114502be0f7SPyun YongHyeon 	 */
3115502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) != 0 &&
3116502be0f7SPyun YongHyeon 	    intr_filter == 0) {
3117502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3118502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169,
3119502be0f7SPyun YongHyeon 			    RL_USECS(sc->rl_int_rx_mod));
3120502be0f7SPyun YongHyeon 	} else {
3121502be0f7SPyun YongHyeon 		if (sc->rl_type == RL_8169)
3122502be0f7SPyun YongHyeon 			CSR_WRITE_4(sc, RL_TIMERINT_8169, RL_USECS(0));
3123502be0f7SPyun YongHyeon 	}
3124ed510fb0SBill Paul #endif
3125a94100faSBill Paul 
3126a94100faSBill Paul 	/*
3127a94100faSBill Paul 	 * For 8169 gigE NICs, set the max allowed RX packet
3128a94100faSBill Paul 	 * size so we can receive jumbo frames.
3129a94100faSBill Paul 	 */
313089feeee4SPyun YongHyeon 	if (sc->rl_type == RL_8169) {
313181eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0) {
313281eee0ebSPyun YongHyeon 			/*
313381eee0ebSPyun YongHyeon 			 * For controllers that use new jumbo frame scheme,
313481eee0ebSPyun YongHyeon 			 * set maximum size of jumbo frame depedning on
313581eee0ebSPyun YongHyeon 			 * controller revisions.
313681eee0ebSPyun YongHyeon 			 */
313781eee0ebSPyun YongHyeon 			if (ifp->if_mtu > RL_MTU)
313881eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
313981eee0ebSPyun YongHyeon 				    sc->rl_hwrev->rl_max_mtu +
314081eee0ebSPyun YongHyeon 				    ETHER_VLAN_ENCAP_LEN + ETHER_HDR_LEN +
314181eee0ebSPyun YongHyeon 				    ETHER_CRC_LEN);
314289feeee4SPyun YongHyeon 			else
314381eee0ebSPyun YongHyeon 				CSR_WRITE_2(sc, RL_MAXRXPKTLEN,
314481eee0ebSPyun YongHyeon 				    RE_RX_DESC_BUFLEN);
314581eee0ebSPyun YongHyeon 		} else if ((sc->rl_flags & RL_FLAG_PCIE) != 0 &&
314681eee0ebSPyun YongHyeon 		    sc->rl_hwrev->rl_max_mtu == RL_MTU) {
314781eee0ebSPyun YongHyeon 			/* RTL810x has no jumbo frame support. */
314881eee0ebSPyun YongHyeon 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, RE_RX_DESC_BUFLEN);
314981eee0ebSPyun YongHyeon 		} else
3150a94100faSBill Paul 			CSR_WRITE_2(sc, RL_MAXRXPKTLEN, 16383);
315189feeee4SPyun YongHyeon 	}
3152a94100faSBill Paul 
315397b9d4baSJohn-Mark Gurney 	if (sc->rl_testmode)
3154a94100faSBill Paul 		return;
3155a94100faSBill Paul 
3156a94100faSBill Paul 	mii_mediachg(mii);
3157a94100faSBill Paul 
315819ecd231SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, CSR_READ_1(sc, RL_CFG1) | RL_CFG1_DRVLOAD);
3159a94100faSBill Paul 
316013f4c340SRobert Watson 	ifp->if_drv_flags |= IFF_DRV_RUNNING;
316113f4c340SRobert Watson 	ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
3162a94100faSBill Paul 
3163351a76f9SPyun YongHyeon 	sc->rl_flags &= ~RL_FLAG_LINK;
31641d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3165d1754a9bSJohn Baldwin 	callout_reset(&sc->rl_stat_callout, hz, re_tick, sc);
3166a94100faSBill Paul }
3167a94100faSBill Paul 
3168a94100faSBill Paul /*
3169a94100faSBill Paul  * Set media options.
3170a94100faSBill Paul  */
3171a94100faSBill Paul static int
31727b5ffebfSPyun YongHyeon re_ifmedia_upd(struct ifnet *ifp)
3173a94100faSBill Paul {
3174a94100faSBill Paul 	struct rl_softc		*sc;
3175a94100faSBill Paul 	struct mii_data		*mii;
31766f0f9b12SPyun YongHyeon 	int			error;
3177a94100faSBill Paul 
3178a94100faSBill Paul 	sc = ifp->if_softc;
3179a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3180d1754a9bSJohn Baldwin 	RL_LOCK(sc);
31816f0f9b12SPyun YongHyeon 	error = mii_mediachg(mii);
3182d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3183a94100faSBill Paul 
31846f0f9b12SPyun YongHyeon 	return (error);
3185a94100faSBill Paul }
3186a94100faSBill Paul 
3187a94100faSBill Paul /*
3188a94100faSBill Paul  * Report current media status.
3189a94100faSBill Paul  */
3190a94100faSBill Paul static void
31917b5ffebfSPyun YongHyeon re_ifmedia_sts(struct ifnet *ifp, struct ifmediareq *ifmr)
3192a94100faSBill Paul {
3193a94100faSBill Paul 	struct rl_softc		*sc;
3194a94100faSBill Paul 	struct mii_data		*mii;
3195a94100faSBill Paul 
3196a94100faSBill Paul 	sc = ifp->if_softc;
3197a94100faSBill Paul 	mii = device_get_softc(sc->rl_miibus);
3198a94100faSBill Paul 
3199d1754a9bSJohn Baldwin 	RL_LOCK(sc);
3200a94100faSBill Paul 	mii_pollstat(mii);
3201d1754a9bSJohn Baldwin 	RL_UNLOCK(sc);
3202a94100faSBill Paul 	ifmr->ifm_active = mii->mii_media_active;
3203a94100faSBill Paul 	ifmr->ifm_status = mii->mii_media_status;
3204a94100faSBill Paul }
3205a94100faSBill Paul 
3206a94100faSBill Paul static int
32077b5ffebfSPyun YongHyeon re_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
3208a94100faSBill Paul {
3209a94100faSBill Paul 	struct rl_softc		*sc = ifp->if_softc;
3210a94100faSBill Paul 	struct ifreq		*ifr = (struct ifreq *) data;
3211a94100faSBill Paul 	struct mii_data		*mii;
321240929967SGleb Smirnoff 	int			error = 0;
3213a94100faSBill Paul 
3214a94100faSBill Paul 	switch (command) {
3215a94100faSBill Paul 	case SIOCSIFMTU:
321681eee0ebSPyun YongHyeon 		if (ifr->ifr_mtu < ETHERMIN ||
321781eee0ebSPyun YongHyeon 		    ifr->ifr_mtu > sc->rl_hwrev->rl_max_mtu) {
3218c1d0b573SPyun YongHyeon 			error = EINVAL;
3219c1d0b573SPyun YongHyeon 			break;
3220c1d0b573SPyun YongHyeon 		}
3221c1d0b573SPyun YongHyeon 		RL_LOCK(sc);
322281eee0ebSPyun YongHyeon 		if (ifp->if_mtu != ifr->ifr_mtu) {
3223a94100faSBill Paul 			ifp->if_mtu = ifr->ifr_mtu;
322481eee0ebSPyun YongHyeon 			if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
322581eee0ebSPyun YongHyeon 			    (ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
322681eee0ebSPyun YongHyeon 				ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
322781eee0ebSPyun YongHyeon 				re_init_locked(sc);
322881eee0ebSPyun YongHyeon 			}
3229ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3230ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
323181eee0ebSPyun YongHyeon 				ifp->if_capenable &= ~(IFCAP_TSO4 |
323281eee0ebSPyun YongHyeon 				    IFCAP_VLAN_HWTSO);
3233ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
323481eee0ebSPyun YongHyeon 			}
3235ecafbbb5SPyun YongHyeon 			VLAN_CAPABILITIES(ifp);
3236ae644087SPyun YongHyeon 		}
3237d1754a9bSJohn Baldwin 		RL_UNLOCK(sc);
3238a94100faSBill Paul 		break;
3239a94100faSBill Paul 	case SIOCSIFFLAGS:
324097b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
3241eed497bbSPyun YongHyeon 		if ((ifp->if_flags & IFF_UP) != 0) {
3242eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0) {
3243eed497bbSPyun YongHyeon 				if (((ifp->if_flags ^ sc->rl_if_flags)
32443021aef8SPyun YongHyeon 				    & (IFF_PROMISC | IFF_ALLMULTI)) != 0)
3245ff191365SJung-uk Kim 					re_set_rxmode(sc);
3246eed497bbSPyun YongHyeon 			} else
324797b9d4baSJohn-Mark Gurney 				re_init_locked(sc);
3248eed497bbSPyun YongHyeon 		} else {
3249eed497bbSPyun YongHyeon 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3250a94100faSBill Paul 				re_stop(sc);
3251eed497bbSPyun YongHyeon 		}
3252eed497bbSPyun YongHyeon 		sc->rl_if_flags = ifp->if_flags;
325397b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3254a94100faSBill Paul 		break;
3255a94100faSBill Paul 	case SIOCADDMULTI:
3256a94100faSBill Paul 	case SIOCDELMULTI:
325797b9d4baSJohn-Mark Gurney 		RL_LOCK(sc);
32588476c243SPyun YongHyeon 		if ((ifp->if_drv_flags & IFF_DRV_RUNNING) != 0)
3259ff191365SJung-uk Kim 			re_set_rxmode(sc);
326097b9d4baSJohn-Mark Gurney 		RL_UNLOCK(sc);
3261a94100faSBill Paul 		break;
3262a94100faSBill Paul 	case SIOCGIFMEDIA:
3263a94100faSBill Paul 	case SIOCSIFMEDIA:
3264a94100faSBill Paul 		mii = device_get_softc(sc->rl_miibus);
3265a94100faSBill Paul 		error = ifmedia_ioctl(ifp, ifr, &mii->mii_media, command);
3266a94100faSBill Paul 		break;
3267a94100faSBill Paul 	case SIOCSIFCAP:
326840929967SGleb Smirnoff 	    {
3269f051cb85SGleb Smirnoff 		int mask, reinit;
3270f051cb85SGleb Smirnoff 
3271f051cb85SGleb Smirnoff 		mask = ifr->ifr_reqcap ^ ifp->if_capenable;
3272f051cb85SGleb Smirnoff 		reinit = 0;
327340929967SGleb Smirnoff #ifdef DEVICE_POLLING
327440929967SGleb Smirnoff 		if (mask & IFCAP_POLLING) {
327540929967SGleb Smirnoff 			if (ifr->ifr_reqcap & IFCAP_POLLING) {
327640929967SGleb Smirnoff 				error = ether_poll_register(re_poll, ifp);
327740929967SGleb Smirnoff 				if (error)
327840929967SGleb Smirnoff 					return (error);
3279d1754a9bSJohn Baldwin 				RL_LOCK(sc);
328040929967SGleb Smirnoff 				/* Disable interrupts */
328140929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, 0x0000);
328240929967SGleb Smirnoff 				ifp->if_capenable |= IFCAP_POLLING;
328340929967SGleb Smirnoff 				RL_UNLOCK(sc);
328440929967SGleb Smirnoff 			} else {
328540929967SGleb Smirnoff 				error = ether_poll_deregister(ifp);
328640929967SGleb Smirnoff 				/* Enable interrupts. */
328740929967SGleb Smirnoff 				RL_LOCK(sc);
328840929967SGleb Smirnoff 				CSR_WRITE_2(sc, RL_IMR, RL_INTRS_CPLUS);
328940929967SGleb Smirnoff 				ifp->if_capenable &= ~IFCAP_POLLING;
329040929967SGleb Smirnoff 				RL_UNLOCK(sc);
329140929967SGleb Smirnoff 			}
329240929967SGleb Smirnoff 		}
329340929967SGleb Smirnoff #endif /* DEVICE_POLLING */
3294d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_TXCSUM) != 0 &&
3295d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TXCSUM) != 0) {
3296d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TXCSUM;
3297d3b181aeSPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_TXCSUM) != 0)
3298dc74159dSPyun YongHyeon 				ifp->if_hwassist |= RE_CSUM_FEATURES;
3299a94100faSBill Paul 			else
3300b61178a9SPyun YongHyeon 				ifp->if_hwassist &= ~RE_CSUM_FEATURES;
3301f051cb85SGleb Smirnoff 			reinit = 1;
330240929967SGleb Smirnoff 		}
3303d3b181aeSPyun YongHyeon 		if ((mask & IFCAP_RXCSUM) != 0 &&
3304d3b181aeSPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_RXCSUM) != 0) {
3305d3b181aeSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_RXCSUM;
3306d3b181aeSPyun YongHyeon 			reinit = 1;
3307d3b181aeSPyun YongHyeon 		}
3308ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_TSO4) != 0 &&
3309ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_TSO) != 0) {
3310dc74159dSPyun YongHyeon 			ifp->if_capenable ^= IFCAP_TSO4;
3311ecafbbb5SPyun YongHyeon 			if ((IFCAP_TSO4 & ifp->if_capenable) != 0)
3312dc74159dSPyun YongHyeon 				ifp->if_hwassist |= CSUM_TSO;
3313dc74159dSPyun YongHyeon 			else
3314dc74159dSPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3315ae644087SPyun YongHyeon 			if (ifp->if_mtu > RL_TSO_MTU &&
3316ae644087SPyun YongHyeon 			    (ifp->if_capenable & IFCAP_TSO4) != 0) {
3317ae644087SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_TSO4;
3318ae644087SPyun YongHyeon 				ifp->if_hwassist &= ~CSUM_TSO;
3319ae644087SPyun YongHyeon 			}
3320dc74159dSPyun YongHyeon 		}
3321ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTSO) != 0 &&
3322ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTSO) != 0)
3323ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTSO;
3324ecafbbb5SPyun YongHyeon 		if ((mask & IFCAP_VLAN_HWTAGGING) != 0 &&
3325ecafbbb5SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_VLAN_HWTAGGING) != 0) {
3326ecafbbb5SPyun YongHyeon 			ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
3327ecafbbb5SPyun YongHyeon 			/* TSO over VLAN requires VLAN hardware tagging. */
3328ecafbbb5SPyun YongHyeon 			if ((ifp->if_capenable & IFCAP_VLAN_HWTAGGING) == 0)
3329ecafbbb5SPyun YongHyeon 				ifp->if_capenable &= ~IFCAP_VLAN_HWTSO;
3330ecafbbb5SPyun YongHyeon 			reinit = 1;
3331ecafbbb5SPyun YongHyeon 		}
333281eee0ebSPyun YongHyeon 		if ((sc->rl_flags & RL_FLAG_JUMBOV2) != 0 &&
333381eee0ebSPyun YongHyeon 		    (mask & (IFCAP_HWCSUM | IFCAP_TSO4 |
333481eee0ebSPyun YongHyeon 		    IFCAP_VLAN_HWTSO)) != 0)
333581eee0ebSPyun YongHyeon 				reinit = 1;
33367467bd53SPyun YongHyeon 		if ((mask & IFCAP_WOL) != 0 &&
33377467bd53SPyun YongHyeon 		    (ifp->if_capabilities & IFCAP_WOL) != 0) {
33387467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_UCAST) != 0)
33397467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_UCAST;
33407467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MCAST) != 0)
33417467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MCAST;
33427467bd53SPyun YongHyeon 			if ((mask & IFCAP_WOL_MAGIC) != 0)
33437467bd53SPyun YongHyeon 				ifp->if_capenable ^= IFCAP_WOL_MAGIC;
33447467bd53SPyun YongHyeon 		}
33458476c243SPyun YongHyeon 		if (reinit && ifp->if_drv_flags & IFF_DRV_RUNNING) {
33468476c243SPyun YongHyeon 			ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
3347f051cb85SGleb Smirnoff 			re_init(sc);
33488476c243SPyun YongHyeon 		}
3349960fd5b3SPyun YongHyeon 		VLAN_CAPABILITIES(ifp);
335040929967SGleb Smirnoff 	    }
3351a94100faSBill Paul 		break;
3352a94100faSBill Paul 	default:
3353a94100faSBill Paul 		error = ether_ioctl(ifp, command, data);
3354a94100faSBill Paul 		break;
3355a94100faSBill Paul 	}
3356a94100faSBill Paul 
3357a94100faSBill Paul 	return (error);
3358a94100faSBill Paul }
3359a94100faSBill Paul 
3360a94100faSBill Paul static void
33617b5ffebfSPyun YongHyeon re_watchdog(struct rl_softc *sc)
33621d545c7aSMarius Strobl {
3363130b6dfbSPyun YongHyeon 	struct ifnet		*ifp;
3364a94100faSBill Paul 
33651d545c7aSMarius Strobl 	RL_LOCK_ASSERT(sc);
33661d545c7aSMarius Strobl 
33671d545c7aSMarius Strobl 	if (sc->rl_watchdog_timer == 0 || --sc->rl_watchdog_timer != 0)
33681d545c7aSMarius Strobl 		return;
33691d545c7aSMarius Strobl 
3370130b6dfbSPyun YongHyeon 	ifp = sc->rl_ifp;
3371a94100faSBill Paul 	re_txeof(sc);
3372130b6dfbSPyun YongHyeon 	if (sc->rl_ldata.rl_tx_free == sc->rl_ldata.rl_tx_desc_cnt) {
3373130b6dfbSPyun YongHyeon 		if_printf(ifp, "watchdog timeout (missed Tx interrupts) "
3374130b6dfbSPyun YongHyeon 		    "-- recovering\n");
3375130b6dfbSPyun YongHyeon 		if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3376d180a66fSPyun YongHyeon 			re_start_locked(ifp);
3377130b6dfbSPyun YongHyeon 		return;
3378130b6dfbSPyun YongHyeon 	}
3379130b6dfbSPyun YongHyeon 
3380130b6dfbSPyun YongHyeon 	if_printf(ifp, "watchdog timeout\n");
3381130b6dfbSPyun YongHyeon 	ifp->if_oerrors++;
3382130b6dfbSPyun YongHyeon 
33831abcdbd1SAttilio Rao 	re_rxeof(sc, NULL);
33848476c243SPyun YongHyeon 	ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
338597b9d4baSJohn-Mark Gurney 	re_init_locked(sc);
3386130b6dfbSPyun YongHyeon 	if (!IFQ_DRV_IS_EMPTY(&ifp->if_snd))
3387d180a66fSPyun YongHyeon 		re_start_locked(ifp);
3388a94100faSBill Paul }
3389a94100faSBill Paul 
3390a94100faSBill Paul /*
3391a94100faSBill Paul  * Stop the adapter and free any mbufs allocated to the
3392a94100faSBill Paul  * RX and TX lists.
3393a94100faSBill Paul  */
3394a94100faSBill Paul static void
33957b5ffebfSPyun YongHyeon re_stop(struct rl_softc *sc)
3396a94100faSBill Paul {
33970ce0868aSPyun YongHyeon 	int			i;
3398a94100faSBill Paul 	struct ifnet		*ifp;
3399d65abd66SPyun YongHyeon 	struct rl_txdesc	*txd;
3400d65abd66SPyun YongHyeon 	struct rl_rxdesc	*rxd;
3401a94100faSBill Paul 
340297b9d4baSJohn-Mark Gurney 	RL_LOCK_ASSERT(sc);
340397b9d4baSJohn-Mark Gurney 
3404fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
3405a94100faSBill Paul 
34061d545c7aSMarius Strobl 	sc->rl_watchdog_timer = 0;
3407d1754a9bSJohn Baldwin 	callout_stop(&sc->rl_stat_callout);
340813f4c340SRobert Watson 	ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
3409a94100faSBill Paul 
3410ead8fc66SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_CMDSTOP) != 0)
3411ead8fc66SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_STOPREQ | RL_CMD_TX_ENB |
3412ead8fc66SPyun YongHyeon 		    RL_CMD_RX_ENB);
3413ead8fc66SPyun YongHyeon 	else
3414a94100faSBill Paul 		CSR_WRITE_1(sc, RL_COMMAND, 0x00);
3415ead8fc66SPyun YongHyeon 	DELAY(1000);
3416a94100faSBill Paul 	CSR_WRITE_2(sc, RL_IMR, 0x0000);
3417ed510fb0SBill Paul 	CSR_WRITE_2(sc, RL_ISR, 0xFFFF);
3418a94100faSBill Paul 
3419a94100faSBill Paul 	if (sc->rl_head != NULL) {
3420a94100faSBill Paul 		m_freem(sc->rl_head);
3421a94100faSBill Paul 		sc->rl_head = sc->rl_tail = NULL;
3422a94100faSBill Paul 	}
3423a94100faSBill Paul 
3424a94100faSBill Paul 	/* Free the TX list buffers. */
3425a94100faSBill Paul 
3426d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_tx_desc_cnt; i++) {
3427d65abd66SPyun YongHyeon 		txd = &sc->rl_ldata.rl_tx_desc[i];
3428d65abd66SPyun YongHyeon 		if (txd->tx_m != NULL) {
3429d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3430d65abd66SPyun YongHyeon 			    txd->tx_dmamap, BUS_DMASYNC_POSTWRITE);
3431d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_tx_mtag,
3432d65abd66SPyun YongHyeon 			    txd->tx_dmamap);
3433d65abd66SPyun YongHyeon 			m_freem(txd->tx_m);
3434d65abd66SPyun YongHyeon 			txd->tx_m = NULL;
3435a94100faSBill Paul 		}
3436a94100faSBill Paul 	}
3437a94100faSBill Paul 
3438a94100faSBill Paul 	/* Free the RX list buffers. */
3439a94100faSBill Paul 
3440d65abd66SPyun YongHyeon 	for (i = 0; i < sc->rl_ldata.rl_rx_desc_cnt; i++) {
3441d65abd66SPyun YongHyeon 		rxd = &sc->rl_ldata.rl_rx_desc[i];
3442d65abd66SPyun YongHyeon 		if (rxd->rx_m != NULL) {
3443d65abd66SPyun YongHyeon 			bus_dmamap_sync(sc->rl_ldata.rl_tx_mtag,
3444d65abd66SPyun YongHyeon 			    rxd->rx_dmamap, BUS_DMASYNC_POSTREAD);
3445d65abd66SPyun YongHyeon 			bus_dmamap_unload(sc->rl_ldata.rl_rx_mtag,
3446d65abd66SPyun YongHyeon 			    rxd->rx_dmamap);
3447d65abd66SPyun YongHyeon 			m_freem(rxd->rx_m);
3448d65abd66SPyun YongHyeon 			rxd->rx_m = NULL;
3449a94100faSBill Paul 		}
3450a94100faSBill Paul 	}
3451a94100faSBill Paul }
3452a94100faSBill Paul 
3453a94100faSBill Paul /*
3454a94100faSBill Paul  * Device suspend routine.  Stop the interface and save some PCI
3455a94100faSBill Paul  * settings in case the BIOS doesn't restore them properly on
3456a94100faSBill Paul  * resume.
3457a94100faSBill Paul  */
3458a94100faSBill Paul static int
34597b5ffebfSPyun YongHyeon re_suspend(device_t dev)
3460a94100faSBill Paul {
3461a94100faSBill Paul 	struct rl_softc		*sc;
3462a94100faSBill Paul 
3463a94100faSBill Paul 	sc = device_get_softc(dev);
3464a94100faSBill Paul 
346597b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3466a94100faSBill Paul 	re_stop(sc);
34677467bd53SPyun YongHyeon 	re_setwol(sc);
3468a94100faSBill Paul 	sc->suspended = 1;
346997b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3470a94100faSBill Paul 
3471a94100faSBill Paul 	return (0);
3472a94100faSBill Paul }
3473a94100faSBill Paul 
3474a94100faSBill Paul /*
3475a94100faSBill Paul  * Device resume routine.  Restore some PCI settings in case the BIOS
3476a94100faSBill Paul  * doesn't, re-enable busmastering, and restart the interface if
3477a94100faSBill Paul  * appropriate.
3478a94100faSBill Paul  */
3479a94100faSBill Paul static int
34807b5ffebfSPyun YongHyeon re_resume(device_t dev)
3481a94100faSBill Paul {
3482a94100faSBill Paul 	struct rl_softc		*sc;
3483a94100faSBill Paul 	struct ifnet		*ifp;
3484a94100faSBill Paul 
3485a94100faSBill Paul 	sc = device_get_softc(dev);
348697b9d4baSJohn-Mark Gurney 
348797b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
348897b9d4baSJohn-Mark Gurney 
3489fc74a9f9SBrooks Davis 	ifp = sc->rl_ifp;
349061f45a72SPyun YongHyeon 	/* Take controller out of sleep mode. */
349161f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
349261f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
349361f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
349461f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) | 0x01);
349561f45a72SPyun YongHyeon 	}
3496a94100faSBill Paul 
34977467bd53SPyun YongHyeon 	/*
34987467bd53SPyun YongHyeon 	 * Clear WOL matching such that normal Rx filtering
34997467bd53SPyun YongHyeon 	 * wouldn't interfere with WOL patterns.
35007467bd53SPyun YongHyeon 	 */
35017467bd53SPyun YongHyeon 	re_clrwol(sc);
350201d1a6c3SPyun YongHyeon 
350301d1a6c3SPyun YongHyeon 	/* reinitialize interface if necessary */
350401d1a6c3SPyun YongHyeon 	if (ifp->if_flags & IFF_UP)
350501d1a6c3SPyun YongHyeon 		re_init_locked(sc);
350601d1a6c3SPyun YongHyeon 
3507a94100faSBill Paul 	sc->suspended = 0;
350897b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
3509a94100faSBill Paul 
3510a94100faSBill Paul 	return (0);
3511a94100faSBill Paul }
3512a94100faSBill Paul 
3513a94100faSBill Paul /*
3514a94100faSBill Paul  * Stop all chip I/O so that the kernel's probe routines don't
3515a94100faSBill Paul  * get confused by errant DMAs when rebooting.
3516a94100faSBill Paul  */
35176a087a87SPyun YongHyeon static int
35187b5ffebfSPyun YongHyeon re_shutdown(device_t dev)
3519a94100faSBill Paul {
3520a94100faSBill Paul 	struct rl_softc		*sc;
3521a94100faSBill Paul 
3522a94100faSBill Paul 	sc = device_get_softc(dev);
3523a94100faSBill Paul 
352497b9d4baSJohn-Mark Gurney 	RL_LOCK(sc);
3525a94100faSBill Paul 	re_stop(sc);
3526536fde34SMaxim Sobolev 	/*
3527536fde34SMaxim Sobolev 	 * Mark interface as down since otherwise we will panic if
3528536fde34SMaxim Sobolev 	 * interrupt comes in later on, which can happen in some
352972293673SRuslan Ermilov 	 * cases.
3530536fde34SMaxim Sobolev 	 */
3531536fde34SMaxim Sobolev 	sc->rl_ifp->if_flags &= ~IFF_UP;
35327467bd53SPyun YongHyeon 	re_setwol(sc);
353397b9d4baSJohn-Mark Gurney 	RL_UNLOCK(sc);
35346a087a87SPyun YongHyeon 
35356a087a87SPyun YongHyeon 	return (0);
3536a94100faSBill Paul }
35377467bd53SPyun YongHyeon 
35387467bd53SPyun YongHyeon static void
35397b5ffebfSPyun YongHyeon re_setwol(struct rl_softc *sc)
35407467bd53SPyun YongHyeon {
35417467bd53SPyun YongHyeon 	struct ifnet		*ifp;
35427467bd53SPyun YongHyeon 	int			pmc;
35437467bd53SPyun YongHyeon 	uint16_t		pmstat;
35447467bd53SPyun YongHyeon 	uint8_t			v;
35457467bd53SPyun YongHyeon 
35467467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
35477467bd53SPyun YongHyeon 
35487467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
35497467bd53SPyun YongHyeon 		return;
35507467bd53SPyun YongHyeon 
35517467bd53SPyun YongHyeon 	ifp = sc->rl_ifp;
355261f45a72SPyun YongHyeon 	/* Put controller into sleep mode. */
355361f45a72SPyun YongHyeon 	if ((sc->rl_flags & RL_FLAG_MACSLEEP) != 0) {
355461f45a72SPyun YongHyeon 		if ((CSR_READ_1(sc, RL_MACDBG) & 0x80) == 0x80)
355561f45a72SPyun YongHyeon 			CSR_WRITE_1(sc, RL_GPIO,
355661f45a72SPyun YongHyeon 			    CSR_READ_1(sc, RL_GPIO) & ~0x01);
355761f45a72SPyun YongHyeon 	}
3558886ff602SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3559886ff602SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_WOLRXENB) != 0)
3560886ff602SPyun YongHyeon 		CSR_WRITE_1(sc, RL_COMMAND, RL_CMD_RX_ENB);
35617467bd53SPyun YongHyeon 	/* Enable config register write. */
35627467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
35637467bd53SPyun YongHyeon 
35647467bd53SPyun YongHyeon 	/* Enable PME. */
35657467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG1);
35667467bd53SPyun YongHyeon 	v &= ~RL_CFG1_PME;
35677467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
35687467bd53SPyun YongHyeon 		v |= RL_CFG1_PME;
35697467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG1, v);
35707467bd53SPyun YongHyeon 
35717467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
35727467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
35737467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MAGIC) != 0)
35747467bd53SPyun YongHyeon 		v |= RL_CFG3_WOL_MAGIC;
35757467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
35767467bd53SPyun YongHyeon 
35777467bd53SPyun YongHyeon 	/* Config register write done. */
3578f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
35797467bd53SPyun YongHyeon 
35807467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
35817467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
35827467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
35837467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_UCAST) != 0)
35847467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_UCAST;
35857467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL_MCAST) != 0)
35867467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_MCAST | RL_CFG5_WOL_BCAST;
35877467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
35887467bd53SPyun YongHyeon 		v |= RL_CFG5_WOL_LANWAKE;
35897467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
35907467bd53SPyun YongHyeon 
3591d0c45156SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0 &&
3592d0c45156SPyun YongHyeon 	    (sc->rl_flags & RL_FLAG_PHYWAKE_PM) != 0)
3593d0c45156SPyun YongHyeon 		CSR_WRITE_1(sc, RL_PMCH, CSR_READ_1(sc, RL_PMCH) & ~0x80);
35947467bd53SPyun YongHyeon 	/*
35957467bd53SPyun YongHyeon 	 * It seems that hardware resets its link speed to 100Mbps in
35967467bd53SPyun YongHyeon 	 * power down mode so switching to 100Mbps in driver is not
35977467bd53SPyun YongHyeon 	 * needed.
35987467bd53SPyun YongHyeon 	 */
35997467bd53SPyun YongHyeon 
36007467bd53SPyun YongHyeon 	/* Request PME if WOL is requested. */
36017467bd53SPyun YongHyeon 	pmstat = pci_read_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, 2);
36027467bd53SPyun YongHyeon 	pmstat &= ~(PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE);
36037467bd53SPyun YongHyeon 	if ((ifp->if_capenable & IFCAP_WOL) != 0)
36047467bd53SPyun YongHyeon 		pmstat |= PCIM_PSTAT_PME | PCIM_PSTAT_PMEENABLE;
36057467bd53SPyun YongHyeon 	pci_write_config(sc->rl_dev, pmc + PCIR_POWER_STATUS, pmstat, 2);
36067467bd53SPyun YongHyeon }
36077467bd53SPyun YongHyeon 
36087467bd53SPyun YongHyeon static void
36097b5ffebfSPyun YongHyeon re_clrwol(struct rl_softc *sc)
36107467bd53SPyun YongHyeon {
36117467bd53SPyun YongHyeon 	int			pmc;
36127467bd53SPyun YongHyeon 	uint8_t			v;
36137467bd53SPyun YongHyeon 
36147467bd53SPyun YongHyeon 	RL_LOCK_ASSERT(sc);
36157467bd53SPyun YongHyeon 
36167467bd53SPyun YongHyeon 	if (pci_find_extcap(sc->rl_dev, PCIY_PMG, &pmc) != 0)
36177467bd53SPyun YongHyeon 		return;
36187467bd53SPyun YongHyeon 
36197467bd53SPyun YongHyeon 	/* Enable config register write. */
36207467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EE_MODE);
36217467bd53SPyun YongHyeon 
36227467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG3);
36237467bd53SPyun YongHyeon 	v &= ~(RL_CFG3_WOL_LINK | RL_CFG3_WOL_MAGIC);
36247467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG3, v);
36257467bd53SPyun YongHyeon 
36267467bd53SPyun YongHyeon 	/* Config register write done. */
3627f98dd8cfSPyun YongHyeon 	CSR_WRITE_1(sc, RL_EECMD, RL_EEMODE_OFF);
36287467bd53SPyun YongHyeon 
36297467bd53SPyun YongHyeon 	v = CSR_READ_1(sc, RL_CFG5);
36307467bd53SPyun YongHyeon 	v &= ~(RL_CFG5_WOL_BCAST | RL_CFG5_WOL_MCAST | RL_CFG5_WOL_UCAST);
36317467bd53SPyun YongHyeon 	v &= ~RL_CFG5_WOL_LANWAKE;
36327467bd53SPyun YongHyeon 	CSR_WRITE_1(sc, RL_CFG5, v);
36337467bd53SPyun YongHyeon }
36340534aae0SPyun YongHyeon 
36350534aae0SPyun YongHyeon static void
36360534aae0SPyun YongHyeon re_add_sysctls(struct rl_softc *sc)
36370534aae0SPyun YongHyeon {
36380534aae0SPyun YongHyeon 	struct sysctl_ctx_list	*ctx;
36390534aae0SPyun YongHyeon 	struct sysctl_oid_list	*children;
3640502be0f7SPyun YongHyeon 	int			error;
36410534aae0SPyun YongHyeon 
36420534aae0SPyun YongHyeon 	ctx = device_get_sysctl_ctx(sc->rl_dev);
36430534aae0SPyun YongHyeon 	children = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->rl_dev));
36440534aae0SPyun YongHyeon 
36450534aae0SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "stats",
36460534aae0SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, sc, 0, re_sysctl_stats, "I",
36470534aae0SPyun YongHyeon 	    "Statistics Information");
3648502be0f7SPyun YongHyeon 	if ((sc->rl_flags & (RL_FLAG_MSI | RL_FLAG_MSIX)) == 0)
3649502be0f7SPyun YongHyeon 		return;
3650502be0f7SPyun YongHyeon 
3651502be0f7SPyun YongHyeon 	SYSCTL_ADD_PROC(ctx, children, OID_AUTO, "int_rx_mod",
3652502be0f7SPyun YongHyeon 	    CTLTYPE_INT | CTLFLAG_RW, &sc->rl_int_rx_mod, 0,
3653502be0f7SPyun YongHyeon 	    sysctl_hw_re_int_mod, "I", "re RX interrupt moderation");
3654502be0f7SPyun YongHyeon 	/* Pull in device tunables. */
3655502be0f7SPyun YongHyeon 	sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3656502be0f7SPyun YongHyeon 	error = resource_int_value(device_get_name(sc->rl_dev),
3657502be0f7SPyun YongHyeon 	    device_get_unit(sc->rl_dev), "int_rx_mod", &sc->rl_int_rx_mod);
3658502be0f7SPyun YongHyeon 	if (error == 0) {
3659502be0f7SPyun YongHyeon 		if (sc->rl_int_rx_mod < RL_TIMER_MIN ||
3660502be0f7SPyun YongHyeon 		    sc->rl_int_rx_mod > RL_TIMER_MAX) {
3661502be0f7SPyun YongHyeon 			device_printf(sc->rl_dev, "int_rx_mod value out of "
3662502be0f7SPyun YongHyeon 			    "range; using default: %d\n",
3663502be0f7SPyun YongHyeon 			    RL_TIMER_DEFAULT);
3664502be0f7SPyun YongHyeon 			sc->rl_int_rx_mod = RL_TIMER_DEFAULT;
3665502be0f7SPyun YongHyeon 		}
3666502be0f7SPyun YongHyeon 	}
3667502be0f7SPyun YongHyeon 
36680534aae0SPyun YongHyeon }
36690534aae0SPyun YongHyeon 
36700534aae0SPyun YongHyeon static int
36710534aae0SPyun YongHyeon re_sysctl_stats(SYSCTL_HANDLER_ARGS)
36720534aae0SPyun YongHyeon {
36730534aae0SPyun YongHyeon 	struct rl_softc		*sc;
36740534aae0SPyun YongHyeon 	struct rl_stats		*stats;
36750534aae0SPyun YongHyeon 	int			error, i, result;
36760534aae0SPyun YongHyeon 
36770534aae0SPyun YongHyeon 	result = -1;
36780534aae0SPyun YongHyeon 	error = sysctl_handle_int(oidp, &result, 0, req);
36790534aae0SPyun YongHyeon 	if (error || req->newptr == NULL)
36800534aae0SPyun YongHyeon 		return (error);
36810534aae0SPyun YongHyeon 
36820534aae0SPyun YongHyeon 	if (result == 1) {
36830534aae0SPyun YongHyeon 		sc = (struct rl_softc *)arg1;
36840534aae0SPyun YongHyeon 		RL_LOCK(sc);
368516a4824bSPyun YongHyeon 		if ((sc->rl_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
368616a4824bSPyun YongHyeon 			RL_UNLOCK(sc);
368716a4824bSPyun YongHyeon 			goto done;
368816a4824bSPyun YongHyeon 		}
36890534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
36900534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_PREREAD);
36910534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_HI,
36920534aae0SPyun YongHyeon 		    RL_ADDR_HI(sc->rl_ldata.rl_stats_addr));
36930534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
36940534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr));
36950534aae0SPyun YongHyeon 		CSR_WRITE_4(sc, RL_DUMPSTATS_LO,
36960534aae0SPyun YongHyeon 		    RL_ADDR_LO(sc->rl_ldata.rl_stats_addr |
36970534aae0SPyun YongHyeon 		    RL_DUMPSTATS_START));
36980534aae0SPyun YongHyeon 		for (i = RL_TIMEOUT; i > 0; i--) {
36990534aae0SPyun YongHyeon 			if ((CSR_READ_4(sc, RL_DUMPSTATS_LO) &
37000534aae0SPyun YongHyeon 			    RL_DUMPSTATS_START) == 0)
37010534aae0SPyun YongHyeon 				break;
37020534aae0SPyun YongHyeon 			DELAY(1000);
37030534aae0SPyun YongHyeon 		}
37040534aae0SPyun YongHyeon 		bus_dmamap_sync(sc->rl_ldata.rl_stag,
37050534aae0SPyun YongHyeon 		    sc->rl_ldata.rl_smap, BUS_DMASYNC_POSTREAD);
37060534aae0SPyun YongHyeon 		RL_UNLOCK(sc);
37070534aae0SPyun YongHyeon 		if (i == 0) {
37080534aae0SPyun YongHyeon 			device_printf(sc->rl_dev,
37090534aae0SPyun YongHyeon 			    "DUMP statistics request timedout\n");
37100534aae0SPyun YongHyeon 			return (ETIMEDOUT);
37110534aae0SPyun YongHyeon 		}
371216a4824bSPyun YongHyeon done:
37130534aae0SPyun YongHyeon 		stats = sc->rl_ldata.rl_stats;
37140534aae0SPyun YongHyeon 		printf("%s statistics:\n", device_get_nameunit(sc->rl_dev));
37150534aae0SPyun YongHyeon 		printf("Tx frames : %ju\n",
37160534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_pkts));
37170534aae0SPyun YongHyeon 		printf("Rx frames : %ju\n",
37180534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_pkts));
37190534aae0SPyun YongHyeon 		printf("Tx errors : %ju\n",
37200534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_tx_errs));
37210534aae0SPyun YongHyeon 		printf("Rx errors : %u\n",
37220534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_errs));
37230534aae0SPyun YongHyeon 		printf("Rx missed frames : %u\n",
37240534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_missed_pkts));
37250534aae0SPyun YongHyeon 		printf("Rx frame alignment errs : %u\n",
37260534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_framealign_errs));
37270534aae0SPyun YongHyeon 		printf("Tx single collisions : %u\n",
37280534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_onecoll));
37290534aae0SPyun YongHyeon 		printf("Tx multiple collisions : %u\n",
37300534aae0SPyun YongHyeon 		    le32toh(stats->rl_tx_multicolls));
37310534aae0SPyun YongHyeon 		printf("Rx unicast frames : %ju\n",
37320534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_ucasts));
37330534aae0SPyun YongHyeon 		printf("Rx broadcast frames : %ju\n",
37340534aae0SPyun YongHyeon 		    (uintmax_t)le64toh(stats->rl_rx_bcasts));
37350534aae0SPyun YongHyeon 		printf("Rx multicast frames : %u\n",
37360534aae0SPyun YongHyeon 		    le32toh(stats->rl_rx_mcasts));
37370534aae0SPyun YongHyeon 		printf("Tx aborts : %u\n",
37380534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_tx_aborts));
37390534aae0SPyun YongHyeon 		printf("Tx underruns : %u\n",
37400534aae0SPyun YongHyeon 		    (uint32_t)le16toh(stats->rl_rx_underruns));
37410534aae0SPyun YongHyeon 	}
37420534aae0SPyun YongHyeon 
37430534aae0SPyun YongHyeon 	return (error);
37440534aae0SPyun YongHyeon }
3745502be0f7SPyun YongHyeon 
3746502be0f7SPyun YongHyeon static int
3747502be0f7SPyun YongHyeon sysctl_int_range(SYSCTL_HANDLER_ARGS, int low, int high)
3748502be0f7SPyun YongHyeon {
3749502be0f7SPyun YongHyeon 	int error, value;
3750502be0f7SPyun YongHyeon 
3751502be0f7SPyun YongHyeon 	if (arg1 == NULL)
3752502be0f7SPyun YongHyeon 		return (EINVAL);
3753502be0f7SPyun YongHyeon 	value = *(int *)arg1;
3754502be0f7SPyun YongHyeon 	error = sysctl_handle_int(oidp, &value, 0, req);
3755502be0f7SPyun YongHyeon 	if (error || req->newptr == NULL)
3756502be0f7SPyun YongHyeon 		return (error);
3757502be0f7SPyun YongHyeon 	if (value < low || value > high)
3758502be0f7SPyun YongHyeon 		return (EINVAL);
3759502be0f7SPyun YongHyeon 	*(int *)arg1 = value;
3760502be0f7SPyun YongHyeon 
3761502be0f7SPyun YongHyeon 	return (0);
3762502be0f7SPyun YongHyeon }
3763502be0f7SPyun YongHyeon 
3764502be0f7SPyun YongHyeon static int
3765502be0f7SPyun YongHyeon sysctl_hw_re_int_mod(SYSCTL_HANDLER_ARGS)
3766502be0f7SPyun YongHyeon {
3767502be0f7SPyun YongHyeon 
3768502be0f7SPyun YongHyeon 	return (sysctl_int_range(oidp, arg1, arg2, req, RL_TIMER_MIN,
3769502be0f7SPyun YongHyeon 	    RL_TIMER_MAX));
3770502be0f7SPyun YongHyeon }
3771